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

create README.rst, fix file saving #3

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
26 changes: 26 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
NeuroVault Collection Downloader
================================

CLI tool for bulk downloads of NeuroVault data.

* Free software: Apache Software License 2.0


Installation
------------

`pip install git+https://github.com/NeuroVault/neurovault_collection_downloader.git`

Usage
-----

1. Create a text file with one collection ID (a number or string of capital letters in case of private collection) per line.
2. `neurovault_collection_downloader collections.txt /output_directory`

Credits
-------

This package was created with Cookiecutter_ and the `audreyr/cookiecutter-pypackage`_ project template.

.. _Cookiecutter: https://github.com/audreyr/cookiecutter
.. _`audreyr/cookiecutter-pypackage`: https://github.com/audreyr/cookiecutter-pypackage
29 changes: 25 additions & 4 deletions neurovault_collection_downloader/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ def mkdir_p(path):
pass
else: raise

def fetch_collection(col_id, destination_folder):
def fetch_collection(col_id, destination_folder,
alt_label=None, exclude_tag=None):
if exclude_tag is not None:
if not isinstance(exclude_tag, list):
exclude_tag = [exclude_tag]
images_url_template = "http://neurovault.org/api/collections/%s/images/"
next_url = images_url_template%col_id
images = []
Expand All @@ -26,13 +30,30 @@ def fetch_collection(col_id, destination_folder):
images += data['results']
next_url = data['next']

col_folder = os.path.join(destination_folder, "%s"%col_id)
if alt_label is not None:
col_folder = os.path.join(destination_folder, alt_label)
else:
col_folder = os.path.join(destination_folder, "%s"%col_id)

mkdir_p(col_folder)
json.dump(images, open(os.path.join(col_folder, "images.json"), "w"),
json.dump(images,
open(os.path.join(col_folder, "images.json"), "w"),
indent=4, sort_keys=True)
for image in images:
click.echo("fetching %s"%image['file'])
urllib.request.urlretrieve(image['file'], os.path.join(col_folder, str(image['id']) + ".nii.gz"))
fname = os.path.basename(image['file'])
exclude_img = False
if exclude_tag is not None:
for et in exclude_tag:
if fname.find(et)>-1:
exclude_img = True
if not exclude_img:
print("saving",
image['file'],"to",
os.path.join(col_folder, fname))
urllib.request.urlretrieve(
image['file'],
os.path.join(col_folder, fname))


@click.command()
Expand Down
14 changes: 7 additions & 7 deletions requirements_dev.txt
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
pip==18.1
pip==19.1.1
bumpversion==0.5.3
wheel==0.32.1
wheel==0.33.4
watchdog==0.9.0
flake8==3.5.0
tox==3.5.2
coverage==4.5.1
Sphinx==1.8.1
twine==1.12.1
flake8==3.7.8
tox==3.13.2
coverage==4.5.3
Sphinx==2.1.2
twine==1.13.1