Skip to content

Commit 5bc2ccb

Browse files
ibro45dependabot[bot]surajpaib
authored
Switch from manual to pydantic validation of config and LighterSystem (#135)
* Switch from manual to pydantic validation of config and LighterSystem components * Add pydantic dependency * Do not like how Postprocessing looks. Fix style. * Match current Lighter's postprocessing scheme * Fix validators in schema * Add PatchedModuleDict, add missing "test" in ArgsConfigShema, use model_dump() and remove SubscriptableBaseModel * Fix mistake in schema * _lightning_module_methods_defined not needed anymore * Reorganize LighterSystem methods * See if pandas upgrade will fix the numpy issue in checks * Attempt to fix "Numpy is not available" in checks * Another attempt, numpy below v2 * Add missing predict to batch postprocessing schema * Bump aiohttp from 3.9.5 to 3.10.2 (#136) Bumps [aiohttp](https://github.com/aio-libs/aiohttp) from 3.9.5 to 3.10.2. - [Release notes](https://github.com/aio-libs/aiohttp/releases) - [Changelog](https://github.com/aio-libs/aiohttp/blob/master/CHANGES.rst) - [Commits](aio-libs/aiohttp@v3.9.5...v3.10.2) --- updated-dependencies: - dependency-name: aiohttp dependency-type: direct:development ... Signed-off-by: dependabot[bot] <[email protected]> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Ibrahim Hadzic <[email protected]> * Bump zipp from 3.19.0 to 3.19.1 (#132) Bumps [zipp](https://github.com/jaraco/zipp) from 3.19.0 to 3.19.1. - [Release notes](https://github.com/jaraco/zipp/releases) - [Changelog](https://github.com/jaraco/zipp/blob/main/NEWS.rst) - [Commits](jaraco/zipp@v3.19.0...v3.19.1) --- updated-dependencies: - dependency-name: zipp dependency-type: indirect ... Signed-off-by: dependabot[bot] <[email protected]> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Ibrahim Hadzic <[email protected]> * Bump certifi from 2024.2.2 to 2024.7.4 (#131) Bumps [certifi](https://github.com/certifi/python-certifi) from 2024.2.2 to 2024.7.4. - [Commits](certifi/python-certifi@2024.02.02...2024.07.04) --- updated-dependencies: - dependency-name: certifi dependency-type: indirect ... Signed-off-by: dependabot[bot] <[email protected]> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Ibrahim Hadzic <[email protected]> * Filter out the warning about "validate" field in ArgsConfigSchema * Enable interpolation strings in ArgsConfig * Allow multiple lighter commands at once (lighter fit test --config). Forbid extra fields pydantic. * Add workaround for the validation of _requires_ with pydantic * Add basic tests for schema * Fix codestyle --------- Signed-off-by: dependabot[bot] <[email protected]> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Suraj Pai <[email protected]>
1 parent 3e043af commit 5bc2ccb

File tree

14 files changed

+1061
-773
lines changed

14 files changed

+1061
-773
lines changed

lighter/callbacks/utils.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import torch
22
import torchvision
3+
from torch import Tensor
34

45

56
def get_lighter_mode(lightning_stage: str) -> str:
@@ -15,13 +16,13 @@ def get_lighter_mode(lightning_stage: str) -> str:
1516
return lightning_to_lighter[lightning_stage]
1617

1718

18-
def preprocess_image(image: torch.Tensor) -> torch.Tensor:
19+
def preprocess_image(image: Tensor) -> Tensor:
1920
"""Preprocess the image before logging it. If it is a batch of multiple images,
2021
it will create a grid image of them. In case of 3D, a single image is displayed
2122
with slices stacked vertically, while a batch of 3D images as a grid where each
2223
column is a different 3D image.
2324
Args:
24-
image (torch.Tensor): A 2D or 3D image tensor.
25+
image (Tensor): A 2D or 3D image tensor.
2526
Returns:
2627
The image ready for logging.
2728
"""

lighter/callbacks/writer/base.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22

33
import gc
44
from abc import ABC, abstractmethod
5-
from datetime import datetime
65
from pathlib import Path
76

87
import torch
98
from loguru import logger
109
from pytorch_lightning import Callback, Trainer
10+
from torch import Tensor
1111

1212
from lighter import LighterSystem
1313

@@ -48,7 +48,7 @@ def writers(self) -> Dict[str, Callable]:
4848
"""
4949

5050
@abstractmethod
51-
def write(self, tensor: torch.Tensor, id: int) -> None:
51+
def write(self, tensor: Tensor, id: int) -> None:
5252
"""
5353
Method to define how a tensor should be saved. The input tensor will be a single tensor without
5454
the batch dimension.
@@ -57,7 +57,7 @@ def write(self, tensor: torch.Tensor, id: int) -> None:
5757
A specific writer function can be retrieved using `self.get_writer(self.format)`.
5858
5959
Args:
60-
tensor (torch.Tensor): Tensor, without the batch dimension, to be saved.
60+
tensor (Tensor): Tensor, without the batch dimension, to be saved.
6161
id (int): Identifier for the tensor, can be used for naming files or adding table records.
6262
"""
6363

lighter/callbacks/writer/file.py

+3-6
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
from typing import Callable, Dict, Union
22

33
from functools import partial
4-
from pathlib import Path
54

65
import torch
76
import torchvision
87
from monai.data import metatensor_to_itk_image
98
from monai.transforms import DivisiblePad
9+
from torch import Tensor
1010

1111
from lighter.callbacks.utils import preprocess_image
1212
from lighter.callbacks.writer.base import LighterBaseWriter
@@ -27,9 +27,6 @@ class LighterFileWriter(LighterBaseWriter):
2727
`tensor` is a single tensor without the batch dimension.
2828
"""
2929

30-
def __init__(self, path: Union[str, Path], writer: Union[str, Callable]) -> None:
31-
super().__init__(path, writer)
32-
3330
@property
3431
def writers(self) -> Dict[str, Callable]:
3532
return {
@@ -41,7 +38,7 @@ def writers(self) -> Dict[str, Callable]:
4138
"itk_nifti": partial(write_itk_image, suffix=".nii.gz"),
4239
}
4340

44-
def write(self, tensor: torch.Tensor, id: Union[int, str]) -> None:
41+
def write(self, tensor: Tensor, id: Union[int, str]) -> None:
4542
"""
4643
Write the tensor using the writer specified at the instatiation.
4744
@@ -82,7 +79,7 @@ def write_video(path, tensor):
8279
torchvision.io.write_video(str(path), tensor, fps=24)
8380

8481

85-
def write_itk_image(path: str, tensor: torch.Tensor, suffix) -> None:
82+
def write_itk_image(path: str, tensor: Tensor, suffix) -> None:
8683
path = path.with_suffix(suffix)
8784
itk_image = metatensor_to_itk_image(tensor, channel_dim=0, dtype=tensor.dtype)
8885
OPTIONAL_IMPORTS["itk"].imwrite(itk_image, str(path), True)

0 commit comments

Comments
 (0)