-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
60 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: [] }); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; | ||
} |