Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add code style via ruff #573

Merged
merged 20 commits into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .codespellignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
assertIn
mapp
padd
struc
TE
warmup
16 changes: 8 additions & 8 deletions .github/workflows/code-style.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ concurrency:
group: ${{ github.workflow }}-${{ github.event.number }}-${{ github.event.ref }}
cancel-in-progress: true
on:
# pull_request:
# push:
# branches: [dev]
pull_request:
push:
branches: [dev]
workflow_dispatch:

jobs:
Expand All @@ -32,9 +32,9 @@ jobs:
with:
check_filenames: true
check_hidden: true
skip: './.git,./build,./.mypy_cache,./.pytest_cache'
skip: './build,./doc/images,./Tutorial,./.git,./.mypy_cache,./.pytest_cache'
ignore_words_file: ./.codespellignore
- name: Run pydocstyle
run: pydocstyle .
- name: Run bibclean
run: bibclean-check doc/references.bib
# - name: Run pydocstyle
# run: pydocstyle .
# - name: Run bibclean
# run: bibclean-check doc/references.bib
6 changes: 3 additions & 3 deletions CerebNet/apply_warp.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import argparse
from os.path import join

import nibabel as nib

# Copyright 2022 Image Analysis Lab, German Center for Neurodegenerative Diseases (DZNE), Bonn
#
Expand All @@ -13,12 +16,9 @@
# 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.

# IMPORTS
import numpy as np
import nibabel as nib

from os.path import join
from CerebNet.datasets import utils


Expand Down
1 change: 1 addition & 0 deletions CerebNet/config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
# IMPORTS
from CerebNet.config.cerebnet import get_cfg_cerebnet
from CerebNet.config.dataset import get_cfg_dataset

__all__ = [
"cerebnet",
"dataset",
Expand Down
2 changes: 1 addition & 1 deletion CerebNet/config/cerebnet.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@
# Data Augmentation options
# ---------------------------------------------------------------------------- #

# Augmentation for traning
# Augmentation for training
_C.AUGMENTATION = CN()

# list of augmentations to use for training
Expand Down
18 changes: 8 additions & 10 deletions CerebNet/data_loader/augmentation.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,19 @@
# IMPORTS
import numbers
import random
from typing import Optional

import numpy as np
import torch
from numpy import random as npr
from scipy.ndimage import gaussian_filter, affine_transform
from scipy.ndimage import affine_transform, gaussian_filter
from scipy.stats import median_abs_deviation
from torchvision import transforms

from CerebNet.data_loader.data_utils import FLIPPED_LABELS



# Transformations for training
class ToTensor(object):
class ToTensor:
"""
Convert ndarrays in sample to Tensors.
"""
Expand Down Expand Up @@ -66,7 +64,7 @@ def _apply_img(self, img):
return super()._apply_img(img.transpose((2, 0, 1)))


class RandomAffine(object):
class RandomAffine:
"""
Apply a random affine transformation to
images, label and weight
Expand Down Expand Up @@ -99,15 +97,15 @@ def _get_random_affine(self):
degrees = (-self.degree, self.degree)
else:
assert (
isinstance(self.degree, (tuple, list)) and len(self.degree) == 2
isinstance(self.degree, tuple | list) and len(self.degree) == 2
), "degrees should be a list or tuple and it must be of length 2."
if isinstance(self.translate, numbers.Number):
if not (0.0 <= self.translate <= 1.0):
raise ValueError("translation values should be between 0 and 1")
translate = (self.translate, self.translate)
else:
assert (
isinstance(self.translate, (tuple, list)) and len(self.translate) == 2
isinstance(self.translate, tuple | list) and len(self.translate) == 2
), "translate should be a list or tuple and it must be of length 2."
for t in self.translate:
if not (0.0 <= t <= 1.0):
Expand Down Expand Up @@ -159,7 +157,7 @@ def __call__(self, sample):
return sample


class RandomFlip(object):
class RandomFlip:
"""
Random horizontal flipping.
"""
Expand Down Expand Up @@ -196,7 +194,7 @@ class RandomBiasField:
def __init__(
self,
cfg,
seed: Optional[int] = None,
seed: int | None = None,
):
"""
Initialize the RandomBiasField object with configuration and optional seed.
Expand Down Expand Up @@ -287,7 +285,7 @@ def __call__(self, sample):
return sample


class RandomLabelsToImage(object):
class RandomLabelsToImage:
"""
Generate image from segmentation
using the dataset intensity priors.
Expand Down
2 changes: 1 addition & 1 deletion CerebNet/data_loader/data_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ def uncrop_volume(vol, uncrop_shape, roi):


def get_binary_map(lbl_map, class_names):
bin_map = np.logical_or.reduce(list(map(lambda l: lbl_map == l, class_names)))
bin_map = np.logical_or.reduce(list(map(lambda lb: lbl_map == lb, class_names)))
return bin_map


Expand Down
35 changes: 15 additions & 20 deletions CerebNet/data_loader/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,31 +13,30 @@
# limitations under the License.

# IMPORTS
from typing import Tuple, Literal, TypeVar, Dict
from numbers import Number
from typing import Literal, TypeVar

import h5py
import nibabel as nib
import torch
import numpy as np
import torch
from numpy import typing as npt
import h5py
from torch.utils.data.dataset import Dataset
from torchvision.transforms import Compose

from FastSurferCNN.utils import logging, Plane
from CerebNet.data_loader import data_utils as utils
from CerebNet.data_loader.augmentation import ToTensor
from CerebNet.datasets.load_data import SubjectLoader
from CerebNet.datasets.utils import bounding_volume_offset, crop_transform
from FastSurferCNN.data_loader.data_utils import (
get_thick_slices,
transform_axial,
transform_sagittal,
)

from CerebNet.data_loader import data_utils as utils
from CerebNet.data_loader.augmentation import ToTensor
from CerebNet.datasets.load_data import SubjectLoader
from CerebNet.datasets.utils import crop_transform, bounding_volume_offset
from FastSurferCNN.utils import Plane, logging

ROIKeys = Literal["source_shape", "offsets", "target_shape"]
LocalizerROI = Dict[ROIKeys, Tuple[int, ...]]
LocalizerROI = dict[ROIKeys, tuple[int, ...]]

NT = TypeVar("NT", bound=Number)

Expand Down Expand Up @@ -118,11 +117,7 @@ def __init__(self, dataset_path, cfg, transforms, load_aux_data):
del self.dataset["subject"]

logger.info(
"Successfully loaded {} slices in {} plane from {}".format(
self.count,
cfg.DATA.PLANE,
dataset_path,
)
f"Successfully loaded {self.count} slices in {cfg.DATA.PLANE} plane from {dataset_path}"
)

logger.info(
Expand Down Expand Up @@ -242,7 +237,7 @@ def __init__(
self,
img_org: nib.analyze.SpatialImage,
brain_seg: nib.analyze.SpatialImage,
patch_size: Tuple[int, ...],
patch_size: tuple[int, ...],
slice_thickness: int,
primary_slice: str,
):
Expand Down Expand Up @@ -298,10 +293,10 @@ def __init__(
"coronal": img,
"sagittal": transform_sagittal(img),
}
for plane, data in data.items():
for plane, data_i in data.items():
# data is transformed to 'plane'-direction in axis 2
thick_slices = get_thick_slices(
data, self.slice_thickness
data_i, self.slice_thickness
) # [H, W, n_slices, C]
# it seems x and y are flipped with respect to expectations here
self.images_per_plane[plane] = np.transpose(
Expand All @@ -315,7 +310,7 @@ def locate_mask_bbox(self, mask: npt.NDArray[bool]):
bbox of min0, min1, ..., max0, max1, ...
"""
# filter disconnected components
from skimage.measure import regionprops, label
from skimage.measure import label, regionprops

label_image = label(mask, connectivity=3)
regions = regionprops(label_image)
Expand All @@ -341,7 +336,7 @@ def plane(self) -> Plane:
"""Returns the active plane"""
return self._plane

def __getitem__(self, index: int) -> Tuple[Plane, np.ndarray]:
def __getitem__(self, index: int) -> tuple[Plane, np.ndarray]:
"""Get the plane and data belonging to indices given."""

if not (0 <= index < self.images_per_plane[self.plane].shape[0]):
Expand Down
5 changes: 2 additions & 3 deletions CerebNet/data_loader/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,12 @@
# limitations under the License.

# IMPORTS
from torchvision import transforms
from torch.utils.data import DataLoader

from FastSurferCNN.utils import logging
from torchvision import transforms

from CerebNet.data_loader import dataset as dset
from CerebNet.data_loader.augmentation import ToTensor, get_transform
from FastSurferCNN.utils import logging

logger = logging.get_logger(__name__)

Expand Down
18 changes: 7 additions & 11 deletions CerebNet/datasets/generate_hdf5.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@


# IMPORTS
import time
import warnings
from collections import defaultdict
from os.path import join, isfile
import time
from os.path import isfile, join

import h5py
import numpy as np
Expand Down Expand Up @@ -65,7 +65,7 @@ def _read_warp_dict(self):
"""
subj2warps = defaultdict(list)
all_imgs = []
with open(join(self.cfg.REG_DATA_DIR, self.cfg.REG_DATA_CSV), "r") as f:
with open(join(self.cfg.REG_DATA_DIR, self.cfg.REG_DATA_CSV)) as f:
for line in f.readlines():
line = line.strip()
ids = line.split(",")
Expand All @@ -84,7 +84,7 @@ def _read_warp_dict(self):
all_imgs.append(img_path)
subj2warps[i].append((img_path, lbl_path))
else:
warnings.warn(f"Warp field at {img_path} not found.")
warnings.warn(f"Warp field at {img_path} not found.", stacklevel=2)
return subj2warps

def create_hdf5_dataset(
Expand Down Expand Up @@ -123,9 +123,7 @@ def create_hdf5_dataset(
# try:
start = time.time()
print(
"Volume Nr: {}/{} Processing MRI Data from {}".format(
idx + 1, len(subjects_list), current_subject
)
f"Volume Nr: {idx + 1}/{len(subjects_list)} Processing MRI Data from {current_subject}"
)

in_data = self.subj_loader.load_subject(
Expand All @@ -145,9 +143,7 @@ def create_hdf5_dataset(
end = time.time() - start
print("Number of Cerebellum classes", len(np.unique(in_data["label"])))
print(
"Volume: {} Finished Data Reading and Appending in {:.3f} seconds.".format(
idx + 1, end
)
f"Volume: {idx + 1} Finished Data Reading and Appending in {end:.3f} seconds."
)

# except Exception as e:
Expand All @@ -156,4 +152,4 @@ def create_hdf5_dataset(

self._save_hdf5_file(datasets, dataset_name)
end_d = time.time() - start_d
print("Successfully written {} in {:.3f} seconds.".format(dataset_name, end_d))
print(f"Successfully written {dataset_name} in {end_d:.3f} seconds.")
6 changes: 3 additions & 3 deletions CerebNet/datasets/load_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@


# IMPORTS
from os.path import join, isfile
from functools import partial
from os.path import isfile, join

import numpy as np

Expand Down Expand Up @@ -91,9 +91,9 @@ def _load_volumes(self, subject_path, store_talairach=False):

img_meta_data = {}
orig, _ = utils.load_reorient_rescale_image(orig_path)
print("Orig image {}".format(orig_path))
print(f"Orig image {orig_path}")

print("Loading from {}".format(subseg_path))
print(f"Loading from {subseg_path}")
subseg_file = utils.load_reorient(subseg_path)
cereb_subseg = np.asarray(subseg_file.get_fdata(), dtype=np.int16)
img_meta_data["affine"] = subseg_file.affine
Expand Down
Loading
Loading