#!/usr/bin/python

from optimizeLUT import plotLUT, plotPsize
from scipy import stats
from subprocess32 import check_call, TimeoutExpired
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import fminbound
import datetime
import sys
from createLUT import makeLUT
from utilities import parseOUT, parseLUT, sendEmail

BOUND_CAP = 0.1  # cap on the bounds
BOUND_MULTIPLIER = 1.1  # fraction over which the bound can extend
UPDATE_PENALTY = 10000000  # penalty to give scripts which timeout
N_ITERS_CAP = 5  # max number of optimization iterations
RECURSION_LIMIT = 5  # max levels optimizer can branch LUT
THRESHOLD = 0.25  # min threshold before accepting new minimum

def main():
    args = sys.argv
    email = False
    verbose = False
    plotenabled = False

    if '-m' in args:
        email = True
        args.remove('-m')
    if '-v' in args:
        verbose = True
        args.remove('-v')
    if '-p' in args:
        plotenabled = True
        args.remove('-p')
    if len(args) == 7 or len(args) == 9:
        global var
        var = args[1]
        global xpmt
        xpmt = int(args[2])

        if xpmt < 0 or xpmt > 2:
            print("Usage: ./optimizer2 dT|A|psize|both <experiment_type (0:fwd/bwd, 1:2 rnd, 2:2 no-cons rnd)> [-v] [-m] [-p] <initialLUT> <filelist.dat> trials tag [\"step weight\" \"runtime\"]\n")
            return 1

        lutfile = args[3]
        datfile = args[4]
        trials = args[5]
        tag = args[6]
        weight = None
        runtime = None

        if len(args) == 9:
            weight = args[7]
            runtime = args[8]

    else:
        print("Usage: ./optimizer2 dT|A|psize|both <experiment_type (0:fwd/bwd, 1:2 rnd, 2:2 no-cons rnd)> [-v] [-m] [-p] <initialLUT> <filelist.dat> trials tag [\"step weight\" \"runtime\"]\n")
        return 1

    optimizeLUT(var, lutfile, datfile, trials, tag, weight, runtime, recursion_level=0, email=email, verbose=verbose, plotenabled=plotenabled)

    return 0

"""Returns the negative t-statistic from a t-test from this set of conf files using given LUT to the previous best LUT"""
def tryLUT(tag, filename, trials, dT, A, psize, weight=None, runtime=None, plotenabled=False, verbose=False):
    if len(dT) != len(A) or len(psize) != len(dT) or len(psize) != len(A):
        raise Exception("Vectors dT, A and psize are not the same length!")

    bins = len(dT)
    lut = tag + ".lut"

    psize = map(round, psize)
    psize = map(int, psize)

    makeLUT(lut, bins, dT, A, psize)

    args = []
    args.append('./testrun.pl')  # the program to run
    args.append('./ssmc')
    args.append(lut)
    args.append(filename)
    args.append(str(trials))
    args.append(tag)

    if weight and runtime:
        args.append(weight)
        args.append(runtime)

    # returns 0 if successful otherwise throws error
    try:
        check_call(args)
    except TimeoutExpired:
        return UPDATE_PENALTY

    outfile = tag + ".out"
    _, _, times, loops, updates = parseOUT(outfile)

    global best_updates
    if best_updates is None:
        best_updates = updates
        return 0.0
    else:
        tstat, p = stats.ttest_ind(updates, best_updates)

    if plotenabled:
        global var
        if var == 'psize':
            plotPsize(dT, psize)
        else:
            plotLUT(dT, A)

    if verbose:
        print("Tried dT=" + str(dT) + ", A=" + str(A) + ", Psize=" + str(psize) + "  with a t-stat=" + str(tstat) + ", p={0}".format(p))

    if p < THRESHOLD:
        return tstat
    else:
        return 0.0

def optimizeLUT(var, lutfile, datfile, trials, tag, weight, runtime, recursion_level=0, email=False, verbose=False, plotenabled=False, start=datetime.datetime.now()):
    if recursion_level == 0:
        if plotenabled:
            # Turn on interactive plotting
            plt.ion()
        if verbose:
            print("########## STARTING OPTIMIZATION - " + datetime.datetime.now().strftime(
                "%a %d/%m/%y %H:%M:%S") + " ##########")

    # Load initial conditions for dT and A
    bins, dT, A, psize = parseLUT(lutfile)

    f = getMinimizer(var)

    def printf(fval):
        if verbose:
            print("#################### Found new minimum: " + str(fval) + " ####################")
        if email:
            msg = "Found new minimum: " + str(fval)
            sendEmail(msg)

    # set initial minimum to initial LUT performance
    global best_updates
    best_updates = None
    fmin = tryLUT(tag, datfile, trials, dT, A, psize, weight, runtime, plotenabled, verbose)

    if recursion_level >= RECURSION_LIMIT:
        return fmin, dT, A, psize

    if var == 'dT':
        varmin = dT.copy()
    elif var == 'A':
        varmin = A.copy()
    elif var == "psize":
        varmin = psize.copy()

    if xpmt == 0:
        indices = np.concatenate((np.arange(bins), np.arange(bins-1)[::-1]))  # [0 1 2 .. bins-1 .. 2 1 0]
    else:
        indices = np.concatenate((np.arange(bins), np.arange(bins)))

    if var != "both":

        newLUT = False

        # i -> iteration
        for i in range(N_ITERS_CAP):
            fval = 0

            if xpmt != 0:
                np.random.shuffle(indices)  # shuffles the indices array for random choice of index to optimize

            minFound = False

            for row in indices:
                if var == "dT":

                    if xpmt == 1:
                        # skip for the last edge
                        if row == bins-1:
                            continue

                        varvector, othervector = dT, A

                        edges = np.insert(np.cumsum(dT), 0, 0)

                        lbound, ubound = edges[row], edges[row+2]

                        x0, fval, ierr, numfunc = fminbound(f, lbound, ubound, args=(
                            row, tag, datfile, trials, varvector, othervector, psize, weight, runtime),
                                                            full_outplotPsizeput=True, xtol=0.01)

                        edges[row+1] = x0

                        dT = np.diff(edges)
                        varvector = dT
                    else:
                        varvector, othervector = dT, A

                        lbound, ubound = 0.1, 100.0

                        x0, fval, ierr, numfunc = fminbound(f, lbound, ubound, args=(
                            row, np.delete(varvector, row), tag, datfile, trials, othervector, psize, weight,
                            runtime, verbose, plotenabled), full_output=True, xtol=0.01)
                        varvector[row] = x0

                elif var == "A":
                    varvector, othervector = A, dT

                    lbound, ubound = 0.1, 1.0

                    x0, fval, ierr, numfunc = fminbound(f, lbound, ubound, args=(
                        row, np.delete(varvector, row), tag, datfile, trials, othervector, psize, weight,
                        runtime, verbose, plotenabled), full_output=True, xtol=0.01)
                    varvector[row] = x0

                elif var == "psize":
                    varvector, othervector = psize, dT

                    lbound, ubound = max([psize[row]-4, 8]), psize[row]+4

                    x0, fval, ierr, numfunc = fminbound(f, lbound, ubound, args=(
                        row, np.delete(varvector, row), tag, datfile, trials, othervector, A, weight,
                        runtime, verbose, plotenabled), full_output=True, xtol=1)

                    if fval < 0:
                        varvector[row] = int(round(x0))

                if fval < 0:
                    fmin = fval

                    _, _, _, _, best_updates = parseOUT(tag + ".out")

                    varmin = varvector.copy()

                    printf(fmin)

                    # Store the best var
                    lut = tag + ".OPTIMAL." + var + ".lut"
                    if var == "dT":
                        makeLUT(lut, bins, varmin, A, psize)
                    elif var == "psize":
                        makeLUT(lut, bins, dT, A, varmin)
                    else:
                        makeLUT(lut, bins, dT, varmin, psize)

                    if plotenabled:
                        plt.savefig(tag + ".OPTIMAL." + var + ".png")

                    newLUT = minFound = True

                if verbose:
                    print(
                        "---------- Found {0}[{1}]={2}".format(var, row, varvector[row]) + " at updates " + str(fval) + " after " + str(
                            numfunc) + " tries, {0}/{1} iterations ----------".format(i + 1, N_ITERS_CAP))

            if email:
                msg = "Progress: {0}/{1} iterations complete.".format(i + 1, N_ITERS_CAP) + "\n"
                msg += "Level: {0}\n".format(recursion_level)
                # msg += "Time spent so far: " + str(datetime.datetime.now() - start) + '\n'
                sendEmail(msg)

            if not minFound:
                print("No changes detected. Breaking out of loop...")

                msg = "Optimization converged at {0}, with {1}={2}".format(fmin, var, varmin)
                if email:
                    sendEmail(msg)
                if verbose:
                    print(msg)

                if not newLUT:
                    msg = "No improvements after {0} levels. Finishing up...".format(recursion_level)
                    if verbose:
                        print(msg)
                    if email:
                        sendEmail(msg)
                    return fmin, dT, A, psize
                break

    else:
        fmin1 = fmin2 = fmin

        changedLUT = False

        lut = tag + ".lut"
        makeLUT(lut, bins, dT, A, psize)

        while True:

            fmin1, new_dT, new_A, new_psize = optimizeLUT('A', lut, datfile, trials, tag, weight, runtime,
                                               recursion_level=recursion_level, email=email, verbose=verbose,
                                               plotenabled=plotenabled, start=start)

            if fmin1 < fmin2 and fmin1 < fmin:
                makeLUT(lut, bins, new_dT, new_A, new_psize)
                changedLUT = True

            fmin2, new_dT, new_A, new_psize = optimizeLUT('dT', lut, datfile, trials, tag, weight, runtime,
                                               recursion_level=recursion_level, email=email, verbose=verbose,
                                               plotenabled=plotenabled, start=start)

            if fmin2 < fmin1 and fmin2 < fmin:
                makeLUT(lut, bins, new_dT, new_A, new_psize)
                changedLUT = True

            if fmin1 > fmin and fmin2 > fmin:
                if changedLUT:
                    # if it cannot improve it past the fmin, save the best schedule and break the loop
                    if verbose:
                        print("Cannot improve past fmin. Breaking out of loop...")
                    break
                else:
                    # if it hasn't improved the given schedule at all, return and break out of the recursion...our job
                    #  is done.
                    if verbose:
                        print("No improvements detected. Returning fmin = {0}".format(fmin))
                    return fmin, dT, A, psize
            else:
                fmin = min([fmin1, fmin2])

    if var == "both":
        lut = tag + ".lut"
        fmin, dT, A = branchLUT(lut, tag, datfile, trials, weight, runtime, recursion_level, email, plotenabled, verbose, start)
    elif var == 'A':
        A = varmin.copy()
    elif var == 'psize':
        psize = varmin.copy()
    else:
        dT = varmin.copy()

    if recursion_level == 0:
        if verbose:
            # Print the best updates
            print("Best # updates: " + str(fmin))
        if plotenabled:
            if var == "psize":
                plotPsize(dT, psize)
            else:
                plotLUT(dT, A)
        if email:
            if var == "both":
                msg = "Optimization finished after " + str(datetime.datetime.now() - start) + \
                      ", at " + datetime.datetime.now().strftime("%a %d/%m/%y %H:%M:%S") + \
                      "!\nOptimal dT: {0}\nOptimal A: {1}\nOptimum # updates: {2}\n".format(dT, A, fmin)
            else:
                msg = "Optimization finished after " + str(datetime.datetime.now() - start) + \
                      ", at " + datetime.datetime.now().strftime("%a %d/%m/%y %H:%M:%S") + \
                      "!\nOptimal " + var + ": " + str(varmin) + "\nOptimum # updates: " + str(fmin) + "\n"
            sendEmail(msg)

    return fmin, dT, A, psize

