Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
DataAnalysis/visualizing_gpr.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
134 lines (97 sloc)
4.11 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import sys | |
import matplotlib.pyplot as plt | |
import os | |
import pickle | |
sys.path.append(r"C:\Users\hoodl\Documents\GitHub\DataAnalysis") | |
from GPR_Optimizer import GPR_Optimizer | |
import time | |
import h5py | |
#%% | |
# Function to plot data from an .h5 file | |
def plot_from_h5(file_path, x_dataset_name, y_dataset_name): | |
# Open the .h5 file | |
with h5py.File(file_path, 'r') as file: | |
# The datasets are in the 'datasets' group, so we prefix the names with 'datasets/' | |
x_data = file[f'datasets/{x_dataset_name}'][:] | |
y_data = file[f'datasets/{y_dataset_name}'][:] | |
# Plotting the graph with points only, no lines | |
plt.figure(figsize=(10, 5)) | |
plt.scatter(x_data, y_data, marker='o') # Changed from plot to scatter for points | |
plt.xlabel(x_dataset_name) | |
plt.ylabel(y_dataset_name) | |
plt.title(f'{y_dataset_name} as a Function of {x_dataset_name}') | |
plt.grid(True) | |
plt.show() | |
def print_most_recently_modified_file(directory): | |
# Ensure the provided directory exists | |
if not os.path.exists(directory): | |
print(f"The directory {directory} does not exist.") | |
return | |
# Initialize variables to track the most recent file | |
most_recent_file = None | |
most_recent_mod_time = 0 | |
# Iterate over all files in the directory | |
for filename in os.listdir(directory): | |
file_path = os.path.join(directory, filename) | |
# Check if it's a file and not a directory | |
if os.path.isfile(file_path): | |
mod_time = os.path.getmtime(file_path) | |
# Update the most recent file and its modification time | |
if mod_time > most_recent_mod_time: | |
most_recent_mod_time = mod_time | |
most_recent_file = filename | |
print(f"Importing: {most_recent_file}") | |
return str(directory)+str(most_recent_file) | |
#%% | |
loop = False | |
if loop == True: | |
while 1: | |
path = print_most_recently_modified_file(r'C:\\Users\\hoodl\\Desktop\\save_opt_folder\\') | |
with open(path, 'rb') as file: opt = pickle.load(file) | |
res = opt.get_result() | |
print("Best Point:", np.round(res.x,3), res.fun) | |
plt.figure() | |
opt.plot_objective() | |
plt.title("Best Point:"+str(np.round(res.x,3))+str(res.fun), y=3.5) | |
plt.show() | |
time.sleep(10) | |
else: | |
path = print_most_recently_modified_file(r'C:\\Users\\hoodl\\Desktop\\save_opt_folder\\') | |
with open(path, 'rb') as file: opt = pickle.load(file) | |
res = opt.get_result() | |
# print("Best Point:", np.round(res.x,3), res.fun) | |
for i in np.argsort(res['func_vals'])[:5]: | |
print(repr(np.round(res['x_iters'][i],3)), res['func_vals'][i]) | |
print('\n') | |
try: | |
plt.figure() | |
opt.plot_convergence() | |
plt.show() | |
# plt.figure() | |
# opt.plot_objective() # Partial dependence (can take > 1 min to run for large num of variable) | |
# plt.title("Best Point:"+str(np.round(res.x,3))+str(res.fun), y=3.5) | |
# plt.show() | |
plt.figure() | |
opt.plot_hyperparameters() | |
plt.show() | |
plt.figure() | |
opt.plot_cross_section() # X-section through best point (let me know if you want predicted best point rather than best point) | |
plt.show() | |
print("Predicted best:", np.round(opt.expected_minimum()[0],3)) | |
except Exception as e: | |
print("Only",len(res['x_iters']),"points so far!",e) | |
#%% | |
# plot_from_h5(r'C:\Users\hoodl\Documents\GitHub\Artiq\Experiments\results\2024-01-19\20\000061050-gpr_manager.h5', | |
# 'variable', 'loading_rate_roi0') | |
#%% | |
if 0: | |
print("self.v subs:", | |
repr(np.round( | |
[1.966+0.066-0.386+-0.106, -2.041+0.99 -3.585 +3.647, 1.115-0.995+5.558+1.627, -5.313-0.371-0.333+0.139, -1.2] | |
,3))) | |
if 1: | |
x = repr(np.round( | |
[ 1.54-0.33-1.48, -0.989-2.325-0.32, 7.305+1.646+0.128, -5.878+2.10-1.15 ]+[0.488-0.213 -0.173+0.671, -1.044+0.505+1.217+1.065, 8.714+0.551-2.43-1.391, -9.288+3.58+0.25-3.49] | |
,3)) | |
opt_x = [ 2.081, -0.374, -0.746, -0.094, -0.031, 0.08 , 0.898, -0.23 ] | |