Skip to content

Commit

Permalink
Merge pull request #777 from tsalo/dds-config
Browse files Browse the repository at this point in the history
Pass custom configuration into DerivativesDataSink entity parser
  • Loading branch information
effigies authored Feb 16, 2023
2 parents bf9821d + f08e74e commit 19fc90c
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 7 deletions.
2 changes: 1 addition & 1 deletion niworkflows/data/nipreps.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
},
{
"name": "run",
"pattern": "[_/\\\\]+run-0*(\\d+)",
"pattern": "[_/\\\\]+run-(\\d+)",
"dtype": "int"
},
{
Expand Down
17 changes: 13 additions & 4 deletions niworkflows/interfaces/bids.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@

regz = re.compile(r"\.gz$")
_pybids_spec = loads(Path(_pkgres("niworkflows", "data/nipreps.json")).read_text())
BIDS_DERIV_ENTITIES = frozenset({e["name"] for e in _pybids_spec["entities"]})
BIDS_DERIV_ENTITIES = _pybids_spec["entities"]
BIDS_DERIV_PATTERNS = tuple(_pybids_spec["default_path_patterns"])

STANDARD_SPACES = tf.api.templates()
Expand Down Expand Up @@ -498,7 +498,8 @@ class DerivativesDataSink(SimpleInterface):
output_spec = _DerivativesDataSinkOutputSpec
out_path_base = "niworkflows"
_always_run = True
_config_entities = BIDS_DERIV_ENTITIES
_config_entities = frozenset({e["name"] for e in BIDS_DERIV_ENTITIES})
_config_entities_dict = BIDS_DERIV_ENTITIES
_standard_spaces = STANDARD_SPACES
_file_patterns = BIDS_DERIV_PATTERNS
_default_dtypes = DEFAULT_DTYPES
Expand Down Expand Up @@ -526,7 +527,7 @@ def __init__(self, allowed_entities=None, out_path_base=None, **inputs):
setattr(self.inputs, k, inputs[k])

def _run_interface(self, runtime):
from bids.layout import parse_file_entities
from bids.layout import parse_file_entities, Config
from bids.layout.writing import build_path
from bids.utils import listify

Expand All @@ -549,8 +550,16 @@ def _run_interface(self, runtime):
self._metadata = meta

# Initialize entities with those from the source file.
custom_config = Config(
name="custom",
entities=self._config_entities_dict,
default_path_patterns=self._file_patterns,
)
in_entities = [
parse_file_entities(str(relative_to_root(source_file)))
parse_file_entities(
str(relative_to_root(source_file)),
config=["bids", "derivatives", custom_config],
)
for source_file in self.inputs.source_file
]
out_entities = {k: v for k, v in in_entities[0].items()
Expand Down
24 changes: 22 additions & 2 deletions niworkflows/reports/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,26 @@
]


class Smallest:
"""An object that always evaluates smaller than anything else, for sorting
>>> Smallest() < 1
True
>>> Smallest() < "epsilon"
True
>>> sorted([1, None, 2], key=lambda x: x if x is not None else Smallest())
[None, 1, 2]
"""
def __lt__(self, other):
return not isinstance(other, Smallest)

def __eq__(self, other):
return isinstance(other, Smallest)

def __gt__(self, other):
return False


class Element(object):
"""Just a basic component of a report"""

Expand Down Expand Up @@ -252,7 +272,7 @@ class Report:
>>> robj.generate_report()
0
>>> len((testdir / 'out' / 'fmriprep' / 'sub-01.html').read_text())
36693
36713
.. testcleanup::
Expand Down Expand Up @@ -483,7 +503,7 @@ def _process_orderings(orderings, layout):
# sort the value combinations alphabetically from the first entity to the last entity
value_combos.sort(
key=lambda entry: tuple(
str(value) if value is not None else "0" for value in entry
value if value is not None else Smallest() for value in entry
)
)

Expand Down

0 comments on commit 19fc90c

Please sign in to comment.