Common

Utils

Some frequently used utilityfunctions and classes

mesmerize.common.utils.make_workdir(prefix: str = '') → str[source]

Make a workdir within the mesmerize_tmp directory of the workdir specified in the configuration The name of the created workdir is the date & time of its creation. You can add a prefix to this name.

Parameters:prefix – Prefix for the workdir name
Returns:full workdir path
mesmerize.common.utils.make_runfile(module_path: str, savedir: str, args_str: Optional[str] = None, filename: Optional[str] = None, pre_run: Optional[str] = None, post_run: Optional[str] = None) → str[source]
Parameters:
  • module_path – absolute module path
  • args_str – str of args that is directly passed with the python command in the bash script
  • savedir – working directory
  • filename – optional, specific filename for the script
  • pre_run – optional, str to run before module is ran
  • post_run – optional, str to run after module has run
Returns:

path to the shell script that can be run

class mesmerize.common.utils.HdfTools[source]

Functions for saving and loading HDF5 data

static save_dataframe(path: str, dataframe: pandas.core.frame.DataFrame, metadata: Optional[dict] = None, metadata_method: str = 'json', raise_meta_fail: bool = True)[source]

Save DataFrame to hdf5 file along with a meta data dict.

Meta data dict can either be serialized with json and stored as a str in the hdf5 file, or recursively saved into hdf5 groups if the dict contains types that hdf5 can deal with. Experiment with both methods and see what works best

Currently the hdf5 method can work with these types: [str, bytes, int, float, np.int, np.int8, np.int16, np.int32, np.int64, np.float, np.float16, np.float32, np.float64, np.float128, np.complex].

If it encounters an object that is not of these types it will store whatever that object’s __str__() method returns if on_meta_fail is False, else it will raise an exception.

Parameters:
  • path – path to save the file to
  • dataframe – DataFrame to save in the hdf5 file
  • metadata – Any associated meta data to store along with the DataFrame in the hdf5 file
  • metadata_method – method for storing the metadata dict, either ‘json’ or ‘recursive’
  • raise_meta_fail – raise an exception if recursive metadata saving encounters an unsupported object
static save_dict(d: dict, filename: str, group: str, raise_type_fail=True)[source]

Recursively save a dict to an hdf5 group.

Parameters:
  • d – dict to save
  • filename – filename
  • group – group name to save the dict to
  • raise_type_fail – whether to raise if saving a piece of data fails
static load_dict(filename: str, group: str) → dict[source]

Recursively load a dict from an hdf5 group.

Parameters:
  • filename – filename
  • group – group name of the dict
Returns:

dict recursively loaded from the hdf5 group

QDialogs

Decorators for Qt Dialog GUIs used throughout Mesmerize

mesmerize.common.qdialogs.present_exceptions(title: str = 'error', msg: str = 'The following error occurred.', help_func: callable = None)[source]

Use to catch exceptions and present them to the user in a QMessageBox warning dialog. The traceback from the exception is also shown.

Parameters:
  • title – Title of the dialog box
  • msg – Message to display above the traceback in the dialog box
  • help_func – A helper function which is called if the user clicked the “Help” button
mesmerize.common.qdialogs.exceptions_label(label: str, exception_holder: str = None, title: str = 'error', msg: str = 'The following error occured')[source]

Use a label to display an exception instead of a QMessageBox

Parameters:
  • label – name of a QLabel instance
  • exception_holder – name of an exception_holder attribute where the exception message is stored. This can be used to view the whole exception when the label is clicked on for example.
  • title – title supplied for the QMessageBox (if used later)
  • msg – message supplied for the QMessageBox (if used later)
mesmerize.common.qdialogs.use_open_file_dialog(title: str = 'Choose file', start_dir: Optional[str] = None, exts: List[str] = None)[source]

Use to pass a file path, for opening, into the decorated function using QFileDialog.getOpenFileName

Parameters:
  • title – Title of the dialog box
  • start_dir – Directory that is first shown in the dialog box.
  • exts – List of file extensions to set the filter in the dialog box
mesmerize.common.qdialogs.use_save_file_dialog(title: str = 'Save file', start_dir: Optional[str] = None, ext: str = None)[source]

Use to pass a file path, for saving, into the decorated function using QFileDialog.getSaveFileName

Parameters:
  • title – Title of the dialog box
  • start_dir – Directory that is first shown in the dialog box.
  • exts – List of file extensions to set the filter in the dialog box
mesmerize.common.qdialogs.use_open_dir_dialog(title: str = 'Open directory', start_dir: Optional[str] = None)[source]

Use to pass a dir path, to open, into the decorated function using QFileDialog.getExistingDirectory

Parameters:
  • title – Title of the dialog box
  • start_dir – Directory that is first shown in the dialog box.