JSON serialisation and deserialisation

[1]:
%matplotlib inline
import matplotlib
matplotlib.rcParams['figure.dpi'] = 100

try:
    import srxraylib.plot.gol as _gol
    _gol.set_qt = lambda: None
except Exception:
    pass

[2]:

from syned.util.json_tools import load_from_json_file from syned.storage_ring.electron_beam import ElectronBeam from syned.storage_ring.magnetic_structures.undulator import Undulator from syned.beamline.optical_elements.ideal_elements.screen import Screen from syned.beamline.optical_elements.ideal_elements.ideal_lens import IdealLens from syned.beamline.optical_elements.absorbers.filter import Filter from syned.beamline.optical_elements.absorbers.slit import Slit from syned.beamline.optical_elements.absorbers.beam_stopper import BeamStopper from syned.beamline.optical_elements.mirrors.mirror import Mirror from syned.beamline.optical_elements.crystals.crystal import Crystal from syned.beamline.optical_elements.gratings.grating import Grating from syned.beamline.shape import SurfaceShape, Conic, Ellipsoid, Plane, Toroid from syned.beamline.shape import Rectangle from syned.storage_ring.light_source import LightSource from syned.beamline.beamline import Beamline from syned.beamline.beamline_element import BeamlineElement from syned.beamline.element_coordinates import ElementCoordinates if __name__ == "__main__": src1 = ElectronBeam.initialize_as_pencil_beam(energy_in_GeV=6.0,current=0.2) src2 = Undulator() lightsource1 = LightSource("test_source", src1, src2) screen1 = Screen("screen1") lens1 = IdealLens(name="lens1",focal_y=6.0,focal_x=None,) filter1 = Filter("filter1","H2O",3.0e-6) slit1 = Slit(name="slit1",boundary_shape=Rectangle(-0.5e-3,0.5e-3,-2e-3,2e-3)) stopper1 = BeamStopper(name="stopper1",boundary_shape=Rectangle(-0.5e-3,0.5e-3,-2e-3,2e-3)) mirror1 = Mirror(name="mirror1",boundary_shape=Rectangle(-0.5e-3,0.5e-3,-2e-3,2e-3), surface_shape=Toroid()) grating1 = Grating(name="grating1",surface_shape=Conic()) crystal1 = Crystal(name="crystal1",surface_shape=Plane()) mylist = [lightsource1, screen1, lens1, filter1, slit1, stopper1, mirror1, crystal1, grating1] # # test individual elements # for i,element in enumerate(mylist): element.to_json("tmp_%d.json"%i) for i,element in enumerate(mylist): print("loading element %d"%i) tmp = load_from_json_file("tmp_%d.json"%i) print("returned class: ",type(tmp)) print(element.to_dictionary()) print(tmp.to_dictionary()) # assert (element.to_dictionary() == tmp.to_dictionary()) # # test full beamline # SCREEN1 = BeamlineElement(screen1, coordinates=ElementCoordinates(p=11.0)) LENS1 = BeamlineElement(lens1, coordinates=ElementCoordinates(p=12.0)) FILTER1 = BeamlineElement(filter1, coordinates=ElementCoordinates(p=13.0)) SLIT1 = BeamlineElement(slit1, coordinates=ElementCoordinates(p=15.0)) STOPPER1 = BeamlineElement(stopper1, coordinates=ElementCoordinates(p=16.0)) MIRROR1 = BeamlineElement(mirror1, coordinates=ElementCoordinates(p=17.0)) GRATING1 = BeamlineElement(grating1, coordinates=ElementCoordinates(p=18.0)) CRYSTAL1 = BeamlineElement(crystal1, coordinates=ElementCoordinates(p=19.0)) MyList = [SCREEN1,LENS1,FILTER1,SLIT1,STOPPER1,MIRROR1,CRYSTAL1,GRATING1] # # test BeamlineElement # # for i,element in enumerate(MyList): # # element.to_json("tmp_%d.json"%(100+i)) # tmp = load_from_json_file("tmp_%d.json"%(100+i)) # print("returned class: ",type(tmp)) # print("\n-----------Info on: \n",tmp.info(),"----------------\n\n") # # test Beamline # BL = Beamline(LightSource(name="test",electron_beam=src1,magnetic_structure=src2), [SCREEN1,LENS1,FILTER1,SLIT1,STOPPER1,MIRROR1,CRYSTAL1,GRATING1]) BL.to_json("tmp_200.json") tmp = load_from_json_file("tmp_200.json") print("returned class: ",type(tmp)) print(BL.to_full_dictionary()) print(tmp.to_full_dictionary()) # assert(BL.to_full_dictionary() == tmp.to_full_dictionary()) # # print(tmp.get_light_source().info()) # for element in tmp.get_beamline_elements(): # print("list element class: ",type(element)) # print(element.info()) # # # # # print("\n-----------Info on: \n",tmp.info(),"----------------\n\n")
File written to disk: tmp_0.json
File written to disk: tmp_1.json
File written to disk: tmp_2.json
File written to disk: tmp_3.json
File written to disk: tmp_4.json
File written to disk: tmp_5.json
File written to disk: tmp_6.json
File written to disk: tmp_7.json
File written to disk: tmp_8.json
loading element 0
returned class:  <class 'syned.storage_ring.light_source.LightSource'>
OrderedDict({'CLASS_NAME': 'LightSource', 'name': 'test_source', 'electron_beam': OrderedDict({'CLASS_NAME': 'ElectronBeam', 'energy_in_GeV': 6.0, 'energy_spread': 0.0, 'current': 0.2, 'number_of_bunches': 1, 'moment_xx': 0.0, 'moment_xxp': 0.0, 'moment_xpxp': 0.0, 'moment_yy': 0.0, 'moment_yyp': 0.0, 'moment_ypyp': 0.0, 'dispersion_x': 0.0, 'dispersion_y': 0.0, 'dispersionp_x': 0.0, 'dispersionp_y': 0.0}), 'magnetic_structure': OrderedDict({'CLASS_NAME': 'Undulator', 'K_vertical': 0.0, 'K_horizontal': 0.0, 'period_length': 0.0, 'number_of_periods': 1.0})})
OrderedDict({'CLASS_NAME': 'LightSource', 'name': 'test_source', 'electron_beam': OrderedDict({'CLASS_NAME': 'ElectronBeam', 'energy_in_GeV': 6.0, 'energy_spread': 0.0, 'current': 0.2, 'number_of_bunches': 1, 'moment_xx': 0.0, 'moment_xxp': 0.0, 'moment_xpxp': 0.0, 'moment_yy': 0.0, 'moment_yyp': 0.0, 'moment_ypyp': 0.0, 'dispersion_x': 0.0, 'dispersion_y': 0.0, 'dispersionp_x': 0.0, 'dispersionp_y': 0.0}), 'magnetic_structure': OrderedDict({'CLASS_NAME': 'Undulator', 'K_vertical': 0.0, 'K_horizontal': 0.0, 'period_length': 0.0, 'number_of_periods': 1.0})})
loading element 1
returned class:  <class 'syned.beamline.optical_elements.ideal_elements.screen.Screen'>
OrderedDict({'CLASS_NAME': 'Screen', 'name': 'screen1', 'boundary_shape': None})
OrderedDict({'CLASS_NAME': 'Screen', 'name': 'screen1', 'boundary_shape': None})
loading element 2
returned class:  <class 'syned.beamline.optical_elements.ideal_elements.ideal_lens.IdealLens'>
OrderedDict({'CLASS_NAME': 'IdealLens', 'name': 'lens1', 'boundary_shape': None, 'focal_x': None, 'focal_y': 6.0})
OrderedDict({'CLASS_NAME': 'IdealLens', 'name': 'lens1', 'boundary_shape': None, 'focal_x': None, 'focal_y': 6.0})
loading element 3
returned class:  <class 'syned.beamline.optical_elements.absorbers.filter.Filter'>
OrderedDict({'CLASS_NAME': 'Filter', 'material': 'H2O', 'thickness': 3e-06})
OrderedDict({'CLASS_NAME': 'Filter', 'material': 'H2O', 'thickness': 3e-06})
loading element 4
returned class:  <class 'syned.beamline.optical_elements.absorbers.slit.Slit'>
OrderedDict({'CLASS_NAME': 'Slit', 'name': 'slit1', 'boundary_shape': OrderedDict({'CLASS_NAME': 'Rectangle', 'x_left': -0.0005, 'x_right': 0.0005, 'y_bottom': -0.002, 'y_top': 0.002})})
OrderedDict({'CLASS_NAME': 'Slit', 'name': 'slit1', 'boundary_shape': OrderedDict({'CLASS_NAME': 'Rectangle', 'x_left': -0.0005, 'x_right': 0.0005, 'y_bottom': -0.002, 'y_top': 0.002})})
loading element 5
returned class:  <class 'syned.beamline.optical_elements.absorbers.beam_stopper.BeamStopper'>
OrderedDict({'CLASS_NAME': 'BeamStopper', 'name': 'stopper1', 'boundary_shape': OrderedDict({'CLASS_NAME': 'Rectangle', 'x_left': -0.0005, 'x_right': 0.0005, 'y_bottom': -0.002, 'y_top': 0.002})})
OrderedDict({'CLASS_NAME': 'BeamStopper', 'name': 'stopper1', 'boundary_shape': OrderedDict({'CLASS_NAME': 'Rectangle', 'x_left': -0.0005, 'x_right': 0.0005, 'y_bottom': -0.002, 'y_top': 0.002})})
loading element 6
returned class:  <class 'syned.beamline.optical_elements.mirrors.mirror.Mirror'>
OrderedDict({'CLASS_NAME': 'Mirror', 'name': 'mirror1', 'surface_shape': OrderedDict({'CLASS_NAME': 'Toroid', 'min_radius': 0.0, 'maj_radius': 0.0}), 'boundary_shape': OrderedDict({'CLASS_NAME': 'Rectangle', 'x_left': -0.0005, 'x_right': 0.0005, 'y_bottom': -0.002, 'y_top': 0.002}), 'coating': None, 'coating_thickness': None})
OrderedDict({'CLASS_NAME': 'Mirror', 'name': 'mirror1', 'surface_shape': OrderedDict({'CLASS_NAME': 'Toroid', 'min_radius': 0.0, 'maj_radius': 0.0}), 'boundary_shape': OrderedDict({'CLASS_NAME': 'Rectangle', 'x_left': -0.0005, 'x_right': 0.0005, 'y_bottom': -0.002, 'y_top': 0.002}), 'coating': None, 'coating_thickness': None})
loading element 7
returned class:  <class 'syned.beamline.optical_elements.crystals.crystal.Crystal'>
OrderedDict({'CLASS_NAME': 'Crystal', 'name': 'crystal1', 'surface_shape': OrderedDict({'CLASS_NAME': 'Plane'}), 'boundary_shape': None, 'material': 'Si', 'diffraction_geometry': 0, 'miller_index_h': 1, 'miller_index_k': 1, 'miller_index_l': 1, 'asymmetry_angle': 0.0, 'thickness': 0.0})
OrderedDict({'CLASS_NAME': 'Crystal', 'name': 'crystal1', 'surface_shape': OrderedDict({'CLASS_NAME': 'Plane'}), 'boundary_shape': None, 'material': 'Si', 'diffraction_geometry': 0, 'miller_index_h': 1, 'miller_index_k': 1, 'miller_index_l': 1, 'asymmetry_angle': 0.0, 'thickness': 0.0})
loading element 8
returned class:  <class 'syned.beamline.optical_elements.gratings.grating.Grating'>
OrderedDict({'CLASS_NAME': 'Grating', 'name': 'grating1', 'surface_shape': OrderedDict({'CLASS_NAME': 'Conic', 'conic_coefficients': array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])}), 'boundary_shape': OrderedDict({'CLASS_NAME': 'BoundaryShape'}), 'ruling': 800000.0})
OrderedDict({'CLASS_NAME': 'Grating', 'name': 'grating1', 'surface_shape': OrderedDict({'CLASS_NAME': 'Conic', 'conic_coefficients': array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])}), 'boundary_shape': OrderedDict({'CLASS_NAME': 'BoundaryShape'}), 'ruling': 800000.0})
File written to disk: tmp_200.json
returned class:  <class 'syned.beamline.beamline.Beamline'>
OrderedDict({'CLASS_NAME': 'Beamline', 'light_source': OrderedDict({'CLASS_NAME': 'LightSource', 'name': ['test', ('Name', '')], 'electron_beam': OrderedDict({'CLASS_NAME': 'ElectronBeam', 'energy_in_GeV': [6.0, ('Electron beam energy', 'GeV')], 'energy_spread': [0.0, ('Electron beam energy spread (relative)', '')], 'current': [0.2, ('Electron beam current', 'A')], 'number_of_bunches': [1, ('Number of bunches', '')], 'moment_xx': [0.0, ('Moment (spatial^2, horizontal)', 'm^2')], 'moment_xxp': [0.0, ('Moment (spatial-angular, horizontal)', 'm')], 'moment_xpxp': [0.0, ('Moment (angular^2, horizontal)', '')], 'moment_yy': [0.0, ('Moment (spatial^2, vertical)', 'm^2')], 'moment_yyp': [0.0, ('Moment (spatial-angular, vertical)', 'm')], 'moment_ypyp': [0.0, ('Moment (angular^2, vertical)', '')], 'dispersion_x': [0.0, ('Dispersion (horizontal)', '')], 'dispersion_y': [0.0, ('Dispersion (vertical)', '')], 'dispersionp_x': [0.0, ('Dispersion Derivative (horizontal)', '')], 'dispersionp_y': [0.0, ('Dispersion Derivative (vertical)', '')]}), 'magnetic_structure': OrderedDict({'CLASS_NAME': 'Undulator', 'K_vertical': [0.0, ('K value (vertical)', '')], 'K_horizontal': [0.0, ('K value (horizontal)', '')], 'period_length': [0.0, ('Period length', 'm')], 'number_of_periods': [1.0, ('Number of periods', '')]})}), 'beamline_elements_list': [[<syned.beamline.beamline_element.BeamlineElement object at 0x7070e6a1b3b0>, <syned.beamline.beamline_element.BeamlineElement object at 0x7070e90695b0>, <syned.beamline.beamline_element.BeamlineElement object at 0x7070e6be21e0>, <syned.beamline.beamline_element.BeamlineElement object at 0x7070e6a35df0>, <syned.beamline.beamline_element.BeamlineElement object at 0x7070e6a36fc0>, <syned.beamline.beamline_element.BeamlineElement object at 0x7070e6a35b20>, <syned.beamline.beamline_element.BeamlineElement object at 0x7070e6a34a70>, <syned.beamline.beamline_element.BeamlineElement object at 0x7070e6a36ff0>], ('Beamline Elements', '')]})
OrderedDict({'CLASS_NAME': 'Beamline', 'light_source': OrderedDict({'CLASS_NAME': 'LightSource', 'name': ['test', ('Name', '')], 'electron_beam': OrderedDict({'CLASS_NAME': 'ElectronBeam', 'energy_in_GeV': [6.0, ('Electron beam energy', 'GeV')], 'energy_spread': [0.0, ('Electron beam energy spread (relative)', '')], 'current': [0.2, ('Electron beam current', 'A')], 'number_of_bunches': [1, ('Number of bunches', '')], 'moment_xx': [0.0, ('Moment (spatial^2, horizontal)', 'm^2')], 'moment_xxp': [0.0, ('Moment (spatial-angular, horizontal)', 'm')], 'moment_xpxp': [0.0, ('Moment (angular^2, horizontal)', '')], 'moment_yy': [0.0, ('Moment (spatial^2, vertical)', 'm^2')], 'moment_yyp': [0.0, ('Moment (spatial-angular, vertical)', 'm')], 'moment_ypyp': [0.0, ('Moment (angular^2, vertical)', '')], 'dispersion_x': [0.0, ('Dispersion (horizontal)', '')], 'dispersion_y': [0.0, ('Dispersion (vertical)', '')], 'dispersionp_x': [0.0, ('Dispersion Derivative (horizontal)', '')], 'dispersionp_y': [0.0, ('Dispersion Derivative (vertical)', '')]}), 'magnetic_structure': OrderedDict({'CLASS_NAME': 'Undulator', 'K_vertical': [0.0, ('K value (vertical)', '')], 'K_horizontal': [0.0, ('K value (horizontal)', '')], 'period_length': [0.0, ('Period length', 'm')], 'number_of_periods': [1.0, ('Number of periods', '')]})}), 'beamline_elements_list': [[<syned.beamline.beamline_element.BeamlineElement object at 0x7070e6a1bd70>, <syned.beamline.beamline_element.BeamlineElement object at 0x7070e6a1bdd0>, <syned.beamline.beamline_element.BeamlineElement object at 0x7070e6a1b680>, <syned.beamline.beamline_element.BeamlineElement object at 0x7070e6a1bb90>, <syned.beamline.beamline_element.BeamlineElement object at 0x7070e6a1b7a0>, <syned.beamline.beamline_element.BeamlineElement object at 0x7070e6a36990>, <syned.beamline.beamline_element.BeamlineElement object at 0x7070e6a369c0>, <syned.beamline.beamline_element.BeamlineElement object at 0x7070e6a369f0>], ('Beamline Elements', '')]})