"""
# Stage 03 Trigger Detection
"""

from pathlib import Path
configfile: Path('configs') / 'config_template.yaml'
include: Path() / '..' / 'utils' / 'Snakefile'

#### Housekeeping ####

def filtered_triggers(wildcards):
    default_input = OUTPUT_DIR / config.DETECTION_BLOCK / config.STAGE_OUTPUT
    return prev_rule_output(wildcards, rule_list=config.TRIGGER_FILTER,
                            default_input=default_input)

#### UTILITY BLOCKS ####

use rule template_all as all with:
    input:
        check = OUTPUT_DIR / 'input.check',
        # configfile = Path('configs') / f'config_{PROFILE}.yaml',
        data = filtered_triggers,
        img = OUTPUT_DIR / 'plot_trigger_times'


use rule template as plot_trigger_times with:
    input:
        data = filtered_triggers,
        script = SCRIPTS / 'plot_trigger_times.py'
    output:
        directory(OUTPUT_DIR / 'plot_trigger_times')
    params:
        params('plot_tstart', 'plot_tstop', 'plot_channels',
                filename='trigger_times_channel0.'+config.PLOT_FORMAT,
                config=config)

#### DETECTION BLOCKS (choose one) ####

rule threshold:
    input:
        data = config.STAGE_INPUT,
        thresholds = Path('{dir}') / 'threshold' / str(config.THRESHOLD_METHOD + "_thresholds.npy"),
        script = SCRIPTS / 'threshold.py'
    output:
        data = Path('{dir}') / 'threshold' / config.STAGE_OUTPUT
    shell:
        """
        {ADD_UTILS}
        python3 {input.script:q} --data {input.data:q} \
                                  --output {output.data:q} \
                                  --thresholds {input.thresholds:q}
        """


use rule template as calc_threshold_fixed with:
    # subrule of threshold
    input:
        data = config.STAGE_INPUT,
        script = SCRIPTS / 'calc_thresholds_fixed.py'
    output:
        Path('{dir}') / 'threshold' / 'fixed_thresholds.npy',
    params:
        params(threshold=config.FIXED_THRESHOLD)


use rule template as calc_threshold_fitted with:
    # subrule of threshold
    input:
        data = config.STAGE_INPUT,
        script = SCRIPTS / 'calc_thresholds_fitted.py'
    output:
        Path('{dir}') / 'threshold' / 'fitted_thresholds.npy',
        img_dir = directory(Path('{dir}') / 'threshold' / 'fitted_thresholds')
    params:
        params('sigma_factor', 'fit_function', 'bin_num', 'plot_channels',
               img_name="amplitudes_channel0." + config.PLOT_FORMAT, config=config)


use rule template as hilbert_phase with:
    input:
        data = config.STAGE_INPUT,
        script = SCRIPTS / 'hilbert_phase.py'
    output:
        Path('{dir}') / 'hilbert_phase' / config.STAGE_OUTPUT,
        img_dir = directory(Path('{dir}') / 'hilbert_phase' / 'hilbert_phase_plots')
    params:
        params('transition_phase', 'plot_channels', 'plot_tstart',
               'plot_tstop', config=config,
                img_name="hilbert_phase_channel0." + config.PLOT_FORMAT)


use rule template as minima with:
    input:
        data = config.STAGE_INPUT,
        script = SCRIPTS / 'minima.py'
    output:
        Path('{dir}') / 'minima' / config.STAGE_OUTPUT,
        img_dir = directory(Path('{dir}') / 'minima' / 'minima_plots')
    params:
        params('minima_persistence', 'min_peak_distance', 'maxima_threshold_fraction',
               'maxima_threshold_window', 'num_interpolation_points',
               'plot_channels', 'plot_tstart', 'plot_tstop',
                img_name="minima_channel0." + config.PLOT_FORMAT, config=config)


#### FILTER BLOCKS (choose any) ####

use rule template as remove_short_states with:
    input:
        data = filtered_triggers,
        script = SCRIPTS / 'remove_short_states.py'
    output:
        Path('{dir}') / '{rule_name}' / str("remove_short_states." + config.NEO_FORMAT)
    params:
        params('min_up_duration', 'min_down_duration', 'remove_down_first',
                config=config)
