Example #1
0
def matrix_argmax(M):
    """returns the indices (row,col) of the maximum in M

    :type M: ndarray
    :param M: matrix
    :returns: tuple: indices of the max in M
    """
    idx = sp.nanargmax(M)
    j = int(idx % M.shape[1])
    i = int(sp.floor(idx / M.shape[1]))
    return i, j
Example #2
0
def matrix_argmax(M):
    """returns the indices (row,col) of the maximum in M

    :type M: ndarray
    :param M: matrix
    :returns: tuple: indices of the max in M
    """
    idx = sp.nanargmax(M)
    j = int(idx % M.shape[1])
    i = int(sp.floor(idx / M.shape[1]))
    return i, j
Example #3
0
def matrix_argmax(mat):
    """returns the indices (row,col) of the maximum value in :mat:

    :type mat: ndarray
    :param mat: input matrix
    :returns: tuple - (row,col) of the maximum value in :mat:
    """

    idx = sp.nanargmax(mat)
    j = int(idx % mat.shape[1])
    i = int(sp.floor(idx / mat.shape[1]))
    return i, j
Example #4
0
def matrix_argmax(mat):
    """returns the indices (row,col) of the maximum value in :mat:

    :type mat: ndarray
    :param mat: input matrix
    :returns: tuple - (row,col) of the maximum value in :mat:
    """

    idx = sp.nanargmax(mat)
    j = int(idx % mat.shape[1])
    i = int(sp.floor(idx / mat.shape[1]))
    return i, j
Example #5
0
    def _sort_chunk(self):
        """sort this chunk on the calculated discriminant functions

        method: "och"
            Examples for overlap samples
                  tau=-2     tau=-1      tau=0      tau=1      tau=2
            f1:  |-----|    |-----|    |-----|    |-----|    |-----|
            f2:    |-----|   |-----|   |-----|   |-----|   |-----|
            res:    +++       ++++      +++++      ++++       +++
        method: "sic"
            TODO:
        """

        # init
        if self.nf == 0:
            return
        spk_ep = epochs_from_binvec(
            sp.nanmax(self._disc, axis=1) > self._lpr_n)
        if spk_ep.size == 0:
            return
        l, r = get_cut(self._tf)
        for i in xrange(spk_ep.shape[0]):
            # FIX: for now we just continue for empty epochs,
            # where do they come from anyways?!
            if spk_ep[i, 1] - spk_ep[i, 0] < 1:
                continue
            mc = self._disc[spk_ep[i, 0]:spk_ep[i, 1], :].argmax(0).argmax()
            s = self._disc[spk_ep[i, 0]:spk_ep[i, 1], mc].argmax() + spk_ep[
                                                                     i, 0]
            spk_ep[i] = [s - l, s + r]

        # check epochs
        spk_ep = merge_epochs(spk_ep)
        n_ep = spk_ep.shape[0]

        for i in xrange(n_ep):
            #
            # method: overlap channels
            #
            if self._ovlp_taus is not None:
                # get event time and channel
                ep_t, ep_c = matrix_argmax(
                    self._disc[spk_ep[i, 0]:spk_ep[i, 1]])
                ep_t += spk_ep[i, 0]

                # lets fill in the results
                if ep_c < self.nf:
                    # was single unit
                    fid = self.get_idx_for(ep_c)
                    self.rval[fid].append(ep_t + self._chunk_offset)
                else:
                    # was overlap
                    my_oc_idx = self._oc_idx[ep_c]
                    fid0 = self.get_idx_for(my_oc_idx[0])
                    self.rval[fid0].append(ep_t + self._chunk_offset)
                    fid1 = self.get_idx_for(my_oc_idx[1])
                    self.rval[fid1].append(
                        ep_t + my_oc_idx[2] + self._chunk_offset)

            #
            # method: subtractive interference cancellation
            #
            else:
                ep_fout = self._fout[spk_ep[i, 0]:spk_ep[i, 1], :]
                ep_fout_norm = sp_la.norm(ep_fout)
                ep_disc = self._disc[spk_ep[i, 0]:spk_ep[i, 1], :].copy()

                niter = 0
                while sp.nanmax(ep_disc) > self._lpr_n:
                    # warn on spike overflow
                    niter += 1
                    if niter > self.nf:
                        warnings.warn(
                            'more spikes than filters found! '
                            'epoch: [%d:%d] %d' % (
                                spk_ep[i][0] + self._chunk_offset,
                                spk_ep[i][1] + self._chunk_offset,
                                niter))
                        if niter > 2 * self.nf:
                            break

                    # find epoch details
                    ep_t = sp.nanargmax(sp.nanmax(ep_disc, axis=1))
                    ep_c = sp.nanargmax(ep_disc[ep_t])

                    # build subtrahend
                    sub = shifted_matrix_sub(
                        sp.zeros_like(ep_disc),
                        self._xcorrs[ep_c, :, :].T,
                        ep_t - self._tf + 1)

                    # apply subtrahend
                    if ep_fout_norm > sp_la.norm(ep_fout + sub):
                        ## DEBUG

                        if self.verbose.get_has_plot(1):
                            try:
                                from spikeplot import xvf_tensor, plt, COLOURS

                                x_range = sp.arange(
                                    spk_ep[i, 0] + self._chunk_offset,
                                    spk_ep[i, 1] + self._chunk_offset)
                                f = plt.figure()
                                f.suptitle('spike epoch [%d:%d] #%d' %
                                           (spk_ep[i, 0] + self._chunk_offset,
                                            spk_ep[i, 1] + self._chunk_offset,
                                            niter))
                                ax1 = f.add_subplot(211)
                                ax1.set_color_cycle(
                                    ['k'] + COLOURS[:self.nf] * 2)
                                ax1.plot(x_range, sp.zeros_like(x_range),
                                         ls='--')
                                ax1.plot(x_range, ep_disc, label='pre_sub')
                                ax1.axvline(x_range[ep_t], c='k')
                                ax2 = f.add_subplot(212, sharex=ax1, sharey=ax1)
                                ax2.set_color_cycle(['k'] + COLOURS[:self.nf])
                                ax2.plot(x_range, sp.zeros_like(x_range),
                                         ls='--')
                                ax2.plot(x_range, sub)
                                ax2.axvline(x_range[ep_t], c='k')
                            except:
                                pass

                        ## BUGED

                        ep_disc += sub + self._lpr_s
                        if self._pr_s_b is not None:
                            bias, extend = self._pr_s_b
                            ep_disc[ep_t:min(ep_t + extend,
                                             ep_disc.shape[0]), ep_c] -= bias

                        ## DEBUG

                        if self.verbose.get_has_plot(1):
                            try:
                                ax1.plot(x_range, ep_disc, ls=':', lw=2,
                                         label='post_sub')
                                ax1.legend(loc=2)
                            except:
                                pass

                        ## BUGED

                        fid = self.get_idx_for(ep_c)
                        self.rval[fid].append(
                            spk_ep[i, 0] + ep_t + self._chunk_offset)
                    else:
                        break
                del ep_fout, ep_disc, sub