io#


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_text_file(filename_suffix: str, content: str)[source]#
write_text_file_lines(filename_suffix: str, lines: List[str])[source]#
write_data_frame_text_file(filename_suffix: str, df: pd.DataFrame)[source]#
write_data_frame_csv_file(filename_suffix: str, df: pd.DataFrame, index=True, header=True)[source]#
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_figures(figures: Sequence[Tuple[str, plt.Figure]], close_figures=False)[source]#
write_pickle(filename_suffix: str, obj: Any)[source]#
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

is_s3_path(path: str)[source]#
class S3Object(path)[source]#

Bases: object

class OutputFile(s3_object: S3Object)[source]#

Bases: object

write(obj: bytes)[source]#
get_file_content()[source]#
open_file(mode)[source]#
put(obj: bytes)[source]#