forked from eulerlab/QDSpy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQDS.py
1000 lines (836 loc) · 36 KB
/
QDS.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 script API
This is a simple Python software for scripting and presenting stimuli
for visual neuroscience. It is based on QDS, currently uses OpenGL via
pyglet for graphics. It primarly targets Windows, but may also run on
other operating systems
Copyright (c) 2013-2019 Thomas Euler
All rights reserved.
"""
# ---------------------------------------------------------------------
__author__ = "[email protected]"
import os
os.environ['PYGAME_HIDE_SUPPORT_PROMPT'] = "hide"
import time
import sys
from datetime import datetime
import QDSpy_global as glo
import QDSpy_stim as stm
import QDSpy_stim_support as ssp
import QDSpy_config as cfg
# ---------------------------------------------------------------------
_Stim = stm.Stim()
# ---------------------------------------------------------------------
def Initialize(_sName="noname", _sDescr="nodescription", _runMode=1):
"""
Initializes the QDS library. Needs to be called **before** any other
QDS command is used.
=============== ==================================================
Parameters:
=============== ==================================================
_sName | stimulus name
_sDescr | description of stimulus (optional)
_runMode | 0=just re-compile,
| 1=run stimulus if script unchanged
=============== ==================================================
"""
_Stim.clear()
_Stim.nameStr = _sName
_Stim.descrStr = _sDescr
_Stim.ErrC = stm.StimErrC.ok
_Stim.tStart = time.time()
_Stim.isRunSect = False
_Stim.Conf = cfg.Config()
# Parse command-line arguments
#
fName = (os.path.splitext(os.path.basename(sys.argv[0])))[0]
fNameOnlyDir = os.path.dirname(sys.argv[0])
_Stim.fNameDir = fNameOnlyDir +"/" +fName
fNameDir_py = _Stim.fNameDir +".py"
fNameDir_pk = _Stim.fNameDir +".pickle"
args = cfg.getParsedArgv()
# Display startup message and return if running the up-to-date stimulus
# immediately is not requested
#
ssp.Log.write("***", glo.QDSpy_versionStr +
" Compiler - " +glo.QDSpy_copyrightStr)
ssp.Log.write(" ", "Initializing ...")
if _runMode == 0:
return
# Check if pickle-file is current, if so, run the stimulus without
# recompiling
#
tLastUpt_py = datetime.fromtimestamp(os.path.getmtime(fNameDir_py))
try:
tLastUpt_pick = datetime.fromtimestamp(os.path.getmtime(fNameDir_pk))
if tLastUpt_pick > tLastUpt_py and not args.compile:
# ***********
# CHECK
# ***********
#pythonPath = os.environ.get("PYTHONPATH", "").split(";")[1]
pythonPath = os.environ.get("PYTHONPATH", "").split(";")[0]
# ***********
if len(pythonPath) > 0:
pythonPath += "\\"
ssp.Log.write("INFO", "Script has not changed, running stimulus now ...")
os.system("python "+ pythonPath +"QDSpy_core.py -t={0} {1} {2}"
.format(args.timing, "-v" if args.verbose else "", fName))
exit()
except WindowsError:
pass
# ---------------------------------------------------------------------
def GetDefaultRefreshRate():
"""
Returns the refresh rate (in Hz) defined in the QDS configuration files.
"""
_Stage = cfg.Config().createStageFromConfig()
return _Stage.scrReqFreq_Hz
# ---------------------------------------------------------------------
def GetStimulusPath():
"""
Returns the current path of the stimulus folder. Use this function
to make sure that the script can locate user-provided accessory files
(e.g. a random number series for a noise stimulus):
.. code-block:: python
path = QDS.getStimulusPath()
file = open(path +"/parameters.txt", "r")
"""
return os.path.split(os.path.abspath(_Stim.fNameDir))[0]
# ---------------------------------------------------------------------
def GetRandom(_seed):
"""
Returns a random number in the interval [0, 1) at runtime using the
random.random() function.
=============== ==================================================
Parameters:
=============== ==================================================
_seed | 'None' or an integer value (see random.seed()
| for more information)
=============== ==================================================
"""
try:
_Stim.getRandom(_seed)
except stm.StimException as e:
ssp.Log.write("ERROR", "GetRandom: {0}, {1}".format(e.value, e))
return _Stim.LastErrC
# ---------------------------------------------------------------------
def LogUserParameters(_dict):
"""
Writes a user-defined set of parameters to the history and log file.
=============== ==================================================
Parameters:
=============== ==================================================
_dict | dictionary with parameters
| as key-value pairs
=============== ==================================================
Example for such a user parameter entry as it appears in the history
and log file:
.. code-block:: python
20151220_135948 DATA {'nTrials': 1, 'dxStim_um': 1000}
"""
try:
_Stim.logUserParameters(_dict)
except stm.StimException as e:
ssp.Log.write("ERROR", "logUserParameters: {0}, {1}".format(e.value, e))
return _Stim.LastErrC
# ---------------------------------------------------------------------
def SetColorLUTEntry (_index, _rgb):
"""
Redefines an entry in the colour lookup table (LUT), allowing to linearize
the intensity range of a display.
Note that it alters the gamma LUT at **run-time**
(see section :doc:`how_QDSpy_works`) on the operation system-side, that is
for *all* connected display devices, including the screen that shows the
GUI.
When the program ends, a linear gamma LUT will be automatically restored.
While this adjustment is completely independent of the color mode setting
(see :py:func:`QDS.setColorMode`), it is only meaningful for color modes
0 and 1, and not for special lightcrafter modes. Therefore, LUT corrections
will not be applied to stimuli using color modes >1.
=============== ==================================================
Parameters:
=============== ==================================================
_index | LUT index, 0..255
_rgb | new LUT entries as tuple (r, g, b)
| with r,g,b in 0..255
=============== ==================================================
"""
try:
_Stim.setColorLUTEntry(_index, _rgb)
except stm.StimException as e:
ssp.Log.write("ERROR", "LUT_changeEntry(Ex): {0}, {1}".format(e.value, e))
return _Stim.LastErrC
# ---------------------------------------------------------------------
def SetColorMode(_depth_bit, _shift_bit=(0,0,0),
_mode=stm.ColorMode.range0_255):
"""
Set color mode and bit depth as well as bit offset.
.. note:: Note that this conversion happens at **compile-time**
(see section :doc:`how_QDSpy_works`). Changing the mode requires
recompilation of the script to affect presentation.
Bit depth and offset each are defined by color (as (r,b,b) tuple). All
color values are scaled by the given bit depth and then bitwise left-
shifted by the given offset. For example, when color mode is 0..255
(see below), "red" is scaled:
.. code-block:: python
r_new = (r/255 x(2^BitDepth_r -1)) << BitShift_r
=============== ==================================================
Parameters:
=============== ==================================================
_depth_bit | bit depth as tuple (dr, bg, bb)
_shift_bit | bit offsets are tuple (or, og, ob)
_mode | 0=color in "gun" values, 0..255
| 1=color as fraction, 0..1
| 2=LightCrafter mode B9G9
=============== ==================================================
**LightCrafter modes (EXPERIMENTAL)**
(here, _depth_bit and _shift_bit are ignored.)
* Mode B9G9: black+8 grey levels for green and blue LEDs (dichromatic), with
blanks (=red LED) for laser-scanning (700 us LED +1400 us blank),
8 lines (=16.667 ms) per stimulus frame (=60 Hz)
* ...
"""
if isinstance(_depth_bit, tuple) and len(_depth_bit) == 3:
_Stim.bitDepth = _depth_bit
if isinstance(_shift_bit, tuple) and len(_shift_bit) == 3:
_Stim.bitShift = _shift_bit
if _mode in [stm.ColorMode.range0_255, stm.ColorMode.range0_1,
stm.ColorMode.LC_G9B9]:
_Stim.colorMode = _mode
# ---------------------------------------------------------------------
def StartScript():
"""
Start of the stimulus run section. No definitions are allowed after
this command. Must be called before QDS commands that generate
stimulus scenes are called.
"""
# ...
ssp.Log.write("ok", "{0} object(s) defined.".format(len(_Stim.ObjList)))
ssp.Log.write("ok", "{0} shader(s) defined.".format(len(_Stim.ShList)))
ssp.Log.write(" ", "Generating scenes ...")
_Stim.isRunSect = True
return
# ---------------------------------------------------------------------
def Loop(_nRepeats, _func):
"""
Loops the stimulus sequence generated by the given function.
Note that _func is called only once and the loop is coded in the
actual compiled stimulus; this can be used to keep compilation time
short and reduces the copiled stimulus file size.
=============== ==================================================
Parameters:
=============== ==================================================
_nRepeats | >0 number of repeats, <0 indefinitely
_func | python function that scripts a stimulus
=============== ==================================================
"""
_Stim.loopBegin(_nRepeats)
_func()
_Stim.loopEnd()
return
# ---------------------------------------------------------------------
def EndScript():
"""
Must be called after all stimulus scenes have been created, i.e.
at the end of the script.
"""
ssp.Log.write("ok", "{0} scene(s) defined.".format(_Stim.nSce))
# Compile stimulus
#
_Conf = cfg.Config()
_Stage = _Conf.createStageFromConfig()
_Stim.compile(_Stage)
# Save compiled stimulus code to a pickle file
#
"""
sPath = os.path.basename(main.__file__)
sFName, sFExt = os.path.splitext(sPath)
"""
_Stim.save(_Stim.fNameDir)
ssp.Log.write("ok", "... done in {0:.3f} s"
.format(time.time() -_Stim.tStart))
# ---------------------------------------------------------------------
def DefObj_BoxEx(_iobj, _dx, _dy, _enShader=0):
"""
Defines a box object and if shaders can be used for this object.
=============== ==================================================
Parameters:
=============== ==================================================
_iobj | object index
_dx,_dy | box width and height in µm
_enShader | 0=disable shaders (default)
| 1=disable shaders,
| see :py:func:`QDS.SetObjShader`
=============== ==================================================
"""
try:
if _Stim.isRunSect:
_Stim.LastErrC = stm.StimErrC.noDefsInRunSection
raise stm
if len(_Stim.ObjList) == 0:
ssp.Log.write(" ", "Defining objects ...")
_Stim.defObj_box(_iobj, _dx, _dy, _enShader)
except stm.StimException as e:
ssp.Log.write("ERROR", "DefObj_Box(Ex): {0}, {1}".format(e.value, e))
return _Stim.LastErrC
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
def DefObj_Box(_iobj, _dx, _dy, _angle=0.0):
"""
See :py:func:`QDS.DefObj_BoxEx`
.. note:: Note that "_angle" is depreciated and ignored in QDSpy. The
angle of an object is now set in the render command.
"""
if _angle != 0:
ssp.Log.write("WARNING", "DefObj_Box: 'angle' is depreciated")
DefObj_BoxEx(_iobj, _dx, _dy, 0)
# ---------------------------------------------------------------------
def DefObj_SectorEx(_iobj, _r, _offs, _angle, _awidth, _astep=None,
_enShader=0):
"""
Defines a sector object and if shaders can be used for this object.
=============== ==================================================
Parameters:
=============== ==================================================
_iobj | object index
_r | outer radius (of disk) in um
_offs | inner radius (=radius of center "hole")
| in um (0 ≤ offs < r)
_angle | rotation angle in degrees
| (of the center of the arc)
_awidth | width of sector
| (angle, in degrees)
_astep | "smoothness" of the arc
| (1° <= _astep <= 90)
| if omitted, _astep is automatically optimized
_enShader | 0=disable shaders (default)
| 1=disable shaders,
| see :py:func:`QDS.SetObjShader`
=============== ==================================================
"""
if len(_Stim.ObjList) == 0:
ssp.Log.write(" ", "Defining objects ...")
try:
_Stim.defObj_sector(_iobj, _r, _offs, _angle, _awidth, _astep,
_enShader)
except stm.StimException as e:
ssp.Log.write("ERROR", "DefObj_Sector(Ex): {0}, {1}".format(e.value, e))
return _Stim.LastErrC
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
def DefObj_Sector(_iobj, _r, _offs, _angle, _awidth, _astep=None):
"""
See :py:func:`QDS.DefObj_SectorEx`
"""
DefObj_SectorEx(_iobj, _r, _offs, _angle, _awidth, _astep, 0)
# ---------------------------------------------------------------------
def DefObj_EllipseEx(_iobj, _dx, _dy, _enShader=0):
"""
Defines an ellipse object.
=============== ==================================================
Parameters:
=============== ==================================================
_iobj | object index
_dx,_dy | ellipse diameters in µm
_enShader | 0=disable shaders (default)
| 1=disable shaders,
| see :py:func:`QDS.SetObjShader`
=============== ==================================================
"""
if len(_Stim.ObjList) == 0:
ssp.Log.write(" ", "Defining objects ...")
try:
_Stim.defObj_ellipse(_iobj, _dx, _dy, _enShader)
except stm.StimException as e:
ssp.Log.write("ERROR", "DefObj_Ellipse: {0}, {1}".format(e.value, e))
return _Stim.LastErrC
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
def DefObj_Ellipse(_iobj, _dx, _dy, _angle=0.0):
"""
See :py:func:`QDS.DefObj_EllipseEx`
.. note:: Note that "_angle" is depreciated and ignored in QDSpy. The
angle of an object is now set in the render command.
"""
if _angle != 0:
ssp.Log.write("WARNING", "DefObj_Ellipse: 'angle' is depreciated")
DefObj_EllipseEx(_iobj, _dx, _dy, 0)
# ---------------------------------------------------------------------
def DefObj_Movie(_iobj, _fName):
"""
Defines and loads a movie object.
A movie object consists of two files that have the same name but
different extensions: a text file (.txt) that describes the dimensions
of a frame and the number of frames, and an image file that contains
a montage of all frames (.png, .jpg, ...).
.. note:: In contrast to QDS, QDSpy considers the **bottom-left frame of
a montage the first frame** (in QDS it was to top-left frame).
Therefore, movie montage files have to be adapted. This can
be easily done in ImageJ:
1) load the montage into ImageJ
2) use `Image/Stacks/Tools/Montage To Stack...` to convert the
montage into a stack
3) use `Image/Transform/Flip Vertically` to mirror the stack
along the x axis
4) use `Image/Stacks/Make Montage...` to convert the stack back
into a montage
5) use `Image/Transform/Flip Vertically` again now to mirror the
montage along the x axis. Now the frame sequence starts with
the bottom-left frame.
=============== ==================================================
Parameters:
=============== ==================================================
_iobj | object index
_fName | string with movie file name
| (with image file extension)
=============== ==================================================
"""
if len(_Stim.ObjList) == 0:
ssp.Log.write(" ", "Defining objects ...")
try:
_Stim.defObj_movie(_iobj, _fName)
except stm.StimException as e:
ssp.Log.write("ERROR", "DefObj_Movie: {0}, {1}".format(e.value, e))
return _Stim.LastErrC
# ---------------------------------------------------------------------
def GetMovieParameters(_iobj):
"""
Returns a list with the parameters of a movie object or `None`, if
an error occurs. The movie object must have been loaded.
=============== ==================================================
Parameters:
=============== ==================================================
_iobj | object index
=============== ==================================================
=============== ==================================================
Returns:
=============== ==================================================
dictionary | "dxFr", "dyFr", and "nFr"
| with dx,dy the frame size in pixels,
| and nFr the number of frames
=============== ==================================================
"""
params = None
try:
params = _Stim.getMovieParams(_iobj)
except stm.StimException as e:
ssp.Log.write("ERROR", "getMovieParameters: {0}, {1}".format(e.value, e))
return params
# ---------------------------------------------------------------------
def DefObj_Video(_iobj, _fName):
"""
Defines and loads a video object.
A video object consists of a single file; currently the following
video format(s) is/are allowed: .avi
=============== ==================================================
Parameters:
=============== ==================================================
_iobj | object index
_fName | string with video file name
=============== ==================================================
"""
if len(_Stim.ObjList) == 0:
ssp.Log.write(" ", "Defining objects ...")
try:
_Stim.defObj_video(_iobj, _fName)
except stm.StimException as e:
ssp.Log.write("ERROR", "DefObj_Video: {0}, {1}".format(e.value, e))
return _Stim.LastErrC
# ---------------------------------------------------------------------
def GetVideoParameters(_iobj):
"""
Returns a list with the parameters of a video object. The video
object must have been loaded.
=============== ==================================================
Parameters:
=============== ==================================================
_iobj | object index
=============== ==================================================
=============== ==================================================
Returns:
=============== ==================================================
dictionary | "dxFr", "dyFr", "nFr", and "fps"
| with dx,dy the frame size in pixels,
| nFr the number of frames, and fps
| refresh rate in frames per second
=============== ==================================================
"""
try:
params = _Stim.getVideoParams(_iobj)
except stm.StimException as e:
ssp.Log.write("ERROR", "getVideoParams: {0}, {1}".format(e.value, e))
return params
# ---------------------------------------------------------------------
def DefShader(_ishd, _shType):
"""
Defines a shader. For details, see :doc:`shaderfiles`.
=============== ==================================================
Parameters:
=============== ==================================================
_ishd | shader index
_shType | string with shader type
| (from the shader folder)
=============== ==================================================
"""
try:
if _Stim.isRunSect:
_Stim.LastErrC = stm.StimErrC.noDefsInRunSection
raise stm
if len(_Stim.ObjList) == 0:
ssp.Log.write(" ", "Defining shaders ...")
_Stim.defShader(_ishd, _shType)
except stm.StimException as e:
ssp.Log.write("ERROR", "DefShader: {0}, {1}".format(e.value, e))
return _Stim.LastErrC
# ---------------------------------------------------------------------
def SetObjColorEx(_iobjs, _ocols, _alphas=[]):
"""
Set color of object(s) (not defined for all object types).
.. attention:: **Extended version**: Number of objects not anymore a
parameter - it is determined by the lenght of the object
index list -, and allows to define alpha (transparency)
values for each object.
=============== ==================================================
Parameters:
=============== ==================================================
_iobjs | list of objects,
| [o1, o2, ...],
| with oX being valid object indices
_ocols | list of object colors in RGB values,
| [(r1,g1,b1{,u1,v1,w1}),(r2,g2,b2{,u2,v2,w2}), ...],
| with 0 <= r,g,b,u,v,w <= 255 and
| a tuple length between 3 and 6
*_oalphas* | *list of object alpha values, 0..255*
=============== ==================================================
In the parameter ``_ocols``, the values beyond ``r,g,b`` are optional;
they are only relevant in "screen overlay mode" and otherwise ignored.
*new or changed parameters*
"""
try:
if len(_alphas) == 0:
_alphas = len(_iobjs) *[255]
_Stim.setObjColor(_iobjs, _ocols, _alphas)
except stm.StimException as e:
ssp.Log.write("ERROR", "SetObjColor(Ex): {0}, {1}".format(e.value, e))
return _Stim.LastErrC
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
def SetObjColor(_nobj, _iobjs, _ocols):
_alphas = len(_iobjs) *[255]
SetObjColorEx(_iobjs, _ocols, _alphas)
# ---------------------------------------------------------------------
def SetObjColorAlphaByVertex(_iobjs, _oRGBAs):
"""
Set color and transparency (alpha) of object(s) by vertex
(not defined for all object types).
The number of objects is determined by the lenght of the object index list.
Individual color (RGB) and alpha (A) values are expected as a tuples with 4
elements (RGBA). For each object, a list of such RGBA tuples is expected,
the number of tuples per object depends on the object type:
* box, one RGBA tuple for each corner (n=4)
* ellipse, RGBA[0]=center, RGBA[1]=circumfence, (n=2)
* sector (actual sector), RGBA[0]=center, RGBA[1]=circumfence, (n=2)
* sector (arc), RGBA[0]=outer, RGBA[1]=inner circumfence, (n=2)
=============== ==================================================
Parameters:
=============== ==================================================
_iobjs | list of objects,
| [o1, o2, ...],
| with oX being valid object indices
_oRGBAs | list of object colors/transparency as RGBA,
| [o1List, o2List, ...],
| with o1List := [(r1,g2,b1,a1),(r2, ...), ...]
| and 0 <= r,g,b,a <= 255
=============== ==================================================
"""
try:
_Stim.setObjColorAlphaByVertex(_iobjs, _oRGBAs)
except stm.StimException as e:
ssp.Log.write("ERROR", "SetObjColorAlphaByVertex: {0}, {1}"
.format(e.value, e))
return _Stim.LastErrC
# ---------------------------------------------------------------------
def SetShaderParams(_ishd, _shParams):
"""
Sets or changes the parameters of a shader.
=============== ==================================================
Parameters:
=============== ==================================================
_ishd | index of existing shader
| see :py:func:`QDS.DefShader`
_shParams | list of shader parameters,
| depending on shader type
=============== ==================================================
Parameters by shader type:
* "SINE_WAVE_GRATING", drifting or stationary sine wave grating.
[perLen_um, perDur_s, minRGBA, maxRGBA], with:
- perLen_um, period length, in um
- perDur_s, period duration, in seconds
- minRGB, minimum color +tranparency (alpha)
- maxRGB, maximum color +tranparency (alpha)
* "SINE_WAVE_GRATING_MIX", like SINE_WAVE_GRATING, but in addition, the
shader mixes the object color including transparency (RGBA) with the
grating colours, enabling transparent shader objects.
"""
try:
_Stim.setShaderParams(_ishd, _shParams)
except stm.StimException as e:
ssp.Log.write("ERROR", "SetShaderParams: {0}, {1}".format(e.value, e))
return _Stim.LastErrC
# ---------------------------------------------------------------------
def SetObjShader(_iobjs, _ishds):
"""
Attach shaders to the objects in the list.
.. attention:: Other than object color, shader assignment cannot be
changed during the run section, that is after
:py:func:`QDS.StartScript` was called.
=============== ==================================================
Parameters:
=============== ==================================================
_iobjs | list of objects,
| [o1, o2, ...],
| with oX being valid object indices
_ishds | list of existing shaders
| [s1, s2, ...],
| see :py:func:`QDS.DefShader`
| To detach a shader from an object
| set shader ID to -1
=============== ==================================================
"""
try:
"""
if _Stim.isRunSect:
_Stim.LastErrC = StimErrC.noShadersInRunSect
raise(StimException(_Stim.LastErrC))
"""
_Stim.setObjShader(_iobjs, _ishds)
except stm.StimException as e:
ssp.Log.write("ERROR", "SetObjShader: {0}, {1}".format(e.value, e))
return _Stim.LastErrC
# ---------------------------------------------------------------------
def SetBkgColor(_col):
"""
Set color of background.
=============== ==================================================
Parameters:
=============== ==================================================
_col | color in RGB values as tupple (r,g,b{,u,v,w})
=============== ==================================================
In the parameter ``_col``, the values beyond ``r,g,b`` are optional;
they are only relevant in "screen overlay mode" and otherwise ignored.
"""
try:
_Stim.setBkgColor(_col)
except stm.StimException as e:
ssp.Log.write("ERROR", "SetBkgColor: {0}, {1}".format(e.value, e))
return _Stim.LastErrC
# ---------------------------------------------------------------------
def Scene_Clear(_dur, _marker=0):
"""
Clear screen and wait.
Enabling marker causes the display of a white square in the bottom
left corner of the screen and/or the output of a TTL pulse via the
DIO24 board, if installed).
=============== ==================================================
Parameters:
=============== ==================================================
_dur | duration of scene in seconds
_marker | 0=no marker, 1=marker
=============== ==================================================
"""
try:
_Stim.clearScene(_dur, (_marker==1))
except stm.StimException as e:
ssp.Log.write("ERROR", "Scene_Clear: {0}, {1}".format(e.value, e))
return _Stim.LastErrC
# ---------------------------------------------------------------------
def Scene_RenderEx(_dur, _iobjs, _opos, _omag, _oang, _marker=0,
_screen=0):
"""
Draw objects and wait.
Enabling marker causes the display of a white square in the bottom
left corner of the screen and/or the output of a TTL pulse via the
DIO24 board, if installed).
.. attention:: **Extended version**: Number of objects not anymore a
parameter - it is determined by the lenght of the object
index list -, and allows to define magnification factors
and rotation angles for each object.
=============== ==================================================
Parameters:
=============== ==================================================
_dur | duration of scene in seconds
_iobjs | object indices, as [i1, i2, ...]
_opos | object positions, as [(x1,y1), (x2,y2), ...]
*_omag* | *object magification as [(mx,my), ...]*
*_oang* | *object rotation angles in degree as [a1, ...]*
_marker | 0=no marker, 1=marker
=============== ==================================================
*new or changed parameters*
"""
try:
_Stim.renderScene(_dur, _iobjs, _opos, _omag, _oang, (_marker==1))
except stm.StimException as e:
ssp.Log.write("ERROR", "Scene_Render(Ex): {0}, {1}".format(e.value, e))
return _Stim.LastErrC
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
def Scene_Render(_dur, _nobjs, _iobjs, _opos, _marker=0):
_omag = len(_iobjs)*[(1.0, 1.0)]
_oang = len(_iobjs)*[0.0]
Scene_RenderEx(_dur, _iobjs, _opos, _omag, _oang, _marker)
# ---------------------------------------------------------------------
def Start_Movie(_iobj, _opos, _seq, _omag, _trans, _oang, _screen=0):
"""
Start playing a movie object.
=============== ==================================================
Parameters:
=============== ==================================================
_iobj | index of movie object
_opos | object positions, as (x1,y1)
_seq | sequence parameters, as
| [fr0, fr1, frreps, sqreps], with ...
| fr0=first, f1=last frame,
| frreps=number of frame repeats,
| sqreps=number of sequence repeats
_omag | object magification as (mx,my)
_trans | transparency, 0=transparent ... 255=opaque
_oang | object rotation angles in degree
_screen | 0=standard, 1=render on 2nd screen (=2nd half
| of wide screen) in screen overlay mode
=============== ==================================================
"""
try:
_Stim.startMovie(_iobj, _opos, _seq, _omag, _trans, _oang, _screen)
except stm.StimException as e:
ssp.Log.write("ERROR", "Start_Movie: {0}, {1}".format(e.value, e))
return _Stim.LastErrC
# ---------------------------------------------------------------------
def Start_Video(_iobj, _opos, _omag, _trans, _oang, _screen=0):
"""
Start playing a movie object.
=============== ==================================================
Parameters:
=============== ==================================================
_iobj | index of movie object
_opos | object positions, as (x1,y1)
_omag | object magification as (mx,my)
_trans | transparency, 0=transparent ... 255=opaque
_oang | object rotation angles in degree
_screen | 0=standard, 1=render on 2nd screen (=2nd half
| of wide screen) in screen overlay mode
=============== ==================================================
"""
try:
_Stim.startVideo(_iobj, _opos, _omag, _trans, _oang, _screen)
except stm.StimException as e:
ssp.Log.write("ERROR", "Start_Video: {0}, {1}".format(e.value, e))
return _Stim.LastErrC
# =====================================================================
# Lightcrafter-related commands
# ---------------------------------------------------------------------
def LC_softwareReset(_devIndex):
"""
Signal the device (_devIndex) to do a software reset. This will take a
couple of seconds. After the reset the device is disconnected.
=============== ==================================================
Parameters:
=============== ==================================================
_devIndex | index of device (starting with 0)
=============== ==================================================
"""
try:
_Stim.processLCrCommand(stm.StimLCrCmd.softwareReset,
[_devIndex])
except stm.StimException as e:
ssp.Log.write("ERROR", "LC_softwareReset: {0}, {1}".format(e.value, e))
return _Stim.LastErrC
# ---------------------------------------------------------------------
def LC_setInputSource(_devIndex, _source, _bitDepth):
"""
Defines the input source of the device.
=============== ==================================================
Parameters:
=============== ==================================================
_devIndex | index of device (starting with 0)
_source | 0=parallel interface (PP),
| 1=internal test pattern,
| 2=Flash
| 3=FPD-link
_bitDepth | Parallel interface bit depth
| 0=30, 1=24, 2=20, 3=16, 4=10, 5=8 bits
=============== ==================================================
"""
try:
_Stim.processLCrCommand(stm.StimLCrCmd.setInputSource,
[_devIndex, _source, _bitDepth])
except stm.StimException as e:
ssp.Log.write("ERROR", "LC_setInputSource: {0}, {1}"
.format(e.value, e))
return _Stim.LastErrC
# ---------------------------------------------------------------------
def LC_setDisplayMode(_devIndex, _mode):
"""
Sets the display mode of the device.
=============== ==================================================
Parameters:
=============== ==================================================
_devIndex | index of device (starting with 0)
_mode | 0=video display mode,
| Assumes streaming video image from the
| 30-bit RGB or FPD-link interface with a
| pixel resolution of up to 1280 × 800 up
| to 120 Hz.
| 1=Pattern display mode
| Assumes a 1-bit through 8-bit image with
| a pixel resolution of 912 × 1140 and
| bypasses all the image processing functions
| of the DLPC350.
=============== ==================================================
"""
try:
"""
_Stim.processLCrCommand(stm.StimLCrCmd.setInputSource,
[_source, _bitDepth])
"""
_Stim.processLCrCommand(stm.StimLCrCmd.setInputSource,
[_devIndex])
except stm.StimException as e:
ssp.Log.write("ERROR", "LC_setInputSource: {0}, {1}"
.format(e.value, e))
return _Stim.LastErrC
# ---------------------------------------------------------------------
def LC_setLEDCurrents(_devIndex, _rgb):
"""
Sets the current of the LEDs.
=============== ==================================================
Parameters:
=============== ==================================================
_devIndex | index of device (starting with 0)
_rgb | currents as a list [r,g,b]
| with 0 <= r,g,b <= 255
=============== ==================================================
"""
try:
_Stim.processLCrCommand(stm.StimLCrCmd.setLEDCurrents,
[_devIndex, _rgb])
except stm.StimException as e:
ssp.Log.write("ERROR", "LC_setLEDCurrents: {0}, {1}"
.format(e.value, e))
return _Stim.LastErrC
# ---------------------------------------------------------------------
def LC_setLEDEnabled(_devIndex, _rgb):
"""
Enable or disable the LEDs.
=============== ==================================================
Parameters:
=============== ==================================================
_devIndex | index of device (starting with 0)
_rgb | state of LEDas a list [r,g,b]
| with True or False
=============== ==================================================
"""
try:
_Stim.processLCrCommand(stm.StimLCrCmd.setLEDEnabled,
[_devIndex, _rgb])
except stm.StimException as e:
ssp.Log.write("ERROR", "LC_setLEDEnabled: {0}, {1}"
.format(e.value, e))
return _Stim.LastErrC
# ---------------------------------------------------------------------