Skip to content

Commit cb97214

Browse files
authored
Merge pull request #2509 from entrylabs/issue/6324
issue/6324
2 parents c6d43ff + 18970db commit cb97214

File tree

9 files changed

+118
-5
lines changed

9 files changed

+118
-5
lines changed

extern/lang/code.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5660,7 +5660,8 @@ Lang.Helper = {
56605660
"get_value_from_table": "선택한 테이블에서 입력한 순서의 선택된 열 값 입니다.",
56615661
"calc_values_from_table": "선택한 테이블에서 선택한 열들에 대한 계산값입니다. (합, 최댓값, 최솟값, 평균값, 표준편차)",
56625662
"open_table_chart": "선택한 테이블의 차트창을 엽니다.",
5663-
"close_table_chart": "열려있는 테이블 차트창을 닫습니다."
5663+
"close_table_chart": "열려있는 테이블 차트창을 닫습니다.",
5664+
"get_coefficient": '선택한 테이블에서 두 속성 사이의 상관계수입니다. 상관계수가 1(-1)에 가까울 수록 강한 양(음)의 선형 상관관계를 갖습니다.'
56645665
};
56655666
Lang.Category = {
56665667
"entrybot_friends": "엔트리봇 친구들",
@@ -6411,6 +6412,7 @@ Lang.template = {
64116412
"calc_values_from_table": "테이블 %1 %2의 %3",
64126413
"open_table_chart": "테이블 %1의 차트 창 열기 %2",
64136414
"close_table_chart": "테이블 차트 창 닫기 %1",
6415+
"get_coefficient": '테이블 %1 %2과(와) %3의 상관계수',
64146416
"variableAddButton": "%1",
64156417
"listAddButton": "%1",
64166418
"change_variable": "Entry.addValueToVariable( %1 , %2 ) %3",

extern/lang/ebs.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5673,7 +5673,8 @@ Lang.Helper = {
56735673
"get_value_from_table": "선택한 테이블에서 입력한 순서의 선택된 열 값 입니다.",
56745674
"calc_values_from_table": "선택한 테이블에서 선택한 열들에 대한 계산값입니다. (합, 최댓값, 최솟값, 평균값, 표준편차)",
56755675
"open_table_chart": "선택한 테이블의 차트창을 엽니다.",
5676-
"close_table_chart": "열려있는 테이블 차트창을 닫습니다."
5676+
"close_table_chart": "열려있는 테이블 차트창을 닫습니다.",
5677+
"get_coefficient": '선택한 테이블에서 두 속성 사이의 상관계수입니다. 상관계수가 1(-1)에 가까울 수록 강한 양(음)의 선형 상관관계를 갖습니다.'
56775678
};
56785679
Lang.Category = {
56795680
"entrybot_friends": "엔트리봇 친구들",
@@ -6427,6 +6428,7 @@ Lang.template = {
64276428
"calc_values_from_table": "테이블 %1 %2의 %3",
64286429
"open_table_chart": "테이블 %1의 차트 창 열기 %2",
64296430
"close_table_chart": "테이블 차트 창 닫기 %1",
6431+
"get_coefficient": '테이블 %1 %2과(와) %3의 상관계수',
64306432
"variableAddButton": "%1",
64316433
"listAddButton": "%1",
64326434
"change_variable": "%1 에 %2 만큼 더하기 %3",

extern/lang/en.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4880,7 +4880,8 @@ Lang.Helper = {
48804880
"get_value_from_table": "Reports the value of selected attribute at the location of the input value from the selected table.",
48814881
"calc_values_from_table": "Reports calculated value of the selected attributes in the selected table. (sum, maximum, minimum, average, standard deviation)",
48824882
"open_table_chart": "Open the chart of the selected table.",
4883-
"close_table_chart": "Close the table chart."
4883+
"close_table_chart": "Close the table chart.",
4884+
"get_coefficient": "Correlation coefficient between two attributes in the selected table. The closer the correlation coefficient is to 1(-1), the stronger the positive (negative) linear correlation."
48844885
};
48854886
Lang.Category = {
48864887
"entrybot_friends": "Entrybot friends",
@@ -5637,6 +5638,7 @@ Lang.template = {
56375638
"calc_values_from_table": "%3 of the %2 in the %1",
56385639
"open_table_chart": "Open the chart of the %1 %2",
56395640
"close_table_chart": "Close the table chart",
5641+
"get_coefficient": 'Correlation coefficient between %2and %3 in table %1',
56405642
"variableAddButton": "%1",
56415643
"listAddButton": "%1",
56425644
"change_variable": "Plus to %1 by %2 %3",

extern/lang/jp.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5664,7 +5664,8 @@ Lang.Helper = {
56645664
"get_value_from_table": "Reports the value of selected attribute at the location of the input value from the selected table.",
56655665
"calc_values_from_table": "Reports calculated value of the selected attributes in the selected table. (sum, maximum, minimum, average, standard deviation)",
56665666
"open_table_chart": "Open the chart of the selected table.",
5667-
"close_table_chart": "Close the table chart."
5667+
"close_table_chart": "Close the table chart.",
5668+
"get_coefficient": "Correlation coefficient between two attributes in the selected table. The closer the correlation coefficient is to 1(-1), the stronger the positive (negative) linear correlation."
56685669
};
56695670
Lang.Category = {
56705671
"entrybot_friends": "エントリーボットの友達",
@@ -6417,6 +6418,7 @@ Lang.template = {
64176418
"calc_values_from_table": "%3 of the %2 in the %1",
64186419
"open_table_chart": "Open the chart of the %1 %2",
64196420
"close_table_chart": "Close the table chart",
6421+
"get_coefficient": 'Correlation coefficient between %2and %3 in table %1',
64206422
"variableAddButton": "%1",
64216423
"listAddButton": "%1",
64226424
"change_variable": "%1 に %2 だけ足す %3",

extern/lang/ko.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6373,6 +6373,7 @@ Lang.Helper = {
63736373
'선택한 테이블에서 선택한 속성들에 대한 요약통계량입니다. (합, 최댓값, 최솟값, 평균값, 표준편차)',
63746374
open_table_chart: '선택한 테이블의 차트창을 엽니다.',
63756375
close_table_chart: '열려있는 테이블 차트 창을 닫습니다.',
6376+
get_coefficient: '선택한 테이블에서 두 속성 사이의 상관계수입니다. 상관계수가 1(-1)에 가까울 수록 강한 양(음)의 선형 상관관계를 갖습니다.',
63766377
insert_data_for_test: '데이터를 입력하고 학습한 모델로 인식합니다.',
63776378
insert_text_block_for_test: '데이터를 입력하고 학습한 모델로 인식합니다.',
63786379
test_result:
@@ -7439,6 +7440,7 @@ Lang.template = {
74397440
calc_values_from_table: '테이블 %1 %2의 %3',
74407441
open_table_chart: '테이블 %1의 %2 차트 창 열기 %3',
74417442
close_table_chart: '테이블 차트 창 닫기 %1',
7443+
get_coefficient: '테이블 %1 %2과(와) %3의 상관계수',
74427444
variableAddButton: '%1',
74437445
listAddButton: '%1',
74447446
change_variable: '%1 에 %2 만큼 더하기 %3',

extern/lang/vn.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5663,7 +5663,8 @@ Lang.Helper = {
56635663
"get_value_from_table": "Reports the value of selected attribute at the location of the input value from the selected table.",
56645664
"calc_values_from_table": "Reports calculated value of the selected attributes in the selected table. (sum, maximum, minimum, average, standard deviation)",
56655665
"open_table_chart": "Open the chart of the selected table.",
5666-
"close_table_chart": "Close the table chart."
5666+
"close_table_chart": "Close the table chart.",
5667+
"get_coefficient": "Correlation coefficient between two attributes in the selected table. The closer the correlation coefficient is to 1(-1), the stronger the positive (negative) linear correlation."
56675668
};
56685669
Lang.Category = {
56695670
"entrybot_friends": "Entrybot friends",
@@ -6414,6 +6415,7 @@ Lang.template = {
64146415
"calc_values_from_table": "%3 of the %2 in the %1",
64156416
"open_table_chart": "Open the chart of the %1 %2",
64166417
"close_table_chart": "Close the table chart",
6418+
"get_coefficient": 'Correlation coefficient between %2and %3 in table %1',
64176419
"variableAddButton": "%1",
64186420
"listAddButton": "%1",
64196421
"change_variable": "thêm vào %1 bằng %2 %3",

extern/util/static.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,7 @@ EntryStatic.getAllBlocks = function() {
409409
'open_table_wait',
410410
'open_table_chart',
411411
'close_table_chart',
412+
'get_coefficient',
412413
],
413414
},
414415
{

src/class/source/DataTableSource.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,26 @@ import CloudVariable from '../../extensions/CloudVariable';
33
import _throttle from 'lodash/throttle';
44
import _cloneDeep from 'lodash/cloneDeep';
55

6+
const getColumns = (table, indexes, wrapper = (x) => x) =>
7+
indexes.map((index) => table.map((field) => wrapper(field[index])));
8+
9+
function corr(d1, d2) {
10+
const { min, pow, sqrt } = Math;
11+
const add = (a, b) => Number(a) + Number(b);
12+
const n = min(d1.length, d2.length);
13+
if (n === 0) {
14+
return 0;
15+
}
16+
[d1, d2] = [d1.slice(0, n), d2.slice(0, n)];
17+
const [sum1, sum2] = [d1, d2].map((l) => l.reduce(add));
18+
const [pow1, pow2] = [d1, d2].map((l) => l.reduce((a, b) => a + pow(b, 2), 0));
19+
const mulSum = d1.map((n, i) => n * d2[i]).reduce(add);
20+
const dense = sqrt((pow1 - pow(sum1, 2) / n) * (pow2 - pow(sum2, 2) / n));
21+
if (dense === 0) {
22+
return 0;
23+
}
24+
return ((mulSum - (sum1 * sum2) / n) / dense).toFixed(2);
25+
}
626
class DataTableSource {
727
#id;
828
#name;
@@ -296,6 +316,10 @@ class DataTableSource {
296316
};
297317
}
298318

319+
getCoefficient(colX, colY) {
320+
return corr(...getColumns(this.rows, [colY, colX]));
321+
}
322+
299323
clone() {
300324
return {
301325
name: this.#name,

src/playground/blocks/block_analysis.js

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -966,6 +966,82 @@ module.exports = {
966966
py: [],
967967
},
968968
},
969+
get_coefficient: {
970+
color: EntryStatic.colorSet.block.default.ANALYSIS,
971+
outerLine: EntryStatic.colorSet.block.darken.ANALYSIS,
972+
skeleton: 'basic_string_field',
973+
statements: [],
974+
params: [
975+
{
976+
type: 'DropdownDynamic',
977+
value: null,
978+
menuName: 'tables',
979+
dropdownSync: 'dataTables',
980+
fontSize: 10,
981+
bgColor: EntryStatic.colorSet.block.darken.ANALYSIS,
982+
arrowColor: EntryStatic.colorSet.arrow.default.DEFAULT,
983+
},
984+
{
985+
type: 'Block',
986+
accept: 'string',
987+
defaultType: 'number',
988+
},
989+
{
990+
type: 'Block',
991+
accept: 'string',
992+
defaultType: 'number',
993+
},
994+
],
995+
events: {},
996+
def: {
997+
params: [
998+
null,
999+
{
1000+
type: 'get_table_fields',
1001+
},
1002+
{
1003+
type: 'get_table_fields',
1004+
},
1005+
],
1006+
type: 'get_coefficient',
1007+
},
1008+
pyHelpDef: {
1009+
params: [
1010+
{
1011+
type: 'text',
1012+
params: ['A&value'],
1013+
},
1014+
{
1015+
type: 'text',
1016+
params: ['B&value'],
1017+
},
1018+
{
1019+
type: 'text',
1020+
params: ['C&value'],
1021+
},
1022+
null,
1023+
],
1024+
type: 'get_coefficient',
1025+
},
1026+
paramsKeyMap: {
1027+
MATRIX: 0,
1028+
FIELD1: 1,
1029+
FIELD2: 2,
1030+
},
1031+
class: 'analysis',
1032+
isNotFor: ['analysis'],
1033+
func(sprite, script) {
1034+
const tableId = script.getField('MATRIX', script);
1035+
const x = DataTable.getColumnIndex(script.getValue('FIELD1', script));
1036+
const y = DataTable.getColumnIndex(script.getValue('FIELD2', script));
1037+
const table = DataTable.getSource(tableId, sprite);
1038+
return table.getCoefficient(x, y);
1039+
},
1040+
syntax: {
1041+
js: [],
1042+
py: [],
1043+
},
1044+
},
9691045
};
9701046
},
9711047
};

0 commit comments

Comments
 (0)