Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix incorrect types in pypesto.result.profile.ProfilerResult #1210

Merged
merged 3 commits into from
Nov 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions pypesto/profile/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,11 +183,11 @@ def fill_profile_list(

# create blank profile
new_profile = ProfilerResult(
x_path=optimizer_result["x"],
x_path=optimizer_result["x"][..., np.newaxis],
fval_path=np.array([optimizer_result["fval"]]),
ratio_path=np.array([np.exp(global_opt - optimizer_result["fval"])]),
gradnorm_path=gradnorm,
exitflag_path=optimizer_result["exitflag"],
gradnorm_path=np.array([gradnorm]),
exitflag_path=np.array([optimizer_result["exitflag"]]),
time_path=np.array([0.0]),
time_total=0.0,
n_fval=0,
Expand Down
78 changes: 43 additions & 35 deletions pypesto/result/profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ def __init__(
x_path: np.ndarray,
fval_path: np.ndarray,
ratio_path: np.ndarray,
gradnorm_path: np.ndarray = np.nan,
exitflag_path: np.ndarray = np.nan,
time_path: np.ndarray = np.nan,
gradnorm_path: np.ndarray = None,
exitflag_path: np.ndarray = None,
time_path: np.ndarray = None,
time_total: float = 0.0,
n_fval: int = 0,
n_grad: int = 0,
Expand All @@ -64,19 +64,41 @@ def __init__(
super().__init__()

# initialize profile path
x_shape = x_path.shape
if len(x_shape) == 1:
self.x_path = np.zeros((x_shape[0], 1))
self.x_path[:, 0] = x_path[:]
if not x_path.ndim == 2:
raise ValueError("x_path must be a 2D array.")

self.x_path = x_path.copy()
self.fval_path = fval_path.copy()
self.ratio_path = ratio_path.copy()

if gradnorm_path is None:
self.gradnorm_path = np.full(x_path.shape[1], np.nan)
else:
self.gradnorm_path = gradnorm_path.copy()

if exitflag_path is None:
self.exitflag_path = np.full(x_path.shape[1], np.nan)
else:
self.x_path = np.zeros((x_shape[0], x_shape[1]))
self.x_path[:, :] = x_path[:, :]

self.fval_path = np.asarray(fval_path)
self.ratio_path = np.asarray(ratio_path)
self.gradnorm_path = np.asarray(gradnorm_path)
self.exitflag_path = np.asarray(exitflag_path)
self.time_path = np.asarray(time_path)
self.exitflag_path = exitflag_path.copy()

if time_path is None:
self.time_path = np.full(x_path.shape[1], np.nan)
else:
self.time_path = time_path.copy()

if (
not self.x_path.shape[1]
== len(self.fval_path)
== len(self.ratio_path)
== len(self.gradnorm_path)
== len(self.exitflag_path)
== len(self.time_path)
):
raise ValueError(
"x_path, fval_path, ratio_path, gradnorm_path, exitflag_path, "
"time_path must have the same length."
)

self.time_total = time_total
self.n_fval = n_fval
self.n_grad = n_grad
Expand Down Expand Up @@ -131,26 +153,12 @@ def append_profile_point(
n_hess:
Number of Hessian evaluations performed to find `x`.
"""

# short function to append to numpy vectors
def append_to_vector(field_name, val):
field_new = np.zeros(self[field_name].size + 1)
field_new[0:-1] = self[field_name]
field_new[-1] = val
self[field_name] = field_new

# write profile path
x_new = np.zeros((self.x_path.shape[0], self.x_path.shape[1] + 1))
x_new[:, 0:-1] = self.x_path
x_new[:, -1] = x
self.x_path = x_new

# append to other paths
append_to_vector("fval_path", fval)
append_to_vector("ratio_path", ratio)
append_to_vector("gradnorm_path", gradnorm)
append_to_vector("exitflag_path", exitflag)
append_to_vector("time_path", time)
self.x_path = np.hstack((self.x_path, x[..., np.newaxis]))
self.fval_path = np.hstack((self.fval_path, fval))
self.ratio_path = np.hstack((self.ratio_path, ratio))
self.gradnorm_path = np.hstack((self.gradnorm_path, gradnorm))
self.exitflag_path = np.hstack((self.exitflag_path, exitflag))
self.time_path = np.hstack((self.time_path, time))

# increment the time and f_eval counters
self.time_total += time
Expand Down
2 changes: 1 addition & 1 deletion pypesto/store/read_from_hdf5.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def read_hdf5_profile(
specifies the profile index that is read
from the HDF5 file
"""
result = ProfilerResult(np.array([]), np.array([]), np.array([]))
result = ProfilerResult(np.empty((0, 0)), np.array([]), np.array([]))

for profile_key in result.keys():
if profile_key in f[f'/profiling/{profile_id}/{parameter_id}']:
Expand Down
4 changes: 2 additions & 2 deletions test/visualize/test_visualize.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,8 @@ def create_profile_result():
result = create_optimization_result()

# write some dummy results for profiling
ratio_path_1 = [0.15, 0.25, 0.7, 1.0, 0.8, 0.35, 0.15]
ratio_path_2 = [0.1, 0.2, 0.7, 1.0, 0.8, 0.3, 0.1]
ratio_path_1 = np.array([0.15, 0.25, 0.7, 1.0, 0.8, 0.35, 0.15])
ratio_path_2 = np.array([0.1, 0.2, 0.7, 1.0, 0.8, 0.3, 0.1])
x_path_1 = np.array(
[
[2.0, 2.1, 2.3, 2.5, 2.7, 2.9, 3.0],
Expand Down