<jupyter_start><jupyter_text>Parameter extraction  Stride length<jupyter_code>import peakutils
from scipy.signal import argrelmax

xmin = 0
xmax = 10
bins = 200
width = (xmax-xmin)/bins

stride_all = []

for file_id in range(len(disp_abs_all)):
    stride_seg = []
    for seg in range(len(disp_abs_all[0])):
        stride_seg0 = []
        hist_dat = np.histogram(disp_abs_all[file_id][seg,:,1], bins=bins, range=(xmin,xmax))
        #peaks = hist_dat[1][argrelmax(hist_dat[0], order=4)]
        peaks_id = peakutils.indexes(hist_dat[0], thres=0.2, min_dist=5)
        peaks_id = np.sort(peaks_id)
        peaks = hist_dat[1][peaks_id]
        for peak_id in range(len(peaks)):
            dat0 = disp_abs_all[file_id][seg]
            disp_peak = [dat0[i,1] for i in range(len(dat0)) 
                         if dat0[i,1] > peaks[peak_id] and dat0[i,1] < peaks[peak_id] + width]
            time_peak = [dat0[i,0] for i in range(len(dat0)) 
                         if dat0[i,1] > peaks[peak_id] and dat0[i,1] < peaks[peak_id] + width]
            disp_peak_med = np.median(disp_peak)
            time_peak_med = np.median(time_peak)
            stride_seg0.append([time_peak_med, disp_peak_med])
        stride_seg.append(np.array(stride_seg0))

    stride_all.append(stride_seg)

    plt.figure()
    for seg in range(11):
        plt.plot(disp_abs_all[file_id][seg,:,0], disp_abs_all[file_id][seg,:,1])
        plt.plot(stride_all[file_id][seg][:,0], stride_all[file_id][seg][:,1], 'o')

    plt.title("Displacement of file {0}".format(src_name_list[file_id]))
    plt.xlabel("Time (sec)")
    plt.ylabel("Displacement (mm)")
    plt.xlim([0,5])
    plt.ylim([0,10])
    plt.xticks([0,1,2,3,4,5])
    plt.show()
import numpy as np

stride_length_all = []
for file_id in range(len(stride_all)):
    dst1 = []
    for bd in range(len(stride_all[file_id])):
        dat_stride = stride_all[file_id][bd]
        dst0 = []
        for i in range(len(dat_stride)-1):
            dst0.append(dat_stride[i+1,1]-dat_stride[i,1])
        dst1.append(np.median(dst0))
    stride_length_all.append(dst1)

stride_length_all_median = []
for file_id in range(len(stride_length_all)):
    stride_length_all_median.append(np.median(stride_length_all[file_id]))

print("Stride length:")
for file_id in range(len(stride_length_all)):
    print("{0}:  {1:.3f} mm".format(src_name_list[file_id], stride_length_all_median[file_id]))<jupyter_output>Stride length:
EI_Response-w_ES--0.5-theta_S-0.8.mat:  0.964 mm<jupyter_text>Stride duration (and intersegmental delay)<jupyter_code>import numpy as np
import peakutils

sec_per_frame = 0.01

# boundary stride duration
boundary_stride_duration_all = []

for file_id in range(len(disp_abs_all)):
    dst0 = []
    for bd in range(len(disp_abs_all[file_id])):

        sig0 = np.diff(disp_abs_all[file_id][bd][:,1])
        sig1 = np.diff(disp_abs_all[file_id][bd][:,1])

        # centralization
        sig0 = sig0 - sig0.mean()
        sig1 = sig1 - sig1.mean()
        corr = np.correlate(sig1, sig0, "full")
        peaks_id = peakutils.indexes(corr[len(corr)-len(sig0):], thres=0.2, min_dist=20)
        estimated_delay = peaks_id[0] * sec_per_frame
        dst0.append(estimated_delay)
        
        #fig, ax = plt.subplots(2,1, figsize = (10,8))
        #ax[0].plot(sig0, label="sig0")
        #ax[0].plot(sig1, label="sig1")
        #ax[0].legend()
        #ax[1].set_ylabel("corr")
        #ax[1].plot(np.arange(len(corr))-len(sig0)+1, corr)
        #ax[1].plot(peaks_id, corr[peaks_id+len(sig0)-1], 'ro')
        #ax[1].set_xlim([0, len(sig1)])
        #plt.show()
        #plt.savefig(fig_path + "201126_boundary_auto_corr_{0}_seg{1}.png".format(src_name[file_id], seg_id))
        #plt.close()
    boundary_stride_duration_all.append(dst0)

# intersegmental delay
boundary_motion_delay_all = []

for file_id in range(len(disp_abs_all)):
    dst0 = []
    for seg in range(len(disp_abs_all[file_id])-1):

        sig0 = np.diff(disp_abs_all[file_id][seg][:,1])
        sig1 = np.diff(disp_abs_all[file_id][seg+1][:,1])

        # centralization
        sig0 = sig0 - sig0.mean()
        sig1 = sig1 - sig1.mean()
        corr = np.correlate(sig1, sig0, "full")
        t_margin = 2
        peaks_id = peakutils.indexes(corr[len(corr)-len(sig0)-t_margin:], thres=0.2, min_dist=20)
        peaks_id = peaks_id - t_margin
        estimated_delay = peaks_id[0] * sec_per_frame
        dst0.append(estimated_delay)
        
        #fig, ax = plt.subplots(2,1, figsize = (10,8))
        #ax[0].plot(sig0, label="sig0")
        #ax[0].plot(sig1, label="sig1")
        #ax[0].legend()
        #ax[1].set_ylabel("corr")
        #ax[1].plot(np.arange(len(corr))-len(sig0)+1, corr)
        #ax[1].plot(peaks_id, corr[peaks_id+len(sig0)-1], 'ro')
        #ax[1].set_xlim([0, len(sig1)])
        #plt.show()
        #plt.savefig(fig_path + "201126_boundary_motion_interseg_corr_{0}_seg{1}.png".format(src_name[file_id], seg_id))
        #plt.close()
    boundary_motion_delay_all.append(dst0)
print("Stride duration:")
for file_id in range(len(boundary_stride_duration_all)):
    print("{0}:  {1:.3f} sec".format(src_name_list[file_id], np.median(boundary_stride_duration_all[file_id]))) 
print("Intersegmental delay:")
for file_id in range(len(boundary_motion_delay_all)):
    print("{0}:  {1:.3f} sec".format(src_name_list[file_id], np.median(boundary_motion_delay_all[file_id])))<jupyter_output>Stride duration:
EI_Response-w_ES--0.5-theta_S-0.8.mat:  1.190 sec
Intersegmental delay:
EI_Response-w_ES--0.5-theta_S-0.8.mat:  0.110 sec<jupyter_text>Speed<jupyter_code>import matplotlib.pyplot as plt
import numpy as np
from sklearn.linear_model import LinearRegression

speed_all = []

for file_id in range(len(disp_abs_all)):
    dst = []
    for bd in range(len(disp_abs_all[file_id])):
        lr = LinearRegression()
        X = disp_abs_all[file_id][bd,:,0].reshape(-1,1)
        Y = disp_abs_all[file_id][bd,:,1].reshape(-1,1)
        lr.fit(X, Y)

        plt.plot(X, Y, color='green')
        plt.plot(X, lr.predict(X), color='magenta')
        plt.show()
        #plt.savefig(fig_path + "201126_11_points_speed_{0}_seg{1}.png".format(src_name[file_id], seg_id))
        #plt.close()
        dst.append(lr.coef_[0][0])
    speed_all.append(dst)
speed_all = np.array(speed_all)
print("Speed:")
for file_id in range(len(speed_all)):
    print("{0}:  {1:.3f} mm/sec".format(src_name_list[file_id], np.median(speed_all[file_id])))<jupyter_output>Speed:
EI_Response-w_ES--0.5-theta_S-0.8.mat:  0.815 mm/sec<jupyter_text>Max and min segment length<jupyter_code>import numpy as np

max_len_all = []
min_len_all = []

for file_id in range(len(seg_len_range_all)):
    dst_max = []
    dst_min = []
    for seg in range(len(seg_len_range_all[file_id])):
        dat = seg_len_range_all[file_id][seg]
        dst_max.append(dat[0][1].max())
        dst_min.append(dat[1][1].min())
    max_len_all.append(dst_max)
    min_len_all.append(dst_min)
max_len_all = np.array(max_len_all)
min_len_all = np.array(min_len_all)

print("Max length:")
for file_id in range(len(max_len_all)):
    print("{0}:  {1:.3f} mm".format(src_name_list[file_id], np.median(max_len_all[file_id])))

print("Min length:")
for file_id in range(len(min_len_all)):
    print("{0}:  {1:.3f} mm".format(src_name_list[file_id], np.median(min_len_all[file_id])))<jupyter_output>Max length:
EI_Response-w_ES--0.5-theta_S-0.8.mat:  0.489 mm
Min length:
EI_Response-w_ES--0.5-theta_S-0.8.mat:  -0.093 mm<jupyter_text>Contraction duration<jupyter_code>from scipy import signal

