def test_multipolyfit(): xs = np.array([[1, 2, 3, 4, 5]]).T y = np.array([2, 3, 4, 5, 6]) betas = multipolyfit(xs, y, 2) # 1 + x + 0x**2 assert np.linalg.norm(betas - np.asarray((1, 1, 0))) < .0001
def test_multipolyfit(): xs = np.array([[1,2,3,4,5]]).T y = np.array([2,3,4,5,6]) betas = multipolyfit(xs, y, 2) # 1 + x + 0x**2 assert np.linalg.norm(betas - np.asarray((1,1,0))) < .0001
def test_model(): xs = np.array([[1,2,3,4,5], [1,-1,1,-1,1]]).T y = np.array([2,2,4,4,6]) model = multipolyfit(xs, y, 2, model_out = True) yhat = np.asarray(map(model, xs[:,0], xs[:,1])) assert np.linalg.norm(y - yhat) < .0001
def test_model(): xs = np.array([[1, 2, 3, 4, 5], [1, -1, 1, -1, 1]]).T y = np.array([2, 2, 4, 4, 6]) model = multipolyfit(xs, y, 2, model_out=True) yhat = np.asarray(map(model, xs[:, 0], xs[:, 1])) assert np.linalg.norm(y - yhat) < .0001
def test_sympy(): try: from sympy import symbols xs = np.array([[1,2,3,4,5], [1,-1,1,-1,1]]).T y = np.array([2,3,4,5,6]) betas, powers = multipolyfit(xs, y, 2, powers_out=True) expr = mk_sympy_function(betas, powers) x0, x1 = symbols('x0,x1') symbol_sets = {tuple(sorted(arg.free_symbols)) for arg in expr.args} assert symbol_sets == {tuple(), (x0,), (x1,), (x0, x1)} except ImportError: pass
def test_sympy(): try: from sympy import symbols xs = np.array([[1, 2, 3, 4, 5], [1, -1, 1, -1, 1]]).T y = np.array([2, 3, 4, 5, 6]) betas, powers = multipolyfit(xs, y, 2, powers_out=True) expr = mk_sympy_function(betas, powers) x0, x1 = symbols('x0,x1') symbol_sets = {tuple(sorted(arg.free_symbols)) for arg in expr.args} assert symbol_sets == {tuple(), (x0, ), (x1, ), (x0, x1)} except ImportError: pass
def __above_diaphragm_calculation(self, seg_prub, internal_resize_shape=[20, 20, 20], data_degradation=4): import multipolyfit as mpf # print seg_prub.dtype # seg_prub_tmp = misc.resize_to_shape(seg_prub, internal_resize_shape) # print seg_prub_tmp.dtype seg_prub_tmp = seg_prub z, x, y = np.nonzero(seg_prub_tmp) # data degradation x = x[::data_degradation] y = y[::data_degradation] z = z[::data_degradation] # import PyQt4.QtCore # PyQt4.QtCore.pyqtRemoveInputHook() # import ipdb; ipdb.set_trace() # noqa BREAKPOINT s = np.array([x, y]).T h = np.array(z) model = mpf.multipolyfit(s, h, self.rad_diaphragm, model_out=True) # tck = interpolate.bisplrep(x,y,z, s=10) ran = seg_prub.shape x = np.arange(ran[1]) y = np.arange(ran[2]) x, y = np.meshgrid(x, y) # sh = [len(x), len(y)] ##x, y = np.meshgrid(x, y) # z = np.asarray(map(self.__spl(x,y), x.reshape(-1), y.reshape(-1))).reshape(sh) z = np.floor(np.asarray(map(model, x.reshape(-1), y.reshape(-1)))).astype(int) x = x.reshape(z.shape) y = y.reshape(z.shape) cc = np.zeros(ran) for a in range(z.shape[0]): if (z[a] < ran[0]) and (z[a] >= 0): #self.segmentation[z[a], x[a], y[a]] = self.slab['diaphragm'] if self.smer == 0: cc[z[a] + 1:, x[a], y[a]] = 1 else: cc[:z[a] - 1, x[a], y[a]] = 1 # self.segmentation[z[a], x[a], y[a]] = self.slab['diaphragm'] for b in range(z[a] + 1, ran[0]): cc[b, x[a], y[a]] = 1 # cc = misc.resize_to_shape(cc, seg_prub.shape) return cc
def __above_diaphragm_calculation(self, seg_prub, internal_resize_shape=[20, 20, 20], data_degradation=4): import multipolyfit as mpf # print seg_prub.dtype # seg_prub_tmp = misc.resize_to_shape(seg_prub, internal_resize_shape) # print seg_prub_tmp.dtype seg_prub_tmp = seg_prub z, x, y = np.nonzero(seg_prub_tmp) # data degradation x = x[::data_degradation] y = y[::data_degradation] z = z[::data_degradation] # import PyQt4.QtCore # PyQt4.QtCore.pyqtRemoveInputHook() # import ipdb; ipdb.set_trace() # noqa BREAKPOINT s = np.array([x , y]).T h = np.array(z) model = mpf.multipolyfit(s, h, self.rad_diaphragm, model_out = True) # tck = interpolate.bisplrep(x,y,z, s=10) ran = seg_prub.shape x = np.arange( ran[1] ) y = np.arange( ran[2] ) x, y = np.meshgrid( x, y) # sh = [len(x), len(y)] ##x, y = np.meshgrid(x, y) # z = np.asarray(map(self.__spl(x,y), x.reshape(-1), y.reshape(-1))).reshape(sh) z = np.floor(np.asarray(map(model, x.reshape(-1), y.reshape(-1)))).astype(int) x = x.reshape(z.shape) y = y.reshape(z.shape) cc = np.zeros(ran) for a in range(z.shape[0]): if (z[a] < ran[0]) and (z[a] >= 0): #self.segmentation[z[a], x[a], y[a]] = self.slab['diaphragm'] if self.smer==0: cc[z[a]+1:, x[a], y[a]] = 1 else: cc[:z[a]-1, x[a], y[a]] = 1 # self.segmentation[z[a], x[a], y[a]] = self.slab['diaphragm'] for b in range(z[a]+1, ran[0]): cc[b, x[a], y[a]] = 1 # cc = misc.resize_to_shape(cc, seg_prub.shape) return cc
def fit(self, degree): """ Fit a multi-variable polynomial to the given reader data with a specified degree :param degree: The maximum degree of the polynomial function """ x = [] y = self.reader.space[self.reader.names[-1]] for i in range(0, len(self.reader.names)-1): x.append(self.reader.space[self.reader.names[i]]) (beta, powers) = multipolyfit(np.array(x).T, y, degree, powers_out=True) self.beta = beta self.degree = degree self.powers = powers print 'The global precision error is: ' + str(100*self.global_precision_error()) + '%.'
def __above_diaphragm_calculation(self, seg_prub, internal_resize_shape=[20, 20, 20]): # print seg_prub.dtype # seg_prub_tmp = misc.resize_to_shape(seg_prub, internal_resize_shape) # print seg_prub_tmp.dtype seg_prub_tmp = seg_prub z, x, y= np.nonzero(seg_prub_tmp) s = np.array([x , y]).T h = np.array(z) model = mpf.multipolyfit(s, h, self.rad_diaphragm, model_out = True) # tck = interpolate.bisplrep(x,y,z, s=10) ran = seg_prub.shape x = np.arange( ran[1] ) y = np.arange( ran[2] ) x, y = np.meshgrid( x, y) # sh = [len(x), len(y)] ##x, y = np.meshgrid(x, y) # z = np.asarray(map(self.__spl(x,y), x.reshape(-1), y.reshape(-1))).reshape(sh) z = np.floor(np.asarray(map(model, x.reshape(-1), y.reshape(-1)))).astype(int) x = x.reshape(z.shape) y = y.reshape(z.shape) cc = np.zeros(ran) for a in range(z.shape[0]): if (z[a] < ran[0]) and (z[a] >= 0): # self.segmentation[z[a], x[a], y[a]] = self.slab['diaphragm'] # if self.smer==0: # cc[z[a]+1:, x[a], y[a]] = 1 # else: # cc[:z[a]-1, x[a], y[a]] = 1 # self.segmentation[z[a], x[a], y[a]] = self.slab['diaphragm'] for b in range(z[a]+1, ran[0]): cc[b, x[a], y[a]] = 1 # cc = misc.resize_to_shape(cc, seg_prub.shape) return cc
( 8.75675676+(12*3))/48, ( 8.59459459+(12*3))/48, ( 8.43243243+(12*3))/48, ( 8.27027027+(12*3))/48, ( 8.10810811+(12*3))/48, ( 7.94594595+(12*3))/48, ( 7.78378378+(12*3))/48, ( 7.62162162+(12*3))/48, ( 7.45945946+(12*3))/48, ( 7.2972973+(12*3))/48, ( 7.13513514+(12*3))/48, ( 6.97297297+(12*3))/48, ( 6.81081081+(12*3))/48, ( 6.64864865+(12*3))/48, ( 6.48648649+(12*3))/48, ( 6.32432432+(12*3))/48, ( 6.16216216+(12*3))/48, (6. +(12*3))/48 , ( 5.83783784+(12*3))/48, ( 5.67567568+(12*3))/48, ( 5.51351351+(12*3))/48, ( 5.35135135+(12*3))/48, ( 5.18918919+(12*3))/48, ( 5.02702703+(12*3))/48, ( 4.86486486+(12*3))/48, ( 4.7027027+(12*3))/48 , ( 4.54054054+(12*3))/48, ( 4.37837838+(12*3))/48, ( 4.21621622+(12*3))/48, ( 4.05405405+(12*3))/48, ( 3.89189189+(12*3))/48, ( 3.72972973+(12*3))/48, ( 3.56756757+(12*3))/48, ( 3.40540541+(12*3))/48, ( 3.24324324+(12*3))/48, ( 3.08108108+(12*3))/48, ( 2.91891892+(12*3))/48, ( 2.75675676+(12*3))/48, ( 2.59459459+(12*3))/48, ( 2.43243243+(12*3))/48, ( 2.27027027+(12*3))/48, ( 2.10810811+(12*3))/48, ( 1.94594595+(12*3))/48, ( 1.78378378+(12*3))/48, ( 1.62162162+(12*3))/48, ( 1.45945946+(12*3))/48, ( 1.2972973+(12*3))/48 , ( 1.13513514+(12*3))/48, ( 0.97297297+(12*3))/48, ( 0.81081081+(12*3))/48, ( 0.64864865+(12*3))/48, ( 0.48648649+(12*3))/48, ( 0.32432432+(12*3))/48, ( 0.16216216+(12*3))/48, 0. ]) xs = zip(*xs) #performing multi variable linear regression and graphing it out, m, b = multipolyfit(xs, win_percent_array, 1, model_out = False) print(out) print(m) print(b) print(np.mean(win_percent_array)) print(np.std(win_percent_array)) print(statistics.variance(win_percent_array)) plt.figure('linear regression for win percentage') plt.plot(xs, win_percent_array, '.') plt.plot(point_diff2, b*(point_diff2) + m*(time) + out, '-')
def calculate_moment_arm_symbolically(model_file, results_dir): """Calculate the muscle moment arm matrix symbolically for a particular OpenSim model. """ print('Calculating...') # parse csv muscle_coordinates = {} with open(results_dir + 'muscle_coordinates.csv') as csv_file: reader = csv.reader(csv_file, delimiter=';') for row in reader: muscle_coordinates[row[0]] = row[1:] # load opensim model model = opensim.Model(model_file) state = model.initSystem() model_coordinates = {} # coordinates in multibody order for i, coordinate in enumerate(model.getCoordinateSet()): mbix = coordinate.getBodyIndex() mqix = coordinate.getMobilizerQIndex() model_coordinates[coordinate.getName()] = (mbix, mqix) model_coordinates = dict( sorted(model_coordinates.items(), key=lambda x: x[1])) for i, (key, value) in enumerate(model_coordinates.items()): model_coordinates[key] = i model_muscles = {} for i, muscle in enumerate(model.getMuscles()): model_muscles[muscle.getName()] = i # calculate moment arm matrix (R) symbolically R = [] sampling_dict = {} resolution = {1: 15, 2: 10, 3: 8, 4: 5, 5: 5} for muscle, k in tqdm( sorted(model_muscles.items(), key=operator.itemgetter(1))): coordinates = muscle_coordinates[muscle] N = resolution[len(coordinates)] # calculate moment arms for this muscle and spanning coordinates sampling_grid = construct_coordinate_grid(model, coordinates, N) moment_arm = [] for q in sampling_grid: for i, coordinate in enumerate(coordinates): model.updCoordinateSet().get(coordinate).setValue(state, q[i]) # realize stage model.realizePosition(state) tmp = [] for coordinate in coordinates: coord = model.getCoordinateSet().get(coordinate) tmp.append(model.getMuscles().get(muscle).computeMomentArm( state, coord)) moment_arm.append(tmp) moment_arm = np.array(moment_arm) sampling_dict[muscle] = { 'coordinates': coordinates, 'sampling_grid': sampling_grid, 'moment_arm': moment_arm } # polynomial regression degree = 5 muscle_moment_row = [0] * len(model_coordinates) for i, coordinate in enumerate(coordinates): coeffs, powers = multipolyfit(sampling_grid, moment_arm[:, i], degree, powers_out=True) polynomial = mk_sympy_function(coeffs, powers) polynomial = polynomial.subs({ sp.Symbol('x%d' % i): sp.Symbol(x) for i, x in enumerate(coordinates) }) muscle_moment_row[model_coordinates[coordinate]] = polynomial R.append(muscle_moment_row) # export data to file because the process is time consuming R = sp.Matrix(R) with open(results_dir + 'R.dat', 'wb') as f_R,\ open(results_dir + 'sampling_dict.dat', 'wb') as f_sd,\ open(results_dir + 'model_muscles.dat', 'wb') as f_mm,\ open(results_dir + 'model_coordinates.dat', 'wb') as f_mc: pickle.dump(R, f_R) pickle.dump(sampling_dict, f_sd) pickle.dump(model_muscles, f_mm) pickle.dump(model_coordinates, f_mc)
def regression(): xagPrices = dataloader.importToArray('XAGUSD5_2013070809') xags = [p['close'] for p in xagPrices] usdxPrices = dataloader.importToArray('_USDX5_2013070809') usdxs = [p['close'] for p in usdxPrices] us500Prices = dataloader.importToArray('_US5005_2013070809') us500s = [p['close'] for p in us500Prices] us30Prices = dataloader.importToArray('_US305_2013070809') us30s = [p['close'] for p in us30Prices] nq100Prices = dataloader.importToArray('_NQ1005_2013070809') nq100s = [p['close'] for p in nq100Prices] l = len(xagPrices) xs = [] ys = [] for i in range(10, l - 1): xag = xagPrices[i] xagd = xags[i-10+1 : i+1] dtlong = xag['dtlong'] usdxd = findData(dtlong, usdxPrices, usdxs) us500d = findData(dtlong, us500Prices, us500s) us30d = findData(dtlong, us30Prices, us30s) nq100d = findData(dtlong, nq100Prices, nq100s) if not (usdxd and us500d and us30d and nq100d): log.debug('Insufficient Data. ' + xag['date'] + ' ' + xag['time']) continue xs.append([xags[i], slope(xagd), std(xagd), slope(usdxd), std(usdxd), slope(us500d), std(us500d), slope(us30d), std(us30d), slope(nq100d), std(nq100d)]) ys.append(xags[i+1] - xags[i]) #print xs #print ys xxs = np.array(xs) #print xsT yys = np.array(ys) mymodel = multipolyfit(xxs.T, yys, 20, model_out = False) #mymodel = ols.ols(yys, xxs, 'y',['x1', 'x2', 'x3', 'x4', 'x5', 'x6', 'x7', 'x8', 'x9', 'x10', 'x11']) #print mymodel.b print mymodel #print mymodel.summary() return mymodel yps = [] co = mymodel.b for i in range(len(xs)): yp = co[0] + xs[i][0] * co[1] + xs[i][1] * co[2] + xs[i][2] * co[3] + xs[i][3] * co[4] yp += xs[i][4] * co[5] + xs[i][5] * co[6] + xs[i][6] * co[7] + xs[i][7] * co[8] yp += xs[i][8] * co[9] + xs[i][9] * co[10] + xs[i][10] * co[11] yps.append(yp) ts = range(len(xs)) plt.plot(ts, ys, 'o', label='Original data', linestyle='-', marker='') plt.plot(ts, yps, 'r', label='Fitted line') plt.legend() plt.show()
def calculate_moment_arm_symbolically(model_file, results_dir): """Calculate the muscle moment arm matrix symbolically for a particular OpenSim model. """ print('Calculating...') # parse csv muscle_coordinates = {} with open(results_dir + 'muscle_coordinates.csv') as csv_file: reader = csv.reader(csv_file, delimiter=';') for row in reader: muscle_coordinates[row[0]] = row[1:] # load opensim model model = opensim.Model(model_file) state = model.initSystem() model_coordinates = {} for i in range(0, model.getNumCoordinates()): model_coordinates[model.getCoordinateSet().get(i).getName()] = i model_muscles = {} for i in range(0, model.getNumControls()): model_muscles[model.getMuscles().get(i).getName()] = i # calculate moment arm matrix (R) symbolically R = [] sampling_dict = {} resolution = {1: 15, 2: 10, 3: 8, 4: 5, 5: 5} for muscle, k in tqdm( sorted(model_muscles.items(), key=operator.itemgetter(1))): # get initial state each time state = model.initSystem() coordinates = muscle_coordinates[muscle] N = resolution[len(coordinates)] # calculate moment arms for this muscle and spanning coordinates sampling_grid = construct_coordinate_grid(model, coordinates, N) moment_arm = [] for q in sampling_grid: for i, coordinate in enumerate(coordinates): model.updCoordinateSet().get(coordinate).setValue(state, q[i]) # model.realizePosition(state) tmp = [] for coordinate in coordinates: coord = model.getCoordinateSet().get(coordinate) tmp.append(model.getMuscles().get(muscle).computeMomentArm( state, coord)) moment_arm.append(tmp) moment_arm = np.array(moment_arm) sampling_dict[muscle] = { 'coordinates': coordinates, 'sampling_grid': sampling_grid, 'moment_arm': moment_arm } # polynomial regression degree = 4 muscle_moment_row = [0] * len(model_coordinates) for i, coordinate in enumerate(coordinates): coeffs, powers = multipolyfit(sampling_grid, moment_arm[:, i], degree, powers_out=True) polynomial = mk_sympy_function(coeffs, powers) polynomial = polynomial.subs( dict( zip(polynomial.free_symbols, [sp.Symbol(x) for x in coordinates]))) muscle_moment_row[model_coordinates[coordinate]] = polynomial R.append(muscle_moment_row) # export data to file because the process is time consuming R = sp.Matrix(R) pickle.dump(R, file(results_dir + 'R.dat', 'w')) pickle.dump(sampling_dict, file(results_dir + 'sampling_dict.dat', 'w')) pickle.dump(model_muscles, file(results_dir + 'model_muscles.dat', 'w')) pickle.dump(model_coordinates, file(results_dir + 'model_coordinates.dat', 'w'))