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"""Utility functions for calculating statistics. 9""" 10 11from __future__ import division 12import collections 13import sys 14 15 16def CountReordered(sequence_numbers): 17 """Returns number of reordered indices. 18 19 A reordered index is an index `i` for which sequence_numbers[i] >= 20 sequence_numbers[i + 1] 21 """ 22 return sum(1 for (s1, s2) in zip(sequence_numbers, sequence_numbers[1:]) 23 if s1 >= s2) 24 25 26def SsrcNormalizedSizeTable(data_points): 27 """Counts proportion of data for every SSRC. 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 """ 38 mapping = collections.defaultdict(int) 39 for point in data_points: 40 mapping[point.ssrc] += point.size 41 return NormalizeCounter(mapping) 42 43 44def NormalizeCounter(counter): 45 """Returns a normalized version of the dictionary `counter`. 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 """ 53 total = sum(counter.values()) 54 return {key: counter[key] / total for key in counter} 55 56 57def Unwrap(data, mod): 58 """Returns `data` unwrapped modulo `mod`. Does not modify data. 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 64 E.g. Unwrap([0, 1, 2, 0, 1, 2, 7, 8], 3) -> [0, 1, 2, 3, 65 4, 5, 4, 5] 66 """ 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 72 73 74def SsrcDirections(data_points): 75 ssrc_is_incoming = {} 76 for point in data_points: 77 ssrc_is_incoming[point.ssrc] = point.incoming 78 return ssrc_is_incoming 79 80 81# Python 2/3-compatible input function 82if sys.version_info[0] <= 2: 83 get_input = raw_input # pylint: disable=invalid-name 84else: 85 get_input = input # pylint: disable=invalid-name 86