Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add negative peaks to 2D spectra #85

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,19 +49,19 @@
"@types/d3-random": "^3.0.3",
"@types/jest": "^29.5.12",
"cheminfo-build": "^1.2.0",
"cheminfo-types": "^1.7.2",
"cheminfo-types": "^1.7.3",
"eslint": "^8.57.0",
"eslint-config-cheminfo-typescript": "^12.2.0",
"eslint-config-cheminfo-typescript": "^12.4.0",
"jest": "^29.7.0",
"jest-matcher-deep-close-to": "^3.0.2",
"ml-savitzky-golay-generalized": "^4.0.1",
"prettier": "^3.2.5",
"rimraf": "^5.0.5",
"ts-jest": "^29.1.2",
"typescript": "^5.4.2"
"ml-savitzky-golay-generalized": "^4.2.0",
"prettier": "^3.3.2",
"rimraf": "^5.0.7",
"ts-jest": "^29.1.4",
"typescript": "^5.4.5"
},
"dependencies": {
"ml-peak-shape-generator": "^4.1.2",
"ml-spectra-processing": "^14.2.0"
"ml-spectra-processing": "^14.5.0"
}
}
24 changes: 10 additions & 14 deletions src/Spectrum2DGenerator.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { NumberArray } from 'cheminfo-types';
import { getShape2D, Shape2DClass } from 'ml-peak-shape-generator';
import type {
Shape2D,
Shape2DInstance,
XYNumber,
} from 'ml-peak-shape-generator';
import { matrixMinMaxZ } from 'ml-spectra-processing';
import { matrixCreateEmpty, matrixMinMaxZ } from 'ml-spectra-processing';

import type { Data2D } from './types/Data2D';
import type { Peak2D, Peak2DSeries } from './types/Peaks2D';
Expand All @@ -27,12 +28,12 @@ const convertWidthToFWHM = (shape: Shape2DClass, width: number | XYNumber) => {

export interface OptionsSG2D {
/**
* First x value (inclusive).
* First xy value (inclusive).
* @default `0`
*/
from?: number | XYNumber;
/**
* Last x value (inclusive).
* Last xy value (inclusive).
* @default `100`
*/
to?: number | XYNumber;
Expand Down Expand Up @@ -99,7 +100,7 @@ export interface Spectrum2D {
maxY: number;
minZ: number;
maxZ: number;
z: Float64Array[] | number[][];
z: NumberArray[];
}

export class Spectrum2DGenerator {
Expand Down Expand Up @@ -145,7 +146,10 @@ export class Spectrum2DGenerator {
this.data = {
x: new Float64Array(nbPoints.x),
y: new Float64Array(nbPoints.y),
z: createMatrix(this.nbPoints),
z: matrixCreateEmpty({
nbRows: this.nbPoints.y,
nbColumns: this.nbPoints.x,
}),
};

for (const axis of axis2D) {
Expand Down Expand Up @@ -289,7 +293,7 @@ export class Spectrum2DGenerator {
this.data.x[xIndex] - position.x,
this.data.y[yIndex] - position.y,
);
if (value > 1e-6) {
if (Math.abs(value) > 1e-6) {
this.data.z[yIndex][xIndex] += value;
}
}
Expand Down Expand Up @@ -367,11 +371,3 @@ function assertNumber(value: number, name: string) {
throw new TypeError(`${name} option must be a number`);
}
}

function createMatrix(nbPoints: XYNumber) {
const zMatrix = new Array(nbPoints.y);
for (let i = 0; i < nbPoints.y; i++) {
zMatrix[i] = new Float64Array(nbPoints.x);
}
return zMatrix;
}
27 changes: 26 additions & 1 deletion src/__tests__/generateSpectrum2D.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { xMaxValue } from 'ml-spectra-processing';
import { xMaxValue, xMinMaxValues } from 'ml-spectra-processing';

import type { Spectrum2D } from '../Spectrum2DGenerator';
import { generateSpectrum2D } from '../Spectrum2DGenerator';
Expand Down Expand Up @@ -66,6 +66,31 @@ describe('generateSpectrum with one peak and small window', () => {
checkMax(spectrum, 10);
});

it('should have negative min value', () => {
const spectrum = generateSpectrum2D(
[
{ x: 1, y: 1, z: 0.3, width: 0.01 },
{ x: 1.3, y: 0.8, z: -0.3, width: 0.01 },
],
{
generator: {
nbPoints: 101,
from: { x: 0.6, y: 0.6 },
to: { x: 2, y: 1.5 },
},
},
);

let max = Number.MIN_SAFE_INTEGER;
let min = Number.MAX_SAFE_INTEGER;
for (const row of spectrum.z) {
const rowMinMax = xMinMaxValues(row);
if (max < rowMinMax.max) max = rowMinMax.max;
if (min > rowMinMax.min) min = rowMinMax.min;
}
expect(max).toBeCloseTo(0.3, 0);
expect(min).toBeCloseTo(-0.3, 0);
});
it('should work with shape 17/4, peak width 0.2', () => {
const spectrum = generateSpectrum2D([[10, 10, 1]], {
generator: {
Expand Down
Loading