def test_feature_assembler_invalid_dtype_input_fail():
    df = util.iris(['sepallength', 'sepalwidth', 'petalwidth', 'petallength'],
                   size=10)
    test_out = {
        'Feat': [[3.1, 0.2], [3.5, 0.3], [3.3, 0.4], [3.1, 0.2], [3.6, 0.2],
                 [3.9, 0.4], [3.4, 0.3], [3.4, 0.2], [2.9, 0.2], [3.1, 0.1]]
    }
    test_out_2 = {
        'Feat2':
        ['3.1', '0.3', '0.4', '0.2', '3.6', '3.9', '0.3', '0.2', '2.9', '0.1']
    }
    test_out = pd.DataFrame(test_out)
    test_out_2 = pd.DataFrame(test_out_2)
    df = pd.concat([df, test_out, test_out_2], axis=1, join='inner')
    arguments = {
        'parameters': {
            'attributes': ['sepalwidth', 'Feat', 'Feat2'],
            'multiplicity': {
                'input data': 0
            }
        },
        'named_inputs': {
            'input data': 'df',
        },
        'named_outputs': {
            'output data': 'out'
        }
    }
    instance = FeatureAssemblerOperation(**arguments)
    with pytest.raises(ValueError) as val_err:
        util.execute(instance.generate_code(), {'df': df})
    assert "Input 'df' must contain numeric values only for task" in str(
        val_err.value)
def test_feature_assembler_alias_param_success():
    df = util.iris(['sepallength', 'sepalwidth', 'petalwidth', 'petallength'],
                   size=10)
    test_df = df.copy()

    arguments = {
        'parameters': {
            'attributes': ['sepalwidth', 'petalwidth'],
            'multiplicity': {
                'input data': 0
            },
            'alias': 'Feat'
        },
        'named_inputs': {
            'input data': 'df',
        },
        'named_outputs': {
            'output data': 'out'
        }
    }
    instance = FeatureAssemblerOperation(**arguments)
    result = util.execute(instance.generate_code(), {'df': df})

    test_out = {
        'Feat': [[3.5, 0.2], [3.0, 0.2], [3.2, 0.2], [3.1, 0.2], [3.6, 0.2],
                 [3.9, 0.4], [3.4, 0.3], [3.4, 0.2], [2.9, 0.2], [3.1, 0.1]]
    }
    test_out = pd.DataFrame(test_out)
    test_out = pd.concat([test_df, test_out], axis=1)
    assert result['out'].equals(test_out)
Beispiel #3
0
def test_feature_assembler_operation_success():
    params = {
        FeatureAssemblerOperation.ATTRIBUTES_PARAM: ['col'],
    }

    n_in = {'input data': 'input_1'}
    n_out = {'output data': 'output_1'}
    in1 = n_in['input data']
    out = n_out['output data']

    instance = FeatureAssemblerOperation(params,
                                         named_inputs=n_in,
                                         named_outputs=n_out)

    code = instance.generate_code()

    expected_code = dedent("""
        cols = {cols}
        {output} = {input}
        {output}['FeatureField'] = {input}[cols].values.tolist()
        """.format(cols=params[FeatureAssemblerOperation.ATTRIBUTES_PARAM],
                   output=out,
                   input=in1))

    result, msg = compare_ast(ast.parse(code), ast.parse(expected_code))

    assert result, msg + format_code_comparison(code, expected_code)
def test_feature_assembler_missing_input_implies_no_code_success():
    arguments = {
        'parameters': {
            'attributes': ['sepalwidth', 'petalwidth'],
            'multiplicity': {
                'input data': 0
            }
        },
        'named_inputs': {},
        'named_outputs': {
            'output data': 'out'
        }
    }
    instance = FeatureAssemblerOperation(**arguments)
    assert instance.generate_code() is None
Beispiel #5
0
def test_feature_assembler_operation_failure():
    params = {FeatureAssemblerOperation.ALIAS_PARAM: 'c'}
    with pytest.raises(ValueError):
        n_in = {'input data': 'input_1'}
        n_out = {'output data': 'output_1'}
        FeatureAssemblerOperation(params,
                                  named_inputs=n_in,
                                  named_outputs=n_out)
def test_feature_assembler_missing_multiplicity_param_fail():
    df = util.iris(['sepallength', 'sepalwidth', 'petalwidth', 'petallength'],
                   size=10)
    arguments = {
        'parameters': {
            'attributes': ['sepalwidth', 'petalwidth']
        },
        'named_inputs': {
            'input data': 'df',
        },
        'named_outputs': {
            'output data': 'out'
        }
    }
    instance = FeatureAssemblerOperation(**arguments)
    with pytest.raises(KeyError) as key_err:
        util.execute(instance.generate_code(), {'df': df})
    assert "'multiplicity'" in str(key_err.value)
def test_feature_assembler_invalid_multiplicity_param_fail():
    df = util.iris(['sepallength', 'sepalwidth', 'petalwidth', 'petallength'],
                   size=10)
    arguments = {
        'parameters': {
            'attributes': ['sepalwidth', 'petalwidth'],
            'multiplicity': 'invalid'
        },
        'named_inputs': {
            'input data': 'df',
        },
        'named_outputs': {
            'output data': 'out'
        }
    }
    instance = FeatureAssemblerOperation(**arguments)
    with pytest.raises(TypeError) as typ_err:
        util.execute(instance.generate_code(), {'df': df})
    assert "string indices must be integers" in str(typ_err)
def test_feature_assembler_invalid_attributes_param_fail():
    df = util.iris(['sepallength', 'sepalwidth', 'petalwidth', 'petallength'],
                   size=10)
    arguments = {
        'parameters': {
            'attributes': 'invalid',
            'multiplicity': {
                'input data': 0
            }
        },
        'named_inputs': {
            'input data': 'df',
        },
        'named_outputs': {
            'output data': 'out'
        }
    }
    instance = FeatureAssemblerOperation(**arguments)
    with pytest.raises(NameError) as nam_err:
        util.execute(instance.generate_code(), {'df': df})
    assert "name 'invalid' is not defined" in str(nam_err)
def test_feature_assembler_na_drop_success():
    """
    The chained assignment warnings / exceptions are aiming to inform the user
    of a possibly invalid assignment. There may be false positives; situations
    where a chained assignment is inadvertently reported.
    https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
    """
    df = util.iris(['sepallength', 'sepalwidth', 'petalwidth', 'petallength'],
                   size=10)
    # Line below is causing a false positive warning (?)
    df.loc[0:2, 'sepalwidth'] = np.NaN
    test_df = df.copy()

    arguments = {
        'parameters': {
            'attributes': ['sepalwidth', 'petalwidth'],
            'multiplicity': {
                'input data': 0
            }
        },
        'named_inputs': {
            'input data': 'df',
        },
        'named_outputs': {
            'output data': 'out'
        }
    }
    instance = FeatureAssemblerOperation(**arguments)
    result = util.execute(instance.generate_code(), {'df': df})

    test_out = {
        'FeatureField': [
            np.NaN, np.NaN, np.NaN, [3.1, 0.2], [3.6, 0.2], [3.9, 0.4],
            [3.4, 0.3], [3.4, 0.2], [2.9, 0.2], [3.1, 0.1]
        ]
    }
    test_out = pd.DataFrame(test_out)
    test_out = pd.concat([test_df, test_out], axis=1)
    test_out.drop([0, 1, 2], axis=0, inplace=True)
    assert result['out'].equals(test_out)
def test_feature_assembler_missing_attributes_param_fail():
    arguments = {
        'parameters': {
            'multiplicity': {
                'input data': 0
            }
        },
        'named_inputs': {
            'input data': 'df',
        },
        'named_outputs': {
            'output data': 'out'
        }
    }
    with pytest.raises(ValueError) as val_err:
        FeatureAssemblerOperation(**arguments)
    assert "Parameters 'attributes' must be informed for task" in str(
        val_err.value)