def test_step_cloner_should_set_steps_hyperparams_space():
    p = StepClonerForEachDataInput(SomeStep())
    p.fit_transform([[0, 0]])

    p.set_hyperparams_space(
        HyperparameterSpace({
            META_STEP_HP: RAND_INT_STEP_CLONER,
            SOME_STEP_HP: RAND_INT_SOME_STEP
        }))

    assert isinstance(p.steps[0].hyperparams_space, HyperparameterSpace)
    assert p.steps[0].hyperparams_space[SOME_STEP_HP_KEY] == RAND_INT_SOME_STEP
def test_step_cloner_should_set_steps_hyperparams():
    p = StepClonerForEachDataInput(SomeStep())
    p.fit_transform([[0, 0]])

    p.set_hyperparams(
        HyperparameterSamples({
            META_STEP_HP: META_STEP_HP_VALUE,
            SOME_STEP_HP: SOME_STEP_HP_VALUE
        }))

    assert isinstance(p.hyperparams, HyperparameterSamples)
    assert isinstance(p.steps[0].hyperparams, HyperparameterSamples)
    assert p.steps[0].get_hyperparams()[SOME_STEP_HP_KEY] == SOME_STEP_HP_VALUE
Exemplo n.º 3
0
def test_step_cloner_should_fit_transform():
    # Given
    tape = TapeCallbackFunction()
    p = StepClonerForEachDataInput(
        Pipeline([FitCallbackStep(tape), MultiplyByN(2)]))
    data_inputs = _create_data((2, 2))
    expected_outputs = _create_data((2, 2))

    # When
    p, processed_outputs = p.fit_transform(data_inputs, expected_outputs)

    # Then
    assert isinstance(p.steps[0], Pipeline)
    assert np.array_equal(p.steps[0][0].callback_function.data[0][0],
                          data_inputs[0])
    assert np.array_equal(p.steps[0][0].callback_function.data[0][1],
                          expected_outputs[0])

    assert isinstance(p.steps[1], Pipeline)
    assert np.array_equal(p.steps[1][0].callback_function.data[0][0],
                          data_inputs[1])
    assert np.array_equal(p.steps[1][0].callback_function.data[0][1],
                          expected_outputs[1])

    assert np.array_equal(processed_outputs, data_inputs * 2)
def test_should_inverse_transform():
    step_cloner = StepClonerForEachDataInput(SomeStepInverseTransform())

    step_cloner, processed_outputs = step_cloner.fit_transform([0])
    step_cloner = step_cloner.reverse()
    processed_outputs = step_cloner.inverse_transform(processed_outputs)

    assert processed_outputs == ['inverse_transform']
def test_should_fit_transform():
    some_step = SomeStepInverseTransform()
    step_cloner = StepClonerForEachDataInput(some_step)

    step_cloner, processed_outputs = step_cloner.fit_transform([0])

    assert isinstance(step_cloner.steps[0], SomeStepInverseTransform)
    assert processed_outputs == ['fit_transform']
def test_step_cloner_should_save_sub_steps(tmpdir):
    tape = TapeCallbackFunction()
    p = StepClonerForEachDataInput(Pipeline(
        [FitCallbackStep(tape), MultiplyByN(2)]),
                                   cache_folder_when_no_handle=tmpdir)
    data_inputs = _create_data((2, 2))
    expected_outputs = _create_data((2, 2))
    p, processed_outputs = p.fit_transform(data_inputs, expected_outputs)

    p.save(ExecutionContext(tmpdir), full_dump=True)

    saved_paths = [
        os.path.join(
            tmpdir,
            'StepClonerForEachDataInput/Pipeline[0]/FitCallbackStep/FitCallbackStep.joblib'
        ),
        os.path.join(
            tmpdir,
            'StepClonerForEachDataInput/Pipeline[0]/MultiplyByN/MultiplyByN.joblib'
        ),
        os.path.join(
            tmpdir,
            'StepClonerForEachDataInput/Pipeline[0]/MultiplyByN/MultiplyByN.joblib'
        ),
        os.path.join(
            tmpdir,
            'StepClonerForEachDataInput/Pipeline[0]/Pipeline[0].joblib'),
        os.path.join(
            tmpdir,
            'StepClonerForEachDataInput/Pipeline[1]/FitCallbackStep/FitCallbackStep.joblib'
        ),
        os.path.join(
            tmpdir,
            'StepClonerForEachDataInput/Pipeline[1]/MultiplyByN/MultiplyByN.joblib'
        ),
        os.path.join(
            tmpdir,
            'StepClonerForEachDataInput/Pipeline[1]/Pipeline[1].joblib'),
        os.path.join(
            tmpdir,
            'StepClonerForEachDataInput/Pipeline/FitCallbackStep/FitCallbackStep.joblib'
        ),
        os.path.join(
            tmpdir,
            'StepClonerForEachDataInput/Pipeline/MultiplyByN/MultiplyByN.joblib'
        ),
        os.path.join(tmpdir,
                     'StepClonerForEachDataInput/Pipeline/Pipeline.joblib'),
        os.path.join(
            tmpdir,
            'StepClonerForEachDataInput/StepClonerForEachDataInput.joblib')
    ]

    for p in saved_paths:
        assert os.path.exists(p)
def test_step_cloner_should_set_train():
    tape = TapeCallbackFunction()
    p = StepClonerForEachDataInput(
        Pipeline([FitCallbackStep(tape), MultiplyByN(2)]))
    data_inputs = _create_data((2, 2))
    expected_outputs = _create_data((2, 2))
    p, processed_outputs = p.fit_transform(data_inputs, expected_outputs)

    p.set_train(False)

    assert not p.is_train
    assert not p.steps_as_tuple[0][1].is_train
    assert not p.steps_as_tuple[1][1].is_train
Exemplo n.º 8
0
def test_step_cloner_should_inverse_transform():
    tape = TapeCallbackFunction()
    p = StepClonerForEachDataInput(
        Pipeline([FitCallbackStep(tape), MultiplyByN(2)]))
    data_inputs = _create_data((2, 2))
    expected_outputs = _create_data((2, 2))

    p, processed_outputs = p.fit_transform(data_inputs, expected_outputs)
    p = p.reverse()

    assert np.array_equal(processed_outputs, data_inputs * 2)
    inverse_processed_outputs = p.inverse_transform(processed_outputs)
    assert np.array_equal(np.array(inverse_processed_outputs),
                          np.array(data_inputs))
def test_step_cloner_should_load_sub_steps(tmpdir):
    tape = TapeCallbackFunction()
    p = StepClonerForEachDataInput(Pipeline(
        [FitCallbackStep(tape), MultiplyByN(2)]),
                                   cache_folder_when_no_handle=tmpdir)
    data_inputs = _create_data((2, 2))
    expected_outputs = _create_data((2, 2))
    p, processed_outputs = p.fit_transform(data_inputs, expected_outputs)

    p.save(ExecutionContext(tmpdir), full_dump=True)

    loaded_step_cloner = ExecutionContext(tmpdir).load(
        'StepClonerForEachDataInput')
    assert isinstance(loaded_step_cloner.wrapped, Pipeline)
    assert len(loaded_step_cloner.steps_as_tuple) == len(data_inputs)
    assert isinstance(loaded_step_cloner.steps_as_tuple[0][1], Pipeline)
    assert isinstance(loaded_step_cloner.steps_as_tuple[1][1], Pipeline)