def bonferroni_test(p_vals, sig_level, Nsurr, Nhyp=None, Sidak=False):
    """
    Run a Bonferroni multiple testing procedure on p-values <p_vals> with significance
    level <sig_level> given that <Nsurr> surrogates were used to compute the p-values.
    Optionally the number of simultaneously tested hypotheses can be set with <Nhyp>,
    if left at None, len(p_vals) will be used.
    If Sidak is True, will run Bonferroni-Sidak test.

    Written by Martin Vejmelka -- https://github.com/vejmelkam/ndw-climate/blob/master/src/multi_stats.py
    """
    if Nhyp is None:
        Nhyp = len(p_vals)
    if Nhyp < len(p_vals):
        p_vals = p_vals[:Nhyp]
    if not Sidak:
        bonf_level = sig_level / Nhyp
    else:
        bonf_level = 1.0 - (1.0 - sig_level) ** (1.0 / Nhyp)
    if bonf_level < 1.0 / Nsurr:
        raise Exception(
            'Will not run Bonferroni, not enough surrogates available for the test!'
            )
    return p_vals < bonf_level
