-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add toJSON and fromJSON methods
- Loading branch information
1 parent
1c6089a
commit 8848b4c
Showing
7 changed files
with
117 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
'use strict'; | ||
|
||
const Chromatogram = require('./Chromatogram'); | ||
|
||
/** | ||
* Parse from a JSON element to a new Chromatogram | ||
* @param {Array<object>} json - Result from the toJSON | ||
* @return {Chromatogram} - New parsed Chromatogram | ||
*/ | ||
function fromJSON(json) { | ||
let time = new Array(json.length); | ||
let tic = new Array(json.length); | ||
let ms = new Array(json.length); | ||
|
||
for (var i = 0; i < json.length; i++) { | ||
time[i] = json[i].time; | ||
tic[i] = json[i].tic; | ||
ms[i] = [new Array(json[i].mass.length), new Array(json[i].mass.length)]; | ||
for (var j = 0; j < json[i].mass.length; j++) { | ||
ms[i][0][j] = json[i].mass[j].mass; | ||
ms[i][1][j] = json[i].mass[j].intensity; | ||
} | ||
} | ||
|
||
let chrom = new Chromatogram(time); | ||
chrom.addSerie({ | ||
dimension: 1, | ||
name: 'tic', | ||
data: tic | ||
}); | ||
chrom.addSerie({ | ||
dimension: 2, | ||
name: 'ms', | ||
data: ms | ||
}); | ||
|
||
return chrom; | ||
} | ||
|
||
module.exports = fromJSON; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import test from 'ava'; | ||
import {Chromatogram, fromJSON} from '..'; | ||
|
||
// https://en.wikipedia.org/wiki/Cauchy_distribution | ||
function lorentzian(x, x0 = 0, gamma = 1) { | ||
return (gamma * gamma) / (Math.PI * gamma * (gamma * gamma + (x - x0) * (x - x0))); | ||
} | ||
|
||
test('toJSON - fromJSON', t => { | ||
const size = 30; | ||
const fourth = size / 4; | ||
let times = new Array(size); | ||
let tic = new Array(size); | ||
let ms = new Array(size); | ||
for (let i = 0; i < size; ++i) { | ||
times[i] = i; | ||
tic[i] = lorentzian(i, fourth) + 2 * lorentzian(i, 2 * fourth) + lorentzian(i, 3 * fourth); | ||
ms[i] = [[1, 2, 3], [1, 1, 1]]; | ||
} | ||
let chrom = new Chromatogram(times); | ||
chrom.addSerie({ | ||
dimension: 1, | ||
name: 'tic', | ||
data: tic | ||
}); | ||
chrom.addSerie({ | ||
dimension: 2, | ||
name: 'ms', | ||
data: ms | ||
}); | ||
|
||
let json = chrom.toJSON(); | ||
t.is(json.length, chrom.getTimes().length); | ||
t.is(json.length, 30); | ||
|
||
let newChrom = fromJSON(json); | ||
t.deepEqual(newChrom.getTimes(), chrom.getTimes()); | ||
t.deepEqual(newChrom.findSerieByName('tic'), chrom.findSerieByName('tic')); | ||
t.deepEqual(newChrom.findSerieByName('ms'), chrom.findSerieByName('ms')); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters