careamics 0.1.0rc2__py3-none-any.whl → 0.1.0rc3__py3-none-any.whl

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.

Potentially problematic release.


This version of careamics might be problematic. Click here for more details.

Files changed (133) hide show
  1. careamics/__init__.py +14 -4
  2. careamics/callbacks/__init__.py +6 -0
  3. careamics/callbacks/hyperparameters_callback.py +42 -0
  4. careamics/callbacks/progress_bar_callback.py +57 -0
  5. careamics/careamist.py +761 -0
  6. careamics/config/__init__.py +27 -3
  7. careamics/config/algorithm_model.py +167 -0
  8. careamics/config/architectures/__init__.py +17 -0
  9. careamics/config/architectures/architecture_model.py +29 -0
  10. careamics/config/architectures/custom_model.py +150 -0
  11. careamics/config/architectures/register_model.py +101 -0
  12. careamics/config/architectures/unet_model.py +96 -0
  13. careamics/config/architectures/vae_model.py +39 -0
  14. careamics/config/callback_model.py +92 -0
  15. careamics/config/configuration_factory.py +460 -0
  16. careamics/config/configuration_model.py +596 -0
  17. careamics/config/data_model.py +555 -0
  18. careamics/config/inference_model.py +283 -0
  19. careamics/config/noise_models.py +162 -0
  20. careamics/config/optimizer_models.py +181 -0
  21. careamics/config/references/__init__.py +45 -0
  22. careamics/config/references/algorithm_descriptions.py +131 -0
  23. careamics/config/references/references.py +38 -0
  24. careamics/config/support/__init__.py +33 -0
  25. careamics/config/support/supported_activations.py +24 -0
  26. careamics/config/support/supported_algorithms.py +18 -0
  27. careamics/config/support/supported_architectures.py +18 -0
  28. careamics/config/support/supported_data.py +82 -0
  29. careamics/{dataset/extraction_strategy.py → config/support/supported_extraction_strategies.py} +5 -2
  30. careamics/config/support/supported_loggers.py +8 -0
  31. careamics/config/support/supported_losses.py +25 -0
  32. careamics/config/support/supported_optimizers.py +55 -0
  33. careamics/config/support/supported_pixel_manipulations.py +15 -0
  34. careamics/config/support/supported_struct_axis.py +19 -0
  35. careamics/config/support/supported_transforms.py +23 -0
  36. careamics/config/tile_information.py +104 -0
  37. careamics/config/training_model.py +65 -0
  38. careamics/config/transformations/__init__.py +14 -0
  39. careamics/config/transformations/n2v_manipulate_model.py +63 -0
  40. careamics/config/transformations/nd_flip_model.py +32 -0
  41. careamics/config/transformations/normalize_model.py +31 -0
  42. careamics/config/transformations/transform_model.py +44 -0
  43. careamics/config/transformations/xy_random_rotate90_model.py +29 -0
  44. careamics/config/validators/__init__.py +5 -0
  45. careamics/config/validators/validator_utils.py +100 -0
  46. careamics/conftest.py +26 -0
  47. careamics/dataset/__init__.py +5 -0
  48. careamics/dataset/dataset_utils/__init__.py +19 -0
  49. careamics/dataset/dataset_utils/dataset_utils.py +100 -0
  50. careamics/dataset/dataset_utils/file_utils.py +140 -0
  51. careamics/dataset/dataset_utils/read_tiff.py +61 -0
  52. careamics/dataset/dataset_utils/read_utils.py +25 -0
  53. careamics/dataset/dataset_utils/read_zarr.py +56 -0
  54. careamics/dataset/in_memory_dataset.py +323 -134
  55. careamics/dataset/iterable_dataset.py +416 -0
  56. careamics/dataset/patching/__init__.py +8 -0
  57. careamics/dataset/patching/patch_transform.py +44 -0
  58. careamics/dataset/patching/patching.py +212 -0
  59. careamics/dataset/patching/random_patching.py +190 -0
  60. careamics/dataset/patching/sequential_patching.py +206 -0
  61. careamics/dataset/patching/tiled_patching.py +158 -0
  62. careamics/dataset/patching/validate_patch_dimension.py +60 -0
  63. careamics/dataset/zarr_dataset.py +149 -0
  64. careamics/lightning_datamodule.py +665 -0
  65. careamics/lightning_module.py +292 -0
  66. careamics/lightning_prediction_datamodule.py +390 -0
  67. careamics/lightning_prediction_loop.py +116 -0
  68. careamics/losses/__init__.py +4 -1
  69. careamics/losses/loss_factory.py +24 -14
  70. careamics/losses/losses.py +65 -5
  71. careamics/losses/noise_model_factory.py +40 -0
  72. careamics/losses/noise_models.py +524 -0
  73. careamics/model_io/__init__.py +8 -0
  74. careamics/model_io/bioimage/__init__.py +11 -0
  75. careamics/model_io/bioimage/_readme_factory.py +120 -0
  76. careamics/model_io/bioimage/bioimage_utils.py +48 -0
  77. careamics/model_io/bioimage/model_description.py +318 -0
  78. careamics/model_io/bmz_io.py +231 -0
  79. careamics/model_io/model_io_utils.py +80 -0
  80. careamics/models/__init__.py +4 -1
  81. careamics/models/activation.py +35 -0
  82. careamics/models/layers.py +244 -0
  83. careamics/models/model_factory.py +21 -221
  84. careamics/models/unet.py +46 -20
  85. careamics/prediction/__init__.py +1 -3
  86. careamics/prediction/stitch_prediction.py +73 -0
  87. careamics/transforms/__init__.py +41 -0
  88. careamics/transforms/n2v_manipulate.py +113 -0
  89. careamics/transforms/nd_flip.py +93 -0
  90. careamics/transforms/normalize.py +109 -0
  91. careamics/transforms/pixel_manipulation.py +383 -0
  92. careamics/transforms/struct_mask_parameters.py +18 -0
  93. careamics/transforms/tta.py +74 -0
  94. careamics/transforms/xy_random_rotate90.py +95 -0
  95. careamics/utils/__init__.py +10 -12
  96. careamics/utils/base_enum.py +32 -0
  97. careamics/utils/context.py +22 -2
  98. careamics/utils/metrics.py +0 -46
  99. careamics/utils/path_utils.py +24 -0
  100. careamics/utils/ram.py +13 -0
  101. careamics/utils/receptive_field.py +102 -0
  102. careamics/utils/running_stats.py +43 -0
  103. careamics/utils/torch_utils.py +112 -75
  104. careamics-0.1.0rc3.dist-info/METADATA +122 -0
  105. careamics-0.1.0rc3.dist-info/RECORD +109 -0
  106. {careamics-0.1.0rc2.dist-info → careamics-0.1.0rc3.dist-info}/WHEEL +1 -1
  107. careamics/bioimage/__init__.py +0 -15
  108. careamics/bioimage/docs/Noise2Void.md +0 -5
  109. careamics/bioimage/docs/__init__.py +0 -1
  110. careamics/bioimage/io.py +0 -182
  111. careamics/bioimage/rdf.py +0 -105
  112. careamics/config/algorithm.py +0 -231
  113. careamics/config/config.py +0 -297
  114. careamics/config/config_filter.py +0 -44
  115. careamics/config/data.py +0 -194
  116. careamics/config/torch_optim.py +0 -118
  117. careamics/config/training.py +0 -534
  118. careamics/dataset/dataset_utils.py +0 -111
  119. careamics/dataset/patching.py +0 -492
  120. careamics/dataset/prepare_dataset.py +0 -175
  121. careamics/dataset/tiff_dataset.py +0 -212
  122. careamics/engine.py +0 -1014
  123. careamics/manipulation/__init__.py +0 -4
  124. careamics/manipulation/pixel_manipulation.py +0 -158
  125. careamics/prediction/prediction_utils.py +0 -106
  126. careamics/utils/ascii_logo.txt +0 -9
  127. careamics/utils/augment.py +0 -65
  128. careamics/utils/normalization.py +0 -55
  129. careamics/utils/validators.py +0 -170
  130. careamics/utils/wandb.py +0 -121
  131. careamics-0.1.0rc2.dist-info/METADATA +0 -81
  132. careamics-0.1.0rc2.dist-info/RECORD +0 -47
  133. {careamics-0.1.0rc2.dist-info → careamics-0.1.0rc3.dist-info}/licenses/LICENSE +0 -0
