-
-
Notifications
You must be signed in to change notification settings - Fork 511
Expand file tree
/
Copy pathPredictionResults.vue
More file actions
256 lines (240 loc) · 7.22 KB
/
Copy pathPredictionResults.vue
File metadata and controls
256 lines (240 loc) · 7.22 KB
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
<template>
<v-card class="mb-4">
<OverlayLoader v-show="loading"/>
<v-card-title>Result</v-card-title>
<v-card-text class="text-center" v-if="inputData">
<v-row v-if="prediction && isClassification(predictionTask)">
<v-col
lg="8"
md="12"
sm="12"
xs="12"
v-if="
resultProbabilities && Object.keys(resultProbabilities).length > 0
"
>
<div>Probabilities</div>
<v-chart class="chart" :option="chartOptions" autoresize/>
</v-col>
<v-col lg="4">
<div class="mb-3">
<div>Prediction</div>
<div
class="text-h6"
:class="
!actual
? 'info--text text--darken-2'
: prediction === actualForDisplay
? 'success--text'
: 'error--text'
"
>
{{ prediction }}
</div>
</div>
<div>
<div class="mb-2">
<div>Actual <span v-show="actual && modified">(before modification)</span></div>
<div v-if="actual" class="text-h6">{{ actualForDisplay }}</div>
<div v-else>-</div>
</div>
<div class="caption">
<div v-if="targetFeature">target: {{ targetFeature }}</div>
<div v-if="model && model.threshold">threshold: {{ model.threshold }}</div>
</div>
</div>
</v-col>
</v-row>
<v-row>
</v-row>
<v-row v-if="prediction && predictionTask === ModelType.REGRESSION">
<v-col lg="4">
<div>Prediction</div>
<div class="text-h6 success--text">
{{ prediction | formatTwoDigits }}
</div>
</v-col>
<v-col lg="4">
<div>Actual <span v-show="actual && modified">(before modification)</span></div>
<div v-if="actual" class="text-h6">{{ actual | formatTwoDigits }}</div>
<div v-else>-</div>
</v-col>
<v-col lg="4">
<div>Difference</div>
<div v-if="actual" class="font-weight-light center-center">
{{ ((prediction - actual) / actual) * 100 | formatTwoDigits }} %
</div>
<div v-else>-</div>
</v-col>
</v-row>
<p v-if="!prediction && !errorMsg">No data yet</p>
<p v-if="errorMsg" class="error--text">
{{ errorMsg }}
</p>
</v-card-text>
</v-card>
</template>
<script lang="ts">
import {Component, Prop, Vue, Watch} from "vue-property-decorator";
import OverlayLoader from "@/components/OverlayLoader.vue";
import {api} from "@/api";
import ECharts from "vue-echarts";
import {use} from "echarts/core";
import {BarChart} from "echarts/charts";
import {CanvasRenderer} from "echarts/renderers";
import {GridComponent} from "echarts/components";
import {ModelDTO, ModelType} from "@/generated-sources";
import {isClassification} from "@/ml-utils";
use([CanvasRenderer, BarChart, GridComponent]);
Vue.component("v-chart", ECharts);
@Component({
components: {OverlayLoader}
})
export default class PredictionResults extends Vue {
@Prop({required: true}) model!: ModelDTO;
@Prop({required: true}) datasetId!: number;
@Prop({required: true}) predictionTask!: ModelType;
@Prop() targetFeature!: string;
@Prop() classificationLabels!: string[];
@Prop() inputData!: {[key: string]: string};
@Prop({default: false}) modified!: boolean;
prediction: string | number | undefined = "";
resultProbabilities: object = {};
loading: boolean = false;
errorMsg: string = "";
isClassification = isClassification;
ModelType = ModelType;
predCategoriesN = 10;
async mounted() {
await this.submitPrediction()
}
@Watch("inputData", {deep: true})
public async submitPrediction() {
if (Object.keys(this.inputData).length) {
try {
this.loading = true;
const predictionResult = (await api.predict(
this.model.id,
this.datasetId,
this.inputData
))
this.prediction = predictionResult.prediction;
this.$emit("result", this.prediction);
this.resultProbabilities = predictionResult.probabilities
// Sort the object by value - solution based on:
// https://stackoverflow.com/questions/55319092/sort-a-javascript-object-by-key-or-value-es6
this.resultProbabilities = Object.entries(this.resultProbabilities)
.sort(([, v1], [, v2]) => +v2 - +v1)
.reduce((r, [k, v]) => ({...r, [k]: v}), {});
this.errorMsg = "";
} catch (error) {
this.errorMsg = error.response.data.detail;
this.prediction = undefined;
} finally {
this.loading = false;
}
} else {
// reset
this.errorMsg = "";
this.prediction = undefined;
this.resultProbabilities = {};
}
}
get actual() {
if (this.targetFeature && !this.errorMsg) return this.inputData[this.targetFeature]
else return undefined
}
get actualForDisplay() {
if (this.actual) {
if (isNaN(parseInt(this.actual.toString()))) return this.actual;
else return this.classificationLabels[parseInt(this.actual.toString())];
} else return "";
}
/**
* Getting first n entries of sorted objects and sort alphabetically, aggregating for "Others" options
*
* @param obj object
* @param n number of entries to keep
* @private
*/
private firstNSortedByKey(obj, n) {
const numberExtraCategories=Object.keys(obj).length-n;
let filteredObject=Object.keys(obj)
.slice(0,n)
.sort()
.reduce(function(acc, current) {
acc[current] = obj[current]
return acc;
}, {});
if (numberExtraCategories > 0) {
const sumOthers = Object.values(obj).slice(n, -1).reduce((acc: any, val: any) => acc + val, 0);
filteredObject={[`Others (${numberExtraCategories})`] : sumOthers,...filteredObject };
}
return filteredObject;
}
get chartOptions() {
const results = this.firstNSortedByKey(this.resultProbabilities, this.predCategoriesN)
return {
xAxis: {
type: "value",
min: 0,
max: 1,
},
yAxis: {
type: "category",
data: Object.keys(results),
axisLabel: {
interval: 0,
}
},
series: [
{
type: "bar",
label: {
show: true,
position: "right",
formatter: (params) =>
params.value % 1 == 0
? params.value
: params.value.toFixed(2).toLocaleString(),
},
data: Object.values(results),
},
],
color: ["#0091EA"],
grid: {
width: "80%",
height: "80%",
top: "10%",
left: "10%",
right: "10%",
containLabel: true,
},
};
}
}
</script>
<style scoped>
div.center-center {
height: 32px;
display: flex;
align-items: center;
justify-content: center;
}
.chart {
height: 90%;
min-height: 100px;
width: 90%;
}
div.caption {
font-size: 11px !important;
line-height: 1rem !important;
}
#labels-container {
font-size: 10px;
margin-top: 20px;
}
.v-data-table tbody td {
font-size: 10px !important;
}
</style>