mirror of
https://github.com/chanzuckerberg/cellxgene.git
synced 2026-10-03 19:28:12 +08:00
Merge pull request #156 from chanzuckerberg/csweaver/infer-metadata
Infer schema if none exists
This commit is contained in:
@@ -39,11 +39,38 @@ class ScanpyEngine(CXGDriver):
|
|||||||
def _load_data(data):
|
def _load_data(data):
|
||||||
return sc.read(os.path.join(data, "data.h5ad"))
|
return sc.read(os.path.join(data, "data.h5ad"))
|
||||||
|
|
||||||
@staticmethod
|
def _load_or_infer_schema(self, data, schema):
|
||||||
def _load_or_infer_schema(data, schema):
|
if not os.path.isfile(os.path.join(data, schema)):
|
||||||
data_schema = None
|
# Initialize with cell name which is built off the index
|
||||||
if not schema:
|
data_schema = {
|
||||||
pass
|
"CellName": {
|
||||||
|
"type": "string",
|
||||||
|
"variabletype": "categorical",
|
||||||
|
"displayname": "Name",
|
||||||
|
"include": True
|
||||||
|
}
|
||||||
|
}
|
||||||
|
metadata_fields = list(self.data.obs)
|
||||||
|
for m in metadata_fields:
|
||||||
|
# Since there are many type of float/int in numpy datatypes the kind attribute of a datatype object
|
||||||
|
# offers a decent insight into whether it can be lumped in with floats or ints, which is what we
|
||||||
|
# care about here.
|
||||||
|
data_kind = self.data.obs[m].dtype.kind
|
||||||
|
variable_type = "categorical"
|
||||||
|
data_type = "string"
|
||||||
|
if data_kind == 'f':
|
||||||
|
variable_type = "continuous"
|
||||||
|
data_type = "float"
|
||||||
|
elif data_kind in ['i', 'u']:
|
||||||
|
data_type = "int"
|
||||||
|
if self.data.obs[m].nunique() > 50:
|
||||||
|
variable_type = "continuous"
|
||||||
|
data_schema[m] = {
|
||||||
|
"type": data_type,
|
||||||
|
"variabletype": variable_type,
|
||||||
|
"displayname": m,
|
||||||
|
"include": True
|
||||||
|
}
|
||||||
else:
|
else:
|
||||||
data_schema = parse_schema(os.path.join(data, schema))
|
data_schema = parse_schema(os.path.join(data, schema))
|
||||||
return data_schema
|
return data_schema
|
||||||
|
|||||||
Reference in New Issue
Block a user