Skip to content

Commit

Permalink
use logger
Browse files Browse the repository at this point in the history
  • Loading branch information
Remi-Gau committed Dec 14, 2023
1 parent 35e9756 commit 39c9c93
Show file tree
Hide file tree
Showing 9 changed files with 120 additions and 52 deletions.
8 changes: 6 additions & 2 deletions giga_connectome/atlas.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
from nibabel import Nifti1Image
from pkg_resources import resource_filename

from giga_connectome.logger import gc_logger

gc_log = gc_logger()


PRESET_ATLAS = ["DiFuMo", "MIST", "Schaefer20187Networks"]

Expand Down Expand Up @@ -42,7 +46,7 @@ def load_atlas_setting(atlas: Union[str, Path, dict]):
Path to the atlas files.
"""
atlas_config = _check_altas_config(atlas)
print(atlas_config)
gc_log.info(atlas_config)

# load template flow
templateflow_dir = atlas_config.get("templateflow_dir")
Expand Down Expand Up @@ -104,7 +108,7 @@ def resample_atlas_collection(
List of pathlib.Path
Paths to atlases sampled to group level grey matter mask.
"""
print("Resample atlas to group grey matter mask.")
gc_log.info("Resample atlas to group grey matter mask.")
resampled_atlases = []
for desc in tqdm(atlas_config["file_paths"]):
parcellation = atlas_config["file_paths"][desc]
Expand Down
16 changes: 16 additions & 0 deletions giga_connectome/logger.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"""General logger for the cohort_creator package."""
from __future__ import annotations

import logging


def gc_logger(log_level: str = "INFO") -> logging.Logger:
FORMAT = "%(message)s"

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

return logging.getLogger("giga_connectome")
60 changes: 27 additions & 33 deletions giga_connectome/mask.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@

from giga_connectome.atlas import resample_atlas_collection

from giga_connectome.logger import gc_logger

gc_log = gc_logger()


