Skip to content

Commit 89b6fa2

Browse files
committed
added AIChE edits, created pairplots
1 parent 18bdb77 commit 89b6fa2

16 files changed

Lines changed: 1083 additions & 163 deletions

aiche_delete_l8r.png

95.3 KB
Loading

aiche_delete_l8r_blue.png

82.2 KB
Loading

aiche_delete_l8r_orange.png

66.3 KB
Loading

aiche_plots.py

Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
import matplotlib.pyplot as plt
2+
from scipy import stats
3+
import numpy as np
4+
5+
6+
# Set global plot settings
7+
plt.rcParams['figure.figsize'] = (8, 6)
8+
plt.rcParams['figure.dpi'] = 300
9+
plt.rcParams['axes.labelsize'] = 16
10+
plt.rcParams['xtick.labelsize'] = 15
11+
plt.rcParams['ytick.labelsize'] = 15
12+
plt.rcParams['legend.fontsize'] = 12
13+
plt.rcParams['lines.linewidth'] = 3
14+
plt.rcParams['lines.markersize'] = 8
15+
plt.rcParams['axes.labelweight'] = 'bold'
16+
plt.rcParams['xtick.direction'] = 'in'
17+
plt.rcParams['ytick.direction'] = 'in'
18+
plt.rcParams['xtick.top'] = True
19+
plt.rcParams['ytick.right'] = True
20+
plt.rcParams['savefig.bbox'] = 'tight'
21+
22+
23+
if False:
24+
U = np.linspace(-4,4, 200)
25+
u = stats.norm.pdf(U, loc = 0, scale = 1)
26+
pi = 0.2
27+
z = pi * stats.norm.pdf(U, loc = -2, scale = 0.5) + (1 - pi) * stats.norm.pdf(U, loc = 1, scale = 0.5)
28+
29+
plt.figure()
30+
plt.plot(U,u, linewidth = 3)
31+
plt.fill_between(U, u * 0, u, alpha = 0.5)
32+
plt.savefig("aiche_delete_l8r_blue", dpi = 300)
33+
plt.close()
34+
35+
plt.figure()
36+
plt.plot(U,z, linewidth = 3, color = "r")
37+
plt.fill_between(U, u * 0, z, alpha = 0.5, color = "r")
38+
plt.savefig("aiche_delete_l8r_orange", dpi = 300)
39+
plt.close()
40+
41+
if False:
42+
X = np.linspace(-5, 5, 200)
43+
shift = 6
44+
scale = 1
45+
x1 = stats.norm.pdf(X, 0 , 1)
46+
x2 = 0.75 * stats.norm.pdf(X - shift, 0 - shift, 1.5 * scale)
47+
x3 = 0.9 * stats.norm.pdf(X + shift, 0 + shift, 1.25 * scale)
48+
49+
plt.figure(figsize=(10, 4))
50+
51+
52+
plt.plot(X - shift, x2, linewidth = 3, color = "r")
53+
plt.fill_between(X - shift, x2 * 0, x2, alpha = 0.3, color = "r")
54+
55+
plt.plot(X + shift, x3, linewidth = 3, color = "b")
56+
plt.fill_between(X + shift, x3 * 0, x3, alpha = 0.3, color = "b")
57+
58+
plt.plot(X, x1, linewidth = 3, color = "g")
59+
plt.fill_between(X, x1 * 0, x1, alpha = 0.3, color = "g")
60+
61+
plt.plot(X, x1*0, linewidth = 3, color = "k")
62+
plt.plot(X - shift, x1*0, linewidth = 3, color = "k")
63+
plt.plot(X + shift, x1*0, linewidth = 3, color = "k")
64+
plt.savefig("kl_divergence", dpi = 300)
65+
66+
import numpy as np
67+
import matplotlib.pyplot as plt
68+
from scipy.stats import norm
69+
70+
# Observed data
71+
np.random.seed(0)
72+
x_observed = np.random.normal(loc=2.5, scale=1.0, size=10) # Data with unknown mean and known variance
73+
74+
# Known parameters
75+
sigma = 1.0 # Known standard deviation of the observations
76+
n = len(x_observed) # Number of observations
77+
sample_mean = np.mean(x_observed)
78+
79+
# Prior parameters
80+
mu_0 = 0.0 # Prior mean
81+
tau_0 = 1.0 # Prior standard deviation
82+
83+
# Posterior parameters
84+
tau_n_sq = 1 / (1 / tau_0**2 + n / sigma**2) # Posterior variance
85+
mu_n = tau_n_sq * (mu_0 / tau_0**2 + n * sample_mean / sigma**2) # Posterior mean
86+
tau_n = np.sqrt(tau_n_sq) # Posterior standard deviation
87+
88+
# Range of mu values
89+
mu_values = np.linspace(1.5, 4.5, 200)
90+
91+
# Calculate unnormalized posterior (prior * likelihood)
92+
prior = norm.pdf(mu_values, mu_0, tau_0)
93+
likelihood = norm.pdf(mu_values, sample_mean, sigma / np.sqrt(n))
94+
unnormalized_posterior = prior * likelihood
95+
96+
# Scale up unnormalized posterior for visibility
97+
unnormalized_posterior_scaled = unnormalized_posterior * 200 # Adjust this factor as needed
98+
99+
# Calculate normalized posterior
100+
normalized_posterior = norm.pdf(mu_values, mu_n, tau_n)
101+
102+
# Plotting
103+
plt.figure(figsize=(5, 5))
104+
105+
# Unnormalized posterior with shading (scaled up)
106+
plt.plot(mu_values, unnormalized_posterior_scaled, label="Unnormalized", linestyle="--", color="blue")
107+
plt.fill_between(mu_values, unnormalized_posterior_scaled, color="blue", alpha=0.2)
108+
109+
# Normalized posterior with shading
110+
plt.plot(mu_values, normalized_posterior, label="Normalized", color="red")
111+
plt.fill_between(mu_values, normalized_posterior, color="red", alpha=0.2)
112+
113+
# Annotation to highlight normalization importance
114+
plt.text(4.0, 0.8, "Area = 1", color="red", fontsize=16, ha="center", backgroundcolor="white")
115+
plt.text(4.0, 0.5, "Area < 1", color="blue", fontsize=16, ha="center", backgroundcolor="white")
116+
117+
# Labels and legend
118+
plt.xlabel(r"$\mu$")
119+
plt.ylabel("Density")
120+
plt.legend(loc="upper right", fontsize = 14)
121+
plt.ylim(0,1.4)
122+
123+
plt.savefig("normalized_density_fig", dpi = 300)
124+
125+
import os
126+
import torch
127+
from linfa.models.discrepancy_models import PhysChem
128+
import matplotlib.pyplot as plt
129+
130+
131+
132+
samples = np.loadtxt('results/TP15_no_disc_error_estimation_aiche/TP15_no_disc_error_estimation_aiche_outputs_lf+noise_6000')
133+
observations = np.loadtxt("observations.csv", skiprows=1, delimiter=',')
134+
135+
136+
# Set variable grid
137+
T = [50.0, 400.0, 450.0]
138+
P = [1.0, 2.0, 3.0, 4.0, 5.0]
139+
140+
samples = samples.reshape(3,5,5000)
141+
142+
143+
# Plot the samples
144+
for i in range(5000):
145+
plt.plot(P, samples[0, :, i], 'm-', linewidth=0.005)
146+
plt.plot(P, samples[1, :, i], 'r-', linewidth=0.005)
147+
plt.plot(P, samples[2, :, i], color="orange", linestyle='-', linewidth=0.005)
148+
149+
# Plot observations
150+
plt.plot(observations[:, 1], observations[:, 2], 'ko')
151+
152+
# Add custom legend entries by plotting representative lines
153+
plt.plot([], [], 'm-', label="350 K") # Magenta line
154+
plt.plot([], [], 'r-', label="400 K") # Red line
155+
plt.plot([], [], color="orange", linestyle='-', label="450 K") # Orange line
156+
plt.plot([], [], 'ko', label="Observations") # Black circles for observations
157+
158+
# Add legend, labels, and limits
159+
plt.legend()
160+
plt.xlim(1, 5)
161+
plt.xlabel("Pressure, $P$ [Pa]")
162+
plt.ylabel("Coverage")
163+
plt.savefig("function", dpi=300)
164+
165+
# for i in range(5000):
166+
# plt.plot(P, samples[0,:,i],'m-', linewidth = 0.005)
167+
# plt.plot(P, samples[1,:,i],'r-', linewidth = 0.005)
168+
# plt.plot(P, samples[2,:,i],color = "orange", linestyle = '-', linewidth = 0.005)
169+
# plt.plot(observations[:,1], observations[:,2], 'ko')
170+
# plt.xlim(1,5)
171+
# plt.xlabel("Pressure, $P$ [Pa]")
172+
# plt.ylabel("Coverage")
173+
# plt.savefig("function", dpi = 300)
174+
# exit()

