Skip to content

Commit

Permalink
add show_plot argument to plot_z
Browse files Browse the repository at this point in the history
add tests for plot_z function
  • Loading branch information
schuenke committed Jul 16, 2024
1 parent ee4fb2c commit 071d217
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/bmctool/utils/eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ def plot_z(
title: str = 'spectrum',
x_label: str = 'offsets [ppm]',
y_label: str = 'signal',
show_plot: bool = True,
) -> Figure:
"""Plot Z-spectrum according to the given parameters.
Expand All @@ -153,6 +154,8 @@ def plot_z(
Label of x-axis, by default "offsets [ppm]"
y_label : str, optional
Label of y-axis, by default "signal"
show_plot : bool, optional
Flag to show the plot, by default True
Returns
-------
Expand Down Expand Up @@ -185,6 +188,7 @@ def plot_z(
fig.tight_layout()

plt.title(title)
plt.show()
if show_plot:
plt.show()

return fig
52 changes: 52 additions & 0 deletions tests/utils/test_eval_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
import pytest
from bmctool.utils.eval import calc_mtr_asym
from bmctool.utils.eval import normalize_data
from bmctool.utils.eval import plot_z
from bmctool.utils.eval import split_data
from matplotlib.figure import Figure


def test_calc_mtr_asym():
Expand Down Expand Up @@ -32,6 +34,10 @@ def test_calc_mtr_asym():
expected_asym = np.array([0, 0, 0, 0, 0.5, 0, 0, 0, 0, 0, -0.5, 0, 0], dtype=float)
np.testing.assert_array_almost_equal(calc_mtr_asym(m_z, offsets), expected_asym)

# wrong dimensions
with pytest.raises(ValueError, match='m_z and offsets must have the same dimensions.'):
calc_mtr_asym(np.array([1, 2, 3]), np.array([1, 2, 3, 4]))


@pytest.mark.parametrize(
('threshold', 'expected_norm_value'),
Expand Down Expand Up @@ -116,3 +122,49 @@ def test_split_data(m_z, offsets, threshold, expected_offsets, expected_data, ex
assert result_norm is None, f'expected None, got {result_norm}'
else:
assert np.array_equal(result_norm, expected_norm_data), f'expected {expected_norm_data}, got {result_norm}' # type: ignore


def test_split_data_raises():
"""Test the split_data function with invalid threshold values."""
with pytest.raises(TypeError, match='Threshold of type'):
split_data(np.array([1, 2, 3]), np.array([1, 2, 3, 4]), 'str') # type: ignore


def test_plot_z():
"""Test the plot_z function."""

# define offsets
offsets = np.linspace(-6, 6, 13)
m_z = np.array([1, 0.75, 0.5, 0.75, 1, 1, 0, 1, 1, 1, 1, 1, 1])

# Test case 1: Basic test case with default parameters
expected_fig = plot_z(m_z=m_z, show_plot=False)
assert isinstance(expected_fig, Figure)

# Test case 2: Test with custom offsets
expected_fig = plot_z(m_z=m_z, offsets=offsets, show_plot=False)
assert isinstance(expected_fig, Figure)

# Test case 3: Test with norm=True, but no threshold
normalize = True
expected_fig = plot_z(m_z=m_z, offsets=offsets, normalize=normalize, show_plot=False)
assert isinstance(expected_fig, Figure)

# Test case 4: Test with normalization and custom threshold
expected_fig = plot_z(m_z=m_z, offsets=offsets, normalize=normalize, norm_threshold=6, show_plot=False)
assert isinstance(expected_fig, Figure)

# Test case 5: Test without inverted x-axis
expected_fig = plot_z(m_z=m_z, offsets=offsets, invert_ax=False, show_plot=False)
assert isinstance(expected_fig, Figure)

# Test case 6: Test with MTRasym plot
expected_fig = plot_z(m_z=m_z, offsets=offsets, plot_mtr_asym=True, show_plot=False)
assert isinstance(expected_fig, Figure)

# Test case 7: Test with custom title, x_label, and y_label
title = 'Custom Title'
x_label = 'Custom X Label'
y_label = 'Custom Y Label'
expected_fig = plot_z(m_z=m_z, offsets=offsets, title=title, x_label=x_label, y_label=y_label, show_plot=False)
assert isinstance(expected_fig, Figure)

0 comments on commit 071d217

Please sign in to comment.