From 94e36448cecd617edc9c62406f6a2253ee25a7db Mon Sep 17 00:00:00 2001 From: Soren Wacker Date: Mon, 17 Apr 2023 17:14:56 -0600 Subject: [PATCH] adjusted tests --- ms_mint/chromatogram.py | 12 ++++---- ms_mint/targets.py | 10 +++---- tests/test__chromatogram.py | 32 ++++++++++---------- tests/test__plotly_tools.py | 59 ++++++++----------------------------- 4 files changed, 39 insertions(+), 74 deletions(-) diff --git a/ms_mint/chromatogram.py b/ms_mint/chromatogram.py index 518d3ba7..74d6abe1 100644 --- a/ms_mint/chromatogram.py +++ b/ms_mint/chromatogram.py @@ -6,7 +6,7 @@ from .tools import find_peaks_in_timeseries, gaussian, mz_mean_width_to_min_max from .io import ms_file_to_df -from .filter import Resampler, Smoother, GaussFilter +from .filters import Resampler, Smoother, GaussFilter from .matplotlib_tools import plot_peaks @@ -21,10 +21,10 @@ def __init__( if intensities is not None: self.x = np.append(self.x, intensities) self.noise_level = None - if filter is None: - self.filter = [Resampler(), GaussFilter(), Smoother()] + if filters is None: + self.filters = [Resampler(), GaussFilter(), Smoother()] else: - self.filter = filter + self.filters = filters self.peaks = None self.selected_peak_ndxs = None if expected_rt is None and scan_times is not None: @@ -43,8 +43,8 @@ def estimate_noise_level(self, window=20): data = pd.Series(index=self.t, data=self.x) self.noise_level = data.rolling(window, center=True).std().median() - def apply_filter(self): - for filt in self.filter: + def apply_filters(self): + for filt in self.filters: self.t, self.x = filt.transform(self.t, self.x) def find_peaks(self, prominence=None, rel_height=0.9): diff --git a/ms_mint/targets.py b/ms_mint/targets.py index 9756d0fd..90fd8b7b 100644 --- a/ms_mint/targets.py +++ b/ms_mint/targets.py @@ -246,7 +246,7 @@ def rt_min_max( minimum_intensity=1e4, plot=False, sigma=20, - filter=None, + filters=None, post_opt=False, post_opt_kwargs=None, rel_height=0.9, @@ -271,8 +271,8 @@ def rt_min_max( :type plot: bool, optional :param sigma: Sigma value for peak selection, defaults to 20 :type sigma: float, optional - :param filter: Filter instances to apply in respective order, defaults to None - :type filter: ms_mint.filter.Filter, optional + :param filters: Filter instances to apply in respective order, defaults to None + :type filters: ms_mint.filters.Filter, optional :param post_opt: Optimize retention times after peak selection, defaults to False :type post_opt: bool, optional :param post_opt_kwargs: _description_, defaults to 20 @@ -312,7 +312,7 @@ def rt_min_max( _slice = extract_chromatogram_from_ms1(ms1, mz).groupby("scan_time").sum() chrom = Chromatogram( - _slice.index, _slice.values, expected_rt=rt, filter=filter + _slice.index, _slice.values, expected_rt=rt, filters=filters ) if chrom.x.max() < minimum_intensity: @@ -321,7 +321,7 @@ def rt_min_max( ) continue - chrom.apply_filter() + chrom.apply_filters() chrom.find_peaks(rel_height=rel_height) chrom.select_peak_with_gaussian_weight(rt, sigma) diff --git a/tests/test__chromatogram.py b/tests/test__chromatogram.py index 49b7e0a9..a27d197a 100644 --- a/tests/test__chromatogram.py +++ b/tests/test__chromatogram.py @@ -12,14 +12,14 @@ from paths import TEST_MZML -def test__Chromatogram_without_filter_identifies_peaks_correctly(): +def test__Chromatogram_without_filters_identifies_peaks_correctly(): rt_peak_1 = 200 rt_peak_2 = 500 x = np.arange(1000) y = gaussian(x, rt_peak_1, 20) * 5e6 + gaussian(x, rt_peak_2, 100) * 5e5 - chrom = Chromatogram(scan_times=x, intensities=y, filter=None, expected_rt=2) + chrom = Chromatogram(scan_times=x, intensities=y, filters=None, expected_rt=2) chrom.find_peaks() rt_peak_1_pred, rt_peak_2_pred = chrom.peaks.rt.values @@ -30,15 +30,15 @@ def test__Chromatogram_without_filter_identifies_peaks_correctly(): ] -def test__Chromatogram__with_filter_identifies_peaks_correctly(): +def test__Chromatogram__with_filters_identifies_peaks_correctly(): rt_peak_1 = 200 rt_peak_2 = 500 x = np.arange(1000) y = gaussian(x, rt_peak_1, 20) * 5e6 + gaussian(x, rt_peak_2, 100) * 5e5 - chrom = Chromatogram(scan_times=x, intensities=y, filter=[], expected_rt=2) - chrom.apply_filter() + chrom = Chromatogram(scan_times=x, intensities=y, filters=[], expected_rt=2) + chrom.apply_filters() chrom.find_peaks() rt_peak_1_pred, rt_peak_2_pred = chrom.peaks.rt.values @@ -47,7 +47,7 @@ def test__Chromatogram__with_filter_identifies_peaks_correctly(): assert max_diff < 1 -def test__Chromatogram__with_filter_and_opt_identifies_peaks_correctly(): +def test__Chromatogram__with_filters_and_opt_identifies_peaks_correctly(): rt_peak_1 = 200 rt_peak_2 = 500 @@ -55,8 +55,8 @@ def test__Chromatogram__with_filter_and_opt_identifies_peaks_correctly(): y = gaussian(x, rt_peak_1, 20) * 5e6 + gaussian(x, rt_peak_2, 100) * 5e5 - chrom = Chromatogram(scan_times=x, intensities=y, filter=None, expected_rt=2) - chrom.apply_filter() + chrom = Chromatogram(scan_times=x, intensities=y, filters=None, expected_rt=2) + chrom.apply_filters() chrom.find_peaks() chrom.optimise_peak_times_with_diff() rt_peak_1_pred, rt_peak_2_pred = chrom.peaks.rt.values @@ -75,7 +75,7 @@ def test__Chromatogram__select_peak_by_rt(): y = gaussian(x, rt_peak_1, 20) * 5e6 + gaussian(x, rt_peak_2, 100) * 5e5 chrom = Chromatogram( - scan_times=x, intensities=y, filter=None, expected_rt=rt_peak_1 + scan_times=x, intensities=y, filters=None, expected_rt=rt_peak_1 ) chrom.find_peaks() chrom.select_peak_by_rt() @@ -95,7 +95,7 @@ def test__Chromatogram__select_peak_by_rt_as_argument(): y = gaussian(x, rt_peak_1, 20) * 5e6 + gaussian(x, rt_peak_2, 100) * 5e5 chrom = Chromatogram( - scan_times=x, intensities=y, filter=None, expected_rt=rt_peak_1 + scan_times=x, intensities=y, filters=None, expected_rt=rt_peak_1 ) chrom.find_peaks() chrom.select_peak_by_rt(rt_peak_2) @@ -115,7 +115,7 @@ def test__Chromatogram__select_highest(): y = gaussian(x, rt_peak_1, 20) * 5e6 + gaussian(x, rt_peak_1, 100) * 5e5 chrom = Chromatogram( - scan_times=x, intensities=y, filter=None, expected_rt=rt_peak_2 + scan_times=x, intensities=y, filters=None, expected_rt=rt_peak_2 ) chrom.find_peaks() chrom.select_peak_by_highest_intensity() @@ -144,7 +144,7 @@ def test__Chromatogram__select_peak_with_gaussian_prefers_higher_peak(): ) chrom = Chromatogram( - scan_times=x, intensities=y, filter=None, expected_rt=expected_rt + scan_times=x, intensities=y, filters=None, expected_rt=expected_rt ) chrom.find_peaks() chrom.select_peak_with_gaussian_weight() @@ -163,7 +163,7 @@ def test__Chromatogram__plot_runs_without_error_return_figure(): y = gaussian(x, peak_rt, 200) * peak_in - chrom = Chromatogram(scan_times=x, intensities=y, filter=None) + chrom = Chromatogram(scan_times=x, intensities=y, filters=None) chrom.find_peaks() fig = chrom.plot() @@ -174,7 +174,7 @@ def test__Chromatogram__plot_runs_without_error_return_figure(): def test__Chromatogram_from_files_runs_through(): chrom = Chromatogram() chrom.from_file(TEST_MZML, mz_mean=101.024323, mz_width=10) - chrom.apply_filter() + chrom.apply_filters() chrom.find_peaks() chrom.select_peak_by_highest_intensity() peaks = chrom.selected_peak_ndxs @@ -194,7 +194,7 @@ def test__Chromatogram__optimise_peak_times_with_diff_with_plot(): y = gaussian(x, rt_peak_1, 20) * 5e6 + gaussian(x, rt_peak_1, 100) * 5e5 chrom = Chromatogram( - scan_times=x, intensities=y, filter=None, expected_rt=rt_peak_2 + scan_times=x, intensities=y, filters=None, expected_rt=rt_peak_2 ) chrom.find_peaks() chrom.optimise_peak_times_with_diff(plot=True) @@ -209,7 +209,7 @@ def test__Chromatogram__data(): y = gaussian(x, rt_peak_1, 20) * 5e6 + gaussian(x, rt_peak_1, 100) * 5e5 chrom = Chromatogram( - scan_times=x, intensities=y, filter=None, expected_rt=rt_peak_2 + scan_times=x, intensities=y, filters=None, expected_rt=rt_peak_2 ) data = chrom.data diff --git a/tests/test__plotly_tools.py b/tests/test__plotly_tools.py index 1fcb48c4..d2877aac 100644 --- a/tests/test__plotly_tools.py +++ b/tests/test__plotly_tools.py @@ -1,63 +1,28 @@ import pandas as pd import numpy as np - +import pytest from plotly.graph_objs._figure import Figure - from ms_mint.plotly_tools import ( set_template, plotly_heatmap, ) - -def test__plotly_heatmap(): - N = 10 - data = np.random.uniform(size=(N, N)) + np.arange(N) - N / 2 - df = pd.DataFrame(data) - img = plotly_heatmap(df) - assert isinstance(img, Figure), type(img) - - -def test__plotly_heatmap__transposed(): - N = 10 - data = np.random.uniform(size=(N, N)) + np.arange(N) - N / 2 - df = pd.DataFrame(data) - img = plotly_heatmap(df, transposed=True) - assert isinstance(img, Figure), type(img) - - -def test__plotly_heatmap__normed_by_cols(): - N = 10 - data = np.random.uniform(size=(N, N)) + np.arange(N) - N / 2 - df = pd.DataFrame(data) - img = plotly_heatmap(df, normed_by_cols=True) - assert isinstance(img, Figure), type(img) - - -def test__plotly_heatmap__correlation(): +@pytest.mark.parametrize("transposed,normed_by_cols,correlation,clustered,add_dendrogram", [ + (False, False, False, False, False), + (True, False, False, False, False), + (False, True, False, False, False), + (False, False, True, False, False), + (False, False, False, True, True), + (False, False, True, True, False), +]) +def test__plotly_heatmap(transposed, normed_by_cols, correlation, clustered, add_dendrogram): N = 10 data = np.random.uniform(size=(N, N)) + np.arange(N) - N / 2 df = pd.DataFrame(data) - img = plotly_heatmap(df, correlation=True) + img = plotly_heatmap(df, transposed=transposed, normed_by_cols=normed_by_cols, correlation=correlation, clustered=clustered, add_dendrogram=add_dendrogram) assert isinstance(img, Figure), type(img) - -def test__plotly_heatmap__clustered_with_dendrogram(): - N = 10 - data = np.random.uniform(size=(N, N)) + np.arange(N) - N / 2 - df = pd.DataFrame(data) - img = plotly_heatmap(df, clustered=True, add_dendrogram=True) - assert isinstance(img, Figure), type(img) - - -def test__plotly_heatmap__clustered_correlation(): - N = 10 - data = np.random.uniform(size=(N, N)) + np.arange(N) - N / 2 - df = pd.DataFrame(data) - img = plotly_heatmap(df, clustered=True, add_dendrogram=False, correlation=True) - assert isinstance(img, Figure), type(img) - - def test__set_template(): set_template() - assert True + assert True \ No newline at end of file