-
Notifications
You must be signed in to change notification settings - Fork 21
/
filemanip.py
148 lines (126 loc) · 3.78 KB
/
filemanip.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# -*- coding: utf-8 -*-
# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*-
# vi: set ft=python sts=4 ts=4 sw=4 et:
"""Miscellaneous file manipulation functions."""
import os.path as op
import numpy as np
from nipype import logging
from nipype.utils.misc import is_container
fmlogger = logging.getLogger("nipype.utils")
related_filetype_sets = [(".hdr", ".img", ".mat"), (".nii", ".mat"), (".BRIK", ".HEAD")]
def split_filename(fname):
"""Split a filename into parts: path, base filename and extension.
Parameters
----------
fname : :obj:`str`
file or path name
Returns
-------
pth : :obj:`str`
base path from fname
fname : :obj:`str`
filename from fname, without extension
ext : :obj:`str`
file extension from fname
Examples
--------
>>> from nipype.utils.filemanip import split_filename
>>> pth, fname, ext = split_filename('/home/data/subject.nii.gz')
>>> pth
'/home/data'
>>> fname
'subject'
>>> ext
'.nii.gz'
"""
# TM 07152022 - edited to add cifti and workbench extensions
special_extensions = [
".nii.gz",
".tar.gz",
".niml.dset",
".dconn.nii",
".dlabel.nii",
".dpconn.nii",
".dscalar.nii",
".dtseries.nii",
".fiberTEMP.nii",
".trajTEMP.wbsparse",
".pconn.nii",
".pdconn.nii",
".plabel.nii",
".pscalar.nii",
".ptseries.nii",
".sdseries.nii",
".label.gii",
".label.gii",
".func.gii",
".shape.gii",
".rgba.gii",
".surf.gii",
".dpconn.nii",
".dtraj.nii",
".pconnseries.nii",
".pconnscalar.nii",
".dfan.nii",
".dfibersamp.nii",
".dfansamp.nii",
]
pth = op.dirname(fname)
fname = op.basename(fname)
ext = None
for special_ext in special_extensions:
ext_len = len(special_ext)
if (len(fname) > ext_len) and (fname[-ext_len:].lower() == special_ext.lower()):
ext = fname[-ext_len:]
fname = fname[:-ext_len]
break
if not ext:
fname, ext = op.splitext(fname)
return pth, fname, ext
def fname_presuffix(fname, prefix="", suffix="", newpath=None, use_ext=True):
"""Manipulate path and name of input filename.
Parameters
----------
fname : string
A filename (may or may not include path)
prefix : string
Characters to prepend to the filename
suffix : string
Characters to append to the filename
newpath : string
Path to replace the path of the input fname
use_ext : boolean
If True (default), appends the extension of the original file
to the output name.
Returns
-------
str
Absolute path of the modified filename
Examples
--------
>>> from nipype.utils.filemanip import fname_presuffix
>>> fname = 'foo.nii.gz'
>>> fname_presuffix(fname,'pre','post','/tmp')
'/tmp/prefoopost.nii.gz'
>>> from nipype.interfaces.base import Undefined
>>> fname_presuffix(fname, 'pre', 'post', Undefined) == \
fname_presuffix(fname, 'pre', 'post')
True
"""
pth, fname, ext = split_filename(fname)
if not use_ext:
ext = ""
# No need for isdefined: bool(Undefined) evaluates to False
if newpath:
pth = op.abspath(newpath)
return op.join(pth, prefix + fname + suffix + ext)
def ensure_list(filename):
"""Return a list given either a string or a list."""
if isinstance(filename, (str, bytes)):
return [filename]
elif isinstance(filename, (list, tuple, type(None), np.ndarray)):
return filename
elif is_container(filename):
return [x for x in filename]
else:
return None