Skip to content
Snippets Groups Projects
Commit 98e181dc authored by Paul McCarthy's avatar Paul McCarthy :mountain_bicyclist:
Browse files

RF: Don't be lenient, error on bad components or confounds

parent 86ae7caa
No related branches found
No related tags found
No related merge requests found
...@@ -28,16 +28,16 @@ with warnings.catch_warnings(): ...@@ -28,16 +28,16 @@ with warnings.catch_warnings():
DTYPE = np.float64 DTYPE = np.float64
name = "extract_noise"
desc = 'Extract component time series from a MELODIC .ica directory'
usage = """ usage = """
{name}: {desc}
Usage: Usage:
extract_noise <.ica directory> [-o outfile] <fixfile> {name} <.ica directory> [-o outfile] <fixfile>
extract_noise <.ica directory> [-o outfile] <component> [<component> ...] {name} <.ica directory> [-o outfile] <component> [<component> ...]
""".strip() {name} <.ica directory> [-o outfile] [-c conffile] [-c conffile] <fixfile>
{name} <.ica directory> [-o outfile] [-c conffile] [-c conffile] <component> [<component> ...]
""".format(name=name, desc=desc).strip() # noqa
desc = 'Extract component time series from a MELODIC .ica directory'
helps = { helps = {
...@@ -69,7 +69,7 @@ def parseArgs(args): ...@@ -69,7 +69,7 @@ def parseArgs(args):
print(usage) print(usage)
sys.exit(0) sys.exit(0)
parser = argparse.ArgumentParser(prog='extract_noise', parser = argparse.ArgumentParser(prog=name,
usage=usage, usage=usage,
description=desc) description=desc)
...@@ -144,7 +144,6 @@ def genComponentIndexList(comps, ncomps): ...@@ -144,7 +144,6 @@ def genComponentIndexList(comps, ncomps):
""" """
allcomps = [] allcomps = []
badcomps = []
for c in comps: for c in comps:
if isinstance(c, int): if isinstance(c, int):
...@@ -152,12 +151,10 @@ def genComponentIndexList(comps, ncomps): ...@@ -152,12 +151,10 @@ def genComponentIndexList(comps, ncomps):
else: else:
ccomps = fixlabels.loadLabelFile(c, returnIndices=True)[2] ccomps = fixlabels.loadLabelFile(c, returnIndices=True)[2]
badcomps.extend([cc for cc in ccomps if cc >= ncomps]) allcomps.extend([c - 1 for c in ccomps])
allcomps.extend([cc - 1 for cc in ccomps if cc < ncomps])
if len(badcomps) > 0: if any([cc < 0 or cc >= ncomps for cc in ccomps]):
print('Warning: Ignoring components: {}'.format(badcomps), raise ValueError('Invalid component indices: {}'.format(ccomps))
file=sys.stderr)
return list(sorted(set(allcomps))) return list(sorted(set(allcomps)))
...@@ -185,32 +182,21 @@ def loadConfoundFiles(conffiles, npts): ...@@ -185,32 +182,21 @@ def loadConfoundFiles(conffiles, npts):
mat = np.atleast_2d(mat).T mat = np.atleast_2d(mat).T
if mat.shape[0] != npts: if mat.shape[0] != npts:
print('Warning: confound file {} does not have correct number of ' raise ValueError('Confound file {} does not have correct number '
'points (expected {}, has {}). Output will be truncated or ' 'of points (expected {}, has {})'.format(
'padded with NaNs.'.format(cfile, npts, mat.shape[1]), cfile, npts, mat.shape[0]))
file=sys.stderr)
matrices.append(mat)
totalcols = sum([m.shape[0] for m in matrices])
confounds = np.zeros((npts, totalcols), dtype=DTYPE)
coli = 0 matrices.append(mat)
for mat in matrices:
ncols = mat.shape[1]
# too many timepoints - truncate ncols = sum([m.shape[1] for m in matrices])
if mat.shape[0] > npts: confounds = np.zeros((npts, ncols), dtype=DTYPE)
mat = mat[:npts, :]
# too few timepoints - pad with NaN coli = 0
elif mat.shape[0] < npts:
tmat = mat
mat = np.zeros((npts, ncols), dtype=DTYPE)
mat[:npts, :] = tmat
mat[ npts:, :] = np.nan
confounds[:, coli:coli + ncols] = mat for mat in matrices:
coli = coli + ncols matcols = mat.shape[1]
confounds[:, coli:coli + matcols] = mat
coli = coli + matcols
return confounds return confounds
...@@ -226,12 +212,18 @@ def main(argv=None): ...@@ -226,12 +212,18 @@ def main(argv=None):
argv = sys.argv[1:] argv = sys.argv[1:]
args = parseArgs(argv) args = parseArgs(argv)
comps = genComponentIndexList(args.components)
ts = melanalysis.getComponentTimeSeries(args.icadir)
ts = ts[:, comps]
confs = loadConfoundFiles(args.conffiles, ts.shape[0])
ts = np.hstack((ts, confs))
try:
comps = genComponentIndexList(args.components)
ts = melanalysis.getComponentTimeSeries(args.icadir)
ts = ts[:, comps]
confs = loadConfoundFiles(args.conffiles, ts.shape[0])
except ValueError as e:
print(e)
sys.exit(1)
ts = np.hstack((ts, confs))
np.savetxt(args.outfile, ts, fmt='%10.5f') np.savetxt(args.outfile, ts, fmt='%10.5f')
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment