[docs]classBitsSource(ABC,RandomNode):"""Base Class for Arbitrary Streams of Communication Bits. Inheriting classes are required to implement the :meth:`.generate_bits` routine. """def__init__(self,seed:Optional[int]=None)->None:""" Args: seed (int, optional): Seed used to initialize the pseudo-random number generator. """RandomNode.__init__(self,seed=seed)
[docs]@abstractmethoddefgenerate_bits(self,num_bits:int)->np.ndarray:"""Generate a new sequence of bits. Args: num_bits (int): Number of bits to be generated. Returns: np.ndarray: A numpy vector of `num_bits` generated bits. """...# pragma: no cover
[docs]classRandomBitsSource(BitsSource,Serializable):"""Bit stream generator for pseudo-random sequences of bits."""yaml_tag="RandomBits"def__init__(self,seed:Optional[int]=None)->None:""" Args: seed (int, optional): Seed used to initialize the pseudo-random number generator. """# Initialize base classesBitsSource.__init__(self,seed=seed)
[docs]classStreamBitsSource(BitsSource,Serializable):"""Bit-stream generator mapping representing file system streams as bit sources."""__stream:BufferedReaderdef__init__(self,path:str)->None:""" Args: path (str): Path to the stream bits source. """BitsSource.__init__(self)self.__stream=open(path,mode="rb")def__del__(self)->None:self.__stream.close()
[docs]defgenerate_bits(self,num_bits:int)->np.ndarray:num_bytes=int(ceil(num_bits/8))bit_overflow=num_bytes*8-num_bitsifbit_overflow>0:raiseRuntimeError("Bit caching not yet supported")byte_string=self.__stream.read(num_bytes)array=np.unpackbits(np.frombuffer(byte_string,dtype=np.uint8))returnarray