def test_consistency_with_transition_first_and_uneven_level_lengths(): color3 = Factor("color3", ["red", "blue", "green"]) yes_fn = lambda colors: colors[0] == colors[1] == colors[2] no_fn = lambda colors: not yes_fn(colors) color3_repeats_factor = Factor("color3 repeats?", [ DerivedLevel("yes", Window(yes_fn, [color3], 3, 1)), DerivedLevel("no", Window(no_fn, [color3], 3, 1)) ]) block = fully_cross_block([color3_repeats_factor, color3, text], [color3, text], []) backend_request = BackendRequest(0) Consistency.apply(block, backend_request) assert backend_request.ll_requests == [ LowLevelRequest("EQ", 1, [1, 2, 3]), LowLevelRequest("EQ", 1, [4, 5]), LowLevelRequest("EQ", 1, [6, 7, 8]), LowLevelRequest("EQ", 1, [9, 10]), LowLevelRequest("EQ", 1, [11, 12, 13]), LowLevelRequest("EQ", 1, [14, 15]), LowLevelRequest("EQ", 1, [16, 17, 18]), LowLevelRequest("EQ", 1, [19, 20]), LowLevelRequest("EQ", 1, [21, 22, 23]), LowLevelRequest("EQ", 1, [24, 25]), LowLevelRequest("EQ", 1, [26, 27, 28]), LowLevelRequest("EQ", 1, [29, 30]), LowLevelRequest("EQ", 1, [31, 32]), LowLevelRequest("EQ", 1, [33, 34]), LowLevelRequest("EQ", 1, [35, 36]), LowLevelRequest("EQ", 1, [37, 38]) ]
def test_generate_encoding_diagram_with_window_with_stride(): congruent_bookend = Factor("congruent bookend?", [ DerivedLevel("yes", Window(lambda colors, texts: colors[0] == texts[0], [color, text], 1, 3)), DerivedLevel("no", Window(lambda colors, texts: colors[0] != texts[0], [color, text], 1, 3)) ]) block = fully_cross_block([color, text, congruent_bookend], [color, text], []) assert __generate_encoding_diagram(block) == "\ ------------------------------------------------------\n\ | Trial | color | text | congruent bookend? |\n\ | # | red blue | red blue | yes no |\n\ ------------------------------------------------------\n\ | 1 | 1 2 | 3 4 | 17 18 |\n\ | 2 | 5 6 | 7 8 | |\n\ | 3 | 9 10 | 11 12 | |\n\ | 4 | 13 14 | 15 16 | 19 20 |\n\ ------------------------------------------------------\n" congruent_bookend = Factor("congruent bookend?", [ DerivedLevel("yes", Window(lambda colors, texts: colors[0] == texts[0], [color, text], 2, 2)), DerivedLevel("no", Window(lambda colors, texts: colors[0] != texts[0], [color, text], 2, 2)) ]) block = fully_cross_block([color, text, congruent_bookend], [color, text], []) assert __generate_encoding_diagram(block) == "\
def test_shift_window(): assert DerivationProcessor.shift_window([[0, 0], [1, 1]], WithinTrial(lambda x: x, None), 0) == [[0, 0], [1, 1]] assert DerivationProcessor.shift_window([[0, 0], [1, 1]], Transition(lambda x: x, None), 4) == [[0, 4], [1, 5]] assert DerivationProcessor.shift_window([[0, 2, 4], [1, 3, 5]], Window(lambda x: x, [1, 2], 2, 3), 6) == [[0, 8, 16], [1, 9, 17]] assert DerivationProcessor.shift_window([[1, 1, 1, 1], [2, 2, 2, 2]], Window(lambda x: x, [1, 2], 2, 4), 10) == \ [[1, 11, 21, 31], [2, 12, 22, 32]]
def test_generate_encoding_diagram_with_windows(): color3 = Factor("color3", ["red", "blue", "green"]) yes_fn = lambda colors: colors[0] == colors[1] == colors[2] no_fn = lambda colors: not yes_fn(colors) color3_repeats_factor = Factor("color3 repeats?", [ DerivedLevel("yes", Window(yes_fn, [color3], 3, 1)), DerivedLevel("no", Window(no_fn, [color3], 3, 1)) ]) block = fully_cross_block([color3_repeats_factor, color3, text], [color3, text], []) assert __generate_encoding_diagram(block) == "\
def test_factor_applies_to_trial(): assert color.applies_to_trial(1) == True assert color.applies_to_trial(2) == True assert color.applies_to_trial(3) == True assert color.applies_to_trial(4) == True with pytest.raises(ValueError): color.applies_to_trial(0) assert color_repeats_factor.applies_to_trial(1) == False assert color_repeats_factor.applies_to_trial(2) == True assert color_repeats_factor.applies_to_trial(3) == True assert color_repeats_factor.applies_to_trial(4) == True f = Factor('f', [DerivedLevel('l', Window(op.eq, [color], 2, 2))]) assert f.applies_to_trial(1) == False assert f.applies_to_trial(2) == True assert f.applies_to_trial(3) == False assert f.applies_to_trial(4) == True assert color3.applies_to_trial(1) == True
from sweetpea.constraints import at_most_k_in_a_row, exclude from sweetpea.encoding_diagram import print_encoding_diagram from sweetpea.primitives import Factor, DerivedLevel, Window from sweetpea.tests.test_utils import get_level_from_name from sweetpea.server import build_cnf from acceptance import path_to_cnf_files # Basic setup color_list = ["red", "blue"] color = Factor("color", color_list) text = Factor("text", color_list) # congruent 'bookend' Factor. (color and text in first and last trial are congruent) congruent_bookend = Factor("congruent bookend?", [ DerivedLevel( "yes", Window(lambda color, text: color == text, [color, text], 1, 3)), DerivedLevel( "no", Window(lambda color, text: color != text, [color, text], 1, 3)) ]) @pytest.mark.parametrize('design', permutations([color, text, congruent_bookend])) def test_correct_solution_count_when_unconstrained(design): crossing = [color, text] constraints = [] block = fully_cross_block(design, crossing, constraints) experiments = synthesize_trials_non_uniform(block, 100) assert len(experiments) == 24
color = Factor("color", ["red", "blue"]) text = Factor("text", ["red", "blue"]) red_color = get_level_from_name(color, "red") blue_color = get_level_from_name(color, "blue") red_text = get_level_from_name(text, "red") blue_text = get_level_from_name(text, "blue") con_factor_within_trial = Factor("congruent?", [ DerivedLevel("con", WithinTrial(op.eq, [color, text])), DerivedLevel("inc", WithinTrial(op.ne, [color, text])) ]) con_factor_window = Factor("congruent?", [ DerivedLevel("con", Window(op.eq, [color, text], 1, 1)), DerivedLevel("inc", Window(op.ne, [color, text], 1, 1)) ]) color_repeats_factor = Factor("repeated color?", [ DerivedLevel("yes", Transition(lambda colors: colors[0] == colors[1], [color])), DerivedLevel("no", Transition(lambda colors: colors[0] != colors[1], [color])) ]) def test_validate_accepts_basic_factors(): block = fully_cross_block([color, text], [color, text], []) UniformCombinatoricSamplingStrategy._UniformCombinatoricSamplingStrategy__validate( block)
con_level = DerivedLevel("con", WithinTrial(op.eq, [color, text])) inc_level = DerivedLevel("inc", WithinTrial(op.ne, [color, text])) con_factor = Factor("congruent?", [con_level, inc_level]) color_repeats_factor = Factor("color repeats?", [ DerivedLevel("yes", Transition(lambda colors: colors[0] == colors[1], [color])), DerivedLevel("no", Transition(lambda colors: colors[0] != colors[1], [color])) ]) text_repeats_factor = Factor("text repeats?", [ DerivedLevel("yes", Transition(lambda colors: colors[0] == colors[1], [text])), DerivedLevel("no", Transition(lambda colors: colors[0] != colors[1], [text])) ]) congruent_bookend = Factor("congruent bookend?", [ DerivedLevel("yes", Window(lambda color, text: color == text, [color, text], 1, 3)), DerivedLevel("no", Window(lambda color, text: color != text, [color, text], 1, 3)) ]) design = [color, text, con_factor] crossing = [color, text] blk = fully_cross_block(design, crossing, []) def test_decode(): solution = [-1, 2, -3, 4, 5, -6, -7, 8, 9, -10, -11, 12, 13, -14, -15, 16, -17, 18, 19, -20, 21, -22, 23, -24] assert __decode(blk, solution) == { 'color': ['blue', 'blue', 'red', 'red'],
con_level = DerivedLevel("con", WithinTrial(op.eq, [color, text])) inc_level = DerivedLevel("inc", WithinTrial(op.ne, [color, text])) con_factor = Factor("congruent?", [con_level, inc_level]) color_repeats_level = DerivedLevel("yes", Transition(op.eq, [color])) color_no_repeat_level = DerivedLevel("no", Transition(op.ne, [color])) color_repeats_factor = Factor("color repeats?", [color_repeats_level, color_no_repeat_level]) color3 = Factor("color3", ["red", "blue", "green"]) yes_fn = lambda colors: colors[0] == colors[1] == colors[2] no_fn = lambda colors: not yes_fn(colors) color3_repeats_factor = Factor("color3 repeats?", [ DerivedLevel("yes", Window(yes_fn, [color3], 3, 1)), DerivedLevel("no", Window(no_fn, [color3], 3, 1)) ]) def test_factor_validation(): Factor("factor name", ["level 1", "level 2"]) # Non-string name with pytest.raises(ValueError): Factor(56, ["level "]) # Non-list levels with pytest.raises(ValueError): Factor("name", 42)
def A_B_A(tasks): return (tasks[0] == tasks[2]) and (tasks[0] != tasks[1]) def A_B_C(tasks): return (tasks[0] != tasks[2]) and (tasks[0] != tasks[1]) and (tasks[1] != tasks[2]) def other_transition(tasks): return (not (A_B_A(tasks) or A_B_C(tasks))) task_transition = Factor("task transition", [ DerivedLevel("A-B-A", Window(A_B_A, [task], 3, 1)), DerivedLevel("A-B-C", Window(A_B_C, [task], 3, 1)), ]) # DEFINE CONSTRAINTS constraints = [Exclude("object configuration", "illegal")] # DEFINE EXPERIMENT design = [ deviantColorObject, deviantOrientationObject, deviantMovementObject, objectConfiguration, task, task_transition ] crossing = [ deviantColorObject, deviantOrientationObject, deviantMovementObject, task, task_transition
color_repeats_factor = Factor("repeated color?", [ DerivedLevel("yes", Transition(lambda colors: colors[0] == colors[1], [color])), DerivedLevel("no", Transition(lambda colors: colors[0] != colors[1], [color])) ]) text_repeats_factor = Factor("repeated text?", [ DerivedLevel("yes", Transition(lambda texts: texts[0] == texts[1], [text])), DerivedLevel("no", Transition(lambda texts: texts[0] != texts[1], [text])) ]) congruent_bookend = Factor("congruent bookend?", [ DerivedLevel( "yes", Window(lambda color, text: color == text, [color, text], 1, 3)), DerivedLevel( "no", Window(lambda color, text: color != text, [color, text], 1, 3)) ]) color3 = Factor("color3", ["red", "blue", "green"]) yes_fn = lambda colors: colors[0] == colors[1] == colors[2] no_fn = lambda colors: not yes_fn(colors) color3_repeats_factor = Factor("color3 repeats?", [ DerivedLevel("yes", Window(yes_fn, [color3], 3, 1)), DerivedLevel("no", Window(no_fn, [color3], 3, 1)) ]) def test_fully_cross_block_validate():