File: parallel_run.py

package info (click to toggle)
nipy 0.6.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, trixie
  • size: 7,352 kB
  • sloc: python: 39,115; ansic: 30,931; makefile: 210; sh: 93
file content (130 lines) | stat: -rw-r--r-- 3,591 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*-
# vi: set ft=python sts=4 ts=4 sw=4 et:
"""Script to run the main analyses in parallel, using the IPython machinery.

See ``fiac_example.py``.
"""
#-----------------------------------------------------------------------------
# Imports
#-----------------------------------------------------------------------------

import os

import numpy as np
from IPython import parallel

#-----------------------------------------------------------------------------
# Utility functions
#-----------------------------------------------------------------------------

_client = None
def setup_client():
    """Get a Client and initialize it.

    This assumes that all nodes see a shared filesystem.
    """
    global _client
    if _client is None:
        _client = parallel.Client()
        mydir = os.path.split(os.path.abspath(__file__))[0]
        def cd(path):
            import os
            os.chdir(path)
        _client[:].apply_sync(cd, mydir)
    return _client


def getruns():
    for i in range(16):
        for j in range(1,5):
            yield i, j

def getvals():
    for con in ['sentence:speaker_0',
                'sentence_1',
                'sentence_0',
                'sentence:speaker_1',
                'speaker_1',
                'speaker_0',
                'constant_1',
                'constant_0']:
        for design in ['block', 'event']:
            yield design, con

#-----------------------------------------------------------------------------
# Main analysis functions
#-----------------------------------------------------------------------------

def fitruns():
    """Run the basic model fit."""
    rc = setup_client()
    view = rc.load_balanced_view()
    i_s, j_s = zip(*getruns())

    def _fit(subj, run):
        import fiac_example
        try:
            return fiac_example.run_model(subj, run)
        except OSError:
            pass

    return view.map(_fit, i_s, j_s)


def fitfixed():
    """Run the fixed effects analysis for all subjects."""
    rc = setup_client()
    view = rc.load_balanced_view()
    subjects = range(16)

    def _fit(subject):
        import fiac_example
        try:
            fiac_example.fixed_effects(subject, "block")
        except OSError:
            pass
        try:
            fiac_example.fixed_effects(subject, "event")
        except OSError:
            pass

    return view.map(_fit, subjects)


def fitgroup():
    """Run the group analysis"""
    rc = setup_client()
    view = rc.load_balanced_view()
    d_s, c_s = zip(*getvals())

    def _fit(d, c):
        import fiac_example
        return fiac_example.group_analysis(d, c)

    return view.map(_fit, d_s, c_s)


def run_permute_test(design, contrast, nsample=1000):
    rc = setup_client()
    dview = rc[:]
    nnod = len(dview)
    # Samples per node.  Round up
    ns_nod = np.ceil(nsample / float(nnod))

    def _run_test(n, des, con):
        import fiac_example
        from fiac_example import GROUP_MASK
        min_vals, max_vals = fiac_example.permutation_test(des, con,
                                       GROUP_MASK, n)
        return min_vals, max_vals

    ar = dview.apply_async(_run_test, ns_nod, design, contrast)
    min_vals, max_vals = zip(*list(ar))
    return np.concatenate(min_vals), np.concatenate(max_vals)


#-----------------------------------------------------------------------------
# Script entry point
#-----------------------------------------------------------------------------
if __name__ == '__main__':
    pass