Skip to content

Commit

Permalink
Fix: read correct positions in header/data
Browse files Browse the repository at this point in the history
  • Loading branch information
pietsjoh committed Aug 3, 2023
1 parent f7db0c1 commit 21a2f05
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 22 deletions.
30 changes: 14 additions & 16 deletions rsciio/hamamatsu/_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,14 +167,16 @@ def parse_file(self):
header["offset_y"] = int(self.__read_numeric("int16"))
file_type = FileType(int(self.__read_numeric("int16"))).name
header["file_type"] = file_type
header["num_images_in_channel"] = int.from_bytes(
self._file_obj.read(3), "little"
)
header["num_images_in_channel"] = int(self.__read_numeric("int32"))
header["num_additional_channels"] = int(self.__read_numeric("int16"))
header["channel_number"] = int(self.__read_numeric("int16"))
header["timestamp"] = self.__read_numeric("double")
header["marker"] = self.__read_utf8(3)
header["additional_info"] = self.__read_utf8(29)
header["marker"] = self.__read_utf8(4)
## according to the manual, additional_info is one byte shorter
## however, there is also an unexplained 1 byte gap between marker and additional info
## so this extra byte is absorbed in additional_info
## in the testfiles both marker and additional_info contain only zeros
header["additional_info"] = self.__read_utf8(30)
comment = self.__read_utf8(com_len)
if file_type == "bit8":
dtype = "uint8"
Expand All @@ -183,13 +185,8 @@ def parse_file(self):
elif file_type == "bit32":
dtype = "uint32"
else:
raise NotImplementedError(f"reading type: {file_type} not implemented")
# TODO: check if all previous read positions are correct
self._file_obj.seek(64 + com_len)
raise RuntimeError(f"reading type: {file_type} not implemented")
data = self.__read_numeric(dtype, size=w_px * self._h_lines)
_logger.debug(f"file read until: {self._file_obj.tell()}")
_logger.debug(f"total file_size: {self._filesize}")
## missing bytes, because calibration data at the end
self.original_metadata.update(header)
return data, comment

Expand All @@ -207,8 +204,9 @@ def _extract_calibration_data(self, cal):
self._file_obj.seek(pos)
return self.__read_numeric("float", size=size)
else:
_logger.warning(f"Cannot read axis data (invalid start for address {cal})")
return None
raise RuntimeError(
f"Cannot read axis data (invalid start for address {cal})"
)

def _set_axis(self, name, scale_type, unit, cal_addr):
axis = {"units": unit, "name": name, "navigate": False}
Expand Down Expand Up @@ -245,8 +243,9 @@ def _set_axis(self, name, scale_type, unit, cal_addr):
else:
axis["axis"] = data
else:
# TODO: better error msg
raise ValueError
raise ValueError(
f"Cannot extract {name}-axis information (invalid scale-type)."
)
return axis

def _get_axes(self):
Expand All @@ -258,7 +257,6 @@ def _get_axes(self):
x_type, y_type = map(int, self._get_scaling_entry(scaling_md, "Type"))
x_axis = self._set_axis("Wavelength", x_type, x_unit, x_cal_address)
y_axis = self._set_axis("Time", y_type, y_unit, y_cal_address)
_logger.debug(f"file read until: {self._file_obj.tell()}")
y_axis["index_in_array"] = 0
x_axis["index_in_array"] = 1
axes_list = sorted([x_axis, y_axis], key=lambda item: item["index_in_array"])
Expand Down
16 changes: 10 additions & 6 deletions rsciio/tests/test_hamamatsu.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ def teardown_class(cls):

def test_data_deflection(self):
expected_data_start = [
[0, 2, 3405774848],
[4127195136, 2701131777, 3674210304],
[1778384899, 2197815296, 2],
[0, 0, 715],
[246, 161, 475],
[106, 899, 0],
]
np.testing.assert_allclose(expected_data_start, self.s.isig[:3, :3].data)

Expand Down Expand Up @@ -268,7 +268,7 @@ def test_original_metadata_comment(self):
"ScalingYUnit": "us",
"ScalingYScalingFile": "#1382319,0512",
},
"Comment": "UserComment",
"Comment": {"UserComment": ""},
}

assert expected_metadata == original_metadata
Expand Down Expand Up @@ -322,7 +322,7 @@ def teardown_class(cls):
gc.collect()

def test_data_focus(self):
expected_data_end = [[0, 0, 603979776], [0, 0, 0], [352321536, 0, 0]]
expected_data_end = [[0, 0, 36], [0, 0, 0], [21, 0, 0]]
np.testing.assert_allclose(expected_data_end, self.s_focus.isig[-3:, -3:].data)

def test_axes_focus(self):
Expand Down Expand Up @@ -356,7 +356,7 @@ def teardown_class(cls):
gc.collect()

def test_data(self):
expected_data = [0, 34, 8765]
expected_data = [0, 0, 0]
np.testing.assert_allclose(self.s.isig[-3:, 0].data, expected_data)

def test_metadata(self):
Expand Down Expand Up @@ -386,3 +386,7 @@ def test_metadata(self):
np.testing.assert_allclose(
self.s.metadata.Acquisition_instrument.Detector.time_range, 4
)

def test_data(self):
expected_data = [9385, 8354, 7658]
np.testing.assert_allclose(self.s.isig[:3, 0].data, expected_data)

0 comments on commit 21a2f05

Please sign in to comment.