Skip to content

Commit

Permalink
updated script to plot true process posterior across multiple tempera…
Browse files Browse the repository at this point in the history
…tures and pressures and deleted unneeded comment on test 9
  • Loading branch information
kylajones committed Dec 11, 2023
1 parent 23a9a7a commit 370b9b7
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 23 deletions.
70 changes: 60 additions & 10 deletions linfa/plot_disc.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import numpy as np
import os
import argparse
from numpy import random
import array

def scale_limits(min,max,factor):
if(min>max):
Expand All @@ -14,21 +16,69 @@ def scale_limits(min,max,factor):
range = max - min
return center - factor*0.5*range, center + factor*0.5*range

def plot_disr_histograms(lf_file,lf_dicr_file,lf_discr_noise_file):
def plot_disr_histograms(lf_file, lf_dicr_file, lf_discr_noise_file, data_file, sample_size = 500):

# Read result files
lf_model = np.loadtxt(lf_file)
lf_model_plus_disc = np.loadtxt(lf_dicr_file)
lf_model_plus_disc_plus_noise = np.loadtxt(lf_discr_noise_file)
data = np.loadtxt(data_file)

# Plot histograms
plt.hist(lf_model, label = 'LF', alpha =0.5)
plt.hist(lf_model_plus_disc, label = 'LF + disc', alpha =0.5)
plt.hist(lf_model_plus_disc_plus_noise, label = 'LF + disc + noise', alpha = 0.5)
plt.xlabel('Coverage')
plt.ylabel('Frequency')
plt.legend()
plt.show()
# Check for multiple TP-pairs
## shape : no. var inputs pairs x no. batches
num_dim = len(np.shape(lf_model))

if num_dim == 1:
# Plot histograms
plt.hist(lf_model, label = 'LF', alpha = 0.5, density = True)
plt.hist(lf_model_plus_disc, label = 'LF + disc', alpha = 0.5, density = True)
plt.hist(lf_model_plus_disc_plus_noise, label = 'LF + disc + noise', alpha = 0.5, density = True)
plt.xlabel('Coverage')
plt.ylabel('Density')
plt.legend()
plt.show()

else:

batch_size = len(lf_model_plus_disc[0])
temps = np.unique(data[:, 0])
pressures = np.unique(data[:, 1])

## Prepare for random sampling of batches
lf_model_plus_disc = lf_model_plus_disc.reshape(len(temps), len(pressures), batch_size)
random_array = np.random.randint(low = 0, high = batch_size, size = sample_size) # Randomly sample batch numbers without replacement
sample = np.zeros([len(temps), len(pressures), sample_size]) # Initialize

# For plotting
clrs = ['b', 'm', 'r'] # Line colors for each temperature
lines = [] # List to store Line2D objects for legend lines

# Loop over temperatures
for loopA, temp in enumerate(temps):

# Loop over pressures
for loopB, pressure in enumerate(pressures):

# Evalute random sample of true process posterior
sample[loopA, loopB] = lf_model_plus_disc[loopA, loopB, random_array]

# Plot function & save line properties for legend
line = plt.plot(np.tile(pressures, (sample_size, 1)).transpose(),
sample[loopA],
linewidth=0.05,
color=clrs[loopA])[0]
lines.append(line)

# Manually create the legend with custom linewidth
legend = plt.legend(lines, ['{} K'.format(temp) for temp in temps])

# Set the linewidth for the legend lines
for line in legend.get_lines():
line.set_linewidth(2.0) # Adjust the linewidth as needed

plt.xlabel('Pressure, [Pa]')
plt.ylabel('Coverage, [ ]')
plt.show()

def plot_discr_surface_2d(file_path,data_file,num_1d_grid_points,data_limit_factor):

Expand Down Expand Up @@ -217,7 +267,7 @@ def eval_discrepancy_custom_grid(file_path,train_grid_in,train_grid_out,test_gri
# out_info = args.exp_name + '_' + str(args.step_num)

if(args.result_mode == 'histograms'):
plot_disr_histograms(lf_file,lf_dicr_file,lf_discr_noise_file)
plot_disr_histograms(lf_file,lf_dicr_file,lf_discr_noise_file,data_file)
elif(args.result_mode == 'discr_surface'):
plot_discr_surface_2d(discr_sur_file,data_file,args.num_1d_grid_points,args.data_limit_factor)
else:
Expand Down
2 changes: 1 addition & 1 deletion linfa/tests/test_discr_09_prior_TP15.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def run_test():
exp.input_size = 2 # int: Dimensionalty of input (default 2)
exp.batch_size = 200 # int: Number of samples generated (default 100)
exp.true_data_num = 2 # double: Number of true model evaluted (default 2)
exp.n_iter = 25001 # int: Number of iterations (default 25001)
exp.n_iter = 2501 # int: Number of iterations (default 25001)
exp.lr = 0.001 # float: Learning rate (default 0.003)
exp.lr_decay = 0.9999 # float: Learning rate decay (default 0.9999)
exp.log_interal = 10 # int: How often to show loss stat (default 10)
Expand Down
24 changes: 12 additions & 12 deletions linfa/tests/test_plot_discr.sh
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
# discr_surface
# python3 -m linfa.plot_disc --folder results/ \
# --name test_lf_with_disc_hf_data_prior_TP15 \
# --iter 2000 \
# --mode histograms \
# --num_points 10 \
# --limfactor 1.0 \



python3 -m linfa.plot_disc --folder results/ \
--name test_lf_with_disc_hf_data_prior_TP15 \
--iter 2000 \
--mode discr_surface \
--num_points 40 \
--limfactor 2.0 \
--mode histograms \
--num_points 10 \
--limfactor 1.0 \



# python3 -m linfa.plot_disc --folder results/ \
# --name test_lf_with_disc_hf_data_prior_TP15 \
# --iter 2000 \
# --mode discr_surface \
# --num_points 40 \
# --limfactor 2.0 \

0 comments on commit 370b9b7

Please sign in to comment.