Demo entry 6091774

biomechanics post for anybody modeling system

   

Submitted by Can on Sep 26, 2016 at 11:18
Language: Python. Code size: 11.0 kB.

# -*- coding: utf-8 -*-
"""
Created on Fri Sep 09 16:40:16 2016

AnyBody 2016 September Course Assignment

@author: Can Ozcan
"""

import h5py
import numpy as np
from matplotlib import pyplot as plt
import csv

def genPlot(i, title, labely='', labelx='Time (s)'):
    resultsNames = ['Normal', 'Toe In', 'Toe Out', 'Insole 10']
    for j in range(4):
        plt.plot(dataSet[j][0], dataSet[j][i], label=resultsNames[j])
    plt.xlabel(labelx)
    plt.ylabel(labely)
    leg = plt.legend(bbox_to_anchor=(1.0, 0.815),loc='center left', ncol=1)
    plt.grid(which='both')
    plt.title(title)
    plt.show() 
    plt.savefig(title+'.png', bbox_extra_artists=(leg,), bbox_inches='tight')
    plt.close()
    
    # Write csv file
    maxLen=max(len(dataSet[0][i]), len(dataSet[1][i]), len(dataSet[2][i]), len(dataSet[3][i]))
    csvArray = np.zeros((maxLen, 8))
    for j in range(4):
        for (k,item) in enumerate(dataSet[j][i]):
            csvArray[k, 2*j] = dataSet[j][0][k]        
            csvArray[k, 2*j+1] = item
    with open(title+'.csv', 'wb') as csvfile:
        spamwriter = csv.writer(csvfile, delimiter=',',
                            quotechar='|', quoting=csv.QUOTE_MINIMAL)
        for row in csvArray:
            spamwriter.writerow(row)

baseDir=r"E:\WORK\ANYBODY_WORK\AALBORG_2016\ASSIGNMENT\AssignmentModel\LowerExtremity-RBF-scaling\Output"
resultsList = ['\S1_Gait_Shod.anydata',
               '\S1_Gait_Shod_Toe_in.anydata',
               '\S1_Gait_Shod_Toe_out_wide.anydata',
               '\S1_Gait_Insole_10.anydata']
            

