Skip to content

Commit a2d78e4

Browse files
committed
refactor: bbox coordinates are mapped to embryo number
1 parent cab7459 commit a2d78e4

4 files changed

Lines changed: 27 additions & 24 deletions

File tree

snazzy_processing/notebooks/snazzy-processing-pipeline.ipynb

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,10 @@
9292
"# If necessary, manually change the bboxes size by changing w and h:\n",
9393
"boundaries = slice_img.increase_bbox(coords, w=190, h=40, shape=img.shape)\n",
9494
"\n",
95-
"rect_coords = [slice_img.boundary_to_rect_coords(b) for b in boundaries]\n",
95+
"rect_coords = [slice_img.boundary_to_rect_coords(b) for b in boundaries.values()]\n",
9696
"recs = [Rectangle((y, x), w, h) for (x, y, w, h) in rect_coords]\n",
9797
"\n",
98-
"centroids = [((x0 + x1) // 2, (y0 + y1) // 2) for (x0, x1, y0, y1) in coords]\n",
98+
"centroids = [((x0 + x1) // 2, (y0 + y1) // 2) for (x0, x1, y0, y1) in coords.values()]\n",
9999
"h = img.shape[0]\n",
100100
"w = img.shape[1]\n",
101101
"\n",
@@ -105,8 +105,10 @@
105105
"ax.set_axis_off()\n",
106106
"ax.imshow(img)\n",
107107
"\n",
108-
"for i, (y, x) in enumerate(centroids):\n",
109-
" ax.text(x, y, str(i + 1))\n",
108+
"for i, (x0, x1, y0, y1) in boundaries.items():\n",
109+
" centroid_x = (x0 + x1) // 2\n",
110+
" centroid_y = (y0 + y1) // 2\n",
111+
" ax.text(centroid_y, centroid_x, str(i))\n",
110112
"\n",
111113
"pc = PatchCollection(recs, color=\"red\", alpha=0.7, linewidth=1, facecolor=\"none\")\n",
112114
"ax.add_collection(pc)\n",
@@ -138,7 +140,7 @@
138140
"# Delete the individual movies created after the end of the analysis\n",
139141
"clean_up_data = False\n",
140142
"# List of the ids of the embryos that should be processed\n",
141-
"embryos = list(range(1, 15))\n",
143+
"embryos = list(range(1, 4))\n",
142144
"# Interval used to calculate VNC length\n",
143145
"vnc_length_interval = 10\n",
144146
"# Window to calculate VNC ROI (which is then used to calculate activity)\n",
@@ -154,7 +156,7 @@
154156
"\n",
155157
"# tif to individual movies\n",
156158
"print(\"Cropping individual movies\")\n",
157-
"slice_img.cut_movies(coords, img_path, embs_dest, embryos=embryos, overwrite=False)\n",
159+
"slice_img.cut_movies(boundaries, img_path, embs_dest, embryos=embryos, overwrite=False)\n",
158160
"\n",
159161
"# movies to length\n",
160162
"print(\"Calculating VNC lengths\")\n",

