Commit af84fe54 authored by Antoine Laudrain's avatar Antoine Laudrain
Browse files

MSO: add ut for read_data.

parent 0f49d2d6
2,6,3,1,1.00000000E-10,-4.00183944316E-07,0E+00,2.63584E-05,-5.73431E-01,0E+00,2,2.00000E-06,-4.00000000000E-07,1.60000E+00,-6.00000E-01,"22 APR 2021","20:16:36:00","MSOS254A:MY58150113",2,100,2,1,2.10084E+09,0E+00,5
#0 SÀSdTÀTT4T T$T@TlT°TðTÈT<T”S
......@@ -14,7 +14,7 @@ import numpy as np
from helpers_wav import (
get_time_points, process_waveform, process_sequence,
process_data_file)
process_data_file, read_data)
###############################################################################
......@@ -33,6 +33,19 @@ FILE1_WAVEFORMS = [
np.array([ 0.00429241, 0.00418698, 0.0064538 , 0.01299068, 0.00039137])
]
FILE2_PATH = "test/data2.210422_153404.CHAN1.raw"
FILE2_TIME_POINTS = np.array([
-4.00183944e-07, -4.00083944e-07, -3.99983944e-07
])
FILE2_WAVEFORMS = [
np.array([-0.00915037, -0.0083069 , -0.00398413]),
np.array([-0.00155915, -0.00282436, -0.00524933]),
np.array([-0.00630367, -0.00567106, -0.00493303]),
np.array([-0.00377326, -0.00198089, -0.00029395]),
np.array([-0.00134829, -0.00503846, -0.00946667])
]
###############################################################################
class TestGetTimePoints(ut.TestCase):
......@@ -101,6 +114,8 @@ class TestProcessSequence(ut.TestCase):
)
###############################################################################
class TestProcessDataFile(ut.TestCase):
"""Test cases for process_data_file."""
......@@ -144,6 +159,206 @@ class TestProcessDataFile(ut.TestCase):
_, waveforms = process_data_file(FILE1_PATH)
np.testing.assert_allclose(waveforms, FILE1_WAVEFORMS, rtol=1e-4)
###############################################################################
class TestReadData(ut.TestCase):
"""Test cases for read_data."""
###########################################################################
# Test single string input instead of list of strings.
def test_one_file_is_list_of_one_file_noflatten_time_points(self):
"""Providing string without wildcard should be one file."""
np.testing.assert_allclose(
read_data(FILE1_PATH, flatten=False)[0],
read_data([FILE1_PATH], flatten=False)[0]
)
def test_one_file_is_list_of_one_file_noflatten_waveform(self):
"""Providing string without wildcard should be one file."""
np.testing.assert_allclose(
read_data(FILE1_PATH, flatten=False)[1],
read_data([FILE1_PATH], flatten=False)[1]
)
def test_one_file_is_list_of_one_file_flatten_time_points(self):
"""Providing string without wildcard should be one file."""
np.testing.assert_allclose(
read_data(FILE1_PATH, flatten=True)[0],
read_data([FILE1_PATH], flatten=True)[0]
)
def test_one_file_is_list_of_one_file_flatten_waveform(self):
"""Providing string without wildcard should be one file."""
np.testing.assert_allclose(
read_data(FILE1_PATH, flatten=True)[1],
read_data([FILE1_PATH], flatten=True)[1]
)
###########################################################################
# One file
def test_one_file_noflatten_time_points(self):
"Processing one file should give one more level as process_data_file."
time_points, _ = read_data(FILE1_PATH, flatten=False)
np.testing.assert_allclose(
time_points, [FILE1_TIME_POINTS]
)
def test_one_file_noflatten_waveforms(self):
"Processing one file should give one more level as process_data_file."
_, waveforms = read_data(FILE1_PATH, flatten=False)
np.testing.assert_allclose(
waveforms, [FILE1_WAVEFORMS], rtol=1e-4
)
def test_one_file_flatten_time_points(self):
"""Test time points after flattening one file."""
time_points, _ = read_data(FILE1_PATH, flatten=True)
np.testing.assert_allclose(
time_points,
[FILE1_TIME_POINTS] * len(FILE1_WAVEFORMS)
)
def test_one_file_flatten_waveforms(self):
"""Test waveforms after flattening one file."""
_, waveforms = read_data(FILE1_PATH, flatten=True)
np.testing.assert_allclose(waveforms, FILE1_WAVEFORMS, rtol=1e-4)
###########################################################################
# Multiple file
def test_multiple_files_noflatten_time_points(self):
"""Test time_points in processing of multiple files."""
time_points, _ = read_data([FILE1_PATH, FILE2_PATH], flatten=False)
expected = [FILE1_TIME_POINTS, FILE2_TIME_POINTS]
self.assertEqual(type(time_points), type(expected),
msg="Wrong type (given: {}, expected {}).".format(
type(time_points), type(expected))
)
self.assertEqual(len(time_points), len(expected),
msg="Wrong length (given: {}, expected {}).\n{}\n{}".format(
len(time_points), len(expected), time_points, expected)
)
for idx in range(len(time_points)):
np.testing.assert_allclose(
time_points[idx], expected[idx], rtol=1e-4)
def test_multiple_files_noflatten_waveforms(self):
"""Test waveforms in processing of multiple files."""
_, waveforms = read_data([FILE1_PATH, FILE2_PATH], flatten=False)
expected = [FILE1_WAVEFORMS, FILE2_WAVEFORMS]
self.assertEqual(type(waveforms), type(expected),
msg="Wrong type (given: {}, expected {}).".format(
type(waveforms), type(expected))
)
self.assertEqual(len(waveforms), len(expected),
msg="Wrong length (given: {}, expected {}).\n{}\n{}".format(
len(waveforms), len(expected), waveforms, expected)
)
for idx in range(len(waveforms)):
np.testing.assert_allclose(
waveforms[idx], expected[idx], rtol=1e-4)
def test_multiple_files_flatten_time_points(self):
"""Test time points after flattening several file."""
time_points, _ = read_data([FILE1_PATH, FILE2_PATH], flatten=True)
expected = [FILE1_TIME_POINTS] * len(FILE1_WAVEFORMS) \
+ [FILE2_TIME_POINTS] * len(FILE2_WAVEFORMS)
self.assertEqual(type(time_points), type(expected),
msg="Wrong type (given: {}, expected {}).".format(
type(time_points), type(expected))
)
self.assertEqual(len(time_points), len(expected),
msg="Wrong length (given: {}, expected {}).\n{}\n{}".format(
len(time_points), len(expected), time_points, expected)
)
for idx in range(len(time_points)):
np.testing.assert_allclose(
time_points[idx], expected[idx], rtol=1e-4)
def test_multiple_files_flatten_waveforms(self):
"""Test waveforms after flattening several file."""
_, waveforms = read_data([FILE1_PATH, FILE2_PATH], flatten=True)
expected = FILE1_WAVEFORMS + FILE2_WAVEFORMS
self.assertEqual(type(waveforms), type(expected),
msg="Wrong type (given: {}, expected {}).".format(
type(waveforms), type(expected))
)
self.assertEqual(len(waveforms), len(expected),
msg="Wrong length (given: {}, expected {}).\n{}\n{}".format(
len(waveforms), len(expected), waveforms, expected)
)
for idx in range(len(waveforms)):
np.testing.assert_allclose(
waveforms[idx], expected[idx], rtol=1e-4)
###########################################################################
# Empty file
def test_empty_file_noflatten_time_points(self):
"""Test time points if empty file, no flattening."""
time_points, _ = read_data([FILE1_PATH, FILE_NODAT_PATH], flatten=False)
expected = [FILE1_TIME_POINTS, FILE_EMPTY_TIME_POINTS]
self.assertEqual(type(time_points), type(expected),
msg="Wrong type (given: {}, expected {}).".format(
type(time_points), type(expected))
)
self.assertEqual(len(time_points), len(expected),
msg="Wrong length (given: {}, expected {}).\n{}\n{}".format(
len(time_points), len(expected), time_points, expected)
)
for idx in range(len(time_points)):
np.testing.assert_allclose(
time_points[idx], expected[idx], rtol=1e-4)
def test_empty_file_noflatten_waveforms(self):
"""Test waveforms if empty file, no flattening."""
_, waveforms = read_data([FILE1_PATH, FILE_NODAT_PATH], flatten=False)
expected = [FILE1_WAVEFORMS, FILE_EMPTY_WAVEFORMS]
self.assertEqual(type(waveforms), type(expected),
msg="Wrong type (given: {}, expected {}).".format(
type(waveforms), type(expected))
)
self.assertEqual(len(waveforms), len(expected),
msg="Wrong length (given: {}, expected {}).\n{}\n{}".format(
len(waveforms), len(expected), waveforms, expected)
)
for idx in range(len(waveforms)):
np.testing.assert_allclose(
waveforms[idx], expected[idx], rtol=1e-4)
def test_empty_file_flatten_time_points(self):
"""Test time points if empty file, after flattening."""
time_points, _ = read_data([FILE1_PATH, FILE_NODAT_PATH], flatten=True)
expected = [FILE1_TIME_POINTS] * len(FILE1_WAVEFORMS)
self.assertEqual(type(time_points), type(expected),
msg="Wrong type (given: {}, expected {}).".format(
type(time_points), type(expected))
)
self.assertEqual(len(time_points), len(expected),
msg="Wrong length (given: {}, expected {}).\n{}\n{}".format(
len(time_points), len(expected), time_points, expected)
)
for idx in range(len(time_points)):
np.testing.assert_allclose(
time_points[idx], expected[idx], rtol=1e-4)
def test_empty_file_flatten_waveforms(self):
"""Test waveforms if empty file, after flattening."""
_, waveforms = read_data([FILE1_PATH, FILE_NODAT_PATH], flatten=True)
expected = FILE1_WAVEFORMS
self.assertEqual(type(waveforms), type(expected),
msg="Wrong type (given: {}, expected {}).".format(
type(waveforms), type(expected))
)
self.assertEqual(len(waveforms), len(expected),
msg="Wrong length (given: {}, expected {}).\n{}\n{}".format(
len(waveforms), len(expected), waveforms, expected)
)
for idx in range(len(waveforms)):
np.testing.assert_allclose(
waveforms[idx], expected[idx], rtol=1e-4)
###############################################################################
......@@ -153,6 +368,7 @@ def suite():
ut.TestLoader().loadTestsFromTestCase(TestProcessWaveform),
ut.TestLoader().loadTestsFromTestCase(TestProcessSequence),
ut.TestLoader().loadTestsFromTestCase(TestProcessDataFile),
ut.TestLoader().loadTestsFromTestCase(TestReadData),
]
if __name__ == "__main__":
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment