A very basic exploration
In this example, we will perform a basic exploration with mopet
. To demonstrate the usage, we will simply scan a 2D surface and, at each point, return a number that is equal to the distance to a unit circle. Of course, this is not a realistic experiment for parameter exploration but it serves as a simplified example.
# change into the root directory of the project
import os
if os.getcwd().split("/")[-1] == "examples":
os.chdir('..')
%load_ext autoreload
%autoreload 2
import logging
logger = logging.getLogger()
logger.setLevel(logging.INFO)
import numpy as np
!pip install matplotlib
import matplotlib.pyplot as plt
import mopet
# a nice color map
plt.rcParams['image.cmap'] = 'plasma'
def evalFunction(params):
result_float = abs((params["x"] ** 2 + params["y"] ** 2) - 1)
result_array = np.random.randn(np.random.randint(1, 131), np.random.randint(1, 5000))
result = {}
result["float_result"] = result_float
result["array_result"] = result_array
return result
explore_params = {"x": np.linspace(-2, 2, 21), "y": np.linspace(-2, 2, 21)}
# we need this random filename to avoid testing clashes
hdf_filename = f"exploration-{np.random.randint(99999)}.h5"
ex = mopet.Exploration(evalFunction, explore_params, hdf_filename=hdf_filename)
ex.run()
ex.load_results(arrays=True)
len(ex.results)
ex.get_run(0)
for r in ex.df.index:
ex.df.loc[r, "mean_array_result"] = np.mean(ex.get_run(r)['array_result'])
ex.df.dropna(axis='columns', how='all')
pivoted = ex.df.pivot_table(values='float_result', index = 'y', columns='x', aggfunc='first')
plt.imshow(pivoted, \
extent = [min(ex.df.x), max(ex.df.x),
min(ex.df.y), max(ex.df.y)], origin='lower')
plt.colorbar(label='Distance from unit circle')
plt.xlabel("x")
plt.ylabel("y")