-
Notifications
You must be signed in to change notification settings - Fork 18
Feature/to_xarray #23
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
Merged
olivhoenen
merged 7 commits into
iterorganization:develop
from
maarten-ic:feature/to-xarray
Mar 13, 2025
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
6476ff8
Refactor iterators used in ids2nc and nc2ids
maarten-ic 815729c
Refactor ids2nc, extract common tensorization logic in IDSTensorizer
maarten-ic 76c265a
Implement `imas.util.to_xarray`
maarten-ic bd15373
Add `xarray` as optional imas-python dependency
maarten-ic 334268f
Update xarray advanced course to mention imas.util.to_xarray
maarten-ic 10d30ec
Additional documentation for `imas.util.to_xarray`
maarten-ic f44bcbb
Additional documentation and example for to_xarray
maarten-ic File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,73 @@ | ||
| # xarray is an optional dependency, but this module won't be imported when xarray is not | ||
| # available | ||
| import numpy | ||
| import xarray | ||
|
|
||
| from imas.ids_toplevel import IDSToplevel | ||
| from imas.backends.netcdf.ids_tensorizer import IDSTensorizer | ||
| from imas.ids_data_type import IDSDataType | ||
|
|
||
| fillvals = { | ||
| IDSDataType.INT: -(2**31) + 1, | ||
| IDSDataType.STR: "", | ||
| IDSDataType.FLT: numpy.nan, | ||
| IDSDataType.CPX: numpy.nan * (1 + 1j), | ||
| } | ||
|
|
||
|
|
||
| def to_xarray(ids: IDSToplevel, *paths: str) -> xarray.Dataset: | ||
| """See :func:`imas.util.to_xarray`""" | ||
| # We really need an IDS toplevel element | ||
| if not isinstance(ids, IDSToplevel): | ||
| raise TypeError( | ||
| f"to_xarray needs a toplevel IDS element as first argument, but got {ids!r}" | ||
| ) | ||
|
|
||
| # Valid path can use / or . as separator, but IDSTensorizer expects /. The following | ||
| # block checks if the paths are valid, and by using "metadata.path_string" we ensure | ||
| # that / are used as separator. | ||
| try: | ||
| paths = [ids.metadata[path].path_string for path in paths] | ||
| except KeyError as exc: | ||
| raise ValueError(str(exc)) from None | ||
|
|
||
| # Converting lazy-loaded IDSs requires users to specify at least one path | ||
| if ids._lazy and not paths: | ||
| raise RuntimeError( | ||
| "This IDS is lazy loaded. Please provide at least one path to convert to" | ||
| " xarray." | ||
| ) | ||
|
|
||
| # Use netcdf IDS Tensorizer to tensorize the data and determine metadata | ||
| tensorizer = IDSTensorizer(ids, paths) | ||
| tensorizer.include_coordinate_paths() | ||
| tensorizer.collect_filled_data() | ||
| tensorizer.determine_data_shapes() | ||
|
|
||
| data_vars = {} | ||
| coordinate_names = set() | ||
| for path in tensorizer.filled_data: | ||
| var_name = path.replace("/", ".") | ||
| metadata = ids.metadata[path] | ||
| if metadata.data_type in (IDSDataType.STRUCTURE, IDSDataType.STRUCT_ARRAY): | ||
| continue # We don't store these in xarray | ||
|
|
||
| dimensions = tensorizer.ncmeta.get_dimensions(path, tensorizer.homogeneous_time) | ||
| data = tensorizer.tensorize(path, fillvals[metadata.data_type]) | ||
|
|
||
| attrs = dict(documentation=metadata.documentation) | ||
| if metadata.units: | ||
| attrs["units"] = metadata.units | ||
| coordinates = tensorizer.filter_coordinates(path) | ||
| if coordinates: | ||
| coordinate_names.update(coordinates.split(" ")) | ||
| attrs["coordinates"] = coordinates | ||
|
|
||
| data_vars[var_name] = (dimensions, data, attrs) | ||
|
|
||
| # Remove coordinates from data_vars and put in coordinates mapping: | ||
| coordinates = {} | ||
| for coordinate_name in coordinate_names: | ||
| coordinates[coordinate_name] = data_vars.pop(coordinate_name) | ||
|
|
||
| return xarray.Dataset(data_vars, coordinates) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there still value in keeping this lesson if it's completely superceded by the new capabilities introduced by this PR?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some of the examples can still be useful for people, so I would not remove it: there's some practical use cases for accessing IDS metadata, and some examples for working with IMAS data in Xarray Datasets.