Skip to content
Snippets Groups Projects
Commit 75eacb9f authored by Christopher Randolph Rhodes's avatar Christopher Randolph Rhodes
Browse files

Apply labels to ilastik project file from training data

parent 215a8002
No related branches found
No related tags found
No related merge requests found
......@@ -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')
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'
)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment