Skip to content

Commit

Permalink
[pre-commit.ci] auto fixes from pre-commit.com hooks
Browse files Browse the repository at this point in the history
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] committed May 8, 2024
1 parent b84d53e commit 6443e9a
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 36 deletions.
33 changes: 17 additions & 16 deletions monailabel/transform/post.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,12 @@
generate_spatial_bounding_box,
get_extreme_points,
)
from monai.utils import InterpolateMode, convert_to_numpy, ensure_tuple_rep
from monai.utils import InterpolateMode, convert_to_numpy, ensure_tuple_rep, optional_import
from shapely.geometry import Point, Polygon
from skimage.measure import approximate_polygon, find_contours
from torchvision.utils import make_grid, save_image

from monailabel.utils.others.label_colors import get_color
from monai.utils import optional_import



logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -215,13 +212,13 @@ def __call__(self, data):
p = d[key]
if np.count_nonzero(p) < self.min_positive:
continue

labels = [label for label in np.unique(p).tolist() if label > 0]
for label_idx in labels:
p = convert_to_numpy(d[key]) if isinstance(d[key], torch.Tensor) else d[key]
p = np.where(p == label_idx, 1, 0).astype(np.uint8)
p = np.moveaxis(p, 0, 1)

if label_idx == 0:
continue

Check warning on line 223 in monailabel/transform/post.py

View check run for this annotation

Codecov / codecov/patch

monailabel/transform/post.py#L223

Added line #L223 was not covered by tests
label_name = self.labels.get(label_idx, label_idx)
Expand All @@ -237,13 +234,13 @@ def __call__(self, data):

contour = np.squeeze(contour)
area = cv2.contourArea(contour)
if area < min_poly_area:
if area < min_poly_area:
continue
if 0 < max_poly_area < area:
if 0 < max_poly_area < area:
continue

Check warning on line 240 in monailabel/transform/post.py

View check run for this annotation

Codecov / codecov/patch

monailabel/transform/post.py#L235-L240

Added lines #L235 - L240 were not covered by tests

contour[:, 0] += location[0]
contour[:, 1] += location[1]
contour[:, 0] += location[0]
contour[:, 1] += location[1]

Check warning on line 243 in monailabel/transform/post.py

View check run for this annotation

Codecov / codecov/patch

monailabel/transform/post.py#L242-L243

Added lines #L242 - L243 were not covered by tests

coords = contour.astype(int).tolist()
if foreground_points:
Expand All @@ -256,7 +253,7 @@ def __call__(self, data):

if len(polygons):
logger.debug(f"+++++ {label_idx} => Total Polygons Found: {len(polygons)}")
elements.append({"label": label_name, "contours": polygons})
elements.append({"label": label_name, "contours": polygons})

Check warning on line 256 in monailabel/transform/post.py

View check run for this annotation

Codecov / codecov/patch

monailabel/transform/post.py#L254-L256

Added lines #L254 - L256 were not covered by tests
else:
contours = find_contours(p, 0.5)
contours = [np.round(contour).astype(int) for contour in contours]
Expand All @@ -266,14 +263,18 @@ def __call__(self, data):

simplified_contour = approximate_polygon(contour, tolerance=0.5)
if len(simplified_contour) < 4:
continue
continue

simplified_contour = np.flip(simplified_contour, axis=1)
simplified_contour += location
simplified_contour = simplified_contour.astype(int)
simplified_contour = np.flip(simplified_contour, axis=1)
simplified_contour += location
simplified_contour = simplified_contour.astype(int)

polygon = Polygon(simplified_contour)
if polygon.is_valid and polygon.area >= min_poly_area and (max_poly_area <= 0 or polygon.area <= max_poly_area):
if (
polygon.is_valid
and polygon.area >= min_poly_area
and (max_poly_area <= 0 or polygon.area <= max_poly_area)
):
formatted_contour = [simplified_contour.tolist()]
if foreground_points:
if any(polygon.contains(point) for point in foreground_points):
Expand Down
2 changes: 1 addition & 1 deletion sample-apps/pathology/lib/trainers/hovernet_nuclei.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@
import numpy as np
from lib.hovernet import PatchExtractor
from lib.utils import split_dataset
from monai.utils import optional_import
from PIL import Image
from scipy.ndimage import label
from tqdm import tqdm

from monailabel.interfaces.datastore import Datastore
from monailabel.tasks.train.bundle import BundleConstants, BundleTrainTask
from monailabel.utils.others.generic import remove_file
from monai.utils import optional_import

logger = logging.getLogger(__name__)

