Skip to content

Commit

Permalink
feat: add xyFilterMinYValue
Browse files Browse the repository at this point in the history
  • Loading branch information
lpatiny committed Dec 19, 2022
1 parent ad1c418 commit 5845edd
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ export * from './xy/xyCumulativeDistributionStatistics';
export * from './xy/xyEnsureGrowingX';
export * from './xy/xyEquallySpaced';
export * from './xy/xyExtract';
export * from './xy/xyFilterMinYValue';
export * from './xy/xyFilterX';
export * from './xy/xyFilterXPositive';
export * from './xy/xyGetNMaxY';
Expand Down
26 changes: 26 additions & 0 deletions src/xy/__tests__/xyFilterMinYValue.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { xyFilterMinYValue } from '../xyFilterMinYValue';

describe('xyFilterMinYValue', () => {
const x = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
const y = [10, 8, 6, 4, 2, 0, 2, 4, 6, 8, 10];
it('no threshold', () => {
const results = xyFilterMinYValue({ x, y });
expect(results).toStrictEqual({ x, y });
expect(results.x).toBe(x);
expect(results.y).toBe(y);
});
it('threshold very low', () => {
const results = xyFilterMinYValue({ x, y }, 0);
expect(results).toStrictEqual({ x, y });
expect(results.x).toBe(x);
expect(results.y).toBe(y);
});
it('threshold', () => {
const results = xyFilterMinYValue({ x, y }, 0.8);
expect(results).toStrictEqual({ x: [0, 1, 9, 10], y: [10, 8, 8, 10] });
});
it('threshold 1.1', () => {
const results = xyFilterMinYValue({ x, y }, 1.1);
expect(results).toStrictEqual({ x: [], y: [] });
});
});
33 changes: 33 additions & 0 deletions src/xy/xyFilterMinYValue.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { DataXY } from 'cheminfo-types';

import { xMinMaxValues } from '../..';

/** Filter an array x/y based on various criteria x points are expected to be sorted
*
* @param data - object containing 2 properties x and y
* @param minRelativeYValue - the minimum relative value compare to the Y max value
* @return filtered data
*/
export function xyFilterMinYValue(data: DataXY, minRelativeYValue?: number) {
if (minRelativeYValue === undefined) return data;

const { x, y } = data;

const { min, max } = xMinMaxValues(y);
const threshold = max * minRelativeYValue;
if (min >= threshold) return data;

const newX: number[] = [];
const newY: number[] = [];
for (let i = 0; i < x.length; i++) {
if (y[i] >= threshold) {
newX.push(x[i]);
newY.push(y[i]);
}
}

return {
x: newX,
y: newY,
};
}

0 comments on commit 5845edd

Please sign in to comment.