mirp 2.2.2__tar.gz → 2.2.3__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (112) hide show
  1. {mirp-2.2.2 → mirp-2.2.3}/NEWS.md +13 -0
  2. {mirp-2.2.2 → mirp-2.2.3}/PKG-INFO +3 -3
  3. {mirp-2.2.2 → mirp-2.2.3}/README.md +2 -2
  4. {mirp-2.2.2 → mirp-2.2.3}/mirp/_data_import/dicom_file.py +49 -2
  5. {mirp-2.2.2 → mirp-2.2.3}/mirp/_data_import/dicom_file_pet.py +4 -1
  6. {mirp-2.2.2 → mirp-2.2.3}/mirp/_data_import/dicom_file_rtstruct.py +3 -1
  7. {mirp-2.2.2 → mirp-2.2.3}/mirp/_data_import/dicom_file_seg.py +3 -1
  8. {mirp-2.2.2 → mirp-2.2.3}/mirp/_data_import/dicom_file_stack.py +31 -0
  9. {mirp-2.2.2 → mirp-2.2.3}/mirp/_data_import/generic_file.py +82 -3
  10. {mirp-2.2.2 → mirp-2.2.3}/mirp/_data_import/itk_file.py +15 -0
  11. {mirp-2.2.2 → mirp-2.2.3}/mirp/_images/base_image.py +16 -0
  12. {mirp-2.2.2 → mirp-2.2.3}/mirp/_images/generic_image.py +5 -0
  13. {mirp-2.2.2 → mirp-2.2.3}/mirp/_masks/base_mask.py +16 -0
  14. {mirp-2.2.2 → mirp-2.2.3}/mirp/_workflows/standardWorkflow.py +52 -20
  15. {mirp-2.2.2 → mirp-2.2.3}/mirp/extract_features_and_images.py +4 -0
  16. {mirp-2.2.2 → mirp-2.2.3}/mirp.egg-info/PKG-INFO +3 -3
  17. {mirp-2.2.2 → mirp-2.2.3}/pyproject.toml +1 -1
  18. {mirp-2.2.2 → mirp-2.2.3}/CONTRIBUTING.md +0 -0
  19. {mirp-2.2.2 → mirp-2.2.3}/LICENSE.txt +0 -0
  20. {mirp-2.2.2 → mirp-2.2.3}/MANIFEST.in +0 -0
  21. {mirp-2.2.2 → mirp-2.2.3}/mirp/__init__.py +0 -0
  22. {mirp-2.2.2 → mirp-2.2.3}/mirp/_data_import/__init__.py +0 -0
  23. {mirp-2.2.2 → mirp-2.2.3}/mirp/_data_import/dicom_file_ct.py +0 -0
  24. {mirp-2.2.2 → mirp-2.2.3}/mirp/_data_import/dicom_file_mr.py +0 -0
  25. {mirp-2.2.2 → mirp-2.2.3}/mirp/_data_import/dicom_file_rtdose.py +0 -0
  26. {mirp-2.2.2 → mirp-2.2.3}/mirp/_data_import/directory.py +0 -0
  27. {mirp-2.2.2 → mirp-2.2.3}/mirp/_data_import/generic_file_stack.py +0 -0
  28. {mirp-2.2.2 → mirp-2.2.3}/mirp/_data_import/itk_file_stack.py +0 -0
  29. {mirp-2.2.2 → mirp-2.2.3}/mirp/_data_import/mask_contour.py +0 -0
  30. {mirp-2.2.2 → mirp-2.2.3}/mirp/_data_import/numpy_file.py +0 -0
  31. {mirp-2.2.2 → mirp-2.2.3}/mirp/_data_import/numpy_file_stack.py +0 -0
  32. {mirp-2.2.2 → mirp-2.2.3}/mirp/_data_import/read_data.py +0 -0
  33. {mirp-2.2.2 → mirp-2.2.3}/mirp/_data_import/suv.py +0 -0
  34. {mirp-2.2.2 → mirp-2.2.3}/mirp/_data_import/utilities.py +0 -0
  35. {mirp-2.2.2 → mirp-2.2.3}/mirp/_featuresets/__init__.py +0 -0
  36. {mirp-2.2.2 → mirp-2.2.3}/mirp/_featuresets/cm.py +0 -0
  37. {mirp-2.2.2 → mirp-2.2.3}/mirp/_featuresets/dzm.py +0 -0
  38. {mirp-2.2.2 → mirp-2.2.3}/mirp/_featuresets/intensity_histogram.py +0 -0
  39. {mirp-2.2.2 → mirp-2.2.3}/mirp/_featuresets/intensity_volume_histogram.py +0 -0
  40. {mirp-2.2.2 → mirp-2.2.3}/mirp/_featuresets/local_intensity.py +0 -0
  41. {mirp-2.2.2 → mirp-2.2.3}/mirp/_featuresets/morphology_3d.py +0 -0
  42. {mirp-2.2.2 → mirp-2.2.3}/mirp/_featuresets/ngldm.py +0 -0
  43. {mirp-2.2.2 → mirp-2.2.3}/mirp/_featuresets/ngtdm.py +0 -0
  44. {mirp-2.2.2 → mirp-2.2.3}/mirp/_featuresets/rlm.py +0 -0
  45. {mirp-2.2.2 → mirp-2.2.3}/mirp/_featuresets/statistics.py +0 -0
  46. {mirp-2.2.2 → mirp-2.2.3}/mirp/_featuresets/szm.py +0 -0
  47. {mirp-2.2.2 → mirp-2.2.3}/mirp/_featuresets/utilities.py +0 -0
  48. {mirp-2.2.2 → mirp-2.2.3}/mirp/_image_processing/__init__.py +0 -0
  49. {mirp-2.2.2 → mirp-2.2.3}/mirp/_image_processing/alter_mask.py +0 -0
  50. {mirp-2.2.2 → mirp-2.2.3}/mirp/_image_processing/anti_aliasing.py +0 -0
  51. {mirp-2.2.2 → mirp-2.2.3}/mirp/_image_processing/cropping.py +0 -0
  52. {mirp-2.2.2 → mirp-2.2.3}/mirp/_image_processing/discretise_image.py +0 -0
  53. {mirp-2.2.2 → mirp-2.2.3}/mirp/_image_processing/randomise_mask.py +0 -0
  54. {mirp-2.2.2 → mirp-2.2.3}/mirp/_image_processing/split_mask.py +0 -0
  55. {mirp-2.2.2 → mirp-2.2.3}/mirp/_image_processing/tissue_mask.py +0 -0
  56. {mirp-2.2.2 → mirp-2.2.3}/mirp/_image_processing/utilities.py +0 -0
  57. {mirp-2.2.2 → mirp-2.2.3}/mirp/_imagefilters/__init__.py +0 -0
  58. {mirp-2.2.2 → mirp-2.2.3}/mirp/_imagefilters/exponential_transform.py +0 -0
  59. {mirp-2.2.2 → mirp-2.2.3}/mirp/_imagefilters/gabor.py +0 -0
  60. {mirp-2.2.2 → mirp-2.2.3}/mirp/_imagefilters/gaussian.py +0 -0
  61. {mirp-2.2.2 → mirp-2.2.3}/mirp/_imagefilters/generic.py +0 -0
  62. {mirp-2.2.2 → mirp-2.2.3}/mirp/_imagefilters/laplacian_of_gaussian.py +0 -0
  63. {mirp-2.2.2 → mirp-2.2.3}/mirp/_imagefilters/laws.py +0 -0
  64. {mirp-2.2.2 → mirp-2.2.3}/mirp/_imagefilters/log_transform.py +0 -0
  65. {mirp-2.2.2 → mirp-2.2.3}/mirp/_imagefilters/mean.py +0 -0
  66. {mirp-2.2.2 → mirp-2.2.3}/mirp/_imagefilters/nonseparable_wavelet.py +0 -0
  67. {mirp-2.2.2 → mirp-2.2.3}/mirp/_imagefilters/separable_wavelet.py +0 -0
  68. {mirp-2.2.2 → mirp-2.2.3}/mirp/_imagefilters/square_root_transform.py +0 -0
  69. {mirp-2.2.2 → mirp-2.2.3}/mirp/_imagefilters/square_transform.py +0 -0
  70. {mirp-2.2.2 → mirp-2.2.3}/mirp/_imagefilters/utilities.py +0 -0
  71. {mirp-2.2.2 → mirp-2.2.3}/mirp/_images/__init__.py +0 -0
  72. {mirp-2.2.2 → mirp-2.2.3}/mirp/_images/ct_image.py +0 -0
  73. {mirp-2.2.2 → mirp-2.2.3}/mirp/_images/mask_image.py +0 -0
  74. {mirp-2.2.2 → mirp-2.2.3}/mirp/_images/mr_image.py +0 -0
  75. {mirp-2.2.2 → mirp-2.2.3}/mirp/_images/pet_image.py +0 -0
  76. {mirp-2.2.2 → mirp-2.2.3}/mirp/_images/rtdose_image.py +0 -0
  77. {mirp-2.2.2 → mirp-2.2.3}/mirp/_images/transformed_image.py +0 -0
  78. {mirp-2.2.2 → mirp-2.2.3}/mirp/_images/utilities.py +0 -0
  79. {mirp-2.2.2 → mirp-2.2.3}/mirp/_masks/__init__.py +0 -0
  80. {mirp-2.2.2 → mirp-2.2.3}/mirp/_workflows/__init__.py +0 -0
  81. {mirp-2.2.2 → mirp-2.2.3}/mirp/_workflows/baseWorkflow.py +0 -0
  82. {mirp-2.2.2 → mirp-2.2.3}/mirp/config_data.xml +0 -0
  83. {mirp-2.2.2 → mirp-2.2.3}/mirp/config_settings.xml +0 -0
  84. {mirp-2.2.2 → mirp-2.2.3}/mirp/data_import/__init__.py +0 -0
  85. {mirp-2.2.2 → mirp-2.2.3}/mirp/data_import/import_image.py +0 -0
  86. {mirp-2.2.2 → mirp-2.2.3}/mirp/data_import/import_image_and_mask.py +0 -0
  87. {mirp-2.2.2 → mirp-2.2.3}/mirp/data_import/import_mask.py +0 -0
  88. {mirp-2.2.2 → mirp-2.2.3}/mirp/deep_learning_preprocessing.py +0 -0
  89. {mirp-2.2.2 → mirp-2.2.3}/mirp/deprecated.py +0 -0
  90. {mirp-2.2.2 → mirp-2.2.3}/mirp/extract_image_parameters.py +0 -0
  91. {mirp-2.2.2 → mirp-2.2.3}/mirp/extract_mask_labels.py +0 -0
  92. {mirp-2.2.2 → mirp-2.2.3}/mirp/settings/__init__.py +0 -0
  93. {mirp-2.2.2 → mirp-2.2.3}/mirp/settings/feature_parameters.py +0 -0
  94. {mirp-2.2.2 → mirp-2.2.3}/mirp/settings/general_parameters.py +0 -0
  95. {mirp-2.2.2 → mirp-2.2.3}/mirp/settings/generic.py +0 -0
  96. {mirp-2.2.2 → mirp-2.2.3}/mirp/settings/image_processing_parameters.py +0 -0
  97. {mirp-2.2.2 → mirp-2.2.3}/mirp/settings/import_config_parameters.py +0 -0
  98. {mirp-2.2.2 → mirp-2.2.3}/mirp/settings/import_data_parameters.py +0 -0
  99. {mirp-2.2.2 → mirp-2.2.3}/mirp/settings/interpolation_parameters.py +0 -0
  100. {mirp-2.2.2 → mirp-2.2.3}/mirp/settings/perturbation_parameters.py +0 -0
  101. {mirp-2.2.2 → mirp-2.2.3}/mirp/settings/resegmentation_parameters.py +0 -0
  102. {mirp-2.2.2 → mirp-2.2.3}/mirp/settings/transformation_parameters.py +0 -0
  103. {mirp-2.2.2 → mirp-2.2.3}/mirp/settings/utilities.py +0 -0
  104. {mirp-2.2.2 → mirp-2.2.3}/mirp/utilities/__init__.py +0 -0
  105. {mirp-2.2.2 → mirp-2.2.3}/mirp/utilities/config_utilities.py +0 -0
  106. {mirp-2.2.2 → mirp-2.2.3}/mirp/utilities/parallel.py +0 -0
  107. {mirp-2.2.2 → mirp-2.2.3}/mirp/utilities/utilities.py +0 -0
  108. {mirp-2.2.2 → mirp-2.2.3}/mirp.egg-info/SOURCES.txt +0 -0
  109. {mirp-2.2.2 → mirp-2.2.3}/mirp.egg-info/dependency_links.txt +0 -0
  110. {mirp-2.2.2 → mirp-2.2.3}/mirp.egg-info/requires.txt +0 -0
  111. {mirp-2.2.2 → mirp-2.2.3}/mirp.egg-info/top_level.txt +0 -0
  112. {mirp-2.2.2 → mirp-2.2.3}/setup.cfg +0 -0
