Skip to content

Commit

Permalink
back to functioning
Browse files Browse the repository at this point in the history
  • Loading branch information
t-sasatani committed Jun 22, 2024
1 parent 59a60c7 commit c3d6bc9
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 30 deletions.
33 changes: 16 additions & 17 deletions miniscope_io/bit_operation.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,10 @@ def _reverse_byte_order_32bit_int(cls, x: int) -> int:
@classmethod
def bytebuffer_to_ndarrays(cls,
buffer,
header_length_bits,
reverse_header_bits=True,
header_length_words,
preamble_length_words,
reverse_header_bits=True,
reverse_header_bytes=True,
reverse_body_bits=True,
reverse_body_bytes=True
) -> Tuple[np.ndarray, np.ndarray]:
Expand All @@ -89,27 +91,25 @@ def bytebuffer_to_ndarrays(cls,
padding_length = (4 - (len(buffer) % 4)) % 4
padded_buffer = buffer + b'\x00' * padding_length

# Check if header_length_bits is valid
if header_length_bits % 32 != 0:
raise ValueError("header_length_bits must be a multiple of 32")

# Convert header length from bits to bytes and then to 32-bit words
header_length_bytes = header_length_bits // 8
header_words = header_length_bytes // 4

# Convert the padded buffer to a uint32 numpy array
data = np.frombuffer(padded_buffer, dtype=np.uint32)

# Process header
header_data = data[:header_words]
header = data[preamble_length_words:header_length_words]
if reverse_header_bits:
header_data = np.array(
[cls._reverse_bits_in_32bit_int(x) for x in header_data],
header = np.array(
[cls._reverse_bits_in_32bit_int(x) for x in header],
dtype=np.uint32
)
if reverse_header_bytes:
header = np.array(
[cls._reverse_byte_order_32bit_int(x) for x in header],
dtype=np.uint32
)


# Process body
payload_data = data[header_words:]
payload_data = data[header_length_words:]
if reverse_body_bits:
payload_data = np.array(
[cls._reverse_bits_in_32bit_int(x) for x in payload_data],
Expand All @@ -123,6 +123,5 @@ def bytebuffer_to_ndarrays(cls,
)

# Convert processed body buffer to uint8 numpy array
payload_uint8 = payload_data.astype(np.uint8)

return header_data, payload_uint8
payload_uint8 = payload_data.view(np.uint8)
return header, payload_uint8
23 changes: 10 additions & 13 deletions miniscope_io/stream_daq.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,23 +146,20 @@ def _parse_header(
Tuple[BufferHeader, bytes]
The returned header data and (optionally truncated) buffer data.
"""
pre = Bits(self.preamble)
pre_len = len(pre)
locallogs = init_logger("streamDaq._parse_header")

locallogs.debug(f'self.preamble: {self.preamble}')
locallogs.debug(f'buffer[:pre_len]: {buffer[:pre_len]}')

assert buffer[:pre_len] == pre

header, payload = BufferFormatter.bytebuffer_to_ndarrays(buffer=buffer,
header_length_bits=self.config.header_len,
header_length_words=int(self.config.header_len/32),
preamble_length_words=int(len(Bits(self.config.preamble))/32),
reverse_header_bits=True,
reverse_header_bytes=True,
reverse_body_bits=True,
reverse_body_bytes=True)

#locallogs.debug(f'len(payload): {len(payload)}')
header_data = dict()
for hd, header_index in self.header_fmt.model_dump().items():
header_data[hd] = header[header_index]
#locallogs.debug(f'{header_data}')

header_data = BufferHeader.model_construct(**header_data)

Expand Down Expand Up @@ -355,7 +352,7 @@ def _buffer_to_frame(

header_data, serial_buffer = self._parse_header(serial_buffer)
# log metadata
locallogs.debug(header_data)
# locallogs.debug(header_data)

serial_buffer = self._trim(
serial_buffer, self.buffer_npix[header_data.frame_buffer_count], locallogs
Expand Down Expand Up @@ -433,9 +430,9 @@ def _format_frame(
continue
frame_data = np.concat(frame_data)

# Shouldn't be bad but kind of weird
#if self.config.LSB:
# frame_data = np.flip(frame_data)
# I'm not sure if we need this but for consistency with the current test function
if self.config.LSB:
frame_data = np.flip(frame_data)

try:
frame = np.reshape(frame_data, (self.config.frame_width, self.config.frame_height))
Expand Down

0 comments on commit c3d6bc9

Please sign in to comment.