xy-scale 1.3.0 → 1.3.2
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 +1 -1
- package/src/scale.js +9 -18
- package/test/test.js +3 -3
package/dist/xy-scale.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var XY_Scale;(()=>{"use strict";var e={d:(r,t)=>{for(var n in t)e.o(t,n)&&!e.o(r,n)&&Object.defineProperty(r,n,{enumerable:!0,get:t[n]})},o:(e,r)=>Object.prototype.hasOwnProperty.call(e,r),r:e=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})}},r={};e.r(r),e.d(r,{arrayToTimesteps:()=>p,parseProductionX:()=>u,parseTrainingXY:()=>i});const t=({arrObj:e,repeat:r={},minmaxRange:t=[0,1],groups:i={},prevConfig:u=null,customMinMaxRanges:p=null})=>{const m=[...e],f=m.length,l=m[0],c="object"==typeof p&&null!==p;if(0===f)return{scaledOutput:[],scaledConfig:{}};let g={};const h=u&&o(u);if(h)s(u,{minmaxRange:t,repeat:r,groups:i,firstRow:l}),g={...u};else{const e=Object.keys(l),n=e.map((e=>r.hasOwnProperty(e)?Math.max(r[e],1):1)),a=n.reduce(((e,r)=>e+r),0);g={arrObjLen:f,rangeMin:t[0],rangeMax:t[1],inputTypes:{},min:{},max:{},
|
|
1
|
+
var XY_Scale;(()=>{"use strict";var e={d:(r,t)=>{for(var n in t)e.o(t,n)&&!e.o(r,n)&&Object.defineProperty(r,n,{enumerable:!0,get:t[n]})},o:(e,r)=>Object.prototype.hasOwnProperty.call(e,r),r:e=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})}},r={};e.r(r),e.d(r,{arrayToTimesteps:()=>p,parseProductionX:()=>u,parseTrainingXY:()=>i});const t=({arrObj:e,repeat:r={},minmaxRange:t=[0,1],groups:i={},prevConfig:u=null,customMinMaxRanges:p=null})=>{const m=[...e],f=m.length,l=m[0],c="object"==typeof p&&null!==p;if(0===f)return{scaledOutput:[],scaledConfig:{}};let g={};const h=u&&o(u);if(h)s(u,{minmaxRange:t,repeat:r,groups:i,firstRow:l}),g={...u};else{const e=Object.keys(l),n=e.map((e=>r.hasOwnProperty(e)?Math.max(r[e],1):1)),a=n.reduce(((e,r)=>e+r),0);g={arrObjLen:f,rangeMin:t[0],rangeMax:t[1],inputTypes:{},min:{},max:{},groupMinMax:{},repeat:r,groups:i,inputKeyNames:e,outputKeyNames:new Array(a),repeatedKeyNames:n};let s=0;for(let e=0;e<g.inputKeyNames.length;e++)for(let r=0;r<g.repeatedKeyNames[e];r++)g.outputKeyNames[s++]=g.inputKeyNames[e];o(g)}n(g.groups);const y=["number","boolean"];for(const e of g.inputKeyNames){const r=typeof l[e],t=a(e,g.groups);if(!y.includes(r))throw new Error(`Invalid input type "${r}" provided for key "${e}". Only accepting `);if(h){if(!g.inputTypes.hasOwnProperty(e))throw new Error(`Error: A new unknown inputType property "${e}" found.`);if(g.inputTypes[e]!==r)throw new Error(`Error: Current inputType of property "${e}" is not the same as in the prevConfig inputType.`)}else g.inputTypes[e]=r,c&&p.hasOwnProperty(e)?t?g.groupMinMax[t]=p[e]:(g.min[e]=p[e].min,g.max[e]=p[e].max):t?g.groupMinMax[t]={min:1/0,max:-1/0}:(g.min[e]=1/0,g.max[e]=-1/0)}for(const e of m)for(const r of g.inputKeyNames){let t=e[r];"boolean"===g.inputTypes[r]&&(e[r]=Number(t));const n=a(r,g.groups);(!1===c||c&&!p.hasOwnProperty(r))&&(n?(g.groupMinMax[n].min=Math.min(g.groupMinMax[n].min,t),g.groupMinMax[n].max=Math.max(g.groupMinMax[n].max,t)):(g.min[r]=Math.min(g.min[r],t),g.max[r]=Math.max(g.max[r],t)))}const w=new Array(f);for(let e=0;e<f;e++){const r=m[e],t=new Array(g.outputKeyNames.length);let n=0;for(let e=0;e<g.inputKeyNames.length;e++){const o=g.inputKeyNames[e],s=r[o],i=a(o,g.groups);let u,p;i?(u=g.groupMinMax[i].min,p=g.groupMinMax[i].max):(u=g.min[o],p=g.max[o]);const m=p!==u?g.rangeMin+(s-u)/(p-u)*(g.rangeMax-g.rangeMin):g.rangeMin,f=g.repeatedKeyNames[e];for(let e=0;e<f;e++)t[n++]=m}w[e]=t}return{scaledOutput:w,scaledConfig:g}},n=e=>{const r=new Set,t=[];for(const[n,a]of Object.entries(e))r.add(n),t.push(n),a.forEach((e=>{r.add(e),t.push(e)}));if(r.size!==t.length)throw new Error("Duplicate value found between properties in validateUniqueProperties function.")},a=(e,r)=>{for(const[t,n]of Object.entries(r))if(n.includes(e))return t;return null},o=e=>{if(!e)return!1;const r=["rangeMin","rangeMax","inputTypes","min","max","groupMinMax","repeat","groups","inputKeyNames","outputKeyNames","repeatedKeyNames"];for(const t of r)if(!e.hasOwnProperty(t))throw new Error(`Missing key "${t}" in config.`);const{rangeMin:t,rangeMax:n,inputTypes:a,min:o,max:s,groupMinMax:i,repeat:u,groups:p,inputKeyNames:m,outputKeyNames:f,repeatedKeyNames:l}=e;if("number"!=typeof t||"number"!=typeof n)throw new Error("rangeMin and rangeMax must be numbers.");if(t>=n)throw new Error("rangeMin must be less than rangeMax.");const c=e=>"object"==typeof e&&null!==e&&!Array.isArray(e);if(!c(a))throw new Error("inputTypes must be an object.");if(!c(o))throw new Error("min must be an object.");if(!c(s))throw new Error("max must be an object.");if(!c(i))throw new Error("groupMinMax must be an object.");if(!c(u))throw new Error("repeat must be an object.");if(!c(p))throw new Error("groups must be an object.");if(!Array.isArray(m))throw new Error("inputKeyNames must be an array.");if(!Array.isArray(f))throw new Error("outputKeyNames must be an array.");if(!Array.isArray(l))throw new Error("repeatedKeyNames must be an array.");return!0},s=(e,{minmaxRange:r,repeat:t,groups:n,firstRow:a})=>{if(e.rangeMin!==r[0]||e.rangeMax!==r[1])throw new Error('"prevConfig.minmaxRange" is not equal "minmaxRange".');if(JSON.stringify(e.inputKeyNames)!==JSON.stringify(Object.keys(a)))throw new Error('"prevConfig.inputKeyNames" structure does not match "Object.keys(firstRow)" structure. The order of keys is important.');if(JSON.stringify(e.repeat)!==JSON.stringify(t))throw new Error('"prevConfig.repeat" structure does not match "repeat" structure. The order of keys is important.');if(JSON.stringify(e.groups)!==JSON.stringify(n))throw new Error('"prevConfig.groups" structure does not match "groups" structure. The order of keys is important.');return!0},i=({arrObj:e,trainingSplit:r=.8,repeat:n,yCallbackFunc:a=e=>e,xCallbackFunc:o=e=>e,validateRows:s=()=>!0,groups:i,shuffle:u=!1,minmaxRange:p,balancing:m="",state:f={},customMinMaxRanges:l})=>{let c=[],g=[];for(let r=0;r<e.length;r++){if(!s({objRow:e,index:r,state:f}))continue;const t=o({objRow:e,index:r,state:f}),n=a({objRow:e,index:r,state:f});null!=t&&null!=n&&(c.push(t),g.push(n))}if(u){const{shuffledX:e,shuffledY:r}=((e,r)=>{if(e.length!==r.length)throw new Error("X and Y arrays must have the same length");const t=Array.from({length:e.length},((e,r)=>r));for(let e=t.length-1;e>0;e--){const r=Math.floor(Math.random()*(e+1));[t[e],t[r]]=[t[r],t[e]]}return{shuffledX:t.map((r=>e[r])),shuffledY:t.map((e=>r[e]))}})(c,g);c=e,g=r}let{scaledOutput:h,scaledConfig:y}=t({arrObj:c,repeat:n,groups:i,minmaxRange:p,customMinMaxRanges:l}),{scaledOutput:w,scaledConfig:M}=t({arrObj:g,repeat:n,groups:i,minmaxRange:p,customMinMaxRanges:l});const d=Math.floor(h.length*r);let b=h.slice(0,d),x=w.slice(0,d),O=h.slice(d),j=w.slice(d);if(m){let e;if("oversample"===m)e=((e,r)=>{const t={},n={};r.forEach(((a,o)=>{t[a]||(t[a]=0,n[a]=[]),t[a]++,n[a].push([e[o],r[o]])}));const a=Math.max(...Object.values(t)),o=[],s=[];return Object.keys(n).forEach((e=>{const r=n[e],t=r.length;for(let e=0;e<a;e++){const n=r[e%t];o.push(n[0]),s.push(n[1])}})),{X:o,Y:s}})(b,x),b=e.X,x=e.Y;else{if("undersample"!==m)throw Error('balancing argument only accepts "false", "oversample" and "undersample". Defaults to "false".');e=((e,r)=>{const t={},n={};r.forEach(((a,o)=>{t[a]||(t[a]=0,n[a]=[]),t[a]++,n[a].push([e[o],r[o]])}));const a=Math.min(...Object.values(t)),o=[],s=[];return Object.keys(n).forEach((e=>{const r=n[e];for(let e=0;e<a;e++){const t=r[e];o.push(t[0]),s.push(t[1])}})),{X:o,Y:s}})(b,x),b=e.X,x=e.Y}}return{trainX:b,trainY:x,testX:O,testY:j,configX:y,configY:M}},u=({arrObj:e,repeat:r,xCallbackFunc:n=e=>e,validateRows:a=()=>!0,groups:o,shuffle:s=!1,minmaxRange:i,state:u={},prevConfig:p,customMinMaxRanges:m})=>{let f=[];for(let r=0;r<e.length;r++){if(!a(e[r]))continue;const t=n({objRow:e,index:r,state:u});null!=t&&!1!==t&&f.push(t)}s&&(f=(e=>{const r=[...e];for(let e=r.length-1;e>0;e--){const t=Math.floor(Math.random()*(e+1));[r[e],r[t]]=[r[t],r[e]]}return r})(f));const{scaledOutput:l,scaledConfig:c}=t({arrObj:f,repeat:r,groups:o,minmaxRange:i,prevConfig:p,customMinMaxRanges:m});return{X:l,configX:c}},p=(e,r)=>{if(0===r)return e;if(r<0)throw new Error("timeSteps must be greater than 0");const t=[];for(let n=0;n<=e.length-r;n++)t.push(e.slice(n,n+r));return t};XY_Scale=r})();
|
package/package.json
CHANGED
package/src/datasets.js
CHANGED
|
@@ -22,7 +22,7 @@ export const parseTrainingXY = ({
|
|
|
22
22
|
//if parsedX and parsedY is undefined or null the current row will be excluded from training or production
|
|
23
23
|
for (let x = 0; x < arrObj.length; x++) {
|
|
24
24
|
|
|
25
|
-
if(!validateRows(arrObj
|
|
25
|
+
if(!validateRows({ objRow: arrObj, index: x, state})) continue
|
|
26
26
|
|
|
27
27
|
const parsedX = xCallbackFunc({ objRow: arrObj, index: x, state})
|
|
28
28
|
const parsedY = yCallbackFunc({ objRow: arrObj, index: x, state})
|
package/src/scale.js
CHANGED
|
@@ -38,7 +38,6 @@ export const scaleArrayObj = ({ arrObj, repeat = {}, minmaxRange = [0, 1], group
|
|
|
38
38
|
inputTypes: {},
|
|
39
39
|
min: {},
|
|
40
40
|
max: {},
|
|
41
|
-
uniqueStrIdx: {},
|
|
42
41
|
groupMinMax: {},
|
|
43
42
|
repeat,
|
|
44
43
|
groups,
|
|
@@ -61,11 +60,19 @@ export const scaleArrayObj = ({ arrObj, repeat = {}, minmaxRange = [0, 1], group
|
|
|
61
60
|
|
|
62
61
|
validateUniqueProperties(config.groups);
|
|
63
62
|
|
|
63
|
+
const validInputTypes = ['number', 'boolean']
|
|
64
|
+
|
|
64
65
|
for (const key of config.inputKeyNames) {
|
|
65
66
|
|
|
66
67
|
const firstType = typeof firstRow[key]
|
|
67
68
|
const thisGroup = findGroup(key, config.groups);
|
|
68
69
|
|
|
70
|
+
|
|
71
|
+
if(!validInputTypes.includes(firstType))
|
|
72
|
+
{
|
|
73
|
+
throw new Error(`Invalid input type "${firstType}" provided for key "${key}". Only accepting `)
|
|
74
|
+
}
|
|
75
|
+
|
|
69
76
|
if(isValidPrevConfig)
|
|
70
77
|
{
|
|
71
78
|
if(!config.inputTypes.hasOwnProperty(key))
|
|
@@ -83,10 +90,6 @@ export const scaleArrayObj = ({ arrObj, repeat = {}, minmaxRange = [0, 1], group
|
|
|
83
90
|
}
|
|
84
91
|
|
|
85
92
|
config.inputTypes[key] = firstType;
|
|
86
|
-
|
|
87
|
-
if (firstType === 'string') {
|
|
88
|
-
config.uniqueStrIdx[key] = {};
|
|
89
|
-
}
|
|
90
93
|
|
|
91
94
|
if(validCustomMinMaxRanges && customMinMaxRanges.hasOwnProperty(key))
|
|
92
95
|
{
|
|
@@ -115,14 +118,7 @@ export const scaleArrayObj = ({ arrObj, repeat = {}, minmaxRange = [0, 1], group
|
|
|
115
118
|
for (const key of config.inputKeyNames) {
|
|
116
119
|
let value = obj[key];
|
|
117
120
|
|
|
118
|
-
if (config.inputTypes[key] === '
|
|
119
|
-
const uniqueIndexes = config.uniqueStrIdx[key];
|
|
120
|
-
if (!uniqueIndexes.hasOwnProperty(value)) {
|
|
121
|
-
uniqueIndexes[value] = Object.keys(uniqueIndexes).length;
|
|
122
|
-
}
|
|
123
|
-
value = uniqueIndexes[value];
|
|
124
|
-
obj[key] = value;
|
|
125
|
-
} else if (config.inputTypes[key] === 'boolean') {
|
|
121
|
+
if (config.inputTypes[key] === 'boolean') {
|
|
126
122
|
obj[key] = Number(value);
|
|
127
123
|
}
|
|
128
124
|
|
|
@@ -226,7 +222,6 @@ const validateConfig = config => {
|
|
|
226
222
|
"inputTypes",
|
|
227
223
|
"min",
|
|
228
224
|
"max",
|
|
229
|
-
"uniqueStrIdx",
|
|
230
225
|
"groupMinMax",
|
|
231
226
|
"repeat",
|
|
232
227
|
"groups",
|
|
@@ -248,7 +243,6 @@ const validateConfig = config => {
|
|
|
248
243
|
inputTypes,
|
|
249
244
|
min,
|
|
250
245
|
max,
|
|
251
|
-
uniqueStrIdx,
|
|
252
246
|
groupMinMax,
|
|
253
247
|
repeat,
|
|
254
248
|
groups,
|
|
@@ -277,9 +271,6 @@ const validateConfig = config => {
|
|
|
277
271
|
if (!isPlainObject(max)) {
|
|
278
272
|
throw new Error("max must be an object.");
|
|
279
273
|
}
|
|
280
|
-
if (!isPlainObject(uniqueStrIdx)) {
|
|
281
|
-
throw new Error("uniqueStrIdx must be an object.");
|
|
282
|
-
}
|
|
283
274
|
if (!isPlainObject(groupMinMax)) {
|
|
284
275
|
throw new Error("groupMinMax must be an object.");
|
|
285
276
|
}
|
package/test/test.js
CHANGED
|
@@ -5,7 +5,8 @@ import * as tf from '@tensorflow/tfjs-node'
|
|
|
5
5
|
|
|
6
6
|
const test = async () => {
|
|
7
7
|
|
|
8
|
-
const myArray = (await loadFile({fileName: '1d-spy.json', pathName: 'datasets'}))
|
|
8
|
+
const myArray = (await loadFile({fileName: '1d-spy.json', pathName: 'datasets'}))
|
|
9
|
+
.map(({open, high, low, close}) => ({open, high, low, close})) //file in /datasets/1d-spy.json
|
|
9
10
|
|
|
10
11
|
//callback function used to prepare X before scaling
|
|
11
12
|
const xCallbackFunc = ({ objRow, index }) => {
|
|
@@ -15,10 +16,9 @@ const test = async () => {
|
|
|
15
16
|
//returning null or undefined will exclude current row X and Y from training
|
|
16
17
|
if(typeof prev === 'undefined') return null
|
|
17
18
|
|
|
18
|
-
const { open, high, low, close
|
|
19
|
+
const { open, high, low, close } = curr
|
|
19
20
|
|
|
20
21
|
return {
|
|
21
|
-
date,
|
|
22
22
|
open,
|
|
23
23
|
high,
|
|
24
24
|
low,
|