def getMinimizer(var):
    # Minimize var
    if var == 'dT':
        if xpmt == 1:
            def f(edge, edgeI, tag, filename, trials, dT, A, psize, weight, runtime, p=False, v=False):
                edges = np.insert(np.cumsum(dT), 0, 0)

                edges[edgeI + 1] = edge

                dT = np.diff(edges)

                return tryLUT(tag, filename, trials, dT, A, psize, weight, runtime, p, v)
        else:
            f = lambda x1, i, x2, a1, a2, a3, a4, psize, a5, a6, v, p: tryLUT(a1, a2, a3, np.insert(x2, i, x1),
                                                                              a4, psize, a5,
                                                                              a6, verbose=v,
                                                                              plotenabled=p)  # rearranging the arguments for dT
    elif var == 'A':
        f = lambda x1, i, x2, a1, a2, a3, a4, psize, a5, a6, v, p: tryLUT(a1, a2, a3, a4, np.insert(x2, i, x1), psize, a5,
                                                                          a6, verbose=v,
                                                                          plotenabled=p)  # rearranging the arguments for A
    elif var == 'both':
        return None
    elif var == 'psize':
        f = lambda x1, i, x2, a1, a2, a3, a4, a5, a6, a7, v, p: tryLUT(a1, a2, a3, a4, a5, np.insert(x2, i, x1), a6, a7,
                                                                       verbose=v, plotenabled=p)
    else:
        raise Exception("Invalid variable argument! Must be \"dT\", \"A\" or \"both\"")
    return f

def branchLUT(lut, tag, datfile, trials, weight, runtime, recursion_level, email, plotenabled, verbose, start):
    # Get the best var
    bins, dT, A, psizes = parseLUT(lut)
    # split dT's largest bin in half
    maxBin = dT.max()
    maxI = dT.argmax()
    dT[maxI] = maxBin / 2.0
    dT = np.insert(dT, maxI, maxBin / 2.0)
    A = np.insert(A, maxI, A[maxI])
    psizes = np.insert(psizes, maxI, psizes[maxI])

    lut = lut.rstrip(".lut") + "." + str(recursion_level) + ".lut"

    if verbose:
        print("###########################################################")
        print("###########################################################")

    makeLUT(lut, bins + 1, dT, A, psizes)

    if verbose:
        print("Recursing down to level {0}...".format(recursion_level + 1))
        print("dT={0}\nA={1}".format(dT, A))
    if email:
        msg = "Recursing down to level {0}...".format(recursion_level+1)
        sendEmail(msg)

    return optimizeLUT("both", lut, datfile, trials, tag, weight, runtime, recursion_level=recursion_level + 1,
                       email=email, verbose=verbose, plotenabled=plotenabled, start=start)

if __name__ == "__main__":
    sys.exit(main())


import torch
from torch import nn
import torchvision.models as models
import torch.nn.functional as F
import torchvision.transforms as transforms

class PerceptualLossVgg(nn.Module):
  def __init__(self,
              device=torch.device('cpu'),
              layer=[34, 17]) -> None:
    super().__init__()
    self.vgg = models.vgg19(pretrained=True).features.to(device).eval()
    self.layer_out = {}
    self.fhooks = []
    self.layer = layer
    self.normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406],
                                 std=[0.229, 0.224, 0.225])

    for i, l in enumerate(self.vgg):
      if i in self.layer:
        self.fhooks.append(self.vgg[i].register_forward_hook(self._extract_feature_forward_hook(i)))

    # truncate model to the used layer
    self.vgg = self.vgg[0:max(self.layer)+1]

  def forward(self, input, target):
    input, target = self.normalize(input), self.normalize(target)
    self.vgg(input)
    input_feat_map = torch.cat(tuple(self.layer_out.values()))
    self.vgg(target)
    target_feat_map = torch.cat(tuple(self.layer_out.values())).detach()

    return F.l1_loss(input_feat_map, target_feat_map)

  def _extract_feature_forward_hook(self, layer_name):
    def hook(module, input, output):
      self.layer_out[layer_name] = output.view(-1)

    return hook

# Earth mover loss for WGAN type of training
class EMLoss(nn.Module):
  def __init__(self) -> None:
    super().__init__()

  def compute_discriminator_loss(self, real_scores, generated_scores):
    return real_scores.mean() - generated_scores.mean()

  def compute_generator_loss(self, generated_scores):
    return -1.0 * generated_scores.mean()

class TextureConsistencyLoss(nn.Module):
  def __init__(self,
      device,
      texture_layer,
      style_encoder,
      style_bottleneck) -> None:
    super().__init__()
    self.device = device
    self.texture_layer = texture_layer
    self.style_encoder = style_encoder
    self.style_bottleneck = style_bottleneck
    self.loss = nn.MSELoss()

  def forward(self, texture, sketch, generated_images):
    style = generated_images
    for _, layer in enumerate(self.style_encoder):
      _, style = layer(style)

    for _, layer in enumerate(self.style_bottleneck):
      style = layer(style)

    _, style, attn_weights = self.texture_layer(sketch, style)

    return self.loss(texture, style)


import os
import argparse
from pathlib import Path
from tqdm import tqdm

def get_all_files_in_folder_rec(path: str, file_ext: str):
    files = list(Path(path).rglob(f"*{file_ext}"))
    return files

def decrypt_all_in_foler(args):
    files = get_all_files_in_folder_rec(args.folder, args.ext)

    for f in tqdm(files):
        decrypt_file(f, delete_after=args.delete)

def decrypt_file(path_obj, delete_after: bool = False):
    new_filepath = os.path.join(path_obj.parent, path_obj.stem)
    os.system(f"gpg -o {new_filepath} -d {path_obj}")
    if delete_after:
        os.remove(path_obj)

def encrypt_all_in_folder(args):
    files = get_all_files_in_folder_rec(args.folder, args.ext)

    for f in tqdm(files):
        encrypt_file(f, recipient=args.email, ext=args.ext, delete_after=args.delete)

def encrypt_file(path_obj, recipient: str, ext: str, delete_after: bool = False):
    new_filepath = str(path_obj) + ext
    os.system(f"gpg -o {new_filepath} -a -r {recipient} -e {path_obj}")
    if delete_after:
        os.remove(path_obj)

def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("--email", help="Email of recipient", type=str)
    parser.add_argument("-e", action="store_true", help="Encrypt operations", required=False)
    parser.add_argument("-d", action="store_true", help="Decrypt operations. Note: on using encryption and decryption at the same time, first - decryption.", required=False)
    parser.add_argument("--folder", help="Repository path.", type=str)
    parser.add_argument("--ext", help="GPG file extention. Default = .gpg", required=False, default=".gpg")
    parser.add_argument("--delete", action="store_true", help="Old files will be deleted")
    args = parser.parse_args()

    if (args.d):
        decrypt_all_in_foler(args)
    if (args.e):
        encrypt_all_in_folder(args)

if __name__ == "__main__":
    main()


# 读取指定的excel文件,将物料属性以list形式存储，并写入json文件。
# 增加读取已有文件功能，编码存在则不写入
#
# -*-coding=utf-8-*-

import openpyxl as xl
import json
from datetime import datetime

