def compare_first_four_keys(expected_dict, predicted_dict):
#Compare the first 4 key-value pairs to see if the dictionaries match,
#including checking the first 3 values if the value is a list.
expected_items = list(expected_dict.items())[:3] # Get the first 4 key-value pairs
for key, expected_value in expected_items:
if key in predicted_dict:
predicted_value = predicted_dict[key]
# If both values are lists, compare the first 3 elements
if isinstance(expected_value, list) and isinstance(predicted_value, list):
if len(expected_value) >= 3 and len(predicted_value) >= 3:
if all(pytest.approx(ev) == pv for ev, pv in zip(expected_value, predicted_value)):
return True
return False
# Sample function that generates the output dictionaries (replace with your function)
def get_output_dict(Table_Category):
# Example function output based on Table_Category
if Table_Category == 'SLP':
return {'key1': 'value1', 'key2': 'value2', ...} # Replace with actual dictionary
elif Table_Category == 'XYZ':
return {'keyA': 'valueA', 'keyB': 'valueB', ...}
return {}
def dict_length_check(expected_dict):
if expected_dict['Table Category'] == 'RLM' and len(expected_dict) == 46:
print("Length of this RLM table matches")
return True
elif expected_dict['Table Category'] == 'SLP' and len(expected_dict) == 24:
print("Length of this SLP table matches")
return True
elif expected_dict['Table Category'] == 'Metering' and len(expected_dict) == 17:
print("Length of this Metering table matches")
return True
#test with parametrize for two arguments: pdf_name and expected_pdf
@pytest.mark.parametrize("pdf_name, expected_pdf", [
("Actual-Avacon_2024", "Actual-Avacon_2024"),
("ACTUAL_Erdgas Mittelsachsen GmbH_2024", "Mittelsachsen"),("Actual-bnNETZE_2024","bnNetze"),
("Preliminar_Gemeindewerke Peiner Land GmbH_2024","Preliminar_Gemeindewerke")])
def test_actual_avacon(pdf_name,expected_pdf):
accuracies = []
pdf_file_path = os.path.join(current_dir, '..', '..', 'docs', 'RTE', pdf_name + ".pdf")
with open (pdf_file_path,'rb') as file:
response = client.post('/rte_from_file', files= {'file': file})
print("GPT Response received")
assert response.status_code == 200
predicted_json = response.json()
expected_output = expected_op_json['expected_output'][expected_pdf]
for i in range (len(predicted_json['extracted_keyword_list'])):
expected_dict = expected_output[i]
predicted_dict = predicted_json['extracted_keyword_list'][i]
if compare_first_four_keys(expected_dict,predicted_dict) and dict_length_check(expected_dict):
total_elements = 0
matched_keys = 0
# Iterate through the expected dictionary
for key in list(expected_dict.keys()):
if key in predicted_dict:
total_elements += 1
expected_value = expected_dict[key]
predicted_value = predicted_dict[key]
# Handle list comparisons
if isinstance(expected_value, list) and isinstance(predicted_value, list):
if len(expected_value) == len(predicted_value):
# Use pytest.approx for numerical lists
if all(isinstance(i, (int, float)) for i in expected_value):
if all(pytest.approx(ev) == pv for ev, pv in zip(expected_value, predicted_value)):
matched_keys += 1
# Direct equality for non-numeric lists
elif expected_value == predicted_value:
matched_keys += 1
# Use pytest.approx for single numeric values
elif isinstance(expected_value, (int, float)) and isinstance(predicted_value, (int, float)):
if pytest.approx(expected_value) == predicted_value:
matched_keys += 1
# Direct equality for non-numeric values
elif expected_value == predicted_value:
matched_keys += 1
accuracy = (matched_keys/total_elements) * 100 if total_elements else 0
accuracies.append(accuracy)
print(f"Dictionary {i+1}: Accuracy is {accuracy:.2f}% with {matched_keys}/{total_elements} elements matched.")
print(f"Dictionary {i+1} has Accuracy: {accuracy:.2f}%.")
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter