from Rock import Rock
import plotly.graph_objects as go
import math
import numpy as np
from StreamDiverter import StreamDiverter
import plotly.io as pio

# needs to install psutil, requests, ipython, nbformat, orca as well, dependency of plotly

def luciaManifold(_k, _phi, queue):
    sample = Rock(_k, _phi)
    lc = Lucia(sample)
    lc.canvas()
    lc.classify()
    plot = lc.retrievePlot()
    queue.put([plot, lc.json, lc.rFNumber()])

class Lucia:
    def __init__(self, _sample):
        self.rock = _sample
        self.rfn = list()
        self.lrgr = np.array([])
        self.plot = go.Figure()
        self.json = ''
        self.rock.setPhi([p / 100 for p in _sample.getPhi()])  # convert porosity from % to decimal

    def canvas(self):
        self.rfnData()
        self.luciaRegr()
        self.plot.add_trace(go.Scatter(x=np.arange(0.01, 0.5, 0.001), y=self.rfn[0], fill=None, name='RFN 0.5'))
        self.plot.add_trace(go.Scatter(x=np.arange(0.01, 0.5, 0.001), y=self.rfn[1], fill='tonexty', name='RFN 1.5'))
        self.plot.add_trace(go.Scatter(x=np.arange(0.01, 0.5, 0.001), y=self.rfn[2], fill='tonexty', name='RFN 2.5'))
        self.plot.add_trace(go.Scatter(x=np.arange(0.01, 0.5, 0.001), y=self.rfn[3], fill='tonexty', name='RFN 4.0'))
        self.plot.add_trace(go.Scatter(x=np.arange(0.01, 0.5, 0.001), y=self.lrgr[0], name='Regressão Lucia C1'))
        self.plot.add_trace(go.Scatter(x=np.arange(0.01, 0.5, 0.001), y=self.lrgr[1], name='Regressão Lucia C2'))
        self.plot.add_trace(go.Scatter(x=np.arange(0.01, 0.5, 0.001), y=self.lrgr[2], name='Regressão Lucia C3'))
        self.plot.update_layout(title="Classificação de Lucia", xaxis=dict(title="Porosidade Interpartícula"), yaxis=dict(title="Permeabilidade (mD)"), xaxis_type='log', yaxis_type='log')
        self.plot.update_xaxes(range=[-2, -0.3010])
        self.plot.update_yaxes(range=[-2, 4])

    def classify(self):
        k = self.rock.getK()
        phi = self.rock.getPhi()
        print(k, phi)
        self.plot.add_trace(go.Scatter(x=phi, y=k, name='Series A', mode='markers'))

    def retrievePlot(self):
        self.json = pio.to_json(self.plot)

        diverter = StreamDiverter()
        self.plot.show(renderer='iframe')
        return diverter.srcExtractor()

    def rfnData(self):
        rfn = [0.5, 1.5, 2.5, 4]
        A = 9.7982
        B = 12.0838
        C = 8.6711
        D = 8.2965
        tomiliDarcy = 1000 / (1.01325 * 10 ** 12)

        k = []
        phi = np.arange(0.01, 0.5, 0.001)

        for n in rfn:
            k.clear()
            for p in phi:
                st = (A - (B * math.log10(n)))
                nd = (C - (D * math.log10(n)))
                rslt = 10 ** (st + (nd * math.log10(p)))
                k.append(rslt)
            self.rfn.append(k.copy())

    def luciaRegr(self):
        phi = np.arange(0.01, 0.5, 0.001)
        k = [0, 0, 0]
        k[0] = [(45.35 * 10 ** 8) * p ** 8.537 for p in phi]
        k[1] = [(2.040 * 10 ** 6) * p ** 6.38 for p in phi]
        k[2] = [(2.884 * 10 ** 3) * p ** 4.275 for p in phi]
        self.lrgr = np.array(k)

    def rFNumber(self):
        """
        Calcula o número rfn
        :return:
        """
        A = 9.7982
        B = 12.0838
        C = 8.6711
        D = 8.2965

        k = self.rock.getK()
        phi = self.rock.getPhi()
        rfn = []

        for i in range(len(k)):
            numerator = pow(10, A+C*math.log10(phi[i]))
            denominator = k[i]
            inverse_power = B+D*math.log10(phi[i])
            rfn.append(pow(numerator/denominator, 1/inverse_power))

        return rfn


import torch
import torch.nn.functional as F

def mention_detection_entropy(mention_logits: torch.Tensor, threshold=0.0) -> torch.Tensor:
    """See https://aclanthology.org/2022.acl-long.519/ for details

    Args:
        mention_logits (torch.Tensor): An 1d tensor in which each element is a mention score (logit value)

    Returns:
        torch.Tensor: An 1d tensor in which each element is the entropy of the corresponding mention.
            The size of return tensor is identical to the size of input arg:`mention_logits`
    """
    new_mention_logits = mention_logits.unsqueeze(1)
    threshold_tensor = torch.full_like(new_mention_logits, threshold)
    mention_logits_with_threshold = torch.cat((threshold_tensor, new_mention_logits), dim=1)
    probs = F.softmax(mention_logits_with_threshold, dim=1)
    entropy = -torch.sum(probs * torch.log(probs), dim=1)
    return entropy

if __name__ == "__main__":
    t = torch.Tensor([1, 0, 2, -1])
    mention_detection_entropy(t)


from rest_framework import routers
from .api import SalesLeadViewSet, UserViewSet, CompanyViewSet
from django.urls import path, include
from . import views

router = routers.DefaultRouter()
router.register('api/leads', SalesLeadViewSet, 'leads')
router.register('api/users', UserViewSet, 'users')
router.register('api/companies', CompanyViewSet, 'companies')

# Wire up our API using automatic URL routing.
# Additionally, we include login URLs for accessible API.
urlpatterns = [
    path('', include(router.urls)),
    path('auth_users/', views.auth_users, name='auth_users'),
    path('get_leads/<str:userFk>/', views.get_lead_models, name='get_user_models'),
    path('get_userid/<str:username>/', views.get_userid, name='get_userid'),
    path('api-auth/', include('rest_framework.urls', namespace='rest_framework'))
]

# urlpatterns = router.urls


from random import randint
from multiprocessing import Pool
import numpy as np
import time

# change DEBUG to True to see matrices printed
# Print them when the SIZE is a small number ( < 4?)
DEBUG = False
SIZE = 500
a = [[randint(-100,100) for _ in range(SIZE)] for _ in range(SIZE)]
b = [[randint(-100,100) for _ in range(SIZE)] for _ in range(SIZE)]
npa = np.array(a)
npb = np.array(b)
# a = [[1,2],[3,4]]
# b = [[1,2],[3,4]]

# the single core implementation
def matrix_multiplication(m0, m1):
    """The following algorithm is exactly how suhak's jungsuk does matrix
    multiplication"""

    # build resulting matrix
    res = [[0 for x in range(len(m0))] for x in range(len(m1))]

    # iterate column of lefthand
    for k in range(len(m0[0])):
    # iterate row of lefthand
        for i in range(len(m0)):
            # r = ith row, kth column
            r = m0[i][k]
            for j in range(len(m1)):
                # ith row, jth column of result is summing
                # r * kth row, jth column of righthand
                res[i][j] += (r * m1[k][j])
    return res

def transpose(m):
    # zip(*m) returns the transposed list, but its elements changed to tuples.
    # therefore we change its type to list.
    return [list(r) for r in zip(*m)]

def f(column):

    # functions used in pool can only accept ONE arguments.
    # Therefore, instead of f(m0, c), we use global a to access the lefthand
    # matrix
    global a

    # build resulting column
    res = [0 for x in range(len(a))]
    for i in range(len(a)):
        for j in range(len(a[i])):
            res[i] += (a[i][j] * column[j])
    return res

if DEBUG:
    for (r, r2) in zip(a, b):
        print(r,"  ", r2)
"""SINGLECORE"""

# get the time before the operation
start = time.time()

result = matrix_multiplication(a, b)

# get the time after the operation
end = time.time()

# the difference between the two becomes the time spent
print("singlecore took: ", end - start)

"""MULTICORE"""

start = time.time()
# initiate a pool
with Pool() as p:
    # transpose the righthand matrix so that we can do column-wise access
    ll = transpose(b)

    if DEBUG:
        print("RIGHT matrix transposed:")
        for l in ll:
            print(l)

    # apply f to elements in l, using parallelization
    # the result would be list of list(columns).
    # We can transpose it back to match the row-wise representation
    tmp = p.map(f, ll)

    # transpose back to get the correct result
    ret = transpose(tmp)

    if DEBUG:
        print("result of singlecore:")
        for r in result:
            print(r)
        print("result of multicore:")
        for r in ret:
            print(r)

    end = time.time()
print("multicore took: ", end - start)

start = time.time()
r = np.dot(npa, npb)
end = time.time()
print("numpy took: ", end - start)
# check that multicore version works correctly
assert(result == ret)
assert(np.array_equal(np.array(result), r))


""" this counts the number of tokens in a string or file
    to run in command line type:
    'python token_counter.py [filename.txt or "string in quotes"]' """

import sys
import os
import tiktoken

def tiktoken_len(text):
    """
    Counts the number of tokens in a string
    """
    tokenizer = tiktoken.get_encoding('cl100k_base')
    tokens = tokenizer.encode(text, disallowed_special=())
    return len(tokens)

def read_file(filename):
    """
    This function reads a file and returns its content as a string so that the number of tokens can be counted
    """
    try:
        with open(filename, 'r') as file:
            data = file.read()
        return data
    except Exception as e:
        print(f"An error occurred while reading the file: {str(e)}")
        return None

def main(input_arg):
    """
    Main function to read a text file or process a string and print its token count.
    """
    if os.path.isfile(input_arg):
        content = read_file(input_arg)
        if content is None:
            print("Could not read the file. Exiting...")
            return
    else:
        content = input_arg

    token_count = tiktoken_len(content)
    print(f"The token count is: {token_count}")

if __name__ == "__main__":
    if len(sys.argv) != 2:
        print("Please provide a file path or a string as an argument.")
        sys.exit(1)

    main(sys.argv[1])


from __future__ import annotations

import aiohttp
import pytest

from yutto._typing import CId
from yutto.api.danmaku import get_danmaku, get_protobuf_danmaku, get_xml_danmaku
from yutto.utils.fetcher import Fetcher
from yutto.utils.funcutils import as_sync

@pytest.mark.api
@as_sync
async def test_xml_danmaku():
    cid = CId("144541892")
    async with aiohttp.ClientSession(
        headers=Fetcher.headers,
        cookies=Fetcher.cookies,
        trust_env=Fetcher.trust_env,
        timeout=aiohttp.ClientTimeout(total=5),
    ) as session:
        danmaku = await get_xml_danmaku(session, cid=cid)
        assert len(danmaku) > 0

@pytest.mark.api
@as_sync
async def test_protobuf_danmaku():
    cid = CId("144541892")
    async with aiohttp.ClientSession(
        headers=Fetcher.headers,
        cookies=Fetcher.cookies,
        trust_env=Fetcher.trust_env,
        timeout=aiohttp.ClientTimeout(total=5),
    ) as session:
        danmaku = await get_protobuf_danmaku(session, cid=cid, segment_id=1)
        assert len(danmaku) > 0

@pytest.mark.api
@as_sync
async def test_danmaku():
    cid = CId("144541892")
    async with aiohttp.ClientSession(
        headers=Fetcher.headers,
        cookies=Fetcher.cookies,
        trust_env=Fetcher.trust_env,
        timeout=aiohttp.ClientTimeout(total=5),
    ) as session:
        danmaku = await get_danmaku(session, cid=cid, save_type="ass", last_n_segments=2)
        assert len(danmaku["data"]) > 0
        assert danmaku["source_type"] == "xml"
        assert danmaku["save_type"] == "ass"


"""Initial Migration

Revision ID: 655997557eb7
Revises: 16fb80f175f8
Create Date: 2019-11-26 10:30:18.002304

"""
from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = '655997557eb7'
down_revision = '16fb80f175f8'
branch_labels = None
depends_on = None

def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column('comments', sa.Column('comment', sa.Text(), nullable=True))
    # ### end Alembic commands ###

def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_column('comments', 'comment')
    # ### end Alembic commands ###


from __future__ import unicode_literals, absolute_import
from __future__ import print_function, division

import logging
from traceback import format_exc
from collections import OrderedDict

log = logging.getLogger(__name__)

class ParseException(Exception):
    """
    Custom exception thrown by the parser if it could succeded.

    :var lineno: Line number of the parsing error.
    :var raw_line: Raw line that failed to be parsed.
    :var exc: Internal failure traceback of the parser.
    """
    def __init__(self, lineno, raw_line, exc):
        super(ParseException, self).__init__(
            'Unable to parse line #{}: "{}"'.format(
                lineno, raw_line
            )
        )
        self.lineno = lineno
        self.raw_line = raw_line
        self.exc = exc

def build_parser():
    """
    Build a pyparsing parser for our custom topology description language.

    :rtype: pyparsing.MatchFirst
    :return: A pyparsing parser.
    """
    from pyparsing import (
        Word, Literal, QuotedString,
        StringStart, StringEnd,
        alphas, nums, alphanums,
        Group, OneOrMore, Optional
    )

    number = Word(nums)
    text = QuotedString('"')
    identifier = Word(alphas, alphanums + '_')

    attribute = (
        identifier('key') + Literal('=') +
        (text | number | identifier)('value')
    )
    attributes = (
        Literal('[') +
        OneOrMore(Group(attribute))('attributes') +
        Literal(']')
    )

    node = identifier('node')
    port = node + Literal(':') + (identifier | number)('port')
    link = port('endpoint_a') + Literal('--') + port('endpoint_b')

    nodes_spec = (
        StringStart() + Optional(attributes) +
        OneOrMore(Group(node))('nodes') + StringEnd()
    )
    ports_spec = (
        StringStart() + Optional(attributes) +
        OneOrMore(Group(port))('ports') + StringEnd()
    )
    link_spec = (
        StringStart() + Optional(attributes) +
        link('link') + StringEnd()
    )

    statement = link_spec | ports_spec | nodes_spec

    return statement

def parse_txtmeta(txtmeta):
    """
    Parse a textual description of a topology and return a dictionary of it's
    elements.

    :param str txtmeta: The textual meta-description of the topology.
    :rtype: dict
    :return: Topology as dictionary.
    """
    statement = build_parser()
    data = {
        'nodes': [],
        'ports': [],
        'links': []
    }

    def process_attributes(parsed):
        """
        Convert a pyparsing object with parsed attributes in a dictionary.
        """
        attrs = OrderedDict()

        if 'attributes' not in parsed:
            return attrs

        for attr in parsed.attributes:

            value = attr.value

            # Try to convert simple types
            try:
                value = int(value)
            except ValueError:
                pass
            if value == 'True':
                value = True
            elif value == 'False':
                value = False

            attrs[attr.key] = value
        return attrs

    for lineno, raw_line in enumerate(txtmeta.splitlines(), 1):
        try:
            # Ignore comments and empty line
            line = raw_line.strip()
            if not line or line.startswith('#'):
                continue

            # Parse line and optional attributes
            parsed = statement.parseString(line)
            attrs = process_attributes(parsed)
            log.debug(parsed.dump())
            log.debug(attrs)

            # Process link lines
            if 'link' in parsed:
                link = parsed.link
                data['links'].append({
                    'endpoints': (
                        (link.endpoint_a.node, link.endpoint_a.port),
                        (link.endpoint_b.node, link.endpoint_b.port),
                    ),
                    'attributes': attrs
                })
                continue

            # Process port lines
            if 'ports' in parsed:
                data['ports'].append({
                    'ports': [
                        (port.node, port.port) for port in parsed.ports
                    ],
                    'attributes': attrs
                })
                continue

            # Process port lines
            if 'nodes' in parsed:
                data['nodes'].append({
                    'nodes': [node.node for node in parsed.nodes],
                    'attributes': attrs
                })
                continue

            raise Exception('Unknown line type parsed.')

        except Exception:
            e = ParseException(lineno, raw_line, format_exc())
            log.error(str(e))
            log.debug(e.exc)
            raise e

    return data

def find_topology_in_python(filename):
    """
    Find the TOPOLOGY variable inside a Python file.

    This helper functions build a AST tree a grabs the variable from it. Thus,
    the Python code isn't executed.

    :param str filename: Path to file to search for TOPOLOGY.
    :rtype: str or None
    :return: The value of the TOPOLOGY variable if found, None otherwise.
    """
    import ast

    try:
        with open(filename) as fd:
            tree = ast.parse(fd.read())

        for node in ast.iter_child_nodes(tree):
            if not isinstance(node, ast.Assign):
                continue
            if node.targets[0].id == 'TOPOLOGY':
                return node.value.s

    except:
        log.error(format_exc())
    return None

__all__ = [
    'ParseException',
    'parse_txtmeta',
    'find_topology_in_python'
]


import pandas as pd
import numpy as np
from copy import deepcopy
from rdkit import Chem
from data import *
from sklearn.externals import joblib
from sklearn.manifold import TSNE
import matplotlib
#matplotlib.use('TkAgg')
import matplotlib.pyplot as plot

def can_smile(smi_list):
    can_list = []
    for item in smi_list:
        if Chem.MolFromSmiles(item) is not None:
            can_item = Chem.MolToSmiles(Chem.MolFromSmiles(item))
            can_list.append(can_item)
    return can_list

def drop_internal_overlap(fname):

    """
    Drop internal overlap within generated SMILES file
    Args:
        fname: path to the file containing SMILES separated by \n

    Returns: None. Save output file.

    """
    smi_list = []
    with open (fname) as f:
        for line in f:
            smi_list.append(line.rstrip())
    can_smi_list = can_smile(smi_list)
    unique_lst = set(can_smi_list)
    unique_lst = list(unique_lst)
    outf = pd.DataFrame()
    outf['Cano_SMILES'] = pd.Series(data=unique_lst)
    outf.to_csv('Unique_'+fname, index=False)

def get_smi_list_overlap(large, small):
    """

    Args:
        large: list containing the SMILE structures for transfer training
        small: list containing the SMILE structures for transfer sampling

    Returns: num of repeat SMILES, num of unique SMILES in transfer sampling, list of unique SMILES

    """

    large_can, small_can = can_smile(large), can_smile(small)
    small_copy = deepcopy(small_can)
    overlap = set(small_can).intersection(large_can)
    #for item in overlap:
        #list(filter(lambda a: a != item, small_copy))
    small_copy_novel = [item for item in small_copy if item not in large_can]
    return len(overlap), len(small_copy), small_copy_novel

def predict_property(model_file, fps):
    """
    Function to predict the properties of generated molecules
    Args:
        model_file: File containing pre-trained ML model for prediction
        fps: list of molecular fingerprints

    Returns: list of predicted valued

    """
    model = joblib.load(model_file)
    return model.predict(fps)

def save_predict_results():
    """
    Predict the gap and dip of generated SMILES from files and save the results
    Also save the generated with gap < 2, dip <3.66 as promising candidates
    Returns:

    """

    ori_lst = []
    for i in range(1, 4):
        ori_df = pd.read_csv('Training_Model'+str(i)+'.csv')
        ori_list = ori_df['SMILES'].tolist()
        ori_lst.append(ori_list)
    frames = []
    gen_mols = []
    gen_fps = []
    for i, group in enumerate(['all', 'class3', 'prom']):
        gen_df = pd.read_csv('novel_sampled_cano_script_'+group+'_until.csv')
        gen_list = gen_df['SMILES'].tolist()
        print('Number of molecules in training for model {} is {}'.format(i+1, len(ori_lst[i])))
        over, num, smi_list = get_smi_list_overlap(ori_lst[i], gen_list)
        smi_mols = get_mols(smi_list)
        smi_fps, failed_mols = get_fingerprints(smi_mols)
        for idx in sorted(failed_mols, reverse=True):
            del smi_list[idx]
        smi_df = pd.Series(data=smi_list, name='SMILES').to_frame()
        smi_df.loc[:,'Group'] = i+1
        frames.append(smi_df)

    unique_df = pd.concat(frames)
    gen_smi = unique_df['SMILES'].tolist()
    gen_mols = get_mols(gen_smi)
    gen_fps, _ = get_fingerprints(gen_mols)
    unique_df['Gaps'] = predict_property('gbdt_regessor_gap_regu.joblib', gen_fps)
    unique_df['Dips'] = predict_property('gbdt_regessor_dip_reg.joblib', gen_fps)
    promising_df = unique_df.loc[(unique_df['Gaps'] <= 2.0) & (unique_df['Dips']<=3.66)]
    unique_df.to_csv('Unique_models_15epoch.csv', index=False)
    promising_df.to_csv('Promising_models_15epoch.csv', index=False)

def tsne_projection(train_file, gen_file, i):
    """
    Creat tsne projection of the fps of the generated promising smiles and the smiles for transfer training.
    Returns: plot of the tsne result

    """
    train_data = pd.read_csv(train_file)
    def get_ori_prom(row):
        if row['gaps'] <= 2 and row['dips'] <=2:
            return 'Model'+str(i)+'_train_promising'
        else:
            return 'Model'+str(i)+'_train'
    train_data['label'] = train_data.apply(get_ori_prom, axis=1)
    train_data = train_data.drop(['id', 'gaps', 'dips'], axis=1)
    gen_prom_smiles = pd.read_csv(gen_file)
    gen_prom_smiles = gen_prom_smiles.drop(['Group', 'Gaps', 'Dips'], axis=1)
    gen_prom_smiles['label'] = 'Generated_promising'
    all_smi = pd.concat([train_data, gen_prom_smiles])
    mols = get_mols(all_smi.SMILES)
    fps, _ = get_fingerprints(mols)
    fp_embeded = TSNE(n_components=2, perplexity=100).fit_transform(fps)
    all_smi['tsne1'] = fp_embeded[:, 0]
    all_smi['tsne2'] = fp_embeded[:, 1]
    return all_smi, len(train_data)

def plot_tsne(df, num_train):
    groups = df.groupby('label')
    colors = ['greenyellow', 'blue', 'red']
    fig, ax = plot.subplots()
    ax.set_prop_cycle(color=colors)
    for name, group in sorted(groups):
        ax.scatter(group.tsne1, group.tsne2, marker='o', label=name, edgecolors='black', linewidths=0.4, s=25)
    ax.legend(loc='best', frameon=False, prop={'size': 20})

    ax.grid(True, alpha=0.75)
    #ax.axis([-70, 70, -70, 70])
    ax.legend(loc='best',frameon=False, prop={'size':10})
    return ax

def main():
    save_predict_results()
    #for i in range(1,4):
    #    smi_df, num = tsne_projection('Training_model'+str(i)+'.csv', 'Promising_model'+str(i)+'.csv', i)
    #    ax = plot_tsne(smi_df, num_train=num)
    #    plot.savefig('tsne_plot_model'+str(i)+'.png', dpi=300)
    #for group in ['all', 'class3', 'prom']:
    #    drop_internal_overlap('sampled_da_'+group+'_untill_10000_10epoch.csv')

if __name__ == '__main__':
    main()


from PIL import Image,ImageDraw
#from matplotlib import pylot as plt
import matplotlib.pyplot as plt
import io
# img = Image.open('office1.jpg')
# #print img
# im = img.copy()
# draw = ImageDraw.Draw(im)
# draw.rectangle([(1,2),(100,200)],outline='blue')
#
# im.show()
fpr = [0,1,1,1,1,1]
tpr = [0,1,1,1,1,1]
fig = plt.figure()
plt.xlabel('frr',fontsize=14)
plt.ylabel('trr',fontsize=14)
plt.title('roc',fontsize=14)
plot = plt.plot(fpr,tpr,linewidth=2)
#plt.show()
canvas = fig.canvas
buf = io.BytesIO()
canvas.print_png(buf)
plt.savefig('e.png',format='png')
# #plt.savefig('a.jpg',format='jpg')
buf.seek(0)
# data = buf.getvalue()
# buf.close()
#
# buff = io.BytesIO()
# buff.write(data)
im = Image.open(buf)
im.save('d.png')
im.show()


# -*- coding: utf-8 -*-
# @Time    : 2021/12/31
# @Author  : guanzhong.zhou
# @File    : 报告上传模块页面方法封装

from datetime import datetime
import openpyxl
from PageElemens.report_upload_ele import *
from common.editYaml import *
from common.all_path import  sampledata_path, mutation_file_path, excel_doc_file_path, \
    orderNub_path
from common.screenshot import Screenshot
from uitestframework.basepageTools import BasePage
from common.logs import log
import pandas as pd

def create_upload_path():

    order_nub  = read_yaml(orderNub_path)  # 读取订单号信息
    file_name = "报告上传文件" + str(order_nub['name']) + "_" + str(order_nub['order_number'])  # 新建Excel文件，以读取的患者姓名和订单号为文件名
    return file_name

class ReportUploadPage(BasePage):
    """
    报告-基本信息任务分配模块页面基础方法封装
    """

    def search_by_poolingdate(self):
        """
        根据预计实验日期查询
        :return: 返回查询结果数量
        """
        log.info("根据预计实验日期查询")
        str_time = datetime.now().strftime('%Y.%m.%d')  # 获取当前时间
        # str_time = "2023.02.10"  # 获取当前时间

        print('预计上机日期', str_time)
        self.input('xpath', poolingDate, str_time)  # 调用搜索方法
        self.sleep(0.5)
        self.click_by_js('css', search_btn)  # 点击查询
        self.wait_loading()
        if self.isElementExists('xpath', all_orders):
            allorder = self.findelements('xpath', all_orders)  # 获取查询结果数量
            self.clicks('css', reset_btn)  # 重置查询条件
            return len(allorder)
        else:
            return 0

    def search_by_assignmentDate(self):
        """
        根据报告分配日期查询
        :return: 返回查询结果数量
        """
        log.info("根据报告分配日期查询")
        str_time = datetime.now().strftime('%Y.%m.%d')  # 获取当前时间
        # str_time = "2023.02.10"  # 获取当前时间

        self.input('css', lastPoolingDate, str_time)  # 调用搜索方法
        self.clicks('css', search_btn)  # 点击查询
        self.wait_loading()
        if self.isElementExists('xpath', all_orders):
            allorder = self.findelements('xpath', all_orders)  # 获取查询结果数量
            self.clicks('css', reset_btn)  # 重置查询条件
            return len(allorder)
        else:
            return 0

    def search_by_order(self):
        """
        按订单号搜索
        :return:返回查询出的订单信息数量
        """

        order = read_yaml(orderNub_path)
        log.info("按订单号搜索:%s"%order['order_number'])
        self.input('css', order_num, order['order_number'])
        self.click_by_js('css', search_btn)  # 点击查询
        self.wait_loading()
        if self.isElementExists('xpath', all_orders):
            allorder = self.findelements('xpath', all_orders)  # 获取查询结果数量
            return len(allorder)
        else:
            return 0

    def select_report_belong(self):
        """
        报告归属下拉选择
        """
        # 先按照订单号搜索任务
        self.search_by_order()
        log.info('报告归属下拉选择“世和')
        self.search_by_order()
        self.clicks('css', report_belong)
        self.sleep(0.5)
        self.clicks('css', report_belong_input)
        self.sleep(0.5)
        self.clicks('xpath', report_belong_choice)
        self.sleep(1)
        self.wait_loading()

    def choice_rehearing_person(self):
        """
        选择复审人
        :return:返回页面提示信息
        """
        log.info("选择复审人")

        self.executeJscript(
            'document.getElementsByClassName("vxe-table--body-wrapper")[0].scrollLeft=1250')
        self.sleep(0.5)
        self.clicks('xpath', rehearing_person)  # 复审人表单定位
        self.sleep(0.5)
        self.clicks('css', rehearing_person_input)
        self.input('css', rehearing_person_input, '周官钟')  # 复审人弹框文本录入
        self.sleep(1)
        self.clicks('xpath', rehearing_person_choice.format('周官钟'))  # 复审人弹框文本录入后选择
        self.clicks('css', rehearing_person_comfirm)
        pageinfo = self.get_save_info()  # 获取页面提示信息
        self.sleep(3)
        return pageinfo

    def upload_report_file(self):
        """
        上传报告文件
        """
        log.info("上传报告文件")
        self.clicks('xpath', report_file_btn)  # 点击报告文件编辑按钮
        self.sleep(1)
        # 上传报告文件
        # 执行JS，修改上传控件样式，由不显示变为显示，以便上传文件
        self.executeJscript(
            "document.querySelector('.card-report-file .el-upload input').style.setProperty('display','block','important');")
        self.sleep(0.5)

        # 上传报告文件需要包含患者姓名和订单号，分别取出保存为文件名
        file_name = create_upload_path()
        wb = openpyxl.Workbook()
        ws = wb.active
        ws.title = 'test_sheet1'
        ws.cell(row=1, column=2).value = 1

        wb.save(excel_doc_file_path + '/' + '{}.xlsx'.format(file_name))  # 保存文件

        report_file_path = os.path.abspath(excel_doc_file_path + '/' + '{}.xlsx'.format(file_name))
        self.input('css', report_upload_btn, report_file_path)  # 用send_keys方法上传文件
        report_info = self.get_save_info()
        Screenshot(self.driver).get_img("报告上传弹框，点击上传报告文件，上传报告文件","上传报告文件成功")
        self.sleep(1)
        return report_info

    def upload_read_file(self):
        # 上传解读文件
        log.info("上传解读文件")
        file_name = create_upload_path()
        self.executeJscript(
            "document.querySelector('.card-decode-file .el-upload input').style.setProperty('display','block','important');")
        self.sleep(0.5)
        # 创建TXT类型的解读文件
        with open(excel_doc_file_path + '/' + '{}_解读文件.txt'.format(file_name), 'w', encoding='utf-8') as f:
            f.write('自动化测试解读文件上传')
        decode_file_path = os.path.abspath(excel_doc_file_path + '/' + '{}_解读文件.txt'.format(file_name))
        self.input('css', decode_upload_btn, decode_file_path)  # 用send_keys方法上传文件
        decode_info = self.get_save_info()
        Screenshot(self.driver).get_img("报告上传弹框，点击上传解读文件，上传解读文件","上传解读文件成功")
        self.sleep(1)
        return decode_info
        # 这里调用自定义截图方法

    def upload_other_file(self):
        """上传其他文件"""
        log.info("上传其他文件")
        self.executeJscript(
            "document.querySelector('.card-other-file .el-upload input').style.setProperty('display','block','important');")
        # 创建doc类型的其他文件
        with open(excel_doc_file_path + '/' + '自动化测试其他文件上传.doc', 'w', encoding='utf-8') as f:
            f.write('自动化测试解读文件上传')
        other_file_path = os.path.abspath(excel_doc_file_path + '/' + '自动化测试其他文件上传.doc')
        self.input('css', other_upload_btn, other_file_path)  # 用send_keys方法上传文件

        # 这里调用自定义截图方法
        Screenshot(self.driver).get_img("报告上传弹框，点击上传其他文件，上传其他文件","上传其他文件成功")
        self.sleep(1)

        self.clicks('css', report_upload_comfirm)  # 上传文件弹框确认按钮
        self.wait_loading()
        self.sleep(1)

    def update_mutation_file(self):
        """
        突变文件上传
        """
        log.info("突变文件上传")
        self.clicks('xpath', mutation_file_btn)  # 突变文件编辑表单定位
        self.wait_loading()

        self.executeJscript(
            "document.querySelector('.dialog-mutation-file .el-upload input').style.setProperty('display','block','important');")

        # 用send_keys方法上传文件
        # 读取基本信息处理模块存储的实验室号
        testdata = read_yaml(sampledata_path)
        df = pd.read_csv(mutation_file_path, encoding='utf-8')
        df.loc[:, 'id'] = testdata['bioinformatic_negative_lab_num']  # 把读取的实验室号写入到突变模板中
        df.to_csv(mutation_file_path, index=False, encoding='utf-8')

        self.input('css', mutation_file_upload_btn, mutation_file_path)

        # 这里调用自定义截图方法

        self.sleep(1)
        mutation_file_info = self.get_save_info()
        Screenshot(self.driver).get_img("点击上传突变文件按钮，上传突变文件，上传突变文件","上传突变文件成功")
        self.clicks('css', mutation_file_upload_comfirm)
        self.sleep(1)
        print("突变文件上传", mutation_file_info)
        return mutation_file_info

    def complete_report_task(self):
        """
        完成报告任务
        """
        log.info("完成报告任务")
        self.clicks('css', complete_task)
        self.wait_loading()
        self.sleep(1)
        pageinfo = self.get_text('css', complete_task)
        return pageinfo

    def get_save_info(self):
        """
        获取数据操作后，页面给出的提示信息语
        Submit successfully
        review successfully
        """
        log.info('获取页面提示信息')
        if self.isElementExists('xpath', page_info):
            return self.get_text('xpath', page_info)
        else:
            return None
if __name__ == '__main__':
    s=create_upload_path()
    print(s)


from __future__ import print_function
import re
import spacy

from pyclausie import ClausIE

nlp = spacy.load('en_core_web_sm')
re_spaces = re.compile(r'\s+')
cl = ClausIE.get_instance()

class Person(object):
    def __init__(self, name, likes=None, has=None, travels=None):
        """
        :param name: the person's name
        :type name: basestring
        :param likes: (Optional) an initial list of likes
        :type likes: list
        :param dislikes: (Optional) an initial list of likes
        :type dislikes: list
        :param has: (Optional) an initial list of things the person has
        :type has: list
        :param travels: (Optional) an initial list of the person's travels
        :type travels: list
        """
        self.name = name
        self.likes = [] if likes is None else likes
        self.has = [] if has is None else has
        self.travels = [] if travels is None else travels

    def __repr__(self):
        return self.name

class Pet(object):
    def __init__(self, pet_type, name=None):
        self.name = name
        self.type = pet_type

class Trip(object):
    def __init__(self,departs_on = None,departs_to = None,departs_time = None):
        self.departs_on = departs_on
        self.departs_to = departs_to
        self.departs_time = departs_time

persons = []
pets = []
trips = []

def process_data_from_input_file(file_path='./assignment_01.data'):
    with open(file_path) as infile:
        cleaned_lines = [line.strip() for line in infile if (not line.startswith(('$$$', '###', '==='))) and line!='']

    return cleaned_lines

def select_person(name):
    for person in persons:
        if person.name == name:
            return person

def add_person(name):
    person = select_person(name)

    if person is None:
        new_person = Person(name)
        persons.append(new_person)

        return new_person

    return person

def select_pet(name2):
    for pet in pets:
        if pet.name == name2:
            return pet

def add_pet(type, name1=None):
    pet = None

    if name1:
        pet = select_pet(name1)

    if pet is None:
        pet = Pet(type, name1)
        pets.append(pet)

    return pet

def selct_trip( on = None , to = None, dep_time = None):
    for trip in trips:
        if trip.departs_on == on and trip.departs_to == to  :
            return trip

def add_trip(on = None, to = None, dep_time = None):
    new_trip  = None
    new_trip = selct_trip(on,to,dep_time)
    if new_trip is None:
        new_trip = Trip(on,to,dep_time)
        trips.append(new_trip)

    return new_trip

def get_persons_pet(person_name):

    person = select_person(person_name)

    for thing in person.has:
        if isinstance(thing, Pet):
            return thing

