-
Notifications
You must be signed in to change notification settings - Fork 3
/
histogram.js
238 lines (218 loc) · 7.64 KB
/
histogram.js
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
function createBinTooltip() {
const tip = d3.tip()
.attr('class', "d3-tip")
.style("color", "black")
.style("background-color", "lightgrey")
.style("padding", "6px")
.style("border-radius", "4px")
.style("font-size", "14px")
.offset([-10, 0])
.html(function(d) {
var num_posits = d.values.length
var values_string;
if (num_posits === 1) {
values_string = "value in bin"
}
else {
values_string = "values in bin"
}
return String(num_posits) + " " + values_string
});
svg_viz_container.call(tip);
binMouseInteractionHelper(svg_viz_container.selectAll('.positiveHistogramBar'), tip);
binMouseInteractionHelper(svg_viz_container.selectAll('.negativeHistogramBar'), tip);
}
function binMouseInteractionHelper(nodes, tip) {
nodes.on('mouseover', function(d) {
tip.show(d, this);
})
nodes.on('mouseout', function(d) {
tip.hide();
})
}
function calculateBinSize(n) {
var num_posits = (1 << n);
var bin_size = dot_total_angle.DEGREES/(num_posits/2)
return bin_size;
}
function calculateNumBins(n) {
var bin_width = calculateBinSize(n);
// we need an extra bin, because we may have a dot that falls right on the end of the range
// of valid angles as defined by dot_total_angle.DEGREES
var num_bins = Math.ceil(dot_total_angle.DEGREES/bin_width) + 1;
return num_bins;
}
/**
* @brief Calculate the bins for drawing posit distribution around the circle
* The number/width of bins is calculated such that for ordinal, there is one posit in each bin.
* Furthermore, bins are centered on a point, in ordinal, so the angles are a little tricky. All
* bins are essentially shifted a bin_width/2 backwards except for the zero bin which is half
* the size
*/
function getBinning(n, es, posits, sign) {
var dThetas;
if (sign === psign.POSITIVE) {
dThetas = calculateDTheta(n, posits.map((p)=>p.value));
}
else {
dThetas = calculateDTheta(n, posits.map((p)=>p.value));
}
var bin_width = calculateBinSize(n);
var num_bins = calculateNumBins(n);
var bin_counts = [];
for (var i = 0; i < num_bins; i++) {
bin_counts.push({
count: 0,
values: []
})
}
for (var i = 0; i < dThetas.length; i++) {
var bin_index;
// check if we could be in the 0th bin. This one is tricky, because it's half the size
if (dThetas[i] < bin_width/2) {
bin_index = 0;
}
else {
// 1 and 2 in half index map to bin 1,
// 3 and 4 to bin 2, so on
var half_index = Math.floor(dThetas[i]/(bin_width/2));
bin_index = Math.floor((half_index + 1)/2)
}
bin_counts[bin_index].count += 1;
bin_counts[bin_index].values.push(posits[i])
}
return bin_counts;
}
function drawHistogram(x_center, y_center, radius, n, es, posits) {
drawPositivePosits(x_center, y_center, radius, n, es, posits.pos)
drawNegativePosits(x_center, y_center, radius, n, es, posits.neg)
}
function drawPositivePosits(x_center, y_center, radius, n, es, posits) {
var bin_counts = getBinning(n, es, posits, psign.POSITIVE)
var bin_width = calculateBinSize(n)
var max = d3.max(bin_counts, d => d.count);
if (max < 5) {
max = 5;
}
var barScale = d3.scaleLinear()
.domain([0, max])
.range([radius + 95, radius + 150]);
var arc = d3.arc()
.startAngle(function(d,i) {
// if this is the start arc, it needs to be half the size and start at 0
// (which is 180 in degrees)
if (i === 0) {
return Math.PI
}
else {
var bin_start = 180 - ((i * bin_width) - bin_width/2)
// convert to radians
return bin_start * (Math.PI/180)
}
})
.endAngle(function(d,i) {
// if this is the start arc, it needs to be half the size and start at 0
if (i === 0) {
if (n === 8) {
return (180 - bin_width/2) * (Math.PI/180) - 0.01
}
else {
return (180 - bin_width/2) * (Math.PI/180)
}
}
else {
var bin_end = 180 - (((i + 1) * bin_width) - bin_width/2)
// if the bin_end would go past the start of the circle, bump it up
if (bin_end < 0) {
return 0
}
else {
return bin_end * (Math.PI/180)
}
}
})
.innerRadius(radius + 90)
.outerRadius(function(d) {
if (d.count === 0) {
return radius + 90
}
else {
return barScale(+d.count)
}
})
.padAngle(0.01);
var segments = svg_viz_container.selectAll(".positiveHistogramBar").data(bin_counts)
segments.enter().append("path")
.attr("class", "positiveHistogramBar")
.attr("d", arc)
.attr('transform', "translate(" + x_center +"," + y_center + ")");
segments
.attr("d", arc)
.attr('transform', "translate(" + x_center +"," + y_center + ")");
segments.exit().remove();
}
function drawNegativePosits(x_center, y_center, radius, n, es, posits) {
var bin_counts = getBinning(n, es, posits, psign.NEGATIVE)
var bin_width = calculateBinSize(n)
var bin_width = calculateBinSize(n)
var max = d3.max(bin_counts, d => d.count);
if (max < 5) {
max = 5;
}
var barScale = d3.scaleLinear()
.domain([0, max])
.range([radius + 95, radius + 150]);
var arc = d3.arc()
.startAngle(function(d,i) {
// if this is the start arc, it needs to be half the size and start at 0
// (which is 180 degrees)
if (i === 0) {
return Math.PI
}
else {
var bin_start = 180 + ((i * bin_width) - bin_width/2)
// convert to radians
return bin_start * (Math.PI/180)
}
})
.endAngle(function(d,i) {
// if this is the start arc, it needs to be half the size and start at 0
if (i === 0) {
if (n === 8) {
return (180 + bin_width/2) * (Math.PI/180) + 0.01
}
else {
return (180 + bin_width/2) * (Math.PI/180)
}
}
else {
var bin_end = 180 + (((i + 1) * bin_width) - bin_width/2)
// if bin_end would go past the end of the circle, bump it back
if (bin_end > 360) {
return 2*Math.PI
}
else {
return bin_end * (Math.PI/180)
}
}
})
.innerRadius(radius + 90)
.outerRadius(function(d) {
if (d.count === 0) {
return radius + 90
}
else {
return barScale(+d.count)
}
})
.padAngle(0.01);
var segments = svg_viz_container.selectAll(".negativeHistogramBar").data(bin_counts)
segments.enter().append("path")
.attr("class", "negativeHistogramBar")
.attr("d", arc)
.attr('transform', "translate(" + x_center +"," + y_center + ")");
segments
.attr('transform', "translate(" + x_center +"," + y_center + ")")
.attr("d", arc)
segments.exit().remove();
}