@@ -0,0 +1,131 @@
1
+ """Descriptions of the algorithms used in CAREmics."""
2
+ from pydantic import BaseModel
3
+
4
+ CUSTOM = "Custom"
5
+ N2V = "Noise2Void"
6
+ N2V2 = "N2V2"
7
+ STRUCT_N2V = "StructN2V"
8
+ STRUCT_N2V2 = "StructN2V2"
9
+ N2N = "Noise2Noise"
10
+ CARE = "CARE"
11
+
12
+
13
+ N2V_DESCRIPTION = (
14
+ "Noise2Void is a UNet-based self-supervised algorithm that "
15
+ "uses blind-spot training to denoise images. In short, in every "
16
+ "patches during training, random pixels are selected and their "
17
+ "value replaced by a neighboring pixel value. The network is then "
18
+ "trained to predict the original pixel value. The algorithm "
19
+ "relies on the continuity of the signal (neighboring pixels have "
20
+ "similar values) and the pixel-wise independence of the noise "
21
+ "(the noise in a pixel is not correlated with the noise in "
22
+ "neighboring pixels)."
23
+ )
24
+
25
+
26
+ class AlgorithmDescription(BaseModel):
27
+ """Description of an algorithm.
28
+
29
+ Attributes
30
+ ----------
31
+ description : str
32
+ Description of the algorithm.
33
+ """
34
+
35
+ description: str
36
+
37
+
38
+ class N2VDescription(AlgorithmDescription):
39
+ """Description of Noise2Void.
40
+
41
+ Attributes
42
+ ----------
43
+ description : str
44
+ Description of Noise2Void.
45
+ """
46
+
47
+ description: str = N2V_DESCRIPTION
48
+
49
+
50
+ class N2V2Description(AlgorithmDescription):
51
+ """Description of N2V2.
52
+
53
+ Attributes
54
+ ----------
55
+ description : str
56
+ Description of N2V2.
57
+ """
58
+
59
+ description: str = (
60
+ "N2V2 is a variant of Noise2Void. "
61
+ + N2V_DESCRIPTION
62
+ + "\nN2V2 introduces blur-pool layers and removed skip "
63
+ "connections in the UNet architecture to remove checkboard "
64
+ "artefacts, a common artefacts ocurring in Noise2Void."
65
+ )
66
+
67
+
68
+ class StructN2VDescription(AlgorithmDescription):
69
+ """Description of StructN2V.
70
+
71
+ Attributes
72
+ ----------
73
+ description : str
74
+ Description of StructN2V.
75
+ """
76
+
77
+ description: str = (
78
+ "StructN2V is a variant of Noise2Void. "
79
+ + N2V_DESCRIPTION
80
+ + "\nStructN2V uses a linear mask (horizontal or vertical) to replace "
81
+ "the pixel values of neighbors of the masked pixels by a random "
82
+ "value. Such masking allows removing 1D structured noise from the "
83
+ "the images, the main failure case of the original N2V."
84
+ )
85
+
86
+
87
+ class StructN2V2Description(AlgorithmDescription):
88
+ """Description of StructN2V2.
89
+
90
+ Attributes
91
+ ----------
92
+ description : str
93
+ Description of StructN2V2.
94
+ """
95
+
96
+ description: str = (
97
+ "StructN2V2 is a a variant of Noise2Void that uses both "
98
+ "structN2V and N2V2. "
99
+ + N2V_DESCRIPTION
100
+ + "\nStructN2V2 uses a linear mask (horizontal or vertical) to replace "
101
+ "the pixel values of neighbors of the masked pixels by a random "
102
+ "value. Such masking allows removing 1D structured noise from the "
103
+ "the images, the main failure case of the original N2V."
104
+ "\nN2V2 introduces blur-pool layers and removed skip connections in "
105
+ "the UNet architecture to remove checkboard artefacts, a common "
106
+ "artefacts ocurring in Noise2Void."
107
+ )
108
+
109
+
110
+ class N2NDescription(AlgorithmDescription):
111
+ """Description of Noise2Noise.
112
+
113
+ Attributes
114
+ ----------
115
+ description : str
116
+ Description of Noise2Noise.
117
+ """
118
+
119
+ description: str = "Noise2Noise" # TODO
120
+
121
+
122
+ class CAREDescription(AlgorithmDescription):
123
+ """Description of CARE.
124
+
125
+ Attributes
126
+ ----------
127
+ description : str
128
+ Description of CARE.
129
+ """
130
+
131
+ description: str = "CARE" # TODO
@@ -0,0 +1,38 @@
1
+ """References for the CAREamics algorithms."""
2
+ from bioimageio.spec.generic.v0_3 import CiteEntry
3
+
4
+ N2VRef = CiteEntry(
5
+ text='Krull, A., Buchholz, T.O. and Jug, F., 2019. "Noise2Void - Learning '
6
+ 'denoising from single noisy images". In Proceedings of the IEEE/CVF '
7
+ "conference on computer vision and pattern recognition (pp. 2129-2137).",
8
+ doi="10.1109/cvpr.2019.00223",
9
+ )
10
+
11
+ N2V2Ref = CiteEntry(
12
+ text="Höck, E., Buchholz, T.O., Brachmann, A., Jug, F. and Freytag, A., "
13
+ '2022. "N2V2 - Fixing Noise2Void checkerboard artifacts with modified '
14
+ 'sampling strategies and a tweaked network architecture". In European '
15
+ "Conference on Computer Vision (pp. 503-518).",
16
+ doi="10.1007/978-3-031-25069-9_33",
17
+ )
18
+
19
+ StructN2VRef = CiteEntry(
20
+ text="Broaddus, C., Krull, A., Weigert, M., Schmidt, U. and Myers, G., 2020."
21
+ '"Removing structured noise with self-supervised blind-spot '
22
+ 'networks". In 2020 IEEE 17th International Symposium on Biomedical '
23
+ "Imaging (ISBI) (pp. 159-163).",
24
+ doi="10.1109/isbi45749.2020.9098336",
25
+ )
26
+
27
+ N2NRef = CiteEntry(
28
+ text="Lehtinen, J., Munkberg, J., Hasselgren, J., Laine, S., Karras, T., "
29
+ 'Aittala, M. and Aila, T., 2018. "Noise2Noise: Learning image restoration '
30
+ 'without clean data". arXiv preprint arXiv:1803.04189.',
31
+ doi="10.48550/arXiv.1803.04189",
32
+ )
33
+
34
+ CARERef = CiteEntry(
35
+ text='Weigert, Martin, et al. "Content-aware image restoration: pushing the '
36
+ 'limits of fluorescence microscopy." Nature methods 15.12 (2018): 1090-1097.',
37
+ doi="10.1038/s41592-018-0216-7",
38
+ )
@@ -0,0 +1,33 @@
1
+ """Supported configuration options.
2
+
3
+ Used throughout the code to ensure consistency. These should be kept in sync with the
4
+ corresponding configuration options in the Pydantic models.
5
+ """
6
+
7
+ __all__ = [
8
+ "SupportedArchitecture",
9
+ "SupportedActivation",
10
+ "SupportedOptimizer",
11
+ "SupportedScheduler",
12
+ "SupportedLoss",
13
+ "SupportedAlgorithm",
14
+ "SupportedPixelManipulation",
15
+ "SupportedTransform",
16
+ "SupportedData",
17
+ "SupportedExtractionStrategy",
18
+ "SupportedStructAxis",
19
+ "SupportedLogger",
20
+ ]
21
+
22
+
23
+ from .supported_activations import SupportedActivation
24
+ from .supported_algorithms import SupportedAlgorithm
25
+ from .supported_architectures import SupportedArchitecture
26
+ from .supported_data import SupportedData
27
+ from .supported_extraction_strategies import SupportedExtractionStrategy
28
+ from .supported_loggers import SupportedLogger
29
+ from .supported_losses import SupportedLoss
30
+ from .supported_optimizers import SupportedOptimizer, SupportedScheduler
31
+ from .supported_pixel_manipulations import SupportedPixelManipulation
32
+ from .supported_struct_axis import SupportedStructAxis
33
+ from .supported_transforms import SupportedTransform
@@ -0,0 +1,24 @@
1
+ from careamics.utils import BaseEnum
2
+
3
+
4
+ class SupportedActivation(str, BaseEnum):
5
+ """Supported activation functions.
6
+
7
+ - None, no activation will be used.
8
+ - Sigmoid
9
+ - Softmax
10
+ - Tanh
11
+ - ReLU
12
+ - LeakyReLU
13
+
14
+ All activations are defined in PyTorch.
15
+
16
+ See: https://pytorch.org/docs/stable/nn.html#loss-functions
17
+ """
18
+
19
+ NONE = "None"
20
+ SIGMOID = "Sigmoid"
21
+ SOFTMAX = "Softmax"
22
+ TANH = "Tanh"
23
+ RELU = "ReLU"
24
+ LEAKYRELU = "LeakyReLU"
@@ -0,0 +1,18 @@
1
+ from __future__ import annotations
2
+
3
+ from careamics.utils import BaseEnum
4
+
5
+
6
+ class SupportedAlgorithm(str, BaseEnum):
7
+ """Algorithms available in CAREamics.
8
+
9
+ # TODO
10
+ """
11
+
12
+ N2V = "n2v"
13
+ CUSTOM = "custom"
14
+ CARE = "care"
15
+ N2N = "n2n"
16
+ # PN2V = "pn2v"
17
+ # HDN = "hdn"
18
+ # SEG = "segmentation"
@@ -0,0 +1,18 @@
1
+ from careamics.utils import BaseEnum
2
+
3
+
4
+ class SupportedArchitecture(str, BaseEnum):
5
+ """Supported architectures.
6
+
7
+ # TODO add details, in particular where to find the API for the models
8
+
9
+ - UNet: classical UNet compatible with N2V2
10
+ - VAE: variational Autoencoder
11
+ - Custom: custom model registered with `@register_model` decorator
12
+ """
13
+
14
+ UNET = "UNet"
15
+ VAE = "VAE"
16
+ CUSTOM = (
17
+ "Custom" # TODO all the others tags are small letters, except the architect
18
+ )
@@ -0,0 +1,82 @@
1
+ from __future__ import annotations
2
+
3
+ from typing import Union
4
+
5
+ from careamics.utils import BaseEnum
6
+
7
+
8
+ class SupportedData(str, BaseEnum):
9
+ """Supported data types.
10
+
11
+ Attributes
12
+ ----------
13
+ ARRAY : str
14
+ Array data.
15
+ TIFF : str
16
+ TIFF image data.
17
+ CUSTOM : str
18
+ Custom data.
19
+ """
20
+
21
+ ARRAY = "array"
22
+ TIFF = "tiff"
23
+ CUSTOM = "custom"
24
+ # ZARR = "zarr"
25
+
26
+ # TODO remove?
27
+ @classmethod
28
+ def _missing_(cls, value: object) -> str:
29
+ """
30
+ Override default behaviour for missing values.
31
+
32
+ This method is called when `value` is not found in the enum values. It converts
33
+ `value` to lowercase, removes "." if it is the first character and tries to
34
+ match it with enum values.
35
+
36
+ Parameters
37
+ ----------
38
+ value : object
39
+ Value to be matched with enum values.
40
+
41
+ Returns
42
+ -------
43
+ str
44
+ Matched enum value.
45
+ """
46
+ if isinstance(value, str):
47
+ lower_value = value.lower()
48
+
49
+ if lower_value.startswith("."):
50
+ lower_value = lower_value[1:]
51
+
52
+ # attempt to match lowercase value with enum values
53
+ for member in cls:
54
+ if member.value == lower_value:
55
+ return member
56
+
57
+ # still missing
58
+ return super()._missing_(value)
59
+
60
+ @classmethod
61
+ def get_extension(cls, data_type: Union[str, SupportedData]) -> str:
62
+ """
63
+ Path.rglob and fnmatch compatible extension.
64
+
65
+ Parameters
66
+ ----------
67
+ data_type : SupportedData
68
+ Data type.
69
+
70
+ Returns
71
+ -------
72
+ str
73
+ Corresponding extension.
74
+ """
75
+ if data_type == cls.ARRAY:
76
+ raise NotImplementedError(f"Data {data_type} are not loaded from file.")
77
+ elif data_type == cls.TIFF:
78
+ return "*.tif*"
79
+ elif data_type == cls.CUSTOM:
80
+ return "*.*"
81
+ else:
82
+ raise ValueError(f"Data type {data_type} is not supported.")
@@ -3,19 +3,22 @@ Extraction strategy module.
3
3
 
