def test_nomorethankinarow_validate(): with pytest.raises(ValueError): NoMoreThanKInARow("yo", color) # Levels must either be a factor/level tuple, or a Factor. NoMoreThanKInARow(1, ("factor", "level")) NoMoreThanKInARow(1, color) with pytest.raises(ValueError): NoMoreThanKInARow(1, 42) with pytest.raises(ValueError): NoMoreThanKInARow(1, ("factor", "level", "oops"))
def test_that_design_is_correctly_constrained(design): crossing = [color, motion, task] k = 2 constraints = [ NoMoreThanKInARow(k, task_transition), NoMoreThanKInARow(k, response_transition) ] block = fully_cross_block(design, crossing, constraints) experiments = synthesize_trials_non_uniform(block, 100) assert len(experiments) == 100, "Design: %s" % str(list(map(lambda f: f.name, design))) for c in constraints: assert_nomorethankinarow(c, experiments)
def test_constraint_violation(): are_constraints_violated = UniformCombinatoricSamplingStrategy._UniformCombinatoricSamplingStrategy__are_constraints_violated block = fully_cross_block([color, text, con_factor_within_trial], [color, text], [ExactlyKInARow(2, (color, red_color))]) assert are_constraints_violated( block, {color: [red_color, red_color, blue_color, blue_color]}) == False assert are_constraints_violated( block, {color: [red_color, blue_color, red_color, blue_color]}) == True block = fully_cross_block([color, text, con_factor_within_trial], [color, text], [NoMoreThanKInARow(2, (color, red_color))]) assert are_constraints_violated( block, {color: [red_color, blue_color, blue_color, blue_color]}) == False assert are_constraints_violated( block, {color: [red_color, red_color, blue_color, blue_color]}) == False assert are_constraints_violated( block, {color: [red_color, red_color, red_color, blue_color]}) == True assert are_constraints_violated( block, {color: [blue_color, red_color, red_color, red_color]}) == True
def test_nomorethankinarow_with_transition(design): block = fully_cross_block(design, [color, text], []) backend_request = __run_nomorethankinarow( NoMoreThanKInARow(1, ("color repeats?", "yes")), block) assert backend_request.ll_requests == [ LowLevelRequest("LT", 2, [17, 19]), LowLevelRequest("LT", 2, [19, 21]) ] backend_request = __run_nomorethankinarow( NoMoreThanKInARow(1, ("color repeats?", "no")), block) assert backend_request.ll_requests == [ LowLevelRequest("LT", 2, [18, 20]), LowLevelRequest("LT", 2, [20, 22]) ]
def test_nomorethankinarow_sugar(): backend_request = __run_kinarow( NoMoreThanKInARow(1, (color, get_level_from_name(color, "red")))) assert backend_request.ll_requests == [ LowLevelRequest("LT", 2, [1, 7]), LowLevelRequest("LT", 2, [7, 13]), LowLevelRequest("LT", 2, [13, 19]) ]
def test_nomorethankinarow_with_multiple_transitions(): block = fully_cross_block( [color, text, color_repeats_factor, text_repeats_factor], [color, text], []) backend_request = __run_nomorethankinarow( NoMoreThanKInARow(1, ("text repeats?", "yes")), block) assert backend_request.ll_requests == [ LowLevelRequest("LT", 2, [23, 25]), LowLevelRequest("LT", 2, [25, 27]) ] backend_request = __run_nomorethankinarow( NoMoreThanKInARow(1, ("text repeats?", "no")), block) assert backend_request.ll_requests == [ LowLevelRequest("LT", 2, [24, 26]), LowLevelRequest("LT", 2, [26, 28]) ]
def test_correct_solution_count_when_bookends_must_not_be_congruent(design): crossing = [color, text] # Require both bookends to not be congruent. constraints = [NoMoreThanKInARow(0, ("congruent bookend?", "yes"))] block = fully_cross_block(design, crossing, constraints) experiments = synthesize_trials_non_uniform(block, 100) assert len(experiments) == 4
def test_correct_solution_count_when_bookends_must_not_match_each_other( design): crossing = [color, text] # Require both bookends to be incongruent with each other. constraints = [NoMoreThanKInARow(1, congruent_bookend)] block = fully_cross_block(design, crossing, constraints) experiments = synthesize_trials_non_uniform(block, 100) assert len(experiments) == 16
def test_nomorethankinarow(): backend_request = __run_nomorethankinarow(NoMoreThanKInARow(3, color)) assert backend_request.ll_requests == [ LowLevelRequest("LT", 4, [1, 7, 13, 19]), LowLevelRequest("LT", 4, [2, 8, 14, 20]) ] backend_request = __run_nomorethankinarow( NoMoreThanKInARow(1, ("color", "red"))) assert backend_request.ll_requests == [ LowLevelRequest("LT", 2, [1, 7]), LowLevelRequest("LT", 2, [7, 13]), LowLevelRequest("LT", 2, [13, 19]) ] backend_request = __run_nomorethankinarow( NoMoreThanKInARow(2, ("color", "red"))) assert backend_request.ll_requests == [ LowLevelRequest("LT", 3, [1, 7, 13]), LowLevelRequest("LT", 3, [7, 13, 19]) ] backend_request = __run_nomorethankinarow( NoMoreThanKInARow(1, ("color", "blue"))) assert backend_request.ll_requests == [ LowLevelRequest("LT", 2, [2, 8]), LowLevelRequest("LT", 2, [8, 14]), LowLevelRequest("LT", 2, [14, 20]) ] backend_request = __run_nomorethankinarow( NoMoreThanKInARow(2, ("color", "blue"))) assert backend_request.ll_requests == [ LowLevelRequest("LT", 3, [2, 8, 14]), LowLevelRequest("LT", 3, [8, 14, 20]) ] backend_request = __run_nomorethankinarow( NoMoreThanKInARow(3, ("congruent?", "con"))) assert backend_request.ll_requests == [ LowLevelRequest("LT", 4, [5, 11, 17, 23]) ] backend_request = __run_nomorethankinarow( NoMoreThanKInARow(0, ("congruent?", "con"))) assert backend_request.ll_requests == [ LowLevelRequest("LT", 1, [5]), LowLevelRequest("LT", 1, [11]), LowLevelRequest("LT", 1, [17]), LowLevelRequest("LT", 1, [23]) ]
return response[0] == response[1] def response_switch(response): return not response_repeat(response) resp_transition = Factor("response_transition", [ DerivedLevel("repeat", Transition(response_repeat, [response])), DerivedLevel("switch", Transition(response_switch, [response])) ]) # DEFINE SEQUENCE CONSTRAINTS k = 7 constraints = [NoMoreThanKInARow(k, resp_transition)] # DEFINE EXPERIMENT design = [color, word, congruency, resp_transition, response] crossing = [color, word, resp_transition] block = fully_cross_block(design, crossing, constraints) # SOLVE from sweetpea.metrics import collect_design_metrics metrics = collect_design_metrics(block) from pprint import pprint pprint(metrics)
def __run_nomorethankinarow(c: NoMoreThanKInARow, block: Block = block) -> BackendRequest: backend_request = BackendRequest(0) c.apply(block, backend_request) return backend_request
def response_repeat(response): return response[0] == response[1] def response_switch(response): return not response_repeat(response) resp_transition = Factor("response_transition", [ DerivedLevel("repeat", Transition(response_repeat, [response])), DerivedLevel("switch", Transition(response_switch, [response])) ]) # DEFINE SEQUENCE CONSTRAINTS k = 7 constraints = [NoMoreThanKInARow(2, word_transition), NoMoreThanKInARow(2, location_transition), NoMoreThanKInARow(2, color_transition)] # DEFINE EXPERIMENT design = [color, word, congruency, location, response, resp_transition, word_transition, color_transition, location_transition] crossing = [color, congruency, location, resp_transition] block = fully_cross_block(design, crossing, constraints) # SOLVE from sweetpea.metrics import collect_design_metrics metrics = collect_design_metrics(block) from pprint import pprint
def response_repeat(responses): return responses[0] == responses[1] def response_switch(responses): return not response_repeat(responses) resp_transition = Factor("resp_transition", [ DerivedLevel("repeat", Transition(response_repeat, [response])), DerivedLevel("switch", Transition(response_switch, [response])) ]) k = 7 constraints = [ NoMoreThanKInARow(k, task_transition), NoMoreThanKInARow(k, resp_transition) ] design = [ color, motion, task, congruency, response, task_transition, resp_transition ] crossing = [color, motion, task] block = fully_cross_block(design, crossing, constraints) experiments = synthesize_trials_non_uniform(block, 5) print_experiments(block, experiments)
def response_repeat(response): return response[0] == response[1] def response_switch(response): return not response_repeat(response) response_transition = Factor("response_transition", [ DerivedLevel("repeat", Transition(response_repeat, [response])), DerivedLevel("switch", Transition(response_switch, [response])) ]) # DEFINE SEQUENCE CONSTRAINTS k = 7 constraints = [NoMoreThanKInARow(k, task_transition), NoMoreThanKInARow(k, response_transition)] # constraints = [] # DEFINE EXPERIMENT design = [color, word, task, cue, task_transition, cue_transition, congruency, response, response_transition] crossing = [color, word, task, cue, response, task_transition, cue_transition] block = fully_cross_block(design, crossing, constraints) # SOLVE from sweetpea.encoding_diagram import print_encoding_diagram print_encoding_diagram(block)
import operator as op from sweetpea.primitives import Factor, DerivedLevel, WithinTrial from sweetpea.constraints import NoMoreThanKInARow from sweetpea import fully_cross_block, synthesize_trials_non_uniform, print_experiments color_list = ["red", "green", "blue"] color = Factor("color", color_list) text = Factor("text", color_list) conLevel = DerivedLevel("con", WithinTrial(op.eq, [color, text])) incLevel = DerivedLevel("inc", WithinTrial(op.ne, [color, text])) conFactor = Factor("congruent?", [conLevel, incLevel]) design = [color, text, conFactor] crossing = [color, text] constraints = [NoMoreThanKInARow(1, ("congruent?", "con"))] block = fully_cross_block(design, crossing, constraints) # Synthesize 5 unique, but non-uniformly sampled, trials. experiments = synthesize_trials_non_uniform(block, 5) print_experiments(block, experiments)