def GenerateHighPredictionGraph(net, data, config, directory, fname):
    global reportinfo
    # region Measure Prediction
    best = 0
    secondbest = 0
    xaxis = ""
    yaxis = ""
    for inputa in net.inputs:
        if inputa in reportinfo.usedpredictability:
            continue
        score = 0
        for item in data["data"]:
            net.reset()
            for key in item:
                if key == inputa:
                    net.setNode(key, m.unitscale(data["inputs"][key]["min"], item[key], data["inputs"][key]["max"]))
                elif key in net.inputs:
                    net.setNode(key, 0)
            net.process()
            for out in net.outputs:
                val = item[out]
                if config["test-mode"] != "SimplePosi":
                    val = net.scale(out, item[out])
                score += ge.Test_Output(net.getOutput()[out], val, config["test-mode"])

        if score > best:
            yaxis = xaxis
            xaxis = inputa
            secondbest = best
            best = score
        elif score > secondbest:
            yaxis = inputa
            secondbest = score

    if yaxis == "":
        return "<p>Not enough variables for graph.</p>"
    # endregion
    #region Handle graph
    fig = pyplot.figure()
    plt = fig.add_subplot(111, projection='3d')

    if not config["add-data"]:
        MLGraphing.GraphNet(net, xaxis, yaxis, plt)
    else:
        MLGraphing.GraphNetData(net, data, xaxis, yaxis, net.classifier_output, 0, config["clump"], config["percents"], config["color"], plt)

    if config["customize"]:
        pyplot.show()
    fig.savefig(directory + '/' + fname, bbox_inches='tight')
    pyplot.close(fig)
    out = GenerateGraphHTML(net, config["header"], fname, xaxis, yaxis, config["add-data"], config["color"],
                            config["clump"], config["percents"], reportinfo.usedpredictability)
    if reportinfo.totalaccuracy is None:
        reportinfo.totalaccuracy = ge.Test_Obj(net, data["data"], config["comparison-mode"]) * 100 /len(data["data"])
    localscore = best*100 / len(data["data"])
    out += ("<p>This gets " + str(round(localscore, 2)) + "% of the data items correct." +
            "This is " + str(round(localscore * 100 / reportinfo.totalaccuracy, 2)) + "% of the max accuracy.</p>\n")
    reportinfo.usedpredictability.append(xaxis)
    reportinfo.usedpredictability.append(yaxis)
    return out
def Graph(config):
    netfile = config["net-file"]
    net = gn.loadNets(netfile)[0][0]

    usedata = config["use-data"]

    xaxis = config["xaxis"]
    yaxis = config["yaxis"]
    zaxis = config["zaxis"]

    fig = pyplot.figure()
    plt = fig.add_subplot(111, projection='3d')
    if usedata:
        datafile = config["data-file"]
        colormode = config["color-mode"]
        useclumping = config["clump"]
        usepercents = config["usepercents"]
        with open(datafile) as f:
            data = json.load(f)
        MLGraph.GraphNetData(net, data, xaxis, yaxis, zaxis, 0, useclumping,
                             usepercents, colormode, plt)
    else:
        MLGraph.GraphNet(net, xaxis, yaxis, plt)

    pyplot.show()
def GenerateHighVarianceGraph(net, data, config, directory, fname):
    global reportinfo
    #region Measure Variance
    best = 0
    secondbest = 0
    xaxis = ""
    yaxis = ""
    for inputa in net.inputs:
        if inputa in reportinfo.usedvariance:
            continue
        outputs = []
        x = -1
        while x <= 1:
            net.reset()
            for name in net.inputs:
                if name == inputa:
                    net.setNode(name, x)
                else:
                    net.setNode(name, 0)
            net.process()
            outputs.append(net.getOutput()[net.classifier_output])
            x += 0.1
        dif = max(outputs) - min(outputs)
        if dif > best:
            yaxis = xaxis
            xaxis = inputa
            secondbest = best
            best = dif
        elif dif > secondbest:
            yaxis = inputa
            secondbest = dif
    if yaxis == "":
        return "<p>Not enough variables to create graph.</p>\n"
    #endregion
    fig = pyplot.figure()
    plt = fig.add_subplot(111, projection='3d')
    if not config["add-data"]:
        MLGraphing.GraphNet(net, xaxis, yaxis, plt)
    else:
        MLGraphing.GraphNetData(net, data, xaxis, yaxis, net.classifier_output, 0, config["clump"], config["percents"], config["color"], plt)
    if config["customize"]:
        pyplot.show()
    fig.savefig(directory + '/' + fname, bbox_inches='tight')
    pyplot.close(fig)
    out = GenerateGraphHTML(net, config["header"], fname, xaxis, yaxis, config["add-data"], config["color"],
                            config["clump"], config["percents"], reportinfo.usedvariance)
    reportinfo.usedvariance.append(xaxis)
    reportinfo.usedvariance.append(yaxis)
    return out
