Exemplo n.º 1
0
def test_incorporate_engine():
    engine = Engine(
        T[:,:2],
        cctypes=CCTYPES[:2],
        distargs=DISTARGS[:2],
        num_states=4,
        rng=gu.gen_rng(0),
    )
    engine.transition(N=5)

    # Incorporate a new dim into with a non-contiguous output.
    engine.incorporate_dim(
        T[:,2],
        outputs=[10],
        cctype=CCTYPES[2],
        distargs=DISTARGS[2]
    )
    engine.transition(N=2)

    # Serialize the engine, and run a targeted transtion on variable 10.
    m = engine.to_metadata()
    engine2 = Engine.from_metadata(m)
    engine2.transition(N=2, cols=[10], multiprocess=0)
    assert all(s.outputs == [0,1,10] for s in engine.states)
Exemplo n.º 2
0
def test_lovecat_transition_columns():
    """Test transition_lovecat targeting specific rows and columns."""
    D = generate_dataset_2()

    # Create engine and place each variable in its own view.
    engine = Engine(
        D,
        outputs=[
            3,
            2,
            1,
            0,
        ],
        cctypes=['normal'] * D.shape[1],
        Zv={
            3: 0,
            2: 1,
            1: 2,
            0: 3
        },
        multiprocess=1,
        num_states=4,
        rng=gu.gen_rng(2),
    )

    # Confirm all variables in singleton views.
    for s in engine.states:
        for a, b in itertools.combinations(s.outputs, 2):
            assert s.Zv(a) != s.Zv(b)

    # Store the row partition assignments of each variable.
    Zr_saved = [{
        var: sorted(s.view_for(var).Zr().items())
        for var in s.outputs
    } for s in engine.states]

    # Transition only the column hyperparameters of outputs 2 and 3 should not alter
    # the structure of either row or column partitions.
    engine.transition_lovecat(
        N=100,
        cols=[
            2,
            3,
        ],
        kernels=['column_hyperparameters'],
        progress=False,
    )

    for i, s in enumerate(engine.states):
        for a, b in itertools.combinations(s.outputs, 2):
            assert s.Zv(a) != s.Zv(b)

        assert all(
            sorted(s.view_for(var).Zr().items()) == Zr_saved[i][var]
            for var in s.outputs)

    # Transition variables 2 and 3 should place them in the same view, without
    # altering the view of variables 0 and 1.
    engine.transition_lovecat(N=100, cols=[
        2,
        3,
    ])

    for s in engine.states:
        assert s.Zv(3) == s.Zv(2)
        assert s.Zv(1) != s.Zv(0)

    # Transition only row assignments of outputs 0 and 1 should not alter the
    # view partition.
    engine.transition_lovecat(
        N=100,
        cols=[
            0,
            1,
        ],
        kernels=['row_partition_assignments'],
        checkpoint=2,
    )

    for s in engine.states:
        assert s.Zv(3) == s.Zv(2)
        assert s.Zv(1) != s.Zv(0)

    # Transition variables 0 and 1 should put them in the same view.
    engine.transition_lovecat(
        N=100,
        cols=[
            0,
            1,
        ],
    )

    for s in engine.states:
        assert s.Zv(3) == s.Zv(2)
        assert s.Zv(1) == s.Zv(0)

    # Add a new column 19 which is sum of 2 and 3, and place it in a singleton
    # view.
    T19 = D[:, 0] + D[:, 1]
    engine.incorporate_dim(T19, [19], cctype='normal', v=125)

    for s in engine.states:
        assert s.Zv(19) == 125

    # Transition all variables except 19 should not influence its view
    # assignment.
    engine.transition_lovecat(
        N=10,
        cols=[
            3,
            2,
            0,
            1,
        ],
        checkpoint=2,
    )

    for s in engine.states:
        assert all(s.Zv(19) != s.Zv(o) for o in s.outputs if o != 19)

    # Transition only 19 should place it in the same views as 3 and 2.
    engine.transition_lovecat(
        N=100,
        cols=[19],
    )

    for s in engine.states:
        assert s.Zv(19) == s.Zv(3) == s.Zv(2)