From 55fea23ac4cb5c9a4d7e8ab6293bc95e94174b9b Mon Sep 17 00:00:00 2001
From: Djalim Simaila <DjalimS.pro@outlook.fr>
Date: Mon, 17 Apr 2023 16:03:50 +0200
Subject: [PATCH] added test that show how different file are from the expected
 dataset

---
 .../Cylindre_Delta 1,0_analyse brute.txt      |  2 +-
 .../Cylindre_Delta 1,0_analyse rayon.txt      |  2 +-
 .../30-03-2023_Delta 1,0_analyse brute.txt    |  2 +-
 .../30-03-2023_Delta 1,0_analyse rayon.txt    |  2 +-
 integration_tests/data_test.py                | 70 +++++++++++++++++--
 test.py                                       | 50 +++++++++++++
 6 files changed, 117 insertions(+), 11 deletions(-)
 create mode 100644 test.py

diff --git a/Datasets/Cylindre/Cylindre_Delta 1,0_analyse brute.txt b/Datasets/Cylindre/Cylindre_Delta 1,0_analyse brute.txt
index 5c14c54..a19ee3e 100644
--- a/Datasets/Cylindre/Cylindre_Delta 1,0_analyse brute.txt	
+++ b/Datasets/Cylindre/Cylindre_Delta 1,0_analyse brute.txt	
@@ -1,4 +1,4 @@
-X (mm) 	 Y (mm) 	 Z (mm) 	 téta (radian) 	 rayon (mm) 	 (xi-xmoy) 	 (yi-ymoy) 
+X (mm) 	 Y (mm) 	 Z (mm) 	 teta (radian) 	 rayon (mm) 	 (xi-xmoy) 	 (yi-ymoy) 
 -4,898874	34,334751	0,000000	-0,681820	30,756656	-19,383064	23,880300
 -4,899164	34,141937	0,001082	-0,685797	30,607375	-19,383354	23,687486
 -3,789294	35,196819	0,001400	-0,636133	30,758820	-18,273484	24,742368
diff --git a/Datasets/Cylindre/Cylindre_Delta 1,0_analyse rayon.txt b/Datasets/Cylindre/Cylindre_Delta 1,0_analyse rayon.txt
index 89b39fe..187e334 100644
--- a/Datasets/Cylindre/Cylindre_Delta 1,0_analyse rayon.txt	
+++ b/Datasets/Cylindre/Cylindre_Delta 1,0_analyse rayon.txt	
@@ -1,4 +1,4 @@
-Xmoy (mm) 	 Ymoy (mm) 	 Zmoy (mm) 	 rayon moyen (mm) 	 rayon écart type (mm) 
+Xmoy (mm) 	 Ymoy (mm) 	 Zmoy (mm) 	 rayon moyen (mm) 	 rayon ecart type (mm) 
 14,484190	10,454451	0,480723	21,971957	7,737398
 15,253762	8,435195	1,492448	32,757553	1,243314
 14,638315	9,688970	2,496679	32,908071	0,251908
diff --git a/Datasets/Echantillon/30-03-2023_Delta 1,0_analyse brute.txt b/Datasets/Echantillon/30-03-2023_Delta 1,0_analyse brute.txt
index 3bfb9ad..d278c88 100644
--- a/Datasets/Echantillon/30-03-2023_Delta 1,0_analyse brute.txt	
+++ b/Datasets/Echantillon/30-03-2023_Delta 1,0_analyse brute.txt	
@@ -1,4 +1,4 @@
-X (mm) 	 Y (mm) 	 Z (mm) 	 téta (radian) 	 rayon (mm) 	 (xi-xmoy) 	 (yi-ymoy) 
+X (mm) 	 Y (mm) 	 Z (mm) 	 teta (radian) 	 rayon (mm) 	 (xi-xmoy) 	 (yi-ymoy) 
 10,210823	7,103537	0,000000	-0,556636	1,448117	0,765088	-1,229506
 10,184198	7,103548	0,000030	-0,540878	1,434219	0,738463	-1,229495
 10,185973	7,121432	0,000572	-0,548435	1,419842	0,740238	-1,211611
diff --git a/Datasets/Echantillon/30-03-2023_Delta 1,0_analyse rayon.txt b/Datasets/Echantillon/30-03-2023_Delta 1,0_analyse rayon.txt
index ef17d70..a627a6a 100644
--- a/Datasets/Echantillon/30-03-2023_Delta 1,0_analyse rayon.txt	
+++ b/Datasets/Echantillon/30-03-2023_Delta 1,0_analyse rayon.txt	
@@ -1,4 +1,4 @@
-Xmoy (mm) 	 Ymoy (mm) 	 Zmoy (mm) 	 rayon moyen (mm) 	 rayon écart type (mm) 
+Xmoy (mm) 	 Ymoy (mm) 	 Zmoy (mm) 	 rayon moyen (mm) 	 rayon ecart type (mm) 
 9,445735	8,333043	0,520592	2,285259	0,887739
 8,214791	10,014653	1,447270	3,078851	1,137282
 8,364474	9,582373	2,514221	3,834010	0,231461
