Skip to content

Commit e51113b

Browse files
author
Thinh Nguyen
committed
code refactor - improve logic for spikeglx_meta file search
1 parent 91a3824 commit e51113b

File tree

1 file changed

+30
-17
lines changed

1 file changed

+30
-17
lines changed

element_array_ephys/ephys.py

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -272,17 +272,14 @@ class Electrode(dj.Part):
272272
_skip_channel_counts = 9
273273

274274
def make(self, key):
275-
root_dir = pathlib.Path(get_ephys_root_data_dir())
276275
acq_software, probe_sn = (EphysRecording
277276
* ProbeInsertion & key).fetch1('acq_software', 'probe')
278277

279278
electrode_keys, lfp = [], []
280279

281280
if acq_software == 'SpikeGLX':
282-
spikeglx_meta_fp = (EphysRecording.EphysFile
283-
& key & 'file_path LIKE "%.ap.meta"').fetch1('file_path')
284-
spikeglx_rec_dir = (root_dir / spikeglx_meta_fp).parent
285-
spikeglx_recording = spikeglx.SpikeGLX(spikeglx_rec_dir)
281+
spikeglx_meta_filepath = get_spikeglx_meta_filepath(key)
282+
spikeglx_recording = spikeglx.SpikeGLX(spikeglx_meta_filepath.parent)
286283

287284
lfp_channel_ind = spikeglx_recording.lfmeta.recording_channels[
288285
-1::-self._skip_channel_counts]
@@ -619,10 +616,8 @@ def yield_unit_waveforms():
619616

620617
else:
621618
if acq_software == 'SpikeGLX':
622-
ephys_root_dir = get_ephys_root_data_dir()
623-
npx_meta_fp = ephys_root_dir / (EphysRecording.EphysFile & key
624-
& 'file_path LIKE "%.ap.meta"').fetch1('file_path')
625-
neuropixels_recording = spikeglx.SpikeGLX(npx_meta_fp.parent)
619+
spikeglx_meta_filepath = get_spikeglx_meta_filepath(key)
620+
neuropixels_recording = spikeglx.SpikeGLX(spikeglx_meta_filepath.parent)
626621
elif acq_software == 'Open Ephys':
627622
sess_dir = pathlib.Path(get_session_directory(key))
628623
openephys_dataset = openephys.OpenEphys(sess_dir)
@@ -659,17 +654,35 @@ def yield_unit_waveforms():
659654

660655
# ---------------- HELPER FUNCTIONS ----------------
661656

657+
def get_spikeglx_meta_filepath(ephys_recording_key):
658+
# attempt to retrieve from EphysRecording.EphysFile
659+
ephys_root_dir = get_ephys_root_data_dir()
660+
spikeglx_meta_filepath = ephys_root_dir / (
661+
EphysRecording.EphysFile & ephys_recording_key
662+
& 'file_path LIKE "%.ap.meta"').fetch1('file_path')
663+
# if not found, search in session_dir again
664+
if not spikeglx_meta_filepath.exists():
665+
sess_dir = pathlib.Path(get_session_directory(ephys_recording_key))
666+
inserted_probe_serial_number = (ProbeInsertion * probe.Probe
667+
& ephys_recording_key).fetch1('probe')
668+
669+
spikeglx_meta_filepaths = [fp for fp in sess_dir.rglob('*.ap.meta')]
670+
for meta_filepath in spikeglx_meta_filepaths:
671+
spikeglx_meta = spikeglx.SpikeGLXMeta(meta_filepath)
672+
if str(spikeglx_meta.probe_SN) == inserted_probe_serial_number:
673+
spikeglx_meta_filepath = meta_filepath
674+
break
675+
else:
676+
raise FileNotFoundError(
677+
'No SpikeGLX data found for probe insertion: {}'.format(ephys_recording_key))
678+
679+
return spikeglx_meta_filepath
680+
662681

663682
def get_neuropixels_channel2electrode_map(ephys_recording_key, acq_software):
664-
root_dir = pathlib.Path(get_ephys_root_data_dir())
665683
if acq_software == 'SpikeGLX':
666-
npx_meta_path = root_dir / (EphysRecording.EphysFile
667-
& ephys_recording_key
668-
& 'file_path LIKE "%.ap.meta"').fetch1('file_path')
669-
neuropixels_dir = (root_dir / npx_meta_path).parent
670-
671-
meta_filepath = next(pathlib.Path(neuropixels_dir).glob('*.ap.meta'))
672-
spikeglx_meta = spikeglx.SpikeGLXMeta(meta_filepath)
684+
spikeglx_meta_filepath = get_spikeglx_meta_filepath(ephys_recording_key)
685+
spikeglx_meta = spikeglx.SpikeGLXMeta(spikeglx_meta_filepath)
673686
electrode_config_key = (EphysRecording * probe.ElectrodeConfig
674687
& ephys_recording_key).fetch1('KEY')
675688

0 commit comments

Comments
 (0)