-
Notifications
You must be signed in to change notification settings - Fork 0
/
sdeDataUtilities.js
466 lines (414 loc) · 19.6 KB
/
sdeDataUtilities.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
// Extend the Array prototype to include a custom sortSamples method
Array.prototype.sortSamples = function(ds) {
// Use the built-in sort method on the array
return this.sort((a, b) => {
// Sorting by latitude
if (xAxisSort === 'latitude') {
const latitudeA = selectedSampleInfo[ds].position[a]['Position latitude'];
const latitudeB = selectedSampleInfo[ds].position[b]['Position latitude'];
return latitudeA - latitudeB;
}
// Sorting by longitude
if (xAxisSort === 'longitude') {
const latitudeA = selectedSampleInfo[ds].position[a]['Position longitude'];
const latitudeB = selectedSampleInfo[ds].position[b]['Position longitude'];
return latitudeA - latitudeB;
}
// Sorting by totalArea
if (xAxisSort === 'totalarea') {
const totalAreaA = selectedSampleMeasurements[ds]['Physical Data'].samples[a].totalArea;
const totalAreaB = selectedSampleMeasurements[ds]['Physical Data'].samples[b].totalArea;
return totalAreaA - totalAreaB;
}
// Default case if no valid sort key is provided
return 0;
});
};
// Extend the Array prototype to include a custom sortSamples method
Array.prototype.sortComplexSamples = function() {
// Use the built-in sort method on the array
return this.sort((a, b) => {
//console.log(a,b);
const partsA = a.split(": ");
const partsB = b.split(": ");
selectedSampleInfo[partsA[0]].position[partsA[1]]
//console.log(partsA);
//console.log(partsB);
// Sorting by latitude
if (xAxisSort === 'latitude') {
const latitudeA = selectedSampleInfo[partsA[0]].position[partsA[1]]['Position latitude'];
const latitudeB = selectedSampleInfo[partsB[0]].position[partsB[1]]['Position latitude'];
return latitudeA - latitudeB;
}
// Sorting by longitude
if (xAxisSort === 'longitude') {
const latitudeA = selectedSampleInfo[partsA[0]].position[partsA[1]]['Position longitude'];
const latitudeB = selectedSampleInfo[partsB[0]].position[partsB[1]]['Position longitude'];
return latitudeA - latitudeB;
}
// Sorting by totalArea
if (xAxisSort === 'totalarea') {
const totalAreaA = selectedSampleMeasurements[partsA[0]]['Physical Data'].samples[partsA[1]].totalArea;
const totalAreaB = selectedSampleMeasurements[partsB[0]]['Physical Data'].samples[partsB[1]].totalArea;
return totalAreaA - totalAreaB;
}
// Sorting by gravel
if (xAxisSort === 'gravel') {
const gravelA = selectedSampleMeasurements[partsA[0]]['Physical Data'].samples[partsA[1]].splitWeights['Gravel'];
const gravelB = selectedSampleMeasurements[partsB[0]]['Physical Data'].samples[partsB[1]].splitWeights['Gravel'];
return gravelA - gravelB;
}
// Sorting by silt
if (xAxisSort === 'silt') {
const siltA = selectedSampleMeasurements[partsA[0]]['Physical Data'].samples[partsA[1]].splitWeights['Silt And Clay'];
const siltB = selectedSampleMeasurements[partsB[0]]['Physical Data'].samples[partsB[1]].splitWeights['Silt And Clay'];
return siltA - siltB;
}
// Sorting by sand
if (xAxisSort === 'sand') {
const splitWeightsA = selectedSampleMeasurements[partsA[0]]['Physical Data'].samples[partsA[1]].splitWeights;
const splitWeightsB = selectedSampleMeasurements[partsB[0]]['Physical Data'].samples[partsB[1]].splitWeights;
const sandA = splitWeightsA['Fine And Very Fine Sand'] + splitWeightsA['Medium Sand'] +
splitWeightsA['Very Coarse And Coarse Sand'];
const sandB = splitWeightsB['Fine And Very Fine Sand'] + splitWeightsB['Medium Sand'] +
splitWeightsB['Very Coarse And Coarse Sand'];
return sandA - sandB;
}
// Sorting by sand
if (xAxisSort === 'slitsand') {
const splitWeightsA = selectedSampleMeasurements[partsA[0]]['Physical Data'].samples[partsA[1]].splitWeights;
const splitWeightsB = selectedSampleMeasurements[partsB[0]]['Physical Data'].samples[partsB[1]].splitWeights;
const sandA = splitWeightsA['Fine And Very Fine Sand'] + splitWeightsA['Medium Sand'] +
splitWeightsA['Very Coarse And Coarse Sand'] + splitWeightsA['Silt And Clay'];
const sandB = splitWeightsB['Fine And Very Fine Sand'] + splitWeightsB['Medium Sand'] +
splitWeightsB['Very Coarse And Coarse Sand'] + splitWeightsB['Silt And Clay'];
return sandA - sandB;
}
// Sorting by datelatitude
if (partsA[0] === partsB[0]) {
if (xAxisSort === 'datelatitude') {
const latitudeA = selectedSampleInfo[partsA[0]].position[partsA[1]]['Position latitude'];
const latitudeB = selectedSampleInfo[partsB[0]].position[partsB[1]]['Position latitude'];
return latitudeA - latitudeB;
}
} else {
if (partsA[0] > partsB[0]) {
return 1
} else {
return -1
}
}
// Sorting by datelongitude
if (partsA[0] === partsB[0]) {
if (xAxisSort === 'datelongitude') {
const longitudeA = selectedSampleInfo[partsA[0]].position[partsA[1]]['Position longitude'];
const longitudeB = selectedSampleInfo[partsB[0]].position[partsB[1]]['Position longitude'];
return longitudeA - longitudeB;
}
} else {
if (partsA[0] > partsB[0]) {
return 1
} else {
return -1
}
}
// Sorting by datetototalarea
if (partsA[0] === partsB[0]) {
if (xAxisSort === 'datetotalarea') {
const totalAreaA = selectedSampleMeasurements[partsA[0]]['Physical Data'].samples[partsA[1]].totalArea;
const totalAreaB = selectedSampleMeasurements[partsB[0]]['Physical Data'].samples[partsB[1]].totalArea;
return totalAreaA - totalAreaB;
}
} else {
if (partsA[0] > partsB[0]) {
return 1
} else {
return -1
}
}
// Default case if no valid sort key is provided
return 0;
});
};
// Define ranges for different materials
let ranges = {
'Gravel': [0, 9],
'Very Coarse And Coarse Sand': [10, 13],
'Medium Sand': [14, 15],
'Fine And Very Fine Sand': [16, 19],
'Silt And Clay': [20, 40]
// Ignore < 0.4um particles
};
// Function to calculate sum of a range
function sumInRange(psd, range) {
let sum = 0;
for (let i = range[0]; i <= range[1]; i++) {
if (i < psd.length) {
sum += psd[i];
}
}
return sum;
}
function psdSplit(psd) {
let split = {};
// Calculate sums for each category
split['Silt And Clay'] = sumInRange(psd, ranges['Silt And Clay']);
split['Fine And Very Fine Sand'] = sumInRange(psd, ranges['Fine And Very Fine Sand']);
split['Medium Sand'] = sumInRange(psd, ranges['Medium Sand']);
split['Very Coarse And Coarse Sand'] = sumInRange(psd, ranges['Very Coarse And Coarse Sand']);
split['Gravel'] = sumInRange(psd, ranges['Gravel']);
return split
}
function psdPostProcess(currentPsd, sizes) {
ptsSizes = null;
ptsAreas = null;
ptsVolumes = null;
splitWeights = {};
splitAreas = {};
// Ignore < 0.4um particles
sizes = sizes.slice(0,-1);
//console.log(sizes);
//Sizes are in mm so convert to SI
ptsSizes = sizes.map(phiSize => Math.pow(2, -phiSize)/1000);
ptsAreas = ptsSizes.map(size => Math.PI * size * size);
ptsVolumes = ptsSizes.map(size => (Math.PI * size * size * size) / 6);
//console.log(ptsSizes,ptsAreas,ptsVolumes);
areas = [];
totalArea = 0;
cumAreas = [];
cumWeights = [];
relativeAreas = [];
for (i = 0; i < ptsSizes.length; i++) {
// noPts = currentPsd[i] / volumes[i];
// Divide by 100 as psd in weight %
// Divide by 1000 as 1kg is 1000th of tonne which is 1m^3 with density of 1
currentArea = (ptsAreas[i] * currentPsd[i]) / (ptsVolumes[i] * 100 * 1000);
areas[i] = currentArea;
totalArea += currentArea;
cumAreas[i] = 0;
cumWeights[i] = 0;
relativeAreas[i] = 0;
}
// cumAreas = Array(areas.length);
previousCumArea = 0;
// cumWeights = Array(areas.length);
previousCumWeight = 0;
for (i = ptsSizes.length-1; i > -1; i--) {
relativeAreas[i] = areas[i]*100/totalArea;
cumAreas[i] = previousCumArea + relativeAreas[i];
previousCumArea = cumAreas[i];
cumWeights[i] = previousCumWeight + currentPsd[i];
previousCumWeight = cumWeights[i];
}
splitWeights = psdSplit(currentPsd);
splitAreas = psdSplit(areas);
splitRelativeAreas = psdSplit(relativeAreas);
console.log( areas, relativeAreas, splitWeights, splitAreas, splitRelativeAreas, cumAreas, cumWeights, totalArea );
return { areas, relativeAreas, splitWeights, splitAreas, splitRelativeAreas, cumAreas, cumWeights, totalArea }
}
function pcbPostProcess(newMeas,dateSampled) {
sheetName = 'PCB data';
//console.log(newMeas);
mmeas = newMeas;
sums = {};
const ICES7 = ["2,2',5,5'-Tetrachlorobiphenyl", "2,4,4'-Trichlorobiphenyl", "2,2',3,4,4',5,5'-Heptachlorobiphenyl",
"2,2',4,4',5,5'-Hexachlorobiphenyl", "2,2',3,4,4',5'-Hexachlorobiphenyl",
"2,3',4,4',5-Pentachlorobiphenyl", "2,2',4,5,5'-Pentachlorobiphenyl"];
//console.log(mmeas);
for (const chemical in mmeas.chemicals) {
for (const sample in mmeas.chemicals[chemical].samples) {
//console.log(chemical,sample);
if (!sums[sample]) {
sums[sample] = {
ICES7: 0,
All: 0
};
}
// console.log(chemical,sample);
// console.log(mmeas.chemicals[chemical].samples[sample]);
// const congenerConcentration = meas.chemicals[chemical].samples[sample].reduce((acc, val) => acc + val, 0);
const congenerConcentration = mmeas.chemicals[chemical].samples[sample] || 0;
if (ICES7.includes(chemical)) {
sums[sample].ICES7 += congenerConcentration;
}
sums[sample].All += congenerConcentration;
}
}
sampleMeasurements[dateSampled][sheetName].congenerTest = sums;
}
function pahPostProcess(newMeas,dateSampled) {
sheetName = 'PAH data';
const chemicals = sampleMeasurements[dateSampled][sheetName].chemicals;
if ('Acenapthene' in chemicals) {
//If totalHC not read then set to zero
if (!('totalHC' in sampleMeasurements[dateSampled][sheetName])) {
thc = {};
// sampleMeasurements[dateSampled][sheetName].totalHC = [];
for(sample in sampleMeasurements[dateSampled][sheetName].chemicals['Acenapthene'].samples) {
thc[sample] = 0.0;
// totalHC[sample] = 0.0;
}
sampleMeasurements[dateSampled][sheetName].totalHC = thc;
}
// mmeas = newMeas[sheetName];
mmeas = newMeas;
sums = {};
// Goring Test protocol here, but results stored by sample
const lmw = ['Acenaphthene', 'Acenaphthylene', 'Anthracene', 'Fluorene', 'C1-Naphthalenes', 'Naphthalene', 'Phenanthrene'];
const hmw = ['Benz[a]anthracene', 'Benzo[a]pyrene', 'Chrysene', 'Dibenz[a,h]anthracene', 'Fluoranthene', 'Pyrene'];
for (const chemical in mmeas.chemicals) {
for (const sample in mmeas.chemicals[chemical].samples) {
//console.log(chemical,sample);
if (!sums[sample]) {
sums[sample] = {
lmwSum: 0,
hmwSum: 0
};
}
//console.log(meas[chemical][sample]);
if (lmw.includes(chemical)) {
const lmwConcentrationSum = mmeas.chemicals[chemical].samples[sample] || 0;
sums[sample].lmwSum += lmwConcentrationSum;
} else if (hmw.includes(chemical)) {
const hmwConcentrationSum = mmeas.chemicals[chemical].samples[sample] || 0;
sums[sample].hmwSum += hmwConcentrationSum;
}
}
}
sampleMeasurements[dateSampled][sheetName].gorhamTest = sums;
// const chemicals = sampleMeasurements[dateSampled][sheetName].chemicals;
sampleMeasurements[dateSampled][sheetName].ratios = {};
sampleMeasurements[dateSampled][sheetName].ringSums = {};
sampleMeasurements[dateSampled][sheetName].simpleRatios = {};
const allSamples = Object.keys(sampleInfo[dateSampled].position);
allSamples.sort();
allSamples.forEach(s => {
const ace = chemicals['Acenapthene'].samples[s];//3
const aceph = chemicals['Acenapthylene'].samples[s];//3
const anth = chemicals['Anthracene'].samples[s];//3//Ant
const baa = chemicals['Benz[a]anthracene'].samples[s];//4
const bap = chemicals['Benzo[a]pyrene'].samples[s];//5
const bbf = chemicals['Benzo[b]fluoranthene'].samples[s];//4
const bghip = chemicals['Benzo[g,h,i]perylene'].samples[s];//6//Bgp
const bkf = chemicals['Benzo[k]fluoranthene'].samples[s];//4
const chr = chemicals['Chrysene'].samples[s];//4
const dba = chemicals['Dibenz[a,h]anthracene'].samples[s];//5
const fl = chemicals['Fluorene'].samples[s];//3
const flu = chemicals['Fluoranthene'].samples[s];//4
const ip = chemicals['Indeno[123-c,d]pyrene'].samples[s];//5//Inp
const phen = chemicals['Phenanthrene'].samples[s];//3
const naph = chemicals['Napthalene'].samples[s];//2
const pyr = chemicals['Pyrene'].samples[s];//4
m = {};
//Diagnostic ratios
//IP/(IP+B(ghi)P)
m['IP/(IP+B(ghi)P)'] = ip / (ip + bghip);
//BaA/(BaA+Chr)
m['BaA/(BaA+Chr)'] = baa / (baa + chr);
//BaP/(BaP+Chr)
m['BaP/(BaP+Chr)'] = bap / (bap + chr);
//Phen/(Phen+Anth)
m['Phen/(Phen+Anth)'] = phen / (phen + anth);
//BaA/(BaA+BaP)
m['BaA/(BaA+BaP)'] = baa / (baa + bap);
//BbF/(BbF+BkF)
m['BbF/(BbF+BkF)'] = bbf / (bbf + bkf);
sampleMeasurements[dateSampled][sheetName].ratios[s] = m;
m = {};
// Dash Sums: L'PAHs - Phen + Anth + Flu + Pyr; H'PAHs - BaA + Chr + BbF + BkF + BaP + IP + DBA + BgP
m['LdPAHs'] = phen + anth + flu + pyr;
m['HdPAHs'] = baa + chr + bbf + bkf + bap + ip + dba + bghip;
m['Total d PAHs'] = m['LdPAHs'] + m['HdPAHs'];
// EPS Sums: LPAHs - Naph, Aceph, Ace, Fl, Phen and Ant; HPAHs - Flu, Pyr, BaA, Chr, BbF, BkF, BaP, DBA, BgP and Inp
m['LPAHs'] = naph + aceph + ace + fl + phen + anth;
m['HPAHs'] = flu + pyr + baa + chr + bbf + bkf + bap + dba + bghip + ip;
m['Total EPA PAHs'] = m['LPAHs'] + m['HPAHs'];
// Ring Sums
m['Sum of 2 rings'] = naph;//2
m['Sum of 3 rings'] = ace + aceph + anth + fl + phen;//3
m['Sum of 4 rings'] = baa + bbf + bkf + chr + flu + pyr;//4
m['Sum of 5 rings'] = bap + dba + ip;//5
m['Sum of 6 rings'] = bghip;//6
m['Total all rings'] = m['Sum of 2 rings'] + m['Sum of 3 rings'] + m['Sum of 4 rings'] + m['Sum of 5 rings'] + m['Sum of 6 rings'];
sampleMeasurements[dateSampled][sheetName].ringSums[s] = m;
m = {};
m['Phen/Anth'] = phen / anth;
m['Flu/Pyr'] = flu / pyr;
m['Baa/Chr'] = baa / chr;
if (chemicals['Benzo[e]pyrene'] === undefined || chemicals['Benzo[e]pyrene'] === null) {
const bep = chemicals['Benzo[e]pyrene'].sample[s];
m['Bep/Bap'] = bep / bap;
} else {
m['Bep/Bap'] = 0;
}
sampleMeasurements[dateSampled][sheetName].simpleRatios[s] = m;
});
}
}
function dualfitConcentration(concentration, totalArea, totalHC, ignoreSamples = []) {
// Extract sample keys and filter out ignored samples
const samples = Object.keys(concentration).filter(sample => !ignoreSamples.includes(sample));
console.log(samples,samples.length);
/* // Extract sample keys
const samples = Object.keys(concentration);*/
// Create Y vector (concentration values)
const Y = samples.map(sample => concentration[sample]);
// Create X matrix with [totalArea, totalHC, 1] for each sample
const X = samples.map(sample => [totalArea[sample], totalHC[sample], 1]);
// Compute X transpose (X^T)
const XT = math.transpose(X);
// Compute (X^T * X)
const XTX = math.multiply(XT, X);
// Compute (X^T * X)^-1
const XTX_inv = math.inv(XTX);
// Compute (X^T * Y)
const XTY = math.multiply(XT, Y);
// Compute beta = (X^T * X)^-1 * (X^T * Y)
const beta = math.multiply(XTX_inv, XTY);
// Extract coefficients
const a = beta[0];
const b = beta[1];
const c = beta[2];
// Calculate predicted values and residuals
const Y_pred = samples.map(sample => a * totalArea[sample] + b * totalHC[sample] + c);
const Y_mean = math.mean(Y);
// Compute the total sum of squares (SS_tot) and residual sum of squares (SS_res)
const SS_tot = math.sum(Y.map(y => Math.pow(y - Y_mean, 2)));
const SS_res = math.sum(Y.map((y, i) => Math.pow(y - Y_pred[i], 2)));
// Calculate R-squared
const R_squared = 1 - (SS_res / SS_tot);
return { a, b, c, R_squared };
}
function concentrationFitter(concentration, predictors, ignoreSamples = []) {
console.log(concentration, predictors);
// Extract sample keys and filter out ignored samples
const samples = Object.keys(concentration).filter(sample => !ignoreSamples.includes(sample));
// Create Y vector (concentration values) excluding ignored samples
const Y = samples.map(sample => concentration[sample]);
// Create X matrix dynamically based on the predictors provided, adding a column of 1s for the intercept
const X = samples.map(sample => [...predictors[sample], 1]);
// Compute X transpose (X^T)
const XT = math.transpose(X);
// Compute (X^T * X)
const XTX = math.multiply(XT, X);
// Compute (X^T * X)^-1
const XTX_inv = math.inv(XTX);
// Compute (X^T * Y)
const XTY = math.multiply(XT, Y);
// Compute beta = (X^T * X)^-1 * (X^T * Y)
const beta = math.multiply(XTX_inv, XTY);
// Calculate predicted values and residuals (only for included samples)
const Y_pred = samples.map(sample => {
// Calculate predicted concentration using the fitted model coefficients
return predictors[sample].reduce((sum, value, index) => sum + beta[index] * value, beta[beta.length - 1]);
});
const Y_mean = math.mean(Y);
// Compute the total sum of squares (SS_tot) and residual sum of squares (SS_res)
const SS_tot = math.sum(Y.map(y => Math.pow(y - Y_mean, 2)));
const SS_res = math.sum(Y.map((y, i) => Math.pow(y - Y_pred[i], 2)));
// Calculate R-squared
const R_squared = 1 - (SS_res / SS_tot);
// Return the coefficients (beta) and the R-squared value
// return { coefficients: beta, R_squared };
return { beta, R_squared };
}