Skip to content

Commit

Permalink
Stop loading data when copying unloaded image (#982)
Browse files Browse the repository at this point in the history
* Add failing test

* Stop loading data when copying image

* Add assertions after loading as well

Co-authored-by: Derk <derk@mriguidance.com>
  • Loading branch information
fepegar and Derk authored Oct 11, 2022
1 parent 74457f8 commit 134fa4a
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/torchio/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
STEM = 'stem'
DATA = 'data'
AFFINE = 'affine'
TENSOR = 'tensor'

# For aggregator
IMAGE = 'image'
Expand Down
10 changes: 6 additions & 4 deletions src/torchio/data/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from ..constants import LABEL
from ..constants import PATH
from ..constants import STEM
from ..constants import TENSOR
from ..constants import TYPE
from ..typing import TypeData
from ..typing import TypeDataAffine
Expand Down Expand Up @@ -204,11 +205,12 @@ def __array__(self):

def __copy__(self):
kwargs = {
'tensor': self.data,
'affine': self.affine,
'type': self.type,
'path': self.path,
TYPE: self.type,
PATH: self.path,
}
if self._loaded:
kwargs[TENSOR] = self.data
kwargs[AFFINE] = self.affine
for key, value in self.items():
if key in PROTECTED_KEYS:
continue
Expand Down
14 changes: 14 additions & 0 deletions tests/data/test_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,3 +247,17 @@ def numpy_reader(path):
np.save(test_path, tensor)
image = tio.ScalarImage(test_path, reader=numpy_reader)
image.load()

def test_copy_no_data(self):
# https://github.com/fepegar/torchio/issues/974
path = self.get_image_path('im_copy')
my_image = tio.LabelMap(path)
assert not my_image._loaded
new_image = copy.copy(my_image)
assert not my_image._loaded
assert not new_image._loaded

my_image.load()
new_image = copy.copy(my_image)
assert my_image._loaded
assert new_image._loaded

0 comments on commit 134fa4a

Please sign in to comment.