mrsimulator I/O

Simulator object

Export simulator object to a JSON file

To serialize a Simulator object to a JSON-compliant file, use the save() method of the object.


where sim_coesite is a Simulator object. By default, the attribute values are serialized as physical quantities, represented as a string with a value and a unit.

Load simulator object from a JSON file

To load a JSON-compliant Simulator serialized file, use the load() method of the class. By default, the load method parses the file for units.

>>> from mrsimulator import Simulator
>>> sim_load = Simulator.load('sample.mrsim')
>>> sim_coesite == sim_load

Spin systems objects from Simulator class

Export spin systems to a JSON file

You may also serialize the spin system objects from the Simulator object to a JSON-compliant file using the export_spin_systems() method as

>>> sim_coesite.export_spin_systems('coesite_spin_systems.mrsys')

Import spin systems from a JSON file

Similarly, a list of spin systems can be directly imported from a JSON serialized file. To import the spin systems, use the load_spin_systems() method of the Simulator class as

>>> sim.load_spin_systems('coesite_spin_systems.mrsys')

Importing spin systems from URL

>>> from mrsimulator import Simulator
>>> sim = Simulator()
>>> filename = ''
>>> sim.load_spin_systems(filename)
>>> # The seven spin systems from the file are added to the sim object.
>>> len(sim.spin_systems)

Method objects from Simulator class

Export methods to a JSON file

Similarly, you may also serialize the method objects from the Simulator object to a JSON-compliant file using export_methods() as

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

Import methods from a JSON file

Likewise, to import methods, use load_methods() as

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

Serialize simulation object from Method class as CSDM compliant file

Export simulation to a JSON file

You may serialize the simulation object from the method object to a CSDM compliant JSON file using the save function as follows,

>>> sim_coesite.method[0]'coesite_simulation.csdf') 

Serialize Simulator, SignalProcessor object to file

Export Simulator, SignalProcessor objects to a JSON file

You may serialize the Simulator, a list of SignalProcessor objects to a .mrsim file as follows. The order of SignalProcessor objects is the order of the methods in the Simulator object.

>>> from mrsimulator import save
>>> save('coesite.mrsim', sim_coesite, processors) 

Load Simulator, SignalProcessor objects from a JSON file

>>> from mrsimulator import load
>>> sim_coesite, processors, _ = load('coesite.mrsim')