Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Remi-Gau committed Aug 7, 2024
1 parent 3e0c005 commit 7f431d3
Show file tree
Hide file tree
Showing 8 changed files with 168 additions and 134 deletions.
5 changes: 5 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[flake8]
count = True
show-source = True
statistics = True
max_function_length = 150
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,5 @@ repos:
rev: 7.1.1
hooks:
- id: flake8
args: [--verbose]
args: [--config, .flake8, --verbose]
additional_dependencies: [flake8-docstrings, flake8-use-fstring, flake8-functions, flake8-bugbear]
56 changes: 37 additions & 19 deletions code/_parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,20 +62,25 @@ def _add_common_arguments(parser: ArgumentParser) -> ArgumentParser:
Multiple participants can be specified with a space separated list.
""",
nargs="+",
required=False,
)
parser.add_argument(
"-v",
"--verbose",
dest="log_level",
action="append_const",
const=-1,
help="""
Verbosity level.
""",
choices=[0, 1, 2, 3],
default=2,
type=int,
nargs=1,
)
parser.add_argument(
"--bids_filter_file",
help="""
A JSON file describing custom BIDS input filters using PyBIDS.
For further details, please check out TBD.
""",
required=False,
)
return parser

Expand Down Expand Up @@ -105,6 +110,12 @@ def common_parser(
required=True,
)

subparsers.add_parser(
"help",
help="Show cat12 script help.",
formatter_class=parser.formatter_class,
)

view_parser = subparsers.add_parser(
"view",
help="View batch.",
Expand All @@ -125,20 +136,27 @@ def common_parser(
formatter_class=parser.formatter_class,
)
segment_parser = _add_common_arguments(segment_parser)
segment_parser.add_argument(
"--reset_database",
help="Resets the database of the input dataset.",
action="store_true",
required=False,
)
segment_parser.add_argument(
"--type",
help="""Type of segmentation.
default: default CAT12 preprocessing batch;
default: simple processing batch;
0 - longitudinal developmental;
1 - longitudinal plasticity/learning;
2 - longitudinal aging;
3 - longitudinal save models 1 and 2;
""",
choices=["default", "simple", "0", "1", "2", "3"],
default="default",
required=False,
type=str,
nargs=1,
)

return parser


# cat_standalone_segment_enigma.m
# cat_standalone_segment_long.m
# cat_standalone_resample.m
# cat_standalone_simple.m
# cat_standalone_tfce.m
# cat_standalone_get_IQR.m
# cat_standalone_smooth.m
# cat_standalone_dicom2nii.m
# cat_standalone_deface.m
# cat_standalone_get_TIV.m
# cat_standalone_segment.m
# cat_standalone_get_quality.m
# cat_standalone_get_ROI_values.m
84 changes: 27 additions & 57 deletions code/bids_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def get_dataset_layout(
)


def init_derivatives_layout(output_dir) -> BIDSLayout:
def init_derivatives_layout(output_dir: Path) -> BIDSLayout:
"""Initialize a derivatives dataset and returns its layout.
:param output_dir:
Expand All @@ -64,11 +64,8 @@ def init_derivatives_layout(output_dir) -> BIDSLayout:
:rtype: BIDSLayout
"""
create_dir_if_absent(output_dir)
write_dataset_description(output_dir)
layout_out = get_dataset_layout(output_dir)
layout_out = set_dataset_description(layout_out)
layout_out.dataset_description["DatasetType"] = "derivative"
layout_out.dataset_description["GeneratedBy"][0]["Name"] = "CAT12"
write_dataset_description(layout_out)
return layout_out


Expand Down Expand Up @@ -96,64 +93,37 @@ def list_subjects(layout: BIDSLayout, subjects) -> list[str]:
return subjects


def set_dataset_description(
layout: BIDSLayout, is_derivative: bool = True
) -> BIDSLayout:
def write_dataset_description(output_dir) -> None:
"""Add dataset description to a layout.
:param layout: _description_
:type layout: BIDSLayout
:param is_derivative: Defaults to True
:type is_derivative: bool, optional
:return: Updated BIDSLayout of the dataset
:rtype: BIDSLayout
:param output_dir: output_dir
:type output_dir: Path
"""
data: dict[str, Any] = {
"Name": "dataset name",
"BIDSVersion": "1.9.0",
"DatasetType": "raw",
"License": "CCO",
"Authors": ["", ""],
"Acknowledgements": "Special thanks to: ",
"HowToAcknowledge": """Please cite this paper: """,
"Funding": ["", ""],
"ReferencesAndLinks": [],
"DatasetDOI": "doi:",
"DatasetType": "derivative",
"License": "???",
"ReferencesAndLinks": ["https://doi.org/10.1101/2022.06.11.495736"],
}

if is_derivative:
data["GeneratedBy"] = [
{
"Name": "bidsMReye",
"Version": __version__,
"Container": {"Type": "", "Tag": ""},
"Description": "",
"CodeURL": "",
},
]

data["SourceDatasets"] = [
{
"DOI": "doi:",
"URL": "",
"Version": "",
}
]

layout.dataset_description = data

return layout


def write_dataset_description(layout: BIDSLayout) -> None:
"""Add a dataset_description.json to a BIDS dataset.
:param layout: BIDSLayout of the dataset to update.
:type layout: BIDSLayout
"""
output_file = Path(layout.root) / "dataset_description.json"
data["GeneratedBy"] = [
{
"Name": "cat12",
"Version": __version__,
"Container": {"Type": "", "Tag": __version__},
"Description": "",
"CodeURL": "",
},
]
data["SourceDatasets"] = [
{
"DOI": "doi:",
"URL": "",
"Version": "",
}
]

output_file = output_dir / "dataset_description.json"

with open(output_file, "w") as ff:
json.dump(layout.dataset_description, ff, indent=4)
json.dump(data, ff, indent=4)
10 changes: 2 additions & 8 deletions code/cat_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,22 @@

import logging

from defaults import default_log_level
from rich.logging import RichHandler
from rich.traceback import install


def cat12_log(
name: str | None = None, log_level: int = logging.INFO
) -> logging.Logger:
"""Create log."""
def cat12_log(name: str | None = None) -> logging.Logger:
"""Create log."""
# let rich print the traceback
install(show_locals=True)

FORMAT = "cat12 - %(asctime)s - %(message)s"

log_level = default_log_level()

if not name:
name = "rich"

logging.basicConfig(
level=log_level,
level="INFO",
format=FORMAT,
datefmt="[%X]",
handlers=[
Expand Down
22 changes: 16 additions & 6 deletions code/defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,9 @@
from __future__ import annotations


def default_log_level() -> str:
"""Return default log level."""
return "INFO"


def log_levels() -> list[str]:
"""Return a list of log levels."""
return ["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"]
return ["ERROR", "WARNING", "INFO", "DEBUG"]


def supported_batches() -> list[str]:
Expand All @@ -28,3 +23,18 @@ def supported_batches() -> list[str]:
"get_quality",
"get_ROI_values",
]


# cat_standalone_segment_enigma.m
# cat_standalone_segment_long.m
# cat_standalone_resample.m
# cat_standalone_simple.m
# cat_standalone_tfce.m
# cat_standalone_get_IQR.m
# cat_standalone_smooth.m
# cat_standalone_dicom2nii.m
# cat_standalone_deface.m
# cat_standalone_get_TIV.m
# cat_standalone_segment.m
# cat_standalone_get_quality.m
# cat_standalone_get_ROI_values.m
Loading

0 comments on commit 7f431d3

Please sign in to comment.