ipos.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. """Figure 1S2 and 5S1 eye position plots, use run -i ipos.py"""
  2. index = pd.Index([], name='mseu') # init as empty
  3. iposmi = pd.DataFrame(index=index, columns=['stimtype', 'iposfmi', 'relfmi', 'iposrmi',
  4. 'relrmi', 'areafmi', 'meanratefmi',
  5. 'meanburstratiofmi'])
  6. maxiposedge, iposbinw = 10, 0.5
  7. iposedges = np.arange(0, maxiposedge+iposbinw, iposbinw)
  8. """Figure 1S2"""
  9. ## plot cross trial stdev PDFs for control and opto trials:
  10. for stimtype, stimtypei in zip(STIMTYPESPLUSALL, STIMTYPESPLUSALLI):
  11. f, a = plt.subplots(figsize=DEFAULTFIGURESIZE)
  12. wintitle('ipos_opto %s stdev cross trial PDF' % stimtype)
  13. stdctrl = ipos_opto['std_xpos_cross'][:, stimtypei, False]
  14. stdopto = ipos_opto['std_xpos_cross'][:, stimtypei, True]
  15. a.hist(stdctrl, bins=iposedges, histtype='step', lw=1.5,
  16. color=opto2clr[False], alpha=1, label=opto2fb[False])
  17. a.hist(stdopto, bins=iposedges, histtype='step', lw=1.5,
  18. color=opto2clr[True], alpha=1, label=opto2fb[True])
  19. a.set_xlabel('Eye position $\sigma$ ($\mathregular{\degree}$)')
  20. a.set_ylabel('Experiment count')
  21. #a.legend(frameon=False)
  22. print('opto %s std_cross:' % stimtype, wilcoxon(stdctrl, stdopto))
  23. ## plot cross trial stdev CDFs for control and opto trials:
  24. for stimtype, stimtypei in zip(STIMTYPESPLUSALL, STIMTYPESPLUSALLI):
  25. f, a = plt.subplots(figsize=DEFAULTFIGURESIZE)
  26. wintitle('ipos_opto %s stdev cross trial CDF' % stimtype)
  27. stdctrl = ipos_opto['std_xpos_cross'][:, stimtypei, False]
  28. stdopto = ipos_opto['std_xpos_cross'][:, stimtypei, True]
  29. stdctrlbins = list(np.unique(stdctrl)) + [maxiposedge]
  30. stdoptobins = list(np.unique(stdopto)) + [maxiposedge]
  31. a.hist(stdctrl, bins=stdctrlbins, density=True, cumulative=True, histtype='step',
  32. lw=1.5, color=opto2clr[False], alpha=1, label=opto2fb[False])
  33. a.hist(stdopto, bins=stdoptobins, density=True, cumulative=True, histtype='step',
  34. lw=1.5, color=opto2clr[True], alpha=1, label=opto2fb[True])
  35. a.set_xlabel('Eye position $\sigma$ ($\mathregular{\degree}$)')
  36. a.set_ylabel('Cumulative probability')
  37. a.set_xlim(0, 9.5)
  38. a.set_xticks([0, 5])
  39. a.set_yticks([0, 0.5, 1])
  40. #a.legend(frameon=False)
  41. _, KS_p = ks_2samp(stdctrl, stdopto)
  42. #_, MW_p = scipy.stats.mannwhitneyu(stdrun, stdsit)
  43. txt = '$\mathregular{p_{KS}=%.2g}$' % KS_p # prob that distribs are the same
  44. a.add_artist(AnchoredText(txt, loc='upper left', frameon=False))
  45. ## scatter plot movie reliability FMI vs eye position stdev FMI:
  46. ## TODO: this correlation is done with FMI calculated from run trials. Is this optimal?
  47. # run reliability FMI for each movie mseu:
  48. mseus, relfmis = zip(*mviFMI['rel'][:, 'run'].items()) # unpack indices and relfmi values
  49. mseus, relfmis = np.array(mseus), np.array(relfmis)
  50. # across-trial eye position stdev FMI for all movie experiments:
  51. mseiposfmis = ((ipos_opto['std_xpos_cross'][:, 'mvi', False]
  52. - ipos_opto['std_xpos_cross'][:, 'mvi', True]) /
  53. (ipos_opto['std_xpos_cross'][:, 'mvi', False]
  54. + ipos_opto['std_xpos_cross'][:, 'mvi', True]))
  55. # get mse string of every mseu in mviFMI:
  56. mses = np.array(['_'.join(mseu.split('_')[:-1]) for mseu in mseus])
  57. # assign appropriate iposfmi to each mseu:
  58. iposfmis = np.tile(np.nan, len(mseus)) # init to nans
  59. for msei, mse in enumerate(mses):
  60. if mse in mseiposfmis.index:
  61. iposfmis[msei] = mseiposfmis[mse]
  62. # boolean indicating entries with non-NaN rel FMI & ipos FMI:
  63. valid = ~np.isnan(iposfmis) & ~np.isnan(relfmis)
  64. # scatter plot:
  65. figsize = DEFAULTFIGURESIZE[0]*1.05, DEFAULTFIGURESIZE[1] # tweak to make space for labels
  66. f, a = plt.subplots(figsize=figsize)
  67. wintitle('rel FMI vs ipos stdev FMI')
  68. a.scatter(iposfmis[valid], relfmis[valid], clip_on=False,
  69. marker='.', c='None', edgecolor='black', s=DEFSZ)
  70. # plot regression:
  71. fname = os.path.join('stats', 'figure_1S2i_coefs.csv')
  72. try:
  73. df = pd.read_csv(fname)
  74. foundregression = True
  75. except FileNotFoundError:
  76. print('Missing file: %s' % fname)
  77. foundregression = False
  78. if foundregression:
  79. mm = df['slope'][0]
  80. b = df['intercept'][0]
  81. x = np.array([iposfmis[valid].min(), iposfmis[valid].max()])
  82. y = mm * x + b
  83. a.plot(x, y, '-', color='red') # plot linregress fit
  84. a.set_ylabel("Reliability FMI")
  85. a.set_xlabel("Eye position $\sigma$ FMI")
  86. xmin, xmax = -0.15, 0.15
  87. ymin, ymax = -1, 1
  88. a.set_xlim(xmin, xmax)
  89. a.set_ylim(ymin, ymax)
  90. a.set_xticks([xmin, 0, xmax])
  91. a.set_yticks([ymin, 0, ymax])
  92. a.spines['left'].set_position(('outward', 4))
  93. a.spines['bottom'].set_position(('outward', 4))
  94. #txt = ('$\mathregular{p=%.2g}$\n' # prob that slope is 0
  95. # '$\mathregular{R^{2}=%.2g}$\n'
  96. # '$\mathregular{slope=%.2g}$' % (p, rsq, mm))
  97. #a.add_artist(AnchoredText(txt, loc='upper left', frameon=False))
  98. # save to iposmi dataframe:
  99. for mseu, iposfmi, relfmi in zip(mseus, iposfmis, relfmis):
  100. iposmi.loc[mseu, 'stimtype'] = 'mvi'
  101. iposmi.loc[mseu, 'iposfmi'] = iposfmi
  102. iposmi.loc[mseu, 'relfmi'] = relfmi
  103. """Figure 5S1"""
  104. ## plot eye position stdev distributions:
  105. ## plot cross trial stdev PDFs for run and sit trials:
  106. for stimtype, stimtypei in zip(STIMTYPESPLUSALL, STIMTYPESPLUSALLI):
  107. f, a = plt.subplots(figsize=DEFAULTFIGURESIZE)
  108. wintitle('ipos_st8 %s stdev cross trial PDF' % stimtype)
  109. stdrun = ipos_st8['std_xpos_cross'][:, stimtypei, 'run']
  110. stdsit = ipos_st8['std_xpos_cross'][:, stimtypei, 'sit']
  111. a.hist(stdrun, bins=iposedges, histtype='step', lw=1.5,
  112. color=st82clr['run'], alpha=1, label='Run')
  113. a.hist(stdsit, bins=iposedges, histtype='step', lw=1.5,
  114. color=st82clr['sit'], alpha=1, label='Sit')
  115. a.set_xlabel('Eye position $\sigma$ ($\mathregular{\degree}$)')
  116. a.set_ylabel('Experiment count')
  117. #a.legend(frameon=False)
  118. print('st8 %s std_cross:' % stimtype, wilcoxon(stdrun, stdsit))
  119. ## plot cross trial stdev CDFs for run and sit trials:
  120. for stimtype, stimtypei in zip(STIMTYPESPLUSALL, STIMTYPESPLUSALLI):
  121. f, a = plt.subplots(figsize=DEFAULTFIGURESIZE)
  122. wintitle('ipos_st8 %s stdev cross trial CDF' % stimtype)
  123. stdrun = ipos_st8['std_xpos_cross'][:, stimtypei, 'run']
  124. stdsit = ipos_st8['std_xpos_cross'][:, stimtypei, 'sit']
  125. stdrunbins = list(np.unique(stdrun)) + [maxiposedge]
  126. stdsitbins = list(np.unique(stdsit)) + [maxiposedge]
  127. a.hist(stdrun, bins=stdrunbins, density=True, cumulative=True, histtype='step',
  128. lw=1.5, color=st82clr['run'], alpha=1, label='Run')
  129. a.hist(stdsit, bins=stdsitbins, density=True, cumulative=True, histtype='step',
  130. lw=1.5, color=st82clr['sit'], alpha=1, label='Sit')
  131. a.set_xlabel('Eye position $\sigma$ ($\mathregular{\degree}$)')
  132. a.set_ylabel('Cumulative probability')
  133. a.set_xlim(0, 8)
  134. a.set_xticks([0, 5])
  135. a.set_yticks([0, 0.5, 1])
  136. #a.legend(frameon=False)
  137. _, KS_p = ks_2samp(stdrun, stdsit)
  138. #_, MW_p = scipy.stats.mannwhitneyu(stdrun, stdsit)
  139. txt = '$\mathregular{p_{KS}=%.2g}$' % KS_p # prob that distribs are the same
  140. a.add_artist(AnchoredText(txt, loc='upper left', frameon=False))
  141. '''
  142. ## plot within trial stdev PDFs for run and sit trials:
  143. f, a = plt.subplots(figsize=DEFAULTFIGURESIZE)
  144. wintitle('ipos_st8 stdev within trial PDF')
  145. stdrun = ipos_st8['std_xpos_within'][:, 'run']
  146. stdsit = ipos_st8['std_xpos_within'][:, 'sit']
  147. a.hist(stdrun, bins=iposedges, histtype='step', lw=1.5,
  148. color=st82clr['run'], alpha=1, label='Run')
  149. a.hist(stdsit, bins=iposedges, histtype='step', lw=1.5,
  150. color=st82clr['sit'], alpha=1, label='Sit')
  151. a.set_xlabel('Eye position $\sigma$ ($\mathregular{\degree}$)')
  152. a.set_ylabel('Experiment count')
  153. #a.legend(frameon=False)
  154. print('st8 std_within:', wilcoxon(stdrun, stdsit))
  155. '''
  156. ## scatter plot movie reliability RMI vs eye position stdev FMI:
  157. # feedback reliability RMI for each movie mseu:
  158. mseus, relrmis = zip(*mviRMI['rel'][:, False].items()) # unpack indices and relrmi values
  159. mseus, relrmis = np.array(mseus), np.array(relrmis)
  160. # across-trial eye position stdev RMI for each experiment, 27 vals, 1 per exp:
  161. mseiposrmis = ((ipos_st8['std_xpos_cross'][:, 'mvi', 'run']
  162. - ipos_st8['std_xpos_cross'][:, 'mvi', 'sit']) /
  163. (ipos_st8['std_xpos_cross'][:, 'mvi', 'run']
  164. + ipos_st8['std_xpos_cross'][:, 'mvi', 'sit']))
  165. # get mse string of every mseu in mviRMI:
  166. mses = np.array(['_'.join(mseu.split('_')[:-1]) for mseu in mseus])
  167. # assign appropriate iposrmi to each mseu:
  168. iposrmis = np.tile(np.nan, len(mseus)) # init to nans
  169. for msei, mse in enumerate(mses):
  170. if mse in mseiposrmis.index:
  171. iposrmis[msei] = mseiposrmis[mse]
  172. # boolean indicating entries with non-NaN rel RMI & ipos RMI:
  173. valid = ~np.isnan(iposrmis) & ~np.isnan(relrmis)
  174. # scatter plot:
  175. figsize = DEFAULTFIGURESIZE[0]*1.05, DEFAULTFIGURESIZE[1] # tweak to make space for labels
  176. f, a = plt.subplots(figsize=figsize)
  177. wintitle('rel RMI vs ipos stdev RMI')
  178. a.scatter(iposrmis[valid], relrmis[valid], clip_on=False,
  179. marker='.', c='None', edgecolor='black', s=DEFSZ)
  180. # plot regression:
  181. fname = os.path.join('stats', 'figure_5S1i_coefs.csv')
  182. try:
  183. df = pd.read_csv(fname)
  184. foundregression = True
  185. except FileNotFoundError:
  186. print('Missing file: %s' % fname)
  187. foundregression = False
  188. if foundregression:
  189. mm = df['slope'][0]
  190. b = df['intercept'][0]
  191. x = np.array([iposrmis[valid].min(), iposrmis[valid].max()])
  192. y = mm * x + b
  193. a.plot(x, y, '-', color='red') # plot linregress fit
  194. a.set_ylabel("Reliability RMI")
  195. a.set_xlabel("Eye position $\sigma$ RMI")
  196. ymin, ymax = -1, 1
  197. #a.set_xlim(-0.1, 0.6)
  198. a.set_ylim(ymin, ymax)
  199. a.set_xticks([0, 0.5])
  200. a.set_yticks([ymin, 0, ymax])
  201. a.spines['left'].set_position(('outward', 4))
  202. a.spines['bottom'].set_position(('outward', 4))
  203. #txt = ('$\mathregular{p=%.2g}$\n' # prob that slope is 0
  204. # '$\mathregular{R^{2}=%.2g}$\n'
  205. # '$\mathregular{slope=%.2g}$' % (p, rsq, mm))
  206. #a.add_artist(AnchoredText(txt, loc='upper left', frameon=False))
  207. # save to iposmi dataframe:
  208. for mseu, iposrmi, relrmi in zip(mseus, iposrmis, relrmis):
  209. iposmi.loc[mseu, 'stimtype'] = 'mvi' # automatically adds a new mseu row if needed
  210. iposmi.loc[mseu, 'iposrmi'] = iposrmi
  211. iposmi.loc[mseu, 'relrmi'] = relrmi