Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Paths as URIs #243

Merged
merged 78 commits into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
78 commits
Select commit Hold shift + click to select a range
c787da4
validate that paths can be coerced to valid URIs
TomNicholas Oct 2, 2024
b2f3f26
add a test that paths are converted to URIs
TomNicholas Oct 2, 2024
6b15d9e
added test and better error if local path is not absolute
TomNicholas Oct 2, 2024
045cc7a
raise more informative error if path is not absolute
TomNicholas Oct 2, 2024
ddc6b7f
test that empty paths are allowed
TomNicholas Oct 2, 2024
61bf5d6
add failing test for raising on malformed paths
TomNicholas Oct 2, 2024
2896bd8
fix paths in tests
TomNicholas Oct 2, 2024
584adf9
fix more tests
TomNicholas Oct 2, 2024
14b8ca3
remove the file:/// prefix when writing to kerchunk format
TomNicholas Oct 2, 2024
bad0b1b
Merge branch 'main' into paths_as_uris
TomNicholas Oct 19, 2024
739a6bd
absolute paths in recently-added tests
TomNicholas Oct 19, 2024
06f3a4d
absolute paths in recently-added tests
TomNicholas Oct 19, 2024
3d50def
fix one more test
TomNicholas Oct 20, 2024
b37f8d0
Merge branch 'main' into paths_as_uris
TomNicholas Nov 21, 2024
a9bd0a3
stop wrapping specific error in less useful one
TomNicholas Nov 21, 2024
f0bc445
moved remaining kerchunk parsing logic out into translator file
TomNicholas Nov 21, 2024
b224af8
add fs_root parameter to validation fn
TomNicholas Nov 21, 2024
c8d513c
demote ChunkEntry to a TypedDict to separate validation fn
TomNicholas Nov 21, 2024
4ad26cf
actually instead add new constructor method to TypedDict
TomNicholas Nov 21, 2024
7aaaabf
test kerchunk writer with absolute paths
TomNicholas Nov 21, 2024
f66ed79
make kerchunk reader tests pass
TomNicholas Nov 21, 2024
e30ab05
try to implement the fs_root concatenation
TomNicholas Nov 21, 2024
0a4a11d
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Nov 21, 2024
98644a7
implementation using cloudpathlib working
TomNicholas Nov 22, 2024
4499cf9
check that fs_root doesn't have a filetype suffix
TomNicholas Nov 22, 2024
d27bdf6
add cloudpathlib to dependencies
TomNicholas Nov 22, 2024
fefab90
allow http paths, and require file suffixes
TomNicholas Nov 22, 2024
927121c
unit tests for path validation
TomNicholas Nov 22, 2024
c561a5d
test whether we can catch malformed paths
TomNicholas Nov 22, 2024
d42ff33
test fs_root
TomNicholas Nov 22, 2024
4b7cd2d
add (unimplemented) validate_entries kwarg to .from_arrays
TomNicholas Nov 23, 2024
0f8b550
add .keys(), .values(), .items()
TomNicholas Nov 23, 2024
8055ba7
test validate_paths during .from_arrays
TomNicholas Nov 23, 2024
f18bf59
ensure validation actually normalizes paths to uris
TomNicholas Nov 23, 2024
96f6360
test .rename_paths correctly validates paths
TomNicholas Nov 23, 2024
b2ed1cd
some release notes
TomNicholas Nov 23, 2024
87fad80
remove now-superfluous coercion to URI in icechunk writer
TomNicholas Nov 23, 2024
1f41d69
added link to icechunk writer performance benchmark
TomNicholas Nov 23, 2024
41ec08f
add reader_kwargs argument to open_virtual_dataset, and pass it down …
TomNicholas Nov 23, 2024
b4cc617
Merge branch 'main' into reader_kwargs
TomNicholas Nov 23, 2024
ffff93e
Merge branch 'main' into paths_as_uris
TomNicholas Nov 23, 2024
26e8950
Merge branch 'main' into paths_as_uris
TomNicholas Nov 23, 2024
19c13ae
ensure relative paths containing .. can be normalized
TomNicholas Nov 23, 2024
6ce7ebd
ensure HDF5 reader always returns absolute URIs
TomNicholas Nov 23, 2024
24b6ea7
ensure HDF reader always returns absolute URIs
TomNicholas Nov 23, 2024
5f0f362
add relative path handling to other kerchunk-based readers
TomNicholas Nov 23, 2024
001f95b
Merge branch 'reader_kwargs' into paths_as_uris
TomNicholas Nov 23, 2024
15ae139
add dmrpp relative path integration test
ayushnag Nov 24, 2024
b3432c6
Merge branch 'main' into paths_as_uris
TomNicholas Nov 26, 2024
ae00ac3
Merge branch 'main' into paths_as_uris
TomNicholas Nov 27, 2024
ddc19c3
Merge branch 'main' into filepath_test_dmrpp
TomNicholas Nov 27, 2024
8353a9b
fix kerchunk relative paths test by pluggin through fs_root kwarg
TomNicholas Nov 27, 2024
251469f
Merge branch 'filepath_test_dmrpp' of https://github.com/ayushnag/Vir…
TomNicholas Nov 27, 2024
0915433
fix dmrpp tests by using absolute filepaths in DMR++ contents
TomNicholas Nov 27, 2024
3ca8f7f
clarify new dmrpp test
TomNicholas Nov 27, 2024
8a90ec7
test handling of relative filepaths to dmrpp files
TomNicholas Nov 27, 2024
4afa34c
group related tests
TomNicholas Nov 27, 2024
da9ee10
removed cloudpathlib from validation code
TomNicholas Dec 1, 2024
3557008
fix bug but restrict fs_root to only handle filesystem paths, not buc…
TomNicholas Dec 2, 2024
ca09096
global list of recognized URI prefixes
TomNicholas Dec 2, 2024
7727d4e
cleanup
TomNicholas Dec 2, 2024
03aa6bc
remove cloudpathlib from dependencies
TomNicholas Dec 2, 2024
4d8ceea
fix/ignore some typing errors
TomNicholas Dec 2, 2024
cd5bec1
rewrite tests to use a new dmrparser_factory
TomNicholas Dec 3, 2024
e521e20
rewrite using global dict of XML strings
TomNicholas Dec 3, 2024
4ff17d0
fix final test by explicitly passing in tmp_path instead of using a f…
TomNicholas Dec 3, 2024
7e0be24
Merge branch 'dmrpp_tests_tmp_path' into paths_as_uris
TomNicholas Dec 3, 2024
bf0bb87
fix bug with not converting Path objects to strings
TomNicholas Dec 3, 2024
25acdf9
Merge branch 'dmrpp_tests_tmp_path' into paths_as_uris
TomNicholas Dec 3, 2024
ac04cef
dmrpp relative paths tests passing
TomNicholas Dec 3, 2024
39bbfaf
fix type hint for filetype kwarg
TomNicholas Dec 3, 2024
9b6906b
Merge branch 'main' into paths_as_uris
TomNicholas Dec 3, 2024
0127f05
user documentation on fs_root
TomNicholas Dec 3, 2024
cd5da5d
change example manifests to use URIs
TomNicholas Dec 3, 2024
df6663f
reminder that rename_paths exists
TomNicholas Dec 3, 2024
be20994
Merge branch 'paths_as_uris' of https://github.com/TomNicholas/Virtua…
TomNicholas Dec 3, 2024
528f722
update release notes
TomNicholas Dec 3, 2024
a8d5a15
remove note about .rename_paths
TomNicholas Dec 3, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/releases.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ Breaking changes

