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 0e93bec commit 4320681
Show file tree
Hide file tree
Showing 16 changed files with 101 additions and 101 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.LindiH5pyFile.from_reference_file_system(rfs)
client = lindi.LindiZarrWrapper.from_reference_file_system(rfs)

# Try to read using pynwb
# (This part does not work yet)
Expand Down
14 changes: 7 additions & 7 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 LindiH5pyFile, LindiGroup, LindiDataset
from lindi import LindiZarrWrapper, LindiZarrWrapperGroup, LindiZarrWrapperDataset


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

for k, v in client.attrs.items():
print(f"{k}: {v}")
Expand All @@ -11,23 +11,23 @@ def test_lindi_client():
print(k)

acquisition = client["acquisition"]
assert isinstance(acquisition, LindiGroup)
assert isinstance(acquisition, LindiZarrWrapperGroup)
for k in acquisition.keys():
print(k)

aa = client["acquisition/ElectricalSeriesAp"]
assert isinstance(aa, LindiGroup)
assert isinstance(aa, LindiZarrWrapperGroup)
x = aa["data"]
assert isinstance(x, LindiDataset)
assert isinstance(x, LindiZarrWrapperDataset)

print(x.shape)
print(x[:5])

general = client["general"]
assert isinstance(general, LindiGroup)
assert isinstance(general, LindiZarrWrapperGroup)
for k in general.keys():
a = general[k]
if isinstance(a, LindiDataset):
if isinstance(a, LindiZarrWrapperDataset):
print(f"{k}: {a.shape}")
print(a[()])

Expand Down
20 changes: 10 additions & 10 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 LindiH5ZarrStore, LindiH5pyFile, LindiGroup, LindiDataset
from lindi import LindiH5ZarrStore, LindiZarrWrapper, LindiZarrWrapperGroup, LindiZarrWrapperDataset


def test_scalar_dataset():
Expand All @@ -15,12 +15,12 @@ def test_scalar_dataset():
filename, url=filename
) as store: # set url so that a reference file system can be created
rfs = store.to_reference_file_system()
client = LindiH5pyFile.from_reference_file_system(rfs)
client = LindiZarrWrapper.from_reference_file_system(rfs)
h5f = h5py.File(filename, "r")
X1 = h5f["X"]
assert isinstance(X1, h5py.Dataset)
X2 = client["X"]
assert isinstance(X2, LindiDataset)
assert isinstance(X2, LindiZarrWrapperDataset)
if not _check_equal(X1[()], X2[()]):
print(f"WARNING: {X1} ({type(X1)}) != {X2} ({type(X2)})")

Expand Down Expand Up @@ -73,12 +73,12 @@ def test_numpy_array():
filename, url=filename
) as store: # set url so that a reference file system can be created
rfs = store.to_reference_file_system()
client = LindiH5pyFile.from_reference_file_system(rfs)
client = LindiZarrWrapper.from_reference_file_system(rfs)
h5f = h5py.File(filename, "r")
X1 = h5f["X"]
assert isinstance(X1, h5py.Dataset)
X2 = client["X"]
assert isinstance(X2, LindiDataset)
assert isinstance(X2, LindiZarrWrapperDataset)
if not _check_equal(X1[:], X2[:]):
print("WARNING. Arrays are not equal")
print(X1[:])
Expand All @@ -94,11 +94,11 @@ def test_numpy_array_of_strings():
h5f = h5py.File(filename, "r")
with LindiH5ZarrStore.from_file(filename, url=filename) as store:
rfs = store.to_reference_file_system()
client = LindiH5pyFile.from_reference_file_system(rfs)
client = LindiZarrWrapper.from_reference_file_system(rfs)
X1 = h5f["X"]
assert isinstance(X1, h5py.Dataset)
X2 = client["X"]
assert isinstance(X2, LindiDataset)
assert isinstance(X2, LindiZarrWrapperDataset)
if not _check_equal(X1[:], X2[:]):
print("WARNING. Arrays are not equal")
print(X1[:])
Expand All @@ -122,11 +122,11 @@ def test_attributes():
h5f = h5py.File(filename, "r")
with LindiH5ZarrStore.from_file(filename, url=filename) as store:
rfs = store.to_reference_file_system()
client = LindiH5pyFile.from_reference_file_system(rfs)
client = LindiZarrWrapper.from_reference_file_system(rfs)
X1 = h5f["X"]
assert isinstance(X1, h5py.Dataset)
X2 = client["X"]
assert isinstance(X2, LindiDataset)
assert isinstance(X2, LindiZarrWrapperDataset)
if X1.attrs["foo"] != X2.attrs["foo"]:
print("WARNING. Attributes are not equal")
print(X1.attrs["foo"])
Expand All @@ -138,7 +138,7 @@ def test_attributes():
group1 = h5f["group"]
assert isinstance(group1, h5py.Group)
group2 = client["group"]
assert isinstance(group2, LindiGroup)
assert isinstance(group2, LindiZarrWrapperGroup)
if group1.attrs["foo"] != group2.attrs["foo"]:
print("WARNING. Attributes are not equal")
print(group1.attrs["foo"])
Expand Down
16 changes: 8 additions & 8 deletions examples/example1.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import h5py
import tempfile
import lindi
from lindi import LindiH5ZarrStore, LindiH5pyFile, LindiDataset
from lindi import LindiH5ZarrStore, LindiZarrWrapper, LindiZarrWrapperDataset
from _check_equal import _check_equal


