Skip to content

Commit

Permalink
rename classes
Browse files Browse the repository at this point in the history
  • Loading branch information
magland committed Mar 19, 2024
1 parent 4db0704 commit 0e93bec
Show file tree
Hide file tree
Showing 25 changed files with 87 additions and 79 deletions.
4 changes: 4 additions & 0 deletions .vscode/tasks/quick_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
set -ex

# black --check .

cd lindi
flake8 .
# pyright
cd ..

pytest --cov=lindi --cov-report=xml --cov-report=term -m "not slow" tests/
4 changes: 4 additions & 0 deletions .vscode/tasks/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
set -ex

# black --check .

cd lindi
flake8 .
pyright
cd ..

pytest --cov=lindi --cov-report=xml --cov-report=term tests/
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ h5_url = "https://api.dandiarchive.org/api/assets/3d12a902-139a-4c1a-8fd0-0a7faf


# Create the read-only Zarr store for the h5 file
store = lindi.LindiH5Store.from_file(h5_url)
store = lindi.LindiH5ZarrStore.from_file(h5_url)

# Create the reference file system object
rfs = store.to_reference_file_system()
Expand All @@ -47,7 +47,7 @@ with open("example.zarr.json", "w") as f:
json.dump(rfs, f, indent=2)

# Create the h5py-like client from the reference file system
client = lindi.LindiClient.from_reference_file_system(rfs)
client = lindi.LindiH5pyFile.from_reference_file_system(rfs)

# Try to read using pynwb
# (This part does not work yet)
Expand Down
4 changes: 2 additions & 2 deletions devel/old_tests/old_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import h5py
import zarr
import kerchunk.hdf # type: ignore
from lindi import LindiH5Store
from lindi import LindiH5ZarrStore
from fsspec.implementations.reference import ReferenceFileSystem


Expand Down Expand Up @@ -88,7 +88,7 @@ def test_numpy_array_of_strings():


def _get_lindi_zarr(filename):
store = LindiH5Store.from_file(filename, url='.') # use url='.' so that a reference file system can be created
store = LindiH5ZarrStore.from_file(filename, url='.') # use url='.' so that a reference file system can be created
root = zarr.open(store)
return root, store

Expand Down
4 changes: 2 additions & 2 deletions devel/old_tests/test_lindi_client.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from lindi import LindiClient, LindiGroup, LindiDataset
from lindi import LindiH5pyFile, LindiGroup, LindiDataset


def test_lindi_client():
client = LindiClient.from_file("example_0.zarr.json")
client = LindiH5pyFile.from_file("example_0.zarr.json")

for k, v in client.attrs.items():
print(f"{k}: {v}")
Expand Down
4 changes: 2 additions & 2 deletions devel/old_tests/test_with_real_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import zarr
import h5py
import remfile
from lindi import LindiH5Store
from lindi import LindiH5ZarrStore

examples = []

Expand Down Expand Up @@ -40,7 +40,7 @@ def do_compare(example_num: int):
h5_url = example["h5_url"]
print(f"Running comparison for {h5_url}")
h5f = h5py.File(remfile.File(h5_url), "r")
with LindiH5Store.from_file(h5_url) as store:
with LindiH5ZarrStore.from_file(h5_url) as store:
root = zarr.open(store)
assert isinstance(root, zarr.Group)

Expand Down
18 changes: 9 additions & 9 deletions devel/old_tests/tests.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import tempfile
import numpy as np
import h5py
from lindi import LindiH5Store, LindiClient, LindiGroup, LindiDataset
from lindi import LindiH5ZarrStore, LindiH5pyFile, LindiGroup, LindiDataset


def test_scalar_dataset():
Expand All @@ -11,11 +11,11 @@ def test_scalar_dataset():
filename = f"{tmpdir}/test.h5"
with h5py.File(filename, "w") as f:
f.create_dataset("X", data=val)
with LindiH5Store.from_file(
with LindiH5ZarrStore.from_file(
filename, url=filename
) as store: # set url so that a reference file system can be created
rfs = store.to_reference_file_system()
client = LindiClient.from_reference_file_system(rfs)
client = LindiH5pyFile.from_reference_file_system(rfs)
h5f = h5py.File(filename, "r")
X1 = h5f["X"]
assert isinstance(X1, h5py.Dataset)
Expand Down Expand Up @@ -69,11 +69,11 @@ def test_numpy_array():
filename = f"{tmpdir}/test.h5"
with h5py.File(filename, "w") as f:
f.create_dataset("X", data=array, chunks=chunks)
with LindiH5Store.from_file(
with LindiH5ZarrStore.from_file(
filename, url=filename
) as store: # set url so that a reference file system can be created
rfs = store.to_reference_file_system()
client = LindiClient.from_reference_file_system(rfs)
client = LindiH5pyFile.from_reference_file_system(rfs)
h5f = h5py.File(filename, "r")
X1 = h5f["X"]
assert isinstance(X1, h5py.Dataset)
Expand All @@ -92,9 +92,9 @@ def test_numpy_array_of_strings():
with h5py.File(filename, "w") as f:
f.create_dataset("X", data=["abc", "def", "ghi"])
h5f = h5py.File(filename, "r")
with LindiH5Store.from_file(filename, url=filename) as store:
with LindiH5ZarrStore.from_file(filename, url=filename) as store:
rfs = store.to_reference_file_system()
client = LindiClient.from_reference_file_system(rfs)
client = LindiH5pyFile.from_reference_file_system(rfs)
X1 = h5f["X"]
assert isinstance(X1, h5py.Dataset)
X2 = client["X"]
Expand All @@ -120,9 +120,9 @@ def test_attributes():
f["group"].attrs["foo"] = "bar2"
f["group"].attrs["baz"] = 3.15
h5f = h5py.File(filename, "r")
with LindiH5Store.from_file(filename, url=filename) as store:
with LindiH5ZarrStore.from_file(filename, url=filename) as store:
rfs = store.to_reference_file_system()
client = LindiClient.from_reference_file_system(rfs)
client = LindiH5pyFile.from_reference_file_system(rfs)
X1 = h5f["X"]
assert isinstance(X1, h5py.Dataset)
X2 = client["X"]
Expand Down
18 changes: 9 additions & 9 deletions examples/example1.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@
import h5py
import tempfile
import lindi
from lindi import LindiH5Store, LindiClient, LindiDataset
from lindi import LindiH5ZarrStore, LindiH5pyFile, LindiDataset
from _check_equal import _check_equal


def example1():
# In this example, we create a temporary hdf5 file, with some sample
# datasets, groups, and attributes. We then load that file using
# LindiH5Store which is a zarr storage backend providing read-only view of
# LindiH5ZarrStore which is a zarr storage backend providing read-only view of
# that hdf5 file. We then create a reference file system and use that to
# create a LindiClient, which mimics the h5py API. We then compare the
# create a LindiH5pyFile, which mimics the h5py API. We then compare the
# datasets, groups, and attributes of the original hdf5 file with those of
# the LindiClient.
# the LindiH5pyFile.

with tempfile.TemporaryDirectory() as tmpdir:
print("Creating an example hdf5 file")
Expand All @@ -31,16 +31,16 @@ def example1():
f["group"].attrs["baz"] = 3.15
h5f = h5py.File(filename, "r")

print("Creating a LindiH5Store from the hdf5 file")
print("Creating a LindiH5ZarrStore from the hdf5 file")
# We set url to filename so that the references can point to a local file
# but normally, this would be a remote URL
with LindiH5Store.from_file(filename, url=filename) as store:
print("Creating a reference file system from the LindiH5Store")
with LindiH5ZarrStore.from_file(filename, url=filename) as store:
print("Creating a reference file system from the LindiH5ZarrStore")
rfs_fname = f"{tmpdir}/example.zarr.json"
store.to_file(rfs_fname)

print("Creating a LindiClient from the reference file system")
client = LindiClient.from_file(rfs_fname)
print("Creating a LindiH5pyFile from the reference file system")
client = LindiH5pyFile.from_file(rfs_fname)

print("Comparing dataset: X")
X1 = h5f["X"]
Expand Down
4 changes: 2 additions & 2 deletions examples/try_pynwb.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

def try_pynwb():
# Create the read-only store for the h5 file
store = lindi.LindiH5Store.from_file(h5_url)
store = lindi.LindiH5ZarrStore.from_file(h5_url)

# Create the reference file system object
rfs = store.to_reference_file_system()
Expand All @@ -19,7 +19,7 @@ def try_pynwb():
json.dump(rfs, f, indent=2)

# Create the client from the reference file system
client = lindi.LindiClient.from_reference_file_system(rfs)
client = lindi.LindiH5pyFile.from_reference_file_system(rfs)

# Try to read using pynwb
with pynwb.NWBHDF5IO(file=client, mode="r") as io:
Expand Down
1 change: 0 additions & 1 deletion lindi/LindiH5Store/__init__.py

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -18,30 +18,30 @@


@dataclass
class LindiH5StoreOpts:
class LindiH5ZarrStoreOpts:
num_dataset_chunks_threshold: Union[int, None] = 1000


class LindiH5Store(Store):
class LindiH5ZarrStore(Store):
"""A zarr store that provides a read-only view of an HDF5 file.
Do not call the constructor directly. Instead do one of the following:
store = LindiH5Store.from_file(hdf5_file_name)
store = LindiH5ZarrStore.from_file(hdf5_file_name)
# do stuff with store
store.close()
or
with LindiH5Store.from_file(hdf5_file_name) as store:
with LindiH5ZarrStore.from_file(hdf5_file_name) as store:
# do stuff with store
"""

def __init__(
self,
*,
_file: Union[IO, Any],
_opts: LindiH5StoreOpts,
_opts: LindiH5ZarrStoreOpts,
_url: Union[str, None] = None,
):
"""
Expand Down Expand Up @@ -76,17 +76,17 @@ def close(self):
def from_file(
hdf5_file_name_or_url: str,
*,
opts: LindiH5StoreOpts = LindiH5StoreOpts(),
opts: LindiH5ZarrStoreOpts = LindiH5ZarrStoreOpts(),
url: Union[str, None] = None,
):
"""
Create a LindiH5Store from a file or url.
Create a LindiH5ZarrStore from a file or url.
Parameters
----------
hdf5_file_name_or_url : str
The name of the HDF5 file or a URL to the HDF5 file.
opts : LindiH5StoreOpts
opts : LindiH5ZarrStoreOpts
Options for the store.
url : str or None
If hdf5_file_name_or_url is a local file name, then this can
Expand All @@ -100,10 +100,10 @@ def from_file(
"http://"
) or hdf5_file_name_or_url.startswith("https://"):
remf = remfile.File(hdf5_file_name_or_url, verbose=False)
return LindiH5Store(_file=remf, _url=hdf5_file_name_or_url, _opts=opts)
return LindiH5ZarrStore(_file=remf, _url=hdf5_file_name_or_url, _opts=opts)
else:
f = open(hdf5_file_name_or_url, "rb")
return LindiH5Store(_file=f, _url=url, _opts=opts)
return LindiH5ZarrStore(_file=f, _url=url, _opts=opts)

def __getitem__(self, key):
"""Get an item from the store (required by base class)."""
Expand Down Expand Up @@ -397,7 +397,7 @@ def listdir(self, path: str = "") -> List[str]:
def to_file(self, file_name: str, *, file_type: Literal["zarr.json"] = "zarr.json"):
"""Write a reference file system cooresponding to this store to a file.
This can then be loaded using LindiClient.from_file(fname)
This can then be loaded using LindiH5pyFile.from_file(fname)
"""
if file_type != "zarr.json":
raise Exception(f"Unsupported file type: {file_type}")
Expand All @@ -410,7 +410,7 @@ def to_reference_file_system(self) -> dict:
"""Create a reference file system cooresponding to this store.
This can then be loaded using
LindiClient.from_reference_file_system(obj)
LindiH5pyFile.from_reference_file_system(obj)
"""
if self._h5f is None:
raise Exception("Store is closed")
Expand Down
1 change: 1 addition & 0 deletions lindi/LindiH5ZarrStore/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .LindiH5ZarrStore import LindiH5ZarrStore, LindiH5ZarrStoreOpts # noqa: F401
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def _h5_ref_to_zarr_attr(ref: h5py.Reference, *, label: str = '', h5f: h5py.File
object_id = dref_obj.attrs.get("object_id", None)

# Here we assume that the file has a top-level attribute called "object_id".
# This will be the case for files created by the LindiH5Store class.
# This will be the case for files created by the LindiH5ZarrStore class.
file_object_id = h5f.attrs.get("object_id", None)

# See https://hdmf-zarr.readthedocs.io/en/latest/storage.html#storing-object-references-in-attributes
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class ZarrInfoForH5Dataset:
def _zarr_info_for_h5_dataset(h5_dataset: h5py.Dataset) -> ZarrInfoForH5Dataset:
"""Get the information needed to create a zarr dataset from an h5py dataset.
This is the main workhorse function for LindiH5Store. It takes an h5py
This is the main workhorse function for LindiH5ZarrStore. It takes an h5py
dataset and returns a ZarrInfoForH5Dataset object.
It handles the following cases:
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from .LindiReference import LindiReference


class LindiClient(LindiGroup):
class LindiH5pyFile(LindiGroup):
def __init__(
self,
*,
Expand All @@ -25,38 +25,38 @@ def filename(self):
return ''

@staticmethod
def from_zarr_store(zarr_store: Union[Store, FSMap]) -> "LindiClient":
def from_zarr_store(zarr_store: Union[Store, FSMap]) -> "LindiH5pyFile":
zarr_group = zarr.open(store=zarr_store, mode="r")
assert isinstance(zarr_group, zarr.Group)
return LindiClient.from_zarr_group(zarr_group)
return LindiH5pyFile.from_zarr_group(zarr_group)

@staticmethod
def from_file(
json_file: str, file_type: Literal["zarr.json"] = "zarr.json"
) -> "LindiClient":
) -> "LindiH5pyFile":
if file_type == "zarr.json":
if json_file.startswith("http") or json_file.startswith("https"):
with tempfile.TemporaryDirectory() as tmpdir:
filename = f"{tmpdir}/temp.zarr.json"
_download_file(json_file, filename)
with open(filename, "r") as f:
data = json.load(f)
return LindiClient.from_reference_file_system(data)
return LindiH5pyFile.from_reference_file_system(data)
else:
with open(json_file, "r") as f:
data = json.load(f)
return LindiClient.from_reference_file_system(data)
return LindiH5pyFile.from_reference_file_system(data)
else:
raise ValueError(f"Unknown file_type: {file_type}")

@staticmethod
def from_zarr_group(zarr_group: zarr.Group) -> "LindiClient":
return LindiClient(_zarr_group=zarr_group)
def from_zarr_group(zarr_group: zarr.Group) -> "LindiH5pyFile":
return LindiH5pyFile(_zarr_group=zarr_group)

@staticmethod
def from_reference_file_system(data: dict) -> "LindiClient":
def from_reference_file_system(data: dict) -> "LindiH5pyFile":
fs = ReferenceFileSystem(data).get_mapper(root="")
return LindiClient.from_zarr_store(fs)
return LindiH5pyFile.from_zarr_store(fs)

def get(self, key, default=None, getlink: bool = False):
try:
Expand Down Expand Up @@ -95,7 +95,7 @@ def __getitem__(self, key): # type: ignore
raise Exception(f'Mismatch in object_id: "{key._object_id}" and "{target.attrs.get("object_id")}"')
return target
else:
raise Exception(f'Cannot use key "{key}" of type "{type(key)}" to index into a LindiClient')
raise Exception(f'Cannot use key "{key}" of type "{type(key)}" to index into a LindiH5pyFile')


def _download_file(url: str, filename: str) -> None:
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from .LindiClient import LindiClient # noqa: F401
from .LindiH5pyFile import LindiH5pyFile # noqa: F401
from .LindiGroup import LindiGroup # noqa: F401
from .LindiDataset import LindiDataset # noqa: F401
from .LindiAttributes import LindiAttributes # noqa: F401
Expand Down
4 changes: 2 additions & 2 deletions lindi/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
from .LindiClient import LindiClient, LindiGroup, LindiDataset, LindiAttributes, LindiReference # noqa: F401
from .LindiH5Store import LindiH5Store, LindiH5StoreOpts # noqa: F401
from .LindiH5pyFile import LindiH5pyFile, LindiGroup, LindiDataset, LindiAttributes, LindiReference # noqa: F401
from .LindiH5ZarrStore import LindiH5ZarrStore, LindiH5ZarrStoreOpts # noqa: F401
Loading

0 comments on commit 0e93bec

Please sign in to comment.