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

Still trying to get autogenerated object classifier working by messing with HDF5

parent 672218ca
No related branches found
No related tags found
No related merge requests found
......@@ -98,7 +98,7 @@ def generate_ilastik_object_classifier(
Starting with a template project file, transfer input data and labels to a duplicate project file.
:param template_ilp: absolute path to existing ilastik object classifier to use as a template
:param where: location of folder containing input data, segmentation maps, labels, and label descriptions
:param where: absolute path to folder containing input data, segmentation maps, labels, and label descriptions
:poram stack_name: prefix of .tif and .csv files that contain classifier training data (e.g. train, test)
:param lane: ilastik lane identifier
:return: (str) name of new ilastik classifier project file
......@@ -106,12 +106,12 @@ def generate_ilastik_object_classifier(
# validate z-stack input data
root = Path(where)
paths = {
'Raw Data': root / f'zstack_{stack_name}_raw.tif',
'Segmentation Image': root / f'zstack_{stack_name}_mask.tif',
rel_paths = {
'Raw Data': Path(f'zstack_{stack_name}_raw.tif'),
'Segmentation Image': Path(f'zstack_{stack_name}_mask.tif'),
}
accessors = {k: generate_file_accessor(pa) for k, pa in paths.items()}
accessors = {k: generate_file_accessor(root / pa) for k, pa in rel_paths.items()}
assert accessors['Raw Data'].chroma == 1
assert accessors['Segmentation Image'].is_mask()
......@@ -153,23 +153,39 @@ def generate_ilastik_object_classifier(
return df_patches.loc[df_patches.zi == idx, 'annotation_class_id'].iat[0]
for hg in ['Raw Data', 'Segmentation Image']:
set_ds(hg, 'filePath', paths[hg].__str__())
set_ds(hg, 'nickname', paths[hg].stem)
set_ds(hg, 'filePath', rel_paths[hg].__str__())
set_ds(hg, 'nickname', rel_paths[hg].stem)
shape_zyx = [accessors[hg].shape_dict[ax] for ax in ['Z', 'Y', 'X']]
set_ds(hg, 'shape', np.array(shape_zyx))
# change key of label names
del h5['ObjectClassification/LabelNames']
if (k_ln := 'ObjectClassification/LabelNames') in h5.keys():
del h5[k_ln]
ln = np.array(label_names)
h5.create_dataset('ObjectClassification/LabelNames', data=ln.astype('O'))
h5.create_dataset(k_ln, data=ln.astype('O'))
if (k_mn := 'ObjectClassification/MaxNumObj') in h5.keys():
del h5[k_mn]
# h5.create_dataset(k_mn, data=(len(label_names) - 1))
h5[k_mn] = len(label_names) - 1
del h5['currentApplet']
h5['currentApplet'] = 1
# change object labels
la_groupname = f'ObjectClassification/LabelInputs/{lns}'
del h5[la_groupname]
lag = h5.create_group(la_groupname)
if (k_li := f'ObjectClassification/LabelInputs/{lns}') in h5.keys():
del h5[k_li]
lag = h5.create_group(k_li)
for zi in range(0, nz):
lag[f'{zi}'] = np.array([0., float(get_label(zi))])
# delete existing classification weights
if (k_rf := f'ObjectExtraction/RegionFeatures/{lane:04d}') in h5.keys():
del h5[k_rf]
if (k_cf := 'ObjectClassification/ClassificationForests') in h5.keys():
del h5[k_cf]
return new_ilp
def compare_object_maps(truth: GenericImageDataAccessor, inferred: GenericImageDataAccessor) -> pd.DataFrame:
......@@ -218,6 +234,7 @@ if __name__ == '__main__':
proj_name='auto_obj_before'
)
def infer_and_compare(ilp, suffix):
# infer object labels from the same data used to train the classifier
train_zstack_raw = generate_file_accessor(where_patch_stack / 'zstack_train_raw.tif')
......@@ -234,6 +251,16 @@ if __name__ == '__main__':
print('Truth and inferred labels match?')
print(pd.value_counts(df_comp['truth_label'] == df_comp['inferred_label']))
# report out some things for debugging
rks = [
'ObjectClassification/MaxNumObj',
'ObjectClassification/LabelInputs/0000/0',
'currentApplet'
]
with h5py.File(ilp, 'r') as h5:
for r in rks:
print(f'{r}: {h5[r][()]}')
# infer object labels from the same data used to train the classifier
infer_and_compare(auto_ilp, 'before')
......
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