@@ -8,8 +8,8 @@ https://badge.fury.io/py/medio)
8
8
9
9
** Medical images I/O python package**
10
10
11
- This package unifies the io engines of itk, nibabel, and pydicom (and dicom-numpy) packages in a
12
- simple and comprehensive interface.
11
+ This package unifies the io engines of itk, nibabel, and pydicom (including dicom-numpy) packages
12
+ in a simple and comprehensive interface.
13
13
14
14
It includes conversion between the metadata conventions, reorientations, affine matrix computation
15
15
for itk and pydicom and saving dicom series or file.
@@ -57,7 +57,8 @@ channels_axis=-1, coord_sys='itk', **kwargs)`
57
57
- ` input_path ` : * path-like* <br >
58
58
Path for the data to be read (str or pathlib.Path object for example). It can be a file or a
59
59
folder (in the case of a dicom series). It is the only required parameter.
60
- If the input path is a folder, it should contain a single dicom series.
60
+ If the input path is a folder, it should contain a single dicom series.
61
+ If there is more than a single series, the optional parameter ` series ` can help.
61
62
- Returns: * array, metadata* <br >
62
63
array of type * numpy.ndarray* and metadata of type * medio.MetaData* . The first is a numpy array
63
64
of the image, and the second is a metadata object of the image (see [ MetaData] ( #metadata ) class
@@ -97,27 +98,37 @@ Optional parameters:
97
98
` itk.ctype('unsigned short') == itk.US `
98
99
- ` fallback_only=True ` : * bool* <br >
99
100
If True, the pixel type is automatically found and if failed then ` pixel_type ` is used
100
- (` pixel_type ` must be not None in this case).<br >
101
+ (` pixel_type ` must not be None in this case).<br >
101
102
Note: if ` itk.imread(input_path) ` fails, using ` fallback_only=True ` will result in a slightly
102
- inferior performance. If you know what is pixel-type of the image, you can set it with
103
- ` pixel_type ` and use ` fallback_only=False ` .
103
+ inferior performance. If you know what is pixel-type of the image beforehand, you can set it
104
+ with ` pixel_type ` and use ` fallback_only=False ` .
105
+ - ` series=None ` : * str, int or None* <br >
106
+ If ` input_path ` is a directory that contains multiple dicom series, selecting a specific one is
107
+ possible with the ` series ` argument. It can be the exact series instance UID (str), an int
108
+ between 0 and n-1, where n is the number of series in the directory, or None.<br >
109
+ If ` series ` is None and there are multiple series in the directory, a detailed error message
110
+ is raised.
104
111
105
112
- 'pydicom' backend
106
113
- ` globber='*' ` : * str* <br >
107
- Relevant for a directory - glob pattern for selecting the series files (all files by default).
114
+ Relevant for a directory - glob pattern for selecting the series files (all the files in the
115
+ directory by default).
108
116
- ` allow_default_affine=False ` : * bool* <br >
109
- Relevant for multiframe dicom file - if True and the dicom miss some physical tags for the
117
+ Relevant for a multiframe dicom file - if True and the dicom misses some physical tags for the
110
118
affine calculation, use a default affine value.
119
+ - ` series=None ` : * str, int or None* <br >
120
+ The counterpart of ` series ` in itk backend, see the explanation above.
111
121
112
122
### save_img
113
123
`medio.save_img(filename, np_image, metadata, use_original_ornt=True, backend=None, dtype=None,
114
124
channels_axis=None, mkdir=False, parents=False, ** kwargs)`
115
125
- ` filename ` : * path-like* <br >
116
- The file to be saved.
126
+ The file to be saved, including the format suffix .
117
127
- ` np_image ` : * numpy.ndarray* <br >
118
128
The image array.
119
129
- ` metadata ` : * medio.MetaData* <br >
120
- The corresponding metadata.
130
+ The corresponding metadata, from ` medio.read_img ` for example. In the absence of a known
131
+ metadata, a default one can be constructed with ` medio.MetaData(np.eye(4)) ` .
121
132
122
133
Optional parameters:
123
134
- ` use_original_ornt ` : * bool* <br >
@@ -137,7 +148,7 @@ Optional parameters:
137
148
'itk' backend optional parameters (` **kwargs ` ):
138
149
- ` allow_dcm_reorient=False ` : * bool* <br >
139
150
When saving a dicom file ('.dcm' or '.dicom' suffix) the image orientation should be right-handed.
140
- If it is left-handed, the image can be reoriented to a right-handed orientation with setting this
151
+ If it is left-handed, the image can be reoriented to a right-handed orientation by setting this
141
152
parameter to True, which flips the last axis direction.
142
153
- ` compression=False ` : * bool* <br >
143
154
Whether to use compression in itk writer. Using a '.nii.gz' suffix in ` filename ` also compresses
@@ -151,16 +162,18 @@ Save a 3d numpy array `np_image` as a dicom series of 2d slices in the directory
151
162
backend).
152
163
153
164
- ` dirname ` : * path-like* <br >
154
- The directory to save the files in (str or pathlib.Path). If it exists - must be empty.
165
+ The directory to save the files in (str or pathlib.Path). If it exists, it must be empty (unless
166
+ ` exist_ok ` is True).
155
167
156
168
The other parameters: ` np_image ` , ` metadata ` , ` use_original_ornt ` , ` dtype ` , ` channels_axis ` ,
157
- ` parents ` and ` allow_dcm_reorient ` are equivalent to those used in [ save_img] ( #save_img ) .
169
+ ` parents ` and ` allow_dcm_reorient ` are equivalent to those of [ save_img] ( #save_img ) .
158
170
159
171
Additional optional parameters (` **kwargs ` ):
160
172
- ` exist_ok ` : * bool* <br >
161
173
If True, non-empty existing directory will not raise an error.
162
174
- ` pattern='IM{}.dcm' ` : * str* <br >
163
175
Pattern for the filenames to save, including a placeholder ('` {} ` ') for the slice number.
176
+ For example, one can use: ` pattern={:03d} ` .
164
177
- ` metadata_dict=None ` : * dict or None* <br >
165
178
Dictionary of metadata for adding tags or overriding the default values. For example,
166
179
` metadata_dict={'0008|0060': 'US'} ` will override the default 'CT' modality and set it to 'US'
@@ -206,16 +219,16 @@ A MetaData object 'metadata' is mainly comprised of:
206
219
- ` metadata.coord_sys ` : coordinate system ('itk' or 'nib')
207
220
- ` metadata.orig_ornt ` : the original orientation of the image (used for saving)
208
221
- ` metadata.header ` : a dictionary that includes additional metadata properties when ` header=True `
209
- in ` read_img `
222
+ in ` read_img ` , otherwise None
210
223
211
224
Other properties of the metadata are derived from the affine:
212
225
- ` metadata.spacing ` : voxels spacing (a reference to ` metadata.affine.spacing ` )
213
- - ` metadata.ornt ` : the current image orientation (also depends on the coordinate system)
226
+ - ` metadata.ornt ` : the current image orientation (depends on the coordinate system ` coord_sys ` )
214
227
215
228
All these properties can be viewed easily in the console:
216
229
``` python
217
230
>> > import medio
218
- >> > array, metadata = medio.read_img(' avg152T1_LR_nifti.nii.gz' )
231
+ >> > array, metadata = medio.read_img(' avg152T1_LR_nifti.nii.gz' , header = False )
219
232
>> > print (metadata)
220
233
Affine:
221
234
[[ - 2 . 0. 0. 90.]
@@ -246,8 +259,9 @@ For example, 'RAS' orientation in itk:
246
259
'RAS' in nib - also 'RAS+':
247
260
- Left to ** R** ight, Posterior to ** A** nterior, Inferior to ** S** uperior
248
261
249
- Note that the conventions are opposite. For stability reasons, we use itk convention
250
- by default for ` read_img ` 's argument ` desired_ornt ` .
262
+ Note that the conventions are opposite. For stability reasons, we use itk convention by default
263
+ for ` read_img ` 's argument ` desired_ornt ` , although one can choose otherwise with the parameter
264
+ ` coord_sys ` .
251
265
252
266
For further discussion see
253
267
[ NiBabel's image orientation documentation] (
0 commit comments