Saves experiment artifacts to a file Attributes: output_file_name: str, name of file to write to. output_dir: str, path to directory to write to. save_file_path: str, path to file to write to. on_csv_write: str, "append" or "update", whether or not to append
| 11 | |
| 12 | |
| 13 | class FileOutput(BaseOutput): |
| 14 | """ |
| 15 | Saves experiment artifacts to a file |
| 16 | |
| 17 | Attributes: |
| 18 | output_file_name: str, name of file to write to. |
| 19 | output_dir: str, path to directory to write to. |
| 20 | save_file_path: str, path to file to write to. |
| 21 | on_csv_write: str, "append" or "update", whether or not to append or overwrite a file if it exists. |
| 22 | """ |
| 23 | |
| 24 | def __init__( |
| 25 | self, output_file_name: str, output_dir: str, on_csv_write: str = "append" |
| 26 | ): |
| 27 | """ |
| 28 | Initialize the FileOutput object. |
| 29 | |
| 30 | Args: |
| 31 | output_file_name: name of file to write to. |
| 32 | output_dir: path to directory to write to. |
| 33 | on_csv_write: "append" or "update", whether or not to append or overwrite a file if it exists |
| 34 | |
| 35 | Raises: |
| 36 | ValueError: If the on_csv_write value is invalid. |
| 37 | OSError: If the output directory does not exist. |
| 38 | """ |
| 39 | if on_csv_write not in {"append", "update"}: |
| 40 | raise ValueError( |
| 41 | f"Unknown `on_csv_write` value: {on_csv_write}" |
| 42 | + " (should be one of 'append' or 'update'" |
| 43 | ) |
| 44 | self.output_file_name: str = output_file_name |
| 45 | if not os.path.exists(output_dir): |
| 46 | raise OSError(f"Folder '{output_dir}' doesn't exist !") |
| 47 | self.output_dir: str = output_dir |
| 48 | self.on_csv_write: str = on_csv_write |
| 49 | self.save_file_path = os.path.join(self.output_dir, self.output_file_name) |
| 50 | logger.info( |
| 51 | f"Emissions data (if any) will be saved to file {os.path.abspath(self.save_file_path)}" |
| 52 | ) |
| 53 | |
| 54 | def has_valid_headers(self, data: EmissionsData) -> bool: |
| 55 | """ |
| 56 | Checks self.save_file_path has headers matching those from passed data. |
| 57 | |
| 58 | Args: |
| 59 | data: EmissionsData object with valid headers. |
| 60 | |
| 61 | Returns: |
| 62 | True if the file has valid headers, False otherwise. |
| 63 | """ |
| 64 | with open(self.save_file_path) as csv_file: |
| 65 | csv_reader = csv.DictReader(csv_file) |
| 66 | csv_entries_list = list(csv_reader) |
| 67 | if len(csv_entries_list) == 0: |
| 68 | # No entries |
| 69 | return True |
| 70 | dict_from_csv = dict(csv_entries_list[0]) |
no outgoing calls
searching dependent graphs…