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

[ENH] Adds CLI for abagen.get_expression_data functionality #82

Merged
merged 8 commits into from
Aug 29, 2019
Merged
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
Empty file added abagen/cli/__init__.py
Empty file.
311 changes: 311 additions & 0 deletions abagen/cli/run.py

Large diffs are not rendered by default.

7 changes: 6 additions & 1 deletion abagen/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import pytest
from abagen.datasets import fetch_microarray
from abagen.datasets import fetch_desikan_killiany, fetch_microarray


@pytest.fixture(scope='session')
Expand All @@ -14,3 +14,8 @@ def testfiles(testdir):
donors=['12876', '15496'],
convert=True)
return files


@pytest.fixture(scope='session')
def atlas():
return fetch_desikan_killiany()
110 changes: 110 additions & 0 deletions abagen/tests/test_cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
# -*- coding: utf-8 -*-
"""
Tests for abagen.cli module
"""

import os
from pkg_resources import resource_filename
import pytest

from abagen import __version__ as version
from abagen.cli import run


def test_run_get_parser(capsys, atlas, testdir):
parser = run.get_parser()

# since we have a positional argument this fails, hard
with pytest.raises(SystemExit):
parser.parse_args([])
assert "following arguments are required: atlas" in capsys.readouterr().err

# providing the positional succeeds!
args = parser.parse_args([atlas['image']])
assert args.atlas == atlas['image']

# some data directories/files need to exist!
with pytest.raises(SystemExit):
parser.parse_args(['notanatlas.nii.gz'])
assert "does not exist" in capsys.readouterr().err
with pytest.raises(SystemExit):
parser.parse_args(['--data-dir', 'notadir', atlas['image']])
assert "does not exist" in capsys.readouterr().err
with pytest.raises(SystemExit):
parser.parse_args(['--atlas-info', 'notafile', atlas['image']])
assert "does not exist" in capsys.readouterr().err

# does version print correctly?
with pytest.raises(SystemExit):
parser.parse_args(['--version'])
assert 'abagen {}'.format(version) == capsys.readouterr().out.strip()

# arguments with invalid choices (probe_selection) raise errors
with pytest.raises(SystemExit):
parser.parse_args(['--probe-selection', 'notamethod', atlas['image']])
assert "invalid choice: 'notamethod'" in capsys.readouterr().err

# just test every option
args = parser.parse_args([
'-v', '-v', '-v',
'--quiet',
'--debug',
'--atlas-info', atlas['info'],
'--donors', '12876', '15496',
'--data-dir', testdir,
'--inexact',
'--tol', '5',
'--ibf-threshold', '0.6',
'--metric', 'median',
'--probe-selection', 'average',
'--no-reannotated', '--no-corrected-mni',
'--output-file', 'test.csv',
'--save-counts', '--save-donors',
atlas['image']
])


def test_run_main(capsys, atlas, testdir):
outputfile = os.path.join(str(testdir), 'abagen_expression.csv')

# check basic usage
run.main([
'--data-dir', testdir,
'--donors', '12876', '15496',
'--output-file', outputfile,
atlas['image']
])
assert os.path.exists(outputfile)

# check that save donors/counts outputs desired files
run.main([
'--data-dir', testdir,
'--donors', '12876', '15496',
'--output-file', outputfile,
'--save-donors', '--save-counts',
atlas['image']
])
for rep in ['_counts.csv', '_12876.csv', '_15496.csv']:
assert os.path.exists(outputfile.replace('.csv', rep))

# check stdout (BLARGH)
run.main([
'--data-dir', testdir,
'--donors', '12876', '15496',
'--output-file', outputfile,
'--stdout',
atlas['image']
])
stdout = capsys.readouterr().out
with open(outputfile, 'r') as src:
assert stdout == src.read()


def test_exec_run_fail():
executable = resource_filename('abagen', 'cli/run.py')

# need to set this otherwise it won't fail
__name__ = '__main__' # noqa
with pytest.raises(RuntimeError):
with open(executable, 'r') as src:
exec(src.read())
18 changes: 18 additions & 0 deletions docs/cli.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
.. _cli:

------------------
Command-line usage
------------------

You can use many of the primary workflows in ``abagen`` from the command line.

.. _cli_abagen:

The ``abagen`` command
======================

.. argparse::
:ref: abagen.cli.run.get_parser
:prog: abagen
:nodefault:
:nodefaultconst:
1 change: 1 addition & 0 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
'sphinx.ext.mathjax',
'sphinx.ext.napoleon',
'sphinx.ext.viewcode',
'sphinxarg.ext',
]

# Generate the API documentation when building
Expand Down
1 change: 1 addition & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ Contents

installation
changes
cli
usage
api
3 changes: 2 additions & 1 deletion docs/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
-r ../requirements.txt
sphinx>=1.2
sphinx>=1.6
sphinx-argparse
sphinx_rtd_theme
7 changes: 6 additions & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ include_package_data = True

[options.extras_require]
doc =
sphinx >=1.2
sphinx >=1.6
sphinx-argparse
sphinx_rtd_theme
io =
fastparquet
Expand All @@ -54,6 +55,10 @@ nibabel =
tests/data/*
abagen/data/*

[options.entry_points]
console_scripts =
abagen=abagen.cli.run:main

[coverage:run]
omit =
abagen/_version.py
Expand Down