Skip to content
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

Add tests #126

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions neuromaps/datasets/_osf.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@
"""Functions for working with data/osf.json file."""

import os
from pkg_resources import resource_filename
import json

import importlib.resources
from nilearn.datasets.utils import _md5_sum_file

from neuromaps.datasets.utils import _get_session

if getattr(importlib.resources, 'files', None) is not None:
_importlib_avail = True
else:
from pkg_resources import resource_filename
_importlib_avail = False

# uniquely identify each item ('hemi' can be None)
FNAME_KEYS = ['source', 'desc', 'space', 'den', 'res', 'hemi']
# auto-generated (checksum can be None if file does not exist)
Expand All @@ -23,9 +27,12 @@
INFO_KEYS = ['source', 'refs', 'comments', 'demographics']

# distribution JSON
OSFJSON = resource_filename(
'neuromaps', os.path.join('datasets', 'data', 'osf.json')
)
if _importlib_avail:
OSFJSON = importlib.resources.files("neuromaps") / "datasets/data/osf.json"
else:
OSFJSON = resource_filename(
'neuromaps', os.path.join('datasets', 'data', 'osf.json')
)


def parse_filename(fname, return_ext=True, verbose=False):
Expand Down
15 changes: 11 additions & 4 deletions neuromaps/datasets/tests/test__osf.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
# -*- coding: utf-8 -*-
"""For testing neuromaps.datasets._osf functionality."""

from pkg_resources import resource_filename

import pytest

import importlib.resources
from neuromaps.datasets import _osf

if getattr(importlib.resources, 'files', None) is not None:
_importlib_avail = True
else:
from pkg_resources import resource_filename
_importlib_avail = False


@pytest.mark.xfail
def test_parse_filename():
Expand All @@ -22,7 +26,10 @@ def test_parse_fname_list():

def test_parse_json():
"""Test parsing a JSON file."""
osf = resource_filename('neuromaps', 'datasets/data/osf.json')
if _importlib_avail:
osf = importlib.resources.files("neuromaps") / "datasets/data/osf.json"
else:
osf = resource_filename('neuromaps', 'datasets/data/osf.json')
out = _osf.parse_json(osf)
assert isinstance(out, list) and all(isinstance(i, dict) for i in out)

Expand Down
19 changes: 15 additions & 4 deletions neuromaps/datasets/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,15 @@

import json
import os
from pkg_resources import resource_filename

import importlib.resources
import requests

if getattr(importlib.resources, 'files', None) is not None:
_importlib_avail = True
else:
from pkg_resources import resource_filename
_importlib_avail = False

RESTRICTED = ["grh4d"]


Expand Down Expand Up @@ -70,8 +75,14 @@ def get_dataset_info(name, return_restricted=True):
dataset : dict or list-of-dict
Information on requested data
"""
fn = resource_filename('neuromaps',
os.path.join('datasets', 'data', 'osf.json'))
if _importlib_avail:
fn = importlib.resources.files("neuromaps") / "datasets/data/osf.json"
else:
fn = resource_filename(
'neuromaps',
os.path.join('datasets', 'data', 'osf.json')
)

with open(fn) as src:
osf_resources = _osfify_urls(json.load(src), return_restricted)

Expand Down
Loading
Loading