package documentation

A lose collection of code I reuse in different (scientific) projects.

Module analy Generate output for analysis in form of tables and figures.
Module bandas Helpers with pandas.
Module khq Scoring mode of the German version of the King's Health Questionnaire described in Bjelic-Radisci V et al. (2005) DOI: 10.1055/s-2005-872957.
Module report That module can create reports from data objects like pandas.DataFrame, images or figures (e.g. based on seaborn or matplotlib).
Module stausberg Calculation of comorbidity score based on the structure of the ICD-10 described in Stausberg; Hagn (2015): New Morbidity and Comorbidity Scores based on the Structure of the ICD-10 DOI: 10.1371/journal.pone.0143365...
Module zuf Scoring mode of the ZUF-8 "Fragenbogen zur Patientenzufriedenheit". A German translation based on the CSQ-8 "Client Satisfaction Questionnaire".
Module _buhtzology Undocumented
Module _datacontainer The bandas data container.

From __init__.py:

Function generate_filepath Combine basename, path and timestamp to a filepath of an existing file.
Function get_full_application_string Build a string representing the application name and its version.
Function get_git_repository_info Return the current branch and last commit hash.
Function how_much_elements_per_piece Calculate the number of elements in one piece when a bigger list of elements is cut into pieces depending on rules.
Function runtime_as_string Gives the runtime as a string.
Function setup_logging Setup the root logger with a console and a file handler.
Function shorten_strings_but_unique Shorten strings to a given limit but keep them unique to the list.
Variable __full_name__ Full name string including version, branch, commit.
Variable __version__ Version string of the package.
Variable meta Undocumented
Function _package_metadata_as_dict Get package metadata and return it as a dict.
def generate_filepath(basename: str, file_suffix: str, folder_path: pathlib.Path = None, age_offset: int = 0) -> pathlib.Path: (source)

Combine basename, path and timestamp to a filepath of an existing file.

Generates a path of an existing file with a timestamp in its name based on a name stem, a file suffix. Optionally a folder different from current working dir (default) and an offset for the file age (based on the timestamp in filename) can be set.

The original file names should contain a timestamp (e.g. YYYY-MM-DD) or another numeric or lexicographical sortable element. The =folder_path= is searched for all files matching the name pattern =*basename*file_suffix=. The found names are sorted reverse lexicographical. The 'age_offset' element of that list is returned which is by default the first element in the list which in turn is the youngest file provided the timestamp's in the filenames are correct.

Parameters
basename:strName stem of the file's to search.
file_suffix:strSuffix of the file's to search.
folder_path:pathlib.PathPath to the folder where to search in (default: current working dir).
age_offset:intElement to use after reverse lexicographic sorting.
Returns
pathlib.PathFile path relative to current working dir.
Raises
FileNotFoundErrorWhen no file was found.
IndexErrorIf the 'age_offset' does not work.
def runtime_as_string(start_time: datetime.datetime) -> str: (source)

Gives the runtime as a string.

The runtime is calculated from the difference between current time and 'start_time'. Example results are "20 seconds" or "4.5 minutes".

Parameters
start_time:datetime.datetimeTimestamp of start.
Returns
strUndocumented
def how_much_elements_per_piece(total_elements_n: int, pieces_n: int = None, min_max_per_piece: int = None, min_pieces_n: int = None) -> int: (source)

Calculate the number of elements in one piece when a bigger list of elements is cut into pieces depending on rules.

If 'pieces_n' is given all other arguments are ignored and the result are the number of elements per piece you have to cut from your list to get the number of pieces specified by 'pieces_n'.

If 'pieces_n' is None then the number of elements per piece is calculated based on 'min_max_per_piece'. But if 'min_pieces_n' is given also then the min/max rules can be overwritten to give you as much elements per pieces to result with 'min_pieces_n.

There is a hierarchy of the rules.
  1. If 'pieces_n' is given the rest of arguments are ignored.
  2. If 'pieces_n' is None then 'min_max_per_piece' is taken into account. If present 'min_pieces_n' is also taken into account.

Be aware that the last piece in your list can have less then the resulting number of elements per piece.

Parameters
total_elements_n:intx
pieces_n:inty
min_max_per_piece:intz
min_pieces_n:inta
Returns
intNumber of elements one piece should have to fulfill the rules.
Raises
ValueErrorNo rules specified with arguments.
def setup_logging(log_directory: pathlib.Path = None, console_level: int = logging.INFO, console_format_str: str = '%(log_color)s[%(levelname)s]%(reset)s %(message)s', file_level: int = logging.DEBUG, file_format_str: str = '%(asctime)s - %(name)s - %(levelname)s - %(message)s', file_backup_count: int = 20, exceptional_loggers: Dict[str, int] = None): (source)

Setup the root logger with a console and a file handler.

The file handler is rotated for each session. The file name is based on sys.argv[0] (the filename of the current running python script). With exceptional_loggers it is possible to set levels to specific loggers; e.g. matplotlib.font_manager or PIL which are know for annoying debug messages.

Colorized console output is activated by default if the package colorlog is available.

Parameters
log_directory:pathlib.PathPath to log file directory.
console_level:intLog level for console (stdout) handler.
console_format_str:strFormat of the console log messages.
file_level:intLog level for file handler. Deactivate with None.
file_format_str:strFormat of the file log messages.
file_backup_count:intKeep that amount of log files.
exceptional_loggers:Dict[str, int]Levels for specific loggers.
def shorten_strings_but_unique(string_list: Iterable[str], limit: int = 25, omit_string: str = '') -> Iterable[str]: (source)

Shorten strings to a given limit but keep them unique to the list.

Parameters
string_list:Iterable[str]List of strings to shorten.
limit:intNumber of characters each string is shorten to.
omit_string:strCharacter used to visualize the omit.
Returns
Iterable[str]List of shorten strings.
def _package_metadata_as_dict(exclude_keys: list = None) -> dict: (source)

Get package metadata and return it as a dict.

Undocumented

__version__ = (source)

Version string of the package.

def get_full_application_string() -> str: (source)

Build a string representing the application name and its version.

If a git repo is present also information about its current state are added.

def get_git_repository_info() -> dict: (source)

Return the current branch and last commit hash.

A special case is when that script runs on a Read The Docs instance. In that case the branch name is extracted from environment variables.

Credits: https://stackoverflow.com/a/51224861/4865723

__full_name__ = (source)

Full name string including version, branch, commit.