def __init__(self, fileName=None, w=None): """Loads a .mat file and data from the database to construct a data frame.""" if fileName is None: self.fileName = PATH_TO_SYSTEM_ID_DATA else: self.fileName = fileName if w is None: w = np.logspace(-1.0, 1.0, num=100) mat = loadmat(self.fileName, squeeze_me=True) d = {} d['RunID'] = [os.path.splitext(str(r))[0] for r in mat['matFiles']] d['ActualSpeed'] = mat['speeds'] d['Duration'] = mat['durations'] for fit in mat['fits']: for i, state in enumerate(self.states): try: d[state + 'Fit'].append(fit[i]) except KeyError: d[state + 'Fit'] = [fit[i]] d['MeanFit'] = np.mean(mat['fits'], 1) self.stateMatrices = mat['stateMatrices'] for A in mat['stateMatrices']: for i in range(2, 4): for j in range(len(self.states)): col = 'a' + str(i + 1) + str(j + 1) try: d[col].append(A[i, j]) except KeyError: d[col] = [A[i, j]] self.inputMatrices = mat['inputMatrices'] for B in mat['inputMatrices']: for i in range(2, 4): for j in range(0, 2): col = 'b' + str(i + 1) + str(j + 1) try: d[col].append(B[i, j]) except KeyError: d[col] = [B[i, j]] dataset = bdp.DataSet(fileName=PATH_TO_DATABASE, pathToH5=PATH_TO_H5, pathToCorruption=PATH_TO_CORRUPT) dataset.open() table = dataset.database.root.runTable tableCols = ['Rider', 'Maneuver', 'Environment', 'Speed'] for col in tableCols: d[col] = [] for r in d['RunID']: i = get_row_num(r, table) for col in tableCols: d[col].append(table[i][col]) dataset.close() self.dataFrame = pandas.DataFrame(d) self.w = w self.load_bode_data() self.load_eig_data()
def plot_coefficients(fitThreshold): meanFits = np.mean(a['fits'], 1) print(runNums[np.nanargmax(meanFits)]) indices = np.nonzero(meanFits > fitThreshold) stateMats = a['stateMatrices'][indices[0], :, :] inputMats = a['inputMatrices'][indices[0], :] speeds = a['speeds'][indices[0]] dataset = bdp.DataSet(fileName=PATH_TO_DATABASE, pathToH5=PATH_TO_H5, pathToCorruption=PATH_TO_CORRUPT) dataset.open() # find all the runs for Jason that were without disturbances table = dataset.database.root.runTable riders = [] maneuvers = [] for r in runNums[indices[0]]: i = get_row_num(r, table) riders.append(table[i]['Rider']) maneuvers.append(table[i]['Maneuver']) riders = np.array(riders) dataset.close() modelSpeeds = np.linspace(0., 10., num=50) modelStateMats = {} modelInputMats = {} for rider in set(riders): modelStateMats[rider] = np.zeros((len(modelSpeeds), 4, 4)) modelInputMats[rider] = np.zeros((len(modelSpeeds), 4)) # create a Bicycle object for the rider/bicycle if rider == 'Jason': bicycle = 'Rigid' else: bicycle = 'Rigidcl' bicycle = bp.Bicycle(bicycle, pathToData=PATH_TO_PARAMETERS, forceRawCalc=True) bicycle.add_rider(rider) # compute the A and B matrices as a function of speed for the Whipple # model for i, v in enumerate(modelSpeeds): A, B = bicycle.state_space(v, nominal=True) modelStateMats[rider][i, :, :] = A # the second column is for steer torque modelInputMats[rider][i, :] = B[:, 1] fig = plt.figure() equations = [r'\dot{\phi}', r'\dot{\delta}', r'\ddot{\phi}', r'\ddot{\delta}'] states = [r'\phi', r'\delta', r'\dot{\phi}', r'\dot{\delta}'] aXlims = np.array([[0., 20.], [-80., 0.], [-1.5, 4.], [-8., 0.], [-0.2, 1.2], [0., 200.], [-175., 40.], [-40., 60.], [-100., 0.], [0., 20.]]) onlyRiders = set(riders) for i in range(2, 4): for j in range(4): # make a row for for the roll acceleration and steer acceleration # equations and a colum for the four states and one for the input ax = fig.add_subplot(2, 5, 5 * (i - 2) + j + 1) ax.set_title('$a_{' + equations[i] + states[j] + '}$') for rider in onlyRiders: modelLine = ax.plot(modelSpeeds, modelStateMats[rider][:, i, j]) riderIndices = np.nonzero(riders == rider) expLine = ax.plot(speeds[riderIndices[0]], stateMats[riderIndices[0], i, j], '.') expLine[0].set_color(modelLine[0].get_color()) print(rider, modelLine[0].get_color()) ax.set_ylim(aXlims[5 * (i - 2) + j]) for i, p in zip(range(2, 4), [5, 10]): ax = fig.add_subplot(2, 5, p) ax.set_title('$b_{' + equations[i] + r'T_\delta' + '}$') for rider in onlyRiders: riderIndices = np.nonzero(riders == rider) modelLine = ax.plot(modelSpeeds, modelInputMats[rider][:, i]) expLine = ax.plot(speeds[riderIndices[0]], inputMats[riderIndices[0], i], '.') expLine[0].set_color(modelLine[0].get_color()) ax.set_ylim(aXlims[p - 1]) fig.suptitle('n = {}, Mean fit threshold: {}%'.format(len(speeds), fitThreshold)) return fig