diff --git a/scripts/attributes/master.py b/scripts/attributes/master.py index 4ceb1eac22d0821c85e307a14690dbe55e842c29..e466b7f76983bdb3ada7d09e242f459811618561 100644 --- a/scripts/attributes/master.py +++ b/scripts/attributes/master.py @@ -4,6 +4,7 @@ import h5py from .base_attributes import base_attributes from .map_objects import map_objects from .genes import write_genes_table +from .morphology import write_morphology_cells, write_morphology_nuclei from ..files import get_h5_path_from_xml @@ -49,7 +50,11 @@ def make_cell_tables(folder, name, tmp_folder, resolution, if not os.path.exists(aux_gene_path): raise RuntimeError("Can't find auxiliary gene file") gene_out = os.path.join(table_folder, 'genes.csv') - write_genes_table(seg_path, aux_gene_path, gene_out, label_ids) + # write_genes_table(seg_path, aux_gene_path, gene_out, label_ids) + + # make table with morphology + morpho_out = os.path.join(table_folder, 'morphology.csv') + write_morphology_cells(seg_path, base_out, map_out, morpho_out) # TODO additional tables: # regions / semantics @@ -71,10 +76,9 @@ def make_nucleus_tables(folder, name, tmp_folder, resolution, tmp_folder, target=target, max_jobs=max_jobs, correct_anchors=True) - # TODO do we need this for nuclei as well ? - # make table with mapping to other objects - # cells, ... + # make the morphology attribute table + morpho_out = os.path.join(table_folder, 'morphology.csv') + write_morphology_nuclei(seg_path, base_out, morpho_out) # TODO additional tables: - # kimberly's nucleus attributes # ??? diff --git a/test/attributes/test_base.py b/test/attributes/test_base.py new file mode 100644 index 0000000000000000000000000000000000000000..00dba67890f729478e0f139a9744d58b8245c110 --- /dev/null +++ b/test/attributes/test_base.py @@ -0,0 +1,65 @@ +import unittest +import sys +import os +import pandas +import h5py +from shutil import rmtree +sys.path.append('../..') + + +# check new version of gene mapping against original +class TestGeneAttributes(unittest.TestCase): + tmp_folder = 'tmp' + + def tearDown(self): + try: + rmtree(self.tmp_folder) + except OSError: + pass + + def test_genes(self): + from scripts.attributes.base_attributes import base_attributes + + input_path = '../../data/0.0.0/segmentations/em-segmented-nuclei-labels.h5' + input_key = 't00000/s00/0/cells' + output_path = os.path.join(self.tmp_folder, 'table-test.csv') + target = 'local' + max_jobs = 32 + resolution = [0.1, 0.08, 0.08] + base_attributes(input_path, input_key, output_path, resolution, + self.tmp_folder, target, max_jobs, correct_anchors=False) + + table = pandas.read_csv(output_path, sep='\t') + print("Checking attributes ...") + with h5py.File(input_path, 'r') as f: + ds = f[input_key] + for row in table.itertuples(index=False): + label_id = int(row.label_id) + if label_id == 0: + continue + + # NOTE we increase the bounding box by 1 due to potential rounding artifacts + # check bounding box + bb_min = [row.bb_min_z, row.bb_min_y, row.bb_min_x] + bb_max = [row.bb_max_z, row.bb_max_y, row.bb_max_x] + bb = tuple(slice(int(min_ / res) - 1, int(max_ / res) + 2) + for min_, max_, res in zip(bb_min, bb_max, resolution)) + seg = ds[bb] + + # TODO check anchor once we have anchor correction + # anchor = [row.anchor_z, row.anchor_y, row.anchor_x] + # anchor = tuple(anch // res - b.start for anch, res, b in zip(anchor, resolution, bb)) + # anchor_id = seg[anchor] + # self.assertEqual(anchor_id, label_id) + + label_mask = seg == label_id + n_pixels = label_mask.sum() + self.assertGreater(n_pixels, 0) + + # check the pixel size + pixel_size = int(row.n_pixels) + self.assertEqual(pixel_size, n_pixels) + + +if __name__ == '__main__': + unittest.main()