Skip to content

Commit

Permalink
FIX-ADD: no more distances in csv export, added distances export opti…
Browse files Browse the repository at this point in the history
…on as separate
  • Loading branch information
9and3 committed Nov 11, 2024
1 parent d830fe0 commit 926a9ec
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 20 deletions.
95 changes: 75 additions & 20 deletions src/gh/components/DF_csv_exporter/code.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
#! python3

import csv
import os
import typing

from ghpythonlib.componentbase import executingcomponent as component

from diffCheck.df_error_estimation import DFInvalidData
import csv
import os
from diffCheck.df_error_estimation import DFInvalidData, DFVizResults


class DFCsvExporter(component):
Expand All @@ -14,7 +15,10 @@ def __init__(self):
self.prefix = ""
self.counter = 0

def _get_id(self, idx, i_result):
def _get_id(self,
idx: int,
i_result: DFVizResults
) -> str:
""" Get the ID of the element """
counter = 0

Expand All @@ -34,15 +38,18 @@ def _get_id(self, idx, i_result):
return f"{idx_b}--{idx_j}--{idx_f}"
counter += 1

def _write_csv(self, file_path, rows):
""" Write the CSV file """
with open(file_path, mode='w', newline='') as file:
writer = csv.writer(file)
writer.writerow([f"{self.prefix} id", "distances", "min_deviation", "max_deviation", "std_deviation", "rmse", "mean"])
writer.writerows(rows)
def _prepare_row(self,
idx: int,
i_result: DFVizResults
) -> typing.Dict:
"""
Convert the results contained in the DFVizResults object to a dict to be written in the CSV file
:param idx: Index of the element
:param i_result: DFVizResults object containing all the values
def _prepare_row(self, idx, i_result):
""" Prepare a row for the CSV file """
:return: Dict of values containng as keys the header and as items the values to be written in the CSV file
"""
if i_result.sanity_check[idx].value != DFInvalidData.VALID.value:
invalid_type = i_result.sanity_check[idx].name
return [self._get_id(idx, i_result), invalid_type, invalid_type, invalid_type, invalid_type, invalid_type, invalid_type]
Expand All @@ -53,16 +60,57 @@ def _prepare_row(self, idx, i_result):
std_dev = round(i_result.distances_sd_deviation[idx], 4)
rmse = round(i_result.distances_rmse[idx], 4)
mean = round(i_result.distances_mean[idx], 4)
distances_str = ";".join(map(str, distances))
return [self._get_id(idx, i_result), distances_str, min_dev, max_dev, std_dev, rmse, mean]

row: typing.Dict = {
f"{self.prefix} id": self._get_id(idx, i_result),
"distances": distances,
"min_deviation": min_dev,
"max_deviation": max_dev,
"std_deviation": std_dev,
"rmse": rmse,
"mean": mean
}
return row

def _write_csv(self,
csv_path: str,
rows: typing.List[typing.Dict],
is_writing_only_distances: bool = False
) -> None:
"""
Write the CSV file
:param csv_path: Path of the CSV file
:param rows: Dict of values to be written in the CSV file
:param is_writing_only_distances: Flag to check if to write ONLY distances or the whole analysis
:return: None
"""
with open(csv_path, mode='w', newline='') as file:
writer = csv.writer(file, quoting=csv.QUOTE_MINIMAL)
if is_writing_only_distances:
writer.writerow(list(rows[0].keys())[:2]) # header
element_id = [row[f"{self.prefix} id"] for row in rows]
dist_rows = [row["distances"] for row in rows]
for idx, dist_row in enumerate(dist_rows):
for dist in dist_row:
writer.writerow([element_id[idx], dist])
else:
rows = [{k: v for k, v in row.items() if k != "distances"} for row in rows] # no distances
writer.writerow(list(rows[0].keys())) # header
writer.writerows([list(row.values()) for row in rows])

def RunScript(self,
i_dump: bool,
i_export_dir: str,
i_file_name: str,
i_export_seperate_files: bool,
i_export_distances: bool,
i_result):

csv_analysis_path: str = None
csv_distances_path: str = None

if i_dump:
os.makedirs(i_export_dir, exist_ok=True)

Expand All @@ -75,10 +123,17 @@ def RunScript(self,

if i_export_seperate_files:
for idx in range(len(i_result.source)):
element_id = self._get_id( idx, i_result)
file_path = os.path.join(i_export_dir, f"{i_file_name}_{self.prefix}_{element_id}.csv")
self._write_csv(file_path, [self._prepare_row(idx, i_result)])
element_id = self._get_id(idx, i_result)
csv_analysis_path = os.path.join(i_export_dir, f"{i_file_name}_{self.prefix}_{element_id}.csv")
rows = [self._prepare_row(idx, i_result)]
self._write_csv(csv_analysis_path, rows)
if i_export_distances:
csv_distances_path = os.path.join(i_export_dir, f"{i_file_name}_{self.prefix}_{element_id}_distances.csv")
self._write_csv(csv_distances_path, rows, is_writing_only_distances=True)
else:
file_path = os.path.join(i_export_dir, f"{i_file_name}.csv")
rows = [self._prepare_row(idx, i_result) for idx in range(len(i_result.source))]
self._write_csv(file_path, rows)
csv_analysis_path = os.path.join(i_export_dir, f"{i_file_name}.csv")
merged_rows = [self._prepare_row(idx, i_result) for idx in range(len(i_result.source))]
self._write_csv(csv_analysis_path, merged_rows)
if i_export_distances:
csv_distances_path = os.path.join(i_export_dir, f"{i_file_name}_distances.csv")
self._write_csv(csv_distances_path, merged_rows, is_writing_only_distances=True)
12 changes: 12 additions & 0 deletions src/gh/components/DF_csv_exporter/metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,18 @@
"sourceCount": 0,
"typeHintID": "bool"
},
{
"name": "i_export_distances",
"nickname": "i_export_distances",
"description": "Whether to export the calculated error distances for each point of the analysed point cloud.",
"optional": true,
"allowTreeAccess": true,
"showTypeHints": true,
"scriptParamAccess": "item",
"wireDisplay": "default",
"sourceCount": 0,
"typeHintID": "bool"
},
{
"name": "i_result",
"nickname": "i_result",
Expand Down

0 comments on commit 926a9ec

Please sign in to comment.