Figure2_super_compact.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. #!/usr/bin/env python
  2. # coding: utf-8
  3. # ### Link to the file with meta information on recordings
  4. # In[14]:
  5. #import matplotlib.pyplot as plt
  6. #plt.rcParams["figure.figsize"] = (20,3)
  7. database_path = '/media/andrey/My Passport/GIN/Anesthesia_CA1/meta_data/meta_recordings_transition_state.xlsx'
  8. # ### Select the range of recordings for the analysis (see "Number" row in the meta data file)
  9. # In[4]:
  10. #rec = [x for x in range(0,198+1)]
  11. rec = [127,128]
  12. # In[1]:
  13. import numpy as np
  14. import numpy.ma as ma
  15. import matplotlib.pyplot as plt
  16. import matplotlib.ticker as ticker
  17. import pandas as pd
  18. import seaborn as sns
  19. import pickle
  20. import os
  21. sns.set()
  22. sns.set_style("whitegrid")
  23. from scipy.signal import medfilt
  24. from scipy.stats import skew, kurtosis, zscore
  25. from scipy import signal
  26. from sklearn.linear_model import LinearRegression, TheilSenRegressor
  27. plt.rcParams['figure.figsize'] = [16, 8]
  28. color_awake = (0,191/255,255/255)
  29. color_mmf = (245/255,143/255,32/255)
  30. color_keta = (181./255,34./255,48./255)
  31. color_iso = (143./255,39./255,143./255)
  32. color_post = (142/255,226/255,255/255)
  33. custom_palette ={'keta':color_keta, 'iso':color_iso,'mmf':color_mmf,'awake':color_awake,'post':color_post}
  34. # In[2]:
  35. from capipeline import *
  36. df_estimators = pd.read_pickle("./transition_state_calcium_imaging_stability_validation.pkl")
  37. df_estimators['neuronID'] = df_estimators.index
  38. #df_estimators["animal"] = df_estimators["animal"]
  39. print(np.unique(df_estimators["condition"]))
  40. df_estimators["CONDITION"] = df_estimators["condition"]
  41. df_estimators.loc[(df_estimators.condition == 'awake1'),"condition"] = 'awake'
  42. df_estimators.loc[:,"CONDITION"] = 'post'
  43. df_estimators.loc[(df_estimators.condition == 'awake'),"CONDITION"] = 'awake'
  44. df_estimators.loc[(df_estimators.condition == 'iso'),"CONDITION"] = 'iso'
  45. df_estimators.loc[(df_estimators.condition == 'keta'),"CONDITION"] = 'keta'
  46. df_estimators.loc[(df_estimators.condition == 'mmf'),"CONDITION"] = 'mmf'
  47. print(np.unique(df_estimators["CONDITION"]))
  48. #df_estimators["multihue"] = df_estimators["animal"].astype("string") + df_estimators["CONDITION"]
  49. df_estimators["batch"] = (df_estimators["recording"] > 101).astype("int") + 1
  50. #df_estimators["multihue"] = df_estimators["animal"].astype("string") + df_estimators["CONDITION"]
  51. df_estimators["animal"] = df_estimators["animal"].astype("string")
  52. #df_estimators = df_estimators[df_estimators.animal == 'F1']
  53. print(np.unique(df_estimators["animal"].astype("string")))
  54. df_estimators["multihue"] = "batch" + df_estimators["batch"].astype("string") + df_estimators["CONDITION"]
  55. #print(np.unique(df_estimators["batch"]))
  56. print(np.unique(df_estimators["CONDITION"]))
  57. #print()
  58. # ### Plot
  59. # In[9]:
  60. parameters = ['number.neurons','traces.median','traces.skewness','decay','median.stability']
  61. labels = ['Extracted \n ROIs','Median, \n A.U.','Skewness','Decay time, \n s','1st/2nd \n ratio, %']
  62. number_subplots = len(parameters)
  63. recordings_ranges = [[0,198]]
  64. #sns.stripplot(x='multihue', y='number.neurons', data=df_estimators, jitter=True)
  65. #sns.scatterplot(x='multihue', y='number.neurons', data=df_estimators)
  66. #plt.show()
  67. #sns.swarmplot(x='multihue', y='number.neurons', data=df_estimators)
  68. for rmin,rmax in recordings_ranges:
  69. f, axes = plt.subplots(number_subplots, 1, figsize=(1.25, 4.5)) # sharex=Truerex=True
  70. #plt.subplots_adjust(left=None, bottom=0.1, right=None, top=0.9, wspace=None, hspace=0.2)
  71. #f.tight_layout()
  72. sns.despine(left=True)
  73. for i, param in enumerate(parameters):
  74. lw = 0.8
  75. #else:
  76. df_nneurons = df_estimators.groupby(['recording','multihue','CONDITION'], as_index=False)['number.neurons'].median()
  77. if (i == 0):
  78. #sns.stripplot(x='CONDITION', y='number.neurons', data=df_nneurons[(df_nneurons.recording>=rmin)&(df_nneurons.recording<=rmax)], hue = "CONDITION", palette = custom_palette, order = ['awake', 'iso', 'mmf', 'keta', 'post'], ax=axes[i], marker = '.',edgecolor="black", linewidth = lw*0.5) #
  79. sns.swarmplot(x='CONDITION', y='number.neurons', data=df_nneurons[(df_nneurons.recording>=rmin)&(df_nneurons.recording<=rmax)], hue = "CONDITION", palette = custom_palette, order=['awake', 'iso', 'mmf', 'keta', 'post'], ax=axes[i], marker = '.',size=1,edgecolor="black", linewidth = lw*0.5)
  80. else:
  81. sns.boxplot(x='CONDITION', y=param, data=df_estimators[(df_estimators.recording>=rmin)&(df_estimators.recording<=rmax)], hue = "CONDITION", palette = custom_palette, dodge=False, order = ['awake', 'iso', 'mmf','keta', 'post' ]
  82. , showfliers = False,ax=axes[i],linewidth=lw)
  83. # param = "animal"
  84. # print(np.unique(df_estimators[param]))
  85. # axes[i].set_yticks(np.unique(df_estimators[param]))
  86. # sns.swarmplot(x='recording', y=param, data=df_estimators[(df_estimators.recording>=rmin)&(df_estimators.recording<=rmax)&(df_estimators['neuronID'] == 0)],dodge=False, s=1, edgecolor='black', linewidth=1, ax=axes[i])
  87. #ax.set(ylabel="")
  88. if i == 0:
  89. axes[i].set_ylim([0.0,1200.0])
  90. if i > 0:
  91. axes[i].set_ylim([-100.0,1500.0])
  92. if i > 1:
  93. axes[i].set_ylim([-3.0,15.0])
  94. if i > 2:
  95. axes[i].set_ylim([-0.5,1.5])
  96. if i > 3:
  97. axes[i].set_ylim([90,110])
  98. axes[i].get_xaxis().set_visible(True)
  99. else:
  100. axes[i].get_xaxis().set_visible(False)
  101. #if i < number_subplots-1:
  102. axes[i].xaxis.label.set_visible(False)
  103. #if i==0:
  104. # axes[i].set_title("Validation: stability check (recordings #%d-#%d)" % (rmin,rmax), fontsize=9, pad=30) #45
  105. axes[i].set_ylabel(labels[i], fontsize=9,labelpad=5) #40
  106. #axes[i].set_xlabel("Recording", fontsize=5,labelpad=5) #40
  107. #axes[i].axis('off')
  108. axes[i].xaxis.set_tick_params(labelsize=9) #35
  109. axes[i].yaxis.set_tick_params(labelsize=9) #30
  110. axes[i].get_legend().remove()
  111. #axes[i].xaxis.set_major_locator(ticker.MultipleLocator(10))
  112. #axes[i].xaxis.set_major_formatter(ticker.ScalarFormatter())
  113. plt.xticks(rotation=90)
  114. #plt.legend(bbox_to_anchor=(1.01, 1), loc=2, borderaxespad=0.,fontsize=25)
  115. plt.savefig("Validation_stability_check_figure2_super_compact.png",dpi=400)
  116. plt.savefig("Validation_stability_check_figure2_super_compact.svg")
  117. #plt.show()
  118. # In[13]:
  119. sns.displot(data=df_estimators, x="median.stability", kind="kde", hue = "animal")
  120. plt.xlim([80,120])
  121. plt.xlabel("Stability, %", fontsize = 15)
  122. plt.title("Validation: summary on stability (recordings #%d-#%d)" % (min(rec),max(rec)), fontsize = 20, pad=20)
  123. plt.grid(False)
  124. plt.savefig("Validation_summary_stability_recordings_#%d-#%d)" % (min(rec),max(rec)))
  125. #plt.show()
  126. # In[62]:
  127. sns.displot(data=df_estimators, x="number.neurons", kind="kde", hue = "multihue")
  128. #plt.xlim([80,120])
  129. plt.xlabel("Number of neurons", fontsize = 15)
  130. plt.grid(False)
  131. plt.savefig("Number of neurons multihue.png")
  132. #plt.show()