-
-
Notifications
You must be signed in to change notification settings - Fork 118
Expand file tree
/
Copy pathClassification.js
More file actions
333 lines (295 loc) · 13.8 KB
/
Classification.js
File metadata and controls
333 lines (295 loc) · 13.8 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
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
/*
* Project Name : Visual Python
* Description : GUI-based Python code generator
* File Name : Classification.js
* Author : Black Logic
* Note : Model selection and fitting
* License : GNU GPLv3 with Visual Python special exception
* Date : 2022. 02. 07
* Change Date :
*/
//============================================================================
// [CLASS] Classification
//============================================================================
define([
__VP_TEXT_LOADER__('vp_base/html/m_ml/model.html'), // INTEGRATION: unified version of text loader
'vp_base/js/com/com_util',
'vp_base/js/com/com_interface',
'vp_base/js/com/com_String',
'vp_base/js/com/com_generatorV2',
'vp_base/data/m_ml/mlLibrary',
'vp_base/js/com/component/PopupComponent',
'vp_base/js/com/component/VarSelector2',
'vp_base/js/com/component/ModelEditor'
], function(msHtml, com_util, com_interface, com_String, com_generator, ML_LIBRARIES, PopupComponent, VarSelector2, ModelEditor) {
/**
* Classification
*/
class Classification extends PopupComponent {
_init() {
super._init();
this.config.sizeLevel = 2;
this.config.dataview = false;
this.state = {
// model creation
modelControlType: 'creation',
modelType: 'lg-rgs',
userOption: '',
featureData: 'X_train',
targetData: 'y_train',
allocateToCreation: 'model',
// model selection
model: '',
method: '',
...this.state
}
this.modelConfig = ML_LIBRARIES;
this.modelTypeList = {
'Classification': ['lg-rgs', 'bern-nb', 'mulnom-nb', 'gaus-nb', 'sv-clf', 'dt-clf', 'rf-clf', 'gbm-clf', 'xgb-clf', 'lgbm-clf', 'cb-clf'],
}
}
_bindEvent() {
super._bindEvent();
/** Implement binding events */
var that = this;
// select model control type
$(this.wrapSelector('#modelControlType')).on('change', function() {
let modelControlType = $(this).val();
// show/hide model box
$(that.wrapSelector('.vp-model-box')).hide();
$(that.wrapSelector(`.vp-model-box[data-type="${modelControlType}"]`)).show();
});
// select model type
$(this.wrapSelector('#modelType')).on('change', function() {
let modelType = $(this).val();
that.state.modelType = modelType;
$(that.wrapSelector('.vp-model-option-box')).html(that.templateForOption(modelType));
that.viewOption();
// show install button
if (that.modelConfig[modelType].install != undefined) {
$(that.wrapSelector('#vp_installLibrary')).show();
} else {
$(that.wrapSelector('#vp_installLibrary')).hide();
}
});
// install library
$(this.wrapSelector('#vp_installLibrary')).on('click', function() {
let config = that.modelConfig[that.state.modelType];
if (config && config.install != undefined) {
// insert install code
let installCode = config.install;
if (vpConfig.extensionType === 'lite') {
installCode = installCode.replace('!', '%');
}
com_interface.insertCell('code', installCode, true, 'Machine Learning > Classification');
}
});
// change model
$(this.wrapSelector('#model')).on('change', function() {
that.modelEditor.reload();
});
}
templateForBody() {
let page = $(msHtml);
let that = this;
// model control type
$(page).find('.vp-model-box').hide();
$(page).find(`.vp-model-box[data-type="${this.state.modelControlType}"]`).show();
//================================================================
// Model creation
//================================================================
// model types
let modelTypeTag = new com_String();
Object.keys(this.modelTypeList).forEach(modelCategory => {
let modelOptionTag = new com_String();
that.modelTypeList[modelCategory].forEach(opt => {
let optConfig = that.modelConfig[opt];
let selectedFlag = '';
if (opt == that.state.modelType) {
selectedFlag = 'selected';
}
modelOptionTag.appendFormatLine('<option value="{0}" {1}>{2}</option>',
opt, selectedFlag, optConfig.name);
})
modelTypeTag.appendFormatLine('<optgroup label="{0}">{1}</optgroup>',
modelCategory, modelOptionTag.toString());
});
$(page).find('#modelType').html(modelTypeTag.toString());
// show install button
if (this.modelConfig[this.state.modelType].install != undefined) {
$(page).find('#vp_installLibrary').show();
} else {
$(page).find('#vp_installLibrary').hide();
}
// render option page
$(page).find('.vp-model-option-box').html(this.templateForOption(this.state.modelType));
let varSelector = new VarSelector2(this.wrapSelector());
varSelector.setComponentID('featureData');
varSelector.addClass('vp-state vp-input');
varSelector.setValue(this.state.featureData);
$(page).find('#featureData').replaceWith(varSelector.toTagString());
varSelector = new VarSelector2(this.wrapSelector());
varSelector.setComponentID('targetData');
varSelector.addClass('vp-state vp-input');
varSelector.setValue(this.state.targetData);
$(page).find('#targetData').replaceWith(varSelector.toTagString());
//================================================================
// Model selection
//================================================================
// set model list
let modelOptionTag = new com_String();
vpKernel.getModelList('Classification').then(function(resultObj) {
let { result } = resultObj;
var modelList = JSON.parse(result);
modelList && modelList.forEach(model => {
let selectFlag = '';
if (model.varName == that.state.model) {
selectFlag = 'selected';
}
modelOptionTag.appendFormatLine('<option value="{0}" data-type="{1}" {2}>{3} ({4})</option>',
model.varName, model.varType, selectFlag, model.varName, model.varType);
});
$(page).find('#model').html(modelOptionTag.toString());
$(that.wrapSelector('#model')).html(modelOptionTag.toString());
if (!that.state.model || that.state.model == '') {
that.state.model = $(that.wrapSelector('#model')).val();
}
that.modelEditor.show();
});
//================================================================
// Load state
//================================================================
Object.keys(this.state).forEach(key => {
let tag = $(page).find('#' + key);
let tagName = $(tag).prop('tagName'); // returns with UpperCase
let value = that.state[key];
if (value == undefined) {
return;
}
switch(tagName) {
case 'INPUT':
let inputType = $(tag).prop('type');
if (inputType == 'text' || inputType == 'number' || inputType == 'hidden') {
$(tag).val(value);
break;
}
if (inputType == 'checkbox') {
$(tag).prop('checked', value);
break;
}
break;
case 'TEXTAREA':
case 'SELECT':
default:
$(tag).val(value);
break;
}
});
return page;
}
templateForOption(modelType) {
let config = this.modelConfig[modelType];
let state = this.state;
let optBox = new com_String();
// render tag
config.options.forEach(opt => {
optBox.appendFormatLine('<label for="{0}" title="{1}">{2}</label>'
, opt.name, opt.name, com_util.optionToLabel(opt.name));
let content = com_generator.renderContent(this, opt.component[0], opt, state);
optBox.appendLine(content[0].outerHTML);
});
// render user option
optBox.appendFormatLine('<label for="{0}">{1}</label>', 'userOption', 'User option');
optBox.appendFormatLine('<input type="text" class="vp-input vp-state" id="{0}" placeholder="{1}" value="{2}"/>',
'userOption', 'key=value, ...', this.state.userOption);
return optBox.toString();
}
viewOption() {
// SVC - kernel selection
if (this.state.modelType == 'sv-clf') {
let kernelType = this.state.kernel;
switch (kernelType) {
case undefined: // default = rbf
case '':
case 'rbf': // gamma
$(this.wrapSelector('label[for="gamma"]')).show();
$(this.wrapSelector('#gamma')).show();
// hide others
$(this.wrapSelector('label[for="degree"]')).hide();
$(this.wrapSelector('#degree')).hide();
$(this.wrapSelector('label[for="coef0"]')).hide();
$(this.wrapSelector('#coef0')).hide();
break;
case 'poly': // gamma / degree / coef0
$(this.wrapSelector('label[for="gamma"]')).show();
$(this.wrapSelector('#gamma')).show();
$(this.wrapSelector('label[for="degree"]')).show();
$(this.wrapSelector('#degree')).show();
$(this.wrapSelector('label[for="coef0"]')).show();
$(this.wrapSelector('#coef0')).show();
break;
case 'sigmoid': // gamma / coef0
$(this.wrapSelector('label[for="gamma"]')).show();
$(this.wrapSelector('#gamma')).show();
$(this.wrapSelector('label[for="coef0"]')).show();
$(this.wrapSelector('#coef0')).show();
// hide others
$(this.wrapSelector('label[for="degree"]')).hide();
$(this.wrapSelector('#degree')).hide();
break;
default:
// hide others
$(this.wrapSelector('label[for="gamma"]')).hide();
$(this.wrapSelector('#gamma')).hide();
$(this.wrapSelector('label[for="degree"]')).hide();
$(this.wrapSelector('#degree')).hide();
$(this.wrapSelector('label[for="coef0"]')).hide();
$(this.wrapSelector('#coef0')).hide();
break;
}
// Model Creation - SVC kernel selection
let that = this;
$(this.wrapSelector('#kernel')).off('change');
$(this.wrapSelector('#kernel')).change(function() {
that.state.kernel = $(this).val();
that.viewOption();
});
}
}
render() {
super.render();
// Model Creation - dynamically view options
this.viewOption();
// Model Editor
this.modelEditor = new ModelEditor(this, "model", "instanceEditor");
}
generateCode() {
let { modelControlType, modelType, userOption, allocateToCreation, model } = this.state;
let code = new com_String();
if (modelControlType == 'creation') {
/**
* Model Creation
* ---
* from module import model_function
* model = Model(key=value, ...)
*/
let config = this.modelConfig[modelType];
code.appendLine(config.import);
code.appendLine();
// model code
let modelCode = config.code;
modelCode = com_generator.vp_codeGenerator(this, config, this.state, (userOption != ''? ', ' + userOption : ''));
code.appendFormat('{0} = {1}', allocateToCreation, modelCode);
} else {
/**
* Model Selection
* ---
* ...
*/
code.append(this.modelEditor.getCode({'${model}': model}));
}
return code.toString();
}
}
return Classification;
});