4
4
  This module defines the various extraction strategies available in CAREamics.
5
5
  """
6
- from enum import Enum
6
+ from careamics.utils import BaseEnum
7
7
 
8
8
 
9
- class ExtractionStrategy(str, Enum):
9
+ class SupportedExtractionStrategy(str, BaseEnum):
10
10
  """
11
11
  Available extraction strategies.
12
12
 
13
13
  Currently supported:
14
14
  - random: random extraction.
15
+ # TODO
15
16
  - sequential: grid extraction, can miss edge values.
16
17
  - tiled: tiled extraction, covers the whole image.
17
18
  """
18
19
 
19
20
  RANDOM = "random"
21
+ RANDOM_ZARR = "random_zarr"
20
22
  SEQUENTIAL = "sequential"
21
23
  TILED = "tiled"
24
+ NONE = "none"
@@ -0,0 +1,8 @@
1
+ from careamics.utils import BaseEnum
2
+
3
+
4
+ class SupportedLogger(str, BaseEnum):
5
+ """Available loggers."""
6
+
7
+ WANDB = "wandb"
8
+ TENSORBOARD = "tensorboard"
@@ -0,0 +1,25 @@
1
+ from careamics.utils import BaseEnum
2
+
3
+
4
+ # TODO register loss with custom_loss decorator?
5
+ class SupportedLoss(str, BaseEnum):
6
+ """Supported losses.
7
+
8
+ Attributes
9
+ ----------
10
+ MSE : str
11
+ Mean Squared Error loss.
12
+ MAE : str
13
+ Mean Absolute Error loss.
14
+ N2V : str
15
+ Noise2Void loss.
16
+ """
17
+
18
+ MSE = "mse"
19
+ MAE = "mae"
20
+ N2V = "n2v"
21
+ # PN2V = "pn2v"
22
+ # HDN = "hdn"
23
+ # CE = "ce"
24
+ # DICE = "dice"
25
+ # CUSTOM = "custom" # TODO create mechanism for that
@@ -0,0 +1,55 @@
1
+ from careamics.utils import BaseEnum
2
+
3
+
4
+ class SupportedOptimizer(str, BaseEnum):
5
+ """Supported optimizers.
6
+
7
+ Attributes
8
+ ----------
9
+ Adam : str
10
+ Adam optimizer.
11
+ SGD : str
12
+ Stochastic Gradient Descent optimizer.
13
+ """
14
+
15
+ # ASGD = "ASGD"
16
+ # Adadelta = "Adadelta"
17
+ # Adagrad = "Adagrad"
18
+ Adam = "Adam"
19
+ # AdamW = "AdamW"
20
+ # Adamax = "Adamax"
21
+ # LBFGS = "LBFGS"
22
+ # NAdam = "NAdam"
23
+ # RAdam = "RAdam"
24
+ # RMSprop = "RMSprop"
25
+ # Rprop = "Rprop"
26
+ SGD = "SGD"
27
+ # SparseAdam = "SparseAdam"
28
+
29
+
30
+ class SupportedScheduler(str, BaseEnum):
31
+ """Supported schedulers.
32
+
33
+ Attributes
34
+ ----------
35
+ ReduceLROnPlateau : str
36
+ Reduce learning rate on plateau.
37
+ StepLR : str
38
+ Step learning rate.
39
+ """
40
+
41
+ # ChainedScheduler = "ChainedScheduler"
42
+ # ConstantLR = "ConstantLR"
43
+ # CosineAnnealingLR = "CosineAnnealingLR"
44
+ # CosineAnnealingWarmRestarts = "CosineAnnealingWarmRestarts"
45
+ # CyclicLR = "CyclicLR"
46
+ # ExponentialLR = "ExponentialLR"
47
+ # LambdaLR = "LambdaLR"
48
+ # LinearLR = "LinearLR"
49
+ # MultiStepLR = "MultiStepLR"
50
+ # MultiplicativeLR = "MultiplicativeLR"
51
+ # OneCycleLR = "OneCycleLR"
52
+ # PolynomialLR = "PolynomialLR"
53
+ ReduceLROnPlateau = "ReduceLROnPlateau"
54
+ # SequentialLR = "SequentialLR"
55
+ StepLR = "StepLR"
@@ -0,0 +1,15 @@
1
+ from careamics.utils import BaseEnum
2
+
3
+
4
+ class SupportedPixelManipulation(str, BaseEnum):
5
+ """_summary_.
6
+
7
+ - Uniform: Replace masked pixel value by a (uniformly) randomly selected neighbor
8
+ pixel value.
9
+ - Median: Replace masked pixel value by the mean of the neighborhood.
10
+ """
11
+
12
+ # TODO docs
13
+
14
+ UNIFORM = "uniform"
15
+ MEDIAN = "median"
@@ -0,0 +1,19 @@
1
+ from careamics.utils import BaseEnum
2
+
3
+
4
+ class SupportedStructAxis(str, BaseEnum):
5
+ """Supported structN2V mask axes.
6
+
7
+ Attributes
8
+ ----------
9
+ HORIZONTAL : str
10
+ Horizontal axis.
11
+ VERTICAL : str
12
+ Vertical axis.
13
+ NONE : str
14
+ No axis, the mask is not applied.
15
+ """
16
+
17
+ HORIZONTAL = "horizontal"
18
+ VERTICAL = "vertical"
19
+ NONE = "none"
@@ -0,0 +1,23 @@
1
+ from careamics.utils import BaseEnum
2
+
3
+
4
+ class SupportedTransform(str, BaseEnum):
5
+ """Transforms officially supported by CAREamics.
6
+
7
+ - Flip: from Albumentations, randomly flip the input horizontally, vertically or
8
+ both, parameter `p` can be used to set the probability to apply the transform.
9
+ - XYRandomRotate90: #TODO
10
+ - Normalize # TODO add details, in particular about the parameters
11
+ - ManipulateN2V # TODO add details, in particular about the parameters
12
+ - NDFlip
13
+
14
+ Note that while any Albumentations (see https://albumentations.ai/) transform can be
15
+ used in CAREamics, no check are implemented to verify the compatibility of any other
16
+ transforms than the ones officially supported.
17
+ """
18
+
19
+ NDFLIP = "NDFlip"
20
+ XY_RANDOM_ROTATE90 = "XYRandomRotate90"
21
+ NORMALIZE = "Normalize"
22
+ N2V_MANIPULATE = "N2VManipulate"
23
+ # CUSTOM = "Custom"
@@ -0,0 +1,104 @@
1
+ from __future__ import annotations
2
+
3
+ from typing import Optional, Tuple
4
+
5
+ from pydantic import BaseModel, ConfigDict, Field, ValidationInfo, field_validator
6
+
7
+
8
+ class TileInformation(BaseModel):
9
+ """
10
+ Pydantic model containing tile information.
11
+
12
+ This model is used to represent the information required to stitch back a tile into
13
+ a larger image. It is used throughout the prediction pipeline of CAREamics.
14
+ """
15
+
16
+ model_config = ConfigDict(validate_default=True)
17
+
18
+ array_shape: Tuple[int, ...]
19
+ tiled: bool = False
20
+ last_tile: bool = False
21
+ overlap_crop_coords: Optional[Tuple[Tuple[int, ...], ...]] = Field(default=None)
22
+ stitch_coords: Optional[Tuple[Tuple[int, ...], ...]] = Field(default=None)
23
+
24
+ @field_validator("array_shape")
25
+ @classmethod
26
+ def no_singleton_dimensions(cls, v: Tuple[int, ...]):
27
+ """
28
+ Check that the array shape does not have any singleton dimensions.
29
+
30
+ Parameters
31
+ ----------
32
+ v : Tuple[int, ...]
33
+ Array shape to check.
34
+
35
+ Returns
36
+ -------
37
+ Tuple[int, ...]
38
+ The array shape if it does not contain singleton dimensions.
39
+
40
+ Raises
41
+ ------
42
+ ValueError
43
+ If the array shape contains singleton dimensions.
44
+ """
45
+ if any(dim == 1 for dim in v):
46
+ raise ValueError("Array shape must not contain singleton dimensions.")
47
+ return v
48
+
49
+ @field_validator("last_tile")
50
+ @classmethod
51
+ def only_if_tiled(cls, v: bool, values: ValidationInfo):
52
+ """
53
+ Check that the last tile flag is only set if tiling is enabled.
54
+
55
+ Parameters
56
+ ----------
57
+ v : bool
58
+ Last tile flag.
59
+ values : ValidationInfo
60
+ Validation information.
61
+
62
+ Returns
63
+ -------
64
+ bool
65
+ The last tile flag.
66
+ """
67
+ if not values.data["tiled"]:
68
+ return False
69
+ return v
70
+
71
+ @field_validator("overlap_crop_coords", "stitch_coords")
72
+ @classmethod
73
+ def mandatory_if_tiled(
74
+ cls, v: Optional[Tuple[int, ...]], values: ValidationInfo
75
+ ) -> Optional[Tuple[int, ...]]:
76
+ """
77
+ Check that the coordinates are not `None` if tiling is enabled.
78
+
79
+ The method also return `None` if tiling is not enabled.
80
+
81
+ Parameters
82
+ ----------
83
+ v : Optional[Tuple[int, ...]]
84
+ Coordinates to check.
85
+ values : ValidationInfo
86
+ Validation information.
87
+
88
+ Returns
89
+ -------
90
+ Optional[Tuple[int, ...]]
91
+ The coordinates if tiling is enabled, otherwise `None`.
92
+
93
+ Raises
94
+ ------
95
+ ValueError
96
+ If the coordinates are `None` and tiling is enabled.
97
+ """
98
+ if values.data["tiled"]:
99
+ if v is None:
100
+ raise ValueError("Value must be specified if tiling is enabled.")
101
+
102
+ return v
103
+ else:
104
+ return None