@@ -1,3 +1,16 @@
1
+ # Version 2.2.3
2
+
3
+ ## Minor changes
4
+
5
+ - Tables with feature values now contain extra columns. These columns specify the file name (for non-DICOM input),
6
+ the directory path of the image and masks and several DICOM tags for identifying the input.
7
+
8
+ - MIRP now checks whether there are potential problems between the frames of reference of image and mask files.
9
+
10
+ # Fixes
11
+
12
+ - Fixed an error that occurs when attempting to create a deep copy `ImageITKFile` objects.
13
+
1
14
  # Version 2.2.2
2
15
 
3
16
  ## Minor changes
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: mirp
3
- Version: 2.2.2
3
+ Version: 2.2.3
4
4
  Summary: A package for standardised processing of medical imaging and computation of quantitative features.
5
5
  Author-email: Alex Zwanenburg <alexander.zwanenburg@nct-dresden.de>
6
6
  License: European Union Public Licence
@@ -354,8 +354,8 @@ pip install mirp
354
354
 
355
355
  MIRP can be used to compute quantitative features from regions of interest in images in an IBSI-compliant manner
356
356
  using a standardized workflow This requires both images and masks. MIRP can process DICOM, NIfTI, NRRD and numpy
357
- images. Masks are DICOM radiotherapy structure sets (RTSTRUCT), or volumetric data with integer labels (e.g. 1, 2,
358
- etc.).
357
+ images. Masks are DICOM radiotherapy structure sets (RTSTRUCT), DICOM segmentation (SEG) or volumetric data with
358
+ integer labels (e.g. 1, 2, etc.).
359
359
 
