Skip to content

Commit abbf225

Browse files
committed
feat: add setMinX setMaxX setMinY setMaxY
1 parent 9c2c0d2 commit abbf225

File tree

9 files changed

+316
-0
lines changed

9 files changed

+316
-0
lines changed

src/filters/filters.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,11 @@ export * from './x/ensureGrowing';
2121
export * from './x/reverseIfNeeded';
2222
export * from './x/equallySpaced';
2323
export * from './x/filterX';
24+
export * from './x/setMaxX';
25+
export * from './x/setMinX';
2426
export * from './x/calibrateX';
2527
export * from './x/xFunction';
2628

2729
export * from './scaling/yFunction';
30+
export * from './y/setMaxY';
31+
export * from './y/setMinY';
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { describe, it, expect } from 'vitest';
2+
3+
import { setMaxX } from '../setMaxX';
4+
5+
describe('setMaxX', () => {
6+
it('default values', () => {
7+
const data = {
8+
x: Float64Array.from([1, 2, 3]),
9+
y: Float64Array.from([2, 3, 4]),
10+
};
11+
const result = setMaxX(data, {});
12+
expect(result.data).toStrictEqual({
13+
x: Float64Array.from([-1, 0, 1]),
14+
y: Float64Array.from([2, 3, 4]),
15+
});
16+
expect(result.data).not.toBe(data);
17+
});
18+
19+
it('nothing to do', () => {
20+
const data = {
21+
x: Float64Array.from([1, 2, 3]),
22+
y: Float64Array.from([2, 3, 4]),
23+
};
24+
const result = setMaxX(data, { max: 3 });
25+
expect(result.data).toStrictEqual({
26+
x: Float64Array.from([1, 2, 3]),
27+
y: new Float64Array([2, 3, 4]),
28+
});
29+
expect(result.data).toBe(data);
30+
});
31+
32+
it('negative value', () => {
33+
const data = {
34+
x: Float64Array.from([1, 2, 3]),
35+
y: Float64Array.from([2, 3, 4]),
36+
};
37+
const result = setMaxX(data, { max: -1 });
38+
expect(result.data).toStrictEqual({
39+
x: Float64Array.from([-3, -2, -1]),
40+
y: Float64Array.from([2, 3, 4]),
41+
});
42+
});
43+
});
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { describe, it, expect } from 'vitest';
2+
3+
import { setMinX } from '../setMinX';
4+
5+
describe('setMinX', () => {
6+
it('default values', () => {
7+
const data = {
8+
x: Float64Array.from([1, 2, 3]),
9+
y: Float64Array.from([2, 3, 4]),
10+
};
11+
const result = setMinX(data, {});
12+
expect(result.data).toStrictEqual({
13+
x: Float64Array.from([0, 1, 2]),
14+
y: Float64Array.from([2, 3, 4]),
15+
});
16+
expect(result.data).not.toBe(data);
17+
});
18+
19+
it('nothing to do', () => {
20+
const data = {
21+
x: Float64Array.from([1, 2, 3]),
22+
y: Float64Array.from([2, 3, 4]),
23+
};
24+
const result = setMinX(data, { min: 1 });
25+
expect(result.data).toStrictEqual({
26+
x: Float64Array.from([1, 2, 3]),
27+
y: Float64Array.from([2, 3, 4]),
28+
});
29+
expect(result.data).toBe(data);
30+
});
31+
32+
it('negative value', () => {
33+
const data = {
34+
x: Float64Array.from([1, 2, 3]),
35+
y: Float64Array.from([2, 3, 4]),
36+
};
37+
const result = setMinX(data, { min: -1 });
38+
expect(result.data).toStrictEqual({
39+
x: Float64Array.from([-1, 0, 1]),
40+
y: Float64Array.from([2, 3, 4]),
41+
});
42+
});
43+
});

src/filters/x/setMaxX.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { DataXY } from 'cheminfo-types';
2+
import { xAdd, xMaxValue } from 'ml-spectra-processing';
3+
4+
export interface SetMaxXFilter {
5+
name: 'setMaxX';
6+
options?: SetMaxXOptions;
7+
}
8+
9+
export interface SetMaxXOptions {
10+
/**
11+
* @default 1
12+
*/
13+
max?: number;
14+
}
15+
/**
16+
* Filter that allows to
17+
* @param data
18+
* @param options
19+
*/
20+
export function setMaxX(
21+
data: DataXY<Float64Array>,
22+
options: SetMaxXOptions = {},
23+
) {
24+
const { max = 1 } = options;
25+
const existingMax = xMaxValue(data.x);
26+
if (existingMax === max) {
27+
return { data };
28+
}
29+
return {
30+
data: {
31+
x: xAdd(data.x, max - existingMax),
32+
y: data.y,
33+
},
34+
};
35+
}

