Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stop loading data when copying unloaded image #982

Merged
merged 3 commits into from
Oct 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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