360
360
  Below is a minimal working example for extracting features from a single image file and its mask.
361
361
 
@@ -44,8 +44,8 @@ pip install mirp
44
44
 
45
45
  MIRP can be used to compute quantitative features from regions of interest in images in an IBSI-compliant manner
46
46
  using a standardized workflow This requires both images and masks. MIRP can process DICOM, NIfTI, NRRD and numpy
47
- images. Masks are DICOM radiotherapy structure sets (RTSTRUCT), or volumetric data with integer labels (e.g. 1, 2,
48
- etc.).
47
+ images. Masks are DICOM radiotherapy structure sets (RTSTRUCT), DICOM segmentation (SEG) or volumetric data with
48
+ integer labels (e.g. 1, 2, etc.).
49
49
 
50
50
  Below is a minimal working example for extracting features from a single image file and its mask.
51
51
 
@@ -449,14 +449,61 @@ class ImageDicomFile(ImageFile):
449
449
 
450
450
  return image_data
451
451
 
452
+ def set_object_metadata(self):
453
+ """
454
+ Updates the object metadata that is passed to native image and mask classes in to_object.
455
+ """
456
+ metadata = []
457
+ super().set_object_metadata()
458
+
459
+ # Ensure that metadata are present.
460
+ self.load_metadata(limited=False)
461
+
462
+ # Study date
463
+ study_date = get_pydicom_meta_tag(
464
+ dcm_seq=self.image_metadata,
465
+ tag=(0x0008, 0x0020),
466
+ tag_type="str"
467
+ )
468
+
469
+ if study_date is not None and not study_date == "":
470
+ metadata += [("study_date", study_date)]
471
+
472
+ # Study description
473
+ study_description = get_pydicom_meta_tag(
474
+ dcm_seq=self.image_metadata,
475
+ tag=(0x0008, 0x1030),
476
+ tag_type="str"
477
+ )
478
+
479
+ if study_description is not None and not study_description == "":
480
+ metadata += [("study_description", study_description)]
481
+
482
+ # Series description
483
+ series_description = get_pydicom_meta_tag(
484
+ dcm_seq=self.image_metadata,
485
+ tag=(0x0008, 0x103E),
486
+ tag_type="str"
487
+ )
488
+
489
+ if series_description is not None and not series_description == "":
490
+ metadata += [("series_description", series_description)]
491
+
492
+ # Series instance UID
493
+ if self.series_instance_uid is not None:
494
+ metadata += [("series_instance_uid", self.series_instance_uid)]
495
+
496
+ # Update object_metadata
497
+ self.object_metadata.update(dict(metadata))
498
+
452
499
  @staticmethod
