File size: 11,319 Bytes
bd9ac5f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
!(async function(){
  var isLock = false

  var csvstr = await (await fetch('rotated-accuracy.csv')).text()
  var allData = d3.csvParse(csvstr)
    .filter(d => {
      d.slug = [d.dataset_size, d.aVal, d.minority_percent].join(' ')

      d.accuracy_orig = (+d.accuracy_test_data_1     + +d.accuracy_test_data_7)/2000
      d.accuracy_rot  = (+d.accuracy_test_data_1_rot + +d.accuracy_test_data_7_rot)/2000
      d.accuracy_dif = d.accuracy_orig - d.accuracy_rot

      return d.accuracy_orig > 0 && d.accuracy_rot > 0
    })

  var data = d3.nestBy(allData, d => d.slug)
  data.forEach(slug => {
    slug.accuracy_orig = d3.median(slug, d => d.accuracy_orig)
    slug.accuracy_rot = d3.median(slug, d => d.accuracy_rot)
    slug.accuracy_dif = slug.accuracy_orig - slug.accuracy_rot

    slug.dataset_size = +slug[0].dataset_size
    slug.aVal = +slug[0].aVal
    slug.minority_percent = +slug[0].minority_percent
  })

  // d3.nestBy(data, d => d.length).forEach(d => {
  //   console.log(d.key, d.length)
  // })

  var byMetrics = 'dataset_size aVal minority_percent'
    .split(' ')
    .map(metricStr => {
      var byMetric = d3.nestBy(data, d => d[metricStr])
      byMetric.forEach(d => d.key = +d.key)
      byMetric = _.sortBy(byMetric, d => d.key)
      byMetric.forEach((d, i) => {
        d.metricIndex = i
        d.forEach(e => e['metric_' + metricStr] = d)
      })

      byMetric.forEach((d, i) => {
        if (metricStr == 'dataset_size') d.label = i % 2 == 0 ? '' : d3.format(',')(d.key)
        if (metricStr == 'aVal') d.label = ''
        if (metricStr == 'minority_percent') d.label = i % 2 ? '' : d3.format('.0%')(d.key)
      })

      byMetric.active = byMetric[5]
      byMetric.metricStr = metricStr
      byMetric.label = {dataset_size: 'Training Points', aVal: 'Less Privacy', minority_percent: 'Percent Rotated In Training Data'}[metricStr]

      return byMetric
    })


  // Heat map
  !(function(){
    var sel = d3.select('.rotated-accuracy-heatmap').html('')
      .st({width: 1100, position: 'relative', left: (850 - 1100)/2})
      .at({role: 'graphics-document', 'aria-label': `Faceted MNIST models by the percent of rotated digits in training data. Heatmaps show how privacy and training data change accuracy on rotated and original digits.`})

    sel.append('div.chart-title').text('Percentage of training data rotated 90° →')

    sel.appendMany('div', byMetrics[2])//.filter((d, i) => i % 2 == 0))
      .st({display: 'inline-block'})
      .each(drawHeatmap)
  })()
  function drawHeatmap(sizeData, chartIndex){

    var s = 8
    var n = 11

    var c = d3.conventions({
      sel: d3.select(this),
      width: s*n,
      height: s*n,
      margin: {left: 5, right: 5, top: 30, bottom: 50},
    })

    c.svg.append('rect').at({width: c.width, height: c.height, fillOpacity: 0})

    c.svg.append('text.chart-title')
      .text(d3.format('.0%')(sizeData.key)).at({dy: -4, textAnchor: 'middle', x: c.width/2})
      .st({fontWeight: 300})

    var linearScale = d3.scaleLinear().domain([0, .5]).clamp(1)
    var colorScale = d => d3.interpolatePlasma(linearScale(d))
    
    var pad = .5
    var dataSel = c.svg
      .on('mouseleave', () => isLock = false)
      .append('g').translate([.5, .5])
      .appendMany('g.accuracy-rect', sizeData)
      .translate(d => [
        s*d.metric_dataset_size.metricIndex, 
        s*(n - d.metric_aVal.metricIndex)
      ])
      .call(d3.attachTooltip)
      .on('mouseover', (d, i, node, isClickOverride) => {
        updateTooltip(d)

        if (isLock && !isClickOverride) return

        byMetrics[0].setActiveCol(d.metric_dataset_size)
        byMetrics[1].setActiveCol(d.metric_aVal)
        byMetrics[2].setActiveCol(d.metric_minority_percent)

        return d
      })
      .on('click', clickCb)
      .st({cursor: 'pointer'})



    dataSel.append('rect')
      .at({
        width: s - pad, 
        height: s - pad,
        fillOpacity: .1
      })
    
    // dataSel.append('rect')      
    //   .at({
    //     width:  d => Math.max(1, (s - pad)*(d.accuracy_orig - .5)*2), 
    //     height: d => Math.max(1, (s - pad)*(d.accuracy_rot - .5)*2),
    //   })
    sizeData.forEach(d => {
      d.y_orig = Math.max(0, (s - pad)*(d.accuracy_orig - .5)*2)
      d.y_rot  = Math.max(0, (s - pad)*(d.accuracy_rot - .5)*2)
    })

    dataSel.append('rect')
      .at({
        height: d => d.y_orig,
        y: d => s - d.y_orig,
        width: s/2,
        x: s/2,
        fill: 'purple',
      })
    dataSel.append('rect')
      .at({
        height: d => d.y_rot,
        y: d => s - d.y_rot,
        width: s/2,
        fill: 'orange',
      })

    sizeData.updateActiveRect = function(match){
      dataSel
        .classed('active', d => match == d)
        .filter(d => match == d)
        .raise()
    }

    if (chartIndex == 0){
      c.svg.append('g.x.axis').translate([10, c.height])
      c.svg.append('g.y.axis').translate([0, 5])

      util.addAxisLabel(c, 'Training Points →', 'Less Privacy →', 30, -15)
    }

    if (chartIndex == 8){
      c.svg.appendMany('g.axis', ['Original Digit Accuracy', 'Rotated Digit Accuracy'])
        .translate((d, i) => [c.width - 230*i - 230 -50, c.height + 30])
        .append('text.axis-label').text(d => d)
        .st({fontSize: 14})
        .parent()
        .appendMany('rect', (d, i) => d3.range(.2, 1.2, .2).map((v, j) => ({i, v, j})))
        .at({
          width: s/2,
          y: d => s - d.v*s - s,
          height: d => d.v*s,
          fill: d => ['purple', 'orange'][d.i],
          x: d => d.j*s*.75 - 35
        })
    }
  }

  // Metric barbell charts
  !(function(){
    var sel = d3.select('.rotated-accuracy').html('')
      .at({role: 'graphics-document', 'aria-label': `Barbell charts showing up privacy / data / percent underrepresented data all trade-off in complex ways.`})

    sel.appendMany('div', byMetrics)
      .st({display: 'inline-block', width: 300, marginRight: 10, marginBottom: 50, marginTop: 10})
      .each(drawMetricBarbell)
  })()
  function drawMetricBarbell(byMetric, byMetricIndex){
    var sel = d3.select(this)

    var c = d3.conventions({
      sel,
      height: 220,
      width: 220,
      margin: {bottom: 10, top: 5},
      layers: 's',
    })
    c.svg.append('rect').at({width: c.width, height: c.height, fillOpacity: 0})

    c.y.domain([.5, 1]).interpolate(d3.interpolateRound)
    c.x.domain([0, byMetric.length - 1]).clamp(1).interpolate(d3.interpolateRound)

    c.xAxis
      .tickValues(d3.range(byMetric.length))
      .tickFormat(i => byMetric[i].label)
    c.yAxis.ticks(5).tickFormat(d => d3.format('.0%')(d))

    d3.drawAxis(c)
    util.addAxisLabel(c, byMetric.label + ' →', byMetricIndex ? '' : 'Accuracy')
    util.ggPlotBg(c, false)

    c.svg.select('.x').raise()
    c.svg.selectAll('.axis').st({pointerEvents: 'none'})

    c.svg.append('defs').append('linearGradient#purple-to-orange')
      .at({x1: '0%', x2: '0%', y1: '0%', y2: '100%'})
      .append('stop').at({offset: '0%', 'stop-color': 'purple'}).parent()
      .append('stop').at({offset: '100%', 'stop-color': 'orange'})

    c.svg.append('defs').append('linearGradient#orange-to-purple')
      .at({x1: '0%', x2: '0%', y2: '0%', y1: '100%'})
      .append('stop').at({offset: '0%', 'stop-color': 'purple'}).parent()
      .append('stop').at({offset: '100%', 'stop-color': 'orange'})

    var colSel = c.svg.appendMany('g', byMetric)
      .translate(d => c.x(d.metricIndex) + .5, 0)
      .st({pointerEvents: 'none'})

    var pathSel = colSel.append('path')
      .at({stroke: 'url(#purple-to-orange)', strokeWidth: 1})

    var rectSel = colSel.append('rect')
      .at({width: 1, x: -.5})

    var origCircleSel = colSel.append('circle')
      .at({r: 3, fill: 'purple', stroke: '#000', strokeWidth: .5})

    var rotCircleSel = colSel.append('circle')
      .at({r: 3, fill: 'orange', stroke: '#000', strokeWidth: .5})

    function clampY(d){
      return d3.clamp(0, c.y(d), c.height + 3)
    }

    byMetric.updateActiveCol = function(){
      var findObj = {}
      byMetrics
        .filter(d => d != byMetric)
        .forEach(d => {
          findObj[d.metricStr] = d.active.key
        })

      byMetric.forEach(col => {
        col.active = _.find(col, findObj)
      })

      origCircleSel.at({cy: d => clampY(d.active.accuracy_orig)})
      rotCircleSel.at({cy: d => clampY(d.active.accuracy_rot)})

      // pathSel.at({
      //   d: d => 'M 0 ' + clampY(d.active.accuracy_orig) + ' L 1 ' + clampY(d.active.accuracy_rot)
      // })

      rectSel.at({
        y: d => Math.min(clampY(d.active.accuracy_orig), clampY(d.active.accuracy_rot)),
        height: d => Math.abs(clampY(d.active.accuracy_orig) - clampY(d.active.accuracy_rot)),
        fill: d => d.active.accuracy_orig > d.active.accuracy_rot ? 'url(#purple-to-orange)' : 'url(#orange-to-purple)'
      })
    }
    byMetric.updateActiveCol()


    c.svg
      .call(d3.attachTooltip)
      .st({cursor: 'pointer'})
      .on('mousemove', function(d, i, node, isClickOverride){
        var [mx] = d3.mouse(this)
        var metricIndex = Math.round(c.x.invert(mx))

        var prevActive = byMetric.active
        byMetric.active = byMetric[metricIndex]
        updateTooltip()
        byMetric.active = prevActive

        if (isLock && !isClickOverride) return
        byMetric.setActiveCol(byMetric[metricIndex])

        return byMetric[metricIndex]
      })
      .on('click', clickCb)
      .on('mouseexit', () => isLock = false)


    byMetric.setActiveCol = function(col){
      if (col) byMetric.active = col

      c.svg.selectAll('.x .tick')
        .classed('active', i => i == byMetric.active.metricIndex)

      colSel.classed('active', d => d == byMetric.active)

      if (col) renderActiveCol()
    }
    byMetric.setActiveCol()
  }

  function renderActiveCol(){
    byMetrics.forEach(d => {
      if (d.updateActiveCol) d.updateActiveCol()
    })

    var findObj = {}
    byMetrics.forEach(d => findObj[d.metricStr] = d.active.key)
    var match = _.find(data, findObj)

    byMetrics[2].forEach(d => {
      if (d.updateActiveRect) d.updateActiveRect(match)
    })
  }

  function updateTooltip(d){
    if (!d){
      var findObj = {}
      byMetrics.forEach(d => findObj[d.metricStr] = d.active.key)
      d = _.find(data, findObj)
    }

    var epsilon = Math.round(d[0].epsilon*100)/100
    ttSel.html(`
      <div>
        <b>${d3.format('.0%')(d.accuracy_orig)}</b> 
        accuracy on 
        <span style='padding: 2px; background: purple; color: #fff'>
          original digits
        </span>
      <div>
      <div>
        <b>${d3.format('.0%')(d.accuracy_rot)}</b> 
        accuracy on 
        <span style='padding: 2px; background: orange; color: #000'>
          rotated digits
        </span>
      <br>
      <br>
      <div>Training points: ${d3.format(',')(d.dataset_size)}</div>
      <div>Privacy: ${epsilon} ε</div>
      <div>Rotated in training data: ${d3.format('.0%')(d.minority_percent)} </div>

    `).st({width: 230})

    ttSel.classed('tooltip-footnote', 0)
  }

  function clickCb(d, i, node){
    var mFn = d3.select(this).on('mouseover') || d3.select(this).on('mousemove')

    var e = mFn.call(this, d, i, node, true)
    isLock = e == isLock ? null : e
  }


})()