def parsedata(self, package):
"""
This function parses the Data Package sent by MuLES to obtain all the data
available in MuLES as matrix of the size [n_samples, n_columns], therefore the
total of elements in the matrix is n_samples * n_columns. Each column represents
one channel
Argument:
package: Data package sent by MuLES.
"""
size_element = 4 # Size of each one of the elements is 4 bytes
n_columns = len(self.params['data format'])
n_bytes = len(package)
n_samples = (n_bytes/size_element) / n_columns
####mesData = np.uint8(mesData) # Convert from binary to integers (not necessary pyton)
bytes_per_element = np.flipud(np.reshape(list(bytearray(package)), [size_element,-1],order='F'))
# Changes "package" to a list with size (n_bytes,1)
# Reshapes the list into a matrix bytes_per_element which has the size: (4,n_bytes/4)
# Flips Up-Down the matrix of size (4,n_bytes/4) to correct the swap in bytes
package_correct_order = np.uint8(np.reshape(bytes_per_element,[n_bytes,-1],order='F' ))
# Unrolls the matrix bytes_per_element, in "package_correct_order"
# that has a size (n_bytes,1)
data_format_tags = self.params['data format']*n_samples
# Tags used to map the elements into their corresponding representation
package_correct_order_char = "".join(map(chr,package_correct_order))
elements = struct.unpack(data_format_tags,package_correct_order_char)
# Elements are cast in their corresponding representation
data = np.reshape(np.array(elements),[n_samples,n_columns],order='C')
# Elements are reshap into data [n_samples, n_columns]
return data
评论列表
文章目录