#!/usr/bin/env python

import os
import json
import math
import statistics
import collections

import matplotlib.pyplot as plt

import numpy as np
from scipy.stats import gaussian_kde

result_path = "CoriResults"

def setDefaultPath(path):
    global result_path
    result_path = path

def extract(fname, useThreads=False):
    data = None
    with open(os.path.join(result_path, fname)) as f:
        data = json.load(f)
    tmp = collections.defaultdict(list)
    for r in data["results"]:
        x = r["threads"] if useThreads else r["streams"]
        th = r["throughput"]
        tmp[x].append(th)


    xs = sorted(tmp.keys())
    ys = []
    for x in xs:
        vals = tmp[x]
        m = statistics.mean(vals)
        err = 0
        if len(vals) > 1:
            err = statistics.stdev(vals) #/ math.sqrt(len(vals))
        ys.append( (m, err) )
    
    return (xs, ys)

def extractSeparate(fname, useThreads=False):
    data = None
    with open(os.path.join(result_path, fname)) as f:
        data = json.load(f)
    ret = []
    curx = []
    cury = []
    for r in data["results"]:
        x = r["threads"] if useThreads else r["streams"]
        th = r["throughput"]
        if len(curx) == 0 or x > curx[-1]:
            curx.append(x)
            cury.append( (th, 0) )
        else:
            ret.append( (curx, cury) )
            curx = [x]
            cury = [ (th, 0) ]
    ret.append( (curx, cury) )
    return ret

def extractIndex(fnames, index):
    streams = []
    throughput = []
    for fn in fnames:
        (st, th) = extract(fn)
        if index < len(st):
            streams.append(st[index])
            throughput.append(th[index])
    return (streams, (throughput, 0))

def join(lsts1, lsts2):
    return (list(lsts1[0]) + list(lsts2[0]),
            list(lsts1[1]) + list(lsts2[1]))

def ratio(lsts1, lsts2):
    retx = []
    rety = []
    for x1, y1 in zip(lsts1[0], lsts1[1]):
        y2 = [y for x,y in zip(lsts2[0], lsts2[1]) if x == x1]
        if len(y2) == 0:
            continue
        if len(y2) != 1:
            raise Exception("Unexpected happened, y2 length is %d"%len(y2))
        y2 = y2[0]
        if y2 == 0:
            continue
        retx.append(x1)
        ratio = y1[0]/y2[0]
        err = 0
        if y1[1] != 0 and y2[1] != 0:
            err = ratio * math.sqrt( (y1[1]/y1[0])**2 + (y2[1]/y2[0])**2 )
        rety.append( (ratio, err))
    return (retx, rety)

def fillAxes(inputdata, ax, title=None, custom_plt=None, legend=True, xlabel=None, ylabel=None, xlim=None, ylim=None, skipColors=0, skipColorIndices=[]):
    if xlabel is None:
        xlabel = "EDM streams/~threads"
    if ylabel is None:
        ylabel = "Throughput (events/s)"
    ax.set(xlabel=xlabel, ylabel=ylabel)
    if title is not None:
        ax.set(title=title)
    ax.grid()

    for i in range(skipColors):
        ax.plot([], [])

    ind = 0
    skip = skipColorIndices[:]
    for (x, throughput), label in inputdata:
        while len(skip) > 0 and ind == skip[0]: 
            ind += 1
            ax.plot([], [])
            del skip[0]
        #ax.plot(x, [y[0] for y in throughput], ".-", label=label)
        ax.errorbar(x,
                    [y[0] for y in throughput],
                    yerr=[y[1] for y in throughput],
                    fmt=".-", label=label)
        #for s, y in zip(x, throughput):
        #    print("%d: %f +- %f" % (s, y[0], y[1]))
        ind += 1

    if ylim is None:
        ylim = dict(bottom=0)
    if len(ylim) > 0:
        ax.set_ylim(**ylim)
    if xlim is not None:
        ax.set_xlim(**xlim)
    if legend:
        ax.legend()
    if custom_plt is not None:
        custom_plt(ax)

def makePlot(inputdata, output=None,
             xlabel=None, ylabel=None, title=None,
             legend=True,
             xlim=None, ylim=None,
             custom_plt=None):
    fig, ax = plt.subplots(1, 1)
    fillAxes(inputdata, ax, title=title, custom_plt=custom_plt,
             legend=legend, xlabel=xlabel, ylabel=ylabel,
             xlim=xlim, ylim=ylim)
    fig.show()
    if output is not None:
        fig.savefig(output+".png")
        fig.savefig(output+".pdf")