def generate_gm_mask_atlas(
working_dir: Path,
Expand Down Expand Up @@ -63,7 +67,6 @@ def generate_group_mask(
template: str = "MNI152NLin2009cAsym",
templateflow_dir: Optional[Path] = None,
n_iter: int = 2,
verbose: int = 1,
) -> Nifti1Image:
"""
Generate a group EPI grey matter mask, and overlaid with a MNI grey
Expand All @@ -88,9 +91,6 @@ def generate_group_mask(
Number of repetitions of dilation and erosion steps performed in
scipy.ndimage.binary_closing function.
verbose :
Level of verbosity.
Keyword Arguments
-----------------
Used to filter the cirret
Expand All @@ -102,12 +102,10 @@ def generate_group_mask(
nibabel.nifti1.Nifti1Image
EPI (grey matter) mask for the current group of subjects.
"""
if verbose > 1:
print(f"Found {len(imgs)} masks")
if exclude := _check_mask_affine(imgs, verbose):
gc_log.debug(f"Found {len(imgs)} masks")
if exclude := _check_mask_affine(imgs):
imgs, __annotations__ = _get_consistent_masks(imgs, exclude)
if verbose > 1:
print(f"Remaining: {len(imgs)} masks")
gc_log.debug(f"Remaining: {len(imgs)} masks")

# templateflow environment setting to get around network issue
if templateflow_dir and templateflow_dir.exists():
Expand All @@ -129,7 +127,7 @@ def generate_group_mask(
memory=None,
verbose=0,
)
print(
gc_log.info(
f"Group EPI mask affine:\n{group_epi_mask.affine}"
f"\nshape: {group_epi_mask.shape}"
)
Expand Down Expand Up @@ -204,7 +202,7 @@ def _get_consistent_masks(


def _check_mask_affine(
mask_imgs: List[Union[Path, str, Nifti1Image]], verbose: int = 1
mask_imgs: List[Union[Path, str, Nifti1Image]]
) -> Union[list, None]:
"""Given a list of input mask images, show the most common affine matrix
and subjects with different values.
Expand All @@ -215,9 +213,6 @@ def _check_mask_affine(
See :ref:`extracting_data`.
3D or 4D EPI image with same affine.
verbose :
Level of verbosity.
Returns
-------
Expand All @@ -244,12 +239,11 @@ def _check_mask_affine(
common_affine = max(
set(header_info["affine"]), key=header_info["affine"].count
)
if verbose > 0:
print(
f"We found {len(set(header_info['affine']))} unique affine "
f"matrices. The most common one is "
f"{key_to_header[common_affine]}"
)
gc_log.debug(
f"We found {len(set(header_info['affine']))} unique affine "
f"matrices. The most common one is "
f"{key_to_header[common_affine]}"
)
odd_balls = set(header_info["affine"]) - {common_affine}
if not odd_balls:
return None
Expand All @@ -259,18 +253,16 @@ def _check_mask_affine(
ob_index = [
i for i, aff in enumerate(header_info["affine"]) if aff == ob
]
if verbose > 1:
print(
"The following subjects has a different affine matrix "
f"({key_to_header[ob]}) comparing to the most common value: "
f"{mask_imgs[ob_index]}."
)
exclude += ob_index
if verbose > 0:
print(
f"{len(exclude)} out of {len(mask_imgs)} has "
"different affine matrix. Ignore when creating group mask."
gc_log.debug(
"The following subjects has a different affine matrix "
f"({key_to_header[ob]}) comparing to the most common value: "
f"{mask_imgs[ob_index]}."
)
exclude += ob_index
gc_log.info(
f"{len(exclude)} out of {len(mask_imgs)} has "
"different affine matrix. Ignore when creating group mask."
)
return sorted(exclude)


Expand All @@ -284,7 +276,9 @@ def _check_pregenerated_masks(template, working_dir, atlas):
if not group_mask.exists():
group_mask = None
else:
print(f"Found pregenerated group level grey matter mask: {group_mask}")
gc_log.info(
f"Found pregenerated group level grey matter mask: {group_mask}"
)

# atlas
resampled_atlases = []
Expand All @@ -301,7 +295,7 @@ def _check_pregenerated_masks(template, working_dir, atlas):
if not all(all_exist):
resampled_atlases = None
else:
print(
gc_log.info(
f"Found resampled atlases: {resampled_atlases}. Skipping group "
"level mask generation step."
)
Expand Down
15 changes: 12 additions & 3 deletions giga_connectome/postprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@
from nilearn.connectome import ConnectivityMeasure
from nilearn.maskers import NiftiLabelsMasker, NiftiMapsMasker
from bids.layout import BIDSImageFile

from giga_connectome import utils
from giga_connectome.connectome import generate_timeseries_connectomes
from giga_connectome.denoise import denoise_nifti_voxel
from giga_connectome.logger import gc_logger

gc_log = gc_logger()


def run_postprocessing_dataset(
Expand Down Expand Up @@ -97,7 +101,8 @@ def run_postprocessing_dataset(
)

# transform data
print("processing subjects")
gc_log.info("processing subjects")

for img in tqdm(images):
# process timeseries
denoised_img = denoise_nifti_voxel(
Expand All @@ -109,7 +114,9 @@ def run_postprocessing_dataset(
attribute_name = f"{subject}_{specifier}_atlas-{atlas}_desc-{desc}"
if not denoised_img:
time_series_atlas, correlation_matrix = None, None
print(f"{attribute_name}: no volume after scrubbing")

gc_log.info(f"{attribute_name}: no volume after scrubbing")

continue

# extract timeseries and connectomes
Expand Down Expand Up @@ -140,7 +147,9 @@ def run_postprocessing_dataset(
)

if analysis_level == "group":
print("create group connectome")

gc_log.info("create group connectome")

for desc in connectomes:
average_connectome = np.mean(
np.array(connectomes[desc]), axis=0
Expand Down
11 changes: 11 additions & 0 deletions giga_connectome/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,17 @@ def main(argv=None):
"pipeline (option A). The default is False.",
action="store_true",
)
parser.add_argument(
"--verbosity",
help="""
Verbosity level.
""",
required=False,
choices=[0, 1, 2, 3],
default=2,
type=int,
nargs=1,
)

args = parser.parse_args(argv)

Expand Down
3 changes: 1 addition & 2 deletions giga_connectome/tests/test_mask.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ def test_check_mask_affine():
weird = Nifti1Image(processed_vol, np.eye(4) * np.array([1, 1, 1.5, 1]).T)
weird2 = Nifti1Image(processed_vol, np.eye(4) * np.array([1, 1, 1.6, 1]).T)
exclude = mask._check_mask_affine(
[processed, processed, processed, processed, weird, weird, weird2],
verbose=2,
[processed, processed, processed, processed, weird, weird, weird2]
)
assert len(exclude) == 3
assert exclude == [4, 5, 6]
Expand Down
11 changes: 8 additions & 3 deletions giga_connectome/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
from bids.layout import Query
from bids import BIDSLayout

from giga_connectome.logger import gc_logger

gc_log = gc_logger()


def get_bids_images(
subjects: List[str],
Expand Down Expand Up @@ -175,7 +179,7 @@ def get_subject_lists(
]


def check_path(path: Path, verbose=True):
def check_path(path: Path):
"""Check if given path (file or dir) already exists, and if so returns a
new path with _<n> appended (n being the number of paths with the same name
that exist already).
Expand All @@ -196,6 +200,7 @@ def check_path(path: Path, verbose=True):
]
n = str(max(existing_numbers) + 1) if existing_numbers else "1"
path = path_parent / f"{path.stem}_{n}{ext}"
if verbose:
print(f"Specified path already exists, using {path} instead.")

gc_log.debug(f"Specified path already exists, using {path} instead.")

return path
40 changes: 33 additions & 7 deletions giga_connectome/workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,29 @@

from giga_connectome.denoise import is_ica_aroma
from giga_connectome import utils
from giga_connectome.logger import gc_logger


gc_log = gc_logger()


def set_verbosity(verbosity: int | list[int]) -> None:
if isinstance(verbosity, list):
verbosity = verbosity[0]
if verbosity == 0:
gc_log.setLevel("ERROR")
elif verbosity == 1:
gc_log.setLevel("WARNING")
elif verbosity == 2:
gc_log.setLevel("INFO")
elif verbosity == 3:
gc_log.setLevel("DEBUG")


def workflow(args):
print(vars(args))

gc_log.info(vars(args))

# set file paths
bids_dir = args.bids_dir
output_dir = args.output_dir
Expand All @@ -30,6 +49,8 @@ def workflow(args):
strategy = get_denoise_strategy(args.denoise_strategy)
atlas = load_atlas_setting(args.atlas)

set_verbosity(args.verbosity)

# check output path
output_dir.mkdir(parents=True, exist_ok=True)
working_dir.mkdir(parents=True, exist_ok=True)
Expand All @@ -38,7 +59,8 @@ def workflow(args):
template = (
"MNI152NLin6Asym" if is_ica_aroma(strategy) else "MNI152NLin2009cAsym"
)
print("Indexing BIDS directory")

gc_log.info("Indexing BIDS directory")

# create subject ts and connectomes
# refactor the two cases into one
Expand All @@ -55,8 +77,10 @@ def workflow(args):
f"sub-{subject}_atlas-{atlas['name']}"
f"_desc-{strategy['name']}.h5"
)
connectome_path = utils.check_path(connectome_path, verbose=True)
print("Generate subject level connectomes")
connectome_path = utils.check_path(connectome_path)

gc_log.info("Generate subject level connectomes")

run_postprocessing_dataset(
strategy,
resampled_atlases,
Expand All @@ -80,9 +104,11 @@ def workflow(args):
connectome_path = (
output_dir / f"atlas-{atlas['name']}_desc-{strategy['name']}.h5"
)
connectome_path = utils.check_path(connectome_path, verbose=True)
print(connectome_path)
print("Generate subject level connectomes")
connectome_path = utils.check_path(connectome_path)

gc_log.info(connectome_path)
gc_log.info("Generate subject level connectomes")

run_postprocessing_dataset(
strategy,
resampled_atlases,
Expand Down
8 changes: 6 additions & 2 deletions tools/download_templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
"""
from pathlib import Path

from giga_connectome.logger import gc_logger

gc_log = gc_logger()


def fetch_tpl_atlas():
"""Download datasets from templateflow."""
Expand All @@ -13,7 +17,7 @@ def fetch_tpl_atlas():
for atlas in atlases:
tf_path = tf.get("MNI152NLin2009cAsym", atlas=atlas)
if isinstance(tf_path, list) and len(tf_path) > 0:
print(f"{atlas} exists.")
gc_log.info(f"{atlas} exists.")
# download MNI grey matter template
tf.get("MNI152NLin2009cAsym", label="GM")

Expand All @@ -24,7 +28,7 @@ def download_mist():

tf_path = templateflow.api.get("MNI152NLin2009bAsym", atlas="BASC")
if isinstance(tf_path, list) and len(tf_path) > 0:
print("BASC / MIST atlas exists.")
gc_log.info("BASC / MIST atlas exists.")
return

# download and convert
Expand Down

0 comments on commit 39c9c93

Please sign in to comment.