diff --git a/TEST/NoseTests/test_image.py b/TEST/NoseTests/test_image.py index faae7d0..b221476 100644 --- a/TEST/NoseTests/test_image.py +++ b/TEST/NoseTests/test_image.py @@ -2,7 +2,7 @@ from unittest import TestCase from bidsmanager.base.group import Group -from bidsmanager.base.image import Image, FunctionalImage, DiffusionImage +from bidsmanager.base.image import Image from bidsmanager.read.image_reader import read_image_from_bids_path from bidsmanager.write.dataset_writer import write_json from bidsmanager.utils.utils import read_json @@ -22,10 +22,12 @@ def test_change_acquisition(self): self.assertEqual(basename.replace("contrast", "postcontrast"), image.get_basename()) def test_change_task_name(self): - image = FunctionalImage(modality="bold", task="prediction", run=3) + image = Image(modality="bold", task="prediction", run=3) basename = image.get_basename() - image.set_task_name("weatherprediction") - self.assertEqual(basename.replace("prediction", "weatherprediction"), image.get_basename()) + new_task_name = "weatherprediction" + image.set_task_name(new_task_name) + self.assertEqual(basename.replace("prediction", new_task_name), image.get_basename()) + self.assertEqual(image.get_task_name(), new_task_name) def test_write_changed_acquisition(self): tmp_file = "run-05_FLAIR.nii.gz" @@ -129,7 +131,7 @@ def test_reconstruction(self): assert "rec-axial" in image.get_basename() def test_add_metadata_without_sidecar(self): - image = DiffusionImage(modality="dwi", bval_path="./bval", bvec_path="./bvec") + image = Image(modality="dwi", bval_path="./bval", bvec_path="./bvec") image.add_metadata("WellThoughtOutTest", False) image.set_path("./test.nii.gz") touch(image.get_path()) diff --git a/bidsmanager/base/image.py b/bidsmanager/base/image.py index 8613417..a014efd 100644 --- a/bidsmanager/base/image.py +++ b/bidsmanager/base/image.py @@ -6,7 +6,8 @@ class Image(BIDSObject): - def __init__(self, sidecar_path=None, modality=None, extension=".nii", *inputs, **kwargs): + def __init__(self, sidecar_path=None, modality=None, extension=".nii", bval_path=None, bvec_path=None, + *inputs, **kwargs): self._session = None self._subject = None self._group = None @@ -21,6 +22,8 @@ def __init__(self, sidecar_path=None, modality=None, extension=".nii", *inputs, self._modality = modality self._type = "Image" self._extension = extension + self._bval_path = bval_path + self._bvec_path = bvec_path def get_basename(self): return "_".join(self.get_subject_session_keys(keys=self.get_image_keys())) + self.get_extension() @@ -167,6 +170,7 @@ def update(self, move=False): self.set_path(os.path.join(os.path.dirname(self.get_path()), self.get_basename())) update_file(self._previous_path, self._path, move=move) self.update_sidecar(move=move) + self.update_diffusion_files(move=move) def update_sidecar(self, move=False): if self._sidecar_metadata: @@ -218,22 +222,6 @@ def _set_key_attribute(self, attribute, value): def get_sidecar_path(self): return self.sidecar_path - -class FunctionalImage(Image): - def __init__(self, *inputs, **kwargs): - super(FunctionalImage, self).__init__(*inputs, **kwargs) - - def is_match(self, task_name=None, **kwargs): - return (not task_name or task_name == self.get_task_name()) and super(FunctionalImage, self).is_match(**kwargs) - - -class DiffusionImage(Image): - def __init__(self, bval_path, bvec_path, *inputs, **kwargs): - super(DiffusionImage, self).__init__(*inputs, **kwargs) - self._bval_path = bval_path - self._bvec_path = bvec_path - self._modality = "dwi" - def get_bval_path(self): return self._bval_path @@ -250,12 +238,19 @@ def update_bvec(self, move=False): update_file(self._bvec_path, tmp_bvec_file, move=move) self._bvec_path = tmp_bvec_file - def update(self, *args, **kwargs): - super(DiffusionImage, self).update(*args, **kwargs) + def update_diffusion_files(self, move=False): if self._bval_path: - self.update_bval(*args, **kwargs) + self.update_bval(move=move) if self._bvec_path: - self.update_bvec(*args, **kwargs) + self.update_bvec(move=move) + + +class FunctionalImage(Image): + def __init__(self, *inputs, **kwargs): + super(FunctionalImage, self).__init__(*inputs, **kwargs) + + def is_match(self, task_name=None, **kwargs): + return (not task_name or task_name == self.get_task_name()) and super(FunctionalImage, self).is_match(**kwargs) image_entities = ("task", "acq", "ce", "dir", "rec", "run", "echo") diff --git a/bidsmanager/read/dicom_reader.py b/bidsmanager/read/dicom_reader.py index 0df0586..a66f013 100644 --- a/bidsmanager/read/dicom_reader.py +++ b/bidsmanager/read/dicom_reader.py @@ -11,7 +11,7 @@ from ..base.subject import Subject from ..base.dataset import DataSet -from ..base.image import DiffusionImage +from ..base.image import Image from ..base.base import BIDSObject from ..base.session import Session from ..utils.image_utils import load_image @@ -224,7 +224,7 @@ def convert_dicoms(dicom_objects): def convert_dwi_dicom(in_file): nifti_file, bval_file, bvec_file, sidecar_file = dcm2niix_dwi(in_file) - return DiffusionImage(path=nifti_file, bval_path=bval_file, bvec_path=bvec_file, sidecar_path=sidecar_file) + return Image(path=nifti_file, bval_path=bval_file, bvec_path=bvec_file, sidecar_path=sidecar_file) def dcm2niix_dwi(in_file): diff --git a/bidsmanager/utils/image_utils.py b/bidsmanager/utils/image_utils.py index 055ddb1..299de5f 100644 --- a/bidsmanager/utils/image_utils.py +++ b/bidsmanager/utils/image_utils.py @@ -1,4 +1,4 @@ -from bidsmanager.base.image import FunctionalImage, Image, DiffusionImage +from bidsmanager.base.image import FunctionalImage, Image def load_image(path_to_image, modality=None, acquisition=None, task_name=None, run_number=None, path_to_sidecar=None, @@ -9,11 +9,7 @@ def load_image(path_to_image, modality=None, acquisition=None, task_name=None, r sidecar_path=path_to_sidecar, metadata=metadata, **entities) - elif modality == "dwi": - return DiffusionImage(bval_path=bval_path, bvec_path=bvec_path, path=path_to_image, modality=modality, - sidecar_path=path_to_sidecar, - metadata=metadata, - **entities) else: return Image(modality=modality, path=path_to_image, - sidecar_path=path_to_sidecar, metadata=metadata, **entities) + sidecar_path=path_to_sidecar, metadata=metadata, bval_path=bval_path, bvec_path=bvec_path, + **entities)