error_calcs.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# import numpy as np
2+
# import os
3+
4+
# # Define file path
5+
# filename = "TP15_no_disc_error_estimation_2"
6+
# path = os.path.join("results", filename)
7+
# iters = 10000
8+
9+
# # Load data
10+
# mcmc = np.loadtxt(os.path.join(path, 'mcmc'))
11+
# linfa = np.loadtxt(os.path.join(path, f"{filename}_params_{iters}"))
12+
13+
# # Ground truth parameters
14+
# gt_params = np.array([1.0E3, -21.0E3, 0.05])
15+
16+
# # Calculate RMSE for mcmc
17+
# diff_mcmc = (mcmc - gt_params) ** 2
18+
# rmse_mcmc = np.sqrt(np.mean(diff_mcmc))
19+
20+
# # Calculate RMSE for linfa
21+
# diff_linfa = (linfa - gt_params) ** 2
22+
# rmse_linfa = np.sqrt(np.mean(diff_linfa))
23+
24+
# print("RMSE for MCMC:", rmse_mcmc)
25+
# print("RMSE for Linfa:", rmse_linfa)
26+
27+
import numpy as np
28+
import os
29+
30+
# Define file path
31+
filename = "TP15_no_disc_error_estimation_aiche"
32+
path = os.path.join("results", filename)
33+
iters = 6000
34+
35+
# Load data
36+
mcmc = np.loadtxt(os.path.join(path, 'mcmc'))
37+
linfa = np.loadtxt(os.path.join(path, f"{filename}_params_{iters}"))
38+
39+
# Ground truth parameters
40+
gt_params = np.array([1.0E3, -21.0E3, 0.05])
41+
42+
# Calculate RMSE for mcmc
43+
diff_mcmc = (mcmc - gt_params) ** 2
44+
rmse_mcmc = np.sqrt(np.mean(diff_mcmc))
45+
46+
# Calculate RMSE for linfa
47+
diff_linfa = (linfa - gt_params) ** 2
48+
rmse_linfa = np.sqrt(np.mean(diff_linfa))
49+
50+
# Calculate the average standard deviation of β samples for mcmc
51+
std_dev_mcmc = np.std(mcmc, axis=0) # Standard deviation for each parameter
52+
avg_beta_sd_mcmc = np.mean(std_dev_mcmc) # Average standard deviation across parameters
53+
54+
# Calculate the average standard deviation of β samples for linfa
55+
std_dev_linfa = np.std(linfa, axis=0) # Standard deviation for each parameter
56+
avg_beta_sd_linfa = np.mean(std_dev_linfa) # Average standard deviation across parameters
57+
58+
print("RMSE for MCMC:", rmse_mcmc)
59+
print("RMSE for Linfa:", rmse_linfa)
60+
print("Average standard deviation of β samples for MCMC:", avg_beta_sd_mcmc)
61+
print("Average standard deviation of β samples for Linfa:", avg_beta_sd_linfa)

