116
116
----------------------
117
117
The :py:mod:`config` is responsible for other conveniency actions.
118
118
119
- * Switching Python's `` multiprocessing` ` to *forkserver* mode.
119
+ * Switching Python's :obj:` multiprocessing` to *forkserver* mode.
120
120
* Set up a filter for warnings as early as possible.
121
- * Automated I/O magic operations:
122
-
123
- * :obj:`Path` \<-\> :obj:`str` \<-\> :obj:`Path`).
124
- * :py:class:`~niworkflows.util.spaces.SpatialReferences` \<-\> :obj:`str` \<-\>
125
- :py:class:`~niworkflows.util.spaces.SpatialReferences`
126
- * :py:class:`~bids.layout.BIDSLayout` \<-\> :obj:`str` \<-\>
127
- :py:class:`~bids.layout.BIDSLayout`
121
+ * Automated I/O magic operations. Some conversions need to happen in the
122
+ store/load processes (e.g., from/to :obj:`~pathlib.Path` \<-\> :obj:`str`,
123
+ :py:class:`~bids.layout.BIDSLayout`, etc.)
128
124
129
125
"""
130
126
from multiprocessing import set_start_method
@@ -229,7 +225,7 @@ def __init__(self):
229
225
raise RuntimeError ('Configuration type is not instantiable.' )
230
226
231
227
@classmethod
232
- def load (cls , settings ):
228
+ def load (cls , settings , init = True ):
233
229
"""Store settings from a dictionary."""
234
230
for k , v in settings .items ():
235
231
if v is None :
@@ -240,6 +236,12 @@ def load(cls, settings):
240
236
if hasattr (cls , k ):
241
237
setattr (cls , k , v )
242
238
239
+ if init :
240
+ try :
241
+ cls .init ()
242
+ except AttributeError :
243
+ pass
244
+
243
245
@classmethod
244
246
def get (cls ):
245
247
"""Return defined settings."""
@@ -331,6 +333,32 @@ def get_plugin(cls):
331
333
out ['plugin_args' ]['memory_gb' ] = float (cls .memory_gb )
332
334
return out
333
335
336
+ @classmethod
337
+ def init (cls ):
338
+ """Set NiPype configurations."""
339
+ from nipype import config as ncfg
340
+
341
+ # Configure resource_monitor
342
+ if cls .resource_monitor :
343
+ ncfg .update_config ({
344
+ 'monitoring' : {
345
+ 'enabled' : cls .resource_monitor ,
346
+ 'sample_frequency' : '0.5' ,
347
+ 'summary_append' : True ,
348
+ }
349
+ })
350
+ ncfg .enable_resource_monitor ()
351
+
352
+ # Nipype config (logs and execution)
353
+ ncfg .update_config ({
354
+ 'execution' : {
355
+ 'crashdump_dir' : str (execution .log_dir ),
356
+ 'crashfile_format' : cls .crashfile_format ,
357
+ 'get_linked_libs' : cls .get_linked_libs ,
358
+ 'stop_on_first_crash' : cls .stop_on_first_crash ,
359
+ }
360
+ })
361
+
334
362
335
363
class execution (_Config ):
336
364
"""Configure run-level settings."""
@@ -352,8 +380,7 @@ class execution(_Config):
352
380
fs_subjects_dir = None
353
381
"""FreeSurfer's subjects directory."""
354
382
layout = None
355
- """A py:class:`~bids.layout.BIDSLayout` object, see
356
- :py:func:`~fmriprep.config.init_layout`."""
383
+ """A :py:class:`~bids.layout.BIDSLayout` object, see :py:func:`init`."""
357
384
log_dir = None
358
385
"""The path to a directory that contains execution logs."""
359
386
log_level = 25
@@ -397,6 +424,22 @@ class execution(_Config):
397
424
'work_dir' ,
398
425
)
399
426
427
+ @classmethod
428
+ def init (cls ):
429
+ """Create a new BIDS Layout accessible with :attr:`~execution.layout`."""
430
+ if cls ._layout is None :
431
+ import re
432
+ from bids .layout import BIDSLayout
433
+ work_dir = cls .work_dir / 'bids.db'
434
+ work_dir .mkdir (exist_ok = True , parents = True )
435
+ cls ._layout = BIDSLayout (
436
+ str (cls .bids_dir ),
437
+ validate = False ,
438
+ # database_path=str(work_dir),
439
+ ignore = ("code" , "stimuli" , "sourcedata" , "models" ,
440
+ "derivatives" , re .compile (r'^\.' )))
441
+ cls .layout = cls ._layout
442
+
400
443
401
444
# These variables are not necessary anymore
402
445
del _fs_license
@@ -467,18 +510,7 @@ class workflow(_Config):
467
510
468
511
469
512
class loggers :
470
- """
471
- Setup loggers, providing access to them across the *fMRIPrep* run.
472
-
473
- * Add new logger levels (25: IMPORTANT, and 15: VERBOSE).
474
- * Add a new sub-logger (``cli``).
475
- * Logger configuration.
476
-
477
- See also:
478
-
479
- .. autofunction: init_loggers
480
-
481
- """
513
+ """Keep loggers easily accessible (see :py:func:`init`)."""
482
514
483
515
_fmt = "%(asctime)s,%(msecs)d %(name)-2s " "%(levelname)-2s:\n \t %(message)s"
484
516
_datefmt = "%y%m%d-%H:%M:%S"
@@ -494,13 +526,41 @@ class loggers:
494
526
utils = nlogging .getLogger ('nipype.utils' )
495
527
"""NiPype's utils logger."""
496
528
529
+ @classmethod
530
+ def init (cls ):
531
+ """
532
+ Set the log level, initialize all loggers into :py:class:`loggers`.
533
+
534
+ * Add new logger levels (25: IMPORTANT, and 15: VERBOSE).
535
+ * Add a new sub-logger (``cli``).
536
+ * Logger configuration.
537
+
538
+ """
539
+ from nipype import config as ncfg
540
+ _handler = logging .StreamHandler (stream = sys .stdout )
541
+ _handler .setFormatter (
542
+ logging .Formatter (fmt = cls ._fmt , datefmt = cls ._datefmt )
543
+ )
544
+ cls .cli .addHandler (_handler )
545
+ cls .default .setLevel (execution .log_level )
546
+ cls .cli .setLevel (execution .log_level )
547
+ cls .interface .setLevel (execution .log_level )
548
+ cls .workflow .setLevel (execution .log_level )
549
+ cls .utils .setLevel (execution .log_level )
550
+ ncfg .update_config ({
551
+ 'logging' : {
552
+ 'log_directory' : str (execution .log_dir ),
553
+ 'log_to_file' : True
554
+ },
555
+ })
556
+
497
557
498
558
def from_dict (settings ):
499
559
"""Read settings from a flat dictionary."""
500
560
nipype .load (settings )
501
561
execution .load (settings )
502
562
workflow .load (settings )
503
- init_loggers ()
563
+ loggers . init ()
504
564
505
565
506
566
def load (filename ):
@@ -512,10 +572,7 @@ def load(filename):
512
572
if sectionname != 'environment' :
513
573
section = getattr (sys .modules [__name__ ], sectionname )
514
574
section .load (configs )
515
- init_nipype ()
516
- init_loggers ()
517
575
init_spaces ()
518
- init_layout ()
519
576
520
577
521
578
def get (flat = False ):
@@ -546,43 +603,6 @@ def to_filename(filename):
546
603
filename .write_text (dumps ())
547
604
548
605
549
- def init_layout ():
550
- """Create a new BIDS Layout accessible with :attr:`~execution.layout`."""
551
- if execution ._layout is None :
552
- import re
553
- from bids .layout import BIDSLayout
554
- work_dir = execution .work_dir / 'bids.db'
555
- work_dir .mkdir (exist_ok = True , parents = True )
556
- execution ._layout = BIDSLayout (
557
- str (execution .bids_dir ),
558
- validate = False ,
559
- # database_path=str(work_dir),
560
- ignore = ("code" , "stimuli" , "sourcedata" , "models" ,
561
- "derivatives" , re .compile (r'^\.' )))
562
- execution .layout = execution ._layout
563
-
564
-
565
- def init_loggers ():
566
- """Set the current log level to all loggers."""
567
- from nipype import config as ncfg
568
- _handler = logging .StreamHandler (stream = sys .stdout )
569
- _handler .setFormatter (
570
- logging .Formatter (fmt = loggers ._fmt , datefmt = loggers ._datefmt )
571
- )
572
- loggers .cli .addHandler (_handler )
573
- loggers .default .setLevel (execution .log_level )
574
- loggers .cli .setLevel (execution .log_level )
575
- loggers .interface .setLevel (execution .log_level )
576
- loggers .workflow .setLevel (execution .log_level )
577
- loggers .utils .setLevel (execution .log_level )
578
- ncfg .update_config ({
579
- 'logging' : {
580
- 'log_directory' : str (execution .log_dir ),
581
- 'log_to_file' : True
582
- },
583
- })
584
-
585
-
586
606
def init_spaces (checkpoint = True ):
587
607
"""Initialize the :attr:`~workflow.spaces` setting."""
588
608
from niworkflows .utils .spaces import Reference , SpatialReferences
@@ -624,29 +644,3 @@ def init_spaces(checkpoint=True):
624
644
625
645
# Make the SpatialReferences object available
626
646
workflow .spaces = spaces
627
-
628
-
629
- def init_nipype ():
630
- """Set NiPype configurations."""
631
- from nipype import config as ncfg
632
-
633
- # Configure resource_monitor
634
- if nipype .resource_monitor :
635
- ncfg .update_config ({
636
- 'monitoring' : {
637
- 'enabled' : nipype .resource_monitor ,
638
- 'sample_frequency' : '0.5' ,
639
- 'summary_append' : True ,
640
- }
641
- })
642
- ncfg .enable_resource_monitor ()
643
-
644
- # Nipype config (logs and execution)
645
- ncfg .update_config ({
646
- 'execution' : {
647
- 'crashdump_dir' : str (execution .log_dir ),
648
- 'crashfile_format' : nipype .crashfile_format ,
649
- 'get_linked_libs' : nipype .get_linked_libs ,
650
- 'stop_on_first_crash' : nipype .stop_on_first_crash ,
651
- }
652
- })
0 commit comments