453
500
  def _get_limited_metadata_tags():
454
501
  # Limited tags are read to populate basic
455
502
  return [
456
503
  (0x0008, 0x0018), # SOP instance UID
457
504
  (0x0008, 0x0060), # modality
458
- (0x0008, 0x1030), # series description
459
- (0x0008, 0x103E), # study description
505
+ (0x0008, 0x1030), # study description
506
+ (0x0008, 0x103E), # series description
460
507
  (0x0010, 0x0010), # patient name
461
508
  (0x0010, 0x0020), # patient id
462
509
  (0x0018, 0x0050), # slice thickness
@@ -86,11 +86,14 @@ class ImageDicomFilePT(ImageDicomFile):
86
86
  conversion_possible = False
87
87
  bqml_factor = 1.0
88
88
 
89
- # Get SUV conversion factor.
89
+ # Get SUV conversion factor and update the object_metadata attribute.
90
90
  if conversion_possible:
91
91
  suv_factor = self._get_suv_conversion_factor(new_suv_type=pet_suv_conversion)
92
+ self.object_metadata.update(dict([("suv_type", pet_suv_conversion)]))
93
+
92
94
  else:
93
95
  suv_factor = 1.0
96
+ self.object_metadata.update(dict([("suv_type", "none")]))
94
97
 
95
98
  # Update image_data
96
99
  image_data *= decay_factor * bqml_factor * suv_factor
@@ -116,6 +116,7 @@ class MaskDicomFileRTSTRUCT(MaskDicomFile):
116
116
  image.complete()
117
117
 
118
118
  self.load_metadata()
119
+ self.set_object_metadata()
119
120
  if not self.check_mask():
120
121
  return None
121
122
 
@@ -221,7 +222,8 @@ class MaskDicomFileRTSTRUCT(MaskDicomFile):
221
222
  image_spacing=temp_mask_object.image_spacing,
222
223
  image_origin=temp_mask_object.image_origin,
223
224
  image_orientation=temp_mask_object.image_orientation,
224
- image_dimensions=temp_mask_object.image_dimension
225
+ image_dimensions=temp_mask_object.image_dimension,
226
+ metadata=self.object_metadata
225
227
  )
226
228
  ]
227
229
 
@@ -70,6 +70,7 @@ class MaskDicomFileSEG(MaskDicomFile):
70
70
  def to_object(self, **kwargs) -> None | list[BaseMask]:
71
71
 
72
72
  self.load_metadata()
73
+ self.set_object_metadata()
73
74
  if not self.check_mask():
74
75
  return None
75
76
 
@@ -160,7 +161,8 @@ class MaskDicomFileSEG(MaskDicomFile):
160
161
  image_spacing=mask_spacing,
161
162
  image_origin=mask_origin,
162
163
  image_orientation=mask_orientation,
163
- image_dimensions=mask_dimension
164
+ image_dimensions=mask_dimension,
165
+ metadata=self.object_metadata
164
166
  )
165
167
  ]
166
168
 
