HoDA_Verifier/core/verifier.py
2025-07-10 19:21:41 +02:00

83 lines
3.3 KiB
Python

import json, csv
from utils.paths import resource_path
class Patient:
def __init__(self, json_data : dict):
self.json_data = json_data
def get_value(self, key):
return self.json_data.get(key, None)
def compare_value(self, key, value):
return self.get_value(key) == value
def __str__(self):
return f"Patient({self.json_data})"
class Patients:
def __init__(self, patients_data : list[dict]):
self.patients = [Patient(data) for data in patients_data]
def get_patients(self):
return self.patients
def trouver(self, to_find):
for patient in self.patients:
if patient.get_value("Numéro de correspondance") == to_find.get_value("Numéro de correspondance"):
return patient
return None
class Verifier:
def __init__(self):
try:
truth_file_path = resource_path("data/truth.json")
with open(truth_file_path, 'r', encoding='utf-8') as file:
truth_data = json.load(file)
self.truth = Patients(truth_data["patients"])
except Exception:
self.truth = Patients([])
def verify(self, patients_csv: str):
# Read CSV file and convert to list of dictionaries
patients_data = []
try:
with open(patients_csv, 'r', encoding='utf-8') as file:
csv_reader = csv.reader(file, delimiter=';')
rows = list(csv_reader)
if not rows:
return ["📄 Le fichier CSV est vide."]
# First row contains column names
headers = rows[0]
# Convert each subsequent row to a dictionary
for row in rows[1:]:
patient_dict = {headers[i]: row[i] for i in range(len(headers))}
patients_data.append(patient_dict)
except FileNotFoundError:
return [f"📁 Erreur: Le fichier {patients_csv} n'a pas été trouvé."]
except Exception as e:
return [f"💥 Erreur lors de la lecture du fichier CSV: {e}."]
self.patients = Patients(patients_data)
# Check if we have truth data
if not self.truth.get_patients():
return ["❌ Erreur: Aucune donnée de vérité n'a pu être chargée."]
key_to_test = self.truth.get_patients()[0].json_data.keys()
issues = []
for patient in self.patients.get_patients():
issue_founded = False
truth_patient = self.truth.trouver(patient)
if truth_patient:
for key in key_to_test:
if not patient.compare_value(key, truth_patient.get_value(key)):
issue_founded = True
issues.append(f"⚠️ Le patient n°{patient.get_value('Numéro de correspondance')} a une discordance avec {key}. Attendu : {truth_patient.get_value(key)}, Trouvé : {patient.get_value(key)}.")
else:
issue_founded = True
issues.append(f"❌ Le patient n°{patient.get_value('Numéro de correspondance')} n'a pas été trouvé dans les données de vérité.")
if issue_founded:
issues.append("")
return issues