-
Notifications
You must be signed in to change notification settings - Fork 124
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[WIP] Add support for experimental design aggregation #724
Draft
leej3
wants to merge
38
commits into
master
Choose a base branch
from
add_design_aggregator
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
38 commits
Select commit
Hold shift + click to select a range
27c21bf
add initial prototype of design aggregation tool
leej3 bc6cd91
tidy arg parsing
leej3 bba5284
move get_events_collection_to variables.io
Shotgunosine 0d2a888
fix data path during testing
leej3 4e41306
fix event file reading
leej3 14d32df
tidy get_events_collection
leej3 0fb031b
move loading of regressor into function
leej3 8ba2902
move loading of phys and stim files into function
leej3 605c2c0
output sampling rate not needed for reading input
leej3 b115bc0
move source logic out of get_rec_collection
leej3 63f84a5
will not drop na in records or reg collections for now
leej3 1ee5de1
use tempdir for output during test
leej3 31776fa
remove output-tsv arg and start sparse/dense saving
leej3 bc39cd6
have tfm manager check for densification or deletion of sparse variables
Shotgunosine 044386b
parametrize tests
leej3 ae83df9
remove stutter
leej3 a8fb923
add test for sampling rate with associated fix
leej3 ce7a50b
move test output to the pytest temp dir
leej3 810f29e
oops
leej3 76c0c54
consider the sparse variables
leej3 a2fba92
correct indentation bug
leej3 4a6dac0
update TODOs
leej3 1094c2f
fix sparse var saving
Shotgunosine e1a977a
more fixes for sparse/dense
leej3 1889e41
add model with convolution
leej3 a1764f1
Fix sparse variable filtering
Shotgunosine 5369664
fix check columns in output dataframes
Shotgunosine 34a209f
use click for cli
leej3 3c57020
enh don't rely on run node for get events collection
Shotgunosine 979ec10
enh remove run node from rec and reg loading
Shotgunosine 69c3720
remove params, kwargs no longer captured in params
leej3 02cd6fc
add transforms reading function
leej3 a9ae623
add additional support for transformation parsing
leej3 857c5e7
Apply suggestions from code review
520bab8
Merge remote-tracking branch 'origin/master' into add_design_aggregator
leej3 6344816
rename and move to cli
leej3 bb47b4c
make ta default to tr
leej3 14391a9
improve parsing of transforms_in
leej3 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,7 +13,7 @@ | |
"layout", | ||
"reports", | ||
"utils", | ||
"variables" | ||
"variables", | ||
] | ||
|
||
due.cite(Doi("10.1038/sdata.2016.44"), | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
#! /usr/bin/env python | ||
import argparse | ||
import sys | ||
import json | ||
from pathlib import Path | ||
import pandas as pd | ||
import numpy as np | ||
from collections import namedtuple | ||
from bids.modeling import transformations | ||
from bids.utils import convert_JSON | ||
from bids.variables import BIDSRunVariableCollection, SparseRunVariable, merge_collections | ||
from bids.layout.utils import parse_file_entities | ||
from bids.variables.io import get_events_collection, parse_transforms | ||
from bids.variables.entities import RunNode | ||
|
||
|
||
def morphing_time( | ||
*, | ||
events_tsv, | ||
transforms, | ||
nvol, | ||
tr, | ||
ta=None, | ||
output_sampling_rate=None, | ||
output_dir=None, | ||
): | ||
|
||
output_dir = Path(output_dir or "design_synthesizer") | ||
output_dir.mkdir(exist_ok=True) | ||
model_transforms = parse_transforms(transforms) | ||
duration = nvol * tr | ||
ta = ta or tr | ||
|
||
# Get relevant collection | ||
coll_df = pd.read_csv(events_tsv, delimiter="\t") | ||
RunInfo = namedtuple('RunInfo', ['entities', 'duration', 'tr', 'image', 'n_vols']) | ||
|
||
#run_info = RunInfo(parse_file_entities(events_tsv), duration) | ||
# TODO: this will need to be implemented without RunNode to break cyclic | ||
# dependencies if transformations is to be extracted | ||
run_info = RunInfo(parse_file_entities(events_tsv), duration, tr, None, nvol) | ||
coll = BIDSRunVariableCollection(get_events_collection(coll_df, run_info)) | ||
|
||
# perform transformations, additionally save variables that were changed. | ||
# If a column is transformed but not densified it will not be in | ||
# colls_pre_densification. | ||
colls, colls_pre_densification = ( | ||
transformations.TransformerManager(save_pre_dense=True) | ||
.transform(coll, model_transforms) | ||
) | ||
|
||
# Save sparse vars | ||
if colls_pre_densification is not None: | ||
final_sparse_colls = BIDSRunVariableCollection(colls.get_sparse_variables()) | ||
final_sparse_names = set([vv for vv in final_sparse_colls.variables]) | ||
pre_dense_names = set([vv for vv in colls_pre_densification.variables]) | ||
shared_names = final_sparse_names.intersection(pre_dense_names) | ||
|
||
if len(shared_names) > 0: | ||
raise ValueError( | ||
f"""Somehow you've ended up with a copy of {shared_names} in both the final | ||
transformed variables and in the pre-densification variables. Did you delete a | ||
variable and recreate one with same name?""" | ||
) | ||
output = merge_collections( | ||
[colls_pre_densification, final_sparse_colls] | ||
) | ||
assert output.all_sparse() | ||
|
||
df_sparse = output.to_df() | ||
else: | ||
df_sparse = colls.to_df(include_dense=False) | ||
|
||
df_sparse.to_csv(output_dir / "transformed_events.tsv", index=None, sep="\t", na_rep="n/a") | ||
# Save dense vars | ||
try: | ||
df_dense = colls.to_df(include_sparse=False) | ||
df_dense.to_csv(output_dir / "transformed_time_series.tsv", index=None, sep="\t", na_rep="n/a") | ||
except ValueError: | ||
pass | ||
|
||
# Save full design_matrix | ||
if output_sampling_rate: | ||
df_full = colls.to_df(sampling_rate=output_sampling_rate) | ||
df_full.to_csv(output_dir / "aggregated_design.tsv", index=None, sep="\t", na_rep="n/a") | ||
|
109 changes: 109 additions & 0 deletions
109
bids/tests/data/ds005/models/ds-005_type-convolution_model.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
{ | ||
"Name": "test_model", | ||
"Description": "simple test model", | ||
"Nodes": [ | ||
{ | ||
"Name": "run", | ||
"Level": "Run", | ||
"GroupBy": [ | ||
"run", | ||
"subject" | ||
], | ||
"Model": { | ||
"X": [ | ||
"RT", | ||
"gain" | ||
], | ||
"Formula": "1 + RT * gain" | ||
}, | ||
"Transformations": [ | ||
{ | ||
"Name": "Factor", | ||
"Input": "trial_type" | ||
}, | ||
{ | ||
"Name": "Rename", | ||
"Input": "trial_type.parametric gain", | ||
"Output": "gain" | ||
}, | ||
{ | ||
"Name": "Threshold", | ||
"Input": "respcat", | ||
"Output": "pos_respcat", | ||
"Binarize": true | ||
}, | ||
{ | ||
"Name": "Scale", | ||
"Input": "RT" | ||
}, | ||
{ | ||
"Name": "Convolve", | ||
"Input": ["gain", "pos_respcat"], | ||
"Model": "spm" | ||
} | ||
], | ||
"DummyContrasts": { | ||
"Test": "t" | ||
} | ||
}, | ||
{ | ||
"Name": "participant", | ||
"Level": "Subject", | ||
"Model": { | ||
"X": [ | ||
1 | ||
] | ||
}, | ||
"DummyContrasts": { | ||
"Type": "FEMA" | ||
} | ||
}, | ||
{ | ||
"Name": "by-group", | ||
"Level": "Dataset", | ||
"Model": { | ||
"X": [ | ||
"@intercept" | ||
] | ||
}, | ||
"DummyContrasts": { | ||
"Type": "t" | ||
} | ||
}, | ||
{ | ||
"Name": "group-diff", | ||
"Level": "Dataset", | ||
"Model": { | ||
"X": [ | ||
"@intercept", | ||
"sex" | ||
] | ||
}, | ||
"DummyContrasts": { | ||
"Type": "t" | ||
} | ||
} | ||
], | ||
"Edges": [ | ||
{ | ||
"Source": "run", | ||
"Destination": "participant", | ||
"GroupBy": [ | ||
"subject", | ||
"contrast" | ||
] | ||
}, | ||
{ | ||
"Source": "participant", | ||
"Destination": "by-group", | ||
"GroupBy": [ | ||
"sex" | ||
] | ||
}, | ||
{ | ||
"Source": "participant", | ||
"Destination": "group-diff", | ||
"GroupBy": [] | ||
} | ||
] | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
GroupBy should now be in the nodes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Shotgunosine could you look at this one.