Skip to content

Commit

Permalink
Combine analyses in Studyset (#810)
Browse files Browse the repository at this point in the history
* combine analyses

* Update nimads.py

* Update .gitignore

* Return copy of Studyset
  • Loading branch information
JulioAPeraza committed Jun 7, 2023
1 parent 0b1f20c commit 6c60e91
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ docs/auto_examples/
nimare/resources/data/neurovault-data/
nimare/tests/data/downloaded/
nimare/tests/resources/data/neurovault-data/
_readthedocs/
.pytest_cache

#
Expand Down
22 changes: 22 additions & 0 deletions nimare/nimads.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,28 @@ def from_nimads(cls, filename):

return cls(nimads)

def combine_analyses(self):
"""Combine analyses in Studyset."""
studyset = self.copy()
for study in studyset.studies:
if len(study.analyses) > 1:
source_lst = [analysis.to_dict() for analysis in study.analyses]
ids, names, conditions, images, points, weights = [
[source[key] for source in source_lst] for key in source_lst[0]
]

new_source = {
"id": "_".join(ids),
"name": "; ".join(names),
"conditions": [cond for c_list in conditions for cond in c_list],
"images": [image for i_list in images for image in i_list],
"points": [point for p_list in points for point in p_list],
"weights": [weight for w_list in weights for weight in w_list],
}
study.analyses = [Analysis(new_source)]

return studyset

def to_nimads(self, filename):
"""Write the Studyset to a NIMADS JSON file."""
with open(filename, "w+") as fn:
Expand Down
5 changes: 4 additions & 1 deletion nimare/tests/test_nimads.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@ def test_load_nimads(example_nimads_studyset, example_nimads_annotation):
# filter the studyset to only include analyses with include=True
annotation = studyset.annotations[0]
analysis_ids = [n.analysis.id for n in annotation.notes if n.note["include"]]
analysis_ids = analysis_ids[0:5]
analysis_ids = analysis_ids[:5]
filtered_studyset = studyset.slice(analyses=analysis_ids)
# Combine analyses after filtering
filtered_studyset = filtered_studyset.combine_analyses()

assert isinstance(filtered_studyset, nimads.Studyset)
dataset = filtered_studyset.to_dataset()
assert isinstance(dataset, Dataset)

0 comments on commit 6c60e91

Please sign in to comment.