File size: 24,508 Bytes
6a45c3a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
import json
import os
import sqlite3
import shutil
import time
import tempfile
import numpy as np
import soundfile as sf
from pathlib import Path
from itertools import permutations
from typing import Any, Dict, List, Optional

from reasoning_pipeline import aggregate_reasoning_summaries

MODEL_PATH = "best_audio_separation_model.pt"
_model = None


def get_model():
    global _model
    if _model is None:
        from model import load_model
        _model = load_model(MODEL_PATH)
    return _model


def apply_wiener_filter(est_sources, mixture, iterations=1):
    """
    A simplified Wiener filter refinement. 
    In source separation, this often refers to re-masking the mixture 
    based on the relative energy of the estimated sources.
    """
    # eps to avoid division by zero
    eps = 1e-10
    # Square the estimates to get power/variance proxies
    est_power = np.maximum(np.abs(est_sources)**2, eps) 
    total_power = np.sum(est_power, axis=0, keepdims=True) + eps
    
    # The Wiener gain is est_power / total_power
    # We apply this gain to the original mixture
    refined_sources = (est_power / total_power) * mixture
    return refined_sources


def save_waveform_plot(waveform, sample_rate, title, path=None):
    import matplotlib.pyplot as plt
    import torch
    if waveform.ndim > 1:
        waveform = waveform.mean(dim=0)
    data = waveform.cpu().numpy()
    times = np.arange(data.shape[-1]) / sample_rate
    fig, ax = plt.subplots(figsize=(10, 2.5))
    ax.plot(times, data, linewidth=0.7)
    ax.set_title(title)
    ax.set_xlabel("Time (s)")
    ax.set_ylabel("Amplitude")
    ax.set_xlim(0, times[-1] if len(times) > 0 else 1)
    ax.grid(True)
    fig.tight_layout()
    if path is None:
        temp_image = tempfile.NamedTemporaryFile(suffix=".png", delete=False)
        fig.savefig(temp_image.name)
        plt.close(fig)
        return temp_image.name
    out_path = str(path)
    fig.savefig(out_path)
    plt.close(fig)
    return out_path


def predict_sources(waveform, sr, reference_waveforms=None):
    import torch
    import torchaudio

    if waveform.shape[0] > 1:
        waveform = waveform.mean(dim=0, keepdim=True)

    if sr != 16000:
        waveform = torchaudio.functional.resample(
            waveform,
            sr,
            16000,
        )

    waveform = waveform.squeeze(0)
    with torch.no_grad():
        mixture = waveform.unsqueeze(0)
        prediction = get_model()(mixture)
        prediction = prediction.squeeze(0).permute(1, 0)
        
    # Convert to numpy for post-processing as per your evaluation snippet
    pred_np = prediction.cpu().numpy() # [3, T]
    mix_np = waveform.cpu().numpy()    # [T]

    # 1. Apply Wiener Filter
    refined_preds = apply_wiener_filter(pred_np, mix_np)

    # 2. Automated Permutation Alignment using Reference Audios
    if reference_waveforms is not None and len(reference_waveforms) > 0:
        best_perm = None
        min_mse = float('inf')
        num_sources = refined_preds.shape[0]

        for perm in permutations(range(num_sources)):
            current_mse = 0
            for i, p in enumerate(perm):
                if i < len(reference_waveforms) and reference_waveforms[i] is not None:
                    # Compare the first N samples to find the best match
                    ref = reference_waveforms[i]
                    length = min(refined_preds[p].shape[0], ref.shape[0])
                    current_mse += np.mean((ref[:length] - refined_preds[p][:length]) ** 2)
            
            if current_mse < min_mse:
                min_mse = current_mse
                best_perm = perm
        
        refined_preds = np.array([refined_preds[p] for p in best_perm])

    return torch.from_numpy(refined_preds.copy()), 16000


def save_separated_sources(prediction, output_dir, base_name, patient_names=None):
    import torch
    output_dir = Path(output_dir)
    output_dir.mkdir(parents=True, exist_ok=True)

    patient_names = patient_names or ["Patient 1", "Patient 2", "Patient 3"]
    patient_names = [name if name else f"Patient {idx + 1}" for idx, name in enumerate(patient_names)]

    output_audio_paths = []
    output_image_paths = []

    for i in range(prediction.shape[0]):
        audio_name = f"{base_name}_s{i+1}.wav"
        audio_path = output_dir / audio_name
        save_audio_file(
            str(audio_path),
            prediction[i].unsqueeze(0),
            16000,
        )
        output_audio_paths.append(str(audio_path))

        image_name = f"{base_name}_s{i+1}_waveform.png"
        image_path = output_dir / image_name
        save_waveform_plot(
            prediction[i],
            16000,
            title=f"Separated waveform - {patient_names[i]}",
            path=image_path,
        )
        output_image_paths.append(str(image_path))

    return output_audio_paths, output_image_paths


def separate_audio_file(audio_path, output_dir, patient_names=None):
    waveform, sr = load_audio(audio_path)
    prediction, sample_rate = predict_sources(waveform, sr)
    return save_separated_sources(prediction, output_dir, Path(audio_path).stem, patient_names)


def load_audio(audio_path):
    import torch
    try:
        import torchaudio
        return torchaudio.load(audio_path)
    except (ImportError, RuntimeError, OSError):
        try:
            data, sr = sf.read(audio_path, dtype="float32")
            if data.ndim == 1:
                waveform = torch.from_numpy(data).unsqueeze(0)
            else:
                waveform = torch.from_numpy(data.T)
            return waveform, sr
        except (RuntimeError, ValueError, OSError):
            import librosa
            data, sr = librosa.load(audio_path, sr=None, mono=False)
            if data.ndim == 1:
                waveform = torch.from_numpy(data).unsqueeze(0)
            else:
                waveform = torch.from_numpy(data)
            return waveform, sr


def save_audio_file(path, waveform, sample_rate):
    import torch
    audio = waveform.detach().cpu().numpy()
    if audio.ndim == 1:
        audio = audio[np.newaxis, :]
    if audio.shape[0] > 1:
        audio = audio.T
    else:
        audio = audio[0]
    sf.write(path, audio, sample_rate) # save_audio_file now only saves the audio


RESULTS_DIR = Path("pipeline_results")
REASONING_SUMMARY_PATH = RESULTS_DIR / "reasoning_summary.json"
PATIENT_REGISTRY_PATH = RESULTS_DIR / "patient_registry.json"
HISTORY_RECORDS_PATH = RESULTS_DIR / "history_records.json"
DB_PATH = RESULTS_DIR / "speformer.db"
AUDIO_STORAGE_DIR = RESULTS_DIR / "audio"
GNN_RUN_ROOT = RESULTS_DIR / "gnn_runs"
DEFAULT_GNN_CHECKPOINT = "best_audio_separation_model.pt"


def _get_db_connection():
    RESULTS_DIR.mkdir(parents=True, exist_ok=True)
    conn = sqlite3.connect(DB_PATH)
    conn.row_factory = sqlite3.Row
    conn.execute("PRAGMA journal_mode=WAL;")
    return conn


def initialize_database():
    conn = _get_db_connection()
    conn.execute(
        """
        CREATE TABLE IF NOT EXISTS patient_registry (
            id INTEGER PRIMARY KEY,
            patient_id TEXT UNIQUE,
            name TEXT,
            reference_audio TEXT,
            local_reference_audio TEXT,
            created_at TEXT
        )
        """
    )
    conn.execute(
        """
        CREATE TABLE IF NOT EXISTS history_records (
            id INTEGER PRIMARY KEY,
            timestamp TEXT,
            mix_audio TEXT,
            local_mix_audio TEXT,
            patient_names TEXT,
            separated_sources TEXT,
            run_dirs TEXT,
            reasoning_summary_path TEXT,
            reasoning_count INTEGER
        )
        """
    )
    conn.execute(
        """
        CREATE TABLE IF NOT EXISTS audio_files (
            id INTEGER PRIMARY KEY,
            path TEXT UNIQUE,
            file_type TEXT,
            patient_id TEXT,
            created_at TEXT,
            notes TEXT
        )
        """
    )
    conn.commit()
    conn.close()


