def plot_meansine():
    dt = 0.001  # integration time step in seconds
    tmax = 4.0  # stimulus duration in seconds
    cutoff = 40.0  # cutoff frequency of stimulus in Hertz
    rng = np.random.RandomState(583)
    stimulus = 0.5 * mv.whitenoise(0.0, cutoff, dt, tmax, rng)
    time = np.arange(len(stimulus)) * dt
    mean = 3.0 * (1.0 - np.cos(2.0 * np.pi * time / time[-1]))
    stimulus += mean
    # response:
    rate0, adapt0 = mv.adaptation(time, stimulus, alpha=0.0, taua=0.3)
    rate, adapt = mv.adaptation(time, stimulus, alpha=0.2, taua=0.3)
    # plot stimulus and threshold:
    fig, axs = plt.subplots(2, 1, figsize=(figwidth, 0.6 * figwidth))
    ax = axs[0]
    ax.plot(time, stimulus, color=colors['green'], label='stimulus')
    ax.fill_between(time, adapt, -1.5, fc=colors['gray'], alpha=0.75)
    ax.plot(time, adapt, color=colors['red'], label='threshold')
    ax.set_ylim(-1.5, 7.5)
    ax.set_ylabel('Stimulus')
    ax.xaxis.set_major_locator(ticker.MultipleLocator(1.0))
    ax.xaxis.set_major_formatter(ticker.NullFormatter())
    ax.legend(loc='upper left')
    # plot rate:
    ax = axs[1]
    ax.plot(time, rate0, color=colors['cyan'], label='non adapting')
    ax.plot(time, rate, color=colors['blue'], label='adapting')
    ax.set_ylim(0.0, 200.0)
    ax.set_xlabel('Time [s]')
    ax.set_ylabel('Spike frequency [Hz]')
    ax.xaxis.set_major_locator(ticker.MultipleLocator(1.0))
    ax.legend(loc='upper left')
    fig.savefig('meanvariance-meansine')
def plot_meanvaradapt():
    dt = 0.001  # integration time step in seconds
    tmax = 4.0  # stimulus duration in seconds
    cutoff = 60.0  # cutoff frequency of stimulus in Hertz
    T = 1.0  # duration of segements with constant mean in seconds
    means = [1.0, 3.0, 6.0, 2.0]  # mean stimulus values for each segment
    stdevs = [1.0, 3.0, 1.5, 0.5]  # standard deviations for each segment
    rng = np.random.RandomState(583)
    stimulus = 0.5 * mv.whitenoise(0.0, cutoff, dt, tmax, rng)
    time = np.arange(len(stimulus)) * dt
    mean = np.zeros(len(stimulus))
    for k, m in enumerate(means):
        mean[(time > k * T) & (time <= (k + 1) * T)] += m
    std = np.zeros(len(stimulus))
    for k, s in enumerate(stdevs):
        std[(time > k * T) & (time <= (k + 1) * T)] += s
    stimulus *= std
    stimulus += mean
    # adaptation:
    rate1, adapt1 = mv.adaptation(time, stimulus, alpha=0.2, taua=0.5)
    rate1[rate1 < 0.0] = 0.0  # thresholding
    rate2, adapt2 = mv.divisive_adaptation(time, rate1, taua=0.3, slope=0.1)
    # plot:
    fig, axs = plt.subplots(2, 1, figsize=(figwidth, 0.6 * figwidth))
    ax = axs[0]
    ax.plot(time, stimulus, color=colors['green'], label='stimulus')
    ax.set_ylabel('Stimulus')
    ax.xaxis.set_major_locator(ticker.MultipleLocator(1.0))
    ax = axs[1]
    ax.plot(time, rate2, color=colors['blue'], label='adapting')
    ax.set_xlabel('Time [s]')
    ax.set_ylabel('Spike frequency [Hz]')
    ax.xaxis.set_major_locator(ticker.MultipleLocator(1.0))
    fig.savefig('meanvariance-meanvaradapt')
def plot_divisiveadapt():
    dt = 0.001  # integration time step in seconds
    tmax = 4.0  # stimulus duration in seconds
    cutoff = 60.0  # cutoff frequency of stimulus in Hertz
    T = 1.0  # duration of segements with constant mean in seconds
    stdevs = [0.5, 1.5, 3.0, 0.5]  # standard deviations for each segment
    rng = np.random.RandomState(583)
    stimulus = 0.5 * mv.whitenoise(0.0, cutoff, dt, tmax, rng)
    time = np.arange(len(stimulus)) * dt
    std = np.zeros(len(stimulus))
    for k, s in enumerate(stdevs):
        std[(time > k * T) & (time <= (k + 1) * T)] += s
    stimulus *= std
    stimulus[stimulus < 0.0] = 0.0
    rate, adapt = mv.divisive_adaptation(time, stimulus, taua=0.3, slope=0.1)
    # plot:
    fig, axs = plt.subplots(2, 1, figsize=(figwidth, 0.6 * figwidth))
    ax = axs[0]
    ax.plot(time, stimulus, color=colors['green'], label='stimulus')
    ax.plot(time, adapt, color=colors['red'], label='adaptation')
    ax.xaxis.set_major_locator(ticker.MultipleLocator(1.0))
    ax.set_ylabel('Adaptation')
    ax.legend(loc='upper left')
    ax = axs[1]
    ax.plot(time, rate, color=colors['blue'], label='adapting')
    ax.set_xlabel('Time [s]')
    ax.set_ylabel('Spike frequency [Hz]')
    ax.xaxis.set_major_locator(ticker.MultipleLocator(1.0))
    fig.savefig('meanvariance-divisiveadapt')
