ROI_adjustment_check.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. #!/usr/bin/env python
  2. # coding: utf-8
  3. # ### Define paths and animals for the analysis
  4. # In[1]:
  5. path = '/media/andrey/My Passport/GIN/Anesthesia_CA1/meta_data/meta_recordings_transition_state.xlsx'
  6. path4results = '/media/andrey/My Passport/GIN/Anesthesia_CA1/validation/calcium_imaging_transition_state/' #To store transformation matrix
  7. save_plots_path = '/media/andrey/My Passport/GIN/Anesthesia_CA1/validation/calcium_imaging_transition_state/'
  8. log_file_path = save_plots_path + 'registration_logs.txt'
  9. #animals_for_analysis = [8235,8237,8238]
  10. animals_for_analysis = ['F0','M0','M3']
  11. # ### Align FOV's for all recordings
  12. # In[2]:
  13. repeat_calc = 1
  14. silent_mode = False
  15. #######################
  16. import pandas as pd
  17. import numpy as np
  18. import os
  19. import matplotlib.pyplot as plt
  20. import sys
  21. np.set_printoptions(threshold=sys.maxsize)
  22. from pystackreg import StackReg
  23. # Sobel filter (not used)
  24. #from scipy import ndimage #https://docs.scipy.org/doc/scipy/reference/generated/scipy.ndimage.sobel.html
  25. meta_data = pd.read_excel(path, engine='openpyxl')
  26. #%% compute transformations matrices between recordings
  27. recordings = meta_data['Number']
  28. animals = animals_for_analysis
  29. #print("Recordings: ", recordings)
  30. #ALIGNMENT ALGORITHM
  31. sr = StackReg(StackReg.AFFINE)
  32. # ROI adjustment check for all recordings
  33. from matplotlib import colors
  34. Amap = colors.ListedColormap(['white','tab:blue', 'tab:red','purple'])
  35. '''
  36. if not os.path.exists(save_plots_path + 'ROIAdjustmentCheck/'):
  37. os.makedirs(save_plots_path + 'ROIAdjustmentCheck/')
  38. for animal in animals:
  39. if not os.path.exists(save_plots_path + 'ROIAdjustmentCheck/' + str(animal) + "/"):
  40. os.makedirs(save_plots_path + 'ROIAdjustmentCheck/' + str(animal) + "/")
  41. tmats_loaded = np.load(path4results + 'StackReg/' + str(animal) + "_best_tmats" + '.npy')
  42. meta_animal = meta_data[meta_data['Mouse_all'] == animal]
  43. recordings = meta_animal['Number'] #,'blue', 'green','cyan'])
  44. print(recordings)
  45. images = np.zeros((512, 512, np.shape(meta_animal)[0]))
  46. for idx, recording in enumerate(recordings):
  47. stats = np.load(meta_data['Folder'][recording] +
  48. str(meta_data['Subfolder'][recording]) +
  49. str(int(meta_data['Recording idx'][recording])) +
  50. '/suite2p/plane0/stat.npy',
  51. allow_pickle=True)
  52. iscell = np.load(meta_data['Folder'][recording] +
  53. str(meta_data['Subfolder'][recording]) +
  54. str(int(meta_data['Recording idx'][recording])) +
  55. '/suite2p/plane0/iscell.npy',
  56. allow_pickle=True)
  57. stats = stats[iscell[:, 0].astype(bool)]
  58. for stats_neuron in stats:
  59. images[stats_neuron['ypix'], stats_neuron['xpix'], idx] += 1
  60. images[:,:,idx][images[:,:,idx]>1] = 1
  61. roi_corresp = np.zeros((512, 512))
  62. #for idx0 in range(np.shape(images)[2]):
  63. #for idx1 in range(idx0, np.shape(images)[2]):
  64. for idx0, recording0 in enumerate(recordings):
  65. for idx1, recording1 in enumerate(recordings):
  66. # roi_corresp = images[:,:,idx0] + 2*images[:,:,idx1]
  67. roi_corresp = images[:,:,idx0] + 2*sr.transform(images[:, :, idx1], tmats_loaded[idx0, idx1, :, :])
  68. file_title = str(idx0) + "_" + str(idx1) + "_" + meta_data['Condition'][recording0] + "_" + meta_data['Condition'][recording1]
  69. print(file_title)
  70. plt.imshow(roi_corresp[:,:],cmap=Amap) #'tab10'
  71. #plt.annotate('blue - reference / green - corrected / cyan - intersection', xy=(0, 0), xytext=(.8, 0), fontsize=12)
  72. plt.savefig(save_plots_path + "ROIAdjustmentCheck/" + str(animal) + '/' + file_title + "_ROI_correspondence.svg")
  73. #plt.show()
  74. '''
  75. if not os.path.exists(save_plots_path + 'ROIs/'):
  76. os.makedirs(save_plots_path + 'ROIs/')
  77. for animal in animals:
  78. if not os.path.exists(save_plots_path + 'ROIs/' + str(animal) + "/"):
  79. os.makedirs(save_plots_path + 'ROIs/' + str(animal) + "/")
  80. #tmats_loaded = np.load(path4results + 'StackReg/' + str(animal) + "_best_tmats" + '.npy')
  81. meta_animal = meta_data[meta_data['Mouse_all'] == animal]
  82. recordings = meta_animal['Number'] #,'blue', 'green','cyan'])
  83. print(recordings)
  84. images = np.zeros((512, 512, np.shape(meta_animal)[0]))
  85. for idx, recording in enumerate(recordings):
  86. stats = np.load(meta_data['Folder'][recording] +
  87. str(meta_data['Subfolder'][recording]) +
  88. str(int(meta_data['Recording idx'][recording])) +
  89. '/suite2p/plane0/stat.npy',
  90. allow_pickle=True)
  91. iscell = np.load(meta_data['Folder'][recording] +
  92. str(meta_data['Subfolder'][recording]) +
  93. str(int(meta_data['Recording idx'][recording])) +
  94. '/suite2p/plane0/iscell.npy',
  95. allow_pickle=True)
  96. #stats = stats[iscell[:, 0].astype(bool)]
  97. for stats_neuron in stats:
  98. images[stats_neuron['ypix'], stats_neuron['xpix'], idx] += 1
  99. images[:,:,idx][images[:,:,idx]>1] = 1
  100. roi_corresp = np.zeros((512, 512))
  101. for idx0, recording0 in enumerate(recordings):
  102. #roi_corresp = sr.transform(images[:, :, idx0], tmats_loaded[0, idx0, :, :])
  103. roi_corresp = images[:, :, idx0]
  104. #roi_corresp [roi_corresp > 0.5] = 1
  105. file_title = str(idx0) + "_" + meta_data['Condition'][recording0]
  106. print(file_title)
  107. plt.imshow(roi_corresp[:,:],cmap='binary') #'tab10'
  108. plt.savefig(save_plots_path + "ROIs/" + str(animal) + '/' + file_title + "_ROIs.svg")