Skip to content

Commit

Permalink
#4
Browse files Browse the repository at this point in the history
  • Loading branch information
loco-philippe committed May 15, 2024
1 parent daf9680 commit a83fe1f
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 25 deletions.
20 changes: 10 additions & 10 deletions ntv_numpy/xconnector.py
Original file line number Diff line number Diff line change
Expand Up @@ -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) +
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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))
Expand Down
28 changes: 20 additions & 8 deletions ntv_numpy/xdataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
'''
Expand All @@ -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)


Expand Down
14 changes: 7 additions & 7 deletions tests/tests_ntv_numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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')[
Expand All @@ -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],
Expand All @@ -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))


Expand Down

0 comments on commit a83fe1f

Please sign in to comment.