Skip to content

Commit

Permalink
fix slicing of the studyset
Browse files Browse the repository at this point in the history
  • Loading branch information
jdkent committed Mar 10, 2023
1 parent 3993cb6 commit 4f08818
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 34 deletions.
8 changes: 2 additions & 6 deletions nimare/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,10 @@ def _study_to_dict(study):
"journal": study.publication,
"title": study.name,
},
"contrasts": {
_create_name(a): _analysis_to_dict(study, a) for a in study.analyses
},
"contrasts": {_create_name(a): _analysis_to_dict(study, a) for a in study.analyses},
}

return Dataset(
{_create_name(s): _study_to_dict(s) for s in list(studyset.studies.values())}
)
return Dataset({_create_name(s): _study_to_dict(s) for s in list(studyset.studies)})


def convert_neurosynth_to_dict(
Expand Down
86 changes: 58 additions & 28 deletions nimare/nimads.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""NIMADS-related classes for NiMARE."""
import json
from copy import deepcopy
import weakref
from copy import deepcopy


class Studyset:
Expand Down Expand Up @@ -36,7 +36,10 @@ def annotations(self):

@annotations.setter
def annotations(self, annotation):
loaded_annotation = Annotation(annotation, self)
if isinstance(annotation, dict):
loaded_annotation = Annotation(annotation, self)
elif isinstance(annotation, Annotation):
loaded_annotation = annotation
self._annotations.append(loaded_annotation)

@annotations.deleter
Expand All @@ -60,6 +63,11 @@ def to_nimads(self, filename):

def to_dict(self):
"""Return a dictionary representation of the Studyset."""
return {
"id": self.id,
"name": self.name,
"studies": [s.to_dict() for s in self.studies],
}

def load(self, filename):
"""Load a Studyset from a pickled file."""
Expand All @@ -75,18 +83,19 @@ def copy(self):

def slice(self, analyses):
"""Create a new Studyset with only requested Analyses."""
studyset = self.copy()

for study in studyset.studies:
unused_analyses = [a for a in study.analyses if a.id not in analyses]
for a in unused_analyses:
del a
for study in studyset.studies:
unused_analyses = [a for a in study.analyses if a.id not in analyses]
for a in unused_analyses:
del a
studyset_dict = self.to_dict()
annotations = [annot.to_dict() for annot in self.annotations]

for study in studyset_dict["studies"]:
study["analyses"] = [a for a in study["analyses"] if a["id"] in analyses]

studyset = self.__class__(source=studyset_dict)

for annot in annotations:
annot["notes"] = [n for n in annot["notes"] if n["analysis"] in analyses]
studyset.annotation = annot

return studyset
# studyset.studies

def merge(self, right):
"""Merge a separate Studyset into the current one."""
Expand Down Expand Up @@ -177,6 +186,17 @@ def get_analyses(self):
"""
...

def to_dict(self):
"""Return a dictionary representation of the Study."""
return {
"id": self.id,
"name": self.name,
"authors": self.authors,
"publication": self.publication,
"metadata": self.metadata,
"analyses": [a.to_dict() for a in self.analyses],
}


class Analysis:
"""A single statistical analyses from a Study.
Expand Down Expand Up @@ -224,6 +244,20 @@ def __str__(self):
" ".join([self.name, f"images: {len(self.images)}", f"points: {len(self.points)}"])
)

def to_dict(self):
"""Convert the Analysis to a dictionary."""
return {
"id": self.id,
"name": self.name,
"conditions": [
{k: v for k, v in c.to_dict().items() if k in ["name", "description"]}
for c in self.conditions
],
"images": [i.to_dict() for i in self.images],
"points": [p.to_dict() for p in self.points],
"weights": [c.to_dict()["weight"] for c in self.conditions],
}


class Condition:
"""A condition within an Analysis.
Expand All @@ -247,11 +281,8 @@ def __init__(self, condition, weight):
self.weight = weight

def to_dict(self):
return {
"name": self.name,
"description": self.description,
"weight": self.weight
}
"""Convert the Condition to a dictionary."""
return {"name": self.name, "description": self.description, "weight": self.weight}


class Annotation:
Expand Down Expand Up @@ -287,6 +318,10 @@ def __init__(self, source, studyset):
for note in self.notes:
self._analysis_ref[note.analysis.id].annotations[self.id] = note.note

def to_dict(self):
"""Convert the Annotation to a dictionary."""
return {"name": self.name, "id": self.id, "notes": [note.to_dict() for note in self.notes]}


class Note:
"""A Note within an annotation.
Expand All @@ -300,14 +335,12 @@ class Note:
"""

def __init__(self, analysis, note):
self.analysis = weakref.proxy(analysis)
self.analysis = analysis
self.note = note

def to_dict(self):
return {
"analysis": self.analysis.id,
"note": self.note
}
"""Convert the Note to a dictionary."""
return {"analysis": self.analysis.id, "note": self.note}


class Image:
Expand Down Expand Up @@ -335,7 +368,7 @@ def to_dict(self):
"url": self.url,
"filename": self.filename,
"space": self.space,
"value_type": self.value_type
"value_type": self.value_type,
}


Expand All @@ -361,7 +394,4 @@ def __init__(self, source):

def to_dict(self):
"""Convert the Point to a dictionary."""
return {
"space": self.space,
"coordinates": [self.x, self.y, self.z]
}
return {"space": self.space, "coordinates": [self.x, self.y, self.z]}

0 comments on commit 4f08818

Please sign in to comment.