Skip to content
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

enable export to and from json for MS #376

Merged
merged 1 commit into from
Apr 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 45 additions & 1 deletion pyleoclim/core/multipleseries.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
to such a collection at once (e.g. process a bunch of series in a consistent fashion).
"""

from ..utils import tsutils, plotting
from ..utils import tsutils, plotting, jsonutils
from ..utils import correlation as corrutils

from ..core.correns import CorrEns
Expand Down Expand Up @@ -2288,3 +2288,47 @@ def to_pandas(self, paleo_style=False, *args, use_common_time=False, **kwargs):
tl = ms.series_list[0].time_name
df.index.name = tl if tl is not None else 'time'
return df


def to_json(self, path=None):
'''
Export the pyleoclim.MultipleSeries object to a json file

Parameters
----------
path : string, optional
The path to the file. The default is None, resulting in a file saved in the current working directory using the label for the dataset as filename if available or 'mulitpleseries.json' if label is not provided.

Returns
-------
None.

'''

if path is None:
path = self.series_list[0].label.replace(" ", "_") + '.json' if self.series_list[0].label is not None else 'multipleseries.json'

jsonutils.PyleoObj_to_json(self, path)

@classmethod
def from_json(cls, path):
''' Creates a pyleoclim.MulitpleSeries from a JSON file

The keys in the JSON file must correspond to the parameter associated with MulitpleSeries and Series objects

Parameters
----------
path : str
Path to the JSON file

Returns
-------
ts : pyleoclim.core.series.MulitplesSeries
A Pyleoclim MultipleSeries object.

'''

a = jsonutils.open_json(path)
b = jsonutils.iterate_through_dict(a, 'MultipleSeries')

return cls(**b)
5 changes: 5 additions & 0 deletions pyleoclim/utils/jsonutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,11 @@ def iterate_through_dict(dictionary, objname):
item['timeseries'] = pyleo.Series(**item['timeseries'])
a[k]['scalogram_list'][idx]=pyleo.Scalogram(**a[k]['scalogram_list'][idx])
a[k] = pyleo.MultipleScalogram(**a[k])

#Deal with MultipleSeries
if objname == 'MultipleSeries':
for idx,item in enumerate(a['series_list']):
a['series_list'][idx]=pyleo.Series(**item)

return a

Expand Down