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 9433552ed3d600e843272311efda5d855e3f5938..d79127c39825cafc56866cc6c4c3711801b404a1 100644 --- a/extensions/chaeo/examples/transfer_labels_to_ilastik_object_classifier.py +++ b/extensions/chaeo/examples/transfer_labels_to_ilastik_object_classifier.py @@ -4,6 +4,7 @@ import h5py import json import numpy as np import pandas as pd +import skimage import uuid import vigra @@ -149,6 +150,32 @@ def generate_ilastik_object_classifier(template_ilp, where: str, lane=0): return new_ilp +def compare_object_maps(truth: GenericImageDataAccessor, inferred: GenericImageDataAccessor) -> pd.DataFrame: + assert truth.shape == inferred.shape + assert np.all((truth.data == 0) == (inferred.data == 0)) + assert inferred.chroma == 1 + + labels = [] + for zi in range(0, inferred.nz): + inf_img = inferred.data[:, :, :, zi] + + unique = np.unique(inf_img) + assert unique[0] == 0 + + dd = {'zi': zi, 'truth_label': np.unique(truth.data[:, :, :, zi])[1], 'multiples': False} + + if len(unique) == 1: # no object in frame + dd['inferred_label'] = unique[0] + elif len(unique) > 2: # multiple objects in frame, so mask out all but largest + ob_id = skimage.measure.label(inf_img) + pr = skimage.measure.regionprops_table(ob_id, properties=['label', 'area']) + mask = inf_img == pr['label'][pr['area'].argmax()] + dd['inferred_label'] = np.unique(mask * inf_img)[1] + dd['multiples'] = True + else: # exactly one unique object class in frame + dd['inferred_label'] = unique[1] + labels.append(dd) + return pd.DataFrame(labels) if __name__ == '__main__': root = Path('c:/Users/rhodes/projects/proj0011-plankton-seg/') @@ -172,7 +199,6 @@ if __name__ == '__main__': # write comparison train_labels = generate_file_accessor(where_patch_stack / 'zstack_train_label.tif') - write_accessor_data_to_file( - where_patch_stack / 'comp.tif', - InMemoryDataAccessor(result_acc.data == train_labels.data) - ) \ No newline at end of file + df_comp = compare_object_maps(train_labels, result_acc) + df_comp.to_csv(where_patch_stack / autonumber_new_file(where_patch_stack, 'comp', 'csv'), index=False) +