summaryrefslogtreecommitdiff
path: root/scripts/random-muscle-activation.py
blob: 870fee30ebecba484232ea75cc70833661f9a7e3 (plain)
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# random-muscle-activation.py -- Generate random muscle activation data in CSV
#
# Copyright (C) 2013 Tobias Klauser <tklauser@distanz.ch>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.

import os
import sys
import getopt
import numpy as np
import matplotlib.pyplot as plt
import plotcsv

DEFAULT_N = 1000
DEFAULT_XMIN = 0.0
DEFAULT_XMAX = 100.0
DEFAULT_YMIN = 0.0
DEFAULT_YMAX = 50.0
DEFAULT_YDMIN = -0.5
DEFAULT_YDMAX = 0.5

WINDOWS = [ 'flat', 'hanning', 'hamming', 'bartlett', 'blackman' ]
WINDOW_LEN = 11
DEFAULT_WINDOW = 'blackman'

def usage():
    print("""usage: {} [OPTION...] FILE...

Generate random muscle activation data in CSV.

options:
    -f    force overwrite of existing file
    -n N  set number of data points to N (default: {})
    -p    plot data after generating it
    -s    smoothen the data using blackman window function
    -i N  set minimum initial y value to N (default: {})
    -I N  set maximum initial y value to N (default: {})
    -x N  set minimum x value to N (default: {})
    -X N  set maximum x value to N (default: {})
    -y N  set minimum y value to N (default: {})
    -Y N  set maximum y value to N (default: {})
    -d N  set minimum y change value to N (default: {})
    -D N  set maximum y change value to N (default: {})""".format(
        os.path.basename(sys.argv[0]), DEFAULT_N,
        DEFAULT_YMIN, DEFAULT_YMAX,
        DEFAULT_XMIN, DEFAULT_XMAX,
        DEFAULT_YMIN, DEFAULT_YMAX,
        DEFAULT_YDMIN, DEFAULT_YDMAX))

def generate_activation_data(N, xmin, xmax, ymin, ymax, ydmin, ydmax, imin, imax):
    X = np.array([ ((x / float(N)) * (xmax - xmin)) + xmin for x in range(0, N) ])
    Y = np.zeros(N)
    # Y = np.array([ ((y / float(N)) * (ymax - ymin)) + ymin for y in range(0, N) ])

    Y[0] = (imax - imin) * np.random.random_sample() + imin
    for y in range(1, N):
        Y[y] = Y[y-1] + ((ydmax - ydmin) * np.random.random_sample() + ydmin)
        if Y[y] < ymin:
            Y[y] = ymin
        if Y[y] > ymax:
            Y[y] = ymax

    act = np.array((X, Y)).transpose()

    return act

# based on scipy.org/Cookbook/SignalSmooth
def smoothen(x, window=DEFAULT_WINDOW, window_len=WINDOW_LEN):
    """
    smoothen signal using a window with requested size

    input:
      x:          the input signal
      window:     type of window from 'flat', 'hanning', 'hamming', 'bartlett',
                  'blackman'; the 'flat' method will apply a moving average
      window_len: dimension of smoothing window, must be an odd integer

    output:
      smoothened signal
    """

    if x.ndim != 1:
        raise ValueError, "only 1-dimensional arrays can be smoothened"
    if x.size < window_len:
        raise ValueError, "input vector needs to be bigger than window size"
    if type(window_len) != int or window_len % 2 != 1:
        raise ValueError, "window length must be an odd integer"

    if window_len < 3:
        return x
    if not window in WINDOWS:
        raise ValueError, "window type must be one of " + ", ".join([ "'" + w + "'" for w in WINDOWS ])

    s = np.r_[x[window_len-1:0:-1],x,x[-1:-window_len:-1]]
    if window == 'flat':
        w = np.ones(window_len, 'd')
    else:
        w = eval('np.' + window + '(window_len)')

    return np.convolve(w / w.sum(), s, mode='valid')

