Skip to content

Commit

Permalink
revised figures
Browse files Browse the repository at this point in the history
  • Loading branch information
aszorko authored Jun 4, 2023
1 parent 817c520 commit 6c886da
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 28 deletions.
31 changes: 21 additions & 10 deletions CPG/paper2_results1.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ def braindata(file2base,plotmeasures,cpglist=[],plot=False,labels=[],titles=True
brainscores = []
brainheights = []
allfigs = []
allcpgs = []
minheight = 0.75
mpl.style.use('default')
textkw = {'fontdict':{'fontsize':16}}
Expand All @@ -159,8 +160,16 @@ def braindata(file2base,plotmeasures,cpglist=[],plot=False,labels=[],titles=True
continue

brainfile = file2base + str(run) + '_brain' + str(cpg) + '.txt'

f = open(brainfile,'r')
line = f.readline()
cpgarr = line.split(':')[1].replace('[','').replace(']','').split(',')
f.close()
allcpgs.append([int(x) for x in cpgarr])

evodata,_,_ = evoplot.main(brainfile,[11,12,13])
allevodata.append(evodata)


i=0
currscore = np.nan
Expand Down Expand Up @@ -256,12 +265,12 @@ def braindata(file2base,plotmeasures,cpglist=[],plot=False,labels=[],titles=True
#df = pd.DataFrame(data=np.array([brainscores,brainheights,np.sqrt(np.array(alldy)),np.sqrt(np.array(alldz)),allz,allheight,allind]).T,columns=["brainscore","brainheight","diffperiod","diffcorr","corrmax","height","corrind"])
df = pd.DataFrame(data=np.array([brainscores,brainheights,alldy,alldz,allz,allheight,allind]).T,columns=["brainscore","brainheight","diffperiod","diffcorr","corrmax","height","corrind"])
df.corrind[df.corrmax<0.3] = 0
return df, np.array(allevodata), allbrains, allfigs
return df, np.array(allevodata), allbrains, allcpgs, allfigs

if __name__ == "__main__":

#1: plot CPG scatter plots, 2: CPG stats, 3: CPG heat plots, 4: brain stats, 5: brain evolution, 6: get best CPG, 7: CPG evolution, 8: control parameters figure
runmode = [4]
runmode = [2]

mpl.style.use('default')
textkw = {'fontsize':16}
Expand Down Expand Up @@ -408,11 +417,12 @@ def braindata(file2base,plotmeasures,cpglist=[],plot=False,labels=[],titles=True
else:
heatplot = False

df1,ev1,brains1,figs1 = braindata(file2base,measures,cpglist=normalplots,plot=heatplot,labels=['A'])
df1,ev1,brains1,cpgs1,figs1 = braindata(file2base,measures,cpglist=normalplots,plot=heatplot,labels=['A'])
df1["short"] = 0
df2,ev2,brains2,figs2 = braindata(file2base_short,measures,cpglist=shortplots,plot=heatplot,labels=['B'],titles=False)
df2,ev2,brains2,cpgs2,figs2 = braindata(file2base_short,measures,cpglist=shortplots,plot=heatplot,labels=['B'],titles=False)
df2["short"] = 1
braindf = pd.concat([df1,df2])
cpgs1.extend(cpgs2)

if 4 in runmode:

Expand Down Expand Up @@ -517,7 +527,7 @@ def braindata(file2base,plotmeasures,cpglist=[],plot=False,labels=[],titles=True

t = [0,10,12,20,30]
br = [1,0.5,0.5,0.5,1]
tilt = [-0.016,-0.016,0.016,0.016,0.016]
tilt = [-1.44,-1.44,1.44,1.44,1.44]
fig8 = plt.figure()
mpl.style.use('default')
gs = fig8.add_gridspec(3,1, hspace=0.25,wspace=0.25)
Expand All @@ -528,9 +538,9 @@ def braindata(file2base,plotmeasures,cpglist=[],plot=False,labels=[],titles=True
axs[1].set_yticks([0.5,1])
axs[1].set_xlim([0,30])
axs[2].plot(t,tilt)
axs[2].set_ylabel(r"$\theta_C$",**textkw)
axs[2].set_ylabel(r"$\theta_C$ (deg)",**textkw)
axs[2].set_xlabel("Time (s)",**textkw)
axs[2].set_yticks([-0.016,0,0.016])
axs[2].set_yticks([-1.44,0,1.44])
axs[2].set_xlim([0,30])
axs[0].set_ylim([-1,1])
axs[0].set_xlim([0,30])
Expand All @@ -540,9 +550,10 @@ def braindata(file2base,plotmeasures,cpglist=[],plot=False,labels=[],titles=True
axs[0].annotate(text='', xy=(10,-0.9), xytext=(0,-0.9), arrowprops=dict(arrowstyle='<->', color='k', lw=1, shrinkA=0, shrinkB=0))
axs[0].annotate(text='', xy=(20,-0.9), xytext=(10,-0.9), arrowprops=dict(arrowstyle='<->', color='k', lw=1, shrinkA=0, shrinkB=0))
axs[0].annotate(text='', xy=(30,-0.9), xytext=(20,-0.9), arrowprops=dict(arrowstyle='<->', color='k', lw=1, shrinkA=0, shrinkB=0))
axs[0].text(2,-0.8,'$F_1$ (backwards)')
axs[0].text(12.5,-0.8,'$F_2$ (forwards)')
axs[0].text(21.5,-0.8,'$F_3$ (accelerating)')
axs[0].text(2,-0.8,'$F_1$ (back. fast)')
axs[0].text(11.5,-0.8,'$F_2$ (forw. steady)')
axs[0].text(21.5,-0.8,'$F_3$ (forw. fast)')
axs[0].text(12.5,-0.2,'$F_4$ (upright)')
axs[0].axis('off')
plt.tight_layout()

64 changes: 46 additions & 18 deletions CPG/paper2_results2.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,18 @@
from matplotlib import pyplot as plt
from scipy import signal

def convolve(sig,period,dt):
wt = np.arange(-0.5,0.5,dt)
gamma = 0.667 #0.5
wavelet = (np.cos(2*np.pi*wt/period) - np.exp(-0.5/gamma**2))*np.exp(-0.5*(wt*gamma/period)**2)
out = signal.convolve(sig,wavelet,mode='same',method='direct')/sum(wavelet**2)

wind = 2
gamma2 = 2
y = np.exp(-0.5*gamma2**2*np.arange(-wind,wind,dt)**2)

return np.convolve(out**2, y, 'same')


if __name__ == "__main__":
mpl.style.use('ggplot')
Expand All @@ -23,8 +35,8 @@
baseperiod = 8.16 #cpg units

t_arr = [0.8,1.25]


t_ratios = [1.5,1]
legtext = ['Stimulus T x 3/2','Stimulus T']


n_brain = 6
Expand All @@ -33,8 +45,8 @@

dt = 0.16
dt_unity = 0.1
stepsperframe = 12
t_length = 20 #seconds
stepsperframe = 13 #12
t_length = 22 #seconds
nframes = int(t_length / dt_unity)
stimstart = 6 #seconds
stimend = 16 #seconds
Expand Down Expand Up @@ -71,36 +83,52 @@


outpks = []
outsync0 = []
outsync1 = []
inpks,_ = signal.find_peaks(allinput[i],height=0.01)
for k in range(4):
alloutput[i][k,:] = limbamp[k]*alloutput[i][k,:]
times,_ = signal.find_peaks(np.diff(np.real(alloutput[i][k,:])),height=0.05,prominence=0.1)
outpks.append(times)

fig, axs = plt.subplots(4,1)
sync0 = convolve(np.diff(np.real(alloutput[i][k,:])),baseperiod,dt_unity/stepsperframe)
sync1 = convolve(np.diff(np.real(alloutput[i][k,:])),t_ratios[j]*t_arr[j]*baseperiod,dt_unity/stepsperframe)
outsync0.append(sync0)
outsync1.append(sync1)

fig, axs = plt.subplots(5,1)
for k in range(4):
axs[0].plot(t0[outpks[k]],k+0*t0[outpks[k]],'|')
axs[0].plot(t0[inpks],4+0*t0[inpks],'|k')
axs[0].set_xlim([2,t_length])
axs[0].set_xlim([2,t_length-2])
axs[0].set_xticks([])
axs[0].set_yticks([0,1,2,3,4])
#axs[0].set_yticklabels(['RH','LF','RF','LH','Stim'])
axs[0].set_yticklabels(['LH','RH','LF','RF','Stim'])
axs[0].set_ylim([-1,5])
axs[1].plot(t0[1:],2*stepsperframe*UnityInterfaceBrain.sig(2*legamp*np.diff(np.real(alloutput[i]))).squeeze().T)
axs[1].set_xlim([2,t_length])
axs[1].set_xticks([])
axs[1].set_xlim([2,t_length-2])
axs[1].set_xticks([])
axs[1].set_yticks([0,0.2])
axs[1].set_ylabel('Leg output')
axs[2].plot(t1,allheight[i])
axs[2].set_xlim([2,t_length])
axs[2].set_xticks([])
axs[2].set_ylabel('Height')#,labelpad=20)
axs[3].plot(t1,alltilt[i])
axs[3].set_xlim([2,t_length])
axs[3].set_ylabel('Tilt')#,labelpad=17)
axs[3].set_xlabel('Time (s)')
y1 = np.max(np.array(outsync1),0)
y0 = np.max(np.array(outsync0),0)
line1, = axs[2].plot(t0[1:],y1/np.max(y1))
line2, = axs[2].plot(t0[1:],y0/np.max(y0))
axs[2].set_xlim([2,t_length-2])
axs[2].set_xticklabels([])
axs[2].tick_params(axis='x', color='white')
axs[2].set_ylabel('Sync')
axs[2].legend([line1,line2],['Intrinsic T',legtext[j]],fontsize=8,loc='lower left')
axs[3].plot(t1,allheight[i])
axs[3].set_xlim([2,t_length-2])
axs[3].set_xticks([])
axs[3].set_ylabel('Height')#,labelpad=20)
axs[4].plot(t1,alltilt[i])
axs[4].set_xlim([2,t_length-2])
axs[4].set_ylabel('Tilt')#,labelpad=17)
axs[4].set_xlabel('Time (s)')
plt.gcf().text(0.01, 0.85,figlabels[j],**textkw)
plt.show()
fig.savefig('paper2_figures/timeseries_v2' + figlabels[j] + '.eps')
fig.savefig('paper2_figures/timeseries_v3' + figlabels[j] + '.eps')

env.close()

0 comments on commit 6c886da

Please sign in to comment.