Commit 07c31ad7 authored by paugier's avatar paugier
Browse files

First course nearly ready

parent d0f0e239
Pipeline #49930 passed with stage
in 43 seconds
%% Cell type:markdown id: tags:
# Scientific computing course 2020
Pierre Augier (LEGI) and Enzo Le BoueDec (LEGI)
(And Cyrille Bonamy (LEGI), Eric Maldonado (Irstea), Franck Thollard (ISTerre), Christophe Picard (LJK), Loïc Huder (ISTerre)...)
%% Cell type:markdown id: tags:
## Scientific computing course 2020
During sessions 1, 2 and 4 (9 hours), we will present an introduction on
scientific computing and programming:
During sessions 1, 2 and 4 (9 hours), we will study some basics on Linux and Python for scientific computing.
We will also give an introduction on scientific computing and programming:
- Computers to compute (CPU, memories, GPU, clusters)
- Numbers in computers
- Programming languages
- Difference open-source / close-source
- Operating systems and importance of Linux for scientific computing
- Install a good environment, example of Conda
- Versioning and Gitlab (https://gricad-gitlab.univ-grenoble-alpes.fr)
We will study some basics on Linux and Python for scientific computing.
The personal project part will be presented during session 3. Sessions 3, 5, 6
and 7 will be dedicated to practical exercises on (3) Computation of integrals,
(5) Finite Difference methods, (6) Gradient descent and (7) Machine learning.
We will work together on your personal projects during session 8.
This course should be fun! Do not hesitate to interrupt and ask any questions!
%% Cell type:markdown id: tags:
## Why using Python for this course ?
- A very nice, clean and well-thought programming language.
- **Generalist**: good for several applications, [used with success in many fields](http://brochure.getpython.info/).
- For academic purposes (teaching, research): **great scientific ecosystem**.
Nowadays, scientists must know a bit of Python... Good first language.
......
%% Cell type:markdown id: tags:
# Introduction to Bash
# First steps with Bash and Linux
Bash is the most standard shell on Unix systems (Linux, MacOS, ...). A shell is a basic programming language to interact with the computer and launch commands.
There are standard commands (corresponding to programs) to do many basic tasks. You can read this short document https://files.fosswire.com/2007/08/fwunixref.pdf.
For example to get the current working directory, one can use:
%% Cell type:code id: tags:
``` bash
pwd
```
%% Output
/home/pierre/Output/Teach/scientific-computing-m2-efm/ipynb
%% Cell type:markdown id: tags:
We see that we ask `pwd` and we get an answer (as a string).
There are also commands that do something but does not returb anything, for example cd (for "change directory"):
%% Cell type:code id: tags:
``` bash
cd ..
```
%% Cell type:code id: tags:
``` bash
ls
```
%% Output
check_env.py ipynb Makefile README.md
install.md LICENSE.txt pyfiles requirements.txt
%% Cell type:code id: tags:
``` bash
ls -l
```
%% Output
total 32
-rw-r--r-- 1 pierre pierre 641 Sep 25 14:40 check_env.py
-rw-r--r-- 1 pierre pierre 3715 Sep 25 15:31 install.md
drwxrwxr-x 4 pierre pierre 4096 Sep 25 15:42 ipynb
-rw-rw-r-- 1 pierre pierre 1499 Sep 25 14:40 LICENSE.txt
-rw-r--r-- 1 pierre pierre 938 Sep 25 15:10 Makefile
drwxr-xr-x 4 pierre pierre 4096 Sep 25 14:44 pyfiles
-rw-rw-r-- 1 pierre pierre 1655 Sep 25 15:35 README.md
-rw-r--r-- 1 pierre pierre 196 Sep 25 14:52 requirements.txt
%% Cell type:code id: tags:
``` bash
ls -a
```
%% Output
. check_env.py .hg ipynb Makefile README.md .vscode
.. .gitignore install.md LICENSE.txt pyfiles requirements.txt
%% Cell type:code id: tags:
``` bash
ls -la
```
%% Output
total 52
drwxrwxr-x 6 pierre pierre 4096 Sep 25 14:59 .
drwxr-xr-x 7 pierre pierre 4096 Sep 25 13:50 ..
-rw-r--r-- 1 pierre pierre 641 Sep 25 14:40 check_env.py
-rw-r--r-- 1 pierre pierre 190 Sep 25 14:44 .gitignore
drwxrwxr-x 6 pierre pierre 4096 Sep 25 15:40 .hg
-rw-r--r-- 1 pierre pierre 3715 Sep 25 15:31 install.md
drwxrwxr-x 4 pierre pierre 4096 Sep 25 15:42 ipynb
-rw-rw-r-- 1 pierre pierre 1499 Sep 25 14:40 LICENSE.txt
-rw-r--r-- 1 pierre pierre 938 Sep 25 15:10 Makefile
drwxr-xr-x 4 pierre pierre 4096 Sep 25 14:44 pyfiles
-rw-rw-r-- 1 pierre pierre 1655 Sep 25 15:35 README.md
-rw-r--r-- 1 pierre pierre 196 Sep 25 14:52 requirements.txt
drwxrwxr-x 2 pierre pierre 4096 Sep 25 14:59 .vscode
%% Cell type:code id: tags:
``` bash
cd ipynb
```
%% Cell type:code id: tags:
``` bash
pwd
```
%% Output
/home/pierre/Output/Teach/scientific-computing-m2-efm/ipynb
%% Cell type:code id: tags:
``` bash
cd ..
```
%% Cell type:code id: tags:
``` bash
ls
```
%% Output
check_env.py ipynb Makefile README.md
install.md LICENSE.txt pyfiles requirements.txt
%% Cell type:code id: tags:
``` bash
mkdir toto
```
%% Cell type:code id: tags:
``` bash
ls
```
%% Output
check_env.py ipynb Makefile README.md toto
install.md LICENSE.txt pyfiles requirements.txt
%% Cell type:code id: tags:
``` bash
rm -rf toto
```
%% Cell type:code id: tags:
``` bash
which ls
```
%% Output
/bin/ls
%% Cell type:code id: tags:
``` bash
echo "toto"
```
%% Output
toto
%% Cell type:code id: tags:
``` bash
echo $PATH
```
%% Output
/data0/opt/miniconda3/condabin/app:/home/pierre/.pyenv/shims:/home/pierre/.pyenv/bin:/home/pierre/.pyenv/versions/3.8.2/bin:/home/pierre/.pyenv/libexec:/home/pierre/.pyenv/plugins/python-build/bin:/data0/opt/miniconda3/condabin/app:/home/pierre/.pyenv/shims:/home/pierre/.pyenv/bin:/home/pierre/.cargo/bin:/data0/opt/miniconda3/condabin/app:/data0/opt/miniconda3/condabin:/home/pierre/.pyenv/shims:/home/pierre/.pyenv/bin:/home/pierre/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/home/pierre/opt/env_xonsh/bin/
%% Cell type:code id: tags:
``` bash
echo $HOME
```
%% Output
/home/pierre
%% Cell type:code id: tags:
``` bash
ls
```
%% Output
check_env.py ipynb Makefile README.md
install.md LICENSE.txt pyfiles requirements.txt
%% Cell type:code id: tags:
``` bash
echo "toto" > tmp.txt
```
%% Cell type:code id: tags:
``` bash
ls
```
%% Output
check_env.py ipynb Makefile README.md tmp.txt
install.md LICENSE.txt pyfiles requirements.txt
%% Cell type:code id: tags:
``` bash
cat tmp.txt
```
%% Output
toto
%% Cell type:code id: tags:
``` bash
echo "titi" >> tmp.txt
```
%% Cell type:code id: tags:
``` bash
cat tmp.txt
```
%% Output
toto
titi
%% Cell type:code id: tags:
``` bash
mv tmp.txt tmp2.txt
```
%% Cell type:code id: tags:
``` bash
ls
```
%% Output
check_env.py ipynb Makefile README.md tmp2.txt
install.md LICENSE.txt pyfiles requirements.txt
%% Cell type:code id: tags:
``` bash
mv tmp2.txt ..
```
%% Cell type:code id: tags:
``` bash
ls ..
```
%% Output
coursem1_pa_instabilities_turbulence py-training-2017 tmp2.txt
intro-python scientific-computing-m2-efm training-hpc
%% Cell type:code id: tags:
``` bash
rm -f ../tmp2.txt
```
%% Cell type:code id: tags:
``` bash
ls ..
```
%% Output
coursem1_pa_instabilities_turbulence py-training-2017 training-hpc
intro-python scientific-computing-m2-efm
......
%% Cell type:markdown id: tags:
# Scientific computing course 2020
Pierre Augier (LEGI) and Enzo Le BoueDec (LEGI)
(And Cyrille Bonamy (LEGI), Eric Maldonado (Irstea), Franck Thollard (ISTerre), Christophe Picard (LJK), Loïc Huder (ISTerre)...)
# First steps
%% Cell type:markdown id: tags:
## Why Python ?
- A very nice and well-thought programming language.
- **Generalist**: good for several applications, [used with success in many fields](http://brochure.getpython.info/).
- For academic purposes (teaching, research): **great scientific ecosystem**.
# First steps. Different ways to use Python.
%% Cell type:markdown id: tags:
## Python: a language, an interpreter
Python is a programming language.
It is first an **interpreted** language (like Bash, Matlab, and in contrast with Fortran, C or C++), so we need an interpreter, i.e. a binary parsing Python code and running computer instructions.
Open a terminal. There are usually different python commands: `python`, `python2` and `python3`.
%% Cell type:markdown id: tags:
### Remark Python 2 / Python 3
2 languages, 2 interpreters, 2 commands
%% Cell type:markdown id: tags:
## Python distributions
- On Linux, there are the system python binaries.
- On Windows, Mac (and Linux), you can install and use
* https://www.python.org/downloads/
* [Anaconda or Miniconda](https://www.continuum.io/downloads)
([advice on how to install a good scientific Python environment](http://fluiddyn.readthedocs.io/en/latest/get_good_Python_env.html))
%% Cell type:markdown id: tags:
### Do it yourself: personal work
Download the repository of this training (you need one of the programs `git` or `mercurial`). On Unix machines, use the commands:
```bash
hg clone https://gricad-gitlab.univ-grenoble-alpes.fr/meige-legi/scientific-computing-m2-efm.git
cd scientific-computing-m2-efm
cat README.rst
make help
```
Warning: `cat` and `make` are Unix commands...
%% Cell type:markdown id: tags:
### Do it yourself: execute a script with the command `python3`
Run a script with `python3` (pyfiles/helloworld.py is *just* a text file):
```bash
cat pyfiles/helloworld.py
python3 pyfiles/helloworld.py
```
(`cat` is a Unix command that print the content of a text file.)
%% Cell type:markdown id: tags:
### Do it yourself: work interactively with [ipython](https://ipython.org/)
The command `ipython3` launch the program [ipython](https://ipython.org/), which is used for interactive python.
- In ipython, you can execute a first interactive instruction:
%% Cell type:code id: tags:
``` python
2 + 2
```
%% Output
4
%% Cell type:code id: tags:
``` python
3 / 4
```
%% Output
0.75
%% Cell type:markdown id: tags:
- Run the script from ipython.
%% Cell type:code id: tags:
``` python
run ../pyfiles/helloworld.py
```
%% Output
Hello world
My name is Pierre
%% Cell type:code id: tags:
``` python
# name is defined in ../pyfiles/helloworld.py
print(name)
```
%% Output
Pierre
%% Cell type:markdown id: tags:
- Help on an object can be displayed with the question mark (try it):
```ipython
name?
```
- Try to type "name." and to press on tab... The tab key is used to tell you how what you typed can be completed.
- Try to use the top and bottom arrows...
%% Cell type:markdown id: tags:
### Python in an IDE ([Spyder](https://pythonhosted.org/spyder/))
Launch the application Spyder, a Python IDE (Integrated development environment).
- a quite good code editor with:
- syntax coloring,
- code analysis powered by pyflakes and pylint,
- introspection capabilities such as code completion.
- Ipython console
- variable inspector
- ...
Remarks:
- it is very important to use a good editor to code in Python (for example Spyder, emacs or vi with [a good setup](https://bitbucket.org/fluiddyn/fluid_emacs.d), or [Visual Studio Code](https://code.visualstudio.com/docs/languages/python)).
%% Cell type:markdown id: tags:
### Python in the browser ([Jupyter](https://jupyter.readthedocs.io))
The presentations of this python training are made with Jupyter (demonstration).
This is a very powerful tool to present results (see these [examples](http://nbviewer.jupyter.org/)).
```bash
cd scientific-computing-m2-efm
jupyter-lab
```
......
%% Cell type:markdown id: tags:
# Python training UGA 2017
# Scientific computing course 2020
**A training to acquire strong basis in Python to use it efficiently**
**Acquire strong basis in Python to use it efficiently**
Pierre Augier (LEGI), Cyrille Bonamy (LEGI), Eric Maldonado (Irstea), Franck Thollard (ISTerre), Christophe Picard (LJK), Loïc Huder (ISTerre)
# Characteristics of the Python language
%% Cell type:markdown id: tags:
## Characteristics of the Python language
- Open-source language, interpreters and ecosystem
- Interpreted (but there are tools to compile Python code)
- Automatic memory management
- Dynamically strongly typed
%% Cell type:markdown id: tags:
## Dynamically and strongly typed: objects and variables
The function `type` returns the type of an **object**:
%% Cell type:code id: tags:
``` python
type('hello')
```
%% Output
str
%% Cell type:code id: tags:
``` python
type(2)
```
%% Output
int
%% Cell type:code id: tags:
``` python
type(2.)
```
%% Output
float
%% Cell type:code id: tags:
``` python
type(2 + 2)
```
%% Output
int
%% Cell type:code id: tags:
``` python
type(2 + 2.)
```
%% Output
float
%% Cell type:code id: tags:
``` python
type(True)
```
%% Output
bool
%% Cell type:markdown id: tags:
## Dynamically and strongly typed: objects and variables
**Variables** are just tags pointing towards objects. New variables can be used when needed. They are not associated with a type but only with an object (which has a type)...
%% Cell type:code id: tags:
``` python
myvar = 1
print(myvar, type(myvar))
```
%% Output
1 <class 'int'>
%% Cell type:code id: tags:
``` python
myvar = 'hello'
print(myvar, type(myvar))
```
%% Output
hello <class 'str'>
%% Cell type:markdown id: tags:
## Spaces for objects and variables (names)
Objects and variables (names) are two very different concepts:
- Objects live in one "object space". They have an address in the memory.
- Names live in namespaces.
It is often interesting to represent the execution of a Python program in an **"object space - namespaces" diagram**.
The Zen of Python says "Namespaces are one honking great idea -- let's do more of those!". A namespace is created for every module (file) and for every function execution,
%% Cell type:markdown id: tags:
## Characteristics of the Python language
- Open-source language, interpreters and ecosystem
- Interpreted (but there are tools to compile Python code)
- Automatic memory management
- Dynamically strongly typed
- Gradual learning curve
- A philosophy: the [Zen of Python](https://www.python.org/dev/peps/pep-0020/)
- Very clean and readable
- Indentation defines the blocks
- Style coding is important: [pep8](https://www.python.org/dev/peps/pep-0008/)
%% Cell type:markdown id: tags:
## PEP8 (Python Extension Proposal)
https://www.python.org/dev/peps/pep-0008/
- Code layout
- Imports
- Whitespace in expressions and statements
- Comments
- Documentation strings
- Naming conventions
- Programming recommendations
%% Cell type:markdown id: tags:
## PEP8: examples of bad and good style practices
%% Cell type:code id: tags:
``` python
# bad (spaces between operator)
number=0
# ok
number = 0
```
%% Cell type:code id: tags:
``` python
# bad (indentation with 2 spaces, has to be 4)
if number == 0:
number = 1
# ok
if number == 0:
number = 1
```
%% Cell type:code id: tags:
``` python
# bad (space after ,)
mylist = [1,2,3]
# ok
mylist = [1, 2, 3]
```
%% Cell type:markdown id: tags:
## Characteristics of the Python language
- Open-source language, interpreters and ecosystem
- Interpreted (but there are tools to compile Python code)
- Automatic memory management
- Dynamically strongly typed
- Gradual learning curve
- A philosophy: the [Zen of Python](https://www.python.org/dev/peps/pep-0020/)
- Very clean and readable
- Indentation defines the blocks
- Style coding is important: [pep8](https://www.python.org/dev/peps/pep-0008/)
%% Cell type:markdown id: tags:
## You have to use a good editor!!
Python:
- (most of the time) interpreted (no compilation)
- very dynamic
- pep8 (style).
- indentation defines the blocks
The editor can and has to help you!!
- syntax coloring
- automatic indentation
- code analysis (code analysis powered by pyflakes and pylint)
- introspection capabilities such as code completion
%% Cell type:markdown id: tags:
## Do it yourself: PEP8 checker with Spyder editor
In Spyder, open the file `./pyfiles/wrong.py` and see what append.
Activate the "Real time code style analysis (PEP 8)" (Preferences -> Editor -> Code Introspection/Analysis).
Correct the file to remove the warnings.
%% Cell type:markdown id: tags:
## Characteristics of the Python language
- Open-source language, interpreters and ecosystem
- Interpreted (but there are tools to compile Python code)
- Automatic memory management
- Dynamically strongly typed
- Gradual learning curve
- A philosophy: the [Zen of Python](https://www.python.org/dev/peps/pep-0020/)
- Very clean and readable
- Indentation defines the blocks
- Style coding is important: [pep8](https://www.python.org/dev/peps/pep-0008/)
- Only few [keywords](https://hg.python.org/cpython/file/3.6/Lib/keyword.py) and [built-in functions](https://docs.python.org/3/library/functions.html)
%% Cell type:markdown id: tags:
## Keywords
%% Cell type:code id: tags:
``` python
help("keywords")
```
%% Output
Here is a list of the Python keywords. Enter any keyword to get more help.
False def if raise
None del import return
True elif in try
and else is while
as except lambda with
assert finally nonlocal yield
break for not
class from or
continue global pass
%% Cell type:markdown id: tags:
plus 68 [built-in functions](https://docs.python.org/3/library/functions.html)... plus few [built-in constants](https://docs.python.org/3/library/constants.html)... plus [built-in exceptions](https://docs.python.org/3/library/exceptions.html)!
%% Cell type:markdown id: tags:
## Characteristics of the Python language
- Open-source language, interpreters and ecosystem
- Interpreted (but there are tools to compile Python code)
- Automatic memory management
- Dynamically strongly typed
- Gradual learning curve
- A philosophy: the [Zen of Python](https://www.python.org/dev/peps/pep-0020/)
- Very clean and readable
- Indentation defines the blocks
- Style coding is important: [pep8](https://www.python.org/dev/peps/pep-0008/)
- Only few [keywords](https://hg.python.org/cpython/file/3.6/Lib/keyword.py) and [built-in functions](https://docs.python.org/3/library/functions.html)
- Errors should never pass silently
- Multi-paradigm (sequential, object-oriented, functional)
- "Batteries Included": [the standard library](https://docs.python.org/3/tutorial/stdlib.html)
......