class Test_Brush(): """Test suite for Brush component""" def setup_class(self): self.win = Window([128, 128], pos=[50, 50], allowGUI=False, autoLog=False) def teardown_class(self): self.win.close() def test_line_color(self): colors = ['black', 'red'] for color in colors: testBrush = Brush(self.win, lineColor=color) assert testBrush.lineColor == color def test_line_width(self): widths = [1, 5] for width in widths: testBrush = Brush(self.win, lineWidth=width) assert testBrush.lineWidth == width def test_close_shape(self): testBrush = Brush(self.win) assert testBrush.closeShape == False def test_create_stroke(self): testBrush = Brush(self.win) testBrush._createStroke() assert len(testBrush.shapes) == 1 assert isinstance(testBrush.shapes[0], ShapeStim) def test_brush_down(self): testBrush = Brush(self.win) assert testBrush.brushDown == False def test_brush_vertices(self): testBrush = Brush(self.win) assert testBrush.brushPos == [] def test_at_start_point(self): testBrush = Brush(self.win) assert testBrush.atStartPoint == False def test_current_shape(self): testBrush = Brush(self.win) testBrush._createStroke() assert testBrush.currentShape == 0 def test_reset(self): testBrush = Brush(self.win) testBrush._createStroke() testBrush.reset() assert testBrush.shapes == [] assert testBrush.atStartPoint == False
class Test_Slider(object): def setup_class(self): self.win = Window([128, 128], pos=[50, 50], allowGUI=False, autoLog=False) def teardown_class(self): self.win.close() def test_color(self): colors = ['black', 'red'] for color in colors: s = Slider(self.win, color=color) assert s.line.color == color assert s.tickLines.colors == color for l in s.labelObjs: assert l.color == color def test_change_color(self): s = Slider(self.win, color='black') with pytest.raises(AttributeError): s.color = 'blue' def test_size(self): sizes = [(1, 0.1), (1.5, 0.5)] for size in sizes: s = Slider(self.win, size=size) assert s.size == size def test_change_size(self): s = Slider(self.win, size=(1, 0.1)) with pytest.raises(AttributeError): s.size = (1.5, 0.5) def test_marker_scaling_factor(self): markerScalingFactors = [1, 1.0] for thisScalingFactor in markerScalingFactors: s = Slider(self.win, markerScalingFactor=thisScalingFactor) assert s.markerScalingFactor == thisScalingFactor def test_change_marker_scaling_factor(self): s = Slider(self.win, markerScalingFactor=1.0) with pytest.raises(AttributeError): s.markerScalingFactor = 0.5
class Test_Form(object): """Test suite for Form component""" def setup_class(self): # Create temp files for storing items self.temp_dir = mkdtemp() self.fileName_xlsx = os.path.join(self.temp_dir, 'items.xlsx') self.fileName_csv = os.path.join(self.temp_dir, 'items.csv') # create some questions self.questions = [] self.genderItem = { "questionText": "What is your gender?", "questionWidth": 0.7, "type": "radio", "responseWidth": 0.3, "options": "Male, Female, Other", "layout": 'vert', "index": 0, "questionColor": "white", "responseColor": "white" } self.questions.append(self.genderItem) # then a set of ratings items = ["running", "cake", "programming"] for idx, item in enumerate(items): entry = { "questionText": "How much you like {}".format(item), "questionWidth": 0.7, "type": "rating", "responseWidth": 0.3, "options": "Lots, some, Not a lot, Longest Option", "layout": 'horiz', "index": idx + 1, "questionColor": "white", "responseColor": "white" } self.questions.append(entry) self.win = Window(units='height', allowStencil=True, autoLog=False) self.survey = Form(self.win, items=self.questions, size=(1.0, 0.3), pos=(0.0, 0.0), autoLog=False) # Create datafiles df = DataFrame(self.questions) df.to_excel(self.fileName_xlsx, index=False) df.to_csv(self.fileName_csv, index=False) def test_importItems(self): wrongFields = [{ "a": "What is your gender?", "b": 0.7, "c": "radio", "d": 0.3, "e": "Male, Female, Other", "f": 'vert', "g": "white", "h": "white" }] # this doesn't include a itemText or questionText so should get an err missingHeader = [{ "qText": "What is your gender?", "questionWidth": 0.7, "type": "radio", "responseWidth": 0.3, "options": "Other", "layout": 'vert', "index": 0, "questionColor": "white", "responseColor": "white" }] # Check options for list of dicts with pytest.raises(ValueError): self.survey = Form(self.win, items=missingHeader, size=(1.0, 0.3), pos=(0.0, 0.0), autoLog=False) # Check csv self.survey = Form(self.win, items=self.fileName_csv, size=(1.0, 0.3), pos=(0.0, 0.0), autoLog=False) # Check Excel self.survey = Form(self.win, items=self.fileName_xlsx, size=(1.0, 0.3), pos=(0.0, 0.0), randomize=False, autoLog=False) def test_set_scroll_speed(self): items = 2 for multipliers in [1, 2, 3, 4]: assert self.survey.setScrollSpeed( [0] * items, multipliers) == items * multipliers assert self.survey.setScrollSpeed( [0] * items, multipliers) == items * multipliers assert self.survey.setScrollSpeed( [0] * items, multipliers) == items * multipliers def test_response_text_wrap(self): options = ['a', 'b', 'c'] for size in [.2, .3, .4]: item = {"responseWidth": size, "options": options} def test_set_questions(self): survey = Form(self.win, items=[self.genderItem], size=(1.0, 0.3), pos=(0.0, 0.0), autoLog=False) ctrl, h, w = survey._setQuestion(self.genderItem) assert type(ctrl) == TextBox2 assert type(h) in [float, np.float64] assert type(w) in [float, np.float64] def test_set_response(self): survey = Form(self.win, items=[self.genderItem], size=(1.0, 0.3), pos=(0.0, 0.0), autoLog=False) ctrl, h, w = survey._setQuestion(self.genderItem) sliderStim, respHeight = survey._setResponse(self.genderItem) assert type(sliderStim) == Slider assert type(respHeight) in [float, np.float64] def test_form_size(self): assert self.survey.size[0] == (1.0, 0.3)[0] # width assert self.survey.size[1] == (1.0, 0.3)[1] # height def test_aperture_size(self): assert self.survey.aperture.size[0] == self.survey.size[0] assert self.survey.aperture.size[1] == self.survey.size[1] def test_border_limits(self): survey = self.survey assert survey.leftEdge == survey.pos[0] - survey.size[0] / 2.0 assert survey.rightEdge == survey.pos[0] + survey.size[0] / 2.0 assert survey.topEdge == survey.pos[1] + survey.size[1] / 2.0 def test_text_height(self): assert self.survey.textHeight == 0.02 def test_item_padding(self): assert self.survey.itemPadding == 0.05 def test_form_units(self): assert self.survey.units == 'height' def test_screen_status(self): """Test whether the object is visible""" assert self.survey._inRange(self.survey.items[0]['itemCtrl']) def test_get_data(self): self.survey = Form(self.win, items=self.questions, size=(1.0, 0.3), pos=(0.0, 0.0), autoLog=False) data = self.survey.getData() Qs = [this['itemText'] for this in data] indices = [item['index'] for item in data] assert Qs == [ 'What is your gender?', 'How much you like running', 'How much you like cake', 'How much you like programming', ] assert all([item['response'] is None for item in data]) assert all([item['rt'] is None for item in data]) assert list(indices) == list(range(4)) def teardown_class(self): shutil.rmtree(self.temp_dir) self.win.close()
from psychopy import core from psychopy import misc from psychopy.visual.windowwarp import Warper from psychopy.monitors import Monitor from psychopy.visual.grating import GratingStim from psychopy.visual.window import Window mon = Monitor('GenericMonitor', width=33.169, distance=10) mon.setSizePix((1440, 900)) win = Window((1440, 900), monitor=mon, fullscr=True, useFBO=True, allowStencil=True) warper = Warper(win, warp='spherical', warpGridsize=300, eyepoint=[0.5, 0.5]) stim = GratingStim(win=win, units='deg', tex='sin', sf=0.1, size=misc.pix2deg(win.size, win.monitor)) print win.size print 'win size', win.size print 'mon size', win.monitor.getSizePix() print 'as deg', misc.pix2deg(win.size, win.monitor) stim.draw() win.flip() core.wait(0.5) win.close()
class Test_Form(object): """Test suite for Form component""" def setup_class(self): self.questions = [] self.win = Window(units='height', allowStencil=True, autoLog=False) # create some questions self.genderItem = {"questionText": "What is your gender?", "questionWidth": 0.7, "type": "radio", "responseWidth": 0.3, "options": "Male, Female, Other", "layout": 'vert', "index": 0} self.questions.append(self.genderItem) # then a set of ratings items = ["running", "cake", "programming"] for idx, item in enumerate(items): entry = {"questionText": "How much you like {}".format(item), "questionWidth": 0.7, "type": "rating", "responseWidth": 0.3, "options":"Lots, some, Not a lot, Longest Option", "layout": 'horiz', "index": idx+1} self.questions.append(entry) self.survey = Form(self.win, items=self.questions, size=(1.0, 0.3), pos=(0.0, 0.0), autoLog=False) def test_importItems(self): wrongFields = [{"a": "What is your gender?", "b": 0.7, "c": "radio", "d": 0.3, "e": "Male, Female, Other", "f": 'vert'}] wrongOptions = [{"questionText": "What is your gender?", "questionWidth": 0.7, "type": "radio", "responseWidth": 0.3, "options": "Other", "layout": 'vert', "index": 0}] df = DataFrame(self.questions) df.to_excel(fileName_xlsx, index=False) df.to_csv(fileName_csv, index=False) # Check wrong field error with pytest.raises(NameError): self.survey = Form(self.win, items=wrongFields, size=(1.0, 0.3), pos=(0.0, 0.0), autoLog=False) # Check options for list of dicts with pytest.raises(ValueError): self.survey = Form(self.win, items=wrongOptions, size=(1.0, 0.3), pos=(0.0, 0.0), autoLog=False) # Check csv self.survey = Form(self.win, items=fileName_csv, size=(1.0, 0.3), pos=(0.0, 0.0), autoLog=False) # Check Excel self.survey = Form(self.win, items=fileName_xlsx, size=(1.0, 0.3), pos=(0.0, 0.0), randomize=False, autoLog=False) def test_randomize_items(self): assert self.questions == self.survey.items self.survey.randomize = True assert self.questions != self.survey.randomizeItems(self.questions) def test_set_scroll_speed(self): items = 2 for multipliers in [1,2,3,4]: assert self.survey.setScrollSpeed([0] * items, multipliers) == items * multipliers assert self.survey.setScrollSpeed([0] * items, multipliers) == items * multipliers assert self.survey.setScrollSpeed([0] * items, multipliers) == items * multipliers def test_question_text_wrap(self): for size in [.2, .3, .4]: assert self.survey._questionTextWrap(size) == size * self.survey.size[0] - (self.survey.itemPadding * 2) def test_response_text_wrap(self): options = ['a', 'b', 'c'] for size in [.2, .3, .4]: item = {"responseWidth": size, "options": options} assert self.survey._responseTextWrap(item) == size * self.survey.size[0] / len(options) def test_set_questions(self): survey = Form(self.win, items=[self.genderItem], size=(1.0, 0.3), pos=(0.0, 0.0), autoLog=False) textStim, questionHeight, questionWidth = survey._setQuestion(self.genderItem) assert type(textStim) == TextStim assert type(questionHeight) == float assert type(questionWidth) == float def test_set_response(self): survey = Form(self.win, items=[self.genderItem], size=(1.0, 0.3), pos=(0.0, 0.0), autoLog=False) textStim, questionHeight, questionWidth = survey._setQuestion(self.genderItem) sliderStim, respHeight = survey._setResponse(self.genderItem, textStim) assert type(sliderStim) == Slider assert type(respHeight) == float def test_form_size(self): assert self.survey.size[0] == (1.0, 0.3)[0] # width assert self.survey.size[1] == (1.0, 0.3)[1] # height def test_aperture_size(self): assert self.survey.aperture.size[0] == self.survey.size[0] assert self.survey.aperture.size[1] == self.survey.size[1] def test_border_limits(self): survey = self.survey assert survey.leftEdge == survey.pos[0] - survey.size[0]/2.0 assert survey.rightEdge == survey.pos[0] + survey.size[0]/2.0 assert survey.topEdge == survey.pos[1] + survey.size[1]/2.0 def test_text_height(self): assert self.survey.textHeight == 0.02 def test_item_padding(self): assert self.survey.itemPadding == 0.05 def test_form_units(self): assert self.survey.units == 'height' def test_scroll_offset(self): for idx, positions in enumerate([1, 0]): # 1 is start position self.survey.scrollbar.markerPos = positions posZeroOffset = (self.survey.size[1] - self.survey.itemPadding + min(self.survey._baseYpositions)) assert self.survey._getScrollOffset() == [0., posZeroOffset][idx] def test_screen_status(self): assert self.survey._inRange(self.survey.formElements['question'][0]) if constants.PY3: with pytest.raises(AssertionError): assert self.survey._inRange(self.survey.formElements['question'][3]) def test_get_data(self): self.survey = Form(self.win, items=self.questions, size=(1.0, 0.3), pos=(0.0, 0.0), autoLog=False) data = self.survey.getData() assert set(data['questions']) == {'What is your gender?', 'How much you like running', 'How much you like cake', 'How much you like programming',} assert set(data['ratings']) == {None} assert set(data['rt']) == {None} assert set(data['itemIndex']) == {0, 1, 2, 3} def teardown_class(self): self.win.close()
class Test_Slider(_TestColorMixin): def setup_class(self): self.win = Window([128, 128], pos=[50, 50], allowGUI=False, autoLog=False) self.obj = Slider(self.win, units="height", size=(1, 0.1), pos=(0, 0.5), style='radio') self.obj.markerPos = 1 # Pixel which is the border color self.borderPoint = (0, 127) self.borderUsed = True # Pixel which is the fill color self.fillPoint = (0, 0) self.fillUsed = True # Pixel which is the fore color self.forePoint = (0, 0) self.foreUsed = False def teardown_class(self): self.win.close() def test_size(self): sizes = [(1, 0.1), (1.5, 0.5)] for size in sizes: s = Slider(self.win, size=size) assert s.size == size def test_change_size(self): s = Slider(self.win, size=(1, 0.1)) with pytest.raises(AttributeError): s.size = (1.5, 0.5) def test_lineLength(self): s = Slider(self.win, size=(1, 0.1)) assert s._lineL == 1 def test_tickWidth(self): s = Slider(self.win, size=(1, 0.1)) assert s._lineW == (1 * s._lineAspectRatio) def test_horiz(self): s = Slider(self.win, size=(1, 0.1)) assert s.horiz == True s = Slider(self.win, size=(0.1, 1)) assert s.horiz == False def test_reset(self): s = Slider(self.win, size=(1, 0.1)) s.markerPos = 1 s.history = [1] s.rating = 1 s.rt = 1 s.status = constants.STARTED s.reset() assert s.markerPos == None assert s.history == [] assert s.rating == None assert s.rt == None assert s.status == constants.NOT_STARTED def test_elements(self): s = Slider(self.win, size=(1, 0.1)) assert type(s.line) == type(GratingStim(self.win)) assert type(s.tickLines) == type(ElementArrayStim(self.win)) assert type(s.marker) == type(Circle(self.win)) assert type(s.validArea) == type(Rect(self.win)) def test_pos(self): s = Slider(self.win, size=(1, 0.1)) positions = [(.05, .05), (0.2, .2)] for newPos in positions: s.pos = newPos assert array_equal(s.pos, newPos) def test_ratingToPos(self): s = Slider( self.win, size=(1, 0.1), ) assert s._ratingToPos(3)[0][0] == 0 assert s._ratingToPos(1)[0][0] == -.5 assert s._ratingToPos(5)[0][0] == .5 def test_posToRatingToPos(self): s = Slider( self.win, size=(1, 0.1), ) assert s._posToRating((0, 0)) == 3 assert s._posToRating((-.5, 0)) == 1 assert s._posToRating((.5, 0)) == 5 def test_tickLocs(self): s = Slider( self.win, size=(1, 0.1), ) assert s.tickLocs[0][0] == -.5 and s.tickLocs[0][1] == 0.0 assert s.tickLocs[1][0] == -.25 and s.tickLocs[1][1] == 0.0 assert s.tickLocs[2][0] == .0 and s.tickLocs[2][1] == 0.0 assert s.tickLocs[3][0] == .25 and s.tickLocs[3][1] == 0.0 assert s.tickLocs[4][0] == .5 and s.tickLocs[4][1] == 0.0 def test_labelLocs(self): s = Slider(self.win, size=(1, 0.1), labels=('a', 'b', 'c', 'd', 'e')) assert s.labelLocs[0][0] == -.5 and s.labelLocs[0][1] == -.1 assert s.labelLocs[1][0] == -.25 and s.labelLocs[1][1] == -.1 assert s.labelLocs[2][0] == .0 and s.labelLocs[2][1] == -.1 assert s.labelLocs[3][0] == .25 and s.labelLocs[3][1] == -.1 assert s.labelLocs[4][0] == .5 and s.labelLocs[4][1] == -.1 def test_granularity(self): s = Slider(self.win, size=(1, 0.1), granularity=1) minRating, maxRating = 1, 5 assert s.granularity == 1 assert s._granularRating(1) == minRating assert s._granularRating(5) == maxRating assert s._granularRating(0) == minRating assert s._granularRating(6) == maxRating def test_rating(self): s = Slider(self.win, size=(1, 0.1)) minRating, maxRating = 1, 5 s.rating = 1 assert s.rating == minRating s.rating = 5 assert s.rating == maxRating s.rating = 0 assert s.rating == minRating s.rating = 6 assert s.rating == maxRating def test_markerPos(self): s = Slider(self.win, size=(1, 0.1)) s._updateMarkerPos = False minPos, maxPos = 1, 5 assert s._updateMarkerPos != True s.markerPos = 1 assert s.markerPos == minPos s.markerPos = 5 assert s.markerPos == maxPos s.markerPos = 0 assert s.markerPos == minPos s.markerPos = 6 assert s.markerPos == maxPos assert s._updateMarkerPos == True def test_recordRating(self): s = Slider(self.win, size=(1, 0.1)) minRating, maxRating = 1, 5 counter = 0 for rates in range(0, 7): s.recordRating(rates, random.random()) counter += 1 ratings = [rating[0] for rating in s.history] RT = [rt[1] for rt in s.history] assert len(s.history) == counter assert len(ratings) == counter assert min(ratings) == minRating assert max(ratings) == maxRating assert len(RT) == counter assert max(RT) <= 1 assert min(RT) >= 0 def test_getRating(self): s = Slider(self.win, size=(1, 0.1)) minRating, maxRating = 1, 5 s.rating = 1 assert s.getRating() == minRating s.rating = 5 assert s.getRating() == maxRating s.rating = 0 assert s.getRating() == minRating s.rating = 6 assert s.getRating() == maxRating def test_getRT(self): s = Slider(self.win, size=(1, 0.1)) testRT = .89 s.recordRating(2, testRT) assert s.history[-1][1] == s.getRT() assert type(s.getRT()) == float assert s.getRT() == testRT
class Test_Form(object): """Test suite for Form component""" def setup_class(self): self.questions = [] self.win = Window(units='height', allowStencil=True, autoLog=False) # create some questions self.genderItem = { "questionText": "What is your gender?", "questionWidth": 0.7, "type": "radio", "responseWidth": 0.3, "options": "Male, Female, Other", "layout": 'vert', "index": 0 } self.questions.append(self.genderItem) # then a set of ratings items = ["running", "cake", "programming"] for idx, item in enumerate(items): entry = { "questionText": "How much you like {}".format(item), "questionWidth": 0.7, "type": "rating", "responseWidth": 0.3, "options": "Lots, some, Not a lot, Longest Option", "layout": 'horiz', "index": idx + 1 } self.questions.append(entry) self.survey = Form(self.win, items=self.questions, size=(1.0, 0.3), pos=(0.0, 0.0), autoLog=False) def test_importItems(self): wrongFields = [{ "a": "What is your gender?", "b": 0.7, "c": "radio", "d": 0.3, "e": "Male, Female, Other", "f": 'vert' }] wrongOptions = [{ "questionText": "What is your gender?", "questionWidth": 0.7, "type": "radio", "responseWidth": 0.3, "options": "Other", "layout": 'vert', "index": 0 }] df = DataFrame(self.questions) df.to_excel(fileName_xlsx, index=False) df.to_csv(fileName_csv, index=False) # Check wrong field error with pytest.raises(NameError): self.survey = Form(self.win, items=wrongFields, size=(1.0, 0.3), pos=(0.0, 0.0), autoLog=False) # Check options for list of dicts with pytest.raises(ValueError): self.survey = Form(self.win, items=wrongOptions, size=(1.0, 0.3), pos=(0.0, 0.0), autoLog=False) # Check csv self.survey = Form(self.win, items=fileName_csv, size=(1.0, 0.3), pos=(0.0, 0.0), autoLog=False) # Check Excel self.survey = Form(self.win, items=fileName_xlsx, size=(1.0, 0.3), pos=(0.0, 0.0), randomize=False, autoLog=False) def test_randomize_items(self): assert self.questions == self.survey.items self.survey.randomize = True assert self.questions != self.survey.randomizeItems(self.questions) def test_set_scroll_speed(self): items = 2 for multipliers in [1, 2, 3, 4]: assert self.survey.setScrollSpeed( [0] * items, multipliers) == items * multipliers assert self.survey.setScrollSpeed( [0] * items, multipliers) == items * multipliers assert self.survey.setScrollSpeed( [0] * items, multipliers) == items * multipliers def test_question_text_wrap(self): for size in [.2, .3, .4]: assert self.survey._questionTextWrap( size) == size * self.survey.size[0] - ( self.survey.itemPadding * 2) def test_response_text_wrap(self): options = ['a', 'b', 'c'] for size in [.2, .3, .4]: item = {"responseWidth": size, "options": options} assert self.survey._responseTextWrap( item) == size * self.survey.size[0] / len(options) def test_set_questions(self): survey = Form(self.win, items=[self.genderItem], size=(1.0, 0.3), pos=(0.0, 0.0), autoLog=False) textStim, questionHeight, questionWidth = survey._setQuestion( self.genderItem) assert type(textStim) == TextStim assert type(questionHeight) == float assert type(questionWidth) == float def test_set_response(self): survey = Form(self.win, items=[self.genderItem], size=(1.0, 0.3), pos=(0.0, 0.0), autoLog=False) textStim, questionHeight, questionWidth = survey._setQuestion( self.genderItem) sliderStim, respHeight = survey._setResponse(self.genderItem, textStim) assert type(sliderStim) == Slider assert type(respHeight) == float def test_form_size(self): assert self.survey.size[0] == (1.0, 0.3)[0] # width assert self.survey.size[1] == (1.0, 0.3)[1] # height def test_aperture_size(self): assert self.survey.aperture.size[0] == self.survey.size[0] assert self.survey.aperture.size[1] == self.survey.size[1] def test_border_limits(self): survey = self.survey assert survey.leftEdge == survey.pos[0] - survey.size[0] / 2.0 assert survey.rightEdge == survey.pos[0] + survey.size[0] / 2.0 assert survey.topEdge == survey.pos[1] + survey.size[1] / 2.0 def test_text_height(self): assert self.survey.textHeight == 0.02 def test_item_padding(self): assert self.survey.itemPadding == 0.05 def test_form_units(self): assert self.survey.units == 'height' def test_scroll_offset(self): for idx, positions in enumerate([1, 0]): # 1 is start position self.survey.scrollbar.markerPos = positions posZeroOffset = (self.survey.size[1] - self.survey.itemPadding + min(self.survey._baseYpositions)) assert self.survey._getScrollOffset() == [0., posZeroOffset][idx] def test_screen_status(self): assert self.survey._inRange(self.survey.formElements['question'][0]) if constants.PY3: with pytest.raises(AssertionError): assert self.survey._inRange( self.survey.formElements['question'][3]) def test_get_data(self): self.survey = Form(self.win, items=self.questions, size=(1.0, 0.3), pos=(0.0, 0.0), autoLog=False) data = self.survey.getData() assert set(data['questions']) == { 'What is your gender?', 'How much you like running', 'How much you like cake', 'How much you like programming', } assert set(data['ratings']) == {None} assert set(data['rt']) == {None} assert set(data['itemIndex']) == {0, 1, 2, 3} def teardown_class(self): self.win.close()
class Test_Slider(_TestColorMixin): def setup_class(self): self.win = Window([128, 128], pos=[50, 50], monitor="testMonitor", allowGUI=False, autoLog=False) self.obj = Slider(self.win, units="height", size=(1, 0.1), pos=(0, 0.5), style='radio') self.obj.markerPos = 1 # Pixel which is the border color self.borderPoint = (0, 127) self.borderUsed = True # Pixel which is the fill color self.fillPoint = (0, 0) self.fillUsed = True # Pixel which is the fore color self.forePoint = (0, 0) self.foreUsed = False def teardown_class(self): self.win.close() def test_horiz(self): # Define cases exemplars = [ { 'size': (1, 0.2), 'ori': 0, 'horiz': True, 'tag': 'horiz' }, # Wide slider, no rotation { 'size': (0.2, 1), 'ori': 0, 'horiz': False, 'tag': 'vert' }, # Tall slider, no rotation { 'size': (1, 0.2), 'ori': 90, 'horiz': False, 'tag': 'vert' }, # Wide slider, 90deg rotation { 'size': (0.2, 1), 'ori': 90, 'horiz': True, 'tag': 'horiz' }, # Tall slider, 90deg rotation ] tykes = [ { 'size': (1, 0.2), 'ori': 25, 'horiz': True, 'tag': 'accute_horiz' }, # Wide slider, accute rotation { 'size': (0.2, 1), 'ori': 25, 'horiz': False, 'tag': 'accute_vert' }, # Tall slider, accute rotation { 'size': (1, 0.2), 'ori': 115, 'horiz': False, 'tag': 'obtuse_horiz' }, # Wide slider, obtuse rotation { 'size': (0.2, 1), 'ori': 115, 'horiz': True, 'tag': 'obtuse_vert' }, # Tall slider, obtuse rotation ] # Try each case self.win.flip() for case in exemplars + tykes: # Make sure horiz is set as intended obj = Slider(self.win, labels=["a", "b", "c", "d"], ticks=[1, 2, 3, 4], labelHeight=0.2, labelColor='red', size=case['size'], ori=case['ori']) assert obj.horiz == case['horiz'] # Make sure slider looks as intended obj.draw() filename = f"test_slider_horiz_{case['tag']}.png" # self.win.getMovieFrame(buffer='back').save(Path(utils.TESTS_DATA_PATH) / filename) utils.compareScreenshot(Path(utils.TESTS_DATA_PATH) / filename, self.win, crit=10) self.win.flip() def test_reset(self): s = Slider(self.win, size=(1, 0.1)) s.markerPos = 1 s.history = [1] s.rating = 1 s.rt = 1 s.status = constants.STARTED s.reset() assert s.markerPos == None assert s.history == [] assert s.rating == None assert s.rt == None assert s.status == constants.NOT_STARTED def test_elements(self): s = Slider(self.win, size=(1, 0.1)) assert type(s.line) == type(Rect(self.win)) assert type(s.tickLines) == type(ElementArrayStim(self.win)) assert type(s.marker) == type(ShapeStim(self.win)) assert type(s.validArea) == type(Rect(self.win)) def test_pos(self): s = Slider(self.win, size=(1, 0.1)) positions = [(.05, .05), (0.2, .2)] for newPos in positions: s.pos = newPos assert array_equal(s.pos, newPos) def test_ratingToPos(self): s = Slider( self.win, size=(1, 0.1), ) assert s._ratingToPos(3)[0] == 0 assert s._ratingToPos(1)[0] == -.5 assert s._ratingToPos(5)[0] == .5 def test_posToRatingToPos(self): s = Slider( self.win, size=(1, 0.1), ) assert s._posToRating((0, 0)) == 3 assert s._posToRating((-.5, 0)) == 1 assert s._posToRating((.5, 0)) == 5 def test_tick_and_label_locs(self): exemplars = [ { 'ticks': [1, 2, 3, 4, 5], 'labels': ["a", "b", "c", "d", "e"], 'tag': "simple" }, { 'ticks': [1, 2, 3, 9, 10], 'labels': ["a", "b", "c", "d", "e"], 'tag': "clustered" }, { 'ticks': [1, 2, 3, 4, 5], 'labels': ["", "b", "c", "d", ""], 'tag': "blanks" }, { 'ticks': None, 'labels': ["a", "b", "c", "d", "e"], 'tag': "noticks" }, { 'ticks': [1, 2, 3, 4, 5], 'labels': None, 'tag': "nolabels" }, ] tykes = [ { 'ticks': [1, 2, 3], 'labels': ["a", "b", "c", "d", "e"], 'tag': "morelabels" }, { 'ticks': [1, 2, 3, 4, 5], 'labels': ["a", "b", "c"], 'tag': "moreticks" }, { 'ticks': [1, 9, 10], 'labels': ["a", "b", "c", "d", "e"], 'tag': "morelabelsclustered" }, { 'ticks': [1, 7, 8, 9, 10], 'labels': ["a", "b", "c", "d"], 'tag': "moreticksclustered" }, ] # Test all cases self.win.flip() for case in exemplars + tykes: # Make vertical slider vert = Slider(self.win, size=(0.1, 0.5), pos=(-0.25, 0), units="height", labels=case['labels'], ticks=case['ticks']) vert.draw() # Make horizontal slider horiz = Slider(self.win, size=(0.5, 0.1), pos=(0.2, 0), units="height", labels=case['labels'], ticks=case['ticks']) horiz.draw() # Compare screenshot filename = "test_slider_ticklabelloc_%(tag)s.png" % case #self.win.getMovieFrame(buffer='back').save(Path(utils.TESTS_DATA_PATH) / filename) utils.compareScreenshot( Path(utils.TESTS_DATA_PATH) / filename, self.win) self.win.flip() def test_granularity(self): s = Slider(self.win, size=(1, 0.1), granularity=1) minRating, maxRating = 1, 5 assert s.granularity == 1 assert s._granularRating(1) == minRating assert s._granularRating(5) == maxRating assert s._granularRating(0) == minRating assert s._granularRating(6) == maxRating def test_rating(self): s = Slider(self.win, size=(1, 0.1)) minRating, maxRating = 1, 5 s.rating = 1 assert s.rating == minRating s.rating = 5 assert s.rating == maxRating s.rating = 0 assert s.rating == minRating s.rating = 6 assert s.rating == maxRating def test_markerPos(self): s = Slider(self.win, size=(1, 0.1)) s._updateMarkerPos = False minPos, maxPos = 1, 5 assert s._updateMarkerPos != True s.markerPos = 1 assert s.markerPos == minPos s.markerPos = 5 assert s.markerPos == maxPos s.markerPos = 0 assert s.markerPos == minPos s.markerPos = 6 assert s.markerPos == maxPos assert s._updateMarkerPos == True def test_recordRating(self): s = Slider(self.win, size=(1, 0.1)) minRating, maxRating = 1, 5 counter = 0 for rates in range(0, 7): s.recordRating(rates, random.random()) counter += 1 ratings = [rating[0] for rating in s.history] RT = [rt[1] for rt in s.history] assert len(s.history) == counter assert len(ratings) == counter assert min(ratings) == minRating assert max(ratings) == maxRating assert len(RT) == counter assert max(RT) <= 1 assert min(RT) >= 0 def test_getRating(self): s = Slider(self.win, size=(1, 0.1)) minRating, maxRating = 1, 5 s.rating = 1 assert s.getRating() == minRating s.rating = 5 assert s.getRating() == maxRating s.rating = 0 assert s.getRating() == minRating s.rating = 6 assert s.getRating() == maxRating def test_getRT(self): s = Slider(self.win, size=(1, 0.1)) testRT = .89 s.recordRating(2, testRT) assert s.history[-1][1] == s.getRT() assert type(s.getRT()) == float assert s.getRT() == testRT
class Test_Slider(object): def setup_class(self): self.win = Window([128, 128], pos=[50, 50], allowGUI=False, autoLog=False) def teardown_class(self): self.win.close() def test_color(self): colors = ['black', 'red'] for color in colors: s = Slider(self.win, color=color) assert s.line.color == color assert s.tickLines.colors == color for l in s.labelObjs: assert l.color == color def test_change_color(self): s = Slider(self.win, color='black') with pytest.raises(AttributeError): s.color = 'blue' def test_size(self): sizes = [(1, 0.1), (1.5, 0.5)] for size in sizes: s = Slider(self.win, size=size) assert s.size == size def test_change_size(self): s = Slider(self.win, size=(1, 0.1)) with pytest.raises(AttributeError): s.size = (1.5, 0.5) def test_lineLength(self): s = Slider(self.win, size=(1, 0.1)) assert s._lineL == 1 def test_tickWidth(self): s = Slider(self.win, size=(1, 0.1)) assert s._lineW == (1 * s._lineAspectRatio) def test_horiz(self): s = Slider(self.win, size=(1, 0.1)) assert s.horiz == True s = Slider(self.win, size=(0.1, 1)) assert s.horiz == False def test_reset(self): s = Slider(self.win, size=(1, 0.1)) s.markerPos = 1 s.history = [1] s.rating = 1 s.rt = 1 s.status = constants.STARTED s.reset() assert s.markerPos == None assert s.history == [] assert s.rating == None assert s.rt == None assert s.status == constants.NOT_STARTED def test_elements(self): s = Slider(self.win, size=(1, 0.1)) assert type(s.line) == type(GratingStim(self.win)) assert type(s.tickLines) == type(ElementArrayStim(self.win)) assert type(s.marker) == type(Circle(self.win)) assert type(s.validArea) == type(Rect(self.win)) def test_ratingToPos(self): s = Slider( self.win, size=(1, 0.1), ) assert s._ratingToPos(3)[0][0] == 0 assert s._ratingToPos(1)[0][0] == -.5 assert s._ratingToPos(5)[0][0] == .5 def test_posToRatingToPos(self): s = Slider( self.win, size=(1, 0.1), ) assert s._posToRating((0, 0)) == 3 assert s._posToRating((-.5, 0)) == 1 assert s._posToRating((.5, 0)) == 5 def test_tickLocs(self): s = Slider( self.win, size=(1, 0.1), ) assert s.tickLocs[0][0] == -.5 and s.tickLocs[0][1] == 0.0 assert s.tickLocs[1][0] == -.25 and s.tickLocs[1][1] == 0.0 assert s.tickLocs[2][0] == .0 and s.tickLocs[2][1] == 0.0 assert s.tickLocs[3][0] == .25 and s.tickLocs[3][1] == 0.0 assert s.tickLocs[4][0] == .5 and s.tickLocs[4][1] == 0.0 def test_labelLocs(self): s = Slider(self.win, size=(1, 0.1), labels=('a', 'b', 'c', 'd', 'e')) assert s.labelLocs[0][0] == -.5 and s.labelLocs[0][1] == -.1 assert s.labelLocs[1][0] == -.25 and s.labelLocs[1][1] == -.1 assert s.labelLocs[2][0] == .0 and s.labelLocs[2][1] == -.1 assert s.labelLocs[3][0] == .25 and s.labelLocs[3][1] == -.1 assert s.labelLocs[4][0] == .5 and s.labelLocs[4][1] == -.1 def test_granularity(self): s = Slider(self.win, size=(1, 0.1), granularity=1) minRating, maxRating = 1, 5 assert s.granularity == 1 assert s._granularRating(1) == minRating assert s._granularRating(5) == maxRating assert s._granularRating(0) == minRating assert s._granularRating(6) == maxRating def test_rating(self): s = Slider(self.win, size=(1, 0.1)) minRating, maxRating = 1, 5 s.rating = 1 assert s.rating == minRating s.rating = 5 assert s.rating == maxRating s.rating = 0 assert s.rating == minRating s.rating = 6 assert s.rating == maxRating def test_markerPos(self): s = Slider(self.win, size=(1, 0.1)) s._updateMarkerPos = False minPos, maxPos = 1, 5 assert s._updateMarkerPos != True s.markerPos = 1 assert s.markerPos == minPos s.markerPos = 5 assert s.markerPos == maxPos s.markerPos = 0 assert s.markerPos == minPos s.markerPos = 6 assert s.markerPos == maxPos assert s._updateMarkerPos == True def test_recordRating(self): s = Slider(self.win, size=(1, 0.1)) minRating, maxRating = 1, 5 counter = 0 for rates in range(0, 7): s.recordRating(rates, random.random()) counter += 1 ratings = [rating[0] for rating in s.history] RT = [rt[1] for rt in s.history] assert len(s.history) == counter assert len(ratings) == counter assert min(ratings) == minRating assert max(ratings) == maxRating assert len(RT) == counter assert max(RT) <= 1 assert min(RT) >= 0 def test_getRating(self): s = Slider(self.win, size=(1, 0.1)) minRating, maxRating = 1, 5 s.rating = 1 assert s.getRating() == minRating s.rating = 5 assert s.getRating() == maxRating s.rating = 0 assert s.getRating() == minRating s.rating = 6 assert s.getRating() == maxRating def test_getRT(self): s = Slider(self.win, size=(1, 0.1)) testRT = .89 s.recordRating(2, testRT) assert s.history[-1][1] == s.getRT() assert type(s.getRT()) == float assert s.getRT() == testRT
def test_open_glfw_window(): from psychopy.visual.window import Window win = Window(winType='glfw', autoLog=False) assert win.winType == 'glfw' win.flip() win.close()
def test_open_glfw_window(): win = Window(winType='glfw', autoLog=False) assert win.winType == 'glfw' win.flip() win.close()
class Test_Form(object): """Test suite for Form component""" def setup_class(self): self.questions = [] self.win = Window(units='height', allowStencil=True) # create some questions self.genderItem = {"qText": "What is your gender?", "qWidth": 0.7, "aType": "choice", "aWidth": 0.3, "aOptions": ["Male", "Female", "Other"], "aLayout": 'vert'} self.questions.append(self.genderItem) # then a set of ratings items = ["running", "cake"] for item in items: entry = {"qText": "How much do you like {}".format(item), "qWidth": 0.7, "aType": "rating", "aWidth": 0.3, "aOptions": ["Lots", "Not a lot"], "aLayout": 'horiz'} self.questions.append(entry) self.survey = Form(self.win, items=self.questions, size=(1.0, 0.3), pos=(0.0, 0.0)) def test_set_questions(self): survey = Form(self.win, items=[], size=(1.0, 0.3), pos=(0.0, 0.0)) textStim, qHeight, qWidth = survey._setQuestion(self.genderItem) assert type(textStim) == TextStim assert type(qHeight) == float assert type(qWidth) == float def test_set_response(self): survey = Form(self.win, items=[], size=(1.0, 0.3), pos=(0.0, 0.0)) textStim, qHeight, qWidth = survey._setQuestion(self.genderItem) sliderStim, aHeight = survey._setResponse(self.genderItem, textStim) assert type(sliderStim) == Slider assert type(aHeight) == float def test_questionHeight(self): for item in self.survey._items['question']: assert self.survey.getQuestionHeight(item) == item.boundingBox[1] / float(self.win.size[1] / 2) def test_questionWidth(self): for item in self.survey._items['question']: assert self.survey.getQuestionWidth(item) == item.boundingBox[0] / float(self.win.size[0] / 2) def test_respHeight(self): for item in self.survey.items: if item['aLayout'] == 'vert': assert self.survey.getRespHeight(item) == (len(item['aOptions']) * self.survey.textHeight) elif item['aLayout'] == 'horiz': assert self.survey.getRespHeight(item) == self.survey.textHeight def test_form_size(self): assert self.survey.size[0] == (1.0, 0.3)[0] # width assert self.survey.size[1] == (1.0, 0.3)[1] # height def test_aperture_size(self): assert self.survey.aperture.size[0] == self.survey.size[0] assert self.survey.aperture.size[1] == self.survey.size[1] def test_border_limits(self): survey = self.survey assert survey.leftEdge == survey.pos[0] - survey.size[0]/2.0 assert survey.rightEdge == survey.pos[0] + survey.size[0]/2.0 assert survey.topEdge == survey.pos[1] + survey.size[1]/2.0 def test_text_height(self): assert self.survey.textHeight == 0.03 def test_label_height(self): assert self.survey.labelHeight == 0.02 def test_item_padding(self): assert self.survey.itemPadding == 0.05 def test_form_units(self): assert self.survey.units == 'height' def test_virtual_height(self): assert isclose(self.survey.virtualHeight, (self.survey._baseYpositions[-1]-self.survey.itemPadding), atol=0.02) # TODO: liberal atol, needs tightening up def test_baseYpositions(self): survey = Form(self.win, items=self.questions, size=(1.0, 0.3), pos=(0.0, 0.0)) testPositions = [] survey.virtualHeight = 0 for item in survey.items: question, qHeight, qWidth = survey._setQuestion(item) response, aHeight, = survey._setResponse(item, question) testPositions.append(survey.virtualHeight - max(aHeight, qHeight) + (aHeight / 2) - survey.textHeight) survey.virtualHeight -= max(aHeight, qHeight) + survey.itemPadding for idx, pos in enumerate(survey._baseYpositions): assert testPositions[idx] == pos def test_scroll_offset(self): for idx, positions in enumerate([1, 0]): # 1 is start position self.survey.scrollbar.markerPos = positions posZeroOffset = (self.survey.size[1] - self.survey.itemPadding + min(self.survey._baseYpositions)) assert self.survey._getScrollOffet() == [0., posZeroOffset][idx] def test_screen_status(self): assert self.survey._inRange(self.survey._items['question'][0]) with pytest.raises(AssertionError): assert self.survey._inRange(self.survey._items['question'][2]) def teardown_class(self): self.win.close()
class Test_Slider(object): def setup_class(self): self.win = Window([128,128], pos=[50,50], allowGUI=False, autoLog=False) def teardown_class(self): self.win.close() def test_color(self): colors = ['black', 'red'] for color in colors: s = Slider(self.win, color=color) assert s.line.color == color assert s.tickLines.colors == color for l in s.labelObjs: assert l.color == color def test_change_color(self): s = Slider(self.win, color='black') with pytest.raises(AttributeError): s.color = 'blue' def test_size(self): sizes = [(1, 0.1), (1.5, 0.5)] for size in sizes: s = Slider(self.win, size=size) assert s.size == size def test_change_size(self): s = Slider(self.win, size=(1, 0.1)) with pytest.raises(AttributeError): s.size = (1.5, 0.5) def test_lineLength(self): s = Slider(self.win, size=(1, 0.1)) assert s._lineL == 1 def test_tickWidth(self): s = Slider(self.win, size=(1, 0.1)) assert s._lineW == (1 * s._lineAspectRatio) def test_horiz(self): s = Slider(self.win, size=(1, 0.1)) assert s.horiz == True s = Slider(self.win, size=(0.1, 1)) assert s.horiz == False def test_reset(self): s = Slider(self.win, size=(1, 0.1)) s.markerPos = 1 s.history = [1] s.rating = 1 s.rt = 1 s.status = constants.STARTED s.reset() assert s.markerPos == None assert s.history == [] assert s.rating == None assert s.rt == None assert s.status == constants.NOT_STARTED def test_elements(self): s = Slider(self.win, size=(1, 0.1)) assert type(s.line) == type(GratingStim(self.win)) assert type(s.tickLines) == type(ElementArrayStim(self.win)) assert type(s.marker) == type(Circle(self.win)) assert type(s.validArea) == type(Rect(self.win)) def test_pos(self): s = Slider(self.win, size=(1, 0.1)) positions = [(.05, .05),(0.2, .2)] for newPos in positions: s.pos = newPos assert array_equal(s.pos, newPos) def test_ratingToPos(self): s = Slider(self.win, size=(1, 0.1), ) assert s._ratingToPos(3)[0][0] == 0 assert s._ratingToPos(1)[0][0] == -.5 assert s._ratingToPos(5)[0][0] == .5 def test_posToRatingToPos(self): s = Slider(self.win, size=(1, 0.1), ) assert s._posToRating((0, 0)) == 3 assert s._posToRating((-.5, 0)) == 1 assert s._posToRating((.5, 0)) == 5 def test_tickLocs(self): s = Slider(self.win, size=(1, 0.1), ) assert s.tickLocs[0][0] == -.5 and s.tickLocs[0][1] == 0.0 assert s.tickLocs[1][0] == -.25 and s.tickLocs[1][1] == 0.0 assert s.tickLocs[2][0] == .0 and s.tickLocs[2][1] == 0.0 assert s.tickLocs[3][0] == .25 and s.tickLocs[3][1] == 0.0 assert s.tickLocs[4][0] == .5 and s.tickLocs[4][1] == 0.0 def test_labelLocs(self): s = Slider(self.win, size=(1, 0.1), labels=('a','b','c','d','e')) assert s.labelLocs[0][0] == -.5 and s.labelLocs[0][1] == -.1 assert s.labelLocs[1][0] == -.25 and s.labelLocs[1][1] == -.1 assert s.labelLocs[2][0] == .0 and s.labelLocs[2][1] == -.1 assert s.labelLocs[3][0] == .25 and s.labelLocs[3][1] == -.1 assert s.labelLocs[4][0] == .5 and s.labelLocs[4][1] == -.1 def test_granularity(self): s = Slider(self.win, size=(1, 0.1), granularity=1) minRating, maxRating = 1, 5 assert s.granularity == 1 assert s._granularRating(1) == minRating assert s._granularRating(5) == maxRating assert s._granularRating(0) == minRating assert s._granularRating(6) == maxRating def test_rating(self): s = Slider(self.win, size=(1, 0.1)) minRating, maxRating = 1, 5 s.rating = 1 assert s.rating == minRating s.rating = 5 assert s.rating == maxRating s.rating = 0 assert s.rating == minRating s.rating = 6 assert s.rating == maxRating def test_markerPos(self): s = Slider(self.win, size=(1, 0.1)) s._updateMarkerPos = False minPos, maxPos = 1, 5 assert s._updateMarkerPos != True s.markerPos = 1 assert s.markerPos == minPos s.markerPos = 5 assert s.markerPos == maxPos s.markerPos = 0 assert s.markerPos == minPos s.markerPos = 6 assert s.markerPos == maxPos assert s._updateMarkerPos == True def test_recordRating(self): s = Slider(self.win, size=(1, 0.1)) minRating, maxRating = 1, 5 counter = 0 for rates in range(0,7): s.recordRating(rates, random.random()) counter +=1 ratings = [rating[0] for rating in s.history] RT = [rt[1] for rt in s.history] assert len(s.history) == counter assert len(ratings) == counter assert min(ratings) == minRating assert max(ratings) == maxRating assert len(RT) == counter assert max(RT) <= 1 assert min(RT) >= 0 def test_getRating(self): s = Slider(self.win, size=(1, 0.1)) minRating, maxRating = 1, 5 s.rating = 1 assert s.getRating() == minRating s.rating = 5 assert s.getRating() == maxRating s.rating = 0 assert s.getRating() == minRating s.rating = 6 assert s.getRating() == maxRating def test_getRT(self): s = Slider(self.win, size=(1, 0.1)) testRT = .89 s.recordRating(2, testRT) assert s.history[-1][1] == s.getRT() assert type(s.getRT()) == float assert s.getRT() == testRT