spectrum-generator
Advanced tools
Comparing version 3.1.0 to 3.1.3
121
lib/index.js
@@ -8,14 +8,8 @@ 'use strict'; | ||
var d3Random = require('d3-random'); | ||
var seedrandom = _interopDefault(require('seedrandom')); | ||
var XSAdd = _interopDefault(require('ml-xsadd')); | ||
/** | ||
* Add a baseline to the spectrum | ||
* @param {object} [data] - Your spectrum data in the format {x:[x1, x2, ...], y:[y1, y2, ...]} | ||
* @param {function} [baselineFct] - Mathematical function producing the baseline you want | ||
* @return {object} data | ||
*/ | ||
function addBaseline(data, baselineFct) { | ||
if (!baselineFct) return data; | ||
var xs = data.x; | ||
var ys = data.y; | ||
let xs = data.x; | ||
let ys = data.y; | ||
for (let i = 0; i < xs.length; i++) { | ||
@@ -27,11 +21,2 @@ ys[i] += baselineFct(xs[i]); | ||
/** | ||
* Add noise to the spectrum | ||
* @param {object} [data] - Your spectrum data in the format {x:[x1, x2, ...], y:[y1, y2, ...]} | ||
* @param {number} [percent = 0] - Noise's amplitude in percents of the spectrum max value | ||
* @param {object} [options={}] | ||
* @param {number} [options.seed] - Seed for a deterministic sequence of random number | ||
* @param {string} [options.distribution='uniform'] - Type of random: 'uniform' (true random), 'normal' (gaussian distribution), | ||
* @return {data} data | ||
*/ | ||
function addNoise(data, percent = 0, options = {}) { | ||
@@ -55,4 +40,4 @@ const { distribution = 'uniform', seed } = options; | ||
if (!percent) return data; | ||
var ys = data.y; | ||
var factor = (percent * findMax(ys)) / 100; | ||
let ys = data.y; | ||
let factor = (percent * findMax(ys)) / 100; | ||
for (let i = 0; i < ys.length; i++) { | ||
@@ -66,3 +51,3 @@ ys[i] += generateRandomNumber() * factor; | ||
return typeof seed === 'number' | ||
? func.source(seedrandom(seed))(...args) | ||
? func.source(new XSAdd(seed).random)(...args) | ||
: func(...args); | ||
@@ -90,5 +75,5 @@ } | ||
ratio, | ||
2 | ||
) | ||
) | ||
2, | ||
), | ||
), | ||
); | ||
@@ -98,38 +83,2 @@ } | ||
class SpectrumGenerator { | ||
/** | ||
* @class SpectrumGenerator | ||
* @constructor | ||
* @param {object} [options={}] | ||
* @param {number} [options.start=0] - First x value (inclusive) | ||
* @param {number} [options.end=1000] - Last x value (inclusive) | ||
* @param {function} [options.peakWidthFct=function(x){return(5)}] - Width of peak depending the x value | ||
* @param {number} [options.pointsPerUnit=5] - Number of values between each unit of the x axis | ||
* @param {number} [options.maxSize=1e7] - maximal array size | ||
* | ||
* @example | ||
* import SG from 'spectrum-generator'; | ||
* const sg = new SG({start: 0, end: 100, pointsPerUnit: 5, peakWidthFct: (x) => 1 + 3 * x / 1000 }); | ||
* sg.addPeak( [5, 50] ); | ||
* sg.addPeak([20, 100], { width: 3 }); | ||
* sg.addPeak([35, 100], { widthLeft: 10, widthRight: 30 }); | ||
* sg.addPeak([50, 10], { widthLeft: 5, widthRight: 5 }); | ||
* sg.addPeaks([ [70,20], [80,40], [90,10] ]); | ||
* sg.addNoise(10); | ||
* sg.addBaseline( (x) => x * x / 100 ); | ||
* var spectrum = sg.getSpectrum(); | ||
* | ||
* @example | ||
* import SG from 'spectrum-generator'; | ||
* const spectrum=SG.generateSpectrum([ [20,3], [30,2], [40,2] ], { | ||
* start: 0, | ||
* end: 100, | ||
* pointsPerUnit: 1, | ||
* noise: { | ||
* percent: 10, | ||
* distribution: 'normal', | ||
* seed: 42 | ||
* }, | ||
* baseline: (x) => 2 * x, | ||
* }) | ||
*/ | ||
constructor(options = {}) { | ||
@@ -143,5 +92,5 @@ options = Object.assign( | ||
peakWidthFct: () => 5, | ||
maxSize: 1e7 | ||
maxSize: 1e7, | ||
}, | ||
options | ||
options, | ||
); | ||
@@ -175,7 +124,2 @@ this.start = options.start; | ||
/** | ||
* Add a series of peaks to the spectrum. | ||
* @param {Array<Array<number>>} peaks | ||
* @return {this} | ||
*/ | ||
addPeaks(peaks) { | ||
@@ -191,11 +135,2 @@ if (!Array.isArray(peaks)) { | ||
/** | ||
* Add a single peak to the spectrum. | ||
* @param {Array<number>} peak | ||
* @param {object} [options={}] | ||
* @param {number} [options.width] Half-height width | ||
* @param {number} [options.widthLeft] Half-height width left (asymmetric peak) | ||
* @param {number} [options.widthRight] Half-height width right (asymmetric peak) | ||
* @return {this} | ||
*/ | ||
addPeak(peak, options = {}) { | ||
@@ -229,3 +164,3 @@ if (!Array.isArray(peak) || peak.length !== 2) { | ||
this.pointsPerUnit + | ||
(gaussianFactor * gaussianWidth) / 2 | ||
(gaussianFactor * gaussianWidth) / 2, | ||
); | ||
@@ -245,3 +180,3 @@ if (gaussianIndex >= 0 && gaussianIndex < gaussian.length) { | ||
this.pointsPerUnit + | ||
(gaussianFactor * gaussianWidth) / 2 | ||
(gaussianFactor * gaussianWidth) / 2, | ||
); | ||
@@ -259,2 +194,3 @@ if (gaussianIndex >= 0 && gaussianIndex < gaussian.length) { | ||
addBaseline(this.data, baselineFct); | ||
return this; | ||
} | ||
@@ -264,12 +200,5 @@ | ||
addNoise(this.data, percent, options); | ||
return this; | ||
} | ||
/** | ||
* Get the generated spectrum. | ||
* @param {object} [options={}] | ||
* @param {number} [options.threshold=0] - minimal ratio of Y to keep the value | ||
* @param {boolean} [options.copy=true] - If true, returns a copy of the spectrum. | ||
* Otherwise, return the internal value that can be mutated if subsequent calls to addPeak are made. | ||
* @return {object} | ||
*/ | ||
getSpectrum(options = {}) { | ||
@@ -295,3 +224,3 @@ if (typeof options === 'boolean') { | ||
x: this.data.x.slice(), | ||
y: this.data.y.slice() | ||
y: this.data.y.slice(), | ||
}; | ||
@@ -303,12 +232,6 @@ } else { | ||
/** | ||
* Resets the generator with an empty spectrum. | ||
* @return {this} | ||
*/ | ||
reset() { | ||
if (this.size > this.maxSize) { | ||
throw new Error( | ||
`Generated array has size ${this.size} larger than maxSize: ${ | ||
this.maxSize | ||
}` | ||
`Generated array has size ${this.size} larger than maxSize: ${this.maxSize}`, | ||
); | ||
@@ -319,3 +242,3 @@ } | ||
x: [], | ||
y: new Array(this.size).fill(0) | ||
y: new Array(this.size).fill(0), | ||
}); | ||
@@ -347,8 +270,2 @@ | ||
/** | ||
* Generates a spectrum and returns it | ||
* @param {Array<Array<number>>} peaks - list of peaks to put in the spectrum | ||
* @param {object} [options] - same options as new SpectrumGenerator | ||
* @return {object} spectrum | ||
*/ | ||
function generateSpectrum(peaks, options = {}) { | ||
@@ -360,3 +277,3 @@ const generator = new SpectrumGenerator(options); | ||
return generator.getSpectrum({ | ||
threshold: options.threshold | ||
threshold: options.threshold, | ||
}); | ||
@@ -363,0 +280,0 @@ } |
{ | ||
"name": "spectrum-generator", | ||
"version": "3.1.0", | ||
"version": "3.1.3", | ||
"description": "generate a spectrum from discrete peaks", | ||
"main": "lib/index.js", | ||
"module": "src/index.js", | ||
"types": "spectrum-generator.d.ts", | ||
"files": [ | ||
"spectrum-generator.d.ts", | ||
"lib", | ||
@@ -12,9 +14,9 @@ "src" | ||
"scripts": { | ||
"eslint": "eslint src", | ||
"build": "cheminfo-build --entry src/index.js --root SpectrumGenerator", | ||
"eslint": "eslint src --cache", | ||
"eslint-fix": "npm run eslint -- --fix", | ||
"example": "node -r esm examples/generateSpectrum.js", | ||
"prepublishOnly": "rollup -c", | ||
"test": "run-s test-only eslint", | ||
"test-coverage": "npm run test-only -- --coverage", | ||
"test-travis": "run-s test-coverage eslint", | ||
"test": "npm run test-coverage && npm run eslint", | ||
"test-coverage": "jest --coverage", | ||
"test-only": "jest" | ||
@@ -41,17 +43,20 @@ }, | ||
"devDependencies": { | ||
"babel-plugin-transform-es2015-modules-commonjs": "^6.26.2", | ||
"eslint": "^5.12.0", | ||
"eslint-config-cheminfo": "^1.20.0", | ||
"eslint-plugin-import": "^2.14.0", | ||
"eslint-plugin-jest": "^22.1.0", | ||
"eslint-plugin-no-only-tests": "^2.0.1", | ||
"esm": "^3.0.84", | ||
"jest": "^23.6.0", | ||
"npm-run-all": "^4.1.3", | ||
"rollup": "^1.1.0" | ||
"@babel/plugin-transform-modules-commonjs": "^7.8.3", | ||
"@types/jest": "^25.1.3", | ||
"cheminfo-build": "^1.0.6", | ||
"eslint": "^6.8.0", | ||
"eslint-config-cheminfo": "^2.0.4", | ||
"eslint-plugin-import": "^2.20.1", | ||
"eslint-plugin-jest": "^23.8.0", | ||
"eslint-plugin-no-only-tests": "^2.4.0", | ||
"eslint-plugin-prettier": "^3.1.2", | ||
"esm": "^3.2.25", | ||
"jest": "^25.1.0", | ||
"prettier": "^1.19.1", | ||
"rollup": "^1.31.1" | ||
}, | ||
"dependencies": { | ||
"d3-random": "^1.1.2", | ||
"seedrandom": "^2.4.4" | ||
"d3-random": "^2.0.1", | ||
"ml-xsadd": "2.0.0" | ||
} | ||
} |
# spectrum-generator | ||
[![NPM version][npm-image]][npm-url] | ||
[![build status][travis-image]][travis-url] | ||
[![Test coverage][codecov-image]][codecov-url] | ||
[![build status][ci-image]][ci-url] | ||
[![npm download][download-image]][download-url] | ||
generate a spectrum from discrete peaks. | ||
Generate a spectrum from discrete peaks. | ||
## Installation | ||
`$ npm install --save spectrum-generator` | ||
`$ npm i spectrum-generator` | ||
@@ -24,3 +23,8 @@ ## Usage | ||
const peaks = [[4, 10], [20, 30], [236, 1], [569, 76]]; | ||
const peaks = [ | ||
[4, 10], | ||
[20, 30], | ||
[236, 1], | ||
[569, 76], | ||
]; | ||
const spectrum = generateSpectrum(peaks, { pointsPerUnit: 1 }); | ||
@@ -37,3 +41,6 @@ ``` | ||
generator.addPeak([30, 56]); | ||
generator.addPeaks([[40, 12], [10, 1]]); | ||
generator.addPeaks([ | ||
[40, 12], | ||
[10, 1], | ||
]); | ||
const spectrum = generator.getSpectrum(); | ||
@@ -52,9 +59,7 @@ | ||
[npm-image]: https://img.shields.io/npm/v/spectrum-generator.svg?style=flat-square | ||
[npm-image]: https://img.shields.io/npm/v/spectrum-generator.svg | ||
[npm-url]: https://www.npmjs.com/package/spectrum-generator | ||
[travis-image]: https://img.shields.io/travis/cheminfo/spectrum-generator/master.svg?style=flat-square | ||
[travis-url]: https://travis-ci.org/cheminfo/spectrum-generator | ||
[codecov-image]: https://img.shields.io/codecov/c/github/cheminfo/spectrum-generator.svg?style=flat-square | ||
[codecov-url]: https://codecov.io/gh/cheminfo/spectrum-generator | ||
[download-image]: https://img.shields.io/npm/dm/spectrum-generator.svg?style=flat-square | ||
[ci-image]: https://github.com/cheminfo/spectrum-generator/workflows/Node.js%20CI/badge.svg?branch=master | ||
[ci-url]: https://github.com/cheminfo/spectrum-generator/actions?query=workflow%3A%22Node.js+CI%22 | ||
[download-image]: https://img.shields.io/npm/dm/spectrum-generator.svg | ||
[download-url]: https://www.npmjs.com/package/spectrum-generator |
@@ -18,17 +18,17 @@ import { SpectrumGenerator } from '..'; | ||
expect(() => new SpectrumGenerator({ pointsPerUnit: 0.5 })).toThrow( | ||
integerReg | ||
integerReg, | ||
); | ||
expect(() => new SpectrumGenerator({ pointsPerUnit: false })).toThrow( | ||
integerReg | ||
integerReg, | ||
); | ||
expect(() => new SpectrumGenerator({ start: 0, end: 0 })).toThrow( | ||
endStartReg | ||
endStartReg, | ||
); | ||
expect(() => new SpectrumGenerator({ start: 0, end: -10 })).toThrow( | ||
endStartReg | ||
endStartReg, | ||
); | ||
expect(() => new SpectrumGenerator({ peakWidthFct: null })).toThrow( | ||
peakWidthReg | ||
peakWidthReg, | ||
); | ||
@@ -35,0 +35,0 @@ }); |
@@ -11,3 +11,3 @@ /* eslint-disable jest/expect-expect */ | ||
end: 10, | ||
peak: 5 | ||
peak: 5, | ||
}); | ||
@@ -20,3 +20,3 @@ }); | ||
end: 15, | ||
peak: 10 | ||
peak: 10, | ||
}); | ||
@@ -29,3 +29,3 @@ }); | ||
end: -5, | ||
peak: -10 | ||
peak: -10, | ||
}); | ||
@@ -41,3 +41,3 @@ }); | ||
pointsPerUnit: 10, | ||
getWidth: () => 0.1 | ||
getWidth: () => 0.1, | ||
}); | ||
@@ -59,6 +59,6 @@ expect(Math.max(...spectrum.y)).toBe(1); | ||
pointsPerUnit: 1000, | ||
getWidth: () => 0.1 | ||
}) | ||
getWidth: () => 0.1, | ||
}), | ||
).toThrow( | ||
'Generated array has size 10000001 larger than maxSize: 10000000' | ||
'Generated array has size 10000001 larger than maxSize: 10000000', | ||
); | ||
@@ -74,4 +74,4 @@ }); | ||
maxSize: 1, | ||
getWidth: () => 0.1 | ||
}) | ||
getWidth: () => 0.1, | ||
}), | ||
).toThrow('Generated array has size 3 larger than maxSize: 1'); | ||
@@ -86,3 +86,3 @@ }); | ||
pointsPerUnit: 1, | ||
getWidth: simpleGetWidth | ||
getWidth: simpleGetWidth, | ||
}); | ||
@@ -89,0 +89,0 @@ assertSize(spectrum, end - start + 1); |
@@ -9,3 +9,3 @@ /* eslint-disable jest/expect-expect */ | ||
end: 2, | ||
pointsPerUnit: 5 | ||
pointsPerUnit: 5, | ||
}); | ||
@@ -23,3 +23,3 @@ | ||
end: 2, | ||
pointsPerUnit: 5 | ||
pointsPerUnit: 5, | ||
}); | ||
@@ -37,3 +37,3 @@ | ||
end: 2, | ||
pointsPerUnit: 5 | ||
pointsPerUnit: 5, | ||
}); | ||
@@ -51,3 +51,3 @@ | ||
end: 100, | ||
pointsPerUnit: 2 | ||
pointsPerUnit: 2, | ||
}); | ||
@@ -64,3 +64,3 @@ generator.addPeak([35, 100], { widthLeft: 10, widthRight: 30 }); | ||
pointsPerUnit: 10, | ||
peakWidthFct: (x) => 1 + (3 * x) / 1000 | ||
peakWidthFct: (x) => 1 + (3 * x) / 1000, | ||
}); | ||
@@ -80,3 +80,3 @@ | ||
end: 5, | ||
pointsPerUnit: 5 | ||
pointsPerUnit: 5, | ||
}); | ||
@@ -102,3 +102,6 @@ | ||
generator.addPeak([50, 12]); | ||
generator.addPeaks([[100, 10], [14, 2]]); | ||
generator.addPeaks([ | ||
[100, 10], | ||
[14, 2], | ||
]); | ||
@@ -119,3 +122,3 @@ const spectrum = generator.getSpectrum(); | ||
maxSize: 1e8, | ||
peakWidthFct: () => 0.001 | ||
peakWidthFct: () => 0.001, | ||
}); | ||
@@ -125,3 +128,6 @@ | ||
generator.addPeak([50, 12]); | ||
generator.addPeaks([[100, 10], [14, 2]]); | ||
generator.addPeaks([ | ||
[100, 10], | ||
[14, 2], | ||
]); | ||
@@ -128,0 +134,0 @@ const spectrum = generator.getSpectrum({ threshold: 0.001 }); |
@@ -15,5 +15,5 @@ import addBaseline from './util/addBaseline.js'; | ||
ratio, | ||
2 | ||
) | ||
) | ||
2, | ||
), | ||
), | ||
); | ||
@@ -23,38 +23,2 @@ } | ||
export class SpectrumGenerator { | ||
/** | ||
* @class SpectrumGenerator | ||
* @constructor | ||
* @param {object} [options={}] | ||
* @param {number} [options.start=0] - First x value (inclusive) | ||
* @param {number} [options.end=1000] - Last x value (inclusive) | ||
* @param {function} [options.peakWidthFct=function(x){return(5)}] - Width of peak depending the x value | ||
* @param {number} [options.pointsPerUnit=5] - Number of values between each unit of the x axis | ||
* @param {number} [options.maxSize=1e7] - maximal array size | ||
* | ||
* @example | ||
* import SG from 'spectrum-generator'; | ||
* const sg = new SG({start: 0, end: 100, pointsPerUnit: 5, peakWidthFct: (x) => 1 + 3 * x / 1000 }); | ||
* sg.addPeak( [5, 50] ); | ||
* sg.addPeak([20, 100], { width: 3 }); | ||
* sg.addPeak([35, 100], { widthLeft: 10, widthRight: 30 }); | ||
* sg.addPeak([50, 10], { widthLeft: 5, widthRight: 5 }); | ||
* sg.addPeaks([ [70,20], [80,40], [90,10] ]); | ||
* sg.addNoise(10); | ||
* sg.addBaseline( (x) => x * x / 100 ); | ||
* var spectrum = sg.getSpectrum(); | ||
* | ||
* @example | ||
* import SG from 'spectrum-generator'; | ||
* const spectrum=SG.generateSpectrum([ [20,3], [30,2], [40,2] ], { | ||
* start: 0, | ||
* end: 100, | ||
* pointsPerUnit: 1, | ||
* noise: { | ||
* percent: 10, | ||
* distribution: 'normal', | ||
* seed: 42 | ||
* }, | ||
* baseline: (x) => 2 * x, | ||
* }) | ||
*/ | ||
constructor(options = {}) { | ||
@@ -68,5 +32,5 @@ options = Object.assign( | ||
peakWidthFct: () => 5, | ||
maxSize: 1e7 | ||
maxSize: 1e7, | ||
}, | ||
options | ||
options, | ||
); | ||
@@ -100,7 +64,2 @@ this.start = options.start; | ||
/** | ||
* Add a series of peaks to the spectrum. | ||
* @param {Array<Array<number>>} peaks | ||
* @return {this} | ||
*/ | ||
addPeaks(peaks) { | ||
@@ -116,11 +75,2 @@ if (!Array.isArray(peaks)) { | ||
/** | ||
* Add a single peak to the spectrum. | ||
* @param {Array<number>} peak | ||
* @param {object} [options={}] | ||
* @param {number} [options.width] Half-height width | ||
* @param {number} [options.widthLeft] Half-height width left (asymmetric peak) | ||
* @param {number} [options.widthRight] Half-height width right (asymmetric peak) | ||
* @return {this} | ||
*/ | ||
addPeak(peak, options = {}) { | ||
@@ -154,3 +104,3 @@ if (!Array.isArray(peak) || peak.length !== 2) { | ||
this.pointsPerUnit + | ||
(gaussianFactor * gaussianWidth) / 2 | ||
(gaussianFactor * gaussianWidth) / 2, | ||
); | ||
@@ -170,3 +120,3 @@ if (gaussianIndex >= 0 && gaussianIndex < gaussian.length) { | ||
this.pointsPerUnit + | ||
(gaussianFactor * gaussianWidth) / 2 | ||
(gaussianFactor * gaussianWidth) / 2, | ||
); | ||
@@ -184,2 +134,3 @@ if (gaussianIndex >= 0 && gaussianIndex < gaussian.length) { | ||
addBaseline(this.data, baselineFct); | ||
return this; | ||
} | ||
@@ -189,12 +140,5 @@ | ||
addNoise(this.data, percent, options); | ||
return this; | ||
} | ||
/** | ||
* Get the generated spectrum. | ||
* @param {object} [options={}] | ||
* @param {number} [options.threshold=0] - minimal ratio of Y to keep the value | ||
* @param {boolean} [options.copy=true] - If true, returns a copy of the spectrum. | ||
* Otherwise, return the internal value that can be mutated if subsequent calls to addPeak are made. | ||
* @return {object} | ||
*/ | ||
getSpectrum(options = {}) { | ||
@@ -220,3 +164,3 @@ if (typeof options === 'boolean') { | ||
x: this.data.x.slice(), | ||
y: this.data.y.slice() | ||
y: this.data.y.slice(), | ||
}; | ||
@@ -228,12 +172,6 @@ } else { | ||
/** | ||
* Resets the generator with an empty spectrum. | ||
* @return {this} | ||
*/ | ||
reset() { | ||
if (this.size > this.maxSize) { | ||
throw new Error( | ||
`Generated array has size ${this.size} larger than maxSize: ${ | ||
this.maxSize | ||
}` | ||
`Generated array has size ${this.size} larger than maxSize: ${this.maxSize}`, | ||
); | ||
@@ -244,3 +182,3 @@ } | ||
x: [], | ||
y: new Array(this.size).fill(0) | ||
y: new Array(this.size).fill(0), | ||
}); | ||
@@ -272,8 +210,2 @@ | ||
/** | ||
* Generates a spectrum and returns it | ||
* @param {Array<Array<number>>} peaks - list of peaks to put in the spectrum | ||
* @param {object} [options] - same options as new SpectrumGenerator | ||
* @return {object} spectrum | ||
*/ | ||
export function generateSpectrum(peaks, options = {}) { | ||
@@ -285,4 +217,4 @@ const generator = new SpectrumGenerator(options); | ||
return generator.getSpectrum({ | ||
threshold: options.threshold | ||
threshold: options.threshold, | ||
}); | ||
} |
import addBaseline from '../addBaseline'; | ||
test('Test addBaseline', () => { | ||
var corrected = addBaseline({ x: [1, 2, 3], y: [2, 3, 4] }, (x) => 2 * x); | ||
test('addBaseline', () => { | ||
let corrected = addBaseline({ x: [1, 2, 3], y: [2, 3, 4] }, (x) => 2 * x); | ||
expect(corrected).toMatchSnapshot(); | ||
}); |
import addNoise from '../addNoise'; | ||
test('Test addNoise', () => { | ||
var corrected = addNoise({ x: [1, 2, 3], y: [2, 3, 4] }, 10, { seed: 0 }); | ||
test('addNoise', () => { | ||
let corrected = addNoise({ x: [1, 2, 3], y: [2, 3, 4] }, 10, { seed: 0 }); | ||
expect(corrected.x).toHaveLength(3); | ||
expect(corrected).toMatchSnapshot(); | ||
}); |
@@ -1,11 +0,5 @@ | ||
/** | ||
* Add a baseline to the spectrum | ||
* @param {object} [data] - Your spectrum data in the format {x:[x1, x2, ...], y:[y1, y2, ...]} | ||
* @param {function} [baselineFct] - Mathematical function producing the baseline you want | ||
* @return {object} data | ||
*/ | ||
export default function addBaseline(data, baselineFct) { | ||
if (!baselineFct) return data; | ||
var xs = data.x; | ||
var ys = data.y; | ||
let xs = data.x; | ||
let ys = data.y; | ||
for (let i = 0; i < xs.length; i++) { | ||
@@ -12,0 +6,0 @@ ys[i] += baselineFct(xs[i]); |
import { randomUniform, randomNormal } from 'd3-random'; | ||
import seedrandom from 'seedrandom'; | ||
/** | ||
* Add noise to the spectrum | ||
* @param {object} [data] - Your spectrum data in the format {x:[x1, x2, ...], y:[y1, y2, ...]} | ||
* @param {number} [percent = 0] - Noise's amplitude in percents of the spectrum max value | ||
* @param {object} [options={}] | ||
* @param {number} [options.seed] - Seed for a deterministic sequence of random number | ||
* @param {string} [options.distribution='uniform'] - Type of random: 'uniform' (true random), 'normal' (gaussian distribution), | ||
* @return {data} data | ||
*/ | ||
import XSAdd from 'ml-xsadd'; | ||
export default function addNoise(data, percent = 0, options = {}) { | ||
@@ -30,4 +22,4 @@ const { distribution = 'uniform', seed } = options; | ||
if (!percent) return data; | ||
var ys = data.y; | ||
var factor = (percent * findMax(ys)) / 100; | ||
let ys = data.y; | ||
let factor = (percent * findMax(ys)) / 100; | ||
for (let i = 0; i < ys.length; i++) { | ||
@@ -41,3 +33,3 @@ ys[i] += generateRandomNumber() * factor; | ||
return typeof seed === 'number' | ||
? func.source(seedrandom(seed))(...args) | ||
? func.source(new XSAdd(seed).random)(...args) | ||
: func(...args); | ||
@@ -44,0 +36,0 @@ } |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
17
63
42455
13
856
1
+ Addedml-xsadd@2.0.0
+ Addedd3-random@2.2.2(transitive)
+ Addedml-xsadd@2.0.0(transitive)
- Removedseedrandom@^2.4.4
- Removedd3-random@1.1.2(transitive)
- Removedseedrandom@2.4.4(transitive)
Updatedd3-random@^2.0.1