1- from ibllib .ephys .spikes import ks2_to_alf
21from ibllib .io import spikeglx
32import numpy as np
43import ibllib .dsp as dsp
54from scipy import signal
65from ibllib .misc import print_progress
76from pathlib import Path
8- import alf .io
7+ import alf .io as aio
98import logging
9+ import ibllib .ephys .ephysqc as ephysqc
10+ from phylib .io import alf
1011
1112_logger = logging .getLogger ('ibllib' )
1213
@@ -87,11 +88,11 @@ def extract_rmsmap(fbin, out_folder=None, spectra=True):
8788 if not out_folder .exists ():
8889 out_folder .mkdir ()
8990 tdict = {'rms' : rms ['TRMS' ].astype (np .single ), 'timestamps' : rms ['tscale' ].astype (np .single )}
90- alf . io .save_object_npy (out_folder , object = alf_object_time , dico = tdict )
91+ aio .save_object_npy (out_folder , object = alf_object_time , dico = tdict )
9192 if spectra :
9293 fdict = {'power' : rms ['spectral_density' ].astype (np .single ),
9394 'freqs' : rms ['fscale' ].astype (np .single )}
94- alf . io .save_object_npy (out_folder , object = alf_object_freq , dico = fdict )
95+ aio .save_object_npy (out_folder , object = alf_object_freq , dico = fdict )
9596
9697
9798def _sample2v (ap_file ):
@@ -103,13 +104,28 @@ def _sample2v(ap_file):
103104 return s2v ['ap' ][0 ]
104105
105106
107+ def ks2_to_alf (ks_path , bin_path , out_path , bin_file = None , ampfactor = 1 , label = None , force = True ):
108+ """
109+ Convert Kilosort 2 output to ALF dataset for single probe data
110+ :param ks_path:
111+ :param bin_path: path of raw data
112+ :param out_path:
113+ :return:
114+ """
115+ m = ephysqc .phy_model_from_ks2_path (ks2_path = ks_path , bin_path = bin_path , bin_file = bin_file )
116+ ephysqc .unit_metrics_ks2 (ks_path , m , save = True )
117+ ac = alf .EphysAlfCreator (m )
118+ ac .convert (out_path , label = label , force = force , ampfactor = ampfactor )
119+
120+
106121def extract_data (ks_path , ephys_path , out_path ):
107122 efiles = spikeglx .glob_ephys_files (ephys_path )
108123
109124 for efile in efiles :
110125 if efile .get ('ap' ) and efile .ap .exists ():
111126 ks2_to_alf (ks_path , ephys_path , out_path , bin_file = efile .ap ,
112127 ampfactor = _sample2v (efile .ap ), label = None , force = True )
128+
113129 extract_rmsmap (efile .ap , out_folder = out_path , spectra = False )
114130 if efile .get ('lf' ) and efile .lf .exists ():
115131 extract_rmsmap (efile .lf , out_folder = out_path )
0 commit comments