File: arrays.py

package info (click to toggle)
brian 2.9.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,872 kB
  • sloc: python: 51,820; cpp: 2,033; makefile: 108; sh: 72
file content (37 lines) | stat: -rw-r--r-- 1,299 bytes parent folder | download | duplicates (2)
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
"""
Helper module containing functions that operate on numpy arrays.
"""

import numpy as np


def calc_repeats(delay):
    """
    Calculates offsets corresponding to an array, where repeated values are
    subsequently numbered, i.e. if there n identical values, the returned array
    will have values from 0 to n-1 at their positions.
    The code is complex because tricks are needed for vectorisation.

    This function is used in the Python `SpikeQueue` to calculate the offset
    array for the insertion of spikes with their respective delays into the
    queue and in the numpy code for synapse creation to calculate how many
    synapses for each source-target pair exist.

    Examples
    --------
    >>> import numpy as np
    >>> print(calc_repeats(np.array([7, 5, 7, 3, 7, 5])))
    [0 0 1 0 2 1]
    """
    # We use merge sort because it preserves the input order of equal
    # elements in the sorted output
    sort_indices = np.argsort(delay, kind="mergesort")
    xs = delay[sort_indices]
    J = xs[1:] != xs[:-1]
    A = np.hstack((0, np.cumsum(J)))
    B = np.hstack((0, np.cumsum(np.logical_not(J))))
    BJ = np.hstack((0, B[:-1][J]))
    ei = B - BJ[A]
    ofs = np.zeros_like(delay, dtype=np.int32)
    ofs[sort_indices] = np.array(ei, dtype=ofs.dtype)
    return ofs