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
60614f25
Commit
60614f25
authored
5 years ago
by
Paul McCarthy
Browse files
Options
Downloads
Patches
Plain Diff
BF: FileTreeQuery was not handling "scalar" templates, i.e. templates
which do not vary, and only match zero or one file.
parent
287207e5
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
fsl/utils/filetree/query.py
+24
-13
24 additions, 13 deletions
fsl/utils/filetree/query.py
with
24 additions
and
13 deletions
fsl/utils/filetree/query.py
+
24
−
13
View file @
60614f25
...
@@ -6,7 +6,7 @@
...
@@ -6,7 +6,7 @@
# Author: Michiel Cottaar <michiel.cottaar@.ndcn.ox.ac.uk>
# Author: Michiel Cottaar <michiel.cottaar@.ndcn.ox.ac.uk>
#
#
"""
This module contains the :class:`FileTreeQuery` class, which can be used to
"""
This module contains the :class:`FileTreeQuery` class, which can be used to
search for files in a directory described by a `.FileTree`. A
search for files in a directory described by a
:class:
`.FileTree`. A
``FileTreeQuery`` object returns :class:`Match` objects which each represent a
``FileTreeQuery`` object returns :class:`Match` objects which each represent a
file that is described by the ``FileTree``, and which is present in the
file that is described by the ``FileTree``, and which is present in the
directory.
directory.
...
@@ -22,8 +22,9 @@ defined in this module:
...
@@ -22,8 +22,9 @@ defined in this module:
"""
"""
import
logging
import
logging
import
collections
import
collections
import
functools
as
ft
import
os.path
as
op
import
os.path
as
op
from
typing
import
Dict
,
List
,
Tuple
from
typing
import
Dict
,
List
,
Tuple
...
@@ -121,6 +122,12 @@ class FileTreeQuery(object):
...
@@ -121,6 +122,12 @@ class FileTreeQuery(object):
tvarlens
=
[
len
(
allvars
[
v
])
for
v
in
tvars
]
tvarlens
=
[
len
(
allvars
[
v
])
for
v
in
tvars
]
# "Scalar" match objects - templates
# which have no variables, and for
# which zero or one file is present
if
len
(
tvarlens
)
==
0
:
tvarlens
=
1
# An ND array for this short
# An ND array for this short
# name. Each element is a
# name. Each element is a
# Match object, or nan.
# Match object, or nan.
...
@@ -142,10 +149,13 @@ class FileTreeQuery(object):
...
@@ -142,10 +149,13 @@ class FileTreeQuery(object):
tvaridxs
=
varidxs
[
match
.
full_name
]
tvaridxs
=
varidxs
[
match
.
full_name
]
tarr
=
matcharrays
[
match
.
full_name
]
tarr
=
matcharrays
[
match
.
full_name
]
idx
=
[]
idx
=
[]
for
var
in
tvars
:
val
=
match
.
variables
[
var
]
if
len
(
match
.
variables
)
==
0
:
idx
.
append
(
tvaridxs
[
var
][
val
])
idx
=
[
0
]
else
:
for
var
in
tvars
:
val
=
match
.
variables
[
var
]
idx
.
append
(
tvaridxs
[
var
][
val
])
tarr
[
tuple
(
idx
)]
=
match
tarr
[
tuple
(
idx
)]
=
match
...
@@ -253,6 +263,7 @@ class FileTreeQuery(object):
...
@@ -253,6 +263,7 @@ class FileTreeQuery(object):
else
:
return
[
m
for
m
in
result
.
flat
if
isinstance
(
m
,
Match
)]
else
:
return
[
m
for
m
in
result
.
flat
if
isinstance
(
m
,
Match
)]
@ft.total_ordering
class
Match
(
object
):
class
Match
(
object
):
"""
A ``Match`` object represents a file with a name matching a template in
"""
A ``Match`` object represents a file with a name matching a template in
a ``FileTree``. The :func:`scan` function and :meth:`FileTree.query`
a ``FileTree``. The :func:`scan` function and :meth:`FileTree.query`
...
@@ -338,10 +349,6 @@ class Match(object):
...
@@ -338,10 +349,6 @@ class Match(object):
return
isinstance
(
other
,
Match
)
and
self
.
filename
<
other
.
filename
return
isinstance
(
other
,
Match
)
and
self
.
filename
<
other
.
filename
def
__le__
(
self
,
other
):
return
isinstance
(
other
,
Match
)
and
self
.
filename
<=
other
.
filename
def
__repr__
(
self
):
def
__repr__
(
self
):
"""
Returns a string representation of this ``Match``.
"""
"""
Returns a string representation of this ``Match``.
"""
return
'
Match({}: {})
'
.
format
(
self
.
full_name
,
self
.
filename
)
return
'
Match({}: {})
'
.
format
(
self
.
full_name
,
self
.
filename
)
...
@@ -397,9 +404,13 @@ def allVariables(
...
@@ -397,9 +404,13 @@ def allVariables(
containing the variables which are relevant to each template.
containing the variables which are relevant to each template.
"""
"""
allvars
=
collections
.
defaultdict
(
set
)
allvars
=
collections
.
defaultdict
(
set
)
alltemplates
=
collections
.
defaultdict
(
set
)
alltemplates
=
{}
for
m
in
matches
:
for
m
in
matches
:
if
m
.
full_name
not
in
alltemplates
:
alltemplates
[
m
.
full_name
]
=
set
()
for
var
,
val
in
m
.
variables
.
items
():
for
var
,
val
in
m
.
variables
.
items
():
allvars
[
var
]
.
add
(
val
)
allvars
[
var
]
.
add
(
val
)
alltemplates
[
m
.
full_name
].
add
(
var
)
alltemplates
[
m
.
full_name
].
add
(
var
)
...
@@ -411,7 +422,7 @@ def allVariables(
...
@@ -411,7 +422,7 @@ def allVariables(
allvars
=
{
var
:
list
(
sorted
(
vals
,
key
=
key
))
allvars
=
{
var
:
list
(
sorted
(
vals
,
key
=
key
))
for
var
,
vals
in
allvars
.
items
()}
for
var
,
vals
in
allvars
.
items
()}
alltemplates
=
{
s
n
:
list
(
sorted
(
vars
))
alltemplates
=
{
t
n
:
list
(
sorted
(
vars
))
for
s
n
,
vars
in
alltemplates
.
items
()}
for
t
n
,
vars
in
alltemplates
.
items
()}
return
allvars
,
alltemplates
return
allvars
,
alltemplates
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