def test_encode_char(self): encoder = OneHotMatrixEncoder(1) sequences = np.array([[1], [4], [0]]) encoded_vectors = encoder.encode_sequences(sequences) expected_vectors = np.array([[[0, 1, 0, 0, 0]], [[0, 0, 0, 0, 1]], [[1, 0, 0, 0, 0]]]) np.testing.assert_array_equal(encoded_vectors, expected_vectors)
def test_encode_sequences(self): encoder = OneHotMatrixEncoder(4) sequences = np.array([[2, 1, 4, 3], [1, 4, 3, 2], [1, 4, 0, 2]]) encoded_vectors = encoder.encode_sequences(sequences) expected_vectors = np.array([[[0, 0, 1, 0, 0], [0, 1, 0, 0, 0], [0, 0, 0, 0, 1], [0, 0, 0, 1, 0]], [[0, 1, 0, 0, 0], [0, 0, 0, 0, 1], [0, 0, 0, 1, 0], [0, 0, 1, 0, 0]], [[0, 1, 0, 0, 0], [0, 0, 0, 0, 1], [1, 0, 0, 0, 0], [0, 0, 1, 0, 0]]]) np.testing.assert_array_equal(encoded_vectors, expected_vectors)
def test_encode_bad_prediction_length(self): try: encoder = OneHotMatrixEncoder(1, -1) except NegativePredictionLengthException as e: pass except Exception as e: self.fail()
def test_encode_bad_length(self): try: encoder = OneHotMatrixEncoder(0) except NonpositiveLengthException as e: pass except Exception as e: self.fail()
class OneHotVectorEncoder(_OneHotVectorUtil): def __init__(self, sequence_length, encoding_constants=CONSTANTS): super().__init__(sequence_length, encoding_constants=encoding_constants) self.encoder = OneHotMatrixEncoder( sequence_length, encoding_constants=self.encoding_constants) def encode_sequences(self, integer_encoding): if len(integer_encoding) == 0: return np.array([]) cube = self.encoder.encode_sequences(integer_encoding) encoding_length = len(self.encoding_constants.ONE_HOT_ENCODING) dimensions = (len(cube), encoding_length * self.sequence_length) encoded_matrix = cube.reshape(dimensions) return encoded_matrix
def __init__(self, sequence_length, encoding_constants=CONSTANTS): super().__init__(sequence_length, encoding_constants=encoding_constants) self.encoder = OneHotMatrixEncoder( sequence_length, encoding_constants=self.encoding_constants)
def test_encode_empty_array(self): encoder = OneHotMatrixEncoder(4) sequences = np.array([]) encoded_vectors = encoder.encode_sequences(sequences) expected_vectors = np.array([]) np.testing.assert_array_equal(encoded_vectors, expected_vectors)