Skip to content

Commit

Permalink
[ENH] Continue working on the changes
Browse files Browse the repository at this point in the history
  • Loading branch information
liuzhenqi77 committed May 22, 2024
1 parent 2a7ba6b commit 8465bec
Show file tree
Hide file tree
Showing 4 changed files with 173 additions and 58 deletions.
14 changes: 8 additions & 6 deletions neuromaps/datasets/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,18 @@
'fetch_all_atlases', 'fetch_atlas', 'fetch_civet', 'fetch_fsaverage',
'fetch_fslr', 'fetch_mni152', 'fetch_regfusion', 'get_atlas_dir',
'DENSITIES', 'ALIAS', 'available_annotations', 'available_tags',
'fetch_annotation'
'fetch_annotation', "get_annotations_desc", "get_annotations_summary",
"get_annotations_report"
]

# TODO: remove after nilearn v0.9 release
import warnings
warnings.filterwarnings('ignore', message='Fetchers from the nilearn.datasets',
category=FutureWarning)

from .atlases import (fetch_all_atlases, fetch_atlas, fetch_civet, # noqa
fetch_fsaverage, fetch_fslr, fetch_mni152,
fetch_regfusion, get_atlas_dir, DENSITIES, ALIAS)
from .annotations import (available_annotations, available_tags, # noqa
<<<<<<< HEAD
fetch_annotation)
=======
fetch_annotation, get_annotations_summary,
get_annotations_desc, get_annotations_report)
from .contributions import (upload_annotation)
>>>>>>> [ENH] Continue working on the changes
38 changes: 26 additions & 12 deletions neuromaps/datasets/annotations.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@
except ImportError:
_rich_avail = False

try:
import jinja2
_jinja2_avail = True
except ImportError:
_jinja2_avail = False

MATCH = re.compile(
r'source-(\S+)_desc-(\S+)_space-(\S+)_(?:den|res)-(\d+[k|m]{1,2})_'
)
Expand Down Expand Up @@ -250,9 +256,9 @@ def fetch_annotation(*, source=None, desc=None, space=None, den=None, res=None,
return _groupby_match(data, return_single=return_single)


def get_annotations_desc(annots, expand_index=False):
def get_annotations_desc(annots):
"""
Returns detailed descriptions for annotations
Returns detailed descriptions for annotations as a pandas dataframe
Parameters
----------
Expand All @@ -279,19 +285,23 @@ def get_annotations_desc(annots, expand_index=False):
with open(fn) as f:
info = json.load(f)["annotations"]

df_annot_info = []
df_annot_info = pd.json_normalize(info)
df_annot_info["annot.denres"] = df_annot_info["annot.den"].combine_first(df_annot_info["annot.res"])
df_annot_info["annot.key"] = list(zip(df_annot_info["annot.source"], df_annot_info["annot.desc"], df_annot_info["annot.space"], df_annot_info["annot.denres"]))
df_annot_info_keys_list = df_annot_info["annot.key"].tolist()

# find the annotations that are not available
annots_not_avail = []
for annot in annots:
for item_dict in info:
if tuple(item_dict["annot"].values()) == annot:
_item_dict = item_dict.copy()
if not expand_index:
_item_dict["annot"] = annot
df_annot_info.append((_item_dict))
break
df_annot_info = pd.json_normalize(df_annot_info)
if annot not in df_annot_info_keys_list:
annots_not_avail.append(annot)
if len(annots_not_avail) > 0:
raise warnings.warn(f"Annotations {annots_not_avail} are not available.")

return df_annot_info
annots_avail = [_ for _ in annots if _ not in annots_not_avail]
df_annot_info = df_annot_info.set_index("annot.key").loc[annots_avail, :].reset_index()

return df_annot_info[["annot.key", "full_desc", "refs.primary", "refs.secondary", "demographics.N", "demographics.age"]]


def get_annotations_summary(annots, return_full=False):
Expand Down Expand Up @@ -354,6 +364,10 @@ def get_annotations_report(annots):
List of tuples identifying annotations, in the same form as returned
by `available_annotations()`.
"""
if not _jinja2_avail:
raise ImportError("This function requires the Jinja2 package to work. "
"Please `pip install Jinja2` as try again.")

df_annot_info = get_annotations_desc(annots, expand_index=False)

df_annot_info_latex = df_annot_info[["full_desc", "refs.primary"]].copy()
Expand Down
Loading

0 comments on commit 8465bec

Please sign in to comment.