def main():
    try:
        opts, args = getopt.getopt(sys.argv[1:], "fn:psi:I:x:X:y:Y:d:D:h")
    except getopt.GetoptError as err:
        print(str(err))
        usage()
        sys.exit(-1)

    do_overwrite = False
    do_plot = False
    do_smooth = False
    N = DEFAULT_N
    imin = DEFAULT_YMIN
    imax = DEFAULT_YMAX
    xmin = DEFAULT_XMIN
    xmax = DEFAULT_XMAX
    ymin = DEFAULT_YMIN
    ymax = DEFAULT_YMAX
    ydmin = DEFAULT_YDMIN
    ydmax = DEFAULT_YDMAX
    for o, a in opts:
        if o == '-f':
            do_overwrite = True
        elif o == '-n':
            try:
                N = int(a)
            except ValueError:
                print("Error: number of data points must be an integer > 0")
                usage()
                sys.exit(-1)
        elif o == '-p':
            do_plot = True
        elif o == '-s':
            do_smooth = True
        elif o == '-i':
            try:
                imin = float(a)
            except ValueError:
                print("Error: imin must be a float")
                usage()
                sys.exit(-1)
        elif o == '-I':
            try:
                imax = float(a)
            except ValueError:
                print("Error: imax must be a float")
                usage()
                sys.exit(-1)
        elif o == '-x':
            try:
                xmin = float(a)
            except ValueError:
                print("Error: xmin must be a float")
                usage()
                sys.exit(-1)
        elif o == '-X':
            try:
                xmax = float(a)
            except ValueError:
                print("Error: xmax must be a float")
                usage()
                sys.exit(-1)
        elif o == '-y':
            try:
                ymin = float(a)
            except ValueError:
                print("Error: ymin must be a float")
                usage()
                sys.exit(-1)
        elif o == '-Y':
            try:
                ymax = float(a)
            except ValueError:
                print("Error: ymax must be a float")
                usage()
                sys.exit(-1)
        elif o == '-d':
            try:
                ydmin = float(a)
            except ValueError:
                print("Error: ydmin must be a float")
                usage()
                sys.exit(-1)
        elif o == '-D':
            try:
                ydmax = float(a)
            except ValueError:
                print("Error: ydmax must be a float")
                usage()
                sys.exit(-1)
        elif o == '-h':
            usage()
            sys.exit(0)
        else:
            assert False, "unhandled option"

    if N <= 0:
        print("Error: invalid number of data points ({}), must be > 0".format(N))
        sys.exit(-1)

    if not xmin < xmax:
        print("Error: xmin must be smaller than xmax")
        sys.exit(-1)

    if not ymin < ymax:
        print("Error: ymin must be smaller than ymax")
        sys.exit(-1)

    if not ydmin <= ydmax:
        print("Error: ydmin must be smaller than or equal to ydmax")
        sys.exit(-1)

    if not imin < imax:
        print("Error: imin must be smaller than imax")
        sys.exit(-1)

    if imin < ymin or imin > ymax:
        print("Error: imin must be between ymin and ymax ({} <= {} <= {})".format(ymin, imin, ymax))
        sys.exit(-1)

    if imax < ymin or imax > ymax:
        print("Error: imax must be between ymin and ymax ({} <= {} <= {})".format(ymin, imax, ymax))
        sys.exit(-1)

    if len(args) < 1:
        print("Error: no output file(s) specified")
        usage()
        sys.exit(-1)

    for a in args:
        if not do_overwrite and os.path.exists(a):
            print("Error: output file {} already exists, use -f to overwrite".format(a))
            sys.exit(-1)

    print("Generating {} files with {} data points each...".format(len(args), N))
    for a in args:
        with open(a, 'w') as f:
            act = generate_activation_data(N, xmin, xmax, ymin, ymax, ydmin, ydmax, imin, imax)
            if do_smooth:
                all_in_range = False
                n = 0
                while not all_in_range:
                    print("Smooth pass " + str(n))
                    sact = smoothen(act[:,1], DEFAULT_WINDOW, WINDOW_LEN)
                    sact = sact[(WINDOW_LEN - 1) / 2:-(WINDOW_LEN - 1) / 2]
                    if sact.min() >= ymin and sact.max() <= ymax:
                        all_in_range = True
                    n += 1
                plt.plot(act[:,1])
                plt.plot(sact)
                plt.legend(['original', 'smoothened'])
                plt.show()
                act[:,1] = sact
            # XXX: temp
            #    plt.plot(act[:,1])
            #    l = ['original']
            #    for w in WINDOWS:
            #        sact = smoothen(act[:,1], w, WINDOW_LEN)
            #        plt.plot(sact[(WINDOW_LEN-1)/2:-(WINDOW_LEN-1)/2])
            #        l.append(w)
            #    plt.legend(l)
            #    plt.show()

            f.write('x,y\n')
            for x, y in act:
                f.write('{},{}\n'.format(x, y))
            if do_plot:
                print("Plotting data for {}, please close window to continue...".format(a))
                plotcsv.plot(act[:,0], act[:,1], xmax, ymax, 'x', 'y', a, False, False)

    sys.exit(0)

if __name__ == '__main__':
    main()