diff --git a/integration_tests/data_test.py b/integration_tests/data_test.py
index 1bcafcb..ef2889d 100644
--- a/integration_tests/data_test.py
+++ b/integration_tests/data_test.py
@@ -6,6 +6,7 @@ def check_discrete_data(expected_file, actual_file, ndigits=7,eps=0.00001) :
     minimal_output_data = ""
     output_data = ""
     expected = []
+    x,y,z,r,std = 0,0,0,0,0
     with open(expected_file, "r") as f:
         expected = f.readlines()[1:]
     expected = [line.replace(',','.').split('\t') for line in expected]
@@ -21,6 +22,13 @@ def check_discrete_data(expected_file, actual_file, ndigits=7,eps=0.00001) :
         z_diff = round(abs(expected[i][2] - actual[i][2]),ndigits)
         r_diff = round(abs(expected[i][3] - actual[i][3]),ndigits)
         std_diff = round(abs(expected[i][4] - actual[i][4]),ndigits)
+
+        x += x_diff
+        y += y_diff
+        z += z_diff
+        r += r_diff
+        std += std_diff
+
         line = f"{str(i).rjust(4)}:\t X: {str(x_diff).rjust(8)}\t Y: {str(y_diff).rjust(8)}\t Z: {str(z_diff).rjust(8)}\t R: {str(r_diff).rjust(8)}\t STD: {str(std_diff).rjust(8)}"
         output_data += line + "\n"
         if x_diff > eps or y_diff > eps or z_diff > eps or r_diff > eps or std_diff > eps:
@@ -28,12 +36,30 @@ def check_discrete_data(expected_file, actual_file, ndigits=7,eps=0.00001) :
     output.save_output_file(output_file, output_data)
     output.save_output_file(minimal_output_file, minimal_output_data)
 
+    x = round(x/len(expected),ndigits)
+    y = round(y/len(expected),ndigits)
+    z = round(z/len(expected),ndigits)
+    r = round(r/len(expected),ndigits)
+    std = round(std/len(expected),ndigits)
+    print()
+    print("Analyse données discretisées:")
+    print(f"difference moyenne X: {x}")
+    print(f"difference moyenne Y: {y}")
+    print(f"difference moyenne Z: {z}")
+    print(f"difference moyenne R: {r}")
+    print(f"difference moyenne STD: {std}")
+    print(f"diff globale des fichiers: {(x+y+z+r+std)/5}")
+    print(f"Voir {output_file} pour plus de détails")
+    print(f"Voir {minimal_output_file} pour les différences significatives")
+    print("_"*80)
+
 def check_raw_data(expected_file, actual_file, ndigits=7,eps=0.00001) :
     output_file = "check_raw_data_full.txt"
     minimal_output_file = "check_raw_data_minimal.txt"
     minimal_output_data = ""
     output_data = ""
     expected = []
+    x,y,z,t,r,xmoy,ymoy = 0,0,0,0,0,0,0
     with open(expected_file, "r") as f:
         expected = f.readlines()[1:]
     expected = [line.replace(',','.').split('\t') for line in expected]
@@ -51,21 +77,51 @@ def check_raw_data(expected_file, actual_file, ndigits=7,eps=0.00001) :
         r_diff = round(abs(expected[i][4] - actual[i][4]),ndigits)
         xmoy_diff = round(abs(expected[i][5] - actual[i][5]),ndigits)
         ymoy_diff = round(abs(expected[i][6] - actual[i][6]),ndigits)
+
+        x += x_diff
+        y += y_diff
+        z += z_diff     
+        t += t_diff
+        r += r_diff
+        xmoy += xmoy_diff
+        ymoy += ymoy_diff
+
         line = f"{str(i).rjust(4)}:\t X: {str(x_diff).rjust(8)}\t Y: {str(y_diff).rjust(8)}\t Z: {str(z_diff).rjust(8)}\t T: {str(t_diff).rjust(8)}\t R: {str(r_diff).rjust(8)}\t Xmoy: {str(xmoy_diff).rjust(8)}\t Ymoy: {str(ymoy_diff).rjust(8)}"
         output_data += line + "\n"
         if x_diff > eps:
-            minimal_output_data += f"{i} : {x_diff}\n"
+            minimal_output_data += f"{i} : X diff {x_diff}\n"
         if y_diff > eps:
-            minimal_output_data += f"{i} : {y_diff}\n"
+            minimal_output_data += f"{i} : Y diff {y_diff}\n"
         if z_diff > eps:
-            minimal_output_data += f"{i} : {z_diff}\n"
+            minimal_output_data += f"{i} : Z diff {z_diff}\n"
         if t_diff > eps:
-            minimal_output_data += f"{i} : {t_diff}\n"
+            minimal_output_data += f"{i} : teta diff{ t_diff}\n"
         if r_diff > eps:
