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_data_frame_excel_file(filename_suffix: str, df: pd.DataFrame, index=True, header=True, sheet_name='Sheet1')[source]#
Saves the given data frame to an Excel spreadsheet.
Requires that an appropriate engine be installed, e.g. openpyxl.
- Parameters:
filename_suffix – the filename suffix (or full basename for the case where no prefix was specified at construction), to which the extension .xlsx will be added by default if no extension is given
df – the data frame to save
index – whether to save the index
header – whether to save the header row
sheet_name – the name of the spreadsheet
- Returns:
the path to the saved file
- 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
- filename_compatible(fn: str, replacement: str = '-') str [source]#
Converts the given string into a string that can be used in a filename
- Parameters:
fn – original filename, which may or may not be compatible with common filenames
replacement – default character to use as a replacement
- Returns:
adapted filename