io

class ResultWriter(result_dir, filename_prefix='', enabled: bool = True, close_figures: bool = False)[source]

Bases: object

__init__(result_dir, filename_prefix='', enabled: bool = True, close_figures: bool = False)
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

child_with_added_prefix(prefix: str) sensai.util.io.ResultWriter

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) sensai.util.io.ResultWriter
path(filename_suffix: str, extension_to_add=None, valid_other_extensions: Optional[Sequence[str]] = None) str
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)
write_text_file_lines(filename_suffix: str, lines: List[str])
write_data_frame_text_file(filename_suffix: str, df: pandas.core.frame.DataFrame)
write_data_frame_csv_file(filename_suffix: str, df: pandas.core.frame.DataFrame, index=True, header=True)
write_figure(filename_suffix: str, fig: matplotlib.figure.Figure, close_figure: Optional[bool] = None)
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, matplotlib.figure.Figure]], close_figures=False)
write_pickle(filename_suffix: str, obj: Any)
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

__init__(path)
class OutputFile(s3_object: sensai.util.io.S3Object)

Bases: object

__init__(s3_object: sensai.util.io.S3Object)
write(obj: bytes)
get_file_content()
open_file(mode)
put(obj: bytes)