linnarsson-lab / loom-viewer

Tool for sharing, browsing and visualizing single-cell data stored in the Loom file format
BSD 2-Clause "Simplified" License
35 stars 6 forks source link

loompy.create_from_cef assumes data is stored in arrays instead of lists #67

Closed gioelelm closed 7 years ago

gioelelm commented 7 years ago

This code throws an error:

loompy.create_from_cef('Yiwen/Yiwen_clustering_v2.cef', 'Yiwen/Yiwen_clustering_v2.loom')

I tried to solve by changing:

for ix in xrange(len(self.row_attr_names)):
            row_types[self.row_attr_names[ix]] = "float64" if np.issubdtype(self.row_attr_values[ix].dtype, np.number) else "string" 

to:

for ix in xrange(len(self.row_attr_names)):
            row_types[self.row_attr_names[ix]] = "float64" if np.issubdtype(np.array(self.row_attr_values[ix]).dtype, np.number) else "string" 

but I get another error this time:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-3-3bc46a868eb4> in <module>()
----> 1 loompy.create_from_cef('Yiwen/Yiwen_clustering_v2.cef', 'Yiwen/Yiwen_clustering_v2.loom')

/Users/admin/anaconda/lib/python2.7/site-packages/loompy/loompy.py in create_from_cef(cef_file, loom_file)
    104         cef = _CEF()
    105         cef.readCEF(cef_file)
--> 106         cef.export_as_loom(loom_file)
    107 
    108 def create_from_pandas(df, loom_file):

/Users/admin/anaconda/lib/python2.7/site-packages/loompy/loompy.py in export_as_loom(self, filename)
   1291                         col_types[self.col_attr_names[ix]] = "float64" if np.issubdtype(np.array(self.col_attr_values[ix]).dtype, np.number) else "string"
   1292 
-> 1293                 create(filename, self.matrix, dict(zip(self.row_attr_names, self.row_attr_values)), dict(zip(self.col_attr_names, self.col_attr_values)), row_types, col_types)
   1294 
   1295         def add_header(self, name, value):

/Users/admin/anaconda/lib/python2.7/site-packages/loompy/loompy.py in create(filename, matrix, row_attrs, col_attrs, row_attr_types, col_attr_types)
     80                 if not row_attr_types.has_key(key):
     81                         raise ValueError, "Type information missing for row attribute " + key
---> 82                 ds.set_attr(key, vals, axis = 0, dtype=row_attr_types[key])
     83 
     84         for key, vals in col_attrs.iteritems():

/Users/admin/anaconda/lib/python2.7/site-packages/loompy/loompy.py in set_attr(self, name, values, axis, dtype)
    571                         raise TypeError, "Data type must be provided"
    572 
--> 573                 values = values.astype(dtype)
    574 
    575                 if dtype != "int" and dtype != "float64" and dtype != "string":

AttributeError: 'list' object has no attribute 'astype'

Probably the error is still becouse array is expected instead of lists

gioelelm commented 7 years ago

Fixed by 1b8754aa2021d8abfe90a640c40f87e054a7e014