|
| 1 | +# Scaling tasks |
| 2 | + |
| 3 | +In any bigger project you quickly come to the point where you stack multiple repetitions |
| 4 | +of tasks on top of each other. |
| 5 | + |
| 6 | +For example, you have one dataset, four different ways to prepare it, and three |
| 7 | +statistical models to analyze the data. The cartesian product of all steps combined |
| 8 | +comprises twelve differently fitted models. |
| 9 | + |
| 10 | +Here you find some tips on how to set up your tasks such that you can easily modify the |
| 11 | +cartesian product of steps. |
| 12 | + |
| 13 | +## Scalability |
| 14 | + |
| 15 | +Let us dive right into the aforementioned example. We start with one dataset `data.csv`. |
| 16 | +Then, we will create four different specifications of the data and, finally, fit three |
| 17 | +different models to each specification. |
| 18 | + |
| 19 | +This is the structure of the project. |
| 20 | + |
| 21 | +``` |
| 22 | +my_project |
| 23 | +├───pyproject.toml |
| 24 | +│ |
| 25 | +├───src |
| 26 | +│ └───my_project |
| 27 | +│ ├────config.py |
| 28 | +│ │ |
| 29 | +│ ├───data |
| 30 | +│ │ └────data.csv |
| 31 | +│ │ |
| 32 | +│ ├───data_preparation |
| 33 | +│ │ ├────__init__.py |
| 34 | +│ │ ├────config.py |
| 35 | +│ │ └────task_prepare_data.py |
| 36 | +│ │ |
| 37 | +│ └───estimation |
| 38 | +│ ├────__init__.py |
| 39 | +│ ├────config.py |
| 40 | +│ └────task_estimate_models.py |
| 41 | +│ |
| 42 | +│ |
| 43 | +├───setup.py |
| 44 | +│ |
| 45 | +├───.pytask.sqlite3 |
| 46 | +│ |
| 47 | +└───bld |
| 48 | +``` |
| 49 | + |
| 50 | +The folder structure, the main `config.py` which holds `SRC` and `BLD`, and the tasks |
| 51 | +follow the same structure advocated throughout the tutorials. |
| 52 | + |
| 53 | +New are the local configuration files in each subfolder of `my_project`, which contain |
| 54 | +objects shared across tasks. For example, `config.py` holds the paths to the processed |
| 55 | +data and the names of the data sets. |
| 56 | + |
| 57 | +```{literalinclude} ../../../docs_src/how_to_guides/bp_scaling_tasks_1.py |
| 58 | +``` |
| 59 | + |
| 60 | +The task file `task_prepare_data.py` uses these objects to build the repetitions. |
| 61 | + |
| 62 | +```{literalinclude} ../../../docs_src/how_to_guides/bp_scaling_tasks_2.py |
| 63 | +``` |
| 64 | + |
| 65 | +All arguments for the loop and the {func}`@task <pytask.task>` decorator are built |
| 66 | +within a function to keep the logic in one place and the module's namespace clean. |
| 67 | + |
| 68 | +Ids are used to make the task {ref}`ids <ids>` more descriptive and to simplify their |
| 69 | +selection with {ref}`expressions <expressions>`. Here is an example of the task ids with |
| 70 | +an explicit id. |
| 71 | + |
| 72 | +``` |
| 73 | +# With id |
| 74 | +.../my_project/data_preparation/task_prepare_data.py::task_prepare_data[data_0] |
| 75 | +``` |
| 76 | + |
| 77 | +Next, we move to the estimation to see how we can build another repetition on top. |
| 78 | + |
| 79 | +```{literalinclude} ../../../docs_src/how_to_guides/bp_scaling_tasks_3.py |
| 80 | +``` |
| 81 | + |
| 82 | +In the local configuration, we define `ESTIMATIONS` which combines the information on |
| 83 | +data and model. The dictionary's key can be used as a task id whenever the estimation is |
| 84 | +involved. It allows triggering all tasks related to one estimation - estimation, |
| 85 | +figures, tables - with one command. |
| 86 | + |
| 87 | +```console |
| 88 | +pytask -k linear_probability_data_0 |
| 89 | +``` |
| 90 | + |
| 91 | +And here is the task file. |
| 92 | + |
| 93 | +```{literalinclude} ../../../docs_src/how_to_guides/bp_scaling_tasks_4.py |
| 94 | +``` |
| 95 | + |
| 96 | +Replicating this pattern across a project allows a clean way to define repetitions. |
| 97 | + |
| 98 | +## Extending repetitions |
| 99 | + |
| 100 | +Some parametrized tasks are costly to run - costly in terms of computing power, memory, |
| 101 | +or time. Users often extend repetitions triggering all repetitions to be rerun. Thus, |
| 102 | +use the {func}`@pytask.mark.persist <pytask.mark.persist>` decorator, which is explained |
| 103 | +in more detail in this {doc}`tutorial <../tutorials/making_tasks_persist>`. |
0 commit comments