JSON/YAML Config Files

JSON/YAML Config Files#

Loading the parameter tree from a JSON/YAML file and running the reconstruction.

Some users might prefer to specify the parameter tree as a JSON or YAML configuration file. The same parameter tree from the previous example, looks like this in JSON format:

{
    "verbose_level": "interactive",
    "io": {
        "rfile": null,
        "autosave": {
            "active": false
        },
        "autoplot": {
            "active": false
        },
        "interaction": {
            "active": false
        }
    },
    "scans": {
        "MF": {
            "name": "Full",
            "data": {
                "name": "MoonFlowerScan",
                "shape": 128
                "num_frames": 200,
                "save": null,
                "density": 0.2,
                "photons": 1e8,
                "psf": 0.0,

            }
        }
    },
    "engines": {
        "engine00": {
            "name": "DM",
            "numiter": 80
        }
    }
}

and like this in the slighty more compact YAML format:

verbose_level: "interactive"
io:
  rfile: null
  autosave:
    active: false
  autoplot:
    active: False
  interaction:
    active: false
scans:
  MF:
    name: "Full"
    data:
      name: "MoonFlowerScan"
      shape: 128
      num_frames: 200
      save: null
      density: 0.2
      photons: 1.0e+08
      psf: 0.
engines:
  engine00:
    name: "DM"
    numiter: 80

If we save these JSON/YAML config files as config/moonflower.json and config/moonflower.yaml, respectively (you should already have these files in the same folder as this notebook), we can simply import the parameter tree from a JSON file into our script/notebook like this

import ptypy
import ptypy.utils as u

# Parameter tree
p = u.param_from_json("config/moonflower.json")

# Prepare and run
P = ptypy.core.Ptycho(p,level=5)

# Display the results
ptypy.utils.plot_client.figure_from_ptycho(P)

and the same from a YAML file

import ptypy
import ptypy.utils as u

# Parameter tree
p = u.param_from_yaml("config/moonflower.yaml")

# Prepare and run
P = ptypy.core.Ptycho(p,level=5)

# Display the results
ptypy.utils.plot_client.figure_from_ptycho(P)

Challenge

Open the JSON/YAML config file, modify the DM numiter to 100 and run the MoonFlower example below. To modify the config files in JupyterLab right click on the JSON/YAML file in the browser on the left and then -> Open With -> Editor.


import ptypy
import ptypy.utils as u

# Parameter tree
p = u.param_from_json("config/moonflower.json")

# Prepare and run
P = ptypy.core.Ptycho(p,level=5)

# Display the results
fig = ptypy.utils.plot_client.figure_from_ptycho(P)
import ptypy
import ptypy.utils as u

# Parameter tree
p = u.param_from_yaml("config/moonflower.yaml")

# Prepare and run
P = ptypy.core.Ptycho(p,level=5)

# Display the results
fig = ptypy.utils.plot_client.figure_from_ptycho(P)