From b93f85fa3e315346ef2149ff60be975919369e0d Mon Sep 17 00:00:00 2001 From: Ilya Zverev Date: Mon, 6 Jun 2016 20:23:17 +0300 Subject: [PATCH] [mwm.py] Use reservoir sampling for printing out features --- tools/python/mwm/dump_mwm.py | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/tools/python/mwm/dump_mwm.py b/tools/python/mwm/dump_mwm.py index dfa0203aa2..0147c8dc19 100755 --- a/tools/python/mwm/dump_mwm.py +++ b/tools/python/mwm/dump_mwm.py @@ -25,12 +25,13 @@ if cross: print('Outgoing regions: {0}'.format(set(cross['neighbours']))) print('Sample features:') -# Print 5 random features ~10000 features apart +# Print some random features using reservoir sampling count = 5 -probability = 1.0 / 10000 -for feature in mwm.iter_features(): - if random.random() < probability: - print(json.dumps(feature, ensure_ascii=False)) - count -= 1 - if count <= 0: - break +sample = [] +for i, feature in enumerate(mwm.iter_features()): + if i < count: + sample.append(feature) + elif random.randint(0, i) < count: + sample[random.randint(0, count-1)] = feature +for feature in sample: + print(json.dumps(feature, ensure_ascii=False))