This repository was archived by the owner on Jul 25, 2024. It is now read-only.
forked from Azure-Samples/azure-iot-samples-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdist_median.py
More file actions
76 lines (59 loc) · 1.92 KB
/
dist_median.py
File metadata and controls
76 lines (59 loc) · 1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import fileinput
import shutil
N = 1000000
# runs on PI
def median_counts(device_seq, median, keep_values):
with open('vibrations-m%d.txt' % device_seq, 'r') as f:
low, high, eq = 0, 0, 0
for line in f.readlines():
if not line.startswith('vibration'):
n = int(line)
if n < median:
low += 1
if n > median:
high += 1
if n == median:
eq += 1
# TODO cache count of numbers under and over the keep_values window
# TODO discard numbers outside the keep_values window from the file to speed up next iteration
return low, eq, high
def accumulate_counts(device_ids, median, keep_values):
lows, eqs, highs = 0, 0, 0
for device_id in device_ids:
low, eq, high = median_counts(device_id, median, keep_values)
lows += low
highs += high
eqs += eq
return lows, highs, eqs
state = {
"low_median": 0,
"high_median": N,
"approx_median": None,
"low_low_count": 0,
"high_high_count": 0
}
num_devices = 6
low_median = 305525
high_median = 305529
# 4 dataset
num_devices = 4
low_median = 262682
high_median = 262686
num_devices = 6
low_median = 0
high_median = N
while True:
approx_median = (low_median + high_median) / 2
lcount, hcount, ecount = accumulate_counts(range(0, num_devices), approx_median, (low_median, high_median))
print(low_median, lcount, approx_median, ecount, hcount, high_median)
if lcount < hcount:
low_median = approx_median
elif hcount < lcount:
high_median = approx_median
print("Median: " + str(approx_median))
if approx_median == (low_median + high_median) / 2:
if (ecount == 0) and ((lcount + hcount) % 2 == 0):
print("Median %f: " % (low_median + high_median) / 2.0)
else:
print("Median %d: " % approx_median)
break