Skip to content

Commit

Permalink
feat(meanFilter): filter a serie2D by it's median (#36)
Browse files Browse the repository at this point in the history
* feat(meanFilter): filter a serie2D by it's median

* feat(meanFilter): allows to rename the filtered serie
  • Loading branch information
maasencioh authored Jul 13, 2017
1 parent 1025fdf commit e68ba9b
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 4 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
"chemcalc": "^3.3.3",
"jcampconverter": "^2.3.0",
"ml-array-max": "^1.0.0",
"ml-array-mean": "^1.0.0",
"ml-gsd": "^2.0.1",
"ml-regression-polynomial": "^1.0.2",
"mzmjs": "^0.2.0",
Expand Down
17 changes: 17 additions & 0 deletions src/Chromatogram.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {merge} from './util/merge';
import {getKovatsRescale} from './getKovatsRescale';
import {getClosestTime} from './util/getClosestTime';
import {applyLockMass} from './ms/applyLockMass';
import {meanFilter} from './ms/meanFilter';
import {toJSON} from './to/json';
import {getClosestData} from './util/getClosestData';

Expand Down Expand Up @@ -261,6 +262,22 @@ export class Chromatogram {
const json = JSON.parse(JSON.stringify(this));
return fromJSON(json);
}

/**
* Filter the given serie2D based on it's median value
* @param {string} serieName
* @param {object} [options]
* @param {string} [options.serieName = 'msMedian'] - Name of the new serie
* @param {number} [options.factor = 2] - The values under the median times this factor are removed
*/
meanFilter(serieName, options = {}) {
var serie = meanFilter(this, serieName, options);
if (options.serieName) {
this.series[options.serieName] = serie;
} else {
this.series.msMedian = serie;
}
}
}

Chromatogram.prototype.applyLockMass = applyLockMass;
Expand Down
62 changes: 62 additions & 0 deletions src/__tests__/ms/meanFilter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import {Chromatogram} from '../..';
import {meanFilter} from '../../ms/meanFilter';

test('simple case', () => {
let chromatogram = new Chromatogram(
[1, 2], {
ms: [
[[100, 200, 300], [10, 20, 300]],
[[600], [274]]
]
}
);

let ms = meanFilter(chromatogram, 'ms');
expect(ms.data).toEqual([
[[300], [300]],
[[], []]
]);
});

describe('inplace', () => {
it('default behavior', () => {
let chromatogram = new Chromatogram(
[1, 2], {
ms: [
[[100, 200, 300], [10, 20, 300]],
[[600], [274]]
]
}
);
chromatogram.meanFilter('ms');
expect(chromatogram.series.msMedian.data).toEqual([
[[300], [300]],
[[], []]
]);
expect(chromatogram.series.ms.data).toEqual([
[[100, 200, 300], [10, 20, 300]],
[[600], [274]]
]);
});

it('input name', () => {
let chromatogram = new Chromatogram(
[1, 2], {
ms: [
[[100, 200, 300], [10, 20, 300]],
[[600], [274]]
]
}
);
chromatogram.meanFilter('ms', {serieName: 'filtered'});
expect(chromatogram.series.filtered.data).toEqual([
[[300], [300]],
[[], []]
]);
expect(chromatogram.series.ms.data).toEqual([
[[100, 200, 300], [10, 20, 300]],
[[600], [274]]
]);
});
});

28 changes: 28 additions & 0 deletions src/ms/meanFilter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import arrayMean from 'ml-array-mean';
import {serieFromArray} from '../serieFromArray';

export function meanFilter(chromatogram, serieName, options = {}) {
const {
factor = 2
} = options;

var serie = chromatogram.getSerie(serieName);
var filtered = [];
for (var i = 0; i < serie.data.length; i++) {
filtered.push(applyFilter(serie.data[i], factor));
}

return serieFromArray(filtered);
}

function applyFilter(serie, factor) {
const meanIntensity = factor * arrayMean(serie[1]);
var filtered = [[], []];
for (var i = 0; i < serie[0].length; i++) {
if (serie[1][i] > meanIntensity) {
filtered[0].push(serie[0][i]);
filtered[1].push(serie[1][i]);
}
}
return filtered;
}
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4352,6 +4352,10 @@ ml-array-max@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/ml-array-max/-/ml-array-max-1.0.0.tgz#c2ee7d984dddb8cb77e6a1e29714a6f271a17a48"

ml-array-mean@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/ml-array-mean/-/ml-array-mean-1.0.0.tgz#0f51113f1fbaba7262c843a945234228fe87b157"

ml-array-utils@^0.3.0:
version "0.3.0"
resolved "https://registry.yarnpkg.com/ml-array-utils/-/ml-array-utils-0.3.0.tgz#c0cc5afdc1cb051f5a83b1ce71641c243672f649"
Expand Down Expand Up @@ -5483,10 +5487,6 @@ rollup@^0.42.0:
dependencies:
source-map-support "^0.4.0"

round-to@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/round-to/-/round-to-2.0.0.tgz#bcef4f2bcafd9480902c2142150b28c897f03e37"

run-async@^2.0.0, run-async@^2.2.0:
version "2.3.0"
resolved "https://registry.yarnpkg.com/run-async/-/run-async-2.3.0.tgz#0371ab4ae0bdd720d4166d7dfda64ff7a445a6c0"
Expand Down

0 comments on commit e68ba9b

Please sign in to comment.