Exemplo n.º 1
0
def test_bad_design_tree_spec():
    spec = {
        'main': [
            {
                'name': 'participant',
                'ivs': {
                    'a': [1, 2],
                    'b': [1, 2]
                },
                'number': 3,
                'ordering': 'Shuffle',
            },
            {
                'name': 'session',
                'ivs': {
                    'design': ['practice', 'test']
                },
                'design_matrix': [[0], [1], [1]],
            },
        ],
        'practice': [
            {
                'name': 'block'
            },
            {
                'name': 'trial',
                'ivs': {
                    'difficulty': [1, 2]
                },
                'n': 2,
                'order': 'Shuffle',
            },
        ],
        'test': [
            {
                'name': 'block',
                'n': 2,
            },
            [
                {
                    'name': 'trial',
                    'ivs': {
                        'difficulty': [1, 3]
                    },
                    'number': 2,
                    'order': 'Shuffle',
                },
                {
                    'name': 'not-trial',
                    'ivs': {
                        'difficulty': [5, 7]
                    },
                    'n': 3,
                    'order': 'Shuffle',
                },
            ],
        ],
    }
    with pytest.raises(ValueError):
        DesignTree.from_spec(spec)
Exemplo n.º 2
0
def test_bad_heterogeneity():
    main_structure = [
        ('participant',
         Design(ivs={
             'a': [1, 2],
             'b': [1, 2]
         }, ordering=Shuffle(3))),
        ('session',
         Design(ivs={'design': ['practice', 'test']},
                design_matrix=[[0], [1], [1]])),
    ]
    other_structures = {
        'practice': [
            ('block', Design()),
            ('trial', Design(ivs={'difficulty': [1, 2]},
                             ordering=Shuffle(20))),
        ],
        'test': [
            ('block', Design(ordering=CompleteCounterbalance(2))),
            ('trial',
             Design(ivs={'difficulty': [1, 3, 5, 7]}, ordering=Shuffle(5))),
        ],
    }
    with pytest.raises(ValueError):
        DesignTree.new(main_structure, **other_structures)
Exemplo n.º 3
0
def test_bad_design_tree_spec():
    spec = {
        'main':
        [
            {
                'name': 'participant',
                'ivs': {'a': [1, 2], 'b': [1, 2]},
                'number': 3,
                'ordering': 'Shuffle',
            },
            {
                'name': 'session',
                'ivs': {'design': ['practice', 'test']},
                'design_matrix': [[0], [1], [1]],
            },
        ],
        'practice':
        [
            {
                'name': 'block'
            },
            {
                'name': 'trial',
                'ivs': {'difficulty': [1, 2]},
                'n': 2,
                'order': 'Shuffle',
            },
        ],
        'test':
        [
            {
                'name': 'block',
                'n': 2,
            },
            [
                {
                    'name': 'trial',
                    'ivs': {'difficulty': [1, 3]},
                    'number': 2,
                    'order': 'Shuffle',
                },
                {
                    'name': 'not-trial',
                    'ivs': {'difficulty': [5, 7]},
                    'n': 3,
                    'order': 'Shuffle',
                },
            ],
        ],
    }
    with pytest.raises(ValueError):
        DesignTree.from_spec(spec)
Exemplo n.º 4
0
def test_bad_heterogeneity():
    main_structure = [
        ("participant", Design(ivs={"a": [1, 2], "b": [1, 2]}, ordering=Shuffle(3))),
        ("session", Design(ivs={"design": ["practice", "test"]}, design_matrix=[[0], [1], [1]])),
    ]
    other_structures = {
        "practice": [("block", Design()), ("trial", Design(ivs={"difficulty": [1, 2]}, ordering=Shuffle(20)))],
        "test": [
            ("block", Design(ordering=CompleteCounterbalance(2))),
            ("trial", Design(ivs={"difficulty": [1, 3, 5, 7]}, ordering=Shuffle(5))),
        ],
    }
    with pytest.raises(ValueError):
        DesignTree.new(main_structure, **other_structures)
Exemplo n.º 5
0
def test_design_tree_from_spec():
    spec = {
        "main": [
            {"name": "participant", "ivs": {"a": [1, 2], "b": [1, 2]}, "number": 3, "ordering": "Shuffle"},
            {"name": "session", "ivs": {"design": ["practice", "test"]}, "design_matrix": [[0], [1], [1]]},
        ],
        "practice": [{"name": "block"}, {"name": "trial", "ivs": {"difficulty": [1, 2]}, "n": 2, "order": "Shuffle"}],
        "test": [
            {"name": "block", "n": 2},
            [
                {"name": "trial", "ivs": {"difficulty": [1, 3]}, "number": 2, "order": "Shuffle"},
                {"ivs": {"difficulty": [5, 7]}, "n": 3, "order": "Shuffle"},
            ],
        ],
    }
    tree = DesignTree.from_spec(spec)
    yield check_length, tree, 4
    participant = next(tree)
    yield check_length, participant, 3
    sessions = next(participant)
    yield check_type, sessions, dict
    yield check_length, sessions, 2
    yield check_length, sessions["practice"], 2
    yield check_length, sessions["test"], 2
    yield check_equality, sessions["practice"].levels_and_designs[0][0], "block"
    yield check_equality, sessions["test"].levels_and_designs[0][0], "block"
    practice_block = next(sessions["practice"])
    test_block = next(sessions["test"])
    yield check_equality, practice_block.levels_and_designs[0][0], "trial"
    yield check_equality, test_block.levels_and_designs[0][0], "trial"
Exemplo n.º 6
0
def make_heterogeneous_tree():
    main_structure = [
        ('participant',
         Design(ivs={
             'a': [1, 2],
             'b': [1, 2]
         }, ordering=Shuffle(3))),
        ('session',
         Design(ivs={'design': ['practice', 'test']},
                design_matrix=[[0], [1], [1]])),
    ]
    other_structures = {
        'practice': [
            ('block', Design()),
            ('trial', Design(ivs={'difficulty': [1, 2]},
                             ordering=Shuffle(20))),
        ],
        'test': [
            ('block', Design(ordering=Ordering(2))),
            ('trial',
             Design(ivs={'difficulty': [1, 3, 5, 7]}, ordering=Shuffle(5))),
        ],
    }

    return DesignTree.new(main_structure, **other_structures)
Exemplo n.º 7
0
def test_bad_design_tree_spec():
    spec = {
        "main": [
            {"name": "participant", "ivs": {"a": [1, 2], "b": [1, 2]}, "number": 3, "ordering": "Shuffle"},
            {"name": "session", "ivs": {"design": ["practice", "test"]}, "design_matrix": [[0], [1], [1]]},
        ],
        "practice": [{"name": "block"}, {"name": "trial", "ivs": {"difficulty": [1, 2]}, "n": 2, "order": "Shuffle"}],
        "test": [
            {"name": "block", "n": 2},
            [
                {"name": "trial", "ivs": {"difficulty": [1, 3]}, "number": 2, "order": "Shuffle"},
                {"name": "not-trial", "ivs": {"difficulty": [5, 7]}, "n": 3, "order": "Shuffle"},
            ],
        ],
    }
    with pytest.raises(ValueError):
        DesignTree.from_spec(spec)
Exemplo n.º 8
0
def make_manual_exp():
    tree = DesignTree.new([('participant', [Design(ordering=Shuffle(2))]),
                           ('block', [Design(ivs={'b': [0, 1, 2]}, ordering=CompleteCounterbalance())]),
                           ('trial', [Design({'a': [False, True]}, ordering=Shuffle(4))]),
                           ])
    exp = Experiment.new(tree)
    exp.add_callback('trial', trial)
    return exp
Exemplo n.º 9
0
def test_simple_design_tree_spec():
    spec = [
        {"name": "participant", "ivs": {"a": [1, 2], "b": [1, 2]}, "number": 3, "ordering": "Shuffle"},
        {"ivs": {"type": ["over", "under"]}, "design_matrix": [[0], [1], [1]]},
    ]
    tree = DesignTree.from_spec(spec)
    yield check_length, tree, 2
    yield check_equality, next(tree), next(tree)
    participant = next(tree)
    yield check_length, participant, 1
    yield check_equality, participant.levels_and_designs[0][0], None
