# RateMonitor class

(Shortest import: `from brian2.monitors import RateMonitor)`

class brian2.monitors.ratemonitor.RateMonitor(*args, **kw)

  Bases: `CodeRunner`, `Group`, `ABC`

  Abstract base class for monitors that record rates.

  Methods

  `binned_rate`(bin_size) | Return the rate calculated in bins of a certain size.
  `smooth_rate`([window, width]) | Returns a smoothed out version of the firing rate(s).

  Details

  abstractmethod binned_rate(bin_size)

    Return the rate calculated in bins of a certain 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 values. For EventMonitor subclasses, this is a 2D array with shape (neurons, bins). For PopulationRateMonitor, this is a 1D array.

    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.

  smooth_rate(window='gaussian', width=None)

    Returns a smoothed out version of the firing rate(s).

    Parameters:

      window : str, ndarray

      > The window to use for smoothing. Can be a string to chose a predefined window(`flat` for a rectangular, and `gaussian` for a Gaussian-shaped window).

      > In this case the width of the window is determined by the `width` argument. Note that for the Gaussian window, the `width` parameter specifies the standard deviation of the Gaussian, the width of the actual window is `4*width + dt` (rounded to the nearest dt). For the flat window, the width is rounded to the nearest odd multiple of dt to avoid shifting the rate in time. Alternatively, an arbitrary window can be given as a numpy array (with an odd number of elements). In this case, the width in units of time depends on the `dt` of the simulation, and no `width` argument can be specified. The given window will be automatically normalized to a sum of 1.

      width : `Quantity`, optional

      > The width of the `window` in seconds (for a predefined window).

    Returns:

      rate : `Quantity`

      > The smoothed firing rate(s) in Hz. For EventMonitor subclasses, this returns a 2D array with shape (neurons, time_bins). For PopulationRateMonitor, this returns a 1D array. Note that the rates are smoothed at the original time step resolution (dt), not re-binned. The length of the returned array equals the number of recorded time steps and can be plotted against the original time values (e.g., self.t).

    Warning

    This method will give incorrect results if the monitor has recorded values with varying `dt` values.