From d3a3e1df8b4d2d7348bfd780e8edaa7d415ba3ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ianar=C3=A9=20S=C3=A9vi?= Date: Wed, 26 Jan 2022 17:02:56 +0100 Subject: [PATCH] fix: :bug: fix probabilities not loaded from API --- mindee/documents/financial_document.py | 4 +- mindee/documents/invoice.py | 14 ++- mindee/documents/passport.py | 8 +- mindee/documents/receipt.py | 8 +- mindee/fields/__init__.py | 8 +- tests/documents/test_financial_doc.py | 68 +++++++-------- tests/documents/test_invoice.py | 114 +++++++++++++------------ tests/documents/test_receipt.py | 36 ++++---- tests/fields/test_amount.py | 4 +- tests/fields/test_date.py | 4 +- tests/fields/test_field.py | 26 +++--- tests/fields/test_locale.py | 4 +- tests/fields/test_orientation.py | 6 +- tests/fields/test_payment_details.py | 6 +- tests/fields/test_tax.py | 6 +- 15 files changed, 160 insertions(+), 156 deletions(-) diff --git a/mindee/documents/financial_document.py b/mindee/documents/financial_document.py index 6ee2a73e..846c6c1d 100644 --- a/mindee/documents/financial_document.py +++ b/mindee/documents/financial_document.py @@ -141,7 +141,7 @@ def build_from_api_prediction(self, api_prediction, input_file, page_n=0): self.company_number = invoice.company_number self.orientation = invoice.orientation self.total_tax = invoice.total_tax - self.time = Field({"value": None, "probability": 0.0}) + self.time = Field({"value": None, "confidence": 0.0}) else: receipt = Receipt(api_prediction, input_file, page_n=page_n) self.orientation = receipt.orientation @@ -154,7 +154,7 @@ def build_from_api_prediction(self, api_prediction, input_file, page_n=0): self.merchant_name = receipt.merchant_name self.time = receipt.time self.total_tax = receipt.total_tax - self.invoice_number = Field({"value": None, "probability": 0.0}) + self.invoice_number = Field({"value": None, "confidence": 0.0}) self.payment_details = [] self.company_number = [] diff --git a/mindee/documents/invoice.py b/mindee/documents/invoice.py index b34702bc..dd302e09 100644 --- a/mindee/documents/invoice.py +++ b/mindee/documents/invoice.py @@ -67,7 +67,6 @@ def __init__( self.total_incl = Amount( {"value": total_incl}, value_key="value", page_n=page_n ) - self.date = Date({"value": invoice_date}, value_key="value", page_n=page_n) self.invoice_date = Date( {"value": invoice_date}, value_key="value", page_n=page_n ) @@ -111,7 +110,7 @@ def __init__( # Reconstruct extra fields self._reconstruct() - def build_from_api_prediction(self, api_prediction, page_n=0): + def build_from_api_prediction(self, api_prediction: dict, page_n=0): """ :param api_prediction: Raw prediction from HTTP response :param page_n: Page number for multi pages pdf input @@ -149,7 +148,7 @@ def build_from_api_prediction(self, api_prediction, page_n=0): api_prediction["total_excl"], value_key="value", page_n=page_n ) self.total_tax = Amount( - {"value": None, "probability": 0.0}, value_key="value", page_n=page_n + {"value": None, "confidence": 0.0}, value_key="value", page_n=page_n ) def __str__(self) -> str: @@ -364,7 +363,7 @@ def __reconstruct_total_incl_from_taxes_plus_excl(self): [tax.value if tax.value is not None else 0 for tax in self.taxes] ) + self.total_excl.value, - "probability": Field.array_probability(self.taxes) + "confidence": Field.array_probability(self.taxes) * self.total_excl.probability, } self.total_incl = Amount(total_incl, value_key="value", reconstructed=True) @@ -388,7 +387,7 @@ def __reconstruct_total_excl_from_tcc_and_taxes(self): - sum( [tax.value if tax.value is not None else 0 for tax in self.taxes] ), - "probability": Field.array_probability(self.taxes) + "confidence": Field.array_probability(self.taxes) * self.total_incl.probability, } self.total_excl = Amount(total_excl, value_key="value", reconstructed=True) @@ -404,7 +403,7 @@ def __reconstruct_total_tax_from_tax_lines(self): "value": sum( [tax.value if tax.value is not None else 0 for tax in self.taxes] ), - "probability": Field.array_probability(self.taxes), + "confidence": Field.array_probability(self.taxes), } if total_tax["value"] > 0: self.total_tax = Amount( @@ -427,8 +426,7 @@ def __reconstruct_total_tax_from_incl_and_excl(self): total_tax = { "value": self.total_incl.value - self.total_excl.value, - "probability": self.total_incl.probability - * self.total_excl.probability, + "confidence": self.total_incl.probability * self.total_excl.probability, } if total_tax["value"] >= 0: self.total_tax = Amount( diff --git a/mindee/documents/passport.py b/mindee/documents/passport.py index bf09b631..af877feb 100644 --- a/mindee/documents/passport.py +++ b/mindee/documents/passport.py @@ -123,8 +123,8 @@ def build_from_api_prediction(self, api_prediction, page_n=0): Field(given_name, page_n=page_n) for given_name in api_prediction["given_names"] ] - self.mrz = Field({"value": None, "probability": 0.0}, page_n=page_n) - self.full_name = Field({"value": None, "probability": 0.0}, page_n=page_n) + self.mrz = Field({"value": None, "confidence": 0.0}, page_n=page_n) + self.full_name = Field({"value": None, "confidence": 0.0}, page_n=page_n) def __str__(self) -> str: return ( @@ -322,7 +322,7 @@ def __reconstruct_mrz(self): ): mrz = { "value": self.mrz1.value + self.mrz2.value, - "probability": Field.array_probability( + "confidence": Field.array_probability( [self.mrz1.probability, self.mrz2.probability] ), } @@ -342,7 +342,7 @@ def __reconstruct_full_name(self): ): full_name = { "value": self.given_names[0].value + " " + self.surname.value, - "probability": Field.array_probability( + "confidence": Field.array_probability( [self.surname.probability, self.given_names[0].probability] ), } diff --git a/mindee/documents/receipt.py b/mindee/documents/receipt.py index cb99878c..75787a05 100644 --- a/mindee/documents/receipt.py +++ b/mindee/documents/receipt.py @@ -147,10 +147,10 @@ def build_from_api_prediction(self, api_prediction, page_n=0): if str(page_n) != "-1": self.orientation = Orientation(api_prediction["orientation"], page_n=page_n) self.total_tax = Amount( - {"value": None, "probability": 0.0}, value_key="value", page_n=page_n + {"value": None, "confidence": 0.0}, value_key="value", page_n=page_n ) self.total_excl = Amount( - {"value": None, "probability": 0.0}, value_key="value", page_n=page_n + {"value": None, "confidence": 0.0}, value_key="value", page_n=page_n ) @staticmethod @@ -251,7 +251,7 @@ def __reconstruct_total_excl_from_tcc_and_taxes(self): if len(self.taxes) and self.total_incl.value is not None: total_excl = { "value": self.total_incl.value - Field.array_sum(self.taxes), - "probability": Field.array_probability(self.taxes) + "confidence": Field.array_probability(self.taxes) * self.total_incl.probability, } self.total_excl = Amount(total_excl, value_key="value", reconstructed=True) @@ -267,7 +267,7 @@ def __reconstruct_total_tax(self): "value": sum( [tax.value if tax.value is not None else 0 for tax in self.taxes] ), - "probability": Field.array_probability(self.taxes), + "confidence": Field.array_probability(self.taxes), } if total_tax["value"] > 0: self.total_tax = Amount( diff --git a/mindee/fields/__init__.py b/mindee/fields/__init__.py index 9ae9cac4..933b2e8c 100644 --- a/mindee/fields/__init__.py +++ b/mindee/fields/__init__.py @@ -1,4 +1,6 @@ class Field: + probability: float + def __init__( self, abstract_prediction, @@ -25,9 +27,9 @@ def __init__( else: self.value = abstract_prediction[value_key] - if "probability" in abstract_prediction: - self.probability = abstract_prediction["probability"] - else: + try: + self.probability = float(abstract_prediction["confidence"]) + except (KeyError, TypeError): self.probability = 0.0 if "polygon" in abstract_prediction: diff --git a/tests/documents/test_financial_doc.py b/tests/documents/test_financial_doc.py index 1485cd77..c4191043 100644 --- a/tests/documents/test_financial_doc.py +++ b/tests/documents/test_financial_doc.py @@ -111,15 +111,15 @@ def test__str__receipt(financial_doc_from_receipt_object): # Business tests from receipt def test__receipt_reconstruct_total_excl_from_total_and_taxes_1(receipt_pred): # no incl implies no reconstruct for total excl - receipt_pred["total_incl"] = {"value": "N/A", "probability": 0.0} - receipt_pred["taxes"] = [{"rate": 20, "value": 9.5, "probability": 0.9}] + receipt_pred["total_incl"] = {"value": "N/A", "confidence": 0.0} + receipt_pred["taxes"] = [{"rate": 20, "value": 9.5, "confidence": 0.9}] financial_doc = FinancialDocument(receipt_pred) assert financial_doc.total_excl.value is None def test__receipt_reconstruct_total_excl_from_total_and_taxes_2(receipt_pred): # no taxes implies no reconstruct for total excl - receipt_pred["total_incl"] = {"value": 12.54, "probability": 0.0} + receipt_pred["total_incl"] = {"value": 12.54, "confidence": 0.0} receipt_pred["taxes"] = [] financial_doc = FinancialDocument(receipt_pred) assert financial_doc.total_excl.value is None @@ -127,10 +127,10 @@ def test__receipt_reconstruct_total_excl_from_total_and_taxes_2(receipt_pred): def test__receipt_reconstruct_total_excl_from_total_and_taxes_3(receipt_pred): # working example - receipt_pred["total_incl"] = {"value": 12.54, "probability": 0.5} + receipt_pred["total_incl"] = {"value": 12.54, "confidence": 0.5} receipt_pred["taxes"] = [ - {"rate": 20, "value": 0.5, "probability": 0.1}, - {"rate": 10, "value": 4.25, "probability": 0.6}, + {"rate": 20, "value": 0.5, "confidence": 0.1}, + {"rate": 10, "value": 4.25, "confidence": 0.6}, ] financial_doc = FinancialDocument(receipt_pred) assert financial_doc.total_excl.probability == 0.03 @@ -147,8 +147,8 @@ def test__receipt_reconstruct_total_tax_1(receipt_pred): def test__receipt_reconstruct_total_tax_2(receipt_pred): # working example receipt_pred["taxes"] = [ - {"rate": 20, "value": 10.2, "probability": 0.5}, - {"rate": 10, "value": 40.0, "probability": 0.1}, + {"rate": 20, "value": 10.2, "confidence": 0.5}, + {"rate": 10, "value": 40.0, "confidence": 0.1}, ] financial_doc = FinancialDocument(receipt_pred) assert financial_doc.total_tax.value == 50.2 @@ -157,10 +157,10 @@ def test__receipt_reconstruct_total_tax_2(receipt_pred): def test__receipt_taxes_match_total_incl_1(receipt_pred): # matching example - receipt_pred["total_incl"] = {"value": 507.25, "probability": 0.6} + receipt_pred["total_incl"] = {"value": 507.25, "confidence": 0.6} receipt_pred["taxes"] = [ - {"rate": 20, "value": 10.99, "probability": 0.5}, - {"rate": 10, "value": 40.12, "probability": 0.1}, + {"rate": 20, "value": 10.99, "confidence": 0.5}, + {"rate": 10, "value": 40.12, "confidence": 0.1}, ] financial_doc = FinancialDocument(receipt_pred) assert financial_doc.checklist["taxes_match_total_incl"] is True @@ -171,10 +171,10 @@ def test__receipt_taxes_match_total_incl_1(receipt_pred): def test__receipt_taxes_match_total_incl_2(receipt_pred): # not matching example with close error - receipt_pred["total_incl"] = {"value": 507.25, "probability": 0.6} + receipt_pred["total_incl"] = {"value": 507.25, "confidence": 0.6} receipt_pred["taxes"] = [ - {"rate": 20, "value": 10.9, "probability": 0.5}, - {"rate": 10, "value": 40.12, "probability": 0.1}, + {"rate": 20, "value": 10.9, "confidence": 0.5}, + {"rate": 10, "value": 40.12, "confidence": 0.1}, ] financial_doc = FinancialDocument(receipt_pred) assert financial_doc.checklist["taxes_match_total_incl"] is False @@ -182,8 +182,8 @@ def test__receipt_taxes_match_total_incl_2(receipt_pred): def test__receipt_taxes_match_total_incl_3(receipt_pred): # sanity check with null tax - receipt_pred["total_incl"] = {"value": 507.25, "probability": 0.6} - receipt_pred["taxes"] = [{"rate": 20, "value": 0.0, "probability": 0.5}] + receipt_pred["total_incl"] = {"value": 507.25, "confidence": 0.6} + receipt_pred["taxes"] = [{"rate": 20, "value": 0.0, "confidence": 0.5}] financial_doc = FinancialDocument(receipt_pred) assert financial_doc.checklist["taxes_match_total_incl"] is False @@ -191,15 +191,15 @@ def test__receipt_taxes_match_total_incl_3(receipt_pred): # Business tests from invoice def test__invoice_reconstruct_total_excl_from_total_and_taxes_1(invoice_pred): # no incl implies no reconstruct for total excl - invoice_pred["total_incl"] = {"amount": "N/A", "probability": 0.0} - invoice_pred["taxes"] = [{"rate": 20, "amount": 9.5, "probability": 0.9}] + invoice_pred["total_incl"] = {"amount": "N/A", "confidence": 0.0} + invoice_pred["taxes"] = [{"rate": 20, "amount": 9.5, "confidence": 0.9}] financial_doc = FinancialDocument(invoice_pred) assert financial_doc.total_excl.value is None def test__invoice_reconstruct_total_excl_from_total_and_taxes_2(invoice_pred): # no taxes implies no reconstruct for total excl - invoice_pred["total_incl"] = {"amount": 12.54, "probability": 0.0} + invoice_pred["total_incl"] = {"amount": 12.54, "confidence": 0.0} invoice_pred["taxes"] = [] financial_doc = FinancialDocument(invoice_pred) assert financial_doc.total_excl.value is None @@ -207,10 +207,10 @@ def test__invoice_reconstruct_total_excl_from_total_and_taxes_2(invoice_pred): def test__invoice_reconstruct_total_excl_from_total_and_taxes_3(invoice_pred): # working example - invoice_pred["total_incl"] = {"value": 12.54, "probability": 0.5} + invoice_pred["total_incl"] = {"value": 12.54, "confidence": 0.5} invoice_pred["taxes"] = [ - {"rate": 20, "value": 0.5, "probability": 0.1}, - {"rate": 10, "value": 4.25, "probability": 0.6}, + {"rate": 20, "value": 0.5, "confidence": 0.1}, + {"rate": 10, "value": 4.25, "confidence": 0.6}, ] financial_doc = FinancialDocument(invoice_pred) assert financial_doc.total_excl.probability == 0.03 @@ -227,8 +227,8 @@ def test__invoice_reconstruct_total_tax_1(invoice_pred): def test__invoice_reconstruct_total_tax_2(invoice_pred): # working example invoice_pred["taxes"] = [ - {"rate": 20, "value": 10.2, "probability": 0.5}, - {"rate": 10, "value": 40.0, "probability": 0.1}, + {"rate": 20, "value": 10.2, "confidence": 0.5}, + {"rate": 10, "value": 40.0, "confidence": 0.1}, ] financial_doc = FinancialDocument(invoice_pred) assert financial_doc.total_tax.value == 50.2 @@ -237,10 +237,10 @@ def test__invoice_reconstruct_total_tax_2(invoice_pred): def test__invoice_taxes_match_total_incl_1(invoice_pred): # matching example - invoice_pred["total_incl"] = {"value": 507.25, "probability": 0.6} + invoice_pred["total_incl"] = {"value": 507.25, "confidence": 0.6} invoice_pred["taxes"] = [ - {"rate": 20, "value": 10.99, "probability": 0.5}, - {"rate": 10, "value": 40.12, "probability": 0.1}, + {"rate": 20, "value": 10.99, "confidence": 0.5}, + {"rate": 10, "value": 40.12, "confidence": 0.1}, ] financial_doc = FinancialDocument(invoice_pred) assert financial_doc.checklist["taxes_match_total_incl"] is True @@ -251,10 +251,10 @@ def test__invoice_taxes_match_total_incl_1(invoice_pred): def test__invoice_taxes_match_total_incl_2(invoice_pred): # not matching example with close error - invoice_pred["total_incl"] = {"value": 507.25, "probability": 0.6} + invoice_pred["total_incl"] = {"value": 507.25, "confidence": 0.6} invoice_pred["taxes"] = [ - {"rate": 20, "value": 10.9, "probability": 0.5}, - {"rate": 10, "value": 40.12, "probability": 0.1}, + {"rate": 20, "value": 10.9, "confidence": 0.5}, + {"rate": 10, "value": 40.12, "confidence": 0.1}, ] financial_doc = FinancialDocument(invoice_pred) assert financial_doc.checklist["taxes_match_total_incl"] is False @@ -262,16 +262,16 @@ def test__invoice_taxes_match_total_incl_2(invoice_pred): def test__invoice_taxes_match_total_incl_3(invoice_pred): # sanity check with null tax - invoice_pred["total_incl"] = {"value": 507.25, "probability": 0.6} - invoice_pred["taxes"] = [{"rate": 20, "value": 0.0, "probability": 0.5}] + invoice_pred["total_incl"] = {"value": 507.25, "confidence": 0.6} + invoice_pred["taxes"] = [{"rate": 20, "value": 0.0, "confidence": 0.5}] financial_doc = FinancialDocument(invoice_pred) assert financial_doc.checklist["taxes_match_total_incl"] is False def test__shouldnt_raise_when_tax_rate_none(invoice_pred): # sanity check with null tax - invoice_pred["total_incl"] = {"value": 507.25, "probability": 0.6} - invoice_pred["taxes"] = [{"rate": "N/A", "value": 0.0, "probability": 0.5}] + invoice_pred["total_incl"] = {"value": 507.25, "confidence": 0.6} + invoice_pred["taxes"] = [{"rate": "N/A", "value": 0.0, "confidence": 0.5}] financial_doc = FinancialDocument(invoice_pred) assert financial_doc.checklist["taxes_match_total_incl"] is False diff --git a/tests/documents/test_invoice.py b/tests/documents/test_invoice.py index 072821cd..3cbd851e 100644 --- a/tests/documents/test_invoice.py +++ b/tests/documents/test_invoice.py @@ -47,7 +47,11 @@ def test_constructor(invoice_object): assert invoice_object.checklist["taxes_match_total_excl"] is True assert invoice_object.checklist["taxes_plus_total_excl_match_total_incl"] is True assert invoice_object.total_tax.value == 97.98 - assert type(invoice_object.__str__()) == str + assert invoice_object.invoice_date.value == "2020-02-17" + assert invoice_object.invoice_date.probability == 0.97 + assert invoice_object.invoice_number.value == "0042004801351" + assert invoice_object.invoice_number.probability == 0.95 + assert isinstance(invoice_object.__str__(), str) def test_all_na(invoice_object_all_na): @@ -72,8 +76,8 @@ def test_checklist_on_empty(invoice_object_all_na): # Business tests def test__reconstruct_total_incl_from_taxes_plus_excl_1(invoice_pred): # no taxes implies no reconstruct for total incl - invoice_pred["total_incl"] = {"value": "N/A", "probability": 0.0} - invoice_pred["total_excl"] = {"value": 240.5, "probability": 0.9} + invoice_pred["total_incl"] = {"value": "N/A", "confidence": 0.0} + invoice_pred["total_excl"] = {"value": 240.5, "confidence": 0.9} invoice_pred["taxes"] = [] invoice = Invoice(invoice_pred) assert invoice.total_incl.value is None @@ -81,18 +85,18 @@ def test__reconstruct_total_incl_from_taxes_plus_excl_1(invoice_pred): def test__reconstruct_total_incl_from_taxes_plus_excl_2(invoice_pred): # no excl implies no reconstruct for total incl - invoice_pred["total_incl"] = {"value": "N/A", "probability": 0.0} - invoice_pred["total_excl"] = {"value": "N/A", "probability": 0.0} - invoice_pred["taxes"] = [{"rate": 20, "value": 9.5, "probability": 0.9}] + invoice_pred["total_incl"] = {"value": "N/A", "confidence": 0.0} + invoice_pred["total_excl"] = {"value": "N/A", "confidence": 0.0} + invoice_pred["taxes"] = [{"rate": 20, "value": 9.5, "confidence": 0.9}] invoice = Invoice(invoice_pred) assert invoice.total_incl.value is None def test__reconstruct_total_incl_from_taxes_plus_excl_3(invoice_pred): # incl already exists implies no reconstruct - invoice_pred["total_incl"] = {"value": 260, "probability": 0.4} - invoice_pred["total_excl"] = {"value": 240.5, "probability": 0.9} - invoice_pred["taxes"] = [{"rate": 20, "value": 9.5, "probability": 0.9}] + invoice_pred["total_incl"] = {"value": 260, "confidence": 0.4} + invoice_pred["total_excl"] = {"value": 240.5, "confidence": 0.9} + invoice_pred["taxes"] = [{"rate": 20, "value": 9.5, "confidence": 0.9}] invoice = Invoice(invoice_pred) assert invoice.total_incl.value == 260 assert invoice.total_incl.probability == 0.4 @@ -100,9 +104,9 @@ def test__reconstruct_total_incl_from_taxes_plus_excl_3(invoice_pred): def test__reconstruct_total_incl_from_taxes_plus_excl_4(invoice_pred): # working example - invoice_pred["total_incl"] = {"value": "N/A", "probability": 0.0} - invoice_pred["total_excl"] = {"value": 240.5, "probability": 0.9} - invoice_pred["taxes"] = [{"rate": 20, "value": 9.5, "probability": 0.9}] + invoice_pred["total_incl"] = {"value": "N/A", "confidence": 0.0} + invoice_pred["total_excl"] = {"value": 240.5, "confidence": 0.9} + invoice_pred["taxes"] = [{"rate": 20, "value": 9.5, "confidence": 0.9}] invoice = Invoice(invoice_pred) assert invoice.total_incl.value == 250 assert invoice.total_incl.probability == 0.81 @@ -110,17 +114,17 @@ def test__reconstruct_total_incl_from_taxes_plus_excl_4(invoice_pred): def test__reconstruct_total_excl_from_tcc_and_taxes_1(invoice_pred): # no incl implies no reconstruct for total excl - invoice_pred["total_incl"] = {"value": "N/A", "probability": 0.0} - invoice_pred["total_excl"] = {"value": "N/A", "probability": 0.0} - invoice_pred["taxes"] = [{"rate": 20, "value": 9.5, "probability": 0.9}] + invoice_pred["total_incl"] = {"value": "N/A", "confidence": 0.0} + invoice_pred["total_excl"] = {"value": "N/A", "confidence": 0.0} + invoice_pred["taxes"] = [{"rate": 20, "value": 9.5, "confidence": 0.9}] invoice = Invoice(invoice_pred) assert invoice.total_excl.value is None def test__reconstruct_total_excl_from_tcc_and_taxes_2(invoice_pred): # no taxes implies no reconstruct for total excl - invoice_pred["total_incl"] = {"value": 1150.20, "probability": 0.7} - invoice_pred["total_excl"] = {"value": "N/A", "probability": 0.0} + invoice_pred["total_incl"] = {"value": 1150.20, "confidence": 0.7} + invoice_pred["total_excl"] = {"value": "N/A", "confidence": 0.0} invoice_pred["taxes"] = [] invoice = Invoice(invoice_pred) assert invoice.total_excl.value is None @@ -128,8 +132,8 @@ def test__reconstruct_total_excl_from_tcc_and_taxes_2(invoice_pred): def test__reconstruct_total_excl_from_tcc_and_taxes_3(invoice_pred): # excl already exists implies no reconstruct - invoice_pred["total_incl"] = {"value": 1150.20, "probability": 0.7} - invoice_pred["total_excl"] = {"value": 1050.0, "probability": 0.4} + invoice_pred["total_incl"] = {"value": 1150.20, "confidence": 0.7} + invoice_pred["total_excl"] = {"value": 1050.0, "confidence": 0.4} invoice_pred["taxes"] = [] invoice = Invoice(invoice_pred) assert invoice.total_excl.value == 1050.0 @@ -138,11 +142,11 @@ def test__reconstruct_total_excl_from_tcc_and_taxes_3(invoice_pred): def test__reconstruct_total_excl_from_tcc_and_taxes_4(invoice_pred): # working example - invoice_pred["total_incl"] = {"value": 1150.20, "probability": 0.6} - invoice_pred["total_excl"] = {"value": "N/A", "probability": 0.0} + invoice_pred["total_incl"] = {"value": 1150.20, "confidence": 0.6} + invoice_pred["total_excl"] = {"value": "N/A", "confidence": 0.0} invoice_pred["taxes"] = [ - {"rate": 20, "value": 10.2, "probability": 0.5}, - {"rate": 10, "value": 40.0, "probability": 0.1}, + {"rate": 20, "value": 10.2, "confidence": 0.5}, + {"rate": 10, "value": 40.0, "confidence": 0.1}, ] invoice = Invoice(invoice_pred) assert invoice.total_excl.value == 1100 @@ -159,8 +163,8 @@ def test__reconstruct_total_tax_1(invoice_pred): def test__reconstruct_total_tax_2(invoice_pred): # working example invoice_pred["taxes"] = [ - {"rate": 20, "value": 10.2, "probability": 0.5}, - {"rate": 10, "value": 40.0, "probability": 0.1}, + {"rate": 20, "value": 10.2, "confidence": 0.5}, + {"rate": 10, "value": 40.0, "confidence": 0.1}, ] invoice = Invoice(invoice_pred) assert invoice.total_tax.value == 50.2 @@ -169,10 +173,10 @@ def test__reconstruct_total_tax_2(invoice_pred): def test__taxes_match_total_incl_1(invoice_pred): # matching example - invoice_pred["total_incl"] = {"value": 507.25, "probability": 0.6} + invoice_pred["total_incl"] = {"value": 507.25, "confidence": 0.6} invoice_pred["taxes"] = [ - {"rate": 20, "value": 10.99, "probability": 0.5}, - {"rate": 10, "value": 40.12, "probability": 0.1}, + {"rate": 20, "value": 10.99, "confidence": 0.5}, + {"rate": 10, "value": 40.12, "confidence": 0.1}, ] invoice = Invoice(invoice_pred) assert invoice.checklist["taxes_match_total_incl"] is True @@ -183,10 +187,10 @@ def test__taxes_match_total_incl_1(invoice_pred): def test__taxes_match_total_incl_2(invoice_pred): # not matching example with close error - invoice_pred["total_incl"] = {"value": 507.25, "probability": 0.6} + invoice_pred["total_incl"] = {"value": 507.25, "confidence": 0.6} invoice_pred["taxes"] = [ - {"rate": 20, "value": 10.9, "probability": 0.5}, - {"rate": 10, "value": 40.12, "probability": 0.1}, + {"rate": 20, "value": 10.9, "confidence": 0.5}, + {"rate": 10, "value": 40.12, "confidence": 0.1}, ] invoice = Invoice(invoice_pred) assert invoice.checklist["taxes_match_total_incl"] is False @@ -194,18 +198,18 @@ def test__taxes_match_total_incl_2(invoice_pred): def test__taxes_match_total_incl_3(invoice_pred): # sanity check with null tax - invoice_pred["total_incl"] = {"value": 507.25, "probability": 0.6} - invoice_pred["taxes"] = [{"rate": 20, "value": 0.0, "probability": 0.5}] + invoice_pred["total_incl"] = {"value": 507.25, "confidence": 0.6} + invoice_pred["taxes"] = [{"rate": 20, "value": 0.0, "confidence": 0.5}] invoice = Invoice(invoice_pred) assert invoice.checklist["taxes_match_total_incl"] is False def test__taxes_match_total_excl_1(invoice_pred): # matching example - invoice_pred["total_excl"] = {"value": 456.15, "probability": 0.6} + invoice_pred["total_excl"] = {"value": 456.15, "confidence": 0.6} invoice_pred["taxes"] = [ - {"rate": 20, "value": 10.99, "probability": 0.5}, - {"rate": 10, "value": 40.12, "probability": 0.1}, + {"rate": 20, "value": 10.99, "confidence": 0.5}, + {"rate": 10, "value": 40.12, "confidence": 0.1}, ] invoice = Invoice(invoice_pred) assert invoice.checklist["taxes_match_total_excl"] is True @@ -216,10 +220,10 @@ def test__taxes_match_total_excl_1(invoice_pred): def test__taxes_match_total_excl_2(invoice_pred): # not matching example with close error - invoice_pred["total_excl"] = {"value": 456.15, "probability": 0.6} + invoice_pred["total_excl"] = {"value": 456.15, "confidence": 0.6} invoice_pred["taxes"] = [ - {"rate": 20, "value": 10.9, "probability": 0.5}, - {"rate": 10, "value": 40.12, "probability": 0.1}, + {"rate": 20, "value": 10.9, "confidence": 0.5}, + {"rate": 10, "value": 40.12, "confidence": 0.1}, ] invoice = Invoice(invoice_pred) assert invoice.checklist["taxes_match_total_excl"] is False @@ -227,19 +231,19 @@ def test__taxes_match_total_excl_2(invoice_pred): def test__taxes_match_total_excl_3(invoice_pred): # sanity check with null tax - invoice_pred["total_excl"] = {"value": 507.25, "probability": 0.6} - invoice_pred["taxes"] = [{"rate": 20, "value": 0.0, "probability": 0.5}] + invoice_pred["total_excl"] = {"value": 507.25, "confidence": 0.6} + invoice_pred["taxes"] = [{"rate": 20, "value": 0.0, "confidence": 0.5}] invoice = Invoice(invoice_pred) assert invoice.checklist["taxes_match_total_incl"] is False def test__taxes_plus_total_excl_match_total_incl_1(invoice_pred): # matching example - invoice_pred["total_incl"] = {"value": 507.25, "probability": 0.6} - invoice_pred["total_excl"] = {"value": 456.15, "probability": 0.6} + invoice_pred["total_incl"] = {"value": 507.25, "confidence": 0.6} + invoice_pred["total_excl"] = {"value": 456.15, "confidence": 0.6} invoice_pred["taxes"] = [ - {"rate": 20, "value": 10.99, "probability": 0.5}, - {"rate": 10, "value": 40.12, "probability": 0.1}, + {"rate": 20, "value": 10.99, "confidence": 0.5}, + {"rate": 10, "value": 40.12, "confidence": 0.1}, ] invoice = Invoice(invoice_pred) assert invoice.checklist["taxes_plus_total_excl_match_total_incl"] is True @@ -251,11 +255,11 @@ def test__taxes_plus_total_excl_match_total_incl_1(invoice_pred): def test__taxes_plus_total_excl_match_total_incl_2(invoice_pred): # not matching example - invoice_pred["total_incl"] = {"value": 507.2, "probability": 0.6} - invoice_pred["total_excl"] = {"value": 456.15, "probability": 0.6} + invoice_pred["total_incl"] = {"value": 507.2, "confidence": 0.6} + invoice_pred["total_excl"] = {"value": 456.15, "confidence": 0.6} invoice_pred["taxes"] = [ - {"rate": 20, "value": 10.99, "probability": 0.5}, - {"rate": 10, "value": 40.12, "probability": 0.1}, + {"rate": 20, "value": 10.99, "confidence": 0.5}, + {"rate": 10, "value": 40.12, "confidence": 0.1}, ] invoice = Invoice(invoice_pred) assert invoice.checklist["taxes_plus_total_excl_match_total_incl"] is False @@ -263,18 +267,18 @@ def test__taxes_plus_total_excl_match_total_incl_2(invoice_pred): def test__taxes_plus_total_excl_match_total_incl_3(invoice_pred): # sanity check with null tax - invoice_pred["total_excl"] = {"value": 456.15, "probability": 0.6} - invoice_pred["total_incl"] = {"value": 507.25, "probability": 0.6} - invoice_pred["taxes"] = [{"rate": 20, "value": 0.0, "probability": 0.5}] + invoice_pred["total_excl"] = {"value": 456.15, "confidence": 0.6} + invoice_pred["total_incl"] = {"value": 507.25, "confidence": 0.6} + invoice_pred["taxes"] = [{"rate": 20, "value": 0.0, "confidence": 0.5}] invoice = Invoice(invoice_pred) assert invoice.checklist["taxes_match_total_incl"] is False def test__shouldnt_raise_when_tax_rate_none(invoice_pred): # sanity check with null tax - invoice_pred["total_excl"] = {"value": 456.15, "probability": 0.6} - invoice_pred["total_incl"] = {"value": 507.25, "probability": 0.6} - invoice_pred["taxes"] = [{"rate": "N/A", "value": 0.0, "probability": 0.5}] + invoice_pred["total_excl"] = {"value": 456.15, "confidence": 0.6} + invoice_pred["total_incl"] = {"value": 507.25, "confidence": 0.6} + invoice_pred["taxes"] = [{"rate": "N/A", "value": 0.0, "confidence": 0.5}] invoice = Invoice(invoice_pred) assert invoice.checklist["taxes_match_total_incl"] is False diff --git a/tests/documents/test_receipt.py b/tests/documents/test_receipt.py index c4b46c6c..899bffda 100644 --- a/tests/documents/test_receipt.py +++ b/tests/documents/test_receipt.py @@ -69,15 +69,15 @@ def test_checklist_on_empty(receipt_object_all_na): # Business tests def test__reconstruct_total_excl_from_total_and_taxes_1(receipt_pred): # no incl implies no reconstruct for total excl - receipt_pred["total_incl"] = {"value": "N/A", "probability": 0.0} - receipt_pred["taxes"] = [{"rate": 20, "value": 9.5, "probability": 0.9}] + receipt_pred["total_incl"] = {"value": "N/A", "confidence": 0.0} + receipt_pred["taxes"] = [{"rate": 20, "value": 9.5, "confidence": 0.9}] receipt = Receipt(receipt_pred) assert receipt.total_excl.value is None def test__reconstruct_total_excl_from_total_and_taxes_2(receipt_pred): # no taxes implies no reconstruct for total excl - receipt_pred["total_incl"] = {"value": 12.54, "probability": 0.0} + receipt_pred["total_incl"] = {"value": 12.54, "confidence": 0.0} receipt_pred["taxes"] = [] receipt = Receipt(receipt_pred) assert receipt.total_excl.value is None @@ -85,10 +85,10 @@ def test__reconstruct_total_excl_from_total_and_taxes_2(receipt_pred): def test__reconstruct_total_excl_from_total_and_taxes_3(receipt_pred): # working example - receipt_pred["total_incl"] = {"value": 12.54, "probability": 0.5} + receipt_pred["total_incl"] = {"value": 12.54, "confidence": 0.5} receipt_pred["taxes"] = [ - {"rate": 20, "value": 0.5, "probability": 0.1}, - {"rate": 10, "value": 4.25, "probability": 0.6}, + {"rate": 20, "value": 0.5, "confidence": 0.1}, + {"rate": 10, "value": 4.25, "confidence": 0.6}, ] receipt = Receipt(receipt_pred) assert receipt.total_excl.probability == 0.03 @@ -105,8 +105,8 @@ def test__reconstruct_total_tax_1(receipt_pred): def test__reconstruct_total_tax_2(receipt_pred): # working example receipt_pred["taxes"] = [ - {"rate": 20, "value": 10.2, "probability": 0.5}, - {"rate": 10, "value": 40.0, "probability": 0.1}, + {"rate": 20, "value": 10.2, "confidence": 0.5}, + {"rate": 10, "value": 40.0, "confidence": 0.1}, ] receipt = Receipt(receipt_pred) assert receipt.total_tax.value == 50.2 @@ -115,10 +115,10 @@ def test__reconstruct_total_tax_2(receipt_pred): def test__taxes_match_total_incl_1(receipt_pred): # matching example - receipt_pred["total_incl"] = {"value": 507.25, "probability": 0.6} + receipt_pred["total_incl"] = {"value": 507.25, "confidence": 0.6} receipt_pred["taxes"] = [ - {"rate": 20, "value": 10.99, "probability": 0.5}, - {"rate": 10, "value": 40.12, "probability": 0.1}, + {"rate": 20, "value": 10.99, "confidence": 0.5}, + {"rate": 10, "value": 40.12, "confidence": 0.1}, ] receipt = Receipt(receipt_pred) assert receipt.checklist["taxes_match_total_incl"] is True @@ -129,10 +129,10 @@ def test__taxes_match_total_incl_1(receipt_pred): def test__taxes_match_total_incl_2(receipt_pred): # not matching example with close error - receipt_pred["total_incl"] = {"value": 507.25, "probability": 0.6} + receipt_pred["total_incl"] = {"value": 507.25, "confidence": 0.6} receipt_pred["taxes"] = [ - {"rate": 20, "value": 10.9, "probability": 0.5}, - {"rate": 10, "value": 40.12, "probability": 0.1}, + {"rate": 20, "value": 10.9, "confidence": 0.5}, + {"rate": 10, "value": 40.12, "confidence": 0.1}, ] receipt = Receipt(receipt_pred) assert receipt.checklist["taxes_match_total_incl"] is False @@ -140,16 +140,16 @@ def test__taxes_match_total_incl_2(receipt_pred): def test__taxes_match_total_incl_3(receipt_pred): # sanity check with null tax - receipt_pred["total_incl"] = {"value": 507.25, "probability": 0.6} - receipt_pred["taxes"] = [{"rate": 20, "value": 0.0, "probability": 0.5}] + receipt_pred["total_incl"] = {"value": 507.25, "confidence": 0.6} + receipt_pred["taxes"] = [{"rate": 20, "value": 0.0, "confidence": 0.5}] receipt = Receipt(receipt_pred) assert receipt.checklist["taxes_match_total_incl"] is False def test__taxes_match_total_incl_4(receipt_pred): # sanity check with None tax rate - receipt_pred["total_incl"] = {"value": 507.25, "probability": 0.6} - receipt_pred["taxes"] = [{"rate": "N/A", "value": 0.0, "probability": 0.5}] + receipt_pred["total_incl"] = {"value": 507.25, "confidence": 0.6} + receipt_pred["taxes"] = [{"rate": "N/A", "value": 0.0, "confidence": 0.5}] receipt = Receipt(receipt_pred) assert receipt.checklist["taxes_match_total_incl"] is False assert type(str(receipt.taxes[0])) is str diff --git a/tests/fields/test_amount.py b/tests/fields/test_amount.py index 23050937..0d0b9dc5 100644 --- a/tests/fields/test_amount.py +++ b/tests/fields/test_amount.py @@ -4,7 +4,7 @@ def test_constructor(): field_dict = { "amount": "2", - "probability": 0.1, + "confidence": 0.1, "segmentation": { "bounding_box": [ [0.016, 0.707], @@ -20,6 +20,6 @@ def test_constructor(): def test_constructor_no_amount(): - field_dict = {"amount": "N/A", "probability": 0.1} + field_dict = {"amount": "N/A", "confidence": 0.1} amount = Amount(field_dict) assert amount.value is None diff --git a/tests/fields/test_date.py b/tests/fields/test_date.py index 2da99363..17e3940b 100644 --- a/tests/fields/test_date.py +++ b/tests/fields/test_date.py @@ -5,7 +5,7 @@ def test_constructor(): field_dict = { "iso": "2018-04-01", - "probability": 0.1, + "confidence": 0.1, "segmentation": { "bounding_box": [ [0.016, 0.707], @@ -20,6 +20,6 @@ def test_constructor(): def test_constructor_no_date(): - field_dict = {"iso": "N/A", "probability": 0.1} + field_dict = {"iso": "N/A", "confidence": 0.1} date = Date(field_dict) assert date.value is None diff --git a/tests/fields/test_field.py b/tests/fields/test_field.py index 45bd6d97..dbf3e5d2 100644 --- a/tests/fields/test_field.py +++ b/tests/fields/test_field.py @@ -4,7 +4,7 @@ def test_constructor(): field_dict = { "value": "test", - "probability": 0.1, + "confidence": 0.1, "polygon": [[0.016, 0.707], [0.414, 0.707], [0.414, 0.831], [0.016, 0.831]], } field = Field(field_dict) @@ -14,14 +14,14 @@ def test_constructor(): def test_constructor_no_segmentation(): - field_dict = {"value": "test", "probability": 0.1} + field_dict = {"value": "test", "confidence": 0.1} field = Field(field_dict) assert len(field.bbox) == 0 def test_equality(): - field_dict_1 = {"value": "test", "probability": 0.1} - field_dict_2 = {"value": "other", "probability": 0.1} + field_dict_1 = {"value": "test", "confidence": 0.1} + field_dict_2 = {"value": "other", "confidence": 0.1} field_1 = Field(field_dict_1) field_2 = Field(field_dict_2) assert field_1 == field_1 @@ -29,7 +29,7 @@ def test_equality(): def test_constructor_na(): - field_dict = {"value": "N/A", "probability": 0.1} + field_dict = {"value": "N/A", "confidence": 0.1} field = Field(field_dict) assert field.value is None @@ -44,25 +44,25 @@ def test_no_probability(): def test_array_probability(): fields = [ - Field({"value": None, "probability": 0.1}), - Field({"value": None, "probability": 0.8}), + Field({"value": None, "confidence": 0.1}), + Field({"value": None, "confidence": 0.8}), ] assert Field.array_probability(fields) == 0.8 * 0.1 fields = [ - Field({"value": None, "probability": 0.1}), - Field({"value": None, "probability": None}), + Field({"value": None, "confidence": 0.1}), + Field({"value": None, "confidence": None}), ] assert Field.array_probability(fields) == 0.0 def test_array_sum(): fields = [ - Field({"value": 1, "probability": 0.1}), - Field({"value": 2, "probability": 0.8}), + Field({"value": 1, "confidence": 0.1}), + Field({"value": 2, "confidence": 0.8}), ] assert Field.array_sum(fields) == 3 fields = [ - Field({"value": None, "probability": 0.1}), - Field({"value": 4, "probability": 0.8}), + Field({"value": None, "confidence": 0.1}), + Field({"value": 4, "confidence": 0.8}), ] assert Field.array_sum(fields) == 0.0 diff --git a/tests/fields/test_locale.py b/tests/fields/test_locale.py index 150cda78..7fbbf13c 100644 --- a/tests/fields/test_locale.py +++ b/tests/fields/test_locale.py @@ -7,7 +7,7 @@ def test_constructor(): "language": "en", "country": "uk", "currency": "GBP", - "probability": 0.1, + "confidence": 0.1, } locale = Locale(field_dict) assert locale.value == "en-EN" @@ -17,7 +17,7 @@ def test_constructor(): def test_constructor_almost_empty_field(): - field_dict = {"value": "en-EN", "probability": 0.1} + field_dict = {"value": "en-EN", "confidence": 0.1} locale = Locale(field_dict) assert locale.language is None assert locale.country is None diff --git a/tests/fields/test_orientation.py b/tests/fields/test_orientation.py index 19118da1..86d52156 100644 --- a/tests/fields/test_orientation.py +++ b/tests/fields/test_orientation.py @@ -4,7 +4,7 @@ def test_constructor(): field_dict = { "degrees": 90, - "probability": 0.1, + "confidence": 0.1, } orientation = Orientation(field_dict) assert orientation.value == 90 @@ -13,7 +13,7 @@ def test_constructor(): def test_not_number(): field_dict = { "degrees": "aze", - "probability": 0.1, + "confidence": 0.1, } orientation = Orientation(field_dict) assert orientation.value == 0 @@ -22,7 +22,7 @@ def test_not_number(): def test_not_90(): field_dict = { "degrees": 255, - "probability": 0.1, + "confidence": 0.1, } orientation = Orientation(field_dict) assert orientation.value == 0 diff --git a/tests/fields/test_payment_details.py b/tests/fields/test_payment_details.py index 5c42ba60..df6dedb0 100644 --- a/tests/fields/test_payment_details.py +++ b/tests/fields/test_payment_details.py @@ -7,7 +7,7 @@ def test_constructor(): "iban": "iban", "routing_number": "routing_number", "swift": "swift", - "probability": 0.1, + "confidence": 0.1, "segmentation": { "bounding_box": [ [0.016, 0.707], @@ -31,7 +31,7 @@ def test_constructor_all_na(): "iban": "N/A", "routing_number": "N/A", "swift": "N/A", - "probability": 0.1, + "confidence": 0.1, "segmentation": { "bounding_box": [ [0.016, 0.707], @@ -54,7 +54,7 @@ def test_constructor_all_none(): "iban": {}, "routing_number": {}, "swift": {}, - "probability": 0.1, + "confidence": 0.1, "segmentation": { "bounding_box": [ [0.016, 0.707], diff --git a/tests/fields/test_tax.py b/tests/fields/test_tax.py index a043ca5d..ccdaff38 100644 --- a/tests/fields/test_tax.py +++ b/tests/fields/test_tax.py @@ -6,7 +6,7 @@ def test_constructor(): "value": "2", "rate": 0.2, "code": "QST", - "probability": 0.1, + "confidence": 0.1, "polygon": [[0.016, 0.707], [0.414, 0.707], [0.414, 0.831], [0.016, 0.831]], } tax = Tax(field_dict, value_key="value") @@ -18,14 +18,14 @@ def test_constructor(): def test_constructor_no_rate(): - field_dict = {"value": "2", "rate": "AA", "probability": 0.1} + field_dict = {"value": "2", "rate": "AA", "confidence": 0.1} tax = Tax(field_dict) assert tax.rate is None assert len(tax.bbox) == 0 def test_constructor_no_amount(): - field_dict = {"value": "NA", "rate": "AA", "code": "N/A", "probability": 0.1} + field_dict = {"value": "NA", "rate": "AA", "code": "N/A", "confidence": 0.1} tax = Tax(field_dict) assert tax.value is None assert type(str(tax)) == str