-
Notifications
You must be signed in to change notification settings - Fork 0
/
plots.py
162 lines (146 loc) · 5.37 KB
/
plots.py
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Jun 11 11:50:44 2023
@author: Tommaso Giacometti
"""
import matplotlib.pyplot as plt
from numpy.typing import ArrayLike
import numpy as np
import torch
class Bcolors:
#Class to print on terminal with different colors
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKCYAN = '\033[96m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
#Plots
def plot_solution_scipy(time : ArrayLike, sol : ArrayLike, sol2 : ArrayLike = None) -> None:
'''
Function to plot the solution of the differentail equation computed by scipy odeint.
It can plot in a subplot also the 'normalized' solution to check if they are equal.
Parameters
----------
time : ArrayLike
Time array for the solutions.
sol : ArrayLike
odeint solution of the differential equation.
sol2 : ArrayLike, optional
odeint solution of the 'normalized' solution of the diff. eq. The default is None
and will be plotted only the first solution.
Returns
-------
None
'''
if sol2 is None:
fig, ax = plt.subplots()
ax.set_title('Solution of the differentail equation')
ax.plot(time, sol[:,0], label = 'x1')
ax.plot(time, sol[:,1], label = 'x2')
ax.plot(time, sol[:,2], label = 'y1')
ax.plot(time, sol[:,3], label = 'z')
ax.legend()
plt.show()
else:
fig, ax = plt.subplots(1,2, figsize = (8,4))
fig.suptitle('Solution of the differentail equation by scipy')
ax[0].plot(time, sol[:,0], label = 'x1')
ax[0].plot(time, sol[:,1], label = 'x2')
ax[0].plot(time, sol[:,2], label = 'y1')
ax[0].plot(time, sol[:,3], label = 'z')
ax[0].legend()
ax[0].set_title('Solution')
ax[1].plot(time, sol2[:,0], label = 'x1')
ax[1].plot(time, sol2[:,1], label = 'x2')
ax[1].plot(time, sol2[:,2], label = 'y1')
ax[1].plot(time, sol2[:,3], label = 'z')
ax[1].legend()
ax[1].set_title('Normalized solution')
plt.show()
pass
def plot_loss(lossi : list, mean = 20, tit = None) -> None:
'''
Plot the loss history in log scale.
Parameters
----------
lossi : list, ArrayLike
mean : Optional
The plot will show the mean of the loss for this number of steps.
tit : str, optional
Title to put on the plot.
Returns
-------
Show the plot
'''
try:
lossi = np.array(lossi)
y = lossi.reshape(-1,mean).mean(axis=1)
x = np.linspace(1, len(y), num=len(y))
fig, ax = plt.subplots()
ax.plot(x,y)
if tit is None:
ax.set_title(f'Mean of {mean} losses steps')
else:
ax.set_title(tit)
ax.set_ylabel('loss')
ax.set_xlabel(f'epoch/{mean}')
ax.set_yscale('log')
plt.show()
pass
except:
print(f'{Bcolors.WARNING}WARNING : {Bcolors.ENDC}the shape of lossi is not multiple of {mean}!')
print('The loss track plot will not be shown')
pass
def plot_solution_pinn(model, time, sol = None):
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
model.eval()
pred = model(torch.from_numpy(time).to(device).float().view(-1,1))
x1 = pred[:,0].detach().cpu().numpy()
x2 = pred[:,1].detach().cpu().numpy()
y1 = pred[:,2].detach().cpu().numpy()
z = pred[:,3].detach().cpu().numpy()
fig, ax = plt.subplots()
ax.plot(time, x1, label='x1')
ax.plot(time, x2, label='x2')
ax.plot(time, y1, label='y1')
ax.plot(time, z, label='z')
if sol is not None:
ax.plot(time, sol[:,0], label = 'real', linestyle='--', linewidth=1., c='black')
ax.plot(time, sol[:,1], linestyle='--', linewidth=1., c='black')
ax.plot(time, sol[:,2], linestyle='--', linewidth=1., c='black')
ax.plot(time, sol[:,3], linestyle='--', linewidth=1., c='black')
ax.legend()
ax.set_title('PINN solution of the differentail equation')
plt.show()
def plot_solution_pinn_inverse(model, time, data, sol = None):
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
model.eval()
pred = model(torch.from_numpy(time).to(device).float().view(-1,1))
x1 = pred[:,0].detach().cpu().numpy()
x2 = pred[:,1].detach().cpu().numpy()
y1 = pred[:,2].detach().cpu().numpy()
z = pred[:,3].detach().cpu().numpy()
fig, ax = plt.subplots()
ax.plot(time, x1, label='x1', linewidth=2.)
ax.plot(time, x2, label='x2', linewidth=2.)
ax.plot(time, y1, label='y1', linewidth=2.)
ax.plot(time, z, label='z', linewidth=2.)
data_t = data[:,0]
data_points = data[:,1:]
ax.scatter(data_t, data_points[:,3], label='data used', s = 10, c = 'tab:gray')
ax.scatter(data_t, data_points[:,2], s = 10, c = 'tab:gray')
if sol is not None:
ax.plot(time, sol[:,0], label = 'real', linestyle='--', linewidth=1., c='black')
ax.plot(time, sol[:,1], linestyle='--', linewidth=1., c='black')
ax.plot(time, sol[:,2], linestyle='--', linewidth=1., c='black')
ax.plot(time, sol[:,3], linestyle='--', linewidth=1., c='black')
ax.legend()
ax.set_xlabel('time (days)')
ax.set_ylabel('cells')
ax.set_title('PINN solution of the differentail equation')
plt.show()