AnalyseMorphologique/utils/files/output.py
Djalim Simaila fb2bb6e9ce 🐛 fix(data_test.py): change variable name from teta_diffs to theta_diffs to improve semantics
 feat(data_processing.py): add function to calculate morphological indicators from discrete data
The variable name teta_diffs was changed to theta_diffs to improve semantics. A new function was added to calculate morphological indicators from discrete data. The function calculates Tortuosity, Volume, Surface, Mean radius, Standard deviation of radius, Sigma r tot, MI_l, and MI_p.

🔥 refactor(input.py): remove unused result_file_path parameter from ScannedObject constructor and from_xyz_file method
 feat(input.py): add encoding parameter to open method in from_obj_file and from_xyz_file methods
The result_file_path parameter was not being used in the ScannedObject constructor and from_xyz_file method, so it was removed to simplify the code. The encoding parameter was added to the open method in the from_obj_file and from_xyz_file methods to ensure that the files are opened with the correct encoding.

🐛 fix(output.py): add utf-8 encoding when writing to output file
 feat(output.py): remove unused import and function argument, improve code readability
The fix adds the utf-8 encoding when writing to the output file to avoid encoding issues. The feat removes the unused import and function argument to improve code readability. The function format_data now only takes the necessary arguments and the unused import is removed.

🐛 fix(main_window.py): fix typo in function name
 feat(main_window.py): add persistence to pre-processed data
The fix corrects a typo in the function name get_true_theta_from_x_y. The feat adds persistence to the pre-processed data by storing the raw data, discrete data, and advanced data in the main window. This avoids re-computation of the data when switching between tabs.

🎨 style(MainWindow.ui): add export_advanced_metrics button to the UI
🎨 style(UI_MainWindow.py): add export_advanced_metrics button to the UI
🎨 style(ressources_rc.py): update the resource file
🐛 fix(data_extraction.py): fix typo in function name get_mean_teta to get_mean_theta
The changes add a new button to the UI named "export_advanced_metrics" which allows the user to export variables. The resource file is updated to reflect the changes. The typo in the function name get_mean_teta is fixed to get_mean_theta.
2023-05-09 10:56:43 +02:00

65 lines
2.0 KiB
Python

"""
Created on Mon Apr 17 2023
@name: output.py
@desc: Output file formatting and saving
@auth: Djalim Simaila
@e-mail: djalim.simaila@inrae.fr
"""
from utils.settings.SettingManager import SettingManager
def format_data(data:dict, separator:str, selected_columns:list = None) -> str:
"""
Format the data to be saved in the output file.
:param data: Data to be formatted
:param selected_columns: Columns to be saved (in the specified order)
:param separator: Separator of the columns
:return: Formatted data
Example:
>>> data = {
... 'col1': [1, 2, 3],
... 'col2': [4, 5, 6],
... 'col3': [7, 8, 9]
... }
>>> format_data(data, separator=';')
'col1;col2;col3
1 ;4 ;7
2 ;5 ;8
3 ;6 ;9
'
"""
# Get the setting for the output file
is_prettier = SettingManager.get_instance().get_setting('pretiffy_output_file')
output = ''
# If the columns are not specified, we take all the columns from dict
if selected_columns is None:
selected_columns = list(data.keys())
# Write the columns headers
for column_name in selected_columns:
if is_prettier:
output += column_name.ljust(len(column_name) if len(column_name) > 8 else 9 ) + separator
else:
output += column_name + separator
output += '\n'
# Write the columns values
for i in range(len(data[selected_columns[0]])):
for column in selected_columns:
if is_prettier:
output += str(data[column][i]).ljust(len(column) if len(column) > 8 else 9 ) + separator
else:
output += str(data[column][i]) + separator
output += '\n'
return output
def save_output_file(output_file:str, content:str):
"""
Save the output file.
:param output_file: Path to the output file
:param content: Content of the output file
"""
with open(output_file, 'w',encoding='utf-8') as file:
file.write(content)