Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
# Check physical memory
total_memory = opts.mem_gb
if total_memory < 0:
try:
from psutil import virtual_memory
total_memory = virtual_memory().total // (1024 ** 3) + 1
except ImportError:
MRIQC_LOG.warn('Total physical memory could not be estimated, using %d'
'GB as default', DEFAULT_MEM_GB)
total_memory = DEFAULT_MEM_GB
if total_memory > 0:
av_procs = total_memory // 4
if av_procs < 1:
MRIQC_LOG.warn('Total physical memory is less than 4GB, memory allocation'
' problems are likely to occur.')
n_procs = 1
elif n_procs > av_procs:
n_procs = av_procs
settings = {
'bids_dir': bids_dir,
'write_graph': opts.write_graph,
'testing': opts.testing,
'hmc_afni': opts.hmc_afni,
'n_procs': n_procs,
'ants_nthreads': opts.ants_nthreads,
'output_dir': op.abspath(opts.output_dir),
'work_dir': op.abspath(opts.work_dir),
'verbose_reports': opts.verbose_reports or opts.testing
}
'considered for preprocessing')
g_afni.add_argument('--stop-idx', action='store', type=int,
help='Final volume in functional timeseries that should be '
'considered for preprocessing')
g_afni.add_argument('--correct-slice-timing', action='store_true', default=False,
help='Perform slice timing correction')
opts = parser.parse_args()
# Build settings dict
bids_dir = op.abspath(opts.bids_dir)
# Number of processes
n_procs = 0
if opts.nthreads is not None:
MRIQC_LOG.warn('Option --nthreads has been deprecated in mriqc 0.8.8. '
'Please use --n_procs instead.')
n_procs = opts.nthreads
if opts.n_procs is not None:
n_procs = opts.n_procs
# Check physical memory
total_memory = opts.mem_gb
if total_memory < 0:
try:
from psutil import virtual_memory
total_memory = virtual_memory().total // (1024 ** 3) + 1
except ImportError:
MRIQC_LOG.warn('Total physical memory could not be estimated, using %d'
'GB as default', DEFAULT_MEM_GB)
total_memory = DEFAULT_MEM_GB
n_procs = 0
if opts.nthreads is not None:
MRIQC_LOG.warn('Option --nthreads has been deprecated in mriqc 0.8.8. '
'Please use --n_procs instead.')
n_procs = opts.nthreads
if opts.n_procs is not None:
n_procs = opts.n_procs
# Check physical memory
total_memory = opts.mem_gb
if total_memory < 0:
try:
from psutil import virtual_memory
total_memory = virtual_memory().total // (1024 ** 3) + 1
except ImportError:
MRIQC_LOG.warn('Total physical memory could not be estimated, using %d'
'GB as default', DEFAULT_MEM_GB)
total_memory = DEFAULT_MEM_GB
if total_memory > 0:
av_procs = total_memory // 4
if av_procs < 1:
MRIQC_LOG.warn('Total physical memory is less than 4GB, memory allocation'
' problems are likely to occur.')
n_procs = 1
elif n_procs > av_procs:
n_procs = av_procs
settings = {
'bids_dir': bids_dir,
'write_graph': opts.write_graph,
'testing': opts.testing,
plugin_settings = loadyml(pfile)
else:
# Setup multiprocessing
if settings['n_procs'] == 0:
settings['n_procs'] = 1
max_parallel_ants = cpu_count() // settings['ants_nthreads']
if max_parallel_ants > 1:
settings['n_procs'] = max_parallel_ants
if settings['n_procs'] > 1:
plugin_settings['plugin'] = 'MultiProc'
plugin_settings['plugin_args'] = {'n_procs': settings['n_procs']}
MRIQC_LOG.info(
'Running MRIQC-%s (analysis_level=%s, participant_label=%s)\n\tSettings=%s',
__version__, opts.analysis_level, opts.participant_label, settings)
# Set up participant level
if opts.analysis_level == 'participant':
for qctype in opts.data_type:
ms_func = getattr(mwc, 'ms_' + qctype)
workflow = ms_func(subject_id=opts.participant_label, session_id=opts.session_id,
run_id=opts.run_id, settings=settings)
if workflow is None:
MRIQC_LOG.warn(
'%s QC workflow - no scans were found for the given inputs',
'Anatomical' if qctype[:4] == 'anat' else 'Functional')
continue
workflow.base_dir = settings['work_dir']
if settings.get('write_graph', False):
workflow.write_graph()
from yaml import load as loadyml
with open(opts.use_plugin) as pfile:
plugin_settings = loadyml(pfile)
else:
# Setup multiprocessing
if settings['n_procs'] == 0:
settings['n_procs'] = 1
max_parallel_ants = cpu_count() // settings['ants_nthreads']
if max_parallel_ants > 1:
settings['n_procs'] = max_parallel_ants
if settings['n_procs'] > 1:
plugin_settings['plugin'] = 'MultiProc'
plugin_settings['plugin_args'] = {'n_procs': settings['n_procs']}
MRIQC_LOG.info(
'Running MRIQC-%s (analysis_level=%s, participant_label=%s)\n\tSettings=%s',
__version__, opts.analysis_level, opts.participant_label, settings)
# Set up participant level
if opts.analysis_level == 'participant':
for qctype in opts.data_type:
ms_func = getattr(mwc, 'ms_' + qctype)
workflow = ms_func(subject_id=opts.participant_label, session_id=opts.session_id,
run_id=opts.run_id, settings=settings)
if workflow is None:
MRIQC_LOG.warn(
'%s QC workflow - no scans were found for the given inputs',
'Anatomical' if qctype[:4] == 'anat' else 'Functional')
continue
workflow.base_dir = settings['work_dir']
def mock_config():
"""Create a mock config for documentation and testing purposes."""
from . import config
filename = Path(pkgrf("mriqc", "data/config-example.toml"))
settings = loads(filename.read_text())
for sectionname, configs in settings.items():
if sectionname != "environment":
section = getattr(config, sectionname)
section.load(configs, init=False)
config.nipype.init()
config.loggers.init()
config.execution.work_dir = Path(mkdtemp())
config.execution.bids_dir = Path(pkgrf("mriqc", "data/tests/ds000005")).absolute()
config.execution.init()
yield
def mock_config():
"""Create a mock config for documentation and testing purposes."""
from . import config
filename = Path(pkgrf("mriqc", "data/config-example.toml"))
settings = loads(filename.read_text())
for sectionname, configs in settings.items():
if sectionname != "environment":
section = getattr(config, sectionname)
section.load(configs, init=False)
config.nipype.init()
config.loggers.init()
config.execution.work_dir = Path(mkdtemp())
config.execution.bids_dir = Path(pkgrf("mriqc", "data/tests/ds000005")).absolute()
config.execution.init()
yield
scale = {"G": 1, "T": 10 ** 3, "M": 1e-3, "K": 1e-6, "B": 1e-9}
digits = "".join([c for c in value if c.isdigit()])
units = value[len(digits):] or "G"
return int(digits) * scale[units[0]]
def _drop_sub(value):
value = str(value)
return value.lstrip("sub-")
def _bids_filter(value):
from json import loads
if value and Path(value).exists():
return loads(Path(value).read_text())
verstr = f"MRIQC v{config.environment.version}"
currentv = Version(config.environment.version)
parser = ArgumentParser(
description=f"""\
MRIQC {config.environment.version}
Automated Quality Control and visual reports for Quality Assesment of structural \
(T1w, T2w) and functional MRI of the brain.
{config.DSA_MESSAGE}""",
formatter_class=ArgumentDefaultsHelpFormatter,
)
PathExists = partial(_path_exists, parser=parser)
PositiveInt = partial(_min_one, parser=parser)
# Arguments as specified by BIDS-Apps
# required, positional arguments
def dumps():
"""Format config into toml."""
from toml import dumps
return dumps(get())
def to_filename(filename):
"""Write settings to file."""
filename = Path(filename)
filename.write_text(dumps())
# Make sure loggers are started
loggers.init()
def git_versions_from_keywords(keywords, tag_prefix, verbose):
"""Get version information from git keywords."""
if not keywords:
raise NotThisMethod("no keywords at all, weird")
date = keywords.get("date")
if date is not None:
# git-2.2.0 added "%cI", which expands to an ISO-8601 -compliant
# datestamp. However we prefer "%ci" (which expands to an "ISO-8601
# -like" string, which we must then edit to make compliant), because
# it's been around since git-1.5.3, and it's too difficult to
# discover which version we're using, or to work around using an
# older one.
date = date.strip().replace(" ", "T", 1).replace(" ", "", 1)
refnames = keywords["refnames"].strip()
if refnames.startswith("$Format"):
if verbose:
print("keywords are unexpanded, not using")
raise NotThisMethod("unexpanded keywords, not a git-archive tarball")
refs = set([r.strip() for r in refnames.strip("()").split(",")])
# starting in git-1.8.3, tags are listed as "tag: foo-1.0" instead of