def _smoothldljgyro(gyo, gys, dgys, d2gys, fs): _sl = log_dimensionless_jerk(gyo, fs=fs, data_type="vel") # Cross product. _cp = np.array([np.cross(_dgy, _gy) for _dgy, _gy in zip(dgys, gys)]) # Jerk magnitude _gyjerk = np.sum(np.power(np.linalg.norm(d2gys - _cp, axis=1), 2)) * (1 / fs) _gymag = np.max(np.linalg.norm(gys, axis=1)) _dur = np.shape(gys)[0] * (1 / fs) _sls = -np.log(np.power(_dur, 3) * _gyjerk / np.power(_gymag, 2)) return _sl, _sls
def analyse_smoothness_from_different_signals(data, fs, out_dir, diff_smooth): _cols = ('Nvia', 'Nmove', 'sparc_v', 'sparc_a', 'sparc_j', 'ldlj_v', 'ldlj_a', 'ldlj_j') smoothness_summary = pd.DataFrame(columns=_cols) _outfile = "{0}/{1}/{2}".format(out_dir, diff_smooth['dir'], diff_smooth['fig_file']) with PdfPages(_outfile) as pdf: # Go through each data file and estimate smoothness using # both SPARC and LDLJ. for Nvia, Nmove, minfo, mdata in data: # Calculate speed, acceleration and jerk magnitude v = np.linalg.norm(np.array(mdata[['vx', 'vy', 'vz']]), axis=1)[:-1] a = np.linalg.norm(np.array(mdata[['ax', 'ay', 'az']]), axis=1)[:-2] j = np.linalg.norm(np.array(mdata[['jx', 'jy', 'jz']]), axis=1)[:-3] # Estimate smoothness _sparcv, v1, v2 = sparc(v, fs=fs, fc=20., amp_th=0.05) _sparca, a1, a2 = sparc(a, fs=fs, fc=20., amp_th=0.05) _sparcj, j1, j2 = sparc(j, fs=fs, fc=20., amp_th=0.05) _ldljv = log_dimensionless_jerk(np.array(mdata[['vx', 'vy', 'vz']]), fs=fs, data_type="vel", scale="ms") _ldlja = log_dimensionless_jerk(np.array(mdata[['ax', 'ay', 'az']]), fs=fs, data_type="accl", scale="ms") _ldljj = log_dimensionless_jerk(np.array(mdata[['jx', 'jy', 'jz']]), fs=fs, data_type="jerk", scale="ms") # Update data row _datarow = { 'Nvia': [int(Nvia)], 'Nmove': [int(Nmove)], 'sparc_v': [_sparcv], 'sparc_a': [_sparca], 'sparc_j': [_sparcj], 'ldlj_v': [_ldljv], 'ldlj_a': [_ldlja], 'ldlj_j': [_ldljj] } # Update smoothness summary DF smoothness_summary = pd.concat( [smoothness_summary, pd.DataFrame.from_dict(_datarow)], ignore_index=True) # plot data generate_summary_plot_for_movement(pdf, mdata, minfo, Nvia, Nmove, fs, v, v1, v2, a, a1, a2, j, j1, j2, _sparcv, _sparca, _sparcj, _ldljv, _ldlja, _ldljj) sys.stdout.write("\r{0}".format(minfo['file'])) sys.stdout.flush() # Update PDF file details d = pdf.infodict() d['Title'] = 'Smoothness estimates for different signals' d['Author'] = u'Sivakumar Balasubramanian' d['Subject'] = 'Smoothness Analysis' d['Keywords'] = 'Smoothness Analysis' d['CreationDate'] = datetime.datetime(2017, 12, 16) d['ModDate'] = datetime.datetime.today() # Save summary data _dfile = "{0}/{1}/{2}".format(out_dir, diff_smooth['dir'], diff_smooth['data_file']) smoothness_summary.to_csv(path_or_buf=_dfile, index=False) sys.stdout.write("\rDone!")
def analyse_smoothness(params): # Initialize smoothness values variable. smoothvals = { 'sparc': np.zeros((params.Ndur, params.Ntgt)), 'ldljv': np.zeros((params.Ndur, params.Ntgt)), 'ldlja': np.zeros((params.Ndur, params.Ntgt)), 'sparcs': np.zeros((params.Ndur, params.Ntgt)), 'ldljsv': np.zeros((params.Ndur, params.Ntgt)), 'ldljsa': np.zeros((params.Ndur, params.Ntgt)), 'sparcs-wom': np.zeros((params.Ndur, params.Ntgt)), 'ldljsv-wom': np.zeros((params.Ndur, params.Ntgt)), 'ldljsa-wom': np.zeros((params.Ndur, params.Ntgt)), 'agr': np.zeros((params.Ndur, params.Ntgt)), } files = glob.glob("{0}/data_*.csv".format(params.outdir)) for i, f in enumerate(files): dinx, tinx = get_dur_tgt_index_from_fname(f) # Read accl, vel and speed data mdata = read_get_vel_accl_data(f, params) # Remove mean for each accelerometer sensor data mdata['accls-wom'] = mdata['accls'] - np.mean(mdata['accls'], axis=0) mdata['vels-wom'] = np.cumsum(mdata['accls-wom'], axis=0) * params.dt mdata['spds-wom'] = np.linalg.norm(mdata['vels-wom'], axis=1) # Estimate AGR. _g = (np.sqrt(len(mdata['accl'].T)) * 98.1) agr = 20 * np.log10(np.linalg.norm(mdata['accl']) / _g) # Calculate smoothness - SPARC _sparc, _, _ = sparc(mdata['spd'], fs=params.fs) _sparcs, _, _ = sparc(mdata['spds'], fs=params.fs) _sparcswom, _, _ = sparc(mdata['spds-wom'], fs=params.fs) # Calculate smoothness - LDLJ # From velocity _ldljv = log_dimensionless_jerk(mdata['vel'], fs=params.fs, data_type="vel") _ldljsv = log_dimensionless_jerk(mdata['vels'], fs=params.fs, data_type="vel") _ldljsvwom = log_dimensionless_jerk(mdata['vels-wom'], fs=params.fs, data_type="vel") # From velocity _ldlja = log_dimensionless_jerk(mdata['accl'], fs=params.fs, data_type="accl") _ldljsa = log_dimensionless_jerk(mdata['accls'], fs=params.fs, data_type="accl") _ldljsawom = log_dimensionless_jerk(mdata['accls-wom'], fs=params.fs, data_type="accl") # Save data. smoothvals['sparc'][dinx, tinx] = _sparc smoothvals['sparcs'][dinx, tinx] = _sparcs smoothvals['sparcs-wom'][dinx, tinx] = _sparcswom smoothvals['ldljv'][dinx, tinx] = _ldljv smoothvals['ldljsv'][dinx, tinx] = _ldljsv smoothvals['ldljsv-wom'][dinx, tinx] = _ldljsvwom smoothvals['ldlja'][dinx, tinx] = _ldlja smoothvals['ldljsa'][dinx, tinx] = _ldljsa smoothvals['ldljsa-wom'][dinx, tinx] = _ldljsawom smoothvals['agr'][dinx, tinx] = agr sys.stdout.write("\r {0:3d}/{1:3d} | {2}".format(i, len(files), f)) sys.stdout.write("\nDone!") return smoothvals
def _smoothldlja(ac, acs, acwom, acswom, fs): _sl = log_dimensionless_jerk(ac, fs=fs, data_type="accl") _sls = log_dimensionless_jerk(acs, fs=fs, data_type="accl") _slwom = log_dimensionless_jerk(acwom, fs=fs, data_type="accl") _slswom = log_dimensionless_jerk(acswom, fs=fs, data_type="accl") return _sl, _sls, _slwom, _slswom
def _smoothldljv(v, vs, vwom, vswom, fs): _sl = log_dimensionless_jerk(v, fs=fs, data_type="vel") _sls = log_dimensionless_jerk(vs, fs=fs, data_type="vel") _slwom = log_dimensionless_jerk(vwom, fs=fs, data_type="vel") _slswom = log_dimensionless_jerk(vswom, fs=fs, data_type="vel") return _sl, _sls, _slwom, _slswom
def gen_sensordata_ldljmsoothness(fdetails, logdetails, g, dsratio): results = pd.DataFrame(columns=[ 'move', 'wg', 'ag', 'sgr', 'arms', 'wrms', 'loa', 'lsa', 'lsaw', 'To', 'Tsa', 'Tsaw', 'aoms', 'asms', 'aswms', 'jo', 'jsa', 'jsaw' ]) for _fr in fdetails.iterrows(): sys.stdout.write("\r N: {0:04d} {1}".format(_fr[1]['N'], _fr[1]['fname'])) # Read file data. origdata = pd.read_csv( _fr[1]["fname"], sep=", ", header=0, index_col=None).rename(columns={"# time": "time"}) # Time and global kinematics t = np.array(origdata['time']) aco = np.array(origdata[['aox', 'aoy', 'aoz']]).T jo = np.array(origdata[['jox', 'joy', 'joz']]).T wo = np.array(origdata[['wox', 'woy', 'woz']]).T # Get sensor data ts, acs, ws = get_sensor_data(t, aco, wo, jo, g, dsratio) # True LDLJ smoothness fs = 1 / (t[1] - t[0]) loa = pysm.log_dimensionless_jerk(aco.T[::dsratio, :], fs / dsratio, data_type='accl', scale='ms') loafac = pysm.log_dimensionless_jerk_factors(aco.T[::dsratio, :], fs / dsratio, data_type='accl', scale='ms') # Sensor LDLJ smoothness from just accelerometer fss = 1 / (ts[1] - ts[0]) lsa = pysm.log_dimensionless_jerk_imu(acs.T, None, g, fss) lsafac = pysm.log_dimensionless_jerk_imu_factors(acs.T, None, g, fss) # Sensor LDLJ smoothness from just accelerometer and gyroscope lsaw = pysm.log_dimensionless_jerk_imu(acs.T, ws.T, g, fss) lsawfac = pysm.log_dimensionless_jerk_imu_factors(acs.T, ws.T, g, fss) # Signal measures _N = np.shape(acs)[1] _sgr = np.linalg.norm(acs) / (np.sqrt(_N) * np.linalg.norm(g)) _N = np.shape(aco)[1] _arms = np.linalg.norm(aco) / np.sqrt(_N) _N = np.shape(wo)[1] _wrms = np.linalg.norm(wo) / np.sqrt(_N) # Update data. _df = pd.DataFrame.from_dict({ 'move': [_fr[1]['N']], 'wg': [_fr[1]['wg']], 'ag': [_fr[1]['ag']], 'sgr': [_sgr], 'arms': [_arms], 'wrms': [_wrms], 'loa': [loa], 'lsa': [lsa], 'lsaw': [lsaw], 'To': [loafac[0]], 'Tsa': [lsafac[0]], 'Tsaw': [lsawfac[0]], 'aoms': [loafac[1]], 'asms': [lsafac[1]], 'aswms': [lsawfac[1]], 'jo': [loafac[2]], 'jsa': [lsafac[2]], 'jsaw': [lsawfac[2]] }) results = results.append(_df, ignore_index=True, sort=False) return results
def generate_imuldlj_summary(t, T, g, wi, agvals, wgvals, dsratio, logdetails, results): # Generate simulated data proc = multiprocessing.Pool(1) # Run the simulation for 1000 different movements. for i, _vals in enumerate(np.array([agvals, wgvals]).T): ag, wg = _vals sys.stdout.write("\r{2}:: SGR: {0} | Wg: {1}".format(ag, wg, i)) # params params = [t, T, g, wi, ag, wg, dsratio] # rawdata = proc.map(gen_original_and_sensor_data, [params])[0] rawdata = gen_original_and_sensor_data(params) # Save data. save_rawdata(ag, wg, i, logdetails, t, rawdata) # True LDLJ smoothness fs = 1 / (t[1] - t[0]) acclo, wo = rawdata[0].T, rawdata[2].T loa = pysm.log_dimensionless_jerk(acclo, fs, data_type='accl', scale='ms') loafac = pysm.log_dimensionless_jerk_factors(acclo, fs, data_type='accl', scale='ms') ts, accls, ws = rawdata[3], rawdata[4].T, rawdata[5].T fs = 1 / (ts[1] - ts[0]) # Sensor LDLJ smoothness from just accelerometer lsa = pysm.log_dimensionless_jerk_imu(accls, None, g, fs) lsafac = pysm.log_dimensionless_jerk_imu_factors(accls, None, g, fs) # Sensor LDLJ smoothness from just accelerometer and gyroscope lsaw = pysm.log_dimensionless_jerk_imu(accls, ws, g, fs) lsawfac = pysm.log_dimensionless_jerk_imu_factors(accls, ws, g, fs) # True gyroscope smoothness # Sensor smoothness from gyroscope # Update data. _N = np.shape(wo)[0] _wg = np.linalg.norm(wo) / np.sqrt(_N) _N = np.shape(accls)[0] _sgr = np.linalg.norm(accls) / (np.sqrt(_N) * np.linalg.norm(g)) _df = pd.DataFrame.from_dict({ 'move': [i], 'loa': [loa], 'lsa': [lsa], 'lsaw': [lsaw], 'To': [loafac[0]], 'Tsa': [lsafac[0]], 'Tsaw': [lsawfac[0]], 'aoms': [loafac[1]], 'asms': [lsafac[1]], 'aswms': [lsawfac[1]], 'jo': [loafac[2]], 'jsa': [lsafac[2]], 'jsaw': [lsawfac[2]], 'wg': [wg], 'ag': [ag], '_wg': [_wg], 'sgr': [_sgr] }) results = results.append(_df, ignore_index=True, sort=True) return results
def get_smooth_vals(params): cols = [ "via", "dur", "gynorm", "viarep", "gynormrep", "wg", "sgr", "lao", "las", "laws", "laserr", "lawserr", "lgyo", "lgys", "lgyserr" ] # Indices to be used for accessing data _accinx = ['ax', 'ay', 'az'] _accsinx = ['axs', 'ays', 'azs'] _gyinx = ['gyx', 'gyy', 'gyz'] _gysinx = ['gyxs', 'gyys', 'gyzs'] _dgysinx = ['dgyxs', 'dgyys', 'dgyzs'] _d2gysinx = ['d2gyxs', 'd2gyys', 'd2gyzs'] smoothvals = pd.DataFrame(columns=cols) indices = get_all_indices(params) cnt = 0 # define progress bar widgets = ['[', pb.Timer(), '] ', pb.Bar(), ' (', pb.ETA(), ')'] bar = pb.ProgressBar(widgets=widgets, maxval=params.Ntotal) for inx in indices: _f = Params.fnamestr.format(*inx) if os.path.isfile(_f) is True: # read reconstrucuted data data = pd.read_csv(filepath_or_buffer=_f, delimiter=',') data = data.rename(columns=lambda x: x.strip()) _N = len(data) else: continue # Sensor to gravity ratio _g = np.sqrt(_N) * np.linalg.norm(params.grav) _s = np.linalg.norm(np.array(data[_accsinx])) _sgr = _s / _g # w_rms _wg = np.linalg.norm(data[_gyinx]) / np.sqrt(_N) # Sampling frequency fs = Params.fs / params.Dur[inx[1]] # LDLJ of global linear acceleration and angular rotation lao = pysm.log_dimensionless_jerk(np.array(data[_accinx]), fs, data_type='accl', scale='ms') lgyo = pysm.log_dimensionless_jerk(np.array(data[_gyinx]), fs, data_type='vel', scale='max') # LDLJ of from IMU data las = pysm.log_dimensionless_jerk_imu(np.array(data[_accsinx]), None, params.grav, fs) laws = pysm.log_dimensionless_jerk_imu(np.array(data[_accsinx]), np.array(data[_gysinx]), params.grav, fs) laserr = 100 * (las - lao) / np.abs(lao) lawserr = 100 * (laws - lao) / np.abs(lao) # LDLJ of gyroscope data lgys = _smoothldljgyro(np.array(data[_gysinx]), np.array(data[_dgysinx]), np.array(data[_d2gysinx]), fs) lgyserr = 100 * (lgys - lgyo) / np.abs(lgyo) # Append to dataframe _data = { "via": [params.nVia[inx[0]]], "dur": params.Dur[inx[1]], "gynorm": [params.gyNorm[inx[2]]], "viarep": [inx[3]], "gynormrep": [inx[4]], "wg": [_wg], "sgr": [_sgr], "lao": [lao], "las": [las], "laws": [laws], "laserr": [laserr], "lawserr": [lawserr], "lgyo": [lgyo], "lgys": [lgys], "lgyserr": [lgyserr] } smoothvals = smoothvals.append(pd.DataFrame.from_dict(_data), ignore_index=True) cnt += 1 bar.update(cnt) return smoothvals