ds003 - model 1#

%load_ext autoreload
%autoreload 2

import json
from pathlib import Path
from itertools import chain

import numpy as np
import pandas as pd

Preamble#

We will use Datalad to manage our data, which in turn depends on git-annex.

! datalad get ds000003-fmriprep/sub-*/func/*_desc-confounds_*.tsv \
              ds000003-fmriprep/sub-*/func/*_desc-confounds_*.json \
              ds000003-fmriprep/dataset_description.json 
[ERROR  ] No working git-annex installation of version >= 8.20200309. Visit http://handbook.datalad.org/r.html?install for instructions on how to install DataLad and git-annex. 
from nilearn.plotting import plot_design_matrix

import bids
from bids.modeling import BIDSStatsModelsGraph
from bids.layout import BIDSLayout


def api(obj):
    return {attr: getattr(obj, attr) for attr in dir(obj) if not attr[0] == '_'}
layout = BIDSLayout('./ds000003', derivatives='./ds000003-fmriprep')
---------------------------------------------------------------------------
NotADirectoryError                        Traceback (most recent call last)
Cell In[4], line 1
----> 1 layout = BIDSLayout('./ds000003', derivatives='./ds000003-fmriprep')

File ~/work/model-zoo/model-zoo/src/pybids/bids/layout/layout.py:182, in BIDSLayout.__init__(self, root, validate, absolute_paths, derivatives, config, sources, regex_search, database_path, reset_database, indexer, is_derivative, **indexer_kwargs)
    180 if derivatives is True:
    181     derivatives = root / 'derivatives'
--> 182 self.add_derivatives(
    183     derivatives, parent_database_path=database_path,
    184     validate=validate, absolute_paths=absolute_paths,
    185     derivatives=None, sources=self, config=None,
    186     regex_search=regex_search, reset_database=reset_database,
    187     **indexer_kwargs)

File ~/work/model-zoo/model-zoo/src/pybids/bids/layout/layout.py:534, in BIDSLayout.add_derivatives(self, path, parent_database_path, **kwargs)
    529 if name in self.derivatives:
    530     raise BIDSDerivativesValidationError(
    531         f"Pipeline name {name} ({path!s}) has already been added to this "
    532         "BIDSLayout. Every added pipeline must have a unique name!"
    533     )
--> 534 self.derivatives[name] = BIDSLayout(path, is_derivative=True, **kwargs)

File ~/work/model-zoo/model-zoo/src/pybids/bids/layout/layout.py:176, in BIDSLayout.__init__(self, root, validate, absolute_paths, derivatives, config, sources, regex_search, database_path, reset_database, indexer, is_derivative, **indexer_kwargs)
    172     if indexer is None:
    173         indexer = BIDSLayoutIndexer(
    174             validate=validate and not is_derivative, **indexer_kwargs
    175         )
--> 176     indexer(self)
    178 # Add derivatives if any are found
    179 if derivatives:

File ~/work/model-zoo/model-zoo/src/pybids/bids/layout/index.py:150, in BIDSLayoutIndexer.__call__(self, layout)
    148 self._index_dir(self._layout._root, self._config)
    149 if self.index_metadata:
--> 150     self._index_metadata()

File ~/work/model-zoo/model-zoo/src/pybids/bids/layout/index.py:403, in BIDSLayoutIndexer._index_metadata(self)
    401 for pl, js_file in payloads[::-1]:
    402     try:
--> 403         file_md.update(pl())
    404     except FileNotFoundError:
    405         if not virtual_datafile:

File ~/work/model-zoo/model-zoo/src/pybids/bids/layout/index.py:293, in BIDSLayoutIndexer._index_metadata.<locals>.load_json(path)
    291 @lru_cache(maxsize=None)
    292 def load_json(path):
--> 293     with open(path, 'r', encoding='utf-8') as handle:
    294         try:
    295             return json.load(handle)

NotADirectoryError: [Errno 20] Not a directory: '/home/runner/work/model-zoo/model-zoo/model-zoo/exhibits/ds003/ds000003-fmriprep/sub-01/func/sub-01_task-rhymejudgment_desc-confounds_timeseries.json'
json_file = 'model-001_smdl.json'
spec = json.loads(Path(json_file).read_text())
spec
layout
graph = BIDSStatsModelsGraph(layout, spec)
graph.load_collections(scan_length=320) # Set scan_length in case images not available
graph.run_graph(transformation_history=True, node_reports=True, missing_values='fill')
collections = layout.get_collections('run', task='rhymejudgment', scan_length=320)
root_node = graph.root_node
root_node.get_collections()[-1].entities
specs = root_node.outputs_
len(specs)
specs[0]
plot_design_matrix(specs[0].X, rescale=False)