From ab52606b3cfbf4739d7e016759a528f43d82e363 Mon Sep 17 00:00:00 2001 From: khider <11758571+khider@users.noreply.github.com> Date: Thu, 6 Apr 2023 14:19:14 -0700 Subject: [PATCH] enable export to and from json for MS --- pyleoclim/core/multipleseries.py | 46 +++++++++++++++++++++++++++++++- pyleoclim/utils/jsonutils.py | 5 ++++ 2 files changed, 50 insertions(+), 1 deletion(-) diff --git a/pyleoclim/core/multipleseries.py b/pyleoclim/core/multipleseries.py index 0ab379f0..8fe489a1 100644 --- a/pyleoclim/core/multipleseries.py +++ b/pyleoclim/core/multipleseries.py @@ -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 @@ -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) diff --git a/pyleoclim/utils/jsonutils.py b/pyleoclim/utils/jsonutils.py index 3389446f..28a91e28 100644 --- a/pyleoclim/utils/jsonutils.py +++ b/pyleoclim/utils/jsonutils.py @@ -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