-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
array.py
1717 lines (1381 loc) · 68.4 KB
/
array.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Copyright (c) MONAI Consortium
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
A collection of "vanilla" transforms for utility functions
https://github.com/Project-MONAI/MONAI/wiki/MONAI_Design
"""
from __future__ import annotations
import logging
import sys
import time
import warnings
from collections.abc import Mapping, Sequence
from copy import deepcopy
from functools import partial
from typing import Any, Callable
import numpy as np
import torch
import torch.nn as nn
from monai.config import DtypeLike
from monai.config.type_definitions import NdarrayOrTensor
from monai.data.meta_obj import get_track_meta
from monai.data.meta_tensor import MetaTensor
from monai.data.utils import no_collation
from monai.networks.layers.simplelayers import (
ApplyFilter,
EllipticalFilter,
GaussianFilter,
LaplaceFilter,
MeanFilter,
SavitzkyGolayFilter,
SharpenFilter,
median_filter,
)
from monai.transforms.inverse import InvertibleTransform
from monai.transforms.traits import MultiSampleTrait
from monai.transforms.transform import Randomizable, RandomizableTrait, RandomizableTransform, Transform
from monai.transforms.utils import (
extreme_points_to_image,
get_extreme_points,
map_binary_to_indices,
map_classes_to_indices,
)
from monai.transforms.utils_pytorch_numpy_unification import concatenate, in1d, moveaxis, unravel_indices
from monai.utils import (
TraceKeys,
convert_data_type,
convert_to_cupy,
convert_to_numpy,
convert_to_tensor,
deprecated,
ensure_tuple,
look_up_option,
min_version,
optional_import,
)
from monai.utils.enums import TransformBackends
from monai.utils.misc import is_module_ver_at_least
from monai.utils.type_conversion import convert_to_dst_type, get_equivalent_dtype
PILImageImage, has_pil = optional_import("PIL.Image", name="Image")
pil_image_fromarray, _ = optional_import("PIL.Image", name="fromarray")
cp, has_cp = optional_import("cupy")
__all__ = [
"Identity",
"RandIdentity",
"AsChannelFirst",
"AsChannelLast",
"AddChannel",
"AddCoordinateChannels",
"EnsureChannelFirst",
"EnsureType",
"RepeatChannel",
"RemoveRepeatedChannel",
"SplitDim",
"SplitChannel",
"CastToType",
"ToTensor",
"ToNumpy",
"ToPIL",
"Transpose",
"SqueezeDim",
"DataStats",
"SimulateDelay",
"Lambda",
"RandLambda",
"LabelToMask",
"FgBgToIndices",
"ClassesToIndices",
"ConvertToMultiChannelBasedOnBratsClasses",
"AddExtremePointsChannel",
"TorchVision",
"MapLabelValue",
"IntensityStats",
"ToDevice",
"CuCIM",
"RandCuCIM",
"ToCupy",
"ImageFilter",
"RandImageFilter",
]
class Identity(Transform):
"""
Do nothing to the data.
As the output value is same as input, it can be used as a testing tool to verify the transform chain,
Compose or transform adaptor, etc.
"""
backend = [TransformBackends.TORCH, TransformBackends.NUMPY]
def __call__(self, img: NdarrayOrTensor) -> NdarrayOrTensor:
"""
Apply the transform to `img`.
"""
return img
class RandIdentity(RandomizableTrait):
"""
Do nothing to the data. This transform is random, so can be used to stop the caching of any
subsequent transforms.
"""
backend = [TransformBackends.TORCH, TransformBackends.NUMPY]
def __call__(self, data: Any) -> Any:
return data
@deprecated(since="0.8", msg_suffix="please use MetaTensor data type and monai.transforms.EnsureChannelFirst instead.")
class AsChannelFirst(Transform):
"""
Change the channel dimension of the image to the first dimension.
Most of the image transformations in ``monai.transforms``
assume the input image is in the channel-first format, which has the shape
(num_channels, spatial_dim_1[, spatial_dim_2, ...]).
This transform could be used to convert, for example, a channel-last image array in shape
(spatial_dim_1[, spatial_dim_2, ...], num_channels) into the channel-first format,
so that the multidimensional image array can be correctly interpreted by the other transforms.
Args:
channel_dim: which dimension of input image is the channel, default is the last dimension.
"""
backend = [TransformBackends.TORCH, TransformBackends.NUMPY]
def __init__(self, channel_dim: int = -1) -> None:
if not (isinstance(channel_dim, int) and channel_dim >= -1):
raise ValueError(f"invalid channel dimension ({channel_dim}).")
self.channel_dim = channel_dim
def __call__(self, img: NdarrayOrTensor) -> NdarrayOrTensor:
"""
Apply the transform to `img`.
"""
out: NdarrayOrTensor = convert_to_tensor(moveaxis(img, self.channel_dim, 0), track_meta=get_track_meta())
return out
class AsChannelLast(Transform):
"""
Change the channel dimension of the image to the last dimension.
Some of other 3rd party transforms assume the input image is in the channel-last format with shape
(spatial_dim_1[, spatial_dim_2, ...], num_channels).
This transform could be used to convert, for example, a channel-first image array in shape
(num_channels, spatial_dim_1[, spatial_dim_2, ...]) into the channel-last format,
so that MONAI transforms can construct a chain with other 3rd party transforms together.
Args:
channel_dim: which dimension of input image is the channel, default is the first dimension.
"""
backend = [TransformBackends.TORCH, TransformBackends.NUMPY]
def __init__(self, channel_dim: int = 0) -> None:
if not (isinstance(channel_dim, int) and channel_dim >= -1):
raise ValueError(f"invalid channel dimension ({channel_dim}).")
self.channel_dim = channel_dim
def __call__(self, img: NdarrayOrTensor) -> NdarrayOrTensor:
"""
Apply the transform to `img`.
"""
out: NdarrayOrTensor = convert_to_tensor(moveaxis(img, self.channel_dim, -1), track_meta=get_track_meta())
return out
@deprecated(since="0.8", msg_suffix="please use MetaTensor data type and monai.transforms.EnsureChannelFirst instead.")
class AddChannel(Transform):
"""
Adds a 1-length channel dimension to the input image.
Most of the image transformations in ``monai.transforms``
assumes the input image is in the channel-first format, which has the shape
(num_channels, spatial_dim_1[, spatial_dim_2, ...]).
This transform could be used, for example, to convert a (spatial_dim_1[, spatial_dim_2, ...])
spatial image into the channel-first format so that the
multidimensional image array can be correctly interpreted by the other
transforms.
"""
backend = [TransformBackends.TORCH, TransformBackends.NUMPY]
def __call__(self, img: NdarrayOrTensor) -> NdarrayOrTensor:
"""
Apply the transform to `img`.
"""
out: NdarrayOrTensor = convert_to_tensor(img[None], track_meta=get_track_meta())
return out
class EnsureChannelFirst(Transform):
"""
Adjust or add the channel dimension of input data to ensure `channel_first` shape.
This extracts the `original_channel_dim` info from provided meta_data dictionary or MetaTensor input. This value
should state which dimension is the channel dimension so that it can be moved forward, or contain "no_channel" to
state no dimension is the channel and so a 1-size first dimension is to be added.
Args:
strict_check: whether to raise an error when the meta information is insufficient.
channel_dim: This argument can be used to specify the original channel dimension (integer) of the input array.
It overrides the `original_channel_dim` from provided MetaTensor input.
If the input array doesn't have a channel dim, this value should be ``'no_channel'``.
If this is set to `None`, this class relies on `img` or `meta_dict` to provide the channel dimension.
"""
backend = [TransformBackends.TORCH, TransformBackends.NUMPY]
def __init__(self, strict_check: bool = True, channel_dim: None | str | int = None):
self.strict_check = strict_check
self.input_channel_dim = channel_dim
def __call__(self, img: torch.Tensor, meta_dict: Mapping | None = None) -> torch.Tensor:
"""
Apply the transform to `img`.
"""
if not isinstance(img, MetaTensor) and not isinstance(meta_dict, Mapping):
if self.input_channel_dim is None:
msg = "Metadata not available and channel_dim=None, EnsureChannelFirst is not in use."
if self.strict_check:
raise ValueError(msg)
warnings.warn(msg)
return img
else:
img = MetaTensor(img)
if isinstance(img, MetaTensor):
meta_dict = img.meta
channel_dim = meta_dict.get("original_channel_dim", None) if isinstance(meta_dict, Mapping) else None
if self.input_channel_dim is not None:
channel_dim = self.input_channel_dim
if channel_dim is None:
msg = "Unknown original_channel_dim in the MetaTensor meta dict or `meta_dict` or `channel_dim`."
if self.strict_check:
raise ValueError(msg)
warnings.warn(msg)
return img
# track the original channel dim
if isinstance(meta_dict, dict):
meta_dict["original_channel_dim"] = channel_dim
if channel_dim == "no_channel":
result = img[None]
else:
result = moveaxis(img, channel_dim, 0) # type: ignore
return convert_to_tensor(result, track_meta=get_track_meta()) # type: ignore
class RepeatChannel(Transform):
"""
Repeat channel data to construct expected input shape for models.
The `repeats` count includes the origin data, for example:
``RepeatChannel(repeats=2)([[1, 2], [3, 4]])`` generates: ``[[1, 2], [1, 2], [3, 4], [3, 4]]``
Args:
repeats: the number of repetitions for each element.
"""
backend = [TransformBackends.TORCH]
def __init__(self, repeats: int) -> None:
if repeats <= 0:
raise ValueError(f"repeats count must be greater than 0, got {repeats}.")
self.repeats = repeats
def __call__(self, img: NdarrayOrTensor) -> NdarrayOrTensor:
"""
Apply the transform to `img`, assuming `img` is a "channel-first" array.
"""
repeat_fn = torch.repeat_interleave if isinstance(img, torch.Tensor) else np.repeat
return convert_to_tensor(repeat_fn(img, self.repeats, 0), track_meta=get_track_meta()) # type: ignore
class RemoveRepeatedChannel(Transform):
"""
RemoveRepeatedChannel data to undo RepeatChannel
The `repeats` count specifies the deletion of the origin data, for example:
``RemoveRepeatedChannel(repeats=2)([[1, 2], [1, 2], [3, 4], [3, 4]])`` generates: ``[[1, 2], [3, 4]]``
Args:
repeats: the number of repetitions to be deleted for each element.
"""
backend = [TransformBackends.TORCH, TransformBackends.NUMPY]
def __init__(self, repeats: int) -> None:
if repeats <= 0:
raise ValueError(f"repeats count must be greater than 0, got {repeats}.")
self.repeats = repeats
def __call__(self, img: NdarrayOrTensor) -> NdarrayOrTensor:
"""
Apply the transform to `img`, assuming `img` is a "channel-first" array.
"""
if img.shape[0] < 2:
raise ValueError(f"Image must have more than one channel, got {img.shape[0]} channels.")
out: NdarrayOrTensor = convert_to_tensor(img[:: self.repeats, :], track_meta=get_track_meta())
return out
class SplitDim(Transform, MultiSampleTrait):
"""
Given an image of size X along a certain dimension, return a list of length X containing
images. Useful for converting 3D images into a stack of 2D images, splitting multichannel inputs into
single channels, for example.
Note: `torch.split`/`np.split` is used, so the outputs are views of the input (shallow copy).
Args:
dim: dimension on which to split
keepdim: if `True`, output will have singleton in the split dimension. If `False`, this
dimension will be squeezed.
update_meta: whether to update the MetaObj in each split result.
"""
backend = [TransformBackends.TORCH, TransformBackends.NUMPY]
def __init__(self, dim: int = -1, keepdim: bool = True, update_meta=True) -> None:
self.dim = dim
self.keepdim = keepdim
self.update_meta = update_meta
def __call__(self, img: torch.Tensor) -> list[torch.Tensor]:
"""
Apply the transform to `img`.
"""
n_out = img.shape[self.dim]
if n_out <= 1:
raise RuntimeError(f"Input image is singleton along dimension to be split, got shape {img.shape}.")
if isinstance(img, torch.Tensor):
outputs = list(torch.split(img, 1, self.dim))
else:
outputs = np.split(img, n_out, self.dim)
for idx, item in enumerate(outputs):
if not self.keepdim:
outputs[idx] = item.squeeze(self.dim)
if self.update_meta and isinstance(img, MetaTensor):
if not isinstance(item, MetaTensor):
item = MetaTensor(item, meta=img.meta)
if self.dim == 0: # don't update affine if channel dim
continue
ndim = len(item.affine)
shift = torch.eye(ndim, device=item.affine.device, dtype=item.affine.dtype)
shift[self.dim - 1, -1] = idx
item.affine = item.affine @ shift
return outputs
@deprecated(since="0.8", msg_suffix="please use `SplitDim` instead.")
class SplitChannel(SplitDim):
"""
Split Numpy array or PyTorch Tensor data according to the channel dim.
It can help applying different following transforms to different channels.
Note: `torch.split`/`np.split` is used, so the outputs are views of the input (shallow copy).
Args:
channel_dim: which dimension of input image is the channel, default to 0.
"""
def __init__(self, channel_dim: int = 0) -> None:
super().__init__(channel_dim)
class CastToType(Transform):
"""
Cast the Numpy data to specified numpy data type, or cast the PyTorch Tensor to
specified PyTorch data type.
"""
backend = [TransformBackends.TORCH, TransformBackends.NUMPY]
def __init__(self, dtype=np.float32) -> None:
"""
Args:
dtype: convert image to this data type, default is `np.float32`.
"""
self.dtype = dtype
def __call__(self, img: NdarrayOrTensor, dtype: DtypeLike | torch.dtype = None) -> NdarrayOrTensor:
"""
Apply the transform to `img`, assuming `img` is a numpy array or PyTorch Tensor.
Args:
dtype: convert image to this data type, default is `self.dtype`.
Raises:
TypeError: When ``img`` type is not in ``Union[numpy.ndarray, torch.Tensor]``.
"""
return convert_data_type(img, output_type=type(img), dtype=dtype or self.dtype)[0] # type: ignore
class ToTensor(Transform):
"""
Converts the input image to a tensor without applying any other transformations.
Input data can be PyTorch Tensor, numpy array, list, dictionary, int, float, bool, str, etc.
Will convert Tensor, Numpy array, float, int, bool to Tensor, strings and objects keep the original.
For dictionary, list or tuple, convert every item to a Tensor if applicable and `wrap_sequence=False`.
Args:
dtype: target data type to when converting to Tensor.
device: target device to put the converted Tensor data.
wrap_sequence: if `False`, then lists will recursively call this function, default to `True`.
E.g., if `False`, `[1, 2]` -> `[tensor(1), tensor(2)]`, if `True`, then `[1, 2]` -> `tensor([1, 2])`.
track_meta: whether to convert to `MetaTensor` or regular tensor, default to `None`,
use the return value of ``get_track_meta``.
"""
backend = [TransformBackends.TORCH]
def __init__(
self,
dtype: torch.dtype | None = None,
device: torch.device | None = None,
wrap_sequence: bool = True,
track_meta: bool | None = None,
) -> None:
super().__init__()
self.dtype = dtype
self.device = device
self.wrap_sequence = wrap_sequence
self.track_meta = get_track_meta() if track_meta is None else bool(track_meta)
def __call__(self, img: NdarrayOrTensor):
"""
Apply the transform to `img` and make it contiguous.
"""
if isinstance(img, MetaTensor):
img.applied_operations = [] # drops tracking info
return convert_to_tensor(
img, dtype=self.dtype, device=self.device, wrap_sequence=self.wrap_sequence, track_meta=self.track_meta
)
class EnsureType(Transform):
"""
Ensure the input data to be a PyTorch Tensor or numpy array, support: `numpy array`, `PyTorch Tensor`,
`float`, `int`, `bool`, `string` and `object` keep the original.
If passing a dictionary, list or tuple, still return dictionary, list or tuple will recursively convert
every item to the expected data type if `wrap_sequence=False`.
Args:
data_type: target data type to convert, should be "tensor" or "numpy".
dtype: target data content type to convert, for example: np.float32, torch.float, etc.
device: for Tensor data type, specify the target device.
wrap_sequence: if `False`, then lists will recursively call this function, default to `True`.
E.g., if `False`, `[1, 2]` -> `[tensor(1), tensor(2)]`, if `True`, then `[1, 2]` -> `tensor([1, 2])`.
track_meta: if `True` convert to ``MetaTensor``, otherwise to Pytorch ``Tensor``,
if ``None`` behave according to return value of py:func:`monai.data.meta_obj.get_track_meta`.
"""
backend = [TransformBackends.TORCH, TransformBackends.NUMPY]
def __init__(
self,
data_type: str = "tensor",
dtype: DtypeLike | torch.dtype | None = None,
device: torch.device | None = None,
wrap_sequence: bool = True,
track_meta: bool | None = None,
) -> None:
self.data_type = look_up_option(data_type.lower(), {"tensor", "numpy"})
self.dtype = dtype
self.device = device
self.wrap_sequence = wrap_sequence
self.track_meta = get_track_meta() if track_meta is None else bool(track_meta)
def __call__(self, data: NdarrayOrTensor):
"""
Args:
data: input data can be PyTorch Tensor, numpy array, list, dictionary, int, float, bool, str, etc.
will ensure Tensor, Numpy array, float, int, bool as Tensors or numpy arrays, strings and
objects keep the original. for dictionary, list or tuple, ensure every item as expected type
if applicable and `wrap_sequence=False`.
"""
if self.data_type == "tensor":
output_type = MetaTensor if self.track_meta else torch.Tensor
else:
output_type = np.ndarray # type: ignore
out: NdarrayOrTensor
out, *_ = convert_data_type(
data=data,
output_type=output_type, # type: ignore
dtype=self.dtype,
device=self.device,
wrap_sequence=self.wrap_sequence,
)
return out
class ToNumpy(Transform):
"""
Converts the input data to numpy array, can support list or tuple of numbers and PyTorch Tensor.
Args:
dtype: target data type when converting to numpy array.
wrap_sequence: if `False`, then lists will recursively call this function, default to `True`.
E.g., if `False`, `[1, 2]` -> `[array(1), array(2)]`, if `True`, then `[1, 2]` -> `array([1, 2])`.
"""
backend = [TransformBackends.NUMPY]
def __init__(self, dtype: DtypeLike = None, wrap_sequence: bool = True) -> None:
super().__init__()
self.dtype = dtype
self.wrap_sequence = wrap_sequence
def __call__(self, img: NdarrayOrTensor):
"""
Apply the transform to `img` and make it contiguous.
"""
return convert_to_numpy(img, dtype=self.dtype, wrap_sequence=self.wrap_sequence)
class ToCupy(Transform):
"""
Converts the input data to CuPy array, can support list or tuple of numbers, NumPy and PyTorch Tensor.
Args:
dtype: data type specifier. It is inferred from the input by default.
if not None, must be an argument of `numpy.dtype`, for more details:
https://docs.cupy.dev/en/stable/reference/generated/cupy.array.html.
wrap_sequence: if `False`, then lists will recursively call this function, default to `True`.
E.g., if `False`, `[1, 2]` -> `[array(1), array(2)]`, if `True`, then `[1, 2]` -> `array([1, 2])`.
"""
backend = [TransformBackends.CUPY]
def __init__(self, dtype: np.dtype | None = None, wrap_sequence: bool = True) -> None:
super().__init__()
self.dtype = dtype
self.wrap_sequence = wrap_sequence
def __call__(self, data: NdarrayOrTensor):
"""
Create a CuPy array from `data` and make it contiguous
"""
return convert_to_cupy(data, dtype=self.dtype, wrap_sequence=self.wrap_sequence)
class ToPIL(Transform):
"""
Converts the input image (in the form of NumPy array or PyTorch Tensor) to PIL image
"""
backend = [TransformBackends.NUMPY]
def __call__(self, img):
"""
Apply the transform to `img`.
"""
if isinstance(img, PILImageImage):
return img
if isinstance(img, torch.Tensor):
img = img.detach().cpu().numpy()
return pil_image_fromarray(img)
class Transpose(Transform):
"""
Transposes the input image based on the given `indices` dimension ordering.
"""
backend = [TransformBackends.TORCH]
def __init__(self, indices: Sequence[int] | None) -> None:
self.indices = None if indices is None else tuple(indices)
def __call__(self, img: NdarrayOrTensor) -> NdarrayOrTensor:
"""
Apply the transform to `img`.
"""
img = convert_to_tensor(img, track_meta=get_track_meta())
return img.permute(self.indices or tuple(range(img.ndim)[::-1])) # type: ignore
class SqueezeDim(Transform):
"""
Squeeze a unitary dimension.
"""
backend = [TransformBackends.TORCH, TransformBackends.NUMPY]
def __init__(self, dim: int | None = 0, update_meta=True) -> None:
"""
Args:
dim: dimension to be squeezed. Default = 0
"None" works when the input is numpy array.
update_meta: whether to update the meta info if the input is a metatensor. Default is ``True``.
Raises:
TypeError: When ``dim`` is not an ``Optional[int]``.
"""
if dim is not None and not isinstance(dim, int):
raise TypeError(f"dim must be None or a int but is {type(dim).__name__}.")
self.dim = dim
self.update_meta = update_meta
def __call__(self, img: NdarrayOrTensor) -> NdarrayOrTensor:
"""
Args:
img: numpy arrays with required dimension `dim` removed
"""
img = convert_to_tensor(img, track_meta=get_track_meta())
if self.dim is None:
if self.update_meta:
warnings.warn("update_meta=True is ignored when dim=None.")
return img.squeeze()
dim = (self.dim + len(img.shape)) if self.dim < 0 else self.dim
# for pytorch/numpy unification
if img.shape[dim] != 1:
raise ValueError(f"Can only squeeze singleton dimension, got shape {img.shape[dim]} of {img.shape}.")
img = img.squeeze(dim)
if self.update_meta and isinstance(img, MetaTensor) and dim > 0 and len(img.affine.shape) == 2:
h, w = img.affine.shape
affine, device = img.affine, img.affine.device if isinstance(img.affine, torch.Tensor) else None
if h > dim:
affine = affine[torch.arange(0, h, device=device) != dim - 1]
if w > dim:
affine = affine[:, torch.arange(0, w, device=device) != dim - 1]
if (affine.shape[0] == affine.shape[1]) and not np.linalg.det(convert_to_numpy(affine, wrap_sequence=True)):
warnings.warn(f"After SqueezeDim, img.affine is ill-posed: \n{img.affine}.")
img.affine = affine
return img
class DataStats(Transform):
"""
Utility transform to show the statistics of data for debug or analysis.
It can be inserted into any place of a transform chain and check results of previous transforms.
It support both `numpy.ndarray` and `torch.tensor` as input data,
so it can be used in pre-processing and post-processing.
It gets logger from `logging.getLogger(name)`, we can setup a logger outside first with the same `name`.
If the log level of `logging.RootLogger` is higher than `INFO`, will add a separate `StreamHandler`
log handler with `INFO` level and record to `stdout`.
"""
backend = [TransformBackends.TORCH, TransformBackends.NUMPY]
def __init__(
self,
prefix: str = "Data",
data_type: bool = True,
data_shape: bool = True,
value_range: bool = True,
data_value: bool = False,
additional_info: Callable | None = None,
name: str = "DataStats",
) -> None:
"""
Args:
prefix: will be printed in format: "{prefix} statistics".
data_type: whether to show the type of input data.
data_shape: whether to show the shape of input data.
value_range: whether to show the value range of input data.
data_value: whether to show the raw value of input data.
a typical example is to print some properties of Nifti image: affine, pixdim, etc.
additional_info: user can define callable function to extract additional info from input data.
name: identifier of `logging.logger` to use, defaulting to "DataStats".
Raises:
TypeError: When ``additional_info`` is not an ``Optional[Callable]``.
"""
if not isinstance(prefix, str):
raise ValueError(f"prefix must be a string, got {type(prefix)}.")
self.prefix = prefix
self.data_type = data_type
self.data_shape = data_shape
self.value_range = value_range
self.data_value = data_value
if additional_info is not None and not callable(additional_info):
raise TypeError(f"additional_info must be None or callable but is {type(additional_info).__name__}.")
self.additional_info = additional_info
self._logger_name = name
_logger = logging.getLogger(self._logger_name)
_logger.setLevel(logging.INFO)
if logging.root.getEffectiveLevel() > logging.INFO:
# Avoid duplicate stream handlers to be added when multiple DataStats are used in a chain.
has_console_handler = any(
hasattr(h, "is_data_stats_handler") and h.is_data_stats_handler for h in _logger.handlers
)
if not has_console_handler:
# if the root log level is higher than INFO, set a separate stream handler to record
console = logging.StreamHandler(sys.stdout)
console.setLevel(logging.INFO)
console.is_data_stats_handler = True # type:ignore[attr-defined]
_logger.addHandler(console)
def __call__(
self,
img: NdarrayOrTensor,
prefix: str | None = None,
data_type: bool | None = None,
data_shape: bool | None = None,
value_range: bool | None = None,
data_value: bool | None = None,
additional_info: Callable | None = None,
) -> NdarrayOrTensor:
"""
Apply the transform to `img`, optionally take arguments similar to the class constructor.
"""
lines = [f"{prefix or self.prefix} statistics:"]
if self.data_type if data_type is None else data_type:
lines.append(f"Type: {type(img)} {img.dtype if hasattr(img, 'dtype') else None}")
if self.data_shape if data_shape is None else data_shape:
lines.append(f"Shape: {img.shape}")
if self.value_range if value_range is None else value_range:
if isinstance(img, np.ndarray):
lines.append(f"Value range: ({np.min(img)}, {np.max(img)})")
elif isinstance(img, torch.Tensor):
lines.append(f"Value range: ({torch.min(img)}, {torch.max(img)})")
else:
lines.append(f"Value range: (not a PyTorch or Numpy array, type: {type(img)})")
if self.data_value if data_value is None else data_value:
lines.append(f"Value: {img}")
additional_info = self.additional_info if additional_info is None else additional_info
if additional_info is not None:
lines.append(f"Additional info: {additional_info(img)}")
separator = "\n"
output = f"{separator.join(lines)}"
logging.getLogger(self._logger_name).info(output)
return img
class SimulateDelay(Transform):
"""
This is a pass through transform to be used for testing purposes. It allows
adding fake behaviors that are useful for testing purposes to simulate
how large datasets behave without needing to test on large data sets.
For example, simulating slow NFS data transfers, or slow network transfers
in testing by adding explicit timing delays. Testing of small test data
can lead to incomplete understanding of real world issues, and may lead
to sub-optimal design choices.
"""
backend = [TransformBackends.TORCH, TransformBackends.NUMPY]
def __init__(self, delay_time: float = 0.0) -> None:
"""
Args:
delay_time: The minimum amount of time, in fractions of seconds,
to accomplish this delay task.
"""
super().__init__()
self.delay_time: float = delay_time
def __call__(self, img: NdarrayOrTensor, delay_time: float | None = None) -> NdarrayOrTensor:
"""
Args:
img: data remain unchanged throughout this transform.
delay_time: The minimum amount of time, in fractions of seconds,
to accomplish this delay task.
"""
time.sleep(self.delay_time if delay_time is None else delay_time)
return img
class Lambda(InvertibleTransform):
"""
Apply a user-defined lambda as a transform.
For example:
.. code-block:: python
:emphasize-lines: 2
image = np.ones((10, 2, 2))
lambd = Lambda(func=lambda x: x[:4, :, :])
print(lambd(image).shape)
(4, 2, 2)
Args:
func: Lambda/function to be applied.
inv_func: Lambda/function of inverse operation, default to `lambda x: x`.
Raises:
TypeError: When ``func`` is not an ``Optional[Callable]``.
"""
backend = [TransformBackends.TORCH, TransformBackends.NUMPY]
def __init__(self, func: Callable | None = None, inv_func: Callable = no_collation) -> None:
if func is not None and not callable(func):
raise TypeError(f"func must be None or callable but is {type(func).__name__}.")
self.func = func
self.inv_func = inv_func
def __call__(self, img: NdarrayOrTensor, func: Callable | None = None):
"""
Apply `self.func` to `img`.
Args:
func: Lambda/function to be applied. Defaults to `self.func`.
Raises:
TypeError: When ``func`` is not an ``Optional[Callable]``.
"""
fn = func if func is not None else self.func
if not callable(fn):
raise TypeError(f"func must be None or callable but is {type(fn).__name__}.")
out = fn(img)
# convert to MetaTensor if necessary
if isinstance(out, (np.ndarray, torch.Tensor)) and not isinstance(out, MetaTensor) and get_track_meta():
out = MetaTensor(out)
if isinstance(out, MetaTensor):
self.push_transform(out)
return out
def inverse(self, data: torch.Tensor):
if isinstance(data, MetaTensor):
self.pop_transform(data)
return self.inv_func(data)
class RandLambda(Lambda, RandomizableTransform):
"""
Randomizable version :py:class:`monai.transforms.Lambda`, the input `func` may contain random logic,
or randomly execute the function based on `prob`.
Args:
func: Lambda/function to be applied.
prob: probability of executing the random function, default to 1.0, with 100% probability to execute.
inv_func: Lambda/function of inverse operation, default to `lambda x: x`.
For more details, please check :py:class:`monai.transforms.Lambda`.
"""
backend = Lambda.backend
def __init__(self, func: Callable | None = None, prob: float = 1.0, inv_func: Callable = no_collation) -> None:
Lambda.__init__(self=self, func=func, inv_func=inv_func)
RandomizableTransform.__init__(self=self, prob=prob)
def __call__(self, img: NdarrayOrTensor, func: Callable | None = None):
self.randomize(img)
out = deepcopy(super().__call__(img, func) if self._do_transform else img)
# convert to MetaTensor if necessary
if not isinstance(out, MetaTensor) and get_track_meta():
out = MetaTensor(out)
if isinstance(out, MetaTensor):
lambda_info = self.pop_transform(out) if self._do_transform else {}
self.push_transform(out, extra_info=lambda_info)
return out
def inverse(self, data: torch.Tensor):
do_transform = self.get_most_recent_transform(data).pop(TraceKeys.DO_TRANSFORM)
if do_transform:
data = super().inverse(data)
else:
self.pop_transform(data)
return data
class LabelToMask(Transform):
"""
Convert labels to mask for other tasks. A typical usage is to convert segmentation labels
to mask data to pre-process images and then feed the images into classification network.
It can support single channel labels or One-Hot labels with specified `select_labels`.
For example, users can select `label value = [2, 3]` to construct mask data, or select the
second and the third channels of labels to construct mask data.
The output mask data can be a multiple channels binary data or a single channel binary
data that merges all the channels.
Args:
select_labels: labels to generate mask from. for 1 channel label, the `select_labels`
is the expected label values, like: [1, 2, 3]. for One-Hot format label, the
`select_labels` is the expected channel indices.
merge_channels: whether to use `np.any()` to merge the result on channel dim. if yes,
will return a single channel mask with binary data.
"""
backend = [TransformBackends.TORCH, TransformBackends.NUMPY]
def __init__( # pytype: disable=annotation-type-mismatch
self, select_labels: Sequence[int] | int, merge_channels: bool = False
) -> None: # pytype: disable=annotation-type-mismatch
self.select_labels = ensure_tuple(select_labels)
self.merge_channels = merge_channels
def __call__(
self, img: NdarrayOrTensor, select_labels: Sequence[int] | int | None = None, merge_channels: bool = False
) -> NdarrayOrTensor:
"""
Args:
select_labels: labels to generate mask from. for 1 channel label, the `select_labels`
is the expected label values, like: [1, 2, 3]. for One-Hot format label, the
`select_labels` is the expected channel indices.
merge_channels: whether to use `np.any()` to merge the result on channel dim. if yes,
will return a single channel mask with binary data.
"""
img = convert_to_tensor(img, track_meta=get_track_meta())
if select_labels is None:
select_labels = self.select_labels
else:
select_labels = ensure_tuple(select_labels)
if img.shape[0] > 1:
data = img[[*select_labels]]
else:
where: Callable = np.where if isinstance(img, np.ndarray) else torch.where # type: ignore
if isinstance(img, np.ndarray) or is_module_ver_at_least(torch, (1, 8, 0)):
data = where(in1d(img, select_labels), True, False).reshape(img.shape)
# pre pytorch 1.8.0, need to use 1/0 instead of True/False
else:
data = where(
in1d(img, select_labels), torch.tensor(1, device=img.device), torch.tensor(0, device=img.device)
).reshape(img.shape)
if merge_channels or self.merge_channels:
if isinstance(img, np.ndarray) or is_module_ver_at_least(torch, (1, 8, 0)):
return data.any(0)[None]
# pre pytorch 1.8.0 compatibility
return data.to(torch.uint8).any(0)[None].to(bool) # type: ignore
return data
class FgBgToIndices(Transform, MultiSampleTrait):
"""
Compute foreground and background of the input label data, return the indices.
If no output_shape specified, output data will be 1 dim indices after flattening.
This transform can help pre-compute foreground and background regions for other transforms.
A typical usage is to randomly select foreground and background to crop.
The main logic is based on :py:class:`monai.transforms.utils.map_binary_to_indices`.
Args:
image_threshold: if enabled `image` at runtime, use ``image > image_threshold`` to
determine the valid image content area and select background only in this area.
output_shape: expected shape of output indices. if not None, unravel indices to specified shape.
"""
backend = [TransformBackends.NUMPY, TransformBackends.TORCH]
def __init__(self, image_threshold: float = 0.0, output_shape: Sequence[int] | None = None) -> None:
self.image_threshold = image_threshold
self.output_shape = output_shape