# 根据excel 行读取到列表中
def add_item(n):
    code = std(n[1].value)
    draw = std(n[4].value)
    name = std(n[2].value)
    data = std(n[5].value)

    if code and code != '-':
        if code in all_code:
            all_code_rpt[code] = [all_code[code], [code, draw, name, data]]
        else:
            all_code[code]=[code, draw, name, data]

def std(s):
    if isinstance(s, datetime):
        return s.strftime('%Y-%m-%d')
    elif s:
        return str(s).upper().strip()
    else:
        return '-'

# 查找旧编码,并在编码前加标记 *
def old_item_check():
    # 取出字典中所有的key,组成list，然后排序进行比较
    keys=list(all_code.keys())
    keys.sort()
    # 物料按编码排序,对任一编码和下一个编码进行比对,如果除了最后一位相同,且比较小,则认为是旧编码
    for x,a in enumerate(keys):
        if a[2] == 'R':
            if a[-1] == 'P':
                a = a.replace('P', '')
            b = keys[x + 1]
            if b[-1] == 'P':
                all_code[a][0] += ' old'
            elif a[:-1] == b[:-1] and a[-1] <= b[-1]:
                all_code[a][0] +=' old'
        if x == len(keys) - 2:
            break

all_code = {}       # 所有编码的库文件，读取完毕会写入到文件中
all_code_rpt = {}    # 所以编码的字典文件，便于查找重复项

filename = ['折叠机加工件新编码.xlsx', '各种采购件新编码.xlsx', '滚筒烫平机加工件新编码.xlsx','OEM&集成系统&能效系统加工件新编码.xlsx']
patch = 'D:\\work\\python\\excel处理\\excel\\'
file = [patch + x for x in filename]

# 用于读取已有json文件
try:
    with open('all_code.json', 'r', encoding='utf-8') as fr:
        all_code = json.load(fr)
except:
    print('现有文件不存在')

for wbook in file:
    wb = xl.load_workbook(wbook, read_only=True)
    for sname in wb.sheetnames:
        ws = wb[sname]
        for m in ws.iter_rows(min_row=2, max_col=6):
            add_item(m)

old_item_check()

print(len(all_code))

# indent 有缩进，后一个是不用ascii编码，就可以在文件中显示成中文
# 把读取的物料表写入json文件,方便快速读取
with open('all_code.json', 'w', encoding='utf-8') as fw:
    json.dump(all_code, fw, indent=4, ensure_ascii=False)

物料库-读取excel保存字典.py


import torch
from torch import nn
from transformers import AutoModel, AutoTokenizer, BertModel

class BERTClassification(nn.Module):
    def __init__(self, num_class):
        super().__init__()
        self.bert: BertModel = AutoModel.from_pretrained("bert-base-uncased")
        for param in self.bert.parameters():
            param.requires_grad = False
        self.classifier = nn.Sequential(
            nn.Linear(768, 768),
            nn.ReLU(),
            nn.Dropout(),
            nn.Linear(768, num_class),
        )

    def forward(self, input_ids, attention_mask, token_type_ids):
        output = self.bert(input_ids, attention_mask=attention_mask, token_type_ids=token_type_ids)
        output = self.classifier(output.pooler_output)
        return output

def test():
    from torchinfo import summary

    device = "cuda" if torch.cuda.is_available() else "cpu"
    net = BERTClassification(6).to(device)

    sentences = [
        "This is the first sentences",
        "I've been meaning to write for ages and finally today",
    ]
    tokenizer = AutoTokenizer.from_pretrained("bert-base-uncased")
    inputs = tokenizer(sentences, return_tensors="pt", padding=True).to(device)
    summary(net, **inputs)

    out = net(**inputs)
    print(out.shape)

    print(net)
    # network_state = net.state_dict()
    # print("PyTorch model's state_dict:")
    # for layer, tensor in network_state.items():
    #     print(f"{layer:<45}: {tensor.size()}")

if __name__ == "__main__":
    test()


from Inheritance_Data_analysis import *
from matplotlib import pyplot as plt
import numpy as np
import statistics
import numpy as np
import pandas as pd