function.png

2 MB
Loading

kl_divergence.png

111 KB
Loading

linfa/models/discrepancy_models.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,11 @@ def solve_true(self, cal_inputs):
119119

120120
# compute equilibrium constant of site one
121121
k1Const = 1.0/p01Const * torch.exp(-e1Const / self.RConst / T)
122+
print(k1Const)
122123

123124
# compute equilibrium constant of site two
124125
k2Const = 1.0/p02Const * torch.exp(-e2Const / self.RConst / T)
125-
126+
print(k2Const)
126127
# compute surface coverage fraction for two adsorption sites with different equilibrium conditions
127128
cov_frac = lambda1Const * (k1Const*P/(1 + k1Const*P)) + lambda2Const * (k2Const*P/(1 + k2Const*P))
128129

linfa/plot_res.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,18 @@ def plot_log(log_file,out_dir,fig_format='png', use_dark_mode=False):
2626
log_data = np.loadtxt(log_file)
2727
# loss profile
2828
plt.figure()
29-
plt.plot(log_data[:, 1],log_data[:, 2],'b-', label = 'ELBO')
30-
plt.plot(log_data[:, 1],log_data[:, 3],'g-', label = 'Sum Log Jacobian')
31-
plt.plot(log_data[:, 1],log_data[:, 4],'m-', label = 'Likelihood')
32-
plt.plot(log_data[:, 1],log_data[:, 5],'k-', label = 'Prior')
29+
30+
plt.plot(log_data[:, 1], log_data[:, 2],'b-', label = 'ELBO')
31+
# plt.plot(log_data[:, 1],log_data[:, 3],'g-', label = 'Sum Log Jacobian')
32+
# plt.plot(log_data[:, 1],log_data[:, 4],'m-', label = 'Likelihood')
33+
# plt.plot(log_data[:, 1],log_data[:, 5],'k-', label = 'Prior')
3334
plt.xlabel('Iterations')
3435
plt.ylabel('Log Loss')
3536
plt.legend()
3637
plt.savefig(os.path.join(out_dir,'log_plot.'+fig_format))
3738
plt.close()
39+
print("success")
40+
exit()
3841

