careamics 0.0.4.2__py3-none-any.whl → 0.0.6__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 (118) hide show
  1. careamics/__init__.py +17 -2
  2. careamics/careamist.py +239 -28
  3. careamics/cli/conf.py +19 -31
  4. careamics/cli/main.py +112 -12
  5. careamics/cli/utils.py +29 -0
  6. careamics/config/__init__.py +48 -24
  7. careamics/config/algorithms/__init__.py +15 -0
  8. careamics/config/algorithms/care_algorithm_model.py +50 -0
  9. careamics/config/algorithms/n2n_algorithm_model.py +42 -0
  10. careamics/config/algorithms/n2v_algorithm_model.py +35 -0
  11. careamics/config/algorithms/unet_algorithm_model.py +88 -0
  12. careamics/config/{vae_algorithm_model.py → algorithms/vae_algorithm_model.py} +26 -23
  13. careamics/config/architectures/__init__.py +1 -11
  14. careamics/config/architectures/architecture_model.py +3 -3
  15. careamics/config/architectures/lvae_model.py +109 -21
  16. careamics/config/architectures/unet_model.py +1 -0
  17. careamics/config/care_configuration.py +100 -0
  18. careamics/config/configuration.py +354 -0
  19. careamics/config/{configuration_factory.py → configuration_factories.py} +152 -81
  20. careamics/config/configuration_io.py +85 -0
  21. careamics/config/data/__init__.py +10 -0
  22. careamics/config/{data_model.py → data/data_model.py} +58 -198
  23. careamics/config/data/n2v_data_model.py +193 -0
  24. careamics/config/likelihood_model.py +8 -8
  25. careamics/config/loss_model.py +56 -0
  26. careamics/config/n2n_configuration.py +101 -0
  27. careamics/config/n2v_configuration.py +266 -0
  28. careamics/config/nm_model.py +24 -25
  29. careamics/config/support/__init__.py +7 -7
  30. careamics/config/support/supported_algorithms.py +0 -3
  31. careamics/config/support/supported_architectures.py +0 -4
  32. careamics/config/transformations/__init__.py +10 -4
  33. careamics/config/transformations/transform_model.py +3 -3
  34. careamics/config/transformations/transform_unions.py +42 -0
  35. careamics/config/validators/validator_utils.py +3 -3
  36. careamics/dataset/__init__.py +2 -2
  37. careamics/dataset/dataset_utils/__init__.py +3 -3
  38. careamics/dataset/dataset_utils/dataset_utils.py +4 -6
  39. careamics/dataset/dataset_utils/file_utils.py +9 -9
  40. careamics/dataset/dataset_utils/iterate_over_files.py +4 -3
  41. careamics/dataset/dataset_utils/running_stats.py +22 -23
  42. careamics/dataset/in_memory_dataset.py +11 -12
  43. careamics/dataset/iterable_dataset.py +4 -4
  44. careamics/dataset/iterable_pred_dataset.py +2 -1
  45. careamics/dataset/iterable_tiled_pred_dataset.py +2 -1
  46. careamics/dataset/patching/random_patching.py +11 -10
  47. careamics/dataset/patching/sequential_patching.py +26 -26
  48. careamics/dataset/patching/validate_patch_dimension.py +3 -3
  49. careamics/dataset/tiling/__init__.py +2 -2
  50. careamics/dataset/tiling/collate_tiles.py +3 -3
  51. careamics/dataset/tiling/lvae_tiled_patching.py +2 -1
  52. careamics/dataset/tiling/tiled_patching.py +11 -10
  53. careamics/file_io/__init__.py +5 -5
  54. careamics/file_io/read/__init__.py +1 -1
  55. careamics/file_io/read/get_func.py +2 -2
  56. careamics/file_io/write/__init__.py +2 -2
  57. careamics/lightning/__init__.py +5 -5
  58. careamics/lightning/callbacks/__init__.py +1 -1
  59. careamics/lightning/callbacks/prediction_writer_callback/__init__.py +3 -3
  60. careamics/lightning/callbacks/prediction_writer_callback/prediction_writer_callback.py +2 -1
  61. careamics/lightning/callbacks/prediction_writer_callback/write_strategy.py +2 -1
  62. careamics/lightning/callbacks/progress_bar_callback.py +2 -2
  63. careamics/lightning/lightning_module.py +69 -34
  64. careamics/lightning/train_data_module.py +41 -27
  65. careamics/losses/__init__.py +3 -3
  66. careamics/losses/loss_factory.py +1 -85
  67. careamics/losses/lvae/losses.py +223 -164
  68. careamics/lvae_training/calibration.py +184 -0
  69. careamics/lvae_training/dataset/config.py +2 -2
  70. careamics/lvae_training/dataset/multich_dataset.py +11 -19
  71. careamics/lvae_training/dataset/multifile_dataset.py +3 -2
  72. careamics/lvae_training/dataset/types.py +15 -26
  73. careamics/lvae_training/dataset/utils/index_manager.py +4 -4
  74. careamics/lvae_training/eval_utils.py +125 -213
  75. careamics/model_io/__init__.py +1 -1
  76. careamics/model_io/bioimage/__init__.py +1 -1
  77. careamics/model_io/bioimage/_readme_factory.py +26 -34
  78. careamics/model_io/bioimage/cover_factory.py +171 -0
  79. careamics/model_io/bioimage/model_description.py +56 -34
  80. careamics/model_io/bmz_io.py +42 -42
  81. careamics/model_io/model_io_utils.py +9 -9
  82. careamics/models/layers.py +22 -20
  83. careamics/models/lvae/layers.py +348 -975
  84. careamics/models/lvae/likelihoods.py +10 -8
  85. careamics/models/lvae/lvae.py +214 -275
  86. careamics/models/lvae/noise_models.py +179 -112
  87. careamics/models/lvae/stochastic.py +393 -0
  88. careamics/models/lvae/utils.py +82 -73
  89. careamics/models/model_factory.py +2 -15
  90. careamics/models/unet.py +8 -8
  91. careamics/prediction_utils/__init__.py +1 -1
  92. careamics/prediction_utils/prediction_outputs.py +15 -15
  93. careamics/prediction_utils/stitch_prediction.py +6 -6
  94. careamics/transforms/__init__.py +5 -5
  95. careamics/transforms/compose.py +13 -13
  96. careamics/transforms/n2v_manipulate.py +3 -3
  97. careamics/transforms/pixel_manipulation.py +9 -9
  98. careamics/transforms/xy_random_rotate90.py +4 -4
  99. careamics/utils/__init__.py +5 -5
  100. careamics/utils/context.py +2 -1
  101. careamics/utils/lightning_utils.py +57 -0
  102. careamics/utils/logging.py +11 -10
  103. careamics/utils/serializers.py +2 -0
  104. careamics/utils/torch_utils.py +8 -8
  105. {careamics-0.0.4.2.dist-info → careamics-0.0.6.dist-info}/METADATA +16 -13
  106. careamics-0.0.6.dist-info/RECORD +176 -0
  107. {careamics-0.0.4.2.dist-info → careamics-0.0.6.dist-info}/WHEEL +1 -1
  108. careamics/config/architectures/custom_model.py +0 -162
  109. careamics/config/architectures/register_model.py +0 -103
  110. careamics/config/configuration_model.py +0 -603
  111. careamics/config/fcn_algorithm_model.py +0 -152
  112. careamics/config/references/__init__.py +0 -45
  113. careamics/config/references/algorithm_descriptions.py +0 -132
  114. careamics/config/references/references.py +0 -39
  115. careamics/config/transformations/transform_union.py +0 -20
  116. careamics-0.0.4.2.dist-info/RECORD +0 -165
  117. {careamics-0.0.4.2.dist-info → careamics-0.0.6.dist-info}/entry_points.txt +0 -0
  118. {careamics-0.0.4.2.dist-info → careamics-0.0.6.dist-info}/licenses/LICENSE +0 -0
