Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

utils.file_helpers

utils.file_helpers

Attributes

NameDescription
DCM2NIIX_HINT

Classes

NameDescription
NiftiSeriesOne series’ main NIfTI, identified from its JSON sidecar rather than any folder name.

NiftiSeries

utils.file_helpers.NiftiSeries(series_id, nii_path, description)

One series’ main NIfTI, identified from its JSON sidecar rather than any folder name.

Attributes

NameDescription
description
nii_path
series_id

Functions

NameDescription
get_all_exam_series_idsRetrieve all exam series IDs from a specified folder.
get_all_nifti_series_idsRetrieve all series IDs that have a NIfTI + sidecar pair, and any gaps between them.
get_exam_folderFind a folder containing ‘Exam’ in its name within the specified base path.
get_exam_overviewBuild (and optionally print) an overview of all exam series in a data folder.
get_h5_data_from_seriesFind a specific .h5 file within a designated series folder.
get_latest_processing_fileGet the latest processing file for a given series ID.
get_mat_archivesList a series folder’s ScanArchive*.mat files, the raw archive first.
get_mat_data_from_seriesFind a specific .mat file within a designated series folder.
get_nifti_dataRetrieve a NIfTI file from a folder.
get_nifti_fileFind the first NIfTI file in a folder matching the given keyword filters.
get_niftis_from_seriesFind a series’ main NIfTI under a data folder, by the sidecar’s SeriesNumber.
index_nifti_seriesIndex every NIfTI series under a data folder by the SeriesNumber in its sidecar.
is_running_in_jupyterCheck if the code is being run within an IPython kernel (Jupyter Notebook or Console).
move_files_with_globMove files matching a glob pattern from a folder to a target folder.
print_hdf5_treeReturn a string containing the tree representation of the HDF5 file.

get_all_exam_series_ids

utils.file_helpers.get_all_exam_series_ids(exam_folder)

Retrieve all exam series IDs from a specified folder.

Scans the given folder for subdirectories whose names match the pattern “SeriesN”, where N is any number of digits, and that hold a ScanArchive*.mat. Series with only a raw .h5 archive are left out: they carry no series description to identify them by, but can still be loaded by explicit series ID.

Parameters

NameTypeDescriptionDefault
exam_folderstr or PathThe path to the folder containing the exam series.required

Returns

NameTypeDescription
list[int]A sorted list of integers representing the series IDs.

get_all_nifti_series_ids

utils.file_helpers.get_all_nifti_series_ids(data_folder)

Retrieve all series IDs that have a NIfTI + sidecar pair, and any gaps between them.

Parameters

NameTypeDescriptionDefault
data_folderstr or PathThe folder holding the converted series, searched recursively.required

Returns

NameTypeDescription
tuple[list[int], list[int]]A sorted list of the series IDs, and a list of missing series IDs, if any.

get_exam_folder

utils.file_helpers.get_exam_folder(base_path)

Find a folder containing ‘Exam’ in its name within the specified base path.

Parameters

NameTypeDescriptionDefault
base_pathstr or PathThe base directory to search.required

Returns

NameTypeDescription
PathThe Path object to the found ‘Exam’ folder.

Raises

NameTypeDescription
ValueErrorIf no ‘Exam’ folder is found or if multiple ‘Exam’ folders exist.

get_exam_overview

utils.file_helpers.get_exam_overview(data_folder, print_overview=True)

Build (and optionally print) an overview of all exam series in a data folder.

Series are found from the files, never from folder names: a NIfTI counts through the SeriesNumber in its JSON sidecar, raw data through a ScanArchive*.mat in an Exam*/SeriesN folder. A series’ description is the sidecar’s SeriesDescription when it has a NIfTI, else the .mat header’s se_desc.

Parameters

NameTypeDescriptionDefault
data_folderstr or PathThe data folder holding the converted NIfTIs and the Exam* folder.required
print_overviewboolWhether to print/display the overview. Defaults to True.True

Returns

NameTypeDescription
pandas.DataFrameA DataFrame containing the overview of all exam series, indexed by series ID, with “Has Exam”, “Has NIfTI” and “Description” columns.

get_h5_data_from_series

utils.file_helpers.get_h5_data_from_series(
    base_folder,
    series,
    file_start='ScanArchive',
)

Find a specific .h5 file within a designated series folder.

Parameters

NameTypeDescriptionDefault
base_folderstr or PathThe base directory.required
seriesintThe series number.required
file_startstrThe filename prefix to match. Defaults to “ScanArchive”.'ScanArchive'

Returns

NameTypeDescription
PathThe Path object to the first found .h5 file.

Raises

NameTypeDescription
ValueErrorIf base_folder does not exist or series is not a positive integer.
FileNotFoundErrorIf no .h5 files are found in the series folder.

get_latest_processing_file

utils.file_helpers.get_latest_processing_file(output_folder, series_id)

Get the latest processing file for a given series ID.

Parameters

NameTypeDescriptionDefault
output_folderPathThe output folder where processing files are stored.required
series_idintThe series ID to look for.required

Returns

