Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update model for python3 and modeldb-ci support #4

Merged
merged 3 commits into from
Jul 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
235 changes: 117 additions & 118 deletions analysis.py

Large diffs are not rendered by default.

145 changes: 70 additions & 75 deletions arm.py

Large diffs are not rendered by default.

613 changes: 306 additions & 307 deletions armGraphs.py

Large diffs are not rendered by default.

916 changes: 457 additions & 459 deletions arminterface.py

Large diffs are not rendered by default.

138 changes: 65 additions & 73 deletions dummyArm.py
Original file line number Diff line number Diff line change
@@ -1,68 +1,68 @@
# dummyArm.py -- Python code for interfacing the sim with a virtual arm
#
#
# Last update: 07/21/14 ([email protected])

import matplotlib
import matplotlib
matplotlib.use('TkAgg') # for visualization
from socket import *
import select # for socket reading
import struct # to pack messages for socket comms
from socket import *
import select # for socket reading
import struct # to pack messages for socket comms
import numpy
from pylab import figure, show, ion, pause


# Initialize and setup the sockets and data format
# Initialize and setup the sockets and data format
def setup():
sendMsgFormat = "dd" # messages contain 2 doubles = joint angles of shoulder and elbow
receiveMsgFormat = "dd" # messages contain 2 doubles = velocities
receiveMsgSize = struct.calcsize(receiveMsgFormat)
localHostIP = "127.0.0.1"#"localhost"#"192.168.1.2"# # set IP for local connection

print "Setting up connection..." # setup connection to model
sockReceive = socket(AF_INET, SOCK_DGRAM) # create sockets
hostPortReceive = 31000 # set port for receiving packets
sockReceive.bind(('', hostPortReceive)) # bind to port
sockReceive.setblocking(1) # Set blocking/non-blocking mode
print ("Created UDP socket to receive packets from NEURON model; binded to port %d"%hostPortReceive)
sockSend = socket(AF_INET, SOCK_DGRAM) # connect to socket for sending packets
hostPortSend = 32000 # set port for sending packets
sockSend.connect((localHostIP, hostPortSend))
sockSend.setblocking(1) # Set blocking/non-blocking mode
print ("Created UDP socket to send packets to NEURON model; socket connected to IP %s, port %d" % (localHostIP, hostPortSend))

return sendMsgFormat, receiveMsgFormat, sockReceive, sockSend
sendMsgFormat = "dd" # messages contain 2 doubles = joint angles of shoulder and elbow
receiveMsgFormat = "dd" # messages contain 2 doubles = velocities
receiveMsgSize = struct.calcsize(receiveMsgFormat)
localHostIP = "127.0.0.1"#"localhost"#"192.168.1.2"# # set IP for local connection

print("Setting up connection...") # setup connection to model
sockReceive = socket(AF_INET, SOCK_DGRAM) # create sockets
hostPortReceive = 31000 # set port for receiving packets
sockReceive.bind(('', hostPortReceive)) # bind to port
sockReceive.setblocking(1) # Set blocking/non-blocking mode
print(("Created UDP socket to receive packets from NEURON model; binded to port %d"%hostPortReceive))

sockSend = socket(AF_INET, SOCK_DGRAM) # connect to socket for sending packets
hostPortSend = 32000 # set port for sending packets
sockSend.connect((localHostIP, hostPortSend))
sockSend.setblocking(1) # Set blocking/non-blocking mode
print(("Created UDP socket to send packets to NEURON model; socket connected to IP %s, port %d" % (localHostIP, hostPortSend)))

return sendMsgFormat, receiveMsgFormat, sockReceive, sockSend

# Send and receive packets to/from virtual arm
def sendAndReceivePackets(dataSend, sendMsgFormat, receiveMsgFormat, sockReceive, sockSend):
dataReceived = [0,0]
try:
receiveMsgSize = struct.calcsize(receiveMsgFormat)
dataReceivedPacked = sockReceive.recv(receiveMsgSize) # read packet from socket
if len(dataReceivedPacked) == receiveMsgSize:
dataReceived = struct.unpack(receiveMsgFormat, dataReceivedPacked)
print "Received packet from model: (%.2f,%.2f)" % (dataReceived[0],dataReceived[1])
except:
print "Error receiving packet"

inputready, outputready, e = select.select([] ,[sockSend],[], 0.0) # check if other side ready to receive
if len(outputready)>0:
try:
sent = sockSend.send(struct.pack(sendMsgFormat, dataSend[0], dataSend[1])) # send packet
print "Sent packet to virtual arm: (%.2f, %.2f)" % (dataSend[0], dataSend[1])
except:
print "Sending socket ready but error sending packet"
else:
print "Sending socket not ready"
return dataReceived
dataReceived = [0,0]
try:
receiveMsgSize = struct.calcsize(receiveMsgFormat)
dataReceivedPacked = sockReceive.recv(receiveMsgSize) # read packet from socket
if len(dataReceivedPacked) == receiveMsgSize:
dataReceived = struct.unpack(receiveMsgFormat, dataReceivedPacked)
print("Received packet from model: (%.2f,%.2f)" % (dataReceived[0],dataReceived[1]))
except:
print("Error receiving packet")

inputready, outputready, e = select.select([] ,[sockSend],[], 0.0) # check if other side ready to receive
if len(outputready)>0:
try:
sent = sockSend.send(struct.pack(sendMsgFormat, dataSend[0], dataSend[1])) # send packet
print("Sent packet to virtual arm: (%.2f, %.2f)" % (dataSend[0], dataSend[1]))
except:
print("Sending socket ready but error sending packet")
else:
print("Sending socket not ready")
return dataReceived

# Main code for simple virtual arm
duration = 4 # sec
interval = 0.010 # time between packets (sec)
L1 = 1.0 # arm segment 1 length
L1 = 1.0 # arm segment 1 length
L2 = 0.8 # arm segment 2 length
shang = numpy.pi/2 # shoulder angle (rad)
elang = numpy.pi/2 # elbow angle (rad)
shang = numpy.pi/2 # shoulder angle (rad)
elang = numpy.pi/2 # elbow angle (rad)
shvel = 0 # shoulder velocity (rad/s)
elvel = 0 # elbow velocity (rad/s)
friction = 0.1 # friction coefficient
Expand All @@ -75,29 +75,21 @@ def sendAndReceivePackets(dataSend, sendMsgFormat, receiveMsgFormat, sockReceiv
ax.grid()
line, = ax.plot([], [], 'o-', lw=2)

raw_input("Press Enter to continue...")
input("Press Enter to continue...")
for i in numpy.arange(0, duration, interval):
shang = (shang + shvel * interval) % (2*numpy.pi) # update shoulder angle
elang = (elang + elvel * interval) % (2*numpy.pi)# update elbow angle
if shang<0: shang = 2*numpy.pi + shang
if elang<0: elang = 2*numpy.pi + shang
shpos = [L1*numpy.sin(shang), L1*numpy.cos(shang)] # calculate shoulder x-y pos
elpos = [L2*numpy.sin(shang+elang) + shpos[0], L2*numpy.cos(shang+elang) + shpos[1]] # calculate elbow x-y pos

dataSend = [shang, elang] # set data to send
dataReceived = sendAndReceivePackets(dataSend, sendMsgFormat, receiveMsgFormat, sockReceive, sockSend) # send and receive data
shvel = shvel+dataReceived[0] - (friction * shvel)# update velocities based on incoming commands (accelerations) and friction
elvel = elvel+dataReceived[1] - (friction * elvel)

line.set_data([0, shpos[0], elpos[0]], [0, shpos[1], elpos[1]]) # update line in figure
ax.set_title('Time = %.1f ms, shoulder: pos=%.2f rad, vel=%.2f, acc=%.2f ; elbow: pos = %.2f rad, vel = %.2f, acc=%.2f' % (float(i)*1000, shang, shvel, dataReceived[0] - (friction * shvel), elang, elvel, dataReceived[1] - (friction * elvel) ), fontsize=10)
show()
pause(0.0001) # pause so that the figure refreshes at every time step








shang = (shang + shvel * interval) % (2*numpy.pi) # update shoulder angle
elang = (elang + elvel * interval) % (2*numpy.pi)# update elbow angle
if shang<0: shang = 2*numpy.pi + shang
if elang<0: elang = 2*numpy.pi + shang
shpos = [L1*numpy.sin(shang), L1*numpy.cos(shang)] # calculate shoulder x-y pos
elpos = [L2*numpy.sin(shang+elang) + shpos[0], L2*numpy.cos(shang+elang) + shpos[1]] # calculate elbow x-y pos

dataSend = [shang, elang] # set data to send
dataReceived = sendAndReceivePackets(dataSend, sendMsgFormat, receiveMsgFormat, sockReceive, sockSend) # send and receive data
shvel = shvel+dataReceived[0] - (friction * shvel)# update velocities based on incoming commands (accelerations) and friction
elvel = elvel+dataReceived[1] - (friction * elvel)

line.set_data([0, shpos[0], elpos[0]], [0, shpos[1], elpos[1]]) # update line in figure
ax.set_title('Time = %.1f ms, shoulder: pos=%.2f rad, vel=%.2f, acc=%.2f ; elbow: pos = %.2f rad, vel = %.2f, acc=%.2f' % (float(i)*1000, shang, shvel, dataReceived[0] - (friction * shvel), elang, elvel, dataReceived[1] - (friction * elvel) ), fontsize=10)
show()
pause(0.0001) # pause so that the figure refreshes at every time step
92 changes: 45 additions & 47 deletions error.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,77 +4,77 @@


def getTarget0err(f):
stdin,stdout = os.popen2("tail -n 4 "+f)
stdin.close()
lines = stdout.readlines(); stdout.close()
line = lines[0].split()
return float(line[5])
stdin,stdout = os.popen2("tail -n 4 "+f)
stdin.close()
lines = stdout.readlines(); stdout.close()
line = lines[0].split()
return float(line[5])

def errorFromShell(filestem, readTarget0ErrFromRunFile, maxGens, maxCands, maxTargets):

minVal = 1
minValtarg0 = 1
for igen in range(maxGens):
for icand in range(maxCands):
error = []
for itarget in range(maxTargets):
try:
errfilename = '%s/gen_%s_cand_%d_target_%d_error' % (filestem,igen,icand,itarget)
runfilename = '%s/gen_%s_cand_%d.run' % (filestem,igen,icand)
with open(errfilename, 'r') as f:
error.append(pickle.load(f))
print 'gen=%d, cand=%d, target=%d: error = %f' % (igen, icand, itarget, error[-1])
except:
print('not found file: stem=%s, gen=%d, cand=%d, targ=%d' % (filestem,igen,icand,itarget))
avgErr = pylab.mean(error)
if readTarget0ErrFromRunFile:
try:
target0err = getTarget0err(runfilename)
print "error target 0: %.2f \n" % (target0err)
if target0err < minValtarg0:
minValtarg0 = target0err
minCandtarg0 = icand
minGentarg0 = igen
except:
print "error reading file:", runfilename
for icand in range(maxCands):
error = []
for itarget in range(maxTargets):
try:
errfilename = '%s/gen_%s_cand_%d_target_%d_error' % (filestem,igen,icand,itarget)
runfilename = '%s/gen_%s_cand_%d.run' % (filestem,igen,icand)
with open(errfilename, 'r') as f:
error.append(pickle.load(f))
print('gen=%d, cand=%d, target=%d: error = %f' % (igen, icand, itarget, error[-1]))
except:
print(('not found file: stem=%s, gen=%d, cand=%d, targ=%d' % (filestem,igen,icand,itarget)))
avgErr = pylab.mean(error)
if readTarget0ErrFromRunFile:
try:
target0err = getTarget0err(runfilename)
print("error target 0: %.2f \n" % (target0err))
if target0err < minValtarg0:
minValtarg0 = target0err
minCandtarg0 = icand
minGentarg0 = igen
except:
print("error reading file:", runfilename)

print "avg error: %.2f \n" % (avgErr)
if avgErr < minVal:
minVal = avgErr
minCand = icand
minGen = igen
print("avg error: %.2f \n" % (avgErr))
if avgErr < minVal:
minVal = avgErr
minCand = icand
minGen = igen

print "Min error = %f ; gen = %d ; cand = %d \n" % (minVal, minGen, minCand)
if readTarget0ErrFromRunFile:
print "Min error (target 0) = %f ; gen = %d ; cand = %d \n" % (minValtarg0, minGentarg0, minCandtarg0)
print("Min error = %f ; gen = %d ; cand = %d \n" % (minVal, minGen, minCand))
if readTarget0ErrFromRunFile:
print("Min error (target 0) = %f ; gen = %d ; cand = %d \n" % (minValtarg0, minGentarg0, minCandtarg0))

def errorFromPickle(filestem, maxGens, maxCands):
from collections import OrderedDict
minVals = OrderedDict
minVals = {'error0_pre': [1,0,0], 'error1_pre': [1,0,0], 'error_pre': [1,0,0], 'errord_pre': [1,0,0], \
'error0_post': [1,0,0], 'error1_post': [1,0,0], 'error_post': [1,0,0], 'errord_post': [1,0,0], \
'error0_lesion': [1,0,0], 'error1_lesion': [1,0,0], 'error_pre': [1,0,0], 'errord_tot': [1,0,0], 'error_fitness': [1,0,0]}

for igen in range(maxGens):
for icand in range(maxCands):
error = []
try:
errfilename = '%s/gen_%d_cand_%d_target_0_error' % (filestem,igen,icand)
with open(errfilename, 'r') as f:
error = pickle.load(f)
#print 'gen=%d, cand=%d, errord_tot = %f, error_fitness = %f' % (igen, icand, error['errord_tot'], error['error_fitness'])
for key,val in error.iteritems():
if val < minVals[key][0]:
error = pickle.load(f)
#print 'gen=%d, cand=%d, errord_tot = %f, error_fitness = %f' % (igen, icand, error['errord_tot'], error['error_fitness'])
for key,val in error.items():
if val < minVals[key][0]:
minVals[key][0] = val
minVals[key][1] = igen
minVals[key][2] = icand
except:
print('not found file: stem=%s, gen=%d, cand=%d' % (filestem,igen,icand))
print(('not found file: stem=%s, gen=%d, cand=%d' % (filestem,igen,icand)))

for key,val in minVals.items():
print("%s = %f ; gen = %d ; cand = %d \n" % (key, val[0], val[1], val[2]))


for key,val in minVals.iteritems():
print "%s = %f ; gen = %d ; cand = %d \n" % (key, val[0], val[1], val[2])



# set params
filestem = '../data/15aug29_evolutionStrategy'
Expand All @@ -88,5 +88,3 @@ def errorFromPickle(filestem, maxGens, maxCands):

# call function to obtain errors from pickle file
errorFromPickle(filestem, maxGens, maxCands)


Loading