Skip to content

Commit

Permalink
Raise error when writing channel data that is not 1d (#207)
Browse files Browse the repository at this point in the history
  • Loading branch information
adamreeve committed Jul 23, 2020
1 parent e42a94d commit 5e13ba9
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
16 changes: 15 additions & 1 deletion nptdms/test/writer/test_tdms_segment.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
OrderedDict = dict
import numpy as np

from nptdms.writer import TdmsSegment, read_properties_dict
from nptdms.writer import ChannelObject, TdmsSegment, read_properties_dict
from nptdms.types import *


Expand Down Expand Up @@ -198,6 +198,20 @@ def test_error_raised_when_cannot_convert_property_value():
read_properties_dict(properties)


@pytest.mark.parametrize(
"data",
[
np.array(0.0, dtype=np.dtype('float32')),
np.zeros((2, 2), dtype=np.dtype('float32')),
np.zeros((2, 2, 2), dtype=np.dtype('float32')),
]
)
def test_channel_with_invalid_data(data):
with pytest.raises(ValueError) as exc_info:
_ = ChannelObject("Group", "Channel", data, {})
assert str(exc_info.value) == "Channel data must be a 1d array"


def _assert_sequence_equal(values, expected_values):
position = 1
expected_values = iter(expected_values)
Expand Down
2 changes: 2 additions & 0 deletions nptdms/writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,8 @@ def __init__(self, group, channel, data, properties=None):
self.group = group
self.channel = channel
self.data = _to_np_array(data)
if self.data.ndim != 1:
raise ValueError("Channel data must be a 1d array")
self.properties = properties

@property
Expand Down

0 comments on commit 5e13ba9

Please sign in to comment.