Skip to content

Commit

Permalink
Bumped version number
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Immudzen committed Jun 29, 2020
1 parent a7befbd commit 5d106de
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 3 deletions.
8 changes: 6 additions & 2 deletions CADET-Python.pyproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{12356d41-d8b4-45c3-8413-01e847dcfac2}</ProjectGuid>
<ProjectHome />
<StartupFile>examples\SMB.py</StartupFile>
<StartupFile>examples\cadet_json.py</StartupFile>
<SearchPath />
<WorkingDirectory>.</WorkingDirectory>
<OutputPath>.</OutputPath>
<ProjectTypeGuids>{888888a0-9f3d-457c-b088-3a5042f75d52}</ProjectTypeGuids>
<LaunchProvider>Standard Python launcher</LaunchProvider>
<InterpreterId>Global|ContinuumAnalytics|Anaconda37-64</InterpreterId>
<InterpreterId>CondaEnv|CondaEnv|cadet_devel</InterpreterId>
<IsWindowsApplication>False</IsWindowsApplication>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)' == 'Debug'" />
Expand All @@ -29,6 +29,9 @@
<Compile Include="cadet\__init__.py">
<SubType>Code</SubType>
</Compile>
<Compile Include="examples\cadet_json.py">
<SubType>Code</SubType>
</Compile>
<Compile Include="examples\cadet_library.py">
<SubType>Code</SubType>
</Compile>
Expand Down Expand Up @@ -76,6 +79,7 @@
</Compile>
</ItemGroup>
<ItemGroup>
<InterpreterReference Include="CondaEnv|CondaEnv|cadet_devel" />
<InterpreterReference Include="Global|ContinuumAnalytics|Anaconda37-64" />
</ItemGroup>
<ItemGroup>
Expand Down
2 changes: 2 additions & 0 deletions cadet/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
name = 'CADET-python'

__version__ = 0.4

from .cadet import H5
from .cadet import Cadet
44 changes: 44 additions & 0 deletions cadet/cadet.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import subprocess
import pprint
import copy
import json

from pathlib import Path

Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
9 changes: 9 additions & 0 deletions examples/cadet_json.py
Original file line number Diff line number Diff line change
@@ -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")
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setuptools.setup(
name="CADET",
version="0.3",
version="0.4",
author="William Heymann",
author_email="[email protected]",
description="CADET is a python interface to the CADET chromatography simulator",
Expand Down

0 comments on commit 5d106de

Please sign in to comment.