From 9e0185338c33a42a50c6396fcac1425c6b93cb86 Mon Sep 17 00:00:00 2001
From: Christopher Rhodes <christopher.rhodes@embl.de>
Date: Sat, 14 Oct 2023 10:42:59 +0200
Subject: [PATCH] Harmonized unit index of object class

---
 .../transfer_labels_to_ilastik_object_classifier.py         | 6 ++++--
 extensions/chaeo/workflows.py                               | 4 ++--
 2 files changed, 6 insertions(+), 4 deletions(-)

diff --git a/extensions/chaeo/examples/transfer_labels_to_ilastik_object_classifier.py b/extensions/chaeo/examples/transfer_labels_to_ilastik_object_classifier.py
index 8b8f16de..9d5c273e 100644
--- a/extensions/chaeo/examples/transfer_labels_to_ilastik_object_classifier.py
+++ b/extensions/chaeo/examples/transfer_labels_to_ilastik_object_classifier.py
@@ -10,6 +10,8 @@ from extensions.ilastik.models import IlastikObjectClassifierModel
 def transfer_labels_to_ilastik_ilp(ilp, df_stack_meta):
 
     with h5py.File(ilp, 'r+') as h5:
+        # TODO: force make copy if ilp file starts with template_
+        # TODO: enforce somehow that zstack and df_stack_meta are from same export run
         where_out = Path(ilp).parent
 
         # export complete HDF5 tree
@@ -34,12 +36,12 @@ def transfer_labels_to_ilastik_ilp(ilp, df_stack_meta):
             la_old = ds[1]
 
             # unit index, i.e. reserve 1 for no object
-            ds[1] = float(1 + df_stack_meta.loc[df_stack_meta.zi == idx, 'annotation_class_id'].iat[0])
+            ds[1] = float(df_stack_meta.loc[df_stack_meta.zi == idx, 'annotation_class_id'].iat[0])
             print(f'Changed label {ti} from {la_old} to {ds[1]}')
 
 if __name__ == '__main__':
     ilp = 'c:/Users/rhodes/model-server/ilastik/test_autolabel_obj.ilp'
     df = pd.read_csv(
-        'c:/Users/rhodes/projects/proj0011-plankton-seg/exp0009/output/labeled_patches-20231013-0022/training_stack.csv'
+        'c:/Users/rhodes/projects/proj0011-plankton-seg/exp0009/output/labeled_patches-20231014-0002/train_stack.csv'
     )
     transfer_labels_to_ilastik_ilp(ilp, df)
\ No newline at end of file
diff --git a/extensions/chaeo/workflows.py b/extensions/chaeo/workflows.py
index 1a1ef8ce..4d7e0ccd 100644
--- a/extensions/chaeo/workflows.py
+++ b/extensions/chaeo/workflows.py
@@ -205,7 +205,7 @@ def transfer_ecotaxa_labels_to_patch_stacks(
         )
     )
     df_labels = pd.DataFrame({
-        'annotation_class_id': df_split.index,
+        'annotation_class_id': df_split.index + 1,
         'hierarchy': se_unique,
         'annotation_class': df_split.loc[:, 1].str.lower()
     })
@@ -241,7 +241,7 @@ def transfer_ecotaxa_labels_to_patch_stacks(
     # export patches as z-stacks
     for (dfk, dfv) in {'train': df_tr, 'test': df_te}.items():
         zstack_keys = ['mask', 'label', 'raw']
-        zstacks = {f'{dfk}_{zsk}': np.zeros((*patch_size, 1, len(df_tr)), dtype='uint8') for zsk in zstack_keys}
+        zstacks = {f'{dfk}_{zsk}': np.zeros((*patch_size, 1, len(dfv)), dtype='uint8') for zsk in zstack_keys}
         stack_meta = []
         for fi, pl in enumerate(dfv.itertuples(name='PatchFile')):
             fn = pl._asdict()['patch_filename']
-- 
GitLab