def to_bit_sequence(self, event):
""" Creates an array of bits containing the details in the event
dictionary. This array is then upsampled and converted to float64 to be
sent down an analog output. Once created, the array is cached to speed
up future calls.
Parameters
----------
event: dict
A dictionary describing the current component event. It should have
3 keys: name, action, and metadata.
Returns
-------
The array of bits expressed as analog values
"""
key = (event["name"], event["action"], event["metadata"])
# Check if the bit string is already stored
if key in self.map_to_bit:
return self.map_to_bit[key]
trim = lambda ss, l: ss.ljust(l)[:l]
# Set up int8 arrays where strings are converted to integers using ord
name_array = np.array(map(ord, trim(event["name"], self.name_bytes)),
dtype=np.uint8)
action_array = np.array(map(ord, trim(event["action"],
self.action_bytes)),
dtype=np.uint8)
# Add the metadata array if a value was passed
if event["metadata"] is not None:
metadata_array = np.array(map(ord, trim(event["metadata"],
self.metadata_bytes)),
dtype=np.uint8)
else:
metadata_array = np.array([], dtype=np.uint8)
sequence = ([True] +
np.unpackbits(name_array).astype(bool).tolist() +
np.unpackbits(action_array).astype(bool).tolist() +
np.unpackbits(metadata_array).astype(bool).tolist() +
[False])
sequence = np.repeat(sequence, self.upsample_factor).astype("float64")
sequence *= self.scaling
self.map_to_bit[key] = sequence
return sequence
评论列表
文章目录