Skip to content

Commit

Permalink
Add optional postprocessing
Browse files Browse the repository at this point in the history
  • Loading branch information
alicjak1519 committed Feb 13, 2022
1 parent 5c500de commit 74e22bb
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 5 deletions.
9 changes: 5 additions & 4 deletions bayesian_cnn_prometheus/analysis/analyse_lesions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__':
Expand Down
23 changes: 23 additions & 0 deletions bayesian_cnn_prometheus/analysis/apply_variance_postprocess.py
Original file line number Diff line number Diff line change
@@ -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
6 changes: 5 additions & 1 deletion bayesian_cnn_prometheus/analysis/lesions_analyzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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))

Expand Down Expand Up @@ -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)

Expand Down
21 changes: 21 additions & 0 deletions bayesian_cnn_prometheus/run_analysis_for_experiments.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import os
import sys
from pathlib import Path

from bayesian_cnn_prometheus.constants import Paths
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} {os.path.join(Paths.PROJECT_DIR, "analysis", "analyse_lesions.py")} {model_path} {config_path} {input_path} {output_path}')


if __name__ == '__main__':
run_analysis_for_experiment()

0 comments on commit 74e22bb

Please sign in to comment.