|
| 1 | +import os |
| 2 | +import sys |
| 3 | +import subprocess |
| 4 | +import json |
| 5 | + |
| 6 | +from helpers import SpikeGLX_utils |
| 7 | +from helpers import log_from_json |
| 8 | +from helpers import run_one_probe |
| 9 | +from create_input_json import createInputJson |
| 10 | + |
| 11 | + |
| 12 | +modules = ['kilosort_helper', |
| 13 | + 'kilosort_postprocessing', |
| 14 | + 'noise_templates', |
| 15 | + 'mean_waveforms', |
| 16 | + 'quality_metrics'] |
| 17 | + |
| 18 | + |
| 19 | +def run_probe(prb, json_directory, npx_directory, |
| 20 | + session_str, gate_str, trigger_str, |
| 21 | + run_CatGT, catGT_dest, catGT_car_mode, |
| 22 | + catGT_loccar_min_um, catGT_loccar_max_um, |
| 23 | + catGT_cmd_string, |
| 24 | + ks_Th, refPerMS, |
| 25 | + ks_remDup=0, |
| 26 | + ks_saveRez=1, |
| 27 | + ks_copy_fproc=0, |
| 28 | + ks_templateRadius_um=163, |
| 29 | + ks_whiteningRadius_um=163, |
| 30 | + ks_minfr_goodchannels=0.1, |
| 31 | + c_Waves_snr_um=160, |
| 32 | + ni_present=True, |
| 33 | + ni_extract_string=None): |
| 34 | + # build path to the first probe folder; look into that folder |
| 35 | + # to determine the range of trials if the user specified t limits as |
| 36 | + # start and end |
| 37 | + run_folder_name = session_str + '_g' + gate_str |
| 38 | + prb0_fld_name = run_folder_name + '_imec' + prb |
| 39 | + prb0_fld = os.path.join(npx_directory, run_folder_name, prb0_fld_name) |
| 40 | + first_trig, last_trig = SpikeGLX_utils.ParseTrigStr( |
| 41 | + trigger_str, prb, gate_str, prb0_fld) |
| 42 | + trigger_str = repr(first_trig) + ',' + repr(last_trig) |
| 43 | + |
| 44 | + # create CatGT command for this probe |
| 45 | + print('Creating json file for CatGT on probe: ' + prb) |
| 46 | + # Run CatGT |
| 47 | + catGT_input_json = os.path.join(json_directory, session_str + prb + '_CatGT' + '-input.json') |
| 48 | + catGT_output_json = os.path.join(json_directory, session_str + prb + '_CatGT' + '-output.json') |
| 49 | + |
| 50 | + # build extract string for SYNC channel for this probe |
| 51 | + sync_extract = '-SY=' + prb + ',-1,6,500' |
| 52 | + # if this is the first probe processed, process the ni stream with it |
| 53 | + if ni_present: |
| 54 | + catGT_stream_string = '-ap -ni' |
| 55 | + ni_extract_string = ni_extract_string or '-XA=0,1,3,500 -iXA=1,3,3,0 -XD=-1,1,50 -XD=-1,2,1.7 -XD=-1,3,5 -iXD=-1,3,5' |
| 56 | + extract_string = sync_extract + ' ' + ni_extract_string |
| 57 | + else: |
| 58 | + catGT_stream_string = '-ap' |
| 59 | + extract_string = sync_extract |
| 60 | + # build name of first trial to be concatenated/processed; |
| 61 | + # allows reading of the metadata |
| 62 | + run_str = session_str + '_g' + gate_str |
| 63 | + run_folder = run_str |
| 64 | + prb_folder = run_str + '_imec' + prb |
| 65 | + input_data_directory = os.path.join(npx_directory, run_folder, prb_folder) |
| 66 | + fileName = run_str + '_t' + repr(first_trig) + '.imec' + prb + '.ap.bin' |
| 67 | + continuous_file = os.path.join(input_data_directory, fileName) |
| 68 | + metaName = run_str + '_t' + repr(first_trig) + '.imec' + prb + '.ap.meta' |
| 69 | + input_meta_fullpath = os.path.join(input_data_directory, metaName) |
| 70 | + print(input_meta_fullpath) |
| 71 | + createInputJson(catGT_input_json, |
| 72 | + npx_directory=npx_directory, |
| 73 | + continuous_file=continuous_file, |
| 74 | + kilosort_output_directory=catGT_dest, |
| 75 | + spikeGLX_data=True, |
| 76 | + input_meta_path=input_meta_fullpath, |
| 77 | + catGT_run_name=session_str, |
| 78 | + gate_string=gate_str, |
| 79 | + trigger_string=trigger_str, |
| 80 | + probe_string=prb, |
| 81 | + catGT_stream_string=catGT_stream_string, |
| 82 | + catGT_car_mode=catGT_car_mode, |
| 83 | + catGT_loccar_min_um=catGT_loccar_min_um, |
| 84 | + catGT_loccar_max_um=catGT_loccar_max_um, |
| 85 | + catGT_cmd_string=catGT_cmd_string + ' ' + extract_string, |
| 86 | + extracted_data_directory=catGT_dest |
| 87 | + ) |
| 88 | + # create json files for the other modules |
| 89 | + session_id = session_str + '_imec' + prb |
| 90 | + module_input_json = os.path.join(json_directory, session_id + '-input.json') |
| 91 | + # location of the binary created by CatGT, using -out_prb_fld |
| 92 | + run_str = session_str + '_g' + gate_str |
| 93 | + run_folder = 'catgt_' + run_str |
| 94 | + prb_folder = run_str + '_imec' + prb |
| 95 | + data_directory = os.path.join(catGT_dest, run_folder, prb_folder) |
| 96 | + fileName = run_str + '_tcat.imec' + prb + '.ap.bin' |
| 97 | + continuous_file = os.path.join(data_directory, fileName) |
| 98 | + outputName = 'imec' + prb + '_ks2' |
| 99 | + # kilosort_postprocessing and noise_templates modules alter the files |
| 100 | + # that are input to phy. If using these modules, keep a copy of the |
| 101 | + # original phy output |
| 102 | + ks_make_copy = 'kilosort_postprocessing' in modules or 'noise_templates' in modules |
| 103 | + kilosort_output_dir = os.path.join(data_directory, outputName) |
| 104 | + print(data_directory) |
| 105 | + print(continuous_file) |
| 106 | + print('ks_Th: ' + repr(ks_Th) + ' ,refPerMS: ' + repr(refPerMS)) |
| 107 | + createInputJson(module_input_json, |
| 108 | + npx_directory=npx_directory, |
| 109 | + continuous_file=continuous_file, |
| 110 | + spikeGLX_data=True, |
| 111 | + input_meta_path=input_meta_fullpath, |
| 112 | + kilosort_output_directory=kilosort_output_dir, |
| 113 | + ks_make_copy=ks_make_copy, |
| 114 | + noise_template_use_rf=False, |
| 115 | + catGT_run_name=session_id, |
| 116 | + gate_string=gate_str, |
| 117 | + probe_string=str(prb), |
| 118 | + ks_remDup=ks_remDup, |
| 119 | + ks_finalSplits=1, |
| 120 | + ks_labelGood=1, |
| 121 | + ks_saveRez=ks_saveRez, |
| 122 | + ks_copy_fproc=ks_copy_fproc, |
| 123 | + ks_minfr_goodchannels=ks_minfr_goodchannels, |
| 124 | + ks_whiteningRadius_um=ks_whiteningRadius_um, |
| 125 | + ks_Th=ks_Th, |
| 126 | + ks_CSBseed=1, |
| 127 | + ks_LTseed=1, |
| 128 | + ks_templateRadius_um=ks_templateRadius_um, |
| 129 | + extracted_data_directory=catGT_dest, |
| 130 | + c_Waves_snr_um=c_Waves_snr_um, |
| 131 | + qm_isi_thresh=refPerMS / 1000 |
| 132 | + ) |
| 133 | + |
| 134 | + # check for existence of log file, create if not there |
| 135 | + logFullPath = os.path.join(catGT_dest, 'ecephys_run_log.csv') |
| 136 | + if not os.path.isfile(logFullPath): |
| 137 | + # create the log file, write header |
| 138 | + log_from_json.writeHeader(logFullPath) |
| 139 | + |
| 140 | + # actually running the modules for this probe |
| 141 | + run_one_probe.runOne(session_id, |
| 142 | + json_directory, |
| 143 | + data_directory, |
| 144 | + run_CatGT, |
| 145 | + catGT_input_json, |
| 146 | + catGT_output_json, |
| 147 | + modules, |
| 148 | + module_input_json, |
| 149 | + logFullPath) |
| 150 | + |
| 151 | + |
| 152 | +if __name__ == '__main__': |
| 153 | + json_fp = sys.argv[1] |
| 154 | + |
| 155 | + with open(json_fp) as f: |
| 156 | + kwargs = json.load(f) |
| 157 | + |
| 158 | + run_one_probe(**kwargs) |
0 commit comments