xy-scale 1.4.36 → 1.4.38
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 +28 -4
- package/test/test.js +5 -1
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:()=>
|
|
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,{arrayShuffle:()=>n,arrayShuffleXY:()=>a,arrayToTimesteps:()=>I,parseProductionX:()=>v,parseTrainingXY:()=>$});const t=e=>null==e||!Number.isFinite(e),n=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},a=(e,r)=>{if(!Array.isArray(e)||!Array.isArray(r))throw new TypeError("Both X and Y must be arrays");if(e.length!==r.length)throw new Error("X and Y must have the same length");const t=[...e],n=[...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]],[n[e],n[r]]=[n[r],n[e]]}return{X:t,Y:n}},o=e=>"string"==typeof e||"boolean"==typeof e||("number"==typeof e?Number.isFinite(e):!!Array.isArray(e)&&e.every(o)),s=({random:e,seed:r}={})=>"function"==typeof e?e:Number.isInteger(r)?(e=>{let r=e>>>0;return()=>{r+=1831565813;let e=Math.imul(r^r>>>15,1|r);return e^=e+Math.imul(e^e>>>7,61|e),((e^e>>>14)>>>0)/4294967296}})(r):Math.random,l=(e,r=Math.random)=>{for(let t=e.length-1;t>0;t--){const n=Math.floor(r()*(t+1));[e[t],e[n]]=[e[n],e[t]]}return e},i=(e,r,t=Math.random)=>{if(r>e.length)throw new Error("Cannot sample more items than available without replacement.");const n=[...e];return l(n,t),n.slice(0,r)},c=(e,r,t=Math.random)=>{if(0===e.length)throw new Error("Cannot sample from an empty array.");const n=[];for(let a=0;a<r;a++){const r=Math.floor(t()*e.length);n.push(e[r])}return n},h=(e,r)=>r?structuredClone(e):e,u=(e,r,{cloneX:t=!1}={})=>{((e,r)=>{if(!Array.isArray(e)||!Array.isArray(r))throw new Error("X and Y must be arrays.");if(e.length!==r.length)throw new Error("X and Y must have the same length.");if(0===e.length)throw new Error("X and Y cannot be empty.")})(e,r);const n=new Map;return r.forEach(((r,a)=>{const s=(e=>{if(!o(e))throw new Error("Invalid Y label. Allowed types: finite numbers, strings, booleans, or nested arrays of those.");return JSON.stringify(e)})(r);n.has(s)||n.set(s,[]),n.get(s).push({x:h(e[a],t),y:r})})),n},f=(e,{min:r=-1/0,max:t=1/0},n)=>{if(!Array.isArray(e))throw new Error(`Invalid property. "${n}" expected an array.`);if(e.length<r)throw new Error(`Invalid property value. Array "${n}" expected at least ${t} items.`);if(e.length>t)throw new Error(`Invalid property value. Array "${n}" expected at max ${t} items.`);return!0},d=e=>{for(const[r,t]of Object.entries(e)){if("number"==typeof t&&Number.isNaN(t))throw new Error(`Invalid value at index 0 property "${r}": value is "${t}". Expected a numeric value.`);if(null===t)throw new Error(`Invalid value at index 0 property "${r}": value is "${t}".`)}return!0},y=e=>{return Array.isArray(e)?"array":(r=e,"[object Object]"===Object.prototype.toString.call(r)?"object":typeof e);var r},p=e=>Object.keys(e).filter((e=>"tempIdx"!==e)),g=({parentPath:e="",key:r,isArrayParent:t=!1})=>`${e}${t?`[${r}]`:e?`.${r}`:`${r}`}`,m=({rowLabel:e,sourceIndex:r,path:t="",detail:n,strictFlagName:a})=>new Error(`${e} schema error at index "${r}"${t?` path "${t}"`:""}. ${n} Set "${a}" to false to disable this validation.`),w=({referenceRow:e,currentRow:r,rowLabel:t,currentIndex:n,strictFlagName:a})=>{const o=y(e),s=y(r);if(o!==s)throw m({rowLabel:t,sourceIndex:n,detail:`Expected row type "${o}" based on the first parsed ${t} row, but got "${s}".`,strictFlagName:a});if("array"!==o&&"object"!==o)throw m({rowLabel:t,sourceIndex:n,detail:`Parsed ${t} rows must be arrays or plain objects.`,strictFlagName:a});const l=p(e),i=p(r),c=new Set(l),h=new Set(i),u=Array.isArray(e);for(let e=0;e<l.length;e++){const r=l[e];if(!h.has(r))throw m({rowLabel:t,sourceIndex:n,path:g({key:r,isArrayParent:u}),detail:`Missing required ${u?"index":"property"} "${r}" found in the first parsed ${t} row.`,strictFlagName:a})}for(let e=0;e<i.length;e++){const r=i[e];if(!c.has(r))throw m({rowLabel:t,sourceIndex:n,path:g({key:r,isArrayParent:u}),detail:`Unexpected ${u?"index":"property"} "${r}" not present in the first parsed ${t} row.`,strictFlagName:a})}return l},b=e=>Array.isArray(e)?"array":typeof e,x=({referenceValue:e,currentValue:r,sourceIndex:t,path:n,strictFlagName:a})=>{const o=b(e),s=b(r);if(o!==s)throw m({rowLabel:"Y",sourceIndex:t,path:n,detail:`Expected type "${o}" based on the first parsed Y row, but got "${s}".`,strictFlagName:a});if("array"!==o){if("number"!==o&&"boolean"!==o&&"string"!==o)throw m({rowLabel:"Y",sourceIndex:t,path:n,detail:`Unsupported Y value type "${o}". Y values must be numbers, booleans, strings, or nested arrays of those types.`,strictFlagName:a})}else{if(e.length!==r.length)throw m({rowLabel:"Y",sourceIndex:t,path:n,detail:`Expected array length "${e.length}" based on the first parsed Y row, but got "${r.length}".`,strictFlagName:a});for(let o=0;o<e.length;o++)x({referenceValue:e[o],currentValue:r[o],sourceIndex:t,path:g({parentPath:n,key:o,isArrayParent:!0}),strictFlagName:a})}},A=({rows:e,sourceIndexes:r,strictFlagName:t})=>{if(!e.length)return;const n=e[0],a=(e,r)=>{w({referenceRow:n,currentRow:e,rowLabel:"X",currentIndex:r,strictFlagName:t})};e.length>1&&a(e[e.length-1],r[e.length-1]);for(let t=0;t<e.length;t++)a(e[t],r[t])},$=({arrObj:e=[],trainingSplit:r=.8,yCallbackFunc:a=e=>e,xCallbackFunc:o=e=>e,validateRows:y=()=>!0,shuffle:m=!1,balancing:b="",strictXSchema:$=!0,strictYSchema:v=!0,state:I={}})=>{let X=[],N=[];const Y=[];f(e,{min:5},"parseTrainingXY"),d(e[0]);for(let r=0;r<e.length;r++){if(!y({objRow:e,index:r,state:I}))continue;const t=o({objRow:e,index:r,state:I}),n=a({objRow:e,index:r,state:I});null!=t&&null!=n&&(X.push(t),N.push(n),Y.push(r))}if($&&A({rows:X,sourceIndexes:Y,strictFlagName:"strictXSchema"}),v&&(({rows:e,sourceIndexes:r,strictFlagName:t})=>{if(!e.length)return;const n=e[0],a=(e,r)=>{const a=w({referenceRow:n,currentRow:e,rowLabel:"Y",currentIndex:r,strictFlagName:t}),o=Array.isArray(n);for(let s=0;s<a.length;s++){const l=a[s],i=g({key:l,isArrayParent:o});x({referenceValue:n[l],currentValue:e[l],sourceIndex:r,path:i,strictFlagName:t})}};e.length>1&&a(e[e.length-1],r[e.length-1]);for(let t=0;t<e.length;t++)a(e[t],r[t])})({rows:N,sourceIndexes:Y,strictFlagName:"strictYSchema"}),m){const e=new Array(X.length);for(let r=0;r<X.length;r++)e[r]={x:X[r],y:N[r],sourceIndex:Y[r]};const r=n(e);X=new Array(r.length),N=new Array(r.length);for(let e=0;e<r.length;e++)X[e]=r[e].x,N[e]=r[e].y,Y[e]=r[e].sourceIndex}const F=X.length,E=N.length,S=F?p(X[0]):[],j=E?p(N[0]):[],M=new Array(F),k=new Array(E),P={keyNames:S},R={keyNames:j};for(let e=0;e<F;e++){const r=X[e],n=Y[e],a=new Array(S.length);for(let e=0;e<S.length;e++){const o=S[e],s=r[o];if(t(s))throw new Error(`Invalid property value (${s}) returned from "xCallbackFunc" on index "${n}" property "${o}".`);a[e]=s}M[e]=a}for(let e=0;e<E;e++){const r=N[e],t=new Array(j.length);for(let e=0;e<j.length;e++)t[e]=r[j[e]];k[e]=t}const O=Math.floor(M.length*r);let L=M.slice(0,O),C=k.slice(0,O),T=M.slice(O),V=k.slice(O);if(b){let e;if("oversample"===b)e=((e,r,t={})=>{const{random:n,seed:a,shuffleResult:o=!0,cloneX:i=!1}=t,f=s({random:n,seed:a}),d=u(e,r,{cloneX:i}),y=[...d.values()].map((e=>e.length)),p=Math.max(...y),g=[];for(const e of d.values()){const r=[...e],t=p-r.length,n=t>0?c(e,t,f).map((e=>({x:h(e.x,i),y:e.y}))):[];g.push(...r,...n)}return o&&l(g,f),{X:g.map((({x:e})=>e)),Y:g.map((({y:e})=>e))}})(L,C),L=e.X,C=e.Y;else{if("undersample"!==b)throw Error('balancing argument only accepts "", "oversample" and "undersample". Defaults to "".');e=((e,r,t={})=>{const{random:n,seed:a,shuffleResult:o=!0,cloneX:c=!1}=t,f=s({random:n,seed:a}),d=u(e,r,{cloneX:c}),y=[...d.values()].map((e=>e.length)),p=Math.min(...y),g=[];for(const e of d.values()){const r=i(e,p,f).map((e=>({x:h(e.x,c),y:e.y})));g.push(...r)}return o&&l(g,f),{X:g.map((({x:e})=>e)),Y:g.map((({y:e})=>e))}})(L,C),L=e.X,C=e.Y}}return{trainX:L,trainY:C,testX:T,testY:V,configX:P,configY:R}},v=({arrObj:e=[],xCallbackFunc:r=e=>e,yCallbackFunc:a=null,validateRows:o=()=>!0,shuffle:s=!1,strictXSchema:l=!0,state:i={}})=>{let c=[],h=[];if(f(e,{min:5},"parseProductionX"),d(e[0]),null!=a)throw new Error('The property "yCallbackFunc" must not be set in "parseProductionX".');for(let t=0;t<e.length;t++){if(!o({objRow:e,index:t,state:i}))continue;const n=r({objRow:e,index:t,state:i});null!=n&&!1!==n&&(c.push(n),h.push(t))}if(l&&A({rows:c,sourceIndexes:h,strictFlagName:"strictXSchema"}),s){const e=new Array(c.length);for(let r=0;r<c.length;r++)e[r]={x:c[r],sourceIndex:h[r]};const r=n(e);c=new Array(r.length),h=new Array(r.length);for(let e=0;e<r.length;e++)c[e]=r[e].x,h[e]=r[e].sourceIndex}const u=c.length,y=u?p(c[0]):[],g=new Array(u),m={keyNames:y};for(let e=0;e<u;e++){const r=c[e],n=h[e],a=new Array(y.length);for(let e=0;e<y.length;e++){const o=y[e],s=r[o];if(t(s))throw new Error(`Invalid property value (${s}) returned from "xCallbackFunc" on index "${n}" property "${o}".`);a[e]=s}g[e]=a}return{X:g,configX:m}},I=(e,r,t=1)=>{if(!Array.isArray(e))throw new Error("arr must be an array");if(!Number.isInteger(r)||r<=0)throw new Error("timeSteps must be a positive integer");if(!Number.isInteger(t)||t<=0)throw new Error("step must be a positive integer");if(r>e.length)return[];const n=[];for(let a=0;a<=e.length-r;a+=t)n.push(e.slice(a,a+r));return n};XY_Scale=r})();
|
package/package.json
CHANGED
package/src/datasets.js
CHANGED
|
@@ -316,9 +316,7 @@ export const parseTrainingXY = ({
|
|
|
316
316
|
keyNames: xKeys,
|
|
317
317
|
};
|
|
318
318
|
|
|
319
|
-
|
|
320
|
-
keyNames: yKeys,
|
|
321
|
-
};
|
|
319
|
+
|
|
322
320
|
|
|
323
321
|
for (let idx = 0; idx < xLen; idx++) {
|
|
324
322
|
const rowObj = X[idx];
|
|
@@ -341,12 +339,33 @@ export const parseTrainingXY = ({
|
|
|
341
339
|
flatX[idx] = flatRow;
|
|
342
340
|
}
|
|
343
341
|
|
|
342
|
+
const toLabelKey = value => Array.isArray(value)
|
|
343
|
+
? JSON.stringify(value)
|
|
344
|
+
: String(value);
|
|
345
|
+
|
|
346
|
+
const initLabelCounts = keyNames =>
|
|
347
|
+
Object.fromEntries(
|
|
348
|
+
keyNames.map(keyName => [keyName, ({})])
|
|
349
|
+
);
|
|
350
|
+
|
|
351
|
+
const configY = {
|
|
352
|
+
keyNames: yKeys,
|
|
353
|
+
labelCounts: initLabelCounts(yKeys),
|
|
354
|
+
};
|
|
355
|
+
|
|
344
356
|
for (let idx = 0; idx < yLen; idx++) {
|
|
345
357
|
const rowObj = Y[idx];
|
|
346
358
|
const flatRow = new Array(yKeys.length);
|
|
347
359
|
|
|
348
360
|
for (let j = 0; j < yKeys.length; j++) {
|
|
349
|
-
|
|
361
|
+
const keyName = yKeys[j];
|
|
362
|
+
const value = rowObj[keyName];
|
|
363
|
+
|
|
364
|
+
flatRow[j] = value;
|
|
365
|
+
|
|
366
|
+
const labelKey = toLabelKey(value);
|
|
367
|
+
configY.labelCounts[keyName][labelKey] =
|
|
368
|
+
(configY.labelCounts[keyName][labelKey] ?? 0) + 1;
|
|
350
369
|
}
|
|
351
370
|
|
|
352
371
|
flatY[idx] = flatRow;
|
|
@@ -388,6 +407,7 @@ export const parseTrainingXY = ({
|
|
|
388
407
|
export const parseProductionX = ({
|
|
389
408
|
arrObj = [],
|
|
390
409
|
xCallbackFunc = row => row,
|
|
410
|
+
yCallbackFunc = null,
|
|
391
411
|
validateRows = () => true,
|
|
392
412
|
shuffle = false,
|
|
393
413
|
strictXSchema = true,
|
|
@@ -399,6 +419,10 @@ export const parseProductionX = ({
|
|
|
399
419
|
validateArray(arrObj, { min: 5 }, 'parseProductionX');
|
|
400
420
|
validateFirstRow(arrObj[0]);
|
|
401
421
|
|
|
422
|
+
if(yCallbackFunc != null) {
|
|
423
|
+
throw new Error('The property "yCallbackFunc" must not be set in "parseProductionX".')
|
|
424
|
+
}
|
|
425
|
+
|
|
402
426
|
for (let x = 0; x < arrObj.length; x++) {
|
|
403
427
|
if (!validateRows({ objRow: arrObj, index: x, state })) continue;
|
|
404
428
|
|
package/test/test.js
CHANGED
|
@@ -37,6 +37,7 @@ const test = async () => {
|
|
|
37
37
|
testX,
|
|
38
38
|
testY,
|
|
39
39
|
configX,
|
|
40
|
+
configY
|
|
40
41
|
} = parseTrainingXY({
|
|
41
42
|
arrObj,
|
|
42
43
|
trainingSplit: 0.50,
|
|
@@ -61,6 +62,7 @@ const test = async () => {
|
|
|
61
62
|
console.log('row_1', {features: trainX[0], labels: trainY[0]})
|
|
62
63
|
|
|
63
64
|
console.log(trainY.length, trainX.length)
|
|
65
|
+
console.log(configY)
|
|
64
66
|
|
|
65
67
|
const timeSteps = arrayToTimesteps(trainX, 10)
|
|
66
68
|
|
|
@@ -87,13 +89,15 @@ const xCallbackFunc = ({ objRow, index }) => {
|
|
|
87
89
|
//callback function used to prepare Y before flattening
|
|
88
90
|
const yCallbackFunc = ({ objRow, index }) => {
|
|
89
91
|
|
|
92
|
+
const curr = objRow[index]
|
|
90
93
|
const next = objRow[index + 1]
|
|
91
94
|
|
|
92
95
|
//returning null or undefined will exclude current row X and Y from training
|
|
93
96
|
if (typeof next === 'undefined') return null
|
|
94
97
|
|
|
95
98
|
return {
|
|
96
|
-
|
|
99
|
+
label_1: Number(next.close > next.open),
|
|
100
|
+
label_2: Number(next.close > curr.close)
|
|
97
101
|
}
|
|
98
102
|
}
|
|
99
103
|
|