<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">#!/usr/bin/env python

'''
-------------------
Copyright (c) 2015 Computational Biomechanics (CoBi) Core, Department of
Biomedical Engineering, Cleveland Clinic

Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to permit
persons to whom the Software is furnished to do so, subject to the
following conditions:

The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
USE OR OTHER DEALINGS IN THE SOFTWARE.
-------------------

WRITTEN BY:
-----------
Craig Bennetts, MS
Computational Biomodeling (CoBi) Core
Department of Biomedical Engineering
Lerner Research Institute
Cleveland Clinic
Cleveland, OH
bennect2@ccf.org


DESCRIPTION:
------------
Plots the 6 DOF kinematics (positions: x,y,z; angles: Rx,Ry,Rz) extracted from 
an FEBio simulation, converted from quaternions to Euler angles.

LEGEND:
-------
&lt;...&gt; = variable argument

USAGE:
------
./plot_FEBio_kinematics.py &lt;FEBio_kinematics.txt&gt;

INPUT FILE:
-----------

&lt;FEBio_kinematics.txt&gt;

	Each line of the FEBio simulation has the following format:
	
	&lt;time&gt; &lt;x&gt; &lt;y&gt; &lt;z&gt; &lt;Rx&gt; Ry&gt; &lt;Rz&gt;

OUTPUT FILE:

	image (svg) of plot

'''

import sys
import matplotlib.pyplot as plt

def USAGE(argv):

	print
	print 'USAGE: ' + argv[0] + ' &lt;kinematics.txt&gt;'
	print


def plot_FEBio_kinematics(argv):

	# CHECK SCRIPT USAGE
	if len(argv)!=2:
		USAGE(argv)
		sys.exit(1)

	#log_filenames = argv[1:]
	kinematics_filename = argv[1]

	# READ LOG FILE
	KinematicsFile = open(kinematics_filename,'r')
	KinematicsLines = KinematicsFile.readlines()
	KinematicsFile.close()

	# create lists for kinematic degrees of freedom (DOFs)
	t = []
	x = []
	y = []
	z = []
	Rx = []
	Ry = []
	Rz = []

	for line in KinematicsLines:
		split_line = line.strip('\n').split(' ')
		text_values = filter(lambda a: a!='', split_line)
		values = map(float, text_values)
		t.append( values[0] )
		x.append( values[1] )
		y.append( values[2] )
		z.append( values[3] )
		Rx.append( values[4] )
		Ry.append( values[5] )
		Rz.append( values[6] )

	plt.figure(1)

	ax = plt.subplot(211)
	plt.plot( t, x, 'r.-', label='x')
	# plot line label at end of line
	#plt.plot( t, x, 'r.-')
	#plt.text( t[-1], x[-1], 'x')
	plt.plot( t, y, 'g.-', label='y')
	plt.plot( t, z, 'b.-', label='z')
	plt.xlabel('t (s)')
	plt.ylabel('position (mm)')

	# positive position DOF labels
	# WARNING: need to know right/left knee to set medial/lateral this properly!!
	plt.text(0.20,0.925, 'lateral', color='r', transform=ax.transAxes)
	plt.text(0.45,0.925, 'anterior', color='g', transform=ax.transAxes)
	plt.text(0.70,0.925, 'superior', color='b', transform=ax.transAxes)

	# negative position DOF labels
	# WARNING: need to know right/left knee to set medial/lateral this properly!!
	plt.text(0.20,0.025, 'medial', color='r', transform=ax.transAxes)
	plt.text(0.45,0.025, 'posterior', color='g', transform=ax.transAxes)
	plt.text(0.70,0.025, 'inferior', color='b', transform=ax.transAxes)

	legend = ax.legend(framealpha=0.5)
	legend.draggable()

	#ax.relim()
	#ax.autoscale_view(False,True,True)
	#ax.margins(0.0,0.1)
	
	#plt.legend(loc='upper left', shadow=False)
	#plt.legend(bbox_to_anchor=(0.9, 1), framealpha=None, borderaxespad=0.)

	ax = plt.subplot(212)
	plt.plot( t, Rx, 'r.-', label='Rx')
	plt.plot( t, Ry, 'g.-', label='Ry')
	plt.plot( t, Rz, 'b.-', label='Rz')
	plt.xlabel('t (s)')
	plt.ylabel('angle (degrees)')

	#ax.text()
	
	# positive position DOF labels
	# WARNING: need to know right/left knee to set medial/lateral this properly!!
	plt.text(0.20,0.925, 'flexion', color='r', transform=ax.transAxes)
	plt.text(0.45,0.925, 'valgus', color='g', transform=ax.transAxes)
	plt.text(0.70,0.925, 'external', color='b', transform=ax.transAxes)

	# negative position DOF labels
	# WARNING: need to know right/left knee to set medial/lateral this properly!!
	plt.text(0.20,0.025, 'extension', color='r', transform=ax.transAxes)
	plt.text(0.45,0.025, 'varus', color='g', transform=ax.transAxes)
	plt.text(0.70,0.025, 'internal', color='b', transform=ax.transAxes)

	legend = ax.legend(framealpha=0.5)
	legend.draggable()

	# weird, doesn't seem to do what I want
	#ax.relim()
	#ax.autoscale_view(False,True,True)
	
	# Save plot
	# "supposedly" valid output image types: png, pdf, ps, eps, svg
	# must save plot before showing it!
	svg_filename = kinematics_filename[:-4] + '_PLOT.svg'
	plt.savefig(svg_filename)
	png_filename = kinematics_filename[:-4] + '_PLOT.png'
	plt.savefig(png_filename)

	# comment out to remove interactivity
	plt.show()


# END OF plot_FEBio_kinematics()


# Default calls SLURM_FEBio_BATCH() if this script is not called as a function
if __name__ == "__main__":
	plot_FEBio_kinematics(sys.argv)</pre></body></html>