def splitFilename(filename):
    """Split a NIfTI filename into basename and extension.

    :Parameters:
        filename: str
            Filename to be split.

    :Returns:
      tuple: (basename, extension)
        The function returns a tuple of basename and extension. If no valid
        NIfTI filename extension is found, the whole string is returned as
        basename and the extension string will be empty.
    """
    parts = filename.split('.')

    if parts[-1] == 'gz':
        if not parts[-2] in [ 'nii', 'hdr', 'img' ]:
            return filename, ''
        else:
            return '.'.join(parts[:-2]), '.'.join(parts[-2:])
    else:
        if not parts[-1] in [ 'nii', 'hdr', 'img' ]:
            return filename, ''
        else:
            return '.'.join(parts[:-1]), parts[-1]