pytrnsys_process.util package#

Submodules#

pytrnsys_process.util.file_converter module#

class pytrnsys_process.util.file_converter.CsvConverter[source]#

Bases: object

static rename_file_with_prefix(file_path: ~pathlib.Path, prefix: ~pytrnsys_process.config.constants.FileType, logger: ~logging.Logger = <Logger default_pytrnsys_process (WARNING)>) None[source]#

Rename a file with a given prefix.

Parameters:
  • file_path – Path to the file to rename

  • prefix – FileType enum value specifying the prefix to use

Returns:

Path – Path to the renamed file

Return type:

pathlib.Path

Raises:

FileNotFoundError – If the file doesn’t exist:

convert_sim_results_to_csv(input_path: ~pathlib.Path, output_dir: ~pathlib.Path, logger: ~logging.Logger = <Logger default_pytrnsys_process (WARNING)>) None[source]#

Convert TRNSYS simulation results to CSV format.

Parameters:
  • input_path (pathlib.Path) – Path to input file or directory containing input files

  • output_dir (pathlib.Path) – Directory where CSV files will be saved

Raises:

ValueError – If a file doesn’t match any known pattern:

static using_file_content_read_appropriately(file_path: ~pathlib.Path, logger: ~logging.Logger = <Logger default_pytrnsys_process (WARNING)>) tuple[str, DataFrame][source]#

Read the file according to the file contents.

pytrnsys_process.util.utils module#

pytrnsys_process.util.utils.get_sim_folders(path_to_results: Path) Sequence[Path][source]#
pytrnsys_process.util.utils.get_files(sim_folders: Sequence[Path], results_folder_name: str | None = None, get_mfr_and_t: bool | None = None, read_deck_files: bool | None = None) Sequence[Path][source]#

Get simulation files from folders based on configuration.

Parameters:
  • sim_folders – Sequence of paths to simulation folders

  • results_folder_name – Name of folder containing printer files

  • get_mfr_and_t – Whether to include step files (T and Mfr files)

  • read_deck_files – Whether to include deck files

Return type:

Sequence of paths to simulation files

pytrnsys_process.util.utils.export_plots_in_configured_formats(fig: Figure, path_to_directory: str, plot_name: str, plots_folder_name: str = 'plots') None[source]#

Save a matplotlib figure in multiple formats and sizes.

Saves the figure in all configured formats (png, pdf, emf) and sizes (A4, A4_HALF) as specified in the plot settings (api.settings.plot). For EMF format, the figure is first saved as SVG and then converted using Inkscape.

Parameters:
  • fig – The matplotlib Figure object to save.

  • path_to_directory – Directory path where the plots should be saved.

  • plot_name – Base name for the plot file (will be appended with size and format).

  • plots_folder_name – leave empty if you don’t want to save in a new folder

Note

  • Creates a ‘plots’ subdirectory if it doesn’t exist

  • For EMF files, requires Inkscape to be installed at the configured path

  • File naming format: {plot_name}-{size_name}.{format}

Example

>>> from pytrnsys_process import api, data_structures
>>> def processing_of_monthly_data(simulation: data_structures.Simulation):
>>>     monthly_df = simulation.monthly
>>>     columns_to_plot = ["QSnk60P", "QSnk60PauxCondSwitch_kW"]
>>>     fig, ax = api.bar_chart(monthly_df, columns_to_plot)
>>>
>>>     # Save the plot in multiple formats
>>>     api.export_plots_in_configured_formats(fig, simulation.path, "monthly-bar-chart")
>>>     # Creates files like:
>>>     #   results/simulation1/plots/monthly-bar-chart-A4.png
>>>     #   results/simulation1/plots/monthly-bar-chart-A4.pdf
>>>     #   results/simulation1/plots/monthly-bar-chart-A4.emf
>>>     #   results/simulation1/plots/monthly-bar-chart-A4_HALF.png
>>>     #   etc.
pytrnsys_process.util.utils.convert_svg_to_emf(file_no_suffix: Path) None[source]#
pytrnsys_process.util.utils.get_file_content_as_string(file_path: Path, encoding: str | None = None) str[source]#