- Minimum required version of Xarray is now v2024.10.0.
(:pull:`284`) By `Tom Nicholas <https://github.com/TomNicholas>`_.
- Opening kerchunk-formatted references from disk which contain relative paths now requires passing the ``fs_root`` keyword argument via ``virtual_backend_kwargs``.
(:pull:`243`) By `Tom Nicholas <https://github.com/TomNicholas>`_.

Deprecations
~~~~~~~~~~~~
Expand Down Expand Up @@ -50,6 +52,8 @@ Internal Changes
(:pull:`87`) By `Sean Harkins <https://github.com/sharkinsspatial>`_.
- Support downstream type checking by adding py.typed marker file.
(:pull:`306`) By `Max Jones <https://github.com/maxrjones>`_.
- File paths in chunk manifests are now always stored as abolute URIs.
(:pull:`243`) By `Tom Nicholas <https://github.com/TomNicholas>`_.

.. _v1.1.0:

Expand Down
37 changes: 25 additions & 12 deletions docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,10 @@ manifest = marr.manifest
manifest.dict()
```
```python
{'0.0.0': {'path': 'air.nc', 'offset': 15419, 'length': 7738000}}
{'0.0.0': {'path': 'file:///work/data/air.nc', 'offset': 15419, 'length': 7738000}}
```

In this case we can see that the `"air"` variable contains only one chunk, the bytes for which live in the `air.nc` file at the location given by the `'offset'` and `'length'` attributes.
In this case we can see that the `"air"` variable contains only one chunk, the bytes for which live in the `file:///work/data/air.nc` file, at the location given by the `'offset'` and `'length'` attributes.

