def data_out(self) -> bytes: """ Formats the data bytes string for the counters and returns the result Returns: formatted data bytes string """ if self.stop_connections or self.exit_measurement or not self.enable: return b"" try: data_shape = np.array(self.data).shape flat_data = np.reshape(self.data, np.prod(data_shape)) # nD data --> 1D data shape_str = ",".join([str(x) for x in data_shape]) data_bytes = struct.pack(f"!{len(flat_data)}L", *flat_data) self.data_string = (TCP.format_data('counter/dimensions', shape_str)) self.data_string += (TCP.format_data('counter/data', data_bytes)) except Exception as e: self.logger.exception(f"Error formatting data for counters.\n{e}", exc_info=True) return self.data_string
def data_out(self) -> str: """ Convert the received data into a string parsable by CsPy Returns: the instance's data string, formatted for reception by CsPy """ if not (self.stop_connections or self.exit_measurement) and self.enable: try: # flatten the data and convert to a str data_shape = np.array(self.data).shape flat_data = np.reshape(self.data, np.prod(data_shape)) # nD data --> 1D data shape_str = ",".join([str(x) for x in data_shape]) data_bytes = struct.pack(f'!{len(flat_data)}d', *flat_data) self.data_string = (TCP.format_data('AI/dimensions', shape_str) + TCP.format_data('AI/data', data_bytes)) except Exception as e: self.logger.exception(f"Error formatting data from {self.__class__.__name__}") raise e return self.data_string
def data_out(self) -> bytes: """ Returns a formatted string of relevant hamamatsu data to be written to hdf5 fike Returns: formatted data string """ if self.stop_connections or self.reset_connection or not self.enable: return b"" hm = "Hamamatsu" hm_str = b"" sz = self.last_measurement.shape hm_str += TCP.format_data(f"{hm}/numShots", f"{sz[0]}") hm_str += TCP.format_data(f"{hm}/rows", f"{sz[1]}") hm_str += TCP.format_data(f"{hm}/columns", f"{sz[2]}") for shot in range(sz[0]): if self.measurement_success: flat_ar = np.reshape(self.last_measurement[shot, :, :], sz[1] * sz[2]) else: # A failed measurement returns useless data of all 0 flat_ar = np.zeroes(sz[1] * sz[2]) tmp_str = u16_ar_to_bytes(flat_ar) hm_str += TCP.format_data(f"{hm}/shots/{shot}", tmp_str) hm_str += TCP.format_data(f"{hm}/temperature", "{:.3f}".format(self.camera_temp)) return hm_str
def test_format_data(): test_name = "Test" test_data = "one, two, three, four" expected_message = "b'\\x00\\x00\\x00\\x04'Testb'\\x00\\x00\\x00\\x15'one, two, three, four" assert TCP.format_data(name=test_name, data=test_data) == expected_message