xy-scale 1.4.45 → 1.4.46
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/index.js +2 -1
- package/package.json +1 -1
- package/src/datasets.js +4 -4
- package/src/{zscore2.js → zscore.js} +7 -7
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,{arrayShuffle:()=>t,
|
|
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:()=>t,arrayToTimesteps:()=>h,parseProductionX:()=>w,parseTrainingXY:()=>d,zscore:()=>f});const t=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},n=e=>null!=e&&Number.isFinite(e),o=e=>Number.isInteger(e)&&e>0,a=e=>null!==e&&"object"==typeof e&&!Array.isArray(e)&&Object.keys(e).length>0,i=(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 ${r} items.`);if(e.length>t)throw new Error(`Invalid property value. Array "${n}" expected at max ${t} items.`);return!0},l=e=>{if(!a(e))throw new Error("The first item in arrObj is expeted to be a key par object.");const r={},t={};for(const[n,o]of Object.entries(e))"number"==typeof o?r[n]=o:t[n]=o;if(Object.keys(r).length>0&&s(r,"validateFirstRow"))throw new Error("Invalid numeric value at index 0.");if(Object.keys(t).length>0&&c(t,"validateFirstRow"))throw new Error("Invalid non-numeric value at index 0.");return!0},s=(e,r)=>{if(null==r)throw new Error('[hasInvalidNumbers] Missing required param "callerName".');if(!a(e))throw new Error(`[${r}:hasInvalidNumbers] Received an invalid "list" param — expected a non-empty key-pair object, got: ${JSON.stringify(e)}.`);for(const[t,o]of Object.entries(e))if(!n(o))return console.error(`[${r}:hasInvalidNumbers] property "${t}" only accept numbers. Invalid value is "${o}" and invalid type is "${typeof o}".`),!0;return!1},c=(e,r)=>{if(null==r)throw new Error('[hasNullOrUndefined] Missing required param "callerName".');if(!a(e))throw new Error(`[${r}:hasNullOrUndefined] Received an invalid "list" param — expected a non-empty key-pair object, got: ${JSON.stringify(e)}.`);for(const[t,n]of Object.entries(e))if(a(n)){for(const[e,o]of Object.entries(n))if(null==o)return console.error(`[${r}:hasNullOrUndefined] Null or undefined value detected for key "${t}.${e}".\n${String(o)}`),!0}else if(null==n)return console.error(`[${r}:hasNullOrUndefined] Null or undefined value detected for key "${t}".\n${String(n)}`),!0;return!1},u=e=>Array.from(e),y=(e,r)=>{const t=new Float64Array(r),n=new Float64Array(r),o=new Float64Array(r),a=new Uint32Array(r),i=Array.isArray(e?.mean)?e.mean:[],l=Array.isArray(e?.std)?e.std:[],s=Array.isArray(e?.scale)?e.scale:[],c=Array.isArray(e?.count)?e.count:[];for(let e=0;e<r;e++){const r=Number(i[e]),u=Number(l[e]),y=Number(s[e]),f=Number(c[e]);t[e]=Number.isFinite(r)?r:0,n[e]=Number.isFinite(u)&&u>0?u:0,a[e]=Number.isFinite(f)&&f>0?f:0,Number.isFinite(y)&&y>0?(o[e]=y,n[e]>0||(n[e]=1/y)):n[e]>0?o[e]=1/n[e]:o[e]=0}return{mean:t,std:n,scale:o,count:a}},f=(e,r=null)=>{if(!Array.isArray(e))throw new TypeError('[zscore] "arr" must be a 2D array.');if(0===e.length){const e=y(r??{},0);return{stats:{mean:u(e.mean),std:u(e.std),scale:u(e.scale),count:u(e.count)},data:[]}}const t=e[0];if(!Array.isArray(t))throw new TypeError('[zscore] "arr" must be a 2D array of rows.');const n=t.length,o=r?y(r,n):((e,r)=>{const t=e.length,n=new Float64Array(r),o=new Float64Array(r),a=new Uint32Array(r);for(let i=0;i<t;i++){const t=e[i];if(!Array.isArray(t))throw new TypeError(`[zscore] Invalid row at index=${i}. Expected an array.`);if(t.length!==r)throw new Error(`[zscore] Inconsistent row size at index=${i}. Expected ${r}, got ${t.length}.`);for(let e=0;e<r;e++){const r=Number(t[e]);if(!Number.isFinite(r))continue;a[e]++;const i=r-n[e];n[e]+=i/a[e],o[e]+=i*(r-n[e])}}const i=new Float64Array(r),l=new Float64Array(r);for(let e=0;e<r;e++){const r=a[e]>1?o[e]/a[e]:0,t=r>0?Math.sqrt(r):0;i[e]=t,l[e]=t>0?1/t:0}return{mean:n,std:i,scale:l,count:a}})(e,n),a=((e,r)=>{const t=e.length,n=r.mean.length,o=new Array(t);for(let a=0;a<t;a++){const t=e[a];if(!Array.isArray(t))throw new TypeError(`[zscore] Invalid row at index=${a}. Expected an array.`);if(t.length!==n)throw new Error(`[zscore] Inconsistent row size at index=${a}. Expected ${n}, got ${t.length}.`);const i=new Array(n);for(let e=0;e<n;e++){const n=Number(t[e]),o=r.scale[e];i[e]=Number.isFinite(n)&&o>0?(n-r.mean[e])*o:0}o[a]=i}return o})(e,o);return{stats:{mean:u(o.mean),std:u(o.std),scale:u(o.scale),count:u(o.count)},data:a}},d=({arrObj:e=[],trainSize:r=null,testSize:n=null,yCallbackFunc:a=e=>e,xCallbackFunc:c=e=>e,validateRows:u=()=>!0,shuffle:y=!1,state:d={},showSource:w=!1,scaling:h=null})=>{i(e,{min:2},"parseTrainingXY"),l(e[0]);const p=e.length;if((({arrObjSize:e,trainSize:r,testSize:t})=>{if(!o(r))throw new Error(`Invalid property: "trainSize" (${r}) must be a non-negative integer.`);if(!o(t))throw new Error(`Invalid property: "testSize" (${t}) must be a non-negative integer.`);if(!Number.isInteger(e)||e<0)throw new Error(`Invalid property: "arrObjSize" (${e}) must be a non-negative integer.`);if(e<r+t)throw new Error(`Invalid property: The sum of "trainSize" + "testSize" (${r+t}) must not be larger than "arrObj.length" (${e}).`)})({arrObjSize:p,trainSize:r,testSize:n}),![null,"zscore"].includes(h))throw new Error('Invalid "scaling" property. Accepting null or "zscore".');const g=r+n;let m=[],b=[],A=[],v=null,$=null;const E={};for(let r=0;r<e.length;r++)try{if(!u({objRow:e,index:r,state:d}))continue;const t=c({objRow:e,index:r,state:d}),n=a({objRow:e,index:r,state:d});if(null==t||null==n)continue;if(s(t,"parseTrainingXY"))throw new Error('Invalid numeric value returned from "xCallbackFunc".');if(null===v&&(v=Object.keys(t)),null===$){$=Object.keys(n);for(let e=0;e<$.length;e++)E[$[e]]={}}const o=v.length,i=$.length,l=new Array(o),y=new Array(i);for(let e=0;e<o;e++){const r=v[e];l[e]=t[r]}for(let e=0;e<i;e++){const r=$[e],t=n[r];y[e]=t;const o=Array.isArray(t)?JSON.stringify(t):String(t);E[r][o]=(E[r][o]??0)+1}m.push(l),b.push(y),w&&A.push(e[r])}catch(e){throw new Error(`[BUG] - Skipped row index=${r}: ${e.message}`)}if(y){const e=new Array(m.length);for(let r=0;r<m.length;r++)e[r]={x:m[r],y:b[r]},w&&(e[r].source=A[r]);const r=t(e);m=new Array(r.length),b=new Array(r.length),w&&(A=new Array(r.length));for(let e=0;e<r.length;e++)m[e]=r[e].x,b[e]=r[e].y,w&&(A[e]=r[e].source)}const N={keyNames:v??[]},S={keyNames:$??[],labelCounts:E},x=p-g;m.splice(0,x),b.splice(0,x);let j=m.slice(0,r),z=b.slice(0,r),O=null;if("zscore"===h){let e=f(j);O=e.stats,j=e.data,e=null}let I=m.slice(-n);"zscore"===h&&(I=f(I,O).data);let k,F,T=b.slice(-n);return m=null,b=null,w&&(A.splice(0,x),k=A.slice(0,r),F=A.slice(-n),A=null),{trainX:j,trainY:z,testX:I,testY:T,configX:N,configY:S,trainSource:k,testSource:F,stats:O}},w=({arrObj:e=[],xCallbackFunc:r=e=>e,yCallbackFunc:n=null,validateRows:o=()=>!0,shuffle:a=!1,state:c={},showSource:u=!1,scaling:y=null,stats:d})=>{let w=[],h=[],p=null;if(i(e,{min:1},"parseProductionX"),l(e[0]),![null,"zscore"].includes(y))throw new Error('Invalid "scaling" property. Accepting null or "zscore".');if(e.length,null!=n)throw new Error('The property "yCallbackFunc" must not be set in "parseProductionX".');for(let t=0;t<e.length;t++)try{if(!o({objRow:e,index:t,state:c}))continue;const n=r({objRow:e,index:t,state:c});if(null==n)continue;if(s(n,"parseProductionX"))throw new Error('Invalid numeric value returned from "xCallbackFunc".');null===p&&(p=Object.keys(n));const a=p.length,i=new Array(a);for(let e=0;e<a;e++){const r=p[e];i[e]=n[r]}w.push(i),u&&h.push(e[t])}catch(e){throw new Error(`[BUG] - Skipped row index=${t}: ${e.message}`)}if(a){const e=new Array(w.length);for(let r=0;r<w.length;r++)e[r]={x:w[r]},u&&(e[r].source=h[r]);const r=t(e);w=new Array(r.length),u&&(h=new Array(r.length));for(let e=0;e<r.length;e++)w[e]=r[e].x,u&&(h[e]=r[e].source)}const g={keyNames:p??[]};return"zscore"===y&&(w=f(w,d).data),{X:w,source:h,configX:g}},h=(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 o=0;o<=e.length-r;o+=t)n.push(e.slice(o,o+r));return n};XY_Scale=r})();
|
package/index.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { parseTrainingXY, parseProductionX } from "./src/datasets.js"
|
|
2
2
|
import {arrayToTimesteps } from "./src/timeSteps.js"
|
|
3
3
|
import { arrayShuffle } from "./src/utilities.js"
|
|
4
|
+
import { zscore } from "./src/zscore.js"
|
|
4
5
|
|
|
5
|
-
export { parseTrainingXY, parseProductionX, arrayToTimesteps, arrayShuffle }
|
|
6
|
+
export { parseTrainingXY, parseProductionX, arrayToTimesteps, arrayShuffle, zscore }
|
package/package.json
CHANGED
package/src/datasets.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { arrayShuffle } from "./utilities.js";
|
|
2
2
|
import { validateFirstRow, validateArray, hasInvalidNumbers, validateSizes } from "./validators.js";
|
|
3
|
-
import {
|
|
3
|
+
import {zscore} from './zscore.js'
|
|
4
4
|
|
|
5
5
|
export const parseTrainingXY = ({
|
|
6
6
|
arrObj = [],
|
|
@@ -152,7 +152,7 @@ export const parseTrainingXY = ({
|
|
|
152
152
|
let stats = null
|
|
153
153
|
|
|
154
154
|
if(scaling === 'zscore') {
|
|
155
|
-
let trainNormalized =
|
|
155
|
+
let trainNormalized = zscore(trainX)
|
|
156
156
|
stats = trainNormalized.stats
|
|
157
157
|
trainX = trainNormalized.data
|
|
158
158
|
trainNormalized = null
|
|
@@ -161,7 +161,7 @@ export const parseTrainingXY = ({
|
|
|
161
161
|
let testX = flatX.slice(-testSize);
|
|
162
162
|
|
|
163
163
|
if(scaling === 'zscore') {
|
|
164
|
-
testX =
|
|
164
|
+
testX = zscore(testX, stats).data
|
|
165
165
|
}
|
|
166
166
|
|
|
167
167
|
let testY = flatY.slice(-testSize);
|
|
@@ -292,7 +292,7 @@ export const parseProductionX = ({
|
|
|
292
292
|
};
|
|
293
293
|
|
|
294
294
|
if(scaling === 'zscore') {
|
|
295
|
-
flatX =
|
|
295
|
+
flatX = zscore(flatX, stats).data
|
|
296
296
|
}
|
|
297
297
|
|
|
298
298
|
return {
|
|
@@ -44,10 +44,10 @@ const fitStats = (arr, cols) => {
|
|
|
44
44
|
const row = arr[i];
|
|
45
45
|
|
|
46
46
|
if (!Array.isArray(row)) {
|
|
47
|
-
throw new TypeError(`[
|
|
47
|
+
throw new TypeError(`[zscore] Invalid row at index=${i}. Expected an array.`);
|
|
48
48
|
}
|
|
49
49
|
if (row.length !== cols) {
|
|
50
|
-
throw new Error(`[
|
|
50
|
+
throw new Error(`[zscore] Inconsistent row size at index=${i}. Expected ${cols}, got ${row.length}.`);
|
|
51
51
|
}
|
|
52
52
|
|
|
53
53
|
for (let j = 0; j < cols; j++) {
|
|
@@ -84,10 +84,10 @@ const scaleFromStats = (arr, normalizedStats) => {
|
|
|
84
84
|
const row = arr[i];
|
|
85
85
|
|
|
86
86
|
if (!Array.isArray(row)) {
|
|
87
|
-
throw new TypeError(`[
|
|
87
|
+
throw new TypeError(`[zscore] Invalid row at index=${i}. Expected an array.`);
|
|
88
88
|
}
|
|
89
89
|
if (row.length !== cols) {
|
|
90
|
-
throw new Error(`[
|
|
90
|
+
throw new Error(`[zscore] Inconsistent row size at index=${i}. Expected ${cols}, got ${row.length}.`);
|
|
91
91
|
}
|
|
92
92
|
|
|
93
93
|
const normalized = new Array(cols);
|
|
@@ -106,9 +106,9 @@ const scaleFromStats = (arr, normalizedStats) => {
|
|
|
106
106
|
return out;
|
|
107
107
|
};
|
|
108
108
|
|
|
109
|
-
export const
|
|
109
|
+
export const zscore = (arr, stats = null) => {
|
|
110
110
|
if (!Array.isArray(arr)) {
|
|
111
|
-
throw new TypeError('[
|
|
111
|
+
throw new TypeError('[zscore] "arr" must be a 2D array.');
|
|
112
112
|
}
|
|
113
113
|
|
|
114
114
|
if (arr.length === 0) {
|
|
@@ -126,7 +126,7 @@ export const zscore2d = (arr, stats = null) => {
|
|
|
126
126
|
|
|
127
127
|
const firstRow = arr[0];
|
|
128
128
|
if (!Array.isArray(firstRow)) {
|
|
129
|
-
throw new TypeError('[
|
|
129
|
+
throw new TypeError('[zscore] "arr" must be a 2D array of rows.');
|
|
130
130
|
}
|
|
131
131
|
|
|
132
132
|
const cols = firstRow.length;
|