-
Notifications
You must be signed in to change notification settings - Fork 24
/
tp.qmd
397 lines (286 loc) · 11.5 KB
/
tp.qmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
---
title: "Classes & Exceptions"
---
## Classes
*Cslasses* are central elements in *Object-oriented programming (OOP)*
A class structure defines an object, its properties, and all the operations one can apply to it.
In Python, a class contains *attributes* (variables) and *methods* (functions).
It is defined similarly to a function, replacing the `def` keyword with `class`. The name of a class should be CapWords or CamelCase (the naming comes from the "bumpy" look of its letters); see examples below.
Usually, a class contains some class methods (functions inside the class).
* The first argument of a (non `static`) method is usually called `self`: it is a mandatory element. This `self` argument is for self-reference.
* Some method names have a specific meaning, for instance:
* `__init__`: name of the method invoked when creating the object (instantiation)
* `__call__`: method invoked when calling the object
* `__str__`: method invoked when a class has a representation as a string, *e.g.*, when passing it to the `print` function
* see [Python documentation](http://docs.python.org/3/reference/datamodel.html#special-method-names) for more special names.
### Example
Let us define a simple Point class, representing a point in the plane.
```{python}
class Point(object):
"""A class to represent planar points."""
def __init__(self, x, y):
"""Create a new point (x, y)."""
self.x = x
self.y = y
def translate(self, dx, dy):
"""Translate the point by dx and dy."""
self.x += dx
self.y += dy
def __str__(self):
return "Point: [{0:1.3f}, {1:1.3f}]".format(self.x, self.y)
```
::: {.callout-note}
If you are not familiar with printing in Python, start with the new f-strings format, see for instance: <https://zetcode.com/python/fstring/>.
:::
To create a new instance of the class `Point`:
```{python}
p1 = Point(x=0, y=0) # call __init__ ;
print(p1.x)
print(p1.y)
print("{0}".format(p1)) # call __str__
p1
```
To apply our `translate` method to the point `p1`:
```python
p1.translate(dx=1, dy=1)
print(p1.translate)
print(p1)
print(type(p1))
```
To run a method of the object `p1` (which is an instance of `Point`) simply use the dot notation: `p1.translate(arg1, arg2)` is equivalent to `Point.translate(p1, arg1, arg2)`.
```{python}
p2 = Point(1, 1)
p1.translate(0.25, 1.5)
print(p1)
print(p2)
```
::: {.callout-note}
You might have already used that dot notation with `numpy` for instance when executing `numpy` functions as follows
```{python}
import numpy as np
rng = np.random.default_rng(seed=12345)
a = rng.random((3, 3))
a.mean(axis=0)
```
:::
### `__call__`
`MyClass(arg1, arg2, ...)` is a shorthand for `MyClass.__call__(arg1, arg2, ...)`, so this allows writing classes where the instances behave like functions and can be called like a function
```{python}
class Sum:
def __init__(self):
print("Instance Created")
# Defining __call__ method
def __call__(self, a, b):
print(a + b)
# Instance created
sum_as_a_function = Sum()
# __call__ method will be called
sum_as_a_function(10, 20)
```
Test function of interest: `isinstance` allow to check if an object is of the correct class.
```{python}
isinstance(sum_as_a_function, Sum)
isinstance(sum_as_a_function, Point)
```
### Remarks
* A method of a class can modify the state of a particular instance.
This does not alter the other instantiations of the class.
* Methods that do not depend on a particular instantiation can be decorated with the `@staticmethod` keyword. Such method **do not** have their first arg referring to `self`...
::: {.callout-important appearance='default' icon="false"}
## EXERCISE: Gaussian class
Implement a class `Gaussian` with attributes `mean` and `std` with a method
- `__str__` returning a string with the expression of the density
- `__eq__` testing the equality of two instances. You should use numpy.isclose()
- `__add__` implementing the addition of independent Gaussian, or more precisely their pdfs (probability density functions)
- `__radd__` implementing the addition of independent Gaussian, or more precisely their pdfs (probability density functions)
Note: when executing `a+b` what really happens is that the __add__ method of the `a`: object is called `a.__add__(b)`.
```{python}
#| echo: false
class Gaussian:
def __init__(self, mean, std):
"""Create a couple (mean, std)."""
self.mean = mean
self.std = std
def __str__(self):
return f"pdf: exp(-(x - {self.mean:1.3f})^2 / ({self.std:1.3f}*2^2)) / sqrt(2 * pi * {self.std:1.3f}^2)"
def __add__(self, other):
new_mean = self.mean + other.mean
new_std = (self.std ** 2 + other.std ** 2) ** 0.5
return Gaussian(new_mean, new_std)
def __radd__(self, other):
if other == 0:
return self
else:
return self.__add__(other)
def __eq__(self, other):
return np.isclose(self.mean, other.mean) and np.isclose(self.std, other.std)
```
```{python}
g1 = Gaussian(0.0, 1.0)
g2 = Gaussian(1.0, 2.0)
g3 = Gaussian(2.0, 2.0)
g4 = Gaussian(3.0, 3.0)
print(g1)
print(g2)
print(g3)
print(g4)
print(g2 + g1)
print(sum([g1, g2, g3]))
print(sum([g1, g2, g3]) == g4)
# The former should display
# The density function is: exp(-(x - 0)^2 / (2*1^2)) / sqrt(2 * pi * 1^2)
# The density function is: exp(-(x - 1)^2 / (2*2^2)) / sqrt(2 * pi * 2^2)
# True
# False
# The density function is: exp(-(x - 1)^2 / (2 * 2.23606797749979^2)) / sqrt(2 * pi * 2.23606797749979^2)
```
:::
### Inheritance, the sub-Gaussian case (of a kind)
Classes can inherit methods from other classes. You can use `super` (Latin for "above") to access the parent class.
Test function of interest: `isinstance` allow to check if an object is of the correct class.
```{python}
class IsoGaussian(Gaussian):
def __init__(self, mean):
super().__init__(mean, 1.0)
gg1 = IsoGaussian(3)
print(gg1)
issubclass(IsoGaussian, Gaussian)
```
Many examples of interest are considered in the `scikit-learn` package (see for instance the module [Linear Model](https://github.com/scikit-learn/scikit-learn/blob/95119c13a/sklearn/linear_model/_base.py#L391) often used in machine learning or statistics).
For more information on `super`, see for instance this [Real Python Tutorials](https://realpython.com/python-super/).
## Exceptions
This section is inspired by [Fabien Maussion's lecture on Scientific Programming](https://fabienmaussion.info/scipro_ss2018/html/09-Exceptions.html).
* In Python errors are handled through `Exceptions`
* An error throws an `Exception` interrupting the normal code execution
* Execution can overpass such an issue inside a bloc with `try` - `except`
* A typical use case: stop the program when an error occurs:
```python
def my_function(arguments):
if not verify(arguments):
raise Exception("Invalid arguments")
# keep continuing
```
The list of possible errors is available here:
One may use `try`, `except` or `finally` to prevent errors to stop the program:
https://docs.python.org/3/library/exceptions.html#bltin-exceptions
and includes `NameError`, `ImportError`, `AssertionError` etc.
```python
try:
# normal code 1 goes here
except:
# code for error handling goes here
# this code 2 is not executed unless the code 1
# above generated an error
finally:
# optional. This clause is executed no matter what,
# and is generally used to release external resources.
```
### Example
```{python}
try:
print("test_var")
e = 4
print(test_var) # raise an error: the test_var variable is not defined
except NameError:
print("Caught an exception: test_var does not exist")
finally:
print("This code is executed every time")
print("The program keep continuing... it does not freeze!")
print('Beware! the variable ', 'e =', e, 'is still defined.')
```
To obtain some information on the error: it is possible to access the instance of the `Exception` class thrown by the program through the syntax:
```{python}
try:
print("test")
print(testtt) # error: the variable testtt is not defined
except Exception as e:
print("Caught an exception:", e)
```
**Note**: a common use case is to test if the import of a package was successful or not
```python
try:
import ddownload
except Exception as e:
print(e)
```
Other common exceptions: <https://docs.python.org/3/library/exceptions.html>
### The `with` statement
**Warning**: you have to run the following lines with a directory `scripts/` containing a function `hello-world.py`
```{python}
fname = "./scripts/hello-world.py"
with open(fname) as file: # Use file to refer to the file object
data = file.read()
print(data)
# at the end of the code chunk, the file.__exit__() method is called (i.e., file.close() is done automatically)
```
Now
```{python}
fname = "scripts/hello-world_do_not_exist.py"
try:
# 1/0 # uncomment at some point and run
file = open(fname)
data = file.read()
print(data)
except FileNotFoundError:
print("File not found!")
except (RuntimeError, TypeError, NameError, ZeroDivisionError):
print("Specific Error message 2")
finally:
file.close() # important to release the access to the file !
```
::: {.callout-important appearance='default' icon="false"}
## EXERCISE: Improving the Gaussian class
Create a sub-class `GaussianBis` where you check if the user has provided float arguments (see also `assert` and `isinstance` routines).
Print a custom explicit error message if it is not the case.
:::
## Scope
```python
e = 0
print(e)
for i in range(1):
e = 1
print(e)
def f():
e = 2
print(e)
```
**Conclusion**: `e` is only "visible" inside the function definition.
See https://realpython.com/python-scope-legb-rule/ for more information on this topics.
## Manipulate file names on disk across platforms.
The following would avoid naming conflict due to each OS (Linux / Windows /Mac) syntax. This is important for your project if you work with colleagues having a different OS than yours!!!
```python
import os
#
print(os.path.join('~', 'work', 'src'))
print(os.path.join(os.getcwd(), 'new_directory'))
os.path.expanduser?
print(os.path.expanduser(os.path.join('~', 'work', 'src')))
```
::: {.callout-important appearance='default' icon="false"}
## EXERCISE: Create a bunch of files
Write a simple script that creates, in the sub-directory `scripts`, the following text files: `myDb_000.txt`, `myDb_001.txt`, `myDb_002.txt`, ..., `myDb_049.txt`. The `i`-th file should contain a single line with the average of the `i` first digits of pi.
**Hint**:
- you might need zero padding
- you can check what the following code does
```python
file = open("copy.txt")
file.write("Your text goes here")
file.close()
```
- you might also need some precision for the digits of $\pi$, hence using `mpmath` instead of `numpy`
```python
from mpmath import mp
import numpy as np
if not os.path.isdir("script"):
os.mkdir("script")
for i in range(2, 50):
for i in range(2, n_tot + 2):
val = '0' + str(i)
print(str(f"{i:{val}}"))
```
:::
## More links
* [Python official web page](http://www.python.org) and its [style and writing recommendation](http://www.python.org/dev/peps/pep-0008)
* [Think Python](http://www.greenteapress.com/thinkpython/) - A free book by Allen Downey.
* [Python Essential Reference](http://www.amazon.com/Python-Essential-Reference-4th-Edition/dp/0672329786) - a good reference for general Python coding
* [Python Data Science Handbook](https://jakevdp.github.io/PythonDataScienceHandbook/) - an excellent book for data science in Python by Jake VanderPlas (associated notebook available online)