Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
F
fslpy
Manage
Activity
Members
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Analyze
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
FSL
fslpy
Commits
5c4cf4c8
Commit
5c4cf4c8
authored
8 years ago
by
Paul McCarthy
Browse files
Options
Downloads
Patches
Plain Diff
Feels ugly to me. getFileGroups has the option to raise an error if the
path looks like part of an incomplete file group.
parent
ea619b7f
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
fsl/utils/imcp.py
+11
-11
11 additions, 11 deletions
fsl/utils/imcp.py
fsl/utils/path.py
+44
-10
44 additions, 10 deletions
fsl/utils/path.py
with
55 additions
and
21 deletions
fsl/utils/imcp.py
+
11
−
11
View file @
5c4cf4c8
...
...
@@ -118,8 +118,8 @@ def imcp(src,
# match the destination file type,
# we need to perform a conversion.
#
# This is expensive in terms of
io
# and cpu, but programmatically
# This is
more
expensive in terms of
#
io
and cpu, but programmatically
# very easy - nibabel does all the
# hard work.
if
srcExt
!=
destExt
:
...
...
@@ -138,7 +138,7 @@ def imcp(src,
# Otherwise we do a file copy. This
# is actually more complicated than
# conveting the file type due to
# conve
r
ting the file type due to
# hdr/img pairs ...
#
# If the source is part of a file group,
...
...
@@ -154,19 +154,19 @@ def imcp(src,
# (base, ext) tuples, so we don't
# have to re-split when creating
# destination paths.
#
# The unambiguous flag tells getFileGroup
# to raise an error if the source appears
# to be part of an incopmlete file group
# (e.g. file.hdr without an accompanying
# file.img).
copySrcs
=
fslpath
.
getFileGroup
(
src
,
fslimage
.
ALLOWED_EXTENSIONS
,
fslimage
.
FILE_GROUPS
,
fullPaths
=
False
)
fullPaths
=
False
,
unambiguous
=
True
)
copySrcs
=
[(
srcBase
,
e
)
for
e
in
copySrcs
]
# Note that these additional files
# do not have to exist, e.g.
# imcp('blah.img', ...) will still
# work if there is no blah.hdr
copySrcs
=
[(
b
,
e
)
for
(
b
,
e
)
in
copySrcs
if
op
.
exists
(
b
+
e
)]
# Build a list of destinations for each
# copy source - we build this list in
# advance, so we can fail if any of the
...
...
This diff is collapsed.
Click to expand it.
fsl/utils/path.py
+
44
−
10
View file @
5c4cf4c8
...
...
@@ -226,7 +226,11 @@ def splitExt(filename, allowedExts=None):
return
filename
[:
-
extLen
],
filename
[
-
extLen
:]
def
getFileGroup
(
path
,
allowedExts
=
None
,
fileGroups
=
None
,
fullPaths
=
True
):
def
getFileGroup
(
path
,
allowedExts
=
None
,
fileGroups
=
None
,
fullPaths
=
True
,
unambiguous
=
False
):
"""
If the given ``path`` is part of a ``fileGroup``, returns a list
containing the paths to all other files in the group (including the
``path`` itself).
...
...
@@ -273,6 +277,11 @@ def getFileGroup(path, allowedExts=None, fileGroups=None, fullPaths=True):
:arg fullPaths: If ``True`` (the default), full file paths (relative to
the ``path``) are returned. Otherwise, only the file
extensions in the group are returned.
:arg unambiguous: Defaults to ``False``. If ``True``, and the path
is not unambiguouosly part of one group, or part of
no groups, a :exc:`PathError` is raised.
Otherwise, the path is returned.
"""
path
=
addExt
(
path
,
allowedExts
,
mustExist
=
True
,
fileGroups
=
fileGroups
)
...
...
@@ -284,6 +293,8 @@ def getFileGroup(path, allowedExts=None, fileGroups=None, fullPaths=True):
matchedGroups
=
[]
matchedGroupFiles
=
[]
fullMatches
=
0
partialMatches
=
0
for
group
in
fileGroups
:
...
...
@@ -291,29 +302,52 @@ def getFileGroup(path, allowedExts=None, fileGroups=None, fullPaths=True):
continue
groupFiles
=
[
base
+
s
for
s
in
group
]
exist
=
[
op
.
exists
(
f
)
for
f
in
groupFiles
]
if
not
all
([
op
.
exists
(
f
)
for
f
in
groupFiles
]):
continue
if
any
(
exist
):
partialMatches
+=
1
if
all
(
exist
):
fullMatches
+=
1
else
:
continue
matchedGroups
.
append
(
group
)
matchedGroupFiles
.
append
(
groupFiles
)
if
len
(
matchedGroupFiles
)
==
1
:
if
fullPaths
:
return
matchedGroupFiles
[
0
]
else
:
return
matchedGroups
[
0
]
# Path is not part of any group
el
if
len
(
matchedGroupFiles
)
==
0
:
if
partialMatches
==
0
:
if
fullPaths
:
return
[
path
]
else
:
return
[
ext
]
# If the given path is part of more
# than one existing file group, we
# can't resolve this ambiguity.
else
:
if
fullMatches
>
1
:
raise
PathError
(
'
Path is part of multiple
'
'
file groups: {}
'
.
format
(
path
))
# If the unambiguous flag is not set,
# we don't care about partial matches
if
not
unambiguous
:
partialMatches
=
0
# The path is unambiguously part of a
# complete file group - resolve it to
# the first element of the group
if
fullMatches
==
1
and
partialMatches
==
0
:
if
fullPaths
:
return
matchedGroupFiles
[
0
]
else
:
return
matchedGroups
[
0
]
# The path appears to be part of
# an incomplete group - this is
# potentially ambiguuuos, so give
# up (but see the partialMatches
# clobber above).
elif
partialMatches
>
0
:
raise
PathError
(
'
Path is part of an incomplete
'
'
file group: {}
'
.
format
(
path
))
else
:
if
fullPaths
:
return
[
path
]
else
:
return
[
ext
]
def
removeDuplicates
(
paths
,
allowedExts
=
None
,
fileGroups
=
None
):
"""
Reduces the list of ``paths`` down to those which are unique with
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment