From ee10a8e9f365403c8f780bec39affaebaec01005 Mon Sep 17 00:00:00 2001 From: Wanrun Jiang <58099845+Vibsteamer@users.noreply.github.com> Date: Thu, 24 Jul 2025 23:05:06 +0800 Subject: [PATCH 1/3] Update report_adaptive_lower.py --- dpgen2/exploration/report/report_adaptive_lower.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/dpgen2/exploration/report/report_adaptive_lower.py b/dpgen2/exploration/report/report_adaptive_lower.py index cd6087f2..33df129f 100644 --- a/dpgen2/exploration/report/report_adaptive_lower.py +++ b/dpgen2/exploration/report/report_adaptive_lower.py @@ -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 From 9f50df64df1258e5cdefd85de6fda380e1f419bd Mon Sep 17 00:00:00 2001 From: Wanrun Jiang <58099845+Vibsteamer@users.noreply.github.com> Date: Thu, 24 Jul 2025 23:14:07 +0800 Subject: [PATCH 2/3] Update test_report_adaptive_lower.py optimized for non-repeative sampling --- .../exploration/test_report_adaptive_lower.py | 28 ++++++++++--------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/tests/exploration/test_report_adaptive_lower.py b/tests/exploration/test_report_adaptive_lower.py index f5c13354..ccd7c046 100644 --- a/tests/exploration/test_report_adaptive_lower.py +++ b/tests/exploration/test_report_adaptive_lower.py @@ -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)): - tidx, fidx = candi[ii] + 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) From f0e844d92e9d6905ce9bbf31d8f73cd9f5fcda9a Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 24 Jul 2025 15:39:44 +0000 Subject: [PATCH 3/3] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- dpgen2/exploration/report/report_adaptive_lower.py | 2 +- tests/exploration/test_report_adaptive_lower.py | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/dpgen2/exploration/report/report_adaptive_lower.py b/dpgen2/exploration/report/report_adaptive_lower.py index 33df129f..cd2989f3 100644 --- a/dpgen2/exploration/report/report_adaptive_lower.py +++ b/dpgen2/exploration/report/report_adaptive_lower.py @@ -450,7 +450,7 @@ def _get_candidates_inv_pop_f( len(self.candi_picked), size=max_nframes, replace=False, - p=prob / np.sum(prob) + p=prob / np.sum(prob), ) ret = [self.candi_picked[i] for i in indices] else: diff --git a/tests/exploration/test_report_adaptive_lower.py b/tests/exploration/test_report_adaptive_lower.py index ccd7c046..b5f123ca 100644 --- a/tests/exploration/test_report_adaptive_lower.py +++ b/tests/exploration/test_report_adaptive_lower.py @@ -198,19 +198,19 @@ def test_f_inv_pop(self): ) def faked_choices( - a, #numb_candi - size=None, #numb_select - replace=False, # non-repeative sampling + 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 - candi = ter.candi_picked + candi = ter.candi_picked self.assertEqual(a, 12) self.assertEqual(len(p), 12) ret_indices = [] for ii in range(a): - tidx, fidx = candi[ii] + tidx, fidx = candi[ii] this_mdf = md_f[tidx][fidx] if this_mdf < 0.4: self.assertAlmostEqual(p[ii], 0.1) # 1/5 / 2.0