extract_sequence_from_filepath

extract_sequence_from_filepath#

extract_sequence_from_filepath(ifile, default=None)[source]#

Extract the sequence number from a filename stem.

Searches for the first occurrence of a 3- or 6-digit decimal run in the stem. The match is not anchored to a particular position so it works for both the strict MAS schema (br001001.h5) and looser naming conventions.

Parameters:
ifilePath

File path whose stem is examined.

defaultint or None, optional

Value to return when no 3- or 6-digit run is found. Defaults to None.

Returns:
outint or None

Integer sequence number, or default if no match is found.

Examples

>>> from pathlib import Path
>>> from psi_io.mhd_io import extract_sequence_from_filepath
>>> extract_sequence_from_filepath(Path('br001001.h5'))
1001
>>> extract_sequence_from_filepath(Path('vr001.h5'))
1
>>> extract_sequence_from_filepath(Path('nosequence.h5')) is None
True