Comments on Scientific Python Programming
This is a large collection of personal reflections after using Python on various academic and scientific related tasks and missions.
Programming Tricks
Reverse English Naming
In scientific context, data often goes through various stages of processing, and a name pointing to some data often need lots of modifiers.
In English, a basic sentence is Subject-Verb-Noun and naturally lots of people naming things in Verb-Noun format. Similarly, an adjective phrase is Adjective-Noun and naturally people name things this way.
However, a Verb/Adjective can act upon / modify many things, but a Noun only have limited combination with Verb/Adjective. So, write function or variable names as noun_verb, noun_adjective or main_object_modifiers.
For a datum x we may have functions or variables like x_original, x_calculate, x_obtain, x_unit_converted etc. You can add many modifiers like x_when_y_is_10_converge_step2.
It is much easier to use the auto-complete feature that can be found in almost all modern editors.
Tooling
Python itself and dependencies
The bare minimum is always uses a virtual environment venv.
To avoid messing up the global Python, you may set up a global venv in your home directory like ~/PyVenv and always active the environment when using Python.
For Windows, the best way to install python is using uv python install. The wsl is often got recommended, but unless a project is multi-languages or assumes Linux environment, wsl only creates extra layer of complexity.
The packaging of Python is a holy mess beyond comprehension, however, pyproject.toml and uv makes life easier.
Code Cells / Code Dividers
Jupyter is good but the notebook may not suitable for code repository. One could conver the notebook to .py file but there is an easy alternative: code dividers which is a convention originated form Spyder.
Instead of having a full-blown notebook format .ipynb, just using #%% in .py file to divide sections. jupytext may help this process.
Libraries
Numpy & SciPy
NumPy is pretty much the central of the entire scientific python ecosystem.
Effective utilization of NumPy requires lots of training because both vectorization and array manipulation require developing some intuition.
Graphics
matplotlib or simply plt.
DO NOT use the implicit pyplot interface that really shouldn't exist. Do uses the explict Axes interface. Just initialize with fig, ax = plt.subplots() and then manipulate the plot with fig and ax.
For large quantity of data, Altair is much more performant.
Data Frames
Traditionally, pandas is popular, but it is not good as according to its own creator https://wesmckinney.com/blog/apache-arrow-pandas-internals/. The primary problems are immature API, large memory usage, lacking of lazy computation.
polars is currently the best alternative. It is written in a single language Rust.
ibis is good too, but it maybe limited to support too many backends.
Alternatively, one may change the programming model to SQL by using duckdb.
pivot / unpivot
pivot/unpivot is a very important operation for lots of statistical analysis tasks, but they are often overlooked, because its importance is not immediate obvious and the things they do can often be achieved in a more cumbersome way.
Note that Excel also have an identical feature.
SymPy
SymPy might adopt this C++ library for performance https://github.com/symengine/symengine.
Also see "Towards a new SymPy" https://oscarbenjamin.github.io/blog/czi/index.html
Graph
NetworkX
Probability and Statistics
PyMC Bayesian models.
Elementary Machine Learning
scikit-learn