-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
225 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
import os | ||
import json | ||
|
||
from MRICenterline import CFG | ||
from MRICenterline.app.points.point import Point | ||
from MRICenterline.app.points.point_array import PointArray | ||
from MRICenterline.app.points.status import PointStatus | ||
from MRICenterline.app.points.timer import Timer | ||
from MRICenterline.app.database.save_points import save_points | ||
from MRICenterline.app.database.name_id import get_case_id | ||
from MRICenterline.app.gui_data_handling.slice_loc_based_image_properties import SliceLocImageProperties | ||
from MRICenterline.app.file_reader.dicom.DICOMReader import DICOMReader | ||
|
||
import logging | ||
logging.getLogger(__name__) | ||
|
||
|
||
def timestamp_parse(original: str): | ||
from datetime import datetime | ||
|
||
split1 = original.split("T") | ||
date = datetime.strptime(split1[0], "%Y-%m-%d") | ||
|
||
split2 = split1[1].split(".") | ||
time = datetime.strptime(split2[0], "%H:%M:%S") | ||
tz = datetime.strptime(split2[1].lstrip("0123456789"), "%z") | ||
|
||
return datetime(date.year, date.month, date.day, hour=time.hour, minute=time.minute, second=time.second, | ||
microsecond=0, tzinfo=tz.tzinfo) | ||
|
||
|
||
class Ver3AnnotationImport: | ||
def __init__(self, filename, root_folder=None): | ||
self.filename = filename | ||
|
||
if root_folder: | ||
self.case_name = os.path.relpath(self.filename.parents[1], root_folder) | ||
else: | ||
self.case_name = os.path.relpath(self.filename.parents[1], CFG.get_folder('raw')) | ||
|
||
logging.info(f"Reading file: {self.filename}") | ||
|
||
with open(self.filename, 'r') as f: | ||
file = json.load(f) | ||
|
||
self.sequence_name = file['SeriesDescription'] | ||
self.timestamp = timestamp_parse(file['annotation timestamp']) | ||
|
||
try: | ||
self.time_measured = file['Time measurement'] | ||
except KeyError: | ||
logging.info("No time measured listed") | ||
self.time_measured = None | ||
|
||
self.mpr_point_array = PointArray(PointStatus.MPR) | ||
self.length_point_array = PointArray(PointStatus.LENGTH) | ||
|
||
try: | ||
mpr_coords = file['MPR points'] | ||
except KeyError: | ||
logging.info(f"Found no MPR points") | ||
else: | ||
logging.info(f"Found {len(mpr_coords)} MPR points") | ||
self.parse_points(mpr_coords, self.mpr_point_array) | ||
|
||
try: | ||
length_coords = file['length points'] | ||
except KeyError: | ||
logging.info(f"Found no length points") | ||
else: | ||
logging.info(f"Found {len(length_coords)} length points") | ||
self.parse_points(length_coords, self.length_point_array) | ||
|
||
def parse_points(self, points_from_json, point_array: PointArray): | ||
dcm_reader = DICOMReader(case_name=self.case_name, | ||
case_id=get_case_id(self.case_name), | ||
folder=self.filename.parents[1], | ||
is_new_case=False) | ||
|
||
np_array, file_list = dcm_reader[self.sequence_name] | ||
|
||
image_properties = SliceLocImageProperties(np_array=np_array, | ||
z_coords=dcm_reader.get_z_coords(self.sequence_name), | ||
file_list=file_list) | ||
|
||
for pt in points_from_json: | ||
parsed = Point.point_from_vtk_coords(pt, image_properties) | ||
point_array.add_point(parsed) | ||
|
||
def __repr__(self): | ||
return f""" | ||
Importing {self.filename} into database | ||
Timestamp {self.timestamp} | ||
Time measured {self.time_measured} | ||
Length Points: {len(self.length_point_array)} | ||
MPR Points: {len(self.mpr_point_array)} | ||
""" | ||
|
||
def commit(self): | ||
if self.time_measured and len(self.time_measured.strip()) > 0: | ||
timer = DummyTimer(self.time_measured.strip()) | ||
else: | ||
timer = Timer() | ||
|
||
save_points(case_name=self.case_name, | ||
sequence_name=self.sequence_name, | ||
length_points=self.length_point_array, | ||
mpr_points=self.mpr_point_array, | ||
timer_data=timer, | ||
timestamp=self.timestamp) | ||
|
||
|
||
class DummyTimer(Timer): | ||
def __init__(self, gap_from_file): | ||
super().__init__() | ||
from datetime import datetime, timedelta | ||
|
||
clean_gap_string = gap_from_file.split(".")[0] | ||
|
||
t = datetime.strptime(clean_gap_string, "%H:%M:%S") | ||
delta = timedelta(hours=t.hour, minutes=t.minute, seconds=t.second) | ||
|
||
self.measured_gap = delta | ||
|
||
def calculate_time_gap(self): | ||
return int(self.measured_gap.total_seconds()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,36 @@ | ||
from glob import glob | ||
from typing import List | ||
from pathlib import Path | ||
|
||
from MRICenterline.app.points.import_from_v3 import Ver3AnnotationImport | ||
|
||
import logging | ||
logging.getLogger(__name__) | ||
|
||
|
||
def load_v3_points(folder: str or Path): | ||
pass | ||
def load_v3_points(folders: List[str] or List[Path]): | ||
logging.info("Starting v3 point loader") | ||
num_folders = len(folders) | ||
|
||
for index, folder in enumerate(folders): | ||
logging.info(f"[{1 + index} / {num_folders}] Reading {folder}") | ||
root_folder = folder.parents[1] | ||
|
||
if Path(folder).parts[-1] == 'data': | ||
for annotation_file in [Path(i) for i in glob(f"{folder}/*.annotation.json")]: | ||
if Path(annotation_file).name.split(".")[-3] == "centerline": | ||
pass | ||
# centerline_files.add(annotation_file) | ||
else: | ||
try: | ||
importer = Ver3AnnotationImport(annotation_file, root_folder) | ||
# TODO: should not be needed when using the Rambam PC | ||
except KeyError: | ||
pass | ||
else: | ||
print(importer) | ||
importer.commit() | ||
|
||
else: | ||
pass | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,43 @@ | ||
from pathlib import Path | ||
from MRICenterline.app.file_reader.imager import Imager | ||
|
||
import logging | ||
logging.getLogger(__name__) | ||
|
||
|
||
def run_metadata_sequence_scan(folders, parent_widget=None, running_for_v3_scanner=False): | ||
if running_for_v3_scanner: | ||
root_folder = folders.pop(0) | ||
logging.info(f"Using {root_folder} as folder root") | ||
else: | ||
root_folder = None | ||
|
||
def run_metadata_sequence_scan(folders, parent_widget=None): | ||
num_folders = len(folders) | ||
for index, folder in enumerate(folders): | ||
if running_for_v3_scanner and Path(folder).parts[-1] == 'data': | ||
logging.info(f"Skipping {folder}") | ||
continue | ||
|
||
reading_info = f"[{1 + index} / {num_folders}] Reading {folder}" | ||
logging.info(reading_info) | ||
if parent_widget: | ||
parent_widget.add_to_textbox(f"[{1 + index} / {num_folders}] Reading {folder}") | ||
parent_widget.add_to_textbox(reading_info) | ||
|
||
try: | ||
imager = Imager(folder) | ||
imager = Imager(folder, root_folder=root_folder) | ||
except NotImplementedError: | ||
parent_widget.add_to_textbox(f"{folder} is either not supported or has no MRI images.", color='red') | ||
error_info = f"{folder} is either not supported or has no MRI images." | ||
logging.warning(error_info) | ||
if parent_widget: | ||
parent_widget.add_to_textbox(f"{folder} is either not supported or has no MRI images.", color='red') | ||
else: | ||
reading_done_info = f"Folder is {imager.file_type} | {len(imager)} sequences found" | ||
logging.info(reading_done_info) | ||
if parent_widget: | ||
parent_widget.add_to_textbox(f"Folder is {imager.file_type} | {len(imager)} sequences found") | ||
parent_widget.add_to_textbox(reading_done_info) | ||
|
||
logging.info("Metadata sequence scan complete!") | ||
|
||
parent_widget.add_to_textbox(f"Done! You may close.") | ||
if parent_widget: | ||
done_message = f"Done! You may close." | ||
parent_widget.add_to_textbox(done_message) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,4 +23,5 @@ scipy | |
SimpleITK==2.0.2 | ||
vtk | ||
tqdm | ||
nibabel | ||
nibabel | ||
pytz |