FWHM_segment_length_all = []
valley_id = 2   # measure the duration at the valley_id'th valley 

for file_id in range(len(seg_len_all)):
    dst = []
    for seg in range(len(seg_len_all[file_id])):
        dat_time = seg_len_all[file_id][seg][:,0]
        dat_length = seg_len_all[file_id][seg][:,1]
        valley_point = seg_len_range_all[file_id][seg][1][0][valley_id]
        idx = np.where(dat_time == valley_point)[0]
        thrd = (max_len_all[file_id][seg] - min_len_all[file_id][seg])*0.5 + min_len_all[file_id][seg]

        # search for left idx
        left_ = 0
        while(dat_length[idx-left_]<thrd):
            left_ += 1
        idx_left = idx - left_

        # search for right idx
        right_ = 0
        while(dat_length[idx+right_]<thrd):
            right_ += 1
        idx_right = idx + right_

        time_left = dat_time[idx_left]
        time_right = dat_time[idx_right]

        dst0 = [[time_left[0], time_right[0]], [int(idx_left[0]), int(idx_right[0])]]

        dst.append(dst0)
    FWHM_segment_length_all.append(dst)
FWHM_segment_length_all = np.array(FWHM_segment_length_all)

contraction_duration_all = []
for file_id in range(len(FWHM_segment_length_all)):
    dst = []
    for seg in range(len(FWHM_segment_length_all[0])):
        dat = FWHM_segment_length_all[file_id][seg]
        dst.append(dat[0,1] - dat[0,0])
    contraction_duration_all.append(dst)
contraction_duration_all = np.array(contraction_duration_all)

print("Contraction duration:")
for file_id in range(len(contraction_duration_all)):
    print("{0}:  {1:.3f} sec".format(src_name_list[file_id], np.median(contraction_duration_all[file_id])))<jupyter_output>Contraction duration:
EI_Response-w_ES--0.5-theta_S-0.8.mat:  0.160 sec<jupyter_text>Summary<jupyter_code>print("Stride length:")
for file_id in range(len(stride_length_all)):
    print("{0}:  {1:.3f} mm".format(src_name_list[file_id], stride_length_all_median[file_id]))
print("\n")

print("Stride duration:")
for file_id in range(len(boundary_stride_duration_all)):
    print("{0}:  {1:.3f} sec".format(src_name_list[file_id], np.median(boundary_stride_duration_all[file_id])))
print("\n")

print("Intersegmental delay:")
for file_id in range(len(boundary_motion_delay_all)):
    print("{0}:  {1:.3f} sec".format(src_name_list[file_id], np.median(boundary_motion_delay_all[file_id])))
print("\n")
    
print("Speed:")
for file_id in range(len(speed_all)):
    print("{0}:  {1:.3f} mm/sec".format(src_name_list[file_id], np.median(speed_all[file_id])))
print("\n")

print("Max length:")
for file_id in range(len(max_len_all)):
    print("{0}:  {1:.3f} mm".format(src_name_list[file_id], np.median(max_len_all[file_id])))
print("\n")

print("Min length:")
for file_id in range(len(min_len_all)):
    print("{0}:  {1:.3f} mm".format(src_name_list[file_id], np.median(min_len_all[file_id]))) 
print("\n")
    
print("Contraction duration:")
for file_id in range(len(contraction_duration_all)):
    print("{0}:  {1:.3f} sec".format(src_name_list[file_id], np.median(contraction_duration_all[file_id])))<jupyter_output>Stride length:
EI_Response-w_ES--0.5-theta_S-0.8.mat:  0.964 mm


Stride duration:
EI_Response-w_ES--0.5-theta_S-0.8.mat:  1.190 sec


Intersegmental delay:
EI_Response-w_ES--0.5-theta_S-0.8.mat:  0.110 sec


Speed:
EI_Response-w_ES--0.5-theta_S-0.8.mat:  0.815 mm/sec


Max length:
EI_Response-w_ES--0.5-theta_S-0.8.mat:  0.489 mm


Min length:
EI_Response-w_ES--0.5-theta_S-0.8.mat:  -0.093 mm


Contraction duration:
EI_Response-w_ES--0.5-theta_S-0.8.mat:  0.160 sec
{'fraction_non_alphanumeric': 0.15892552331326926, 'fraction_numerical': 0.033150660318030725, 'mean_word_length': 3.9365853658536585, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '300120', 'license': 'Unlicense', 'n_tokens_mistral': 4660, 'n_tokens_neox': 4465, 'n_words': 638, 'path': 'Analyze_matlab_simulation_results_201210.ipynb', 'repo_name': 'AlbertLordsun/Physical_measurement', 'size': '11131'}