@@ -4,6 +4,7 @@ import pandas as pd
4
4
  from typing import Any
5
5
 
6
6
  from mirp._data_import.dicom_file import ImageDicomFile
7
+ from mirp._data_import.generic_file import MaskFile
7
8
  from mirp._data_import.generic_file_stack import ImageFileStack, MaskFileStack
8
9
  from mirp._data_import.utilities import get_pydicom_meta_tag
9
10
 
@@ -230,6 +231,15 @@ class ImageDicomFileStack(ImageFileStack):
230
231
  "position_x": image_position_x
231
232
  }).sort_values(by=["position_z", "position_y", "position_x"], ignore_index=True)
232
233
 
234
+ def set_object_metadata(self):
235
+ """
236
+ Updates the object metadata that is passed to native image and mask classes in to_object. In this case,
237
+ object metadata is copied from the underlying slices.
238
+ """
239
+ self.image_file_objects[0].set_object_metadata()
240
+ self.object_metadata = self.image_file_objects[0].object_metadata
241
+ self.object_metadata.pop("file_name", None)
242
+
233
243
  def export_metadata(self) -> None | dict[str, Any]:
234
244
  metadata = super().export_metadata()
235
245
  additional_metadata = self.image_file_objects[0].export_metadata(only_self=True)
@@ -237,6 +247,27 @@ class ImageDicomFileStack(ImageFileStack):
237
247
 
238
248
  return metadata
239
249
 
250
+ def check_associated_masks(self):
251
+
252
+ if self.associated_masks is None:
253
+ return
254
+
255
+ for mask in self.associated_masks:
256
+ self._check_associated_mask_image_data(mask=mask)
257
+
258
+ def _check_associated_mask_image_data(self, mask: MaskFile):
259
+ """
260
+ Check
261
+
262
+ """
263
+ from mirp._data_import.dicom_file import MaskDicomFile
264
+
265
+ # Skip if the mask is a DICOM file.
266
+ if isinstance(mask, MaskDicomFile):
267
+ return
268
+
269
+ super()._check_associated_mask_image_data(mask=mask)
270
+
240
271
 
241
272
  class MaskDicomFileStack(ImageDicomFileStack, MaskFileStack):
242
273
 
@@ -5,11 +5,16 @@ import itertools
5
5
  import os
6
6
  import os.path
7
7
  import re
8
+ import sys
8
9
  import warnings
9
10
 
10
11
  import numpy as np
11
12
 
12
13
  from typing import Any
14
+ if sys.version_info >= (3, 11):
15
+ from typing import Self
16
+ else:
17
+ from typing_extensions import Self
13
18
 
14
19
  from mirp._images.base_image import BaseImage
15
20
  from mirp._images.generic_image import GenericImage
@@ -80,6 +85,9 @@ class ImageFile(BaseImage):
80
85
  self.dir_path = dir_path
81
86
  self.associated_masks = None
82
87
 
88
+ # Object metadata that will be exported when converted to native format.
89
+ self.object_metadata: dict[str, str] = dict()
90
+
83
91
  def is_stackable(self, stack_images: str):
