-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ENH] add CLI, logger and type annotation (#17)
* add cli and logger * add type annotation * ignore version for type annotation
- Loading branch information
Showing
7 changed files
with
228 additions
and
33 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
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,59 @@ | ||
from __future__ import annotations | ||
|
||
import sys | ||
from pathlib import Path | ||
from typing import Sequence | ||
|
||
from eye2bids._parser import global_parser | ||
from eye2bids.edf2bids import edf2bids | ||
from eye2bids.logger import eye2bids_logger | ||
|
||
e2b_log = eye2bids_logger() | ||
|
||
|
||
def set_verbosity(verbosity: int | list[int]) -> None: | ||
"""Set verbosity level.""" | ||
if isinstance(verbosity, list): | ||
verbosity = verbosity[0] | ||
if verbosity == 0: | ||
e2b_log.setLevel("ERROR") | ||
elif verbosity == 1: | ||
e2b_log.setLevel("WARNING") | ||
elif verbosity == 2: | ||
e2b_log.setLevel("INFO") | ||
elif verbosity == 3: | ||
e2b_log.setLevel("DEBUG") | ||
|
||
|
||
def cli(argv: Sequence[str] = sys.argv) -> None: | ||
"""Entry point.""" | ||
parser = global_parser() | ||
parser.add_argument( | ||
"--input_type", | ||
type=str, | ||
help=""" | ||
Input type if it cannot be determined from input_file. | ||
""", | ||
required=False, | ||
default=None, | ||
) | ||
|
||
args, _ = parser.parse_known_args(argv[1:]) | ||
|
||
input_file = args.input_file | ||
input_file = Path(input_file).resolve() | ||
|
||
metadata_file = args.metadata_file | ||
if metadata_file is not None: | ||
metadata_file = metadata_file[0] | ||
|
||
output_dir = args.output_dir | ||
|
||
set_verbosity(args.verbosity) | ||
|
||
edf2bids( | ||
input_file=input_file, | ||
metadata_file=metadata_file, | ||
output_dir=output_dir, | ||
interactive=args.interactive, | ||
) |
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,16 +1,73 @@ | ||
import argparse | ||
from argparse import ArgumentParser | ||
from pathlib import Path | ||
|
||
from rich_argparse import RichHelpFormatter | ||
|
||
def global_parser(): | ||
from eye2bids._version import __version__ | ||
|
||
|
||
def global_parser() -> ArgumentParser: | ||
"""Parse command line arguments. | ||
Returns | ||
------- | ||
parser: An ArgumentParser object. | ||
""" | ||
parser = argparse.ArgumentParser() | ||
parser.add_argument("--input_file", type=str) | ||
parser.add_argument("--metadata_file", type=str) | ||
parser.add_argument("--output_dir", type=str) | ||
parser = ArgumentParser( | ||
prog="eye2bids", | ||
description="Converts eyetracking data to a BIDS compatible format.", | ||
formatter_class=RichHelpFormatter, | ||
) | ||
parser.add_argument( | ||
"-v", | ||
"--version", | ||
action="version", | ||
version=f"{__version__}", | ||
) | ||
parser.add_argument( | ||
"--input_file", | ||
type=str, | ||
help=""" | ||
Path to the input file to convert. | ||
""", | ||
required=True, | ||
) | ||
parser.add_argument( | ||
"--metadata_file", | ||
type=str, | ||
help=""" | ||
Path to the a yaml file containing extra metadata. | ||
""", | ||
required=False, | ||
default=None, | ||
) | ||
parser.add_argument( | ||
"--output_dir", | ||
type=str, | ||
help=""" | ||
Path to output directory. | ||
""", | ||
required=False, | ||
default=Path.cwd(), | ||
) | ||
parser.add_argument( | ||
"-i", | ||
"--interactive", | ||
help=""" | ||
To run in interactive mode. | ||
""", | ||
action="store_true", | ||
) | ||
parser.add_argument( | ||
"--verbosity", | ||
help=""" | ||
Verbosity level. | ||
""", | ||
required=False, | ||
choices=[0, 1, 2, 3], | ||
default=2, | ||
type=int, | ||
nargs=1, | ||
) | ||
return parser |
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,20 @@ | ||
"""General logger for the eye2bids package.""" | ||
from __future__ import annotations | ||
|
||
import logging | ||
|
||
from rich.logging import RichHandler | ||
|
||
|
||
def eye2bids_logger(log_level: str = "INFO") -> logging.Logger: | ||
"""Create a logger for the eye2bids package.""" | ||
FORMAT = "%(message)s" | ||
|
||
logging.basicConfig( | ||
level=log_level, | ||
format=FORMAT, | ||
datefmt="[%X]", | ||
handlers=[RichHandler()], | ||
) | ||
|
||
return logging.getLogger("cohort_creator") |
Oops, something went wrong.