Read and return the entire content of a file as a string.

Parameters:
  • pathlib.Path (file_path) – Path to the file to read

  • str (encoding) – File encoding to use. defaults to UTF-8 if that fails it tries to use windows-1252

  • optional – File encoding to use. defaults to UTF-8 if that fails it tries to use windows-1252

Returns:

file_content – Content of the file as a string

Return type:

str

pytrnsys_process.util.utils.save_to_pickle(data: ~pytrnsys_process.process.data_structures.Simulation | ~pytrnsys_process.process.data_structures.SimulationsData, path: ~pathlib.Path, logger: ~logging.Logger = <Logger default_pytrnsys_process (WARNING)>) None[source]#

Save ResultsForComparison data to a pickle file.

This function saves the entire ResultsForComparison object to a pickle file, preserving all data structures and relationships.

Parameters:
  • data – data object to save

  • path – Path where to save the pickle file

Raises:

OSError – If there’s an error when writing to the file:

pytrnsys_process.util.utils.load_simulations_data_from_pickle(path: ~pathlib.Path, logger: ~logging.Logger = <Logger default_pytrnsys_process (WARNING)>) SimulationsData[source]#

Load SimulationsData from a pickle file.

This function loads a previously saved SimulationsData object from a pickle file.

Parameters:
  • path (pathlib.Path) – To the pickle file to load

  • logger (logging.Logger, optional) – Logger object that will log any messages, warnings, and/or errors

Returns:

SimulationsData – Reconstructed SimulationsData object

Return type:

pytrnsys_processing.data_structures.SimulationsData

Raises:
pytrnsys_process.util.utils.load_simulation_from_pickle(path: ~pathlib.Path, logger: ~logging.Logger = <Logger default_pytrnsys_process (WARNING)>) Simulation[source]#

Load a Simulation object from a pickle file.

This function loads a previously saved Simulation object from a pickle file.

Parameters:
  • path (pathlib.Path) – To the pickle file to load

  • logger (logging.Logger, optional) – Logger object that will log any messages, warnings, and/or errors

Returns:

Simulation – Reconstructed Simulation object

Return type:

pytrnsys_processing.data_structures.Simulation

Raises:

Module contents#

class pytrnsys_process.util.CsvConverter[source]#

Bases: object

convert_sim_results_to_csv(input_path: ~pathlib.Path, output_dir: ~pathlib.Path, logger: ~logging.Logger = <Logger default_pytrnsys_process (WARNING)>) None[source]#

Convert TRNSYS simulation results to CSV format.

Parameters:
  • input_path (pathlib.Path) – Path to input file or directory containing input files

  • output_dir (pathlib.Path) – Directory where CSV files will be saved

Raises:

ValueError – If a file doesn’t match any known pattern:

static rename_file_with_prefix(file_path: ~pathlib.Path, prefix: ~pytrnsys_process.config.constants.FileType, logger: ~logging.Logger = <Logger default_pytrnsys_process (WARNING)>) None[source]#

Rename a file with a given prefix.

Parameters:
  • file_path – Path to the file to rename

  • prefix – FileType enum value specifying the prefix to use

Returns:

Path – Path to the renamed file

Return type:

pathlib.Path

Raises:

FileNotFoundError – If the file doesn’t exist:

static using_file_content_read_appropriately(file_path: ~pathlib.Path, logger: ~logging.Logger = <Logger default_pytrnsys_process (WARNING)>) tuple[str, DataFrame][source]#

Read the file according to the file contents.

pytrnsys_process.util.get_sim_folders(path_to_results: Path) Sequence[Path][source]#
pytrnsys_process.util.get_files(sim_folders: Sequence[Path], results_folder_name: str | None = None, get_mfr_and_t: bool | None = None, read_deck_files: bool | None = None) Sequence[Path][source]#

Get simulation files from folders based on configuration.

Parameters:
  • sim_folders – Sequence of paths to simulation folders

  • results_folder_name – Name of folder containing printer files

  • get_mfr_and_t – Whether to include step files (T and Mfr files)

  • read_deck_files – Whether to include deck files

Return type:

Sequence of paths to simulation files

