Skip to content

Commit

Permalink
Rename variables
Browse files Browse the repository at this point in the history
  • Loading branch information
nzw0301 committed Feb 22, 2024
1 parent 855692a commit b47535a
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 26 deletions.
45 changes: 23 additions & 22 deletions optuna/visualization/_timeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,34 +115,35 @@ def _is_running_trials_in_study(study: Study, max_run_duration: datetime.timedel

def _get_timeline_info(study: Study) -> _TimelineInfo:
bars = []

max_datetime = _get_max_datetime_complete(study)
timedelta_for_small_bar = datetime.timedelta(seconds=1)
for t in study.get_trials(deepcopy=False):
date_start = t.datetime_start or max_datetime
date_complete = (
for trial in study.get_trials(deepcopy=False):
datetime_start = trial.datetime_start or max_datetime
datetime_complete = (
max_datetime + timedelta_for_small_bar
if t.state == TrialState.RUNNING
else t.datetime_complete or date_start + timedelta_for_small_bar
if trial.state == TrialState.RUNNING
else trial.datetime_complete or datetime_start + timedelta_for_small_bar
)
infeasible = (
False
if _CONSTRAINTS_KEY not in t.system_attrs
else any([x > 0 for x in t.system_attrs[_CONSTRAINTS_KEY]])
if _CONSTRAINTS_KEY not in trial.system_attrs
else any([x > 0 for x in trial.system_attrs[_CONSTRAINTS_KEY]])
)
if date_complete < date_start:
if datetime_complete < datetime_start:
_logger.warning(
(
f"The start and end times for Trial {t.number} seem to be reversed. "
f"The start time is {date_start} and the end time is {date_complete}."
f"The start and end times for Trial {trial.number} seem to be reversed. "
f"The start time is {datetime_start} and the end time is {datetime_complete}."
)
)
bars.append(
_TimelineBarInfo(
number=t.number,
start=date_start,
complete=date_complete,
state=t.state,
hovertext=_make_hovertext(t),
number=trial.number,
start=datetime_start,
complete=datetime_complete,
state=trial.state,
hovertext=_make_hovertext(trial),
infeasible=infeasible,
)
)
Expand All @@ -163,15 +164,15 @@ def _get_timeline_plot(info: _TimelineInfo) -> "go.Figure":
}

fig = go.Figure()
for s in sorted(TrialState, key=lambda x: x.name):
if s.name == "COMPLETE":
infeasible_bars = [b for b in info.bars if b.state == s and b.infeasible]
feasible_bars = [b for b in info.bars if b.state == s and not b.infeasible]
for state in sorted(TrialState, key=lambda x: x.name):
if state.name == "COMPLETE":
infeasible_bars = [b for b in info.bars if b.state == state and b.infeasible]
feasible_bars = [b for b in info.bars if b.state == state and not b.infeasible]
_plot_bars(infeasible_bars, "#cccccc", "INFEASIBLE", fig)
_plot_bars(feasible_bars, _cm[s.name], s.name, fig)
_plot_bars(feasible_bars, _cm[state.name], state.name, fig)
else:
bars = [b for b in info.bars if b.state == s]
_plot_bars(bars, _cm[s.name], s.name, fig)
bars = [b for b in info.bars if b.state == state]
_plot_bars(bars, _cm[state.name], state.name, fig)
fig.update_xaxes(type="date")
fig.update_layout(
go.Layout(
Expand Down
8 changes: 4 additions & 4 deletions optuna/visualization/matplotlib/_timeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,11 @@ def _get_timeline_plot(info: _TimelineInfo) -> "Axes":
fig.tight_layout()

assert len(info.bars) > 0
start_time = min([b.start for b in info.bars])
complete_time = max([b.complete for b in info.bars])
margin = (complete_time - start_time) * 0.05
first_start_time = min([b.start for b in info.bars])
last_complete_time = max([b.complete for b in info.bars])
margin = (last_complete_time - first_start_time) * 0.05

ax.set_xlim(right=complete_time + margin, left=start_time - margin)
ax.set_xlim(right=last_complete_time + margin, left=first_start_time - margin)
ax.yaxis.set_major_locator(matplotlib.ticker.MaxNLocator(integer=True))
ax.xaxis.set_major_formatter(matplotlib.dates.DateFormatter("%H:%M:%S"))
plt.gcf().autofmt_xdate()
Expand Down

0 comments on commit b47535a

Please sign in to comment.