From a83fe1f0d04fe8ca5aefb73d3988ad5e2dfbb1e5 Mon Sep 17 00:00:00 2001 From: Philippe THOMY Date: Wed, 15 May 2024 23:57:04 +0200 Subject: [PATCH] #4 --- ntv_numpy/xconnector.py | 20 ++++++++++---------- ntv_numpy/xdataset.py | 28 ++++++++++++++++++++-------- tests/tests_ntv_numpy.py | 14 +++++++------- 3 files changed, 37 insertions(+), 25 deletions(-) diff --git a/ntv_numpy/xconnector.py b/ntv_numpy/xconnector.py index 0e9869f..67a481f 100644 --- a/ntv_numpy/xconnector.py +++ b/ntv_numpy/xconnector.py @@ -92,12 +92,12 @@ def xexport(xdt, **kwargs): *Parameters* - - **json_name**: Boolean (default True) - if False use full_name else json_name + - **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 ''' - opt = {'json_name': True, 'info': True, 'dims': None} | kwargs - dic_name = {name: xdt[name].json_name if opt['json_name'] else xdt[name].full_name + opt = {'ntv_type': True, 'info': 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']) fields = (xdt.group(dims) + xdt.group(xdt.coordinates) + @@ -301,10 +301,10 @@ def xexport(xdt, **kwargs): - **dataset** : Boolean (default True) - if False and a single data_var, return a xr.DataArray - - **datagroup** : Boolean (default True) - if True, add json representation + - **info** : Boolean (default True) - if True, add json representation of 'relative' Xndarrays and 'data_arrays' Xndarrays in attrs ''' - option = {'dataset': True, 'datagroup': True} | kwargs + option = {'dataset': True, 'info': True} | kwargs coords = XarrayConnec._to_xr_vars( xdt, xdt.dimensions + xdt.coordinates + xdt.uniques) coords |= XarrayConnec._to_xr_vars(xdt, xdt.additionals) @@ -384,12 +384,12 @@ def _to_xr_attrs(xdt, **option): *Parameters* - - **datagroup** : Boolean if True, add json representation of 'relative' + - **info** : Boolean if True, add json representation of 'relative' Xndarrays and 'data_arrays' Xndarrays in attrs ''' attrs = {meta: xdt[meta].meta for meta in xdt.metadata} attrs |= {'name': xdt.name} if xdt.name else {} - if option['datagroup']: + if option['info']: for name in xdt.names: if xdt[name].mode == 'relative': attrs |= xdt[name].to_json(header=False) @@ -449,11 +449,11 @@ def xexport(xdt, **kwargs): - **dataset** : Boolean (default True) - if False and a single data_var, return a DataArray - - **datagroup** : Boolean (default True) - if True return a DataGroup with + - **info** : Boolean (default True) - if True return a DataGroup with metadata and data_arrays - **ntv_type** : Boolean (default True) - if True add ntv-type to the name ''' - option = {'dataset': True, 'datagroup': True, + option = {'dataset': True, 'info': True, 'ntv_type': True} | kwargs coords = dict([ScippConnec._to_scipp_var(xdt, name, **option) for name in xdt.coordinates + xdt.dimensions + xdt.uniques @@ -462,7 +462,7 @@ def xexport(xdt, **kwargs): for name in xdt.data_vars if xdt[name].mode == 'absolute'])) scd = scd if option['dataset'] else scd[list(scd)[0]] - if not option['datagroup']: + if not option['info']: return scd sc_name = xdt.name if xdt.name else 'no_name' return sc.DataGroup({sc_name: scd} | ScippConnec._to_scipp_grp(xdt, **option)) diff --git a/ntv_numpy/xdataset.py b/ntv_numpy/xdataset.py index a843c11..40cda8c 100644 --- a/ntv_numpy/xdataset.py +++ b/ntv_numpy/xdataset.py @@ -180,15 +180,15 @@ def to_json(self, **kwargs): encoded=kwargs.get('encoded', False)) def to_xarray(self, **kwargs): - '''return a DataArray or a Dataset from a Xdataset + '''return a xr.DataArray or a xr.Dataset from a Xdataset *Parameters* - **dataset** : Boolean (default True) - if False and a single data_var, - return a sc.DataArray - - **datagroup** : Boolean (default True) - if True, return a sc.DataGroup - which contains the sc.DataArray/sc.Dataset and the other data else only - sc.DataArray/sc.Dataset''' + return a xr.DataArray + - **info** : Boolean (default True) - if True, add json representation + of 'relative' Xndarrays and 'data_arrays' Xndarrays in attrs + ''' return XarrayConnec.xexport(self, **kwargs) @staticmethod @@ -203,7 +203,7 @@ def to_scipp(self, **kwargs): - **dataset** : Boolean (default True) - if False and a single data_var, return a DataArray - - **datagroup** : Boolean (default True) - if True return a DataGroup with + - **info** : Boolean (default True) - if True return a DataGroup with metadata and data_arrays - **ntv_type** : Boolean (default True) - if True add ntv-type to the name ''' @@ -224,12 +224,24 @@ def from_nddata(ndd, **kwargs): return AstropyNDDataConnec.ximport(ndd, Xdataset, **kwargs) def to_dataframe(self, **kwargs): - '''return a pd.DataFrame from a Xdataset''' + '''return a pd.DataFrame from a Xdataset + + *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 PandasConnec.xexport(self, **kwargs) @staticmethod def from_dataframe(dfr, **kwargs): - '''return a Xdataset from a pd.DataFrame''' + '''return a Xdataset from a pd.DataFrame + + *Parameters* + + - dims: list of string (default None) - order of dimensions to apply + ''' return PandasConnec.ximport(dfr, Xdataset, **kwargs) diff --git a/tests/tests_ntv_numpy.py b/tests/tests_ntv_numpy.py index 43f610f..f5d2904 100644 --- a/tests/tests_ntv_numpy.py +++ b/tests/tests_ntv_numpy.py @@ -18,7 +18,7 @@ from ntv_numpy import Darray, Dfull, Ndarray, Xndarray, Xdataset, Dutil from ntv_numpy.xconnector import PandasConnec -import ntv_pandas as npd # activation of pandas ntv connector +import ntv_pandas as npd # activation of pandas ntv connector nd_equals = Dutil.equals FILE = 'https://raw.githubusercontent.com/loco-philippe/ntv-numpy/master/example/ex_ndarray.ntv' @@ -519,13 +519,13 @@ def test_xdataset_scipp(self): for example in examples: xd = Xdataset.read_json(example) xd2 = Xdataset.from_scipp( - xd.to_scipp(dataset=False, datagroup=False)) + xd.to_scipp(dataset=False, info=False)) self.assertEqual(xd, xd2) xd2 = Xdataset.from_scipp(xd.to_scipp(dataset=False)) self.assertEqual(xd, xd2) xd2 = Xdataset.from_scipp(xd.to_scipp()) self.assertEqual(xd, xd2) - xd2 = Xdataset.from_scipp(xd.to_scipp(datagroup=False)) + xd2 = Xdataset.from_scipp(xd.to_scipp(info=False)) self.assertEqual(xd, xd2) def test_xdataset_mixte(self): @@ -584,7 +584,7 @@ def test_xdataset_dataframe(self): if tab is not None: self.assertTrue(np.all(np.array(df[name]) == tab), name) - dfr = xdt.to_dataframe(json_name=True) + dfr = xdt.to_dataframe(ntv_type=True) xds = Xdataset.from_dataframe(dfr) self.assertEqual(xds, xdt) @@ -668,7 +668,7 @@ def test_xdataset_multipart(self): df1 = pd.DataFrame(fruits) a_df = df1.npd.analysis(distr=True) xdt = Xdataset.from_dataframe(df1) - df3 = xdt.to_dataframe(json_name=False).reset_index() + df3 = xdt.to_dataframe(ntv_type=False).reset_index() df2 = df1.sort_values(a_df.partitions(mode='id') [0]).reset_index(drop=True) df4 = df3.sort_values(a_df.partitions(mode='id')[ @@ -685,7 +685,7 @@ def test_xdataset_unidim(self): 'e': [1, 1, 1, 1, 1]} df1 = pd.DataFrame(simple) df3 = Xdataset.from_dataframe(df1).to_dataframe( - json_name=False)[df1.columns] + ntv_type=False)[df1.columns] self.assertTrue(df3.equals(df1)) simple = {'a': [1, 2, 3, 4, 5], @@ -696,7 +696,7 @@ def test_xdataset_unidim(self): 'e': [1, 1, 1, 1, 1]} df1 = pd.DataFrame(simple) df3 = Xdataset.from_dataframe(df1).to_dataframe( - json_name=False).reset_index()[df1.columns] + ntv_type=False).reset_index()[df1.columns] self.assertTrue(df3.equals(df1))