Chromium Code Reviews| Index: tools/py_event_log_analyzer/misc.py |
| diff --git a/tools/py_event_log_analyzer/misc.py b/tools/py_event_log_analyzer/misc.py |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..af03921d272d9fdec4d2eb4ebb4e3791e678c7e5 |
| --- /dev/null |
| +++ b/tools/py_event_log_analyzer/misc.py |
| @@ -0,0 +1,107 @@ |
| +# Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. |
| +# |
| +# Use of this source code is governed by a BSD-style license |
| +# that can be found in the LICENSE file in the root of the source |
| +# tree. An additional intellectual property rights grant can be found |
| +# in the file PATENTS. All contributing project authors may |
| +# be found in the AUTHORS file in the root of the source tree. |
| + |
| +"""Utility functions for calculating statistics. |
| + |
| +This file also contains a unit test. Run it with |
| + |
| + python -m unittest misc |
| +or |
| + python3 -m unittest misc |
| +""" |
| + |
| +from __future__ import division |
| +import collections |
| +import random |
| +import sys |
| +import unittest |
| + |
| + |
| + |
| +def count_reordered(sequence_numbers): |
|
phoglund
2016/05/31 13:47:43
You sure this passes the linter? We should always
|
| + """Returns number of indices `i` for which |
| + 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.
|
| + """ |
| + return sum(1 for (s1, s2) in zip(sequence_numbers, |
| + sequence_numbers[1:]) if |
| + s1 >= s2) |
| + |
| + |
| +def ssrc_normalized_size_table(data_points): |
| + """Returns mapping from a SSRC to its relative occurance proportion in |
| + the data. |
| + """ |
| + d = collections.defaultdict(int) |
|
phoglund
2016/05/31 13:47:42
You would get away with this in go, but python cul
|
| + for pt in data_points: |
|
phoglund
2016/05/31 13:47:42
Same here, "point"
|
| + d[pt.ssrc] += pt.size |
| + return normalize_counter(d) |
| + |
| + |
| +def normalize_counter(counter): |
| + """Returns a normalized (i.e. divided by total to sum up to 1) version |
| + of the input dictionary `counter`. Does not modify `counter`. |
| + |
| + """ |
| + total = sum(counter.values()) |
| + return {key: counter[key] / total for key in counter} |
| + |
| + |
| +def unwrap(data, mod): |
| + """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
|
| + |
| + Adds integer multiples of mod to all elements of data except the |
| + first, such that all pairs of consecutive elements (a, b) satisfy |
| + -mod / 2 <= b - a < mod / 2. |
| + |
| + E.g. unwrap([0, 1, 2, 0, 1, 2, 7, 8], 3) -> [0, 1, 2, 3, |
| + 4, 5, 4, 5] |
| + |
| + """ |
| + lst = data[:] |
| + for i in range(1, len(data)): |
| + lst[i] = lst[i - 1] + (lst[i] - lst[i - 1] + |
| + mod // 2) % mod - (mod // 2) |
| + return lst |
| + |
| +# Python 2/3-compatible input function |
| +if sys.version_info[0] <= 2: |
| + get_input = raw_input |
| +else: |
| + get_input = input |
| + |
| + |
| +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.
|
| + |
| + def testUnwrap(self): |
|
phoglund
2016/05/31 13:47:42
This test tests at least 5 different things, so it
|
| + data = [0, 1, 2, 0, -1, -2, -3, -4] |
| + unwrapped_3 = unwrap(data, 3) |
| + unwrapped_4 = unwrap(data, 4) |
| + |
| + # Data should not change after unwrap. |
| + self.assertEqual([0, 1, 2, 0, -1, -2, -3, -4], data) |
| + |
| + self.assertEqual([0, 1, 2, 3, 2, 1, 0, -1], unwrapped_3) |
| + self.assertEqual([0, 1, 2, 0, -1, -2, -3, -4], unwrapped_4) |
| + |
| + # Test against the definition of unwrap: |
| + random_data = [random.randint(0, 9) for _ in range(100)] |
| + random_data_copy = random_data[:] |
| + for mod in range(1, 100): |
| + random_data_unwrapped_mod = unwrap(random_data, mod) |
| + |
| + # Check that only multiples of mod are added: |
| + for (old_a, a) in zip(random_data, random_data_unwrapped_mod): |
| + self.assertEqual((old_a - a) % mod, 0) |
| + |
| + # Check unwrap wtr the inquality definition: |
| + for (a, b) in zip(random_data_unwrapped_mod, |
| + random_data_unwrapped_mod[1:]): |
| + self.assertTrue(-mod / 2 <= b - a < mod / 2) |
| + |
| + # Check that data is not modified: |
| + self.assertEqual(random_data, random_data_copy) |