Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 # Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. | |
| 2 # | |
| 3 # Use of this source code is governed by a BSD-style license | |
| 4 # that can be found in the LICENSE file in the root of the source | |
| 5 # tree. An additional intellectual property rights grant can be found | |
| 6 # in the file PATENTS. All contributing project authors may | |
| 7 # be found in the AUTHORS file in the root of the source tree. | |
| 8 | |
| 9 """Utility functions for calculating statistics. | |
| 10 | |
| 11 This file also contains a unit test. Run it with | |
| 12 | |
| 13 python -m unittest misc | |
| 14 or | |
| 15 python3 -m unittest misc | |
| 16 """ | |
| 17 | |
| 18 from __future__ import division | |
| 19 import collections | |
| 20 import random | |
| 21 import sys | |
| 22 import unittest | |
| 23 | |
| 24 | |
| 25 | |
| 26 def count_reordered(sequence_numbers): | |
|
phoglund
2016/05/31 13:47:43
You sure this passes the linter? We should always
| |
| 27 """Returns number of indices `i` for which | |
| 28 sequence_numbers[i] >= sequence_numbers[i+1] | |
|
phoglund
2016/05/31 13:47:42
Try to make this fit on line 27. Docstrings should
aleloi
2016/06/02 14:01:56
Rewrote docstrings everywhere.
| |
| 29 """ | |
| 30 return sum(1 for (s1, s2) in zip(sequence_numbers, | |
| 31 sequence_numbers[1:]) if | |
| 32 s1 >= s2) | |
| 33 | |
| 34 | |
| 35 def ssrc_normalized_size_table(data_points): | |
| 36 """Returns mapping from a SSRC to its relative occurance proportion in | |
| 37 the data. | |
| 38 """ | |
| 39 d = collections.defaultdict(int) | |
|
phoglund
2016/05/31 13:47:42
You would get away with this in go, but python cul
| |
| 40 for pt in data_points: | |
|
phoglund
2016/05/31 13:47:42
Same here, "point"
| |
| 41 d[pt.ssrc] += pt.size | |
| 42 return normalize_counter(d) | |
| 43 | |
| 44 | |
| 45 def normalize_counter(counter): | |
| 46 """Returns a normalized (i.e. divided by total to sum up to 1) version | |
| 47 of the input dictionary `counter`. Does not modify `counter`. | |
| 48 | |
| 49 """ | |
| 50 total = sum(counter.values()) | |
| 51 return {key: counter[key] / total for key in counter} | |
| 52 | |
| 53 | |
| 54 def unwrap(data, mod): | |
| 55 """Returns `data` unwrapped modulo `mod`. Does not modify data. | |
|
phoglund
2016/05/31 13:47:42
This docstring is good, except the blank line on l
| |
| 56 | |
| 57 Adds integer multiples of mod to all elements of data except the | |
| 58 first, such that all pairs of consecutive elements (a, b) satisfy | |
| 59 -mod / 2 <= b - a < mod / 2. | |
| 60 | |
| 61 E.g. unwrap([0, 1, 2, 0, 1, 2, 7, 8], 3) -> [0, 1, 2, 3, | |
| 62 4, 5, 4, 5] | |
| 63 | |
| 64 """ | |
| 65 lst = data[:] | |
| 66 for i in range(1, len(data)): | |
| 67 lst[i] = lst[i - 1] + (lst[i] - lst[i - 1] + | |
| 68 mod // 2) % mod - (mod // 2) | |
| 69 return lst | |
| 70 | |
| 71 # Python 2/3-compatible input function | |
| 72 if sys.version_info[0] <= 2: | |
| 73 get_input = raw_input | |
| 74 else: | |
| 75 get_input = input | |
| 76 | |
| 77 | |
| 78 class TestMisc(unittest.TestCase): | |
|
phoglund
2016/05/31 13:47:42
Don't mix production code and unit tests; just mov
aleloi
2016/06/02 14:01:56
Test moved and broken up in pieces.
| |
| 79 | |
| 80 def testUnwrap(self): | |
|
phoglund
2016/05/31 13:47:42
This test tests at least 5 different things, so it
| |
| 81 data = [0, 1, 2, 0, -1, -2, -3, -4] | |
| 82 unwrapped_3 = unwrap(data, 3) | |
| 83 unwrapped_4 = unwrap(data, 4) | |
| 84 | |
| 85 # Data should not change after unwrap. | |
| 86 self.assertEqual([0, 1, 2, 0, -1, -2, -3, -4], data) | |
| 87 | |
| 88 self.assertEqual([0, 1, 2, 3, 2, 1, 0, -1], unwrapped_3) | |
| 89 self.assertEqual([0, 1, 2, 0, -1, -2, -3, -4], unwrapped_4) | |
| 90 | |
| 91 # Test against the definition of unwrap: | |
| 92 random_data = [random.randint(0, 9) for _ in range(100)] | |
| 93 random_data_copy = random_data[:] | |
| 94 for mod in range(1, 100): | |
| 95 random_data_unwrapped_mod = unwrap(random_data, mod) | |
| 96 | |
| 97 # Check that only multiples of mod are added: | |
| 98 for (old_a, a) in zip(random_data, random_data_unwrapped_mod): | |
| 99 self.assertEqual((old_a - a) % mod, 0) | |
| 100 | |
| 101 # Check unwrap wtr the inquality definition: | |
| 102 for (a, b) in zip(random_data_unwrapped_mod, | |
| 103 random_data_unwrapped_mod[1:]): | |
| 104 self.assertTrue(-mod / 2 <= b - a < mod / 2) | |
| 105 | |
| 106 # Check that data is not modified: | |
| 107 self.assertEqual(random_data, random_data_copy) | |
| OLD | NEW |