Completed
Pull Request — master (#127)
by Jasper
01:07
created

niprov.handler()   F

Complexity

Conditions 20

Size

Total Lines 59

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 20
dl 0
loc 59
rs 3.2035

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

Complexity

Complex classes like niprov.handler() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
# -*- coding: utf-8 -*-
2
"""mnefun support module
3
4
This module provides handlers to attach to mnefun events.
5
6
::
7
8
    import mnefun
9
    import niprov.mnefunsupport
10
    params = mnefun.Params()
11
    params.on_process = niprov.mnefunsupport.handler
12
13
"""
14
import os
15
from niprov.dependencies import Dependencies
16
from niprov import Context
17
18
19
def handler(text, func, out, params, dependencies=Dependencies()):
20
    """mnefun on_process handler
21
22
    Responds to the following mnefun steps:
23
    fetch_raw_files: Runs discover on the subjects' raw data dirs.
24
    fetch_sss_files,
25
    apply_preprocessing_combined,
26
    save_epochs: For these steps, runs log for the new files.
27
    """
28
    listener = dependencies.getListener()
29
    libs = dependencies.getLibraries()
30
    provenance = Context()
31
    funcname = func.func_name
32
    listener.mnefunEventReceived(funcname)
33
    paramdict = {}
34
    for paramname in dir(params):
35
        objectvalue = getattr(params, paramname)
36
        blacklist = ['get_projs_from', 'inv_runs']
37
        if hasattr(objectvalue, '__call__'):
38
            continue
39
        if paramname[0] == '_':
40
            continue
41
        if 'array' in str(type(objectvalue)):
42
            continue
43
        if paramname in blacklist:
44
            continue
45
        paramdict[paramname] = objectvalue
46
    subjects = [params.subjects[i] for i in params.subject_indices]
47
    for subj in subjects:
48
        customprov = {'mnefun':paramdict}
49
        if funcname in ['fetch_raw_files', 'score_fun', 'fetch_sss_files']:
50
            rawfiles = libs.mnefun.get_raw_fnames(params, subj, 'raw', add_splits=True,
51
                                                  run_indices=params.subject_run_indices)
52
        if funcname == 'fetch_raw_files':
53
            for f in rawfiles:
54
                provenance.add(f, provenance=customprov)
55
        elif funcname == 'score_fun':
56
            eventfiles = libs.mnefun._paths.get_event_fnames(params, subj,
57
                                                             params.subject_run_indices)
58
            for rawfile, eventfile in zip(rawfiles, eventfiles):
59
                provenance.log(eventfile, 'scoring', rawfile, provenance=customprov)
60
        elif funcname == 'fetch_sss_files':
61
            trans = 'Signal Space Separation'
62
            sssfiles = libs.mnefun.get_raw_fnames(params, subj, 'sss')
63
            for rawfile, sssfile in zip(rawfiles, sssfiles):
64
                provenance.log(sssfile, trans, rawfile, provenance=customprov)
65
        elif funcname == 'apply_preprocessing_combined':
66
            trans = 'Signal Space Projection'
67
            sssfiles = libs.mnefun.get_raw_fnames(params, subj, 'sss')
68
            pcafiles = libs.mnefun.get_raw_fnames(params, subj, 'pca')
69
            for sssfile, pcafile in zip(sssfiles, pcafiles):
70
                provenance.log(pcafile, trans, sssfile, provenance=customprov)
71
        elif funcname == 'save_epochs':
72
            pcafiles = libs.mnefun.get_raw_fnames(params, subj, 'pca')
73
            evtfiles = libs.mnefun._paths.get_epochs_evokeds_fnames(params, 
74
                subj, params.analyses)
75
            for evtfile in evtfiles:
76
                if os.path.isfile(evtfile[0]):
77
                    provenance.log(evtfile[0], 'Epoching', pcafiles, provenance=customprov)
78
79
80
#        self.reject = dict(eog=np.inf, grad=1500e-13, mag=5000e-15, eeg=150e-6)
81
#        self.flat = dict(eog=-1, grad=1e-13, mag=1e-15, eeg=1e-6)
82