def plot_meanstimulus():
    dt = 0.001  # integration time step in seconds
    tmax = 4.0  # stimulus duration in seconds
    cutoff = 20.0  # cutoff frequency of stimulus in Hertz
    T = 1.0  # duration of segements with constant mean in seconds
    means = [0.0, 3.0, 6.0, 1.5]  # mean stimulus values for each segment
    rng = np.random.RandomState(583)
    stimulus = 0.5 * mv.whitenoise(0.0, cutoff, dt, tmax, rng)
    time = np.arange(len(stimulus)) * dt
    mean = np.zeros(len(stimulus))
    for k, m in enumerate(means):
        mean[(time > k * T) & (time <= (k + 1) * T)] += m
    stimulus += mean
    # plot stimulus:
    fig, ax = plt.subplots(figsize=(figwidth, 0.4 * figwidth))
    ax.plot(time, stimulus, color=colors['green'])
    ax.plot(time, mean, '--', color=colors['lightgreen'])
    ax.set_ylim(-1.5, 7.5)
    ax.set_xlabel('Time [s]')
    ax.set_ylabel('Stimulus')
    ax.xaxis.set_major_locator(ticker.MultipleLocator(1.0))
    fig.savefig('meanvariance-meanstimulus')
    # response:
    rate0, adapt0 = mv.adaptation(time, stimulus, alpha=0.0, taua=0.5)
    rate, adapt = mv.adaptation(time, stimulus, alpha=0.2, taua=0.5)
    # plot rate:
    fig, ax = plt.subplots(figsize=(figwidth, 0.4 * figwidth))
    ax.plot(time, rate0, color=colors['cyan'], label='non adapting')
    ax.plot(time, rate, color=colors['blue'], label='adapting')
    ax.set_ylim(0.0, 200.0)
    ax.set_xlabel('Time [s]')
    ax.set_ylabel('Spike frequency [Hz]')
    ax.xaxis.set_major_locator(ticker.MultipleLocator(1.0))
    ax.legend(loc='upper left')
    fig.savefig('meanvariance-meanresponse')
    # plot adaptation:
    fig, ax = plt.subplots(figsize=(figwidth, 0.4 * figwidth))
    ax.plot(time, adapt, color=colors['red'], label='adapting')
    ax.set_ylim(0.0, 8.0)
    ax.set_xlabel('Time [s]')
    ax.set_ylabel('Adaptation')
    ax.xaxis.set_major_locator(ticker.MultipleLocator(1.0))
    fig.savefig('meanvariance-meanadapt')
    # plot stimulus and threshold:
    fig, ax = plt.subplots(figsize=(figwidth, 0.4 * figwidth))
    ax.plot(time, stimulus, color=colors['green'], label='stimulus')
    ax.fill_between(time, adapt, -1.5, fc=colors['gray'], alpha=0.75)
    ax.plot(time, adapt, color=colors['red'], label='threshold')
    ax.set_ylim(-1.5, 7.5)
    ax.set_xlabel('Time [s]')
    ax.set_ylabel('Stimulus')
    ax.xaxis.set_major_locator(ticker.MultipleLocator(1.0))
    ax.legend(loc='upper left')
    fig.savefig('meanvariance-meanthreshold')
def plot_variancestimulus():
    dt = 0.001  # integration time step in seconds
    tmax = 4.0  # stimulus duration in seconds
    cutoff = 60.0  # cutoff frequency of stimulus in Hertz
    T = 1.0  # duration of segements with constant mean in seconds
    stdevs = [0.5, 1.5, 3.0, 0.5]  # standard deviations for each segment
    rng = np.random.RandomState(583)
    stimulus = 0.5 * mv.whitenoise(0.0, cutoff, dt, tmax, rng)
    time = np.arange(len(stimulus)) * dt
    std = np.zeros(len(stimulus))
    for k, s in enumerate(stdevs):
        std[(time > k * T) & (time <= (k + 1) * T)] += s
    stimulus *= std
    stimulus += 2.0
    # response:
    rate0, adapt0 = mv.adaptation(time, stimulus, alpha=0.0, taua=0.3)
    rate, adapt = mv.adaptation(time, stimulus, alpha=0.2, taua=0.3)
    # plot stimulus and threshold:
    fig, axs = plt.subplots(2, 1, figsize=(figwidth, 0.6 * figwidth))
    ax = axs[0]
    ax.plot(time, stimulus, color=colors['green'], label='stimulus')
    ax.fill_between(time, adapt, -1.5, fc=colors['gray'], alpha=0.75)
    ax.plot(time, adapt, color=colors['red'], label='threshold')
    ax.set_ylim(-1.5, 7.5)
    ax.set_ylabel('Stimulus')
    ax.xaxis.set_major_locator(ticker.MultipleLocator(1.0))
    ax.xaxis.set_major_formatter(ticker.NullFormatter())
    ax.legend(loc='upper left')
    # plot rate:
    ax = axs[1]
    ax.plot(time, rate0, color=colors['cyan'], label='non adapting')
    ax.plot(time, rate, color=colors['blue'], label='adapting')
    ax.set_ylim(0.0, 200.0)
    ax.set_xlabel('Time [s]')
    ax.set_ylabel('Spike frequency [Hz]')
    ax.xaxis.set_major_locator(ticker.MultipleLocator(1.0))
    ax.legend(loc='upper left')
    fig.savefig('meanvariance-variancethreshold')