.. _python_disambiguation:

# Python

RiskScape allows **user defined** Python code to be used for risk modelling.
This allows RiskScape to be extended by scientists/risk modellers without
needing software developers.

.. note::
    To use packages like ``numpy`` or ``scipy`` in your Python function, you need
    to configure RiskScape to use :ref:`cpython-impl`.

There are three main ways to use Python with RiskScape:

1. :ref:`python-functions`: you can customize your risk-modelling by defining
your own RiskScape :ref:`functions` in Python, which can then be integrated with your model.
User-defined Python functions can be used from any RiskScape :ref:`expression<expressions>`,
the same as built-in RiskScape functions. Python functions will get called repeatedly
for *each* row of data.

    .. tip::
      If you're looking to integrate your own
      damage functions or vulnerability curves, then :ref:`python-functions` are most likely what you're after.

2. :ref:`python-post-processing` scripts: you can optionally run Python code at the end of your
model run. This can take your model results and create :ref:`custom model outputs<python-outputs>`, such as
maps, bar graphs, or even custom PDF files.

3. The :ref:`Python pipeline step <python-step>`: advanced users can pass _all_ the pipeline data to Python
for additional processing.
This can be useful for modelling dependencies, such as for [network models](https://en.wikipedia.org/wiki/Network_theory_in_risk_assessment),
where damage to one asset can affect other assets.

.. note::
    The ``python()`` step can run at *any* point in the model pipeline. It runs *before* the results
    files are saved, and can optionally pass the modified data back to the pipeline.
    Whereas the post-processing script only runs at the end, *after* the main results files have been saved.

## Python resources

.. toctree::
   :maxdepth: 2
   :glob:

   python-functions.md
   python-post-processing.md
   python-step.md