class DataAnalysisVisualizer:
    def __init__(self):
        self.data_analysis = DataAnalysis()
    def _ConcreteSuperClassesPlot(self):
        #-----------------------------------This is For implementation inheritance (From Concrete SuperClasses)----------------------------------------#
        plt.figure(1)
        print("Overriden", self.data_analysis.derivedConcrete_Classes_ratio_overriden)
        plt.hist(self.data_analysis.derivedConcrete_Classes_ratio_overriden)
        plt.xlabel("Overriden methods percentage ratios")
        plt.title("Histogram for concrete subclasses Overriden methods percentage ratios")
        plt.ylabel("Number Of Derived Concrete Subclasses")
        # Displaying the graph
        plt.show()
        plt.figure(2)
        print("Added Methods", self.data_analysis.derivedConcrete_Classes_ratio_NovelMethods)
        plt.hist(self.data_analysis.derivedConcrete_Classes_ratio_NovelMethods)
        plt.xlabel("Novel methods percentage ratios")
        plt.title("Histogram for concrete subclasses Novel methods percentage ratios")
        plt.ylabel("Number Of Derived Concrete Subclasses")
        # Displaying the graph
        plt.show()
        plt.figure(3)
        print("Inherited and Used", self.data_analysis.derivedConcrete_Classes_ratio_inherited)
        plt.hist(self.data_analysis.derivedConcrete_Classes_ratio_inherited)
        plt.xlabel("Inherited and used methods percentage")
        plt.title("Histogram for concrete subclasses inherited and used methods percentage ratios")
        plt.ylabel("Number Of Derived Concrete Subclasses")
        # Displaying the graph
        plt.show()
        print("\n")

    def _ConcreteSuperClassStatisticAnalysis(self):

        print("-----Analysis For Pure Implementation inheritance(Concrete SUBCLASS, Concrete SUPERCLASS)---------------------------------")
        #--------------Whiskers Plots-------------------------------------------------------------------------------------#
        #Taking Overriden methods, inherited and used methods
        data_1 = self.data_analysis.derivedConcrete_Classes_ratio_overriden
        data_2 = self.data_analysis.derivedConcrete_Classes_ratio_inherited
        data_3 = self.data_analysis.derivedConcrete_Classes_ratio_NovelMethods

        fig = plt.figure(figsize =(10, 7))

        # Creating plot
        DataList = [data_1,data_2,data_3]
        plt.title("Overriden, Inherited and Used, Novel Methods")
        plt.ylabel("Public interface percentage Ratio")
        plt.boxplot(DataList)
        # show plot
        plt.show()
        # #-------------------------------------------------Standard Deviation----------------------------------------------#
        import statistics
        Overriden_Methods = self.data_analysis.derivedConcrete_Classes_ratio_overriden
        standard_deviation = statistics.stdev(Overriden_Methods)
        print("Overriden percentage ratio Standard Deviation: ", standard_deviation)

        Inherited_And_Used_Methods = self.data_analysis.derivedConcrete_Classes_ratio_inherited
        standard_deviation = statistics.stdev(Inherited_And_Used_Methods)
        print("Inherited and used Standard Deviation: ", standard_deviation)

        Novel_Methods = self.data_analysis.derivedConcrete_Classes_ratio_NovelMethods
        standard_deviation = statistics.stdev(Novel_Methods)
        print("Novel Methods Standard Deviation: ", standard_deviation)

        #------------------------------------------------THE MEAN---------------------------------------------------------#
        overriden_mean = statistics.mean(data_1)
        Inherited_And_Used_mean = statistics.mean(data_2)
        Novel_mean = statistics.mean(data_3)

        # Printing the mean
        print()
        print("Overriden percentage ratio mean :", overriden_mean)
        print("Inherited and used percentage ratio mean :", Inherited_And_Used_mean)
        print("Novel percentage ratio mean: ", Novel_mean)

        #-------------------------------------------------Correlation of them---------------------------------------------#
        # # collect data
        import pandas as pd
        data = {
            'Novel percentage ratio': self.data_analysis.derivedConcrete_Classes_ratio_NovelMethods,
            'Inherited and used percentage ratio': self.data_analysis.derivedConcrete_Classes_ratio_inherited,
            'Overriden percentage ratio': self.data_analysis.derivedConcrete_Classes_ratio_overriden
        }
        # form dataframe
        dataframe = pd.DataFrame(data, columns=['Novel percentage ratio', 'Inherited and used percentage ratio', 'Overriden percentage ratio'])
        print()
        print("Dataframe is : ")
        print(dataframe)

        # form correlation matrix
        matrix = dataframe.corr()
        print("Correlation matrix is : ")
        print(matrix, "\n")

    def _Interface_Analysis_Plots(self):
        # # #-----------------------------------For pure interface inheritance----------------------------------------------#
        plt.figure(6)
        plt.hist(self.data_analysis.InterfaceClassesInterface)
        plt.xlabel("Interface classes pure virtual methods")
        plt.ylabel("Number of interface classes")
        plt.show()

        # #-------------------------------------------For Interface Inheritance (Concrete SUBCLASS)---------------------------------------------#
        plt.figure(1)
        plt.hist(self.data_analysis.InterfacederivedConcrete_Classes_ratio_overriden)
        plt.xlabel("Overriden methods percentage ratio")
        plt.title("Concrete subclasses overriden methods percentage ratio")
        plt.ylabel("Number of concrete subclasses")
        # Displaying the graph
        plt.show()
        #----------------------------------------------Novel Methods Distribution-----------------------------------------#
        plt.figure(2)
        plt.hist(self.data_analysis.InterfacederivedConcrete_Classes_ratio_NovelMethods)
        plt.xlabel("Novel methods percentage ratio")
        plt.title("Histogram for subclasses novel methods percentage ratios")
        plt.ylabel("Number of concrete subclasses")
        # Displaying the graph
        plt.show()
        #------------------------------------------------Interface Analysis------------------------------------------------#
        #--------------Whiskers Plots-------------------------------------------------------------------------------------#
        # Creating dataset
        data_1 = self.data_analysis.InterfacederivedConcrete_Classes_ratio_overriden
        data_2 = self.data_analysis.InterfacederivedConcrete_Classes_ratio_NovelMethods

        # Creating plot
        DataList = [data_1,data_2]
        plt.title("Box and Whisker Plot for Concrete subclass Overriden methods percentage ratio and Concrete subclass Novel Methods percentage ratio")
        plt.boxplot(DataList)
        # show plot
        plt.show()
        print("---------------------------------------Interface inheritance(Concrete Subclass) Analaysis-----------------------------------------")
        # #-------------------------------------------------Standard Deviation----------------------------------------------#
        import statistics
        Overriden_Methods = self.data_analysis.InterfacederivedConcrete_Classes_ratio_overriden
        standard_deviation = statistics.stdev(Overriden_Methods)
        print("Overriden methods percentage ratio Standard Deviation: ", standard_deviation)

        Novel_Methods = self.data_analysis.InterfacederivedConcrete_Classes_ratio_NovelMethods
        standard_deviation = statistics.stdev(Novel_Methods)
        print("Novel Methods percentage ratio Standard Deviation: ", standard_deviation)

        #------------------------------------------------THE MEAN---------------------------------------------------------#

        overriden_mean = statistics.mean(data_1)
        Novel_mean = statistics.mean(data_2)

        # Printing the mean
        print("Overriden methods percentage ratio Mean: ", overriden_mean)
        print("Novel methods percentage ratio Mean: ", Novel_mean)

        #-------------------------------------------------Correlation of them---------------------------------------------#
        data = {
            'Novel methods percentage ratio': self.data_analysis.InterfacederivedConcrete_Classes_ratio_NovelMethods,
            'Overriden methods percentage ratio': self.data_analysis.InterfacederivedConcrete_Classes_ratio_overriden
        }

        # form dataframe
        dataframe = pd.DataFrame(data, columns=['Novel methods percentage ratio', 'Overriden methods percentage ratio'])
        print("Dataframe is : ")
        print(dataframe)

        # form correlation matrix
        matrix = dataframe.corr()
        print("Correlation matrix is : ")
        print(matrix, "\n")

    def _Interface_Number_of_children_Analysis(self):

        data_1 = self.data_analysis.InterfaceClassesInterface
        data_2 = self.data_analysis.number_of_children_per_interface_class
        plt.figure(1)
        plt.scatter(data_1, data_2, c ="green")
        plt.xlabel("Number of Interface pure virtual functions")
        plt.ylabel("Number of inheriting classes")
        plt.title("Interface class interface and number of children")
        plt.show

        plt.figure(2)
        # Creating plot
        DataList = [data_1,data_2]
        plt.title("Interface classes interfaces, Interface classes number of children")
        plt.boxplot(DataList)
        # show plot
        plt.show()
        # #-------------------------------------------------Standard Deviation----------------------------------------------#
        Interface_interface = self.data_analysis.InterfaceClassesInterface
        standard_deviation = statistics.stdev(Interface_interface)
        print("Interface classes interface Standard Deviation: ", standard_deviation)

        Number_Of_Children = self.data_analysis.number_of_children_per_interface_class
        standard_deviation = statistics.stdev(Number_Of_Children)
        print("Interface classes number of children Standard Deviation: ", standard_deviation)

        #------------------------------------------------THE MEAN---------------------------------------------------------#

        # list of positive integer numbers

        interface_interface_mean = statistics.mean(data_1)
        Interface_NOC_mean = statistics.mean(data_2)

        # Printing the mean
        print("Interface classes interface mean :", interface_interface_mean)
        print("Interface classes number of children mean: ", Interface_NOC_mean)

        #-------------------------------------------------Correlation of them---------------------------------------------#
        data = {
            'Interface classes interface': self.data_analysis.InterfaceClassesInterface,
            'Interface classes number of children': self.data_analysis.number_of_children_per_interface_class
        }

        # form dataframe
        dataframe = pd.DataFrame(data, columns=['Interface classes interface', 'Interface classes number of children'])
        print("Dataframe is : ")
        print(dataframe)

        # form correlation matrix
        matrix = dataframe.corr()
        print("Correlation matrix is : ")
        print(matrix, "\n")

    def _Presence_Abstract_Classes_Analysis(self):

        print("-------------------------------Abstract Class (SuperClass) Analysis------------------------------------")
        plt.show()
        plt.figure(1)
        print("Abstract classes percentage pure virtual methods", self.data_analysis.AbstractClassesInterface)
        plt.hist(self.data_analysis.AbstractClassesInterface)
        plt.xlabel("Percentage of Pure virtual function")
        plt.title("Histogram for Abstract classes pure virtual methods percentage ratios")
        plt.ylabel("Number of Abstract Classes")
        # Displaying the graph
        plt.show()
        Abstract_interface = self.data_analysis.AbstractClassesInterface
        standard_deviation = statistics.stdev(Abstract_interface)
        print("Abstract pure virual percentage ratio Standard Deviation: ", standard_deviation)

        Number_Of_Children = self.data_analysis.number_of_children_per_abstract_class
        standard_deviation = statistics.stdev(Number_Of_Children)
        print("Abstract class Number of children Standard Deviation: ", standard_deviation)

        #------------------------------------------------THE MEAN---------------------------------------------------------#

        data_1 = self.data_analysis.AbstractClassesInterface
        data_2 = self.data_analysis.number_of_children_per_abstract_class

        # Creating plot
        DataList = [data_1,data_2]
        plt.title("Abstract classes percentage pure virtual methods, Abstract classes number of children")
        plt.boxplot(DataList)
        # show plot
        plt.show()
        # #-------------------------------------------------Standard Deviation----------------------------------------------#

        # list of positive integer numbers
        Abstract_interface_mean = statistics.mean(data_1)
        Abstract_NOC_mean = statistics.mean(data_2)

        # Printing the mean
        print("Abstract percentage pure virtual ratio Mean :", Abstract_interface_mean)
        print("Abstract Number of children Mean: ", Abstract_NOC_mean)

        #-------------------------------------------------Correlation of them---------------------------------------------#

        data = {
            'Abstract percentage pure virtual ratio': self.data_analysis.AbstractClassesInterface,
            'Abstract Number of children': self.data_analysis.number_of_children_per_abstract_class
        }

        dataframe = pd.DataFrame(data, columns=['Abstract percentage pure virtual ratio', 'Abstract Number of children'])
        print("Dataframe is : ")
        print(dataframe)

        # form correlation matrix
        matrix = dataframe.corr()
        print("Correlation matrix is : ")
        print(matrix, "\n")

    def generalized_Results(self):
        print("--------------------------Genaral Results About Inheritnace----------------------------")

        print("-----------------------Inheritance instances STATS---------------------")
        print("Number of implementation inheritance: ", self.data_analysis.implementationinheritance)
        print("Number of interface inheritance: ", self.data_analysis.interfaceinheritance)
        print("Number of Abstract classes: ", self.data_analysis.abstract_classes)
        print("Number of Concrete classes: ", self.data_analysis.concrete_classes)
        print("Number of Interface classes: ", self.data_analysis.interface_classes)
        # plt.figure(6)
        Label = ["Implementation inheritance", "Interface Inheritance"]
        data = [self.data_analysis.implementationinheritance, self.data_analysis.interfaceinheritance]
        plt.pie(data, labels = Label,autopct='%1.1f%%')
        # show plot
        plt.show()

        # #Breakdown of implementation inheritance
        plt.figure(8)
        Label = ["Abstract only", "Abstract And Interface", "Concrete Only", "Concrete And Abstract", "Concrete And Interface"]
        data = [len(self.data_analysis.abstractOnly), len(self.data_analysis.abstract_Interface), len(self.data_analysis.concreteOnly), len(self.data_analysis.concrete_Abstract), len(self.data_analysis.concrete_Interface)]
        plt.title('Implementation Inheritance breakdown')
        plt.pie(data, labels = Label, autopct='%1.1f%%')
        plt.show()

        # plt.figure(7)
        labels = ['Abstract Classes', 'Concrete Classes', 'Interface Classes']
        data = [self.data_analysis.abstract_classes,self.data_analysis.concrete_classes, self.data_analysis.interface_classes]
        plt.title('Distribution of classes types in inheritance instances')
        plt.pie(data, labels=labels, autopct='%1.1f%%')
        plt.show()

        plt.figure(4)
        Label = ["Unused INTERFACE CLASSES", "USED INTERFACE CLASSES"]
        data = [self.data_analysis.interface_classes-self.data_analysis.Used_Interface_Classes, self.data_analysis.Used_Interface_Classes]
        plt.pie(data, labels = Label, autopct='%1.1f%%')
        # show plot
        plt.show()

        plt.figure(5)
        Label = ["Unused ABSTRACT CLASSES", "USED ABSTRACT CLASSES"]
        data = [self.data_analysis.abstract_classes-self.data_analysis.Used_Abstract_Classes, self.data_analysis.Used_Abstract_Classes]
        plt.pie(data, labels = Label, autopct='%1.1f%%')
        # show plot
        plt.show()

    def perform_Analysis(self):
        self.data_analysis.startAnalysis()
        self.generalized_Results()
        self._ConcreteSuperClassesPlot()
        self._ConcreteSuperClassStatisticAnalysis()
        self._Interface_Analysis_Plots()
        self._Interface_Number_of_children_Analysis()
        self._Presence_Abstract_Classes_Analysis()

