diff --git a/fsl/data/fixlabels.py b/fsl/data/fixlabels.py index d5814d79b2b2f41bc2441eabe0bcf261d40ce06b..8974cc79c9c8971649814904e11fe7f6c0f32cb4 100644 --- a/fsl/data/fixlabels.py +++ b/fsl/data/fixlabels.py @@ -19,7 +19,10 @@ import os.path as op -def loadLabelFile(filename, includeLabel=None, excludeLabel=None): +def loadLabelFile(filename, + includeLabel=None, + excludeLabel=None, + returnIndices=False): """Loads component labels from the specified file. The file is assuemd to be of the format generated by FIX, Melview or ICA-AROMA; such a file should have a structure resembling the following:: @@ -70,23 +73,30 @@ def loadLabelFile(filename, includeLabel=None, excludeLabel=None): *bad* components, i.e. those components which are not classified as signal or unknown. - :arg filename: Name of the label file to load. - - :arg includeLabel: If the file contains a single line containing a list - component indices, this label will be used for the - components in the list. Defaults to 'Unclassified - noise' for FIX-like files, and 'Movement' for - ICA-AROMA-like files. - - :arg excludeLabel: If the file contains a single line containing component - indices, this label will be used for the components - that are not in the list. Defaults to 'Signal' for - FIX-like files, and 'Unknown' for ICA-AROMA-like files. - - :returns: A tuple containing the path to the melodic directory - as specified in the label file, and a list of lists, one - list per component, with each list containing the labels for - the corresponding component. + :arg filename: Name of the label file to load. + + :arg includeLabel: If the file contains a single line containing a list + component indices, this label will be used for the + components in the list. Defaults to 'Unclassified + noise' for FIX-like files, and 'Movement' for + ICA-AROMA-like files. + + :arg excludeLabel: If the file contains a single line containing component + indices, this label will be used for the components + that are not in the list. Defaults to 'Signal' for + FIX-like files, and 'Unknown' for ICA-AROMA-like files. + + :arg returnIndices: Defaults to ``False``. If ``True``, a list containing + the noisy component numbers that were listed in the + file is returned. + + :returns: A tuple containing: + - The path to the melodic directory as specified in the label + file + - A list of lists, one list per component, with each list + containing the labels for the corresponding component. + - If ``returnIndices is True``, a list of the noisy component + indices (starting from 1) that were specified in the file. """ signalLabels = None @@ -176,7 +186,7 @@ def loadLabelFile(filename, includeLabel=None, excludeLabel=None): try: compIdx = int(tokens[0]) - except: + except ValueError: raise InvalidLabelFileError( 'Invalid FIX classification file - ' 'line {}: {}'.format(i + 1, compLine)) @@ -203,7 +213,6 @@ def loadLabelFile(filename, includeLabel=None, excludeLabel=None): noise = isNoisyComponent(labels, signalLabels) if noise and (comp not in noisyComps): - print(signalLabels) raise InvalidLabelFileError('Noisy component {} has invalid ' 'labels: {}'.format(comp, labels)) @@ -217,7 +226,8 @@ def loadLabelFile(filename, includeLabel=None, excludeLabel=None): raise InvalidLabelFileError('Noisy component {} is missing ' 'a noise label'.format(comp)) - return melDir, allLabels + if returnIndices: return melDir, allLabels, noisyComps + else: return melDir, allLabels def saveLabelFile(allLabels,