コード例 #1
0
def test_result_length():
    null_model = ModelInterface("NULL")
    df = pd.DataFrame({
        "id": [1, 2, 3],
        "text": ["the cat", "the hat", "the mat"]
    })
    result = explain_predictions(null_model, df, "text", None)
    assert len(result) == len(
        df), "Explain function returns results for all records"
コード例 #2
0
def test_single_word_model():
    jellybean_model = SingleWordModel("JellyBeanModel", "TEXT", "jellybean")
    df = pd.DataFrame({
        "ID": [1, 2],
        "TEXT": ["bob eats jellybeans", "jane likes to swim"]
    })
    result = explain_predictions(jellybean_model, df, "TEXT", None)
    assert len(result) == len(
        df), "Explain function returns results for all records"
    assert result[0][
        0] == 1, "First record contains discriminative word that perfectly explains output."
    assert result[1][0] == 0, "Second record cannot be determined"
コード例 #3
0
def test_result_attributes():
    null_model = ModelInterface("NULL")
    df = pd.DataFrame({"id": [1, 2], "text": ["the cat", "the hat"]})
    result = explain_predictions(null_model, df, "text", None)
    assert str(
        type(result)) == "<class 'list'>", "Explain function returns a list"
    assert str(type(
        result[0])) == "<class 'tuple'>", "Returned list contains tuples"
    #assert str(type(result[0][0])) == "<class 'float'>", "First element is a number"
    assert isinstance(result[0][0], float) == True, "First element is a number"
    assert str(type(
        result[0][1])) == "<class 'str'>", "Second element is a string"
コード例 #4
0
def test_exceptions():
    null_model = ModelInterface("NULL")
    df = pd.DataFrame({"id": [1, 2], "text": ["the cat", "the hat"]})
    thrown = False
    try:
        result = explain_predictions(null_model, df, 9, None)
    except:
        thrown = True
    assert thrown == True, "Exception should be thrown when column name is not a string"
    thrown = False
    try:
        result = explain_predictions(null_model, df, "textnot", None)
    except:
        thrown = True
    assert thrown == True, "Exception should be thrown when column is not present in dataframe"

    thrown = False
    try:
        result = explain_predictions("garbage", df, "text", None)
    except:
        thrown = True
    assert thrown == True, "Exception should be thrown when model does not implement predict"
コード例 #5
0
def test_multiple_sentences_model():
    jellybean_model = SingleWordModel("JellyBeanModel", "TEXT", "jellybean")
    df = pd.DataFrame({
        "ID": [1, 2],
        "TEXT": ["I eat a jellybean. Bob eats a fig.", "Jane likes to swim"]
    })
    result = explain_predictions(jellybean_model, df, "TEXT", None)
    assert len(result) == len(
        df), "Explain function returns results for all records"
    assert result[0][
        0] == 1, "First record contains word that perfectly explains output."
    assert result[1][0] == 0, "Second record cannot be determined"
    record_one = result[0]
    print("record one:", record_one)
    record_one_text = result[0][1]
    print("record one text:", record_one_text)
    assert record_one_text.__contains__(
        "{{1.0}}"), "One sentence with full contribution"
    assert record_one_text.__contains__(
        "jellybean{{1.0}}"), "jellybean has full contribution"
コード例 #6
0
def test_multi_word_model():
    mood_model = MultiWordModel("MoodsModel", "TEXT", ["happy", "sad"])
    df = pd.DataFrame({
        "ID": [1, 2],
        "TEXT": [
            "bob is very happy today",
            "jane is happy most mornings, but sometimes sad after school"
        ]
    })
    result = explain_predictions(mood_model, df, "TEXT", None)
    assert len(result) == len(
        df), "Explain function returns results for all records"
    record_one = result[0]
    record_one_text = result[0][1]
    assert record_one_text.__contains__("happy{{0.5}}"), "happy contribution"
    record_two = result[1]
    record_two_text = result[1][1]
    assert record_two_text.__contains__(
        "{{0.5}}"), "Words with partial contribution."
    assert record_two_text.__contains__(
        "sad{{0.5}}"), "sad has half contribution"