Paramcontainer

from fluiddyn.util.paramcontainer import ParamContainer

Let’s consider code taken from fluidimage. The object containing the parameter is initialized in the package. It is first created empty:

params = ParamContainer(tag='params')

We then fill it with default parameters:

# taken from fluidimage.work.piv.singlepass
params._set_child('piv0', attribs={
        'shape_crop_im0': 48,
        'shape_crop_im1': None,
        'displacement_max': None})

params.piv0._set_doc("""Parameters describing one PIV step.""")

params.piv0._set_child('grid', attribs={
    'overlap': 0.5,
    'from': 'overlap'})

params.piv0.grid._set_doc("""
Parameters describing the grid.

overlap : float (0.5)
    Number smaller than 1 defining the overlap between interrogation windows.

from : str {'overlap'}
    Keyword for the method from which is computed the grid.
""")

There are other functions to add attribute to a child:

params.piv0._set_attrib
params.piv0._set_attribs
<bound method ParamContainer._set_attribs of <fluiddyn.util.paramcontainer.ParamContainer object at 0x7fb3ee137250>

<piv0 displacement_max="None" shape_crop_im0="48" shape_crop_im1="None">
  <grid from="overlap" overlap="0.5"/>  

</piv0>
>

The ParamContainer object can be used in the code to generate the documentation, as for example in this page.

Then the user has to modify the default parameters in a python script. She/he can first create the object in ipython and play with it. The representation of the object shows the parameters and their values:

params.piv0
  • "piv0 of at 0x7fb3ee137250":
    {
    • "displacement_max": null,
    • "shape_crop_im0": 48,
    • "shape_crop_im1": null,
    • "grid":
      {
      • "from": "overlap",
      • "overlap": 0.5
      }
    }

It is also easy to print the documentation (or part of the documentation):

params.piv0._print_doc()
Documentation for params.piv0
-----------------------------

Parameters describing one PIV step.
params.piv0._print_docs()
Documentation for params.piv0
-----------------------------

Parameters describing one PIV step.

Documentation for params.piv0.grid
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Parameters describing the grid.

overlap : float (0.5)
    Number smaller than 1 defining the overlap between interrogation windows.

from : str {'overlap'}
    Keyword for the method from which is computed the grid.
params.piv0.grid._print_docs()
Documentation for params.piv0.grid
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Parameters describing the grid.

overlap : float (0.5)
    Number smaller than 1 defining the overlap between interrogation windows.

from : str {'overlap'}
    Keyword for the method from which is computed the grid.

Let’s get an example of code to modify the parameters.

params.piv0._print_as_code()
piv0.displacement_max = None
piv0.shape_crop_im0 = 48
piv0.shape_crop_im1 = None
piv0.grid.from = "overlap"
piv0.grid.overlap = 0.5

Modifying a value is as simple as

params.piv0.grid.overlap = 0.2
params.piv0.grid
  • "grid of at 0x7fb3ee1361d0":
    {
    • "from": "overlap",
    • "overlap": 0.2
    }

A spelling mistake is clearly annonced by a AttributeError:

try:
    params.piv0.grid.overlqp = 0.2
except AttributeError as e:
    print(e)
overlqp is not already set in grid.
The attributes are: ['from', 'overlap']
To set a new attribute, use _set_attrib or _set_attribs.