Skip to content

Commit 2bd8b01

Browse files
author
minjk-bl
committed
Add function able to insert multiple code cells - for ML evaluation
1 parent beae73d commit 2bd8b01

File tree

4 files changed

+120
-23
lines changed

4 files changed

+120
-23
lines changed

js/board/BoardFrame.js

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -586,9 +586,22 @@ define([
586586
let prevNewLine = idx > 0?'\n':'';
587587
let indent = ' '.repeat((groupBlock.depth - rootBlockDepth) * indentCount);
588588
let thisBlockCode = groupBlock.popup.generateCode();
589-
// set indent to every line of thisblockcode
590-
thisBlockCode = thisBlockCode.replaceAll('\n', '\n' + indent);
591-
code.appendFormat('{0}{1}{2}', prevNewLine, indent, thisBlockCode);
589+
if (Array.isArray(thisBlockCode)) {
590+
for (let i = 0; i < thisBlockCode.length; i++) {
591+
thisBlockCode[i] = thisBlockCode[i].replaceAll('\n', '\n' + indent);
592+
}
593+
if (addcell) {
594+
// insert single cell using prev code
595+
com_interface.insertCell('code', code.toString(), execute, block.blockNumber);
596+
code = new com_String();
597+
// insert cells using this block code list
598+
com_interface.insertCells('code', thisBlockCode, execute, block.blockNumber);
599+
}
600+
} else {
601+
// set indent to every line of thisblockcode
602+
thisBlockCode = thisBlockCode.replaceAll('\n', '\n' + indent);
603+
code.appendFormat('{0}{1}{2}', prevNewLine, indent, thisBlockCode);
604+
}
592605
});
593606
if (addcell) {
594607
com_interface.insertCell('code', code.toString(), execute, block.blockNumber);

js/com/com_interface.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,45 @@ define([
4949

5050
com_util.renderSuccessMessage('Your code has been executed');
5151
}
52+
53+
/**
54+
* Insert multiple cells
55+
* @param {String} type
56+
* @param {Array} commands
57+
* @param {boolean} exec
58+
* @param {int} sigNum
59+
*/
60+
var insertCells = function(type, commands, exec=true, sigNum=-1) {
61+
var selectedIndex = getSelectedCell();
62+
var targetCell = Jupyter.notebook.insert_cell_below(type, selectedIndex);
63+
64+
commands && commands.forEach((command, idx) => {
65+
// Add signature
66+
if (type == 'code' && sigNum >= 0) {
67+
command = com_util.formatString('# VisualPython [{0}] - {1}\n', sigNum, idx + 1) + command
68+
}
69+
targetCell.set_text(command);
70+
Jupyter.notebook.select_next();
71+
if (exec) {
72+
switch (type) {
73+
case "markdown":
74+
targetCell.render();
75+
break;
76+
case "code":
77+
default:
78+
targetCell.execute();
79+
}
80+
}
81+
82+
selectedIndex = getSelectedCell();
83+
targetCell = Jupyter.notebook.insert_cell_below(type, selectedIndex);
84+
});
85+
86+
// move to executed cell
87+
Jupyter.notebook.scroll_to_cell(Jupyter.notebook.get_selected_index());
88+
89+
com_util.renderSuccessMessage('Your code has been executed');
90+
}
5291

5392
var enableOtherShortcut = function() {
5493
vpLog.display(VP_LOG_TYPE.DEVELOP, 'enable short cut');
@@ -62,6 +101,7 @@ define([
62101

63102
return {
64103
insertCell: insertCell,
104+
insertCells: insertCells,
65105
enableOtherShortcut: enableOtherShortcut,
66106
disableOtherShortcut: disableOtherShortcut,
67107

js/com/component/PopupComponent.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -586,7 +586,12 @@ define([
586586
blockNumber = block.blockNumber;
587587
}
588588
if (addcell) {
589-
com_interface.insertCell(mode, code, execute, blockNumber);
589+
if (Array.isArray(code)) {
590+
// insert cells if it's array of codes
591+
com_interface.insertCells(mode, code, execute, blockNumber);
592+
} else {
593+
com_interface.insertCell(mode, code, execute, blockNumber);
594+
}
590595
}
591596
return code;
592597
}
@@ -693,7 +698,13 @@ define([
693698
openView(viewType) {
694699
if (viewType == 'code') {
695700
var code = this.generateCode();
696-
this.cmCodeview.setValue(code);
701+
let codeText = '';
702+
if (Array.isArray(code)) {
703+
codeText = code.join('\n');
704+
} else {
705+
codeText = code;
706+
}
707+
this.cmCodeview.setValue(codeText);
697708
this.cmCodeview.save();
698709

699710
var that = this;

js/m_ml/evaluation.js

Lines changed: 51 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ define([
118118
}
119119

120120
generateCode() {
121+
let codeCells = [];
121122
let code = new com_String();
122123
let {
123124
modelType, predictData, targetData,
@@ -134,40 +135,56 @@ define([
134135
//====================================================================
135136
if (modelType == 'clf') {
136137
if (confusion_matrix) {
138+
code = new com_String();
137139
code.appendLine("# Confusion Matrix");
138-
code.appendFormatLine('pd.crosstab({0}, {1}, margins=True)', targetData, predictData);
140+
code.appendFormat('pd.crosstab({0}, {1}, margins=True)', targetData, predictData);
141+
codeCells.push(code.toString());
139142
}
140143
if (report) {
144+
code = new com_String();
141145
code.appendLine("# Classification report");
142-
code.appendFormatLine('print(metrics.classification_report({0}, {1}))', targetData, predictData);
146+
code.appendFormat('print(metrics.classification_report({0}, {1}))', targetData, predictData);
147+
codeCells.push(code.toString());
143148
}
144149
if (accuracy) {
150+
code = new com_String();
145151
code.appendLine("# Accuracy");
146-
code.appendFormatLine('metrics.accuracy_score({0}, {1})', targetData, predictData);
152+
code.appendFormat('metrics.accuracy_score({0}, {1})', targetData, predictData);
153+
codeCells.push(code.toString());
147154
}
148155
if (precision) {
156+
code = new com_String();
149157
code.appendLine("# Precision");
150-
code.appendFormatLine("metrics.precision_score({0}, {1}, average='weighted')", targetData, predictData);
158+
code.appendFormat("metrics.precision_score({0}, {1}, average='weighted')", targetData, predictData);
159+
codeCells.push(code.toString());
151160
}
152161
if (recall) {
162+
code = new com_String();
153163
code.appendLine("# Recall");
154-
code.appendFormatLine("metrics.recall_score({0}, {1}, average='weighted')", targetData, predictData);
164+
code.appendFormat("metrics.recall_score({0}, {1}, average='weighted')", targetData, predictData);
165+
codeCells.push(code.toString());
155166
}
156167
if (f1_score) {
168+
code = new com_String();
157169
code.appendLine("# F1-score");
158-
code.appendFormatLine("metrics.f1_score({0}, {1}, average='weighted')", targetData, predictData);
170+
code.appendFormat("metrics.f1_score({0}, {1}, average='weighted')", targetData, predictData);
171+
codeCells.push(code.toString());
159172
}
160173
if (roc_curve) {
174+
code = new com_String();
161175
code.appendLine("# ROC Curve");
162176
code.appendFormatLine("fpr, tpr, thresholds = roc_curve({0}, svc.decision_function({1}}))", predictData, targetData);
163177
code.appendLine("plt.plot(fpr, tpr, label='ROC Curve')");
164178
code.appendLine("plt.xlabel('Sensitivity') ");
165-
code.appendLine("plt.ylabel('Specificity') ")
179+
code.append("plt.ylabel('Specificity') ")
180+
codeCells.push(code.toString());
166181
}
167182
if (auc) {
183+
code = new com_String();
168184
code.appendLine("# AUC");
169185
code.appendFormatLine("fpr, tpr, thresholds = roc_curve({0}, svc.decision_function({1}}))", predictData, targetData);
170-
code.appendLine("metrics.auc(fpr, tpr)");
186+
code.append("metrics.auc(fpr, tpr)");
187+
codeCells.push(code.toString());
171188
}
172189
}
173190

@@ -184,30 +201,40 @@ define([
184201
// code.appendFormatLine('model.intercept_');
185202
// }
186203
if (r_squared) {
204+
code = new com_String();
187205
code.appendLine("# R square");
188-
code.appendFormatLine('metrics.r2_score({0}, {1})', targetData, predictData);
206+
code.appendFormat('metrics.r2_score({0}, {1})', targetData, predictData);
207+
codeCells.push(code.toString());
189208
}
190209
if (mae) {
210+
code = new com_String();
191211
code.appendLine("# MAE(Mean Absolute Error)");
192-
code.appendFormatLine('metrics.mean_absolute_error({0}, {1})', targetData, predictData);
212+
code.appendFormat('metrics.mean_absolute_error({0}, {1})', targetData, predictData);
213+
codeCells.push(code.toString());
193214
}
194215
if (mape) {
216+
code = new com_String();
195217
code.appendLine("# MAPE(Mean Absolute Percentage Error)");
196218
code.appendLine('def MAPE(y_test, y_pred):');
197219
code.appendLine(' return np.mean(np.abs((y_test - pred) / y_test)) * 100');
198220
code.appendLine();
199-
code.appendFormatLine('MAPE({0}, {1})', targetData, predictData);
221+
code.appendFormat('MAPE({0}, {1})', targetData, predictData);
222+
codeCells.push(code.toString());
200223
}
201224
if (rmse) {
225+
code = new com_String();
202226
code.appendLine("# RMSE(Root Mean Squared Error)");
203-
code.appendFormatLine('metrics.mean_squared_error({0}, {1})**0.5', targetData, predictData);
227+
code.appendFormat('metrics.mean_squared_error({0}, {1})**0.5', targetData, predictData);
228+
codeCells.push(code.toString());
204229
}
205230
if (scatter_plot) {
231+
code = new com_String();
206232
code.appendLine('# Regression plot');
207233
code.appendFormatLine('plt.scatter({0}, {1})', targetData, predictData);
208234
code.appendFormatLine("plt.xlabel('{0}')", targetData);
209235
code.appendFormatLine("plt.ylabel('{1}')", predictData);
210-
code.appendLine('plt.show()');
236+
code.append('plt.show()');
237+
codeCells.push(code.toString());
211238
}
212239
}
213240
//====================================================================
@@ -219,20 +246,26 @@ define([
219246
// code.appendFormatLine("print(f'Size of clusters: {np.bincount({0})}')", predictData);
220247
// }
221248
if (silhouetteScore) {
249+
code = new com_String();
222250
code.appendLine("# Silhouette score");
223-
code.appendFormatLine("print(f'Silhouette score: {metrics.cluster.silhouette_score({0}, {1})}')", targetData, predictData);
251+
code.appendFormat("print(f'Silhouette score: {metrics.cluster.silhouette_score({0}, {1})}')", targetData, predictData);
252+
codeCells.push(code.toString());
224253
}
225254
if (ari) {
255+
code = new com_String();
226256
code.appendLine("# ARI");
227-
code.appendFormatLine("print(f'ARI: {metrics.cluster.adjusted_rand_score({0}, {1})}')", targetData, predictData);
257+
code.appendFormat("print(f'ARI: {metrics.cluster.adjusted_rand_score({0}, {1})}')", targetData, predictData);
258+
codeCells.push(code.toString());
228259
}
229260
if (nm) {
261+
code = new com_String();
230262
code.appendLine("# NM");
231-
code.appendFormatLine("print(f'NM: {metrics.cluster.normalized_mutual_info_score({0}, {1})}')", targetData, predictData);
263+
code.appendFormat("print(f'NM: {metrics.cluster.normalized_mutual_info_score({0}, {1})}')", targetData, predictData);
264+
codeCells.push(code.toString());
232265
}
233266
}
234-
// FIXME: as seperated cells
235-
return code.toString();
267+
// return as seperated cells
268+
return codeCells;
236269
}
237270

238271
}

0 commit comments

Comments
 (0)