-
Notifications
You must be signed in to change notification settings - Fork 8
/
QDSpy_stim.py
1442 lines (1270 loc) · 49.5 KB
/
QDSpy_stim.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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
QDSpy module - stimulus routines, classes, and compiler
'Stim'
Class representing a visual stimulus
This class is a graphics API independent.
Copyright (c) 2013-2024 Thomas Euler
All rights reserved.
2024-06-15 - Small fixes for PEP violations
- Reformatted (using Ruff)
"""
# ---------------------------------------------------------------------
__author__ = "[email protected]"
import pickle
import numpy as np
import QDSpy_global as glo
import QDSpy_stim_support as ssp
import QDSpy_stim_draw as drw
import QDSpy_stim_movie as mov
import QDSpy_stim_video as vid
import QDSpy_core_shader as csh
if glo.QDSpy_use_Lightcrafter:
import Devices.lightcrafter as lcr
_LCr = lcr.Lightcrafter(_isCheckOnly=True, _funcLog=ssp.Log.write)
else:
_LCr = None
# ---------------------------------------------------------------------
# fmt: off
class ColorMode:
range0_255 = 0
range0_1 = 1
LC_first = 2
LC_G9B9 = 2
COL_bitDepthRGB_888 = (8,8,8)
COL_bitShiftRGB_000 = (0,0,0)
RGB_MAX = 6
RGBA_MAX = 8
# ---------------------------------------------------------------------
class StimObjType:
box = 101
ellipse = 102
sector = 103
movie = 104
video = 105
shader = 201
# ...
Ellipse_maxTr = 72
Sector_maxTr = 360
Sector_maxStep = 10
SO_field_type = 0
SO_field_ID = 1
SO_field_size = 2
SO_field_fgRGB = 3
SO_field_alpha = 4
SO_field_doRGBAByVert = 5
SO_field_useShader = 6
SO_field_shProgIndex = 7
SH_field_type = 0
SH_field_ID = 1
SH_field_shaderType = 2
SH_field_Params = 3
SM_field_type = 0
SM_field_ID = 1
SM_field_movieFName = 2
SM_field_nFr = 3
SM_field_dxFr = 4
SM_field_dyFr = 5
SV_field_type = 0
SV_field_ID = 1
SV_field_videoFName = 2
SV_field_nFr = 3
SV_field_dxFr = 4
SV_field_dyFr = 5
SV_field_fps = 6
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
#SO_defaultFgRGB = (255, 255, 255)
SO_defaultFgRGBEx = (255,255,255, 255,255,255)
SO_defaultAlpha = 255
SO_default_RGBAByVert = 0
SO_default_useShader = 0
# ---------------------------------------------------------------------
class StimSceType:
changeObjCol = 201
changeBkgCol = 202
changeShParams = 203
changeObjShader = 204
# ...
clearSce = 211
renderSce = 212
startMovie = 213
startVideo = 214
awaitTTL = 215
beginLoop = 220
endLoop = 221
# ...
sendCommandToLCr = 300
#
logUserParams = 400
# ...
getRandom = 500
SC_field_type = 0
SC_field_duration_s = 1
SC_field_index = 2
SC_field_marker = 3
SC_field_IDs = 4
SC_field_posXY = 5
SC_field_magXY = 6
SC_field_rot = 7
'''
SC_field_screen = 8
'''
SC_field_RGBs = 5
SC_field_Alphas = 6
SC_field_doRGBAByVert = 7
SC_field_nLoopTrials = 4
SC_field_BkgRGB = 4
SC_field_ShID = 4
SC_field_ShParams = 5
SC_field_ShIDs = 5
SC_field_MovSeq = 8
SC_field_MovTrans = 9
SC_field_MovScreen = 10
SC_field_VidTrans = 8
SC_field_VidScreen = 9
SC_field_LCrParams = 4
SC_field_userParams = 4
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
#SC_defaultBgRGB = (0, 0, 0)
SC_defaultBgRGBEx = (0,0,0, 0,0,0)
SC_enableRGBAByVert = 1
SC_disableRGBAByVert = 0
SC_vertDataSame = 0
SC_vertDataChanged = 1
SC_ObjNewVer = 0x01
SC_ObjNewiVer = 0x02
SC_ObjNewRGBA = 0x04
SC_ObjNewAll = 0x07
SC_ObjNewNone = 0x00
# ---------------------------------------------------------------------
class StimLCrCmd:
#connect = 0
#disconnect = 1
#getHardwareStatus = 2
#getSystemStatus = 3
#getMainStatus = 4
#getVideoSigStatus = 5
softwareReset = 6
setInputSource = 7
setDisplayMode = 8
#setVideoGamma = 9
setTestPattern = 10
#getLEDCurrents = 11
setLEDCurrents = 12
setLEDEnabled = 13
getLEDEnabled = 14
# ...
# ---------------------------------------------------------------------
class StimErrC:
ok = 0
notYetImplemented = -1
invalidDimensions = -10
invalidDuration = -11
invalidParamType = -12
inconsistentParams = -13
invalidAngles = -14
existingID = -20
noMatchingID = -21
nothingToCompile = -50
noStimOrNotCompiled = -51
wrongStimFileFormat = -52
invalidFileNamePath = -53
noDefsInRunSection = -54
noShadersInRunSect = -55
invalidShaderType = -56
notShaderObject = -57
noCompiledStim = -58
movieFileNotFound = -60
notMovieObject = -61
invalidMovieDesc = -62
inconsMovieDesc = -63
invalidMovieSeq = -64
invalidMovieFormat = -65
videoFileNotFound = -70
invalidVideoFormat = -71
DeviceError_LCr = -99
SetGammaLUTFailed = -200
# ...
StimErrStr = dict([
(StimErrC.ok, "ok"),
(StimErrC.notYetImplemented, "Not yet implemented"),
(StimErrC.invalidDimensions, "Invalid dimensions"),
(StimErrC.invalidDuration, "Invalid duration"),
(StimErrC.invalidParamType, "Invalid parameter type"),
(StimErrC.inconsistentParams, "Inconsistent parameters"),
(StimErrC.invalidAngles, "Angle(s) <0 or >360"),
(StimErrC.existingID, "Object ID(s) already exist(s)"),
(StimErrC.noMatchingID, "Object ID(s) not found"),
(StimErrC.nothingToCompile, "No objects and/or scenes to compile"),
(StimErrC.noStimOrNotCompiled,"No stimulus defined or not yet compiled"),
(StimErrC.wrongStimFileFormat,"Wrong stimulus file format"),
(StimErrC.invalidFileNamePath,"Invalid file name or path"),
(StimErrC.noCompiledStim, "Stimulus needs to be compiled"),
(StimErrC.noDefsInRunSection, "No object definitions allowed in run section"),
(StimErrC.noShadersInRunSect, "Shader must be assigned to objects before run section"),
(StimErrC.invalidShaderType, "Invalid shader type"),
(StimErrC.notShaderObject, "Object(s) in list not shader-enabled"),
(StimErrC.movieFileNotFound, "Movie file(s) not found"),
(StimErrC.notMovieObject, "Object is not a movie"),
(StimErrC.invalidMovieDesc, "Invalid movie description"),
(StimErrC.inconsMovieDesc, "Movie description does not match image"),
(StimErrC.invalidMovieSeq, "Invalid movie sequence"),
(StimErrC.invalidMovieFormat, "Invalid movie format"),
(StimErrC.DeviceError_LCr, "Device error (Lightcrafter), code={0}"),
(StimErrC.SetGammaLUTFailed, "Failed to set gamma LUT")
])
# fmt: on
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
class StimException(Exception):
def __init__(self, value, subvalue=0):
self.value = value
if subvalue == 0:
self.str = StimErrStr[value]
else:
self.str = StimErrStr[value].format(subvalue)
def __str__(self):
return self.str
# ---------------------------------------------------------------------
# Stimulus class
# ---------------------------------------------------------------------
class Stim:
def __init__(self):
"""Initializing
"""
self.clear()
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
def clear(self):
"""Pseudo code representation of the stimulus
(with seconds and µms as units)
"""
self.nameStr = "n/a"
self.descrStr = "no description"
self.fileName = ""
self.ObjList = []
self.ObjDict = dict()
self.ShList = []
self.ShDict = dict()
self.MovList = []
self.MovDict = dict()
self.VidList = []
self.VidDict = dict()
self.SceList = []
self.nSce = 0
self.curBgRGB = SC_defaultBgRGBEx
self.LastErrC = StimErrC.ok
self.tStart = 0.0
self.isComp = False
self.isRunSect = False
self.inLoop = False
self.iLoopSce = -1
self.colorMode = ColorMode.range0_255
self.bitDepth = COL_bitDepthRGB_888
self.bitShift = COL_bitShiftRGB_000
self.Conf = None
self.ShManager = None
self.isUseLCr = False
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
def getLastErrC(self):
return self.LastErrC
def getLastErrStr(self):
return StimErrStr[self.LastErrC]
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
def defObj_box(self, _ID, _dx_um, _dy_um, _enShader):
"""Define a box object and add it to the stimulus object list
(For parameters, see QDS.py)
"""
if (_dx_um <= 0) or (_dy_um <= 0):
self.LastErrC = StimErrC.invalidDimensions
raise StimException(StimErrC.invalidDimensions)
try:
self.ObjDict[_ID]
except KeyError:
pass
else:
self.LastErrC = StimErrC.existingID
raise StimException(self.LastErrC)
newBox = [
StimObjType.box,
_ID,
(float(_dx_um), float(_dy_um)),
SO_defaultFgRGBEx,
SO_defaultAlpha,
SO_default_RGBAByVert,
_enShader,
-1,
]
self.ObjDict[_ID] = len(self.ObjList)
self.ObjList.append(newBox)
self.LastErrC = StimErrC.ok
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
def defObj_ellipse(self, _ID, _dx_um, _dy_um, _enShader):
"""Define an ellipse object and add it to the stimulus object list
(For parameters, see QDS.py)
"""
if (_dx_um <= 0) or (_dy_um <= 0):
self.LastErrC = StimErrC.invalidDimensions
raise StimException(StimErrC.invalidDimensions)
try:
self.ObjDict[_ID]
except KeyError:
pass
else:
self.LastErrC = StimErrC.existingID
raise StimException(self.LastErrC)
newEllipse = [
StimObjType.ellipse,
_ID,
(float(_dx_um), float(_dy_um)),
SO_defaultFgRGBEx,
SO_defaultAlpha,
SO_default_RGBAByVert,
_enShader,
-1,
]
self.ObjDict[_ID] = len(self.ObjList)
self.ObjList.append(newEllipse)
self.LastErrC = StimErrC.ok
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
def defObj_sector(self, _ID, _r, _offs, _angle, _awidth, _astep, _enShader):
"""Define a sector object and add it to the stimulus object list
(For parameters, see QDS.py)
"""
if (_r <= _offs) or (_r <= 0) or (_offs < 0):
self.LastErrC = StimErrC.inconsistentParams
raise StimException(StimErrC.inconsistentParams)
if (_angle < 0) or (_angle > 360) or (_awidth <= 1) or (_awidth > 360):
self.LastErrC = StimErrC.invalidAngles
raise StimException(StimErrC.invalidAngles)
if (_astep is not None) and ((_astep < 1) or (_astep > 90)):
_astep = None
try:
self.ObjDict[_ID]
except KeyError:
pass
else:
self.LastErrC = StimErrC.existingID
raise StimException(self.LastErrC)
newSector = [
StimObjType.sector,
_ID,
(float(_r), float(_offs), float(_angle), float(_awidth), _astep),
SO_defaultFgRGBEx,
SO_defaultAlpha,
SO_default_RGBAByVert,
_enShader,
-1,
]
self.ObjDict[_ID] = len(self.ObjList)
self.ObjList.append(newSector)
self.LastErrC = StimErrC.ok
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
def defShader(self, _shID, _shType):
"""Define a shader
(For parameters, see QDS.py)
Create shader manager, if not already existent, and check if
shader type exists
"""
if self.ShManager is None:
self.ShManager = csh.ShaderManager(self.Conf)
if _shType not in self.ShManager.getShaderTypes():
self.LastErrC = StimErrC.invalidShaderType
raise StimException(StimErrC.invalidShaderType)
try:
self.ShDict[_shID]
except KeyError:
pass
else:
self.LastErrC = StimErrC.existingID
raise StimException(self.LastErrC)
newShader = [
StimObjType.shader,
_shID,
_shType,
self.ShManager.getDefaultParams(_shType),
]
self.ShDict[_shID] = len(self.ShList)
self.ShList.append(newShader)
self.LastErrC = StimErrC.ok
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
def defObj_movie(self, _ID, _fName):
"""Define a movie object and add it to the movie object list
(For parameters, see QDS.py)
"""
tempMovie = mov.Movie(self.Conf)
self.LastErrC = tempMovie.load(self.Conf.pathStim + _fName)
if self.LastErrC != StimErrC.ok:
raise StimException(self.LastErrC)
try:
self.MovDict[_ID]
except KeyError:
pass
else:
self.LastErrC = StimErrC.existingID
raise StimException(self.LastErrC)
newMovie = [
StimObjType.movie,
_ID,
_fName,
tempMovie.nFr,
tempMovie.dxFr,
tempMovie.dyFr,
]
self.MovDict[_ID] = len(self.MovList)
self.MovList.append(newMovie)
self.LastErrC = StimErrC.ok
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
def getMovieParams(self, _ID):
"""Get movie object parameters
(For parameters, see QDS.py)
"""
try:
iMvOb = self.MovDict[_ID]
MvOb = self.MovList[iMvOb]
except KeyError:
self.LastErrC = StimErrC.noMatchingID
raise StimException(self.LastErrC)
d = dict()
d["dxFr"] = MvOb[SM_field_dxFr]
d["dyFr"] = MvOb[SM_field_dyFr]
d["nFr"] = MvOb[SM_field_nFr]
return d
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
def defObj_video(self, _ID, _fName):
"""Define a video object and add it to the video object list
(For parameters, see QDS.py)
"""
tempVideo = vid.Video(self.Conf)
self.LastErrC = tempVideo.load(self.Conf.pathStim + _fName)
if self.LastErrC != StimErrC.ok:
raise StimException(self.LastErrC)
try:
self.VidDict[_ID]
except KeyError:
pass
else:
self.LastErrC = StimErrC.existingID
raise StimException(self.LastErrC)
newVideo = [
StimObjType.video,
_ID,
_fName,
tempVideo.nFr,
tempVideo.dxFr,
tempVideo.dyFr,
tempVideo.fps,
]
self.VidDict[_ID] = len(self.VidList)
self.VidList.append(newVideo)
self.LastErrC = StimErrC.ok
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
def getVideoParams(self, _ID):
"""Get video object parameters
(For parameters, see QDS.py)
"""
try:
iVdOb = self.VidDict[_ID]
VdOb = self.VidList[iVdOb]
except KeyError:
self.LastErrC = StimErrC.noMatchingID
raise StimException
d = dict()
d["dxFr"] = VdOb[SV_field_dxFr]
d["dyFr"] = VdOb[SV_field_dyFr]
d["nFr"] = VdOb[SV_field_nFr]
d["fps"] = VdOb[SV_field_fps]
return d
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
def setObjColor(self, _IDs, _newRGBs, _newAlphas):
"""Changes the color and the alpha values of one or more objects
by creating a scene w/o duration
Parameters:
_IDs := list of IDs
_newRGBs := list of RGB tuples (bytes)
_newAlphas := list of alpha values (byte)
e.g.
setObjColor([1,2], [(255,128,0), (0,63,128)], [128,255])
"""
if (
not (isinstance(_IDs, list))
or not (isinstance(_newRGBs, list))
or not (isinstance(_newRGBs[0], tuple))
or not (isinstance(_newAlphas, list))
or (len(_IDs) != len(_newRGBs))
or (len(_IDs) != len(_newAlphas))
):
self.LastErrC = StimErrC.invalidParamType
raise StimException
for ID in _IDs:
try:
self.ObjDict[ID]
except KeyError:
self.LastErrC = StimErrC.noMatchingID
raise StimException
RGBEx = ssp.completeRGBList(_newRGBs)
newSce = [
StimSceType.changeObjCol,
-1,
self.nSce,
False,
_IDs,
RGBEx,
_newAlphas,
len(_IDs) * [SC_disableRGBAByVert],
]
self.SceList.append(newSce)
self.nSce += 1
self.LastErrC = StimErrC.ok
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
def setObjColorAlphaByVertex(self, _IDs, _newRGBAs):
"""Change the color(s) of the given object(s)
(For parameters, see QDS.py)
"""
if (
not (isinstance(_IDs, list))
or not (isinstance(_newRGBAs, list))
or not (isinstance(_newRGBAs[0], list))
or not (isinstance(_newRGBAs[0][0], tuple))
or (len(_IDs) != len(_newRGBAs))
):
self.LastErrC = StimErrC.invalidParamType
raise StimException
for ID in _IDs:
try:
self.ObjDict[ID]
except KeyError:
self.LastErrC = StimErrC.noMatchingID
raise StimException
RGBAEx = ssp.completeRGBAList(_newRGBAs)
newSce = [
StimSceType.changeObjCol,
-1,
self.nSce,
False,
_IDs,
RGBAEx,
len(_IDs) * [0],
len(_IDs) * [SC_enableRGBAByVert],
]
self.SceList.append(newSce)
self.nSce += 1
self.LastErrC = StimErrC.ok
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
def setColorLUTEntry(self, _index, _rgb):
"""Change a color LUT entry
(For parameters, see QDS.py)
"""
if (
(_index < 0)
or (_index > 255)
or not (isinstance(_rgb, tuple))
or len(_rgb) not in [3, 6]
):
self.LastErrC = StimErrC.invalidParamType
raise StimException
# *********************
# *********************
# *********************
# TODO
# *********************
# *********************
# *********************
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
def setShaderParams(self, _shID, _shParams):
"""Set or change the parameters of an existing shader
(For parameters, see QDS.py)
"""
if not (isinstance(_shID, int)) or not (isinstance(_shParams, list)):
self.LastErrC = StimErrC.invalidParamType
raise StimException
try:
iSh = self.ShDict[_shID]
except KeyError:
self.LastErrC = StimErrC.noMatchingID
raise StimException
shP = _shParams
shType = self.ShList[iSh][SH_field_shaderType]
try:
iShD = self.ShManager.getShaderTypes().index(shType)
ShD = self.ShManager.ShDesc[iShD]
except ValueError:
self.LastErrC = StimErrC.invalidShaderType
raise StimException
# Convert RBG values for each shader parameter (uniform) that
# contains the substring "rgb"
for i, par in enumerate(ShD[1]):
if (ShD[2][i] > 1) and ("rgb" in par.lower()):
shP[i] = ssp.scaleRGBShader(self, shP[i])
newSce = [StimSceType.changeShParams, -1, self.nSce, False, [_shID], shP]
self.SceList.append(newSce)
self.nSce += 1
self.LastErrC = StimErrC.ok
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
def setObjShader(self, _IDs, _shIDs):
"""Link object(s) with existing shader(s)
(For parameters, see QDS.py)
"""
if not (isinstance(_IDs, list)) or not (isinstance(_shIDs, list)):
self.LastErrC = StimErrC.invalidParamType
raise StimException
for ID in _IDs:
try:
# self.ObjDict[ID]
if self.ObjList[self.ObjDict[ID]][SO_field_useShader] == 0:
self.LastErrC = StimErrC.notShaderObject
raise StimException
except KeyError:
self.LastErrC = StimErrC.noMatchingID
raise StimException
for ID in _shIDs:
if ID < 0:
continue
try:
self.ShDict[ID]
except KeyError:
self.LastErrC = StimErrC.noMatchingID
raise StimException
newSce = [StimSceType.changeObjShader, -1, self.nSce, False, _IDs, _shIDs]
self.SceList.append(newSce)
self.nSce += 1
self.LastErrC = StimErrC.ok
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
def setBkgColor(self, _newRGB):
"""Changes the background color by creating a scene w/o duration
Parameters:
_newRGB := RGB tuple (bytes)
e.g.
setBkgColor((255,128,0))
"""
if not (isinstance(_newRGB, tuple)):
self.LastErrC = StimErrC.invalidParamType
raise StimException
RGBEx = ssp.completeRGBList([_newRGB])
newSce = [StimSceType.changeBkgCol, -1, self.nSce, False, RGBEx[0]]
self.SceList.append(newSce)
self.nSce += 1
self.LastErrC = StimErrC.ok
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
def awaitTTL(self):
"""..."""
newSce = [StimSceType.awaitTTL, -1, self.nSce, False]
self.SceList.append(newSce)
self.nSce += 1
self.LastErrC = StimErrC.ok
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
def getRandom(self, _seed):
"""..."""
newSce = [StimSceType.getRandom, -1, self.nSce, False, _seed]
self.SceList.append(newSce)
self.nSce += 1
self.LastErrC = StimErrC.ok
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
def logUserParameters(self, _dict):
"""Writes a user-defined set of parameters to the log file
(For parameters, see QDS.py)
"""
if not (isinstance(_dict, dict)):
self.LastErrC = StimErrC.invalidParamType
raise StimException
# **************************************
# **************************************
# TODO: Check dict for large lists or data structures and if
# present, save right here as external stimulus files to
# keep the amount of text written into the log file and
# the history during the stimulus presentation at a
# minimum
# **************************************
# **************************************
newSce = [StimSceType.logUserParams, -1, self.nSce, False, _dict]
self.SceList.append(newSce)
self.nSce += 1
self.LastErrC = StimErrC.ok
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
def clearScene(self, _dur_s, _isMarker=False):
"""Clear the scene (the screen) using the current background color
and present that scene for the given duration (if _dur_s > 0)
NOTE that all special objects, like moving bars movies etc. that
have not finished, are continued to be updated
"""
if _dur_s < 0:
self.LastErrC = StimErrC.invalidDuration
raise StimException
elif _dur_s == 0:
dur = -1
else:
dur = float(_dur_s)
newSce = [StimSceType.clearSce, dur, self.nSce, _isMarker, self.curBgRGB]
self.SceList.append(newSce)
self.nSce += 1
self.LastErrC = StimErrC.ok
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
def loopBegin(self, _nTrials):
"""..."""
if self.inLoop:
return -1
self.inLoop = True
self.iLoopSce = self.nSce
newSce = [
StimSceType.beginLoop,
-1,
self.nSce,
False,
_nTrials if _nTrials > 0 else -1,
]
self.SceList.append(newSce)
self.nSce += 1
self.LastErrC = StimErrC.ok
def loopEnd(self):
"""..."""
if not self.inLoop:
return -1
newSce = [StimSceType.endLoop, -1, self.nSce, False]
self.SceList.append(newSce)
self.nSce += 1
self.inLoop = False
self.iLoopSce = -1
self.LastErrC = StimErrC.ok
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
def processLCrCommand(self, _cmd, _p=None):
"""..."""
if _p is None:
_p = [0]
if not self.Conf.useLCr:
self.LastErrC = StimErrC.ok
return
# Check if parameters are valid for the respective lightcrafter
# commands using a "dummy" LCr object
# (Note that params[0] is always the lightcrafter device index;
# necessary for the case that there are more than one devices
# connected.)
res = [lcr.ERROR.OK]
if _cmd == StimLCrCmd.softwareReset:
res = _LCr.softwareReset()
elif _cmd == StimLCrCmd.setInputSource:
res = _LCr.setInputSource(_p[1], _p[2])
elif _cmd == StimLCrCmd.setDisplayMode:
res = _LCr.setDisplayMode(_p[1])
elif _cmd == StimLCrCmd.setTestPattern:
res = _LCr.setTestPattern(_p[1])
elif _cmd == StimLCrCmd.setLEDCurrents:
res = _LCr.setLEDCurrents(_p[1])
elif _cmd == StimLCrCmd.setLEDEnabled:
res = _LCr.setLEDEnabled(_p[1], _p[2])
if res[0] != lcr.ERROR.OK:
self.LastErrC = StimErrC.DeviceError_LCr
raise StimException
newSce = [StimSceType.sendCommandToLCr, -1, self.nSce, False, [_cmd] + _p]
self.SceList.append(newSce)
self.nSce += 1
self.isUseLCr = True
self.LastErrC = StimErrC.ok
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
def renderScene(self, _dur_s, _IDs, _posXY, _magXY, _rot, _isMarker=False):
"""Render objects and present that scene for the given duration.
NOTE that all special objects, like moving bars movies etc. that
have not finished, are continued to be updated
Parameters:
_dur_s := scene duration in s
_IDs := list of object IDs to render
_posXY := list of x,y tuples as object positions in µm
_magXY := list of magnification factors (x,y tuples)
_rot := list of rotation angles in degrees
e.g.
renderScene(1.0, [1,3], [(0,0), (50,40)], [(1.0,1.0),(0.5,0.5)],
[90,180])
"""
if (
not (isinstance(_IDs, list))
or not (isinstance(_posXY, list))
or not (isinstance(_posXY[0], tuple))
or not (isinstance(_magXY, list))
or not (isinstance(_magXY[0], tuple))
or not (isinstance(_rot, list))
or (len(_IDs) != len(_posXY))
or (len(_IDs) != len(_magXY))
or (len(_IDs) != len(_rot))
):
self.LastErrC = StimErrC.invalidParamType
raise StimException
if _dur_s <= 0:
self.LastErrC = StimErrC.invalidDuration
raise StimException
for ID in _IDs:
try:
self.ObjDict[ID]
except KeyError:
self.LastErrC = StimErrC.noMatchingID
raise StimException
newSce = [
StimSceType.renderSce,
float(_dur_s),
self.nSce,
_isMarker,
_IDs,
_posXY,
_magXY,
_rot,
]
self.SceList.append(newSce)
self.nSce += 1
self.LastErrC = StimErrC.ok
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
def startMovie(self, _ID, _posXY, _seq, _magXY, _trans, _rot, _screen=0):
"""Start playing a movie object
(For parameters, see QDS.py)
"""
if (
not (isinstance(_posXY, tuple))
or not (isinstance(_magXY, tuple))
or not (isinstance(_seq, list))
or not (len(_seq) == 4)
or (_trans < 0)
or (_trans > 255)
or (_screen < 0)
or (_screen >= glo.QDSpy_maxNumberOfScreens)
):
self.LastErrC = StimErrC.invalidParamType
raise StimException
try:
iMvOb = self.MovDict[_ID]
# if not(self.MovList[iOb][SO_field_type] == StimObjType.movie):
# self.LastErrC = StimErrC.notMovieObject
# raise(StimException(self.LastErrC))
except KeyError:
self.LastErrC = StimErrC.noMatchingID
raise StimException
tmpMCtr = mov.MovieCtrl(_seq, _ID, _nFr=self.MovList[iMvOb][SM_field_nFr])
if not (tmpMCtr.check()):
self.LastErrC = StimErrC.invalidMovieSeq
raise StimException
newSce = [
StimSceType.startMovie,
-1,
self.nSce,
False,
[_ID],
_posXY,
_magXY,
_rot,
_seq,
_trans,
_screen,
]
self.SceList.append(newSce)
self.nSce += 1
self.LastErrC = StimErrC.ok
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
def startVideo(self, _ID, _posXY, _magXY, _trans, _rot, _screen=0):
"""Start playing a video object
(For parameters, see QDS.py)
"""
if (
not (isinstance(_posXY, tuple))
or not (isinstance(_magXY, tuple))
or (_trans < 0)
or (_trans > 255)
or _screen not in [0, 1]
):
self.LastErrC = StimErrC.invalidParamType
raise StimException
try:
_ = self.VidDict[_ID]
except KeyError:
self.LastErrC = StimErrC.noMatchingID
raise StimException
newSce = [
StimSceType.startVideo,
-1,
self.nSce,
False,