-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathtest_waveform.py
More file actions
108 lines (69 loc) · 3.3 KB
/
Copy pathtest_waveform.py
File metadata and controls
108 lines (69 loc) · 3.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import ismrmrd
import ctypes
import numpy as np
import numpy.random
import io
import pytest
import test_common as common
def test_initialization_from_array():
nchannels = 32
nsamples = 256
data = numpy.random.randint(0, 1 << 32, size=(nchannels, nsamples),dtype=np.uint32)
waveform = ismrmrd.Waveform.from_array(data)
assert np.array_equal(waveform.data, data), \
"Waveform data does not match data used to initialize waveform."
def test_initialization_sets_nonzero_version():
waveform = common.create_random_waveform()
assert waveform.version != 0, \
"Default acquisition version should not be zero."
def test_initialization_with_header_fields():
fields = common.create_random_waveform_properties()
waveform = ismrmrd.Waveform.from_array(common.create_random_waveform_data(), **fields)
for field in fields:
expected = fields.get(field)
actual = getattr(waveform, field)
# Use np.isclose for scalar float comparison
if isinstance(expected, float) or isinstance(actual, float):
assert np.isclose(float(expected), float(actual)), f"Field '{field}' does not match: {expected} != {actual}"
elif isinstance(expected, (tuple, list)) and hasattr(actual, '__len__') and len(expected) == len(actual):
for i, (a, b) in enumerate(zip(expected, actual)):
assert a == b, f"Field '{field}' index {i} does not match: {a} != {b}"
else:
assert expected == actual, f"Field '{field}' does not match: {expected} != {actual}"
def test_initialization_with_illegal_header_value():
with pytest.raises(TypeError):
ismrmrd.Waveform.from_array(common.create_random_waveform_data(), version='Bad version')
def test_from_array_raises_on_too_many_samples():
uint16_max = np.iinfo(np.uint16).max
data = np.zeros((1, uint16_max + 1), dtype=np.uint32)
with pytest.raises(ValueError):
ismrmrd.Waveform.from_array(data)
def test_from_array_raises_on_too_many_channels():
uint16_max = np.iinfo(np.uint16).max
data = np.zeros((uint16_max + 1, 1), dtype=np.uint32)
with pytest.raises(ValueError):
ismrmrd.Waveform.from_array(data)
def test_serialize_and_deserialize():
waveform = common.create_random_waveform()
with io.BytesIO() as stream:
waveform.serialize_into(stream.write)
# Rewind the stream, so we can read the bytes back.
stream.seek(0)
deserialized_waveform = ismrmrd.Waveform.deserialize_from(stream.read)
assert waveform == deserialized_waveform
def test_to_and_from_bytes():
waveform = common.create_random_waveform()
deserialized_waveform = ismrmrd.Waveform.from_bytes(waveform.to_bytes())
assert waveform == deserialized_waveform
def test_serialization_with_header_fields():
properties = common.create_random_waveform_properties()
data = common.create_random_waveform_data()
waveform = ismrmrd.Waveform.from_array(data, **properties)
deserialized_waveform = ismrmrd.Waveform.from_bytes(waveform.to_bytes())
assert waveform == deserialized_waveform
def test_deserialization_from_too_few_bytes():
with pytest.raises(ValueError):
ismrmrd.Acquisition.from_bytes(b'')
def test_waveformheader_size():
wav = ismrmrd.WaveformHeader()
assert ctypes.sizeof(wav) == 40