84
92
  raise NotImplementedError(
85
93
  f"DEV: There is (intentionally) no generic implementation of is_stackable. Please specify "
@@ -773,6 +781,7 @@ class ImageFile(BaseImage):
773
781
  self.complete()
774
782
  self.stack_slices()
775
783
  self.update_image_data()
784
+ self.set_object_metadata()
776
785
 
777
786
  return GenericImage(
778
787
  sample_name=self.sample_name,
@@ -781,9 +790,29 @@ class ImageFile(BaseImage):
781
790
  image_spacing=self.image_spacing,
782
791
  image_origin=self.image_origin,
783
792
  image_orientation=self.image_orientation,
784
- image_dimensions=self.image_dimension
793
+ image_dimensions=self.image_dimension,
794
+ metadata=self.object_metadata
785
795
  )
786
796
 
797
+ def set_object_metadata(self):
798
+ """
799
+ Updates the object metadata that is passed to native image and mask classes in to_object.
800
+ """
801
+ attributes = []
802
+ # Add file name to object metadata.
803
+ if self.file_name is not None:
804
+ attributes += [("file_name", self.file_name)]
805
+
806
+ # Add directory to object metadata.
807
+ if self.dir_path is not None:
808
+ attributes += [("dir_path", self.dir_path)]
809
+
810
+ # Add file type to object metadata.
811
+ if self.file_type is not None:
812
+ attributes += [("file_type", self.file_type)]
813
+
814
+ self.object_metadata.update(dict(attributes))
815
+
787
816
  def export_metadata(self, **kwargs) -> None | dict[str, Any]:
788
817
  # Get general attributes.
789
818
  parent_attributes = self._get_export_attributes()
@@ -816,6 +845,53 @@ class ImageFile(BaseImage):
816
845
  def describe_self(self):
817
846
  return f"{self._get_export_attributes()}"
818
847
 
848
+ def check_associated_masks(self):
849
+ if self.associated_masks is None:
850
+ return
851
+
852
+ for mask in self.associated_masks:
853
+ self._check_associated_mask_image_data(mask=mask)
854
+
855
+ def _check_associated_mask_image_data(self, mask: Self):
856
+ """Check whether image and mask plausibly share the same frame of reference."""
857
+
858
+ problem_list = []
859
+ # Mismatch in grid dimension
860
+ if isinstance(self.image_dimension, tuple) and isinstance(mask.image_dimension, tuple):
861
+ if not np.array_equal(self.image_dimension, mask.image_dimension):
862
+ problem_list += [
863
+ f"different dimensions: \n\t\timage: {self.image_dimension}\n\t\tmask: {mask.image_dimension}"
864
+ ]
865
+
866
+ # Mismatch in origin
867
+ if isinstance(self.image_origin, tuple) and isinstance(mask.image_origin, tuple):
868
+ if not np.allclose(self.image_origin, mask.image_origin):
869
+ problem_list += [
870
+ f"different origin: \n\t\timage: {self.image_origin}\n\t\tmask: {mask.image_origin}"
871
+ ]
872
+
873
+ # Mismatch in spacing
874
+ if isinstance(self.image_spacing, tuple) and isinstance(mask.image_spacing, tuple):
875
+ if not np.allclose(self.image_spacing, mask.image_spacing):
876
+ problem_list += [
877
+ f"different spacing: \n\t\timage: {self.image_spacing}\n\t\tmask: {mask.image_spacing}"
878
+ ]
879
+
880
+ # Mismatch in orientation
881
+ if isinstance(self.image_orientation, np.ndarray) and isinstance(mask.image_orientation, np.ndarray):
882
+ if not np.allclose(self.image_orientation, mask.image_orientation):
883
+ problem_list += [
884
+ f"different orientation: \n\t\timage: {np.ravel(self.image_orientation)}\n\t\tmask: "
885
+ f"{np.ravel(mask.image_orientation)}"
886
+ ]
887
+
888
+ if len(problem_list) > 0:
889
+ warnings.warn(
890
+ f"Image {self.describe_self()} and mask {mask.describe_self()} may not have the same frame of "
891
+ f"reference. Please check if segmentation masks are placed correctly:\n\t" + "\n\t".join(problem_list),
892
+ UserWarning
893
+ )
894
+
819
895
 
820
896
  class MaskFile(ImageFile):
821
897
 
@@ -1009,6 +1085,7 @@ class MaskFile(ImageFile):
1009
1085
  self.complete()
1010
1086
  self.stack_slices()
1011
1087
  self.update_image_data()
1088
+ self.set_object_metadata()
1012
1089
  self.check_mask(raise_error=True)
1013
1090
 
1014
1091
  mask_list = []
@@ -1052,7 +1129,8 @@ class MaskFile(ImageFile):
1052
1129
  image_spacing=self.image_spacing,
1053
1130
  image_origin=self.image_origin,
1054
1131
  image_orientation=self.image_orientation,
1055
- image_dimensions=self.image_dimension
1132
+ image_dimensions=self.image_dimension,
1133
+ metadata=self.object_metadata
1056
1134
  )
1057
1135
  ]
1058
1136
 
@@ -1174,5 +1252,6 @@ class MaskFullImage(MaskFile):
1174
1252
  image_spacing=image.image_spacing,
1175
1253
  image_origin=image.image_origin,
1176
1254
  image_orientation=image.image_orientation,
1177
- image_dimensions=image.image_dimension
1255
+ image_dimensions=image.image_dimension,
1256
+ metadata=self.object_metadata
1178
1257
  )]
@@ -1,5 +1,6 @@
1
1
  import itk
2
2
  import os.path
3
+ import copy
3
4
  import numpy as np
4
5
 
5
6
  from mirp._data_import.generic_file import ImageFile, MaskFile
@@ -69,6 +70,20 @@ class ImageITKFile(ImageFile):
69
70
  f"[{self.describe_self()}]"
70
71
  )
71
72
 
73
+ def copy(self):
74
+ # Remove metadata during copying to prevent ITK SwigPyObjects from being pickled (which produces errors).
75
+ temporary_metadata = self.image_metadata
76
+ self.remove_metadata()
77
+
78
+ # Create a copy without copying any metadata.
79
+ image_copy = copy.deepcopy(self)
80
+
81
+ # Restore metadata.
82
+ self.image_metadata = temporary_metadata
83
+ image_copy.image_metadata = temporary_metadata
84
+
85
+ return image_copy
86
+
72
87
  def _complete_image_origin(self, force=False):
73
88
  if self.image_origin is None:
74
89
  self.load_metadata()
@@ -13,6 +13,7 @@ class BaseImage:
13
13
  image_orientation: None | np.ndarray = None,
14
14
  image_spacing: None | tuple[float, ...] = None,
15
15
  image_dimensions: None | tuple[int, ...] = None,
16
+ metadata: None | dict[str, str] = None,
16
17
  **kwargs
17
18
  ):
18
19
  # Make cooperative
@@ -32,6 +33,21 @@ class BaseImage:
32
33
  # Set sample name.
33
34
  self.sample_name = sample_name
34
35
 
36
+ # Set metadata. Entries with empty values (None or "") are removed from the metadata dict to avoid
37
+ # polluting the dictionary with unset entries.
38
+ if isinstance(metadata, dict) and len(metadata) > 0:
39
+ metadata = copy.deepcopy(metadata)
40
+ remove_keys = [key for key, value in metadata.items() if value is None or value == ""]
41
+ for current_key in remove_keys:
42
+ metadata.pop(current_key, None)
43
+
44
+ if len(metadata) > 0:
45
+ self.object_metadata = metadata
46
+ else:
47
+ self.object_metadata = dict()
48
+ else:
49
+ self.object_metadata = dict()
50
+
35
51
  def is_isotropic(self, by_slice: bool) -> bool:
36
52
  if by_slice:
37
53
  spacing = np.array(self.image_spacing)[[1, 2]]
@@ -85,6 +85,7 @@ class GenericImage(BaseImage):
85
85
  self.image_spacing = copy.deepcopy(template.image_spacing)
86
86
  self.image_dimension = copy.deepcopy(template.image_dimension)
87
87
  self.sample_name = copy.deepcopy(template.sample_name)
88
+ self.object_metadata = copy.deepcopy(template.object_metadata)
88
89
 
89
90
  # Attributes from GenericImage
90
91
  self.separate_slices = template.separate_slices
@@ -1314,6 +1315,10 @@ class GenericImage(BaseImage):
1314
1315
  ("image_orientation", self.image_orientation)
1315
1316
  ]
1316
1317
 
1318
+ attributes = dict(attributes)
1319
+ if len(self.object_metadata) > 0:
1320
+ attributes.update(self.object_metadata)
1321
+
1317
1322
  return dict(attributes)
1318
1323
 
1319
1324
  def parse_feature_names(self, x: None | pd.DataFrame) -> pd.DataFrame:
@@ -1,3 +1,5 @@
1
+ import warnings
2
+
1
3
  import numpy as np
2
4
  import pandas as pd
3
5
  import copy
@@ -130,9 +132,13 @@ class BaseMask:
130
132
  if (spline_order is None or anti_aliasing is None or anti_aliasing is None) and settings is None:
131
133
  raise ValueError("None of the parameters for registration can be set.")
132
134
 
135
+ # Check if there is any mask data to work with.
133
136
  if self.is_empty():
134
137
  return
135
138
 
139
+ # Check if the mask is empty.
140
+ empty_before_registration = self.is_empty_mask()
141
+
136
142
  if spline_order is None:
137
143
  spline_order = self.roi.get_interpolation_spline_order(settings=settings)
138
144
  if anti_aliasing is None:
@@ -147,6 +153,16 @@ class BaseMask:
147
153
  anti_aliasing_smoothing_beta=anti_aliasing_smoothing_beta,
148
154
  mode="constant"
149
155
  )
156
+
157
+ # Warn if a previously non-empty mask is empty after registration. This may indicate issues with the frame of
158
+ # reference, i.e. world coordinate systems that do not have the same definition.
159
+ if self.is_empty_mask() and not empty_before_registration:
160
+ warnings.warn(
161
+ f"The {self.roi_name} mask is empty after registering it to its image. Please check that mask and "
162
+ f"image use the same frame of reference.",
163
+ UserWarning
164
+ )
165
+
150
166
  if self.roi_intensity is not None:
151
167
  self.roi_intensity.register(
152
168
  image=image,
@@ -3,10 +3,9 @@ import logging
3
3
  import sys
4
4
  import warnings
5
5
 
6
- from typing import Generator
6
+ from typing import Generator, Any
7
7
 
8
8
  import pandas as pd
9
- import numpy as np
10
9
 
11
10
  from mirp.settings.generic import SettingsClass
12
11
  from mirp.settings.feature_parameters import FeatureExtractionSettingsClass
@@ -632,24 +631,57 @@ class StandardWorkflow(BaseWorkflow):
632
631
  mask: BaseMask
633
632
  ) -> pd.DataFrame:
634
633
 
635
- return pd.DataFrame({
636
- "sample_name": image.sample_name,
637
- "image_settings_id": self.settings_name if self.settings_name is not None else np.nan,
638
- "image_modality": image.modality,
639
- "image_voxel_size_x": image.image_spacing[2],
640
- "image_voxel_size_y": image.image_spacing[1],
641
- "image_voxel_size_z": image.image_spacing[0],
642
- "image_noise_level": image.noise_level if image.noise_level is not None else 0.0,
643
- "image_noise_iteration_id": image.noise_iteration_id if image.noise_iteration_id is not None else np.nan,
644
- "image_rotation_angle": image.rotation_angle if image.rotation_angle is not None else 0.0,
645
- "image_translation_x": image.translation[2] if image.translation is not None else 0.0,
646
- "image_translation_y": image.translation[1] if image.translation is not None else 0.0,
647
- "image_translation_z": image.translation[0] if image.translation is not None else 0.0,
648
- "image_mask_name": mask.roi_name,
649
- "image_mask_randomise_id": mask.roi.slic_randomisation_id if mask.roi.slic_randomisation_id is not None
650
- else np.nan,
651
- "image_mask_adapt_size": mask.roi.alteration_size if mask.roi.alteration_size is not None else 0.0
652
- }, index=[0])
634
+ def _from_dict(
635
+ x: dict[str, str],
636
+ key: str,
637
+ default: Any = None,
638
+ ii: None | int = None
639
+ ):
640
+ value = x[key] if key in x else default
641
+ if value is not None and ii is not None:
642
+ value = value[ii]
643
+
644
+ if value is not None and isinstance(value, str) and value == "":
645
+ value = default
646
+
647
+ return value
648
+
649
+ image_metadata = image.get_export_attributes()
650
+ mask_metadata = mask.get_export_attributes()
651
+
652
+ # The name of the current settings configuration derives from the workflow, not image or mask.
653
+ settings_name = _from_dict({"settings_name": self.settings_name}, "settings_name")
654
+
655
+ feature_set_details = {
656
+ "sample_name": _from_dict(image_metadata, "sample_name"),
657
+ "image_file_name": _from_dict(image_metadata, "file_name"),
658
+ "image_directory": _from_dict(image_metadata, "dir_path"),
659
+ "image_study_date": _from_dict(image_metadata, "study_date"),
660
+ "image_study_description": _from_dict(image_metadata, "study_description"),
661
+ "image_series_description": _from_dict(image_metadata, "series_description"),
662
+ "image_series_instance_uid": _from_dict(image_metadata, "series_instance_uid"),
663
+ "image_modality": _from_dict(image_metadata, "modality", "generic"),
664
+ "image_pet_suv_type": _from_dict(image_metadata, "suv_type"),
665
+ "image_mask_label": _from_dict(mask_metadata, "roi_name"),
666
+ "image_mask_file_name": _from_dict(mask_metadata, "file_name"),
667
+ "image_mask_directory": _from_dict(mask_metadata, "dir_path"),
668
+ "image_mask_series_description": _from_dict(mask_metadata, "series_description"),
669
+ "image_mask_series_instance_uid": _from_dict(mask_metadata, "series_instance_uid"),
670
+ "image_settings_id": settings_name,
671
+ "image_voxel_size_x": _from_dict(image_metadata, "image_spacing", ii=2),
672
+ "image_voxel_size_y": _from_dict(image_metadata, "image_spacing", ii=1),
673
+ "image_voxel_size_z": _from_dict(image_metadata, "image_spacing", ii=0),
674
+ "image_noise_level": _from_dict(image_metadata, "noise_level"),
675
+ "image_noise_iteration_id": _from_dict(image_metadata, "noise_id"),
676
+ "image_rotation_angle": _from_dict(image_metadata, "rotation", default=0.0),
677
+ "image_translation_x": _from_dict(image_metadata, "translation", ii=2, default=0.0),
678
+ "image_translation_y": _from_dict(image_metadata, "translation", ii=1, default=0.0),
679
+ "image_translation_z": _from_dict(image_metadata, "translation", ii=0, default=0.0),
680
+ "image_mask_randomise_id": _from_dict(mask_metadata, "mask_randomisation_id"),
681
+ "image_mask_adapt_size": _from_dict(mask_metadata, "mask_alteration_size", default=0.0)
682
+ }
683
+
684
+ return pd.DataFrame(feature_set_details, index=[0])
653
685
 
654
686
  def deep_learning_conversion(
655
687
  self,
@@ -580,6 +580,10 @@ def _generate_feature_and_image_extraction_workflows(
580
580
  ) -> Generator[StandardWorkflow, None, None]:
581
581
 
582
582
  for image_file in image_list:
583
+
584
+ # Check for potentially problematic stuff.
585
+ image_file.check_associated_masks()
586
+
583
587
  for current_settings in settings:
584
588
 
585
589
  if not current_settings.feature_extr.has_any_feature_family() and (
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: mirp
3
- Version: 2.2.2
3
+ Version: 2.2.3
4
4
  Summary: A package for standardised processing of medical imaging and computation of quantitative features.
5
5
  Author-email: Alex Zwanenburg <alexander.zwanenburg@nct-dresden.de>
6
6
  License: European Union Public Licence
@@ -354,8 +354,8 @@ pip install mirp
354
354
 
355
355
  MIRP can be used to compute quantitative features from regions of interest in images in an IBSI-compliant manner
356
356
  using a standardized workflow This requires both images and masks. MIRP can process DICOM, NIfTI, NRRD and numpy
357
- images. Masks are DICOM radiotherapy structure sets (RTSTRUCT), or volumetric data with integer labels (e.g. 1, 2,
358
- etc.).
357
+ images. Masks are DICOM radiotherapy structure sets (RTSTRUCT), DICOM segmentation (SEG) or volumetric data with
358
+ integer labels (e.g. 1, 2, etc.).
359
359
 
360
360
  Below is a minimal working example for extracting features from a single image file and its mask.
361
361
 
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "mirp"
7
- version = "2.2.2"
7
+ version = "2.2.3"
8
8
  description = "A package for standardised processing of medical imaging and computation of quantitative features."
9
9
  authors = [
10
10
  {name = "Alex Zwanenburg", email = "alexander.zwanenburg@nct-dresden.de"}
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes