biopsykit.utils.file_handling module¶
Helper functions for file handling.
- biopsykit.utils.file_handling.mkdirs(dir_list)[source]¶
Batch-create a list of directories.
Conveniently create a list of directories, e.g. directories for storing processing results, figures, and statistic reports, using
mkdir()
. If parent directories do not exist yet, they are created (pathlib optionparent=True
), if directories already exist no Error is thrown (pathlib optionexist_ok=True
).- Parameters
dir_list (list of path or str) – list of directory names to create
- Return type
None
Examples
>>> from biopsykit.utils.file_handling import mkdirs >>> path_list = [Path("processing_results"), Path("exports/figures"), Path("exports/statistics")] >>> mkdirs(path_list)
- biopsykit.utils.file_handling.get_subject_dirs(base_path, pattern)[source]¶
Filter for subject directories using a name pattern.
- Parameters
- Returns
a list of path or an empty list if no subfolders matched the
pattern
- Return type
list of path
- Raises
FileNotFoundError – if no subfolders in
base_path
matchpattern
.
Examples
>>> from biopsykit.utils.file_handling import get_subject_dirs >>> base_path = Path(".") >>> get_subject_dirs(base_path, "Vp*")
- biopsykit.utils.file_handling.export_figure(fig, filename, base_dir, formats=None, use_subfolder=True, **kwargs)[source]¶
Export matplotlib figure to file(s).
This function allows to export a matplotlib figure in multiple file formats, each format being optionally saved in its own output subfolder.
- Parameters
fig (
Figure
) – matplotlib figure objectfilename (
Path
or str) – name of the output filebase_dir (path or str) – base directory to export figures to
formats (list of str, optional) – list of file formats to export or
None
to export as pdf. Default:None
use_subfolder (bool, optional) – whether to create an own output subfolder per file format or not. Default:
True
**kwargs – additional keyword arguments to pass to
savefig()
Examples
>>> fig = plt.Figure() >>> >>> base_dir = "./img" >>> filename = "plot" >>> formats = ["pdf", "png"] >>> # Export into subfolders (default) >>> export_figure(fig, filename=filename, base_dir=base_dir, formats=formats) >>> # | img/ >>> # | - pdf/ >>> # | - - plot.pdf >>> # | - png/ >>> # | - - plot.png
>>> # Export into one folder >>> export_figure(fig, filename=filename, base_dir=base_dir, formats=formats, use_subfolder=False) >>> # | img/ >>> # | - plot.pdf >>> # | - plot.png
- biopsykit.utils.file_handling.is_excel_file(file_name, raise_exception=True)[source]¶
Check whether the file name is an Excel file.
- Parameters
- Returns
True
iffile_name
is an Excel file, i.e. has the suffix.xlsx
False
otherwise (ifraise_exception
isFalse
)
- Raises
ValidationError – if
raise_exception
isTrue
andfile_name
is not an Excel file- Return type