mlrl.testbed.io module

Author Michael Rapp (michael.rapp.ml@gmail.com)

Provides functions for writing and reading files.

mlrl.testbed.io.clear_directory(directory: str)

Deletes all files contained in a directory (excluding subdirectories).

Parameters:

directory – The directory to be cleared

mlrl.testbed.io.create_csv_dict_reader(csv_file) DictReader

Creates and return a DictReader that allows to read from a CSV file.

Parameters:

csv_file – The CSV file

Returns:

The ‘DictReader’ that has been created

mlrl.testbed.io.create_csv_dict_writer(csv_file, header) DictWriter

Creates and returns a DictWriter that allows to write a dictionary to a CSV file.

Parameters:
  • csv_file – The CSV file

  • header – A list that contains the headers of the CSV file. They must correspond to the keys in the directory that should be written to the file

Returns:

The DictWriter that has been created

mlrl.testbed.io.get_file_name(name: str, suffix: str) str

Returns a file name, including a suffix.

Parameters:
  • name – The name of the file (without suffix)

  • suffix – The suffix of the file

Returns:

The file name

mlrl.testbed.io.get_file_name_per_fold(name: str, suffix: str, fold: int | None) str

Returns a file name, including a suffix, that corresponds to a certain fold.

Parameters:
  • name – The name of the file (without suffix)

  • suffix – The suffix of the file

  • fold – The cross validation fold, the file corresponds to, or None, if the file does not correspond to a specific fold

Returns:

The file name

mlrl.testbed.io.open_readable_csv_file(file_path: str)

Opens a CSV file to be read from.

Parameters:

file_path – The path of the file to be opened

Returns:

The file that has been opened

mlrl.testbed.io.open_writable_csv_file(file_path: str, append: bool = False)

Opens a CSV file to be written to.

Parameters:
  • file_path – The path of the file to be opened

  • append – True, if new data should be appended to the file, if it already exists, False otherwise

Returns:

The file that has been opened

mlrl.testbed.io.open_writable_txt_file(directory: str, file_name: str, fold: int | None = None, append: bool = False)

Opens a text file to be written to.

Parameters:
  • directory – The directory where the file is located

  • file_name – The name of the file to be opened (without suffix)

  • fold – The cross validation fold, the file corresponds to, or None, if the file does not correspond to a specific fold

  • append – True, if new data should be appended to the file, if it already exists, False otherwise

Returns:

The file that has been opened

mlrl.testbed.io.write_xml_file(xml_file, root_element: Element)

Writes an XML structure to a file.

Parameters:
  • xml_file – The XML file

  • root_element – The root element of the XML structure