def compute_precision(invoice, ground_truth): """ :param invoice: Invoice object to compare :param ground_truth: Ground truth Invoice object :return: Precision metrics """ precisions = { "__pre__total_incl": Benchmark.scalar_precision_score(invoice.total_incl, ground_truth.total_incl), "__pre__total_excl": Benchmark.scalar_precision_score(invoice.total_excl, ground_truth.total_excl), "__pre__invoice_date": Benchmark.scalar_precision_score(invoice.invoice_date, ground_truth.invoice_date), "__pre__invoice_number": Benchmark.scalar_precision_score(invoice.invoice_number, ground_truth.invoice_number), "__pre__due_date": Benchmark.scalar_precision_score(invoice.due_date, ground_truth.due_date), "__pre__total_tax": Benchmark.scalar_precision_score(invoice.total_tax, ground_truth.total_tax) } if len(invoice.taxes) == 0: precisions["__pre__taxes"] = None else: precisions["__pre__taxes"] = Tax.compare_arrays( invoice.taxes, ground_truth.taxes) return precisions
def test_constructor_no_amount(): field_dict = { 'value': "NA", 'rate': "AA", 'code': "N/A", 'probability': 0.1 } tax = Tax(field_dict) assert tax.value is None assert type(str(tax)) == str
def compute_accuracy(receipt, ground_truth): """ :param receipt: Receipt object to compare :param ground_truth: Ground truth Receipt object :return: Accuracy metrics """ return { "__acc__total_incl": ground_truth.total_incl == receipt.total_incl, "__acc__total_excl": ground_truth.total_excl == receipt.total_excl, "__acc__receipt_date": ground_truth.date == receipt.date, "__acc__total_tax": ground_truth.total_tax == receipt.total_tax, "__acc__taxes": Tax.compare_arrays(receipt.taxes, ground_truth.taxes), }
def test_constructor(): field_dict = { 'value': "2", 'rate': 0.2, 'code': "QST", 'probability': 0.1, 'segmentation': { "bounding_box": [[0.016, 0.707], [0.414, 0.707], [0.414, 0.831], [0.016, 0.831]] } } tax = Tax(field_dict, value_key="value") assert tax.value == 2 assert tax.probability == 0.1 assert tax.rate == 0.2 assert len(tax.bbox) > 0 assert type(str(tax)) == str
def compute_accuracy(invoice, ground_truth): """ :param invoice: Invoice object to compare :param ground_truth: Ground truth Invoice object :return: Accuracy metrics """ return { "__acc__total_incl": ground_truth.total_incl == invoice.total_incl, "__acc__total_excl": ground_truth.total_excl == invoice.total_excl, "__acc__invoice_date": ground_truth.invoice_date == invoice.invoice_date, "__acc__invoice_number": ground_truth.invoice_number == invoice.invoice_number, "__acc__due_date": ground_truth.due_date == invoice.due_date, "__acc__total_tax": ground_truth.total_tax == invoice.total_tax, "__acc__taxes": Tax.compare_arrays(invoice.taxes, ground_truth.taxes), }
def build_from_api_prediction(self, api_prediction, page_n=0): """ :param api_prediction: Raw prediction from HTTP response :param page_n: Page number for multi pages pdf input :return: (void) set the object attributes with api prediction values """ self.locale = Locale(api_prediction["locale"], page_n=page_n) self.total_incl = Amount(api_prediction["total_incl"], value_key="value", page_n=page_n) self.date = Date(api_prediction["date"], value_key="value", page_n=page_n) self.category = Field(api_prediction["category"], page_n=page_n) self.merchant_name = Field(api_prediction["supplier"], value_key="value", page_n=page_n) self.time = Field(api_prediction["time"], value_key="value", page_n=page_n) self.taxes = [ Tax(tax_prediction, page_n=page_n, value_key="value", rate_key="rate", code_key="code") for tax_prediction in api_prediction["taxes"]] self.orientation = Orientation(api_prediction["orientation"], page_n=page_n) self.total_tax = Amount({"value": None, "probability": 0.}, value_key="value", page_n=page_n) self.total_excl = Amount({"value": None, "probability": 0.}, value_key="value", page_n=page_n)
def build_from_api_prediction(self, api_prediction, page_n=0): """ :param api_prediction: Raw prediction from HTTP response :param page_n: Page number for multi pages pdf input :return: (void) set the object attributes with api prediction values """ self.company_number = [ Field(company_reg, extra_fields={"type"}, page_n=page_n) for company_reg in api_prediction["company_registration"] ] self.invoice_date = Date(api_prediction["date"], value_key="value", page_n=page_n) self.due_date = Date(api_prediction["due_date"], value_key="value", page_n=page_n) self.invoice_number = Field(api_prediction["invoice_number"], page_n=page_n) self.locale = Locale(api_prediction["locale"], value_key="language", page_n=page_n) self.orientation = Orientation(api_prediction["orientation"], page_n=page_n) self.supplier = Field(api_prediction["supplier"], page_n=page_n) self.taxes = [ Tax(tax_prediction, page_n=page_n, value_key="value") for tax_prediction in api_prediction["taxes"] ] self.payment_details = [ PaymentDetails(payment_detail, page_n=page_n) for payment_detail in api_prediction["payment_details"] ] self.total_incl = Amount(api_prediction["total_incl"], value_key="value", page_n=page_n) self.total_excl = Amount(api_prediction["total_excl"], value_key="value", page_n=page_n) self.total_tax = Amount({ "value": None, "probability": 0. }, value_key="value", page_n=page_n)
def compute_precision(receipt, ground_truth): """ :param receipt: Receipt object to compare :param ground_truth: Ground truth Receipt object :return: Precision metrics """ precisions = { "__pre__total_incl": Benchmark.scalar_precision_score( receipt.total_incl, ground_truth.total_incl), "__pre__total_excl": Benchmark.scalar_precision_score( receipt.total_excl, ground_truth.total_excl), "__pre__receipt_date": Benchmark.scalar_precision_score( receipt.date, ground_truth.date), "__pre__total_tax": Benchmark.scalar_precision_score( receipt.total_tax, ground_truth.total_tax)} if len(receipt.taxes) == 0: precisions["__pre__taxes"] = None else: precisions["__pre__taxes"] = Tax.compare_arrays(receipt.taxes, ground_truth.taxes) return precisions
def compute_accuracy(financial_document, ground_truth): """ :param financial_document: FinancialDocument object to compare :param ground_truth: Ground truth FinancialDocument object :return: Accuracy metrics """ return { "__acc__total_incl": ground_truth.total_incl == financial_document.total_incl, "__acc__total_excl": ground_truth.total_excl == financial_document.total_excl, "__acc__invoice_date": ground_truth.date == financial_document.date, "__acc__invoice_number": ground_truth.invoice_number == financial_document.invoice_number, "__acc__due_date": ground_truth.due_date == financial_document.due_date, "__acc__total_tax": ground_truth.total_tax == financial_document.total_tax, "__acc__taxes": Tax.compare_arrays(financial_document.taxes, ground_truth.taxes), }
def test_constructor_no_rate(): field_dict = {'value': "2", 'rate': "AA", 'probability': 0.1} tax = Tax(field_dict) assert tax.rate is None assert len(tax.bbox) == 0
def __init__(self, api_prediction=None, input_file=None, locale=None, total_incl=None, total_excl=None, invoice_date=None, invoice_number=None, due_date=None, taxes=None, supplier=None, payment_details=None, company_number=None, vat_number=None, orientation=None, total_tax=None, page_n=0): """ :param api_prediction: Raw prediction from HTTP response :param input_file: Input object :param locale: locale value for creating Invoice object from scratch :param total_incl: total_incl value for creating Invoice object from scratch :param total_excl: total_excl value for creating Invoice object from scratch :param invoice_date: invoice_date value for creating Invoice object from scratch :param invoice_number: invoice_number value for creating Invoice object from scratch :param due_date: due_date value for creating Invoice object from scratch :param taxes: taxes value for creating Invoice object from scratch :param supplier: supplier value for creating Invoice object from scratch :param payment_details: payment_details value for creating Invoice object from scratch :param company_number: company_number value for creating Invoice object from scratch :param vat_number: vat_number value for creating Invoice object from scratch :param orientation: orientation value for creating Invoice object from scratch :param total_tax: total_tax value for creating Invoice object from scratch :param page_n: Page number for multi pages pdf input """ self.type = "Invoice" self.locale = None self.total_incl = None self.total_excl = None self.invoice_date = None self.invoice_number = None self.due_date = None self.taxes = [] self.supplier = None self.payment_details = None self.company_number = None self.orientation = None self.total_tax = None if api_prediction is not None: self.build_from_api_prediction(api_prediction, page_n=page_n) else: self.locale = Locale({"value": locale}, value_key="value", page_n=page_n) 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) self.due_date = Date({"value": due_date}, value_key="value", page_n=page_n) self.supplier = Field({"value": supplier}, value_key="value", page_n=page_n) if taxes is not None: self.taxes = [ Tax({ "value": t[0], "rate": t[1] }, page_n=page_n, value_key="value", rate_key="rate") for t in taxes ] self.orientation = Orientation({"value": orientation}, value_key="value", page_n=page_n) self.total_tax = Amount({"value": total_tax}, value_key="value", page_n=page_n) self.total_excl = Amount({"value": total_excl}, value_key="value", page_n=page_n) self.invoice_number = Field({"value": invoice_number}, value_key="value", page_n=page_n) self.payment_details = Field({"value": payment_details}, value_key="value", page_n=page_n) self.company_number = Field({"value": company_number}, value_key="value", page_n=page_n) # Invoke Document constructor super(Invoice, self).__init__(input_file) # Run checks self._checklist() # Reconstruct extra fields self._reconstruct()
def __init__( self, api_prediction=None, input_file=None, locale=None, total_incl=None, date=None, category=None, merchant_name=None, time=None, taxes=None, orientation=None, total_tax=None, total_excl=None, page_n=0 ): """ :param api_prediction: Raw prediction from HTTP response :param input_file: Input object :param locale: locale value for creating Receipt object from scratch :param total_incl: total_incl value for creating Receipt object from scratch :param date: date value for creating Receipt object from scratch :param category: category value for creating Receipt object from scratch :param merchant_name: merchant_name value for creating Receipt object from scratch :param time: time value for creating Receipt object from scratch :param taxes: taxes value for creating Receipt object from scratch :param orientation: orientation value for creating Receipt object from scratch :param total_tax: total_tax value for creating Receipt object from scratch :param total_excl: total_excl value for creating Receipt object from scratch :param page_n: Page number for multi pages pdf input """ self.type = "Receipt" self.locale = None self.total_incl = None self.date = None self.category = None self.merchant_name = None self.time = None self.taxes = [] self.orientation = None self.total_tax = None self.total_excl = None if api_prediction is not None: self.build_from_api_prediction(api_prediction, page_n=page_n) else: self.locale = Locale({"value": locale}, value_key="value", page_n=page_n) self.total_incl = Amount({"value": total_incl}, value_key="value", page_n=page_n) self.date = Date({"value": date}, value_key="value", page_n=page_n) self.category = Field({"value": category}, value_key="value", page_n=page_n) self.merchant_name = Field({"value": merchant_name}, value_key="value", page_n=page_n) self.time = Field({"value": time}, value_key="value", page_n=page_n) if taxes is not None: self.taxes = [ Tax({"value": t[0], "rate": t[1]}, page_n=page_n, value_key="value", rate_key="rate") for t in taxes] self.orientation = Orientation({"value": orientation}, value_key="value", page_n=page_n) self.total_tax = Amount({"value": total_tax}, value_key="value", page_n=page_n) self.total_excl = Amount({"value": total_excl}, value_key="value", page_n=page_n) # Invoke Document constructor super(Receipt, self).__init__(input_file) # Run checks self._checklist() # Reconstruct extra fields self._reconstruct()