xy-scale 1.3.3 → 1.3.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/xy-scale.min.js +1 -1
- package/package.json +1 -1
- package/src/datasets.js +18 -13
- package/src/scale.js +34 -14
- package/test/test.js +6 -2
package/dist/xy-scale.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var XY_Scale;(()=>{"use strict";var e={d:(t,n)=>{for(var a in n)e.o(n,a)&&!e.o(t,a)&&Object.defineProperty(t,a,{enumerable:!0,get:n[a]})},o:(e,t)=>Object.prototype.hasOwnProperty.call(e,t),r:e=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})}},t={};e.r(t),e.d(t,{arrayToTimesteps:()=>
|
|
1
|
+
var XY_Scale;(()=>{"use strict";var e={d:(t,n)=>{for(var a in n)e.o(n,a)&&!e.o(t,a)&&Object.defineProperty(t,a,{enumerable:!0,get:n[a]})},o:(e,t)=>Object.prototype.hasOwnProperty.call(e,t),r:e=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})}},t={};e.r(t),e.d(t,{arrayToTimesteps:()=>u,parseProductionX:()=>s,parseTrainingXY:()=>o});const n=({arrObj:e,repeat:t={},minmaxRange:n=[0,1],groups:o={},customMinMaxRanges:s=null,excludes:u=new Set})=>{const i=[...e],l=i.length,c=i[0],p="object"==typeof s&&null!==s;if(0===l)return{scaledOutput:[],scaledConfig:{}};const m=Object.keys(c),f=m.map((e=>t.hasOwnProperty(e)?Math.max(t[e],1):1)),g=f.reduce(((e,t)=>e+t),0),h={arrObjLen:l,rangeMin:n[0],rangeMax:n[1],inputTypes:{},min:{},max:{},groupMinMax:{},repeat:t,groups:o,inputKeyNames:m,outputKeyNames:new Array(g),repeatedKeyNames:f};let d=0;for(let e=0;e<h.inputKeyNames.length;e++)for(let t=0;t<h.repeatedKeyNames[e];t++)h.outputKeyNames[d++]=h.inputKeyNames[e];a(h.groups);const x=["number","boolean"];for(const e of h.inputKeyNames){if(u.has(e)){h.inputTypes[e]="excluded";continue}const t=typeof c[e],n=r(e,h.groups);if(!x.includes(t))throw new Error(`Invalid input type "${t}" provided for key "${e}". Only accepting `);h.inputTypes[e]=t,p&&s.hasOwnProperty(e)?n?h.groupMinMax[n]=s[e]:(h.min[e]=s[e].min,h.max[e]=s[e].max):n?h.groupMinMax[n]={min:1/0,max:-1/0}:(h.min[e]=1/0,h.max[e]=-1/0)}for(const e of i)for(const t of h.inputKeyNames){if("excluded"===h.inputTypes[t])continue;let n=e[t];"boolean"===h.inputTypes[t]&&(e[t]=Number(n));const a=r(t,h.groups);(!1===p||p&&!s.hasOwnProperty(t))&&(a?(h.groupMinMax[a].min=Math.min(h.groupMinMax[a].min,n),h.groupMinMax[a].max=Math.max(h.groupMinMax[a].max,n)):(h.min[t]=Math.min(h.min[t],n),h.max[t]=Math.max(h.max[t],n)))}const M=new Array(l);for(let e=0;e<l;e++){const t=i[e],n=new Array(h.outputKeyNames.length);let a=0;for(let e=0;e<h.inputKeyNames.length;e++){const o=h.inputKeyNames[e],s=t[o];if("excluded"===h.inputTypes[o]){n[a++]=s;continue}const u=r(o,h.groups);let i,l;u?(i=h.groupMinMax[u].min,l=h.groupMinMax[u].max):(i=h.min[o],l=h.max[o]);const c=l!==i?h.rangeMin+(s-i)/(l-i)*(h.rangeMax-h.rangeMin):h.rangeMin,p=h.repeatedKeyNames[e];for(let e=0;e<p;e++)n[a++]=c}M[e]=n}return{scaledOutput:M,scaledConfig:h}},a=e=>{const t=new Set,n=[];for(const[a,r]of Object.entries(e))t.add(a),n.push(a),r.forEach((e=>{t.add(e),n.push(e)}));if(t.size!==n.length)throw new Error("Duplicate value found between properties in validateUniqueProperties function.")},r=(e,t)=>{for(const[n,a]of Object.entries(t))if(a.includes(e))return n;return null},o=({arrObj:e=[],trainingSplit:t=.8,repeat:a={},yCallbackFunc:r=e=>e,xCallbackFunc:o=e=>e,validateRows:s=()=>!0,groups:u={},shuffle:i=!1,minmaxRange:l=[],balancing:c="",state:p={},customMinMaxRanges:m={},excludes:f=[]})=>{let g=[],h=[];for(let t=0;t<e.length;t++){if(!s({objRow:e,index:t,state:p}))continue;const n=o({objRow:e,index:t,state:p}),a=r({objRow:e,index:t,state:p});null!=n&&null!=a&&(g.push(n),h.push(a))}if(i){const{shuffledX:e,shuffledY:t}=((e,t)=>{if(e.length!==t.length)throw new Error("X and Y arrays must have the same length");const n=Array.from({length:e.length},((e,t)=>t));for(let e=n.length-1;e>0;e--){const t=Math.floor(Math.random()*(e+1));[n[e],n[t]]=[n[t],n[e]]}return{shuffledX:n.map((t=>e[t])),shuffledY:n.map((e=>t[e]))}})(g,h);g=e,h=t}const d=new Set(f);let{scaledOutput:x,scaledConfig:M}=n({arrObj:g,repeat:a,groups:u,minmaxRange:l,customMinMaxRanges:m,excludes:d}),{scaledOutput:y,scaledConfig:b}=n({arrObj:h,repeat:a,groups:u,minmaxRange:l,customMinMaxRanges:m,excludes:d});const O=Math.floor(x.length*t);let w=x.slice(0,O),j=y.slice(0,O),R=x.slice(O),v=y.slice(O);if(c){let e;if("oversample"===c)e=((e,t)=>{const n={},a={};t.forEach(((r,o)=>{n[r]||(n[r]=0,a[r]=[]),n[r]++,a[r].push([e[o],t[o]])}));const r=Math.max(...Object.values(n)),o=[],s=[];return Object.keys(a).forEach((e=>{const t=a[e],n=t.length;for(let e=0;e<r;e++){const a=t[e%n];o.push(a[0]),s.push(a[1])}})),{X:o,Y:s}})(w,j),w=e.X,j=e.Y;else{if("undersample"!==c)throw Error('balancing argument only accepts "false", "oversample" and "undersample". Defaults to "false".');e=((e,t)=>{const n={},a={};t.forEach(((r,o)=>{n[r]||(n[r]=0,a[r]=[]),n[r]++,a[r].push([e[o],t[o]])}));const r=Math.min(...Object.values(n)),o=[],s=[];return Object.keys(a).forEach((e=>{const t=a[e];for(let e=0;e<r;e++){const n=t[e];o.push(n[0]),s.push(n[1])}})),{X:o,Y:s}})(w,j),w=e.X,j=e.Y}}return{trainX:w,trainY:j,testX:R,testY:v,configX:M,configY:b}},s=({arrObj:e=[],repeat:t={},xCallbackFunc:a=e=>e,validateRows:r=()=>!0,groups:o={},shuffle:s=!1,minmaxRange:u=[],state:i={},customMinMaxRanges:l,excludes:c=[]})=>{let p=[];for(let t=0;t<e.length;t++){if(!r(e[t]))continue;const n=a({objRow:e,index:t,state:i});null!=n&&!1!==n&&p.push(n)}s&&(p=(e=>{const t=[...e];for(let e=t.length-1;e>0;e--){const n=Math.floor(Math.random()*(e+1));[t[e],t[n]]=[t[n],t[e]]}return t})(p));const{scaledOutput:m,scaledConfig:f}=n({arrObj:p,repeat:t,groups:o,minmaxRange:u,customMinMaxRanges:l,excludes:new Set(c)});return{X:m,configX:f}},u=(e,t)=>{if(0===t)return e;if(t<0)throw new Error("timeSteps must be greater than 0");const n=[];for(let a=0;a<=e.length-t;a++)n.push(e.slice(a,a+t));return n};XY_Scale=t})();
|
package/package.json
CHANGED
package/src/datasets.js
CHANGED
|
@@ -3,18 +3,19 @@ import { arrayShuffle, xyArrayShuffle } from "./utilities.js";
|
|
|
3
3
|
import { oversampleXY, undersampleXY } from "./balancing.js";
|
|
4
4
|
|
|
5
5
|
export const parseTrainingXY = ({
|
|
6
|
-
arrObj,
|
|
6
|
+
arrObj = [],
|
|
7
7
|
trainingSplit = 0.8,
|
|
8
|
-
repeat,
|
|
8
|
+
repeat = {},
|
|
9
9
|
yCallbackFunc = row => row,
|
|
10
10
|
xCallbackFunc = row => row,
|
|
11
11
|
validateRows = () => true,
|
|
12
|
-
groups,
|
|
12
|
+
groups = {},
|
|
13
13
|
shuffle = false,
|
|
14
|
-
minmaxRange,
|
|
14
|
+
minmaxRange = [],
|
|
15
15
|
balancing = '',
|
|
16
16
|
state = {},
|
|
17
|
-
customMinMaxRanges
|
|
17
|
+
customMinMaxRanges = {},
|
|
18
|
+
excludes = []
|
|
18
19
|
}) => {
|
|
19
20
|
let X = [];
|
|
20
21
|
let Y = [];
|
|
@@ -40,15 +41,17 @@ export const parseTrainingXY = ({
|
|
|
40
41
|
Y = shuffledY
|
|
41
42
|
}
|
|
42
43
|
|
|
44
|
+
const excludesSet = new Set(excludes)
|
|
45
|
+
|
|
43
46
|
let {
|
|
44
47
|
scaledOutput: scaledX,
|
|
45
48
|
scaledConfig: configX
|
|
46
|
-
} = scaleArrayObj({arrObj: X, repeat, groups, minmaxRange, customMinMaxRanges})
|
|
49
|
+
} = scaleArrayObj({arrObj: X, repeat, groups, minmaxRange, customMinMaxRanges, excludes: excludesSet})
|
|
47
50
|
|
|
48
51
|
let {
|
|
49
52
|
scaledOutput: scaledY,
|
|
50
53
|
scaledConfig: configY,
|
|
51
|
-
} = scaleArrayObj({arrObj: Y, repeat, groups, minmaxRange, customMinMaxRanges})
|
|
54
|
+
} = scaleArrayObj({arrObj: Y, repeat, groups, minmaxRange, customMinMaxRanges, excludes: excludesSet})
|
|
52
55
|
|
|
53
56
|
|
|
54
57
|
|
|
@@ -98,15 +101,16 @@ export const parseTrainingXY = ({
|
|
|
98
101
|
|
|
99
102
|
|
|
100
103
|
export const parseProductionX = ({
|
|
101
|
-
arrObj,
|
|
102
|
-
repeat,
|
|
104
|
+
arrObj = [],
|
|
105
|
+
repeat = {},
|
|
103
106
|
xCallbackFunc = row => row,
|
|
104
107
|
validateRows = () => true,
|
|
105
|
-
groups,
|
|
108
|
+
groups = {},
|
|
106
109
|
shuffle = false,
|
|
107
|
-
minmaxRange,
|
|
110
|
+
minmaxRange = [],
|
|
108
111
|
state = {},
|
|
109
|
-
customMinMaxRanges
|
|
112
|
+
customMinMaxRanges,
|
|
113
|
+
excludes = []
|
|
110
114
|
}) => {
|
|
111
115
|
let X = [];
|
|
112
116
|
|
|
@@ -126,11 +130,12 @@ export const parseProductionX = ({
|
|
|
126
130
|
X = arrayShuffle(X)
|
|
127
131
|
}
|
|
128
132
|
|
|
133
|
+
|
|
129
134
|
// Scale X
|
|
130
135
|
const {
|
|
131
136
|
scaledOutput: scaledX,
|
|
132
137
|
scaledConfig: configX
|
|
133
|
-
} = scaleArrayObj({arrObj: X, repeat, groups, minmaxRange, customMinMaxRanges})
|
|
138
|
+
} = scaleArrayObj({arrObj: X, repeat, groups, minmaxRange, customMinMaxRanges, excludes: new Set(excludes)})
|
|
134
139
|
|
|
135
140
|
|
|
136
141
|
// Split into training and testing sets
|
package/src/scale.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export const scaleArrayObj = ({ arrObj, repeat = {}, minmaxRange = [0, 1], groups = {}, customMinMaxRanges = null }) => {
|
|
1
|
+
export const scaleArrayObj = ({ arrObj, repeat = {}, minmaxRange = [0, 1], groups = {}, customMinMaxRanges = null, excludes = new Set() }) => {
|
|
2
2
|
|
|
3
3
|
const arrObjClone = [...arrObj]
|
|
4
4
|
const arrObjLen = arrObjClone.length;
|
|
@@ -32,7 +32,7 @@ export const scaleArrayObj = ({ arrObj, repeat = {}, minmaxRange = [0, 1], group
|
|
|
32
32
|
groups,
|
|
33
33
|
inputKeyNames,
|
|
34
34
|
outputKeyNames: new Array(countRepeatedKeyNames),
|
|
35
|
-
repeatedKeyNames
|
|
35
|
+
repeatedKeyNames
|
|
36
36
|
}
|
|
37
37
|
|
|
38
38
|
let keyNamesIdx = 0;
|
|
@@ -49,6 +49,12 @@ export const scaleArrayObj = ({ arrObj, repeat = {}, minmaxRange = [0, 1], group
|
|
|
49
49
|
|
|
50
50
|
for (const key of config.inputKeyNames) {
|
|
51
51
|
|
|
52
|
+
if(excludes.has(key))
|
|
53
|
+
{
|
|
54
|
+
config.inputTypes[key] = 'excluded'
|
|
55
|
+
continue
|
|
56
|
+
}
|
|
57
|
+
|
|
52
58
|
const firstType = typeof firstRow[key]
|
|
53
59
|
const thisGroup = findGroup(key, config.groups);
|
|
54
60
|
|
|
@@ -85,6 +91,12 @@ export const scaleArrayObj = ({ arrObj, repeat = {}, minmaxRange = [0, 1], group
|
|
|
85
91
|
|
|
86
92
|
for (const obj of arrObjClone) {
|
|
87
93
|
for (const key of config.inputKeyNames) {
|
|
94
|
+
|
|
95
|
+
if (config.inputTypes[key] === 'excluded')
|
|
96
|
+
{
|
|
97
|
+
continue;
|
|
98
|
+
}
|
|
99
|
+
|
|
88
100
|
let value = obj[key];
|
|
89
101
|
|
|
90
102
|
if (config.inputTypes[key] === 'boolean') {
|
|
@@ -108,24 +120,32 @@ export const scaleArrayObj = ({ arrObj, repeat = {}, minmaxRange = [0, 1], group
|
|
|
108
120
|
}
|
|
109
121
|
|
|
110
122
|
const scaledOutput = new Array(arrObjLen);
|
|
123
|
+
|
|
124
|
+
|
|
111
125
|
for (let i = 0; i < arrObjLen; i++) {
|
|
112
126
|
const obj = arrObjClone[i];
|
|
113
127
|
const scaledRow = new Array(config.outputKeyNames.length);
|
|
114
128
|
let idx = 0;
|
|
115
129
|
|
|
116
130
|
for (let j = 0; j < config.inputKeyNames.length; j++) {
|
|
117
|
-
const key = config.inputKeyNames[j]
|
|
118
|
-
const value = obj[key]
|
|
131
|
+
const key = config.inputKeyNames[j]
|
|
132
|
+
const value = obj[key]
|
|
133
|
+
|
|
134
|
+
if (config.inputTypes[key] === 'excluded')
|
|
135
|
+
{
|
|
136
|
+
scaledRow[idx++] = value
|
|
137
|
+
continue
|
|
138
|
+
}
|
|
119
139
|
|
|
120
140
|
const thisGroup = findGroup(key, config.groups);
|
|
121
|
-
let minValue, maxValue
|
|
141
|
+
let minValue, maxValue
|
|
122
142
|
|
|
123
143
|
if (thisGroup) {
|
|
124
|
-
minValue = config.groupMinMax[thisGroup].min
|
|
125
|
-
maxValue = config.groupMinMax[thisGroup].max
|
|
144
|
+
minValue = config.groupMinMax[thisGroup].min
|
|
145
|
+
maxValue = config.groupMinMax[thisGroup].max
|
|
126
146
|
} else {
|
|
127
|
-
minValue = config.min[key]
|
|
128
|
-
maxValue = config.max[key]
|
|
147
|
+
minValue = config.min[key]
|
|
148
|
+
maxValue = config.max[key]
|
|
129
149
|
}
|
|
130
150
|
|
|
131
151
|
const scaledValue =
|
|
@@ -133,14 +153,14 @@ export const scaleArrayObj = ({ arrObj, repeat = {}, minmaxRange = [0, 1], group
|
|
|
133
153
|
? config.rangeMin + ((value - minValue) / (maxValue - minValue)) * (config.rangeMax - config.rangeMin)
|
|
134
154
|
: config.rangeMin;
|
|
135
155
|
|
|
136
|
-
const rep = config.repeatedKeyNames[j]
|
|
156
|
+
const rep = config.repeatedKeyNames[j]
|
|
137
157
|
|
|
138
158
|
for (let w = 0; w < rep; w++) {
|
|
139
|
-
scaledRow[idx++] = scaledValue
|
|
159
|
+
scaledRow[idx++] = scaledValue
|
|
140
160
|
}
|
|
141
161
|
|
|
142
162
|
}
|
|
143
|
-
scaledOutput[i] = scaledRow
|
|
163
|
+
scaledOutput[i] = scaledRow
|
|
144
164
|
}
|
|
145
165
|
|
|
146
166
|
|
|
@@ -148,8 +168,8 @@ export const scaleArrayObj = ({ arrObj, repeat = {}, minmaxRange = [0, 1], group
|
|
|
148
168
|
return {
|
|
149
169
|
scaledOutput,
|
|
150
170
|
scaledConfig: config
|
|
151
|
-
}
|
|
152
|
-
}
|
|
171
|
+
}
|
|
172
|
+
}
|
|
153
173
|
|
|
154
174
|
|
|
155
175
|
const validateUniqueProperties = obj => {
|
package/test/test.js
CHANGED
|
@@ -56,11 +56,14 @@ const test = async () => {
|
|
|
56
56
|
shuffle: false,
|
|
57
57
|
minmaxRange: [0, 1],
|
|
58
58
|
balancing: null,
|
|
59
|
-
groups: scaledGroups
|
|
59
|
+
groups: scaledGroups,
|
|
60
|
+
excludes: ['high']
|
|
60
61
|
});
|
|
61
62
|
|
|
62
63
|
//console.log(configX.outputKeyNames)
|
|
63
|
-
console.log(configX)
|
|
64
|
+
//console.log(configX)
|
|
65
|
+
|
|
66
|
+
console.log('trainX', trainX[0])
|
|
64
67
|
|
|
65
68
|
tensorflowExample({
|
|
66
69
|
trainX,
|
|
@@ -137,6 +140,7 @@ const xCallbackFunc = ({ objRow, index }) => {
|
|
|
137
140
|
//console.log(((curr.sma_300 - curr.low) / curr.low) * 100)
|
|
138
141
|
|
|
139
142
|
const output = {
|
|
143
|
+
high: curr.high,
|
|
140
144
|
ema50IsUp: curr.ema_50 > prev.ema_50,
|
|
141
145
|
ema50GtSma200: curr.ema_50 > curr.sma_200,
|
|
142
146
|
ema50GtSma300: curr.ema_50 > curr.sma_300,
|