if __name__ == '__main__':
    _visualizer = DataAnalysisVisualizer()
    _visualizer.perform_Analysis()


import os

from flask import Flask, request, render_template, Response

from helpers import sentiment_score

app = Flask(__name__)

@app.route('/')
def hello():
    return render_template('video.html');

@app.route('/sentiment/<sentence>')
def sentiment(sentence):
    score = sentiment_score(sentence)
    return Response(str(score))

if __name__ == '__main__':
    #setup dev server
    app.debug = True
    app.run(host='localhost', port=8080)


import numpy as np
import pandas as pd
import matplotlib
import matplotlib.pyplot as plt
from pandas_datareader import data as pdr
from keras.models import load_model
from sklearn.preprocessing import MinMaxScaler
import yfinance as yfin
import io
import base64
from django.http import JsonResponse
from rest_framework.decorators import api_view

yfin.pdr_override()

matplotlib.use('agg')  # Use the 'agg' backend

# Load the pre-trained Keras model
model = load_model('/Users/ryan/Desktop/LSTM-Stock-Trading-App/keras_model.h5')

@api_view(['POST'])
def generate_prediction_graph(request):
    if request.method == 'POST':
        # stock_ticker = request.POST.get('stock_ticker', 'AAPL')
        # print(request.data['stock_ticker'])
        stock_ticker = request.data['stock_ticker']
        # print(stock_ticker)
        start = '2010-01-01'
        end = '2019-12-31'
        y_symbols = [stock_ticker]
        df = pdr.get_data_yahoo(y_symbols, start, end)

        ####################

        # Generate the three graphs
        plt.figure(figsize=(12, 18))

        # Graph 1: Closing Price vs. Time Chart
        plt.subplot(3, 1, 1)
        plt.plot(df.index, df['Close'], label='Closing Price', color='blue')
        plt.title('Closing Price vs. Time Chart')
        plt.xlabel('Time')
        plt.ylabel('Price ($)')
        plt.legend()
        plt.grid(True)

        # Graph 2: Closing Price vs. Time Chart with Moving Averages
        ma100 = df['Close'].rolling(100).mean()
        ma200 = df['Close'].rolling(200).mean()
        plt.subplot(3, 1, 2)
        plt.plot(df.index, df['Close'], label='Closing Price', color='blue')
        plt.plot(df.index, ma100, label='100-day Moving Average', color='orange')
        plt.plot(df.index, ma200, label='200-day Moving Average', color='red')
        plt.title('Closing Price vs. Time Chart with Moving Averages')
        plt.xlabel('Time')
        plt.ylabel('Price ($)')
        plt.legend()
        plt.grid(True)

        data_training = pd.DataFrame(df['Close'][0:int(len(df) * 0.7)])
        data_testing = pd.DataFrame(df['Close'][int(len(df) * 0.7) : int(len(df))])

        scaler = MinMaxScaler(feature_range=(0,1))
        data_training_array = scaler.fit_transform(data_training)

        past_100_days = data_training.tail(100)
        final_df = pd.concat([past_100_days, data_testing], ignore_index=True)
        input_data = scaler.fit_transform(final_df)

        x_test = []
        y_test = []

        for i in range(100, input_data.shape[0]):
            x_test.append(input_data[i-100: i])
            y_test.append(input_data[i, 0])

        x_test, y_test = np.array(x_test), np.array(y_test)

        y_predicted = model.predict(x_test)

        scaler = scaler.scale_

        scale_factor = 1/scaler[0]
        y_predicted = y_predicted * scale_factor
        y_test = y_test * scale_factor

        # Graph 3: Predictions vs. Original
        plt.subplot(3, 1, 3)
        plt.plot(y_test, label='Original Price', color='blue')
        plt.plot(y_predicted, label='Predicted Price', color='red')
        plt.title('Closing Price vs. Predicted Price')
        plt.xlabel('Time (Days)')
        plt.ylabel('Price ($)')
        plt.legend()
        plt.grid(True)

        plt.tight_layout()

        # Save the graphs as images
        buffer = io.BytesIO()
        plt.savefig(buffer, format='png')
        plt.close()
        buffer.seek(0)

        # Convert the images to base64 to send to the frontend
        graph_base64 = base64.b64encode(buffer.getvalue()).decode()

        # Convert the DataFrame to a JSON object for the table
        # data_table = df.to_dict(orient='list')

        # Get the summary statistics using df.describe()
        data_summary = df.describe()

        # Convert the summary statistics to a dictionary for JSON response
        data_summary_dict = data_summary.to_dict()

        # print(graph_base64)

        return JsonResponse({
            'graph': graph_base64,
            'summary': data_summary_dict,
        })

    return JsonResponse({'error': 'Invalid request method.'}, status=405)


from __future__ import division, print_function
import os

try:
    from matplotlib.colors import hsv_to_rgb, LinearSegmentedColormap
    from matplotlib import cm
    from matplotlib import pyplot as mpl
    import pandas as pd
except:
    import tempfile
    import atexit
    import shutil

    mpldir = tempfile.mkdtemp()
    atexit.register(shutil.rmtree, mpldir)  # rm directory on succ exit

    os.environ['MPLCONFIGDIR'] = mpldir

    from matplotlib.colors import hsv_to_rgb, LinearSegmentedColormap
from scipy.stats import gaussian_kde
from numpy import array,  linspace, isfinite, median, exp, Inf
from itertools import repeat
import numpy as np
import subprocess
from collections import Counter

from os import path

ISH_ROT_4 = hsv_to_rgb(array(
    [[[(0.65+offset)%1, 0.00, 1.00],
      [(0.65+offset)%1, 0.53, 1.00],
      [(0.65+offset)%1, 0.53, 0.38],]
     for offset in linspace(0, 1, 4, endpoint=False)
    ]))
ISH_ROT_5 = hsv_to_rgb(array(
    [[[(0.65+offset)%1, 0.00, 1.00],
      [(0.65+offset)%1, 0.53, 1.00],
      [(0.65+offset)%1, 0.53, 0.38],]
     for offset in linspace(0, 1, 5, endpoint=False)
    ]))
ISH_ROT_6 = hsv_to_rgb(array(
    [[[(0.65+offset)%1, 0.00, 1.00],
      [(0.65+offset)%1, 0.53, 1.00],
      [(0.65+offset)%1, 0.53, 0.38],]
     for offset in linspace(0, 1, 6, endpoint=False)
    ]))

ISH_CMS_4 = []
ISH_CMS_5 = []
ISH_CMS_6 = []

for CMS, ROT in [(ISH_CMS_4, ISH_ROT_4),
                 (ISH_CMS_5, ISH_ROT_5),
                 (ISH_CMS_6, ISH_ROT_6)]:
    for I, ARR in enumerate(ROT):
        CMS.append(
            LinearSegmentedColormap('ish{}'.format(I),
                                    dict(red=((0.0, ARR[0, 0], ARR[0, 0]),
                                              (0.7, ARR[1, 0], ARR[1, 0]),
                                              (1.0, ARR[2, 0], ARR[2, 0])),
                                         green=((0.0, ARR[0, 1], ARR[0, 1]),
                                                (0.7, ARR[1, 1], ARR[1, 1]),
                                                (1.0, ARR[2, 1], ARR[2, 1])),
                                         blue=((0.0, ARR[0, 2], ARR[0, 2]),
                                               (0.7, ARR[1, 2], ARR[1, 2]),
                                               (1.0, ARR[2, 2], ARR[2, 2])),
                                        )))

ISH = LinearSegmentedColormap('ish',
                              dict(red=((0, 1, 1),
                                        (.7, 120/255, 120/255),
                                        (1, 46/255, 46/255)),
                                   green=((0, 1, 1),
                                          (.7, 129/255, 129/255),
                                          (1, 46/255, 46/255)),
                                   blue=((0, 244/255, 244/255),
                                         (.7, 1, 1),
                                         (1, 98/255, 98/255))))

def scatter_heat(x, y, **kwargs):
    kwargs['s'] = kwargs.get('s', 10)
    kwargs['edgecolors'] = kwargs.get('edgecolors', 'none')
    kwargs['cmap'] = kwargs.get('cmap', cm.jet)

    dropna = kwargs.pop('dropna', False)
    if dropna:
        ix = np.isfinite(x) & np.isfinite(y)
        x = x[ix]
        y = y[ix]

    if 'density' not in kwargs:
        estimator = gaussian_kde([x, y])
        density = estimator.evaluate([x, y])
    else:
        density = kwargs.pop('density')
    min_density = kwargs.pop('min_density', median(density))
    max_density = kwargs.pop('max_density', Inf)

    normdensity = exp(density.clip(min_density, max_density))
    xlim = kwargs.pop('xlim', (min(x), max(x)))
    ylim = kwargs.pop('ylim', (min(y), max(y)))
    retval = mpl.scatter(x, y, c=normdensity, **kwargs)
    ax = mpl.gca()
    ax.set_xlim(xlim)
    ax.set_ylim(ylim)
    return retval, density

