diff --git a/extensions/chaeo/examples/label_patches.py b/extensions/chaeo/examples/label_patches.py index f1638a6688b2e59f71d9c8827d1a729eacabe14c..04f2c00ce5d026732e0d2898cd4120e876a54c0c 100644 --- a/extensions/chaeo/examples/label_patches.py +++ b/extensions/chaeo/examples/label_patches.py @@ -10,6 +10,6 @@ if __name__ == '__main__': where_patches=(root / 'batch-output-20231012-0016/2d_patches_training').__str__(), object_csv=(root / 'batch-output-20231011-0003/df_objects.csv').__str__(), ecotaxa_tsv='c:/Users/rhodes/projects/proj0011-plankton-seg/exp0013/ecotaxa_export_10468_20231012_0930.tsv', - where_output=autonumber_new_directory(root, 'labeled_patches') + where_output=autonumber_new_directory(root, 'labeled_patches'), ) print('Finished') 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 e5e45e66c2930071b3675328920e3b9606b093e2..8b8f16de9d9ae33a2fd516f0f375abe413aa30a3 100644 --- a/extensions/chaeo/examples/transfer_labels_to_ilastik_object_classifier.py +++ b/extensions/chaeo/examples/transfer_labels_to_ilastik_object_classifier.py @@ -1,6 +1,8 @@ +import csv from pathlib import Path import h5py import ilastik.applets.objectClassification +import numpy as np import pandas as pd from extensions.ilastik.models import IlastikObjectClassifierModel @@ -8,16 +10,35 @@ from extensions.ilastik.models import IlastikObjectClassifierModel def transfer_labels_to_ilastik_ilp(ilp, df_stack_meta): with h5py.File(ilp, 'r+') as h5: + where_out = Path(ilp).parent + + # export complete HDF5 tree + with open(where_out / 'h5tree.txt', 'w') as hf: + tt = [] + h5.visititems(lambda k, v: tt.append([k, str(v)])) + for line in tt: + hf.write(f'{line[0]} --- {line[1]}\n') + h5.visititems(lambda k, v: print(k + ' : ' + str(v))) + + # change key of label names + ln = ['none'] + list(df_stack_meta.sort_values('annotation_class_id').annotation_class.unique()) + del h5['ObjectClassification/LabelNames'] + h5.create_dataset('ObjectClassification/LabelNames', data=np.array(ln).astype('O')) + + # change object labels ts = h5['ObjectClassification']['LabelInputs']['0000'] for ti in ts.items(): - idx = int(ti[0]) + assert len(ti) == 2 # one for unlabeled area, one for labeled area + idx = int(ti[0]) # important because keys are strings and hence not sorted numerically ds = ti[1] la_old = ds[1] - ds[1] = float(df_stack_meta.loc[df_stack_meta.zi == idx, 'annotation_class_id'].iat[0]) + + # 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]) print(f'Changed label {ti} from {la_old} to {ds[1]}') if __name__ == '__main__': - ilp = 'c:/Users/rhodes/model-server/ilastik/test_automate_obj_labels.ilp' + 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' )