Validation_motion.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. #!/usr/bin/env python
  2. # coding: utf-8
  3. database_path = '/media/andrey/My Passport/GIN_new/Anesthesia_CA1/meta_data/meta_recordings_sleep.xlsx'
  4. # ### Select the range of recordings for the analysis (see "Number" row in the meta data file)
  5. # In[4]:
  6. rec = [x for x in range(0,54)]
  7. # In[1]:
  8. import numpy as np
  9. import numpy.ma as ma
  10. import matplotlib.pyplot as plt
  11. import matplotlib.ticker as ticker
  12. import pandas as pd
  13. import seaborn as sns
  14. import pickle
  15. import os
  16. sns.set()
  17. sns.set_style("whitegrid")
  18. from scipy.signal import medfilt
  19. from scipy.stats import skew, kurtosis, zscore
  20. from scipy import signal
  21. from sklearn.linear_model import LinearRegression, TheilSenRegressor
  22. plt.rcParams['figure.figsize'] = [8, 8]
  23. # In[2]:
  24. from capipeline import *
  25. # ### Run the analysis
  26. # /media/andrey/My Passport/GIN/Anesthesia_CA1/validation/calcium_imaging
  27. # It creates a data frame *df_estimators* that contains basic information regarding stability of the recordings, such as
  28. #
  29. # - total number of identified neurons,
  30. # - traces and neuropils median inntensities for each ROI
  31. # - their standard deviation
  32. # - skewness of the signal
  33. # - estamation of their baseline (defined as a bottom quartile of signal intensities)
  34. # - their temporal stability (defined as the ratio between median signals of all ROIs in the first and the second parts of the recording)
  35. # In[5]:
  36. #df_estimators = pd.DataFrame()
  37. motion_index = np.zeros((len(rec),50000),dtype='float')
  38. recording_length = np.zeros((len(rec)),dtype='int')
  39. start_quiet_period = np.zeros((len(rec)),dtype='int')
  40. stop_quiet_period = np.zeros((len(rec)),dtype='int')
  41. for i, r in enumerate(rec):
  42. animal = get_animal_from_recording(r, database_path)
  43. #condition = get_condition(r, database_path)
  44. #print("#" + str(r) + " " + str(animal) + " " + str(condition) + " ")
  45. meta_data = pd.read_excel(database_path)
  46. path_excel_rec = str(meta_data['Folder'][r]) + str(meta_data['Subfolder'][r]) + '/suite2p/'
  47. stat = np.load(path_excel_rec+ '/plane0/ops.npy', allow_pickle=True)
  48. #plt.plot(stat.item(0)['yoff'],alpha=0.5)
  49. #plt.plot(stat.item(0)['xoff'],alpha=0.5)
  50. motion_index[i,:len(stat.item(0)['yoff'])] = np.sqrt(stat.item(0)['yoff']**2 + stat.item(0)['xoff']**2)
  51. recording_length[i] = len(stat.item(0)['yoff'])
  52. print(meta_data['Quiet periods'][r].split(','))
  53. start_quiet_period[i] = int(meta_data['Quiet periods'][r].split(',')[0])
  54. stop_quiet_period[i] = int(meta_data['Quiet periods'][r].split(',')[1])
  55. #np.save("./xy-motion.npy",motion_index)
  56. mi = motion_index
  57. #mi = np.load("./xy-motion.npy")
  58. print(np.max(mi))
  59. print(np.min(mi))
  60. mi_av = np.mean(mi[:,:30000].reshape(mi.shape[0],100,300), axis=2)
  61. plt.rcParams["axes.grid"] = False
  62. plt.figure(figsize = (10,10))
  63. pos = plt.imshow(mi_av,cmap='Purples',vmin = 0, vmax = 10,aspect='equal')
  64. plt.colorbar(pos)
  65. plt.scatter(start_quiet_period[0:max(rec)]/300,np.arange(max(rec)),marker='>',color='k',label='start of quiet period')
  66. plt.scatter(stop_quiet_period[0:max(rec)]/300,np.arange(max(rec)),marker='<',color='k',label='end of quiet period')
  67. plt.scatter(recording_length[0:max(rec)]/300, np.arange(max(rec)) ,marker='|',color='k',label='end of the recording')
  68. plt.legend()
  69. plt.xlabel('x 300 frames')
  70. plt.ylabel('recording')
  71. plt.title('Sleep dataset motion validation')
  72. #plt.gcf().set_facecolor("white")
  73. plt.savefig("Validation_motion.png")
  74. plt.savefig("Validation_motion.svg")
  75. #import plotly.express as px
  76. #import numpy as np
  77. #fig = px.imshow(mi, color_continuous_scale='RdBu_r',zmin = -10, zmax = 10)
  78. #fig.show()
  79. plt.show()