def creategraph(fig,
                net,
                inputdict,
                outputdict,
                title,
                defvalue=0,
                data=None,
                percents=False,
                colormode=0,
                clump=False):
    #we can assume that inputs = 2, and outputs > 1:
    #region scan inputs and outputs
    xaxis = ""
    yaxis = ""
    for name in inputdict:
        if inputdict[name].state:
            if xaxis == "":
                xaxis = name
            elif yaxis == "":
                yaxis = name
            else:
                print("Error, too many inputs")

    outputs = []
    for name in outputdict:
        if outputdict[name].state:
            outputs.append(name)

    if data != {} and data is not None:
        grapher.GraphNetData(net,
                             data,
                             xaxis,
                             yaxis,
                             net.classifier_output,
                             defvalue,
                             clump,
                             percents,
                             colormodes[colormode],
                             fig,
                             title=title)
    else:
        grapher.GraphNetOutputs(net, xaxis, yaxis, outputs, fig, title=title)
def GenerateCustomNetDataGraph(net, data, config, directory, fname):
    global reportinfo
    fig = pyplot.figure()
    plt = fig.add_subplot(111, projection='3d')
    MLGraphing.GraphNetData(net, data, config["x"], config["y"], net.classifier_output, 0, config["clump"], config["percents"], config["color"], plt)
    if config["customize"]:
        pyplot.show()
    fig.savefig(directory + '/' + fname, bbox_inches='tight')
    pyplot.close(fig)
    out = GenerateGraphHTML(net, config["header"], fname, config["x"], config["y"], True, config["color"], config["clump"],
                            config["percents"], [])
    return out
def GenerateCustomNetGraph(net, config, directory, fname):
    global reportinfo
    if config["x"] is None or config["y"] is None:
        warnings.warn("Custom net graph missing configuration")
    fig = pyplot.figure()
    plt = fig.add_subplot(111, projection='3d')
    MLGraphing.GraphNet(net, config["x"], config["y"], plt)
    if config["customize"]:
        pyplot.show()
    fig.savefig(directory + '/' + fname, bbox_inches = 'tight')
    pyplot.close(fig)
    out = GenerateGraphHTML(net, config["header"], fname, config["x"], config["y"], False, "", False, False, [])
    return out
def GenerateNonLinearVarianceGraph(net, data, config, directory, fname):
    #region measure variance
    variance = []
    nonlinear = []
    inputs = []
    for inputa in net.inputs:
        if inputa in reportinfo.nonlinear:
            continue
        outputs = []
        x = -1
        while x <= 1:
            net.reset()
            for name in net.inputs:
                if name == inputa:
                    net.setNode(name, x)
                else:
                    net.setNode(name, 0)
            net.process()
            outputs.append(net.getOutput()[net.classifier_output])
            x += 0.1
        variance.append(max(outputs)-min(outputs))
        inputs.append(inputa)
        nonlinear.append((3 < outputs.index(max(outputs)) < 18 or 3 < outputs.index(min(outputs)) < 18))
    #endregion
    #region find nonlinear
    count = 0
    for item in nonlinear:
        if item:
            count += 1
    target = 0
    secondtarget = 0
    xaxis = ""
    yaxis = ""
    bonustext = ""
    if count == 0:
        out = "<h3>" + config["header"] + "</h3>\n<p>None found"
        if len(reportinfo.nonlinear) > 0:
            out +=  "(excluding: " + ', '.join(reportinfo.nonlinear) +")"
        out += "</p>\n"
        return out

    if count == 2:
        for i in range(0, len(nonlinear)):
            if nonlinear[i] and xaxis == "":
                xaxis = inputs[i]
            else:
                yaxis = inputs[i]

    if count > 2:
        for i in range(0, len(nonlinear)):
            if nonlinear[i]:
                if variance[i] > target:
                    yaxis = xaxis
                    secondtarget = target
                    xaxis = inputs[i]
                    target = variance[i]
                elif variance[i] > secondtarget:
                    yaxis = inputs[i]
                    secondtarget = variance[i]
        bonustext = "<p>More than 2 non-linear inputs. We picked the ones with the most variance</p>\n"

    if count == 1:
        secondtarget = max(variance)
        for i in range(0, len(nonlinear)):
            if nonlinear[i]:
                xaxis = inputs[i]
                target = variance[i]
        for i in range(0, len(nonlinear)):
            if inputs[i] != xaxis and abs(variance[i] - target) < secondtarget:
                secondtarget = abs(variance[i] - target)
                yaxis = inputs[i]
        bonustext= "<p>Only one non-linear input (" + xaxis +"). We picked a second input with similar variance.</p>\n"
    #endregion

    fig = pyplot.figure()
    plt = fig.add_subplot(111, projection='3d')
    if not config["add-data"]:
        MLGraphing.GraphNet(net, xaxis, yaxis, plt)
    else:
        MLGraphing.GraphNetData(net, data, xaxis, yaxis, net.classifier_output, 0, config["clump"], config["percents"], config["color"], plt)

    if config["customize"]:
        pyplot.show()
    fig.savefig(directory + '/' + fname, bbox_inches='tight')
    pyplot.close(fig)
    out = GenerateGraphHTML(net, config["header"], fname, xaxis, yaxis, config["add-data"], config["color"],
                            config["clump"], config["percents"], reportinfo.nonlinear)
    reportinfo.nonlinear.append(xaxis)
    reportinfo.nonlinear.append(yaxis)
    return out + bonustext