Expand Down
42 changes: 23 additions & 19 deletions sample-apps/pathology/lib/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import numpy as np
import openslide
import scipy
from monai.utils import optional_import
from PIL import Image, ImageDraw
from scipy.ndimage import center_of_mass, find_objects, label
from tqdm import tqdm
Expand All @@ -29,7 +30,6 @@
from monailabel.datastore.local import LocalDatastore
from monailabel.interfaces.datastore import Datastore
from monailabel.utils.others.generic import get_basename, get_basename_no_ext, is_openslide_supported
from monai.utils import optional_import

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -478,11 +478,11 @@ def split_nuclei_dataset(

mask = Image.open(d["label"])
mask_np = np.array(mask)

cv2, has_cv2 = optional_import("cv2")
if has_cv2:
numLabels, instances, stats, centroids = cv2.connectedComponentsWithStats(mask_np, 4, cv2.CV_32S)
else:
else:
numLabels, instances = label(mask_np)
stats = []
centroids = center_of_mass(mask_np, instances, range(numLabels))
Expand All @@ -493,7 +493,7 @@ def split_nuclei_dataset(
dx, dy = slice_tuple
area = (dx.stop - dx.start) * (dy.stop - dy.start)
stats.append([dy.start, dx.start, dy.stop - dy.start, dx.stop - dx.start, area])

logger.info("-------------------------------------------------------------------------------")
logger.info(f"Image/Label ========> {d['image']} =====> {d['label']}")
logger.info(f"Total Labels: {numLabels}")
Expand All @@ -503,10 +503,10 @@ def split_nuclei_dataset(
logger.info(f"Total Classes in Mask: {np.unique(mask_np)}")

for nuclei_id, centroid in enumerate(centroids):
if nuclei_id == 0:
if nuclei_id == 0:
continue

x, y = int(centroid[1]), int(centroid[0])
x, y = int(centroid[1]), int(centroid[0])

this_instance = np.where(instances == nuclei_id, mask_np, 0)
class_id = int(np.max(this_instance))
Expand Down Expand Up @@ -573,24 +573,26 @@ def _group_item(groups, d, output_dir):


def calculate_bounding_rect(points):
points = np.array(points, dtype=int)
points = np.array(points, dtype=int)
x_min, y_min = np.min(points, axis=0)
x_max, y_max = np.max(points, axis=0)
w = x_max - x_min + 1
h = y_max - y_min + 1
w = x_max - x_min + 1
h = y_max - y_min + 1
return int(x_min), int(y_min), int(w), int(h)

def fill_poly(image_size, polygons, color, mode='L'):
if mode.upper() == 'RGB':
img = Image.new('RGB', image_size, (0, 0, 0))

def fill_poly(image_size, polygons, color, mode="L"):
if mode.upper() == "RGB":
img = Image.new("RGB", image_size, (0, 0, 0))
else:
img = Image.new('L', image_size, 0)
img = Image.new("L", image_size, 0)

draw = ImageDraw.Draw(img)
for polygon in polygons:
draw.polygon([tuple(p) for p in polygon], fill=color)
return np.array(img)


def _to_roi(points, max_region, polygons, annotation_id):
logger.info(f"Total Points: {len(points)}")

Expand All @@ -599,7 +601,7 @@ def _to_roi(points, max_region, polygons, annotation_id):
x, y, w, h = cv2.boundingRect(np.array(points))
else:
x, y, w, h = calculate_bounding_rect(points)

logger.info(f"ID: {annotation_id} => Groups: {polygons.keys()}; Location: ({x}, {y}); Size: {w} x {h}")

if w > max_region[0]:
Expand All @@ -610,6 +612,7 @@ def _to_roi(points, max_region, polygons, annotation_id):
h = max_region[1]
return x, y, w, h


def _to_dataset(item_id, x, y, w, h, img, tile_size, polygons, groups, output_dir, debug=False):
dataset_json = []

Expand All @@ -624,7 +627,6 @@ def _to_dataset(item_id, x, y, w, h, img, tile_size, polygons, groups, output_di
logger.debug(f"Image NP: {image_np.shape}; sum: {np.sum(image_np)}")
tiled_images = _region_to_tiles(name, w, h, image_np, tile_size, output_dir, "Image")


cv2, has_cv2 = optional_import("cv2")
if has_cv2:
label_np = np.zeros((h, w), dtype=np.uint8) # Transposed
Expand All @@ -642,7 +644,7 @@ def _to_dataset(item_id, x, y, w, h, img, tile_size, polygons, groups, output_di
else:
label_img = Image.new("L", (w, h), 0)
draw = ImageDraw.Draw(label_img)

for group, contours in polygons.items():
color = groups.get(group, 1)
pil_contours = [tuple((p[0] - x, p[1] - y) for p in contour) for contour in contours]
Expand All @@ -657,9 +659,11 @@ def _to_dataset(item_id, x, y, w, h, img, tile_size, polygons, groups, output_di
label_img.save(label_path)

label_np = np.array(label_img)

tiled_labels = _region_to_tiles(name, w, h, label_np, tile_size, os.path.join(output_dir, "labels", "final"), "Label")


tiled_labels = _region_to_tiles(
name, w, h, label_np, tile_size, os.path.join(output_dir, "labels", "final"), "Label"
)

for k in tiled_images:
dataset_json.append({"image": tiled_images[k], "label": tiled_labels[k]})

Expand Down

0 comments on commit 6443e9a

Please sign in to comment.