Skip to content

Commit 39d5539

Browse files
author
minjk-bl
committed
ML > Add data schema
1 parent 0a591b8 commit 39d5539

File tree

4 files changed

+122
-17
lines changed

4 files changed

+122
-17
lines changed

html/m_ml/addData.html

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<body>
2+
<div class="vp-grid-box">
3+
<div class="vp-grid-border-box vp-grid-col-95">
4+
<label for="targetVariable" class="vp-orange-text">Target variable</label>
5+
<input type="text" class="vp-input vp-state" id="targetVariable" placeholder="Input target"/>
6+
<label for="predictData" class="vp-orange-text">Predict data</label>
7+
<input type="text" class="vp-input vp-state" id="predictData" placeholder="Select predict data" value="pred"/>
8+
<label for="colName" class="vp-orange-text">New column name</label>
9+
<input type="text" class="vp-input vp-state" id="colName" placeholder="Input column name" value="pred_result"/>
10+
</div>
11+
<div class="vp-grid-border-box vp-grid-col-95">
12+
<label for="allocateTo" class="vp-orange-text">Allocate to</label>
13+
<input type="text" class="vp-input vp-state" id="allocateTo" placeholder="New variable name" value="_vp"/>
14+
</div>
15+
</div>
16+
</body>

js/m_ml/addData.js

