-
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.
to_file -> write_reference_file_system
- Loading branch information
Showing
4 changed files
with
79 additions
and
14 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
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 |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import tempfile | ||
import os | ||
import numpy as np | ||
import lindi | ||
import shutil | ||
|
||
|
||
def test_staging_area(): | ||
with tempfile.TemporaryDirectory() as tmpdir: | ||
staging_area = lindi.StagingArea.create(tmpdir + '/staging_area') | ||
empty_rfs = {'refs': {'.zgroup': {'zarr_format': 2}}} | ||
client = lindi.LindiH5pyFile.from_reference_file_system(empty_rfs, mode='r+', staging_area=staging_area) | ||
X = np.random.randn(1000, 1000).astype(np.float32) | ||
client.create_dataset('large_array', data=X, chunks=(400, 400)) | ||
total_size = _get_total_size_of_directory(tmpdir) | ||
assert total_size >= X.nbytes * 0.5, f'{total_size} < {X.nbytes} * 0.5' # take into consideration compression | ||
rfs = client.to_reference_file_system() | ||
client2 = lindi.LindiH5pyFile.from_reference_file_system(rfs, mode='r') | ||
assert isinstance(client2, lindi.LindiH5pyFile) | ||
X1 = client['large_array'] | ||
assert isinstance(X1, lindi.LindiH5pyDataset) | ||
X2 = client2['large_array'] | ||
assert isinstance(X2, lindi.LindiH5pyDataset) | ||
assert np.allclose(X1[:], X2[:]) | ||
|
||
upload_dir = f'{tmpdir}/upload_dir' | ||
os.makedirs(upload_dir, exist_ok=True) | ||
output_fname = f'{tmpdir}/output.lindi.json' | ||
|
||
def on_upload_blob(fname: str): | ||
random_fname = f'{upload_dir}/{_random_string(10)}' | ||
shutil.copy(fname, random_fname) | ||
return random_fname | ||
|
||
def on_upload_main(fname: str): | ||
shutil.copy(fname, output_fname) | ||
return output_fname | ||
|
||
assert client.staging_store | ||
client.staging_store.upload( | ||
on_upload_blob=on_upload_blob, | ||
on_upload_main=on_upload_main, | ||
consolidate_chunks=True | ||
) | ||
|
||
client3 = lindi.LindiH5pyFile.from_reference_file_system(output_fname, mode='r') | ||
X3 = client3['large_array'] | ||
assert isinstance(X3, lindi.LindiH5pyDataset) | ||
assert np.allclose(X1[:], X3[:]) | ||
|
||
|
||
def _get_total_size_of_directory(directory): | ||
total_size = 0 | ||
for dirpath, dirnames, filenames in os.walk(directory): | ||
for f in filenames: | ||
fp = os.path.join(dirpath, f) | ||
total_size += os.path.getsize(fp) | ||
return total_size | ||
|
||
|
||
def _random_string(n): | ||
import random | ||
import string | ||
return ''.join(random.choices(string.ascii_uppercase + string.digits, k=n)) | ||
|
||
|
||
if __name__ == '__main__': | ||
test_staging_area() |
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