!pip freeze
Cython==0.19.1 -e git+git@github.com:moorepants/DynamicistToolKit.git@83822a2634756ad8a0a1701a0cb3cca3c07877d4#egg=DynamicistToolKit-dev Jinja2==2.7.1 MarkupSafe==0.18 argparse==1.2.1 coverage==3.6 ipython==1.0.0 matplotlib==1.3.0 nose==1.3.0 numpy==1.7.1 pandas==0.12.0 pyparsing==2.0.1 python-dateutil==2.1 pytz==2013b pyzmq==13.1.0 scipy==0.12.0 six==1.3.0 sympy==0.7.3 tornado==3.1 wsgiref==0.1.2
%pylab inline
Populating the interactive namespace from numpy and matplotlib
import pandas
from dtk import walk, process
obinna = pandas.read_csv('../data/obinna-walking.txt', delimiter='\t', index_col="TimeStamp", na_values='0.000000')
obinna.head()
<class 'pandas.core.frame.DataFrame'> Index: 5 entries, 534.133513 to 534.173667 Columns: 617 entries, FrameNumber to HBM.COM.Z dtypes: float64(616), int64(1)
[col for col in obinna.columns if col.endswith('.Ang') or col.endswith('ForY') or col.endswith('.Mom')]
['FP1.ForY', 'FP2.ForY', 'PelvisX.Ang', 'PelvisX.Mom', 'PelvisY.Ang', 'PelvisY.Mom', 'PelvisZ.Ang', 'PelvisZ.Mom', 'PelvisYaw.Ang', 'PelvisYaw.Mom', 'PelvisForwardPitch.Ang', 'PelvisForwardPitch.Mom', 'PelvisRightRoll.Ang', 'PelvisRightRoll.Mom', 'TrunkFlexion.Ang', 'TrunkFlexion.Mom', 'TrunkRightBend.Ang', 'TrunkRightBend.Mom', 'TrunkLeftTwist.Ang', 'TrunkLeftTwist.Mom', 'HeadFlexion.Ang', 'HeadFlexion.Mom', 'HeadRightBend.Ang', 'HeadRightBend.Mom', 'HeadLeftTwist.Ang', 'HeadLeftTwist.Mom', 'RShoulderUp.Ang', 'RShoulderUp.Mom', 'LShoulderUp.Ang', 'LShoulderUp.Mom', 'RShoulderForward.Ang', 'RShoulderForward.Mom', 'LShoulderForward.Ang', 'LShoulderForward.Mom', 'RShoulderInward.Ang', 'RShoulderInward.Mom', 'LShoulderInward.Ang', 'LShoulderInward.Mom', 'RShoulderFlexion.Ang', 'RShoulderFlexion.Mom', 'LShoulderFlexion.Ang', 'LShoulderFlexion.Mom', 'RShoulderAbduction.Ang', 'RShoulderAbduction.Mom', 'LShoulderAbduction.Ang', 'LShoulderAbduction.Mom', 'RShoulderInternalRotation.Ang', 'RShoulderInternalRotation.Mom', 'LShoulderInternalRotation.Ang', 'LShoulderInternalRotation.Mom', 'RElbowFlexion.Ang', 'RElbowFlexion.Mom', 'LElbowFlexion.Ang', 'LElbowFlexion.Mom', 'RForeArmPronation.Ang', 'RForeArmPronation.Mom', 'LForeArmPronation.Ang', 'LForeArmPronation.Mom', 'RWristFlexion.Ang', 'RWristFlexion.Mom', 'LWristFlexion.Ang', 'LWristFlexion.Mom', 'RHandAbduction.Ang', 'RHandAbduction.Mom', 'LHandAbduction.Ang', 'LHandAbduction.Mom', 'RHipFlexion.Ang', 'RHipFlexion.Mom', 'LHipFlexion.Ang', 'LHipFlexion.Mom', 'RHipAbduction.Ang', 'RHipAbduction.Mom', 'LHipAbduction.Ang', 'LHipAbduction.Mom', 'RHipInternalRotation.Ang', 'RHipInternalRotation.Mom', 'LHipInternalRotation.Ang', 'LHipInternalRotation.Mom', 'RKneeFlexion.Ang', 'RKneeFlexion.Mom', 'LKneeFlexion.Ang', 'LKneeFlexion.Mom', 'RAnklePlantarFlexion.Ang', 'RAnklePlantarFlexion.Mom', 'LAnklePlantarFlexion.Ang', 'LAnklePlantarFlexion.Mom', 'RFootPronation.Ang', 'RFootPronation.Mom', 'LFootPronation.Ang', 'LFootPronation.Mom', 'RToeFlexion.Ang', 'RToeFlexion.Mom', 'LToeFlexion.Ang', 'LToeFlexion.Mom']
obinna['FP2.ForY'].plot(figsize=(18,10))
<matplotlib.axes.AxesSubplot at 0x3acfc10>
start = 500
stop = 3000
right_strikes, left_strikes, right_toe_off, left_toe_off = \
walk.gait_landmarks_from_grf(obinna.index.values.astype(float)[start:stop],
obinna['FP1.ForY'][start:stop],
obinna['FP2.ForY'][start:stop],
do_plot=True, min_time=15.0, threshold=28.0)
obinna['FP2.ForY'].to_csv('../data/example_vertical_grf.csv')
first_foot_down = obinna[right_strikes[0]:right_toe_off[1]]
second_foot_down = obinna[right_strikes[1]:right_toe_off[2]]
first_foot_down['FP1.ForY'].shape
(76,)
second_foot_down['FP1.ForY'].shape
(77,)
obinna.replace?
figure(figsize=(10, 8))
right_steps = {}
for i, heel in enumerate(right_strikes[:-1]):
right_steps[i] = obinna[heel:right_strikes[i + 1]].apply(pandas.Series.interpolate)
print("Time samples in step: {}".format(len(right_steps[i])))
time = process.time_vector(len(right_steps[i]), 100)
right_steps[i].index = time
plot(time, right_steps[i]['FP1.ForY'], '.-')
right_steps = pandas.Panel(right_steps)
Time samples in step: 116 Time samples in step: 119 Time samples in step: 119 Time samples in step: 117 Time samples in step: 116 Time samples in step: 120 Time samples in step: 119 Time samples in step: 120 Time samples in step: 119 Time samples in step: 116 Time samples in step: 118 Time samples in step: 121 Time samples in step: 121 Time samples in step: 121 Time samples in step: 124 Time samples in step: 122 Time samples in step: 124 Time samples in step: 122 Time samples in step: 122 Time samples in step: 122
mean_right_steps = right_steps.mean(axis='items')
mean_right_steps['FP1.ForY'].plot(figsize=(10, 8))
<matplotlib.axes.AxesSubplot at 0x5a85f90>
right_steps = right_steps.fillna(method='pad')
sensors = ['RKneeFlexion.Ang', 'RAnklePlantarFlexion.Ang']
controls = ['RKneeFlexion.Mom', 'RHipFlexion.Mom', 'RAnklePlantarFlexion.Mom']
solver = walk.SimpleControlSolver(right_steps, sensors, controls)
gains, sensors = solver.solve()
gains.shape
(237, 3, 2)
figure(figsize=(10, 8))
plot(gains.reshape(gains.shape[0], gains.shape[1] * gains.shape[2]), '.-')
[<matplotlib.lines.Line2D at 0x55ab090>, <matplotlib.lines.Line2D at 0x55ab310>, <matplotlib.lines.Line2D at 0x55ab510>, <matplotlib.lines.Line2D at 0x55ab690>, <matplotlib.lines.Line2D at 0x55ab810>, <matplotlib.lines.Line2D at 0x55ab990>]
solver.sensors = ['RKneeFlexion.Ang']
gains, sensors = solver.solve()
figure(figsize=(10, 8))
plot(gains.reshape(gains.shape[0], gains.shape[1] * gains.shape[2]), '.-')
[<matplotlib.lines.Line2D at 0x5acc4d0>, <matplotlib.lines.Line2D at 0x5acc750>, <matplotlib.lines.Line2D at 0x5acc950>]
A, b = solver.form_a_b()
A.shape, b.shape
((14220, 1422), (14220,))
from scipy.sparse.linalg import lsmr, lsqr
%%time
sol1 = lsmr(A, b)
CPU times: user 57.4 s, sys: 36 ms, total: 57.4 s Wall time: 57.5 s
%%time
sol2 = lsqr(A, b)
CPU times: user 4min 49s, sys: 152 ms, total: 4min 49s Wall time: 4min 51s
np.allclose(sol1[0], sol2[0])
False
sol1[0]
array([ 1.7082228 , -0.09035771, -5.17921152, ..., 52.76824709, -2.36961725, 71.34576768])
sol2[0]
array([ 1.69779373, -0.09126658, -5.19502849, ..., 52.93082764, -2.37696915, 71.56437833])
%%time
sol3 = solver.least_squares(A, b)
CPU times: user 43.4 s, sys: 644 ms, total: 44 s Wall time: 31.3 s
sol3[0]
array([ 1.69744642, -0.09129685, -5.19555523, ..., 52.94942455, -2.3778101 , 71.58938429])
A = np.array([[1, 2],[2, 4], [0, 0]])
b = np.array([1, 2, 3])
(array([ 0.2, 0.4]), array([], dtype=float64), 1, array([ 5.00000000e+00, 1.98602732e-16]))
%%timeit
np.linalg.lstsq(A, b)
10000 loops, best of 3: 172 µs per loop
%%timeit
lsqr(A,b)
1000 loops, best of 3: 550 µs per loop
%%timeit
lsmr(A, b)
1000 loops, best of 3: 570 µs per loop