aleloi | 7ebbf90 | 2016-06-20 07:39:15 -0700 | [diff] [blame] | 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. |
aleloi | 7ebbf90 | 2016-06-20 07:39:15 -0700 | [diff] [blame] | 8 | """Utility functions for calculating statistics. |
| 9 | """ |
| 10 | |
| 11 | from __future__ import division |
| 12 | import collections |
| 13 | import sys |
| 14 | |
| 15 | |
kjellander | dd460e2 | 2017-04-12 12:06:13 -0700 | [diff] [blame] | 16 | def CountReordered(sequence_numbers): |
Mirko Bonadei | 8cc6695 | 2020-10-30 10:13:45 +0100 | [diff] [blame] | 17 | """Returns number of reordered indices. |
aleloi | 7ebbf90 | 2016-06-20 07:39:15 -0700 | [diff] [blame] | 18 | |
| 19 | A reordered index is an index `i` for which sequence_numbers[i] >= |
| 20 | sequence_numbers[i + 1] |
| 21 | """ |
Mirko Bonadei | 8cc6695 | 2020-10-30 10:13:45 +0100 | [diff] [blame] | 22 | return sum(1 for (s1, s2) in zip(sequence_numbers, sequence_numbers[1:]) |
| 23 | if s1 >= s2) |
aleloi | 7ebbf90 | 2016-06-20 07:39:15 -0700 | [diff] [blame] | 24 | |
| 25 | |
kjellander | dd460e2 | 2017-04-12 12:06:13 -0700 | [diff] [blame] | 26 | def SsrcNormalizedSizeTable(data_points): |
Mirko Bonadei | 8cc6695 | 2020-10-30 10:13:45 +0100 | [diff] [blame] | 27 | """Counts proportion of data for every SSRC. |
aleloi | 7ebbf90 | 2016-06-20 07:39:15 -0700 | [diff] [blame] | 28 | |
| 29 | Args: |
| 30 | data_points: list of pb_parse.DataPoint |
| 31 | |
| 32 | Returns: |
| 33 | A dictionary mapping from every SSRC in the data points. The |
| 34 | value of an SSRC `s` is the proportion of sizes of packets with |
| 35 | SSRC `s` to the total size of all packets. |
| 36 | |
| 37 | """ |
Mirko Bonadei | 8cc6695 | 2020-10-30 10:13:45 +0100 | [diff] [blame] | 38 | mapping = collections.defaultdict(int) |
| 39 | for point in data_points: |
| 40 | mapping[point.ssrc] += point.size |
| 41 | return NormalizeCounter(mapping) |
aleloi | 7ebbf90 | 2016-06-20 07:39:15 -0700 | [diff] [blame] | 42 | |
| 43 | |
kjellander | dd460e2 | 2017-04-12 12:06:13 -0700 | [diff] [blame] | 44 | def NormalizeCounter(counter): |
Mirko Bonadei | 8cc6695 | 2020-10-30 10:13:45 +0100 | [diff] [blame] | 45 | """Returns a normalized version of the dictionary `counter`. |
aleloi | 7ebbf90 | 2016-06-20 07:39:15 -0700 | [diff] [blame] | 46 | |
| 47 | Does not modify `counter`. |
| 48 | |
| 49 | Returns: |
| 50 | A new dictionary, in which every value in `counter` |
| 51 | has been divided by the total to sum up to 1. |
| 52 | """ |
Mirko Bonadei | 8cc6695 | 2020-10-30 10:13:45 +0100 | [diff] [blame] | 53 | total = sum(counter.values()) |
| 54 | return {key: counter[key] / total for key in counter} |
aleloi | 7ebbf90 | 2016-06-20 07:39:15 -0700 | [diff] [blame] | 55 | |
| 56 | |
kjellander | dd460e2 | 2017-04-12 12:06:13 -0700 | [diff] [blame] | 57 | def Unwrap(data, mod): |
Mirko Bonadei | 8cc6695 | 2020-10-30 10:13:45 +0100 | [diff] [blame] | 58 | """Returns `data` unwrapped modulo `mod`. Does not modify data. |
aleloi | 7ebbf90 | 2016-06-20 07:39:15 -0700 | [diff] [blame] | 59 | |
| 60 | Adds integer multiples of mod to all elements of data except the |
| 61 | first, such that all pairs of consecutive elements (a, b) satisfy |
| 62 | -mod / 2 <= b - a < mod / 2. |
| 63 | |
kjellander | dd460e2 | 2017-04-12 12:06:13 -0700 | [diff] [blame] | 64 | E.g. Unwrap([0, 1, 2, 0, 1, 2, 7, 8], 3) -> [0, 1, 2, 3, |
aleloi | 7ebbf90 | 2016-06-20 07:39:15 -0700 | [diff] [blame] | 65 | 4, 5, 4, 5] |
| 66 | """ |
Mirko Bonadei | 8cc6695 | 2020-10-30 10:13:45 +0100 | [diff] [blame] | 67 | lst = data[:] |
| 68 | for i in range(1, len(data)): |
| 69 | lst[i] = lst[i - 1] + (lst[i] - lst[i - 1] + mod // 2) % mod - (mod // |
| 70 | 2) |
| 71 | return lst |
aleloi | 7ebbf90 | 2016-06-20 07:39:15 -0700 | [diff] [blame] | 72 | |
aleloi | eb24dd0 | 2016-09-06 06:34:17 -0700 | [diff] [blame] | 73 | |
kjellander | dd460e2 | 2017-04-12 12:06:13 -0700 | [diff] [blame] | 74 | def SsrcDirections(data_points): |
Mirko Bonadei | 8cc6695 | 2020-10-30 10:13:45 +0100 | [diff] [blame] | 75 | ssrc_is_incoming = {} |
| 76 | for point in data_points: |
| 77 | ssrc_is_incoming[point.ssrc] = point.incoming |
| 78 | return ssrc_is_incoming |
aleloi | eb24dd0 | 2016-09-06 06:34:17 -0700 | [diff] [blame] | 79 | |
| 80 | |
aleloi | 7ebbf90 | 2016-06-20 07:39:15 -0700 | [diff] [blame] | 81 | # Python 2/3-compatible input function |
| 82 | if sys.version_info[0] <= 2: |
Mirko Bonadei | 8cc6695 | 2020-10-30 10:13:45 +0100 | [diff] [blame] | 83 | get_input = raw_input # pylint: disable=invalid-name |
aleloi | 7ebbf90 | 2016-06-20 07:39:15 -0700 | [diff] [blame] | 84 | else: |
Mirko Bonadei | 8cc6695 | 2020-10-30 10:13:45 +0100 | [diff] [blame] | 85 | get_input = input # pylint: disable=invalid-name |