Skip to content

Commit

Permalink
REF: remove diffusion images
Browse files Browse the repository at this point in the history
  • Loading branch information
ellisdg committed Jul 13, 2022
1 parent 2ac85d5 commit 6a3e184
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 35 deletions.
12 changes: 7 additions & 5 deletions TEST/NoseTests/test_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"
Expand Down Expand Up @@ -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())
Expand Down
37 changes: 16 additions & 21 deletions bidsmanager/base/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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

Expand All @@ -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")
4 changes: 2 additions & 2 deletions bidsmanager/read/dicom_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down
10 changes: 3 additions & 7 deletions bidsmanager/utils/image_utils.py
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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)

0 comments on commit 6a3e184

Please sign in to comment.