From 729ff1af65330923116fc91ef0e38ae43c5b3311 Mon Sep 17 00:00:00 2001 From: Philippe THOMY Date: Fri, 17 May 2024 15:12:34 +0200 Subject: [PATCH] xarray accessors --- ntv_numpy/xarray_accessors.py | 52 +++++++++++++++++++++++++++++++++++ ntv_numpy/xconnector.py | 5 ++-- ntv_numpy/xdataset.py | 6 ++-- 3 files changed, 57 insertions(+), 6 deletions(-) create mode 100644 ntv_numpy/xarray_accessors.py diff --git a/ntv_numpy/xarray_accessors.py b/ntv_numpy/xarray_accessors.py new file mode 100644 index 0000000..f3e920b --- /dev/null +++ b/ntv_numpy/xarray_accessors.py @@ -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) diff --git a/ntv_numpy/xconnector.py b/ntv_numpy/xconnector.py index 67a481f..a666c36 100644 --- a/ntv_numpy/xconnector.py +++ b/ntv_numpy/xconnector.py @@ -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']) @@ -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} diff --git a/ntv_numpy/xdataset.py b/ntv_numpy/xdataset.py index 40cda8c..2a19221 100644 --- a/ntv_numpy/xdataset.py +++ b/ntv_numpy/xdataset.py @@ -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. @@ -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)