Warning, /acts/docs/examples/python_bindings.rst is written in an unsupported language. File is not indexed.
0001 .. _python-bindings:
0002
0003 Python bindings for the Examples
0004 ================================
0005
0006 The examples part of ACTS ships with python bindings using the ``pybind11``
0007 library. Building these bindings can be enabled via
0008 ``-DACTS_BUILD_EXAMPLES_PYTHON_BINDINGS=ON``, and requires a python installation
0009 including the development files to be installed. You can then build the special
0010 target `ActsPythonBindings` to build everything that can be accessed in python.
0011
0012 The build will create a setup script in ``$BUILD_DIR/python/setup.sh`` which
0013 modifies ``$PYTHONPATH`` so that you can import the ``acts`` module in python.
0014
0015 Here is a minimal example of a python script using the example bindings, which
0016 sets up the particle propagation and runs a few events.
0017
0018 .. code-block:: python
0019
0020 import os
0021
0022 import acts
0023 import acts.examples
0024
0025 detector = acts.examples.GenericDetector()
0026 trackingGeometry = detector.trackingGeometry()
0027 s = acts.examples.Sequencer(events=10)
0028
0029 rnd = acts.examples.RandomNumbers(seed=42)
0030
0031 nav = acts.Navigator(trackingGeometry=trackingGeometry)
0032
0033 field = acts.ConstantBField(acts.Vector3(0, 0, 2 * acts.UnitConstants.T))
0034 stepper = acts.EigenStepper(field)
0035
0036 prop = acts.examples.ConcretePropagator(acts.Propagator(stepper, nav))
0037
0038 alg = acts.examples.PropagationAlgorithm(
0039 propagatorImpl=prop,
0040 level=acts.logging.INFO,
0041 randomNumberSvc=rnd,
0042 ntests=1000,
0043 sterileLogger=False,
0044 outputSummaryCollection="propagation_summary",
0045 )
0046
0047 s.addAlgorithm(alg)
0048
0049 outputDir = "."
0050 objDir = outputDir + "/obj"
0051 if not os.path.exists(objDir):
0052 os.mkdir(objDir)
0053
0054 s.addWriter(
0055 acts.examples.ObjPropagationStepsWriter(
0056 level=acts.logging.INFO,
0057 collection="propagation_summary",
0058 outputDir=objDir,
0059 )
0060 )
0061
0062 s.addWriter(
0063 acts.examples.RootPropagationStepsWriter(
0064 level=acts.logging.INFO,
0065 collection="propagation_summary",
0066 filePath=outputDir + "/propagation_steps.root",
0067 )
0068 )
0069
0070 s.run()
0071
0072 Python based example scripts
0073 ----------------------------
0074
0075 The repository contains a set of example scripts that can be used to execute various workflows.
0076 They can be found in ``$REPO_ROOT/Examples/Scripts/Python``. Make sure you have run
0077
0078 .. code-block:: console
0079
0080 source $BUILD_DIR/python/setup.sh
0081
0082 to make sure python can find the ``acts`` module.
0083
0084 Python based unit tests
0085 -----------------------
0086
0087 A number of unit tests based on the ``pytest`` library are shipped with the
0088 repository. They are located under ``$REPO_ROOT/Examples/Python/tests``, and
0089 intend to cover the public API of the python bindings. A set of tests also
0090 executed the standalone example scripts.
0091
0092 To run these python based tests, ``pytest`` and a few other dependencies need
0093 to be installed. They can be installed via ``pip install -r
0094 Examples/Python/tests/requirements.txt`` from the repository root. You can
0095 then simply run ``pytest`` from the repository root.
0096
0097 .. tip::
0098 :name: python-virtualenv
0099
0100 It is **strongly recommended** to use a `virtual environment`_ for
0101 this purpose! For example, run
0102
0103 .. code-block:: console
0104
0105 $ python -m venv venv
0106 $ source venv/bin/activate
0107
0108 to create a local virtual environment, and then run the `pip` command above.
0109
0110
0111 .. _virtual environment: https://realpython.com/python-virtual-environments-a-primer/
0112
0113 .. _root_hash_checks:
0114
0115 ROOT file hash regression checks
0116 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
0117
0118 In a number of cases, the python based test suite will run hash based regression tests against ROOT files that are
0119 written by the test workloads. These tests use a custom hash algorithm written in python, which hashes each individual
0120 entry of each ``TTree`` found in a file. These entry hashes are then sorted, concatenated and hashed again for the final output.
0121 This procedure ensures that if the ROOT file content changes, the hash changes, while also giving the same hash when the events
0122 stored in the file are reordered.
0123
0124 The tests are implemented by looking up a reference hash from a central data file ``$REPO_ROOT/Examples/Python/tests/root_file_hashes.txt``
0125 that looks like
0126
0127 .. code-block:: none
0128
0129 test_ckf_tracks_example_full_seeding__performance_seeding_trees.root: 938bcc9b9425b12c620f5d0efa2c592817dfe92a18c309e97aa9d87412918620
0130 test_ckf_tracks_example_full_seeding__trackstates_ckf.root: 2faceafd4a521ff4030557301723e29c3d870edad052965eb644b824b57e2146
0131 test_ckf_tracks_example_truth_estimate__performance_seeding_trees.root: 5c0cf9e84af64e6814ab1ddf4cbaf4be6008ad8b2371b5b0241085b19d0fc52c
0132 test_ckf_tracks_example_truth_estimate__performance_seeding_trees.root: 5c0cf9e84af64e6814ab1ddf4cbaf4be6008ad8b2371b5b0241085b19d0fc52c
0133 test_ckf_tracks_example_truth_estimate__trackstates_ckf.root: ac4485c09a68fca3d056cb8d9adb81695e68d822629e48c71fd2b6d2bbd31f88
0134 # ...
0135
0136 where the left side before the ``:`` indicates the test in which the check is performed and the name of the ROOT file
0137 that is checked. The right side is the reference hash.
0138
0139 .. note:: The file from which reference hashes are loaded can be changed by setting the environment variable ``ROOT_HASH_FILE``
0140 to the desired file.
0141
0142 These checks have two purposes:
0143
0144 1. Detect regressions in the algorithms: if an algorithm produces different output, the test will catch it. This also means that
0145 if algorithmic changes are made that intentionally change the output, the reference hashes also have to be updated.
0146
0147 .. warning:: Please make sure to check the contents of a changed file are correct/reasonable before updating the reference hash!
0148
0149 2. Detect potential reproducibility issues. Tests that run with multiple threads should produce the same output every run,
0150 event ordering aside. If a test workload has a thread-reproducibility issue, the output hash should also change.
0151
0152 Running the hash checks locally and how to update the reference hashes
0153 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
0154
0155 By default, the hash checks are not executed when the ``pytest`` command is run. To enable them, you need to set the environment
0156 variable ``ROOT_HASH_CHECKS`` needs to be set to ``ON``, for example like:
0157
0158 .. code-block:: console
0159
0160 ROOT_HASH_CHECKS=ON pytest
0161
0162 If any hash mismatches are observed, the corresponding tests will fail, and ``pytest`` will print a summary at the end that looks like
0163
0164 .. code-block:: console
0165
0166 ------------------------------------------- RootHashAssertionErrors -----------------------------------------------------
0167 The ROOT files produced by tests have changed since the last recorded reference.
0168 This can be be expected if e.g. the underlying algorithm changed, or it can be a test failure symptom.
0169 Please manually check the output files listed below and make sure that their content is correct.
0170 If it is, you can update the test reference file Examples/Python/tests/root_file_hashes.txt with the new hashes below.
0171
0172 test_seeding__estimatedparams.root: 8bbc97cb3d4777c61dd0b483a1c8268fc8411ad182c35bc731e5ed222450deca
0173 test_material_recording__geant4_material_tracks.root: 019ce62ce378efa5c02a94768039686ed3cdfbd60c115c1f0cab2cbc53def57b
0174 test_material_mapping__material-maps_tracks.root: c03215e8b53733a3a7d7a0a5f9aec5bf2df20e8e40cc0492a8fa22400491d216
0175 test_material_mapping__propagation-material.root: a15a5c1e92fc3b848efb232eea1d40c422ee3a1d9ef1f7140294415621a04ce5
0176 test_ckf_tracks_example_full_seeding__tracksummary_ckf.root: 9e4d14169f20961be38d0305853a7cf7eeea4a647f0c94a48aada22c3c2c7a51
0177 test_ckf_tracks_example_truth_estimate__tracksummary_ckf.root: 3d56b26788163852e2c1f7288920f60a505bd14deeabb6f9189b680fcd90bfc5
0178 test_ckf_tracks_example_truth_smeared__tracksummary_ckf.root: ca2ce4069d2a2388c3d3c826dec8bea9f9d1e622239a20f8b985784d6c546c6e
0179 =========================================== short test summary info =====================================================
0180 FAILED Examples/Python/tests/test_examples.py::test_seeding
0181 FAILED Examples/Python/tests/test_examples.py::test_material_recording
0182 FAILED Examples/Python/tests/test_examples.py::test_material_mapping
0183 FAILED Examples/Python/tests/test_examples.py::test_ckf_tracks_example_full_seeding
0184 FAILED Examples/Python/tests/test_examples.py::test_ckf_tracks_example_truth_estimate
0185 FAILED Examples/Python/tests/test_examples.py::test_ckf_tracks_example_truth_smeared
0186 ================================== 6 failed, 183 passed in 199.82s (0:03:19) ============================================
0187
0188 Here, we see that 7 hash checks have failed. The error output conveniently has the same format as the reference hashes found in ``root_file_hashes.txt``.
0189 To update the reference hashes, simply replace the corresponding entries in ``root_file_hashes.txt`` with the output from the ``pytest`` run.
0190
0191 .. note:: The CI runs the ROOT hash checks. However, we have observed the hashes to change between different machines.
0192 This is believed to be due to differences in math libraries producing slightly different outputs. As a consequence,
0193 locally obtained file hashes might cause CI failures, as the CI hashes are different.
0194
0195 For local testing, it is therefore advisable to use ``ROOT_HASH_FILE`` to use a different file for the reference hashes
0196 and populated it with known-good reference hashes from the ``main`` branch, before testing your developments.
0197
0198 To make the CI succeed if it obtains different hashes than you get locally: make sure that the output is correct, and then
0199 update the central ``root_file_hashes.txt`` with the hashes reported in the failed CI job.