def makePlotN(inputdata, output=None, xlabel=None, ylabel=None, title=None, custom_plt=None, ncols=-1):
    figsize = plt.rcParams["figure.figsize"][:]
    nplots = len(inputdata)
    if ncols == -1:
        ncols = nplots
    nrows = nplots // ncols
    if nplots % ncols > 0:
        nrows += 1
    fig, axs = plt.subplots(nrows, ncols, squeeze=False,
                            figsize=[ncols*figsize[0],nrows*figsize[1]])
    for i, d in enumerate(inputdata):
        irow = i // ncols
        icol = i % ncols

        fillAxes(d["data"], axs[irow][icol],
                 title=d.get("title", None),
                 custom_plt=d.get("custom_plt", custom_plt),
                 legend=d.get("legend", True),
                 xlabel=d.get("xlabel", xlabel),
                 ylabel=d.get("ylabel", ylabel),
                 xlim=d.get("xlim", None),
                 ylim=d.get("ylim", None),
                 skipColors=d.get("skipColors", 0),
                 skipColorIndices=d.get("skipColorIndices", []),
                )
    fig.suptitle(title)
    fig.show()
    if output is not None:
        fig.savefig(output+".png")
#        fig.savefig(output+".pdf")
        fig.savefig(output+".svg")
#        fig.savefig(output+".emf")

def plotGangHisto(*fileHistoNames, log=True, makeRatios=False, fraction=False):
    def lastNonZero(lst):
        ret = 0
        for i, v in enumerate(lst):
            if v != 0:
                ret = i
        return ret

    datas = []
    nbins = None
    numMods = None
    trailingZeroIndex = -1
    
    for (fname, histoName) in fileHistoNames:
        data = []
        with open(os.path.join(result_path, fname)) as f:
            # skip lines not relevant for the requested case
            for line in f:
                if histoName in line:
                    break
            
            for line in f:
                s = line.split(" ")
                # stop after processing the histogram
                if len(s) <= 1:
                    break
                values = [int(x) for x in s[1:]]
                if fraction:
                    total = sum(values)
                    values = [x/total for x in values]
                data.append((s[0], values))
                nbins = max(len(s)-1, nbins) if nbins is not None else len(s)-1
                trailingZeroIndex = max(trailingZeroIndex, lastNonZero(data[-1][1])+1)
        if numMods is None:
            numMods = len(data)
        elif numMods != len(data):
            raise Exception("Inconsistency in number of modules, got %d was %d" % (len(data), numMods))
        datas.append(data)
    
    #print(nbins, trailingZeroIndex)
    def cutAndFillZeros(lst, n):
        if len(lst) >= n:
            return lst[:n]
        return lst + [0]*(n-len(lst))
    if trailingZeroIndex > 0:
        tmps = []
        for data in datas:
            tmps.append([(d[0], cutAndFillZeros(d[1], trailingZeroIndex)) for d in data])
        #print(data)
        #print(tmp)
        datas = tmps
        nbins = trailingZeroIndex
    #print(nbins)
    
    figsize = plt.rcParams["figure.figsize"][:]
    nrows = numMods//3
    if numMods%3 != 0:
        nrows += 1
    makeRatios = makeRatios and len(fileHistoNames) > 1
    if makeRatios:
        nrows = nrows*2
    ncols = min(3, numMods)
    fig, axs = plt.subplots(nrows, ncols, squeeze=True,
                            figsize=[ncols*figsize[0], nrows*figsize[1]], linewidth=1, frameon=True)
    width = 0.7
    barwidth = width / len(fileHistoNames)
    
    for idata, (data, (fname, histoName)) in enumerate(zip(datas, fileHistoNames)):
        for imod, mod in enumerate(data):
            irow = imod//3
            if makeRatios:
                irow *= 2
            icol = imod%3
            ax = axs[irow][icol]
    
            xvals = np.arange(1,nbins+1)
            ax.bar(xvals-width/2+barwidth/2+barwidth*idata, mod[1], barwidth, log=log)
        
            if idata == 0:
                ax.set(ylabel="Total number of gangs" if not fraction else "Fraction of gangs", xlabel="Gang size (number of events / gang)")
                ax.text(0.3, 0.8, mod[0], transform=ax.transAxes)
                
                if makeRatios:
                    irow += 1
                    axr = axs[irow][icol]
                    axr.set(ylabel="Other / blue", xlabel="Gang size (number of events / gang)")
                    axr.plot([], []) # to skip blue
                    denom = mod[1]
                    for    i in range(1, len(datas)):
                        numer = datas[i][imod][1]
                        axr.plot(xvals, [n/d if d != 0 else 0 for n,d in zip(numer, denom)], ".-")
    fig.suptitle(fname+" "+histoName)
    fig.show()
