From eddbc834f2c3af153a6b9519d02187971c1dbec5 Mon Sep 17 00:00:00 2001
From: Constantin Pape <constantin.pape@iwr.uni-heidelberg.de>
Date: Tue, 11 Jun 2019 15:50:32 +0200
Subject: [PATCH] Add test for morphology attributes

---
 .gitignore                         |  1 +
 scripts/attributes/master.py       |  2 +-
 test/attributes/test_morphology.py | 60 ++++++++++++++++++++++++++++++
 3 files changed, 62 insertions(+), 1 deletion(-)
 create mode 100644 test/attributes/test_morphology.py

diff --git a/.gitignore b/.gitignore
index 8d98bb0..bfcdccd 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,2 +1,3 @@
 *.h5
 __pycache__/
+tmp*
diff --git a/scripts/attributes/master.py b/scripts/attributes/master.py
index e466b7f..ce8a00a 100644
--- a/scripts/attributes/master.py
+++ b/scripts/attributes/master.py
@@ -50,7 +50,7 @@ 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')
diff --git a/test/attributes/test_morphology.py b/test/attributes/test_morphology.py
new file mode 100644
index 0000000..f92f87e
--- /dev/null
+++ b/test/attributes/test_morphology.py
@@ -0,0 +1,60 @@
+import unittest
+import sys
+import os
+import numpy as np
+sys.path.append('../..')
+
+
+# check new version of gene mapping against original
+class TestMorphologyAttributes(unittest.TestCase):
+    test_file = 'test_table.csv'
+
+    def tearDown(self):
+        try:
+            os.remove(self.test_file)
+        except OSError:
+            pass
+
+    def load_table(self, table_file):
+        table = np.genfromtxt(table_file, delimiter='\t', skip_header=1,
+                              dtype='float32')
+        return table
+
+    def test_nucleus_morphology(self):
+        from scripts.attributes.morphology import write_morphology_nuclei
+
+        # compute and load the morpho table
+        seg_path = '../../data/0.0.0/segmentations/em-segmented-nuclei-labels.h5'
+        table_in_path = '../../data/0.0.0/tables/em-segmented-nuclei-labels/default.csv'
+        table_out_path = self.test_file
+        print("Start computation ...")
+        write_morphology_nuclei(seg_path, table_in_path, table_out_path)
+        table = self.load_table(table_out_path)
+
+        # load original table, make sure new and old table agree
+        original_table_file = '../../data/0.0.0/tables/em-segmented-nuclei-labels/morphology.csv'
+        original_table = self.load_table(original_table_file)
+        self.assertEqual(table.shape, original_table.shape)
+        self.assertTrue(np.allclose(table, original_table))
+
+    def test_cell_morphology(self):
+        from scripts.attributes.morphology import write_morphology_cells
+
+        # compute and load the morpho table
+        seg_path = '../../data/0.0.0/segmentations/em-segmented-cells-labels.h5'
+        mapping_path = '../../data/0.0.0/tables/em-segmented-cells-labels/objects.csv'
+        table_in_path = '../../data/0.0.0/tables/em-segmented-cells-labels/default.csv'
+        table_out_path = self.test_file
+        print("Start computation ...")
+        write_morphology_cells(seg_path, table_in_path, mapping_path, table_out_path)
+        table = self.load_table(table_out_path)
+
+        # load original table, make sure new and old table agree
+        original_table_file = '../../data/0.0.0/tables/em-segmented-cells-labels/morphology.csv'
+        original_table = self.load_table(original_table_file)
+        self.assertEqual(table.shape, original_table.shape)
+        self.assertTrue(np.allclose(table, original_table))
+
+
+if __name__ == '__main__':
+    unittest.main()
-- 
GitLab