From 305c6d659553b8149c55e1d2598131ab14e9c757 Mon Sep 17 00:00:00 2001
From: Constantin Pape <constantin.pape@iwr.uni-heidelberg.de>
Date: Mon, 28 Oct 2019 13:11:46 +0100
Subject: [PATCH] Update segmentation export

---
 data/postprocess.json                 |  2 +-
 scripts/export/export_segmentation.py | 33 +++++++++++++++++----------
 scripts/export/to_bdv.py              |  3 ++-
 3 files changed, 24 insertions(+), 14 deletions(-)

diff --git a/data/postprocess.json b/data/postprocess.json
index d22735c..d83f671 100644
--- a/data/postprocess.json
+++ b/data/postprocess.json
@@ -1 +1 @@
-{"sbem-6dpf-1-whole-segmented-cells-labels": {"boundary_path": "/g/kreshuk/data/arendt/platyneris_v1/data.n5", "boundary_key": "volumes/affinities/s1", "min_segment_size": 88604, "label_segmentation": true}}
\ No newline at end of file
+{"sbem-6dpf-1-whole-segmented-cells-labels": {"boundary_path": "/g/kreshuk/data/arendt/platyneris_v1/data.n5", "boundary_key": "volumes/affinities/s1", "max_segment_number": 32700, "label_segmentation": false}}
diff --git a/scripts/export/export_segmentation.py b/scripts/export/export_segmentation.py
index df64f95..4fbe9dc 100644
--- a/scripts/export/export_segmentation.py
+++ b/scripts/export/export_segmentation.py
@@ -6,7 +6,7 @@ import z5py
 from cluster_tools.downscaling import DownscalingWorkflow
 from paintera_tools import serialize_from_commit, postprocess
 from paintera_tools import set_default_shebang as set_ptools_shebang
-from .to_bdv import to_bdv
+from .to_bdv import to_bdv, check_max_id
 from .map_segmentation_ids import map_segmentation_ids
 from ..default_config import write_default_global_config, get_default_shebang
 from ..files import get_postprocess_dict
@@ -73,15 +73,20 @@ def export_segmentation(paintera_path, paintera_key, folder, new_folder, name, r
     tmp_key = 'seg'
     tmp_key0 = os.path.join(tmp_key, 's0')
 
+    # set correct shebang for paintera tools
+    set_ptools_shebang(get_default_shebang())
+
     # run post-processing if specified for this segmentation name
     pp_dict = get_postprocess_dict()
     run_postprocessing = name in pp_dict
-
     if run_postprocessing:
         pp_config = pp_dict[name]
         boundary_path = pp_config['boundary_path']
         boundary_key = pp_config['boundary_key']
-        min_segment_size = pp_config['min_segment_size']
+
+        min_segment_size = pp_config.get('min_segment_size', None)
+        max_segment_number = pp_config.get('max_segment_number', None)
+
         label_segmentation = pp_config['label_segmentation']
         tmp_postprocess = os.path.join(tmp_folder, 'postprocess_paintera')
         postprocess(paintera_path, paintera_key,
@@ -89,15 +94,19 @@ def export_segmentation(paintera_path, paintera_key, folder, new_folder, name, r
                     tmp_folder=tmp_postprocess,
                     target=target, max_jobs=max_jobs,
                     n_threads=16, size_threshold=min_segment_size,
-                    label=label_segmentation)
-
-    # set correct shebang for paintera tools
-    set_ptools_shebang(get_default_shebang())
-
-    # NOTE map_to_background is needed for cilia, nee some way to enable it automatically
-    # export segmentation from paintera commit for all scales
-    serialize_from_commit(paintera_path, paintera_key, tmp_path, tmp_key0, tmp_folder,
-                          max_jobs, target, relabel_output=True)  # ,  map_to_background=[1])
+                    target_number=max_segment_number,
+                    label=label_segmentation,
+                    output_path=tmp_path, output_key=tmp_key0)
+
+    else:
+        # NOTE map_to_background is needed for cilia, need some way to enable it automatically
+        # export segmentation from paintera commit for all scales
+        serialize_from_commit(paintera_path, paintera_key, tmp_path, tmp_key0, tmp_folder,
+                              max_jobs, target, relabel_output=True)  # ,  map_to_background=[1])
+
+    # check for overflow
+    print("Check max-id @", tmp_path, tmp_key0)
+    check_max_id(tmp_path, tmp_key0)
 
     # downscale the segemntation
     scale_factors = get_scale_factors(paintera_path, paintera_key)
diff --git a/scripts/export/to_bdv.py b/scripts/export/to_bdv.py
index 479b1c3..4a39eaf 100644
--- a/scripts/export/to_bdv.py
+++ b/scripts/export/to_bdv.py
@@ -2,6 +2,7 @@ import os
 import json
 import luigi
 
+import numpy as np
 import h5py
 import z5py
 from cluster_tools.downscaling import PainteraToBdvWorkflow
@@ -12,7 +13,7 @@ def check_max_id(path, key):
     with z5py.File(path) as f:
         attrs = f[key].attrs
         max_id = attrs['maxId']
-    if max_id > 32000:
+    if max_id > np.iinfo('int16').max:
         print("Max-id:", max_id, "does not fit int16")
         raise RuntimeError("Uint16 overflow")
     else:
-- 
GitLab