Skip to content

Commit

Permalink
fixed doing the labeling step first before adding the ivd and eps
Browse files Browse the repository at this point in the history
  • Loading branch information
Hendrik-code committed Nov 26, 2024
1 parent f5ae024 commit de6fa69
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 6 deletions.
2 changes: 2 additions & 0 deletions spineps/phase_labeling.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ def perform_labeling_step(model: VertLabelingClassifier, img_nii: NII, vert_nii:
model.load()
# run model
labelmap = run_model_for_vert_labeling(model, img_nii, vert_nii)[0]

# relabel according to labelmap
return vert_nii.map_labels_(labelmap)

Expand Down Expand Up @@ -64,6 +65,7 @@ def run_model_for_vert_labeling(
)
assert len(orig_label) == len(fpath_post), f"{len(orig_label)} != {len(fpath_post)}"
labelmap = {orig_label[idx]: fpath_post[idx] for idx in range(len(orig_label))}

return labelmap, fcost, fpath, fpath_post, costlist, min_costs_path, predictions


Expand Down
9 changes: 7 additions & 2 deletions spineps/phase_post.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,16 @@
np_volume,
)

from spineps.phase_labeling import VertLabelingClassifier, perform_labeling_step
from spineps.seg_pipeline import logger, vertebra_subreg_labels
from spineps.utils.proc_functions import fix_wrong_posterior_instance_label


def phase_postprocess_combined(
img_nii: NII,
seg_nii: NII,
vert_nii: NII,
model_labeling: VertLabelingClassifier | None,
debug_data: dict | None,
labeling_offset: int = 0,
proc_assign_missing_cc: bool = True,
Expand All @@ -36,6 +39,7 @@ def phase_postprocess_combined(
) -> tuple[NII, NII]:
logger.print("Post process", Log_Type.STAGE)
with logger:
img_nii.assert_affine(other=seg_nii)
seg_nii.assert_affine(other=vert_nii)
# Post process semantic mask
###################
Expand All @@ -54,6 +58,7 @@ def phase_postprocess_combined(
seg_uncropped = seg_nii.copy()

# Crop down
img_nii.apply_crop_(crop_slices)
vert_nii.apply_crop_(crop_slices)
seg_nii.apply_crop_(crop_slices)

Expand All @@ -79,8 +84,8 @@ def phase_postprocess_combined(

# Label vertebra top -> down
whole_vert_nii_cleaned, vert_labels = label_instance_top_to_bottom(whole_vert_nii_cleaned, labeling_offset=labeling_offset)
# if labeling_offset != 0:
# whole_vert_nii_cleaned.map_labels_({i: i + 1 for i in vert_labels if i != 0}, verbose=verbose)
if model_labeling is not None:
whole_vert_nii_cleaned = perform_labeling_step(model=model_labeling, img_nii=img_nii, vert_nii=whole_vert_nii_cleaned)
logger.print(f"Labeled {len(vert_labels)} vertebra instances from top to bottom")
vert_arr_cleaned = add_ivd_ep_vert_label(whole_vert_nii_cleaned, seg_nii_cleaned)
vert_arr_cleaned[seg_nii_cleaned.get_seg_array() == v_name2idx["S1"]] = v_name2idx["S1"]
Expand Down
6 changes: 2 additions & 4 deletions spineps/seg_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -470,8 +470,10 @@ def process_img_nii( # noqa: C901
seg_nii_back.assert_affine(other=input_nii)
# use both seg_raw and vert_raw to clean each other, add ivd_ep ...
seg_nii_clean, vert_nii_clean = phase_postprocess_combined(
img_nii=input_nii_,
seg_nii=seg_nii_back,
vert_nii=whole_vert_nii,
model_labeling=model_labeling,
debug_data=debug_data_run,
labeling_offset=vertebra_instance_labeling_offset - 1,
proc_clean_inst_by_sem=proc_clean_inst_by_sem,
Expand All @@ -484,10 +486,6 @@ def process_img_nii( # noqa: C901
vert_nii_clean.assert_affine(other=input_nii)
# input_package.make_nii_from_this(seg_nii_clean)
# input_package.make_nii_from_this(vert_nii_clean)

if model_labeling is not None:
vert_nii_clean = perform_labeling_step(model=model_labeling, img_nii=input_nii, vert_nii=vert_nii_clean)

seg_nii_clean.save(out_spine, verbose=logger)
vert_nii_clean.save(out_vert, verbose=logger)
done_something = True
Expand Down

0 comments on commit de6fa69

Please sign in to comment.