Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ENH] use logging #34

Merged
merged 3 commits into from
Jul 26, 2022
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
8 changes: 4 additions & 4 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ repos:
hooks:
- id: reorder-python-imports

- repo: https://github.com/pre-commit/mirrors-mypy
rev: 'v0.971'
hooks:
- id: mypy
# - repo: https://github.com/pre-commit/mirrors-mypy
# rev: 'v0.971'
# hooks:
# - id: mypy
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@
"coregistration",
"eyemask",
"filefolder"
]
],
"autoDocstring.docstringFormat": "sphinx"
}
34 changes: 28 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ lint/black: ## check style with black
lint/mypy: ## check style with mypy
mypy bidsmreye

lint: lint/black lint/flake8 lint/mypy ## check style
lint: lint/black lint/mypy lint/flake8 ## check style

docs: ## generate Sphinx HTML documentation, including API docs
rm -f docs/source/bidsmreye.rst
Expand All @@ -85,8 +85,10 @@ install: clean ## install the package to the active Python's site-packages
## TESTS

test: models tests/data/moae_fmriprep ## run tests quickly with the default Python
python -m pytest --cov bidsmreye --cov-report html:htmlcov
$(BROWSER) htmlcov/index.html
python -m pytest --cov bidsmreye

# python -m pytest --cov bidsmreye # --cov-report html:htmlcov
# $(BROWSER) htmlcov/index.html

tests/data/moae_fmriprep:
mkdir -p tests/data
Expand Down Expand Up @@ -115,13 +117,33 @@ demo: clean-demo
make generalize

prepare_data: tests/data/moae_fmriprep models/dataset1_guided_fixations.h5
bidsmreye --space MNI152NLin6Asym --task auditory --action prepare $$PWD/tests/data/moae_fmriprep $$PWD/outputs participant
bidsmreye --space MNI152NLin6Asym \
--task auditory \
--action prepare \
--verbosity WARNING \
--debug True \
$$PWD/tests/data/moae_fmriprep \
$$PWD/outputs participant\


combine:
bidsmreye --space MNI152NLin6Asym --task auditory --action combine $$PWD/tests/data/moae_fmriprep $$PWD/outputs participant
bidsmreye --space MNI152NLin6Asym \
--task auditory \
--action combine \
--verbosity INFO \
--debug False \
$$PWD/tests/data/moae_fmriprep \
$$PWD/outputs participant

generalize:
bidsmreye --space MNI152NLin6Asym --task auditory --model guided_fixations --action generalize $$PWD/tests/data/moae_fmriprep $$PWD/outputs participant
bidsmreye --space MNI152NLin6Asym \
--task auditory \
--model guided_fixations \
--action generalize \
--verbosity INFO \
--debug False \
$$PWD/tests/data/moae_fmriprep \
$$PWD/outputs participant

clean-demo:
rm -fr outputs
Expand Down
6 changes: 4 additions & 2 deletions bidsmreye/bidsutils.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
"""TODO."""
import json
import logging
from pathlib import Path
from typing import Optional
from typing import Union

from bids import BIDSLayout # type: ignore
from rich import print

from bidsmreye.utils import config
from bidsmreye.utils import create_dir_if_absent

log = logging.getLogger("rich")


def get_dataset_layout(dataset_path: Path, config: Optional[dict] = None) -> BIDSLayout:
"""Return a BIDSLayout object for the dataset at the given path.
Expand All @@ -27,7 +29,7 @@ def get_dataset_layout(dataset_path: Path, config: Optional[dict] = None) -> BID
if config is None:
pybids_config = get_pybids_config()

print(f"\nindexing {dataset_path}\n")
log.info(f"indexing {dataset_path}")

return BIDSLayout(
dataset_path, validate=False, derivatives=False, config=pybids_config
Expand Down
8 changes: 5 additions & 3 deletions bidsmreye/combine.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
"""TODO."""
import logging
import pickle
import warnings
from pathlib import Path

import numpy as np # type: ignore
from bids import BIDSLayout # type: ignore
from deepmreye import preprocess # type: ignore
from rich import print

from bidsmreye.bidsutils import check_layout
from bidsmreye.bidsutils import create_bidsname
Expand All @@ -15,6 +15,8 @@
from bidsmreye.utils import move_file
from bidsmreye.utils import return_regex

log = logging.getLogger("rich")


def process_subject(cfg: dict, layout_out: BIDSLayout, subject_label: str):
"""_summary_.
Expand All @@ -24,7 +26,7 @@ def process_subject(cfg: dict, layout_out: BIDSLayout, subject_label: str):

subject_label (str): Can be a regular expression.
"""
print(f"Running subject: {subject_label}")
log.info(f"Running subject: {subject_label}")

masks = layout_out.get(
return_type="filename",
Expand All @@ -38,7 +40,7 @@ def process_subject(cfg: dict, layout_out: BIDSLayout, subject_label: str):

for i, img in enumerate(masks):

print(f"Input mask: {img}")
log.info(f"Input mask: {img}")

# Load mask and normalize it
this_mask = pickle.load(open(img, "rb"))
Expand Down
21 changes: 15 additions & 6 deletions bidsmreye/generalize.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""TODO."""
import logging
import warnings
from pathlib import Path

Expand All @@ -18,6 +19,8 @@
from bidsmreye.utils import move_file
from bidsmreye.utils import return_regex

log = logging.getLogger("rich")


def convert_confounds(cfg: dict, layout_out: BIDSLayout, subject_label: str):
"""Convert numpy output to TSV.
Expand Down Expand Up @@ -47,7 +50,7 @@ def convert_confounds(cfg: dict, layout_out: BIDSLayout, subject_label: str):

confound_name = create_bidsname(layout_out, key + "p", "confounds_tsv")

print(f"Saving to {confound_name} \n")
log.info(f"Saving to {confound_name}")

pd.DataFrame(this_pred).to_csv(
confound_name, sep="\t", header=["x_position", "y_position"], index=None
Expand Down Expand Up @@ -82,14 +85,12 @@ def generalize(cfg: dict) -> None:
)

for file in data:
print(f"adding file: {Path(file).name}")
log.info(f"adding file: {Path(file).name}")
all_data.append(file)

print("\n")

generators = data_generator.create_generators(all_data, all_data)
generators = (*generators, all_data, all_data)

print("\n")

# Get untrained model and load with trained weights
Expand All @@ -104,25 +105,33 @@ def generalize(cfg: dict) -> None:
)
model_inference.load_weights(model_weights)

verbose = 0
if log.isEnabledFor(logging.DEBUG):
verbose = 2
elif log.isEnabledFor(logging.INFO):
verbose = 1

(evaluation, scores) = train.evaluate_model(
dataset="tmp",
model=model_inference,
generators=generators,
save=True,
model_path=f"{layout_out.root}/sub-{subject_label}/func/",
model_description="",
verbose=3,
verbose=verbose,
percentile_cut=80,
)

# TODO save figure
fig = analyse.visualise_predictions_slider(
evaluation,
scores,
color="rgb(0, 150, 175)",
bg_color="rgb(255,255,255)",
ylim=[-11, 11],
)
fig.show()
if log.isEnabledFor(logging.DEBUG) or log.isEnabledFor(logging.INFO):
fig.show()

entities = {"subject": subject_label, "task": cfg["task"], "space": cfg["space"]}
confound_numpy = create_bidsname(layout_out, entities, "confounds_numpy")
Expand Down
9 changes: 6 additions & 3 deletions bidsmreye/prepare_data.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
"""Run coregistration and extract data."""
import logging

from bids import BIDSLayout # type: ignore
from deepmreye import preprocess # type: ignore
from rich import print

from bidsmreye.bidsutils import check_layout
from bidsmreye.bidsutils import create_bidsname
Expand All @@ -14,6 +15,8 @@
from bidsmreye.utils import move_file
from bidsmreye.utils import return_regex

log = logging.getLogger("rich")


def coregister_and_extract_data(img: str) -> None:
"""Coregister image to eye template and extract data from eye mask for one image.
Expand All @@ -31,7 +34,7 @@ def coregister_and_extract_data(img: str) -> None:
z_edges,
) = preprocess.get_masks()

print(f"Input file: {img}")
log.info(f"Input file: {img}")

preprocess.run_participant(
img, dme_template, eyemask_big, eyemask_small, x_edges, y_edges, z_edges
Expand All @@ -48,7 +51,7 @@ def preprocess_subject(
layout_out (BIDSLayout): Layout output dataset.
subject_label (str): Can be a regular expression.
"""
print(f"Running subject: {subject_label}")
log.info(f"Running subject: {subject_label}")

bf = layout_in.get(
return_type="filename",
Expand Down
73 changes: 56 additions & 17 deletions bidsmreye/run.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,26 @@
#!/usr/bin/env python
"""Main script."""
import argparse
import logging
import os
import sys
from glob import glob
from pathlib import Path

from rich import print
from rich.logging import RichHandler
from rich.traceback import install

from bidsmreye.combine import combine
from bidsmreye.generalize import generalize
from bidsmreye.prepare_data import prepare_data
from bidsmreye.utils import config

# let rich print the traceback
install(show_locals=True)

# log format
FORMAT = "%(asctime)s - %(levelname)s - %(message)s"


def main(argv=sys.argv) -> None:
"""Execute the main script."""
Expand Down Expand Up @@ -82,13 +90,57 @@ def main(argv=sys.argv) -> None:
help="model to use",
choices=["guided_fixations"],
)
parser.add_argument(
"--verbosity",
help="INFO, WARNING. Defaults to INFO",
choices=["INFO", "WARNING"],
)
parser.add_argument(
"--debug",
help="true or false",
)

args = parser.parse_args(argv[1:])

cfg = set_cfg(args)

log_level = args.verbosity or "INFO"
if cfg["debug"]:
log_level = "DEBUG"

logging.basicConfig(
level=log_level, format=FORMAT, datefmt="[%X]", handlers=[RichHandler()]
)

log = logging.getLogger("rich")

if cfg["debug"]:
log.debug("DEBUG MODE")

if cfg["model_weights_file"] != "":
assert Path(cfg["model_weights_file"]).is_file()
log.info(f"Using model: {cfg['model_weights_file']}")

if args.analysis_level == "participant":

if args.action == "prepare":
log.info("PREPARING DATA")
prepare_data(cfg)

elif args.action == "combine":
log.info("COMBINING DATA")
combine(cfg)

elif args.action == "generalize":
log.info("GENERALIZING")
generalize(cfg)


def set_cfg(args):
"""Set the config."""
# TODO extract function
cfg = config()

# TODO extract function
subjects_to_analyze = []
# only for a subset of subjects
Expand All @@ -99,9 +151,6 @@ def main(argv=sys.argv) -> None:
subject_dirs = glob(os.path.join(args.bids_dir, "sub-*"))
subjects_to_analyze = [subject_dir.split("-")[-1] for subject_dir in subject_dirs]

# TODO extract function
cfg = config()

cfg["participant"] = subjects_to_analyze

if args.task:
Expand All @@ -118,20 +167,10 @@ def main(argv=sys.argv) -> None:
"dataset1_guided_fixations.h5",
)

if cfg["model_weights_file"] != "":
assert Path(cfg["model_weights_file"]).is_file()
print(f"\nUsing model: {cfg['model_weights_file']}")
if args.debug == "True":
cfg["debug"] = True

if args.analysis_level == "participant":

if args.action == "prepare":
prepare_data(cfg)

elif args.action == "combine":
combine(cfg)

elif args.action == "generalize":
generalize(cfg)
return cfg


if __name__ == "__main__":
Expand Down
Loading