def test_fetch_peak_coordinates():
    resp = mock.Mock()
    resp.content = b"peak_coordinates_test"
    with mock.patch("requests.get", return_value=resp) as mock_get:
        with tempfile.TemporaryDirectory() as tmp_dir:
            coord_file = datasets.fetch_peak_coordinates(tmp_dir)
            with open(coord_file) as f:
                assert f.read() == "peak_coordinates_test"
            coord_file = datasets.fetch_peak_coordinates(tmp_dir)
            mock_get.assert_called_once()
Пример #2
0
# Choose where to store the cache and the model once it is trained
output_directory = "trained_text_to_brain_model"
cache_directory = "cache"

data_dir = pathlib.Path(datasets.fetch_neuroquery_model())

corpus_metadata = pd.read_csv(str(data_dir / "corpus_metadata.csv"))
vectorizer = TextVectorizer.from_vocabulary_file(
    str(data_dir / "vocabulary.csv"))

# The TFIDF features stored with NeuroQuery data correspond to the terms in
# `vocabulary.csv` and the studies in `corpus_metadata.csv`;
# see `README.md` in the data directory for details
tfidf = sparse.load_npz(str(data_dir / "corpus_tfidf.npz"))

coordinates = pd.read_csv(datasets.fetch_peak_coordinates())

######################################################################
# Transform the coordinates into brain maps
# -----------------------------------------

# We cache the `coordinates_to_maps` function with joblib to avoid recomputing
# this if we train a new model.
coord_to_maps = Memory(cache_directory).cache(coordinates_to_maps)

# You can set target_affine to a different value to increase image resolution
# or reduce computation time. The model on neuroquery.org uses 4 mm
# resolution i.e. target_affine=(4, 4, 4)
# You can also adjust the smoothing by setting `fwhm` (Full Width at Half
# maximum)
brain_maps, masker = coord_to_maps(coordinates,
Пример #3
0
import pathlib

import requests
from neuroquery import datasets

datasets.fetch_neuroquery_model()
datasets.fetch_peak_coordinates()
datasets.fetch_neuroquery_model(model_name="ensemble_model_2020-02-12")
maps_url = "https://osf.io/n5avm/download"
data_dir = datasets.get_neuroquery_data_dir()
extra_data = pathlib.Path(data_dir) / "extra"
extra_data.mkdir(exist_ok=True, parents=True)
maps_file = extra_data / "masked_term_maps.npy"
if not maps_file.is_file():
    print("downloading neuroquery maps...")
    resp = requests.get(maps_url)
    with open(str(maps_file), "wb") as f:
        f.write(resp.content)
    print("done")
    del resp