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
10 changes: 6 additions & 4 deletions dpgen2/exploration/report/report_adaptive_lower.py
Original file line number Diff line number Diff line change
Expand Up @@ -446,11 +446,13 @@ def _get_candidates_inv_pop_f(
self.candi_picked = [(ii[0], ii[1]) for ii in self.candi]
if max_nframes is not None and max_nframes < len(self.candi_picked):
prob = self._choice_prob_inv_pop_f(self.candi_picked)
ret = random.choices(
self.candi_picked,
weights=prob,
k=max_nframes,
indices = np.random.choice(
len(self.candi_picked),
size=max_nframes,
replace=False,
p=prob / np.sum(prob),
)
ret = [self.candi_picked[i] for i in indices]
else:
ret = self.candi_picked
return ret
Expand Down
26 changes: 14 additions & 12 deletions tests/exploration/test_report_adaptive_lower.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,30 +198,32 @@ def test_f_inv_pop(self):
)

def faked_choices(
candi,
weights=None,
k=0,
a, # numb_candi
size=None, # numb_select
replace=False, # non-repeative sampling
p=None, # normalized prob
):
# hist: 2bins, 0.1-0.4 5candi, 0.4-0.7 7candi
# only return those with mdf 0.1-0.4
self.assertEqual(len(weights), 12)
self.assertEqual(len(candi), 12)
ret = []
for ii in range(len(candi)):
candi = ter.candi_picked
self.assertEqual(a, 12)
self.assertEqual(len(p), 12)
ret_indices = []
for ii in range(a):
tidx, fidx = candi[ii]
this_mdf = md_f[tidx][fidx]
if this_mdf < 0.4:
self.assertAlmostEqual(weights[ii], 1.0 / 5.0)
ret.append(candi[ii])
self.assertAlmostEqual(p[ii], 0.1) # 1/5 / 2.0
ret_indices.append(ii)
else:
self.assertAlmostEqual(weights[ii], 1.0 / 7.0)
return ret
self.assertAlmostEqual(p[ii], 1.0 / 14.0) # 1/7 / 2.0
return ret_indices

ter.record(model_devi)
self.assertEqual(ter.candi, expected_cand)
self.assertEqual(ter.accur, expected_accu)
self.assertEqual(set(ter.failed), expected_fail)
with mock.patch("random.choices", faked_choices):
with mock.patch("numpy.random.choice", faked_choices):
picked = ter.get_candidate_ids(11)
self.assertFalse(ter.converged([]))
self.assertEqual(len(picked), 2)
Expand Down