kwargs_single_column = dict(
    draw_box=True,
    draw_row_labels=False,
    max_width=221,
    total_width=200,
    vspacer=0,
    split_columns=True,
    col_sep='_sl',
    box_height=60,
    convert=True,
)

kwargs_heatmap = dict(
    draw_box=True,
    draw_row_labels=True,
    draw_name=True,
    total_width=90,
    vspacer=0,
    split_columns=True,
    col_sep='_sl',
    box_height=20,
    make_hyperlinks=True,
    convert=True,
)

kwargs_ase_heatmap = kwargs_heatmap.copy()
kwargs_ase_heatmap['cmap'] = cm.RdBu
kwargs_ase_heatmap['cmap'].set_bad((.5, .5, .5))
kwargs_ase_heatmap['norm_rows_by'] = 'center0pre'

kwargs_expr_heatmap = kwargs_heatmap.copy()
kwargs_expr_heatmap['norm_rows_by'] = 'maxall'

def svg_heatmap(data, filename, row_labels=None, box_size=4,
                index=None,
                all_indices=None, all_colnames=None, internal_datanames=None,
                cmap=ISH, norm_rows_by=None, draw_row_labels=False,
                color_row_labels=False,
                col_sep='', box_height=None, total_width=None,
                draw_box=False, draw_name=False, data_names=None,
                make_hyperlinks = False,
                progress_bar = False,
                max_width=np.inf,
                x_min=10, y_min=10,
                spacers=None,
                convert=False,
                squeeze_rows=None,
                cmap_by_prefix=None,
                draw_average=False,
                draw_average_only=False,
                average_scale=1,
                split_columns=False,
                vspacer=30,
                hatch_nan=True, hatch_size=20,
                figure_title=None,
                nan_replace=None,
                first_col='', last_col=''):
    """
    Draw heatmap as an SVG file stored in filename

    *data* can be either a 2D array-like type (list of lists, numpy array,
    pandas DataFrame, etc), or a tuple of 2D array-likes, in which case a
    separator will be added between each one in the output

    *cmap* is a matplotlib-like colormap (i.e. a callable that expects floats
    in the range 0.0-1.0.), or an iterable of the same length as the tuple
    *data* containing colormaps

    *row_labels* can be supplied, otherwise they will detected from the first
    item in *data*, if available, and if not they will be blank.

    If *total_width* is supplied, width of each dataset in *data* will be
    scaled to that constant. If *box_height* is supplied, the height of each
    row will be *box_height*, otherwise it will be equal to the width of each
    element. If neither are supplied, elements will be squares equal to
    *box_size*. IT IS STRONGLY RECOMMENDED that if if supplying *total_width*,
    *box_height* also be specified, but this is not enforced.

    *draw_row_labels*, if True, will label the rows on the right hand side. As
    of 2013/09/03, this won't scale the SVG properly, so including the
    resulting file in an html element won't display properly.

    *spacers* is the distance between adjacent datasets.  Can either be a
    number, in which case it will apply to all datasets, or an interable for
    different distances. If the iterable is shorter than the number of
    datasets, the last value will be repeated.

    """
    import svgwrite as svg
    try:
        import pandas as pd
        has_pandas = True
    except:
        has_pandas = False
        assert all_indices
        assert all_colnames

    if not isinstance(data, tuple):
        data = (data,)

    if not isinstance(norm_rows_by, tuple):
        norm_rows_by = repeat(norm_rows_by)

    old_data = data
    colname_tuple = repeat(None)
    if split_columns and has_pandas:
        from Utils import sel_startswith
        data = []
        new_normers = []
        new_cmaps = []
        if isinstance(cmap, tuple):
            cmaps = cmap
        else:
            cmaps = repeat(cmap)
        for dataset, normer, c_cmap in zip(old_data, norm_rows_by, cmaps):
            if dataset is None:
                data.append(dataset)
                new_normers.append(normer)
                new_cmaps.append(c_cmap)
                continue

            if not isinstance(dataset, pd.DataFrame):
                dataset = pd.DataFrame(dataset).T
            colnames = list(sorted(
                {col.split(col_sep)[0] for col in dataset.columns}))
            data.extend(
                dataset.select(**sel_startswith(colname)) for colname in colnames
            )
            new_normers.extend(normer for colname in colnames)
            new_cmaps.extend(c_cmap for colname in colnames)
        data = tuple(data)
        norm_rows_by = tuple(new_normers)
        cmap = tuple(new_cmaps)
    elif split_columns and all_colnames:
        colnames = list(sorted(
            {col.split(col_sep)[0] for col in all_colnames}))
        colname = colnames[0]
        data = tuple([
            data[:, array([c.startswith(colname) for c in internal_datanames])]
            for colname in colnames
        ])
        colname_tuple = tuple(
            [c for c in all_colnames if c.startswith(colname)]
            for colname in colnames
        )
    elif not split_columns and all_colnames:
        colname_tuple = tuple(
            [c for c in all_colnames if c.startswith(dataname)]
            for dataname in internal_datanames
        )

    rows, cols = np.shape([ds for ds in data if ds is not None][0])
    if index is not None:
        rows = len(index)
    if box_height is None:
        box_height = box_size

    if row_labels is None:
        if index is not None:
            row_labels = list(index)
        elif hasattr(data[0], 'index'):
            row_labels = list(data[0].index)
        else:
            row_labels = ['' for row in range(rows)]

    if total_width is not None and max_width is not np.inf:
        boxes_per_row = max_width // (1.1 * total_width)
        if ((boxes_per_row + 1) * 1.1 * total_width - .1 * total_width
            < max_width):
            boxes_per_row += 1

        num_plotted_rows = np.ceil(len(data) / boxes_per_row
                                   + (draw_average or draw_average_only))
        if figure_title is None:
            fig_title_height = 0
        elif isinstance(figure_title, tuple):
            fig_title_height = len(figure_title)
        else:
            fig_title_height = 1
        dwg = svg.Drawing(filename,
                          size=(max_width + 2 * x_min + 200 * draw_row_labels,
                                2 * y_min
                                + (num_plotted_rows
                                   * (rows)
                                   * box_height)
                                + 80 * (fig_title_height)
                                + 80 * draw_name
                                + (num_plotted_rows - 1) * vspacer),
                         )
    elif total_width is not None:
        width = len(data) * total_width * 1.1 - .1 * total_width
        height = rows * box_height
        max_row_label_len = max(len(str(i)) for i in row_labels)
        dwg = svg.Drawing(filename,
                          size=(width + 2 * x_min + 20 * draw_row_labels *
                                max_row_label_len,
                                height + 2 * y_min + 80 * draw_name
                                + (80 * (figure_title is not None)))
                         )
    else:
        dwg = svg.Drawing(filename)
    dwg.add(svg.base.Title(path.basename(filename)))

    pat = dwg.pattern(id='hatch', insert=(0, 0), size=(hatch_size, hatch_size),
                      patternUnits='userSpaceOnUse')
    g = pat.add(dwg.g(style="fill:none; stroke:#B0B0B0; stroke-width:1"))
    g.add(dwg.path(('M0,0', 'l{hatch},{hatch}'.format(hatch=hatch_size))))
    g.add(dwg.path(('M{hatch2},0 l{hatch2},{hatch2}'.format(hatch2=hatch_size/2).split())))
    g.add(dwg.path(('M0,{hatch2} l{hatch2},{hatch2}'.format(hatch2=hatch_size/2).split())))

    dwg.add(pat)

    if box_height is None:
        box_height = box_size

    if not hasattr(cmap, "__len__"):
        cmap = [cmap for frame in data]

    if data_names is None:
        data_names = ["" for frame in data]

    if len(cmap) != len(data):
        raise ValueError("cmap and data should be the same length ({} vs {})"
                        .format(len(cmap), len(data)))

    if not hasattr(spacers, "__len__"):
        spacers = [spacers]
    else:
        spacers = list(spacers)
    while len(spacers) < len(data):
        spacers.append(spacers[-1])

    if ((isinstance(norm_rows_by, repeat)
         and isinstance(next(norm_rows_by), str)
         and next(norm_rows_by).startswith('center0all'))
        or (not isinstance(norm_rows_by, repeat)
            and isinstance(norm_rows_by[0], str)
            and np.any([i.startswith('center0all') for i in norm_rows_by]))):
        all_data = pd.concat(data, axis=1)

    if squeeze_rows is not None:
        data = [
            pd.DataFrame(d.apply(squeeze_rows, axis=1),
                         columns=[path.commonprefix(list(d.columns))])
            for d in data
        ]

    x_start = x_min
    y_start = y_min
    y_diff = 0
    iterator = zip(data, cmap, data_names, norm_rows_by, spacers,
                   colname_tuple)
    if figure_title:
        if isinstance(figure_title, tuple):
            font_size = '3em'
            for title_line in figure_title:
                dwg.add(dwg.text(title_line, (x_start, y_start+75,),
                                 style="font-size:{};font-family:sans-serif".format(font_size)))
                y_start += 80
                font_size = '1.5em'

        else:
            dwg.add(dwg.text(figure_title, (x_start, y_start+75,),
                             style="font-size:3em;font-family:sans-serif"))
            y_start += 80
    if progress_bar:
        from progressbar import ProgressBar
        pbar = ProgressBar(maxval=len(data)*rows).start()
        pbar_val = 0

    for frame, c_cmap, name, normer, spacer, colnames in iterator:
        if frame is None:
            dwg.add(dwg.text(normer, (x_start, y_start + box_height/2)))
            if total_width is not None:
                if spacer is None:
                    x_start += total_width * 1.1
                else:
                    x_start += total_width + spacer
            else:
                if spacer is None:
                    x_start += box_size
                else:
                    x_start += spacer
            if x_start > max_width:
                x_start = x_min
                y_start += box_height + vspacer
            continue
        if has_pandas:
            frame = pd.DataFrame(frame)
        if index is not None:
            if has_pandas:
                frame = frame.ix[index]
            else:
                setix = set(index)
                #selector = [i for i, name in enumerate(all_indices) if name in setix]
                #frame = frame[selector, :]
        if normer is None:
            norm_data = array(frame.copy())
        elif normer is 'mean':
            if has_pandas:
                norm_data = array(frame.divide(frame.dropna(axis=1, how='all').mean(axis=1)+10, axis=0))
            else:
                norm_data = frame / (frame[:,isfinite(frame[0,:])].mean(axis=1) + 10).reshape((rows, 1))
        elif normer == 'max':
            if has_pandas:
                norm_data = array(frame.divide(frame.dropna(axis=1, how='all').max(axis=1)+10, axis=0))
            else:
                norm_data = frame / (frame[:,isfinite(frame[0,:])].max(axis=1) + 10).reshape((rows, 1))
        elif normer == 'maxall':
            if has_pandas:
                maxall = frame.max(axis=1)
                assert len(data) == len(new_normers)
                for old_frame, norm_type in zip(data, new_normers):
                    if norm_type != 'maxall': continue
                    if old_frame is not None:
                        old_frame = old_frame.max(axis=1).ix[index
                                                             if index is not None
                                                             else old_frame.index]
                        maxall = maxall.where(maxall > old_frame, old_frame)
                norm_data = array(frame.divide(maxall + 10, axis=0))
            else:
                norm_data = frame / (old_data[:, isfinite(old_data[0, :])]
                                     .max(axis=1) + 10).reshape((rows, 1))
        elif normer == 'fullvar':
            norm_data = frame.subtract(frame
                                       .dropna(axis=1, how='all')
                                       .min(axis=1)-1e-6,
                                       axis=0)
            norm_data = array(norm_data.divide(norm_data
                                               .dropna(axis=1, how='all')
                                               .max(axis=1),
                                               axis=0))
        elif normer == 'center0':
            norm_data = array(0.5 +
                         0.5 * frame.divide(frame.dropna(axis=1).abs().max(axis=1),
                                      axis=0)
                        )
        elif isinstance(normer, str) and normer.startswith('center0min'):
            min_norm = (
                frame.dropna(axis=1).abs() .max(axis=1).clip(float(normer[10:]), 1e6)
            )
            norm_data = array(0.5+
                              0.5 * frame.divide(min_norm, axis=0))

        elif isinstance(normer, str) and normer.startswith('center0allmin'):
            min_norm = (
                all_data.dropna(axis=1).abs() .max(axis=1).clip(float(normer[13:]), 1e6)
            )
            norm_data = array(0.5+
                              0.5 * frame.divide(min_norm, axis=0))

        elif normer == 'center0all':
            norm_data = array(0.5 +
                         0.5 *
                         frame.divide(all_data.dropna(how='all', axis=1).abs().max(axis=1),
                                      axis=0)
                        )
        elif normer == 'center0pre':
            norm_data = array(0.5 + 0.5 * frame)
        elif isinstance(normer, (int, float)):
            norm_data = array(frame / normer)
            normer = 'constant'
        elif index is not None and hasattr(normer, "ix"):
            norm_data = array(frame.divide(normer.ix[index], axis=0))
        elif hasattr(normer, "__len__") and len(normer) == rows:
            if has_pandas:
                norm_data = array(frame.divide(normer, axis=0))
            else:
                norm_data = array(frame / np.reshape(normer, (rows, -1)))

        elif hasattr(normer, "__len__"):
            print('\n'*5)
            print(len(normer), normer, normer=='max')
            print(frame.shape)
            raise TypeError("norm_rows_by should be the same shape "
                            "as the number of rows")
        else:
            norm_data = array(frame / normer)

        if not c_cmap or str(c_cmap).lower() == 'default':
            c_cmap = ISH

        new_rows, new_cols = np.shape(frame)
        if hasattr(frame, 'index'):
            col_labels = frame.columns
        elif colnames:
            col_labels = colnames
        else:
            col_labels = ['' for col in range(new_cols)]
        if new_rows != rows:
            raise ValueError("All input elements must have the same number of"
                             " rows (and same row meanings --unchecked)")

        if total_width is not None:
            box_size = total_width / float(new_cols)

        i = 0
        if not draw_average_only:
            for i in range(rows):
                if progress_bar:
                    pbar.update(pbar_val)
                    pbar_val += 1
                prefix = col_labels[0][:col_labels[0].find(col_sep)]
                if cmap_by_prefix:
                    c_cmap = cmap_by_prefix(prefix)
                for j in range(new_cols):
                    g = dwg.g()
                    val = frame.ix[i,j] if has_pandas else frame[i,j]
                    g.add(svg.base.Title("{}, {}: {:.2f}".format(row_labels[i],
                                                                 col_labels[j],
                                                                 val)))
                    hatch = not isfinite(norm_data[i,j])
                    if hatch and nan_replace is not None:
                        if isinstance(nan_replace, float):
                            norm_data[i, j] = nan_replace
                        else:
                            if normer.startswith('center0'):
                                norm_data[i, j] = 0.5
                            else:
                                norm_data[i, j] = 0.0
                    elif hatch:
                        n = 0
                        norm_data[i, j] = 0
                        left = j - 1
                        while left >= 0:
                            if isfinite(norm_data[i, left]):
                                norm_data[i, j] += norm_data[i, left]
                                n += 1
                                break
                            left -= 1
                        right = j + 1
                        while right  < norm_data.shape[1]:
                            if isfinite(norm_data[i, right]):
                                norm_data[i, j] += norm_data[i, right]
                                n+= 1
                                break
                            right += 1
                        if n == 0:
                            norm_data[i, j] = .5 if 'center' in normer else 0
                        else:
                            norm_data[i, j] /= n
                    g.add(dwg.rect((x_start + box_size*j, y_start + i*box_height),
                                   (box_size, box_height),
                                   style="fill:#{:02x}{:02x}{:02x}"
                                   .format(*[int(255*x) for x in
                                             c_cmap(norm_data[i, j])])))
                    dwg.add(g)
                    if hatch_nan and hatch:
                        g.add(dwg.rect((x_start + box_size*j,
                                        y_start + i*box_height),
                                       (box_size, box_height),
                                       style="fill:url(#hatch)"
                                      )
                             )
                    col_base = col_labels[j][:col_labels[j].find(col_sep)]
                    if col_base != prefix:
                        prefix = col_base
                        if cmap_by_prefix:
                            c_cmap = cmap_by_prefix(prefix)
                        g.add(dwg.line((x_start + box_size * j,
                                        y_start + i * box_height),
                                       (x_start + box_size * j,
                                        y_start + (i + 1) * box_height),
                                       style="stroke-width:{}; stroke:#000000"
                                       .format(.1 * box_size)))
        else:
            for j in range(new_cols):
                hatch = not isfinite(norm_data[0, j])
                if hatch:
                    n = 0
                    norm_data[:, j] = 0
                    if j > 0 and isfinite(norm_data[0,j-1]):
                        norm_data[:, j] += norm_data[:, j-1]
                        n += 1
                    if (j + 1 < norm_data.shape[1]
                        and isfinite(norm_data[0, j+1])):
                        norm_data[:, j] += norm_data[:, j+1]
                        n += 1
                    norm_data[:, j] /= n
        dwg.add(dwg.text(first_col, (x_start,
                                     y_start + (i + 1) * box_height)))
        dwg.add(dwg.text(last_col, (x_start + (new_cols - 1) * box_size,
                                    y_start + (i + 1) * box_height)))
        if draw_box and not draw_average_only:
            dwg.add(dwg.rect((x_start, y_start + 0),
                             (new_cols*box_size, rows*box_height),
                             style="stroke-width:1; "
                             "stroke:#000000; fill:none"))
        if draw_average or draw_average_only:
            avg_frame = norm_data.mean(axis=0)
            for j in range(new_cols):
                col_base = col_labels[j][:col_labels[j].find(col_sep)]
                prefix = col_base
                if cmap_by_prefix:
                    c_cmap = cmap_by_prefix(prefix)
                g = dwg.g()
                g.add(svg.base.Title("Average, {}: {:.2f}".format(col_labels[j],
                                                                  avg_frame[j])))
                g.add(dwg.rect((x_start + box_size*j,
                                y_start + (i+(not draw_average_only))*box_height),
                               (box_size, box_height),
                               style="fill:#{:02x}{:02x}{:02x}"
                               .format(*[int(255*x) for x in
                                         c_cmap(average_scale*avg_frame[j])])))
                if not isfinite(norm_data[0, j]) and hatch_nan:
                    g.add(dwg.rect((x_start + box_size*j,
                                    y_start + (i+(not draw_average_only))*box_height),
                                   (box_size, box_height),
                                   style="fill:url(#hatch)"
                                  )
                         )

                dwg.add(g)
            dwg.add(dwg.rect((x_start,
                              y_start + (i+(not draw_average_only))*box_height),
                             (new_cols*box_size, 1*box_height),
                             style="stroke-width:1; stroke:#000000; fill:none"
                            ))

        if draw_name:
            if name == "" and split_columns:
                name = col_base
            xpos = x_start + box_size * new_cols / 2.0
            text = dwg.text('',
                             (xpos,
                              y_start
                              + box_height * (rows) * (1-draw_average_only)
                              + box_height * (draw_average or draw_average_only)
                              + 13),
                            style="text-anchor: middle;font-family:sans-serif;")
            text.add(dwg.tspan("", dy=["-1.5em"]))
            for line in name.split('_'):
                text.add(dwg.tspan(line,
                                   dy=["1.5em"],
                                   x=[xpos],
                                   style="text-anchor: middle;",
                                   ))
            dwg.add(text)

        if total_width is not None:
            if spacer is None:
                x_start += total_width * 1.1
            else:
                x_start += total_width + spacer
        else:
            if spacer is None:
                x_start += new_cols * box_size + box_size
            else:
                x_start += new_cols * box_size + spacer

        #y_diff = new_rows * box_height + vspacer
        if x_start + total_width >= max_width:
            x_start = x_min
            y_start += new_rows*box_height*(not draw_average_only) + vspacer
            y_start += box_height*(draw_average_only or draw_average)

    if draw_row_labels and isinstance(row_labels[0], tuple):
        lwidths = Counter()
        for r in row_labels:
            for i, l in enumerate(r):
                lwidths[i] = max(lwidths[i], len(str(l)))
        cum_len = 0
        for i in range(len(lwidths)):
            old_width = lwidths[i]
            lwidths[i] += cum_len
            cum_len += old_width

    if draw_row_labels and not draw_average_only:
        for i in range(rows):
            if color_row_labels:
                style = "font-family:sans-serif; font-size: {size}; fill: {color};".format(
                    size=box_height,
                    color='red' if row_labels[i] in color_row_labels else 'black',
                )
            else:
                style = "font-family:sans-serif; font-size: {}".format(box_height)
            if isinstance(row_labels[i], tuple):
                labeltext = dwg.g()
                for lnum, ltext in enumerate(row_labels[i]):
                    labeltext.add(dwg.text(ltext,
                                           (x_start + lwidths[lnum-1] * 10 + lnum * 50,
                                            y_start + i * box_height + box_height),
                                           style=style,
                                          ))
            else:
                labeltext = (dwg.text(row_labels[i],
                                      (x_start, y_start + i*box_height+box_height),
                                      style=style,
                                     ))
            if make_hyperlinks:
                if make_hyperlinks is True:
                    link = dwg.a('http://insitu.fruitfly.org/cgi-bin/ex/report.pl?ftype={}&ftext={}'
                                 .format(2 if (isinstance(row_labels[i], str)
                                               and
                                               (row_labels[i].startswith('FBgn'))
                                              )
                                         else 1,
                                         row_labels[i]),
                                 target='_replace',
                                )
                else:
                    link = dwg.a(make_hyperlinks.format(frame.index[i]))
                link.add(labeltext)
                dwg.add(link)
            else:
                dwg.add(labeltext)
    if progress_bar:
        pbar.finish()
    dwg.saveas(filename)
    if convert:
        cmd = [
            'convert',
            filename,
            '-units', 'PixelsPerInch',
            '+antialias',
            '-density', '600',
            '-background', 'none',
            '-transparent', 'white',
            filename.replace('svg', 'png'),
        ]
        subprocess.Popen(cmd)

def cmap_by_prefix(prefix):
    cms = dict(
        WT = ISH_CMS_5[0],
        bcd = ISH_CMS_5[1],
        zld = ISH_CMS_5[2],
        G20 = ISH_CMS_5[3],
        hb = ISH_CMS_5[4],
    )
    for p in cms:
        if prefix.startswith(p):
            return cms[p]
    return ISH

def minimize_ink(axes):
    axes.spines['right'].set_color('none')
    axes.spines['top'].set_color('none')
    axes.yaxis.set_ticks_position('left')
    axes.xaxis.set_ticks_position('bottom')

def plot_coordinates(chroms, coords, **kwargs):
    offsets = pd.Series({
        '2L': 23011544,
        '2R': 0,
        '3L': 24543557,
        '3R': 0,
        '4': 0,
        'X': 0
    })
    chrom_lens = {
        "YHet"                       :347038,
        "2RHet"                      :3288761,
        "2LHet"                     :368872,
        "3LHet"                      :2555491,
        "3RHet"                      :2517507,
        "U"                          :10049037,
        "XHet"                       :204112,
        "dmel_mitochondrion_genome"  :19517,
        "2L"                         :23011544,
        "X"                          :22422827,
        "3L"                         :24543557,
        "4"                          :1351857,
        "2R"                         :21146708,
        "3R"                         :27905053,
        "Uextra"                     :29004656,
    }

    chrom_y_coords = pd.Series({
        '2L': 1,
        '2R': 1,
        '3L': 2,
        '3R': 2,
        '4': 3,
        'X': 4,
    })

    for chrom in chrom_y_coords.index:
        mpl.hlines(chrom_y_coords[chrom], chrom_lens[chrom] - offsets[chrom],
                   -offsets[chrom])
    return mpl.scatter(
        coords - pd.Series(index=chroms.index, data=offsets[chroms].as_matrix()),
        chrom_y_coords[chroms],
        **kwargs
    )