def process_relation_triplet(triplet):
    """
    Process a relation triplet found by ClausIE and store the data

    find relations of types:
    (PERSON, likes, PERSON)
    (PERSON, has, PET)
    (PET, has_name, NAME)
    (PERSON, travels, TRIP)
    (TRIP, departs_on, DATE)
    (TRIP, departs_to, PLACE)

    :param triplet: The relation triplet from ClausIE
    :type triplet: tuple
    :return: a triplet in the formats specified above
    :rtype: tuple
    """

    sentence = triplet.subject + ' ' + triplet.predicate + ' ' + triplet.object

    doc = nlp(unicode(sentence))

    root = True

    for t in doc:
        if t.pos_ == 'VERB' and t.head == t:
            root = t
        # elif t.pos_ == 'NOUN'

    if root is True:
        return
    # also, if only one sentence
    # root = doc[:].root

    """
    CURRENT ASSUMPTIONS:
    - People's names are unique (i.e. there only exists one person with a certain name).
    - Pet's names are unique
    - The only pets are dogs and cats
    - Only one person can own a specific pet
    - A person can own only one pet
    """

    # Process (PERSON, likes, PERSON) relations
    if root.lemma_ == 'like' and 'neg' not in [t.dep_ for t in root.children]:
        if triplet.subject in [e.text for e in doc.ents if e.label_ == 'PERSON' or e.text in KNOWN_NAMES] and triplet.object in [e.text for e in doc.ents if e.label_ == 'PERSON']:
            s = add_person(triplet.subject)
            o = add_person(triplet.object)
            if o not in s.likes:
                s.likes.append(o)

    if root.lemma_ == 'be' and triplet.object.startswith('friends with') and 'neg' not in [t.dep_ for t in root.children]:
        obj_span = doc.char_span(sentence.find(triplet.object), len(sentence))
        #fw_doc = nlp(unicode(triplet.object))
        #with_token = [t for t in fw_doc if t.text == 'with'][0]
        #fw_who = [t for t in with_token.children if t.dep_ == 'pobj']
        fw_who = [a.text for a in obj_span if a.pos_ == 'PROPN']
        for friend in fw_who:
            if triplet.subject in [e.text for e in doc.ents if e.label_ == 'PERSON'] :  #and fw_who in [e.text for e in doc.ents if e.label_ == 'PERSON']
                s = add_person(triplet.subject)
                o = add_person(friend)
                if o not in s.likes:
                    s.likes.append(o)
                if s not in o.likes:
                    o.likes.append(s)
    if root.text == 'are' and triplet.object == 'friends':
        sub_span = doc.char_span(sentence.find(triplet.subject), len(sentence))
        people = [token.text for token in sub_span if token.ent_type_ == 'PERSON']
        for s in people:
            s = add_person(s)
            for o in [person for person in people if person != s.name]:
                o = add_person(o)
                if o not in s.likes:
                    s.likes.append(o)
                if s not in o.likes:
                    o.likes.append(s)

    # Process (PET, has, NAME)
    if triplet.subject.endswith('name') and ('dog' in triplet.subject or 'cat' in triplet.subject):
        obj_span = doc.char_span(sentence.find(triplet.object), len(sentence))

        # handle single names, but what about compound names? Noun chunks might help.
        for chunks in obj_span.noun_chunks:

            if chunks[0].pos_ == 'PROPN':
                name = chunks
                subj_start = sentence.find(triplet.subject)
                subj_doc = doc.char_span(subj_start, subj_start + len(triplet.subject))

                s_people = [token.text for token in subj_doc if token.ent_type_ == 'PERSON']
                assert len(s_people) == 1
                add_person(s_people[0])
                s_person = select_person(s_people[0])

                s_pet_type = 'dog' if 'dog' in triplet.subject else 'cat'

                pet = add_pet(s_pet_type, str(name))

                s_person.has.append(pet)
    if 'named' in triplet.object and ('dog' in triplet.object or 'cat' in triplet.object):
        sub_triplet = cl.extract_triples([triplet.object+'.'])[0]
        sub_sentence = sub_triplet.subject + ' ' + sub_triplet.predicate + ' ' + sub_triplet.object
        doc2 = nlp(unicode(sub_sentence))
        obj_span = doc2.char_span(sub_sentence.find(sub_triplet.object), len(sub_sentence))
        if obj_span[0].pos_ == 'PROPN':
            name = sub_triplet.object
            subj_start = sentence.find(triplet.subject)
            subj_doc = doc.char_span(subj_start, subj_start + len(triplet.subject))

            s_people = [token.text for token in subj_doc if token.ent_type_ == 'PERSON']
            assert len(s_people) == 1

            add_person(s_people[0])
            s_person = select_person(s_people[0])

            s_pet_type = 'dog' if 'dog' in triplet.object else 'cat'

            pet = add_pet(s_pet_type,name1 = str(name))

            s_person.has.append(pet)

    #Process (PERSON, travels, TRIP)
    if root.lemma_ in ['take','fly','leave','go'] and 'GPE' in [e.label_ for e in doc.ents]:
        obj_span = doc.char_span(sentence.find(triplet.object), len(sentence))
        subj_start = sentence.find(triplet.subject)
        subj_doc = doc.char_span(subj_start, subj_start + len(triplet.subject))

        s_people = [token.text for token in subj_doc if token.ent_type_ == 'PERSON' or token.text in KNOWN_NAMES]
        trip_to = [a.text for a in obj_span if a.ent_type_ == 'GPE']
        trip_time = ' '.join([token.text for token in obj_span if token.ent_type_=='DATE' ])
        for people in s_people:
            people = add_person(people)
            for trip in trip_to:
                trip = add_trip(to=trip,dep_time=trip_time)
                if trip not in people.travels:
                    people.travels.append(trip)

def preprocess_question(question):
    # remove articles: a, an, the

    q_words = question.split(' ')

    # when won't this work?
    for article in ('a', 'an', 'the'):
        try:
            q_words.remove(article)
        except:
            pass
    ques = re.sub(re_spaces, ' ', ' '.join(q_words))
    ques = ques.replace('\'s',' is')
    return ques

def has_question_word(string):
    # note: there are other question words
    for qword in ('who', 'what'):
        if qword in string.lower():
            return True

    return False

def answer_question(question_string = None):
    sents = process_data_from_input_file()

    global KNOWN_NAMES
    KNOWN_NAMES    = {'Joe','Mary','Bob','Sally','Chris'}

    triples = cl.extract_triples(sents)

    for t in triples:
        r = process_relation_triplet(t)
        # print(r)

    question = question_string
    if question[-1] != '?':
        # question = raw_input("Please enter your question: ")

        print('This is not a question... please try again')
        return
    try:
        q_trip = cl.extract_triples([preprocess_question(question)])[0]
        q_sentence = q_trip.subject + ' ' + q_trip.predicate + ' ' + q_trip.object
        q_doc = nlp(unicode(q_sentence))
    except:
        q_trip = cl.extract_triples([preprocess_question('This is a empty question.')])[0]
        q_sentence = q_trip.subject + ' ' + q_trip.predicate + ' ' + q_trip.object
        q_doc = nlp(unicode(preprocess_question(question)))
    #1)     (WHO, has, PET)
    # here's one just for dogs
    if q_trip.subject.lower() == 'who' and (q_trip.object == 'dog' or q_trip.object =='cat'):
        answer = '{} has a {} named {}.'

        for person in persons:
            pet = get_persons_pet(person.name)
            if pet and pet.type == q_trip.object :
                print(answer.format(person.name, q_trip.object, pet.name))

    #2)     (Who is [going to|flying to|traveling to|visiting] <place>?)
    elif 'who' in q_trip.subject.lower() and True in [a in q_trip.predicate for a in ['going','flying','traveling','visiting']] and 'GPE' in [token.ent_type_ for token in q_doc] :
        answer = '{} is going to {}.'
        obj_doc = q_doc.char_span(q_sentence.find(q_trip.object), len(q_sentence))

        place = [token.text for token in obj_doc if token.ent_type_ == 'GPE'][0]

        _p = 0
        for person in persons:
            if place in [travel.departs_to for travel in person.travels]:
                print(answer.format(person.name,place))
                _p = 1
        if _p == 0:
            print(answer.format('No one',place))

    #3)     (Does <person> like <person>?)
    elif 'does' in q_trip.subject.lower() and 'like' in q_trip.predicate and q_trip.object in [e.text for e in q_doc.ents if e.label_ == 'PERSON']:

        subj_start = q_sentence.find(q_trip.subject)
        subj_doc = q_doc.char_span(subj_start, subj_start + len(q_trip.subject))
        s_people = select_person([token.text for token in subj_doc if token.ent_type_ == 'PERSON'][0])
        o_people = select_person(q_trip.object)
        if o_people in s_people.likes:
            print(s_people.name,'like',o_people.name)
        else:
            print(s_people.name,'does not like',o_people.name)

    #4)    (What's the name of <person>'s <pet_type>?)
    elif 'what' in [q.text.lower() for q in q_doc] and 'name' in [q.text.lower() for q in q_doc] and 'PERSON' in [token.ent_type_ for token in q_doc]:

        s_people = select_person([token.text for token in q_doc if token.ent_type_ == 'PERSON'][0])
        # for chunks in q_doc.noun_chunks:
            # for token in chunks:
        pet_type = [t.text for t in q_doc if t.dep_=='attr' and t.pos_=='NOUN']
        pet_type = [p for p in pet_type if p.lower() in ['cat','dog']][0]

                # print(token.text,token.dep_,token.pos_)
                # print('attr','NOUN')
                # if  (str(token.dep_),str(token.pos_)) in [('pobj','NOUN'),[('attr','NOUN')]]:
                #     pet_type = token.text
                #     print(pet_type)

        for pet in s_people.has:

            if pet.type == pet_type:
                print (s_people.name,"'s ",pet_type,"'s name is ",pet.name)

    #5)     ( When is <person> [going to|flying to|traveling to|visiting] <place>?)
    elif 'when' in q_trip.object.lower() and True in [a in q_trip.predicate for a in['going', 'flying', 'traveling', 'visiting']] and 'GPE' in [token.ent_type_ for token in q_doc]:
        obj_doc = q_doc.char_span(q_sentence.find(q_trip.object), len(q_sentence))
        place = [token.text for token in obj_doc if token.ent_type_ == 'GPE'][0]
        o_people = select_person(q_trip.subject)
        answer = '{} is going to {} at the time of {}'
        for trip in o_people.travels:
            if trip.departs_to == place:
                time = trip.departs_time
        try:
            print(answer.format(o_people.name,place,time))
        except:
            print(o_people.name,' is not going to ',place)

    #6)    (Who likes <person>?)
    elif 'who' in q_trip.subject.lower() and 'likes' in q_trip.predicate and q_trip.object in [e.text for e in q_doc.ents if e.label_ == 'PERSON']:
        o_people = select_person(q_trip.object)
        for p in persons:
            if o_people in p.likes:
                print (p.name ,'likes', o_people.name)
    #7)     (Who does <person> like?)
    elif 'who' in q_trip.object.lower() and 'like' in q_trip.predicate and q_trip.subject in [e.text for e in q_doc.ents if e.label_ == 'PERSON']:
        s_people = select_person(q_trip.subject)
        for p in s_people.likes:
            print (s_people.name,'likes',p.name)
    else:
        print("I don't know.")

# if __name__ == '__main__':
#     main()
#     stop_sign = 1


# -*- coding: utf-8 -*-
"""
Created on Mon Oct 26 21:33:24 2015

@author: jade
"""

#Récupérer via crawling la liste des 256 top contributors sur cette page https://gist.github.com/paulmillr/2657075
import requests
#inspection de l'element e.g "watch "texte, transformé en DOM
from bs4 import BeautifulSoup
import pandas as pd

def getSoupFromUrl(url):
  #Execute q request toward Youtube
  request = requests.get(url)
  #parse the restult of the request
  soup = BeautifulSoup(request.text, 'html.parser')
  return soup

url = "https://gist.github.com/paulmillr/2657075"

soup = getSoupFromUrl(url)

i=0
score = []
nom =[]

for element in soup.find_all('tr',limit=257):
    if i>0:
        score.append(element.find('th').get_text())
        nom.append(element.find('td').get_text())
    i=i+1

param = pd.DataFrame({ 'contributeur' : nom} )
param.index +=1
param


import argparse
import torch
import numpy as np
import pandas as pd
from tqdm import tqdm
from Sensitivity import SensitivityRun

parser = argparse.ArgumentParser(description='DQN-Trader arguments')
parser.add_argument('--dataset-name', default="ASD-USDT",
                    help='Name of the data inside the Data folder')
parser.add_argument('--interval', default="8hour",
                    help='The interval of chart data')
parser.add_argument('--nep', type=int, default=30,
                    help='Number of episodes')
parser.add_argument('--window_size', type=int, default=3,
                    help='Window size for sequential models')
parser.add_argument('--cuda', action="store_true",
                    help='run on CUDA (default: False)')
args = parser.parse_args()

if __name__ == '__main__':
    gamma_list = [0.9, 0.8, 0.7]
    batch_size_list = [8, 32, 64]
    replay_memory_size_list = [16, 64, 256]
    n_step = 8
    window_size = args.window_size
    dataset_name = args.dataset_name
    interval = args.interval
    n_episodes = args.nep
    device = torch.device("cuda" if args.cuda and torch.cuda.is_available() else "cpu")
    feature_size = 64
    target_update = 5

    gamma_default = 0.9
    batch_size_default = 16
    replay_memory_size_default = 32

    pbar = tqdm(len(gamma_list) + len(replay_memory_size_list) + len(batch_size_list))

    # test gamma

    run = SensitivityRun(
        dataset_name,
        interval,
        gamma_default,
        batch_size_default,
        replay_memory_size_default,
        feature_size,
        target_update,
        n_episodes,
        n_step,
        window_size,
        device,
        evaluation_parameter='gamma',
        transaction_cost=0)

    for gamma in gamma_list:
        run.gamma = gamma
        run.reset()
        run.train()
        run.evaluate_sensitivity()
        pbar.update(1)

    run.save_experiment()

    # test batch-size
    run = SensitivityRun(
        dataset_name,
        interval,
        gamma_default,
        batch_size_default,
        replay_memory_size_default,
        feature_size,
        target_update,
        n_episodes,
        n_step,
        window_size,
        device,
        evaluation_parameter='batch size',
        transaction_cost=0)

    for batch_size in batch_size_list:
        run.batch_size = batch_size
        run.reset()
        run.train()
        run.evaluate_sensitivity()
        pbar.update(1)

    run.save_experiment()

    # test replay memory size
    run = SensitivityRun(
        dataset_name,
        interval,
        gamma_default,
        batch_size_default,
        replay_memory_size_default,
        feature_size,
        target_update,
        n_episodes,
        n_step,
        window_size,
        device,
        evaluation_parameter='replay memory size',
        transaction_cost=0)

    for replay_memory_size in replay_memory_size_list:
        run.replay_memory_size = replay_memory_size
        run.reset()
        run.train()
        run.evaluate_sensitivity()
        pbar.update(1)

    run.save_experiment()
    pbar.close()