Expand All @@ -11,9 +11,9 @@ def example1():
# datasets, groups, and attributes. We then load that file using
# 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 LindiH5pyFile, which mimics the h5py API. We then compare the
# create a LindiZarrWrapper, which mimics the h5py API. We then compare the
# datasets, groups, and attributes of the original hdf5 file with those of
# the LindiH5pyFile.
# the LindiZarrWrapper.

with tempfile.TemporaryDirectory() as tmpdir:
print("Creating an example hdf5 file")
Expand All @@ -39,14 +39,14 @@ def example1():
rfs_fname = f"{tmpdir}/example.zarr.json"
store.to_file(rfs_fname)

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

print("Comparing dataset: X")
X1 = h5f["X"]
assert isinstance(X1, h5py.Dataset)
X2 = client["X"]
assert isinstance(X2, LindiDataset)
assert isinstance(X2, LindiZarrWrapperDataset)
assert len(X1) == len(X2)
assert X1.shape == X2.shape
assert X1.dtype == X2.dtype
Expand All @@ -70,14 +70,14 @@ def example1():
scalar_dataset1 = h5f["scalar_dataset"]
assert isinstance(scalar_dataset1, h5py.Dataset)
scalar_dataset2 = client["scalar_dataset"]
assert isinstance(scalar_dataset2, LindiDataset)
assert isinstance(scalar_dataset2, LindiZarrWrapperDataset)
assert scalar_dataset1[()] == scalar_dataset2[()]

print("Comparing group: group")
G1 = h5f["group"]
G2 = client["group"]
for k, v in G1.attrs.items():
if not isinstance(G2, lindi.LindiReference):
if not isinstance(G2, lindi.LindiZarrWrapperReference):
assert k in G2.attrs
assert _check_equal(v, G2.attrs[k])

Expand Down
2 changes: 1 addition & 1 deletion examples/try_pynwb.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def try_pynwb():
json.dump(rfs, f, indent=2)

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

