Skip to content

Commit

Permalink
Merge branch 'v0.6.x' of https://github.com/DeepanshS/MRsimulator int…
Browse files Browse the repository at this point in the history
…o v0.6.x
  • Loading branch information
deepanshs committed Oct 10, 2021
2 parents 00c129c + d0a6334 commit d8977c8
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 10 deletions.
8 changes: 6 additions & 2 deletions .github/workflows/continuous-integration-pip.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@ name: CI (pip)

on:
push:
branches: [master]
branches:
- master
- v?.?*
pull_request:
branches: [master]
branches:
- master
- v?.?*

jobs:
code_lint:
Expand Down
2 changes: 2 additions & 0 deletions docs/api_py/simulator.rst
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ Simulator
.. automethod:: get_isotopes
.. automethod:: load_spin_systems
.. automethod:: export_spin_systems
.. automethod:: load_methods
.. automethod:: export_methods
.. automethod:: run
.. automethod:: save
.. automethod:: load
Expand Down
32 changes: 29 additions & 3 deletions docs/mrsim_IO.rst
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ as a string with a value and a unit.

.. >>> sim_coesite.save('sample_no_units.json', with_units=False)
**Load simulator object from a JSON file**

To load a JSON-compliant :ref:`simulator_api` serialized file, use the
Expand All @@ -54,10 +53,8 @@ parses the file for units.
>>> import os
>>> os.remove('sample.mrsim')


----


Spin systems objects from Simulator class
-----------------------------------------

Expand Down Expand Up @@ -103,6 +100,34 @@ class as
>>> import os
>>> os.remove('spin_systems.json')

----

Method objects from Simulator class
-----------------------------------------

**Export methods to a JSON file**

Similarly, you may also serialize the method objects from the :ref:`simulator_api` object to
a JSON-compliant file using :py:meth:`~mrsimulator.Simulator.export_methods` as

.. doctest::

>>> sim_coesite.export_methods('coesite_method.mrmtd')


**Import methods from a JSON file**

Likewise, to import methods, use :py:meth:`~mrsimulator.Simulator.load_methods` as

.. doctest::

>>> sim.load_methods('coesite_method.mrmtd')

.. testsetup::
>>> import os
>>> os.remove('coesite_method.mrmtd')

----

Serialize simulation object from Method class as CSDM compliant file
--------------------------------------------------------------------
Expand All @@ -116,6 +141,7 @@ save function as follows,

>>> sim_coesite.method[0].simulation.save('coesite_simulation.csdf') # doctest:+SKIP

----

Serialize Simulator, SignalProcessor object to file
---------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ def message(self, lib_centos, lib_ubuntu):
print(f"For CentOS users\n\t{stat}")
stat = f"sudo apt-get update\n\tsudo apt-get install {lib_ubuntu}"
print(f"For Ubuntu users\n\t{stat}")
sys.exit(1)
# sys.exit(1)


class MacOSSetup(Setup):
Expand Down
2 changes: 1 addition & 1 deletion src/mrsimulator/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
__license__ = "BSD License"
__maintainer__ = "Deepansh J. Srivastava"
__status__ = "Beta"
__version__ = "0.6.0"
__version__ = "0.6.1"

import os

Expand Down
31 changes: 30 additions & 1 deletion src/mrsimulator/methods/tests/test_method_updates.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
sp = {"count": 1024, "spectral_width": 100}


def test_read_write_methods():
def test_read_write_obj_mrsim():
for item in methods:
kwargs = {"channels": ["93Nb"], "spectral_dimensions": [sp] * item.ndim}

Expand All @@ -27,6 +27,8 @@ def test_read_write_methods():

sim = Simulator(spin_systems=[sys], methods=[fn1])
sim.run()

# Test simulator object (.mrsim)
sim.save("test.mrsim")

sim2 = Simulator.load("test.mrsim")
Expand All @@ -36,4 +38,31 @@ def test_read_write_methods():

assert sim == sim2, f"Error with {item} parse from Simulator.load()."

# Test spin system objects (.mrsys)
sim.export_spin_systems("test.mrsys")

sim2 = sim.copy()
sim2.load_spin_systems("test.mrsys")

assert (
sim.spin_systems == sim2.spin_systems
), f"Error with {item} sim.load_spin_systems()."

# Test methods objects (.mrmth)
sim.export_methods("test.mrmth")

sim2 = sim.copy()
sim2.load_methods("test.mrmth")

# remove timestamps from csdm obj for testing
for mth in sim2.methods:
for item in [mth.simulation, mth.experiment]:
if item is not None:
item._timestamp = ""
_ = [item.to("ppm", "nmr_frequency_ratio") for item in mth.simulation.x]

assert sim.methods == sim2.methods, f"Error with {item} sim.load_methods()."

os.remove("test.mrsim")
os.remove("test.mrsys")
os.remove("test.mrmth")
43 changes: 41 additions & 2 deletions src/mrsimulator/simulator/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,6 @@ def load_spin_systems(self, filename: str):
>>> sim.load_spin_systems(filename) # doctest:+SKIP
"""
contents = import_json(filename)
# json_data = contents["spin_systems"]
self.spin_systems = [SpinSystem.parse_dict_with_units(obj) for obj in contents]

def export_spin_systems(self, filename: str):
Expand All @@ -384,12 +383,52 @@ def export_spin_systems(self, filename: str):
>>> sim.export_spin_systems(filename) # doctest:+SKIP
"""
spin_sys = [SpinSystem.json(obj) for obj in self.spin_systems]
spin_sys = [obj.json() for obj in self.spin_systems]
with open(filename, "w", encoding="utf8") as outfile:
json.dump(
spin_sys, outfile, ensure_ascii=False, sort_keys=False, allow_nan=False
)

def load_methods(self, filename: str):
"""Load a list of methods from the given JSON serialized file.
Args:
str filename: A local or remote address to a JSON serialized file.
Example
-------
>>> sim.load_methods(filename) # doctest:+SKIP
"""
contents = import_json(filename)
method_cls = [
Method
if obj["name"] not in __method_names__
else __sim_methods__[obj["name"]]
for obj in contents
]

self.methods = [
fn.parse_dict_with_units(obj) for obj, fn in zip(contents, method_cls)
]

def export_methods(self, filename: str):
"""Export a list of methods to a JSON serialized file.
Args:
str filename: A filename of the serialized file.
Example
-------
>>> sim.export_methods(filename) # doctest:+SKIP
"""
mth = [obj.json() for obj in self.methods]
with open(filename, "w", encoding="utf8") as outfile:
json.dump(
mth, outfile, ensure_ascii=False, sort_keys=False, allow_nan=False
)

def run(
self,
method_index: list = None,
Expand Down

0 comments on commit d8977c8

Please sign in to comment.