import numpy as np
import os

def computeLinearSlope(x,y,idxStart=0,idxStop=-1,maxCurvature = 100.,stepSize=0.001):
    nanIdx = np.where(np.isfinite(y) == 0)[0]
    try:
        xR = x[idxStart:nanIdx[0]]
        yR = y[idxStart:nanIdx[0]]
    except(IndexError):
        xR = x[idxStart:]
        yR = y[idxStart:]
    if idxStop:
        curvature = computeCurvature(xR,yR)
        for i in curvature:
            if abs(i)>maxCurvature:
                idxStop = curvature.index(i)
                if idxStop>int(0.01/stepSize):break
        if not idxStop: idxStop = len(yR)-1
    # slope of linear fit
    z0 = np.polyfit(xR[:idxStop], yR[:idxStop], 1, full=True)
    # print idxStop
    # import matplotlib.pyplot as plt
    # plt.plot(yR*1000)
    # plt.plot(curvature)
    # plt.show()
    if not z0[1].size: RMS = 0.
    else: RMS = z0[1][0]
    return z0[0][0],RMS,idxStop+idxStart

def computeCurvature(x,y):
    stiffness = list()
    for idx in range(len(x)):
        stiffness.append(computeDerivative(y,x,idx))
    curvature = list()
    for idx in range(len(x)):
        curvature.append(computeDerivative(stiffness,x,idx,3))    
    curvature = savitzky_golay(curvature, 5, 3)
    return list(curvature)
    
def getEndOfLinearity(x,y,idxStart,idxStop,linearResidual):
    #defines the beginning of damage = end of linearity
    #iterate over strain range to find stress/strain values for which the fit residual is diverging (20%) from what was considered good for linear fit
    #returns the index of the strain
    nanIdx = np.where(np.isfinite(y) == 0)[0]
    try:
        xR = x[idxStart:nanIdx[0]]
        yR = y[idxStart:nanIdx[0]]
    except(IndexError):
        xR = x[idxStart:]
        yR = y[idxStart:]
    newResidual = linearResidual
    while (newResidual<1.2*linearResidual and idxStop<len(yR)-1):
        idxStop += 1
        z1 = np.polyfit(xR[:idxStop], yR[:idxStop], 1, full=True)
        if not z1[1].size: newResidual = 0.
        else: newResidual = z1[1][0]
    idxStop -= 1
    return idxStop+idxStart

def getMacroFailure(endFullStrain,endFullStress,stepSize=0.001):
    nbFailure=0
    # getting all parts with consecutive negative slope
    consecNeg = getConseqNegSlope(endFullStrain,endFullStress)
    # if consecutive negative slope for more than 0.5% strain, then it is local failure
    for array in consecNeg: 
        if len(array)>int(0.005/stepSize):nbFailure+=1
    nbFailure -= 1
    # macro failure is the last of the consecutive negative slope parts that is at least 0.1% strain long
    for array in reversed(consecNeg):
        if len(array)>int(0.01/stepSize):
            breakStrain = endFullStrain[array[0]:array[-1]+1]
            breakStress = endFullStress[array[0]:array[-1]+1]
            break
    else:
        breakStrain = endFullStrain[-(int(0.01/stepSize)):-1]
        breakStress = endFullStress[-(int(0.01/stepSize)):-1]
    return breakStrain,breakStress,nbFailure

def getBeginningOfFailure(endFullStrain,endFullStress,stepSize=0.001):
    #gets index of beginning of local failure
    #local failure = end of simple damage range
    #local failure is defined as the first strain level for which the slope of stress/stain (stored in tangent modulus) is negative for at least a chosen strain range (0.5% strain)
    failureStrain = 0
    failureStress = 0
    consecNeg = getConseqNegSlope(endFullStrain,endFullStress)
    for array in consecNeg: 
        if len(array)>int(0.01/stepSize):#at least 0.5% strain consecutive negative slope
            failureStrain = endFullStrain[array[0]]
            failureStress = endFullStress[array[0]]
            break#we're interested only at the first instance of local failure
    if not failureStrain:
        failureStress = endFullStress[-1]
        failureStrain = endFullStrain[-1]
    failureIdx = np.where(endFullStress == failureStress)[0][0]#+1
    return failureIdx

def getConseqNegSlope(endFullStrain,endFullStress):
    tgtModulus = np.diff(endFullStress) / np.diff(endFullStrain)
    itemindex = np.where(tgtModulus<0.)[0]
    return np.array_split(itemindex,np.where(np.diff(itemindex)!=1)[0]+1)
    
def computeDerivative(y,x,point,kernel=0):
    der = 0
    if point == 0:#Euler forwards for the first point
        der = (y[point]-y[point+1])/(x[point]-x[point+1])
    elif kernel == 0:#Euler backwards for a zero kernel
        der = (y[point]-y[point-1])/(x[point]-x[point-1])
    elif (point-kernel>0) and (point+kernel)<len(y):#mean of central derivatives of max kernel size
        for i in range(kernel):
            der += (y[point-(i+1)]-y[point+(i+1)])/(x[point-(i+1)]-x[point+(i+1)])
        der /= kernel
    else:der=computeDerivative(y,x,point,kernel-1)#compute derivative for smaller kernel at the extremities
    return der
    
def savitzky_golay(y, window_size, order, deriv=0, rate=1):
    r"""Smooth (and optionally differentiate) data with a Savitzky-Golay filter.
    The Savitzky-Golay filter removes high frequency noise from data.
    It has the advantage of preserving the original shape and
    features of the signal better than other types of filtering
    approaches, such as moving averages techniques.
    Parameters
    ----------
    y : array_like, shape (N,)
        the values of the time history of the signal.
    window_size : int
        the length of the window. Must be an odd integer number.
    order : int
        the order of the polynomial used in the filtering.
        Must be less then `window_size` - 1.
    deriv: int
        the order of the derivative to compute (default = 0 means only smoothing)
    Returns
    -------
    ys : ndarray, shape (N)
        the smoothed signal (or it's n-th derivative).
    Notes
    -----
    The Savitzky-Golay is a type of low-pass filter, particularly
    suited for smoothing noisy data. The main idea behind this
    approach is to make for each point a least-square fit with a
    polynomial of high order over a odd-sized window centered at
    the point.
    Examples
    --------
    t = np.linspace(-4, 4, 500)
    y = np.exp( -t**2 ) + np.random.normal(0, 0.05, t.shape)
    ysg = savitzky_golay(y, window_size=31, order=4)
    import matplotlib.pyplot as plt
    plt.plot(t, y, label='Noisy signal')
    plt.plot(t, np.exp(-t**2), 'k', lw=1.5, label='Original signal')
    plt.plot(t, ysg, 'r', label='Filtered signal')
    plt.legend()
    plt.show()
    References
    ----------
    .. [1] A. Savitzky, M. J. E. Golay, Smoothing and Differentiation of
       Data by Simplified Least Squares Procedures. Analytical
       Chemistry, 1964, 36 (8), pp 1627-1639.
    .. [2] Numerical Recipes 3rd Edition: The Art of Scientific Computing
       W.H. Press, S.A. Teukolsky, W.T. Vetterling, B.P. Flannery
       Cambridge University Press ISBN-13: 9780521880688
    """
    import numpy as np
    from math import factorial

    try:
        window_size = np.abs(np.int(window_size))
        order = np.abs(np.int(order))
    except ValueError, msg:
        raise ValueError("window_size and order have to be of type int")
    if window_size % 2 != 1 or window_size < 1:
        raise TypeError("window_size size must be a positive odd number")
    if window_size < order + 2:
        raise TypeError("window_size is too small for the polynomials order")
    order_range = range(order+1)
    half_window = (window_size -1) // 2
    # precompute coefficients
    b = np.mat([[k**i for i in order_range] for k in range(-half_window, half_window+1)])
    m = np.linalg.pinv(b).A[deriv] * rate**deriv * factorial(deriv)
    # pad the signal at the extremes with
    # values taken from the signal itself
    firstvals = y[0] - np.abs( y[1:half_window+1][::-1] - y[0] )
    lastvals = y[-1] + np.abs(y[-half_window-1:-1][::-1] - y[-1])
    y = np.concatenate((firstvals, y, lastvals))
    return np.convolve( m[::-1], y, mode='valid')
    
