forked from waynebhayes/CellUniverse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcell.py
366 lines (295 loc) · 12.7 KB
/
cell.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
# -*- coding: utf-8 -*-
"""
cellanneal.cell
~~~~~~~~~~~~~~~
This module contains the Cell class which stores the properties of cells and
related functions.
"""
from math import atan2, ceil, cos, floor, sin, sqrt
import time
import numpy as np
from skimage.draw import polygon
# from skimage.draw import circle, polygon
from drawing import draw_arc, draw_line, circle
from mathhelper import Rectangle, Vector
class Cell(object):
"""The Cell class stores information about a particular cell."""
_REQUIRED_CONFIG = []
def __init__(self, name):
self._name = name
@classmethod
def checkconfig(cls, config):
for required in cls._REQUIRED_CONFIG:
if required not in config:
raise ValueError(f'Invalid config: missing "{required}"')
@property
def name(self):
return self._name
class Bacilli(Cell):
"""The Bacilli class represents a bacilli bacterium."""
_REQUIRED_CONFIG = [
'bacilli.maxSpeed',
'bacilli.maxSpin',
'bacilli.minGrowth',
'bacilli.maxGrowth',
'bacilli.minWidth',
'bacilli.maxWidth',
'bacilli.minLength',
'bacilli.maxLength'
]
def __init__(self, name, x, y, width, length, rotation):
super().__init__(name)
self._position = Vector([x, y, 0])
self._width = width
self._length = length
self._rotation = rotation
self._needs_refresh = True
def _refresh(self):
# get the positions of the centers of the head and tail circles
direction = Vector([cos(self._rotation), sin(self._rotation), 0])
distance = (self._length - self._width)/2
displacement = distance*direction
self._head_center = self._position + displacement
self._tail_center = self._position - displacement
# get the positions of the corners of the bacilli box
side = Vector([-sin(self._rotation), cos(self._rotation), 0])
radius = self._width/2
self._head_right = self._head_center + radius*side
self._head_left = self._head_center - radius*side
self._tail_right = self._tail_center + radius*side
self._tail_left = self._tail_center - radius*side
# compute the region of interest
self._region = Rectangle(
floor(min(self._head_center.x, self._tail_center.x) - radius),
floor(min(self._head_center.y, self._tail_center.y) - radius),
ceil(max(self._head_center.x, self._tail_center.x) + radius) + 1,
ceil(max(self._head_center.y, self._tail_center.y) + radius) + 1)
self._needs_refresh = False
def draw(self, image, is_cell, greySyntheticImage, phaseContractImage):
"""Draws the cell by adding the given value to the image."""
if self._needs_refresh:
self._refresh()
width = self._region.right - self._region.left
height = self._region.bottom - self._region.top
mask = np.zeros((height, width), dtype=np.bool)
body_mask = polygon(
r=(self._head_left.y - self._region.top,
self._head_right.y - self._region.top,
self._tail_right.y - self._region.top,
self._tail_left.y - self._region.top),
c=(self._head_left.x - self._region.left,
self._head_right.x - self._region.left,
self._tail_right.x - self._region.left,
self._tail_left.x - self._region.left),
shape=mask.shape)
body_mask_up = polygon(
r=(self._head_left.y - self._region.top,
ceil((self._head_right.y + self._head_left.y) / 2) - self._region.top,
ceil((self._tail_right.y + self._tail_left.y) / 2) - self._region.top,
self._tail_left.y - self._region.top),
c=(self._head_left.x - self._region.left,
ceil((self._head_right.x + self._head_left.x) / 2) - self._region.left,
ceil((self._tail_right.x + self._tail_left.x) / 2) - self._region.left,
self._tail_left.x - self._region.left),
shape=mask.shape)
body_mask_middle = polygon(
r=(ceil((self._head_right.y + self._head_left.y * 2) / 3) - self._region.top,
ceil((self._head_right.y * 2 + self._head_left.y) / 3) - self._region.top,
ceil((self._tail_right.y * 2 + self._tail_left.y) / 3) - self._region.top,
ceil((self._tail_right.y + self._tail_left.y * 2) / 3) - self._region.top),
c=(ceil((self._head_right.x + self._head_left.x * 2) / 3) - self._region.left,
ceil((self._head_right.x * 2 + self._head_left.x) / 3) - self._region.left,
ceil((self._tail_right.x * 2 + self._tail_left.x) / 3) - self._region.left,
ceil((self._tail_right.x + self._tail_left.x * 2) / 3) - self._region.left),
shape=mask.shape)
head_mask = circle(
x=self._head_center.x - self._region.left,
y=self._head_center.y - self._region.top,
radius=self._width / 2,
shape=mask.shape)
tail_mask = circle(
x=self._tail_center.x - self._region.left,
y=self._tail_center.y - self._region.top,
radius=self._width / 2,
shape=mask.shape)
if greySyntheticImage:
if phaseContractImage:
if not is_cell:
mask[body_mask] = True
mask[head_mask] = True
mask[tail_mask] = True
image[self._region.top:self._region.bottom,
self._region.left:self._region.right][mask] = 0.39 # 0.39*255=100
else:
mask = np.zeros((height, width), dtype=np.bool)
mask[body_mask] = True
image[self._region.top:self._region.bottom,
self._region.left:self._region.right][mask] = 0.25 # 0.25*255=65
mask = np.zeros((height, width), dtype=np.bool)
mask[head_mask] = True
image[self._region.top:self._region.bottom,
self._region.left:self._region.right][mask] = 0.25
mask = np.zeros((height, width), dtype=np.bool)
mask[tail_mask] = True
image[self._region.top:self._region.bottom,
self._region.left:self._region.right][mask] = 0.25
mask = np.zeros((height, width), dtype=np.bool)
mask[body_mask_up] = True
image[self._region.top:self._region.bottom,
self._region.left:self._region.right][mask] = 0.63 # 0.63*255=160
mask = np.zeros((height, width), dtype=np.bool)
mask[body_mask_middle] = True
image[self._region.top:self._region.bottom,
self._region.left:self._region.right][mask] = 0.39 # 0.39*255=100
if not phaseContractImage:
mask[body_mask] = True
mask[head_mask] = True
mask[tail_mask] = True
if is_cell:
image[self._region.top:self._region.bottom,
self._region.left:self._region.right][mask] += -0.24
else:
image[self._region.top:self._region.bottom,
self._region.left:self._region.right][mask] += 0.24
else:
mask[body_mask] = True
mask[head_mask] = True
mask[tail_mask] = True
if is_cell:
image[self._region.top:self._region.bottom,
self._region.left:self._region.right][mask] += 1.0
else:
image[self._region.top:self._region.bottom,
self._region.left:self._region.right][mask] += -1.0
def drawoutline(self, image, color):
"""Draws the outline of the cell over a color image."""
if self._needs_refresh:
self._refresh()
draw_line(image, int(self._tail_left.x), int(self._tail_left.y),
int(self._head_left.x), int(self._head_left.y), color)
draw_line(image, int(self._tail_right.x), int(self._tail_right.y),
int(self._head_right.x), int(self._head_right.y), color)
r0 = self._head_right - self._head_center
r1 = self._head_left - self._head_center
t1 = atan2(r0.y, r0.x)
t0 = atan2(r1.y, r1.x)
draw_arc(image, self._head_center.x, self._head_center.y,
self._width/2, t0, t1, color)
r0 = self._tail_right - self._tail_center
r1 = self._tail_left - self._tail_center
t0 = atan2(r0.y, r0.x)
t1 = atan2(r1.y, r1.x)
draw_arc(image, self._tail_center.x, self._tail_center.y,
self._width/2, t0, t1, color)
def split(self, alpha):
"""Splits a cell into two cells with a ratio determined by alpha."""
if self._needs_refresh:
self._refresh()
direction = Vector([cos(self._rotation), sin(self._rotation), 0])
unit = self._length*direction
front = self._position + unit/2
back = self._position - unit/2
center = self._position + (0.5 - alpha)*unit
position1 = (front + center)/2
position2 = (center + back)/2
cell1 = Bacilli(
self._name + '0',
position1.x, position1.y,
self._width, self._length*alpha,
self._rotation)
cell2 = Bacilli(
self._name + '1',
position2.x, position2.y,
self._width, self._length*(1 - alpha),
self._rotation)
return cell1, cell2
def combine(self, cell):
"""Combines this cell with another cell."""
if self._needs_refresh:
self._refresh()
if cell._needs_refresh:
cell._refresh()
separation = self._position - cell._position
direction = separation/sqrt(separation@separation)
# get combined front
direction1 = Vector([cos(self._rotation), sin(self._rotation), 0])
distance1 = self._length - self._width
if direction1@direction >= 0:
head1 = self._position + distance1*direction1/2
else:
head1 = self._position - distance1*direction1/2
extent1 = head1 + self._width*direction/2
front = self._position + ((extent1 - self._position)@direction)*direction
# get combined back
direction2 = Vector([cos(cell._rotation), sin(cell._rotation), 0])
distance2 = cell._length - cell._width
if direction2@direction >= 0:
tail2 = cell._position - distance2*direction2/2
else:
tail2 = cell._position + distance2*direction2/2
extent2 = tail2 - cell._width*direction/2
back = cell._position + ((extent2 - cell._position)@direction)*direction
# create new cell
position = (front + back)/2
rotation = atan2(direction.y, direction.x)
width = (self._width + cell._width)/2
length = sqrt((front - back)@(front - back))
return Bacilli(
self._name[:-1],
position.x, position.y,
width, length,
rotation)
def __repr__(self):
return (f'Bacilli('
f'name="{self._name}", '
f'x={self._position.x}, y={self._position.y}, '
f'width={self._width}, length={self._length}, '
f'rotation={self._rotation})')
@property
def region(self):
if self._needs_refresh:
self._refresh()
return self._region
@property
def position(self):
return self._position.copy()
@property
def x(self):
return self._position.x
@x.setter
def x(self, value):
if value != self._position.x:
self._position.x = value
self._needs_refresh = True
@property
def y(self):
return self._position.y
@y.setter
def y(self, value):
if value != self._position.y:
self._position.y = value
self._needs_refresh = True
@property
def width(self):
return self._width
@width.setter
def width(self, value):
if value != self._width:
self._width = value
self._needs_refresh = True
@property
def length(self):
return self._length
@length.setter
def length(self, value):
if value != self._length:
self._length = value
self._needs_refresh = True
@property
def rotation(self):
return self._rotation
@rotation.setter
def rotation(self, value):
if value != self._rotation:
self._rotation = value
self._needs_refresh = True