/
Spectrum.js
100 lines (87 loc) · 2.53 KB
/
Spectrum.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import { xMinMaxValues } from 'ml-spectra-processing';
import { getData } from './getData';
import { getNormalized } from './getNormalized';
import { updateRangesInfo } from './updateRangesInfo';
/**
* Class allowing manipulate one IR spectrum
* @class spectrum
* @param {object} [json={}] - object containing a spectrum
* @param {Array} [json.x=[]] - x values
* @param {Array} [json.y=[]] - y values
*/
export class Spectrum {
/**
*
* @param {array} x
* @param {array} y
* @param {string} id
* @param {object} [options={}]
* @param {object} [options.meta={}]
* @param {object} [options.normalization]
* @param {object} [options.normalized]
*/
constructor(x, y, id, options = {}) {
const { meta = {}, normalization = {}, normalized } = options;
if (x && x.length > 1 && x[0] > x[x.length - 1]) {
this.x = x.slice().reverse();
this.y = y.slice().reverse();
} else {
this.x = x || [];
this.y = y || [];
}
this.id = id;
this.meta = meta;
this.normalizedBoundary = { x: { min: 0, max: 0 }, y: { min: 0, max: 0 } };
if (normalized) {
this.normalized = normalized;
this.updateNormalizedBoundary();
this.normalizedAllowedBoundary = this.normalizedBoundary;
} else {
this.updateNormalization(normalization);
}
this.updateMemory();
}
get minX() {
return this.x[0];
}
get maxX() {
return this.x[this.x.length - 1];
}
updateMemory() {
let original = ((this.x && this.x.length) || 0) * 16;
let normalized = this.normalized.x.length * 16;
this.memory = { original, normalized, total: original + normalized };
}
removeOriginal() {
this.x = undefined;
this.y = undefined;
this.updateMemory();
}
getXY() {
if (!Array.isArray(this.x) || !Array.isArray(this.y)) {
throw new Error('Can not get normalized data');
}
return { x: this.x, y: this.y };
}
updateRangesInfo(ranges) {
updateRangesInfo(this, ranges);
}
updateNormalization(normalization) {
const result = getNormalized(this, normalization);
this.normalized = result.data;
this.normalizedAllowedBoundary = result.allowedBoundary;
this.ranges = {};
this.updateMemory();
this.updateNormalizedBoundary();
}
getData(options) {
return getData(this, options);
}
updateNormalizedBoundary() {
this.normalizedBoundary.x = {
min: this.normalized.x[0],
max: this.normalized.x[this.normalized.x.length - 1],
};
this.normalizedBoundary.y = xMinMaxValues(this.normalized.y);
}
}