io#
Source code: sensai/util/io.py
- class ResultWriter(result_dir, filename_prefix='', enabled: bool = True, close_figures: bool = False)[source]#
Bases:
object
- Parameters:
result_dir –
filename_prefix –
enabled – whether the result writer is enabled; if it is not, it will create neither files nor directories
close_figures – whether to close figures that are passed by default
- log = <Logger sensai.util.io.ResultWriter (WARNING)>#
- child_with_added_prefix(prefix: str) ResultWriter [source]#
Creates a derived result writer with an added prefix, i.e. the given prefix is appended to this result writer’s prefix
- Parameters:
prefix – the prefix to append
- Returns:
a new writer instance
- child_for_subdirectory(dir_name: str) ResultWriter [source]#
- path(filename_suffix: str, extension_to_add=None, valid_other_extensions: Optional[Sequence[str]] = None) str [source]#
- Parameters:
filename_suffix – the suffix to add (which may or may not already include a file extension)
extension_to_add – if not None, the file extension to add (without the leading “.”) unless the extension to add or one of the extenions in valid_extensions is already present
valid_other_extensions – a sequence of valid other extensions (without the “.”), only relevant if extensionToAdd is specified
- Returns:
the full path
- write_figure(filename_suffix: str, fig: plt.Figure, close_figure: Optional[bool] = None)[source]#
- Parameters:
filename_suffix – the filename suffix, which may or may not include a file extension, valid extensions being {“png”, “jpg”}
fig – the figure to save
close_figure – whether to close the figure after having saved it; if None, use default passed at construction
- Returns:
the path to the file that was written (or would have been written if the writer was enabled)
- write_text_file_lines(lines: List[str], path)[source]#
- Parameters:
lines – the lines to write (without a trailing newline, which will be added)
path – the path of the text file to write to
- read_text_file_lines(path, strip=True, skip_empty=True) List[str] [source]#
- Parameters:
path – the path of the text file to read from
strip – whether to strip each line, removing whitespace/newline characters
skip_empty – whether to skip any lines that are empty (after stripping)
- Returns:
the list of lines