from rest_framework import viewsets, mixins, status
from curricula.models import LearningUnit, LearningSubject
from curricula.serializers import LearningSubjectSerializer, LearningUnitSerializer
from rest_framework.response import Response
from rest_framework.decorators import action

class LearningUnitViewSet(mixins.ListModelMixin, mixins.RetrieveModelMixin, mixins.CreateModelMixin,
                          mixins.UpdateModelMixin, mixins.DestroyModelMixin,viewsets.GenericViewSet):

    queryset = LearningUnit.objects.all()
    serializer_class = LearningUnitSerializer

    # uniteye bağlı konu listesi
    @action(methods=['GET'], detail=True)
    def subject_list(self, request, pk=None):
        queryset = LearningSubject.objects.filter(unit_id=pk).all()

        serializer = LearningSubjectSerializer(queryset, many=True, context={'request': request})
        return Response(serializer.data)

    # kullanıcı ünite listesi
    @action(methods=['GET'], detail=True)
    def info(self, request, pk=None):
        unit_queryset = LearningUnit.objects.filter(id=pk).first()
        subject_queryset = LearningSubject.objects.filter(unit_id=pk).all()

        unit = LearningUnitSerializer(unit_queryset, many=False)
        subject = LearningSubjectSerializer(subject_queryset, many=True)

        data = {
            'unit': unit.data,
            'subjects': subject.data
        }

        return Response(data)

    # ünite detayı
    @action(methods=['GET'], detail=True)
    def view(self, request, pk=None):
        queryset = LearningUnit.objects.filter(id=pk).first()
        serializer = LearningUnitSerializer(queryset, many=False)

        return Response(serializer.data)

    # ünite test ekler
    @action(methods=['POST'], detail=True)
    def attach_test(self, request, pk=None):
        unit = LearningUnit.objects.filter(id=pk).first()

        try:
            test = unit.test.create(name=request.data.get('name'), content=request.data.get('content'), test_id=request.data.get('test_id'), position=0)

            return Response('Test Eklendi', status=status.HTTP_200_OK)

        except Exception as e:
            return Response('Test Eklenemedi', status=status.HTTP_400_BAD_REQUEST)

    # ders görüntüleme
    @action(methods=['GET'], detail=True)
    def user_view(self, request, pk=None):

        queryset = LearningUnit.objects.select_related('lesson').prefetch_related(
            'learningsubject_set', 'learningsubject_set__lecture_parent'
        ).filter(id=pk).first()

        if queryset:
            subject = LearningSubjectSerializer(queryset.learningsubject_set.order_by('position').all(),
                                                many=True,
                                                context={'request': request})

            data = {
                'lesson': {
                    'id': queryset.lesson.id,
                    'name': queryset.lesson.name,
                    'duration': queryset.lesson.duration
                },
                'unit': {
                    'id': queryset.id,
                    'name': queryset.name
                },
                'subjects': subject.data,
            }

            return Response(data)

        return Response({'error': 'Ünite bulunamadı.'}, status=status.HTTP_400_BAD_REQUEST)


import os

from torchvision import transforms

from easyfl.datasets import FederatedImageDataset

DB_NAMES = ["MSMT17", "Duke", "Market", "cuhk03", "prid", "cuhk01", "viper", "3dpes", "ilids"]

TRANSFORM_TRAIN_LIST = transforms.Compose([
    transforms.Resize((256, 128), interpolation=3),
    transforms.Pad(10),
    transforms.RandomCrop((256, 128)),
    transforms.RandomHorizontalFlip(),
    transforms.ToTensor(),
    transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
])
TRANSFORM_VAL_LIST = transforms.Compose([
    transforms.Resize(size=(256, 128), interpolation=3),
    transforms.ToTensor(),
    transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
])

def prepare_train_data(data_dir, db_names=None):
    if db_names is None:
        db_names = DB_NAMES
    client_ids = []
    roots = []
    for db in db_names:
        client_ids.append(db)
        data_path = os.path.join(data_dir, db, 'pytorch')
        roots.append(os.path.join(data_path, 'train_all'))
    data = FederatedImageDataset(root=roots,
                                 simulated=True,
                                 do_simulate=False,
                                 transform=TRANSFORM_TRAIN_LIST,
                                 client_ids=client_ids)
    return data

def prepare_test_data(data_dir, db_names=None):
    if db_names is None:
        db_names = DB_NAMES
    roots = []
    client_ids = []
    for db in db_names:
        test_gallery = os.path.join(data_dir, db, 'pytorch', 'gallery')
        test_query = os.path.join(data_dir, db, 'pytorch', 'query')
        roots.extend([test_gallery, test_query])
        client_ids.extend(["{}_{}".format(db, "gallery"), "{}_{}".format(db, "query")])
    data = FederatedImageDataset(root=roots,
                                 simulated=True,
                                 do_simulate=False,
                                 transform=TRANSFORM_VAL_LIST,
                                 client_ids=client_ids)
    return data


import pandas as pd
import numpy as np
from datetime import datetime
import seaborn as sns
import matplotlib.pyplot as plt
from scipy import stats
from sklearn.neighbors import KDTree
from math import sin, cos, sqrt, atan2, radians

def distance(cord1, cord2):

    lat1 = float(cord1.split(',')[0][1:])
    lon1 = float(cord1.split(',')[1].replace(' ', '')[0:-1])
    lat2 = float(cord2.split(',')[0][1:])
    lon2 = float(cord2.split(',')[1].replace(' ', '')[0:-1])

    # approximate radius of earth in km
    R = 6373.0

    dlon = lon2 - lon1
    dlat = lat2 - lat1

    a = sin(dlat / 2)**2 + cos(lat1) * cos(lat2) * sin(dlon / 2)**2
    c = 2 * atan2(sqrt(a), sqrt(1 - a))

    distance = R * c

    return distance

def Closest_Stores(df, df_targets, df_stores, stores_per_target):

    def distance(cord1, cord2):

        lat1 = float(cord1.split(',')[0][1:])
        lon1 = float(cord1.split(',')[1].replace(' ', '')[0:-1])
        lat2 = float(cord2.split(',')[0][1:])
        lon2 = float(cord2.split(',')[1].replace(' ', '')[0:-1])

        # approximate radius of earth in km
        R = 6373.0

        dlon = lon2 - lon1
        dlat = lat2 - lat1

        a = sin(dlat / 2)**2 + cos(lat1) * cos(lat2) * sin(dlon / 2)**2
        c = 2 * atan2(sqrt(a), sqrt(1 - a))

        distance = R * c

        return distance

    df_temp = df_targets.copy()

    for storeid in df_stores.StoreID:
        df_temp.loc.__setitem__((slice(None), storeid), np.nan)

    for treat in df.StoreID:
        cord2 = df[df.StoreID == treat].Coordinates.values[0]
        df_temp[treat] = df_temp.Coordinates.apply(lambda x:distance(x, cord2))

    closed_stores = {}
    dist2_stores = {}
    sel_stores = []

    for i, store in enumerate(df_temp.StoreID):
        indexs = df_temp[df_temp.StoreID==store][df_temp.columns[-df.StoreID.shape[0]:]].transpose().sort_values(by=i).iloc[:80]
        count = 0
        A = []
        B = []
        for cls_str in indexs.index[1:80].tolist():
            if cls_str not in sel_stores:
                A.append(cls_str)
                count+=1
                B.append(indexs.loc[cls_str, i])
            if count == stores_per_target:
                break

        #A = [x for x in indexs.index[1:3].tolist() if x not in sel_stores]
        closed_stores[store] = A
        dist2_stores[store] = B
        for x in A:
            sel_stores.append(x)

    pd_df = {}
    strs = []
    dists = []
    indexs = []
    for store in (closed_stores.keys()):
        indexs.append(store)
        for store, distance in zip(closed_stores[store], dist2_stores[store]):
            strs.append(store)
            dists.append(distance)

        for n in range(stores_per_target):
            pd_df['Control_'+str(n)] = strs[n::stores_per_target]

        for n in range(2):
            pd_df['Ctrldist_'+str(n)] = dists[n::stores_per_target]

    df_clst = pd.DataFrame(pd_df, index=indexs)
    df_clst.index.names = ['Treatment']

    return df_clst

def Week_Analysis(ctrl_dates, treat_dates, df, store_treats, weeks = [52, 12, 12], Format='%y/%m/%d'):

    #Defining historical, control and treatment dates
    datetime_ctr = ctrl_dates
    datetime_tar = treat_dates

    # Generating dates for the analysis
    datetime_ctr = [datetime.strptime(datetime_ctr[0], Format).date(), datetime.strptime(datetime_ctr[1], Format).date()]
    datetime_tar = [datetime.strptime(datetime_tar[0], Format).date(), datetime.strptime(datetime_tar[1], Format).date()]

    # Defining indexs for the data related to the dates of interest
    hist_indexs = df[(df['Invoice_Date'] < np.datetime64(datetime_ctr[0]))].index
    ctrl_indexs = df[(df['Invoice_Date'] >= np.datetime64(datetime_ctr[0])) &
                     (df['Invoice_Date'] <= np.datetime64(datetime_ctr[1]))].index
    treat_indexs = df[(df['Invoice_Date'] >= np.datetime64(datetime_tar[0])) &
                      (df['Invoice_Date'] <= np.datetime64(datetime_tar[1]))].index

    # Identifying dates of interest with specific labels
    df.loc.__setitem__((hist_indexs, 'Label'), 'historical')
    df.loc.__setitem__((ctrl_indexs, 'Label'), 'control')
    df.loc.__setitem__((treat_indexs, 'Label'), 'treatment')

    # Removing stores with not enough data for the analysis, to avoid bias
    df_temp = df[(~df.Label.isin(['control', 'treatment']))].groupby('StoreID').Invoice_Date.nunique().to_frame()
    df_temp.Invoice_Date = df_temp.Invoice_Date.apply(lambda x:np.floor(x/7))
    df_temp.reset_index(inplace=True)
    stores2drop = df_temp[(df_temp.Invoice_Date < weeks[0])].StoreID.tolist()

    for i, label in enumerate(['control', 'treatment']):
        df_temp = df[df.Label == label].groupby('StoreID').Invoice_Date.nunique().to_frame()
        df_temp.Invoice_Date = df_temp.Invoice_Date.apply(lambda x:np.ceil(x/7))
        df_temp.reset_index(inplace=True)
        stores2drop += df_temp[(df_temp.Invoice_Date < weeks[i+1])].StoreID.tolist()

    # Removing
    stores2drop = [store for store in list(set(stores2drop)) if store not in store_treats]
    df.drop(df[df.StoreID.isin(stores2drop)].index.tolist(), axis=0, inplace=True)
    df.reset_index(inplace=True)

    return df

def daily_aggregation(df_trans, label, case=0):

    if case==0:
        df = df_trans[df_trans.Label==label].groupby(['StoreID', 'Invoice_Date'])['Sales', 'Gross_Margin', 'QTY'].sum()
        df.reset_index(inplace=True)
        df['Transactions'] = df_trans[df_trans.Label==label].groupby(['StoreID',
                                                                               'Invoice_Date']).Invoice_Number.nunique().values
        df['SKUs'] = df_trans[df_trans.Label==label].groupby(['StoreID','Invoice_Date']).SKU.nunique().values

    elif case==1:
        df = df_trans[~df_trans.Label.isin(label)].groupby(['StoreID', 'Invoice_Date'])['Sales', 'Gross_Margin', 'QTY'].sum()
        df.reset_index(inplace=True)
        df['Transactions'] = df_trans[~df_trans.Label.isin(label)].groupby(['StoreID',
                                                                               'Invoice_Date']).Invoice_Number.nunique().values
        df['SKUs'] = df_trans[~df_trans.Label.isin(label)].groupby(['StoreID','Invoice_Date']).SKU.nunique().values

    return df

def weekly_aggregation(df_trans, label, case=0):

    if case==0:

        # Creating the feature Week to determine the invoice week accordingly with the date used
        dic_2_weeks = {}
        for i, date in enumerate(df_trans[df_trans.Label == label].Invoice_Date.dt.strftime('%Y-%m-%d').unique()):
            dic_2_weeks[date] = i

        # Function to determine the week
        def days2weeks(date, days_dict):
            return int(np.floor(days_dict[date]/7+1))

        # Creating the week feature
        df_trans.loc.__setitem__((df_trans[df_trans.Label == label].index, 'Week'),
                             df_trans[df_trans.Label == label].Invoice_Date.dt.strftime('%Y-%m-%d').apply(
                                 lambda x:days2weeks(x, dic_2_weeks)))

        df = df_trans[df_trans.Label==label].groupby(['StoreID', 'Week'])['Sales', 'Gross_Margin', 'QTY'].sum()
        df.reset_index(inplace=True)
        df['Transactions'] = df_trans[df_trans.Label==label].groupby(['StoreID',
                                                                               'Week']).Invoice_Number.nunique().values
        df['SKUs'] = df_trans[df_trans.Label==label].groupby(['StoreID','Week']).SKU.nunique().values

    elif case==1:

        # Creating the feature Week to determine the invoice week accordingly with the date used
        dic_2_weeks = {}
        for i, week in enumerate(df_trans[~df_trans.Label.isin(label)].Invoice_Date.dt.strftime('%Y-%m-%d').unique()):
            dic_2_weeks[week] = i

        # Function to determine the week
        def days2weeks(date, days_dict):
            return int(np.ceil(days_dict[date]/7))

        # Creating the week feature
        df_trans.loc.__setitem__((df_trans[~df_trans.Label.isin(label)].index, 'Week'),
                             df_trans[~df_trans.Label.isin(label)].Invoice_Date.dt.strftime('%Y-%m-%d').apply(
                                 lambda x:days2weeks(x, dic_2_weeks)))

        df = df_trans[~df_trans.Label.isin(label)].groupby(['StoreID', 'Week'])['Sales', 'Gross_Margin', 'QTY'].sum()
        df.reset_index(inplace=True)
        df['Transactions'] = df_trans[~df_trans.Label.isin(label)].groupby(['StoreID',
                                                                               'Week']).Invoice_Number.nunique().values
        df['SKUs'] = df_trans[~df_trans.Label.isin(label)].groupby(['StoreID','Week']).SKU.nunique().values

    return df

def Stores_Relations(treats, relations, ctrl_vars, Storespertreat, step=1000):

    strs2match = Storespertreat

    rel_stores = {}
    sel_stores = treats.StoreID.tolist()

    for store in treats.StoreID:

        indexs = treats[treats.StoreID==store][ctrl_vars].iloc[0].tolist()
        sim_stores = relations.loc[(indexs[0], indexs[1])].values[0]
        rel_stores[store] = [ID for ID in sim_stores if ID not in sel_stores][0:strs2match]
        if len(rel_stores[store])< strs2match:
            sim_stores = list(relations.loc[(indexs[0]-step, indexs[1])].values[0])
            rel_stores[store] = [ID for ID in sim_stores+rel_stores[store] if ID not in sel_stores][0:strs2match]
        sel_stores += rel_stores[store]

    treatment_store = []
    control_store_1 = []
    control_store_2 = []

    for store in rel_stores.keys():

        treatment_store.append(store)

        for i, ctrl in enumerate(rel_stores[store]):
            if i == 0:
                control_store_1.append(ctrl)
            else:
                control_store_2.append(ctrl)

    stores_relations = pd.DataFrame({'treatment_store':treatment_store,
                                     'control_store_1':control_store_1,
                                     'control_store_2':control_store_2})

    stores_relations.set_index('treatment_store', inplace=True)

    return stores_relations

def Define_targets(relations, region_units):

    treatment_store = []
    control_store = []

    for store in relations.keys():

        for i, ctrl in enumerate(relations[store]):
            treatment_store.append(store)
            control_store.append(ctrl)

    df_targets = pd.DataFrame({'Treatment_store':treatment_store,
                                    'Control_store':control_store})

    df_targets.set_index('Treatment_store', inplace=True)
    df_targets.reset_index(inplace=True)
    df_targets = df_targets.merge(region_units, left_on='Treatment_store',
                                  right_on='StoreID', suffixes=(False, False), how='left')
    df_targets.drop('StoreID', axis=1, inplace=True)

    return df_targets

def Treatment_Dataframe(df_targets, region_sales, comp_period, test_period, Format='%y/%m/%d'):

    datetime_ctr = [datetime.strptime(comp_period[0], Format).date(),
                    datetime.strptime(comp_period[1], Format).date()]
    datetime_tar = [datetime.strptime(test_period[0], Format).date(),
                    datetime.strptime(test_period[1], Format).date()]

    stores = df_targets.Target.tolist()# + df_targets.Ctrl_1.tolist() + df_targets.Ctrl_2.tolist()

    df_tar = region_sales[region_sales.StoreID.isin(stores)].copy()
    df_tar.reset_index(drop=True, inplace=True)
    df_tar.loc[:,'Label'] = np.nan
    indexs1 = df_tar[(df_tar['Week_End'] >= np.datetime64(datetime_ctr[0])) &
                     (df_tar['Week_End'] <= np.datetime64(datetime_ctr[1]))].index
    indexs2 = df_tar[(df_tar['Week_End'] >= np.datetime64(datetime_tar[0])) &
                     (df_tar['Week_End'] <= np.datetime64(datetime_tar[1]))].index
    df_tar.loc.__setitem__((indexs1, 'Label'), 'Control')
    df_tar.loc.__setitem__((indexs2, 'Label'), 'Treatment')
    df_tar.reset_index(drop=True, inplace=True)

    return df_tar

def Control_Dataframe(df_ctrls, region_sales, comp_period, test_period, Format='%y/%m/%d'):

    stores1 = df_ctrls.Ctrl_1.tolist()
    stores2 = df_ctrls.Ctrl_2.tolist()

    datetime_ctr = [datetime.strptime(comp_period[0], Format).date(),
                    datetime.strptime(comp_period[1], Format).date()]
    datetime_tar = [datetime.strptime(test_period[0], Format).date(),
                    datetime.strptime(test_period[1], Format).date()]

    df_ctr = region_sales[region_sales.StoreID.isin(stores1)].copy()
    df_ctr.reset_index(drop=True, inplace=True)
    db = region_sales[region_sales.StoreID.isin(stores2)][['Week','Gross_Margin']].copy()
    db.reset_index(drop=True, inplace=True)
    df_ctr.loc[:,'Label'] = np.nan
    df_ctr.loc[:,'Gross_Margin_mean'] = np.nan
    df_ctr.loc[:,'Gross_Margin_2'] = np.nan
    df_ctr.loc.__setitem__((slice(None), 'Gross_Margin_2'), db.Gross_Margin)

    indexs1 = df_ctr[(df_ctr['Week_End'] >= np.datetime64(datetime_ctr[0])) &
                     (df_ctr['Week_End'] <= np.datetime64(datetime_ctr[1]))].index
    indexs2 = df_ctr[(df_ctr['Week_End'] >= np.datetime64(datetime_tar[0])) &
                     (df_ctr['Week_End'] <= np.datetime64(datetime_tar[1]))].index

    df_ctr.loc.__setitem__((indexs1, 'Label'), 'Control')
    df_ctr.loc.__setitem__((indexs2, 'Label'), 'Treatment')
    df_ctr.loc.__setitem__((slice(None), 'Gross_Margin_mean'),
                           df_ctr[['Gross_Margin', 'Gross_Margin_2']].mean(axis=1))

    return df_ctr

def Units_distributions(Test_period, Comparison_period):

    sns.displot(Comparison_period[Comparison_period.Label.isin(['Control', 'Treatment'])],
                x='Gross_Margin_mean', hue='Label', element="step")
    plt.title('Comparison_period')
    sns.displot(Test_period[Test_period.Label.isin(['Control', 'Treatment'])],
                x='Gross_Margin', hue='Label', element="step")
    plt.title('Test_period')

def GrossMargin_plot(Test_period, Comparison_period):

    f, ax = plt.subplots(1, 2,figsize=(13,4))
    sns.lineplot(x='Week_End', y='Gross_Margin', data=Test_period[Test_period.Label == 'Control'],
                 label='Treatment', ax=ax[0])
    sns.lineplot(x='Week_End', y='Gross_Margin_mean', data=Comparison_period[Comparison_period.Label == 'Control'],
                 label='Control', ax=ax[0])
    ax[0].tick_params(axis='x', rotation=45)
    ax[0].set_title('Comparison_period')
    ax[0].grid(True)

    sns.lineplot(x='Week_End', y='Gross_Margin', data=Test_period[Test_period.Label == 'Treatment'],
                 label='Treatment', ax=ax[1])
    sns.lineplot(x='Week_End', y='Gross_Margin_mean', data=Comparison_period[Comparison_period.Label == 'Treatment'],
                 label='Control', ax=ax[1])
    ax[1].tick_params(axis='x', rotation=45)
    ax[1].set_title('Test_period')
    ax[1].grid(True)

def Boxplots(Test_period, Comparison_period):

    cols1 = ['Week_End', 'Gross_Margin']
    cols2 = ['Week_End', 'Gross_Margin_mean']
    dfA = Test_period[Test_period.Label=='Treatment'][cols1]
    dfA.loc.__setitem__((slice(None), 'Gross_Margin_mean'), Comparison_period[Comparison_period.Label=='Treatment'][cols2[1]])
    dfA.reset_index(drop=True, inplace=True)
    dfA.rename(columns = {'Gross_Margin_mean':'Control', 'Gross_Margin':'Treatment'}, inplace=True)
    dfB = Test_period[Test_period.Label=='Control'][cols1]
    dfB.loc.__setitem__((slice(None), 'Gross_Margin_mean'), Comparison_period[Comparison_period.Label=='Control'][cols2[1]])
    dfB.reset_index(drop=True, inplace=True)
    dfB.rename(columns = {'Gross_Margin_mean':'Control', 'Gross_Margin':'Treatment'}, inplace=True)

    f, ax = plt.subplots(1, 2,figsize=(13,4))
    sns.boxplot(data=dfB[['Control', 'Treatment']],showmeans=True, palette="coolwarm", ax=ax[0],
                meanprops={"marker":"s","markerfacecolor":"white", "markeredgecolor":"blue"})

    ax[0].set_title('Comparison_period')
    ax[0].grid(True)

    sns.lineplot(x='Week_End', y='Gross_Margin', data=Test_period[Test_period.Label == 'Treatment'], ax=ax[1])
    sns.boxplot(data=dfA[['Control', 'Treatment']],showmeans=True, palette="coolwarm", ax=ax[1],
               meanprops={"marker":"s","markerfacecolor":"white", "markeredgecolor":"blue"})
    ax[1].set_title('Test_period')
    ax[1].grid(True)

def Significance_Test(Test_period, Comparison_period, col1, col2, case=0):

    cols1 = ['Week_End', 'Gross_Margin']
    cols2 = ['Week_End', 'Gross_Margin_mean']
    dfA = Test_period[Test_period.Label=='Treatment'][cols1]
    dfA.loc.__setitem__((slice(None), 'Gross_Margin_mean'), Comparison_period[Comparison_period.Label=='Treatment'][cols2[1]])
    dfA.reset_index(drop=True, inplace=True)
    dfA.rename(columns = {'Gross_Margin_mean':'Control', 'Gross_Margin':'Treatment'}, inplace=True)
    dfB = Test_period[Test_period.Label=='Control'][cols1]
    dfB.loc.__setitem__((slice(None), 'Gross_Margin_mean'), Comparison_period[Comparison_period.Label=='Control'][cols2[1]])
    dfB.reset_index(drop=True, inplace=True)
    dfB.rename(columns = {'Gross_Margin_mean':'Control', 'Gross_Margin':'Treatment'}, inplace=True)

    if case == 0:
        t_stat, pval = stats.ttest_rel(dfA[col1], dfB[col2])

    elif case == 1:
        t_stat, pval = stats.ttest_rel(dfA[col1], dfA[col2])

    elif case == 2:
        t_stat, pval = stats.ttest_rel(dfB[col1], dfB[col2])

    lift_up = round((dfA[col1].mean())/dfB[col1].mean(), 3)*100
    lift_down = round((dfA[col2].mean())/dfB[col2].mean(), 3)*100
    lift = abs(lift_up - lift_down)
    lifts = [lift_up, lift_down]

    display(pd.DataFrame({'Values':[t_stat, pval, str('{0:.2f}'.format(lift))+' %',
                                    str('{0:.2f}'.format(-lift_up))+' %',
                                    str('{0:.2f}'.format(lift_down))+' %']},
                         index=['T-stat.', 'P-val', 'Avg_Lift', 'Avg_Lift_Treatment', 'Avg_Lift_Ctrl']))

    return t_stat, pval, lift

