Skip to content

Commit 54f7f72

Browse files
authored
ADD Better format CSV files (#126)
* ADD Better format CSV files * FIX restructuring
1 parent 200bd74 commit 54f7f72

File tree

4 files changed

+5959
-10341
lines changed

4 files changed

+5959
-10341
lines changed

src/gh/components/DF_cloud_mesh_distance/code.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def RunScript(self,
5555
siffed_rh_mesh_target_list.append(rh_mesh_target_list[i])
5656

5757
# calculate distances
58-
o_result = df_error_estimation.df_cloud_2_rh_mesh_comparison(siffed_df_cloud_source_list, siffed_rh_mesh_target_list, i_signed_flag, i_swap)
58+
o_result = df_error_estimation.df_cloud_2_rh_mesh_comparison(i_assembly, df_cloud_source_list, rh_mesh_target_list, i_signed_flag, i_swap)
5959

6060
# distances to tree
6161
distances_tree = Grasshopper.DataTree[object]()

src/gh/components/DF_csv_exporter/code.py

Lines changed: 50 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,43 @@
99

1010

1111
class DFCsvExporter(component):
12+
def _get_id(self, idx, i_result):
13+
""" Get the ID of the element """
14+
counter = 0
15+
16+
if self.prefix == "beam":
17+
return idx
18+
elif self.prefix == "joint":
19+
for idx_b, beam in enumerate(i_result.assembly.beams):
20+
for idx_j, joint in enumerate(beam.joints):
21+
if counter == idx:
22+
return f"{idx_b}--{idx_b}--{idx_j}"
23+
counter += 1
24+
elif self.prefix == "joint_face":
25+
for idx_b, beam in enumerate(i_result.assembly.beams):
26+
for idx_j, joint in enumerate(beam.joints):
27+
for idx_f, face in enumerate(joint.faces):
28+
if counter == idx:
29+
return f"{idx_b}--{idx_j}--{idx_f}"
30+
counter += 1
31+
32+
def _write_csv(self, file_path, rows):
33+
""" Write the CSV file """
34+
with open(file_path, mode='w', newline='') as file:
35+
writer = csv.writer(file)
36+
writer.writerow([f"{self.prefix} id", "distances", "min_deviation", "max_deviation", "std_deviation", "rmse"])
37+
writer.writerows(rows)
38+
39+
def _prepare_row(self, idx, i_result):
40+
""" Prepare a row for the CSV file """
41+
distances = [round(value, 4) for value in i_result.distances[idx]]
42+
min_dev = round(i_result.distances_min_deviation[idx], 4)
43+
max_dev = round(i_result.distances_max_deviation[idx], 4)
44+
std_dev = round(i_result.distances_sd_deviation[idx], 4)
45+
rmse = round(i_result.distances_rmse[idx], 4)
46+
distances_str = ";".join(map(str, distances))
47+
return [self._get_id(idx, i_result), distances_str, min_dev, max_dev, std_dev, rmse]
48+
1249
def RunScript(self,
1350
i_dump: bool,
1451
i_export_dir: str,
@@ -17,31 +54,21 @@ def RunScript(self,
1754
i_result: DFVizResults):
1855

1956
if i_dump:
20-
# Ensure the export directory exists
2157
os.makedirs(i_export_dir, exist_ok=True)
2258

59+
if len(i_result.assembly.beams) == len(i_result.source):
60+
self.prefix = "beam"
61+
elif len(i_result.assembly.all_joints) == len(i_result.source):
62+
self.prefix = "joint"
63+
elif len(i_result.assembly.all_joint_faces) == len(i_result.source):
64+
self.prefix = "joint_face"
65+
2366
if i_export_seperate_files:
24-
# Export each list of values to a separate file
25-
for idx, list_of_values in enumerate(i_result.distances):
26-
file_name = f"{i_file_name}_{idx + 1}.csv"
27-
file_path = os.path.join(i_export_dir, file_name)
28-
with open(file_path, mode='w', newline='') as file:
29-
writer = csv.writer(file)
30-
writer.writerow([list_of_values])
67+
for idx in range(len(i_result.source)):
68+
element_id = self._get_id( idx, i_result)
69+
file_path = os.path.join(i_export_dir, f"{i_file_name}_{self.prefix}_{element_id}.csv")
70+
self._write_csv(file_path, [self._prepare_row(idx, i_result)])
3171
else:
32-
# Export all values to a single file
3372
file_path = os.path.join(i_export_dir, f"{i_file_name}.csv")
34-
with open(file_path, mode='w', newline='') as file:
35-
writer = csv.writer(file)
36-
for list_of_values in i_result.distances:
37-
writer.writerow([list_of_values])
38-
39-
# if __name__ == "__main__":
40-
# com = DFCsvExporter()
41-
# o_viz_settings = com.RunScript(
42-
# i_dump,
43-
# i_export_dir,
44-
# i_file_name,
45-
# i_export_seperate_files,
46-
# i_result
47-
# )
73+
rows = [self._prepare_row(idx, i_result) for idx in range(len(i_result.source))]
74+
self._write_csv(file_path, rows)

src/gh/diffCheck/diffCheck/df_error_estimation.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,27 @@
66
import numpy as np
77
from diffCheck import diffcheck_bindings # type: ignore
88
import Rhino.Geometry as rg
9+
from diffCheck.df_geometries import DFAssembly
910

1011

1112
def df_cloud_2_df_cloud_comparison(source_list, target_list):
1213
"""
1314
Compute the Euclidean distance for every point of a source pcd to its
1415
closest point on a target pointcloud
1516
"""
16-
results = DFVizResults()
17+
results = DFVizResults(DFAssembly())
1718
for source, target in zip(source_list, target_list):
1819
distances = np.asarray(source.compute_distance(target))
1920
results.add(source, target, distances)
2021

2122
return results
2223

2324

24-
def df_cloud_2_rh_mesh_comparison(cloud_source_list, rhino_mesh_target_list, signed_flag, swap):
25+
def df_cloud_2_rh_mesh_comparison(assembly, cloud_source_list, rhino_mesh_target_list, signed_flag, swap):
2526
"""
2627
Computes distances between a pcd and a mesh
2728
"""
28-
results = DFVizResults()
29+
results = DFVizResults(assembly)
2930

3031
for source, target in zip(cloud_source_list, rhino_mesh_target_list):
3132

@@ -123,7 +124,7 @@ class DFVizResults:
123124
This class compiles the resluts of the error estimation into one object
124125
"""
125126

126-
def __init__(self):
127+
def __init__(self, assembly):
127128

128129
self.source = []
129130
self.target = []
@@ -133,6 +134,7 @@ def __init__(self):
133134
self.distances_min_deviation = []
134135
self.distances_sd_deviation = []
135136
self.distances = []
137+
self.assembly = assembly
136138

137139
def add(self, source, target, distances):
138140

0 commit comments

Comments
 (0)