summaryrefslogtreecommitdiff
path: root/examples/example.py
blob: 06c33c520e8b4669acb287f631670d8fa91761c4 (plain)
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
from warnings import warn

import matplotlib as mpl

from transivent import configure_logging, get_waveform_params, process_file

# --- User configuration dictionary ---
CONFIG = {
    "SMOOTH_WIN_T": 10e-3,  # smoothing window in seconds (set to None to use frequency)
    "SMOOTH_WIN_F": None,  # smoothing window in Hz (set to None to use time)
    "DETECTION_SNR": 3,  # point-by-point detection threshold, <MIN_EVENT_KEEP_SNR
    "MIN_EVENT_KEEP_SNR": 5,  # min event (max-)amplitude in multiples of global noise
    "MIN_EVENT_T": 0.75e-6,  # minimum event duration (seconds)
    "WIDEN_FRAC": 10,  # fraction of event length to widen detected events
    "SIGNAL_POLARITY": 1,  # Signal polarity: -1 for negative events (below background), +1 for positive events (above background)
    "LOG_LEVEL": "INFO",  # logging level: DEBUG, INFO, WARNING, ERROR, CRITICAL
    "MAX_PLOT_POINTS": 10000,  # Downsample threshold for plotting
    "ENVELOPE_MODE_LIMIT": 10e-3,  # Use envelope when time span >10ms, show thresholds when <10ms
    "YSCALE_MODE": "snr",  # y-scale mode for event plotter: 'snr', 'percent' or 'raw'
    "FILTER_TYPE": "median",  # Filter type: "savgol", "gaussian", "moving_average", "median"
    "FILTER_ORDER": 3,  # Order of the savgol filter for smoothing
    "CHUNK_SIZE": None,  # Set to None to disable chunking
    # ---
    "DATA_PATH": "../hycav/data/2025-07-17_bsa/",
    "MEASUREMENTS": [
        {
            "data": "RefCurve_2025-07-17_0_065114.Wfm.bin",
        },
        {
            "data": "RefCurve_2025-07-17_1_065214.Wfm.bin",
        },
        {
            "data": "RefCurve_2025-07-17_2_065510.Wfm.bin",
        },
        {
            "data": "RefCurve_2025-07-17_3_065814.Wfm.bin",
        },
        {
            "data": "RefCurve_2025-07-17_4_065850.Wfm.bin",
        },
        {
            "data": "RefCurve_2025-07-17_5_070003.Wfm.bin",
        },
        {
            "data": "RefCurve_2025-07-17_6_070045.Wfm.bin",
        },
        {
            "data": "RefCurve_2025-07-17_7_070339.Wfm.bin",
        },
    ],
}


def main() -> None:
    """
    Main function to process all measurements.
    """
    # Configure logging
    configure_logging(CONFIG.get("LOG_LEVEL", "INFO"))

    for measurement in CONFIG["MEASUREMENTS"]:
        # Merge global config with measurement-specific overrides
        merged_config = CONFIG.copy()
        merged_config.update(measurement)

        # Extract parameters for process_file
        name = merged_config["data"]
        sidecar = merged_config.get("sidecar")

        params = get_waveform_params(
            name, data_path=merged_config["DATA_PATH"], sidecar=sidecar
        )
        sampling_interval = params["sampling_interval"]

        # Call with explicit parameters
        process_file(
            name=name,
            sampling_interval=sampling_interval,
            data_path=merged_config["DATA_PATH"],
            smooth_win_t=merged_config.get("SMOOTH_WIN_T"),
            smooth_win_f=merged_config.get("SMOOTH_WIN_F"),
            detection_snr=merged_config.get("DETECTION_SNR", 3.0),
            min_event_keep_snr=merged_config.get("MIN_EVENT_KEEP_SNR", 6.0),
            min_event_t=merged_config.get("MIN_EVENT_T", 0.75e-6),
            widen_frac=merged_config.get("WIDEN_FRAC", 10.0),
            signal_polarity=merged_config.get("SIGNAL_POLARITY", -1),
            max_plot_points=merged_config.get("MAX_PLOT_POINTS", 10000),
            envelope_mode_limit=merged_config.get("ENVELOPE_MODE_LIMIT", 10e-3),
            sidecar=sidecar,
            crop=merged_config.get("crop"),
            yscale_mode=merged_config.get("YSCALE_MODE", "snr"),
            show_plots=True,
            filter_type=merged_config.get("FILTER_TYPE", "gaussian"),
            filter_order=merged_config.get("FILTER_ORDER", 2),
            chunk_size=merged_config.get("CHUNK_SIZE"),
        )


if __name__ == "__main__":
    # Set Matplotlib rcParams directly here
    for optn, val in {
        "backend": "QtAgg",
        "figure.constrained_layout.use": True,
        "figure.dpi": 90,
        # "figure.figsize": (8.5 / 2.55, 6 / 2.55),
        "font.family": ("sans-serif",),
        "font.size": 11,
        "legend.fontsize": "x-small",
        "legend.handlelength": 1.5,
        "legend.handletextpad": 0.6,
        "lines.markersize": 4.0,
        "lines.markeredgewidth": 1.6,
        "lines.linewidth": 1.8,
        "xtick.labelsize": 10,
        "xtick.major.size": 3,
        "xtick.direction": "in",
        "ytick.labelsize": 10,
        "ytick.direction": "in",
        "ytick.major.size": 3,
        "axes.formatter.useoffset": False,
        "axes.formatter.use_mathtext": True,
        "errorbar.capsize": 3.0,
        "axes.linewidth": 1.4,
        "xtick.major.width": 1.4,
        "xtick.minor.width": 1.1,
        "ytick.major.width": 1.4,
        "ytick.minor.width": 1.1,
        "axes.labelsize": 11,
    }.items():
        if isinstance(val, (list, tuple)):
            val = tuple(val)
        try:
            mpl.rcParams[optn] = val
        except KeyError:
            warn(f"mpl rcparams key '{optn}' not recognised as a valid rc parameter.")
    main()