Skip to content

Commit

Permalink
Merge pull request #32 from Remi-Gau/cli
Browse files Browse the repository at this point in the history
[ENH] add CLI
  • Loading branch information
Remi-Gau authored May 31, 2023
2 parents 99cb686 + f20d7ff commit 90bf1d2
Show file tree
Hide file tree
Showing 14 changed files with 376 additions and 93 deletions.
15 changes: 1 addition & 14 deletions .github/workflows/testing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,12 @@ concurrency:
cancel-in-progress: true

jobs:
# Determine if tests should be run based on commit message.
check_skip:
runs-on: ubuntu-latest
outputs:
skip: ${{ steps.result_step.outputs.ci-skip }}
steps:
- id: result_step
uses: mstachniuk/ci-skip@master
with:
commit-filter: '[skip ci];[ci skip];[skip github];[no ci]'
commit-filter-separator: ;

run_tests:
needs: check_skip
if: ${{ needs.check_skip.outputs.skip == 'false' }}
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, macos-latest]
os: [ubuntu-latest, macos-latest, windows-latest]
python-version: ['3.8', '3.9', '3.10', '3.11']
runs-on: ${{ matrix.os }}
name: ${{ matrix.os }} with Python ${{ matrix.python-version }}
Expand Down
53 changes: 53 additions & 0 deletions .github/workflows/testing_cli.yml
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
2 changes: 2 additions & 0 deletions .gitignore
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

Expand Down
2 changes: 1 addition & 1 deletion bids/ext/reports/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""pybids-reports: A tool for building methods sections for BIDS datasets."""
from __future__ import annotations

from . import _version # type: ignore
from . import _version
from . import parameters
from . import parsing
from . import report
Expand Down
114 changes: 114 additions & 0 deletions bids/ext/reports/cli.py
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()
19 changes: 19 additions & 0 deletions bids/ext/reports/logger.py
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")
Loading

0 comments on commit 90bf1d2

Please sign in to comment.