pytrnsys_process.util.export_plots_in_configured_formats(fig: Figure, path_to_directory: str, plot_name: str, plots_folder_name: str = 'plots') None[source]#

Save a matplotlib figure in multiple formats and sizes.

Saves the figure in all configured formats (png, pdf, emf) and sizes (A4, A4_HALF) as specified in the plot settings (api.settings.plot). For EMF format, the figure is first saved as SVG and then converted using Inkscape.

Parameters:
  • fig – The matplotlib Figure object to save.

  • path_to_directory – Directory path where the plots should be saved.

  • plot_name – Base name for the plot file (will be appended with size and format).

  • plots_folder_name – leave empty if you don’t want to save in a new folder

Note

  • Creates a ‘plots’ subdirectory if it doesn’t exist

  • For EMF files, requires Inkscape to be installed at the configured path

  • File naming format: {plot_name}-{size_name}.{format}

Example

>>> from pytrnsys_process import api, data_structures
>>> def processing_of_monthly_data(simulation: data_structures.Simulation):
>>>     monthly_df = simulation.monthly
>>>     columns_to_plot = ["QSnk60P", "QSnk60PauxCondSwitch_kW"]
>>>     fig, ax = api.bar_chart(monthly_df, columns_to_plot)
>>>
>>>     # Save the plot in multiple formats
>>>     api.export_plots_in_configured_formats(fig, simulation.path, "monthly-bar-chart")
>>>     # Creates files like:
>>>     #   results/simulation1/plots/monthly-bar-chart-A4.png
>>>     #   results/simulation1/plots/monthly-bar-chart-A4.pdf
>>>     #   results/simulation1/plots/monthly-bar-chart-A4.emf
>>>     #   results/simulation1/plots/monthly-bar-chart-A4_HALF.png
>>>     #   etc.
pytrnsys_process.util.convert_svg_to_emf(file_no_suffix: Path) None[source]#
pytrnsys_process.util.get_file_content_as_string(file_path: Path, encoding: str | None = None) str[source]#

Read and return the entire content of a file as a string.

Parameters:
  • pathlib.Path (file_path) – Path to the file to read

  • str (encoding) – File encoding to use. defaults to UTF-8 if that fails it tries to use windows-1252

  • optional – File encoding to use. defaults to UTF-8 if that fails it tries to use windows-1252

Returns:

file_content – Content of the file as a string

Return type:

str

pytrnsys_process.util.save_to_pickle(data: ~pytrnsys_process.process.data_structures.Simulation | ~pytrnsys_process.process.data_structures.SimulationsData, path: ~pathlib.Path, logger: ~logging.Logger = <Logger default_pytrnsys_process (WARNING)>) None[source]#

Save ResultsForComparison data to a pickle file.

This function saves the entire ResultsForComparison object to a pickle file, preserving all data structures and relationships.

Parameters:
  • data – data object to save

  • path – Path where to save the pickle file

Raises:

OSError – If there’s an error when writing to the file:

pytrnsys_process.util.load_simulations_data_from_pickle(path: ~pathlib.Path, logger: ~logging.Logger = <Logger default_pytrnsys_process (WARNING)>) SimulationsData[source]#

Load SimulationsData from a pickle file.

This function loads a previously saved SimulationsData object from a pickle file.

Parameters:
  • path (pathlib.Path) – To the pickle file to load

  • logger (logging.Logger, optional) – Logger object that will log any messages, warnings, and/or errors

Returns:

SimulationsData – Reconstructed SimulationsData object

Return type:

pytrnsys_processing.data_structures.SimulationsData

Raises:
pytrnsys_process.util.load_simulation_from_pickle(path: ~pathlib.Path, logger: ~logging.Logger = <Logger default_pytrnsys_process (WARNING)>) Simulation[source]#

Load a Simulation object from a pickle file.

This function loads a previously saved Simulation object from a pickle file.

Parameters:
  • path (pathlib.Path) – To the pickle file to load

  • logger (logging.Logger, optional) – Logger object that will log any messages, warnings, and/or errors

Returns:

Simulation – Reconstructed Simulation object

Return type:

pytrnsys_processing.data_structures.Simulation

Raises: