Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

ml-spectra-fitting

Package Overview
Dependencies
Maintainers
4
Versions
33
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ml-spectra-fitting - npm Package Compare versions

Comparing version 4.0.2 to 4.1.0-pre.1661357854

14

lib-esm/index.d.ts

@@ -16,2 +16,3 @@ import { DataXY, DoubleArray } from 'cheminfo-types';

export interface Peak {
id?: string;
x: number;

@@ -32,2 +33,7 @@ y: number;

}
declare type OptimizedPeakIDOrNot<T extends Peak> = T extends {
id: string;
} ? OptimizedPeak & {
id: string;
} : OptimizedPeak;
declare type OptimizationParameter = number | ((peak: Peak) => number);

@@ -59,2 +65,6 @@ export interface OptimizationOptions {

/**
* baseline value to shift the intensity of data and peak
*/
baseline?: number;
/**
* Kind of shape used for fitting.

@@ -83,5 +93,5 @@ **/

*/
export declare function optimize(data: DataXY<DoubleArray>, peaks: Peak[], options?: OptimizeOptions): {
export declare function optimize<T extends Peak>(data: DataXY<DoubleArray>, peaks: T[], options?: OptimizeOptions): {
error: number;
peaks: OptimizedPeak[];
peaks: OptimizedPeakIDOrNot<T>[];
iterations: number;

@@ -88,0 +98,0 @@ };

21

lib-esm/index.js

@@ -19,5 +19,6 @@ import { xMinMaxValues } from 'ml-spectra-processing';

// need to rescale what is related to Y
const { baseline: shiftValue = minMaxY.min } = options;
let normalizedY = new Float64Array(data.y.length);
for (let i = 0; i < data.y.length; i++) {
normalizedY[i] = (data.y[i] - minMaxY.min) / minMaxY.range;
normalizedY[i] = (data.y[i] - shiftValue) / minMaxY.range;
}

@@ -51,12 +52,12 @@ const nbParams = internalPeaks[internalPeaks.length - 1].toIndex + 1;

for (let peak of internalPeaks) {
const newPeak = {
x: 0,
y: 0,
shape: peak.shape,
};
newPeak.x = fittedValues[peak.fromIndex];
newPeak.y = fittedValues[peak.fromIndex + 1] * minMaxY.range + minMaxY.min;
for (let i = 2; i < peak.parameters.length; i++) {
const { id, shape, parameters, fromIndex } = peak;
let newPeak = { x: 0, y: 0, shape };
if (id) {
newPeak = { ...newPeak, id };
}
newPeak.x = fittedValues[fromIndex];
newPeak.y = fittedValues[fromIndex + 1] * minMaxY.range + shiftValue;
for (let i = 2; i < parameters.length; i++) {
//@ts-expect-error should be fixed once
newPeak.shape[peak.parameters[i]] = fittedValues[peak.fromIndex + i];
newPeak.shape[parameters[i]] = fittedValues[fromIndex + i];
}

@@ -63,0 +64,0 @@ newPeaks.push(newPeak);

@@ -12,4 +12,4 @@ import { Shape1DInstance, PseudoVoigt } from 'ml-peak-shape-generator';

init: (peak: Peak) => number;
min: () => number;
max: () => number;
min: (peak: Peak) => 0 | -1.1;
max: (peak: Peak) => 0 | 1.1;
gradientDifference: () => number;

@@ -16,0 +16,0 @@ };

@@ -10,4 +10,4 @@ export const DefaultParameters = {

init: (peak) => peak.y,
min: () => 0,
max: () => 1.5,
min: (peak) => (peak.y < 0 ? -1.1 : 0),
max: (peak) => (peak.y < 0 ? 0 : 1.1),
gradientDifference: () => 1e-3,

@@ -14,0 +14,0 @@ },

@@ -6,2 +6,3 @@ import { Shape1D, Shape1DInstance } from 'ml-peak-shape-generator';

export interface InternalPeak {
id?: string;
shape: Shape1D;

@@ -8,0 +9,0 @@ shapeFct: Shape1DInstance;

@@ -14,10 +14,12 @@ import { getShape1D } from 'ml-peak-shape-generator';

let internalPeaks = [];
for (const peak of peaks) {
const shape = peak.shape
? peak.shape
: options.shape
? options.shape
: { kind: 'gaussian' };
const { baseline: shiftValue = minMaxY.min } = options;
const normalizedPeaks = peaks.map((peak) => {
return {
...peak,
y: (peak.y - shiftValue) / minMaxY.range,
};
});
for (const peak of normalizedPeaks) {
const { id, shape = options.shape ? options.shape : { kind: 'gaussian' } } = peak;
const shapeFct = getShape1D(shape);
//@ts-expect-error Should disappear with next release of peak-shape-generator
const parameters = ['x', 'y', ...shapeFct.getParameters()];

@@ -33,7 +35,5 @@ const propertiesValues = {

// check if the property is specified in the peak
let propertyValue = peak.parameters &&
peak.parameters[parameter] &&
peak.parameters[parameter][property];
let propertyValue = peak?.parameters?.[parameter]?.[property];
if (propertyValue) {
propertyValue = getNormalizedValue(propertyValue, parameter, property, minMaxY);
propertyValue = getNormalizedValue(propertyValue, parameter, property, minMaxY, options.baseline);
propertiesValues[property].push(propertyValue);

@@ -43,8 +43,6 @@ continue;

// check if there are some global option, it could be a number or a callback
let generalParameterValue = options.parameters &&
options.parameters[parameter] &&
options.parameters[parameter][property];
let generalParameterValue = options?.parameters?.[parameter]?.[property];
if (generalParameterValue) {
if (typeof generalParameterValue === 'number') {
generalParameterValue = getNormalizedValue(generalParameterValue, parameter, property, minMaxY);
generalParameterValue = getNormalizedValue(generalParameterValue, parameter, property, minMaxY, options.baseline);
propertiesValues[property].push(generalParameterValue);

@@ -55,3 +53,3 @@ continue;

let value = generalParameterValue(peak);
value = getNormalizedValue(value, parameter, property, minMaxY);
value = getNormalizedValue(value, parameter, property, minMaxY, options.baseline);
propertiesValues[property].push(value);

@@ -72,2 +70,3 @@ continue;

internalPeaks.push({
id,
shape,

@@ -83,9 +82,11 @@ shapeFct,

}
function getNormalizedValue(value, parameter, property, minMaxY) {
function getNormalizedValue(value, parameter, property, minMaxY, baseline) {
if (parameter === 'y') {
if (property === 'gradientDifference') {
return value / minMaxY.range;
return value;
}
else {
return (value - minMaxY.min) / minMaxY.range;
return baseline !== undefined
? (value - baseline) / minMaxY.range
: (value - minMaxY.min) / minMaxY.range;
}

@@ -92,0 +93,0 @@ }

@@ -16,2 +16,3 @@ import { DataXY, DoubleArray } from 'cheminfo-types';

export interface Peak {
id?: string;
x: number;

@@ -32,2 +33,7 @@ y: number;

}
declare type OptimizedPeakIDOrNot<T extends Peak> = T extends {
id: string;
} ? OptimizedPeak & {
id: string;
} : OptimizedPeak;
declare type OptimizationParameter = number | ((peak: Peak) => number);

@@ -59,2 +65,6 @@ export interface OptimizationOptions {

/**
* baseline value to shift the intensity of data and peak
*/
baseline?: number;
/**
* Kind of shape used for fitting.

@@ -83,5 +93,5 @@ **/

*/
export declare function optimize(data: DataXY<DoubleArray>, peaks: Peak[], options?: OptimizeOptions): {
export declare function optimize<T extends Peak>(data: DataXY<DoubleArray>, peaks: T[], options?: OptimizeOptions): {
error: number;
peaks: OptimizedPeak[];
peaks: OptimizedPeakIDOrNot<T>[];
iterations: number;

@@ -88,0 +98,0 @@ };

@@ -22,5 +22,6 @@ "use strict";

// need to rescale what is related to Y
const { baseline: shiftValue = minMaxY.min } = options;
let normalizedY = new Float64Array(data.y.length);
for (let i = 0; i < data.y.length; i++) {
normalizedY[i] = (data.y[i] - minMaxY.min) / minMaxY.range;
normalizedY[i] = (data.y[i] - shiftValue) / minMaxY.range;
}

@@ -54,12 +55,12 @@ const nbParams = internalPeaks[internalPeaks.length - 1].toIndex + 1;

for (let peak of internalPeaks) {
const newPeak = {
x: 0,
y: 0,
shape: peak.shape,
};
newPeak.x = fittedValues[peak.fromIndex];
newPeak.y = fittedValues[peak.fromIndex + 1] * minMaxY.range + minMaxY.min;
for (let i = 2; i < peak.parameters.length; i++) {
const { id, shape, parameters, fromIndex } = peak;
let newPeak = { x: 0, y: 0, shape };
if (id) {
newPeak = { ...newPeak, id };
}
newPeak.x = fittedValues[fromIndex];
newPeak.y = fittedValues[fromIndex + 1] * minMaxY.range + shiftValue;
for (let i = 2; i < parameters.length; i++) {
//@ts-expect-error should be fixed once
newPeak.shape[peak.parameters[i]] = fittedValues[peak.fromIndex + i];
newPeak.shape[parameters[i]] = fittedValues[fromIndex + i];
}

@@ -66,0 +67,0 @@ newPeaks.push(newPeak);

@@ -12,4 +12,4 @@ import { Shape1DInstance, PseudoVoigt } from 'ml-peak-shape-generator';

init: (peak: Peak) => number;
min: () => number;
max: () => number;
min: (peak: Peak) => 0 | -1.1;
max: (peak: Peak) => 0 | 1.1;
gradientDifference: () => number;

@@ -16,0 +16,0 @@ };

@@ -13,4 +13,4 @@ "use strict";

init: (peak) => peak.y,
min: () => 0,
max: () => 1.5,
min: (peak) => (peak.y < 0 ? -1.1 : 0),
max: (peak) => (peak.y < 0 ? 0 : 1.1),
gradientDifference: () => 1e-3,

@@ -17,0 +17,0 @@ },

@@ -6,2 +6,3 @@ import { Shape1D, Shape1DInstance } from 'ml-peak-shape-generator';

export interface InternalPeak {
id?: string;
shape: Shape1D;

@@ -8,0 +9,0 @@ shapeFct: Shape1DInstance;

@@ -17,10 +17,12 @@ "use strict";

let internalPeaks = [];
for (const peak of peaks) {
const shape = peak.shape
? peak.shape
: options.shape
? options.shape
: { kind: 'gaussian' };
const { baseline: shiftValue = minMaxY.min } = options;
const normalizedPeaks = peaks.map((peak) => {
return {
...peak,
y: (peak.y - shiftValue) / minMaxY.range,
};
});
for (const peak of normalizedPeaks) {
const { id, shape = options.shape ? options.shape : { kind: 'gaussian' } } = peak;
const shapeFct = (0, ml_peak_shape_generator_1.getShape1D)(shape);
//@ts-expect-error Should disappear with next release of peak-shape-generator
const parameters = ['x', 'y', ...shapeFct.getParameters()];

@@ -36,7 +38,5 @@ const propertiesValues = {

// check if the property is specified in the peak
let propertyValue = peak.parameters &&
peak.parameters[parameter] &&
peak.parameters[parameter][property];
let propertyValue = peak?.parameters?.[parameter]?.[property];
if (propertyValue) {
propertyValue = getNormalizedValue(propertyValue, parameter, property, minMaxY);
propertyValue = getNormalizedValue(propertyValue, parameter, property, minMaxY, options.baseline);
propertiesValues[property].push(propertyValue);

@@ -46,8 +46,6 @@ continue;

// check if there are some global option, it could be a number or a callback
let generalParameterValue = options.parameters &&
options.parameters[parameter] &&
options.parameters[parameter][property];
let generalParameterValue = options?.parameters?.[parameter]?.[property];
if (generalParameterValue) {
if (typeof generalParameterValue === 'number') {
generalParameterValue = getNormalizedValue(generalParameterValue, parameter, property, minMaxY);
generalParameterValue = getNormalizedValue(generalParameterValue, parameter, property, minMaxY, options.baseline);
propertiesValues[property].push(generalParameterValue);

@@ -58,3 +56,3 @@ continue;

let value = generalParameterValue(peak);
value = getNormalizedValue(value, parameter, property, minMaxY);
value = getNormalizedValue(value, parameter, property, minMaxY, options.baseline);
propertiesValues[property].push(value);

@@ -75,2 +73,3 @@ continue;

internalPeaks.push({
id,
shape,

@@ -87,9 +86,11 @@ shapeFct,

exports.getInternalPeaks = getInternalPeaks;
function getNormalizedValue(value, parameter, property, minMaxY) {
function getNormalizedValue(value, parameter, property, minMaxY, baseline) {
if (parameter === 'y') {
if (property === 'gradientDifference') {
return value / minMaxY.range;
return value;
}
else {
return (value - minMaxY.min) / minMaxY.range;
return baseline !== undefined
? (value - baseline) / minMaxY.range
: (value - minMaxY.min) / minMaxY.range;
}

@@ -96,0 +97,0 @@ }

{
"name": "ml-spectra-fitting",
"version": "4.0.2",
"version": "4.1.0-pre.1661357854",
"description": "Fit spectra using gaussian or lorentzian",

@@ -65,5 +65,5 @@ "main": "./lib/index.js",

"ml-levenberg-marquardt": "^4.1.0",
"ml-peak-shape-generator": "^4.1.1",
"ml-peak-shape-generator": "^4.1.2",
"ml-spectra-processing": "^11.5.0"
}
}

@@ -12,4 +12,14 @@ import type { DataXY } from 'cheminfo-types';

const peaks = [
{ x: -0.5, y: 1, shape: { kind: 'gaussian' as const, fwhm: 0.05 } },
{ x: 0.5, y: 1, shape: { kind: 'gaussian' as const, fwhm: 0.05 } },
{
id: 'first',
x: -0.5,
y: 1,
shape: { kind: 'gaussian' as const, fwhm: 0.05 },
},
{
id: 'second',
x: 0.5,
y: 1,
shape: { kind: 'gaussian' as const, fwhm: 0.05 },
},
];

@@ -27,4 +37,14 @@

let result = optimize(data, [
{ x: -0.55, y: 0.9, shape: { kind: 'gaussian' as const, fwhm: 0.08 } },
{ x: 0.55, y: 0.9, shape: { kind: 'gaussian' as const, fwhm: 0.08 } },
{
id: 'first',
x: -0.55,
y: 0.9,
shape: { kind: 'gaussian' as const, fwhm: 0.08 },
},
{
id: 'second',
x: 0.55,
y: 0.9,
shape: { kind: 'gaussian' as const, fwhm: 0.08 },
},
]);

@@ -31,0 +51,0 @@

@@ -23,2 +23,3 @@ import { DataXY, DoubleArray } from 'cheminfo-types';

export interface Peak {
id?: string;
x: number;

@@ -39,2 +40,6 @@ y: number;

type OptimizedPeakIDOrNot<T extends Peak> = T extends { id: string }
? OptimizedPeak & { id: string }
: OptimizedPeak;
type OptimizationParameter = number | ((peak: Peak) => number);

@@ -69,2 +74,6 @@

/**
* baseline value to shift the intensity of data and peak
*/
baseline?: number;
/**
* Kind of shape used for fitting.

@@ -94,9 +103,9 @@ **/

*/
export function optimize(
export function optimize<T extends Peak>(
data: DataXY<DoubleArray>,
peaks: Peak[],
peaks: T[],
options: OptimizeOptions = {},
): {
error: number;
peaks: OptimizedPeak[];
peaks: OptimizedPeakIDOrNot<T>[];
iterations: number;

@@ -109,6 +118,8 @@ } {

const internalPeaks = getInternalPeaks(peaks, minMaxY, options);
// need to rescale what is related to Y
const { baseline: shiftValue = minMaxY.min } = options;
let normalizedY = new Float64Array(data.y.length);
for (let i = 0; i < data.y.length; i++) {
normalizedY[i] = (data.y[i] - minMaxY.min) / minMaxY.range;
normalizedY[i] = (data.y[i] - shiftValue) / minMaxY.range;
}

@@ -143,16 +154,19 @@

const fittedValues = fitted.parameterValues;
let newPeaks: OptimizedPeak[] = [];
let newPeaks = [];
for (let peak of internalPeaks) {
const newPeak = {
x: 0,
y: 0,
shape: peak.shape,
};
newPeak.x = fittedValues[peak.fromIndex];
newPeak.y = fittedValues[peak.fromIndex + 1] * minMaxY.range + minMaxY.min;
for (let i = 2; i < peak.parameters.length; i++) {
const { id, shape, parameters, fromIndex } = peak;
let newPeak = { x: 0, y: 0, shape } as OptimizedPeakIDOrNot<T>;
if (id) {
newPeak = { ...newPeak, id } as OptimizedPeakIDOrNot<T>;
}
newPeak.x = fittedValues[fromIndex];
newPeak.y = fittedValues[fromIndex + 1] * minMaxY.range + shiftValue;
for (let i = 2; i < parameters.length; i++) {
//@ts-expect-error should be fixed once
newPeak.shape[peak.parameters[i]] = fittedValues[peak.fromIndex + i];
newPeak.shape[parameters[i]] = fittedValues[fromIndex + i];
}
newPeaks.push(newPeak);

@@ -159,0 +173,0 @@ }

@@ -19,3 +19,3 @@ import { toBeDeepCloseTo, toMatchCloseTo } from 'jest-matcher-deep-close-to';

min: [-1000, 0, 125],
max: [1000, 1.5, 2000],
max: [1000, 1.1, 2000],
init: [0, 1, 500],

@@ -43,3 +43,3 @@ gradientDifference: [1, 0.001, 1],

min: [-999, 0, 125, 0],
max: [1001, 1.5, 2000, 1],
max: [1001, 1.1, 2000, 1],
init: [1, 2, 500, 0.5],

@@ -69,3 +69,3 @@ gradientDifference: [1, 0.001, 1, 0.01],

min: [-1000, 0, 125],
max: [1000, 1.5, 2000],
max: [1000, 1.1, 2000],
init: [0, 1, 500],

@@ -100,3 +100,3 @@ gradientDifference: [1, 0.001, 1],

min: [-1, -2, 125],
max: [1, 1.5, 2000],
max: [1, 1.1, 2000],
init: [0, 1, 500],

@@ -103,0 +103,0 @@ gradientDifference: [1, 0.001, 1],

@@ -17,4 +17,4 @@ import { Shape1DInstance, PseudoVoigt } from 'ml-peak-shape-generator';

init: (peak: Peak) => peak.y,
min: () => 0,
max: () => 1.5,
min: (peak: Peak) => (peak.y < 0 ? -1.1 : 0),
max: (peak: Peak) => (peak.y < 0 ? 0 : 1.1),
gradientDifference: () => 1e-3,

@@ -21,0 +21,0 @@ },

@@ -13,2 +13,3 @@ import { getShape1D, Shape1D, Shape1DInstance } from 'ml-peak-shape-generator';

export interface InternalPeak {
id?: string;
shape: Shape1D;

@@ -35,12 +36,17 @@ shapeFct: Shape1DInstance;

let internalPeaks: InternalPeak[] = [];
for (const peak of peaks) {
const shape: Shape1D = peak.shape
? peak.shape
: options.shape
? options.shape
: { kind: 'gaussian' };
const { baseline: shiftValue = minMaxY.min } = options;
const normalizedPeaks = peaks.map((peak) => {
return {
...peak,
y: (peak.y - shiftValue) / minMaxY.range,
};
});
for (const peak of normalizedPeaks) {
const { id, shape = options.shape ? options.shape : { kind: 'gaussian' } } =
peak;
const shapeFct: Shape1DInstance = getShape1D(shape);
//@ts-expect-error Should disappear with next release of peak-shape-generator
const parameters: Parameter[] = ['x', 'y', ...shapeFct.getParameters()];

@@ -58,6 +64,3 @@

// check if the property is specified in the peak
let propertyValue =
peak.parameters &&
peak.parameters[parameter] &&
peak.parameters[parameter][property];
let propertyValue = peak?.parameters?.[parameter]?.[property];
if (propertyValue) {

@@ -69,2 +72,3 @@ propertyValue = getNormalizedValue(

minMaxY,
options.baseline,
);

@@ -78,5 +82,3 @@

let generalParameterValue =
options.parameters &&
options.parameters[parameter] &&
options.parameters[parameter][property];
options?.parameters?.[parameter]?.[property];
if (generalParameterValue) {

@@ -89,2 +91,3 @@ if (typeof generalParameterValue === 'number') {

minMaxY,
options.baseline,
);

@@ -95,3 +98,9 @@ propertiesValues[property].push(generalParameterValue);

let value = generalParameterValue(peak);
value = getNormalizedValue(value, parameter, property, minMaxY);
value = getNormalizedValue(
value,
parameter,
property,
minMaxY,
options.baseline,
);
propertiesValues[property].push(value);

@@ -118,2 +127,3 @@ continue;

internalPeaks.push({
id,
shape,

@@ -135,8 +145,11 @@ shapeFct,

minMaxY: { min: number; max: number; range: number },
baseline?: number,
): number {
if (parameter === 'y') {
if (property === 'gradientDifference') {
return value / minMaxY.range;
return value;
} else {
return (value - minMaxY.min) / minMaxY.range;
return baseline !== undefined
? (value - baseline) / minMaxY.range
: (value - minMaxY.min) / minMaxY.range;
}

@@ -143,0 +156,0 @@ }

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc