Skip to content
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
49 changes: 38 additions & 11 deletions hed/cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,9 @@ def validate():
is_flag=True,
help="Disable all logging output",
)
@click.pass_context
def validate_bids_cmd(
ctx,
data_path,
error_limit,
errors_by_file,
Expand Down Expand Up @@ -244,7 +246,8 @@ def validate_bids_cmd(
args.append("-x")
args.extend(exclude_dirs)

validate_bids_main(args)
result = validate_bids_main(args)
ctx.exit(result if result is not None else 0)


@validate.command(
Expand Down Expand Up @@ -354,7 +357,9 @@ def validate_bids_cmd(
is_flag=True,
help="Disable all logging output",
)
@click.pass_context
def validate_hed_string_cmd(
ctx,
hed_string,
schema_version,
definitions,
Expand Down Expand Up @@ -395,14 +400,16 @@ def validate_hed_string_cmd(
if verbose:
args.append("-v")

validate_string_main(args)
result = validate_string_main(args)
ctx.exit(result if result is not None else 0)


@schema.command(name="validate")
@click.argument("schema_path", type=click.Path(exists=True), nargs=-1, required=True)
@click.option("--add-all-extensions", is_flag=True, help="Always verify all versions of the same schema are equal")
@click.option("-v", "--verbose", is_flag=True, help="Enable verbose output")
def schema_validate_cmd(schema_path, add_all_extensions, verbose):
@click.pass_context
def schema_validate_cmd(ctx, schema_path, add_all_extensions, verbose):
"""Validate HED schema files.

This command validates HED schema files for correctness, checking structure,
Expand Down Expand Up @@ -434,13 +441,15 @@ def schema_validate_cmd(schema_path, add_all_extensions, verbose):
if verbose:
args.append("-v")

validate_schemas_main(args)
result = validate_schemas_main(args)
ctx.exit(result if result is not None else 0)


@schema.command(name="convert")
@click.argument("schema_path", type=click.Path(exists=True), nargs=-1, required=True)
@click.option("--set-ids", is_flag=True, help="Set/update HED IDs in the schema")
def schema_convert_cmd(schema_path, set_ids):
@click.pass_context
def schema_convert_cmd(ctx, schema_path, set_ids):
"""Convert HED schema between formats (TSV, XML, MEDIAWIKI, JSON).

This command converts HED schema files between different formats while
Expand All @@ -467,14 +476,16 @@ def schema_convert_cmd(schema_path, set_ids):
if set_ids:
args.append("--set-ids")

convert_main(args)
result = convert_main(args)
ctx.exit(result if result is not None else 0)


@schema.command(name="add-ids")
@click.argument("repo_path", type=click.Path(exists=True))
@click.argument("schema_name")
@click.argument("schema_version")
def schema_add_ids_cmd(repo_path, schema_name, schema_version):
@click.pass_context
def schema_add_ids_cmd(ctx, repo_path, schema_name, schema_version):
"""Add HED IDs to a schema.

This command adds unique HED IDs to schema elements that don't have them,
Expand All @@ -498,7 +509,8 @@ def schema_add_ids_cmd(repo_path, schema_name, schema_version):

args = [repo_path, schema_name, schema_version]

add_ids_main(args)
result = add_ids_main(args)
ctx.exit(result if result is not None else 0)


@cli.group()
Expand Down Expand Up @@ -613,8 +625,19 @@ def extract():
is_flag=True,
help="Suppress log output to stderr; only applicable when --log-file is used (logs go only to file)",
)
@click.pass_context
def extract_bids_sidecar_cmd(
data_path, suffix, value_columns, skip_columns, log_level, log_file, log_quiet, output_file, verbose, exclude_dirs
ctx,
data_path,
suffix,
value_columns,
skip_columns,
log_level,
log_file,
log_quiet,
output_file,
verbose,
exclude_dirs,
):
"""Extract a sidecar template from a BIDS dataset.

Expand Down Expand Up @@ -643,7 +666,8 @@ def extract_bids_sidecar_cmd(
args.append("-x")
args.extend(exclude_dirs)

extract_main(args)
result = extract_main(args)
ctx.exit(result if result is not None else 0)


@extract.command(
Expand Down Expand Up @@ -781,7 +805,9 @@ def extract_bids_sidecar_cmd(
is_flag=True,
help="Suppress log output to stderr; only applicable when --log-file is used (logs go only to file)",
)
@click.pass_context
def extract_tabular_summary_cmd(
ctx,
data_path,
name_prefix,
name_suffix,
Expand Down Expand Up @@ -834,7 +860,8 @@ def extract_tabular_summary_cmd(
if verbose:
args.append("-v")

extract_summary_main(args)
result = extract_summary_main(args)
ctx.exit(result if result is not None else 0)


def main():
Expand Down
62 changes: 10 additions & 52 deletions hed/scripts/extract_tabular_summary.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
from hed import _version as vr
from hed.tools.util.io_util import get_file_list
from hed.tools.analysis.tabular_summary import TabularSummary
from hed.scripts.script_utils import setup_logging


def get_parser():
Expand Down Expand Up @@ -338,57 +339,6 @@ def format_output(summary, args):
return json.dumps(output_dict, indent=4)


def setup_logging(args):
"""Configure logging based on command line arguments.

Parameters:
args (argparse.Namespace): Parsed command line arguments.

Returns:
logging.Logger: Configured logger instance.
"""
# Determine log level
log_level = args.log_level.upper() if args.log_level else "WARNING"
if args.verbose:
log_level = "INFO"

# Configure logging format
log_format = "%(asctime)s - %(name)s - %(levelname)s - %(message)s"
date_format = "%Y-%m-%d %H:%M:%S"

# Clear any existing handlers from root logger
root_logger = logging.getLogger()
for handler in root_logger.handlers[:]:
root_logger.removeHandler(handler)

# Set the root logger level
root_logger.setLevel(getattr(logging, log_level))

# Create formatter
formatter = logging.Formatter(log_format, datefmt=date_format)

# File handler if log file specified
if args.log_file:
file_handler = logging.FileHandler(args.log_file, mode="w", encoding="utf-8")
file_handler.setLevel(getattr(logging, log_level))
file_handler.setFormatter(formatter)
root_logger.addHandler(file_handler)

# Console handler (stderr) unless explicitly quieted and file logging is used
if not args.log_quiet or not args.log_file:
console_handler = logging.StreamHandler(sys.stderr)
console_handler.setLevel(getattr(logging, log_level))
console_handler.setFormatter(formatter)
root_logger.addHandler(console_handler)

logger = logging.getLogger("extract_tabular_summary")
logger.info(f"Starting tabular summary extraction with log level: {log_level}")
if args.log_file:
logger.info(f"Log output will be saved to: {args.log_file}")

return logger


def main(arg_list=None):
"""Main entry point for the script.

Expand All @@ -406,7 +356,15 @@ def main(arg_list=None):
args = parser.parse_args(arg_list)

# Setup logging
logger = setup_logging(args)
setup_logging(args.log_level, args.log_file, args.log_quiet, args.verbose, False)
logger = logging.getLogger("extract_tabular_summary")
effective_level = logging.getLevelName(logger.getEffectiveLevel())
logger.info(
f"Starting tabular summary extraction with effective log level: {effective_level} "
f"(requested: {args.log_level}, verbose={'on' if args.verbose else 'off'})"
)
if args.log_file:
logger.info(f"Log output will be saved to: {args.log_file}")

try:
# Extract the summary
Expand Down
4 changes: 2 additions & 2 deletions hed/scripts/hed_convert_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,12 @@ def convert_and_update(filenames, set_ids):
return 0


def main():
def main(arg_list=None):
parser = argparse.ArgumentParser(description="Update other schema formats based on the changed one.")
parser.add_argument("filenames", nargs="*", help="List of files to process")
parser.add_argument("--set-ids", action="store_true", help="Add missing HED ids")

args = parser.parse_args()
args = parser.parse_args(arg_list)

filenames = args.filenames
set_ids = args.set_ids
Expand Down
40 changes: 7 additions & 33 deletions hed/scripts/hed_extract_bids_sidecar.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import sys
from hed import _version as vr
from hed.tools import BidsDataset
from hed.scripts.script_utils import setup_logging


def get_parser():
Expand Down Expand Up @@ -219,41 +220,14 @@ def main(arg_list=None):
args = parser.parse_args(arg_list)

# Setup logging configuration
log_level = args.log_level.upper() if args.log_level else "WARNING"
if args.verbose:
log_level = "INFO"

# Configure logging format
log_format = "%(asctime)s - %(name)s - %(levelname)s - %(message)s"
date_format = "%Y-%m-%d %H:%M:%S"

# Clear any existing handlers from root logger
root_logger = logging.getLogger()
for handler in root_logger.handlers[:]:
root_logger.removeHandler(handler)

# Set the root logger level - this is crucial for filtering
root_logger.setLevel(getattr(logging, log_level))

# Create and configure handlers
formatter = logging.Formatter(log_format, datefmt=date_format)

# File handler if log file specified
if args.log_file:
file_handler = logging.FileHandler(args.log_file, mode="w", encoding="utf-8")
file_handler.setLevel(getattr(logging, log_level))
file_handler.setFormatter(formatter)
root_logger.addHandler(file_handler)

# Console handler (stderr) unless explicitly quieted and file logging is used
if not args.log_quiet or not args.log_file:
console_handler = logging.StreamHandler(sys.stderr)
console_handler.setLevel(getattr(logging, log_level))
console_handler.setFormatter(formatter)
root_logger.addHandler(console_handler)
setup_logging(args.log_level, args.log_file, args.log_quiet, args.verbose, False)

logger = logging.getLogger("extract_bids_sidecar")
logger.info(f"Starting BIDS sidecar extraction with log level: {log_level}")
effective_level = logging.getLevelName(logger.getEffectiveLevel())
logger.info(
f"Starting BIDS sidecar extraction with effective log level: {effective_level} "
f"(requested: {args.log_level}, verbose={'on' if args.verbose else 'off'})"
)
if args.log_file:
logger.info(f"Log output will be saved to: {args.log_file}")

Expand Down
10 changes: 8 additions & 2 deletions hed/scripts/validate_bids.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,13 +201,19 @@ def main(arg_list=None):

# Parse the arguments
args = parser.parse_args(arg_list)
print(f"{str(args)}")

# Set up logging
setup_logging(args.log_level, args.log_file, args.log_quiet, args.verbose, args.no_log)

logger = logging.getLogger("validate_bids")
logger.info(f"Starting BIDS validation with log level: {args.log_level}")
logger.debug("Parsed arguments: %s", args)
effective_level_name = logging.getLevelName(logger.getEffectiveLevel())
logger.info(
"Starting BIDS validation with effective log level: %s (requested: %s, verbose=%s)",
effective_level_name,
args.log_level,
"on" if args.verbose else "off",
)
if args.log_file:
logger.info(f"Log output will be saved to: {args.log_file}")

Expand Down
9 changes: 9 additions & 0 deletions hed/scripts/validate_hed_string.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,15 @@ def main(arg_list=None):

import logging

logger = logging.getLogger("validate_hed_string")
effective_level_name = logging.getLevelName(logger.getEffectiveLevel())
logger.info(
"Starting HED string validation with effective log level: %s (requested: %s, verbose=%s)",
effective_level_name,
args.log_level,
"on" if args.verbose else "off",
)

try:
# Load schema (handle single version or list of versions)
schema_versions = args.schema_version[0] if len(args.schema_version) == 1 else args.schema_version
Expand Down
4 changes: 2 additions & 2 deletions hed/scripts/validate_schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ def get_parser():
return parser


def main():
def main(arg_list=None):
parser = get_parser()
args = parser.parse_args()
args = parser.parse_args(arg_list)

schema_files = sort_base_schemas(args.schema_files, args.add_all_extensions)
issues = validate_all_schemas(schema_files)
Expand Down