-            minimal_output_data += f"{i} : {r_diff}\n"
+            minimal_output_data += f"{i} : R diff {r_diff}\n"
         if xmoy_diff > eps:
-            minimal_output_data += f"{i} : {xmoy_diff}\n"
+            minimal_output_data += f"{i} : Xi-Xmoy diff {xmoy_diff}\n"
         if ymoy_diff > eps:
-            minimal_output_data += f"{i} : {ymoy_diff}\n"
+            minimal_output_data += f"{i} : Yi-Ymoy diff{ymoy_diff}\n"
     output.save_output_file(output_file, output_data)
     output.save_output_file(minimal_output_file, minimal_output_data)
+    x = round(x/len(expected),ndigits)
+    y = round(y/len(expected),ndigits)
+    z = round(z/len(expected),ndigits)
+    t = round(t/len(expected),ndigits)  
+    r = round(r/len(expected),ndigits)     
+    xmoy = round(xmoy/len(expected),ndigits)
+    ymoy = round(ymoy/len(expected),ndigits)
+    
+    print()
+    print("Analyse données brutes :") 
+    print(f"diff moyenne de x : {x}")
+    print(f"diff moyenne de y : {y}")
+    print(f"diff moyenne de z : {z}")
+    print(f"diff moyenne de t : {t}")
+    print(f"diff moyenne de r : {r}")
+    print(f"diff moyenne de xmoy : {xmoy}")
+    print(f"diff moyenne de ymoy : {ymoy}")
+    print(f"diff gloabale des fichiers : {(x+y+z+t+r+xmoy+ymoy)/7}")
+    print(f"Voir {output_file} pour plus de détails")
+    print(f"Voir {minimal_output_file} pour les différences significatives")
+    print("_"*80)
diff --git a/test.py b/test.py
new file mode 100644
index 0000000..9c93d1b
--- /dev/null
+++ b/test.py
@@ -0,0 +1,50 @@
+from main import get_raw_data, get_discrete_data
+from utils.files.file_data import Object
+from utils.files.output import save_output_file, format_data
+from utils.files.parsers import parse_obj_file
+from integration_tests import data_test
+import time
+
+import os
+
+def test_get_raw_data(obj_path:Object,expected_file:str, ndigits:int = 6, eps:float = 0.0001):
+    """
+    Test the get_raw_data function
+    """
+    obj = parse_obj_file(obj_path,normalised='z')
+
+    # Calculate raw data and save it in a file
+    now = time.time()
+    data = get_raw_data(obj, ndigits)
+    print()
+    print("Time to calculate raw data: ", time.time() - now)
+    save_output_file('tmp_analyse_brute.txt', format_data(data, '\t', ["X (en mm)", "Y (en mm)", "Z (en mm)", "teta (en rad)", "rayon (en mm)","Xi-Xmoy","Yi-Ymoy"] ))
+    data_test.check_raw_data(expected_file, 'tmp_analyse_brute.txt', ndigits, eps)
+    os.remove('tmp_analyse_brute.txt')
+
+def test_get_discrete_data(obj_path:Object,expected_file:str, ndigits:int = 6, eps:float = 0.0001):
+    """
+    Test the get_discrete_data function
+    """
+    obj = parse_obj_file(obj_path,normalised='z')
+
+    # Calculate discrete data and save it in a file
+    now = time.time()
+    data = get_discrete_data(obj, ndigits)
+    print()
+    print("Time to calculate discrete data: ", time.time() - now)
+    save_output_file('tmp_analyse_discrete.txt', format_data(data, '\t', ["X moy (en mm)", "Y moy (en mm)", "Z moy (en mm)","Rayon moyen (en mm)","Rayon ecart type (en mm)"] ))
+    data_test.check_discrete_data(expected_file, 'tmp_analyse_discrete.txt', ndigits, eps)
+    os.remove('tmp_analyse_discrete.txt')
+
+
+if __name__ == '__main__':
+    eps = 0.0001
+
+    #obj = ("datasets/Barette/3 - BARETTE v1.obj","datasets/Barette/BARETTE_Delta 1,0_analyse brute.txt","datasets/Barette/BARETTE_Delta 1,0_analyse rayon.txt")
+    obj = ("datasets/Cylindre/3 - CYLINDRE v1.obj","datasets/Cylindre/Cylindre_Delta 1,0_analyse brute.txt","datasets/Cylindre/Cylindre_Delta 1,0_analyse rayon.txt")
+    #obj = ("datasets/Echantillon/3 - KA50HN50_98% fusion v1.obj","datasets/Echantillon/30-03-2023_Delta 1,0_analyse brute.txt","datasets/Echantillon/30-03-2023_Delta 1,0_analyse rayon.txt")
+
+    test_get_raw_data(obj[0],obj[1],eps=eps)
+    
+    test_get_discrete_data(obj[0],obj[2],eps=eps)
\ No newline at end of file
-- 
GitLab