def Boxplot2(Test_period, Comparison_period):

    df = pd.concat([Test_period[['StoreID', 'Week_End', 'Label',
                                 'Gross_Margin']], Comparison_period[['StoreID', 'Week_End',
                                                                      'Label','Gross_Margin']]])

    dfA = df[df.StoreID.isin(Test_period.StoreID.unique())].copy()
    dfA.loc[dfA[dfA.Label == 'Treatment'].index, 'Test_Period'] = dfA.loc[dfA[dfA.Label ==
                                                                       'Treatment'].index, 'Gross_Margin']
    dfA.loc[dfA[dfA.Label == 'Control'].index, 'Comparison_Period'] = dfA.loc[dfA[dfA.Label ==
                                                                           'Control'].index, 'Gross_Margin']

    dfB = df[df.StoreID.isin(Comparison_period.StoreID.unique())].copy()
    dfB.loc[dfB[dfB.Label == 'Treatment'].index, 'Test_Period'] = dfB.loc[dfB[dfB.Label == 'Treatment'].index, 'Gross_Margin']
    dfB.loc[dfB[dfB.Label == 'Control'].index, 'Comparison_Period'] = dfB.loc[dfB[dfB.Label == 'Control'].index, 'Gross_Margin']

    f, ax = plt.subplots(1, 2,figsize=(13,4))
    sns.boxplot(data=dfB[['Comparison_Period', 'Test_Period']],showmeans=True, palette="coolwarm", ax=ax[0],
                meanprops={"marker":"s","markerfacecolor":"white", "markeredgecolor":"blue"})

    ax[0].set_title('Control Units Analysis')
    ax[0].grid(True)

    lines = ax[0].get_lines()
    categories = ax[0].get_xticks()
    vals=[dfB[(dfB.Comparison_Period!=np.nan) & (dfB.Label=='Control')].Gross_Margin.mean(),
         dfB[(dfB.Comparison_Period!=np.nan) & (dfB.Label=='Treatment')].Gross_Margin.mean()]

    for cat, val in zip(categories, vals):
        # every 4th line at the interval of 6 is median line
        # 0 -> p25 1 -> p75 2 -> lower whisker 3 -> upper whisker 4 -> p50 5 -> upper extreme value
        y = round(val)

        ax[0].text(
            cat,
            y,
            f'{y}',
            ha='center',
            va='center',
            fontweight='bold',
            size=10,
            color='black',
            bbox=dict(facecolor='white'))

    sns.lineplot(x='Week_End', y='Gross_Margin', data=Test_period[Test_period.Label == 'Treatment'], ax=ax[1])
    sns.boxplot(data=dfA[['Comparison_Period', 'Test_Period']],showmeans=True, palette="coolwarm", ax=ax[1],
               meanprops={"marker":"s","markerfacecolor":"white", "markeredgecolor":"blue"})
    ax[1].set_title('Treatment Units Analysis')
    ax[1].grid(True)

    lines = ax[1].get_lines()
    categories = ax[1].get_xticks()
    vals=[dfA[(dfA.Comparison_Period!=np.nan) & (dfA.Label=='Control')].Gross_Margin.mean(),
         dfA[(dfA.Comparison_Period!=np.nan) & (dfA.Label=='Treatment')].Gross_Margin.mean()]

    for cat, val in zip(categories, vals):
        # every 4th line at the interval of 6 is median line
        # 0 -> p25 1 -> p75 2 -> lower whisker 3 -> upper whisker 4 -> p50 5 -> upper extreme value
        y = round(val)

        ax[1].text(
            cat,
            y,
            f'{y}',
            ha='center',
            va='center',
            fontweight='bold',
            size=10,
            color='black',
            bbox=dict(facecolor='white'))

def Corr_Control_Variables(df_stores, df_trans, ctrl_vars, treat_var):

    df_strs = df_stores.copy()
    df_strs.City = df_strs.City.astype('category').cat.codes
    df_strs.Region = df_strs.Region.astype('category').cat.codes
    df_strs.State = df_strs.State.astype('category').cat.codes

    df = df_trans.groupby(['StoreID']).sum()
    df.reset_index(inplace=True)
    df = df[['StoreID', 'Gross_Margin']].merge(df_strs, left_on='StoreID', right_on='StoreID', suffixes=(False, False))
    cols = df_strs.columns.to_list()[1:]+['Gross_Margin']

    display(df[cols].corr())

    plt.figure(figsize=(10, 10));
    g = sns.pairplot(df,
             y_vars=treat_var,
             x_vars=ctrl_vars,
             kind='reg',
             plot_kws={'line_kws':{'color':'red'}, 'scatter_kws': {'alpha': 0.2}});
    g.fig.suptitle("Control varibales vs. Performance metric")

def plot_trend(df_trans, df_treats):

    datetime_ctr = ['15/04/29', '15/07/21']
    datetime_tar = ['16/04/29', '16/07/21']
    df_trans = Week_Analysis(datetime_ctr, datetime_tar, df_trans, df_treats.StoreID.tolist())
    df_trans.head()
    df_ctrl = weekly_aggregation(df_trans, 'control')
    df_targ = weekly_aggregation(df_trans, 'treatment')
    plt.figure(figsize=(9, 4));
    sns.lineplot(x="Week", y="Transactions", label='Control', data=df_ctrl)
    sns.lineplot(x="Week", y="Transactions", label='Treatment', data=df_targ)
    plt.xlim(1, 12)
    plt.title('Gross Margin behaviour in the comparison period')
    plt.grid()

def training_data(df_weeksales, df_stores, comp_period, test_period, Format='%y/%m/%d'):

    # Defining dates for the clustering
    df_weekly = df_weeksales.copy()

    datetime_ctr = [datetime.strptime(comp_period[0], Format).date(),
                    datetime.strptime(comp_period[1], Format).date()]
    datetime_tar = [datetime.strptime(test_period[0], Format).date(),
                    datetime.strptime(test_period[1], Format).date()]

    # Creating the training dataset
    stores = df_stores.StoreID.unique().tolist()
    df = df_weekly[(df_weekly.StoreID.isin(stores))]
    df_train = df_weekly[(df_weekly['Week_End'] >= np.datetime64(datetime_ctr[0])) &
                         (df_weekly['Week_End'] <= np.datetime64(datetime_ctr[1]))].copy()
    df_train.reset_index(drop=True, inplace=True)

    dict_vals = {}
    for i, val in enumerate(df_train.Week.unique()):
        dict_vals[val] = i+1
    df_train.Week = df_train.Week.apply(lambda x:dict_vals[x])

    # Creating the test dataset
    df_test = df_weekly[(df_weekly.StoreID.isin(stores)) &
                        (df_weekly['Week_End'] >= np.datetime64(datetime_tar[0])) &
                        (df_weekly['Week_End'] <= np.datetime64(datetime_tar[1]))].copy()
    df_test.reset_index(drop=True, inplace=True)
    dict_vals = {}

    for i, val in enumerate(df_test.Week.unique()):
        dict_vals[val] = i+1
    df_test['Week'] = df_test.Week.apply(lambda x:dict_vals[x])

    return df_train, df_test

def dict_closest_units(indexs, distances, df_train, df_test, df_treats):

    sel_ctrl_units = []
    relation_units = {}
    distance_units = {}
    for n in range(indexs.shape[0]):
        ctrl_units = []
        dist_units = []

        counter = 0
        while ((len(ctrl_units) < 2)):
            if df_train.StoreID.loc[indexs[n, counter]] not in (sel_ctrl_units +
                                                                df_treats.StoreID.tolist()):
                sel_ctrl_units.append(df_train.StoreID.loc[indexs[n, counter]])
                ctrl_units.append(df_train.StoreID.loc[indexs[n, counter]])
                dist_units.append(distances[n, counter])

            counter +=1

        relation_units[df_test.StoreID.loc[n]] = ctrl_units
        distance_units[df_test.StoreID.loc[n]] = dist_units

    return relation_units, distance_units

def Control_Units(indexs, distances, df_train, df_test, df_treats, df_stores):

    relation_units, distance_units = dict_closest_units(indexs, distances,
                                                        df_train, df_test, df_treats)

    df = pd.merge(pd.DataFrame(relation_units).transpose(),
         pd.DataFrame(distance_units).transpose(), left_index=True, right_index=True)
    df.reset_index(inplace=True)
    df.rename(columns={'index':'Target','0_x':'Ctrl_1', '1_x':'Ctrl_2',
                      '0_y':'Dist2Crtl_1', '1_y':'Dist2Crtl_2'}, inplace=True)

    df['AvgSales_Tar'] = df_treats.AvgMonthSales

    df = df.merge(df_stores[df_stores.StoreID.isin(df.Ctrl_1)][['StoreID',
                  'AvgMonthSales']], left_on='Ctrl_1', right_on='StoreID',
                  suffixes=(False, False), how='left')
    df.rename(columns={'AvgMonthSales':'AvgSales_Ctr1'}, inplace=True)
    df.drop('StoreID', axis=1, inplace=True)

    df = df.merge(df_stores[df_stores.StoreID.isin(df.Ctrl_2)][['StoreID',
                  'AvgMonthSales']], left_on='Ctrl_2', right_on='StoreID',
                  suffixes=(False, False), how='left')
    df.rename(columns={'AvgMonthSales':'AvgSales_Ctr2'}, inplace=True)
    df.drop('StoreID', axis=1, inplace=True)

    return df

def Treat_Ctrl_Relations(indexs, distances, df_train, df_test, df_treats):

    relation_units, distance_units = dict_closest_units(indexs, distances, df_train, df_test, df_treats)

    treatment_store = []
    control_store_1 = []
    control_store_2 = []

    for store in relation_units.keys():

        treatment_store.append(store)

        for i, ctrl in enumerate(relation_units[store]):
            if i == 0:
                control_store_1.append(ctrl)
            else:
                control_store_2.append(ctrl)

    stores_relations = pd.DataFrame({'treatment_store':treatment_store,
                                    'control_store_1':control_store_1,
                                    'control_store_2':control_store_2})

    stores_relations.set_index('treatment_store', inplace=True)

    return stores_relations

def Closest_Stores(stores, sales, treats, region, limits, dates,
                   leafs=10, clusters=22, Format='%y/%m/%d' ):

    stores = stores[(stores.Region.isin(region)) &
                       (stores.AvgMonthSales >= limits[0]) &
                       (stores.AvgMonthSales <= limits[1])]
    stores.reset_index(drop=True, inplace=True)

    sales = sales[sales.StoreID.isin(stores.StoreID)]
    sales.reset_index(drop=True, inplace=True)

    treats = treats[treats.Region.isin(region)]
    treats.reset_index(drop=True, inplace=True)

    df_train, df_test = training_data(sales, stores,
                                      dates[0], dates[1], Format=Format)
    df_train = df_train.groupby(['StoreID']).sum()
    df_train.reset_index(inplace=True)

    df_train = df_train.merge(stores[['StoreID', 'AvgMonthSales']], left_on='StoreID',
                              right_on='StoreID', suffixes=(False, False))

    tree = KDTree(df_train[['CountUnique_Invoice_Number', 'AvgMonthSales']], leaf_size=leafs)

    df_test = df_train[df_train.StoreID.isin(treats.StoreID)].groupby(['StoreID']).sum()
    df_test.reset_index(inplace=True)

    nearest_dist, nearest_ind = tree.query(df_test[['CountUnique_Invoice_Number', 'AvgMonthSales']], k=clusters)

    Relations = Control_Units(nearest_ind, nearest_dist, df_train, df_test, treats, stores)

    return Relations