################################################################
################################################################

def getStiffness(sheet,stepSize,minStrain,maxStrain):
    ### reads data in excel sheet
    geoData,force,extension = readSheetForce(sheet)
    minIdx = int(minStrain/stepSize)
    stepSize *= max(extension)
    maxE = maxStrain*max(extension)
    ### interpolate stress over regular strain grid
    ### (strain from 0 to max user strain and not max data strain so that all data are interpolated over the same range - needed for stats and easier for plots)
    fullExtRange = np.arange(0.,maxE+stepSize,stepSize)
    fullLoadRange = np.interp(fullExtRange, extension, force, right=np.nan)
    nanIdx = np.where(np.isfinite(fullLoadRange) == 0)[0]
    ## STIFFNESS
    ks0,residual,newMax = computeLinearSlope(fullExtRange, fullLoadRange, minIdx,stepSize=stepSize)
    return ks0,newMax,geoData,fullLoadRange,residual
    
def readSheet(sheet):
    #print sheet.name
    listOfNeededValues = ['Width','Length','Thickness','Area','NbLamella','Tensile stress MPa','Tensile strain %']
    listOfFoundValues = list()
    #reads excel sheet and returns useful values
    max_rows = sheet.nrows
    max_cols = sheet.ncols
    geoData = {}
    for row in range(max_rows):
        cellValue = sheet.cell(row,0).value
        if cellValue == 'Thickness': 
            listOfFoundValues.append(cellValue)
            geoData[cellValue] = sheet.cell(row,1).value
        elif cellValue == 'Area': 
            geoData[cellValue] = sheet.cell(row,1).value
            listOfFoundValues.append(cellValue)
        elif cellValue == 'NbLamella':
            geoData[cellValue] = sheet.cell(row,1).value
            listOfFoundValues.append(cellValue)
        elif cellValue == 'Width':
            geoData[cellValue] = sheet.cell(row,1).value
            listOfFoundValues.append(cellValue)
        elif cellValue == 'Length':
            geoData[cellValue] = sheet.cell(row,1).value
            listOfFoundValues.append(cellValue)
        else:
            for col in range(max_cols):
                cellValue = sheet.cell(row,col).value
                if cellValue == 'Tensile stress MPa':
                    fulStress = [sheet.cell(r,col).value for r in range(row+1,max_rows)]                   
                    listOfFoundValues.append(cellValue)
                elif cellValue == 'Tensile strain %':
                    fulStrain = [sheet.cell(r,col).value for r in range(row+1,max_rows)]
                    listOfFoundValues.append(cellValue)
    assert sorted(listOfFoundValues) == sorted(listOfNeededValues), "the excel sheet needs to contain 'Thickness','Area','NbLamella','Tensile stress MPa','Tensile strain %' data"
    return geoData,fulStress,fulStrain

def readSheetForce(sheet):
    listOfNeededValues = ['Width','Length','Thickness','Area','NbLamella','Load N','Extension mm']
    listOfFoundValues = list()
    #reads excel sheet and returns useful values
    max_rows = sheet.nrows
    max_cols = sheet.ncols
    geoData = {}
    for row in range(max_rows):
        cellValue = sheet.cell(row,0).value
        if cellValue == 'Thickness': 
            listOfFoundValues.append(cellValue)
            geoData[cellValue] = sheet.cell(row,1).value
        elif cellValue == 'Area': 
            geoData[cellValue] = sheet.cell(row,1).value
            listOfFoundValues.append(cellValue)
        elif cellValue == 'NbLamella':
            geoData[cellValue] = sheet.cell(row,1).value
            listOfFoundValues.append(cellValue)
        elif cellValue == 'Width':
            geoData[cellValue] = sheet.cell(row,1).value
            listOfFoundValues.append(cellValue)
        elif cellValue == 'Length':
            geoData[cellValue] = sheet.cell(row,1).value
            listOfFoundValues.append(cellValue)
        else:
            for col in range(max_cols):
                cellValue = sheet.cell(row,col).value
                if cellValue == 'Load N':
                    fulForce = [sheet.cell(r,col).value for r in range(row+1,max_rows)]                   
                    listOfFoundValues.append(cellValue)
                elif cellValue == 'Extension mm':
                    fulExtension = [sheet.cell(r,col).value for r in range(row+1,max_rows)]
                    listOfFoundValues.append(cellValue)
    assert sorted(listOfFoundValues) == sorted(listOfNeededValues), "the excell sheet needs to contain 'Thickness','Area','NbLamella','Load N','Extension mm' data"
    return geoData,fulForce,fulExtension
    
def computeStats(listOflist,dir=1):
    listOflist = prepDataForStats(listOflist)
    return np.nanmean(listOflist,dir),np.nanstd(listOflist,dir),np.nanmin(listOflist,dir),np.nanmax(listOflist,dir)

def prepDataForStats(listOflist):
    maxL = 0
    notIn = list()
    newList = list()
    for i,data in enumerate(listOflist):
        try:
            maxL = max(maxL,len(data))
        except(TypeError):notIn.append(i)
    for i,data in enumerate(listOflist):
        try:data = list(data)
        except(TypeError):continue
        if i not in notIn:
            data.extend([np.nan]*(maxL-len(data))) 
            newList.append(data)
    return newList
    
def rmse(values,targets):
    return np.sqrt(np.square(np.divide((values-targets),targets)/len(values)).sum(0))

def rsquare(values,targets):
    from scipy import stats
    slope, intercept, r_value, p_value, std_err = stats.linregress(values,targets)
    return r_value**2
    
def getFailureValues(failStrain,failStress,ext,name='Failure'):
    firstFStrain = list()
    firstFStress = list()
    firstDropStress = list()
    fileName = '%s_%s.csv'%(name,ext)
    os.remove(fileName) if os.path.exists(fileName) else None
    for strain,stress in zip(failStrain, failStress):
        if len(strain)>0:
            firstFStrain.append(strain[0])
            firstFStress.append(stress[0])
            firstDropStress.append((stress[0]-stress[-1])*1000.)
            with open(fileName,'a') as file:
                file.write('%f, %f, %f\n'%(strain[0],stress[0],(stress[0]-stress[-1])*1000.))
    nos = len(firstFStrain)
    means = (np.mean(firstFStrain),np.mean(firstFStress),np.mean(firstDropStress))
    stds = (np.std(firstFStrain),np.std(firstFStress),np.std(firstDropStress))
    range = ([np.min(firstFStrain),np.max(firstFStrain)],[np.min(firstFStress),np.max(firstFStress)],[np.min(firstDropStress),np.max(firstDropStress)])
    return nos,means,stds,range

def getDamageValues(dStrain,dStress,ext):
    fileName = 'Damage_%s.csv'%ext
    os.remove(fileName) if os.path.exists(fileName) else None
    for strain,stress in zip(dStrain, dStress):
        with open(fileName,'a') as file:
            file.write('%f, %f\n'%(strain,stress))
    nos = len(dStrain)
    means = (np.mean(dStrain),np.mean(dStress))
    stds = (np.std(dStrain),np.std(dStress))
    return nos,means,stds    