Skip to content

Commit

Permalink
xarray accessors
Browse files Browse the repository at this point in the history
  • Loading branch information
loco-philippe committed May 17, 2024
1 parent 0eff0bb commit 729ff1a
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 6 deletions.
52 changes: 52 additions & 0 deletions ntv_numpy/xarray_accessors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# -*- coding: utf-8 -*-
"""
The `xarray_accessors` module is part of the `ntv-numpy.ntv_numpy` package.
It contains the class `NnpDatasetAccessors`.
For more information, see the
[user guide](https://loco-philippe.github.io/ntv-numpy/docs/user_guide.html)
or the [github repository](https://github.com/loco-philippe/ntv-numpy).
"""
import xarray as xr

from ntv_numpy.xdataset import Xdataset

try:
# delete the accessor to avoid warning
del xr.Dataset.nnp
except AttributeError:
pass

@xr.register_dataset_accessor("nnp")
class NnpDatasetAccessor:
"""Accessor class for methods invoked as `xr.Dataset.nnp.*`"""

def __init__(self, xarray_obj):
'''initialisation of the class'''
self._obj = xarray_obj

def to_dataframe(self, **kwargs):
"""Accessor for method `Xdataset.from_xarray.to_dataframe` invoked as
xr.Dataset.nnp.to_dataframe`.
*Parameters*
- **ntv_type**: Boolean (default True) - if False use full_name else json_name
- **info**: Boolean (default True) - if True add xdt.info in DataFrame.attrs
- **dims**: list of string (default None) - order of dimensions full_name to apply
- **index**: Boolean (default True) - if True, dimensions are translated into indexes
"""
return Xdataset.from_xarray(self._obj, **kwargs).to_dataframe(**kwargs)

def to_scipp(self, **kwargs):
"""Accessor for method `Xdataset.from_xarray.to_scipp` invoked as
xr.Dataset.nnp.to_scipp`.
*Parameters*
- **ntv_type**: Boolean (default True) - if False use full_name else json_name
- **info**: Boolean (default True) - if True add xdt.info in DataFrame.attrs
- **dims**: list of string (default None) - order of dimensions full_name to apply
"""
return Xdataset.from_xarray(self._obj, **kwargs).to_scipp(**kwargs)
5 changes: 3 additions & 2 deletions ntv_numpy/xconnector.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,9 @@ def xexport(xdt, **kwargs):
- **ntv_type**: Boolean (default True) - if False use full_name else json_name
- **info**: Boolean (default True) - if True add xdt.info in DataFrame.attrs
- **dims**: list of string (default None) - order of dimensions full_name to apply
- **index**: Boolean (default True) - if True, dimensions are translated into indexes
'''
opt = {'ntv_type': True, 'info': True, 'dims': None} | kwargs
opt = {'ntv_type': True, 'info': True, 'index': True, 'dims': None} | kwargs
dic_name = {name: xdt[name].json_name if opt['ntv_type'] else xdt[name].full_name
for name in xdt.names}
dims = xdt.dimensions if not opt['dims'] else tuple(opt['dims'])
Expand All @@ -109,7 +110,7 @@ def xexport(xdt, **kwargs):
for name in fields_array}
dfr = pd.DataFrame(dic_series)
index = [dic_name[name] for name in dims]
if index:
if index and opt['index']:
dfr = dfr.set_index(index)
if opt['info']:
dfr.attrs |= {'info': xdt.tab_info}
Expand Down
6 changes: 2 additions & 4 deletions ntv_numpy/xdataset.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
# -*- coding: utf-8 -*-
"""
@author: Philippe@loco-labs.io
The `xdataset` module is part of the `ntv-numpy.ntv_numpy` package ([specification document](
https://loco-philippe.github.io/ES/JSON%20semantic%20format%20(JSON-NTV).htm)).
The `xdataset` module is part of the `ntv-numpy.ntv_numpy` package.
It contains the classes `Xdataset`, `XdatasetInterface`, `XdatasetCategory` for
the multidimensional dataset.
Expand Down Expand Up @@ -231,6 +228,7 @@ def to_dataframe(self, **kwargs):
- **ntv_type**: Boolean (default True) - if False use full_name else json_name
- **info**: Boolean (default True) - if True add xdt.info in DataFrame.attrs
- **dims**: list of string (default None) - order of dimensions full_name to apply
- **index**: Boolean (default True) - if True, dimensions are translated into indexes
'''
return PandasConnec.xexport(self, **kwargs)

Expand Down

0 comments on commit 729ff1a

Please sign in to comment.