# Try to read using pynwb
with pynwb.NWBHDF5IO(file=client, mode="r") as io:
Expand Down
4 changes: 2 additions & 2 deletions lindi/LindiH5ZarrStore/LindiH5ZarrStore.py
Original file line number Diff line number Diff line change
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 LindiH5pyFile.from_file(fname)
This can then be loaded using LindiZarrWrapper.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
LindiH5pyFile.from_reference_file_system(obj)
LindiZarrWrapper.from_reference_file_system(obj)
"""
if self._h5f is None:
raise Exception("Store is closed")
Expand Down
5 changes: 0 additions & 5 deletions lindi/LindiH5pyFile/__init__.py

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
import urllib.request
from fsspec.implementations.reference import ReferenceFileSystem
from zarr.storage import Store
from .LindiGroup import LindiGroup
from .LindiReference import LindiReference
from .LindiZarrWrapperGroup import LindiZarrWrapperGroup
from .LindiZarrWrapperReference import LindiZarrWrapperReference


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

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

@staticmethod
def from_file(
json_file: str, file_type: Literal["zarr.json"] = "zarr.json"
) -> "LindiH5pyFile":
) -> "LindiZarrWrapper":
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 LindiH5pyFile.from_reference_file_system(data)
return LindiZarrWrapper.from_reference_file_system(data)
else:
with open(json_file, "r") as f:
data = json.load(f)
return LindiH5pyFile.from_reference_file_system(data)
return LindiZarrWrapper.from_reference_file_system(data)
else:
raise ValueError(f"Unknown file_type: {file_type}")

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

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

def get(self, key, default=None, getlink: bool = False):
try:
Expand All @@ -66,7 +66,7 @@ def get(self, key, default=None, getlink: bool = False):
if getlink:
return ret
else:
if isinstance(ret, LindiReference):
if isinstance(ret, LindiZarrWrapperReference):
return self[ret]
else:
return ret
Expand All @@ -83,7 +83,7 @@ def __getitem__(self, key): # type: ignore
for part in parts:
g = g[part]
return g
elif isinstance(key, LindiReference):
elif isinstance(key, LindiZarrWrapperReference):
if key._source != '.':
raise Exception(f'For now, source of reference must be ".", got "{key._source}"')
if key._source_object_id is not None:
Expand All @@ -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 LindiH5pyFile')
raise Exception(f'Cannot use key "{key}" of type "{type(key)}" to index into a LindiZarrWrapper')


def _download_file(url: str, filename: str) -> None:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from typing import Union
import zarr
from .LindiReference import LindiReference
from .LindiZarrWrapperReference import LindiZarrWrapperReference


class LindiAttributes:
class LindiZarrWrapperAttributes:
def __init__(self, *, _object: Union[zarr.Group, zarr.Array]):
self._object = _object

Expand All @@ -16,7 +16,7 @@ def get(self, key, default=None):
def __getitem__(self, key):
val = self._object.attrs[key]
if isinstance(val, dict) and "_REFERENCE" in val:
return LindiReference(val["_REFERENCE"])
return LindiZarrWrapperReference(val["_REFERENCE"])
return self._object.attrs[key]

def __setitem__(self, key, value):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
import zarr
import h5py
import remfile
from .LindiAttributes import LindiAttributes
from .LindiReference import LindiReference
from .LindiZarrWrapperAttributes import LindiZarrWrapperAttributes
from .LindiZarrWrapperReference import LindiZarrWrapperReference


class LindiDataset:
class LindiZarrWrapperDataset:
def __init__(self, *, _zarr_array: zarr.Array, _client):
self._zarr_array = _zarr_array
self._is_scalar = self._zarr_array.attrs.get("_SCALAR", False)
Expand Down Expand Up @@ -41,7 +41,7 @@ def name(self):
@property
def attrs(self):
"""Attributes attached to this object"""
return LindiAttributes(_object=self._zarr_array)
return LindiZarrWrapperAttributes(_object=self._zarr_array)

@property
def ndim(self):
Expand Down Expand Up @@ -115,7 +115,7 @@ def __getitem__(self, selection):
dtype = np.dtype(dt)
# Return a new object that can be sliced further
# It's important that the return type is Any here, because otherwise we get linter problems
ret: Any = LindiDatasetCompoundFieldSelection(
ret: Any = LindiZarrWrapperDatasetCompoundFieldSelection(
dataset=self, ind=ind, dtype=dtype
)
return ret
Expand All @@ -139,14 +139,14 @@ def _get_external_hdf5_client(self, url: str) -> h5py.File:
return self._external_hdf5_clients[url]


class LindiDatasetCompoundFieldSelection:
class LindiZarrWrapperDatasetCompoundFieldSelection:
"""
This class is returned when a compound dataset is indexed with a field name.
For example, if the dataset has dtype [('x', 'f4'), ('y', 'f4')], then we
can do dataset['x'][0] to get the first x value. The dataset['x'] returns an
object of this class.
"""
def __init__(self, *, dataset: LindiDataset, ind: int, dtype: np.dtype):
def __init__(self, *, dataset: LindiZarrWrapperDataset, ind: int, dtype: np.dtype):
self._dataset = dataset # The parent dataset
self._ind = ind # The index of the field in the compound dtype
self._dtype = dtype # The dtype of the field
Expand All @@ -159,8 +159,8 @@ def __init__(self, *, dataset: LindiDataset, ind: int, dtype: np.dtype):
za = self._dataset._zarr_array
d = [za[i][self._ind] for i in range(len(za))]
if self._dtype == h5py.Reference:
# Convert to LindiReference
d = [LindiReference(x['_REFERENCE']) for x in d]
# Convert to LindiZarrWrapperReference
d = [LindiZarrWrapperReference(x['_REFERENCE']) for x in d]
self._data = np.array(d, dtype=self._dtype)

def __len__(self):
Expand Down
Loading

0 comments on commit 4320681

Please sign in to comment.