-
Notifications
You must be signed in to change notification settings - Fork 1
/
cellprocessing.py
215 lines (158 loc) · 6.24 KB
/
cellprocessing.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
"Module used to encapsulate some functions used in the cells module"
import cells
import numpy as np
from skimage import color
from skimage.util import img_as_int
from skimage.segmentation import mark_boundaries
def rotation_matrices(step):
""" returns a list of rotation matrixes over 180 deg
matrixes are transposed to use with 2 column point arrays (x,y),
multiplying after the array
TODO: optimize with np vectors
"""
result = []
ang = 0
while ang < 180:
sa = np.sin(ang / 180.0 * np.pi)
ca = np.cos(ang / 180.0 * np.pi)
# note .T, for column points
result.append(np.matrix([[ca, -sa], [sa, ca]]).T)
ang = ang + step
return result
def bounded_value(minval, maxval, currval):
""" returns the value or the extremes if outside
"""
if currval < minval:
return minval
elif currval > maxval:
return maxval
else:
return currval
def bounded_point(x0, x1, y0, y1, p):
tx, ty = p
tx = bounded_value(x0, x1, tx)
ty = bounded_value(y0, y1, ty)
return tx, ty
def bound_rectangle(points):
""" returns a tuple (x0,y0,x1,y1,width) of the bounding rectangle
points must be a N,2 array of x,y coords
"""
x0, y0 = np.amin(points, axis=0)
x1, y1 = np.amax(points, axis=0)
a = np.min([(x1 - x0), (y1 - y0)])
return x0, y0, x1, y1, a
def stats_format(params):
"""Returns the list of cell stats to be displayed on the report,
depending on the computation of the septum"""
result = []
result.append(('Area', 3))
result.append(('Perimeter', 3))
result.append(('Length', 3))
result.append(('Width', 3))
result.append(('Eccentricity', 3))
result.append(('Irregularity', 3))
result.append(('Neighbours', 0))
result.append(('Baseline', 3))
result.append(('Cell Median', 3))
result.append(('Membrane Median', 3))
result.append(('Cytoplasm Median', 3))
if params.find_septum or params.find_openseptum:
result.append(('Septum Median', 3))
result.append(("Fluor Ratio", 3))
result.append(("Fluor Ratio 75%", 3))
result.append(("Fluor Ratio 25%", 3))
result.append(("Fluor Ratio 10%", 3))
result.append(("Memb+Sept Median", 3))
if params.classify_cells:
result.append(("Cell Cycle Phase", 1))
return result
def overlay_cells(cells, image, colors):
"Overlay the edges of each individual cell in the provided image"
tmp = color.gray2rgb(image)
for k in cells.keys():
c = cells[k]
if c.selection_state == 1:
col = colors[c.color_i][:3]
for px in c.outline:
x, y = px
tmp[x, y] = col
if c.sept_mask is not None:
try:
x0, y0, x1, y1 = c.box
tmp[x0:x1+1, y0:y1+1] = mark_boundaries(tmp[x0:x1+1, y0:y1+1],
img_as_int(
c.sept_mask),
color=col)
except IndexError:
c.selection_state = -1
return tmp
def assign_cell_color(cell, cells, cell_colors, pixel_size):
""" assigns an index to cell.color that is different from the neighbours """
neighcols = []
for neigh in list(iter(cell.neighbours.keys())):
try:
col = cells[str(int(neigh))].color_i
if col not in neighcols:
neighcols.append(col)
except KeyError:
print("Neighbour already merged")
cell.color_i = int(cell.stats["Area"] / float(pixel_size) / float(pixel_size) % len(cell_colors)) # each cell has a preferred color
while len(neighcols) < len(cell_colors) and (cell.color_i in neighcols):
cell.color_i += 1
if cell.color_i >= len(cell_colors):
cell.color_i = 0
def update_neighbours(cells, oldlabel, newlabel):
""" updates the neighbour list when merging cells """
oc = cells[str(oldlabel)]
nc = cells[str(newlabel)]
for nei in oc.neighbours.iterkeys():
tc = cells[str(int(nei))]
inter = tc.neighbours[oldlabel]
del tc.neighbours[oldlabel]
if int(nei) != newlabel:
if newlabel in tc.neighbours:
tc.neighbours[newlabel] = tc.neighbours[newlabel] + inter
nc.neighbours[tc.label] = nc.neighbours[tc.label] + inter
else:
tc.neighbours[newlabel] = inter
nc.neighbours[tc.label] = inter
def check_merge(cell1, cell2, rotations, interface, mask, params):
if cell1.stats["Area"] <= 0 or cell2.stats["Area"] <= 0: # check if both cells exist
return False
# check if any cell is small enough for automatic merge
if cell1.stats["Area"] < params.cellprocessingparams.cell_force_merge_below or \
cell2.stats["Area"] < params.cellprocessingparams.cell_force_merge_below:
return True
# check if dividing cells
if params.cellprocessingparams.merge_dividing_cells and \
interface >= params.cellprocessingparams.merge_min_interface:
tmp = cells.Cell(0)
tmp.outline.extend(cell1.outline)
tmp.outline.extend(cell2.outline)
tmp.lines.extend(cell1.lines)
tmp.lines.extend(cell2.lines)
tmp.stats["Area"] = cell1.stats["Area"] + cell2.stats["Area"]
tmp.compute_axes(rotations, mask.shape, params.imageloaderparams.pixel_size)
tmpshort = tmp.stats["Width"]
maxshort = max(cell1.stats["Width"], cell2.stats["Width"])
if tmpshort <= maxshort * params.cellprocessingparams.merge_length_tolerance:
return True
else:
return False
else:
return False
def paint_cell(cell, image, newval):
""" paints the lines of the cell into the image """
for li in cell.lines:
y, x0, x1 = li
image[x0:x1 + 1, y] = newval
return image
def blocked_by_filter(cell, list_of_filters):
""" returns true if cell is blocked by any filter
[("stat", min, max), ("stat2", min, max)]
"""
for filt in list_of_filters:
val = cell.stats[filt[0]]
if (val < filt[1]) or (val > filt[2]):
return True
return False