Skip to content

Commit

Permalink
Sort imports in ParticleDetection
Browse files Browse the repository at this point in the history
Sort the imports (see #80) and apply formatting with black in the ParticleDetection files.
  • Loading branch information
a-niem committed Jan 30, 2024
1 parent 2719eb7 commit 3ac99fb
Show file tree
Hide file tree
Showing 33 changed files with 2,450 additions and 1,410 deletions.
25 changes: 13 additions & 12 deletions ParticleDetection/src/ParticleDetection/__init__.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
# Copyright (c) 2023 Adrian Niemann Dmitry Puzyrev
# Copyright (c) 2023-24 Adrian Niemann, Dmitry Puzyrev, and others
#
# This file is part of ParticleDetection.
# ParticleDetection is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This file is part of ParticleDetection.
# ParticleDetection is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# ParticleDetection is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# ParticleDetection is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with ParticleDetection. If not, see <http://www.gnu.org/licenses/>.
# You should have received a copy of the GNU General Public License
# along with ParticleDetection. If not, see <http://www.gnu.org/licenses/>.

import logging

logging.getLogger(__name__).addHandler(logging.NullHandler())
76 changes: 43 additions & 33 deletions ParticleDetection/src/ParticleDetection/modelling/annotations.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
# Copyright (c) 2023 Adrian Niemann Dmitry Puzyrev
# Copyright (c) 2023-24 Adrian Niemann, Dmitry Puzyrev
#
# This file is part of ParticleDetection.
# ParticleDetection is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This file is part of ParticleDetection.
# ParticleDetection is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# ParticleDetection is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# ParticleDetection is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with ParticleDetection. If not, see <http://www.gnu.org/licenses/>.
# You should have received a copy of the GNU General Public License
# along with ParticleDetection. If not, see <http://www.gnu.org/licenses/>.

"""
Collection of function to manipulate training dataset metadata in json format.
Expand All @@ -23,15 +23,15 @@
**Date:** 31.10.2022
"""
import os
import json
import logging
import os

import torch
import numpy as np
from PIL import Image
import torch
from detectron2.structures import Instances
from detectron2.utils.visualizer import GenericMask
from PIL import Image

import ParticleDetection.utils.datasets as ds
import ParticleDetection.utils.helper_funcs as hf
Expand Down Expand Up @@ -59,12 +59,14 @@ def remove_duplicate_regions(dataset: ds.DataSet) -> None:
used.append(item)
annotations[img]["regions"] = used
_logger.info(f"origial: {len(regions)}, new: {len(used)}")
deleted_duplicates += (len(regions) - len(used))
deleted_duplicates += len(regions) - len(used)

with open(dataset.annotation, 'w') as metadata:
with open(dataset.annotation, "w") as metadata:
json.dump(annotations, metadata, indent=2)
_logger.info(f"######################################\n"
f"Deleted duplicates: {deleted_duplicates}")
_logger.info(
"######################################\n"
f"Deleted duplicates: {deleted_duplicates}"
)
return


Expand Down Expand Up @@ -102,8 +104,9 @@ def change_class(file: str) -> None:

for idx_f, val_f in to_change.items():
for idx_r, reg in enumerate(val_f["regions"]):
to_change[idx_f]["regions"][idx_r]["region_attributes"]["rod_col"]\
= 0
to_change[idx_f]["regions"][idx_r]["region_attributes"][
"rod_col"
] = 0

with open(file, "w") as f:
json.dump(to_change, f, indent=2)
Expand Down Expand Up @@ -156,8 +159,11 @@ def create_keypoints(file_name: str, single_class=True, order_x=True) -> None:
Has currently no effect.
Default is ``True``.
"""
to_change = ds.DataSet("to_change", os.path.dirname(file_name) + "/",
os.path.basename(file_name))
to_change = ds.DataSet(
"to_change",
os.path.dirname(file_name) + "/",
os.path.basename(file_name),
)
classes = {cls: str(cls) for cls in ds.get_dataset_classes(to_change)}

with open(to_change.annotation) as metadata:
Expand All @@ -184,13 +190,16 @@ def create_keypoints(file_name: str, single_class=True, order_x=True) -> None:
poly = [(x + 0.5, y + 0.5) for x, y in zip(px, py)]
poly = [p for x in poly for p in x]

mask = np.asarray(GenericMask([poly], height, width).mask,
dtype=bool).tolist()
inst = {"instances": Instances(
(height, width),
pred_classes=torch.Tensor([category_id]),
pred_masks=torch.Tensor([mask])
)}
mask = np.asarray(
GenericMask([poly], height, width).mask, dtype=bool
).tolist()
inst = {
"instances": Instances(
(height, width),
pred_classes=torch.Tensor([category_id]),
pred_masks=torch.Tensor([mask]),
)
}
try:
key_points = hf.rod_endpoints(inst, classes)
key_points = key_points[str(category_id)].flatten()
Expand All @@ -199,14 +208,15 @@ def create_keypoints(file_name: str, single_class=True, order_x=True) -> None:
except UnboundLocalError as e:
# no endpoints were found
to_insert = 6 * [-1]
_logger.info(f"No endpoints found. The following error "
f"occurred:\n{e}")
_logger.info(
f"No endpoints found. The following error occurred:\n{e}"
)
annotations[key]["regions"][idx_r]["keypoints"] = to_insert

_logger.info(f"Done with: {key}")

old_file, ext = os.path.splitext(file_name)
with open(old_file + "_keypoints" + ext, 'w') as metadata:
with open(old_file + "_keypoints" + ext, "w") as metadata:
json.dump(annotations, metadata, indent=2)


Expand Down
50 changes: 30 additions & 20 deletions ParticleDetection/src/ParticleDetection/modelling/augmentations.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
# Copyright (c) 2023 Adrian Niemann Dmitry Puzyrev
# Copyright (c) 2023-24 Adrian Niemann, Dmitry Puzyrev
#
# This file is part of ParticleDetection.
# ParticleDetection is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This file is part of ParticleDetection.
# ParticleDetection is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# ParticleDetection is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# ParticleDetection is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with ParticleDetection. If not, see <http://www.gnu.org/licenses/>.
# You should have received a copy of the GNU General Public License
# along with ParticleDetection. If not, see <http://www.gnu.org/licenses/>.

"""
Collection of custom image augmentations extending the Detectron2 augmentation
Expand All @@ -27,10 +27,10 @@
import warnings
from typing import List

import numpy as np
from imgaug import augmenters
import detectron2.data.transforms as T
import numpy as np
from detectron2.data.transforms.augmentation import _transform_to_aug
from imgaug import augmenters


class SomeOf(T.AugmentationList):
Expand All @@ -48,6 +48,7 @@ class SomeOf(T.AugmentationList):
upper : int
Maximum amount of augmentations to choose.
"""

def __init__(self, augments: List[T.Augmentation], lower: int, upper: int):
self.lower = lower
self.upper = upper
Expand Down Expand Up @@ -75,6 +76,7 @@ class GaussianBlurAugmentation(T.Augmentation):
Mean and variance of the constructed Gaussian kernel.\n
Default is ``(0.0, 2.0)``.
"""

def __init__(self, sigmas: tuple = (0.0, 2.0)):
super().__init__()
self.sigmas = sigmas
Expand All @@ -92,6 +94,7 @@ class GaussianBlur(T.Transform):
Mean and variance of the constructed Gaussian kernel.\n
Default is ``(0.0, 2.0)``.
"""

def __init__(self, sigmas: tuple = (0.0, 2.0)):
super().__init__()
self.sigmas = sigmas
Expand Down Expand Up @@ -121,8 +124,10 @@ class SharpenAugmentation(T.Augmentation):
sampled from the interval per image.\n
Default is ``(0.8, 1.2)``.
"""
def __init__(self, alpha: tuple = (0.0, 0.2),
lightness: tuple = (0.8, 1.2)):

def __init__(
self, alpha: tuple = (0.0, 0.2), lightness: tuple = (0.8, 1.2)
):
self.alpha = alpha
self.lightness = lightness

Expand All @@ -144,8 +149,10 @@ class Sharpen(T.Transform):
sampled from the interval per image.\n
Default is ``(0.8, 1.2)``.
"""
def __init__(self, alpha: tuple = (0.0, 0.2),
lightness: tuple = (0.8, 1.2)):

def __init__(
self, alpha: tuple = (0.0, 0.2), lightness: tuple = (0.8, 1.2)
):
super().__init__()
self.alpha = alpha
self.lightness = lightness
Expand All @@ -158,8 +165,9 @@ def inverse(self) -> T.Transform:
return T.NoOpTransform()

def apply_image(self, img: np.ndarray) -> np.ndarray:
return augmenters.Sharpen(alpha=self.alpha,
lightness=self.lightness).augment_image(img)
return augmenters.Sharpen(
alpha=self.alpha, lightness=self.lightness
).augment_image(img)


class MultiplyAugmentation(T.Augmentation):
Expand All @@ -174,6 +182,7 @@ class MultiplyAugmentation(T.Augmentation):
pixels.\n
Default is ``(0.8, 1.2)``
"""

def __init__(self, mul: tuple = (0.8, 1.2)):
super().__init__()
self.mul = mul
Expand All @@ -193,6 +202,7 @@ class Multiply(T.Transform):
pixels.
Default is ``(0.8, 1.2)``
"""

def __init__(self, mul: tuple = (0.8, 1.2)):
super().__init__()
self.mul = mul
Expand Down
Loading

0 comments on commit 3ac99fb

Please sign in to comment.