Skip to content

Commit

Permalink
Fix reading floating point data with units (#173)
Browse files Browse the repository at this point in the history
  • Loading branch information
adamreeve committed Mar 30, 2020
1 parent 5af8c8a commit 8376132
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 10 deletions.
57 changes: 57 additions & 0 deletions nptdms/test/scenarios.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,13 @@


TDS_TYPE_INT32 = 3
TDS_TYPE_BOOL = 0x21
TDS_TYPE_COMPLEX64 = 0x08000c
TDS_TYPE_COMPLEX128 = 0x10000d
TDS_TYPE_FLOAT32 = 9
TDS_TYPE_FLOAT64 = 10
TDS_TYPE_FLOAT32_WITH_UNIT = 0x19
TDS_TYPE_FLOAT64_WITH_UNIT = 0x1A


_scenarios = []
Expand Down Expand Up @@ -559,6 +562,26 @@ def incomplete_last_row_of_interleaved_data():
return test_file, expected_data


@scenario
def bool_data():
""" Test reading a file with boolean valued data
"""
expected_channel_data = np.array([False, True, False, True], dtype=np.dtype('bool8'))

test_file = GeneratedFile()
test_file.add_segment(
("kTocMetaData", "kTocRawData", "kTocNewObjList"),
segment_objects_metadata(
channel_metadata("/'group'/'bool_channel'", TDS_TYPE_BOOL, 2),
),
"00 01 00 01"
)
expected_data = {
('group', 'bool_channel'): expected_channel_data,
}
return test_file, expected_data


@scenario
def float_data():
""" Test reading a file with float valued data
Expand Down Expand Up @@ -591,6 +614,40 @@ def float_data():
return test_file, expected_data


@scenario
def float_data_with_unit():
""" Test reading a file with float valued data with units
These are the same as normal floating point data but have a 'unit_string' property
"""
single_arr = np.array([0.123, 0.234, 0.345, 0.456], dtype=np.float32)
double_arr = np.array([0.987, 0.876, 0.765, 0.654], dtype=np.double)
data = ""
for num in single_arr[0:2]:
data += hexlify_value("<f", num)
for num in double_arr[0:2]:
data += hexlify_value("<d", num)
for num in single_arr[2:4]:
data += hexlify_value("<f", num)
for num in double_arr[2:4]:
data += hexlify_value("<d", num)

test_file = GeneratedFile()
test_file.add_segment(
("kTocMetaData", "kTocRawData", "kTocNewObjList"),
segment_objects_metadata(
channel_metadata("/'group'/'single_channel'", TDS_TYPE_FLOAT32_WITH_UNIT, 2),
channel_metadata("/'group'/'double_channel'", TDS_TYPE_FLOAT64_WITH_UNIT, 2),
),
data
)
expected_data = {
('group', 'single_channel'): single_arr,
('group', 'double_channel'): double_arr,
}
return test_file, expected_data


@scenario
def complex_data():
""" Test reading a file with complex valued data
Expand Down
20 changes: 10 additions & 10 deletions nptdms/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,20 +132,20 @@ class ExtendedFloat(TdmsType):
pass


@tds_data_type(12, None)
class DoubleFloatWithUnit(TdmsType):
size = 8
pass
@tds_data_type(0x19, np.single)
class SingleFloatWithUnit(StructType):
size = 4
struct_declaration = "f"


@tds_data_type(13, None)
class ExtendedFloatWithUnit(TdmsType):
pass
@tds_data_type(0x1A, np.double)
class DoubleFloatWithUnit(StructType):
size = 8
struct_declaration = "d"


@tds_data_type(0x19, None)
class SingleFloatWithUnit(TdmsType):
size = 4
@tds_data_type(0x1B, None)
class ExtendedFloatWithUnit(TdmsType):
pass


Expand Down

0 comments on commit 8376132

Please sign in to comment.