#!/usr/bin/env python # coding: utf-8 # # Read Cortex Motion Analysis Corporation .trc and .forces files (example) # # > [Laboratory of Biomechanics and Motor Control](http://pesquisa.ufabc.edu.br/bmclab) # > Federal University of ABC, Brazil # In[1]: import numpy as np import pandas as pd get_ipython().run_line_magic('matplotlib', 'inline') import matplotlib as mpl import matplotlib.pyplot as plt import seaborn as sns sns.set_style('whitegrid') sns.set_context('notebook', font_scale=1.2, rc={"lines.linewidth": 2}) from scipy import signal import sys, os import pyversions # https://pypi.org/project/pyversions/ sys.path.insert(1, r'./../functions') import io_cortexmac as io # from https://github.com/BMClab/BMC/tree/master/functions # In[2]: pyversions.versions(); # ## Use function `io_cortexmac.py` from BMClab's repo # In[3]: path2 = r'/mnt/B/Dropbox/BMClab/stuff/Biomecanica/2020/' # In[4]: fname = os.path.join(path2, 'walk_diurno.trc') h_trc, trc = io.read_trc(fname, fname2='', units='m', dropna=False, na=0.0, df_multi=False) trc.set_index('Time', drop=True, inplace=True) trc.drop('Frame#', axis=1, inplace=True) # In[5]: fname = os.path.join(path2, 'walk_diurno.forces') h_grf, grf = io.read_forces(fname, time=True, forcepla=[], mm2m=True, show_msg=True) # In[6]: trc # In[7]: grf # ### trc and forces data have different sampling rates # In[8]: freq_trc = 1/np.mean(np.diff(trc.index)) freq_trc # In[9]: freq_grf = 1/np.mean(np.diff(grf.index)) freq_grf # #### Resample trc to the force sampling rate (150 Hz to 450 Hz) # In[10]: # allocate variable nrows = int(trc.shape[0]*np.round(freq_grf/freq_trc)) ncols = trc.shape[1] data = np.nan*np.zeros((nrows, 1+ncols), dtype='float64') # time column data[:, 0] = np.linspace(start=0, stop=nrows/freq_grf, num=nrows, endpoint=False) # resample data for i in range(ncols): data[:, i+1] = signal.resample_poly(trc.iloc[:, i], np.round(freq_grf), np.round(freq_trc), window='blackman') # create datafrane with new data trc = pd.DataFrame(data=data[:, 1:], index=data[:, 0], columns=trc.columns) trc.index.name = trc.index.name # In[11]: trc # ## Plot of some data # In[12]: trc.columns # In[13]: grf.columns # In[14]: fig, axs = plt.subplots(2, 1, sharex = True, squeeze=True, figsize=(10, 5)) trc.plot(y=['R.Heely', 'L.Heely'], ax=axs[0], title='Data diurno') grf.plot(y=['FY5', 'FY6'], ax=axs[1], colormap='viridis') axs[0].set_ylabel('Position [m]') axs[1].set_ylabel('Force [N]') plt.show() # This means that the subject stepped on force plate **#6** with her/his **left** foot and then stepped on force plate **#5** with her/his **right** foot. # ## For data noturno # In[15]: fname = os.path.join(path2, 'walk_noturno.trc') h_trc, trc = io.read_trc(fname, fname2='', units='m', dropna=False, na=0.0, df_multi=False) trc.set_index('Time', drop=True, inplace=True) trc.drop('Frame#', axis=1, inplace=True) fname = os.path.join(path2, 'walk_noturno.forces') h_grf, grf = io.read_forces(fname, time=True, forcepla=[], mm2m=True, show_msg=True) freq_trc = 1/np.mean(np.diff(trc.index)) freq_grf = 1/np.mean(np.diff(grf.index)) # allocate variable nrows = int(trc.shape[0]*np.round(freq_grf/freq_trc)) ncols = trc.shape[1] data = np.nan*np.zeros((nrows, 1+ncols), dtype='float64') # time column data[:, 0] = np.linspace(start=0, stop=nrows/freq_grf, num=nrows, endpoint=False) # resample data for i in range(ncols): data[:, i+1] = signal.resample_poly(trc.iloc[:, i], np.round(freq_grf), np.round(freq_trc), window='blackman') # create datafrane with new data trc = pd.DataFrame(data=data[:, 1:], index=data[:, 0], columns=trc.columns) trc.index.name = trc.index.name fig, axs = plt.subplots(2, 1, sharex = True, squeeze=True, figsize=(10, 5)) trc.plot(y=['R.Heely', 'L.Heely'], ax=axs[0], title='Data noturno') grf.plot(y=['FY5', 'FY6'], ax=axs[1], colormap='viridis') axs[0].set_ylabel('Position [m]') axs[1].set_ylabel('Force [N]') plt.show() # This means that the subject stepped on force plate **#6** with her/his **right** foot and then stepped on force plate **#5** with her/his **left** foot.