dataSet=[]
for i in range(len(resultsList)):
    dataSet.append([])
    hf = h5py.File(baseDir+resultsList[i]+'.h5', 'r')
    data = hf.get('Output')
    # Data to be extracted
    #1 Joint Angles
    #2 Joint Moments
    #3 Ground Reaction Forces
    #4 Joint Reaction Forces
    #5 Muscle Activity
    
    # Time
    time = np.array(data.get('Abscissa').get('t'))    
    dataSet[-1].append(time)
    #1 Joint Angles
    ankleDorsiFlexion = data.get('JointAnglesAndMoments').get('Right').get('AnkleDorsiFlexion').get('Pos')  
    dataSet[-1].append(ankleDorsiFlexion)
    ankleInternalRotation = data.get('JointAnglesAndMoments').get('Right').get('AnkleInternalRotation').get('Pos')  
    dataSet[-1].append(ankleInternalRotation)    
    ankleInversion = data.get('JointAnglesAndMoments').get('Right').get('AnkleInversion').get('Pos')  
    dataSet[-1].append(ankleInversion)  
    
    hipFlexion = data.get('JointAnglesAndMoments').get('Right').get('HipFlexion').get('Pos')
    dataSet[-1].append(hipFlexion)      
    hipInternalRotation = data.get('JointAnglesAndMoments').get('Right').get('HipInternalRotation').get('Pos')
    dataSet[-1].append(hipInternalRotation)      
    hipAbduction = data.get('JointAnglesAndMoments').get('Right').get('HipAdduction').get('Pos')
    dataSet[-1].append(hipAbduction)      
    
    kneeFlexion = data.get('JointAnglesAndMoments').get('Right').get('KneeFlexion').get('Pos')
    dataSet[-1].append(kneeFlexion)      
    kneeInternalRotation = data.get('JointAnglesAndMoments').get('Right').get('KneeInternalRotation').get('Pos')
    dataSet[-1].append(kneeInternalRotation)      
    kneeAdduction = data.get('JointAnglesAndMoments').get('Right').get('KneeAdduction').get('Pos')
    dataSet[-1].append(kneeAdduction)      
    
    #2 Joint Moments
    hipMoments = data.get('JointAnglesAndMoments').get('Right').get('MomentsDistalFrame').get('HipMoments')
    dataSet[-1].append([x[0] for x in hipMoments]) # Abduction(+) and Adduction(-) 
    dataSet[-1].append([x[1] for x in hipMoments]) # Internal Rotation(+) and External Rotation(-)
    dataSet[-1].append([x[2] for x in hipMoments]) # Flexion(+) and Extension(-)
    kneeMoments = data.get('JointAnglesAndMoments').get('Right').get('MomentsDistalFrame').get('KneeMoments')
    dataSet[-1].append([x[0] for x in kneeMoments]) # Abduction(+) and Adduction(-)       
    dataSet[-1].append([x[1] for x in kneeMoments]) # Internal Rotation(+) and External Rotation(-)
    dataSet[-1].append([x[2] for x in kneeMoments]) # Flexion(+) and Extension(-)
    ankleMoments = data.get('JointAnglesAndMoments').get('Right').get('MomentsDistalFrame').get('AnkleMoments')
    dataSet[-1].append([x[0] for x in ankleMoments]) # Inversion(+) and Eversion(-)       
    dataSet[-1].append([x[1] for x in ankleMoments]) # Internal Rotation(+) and External Rotation(-)
    dataSet[-1].append([x[2] for x in ankleMoments]) # Dorsi flexion(+) and Plantar flexion(-)
    
    #3 Ground Reaction Forces
    reactionForce_fx = np.array(data.get('EnvironmentModel').get('Plate1').get('CenterOfPressure').get('fx'))
    dataSet[-1].append(reactionForce_fx)      
    reactionForce_fy = np.array(data.get('EnvironmentModel').get('Plate1').get('CenterOfPressure').get('fy'))
    dataSet[-1].append(reactionForce_fy)
    reactionForce_fz = np.array(data.get('EnvironmentModel').get('Plate1').get('CenterOfPressure').get('fz'))
    dataSet[-1].append(reactionForce_fz)
    reactionForce_mx = np.array(data.get('EnvironmentModel').get('Plate1').get('CenterOfPressure').get('mx'))
    dataSet[-1].append(reactionForce_mx)
    reactionForce_my = np.array(data.get('EnvironmentModel').get('Plate1').get('CenterOfPressure').get('my'))
    dataSet[-1].append(reactionForce_my)
    reactionForce_mz = np.array(data.get('EnvironmentModel').get('Plate1').get('CenterOfPressure').get('mz'))
    dataSet[-1].append(reactionForce_mz)
    reactionForce_Vx = np.array(data.get('EnvironmentModel').get('Plate1').get('CenterOfPressure').get('Vx'))
    dataSet[-1].append(reactionForce_Vx)
    reactionForce_Vy = np.array(data.get('EnvironmentModel').get('Plate1').get('CenterOfPressure').get('Vy'))
    dataSet[-1].append(reactionForce_Vy)
    
    #4 Joint Reaction Forces
    kneeForces = np.array(data.get('JointReactionForces').get('RightKneeForces'))
    for j in range(3):    
        dataSet[-1].append([x[j] for x in kneeForces])      
    kneeLateralForce = np.array(data.get('JointReactionForces').get('RightLateralKneeForce'))
    dataSet[-1].append(kneeLateralForce)      
    kneeMedialForce = np.array(data.get('JointReactionForces').get('RightMedialKneeForce'))
    dataSet[-1].append(kneeMedialForce)      
    ankleForces = np.array(data.get('JointReactionForces').get('RightAnkleForces'))
    for j in range(3):    
        dataSet[-1].append([x[j] for x in ankleForces])      
    hipForces = np.array(data.get('JointReactionForces').get('RightHipForces'))
    for j in range(3):    
        dataSet[-1].append([x[j] for x in hipForces])      
    
    #5 Muscle Activity
    musActivity = np.array(data.get('MaxMuscleActivity'))
    dataSet[-1].append(musActivity)      
    
    # 6 Joint Moments in ISB coordinate systems
    ankleDorsiFlexionM = data.get('JointAnglesAndMoments').get('Right').get('AnkleDorsiFlexion').get('M_Projected')  
    dataSet[-1].append(ankleDorsiFlexionM)
    ankleInternalRotationM = data.get('JointAnglesAndMoments').get('Right').get('AnkleInternalRotation').get('M_Projected')  
    dataSet[-1].append(ankleInternalRotationM)    
    ankleInversionM = data.get('JointAnglesAndMoments').get('Right').get('AnkleInversion').get('M_Projected')  
    dataSet[-1].append(ankleInversionM)  
    
    hipFlexionM = data.get('JointAnglesAndMoments').get('Right').get('HipFlexion').get('M_Projected')
    dataSet[-1].append(hipFlexionM)      
    hipInternalRotationM = data.get('JointAnglesAndMoments').get('Right').get('HipInternalRotation').get('M_Projected')
    dataSet[-1].append(hipInternalRotationM)      
    hipAbductionM = data.get('JointAnglesAndMoments').get('Right').get('HipAdduction').get('M_Projected')
    dataSet[-1].append(hipAbductionM)      
    
    kneeFlexionM = data.get('JointAnglesAndMoments').get('Right').get('KneeFlexion').get('M_Projected')
    dataSet[-1].append(kneeFlexionM)      
    kneeInternalRotationM = data.get('JointAnglesAndMoments').get('Right').get('KneeInternalRotation').get('M_Projected')
    dataSet[-1].append(kneeInternalRotationM)      
    kneeAdductionM = data.get('JointAnglesAndMoments').get('Right').get('KneeAdduction').get('M_Projected')
    dataSet[-1].append(kneeAdductionM)     
    