snazzy_processing/snazzy_processing/slice_img.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -118,15 +118,15 @@ def increase_bbox(coords, w, h, shape):
118118
w: int Number of pixels to increment in the bbox width (half each side)
119119
h: int Number of pixels to increment in the bbox height (half each side)
120120
"""
121-
new_coords = []
122-
for coord in coords:
121+
new_coords = {}
122+
for emb_id, coord in coords.items():
123123
x0, x1, y0, y1 = coord
124124
r, c = shape
125125
new_x0 = max(x0 - h // 2, 0)
126126
new_x1 = min(x1 + h // 2, r)
127127
new_y0 = max(y0 - w // 2, 0)
128128
new_y1 = min(y1 + w // 2, c)
129-
new_coords.append([new_x0, new_x1, new_y0, new_y1])
129+
new_coords[emb_id] = [new_x0, new_x1, new_y0, new_y1]
130130
return new_coords
131131

132132

@@ -139,7 +139,7 @@ def sort_by_grid_pos(extremes, n_cols):
139139
for i, (r0, r1, c0, c1) in enumerate(extremes)
140140
]
141141
# determine bin_size from the maximum column value
142-
bin_size = (max((c for (r, c, i) in centroids)) // n_cols) + 1
142+
bin_size = (max((c[1] for c in centroids)) // n_cols) + 1
143143

144144
bins = [[] for _ in range(n_cols)]
145145
# add centroids to their respective bins
@@ -150,12 +150,14 @@ def sort_by_grid_pos(extremes, n_cols):
150150

151151
# filter out possibly empty bins
152152
bins = [b for b in bins if len(b) > 0]
153+
153154
# sort bins by row value
154155
for b in bins:
155156
b.sort(key=lambda b: b[0])
157+
156158
# extracts each index
157159
indices = [b[2] for bin in bins for b in bin]
158-
return [extremes[i] for i in indices]
160+
return {k + 1: extremes[i] for k, i in enumerate(indices)}
159161

160162

161163
def filter_by_embryos(extremes, selected_embryos):
@@ -171,7 +173,6 @@ def read_mmap(mmap_path, num_frames=None):
171173

172174

173175
def create_tasks(extremes, channels, active_ch, dest, overwrite):
174-
""""""
175176
tasks = []
176177
for id, extreme in extremes.items():
177178
x0, x1, y0, y1 = extreme

snazzy_processing/tests/test_slice_img.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def test_can_mark_neighbors():
2222

2323

2424
def test_increase_bbox_does_not_mutate_coords():
25-
coords = [[30, 80, 30, 80], [100, 300, 50, 250]]
25+
coords = {1: [30, 80, 30, 80], 2: [100, 300, 50, 250]}
2626
orig_coords = coords.copy()
2727
shape = (300, 300)
2828

@@ -34,18 +34,18 @@ def test_increase_bbox_does_not_mutate_coords():
3434

3535
def test_increase_bbox_clips_to_shape():
3636
"""Adding padding should not result in negative or OutOfBounds indices."""
37-
coords = [[2, 200, 5, 490]]
38-
expected_coords = [[0, 210, 0, 500]]
37+
coords = {1: [2, 200, 5, 490]}
38+
expected_coords = {1: [0, 210, 0, 500]}
3939
shape = (300, 500)
4040

4141
actual = slice_img.increase_bbox(coords, w=20, h=20, shape=shape)
42-
assert actual[0] == expected_coords[0]
42+
assert actual == expected_coords
4343

4444

4545
def test_increase_bbox_when_resulting_bbox_inside():
46-
coords = [[10, 200, 50, 490]]
47-
expected_coords = [[0, 210, 40, 500]]
46+
coords = {1: [10, 200, 50, 490]}
47+
expected_coords = {1: [0, 210, 40, 500]}
4848
shape = (600, 600)
4949

5050
actual = slice_img.increase_bbox(coords, w=20, h=20, shape=shape)
51-
assert actual[0] == expected_coords[0]
51+
assert actual == expected_coords

snazzy_processing/tests/test_sort_by_grid_pos.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def shuffle_arr(arr):
2828

2929

3030
def test_sorts_a_full_grid(grid_3x3_bboxes):
31-
expected = grid_3x3_bboxes.copy()
31+
expected = {i + 1: coord for i, coord in enumerate(grid_3x3_bboxes)}
3232

3333
shuffle_arr(grid_3x3_bboxes)
3434

@@ -40,7 +40,7 @@ def test_sorts_a_full_grid(grid_3x3_bboxes):
4040
def test_sorts_a_grid_with_one_missing_embryo(grid_3x3_bboxes):
4141
"""Removed embryo that would take pos 9."""
4242
bboxes = grid_3x3_bboxes[:-1]
43-
expected = bboxes.copy()
43+
expected = {i + 1: coord for i, coord in enumerate(bboxes)}
4444

4545
shuffle_arr(bboxes)
4646

@@ -50,10 +50,10 @@ def test_sorts_a_grid_with_one_missing_embryo(grid_3x3_bboxes):
5050

5151
def test_sorts_a_grid_with_several_missing_embryos(grid_3x3_bboxes):
5252
"""Removed embryos at index 0, 5, 6, and 7."""
53-
keeping_idxs = [1, 2, 3, 4, 7]
53+
keeping_idxs = [1, 2, 3, 4, 8]
5454
bboxes = [grid_3x3_bboxes[i] for i in keeping_idxs]
5555

56-
expected = bboxes.copy()
56+
expected = {i + 1: coord for i, coord in enumerate(bboxes)}
5757

5858
shuffle_arr(bboxes)
5959

@@ -67,7 +67,7 @@ def test_sorts_a_grid_with_empty_column(grid_3x3_bboxes):
6767
pos = [0, 1, 2, 6, 7, 8]
6868
bboxes = [grid_3x3_bboxes[p] for p in pos]
6969

70-
expected = bboxes.copy()
70+
expected = {i + 1: coord for i, coord in enumerate(bboxes)}
7171

7272
shuffle_arr(bboxes)
7373

0 commit comments

Comments
 (0)