Skip to content

Commit

Permalink
Redirect nypipe crash output to logging directory.
Browse files Browse the repository at this point in the history
  • Loading branch information
wasciutto committed Sep 22, 2021
1 parent edeb0ec commit 92848de
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 9 deletions.
9 changes: 6 additions & 3 deletions clpipe/config_json_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ def setup_project(self, project_title, project_dir, source_data):
self.setup_intensity_normalization(os.path.join(self.config['FMRIPrepOptions']['OutputDirectory'], "fmriprep"),
None,
os.path.join(self.config['ProjectDirectory'], 'data_postproc', 'normalized'),
None,
None)
None, None,
os.path.join(self.config['ProjectDirectory'], "logs", "intensity_normalization_logs"))
processing_streams = self.get_processing_stream_names()
if processing_streams:
for stream in processing_streams:
Expand Down Expand Up @@ -169,7 +169,7 @@ def setup_postproc(self, target_dir, target_suffix, output_dir, output_suffix, b
self.config[target_output]['LogDirectory'] = os.path.join(self.config['ProjectDirectory'], 'logs', log_target)
os.makedirs(self.config[target_output]['LogDirectory'], exist_ok=True)

def setup_intensity_normalization(self, target_dir, target_suffix, output_dir, output_suffix, method):
def setup_intensity_normalization(self, target_dir, target_suffix, output_dir, output_suffix, method, log_dir):
target_output = 'IntensityNormalizationOptions'

if target_dir is not None:
Expand All @@ -183,6 +183,9 @@ def setup_intensity_normalization(self, target_dir, target_suffix, output_dir, o
self.config[target_output]['OutputSuffix'] = output_suffix
if method is not None:
self.config[target_output]['Method'] = method
if log_dir is not None:
self.config[target_output]['LogDirectory'] = os.path.abspath(log_dir)
os.makedirs(self.config[target_output]['LogDirectory'], exist_ok=True)

def setup_heudiconv(self, dicom_directory, heuristic_file, output_directory):
if dicom_directory is not None:
Expand Down
3 changes: 2 additions & 1 deletion clpipe/data/defaultConfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@
"TargetSuffix": "space-MNI152NLin2009cAsym_desc-preproc_bold.nii.gz",
"OutputDirectory": "",
"OutputSuffix": "",
"Method": "10000_globalmedian"
"Method": "10000_globalmedian",
"LogDirectory": ""
},
"BetaSeriesOptions": {
"TargetDirectory": "",
Expand Down
12 changes: 9 additions & 3 deletions clpipe/intensity_normalization.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def intensity_normalization(subjects:list=None, config_file:str=None, method:str
# If provided, override project config with parameter config
config.config_updater(config_file)
# For those provided, replace intensity normalization config values with parameter values
config.setup_intensity_normalization(target_dir, target_suffix, output_dir, output_suffix, method)
config.setup_intensity_normalization(target_dir, target_suffix, output_dir, output_suffix, method, log_dir)

target_dir = Path(config.config["IntensityNormalizationOptions"]["TargetDirectory"])
target_suffix = config.config["IntensityNormalizationOptions"]["TargetSuffix"]
Expand All @@ -107,7 +107,7 @@ def intensity_normalization(subjects:list=None, config_file:str=None, method:str
normalize_subject(config, f'sub-{subject}')

def calculate_10000_global_median(in_path: os.PathLike, out_path:os.PathLike,
mask_path: os.PathLike=None, base_dir: os.PathLike=None):
mask_path: os.PathLike=None, base_dir: os.PathLike=None, crashdump_dir: os.PathLike=None):
"""Perform intensity normalization using the 10,000 global median method.
Args:
Expand All @@ -127,11 +127,15 @@ def calculate_10000_global_median(in_path: os.PathLike, out_path:os.PathLike,
div_median_node = pe.Node(BinaryMaths(operation="div", out_file=out_path), name="div_median")

workflow = pe.Workflow(name='10000_global_median', base_dir=base_dir)
if crashdump_dir is not None:
workflow.config['execution']['crashdump_dir'] = crashdump_dir

workflow.connect(mul_10000_node, "out_file", div_median_node, "in_file")
workflow.connect(median_node, "out_stat", div_median_node, "operand_value")
workflow.run()

def calculate_100_voxel_mean(in_path: os.PathLike, out_path: os.PathLike, base_dir: os.PathLike=None):
def calculate_100_voxel_mean(in_path: os.PathLike, out_path: os.PathLike, base_dir: os.PathLike=None,
crashdump_dir: os.PathLike=None):
"""Perform intensity normalization using the 100 voxel mean method.
Args:
Expand All @@ -145,6 +149,8 @@ def calculate_100_voxel_mean(in_path: os.PathLike, out_path: os.PathLike, base_d
div_mean_node = pe.Node(BinaryMaths(operation='div', out_file=out_path), name="div_mean") #operand_file=mean_path

workflow = pe.Workflow(name='100_voxel_mean', base_dir=base_dir)
if crashdump_dir is not None:
workflow.config['execution']['crashdump_dir'] = crashdump_dir

workflow.connect(mul100_node, "out_file", div_mean_node, "in_file")
workflow.connect(mean_node, "out_file", div_mean_node, "operand_file")
Expand Down
6 changes: 6 additions & 0 deletions nipype.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[logging]
workflow_level = DEBUG
log_directory = ./logs

[execution]
crashfile_format = txt
2 changes: 2 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import pytest
import numpy as np
import sys
import nibabel as nib
from pathlib import Path
from click.testing import CliRunner

sys.path.append('../clpipe')
from clpipe.project_setup import project_setup
from clpipe.config_json_parser import ClpipeConfigParser

Expand Down
5 changes: 3 additions & 2 deletions tests/test_intensity_normalization.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,8 @@ def test_calculate_10000_global_median(tmp_path, random_nii):

def test_calculate_10000_global_median_masked(tmp_path, random_nii, random_nii_mask):
out_path = tmp_path / "normalized.nii.gz"
calculate_10000_global_median(random_nii, out_path, mask_path=random_nii_mask, base_dir=tmp_path)
calculate_10000_global_median(random_nii, out_path, mask_path=random_nii_mask, base_dir=tmp_path,
crashdump_dir=tmp_path)

# Load random nii data and a mask
random_nii_data = nib.load(random_nii).get_fdata()
Expand All @@ -189,7 +190,7 @@ def test_calculate_10000_global_median_masked(tmp_path, random_nii, random_nii_m

def test_calculate_100_voxel_mean(tmp_path, random_nii):
out_path = tmp_path / "normalized.nii.gz"
calculate_100_voxel_mean(random_nii, out_path, base_dir=tmp_path)
calculate_100_voxel_mean(random_nii, out_path, base_dir=tmp_path, crashdump_dir=tmp_path)

random_nii_data = nib.load(random_nii).get_fdata()
normalized_data = nib.load(out_path).get_fdata()
Expand Down

0 comments on commit 92848de

Please sign in to comment.