Ejemplo n.º 1
0
def test_wrong_shape(tmp_path, sparse_format, a_shape, b_shape):
    h5_path = tmp_path / "base.h5"
    a_mem = sparse.random(*a_shape, format=sparse_format)
    b_mem = sparse.random(*b_shape, format=sparse_format)

    with h5py.File(h5_path, "a") as f:
        ad._io.h5ad.write_attribute(f, "a", a_mem)
        ad._io.h5ad.write_attribute(f, "b", b_mem)
        a_disk = SparseDataset(f["a"])
        b_disk = SparseDataset(f["b"])

        with pytest.raises(AssertionError):
            a_disk.append(b_disk)
Ejemplo n.º 2
0
def test_dataset_append_memory(tmp_path, sparse_format, append_method):
    h5_path = tmp_path / "test.h5"
    a = sparse_format(sparse.random(100, 100))
    b = sparse_format(sparse.random(100, 100))

    with h5py.File(h5_path, "a") as f:
        ad._io.h5ad.write_attribute(f, "mtx", a)
        diskmtx = SparseDataset(f["mtx"])

        diskmtx.append(b)
        fromdisk = diskmtx.to_memory()

    frommem = append_method([a, b])

    assert_equal(fromdisk, frommem)
Ejemplo n.º 3
0
def read_basic_zarr(elem):
    from anndata._io import zarr

    warn(
        f"Element '{elem.name}' was written without encoding metadata.",
        OldFormatWarning,
        stacklevel=3,
    )

    if isinstance(elem, Mapping):
        # Backwards compat sparse arrays
        if "h5sparse_format" in elem.attrs:
            return SparseDataset(elem).to_memory()
        return {k: read_elem(v) for k, v in elem.items()}
    elif isinstance(elem, ZarrArray):
        return zarr.read_dataset(elem)  # TODO: Handle legacy
Ejemplo n.º 4
0
def test_dataset_append_disk(tmp_path, sparse_format, append_method):
    h5_path = tmp_path / "test.h5"
    a = sparse_format(sparse.random(10, 10))
    b = sparse_format(sparse.random(10, 10))

    with h5py.File(h5_path, "a") as f:
        ad._io.h5ad.write_attribute(f, "a", a)
        ad._io.h5ad.write_attribute(f, "b", b)
        a_disk = SparseDataset(f["a"])
        b_disk = SparseDataset(f["b"])

        a_disk.append(b_disk)
        fromdisk = a_disk.to_memory()

    frommem = append_method([a, b])

    assert_equal(fromdisk, frommem)
Ejemplo n.º 5
0
def test_wrong_formats(tmp_path):
    h5_path = tmp_path / "base.h5"
    base = sparse.random(100, 100, format="csr")

    with h5py.File(h5_path, "a") as f:
        ad._io.h5ad.write_attribute(f, "base", base)
        disk_mtx = SparseDataset(f["base"])
        pre_checks = disk_mtx.to_memory()

        with pytest.raises(ValueError):
            disk_mtx.append(sparse.random(100, 100, format="csc"))
        with pytest.raises(ValueError):
            disk_mtx.append(sparse.random(100, 100, format="coo"))
        with pytest.raises(NotImplementedError):
            disk_mtx.append(np.random.random((100, 100)))
        disk_dense = f.create_dataset("dense",
                                      data=np.random.random((100, 100)))
        with pytest.raises(NotImplementedError):
            disk_mtx.append(disk_dense)

        post_checks = disk_mtx.to_memory()

    # Check nothing changed
    assert not np.any((pre_checks != post_checks).toarray())
Ejemplo n.º 6
0
def read_sparse_partial(elem,
                        *,
                        items=None,
                        indices=(slice(None), slice(None))):
    return SparseDataset(elem)[indices]
Ejemplo n.º 7
0
def read_sparse(elem):
    return SparseDataset(elem).to_memory()