blob: c21f0c466be5edfac83cddae04c3642b7e675d07 [file] [log] [blame]
aleloi7ebbf902016-06-20 07:39:15 -07001# 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.
aleloi7ebbf902016-06-20 07:39:15 -07008"""Utility functions for calculating statistics.
9"""
10
11from __future__ import division
12import collections
13import sys
14
15
kjellanderdd460e22017-04-12 12:06:13 -070016def CountReordered(sequence_numbers):
Mirko Bonadei8cc66952020-10-30 10:13:45 +010017 """Returns number of reordered indices.
aleloi7ebbf902016-06-20 07:39:15 -070018
19 A reordered index is an index `i` for which sequence_numbers[i] >=
20 sequence_numbers[i + 1]
21 """
Mirko Bonadei8cc66952020-10-30 10:13:45 +010022 return sum(1 for (s1, s2) in zip(sequence_numbers, sequence_numbers[1:])
23 if s1 >= s2)
aleloi7ebbf902016-06-20 07:39:15 -070024
25
kjellanderdd460e22017-04-12 12:06:13 -070026def SsrcNormalizedSizeTable(data_points):
Mirko Bonadei8cc66952020-10-30 10:13:45 +010027 """Counts proportion of data for every SSRC.
aleloi7ebbf902016-06-20 07:39:15 -070028
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 Bonadei8cc66952020-10-30 10:13:45 +010038 mapping = collections.defaultdict(int)
39 for point in data_points:
40 mapping[point.ssrc] += point.size
41 return NormalizeCounter(mapping)
aleloi7ebbf902016-06-20 07:39:15 -070042
43
kjellanderdd460e22017-04-12 12:06:13 -070044def NormalizeCounter(counter):
Mirko Bonadei8cc66952020-10-30 10:13:45 +010045 """Returns a normalized version of the dictionary `counter`.
aleloi7ebbf902016-06-20 07:39:15 -070046
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 Bonadei8cc66952020-10-30 10:13:45 +010053 total = sum(counter.values())
54 return {key: counter[key] / total for key in counter}
aleloi7ebbf902016-06-20 07:39:15 -070055
56
kjellanderdd460e22017-04-12 12:06:13 -070057def Unwrap(data, mod):
Mirko Bonadei8cc66952020-10-30 10:13:45 +010058 """Returns `data` unwrapped modulo `mod`. Does not modify data.
aleloi7ebbf902016-06-20 07:39:15 -070059
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
kjellanderdd460e22017-04-12 12:06:13 -070064 E.g. Unwrap([0, 1, 2, 0, 1, 2, 7, 8], 3) -> [0, 1, 2, 3,
aleloi7ebbf902016-06-20 07:39:15 -070065 4, 5, 4, 5]
66 """
Mirko Bonadei8cc66952020-10-30 10:13:45 +010067 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
aleloi7ebbf902016-06-20 07:39:15 -070072
aleloieb24dd02016-09-06 06:34:17 -070073
kjellanderdd460e22017-04-12 12:06:13 -070074def SsrcDirections(data_points):
Mirko Bonadei8cc66952020-10-30 10:13:45 +010075 ssrc_is_incoming = {}
76 for point in data_points:
77 ssrc_is_incoming[point.ssrc] = point.incoming
78 return ssrc_is_incoming
aleloieb24dd02016-09-06 06:34:17 -070079
80
aleloi7ebbf902016-06-20 07:39:15 -070081# Python 2/3-compatible input function
82if sys.version_info[0] <= 2:
Mirko Bonadei8cc66952020-10-30 10:13:45 +010083 get_input = raw_input # pylint: disable=invalid-name
aleloi7ebbf902016-06-20 07:39:15 -070084else:
Mirko Bonadei8cc66952020-10-30 10:13:45 +010085 get_input = input # pylint: disable=invalid-name