src/filters/x/setMinX.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { DataXY } from 'cheminfo-types';
2+
import { xAdd, xMinValue } from 'ml-spectra-processing';
3+
4+
export interface SetMinXFilter {
5+
name: 'setMinX';
6+
options?: SetMinXOptions;
7+
}
8+
9+
export interface SetMinXOptions {
10+
/**
11+
* @default 0
12+
*/
13+
min?: number;
14+
}
15+
/**
16+
* Filter that allows to
17+
* @param data
18+
* @param options
19+
*/
20+
export function setMinX(
21+
data: DataXY<Float64Array>,
22+
options: SetMinXOptions = {},
23+
) {
24+
const { min = 0 } = options;
25+
const existingMin = xMinValue(data.x);
26+
if (existingMin === min) {
27+
return { data };
28+
}
29+
return {
30+
data: {
31+
x: xAdd(data.x, min - existingMin),
32+
y: data.y,
33+
},
34+
};
35+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { describe, it, expect } from 'vitest';
2+
3+
import { setMaxY } from '../setMaxY';
4+
5+
describe('setMaxY', () => {
6+
it('default values', () => {
7+
const data = {
8+
x: Float64Array.from([1, 2, 3]),
9+
y: Float64Array.from([2, 3, 4]),
10+
};
11+
const result = setMaxY(data, {});
12+
expect(result.data).toStrictEqual({
13+
x: Float64Array.from([1, 2, 3]),
14+
y: new Float64Array([-1, 0, 1]),
15+
});
16+
expect(result.data).not.toBe(data);
17+
});
18+
19+
it('nothing to do', () => {
20+
const data = {
21+
x: Float64Array.from([1, 2, 3]),
22+
y: Float64Array.from([2, 3, 4]),
23+
};
24+
const result = setMaxY(data, { max: 4 });
25+
expect(result.data).toStrictEqual({
26+
x: Float64Array.from([1, 2, 3]),
27+
y: new Float64Array([2, 3, 4]),
28+
});
29+
expect(result.data).toBe(data);
30+
});
31+
32+
it('negative value', () => {
33+
const data = {
34+
x: Float64Array.from([1, 2, 3]),
35+
y: Float64Array.from([2, 3, 4]),
36+
};
37+
const result = setMaxY(data, { max: -1 });
38+
expect(result.data).toStrictEqual({
39+
x: Float64Array.from([1, 2, 3]),
40+
y: new Float64Array([-3, -2, -1]),
41+
});
42+
});
43+
});
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { describe, it, expect } from 'vitest';
2+
3+
import { setMinY } from '../setMinY';
4+
5+
describe('setMinY', () => {
6+
it('default values', () => {
7+
const data = {
8+
x: Float64Array.from([1, 2, 3]),
9+
y: Float64Array.from([2, 3, 4]),
10+
};
11+
const result = setMinY(data, {});
12+
expect(result.data).toStrictEqual({
13+
x: Float64Array.from([1, 2, 3]),
14+
y: new Float64Array([0, 1, 2]),
15+
});
16+
expect(result.data).not.toBe(data);
17+
});
18+
19+
it('nothing to do', () => {
20+
const data = {
21+
x: Float64Array.from([1, 2, 3]),
22+
y: Float64Array.from([2, 3, 4]),
23+
};
24+
const result = setMinY(data, { min: 2 });
25+
expect(result.data).toStrictEqual({
26+
x: Float64Array.from([1, 2, 3]),
27+
y: new Float64Array([2, 3, 4]),
28+
});
29+
expect(result.data).toBe(data);
30+
});
31+
32+
it('negative value', () => {
33+
const data = {
34+
x: Float64Array.from([1, 2, 3]),
35+
y: Float64Array.from([2, 3, 4]),
36+
};
37+
const result = setMinY(data, { min: -1 });
38+
expect(result.data).toStrictEqual({
39+
x: Float64Array.from([1, 2, 3]),
40+
y: new Float64Array([-1, 0, 1]),
41+
});
42+
});
43+
});

src/filters/y/setMaxY.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { DataXY } from 'cheminfo-types';
2+
import { xAdd, xMaxValue } from 'ml-spectra-processing';
3+
4+
export interface SetMaxYFilter {
5+
name: 'setMaxY';
6+
options?: SetMaxYOptions;
7+
}
8+
9+
export interface SetMaxYOptions {
10+
/**
11+
* @default 1
12+
*/
13+
max?: number;
14+
}
15+
/**
16+
* Filter that allows to
17+
* @param data
18+
* @param options
19+
*/
20+
export function setMaxY(
21+
data: DataXY<Float64Array>,
22+
options: SetMaxYOptions = {},
23+
) {
24+
const { max = 1 } = options;
25+
const existingMax = xMaxValue(data.y);
26+
if (existingMax === max) {
27+
return { data };
28+
}
29+
return {
30+
data: {
31+
x: data.x,
32+
y: xAdd(data.y, max - existingMax),
33+
},
34+
};
35+
}

src/filters/y/setMinY.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { DataXY } from 'cheminfo-types';
2+
import { xAdd, xMinValue } from 'ml-spectra-processing';
3+
4+
export interface SetMinYFilter {
5+
name: 'setMinY';
6+
options?: SetMinYOptions;
7+
}
8+
9+
export interface SetMinYOptions {
10+
/**
11+
* @default 0
12+
*/
13+
min?: number;
14+
}
15+
/**
16+
* Filter that allows to
17+
* @param data
18+
* @param options
19+
*/
20+
export function setMinY(
21+
data: DataXY<Float64Array>,
22+
options: SetMinYOptions = {},
23+
) {
24+
const { min = 0 } = options;
25+
const existingMin = xMinValue(data.y);
26+
if (existingMin === min) {
27+
return { data };
28+
}
29+
return {
30+
data: {
31+
x: data.x,
32+
y: xAdd(data.y, min - existingMin),
33+
},
34+
};
35+
}

0 commit comments

Comments
 (0)