f_plots.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. import matplotlib
  2. from matplotlib import rcParams
  3. matplotlib.rcParams['text.usetex'] = True
  4. import matplotlib.pyplot as plt
  5. import pylab as pl
  6. from copy import copy
  7. ### Format for figures
  8. import json
  9. P= json.load(open("cfg/PLOSmpl.json")) # import plot parameter
  10. matplotlib.rcParams.update([(j,k) for (j,k) in P.items()
  11. if j in matplotlib.rcParams.keys()])
  12. matplotlib.rcParams['lines.linewidth']=0.065
  13. fig_wide=matplotlib.rcParams["figure.figsize"][0]
  14. fig_height=matplotlib.rcParams["figure.figsize"][1]
  15. ### includes scale bar in the figures
  16. import matplotlib.offsetbox
  17. from matplotlib.lines import Line2D
  18. class AnchoredHScaleBarY(matplotlib.offsetbox.AnchoredOffsetbox):
  19. """ size: length of bar in data units
  20. extent : height of bar ends in axes units """
  21. def __init__(self, size=1, extent = 0.03, label="", loc=2, ax=None,
  22. pad=0.4, borderpad=0.5, ppad = 0, sep=2, prop=None,
  23. frameon=True, linekw={}, **kwargs):
  24. if not ax:
  25. ax = plt.gca()
  26. trans = ax.get_yaxis_transform()
  27. size_bar = matplotlib.offsetbox.AuxTransformBox(trans)
  28. line = Line2D([0,0],[0,size], **linekw)
  29. size_bar.add_artist(line)
  30. txt = matplotlib.offsetbox.TextArea(label, minimumdescent=False,textprops={'fontsize':matplotlib.rcParams['ytick.labelsize']})
  31. self.vpac = matplotlib.offsetbox.HPacker(children=[size_bar,txt],
  32. align="center", pad=ppad, sep=sep)
  33. matplotlib.offsetbox.AnchoredOffsetbox.__init__(self, loc, pad=pad,
  34. borderpad=borderpad, child=self.vpac, prop=prop, frameon=frameon,
  35. **kwargs)
  36. ### Format for figures
  37. #######################################################################
  38. ########################## Creating nice figure
  39. def fancy_fig_4_stimulation(sim,title=[],check_fit=False,add_scale=[],fwidealt=0,popt=[],tad=[]):
  40. t_v=[float(i_n) for i_n in range(0,92)]
  41. lt=int(len(sim.a_Results.t))
  42. if fwidealt==0:
  43. f1 = plt.figure(facecolor="1",figsize=(fig_wide,fig_height*0.7))
  44. else:
  45. f1 = plt.figure(facecolor="1",figsize=(fwidealt,fig_height*0.7))
  46. ax1 = plt.subplot2grid((5,1), (0, 0), colspan=2,rowspan=1)
  47. axp = plt.subplot2grid((5,1), (1, 0), colspan=2,rowspan=4,sharex=ax1)
  48. ##### Plot Input
  49. ### Python 3 doesn't work with this...
  50. ax1.plot(sim.a_Results.t[::20]/1000.0, sim.c_neuron.noisy_current[::20],color="black")
  51. ax1.set_ylim([min(sim.c_neuron.noisy_current)-0.01,max(sim.c_neuron.noisy_current)+0.45])
  52. ax1.get_xaxis().set_visible(False)
  53. ax1.get_yaxis().set_visible(False)
  54. #ax2.set_ylabel("Input",color="white",labelpad=10)
  55. ax1.set_ylabel(r'I [uA]',labelpad=10)
  56. ax1.set_title('input', loc='left')
  57. locatory1 = MaxNLocator(nbins=2) # with 3 bins you will have 4 ticks
  58. ax1.yaxis.set_major_locator(locatory1)
  59. ax1.spines['bottom'].set_color('white')
  60. ax1.spines['top'].set_color('white')
  61. ax1.spines['left'].set_color('white')
  62. ax1.spines['right'].set_color('white')
  63. if len(add_scale)<1:
  64. add_scale=[0.2,'0.2nA']
  65. box1 =ax1.get_position()
  66. xxpos=1.12
  67. yypos=0.7
  68. ob = AnchoredHScaleBarY(ax=ax1,size=add_scale[0], label=add_scale[1],bbox_to_anchor=(xxpos,yypos), bbox_transform=ax1.transAxes, loc="upper right", frameon=False,
  69. pad=0.6,sep=4, linekw=dict(color="k", linewidth=0.8))
  70. ax1.add_artist(ob)
  71. for t in ax1.xaxis.get_ticklines(): t.set_color('white')
  72. #### Plot complete trace
  73. axp.plot(sim.a_Results.t[0:lt]/1000.0,sim.a_Results.V[0:lt],color=[0,0,0])
  74. # ### show fit
  75. # sim_exp=sim
  76. # v_t=sim.a_Results.t
  77. # sp_ampl,sp_V_max,sp_V_min=extract_spike_ampl(sim_exp.a_Results.V, sim_exp.fr[2][0:-1])
  78. # tad, popt,tpeak,perr=doub_expon_fit_2(v_t[sim_exp.fr[2][0:-1]],sp_V_max,std_error=True)
  79. # mqe=mean_squared_error(sp_V_max[len(sp_V_max)-len(doub_expon_fun_2(tad,*popt)):], doub_expon_fun_2(tad,*popt))
  80. # # f1,axp=fancy_fig_4_stimulation(sim,title='40Hz stim '+s_Cell+' msqe='+str(mqe),check_fit=True)
  81. # c=0
  82. # while c < 4 and mqe>1.5:
  83. # print('Trying improvement!')
  84. # poptx=popt
  85. # # poptx[0]=popt[0]/10
  86. # tad, popt,tpeak,perr=doub_expon_fit_2(v_t[sim_exp.fr[2][0:-1]],sp_V_max,p=poptx,std_error=True)
  87. # mqe=mean_squared_error(sp_V_max[len(sp_V_max)-len(doub_expon_fun_2(tad,*popt)):], doub_expon_fun_2(tad,*popt))
  88. # c+=1
  89. if len(popt)!=0:
  90. sim=sim_exp
  91. sp_ampl,sp_V_max,sp_V_min=extract_spike_ampl(sim_exp.a_Results.V, sim_exp.fr[2][0:-1])
  92. tad, popt,tpeak,perr=doub_expon_fit_2(v_t[sim_exp.fr[2][0:-1]],sp_V_max,std_error=True)
  93. mqe=mean_squared_error(sp_V_max[len(sp_V_max)-len(doub_expon_fun_2(tad,*popt)):], doub_expon_fun_2(tad,*popt))
  94. # f1,axp=fancy_fig_4_stimulation(sim,title='40Hz stim '+s_Cell+' msqe='+str(mqe),check_fit=True)
  95. c=0
  96. while c < 4 and mqe>1.5:
  97. print('Trying improvement!')
  98. poptx=popt
  99. # poptx[0]=popt[0]/10
  100. tad, popt,tpeak,perr=doub_expon_fit_2(v_t[sim_exp.fr[2][0:-1]],sp_V_max,p=poptx,std_error=True)
  101. mqe=mean_squared_error(sp_V_max[len(sp_V_max)-len(doub_expon_fun_2(tad,*popt)):], doub_expon_fun_2(tad,*popt))
  102. c+=1
  103. axp.plot(tad/1000.0,doub_expon_fun_2(tad,*popt),'--',linewidth=2,color='gray')
  104. axp.set_ylabel(r'V (mV)',labelpad=5)
  105. axp.set_xlim([-10/1000.0,max(sim.a_Results.t[0:lt])/1000.0])
  106. axp.set_ylim([-100,80])
  107. axp.set_xlabel('time (sec)',labelpad=5)
  108. locatory11 = MaxNLocator(nbins=3) # with 3 bins you will have 4 ticks
  109. axp.yaxis.set_major_locator(locatory11)
  110. #f1.subplots_adjust(bottom=0.17)
  111. if title ==[]:
  112. pass
  113. else:
  114. f1.suptitle(title)
  115. if check_fit:
  116. return f1,axp
  117. else:
  118. return f1