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.
>>> sim_coesite.save('sample.mrsim')
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
True
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 = 'https://raw.githubusercontent.com/DeepanshS/mrsimulator-examples/master/spin_systems.json'
>>> sim.load_spin_systems(filename)
>>> # The seven spin systems from the file are added to the sim object.
>>> len(sim.spin_systems)
7