import numpy as np
import logging

from allensdk.internal.model.glif.find_spikes import find_spikes_list

from allensdk.ephys.extract_cell_features import get_stim_characteristics

def least_squares_RCEl_calc_tested(voltage_list, current_list, dt):
    '''Calculate resistance, capacitance and resting potential by performing 
    least squares on current and voltage.
    
    Parameters
    ----------
    voltage_list: list of arrays 
        voltage responses for several sweep repeats
    current_list: list of arrays 
        current injections for several sweep repeats
    dt: float
        time step size in voltage and current traces
    
    Returns
    -------
    r_list: list of floats
        each value corresponds to the resistance of a sweep
    c_list: list of floats 
        each value corresponds to the capacitance of a sweep
    el_list: list of floats 
        each value corresponds to the resting potential of a sweep
        '''    

    r_list=[]
    c_list=[]
    el_list=[]
    for voltage, current in zip(voltage_list, current_list):
        matrix=np.ones((len(voltage)-1, 3))
        matrix[:,0]=voltage[0:len(voltage)-1]
        matrix[:,1]=current[0:len(current)-1]
        lsq_non_der_raw=np.linalg.lstsq(matrix, voltage[1:])[0]
#        (r_lsq_non_der_raw, c_lsq_non_der_raw, El_lsq_non_der_raw)=RCEL_from_standard_space(lsq_non_der_raw) 
        
        c_lsq_non_der_raw=dt/lsq_non_der_raw[1]
        r_lsq_non_der_raw=-lsq_non_der_raw[1]/(lsq_non_der_raw[0]-1)
        El_lsq_non_der_raw=-lsq_non_der_raw[2]/(lsq_non_der_raw[0]-1)   
        r_list.append(r_lsq_non_der_raw)
        c_list.append(c_lsq_non_der_raw)
        el_list.append(El_lsq_non_der_raw)
    
    return r_list, c_list, el_list


