Davide Gerosa - [email protected]
University of Milano-Bicocca, 2023-2024.
The python programming language and its library ecosystem are essential tools in modern science. This class provides an advanced introduction to python and its main functionalities, focusing in particular on its applications to computational physics. Targeted topics include: array vectorization with numpy, pretty plotting with matplotlib, scientific recipes with scipy, just-in-time compilating with numba, module packaging, and unit testing. I will also introduce other essential computational tools, notably Mathematica for symbolic manipulation and git for version control. The format will be highly interactive and tailored to the research interests of the participants.
- Introduction: python. Why python. Basics. Datatypes. Control flow. Functions. Classes. Modules. Exceptions. File I/O.
- Arrays: numpy. Array. Shaping. Slicing. Masking. Avoid loops.
- Plotting: matplotlib. Pretty plotting. OO interface. Latex compatibility. Lines. Histograms. Contour maps. Annotations. Surfaces.
- Numerical methods: scipy. Integration. Interpolation. Root finding. Initial value problems. Boundary value problems. Fitting. Fourier transforms. Linear algebra.
- Symbolic maths: mathematica Simplification. Solvers. Calculus. Differential Equations
- Version control: git. Why. Git walkthrough. SSH keys. Github.
- Go faster: numba and multiprocessing. Python extensions. Decorators. Just-in-time compilation. Notions of parallel computing. Embarrassingly parallel tasks.
- Code development: pytest, pip, cprofile. Module setup. Module deployment. Unit tests. Automated tests. Benchmark tests. Profiling.
Other (not covered in class):
- Symbolic maths: sympy. Symbols. Substitution. Simplification. Calculus. Solvers. Units.
- Data manipulation: pandas. When I find the time I would like to prepare something about pandas.
Each lecture has some exercises at the end.
There are infinitely many textbooks on scientific computing. Here are three that I think are particularly useful.
-
This textbook provides a gentle introduction to the beautiful world of python; it's a great starting point.
- "Learning Scientific Programming with Python", C. Hill, Cambridge University Press, 2020. Supporting code.
-
This one is more advanced. It's ideal for sharpening your existing Python skills and go the extra mile. Instead of keep on coding the same way, as some point you'll need to do it better. It's one of my go-to references when looking for a specific package/topic/task.
- "Scientific Computing with Python: High-performance scientific computing with NumPy, SciPy, and pandas", C. Fuhrer, O. Verdier, J. E., Packt Publishing, 2021. Supporting code.
-
Perhaps a bit outdated, but the really nice thing about this textbook is that has a bit of everything you might need for science, not just python. Things like command line operations, data visualization, regular expressions, version control, debugging, latex, etc). This book is a perfect companion when starting your PhD.
- "Effective Computation in Physics", A. Scopatz, K. D. Huff, O'Reilly Media, 2015.
Here are some classes similar to this one which you might find useful:
- "Scientific Computing with Python" University of Chicago, USA.
- "Python for Scientific Computing", Stony Brook University, USA
- "Python Programming for Scientists", University of Heidelberg, Germany.
- "Python for Scientific Computing", Aalto University, Finland.
- "Lectures on scientific computing with Python", R. Johansson et al.
- "Python for Astronomers", University of Bonn, Germany.
- "Google's python class", Google.
For students at Milano-Bicocca, recordings are available at elearning.unimib.it/course/view.php?id=53206
Here is our timetable:
- Tue Nov 21, 2023 - 10.30-12.30 - Room U2.06
- Mon Nov 27, 2023 - 10.30-12.30 - Room U4.06
- Tue Nov 28, 2023 - 10.30-12.30 - Room U2.06
- Mon Dec 4, 2023 - 10.30-12.30 - Room U4.06
- Tue Dec 5, 2023 - 10.30-12.30 - Room U4.07
- Mon Jan 8, 2024 - 10.30-12.30 - Room U4.06
- Tue Jan 9, 2024- 10.30-12.30 - Room U4.06
- Mon Jan 15, 2024 - 10.30-12.30 - Room U2.04
- Tue Jan 16, 2024 - 10.30-12.30 - Room U4.06 (backup slot in case we need to skip one lecture)
- Please come to class with your laptop.
- Make sure to have a working python distribution already installed and fully working (with privileges to
pip install
packages). If you've never installed python, I recommend using brew on macOs and anaconda on Windows. - Also, install Mathematica (this is free for Milano-Bicocca PhD students, see here, otherwise you can get a 30-day free trial).
For students enrolled in the Physics and Astronomy PhD at Milano-Bicocca, this class awards 2 CFU (16 hours). Exams will be light touch and without grades (i.e. just approved / not approved).
Each notebook has some exercises at the end. Note that the exercises provided are far more than what you'll need to do! Overall, it's ok if you work on 2 exercises for each lecture (see the notebooks). You can choose what you prefer based on your interests and what you think will be more useful for your PhD research. You'll see the exercises are very different: some of them are long, some are short, some have defined tasks, and some are more open-ended. If in doubt, feel free to shoot me an email with "I'd like to prepare these ones; is that ok?".
That should be a total of 2 x 8 = 16 exercises. Hopefully you will have finished many of them during the lectures themselves. To submit your exercises, please fork this repository and add your codes to the working
directory of your fork. Please send me an email with your github username to let me know you are done (do not send me code by email! Use git!). I will mark them in batches (about once a month) and communicate the outcome to the PhD secretary.
In general, I'm always happy to chat coding and science with you. My office is room U2-2007.
This class is built on top of that taught by Michael Zingale at Stony Brook University: https://sbu-python-class.github.io with changes and additions from a number of other sources.
Credit: xkcd 2582. This actually a Python easter egg... You can truly pip install antigravity
and import antigravity
. Try!