-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #60 from NeurodataWithoutBorders/lindi-file
implement lindi.File, drop-in replacement for h5py.File that supports .lindi.json files based on extension (4)
- Loading branch information
Showing
11 changed files
with
103 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
*.lindi.json | ||
*.lindi.json* | ||
*.nwb | ||
|
||
.coverage | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
from typing import Literal | ||
import os | ||
import h5py | ||
from ..LindiH5pyFile.LindiH5pyFile import LindiH5pyFile | ||
from ..LindiStagingStore.StagingArea import StagingArea | ||
from ..LocalCache.LocalCache import LocalCache | ||
|
||
|
||
class File(h5py.File): | ||
""" | ||
A drop-in replacement for h5py.File that is either a lindi.LindiH5pyFile or | ||
h5py.File depending on whether the file name ends with .lindi.json or not. | ||
""" | ||
def __new__(cls, name, mode: Literal['r', 'r+', 'w', 'w-', 'x', 'a'] = 'r', **kwds): | ||
if isinstance(name, str) and name.endswith('.lindi.json'): | ||
# should we raise exceptions on select unsupported kwds? or just go with the flow? | ||
if mode != 'r': | ||
staging_area = StagingArea.create(dir=name + '.d') | ||
else: | ||
staging_area = None | ||
local_cache_dir = os.environ.get('LINDI_LOCAL_CACHE_DIR', None) | ||
if local_cache_dir is not None: | ||
local_cache = LocalCache(cache_dir=local_cache_dir) | ||
else: | ||
local_cache = None | ||
|
||
return LindiH5pyFile.from_lindi_file( | ||
name, | ||
mode=mode, | ||
staging_area=staging_area, | ||
local_cache=local_cache | ||
) | ||
else: | ||
return h5py.File(name, mode=mode, **kwds) |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[tool.poetry] | ||
name = "lindi" | ||
version = "0.3.0" | ||
version = "0.3.1" | ||
description = "" | ||
authors = [ | ||
"Jeremy Magland <[email protected]>", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import tempfile | ||
import numpy as np | ||
import h5py | ||
import lindi | ||
|
||
|
||
def test_lindi_file(): | ||
with tempfile.TemporaryDirectory() as tmpdir: | ||
fname = f'{tmpdir}/test.lindi.json' | ||
with lindi.File(fname, 'w') as f: | ||
f.create_dataset('data', data=np.arange(500000, dtype=np.uint32), chunks=(100000,)) | ||
|
||
with lindi.File(fname, 'r') as f: | ||
ds = f['data'] | ||
assert isinstance(ds, h5py.Dataset) | ||
assert ds.shape == (500000,) | ||
assert ds.chunks == (100000,) | ||
assert ds.dtype == np.uint32 | ||
assert np.all(ds[:] == np.arange(500000, dtype=np.uint32)) | ||
|
||
|
||
if __name__ == '__main__': | ||
test_lindi_file() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters