Skip to content

Commit 34e59cc

Browse files
author
Thinh Nguyen
committed
smart handling of finished median subtraction step
1 parent 296f7c6 commit 34e59cc

File tree

1 file changed

+37
-10
lines changed

1 file changed

+37
-10
lines changed

element_array_ephys/readers/kilosort_triggering.py

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,7 @@ def __init__(self, npx_input_dir: str, ks_output_dir: str,
347347
self._json_directory = self._ks_output_dir / 'json_configs'
348348
self._json_directory.mkdir(parents=True, exist_ok=True)
349349

350+
self._median_subtraction_finished = False
350351
self.ks_input_params = None
351352
self._modules_input_hash = None
352353
self._modules_input_hash_fp = None
@@ -372,15 +373,7 @@ def generate_modules_input_json(self):
372373

373374
self._module_input_json = self._json_directory / f'{self._npx_input_dir.name}-input.json'
374375

375-
if 'median_subtraction' in self._modules:
376-
# median subtraction step will overwrite original continuous.dat file with the corrected version
377-
# to preserve the original raw data - make a copy here and work on the copied version
378-
assert 'depth_estimation' in self._modules
379-
raw_ap_fp = self._npx_input_dir / 'continuous.dat'
380-
continuous_file = self._npx_input_dir / 'preproc_continuous.dat'
381-
shutil.copy2(raw_ap_fp, continuous_file)
382-
else:
383-
continuous_file = self._npx_input_dir / 'continuous.dat'
376+
continuous_file = self._get_raw_data_filepaths()
384377

385378
params = {}
386379
for k, v in self._params.items():
@@ -422,7 +415,14 @@ def run_modules(self):
422415
if module_status['completion_time'] is not None:
423416
continue
424417

425-
module_output_json = self._get_module_output_json_filename(module)
418+
if module == 'median_subtraction' and self._median_subtraction_finished:
419+
self._update_module_status(
420+
{module: {'start_time': datetime.utcnow(),
421+
'completion_time': datetime.utcnow(),
422+
'duration': 0}})
423+
continue
424+
425+
module_output_json = self._get_module_output_json_filename(module)
426426
command = [sys.executable,
427427
'-W', 'ignore', '-m', 'ecephys_spike_sorting.modules.' + module,
428428
'--input_json', module_input_json,
@@ -443,6 +443,33 @@ def run_modules(self):
443443

444444
self._update_total_duration()
445445

446+
def _get_raw_data_filepaths(self):
447+
if 'median_subtraction' not in self._modules:
448+
return self._npx_input_dir / 'continuous.dat'
449+
450+
# median subtraction step will overwrite original continuous.dat file with the corrected version
451+
# to preserve the original raw data - make a copy here and work on the copied version
452+
assert 'depth_estimation' in self._modules
453+
raw_ap_fp = self._npx_input_dir / 'continuous.dat'
454+
continuous_file = self._ks_output_dir / 'continuous.dat'
455+
if continuous_file.exists():
456+
if raw_ap_fp.stat().st_mtime < continuous_file.stat().st_mtime:
457+
# if the copied continuous.dat was actually modified,
458+
# median_subtraction may have been completed - let's check
459+
module_input_json = self._module_input_json.as_posix()
460+
module_logfile = module_input_json.replace('-input.json', '-run_modules-log.txt')
461+
with open(module_logfile, 'r') as f:
462+
previous_line = ''
463+
for line in f.readlines():
464+
if (line.startswith('ecephys spike sorting: median subtraction module')
465+
and previous_line.startswith('Total processing time:')):
466+
self._median_subtraction_finished = True
467+
return continuous_file
468+
previous_line = line
469+
470+
shutil.copy2(raw_ap_fp, continuous_file)
471+
return continuous_file
472+
446473
def _update_module_status(self, updated_module_status={}):
447474
if self._modules_input_hash is None:
448475
raise RuntimeError('"generate_modules_input_json()" not yet performed!')

0 commit comments

Comments
 (0)