-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #32 from Remi-Gau/cli
[ENH] add CLI
- Loading branch information
Showing
14 changed files
with
376 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
--- | ||
name: Run tests CLI | ||
|
||
on: | ||
push: | ||
branches: | ||
- main | ||
pull_request: | ||
branches: | ||
- '*' | ||
|
||
concurrency: | ||
group: environment-${{ github.ref }} | ||
cancel-in-progress: true | ||
|
||
jobs: | ||
|
||
test_cli: | ||
strategy: | ||
fail-fast: false | ||
matrix: | ||
os: [ubuntu-latest, macos-latest, windows-latest] | ||
python-version: ['3.11'] | ||
runs-on: ${{ matrix.os }} | ||
name: ${{ matrix.os }} with Python ${{ matrix.python-version }} | ||
defaults: | ||
run: | ||
shell: bash | ||
steps: | ||
- uses: actions/checkout@v3 | ||
with: | ||
submodules: recursive | ||
- name: Set up python | ||
uses: actions/setup-python@v4 | ||
with: | ||
python-version: ${{ matrix.python-version }} | ||
- name: Display Python version | ||
shell: bash {0} | ||
run: python -c "import sys; print(sys.version)" | ||
- name: Install bids examples | ||
run: git clone https://github.com/bids-standard/bids-examples.git | ||
- name: Install pybids-reports | ||
shell: bash {0} | ||
run: pip install . | ||
- name: Info CLI | ||
shell: bash {0} | ||
run: | | ||
pybids_reports --version | ||
pybids_reports --help | ||
- name: Run test for CLI | ||
shell: bash {0} | ||
run: | | ||
bash tools/run_on_examples.sh |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
bids-examples | ||
|
||
# do not track _version.py for hatch | ||
bids/ext/reports/_version.py | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
"""General parser for the cohort_creator package.""" | ||
from __future__ import annotations | ||
|
||
import argparse | ||
import sys | ||
from pathlib import Path | ||
from typing import IO | ||
from typing import Sequence | ||
|
||
import rich | ||
from reports import BIDSReport | ||
|
||
from ._version import __version__ | ||
from .logger import pybids_reports_logger | ||
from bids.layout import BIDSLayout | ||
|
||
# from bids.reports import BIDSReport | ||
LOGGER = pybids_reports_logger() | ||
|
||
|
||
class MuhParser(argparse.ArgumentParser): | ||
def _print_message(self, message: str, file: IO[str] | None = None) -> None: | ||
rich.print(message, file=file) | ||
|
||
|
||
def base_parser() -> MuhParser: | ||
parser = MuhParser( | ||
prog="pybids_reports", | ||
description="Report generator for BIDS datasets.", | ||
epilog=""" | ||
For a more readable version of this help section, | ||
see the online doc https://cohort-creator.readthedocs.io/en/latest/ | ||
""", | ||
) | ||
parser.add_argument( | ||
"bids_dir", | ||
help=""" | ||
Path to BIDS dataset. | ||
""", | ||
nargs=1, | ||
) | ||
parser.add_argument( | ||
"output_dir", | ||
help=""" | ||
Output path. | ||
""", | ||
nargs=1, | ||
) | ||
parser.add_argument( | ||
"--participant_label", | ||
help=""" | ||
The label(s) of the participant(s) that should be used for the report. | ||
The label corresponds to sub-<participant_label> from the BIDS spec | ||
(so it does not include "sub-"). | ||
If this parameter is not provided, The first subject will be used. | ||
Multiple participants can be specified with a space separated list. | ||
""", | ||
nargs="+", | ||
) | ||
parser.add_argument( | ||
"-v", | ||
"--version", | ||
action="version", | ||
version=f"{__version__}", | ||
) | ||
parser.add_argument( | ||
"--verbosity", | ||
help=""" | ||
Verbosity level. | ||
""", | ||
required=False, | ||
choices=[0, 1, 2, 3], | ||
default=2, | ||
type=int, | ||
nargs=1, | ||
) | ||
return parser | ||
|
||
|
||
def set_verbosity(verbosity: int | list[int]) -> None: | ||
if isinstance(verbosity, list): | ||
verbosity = verbosity[0] | ||
if verbosity == 0: | ||
LOGGER.setLevel("ERROR") | ||
elif verbosity == 1: | ||
LOGGER.setLevel("WARNING") | ||
elif verbosity == 2: | ||
LOGGER.setLevel("INFO") | ||
elif verbosity == 3: | ||
LOGGER.setLevel("DEBUG") | ||
|
||
|
||
def cli(argv: Sequence[str] = sys.argv) -> None: | ||
"""Entry point.""" | ||
parser = base_parser() | ||
|
||
args, unknowns = parser.parse_known_args(argv[1:]) | ||
|
||
bids_dir = Path(args.bids_dir[0]).resolve() | ||
# output_dir = Path(args.output_dir[0]) | ||
participant_label = args.participant_label or None | ||
|
||
set_verbosity(args.verbosity) | ||
|
||
LOGGER.debug(f"{bids_dir}") | ||
|
||
layout = BIDSLayout(bids_dir) | ||
|
||
report = BIDSReport(layout) | ||
if participant_label: | ||
report.generate(subject=participant_label) | ||
else: | ||
report.generate() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
"""General logger for the cohort_creator package.""" | ||
from __future__ import annotations | ||
|
||
import logging | ||
|
||
from rich.logging import RichHandler | ||
|
||
|
||
def pybids_reports_logger(log_level: str = "INFO") -> logging.Logger: | ||
FORMAT = "%(message)s" | ||
|
||
logging.basicConfig( | ||
level=log_level, | ||
format=FORMAT, | ||
datefmt="[%X]", | ||
handlers=[RichHandler()], | ||
) | ||
|
||
return logging.getLogger("pybids_reports") |
Oops, something went wrong.