Coding Practices¶
Writing code for research is different from writing code for a course: there is no expected output to check against, the same script needs to run again in six months, and a collaborator may need to understand what you did without asking you. The tools and habits that make this work well are the focus of this track.
The first seven lessons are practical: you will set up your tools, learn enough Python to run physics calculations, and get familiar with the packages used throughout the research group. The final five lessons cover the habits that make code maintainable and reproducible over time.
Prometheus, a neutrino telescope simulation used in IceCube and KM3NeT analyses, is used as a running example throughout. It is a real production codebase, so it has both things done well and things that could be better. Both are worth learning from.
Lessons¶
Foundations¶
- Lesson 01: IDE Setup: Choosing and configuring a code editor; VSCode walkthrough and essential extensions.
- Lesson 02: Python & Jupyter Notebooks: Core Python and working interactively with notebooks.
- Lesson 03: Package Management: Installing packages with pip and isolating projects with virtual environments.
- Lesson 04: Scientific Python Stack: NumPy, SciPy, Pandas, and the libraries you will use every day.
- Lesson 05: Code Style & Quality: PEP 8, ruff, and type hints, introduced as a habit early rather than an afterthought.
- Lesson 06: Performance & Parallelism: Vectorisation, multiprocessing, Numba, and JAX.
- Lesson 07: ML Basics with PyTorch: Tensors, neural networks, the training loop, and where ML fits in physics research.
Best Practices¶
- Lesson 08: Project Layout: How to organise files and modules in a Python project.
- Lesson 09: Documentation: Writing NumPy-style docstrings and building a docs site with MkDocs.
- Lesson 10: Testing: Writing unit tests with pytest and understanding what to test.
- Lesson 11: Packaging: Using
pyproject.tomland pip to make a project installable. - Lesson 12: Linting & Automation: pre-commit hooks, GitHub Actions CI, and automated documentation deployment.
Examples¶
See the examples/coding/
folder for a worked example of a modular Python project with docstrings, tests, and packaging.
The examples/coding/requirements.txt lists every package needed to run the notebooks and
scripts. Install everything with:
pip install -r examples/coding/requirements.txt
pip install -e examples/coding/neutrino_flux/
For the PyTorch notebook (Lesson 07), install the CPU-only wheel if you do not have a GPU:
pip install torch --index-url https://download.pytorch.org/whl/cpu