import matplotlib  # these lines to
matplotlib.use('Agg')  # work remotely
import numpy as np
import pylab as pl
from sys import path
path.append('../')
from scipy.sparse import csr_matrix
from NetPop import NetPop
import cfunctions as cfn
from functions import simpleaxis, errorfill, init_fig

init_fig()

net = NetPop(1)
W = np.copy(net.W)
dt = 1

R0 = net.R4flatPi(net.pstart_state)
Rmax = np.dot(net.calc_Qvalue().max(axis=1), net.pstart_state)

ref = 20
step = .2
rate = 400

## offline ##
try:
    perf = np.load('results/performance.npy')
except IOError:
    try:
        S = np.load('results/spikes.npz')['S']
    except IOError:
import numpy as np
import pylab as pl
from sys import argv, path
path.append('../')
from NetPop import NetPop, NetPopPredict
from functions import simpleaxis, smooth_spikes, accumulate, init_fig

savefig = False if len(argv) == 1 else True

init_fig()
pl.rc('legend', **{'fontsize': 30})
# colors for colorblind from http://www.cookbook-r.com/Graphs/Colors_(ggplot2)/
colRT = ["#009E73", "#0072B2", "#D55E00", "#E69F00",
         "#56B4E9", "#F0E442", "#CC79A7", "#999999"]
# orange to green to cyan:
col = map(tuple, [np.array([.902, .624, 0]) +
                  (np.array([0, .62, .451]) -
                   np.array([.902, .624, 0])) * i / 4 for i in range(5)]) +\
    map(tuple, [np.array([0, .62, .451]) +
                (np.array([.337, .706, .914]) -
                 np.array([0, .62, .451])) * i / 3 for i in range(1, 4)])


def runpopU(W, uinit, step, pop_size, rate=100,
            T=700, tm=50, ts=2, reset=3, delay=0):
    K = len(W)
    spikes = np.zeros((T / step, K))
    spikes[0, K - pop_size:] = 1
    fs = np.exp(-step / ts)  # decay factor synapse
    fm = np.exp(-step / tm)  # decay factor membrane
    # combine factor to save computing time