Skip to content

Commit

Permalink
[pre-commit.ci] auto fixes from pre-commit.com hooks
Browse files Browse the repository at this point in the history
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] committed Jan 23, 2025
1 parent 1a4cce7 commit ae3bca8
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 13 deletions.
25 changes: 14 additions & 11 deletions src/optimagic/optimization/history.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,9 @@ def _get_next_batch_id(self) -> int:
# Function data, function value, and monotone function value
# ----------------------------------------------------------------------------------

def fun_data(self, cost_model: CostModel, monotone: bool, dropna: bool = False) -> pd.DataFrame:
def fun_data(
self, cost_model: CostModel, monotone: bool, dropna: bool = False
) -> pd.DataFrame:
"""Return the function value data.
Args:
Expand All @@ -119,14 +121,15 @@ def fun_data(self, cost_model: CostModel, monotone: bool, dropna: bool = False)
timings = self._get_total_timings(cost_model)

if not self.is_serial:

timings = _apply_reduction_to_batches(
data=timings,
batch_ids=self.batches,
reduction_function=cost_model.aggregate_batch_time,
)

min_or_max = np.nanmin if self.direction == Direction.MINIMIZE else np.nanmax
min_or_max = (
np.nanmin if self.direction == Direction.MINIMIZE else np.nanmax
)
fun = _apply_reduction_to_batches(
data=fun,
batch_ids=self.batches,
Expand All @@ -138,7 +141,7 @@ def fun_data(self, cost_model: CostModel, monotone: bool, dropna: bool = False)

if self.is_serial:
data["task"] = _task_to_categorical(self.task)

if dropna:
data = data.dropna()

Expand Down Expand Up @@ -202,7 +205,6 @@ def params_data(self, dropna: bool = False) -> pd.DataFrame:
# 2. Make long

if not self.is_serial:

if self.direction == Direction.MINIMIZE:
loc = data.groupby("batches")["fun"].idxmin()
elif self.direction == Direction.MAXIMIZE:
Expand All @@ -215,17 +217,19 @@ def params_data(self, dropna: bool = False) -> pd.DataFrame:
data = data.drop(columns=["batches", "fun"])

long = pd.melt(
wide, var_name="name", value_name="value", id_vars=["task", "batches", "fun"]
wide,
var_name="name",
value_name="value",
id_vars=["task", "batches", "fun"],
)

data = long.reindex(columns=["name", "value", "task", "batches", "fun"])

if dropna:
data = data.dropna()

return data.rename_axis("counter")


@property
def params(self) -> list[PyTree]:
return self._params
Expand Down Expand Up @@ -272,7 +276,6 @@ def _get_total_timings(

return fun_time + jac_time + fun_and_jac_time


def _get_timings_per_task(
self, task: EvalTask, cost_factor: float | None
) -> NDArray[np.float64]:
Expand Down Expand Up @@ -314,7 +317,7 @@ def stop_time(self) -> list[float]:
@property
def batches(self) -> list[int]:
return self._batches

@property
def is_serial(self) -> bool:
return all(self.batches == np.arange(len(self.batches)))
Expand Down Expand Up @@ -455,7 +458,7 @@ def _apply_reduction_to_batches(
Returns:
The transformed data. Has one entry per unique batch id, equal to the result of
applying the reduction function to the data of that batch.
applying the reduction function to the data of that batch.
"""
batch_starts, batch_stops = _get_batch_starts_and_stops(batch_ids)
Expand Down
8 changes: 6 additions & 2 deletions tests/optimagic/optimization/test_history.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,9 @@ def test_history_fun_data_with_fun_evaluations_cost_model(history: History):
assert_frame_equal(got, exp, check_dtype=False, check_categorical=False)


def test_history_fun_data_with_fun_evaluations_cost_model_and_monotone(history: History):
def test_history_fun_data_with_fun_evaluations_cost_model_and_monotone(
history: History,
):
got = history.fun_data(
cost_model=om.timing.fun_evaluations,
monotone=True,
Expand Down Expand Up @@ -561,4 +563,6 @@ def test_apply_to_batch_func_with_non_scalar_return():
data = np.array([0, 1, 2, 3, 4])
batch_ids = [0, 0, 1, 1, 2]
with pytest.raises(ValueError, match="Function <lambda> did not return a scalar"):
_apply_reduction_to_batches(data, batch_ids, reduction_function=lambda _list: _list)
_apply_reduction_to_batches(
data, batch_ids, reduction_function=lambda _list: _list
)

0 comments on commit ae3bca8

Please sign in to comment.