@@ -2,8 +2,9 @@
2
2
 
3
3
  from __future__ import annotations
4
4
 
5
+ from collections.abc import Sequence
5
6
  from pprint import pformat
6
- from typing import Any, Literal, Optional, Union
7
+ from typing import Annotated, Any, Literal, Optional, Union
7
8
 
8
9
  import numpy as np
9
10
  from numpy.typing import NDArray
@@ -15,11 +16,10 @@ from pydantic import (
15
16
  field_validator,
16
17
  model_validator,
17
18
  )
18
- from typing_extensions import Annotated, Self
19
+ from typing_extensions import Self
19
20
 
20
- from .support import SupportedTransform
21
- from .transformations import TRANSFORMS_UNION, N2VManipulateModel
22
- from .validators import check_axes_validity, patch_size_ge_than_8_power_of_2
21
+ from ..transformations import N2V_TRANSFORMS_UNION, XYFlipModel, XYRandomRotate90Model
22
+ from ..validators import check_axes_validity, patch_size_ge_than_8_power_of_2
23
23
 
24
24
 
25
25
  def np_float_to_scientific_str(x: float) -> str:
@@ -45,47 +45,8 @@ Float = Annotated[float, PlainSerializer(np_float_to_scientific_str, return_type
45
45
  """Annotated float type, used to serialize floats to strings."""
46
46
 
47
47
 
48
- class DataConfig(BaseModel):
49
- """
50
- Data configuration.
51
-
52
- If std is specified, mean must be specified as well. Note that setting the std first
53
- and then the mean (if they were both `None` before) will raise a validation error.
54
- Prefer instead `set_mean_and_std` to set both at once. Means and stds are expected
55
- to be lists of floats, one for each channel. For supervised tasks, the mean and std
56
- of the target could be different from the input data.
57
-
58
- All supported transforms are defined in the SupportedTransform enum.
59
-
60
- Examples
61
- --------
62
- Minimum example:
63
-
64
- >>> data = DataConfig(
65
- ... data_type="array", # defined in SupportedData
66
- ... patch_size=[128, 128],
67
- ... batch_size=4,
68
- ... axes="YX"
69
- ... )
70
-
71
- To change the image_means and image_stds of the data:
72
- >>> data.set_means_and_stds(image_means=[214.3], image_stds=[84.5])
73
-
74
- One can pass also a list of transformations, by keyword, using the
75
- SupportedTransform value:
76
- >>> from careamics.config.support import SupportedTransform
77
- >>> data = DataConfig(
78
- ... data_type="tiff",
79
- ... patch_size=[128, 128],
80
- ... batch_size=4,
81
- ... axes="YX",
82
- ... transforms=[
83
- ... {
84
- ... "name": "XYFlip",
85
- ... }
86
- ... ]
87
- ... )
88
- """
48
+ class GeneralDataConfig(BaseModel):
49
+ """General data configuration."""
89
50
 
90
51
  # Pydantic class configuration
91
52
  model_config = ConfigDict(
@@ -126,22 +87,18 @@ class DataConfig(BaseModel):
126
87
  """Standard deviations of the target data across channels, used for
127
88
  normalization."""
128
89
 
129
- transforms: list[TRANSFORMS_UNION] = Field(
90
+ # defining as Sequence allows assigning subclasses of TransformModel without mypy
91
+ # complaining, this is important for instance to differentiate N2VDataConfig and
92
+ # DataConfig
93
+ transforms: Sequence[N2V_TRANSFORMS_UNION] = Field(
130
94
  default=[
131
- {
132
- "name": SupportedTransform.XY_FLIP.value,
133
- },
134
- {
135
- "name": SupportedTransform.XY_RANDOM_ROTATE90.value,
136
- },
137
- {
138
- "name": SupportedTransform.N2V_MANIPULATE.value,
139
- },
95
+ XYFlipModel(),
96
+ XYRandomRotate90Model(),
140
97
  ],
141
98
  validate_default=True,
142
99
  )
143
100
  """List of transformations to apply to the data, available transforms are defined
144
- in SupportedTransform. The default values are set for Noise2Void."""
101
+ in SupportedTransform."""
145
102
 
146
103
  dataloader_params: Optional[dict] = None
147
104
  """Dictionary of PyTorch dataloader parameters."""
@@ -210,48 +167,6 @@ class DataConfig(BaseModel):
210
167
 
211
168
  return axes
212
169
 
213
- @field_validator("transforms")
214
- @classmethod
215
- def validate_prediction_transforms(
216
- cls, transforms: list[TRANSFORMS_UNION]
217
- ) -> list[TRANSFORMS_UNION]:
218
- """
219
- Validate N2VManipulate transform position in the transform list.
220
-
221
- Parameters
222
- ----------
223
- transforms : list[Transformations_Union]
224
- Transforms.
225
-
226
- Returns
227
- -------
228
- list of transforms
229
- Validated transforms.
230
-
231
- Raises
232
- ------
233
- ValueError
234
- If multiple instances of N2VManipulate are found.
235
- """
236
- transform_list = [t.name for t in transforms]
237
-
238
- if SupportedTransform.N2V_MANIPULATE in transform_list:
239
- # multiple N2V_MANIPULATE
240
- if transform_list.count(SupportedTransform.N2V_MANIPULATE.value) > 1:
241
- raise ValueError(
242
- f"Multiple instances of "
243
- f"{SupportedTransform.N2V_MANIPULATE} transforms "
244
- f"are not allowed."
245
- )
246
-
247
- # N2V_MANIPULATE not the last transform
248
- elif transform_list[-1] != SupportedTransform.N2V_MANIPULATE:
249
- index = transform_list.index(SupportedTransform.N2V_MANIPULATE.value)
250
- transform = transforms.pop(index)
251
- transforms.append(transform)
252
-
253
- return transforms
254
-
255
170
  @model_validator(mode="after")
256
171
  def std_only_with_mean(self: Self) -> Self:
257
172
  """
@@ -350,32 +265,6 @@ class DataConfig(BaseModel):
350
265
  self.__dict__.update(kwargs)
351
266
  self.__class__.model_validate(self.__dict__)
352
267
 
353
- def has_n2v_manipulate(self) -> bool:
354
- """
355
- Check if the transforms contain N2VManipulate.
356
-
357
- Returns
358
- -------
359
- bool
360
- True if the transforms contain N2VManipulate, False otherwise.
361
- """
362
- return any(
363
- transform.name == SupportedTransform.N2V_MANIPULATE.value
364
- for transform in self.transforms
365
- )
366
-
367
- def add_n2v_manipulate(self) -> None:
368
- """Add N2VManipulate to the transforms."""
369
- if not self.has_n2v_manipulate():
370
- self.transforms.append(
371
- N2VManipulateModel(name=SupportedTransform.N2V_MANIPULATE.value)
372
- )
373
-
374
- def remove_n2v_manipulate(self) -> None:
375
- """Remove N2VManipulate from the transforms."""
376
- if self.has_n2v_manipulate():
377
- self.transforms.pop(-1)
378
-
379
268
  def set_means_and_stds(
380
269
  self,
381
270
  image_means: Union[NDArray, tuple, list, None],
@@ -430,84 +319,55 @@ class DataConfig(BaseModel):
430
319
  """
431
320
  self._update(axes=axes, patch_size=patch_size)
432
321
 
433
- def set_N2V2(self, use_n2v2: bool) -> None:
434
- """
435
- Set N2V2.
436
-
437
- Parameters
438
- ----------
439
- use_n2v2 : bool
440
- Whether to use N2V2.
441
-
442
- Raises
443
- ------
444
- ValueError
445
- If the N2V pixel manipulate transform is not found in the transforms.
446
- """
447
- if use_n2v2:
448
- self.set_N2V2_strategy("median")
449
- else:
450
- self.set_N2V2_strategy("uniform")
451
-
452
- def set_N2V2_strategy(self, strategy: Literal["uniform", "median"]) -> None:
453
- """
454
- Set N2V2 strategy.
455
-
456
- Parameters
457
- ----------
458
- strategy : Literal["uniform", "median"]
459
- Strategy to use for N2V2.
460
322
 
461
- Raises
462
- ------
463
- ValueError
464
- If the N2V pixel manipulate transform is not found in the transforms.
465
- """
466
- found_n2v = False
467
-
468
- for transform in self.transforms:
469
- if transform.name == SupportedTransform.N2V_MANIPULATE.value:
470
- transform.strategy = strategy
471
- found_n2v = True
323
+ class DataConfig(GeneralDataConfig):
324
+ """
325
+ Data configuration.
472
326
 
473
- if not found_n2v:
474
- transforms = [t.name for t in self.transforms]
475
- raise ValueError(
476
- f"N2V_Manipulate transform not found in the transforms list "
477
- f"({transforms})."
478
- )
327
+ If std is specified, mean must be specified as well. Note that setting the std first
328
+ and then the mean (if they were both `None` before) will raise a validation error.
329
+ Prefer instead `set_mean_and_std` to set both at once. Means and stds are expected
330
+ to be lists of floats, one for each channel. For supervised tasks, the mean and std
331
+ of the target could be different from the input data.
479
332
 
480
- def set_structN2V_mask(
481
- self, mask_axis: Literal["horizontal", "vertical", "none"], mask_span: int
482
- ) -> None:
483
- """
484
- Set structN2V mask parameters.
333
+ All supported transforms are defined in the SupportedTransform enum.
485
334
 
486
- Setting `mask_axis` to `none` will disable structN2V.
335
+ Examples
336
+ --------
337
+ Minimum example:
487
338
 
488
- Parameters
489
- ----------
490
- mask_axis : Literal["horizontal", "vertical", "none"]
491
- Axis along which to apply the mask. `none` will disable structN2V.
492
- mask_span : int
493
- Total span of the mask in pixels.
339
+ >>> data = DataConfig(
340
+ ... data_type="array", # defined in SupportedData
341
+ ... patch_size=[128, 128],
342
+ ... batch_size=4,
343
+ ... axes="YX"
344
+ ... )
494
345
 
495
- Raises
496
- ------
497
- ValueError
498
- If the N2V pixel manipulate transform is not found in the transforms.
499
- """
500
- found_n2v = False
346
+ To change the image_means and image_stds of the data:
347
+ >>> data.set_means_and_stds(image_means=[214.3], image_stds=[84.5])
501
348
 
502
- for transform in self.transforms:
503
- if transform.name == SupportedTransform.N2V_MANIPULATE.value:
504
- transform.struct_mask_axis = mask_axis
505
- transform.struct_mask_span = mask_span
506
- found_n2v = True
349
+ One can pass also a list of transformations, by keyword, using the
350
+ SupportedTransform value:
351
+ >>> from careamics.config.support import SupportedTransform
352
+ >>> data = DataConfig(
353
+ ... data_type="tiff",
354
+ ... patch_size=[128, 128],
355
+ ... batch_size=4,
356
+ ... axes="YX",
357
+ ... transforms=[
358
+ ... {
359
+ ... "name": "XYFlip",
360
+ ... }
361
+ ... ]
362
+ ... )
363
+ """
507
364
 
508
- if not found_n2v:
509
- transforms = [t.name for t in self.transforms]
510
- raise ValueError(
511
- f"N2V pixel manipulate transform not found in the transforms "
512
- f"({transforms})."
513
- )
365
+ transforms: Sequence[Union[XYFlipModel, XYRandomRotate90Model]] = Field(
366
+ default=[
367
+ XYFlipModel(),
368
+ XYRandomRotate90Model(),
369
+ ],
370
+ validate_default=True,
371
+ )
372
+ """List of transformations to apply to the data, available transforms are defined
373
+ in SupportedTransform. This excludes N2V specific transformations."""
@@ -0,0 +1,193 @@
1
+ """Noise2Void specific data configuration model."""
2
+
3
+ from collections.abc import Sequence
4
+ from typing import Literal
5
+
6
+ from pydantic import Field, field_validator
7
+
8
+ from careamics.config.data.data_model import GeneralDataConfig
9
+ from careamics.config.support import SupportedTransform
10
+ from careamics.config.transformations import (
11
+ N2V_TRANSFORMS_UNION,
12
+ N2VManipulateModel,
13
+ XYFlipModel,
14
+ XYRandomRotate90Model,
15
+ )
16
+
17
+
18
+ class N2VDataConfig(GeneralDataConfig):
19
+ """N2V specific data configuration model."""
20
+
21
+ transforms: Sequence[N2V_TRANSFORMS_UNION] = Field(
22
+ default=[XYFlipModel(), XYRandomRotate90Model(), N2VManipulateModel()],
23
+ validate_default=True,
24
+ )
25
+ """N2V compatible transforms. N2VManpulate should be the last transform."""
26
+
27
+ @field_validator("transforms")
28
+ @classmethod
29
+ def validate_transforms(
30
+ cls, transforms: list[N2V_TRANSFORMS_UNION]
31
+ ) -> list[N2V_TRANSFORMS_UNION]:
32
+ """
33
+ Validate N2VManipulate transform position in the transform list.
34
+
35
+ Parameters
36
+ ----------
37
+ transforms : list of transforms compatible with N2V
38
+ Transforms.
39
+
40
+ Returns
41
+ -------
42
+ list of transforms
43
+ Validated transforms.
44
+
45
+ Raises
46
+ ------
47
+ ValueError
48
+ If multiple instances of N2VManipulate are found or if it is not the last
49
+ transform.
50
+ """
51
+ transform_list = [t.name for t in transforms]
52
+
53
+ if SupportedTransform.N2V_MANIPULATE in transform_list:
54
+ # multiple N2V_MANIPULATE
55
+ if transform_list.count(SupportedTransform.N2V_MANIPULATE.value) > 1:
56
+ raise ValueError(
57
+ f"Multiple instances of "
58
+ f"{SupportedTransform.N2V_MANIPULATE} transforms "
59
+ f"are not allowed."
60
+ )
61
+
62
+ # N2V_MANIPULATE not the last transform
63
+ elif transform_list[-1] != SupportedTransform.N2V_MANIPULATE:
64
+ raise ValueError(
65
+ f"{SupportedTransform.N2V_MANIPULATE} transform "
66
+ f"should be the last transform."
67
+ )
68
+
69
+ else:
70
+ raise ValueError(
71
+ f"{SupportedTransform.N2V_MANIPULATE} transform "
72
+ f"is required for N2V training."
73
+ )
74
+
75
+ return transforms
76
+
77
+ def set_n2v2(self, use_n2v2: bool) -> None:
78
+ """
79
+ Set the N2V transform to the N2V2 version.
80
+
81
+ Parameters
82
+ ----------
83
+ use_n2v2 : bool
84
+ Whether to use N2V2.
85
+
86
+ Raises
87
+ ------
88
+ ValueError
89
+ If the N2V pixel manipulate transform is not found in the transforms.
90
+ """
91
+ if use_n2v2:
92
+ self.set_masking_strategy("median")
93
+ else:
94
+ self.set_masking_strategy("uniform")
95
+
96
+ def set_masking_strategy(self, strategy: Literal["uniform", "median"]) -> None:
97
+ """
98
+ Set masking strategy.
99
+
100
+ Parameters
101
+ ----------
102
+ strategy : "uniform" or "median"
103
+ Strategy to use for N2V2.
104
+
105
+ Raises
106
+ ------
107
+ ValueError
108
+ If the N2V pixel manipulate transform is not found in the transforms.
109
+ """
110
+ found_n2v = False
111
+
112
+ for transform in self.transforms:
113
+ if transform.name == SupportedTransform.N2V_MANIPULATE.value:
114
+ transform.strategy = strategy
115
+ found_n2v = True
116
+
117
+ if not found_n2v:
118
+ transforms = [t.name for t in self.transforms]
119
+ raise ValueError(
120
+ f"N2V_Manipulate transform not found in the transforms list "
121
+ f"({transforms})."
122
+ )
123
+
124
+ def get_masking_strategy(self) -> Literal["uniform", "median"]:
125
+ """
126
+ Get N2V2 strategy.
127
+
128
+ Returns
129
+ -------
130
+ "uniform" or "median"
131
+ Strategy used for N2V2.
132
+ """
133
+ for transform in self.transforms:
134
+ if transform.name == SupportedTransform.N2V_MANIPULATE.value:
135
+ return transform.strategy
136
+
137
+ raise ValueError(
138
+ f"{SupportedTransform.N2V_MANIPULATE} transform "
139
+ f"is required for N2V training."
140
+ )
141
+
142
+ def set_structN2V_mask(
143
+ self, mask_axis: Literal["horizontal", "vertical", "none"], mask_span: int
144
+ ) -> None:
145
+ """
146
+ Set structN2V mask parameters.
147
+
148
+ Setting `mask_axis` to `none` will disable structN2V.
149
+
150
+ Parameters
151
+ ----------
152
+ mask_axis : Literal["horizontal", "vertical", "none"]
153
+ Axis along which to apply the mask. `none` will disable structN2V.
154
+ mask_span : int
155
+ Total span of the mask in pixels.
156
+
157
+ Raises
158
+ ------
159
+ ValueError
160
+ If the N2V pixel manipulate transform is not found in the transforms.
161
+ """
162
+ found_n2v = False
163
+
164
+ for transform in self.transforms:
165
+ if transform.name == SupportedTransform.N2V_MANIPULATE.value:
166
+ transform.struct_mask_axis = mask_axis
167
+ transform.struct_mask_span = mask_span
168
+ found_n2v = True
169
+
170
+ if not found_n2v:
171
+ transforms = [t.name for t in self.transforms]
172
+ raise ValueError(
173
+ f"N2V pixel manipulate transform not found in the transforms "
174
+ f"({transforms})."
175
+ )
176
+
177
+ def is_using_struct_n2v(self) -> bool:
178
+ """
179
+ Check if structN2V is enabled.
180
+
181
+ Returns
182
+ -------
183
+ bool
184
+ Whether structN2V is enabled or not.
185
+ """
186
+ for transform in self.transforms:
187
+ if transform.name == SupportedTransform.N2V_MANIPULATE.value:
188
+ return transform.struct_mask_axis != "none"
189
+
190
+ raise ValueError(
191
+ f"N2V pixel manipulate transform not found in the transforms "
192
+ f"({self.transforms})."
193
+ )
@@ -1,11 +1,10 @@
1
1
  """Likelihood model."""
2
2
 
3
- from typing import Literal, Optional, Union
3
+ from typing import Annotated, Literal, Optional, Union
4
4
 
5
5
  import numpy as np
6
6
  import torch
7
- from pydantic import BaseModel, ConfigDict, Field, PlainSerializer, PlainValidator
8
- from typing_extensions import Annotated
7
+ from pydantic import BaseModel, ConfigDict, PlainSerializer, PlainValidator
9
8
 
10
9
  from careamics.models.lvae.noise_models import (
11
10
  GaussianMixtureNoiseModel,
@@ -41,7 +40,12 @@ class GaussianLikelihoodConfig(BaseModel):
41
40
 
42
41
 
43
42
  class NMLikelihoodConfig(BaseModel):
44
- """Noise model likelihood configuration."""
43
+ """Noise model likelihood configuration.
44
+
45
+ NOTE: we need to define the data mean and std here because the noise model
46
+ is trained on not-normalized data. Hence, we need to unnormalize the model
47
+ output to compute the noise model likelihood.
48
+ """
45
49
 
46
50
  model_config = ConfigDict(validate_assignment=True, arbitrary_types_allowed=True)
47
51
 
@@ -54,7 +58,3 @@ class NMLikelihoodConfig(BaseModel):
54
58
  data_std: Tensor = torch.ones(1)
55
59
  """The standard deviation of the data, used to unnormalize data for noise
56
60
  model evaluation. Shape is (target_ch,) (or (1, target_ch, [1], 1, 1))."""
57
-
58
- # TODO: serialization/deserialization for this
59
- noise_model: Optional[NoiseModel] = Field(default=None, exclude=True)
60
- """The noise model instance used to compute the likelihood."""
@@ -0,0 +1,56 @@
1
+ """Configuration classes for LVAE losses."""
2
+
3
+ from typing import Literal
4
+
5
+ from pydantic import BaseModel, ConfigDict
6
+
7
+
8
+ class KLLossConfig(BaseModel):
9
+ """KL loss configuration."""
10
+
11
+ model_config = ConfigDict(validate_assignment=True, validate_default=True)
12
+
13
+ loss_type: Literal["kl", "kl_restricted"] = "kl"
14
+ """Type of KL divergence used as KL loss."""
15
+ rescaling: Literal["latent_dim", "image_dim"] = "latent_dim"
16
+ """Rescaling of the KL loss."""
17
+ aggregation: Literal["sum", "mean"] = "mean"
18
+ """Aggregation of the KL loss across different layers."""
19
+ free_bits_coeff: float = 0.0
20
+ """Free bits coefficient for the KL loss."""
21
+ annealing: bool = False
22
+ """Whether to apply KL loss annealing."""
23
+ start: int = -1
24
+ """Epoch at which KL loss annealing starts."""
25
+ annealtime: int = 10
26
+ """Number of epochs for which KL loss annealing is applied."""
27
+ current_epoch: int = 0
28
+ """Current epoch in the training loop."""
29
+
30
+
31
+ class LVAELossConfig(BaseModel):
32
+ """LVAE loss configuration."""
33
+
34
+ model_config = ConfigDict(
35
+ validate_assignment=True, validate_default=True, arbitrary_types_allowed=True
36
+ )
37
+
38
+ loss_type: Literal["musplit", "denoisplit", "denoisplit_musplit"]
39
+ """Type of loss to use for LVAE."""
40
+
41
+ reconstruction_weight: float = 1.0
42
+ """Weight for the reconstruction loss in the total net loss
43
+ (i.e., `net_loss = reconstruction_weight * rec_loss + kl_weight * kl_loss`)."""
44
+ kl_weight: float = 1.0
45
+ """Weight for the KL loss in the total net loss.
46
+ (i.e., `net_loss = reconstruction_weight * rec_loss + kl_weight * kl_loss`)."""
47
+ musplit_weight: float = 0.1
48
+ """Weight for the muSplit loss (used in the muSplit-denoiSplit loss)."""
49
+ denoisplit_weight: float = 0.9
50
+ """Weight for the denoiSplit loss (used in the muSplit-deonoiSplit loss)."""
51
+ kl_params: KLLossConfig = KLLossConfig()
52
+ """KL loss configuration."""
53
+
54
+ # TODO: remove?
55
+ non_stochastic: bool = False
56
+ """Whether to sample latents and compute KL."""