# EventMonitor class

(Shortest import: `from brian2 import EventMonitor)`

class brian2.monitors.spikemonitor.EventMonitor(*args, **kw)

  Bases: `RateMonitor`

  Record events from a `NeuronGroup` or another event source.

  The recorded events can be accessed in various ways: the attributes `i` and `t` store all the indices and event times, respectively. Alternatively, you can get a dictionary mapping neuron indices to event trains, by calling the `event_trains` method.

  Parameters:

    source : `NeuronGroup`, `SpikeSource`

    > The source of events to record.

    event : str

    > The name of the event to record

    variables : str or sequence of str, optional

    > Which variables to record at the time of the event (in addition to the index of the neuron). Can be the name of a variable or a list of names.

    record : bool, optional

    > Whether or not to record each event in `i` and `t` (the `count` will always be recorded). Defaults to `True`.

    when : str, optional

    > When to record the events, by default records events in the same slot where the event is emitted. See Scheduling for possible values.

    order : int, optional

    > The priority of of this group for operations occurring at the same time step and in the same scheduling slot. Defaults to the order where the event is emitted + 1, i.e. it will be recorded directly afterwards.

    name : str, optional

    > A unique name for the object, otherwise will use `source.name+'_eventmonitor_0'`, etc.

    codeobj_class : class, optional

    > The `CodeObject` class to run code with.

  See also

  `SpikeMonitor`

  Attributes

  `count` | The array of event counts (length = size of target group)
  `event` | The event that we are listening to
  `it` | Returns the pair (`i`, `t`).
  `it_` | Returns the pair (`i`, `t_`).
  `num_events` | Returns the total number of recorded events.
  `record` | Whether to record times and indices of events
  `record_variables` | The additional variables that will be recorded
  `source` | The source we are recording from

  Methods

  `after_run`() | Optional method to do work after a run is finished.
  `all_values`() | Return a dictionary mapping recorded variable names (including `t`) to a dictionary mapping neuron indices to arrays of variable values at the time of the events (sorted by time).
  `binned_rate`(bin_size) | Return the event rates binned with the given bin size.
  `event_trains`() | Return a dictionary mapping neuron indices to arrays of event times.
  `reinit`() | Clears all recorded spikes
  `resize`(new_size) |
  `values`(var) | Return a dictionary mapping neuron indices to arrays of variable values at the time of the events (sorted by time).

  Details

  count

    The array of event counts (length = size of target group)

  event

    The event that we are listening to

  it

    Returns the pair (`i`, `t`).

  it_

    Returns the pair (`i`, `t_`).

  num_events

    Returns the total number of recorded events.

  record

    Whether to record times and indices of events

  record_variables

    The additional variables that will be recorded

  source

    The source we are recording from

  after_run()

    Optional method to do work after a run is finished.

    Called by `Network.after_run` after the main simulation loop terminated.

  all_values()

    Return a dictionary mapping recorded variable names (including `t`) to a dictionary mapping neuron indices to arrays of variable values at the time of the events (sorted by time). This is equivalent to (but more efficient than) calling `values` for each variable and storing the result in a dictionary.

    Returns:

      all_values : dict

      > Dictionary mapping variable names to dictionaries which themselves are mapping neuron indicies to arrays of variable values at the time of the events.

    Examples

        >>> from brian2 import *
        >>> G = NeuronGroup(2, '''counter1 : integer
        ...                       counter2 : integer
        ...                       max_value : integer''',
        ...                    threshold='counter1 >= max_value',
        ...                    reset='counter1 = 0')
        >>> G.run_regularly('counter1 += 1; counter2 += 1')
        CodeRunner(...)
        >>> G.max_value = [50, 100]
        >>> mon = EventMonitor(G, event='spike', variables='counter2')
        >>> run(10*ms)
        >>> all_values = mon.all_values()
        >>> print(all_values['counter2'][0])
        [ 50 100]
        >>> print(all_values['t'][1])
        [ 9.9] ms

  binned_rate(bin_size)

    Return the event rates binned with the given bin size.

    Parameters:

      bin_size : `Quantity`

      > The size of the bins in seconds. Should be a multiple of dt.

    Returns:

      bins : `Quantity`

      > The start time of the bins.

      binned_values : `Quantity`

      > The binned rates as a 2D array (neurons × bins) in Hz.

    Notes

    The returned bin times represent the start of each bin interval, not the center. This is consistent with how Brian2 records spike times and other temporal data. For example, a spike recorded at time `t` occurred during the interval `[t, t+dt)`.

    For plotting purposes, especially with larger bin sizes, you may want to use bin centers instead of bin starts for a more intuitive visualization. You can easily calculate the bin centers by adding half the bin size:

        >> bins, rates = monitor.binned_rate(10*ms)
        >> bin_centers = bins + 10*ms / 2
        >> plt.plot(bin_centers, rates)

    This adjustment is particularly helpful when the bins are large relative to the time scale of interest, as it better represents where the rate measurement applies within each time window.

  event_trains()

    Return a dictionary mapping neuron indices to arrays of event times. Equivalent to calling `values('t')`.

    Returns:

      event_trains : dict

      > Dictionary that stores an array with the event times for each neuron index.

    See also

    `SpikeMonitor.spike_trains`

  reinit()

    Clears all recorded spikes

  resize(new_size)
  values(var)

    Return a dictionary mapping neuron indices to arrays of variable values at the time of the events (sorted by time).

    Parameters:

      var : str

      > The name of the variable.

    Returns:

      values : dict

      > Dictionary mapping each neuron index to an array of variable values at the time of the events

    Examples

        >>> from brian2 import *
        >>> G = NeuronGroup(2, '''counter1 : integer
        ...                       counter2 : integer
        ...                       max_value : integer''',
        ...                    threshold='counter1 >= max_value',
        ...                    reset='counter1 = 0')
        >>> G.run_regularly('counter1 += 1; counter2 += 1')
        CodeRunner(...)
        >>> G.max_value = [50, 100]
        >>> mon = EventMonitor(G, event='spike', variables='counter2')
        >>> run(10*ms)
        >>> counter2_values = mon.values('counter2')
        >>> print(counter2_values[0])
        [ 50 100]
        >>> print(counter2_values[1])
        [100]

## Tutorials and examples using this

- Example advanced/custom_events