Skip to content

Commit be15b27

Browse files
committed
Update filters: filter_mz_ranges
1 parent cd921e1 commit be15b27

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

dimspy/process/peak_filters.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -67,27 +67,28 @@ def filter_ringing(pl, threshold, bin_size=1.0, flag_name='ringing_flag', flag_i
6767
return pl.add_attribute(flag_name, pl.intensity > (mask * threshold), is_flag=True, on_index=flag_index)
6868

6969

70-
def filter_mz_ranges(pl, mz_remove_rngs, flag_name='mz_range_remove_flag', flag_index=None):
70+
def filter_mz_ranges(pl, mz_ranges, flag_name='mz_ranges_flag', flagged_only=False, flag_index=None):
7171
"""
7272
Peaklist mz range filter.
73-
7473
:param pl: the target peaklist
75-
:param mz_remove_rngs: the mz ranges to remove. Must be in the format of [(mz_min1, mz_max2), (mz_min2, mz_max2), ...]
74+
:param mz_ranges: the mz ranges to remove. Must be in the format of [(mz_min1, mz_max2), (mz_min2, mz_max2), ...]
7675
:param flag_name: name of the new flag attribute. Default = 'mz_range_remove_flag'
7776
:param flag_index: index of the new flag to be inserted into the peaklist. Default = None
7877
:rtype: PeakList object
79-
8078
This filter will remove all the peaks whose mz values are within any of the ranges in the mz_remove_rngs.
81-
8279
"""
83-
mzrs_removed_flags = np.ones(pl.shape[0], dtype=bool)
84-
for mzr in mz_remove_rngs:
80+
if flagged_only:
81+
flags = np.ones(pl.shape[0], dtype=bool)
82+
else:
83+
flags = np.ones(pl.full_size, dtype=bool)
84+
85+
for mzr in mz_ranges:
8586
if len(mzr) != 2:
8687
raise ValueError('mzr_remove: Provide a list of "start" and "end" values for each m/z range that needs to be removed.')
8788
if mzr[0] >= mzr[1]:
8889
raise ValueError('mzr_remove: Start value cannot be larger then end value.')
89-
mzrs_removed_flags[(pl.mz >= mzr[0]) & (pl.mz <= mzr[1])] = False
90-
pl.add_attribute(flag_name, mzrs_removed_flags, is_flag=True, on_index=flag_index)
90+
flags[(pl.get_attribute("mz", flagged_only) >= mzr[0]) & (pl.get_attribute("mz", flagged_only) <= mzr[1])] = False
91+
pl.add_attribute(flag_name, flags, flagged_only=flagged_only, is_flag=True, on_index=flag_index)
9192
return pl
9293

9394

0 commit comments

Comments
 (0)