def _record_audio_file_metadata(path: str, file_type: str, patient_id: Optional[str] = None, notes: Optional[str] = None):
    conn = None
    try:
        initialize_database()
        conn = _get_db_connection()
        conn.execute(
            "INSERT OR IGNORE INTO audio_files (path, file_type, patient_id, created_at, notes) VALUES (?, ?, ?, ?, ?)",
            (str(Path(path).resolve()), file_type, patient_id, time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime()), notes),
        )
        conn.commit()
    except Exception:
        pass
    finally:
        if conn:
            conn.close()


def _copy_audio_to_storage(src_path: Optional[str], subdir: str, prefix: str) -> Optional[str]:
    if not src_path:
        return None
    src = Path(src_path)
    try:
        if not src.exists():
            return None
        src_resolved = src.resolve()
        if RESULTS_DIR in src_resolved.parents or src_resolved == RESULTS_DIR.resolve():
            return str(src_resolved)
    except Exception:
        pass

    storage_dir = AUDIO_STORAGE_DIR / subdir
    storage_dir.mkdir(parents=True, exist_ok=True)
    timestamp = time.strftime("%Y%m%d%H%M%S", time.gmtime())
    dest = storage_dir / f"{prefix}_{timestamp}_{src.name}"
    shutil.copy2(src, dest)
    _record_audio_file_metadata(str(dest), subdir, prefix)
    return str(dest)


def save_patient_registry(patient_entries: List[Dict[str, str]], registry_path: Optional[Path] = None) -> Path:
    path = Path(registry_path) if registry_path is not None else PATIENT_REGISTRY_PATH
    path.parent.mkdir(parents=True, exist_ok=True)

    initialize_database()
    conn = _get_db_connection()
    local_entries = []
    for entry in patient_entries:
        patient_id = entry.get("patient_id") or entry.get("id") or ""
        name = entry.get("name") or ""
        ref_audio = entry.get("reference_audio")
        local_ref_audio = _copy_audio_to_storage(ref_audio, "patient_reference", patient_id) if ref_audio else None
        conn.execute(
            "INSERT OR REPLACE INTO patient_registry (patient_id, name, reference_audio, local_reference_audio, created_at) VALUES (?, ?, ?, ?, ?)",
            (
                patient_id,
                name,
                str(ref_audio) if ref_audio else None,
                local_ref_audio,
                time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime()),
            ),
        )
        local_entries.append({
            "patient_id": patient_id,
            "name": name,
            "reference_audio": local_ref_audio or (str(ref_audio) if ref_audio else None),
        })
    conn.commit()
    conn.close()

    with open(path, "w", encoding="utf-8") as f:
        json.dump(local_entries, f, indent=2)
    return path


def load_patient_registry(registry_path: Optional[Path] = None) -> List[Dict[str, str]]:
    if registry_path is None and DB_PATH.exists():
        try:
            initialize_database()
            conn = _get_db_connection()
            cursor = conn.execute("SELECT patient_id, name, local_reference_audio AS reference_audio FROM patient_registry ORDER BY id")
            rows = cursor.fetchall()
            conn.close()
            if rows:
                return [{"patient_id": row["patient_id"], "name": row["name"], "reference_audio": row["reference_audio"]} for row in rows]
        except Exception:
            pass

    path = Path(registry_path) if registry_path is not None else PATIENT_REGISTRY_PATH
    if not path.exists():
        return []
    try:
        with open(path, "r", encoding="utf-8") as f:
            data = json.load(f)
            if isinstance(data, list):
                return data
    except Exception:
        pass
    return []


def resolve_patient_names(patient_names=None, registry_path: Optional[Path] = None):
    registry = load_patient_registry(registry_path)
    resolved = []
    patient_names = patient_names or []
    for idx in range(3):
        name = None
        if idx < len(patient_names) and patient_names[idx]:
            name = patient_names[idx]
        elif idx < len(registry) and registry[idx].get("name"):
            name = registry[idx]["name"]
        if not name:
            name = f"Patient {idx + 1}"
        resolved.append(name)
    return resolved


def load_reasoning_summary(results_path: Optional[Path] = None):
    path = Path(results_path) if results_path is not None else REASONING_SUMMARY_PATH
    if not path.exists():
        return []
    try:
        with open(path, "r", encoding="utf-8") as f:
            return json.load(f)
    except Exception:
        return []


def load_history_records(history_path: Optional[Path] = None) -> List[Dict[str, str]]:
    if DB_PATH.exists():
        try:
            initialize_database()
            conn = _get_db_connection()
            cursor = conn.execute("SELECT * FROM history_records ORDER BY id")
            rows = cursor.fetchall()
            conn.close()
            history = []
            for row in rows:
                local_mix_audio = row["local_mix_audio"] or row["mix_audio"]
                history.append({
                    "timestamp": row["timestamp"],
                    "mix_audio": local_mix_audio,
                    "patient_names": json.loads(row["patient_names"] or "[]"),
                    "separated_sources": json.loads(row["separated_sources"] or "[]"),
                    "run_dirs": json.loads(row["run_dirs"] or "[]"),
                    "reasoning_summary_path": row["reasoning_summary_path"],
                    "reasoning_count": row["reasoning_count"],
                    "local_mix_audio": row["local_mix_audio"],
                })
            return history
        except Exception:
            pass
    path = Path(history_path) if history_path is not None else HISTORY_RECORDS_PATH
    if not path.exists():
        return []
    try:
        with open(path, "r", encoding="utf-8") as f:
            data = json.load(f)
            if isinstance(data, list):
                return data
    except Exception:
        pass
    return []


def append_history_record(record: Dict[str, object], history_path: Optional[Path] = None) -> Path:
    path = Path(history_path) if history_path is not None else HISTORY_RECORDS_PATH
    local_mix_audio = _copy_audio_to_storage(record.get("mix_audio"), "mix_audio", "mix_audio") if record.get("mix_audio") else None
    if local_mix_audio:
        record["mix_audio"] = local_mix_audio

    initialize_database()
    conn = _get_db_connection()
    conn.execute(
        "INSERT INTO history_records (timestamp, mix_audio, local_mix_audio, patient_names, separated_sources, run_dirs, reasoning_summary_path, reasoning_count) VALUES (?, ?, ?, ?, ?, ?, ?, ?)",
        (
            record.get("timestamp"),
            str(record.get("mix_audio")) if record.get("mix_audio") else None,
            local_mix_audio,
            json.dumps(record.get("patient_names") or []),
            json.dumps(record.get("separated_sources") or []),
            json.dumps(record.get("run_dirs") or []),
            str(record.get("reasoning_summary_path")) if record.get("reasoning_summary_path") else None,
            int(record.get("reasoning_count") or 0),
        ),
    )
    conn.commit()
    conn.close()

    history = load_history_records(path)
    history.append(record)
    path.parent.mkdir(parents=True, exist_ok=True)
    with open(path, "w", encoding="utf-8") as f:
        json.dump(history, f, indent=2)
    return path


def load_wav2vec(device):
    import torch
    import importlib

    try:
        transformers = importlib.import_module("transformers")
    except ModuleNotFoundError as exc:
        raise ModuleNotFoundError(
            "Required package 'transformers' is not installed. Please install it with `pip install transformers` and restart the app."
        ) from exc

    Wav2Vec2Model = getattr(transformers, "Wav2Vec2Model")
    Wav2Vec2Processor = getattr(transformers, "Wav2Vec2Processor")

    processor = Wav2Vec2Processor.from_pretrained("facebook/wav2vec2-base-960h")
    model = Wav2Vec2Model.from_pretrained("facebook/wav2vec2-base-960h")
    model = model.to(device)
    model.eval()
    return processor, model


_live_sr = 16000
LIVE_PROCESSING_WINDOW_SECONDS = 5.0
LIVE_OVERLAP_SECONDS = 1.0

_live_processor = None
_live_wav2vec_model = None
_live_gnn_model = None
_live_device = None


def _initialize_models_for_live_processing():
    import torch
    global _live_processor, _live_wav2vec_model, _live_gnn_model, _live_device
    if _live_device is None:
        _live_device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
    if _live_processor is None or _live_wav2vec_model is None:
        _live_processor, _live_wav2vec_model = load_wav2vec(_live_device)
    if _live_gnn_model is None:
        from gnn import load_gnn_model
        _live_gnn_model = load_gnn_model(DEFAULT_GNN_CHECKPOINT, _live_device)
    return _live_processor, _live_wav2vec_model, _live_gnn_model, _live_device


def process_audio_chunk_for_separation(audio_chunk_tensor):
    """Expects a 1D tensor at 16kHz."""
    import torch
    prediction, _ = predict_sources(audio_chunk_tensor.unsqueeze(0), 16000)
    return prediction


def infer_on_separated_chunk(
    separated_chunk_np,
    gnn_model,
    processor,
    wav2vec_model,
    device,
    patient_manager,
    patient_id,
    timestamp,
):
    """Runs behavior inference and clinical state tracking on a single audio chunk."""
    import torch
    inputs = processor(separated_chunk_np, sampling_rate=16000, return_tensors="pt", padding=True)
    input_values = inputs.input_values.to(device)

    with torch.no_grad():
        out = wav2vec_model(input_values)
        emb = out.last_hidden_state.mean(dim=1)
        from gnn import build_chain_edge_index
        from torch_geometric.data import Data
        edge_index = build_chain_edge_index(1).to(device)
        data = Data(x=emb, edge_index=edge_index)
        w_logits, c_logits = gnn_model(data)
        w_prob = float(torch.sigmoid(w_logits).view(-1)[0].item())
        c_prob = float(torch.sigmoid(c_logits).view(-1)[0].item())

    from gnn import estimate_breathing_rate_bpm
    br_bpm = estimate_breathing_rate_bpm(separated_chunk_np, 16000, len(separated_chunk_np)/16000)

    return patient_manager.update_and_get_clinical_state(patient_id, w_prob, c_prob, breathing_rate=br_bpm, timestamp=timestamp)


def run_end_to_end(
    mix_audio_path,
    patient_names=None,
    reference_audio_paths=None, # New argument to accept reference audio paths
    gnn_checkpoint: Optional[str] = None,
    device_str: Optional[str] = None,
):
    import torch
    if not mix_audio_path:
        raise ValueError("No mixture audio file provided.")

    # Note: reference_audio_paths are passed but not used by the current separate_audio/predict_sources
    patient_names = resolve_patient_names(patient_names)
    outputs = separate_audio(
        mix_audio_path,
        patient_names=patient_names,
        reference_audio_paths=reference_audio_paths, # Pass reference audio paths
    )
    separated_audio_paths = outputs[:3]

    device_str = device_str or ("cuda" if torch.cuda.is_available() else "cpu")
    device = torch.device(device_str)
    processor, wav2vec_model = load_wav2vec(device)

    from gnn import load_gnn_model

    gnn_model = load_gnn_model(gnn_checkpoint or DEFAULT_GNN_CHECKPOINT, device)

    GNN_RUN_ROOT.mkdir(parents=True, exist_ok=True)
    behavior_results = []
    for audio_path in separated_audio_paths:
        from full_pipeline import infer_on_audio_file

        result = infer_on_audio_file(
            Path(audio_path),
            gnn_model,
            processor,
            wav2vec_model,
            GNN_RUN_ROOT,
            device,
        )
        if result is not None:
            behavior_results.append(result)

    run_dirs = [Path(item["artifacts"]["run_dir"]) for item in behavior_results if item.get("artifacts")]

    # Construct metadata mapping the audio_id (filename stem) to the selected patient name for human-readable reporting
    patient_meta = {}
    for i, audio_path in enumerate(separated_audio_paths):
        if audio_path:
            stem = Path(audio_path).stem
            if i < len(patient_names):
                patient_meta[stem] = {"name": patient_names[i]}

    reasoning_summaries = aggregate_reasoning_summaries(run_dirs, RESULTS_DIR, patient_meta=patient_meta)

    history_record = {
        "timestamp": time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime()),
        "mix_audio": str(mix_audio_path),
        "patient_names": patient_names,
        "separated_sources": separated_audio_paths,
        "run_dirs": [str(path) for path in run_dirs],
        "reasoning_summary_path": str(REASONING_SUMMARY_PATH),
        "reasoning_count": len(reasoning_summaries),
    }
    append_history_record(history_record)

    return outputs, reasoning_summaries, history_record


def monitoring_table_rows(results_path: Optional[Path] = None):
    records = load_reasoning_summary(results_path)
    rows = []
    for item in records:
        rows.append([
            item.get("audio_id"),
            item.get("overall_state"),
            item.get("mean_wheeze_prob"),
            item.get("mean_crackle_prob"),
            item.get("breathing_rate_mean"),
            item.get("comment"),
        ])
    return rows


def search_history_records(query: str, history_path: Optional[Any] = None):
    if not query:
        return []
    query_lower = query.strip().lower()
    records = load_history_records(history_path)
    results = []
    for item in records:
        patient_names = item.get("patient_names") or []
        mix_audio = str(item.get("mix_audio", ""))
        timestamp = str(item.get("timestamp", ""))
        if (
            query_lower in mix_audio.lower()
            or query_lower in timestamp.lower()
            or any(query_lower in str(name).lower() for name in patient_names)
        ):
            results.append([
                item.get("timestamp"),
                mix_audio,
                ", ".join([str(name) for name in patient_names if name]),
                item.get("reasoning_count"),
            ])
    return results


def search_reasoning_records(query: str, results_path: Optional[Any] = None):
    if not query:
        return []
    query_lower = query.strip().lower()
    records = load_reasoning_summary(results_path)
    results = []
    for item in records:
        audio_id = str(item.get("audio_id", ""))
        if query_lower in audio_id.lower():
            results.append([
                item.get("audio_id"),
                item.get("overall_state"),
                item.get("mean_wheeze_prob"),
                item.get("mean_crackle_prob"),
                item.get("breathing_rate_mean"),
                item.get("comment"),
            ])
    return results


def separate_audio(audio_path, patient_names=None, reference_audio_paths=None): # New argument

    import torch
    import torchaudio
    waveform, sr = load_audio(audio_path)
    
    # Load reference waveforms if provided for alignment
    ref_waveforms = []
    if reference_audio_paths:
        for path in reference_audio_paths:
            if path and Path(path).exists():
                ref_wav, _ = load_audio(path)
                ref_waveforms.append(ref_wav.mean(dim=0).cpu().numpy())
            else:
                ref_waveforms.append(None)

    if waveform.shape[0] > 1:
        waveform = waveform.mean(dim=0, keepdim=True)

    if sr != 16000:
        waveform = torchaudio.functional.resample(
            waveform,
            sr,
            16000,
        )

    waveform = waveform.squeeze(0)

    with torch.no_grad():
        # Now passing the references to predict_sources for alignment logic
        prediction, _ = predict_sources(waveform.unsqueeze(0), 16000, reference_waveforms=ref_waveforms)

    patient_names = resolve_patient_names(patient_names)
    output_dir = AUDIO_STORAGE_DIR / "separated" / Path(audio_path).stem
    output_audio_paths, output_image_paths = save_separated_sources(
        prediction,
        output_dir,
        Path(audio_path).stem,
        patient_names=patient_names,
    )

    while len(output_audio_paths) < 3:
        output_audio_paths.append(None)
    while len(output_image_paths) < 3:
        output_image_paths.append(None)

    return output_audio_paths + output_image_paths