.. _examples_modelling_cbed_distortion_estimation_generate_ml_datasets_for_ml_model_test_set_1_sec:

Generating machine learning datasets for the machine learning model test set #1
===============================================================================

In this example, we perform the "action" of generating 3 machine learning (ML)
datasets that can be used test ML models for distortion estimation in convergent
beam electron diffraction (CBED) patterns.

NOTE: Users are advised to read the remainder of the current page in its
entirety before trying to execute this action.

To execute the action, first we need to change into the directory
``<root>/examples/modelling/cbed/distortion/estimation/scripts``, where
``<root>`` is the root of the ``emicroml`` repository. Then, we need to run the
Python script ``./execute_action.py`` via the terminal command::

  python execute_action.py --action=<action> --use_slurm=<use_slurm>

where ``<action>`` must be equal to
``generate_ml_datasets_for_ml_model_test_set_1``, and ``<use_slurm>`` is either
``yes`` or ``no``. If ``<use_slurm>`` equals ``yes`` and a SLURM workload
manager is available on the server from which you intend to run the script, then
the action will be performed as multiple SLURM jobs. If ``<use_slurm>`` is equal
to ``no``, then the action will be performed locally without using a SLURM
workload manager.

If the action is to be performed locally without using a SLURM workload manager,
then prior to executing the above Python script, a set of Python libraries need
to be installed in the Python environment within which said Python script is to
be executed. See :ref:`this page
<examples_prerequisites_for_execution_without_slurm_sec>` for instructions on
how to do so. If the action is being performed as multiple SLURM jobs, then
prior to executing any Python commands that do not belong to Python's standard
library, a customizable sequence of commands are executed that are expected to
try to either activate an existing Python virtual environment, or create then
activate one, in which the Python libraries needed to complete the action
successfully are installed. See :ref:`this page
<examples_prerequisites_for_execution_with_slurm_sec>` for instructions how to
customize the sequence of commands.

The action described at the beginning of the current page takes automatically as
input data output data generated by the action described in the page
:ref:`examples_modelling_cbed_simulations_MoS2_on_amorphous_C_generate_cbed_pattern_sets_sec`,
hence one must execute the latter action first, prior to the former. Upon
successful completion of the former action, for every string ``<disk_size>`` in
the sequence ``(small, medium, large)``, an ML dataset is stored in the HDF5
file at the file path
``<root>/examples/modelling/cbed/distortion/estimation/data/ml_datasets/ml_datasets_for_ml_model_test_set_1/ml_datasets_with_cbed_patterns_of_MoS2_on_amorphous_C/ml_datasets_with_<disk_size>_sized_disks/ml_dataset_0.h5``,
where the ML dataset contains 2880 ML data instances, with each ML data instance
storing a :math:`512 \times 512` "fake" CBED pattern obtained by randomly
distorting the same undistorted CBED pattern with ``<disk_size>``-sized CBED
disks. For every string ``<disk_size>`` in the sequence ``(small, medium,
large)``, the undistorted CBED pattern with ``<disk_size>``-sized CBED disks is
the final CBED pattern of a CBED experiment simulated via the multislice
technique, where the sample is a 5-layer :math:`\text{MoS}_2` thin film with a
:math:`0.5 \ \text{nm}` thick layer of amorphous carbon (C). By small-, medium-,
and large-sized CBED disks, we mean CBED disks with radii equal roughly to 1/35,
(1/35+1/10)/2, and 1/10 in units of the image width, respectively. **Be advised
that each file storing an ML dataset is approximately 3.6 GB in size. Hence, in
total, the output resulting from the action described at the beginning of the
current page is approximately 11 GB of data**.

In executing the action described at the beginning of the current page, multiple
scripts are executed. The particular scripts that are executed depend on the
command line arguments of the parent Python script introduced at the beginning
of this page. If ``<use_slurm>`` equals ``yes``, then the following scripts are
executed in the order that they appear directly below:

:download:`<root>/examples/modelling/cbed/distortion/estimation/scripts/execute_action.py <../../../../../../examples/modelling/cbed/distortion/estimation/scripts/execute_action.py>`
:download:`<root>/examples/modelling/cbed/common/scripts/generate_ml_datasets_for_ml_model_test_set_1/execute_all_action_steps.py <../../../../../../examples/modelling/cbed/common/scripts/generate_ml_datasets_for_ml_model_test_set_1/execute_all_action_steps.py>`
:download:`<root>/examples/modelling/cbed/common/scripts/generate_ml_dataset_for_ml_model_test_set_1/execute_all_action_steps.py <../../../../../../examples/modelling/cbed/common/scripts/generate_ml_dataset_for_ml_model_test_set_1/execute_all_action_steps.py>`
:download:`<root>/examples/modelling/cbed/common/scripts/generate_ml_dataset_for_ml_model_test_set_1/prepare_and_submit_slurm_job.sh <../../../../../../examples/modelling/cbed/common/scripts/generate_ml_dataset_for_ml_model_test_set_1/prepare_and_submit_slurm_job.sh>`
:download:`<root>/examples/modelling/cbed/common/scripts/generate_ml_dataset_for_ml_model_test_set_1/execute_main_action_steps.py <../../../../../../examples/modelling/cbed/common/scripts/generate_ml_dataset_for_ml_model_test_set_1/execute_main_action_steps.py>`

Otherwise, if ``<use_slurm>`` equals ``no``, then the fourth script, i.e. the
one with the basename ``prepare_and_submit_slurm_job.sh`` is not executed. See
the contents of the scripts listed above for implementation details. The last
script uses the module :mod:`emicroml.modelling.cbed.distortion.estimation`. It
is recommended that you consult the documentation of said module as you explore
said script. Lastly, if the action is being performed as multiple SLURM jobs,
then the default ``sbatch`` options, which are specified in the file with the
basename ``prepare_and_submit_slurm_job.sh``, can be overridden by following the
instructions in :ref:`this page <examples_overriding_sbatch_options_sec>`.
