Пример #1
0
    def __call__(self, seq, result=None):
        if result is None and self.working is None:
            self.setResultArray(len(seq))
        elif self.working is not None:
            if len(seq) != self.working.shape[0]:
                self.setResultArray(len(seq))

        result = self.working
        result.fill(0)
        if type(seq) == str:
            seq = seq.encode("utf8")
        elif type(seq) != bytes:
            seq = b"".join(seq)

        return seq_to_symbols(seq, self.motifs, self.motif_length, result)
Пример #2
0
def blockwise_bootstrap(signal,
                        calc,
                        block_size,
                        num_reps,
                        seq_to_symbols=None,
                        num_stats=None):
    """returns observed statistic and the probability from the bootstrap
    test of observing more `power' by chance than that estimated from the
    observed signal

    Parameters
    ----------
    signal
        a series, can be a sequence object
    calc
        function to calculate the period power, e.g. ipdft, hybrid,
        auto_corr or any other statistic.
    block_size
        size of contiguous values for resampling
    num_reps
        number of randomly generated permutations
    seq_to_symbols
        function to convert a sequence to 1/0. If not
        provided, the raw data is used.
    num_stats
        the number of statistics being evaluated for each
        interation. Default to 1.

    """
    signal_length = len(signal)

    if seq_to_symbols is not None:
        dtype = "c"
    else:
        dtype = None  # let numpy guess

    signal = numpy.array(list(signal), dtype=dtype)

    if seq_to_symbols is not None:
        symbolic = seq_to_symbols(signal)
        data = symbolic
    else:
        data = signal

    obs_stat = calc(data)
    if seq_to_symbols is not None:
        if sum(symbolic) == 0:
            p = [numpy.array([1.0, 1.0, 1.0]), 1.0][num_stats == 1]

            return obs_stat, p

    if num_stats is None:
        try:
            num_stats = calc.getNumStats()
        except AttributeError:
            num_stats = 1

    if num_stats == 1:
        count = 0
    else:
        count = numpy.zeros(num_stats)

    for rep in range(num_reps):
        # get sample positions
        sampled_indices = sampled_places(block_size, signal_length)
        new_signal = signal.take(sampled_indices)
        if seq_to_symbols is not None:
            symbolic = seq_to_symbols(new_signal)
            data = symbolic
        else:
            data = new_signal
        sim_stat = calc(data)
        # count if > than observed
        if num_stats > 1:
            count[sim_stat >= obs_stat] += 1
        elif sim_stat >= obs_stat:
            count += 1

    return obs_stat, count / num_reps