Skip to content
Snippets Groups Projects
Commit fa3f00f5 authored by Jean-Luc Parouty's avatar Jean-Luc Parouty
Browse files

Update ci tests...

parent 470741ff
No related branches found
No related tags found
No related merge requests found
%% Cell type:markdown id: tags:
<img width="800px" src="../fidle/img/00-Fidle-header-01.svg"></img>
# <!-- TITLE --> [LINR1] - Linear regression with direct resolution
<!-- DESC --> Low-level implementation, using numpy, of a direct resolution for a linear regression
<!-- AUTHOR : Jean-Luc Parouty (CNRS/SIMaP) -->
## Objectives :
- Just one, the illustration of a direct resolution :-)
## What we're going to do :
Equation : $ Y = X.\theta + N$
Equation : $$Y = X.\theta + N$$
Where N is a noise vector
and $\theta = (a,b)$ a vector as y = a.x + b
%% Cell type:markdown id: tags:
## Step 1 - Import and init
%% Cell type:code id: tags:
``` python
import numpy as np
import math
import matplotlib
import matplotlib.pyplot as plt
import sys
sys.path.append('..')
import fidle.pwk as pwk
datasets_dir = pwk.init('LINR1')
```
%% Cell type:markdown id: tags:
## Step 2 - Retrieve a set of points
%% Cell type:code id: tags:
``` python
# ---- Paramètres
nb = 100 # Nombre de points
xmin = 0 # Distribution / x
xmax = 10
a = 4 # Distribution / y
b = 2 # y= a.x + b (+ bruit)
noise = 7 # bruit
theta = np.array([[a],[b]])
# ---- Vecteur X (1,x) x nb
# la premiere colonne est a 1 afin que X.theta <=> 1.b + x.a
Xc1 = np.ones((nb,1))
Xc2 = np.random.uniform(xmin,xmax,(nb,1))
X = np.c_[ Xc1, Xc2 ]
# ---- Noise
# N = np.random.uniform(-noise,noise,(nb,1))
N = noise * np.random.normal(0,1,(nb,1))
# ---- Vecteur Y
Y = (X @ theta) + N
# print("X:\n",X,"\nY:\n ",Y)
```
%% Cell type:markdown id: tags:
### Show it
%% Cell type:code id: tags:
``` python
width = 12
height = 6
fig, ax = plt.subplots()
fig.set_size_inches(width,height)
ax.plot(X[:,1], Y, ".")
ax.tick_params(axis='both', which='both', bottom=False, left=False, labelbottom=False, labelleft=False)
ax.set_xlabel('x axis')
ax.set_ylabel('y axis')
pwk.save_fig('01-set_of_points')
plt.show()
```
%% Cell type:markdown id: tags:
## Step 3 - Direct calculation of the normal equation
We'll try to find an optimal value of $\theta$, minimizing a cost function.
The cost function, classically used in the case of linear regressions, is the **root mean square error** (racine carré de l'erreur quadratique moyenne):
$RMSE(X,h_\theta)=\sqrt{\frac1n\sum_{i=1}^n\left[h_\theta(X^{(i)})-Y^{(i)}\right]^2}$
$$RMSE(X,h_\theta)=\sqrt{\frac1n\sum_{i=1}^n\left[h_\theta(X^{(i)})-Y^{(i)}\right]^2}$$
With the simplified variant : $MSE(X,h_\theta)=\frac1n\sum_{i=1}^n\left[h_\theta(X^{(i)})-Y^{(i)}\right]^2$
With the simplified variant : $$MSE(X,h_\theta)=\frac1n\sum_{i=1}^n\left[h_\theta(X^{(i)})-Y^{(i)}\right]^2$$
The optimal value of regression is : $ \hat{ \theta } =( X^{-T} .X)^{-1}.X^{-T}.Y$
The optimal value of regression is : $$\hat{ \theta } =( X^{-T} .X)^{-1}.X^{-T}.Y$$
Démontstration : https://eli.thegreenplace.net/2014/derivation-of-the-normal-equation-for-linear-regression
%% Cell type:code id: tags:
``` python
theta_hat = np.linalg.inv(X.T @ X) @ X.T @ Y
print("Theta :\n",theta,"\n\ntheta hat :\n",theta_hat)
```
%% Cell type:markdown id: tags:
### Show it
%% Cell type:code id: tags:
``` python
Xd = np.array([[1,xmin], [1,xmax]])
Yd = Xd @ theta_hat
fig, ax = plt.subplots()
fig.set_size_inches(width,height)
ax.plot(X[:,1], Y, ".")
ax.plot(Xd[:,1], Yd, "-")
ax.tick_params(axis='both', which='both', bottom=False, left=False, labelbottom=False, labelleft=False)
ax.set_xlabel('x axis')
ax.set_ylabel('y axis')
pwk.save_fig('02-regression-line')
plt.show()
```
%% Cell type:code id: tags:
``` python
pwk.end()
```
%% Cell type:markdown id: tags:
---
<img width="80px" src="../fidle/img/00-Fidle-logo-01.svg"></img>
......
%% Cell type:markdown id: tags:
<img width="800px" src="../fidle/img/00-Fidle-header-01.svg"></img>
# Gestion des tests d'intégration continue
**La liste des notebooks a éxécuter** et de leurs paramètres (override) est définie dans un **profile**.
- Un profile est un fichier yaml
- Un profile par défaut (à complété !) peut être généré.
Lorqu'un **profile** est prêt, il peut être soumis pour **éxécution**
- Pour chaque notebook, les paramètres sont positionnés (variables d'env.)
- Le notebook est lu, puis éxécuté
- Après éxécution, le notebook est sauvegardé sous son nom taggé (output_tag)
Un **rapport d'éxécution** est généré durant l'éxécution des tests.
A la fin des tests, ce rapport peut être visualisé via le notebook : [./03-ci-report.ipynb](./03-ci-report.ipynb)
%% Cell type:code id: tags:
```
import cookci
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
```
%% Cell type:markdown id: tags:
## Step 1 - Create default profile
Génère un profile par défaut comprenant tous les notebooks du moment...
...avec la liste des overrides disponibles :-)
%% Cell type:code id: tags:
```
profile = cookci.get_default_profile()
cookci.save_profile(profile, './ci/default.yml')
```
%% Output
Profile saved as ./ci/default.yml
Entries : 46
%% Cell type:markdown id: tags:
## Step 2 - Profile
%% Cell type:code id: tags:
```
profile_name = './ci/small_cpu.yml'
# profile_name = './ci/fidle-ad_s05.yml'
```
%% Cell type:code id: tags:
```
profile_name = os.getenv('FIDLE_OVERRIDE_PROFILE', profile_name )
```
%% Cell type:markdown id: tags:
## Step 3 - Run it
%% Cell type:code id: tags:
```
cookci.run_profile(profile_name, report_name='./logs/ci_report.json')
```
%% Output
Run profile session - FIDLE 2021
Version : 1.0
Load profile :./ci/small_cpu.yml
Entries : 7
Create new ci report : /home/pjluc/dev/fidle/fidle/logs/ci_report.json
Remove error file : /home/pjluc/dev/fidle/fidle/logs/ci_ERROR.txt
Run : Nb_LINR1
Run notebook.....done.
Duration : 0:00:06
=> ../fidle/img/00-Fidle-header-01.svg
=> ../fidle/img/00-Fidle-logo-01.svg
Saved as : ./run/ci/01-Linear-Regression==ci==.ipynb
Saved as : ./run/ci/01-Linear-Regression==ci==.html
Duration : 0:00:05
Saved {output_name} as ipynb, html and pdf
Run : Nb_GRAD1
Run notebook.....done.
Duration : 0:00:09
=> ../fidle/img/00-Fidle-header-01.svg
=> ../fidle/img/00-Fidle-logo-01.svg
Saved as : ./run/ci/02-Gradient-descent==ci==.ipynb
Saved as : ./run/ci/02-Gradient-descent==ci==.html
**Error during pdf convert...
Saved {output_name} as ipynb, html and pdf
Run : Nb_POLR1
Run notebook.....done.
Duration : 0:00:06
=> ../fidle/img/00-Fidle-header-01.svg
=> ../fidle/img/00-Fidle-logo-01.svg
Saved as : ./run/ci/03-Polynomial-Regression==ci==.ipynb
Saved as : ./run/ci/03-Polynomial-Regression==ci==.html
**Error during pdf convert...
Saved {output_name} as ipynb, html and pdf
Run : Nb_LOGR1
Run notebook.....done.
Duration : 0:00:06
=> ../fidle/img/00-Fidle-header-01.svg
=> ../fidle/img/00-Fidle-logo-01.svg
Saved as : ./run/ci/04-Logistic-Regression==ci==.ipynb
Saved as : ./run/ci/04-Logistic-Regression==ci==.html
**Error during pdf convert...
Saved {output_name} as ipynb, html and pdf
Run : Nb_PER57
Run notebook.....done.
Duration : 0:00:05
=> ../fidle/img/00-Fidle-header-01.svg
=> ../fidle/img/00-Fidle-logo-01.svg
Saved as : ./run/ci/01-Simple-Perceptron==ci==.ipynb
Saved as : ./run/ci/01-Simple-Perceptron==ci==.html
Saved {output_name} as ipynb, html and pdf
Run : Nb_BHPD1
set overrides :
FIDLE_OVERRIDE_BHPD1_fit_verbosity = 2
Run notebook...
Run notebook.....done.
Duration : 0:00:26
Saved {output_name} as ipynb, html and pdf
Run : Nb_BHPD2
set overrides :
FIDLE_OVERRIDE_BHPD2_fit_verbosity = 2
Run notebook.....done.
Duration : 0:00:42
Saved {output_name} as ipynb, html and pdf
End of running process
Duration : 0:02:03
Complete ci report : /home/pjluc/dev/fidle/fidle/logs/ci_report.json
%% Cell type:code id: tags:
```
%%bash
tree ./run/ci
find .. -name "*==ci==.*" -ls | sort -k11
# rm $(find .. -name "*==ci==.ipynb")
# rm $(find .. -name "*==ci==.html")
```
%% Output
./run/ci
├── html
│ ├── BHPD
│ │ ├── 01-DNN-Regression==ci==.html
│ │ └── 02-DNN-Regression-Premium==ci==.html
│ ├── IRIS
│ │ └── 01-Simple-Perceptron==ci==.html
│ └── LinearReg
│ ├── 01-Linear-Regression==ci==.html
│ ├── 02-Gradient-descent==ci==.html
│ ├── 03-Polynomial-Regression==ci==.html
│ └── 04-Logistic-Regression==ci==.html
├── ipynb
│ ├── BHPD
│ │ ├── 01-DNN-Regression==ci==.ipynb
│ │ └── 02-DNN-Regression-Premium==ci==.ipynb
│ ├── IRIS
│ │ └── 01-Simple-Perceptron==ci==.ipynb
│ └── LinearReg
│ ├── 01-Linear-Regression==ci==.ipynb
│ ├── 02-Gradient-descent==ci==.ipynb
│ ├── 03-Polynomial-Regression==ci==.ipynb
│ └── 04-Logistic-Regression==ci==.ipynb
└── pdf
├── BHPD
│ ├── 01-DNN-Regression==ci==.pdf
│ └── 02-DNN-Regression-Premium==ci==.pdf
├── IRIS
│ └── 01-Simple-Perceptron==ci==.pdf
└── LinearReg
├── 01-Linear-Regression==ci==.pdf
├── 02-Gradient-descent==ci==.pdf
├── 03-Polynomial-Regression==ci==.pdf
└── 04-Logistic-Regression==ci==.pdf
12 directories, 21 files
491718 768 -rw-r--r-- 1 pjluc pjluc 783282 Oct 25 22:21 ../fidle/run/ci/html/BHPD/01-DNN-Regression==ci==.html
491660 760 -rw-r--r-- 1 pjluc pjluc 774460 Oct 25 22:22 ../fidle/run/ci/html/BHPD/02-DNN-Regression-Premium==ci==.html
491710 680 -rw-r--r-- 1 pjluc pjluc 693754 Oct 25 22:21 ../fidle/run/ci/html/IRIS/01-Simple-Perceptron==ci==.html
491690 648 -rw-r--r-- 1 pjluc pjluc 662112 Oct 25 22:20 ../fidle/run/ci/html/LinearReg/01-Linear-Regression==ci==.html
491695 836 -rw-r--r-- 1 pjluc pjluc 854754 Oct 25 22:20 ../fidle/run/ci/html/LinearReg/02-Gradient-descent==ci==.html
491702 708 -rw-r--r-- 1 pjluc pjluc 722111 Oct 25 22:21 ../fidle/run/ci/html/LinearReg/03-Polynomial-Regression==ci==.html
491705 880 -rw-r--r-- 1 pjluc pjluc 900013 Oct 25 22:21 ../fidle/run/ci/html/LinearReg/04-Logistic-Regression==ci==.html
491714 232 -rw-r--r-- 1 pjluc pjluc 234633 Oct 25 22:21 ../fidle/run/ci/ipynb/BHPD/01-DNN-Regression==ci==.ipynb
138007 216 -rw-r--r-- 1 pjluc pjluc 217906 Oct 25 22:22 ../fidle/run/ci/ipynb/BHPD/02-DNN-Regression-Premium==ci==.ipynb
491708 120 -rw-r--r-- 1 pjluc pjluc 121956 Oct 25 22:21 ../fidle/run/ci/ipynb/IRIS/01-Simple-Perceptron==ci==.ipynb
491687 96 -rw-r--r-- 1 pjluc pjluc 94494 Oct 25 22:20 ../fidle/run/ci/ipynb/LinearReg/01-Linear-Regression==ci==.ipynb
491694 288 -rw-r--r-- 1 pjluc pjluc 291917 Oct 25 22:20 ../fidle/run/ci/ipynb/LinearReg/02-Gradient-descent==ci==.ipynb
491696 144 -rw-r--r-- 1 pjluc pjluc 145643 Oct 25 22:21 ../fidle/run/ci/ipynb/LinearReg/03-Polynomial-Regression==ci==.ipynb
491704 300 -rw-r--r-- 1 pjluc pjluc 307021 Oct 25 22:21 ../fidle/run/ci/ipynb/LinearReg/04-Logistic-Regression==ci==.ipynb
491721 120 -rw-r--r-- 1 pjluc pjluc 121007 Oct 25 22:21 ../fidle/run/ci/pdf/BHPD/01-DNN-Regression==ci==.pdf
491677 124 -rw-r--r-- 1 pjluc pjluc 124238 Oct 25 22:22 ../fidle/run/ci/pdf/BHPD/02-DNN-Regression-Premium==ci==.pdf
491712 84 -rw-r--r-- 1 pjluc pjluc 82133 Oct 25 22:21 ../fidle/run/ci/pdf/IRIS/01-Simple-Perceptron==ci==.pdf
491693 76 -rw-r--r-- 1 pjluc pjluc 76374 Oct 25 22:20 ../fidle/run/ci/pdf/LinearReg/01-Linear-Regression==ci==.pdf
491697 0 -rw-r--r-- 1 pjluc pjluc 0 Oct 25 22:20 ../fidle/run/ci/pdf/LinearReg/02-Gradient-descent==ci==.pdf
491703 0 -rw-r--r-- 1 pjluc pjluc 0 Oct 25 22:21 ../fidle/run/ci/pdf/LinearReg/03-Polynomial-Regression==ci==.pdf
491706 0 -rw-r--r-- 1 pjluc pjluc 0 Oct 25 22:21 ../fidle/run/ci/pdf/LinearReg/04-Logistic-Regression==ci==.pdf
%% Cell type:markdown id: tags:
---
<img width="80px" src="../fidle/img/00-Fidle-logo-01.svg"></img>
%% Cell type:code id: tags:
```
import base64
def get_base64_image(file):
with open(file, "rb") as image_file:
img64 = base64.b64encode(image_file.read())
src="data:image/svg+xml;base64,"+img64.decode("utf-8")
return src
```
%% Cell type:code id: tags:
```
import re
s='qsdqd<img width="800px" src="img/00-Fidle-header-01.svg"></img>qsdqd'
def get_base64_image(file):
with open(file, "rb") as image_file:
img64 = base64.b64encode(image_file.read())
src="data:image/svg+xml;base64,"+img64.decode("utf-8")
return src
def img_embedder64(html):
'''
Images embedder. Search images src="..." link and replace them
by base64 embedded images.
params:
html: input html
return:
output html
'''
for img_tag in re.findall('.*(<img .*>).*', html):
for src_tag in re.findall('.*src=[\'\"](.*)[\'\"].*', img_tag):
if src_tag.startswith('data:'): continue
src_b64 = get_base64_image(src_tag)
img_b64 = img_tag.replace(src_tag, src_b64)
html = html.replace(img_tag,img_b64)
return html
```
%% Cell type:code id: tags:
```
```
%% Cell type:markdown id: tags:
......
......@@ -244,14 +244,18 @@ def run_profile(profile_name, report_name=None, error_name=None, top_dir='..'):
exporter.template_name = 'classic'
(body_html, resources_html) = exporter.from_notebook_node(notebook)
# Check for images to ember in html
# Check for images to embed in html
# Hard job - better to do it in markdown
# body_html = images_embedder(body_html)
# ---- Convert to pdf
#
exporter=PDFExporter()
(body_pdf, resources_pdf) = exporter.from_notebook_node(notebook)
try:
exporter=PDFExporter()
(body_pdf, resources_pdf) = exporter.from_notebook_node(notebook)
except:
print(" **Error during pdf convert...")
body_pdf=b''
# ---- Save notebook as ipynb
#
......@@ -268,10 +272,10 @@ def run_profile(profile_name, report_name=None, error_name=None, top_dir='..'):
# ---- Save notebook as html
#
os.makedirs(f'{output_dir}/pdf/{notebook_dir}', mode=0o750, exist_ok=True)
with open( f'{output_dir}/pdf/{notebook_dir}/{output_name}.pdf', mode='w') as f:
with open( f'{output_dir}/pdf/{notebook_dir}/{output_name}.pdf', mode='wb') as f:
f.write(body_pdf)
print(' Saved {output_name} as ipynb, html and pdf')
print(f' Saved {output_name} as ipynb, html and pdf')
# ---- Clean all ------------------------------------------------------
#
......
......@@ -8,18 +8,78 @@
"description": "Full run on a small cpu",
"host": "Oban",
"profile": "./ci/small_cpu.yml",
"start": "25/10/21 18:22:46",
"end": "Unfinished...",
"duration": "Unfinished..."
"start": "25/10/21 22:20:38",
"end": "25/10/21 22:22:42",
"duration": "0:02:03"
},
"Nb_LINR1": {
"id": "LINR1",
"dir": "LinearReg",
"src": "01-Linear-Regression.ipynb",
"out": "01-Linear-Regression==ci==",
"start": "25/10/21 18:22:46",
"end": "25/10/21 18:22:52",
"start": "25/10/21 22:20:38",
"end": "25/10/21 22:20:44",
"duration": "0:00:05",
"state": "ok"
},
"Nb_GRAD1": {
"id": "GRAD1",
"dir": "LinearReg",
"src": "02-Gradient-descent.ipynb",
"out": "02-Gradient-descent==ci==",
"start": "25/10/21 22:20:48",
"end": "25/10/21 22:20:57",
"duration": "0:00:09",
"state": "ok"
},
"Nb_POLR1": {
"id": "POLR1",
"dir": "LinearReg",
"src": "03-Polynomial-Regression.ipynb",
"out": "03-Polynomial-Regression==ci==",
"start": "25/10/21 22:20:59",
"end": "25/10/21 22:21:06",
"duration": "0:00:06",
"state": "ok"
},
"Nb_LOGR1": {
"id": "LOGR1",
"dir": "LinearReg",
"src": "04-Logistic-Regression.ipynb",
"out": "04-Logistic-Regression==ci==",
"start": "25/10/21 22:21:08",
"end": "25/10/21 22:21:14",
"duration": "0:00:06",
"state": "ok"
},
"Nb_PER57": {
"id": "PER57",
"dir": "IRIS",
"src": "01-Simple-Perceptron.ipynb",
"out": "01-Simple-Perceptron==ci==",
"start": "25/10/21 22:21:16",
"end": "25/10/21 22:21:22",
"duration": "0:00:05",
"state": "ok"
},
"Nb_BHPD1": {
"id": "BHPD1",
"dir": "BHPD",
"src": "01-DNN-Regression.ipynb",
"out": "01-DNN-Regression==ci==",
"start": "25/10/21 22:21:25",
"end": "25/10/21 22:21:52",
"duration": "0:00:26",
"state": "ok"
},
"Nb_BHPD2": {
"id": "BHPD2",
"dir": "BHPD",
"src": "02-DNN-Regression-Premium.ipynb",
"out": "02-DNN-Regression-Premium==ci==",
"start": "25/10/21 22:21:56",
"end": "25/10/21 22:22:38",
"duration": "0:00:42",
"state": "ok"
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment