File size: 9,431 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

// Space out the shapes a bit
shapeParams.forEach((d) => (d.startX = d.startX * 1.1));

// How to draw the background boxes, which will be styled later
const classifierBgPathTop = "M 420 150 H 0 V 0 H 420 V 150";
const classifierBgPathBottom = "M 420 300 H 0 V 0 H 420 V 300";

const toDropdownValueStringDict = {
    shape_name: "circles, triangles, or rectangles",
    pointiness: "pointy shapes or round shapes",
    size: "small shapes or big shapes",
};

const toShortValueStringDict = {
    shape_name: "circles, triangles, or rectangles",
    pointiness: "pointy or round",
    size: "small or big",
};

const toDropdownValueRoundingStringDict = {
    true: "with our best guess",
    false: 'as "other"',
};

const toPropertyStringDict = {
    pointy: "pointy shapes",
    round: "round shapes",
    small: "small shapes",
    large: "big shapes",
    circle: "circles",
    triangle: "triangles",
    rect: "rectangles",
};

function toOriginalString(inputString) {
    for (const [key, value] of Object.entries(toPropertyStringDict)) {
        if (inputString == value) {
            return key;
        }
    }
}

function toPropertyString(inputProperty, isRounding = true) {
    if (!isRounding && inputProperty.startsWith("rt_")) {
        return "others";
    }
    return toPropertyStringDict[inputProperty.replace("rt_", "")];
}

// Dictionary mapping div name to classifier results and summary sentences
var allResults = {};
var summaries = {};

function toBool(inputString) {
    if (inputString == "true") {
        return true;
    }
    return false;
}
function updateResults() {
    allResults["default-classifier"] = calculateResults();
    allResults["second-classifier"] = calculateResults(
        "shape_name",
        toBool(
            document.getElementById("second-classifier-select-rounding").value
        )
    );

    allResults["final-classifier"] = calculateResults(
        document.getElementById("final-classifier-select-category").value,
        toBool(
            document.getElementById("final-classifier-select-rounding").value
        )
    );

    allResults["conclusion"] = calculateResults(
        document.getElementById("conclusion-select-category").value,
        true
    );

    updateSummaries();
    updateSecondInterfaceImages();
}

// Text summaries are written by hand for simplicity, and keyed simply by
// a string of the form "[category]:[useGuess]" (or simply "none").
// These are hashed in the same way as the results, by div name.
function updateSummaries() {
    summaries["default-classifier"] = getPerformanceSummary("none");
    summaries["second-classifier"] = getPerformanceSummary(
        "shape_name:" +
            document.getElementById("second-classifier-select-rounding").value
    );

    summaries["final-classifier"] = getPerformanceSummary(
        document.getElementById("final-classifier-select-category").value +
            ":" +
            document.getElementById("final-classifier-select-rounding").value
    );

    summaries["conclusion"] = getPerformanceSummary(
        document.getElementById("conclusion-select-category").value + ":" + true
    );
}

// Yes, these background colors are hardcoded in,
// no, this is not good design, this is just how it happened.
function getPerformanceSummary(key) {
    allSummaries = {
        "shape_name:true":
            '<mark style="background-color: rgb(206, 234, 135);" class="well">well</mark> on circles, <mark style="background-color: rgb(244, 123, 74);" class="terribly">terribly</mark> on triangles, and <mark style="background-color: rgb(173, 220, 114);" class="best">best</mark> on rectangles',
        "shape_name:false":
            '<mark style="background-color: rgb(251, 163, 94);" class="poorly">poorly</mark> on circles, <mark style="background-color: rgb(155, 212, 108);" class="best">best</mark> on triangles and rectangles, and <mark style="background-color: rgb(252, 244, 171);" class="fine">fine</mark> on other shapes',
        "pointiness:true":
            '<mark style="background-color: rgb(184, 225, 119);" class="better">better</mark> on pointy shapes and <mark style="background-color: rgb(254, 206, 125);" class="worse">worse</mark> on round shapes',
        "pointiness:false":
            '<mark style="background-color: rgb(140, 205, 104);" class="best">best</mark> on pointy shapes, <mark style="background-color: rgb(243, 248, 171);" class="fine">fine</mark> on round shapes, and <mark style="background-color: rgb(253, 190, 111);" class="poorly">poorly</mark> on other shapes',
        "size:true":
            '<mark style="background-color: rgb(206, 234, 135);" class="better">better</mark> on small shapes, <mark style="background-color: rgb(254, 232, 154);" class="worse">worse</mark> on big shapes',
        "size:false":
            '<mark style="background-color: rgb(254, 215, 135);" class="poorly">poorly</mark> on small shapes, <mark style="background-color: rgb(165, 0, 38); color: #FFCCD8;" class="terribly">terribly</mark> on big shapes, and <mark style="background-color: rgb(110, 192, 99);" class="best">best</mark> on other shapes',
        "none:true":
            '<mark style="background-color: rgb(246, 248, 173);" class="fine">fine</mark> on all shapes',
        "none:false":
            '<mark style="background-color: rgb(246, 248, 173);" class="fine">fine</mark> on all shapes',
        none: '<mark style="background-color: rgb(246, 248, 173);" class="fine">fine</mark> on all shapes',
    };

    return "The Is-Shaded Classifier performs " + allSummaries[key] + ".";
}