The {py:class}`ChunkManifest <virtualizarr.manifests.ChunkManifest>` class is virtualizarr's internal in-memory representation of this manifest.

Expand Down Expand Up @@ -153,8 +153,8 @@ ManifestArray<shape=(5840, 25, 53), dtype=int16, chunks=(2920, 25, 53)>
concatenated.manifest.dict()
```
```
{'0.0.0': {'path': 'air.nc', 'offset': 15419, 'length': 7738000},
'1.0.0': {'path': 'air.nc', 'offset': 15419, 'length': 7738000}}
{'0.0.0': {'path': 'file:///work/data/air.nc', 'offset': 15419, 'length': 7738000},
'1.0.0': {'path': 'file:///work/data/air.nc', 'offset': 15419, 'length': 7738000}}
```

This concatenation property is what will allow us to combine the data from multiple netCDF files on disk into a single Zarr store containing arrays of many chunks.
Expand Down Expand Up @@ -254,8 +254,8 @@ We can see that the resulting combined manifest has two chunks, as expected.
combined_vds['air'].data.manifest.dict()
```
```
{'0.0.0': {'path': 'air1.nc', 'offset': 15419, 'length': 3869000},
'1.0.0': {'path': 'air2.nc', 'offset': 15419, 'length': 3869000}}
{'0.0.0': {'path': 'file:///work/data/air1.nc', 'offset': 15419, 'length': 3869000},
'1.0.0': {'path': 'file:///work/data/air2.nc', 'offset': 15419, 'length': 3869000}}
```

```{note}
Expand Down Expand Up @@ -327,8 +327,6 @@ Loading variables can be useful in a few scenarios:

To correctly decode time variables according to the CF conventions, you need to pass `time` to `loadable_variables` and ensure the `decode_times` argument of `open_virtual_dataset` is set to True (`decode_times` defaults to None).



```python
vds = open_virtual_dataset(
'air.nc',
Expand All @@ -354,8 +352,6 @@ Attributes:
title: 4x daily NMC reanalysis (1948)
```



## Writing virtual stores to disk

Once we've combined references to all the chunks of all our legacy files into one virtual xarray dataset, we still need to write these references out to disk so that they can be read by our analysis code later.
Expand Down Expand Up @@ -443,20 +439,37 @@ This store can however be read by {py:func}`~virtualizarr.xarray.open_virtual_da
You can open existing Kerchunk `json` or `parquet` references as Virtualizarr virtual datasets. This may be useful for converting existing Kerchunk formatted references to storage formats like [Icechunk](https://icechunk.io/).

```python

vds = open_virtual_dataset('combined.json', filetype='kerchunk', indexes={})
# or
vds = open_virtual_dataset('combined.parquet', filetype='kerchunk', indexes={})
```

One difference between the kerchunk references format and virtualizarr's internal manifest representation (as well as icechunk's format) is that paths in kerchunk references can be relative paths. Opening kerchunk references that contain relative local filepaths therefore requires supplying another piece of information: the directory of the ``fsspec`` filesystem which the filepath was defined relative to.

You can dis-ambuiguate kerchunk references containing relative paths by passing the ``fs_root`` kwarg to ``virtual_backend_kwargs``.

```python
# file `relative_refs.json` contains a path like './file.nc'

vds = open_virtual_dataset(
'relative_refs.json',
filetype='kerchunk',
indexes={},
virtual_backend_kwargs={'fs_root': 'file:///some_directory/'}
)

# the path in the virtual dataset will now be 'file:///some_directory/file.nc'
```

Note that as the virtualizarr {py:meth}`vds.virtualize.to_kerchunk <virtualizarr.xarray.VirtualiZarrDatasetAccessor.to_kerchunk>` method only writes absolute paths, the only scenario in which you might come across references containing relative paths is if you are opening references that were previously created using the ``kerchunk`` library alone.

## Rewriting existing manifests

Sometimes it can be useful to rewrite the contents of an already-generated manifest or virtual dataset.

### Rewriting file paths

You can rewrite the file paths stored in a manifest or virtual dataset without changing the byte range information using the {py:meth}`ds.virtualize.rename_paths <virtualizarr.xarray.VirtualiZarrDatasetAccessor.rename_paths>` accessor method.
You can rewrite the file paths stored in a manifest or virtual dataset without changing the byte range information using the {py:meth}`vds.virtualize.rename_paths <virtualizarr.xarray.VirtualiZarrDatasetAccessor.rename_paths>` accessor method.

For example, you may want to rename file paths according to a function to reflect having moved the location of the referenced files from local storage to an S3 bucket.

Expand Down
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ test = [
"virtualizarr[hdf_reader]"
]


[project.urls]
Home = "https://github.com/TomNicholas/VirtualiZarr"
Documentation = "https://github.com/TomNicholas/VirtualiZarr/blob/main/README.md"
Expand Down
2 changes: 1 addition & 1 deletion virtualizarr/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ def automatically_determine_filetype(
def open_virtual_dataset(
filepath: str,
*,
filetype: FileType | None = None,
filetype: FileType | str | None = None,
group: str | None = None,
drop_variables: Iterable[str] | None = None,
loadable_variables: Iterable[str] | None = None,
Expand Down
19 changes: 0 additions & 19 deletions virtualizarr/manifests/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
_isnan,
)
from virtualizarr.manifests.manifest import ChunkManifest
from virtualizarr.types.kerchunk import KerchunkArrRefs
from virtualizarr.zarr import ZArray


Expand Down Expand Up @@ -62,24 +61,6 @@ def __init__(
self._zarray = _zarray
self._manifest = _chunkmanifest

@classmethod
def _from_kerchunk_refs(cls, arr_refs: KerchunkArrRefs) -> "ManifestArray":
from virtualizarr.translators.kerchunk import (
fully_decode_arr_refs,
parse_array_refs,
)

decoded_arr_refs = fully_decode_arr_refs(arr_refs)

chunk_dict, zarray, _zattrs = parse_array_refs(decoded_arr_refs)
manifest = ChunkManifest._from_kerchunk_chunk_dict(chunk_dict)

obj = object.__new__(cls)
obj._manifest = manifest
obj._zarray = zarray

return obj

@property
def manifest(self) -> ChunkManifest:
return self._manifest
Expand Down
Loading
Loading