Guest User

Untitled

a guest
Jan 23rd, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.77 KB | None | 0 0
  1. # coding: utf-8
  2.  
  3. # I used this notebook to establish the routines ...
  4.  
  5. # ## Read output from BNT (.*mat*) and write structure into database
  6. from __future__ import print_function # Python 2.x
  7.  
  8. import sys
  9. import os
  10. import time
  11. from villus_helpers.BNT_helpers import *
  12.  
  13. # base params:
  14. BNT_mat = r'N:\horsto\klusta_pipeline\villus\EXPORT\auto_input_file_vars.mat'
  15. BNT_mat = "/".join(BNT_mat.split("\\"))
  16.  
  17. if not os.path.isfile(BNT_mat):
  18. print('Not a file! Trying to convert to linux compatible format...\n')
  19. # change to linux compatible format ...
  20. BNT_mat = "\\mnt\\N" + "".join(BNT_mat.split("N:"))
  21. BNT_mat = "/".join(BNT_mat.split('\\'))
  22.  
  23. if not os.path.isfile(BNT_mat):
  24. print('Not a file: {}\n'.format(BNT_mat))
  25. sys.exit()
  26.  
  27. import argparse
  28. import json
  29. import numpy as np
  30. import math
  31. import pandas as pd
  32. import psycopg2
  33. import cPickle
  34.  
  35. import sys
  36. import os
  37. from fnmatch import fnmatch
  38. import h5py
  39.  
  40. from scipy.io import *
  41.  
  42. from tqdm import tqdm
  43.  
  44.  
  45. from villus_helpers.general import *
  46. from villus_helpers.psql_start import *
  47. from villus_helpers.write2tables import *
  48. # register pickle type to retrieve binary data from database
  49. psycopg2.extensions.register_type(
  50. psycopg2.extensions.new_type(
  51. psycopg2.BINARY.values, 'BINARY-PICKLE', cast_pickle))
  52.  
  53.  
  54. # Get current path:
  55. script_path = os.path.dirname(sys.argv[0])
  56. script_path = "/".join(script_path.split("\\"))
  57. # get parameters to connect to database:
  58. params = config(script_path,'postgresql','hotte')
  59. db_status = test_connect(params)
  60. if db_status == False:
  61. print('\n\n\n\nCould not establish database link. Skipping all psql connects.\n\n\n\n')
  62.  
  63.  
  64. # check time when file was created / modified (in database_helpers.BNT_helpers):
  65. status_mat_file = False
  66. while not status_mat_file:
  67. size_mod = os.path.getsize(BNT_mat)
  68. time.sleep(10) # wait 30 seconds...
  69. size_mod_new = os.path.getsize(BNT_mat)
  70. if size_mod == size_mod_new:
  71. print('File size did not change - loading file!')
  72. status_mat_file = True
  73. else:
  74. print('File size has changed ... waiting another 30 seconds ...')
  75. status_mat_file = False
  76.  
  77. # read .mat
  78. BNT_mat_dict = loadmat(BNT_mat,struct_as_record=True)
  79.  
  80.  
  81. all_purged_screen = []
  82. all_purged_session = []
  83. all_purged_spikes = []
  84.  
  85. print('Purging tetrodes and sessions from database ...')
  86. for no in tqdm(xrange(len(BNT_mat_dict['allStatistics'][0]))):
  87.  
  88. #print('\n\nAnalyzing {}/{} (File {})'.format(no+1,len(BNT_mat_dict['allStatistics'][0])," - ".join(BNT_mat.split("/")[-2:])))
  89. path_name = str(BNT_mat_dict['allStatistics'][0][no]['Path'][0])
  90. path_name = "/".join(path_name.split("\\"))
  91. if not os.path.isdir(path_name):
  92. # change to linux compatible format ...
  93. if 'N:' in path_name:
  94. path_name = "\\mnt\\N" + "".join(path_name.split("N:"))
  95. path_name = "/".join(path_name.split('\\'))
  96. elif 'L:' in path_name:
  97. path_name = "\\mnt\\L" + "".join(path_name.split("L:"))
  98. path_name = "/".join(path_name.split('\\'))
  99. else:
  100. print('Sorry, drive not found - check folder name constructor ({})'.format(path_name))
  101. sys.exit()
  102. session_name = str(BNT_mat_dict['allStatistics'][0][no]['Basename'][0])
  103. tetrode_no = str(BNT_mat_dict['allStatistics'][0][no]['Tetrode'][0][0])
  104. cluster_no = str(BNT_mat_dict['allStatistics'][0][no]['Cell'][0][0])
  105. animal_id,n_drive_user,status,session_ts,analysis_ts = distill_params_root_table_BNT(path_name,session_name)
  106.  
  107. # Purge from tetrodes_tb_bnt and sessions_tb_bnt
  108. # For tetrodes_tb_bnt I need:
  109. # (tetrode_no, n_drive_user, animal_id, session_ts)
  110.  
  111. # for sessions_tb_bnt I need:
  112. # (session_name, n_drive_user, animal_id, session_ts)
  113.  
  114. deleted_screen,deleted_session,deleted_spikes = purge_entries_bnt(animal_id,n_drive_user,session_ts,tetrode_no,session_name,params)
  115. #print(purged_t,purged_s)
  116. if deleted_screen: all_purged_screen.append(deleted_screen[0])
  117. if deleted_spikes: all_purged_spikes.append(deleted_spikes[0])
  118. if deleted_session: all_purged_session.append(deleted_session[0])
  119.  
  120. print('Done. ({} Screen(s), {}Spike(s) and {} Session(s))'.format(len(all_purged_screen),len(all_purged_spikes),len(all_purged_session)))
  121.  
  122.  
  123. # ### Prepare writing to database ...
  124.  
  125. all_scores_labels = {'informationContentWholeRecording':'InformationContent_bnt','hdPeakRateWholeRecording':'tc_stats_hd_peakrate_bnt',
  126. 'gridStats_OrientationWholeRecording':'gridstats_orientation_bnt','numFieldsWholeRecording':'numFields_bnt',
  127. 'meanDirectionWholeRecording':'tc_stats_mean_direction_bnt','meanRateOutsideFieldsWholeRecording':'meanrate_outside_fields_bnt',
  128. 'gridScoreWholeRecording':'grid_score_bnt','peakRateWholeRecording':'peak_rate_bnt',
  129. 'gridStats_SpacingWholeRecording':'gridstats_spacing_bnt','borderScoreWholeRecording':'borderscore_bnt',
  130. 'informationRateWholeRecording':'InformationRate_bnt','speedScore':'speedscore_bnt',
  131. 'peakDirectionWholeRecording':'tc_stats_peakdirection_bnt','mvlWholeRecording':'tc_stats_mvl_bnt',
  132. 'coherenceWholeRecording':'coherence_bnt','gridStats_EllipseWholeRecording':'gridstats_ellipse_bnt',
  133. 'fieldMainWholeRecording':'fieldmain_bnt','sparsityWholeRecording':'sparsity_bnt',
  134. 'gridStats_EllipseThetaWholeRecording':'gridstats_ellipse_theta_bnt','calbindinWholeRecording':'calbindin_bnt',
  135. 'thetaStrengthWholeRecording':'theta_strength_bnt','selectivityWholeRecording':'selectivity_bnt',
  136. 'meanRateWholeRecording':'mean_rate_bnt','thetaMeanPhaseWholeRecording':'theta_mean_phase_bnt','stabilityHalfWholeRecording':'stability_half_bnt',
  137. 'angularStabilityWholeRecording':'angular_stability_bnt','calbindinDistanceWholeRecording':'calbindin_dist_bnt'}
  138.  
  139. # new:
  140. #stabilityHalfWholeRecording
  141. #angularStabilityWholeRecording
  142. #calbindinDistanceWholeRecording
  143.  
  144. counter_cell_array = -1
  145.  
  146. for no in xrange(len(BNT_mat_dict['allStatistics'][0])):
  147. advance = True # label for advancing in the cell array ...
  148.  
  149. try:
  150. print('\n\nAnalyzing {}/{} (File {})'.format(no+1,len(BNT_mat_dict['allStatistics'][0])," - ".join(BNT_mat.split("/")[-2:])))
  151. path_name = str(BNT_mat_dict['allStatistics'][0][no]['Path'][0])
  152. path_name = "/".join(path_name.split("\\"))
  153. if not os.path.isdir(path_name):
  154. # change to linux compatible format ...
  155. if "N:" in path_name:
  156. path_name = "\\mnt\\N" + "".join(path_name.split("N:"))
  157. elif "L:" in path_name:
  158. path_name = "\\mnt\\L" + "".join(path_name.split("L:"))
  159. else:
  160. print('Path not found! Running into error here ...')
  161. path_name = "/".join(path_name.split('\\'))
  162.  
  163. session_name = str(BNT_mat_dict['allStatistics'][0][no]['Basename'][0])
  164. tetrode_no = str(BNT_mat_dict['allStatistics'][0][no]['Tetrode'][0][0])
  165. cluster_no = str(BNT_mat_dict['allStatistics'][0][no]['Cell'][0][0])
  166.  
  167. animal_id,n_drive_user,status,session_ts,analysis_ts = distill_params_root_table_BNT(path_name,session_name)
  168.  
  169. print('Path {} | Session {} | Tetrode {} | Cell {}'.format(path_name,session_name,tetrode_no,cluster_no))
  170. print('Animal ID {} | N Drive user {} | Session timestamp {}'.format(animal_id,n_drive_user,session_ts))
  171.  
  172.  
  173. if not status:
  174. print('Session file not found. Skipping.')
  175. continue
  176.  
  177. # Count meta tb entries
  178. meta_rows = count_meta_BNT(n_drive_user,animal_id,session_ts,params)
  179. #print('{} row(s) were found for this session in the db.'.format(meta_rows))
  180. # write entry to meta tb BNT
  181. if meta_rows == 0:
  182. id = to_meta_tb_BNT(analysis_ts,n_drive_user,animal_id,session_ts,params)
  183. print('New meta_tb entry created. Session timestamp: {}'.format(id[0].strftime("%B %d, %Y at %H-%M-%S")))
  184.  
  185. # write to tetrodes_tb BNT:
  186. id = to_tetrodes_tb_BNT(tetrode_no,analysis_ts,path_name,n_drive_user,animal_id,session_ts,params)
  187. print('Entry tetrodes_tb_bnt: {}'.format(id))
  188. # write to sessions_tb BNT :
  189. id = to_sessions_tb_BNT(session_name, analysis_ts, n_drive_user, animal_id, session_ts,params)
  190. print('Entry sessions_tb_bnt: {}'.format(id))
  191.  
  192. # Write out cluster:
  193. id = to_clusters_tb_BNT(cluster_no,analysis_ts,tetrode_no,path_name,n_drive_user,animal_id,session_ts,params)
  194. print('Entry clusters_tb_bnt: {}'.format(id))
  195.  
  196. # Create scores dict to eventually enter into "BNT_tb_screen"
  197.  
  198. all_scores = dict()
  199. # extract stats entries from allStatistics field
  200. for score in all_scores_labels:
  201. if score in BNT_mat_dict['allStatistics'][0][0].dtype.fields.keys():
  202. #print('Found {}'.format(score))
  203. try:
  204. if len(BNT_mat_dict['allStatistics'][0][no][score]) > 1:
  205. all_scores[all_scores_labels[score]] = BNT_mat_dict['allStatistics'][0][no][score]
  206. elif len(BNT_mat_dict['allStatistics'][0][no][score][0]) > 1:
  207. all_scores[all_scores_labels[score]] = BNT_mat_dict['allStatistics'][0][no][score][0]
  208. else:
  209. all_scores[all_scores_labels[score]] = BNT_mat_dict['allStatistics'][0][no][score][0][0]
  210.  
  211. except IndexError as err:
  212. print('Empty: {}'.format(score))
  213. all_scores[all_scores_labels[score]] = np.nan
  214. advance = False
  215.  
  216. else:
  217. print('Not found: {}'.format(score))
  218. all_scores[all_scores_labels[score]] = np.nan
  219.  
  220.  
  221. if advance: counter_cell_array += 1
  222.  
  223. # check if label in cell array (.mat) matches allStatistics label:
  224. tetrode_no_mat = int(str(BNT_mat_dict['cellsData'][counter_cell_array][0]['unitLabel'][0][0][0]).split("C")[0].split("T")[1])
  225. cluster_no_mat = int(str(BNT_mat_dict['cellsData'][counter_cell_array][0]['unitLabel'][0][0][0]).split("C")[1])
  226. if not (tetrode_no == tetrode_no_mat) and (cluster_no_mat == cluster_no): ('Labels do not match!');sys.exit()
  227.  
  228. # Write out tracking and spiketimes cluster + tracking:
  229. # Spiketimes tracking:
  230. try:
  231. spiketimes_tracking_session_bnt = pd.DataFrame(BNT_mat_dict['cellsData'][counter_cell_array][0]['epochs'][0][0][0][0][0]['spikes2Pos'][0],
  232. columns=['time','correct_x_inter','correct_y_inter'])
  233. spike_no_bnt = len(spiketimes_tracking_session_bnt)
  234. # write to db
  235. cluster_no_spiketimes_tracking_bnt = to_spiketimes_tracking_table_BNT(cluster_no,analysis_ts,tetrode_no,session_name,path_name,n_drive_user,animal_id,session_ts, spiketimes_tracking_session_bnt, spike_no_bnt, params)
  236. print('Entered into to_spiketimes_tracking_table_BNT: {}'.format(cluster_no_spiketimes_tracking_bnt))
  237. except KeyError:
  238. cluster_no_spiketimes_tracking_bnt = to_spiketimes_tracking_table_BNT(cluster_no,analysis_ts,tetrode_no,session_name,path_name,n_drive_user,animal_id,session_ts, [np.nan], np.nan,params)
  239. print('No spiketimes + tracking for this cluster found.')
  240.  
  241. # Tracking:
  242. try:
  243. tracking_session_bnt = pd.DataFrame(BNT_mat_dict['cellsData'][counter_cell_array][0]['epochs'][0][0][0][0][0]['pos'][0],
  244. columns=['time','correct_x_inter','correct_y_inter'])
  245. session_name_tracking_bnt = to_tracking_table_bnt(analysis_ts,session_name,n_drive_user,animal_id,session_ts,
  246. tracking_session_bnt,params)
  247. print('Entered into tracking_tb_BNT: {}'.format(session_name_tracking_bnt))
  248. except KeyError:
  249. session_name_tracking_bnt = to_tracking_table_bnt(analysis_ts,session_name,n_drive_user,animal_id,session_ts,
  250. [np.nan],params)
  251. print('No tracking data found')
  252.  
  253.  
  254. # take care of the rest:
  255. try:
  256. masked_ratemap_bnt = BNT_mat_dict['cellsData'][counter_cell_array][0]['epochs'][0][0][0][0][0]['map'][0]['z'][0][0].T # needs to be transposed to match Klusta
  257. except KeyError:
  258. print('No ratemap entry found')
  259. masked_ratemap_bnt = [np.nan]
  260.  
  261. try:
  262. autocorr_bnt = BNT_mat_dict['cellsData'][counter_cell_array][0]['epochs'][0][0][0][0][0]['aCorr'][0].T # needs to be transposed
  263. except KeyError:
  264. print('No spatial autocorrelation entry found')
  265. autocorr_bnt = [np.nan]
  266.  
  267. try:
  268. occupancy_map_bnt = BNT_mat_dict['cellsData'][counter_cell_array][0]['epochs'][0][0][0][0][0]['map'][0]['time'][0][0].T # needs to be transposed
  269. except KeyError:
  270. print('No occupancy map entry found')
  271. occupancy_map_bnt = [np.nan]
  272.  
  273. try:
  274. hist_angle_smooth_bnt = BNT_mat_dict['cellsData'][counter_cell_array][0]['epochs'][0][0][0][0][0]['hdValues'][0]
  275. hist_angle_smooth_bnt = np.append(hist_angle_smooth_bnt,hist_angle_smooth_bnt[0])
  276. bins_angle_center_bnt = np.radians(BNT_mat_dict['cellsData'][counter_cell_array][0]['epochs'][0][0][0][0][0]['hdAngles_deg'][0][0])
  277.  
  278. except KeyError:
  279. print('No HD tuning values found')
  280. hist_angle_smooth_bnt = [np.nan]
  281. bins_angle_center_bnt = [np.nan]
  282.  
  283. # Spikes cluster:
  284. try:
  285. spiketimes_cluster_bnt = BNT_mat_dict['cellsData'][counter_cell_array][0]['epochs'][0][0][0][0][0]['spikes'][0].squeeze()
  286. except KeyError:
  287. print('No spikes for this cluster found.')
  288. spiketimes_cluster_bnt = [np.nan]
  289.  
  290. # Parameters:
  291. try:
  292. param_dict = {}
  293. for field in BNT_mat_dict['p'].dtype.fields.keys():
  294. param_dict[field] = BNT_mat_dict['p'][field][0][0].squeeze()
  295. params_bnt = pd.DataFrame.from_dict(param_dict, orient='index')
  296. except KeyError:
  297. print('No parameters found!')
  298. params_bnt =[np.nan]
  299.  
  300. # delete entry from scores_...
  301. id = delete_BNT(cluster_no,tetrode_no,session_name,n_drive_user,animal_id,session_ts,params)
  302. print('Deleted from BNT_tb_screen [(Cluster, Tetrode, Session)]: {}'.format(id))
  303.  
  304. id = to_BNT_table(cluster_no,analysis_ts,tetrode_no,session_name,path_name,n_drive_user,animal_id,session_ts,all_scores,
  305. masked_ratemap_bnt,autocorr_bnt,occupancy_map_bnt,hist_angle_smooth_bnt,bins_angle_center_bnt,spiketimes_cluster_bnt,params_bnt,params)
  306.  
  307. print('Entry BNT_tb_screen: {}'.format(id))
  308. if id == None:
  309. print('No entry could be created for ')
  310. sys.exit()
  311. except IndexError as err:
  312. print('Index Error! continuing ...')
  313. continue
Add Comment
Please, Sign In to add comment