Skip to content

Commit

Permalink
Switch from manual to pydantic validation of config and LighterSystem (
Browse files Browse the repository at this point in the history
…#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] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Ibrahim Hadzic <ibrahimhadzic45@gmail.com>

* 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] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Ibrahim Hadzic <ibrahimhadzic45@gmail.com>

* 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] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Ibrahim Hadzic <ibrahimhadzic45@gmail.com>

* 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] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Suraj Pai <bspai@bwh.harvard.edu>
  • Loading branch information
3 people authored Sep 4, 2024
1 parent 3e043af commit 5bc2ccb
Show file tree
Hide file tree
Showing 14 changed files with 1,061 additions and 773 deletions.
5 changes: 3 additions & 2 deletions lighter/callbacks/utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import torch
import torchvision
from torch import Tensor


def get_lighter_mode(lightning_stage: str) -> str:
Expand All @@ -15,13 +16,13 @@ def get_lighter_mode(lightning_stage: str) -> str:
return lightning_to_lighter[lightning_stage]


def preprocess_image(image: torch.Tensor) -> torch.Tensor:
def preprocess_image(image: Tensor) -> Tensor:
"""Preprocess the image before logging it. If it is a batch of multiple images,
it will create a grid image of them. In case of 3D, a single image is displayed
with slices stacked vertically, while a batch of 3D images as a grid where each
column is a different 3D image.
Args:
image (torch.Tensor): A 2D or 3D image tensor.
image (Tensor): A 2D or 3D image tensor.
Returns:
The image ready for logging.
"""
Expand Down
6 changes: 3 additions & 3 deletions lighter/callbacks/writer/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

import gc
from abc import ABC, abstractmethod
from datetime import datetime
from pathlib import Path

import torch
from loguru import logger
from pytorch_lightning import Callback, Trainer
from torch import Tensor

from lighter import LighterSystem

Expand Down Expand Up @@ -48,7 +48,7 @@ def writers(self) -> Dict[str, Callable]:
"""

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

Expand Down
9 changes: 3 additions & 6 deletions lighter/callbacks/writer/file.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
from typing import Callable, Dict, Union

from functools import partial
from pathlib import Path

import torch
import torchvision
from monai.data import metatensor_to_itk_image
from monai.transforms import DivisiblePad
from torch import Tensor

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

def __init__(self, path: Union[str, Path], writer: Union[str, Callable]) -> None:
super().__init__(path, writer)

@property
def writers(self) -> Dict[str, Callable]:
return {
Expand All @@ -41,7 +38,7 @@ def writers(self) -> Dict[str, Callable]:
"itk_nifti": partial(write_itk_image, suffix=".nii.gz"),
}

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


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

0 comments on commit 5bc2ccb

Please sign in to comment.