Skip to content

Commit

Permalink
Autoformat with black and isort (#205)
Browse files Browse the repository at this point in the history
* Add linting action.

* Ignore external packages.

* Run black and isort.

* Consolidate tests.

* Remove relative imports from tests.
  • Loading branch information
tsalo committed Feb 14, 2023
1 parent 3bdca40 commit 649935b
Show file tree
Hide file tree
Showing 44 changed files with 9,032 additions and 5,411 deletions.
37 changes: 37 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: Lint code

on:
push:
branches:
- main
pull_request:
branches:
- main

defaults:
run:
shell: bash

jobs:
stable:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: ['ubuntu-latest']
python-version: [3.8]

steps:
- uses: actions/checkout@v2
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}
- name: Display Python version
run: python -c "import sys; print(sys.version)"
- name: Install flake8 and related packages
run: python -m pip install \
flake8 flake8-absolute-import flake8-black flake8-docstrings \
flake8-isort flake8-unused-arguments flake8-use-fstring \
flake8-warnings pep8-naming
- name: Check aslprep
run: python -m flake8 aslprep
21 changes: 12 additions & 9 deletions aslprep/__about__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@
# vi: set ft=python sts=4 ts=4 sw=4 et:
"""Base module variables."""
from ._version import get_versions
__version__ = get_versions()['version']

__version__ = get_versions()["version"]
del get_versions

__packagename__ = 'aslprep'
__copyright__ = 'Copyright 2020, PENN LINC'
__credits__ = ('Contributors: please check the ``.zenodo.json`` file at the top-level folder'
'of the repository')
__url__ = 'https://github.com/pennlinc/aslprep'
__packagename__ = "aslprep"
__copyright__ = "Copyright 2020, PENN LINC"
__credits__ = (
"Contributors: please check the ``.zenodo.json`` file at the top-level folder"
"of the repository"
)
__url__ = "https://github.com/pennlinc/aslprep"

DOWNLOAD_URL = (
'https://github.com/pennlinc/{name}/archive/{ver}.tar.gz'.format(
name=__packagename__, ver=__version__))
DOWNLOAD_URL = "https://github.com/pennlinc/{name}/archive/{ver}.tar.gz".format(
name=__packagename__, ver=__version__
)
15 changes: 5 additions & 10 deletions aslprep/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,11 @@
# vi: set ft=python sts=4 ts=4 sw=4 et:
"""Top-module metadata."""

from .__about__ import (
__copyright__,
__credits__,
__packagename__,
__version__,
)
from .__about__ import __copyright__, __credits__, __packagename__, __version__

__all__ = [
'__copyright__',
'__credits__',
'__packagename__',
'__version__',
"__copyright__",
"__credits__",
"__packagename__",
"__version__",
]
8 changes: 5 additions & 3 deletions aslprep/__main__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
from .cli.run import main

if __name__ == '__main__':
if __name__ == "__main__":
import sys

from . import __name__ as module

# `python -m <module>` typically displays the command as __main__.py
if '__main__.py' in sys.argv[0]:
sys.argv[0] = '%s -m %s' % (sys.executable, module)
if "__main__.py" in sys.argv[0]:
sys.argv[0] = "%s -m %s" % (sys.executable, module)
main()
6 changes: 2 additions & 4 deletions aslprep/_warnings.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Manipulate Python warnings."""
import warnings
import logging
import warnings

_wlog = logging.getLogger("py.warnings")
_wlog.addHandler(logging.NullHandler())
Expand All @@ -12,9 +12,7 @@ def _warn(message, category=None, stacklevel=1, source=None):
category = type(category).__name__
category = category.replace("type", "WARNING")

logging.getLogger("py.warnings").warning(
f"{category or 'WARNING'}: {message}"
)
logging.getLogger("py.warnings").warning(f"{category or 'WARNING'}: {message}")


def _showwarning(message, category, filename, lineno, file=None, line=None):
Expand Down
31 changes: 17 additions & 14 deletions aslprep/cli/aggregate_qc.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,20 @@
# vi: set ft=python sts=4 ts=4 sw=4 et:
"""aggregate qc of all the subjects"""
import os
import pandas as pd
from pathlib import Path

import pandas as pd


def get_parser():
"""Build parser object"""
from argparse import ArgumentParser
from argparse import RawTextHelpFormatter
from argparse import ArgumentParser, RawTextHelpFormatter

parser = ArgumentParser(description=__doc__, formatter_class=RawTextHelpFormatter)

parser.add_argument('aslprep_dir', action='store', type=Path, help='aslprep output dir')
parser.add_argument("aslprep_dir", action="store", type=Path, help="aslprep output dir")

parser.add_argument('output_prefix', action='store', type=str, help='output prefix for group')
parser.add_argument("output_prefix", action="store", type=str, help="output prefix for group")
return parser


Expand All @@ -25,21 +26,23 @@ def main():
opts = get_parser().parse_args()

allsubj_dir = os.path.abspath(opts.aslprep_dir)
outputfile = os.getcwd() + '/' + str(opts.output_prefix) + '_allsubjects_qc.csv'
outputfile = os.getcwd() + "/" + str(opts.output_prefix) + "_allsubjects_qc.csv"

qclist=[]
qclist = []
for r, d, f in os.walk(allsubj_dir):
for filex in f:
if filex.endswith("quality_control_cbf.csv"):
qclist.append(r+ '/'+ filex)
qclist.append(r + "/" + filex)

datax = pd.read_csv(qclist[0])
for i in range(1,len(qclist)):
for i in range(1, len(qclist)):
dy = pd.read_csv(qclist[i])
datax = pd.concat([datax,dy])
datax = pd.concat([datax, dy])

datax.to_csv(outputfile, index=None)

datax.to_csv(outputfile,index=None)

if __name__ == '__main__':
raise RuntimeError("this should be run after running aslprep;\n"
"it required installation of aslprep")
if __name__ == "__main__":
raise RuntimeError(
"this should be run after running aslprep;\n" "it required installation of aslprep"
)
81 changes: 35 additions & 46 deletions aslprep/cli/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@
# vi: set ft=python sts=4 ts=4 sw=4 et:
"""Parser."""
import sys

from .. import config


def _build_parser():
"""Build parser object."""
from argparse import ArgumentDefaultsHelpFormatter, ArgumentParser
from functools import partial
from pathlib import Path
from argparse import (
ArgumentParser,
ArgumentDefaultsHelpFormatter,
)

from packaging.version import Version

from ..niworkflows.utils.spaces import OutputReferencesAction, Reference
from .version import check_latest, is_flagged
from ..niworkflows.utils.spaces import Reference, OutputReferencesAction

def _path_exists(path, parser):
"""Ensure a given path exists."""
Expand All @@ -38,18 +38,18 @@ def _min_one(value, parser):
return value

def _to_gb(value):
scale = {"G": 1, "T": 10 ** 3, "M": 1e-3, "K": 1e-6, "B": 1e-9}
scale = {"G": 1, "T": 10**3, "M": 1e-3, "K": 1e-6, "B": 1e-9}
digits = "".join([c for c in value if c.isdigit()])
units = value[len(digits):] or "M"
units = value[len(digits) :] or "M"
return int(digits) * scale[units[0]]

def _drop_sub(value):
return value[4:] if value.startswith("sub-") else value

def _filter_pybids_none_any(dct):
from ..pybids import layout
return {k: layout.Query.ANY if v == "*" else v
for k, v in dct.items()}

return {k: layout.Query.ANY if v == "*" else v for k, v in dct.items()}

def _bids_filter(value):
from json import loads
Expand All @@ -59,14 +59,10 @@ def _bids_filter(value):

verstr = f"ASLPrep v{config.environment.version}"
currentv = Version(config.environment.version)
is_release = not any(
(currentv.is_devrelease, currentv.is_prerelease, currentv.is_postrelease)
)
is_release = not any((currentv.is_devrelease, currentv.is_prerelease, currentv.is_postrelease))

parser = ArgumentParser(
description="ASLPrep: ASL PREProcessing workflows v{}".format(
config.environment.version
),
description="ASLPrep: ASL PREProcessing workflows v{}".format(config.environment.version),
formatter_class=ArgumentDefaultsHelpFormatter,
)
PathExists = partial(_path_exists, parser=parser)
Expand Down Expand Up @@ -122,11 +118,11 @@ def _bids_filter(value):
# Re-enable when option is actually implemented
# g_bids.add_argument('-r', '--run-id', action='store', default='single_run',
# help='select a specific run to be processed')
#g_bids.add_argument(
# "-t", "--task-id", action="store", help="select a specific task to be processed"
#)
# g_bids.add_argument(
# "-t", "--task-id", action="store", help="select a specific task to be processed"
# )
g_bids.add_argument(
"--echo-idx",
"--echo-idx",
action="store",
type=int,
help="select a specific echo to be processed in a multiecho series",
Expand Down Expand Up @@ -158,7 +154,7 @@ def _bids_filter(value):
"--nthreads",
"--n_cpus",
"--n-cpus",
dest='nprocs',
dest="nprocs",
action="store",
type=PositiveInt,
help="maximum number of threads across all processes",
Expand All @@ -181,8 +177,7 @@ def _bids_filter(value):
g_perfm.add_argument(
"--low-mem",
action="store_true",
help="attempt to reduce memory usage (will increase disk usage "
"in working directory)",
help="attempt to reduce memory usage (will increase disk usage " "in working directory)",
)
g_perfm.add_argument(
"--use-plugin",
Expand All @@ -192,9 +187,7 @@ def _bids_filter(value):
type=IsFile,
help="nipype plugin configuration file",
)
g_perfm.add_argument(
"--anat-only", action="store_true", help="run anatomical workflows only"
)
g_perfm.add_argument("--anat-only", action="store_true", help="run anatomical workflows only")
g_perfm.add_argument(
"--boilerplate_only",
action="store_true",
Expand Down Expand Up @@ -245,7 +238,7 @@ def _bids_filter(value):
Non-standard spaces imply specific orientations and sampling grids. \
Important to note, the ``res-*`` modifier does not define the resolution used for \
the spatial normalization. To generate no ASL outputs, use this option without specifying \
any spatial references."""
any spatial references.""",
)

g_conf.add_argument(
Expand Down Expand Up @@ -279,7 +272,6 @@ def _bids_filter(value):
default=None,
help="Do not use boundary-based registration (no goodness-of-fit checks)",
)


g_conf.add_argument(
"--m0_scale",
Expand All @@ -303,7 +295,7 @@ def _bids_filter(value):
default=0,
type=int,
help="Number of initial volumes to ignore",
)
)
g_conf.add_argument(
"--smooth_kernel",
action="store",
Expand All @@ -313,18 +305,18 @@ def _bids_filter(value):
)

g_conf.add_argument(
"--scorescrub",
action="store_true",
default=False,
help=" Sudipto algoritms for denoising CBF"
"--scorescrub",
action="store_true",
default=False,
help=" Sudipto algoritms for denoising CBF",
)

g_conf.add_argument(
"--basil",
action="store_true",
default=False,
help=" FSL's CBF computation with spatial regularization and \
partial volume correction"
"--basil",
action="store_true",
default=False,
help=" FSL's CBF computation with spatial regularization and \
partial volume correction",
)
# Confounds options

Expand Down Expand Up @@ -440,8 +432,7 @@ def _bids_filter(value):
"--stop-on-first-crash",
action="store_true",
default=False,
help="Force stopping on first crash, even if a work directory"
" was specified.",
help="Force stopping on first crash, even if a work directory" " was specified.",
)
g_other.add_argument(
"--notrack",
Expand Down Expand Up @@ -480,14 +471,16 @@ def _bids_filter(value):
That means some severe flaw was found in it and we strongly
discourage its usage."""
% (config.environment.version, _reason),
file=sys.stderr,)
file=sys.stderr,
)

return parser


def parse_args(args=None, namespace=None):
"""Parse args and run further checks on the command line."""
import logging

from ..niworkflows.utils.spaces import Reference, SpatialReferences

parser = _build_parser()
Expand Down Expand Up @@ -556,9 +549,7 @@ def parse_args(args=None, namespace=None):

build_log.info(f"Clearing previous aslprep working directory: {work_dir}")
if not clean_directory(work_dir):
build_log.warning(
f"Could not clear all contents of working directory: {work_dir}"
)
build_log.warning(f"Could not clear all contents of working directory: {work_dir}")

# Ensure input and output folders are not the same
if output_dir == bids_dir:
Expand All @@ -584,9 +575,7 @@ def parse_args(args=None, namespace=None):
"Making sure the input data is BIDS compliant (warnings can be ignored in most "
"cases)."
)
validate_input_dir(
config.environment.exec_env, opts.bids_dir, opts.participant_label
)
validate_input_dir(config.environment.exec_env, opts.bids_dir, opts.participant_label)

# Setup directories
config.execution.log_dir = output_dir / "aslprep" / "logs"
Expand Down
Loading

0 comments on commit 649935b

Please sign in to comment.