Skip to content

Commit a6a373b

Browse files
committed
FIX: Use all provided values in random choice generator fixture
Use all provided values in random choice generator fixture. The previous implementation did not ensure that all provided values would be used, and if the `count` value was low compared to the number of elements in `values` chances were that not all values would be used.
1 parent 7f5ad07 commit a6a373b

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

test/conftest.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -187,15 +187,25 @@ def setup_random_uniform_4d_data(request):
187187
def _generate_random_choices(request, values, count):
188188
rng = request.node.rng
189189

190+
values = set(values)
191+
190192
num_elements = len(values)
191193

192-
# Randomly distribute N among the given values
193-
partitions = rng.multinomial(count, np.ones(num_elements) / num_elements)
194+
if count < num_elements:
195+
raise ValueError(
196+
f"Count must be at least the number of unique values to guarantee inclusion\nProvided: {count} and {values}."
197+
)
198+
199+
# Start by assigning one of each value
200+
selected_values = list(values)
201+
202+
# Distribute remaining count: randomly distribute N among the values
203+
remaining = count - num_elements
204+
partitions = rng.multinomial(remaining, np.ones(num_elements) / num_elements)
194205

195-
# Create a list of selected values
196-
selected_values = [
197-
val for val, count in zip(values, partitions, strict=True) for _ in range(count)
198-
]
206+
# Add the remaining values according to the partitions
207+
for val, extra_count in zip(values, partitions, strict=True):
208+
selected_values.extend([val] * extra_count)
199209

200210
return sorted(selected_values)
201211

0 commit comments

Comments
 (0)