OLD | NEW |
1 # Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. | 1 # Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. |
2 # | 2 # |
3 # Use of this source code is governed by a BSD-style license | 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 | 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 | 5 # tree. An additional intellectual property rights grant can be found |
6 # in the file PATENTS. All contributing project authors may | 6 # in the file PATENTS. All contributing project authors may |
7 # be found in the AUTHORS file in the root of the source tree. | 7 # be found in the AUTHORS file in the root of the source tree. |
8 | 8 |
9 """Utility functions for calculating statistics. | 9 """Utility functions for calculating statistics. |
10 """ | 10 """ |
11 | 11 |
12 from __future__ import division | 12 from __future__ import division |
13 import collections | 13 import collections |
14 import sys | 14 import sys |
15 | 15 |
16 | 16 |
17 def count_reordered(sequence_numbers): | 17 def CountReordered(sequence_numbers): |
18 """Returns number of reordered indices. | 18 """Returns number of reordered indices. |
19 | 19 |
20 A reordered index is an index `i` for which sequence_numbers[i] >= | 20 A reordered index is an index `i` for which sequence_numbers[i] >= |
21 sequence_numbers[i + 1] | 21 sequence_numbers[i + 1] |
22 """ | 22 """ |
23 return sum(1 for (s1, s2) in zip(sequence_numbers, | 23 return sum(1 for (s1, s2) in zip(sequence_numbers, |
24 sequence_numbers[1:]) if | 24 sequence_numbers[1:]) if |
25 s1 >= s2) | 25 s1 >= s2) |
26 | 26 |
27 | 27 |
28 def ssrc_normalized_size_table(data_points): | 28 def SsrcNormalizedSizeTable(data_points): |
29 """Counts proportion of data for every SSRC. | 29 """Counts proportion of data for every SSRC. |
30 | 30 |
31 Args: | 31 Args: |
32 data_points: list of pb_parse.DataPoint | 32 data_points: list of pb_parse.DataPoint |
33 | 33 |
34 Returns: | 34 Returns: |
35 A dictionary mapping from every SSRC in the data points. The | 35 A dictionary mapping from every SSRC in the data points. The |
36 value of an SSRC `s` is the proportion of sizes of packets with | 36 value of an SSRC `s` is the proportion of sizes of packets with |
37 SSRC `s` to the total size of all packets. | 37 SSRC `s` to the total size of all packets. |
38 | 38 |
39 """ | 39 """ |
40 mapping = collections.defaultdict(int) | 40 mapping = collections.defaultdict(int) |
41 for point in data_points: | 41 for point in data_points: |
42 mapping[point.ssrc] += point.size | 42 mapping[point.ssrc] += point.size |
43 return normalize_counter(mapping) | 43 return NormalizeCounter(mapping) |
44 | 44 |
45 | 45 |
46 def normalize_counter(counter): | 46 def NormalizeCounter(counter): |
47 """Returns a normalized version of the dictionary `counter`. | 47 """Returns a normalized version of the dictionary `counter`. |
48 | 48 |
49 Does not modify `counter`. | 49 Does not modify `counter`. |
50 | 50 |
51 Returns: | 51 Returns: |
52 A new dictionary, in which every value in `counter` | 52 A new dictionary, in which every value in `counter` |
53 has been divided by the total to sum up to 1. | 53 has been divided by the total to sum up to 1. |
54 """ | 54 """ |
55 total = sum(counter.values()) | 55 total = sum(counter.values()) |
56 return {key: counter[key] / total for key in counter} | 56 return {key: counter[key] / total for key in counter} |
57 | 57 |
58 | 58 |
59 def unwrap(data, mod): | 59 def Unwrap(data, mod): |
60 """Returns `data` unwrapped modulo `mod`. Does not modify data. | 60 """Returns `data` unwrapped modulo `mod`. Does not modify data. |
61 | 61 |
62 Adds integer multiples of mod to all elements of data except the | 62 Adds integer multiples of mod to all elements of data except the |
63 first, such that all pairs of consecutive elements (a, b) satisfy | 63 first, such that all pairs of consecutive elements (a, b) satisfy |
64 -mod / 2 <= b - a < mod / 2. | 64 -mod / 2 <= b - a < mod / 2. |
65 | 65 |
66 E.g. unwrap([0, 1, 2, 0, 1, 2, 7, 8], 3) -> [0, 1, 2, 3, | 66 E.g. Unwrap([0, 1, 2, 0, 1, 2, 7, 8], 3) -> [0, 1, 2, 3, |
67 4, 5, 4, 5] | 67 4, 5, 4, 5] |
68 """ | 68 """ |
69 lst = data[:] | 69 lst = data[:] |
70 for i in range(1, len(data)): | 70 for i in range(1, len(data)): |
71 lst[i] = lst[i - 1] + (lst[i] - lst[i - 1] + | 71 lst[i] = lst[i - 1] + (lst[i] - lst[i - 1] + |
72 mod // 2) % mod - (mod // 2) | 72 mod // 2) % mod - (mod // 2) |
73 return lst | 73 return lst |
74 | 74 |
75 | 75 |
76 def ssrc_directions(data_points): | 76 def SsrcDirections(data_points): |
77 ssrc_is_incoming = {} | 77 ssrc_is_incoming = {} |
78 for point in data_points: | 78 for point in data_points: |
79 ssrc_is_incoming[point.ssrc] = point.incoming | 79 ssrc_is_incoming[point.ssrc] = point.incoming |
80 return ssrc_is_incoming | 80 return ssrc_is_incoming |
81 | 81 |
82 | 82 |
83 # Python 2/3-compatible input function | 83 # Python 2/3-compatible input function |
84 if sys.version_info[0] <= 2: | 84 if sys.version_info[0] <= 2: |
85 get_input = raw_input | 85 get_input = raw_input # pylint: disable=invalid-name |
86 else: | 86 else: |
87 get_input = input | 87 get_input = input # pylint: disable=invalid-name |
OLD | NEW |