Spaces:
Sleeping
Sleeping
File size: 10,055 Bytes
a3290d1 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 |
"""
@author: louisblankemeier
"""
import os
from pathlib import Path
from time import time
from typing import Union
import pandas as pd
from totalsegmentator.libs import (
download_pretrained_weights,
nostdout,
setup_nnunet,
)
from comp2comp.hip import hip_utils
from comp2comp.hip.hip_visualization import (
hip_report_visualizer,
hip_roi_visualizer,
)
from comp2comp.inference_class_base import InferenceClass
from comp2comp.models.models import Models
class HipSegmentation(InferenceClass):
"""Spine segmentation."""
def __init__(self, model_name):
super().__init__()
self.model_name = model_name
self.model = Models.model_from_name(model_name)
def __call__(self, inference_pipeline):
# inference_pipeline.dicom_series_path = self.input_path
self.output_dir = inference_pipeline.output_dir
self.output_dir_segmentations = os.path.join(self.output_dir, "segmentations/")
if not os.path.exists(self.output_dir_segmentations):
os.makedirs(self.output_dir_segmentations)
self.model_dir = inference_pipeline.model_dir
seg, mv = self.hip_seg(
os.path.join(self.output_dir_segmentations, "converted_dcm.nii.gz"),
self.output_dir_segmentations + "hip.nii.gz",
inference_pipeline.model_dir,
)
inference_pipeline.model = self.model
inference_pipeline.segmentation = seg
inference_pipeline.medical_volume = mv
return {}
def hip_seg(
self, input_path: Union[str, Path], output_path: Union[str, Path], model_dir
):
"""Run spine segmentation.
Args:
input_path (Union[str, Path]): Input path.
output_path (Union[str, Path]): Output path.
"""
print("Segmenting hip...")
st = time()
os.environ["SCRATCH"] = self.model_dir
# Setup nnunet
model = "3d_fullres"
folds = [0]
trainer = "nnUNetTrainerV2_ep4000_nomirror"
crop_path = None
task_id = [254]
if self.model_name == "ts_hip":
setup_nnunet()
download_pretrained_weights(task_id[0])
else:
raise ValueError("Invalid model name.")
from totalsegmentator.nnunet import nnUNet_predict_image
with nostdout():
img, seg = nnUNet_predict_image(
input_path,
output_path,
task_id,
model=model,
folds=folds,
trainer=trainer,
tta=False,
multilabel_image=True,
resample=1.5,
crop=None,
crop_path=crop_path,
task_name="total",
nora_tag=None,
preview=False,
nr_threads_resampling=1,
nr_threads_saving=6,
quiet=False,
verbose=False,
test=0,
)
end = time()
# Log total time for hip segmentation
print(f"Total time for hip segmentation: {end-st:.2f}s.")
return seg, img
class HipComputeROIs(InferenceClass):
def __init__(self, hip_model):
super().__init__()
self.hip_model_name = hip_model
self.hip_model_type = Models.model_from_name(self.hip_model_name)
def __call__(self, inference_pipeline):
segmentation = inference_pipeline.segmentation
medical_volume = inference_pipeline.medical_volume
model = inference_pipeline.model
images_folder = os.path.join(inference_pipeline.output_dir, "dev")
results_dict = hip_utils.compute_rois(
medical_volume, segmentation, model, images_folder
)
inference_pipeline.femur_results_dict = results_dict
return {}
class HipMetricsSaver(InferenceClass):
"""Save metrics to a CSV file."""
def __init__(self):
super().__init__()
def __call__(self, inference_pipeline):
metrics_output_dir = os.path.join(inference_pipeline.output_dir, "metrics")
if not os.path.exists(metrics_output_dir):
os.makedirs(metrics_output_dir)
results_dict = inference_pipeline.femur_results_dict
left_head_hu = results_dict["left_head"]["hu"]
right_head_hu = results_dict["right_head"]["hu"]
left_intertrochanter_hu = results_dict["left_intertrochanter"]["hu"]
right_intertrochanter_hu = results_dict["right_intertrochanter"]["hu"]
left_neck_hu = results_dict["left_neck"]["hu"]
right_neck_hu = results_dict["right_neck"]["hu"]
# save to csv
df = pd.DataFrame(
{
"Left Head (HU)": [left_head_hu],
"Right Head (HU)": [right_head_hu],
"Left Intertrochanter (HU)": [left_intertrochanter_hu],
"Right Intertrochanter (HU)": [right_intertrochanter_hu],
"Left Neck (HU)": [left_neck_hu],
"Right Neck (HU)": [right_neck_hu],
}
)
df.to_csv(os.path.join(metrics_output_dir, "hip_metrics.csv"), index=False)
return {}
class HipVisualizer(InferenceClass):
def __init__(self):
super().__init__()
def __call__(self, inference_pipeline):
medical_volume = inference_pipeline.medical_volume
left_head_roi = inference_pipeline.femur_results_dict["left_head"]["roi"]
left_head_centroid = inference_pipeline.femur_results_dict["left_head"][
"centroid"
]
left_head_hu = inference_pipeline.femur_results_dict["left_head"]["hu"]
left_intertrochanter_roi = inference_pipeline.femur_results_dict[
"left_intertrochanter"
]["roi"]
left_intertrochanter_centroid = inference_pipeline.femur_results_dict[
"left_intertrochanter"
]["centroid"]
left_intertrochanter_hu = inference_pipeline.femur_results_dict[
"left_intertrochanter"
]["hu"]
left_neck_roi = inference_pipeline.femur_results_dict["left_neck"]["roi"]
left_neck_centroid = inference_pipeline.femur_results_dict["left_neck"][
"centroid"
]
left_neck_hu = inference_pipeline.femur_results_dict["left_neck"]["hu"]
right_head_roi = inference_pipeline.femur_results_dict["right_head"]["roi"]
right_head_centroid = inference_pipeline.femur_results_dict["right_head"][
"centroid"
]
right_head_hu = inference_pipeline.femur_results_dict["right_head"]["hu"]
right_intertrochanter_roi = inference_pipeline.femur_results_dict[
"right_intertrochanter"
]["roi"]
right_intertrochanter_centroid = inference_pipeline.femur_results_dict[
"right_intertrochanter"
]["centroid"]
right_intertrochanter_hu = inference_pipeline.femur_results_dict[
"right_intertrochanter"
]["hu"]
right_neck_roi = inference_pipeline.femur_results_dict["right_neck"]["roi"]
right_neck_centroid = inference_pipeline.femur_results_dict["right_neck"][
"centroid"
]
right_neck_hu = inference_pipeline.femur_results_dict["right_neck"]["hu"]
output_dir = inference_pipeline.output_dir
images_output_dir = os.path.join(output_dir, "images")
if not os.path.exists(images_output_dir):
os.makedirs(images_output_dir)
hip_roi_visualizer(
medical_volume,
left_head_roi,
left_head_centroid,
left_head_hu,
images_output_dir,
"left_head",
)
hip_roi_visualizer(
medical_volume,
left_intertrochanter_roi,
left_intertrochanter_centroid,
left_intertrochanter_hu,
images_output_dir,
"left_intertrochanter",
)
hip_roi_visualizer(
medical_volume,
left_neck_roi,
left_neck_centroid,
left_neck_hu,
images_output_dir,
"left_neck",
)
hip_roi_visualizer(
medical_volume,
right_head_roi,
right_head_centroid,
right_head_hu,
images_output_dir,
"right_head",
)
hip_roi_visualizer(
medical_volume,
right_intertrochanter_roi,
right_intertrochanter_centroid,
right_intertrochanter_hu,
images_output_dir,
"right_intertrochanter",
)
hip_roi_visualizer(
medical_volume,
right_neck_roi,
right_neck_centroid,
right_neck_hu,
images_output_dir,
"right_neck",
)
hip_report_visualizer(
medical_volume.get_fdata(),
left_head_roi + right_head_roi,
[left_head_centroid, right_head_centroid],
images_output_dir,
"head",
{
"Left Head HU": round(left_head_hu),
"Right Head HU": round(right_head_hu),
},
)
hip_report_visualizer(
medical_volume.get_fdata(),
left_intertrochanter_roi + right_intertrochanter_roi,
[left_intertrochanter_centroid, right_intertrochanter_centroid],
images_output_dir,
"intertrochanter",
{
"Left Intertrochanter HU": round(left_intertrochanter_hu),
"Right Intertrochanter HU": round(right_intertrochanter_hu),
},
)
hip_report_visualizer(
medical_volume.get_fdata(),
left_neck_roi + right_neck_roi,
[left_neck_centroid, right_neck_centroid],
images_output_dir,
"neck",
{
"Left Neck HU": round(left_neck_hu),
"Right Neck HU": round(right_neck_hu),
},
)
return {}
|