def test_build_labelled_matrices_no_drop_missing_error(): dtype = [ ("row_value", np.int32), ("col_value", np.int32), ("row_index", np.int32), ("col_index", np.int32), ("amount", np.float32), ("flip", np.bool), ] array = np.array( [ (0, 1, M, M, 1, True), (1, 0, M, M, 11, False), (2, 0, M, M, 11, False), ], dtype=dtype, ) row_dict = {0: 0, 1: 1} with pytest.raises(ValueError): build_labelled_matrix(array, row_dict=row_dict, drop_missing=False)
def test_build_labelled_matrices_one_d_no_drop_missing_wrong_dimensions(): dtype = [ ("row_value", np.int32), ("row_index", np.int32), ("amount", np.float32), ("flip", np.bool), ] array = np.array( [ (1, M, 99, False), (2, M, 100, False), (3, M, 101, False), ], dtype=dtype, ) row_dict = {1: 0, 2: 1} with pytest.raises(ValueError): build_labelled_matrix(array, row_dict=row_dict, one_d=True, drop_missing=False)
def test_build_labelled_matrices_zero_dimensions(): dtype = [ ("row_value", np.int32), ("col_value", np.int32), ("row_index", np.int32), ("col_index", np.int32), ("amount", np.float32), ("flip", np.bool), ] array = np.array([], dtype=dtype) _, _, _, matrix = build_labelled_matrix(array) assert matrix.shape == (0, 0)
def test_build_labelled_matrices(): dtype = [ ("row_value", np.int32), ("col_value", np.int32), ("row_index", np.int32), ("col_index", np.int32), ("amount", np.float32), ("flip", np.bool), ] array = np.array([ (0, 0, M, M, 1, True), (1, 1, M, M, 11, False), ], dtype=dtype) _, _, _, matrix = build_labelled_matrix(array) assert matrix.shape == (2, 2) assert matrix[0, 0] == -1 assert matrix[1, 1] == 11 assert matrix.sum() == 10
def test_build_labelled_matrices_one_d(): dtype = [ ("row_value", np.int32), ("row_index", np.int32), ("amount", np.float32), ("flip", np.bool), ] array = np.array([ (1, M, 99, False), (2, M, 100, False), ], dtype=dtype) row_dict = {1: 0, 2: 1} array, _, _, matrix = build_labelled_matrix( array, row_dict=row_dict, one_d=True, ) assert np.allclose(array["row_value"], [1, 2]) assert np.allclose(array["row_index"], [0, 1]) assert np.allclose(matrix.toarray(), np.array(((99, 0), (0, 100))))
def test_build_labelled_matrices_only_col_dict(): dtype = [ ("row_value", np.int32), ("col_value", np.int32), ("row_index", np.int32), ("col_index", np.int32), ("amount", np.float32), ("flip", np.bool), ] array = np.array([ (0, 11, M, M, 1, True), (1, 10, M, M, 11, False), ], dtype=dtype) col_dict = {10: 2, 11: 1} _, _, _, matrix = build_labelled_matrix(array, col_dict=col_dict) assert matrix.shape == (2, 3) assert matrix[0, 1] == -1 assert matrix[1, 2] == 11 assert matrix.sum() == 10
def test_build_labelled_matrices_offset_dict(): dtype = [ ("row_value", np.int32), ("col_value", np.int32), ("row_index", np.int32), ("col_index", np.int32), ("amount", np.float32), ("flip", np.bool), ] array = np.array([ (0, 0, M, M, 1, True), (1, 2, M, M, 11, False), ], dtype=dtype) row_dict = {0: 10, 1: 11} col_dict = {0: 20, 1: 21, 2: 22} _, _, _, matrix = build_labelled_matrix(array, row_dict, col_dict) assert matrix.shape == (12, 23) assert matrix[10, 20] == -1 assert matrix[11, 22] == 11 assert matrix.sum() == 10
def test_build_labelled_matrices_mapping_ignores_current_indices(): dtype = [ ("row_value", np.int32), ("col_value", np.int32), ("row_index", np.int32), ("col_index", np.int32), ("amount", np.float32), ("flip", np.bool), ] array = np.array( [ (0, 0, 1000, 1000, 1, True), (1, 2, 1000, 1000, 11, False), ], dtype=dtype, ) row_dict = {0: 0, 1: 1} col_dict = {0: 0, 1: 1, 2: 2} _, _, _, matrix = build_labelled_matrix(array, row_dict, col_dict) assert matrix.shape == (2, 3) assert matrix[0, 0] == -1 assert matrix[1, 2] == 11 assert matrix.sum() == 10
def test_build_labelled_matrices_drop_missing(): dtype = [ ("row_value", np.int32), ("col_value", np.int32), ("row_index", np.int32), ("col_index", np.int32), ("amount", np.float32), ("flip", np.bool), ] array = np.array( [ (0, 1, M, M, 1, True), (1, 0, M, M, 11, False), (2, 0, M, M, 11, False), ], dtype=dtype, ) row_dict = {0: 0, 1: 1} _, _, _, matrix = build_labelled_matrix(array, row_dict=row_dict) assert matrix.shape == (2, 2) assert matrix[0, 1] == -1 assert matrix[1, 0] == 11 assert matrix.sum() == 10