From 9c8cf8bde19d53fb089656d5c762f965b8c9ae4f Mon Sep 17 00:00:00 2001 From: Tobias Klauser Date: Thu, 10 Jan 2013 15:18:48 +0100 Subject: scripts: random-muscle-activation: Implement basic functionality The variation of the values can also be specified (-d/-D), in general they should be -N/+N --- scripts/random-muscle-activation.py | 65 +++++++++++++++++++++++++++++++------ 1 file changed, 55 insertions(+), 10 deletions(-) (limited to 'scripts/random-muscle-activation.py') diff --git a/scripts/random-muscle-activation.py b/scripts/random-muscle-activation.py index e0c3899..fb225cc 100755 --- a/scripts/random-muscle-activation.py +++ b/scripts/random-muscle-activation.py @@ -1,6 +1,8 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- # +# random-muscle-activation.py -- Generate random muscle activation data in CSV +# # Copyright (C) 2013 Tobias Klauser # # This program is free software; you can redistribute it and/or modify @@ -11,50 +13,73 @@ import os import sys import getopt import numpy as np +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 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 -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: {}) -""".format(os.path.basename(sys.argv[0]), DEFAULT_N, DEFAULT_XMIN, DEFAULT_XMAX, - DEFAULT_YMIN, DEFAULT_YMAX)) + -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_XMIN, DEFAULT_XMAX, + DEFAULT_YMIN, DEFAULT_YMAX, + DEFAULT_YDMIN, DEFAULT_YDMAX)) -def generate_activation_data(N, xmin, xmax, ymin, ymax): +def generate_activation_data(N, xmin, xmax, ymin, ymax, ydmin, ydmax): 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] = (ymax - ymin) * np.random.random_sample() + ymin + 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 def main(): try: - opts, args = getopt.getopt(sys.argv[1:], "fn:x:X:y:Y:h") + opts, args = getopt.getopt(sys.argv[1:], "fn:px:X:y:Y:d:D:h") except getopt.GetoptError as err: print(str(err)) usage() sys.exit(-1) - overwrite = False + do_overwrite = False + do_plot = False N = DEFAULT_N 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': - overwrite = True + do_overwrite = True elif o == '-n': try: N = int(a) @@ -62,6 +87,8 @@ def main(): print("Error: number of data points must be an integer > 0") usage() sys.exit(-1) + elif o == '-p': + do_plot = True elif o == '-x': try: xmin = float(a) @@ -90,6 +117,20 @@ def main(): 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) @@ -105,16 +146,20 @@ def main(): sys.exit(-1) for a in args: - if not overwrite and os.path.exists(a): + 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) + act = generate_activation_data(N, xmin, xmax, ymin, ymax, ydmin, ydmax) f.write('x,y\n') - for x,y in act: - f.write('{},{}\n'.format(x,y)) + 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) -- cgit v1.2.3-54-g00ecf