Skip to content
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
24 changes: 12 additions & 12 deletions vizier/pyvizier/converters/spatio_temporal.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ def extract_all_timestamps(
This is a cheaper alternative to:
```
ts = np.concatenate([np.asarray(tl.times).flatten() for tl in timed_labels])
return sorted(list(set(ts)))
return sorted(set(ts))
```
because it skips the label conversions.

Expand All @@ -225,9 +225,10 @@ def extract_all_timestamps(
Returns:
Sorted list of temporal indices.
"""
all_timestamps = np.concatenate(
[self.to_timestamps(t.measurements).flatten() for t in trials])
return sorted(list(set(all_timestamps)))
all_timestamps: set[float] = set()
for t in trials:
all_timestamps.update(self.to_timestamps(t.measurements).flatten())
return sorted(all_timestamps)


class SparseSpatioTemporalConverter(core.TrialToNumpyDict):
Expand Down Expand Up @@ -359,7 +360,7 @@ def __init__(self,
logging.info('Empty temporal_index_points.')
self.temporal_index_points = np.zeros([0])
else:
self.temporal_index_points = temporal_index_points
self.temporal_index_points = np.sort(temporal_index_points)

def _single_timedlabels_to_temporal_observations(
self, timed_labels: TimedLabels, ts: Union[Sequence[float], Sequence[int]]
Expand Down Expand Up @@ -493,7 +494,8 @@ def to_xty(
3-tuple of (input, temporal_index_points, observations).
input: A Dict whose values correspond to parameters.
temporal_index_points: 1-D array of temporal index points. If provided
as input, then it's the same as the input value.
as input, then it's the same as the input value. The return value is
np.array([]) if there are no labels.
observations: Dict of length equal to metrics, whose values
have shape [len(trials), len(temporal_index_points)]. May contain NaNs.
"""
Expand All @@ -506,12 +508,10 @@ def to_xty(
temporal_index_points = self.temporal_index_points
elif temporal_selection == 'infer' or (temporal_selection == 'auto' and
not self.temporal_index_points.size):
temporal_index_points = sorted(
list(
set(
np.concatenate([
np.asarray(tl.times).flatten() for tl in timed_labels
]))))
tmp: set[float | int] = set()
for tl in timed_labels:
tmp.update(tl.times.flatten())
temporal_index_points = sorted(tmp) # [] when tmp is the empty set.
else:
raise ValueError('Invalid value for temporal_index_points: '
f'{temporal_selection}.')
Expand Down
17 changes: 17 additions & 0 deletions vizier/pyvizier/converters/spatio_temporal_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,23 @@ def test_xty(self):
},
err_msg=f'{labels}')

def test_xty_no_trials(self):
"""Tests that to_xty returns empty temporal_index_points for no trials."""
trials = []
extractor = st.TimedLabelsExtractor(
_metric_converters, 'steps', value_extraction='raw'
)
parameter = core.DefaultModelInputConverter(
pyvizier.ParameterConfig.factory(name='x1', bounds=(0, 5))
)
converter = st.DenseSpatioTemporalConverter(
[parameter], extractor, temporal_index_points=None
)
_, temporal_index_points, _ = converter.to_xty(
trials, temporal_selection='infer'
)
self.assertEqual(temporal_index_points.size, 0)


if __name__ == '__main__':
absltest.main()
Loading