Skip to content

Commit 3b0dbf1

Browse files
committed
Improve code documentation of change_hp_value
1 parent 3d1a9b9 commit 3b0dbf1

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

ConfigSpace/c_util.pyx

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -214,29 +214,34 @@ cpdef np.ndarray change_hp_value(
214214
cdef DTYPE_t NaN = np.NaN
215215
cdef dict children_of = configuration_space._children_of
216216

217-
# We maintain to_visit as a minimum heap of indices of hyperparameters that may need to be updated.
218-
# We assume that the hyperparameters are sorted topologically by their index.
217+
# We maintain `to_visit` as a minimum heap of indices of hyperparameters that may need to be updated.
218+
# We assume that the hyperparameters are sorted topologically with respect to the conditions by the hyperparameter indices.
219+
# Initially, we know that the hyperparameter with the index `index` may need to be updated (by changing its value to `hp_value`).
219220
to_visit = [index]
220221

221-
# Since one hyperparameter may be reachable in several ways, we need to make sure we don't process it twice.
222+
# Since one hyperparameter may be reachable in more than one way, we need to make sure we don't schedule it for inspection more than once.
222223
scheduled = np.zeros(len(configuration_space), dtype=bool)
223224
scheduled[index] = True
224225

225-
# Activate hyperparameters if their parent node got activated
226+
# Activate hyperparameters if their parent node got activated.
226227
while len(to_visit) > 0:
227228
assert np.all(scheduled[to_visit])
228229
current_idx = heapq.heappop(to_visit)
229230
current_name = configuration_space._idx_to_hyperparameter[current_idx]
230231
conditions = configuration_space._parent_conditions_of[current_name]
231232

233+
# Should the current hyperparameter be active?
232234
active = True
233235
for condition in conditions:
234236
if not condition._evaluate_vector(configuration_array):
237+
# The current hyperparameter should be inactive because `condition` is not satisfied.
235238
active = False
236239
break
237240

241+
# Should the value of the current hyperparameter be updated?
238242
update = False
239243
if current_idx == index:
244+
# The current hyperparameter should be updated because the caller requested this update.
240245
if not active:
241246
raise ValueError(
242247
"Attempting to change the value of the inactive hyperparameter '%s' to '%s'." % (hp_name, hp_value))
@@ -245,10 +250,12 @@ cpdef np.ndarray change_hp_value(
245250
else:
246251
current_value = configuration_array[current_idx]
247252
if active and not current_value == current_value:
253+
# The current hyperparameter should be active but is inactive.
248254
current = configuration_space._hyperparameters[current_name]
249255
target_value = current.normalized_default_value
250256
update = True
251257
elif not active and current_value == current_value:
258+
# The current hyperparameter should be inactive but is active.
252259
# If the hyperparameter was made inactive,
253260
# all its children need to be deactivated as well
254261
target_value = NaN
@@ -258,6 +265,8 @@ cpdef np.ndarray change_hp_value(
258265
configuration_array[current_idx] = target_value
259266
for child in children_of[current_name]:
260267
child_idx = configuration_space._hyperparameter_idx[child.name]
268+
# We assume that the hyperparameters are ordered topologically by index.
269+
# This means that every child must have an index greater than its parent.
261270
assert child_idx > current_idx
262271
if not scheduled[child_idx]:
263272
heapq.heappush(to_visit, child_idx)

0 commit comments

Comments
 (0)