| Conditions | 20 |
| Total Lines | 59 |
| Lines | 0 |
| Ratio | 0 % |
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:
If many parameters/temporary variables are present:
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 -*- |
||
| 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 | |||
| 82 |