from _decimal import Decimal

from rest_framework import serializers

from src.courses.repository.course_repository import CourseRepository
from src.grades.models import Grade
from src.users.repository.professor_repository import ProfessorRepository
from src.users.repository.student_repository import StudentRepository

class GradeSerializer(serializers.ModelSerializer):
    id = serializers.UUIDField(read_only=True)
    course = serializers.PrimaryKeyRelatedField(
        queryset=CourseRepository().search_all_objects(),
        required=False,
        write_only=True,
    )
    professor = serializers.PrimaryKeyRelatedField(
        queryset=ProfessorRepository().search_all_objects(),
        required=False,
        write_only=True,
    )
    student = serializers.PrimaryKeyRelatedField(
        queryset=StudentRepository().search_all_objects(),
        required=False,
        write_only=True,
    )
    course_name = serializers.SerializerMethodField()
    professor_name = serializers.SerializerMethodField()
    student_name = serializers.SerializerMethodField()
    value = serializers.DecimalField(max_digits=5, decimal_places=2)

    class Meta:
        model = Grade
        fields = [
            "id",
            "course",
            "professor",
            "student",
            "course_name",
            "professor_name",
            "student_name",
            "value",
        ]

    def get_course_name(self, obj):
        if obj.course:
            return obj.course.name
        return None

    def get_professor_name(self, obj):
        if obj.professor:
            return obj.professor.name
        return None

    def get_student_name(self, obj):
        if obj.student:
            return obj.student.name
        return None

    def validate_grade_value(self, value):
        if not value or (not isinstance(value, Decimal) or value < 0):
            raise serializers.ValidationError(
                {"value": "A Nota deve ser maior ou igual a 0 e decimal"}
            )
        return value

    def validate(self, data):
        if self.instance:
            return data

        self.validate_grade_value(data.get("value"))

        model_fields = self.Meta.fields.copy()
        model_fields.remove("id")
        model_fields.remove("course_name")
        model_fields.remove("professor_name")
        model_fields.remove("student_name")
        for field in model_fields:
            if field not in data:
                raise serializers.ValidationError({field: "Este campo é obrigatório."})

        return data


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Apr 14 17:19:01 2021

@author: silicon
"""

import rdkit
from rdkit import Chem
from rdkit.Chem import AllChem, Draw
from rdkit import DataStructs
from collections import Counter
from rdkit.Chem.Draw import IPythonConsole, MolsToGridImage
from fragment_cut import cut_fragment as cf
import numpy as np
from sklearn.cluster import KMeans
import csv

path = "kmeans_10_fragments.log"
kmeans_library_1 = {i: [] for i in range(2)}  # num cluster
kmeans_library_2 = {i: [] for i in range(2)}
with open (path,"r") as f:
    lines = f.readlines()
for line in lines:
    if len(line.split())>0:
        num,smile = int(line.split()[0]), line.split()[1]
    kmeans_library_1[num].append(smile)
#print(kmeans_library_1)
for key, value in kmeans_library_1.items():
   # print(key,value)
    value_tem = [value[0]]
    #print(value_tem)

    for va in value[1:]:
        flag = True
        mol_va = Chem.MolFromSmiles(va)
        for va_t in value_tem:
            mol_va_t = Chem.MolFromSmiles(va_t)
           # print(key,value_tem)
            fp0 = AllChem.GetMorganFingerprintAsBitVect(mol_va,2)
            fp1 = AllChem.GetMorganFingerprintAsBitVect(mol_va_t,2)
            sm12 =DataStructs.FingerprintSimilarity(fp0,fp1,metric=DataStructs.DiceSimilarity)
            #print(key,sm12)
            if sm12 > 0.99:
                flag = False
                break
        if flag:
             value_tem.append(va)
               # print(value_tem)
               # exit
    kmeans_library_2[key] = value_tem

print(kmeans_library_2)
with open("non_repeat_cluster_smiles.log","w")as f:
     for key,value in kmeans_library_2.items():
         ss = str(key)
         for va in value:
             ss += ",  " +va
         f.write(ss)
         f.write('\n')

def plt_moles(smiles,plt_name):
    frag_moles = []
    for sm in smiles:
        mol = Chem.MolFromSmiles(sm)
        frag_moles.append(mol)

    img = Draw.MolsToGridImage(frag_moles, molsPerRow=5,subImgSize=(300, 300))
    img.save("_new_labels_"+str(plt_name)+".png")

for key, value in kmeans_library_2.items():
    plt_moles(value,key)