-
Notifications
You must be signed in to change notification settings - Fork 12
/
MinicolumnCompetition.jsx
329 lines (282 loc) · 8.85 KB
/
MinicolumnCompetition.jsx
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
import React from 'react'
import * as d3 from 'd3'
import PropTypes from 'prop-types'
// FIXME: consolidate color handling
const ppColor = '#FDF542'
const diagramPadding = 40
const selectedColor = 'red'
const connectionColor = 'blue'
const inputColor = '#AAA'
function getGreenToRed(percent) {
let r, g;
percent = 100 - percent;
r = percent < 50 ? 255 : Math.floor(255 - (percent * 2 - 100) * 255 / 100);
g = percent > 50 ? 255 : Math.floor((percent * 2) * 255 / 100);
return rgbToHex(r, g, 0);
}
/* From http://stackoverflow.com/questions/5623838/rgb-to-hex-and-hex-to-rgb */
function rgbToHex(r, g, b) {
return ((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1);
}
class MinicolumnCompetition extends React.Component {
svgRef = React.createRef() // this will give you reference to HTML DOM element
// setup any time params change
componentDidUpdate(prevProps) {
this.update()
}
// setup on initial mount
componentDidMount() {
// Sets up the d3 diagram on an SVG element.
this.root = d3.select(`svg#${this.props.id}`)
.attr('width', this.props.diagramWidth)
.attr('height', this.props.diagramWidth / 2)
}
// handle setting up when params are set/changed
update() {
if (this.props.overlaps) {
this.renderInputSpace()
this.renderCompetition()
this.renderOverlapScore()
}
}
renderInputSpace() {
const g = this.root.select('.input-space')
// Split screen, this goes to the right
g.attr('transform', `translate(${this.props.diagramWidth / 2},0)`)
this.renderInput()
this.renderPotentialPools()
this.renderConnections()
}
renderInput() {
const diagramWidth = this.props.diagramWidth - diagramPadding * 2
const g = this.root.select('.input')
const cols = Math.floor(Math.sqrt(this.props.encoding.length))
const cellWidth = diagramWidth / cols / 2
function treatCells(cell) {
cell.attr('class', 'bit')
.attr('fill', d => {
return d === 1 ? inputColor : 'none'
})
.attr('stroke', 'darkgrey')
.attr('stroke-width', 0.5)
.attr('fill-opacity', 1)
.attr('x', (d, i) => {
return (i % cols) * cellWidth
})
.attr('y', (d, i) => {
return (Math.floor(i / cols)) * cellWidth
})
.attr('width', cellWidth)
.attr('height', cellWidth)
}
// Update
const rects = g.selectAll('rect').data(this.props.encoding)
treatCells(rects)
// Enter
const newRects = rects.enter().append('rect')
treatCells(newRects)
// Exit
rects.exit().remove()
}
renderPotentialPools() {
const diagramWidth = this.props.diagramWidth - diagramPadding * 2
const g = this.root.select('.potential-pool')
const cols = Math.floor(Math.sqrt(this.props.encoding.length))
const cellWidth = diagramWidth / cols / 2
function treatCells(cell) {
cell.attr('class', 'bit')
.attr('fill', ppColor)
.attr('stroke', 'none')
.attr('fill-opacity', 0.5)
.attr('x', (d, i) => {
return (d % cols) * cellWidth
})
.attr('y', (d, i) => {
return (Math.floor(d / cols)) * cellWidth
})
.attr('width', cellWidth)
.attr('height', cellWidth)
}
// Update
const rects = g.selectAll('rect').data(
this.props.potentialPools[this.props.selectedMinicolumn]
)
treatCells(rects)
// Enter
const newRects = rects.enter().append('rect')
treatCells(newRects)
// Exit
rects.exit().remove()
}
renderConnections() {
const diagramWidth = this.props.diagramWidth - diagramPadding * 2
const g = this.root.select('.connections')
const encoding = this.props.encoding
const cols = Math.floor(Math.sqrt(encoding.length))
const cellWidth = diagramWidth / cols / 2
const halfCellWidth = cellWidth / 2
const connectionThreshold = this.props.connectionThreshold
const selectedMinicolumn = this.props.selectedMinicolumn
const pools = this.props.potentialPools[selectedMinicolumn]
function treatCells(cell) {
cell.attr('class', 'bit')
.attr('r', cellWidth / 3)
.attr('cx', (d, i) => {
return (pools[i] % cols) * cellWidth + (halfCellWidth)
})
.attr('cy', (d, i) => {
return (Math.floor(pools[i] / cols)) * cellWidth + (halfCellWidth)
})
.attr('stroke', (d, i) => {
let stroke = 'none'
if (d > connectionThreshold) {
stroke = connectionColor
}
return stroke
})
.attr('fill', (d, i) => {
let fill = 'none'
if (d > connectionThreshold && encoding[pools[i]] === 1) {
fill = connectionColor
}
return fill
})
.attr('stroke-width', (d, i) => {
let width = 1
if (d > connectionThreshold && encoding[pools[i]] === 1) {
width = 0
}
return width
})
.attr('stroke-dasharray', [1, 2])
}
// Update
const circs = g.selectAll('circle')
.data(this.props.permanences[selectedMinicolumn])
treatCells(circs)
// Enter
const newRects = circs.enter().append('circle')
treatCells(newRects)
// Exit
circs.exit().remove()
}
renderCompetition() {
if (this.props.overlaps) {
this.renderMinicolumns()
this.renderSelectedMinicolumn()
}
}
renderMinicolumns() {
const diagramWidth = this.props.diagramWidth - diagramPadding * 2
const g = this.root.select('.minicolumns')
const cols = Math.floor(Math.sqrt(this.props.overlaps.length))
const cellWidth = diagramWidth / cols / 2
const showCompetition = this.props.showCompetition
const maxOverlap = Math.max(...this.props.overlaps.map(o => o.length))
const minOverlap = Math.min(...this.props.overlaps.map(o => o.length))
const colorScale = d3.scaleLinear([minOverlap, maxOverlap], [0, 100])
const winners = this.props.winners
function treatCells(cell) {
cell.attr('class', 'bit')
.attr('fill', (overlap, i) => {
return '#' + getGreenToRed(colorScale(overlap.length))
})
.attr('stroke', (d, i) => {
// FIXME: Refactor out winners into another group
if (showCompetition && winners.includes(i)) return 'black'
return 'darkgrey'
})
.attr('stroke-width', (d, i) => {
// FIXME: Refactor out winners into another group
if (showCompetition && (winners.includes(i))) return 1.5
return 0.5
})
.attr('fill-opacity', 0.5)
.attr('x', (d, i) => {
return (i % cols) * cellWidth
})
.attr('y', (d, i) => {
return (Math.floor(i / cols)) * cellWidth
})
.attr('width', cellWidth)
.attr('height', cellWidth)
.attr('data-index', (d, i) => i)
}
// Update
const rects = g.selectAll('rect')
.data(this.props.overlaps)
treatCells(rects)
// Enter
const newRects = rects.enter().append('rect')
treatCells(newRects)
// Exit
rects.exit().remove()
}
renderSelectedMinicolumn() {
const cols = Math.floor(Math.sqrt(this.props.overlaps.length))
const diagramWidth = this.props.diagramWidth - diagramPadding * 2
const circlePadding = 2
const cellWidth = diagramWidth / cols / 2
const halfCellWidth = cellWidth / 2
const radius = halfCellWidth + circlePadding
const selectedMinicolumn = this.props.selectedMinicolumn
const x = (selectedMinicolumn % cols) * cellWidth + halfCellWidth
const y = (Math.floor(selectedMinicolumn / cols)) * cellWidth + halfCellWidth
const highlight = this.root.select('#highlightedMinicolumn')
highlight.attr('cx', x)
highlight.attr('cy', y)
highlight.attr('r', radius)
.attr('stroke', 'red')
.attr('fill', 'none')
.attr('stroke-width', 2)
}
renderOverlapScore() {
// FIXME: This is inefficient because we're already calculating the overlaps
// elsewhere.
const selectedMinicolumn = this.props.selectedMinicolumn
const encoding = this.props.encoding
const pools = this.props.potentialPools[selectedMinicolumn]
const overlapText = this.root.select('.overlap')
const connectionThreshold = this.props.connectionThreshold
overlapText.attr('transform',
`translate(${this.props.diagramWidth / 2 - 40},${this.props.diagramWidth / 4})`)
let overlap = 0
this.props.permanences[selectedMinicolumn].forEach((perm, i) => {
if (perm > connectionThreshold && encoding[pools[i]] === 1) overlap++
})
overlapText.html(overlap)
}
// Triggers consistently
selectMinicolumn(e) {
const selectedMinicolumn = Number(e.target.getAttribute('data-index'))
this.props.onUpdate(selectedMinicolumn)
}
render() {
return (
<svg id={this.props.id}
ref={this.svgRef}>
<g className="spatialPooler">
<g className="minicolumns" onClick={e => this.selectMinicolumn(e)}></g>
<g className="selectedMinicolumn">
<circle id="highlightedMinicolumn"></circle>
</g>
</g>
<text className="overlap"></text>
<g className="input-space">
<g className="input"></g>
<g className="potential-pool"></g>
<g className="connections"></g>
</g>
</svg>
)
}
}
// FIXME: This is all wrong at this point
MinicolumnCompetition.propTypes = {
id: PropTypes.string.isRequired,
diagramWidth: PropTypes.number,
minicolumnCount: PropTypes.number,
connectedPercent: PropTypes.number,
data: PropTypes.object,
}
export default MinicolumnCompetition