Download Python BTK 0.3 Cheat Sheet and more Cheat Sheet Programming Languages in PDF only on Docsity!
Python BTK 0.3 Cheat Sheet
( version 0.1)
Learn more on
http://b-tk.googlecode.com
http://biomechanical-toolkit.github.io/
Preliminary
Install BTK python
Prequisite :
- Select a python environment (e.g. Anaconda,
pythonxy)
- Download corresponding OS BTK Python binary
- Install the BTK package in your Python package folder
- Call BTK from your script by typing :
import btk
btkAnalog
What can i get from a C3D file?
The C3D file format is a standard widely use to store motion capture system
EVENT POINT ANALOG METADATA force platform
0d 3d 1d info 6 componant device
SIGNALS DEVICE
Find Help
Find help on a method, or function
- Use online Doxygen documentation :
- in your script, type
return help on the the method GetAnalog() of the
btkAquisition object
Use support
- Post a message on the BTK Users forum
help (btk.btkAcquisition.GetAnalog)
File I/O
Goal : construct a btkAcquisition
Object from a C3D
reader = btk.btkAcquisitionFileReader()
reader.SetFilename(“input.c3d")
reader.Update()
acq = reader.GetOutput()
Goal : from a btkAcquisition,
generate a C3D file
writer = btk.btkAcquisitionFileWiter()
writer.SetInput(acq) # acq = btkAcquisition
writer.SetFileName(“output.c3d")
writer.Update()
Pipeline
input.c3d btkFileReader
output.c3d btkAcquisition (acq) btkFileWriter Getter/Setter on acq (getter/setter)
btkEvent btkPoint btkAnalog btkMetadata
Force platform
accessible through a Filter
(see Force platform section)
Filters
Read Write
Object accesible from a btkAcquisition
acq. GetPoint (“LASI”), acq. GetEvent (0) , acq. GetAnalog (“emg1”), acq. GetMetaData ()
examples
Btk contributor : Fabien Leboeuf ( University of Salford,
UK)
You want to start programming with python Btk.
You don’t know how to get C3D-embeded data.
= > This cheat sheet gathers all useful methods for you.
btkAcquisition
basic info
acq.GetFirstFrame()
acq.GetLastFrame()
acq.GetDuration()
btkPoint
acq. GetPointFrequency () acq. GetPointFrameNumber () acq. GetPointNumber () acq. IsEmptyPoint ()
LASI= acq. GetPoint (“LASI”)
Pt0= acq.GetPoint (0)
Return btkPoint()
LASI. GetValue (10) # or acq.GetPoint (“LASI”).GetValue(10)
Return a numpy array ( size : 1,3)
Get value at frame 10
LASI. GetValues () # or acq.GetPoint (“LASI”).GetValues()
Return a numpy array ( size : n frames, 3)
Get all values
Values = LASI.GetValues() Values [0,2] # get row 0, col 2 LASI.GetValues() [:,2] # all rows in col 2
Get Value or Values
Use numpy array indexing to get data
See also :
http://docs.scipy.org/doc/numpy/user/basics.indexing.html
LASI. SetValue (10,2, 100) # or acq.GetPoint (“LASI”).SetValue(10,2, 100)
For Point LASI , Set value ( 100) at a
frame 10 on col 2
Change all values of the LASI marker
Howto : pass a numpy.array
Import numpy as np # do not forget to import numpy
Nframe = acq.GetPointFrameNumber() values = np.zeros((Nframe ,3)) # zeros array ( size : Nframe rows, 3 col) LASI = acq.GetPoint (“LASI”) LASI. SetValues _(values )
---or---
acq.GetPoint (“LASI”). SetValues(values )_
- Values
- Description
- Label
- Residual
- Type
acq.GetPoint(0). GetLabel () acq.GetPoint("LASI"). Set Label("left ASIS ")
acq.GetPoint(0). GetDescription () acq.GetPoint("LASI"). Set Description(« left antero-superior ilica spine ")
newpoint = btk.btkPoint(“newLabel”, acq.GetPointFrameNumber()) newpoint.SetValues(values) # values a 3d numpy array acq.AppendPoint(newPoint)
But…A btkPoint is not a marker only!
btkEvent
By default : type is marker
In Biomechanics, you can find Euler
Angles, Moment
Look out some commercial model add
power as a 3d vector! Then, in BTK, you will
find : btk.btkPointType.Power
btk.btkPointType.Angle btk.btkPointType.Moment
angle= acq.GetPoint (“LHipAngles”) # read an
angle
acq.AppendPoint(newPoint, btk.btkPointType.
Angle ) # append newpoint as an angle
examples : Read an angle. Append a new
point as an angle into an acquisition
Return btkEvent
- Label
- Frame
- Description
- Context
Get an event
ev0= acq.GetEvent (0)
Need an index
See "Collection" to handle ALL Points
See "Collection" to handle ALL Points
ev0.GetLabel() ev0.setLabel("Toe Off")
A Context maybe General or a Left (right) side event
ev0.GetFrame() ev0.SetFrame(200) ev0.GetDescrition() ev0.SetDescription("begin of the swing phase")
ev0.GetContext() ev0.SetContext(200)
Iterator
the parameter behind the
convenient « Find » method
Context : you want find a parameter
by its label
Principle :
1. Find method from your
acquisition
2. This will return an iterator
3. Get iterator value ( could be a
point, analog,event)
Example:
# find a point
myIt= acq. FindPoint (“LHipAngles”)
# know the type of iterator
Print myIt
# get its value
myIt. value() # return a btkPoint here
# now tou can get the btkPoint by classic
method
myIt.value(). GetValues()
Similar process with :
FindEvent(), FindAnalog()
myIt= acq.FindPoint (“unknownPoint”)
# doesn’t call an Exception
A windows error is displayed if :
myIt.value() #cannot find a btkPoint()
Unknown point
begin loop end
iterator
If find
see : iterator
Convenient getter/setter
Get a Point (eg LASI or index 0)
Find a point
Convenient getter/setter
Basic info
Set new Values
Set a new Value
Create a New Point and Append to a btkAcquisition
btkCollection
BTK Filters (Theory)
Definition : a Collection is a list of btk-object ( e.g: btkEvent(), btkPoint(),
btkAnalog())
Do not mix up Collection with the standard python list parameter
btkCollection has its own method
acq.GetPoints()
acq.GetAnalogs
acq.GetEvents()
btkPoint() btkPoint()
items Item(0) Item(1)
btkPointCollection()
return the concrete object
Item(i)
Pts = acq.GetPoints()
Pts.GetItem(0).GetValues() #get values of
the btkPoint located at item 0
Pts.GetItem(0).GetLabel) #get label of the
btkPoint located at item 0.
for it in btk.Iterate (acq.GetEvents()):
print it.GetLabel() # display each
label of it, i.e a btkEvent object
The convenient function Iterate
Notice : we write btk.Iterate
because the btk package is
loaded with the directive : import
btk
Create an empty acquisition
newAcq = btk.btkAcquisition()
newAcq.Init(5, 200, 10, 2)
An empty acquisition is enabled throught calling both btkAcquisition
constructor and Init method
Init signature
(number of Point , point frame number, number of analog , analog sample per point
frame).
In the example above, we infer that we have 5 btkPoints, 10 btkAnalogs, then, that point
frame number and analog frame number are 200 and 400 respectively.
What for?
- Store time-normalized data ( e g : data normalized on a gait cycle)
- Others ?( feel free to propose your application through the Btk website
A set of Filters consists of a Pipeline. A Filter articulates object for a common purpose.
Filters process input data only on demand when using the Update() method. Updating
only the last filter should be enough to process all the other ones
input.c3d
btkFileReader
output.c3d
btkAcquisition (acq) btkFileWriter
Filters 1 (^) Filters 2
input # input #
input #
btkAcquisition (acq)
output #1 output #
In this illustration, 2 filters are inserted. Let us notice than filter #1 has 3 inputs
parameter whereas filter# 2 , 2 inputs only.
Output#1 is not necessary a btkAcquisition. However, output# 2 has to be a
btkAcquisition because eventually, we write a c3d
Advice : identify the nature of each filter input from the main Documentation
Some methods returning a btkCollection
Illustration
Get a object-item Object iteration
Illustration
BTK Filters (Practice)
_reader = btk.btkAcquisitionFileReader() reader.SetFilename(“file.c3d") reader.Update() acq = reader.GetOutput()
the filter---------
pfe = btk._ btkForcePlatformsExtractor() pfe. SetInput (acq) pfe.Update() pfc = pfe.GetOutput() # a btkPlateFormCollection #--------------------- pf1 = pfc.GetItem(0) # item 0 = First force platform
btkAcquisition (acq)
Filter btkForcePlatformsExtractor
btkForcePlatformCollection
Notice : the output object is a Collection. That’s mean we can iterate on it!
The number of item is the number of force platform
Example :
Return btkForcePlatform
- Label
- Type
- Channel (0 to 5)
- Corner (0 to 3)
btkAnalog() per channel
Numpy array(1,3) = global position
pf1.GetType() pf1.GetChannelNumber() ch0 = pf1.GetChannel(0) # ch0: btkAnalog ch0.GetLabel() ch0.GetValues() return the n analog values
Generally, channels 0 to 2 = force components, 3 to 5 = moment components
btkAcquisition # btkAcquisition # btkAcquisition #
Filter mergerAcquisitionFilter
btkAcquisition
# Readers readerTRB = btk.btkAcquisitionFileReader() readerTRB.SetFilename("myGait.trb") readerANB = btk. btkAcquisitionFileReader() readerANB.SetFilename("myGait.anb") readerCAL = btk. btkAcquisitionFileReader() readerCAL.SetFilename("forcepla.cal") readerXLS = btk. btkAcquisitionFileReader() readerXLS.SetFilename("myGait.xls")
# Merger merger = btk. btkMergerAcquisitionFilter() merger. SetInput (0, readerTRB.GetOutput()) merger. SetInput (1, readerANB.GetOutput()) merger. SetInput (2, readerCAL.GetOutput()) merger. SetInput (3, readerXLS.GetOutput()) merger.Update()
# Writer writer = btkAcquisitionFileWriter() writer.SetInput(merger.GetOutput()) writer.Update()
A merger is proprer method if a motion capture system dissociates
biomechanical information on different file
Concrete example : gathering files provided from a « Motion Analysis Corp »
system
Get the 6 components of a force platform Merger
A Script for starting
# -- coding: utf-8 --
Created on Mon Jul 20 11:26:33 2015
@author: --
#------ import packages ---------
import btk
import matplotlib.pyplot as plt
import scipy
import scipy.signal
import scipy.linalg
plt.close("all")
#------ READ YOUR FILE ---------
reader=btk.btkAcquisitionFileReader()
reader.SetFilename("input.c3d")
reader.Update()
acq=reader.GetOutput()
#------ EXPLORE YOUR ACQUISITION ---------
#------ CONSTRUCT YOUR PIPELINE ---------
#------ WRITE YOUR FILE ---------
writer=btk.btkAcquisitionFileWriter()
writer.SetInput(acq)
writer.SetFilename('output.c3d')
writer.Update()
Ready to start, but you don’ t know how.
- Copy/paste this script in your IDE
- Save it as myScript.py
- Run it