Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Save more DSC metrics when in the merge atlas segmentations task #57

Merged
merged 3 commits into from
Jun 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions magmap/atlas/atlas_refiner.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
from magmap.settings import config
from magmap.settings import profiles

_logger = config.logger.getChild(__name__)


def _get_bbox(img_np, threshold=10):
"""Get the bounding box for the largest object within an image.
Expand Down Expand Up @@ -1513,8 +1515,9 @@ def measure_atlas_refinement(

"""
# DSC and total volumes of atlas and labels
print("\nDSC after import:")
overlap_meas_add = config.atlas_profile["overlap_meas_add_lbls"]
lbls_msg = f" (plus {overlap_meas_add})" if overlap_meas_add else ""
_logger.info(f"\nMeasuring overlap of atlas and combined labels{lbls_msg}:")
dsc, atlas_mask, labels_mask = measure_overlap_combined_labels(
img_atlas, img_labels, overlap_meas_add, return_masks=True)
metrics[config.AtlasMetrics.DSC_ATLAS_LABELS] = [dsc]
Expand All @@ -1530,7 +1533,7 @@ def measure_atlas_refinement(
thresh_atlas, img_atlas.GetSpacing()[::-1])
metrics[config.SmoothingMetrics.COMPACTNESS] = [compactness]

print("\nWhole atlas stats:")
_logger.info("\nWhole atlas stats:")
df = df_io.dict_to_data_frame(metrics, path, show=" ")
return df

Expand Down Expand Up @@ -1612,7 +1615,7 @@ def measure_overlap_labels(labels_img1, labels_img2):
overlap_filter = sitk.LabelOverlapMeasuresImageFilter()
overlap_filter.Execute(labels_img1, labels_img2)
mean_region_dsc = overlap_filter.GetDiceCoefficient()
print("Mean label-by-label DSC: {}".format(mean_region_dsc))
_logger.info("Mean label overlap DSC: {}".format(mean_region_dsc))
return mean_region_dsc


Expand Down
21 changes: 13 additions & 8 deletions magmap/atlas/edge_seg.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
from magmap.io import df_io, libmag, sitk_io
from magmap.stats import vols

_logger = config.logger.getChild(__name__)


def _mirror_imported_labels(labels_img_np, start, mirror_mult, axis):
# mirror labels that have been imported and transformed may have had
Expand Down Expand Up @@ -355,26 +357,29 @@ def edge_aware_segmentation(path_atlas, show=True, atlas=True, suffix=None,
labels_sitk_seg = sitk_io.replace_sitk_with_numpy(labels_sitk, labels_seg)

# show DSCs for labels
print("\nMeasuring overlap of atlas and combined watershed labels:")
atlas_refiner.measure_overlap_combined_labels(atlas_sitk, labels_sitk_seg)
print("Measuring overlap of individual original and watershed labels:")
atlas_refiner.measure_overlap_labels(labels_sitk, labels_sitk_seg)
print("\nMeasuring overlap of combined original and watershed labels:")
atlas_refiner.measure_overlap_labels(
_logger.info(
"\nMeasuring overlap of individual original and watershed labels:")
dsc_lbls_comb = atlas_refiner.measure_overlap_labels(
labels_sitk, labels_sitk_seg)
_logger.info(
"\nMeasuring overlap of combined original and watershed labels:")
dsc_lbls_indiv = atlas_refiner.measure_overlap_labels(
atlas_refiner.make_labels_fg(labels_sitk),
atlas_refiner.make_labels_fg(labels_sitk_seg))
print()
_logger.info("")

# measure and save whole atlas metrics
metrics = {
config.AtlasMetrics.SAMPLE: [os.path.basename(mod_path)],
config.AtlasMetrics.REGION: config.REGION_ALL,
config.AtlasMetrics.CONDITION: "|".join(cond),
config.AtlasMetrics.DSC_LABELS_ORIG_NEW_COMBINED: dsc_lbls_comb,
config.AtlasMetrics.DSC_LABELS_ORIG_NEW_INDIV: dsc_lbls_indiv,
}
df_metrics_path = libmag.combine_paths(
mod_path, config.PATH_ATLAS_IMPORT_METRICS)
atlas_refiner.measure_atlas_refinement(
metrics, atlas_sitk, labels_sitk, df_metrics_path)
metrics, atlas_sitk, labels_sitk_seg, df_metrics_path)

# show and write image to same directory as atlas with appropriate suffix
sitk_io.write_reg_images(
Expand Down
2 changes: 2 additions & 0 deletions magmap/settings/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -649,6 +649,8 @@ class AtlasMetrics(Enum):
DSC_ATLAS_SAMPLE = "DSC_atlas_sample"
DSC_ATLAS_SAMPLE_CUR = "DSC_atlas_sample_curated"
DSC_SAMPLE_LABELS = "DSC_sample_labels"
DSC_LABELS_ORIG_NEW_COMBINED = "DSC_labels_orig_new_combined"
DSC_LABELS_ORIG_NEW_INDIV = "DSC_labels_orig_new_individual"
SIMILARITY_METRIC = "Similarity_metric"
LAT_UNLBL_VOL = "Lateral_unlabeled_volume"
LAT_UNLBL_PLANES = "Lateral_unlabeled_planes"
Expand Down