mirror of
https://github.com/chanzuckerberg/cellxgene.git
synced 2026-09-24 08:58:12 +08:00
Format loaded dataset
- create cell and gene ids - recast numbers to float32/int32
This commit is contained in:
@@ -13,6 +13,7 @@ class ScanpyEngine(CXGDriver):
|
||||
|
||||
def __init__(self, data, schema=None, graph_method="umap", diffexp_method="ttest"):
|
||||
self.data = self._load_data(data)
|
||||
self._format_data()
|
||||
self.schema = self._load_or_infer_schema(data, schema)
|
||||
self._set_cell_names()
|
||||
self.cell_count = self.data.shape[0]
|
||||
@@ -39,6 +40,7 @@ class ScanpyEngine(CXGDriver):
|
||||
def _load_data(data):
|
||||
return sc.read(os.path.join(data, "data.h5ad"))
|
||||
|
||||
# TODO delete after v0.1 v2.0 transition
|
||||
def _load_or_infer_schema(self, data, schema):
|
||||
if not os.path.isfile(os.path.join(data, schema)):
|
||||
# Initialize with cell name which is built off the index
|
||||
@@ -75,6 +77,23 @@ class ScanpyEngine(CXGDriver):
|
||||
data_schema = parse_schema(os.path.join(data, schema))
|
||||
return data_schema
|
||||
|
||||
def _format_data(self):
|
||||
# ensure gene
|
||||
self.data.var["name"] = list(self.data.var.index)
|
||||
self.data.var.index = list(range(self.data.var.shape[0]))
|
||||
# ensure cell name
|
||||
self.data.obs["name"] = list(self.data.obs.index)
|
||||
self.data.obs.index = list(range(self.data.obs.shape[0]))
|
||||
# ensure formats correct/ reform old format
|
||||
for annotation in self.data.obs:
|
||||
data_type = self.data.obs[annotation].dtype
|
||||
if data_type.kind == 'f' and data_type != "float32":
|
||||
self.data.obs[annotation] = self.data.obs[annotation].astype("float32")
|
||||
elif data_type.kind in ['i', 'u'] and data_type != "int32":
|
||||
self.data.obs[annotation] = self.data.obs[annotation].astype("int32")
|
||||
if self.data.X.dtype != 'float32':
|
||||
self.data.X = self.data.X.astype("float32")
|
||||
|
||||
def cells(self):
|
||||
return list(self.data.obs.index)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user