-
Notifications
You must be signed in to change notification settings - Fork 8
/
QDSpy_stim_support.py
256 lines (225 loc) · 7.75 KB
/
QDSpy_stim_support.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
QDSpy module - support functions for stimulus generation and compilation
'rotateTranslate()'
Rotate the coordinates by the given angle
'scaleRGB()'
'scaleRGBShader()'
Scale color as RGBA depending on bit depth and color mode
'getHashStr()'
'getHashStrForFile()'
Get hashes for strings or files
'Log'
Class that allows program-wide flexible logging. Only one instance that
is defined in this module. Writes log messages to stdout and/or sends
messages via a pipe to the GUI process.
Copyright (c) 2013-2021 Thomas Euler
All rights reserved.
2021-10-15 - Account for LINUX console text coloring
"""
# ---------------------------------------------------------------------
__author__ = "[email protected]"
import sys
import hashlib
import datetime
import numpy as np
import QDSpy_multiprocessing as mpr
import QDSpy_config as cfg
import QDSpy_global as glo
import QDSpy_stim as stm
PLATFORM_WINDOWS = sys.platform == "win32"
if PLATFORM_WINDOWS:
from ctypes import windll
import Libraries.color_console as con
else:
import Libraries.color_console_linux as con
# ---------------------------------------------------------------------
Msg_Prior_DEBUG = -1
Msg_Prior_None = 0
Msg_Prior_Asterisk = 1
Msg_Prior_Ok = 2
Msg_Prior_WARNING = 3
Msg_Prior_ERROR = 4
Msg_Prior_DATA = 5
# ---------------------------------------------------------------------
def rotateTranslate(_coords, _rot_deg, _posxy):
# Rotate the coordinates in the list ([x1,y1,x2,y2, ...]) by the
# given angle
#
coords = []
a_rad = np.radians(_rot_deg)
for i in range(0, len(_coords), 2):
x = _coords[i] *np.cos(a_rad) \
+_coords[i+1] *np.sin(a_rad) +_posxy[0]
y = -_coords[i] *np.sin(a_rad) \
+_coords[i+1] *np.cos(a_rad) +_posxy[1]
coords += [x, y]
return coords
# ---------------------------------------------------------------------
def toInt(_coords):
# Convert the coordinates in the list to integers
#
coords = []
for v in _coords:
coords.append(int(v))
return coords
# ---------------------------------------------------------------------
def completeRGBList(_RGBs):
# Complete each RGBx2 tuple if incomplete in the given list
#
return [tuple(list(rgb) +[0]*(stm.RGB_MAX -len(rgb))) for rgb in _RGBs]
def completeRGBAList(_RGBAs):
# Complete each RGBAx2 tuple if incomplete in the given list
#
res = []
for obj in _RGBAs:
res.append([rgba +(0,)*(stm.RGBA_MAX -len(rgba)) for rgba in obj])
return res
# ---------------------------------------------------------------------
def scaleRGB(_Stim, _inRGBA):
# Scale color (RGBA) depending on bit depth and color mode (format)
#
if _Stim.colorMode >= stm.ColorMode.LC_first:
# One of the lightcrafter specific modes ...
#
if _Stim.colorMode == stm.ColorMode.LC_G9B9:
RGBA = np.clip(_inRGBA, 0, 255)
g = 2**int(RGBA[1]/255.0 *8) -1
b = 2**int(RGBA[2]/255.0 *8) -1
return 0,g,b, int(RGBA[3])
else:
return 255,255,255,255
else:
# One of the "normal" modes ...
#
if _Stim.colorMode == stm.ColorMode.range0_255:
RGBA = np.clip(_inRGBA, 0, 255)
dv = 255.0
elif _Stim.colorMode == stm.ColorMode.range0_1:
RGBA = np.clip(_inRGBA, 0, 1)
dv = 1.0
r = int(RGBA[0]/dv *(2**_Stim.bitDepth[0] -1)) << _Stim.bitShift[0]
g = int(RGBA[1]/dv *(2**_Stim.bitDepth[1] -1)) << _Stim.bitShift[1]
b = int(RGBA[2]/dv *(2**_Stim.bitDepth[2] -1)) << _Stim.bitShift[2]
RGB = tuple(np.clip((r,g,b), 0, 255))
return RGB +(int(RGBA[3]),)
# ---------------------------------------------------------------------
def scaleRGBShader(_Stim, _inRGBA):
# Scale color (RGBA) depending on bit depth and color mode (format)
# into shader compatible values (0...1)
#
r = _inRGBA[0]/255.0
g = _inRGBA[1]/255.0
b = _inRGBA[2]/255.0
a = _inRGBA[3]/255.0
RGBA = tuple(np.clip((r,g,b,a), 0.0, 1.0))
return RGBA
# ---------------------------------------------------------------------
def getHashStr(_str):
#
#
m = hashlib.md5()
m.update(_str.encode('utf-8'))
return m.hexdigest()
# ---------------------------------------------------------------------
def getHashStrForFile(_sFName):
#
#
m = hashlib.md5()
with open(_sFName, "rb") as f:
while True:
data = f.read(65536)
if not data:
break
m.update(data)
return m.hexdigest()
# ---------------------------------------------------------------------
# Log class
#
# ---------------------------------------------------------------------
class Log:
def __init__(self):
# Initializing
self.isRunFromGUI = False
self.Sync = None
self.stdFCol = con.get_text_attr()
self.stdBCol = self.stdFCol & 0x0070
self.noMsgToStdOut = not glo.QDSpy_workerMsgsToStdOut
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
def setGUISync(self, _Sync):
# Define a synchronisation object to relay messages to the GUI
if _Sync is not None:
self.isRunFromGUI = True
self.Sync = _Sync
self.noMsgToStdOut = cfg.getParsedArgv().gui
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
def write(self, _headerStr, _msgStr, _isProgress=False, _getStr=False):
# Log a message
if (_headerStr.upper() == "DEBUG") and not glo.QDSpy_isDebug:
return
if glo.QDSpy_doLogTimeStamps:
# Generate a time stamp
tStr = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
else:
tStr = ""
# Determine message status/priority
if _headerStr.upper() == "DEBUG":
msgPrior = Msg_Prior_DEBUG
msgAttr = con.FOREGROUND_CYAN |self.stdBCol
msgCol = "darkBlue"
elif _headerStr.upper() == "WARNING":
msgPrior = Msg_Prior_WARNING
msgAttr = con.FOREGROUND_YELLOW |con.FOREGROUND_INTENSITY |self.stdBCol
msgCol = "#C76300"
elif _headerStr.upper() == "ERROR":
msgPrior = Msg_Prior_ERROR
msgAttr = con.FOREGROUND_RED |con.FOREGROUND_INTENSITY |self.stdBCol
msgCol = "darkRed"
elif _headerStr.upper() == "OK":
msgPrior = Msg_Prior_Ok
msgAttr = con.FOREGROUND_GREEN |self.stdBCol
msgCol = "darkGreen"
elif _headerStr.upper() == "***":
msgPrior = Msg_Prior_Asterisk
msgAttr = con.FOREGROUND_CYAN |con.FOREGROUND_INTENSITY |self.stdBCol
msgCol = "darkCyan"
elif _headerStr.upper() == "DATA":
msgPrior = Msg_Prior_DATA
msgAttr = con.FOREGROUND_MAGENTA |con.FOREGROUND_INTENSITY |self.stdBCol
msgCol = "darkMagenta"
else:
msgPrior = Msg_Prior_None
msgAttr = self.stdBCol |self.stdFCol
msgCol = "black"
# Send message to log ...
#
if not self.noMsgToStdOut:
# ... to stdout ...
#
con.set_text_attr(msgAttr)
if len(_headerStr) == 0:
sys.stdout.write("\r{0}{1:70}{2}"
.format(tStr, _msgStr,
"" if _isProgress else "\n"))
else:
sys.stdout.write("\r{0}{1:>8} {2}{3}"
.format(tStr, _headerStr, _msgStr,
"" if _isProgress else "\n"))
con.set_text_attr(self.stdBCol |self.stdFCol)
sys.stdout.flush()
if self.isRunFromGUI:
# ... and via pipe to GUI
#
if len(_headerStr) == 0:
txt = "{0}{1!s:70}".format(tStr, _msgStr)
else:
txt = "{0}{1!s:>8} {2}".format(tStr, _headerStr, _msgStr)
data = [mpr.PipeValType.toCli_log, tStr, txt, msgCol, msgPrior]
if not _getStr:
self.Sync.pipeSrv.send(data)
else:
return data
# ---------------------------------------------------------------------
Log = Log()
# ---------------------------------------------------------------------