-
Notifications
You must be signed in to change notification settings - Fork 0
/
analysis.py
65 lines (58 loc) · 2.12 KB
/
analysis.py
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
#%%
import os
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.patches as patches
import struct
filename = os.getenv('LOCALAPPDATA') + '\\BrightnessSwitch.config'
file = open(filename, mode='rb')
try:
if file.read(2) != b'BS':
raise Exception()
versionFlag = file.read(1)
if versionFlag == b'v':
if file.read(1) != b'1':
raise Exception()
elif versionFlag == b'+':
version = struct.unpack('I', file.read(4))[0]
if version == 2:
file.read(1) # auto
else:
raise Exception()
else:
raise Exception()
b = struct.unpack('d', file.read(8))[0]
w = struct.unpack('d', file.read(8))[0]
nDark = struct.unpack('i', file.read(4))[0]
nLight = struct.unpack('i', file.read(4))[0]
darkList = []
darkWeights = []
lightList = []
lightWeights = []
for i in range(nDark):
darkList.append(np.exp(struct.unpack('d', file.read(8))[0]))
darkWeights.append(0.2 + (i + 1) / nDark)
for i in range(nLight):
lightList.append(np.exp(struct.unpack('d', file.read(8))[0]))
lightWeights.append(0.2 + (i + 1) / nLight)
file.close()
plt.figure(figsize=(6, 2))
plt.plot([np.exp(b)] * 2, [-1, 1], 'k')
# the decision criteria for the app is that a prediction ends up outside +-w / 2
plt.gca().add_patch(patches.Rectangle(
(np.exp(b), -1), np.exp(b - 1 / w / 2) - np.exp(b), 2, facecolor='k', alpha=0.15))
plt.gca().add_patch(patches.Rectangle(
(np.exp(b), -1), np.exp(b + 1 / w / 2) - np.exp(b), 2, facecolor='y', alpha=0.15))
plt.scatter(darkList, (np.random.random(nDark) - 0.5) * 0.4,
[entry * 100 for entry in darkWeights], 'k', alpha=0.25, linewidths=0)
plt.scatter(lightList, (np.random.random(nLight) - 0.5) * 0.4,
[entry * 100 for entry in lightWeights], 'y', alpha=0.25, linewidths=0)
plt.semilogx()
plt.xlabel('Illuminance (lux)')
plt.ylim(-1, 1)
plt.gca().axes.get_yaxis().set_ticks([])
plt.tight_layout()
plt.show()
except:
print('Can\'t read the configuration file.')
file.close()