#+TITLE: Automatic Extraction of Stride Cycles
#+AUTHOR: Falk Mielke
#+OPTIONS: toc:nil
# https://orgmode.org/worg/org-contrib/babel/languages/ob-doc-python.html
#+begin_export html
---
title: "Automatic Extraction of Stride Cycles"
description: "What to do with a ton of data?"
author: Falk Mielke
date: 2021-09-28
tags: ["signal processing", "kinematics", "videography", "tracking"]
mathjax: true
highlight:
enabled: true
theme: tomorrow
---
#+end_export
(You can download this notebook [[/falk/notebooks/27.cycle_extraction.org?download&target=_blank][here]].)
Although I had hoped to avoid it, I recently dug into the world of automated video digitization.
Namely, I trained a neural network with [[https://github.com/DeepLabCut/DeepLabCut?target=_blank][deeplabcut]] to give me lots of landmarks for my old piglet recordings.
Those are hours of video, training was astonishingly succesful.
One immediate problem of such an amount of video is that you need to separate the relevant from the trash.
(I intentionally formulated vaguely so that those of my readers with an affiliation to online movie streaming services will get hooked.)
And because deep learning is "hip", I anticipate that some futer colleagues might benefit from my attempts to solve the problem.
*The goal is to take traces of multiple (limb) landmarks and atuomatically identify stride cycles, without having to manually annotate the video.*
For those of you with less data (i.e. an amount that students could handle in reasonable time), I can highly recommend the [[https://archive.mpi.nl/tla/elan?target=_blank][ELAN video annotation software]] from colleagues at the MPI.
I've used it for a long time, it has little to learn (but: keyboard shortcuts!) and is flexible and versatile, plus it is open source.
But now for the automatic part.
#+TOC: headlines 3
Of course, I do this in my most familiar programming language, but you can easily port the procedure to others.
#+begin_src python :session :tangle yes
import numpy as NP # numeric operations
import pandas as PD # data storage and i/o
import scipy.signal as SIG # signal processing (mainly 1d)
import skimage as SKI # 2d signal processing
from skimage import feature as FEAT # "features" on 2d data
import matplotlib as MP # plotting
import matplotlib.pyplot as PLT
# useful shorthands
xy = ['x', 'y']
xyl = xy + ['likelihood']
dpi = 100
#+end_src
#+RESULTS:
I will also reactivate [[../18.procrustes][my old friend Procrustes]] in this post ([[/falk/code/SuperimpositionToolbox.py?download&target=_blank][toolbox for download]]).
#+begin_src python :session :tangle yes
import SuperimpositionToolbox as ST
#+end_src
#+RESULTS:
* Example Data
This is what deeplabcut spit out after days of preparation, weeks of training and seconds of labeling:
#+begin_export html
#+end_export
You see on this short example that most of the time, nothing spectacular happens.
Yet on a few occasions, the animal is moving, and there might be a full stride cycle.
This is what we are trying to extract.
Here is [[/falk/data/cycles_example_trace.csv?download&target=_blank][the data]]:
#+begin_src python :session :exports both :tangle yes :results output
framerate = 50 # frames per second
trace = PD.read_csv('cycles_example_trace.csv', sep = ';', header = [0,1]).set_index(('bodyparts','coords'))
trace.index = (trace.index.values - trace.index.values[0])/framerate
trace.index.name = 'time'
print(trace.T.iloc[:10, :6].round(3).to_markdown(index = True).replace(':|', '-+').replace('|:', '|-'))
#+end_src
#+RESULTS:
#+begin_example
| | 0.0 | 0.02 | 0.04 | 0.06 | 0.08 | 0.1 |
|-------------------------|---------+---------+---------+---------+---------+---------+
| ('snout', 'x') | 615 | 607.514 | 600.908 | 596.479 | 591.301 | 587.299 |
| ('snout', 'y') | 149.325 | 160.663 | 173.403 | 179.75 | 187.775 | 195.634 |
| ('snout', 'likelihood') | 1 | 0.999 | 1 | 1 | 1 | 1 |
| ('eye', 'x') | 584.4 | 578.644 | 573.638 | 568.485 | 562.76 | 557.214 |
| ('eye', 'y') | 111.137 | 121.162 | 130.867 | 139.213 | 145.05 | 153.298 |
| ('eye', 'likelihood') | 1 | 1 | 1 | 1 | 1 | 1 |
| ('ear', 'x') | 552.8 | 550.952 | 544.992 | 538.487 | 535.367 | 534.582 |
| ('ear', 'y') | 85.473 | 94.946 | 101.946 | 112.196 | 117.147 | 125.438 |
| ('ear', 'likelihood') | 0.997 | 0.97 | 0.998 | 1 | 1 | 0.738 |
| ('withers', 'x') | 366.482 | 362.518 | 350.087 | 570.371 | 345.195 | 331.925 |
#+end_example
Usually I keep time in the rows (zero'th axis in =numpy= and =pandas=), but transpose improves the display.
You can see that we have a time index, as well as (more than two) landmarks with \\(x,y\\)-coordinates and a /likelihood/.
I will explain the likelihood below.
* Shuffling Landmarks
Here is a complete list of landmarks:
#+begin_src python :session :exports both :tangle yes
landmarks = [ lm \
for lm in NP.unique(trace.columns.get_level_values(0)) \
]
",".join(landmarks)
#+end_src
#+RESULTS:
: ankle,croup,ear,elbow,eye,fhoof,hhoof,hip,knee,metacarpal,metatarsal,scapula,shoulder,snout,tailbase,withers,wrist
For some of the calculations below (the Procrustes step), I will restrict the subset of landmarks to the limbs, and exclude those on the back and head.
#+begin_src python :session :exports both :tangle yes
exclude_landmarks = [ 'snout', 'eye', 'ear' \
# , 'withers', 'croup', 'tailbase', 'scapula' \
] # exclude head module and back
landmarks = [lm for lm in landmarks \
if not (lm in exclude_landmarks) \
]
#+end_src
#+RESULTS:
You mind find these landmark choices arbitrary.
That is correct.
But it is my data, so I may do what I like :)
(evasion/lack of a good explanation tells you that this is the result of a tiresome trial-and-error procedure)
Finally, I will focus on the forelimb first.
#+begin_src python :session :tangle yes
limb = 'fl'
#+end_src
#+RESULTS:
* Hoof Speed
Fortunate for our analysis, the hoof landmarks move in a kind of regular pattern: during ground contact, their position is fixed, and in a swing, they move quickly.
Time for some audience participation: please
+ get up now,
+ walk across the room once, and
+ observe the first derivative of your left foots position!
Meanwhile, I will store the labels of the three most distal landmarks.
#+begin_src python :session :exports both :tangle yes
hoof_landmarks = \
{ 'fl': ['wrist', 'metacarpal', 'fhoof'] \
, 'hl': ['ankle', 'metatarsal', 'hhoof'] \
}
# take coordinates of selected distal limb points
points = trace.loc[:, hoof_landmarks[limb]]
likelihoods = [ points.loc[:, (hlm, 'likelihood')].values.reshape((-1,1)) for hlm in hoof_landmarks[limb] ]
points = [ points.loc[:, [(hlm, coord) for coord in xy ]].values for hlm in hoof_landmarks[limb] ]
#+end_src
#+RESULTS:
Images make the blog more interesting:
#+begin_src python :session :results file :exports both :tangle yes
PLT.close()
fig = PLT.figure(figsize = (750/dpi, 250/dpi), dpi = dpi)
ax = fig.add_subplot(1,1,1, aspect = 'equal')
for i, pt in enumerate(points):
ax.scatter(pt[:, 0], pt[:,1] \
, s = 10 \
, marker = 'o' \
, alpha = 0.2+0.5*likelihoods[i] \
, label = hoof_landmarks[limb][i]
)
ax.legend(loc = 'best')
fname = 'images/hooflandmarks_time.png'
fig.savefig(fname, dpi = dpi)
fname
#+end_src
#+RESULTS:
[[file:images/hooflandmarks_time.png]]
Behold the wavy patterns of hoof landmarks.
Here is where you see the *likelihood*: because of the way I ser =alpha= above, they are visible as half-transparent points.
The network is not always accurate (for example, it fails to digitize when my arm blocks view to the piglet on the video).
However, probabilistic deep learning has the neat feature of telling you how sure it was of a given landmark at a given time.
Low likelihood is a good criterium to discard data.
And more: we can use the likelihood to get a weighted average of landmarks.
#+begin_src python :session :exports both :tangle yes
# weighted averaging of distal limb points:
# sum up coordinates
point = NP.stack([pt * NP.dot(likh, NP.ones((1,2))) \
for likh, pt in zip(likelihoods, points)], axis = 2 \
)
# normalize by likelihood
normalizer = NP.dot( NP.sum( NP.concatenate(likelihoods, axis = 1), axis = 1).reshape((-1,1)) , NP.ones((1,2)) )
point = NP.divide(NP.sum(point, axis = 2), normalizer )
point[20:25, :]
#+end_src
#+RESULTS:
| 483.85410117 | 342.20505423 |
| 484.20987155 | 342.95532366 |
| 485.42723442 | 345.55535856 |
| 470.27070492 | 328.79654754 |
| 465.69795559 | 325.10079237 |
Some smoothing might be justified.
I only recently discovered the[[https://docs.scipy.org/doc/scipy/reference/generated/scipy.signal.savgol_filter.html?target=_blank][Savitsky-Golay Filter]] for this.
#+begin_src python :session :tangle yes
# smoothing
point = SIG.savgol_filter(point, window_length = 15, polyorder = 3, axis = 0)
point[20:25, :]
#+end_src
#+RESULTS:
| 477.30967968 | 337.58904729 |
| 477.92034818 | 337.63050531 |
| 476.93015378 | 336.24007836 |
| 475.02057085 | 334.69531584 |
| 472.53506036 | 332.53158915 |
... quite drastic smoothing, but that is okay: this data is only used to extract time stamps, and I want to reduce the chance of false positives triggered by noise.
One more step: let's take the first derivative, and we end up with a smoothed hoof speed.
#+begin_src python :session :tangle yes
# take the differential / spatial derivative
hoofspeed = NP.sum(NP.abs(NP.gradient(point, 5, axis = 0)), axis = 1)
#+end_src
#+RESULTS:
Plotting that speed, you might see what you expected from the participation exercise above: peaks!
#+begin_src python :session :results file :exports results :tangle yes
PLT.close()
fig = PLT.figure(figsize = (750/dpi, 250/dpi), dpi = dpi)
ax = fig.add_subplot(1,1,1)
ax.plot(trace.index.values, hoofspeed, 'k-')
ax.set_xlim([0., NP.max(trace.index.values)])
ax.set_ylim([0., 5.])
ax.set_xlabel('time')
ax.set_ylabel('hoof speed (px/frame)')
fig.tight_layout()
fname = 'images/hoofspeed.png'
fig.savefig(fname, dpi = dpi)
fname
#+end_src
#+RESULTS:
[[file:images/hoofspeed.png]]
Quick note on the start and end: these are rather inaccurate periods, where the auto-detected landmarks are jumpy and have a low likelihood (I selected a bit extra on the sides), and where the smoothing above might produce artifacts.
The peaks above are the swing phases.
They can vary in duration and height.
Our next task will be the automatic selection of these repetitive peaks.
* Cycles!
You can think of many ways of extracting the peak times.
And many disciplines have solved varieties of similar problems.
Some examples from the top of my head:
+ *Local maxima:* Try to find the plain maxima. The problem is that they are not smooth, and there might be multiple peaks, and smoothing could alter the exact timing.
+ *Autocorrelation:* Peaks are repetitive, so there would be a high auto correlation value at a characteristic time lag. However, the flat periods in between, and even some forms of signal noise, may contribute autocorrelation. One ends up with another variant of peak detection, and doesn't come much closer to the goal.
+ *Wavelet Detection:* This one comes from mass spectrometry, where there are particularly sharp peaks to be found in MS spectra. This is the strategy which worked for me.
A [[https://docs.scipy.org/doc/scipy/reference/generated/scipy.signal.ricker.html?target=_blank][wavelet]] is a function of approximately the following form:
#+begin_src python :session :results file :exports results :tangle yes
PLT.close()
fig = PLT.figure(dpi = dpi, figsize = (800/dpi, 300/dpi))
for x in range(2,8):
PLT.plot(SIG.ricker(2**8, 2**x) \
, lw = 2 if x == 4 else 1 \
, ls = '-' \
, zorder = 2 if x == 4 else 1 \
, alpha = 0.9 if x == 4 else 0.4 \
)
fig.tight_layout()
fname = 'images/wavelet.png'
fig.savefig(fname, dpi = dpi)
fname
#+end_src
#+RESULTS:
[[file:images/wavelet.png]]
You see wavelets of different width.
The emphasized green one is an intermediate width.
Looks totally like a Mexican Hat, don't you think?
Anyways. Here is the recipe for [[https://docs.scipy.org/doc/scipy/reference/generated/scipy.signal.cwt.html?target=_blank][Continuous Wavelet Transform]]:
+ generate a set of wavelets of varying width
+ loop over the signal, along the time axis
+ at every time point, for every wave width, calculate the correlation of the signal and the wavelet.
Or, in code: =scipy.signal.cwt=:
#+begin_src python :session :results output :tangle yes
# wavelet transform with mexican head wavelets to find relative peaks
wv = 16
widths = NP.arange(1, wv)
# cwtmatrix = SIG.cwt(hoofspeed, SIG.ricker, widths)
cwtmatrix1 = SIG.cwt(hoofspeed, SIG.ricker, widths)
cwtmatrix2 = SIG.cwt(hoofspeed, SIG.morlet2, widths)
#+end_src
#+RESULTS:
I am trying Ricker and Morlet here, because intuitively, a raw Morlet looks more like the peaks we are trying to detect.
However, ricker works better:
#+begin_src python :session :results file :exports results :tangle yes
PLT.close()
fig = PLT.figure(dpi = dpi, figsize = (800/dpi, 350/dpi))
fig.subplots_adjust(bottom = 0.1, top = 0.9, left = 0.1, right = 0.9, hspace = 0.15)
ax = fig.add_subplot(3, 1, 1)
ax.get_xaxis().set_visible(False)
ax.set_ylabel('hoof speed')
ax.plot(hoofspeed[600:800], 'k-')
ax = fig.add_subplot(3, 1, 2, sharex = ax)
ax.imshow(NP.real(cwtmatrix1[:, 600:800]), cmap = 'RdBu', origin = 'lower')
ax.set_ylabel('width')
ax.get_xaxis().set_visible(False)
ax.set_title('Ricker')
ax = fig.add_subplot(3, 1, 3, sharex = ax, sharey = ax)
ax.imshow(NP.real(cwtmatrix2[:, 600:800]), cmap = 'RdBu', origin = 'lower')
ax.set_ylabel('width')
ax.set_xlabel('time')
ax.set_title('Morlet')
fig.tight_layout()
fname = 'images/cwt.png'
fig.savefig(fname, dpi = dpi)
fname
#+end_src
#+RESULTS:
[[file:images/cwt.png]]
This is a solvable problem: The blue Ricker blobs are at the right time and have best match at approximately the width of the peaks we find.
How can we find these blue blobs?
Good news: the people from scikit image have done that before in =skimage.featude.peak_local_max=!
#+begin_src python :session :tangle yes
# find local maxima in the wavelet spectrogram
coords = FEAT.peak_local_max(cwtmatrix1.T, min_distance = 4, exclude_border = False)
time = coords[:, 0]
width = NP.array([widths[coord] for coord in coords[:, 1]])
#+end_src
#+RESULTS:
Because this algorithm is sensitive to smaller blobs, we can filter based on some heuristics:
+ cwt peaks should be above a threshold
+ ... and have a minimum peak width
#+begin_src python :session :exports both :tangle yes
threshold = 1.
sup = [idx for idx, t in enumerate(time) \
if (hoofspeed[t] > threshold) \
and (width[idx] > 2) \
] # the filter
time = time[sup]
width = width[sup]
peaks = NP.stack([time, width], axis = 1) # combine time/width
peaks = peaks[NP.argsort(peaks[:,0]), :] # sort by ascending time
peaks
#+end_src
#+RESULTS:
| 9 | 15 |
| 80 | 7 |
| 183 | 7 |
| 225 | 8 |
| 275 | 9 |
| 665 | 8 |
| 712 | 8 |
| 770 | 8 |
| 811 | 8 |
| 866 | 9 |
| 910 | 3 |
| 913 | 15 |
| 923 | 4 |
VoilĂ !
See how most peaks have a characteristic width?
#+begin_src python :session :results file :exports results :tangle yes
PLT.close()
fig = PLT.figure(dpi = dpi, figsize = (800/dpi, 200/dpi))
ax = fig.add_subplot(1, 1, 1)
ax.set_xlabel('time')
ax.set_ylabel('hoof speed')
ax.plot(hoofspeed, 'k-')
for t, w in peaks:
ax.axvspan(t-w, t+w, color = (0.3, 0.6, 0.1), alpha = 0.4)
ax.set_ylim([0., 5.])
fig.tight_layout()
fname = 'images/peaks_filtered.png'
fig.savefig(fname, dpi = dpi)
fname
#+end_src
#+RESULTS:
[[file:images/peaks_filtered.png]]
Quite good.
But I still feel the urge to shift, stretch, and rotate!
* Procrustes Optimization
Remember that we have more than just the hoof landmarks.
Even though the hoof movement is repetitive, we should not disregard the rest of the information.
We are looking for cyclical steady state locomotion.
Here are the ones we can filter immediately:
+ peaks are behind the end of the episode (shouldn't happen; could be due to cutting)
+ exclude too close peaks (i.e. too short/unsteady ground contacts or noise)
+ too high inter-peak-interval (ipi)
+ peaks overlap
+ widths are too dissimilar
+ think of more filters for your data!
#+begin_src python :session :tangle yes
def IsThisAGoodCycle(t1, w1, t2, w2):
# first footfall after the end of the episode
if t1 > trace.shape[0]:
return False
# second footfall after the end of the episode
if t2 > trace.shape[0]:
return False
# peaks are way too far apart (my piglets don't move that slow)
if t2-t1 > 120:
return False
# peaks overlap
if t2-t1 < w1+w2:
return False
# too different widths = non-steady-state
if w2-w1 > 6:
return False
# peak is good
return True
#+end_src
#+RESULTS:
We will loop peak pairs, i.e. compare one swing phase to the next.
#+begin_src python :session :tangle yes :results output
# loop peak pairs
goodpeaks = []
for pk1, pk2 in zip(peaks[:-1, :], peaks[1:, :]):
# reference timepoint
t1, w1 = NP.sum(pk1), pk1[1]
# comparison interval center
t2, w2 = NP.sum(pk2), pk2[1]
if not IsThisAGoodCycle(t1, w1, t2, w2):
continue
goodpeaks.append([t1, w1, t2, w2])
print(NP.stack(goodpeaks))
#+end_src
#+RESULTS:
: [[ 24 15 87 7]
: [ 87 7 190 7]
: [190 7 233 8]
: [233 8 284 9]
: [673 8 720 8]
: [720 8 778 8]
: [778 8 819 8]
: [819 8 875 9]
: [875 9 913 3]]
We've lost quite a few false positives.
But we can do even better!
Here are some helper functions to quickly grab all landmarks at a certain time point.
#+begin_src python :session :tangle yes
NewConfiguration = lambda tp: NP.concatenate([ \
trace.iloc[[tp], :].loc[:, [(lm, coord) for coord in xyl]].values \
for lm in landmarks \
], axis = 0)
config_store = {} # due to multiple grabs, it makes sense to store configs temporarily
def GetConfiguration(tp):
configuration = config_store.get(tp, None)
if configuration is not None:
return configuration
configuration = NewConfiguration(tp)
config_store[tp] = configuration
return configuration
MultiConfiguration = lambda tp: NP.concatenate([GetConfiguration(dtp) \
for dtp in [tp-1, tp, tp, tp+1] \
], axis = 0)
landmarkcount_multiplier = 4
SupLikelihoods = lambda configuration: configuration[:, 2] > 0.9
# a function to quickly find a local minimum, given a start position
def FindLocalCenterMinimum(image, pos = None):
if pos is None:
pos = NP.array(image.shape) // 2
print ('start:', pos)
wl = 3 # window length
window = image[pos[0]-wl:pos[0]+wl+1, pos[1]-wl:pos[1]+wl+1]
if NP.all(NP.isnan(window)):
# nan escape
return pos
# get local minimum
d_pos = NP.unravel_index(NP.nanargmin(window), window.shape)
if NP.sum(NP.abs(d_pos - wl*NP.ones((2,)))) == 0:
# found minimum
return pos
# update position
pos = pos + d_pos - wl
if (pos[0] == 0) or (pos[0] + 1 == image.shape[0]) \
or (pos[1] == 0) or (pos[1] + 1 == image.shape[1]):
# escape border
return None
return FindLocalCenterMinimum(image, pos)
#+end_src
#+RESULTS:
Steady state locomotion means that start and end of a cycle will have the animal in approximately the same configuration.
This means, when we take landmarks at those timepoints, the *Procrustes distance* will be close to zero.
It is hardly possible in reasonable time to cross compare all frames to find Procrustes Distance minima.
Luckily, we have some clue where the cycle starts and ends!
From the detected peaks, we roughly know the limb touch down (=t + w=) and can look in the proximity of that time point where the frame resemblance is best.
#+begin_src python :session :tangle yes :results output raw
image_store = {}
PreWindow = lambda tx, wx: tx-wx//2
for t1, w1, t2, w2 in goodpeaks:
# grab configurations
# likelihoods can be very low (threshold!); min number of points above threshold; compare only those
# store output as 2d image
range_t1 = NP.arange(PreWindow(t1, w1), t1+w1+1)
range_t2 = NP.arange(PreWindow(t2, w2), t2+w2+1)
range_t2 = range_t2[range_t2 < trace.shape[0]]
pd_image = NP.empty((len(range_t1), len(range_t2)))
nl_image = NP.empty((len(range_t1), len(range_t2)))
pd_image[:,:] = NP.nan
nl_image[:,:] = NP.nan
for i_ref, t_ref in enumerate(range_t1):
# the reference configuration
ref_configuration = MultiConfiguration(t_ref) # the configuration at the first peak
ref_filter = SupLikelihoods(ref_configuration) # filter only well resolved landmarks
ref_configuration[:, :2] = ST.Standardize(ref_configuration[:, :2])
for i_candidate, t_candidate in enumerate(range_t2):
# the candidate configuration
candidate_configuration = MultiConfiguration(t_candidate) # comparison: configuration at second peak
candidate_configuration[:, :2] = ST.Standardize(candidate_configuration[:, :2]) # filter only well resolved landmarks
landmark_filter = NP.logical_and(ref_filter, SupLikelihoods(candidate_configuration))
# minimum number of landmarks
n_landmarks = NP.sum(landmark_filter)
if n_landmarks < landmarkcount_multiplier*6:
# too few landmarks
continue
# Procrustes!
transformed, pd, _ = ST.Procrustes( ref_configuration[landmark_filter, :2] \
, candidate_configuration[landmark_filter, :2])
pd_image[i_ref, i_candidate] = pd
nl_image[i_ref, i_candidate] = n_landmarks
if NP.sum(NP.logical_not(NP.isnan(pd_image))) < 6:
# not enough comparisons found in the process
continue
image_store[t1] = { \
'tw': [t1, w1, t2, w2] \
, 'pd': pd_image \
, 'nl': nl_image \
}
#+end_src
#+RESULTS:
We can plot the "image" of procrustes distances in the vicinity of the previously detected peak times:
#+begin_src python :session :results file :exports both :tangle yes
t1 = goodpeaks[6][0]# 778
store = image_store[t1]
t1, w1, t2, w2 = store['tw']
pd_image = store['pd']
# find a local minimum
pos = FindLocalCenterMinimum(pd_image, pos = NP.array((w1//2, w2//2)))
print (pos)
PLT.close()
fig = PLT.figure(dpi = dpi)
fig.subplots_adjust(bottom = 0.1, left = 0.1, top = 0.99, right = 0.99, hspace = 0.05)
ax = PLT.subplot(1,1,1, aspect = 'equal')
ax.imshow(pd_image, origin = 'lower', cmap = 'RdBu')
ax.axvline(w2//2, color = 'w', lw = 0.5)
ax.axhline(w1//2, color = 'w', lw = 0.5)
# show minimum
if pos is not None:
ax.axvline(pos[1]+0.1, color = 'y', lw = 0.5)
ax.axhline(pos[0]+0.1, color = 'y', lw = 0.5)
ax.set_xticklabels(ax.get_xticks()-w2//2)
ax.set_yticklabels(ax.get_yticks()-w1//2)
ax.set_xlabel('start time offset')
ax.set_ylabel('end time offset')
fname = 'images/pd_image.png'
PLT.savefig(fname, dpi = dpi)
fname
#+end_src
#+RESULTS:
[[file:images/pd_image.png]]
As we see, shifting the start and end times a bit (yellow cross) will give a better match of the landmark configurations in this example.
The picture is not always that clear: there might be no minima at all, or the search for them might end up at the edges (indicating that our search space is too small).
In my experience, the optimization procedure will usually be good for the good cycles.
Applying the procedure to all the cycles, but with the convenient =skimage= function to find the local minima:
#+begin_src python :session :tangle yes :results output raw
cycles = []
for store in image_store.values():
t1, w1, t2, w2 = store['tw']
pd_image = store['pd']
nl_image = store['nl']
# again, we find the local image maxima to get the peaks.
local_minima = FEAT.peak_local_max(-pd_image+NP.nanmax(pd_image) \
, min_distance = 2 \
, exclude_border = False \
)
if len(local_minima) == 0:
# no local minimum was found
continue
# we will keep only that local minimum which is closest to the original peak position
center_dist = NP.sum(NP.abs(local_minima - NP.array(pd_image.shape).reshape((1,-1))), axis = 1)
best_idx = local_minima[NP.argmin(center_dist), :]
pd_min = pd_image[best_idx[0],best_idx[1]]
nl_min = nl_image[best_idx[0],best_idx[1]]
# ... and that local minimum is our best guess for a cycle!
cycles.append([PreWindow(t1, w1) + best_idx[0], PreWindow(t2, w2) + best_idx[1], NP.round(pd_min, 5), nl_min])
print (PD.DataFrame(cycles, columns = ['start', 'end', 'Procrustes distance', 'n landmarks']) \
.to_markdown(index = False) \
.replace(':|', '-+') \
)
#+end_src
#+RESULTS:
| start | end | Procrustes distance | n landmarks |
|-------+-----+---------------------+-------------|
| 94 | 197 | 0.09501 | 48 |
| 197 | 233 | 0.03045 | 56 |
| 241 | 280 | 0.0029 | 56 |
| 681 | 728 | 0.00339 | 56 |
| 727 | 786 | 0.00902 | 56 |
| 786 | 827 | 0.00235 | 56 |
| 827 | 881 | 0.0065 | 32 |
Seen from this table, Procrustes distance opens the door for another heuristic to filter "bad" cycles.
Furthermore, you can check the stride distance, duration, and many other spatiotemporal parameters at this point to throw out garbage.
But I'll close here.
* Summary
To re-iterate: Here is how you got a quite accurate estimate of cycle times.
+ Use hoof speed, which has characteristic peaks in its temporal profile.
+ Continuous Wavelet Transform can identify these peak times.
+ Procrustes can compare configurations to refine cycle times.
+ Along the way, use bespoke heuristics for your data set to filter out false positives.
#+begin_src python :session :results file :exports both :tangle yes
PLT.close()
fig = PLT.figure(dpi = dpi, figsize = (800/dpi, 400/dpi))
fig.subplots_adjust(bottom = 0.15, left = 0.1, top = 0.98, right = 0.99, hspace = 0.05)
ax = PLT.subplot(2,1,1)
ax.plot(hoofspeed)
ax.set_xlim([0, len(hoofspeed)])
ax.set_ylim([0, 10])
ax.get_xaxis().set_visible(False)
for t, w in zip(time, width):
ax.axvspan(t-w, t+w, color = 'k', alpha = 0.2)
for t1, t2, _, _ in cycles:
ax.axvspan(t1, t2, facecolor = 'g', edgecolor = 'k', alpha = 0.3)
ax.set_ylabel('hoof speed')
ax = PLT.subplot(2,1,2, sharex = ax)
ax.imshow(cwtmatrix1, extent=[0, len(hoofspeed), 1, wv], cmap='RdBu', aspect='auto',
vmax=abs(cwtmatrix1).max()*0.2, vmin=-abs(cwtmatrix1).max()*0.2, origin = 'lower')
ax.scatter(time, width, s = 30, color = 'r', marker = 'x', alpha = 0.7)
ax.set_xlabel('time')
ax.set_ylabel('wavelet width')
fname = 'images/timeline_overview.png'
PLT.savefig(fname, dpi = dpi)
fname
#+end_src
#+RESULTS:
[[file:images/timeline_overview.png]]
I would be happy to hear if this was useful for you, or if you maybe discovered improvements on one or the other intermediate step!
Thanks for reading!