Commit dd65a62e authored by Fernando Perez-Garcia's avatar Fernando Perez-Garcia

Finish first refactoring of images classes

parent b18b1944
# pylint: disable=no-member
from ..interfaces.niftk import VesselExtractor
from ..preprocess import vesselness_filter
VESSELNESS = 'vesselness'
NIFTI_EXT = '.nii.gz'
......@@ -10,13 +10,15 @@ class Image:
def __init__(self, subject):
self.subject = subject
def exists(self):
self.path.is_file()
return self.path.is_file()
@staticmethod
def join(*args):
return '_'.join(*args) # pylint: disable=E1120
parts = [part for part in args if part]
return '_'.join(parts)
......@@ -29,7 +31,7 @@ class AnatomicalImage(Image):
class VesselsImage(Image):
def __init__(self, subject):
super().__init__(subject)
self.vesselness_image = Image(subject)
self.vesselness_image = VesselnessImage(source_image=self)
def run_vesselness_filter(self, force=False):
......@@ -37,7 +39,17 @@ class VesselsImage(Image):
print(self.vesselness_image, 'already exists')
return
node = VesselExtractor()
node.inputs.in_file = self.path
node.inputs.out_file = self.vesselness_image.path
node.run()
vesselness_filter(self, self.vesselness_image)
class VesselnessImage(Image):
def __init__(self, source_image):
super().__init__(source_image.subject)
self.source_image = source_image
self.name = self.join(source_image.name, VESSELNESS)
self.filename = self.name + NIFTI_EXT
self.path = self.source_image.dir / self.filename
def __repr__(self):
return "VesselnessImage('{}')".format(self.path)
......@@ -17,32 +17,53 @@ class DSASeries:
self.subject = subject
self.artery = artery
self.hemisphere = hemisphere
self.bone_image = DSABonelessImage(
self.dir = Path(subject.dsa_dir)
self.bone_image = DSABoneImage(
series=self, artery=artery, hemisphere=hemisphere)
self.boneless_image = DSABoneImage(
self.boneless_image = DSABonelessImage(
series=self, artery=artery, hemisphere=hemisphere)
self.dir = Path(subject.dsa_dir)
def __repr__(self):
s = 'DSASeries(Subject: {}, artery: {}'.format(self.subject,
self.artery)
if self.hemisphere is not None:
s += ', hemisphere: {}'.format(self.hemisphere)
s += ')'
return s
class DSABoneImage(AnatomicalImage):
def __init__(self, series, artery, hemisphere=None):
super().__init__(series.subject)
self.artery = artery
self.hemisphere = hemisphere
self.dir = series.dir
self.name = self.join(series.subject.id, DSA, self.artery, BONE)
self.name = self.join(series.subject.id,
DSA, self.artery, self.hemisphere,
BONE)
self.filename = self.name + NIFTI_EXT
self.path = self.dir / self.filename
super().__init__(series.subject)
def __repr__(self):
return "DSABoneImage('{}')".format(self.path)
class DSABonelessImage(VesselsImage):
def __init__(self, series, artery, hemisphere=None):
super().__init__(series.subject)
self.artery = artery
self.hemisphere = hemisphere
self.dir = series.dir
self.name = self.join(series.subject.id, DSA, self.artery, BONELESS)
self.name = self.join(series.subject.id,
DSA, self.artery, self.hemisphere,
BONELESS)
self.filename = self.name + NIFTI_EXT
self.path = self.dir / self.filename
super().__init__(series.subject)
def __repr__(self):
return "DSABonelessImage('{}')".format(self.path)
from .common import AnatomicalImage, VesselsImage, NIFTI_EXT
try:
from pathlib import Path
except ImportError:
from ..utils.path import Path
ANATOMY = 'anatomy'
VESSELS = ''
class PhaseContrastSeries:
def __init__(self, subject, type_):
self.anatomy_image = AnatomicalImage(subject)
self.vessels_image = VesselsImage(subject)
self.subject = subject
self.type = type_
self.dir = Path(self.subject.mri_dir)
self.anatomy_image = PhaseContrastAnatomyImage(series=self)
self.vessels_image = PhaseContrastVesselsImage(series=self)
def __repr__(self):
return 'PhaseContrastSeries(Subject: {}, type: {}'.format(self.subject,
self.type)
class PhaseContrastAnatomyImage(AnatomicalImage):
def __init__(self, series):
self.type = series.type
self.dir = series.dir
self.name = self.join(series.subject.id, self.type, ANATOMY)
self.filename = self.name + NIFTI_EXT
self.path = self.dir / self.filename
super().__init__(series.subject)
def __repr__(self):
return "PhaseContrastAnatomyImage('{}')".format(self.path)
class PhaseContrastVesselsImage(VesselsImage):
def __init__(self, series):
self.type = series.type
self.dir = series.dir
self.name = self.join(series.subject.id, self.type, VESSELS)
self.filename = self.name + NIFTI_EXT
self.path = self.dir / self.filename
super().__init__(series.subject)
def __repr__(self):
return "PhaseContrastVesselsImage('{}')".format(self.path)
......@@ -197,12 +197,18 @@ class Subject:
self.dsa_ica_right_series = DSASeries(self, const.ICA,
hemisphere=const.RIGHT)
self.mra_series = PhaseContrastSeries(self, const.MRA)
self.mrv_series = PhaseContrastSeries(self, const.MRV)
def __repr__(self):
def __str__(self):
return self.id
def __repr__(self):
return "Subject('{}')".format(self)
def get_t1_gad_to_acpc_transform(self):
ensure_dir(self.t1_gad_to_acpc_path)
registration.get_rigid_acpc_transform(self.acpc_markups_path,
......@@ -278,28 +284,29 @@ class Subject:
def get_vessels_images(self):
dsa = self.get_existing_dsa_images()
dsa = self.get_existing_dsa_vessels_images()
pc = self.get_phase_contrast_vessels_images()
return dsa + pc
def get_phase_contrast_vessels_images(self):
return [self.mra.vessels_image, self.mrv.vessels_image]
return [self.mra_series.vessels_image,
self.mrv_series.vessels_image]
def get_all_dsa_images(self):
images = [
self.dsa_ica_left_image,
self.dsa_ica_right_image,
self.dsa_vert_image,
def get_all_dsa_vessels_images(self):
series_list = [
self.dsa_ica_left_series,
self.dsa_ica_right_series,
self.dsa_vert_series,
]
images = [series.boneless_image for series in series_list]
return images
def get_existing_dsa_images(self):
def get_existing_dsa_vessels_images(self):
images = [
image
for image
in self.get_all_dsa_images()
for image in self.get_all_dsa_vessels_images()
if image.exists()
]
return images
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment