|
| 1 | +.. _case_i: |
| 2 | + |
| 3 | +I — Containerizing a Model with Docker |
| 4 | +====================================== |
| 5 | + |
| 6 | +**Goal.** Show how to containerize a forecasting model with **Docker** and run it inside |
| 7 | +the floatCSEP engine, producing catalog forecasts and evaluating them with an N-test. |
| 8 | +This case uses simple mock models as examples. |
| 9 | + |
| 10 | +.. warning:: |
| 11 | + |
| 12 | + **Docker is required** to containerize models and (optionally) to run in parallel. |
| 13 | + Please install Docker and complete the Linux post-installation steps if applicable. |
| 14 | + See :ref:`docker-install` in the Installation guide. |
| 15 | + |
| 16 | +.. admonition:: **TL; DR** |
| 17 | + |
| 18 | + In a terminal, navigate to ``floatcsep/tutorials/case_i`` and type: |
| 19 | + |
| 20 | + .. code-block:: console |
| 21 | +
|
| 22 | + $ floatcsep run config.yml |
| 23 | +
|
| 24 | + After the calculation is complete, the results will be summarized in ``results/report.md``. |
| 25 | + For troubleshooting, run the experiment with: |
| 26 | + |
| 27 | + .. code-block:: console |
| 28 | +
|
| 29 | + $ floatcsep run config.yml --debug |
| 30 | +
|
| 31 | +.. currentmodule:: floatcsep |
| 32 | + |
| 33 | +.. contents:: Contents |
| 34 | + :local: |
| 35 | + |
| 36 | + |
| 37 | + |
| 38 | +Experiment layout |
| 39 | +----------------- |
| 40 | + |
| 41 | +The experiment input files are: |
| 42 | + |
| 43 | +:: |
| 44 | + |
| 45 | + case_i |
| 46 | + └── pymock |
| 47 | + └── pymock # src code |
| 48 | + | ... |
| 49 | + ├── Dockerfile # Docker image build instructions |
| 50 | + └── setup.cfg # Python build configuration |
| 51 | + └── pymock_slow # Same as pymock, but slower (to test cpu and DAG usage) |
| 52 | + | ... |
| 53 | + ├── catalog.csep |
| 54 | + ├── config.yml |
| 55 | + ├── tests.yml |
| 56 | + └── models.yml |
| 57 | + |
| 58 | + |
| 59 | + |
| 60 | +Configuration |
| 61 | +------------- |
| 62 | + |
| 63 | +``config.yml`` |
| 64 | +^^^^^^^^^^^^^^ |
| 65 | + |
| 66 | +.. code-block:: yaml |
| 67 | +
|
| 68 | + name: case_i |
| 69 | +
|
| 70 | + time_config: |
| 71 | + start_date: 2012-5-23T00:00:00 |
| 72 | + end_date: 2012-8-23T00:00:00 |
| 73 | + horizon: 7days |
| 74 | + exp_class: td |
| 75 | +
|
| 76 | + region_config: |
| 77 | + region: italy_csep_region |
| 78 | + mag_min: 3.5 |
| 79 | + mag_max: 8.0 |
| 80 | + mag_bin: 0.5 |
| 81 | + depth_min: 0 |
| 82 | + depth_max: 70 |
| 83 | +
|
| 84 | + run_mode: parallel |
| 85 | + force_rerun: True |
| 86 | + catalog: catalog.csv |
| 87 | + model_config: models.yml |
| 88 | + test_config: tests.yml |
| 89 | +
|
| 90 | + postprocess: |
| 91 | + plot_forecasts: false |
| 92 | +
|
| 93 | +**Notes** |
| 94 | + |
| 95 | +- ``exp_class: td`` declares a time-dependent experiment with rolling windows of length ``horizon``. |
| 96 | +- ``run_mode: parallel`` enables parallel solving (if resources allow). |
| 97 | +- ``force_rerun: True`` forces regeneration of forecasts even if files exist. |
| 98 | + |
| 99 | + |
| 100 | +``models.yml`` |
| 101 | +^^^^^^^^^^^^^^ |
| 102 | + |
| 103 | +.. code-block:: yaml |
| 104 | +
|
| 105 | + - pymock: |
| 106 | + path: pymock |
| 107 | + func: pymock |
| 108 | + func_kwargs: |
| 109 | + n_sims: 1000 |
| 110 | + mag_min: 3.5 |
| 111 | + build: docker |
| 112 | +
|
| 113 | + - pymock_slow: |
| 114 | + path: pymock_slow |
| 115 | + func: pymock |
| 116 | + prefix: pymock |
| 117 | + func_kwargs: |
| 118 | + n_sims: 1000 |
| 119 | + mag_min: 3.5 |
| 120 | + build: docker |
| 121 | +
|
| 122 | +**Notes** |
| 123 | + |
| 124 | +- ``build: docker`` tells floatCSEP to **build a Docker image** for the model located at ``path``. |
| 125 | +- ``func`` is the entry-point used **inside** the container to run the forecasts (defined in ``setup.cfg``) |
| 126 | +- ``func_kwargs`` are passed to ``model/input/args`` **inside** the container. They are stored and can be visualized in ``results/{time_window/input/{model}``. |
| 127 | +- You can add the options ``force_build`` to rebuild the Docker image. |
| 128 | + |
| 129 | + |
| 130 | +How containerization works here |
| 131 | +------------------------------- |
| 132 | + |
| 133 | +- For each model block marked ``build: docker``, floatCSEP: |
| 134 | + |
| 135 | + 1. Builds a Docker image from the model directory at ``path`` (expects a valid Dockerfile and the model’s code/config there). |
| 136 | + 2. Runs the container, mounting standard I/O folders used by the model (e.g., an ``input/`` and a model ``forecasts/`` directory), and executes your model’s entry-point (``func``). |
| 137 | + 3. Collects the produced forecast files and proceeds with evaluations. |
| 138 | + |
| 139 | + |
| 140 | +**Checklist to Dockerize your own model** |
| 141 | + |
| 142 | +- Add a **Dockerfile** in your model folder (``path``). |
| 143 | +- Ensure your entry-point can be invoked by the engine (e.g., a Python module or script that maps to ``func`` and accepts ``func_kwargs``). |
| 144 | +- The model should be able to read input data (catalog and args) from ``{model_basedir}/input`` |
| 145 | +- Write outputs to the expected location (e.g., a ``{model_basedir}/forecasts/`` directory). |
| 146 | +- (Optional): Avoid writing to root-owned paths inside the container; keep everything under the mounted work basedir. |
| 147 | +- Test your image locally with a dry run: |
| 148 | + |
| 149 | + |
| 150 | +from the ``{model_basedir}`` |
| 151 | + |
| 152 | + .. code-block:: console |
| 153 | +
|
| 154 | + $ docker build \ |
| 155 | + --build-arg USERNAME=$USER \ |
| 156 | + --build-arg USER_UID=$(id -u) \ |
| 157 | + --build-arg USER_GID=$(id -g) \ |
| 158 | + -t {model_name} . |
| 159 | +
|
| 160 | + $ docker run --rm --volume $PWD:/usr/src/pymock:rw model_pymock python run.py input/args.txt |
| 161 | +
|
| 162 | +
|
| 163 | +What happens under the hood |
| 164 | +--------------------------- |
| 165 | + |
| 166 | +1. Controller parses rolling time windows from ``start_date`` to ``end_date`` with a 7-day horizon. |
| 167 | +2. For each Dockerized model (``pymock``, ``pymock_slow``), the controller builds an image and launches |
| 168 | + containers with the appropriate inputs/arguments. |
| 169 | +3. Forecasts are written to each model’s ``forecasts/`` directory (or central output). |
| 170 | +4. The **Catalog N-test** runs on the collected forecasts and generates diagnostic plots. |
| 171 | + |
| 172 | + |
| 173 | +Outputs |
| 174 | +------- |
| 175 | + |
| 176 | +You should find: |
| 177 | + |
| 178 | +- Weekly gridded forecasts in each model’s ``forecasts/`` folder. |
| 179 | +- N-test results (tables/JSON) in ``results/{time_window}/evaluations``. |
| 180 | +- Markdown and PDF reports summarizing the experiment results in ``results/report.md``. |
| 181 | + |
| 182 | + |
| 183 | +Running the experiment |
| 184 | +---------------------- |
| 185 | + |
| 186 | +From the ``tutorials/case_i`` folder, run: |
| 187 | + |
| 188 | +.. code-block:: console |
| 189 | +
|
| 190 | + $ floatcsep run config.yml |
| 191 | +
|
| 192 | +This will build the Docker images (if needed), run the containers for each time window and model, |
| 193 | +perform the evaluations, and create a summarized report in ``results/report.md``. |
| 194 | + |
| 195 | + |
| 196 | +Troubleshooting |
| 197 | +--------------- |
| 198 | + |
| 199 | +- Run in debug mode with ``floatcsep run config.yml --debug``, or add ``--log`` to output a log file ``results/log`` |
| 200 | +- **Docker not found / permission denied**: |
| 201 | + - Ensure Docker is installed and your user can run containers (Linux: add to ``docker`` group). |
| 202 | + - See :ref:`docker-install`. |
| 203 | +- **Model image fails to build**: |
| 204 | + - Check a clean installation (without Docker) and running using its entry-point. |
| 205 | + - Check the Dockerfile, base image, and that all runtime deps are installed in the image. |
| 206 | + - Try building manually with ``docker build`` for clearer error messages. |
| 207 | + - Adapt one of the provided Dockerfiles in the model folders. |
| 208 | +- **No forecasts produced**: |
| 209 | + - Confirm your model writes to the expected ``forecasts/`` directory. |
| 210 | + - Verify that the code actually reads from ``input/args.txt`` (or ``.yml`` or ``.json``). |
| 211 | +- **Plots not generated**: |
| 212 | + - Inspect logs under ``results/`` for tracebacks. |
| 213 | + |
0 commit comments