// On the second-classifier dropdown, update the "task interface" image.
function updateSecondInterfaceImages() {
    d3.select(".second-interface").html(function () {
        if (
            !document.getElementById("second-classifier-select-rounding").value
        ) {
            return;
        }
        var imgPath =
            "img/interface_shape_name_" +
            document.getElementById("second-classifier-select-rounding").value;
        return (
            '<img src="' +
            imgPath +
            '.png" alt="" class="interface-image" srcset="' +
            imgPath +
            '.svg"></img>'
        );
    });
}

// Calculate results given input parameters
function calculateResults(property = "none", useGuess = false) {
    switch (property) {
        case "none":
            var nAccurate = shapeParams.filter(
                (shape) => shape.correctness == "correct"
            ).length;
            var totalShapes = shapeParams.length;

            var results = [
                {
                    object: "shape",
                    n: totalShapes,
                    "n correct": nAccurate,
                    accuracy: (nAccurate / totalShapes).toFixed(3),
                    rawCategoryName: "none",
                },
            ];

            return results;
        case "pointiness":
            categories = ["pointy", "round"];
            break;
        case "size":
            categories = ["small", "large"];
            break;
        case "shape_name":
            categories = ["circle", "triangle", "rect"];
            break;
    }

    var results = [];
    if (useGuess == true) {
        // Rounding shapes to categories

        for (const category of categories) {
            // Get shapes that are either in this category (e.g. rectangle) or "rounds to" this category (e.g. rt_rectangle)
            var theseShapes = shapeParams.filter(
                (shape) =>
                    shape[property] == category ||
                    shape[property] == "rt_" + category
            );
            var nAccurate = theseShapes.filter(
                (shape) => shape.correctness == "correct"
            ).length;
            var totalShapes = theseShapes.length;

            results.push({
                object: toPropertyString(category),
                n: totalShapes,
                "n correct": nAccurate,
                accuracy: (nAccurate / totalShapes).toFixed(3),
                rawCategoryName: category,
            });
        }
    } else {
        // Not rounding, treat everything else as "other"

        // First go through existing categories
        for (const category of categories) {
            var theseShapes = shapeParams.filter(
                (shape) => shape[property] == category
            );
            var nAccurate = theseShapes.filter(
                (shape) => shape.correctness == "correct"
            ).length;
            var totalShapes = theseShapes.length;
            results.push({
                object: toPropertyString(category),
                n: totalShapes,
                "n correct": nAccurate,
                accuracy: (nAccurate / totalShapes).toFixed(3),
                rawCategoryName: category,
            });
        }

        // Now get "other" shapes
        var theseShapes = shapeParams.filter(
            (shape) => !categories.includes(shape[property])
        );
        var nAccurate = theseShapes.filter(
            (shape) => shape.correctness == "correct"
        ).length;
        var totalShapes = theseShapes.length;
        results.push({
            object: "other shapes",
            n: totalShapes,
            "n correct": nAccurate,
            accuracy: (nAccurate / totalShapes).toFixed(3),
            rawCategoryName: "other",
        });
    }

    return results;
}