xy-scale 1.2.4 → 1.2.6
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 +4 -2
- package/src/scale.js +39 -20
- package/test/test.js +2 -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:()=>f,descaleArrayObj:()=>p,parseProductionX:()=>u,parseTrainingXY:()=>i});const t=({arrObj:e,repeat:r={},minmaxRange:t=[0,1],groups:i={},prevConfig:u=null})=>{const
|
|
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:()=>f,descaleArrayObj:()=>p,parseProductionX:()=>u,parseTrainingXY:()=>i});const t=({arrObj:e,repeat:r={},minmaxRange:t=[0,1],groups:i={},prevConfig:u=null,customMinMaxRanges:p=null})=>{const f=[...e],m=f.length,c=f[0],l="object"==typeof p&&null!==p;if(0===m)return{scaledOutput:[],scaledConfig:{}};let g={};const y=u&&o(u);if(y)s(u,{minmaxRange:t,repeat:r,groups:i,firstRow:c}),g={...u};else{const e=Object.keys(c),n=e.map((e=>r.hasOwnProperty(e)?Math.max(r[e],1):1)),a=n.reduce(((e,r)=>e+r),0);g={arrObjLen:m,rangeMin:t[0],rangeMax:t[1],inputTypes:{},min:{},max:{},uniqueStrIdx:{},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);for(const e of g.inputKeyNames){const r=typeof c[e],t=a(e,g.groups);if(y){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,"string"===r&&(g.uniqueStrIdx[e]={}),l&&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 f)for(const r of g.inputKeyNames){let t=e[r];if("string"===g.inputTypes[r]){const n=g.uniqueStrIdx[r];n.hasOwnProperty(t)||(n[t]=Object.keys(n).length),t=n[t],e[r]=t}else"boolean"===g.inputTypes[r]&&(e[r]=Number(t));const n=a(r,g.groups);(!1===l||l&&!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 h=new Array(m);for(let e=0;e<m;e++){const r=f[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 f=p!==u?g.rangeMin+(s-u)/(p-u)*(g.rangeMax-g.rangeMin):g.rangeMin,m=g.repeatedKeyNames[e];for(let e=0;e<m;e++)t[n++]=f}h[e]=t}return{scaledOutput:h,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","uniqueStrIdx","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,uniqueStrIdx:i,groupMinMax:u,repeat:p,groups:f,inputKeyNames:m,outputKeyNames:c,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 g=e=>"object"==typeof e&&null!==e&&!Array.isArray(e);if(!g(a))throw new Error("inputTypes must be an object.");if(!g(o))throw new Error("min must be an object.");if(!g(s))throw new Error("max must be an object.");if(!g(i))throw new Error("uniqueStrIdx must be an object.");if(!g(u))throw new Error("groupMinMax must be an object.");if(!g(p))throw new Error("repeat must be an object.");if(!g(f))throw new Error("groups must be an object.");if(!Array.isArray(m))throw new Error("inputKeyNames must be an array.");if(!Array.isArray(c))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,xCallbackFunc:o,validateRows:s=e=>e,groups:i,shuffle:u=!1,minmaxRange:p,balancing:f="",state:m={},customMinMaxRanges:c})=>{let l=[],g=[];for(let r=0;r<e.length;r++){if(!s(e[r]))continue;const t=o({objRow:e,index:r,state:m}),n=a({objRow:e,index:r,state:m});null!=t&&null!=n&&(l.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]))}})(l,g);l=e,g=r}let{scaledOutput:y,scaledConfig:h}=t({arrObj:l,repeat:n,groups:i,minmaxRange:p}),{scaledOutput:d,scaledConfig:w}=t({arrObj:g,repeat:n,groups:i,minmaxRange:p});const b=Math.floor(y.length*r);let x=y.slice(0,b),M=d.slice(0,b),O=y.slice(b),j=d.slice(b);if(f){let e;if("oversample"===f)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}})(x,M),x=e.X,M=e.Y;else{if("undersample"!==f)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}})(x,M),x=e.X,M=e.Y}}return{trainX:x,trainY:M,testX:O,testY:j,configX:h,configY:w}},u=({arrObj:e,repeat:r,xCallbackFunc:n,validateRows:a=e=>e,groups:o,shuffle:s=!1,minmaxRange:i,state:u={},prevConfig:p,customMinMaxRanges:f})=>{let m=[];for(let r=0;r<e.length;r++){if(!a(e[r]))continue;const t=n({objRow:e,index:r,state:u});t&&m.push(t)}s&&(m=(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})(m));const{scaledOutput:c,scaledConfig:l}=t({arrObj:m,repeat:r,groups:o,minmaxRange:i,prevConfig:p});return{X:c,configX:l,keyNamesX}},p=({scaled:e,config:r,keyNames:t})=>{const{min:n,max:a,std:o,mean:s,approach:i,inputTypes:u,uniqueStringIndexes:p}=r;return e.map((e=>{const r={};let f=0;for(const m of Object.keys(n)){const c=i[m],l=n[m],g=a[m],y=s[m],h=o[m],d=t.filter((e=>e===m)).length;let w=0;for(let r=0;r<d;r++)w+=e[f++];const b=w/d;let x;if("normalization"===c?x=b*(g-l)+l:"standardization"===c&&(x=b*h+y),"string"===u[m]){const e=Object.keys(p[m]).find((e=>p[m][e]===x));x=void 0!==e?e:x}r[m]=x}return r}))},f=(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
|
@@ -13,7 +13,8 @@ export const parseTrainingXY = ({
|
|
|
13
13
|
shuffle = false,
|
|
14
14
|
minmaxRange,
|
|
15
15
|
balancing = '',
|
|
16
|
-
state = {}
|
|
16
|
+
state = {},
|
|
17
|
+
customMinMaxRanges
|
|
17
18
|
}) => {
|
|
18
19
|
let X = [];
|
|
19
20
|
let Y = [];
|
|
@@ -105,7 +106,8 @@ export const parseProductionX = ({
|
|
|
105
106
|
shuffle = false,
|
|
106
107
|
minmaxRange,
|
|
107
108
|
state = {},
|
|
108
|
-
prevConfig
|
|
109
|
+
prevConfig,
|
|
110
|
+
customMinMaxRanges
|
|
109
111
|
}) => {
|
|
110
112
|
let X = [];
|
|
111
113
|
|
package/src/scale.js
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
|
-
export const scaleArrayObj = ({ arrObj, repeat = {}, minmaxRange = [0, 1], groups = {}, prevConfig = null }) => {
|
|
1
|
+
export const scaleArrayObj = ({ arrObj, repeat = {}, minmaxRange = [0, 1], groups = {}, prevConfig = null, customMinMaxRanges = null }) => {
|
|
2
2
|
|
|
3
3
|
const arrObjClone = [...arrObj]
|
|
4
|
-
const
|
|
4
|
+
const arrObjLen = arrObjClone.length;
|
|
5
5
|
const firstRow = arrObjClone[0]
|
|
6
|
+
const validCustomMinMaxRanges = typeof customMinMaxRanges === 'object' && customMinMaxRanges !== null
|
|
6
7
|
|
|
7
|
-
if (
|
|
8
|
+
if (arrObjLen === 0) {
|
|
8
9
|
return {
|
|
9
10
|
scaledOutput: [],
|
|
10
11
|
scaledConfig: {}
|
|
@@ -31,6 +32,7 @@ export const scaleArrayObj = ({ arrObj, repeat = {}, minmaxRange = [0, 1], group
|
|
|
31
32
|
const countRepeatedKeyNames = repeatedKeyNames.reduce((sum, rep) => sum + rep, 0);
|
|
32
33
|
|
|
33
34
|
config = {
|
|
35
|
+
arrObjLen,
|
|
34
36
|
rangeMin: minmaxRange[0],
|
|
35
37
|
rangeMax: minmaxRange[1],
|
|
36
38
|
inputTypes: {},
|
|
@@ -43,7 +45,7 @@ export const scaleArrayObj = ({ arrObj, repeat = {}, minmaxRange = [0, 1], group
|
|
|
43
45
|
inputKeyNames,
|
|
44
46
|
outputKeyNames: new Array(countRepeatedKeyNames),
|
|
45
47
|
repeatedKeyNames,
|
|
46
|
-
}
|
|
48
|
+
}
|
|
47
49
|
|
|
48
50
|
let keyNamesIdx = 0;
|
|
49
51
|
|
|
@@ -62,6 +64,7 @@ export const scaleArrayObj = ({ arrObj, repeat = {}, minmaxRange = [0, 1], group
|
|
|
62
64
|
for (const key of config.inputKeyNames) {
|
|
63
65
|
|
|
64
66
|
const firstType = typeof firstRow[key]
|
|
67
|
+
const thisGroup = findGroup(key, config.groups);
|
|
65
68
|
|
|
66
69
|
if(isValidPrevConfig)
|
|
67
70
|
{
|
|
@@ -84,15 +87,27 @@ export const scaleArrayObj = ({ arrObj, repeat = {}, minmaxRange = [0, 1], group
|
|
|
84
87
|
if (firstType === 'string') {
|
|
85
88
|
config.uniqueStrIdx[key] = {};
|
|
86
89
|
}
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
if (thisGroup) {
|
|
91
|
-
config.groupMinMax[thisGroup] = { min: Infinity, max: -Infinity };
|
|
92
|
-
} else
|
|
90
|
+
|
|
91
|
+
if(validCustomMinMaxRanges && customMinMaxRanges.hasOwnProperty(key))
|
|
93
92
|
{
|
|
94
|
-
|
|
95
|
-
|
|
93
|
+
if (thisGroup)
|
|
94
|
+
{
|
|
95
|
+
config.groupMinMax[thisGroup] = customMinMaxRanges[key]
|
|
96
|
+
}
|
|
97
|
+
else
|
|
98
|
+
{
|
|
99
|
+
config.min[key] = customMinMaxRanges[key].min;
|
|
100
|
+
config.max[key] = -customMinMaxRanges[key].max;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
else {
|
|
104
|
+
if (thisGroup) {
|
|
105
|
+
config.groupMinMax[thisGroup] = { min: Infinity, max: -Infinity };
|
|
106
|
+
}
|
|
107
|
+
else {
|
|
108
|
+
config.min[key] = Infinity;
|
|
109
|
+
config.max[key] = -Infinity;
|
|
110
|
+
}
|
|
96
111
|
}
|
|
97
112
|
}
|
|
98
113
|
|
|
@@ -113,18 +128,22 @@ export const scaleArrayObj = ({ arrObj, repeat = {}, minmaxRange = [0, 1], group
|
|
|
113
128
|
|
|
114
129
|
const thisGroup = findGroup(key, config.groups);
|
|
115
130
|
|
|
116
|
-
if (
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
131
|
+
if(validCustomMinMaxRanges === false || (validCustomMinMaxRanges && !customMinMaxRanges.hasOwnProperty(key)))
|
|
132
|
+
{
|
|
133
|
+
if (thisGroup) {
|
|
134
|
+
config.groupMinMax[thisGroup].min = Math.min(config.groupMinMax[thisGroup].min, value);
|
|
135
|
+
config.groupMinMax[thisGroup].max = Math.max(config.groupMinMax[thisGroup].max, value);
|
|
136
|
+
} else {
|
|
137
|
+
config.min[key] = Math.min(config.min[key], value);
|
|
138
|
+
config.max[key] = Math.max(config.max[key], value);
|
|
139
|
+
}
|
|
122
140
|
}
|
|
141
|
+
|
|
123
142
|
}
|
|
124
143
|
}
|
|
125
144
|
|
|
126
|
-
const scaledOutput = new Array(
|
|
127
|
-
for (let i = 0; i <
|
|
145
|
+
const scaledOutput = new Array(arrObjLen);
|
|
146
|
+
for (let i = 0; i < arrObjLen; i++) {
|
|
128
147
|
const obj = arrObjClone[i];
|
|
129
148
|
const scaledRow = new Array(config.outputKeyNames.length);
|
|
130
149
|
let idx = 0;
|