-
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.
feat: add xBoxPlotWithOutliers mehtod to deal with possible outliers (#…
…260) This removes the previous option in xBoxPlot that would have yield to different types
- Loading branch information
Showing
6 changed files
with
162 additions
and
45 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
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,116 @@ | ||
import { expect, test } from 'vitest'; | ||
|
||
import { xBoxPlotWithOutliers } from '../xBoxPlotWithOutliers'; | ||
|
||
test('test xBoxPlotWithOutliers even', () => { | ||
const array = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]; | ||
expect(xBoxPlotWithOutliers(array)).toStrictEqual({ | ||
q1: 2.5, | ||
median: 5.5, | ||
q3: 8.5, | ||
min: 0, | ||
max: 11, | ||
outliers: [], | ||
}); | ||
}); | ||
|
||
test('test xBoxPlotWithOutliers even small', () => { | ||
const array = [0, 1, 2, 3, 4, 5]; | ||
expect(xBoxPlotWithOutliers(array)).toStrictEqual({ | ||
q1: 1, | ||
median: 2.5, | ||
q3: 4, | ||
min: 0, | ||
max: 5, | ||
outliers: [], | ||
}); | ||
}); | ||
|
||
test('test xBoxPlotWithOutliers odd', () => { | ||
const array = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; | ||
expect(xBoxPlotWithOutliers(array)).toStrictEqual({ | ||
q1: 2, | ||
median: 5, | ||
q3: 8, | ||
min: 0, | ||
max: 10, | ||
outliers: [], | ||
}); | ||
}); | ||
|
||
test('test xBoxPlotWithOutliers odd small', () => { | ||
const array = [0, 1, 2, 3, 4]; | ||
expect(xBoxPlotWithOutliers(array)).toStrictEqual({ | ||
q1: 0.5, | ||
median: 2, | ||
q3: 3.5, | ||
min: 0, | ||
max: 4, | ||
outliers: [], | ||
}); | ||
}); | ||
|
||
test('test xBoxPlotWithOutliers too small', () => { | ||
const array = [0, 1, 2, 3]; | ||
expect(() => xBoxPlotWithOutliers(array)).toThrow( | ||
'can not calculate info if array contains less than 5 elements', | ||
); | ||
}); | ||
|
||
test('test xBoxPlotWithOutliers with one element', () => { | ||
const array = [42]; | ||
expect(() => xBoxPlotWithOutliers(array)).toThrow( | ||
'can not calculate info if array contains less than 5 elements', | ||
); | ||
}); | ||
|
||
test('test xBoxPlotWithOutliers with one element', () => { | ||
const array = [42]; | ||
const result = xBoxPlotWithOutliers(array, { allowSmallArray: true }); | ||
expect(result).toStrictEqual({ | ||
q1: 42, | ||
median: 42, | ||
q3: 42, | ||
min: 42, | ||
max: 42, | ||
outliers: [], | ||
}); | ||
}); | ||
|
||
test('test xBoxPlotWithOutliers with 2 elements', () => { | ||
const array = [40, 44]; | ||
const result = xBoxPlotWithOutliers(array, { allowSmallArray: true }); | ||
expect(result).toStrictEqual({ | ||
q1: 40, | ||
median: 42, | ||
q3: 44, | ||
min: 40, | ||
max: 44, | ||
outliers: [], | ||
}); | ||
}); | ||
|
||
test('test xBoxPlotWithOutliers with 3 elements', () => { | ||
const array = [40, 42, 44]; | ||
const result = xBoxPlotWithOutliers(array, { allowSmallArray: true }); | ||
expect(result).toStrictEqual({ | ||
q1: 40, | ||
median: 42, | ||
q3: 44, | ||
min: 40, | ||
max: 44, | ||
outliers: [], | ||
}); | ||
}); | ||
|
||
test('outliers', () => { | ||
const array = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 100]; | ||
expect(xBoxPlotWithOutliers(array)).toStrictEqual({ | ||
q1: 2, | ||
median: 5, | ||
q3: 8, | ||
min: 0, | ||
max: 9, | ||
outliers: [100], | ||
}); | ||
}); |
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
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,43 @@ | ||
import { NumberArray } from 'cheminfo-types'; | ||
|
||
import { xBoxPlot, XBoxPlotOptions } from './xBoxPlot'; | ||
|
||
export interface XBoxPlotWithOutliers { | ||
q1: number; | ||
median: number; | ||
q3: number; | ||
min: number; | ||
max: number; | ||
outliers: number[]; | ||
} | ||
|
||
/** | ||
* Calculating the box plot of the array | ||
* @param array - data | ||
* @param options | ||
*/ | ||
export function xBoxPlotWithOutliers( | ||
array: NumberArray, | ||
options: XBoxPlotOptions = {}, | ||
): XBoxPlotWithOutliers { | ||
const info: XBoxPlotWithOutliers = { | ||
...xBoxPlot(array, options), | ||
outliers: [], | ||
}; | ||
|
||
const iqr = info.q3 - info.q1; | ||
const min = info.q1 - 1.5 * iqr; | ||
const max = info.q3 + 1.5 * iqr; | ||
// we need to recalculate the min and the max because they could be outliers | ||
info.min = info.median; | ||
info.max = info.median; | ||
for (const value of array) { | ||
if (value < min || value > max) { | ||
info.outliers.push(value); | ||
} else { | ||
if (value < info.min) info.min = value; | ||
if (value > info.max) info.max = value; | ||
} | ||
} | ||
return info; | ||
} |