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 3c721d25da6393d3ffdeb411d02b86ceb5e34223..07142048bc9ea9067ce945c8b7055c5e57d85fe4 100644 --- a/extensions/chaeo/examples/transfer_labels_to_ilastik_object_classifier.py +++ b/extensions/chaeo/examples/transfer_labels_to_ilastik_object_classifier.py @@ -41,13 +41,15 @@ class PatchStackObjectClassifier(IlastikObjectClassifierFromSegmentationModel): obmaps = self.shell.workflow.batchProcessingApplet.run_export(dsi, export_to_array=True) # [z x h x w x n] assert len(obmaps) == 1, 'ilastik generated more than one object map' - assert obmaps[0].shape == (input_img.nz, 1, input_img.hw[0], input_img.hw[1], 1) # z(1)yx(1) + # for some reason these axes get scrambled to Z(1)YX(1) + assert obmaps[0].shape == (input_img.nz, 1, input_img.hw[0], input_img.hw[1], 1) yxcz = np.moveaxis( obmaps[0][:, :, :, :, 0], [2, 3, 1, 0], [0, 1, 2, 3] ) + assert yxcz.shape == input_img.shape return InMemoryDataAccessor(data=yxcz), {'success': True} @@ -75,40 +77,6 @@ def get_dataset_info(h5, lane=0): } return info -def transfer_labels_to_ilastik_ilp(ilp, df_stack_meta, dump_csv=False): - - 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 - if dump_csv: - 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') - - # put certain h5 groups in scope - h5info = get_dataset_info(h5) - - # 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(): - 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] - - # unit index, i.e. reserve 1 for no object - 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]}') def generate_ilastik_object_classifier(template_ilp, where: str, lane=0): @@ -181,20 +149,21 @@ def generate_ilastik_object_classifier(template_ilp, where: str, lane=0): return new_ilp + if __name__ == '__main__': root = Path('c:/Users/rhodes/projects/proj0011-plankton-seg/') template_ilp = root / 'exp0014/template_obj.ilp' + # template_ilp = root / 'exp0014/test_obj_from_seg.ilp' where_patch_stack = root / 'exp0009/output/labeled_patches-20231016-0002' - # new_ilp = generate_ilastik_object_classifier( - # template_ilp, - # where_patch_stack, - # ) + new_ilp = generate_ilastik_object_classifier( + template_ilp, + where_patch_stack, + ) train_zstack_raw = generate_file_accessor(where_patch_stack / 'zstack_train_raw.tif') train_zstack_mask = generate_file_accessor(where_patch_stack / 'zstack_train_mask.tif') - new_ilp = root / 'exp0014/test_obj_from_seg.ilp' mod = PatchStackObjectClassifier({'project_file': new_ilp}) result_acc, _ = mod.infer(train_zstack_raw, train_zstack_mask)