-
-
Notifications
You must be signed in to change notification settings - Fork 118
Expand file tree
/
Copy pathNormTest.js
More file actions
248 lines (224 loc) · 11.1 KB
/
NormTest.js
File metadata and controls
248 lines (224 loc) · 11.1 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
/*
* Project Name : Visual Python
* Description : GUI-based Python code generator
* File Name : NormTest.js
* Author : Black Logic
* Note : Normality test
* License : GNU GPLv3 with Visual Python special exception
* Date : 2023. 05. 09
* Change Date :
*/
//============================================================================
// [CLASS] NormTest
//============================================================================
define([
__VP_TEXT_LOADER__('vp_base/html/m_stats/normTest.html'),
'vp_base/js/com/com_util',
'vp_base/js/com/com_Const',
'vp_base/js/com/com_String',
'vp_base/js/com/com_generatorV2',
'vp_base/js/com/component/PopupComponent',
'vp_base/js/com/component/DataSelector',
'vp_base/js/m_apps/Subset'
], function(nmHTML, com_util, com_Const, com_String, com_generator, PopupComponent, DataSelector, Subset) {
/**
* NormTest
*/
class NormTest extends PopupComponent {
_init() {
super._init();
/** Write codes executed before rendering */
this.config.sizeLevel = 2;
this.config.checkModules = ['pd'];
this.config.docs = 'https://docs.scipy.org/doc/scipy/reference/';
this.state = {
testType: 'shapiro-wilk',
data0: '',
alterHypo: 'two-sided',
histogram: false,
boxplot: false,
qqplot: true,
...this.state
};
this.subsetEditor = {};
}
_bindEvent() {
super._bindEvent();
/** Implement binding events */
var that = this;
$(this.wrapSelector('#testType')).on('change', function() {
let testType = $(this).val();
that.state.testType = testType;
$(that.wrapSelector('.vp-st-option')).hide();
$(that.wrapSelector('.vp-st-option.' + testType)).show();
});
$(this.wrapSelector('#data0')).on('change', function() {
if (that.state.data0type === 'DataFrame') {
// DataFrame
that.state.var0 = '';
$(that.wrapSelector('#var0')).prop('disabled', false);
com_generator.vp_bindColumnSource(that, 'data0', ['var0'], 'select', false, false);
} else {
// Series
that.state.var0 = '';
$(that.wrapSelector('#var0')).html('');
$(that.wrapSelector('#var0')).prop('disabled', true);
}
});
}
templateForBody() {
let page = $(nmHTML);
let that = this;
let dataSelector = new DataSelector({
pageThis: this, id: 'data0', placeholder: 'Select data', required: true, boxClasses: 'vp-flex-gap5',
allowDataType: ['DataFrame', 'Series'], withPopup: false,
finish: function(data, type) {
that.state.data0 = data;
that.state.data0type = type;
that.state.var0 = '';
$(that.wrapSelector('#data0')).trigger('change');
},
select: function(data, type) {
that.state.data0 = data;
that.state.data0type = type;
that.state.var0 = '';
$(that.wrapSelector('#data0')).trigger('change');
}
});
$(page).find('#data0').replaceWith(dataSelector.toTagString());
return page;
}
render() {
super.render();
let that = this;
// render Subset
this.subsetEditor['data0'] = new Subset({
pandasObject: '',
config: { name: 'Subset', category: this.name } },
{
useAsModule: true,
useInputColumns: true,
targetSelector: this.wrapSelector('#data0'),
pageThis: this,
finish: function(code, state) {
that.state.data0 = code;
$(that.wrapSelector('#data0')).val(code);
that.state.data0type = state.returnType;
$(that.wrapSelector('#data0')).trigger('change');
}
});
// control display option
$(this.wrapSelector('.vp-st-option')).hide();
$(this.wrapSelector('.vp-st-option.' + this.state.testType)).show();
}
generateCode() {
let { testType, data0, data0type, var0, alterHypo, histogram, boxplot, qqplot } = this.state;
let codeList = [];
let code = new com_String();
// test type label
let testTypeLabel = $(this.wrapSelector('#testType option:selected')).text();
code.appendFormatLine('# {0}', testTypeLabel);
// variable declaration
code.appendFormatLine("vp_df = {0}.dropna().copy()", data0);
let dataVar = 'vp_df';
if (var0 !== '') {
dataVar += com_util.formatString("[{0}]", var0);
}
switch (testType) {
case 'shapiro-wilk':
// 1. Shapiro-wilk test
code.appendLine();
code.appendLine("# Normality test (Shapiro-Wilk)");
code.appendLine("from IPython.display import display, Markdown");
code.appendLine("from scipy import stats");
code.appendFormatLine("_res = stats.shapiro({0})", dataVar);
code.appendLine("display(Markdown('### Normality test (Shapiro-Wilk)'))");
code.append("display(pd.DataFrame(data={'Statistic':_res.statistic,'p-value':_res.pvalue},index=['Normality test (Shapiro-Wilk)']))");
break;
case 'anderson-darling':
// 1. Anderson-Darling test
code.appendLine();
code.appendLine("# Normality test (Anderson-Darling)");
code.appendLine("from IPython.display import display, Markdown");
code.appendLine("from scipy import stats");
code.appendFormatLine("_res = stats.anderson({0})", dataVar);
code.appendLine("display(Markdown('### Normality test (Anderson-Darling)'))");
code.appendLine("display(pd.DataFrame(data={'Statistic':[_res.statistic],'Critical values':[_res.critical_values],");
code.appendLine(" 'Significance level(%)':[_res.significance_level]},");
code.append(" index=['Normality test (Anderson-Darling)']))");
break;
case 'kolmogorov-smirnov':
// 1. Kolmogorov-Smirnov test
code.appendLine();
code.appendLine("# Normality test (Kolmogorov-Smirnov)");
code.appendLine("from IPython.display import display, Markdown");
code.appendLine("from scipy import stats");
code.appendFormatLine("_res = stats.kstest({0}, 'norm', alternative='{1}')", dataVar, alterHypo);
code.appendLine("display(Markdown('### Normality test (Kolmogorov-Smirnov)'))");
code.appendLine("display(pd.DataFrame(data={'Statistic':_res.statistic,'p-value':_res.pvalue},");
code.append(" index=['Normality test (Kolmogorov-Smirnov)']))");
break;
case 'dagostino-pearson':
// 1. D Agostino and Pearson test
code.appendLine();
code.appendLine("# Normality test (D Agostino and Pearson)");
code.appendLine("from IPython.display import display, Markdown");
code.appendLine("from scipy import stats");
code.appendFormatLine("_res = stats.normaltest({0})", dataVar);
code.appendLine("display(Markdown('### Normality test (D Agostino and Pearson)'))");
code.appendLine("display(pd.DataFrame(data={'Statistic':_res.statistic,'p-value':_res.pvalue},");
code.append(" index=['Normality test (D Agostino and Pearson)']))");
break;
case 'jarque-bera':
// 1. Jarque-Bera test
code.appendLine();
code.appendLine("# Normality test (Jarque-Bera)");
code.appendLine("from IPython.display import display, Markdown");
code.appendLine("from scipy import stats");
code.appendFormatLine("_res = stats.jarque_bera({0})", dataVar);
code.appendLine("display(Markdown('### Normality test (Jarque-Bera)'))");
code.appendLine("display(pd.DataFrame(data={'Statistic':_res.statistic,'p-value':_res.pvalue},");
code.append(" index=['Normality test (Jarque-Bera)']))");
break;
}
// Display option
if (histogram === true || boxplot === true || qqplot === true) {
code.appendLine();
code.appendLine();
code.appendLine("# Charts");
code.appendLine("import seaborn as sns");
code.appendLine("import warnings");
code.appendLine("with warnings.catch_warnings():");
code.append(" warnings.simplefilter(action='ignore', category=Warning)");
let displayNum = 1;
if (histogram === true) {
code.appendLine();
code.appendLine();
code.appendFormatLine(" plt.subplot(2,2,{0})", displayNum++);
code.appendFormatLine(" sns.histplot({0}, stat='density', kde=True)", dataVar);
code.append(" plt.title('Histogram')");
}
if (boxplot === true) {
code.appendLine();
code.appendLine();
code.appendFormatLine(" plt.subplot(2,2,{0})", displayNum++);
code.appendFormatLine(" sns.boxplot(y={0})", dataVar);
code.append(" plt.title('Boxplot')");
}
if (qqplot === true) {
code.appendLine();
code.appendLine();
code.appendFormatLine(" plt.subplot(2,2,{0})", displayNum);
code.appendFormatLine(" stats.probplot({0}, plot=plt)", dataVar);
code.append(" plt.title('Q-Q Plot')");
}
code.appendLine();
code.appendLine();
code.appendLine(" plt.tight_layout()");
code.append(" plt.show()");
}
return code.toString();
}
}
return NormTest;
});