-
Notifications
You must be signed in to change notification settings - Fork 0
/
DLVisual.py
executable file
·1336 lines (1137 loc) · 43.8 KB
/
DLVisual.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
# DLVisual: Set of classes for DotLattice library visual display
#
# Copyright (C) 2011 University of Virginia
# Supported by grants to the University of Virginia from the National Science Foundation
# PI: Prof. Michael Kubovy <kubovy@virginia.edu>
# Author: Yuri Spitsyn <spitsyn@virginia.edu>
#
# version: 0.19.24
# last modified: 03.21.2011
#
# Distributed under the terms of the GNU Lesser General Public License (LGPL).
#
from psychopy import visual, core, event #import some libraries from PsychoPy
import numpy as np
from numpy import pi, sqrt, sin, cos, arccos, arctan, tanh
from random import *
import OpenGL.GL as GL
import OpenGL.GLU as GLU
import OpenGL.GLUT as GLUT
class DotLattice(visual.ElementArrayStim):
"""
Class for the dot lattice stimuli bound by a circular aperture
"""
def __init__(self,
win,
session,
dotVariantsList=[],
tex_res = 128
):
"""
:Parameters:
win :
a :class:`~psychopy.visual.Window` object (required)
session:
instance of DLSession class providing the configuration data for lattice
dotVariantsNameList:
contains names of those lattice parameters that are being expected to have
variants
tex_res :
the number of pixels in the dot texture (overridden if an array
or image is provided)
"""
#
# DLSession object is the main source of lattice data
#
self.ss=session
session.setLattice(self) #make yourself known to session
self.bgnd = session.getExpdata(session.winBgnd)
#
# Dot size - should be known early on
#
self.dotSize = session.getExpdata(session.dotSize)
#
# Quad definition
#
self.aDistance = session.getExpdata(session.aDist)
self.baAspect = session.getExpdata(session.baAspect)
self.gamma = session.getExpdata(session.gamma)
self.theta = session.getExpdata(session.theta)
#
# Quad basis visibility (for demo mode)
#
self.drawBasis=False
colors = ['red', 'green', 'blue']
self.basis = [visual.ShapeStim(win=win, units="pix", vertices=((0,0),(1,1)), lineColor=colors[i], lineWidth=3, closeShape=False)
for i in range(3)]
#
# Quad computed data
#
self.dirAngles = [0,0,0,0]
self.dirMags = [0,0,0,0]
self._basisQuad = [[0,0],[0,0],[0,0],[0,0]]
self._updateBasisQuad()
#
# Possible dot variants
#
self.dotVariants=[]
#
# Dot variants alternation data
#
self.grid=[]
#
# Dot texture
#
dotTex = self._createDotTexture(tex_res)
#
# Dot mask
#
dotMask = self._createDotMask(tex_res)
#
# Create aperture
#
self.apertureSize = session.getExpdata(session.aperSize)
self.aperturePos = session.getExpdata(session.aperPos)
self.apertureColor = session.getExpdata(session.aperBgnd)
self.apertureVisible = session.getExpdata(session.aperShow)
self._abgnd = None
self._createApertureBgnd(win)
self.aperture = Aperture(win=win, size_px=self.apertureSize, pos_px=self.aperturePos)
self.showAperture(session.getExpdata(session.aperShow))
self.aperture.disable() #shouldn't be visible initially
#
# Create lattice layout
#
padding = 3
self.dotSpacing = {}
self._updateDotSpacing()
self.dotsPerSide = {
'a': int(self.apertureSize / (self.dotSize*2) + padding),
'b': int(self.apertureSize / (self.dotSize*2) + padding)
}
self.numDots = self.dotsPerSide['a'] * self.dotsPerSide['b']
self.dotXYs = self._calcLattice()
# call base class init
visual.ElementArrayStim.__init__(self, win, units='pix', xys=self.dotXYs, elementTex=dotTex, elementMask=dotMask,
texRes=tex_res, nElements = self.numDots, oris=0, sizes=self.dotSize, sfs=1.0)
#
# Dots luminance
#
self.dotLum=session.getExpdata(session.dotLum)
#
# Dots orientation
#
self._symbolicDotAngles=False
self.dotAngle = session.getExpdata(session.dotAngle)
self.setDotAngle(self.dotAngle)
#
# Create dots variants (if needed)
#
for variant in dotVariantsList:
#parlist = session.getExpdataRaw(name)
#ln = len(parlist)
#if ln>1:
if type(variant) in [list, tuple]:
if len(variant)==2:
name = variant[0]
for i in range(variant[1]):
val = session.getExpdata(name, i+1, True)
self.setDotVariantParameters(i+1,{ name: val })
else: print "dotVariantsList: the length of a variant should be 2"
else: print "dotVariantsList: variant should be a list or tuple"
#
# Compute dots alternation
#
self.dotAlternation=''
self.setDotAlternation(session.getExpdata(session.dotAlter))
#
# Prepare dots permutations data for masking
#
d = float(2*session.getExpdata(session.maskRad)) #in pixels now
self._pnum = 10
self._plutx = range(self._pnum)
self._perm = np.random.rand(self._pnum, self.numDots, 2)
self._perm *= d
self._perm -= d/2
"""
LATTICE CONFIGURATION
"""
def _createDotMask(self, res):
dotMask=self.ss.getExpdata(self.ss.dotMask)
if type(dotMask) == str:
s = dotMask.lower()
if s.find('log') != -1:
n=1
if len(s)==4:
sn=s[-1]
if sn.isdigit(): n=int(sn)
pwr=1+(n-1)*2
rad = visual.makeRadialMatrix(res)
sq = np.sqrt(2)+0.0001
mx=np.log(sq)**pwr
scale=2/(1+mx)
p = np.log(sq-rad)**pwr
dotMask = (p>=-1)*((p+1)*scale)-1
return dotMask
def _createDotTexture(self, res):
tex = self.ss.getExpdata(self.ss.dotTex)
if type(tex) == str:
s = tex.lower()
if s.find('tanh') != -1:
n=1
if len(s)==5:
sn=s[-1]
if sn.isdigit(): n=int(sn)
tanhA = 0.5*(2-int(n-1)/3)
tanhC = 4+(0 == int(n)%3)
tanhX = 5 + (0 != int(n-1)%3) +2*(0 == int(n)%3)
tex = self.makeTanh(res, tanhA, tanhC, tanhX, -1, 1)
elif s == 'flat':
tex = np.ones((res,res), dtype=float)
return tex
def setPracticeMode(self, mode):
self.practice=mode
def basisVisible(self, bv):
self.drawBasis=bv
def setDotAlternation(self, mode=None):
self.dotAlternation=mode
if mode == 'a':
self.setDotVariantsGrid([[0,1]])
elif mode == 'b':
self.setDotVariantsGrid([0,1])
else:
self.setDotVariantsGrid([])
def _createApertureBgnd(self, win):
v = self.apertureSize/2
p = self.aperturePos
quad = [[-v,-v],[v,-v],[v,v],[-v,v]]
self._abgnd = visual.ShapeStim(win=win, units="pix", pos=p, vertices=quad,
lineColor=self.apertureColor, fillColor=self.apertureColor)
def setDotVariantParameters(self, variant, parms):
"""
This method sets parameters for the dot variant.
'parms' should be a dictionary of parameter names: values.
"""
if variant>0:
variant -= 1 #make it to be an index into self.dotVariants
vlen = len(self.dotVariants)
if variant <= vlen:
if variant==vlen: #add new dot variant
self.dotVariants.append(parms)
else: #update existing dot variant
vdict = self.dotVariants[variant]
for k, v in parms.iteritems():
if k=="dotAngle":
self.setDotAngle(v, variant)
elif k=="dotLum":
self.setDotLum(v, variant)
else: vdict[k]=v
else: print "addDotVariantParameters(): 'variant' parameter (%d) is larger than dotVariants list length (%d)" % (variant, vlen)
else: print "addDotVariantParameters(): 'variant' parameter (%d) has to be positive" % variant
def setDotVariantsGrid(self, grid):
"""
Grid specifies the pattern of dot variants (if more than one is present in the lattice)
'grid' parameter defines the pattern in a row-wise fashion. For instance, if two
dot variants are present, then
a) to create lattice with alternating columns grid=[[0,1]]
b) to create lattice with alternating rows grid=[0,1]
c) for checkerboard pattern grid=[[0,1],[1,0]] etc
In examples 0 specifies the main dot variant that is initially created with the lattice,
1 specifies the first dot variant in the dotVariants list.
"""
self.grid=grid
self._updateDotAngles()
self._updateDotLums()
def _updateBasisQuad(self):
w = self.aDistance #1.0 #width of quad
h = w * self.baAspect * sin(np.radians(self.gamma)) #height of quad
bx = w * self.baAspect * cos(np.radians(self.gamma)) #x coord of point b
dp = w + bx #x projection of vector d
cp = np.abs(w - bx) #x projection of vector c
# Compute directions
self.dirAngles[0]= self.theta
self.dirAngles[1]= self.theta + self.gamma
self.dirAngles[2]= self.theta + 180 - np.degrees(arctan(h/cp))
self.dirAngles[3]= self.theta + np.degrees(arctan(h/dp))
# Compute vector magnitudes
self.dirMags[0]= w
self.dirMags[1]= w * self.baAspect
self.dirMags[2]= sqrt(cp**2 + h**2)
self.dirMags[3]= sqrt(dp**2 + h**2)
# Compute quad's points
# a direction point
self._basisQuad[1][0]= self.dirMags[0] * cos(np.radians(self.dirAngles[0]))
self._basisQuad[1][1]= self.dirMags[0] * sin(np.radians(self.dirAngles[0]))
# b direction point
self._basisQuad[2][0]= self.dirMags[1] * cos(np.radians(self.dirAngles[1]))
self._basisQuad[2][1]= self.dirMags[1] * sin(np.radians(self.dirAngles[1]))
# d direction point
self._basisQuad[3][0]= self.dirMags[3] * cos(np.radians(self.dirAngles[3]))
self._basisQuad[3][1]= self.dirMags[3] * sin(np.radians(self.dirAngles[3]))
#update coordinates of basis
self._updateBasis()
def setApertureSize(self, apertureSize):
self.aperture.setSize(apertureSize)
def setAperturePos(self, aperturePos):
self.aperture.setPos(aperturePos)
def setGamma(self, gamma):
self.gamma = gamma
self._updateBasisQuad()
self._updateXYs()
if self._symbolicDotAngles==True:
self._updateDotAngles()
#print "gamma = %f" % gamma
def setTheta(self, theta):
self.theta = theta
self._updateBasisQuad()
self._updateXYs()
if self._symbolicDotAngles==True:
self._updateDotAngles()
#print "theta = %f" % theta
def setDotLum(self, dl, dotVariant=None):
# set proper dot variant's data
if dotVariant==None: self.dotLum = dl
else: self.dotVariants[dotVariant-1]['dotLum']=dl
self._updateDotLums()
def _updateDotLums(self):
# prepare angles
a = []
a.append(self.dotLum)
for v in self.dotVariants:
if 'dotLum' in v:
a.append(v['dotLum'])
#print a
# set dot lum(s)
g_rows = len(self.grid)
if g_rows==0 or len(a)==1:
visual.ElementArrayStim.setRgbs(self,a[0])
else:
lums = range(self.numDots)
rows = self.dotsPerSide['b']
cols = self.dotsPerSide['a']
g_row = 0
for r in range(rows):
g_item = self.grid[g_row] #grid row description
if not type(g_item) in [list, tuple]:
g_item = [g_item]
g_cols = len(g_item)
g_col = 0
rowed = r*cols
for c in range(cols):
lums[rowed+c] = a[g_item[g_col]]
g_col += 1
if g_col >= g_cols: g_col=0
g_row += 1
if g_row >= g_rows: g_row=0
visual.ElementArrayStim.setRgbs(self,lums)
#print lums
def setDotAngle(self, da, dotVariant=None):
# set proper dot variant's data
if dotVariant==None: self.dotAngle = da
else: self.dotVariants[dotVariant]['dotAngle']=da
self._updateDotAngles()
def _updateDotAngles(self):
# prepare angles
a = []
a.append(self._dereferenceDotAngle(self.dotAngle))
for v in self.dotVariants:
if 'dotAngle' in v:
a.append(self._dereferenceDotAngle(v['dotAngle']))
#print a
# set dot angle(s)
g_rows = len(self.grid)
if g_rows==0 or len(a)==1:
visual.ElementArrayStim.setOris(self,a[0])
else:
#oris = np.ndarray(shape=(self.numDots,1), dtype=float)
oris = range(self.numDots)
rows = self.dotsPerSide['b']
cols = self.dotsPerSide['a']
g_row = 0
for r in range(rows):
g_item = self.grid[g_row] #grid row description
if not type(g_item) in [list, tuple]:
g_item = [g_item]
g_cols = len(g_item)
g_col = 0
rowed = r*cols
for c in range(cols):
oris[rowed+c] = a[g_item[g_col]]
g_col += 1
if g_col >= g_cols: g_col=0
g_row += 1
if g_row >= g_rows: g_row=0
visual.ElementArrayStim.setOris(self,oris)
#print "calced oris: got there!"
#print oris
def _dereferenceDotAngle(self, da):
if type(da)==str:
if da=="a": a=self.dirAngles[0]
elif da=="b": a=self.dirAngles[1]
elif da=="c": a=self.dirAngles[2]
elif da=="d": a=self.dirAngles[3]
else: a = da
self._symbolicDotAngles=True
else: a=da
return a
def setDotSize(self, size):
self.dotSize = size
#self._updateDotSpacing()
visual.ElementArrayStim.setSizes(self,size)
#self._updateXYs()
#print "dotSize = %f" % size
def setADistance(self, aDist):
self.aDistance = aDist
self._updateBasisQuad()
self._updateDotSpacing()
self._updateXYs()
#print "aDistance = %f" % aDist
def setBAAspect(self, baAspect):
self.baAspect = baAspect
self._updateBasisQuad()
self._updateDotSpacing()
self._updateXYs()
if self._symbolicDotAngles==True:
self._updateDotAngles()
#print "baAspect = %f" % baAspect
def setContrast(self, contr):
visual.ElementArrayStim.setContrs(self,contr)
#print "contrast = %f" % contr
def _updateXYs(self):
self.dotXYs = self._calcLattice()
visual.ElementArrayStim.setXYs(self,self.dotXYs)
def _updateDotSpacing(self):
self.dotSpacing['a'] = self.aDistance #*self.dotSize
self.dotSpacing['b'] = self.aDistance*self.baAspect #*self.dotSize
def _updateBasis(self):
vertices = [[0,0],[0,0]]
for i in range(2):
vertices[1]=self._basisQuad[i+1]
self.basis[i].setVertices(vertices)
vertices.append(self._basisQuad[1])
vertices[0]=vertices[1]
vertices[1]=self._basisQuad[3]
self.basis[2].setVertices(vertices)
def _calcLattice(self):
rows = self.dotsPerSide['b']
cols = self.dotsPerSide['a']
halfXspan = cols/2 - 0.5*(1-cols%2)
#print halfXspan
halfYspan = rows/2 - 0.5*(1-rows%2)
#print halfYspan
ys, xs = np.mgrid[-halfYspan:halfYspan:1j*rows, -halfXspan:halfXspan:1j*cols]
xs = xs.reshape(self.numDots,1)
ys = ys.reshape(self.numDots,1)
#print xs
#print ys
sinG = sin(np.radians(self.gamma))
cosG = cos(np.radians(self.gamma))
sinT = sin(np.radians(self.theta))
cosT = cos(np.radians(self.theta))
aDist = self.dotSpacing['a']
bDist = self.dotSpacing['b']
dotXYs = np.ndarray(shape=(self.numDots,2))
for i in range(self.numDots):
x = xs[i]*aDist + ys[i]*bDist*cosG #x coord with gamma angle applied
y = ys[i]*bDist*sinG # same for y
# theta rotation
dotXYs[i][0] = x * cosT - y * sinT
dotXYs[i][1] = x * sinT + y * cosT
return dotXYs
def makeTanh(self, res, a, c, x, val_min, val_max):
span = val_max - val_min
x_dist, y_dist = np.mgrid[-x:x:1j*res, 0:res]
tex = (1 + a * cos(pi*x_dist/2)) * (tanh(x_dist+c) - tanh(x_dist-c))
tex /= (np.max(tex) / span) #normalize and scale
tex += val_min #shift to start with val_min instead of 0
return tex
"""
LATTICE DATA ACCESS
"""
def getBgndColor(self):
return self.bgnd
def pointInsideAperture(self, pt):
s = self.apertureSize/2
return all((pt[0]>-s, pt[0]<s, pt[1]>-s, pt[1]<s))
def getDotParameter(self, parm, dotVariant=None):
if parm=='dotAlternation':
if self.dotAlternation==None: da='no'
else: da=self.dotAlternation
return da
else:
if dotVariant==None: dv='None'
else: dv = dotVariant
s = "getDotParameter(): wrong parameter (%s) variant: %s" % (parm, str(dv))
prim = (dotVariant==None or dotVariant==0)
if prim:
if parm=='dotSize': return self.dotSize
elif parm=='dotLum': return self.dotLum
elif parm=='dotAngle': return self.dotAngle
else:
try: return self.dotVariants[dotVariant-1][parm]
except: print s
def getLatticeParameter(self, parm):
if parm=='theta': return self.theta
elif parm=='baAspect': return self.baAspect
elif parm=='gamma': return self.gamma
elif parm=='lenA': return self.dirMags[0]
elif parm=='angleA': return self.dirAngles[0]
elif parm=='lenB': return self.dirMags[1]
elif parm=='angleB': return self.dirAngles[1]
elif parm=='lenC': return self.dirMags[2]
elif parm=='angleC': return self.dirAngles[2]
elif parm=='lenD': return self.dirMags[3]
elif parm=='angleD': return self.dirAngles[3]
else: print "getlatticeParameter(): unknown parameter name (%s)" % parm
"""
LATTICE DRAWING
"""
def draw(self):
if self.apertureVisible==True:
self.aperture.enable()
self._abgnd.draw()
else: self.aperture.disable()
visual.ElementArrayStim.draw(self)
if self.drawBasis == True:
for b in self.basis:
b.draw()
if self.apertureVisible==True:
self.aperture.disable()
def drawMask(self, repDur):
clock = core.Clock()
count=0
#while count<reps:
while repDur>=clock.getTime():
if not count%self._pnum:
shuffle(self._plutx)
i=0
# permute dot positions
ndx = self._plutx[i]
perm = self._perm[ndx]
XYs = self.dotXYs + perm
visual.ElementArrayStim.setXYs(self, XYs)
self.draw()
self.win.flip()
#core.wait(repDur)
i+=1
count+=1
#restore unpermuted positions
visual.ElementArrayStim.setXYs(self, self.dotXYs)
def showAperture(self, show):
self.apertureVisible = show
class LatticeController():
"""
Class to generate lattice parameters in a controlled fashion
Three types of controllers can be created:
A) Control. This one is for an individual lattice parameter
B) Matrix. This is a cross-combination of two or more Controls.
Each Control defines one dimension of a matrix.
C) Block. This represents an explicit sequence of multi-parameter blocks
to control each subsequent experiment trial
"""
def __init__(self,
lattice
):
"""
:Parameters:
lattice :
instance of dot lattice to control
"""
self.lat=lattice
self.ctrl={}
self.mat={}
self.blockseqs={}
"""
CONFIGURATION
"""
def resetControl(self):
self.ctrl={}
def addBlocksToSequence(self, seqName, blocks):
if seqName in self.blockseqs:
seq = self.blockseqs[seqName]
lblk = len(seq['parnames'])
lins = len(blocks)
#check whether parmValues contains more than one block
count=0
if type(blocks[0]) in [tuple, list]:
if lblk == len(blocks[0]): #it seems like there is more than one block
count=lins
if count==0 and lins == lblk:
blocks=[blocks]
count=1
if count==0:
print "addBlockToSequence(): check parameters!"
else:
vals = seq['blocks']
for i in range(count):
vals.append(blocks[i])
#print seq
else:
print "addBlockToSequence(): unknown sequence name: %s!" % seqName
def createBlockSequence(self, seqName, parmNames):
"""
Blocks holds combinations of given parameters' values.
Unlike in matrix the combinations are not exhaustive,
they are not derived from the controlled parameters dictionary,
but are explicitely provided by user.
'parmNames' should be a tuple or list of parameter names, e.g. ('dotSize','dotLum2',gamma')
"""
if type(parmNames) in [list, tuple]:
count=len(parmNames)
elif type(parmNames)==str:
parmNames=[parmNames]
count=1
else:
print "createBlockSequence(): parmNames should be specified as tuple, list or a string"
return
if seqName in self.blockseqs:
print "createBlockSequence(): sequence %s already exists!" % seqName
return
else:
seq = {
'parnames': parmNames,
'blocks': []
}
self.blockseqs[seqName]=seq
#print self.blockseqs
def finalizeSequence(self, seqName, numCopies=1, shuffled=True):
if seqName in self.blockseqs:
seq = self.blockseqs[seqName]
blkcnt = len(seq['blocks'])
seqlen = blkcnt*numCopies
#set lookup order
lut=range(seqlen)
if numCopies>1:
for i in range(seqlen):
lut[i] %= blkcnt
if shuffled==True: shuffle(lut)
seq['order']=lut
seq['current']=0
else:
print "addBlockToSequence(): unknown sequence name: %s!" % seqName
def setControl(self,
parName,
parms
):
"""
Sets controlling context for specific lattice parameter
"""
#create controller item
good=True
#set defaults
parMin=None
parMax=None
parStep=None
stepCount=None
parTable=None
shuffled=True
method='regular'
#update from dictionary
if 'parMin' in parms: parMin = parms['parMin']
if 'parMax' in parms: parMax = parms['parMax']
if 'parStep' in parms: parStep = parms['parStep']
if 'stepCount' in parms: stepCount = parms['stepCount']
if 'parTable' in parms: parTable = parms['parTable']
if 'shuffled' in parms: shuffled = parms['shuffled']
if 'method' in parms: method = parms['method']
#set ranges based on known limitations
if parTable==None:
if parName=="gamma":
if parMin==None: parMin=60.0
if parMax==None: parMax=90.0
elif parName=="theta":
if parMin==None: parMin=0.0
if parMax==None: parMax=180.0
elif parName=="dotAngle":
if parMin==None: parMin=0.0
if parMax==None: parMax=180.0
elif parName=="baAspect":
if parMin==None: parMin=1.0
elif parName=="contrast":
if parMax==None: parMax=1.0
if method=='random':
if all((stepCount!=None, parMin!=None, parMax!=None)):
r = abs(parMax-parMin)
parTable=np.random.rand(stepCount)
parTable *= r
parTable += parMin
else:
print "setControl(): not enough data (count, min, max) for random generation!"
good=False
elif parStep!=None or stepCount!=None:
if parMin!=None and parMax!=None:
if parStep!=None: parTable=np.arange(parMin,parMax+parStep,parStep)
else: parTable = np.linspace(parMin,parMax,stepCount,True)
else:
print "setControl(): not enough data (min, max) for random generation!"
good=False
else:
print "setControl(): not enough data (step, count) for random generation!"
good=False
if good==True:
count = len(parTable)
if count==0: print "setControl(): zero length of parameter table!"
else:
lut=range(count)
if shuffled==True:
shuffle(lut)
controlUnit = {
'order': lut,
'table': parTable,
'current': 0,
'shuffled': shuffled
}
self.ctrl[parName]=controlUnit
#print self.ctrl
def setControlMatrix(self,
matName,
parms):
"""
Creates exhaustive combination of given parameters' values
"""
if type(parms) in [list, tuple]:
#number of matricized parameters is expected to be 2 or 3
count = len(parms)
if not all((count>=2, count<=3)):
print "setControlMatrix: number of parms should be 2 or 3"
return
permcount=1
names=[]
shape=[]
for p in parms:
#make sure all parameters are known
try:
c = self.ctrl[p]
l = len(c['table'])
names.append(p)
shape.append(l)
permcount *= l
except:
print"setControlMatrix(): unknown parameter name %s" % p
return
ndxar = np.ndarray(shape=(permcount,count), dtype=int)
if count==2:
istep = shape[1]
jstep=1
else:
istep = shape[1]*shape[2]
jstep = shape[2]
for i in range(shape[0]):
i_ndx = i*istep
for j in range(shape[1]):
j_ndx = j*jstep
if count == 2:
ndx=i_ndx+j_ndx
ndxar[ndx][0]=i
ndxar[ndx][1]=j
else:
for k in range(shape[2]):
ndx=i_ndx+j_ndx+k
ndxar[ndx][0]=i
ndxar[ndx][1]=j
ndxar[ndx][2]=k
#set lookup order
lut=range(permcount)
shuffle(lut)
#add to the matrix dictionary
m = {
'parnames': names,
'indices': ndxar,
'order': lut,
'current': 0
}
self.mat[matName]=m
else:
print "setControlMatrix(): parms should be specified as tuple or list"
"""
DATA ACCESS
"""
def getSequenceBlockCount(self, seqName):
if seqName in self.blockseqs:
seq = self.blockseqs[seqName]
return len(seq['blocks'])
else:
print "getSequenceBlockCount(): unknown sequence name: %s!" % seqName
def getPermutationsCount(self):
count=1
for k, v in self.ctrl.iteritems():
count *= len(v['table'])
return count
def getMatrixPermutationsCount(self, matName):
"""
Returns number of permutations this matrix contains
"""
try:
m = self.mat[matName]
return len(m['order'])
except:
print "getMatrixPermutationsCount(): unknown matrix name '%s'" % matName
def getSequenceLength(self, seqName):
if seqName in self.blockseqs:
seq = self.blockseqs[seqName]
return len(seq['order'])
else:
print "getSequenceLength(): unknown sequence name: %s!" % seqName
def getLongestSequenceLength(self):
ln = 0
for k in self.blockseqs.iterkeys():
seql = self.getSequenceLength(k)
ln = max(ln,seql)
return ln
"""
CONTROL
"""
def rewindAll(self):
#rewind blocks
for b in self.blockseqs.itervalues():
b['current']=0
#rewind matrices
for m in self.mat.itervalues():
m['current']=0
#rewind individual controls
self.rewindParms()
def rewindParms(self, parms=None):
if parms==None:
for v in self.ctrl.itervalues():
v['current']=0
else:
t = type(parms)
if t==str:
try:
c = self.ctrl[parms]
c['current']=0
except:
print "rewind(): non-existent parameter %s!" % parms
elif t in [list, tuple]:
for n in parms:
try:
c = self.ctrl[n]
c['current']=0
except:
print "rewind(): non-existent parameter %s!" % n
def nextInControl(self, parNames, ndx='auto'):
"""
Sets lattice parameter(s) to the currently expected value(s) and advances the control pointer
"""
if type(parNames) == str:
self._nextInControl(parNames,ndx)
elif type(parNames) in [list, tuple]:
for n in parNames:
self._nextInControl(n,ndx)
def _nextInControl(self, parName, index):
"""
Sets lattice parameter(s) to the currently expected value(s) and advances the control pointer
"""
try:
c = self.ctrl[parName]
if index=='auto': ndx = c['current']
else: ndx=index
isLast = ((len(c['table'])-ndx) == 1)
i = c['order'][ndx]
v = c['table'][i]
self._setLatticeParm(parName,v)
#update control pointer
if isLast==True:
if c['shuffled']==True: shuffle(c['order'])
c['current']=0
else: c['current']=ndx+1
except:
print "_nextInControl(): unknown parameter '%s'" % parName
def _setLatticeParm(self, parName, parVal):
if parName=="gamma":
self.lat.setGamma(parVal)
elif parName=="theta":
self.lat.setTheta(parVal)
elif parName=="dotAngle":
self.lat.setDotAngle(parVal)
elif parName=="dotSize":
self.lat.setDotSize(parVal)
elif parName=="aDistance":
self.lat.setADistance(parVal)
elif parName=="baAspect":
self.lat.setBAAspect(parVal)
elif parName=="contrast":
self.lat.setContrast(parVal)
elif parName.find("dotLum") != -1:
sn = parName[-1]
n = None
if sn.isdigit(): n = int(sn)-1
#print "lum=%d" % n
self.lat.setDotLum(parVal,n)
elif parName=="dotAlternation":
self.lat.setDotAlternation(parVal)
def nextInMatrix(self, matName):
"""
Sets lattice parameter(s) to the combination of controled parameters stored in matrix
"""
if type(matName) == str:
try:
m = self.mat[matName]
n = m['parnames']