Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 9 """Utility functions for statistics calculations. | |
|
ivoc
2016/05/24 11:50:17
I would reorder this into: Utility functions for c
aleloi
2016/05/24 15:54:40
Done.
| |
| 10 """ | |
| 11 | |
| 12 | |
| 13 import collections | |
| 14 | |
| 15 | |
| 16 def count_reordered(seq_no_s): | |
|
ivoc
2016/05/24 11:50:18
please rename seq_no_s to sequence_numbers
kwiberg-webrtc
2016/05/24 12:09:51
A short comment that describes what this function
aleloi
2016/05/24 15:54:40
Done.
aleloi
2016/05/24 15:54:40
A little better now, I think
| |
| 17 largest = seq_no_s[0] | |
| 18 result = 0 | |
| 19 for seq_no in seq_no_s: | |
| 20 if seq_no < largest: | |
| 21 result += 1 | |
| 22 largest = max(largest, seq_no) | |
|
kwiberg-webrtc
2016/05/24 12:09:51
Nit: "else: largest = seq_no" instead of this stat
aleloi
2016/05/24 15:54:40
Done.
| |
| 23 return result | |
| 24 | |
| 25 | |
| 26 def ssrc_size_table(data_points): | |
| 27 d = collections.defaultdict(int) | |
| 28 for dt in data_points: | |
| 29 d[dt.ssrc] += dt.size | |
| 30 total = sum(d.values()) | |
|
kwiberg-webrtc
2016/05/24 12:09:51
float(sum(d.values())) here, so that you don't hav
aleloi
2016/05/24 15:54:40
removed "float" and added "from __future__ import
| |
| 31 for key in d: | |
| 32 d[key] /= float(total) | |
| 33 return d | |
| 34 | |
| 35 | |
| 36 def percent_table(data): | |
| 37 d = collections.defaultdict(int) | |
| 38 for dt in data: | |
| 39 d[dt] += 1 | |
|
ivoc
2016/05/24 11:50:17
These 3 lines can be replaced by: d = collections.
aleloi
2016/05/24 15:54:40
Thank you!
| |
| 40 total = sum(d.values()) | |
| 41 for key in d: | |
| 42 d[key] /= float(total) | |
| 43 return d | |
| 44 | |
| 45 | |
| 46 def hists(data, n_bins): | |
| 47 """Generates a histogram of `data`. | |
| 48 | |
| 49 The sequence of numbers `data` is divided into `n_bins` many | |
| 50 bins with even proportion of data points in each. | |
|
ivoc
2016/05/24 11:50:18
Is there a difference between this and numpy.histo
kwiberg-webrtc
2016/05/24 12:09:50
Not sure I understand. The intervals will have dif
aleloi
2016/05/24 15:54:40
I got about the same result with numpy.histogram.
| |
| 51 | |
| 52 Args: | |
| 53 data: a sequence of numbers | |
| 54 n_bins: the number of histogram bins into which data is divided. | |
| 55 Returns: | |
| 56 A dict mapping from histogram bin bounds to proportions. Dictionary | |
| 57 keys are pairs of numbers. If `d` is the return value, and `d` | |
| 58 contains an interval (10, 15), d[(10, 15)] is the proportion of | |
| 59 values in data inside this | |
|
ivoc
2016/05/24 11:50:17
Please add a comment on the boundary values (inclu
kwiberg-webrtc
2016/05/24 12:09:50
Are the intervals open, closed, or something else?
aleloi
2016/05/24 15:54:40
not longer relevant
| |
| 60 """ | |
| 61 cumulative = 0 | |
| 62 start = sorted(data)[0]-1 | |
| 63 result = {} | |
| 64 for i in sorted(data): | |
|
kwiberg-webrtc
2016/05/24 12:09:50
i is a bad name for a variable that's not an index
kwiberg-webrtc
2016/05/24 12:09:51
You sort data twice.
aleloi
2016/05/24 15:54:40
here too
| |
| 65 cumulative += data[i] | |
|
kwiberg-webrtc
2016/05/24 12:09:51
You add data[i] here rather than 1, using the elem
aleloi
2016/05/24 15:54:40
here too
| |
| 66 if cumulative >= 1/float(n_bins): | |
|
ivoc
2016/05/24 11:50:17
Is this correct? What if the list contains only ve
kwiberg-webrtc
2016/05/24 12:09:50
cumulative and n_bins are both integers, right, wi
aleloi
2016/05/24 15:54:40
here too
| |
| 67 result[(start, i)] = cumulative | |
| 68 start = i+1 | |
|
kwiberg-webrtc
2016/05/24 12:09:50
You surround binary operators with spaces almost b
aleloi
2016/05/24 15:54:40
here too
| |
| 69 cumulative = 0 | |
| 70 if cumulative != 0: | |
| 71 result[(start, len(data)-1)] = cumulative | |
| 72 return result | |
| 73 | |
| 74 | |
| 75 def unwrap(data, mod): | |
| 76 """Unwraps `data` modulo `mod`. | |
| 77 | |
| 78 If data contains consecutive elements with difference above `mod/2`, | |
| 79 an integer multiple of `mod` is added to one of the elements. | |
|
ivoc
2016/05/24 11:50:18
By reading the code below it looks like the 'integ
kwiberg-webrtc
2016/05/24 12:09:50
I think this is under-specified. What integer mult
aleloi
2016/05/24 15:54:40
unwrap should work like matlab's unwrap (http://se
| |
| 80 | |
| 81 """ | |
| 82 prev = data[0] | |
| 83 for idxm1, curr in enumerate(data[1:]): | |
| 84 idx = idxm1 + 1 | |
| 85 delta_pos = (curr-prev)%mod | |
| 86 delta_neg = (curr-prev)%mod - mod | |
| 87 delta = delta_pos if abs(delta_pos) < abs(delta_neg) else delta_neg | |
| 88 curr = prev + delta | |
| 89 data[idx] = curr | |
| 90 prev = curr | |
| 91 return data | |
| OLD | NEW |