def test_data_current(): dp = load_datapackage(ZipFS(dirpath / "a-first.zip")).filter_by_attribute( "group", "x-third" ) expected = np.array([101, -111, 112]) mm = MappedMatrix(packages=[dp], matrix="matrix-b") for group in mm.groups: assert np.allclose(group.data_current, expected) dp = load_datapackage(ZipFS(dirpath / "a-first.zip")).filter_by_attribute( "group", "x-third" ) expected = np.array([101, -111]) mm = MappedMatrix( packages=[dp], matrix="matrix-b", custom_filter=lambda x: x["row"] < 3 ) for group in mm.groups: assert np.allclose(group.data_current, expected) dp = load_datapackage(ZipFS(dirpath / "a-first.zip")).filter_by_attribute( "group", "x-third" ) expected = np.array([-111, 112]) am = ArrayMapper(array=np.array([2, 3])) mm = MappedMatrix(packages=[dp], matrix="matrix-b", row_mapper=am) for group in mm.groups: assert np.allclose(group.data_current, expected) dp = load_datapackage(ZipFS(dirpath / "a-first.zip")).filter_by_attribute( "group", "x-third" ) expected = np.array([-111]) am = ArrayMapper(array=np.array([2, 3])) mm = MappedMatrix( packages=[dp], matrix="matrix-b", row_mapper=am, custom_filter=lambda x: x["row"] < 3, ) for group in mm.groups: assert np.allclose(group.data_current, expected)
def test_flip_masked(): dp = load_datapackage(ZipFS(dirpath / "a-first.zip")).filter_by_attribute( "group", "x-third" ) expected = np.array([True]) am = ArrayMapper(array=np.array([2, 3])) mm = MappedMatrix( packages=[dp], matrix="matrix-b", row_mapper=am, custom_filter=lambda x: x["row"] < 3, ) for group in mm.groups: assert np.allclose(group.flip, expected)
def test_indices_transposed(): dp = load_datapackage(ZipFS(dirpath / "a-first.zip")).filter_by_attribute( "group", "x-third" ) cexpected = np.array([-1, 0, 1]) rexpected = np.array([0, 1, -1]) am = ArrayMapper(array=np.array([2, 3])) mm = MappedMatrix( packages=[dp], matrix="matrix-b", col_mapper=am, custom_filter=lambda x: x["col"] < 3, transpose=True, ) for group in mm.groups: assert np.allclose(group.row_mapped, rexpected) assert np.allclose(group.col_mapped, cexpected)
def test_all_empty_after_mapping(): s = bwp.create_datapackage() s.add_persistent_vector( matrix="foo", data_array=np.arange(2), indices_array=np.array([(0, 0), (1, 0)], dtype=bwp.INDICES_DTYPE), ) am = ArrayMapper(array=np.array([2, 3])) mm = MappedMatrix( packages=[s], matrix="foo", row_mapper=am, ) # doesn't error out because matrix has shape from array mapper assert not mm.matrix.sum() assert mm.matrix.shape == (2, 1) assert not mm.matrix.tocoo().data.sum()
def test_indices_matrix_with_aggregation(): dp = create_datapackage(sum_intra_duplicates=True) data_array = np.array([101, 111, 112, 113]) indices_array = np.array([(1, 4), (2, 5), (3, 6), (3, 6)], dtype=INDICES_DTYPE) dp.add_persistent_vector( matrix="matrix-b", data_array=data_array, name="x-third", indices_array=indices_array, ) expected = np.array([0, 1, 2]) mm = MappedMatrix(packages=[dp], matrix="matrix-b") for group in mm.groups: assert np.allclose(group.row_matrix, expected) assert np.allclose(group.col_matrix, expected) expected = np.array([0, 1]) mm = MappedMatrix( packages=[dp], matrix="matrix-b", custom_filter=lambda x: x["row"] < 3 ) for group in mm.groups: assert np.allclose(group.row_matrix, expected) assert np.allclose(group.col_matrix, expected) rexpected = np.array([0, 1]) cexpected = np.array([1, 2]) am = ArrayMapper(array=np.array([2, 3])) mm = MappedMatrix(packages=[dp], matrix="matrix-b", row_mapper=am) for group in mm.groups: assert np.allclose(group.row_matrix, rexpected) assert np.allclose(group.col_matrix, cexpected) rexpected = np.array([0]) cexpected = np.array([1]) mm = MappedMatrix( packages=[dp], matrix="matrix-b", row_mapper=am, custom_filter=lambda x: x["row"] < 3, ) for group in mm.groups: assert np.allclose(group.row_matrix, rexpected) assert np.allclose(group.col_matrix, cexpected)
def test_one_empty_after_mapping(): s = bwp.create_datapackage() s.add_persistent_vector( matrix="foo", data_array=np.arange(2), indices_array=np.array([(0, 0), (1, 0)], dtype=bwp.INDICES_DTYPE), ) s.add_persistent_vector( matrix="foo", data_array=np.arange(10, 12), indices_array=np.array([(10, 1), (11, 1)], dtype=bwp.INDICES_DTYPE), ) am = ArrayMapper(array=np.array([10, 11])) mm = MappedMatrix( packages=[s], matrix="foo", row_mapper=am, ) assert mm.matrix.sum() == 21