3942
def plot_params(param_data,LL_data,idx1,idx2,out_dir,out_info,fig_format='png', use_dark_mode=False):
4043

linfa/tests/test_no_disc_TP15_error_estimation.py

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@
1313
def run_test():
1414

1515
exp = experiment()
16-
exp.name = "TP15_no_disc_error_estimation"
16+
exp.name = "TP15_no_disc_error_estimation_2"
1717
exp.flow_type = 'realnvp' # str: Type of flow (default 'realnvp') # TODO: generalize to work for TP1
18-
exp.n_blocks = 50 # int: Number of hidden layers
19-
exp.hidden_size = 10 # int: Hidden layer size for MADE in each layer (default 100)
18+
exp.n_blocks = 1 # int: Number of hidden layers
19+
exp.hidden_size = 10 # int: Hidden layer size for MADE in each layer (default 100)
2020
exp.n_hidden = 1 # int: Number of hidden layers in each MADE
2121
exp.activation_fn = 'relu' # str: Activation function used (default 'relu')
2222
exp.input_order = 'sequential' # str: Input oder for create_mask (default 'sequential')
@@ -25,10 +25,10 @@ def run_test():
2525

2626
# p0,e,sigma_e (measurement noise also estimated)
2727
exp.input_size = 3 # int: Dimensionalty of input (default 2)
28-
exp.batch_size = 300 # int: Number of samples generated (default 100)
28+
exp.batch_size = 100 # int: Number of samples generated (default 100)
2929
exp.true_data_num = 1 # double: Number of true model evaluted (default 2)
30-
exp.n_iter = 4000 # int: Number of iterations (default 25001)
31-
exp.lr = 0.0005 # float: Learning rate (default 0.003)
30+
exp.n_iter = 10000 # int: Number of iterations (default 25001)
31+
exp.lr = 0.001 # float: Learning rate (default 0.003)
3232
exp.lr_decay = 0.9999 # float: Learning rate decay (default 0.9999)
3333
exp.log_interval = 10 # int: How often to show loss stat (default 10)
3434

@@ -56,9 +56,10 @@ def run_test():
5656
exp.device = torch.device('cuda:0' if torch.cuda.is_available() and not exp.no_cuda else 'cpu')
5757

5858
# Define transformation
59-
trsf_info = [['tanh', -40.0, 30.0, 500.0, 5000.0],
59+
trsf_info = [['tanh', -50.0, 100.0, 500.0, 1500.0],
6060
['tanh', -30.0, 30.0, -30000.0, -15000.0],
61-
['tanh', -20.0, 20.0, 0.00001, 0.2]]
61+
['tanh', -10.0, 20.0, 0.00001, 0.2]]
62+
6263
trsf = Transformation(trsf_info)
6364

6465
# Apply the transformation
@@ -152,14 +153,12 @@ def log_prior(calib_inputs, transform):
152153
# Compute the calibration inputs in the physical domain
153154
phys_inputs = transform.forward(calib_inputs)
154155

155-
# Gaussian prior on physically meaningful inputs
156-
mean = np.array([1.0E3, -21.0E3])
157-
cov = np.array([[100.0**2, 0.0],
158-
[0.0, 500.0**2]])
159-
gauss_prior_res = torch.from_numpy(stats.multivariate_normal.logpdf(phys_inputs[:,:2].detach().numpy(), mean = mean, cov = cov))
160-
156+
# Gaussian prior on physically meaningful inputs
157+
mean = torch.tensor([1.0E3, -21.0E3])
158+
cov = torch.diag(torch.tensor([100.0**2, 500.0**2]))
159+
gauss_prior_res = torch.distributions.MultivariateNormal(mean, cov).log_prob(phys_inputs[:,:2])
160+
161161
# Beta prior on noise
162-
#torch.distributions.gamma.Gamma(torch.tensor([2.0]), torch.tensor([2.0]))
163162
std_dev_ratio = torch.distributions.beta.Beta(torch.tensor([1.0]), torch.tensor([19.0]))
164163

165164
if False:
@@ -196,6 +195,6 @@ def generate_data(use_true_model = False, num_observations=50):
196195
# Main code
197196
if __name__ == "__main__":
198197

199-
#generate_data(use_true_model = False, num_observations = 1)
198+
# generate_data(use_true_model = False, num_observations = 1)
200199

201200
run_test()

0 commit comments

Comments
 (0)