2022-03-30 17:02:39 +02:00
|
|
|
import logging
|
|
|
|
|
2021-10-15 16:34:48 +02:00
|
|
|
import pandas as pd
|
2022-01-03 16:59:24 +01:00
|
|
|
import pytest
|
2021-10-15 16:34:48 +02:00
|
|
|
|
2021-10-25 15:50:23 +02:00
|
|
|
from haystack.schema import Document
|
|
|
|
from haystack.pipelines.base import Pipeline
|
2021-10-15 16:34:48 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_table_reader(table_reader):
|
|
|
|
data = {
|
|
|
|
"actors": ["brad pitt", "leonardo di caprio", "george clooney"],
|
2022-01-03 16:59:24 +01:00
|
|
|
"age": ["58", "47", "60"],
|
2021-10-15 16:34:48 +02:00
|
|
|
"number of movies": ["87", "53", "69"],
|
2022-01-03 16:59:24 +01:00
|
|
|
"date of birth": ["18 december 1963", "11 november 1974", "6 may 1961"],
|
2021-10-15 16:34:48 +02:00
|
|
|
}
|
|
|
|
table = pd.DataFrame(data)
|
|
|
|
|
2022-01-03 16:59:24 +01:00
|
|
|
query = "When was Di Caprio born?"
|
2021-10-15 16:34:48 +02:00
|
|
|
prediction = table_reader.predict(query=query, documents=[Document(content=table, content_type="table")])
|
2022-01-03 16:59:24 +01:00
|
|
|
assert prediction["answers"][0].answer == "11 november 1974"
|
2021-10-15 16:34:48 +02:00
|
|
|
assert prediction["answers"][0].offsets_in_context[0].start == 7
|
|
|
|
assert prediction["answers"][0].offsets_in_context[0].end == 8
|
|
|
|
|
|
|
|
|
|
|
|
def test_table_reader_in_pipeline(table_reader):
|
|
|
|
pipeline = Pipeline()
|
|
|
|
pipeline.add_node(table_reader, "TableReader", ["Query"])
|
|
|
|
data = {
|
|
|
|
"actors": ["brad pitt", "leonardo di caprio", "george clooney"],
|
2022-01-03 16:59:24 +01:00
|
|
|
"age": ["58", "47", "60"],
|
2021-10-15 16:34:48 +02:00
|
|
|
"number of movies": ["87", "53", "69"],
|
2022-01-03 16:59:24 +01:00
|
|
|
"date of birth": ["18 december 1963", "11 november 1974", "6 may 1961"],
|
2021-10-15 16:34:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
table = pd.DataFrame(data)
|
2022-01-03 16:59:24 +01:00
|
|
|
query = "When was Di Caprio born?"
|
2021-10-15 16:34:48 +02:00
|
|
|
|
|
|
|
prediction = pipeline.run(query=query, documents=[Document(content=table, content_type="table")])
|
|
|
|
|
2022-01-03 16:59:24 +01:00
|
|
|
assert prediction["answers"][0].answer == "11 november 1974"
|
|
|
|
assert prediction["answers"][0].offsets_in_context[0].start == 7
|
|
|
|
assert prediction["answers"][0].offsets_in_context[0].end == 8
|
2021-10-15 16:34:48 +02:00
|
|
|
|
2022-02-03 13:43:18 +01:00
|
|
|
|
2022-01-03 16:59:24 +01:00
|
|
|
@pytest.mark.parametrize("table_reader", ["tapas"], indirect=True)
|
2021-10-15 16:34:48 +02:00
|
|
|
def test_table_reader_aggregation(table_reader):
|
|
|
|
data = {
|
|
|
|
"Mountain": ["Mount Everest", "K2", "Kangchenjunga", "Lhotse", "Makalu"],
|
2022-02-03 13:43:18 +01:00
|
|
|
"Height": ["8848m", "8,611 m", "8 586m", "8 516 m", "8,485m"],
|
2021-10-15 16:34:48 +02:00
|
|
|
}
|
|
|
|
table = pd.DataFrame(data)
|
|
|
|
|
|
|
|
query = "How tall are all mountains on average?"
|
|
|
|
prediction = table_reader.predict(query=query, documents=[Document(content=table, content_type="table")])
|
|
|
|
assert prediction["answers"][0].answer == "8609.2 m"
|
|
|
|
assert prediction["answers"][0].meta["aggregation_operator"] == "AVERAGE"
|
2022-02-03 13:43:18 +01:00
|
|
|
assert prediction["answers"][0].meta["answer_cells"] == ["8848m", "8,611 m", "8 586m", "8 516 m", "8,485m"]
|
2021-10-15 16:34:48 +02:00
|
|
|
|
|
|
|
query = "How tall are all mountains together?"
|
|
|
|
prediction = table_reader.predict(query=query, documents=[Document(content=table, content_type="table")])
|
|
|
|
assert prediction["answers"][0].answer == "43046.0 m"
|
|
|
|
assert prediction["answers"][0].meta["aggregation_operator"] == "SUM"
|
2022-02-03 13:43:18 +01:00
|
|
|
assert prediction["answers"][0].meta["answer_cells"] == ["8848m", "8,611 m", "8 586m", "8 516 m", "8,485m"]
|
2022-03-30 17:02:39 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_table_without_rows(caplog, table_reader):
|
|
|
|
# empty DataFrame
|
|
|
|
table = pd.DataFrame()
|
|
|
|
document = Document(content=table, content_type="table", id="no_rows")
|
|
|
|
with caplog.at_level(logging.WARNING):
|
|
|
|
predictions = table_reader.predict(query="test", documents=[document])
|
|
|
|
assert "Skipping document with id 'no_rows'" in caplog.text
|
|
|
|
assert len(predictions["answers"]) == 0
|
|
|
|
|
|
|
|
|
|
|
|
def test_text_document(caplog, table_reader):
|
|
|
|
document = Document(content="text", id="text_doc")
|
|
|
|
with caplog.at_level(logging.WARNING):
|
|
|
|
predictions = table_reader.predict(query="test", documents=[document])
|
|
|
|
assert "Skipping document with id 'text_doc'" in caplog.text
|
|
|
|
assert len(predictions["answers"]) == 0
|