def Significance_Test2(Test_period, Comparison_period, col1, col2):

    df = pd.concat([Test_period[['StoreID', 'Week_End', 'Label',
                                 'Gross_Margin']], Comparison_period[['StoreID', 'Week_End',
                                                                      'Label','Gross_Margin']]])

    dfA = df[df.StoreID.isin(Test_period.StoreID.unique())].copy()
    dfA.loc[dfA[dfA.Label == 'Treatment'].index, 'Test_Period'] = dfA.loc[dfA[dfA.Label ==
                                                                       'Treatment'].index, 'Gross_Margin']
    dfA.loc[dfA[dfA.Label == 'Control'].index, 'Comparison_Period'] = dfA.loc[dfA[dfA.Label ==
                                                                           'Control'].index, 'Gross_Margin']

    dfB = df[df.StoreID.isin(Comparison_period.StoreID.unique())].copy()
    dfB.loc[dfB[dfB.Label == 'Treatment'].index, 'Test_Period'] = dfB.loc[dfB[dfB.Label == 'Treatment'].index, 'Gross_Margin']
    dfB.loc[dfB[dfB.Label == 'Control'].index, 'Comparison_Period'] = dfB.loc[dfB[dfB.Label == 'Control'].index, 'Gross_Margin']

    t_stat, pval = stats.ttest_rel(dfA[(dfA.Comparison_Period!=np.nan) &
                                           (dfA.Label==col1)].Gross_Margin.values,
                                       dfA[(dfA.Comparison_Period!=np.nan) &
                                           (dfA.Label==col2)].Gross_Margin.values)

    A1 = dfA[dfA.Label==col1].Gross_Margin.mean()
    A2 = dfA[dfA.Label==col2].Gross_Margin.mean()
    B1 = dfB[dfB.Label==col1].Gross_Margin.mean()
    B2 = dfB[dfB.Label==col2].Gross_Margin.mean()

    lift_up = -round((A2 - A1)/A2, 3)*100
    lift_down = round((B2 - B1)/B2, 3)*100
    lift = abs(lift_up - lift_down)
    lifts = [lift_up, lift_down]

    display(pd.DataFrame({'Values':[t_stat, pval, str('{0:.2f}'.format(lift))+' %',
                                    str('{0:.2f}'.format(abs(lift_up)))+' %',
                                    str('{0:.2f}'.format(abs(lift_down)))+' %']},
                         index=['T-stat.', 'P-val', 'Avg_Lift', 'Avg_Lift_Treatment', 'Avg_Lift_Ctrl']))

    return t_stat, pval, lift


from django.db.models import prefetch_related_objects

from sentry.api.serializers import Serializer, register, serialize
from sentry.api.serializers.models.user import UserSerializer
from sentry.incidents.models import IncidentActivity

@register(IncidentActivity)
class IncidentActivitySerializer(Serializer):
    def get_attrs(self, item_list, user, **kwargs):
        prefetch_related_objects(item_list, "incident__organization")
        prefetch_related_objects(item_list, "user")
        user_serializer = UserSerializer()
        serialized_users = serialize(
            {item.user for item in item_list if item.user_id},
            user=user,
            serializer=user_serializer,
        )
        user_lookup = {user["id"]: user for user in serialized_users}
        return {item: {"user": user_lookup.get(str(item.user_id))} for item in item_list}

    def serialize(self, obj, attrs, user):
        incident = obj.incident

        return {
            "id": str(obj.id),
            "incidentIdentifier": str(incident.identifier),
            "user": attrs["user"],
            "type": obj.type,
            "value": obj.value,
            "previousValue": obj.previous_value,
            "comment": obj.comment,
            "dateCreated": obj.date_added,
        }


import matplotlib.pyplot as plt
import math

import networkx as nx
import numpy as np
import pandas as pd
from random import *
import time
import logging
from scipy.stats import pearsonr

logger = logging.getLogger()
logger.setLevel(logging.INFO)
# 定义handler的输出格式
#logger to console
ch = logging.StreamHandler()
ch.setLevel(logging.INFO)
formatter = logging.Formatter("%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s")
#fh.setFormatter(formatter)
ch.setFormatter(formatter)
logger.addHandler(ch)

f1 = 'D:\\py\\data\\chenjuanjuan\\k-shell.xlsx'
f_correlation = 'D:\\py\\data\\chenjuanjuan\\correlation.csv'

logger.info("loading data...")
time_start=time.time()
df = pd.read_excel(f1)
df = df.loc[::, ['一层节点数量', '二层节点数量', '三层节点数量', 'Performance']]

correlation = pd.DataFrame(columns=['beta', 'correlation', 'p-value'])

data = df.values.tolist()
layer1=[]
layer2=[]
layer3=[]
performance = []

#reorganize data
for i in range(len(data)):
    if (np.isnan(float(data[i][0]))):
        layer1.append(0)
    else:
        layer1.append(int(data[i][0]))

    if (np.isnan(float(data[i][1]))):
        layer2.append(0)
    else:
        layer2.append(int(data[i][1]))

    if (np.isnan(float(data[i][2]))):
        layer3.append(0)
    else:
        layer3.append(int(data[i][2]))

    performance.append(float(data[i][3]))

beta=-20.001
line = 0
step= 10
x=[]
y=[]

'''
score=[]
for i in range(len(layer1)):
    if (layer2[i] == 0):
        score.append(0)
    else:
        score.append(1)
print(pearsonr(score,performance))
'''

while (beta < 20):

    #score = [math.exp(layer1[i]*beta+layer2[i]*beta*2+layer3[i]*beta*3) for i in range(len(layer1))]
    #score = [beta**layer1[i]+beta**layer2[i]+beta**layer3[i] for i in range(len(data))]

    score=[]
    '''
    for i in range(len(layer1)):
        if (layer1[i] ==0):
            score.append(0)
        else:
            score.append(layer1[i]**(beta))
    #score = [layer2[i]**beta for i in range(len(layer1))]
    '''
    for i in range(len(layer1)):
        if (layer1[i] ==0 and layer2[i] ==0):
            score.append(0)
        else:
            s = 0
            if layer1[i] !=0:
                s-= layer1[i]**(beta/2)/1.0
            if layer2[i] !=0:
                s+= layer2[i]**(beta/2)/1.0
            if layer3[i] !=0:
                s+= layer3[i]**(beta/2)/1.0*0

            #score.append((-layer1[i]**(beta)/1.0)+layer2[i]**(beta)/1.0-layer3[i]**beta/1.0)
            score.append(s)
#            print(score[i])
#            exit(0)
    c = pearsonr(score,performance)
    correlation.loc[line] = [beta, c[0], c[1]]

    x.append(beta)
    y.append(c[0])

    if abs(beta) <= 0.99:
        step =0.1
    elif abs(beta) <= 10:
        step = 1
    elif abs(beta) <= 30:
        step = 10
    line += 1
    beta += step

#plt.semilogx(x,y)
plt.plot(x,y)
plt.xlabel('$ \\beta $')
plt.ylabel('correlation')
plt.show()

logger.info("writing results to file..")
correlation.to_csv(f_correlation,index=False)
time_end = time.time()
logger.info('The end,cost time:%d s', time_end - time_start)


import array
import sys
import graphviz as gv

file = open('texto.txt', 'r')
read_file = file.readlines()
file.close()

a = []
transi = []

for i in range(len(read_file)):
    a.append(read_file[i].strip().split(","))

for i in range(len(a)):
    if(i==0):
        Alpha = a[i]
    if(i==1):
        estados = a[i]
    if (i == 2):
        trans = a[i]
    if (i == 3):
        ini = a[i]
    if (i == 4):
        final = a[i]
for i in range(len(trans)):
    cadena = list(trans[i])
    transi.append(cadena)

print(a)
print(estados)
print(transi)
print(ini)
print(final)

def draw(alafabeto, estados, inicial, transiccion, final):
    print("inicio:", str(inicial))
    g = gv.Digraph(format='svg')
    g.graph_attr['rankdir'] = 'LR'
    g.node('ini', shape="point")
    for e in estados:
        if e in final:
            g.node(e, shape="doublecircle")
        else:
            g.node(e)
        if e in inicial:
            g.edge('ini', e)

    for t in transiccion:
        if t[2] not in alafabeto:
            return 0
        g.edge(t[0], t[1], label=str(t[2]))
    g.render(view=False )

#draw(Alpha, estados, ini, transi, final)

def printMatriz(Matriz):
    for i in range(len(Matriz)):
        print(*Matriz[i])

def getW(estado, letra, matriz, alpha):
    #La funcion nos retorna  el estado dada la tabla de transicion en el estado "estado" para una entrada "letra" en el alfabeto
    #alpha
    j = int(estado)
    pos = 0
    for i in range(len(alpha)):
        if alpha[i] == letra:
            return matriz[j][i]

print("Alfabeto:")
print(Alpha)
print("Estados")
print(len(estados))
print("Los estados son los elementos: ")
for j in range(len(estados)):
    print(estados[j])

Matriz = []
for i in range(len(estados)):
    Matriz.append([i])
    for j in range(len(Alpha) - 1):
        Matriz[i].append(None)

for t in transi:
    i = int(t[0])
    aux = t[2]
    j = Alpha.index(aux)
    S = t[1]
    Matriz[i][j] = S

printMatriz(Matriz)
print("Estado final")
for i in range (len(final)):
    print(final[i])

print("Ingrese el numero de pruebas ")
N = int(sys.stdin.readline())
for i in range(N):
    print("INGRESE LA PALABRA")
    S = (sys.stdin.readline().strip())
    estadoa = 0
    print("Estado INICIAL q_" + str(estadoa))  # ESTADO INICIAL
    for j in range(len(S)):
        estadoa = getW(estadoa, S[j], Matriz, Alpha)
        print("Estado actual q_" + str(estadoa)) #RECORRIDO DE ESTADOS

    if str(estadoa) in final : #se hace el cast porque estadosf los reconoce como strings
        print("ACEPTA LA PALARBA: " + S.strip())
    else:
        print("NO LO ACEPTA: " + S)


from django.urls import path
from .import views

urlpatterns = [
    path( '', views.HomePageView.as_view() , name = 'homepageview'),
    path( 'login/', views.LoginView.as_view(), name = 'loginview'),
    path( 'sign_up/' , views.SignUpView, name = 'signup', ),

]


import pandas as pd
import nltk
import numpy as np
from nltk.corpus import stopwords
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.model_selection import train_test_split
from sklearn import naive_bayes
from google_play_scraper import Sort, reviews
from sklearn.metrics import accuracy_score
import play_scraper
import eel
from textblob import TextBlob
import datetime

eel.init('web')

def get_reviews(url):
    result = reviews(
    url,
    lang='en',
    country='us',
    sort=Sort.NEWEST,
    count=200,
    )
    f=[]
    for i in result:
        f.append(i['content'])
    return f

df = pd.read_csv("web/training.txt", sep='\t', names=['liked','txt'])
df.head()
stopset = set(stopwords.words('english'))
vectorizer = TfidfVectorizer(use_idf=True, lowercase=True, strip_accents='ascii', stop_words=stopset)
y = df.liked
x = vectorizer.fit_transform(df.txt.values.astype('U'))
X_train, X_test, y_train, y_test = train_test_split(x, y, random_state=42)
clf = naive_bayes.MultinomialNB()
clf.fit(X_train, y_train)

@eel.expose
def scrap_review(appname):
    a = datetime.datetime.now()
    print("start",a.second)
    raw_review = get_reviews(appname)
    app_reviews_array = np.array(raw_review)
    app_review_vector = vectorizer.transform(app_reviews_array)
    predicts = clf.predict(app_review_vector)
    save_scrapped_result(raw_review,appname,predicts)
    slice_count=[0,0]
    for i in predicts:
        if i==1:
            slice_count[0] +=1
        else:
            slice_count[1] +=1
    b = datetime.datetime.now()
    c = b - a
    print(c.seconds)
    print(slice_count)

    return [slice_count,[(sum(predicts)/len(predicts))*100,sum(predicts)/len(predicts)]]

def save_scrapped_result(rrw,appname,predicts):
    review_sentiments = []
    review_polarity = []
    for i in range(len(rrw)):
            review_sentiments.append("{}\t{}\n".format('1' if predicts[i]==1 else '0',rrw[i]))
    f = open("results/"+appname+".txt", 'w', encoding="utf-8")
    g = open("results/polarity/"+appname+".txt", 'w', encoding="utf-8")
    for i in range(len(rrw)):
        pol = TextBlob(rrw[i]).sentiment.polarity
        review_polarity.append("{:>4}\t\t{}\t{}\n".format(round(pol,1),(1|predicts[i]) if pol>0 else '0',rrw[i]))
    for i in review_polarity:
        g.write(i)
    for i in review_sentiments:
        f.write(i)
    f.close()

@eel.expose
def search_app(query):
    app_data = []
    search_result=play_scraper.search(query, page=1)
    for app in search_result[:8 if len(search_result) > 8 else len(search_result)]:
        app_data.append([app['title'],app['icon'],app['app_id'],app['developer']])
    return app_data

eel.start('index.html', size=(1200,600))


# mixins.py

from django.core.exceptions import ImproperlyConfigured

class ClassNameMixin(object):

    def get_context_data(self, **kwargs):
        context = super(ClassNameMixin, self).get_context_data(**kwargs)
        try:
            context['object_type'] = self.class_name
        except AttributeError:
            raise ImproperlyConfigured(
                'Please set class_name to appropriate string.')

        return context