isotopic-distribution
Advanced tools
Comparing version 1.2.3 to 1.3.0
{ | ||
"name": "isotopic-distribution", | ||
"version": "1.2.3", | ||
"version": "1.3.0", | ||
"description": "Calculate the isotopic distribution of a molecular formula", | ||
@@ -21,8 +21,11 @@ "main": "src/index.js", | ||
"dependencies": { | ||
"chemical-elements": "^1.1.13", | ||
"mf-parser": "^1.3.5", | ||
"mf-utilities": "^1.2.12", | ||
"chemical-elements": "^1.2.0", | ||
"mf-parser": "^1.4.0", | ||
"mf-utilities": "^1.2.13", | ||
"spectrum-generator": "^5.4.0" | ||
}, | ||
"gitHead": "3357658b99069e3a03c06471bc374c5effb919d8" | ||
"devDependencies": { | ||
"jest-matcher-deep-close-to": "^3.0.2" | ||
}, | ||
"gitHead": "b044512a459c82c7bb816c52d0e90d48b8ffeae3" | ||
} |
@@ -8,7 +8,7 @@ 'use strict'; | ||
let dist1 = new Distribution(); | ||
dist1.push(1, 2); | ||
dist1.push(2, 3); | ||
dist1.push({ x: 1, y: 2 }); | ||
dist1.push({ x: 2, y: 3 }); | ||
let dist2 = new Distribution(); | ||
dist2.push(2, 4); | ||
dist2.push(3, 5); | ||
dist2.push({ x: 2, y: 4 }); | ||
dist2.push({ x: 3, y: 5 }); | ||
@@ -15,0 +15,0 @@ dist1.append(dist2); |
@@ -8,6 +8,6 @@ 'use strict'; | ||
let dist = new Distribution(); | ||
dist.push(1, 2); | ||
dist.push(2, 3); | ||
dist.push(3, 3); | ||
dist.push(4, 3); | ||
dist.push({ x: 1, y: 2 }); | ||
dist.push({ x: 2, y: 3 }); | ||
dist.push({ x: 3, y: 3 }); | ||
dist.push({ x: 4, y: 3 }); | ||
@@ -14,0 +14,0 @@ expect(dist.closestPointX(2)).toStrictEqual({ x: 2, y: 3 }); |
@@ -7,27 +7,27 @@ 'use strict'; | ||
it('create array', () => { | ||
let array = new Distribution(); | ||
array.push(1, 2); | ||
array.push(2, 3); | ||
expect(array).toHaveLength(2); | ||
let dist = new Distribution(); | ||
dist.push({ x: 1, y: 2 }); | ||
dist.push({ x: 2, y: 3 }); | ||
expect(dist).toHaveLength(2); | ||
}); | ||
it('join array 0', () => { | ||
let array = new Distribution(); | ||
array.join(); | ||
expect(array.array).toStrictEqual([]); | ||
it('joinX array 0', () => { | ||
let dist = new Distribution(); | ||
dist.joinX(); | ||
expect(dist.array).toStrictEqual([]); | ||
}); | ||
it('join array 1', () => { | ||
let array = new Distribution(); | ||
array.push(1, 1); | ||
array.join(); | ||
expect(array.array).toStrictEqual([{ x: 1, y: 1 }]); | ||
it('joinX array 1', () => { | ||
let dist = new Distribution(); | ||
dist.push({ x: 1, y: 1 }); | ||
dist.joinX(); | ||
expect(dist.array).toStrictEqual([{ x: 1, y: 1 }]); | ||
}); | ||
it('join array', () => { | ||
let array = new Distribution(); | ||
array.push(0, 0); | ||
array.push(1, 1); | ||
array.join(); | ||
expect(array.array).toStrictEqual([ | ||
it('joinX array', () => { | ||
let dist = new Distribution(); | ||
dist.push({ x: 0, y: 0 }); | ||
dist.push({ x: 1, y: 1 }); | ||
dist.joinX(); | ||
expect(dist.array).toStrictEqual([ | ||
{ x: 0, y: 0 }, | ||
@@ -38,43 +38,43 @@ { x: 1, y: 1 }, | ||
it('really join array', () => { | ||
let array = new Distribution(); | ||
array.push(0, 0); | ||
array.push(1, 1); | ||
array.join(1); | ||
expect(array.array).toStrictEqual([{ x: 1, y: 1 }]); | ||
it('really joinX array', () => { | ||
let dist = new Distribution(); | ||
dist.push({ x: 0, y: 0 }); | ||
dist.push({ x: 1, y: 1 }); | ||
dist.joinX(1); | ||
expect(dist.array).toStrictEqual([{ x: 1, y: 1 }]); | ||
}); | ||
it('really join array shifted', () => { | ||
let array = new Distribution(); | ||
array.push(1, 0); | ||
array.push(2, 1); | ||
array.join(1); | ||
expect(array.array).toStrictEqual([{ x: 2, y: 1 }]); | ||
it('really joinX array shifted', () => { | ||
let dist = new Distribution(); | ||
dist.push({ x: 1, y: 0 }); | ||
dist.push({ x: 2, y: 1 }); | ||
dist.joinX(1); | ||
expect(dist.array).toStrictEqual([{ x: 2, y: 1 }]); | ||
}); | ||
it('really join array shifted weighted', () => { | ||
let array = new Distribution(); | ||
array.push(1, 1); | ||
array.push(2, 3); | ||
array.join(1); | ||
expect(array.array).toStrictEqual([{ x: 1.75, y: 4 }]); | ||
it('really joinX array shifted weighted', () => { | ||
let dist = new Distribution(); | ||
dist.push({ x: 1, y: 1 }); | ||
dist.push({ x: 2, y: 3 }); | ||
dist.joinX(1); | ||
expect(dist.array).toStrictEqual([{ x: 1.75, y: 4 }]); | ||
}); | ||
it('really join array shifted weighted 3', () => { | ||
let array = new Distribution(); | ||
array.push(1, 1); | ||
array.push(2, 3); | ||
array.push(2.25, 1); | ||
array.join(1); | ||
expect(array.array).toStrictEqual([{ x: 1.85, y: 5 }]); | ||
it('really joinX array shifted weighted 3', () => { | ||
let dist = new Distribution(); | ||
dist.push({ x: 1, y: 1 }); | ||
dist.push({ x: 2, y: 3 }); | ||
dist.push({ x: 2.25, y: 1 }); | ||
dist.joinX(1); | ||
expect(dist.array).toStrictEqual([{ x: 1.85, y: 5 }]); | ||
}); | ||
it('really join array shifted weighted 4', () => { | ||
let array = new Distribution(); | ||
array.push(1, 1); | ||
array.push(2, 3); | ||
array.push(2.25, 1); | ||
array.push(5, 1); | ||
array.join(1); | ||
expect(array.array).toStrictEqual([ | ||
it('really joinX array shifted weighted 4', () => { | ||
let dist = new Distribution(); | ||
dist.push({ x: 1, y: 1 }); | ||
dist.push({ x: 2, y: 3 }); | ||
dist.push({ x: 2.25, y: 1 }); | ||
dist.push({ x: 5, y: 1 }); | ||
dist.joinX(1); | ||
expect(dist.array).toStrictEqual([ | ||
{ x: 1.85, y: 5 }, | ||
@@ -81,0 +81,0 @@ { x: 5, y: 1 }, |
@@ -8,7 +8,7 @@ 'use strict'; | ||
let dist1 = new Distribution(); | ||
dist1.push(1, 2); | ||
dist1.push(2, 3); | ||
dist1.push({ x: 1, y: 2 }); | ||
dist1.push({ x: 2, y: 3 }); | ||
let dist2 = new Distribution(); | ||
dist2.push(1, 2); | ||
dist2.push(2, 3); | ||
dist2.push({ x: 1, y: 2 }); | ||
dist2.push({ x: 2, y: 3 }); | ||
let dist3 = dist1.multiply(dist2); | ||
@@ -15,0 +15,0 @@ |
@@ -8,4 +8,4 @@ 'use strict'; | ||
let dist = new Distribution(); | ||
dist.push(1, 2); | ||
dist.push(2, 3); | ||
dist.push({ x: 1, y: 2 }); | ||
dist.push({ x: 2, y: 3 }); | ||
dist.normalize(); | ||
@@ -12,0 +12,0 @@ |
@@ -8,4 +8,4 @@ 'use strict'; | ||
let dist = new Distribution(); | ||
dist.push(1, 2); | ||
dist.push(2, 3); | ||
dist.push({ x: 1, y: 2 }); | ||
dist.push({ x: 2, y: 3 }); | ||
dist.power(2); | ||
@@ -21,3 +21,3 @@ expect(dist.array).toStrictEqual([ | ||
let dist = new Distribution(); | ||
dist.push(1, 1); | ||
dist.push({ x: 1, y: 1 }); | ||
dist.power(3); | ||
@@ -29,4 +29,4 @@ expect(dist.array).toStrictEqual([{ x: 3, y: 1 }]); | ||
let dist = new Distribution(); | ||
dist.push(1, 1); | ||
dist.push(2, 1); | ||
dist.push({ x: 1, y: 1 }); | ||
dist.push({ x: 2, y: 1 }); | ||
dist.power(3); | ||
@@ -43,4 +43,4 @@ expect(dist.array).toStrictEqual([ | ||
let dist = new Distribution(); | ||
dist.push(1, 1); | ||
dist.push(2, 1); | ||
dist.push({ x: 1, y: 1 }); | ||
dist.push({ x: 2, y: 1 }); | ||
dist.power(1000); | ||
@@ -53,4 +53,4 @@ expect(dist.array).toHaveLength(1001); | ||
let dist = new Distribution(); | ||
dist.push(1, 0.5); | ||
dist.push(2, 0.5); | ||
dist.push({ x: 1, y: 0.5 }); | ||
dist.push({ x: 2, y: 0.5 }); | ||
dist.power(100000); | ||
@@ -57,0 +57,0 @@ let sum = dist.array.reduce((s, a) => s + a.y, 0); |
@@ -8,4 +8,4 @@ 'use strict'; | ||
let dist = new Distribution(); | ||
dist.push(1, 2); | ||
dist.push(2, 3); | ||
dist.push({ x: 1, y: 2 }); | ||
dist.push({ x: 2, y: 3 }); | ||
dist.square(); | ||
@@ -12,0 +12,0 @@ expect(dist.array).toStrictEqual([ |
@@ -8,6 +8,6 @@ 'use strict'; | ||
let dist = new Distribution(); | ||
dist.push(1, 2); | ||
dist.push(2, 3); | ||
dist.push(2, 1); | ||
dist.push(2, 4); | ||
dist.push({ x: 1, y: 2 }); | ||
dist.push({ x: 2, y: 3 }); | ||
dist.push({ x: 2, y: 1 }); | ||
dist.push({ x: 2, y: 4 }); | ||
dist.topY(2); | ||
@@ -14,0 +14,0 @@ |
@@ -73,4 +73,5 @@ 'use strict'; | ||
Distribution.prototype.push = function push(x, y) { | ||
this.array.push({ x, y }); | ||
Distribution.prototype.push = function push(point) { | ||
this.array.push(point); | ||
this.xSorted = false; | ||
@@ -94,3 +95,3 @@ this.ySorted = false; | ||
Distribution.prototype.sortY = require('./utils/sortY.js'); | ||
Distribution.prototype.join = require('./utils/join.js'); | ||
Distribution.prototype.joinX = require('./utils/joinX.js'); | ||
Distribution.prototype.topY = require('./utils/topY.js'); | ||
@@ -97,0 +98,0 @@ Distribution.prototype.maxToOne = require('./utils/maxToOne.js'); |
@@ -10,3 +10,6 @@ 'use strict'; | ||
const Distribution = require('./Distribution'); | ||
const getDerivedCompositionInfo = require('./utils/getDerivedCompositionInfo'); | ||
const MINIMAL_FWHM = 1e-8; | ||
/** | ||
@@ -69,4 +72,4 @@ * An object containing two arrays | ||
// if fwhm is under 1e-8 there are some artifacts in the spectra | ||
if (this.fwhm < 1e-8) this.fwhm = 1e-8; | ||
this.minY = options.minY === undefined ? 1e-8 : options.minY; | ||
if (this.fwhm < MINIMAL_FWHM) this.fwhm = MINIMAL_FWHM; | ||
this.minY = options.minY === undefined ? MINIMAL_FWHM : options.minY; | ||
this.maxLines = options.maxLines || 5000; | ||
@@ -100,2 +103,3 @@ this.allowNeutral = | ||
y: 1, | ||
composition: this.fwhm === MINIMAL_FWHM ? {} : undefined, | ||
}, | ||
@@ -109,5 +113,14 @@ ]); | ||
if (isotope.number > 0) { | ||
let isotopeDistribution = new Distribution(isotope.distribution); | ||
isotopeDistribution.power(isotope.number, options); | ||
totalDistribution.multiply(isotopeDistribution, options); | ||
const newDistribution = JSON.parse( | ||
JSON.stringify(isotope.distribution), | ||
); | ||
if (this.fwhm === MINIMAL_FWHM) { | ||
// add composition | ||
for (const entry of newDistribution) { | ||
entry.composition = { [Math.round(entry.x) + isotope.atom]: 1 }; | ||
} | ||
} | ||
let distribution = new Distribution(newDistribution); | ||
distribution.power(isotope.number, options); | ||
totalDistribution.multiply(distribution, options); | ||
} | ||
@@ -162,3 +175,9 @@ } | ||
} | ||
if (finalDistribution) finalDistribution.join(this.fwhm); | ||
if (finalDistribution) finalDistribution.joinX(this.fwhm); | ||
for (let entry of finalDistribution.array) { | ||
if (!entry.composition) continue; | ||
Object.assign(entry, getDerivedCompositionInfo(entry.composition)); | ||
} | ||
this.confidence /= this.parts.length; | ||
@@ -228,3 +247,3 @@ this.cachedDistribution = finalDistribution; | ||
* @param {number} [options.sumValue] // if sumValue is defined, maxValue is ignored | ||
* @return {XY} an object containing 2 properties: x:[] and y:[] | ||
* @return {XY} an object containing at least the 2 properties: x:[] and y:[] | ||
*/ | ||
@@ -234,3 +253,3 @@ getXY(options = {}) { | ||
let points = this.getDistribution().array; | ||
if (points.length === 0) return []; | ||
if (points.length === 0) return { x: [], y: [] }; | ||
let factor = 1; | ||
@@ -245,6 +264,13 @@ if (sumValue) { | ||
return { | ||
const result = { | ||
x: points.map((a) => a.x), | ||
y: points.map((a) => a.y / factor), | ||
}; | ||
for (let key of Object.keys(points[0]).filter( | ||
(k) => k !== 'x' && k !== 'y', | ||
)) { | ||
result[key] = points.map((a) => a[key]); | ||
} | ||
return result; | ||
} | ||
@@ -251,0 +277,0 @@ |
@@ -13,13 +13,35 @@ 'use strict'; | ||
let y = entryA.y * entryB.y; | ||
if (y > minY) result.push(entryA.x + entryB.x, y); | ||
if (result.length > maxLines) { | ||
result.join(deltaX); | ||
result.topY(maxLines / 2); | ||
if (y > minY) { | ||
const composition = calculateComposition(entryA, entryB); | ||
if (composition) { | ||
result.push({ x: entryA.x + entryB.x, y, composition }); | ||
} else { | ||
result.push({ x: entryA.x + entryB.x, y }); | ||
} | ||
} | ||
if (result.length > maxLines * 2) { | ||
result.joinX(deltaX); | ||
result.topY(maxLines); | ||
} | ||
} | ||
} | ||
result.join(deltaX); | ||
result.topY(maxLines / 2); | ||
result.joinX(deltaX); | ||
result.topY(maxLines); | ||
this.move(result); | ||
return this; | ||
}; | ||
function calculateComposition(entryA, entryB) { | ||
if (!entryA.composition || !entryB.composition) return; | ||
let toReturn = {}; | ||
const keys = [ | ||
...new Set( | ||
Object.keys(entryA.composition).concat(Object.keys(entryB.composition)), | ||
), | ||
]; | ||
for (let key of keys) { | ||
toReturn[key] = | ||
(entryA.composition[key] || 0) + (entryB.composition[key] || 0); | ||
} | ||
return toReturn; | ||
} |
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
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
48395
30
1322
1
Updatedchemical-elements@^1.2.0
Updatedmf-parser@^1.4.0
Updatedmf-utilities@^1.2.13