Lines changed: 98 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,45 +13,128 @@
1313
// [CLASS] Add data
1414
//============================================================================
1515
define([
16+
'text!vp_base/html/m_ml/addData.html!strip',
1617
'vp_base/js/com/com_util',
1718
'vp_base/js/com/com_Const',
1819
'vp_base/js/com/com_String',
1920
'vp_base/js/com/component/PopupComponent'
20-
], function(com_util, com_Const, com_String, PopupComponent) {
21+
], function(addDataHtml, com_util, com_Const, com_String, PopupComponent) {
2122

2223
/**
2324
* Add data
2425
*/
2526
class AddData extends PopupComponent {
2627
_init() {
2728
super._init();
28-
/** Write codes executed before rendering */
29+
30+
this.state = {
31+
targetVariable: '',
32+
predictData: 'pred',
33+
colName: 'pred_result',
34+
allocateTo: '_vp',
35+
...this.state
36+
}
2937
}
3038

3139
_bindEvent() {
3240
super._bindEvent();
33-
/** Implement binding events */
3441
var that = this;
35-
this.$target.on('click', function(evt) {
36-
var target = evt.target;
37-
if ($(that.wrapSelector()).find(target).length > 0) {
38-
// Sample : getDataList from Kernel
39-
vpKernel.getDataList().then(function(resultObj) {
40-
vpLog.display(VP_LOG_TYPE.DEVELOP, resultObj);
41-
}).catch(function(err) {
42-
vpLog.display(VP_LOG_TYPE.DEVELOP, err);
43-
});
42+
43+
}
44+
45+
templateForDataView() {
46+
;
47+
}
48+
49+
renderDataView() {
50+
super.renderDataView();
51+
52+
this.loadDataPage();
53+
$(this.wrapSelector('.vp-popup-dataview-box')).css('height', '300px');
54+
}
55+
56+
renderDataPage(renderedText, isHtml = true) {
57+
var tag = new com_String();
58+
tag.appendFormatLine('<div class="{0} vp-close-on-blur vp-scrollbar">', 'rendered_html'); // 'rendered_html' style from jupyter output area
59+
if (isHtml) {
60+
tag.appendLine(renderedText);
61+
} else {
62+
tag.appendFormatLine('<pre>{0}</pre>', renderedText);
63+
}
64+
tag.appendLine('</div>');
65+
$(this.wrapSelector('.vp-popup-dataview-box')).html(tag.toString());
66+
}
67+
68+
loadDataPage() {
69+
var that = this;
70+
var code = this.generateCode();
71+
// if not, get output of all data in selected pandasObject
72+
vpKernel.execute(code).then(function(resultObj) {
73+
let { msg } = resultObj;
74+
if (msg.content.data) {
75+
var htmlText = String(msg.content.data["text/html"]);
76+
var codeText = String(msg.content.data["text/plain"]);
77+
if (htmlText != 'undefined') {
78+
that.renderDataPage(htmlText);
79+
} else if (codeText != 'undefined') {
80+
// plain text as code
81+
that.renderDataPage(codeText, false);
82+
} else {
83+
that.renderDataPage('');
84+
}
85+
} else {
86+
var errorContent = new com_String();
87+
if (msg.content.ename) {
88+
errorContent.appendFormatLine('<div class="{0}">', 'vp-popup-data-error-box');
89+
errorContent.appendLine('<i class="fa fa-exclamation-triangle"></i>');
90+
errorContent.appendFormatLine('<label class="{0}">{1}</label>',
91+
'vp-popup-data-error-box-title', msg.content.ename);
92+
if (msg.content.evalue) {
93+
// errorContent.appendLine('<br/>');
94+
errorContent.appendFormatLine('<pre>{0}</pre>', msg.content.evalue.split('\\n').join('<br/>'));
95+
}
96+
errorContent.appendLine('</div>');
97+
}
98+
that.renderDataPage(errorContent);
4499
}
100+
}).catch(function(resultObj) {
101+
let { msg } = resultObj;
102+
var errorContent = new com_String();
103+
if (msg.content.ename) {
104+
errorContent.appendFormatLine('<div class="{0}">', 'vp-popup-data-error-box');
105+
errorContent.appendLine('<i class="fa fa-exclamation-triangle"></i>');
106+
errorContent.appendFormatLine('<label class="{0}">{1}</label>',
107+
'vp-popup-data-error-box-title', msg.content.ename);
108+
if (msg.content.evalue) {
109+
// errorContent.appendLine('<br/>');
110+
errorContent.appendFormatLine('<pre>{0}</pre>', msg.content.evalue.split('\\n').join('<br/>'));
111+
}
112+
errorContent.appendLine('</div>');
113+
}
114+
that.renderDataPage(errorContent);
45115
});
46116
}
47117

48118
templateForBody() {
49-
/** Implement generating template */
50-
return 'This is sample.';
119+
let page = $(addDataHtml);
120+
121+
122+
return page;
51123
}
52124

53125
generateCode() {
54-
return "print('sample code')";
126+
let { targetVariable, predictData, colName, allocateTo } = this.state;
127+
let code = new com_String();
128+
129+
if (targetVariable != '' && targetVariable != allocateTo) {
130+
code.appendFormatLine('{0} = {1}.copy()', allocateTo, targetVariable);
131+
}
132+
code.appendFormat("{0}['{1}'] = {2}", allocateTo, colName, predictData);
133+
if (allocateTo && allocateTo != '') {
134+
code.appendLine();
135+
code.append(allocateTo);
136+
}
137+
return code.toString();
55138
}
56139

57140
}

js/m_ml/dataSplit.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ define([
4646
var that = this;
4747

4848
// import library
49-
$(this.wrapSelector('#vp_libaryImport')).on('click', function() {
49+
$(this.wrapSelector('#vp_importLibrary')).on('click', function() {
5050
com_interface.insertCell('code', 'from sklearn.model_selection import train_test_split');
5151
});
5252
}

js/m_ml/modelSelection.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,11 +141,17 @@ define([
141141
* model.fit(X_train, y_train)
142142
*/
143143
let code = new com_String();
144-
let { model, allocateTo } = this.state;
144+
let { model, featureData, targetData, allocateTo } = this.state;
145145
let config = this.modelConfig[model];
146146
code.appendLine(config.import);
147147
code.appendLine();
148148
code.appendFormat('{0} = {1}', allocateTo, config.code);
149+
150+
// fit
151+
code.appendLine();
152+
code.appendLine('%%time');
153+
code.appendLine('# Model fitting');
154+
code.appendFormat('{0}.fit({1}, {2})', allocateTo, featureData, targetData);
149155
return code.toString();
150156
}
151157

0 commit comments

Comments
 (0)