Exemplo n.º 10
0
def make_heterogeneous_tree():
    main_structure = [
        ("participant", Design(ivs={"a": [1, 2], "b": [1, 2]}, ordering=Shuffle(3))),
        ("session", Design(ivs={"design": ["practice", "test"]}, design_matrix=[[0], [1], [1]])),
    ]
    other_structures = {
        "practice": [("block", Design()), ("trial", Design(ivs={"difficulty": [1, 2]}, ordering=Shuffle(20)))],
        "test": [
            ("block", Design(ordering=Ordering(2))),
            ("trial", Design(ivs={"difficulty": [1, 3, 5, 7]}, ordering=Shuffle(5))),
        ],
    }

    return DesignTree.new(main_structure, **other_structures)
Exemplo n.º 11
0
def test_simple_design_tree_spec():
    spec = [
        {
            'name': 'participant',
            'ivs': {'a': [1, 2], 'b': [1, 2]},
            'number': 3,
            'ordering': 'Shuffle',
        },
        {
            'ivs': {'type': ['over', 'under']},
            'design_matrix': [[0], [1], [1]],
        },
    ]
    tree = DesignTree.from_spec(spec)
    yield check_length, tree, 2
    yield check_equality, next(tree), next(tree)
    participant = next(tree)
    yield check_length, participant, 1
    yield check_equality, participant.levels_and_designs[0][0], None
Exemplo n.º 12
0
def test_simple_design_tree_spec():
    spec = [
        {
            'name': 'participant',
            'ivs': {
                'a': [1, 2],
                'b': [1, 2]
            },
            'number': 3,
            'ordering': 'Shuffle',
        },
        {
            'ivs': {
                'type': ['over', 'under']
            },
            'design_matrix': [[0], [1], [1]],
        },
    ]
    tree = DesignTree.from_spec(spec)
    yield check_length, tree, 2
    yield check_equality, next(tree), next(tree)
    participant = next(tree)
    yield check_length, participant, 1
    yield check_equality, participant.levels_and_designs[0][0], None
Exemplo n.º 13
0
def test_design_tree_from_spec():
    spec = {
        'main': [
            {
                'name': 'participant',
                'ivs': {
                    'a': [1, 2],
                    'b': [1, 2]
                },
                'number': 3,
                'ordering': 'Shuffle',
            },
            {
                'name': 'session',
                'ivs': {
                    'design': ['practice', 'test']
                },
                'design_matrix': [[0], [1], [1]],
            },
        ],
        'practice': [
            {
                'name': 'block'
            },
            {
                'name': 'trial',
                'ivs': {
                    'difficulty': [1, 2]
                },
                'n': 2,
                'order': 'Shuffle',
            },
        ],
        'test': [
            {
                'name': 'block',
                'n': 2,
            },
            [
                {
                    'name': 'trial',
                    'ivs': {
                        'difficulty': [1, 3]
                    },
                    'number': 2,
                    'order': 'Shuffle',
                },
                {
                    'ivs': {
                        'difficulty': [5, 7]
                    },
                    'n': 3,
                    'order': 'Shuffle',
                },
            ],
        ],
    }
    tree = DesignTree.from_spec(spec)
    yield check_length, tree, 4
    participant = next(tree)
    yield check_length, participant, 3
    sessions = next(participant)
    yield check_type, sessions, dict
    yield check_length, sessions, 2
    yield check_length, sessions['practice'], 2
    yield check_length, sessions['test'], 2
    yield check_equality, sessions['practice'].levels_and_designs[0][
        0], 'block'
    yield check_equality, sessions['test'].levels_and_designs[0][0], 'block'
    practice_block = next(sessions['practice'])
    test_block = next(sessions['test'])
    yield check_equality, practice_block.levels_and_designs[0][0], 'trial'
    yield check_equality, test_block.levels_and_designs[0][0], 'trial'
