File size: 24,236 Bytes
6866389
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""

Cross-method PIV comparison: Instantaneous vs Ensemble vs Stereo.



Publication-quality plots comparing one pass from each method against DNS

ground truth. Designed for academic journal submission.



Produces:

  1. mean_velocity_comparison.png   — U+ vs y+ (semi-log)

  2. stresses_comparison.png        — 1×3 subplots (uu+, vv+, −uv+)

  3. combined_stresses_comparison.png — all stresses on one axis



Usage (Python API):

    from cross_method_comparison import compare_methods

    compare_methods(

        gt_dir='path/to/ground_truth',

        inst_stats_path='path/to/mean_stats.mat',

        ens_ensemble_path='path/to/ensemble_result.mat',

        ens_coords_path='path/to/coordinates.mat',

        stereo_stats_path='path/to/stereo/mean_stats.mat',

        output_dir='path/to/output',

        ...

    )

"""

import numpy as np
import scipy.io as sio
from scipy.interpolate import interp1d
import matplotlib.pyplot as plt
import matplotlib as mpl
from pathlib import Path

# ── Publication-quality font setup ────────────────────────────────────────────
mpl.rcParams.update({
    'font.family': 'serif',
    'font.serif': ['CMU Serif', 'Computer Modern Roman', 'DejaVu Serif'],
    'mathtext.fontset': 'cm',
    'axes.unicode_minus': False,
    'text.usetex': False,
    'axes.labelsize': 12,
    'axes.titlesize': 13,
    'legend.fontsize': 11,
    'xtick.labelsize': 10,
    'ytick.labelsize': 10,
    'lines.linewidth': 1.5,
    'figure.dpi': 600,
    'savefig.dpi': 600,
    'savefig.bbox': 'tight',
    'savefig.pad_inches': 0.05,
})

# ── Okabe–Ito colourblind-safe palette ────────────────────────────────────────
METHOD_STYLES = {
    'Instantaneous': {'color': '#0072B2', 'marker': 'o'},   # blue
    'Ensemble':      {'color': '#D55E00', 'marker': 's'},   # vermillion
    'Stereo':        {'color': '#009E73', 'marker': '^'},   # teal
}
DNS_COLOR = 'k'


def _get_style(name):
    """Look up style by method name prefix (e.g. 'Instantaneous (16x16)' → Instantaneous)."""
    for key in METHOD_STYLES:
        if name.startswith(key):
            return METHOD_STYLES[key]
    return {'color': 'gray', 'marker': 'x'}


# =============================================================================
# Data loading helpers (reuse from benchmark_comparison where possible)
# =============================================================================

def _load_wall_units(gt_dir):
    """Load wall units from ground truth directory (auto-detect format)."""
    from benchmark_comparison import load_wall_units
    gt_dir = Path(gt_dir)
    for name in ('wall_units.mat', 'diagnostics.mat', 'direct_stats.mat'):
        p = gt_dir / name
        if p.exists():
            return load_wall_units(p)
    raise FileNotFoundError(f"No wall-units file found in {gt_dir}")


def _load_ground_truth(gt_dir):
    """Load ground truth profiles + wall units."""
    from benchmark_comparison import load_ground_truth
    gt_dir = Path(gt_dir)
    wu = _load_wall_units(gt_dir)

    for name in ('profiles.mat', 'ensemble_statistics_full.mat', 'direct_stats.mat'):
        p = gt_dir / name
        if p.exists():
            gt = load_ground_truth(p, wall_units_path=gt_dir / 'direct_stats.mat')
            break
    else:
        raise FileNotFoundError(f"No ground-truth profiles in {gt_dir}")

    gt_plus = {
        'y_plus': gt['y_plus'],
        'U_plus': gt['U_plus'],
        'V_plus': gt['V'] / wu['u_tau'],
        'uu_plus': gt['uu_plus'],
        'vv_plus': gt['vv_plus'],
        'uv_plus': gt['uv_plus'],
    }
    # Thread CI bounds if available
    for key in ('U_plus_ci_lo', 'U_plus_ci_hi',
                'uu_plus_ci_lo', 'uu_plus_ci_hi',
                'vv_plus_ci_lo', 'vv_plus_ci_hi',
                'uv_plus_ci_lo', 'uv_plus_ci_hi'):
        if key in gt:
            gt_plus[key] = gt[key]
    return gt_plus, wu


def _profiles_from_regular_grid(piv_data, x_exclude_vectors=4):
    """X-averaged profiles from a regular (no NaN border) grid."""
    from benchmark_comparison import compute_piv_profiles
    return compute_piv_profiles(piv_data, x_exclude_vectors=x_exclude_vectors)


def _profiles_from_stereo_grid(piv_struct, coords_struct, x_exclude_vectors=4):
    """X-averaged profiles from a stereo grid with NaN borders."""
    x = coords_struct.x
    y = coords_struct.y

    # Find valid subgrid
    valid_cols = np.any(~np.isnan(y), axis=0)
    col_indices = np.where(valid_cols)[0]
    mid_col = col_indices[len(col_indices) // 2]

    y_col = y[:, mid_col]
    valid_rows = ~np.isnan(y_col)
    y_unique = y_col[valid_rows]

    # X exclusion
    x_start = col_indices[0] + x_exclude_vectors
    x_end = col_indices[-1] - x_exclude_vectors + 1
    x_mask = np.zeros(x.shape[1], dtype=bool)
    x_mask[x_start:x_end] = True

    def _avg(field):
        return np.nanmean(field[valid_rows][:, x_mask], axis=1)

    return {
        'y_mm': y_unique,
        'U': _avg(piv_struct.ux) * 1000,
        'V': _avg(piv_struct.uy) * 1000,
        'uu': _avg(piv_struct.uu) * 1e6,
        'vv': _avg(piv_struct.vv) * 1e6,
        'uv': _avg(piv_struct.uv) * 1e6,
    }


def _to_wall_units(profiles, wall_units, y_plus_offset):
    """Convert profiles to plus units with y-offset."""
    from benchmark_comparison import convert_to_wall_units
    y_offset_mm = -profiles['y_mm'].min()
    piv_plus = convert_to_wall_units(profiles, wall_units, y_offset_mm=y_offset_mm)
    piv_plus['y_plus'] = piv_plus['y_plus'] + 1.0 + y_plus_offset
    return piv_plus


def load_instantaneous(stats_path, run_idx, wall_units, y_plus_offset,

                       x_exclude=4):
    """Load instantaneous PIV and return wall-unit profiles."""
    from benchmark_comparison import load_piv_statistics
    piv = load_piv_statistics(Path(stats_path), run_idx=run_idx)
    profiles = _profiles_from_regular_grid(piv, x_exclude_vectors=x_exclude)
    return _to_wall_units(profiles, wall_units, y_plus_offset)


def load_ensemble(ensemble_path, coords_path, run_idx, wall_units,

                  y_plus_offset, x_exclude=4):
    """Load ensemble PIV and return wall-unit profiles."""
    from benchmark_comparison import load_ensemble_statistics
    piv = load_ensemble_statistics(Path(ensemble_path), Path(coords_path),
                                   run_idx=run_idx)
    profiles = _profiles_from_regular_grid(piv, x_exclude_vectors=x_exclude)
    return _to_wall_units(profiles, wall_units, y_plus_offset)


def load_stereo(stats_path, run_idx, wall_units, y_plus_offset,

                x_exclude=4, trim_top=0):
    """Load stereo PIV (NaN-aware) and return wall-unit profiles."""
    stats = sio.loadmat(str(stats_path), squeeze_me=True, struct_as_record=False)
    piv = stats['piv_result'][run_idx]
    coords = stats['coordinates'][run_idx]
    profiles = _profiles_from_stereo_grid(piv, coords, x_exclude_vectors=x_exclude)

    if trim_top > 0 and len(profiles['y_mm']) > trim_top:
        y = profiles['y_mm']
        # Trim from the high-y end
        if y[0] > y[-1]:
            sl = slice(trim_top, None)
        else:
            sl = slice(None, -trim_top)
        profiles = {k: v[sl] if isinstance(v, np.ndarray) else v
                    for k, v in profiles.items()}

    return _to_wall_units(profiles, wall_units, y_plus_offset)


# =============================================================================
# Plotting
# =============================================================================

def _ci_band(ax, y_plus, lo, hi, sign=1, **kwargs):
    """Shade a 95 % confidence-interval band."""
    if sign == -1:
        lo, hi = hi, lo
    ax.fill_between(y_plus, sign * lo, sign * hi,
                    color=DNS_COLOR, alpha=0.10, linewidth=0, **kwargs)


def plot_velocity_comparison(methods, gt_plus, wall_units, output_dir, title_suffix=''):
    """

    U+ vs y+ — one marker series per method, DNS as reference line.



    Parameters

    ----------

    methods : dict

        {method_name: piv_plus_dict}  (keys must be in METHOD_STYLES)

    gt_plus : dict

        Ground truth in wall units

    wall_units : dict

    output_dir : Path

    """
    output_dir = Path(output_dir)
    output_dir.mkdir(parents=True, exist_ok=True)
    Re_tau = wall_units['Re_tau']

    fig, ax = plt.subplots(figsize=(7, 5))

    # DNS reference
    has_ci = 'U_plus_ci_lo' in gt_plus
    if has_ci:
        _ci_band(ax, gt_plus['y_plus'],
                 gt_plus['U_plus_ci_lo'], gt_plus['U_plus_ci_hi'])
    ax.semilogx(gt_plus['y_plus'], gt_plus['U_plus'], color=DNS_COLOR,
                linewidth=2, label='DNS', zorder=10)

    # PIV methods
    for name, piv_plus in methods.items():
        sty = _get_style(name)
        ax.semilogx(piv_plus['y_plus'], piv_plus['U_plus'],
                    color=sty['color'], marker=sty['marker'],
                    markersize=3.5, alpha=0.7, linestyle='none',
                    label=name, zorder=5)

    ax.set_xlabel(r'$y^+$')
    ax.set_ylabel(r'$U^+$')
    title = 'Mean Velocity Profile Comparison with DNS'
    if title_suffix:
        title += f' ({title_suffix})'
    ax.set_title(title)
    ax.set_xlim(1, Re_tau)
    ax.set_ylim(0, 25)
    ax.grid(True, alpha=0.25, linewidth=0.5)
    ax.legend(loc='lower right', framealpha=0.9)

    fig.tight_layout()
    fig.savefig(output_dir / 'mean_velocity_comparison.png')
    plt.close(fig)
    print(f"  Saved: {output_dir / 'mean_velocity_comparison.png'}")


def plot_stresses_subplots(methods, gt_plus, wall_units, output_dir, title_suffix=''):
    """

    1×3 subplot: uu+, vv+, −uv+ — one marker series per method per panel.

    """
    output_dir = Path(output_dir)
    output_dir.mkdir(parents=True, exist_ok=True)
    Re_tau = wall_units['Re_tau']
    has_ci = 'uu_plus_ci_lo' in gt_plus

    panels = [
        ('uu_plus', r"$\overline{u'u'}^+$", 1),
        ('vv_plus', r"$\overline{v'v'}^+$", 1),
        ('uv_plus', r"$-\overline{u'v'}^+$", -1),
    ]

    fig, axes = plt.subplots(1, 3, figsize=(7, 2.8))

    for ax, (var, ylabel, sign) in zip(axes, panels):
        # CI band
        ci_lo_key, ci_hi_key = f'{var}_ci_lo', f'{var}_ci_hi'
        if has_ci and ci_lo_key in gt_plus:
            _ci_band(ax, gt_plus['y_plus'],
                     gt_plus[ci_lo_key], gt_plus[ci_hi_key], sign=sign)

        # DNS
        ax.plot(gt_plus['y_plus'], sign * gt_plus[var], color=DNS_COLOR,
                linewidth=1.8, label='DNS', zorder=10)

        # PIV methods
        for name, piv_plus in methods.items():
            sty = _get_style(name)
            ax.plot(piv_plus['y_plus'], sign * piv_plus[var],
                    color=sty['color'], marker=sty['marker'],
                    markersize=2.5, alpha=0.65, linestyle='none',
                    label=name, zorder=5)

        ax.set_xlabel(r'$y^+$')
        ax.set_ylabel(ylabel)
        ax.set_xscale('log')
        ax.set_xlim(1, Re_tau)
        ax.grid(True, alpha=0.25, linewidth=0.5)

    # Single shared legend below the title
    handles, labels = axes[0].get_legend_handles_labels()
    fig.legend(handles, labels, loc='upper center', ncol=len(methods) + 1,
               bbox_to_anchor=(0.5, 1.02), framealpha=0.9)

    suptitle = 'Reynolds Stresses Method Comparison with DNS'
    if title_suffix:
        suptitle += f' ({title_suffix})'
    fig.suptitle(suptitle, y=1.08)
    fig.tight_layout()
    fig.subplots_adjust(top=0.88)
    fig.savefig(output_dir / 'stresses_comparison.png')
    plt.close(fig)
    print(f"  Saved: {output_dir / 'stresses_comparison.png'}")


def plot_combined_stresses(methods, gt_plus, wall_units, output_dir, title_suffix=''):
    """

    All stresses on one axis — line styles distinguish components,

    colours distinguish methods.

    """
    output_dir = Path(output_dir)
    output_dir.mkdir(parents=True, exist_ok=True)
    Re_tau = wall_units['Re_tau']
    has_ci = 'uu_plus_ci_lo' in gt_plus

    component_styles = {
        'uu_plus': {'linestyle': '-',  'label_tex': r"$\overline{u'u'}^+$",  'sign': 1},
        'vv_plus': {'linestyle': '--', 'label_tex': r"$\overline{v'v'}^+$",  'sign': 1},
        'uv_plus': {'linestyle': ':',  'label_tex': r"$-\overline{u'v'}^+$", 'sign': -1},
    }

    fig, ax = plt.subplots(figsize=(7, 5))

    # ── DNS reference lines ──────────────────────────────────────────────
    for var, csty in component_styles.items():
        sign = csty['sign']
        # CI band
        ci_lo, ci_hi = f'{var}_ci_lo', f'{var}_ci_hi'
        if has_ci and ci_lo in gt_plus:
            _ci_band(ax, gt_plus['y_plus'],
                     gt_plus[ci_lo], gt_plus[ci_hi], sign=sign)
        ax.plot(gt_plus['y_plus'], sign * gt_plus[var],
                color=DNS_COLOR, linewidth=1.8, linestyle=csty['linestyle'],
                zorder=10)

    # ── PIV method markers ───────────────────────────────────────────────
    for name, piv_plus in methods.items():
        sty = _get_style(name)
        for var, csty in component_styles.items():
            sign = csty['sign']
            ax.plot(piv_plus['y_plus'], sign * piv_plus[var],
                    color=sty['color'], marker=sty['marker'],
                    markersize=2.5, alpha=0.55, linestyle='none',
                    zorder=5)

    # ── Build a clean two-part legend ────────────────────────────────────
    # Part 1: method colours (dummy markers)
    method_handles = []
    for name in methods:
        sty = _get_style(name)
        h = plt.Line2D([], [], color=sty['color'], marker=sty['marker'],
                        markersize=5, linestyle='none', label=name)
        method_handles.append(h)
    # DNS entry
    dns_handle = plt.Line2D([], [], color=DNS_COLOR, linewidth=1.8,
                             linestyle='-', label='DNS')
    method_handles.insert(0, dns_handle)

    # Part 2: component line styles (black dummy lines)
    comp_handles = []
    for var, csty in component_styles.items():
        h = plt.Line2D([], [], color='gray', linewidth=1.5,
                        linestyle=csty['linestyle'], label=csty['label_tex'])
        comp_handles.append(h)

    leg1 = ax.legend(handles=method_handles, loc='upper right',
                     framealpha=0.9, title='Method')
    ax.add_artist(leg1)
    ax.legend(handles=comp_handles, loc='upper left',
              framealpha=0.9, title='Component')

    ax.set_xlabel(r'$y^+$')
    ax.set_ylabel(r'Stress$^+$')
    title = 'Reynolds Stresses Method Comparison with DNS'
    if title_suffix:
        title += f' ({title_suffix})'
    ax.set_title(title)
    ax.set_xscale('log')
    ax.set_xlim(1, Re_tau)
    ax.grid(True, alpha=0.25, linewidth=0.5)

    fig.tight_layout()
    fig.savefig(output_dir / 'combined_stresses_comparison.png')
    plt.close(fig)
    print(f"  Saved: {output_dir / 'combined_stresses_comparison.png'}")


# =============================================================================
# Error summary
# =============================================================================

def print_error_table(methods, gt_plus):
    """Print R² summary for all methods."""
    from benchmark_comparison import compute_errors

    header = f"{'Method':<18} {'U+ R²':<10} {'U+ RMS%':<10} {'uu+ R²':<10} {'vv+ R²':<10} {'-uv+ R²':<10}"
    print("\n" + header)
    print("-" * len(header))

    for name, piv_plus in methods.items():
        errs = compute_errors(piv_plus, gt_plus, y_plus_range=(10, 500))
        u_r2  = errs.get('U_plus',  {}).get('r2',      float('nan'))
        u_rms = errs.get('U_plus',  {}).get('rms_rel', float('nan'))
        uu_r2 = errs.get('uu_plus', {}).get('r2',      float('nan'))
        vv_r2 = errs.get('vv_plus', {}).get('r2',      float('nan'))
        uv_r2 = errs.get('uv_plus', {}).get('r2',      float('nan'))
        print(f"{name:<18} {u_r2:<10.4f} {u_rms:<10.1f} {uu_r2:<10.4f} {vv_r2:<10.4f} {uv_r2:<10.4f}")

    print()


# =============================================================================
# Main entry point
# =============================================================================

def compare_methods(

    gt_dir,

    output_dir,

    # Instantaneous

    inst_stats_path=None, inst_run_idx=3, inst_y_offset=3.0,

    # Ensemble

    ens_ensemble_path=None, ens_coords_path=None,

    ens_run_idx=4, ens_y_offset=0.8,

    # Stereo

    stereo_stats_path=None, stereo_run_idx=3, stereo_y_offset=0.8,

    stereo_trim_top=10,

    # Shared

    x_exclude=4,

    title_suffix='',

    inst_window_label=None,

    ens_window_label=None,

    stereo_window_label=None,

    trim_near_wall=0,

):
    """

    Compare one pass from each method against DNS ground truth.



    Parameters

    ----------

    gt_dir : str or Path

        Directory containing ground truth (direct_stats.mat etc.)

    output_dir : str or Path

        Where to save figures.

    inst_stats_path : str or Path, optional

        Path to instantaneous mean_stats.mat

    inst_run_idx : int

        0-based run index for instantaneous

    inst_y_offset : float

        Additional y+ offset for instantaneous (on top of hardcoded +1)

    ens_ensemble_path, ens_coords_path : str or Path, optional

        Paths to ensemble_result.mat and coordinates.mat

    ens_run_idx : int

        0-based run index for ensemble

    ens_y_offset : float

        Additional y+ offset for ensemble

    stereo_stats_path : str or Path, optional

        Path to stereo mean_stats.mat

    stereo_run_idx : int

        0-based run index for stereo

    stereo_y_offset : float

        Additional y+ offset for stereo

    stereo_trim_top : int

        Trim this many high-y points from stereo (NaN border cleanup)

    x_exclude : int

        Vectors to exclude from each x-edge

    """
    gt_plus, wu = _load_ground_truth(gt_dir)
    print(f"DNS: Re_tau={wu['Re_tau']:.0f}, u_tau={wu['u_tau']:.4f} mm/s")

    methods = {}

    def _trim(piv_plus, n):
        """Remove n lowest y+ points (nearest wall)."""
        if n <= 0:
            return piv_plus
        yp = piv_plus['y_plus']
        if yp[0] > yp[-1]:
            # Sorted high-to-low: wall is at the end
            sl = slice(None, -n if n > 0 else None)
        else:
            # Sorted low-to-high: wall is at the start
            sl = slice(n, None)
        return {k: (v[sl] if isinstance(v, np.ndarray) and len(v) > n else v)
                for k, v in piv_plus.items()}

    if inst_stats_path is not None:
        label = f"Instantaneous ({inst_window_label})" if inst_window_label else "Instantaneous"
        print(f"\nLoading {label} (run {inst_run_idx}, y+ offset +{inst_y_offset})...")
        data = load_instantaneous(inst_stats_path, inst_run_idx, wu, inst_y_offset, x_exclude)
        methods[label] = _trim(data, trim_near_wall)
        p = methods[label]
        print(f"  y+ range: {p['y_plus'].min():.1f}{p['y_plus'].max():.1f}")

    if ens_ensemble_path is not None and ens_coords_path is not None:
        label = f"Ensemble ({ens_window_label})" if ens_window_label else "Ensemble"
        print(f"\nLoading {label} (run {ens_run_idx}, y+ offset +{ens_y_offset})...")
        methods[label] = load_ensemble(
            ens_ensemble_path, ens_coords_path,
            ens_run_idx, wu, ens_y_offset, x_exclude)
        p = methods[label]
        print(f"  y+ range: {p['y_plus'].min():.1f}{p['y_plus'].max():.1f}")

    if stereo_stats_path is not None:
        label = f"Stereo ({stereo_window_label})" if stereo_window_label else "Stereo"
        print(f"\nLoading {label} (run {stereo_run_idx}, y+ offset +{stereo_y_offset}, "
              f"trim top {stereo_trim_top})...")
        data = load_stereo(
            stereo_stats_path, stereo_run_idx, wu, stereo_y_offset,
            x_exclude, stereo_trim_top)
        methods[label] = _trim(data, trim_near_wall)
        p = methods[label]
        print(f"  y+ range: {p['y_plus'].min():.1f}{p['y_plus'].max():.1f}")

    if not methods:
        raise ValueError("No data loaded — provide at least one method path.")

    print_error_table(methods, gt_plus)

    print("Generating plots...")
    output_dir = Path(output_dir)
    plot_velocity_comparison(methods, gt_plus, wu, output_dir, title_suffix=title_suffix)
    plot_stresses_subplots(methods, gt_plus, wu, output_dir, title_suffix=title_suffix)
    plot_combined_stresses(methods, gt_plus, wu, output_dir, title_suffix=title_suffix)
    print("Done.")


if __name__ == '__main__':
    import argparse

    parser = argparse.ArgumentParser(
        description='Cross-method PIV comparison (Instantaneous / Ensemble / Stereo)')

    parser.add_argument('--gt-dir', '-g', required=True,
                        help='Ground truth directory')
    parser.add_argument('--output-dir', '-o', required=True,
                        help='Output directory for figures')

    # Instantaneous
    parser.add_argument('--inst-stats', type=str, default=None,
                        help='Path to instantaneous mean_stats.mat')
    parser.add_argument('--inst-run', type=int, default=3,
                        help='0-based run index (default: 3)')
    parser.add_argument('--inst-y-offset', type=float, default=3.0,
                        help='Additional y+ offset (default: 3.0)')

    # Ensemble
    parser.add_argument('--ens-dir', type=str, default=None,
                        help='Directory with ensemble_result.mat + coordinates.mat')
    parser.add_argument('--ens-run', type=int, default=4,
                        help='0-based run index (default: 4)')
    parser.add_argument('--ens-y-offset', type=float, default=0.8,
                        help='Additional y+ offset (default: 0.8)')

    # Stereo
    parser.add_argument('--stereo-stats', type=str, default=None,
                        help='Path to stereo mean_stats.mat')
    parser.add_argument('--stereo-run', type=int, default=3,
                        help='0-based run index (default: 3)')
    parser.add_argument('--stereo-y-offset', type=float, default=0.8,
                        help='Additional y+ offset (default: 0.8)')
    parser.add_argument('--stereo-trim-top', type=int, default=10,
                        help='Trim N highest y points from stereo (default: 10)')

    parser.add_argument('--x-exclude', type=int, default=4,
                        help='Vectors to exclude from each x-edge (default: 4)')

    args = parser.parse_args()

    ens_ensemble_path = None
    ens_coords_path = None
    if args.ens_dir:
        ens_dir = Path(args.ens_dir)
        ens_ensemble_path = ens_dir / 'ensemble_result.mat'
        ens_coords_path = ens_dir / 'coordinates.mat'

    compare_methods(
        gt_dir=args.gt_dir,
        output_dir=args.output_dir,
        inst_stats_path=args.inst_stats,
        inst_run_idx=args.inst_run,
        inst_y_offset=args.inst_y_offset,
        ens_ensemble_path=ens_ensemble_path,
        ens_coords_path=ens_coords_path,
        ens_run_idx=args.ens_run,
        ens_y_offset=args.ens_y_offset,
        stereo_stats_path=args.stereo_stats,
        stereo_run_idx=args.stereo_run,
        stereo_y_offset=args.stereo_y_offset,
        stereo_trim_top=args.stereo_trim_top,
        x_exclude=args.x_exclude,
    )