## Generate Plots Now
##1 Joint Angles
genPlot(1, 'Ankle Dorsi Flexion', 'Joint Angle (deg)')
genPlot(2, 'Ankle Internal Rotation', 'Joint Angle (deg)')
genPlot(3, 'Ankle Inversion', 'Joint Angle (deg)')
#
genPlot(4, 'Hip Flexion', 'Joint Angle (deg)')
genPlot(5, 'Hip Internal Rotation', 'Joint Angle (deg)')
genPlot(6, 'Hip Abduction', 'Joint Angle (deg)')
#
genPlot(7, 'Knee Flexion', 'Joint Angle (deg)')
genPlot(8, 'Knee Internal Rotation', 'Joint Angle (deg)')
genPlot(9, 'Knee Adduction', 'Joint Angle (deg)')
#
##2 Joint Moments
genPlot(10, 'Hip Moment X', 'Moment (Nm)')
genPlot(11, 'Hip Moment Y', 'Moment (Nm)')
genPlot(12, 'Hip Moment Z', 'Moment (Nm)')
genPlot(13, 'Knee Moment X', 'Moment (Nm)')
genPlot(14, 'Knee Moment Y', 'Moment (Nm)')
genPlot(15, 'Knee Moment Z', 'Moment (Nm)')
genPlot(16, 'Ankle Moment X', 'Moment (Nm)')
genPlot(17, 'Ankle Moment Y', 'Moment (Nm)')
genPlot(18, 'Ankle Moment Z', 'Moment (Nm)')
#
##3 Ground Reaction Forces
genPlot(19, 'Ground Reaction Force X', 'Force (N)')
genPlot(20, 'Ground Reaction Force Y', 'Force (N)')
genPlot(21, 'Ground Reaction Force Z', 'Force (N)')
genPlot(22, 'Ground Reaction Moment X', 'Moment (Nm)')
genPlot(23, 'Ground Reaction Moment Y', 'Moment (Nm)')
genPlot(24, 'Ground Reaction Moment Z', 'Moment (N)')
genPlot(25, 'Center Of Pressure X', 'Pos (m)')
genPlot(26, 'Center Of Pressure Y', 'Pos (m)')
#
##4 Joint Reaction Forces
genPlot(27, 'Knee Force X', 'Force (N)')
genPlot(28, 'Knee Force Y', 'Force (N)')
genPlot(29, 'Knee Force Z', 'Force (N)')
genPlot(30, 'Knee Lateral Force', 'Force (N)')
genPlot(31, 'Knee Medial Force', 'Force (N)')
genPlot(32, 'Ankle Force X', 'Force (N)')
genPlot(33, 'Ankle Force Y', 'Force (N)')
genPlot(34, 'Ankle Force Z', 'Force (N)')
genPlot(35, 'Hip Force X', 'Force (N)')
genPlot(36, 'Hip Force Y', 'Force (N)')
genPlot(37, 'Hip Force Z', 'Force (N)')
#
##5 Muscle Activity
genPlot(38, 'Maximum Muscular Activity', '%MVC')
#
# 6 Joint Moments in ISB coordinate systems
genPlot(39, 'Ankle Dorsi Flexion Moment', 'Joint Moment (Nm)')
genPlot(40, 'Ankle Internal Rotation Moment', 'Joint Moment (Nm)')
genPlot(41, 'Ankle Inversion Moment', 'Joint Moment (Nm)')
#
genPlot(42, 'Hip Flexion Moment', 'Joint Moment (Nm)')
genPlot(43, 'Hip Internal Moment', 'Joint Moment (Nm)')
genPlot(44, 'Hip Abduction Moment', 'Joint Moment (Nm)')
#
genPlot(45, 'Knee Flexion Moment', 'Joint Moment (Nm)')
genPlot(46, 'Knee Internal Rotation Moment', 'Joint Moment (Nm)')
genPlot(47, 'Knee Adduction Moment', 'Joint Moment (Nm)')
#

This snippet took 0.02 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).