Skip to content

Commit

Permalink
feat: function to get mean size from bboxes
Browse files Browse the repository at this point in the history
  • Loading branch information
OBrink committed Sep 18, 2023
1 parent b194315 commit 70a5b2b
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
29 changes: 28 additions & 1 deletion decimer_segmentation/decimer_segmentation.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,9 @@ def segment_chemical_structures(
a certain tolerance for grouping into "lines"(shape: (h, w, num_masks))
"""
if not expand:
masks, _, _ = get_mrcnn_results(image)
masks, bboxes, _ = get_mrcnn_results(image)
else:
average_height, average_width = determine_average_depiction_size(bboxes)
masks = get_expanded_masks(image)

segments, bboxes = apply_masks(image, masks)
Expand All @@ -117,6 +118,32 @@ def segment_chemical_structures(
return segments


def determine_average_depiction_size(
bboxes: List[Tuple[int, int, int, int]]
) -> Tuple[int, int]:
"""
This function takes a list of bounding boxes and returns the average
depiction size (height, width) of the depicted chemical structures.
Args:
bboxes (List[Tuple[int, int, int, int]]): bounding boxes of the structure
depictions (y0, x0, y1, x1)
Returns:
Tuple: average depiction size (height, width)
"""
heights = []
widths = []
for bbox in bboxes:
height = bbox[2] - bbox[0]
width = bbox[3] - bbox[1]
heights.append(height)
widths.append(width)
average_height = int(np.mean(heights))
average_width = int(np.mean(widths))
return int(average_height), int(average_width)


def sort_segments_bboxes(
segments: List[np.array],
bboxes: List[Tuple[int, int, int, int]], # (y0, x0, y1, x1)
Expand Down
13 changes: 13 additions & 0 deletions tests/test_decimer_segmentation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import numpy as np
from decimer_segmentation.decimer_segmentation import *

def test_determine_average_depiction_size():
# Determine the average depiction size of the structures in a given list of structures
bboxes = [
[0, 0, 6, 6],
[0, 0, 8, 8],
[0, 0, 10, 10],
]
expected_result = (8, 8)
actual_result = determine_average_depiction_size(bboxes)
assert expected_result == actual_result

0 comments on commit 70a5b2b

Please sign in to comment.