-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpotts.html
254 lines (236 loc) · 9.9 KB
/
potts.html
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
<html>
<head>
<title>Cellular Potts Model</title>
</head>
<body>
<script type="module">
/**
Cellular Potts model in Agent Script.
Cody Smith 2020
*/
import World from 'https://agentscript.org/src/World.js'
import Model from 'https://agentscript.org/src/Model.js'
import util from 'https://agentscript.org/src/util.js'
import TwoDraw from 'https://agentscript.org/src/TwoDraw.js'
import ColorMap from 'https://agentscript.org/src/ColorMap.js'
// import ColorMap from '../agentscript/src/ColorMap.js'
import Color from 'https://agentscript.org/src/Color.js'
util.toWindow({ ColorMap })
class PottsModel extends Model {
static defaultOptions() {
return {
T: 0.01, //Boltzman temperature
mu: 60, // constant for how strong to follow chemical gradient
lambda: 0.2, // strength of volume constraint
}
}
constructor(worldOptions = World.defaultOptions(48, 48)) {
super(worldOptions)
Object.assign(this, PottsModel.defaultOptions())
}
setup() {
this.cells = [{ followGradient: false }]
this.patches.ask(p => {
p.concentration = (p.x + p.y + 48 * 2) / 96
p.cellID = 0
})
this.addCell(-40, 10, 3)
this.addCell(-40, -10, 4)
this.addCell(-40, -40, 4)
// this.addCell(-50, 12, 9)
for (let y = -40; y < 40; y += 12) {
for (let x = -35; x < 35; x += 12) {
this.addCell(
x + Math.round(Math.random() * 10),
y,
3,
false
)
}
}
}
addCell(x, y, r = 10, followGradient = true) {
let id = this.cells.length
this.cells.push({
followGradient,
targetArea: 3.14 * r * r,
})
this.patches
.inRadius(this.patches.patchXY(x, y), r)
.ask(p => {
p.cellID = id
})
}
step() {
const T = this.T // Boltzmann temperature. What is this exactly?
const mu = this.mu // strength of chemical gradient following behavior
const steplets = 3000
console.time('step')
let nrg1 = this.calcEnergy()
for (let i = 0; i < steplets; i++) {
// do a lot of tries before drawing for speed
let p1 = this.patches.oneOf()
let p2 = p1.neighbors4.oneOf()
if (p1.cellID != p2.cellID) {
// for efficiancy only move if cellIDs differ
const oldP2CellID = p2.cellID
p2.cellID = p1.cellID
let nrg2 = this.calcEnergy()
let dNrg1 = nrg2 - nrg1
let chemoGradient =
p2.concentration - p1.concentration
if (!this.cells[p1.cellID].followGradient)
chemoGradient = 0
let dNrg2 = dNrg1 - mu * chemoGradient
let prob = Math.random() < Math.exp(-dNrg2 / T)
if (dNrg2 < 0 || prob) {
nrg1 = nrg2 // keep change
} else {
//
p2.cellID = oldP2CellID // undo change
}
// console.log(dNrg)
}
}
console.timeEnd('step')
}
calcEnergy() {
const lambda = this.lambda
let totalTerm1 = 0
this.patches.ask(p => {
let J = this.adhesion(p)
totalTerm1 += J
})
let totalTerm2 = 0
let areas = this.getAreas()
for (let i = 1; i < this.cells.length; i++) {
// iterate through cell types.
let vdelta = (areas[i] - this.cells[i].targetArea) ** 2
totalTerm2 += vdelta
}
let total = totalTerm1 + lambda * totalTerm2
return total
}
adhesion(p1) {
let total = 0
p1.neighbors4.ask(pnei => {
if (p1.cellID == pnei.cellID) total += 0.0
// kronicker delta part of equation
else if (p1.cellID == 0 || pnei.cellID == 0)
total += 0.08
// one is empty
else total += 0.4 // different cell types
})
return total
}
getAreas() {
const areas = []
this.patches.ask(p => {
if (!areas[p.cellID]) areas[p.cellID] = 0
areas[p.cellID]++
})
return areas
}
}
/**
DRAWING
*/
const patchColors = ColorMap.Jet
const cellColors = ColorMap.Bright16
/**
Run the model
*/
async function run() {
const model = new PottsModel()
window.model = model // debug
await model.startup()
model.setup()
const view = new TwoDraw(
model,
{
div: 'modelDiv',
patchSize: 6, // Helps me debug, can ignore.
},
{
patchesColor: p =>
p.cellID > 0
? cellColors[p.cellID % cellColors.length].pixel
: patchColors.scaleColor(p.concentration, 0, 2)
.pixel,
}
)
// This is where smoothing would be set but it didn't seem needed.
// view.setPatchesSmoothing(true)
await util.timeoutLoop(
() => {
model.step()
view.draw()
},
10000, // if you want "forever" use -1
15
)
}
run()
</script>
<div>
<h1>
Cellular Potts Model
</h1>
<h3>Cody Smith and Owen Densmore 2020</h3>
<br />
This simulation is intended to be a proof of concept. It is trying
to imitate this
<a href="https://www.youtube.com/watch?v=JnlULOjUhSQ">video</a> of a
neutrophil chasing a staph bacteria through a group of cells .
<br />
There are 2 kinds of cells. The moving ones are gradient following
cells (neutrophils). They are trying to follow the gradient
reperesented by the ranbow background. There are several smaller
cells in the way, which are ignoring the chemical gradient. The
neutropuil cells need to squeeze and shove around the other cells in
order to get to the upper left corner.
<br />
This is currently being run on a 96x96 cell grid. I had originally
intended to write this model for an Immunology Class at UNM taught
be Professor Melany Moses, in fall 2019. I did not have much time
that semester and finally found time during the great quarantine of
2020 to write it.
<br />
<h3>Actual running model</h3>
<div>
<i
>Note that this is really slow. Is there a higher
performance version of this algorithm?</i
>
</div>
<div id="modelDiv"></div>
<br />
<h3>Here is a spead up video of a run:</h3>
<br />
<iframe
width="560"
height="315"
src="https://www.youtube.com/embed/srK0nYuqAg4"
frameborder="0"
allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture"
allowfullscreen
></iframe>
<hr />
<ul>
<li>
It is based on the Wikipedia description of the metropolis
style Cellular Potts model found here,
<a href="https://en.wikipedia.org/wiki/Cellular_Potts_model"
>https://en.wikipedia.org/wiki/Cellular_Potts_model</a
>
</li>
<li>
The original video
<a href="https://www.youtube.com/watch?v=JnlULOjUhSQ"
>https://www.youtube.com/watch?v=JnlULOjUhSQ</a
>
</li>
</ul>
</div>
</body>
</html>