Skip to content

Commit 5d106de

Browse files
committed
Bumped version number
adding save_json(filename) and load_json(filename) to save and load from json it is not recomended to save simulations as json and load them back, this process is destructive to information since json does not keep precise type information like hdf5 does, this is mostly intended as a way to just store the setup in json to simplify some ways of trading simulations around in general json can be used in addition to hdf5 but never as a replacement
1 parent a7befbd commit 5d106de

File tree

5 files changed

+62
-3
lines changed

5 files changed

+62
-3
lines changed

CADET-Python.pyproj

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@
55
<SchemaVersion>2.0</SchemaVersion>
66
<ProjectGuid>{12356d41-d8b4-45c3-8413-01e847dcfac2}</ProjectGuid>
77
<ProjectHome />
8-
<StartupFile>examples\SMB.py</StartupFile>
8+
<StartupFile>examples\cadet_json.py</StartupFile>
99
<SearchPath />
1010
<WorkingDirectory>.</WorkingDirectory>
1111
<OutputPath>.</OutputPath>
1212
<ProjectTypeGuids>{888888a0-9f3d-457c-b088-3a5042f75d52}</ProjectTypeGuids>
1313
<LaunchProvider>Standard Python launcher</LaunchProvider>
14-
<InterpreterId>Global|ContinuumAnalytics|Anaconda37-64</InterpreterId>
14+
<InterpreterId>CondaEnv|CondaEnv|cadet_devel</InterpreterId>
1515
<IsWindowsApplication>False</IsWindowsApplication>
1616
</PropertyGroup>
1717
<PropertyGroup Condition="'$(Configuration)' == 'Debug'" />
@@ -29,6 +29,9 @@
2929
<Compile Include="cadet\__init__.py">
3030
<SubType>Code</SubType>
3131
</Compile>
32+
<Compile Include="examples\cadet_json.py">
33+
<SubType>Code</SubType>
34+
</Compile>
3235
<Compile Include="examples\cadet_library.py">
3336
<SubType>Code</SubType>
3437
</Compile>
@@ -76,6 +79,7 @@
7679
</Compile>
7780
</ItemGroup>
7881
<ItemGroup>
82+
<InterpreterReference Include="CondaEnv|CondaEnv|cadet_devel" />
7983
<InterpreterReference Include="Global|ContinuumAnalytics|Anaconda37-64" />
8084
</ItemGroup>
8185
<ItemGroup>

cadet/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
name = 'CADET-python'
22

3+
__version__ = 0.4
4+
35
from .cadet import H5
46
from .cadet import Cadet

cadet/cadet.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import subprocess
99
import pprint
1010
import copy
11+
import json
1112

1213
from pathlib import Path
1314

@@ -44,6 +45,20 @@ def save(self):
4445
else:
4546
print("Filename must be set before save can be used")
4647

48+
def save_json(self, filename):
49+
with Path(filename).open("w") as fp:
50+
data = convert_from_numpy(self.root, self.transform)
51+
json.dump(data, fp, indent=4, sort_keys=True)
52+
53+
def load_json(self, filename, update=False):
54+
with Path(filename).open("r") as fp:
55+
data = json.load(fp)
56+
data = recursively_load_dict(data, self.inverse_transform)
57+
if update:
58+
self.root.update(data)
59+
else:
60+
self.root = data
61+
4762
def append(self):
4863
"This can only be used to write new keys to the system, this is faster than having to read the data before writing it"
4964
if self.filename is not None:
@@ -97,6 +112,35 @@ def run(self, timeout = None, check=None):
97112
else:
98113
print("Filename must be set before run can be used")
99114

115+
def convert_from_numpy(data, func):
116+
ans = Dict()
117+
for key_original,item in data.items():
118+
key = func(key_original)
119+
if isinstance(item, numpy.ndarray):
120+
item = item.tolist()
121+
122+
if isinstance(item, numpy.generic):
123+
item = item.item()
124+
125+
if isinstance(item, bytes):
126+
item = item.decode('ascii')
127+
128+
if isinstance(item, Dict):
129+
ans[key_original] = convert_from_numpy(item, func)
130+
else:
131+
ans[key] = item
132+
return ans
133+
134+
def recursively_load_dict( data, func):
135+
ans = Dict()
136+
for key_original,item in data.items():
137+
key = func(key_original)
138+
if isinstance(item, dict):
139+
ans[key] = recursively_load_dict(item, func)
140+
else:
141+
ans[key] = item
142+
return ans
143+
100144
def recursively_load( h5file, path, func, paths):
101145
ans = Dict()
102146
if paths is not None:

examples/cadet_json.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import cadet
2+
3+
sim = cadet.Cadet()
4+
sim.filename = r"C:\Users\kosh_000\Documents\Visual Studio 2017\Projects\CADETMatch\Examples\Example1\Dextran\dextran_pulse.h5"
5+
sim.load()
6+
7+
sim.save_json(r"C:\Users\kosh_000\Documents\Visual Studio 2017\Projects\CADETMatch\Examples\Example1\Dextran\dextran_pulse.json")
8+
9+
sim.load_json(r"C:\Users\kosh_000\Documents\Visual Studio 2017\Projects\CADETMatch\Examples\Example1\Dextran\dextran_pulse.json")

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
setuptools.setup(
77
name="CADET",
8-
version="0.3",
8+
version="0.4",
99
author="William Heymann",
1010
author_email="[email protected]",
1111
description="CADET is a python interface to the CADET chromatography simulator",

0 commit comments

Comments
 (0)