Skip to content

Commit 00353fa

Browse files
author
minjk-bl
committed
Update ProbDist app's code and add HelpViewer
1 parent cd676f0 commit 00353fa

File tree

2 files changed

+52
-11
lines changed

2 files changed

+52
-11
lines changed

visualpython/data/m_stats/statsLibrary.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ define([
3535
import: 'from scipy import stats',
3636
code: '_rv = stats.binom(${n}${p})',
3737
description: 'A binomial discrete random variable.',
38+
help: '_vp_stats.binom',
3839
options: [
3940
{ name: 'n', component: ['input_number'], value: 10, required: true, usePair: true },
4041
{ name: 'p', component: ['input_number'], value: 0.6, required: true, usePair: true },
@@ -45,6 +46,7 @@ define([
4546
import: 'from scipy import stats',
4647
code: '_rv = stats.multinomial(${n}${p})',
4748
description: 'A multinomial random variable.',
49+
help: '_vp_stats.multinomial',
4850
options: [
4951
{ name: 'n', component: ['input_number'], value: 10, required: true, usePair: true },
5052
{ name: 'p', component: ['data_select'], value: '[0.4, 0.6]', required: true, usePair: true },
@@ -56,6 +58,7 @@ define([
5658
import: 'from scipy import stats',
5759
code: '_rv = stats.uniform()',
5860
description: 'A uniform continuous random variable.',
61+
help: '_vp_stats.uniform',
5962
options: [
6063
]
6164
},
@@ -64,6 +67,7 @@ define([
6467
import: 'from scipy import stats',
6568
code: '_rv = stats.norm(${loc}${scale})',
6669
description: 'A normal continuous random variable.',
70+
help: '_vp_stats.norm',
6771
options: [
6872
{ name: 'loc', component: ['input_number'], value: 0, usePair: true },
6973
{ name: 'scale', component: ['input_number'], value: 1, usePair: true },
@@ -74,6 +78,7 @@ define([
7478
import: 'from scipy import stats',
7579
code: '_rv = stats.beta(${a}${b})',
7680
description: 'A beta continuous random variable.',
81+
help: '_vp_stats.beta',
7782
options: [
7883
{ name: 'a', component: ['input_number'], required: true, usePair: true },
7984
{ name: 'b', component: ['input_number'], required: true, usePair: true },
@@ -84,6 +89,7 @@ define([
8489
import: 'from scipy import stats',
8590
code: '_rv = stats.gamma(${a})',
8691
description: 'A gamma continuous random variable.',
92+
help: '_vp_stats.gamma',
8793
options: [
8894
{ name: 'a', component: ['input_number'], required: true, usePair: true },
8995
]
@@ -93,6 +99,7 @@ define([
9399
import: 'from scipy import stats',
94100
code: '_rv = stats.t(${df})',
95101
description: "A Student's t continuous random variable.",
102+
help: '_vp_stats.t',
96103
options: [
97104
{ name: 'df', component: ['input_number'], required: true, usePair: true },
98105
]
@@ -102,6 +109,7 @@ define([
102109
import: 'from scipy import stats',
103110
code: '_rv = stats.chi2(${df})',
104111
description: 'A chi-squared continuous random variable.',
112+
help: '_vp_stats.chi2',
105113
options: [
106114
{ name: 'df', component: ['input_number'], required: true, usePair: true },
107115
]
@@ -111,6 +119,7 @@ define([
111119
import: 'from scipy import stats',
112120
code: '_rv = stats.f(${dfn}${dfd})',
113121
description: 'An F continuous random variable.',
122+
help: '_vp_stats.f',
114123
options: [
115124
{ name: 'dfn', component: ['input_number'], required: true, usePair: true },
116125
{ name: 'dfd', component: ['input_number'], required: true, usePair: true },
@@ -121,8 +130,9 @@ define([
121130
import: 'from scipy import stats',
122131
code: '_rv = stats.dirichlet(${alpha}${seed})',
123132
description: 'A Dirichlet random variable.',
133+
help: '_vp_stats.dirichlet',
124134
options: [
125-
{ name: 'alpha', component: ['input_number'], required: true, usePair: true },
135+
{ name: 'alpha', component: ['input'], required: true, usePair: true, value: '(3,5,2)', placeholder: '(x, y, z)' },
126136
{ name: 'seed', component: ['input_number'], usePair: true },
127137
]
128138
},
@@ -131,6 +141,7 @@ define([
131141
import: 'from scipy import stats',
132142
code: '_rv = stats.multivariate_normal(${mean}${cov}${allow_singular})',
133143
description: 'A multivariate normal random variable.',
144+
help: '_vp_stats.multivariate_normal',
134145
options: [
135146
{ name: 'mean', component: ['data_select'], value: '[0]', usePair: true },
136147
{ name: 'cov', component: ['data_select'], value: '[1]', usePair: true },

visualpython/js/m_stats/ProbDist.js

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ define([
3434
this.config.sizeLevel = 2;
3535
this.config.checkModules = ['pd', 'plt'];
3636
this.config.docs = 'https://docs.scipy.org/doc/scipy/reference/';
37+
this.config.helpview = true;
3738

3839
this.state = {
3940
distType: 'normal',
@@ -85,6 +86,10 @@ define([
8586
// discrete option
8687
$(that.wrapSelector('.vp-pd-display-option.dist')).show();
8788

89+
// set size to 100
90+
$(that.wrapSelector('#size')).val(100);
91+
that.state.size = 100;
92+
8893
// hide continuous action
8994
if (that.state.action === 'stats-to-pvalue' || that.state.action === 'pvalue-to-stats') {
9095
$(that.wrapSelector('#action')).val('random-number');
@@ -93,14 +98,22 @@ define([
9398
} else {
9499
// continuous option
95100
$(that.wrapSelector('.vp-pd-display-option.cont')).show();
101+
102+
// set size to 10000
103+
$(that.wrapSelector('#size')).val(10000);
104+
that.state.size = 10000;
96105
}
97106

98107
// show install button
99-
if (STATS_LIBRARIES[distType].install != undefined) {
108+
let thisDistObj = STATS_LIBRARIES[distType];
109+
if (thisDistObj.install != undefined) {
100110
$(that.wrapSelector('#vp_installLibrary')).show();
101111
} else {
102112
$(that.wrapSelector('#vp_installLibrary')).hide();
103113
}
114+
115+
// set help content
116+
that.setHelpContent(thisDistObj.help);
104117
});
105118

106119
$(this.wrapSelector('#action')).on('change', function() {
@@ -198,6 +211,8 @@ define([
198211
}
199212
});
200213
$(this.wrapSelector('#allocatedTo')).replaceWith(allocateSelector.toTagString());
214+
215+
this.setHelpContent(STATS_LIBRARIES[this.state.distType].help, true, 'import scipy.stats as _vp_stats');
201216
}
202217

203218
generateCode() {
@@ -248,7 +263,11 @@ define([
248263
code.appendLine("import warnings");
249264
code.appendLine("with warnings.catch_warnings():");
250265
code.appendLine(" warnings.simplefilter(action='ignore', category=Warning)");
251-
code.appendFormatLine(" sns.histplot({0}, stat='density', kde=True)", allocateTo);
266+
if (this.distList[0].child.includes(distType)) {
267+
code.appendFormatLine(" sns.countplot(x={0})", allocateTo);
268+
} else {
269+
code.appendFormatLine(" sns.histplot({0}, stat='density', kde=True)", allocateTo);
270+
}
252271
code.appendFormatLine(" plt.title('Generate random numbers: {0}')", label.replace("'", "\\'"));
253272
code.appendLine(" plt.xlabel('$x$')");
254273
code.append(" plt.show()");
@@ -263,14 +282,25 @@ define([
263282
code.appendLine();
264283
code.appendLine();
265284
code.appendFormatLine("# Probability mass function ({0})", label);
266-
code.appendLine("plt.bar([0,1], _rv.pmf([0,1]))");
267-
code.appendFormatLine("plt.title('Probability mass function: {0}')", label.replace("'", "\\'"));
268-
code.appendLine("plt.xlim(-1, 2)");
269-
code.appendLine("plt.ylim(0, 1)");
270-
code.appendLine("plt.xticks([0, 1], ['x=0', 'x=1'])");
271-
code.appendLine("plt.xlabel('$x$')");
272-
code.appendLine("plt.ylabel('$p(x)$')");
273-
code.append("plt.show()");
285+
if (distType === 'bernoulli') {
286+
code.appendLine("plt.bar([0,1], _rv.pmf([0,1]))");
287+
code.appendFormatLine("plt.title('Probability mass function: {0}')", label.replace("'", "\\'"));
288+
code.appendLine("plt.xlim(-1, 2)");
289+
code.appendLine("plt.ylim(0, 1)");
290+
code.appendLine("plt.xticks([0, 1], ['x=0', 'x=1'])");
291+
code.appendLine("plt.xlabel('$x$')");
292+
code.appendLine("plt.ylabel('$p(x)$')");
293+
code.append("plt.show()");
294+
} else if (distType === 'binomial' || distType === 'multinomial') {
295+
let { n=10 } = this.state;
296+
code.appendFormatLine("plt.bar(range(0,{0}), _rv.pmf(range(0,{1})))", n, n);
297+
code.appendFormatLine("plt.title('Probability mass function: {0}')", label.replace("'", "\\'"));
298+
code.appendFormatLine("plt.xlim(-1, {0})", n);
299+
code.appendFormatLine("plt.xticks(range(0, {0}), ['x='+str(i) for i in range(0, {1})])", n, n);
300+
code.appendLine("plt.xlabel('$x$')");
301+
code.appendLine("plt.ylabel('$p(x)$')");
302+
code.append("plt.show()");
303+
}
274304
}
275305
} else {
276306
if (probDensityFunc === true || cumDistFunc === true) {

0 commit comments

Comments
 (0)