Exemplo n.º 14
0
def test_design_tree_from_spec():
    spec = {
        'main':
        [
            {
                'name': 'participant',
                'ivs': {'a': [1, 2], 'b': [1, 2]},
                'number': 3,
                'ordering': 'Shuffle',
            },
            {
                'name': 'session',
                'ivs': {'design': ['practice', 'test']},
                'design_matrix': [[0], [1], [1]],
            },
        ],
        'practice':
        [
            {
                'name': 'block'
            },
            {
                'name': 'trial',
                'ivs': {'difficulty': [1, 2]},
                'n': 2,
                'order': 'Shuffle',
            },
        ],
        'test':
        [
            {
                'name': 'block',
                'n': 2,
            },
            [
                {
                    'name': 'trial',
                    'ivs': {'difficulty': [1, 3]},
                    'number': 2,
                    'order': 'Shuffle',
                },
                {
                    'ivs': {'difficulty': [5, 7]},
                    'n': 3,
                    'order': 'Shuffle',
                },
            ],
        ],
    }
    tree = DesignTree.from_spec(spec)
    yield check_length, tree, 4
    participant = next(tree)
    yield check_length, participant, 3
    sessions = next(participant)
    yield check_type, sessions, dict
    yield check_length, sessions, 2
    yield check_length, sessions['practice'], 2
    yield check_length, sessions['test'], 2
    yield check_equality, sessions['practice'].levels_and_designs[0][0], 'block'
    yield check_equality, sessions['test'].levels_and_designs[0][0], 'block'
    practice_block = next(sessions['practice'])
    test_block = next(sessions['test'])
    yield check_equality, practice_block.levels_and_designs[0][0], 'trial'
    yield check_equality, test_block.levels_and_designs[0][0], 'trial'
Exemplo n.º 15
0
def test_design_tree():
    trial_matrix = np.array([[-1, -1],
                             [1, -1],
                             [-1, 1],
                             [1, 1],
                             [0, 0],
                             [0, 0],
                             [0, 0],
                             [0, 0],
                             [-1.41421356, 0.1],
                             [1.41421356,  0.1],
                             [0, -1.41421356],
                             [0, 1.41421356],
                             [0, 0],
                             [0, 0],
                             [0, 0],
                             [0, 0]])  # pyDOE.ccdesign(2) with two 0.1 elements to prevent symmetry in the IVs.
    trial_iv_names = ['a', 'b']
    trial_iv_values = [None, None]
    block_ivs = {'block': [1, 2]}
    participant_iv_names = ('A', 'B')
    participant_iv_values = [[1, 2], [1, 2, 3]]

    trial_design = Design(ivs=zip(trial_iv_names, trial_iv_values), ordering=Shuffle(3), design_matrix=trial_matrix)
    block_design = Design(block_ivs, ordering=CompleteCounterbalance())
    practice_block_design = Design()
    participant_design = Design(dict(zip(participant_iv_names, participant_iv_values)), ordering=Ordering(10))

    levels_and_designs = OrderedDict([('participant', participant_design),
                                      ('block', [practice_block_design, block_design]),
                                      ('trial', trial_design)])

    tree = DesignTree(levels_and_designs)
    tree.add_base_level()

    levels, designs = zip(*tree.levels_and_designs)

    yield check_identity, designs[1][0], participant_design
    yield check_identity, designs[2][0], practice_block_design
    yield check_identity, designs[2][1], block_design
    yield check_identity, designs[3][0], trial_design

    yield check_equality, len(tree), 4
    yield check_equality, tree[3], ('trial', [trial_design])

    yield check_equality, next(tree).levels_and_designs, next(tree).levels_and_designs, tree.levels_and_designs[1:]
    tree_with_participant_base = next(tree)
    tree_with_block_base = next(tree_with_participant_base)
    yield (check_equality,
           tree_with_block_base.levels_and_designs,
           next(next(tree)).levels_and_designs,
           [('block', [practice_block_design, block_design]), ('trial', [trial_design])])
    tree_with_trial_base = next(tree_with_block_base)
    yield (check_equality,
           tree_with_trial_base.levels_and_designs,
           [('trial', [trial_design])],
           next(tree_with_block_base).levels_and_designs)
    with pytest.raises(StopIteration):
        next(tree_with_trial_base)

    for design, iv_names, iv_values, n, data, matrix in zip(
            [designs[1][0], designs[2][0], designs[2][1], designs[3][0]],
            [['A', 'B', CompleteCounterbalance.iv_name], [], ['block'], ['a', 'b']],
            [[[1, 2], [1, 2, 3], [0, 1]], [], [[1, 2]], [None, None]],
            [10*2*2*3, 1, 2, 3*len(trial_matrix)],
            [{}, {}, {CompleteCounterbalance.iv_name: 0}, {}],
            [None, None, None, trial_matrix]):
        yield check_design, design, iv_names, iv_values, n, data, matrix

    yield check_design_matrix, designs[3][0].get_order(), ['a', 'b'], [None, None], trial_matrix
Exemplo n.º 16
0
def test_bizarre_equality():
    design = Design()
    assert (design == 1) is False
    tree = DesignTree.new([("a", design)])
    assert (tree == 1) is False
Exemplo n.º 17
0
def test_bizarre_equality():
    design = Design()
    assert (design == 1) is False
    tree = DesignTree.new([('a', design)])
    assert (tree == 1) is False
Exemplo n.º 18
0
def make_tree(levels, data):
    designs = [[Design([('a', range(len(levels))), ('b', [False, True])], extra_data=data, ordering=Ordering())]
               for _ in levels]
    return DesignTree.new(list(zip(levels, designs)))
Exemplo n.º 19
0
def test_design_tree():
    trial_matrix = np.array(
        [[-1, -1], [1, -1], [-1, 1], [1, 1], [0, 0], [0, 0], [0, 0], [0, 0],
         [-1.41421356, 0.1],
         [1.41421356, 0.1], [0, -1.41421356], [0,
                                               1.41421356],
         [0, 0], [0, 0], [0,
                          0], [0, 0]]
    )  # pyDOE.ccdesign(2) with two 0.1 elements to prevent symmetry in the IVs.
    trial_iv_names = ['a', 'b']
    trial_iv_values = [None, None]
    block_ivs = {'block': [1, 2]}
    participant_iv_names = ('A', 'B')
    participant_iv_values = [[1, 2], [1, 2, 3]]

    trial_design = Design(ivs=zip(trial_iv_names, trial_iv_values),
                          ordering=Shuffle(3),
                          design_matrix=trial_matrix)
    block_design = Design(block_ivs, ordering=CompleteCounterbalance())
    practice_block_design = Design()
    participant_design = Design(dict(
        zip(participant_iv_names, participant_iv_values)),
                                ordering=Ordering(10))

    levels_and_designs = OrderedDict([('participant', participant_design),
                                      ('block',
                                       [practice_block_design, block_design]),
                                      ('trial', trial_design)])

    tree = DesignTree.new(levels_and_designs)
    tree.add_base_level()

    levels, designs = zip(*tree.levels_and_designs)

    yield check_identity, designs[1][0], participant_design
    yield check_identity, designs[2][0], practice_block_design
    yield check_identity, designs[2][1], block_design
    yield check_identity, designs[3][0], trial_design

    yield check_equality, len(tree), 4
    yield check_equality, tree[3], ('trial', [trial_design])

    yield check_equality, next(tree).levels_and_designs, next(
        tree).levels_and_designs, tree.levels_and_designs[1:]
    tree_with_participant_base = next(tree)
    tree_with_block_base = next(tree_with_participant_base)
    yield (check_equality, tree_with_block_base.levels_and_designs,
           next(next(tree)).levels_and_designs,
           [('block', [practice_block_design, block_design]),
            ('trial', [trial_design])])
    tree_with_trial_base = next(tree_with_block_base)
    yield (check_equality, tree_with_trial_base.levels_and_designs, [
        ('trial', [trial_design])
    ], next(tree_with_block_base).levels_and_designs)
    with pytest.raises(StopIteration):
        next(tree_with_trial_base)

    for design, iv_names, iv_values, n, data, matrix in zip(
        [designs[1][0], designs[2][0], designs[2][1], designs[3][0]],
        [['A', 'B', CompleteCounterbalance.iv_name], [], ['block'], ['a', 'b']
         ], [[[1, 2], [1, 2, 3], [0, 1]], [], [[1, 2]], [None, None]],
        [10 * 2 * 2 * 3, 1, 2, 3 * len(trial_matrix)],
        [{}, {}, {
            CompleteCounterbalance.iv_name: 0
        }, {}], [None, None, None, trial_matrix]):
        yield check_design, design, iv_names, iv_values, n, data, matrix

    yield check_design_matrix, designs[3][0].get_order(), ['a', 'b'
                                                           ], [None, None
                                                               ], trial_matrix