mirror of
https://github.com/chanzuckerberg/cellxgene.git
synced 2026-09-27 05:38:12 +08:00
Apply yapf to python files
This commit is contained in:
@@ -14,6 +14,7 @@ from server.app.util.data_locator import DataLocator
|
||||
|
||||
|
||||
class WritableAnnotationTest(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
self.tmpDir = tempfile.mkdtemp()
|
||||
self.annotations_file = path.join(self.tmpDir, "test_annotations.csv")
|
||||
@@ -27,7 +28,8 @@ class WritableAnnotationTest(unittest.TestCase):
|
||||
"annotations_file": self.annotations_file,
|
||||
"annotations_output_dir": None
|
||||
}
|
||||
self.data = ScanpyEngine(DataLocator("../example-dataset/pbmc3k.h5ad"), args)
|
||||
self.data = ScanpyEngine(DataLocator("../example-dataset/pbmc3k.h5ad"),
|
||||
args)
|
||||
|
||||
def tearDown(self):
|
||||
shutil.rmtree(self.tmpDir)
|
||||
@@ -41,7 +43,9 @@ class WritableAnnotationTest(unittest.TestCase):
|
||||
|
||||
n_rows = self.data.data.obs.shape[0]
|
||||
fbs_bad = self.make_fbs({
|
||||
'louvain': pd.Series(['undefined' for l in range(0, n_rows)], dtype='category')
|
||||
'louvain':
|
||||
pd.Series(['undefined' for l in range(0, n_rows)],
|
||||
dtype='category')
|
||||
})
|
||||
|
||||
# ensure attempt to change VAR annotation
|
||||
@@ -56,31 +60,49 @@ class WritableAnnotationTest(unittest.TestCase):
|
||||
# verify the file is written as expected
|
||||
n_rows = self.data.data.obs.shape[0]
|
||||
fbs = self.make_fbs({
|
||||
'cat_A': pd.Series(['label_A' for l in range(0, n_rows)], dtype='category'),
|
||||
'cat_B': pd.Series(['label_B' for l in range(0, n_rows)], dtype='category')
|
||||
'cat_A':
|
||||
pd.Series(['label_A' for l in range(0, n_rows)],
|
||||
dtype='category'),
|
||||
'cat_B':
|
||||
pd.Series(['label_B' for l in range(0, n_rows)],
|
||||
dtype='category')
|
||||
})
|
||||
res = self.data.annotation_put_fbs("obs", fbs)
|
||||
self.assertEqual(res, json.dumps({"status": "OK"}))
|
||||
self.assertTrue(path.exists(self.annotations_file))
|
||||
df = pd.read_csv(self.annotations_file, index_col=0, header=0, comment='#')
|
||||
df = pd.read_csv(self.annotations_file,
|
||||
index_col=0,
|
||||
header=0,
|
||||
comment='#')
|
||||
self.assertEqual(df.shape, (n_rows, 2))
|
||||
self.assertEqual(set(df.columns), set(['cat_A', 'cat_B']))
|
||||
self.assertTrue(self.data.original_obs_index.equals(df.index))
|
||||
self.assertTrue(np.all(df['cat_A'] == ['label_A' for l in range(0, n_rows)]))
|
||||
self.assertTrue(np.all(df['cat_B'] == ['label_B' for l in range(0, n_rows)]))
|
||||
self.assertTrue(
|
||||
np.all(df['cat_A'] == ['label_A' for l in range(0, n_rows)]))
|
||||
self.assertTrue(
|
||||
np.all(df['cat_B'] == ['label_B' for l in range(0, n_rows)]))
|
||||
|
||||
# verify complete overwrite on second attempt, AND rotation occurs
|
||||
fbs = self.make_fbs({
|
||||
'cat_A': pd.Series(['label_A1' for l in range(0, n_rows)], dtype='category'),
|
||||
'cat_C': pd.Series(['label_C' for l in range(0, n_rows)], dtype='category')
|
||||
'cat_A':
|
||||
pd.Series(['label_A1' for l in range(0, n_rows)],
|
||||
dtype='category'),
|
||||
'cat_C':
|
||||
pd.Series(['label_C' for l in range(0, n_rows)],
|
||||
dtype='category')
|
||||
})
|
||||
res = self.data.annotation_put_fbs("obs", fbs)
|
||||
self.assertEqual(res, json.dumps({"status": "OK"}))
|
||||
self.assertTrue(path.exists(self.annotations_file))
|
||||
df = pd.read_csv(self.annotations_file, index_col=0, header=0, comment='#')
|
||||
df = pd.read_csv(self.annotations_file,
|
||||
index_col=0,
|
||||
header=0,
|
||||
comment='#')
|
||||
self.assertEqual(set(df.columns), set(['cat_A', 'cat_C']))
|
||||
self.assertTrue(np.all(df['cat_A'] == ['label_A1' for l in range(0, n_rows)]))
|
||||
self.assertTrue(np.all(df['cat_C'] == ['label_C' for l in range(0, n_rows)]))
|
||||
self.assertTrue(
|
||||
np.all(df['cat_A'] == ['label_A1' for l in range(0, n_rows)]))
|
||||
self.assertTrue(
|
||||
np.all(df['cat_C'] == ['label_C' for l in range(0, n_rows)]))
|
||||
|
||||
# rotation
|
||||
name, ext = path.splitext(self.annotations_file)
|
||||
@@ -93,8 +115,12 @@ class WritableAnnotationTest(unittest.TestCase):
|
||||
# verify we stop rotation at 9
|
||||
n_rows = self.data.data.obs.shape[0]
|
||||
fbs = self.make_fbs({
|
||||
'cat_A': pd.Series(['label_A' for l in range(0, n_rows)], dtype='category'),
|
||||
'cat_B': pd.Series(['label_B' for l in range(0, n_rows)], dtype='category')
|
||||
'cat_A':
|
||||
pd.Series(['label_A' for l in range(0, n_rows)],
|
||||
dtype='category'),
|
||||
'cat_B':
|
||||
pd.Series(['label_B' for l in range(0, n_rows)],
|
||||
dtype='category')
|
||||
})
|
||||
for i in range(0, 11):
|
||||
res = self.data.annotation_put_fbs("obs", fbs)
|
||||
@@ -112,8 +138,12 @@ class WritableAnnotationTest(unittest.TestCase):
|
||||
|
||||
n_rows = self.data.data.obs.shape[0]
|
||||
fbs = self.make_fbs({
|
||||
'cat_A': pd.Series(['label_A' for l in range(0, n_rows)], dtype='category'),
|
||||
'cat_B': pd.Series(['label_B' for l in range(0, n_rows)], dtype='category')
|
||||
'cat_A':
|
||||
pd.Series(['label_A' for l in range(0, n_rows)],
|
||||
dtype='category'),
|
||||
'cat_B':
|
||||
pd.Series(['label_B' for l in range(0, n_rows)],
|
||||
dtype='category')
|
||||
})
|
||||
|
||||
# put
|
||||
@@ -129,27 +159,30 @@ class WritableAnnotationTest(unittest.TestCase):
|
||||
self.assertEqual(annotations["n_cols"], 7)
|
||||
self.assertIsNone(annotations["row_idx"])
|
||||
self.assertEqual(annotations["col_idx"], [
|
||||
obs_index_col_name, "n_genes", "percent_mito", "n_counts", "louvain", "cat_A", "cat_B"
|
||||
obs_index_col_name, "n_genes", "percent_mito", "n_counts",
|
||||
"louvain", "cat_A", "cat_B"
|
||||
])
|
||||
col_idx = annotations["col_idx"]
|
||||
self.assertEqual(annotations["columns"][col_idx.index('cat_A')], [
|
||||
'label_A' for l in range(0, n_rows)
|
||||
])
|
||||
self.assertEqual(annotations["columns"][col_idx.index('cat_B')], [
|
||||
'label_B' for l in range(0, n_rows)
|
||||
])
|
||||
self.assertEqual(annotations["columns"][col_idx.index('cat_A')],
|
||||
['label_A' for l in range(0, n_rows)])
|
||||
self.assertEqual(annotations["columns"][col_idx.index('cat_B')],
|
||||
['label_B' for l in range(0, n_rows)])
|
||||
|
||||
# verify the schema was updated
|
||||
all_col_schema = {c["name"]: c for c in schema["annotations"]["obs"]["columns"]}
|
||||
self.assertEqual(all_col_schema["cat_A"], {
|
||||
"name": "cat_A",
|
||||
"type": "categorical",
|
||||
"categories": ["label_A"],
|
||||
"writable": True
|
||||
})
|
||||
self.assertEqual(all_col_schema["cat_B"], {
|
||||
"name": "cat_B",
|
||||
"type": "categorical",
|
||||
"categories": ["label_B"],
|
||||
"writable": True
|
||||
})
|
||||
all_col_schema = {
|
||||
c["name"]: c for c in schema["annotations"]["obs"]["columns"]
|
||||
}
|
||||
self.assertEqual(
|
||||
all_col_schema["cat_A"], {
|
||||
"name": "cat_A",
|
||||
"type": "categorical",
|
||||
"categories": ["label_A"],
|
||||
"writable": True
|
||||
})
|
||||
self.assertEqual(
|
||||
all_col_schema["cat_B"], {
|
||||
"name": "cat_B",
|
||||
"type": "categorical",
|
||||
"categories": ["label_B"],
|
||||
"writable": True
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user