From af5250350990cbc4465e5e4c0e6887bf928e5929 Mon Sep 17 00:00:00 2001 From: alicjak1519 Date: Sun, 13 Feb 2022 11:17:30 +0100 Subject: [PATCH] Add optional postprocessing --- .../analysis/analyse_lesions.py | 9 ++++---- .../analysis/apply_variance_postprocess.py | 23 +++++++++++++++++++ .../analysis/lesions_analyzer.py | 6 ++++- .../run_analysis_for_experiments.py | 19 +++++++++++++++ 4 files changed, 52 insertions(+), 5 deletions(-) create mode 100644 bayesian_cnn_prometheus/analysis/apply_variance_postprocess.py create mode 100644 bayesian_cnn_prometheus/run_analysis_for_experiments.py diff --git a/bayesian_cnn_prometheus/analysis/analyse_lesions.py b/bayesian_cnn_prometheus/analysis/analyse_lesions.py index e1e8013..4cdb34b 100644 --- a/bayesian_cnn_prometheus/analysis/analyse_lesions.py +++ b/bayesian_cnn_prometheus/analysis/analyse_lesions.py @@ -12,16 +12,17 @@ def analyse_lesions(): input_path = sys.argv[3] output_path = sys.argv[4] - patients_to_analysis = [3, 4, 5, 6, 8, 10, 11, 14, 17, 21, 26, 27, 29, 30, 32, 34, 35, 36, 37, 39, 41] + # patients_to_analysis = [3, 4, 5, 6, 8, 10, 11, 14, 17, 21, 26, 27, 29, 30, 32, 34, 35, 36, 37, 39, 41] + patients_to_analysis = [3] model_config = load_config(Path(config_path)) prediction_options = PredictionOptions( - chunk_size=model_config['preprocessing']['create_chunks']['chunk_size'], - stride=model_config['preprocessing']['create_chunks']['stride'], + chunk_size=model_config['evaluation']['chunk_size'], + stride=model_config['evaluation']['stride'], mc_sample=model_config['mc_samples']) lesions_analyzer = LesionsAnalyzer(model_path, input_path, prediction_options, patients_to_analysis, output_path) - lesions_analyzer.run_analysis() + lesions_analyzer.run_analysis(should_postprocess_variance=True) if __name__ == '__main__': diff --git a/bayesian_cnn_prometheus/analysis/apply_variance_postprocess.py b/bayesian_cnn_prometheus/analysis/apply_variance_postprocess.py new file mode 100644 index 0000000..aeedbce --- /dev/null +++ b/bayesian_cnn_prometheus/analysis/apply_variance_postprocess.py @@ -0,0 +1,23 @@ +import cv2 +import numpy as np + + +def is_contour_bad(c): + _, radius = cv2.minEnclosingCircle(c) + return radius > 10 + + +def getXFromRect(item): + return item[0] + + +def apply_variance_postprocess(variance: np.ndarray): + lower_bound = np.percentile(variance, 99.6) + variance[lower_bound > variance] = 0 + + variance[np.nonzero(variance)] = 255 + variance = variance.astype(np.uint8) + for i in range(variance.shape[0]): + im = cv2.erode(variance[i], np.ones((3, 3))) + variance[i, :, :] = cv2.dilate(im, np.ones((3, 3))) + return variance diff --git a/bayesian_cnn_prometheus/analysis/lesions_analyzer.py b/bayesian_cnn_prometheus/analysis/lesions_analyzer.py index 1090ef4..8b3f6cb 100644 --- a/bayesian_cnn_prometheus/analysis/lesions_analyzer.py +++ b/bayesian_cnn_prometheus/analysis/lesions_analyzer.py @@ -8,6 +8,7 @@ import numpy as np from tqdm import tqdm +from bayesian_cnn_prometheus.analysis.apply_variance_postprocess import apply_variance_postprocess from bayesian_cnn_prometheus.constants import Paths from bayesian_cnn_prometheus.evaluation.bayesian_model_evaluator import BayesianModelEvaluator from bayesian_cnn_prometheus.evaluation.evaluate_model import PredictionOptions, crop_image_to_bounding_box_with_lungs @@ -35,7 +36,7 @@ def __init__(self, model_path: str, input_path: str, prediction_options: Predict self.results = {'chunks_analyse': {'overall': {'tp': 0, 'tn': 0, 'fp': 0, 'fn': 0}}, 'voxels_analyse': {'overall': {'tp': 0, 'tn': 0, 'fp': 0, 'fn': 0}}} - def run_analysis(self): + def run_analysis(self, should_postprocess_variance: bool = False): for image_path in tqdm(glob.glob(os.path.join(self.input_path, Paths.IMAGES_DIR, '*.nii.gz'))): patient_idx = int(get_patient_index(image_path)) @@ -66,6 +67,9 @@ def run_analysis(self): variance = self.get_variance(cropped_image, cropped_segmentation, variance_path, nifti.affine, nifti.header) + if should_postprocess_variance: + variance = apply_variance_postprocess(variance) + self._analyze_chunks(patient_idx, cropped_mask, variance) self._analyze_voxels(patient_idx, cropped_mask, variance) diff --git a/bayesian_cnn_prometheus/run_analysis_for_experiments.py b/bayesian_cnn_prometheus/run_analysis_for_experiments.py new file mode 100644 index 0000000..7ee526c --- /dev/null +++ b/bayesian_cnn_prometheus/run_analysis_for_experiments.py @@ -0,0 +1,19 @@ +import os +import sys +from pathlib import Path + +from bayesian_cnn_prometheus.run_training_for_experiments import create_sbatch_script + + +def run_analysis_for_experiment(): + model_path = sys.argv[1] + config_path = sys.argv[2] + input_path = sys.argv[3] + output_path = sys.argv[4] + + script_path = create_sbatch_script(Path(output_path)) + os.system(f'sbatch {script_path} analysis/analyse_lesions.py {model_path} {config_path} {input_path} {output_path}') + + +if __name__ == '__main__': + run_analysis_for_experiment()