diff --git a/CADET-Python.pyproj b/CADET-Python.pyproj index 2edb092..0345aa4 100644 --- a/CADET-Python.pyproj +++ b/CADET-Python.pyproj @@ -5,13 +5,13 @@ 2.0 {12356d41-d8b4-45c3-8413-01e847dcfac2} - examples\SMB.py + examples\cadet_json.py . . {888888a0-9f3d-457c-b088-3a5042f75d52} Standard Python launcher - Global|ContinuumAnalytics|Anaconda37-64 + CondaEnv|CondaEnv|cadet_devel False @@ -29,6 +29,9 @@ Code + + Code + Code @@ -76,6 +79,7 @@ + diff --git a/cadet/__init__.py b/cadet/__init__.py index 44f9e87..3a2c4b7 100644 --- a/cadet/__init__.py +++ b/cadet/__init__.py @@ -1,4 +1,6 @@ name = 'CADET-python' +__version__ = 0.4 + from .cadet import H5 from .cadet import Cadet diff --git a/cadet/cadet.py b/cadet/cadet.py index afdcfb0..85c7a12 100644 --- a/cadet/cadet.py +++ b/cadet/cadet.py @@ -8,6 +8,7 @@ import subprocess import pprint import copy +import json from pathlib import Path @@ -44,6 +45,20 @@ def save(self): else: print("Filename must be set before save can be used") + def save_json(self, filename): + with Path(filename).open("w") as fp: + data = convert_from_numpy(self.root, self.transform) + json.dump(data, fp, indent=4, sort_keys=True) + + def load_json(self, filename, update=False): + with Path(filename).open("r") as fp: + data = json.load(fp) + data = recursively_load_dict(data, self.inverse_transform) + if update: + self.root.update(data) + else: + self.root = data + def append(self): "This can only be used to write new keys to the system, this is faster than having to read the data before writing it" if self.filename is not None: @@ -97,6 +112,35 @@ def run(self, timeout = None, check=None): else: print("Filename must be set before run can be used") +def convert_from_numpy(data, func): + ans = Dict() + for key_original,item in data.items(): + key = func(key_original) + if isinstance(item, numpy.ndarray): + item = item.tolist() + + if isinstance(item, numpy.generic): + item = item.item() + + if isinstance(item, bytes): + item = item.decode('ascii') + + if isinstance(item, Dict): + ans[key_original] = convert_from_numpy(item, func) + else: + ans[key] = item + return ans + +def recursively_load_dict( data, func): + ans = Dict() + for key_original,item in data.items(): + key = func(key_original) + if isinstance(item, dict): + ans[key] = recursively_load_dict(item, func) + else: + ans[key] = item + return ans + def recursively_load( h5file, path, func, paths): ans = Dict() if paths is not None: diff --git a/examples/cadet_json.py b/examples/cadet_json.py new file mode 100644 index 0000000..15eb106 --- /dev/null +++ b/examples/cadet_json.py @@ -0,0 +1,9 @@ +import cadet + +sim = cadet.Cadet() +sim.filename = r"C:\Users\kosh_000\Documents\Visual Studio 2017\Projects\CADETMatch\Examples\Example1\Dextran\dextran_pulse.h5" +sim.load() + +sim.save_json(r"C:\Users\kosh_000\Documents\Visual Studio 2017\Projects\CADETMatch\Examples\Example1\Dextran\dextran_pulse.json") + +sim.load_json(r"C:\Users\kosh_000\Documents\Visual Studio 2017\Projects\CADETMatch\Examples\Example1\Dextran\dextran_pulse.json") diff --git a/setup.py b/setup.py index 04ecac3..36fb7af 100644 --- a/setup.py +++ b/setup.py @@ -5,7 +5,7 @@ setuptools.setup( name="CADET", - version="0.3", + version="0.4", author="William Heymann", author_email="w.heymann@fz-juelich.de", description="CADET is a python interface to the CADET chromatography simulator",