Skip to content

Commit

Permalink
updated test scripts to save surrogate to experiment results, added s…
Browse files Browse the repository at this point in the history
…ave features to plot_disc, added axis labels to plot_disc
  • Loading branch information
kylajones committed Dec 12, 2023
1 parent 6b30d53 commit 4d01629
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 25 deletions.
36 changes: 21 additions & 15 deletions linfa/plot_disc.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ 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, data_file, sample_size = 10):
def plot_disr_histograms(lf_file, lf_dicr_file, lf_discr_noise_file, data_file, out_dir, sample_size = 500):

# Read result files
lf_model = np.loadtxt(lf_file)
Expand All @@ -29,13 +29,16 @@ def plot_disr_histograms(lf_file, lf_dicr_file, lf_discr_noise_file, data_file,

if num_dim == 1:
# Plot histograms
plt.figure(figsize = (4,4))
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()
plt.tight_layout()
plt.savefig(out_dir+'hist.png', bbox_inches = 'tight', dpi = 200)
plt.close()

else:

Expand All @@ -49,6 +52,7 @@ def plot_disr_histograms(lf_file, lf_dicr_file, lf_discr_noise_file, data_file,
sample = np.zeros([len(temps), len(pressures), sample_size]) # Initialize

# For plotting
plt.figure(figsize = (4,4))
clrs = ['b', 'm', 'r'] # Line colors for each temperature
lines = [] # List to store Line2D objects for legend lines

Expand Down Expand Up @@ -78,16 +82,15 @@ def plot_disr_histograms(lf_file, lf_dicr_file, lf_discr_noise_file, data_file,

plt.xlabel('Pressure, [Pa]')
plt.ylabel('Coverage, [ ]')
plt.tight_layout()
plt.savefig(out_dir+'hist.png',bbox_inches='tight',dpi=200)
plt.close()

plt.show()

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

# TODO: need to rewrite conditional statements to raise error message when there is only one measurement
exp_name = os.path.basename(file_path)
dir_name = os.path.dirname(file_path)

print(exp_name)
print(dir_name)
dir_name = os.path.dirname(file_path)

# Create new discrepancy
dicr = Discrepancy(model_name=exp_name,
Expand All @@ -101,6 +104,7 @@ def plot_discr_surface_2d(file_path,data_file,num_1d_grid_points,data_limit_fact

# Get the number of dimensions for the aux variable
num_dim = dicr.var_grid_in.size(1)

if(num_dim == 2):
min_dim_1 = torch.min(dicr.var_grid_in[:,0])
max_dim_1 = torch.max(dicr.var_grid_in[:,0])
Expand All @@ -120,11 +124,13 @@ def plot_discr_surface_2d(file_path,data_file,num_1d_grid_points,data_limit_fact
y = test_grid[:,1].cpu().detach().numpy()
z = res.cpu().detach().numpy().flatten()

print(x.shape,y.shape,z.shape)

ax = plt.figure().add_subplot(projection='3d')
ax = plt.figure(figsize = (4,4)).add_subplot(projection='3d')
ax.plot_trisurf(x,y,z,linewidth=0.2, antialiased=True)
plt.show()
plt.xlabel('Temperature, [K]')
plt.ylabel('Pressure, [Pa]')
plt.tight_layout()
plt.savefig(out_dir+'disc_surf.png',bbox_inches='tight',dpi=200)
plt.close()

else:
print('ERROR. Invalid number of dimensions. Should be 2. Instead is ',num_dim)
Expand Down Expand Up @@ -268,9 +274,9 @@ 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,data_file)
plot_disr_histograms(lf_file, lf_dicr_file, lf_discr_noise_file, data_file, out_dir)
elif(args.result_mode == 'discr_surface'):
plot_discr_surface_2d(discr_sur_file,data_file,args.num_1d_grid_points,args.data_limit_factor)
plot_discr_surface_2d(discr_sur_file,data_file,args.num_1d_grid_points,args.data_limit_factor, out_dir)
else:
print('ERROR. Invalid execution mode')
exit(-1)
Expand Down
3 changes: 2 additions & 1 deletion linfa/tests/test_discr_08_prior_TP1.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ def run_test():

# Create new discrepancy
exp.surrogate = Discrepancy(model_name=exp.name,
model_folder=exp.output_dir,
lf_model=exp.model.solve_t,
input_size=exp.model.var_in.size(1),
output_size=1,
Expand All @@ -93,7 +94,7 @@ def run_test():
# exp.surrogate.update(langmuir_model.defParams, exp.surr_pre_it, 0.03, 0.9999, 100, store=True)
# exp.surrogate.update(langmuir_model.defParams, 1, 0.03, 0.9999, 100, store=True)
# Load the surrogate
# exp.surrogate.surrogate_load()
# exp.surrogate.surrogate_loaCCd()
else:
exp.surrogate = None

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 = 2501 # int: Number of iterations (default 25001)
exp.n_iter = 25001 # 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
1 change: 1 addition & 0 deletions linfa/tests/test_discr_18_prior_TP15_rep_meas.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ def run_test():

# Create new discrepancy
exp.surrogate = Discrepancy(model_name=exp.name,
model_folder=exp.output_dir,
lf_model=exp.model.solve_t,
input_size=exp.model.var_in.size(1),
output_size=1,
Expand Down
16 changes: 8 additions & 8 deletions linfa/tests/test_plot_discr.sh
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_TP1_rep_meas \
--iter 2000 \
--name test_lf_with_disc_hf_data_prior_TP15_rep_meas \
--iter 15000 \
--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 \
python3 -m linfa.plot_disc --folder results/ \
--name test_lf_with_disc_hf_data_prior_TP15_rep_meas \
--iter 15000 \
--mode discr_surface \
--num_points 40 \
--limfactor 2.0 \

0 comments on commit 4d01629

Please sign in to comment.