def test_new_physio_like(): fname = testutils.get_test_data_path('ECG.csv') data = physio.Physio(np.loadtxt(fname), fs=1000.) data._history = [('does history', 'copy?')] data._metadata['peaks'] = np.array([1, 2, 3]) # assert all copies happen by default new_data = utils.new_physio_like(data, data[:]) assert np.allclose(data, utils.new_physio_like(data, data[:])) assert new_data.fs == data.fs assert new_data.data.dtype == data.data.dtype assert new_data.history == data.history assert new_data._metadata == data._metadata # check if changes apply new_data = utils.new_physio_like(data, data[:], fs=50, dtype=int, copy_history=False, copy_metadata=False) assert np.allclose(data, utils.new_physio_like(data, data[:])) assert new_data.fs == 50 assert new_data.data.dtype == int assert new_data.history == [] for k, v in new_data._metadata.items(): assert v.size == 0
def test_save_history(tmpdir): # get paths of data, original history, new history fname = get_test_data_path('ECG.csv') orig_history = get_test_data_path('history.json') temp_history = tmpdir.join('tmp').purebasename # make physio object and perform some operations phys = physio.Physio(np.loadtxt(fname), fs=1000.) with pytest.warns(UserWarning): # no history = warning io.save_history(temp_history, phys) filt = operations.filter_physio(phys, [5., 15.], 'bandpass') path = io.save_history(temp_history, filt) # dump history= # load both original and new json and ensure equality with open(path, 'r') as src: hist = json.load(src) with open(orig_history, 'r') as src: orig = json.load(src) assert hist == orig
def test_load_rtpeaks(): for channel in [1, 2, 9]: with pytest.warns(UserWarning): hist = dict(fname=DATA, channel=channel, fs=1000.) phys = external.load_rtpeaks(DATA, channel=channel, fs=1000.) assert phys.history == [('load_rtpeaks', hist)] assert phys.fs == 1000. with pytest.raises(ValueError): external.load_rtpeaks(testutils.get_test_data_path('ECG.csv'), channel=channel, fs=1000.)
def test_check_physio(): fname = testutils.get_test_data_path('ECG.csv') data = physio.Physio(np.loadtxt(fname), fs=1000.) # check that `ensure_fs` is functional with pytest.raises(ValueError): utils.check_physio(fname) assert isinstance(utils.check_physio(fname, False), physio.Physio) # "normal" instance should just pass assert isinstance(utils.check_physio(data), physio.Physio) # check that copy works assert utils.check_physio(data) == data assert utils.check_physio(data, copy=True) != data
def test_load_physio(): # try loading pickle file (from io.save_physio) pckl = io.load_physio(get_test_data_path('ECG.phys'), allow_pickle=True) assert isinstance(pckl, physio.Physio) assert pckl.data.size == 44611 assert pckl.fs == 1000. with pytest.warns(UserWarning): pckl = io.load_physio(get_test_data_path('ECG.phys'), fs=500., allow_pickle=True) assert pckl.fs == 500. # try loading CSV file csv = io.load_physio(get_test_data_path('ECG.csv')) assert isinstance(csv, physio.Physio) assert np.allclose(csv, pckl) assert np.isnan(csv.fs) assert csv.history[0][0] == 'load_physio' # try loading array with pytest.warns(UserWarning): arr = io.load_physio(np.loadtxt(get_test_data_path('ECG.csv'))) assert isinstance(arr, physio.Physio) arr = io.load_physio(np.loadtxt(get_test_data_path('ECG.csv')), history=[('np.loadtxt', { 'fname': 'ECG.csv' })]) assert isinstance(arr, physio.Physio) # try loading physio object (and resetting dtype) out = io.load_physio(arr, dtype='float32') assert out.data.dtype == np.dtype('float32') assert out.history[0][0] == 'np.loadtxt' assert out.history[-1][0] == 'load_physio' with pytest.raises(TypeError): io.load_physio([1, 2, 3])
def test_load_history(tmpdir): # get paths of data, new history fname = get_test_data_path('ECG.csv') temp_history = tmpdir.join('tmp').purebasename # make physio object and perform some operations phys = io.load_physio(fname, fs=1000.) filt = operations.filter_physio(phys, [5., 15.], 'bandpass') # save history to file and recreate new object from history path = io.save_history(temp_history, filt) replayed = io.load_history(path, verbose=True) # ensure objects are the same assert np.allclose(filt, replayed) assert filt.history == replayed.history assert filt.fs == replayed.fs
def test_save_physio(tmpdir): pckl = io.load_physio(get_test_data_path('ECG.phys'), allow_pickle=True) out = io.save_physio(tmpdir.join('tmp').purebasename, pckl) assert os.path.exists(out) assert isinstance(io.load_physio(out, allow_pickle=True), physio.Physio)
# Add any paths that contain custom static files (such as style sheets) here, # relative to this directory. They are copied after the builtin static files, # so a file named "default.css" will overwrite the builtin "default.css". html_static_path = ['_static'] # -- Options for HTMLHelp output --------------------------------------------- # Output file base name for HTML help builder. htmlhelp_basename = 'peakdetdoc' # -- Extension configuration ------------------------------------------------- intersphinx_mapping = { 'matplotlib': ('https://matplotlib.org', None), 'numpy': ('https://docs.scipy.org/doc/numpy', None), 'scipy': ('https://docs.scipy.org/doc/scipy/reference', None), } doctest_global_setup = """ import os from peakdet.tests.utils import get_test_data_path os.chdir(get_test_data_path()) """ from peakdet.tests.utils import get_test_data_path # noqa plot_working_directory = get_test_data_path() plot_include_source = True plot_formats = [("png", 90)] plot_html_show_formats = False plot_html_show_source_link = False
# -*- coding: utf-8 -*- import pytest from peakdet import external from peakdet.tests import utils as testutils DATA = testutils.get_test_data_path('rtpeaks.csv') def test_load_rtpeaks(): for channel in [1, 2, 9]: with pytest.warns(UserWarning): hist = dict(fname=DATA, channel=channel, fs=1000.) phys = external.load_rtpeaks(DATA, channel=channel, fs=1000.) assert phys.history == [('load_rtpeaks', hist)] assert phys.fs == 1000. with pytest.raises(ValueError): external.load_rtpeaks(testutils.get_test_data_path('ECG.csv'), channel=channel, fs=1000.)
# -*- coding: utf-8 -*- import numpy as np import pytest from peakdet.physio import Physio from peakdet.tests import utils as testutils DATA = np.loadtxt(testutils.get_test_data_path('ECG.csv')) PROPERTIES = ['data', 'fs', 'history', 'peaks', 'troughs', '_masked'] PHYSIO_TESTS = [ # accepts "correct" inputs for history dict(kwargs=dict(data=DATA, history=[('good', 'history')])), # fails on bad inputs for history dict(kwargs=dict(data=DATA, history=['malformed', 'history']), raises=TypeError), dict(kwargs=dict(data=DATA, history='not real history'), raises=TypeError), # accepts "correct" for metadata dict(kwargs=dict(data=DATA, metadata=dict())), dict(kwargs=dict(data=DATA, metadata=dict(peaks=[], reject=[], troughs=[]))), # fails on bad inputs for metadata dict(kwargs=dict(data=DATA, metadata=[]), raises=TypeError), dict(kwargs=dict(data=DATA, metadata=dict(peaks={})), raises=TypeError), # fails on bad inputs for data dict(kwargs=dict(data=np.column_stack([DATA, DATA])), raises=ValueError), dict(kwargs=dict(data='hello'), raises=ValueError) ] def test_physio(): phys = Physio(DATA, fs=1000)