|
| 1 | +# Batch wrapper: runs DeepUnitMatch and UMPy on the *merged* dataset (see |
| 2 | +# run_deepunitmatch_batch_onMerged.py for how that tree is built and how |
| 3 | +# sessions/good units are derived) with drift correction disabled entirely, |
| 4 | +# to see how much each pipeline's normal performance actually depends on it. |
| 5 | +# Everything else about each pipeline's default scoring stays exactly as it |
| 6 | +# normally is (DUM still uses the DNN similarity score; UMPy still uses its |
| 7 | +# own six metrics) -- this ablation isolates drift correction specifically, |
| 8 | +# independent of the score-source questions the spatialonly/scoreswap batch |
| 9 | +# scripts explore. |
| 10 | +# |
| 11 | +# DUM_nodrift/: run_deep_unit_match_core(..., apply_drift_correction=False). |
| 12 | +# mf.drift_n_sessions is never called, so avg_centroid/avg_waveform_per_tp |
| 13 | +# stay exactly as extract_parameters produced them -- centroid_dist and every |
| 14 | +# downstream step use uncorrected waveforms. The drift-correction pre-pass |
| 15 | +# itself still runs (it also builds the supervised match/non-match labels the |
| 16 | +# final Bayes step needs, regardless of drift correction), only the actual |
| 17 | +# correction step is skipped. |
| 18 | +# |
| 19 | +# UMPy_nodrift/: run_umpy_core(..., niter=1). ov.extract_metric_scores' own |
| 20 | +# niter loop is what applies drift correction (niter=2 does one pass); niter=1 |
| 21 | +# runs the loop once and skips it entirely, computing total_score/ |
| 22 | +# candidate_pairs/scores_to_include from the raw, uncorrected waveforms. |
| 23 | +# |
| 24 | +# Each such folder sits alongside the DeepUnitMatch/ and UMPy/ subfolders that |
| 25 | +# run_deepunitmatch_batch_onMerged.py writes for the same dataset. |
| 26 | + |
| 27 | +import os |
| 28 | +import sys |
| 29 | +import datetime |
| 30 | +import traceback |
| 31 | +import argparse |
| 32 | + |
| 33 | +import matplotlib |
| 34 | + |
| 35 | +matplotlib.use("Agg") # non-interactive backend for batch runs |
| 36 | + |
| 37 | +# ── project paths ─────────────────────────────────────────────────────────── |
| 38 | +_HERE = os.path.dirname(os.path.abspath(__file__)) |
| 39 | +sys.path.insert(0, _HERE) |
| 40 | +sys.path.insert(0, os.path.dirname(_HERE)) |
| 41 | +sys.path.insert(0, os.path.join(_HERE, "DeepUnitMatch")) |
| 42 | + |
| 43 | +import batch_lock |
| 44 | +import run_deepunitmatch_batch_onMerged as base_batch |
| 45 | +from DeepUnitMatch.testing import test |
| 46 | + |
| 47 | +# See batch_lock.sentinel_is_fresh() / run_deepunitmatch_batch_onMerged.py's |
| 48 | +# REDO_FROM_DATE for what this does. A dataset/condition combo is skipped once |
| 49 | +# its MatchingOverview.png exists and is at least this new. |
| 50 | +REDO_FROM_DATE = datetime.datetime(2026, 8, 22, 0, 0, 0) |
| 51 | + |
| 52 | +CONDITIONS = ("DUM_nodrift", "UMPy_nodrift") |
| 53 | + |
| 54 | + |
| 55 | +# ── path helpers ───────────────────────────────────────────────────────────── |
| 56 | + |
| 57 | + |
| 58 | +def get_nodrift_save_dir(merged_dir, condition): |
| 59 | + """Output dir for a given merged-data group + condition (one of CONDITIONS).""" |
| 60 | + subfolder = os.path.relpath(os.path.dirname(merged_dir), base_batch.BASE_INPUT) |
| 61 | + return os.path.join(base_batch.BASE_OUTPUT, subfolder, condition) |
| 62 | + |
| 63 | + |
| 64 | +def nodrift_results_exist(merged_dir, condition): |
| 65 | + """Return True when the sentinel output file is present and fresh for this dataset/condition combo.""" |
| 66 | + sentinel = os.path.join( |
| 67 | + get_nodrift_save_dir(merged_dir, condition), "MatchingOverview.png" |
| 68 | + ) |
| 69 | + return batch_lock.sentinel_is_fresh(sentinel, REDO_FROM_DATE) |
| 70 | + |
| 71 | + |
| 72 | +def get_group_lock_path(merged_dir): |
| 73 | + """ |
| 74 | + Lock file marking 'a run is currently processing the no-drift-correction |
| 75 | + ablation for this group', so multiple machines pointed at the same |
| 76 | + BASE_INPUT/BASE_OUTPUT can split work across groups without |
| 77 | + double-processing one. See batch_lock.py. Named distinctly from the other |
| 78 | + batch scripts' locks so they can all run on the same group concurrently. |
| 79 | + """ |
| 80 | + subfolder = os.path.relpath(os.path.dirname(merged_dir), base_batch.BASE_INPUT) |
| 81 | + return os.path.join(base_batch.BASE_OUTPUT, subfolder, ".processing_nodrift.lock") |
| 82 | + |
| 83 | + |
| 84 | +# ── entry point ─────────────────────────────────────────────────────────────── |
| 85 | + |
| 86 | + |
| 87 | +def parse_args(): |
| 88 | + parser = argparse.ArgumentParser( |
| 89 | + description="Run DeepUnitMatch and UMPy on the merged dataset with drift correction disabled." |
| 90 | + ) |
| 91 | + parser.add_argument( |
| 92 | + "--write-matlab-compat", |
| 93 | + action="store_true", |
| 94 | + help="Also write a MATLAB-compatible UnitMatch.mat from the Python outputs.", |
| 95 | + ) |
| 96 | + return parser.parse_args() |
| 97 | + |
| 98 | + |
| 99 | +def main(): |
| 100 | + args = parse_args() |
| 101 | + base_batch.WRITE_MATLAB_COMPAT = args.write_matlab_compat |
| 102 | + |
| 103 | + print(f"Scanning for merged-data groups under:\n {base_batch.BASE_INPUT}\n") |
| 104 | + groups = base_batch.find_merged_groups() |
| 105 | + if not groups: |
| 106 | + print("No merged-data groups found.") |
| 107 | + return |
| 108 | + print(f"Found {len(groups)} group(s).\n") |
| 109 | + |
| 110 | + model = None # DUM's default trained model, loaded once and reused across all groups |
| 111 | + |
| 112 | + for i, merged_dir in enumerate(groups): |
| 113 | + print(f"\n[{i + 1}/{len(groups)}] {merged_dir}") |
| 114 | + |
| 115 | + pending = [ |
| 116 | + condition |
| 117 | + for condition in CONDITIONS |
| 118 | + if not nodrift_results_exist(merged_dir, condition) |
| 119 | + ] |
| 120 | + if not pending: |
| 121 | + print(" Skipping (results exist and are fresh).") |
| 122 | + continue |
| 123 | + |
| 124 | + lock_path = get_group_lock_path(merged_dir) |
| 125 | + with batch_lock.try_lock(lock_path) as acquired: |
| 126 | + if not acquired: |
| 127 | + print(f" Skipping (already being processed by another run): {lock_path}") |
| 128 | + continue |
| 129 | + |
| 130 | + # re-check now that we hold the lock: another machine may have |
| 131 | + # finished this group while we were scanning/waiting for the lock |
| 132 | + pending = [ |
| 133 | + condition |
| 134 | + for condition in CONDITIONS |
| 135 | + if not nodrift_results_exist(merged_dir, condition) |
| 136 | + ] |
| 137 | + if not pending: |
| 138 | + print(" Skipping (completed by another run).") |
| 139 | + continue |
| 140 | + |
| 141 | + sess = base_batch._prepare_session(merged_dir) |
| 142 | + if sess is None: |
| 143 | + continue |
| 144 | + |
| 145 | + if model is None and "DUM_nodrift" in pending: |
| 146 | + print("Loading DeepUnitMatch model …") |
| 147 | + model = test.load_trained_model(device=base_batch.DEVICE) |
| 148 | + |
| 149 | + for condition in pending: |
| 150 | + save_dir = get_nodrift_save_dir(merged_dir, condition) |
| 151 | + try: |
| 152 | + if condition == "DUM_nodrift": |
| 153 | + base_batch.run_deep_unit_match_core( |
| 154 | + sess, |
| 155 | + save_dir, |
| 156 | + model=model, |
| 157 | + label=condition, |
| 158 | + apply_drift_correction=False, |
| 159 | + ) |
| 160 | + else: |
| 161 | + base_batch.run_umpy_core( |
| 162 | + sess, save_dir, label=condition, niter=1 |
| 163 | + ) |
| 164 | + except Exception as e: |
| 165 | + print(f" {condition} FAILED: {e}") |
| 166 | + traceback.print_exc() |
| 167 | + |
| 168 | + print("\nAll done.") |
| 169 | + |
| 170 | + |
| 171 | +if __name__ == "__main__": |
| 172 | + main() |
0 commit comments