nipoppy.utils.utils

Utility functions.

Module Contents

nipoppy.utils.utils.add_path_suffix(path, suffix, sep='-')

Add a suffix to a path, before the last file extension (if any).

Parameters:
  • path (nipoppy.env.StrOrPathLike)

  • suffix (str)

Return type:

pathlib.Path

nipoppy.utils.utils.add_path_timestamp(path, timestamp_format='%Y%m%d_%H%M', sep='-')

Add a timestamp to a path, before the last file extension (if any).

Parameters:

path (nipoppy.env.StrOrPathLike)

Return type:

pathlib.Path

nipoppy.utils.utils.apply_substitutions_to_json(json_obj, substitutions)

Apply substitutions to a JSON object.

Parameters:
Return type:

dict | list

nipoppy.utils.utils.get_pipeline_tag(pipeline_name, pipeline_version, pipeline_step=None, participant_id=None, session_id=None, sep='-')

Generate a tag for a pipeline.

Parameters:
  • pipeline_name (str)

  • pipeline_version (str)

  • pipeline_step (Optional[str])

  • participant_id (Optional[str])

  • session_id (Optional[str])

nipoppy.utils.utils.get_today()

Get today’s date in the format YYYY-MM-DD.

nipoppy.utils.utils.is_nipoppy_project(cwd=Path.cwd())

Verify if the current directory is a nipoppy project.

This is done by checking if the .nipoppy directory exists in the current directory or any of its parents. If the directory is found, it returns the path to the .nipoppy directory. If not, it returns False.

Parameters:

cwd (nipoppy.env.StrOrPathLike, optional) – Path to directory, by default Path.cwd()

nipoppy.utils.utils.load_json(fpath, **kwargs)

Load a JSON file.

Parameters:
  • fpath (nipoppy.env.StrOrPathLike) – Path to the JSON file

  • **kwargs – Keyword arguments to pass to json.load

Returns:

The JSON object.

Return type:

dict

nipoppy.utils.utils.process_template_str(template_str, resolve_paths=True, objs=None, **kwargs)

Replace template strings with values from kwargs or objects.

Parameters:

template_str (str)

Return type:

str

nipoppy.utils.utils.save_df_with_backup(df, fpath_symlink, dname_backups=None, use_relative_path=True, dry_run=False, **kwargs)

Save a dataframe as a symlink pointing to a timestamped “backup” file.

Parameters:
  • df (pd.DataFrame) – The dataframe to save

  • fpath_symlink (nipoppy.env.StrOrPathLike) – The path to the symlink

  • dname_backups (Optional[str], optional) – The directory where the timestamped backup file should be written (automatically determined if None), by default None

  • use_relative_path (bool, optional) – Use relative instead of absolute path for the symlink, by default True

  • dry_run (bool, optional) – Return the file path but do not save the file, by default False

Returns:

None if no file was saved, otherwise the path to the backup file

Return type:

Path or None

nipoppy.utils.utils.save_json(obj, fpath, **kwargs)

Save a JSON object to a file.

Parameters:
  • obj (dict) – The JSON object

  • fpath (nipoppy.env.StrOrPathLike) – Path to the JSON file to write

  • indent (int, optional) – Indentation level, by default 4

  • **kwargs – Keyword arguments to pass to json.dump

nipoppy.utils.utils.DPATH_DATA
nipoppy.utils.utils.DPATH_EXAMPLES
nipoppy.utils.utils.DPATH_HPC
nipoppy.utils.utils.DPATH_LAYOUTS
nipoppy.utils.utils.FIELD_DESCRIPTION_MAP
nipoppy.utils.utils.FPATH_DEFAULT_LAYOUT
nipoppy.utils.utils.FPATH_HPC_TEMPLATE
nipoppy.utils.utils.FPATH_SAMPLE_CONFIG
nipoppy.utils.utils.FPATH_SAMPLE_DICOM_DIR_MAP
nipoppy.utils.utils.FPATH_SAMPLE_MANIFEST
nipoppy.utils.utils.NIPOPPY_ROOT
nipoppy.utils.utils.TEMPLATE_PIPELINE_PATH
nipoppy.utils.utils.TEMPLATE_REPLACE_PATTERN