NameTypeDescription
PathThe path to the latest processing file.

Raises

NameTypeDescription
FileNotFoundErrorIf no processing files are found for the given series ID.

get_mat_archives

utils.file_helpers.get_mat_archives(series_folder)

List a series folder’s ScanArchive*.mat files, the raw archive first.

Files derived from the archive (..._csi.mat, ..._snr_map.mat) share its name plus a suffix, so sorting by name length puts the plain archive first, and when only derived files exist, the one with the shortest suffix.

Parameters

NameTypeDescriptionDefault
series_folderstr or PathThe SeriesN folder inside the exam folder.required

Returns

NameTypeDescription
list[Path]The matching files, best candidate first; empty if there are none.

get_mat_data_from_series

utils.file_helpers.get_mat_data_from_series(base_folder, series)

Find a specific .mat file within a designated series folder.

Parameters

NameTypeDescriptionDefault
base_folderstr or PathThe base directory.required
seriesintThe series number.required

Returns

NameTypeDescription
PathThe Path object to the first found .mat file.

Raises

NameTypeDescription
ValueErrorIf base_folder does not exist or series is not a positive integer.
FileNotFoundErrorIf no .mat files are found in the series folder.

get_nifti_data

utils.file_helpers.get_nifti_data(folder, **kwargs)

Retrieve a NIfTI file from a folder.

Parameters

NameTypeDescriptionDefault
folderstr | PathThe folder path to search for NIfTI files.required
**kwargsAdditional keyword arguments to pass to get_nifti_file().{}

Returns

NameTypeDescription
PathThe path to a NIfTI file found in the folder.

Raises

NameTypeDescription
FileNotFoundErrorIf no NIfTI files are found in the folder.

Examples

>>> nifti_path = get_nifti_data('/path/to/series/folder')

get_nifti_file

utils.file_helpers.get_nifti_file(
    folder,
    filter_out_keywords=None,
    filter_in_keywords=None,
)

Find the first NIfTI file in a folder matching the given keyword filters.

Parameters

NameTypeDescriptionDefault
folderPathThe folder to search for NIfTI files.required
filter_out_keywordslist[str] or NoneKeywords whose presence in a filename excludes it. Defaults to [“bet”, “skullstrip”, “ss”, “resampled”] when None.None
filter_in_keywordslist[str] or NoneKeywords a filename must contain to be included. Defaults to no filtering when None.None

Returns

NameTypeDescription
Path or NoneThe first matching NIfTI file, or None if no file matches.

get_niftis_from_series

utils.file_helpers.get_niftis_from_series(base_folder, series)

Find a series’ main NIfTI under a data folder, by the sidecar’s SeriesNumber.

Parameters

NameTypeDescriptionDefault
base_folderstr or PathThe data folder, searched recursively.required
seriesintThe series number.required

Returns

NameTypeDescription
PathThe Path object to the series’ NIfTI.

Raises

NameTypeDescription
ValueErrorIf base_folder does not exist or series is not a positive integer.
FileNotFoundErrorIf no NIfTI with a JSON sidecar carries this series number.

index_nifti_series

utils.file_helpers.index_nifti_series(data_folder)

Index every NIfTI series under a data folder by the SeriesNumber in its sidecar.

A NIfTI counts when it has a JSON sidecar of the same name carrying SeriesNumber, so derived files without one (_bet, *_resampled_*) are never picked. When a series has several such pairs (echoes, dynamics), the one with the shortest, then lowest, file name wins and a warning names it.

Parameters

NameTypeDescriptionDefault
data_folderstr or PathThe folder holding the converted series, searched recursively.required

Returns

NameTypeDescription
dict[int, NiftiSeries]The main NIfTI of each series, sorted by series ID.

Raises

NameTypeDescription
ValueErrorIf data_folder does not exist.

is_running_in_jupyter

utils.file_helpers.is_running_in_jupyter()

Check if the code is being run within an IPython kernel (Jupyter Notebook or Console).

move_files_with_glob

utils.file_helpers.move_files_with_glob(
    folder,
    file_glob,
    target_folder_name='old',
    prepend_creation_date=True,
)

Move files matching a glob pattern from a folder to a target folder.

Parameters

NameTypeDescriptionDefault
folderstr or PathThe folder to search for files.required
file_globstrThe glob pattern to look for (e.g. ‘*.csv’), passed directly to Path.glob.required
target_folder_namestrThe name of the target folder, created inside folder. Defaults to “old”.'old'
prepend_creation_dateboolWhether to prepend the creation date to the filename when moving. Defaults to True.True

Raises

NameTypeDescription
ValueErrorIf the folder does not exist or is not a directory.

print_hdf5_tree

utils.file_helpers.print_hdf5_tree(hdf5_file, prefix='')

Return a string containing the tree representation of the HDF5 file.

Parameters

NameTypeDescriptionDefault
hdf5_fileh5py.File or h5py.GroupThe HDF5 file object.required
prefixstrThe line prefix, used for recursive indentation. Defaults to “”.''

Returns

NameTypeDescription
strThe full or partial tree representation of the file.