예제 #1
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