ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/osprai/osprai/trunk/io_module.py
(Generate patch)
# Line 26 | Line 26
26   """
27   __version__ = "120820"
28  
29 + ## To do:
30 + ##   Use more Numpy functions to import data. It can handle strings.
31 + ##   See <http://docs.scipy.org/doc/numpy/reference/routines.array-creation.html>
32 + ##   Consider np.tostring, np.fromstring, np.load, np.save, np.loadtxt
33 + ##   Then I can get rid of TsvTable.
34 +
35  
36   ## Import libraries.
37   from copy import deepcopy
# Line 455 | Line 461
461        This file has 4 datapoints for 2 ROIs.
462      """
463      
464 <    ## Try to open file. Return tiny ba object if it fails.
464 >    ## Open file using Numpy. Return tiny ba object if it fails.
465      ba0 = ba.BiosensorArray(1,1)
466      try:
467 <        f = open(fname, 'r')
462 <        txt = f.read()  ## Convert the file to a string.
463 <        f.close()
467 >        d = np.genfromtxt(fname, delimiter=',', dtype=float)
468      except IOError:
469          print "Error: Cannot open file %s for reading" % fname
470 +        return ba0  
471 +    ## Check the dimensions of the new Numpy 2D array.
472 +    (dpoints, rois) = d.shape
473 +    rois -= 1
474 +    if (rois<1) or (dpoints<2):
475 +        print "Error: not enough data in %s" % fname
476          return ba0
477      
478 <    ## Create a table object, if there are 2+ columns.
469 <    tsv = TsvTable(delimiter=',')
470 <    tsv.read(txt)  ## Convert the string to a table.
471 <    rois, dpoints = (tsv.columns-1), tsv.rows
472 <    if (rois<1):
473 <        print "Error: not enough columns in %s" % fname
474 <        return ba0
475 <    
476 <    ## Create the ba object.
478 >    ## Redimension ba and copy the columns of data.
479      ba0 = ba.BiosensorArray(rois, dpoints)
480      for i in xrange(rois):
481 <        ba0.roi[i].time = np.array(tsv.getcolumn(0), dtype=float)
482 <        ba0.roi[i].value = np.array(tsv.getcolumn(i+1), dtype=float)
481 <    
481 >        ba0.roi[i].time = d[:,0]
482 >        ba0.roi[i].value = d[:,(i+1)]  
483      print "This file has %i datapoints for %i ROIs." % (dpoints, rois)
484      return ba0
485      """End of readcsv()"""

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines