Skip to content

Commit

Permalink
adjusted tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sorenwacker committed Apr 17, 2023
1 parent c236882 commit 94e3644
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 74 deletions.
12 changes: 6 additions & 6 deletions ms_mint/chromatogram.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand All @@ -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:
Expand All @@ -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):
Expand Down
10 changes: 5 additions & 5 deletions ms_mint/targets.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -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:
Expand All @@ -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)

Expand Down
32 changes: 16 additions & 16 deletions tests/test__chromatogram.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand All @@ -47,16 +47,16 @@ 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

x = np.arange(1, 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.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
Expand All @@ -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()
Expand All @@ -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)
Expand All @@ -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()
Expand Down Expand Up @@ -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()
Expand All @@ -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()
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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
Expand Down
59 changes: 12 additions & 47 deletions tests/test__plotly_tools.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 94e3644

Please sign in to comment.