
# Python for Scientific Computing
### November 22-25, 2022
## General information
- Twitch link: https://www.twitch.tv/coderefinery
- Main hackMD: https://notes.coderefinery.org/python2022
- This hackMD: https://hackmd.io/@enccs/python-for-scicomp-nov2022
- Lesson material: https://aaltoscicomp.github.io/python-for-scicomp/
- Zoom: [Link](https://rise.zoom.us/j/66825623428?pwd=SFI4Y1ZUbGNoTEgrekN5ampOdGtkQT09)
- Meeting ID: 668 2562 3428
- Passcode: 328561
- Workshop feedback form: TBD
### Follow ENCCS
- ENCCS: https://enccs.se/
- Sign up for the ENCCS newsletter: https://enccs.se/newsletter
- Follow us on [LinkedIn](https://www.linkedin.com/company/enccs), or [Twitter](https://twitter.com/EuroCC_Sweden)
## Helpers
- Diana, CR, SNIC-UPPMAX - teaching jupyter and helper remaining times
- Björn C, SNIC-UPPMAX - helper during all days
- Matias P, SNIC-UPPMAX - helper on 23rd, 25th
- Thor W, ENCCS - teaching pandas and parallel, helper remaining times except 23rd
- Qiang Li, ENCCS - helper all days
- Johan H, SNIC-PDC - teaching numpy and matplotlib, helper remaining times
### Schedule
**Tue 22 Nov, 08:45-12:00 CET**
| Time | Section |
| ---- | --- |
| 08:45-09:00 | Local Zoom intro |
| 09:00-09:15 | [Workshop intro](https://scicomp.aalto.fi/training/scip/python-for-scicomp/intro/) |
| 09:15-10:00 | [Jupyter](https://aaltoscicomp.github.io/python-for-scicomp/jupyter/) |
| 10:00-11:10 | [numpy](https://aaltoscicomp.github.io/python-for-scicomp/numpy/) |
| 11:10-12:00 | [pandas](https://aaltoscicomp.github.io/python-for-scicomp/pandas/) |
**Wed 23 Nov, 09:00-12:00 CET**
| Time | Section |
| ---- | --- |
| 09:00-09:30 | [pandas](https://aaltoscicomp.github.io/python-for-scicomp/pandas/) continued |
| 09:30-11:10 | [matplotlib](https://aaltoscicomp.github.io/python-for-scicomp/data-visualization/) |
| 11:10-12:00 | [data formats](https://aaltoscicomp.github.io/python-for-scicomp/data-formats/) |
**Thu 24 Nov, 09:00-12:00 CET**
| Time | Section |
| ---- | --- |
| 09:00-10:10 | [scripts](https://aaltoscicomp.github.io/python-for-scicomp/scripts/) |
| 10:10-10:30 | [scipy](https://aaltoscicomp.github.io/python-for-scicomp/scipy/) |
| 10:30-11:10 | [library ecosystem](https://aaltoscicomp.github.io/python-for-scicomp/libraries/) |
| 11:10-12:00 | [parallel](https://aaltoscicomp.github.io/python-for-scicomp/parallel/) |
**Fri 25 Nov, 09:00-12:00 CET**
| Time | Section |
| ---- | --- |
| 09:00 | [parallel](https://aaltoscicomp.github.io/python-for-scicomp/parallel/) continued? |
| 09:00-10:10 | [dependency management](https://aaltoscicomp.github.io/python-for-scicomp/dependencies/) |
| 10:10-10:50 | [binder](https://aaltoscicomp.github.io/python-for-scicomp/binder/) |
| 10:50-11:30 | [packaging](https://aaltoscicomp.github.io/python-for-scicomp/packaging/) |
| 11:30-11:50 | panel discussion or buffer time? |
| 11:50-12:00 | [Outro](https://scicomp.aalto.fi/training/scip/python-for-scicomp/outro/) |
| 12:00 | After-party/discussion in zoom |
---
### Feedback
At the end of each day we will ask you for short feedback:
- one thing you liked or found useful
- one thing that can be improved
---
### Code of conduct
We strive to follow the [Contributor Covenant Code of Conduct](https://www.contributor-covenant.org/version/2/1/code_of_conduct/)
to foster an inclusive and welcoming environment for everyone.
[](https://github.com/ENCCS/event-organisation/blob/main/CODE_OF_CONDUCT.md)
In short:
- Use welcoming and inclusive language
- Be respectful of different viewpoints and experiences
- Gracefully accept constructive criticism
- Focus on what is best for the community
- Show courtesy and respect towards other community members
Contact details to report CoC violations can be [found here](https://enccs.se/kjartan-thor-wikfeldt).
---
:::danger
You can ask questions about the workshop content at the bottom of this page. We use the Zoom chat only for reporting Zoom problems and such.
:::
---
### Icebreaker question
What's your (first) name and what do you want to use Pythonb for?
- Thor: I use Python for various small analysis in my day to day work.
- Hampus: I want to use Python for my psychology PhD thesis work (genetics, stress, inflammation markers)
- Pamela: I want to use Python for my physics research
- Jade: I want to use Python in genomics (and see if it's more useful than just bash and R)
- Liqun: I want to use python for bioinformatics research
- Ana: I want to use python for single cell transcriptomics and bioinformatics research.
- Youchahou: I want to use Python for my Genomics research
- Andrea: I used Matlab so far at the University but just started a new job where I need to use Python for model data analysis.
- Danish: I want to use python for building machine learning models
- Federica (fedvi): I want to move all my scripts in matlab to python for my research in cardiovascular imaging
- Marcus: I want to use python in my fluid dynamics research
- Ingrid: I want to acquire data analysis skills for jobs in industry
- Jon: I would like to further develop my computational genetics skills with Python and apply pedigree analysis of forest tree populations
---
## Questions, answers and information
### Day 1
1. Should we number our questions so that we can easily find them again?
- great suggestion, let's do that
### Jupyter exercises (until 9:45):
- https://aaltoscicomp.github.io/python-for-scicomp/jupyter/#exercises-1
- https://aaltoscicomp.github.io/python-for-scicomp/jupyter/#exercises-2
- https://aaltoscicomp.github.io/python-for-scicomp/jupyter/#exercises-3
- Tip: `m` and `y` to go between markdown and code cells
- further keyboard shortcuts: https://coderefinery.github.io/jupyter/interface/#keyboard-shortcuts
1. here is my question
- here's an answer
### Break until 10:04 CET
### Exercise numpy-advanced-1 until 10:24 CET
https://aaltoscicomp.github.io/python-for-scicomp/numpy-advanced/#exercise-1
Got good timing? Write your result here!
- Hampus: 39.5 ms +- 98.8 us
- wow!
- you can compare what others got in the main hackMD: https://notes.coderefinery.org/python2022?view#Exercise-until-xx24
- I probably did it wrong, heheh, first time using numpy. Ran on a MacBook Pro M1 Max
- no but i think it could be correct, M1's are very fast. I see the same on my M2 mac
- for a more basic intro to numpy there's this episode which is not covered this time: https://aaltoscicomp.github.io/python-for-scicomp/numpy/
### Exercise: https://aaltoscicomp.github.io/python-for-scicomp/numpy-advanced/#exercise-2 until 10:50 CET
:::success
Twitch recommendation: go to "theatre mode" by clicking the third icon in the bottom right corner of the stream.
Recommended screen setup: see https://notes.coderefinery.org/python2022
:::
Solution to exercise 2
```python=
import numpy as np
def ravel(row, col, n_rows, n_cols):
return row*n_cols + col
ravel(1, 2, n_rows=3,n_cols=4)
```
### Break until 11:14 CET
### Next topic, pandas: https://aaltoscicomp.github.io/python-for-scicomp/pandas/
### Exercise 1
- Q: I have an error message when I try to load a histogram
```
- ---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-7-6f11b3fbb2ee> in <module>
----> 1 titanic.hist(column='Age', by='Survived', bins=25, figsize=(8,10), layout=(2,1), zorder=2, sharex=True, rwidth=0.9);
/opt/anaconda3/lib/python3.7/site-packages/pandas/plotting/_core.py in hist_frame(data, column, by, grid, xlabelsize, xrot, ylabelsize, yrot, ax, sharex, sharey, figsize, layout, bins, backend, **kwargs)
206 layout=layout,
207 bins=bins,
--> 208 **kwargs,
209 )
210
/opt/anaconda3/lib/python3.7/site-packages/pandas/plotting/_matplotlib/hist.py in hist_frame(data, column, by, grid, xlabelsize, xrot, ylabelsize, yrot, ax, sharex, sharey, figsize, layout, bins, **kwds)
375 ylabelsize=ylabelsize,
376 yrot=yrot,
--> 377 **kwds,
378 )
379 return axes
```
Can you help?
- A: Could you please check first with version of pandas you're using: type `%conda list pandas` in a cell, for example.
```
# packages in environment at /opt/anaconda3:
#
# Name Version Build Channel
pandas 1.0.1 py37h6c726b0_0
```
this is my version of pandas
Please retry:
```python=
import pandas as pd
url = "https://raw.githubusercontent.com/pandas-dev/pandas/master/doc/data/titanic.csv"
titanic = pd.read_csv(url, index_col='Name')
titanic.hist(column='Age', by='Survived', bins=25, figsize=(8,10),
layout=(2,1), zorder=2, sharex=True, rwidth=0.9);
```
Update: it worked with the new environment install. thanks!
## High Performance Computing (HPC)/parallel Python computing on SNIC clusters (Swedish HPC resources)
### Courses
- UPPMAX/HPC2N: [HPC python](https://uppmax.github.io/HPC-python/)
- PDC: [Introduction to MPI](https://pdc-support.github.io/introduction-to-mpi/)
### SNIC site tutorials/documentation
- UPPMAX: [Python user guide](https://www.uppmax.uu.se/support/user-guides/python-user-guide/)
- HPC2N: [Installing Python packages](https://www.hpc2n.umu.se/resources/software/user_installed/python)
- PDC:
- [working with python virtual environment](https://www.kth.se/blogs/pdc/2020/11/working-with-python-virtual-environments/)
- [how to use python](https://www.pdc.kth.se/software/software/python/cpe21.09/3.8.8/index_using.html)
- [parallel programming in python mpi4py — part 1](https://www.kth.se/blogs/pdc/2019/08/parallel-programming-in-python-mpi4py-part-1/)
- [parallel programming in python mpi4py — part 2](https://www.kth.se/blogs/pdc/2019/11/parallel-programming-in-python-mpi4py-part-2/)
- Lunarc: [Python at LUNARC](https://lunarc-documentation.readthedocs.io/en/latest/Python/)
- C3SE: [Python at C3SE](https://www.c3se.chalmers.se/documentation/applications/python/)
- NSC: [Python at NSC](https://www.nsc.liu.se/software/python/)
:::warning
For questions, use [main HackMD](https://notes.coderefinery.org/python2022)
For technical questions you can use the ZOOM chat instead! :bulb:
:::
:::info
*Always questions at the very bottom of this document, right above this.*
:::