# This script analyzes the runtime of different experiments from 'timing_info'
# The goal is to compute  min, max, avg, std deviation of timing for each '$SOLVER_both' setting
# We will report on min, max, avg, std deviation of timing 'with and without' support computation
# The final results will be put into the $ANALYSES_DIR$ directory

__author__ = "Elaheh"
__date__ = "$Jan 30, 2016 5:35:12 AM$"

import os, sys, shelve
import matplotlib.pyplot as plt
from matplotlib.pyplot import cm
import numpy as np

TIMEOUT = 700.0
SOLVERS = ['z3', 'yices', 'smtinterpol', 'mathsat']
MINING_DIR = 'mining'
ANALYSES_DIR = 'timing_analyses'
TIMING_INFO = 'timing_info'
NUM_OF_MODELS = 395
TIMINGS =  ['jsupport', 'z3_both', 'z3_both_no_sup',
            'yices_both',  'yices_both_no_sup',
            'smtinterpol_both', 'smtinterpol_both_no_sup',
            'mathsat_both', 'mathsat_both_no_sup',
            'z3_k_ind', 'z3_k_ind_no_sup',
            'z3_pdr', 'z3_pdr_no_sup',
            'yices_k_ind', 'yices_k_ind_no_sup',
            'yices_pdr', 'yices_pdr_no_sup',
            'smtinterpol_k_ind', 'smtinterpol_k_ind_no_sup',
            'smtinterpol_pdr', 'smtinterpol_pdr_no_sup',
            'mathsat_k_ind', 'mathsat_k_ind_no_sup',
            'mathsat_pdr', 'mathsat_pdr_no_sup',
            'z3_both_sup', 'z3_k_ind_sup', 'z3_pdr_sup',
            'yices_both_sup', 'yices_k_ind_sup', 'yices_pdr_sup',
            'smtinterpol_both_sup', 'smtinterpol_k_ind_sup', 'smtinterpol_pdr_sup',
            'mathsat_both_sup', 'mathsat_k_ind_sup', 'mathsat_pdr_sup']

LEGENDS =  ['JSupport', 'Z3 with support computation', 'Z3 wihtout support computation',
            'Yices with support computation', 'Yices wihtout support computation',
            'SMTInterpol with support computation', 'SMTInterpol wihtout support computation',
            'MathSAT with support computation', 'MathSAT wihtout support computation']

if not os.path.exists(MINING_DIR):
    print(MINING_DIR + " does not exists! first, try to run mining scripts.")
    sys.exit(-1)

if os.path.exists(ANALYSES_DIR):
    print(ANALYSES_DIR + " alrady exists! first, delete directory: "+ ANALYSES_DIR)
    sys.exit(-1)
os.mkdir(ANALYSES_DIR)

#
# Load timing info
#

reader = shelve.open(os.path.join(MINING_DIR, 'timing_info'))
all_timings = []
for i in range(9):
    all_timings.append(reader[TIMINGS[i]])

# Add support computation time for _both setting
sup_timings = []
for solver in SOLVERS:
    sup_timings.append(reader[solver+'_both_sup'])

reader.close()

#
# Compute min, max, avg, std deviation storing them on disk
#
writer = open(os.path.join(ANALYSES_DIR, 'timing_analyses.txt'), 'a')

for i, legend in enumerate(LEGENDS):
    writer.write('###################  timing report on the \"'+ legend + '\" setting  ###################\n')
    all_timings[i] = [float(x) for x in all_timings[i]]
    # Clean unknown runtimes
    for t, time in enumerate(all_timings[i]):
        if time >= TIMEOUT:
            all_timings[i][t] = float('nan')
    writer.write('\nminimum runtime is: ' + str(min(all_timings[i])))
    writer.write('\nmaximum runtime is: ' + str(max(all_timings[i])))
    writer.write('\npopulation standard deviation runtime is: ' + str(np.nanstd(all_timings[i])))
    writer.write('\naverage runtime is: ' + str(np.nanmean(all_timings[i])))
    writer.write('\n-------------------------------------------------------------------------------------------------')
    writer.write('\nfor '+ str(NUM_OF_MODELS) + ' models, the following shows runtimes in \"'+ legend + '\" setting')
    writer.write('\neach item in the following list is related to a model in the benchmarks.\n\n')
    writer.write(str([round(float(i), 2) for i in all_timings[i]]) + '\n\n\n\n')
writer.close()

#
# Calculate support overhead
# This shows what percentage of the overal runtime is because of support comutation
# Formula:  overhead_percentage = 100 * (SupportRuntime/ Runtime)
#

# required indices in all_timings [1, 3, 5, 7]
for indx, item in enumerate(sup_timings):
    for i in range(len(item)):
        sup_timings[indx][i] = 100.00 * (float(item[i])/ all_timings[indx + 1][i])

# in the following lists, indices show 'z3', 'yices', 'smtinterpol', 'mathsat', respectively
mean_overheads = []
max_overheads = []
min_overheads = []
stdev_overheads = []

for item in sup_timings:
    mean_overheads.append(np.nanmean(item))
    stdev_overheads.append(np.nanstd(item))
    min_overheads.append(min(item))
    max_overheads.append(max(item))
del sup_timings

#
# Report on support runtime overhead
#
writer = open(os.path.join(ANALYSES_DIR, 'sup_overhead.txt'), 'a')
writer.write("this is to report the overhead of support computation on different solvers\n")
writer.write("This report is based on the results when both k_induction and PDR were activated.\n\n")
writer.write("This shows what percentage of the overal runtime is because of support comutation:\n")
writer.write("         Formula:  overhead_percentage = 100 * (SupportRuntime/ Runtime)\n\n\n")

for i, solver in enumerate(SOLVERS):
    writer.write('\n\n###################  runtime overhead on \"'+ solver + '\"  ###################\n')
    writer.write('\naverage overhead is: ' + str(mean_overheads[i]) + "%")
    writer.write('\npopulation standard deviation overhead is: ' + str(stdev_overheads[i]) + "%")
    writer.write('\nminimum overhead is: ' + str(min_overheads[i]) + "%")
    writer.write('\nmaximum overhead is: ' + str(max_overheads[i]) + "%")
writer.close()

#
# Visualize the results
#

# Build a list for x-axis
x_axis = []
for i in range(NUM_OF_MODELS):
    x_axis.append(i)

x = np.arange(NUM_OF_MODELS)
fig = plt.figure()
ax = plt.subplot(111)

#colors = cm.rainbow(np.linspace(0, 1, len(LEGENDS)))

'''for indx, legend in enumerate(LEGENDS):
    #plt.scatter(x_axis, all_timings[indx], label=legend, color=colors[indx])
    plt.plot(x_axis, all_timings[indx], label=legend)'''

'''for indx in range(3):
    #plt.scatter(x_axis, all_timings[indx], label=legend, color=colors[indx])
    plt.plot(x_axis, all_timings[indx], label=LEGENDS[indx])  '''
'''
plt.plot(x_axis, all_timings[1], label=LEGENDS[1])
plt.plot(x_axis, all_timings[3], label=LEGENDS[3])
plt.plot(x_axis, all_timings[5], label=LEGENDS[5])
plt.plot(x_axis, all_timings[7], label=LEGENDS[7])
'''
plt.plot(x_axis, all_timings[1], label=LEGENDS[1])
plt.plot(x_axis, all_timings[2], label=LEGENDS[2])
plt.xlabel('LUS models')
plt.ylabel('Runtime (sec)')
plt.title('Computational runtime')
plt.tight_layout()

#plt.subplots_adjust(left=0.125, bottom=0.5, right=0.9, top=0.9, wspace=0.2, hspace=0.2)
box = ax.get_position()
ax.set_position([box.x0, box.y0, box.width * 0.8, box.height])
ax.legend(loc=2, prop={'size':14})
fig.savefig(os.path.join(ANALYSES_DIR, 'timing_analyses.png'))
plt.show()
plt.close(fig)


import json

DEFAULT_TRUTH_THRESHOLD = 0.5

def get_eval_values(relation, results, discretize = False, truth_threshold = DEFAULT_TRUTH_THRESHOLD):
    """
    Take in the results from inference (for a single predicate),
    and format the data for each relation into two lists of floats representing the predicted and truth value for each data point.
    These lists should be able to be directly passed into general sklean evaluation/scoring metrics.
    Return: (expected, predicted)
    """

    # Order results/truth to match.
    expected = list(sorted([list(map(str, row)) for row in relation.get_truth_data()]))
    if (len(expected) == 0):
        return [], []

    predicted = list(sorted([list(map(str, row)) for row in results]))

    if (len(expected) > len(predicted)):
        raise ValueError("Expecting (%d) more values than actually predicted (%d)." % (len(expected), len(predicted)))

    # If the sizes don't match, then that means there may be latent or missing variables.
    output_warning = False
    if (len(expected) != len(predicted)):
        new_expected = []
        new_predicted = []

        expected_map = {tuple(row[0:-1]) : float(row[-1]) for row in expected}
        predicted_map = {tuple(row[0:-1]) : float(row[-1]) for row in predicted}

        for key in expected_map:
            if (key not in predicted_map):
                if (not output_warning):
                    print("WARNING: Atom(s) for the %s relation were found in truth (len: %d) that were not in the predictions (len: %d). Example: %s(%s)." % (relation.name(), len(expected), len(predicted), relation.name(), ', '.join(map(str, key))))
                    output_warning = True

                continue

            new_expected.append(expected_map[key])
            new_predicted.append(predicted_map[key])

        expected = new_expected
        predicted = new_predicted
    else:
        # Once sorted, just get the values.
        expected = [float(row[-1]) for row in expected]
        predicted = [float(row[-1]) for row in predicted]

    if (discretize):
        expected = [int(value >= truth_threshold) for value in expected]
        predicted = [int(value >= truth_threshold) for value in predicted]

    return expected, predicted

def get_eval_categories(relation, results, label_indexes = [-1]):
    """
    Like get_eval_values(), but will get labels instead of truth values.
    Labels are identified using label_indexes (negative indexes are allowed).
    Any label that is not an index is used to identify the entity represented by the data point.
    Returns: (expected label, predicted label, entity)
    """

    label_indexes = [index if (index >= 0) else (relation.arity() + index ) for index in label_indexes]
    entity_indexes = list(sorted(set(range(relation.arity())) - set(label_indexes)))

    # {(entity): [(best label), best value], ...}
    expected_values = {}
    predicted_values = {}

    for (source, dest) in [(relation.get_truth_data(), expected_values), (results, predicted_values)]:
        for row in source:
            value = float(row[-1])
            entity = tuple([str(row[index]) for index in entity_indexes])
            label = tuple([str(row[index]) for index in label_indexes])

            if ((entity not in dest) or (value > dest[entity][1])):
                dest[entity] = [label, value]

    if (len(expected_values) != len(predicted_values)):
        print("Warning: found different sizes for truth (%d) and predictions (%d)." % (len(expected_values), len(predicted_values)))

    entities = list(sorted(set(expected_values.keys()) & set(predicted_values.keys())))

    expected = [expected_values[entity][0] for entity in entities]
    predicted = [predicted_values[entity][0] for entity in entities]

    return expected, predicted, entities

def load_json_with_comments(path):
    contents = []
    with open(path, 'r') as file:
        for line in file:
            line = line.strip()
            if (line == '' or line.startswith('#') or line.startswith('//')):
                continue

            if ('/*' in line):
                raise ValueError("Multi-line comments ('/* ... */') not allowed.")

            contents.append(line)

    return json.loads(' '.join(contents))


try:
    import asyncio
except Exception:
    pass
from multiprocessing import JoinableQueue
import logging
import sys
import threading
import time
import tornado.ioloop
import tornado.web
if (sys.version_info >= (3, 0)):
    from time import monotonic
else:
    from monotonic import monotonic

HEALTH_CHECK_SERVER = None

class TornadoRequestHandler(tornado.web.RequestHandler):
    """Request handler for when we are using tornado"""
    def get(self):
        """Handle GET requests"""
        response = None
        content_type = 'text/plain'
        response = "FAIL\n"
        status_code = 503

        # Return health if we have been marked as healthy in the last 5 minutes
        elapsed = monotonic() - HEALTH_CHECK_SERVER.last_healthy
        if elapsed < 300:
            response = "OK\n"
            status_code = 200

        if response is not None:
            self.set_status(status_code)
            self.set_header("Content-Type", content_type)
            self.set_header("Referrer-Policy", "no-referrer")
            self.set_header("Access-Control-Allow-Origin", "*")
            self.write(response)

class HealthCheckServer(object):
    """Local HTTP server for interacting with the extension"""
    def __init__(self, server_port):
        global HEALTH_CHECK_SERVER
        HEALTH_CHECK_SERVER = self
        self.thread = None
        self.messages = JoinableQueue()
        self.server_port = server_port
        self.last_healthy = monotonic()
        self.__is_started = threading.Event()

    def start(self):
        """Start running the server in a background thread"""
        self.__is_started.clear()
        self.thread = threading.Thread(target=self.run)
        self.thread.daemon = True
        self.thread.start()
        self.__is_started.wait(timeout=30)

    def stop(self):
        """Stop running the server"""
        logging.debug("Shutting down extension server")
        self.must_exit = True
        if self.thread is not None:
            ioloop = tornado.ioloop.IOLoop.instance()
            ioloop.add_callback(ioloop.stop)
            self.thread.join()
        self.thread = None
        logging.debug("Extension server stopped")

    def healthy(self):
        """Mark the last time that the agent was healthy"""
        self.last_healthy = monotonic()

    def is_ok(self):
        """Check that the server is responding and restart it if necessary"""
        import requests
        end_time = monotonic() + 30
        server_ok = False
        proxies = {"http": None, "https": None}
        while not server_ok and monotonic() < end_time:
            try:
                requests.get('http://127.0.0.1:{}/'.format(self.server_port), timeout=10, proxies=proxies)
                server_ok = True
            except Exception:
                pass
            if not server_ok:
                time.sleep(5)
        return server_ok

    def run(self):
        """Main server loop"""
        logging.debug('Starting health check server on port %d', self.server_port)
        try:
            asyncio.set_event_loop(asyncio.new_event_loop())
        except Exception:
            pass
        application = tornado.web.Application([(r"/.*", TornadoRequestHandler)])
        application.listen(self.server_port, '0.0.0.0')
        self.__is_started.set()
        tornado.ioloop.IOLoop.instance().start()


#!/usr/bin/env python3

import os
import sys
import traceback
import operator

# Add the pulsar path
thispath = os.path.dirname(os.path.realpath(__file__))
psrpath = os.path.join(os.path.dirname(thispath), "modules")
sys.path.insert(0, psrpath)

import pulsar as psr
from pulsar.output import *
from pulsar.testing import *
from pulsar.math import *

from helper.SetOperations import test_set_operations

def SetComp(set1, set2):
    if len(set1) != len(set2):
        return False
    for i in range(0, len(set1)):
        if set1[i] != set2[i]:
            return False
    return True

def SetComp2(set1, set2):
    return set1 == set2

def PrintSet(s):
    string = "{} : ".format(len(s))
    for it in s:
        string += str(it)
    print(string)

def SetElement(s, i, e):
    s[i] = e

def FillUniverse(values):
    u = StringSetUniverse()
    for v in values:
        u.insert(v)
    return u

def Run():
    try:
        Alphabet="abcdefghijklmnopqrstuvwxyz"
        BadAlphabet = "ABCDE"

        tester = Tester("Testing Universe and MathSet")
        tester.print_header()

        ################################
        # Do basic testing of set stuff
        ################################
        u0 = StringSetUniverse()
        u1 = FillUniverse(Alphabet[:7])
        u2 = FillUniverse(Alphabet)
        u3 = FillUniverse(reversed(Alphabet))
        u9 = FillUniverse(Alphabet[7:])
        test_set_operations(tester, StringSetUniverse, Alphabet, BadAlphabet,
                          False, u0, u1, u2, u3, u9)

        ###############################
        # Serialization
        ###############################
        tester.test("Universe serialization - u0", True, TestSerialization_StringSetUniverse, u0)
        tester.test("Universe serialization - u1", True, TestSerialization_StringSetUniverse, u1)
        tester.test("Universe serialization - u2", True, TestSerialization_StringSetUniverse, u2)
        tester.test("Universe serialization - u3", True, TestSerialization_StringSetUniverse, u3)
        tester.test("Universe serialization - u9", True, TestSerialization_StringSetUniverse, u9)

        ###############################
        # Universe-specific stuff
        ###############################
        for idx in range(0, len(u1)):
            tester.test_value("Element {} by idx - getitem".format(idx), u1[idx], Alphabet[idx])
            tester.test_value("Element {} by idx - At".format(idx), u1.at(idx), Alphabet[idx])

        # Try inserting bad values
        tester.test("inserting 10", False, u1.insert, 10)
        tester.test("inserting None", False, u1.insert, None)

        # Modification of an element in a universe -- shouldn't be possible
        tester.test("Modification of an element in universe", False, SetElement, u1, 0, "Z")

        tester.print_results()

    except Exception as e:
      print_global_output("Caught exception in main handler. Contact the developers\n")
      traceback.print_exc()
      print_global_error("\n")
      print_global_error(str(e))
      print_global_error("\n")

psr.initialize(sys.argv, color = True, debug = True)
Run()
psr.finalize()


# -*- coding: utf-8 -*-
# (c) 2021 Andreas Motl <andreas@hiveeyes.org>
# License: GNU Affero General Public License, Version 3
import dataclasses
import logging
from collections import OrderedDict
from typing import Dict, List, Optional
from urllib.parse import urljoin

from munch import Munch

logger = logging.getLogger(__name__)

@dataclasses.dataclass
class GrafanaDataModel:
    admin_stats: Optional[Dict] = dataclasses.field(default_factory=dict)
    dashboards: Optional[List[Munch]] = dataclasses.field(default_factory=list)
    dashboard_list: Optional[List[Munch]] = dataclasses.field(default_factory=list)
    datasources: Optional[List[Munch]] = dataclasses.field(default_factory=list)
    folders: Optional[List[Munch]] = dataclasses.field(default_factory=list)
    organizations: Optional[List[Munch]] = dataclasses.field(default_factory=list)
    users: Optional[List[Munch]] = dataclasses.field(default_factory=list)
    teams: Optional[List[Munch]] = dataclasses.field(default_factory=list)
    annotations: Optional[List[Munch]] = dataclasses.field(default_factory=list)
    snapshots: Optional[List[Munch]] = dataclasses.field(default_factory=list)
    notifications: Optional[List[Munch]] = dataclasses.field(default_factory=list)

@dataclasses.dataclass
class DashboardDetails:
    dashboard: Dict

    @property
    def panels(self) -> List:
        return self.dashboard.dashboard.get("panels", [])

    @property
    def annotations(self) -> List:
        return self.dashboard.dashboard.get("annotations", {}).get("list", [])

    @property
    def templating(self) -> List:
        return self.dashboard.dashboard.get("templating", {}).get("list", [])

@dataclasses.dataclass
class DashboardDataDetails:
    """
    Manage details concerned about "data"-relevant information,
    a subset of the complete bunch of dashboard information.
    """

    panels: List[Dict]
    annotations: List[Dict]
    templating: List[Dict]

    def to_munch(self):
        return Munch(panels=self.panels, annotations=self.annotations, templating=self.templating)

    @classmethod
    def from_dashboard_details(cls, dbdetails: DashboardDetails):
        ds_panels = cls.collect_data_nodes(dbdetails.panels)
        ds_annotations = cls.collect_data_nodes(dbdetails.annotations)
        ds_templating = cls.collect_data_nodes(dbdetails.templating)

        # Inline panel information into data details.
        targets = []
        for panel in ds_panels:
            panel_item = cls._format_panel_compact(panel)
            if "targets" in panel:
                for target in panel.targets:
                    target["_panel"] = panel_item
                    targets.append(target)

        return cls(panels=targets, annotations=ds_annotations, templating=ds_templating)

    @staticmethod
    def collect_data_nodes(element):
        """
        Select all element nodes which have a "datasource" attribute.
        """
        element = element or []
        items = []

        def add(item):
            if item is not None and item not in items:
                items.append(item)

        for node in element:
            if "datasource" in node and node["datasource"]:
                add(node)

        return items

    @staticmethod
    def _format_panel_compact(panel):
        """
        Return a compact representation of panel information.
        """
        attributes = ["id", "title", "type", "datasource"]
        data = OrderedDict()
        for attribute in attributes:
            data[attribute] = panel.get(attribute)
        return data

    @staticmethod
    def _format_data_node_compact(item: Dict) -> Dict:
        """
        Return a compact representation of an element concerned about data.
        """
        data = OrderedDict()
        data["datasource"] = item.get("datasource")
        data["type"] = item.get("type")
        for key, value in item.items():
            if "query" in key.lower():
                data[key] = value
        return data

    def queries_only(self):
        """
        Return a representation of data details information, only where query expressions are present.
        """
        # All attributes containing query-likes.
        attributes_query_likes = ["expr", "jql", "query", "rawSql", "target"]

        attributes = [
            # Carry over datasource and panel references for informational purposes.
            "datasource",
            "_panel",
        ] + attributes_query_likes

        def transform(section):
            new_items = []
            for item in section:
                new_item = OrderedDict()
                for key, value in item.items():
                    if key in attributes:
                        new_item[key] = value
                if any(attr in attributes_query_likes for attr in new_item.keys()):
                    # Filter annotations without any value.
                    if "target" in new_item and isinstance(new_item["target"], dict):
                        if new_item["target"].get("type") == "dashboard":
                            continue
                    # Unnest items with nested "query" slot.
                    for slot in ["query", "target"]:
                        if slot in new_item and isinstance(new_item[slot], dict) and "query" in new_item[slot]:
                            new_item["query"] = new_item[slot]["query"]
                    new_items.append(new_item)
            return new_items

        return DashboardDataDetails(
            panels=transform(self.panels),
            annotations=transform(self.annotations),
            templating=transform(self.templating),
        )

@dataclasses.dataclass
class DatasourceItem:
    uid: Optional[str] = None
    name: Optional[str] = None
    type: Optional[str] = None
    url: Optional[str] = None

    @classmethod
    def from_payload(cls, payload: any):
        if isinstance(payload, Munch):
            payload = dict(payload)
        if isinstance(payload, dict):
            return cls(**payload)
        if isinstance(payload, str):
            return cls(name=payload)
        raise TypeError(f"Unknown payload type for DatasourceItem: {type(payload)}")

@dataclasses.dataclass
class DatasourceExplorationItem:
    datasource: Munch
    used_in: List[Munch]
    grafana_url: str

    def format_compact(self):
        dsshort = OrderedDict(
            uid=self.datasource.get("uid"),
            name=self.datasource.name,
            type=self.datasource.type,
            url=self.datasource.url,
        )
        item = OrderedDict(datasource=dsshort)
        for dashboard in self.used_in:
            item.setdefault("dashboards", [])
            dbshort = OrderedDict(
                title=dashboard.dashboard.title,
                uid=dashboard.dashboard.uid,
                path=dashboard.meta.url,
                url=urljoin(self.grafana_url, dashboard.meta.url),
            )
            item["dashboards"].append(dbshort)
        return item

@dataclasses.dataclass
class DashboardExplorationItem:
    dashboard: Munch
    datasources: List[Munch]
    grafana_url: str

    def format(self, with_data_details: bool = False, queries_only: bool = False):
        """
        Generate a representation from selected information.

        - dashboard
        - datasources
        - details
            - panels/targets
            - annotations
            - templating
        """
        dbshort = OrderedDict(
            title=self.dashboard.dashboard.title,
            uid=self.dashboard.dashboard.uid,
            path=self.dashboard.meta.url,
            url=urljoin(self.grafana_url, self.dashboard.meta.url),
        )

        dsshort = []
        for datasource in self.datasources:
            item = OrderedDict(
                uid=datasource.get("uid"),
                name=datasource.name,
                type=datasource.type,
                url=datasource.url,
            )
            dsshort.append(item)

        data = Munch(dashboard=dbshort, datasources=dsshort)
        if with_data_details:
            details = self.collect_data_details(queries_only=queries_only)
            if not details.panels and not details.annotations and not details.templating:
                return None
            data.details = details.to_munch()
        return data

    def collect_data_details(self, queries_only: bool = False):
        """
        Collect details concerned about data from dashboard information.
        """

        dbdetails = DashboardDetails(dashboard=self.dashboard)
        dbdatadetails = DashboardDataDetails.from_dashboard_details(dbdetails)
        if queries_only:
            dbdatadetails = dbdatadetails.queries_only()
        return dbdatadetails


#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2018/8/24 上午11:26
# @Author  : jlinka
# @File    : search.py

from pymongo import MongoClient
from bson.objectid import ObjectId
from app.main.complex_search import kmeans_search_condition
import jieba

# web = ['C114中国通信网', '中华人民共和国工业和信息化部', '中华人民共和国发展和改革委员会', '中华人民共和国科学技术部',
#        '中华人民共和国商务部', '深圳市发展和改革委员会', 'ITU', 'ITU中国', '3GPP', 'IMT-2020推进组', '通信世界网',
#        'OFweek光通讯网', '中国无线电管理', '电子发烧友', '电子产品世界', '新华网信息化', '新浪新闻', '新浪科技',
#        '新浪财经', '网易科技', '网易财经', '搜狐', '搜狐科技', '腾讯财经', '腾讯科技', '凤凰网', '凤凰科技', '21CN科技',
#        '环球科技', '飞象网', '中国科技网', '智通财经', 'CSDN', '和讯网', '华为', '中兴通讯', '上海诺基亚贝尔', '爱立信',
#        '大唐电信', '三星', '英特尔', '英特尔IQ', '高通Qualcomm', '中国移动设计院', '中国移动设计院', '中国联通研究院',
#        '中国电信北京研究院', '中国信通院']

class mongDbManger_web_showClass:
    __client = None
    __db = None
    __coll = None

    def __init__(self):
        print("__init__mongDbManger_webshow")

    def connect(self, url, port, user, password, database, table, auth):
        self.__client = MongoClient(url, port)
        self.__db = self.__client[auth]
        self.__db.authenticate(user, password)
        self.__db = self.__client[database]
        self.__coll = self.__db[table]

    def closeMongo(self):
        self.__client.close()

    def findOneWeek(self, dt):
        qureyCondition = {'createTime': {'$gt': dt}}
        cursor = self.__coll.find(qureyCondition)
        return cursor

    def find(self):
        cursor = self.__coll.find({"test": {'$exists': True}, 'isLimitSource': 1})
        return cursor

    def findSource(self):
        # cursor = self.__coll.find({"test": {'$exists': True}, 'isLimitSource': 1}, {"source": 1, "_id": 0})
        cursor = self.__coll.distinct('source', {"test": {'$exists': True}, 'isLimitSource': 1})
        return cursor

    def findAuther(self):
        # cursor = self.__coll.find({"test": {'$exists': True}, 'isLimitSource': 1}, {"auther": 1, "_id": 0})
        cursor = self.__coll.distinct('auther', {"test": {'$exists': True}, 'isLimitSource': 1, 'source': "微信公众号"})
        return cursor

    def findlike(self, filter, source, webs, auther):
        test_dict = {}
        source_list = []
        test_dict["test"] = {'$exists': True}
        test_dict['isLimitSource'] = 1
        if filter['content']['$regex'] != '':
            test_dict[list(filter.keys())[0]] = list(filter.values())[0]
        if source:
            for i in source:
                source_list.append(i)
                # test_dict['source'] = i
                if i == '网站':
                    source_list.extend(webs)
            test_dict['source'] = {'$in': source_list}
        else:
            if webs and auther:
                test_dict['$or'] = [{'source': {'$in': webs}},
                                    {'auther': {'$in': auther}}]
            if len(webs) != 0 and len(auther) == 0:
                test_dict['source'] = {'$in': webs}
            if len(webs) == 0 and len(auther) != 0:
                test_dict['auther'] = {'$in': auther}
            # test_dict['source'] = {'$in': webs}
            # test_dict['auther'] = {'$in': auther}

        cursor = self.__coll.find(test_dict)
        cursor.close()
        return cursor

    def findOne(self, filter):
        cursor = self.__coll.find_one({'_id': ObjectId(filter)})
        return cursor

    def test(self, test_dict):
        cursor = db.__coll.find(test_dict)
        for i in cursor:
            print(i['source'])
            print(i['auther'])

class PaginateLeave:
    def __init__(self, page, leave):
        self.total = leave.count()
        self.pages = int(self.total / 10)
        if self.total % 10 != 0:
            self.pages += 1
        if page == -1:
            self.page = self.pages
        else:
            self.page = page
        if self.page == 1:
            self.has_prev = False
        else:
            self.has_prev = True
        if self.page == self.pages:
            self.has_next = False
        else:
            self.has_next = True
        self.next_num = self.page + 1
        self.per_page = 10
        self.prev_num = self.page - 1
        self.current_num = self.total - (10 * (self.page - 1))
        if self.current_num > 10:
            self.current_num = 10
        self.items = []
        for i in range(self.current_num):
            yes_time = str(leave[self.prev_num * 10 + i].get('createTime'))
            yes_time = "".join(str(yes_time[0:4]) + "年" + str(yes_time[4:6]) + "月" + str(yes_time[6:8])) + "日"
            self.items.append(
                {'ObjectId': leave[self.prev_num * 10 + i].get('_id'),
                 'title': leave[self.prev_num * 10 + i].get('title'),
                 # 'createTime': leave[self.prev_num * 10 + i].get('createTime'),
                 'createTime': yes_time,
                 'content': leave[self.prev_num * 10 + i].get('content'),
                 'url': leave[self.prev_num * 10 + i].get('url'),
                 'source': leave[self.prev_num * 10 + i].get('source'),
                 'auther': leave[self.prev_num * 10 + i].get('auther')})

    def iter_pages(self, left_edge=2, left_current=2, right_current=5, right_edge=2):
        last = 0
        for num in range(1, self.pages + 1):
            if num <= left_edge or \
                    (self.page - left_current - 1 < num < self.page + right_current) \
                    or num > self.pages - right_edge:
                if last + 1 != num:
                    yield None
                yield num
                last = num

db = mongDbManger_web_showClass()
db.connect('server21.raisound.com', 24000, "webuser", "webuser1957", "web_show", "xinChuang_topic", "web_show")

def searchAll():
    cursor = db.find().sort('createTime', -1)
    # 关闭游标
    # cursor.close()
    # db.closeMongo()
    return cursor

def searchCondition(keyword, source, web, auther, order):
    filter = {}
    keyword = keyword
    condition = {}
    condition['$regex'] = keyword
    filter["content"] = condition
    cursor = db.findlike(filter, source, web, auther).sort('createTime', -1)
    if order == "智能排序":
        pass
    print(cursor.count())
    cursor.close()
    db.closeMongo()
    return cursor

def searchWeb():
    cursor = db.findSource()
    cursor.remove("微信公众号")
    db.closeMongo()
    return cursor

def searchOfficialAccounts():
    cursor = db.findAuther()
    db.closeMongo()
    return cursor

def findObjectId(keywords):
    cursor = db.findOne(keywords)
    db.closeMongo()
    return cursor

if __name__ == '__main__':
    # a = searchCondition("3GPP", "", "", "", "")
    a = searchAll()
    kmeans_search_condition(a)

    # a = db.findSource()
    # for i in a:
    #     print(i)


from numpy import *
from matplotlib import pyplot as plt

def ln(x):
    return log(x)

def regla_falsa(f, a, b, tol, imax=50):
    print('Metodo de falsa regla: ')
    print ('************************************************************************')
    print ('* i *   a    *   b    *   f(a)  *   f(b) *   ri   *  f(ri)  *   error  *')

    i = 0
    r = a
    r1 = b
    error = 1
    fa = f(a)
    fb = f(b)
    fr = f(r)

    while ((i < imax) and (error > tol)):

        r1 = r
        r = (b - f(b) * (a-b)/(f(a)-f(b)))
        error = abs ((r1-r)/r)

        print ('*{:0>2d}'.format(i),'* {:.4f}'.format(a), '* {:.4f}'.format(b), '* {:.4f}'.format(fa), '* {:.4f}'.format(fb), '* {:.4f}'.format(r), '* {:.4f}'.format(fr), '*  {:.4f}'.format(error),' *')
        if (f(a)*f(r) < 0):
            b = r
        else:
            a = r
        i += 1
        error = abs ((r1-r)/r)
    print ('************************************************************************')
    print ("raiz: {:.6f}".format(r),"es una buena aproximacion, con un error de: ",error)
    print(' ')
    return r

f = input('Ingrese la funcion a evaluar: ')
fx = lambda x: eval(f)
r = 0
a = float(input('Ingrese el limite inferior: '))
b = float(input('Ingrese el limite superior: '))
tol = float(input('Ingrese el error relativo: '))
xgraph = linspace(a, b, 100)
plt.plot(xgraph, fx(xgraph))
plt.grid()
print(' ')

if (fx(a) * fx(b) > 0):
    print ("El programa no puede identificar si existe un corte al ambos puntos ser del mismo signo")
else:
    r = regla_falsa(fx, a, b, tol)
    plt.plot(r,fx(r),'or')
    plt.show()

input()


#  Created by btrif Trif on 21-02-2023 , 6:02 PM.
from fastapi import APIRouter, Depends, HTTPException
from sqlalchemy.orm import Session

from src.mazes.crud import delete_user, get_user_by_username
from src.mazes.database import get_db
from src.mazes.schemas import UserBaseSchema, UserSchema
from src.mazes.utils import get_current_user

admin_router = APIRouter(
        prefix="",
        tags=[ "admin" ],
        # dependencies=[ Depends(TokenSchema) ],
        responses={
            404 : {"description" : "Not found"},
            },
        )

@admin_router.post("/admin")
async def update_admin() :
    return {"message" : "Admin getting schwifty"}

@admin_router.post("/delete_user", response_model=dict)
async def delete_username(
        user: UserBaseSchema,
        current_user: UserBaseSchema = Depends(get_current_user),
        db: Session = Depends(get_db),
        ) :
    loggedin_user = current_user.username
    print(f"loggedin_user : {loggedin_user}   {type(loggedin_user)}   ")

    if loggedin_user in {'admin'} :
        db_user = get_user_by_username(
                db,
                user_name=user.username
                )
        if db_user :
            print(f"delete_user_by_username db_user :  {db_user}")
            deleted_user = delete_user(
                    db=db,
                    user_name=user.username
                    )
            print(f"deleted_user : {deleted_user}")
        else :
            raise HTTPException(status_code=400, detail="This username does not exist")

        return {"status" : "ok", "message" : f"The user {user.username} was successfully deleted"}

    else :
        raise HTTPException(status_code=400, detail="Only administrators can delete accounts")


from simulation_settings import *
import numpy as np
import actions
import pickle
import datetime
from keras.models import Sequential
from keras.layers import Dense
import keras.models
from keras.callbacks import CSVLogger
import random
import pickle

state_mode = 'simple_state'

def custom_metric(state_action_a, state_action_b):
    dist_list = list()
    weights = ((1/5000), (1/5000), (1/180), (1/5), (1/5), 1.0, 1.0)
    for vars in zip(state_action_a, state_action_b, weights):
        var_dist = np.float(abs((vars[0] - vars[1])*vars[2]))
        dist_list.append(var_dist)
    dist = np.average(dist_list)
    #TODO Use fixed divisors for ref distance..avoid zero
    return dist

def get_nn(obj):
    if obj:
        model = keras.models.load_model(obj)
    else:
        # create model
        model = Sequential()
        if state_mode == 'simple_state':
            model.add(Dense(20, input_shape=(5,), activation='relu'))
        else:
            model.add(Dense(20, input_shape=(7,), activation='relu'))
        model.add(Dense(20, activation='relu'))
        model.add(Dense(20, activation='relu'))
        model.add(Dense(1, activation='linear'))
        # Compile model
        # sgd = optimizers.SGD(lr=1, decay=0, momentum=0.9, nesterov=True)
        # rmsprop = optimizers.RMSprop(lr=0.001)
        model.compile(loss='mean_squared_error', optimizer='adam')
        # plot_model(model, to_file='model.png')
    return model

class Learner(object):
    exploring = None

    def __init__(self, file_to_save='agents/agent_'+datetime.datetime.now().strftime('%Y%m%d%H%M%S'),
                 load_saved_regression=False,
                 r_m_=None, nn_=False):
        self.rw_mp = r_m_
        self.debug = True
        self.current_step = 0
        self.batch_list = list()
        self.nn_flag = nn_
        if self.nn_flag:
            self.learner = get_nn(load_saved_regression)
        else:
            pass
        self.end_states = dict()
        self.discount_factor = 0.8
        self.mode = self.mode = 'angle_only'
        self.states = list()
        self.act = list()
        self.rewards = list()
        self.states_p = list()
        self.q_target = list()
        r_mode = '__'
        self.file = None
        self.samples = None
        if self.rw_mp:
            r_mode = self.rw_mp.reward_mode
        self.file = file_to_save + self.learner.__class__.__name__ + '_r_' + r_mode+'_disc_'+str(self.discount_factor)
        self.logger = CSVLogger(self.file + 'log', separator=';', append=True)

    def add_tuples(self, tuples_list):
        self.batch_list = self.batch_list + tuples_list

    def set_up_agent(self):
        print("Batch size: ", len(self.batch_list))
        self.states = [list(k[0]) for k in self.batch_list]
        if self.mode == 'angle_only':
            self.act = [(x[1][0]) for x in self.batch_list]
        else:
            self.act = [(x[1]) for x in self.batch_list]
        self.rewards = np.asarray([x[3] for x in self.batch_list], dtype=np.float64)
        self.states_p = [list(k[2]) for k in self.batch_list]
        self.end_states = [(x[4]) for x in self.batch_list]
        self.q_target = self.rewards
        self.states = np.array(self.states)
        self.act = np.array(self.act)
        self.samples = np.column_stack((self.states, self.act))
        print("Current batch size: ", len(self.samples))

    def save_batch(self):
        with open(self.file + '_batch',
                  'wb') as outfile:
            pickle.dump(self.batch_list, outfile)

    def fqi_step(self, max_iterations, debug=False):
        stop_flag = False
        for it in range(max_iterations):
            self.current_step += 1
            print("FQI_iteration: ", it)
            self.learner.fit(self.samples, self.q_target, batch_size=300, verbose=1, epochs=1000,
                             callbacks=[self.logger])
            if not self.nn_flag:
                sc = self.learner.score(self.samples, self.q_target)
                print("Score: ",sc)
            if self.discount_factor > 0:
                maxq_prediction = np.asarray([self.find_max_q(state_p,i)[0] for i,state_p in enumerate(self.states_p)])
                self.q_target = self.rewards + self.discount_factor*maxq_prediction
            else:
                self.q_target = self.rewards
            if self.nn_flag:
                self.learner.save(self.file+'it'+str(self.current_step)+'.h5')
            else:
                with open(self.file+'it'+str(it), 'wb') as outfile:
                    pickle.dump(self.learner, outfile)
            if stop_flag:
                break

    def find_max_q(self, state_p, i):
        qmax = -float('Inf')
        choice_list = list()
        if not i in self.end_states or self.end_states[i] == 0:
            for action in action_space.action_combinations:
                if self.mode == 'angle_only':
                    state_action = np.append(state_p, action[0])
                else:
                    state_action = np.append(state_p, action)
                state_action = np.reshape(state_action, (1, -1))
                qpred = self.learner.predict(state_action, batch_size=1, verbose=1)[0][0]
                print('Q value and respective action: ', qpred, action)
                if qpred > qmax:
                    qmax = qpred
                    choice_list = [action]
                elif qpred == qmax:
                    choice_list.append(action)
        else:
            print('final')
            qmax = 0
            choice_list = action_space.action_combinations
        return qmax, choice_list

    def select_action(self, state):
        qmax, choice_list = self.find_max_q(state,-1)
        selected_action = random.choice(choice_list)
        print("Max",qmax)
        print(selected_action[0])
        print(selected_action[1])
        return selected_action

if __name__ == '__main__':
    with open('agent20180408200244', 'rb') as infile:
        agent_obj = pickle.load(infile)
        agent = Learner(load_saved_regression=agent_obj, action_space_name='only_rudder_action_space')
        action = agent.select_action((7977.5731952, 4594.6156251, -103.49968, -2.909885, -0.6988991, 0.0005474))
        print(action)


from .models import AVGRegisterline, ExternalReference
from rest_framework import serializers

class AVGRegisterlineSerializer(serializers.ModelSerializer):
    class Meta:
        model = AVGRegisterline
        fields = '__all__'
        exclude = [ ]
        read_only_fields = []

class EmbeddedAVGRegisterlineSerializer(AVGRegisterlineSerializer):
    id = serializers.IntegerField(label='ID', required=False)

class ExternalReferenceSerializer(serializers.ModelSerializer):
    avgregisterline = EmbeddedAVGRegisterlineSerializer()
    class Meta:
        model = ExternalReference
        fields = '__all__'


# coding: utf-8

# In[258]:

#IMPORT REQUIRED LIBRARIES
from Bio import SeqIO
from Bio.SeqRecord import SeqRecord
from Bio import pairwise2
from Bio.pairwise2 import format_alignment
import random
import copy
from copy import deepcopy
import numpy as np
import pandas as pd

# In[259]:

#READ RECORDS FROM AMINO.FASTA
records = list(SeqIO.parse("amino.fasta", "fasta"))

# In[260]:

#READ DISTANCES from CSV
data = pd.read_csv('distances.csv')
#Add all data in a list of lists
dist = []
for i in range(0, len(data)):
    dist.append([data.values[i,j] for j in range(0, len(data.values[0]))])

# In[261]:

#INITIALISE LIST OF CLUSTER WITH A SINGLE CLUSTERING CONTAINING ALL POINTS
cluster=[]
a=set()
for i in range(len(records)):
    a.add(i)
cluster.append(a)

# In[262]:

#TO STORE POINTS FOR DENDOGRAM
xlist=[]

# In[263]:

#CREATING COPY OF CLUSTER
newclust=(copy.deepcopy(cluster))

# In[264]:

#FUNCTION TO BREAK EACH CLUSTER INTO TWO
x=[]
y=[]

pointx=[i for i in range(0,len(dist))]
pointy=[0 for i in range(0,len(dist))]
def breakclust(clust):
    if(len(clust)==1):
        return
    child1=set()
    child2=set()
    #IDENTIFY MOST DISSIMILAR POINTS
    max=-99999
    for i in clust:
        for j in clust:
            if(i!=j):
                if(dist[i][j]!=0):
                    if(dist[i][j]>max):
                        if(i!=j):
                            max=dist[i][j]
                            maxi=i
                            maxj=j
    #FORM NEW CLUSTERS WITH MEDIODS AS THE ABOVE POINTS
    child1.add(maxi)
    child2.add(maxj)
    clust.remove(maxi)
    clust.remove(maxj)
    if(len(clust)!=0):
        for i in clust:
            min1=1
            for j in child1:
                if(dist[i][j]<min1):
                    min1=dist[i][j]
            min2=1
            for j in child2:
                if(dist[i][j]<min2):
                    min2=dist[i][j]
            if(min1<min2):
                child1.add(i)
            else:
                child2.add(i)

    cluster.append(child1)
    cluster.append(child2)
    #BREAK CLUSTER INTO TWO BY CALLING BREAKCLUST FOR EACH CHILD RECURSIVELY
    breakclust(copy.deepcopy(child1))
    breakclust(copy.deepcopy(child2))

    #STORING COORDINATES FOR DENDOGRAMS
    midpoint=(pointx[maxi]+pointx[maxj])/2
    x.append(pointx[maxi])
    x.append(pointx[maxi])
    x.append(pointx[maxj])
    x.append(pointx[maxj])

    y.append(pointy[maxi])
    y.append(1/abs(max))
    y.append(1/abs(max))
    y.append(pointy[maxj])

    pointx[maxi]=midpoint
    pointx[maxj]=midpoint
    pointy[maxi]=1/abs(max)
    pointy[maxj]=1/abs(max)

# In[265]:

#CALL FUNCTION FOR FIRST CLUSTER
breakclust(copy.deepcopy(cluster[0]))

# In[266]:

#PLOTTING THE DENDOGRAM
import matplotlib.pyplot as plt
from matplotlib.pyplot import figure
figure(num=None, figsize=(17, 17), dpi=300)
count=350
loop=int(len(x)/4)
x[len(x)-1]=(x[len(x)-7]+x[len(x)-6])/2
x[len(x)-2]=(x[len(x)-7]+x[len(x)-6])/2
y[len(y)-1]=(y[len(y)-6])
for i in range(loop):
    xlist=[]
    ylist=[]
    for j in range(0,4):
        xlist.append(x[j+i*4])
        ylist.append(y[j+i*4])
    plt.plot(xlist, ylist)


from bs4 import BeautifulSoup
from urllib.request import urlopen
from os import system

if __name__ == "__main__":
    f = open("/home/palpatine/result","w")
    html = urlopen("http://vstup.info/2018/i2018i41.html")
    wordToLookFor = input("textToFind:").lower()
    bs = BeautifulSoup(html)
    print(bs.prettify())
    f.write("")
    f.write("\n<meta charset=\"UTF-8\">\n")
    blocklist = bs.find("table", {"id": "denna1"}).find("tbody").findAll("tr")
    print(len(blocklist))
    for block in blocklist:
        if block.get_text().lower().find(wordToLookFor) != -1:
            f.write(str(block).replace("href=\".", "href=\"http://vstup.info/2018/") + "\n")
            f.write("
")
    f.write("")
    f.write("")
    f.close()
    system("firefox ~/result")


import numpy as np
import cv2
import time
cap=cv2.VideoCapture(0)#Read from the default web cam i.e why 0

time.sleep(3) #for the system to sleep for 3 second before the webcam starts
for i in range(30):
    retval,back=cap.read()
back=np.flip(back,axis=1) #images captured is mirror image so we need to flip it again to get the original background amd axis=1 means laterally invert image
cap=cv2.VideoCapture(0)

## detecting the red portion In each frame

while (cap.isOpened()):  ##Read every Frame from the webcam, until the camera is open
    ret,img=cap.read()
    if ret:
        img=np.flip(img,axis=1)

        ##convert the color space from BGR to HSV because BGR  cant detect colour efficiently
        hsv=cv2.cvtColor(img,cv2.COLOR_BGR2HSV)

        ##Generat masks to detect red color
        lower_red = np.array([0,120,70])
        upper_red = np.array([10,255,255])
        mask1 = cv2.inRange(hsv,lower_red,upper_red)

        lower_red = np.array([170,120,70])
        upper_red = np.array([180,255,255])
        mask2 = cv2.inRange(hsv,lower_red,upper_red)
        mask1+=mask2

        ###Replacing the red portion with a mask image in each frame

        mask = cv2.morphologyEx(mask1, cv2.MORPH_OPEN, np.ones((5,5),np.uint8))
        img[np.where(mask==255)]=back[np.where(mask==255)]

         #Final output
        cv2.imshow("Harry Potter's invisible secret revealed",img)
    key = cv2.waitKey(1)
    if key==ord("q"):
        break
cap.release()
cv2.destroyAllWindows()


import os, warnings
import matplotlib.pyplot as plt
from matplotlib import gridspec

import numpy as np
import tensorflow as tf
from tensorflow.keras.preprocessing import image_dataset_from_directory
from tensorflow.keras.callbacks import EarlyStopping
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow import keras
from tensorflow.keras import layers

import pandas as pd

# Reproducability
def set_seed(seed=31415):
    np.random.seed(seed)
    tf.random.set_seed(seed)
    os.environ['PYTHONHASHSEED'] = str(seed)
    os.environ['TF_DETERMINISTIC_OPS'] = '1'
set_seed(31415)

# Set Matplotlib defaults
plt.rc('figure', autolayout=True)
plt.rc('axes', labelweight='bold', labelsize='large',
       titleweight='bold', titlesize=18, titlepad=10)
plt.rc('image', cmap='magma')
warnings.filterwarnings("ignore") # to clean up output cells

#Importing data using DataGenerator and flow

datagen = ImageDataGenerator(
    horizontal_flip=True,
    vertical_flip=True,
    validation_split=0.3,
    dtype=tf.float32,
)

# Load training and validation sets
ds_train = datagen.flow_from_directory(
    '../input/brainmriv2/brain_tumor_dataset',
    target_size=[128, 128],
    batch_size=64,
    color_mode = 'grayscale',
    shuffle=True,
    subset = 'training',
)
ds_valid = datagen.flow_from_directory(
    '../input/brainmriv2/brain_tumor_dataset',
    target_size=[128, 128],
    batch_size=25,
    color_mode = 'grayscale',
    shuffle=True,
    subset = 'validation',
)

#Model
model = keras.Sequential([
    # Block One
    layers.Conv2D(filters=32, kernel_size=3, activation='relu', padding='same',
                  input_shape=[128, 128, 1]),
    layers.MaxPool2D(),

    # Block Two
    layers.Conv2D(filters=64, kernel_size=3, activation='relu', padding='same'),
    layers.MaxPool2D(),

    # Block Three
    layers.Conv2D(filters=128, kernel_size=3, activation='relu', padding='same'),
    layers.MaxPool2D(),

    # Head
    layers.Flatten(),
    layers.Dense(2048, activation='relu'),
    layers.Dropout(0.5),
    layers.Dense(2, activation='softmax'),
])

#Training the Model
early_stopping = EarlyStopping(
    min_delta=0.001,
    patience=25,
    restore_best_weights=True,
)
model.compile(
    optimizer='adam',
    loss='binary_crossentropy',
    metrics=['binary_accuracy'],
)

history = model.fit(
    ds_train,
    validation_data=ds_valid,
    callbacks=[early_stopping],
    epochs=100,
    verbose=0,
)

#Seeing the results

history_frame = pd.DataFrame(history.history)
history_frame.loc[:, ['loss', 'val_loss']].plot()
history_frame.loc[:, ['binary_accuracy', 'val_binary_accuracy']].plot();
print(history_frame.loc[:, ['binary_accuracy', 'val_binary_accuracy', 'loss', 'val_loss']])


from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from voicerecog_module import *
from selenium.webdriver.common.by import By
from difflib import SequenceMatcher
import time
from selenium.webdriver.chrome.service import Service

options = webdriver.ChromeOptions()
options.add_argument("--start-maximized")
options.add_argument('--log-level=3')
path=Service("C:\Program Files\BraveSoftware\Brave-Browser\Application\112.1.50.119")
options.binary_location = "C:\\Program Files\\BraveSoftware\\Brave-Browser\\Application\\brave.exe"
# Load the YouTube homepage
f = open("C:\\Users\\utkar\\OneDrive\\vscode\\automation_yt oops project\\test_topics.txt", "r")
topics=f.read()
sep=(input("enter the separator :-"))[0]
search_list=list(topics.split(sep))
start_index=input("input start topic no. or say start from begin :- ")
if start_index.isdigit()==True:
    start_index=int(start_index)
else:
    start_index=1
i=start_index-1
driver = webdriver.Chrome(service=path,options=options)
for search_term in search_list[i:]:
    print(start_index,"out of",len(search_list))
    start_index+=1
    driver.get("https://www.youtube.com")
    search_box = driver.find_element(By.NAME,"search_query")
    search_box.send_keys(search_term)

    search_box.send_keys(Keys.RETURN)
    search_box.clear()
    videos = driver.find_elements(By.XPATH,"//div[@id='contents']//ytd-video-renderer")
    title_similarity_scores = {}
    top_video_element = None
    for video in videos:
                title_element = video.find_element(By.XPATH,".//a[@id='video-title']")
                title = title_element.get_attribute("title")
                similarity_score = SequenceMatcher(None, search_term.lower(), title.lower()).ratio()
                title_similarity_scores[title] = similarity_score

                if len(title_similarity_scores) > 0:
                    sorted_titles = sorted(title_similarity_scores, key=title_similarity_scores.get, reverse=True)
                    top_video_title = sorted_titles[0]
                    top_video_element = None
                    for video in videos:
                        title_element = video.find_element(By.XPATH,".//a[@id='video-title']")
                        title = title_element.get_attribute("title")
                        if title == top_video_title:
                            top_video_element = video
                            break

                    top_video_element.click()
                    time.sleep(2)
                    top_video_element.send_keys("f")
                    #driver.find_element(By.XPATH,"//*[@id='movie_player']/div[33]/div[2]/div[2]/button[9]").click()
                    break
    print("listening")
# Listen for voice commands
    while True:
        try:
            command=voice()
    # Respond to voice commands
            if "search for" in command:
                search_term = command.replace("search for ", "")
                search_box = driver.find_element(By.NAME,"search_query")
                search_box.send_keys(search_term)
                search_box.send_keys(Keys.RETURN)
                search_box.clear()
                videos = driver.find_elements(By.XPATH,"//div[@id='contents']//ytd-video-renderer")
                title_similarity_scores = {}
                top_video_element = None
                for video in videos:
                    title_element = video.find_element(By.XPATH,".//a[@id='video-title']")
                    title = title_element.get_attribute("title")
                    similarity_score = SequenceMatcher(None, search_term.lower(), title.lower()).ratio()
                    title_similarity_scores[title] = similarity_score

                    if len(title_similarity_scores) > 0:
                        sorted_titles = sorted(title_similarity_scores, key=title_similarity_scores.get, reverse=True)
                        top_video_title = sorted_titles[0]
                        top_video_element = None
                        for video in videos:
                            title_element = video.find_element(By.XPATH,".//a[@id='video-title']")
                            title = title_element.get_attribute("title")
                            if title == top_video_title:
                                top_video_element = video
                                break
                        if top_video_element:
                            top_video_element.click()
                            break
                        else:
                            Speech("Failed to select video. Please try again.")
                            break
                    else:
                        Speech("No videos found. Please try again with a different search term.")

                    try:
                        top_video_element.click()
                    except TypeError:
                        Speech("retry ")

            if "pause" in command:
                pause_button = driver.find_element(By.CSS_SELECTOR,"#movie_player > div.ytp-chrome-bottom > div.ytp-chrome-controls > div.ytp-left-controls > button")
                pause_button.click()
            elif "play" in command:
                if "next video" in command:
                    seek_forward_button = driver.find_element(By.CSS_SELECTOR,"#movie_player > div.ytp-chrome-bottom > div.ytp-chrome-controls > div.ytp-left-controls > a.ytp-next-button.ytp-button")
                    seek_forward_button.click()
                else:
                    play_button = driver.find_element(By.CSS_SELECTOR,"#movie_player > div.ytp-chrome-bottom > div.ytp-chrome-controls > div.ytp-left-controls > button")
                    play_button.click()
            elif "next video" in command:

                #driver.find_element(By.CSS_SELECTOR,"#movie_player > div.ytp-chrome-bottom > div.ytp-chrome-controls > div.ytp-left-controls > a.ytp-next-button.ytp-button").click()
                break
            elif "mute" in command:
                driver.find_element(By.CSS_SELECTOR,"#movie_player > div.ytp-chrome-bottom > div.ytp-chrome-controls > div.ytp-left-controls > span > button").click()
            elif "exit" in command or 'later' in command :
                exit()
        except TypeError:
            pass
# Quit the browser and release the microphone resources
driver.quit()
f.close()


from flask import request
from flask_restplus import Resource, reqparse

from ..util.dto import HighlightDto
from ..service.highlight_service import save_new_highlight, get_all_highlights, get_a_highlight, update_highlight, delete_a_highlight

api = HighlightDto.api
_highlight = HighlightDto.highlight

@api.route('/')
class NoteList(Resource):
    @api.doc('list_of_highlights for a highlight')
    @api.param('company_id', 'company that the highlights are associated with')
    @api.marshal_list_with(_highlight, envelope='data')
    def get(self):
        """List all highlights"""
        parser = reqparse.RequestParser()
        parser.add_argument("company_id", type=int)
        args = parser.parse_args()
        return get_all_highlights(args["company_id"])

    @api.response(201, 'highlight successfully created.')
    @api.doc('create a new highlight')
    @api.expect(_highlight, validate=True)
    def post(self):
        """Creates a new highlight """
        data = request.json
        return save_new_highlight(data=data)

@api.route('/<highlight_id>')
@api.param('highlight_id', 'The highlight identifier')
@api.response(404, 'Note not found.')
class Note(Resource):
    @api.doc('get a highlight')
    @api.marshal_with(_highlight)
    def get(self, highlight_id):
        """get a highlight given its identifier"""
        highlight = get_a_highlight(highlight_id)
        if not highlight:
            api.abort(404)
        else:
            return highlight

    @api.response(201, 'highlight successfully created.')
    @api.doc('update a highlight')
    @api.expect(_highlight, validate=True)
    def put(self, highlight_id):
        """Update a highlight """
        data = request.json
        return update_highlight(highlight_id, data)

    @api.response(201, 'highlight successfully deleted.')
    @api.doc('delete a highlight')
    def delete(self, highlight_id):
        """Delete a highlight """
        return delete_a_highlight(highlight_id)


from django.shortcuts import render, redirect
from .models import Task
from datetime import datetime, timedelta
from .forms import TaskForm

# Create your views here.
def todo1(request):
    tasks = Task.objects.all();
    context = {
        'task': tasks
    }
    return render (request, "todo_list.html", context)

def todo(request):
    todo_list = Task.objects.all()
    context = {
        'todo_list': todo_list,
    }
    print(context)
    return render (request, 'todo_list.html', context)

def comp_todo(request, index):
    cur_day = datetime.today ()
    task = {
        'title': "Task {}".format (index),
        'created': cur_day.strftime ("%d/%m/%y"),
        'due_on': (cur_day + timedelta (days=3)).strftime ("%d/%m/%y"),
        'owner': "admin",
        'mark': 'Done'
    }
    context = {
        'task': task
    }
    return render (request, 'completed_todo.html', context)

def delete_task(request, id):
    try:
        task = Task.objects.get(pk=id)
        task.delete()
    except Exception as e:
        print (e)
    task = Task.objects.all()
    context = {
        'task': task
    }
    return render (request, 'todo_list.html', context)

def new_task(request):
    if request.method == 'POST':
        form = TaskForm (request.POST)
        if form.is_valid ():
            form.save ()
            return redirect ('todos')
    else:
        form = TaskForm ()
    context = {
        'form': form
    }
    return render (request, 'new.html', context)


"""
This module contains classes and methods for working with interleaved channels.
"""

import typing

import numpy

import redvox.api900.constants as constants
import redvox.api900.exceptions as exceptions
import redvox.api900.lib.api900_pb2 as api900_pb2
import redvox.api900.migrations as migrations
import redvox.api900.reader_utils as reader_utils
import redvox.api900.stat_utils as stat_utils

# pylint: disable=R0902
class InterleavedChannel:
    """
    This class represents an interleaved channel.

    An interleaved channel contains multiple channel types in a single payload. This is useful for situations where
    a sensor produces several values for a single timestamp. For example, a GPS will produce a LATITUDE, LONGITUDE,
    ALTITUDE, and SPEED values with every update. An interleaved channel encodes all four of those channel types
    into a single payload.

    Every channel has a field channel_types that list the channel types contained within the payload. For a GPS sensor,
    the channel_types array would look like [LATITUDE, LONGITUDE, ALTITUDE, SPEED]. The location of the channel type
    in channel_types determines the offset into the payload and the length of channel_types determines the step size.
    As such, this hypothetical GPS channel payload be encoded as:

    [LAT0, LNG0, ALT0, SPD0, LAT1, LNG1, ALT1, SPD1, ..., LATn, LNGn, ALTn, SPDn]

    This class provides methods for working with interleaved channels as well as accessing interleaved statistic values.
    """

    def __init__(self, channel: typing.Optional[typing.Union[api900_pb2.EvenlySampledChannel,
                                                             api900_pb2.UnevenlySampledChannel]] = None):
        """
        Initializes this interleaved channel object.
        :param channel: Either a protobuf evenly or unevenly sampled channel.
        note: value_means, value_medians, value_stds, and channel_type_index are only set during initialization or
            when payload is altered
        payload should only be altered by set_payload or set_deinterleaved_payload due to the extra data values that are
            required to correctly set the protobuf_channel
        """
        if channel is None:
            self.protobuf_channel = None
            self.sensor_name = None
            self.channel_types = [0]
            self.payload = [0]
            self.metadata = []
            self.value_means = []
            self.value_stds = []
            self.value_medians = []
            self.channel_type_index = []
        else:
            self.protobuf_channel: typing.Union[api900_pb2.EvenlySampledChannel,
                                                api900_pb2.UnevenlySampledChannel] = channel
            """Reference to the original protobuf channel"""

            self.sensor_name: str = channel.sensor_name
            """Provided sensor name"""

            self.channel_types: typing.List[
                typing.Union[
                    api900_pb2.EvenlySampledChannel,
                    api900_pb2.UnevenlySampledChannel]] = reader_utils.repeated_to_list(channel.channel_types)
            """List of channel type constant enumerations"""

            self.payload: numpy.ndarray = reader_utils.extract_payload(channel)
            """This channels payload as a numpy array of either floats or ints"""

            self.metadata: typing.List[str] = reader_utils.repeated_to_list(channel.metadata)
            """This channels list of metadata"""

            self.value_means: numpy.ndarray = reader_utils.repeated_to_array(channel.value_means)
            """Interleaved array of mean values"""

            self.value_stds: numpy.ndarray = reader_utils.repeated_to_array(channel.value_stds)
            """Interleaved array of standard deviations of values"""

            self.value_medians: numpy.ndarray = reader_utils.repeated_to_array(channel.value_medians)
            """Interleaves array of median values"""

            self.channel_type_index: typing.Dict[api900_pb2.ChannelType, int] = {self.channel_types[i]: i for
                                                                                i in
                                                                                range(
                                                                                len(
                                                                                self.channel_types))}
            """Contains a mapping of channel type to index in channel_types array"""

    def set_channel_types(self, types: typing.List[typing.Union[api900_pb2.EvenlySampledChannel,
                                                                api900_pb2.UnevenlySampledChannel]]):
        """
        sets the channel_types to the list given
        :param types: a list of channel types
        """
        del self.protobuf_channel.channel_types[:]
        for ctype in types:
            self.protobuf_channel.channel_types.append(ctype)
        self.channel_types = reader_utils.repeated_to_list(self.protobuf_channel.channel_types)
        self.channel_type_index = {self.channel_types[i]: i for i in range(len(self.channel_types))}

    def get_channel_type_names(self) -> typing.List[str]:
        """
        Returns the list of channel_types as a list of names instead of enumeration constants.
        :return: The list of channel_types as a list of names instead of enumeration constants.
        """
        return list(map(reader_utils.channel_type_name_from_enum, self.channel_types))

    def channel_index(self, channel_type: int) -> int:
        """
        Returns the index of a channel type or -1 if it DNE.
        :param channel_type: The channel type to search for.
        :return: The index of the channel or -1 if it DNE.
        """
        return self.channel_type_index[channel_type] if channel_type in self.channel_type_index else -1

    def has_channel(self, channel_type: int) -> bool:
        """
        Returns if channel type exists with in this channel.
        :param channel_type: The channel type to search for.
        :return: True if it exist, False otherwise.
        """
        return channel_type in self.channel_type_index

    def set_channel(self, channel: typing.Union[api900_pb2.EvenlySampledChannel,
                                                api900_pb2.UnevenlySampledChannel]):
        """
        Sets the protobuf channel
        :param channel: protobuf channel
        """
        self.protobuf_channel = channel
        self.sensor_name = channel.sensor_name
        self.channel_types = reader_utils.repeated_to_list(channel.channel_types)
        self.payload = reader_utils.extract_payload(channel)
        self.metadata = reader_utils.repeated_to_list(channel.metadata)
        self.value_means = reader_utils.repeated_to_array(channel.value_means)
        self.value_stds = reader_utils.repeated_to_array(channel.value_stds)
        self.value_medians = reader_utils.repeated_to_array(channel.value_medians)
        self.channel_type_index = {self.channel_types[i]: i for i in range(len(self.channel_types))}

    def has_payload(self, channel_type: int) -> bool:
        """
        Returns if channel contains a non-empty specified payload.
        :param channel_type: The channel to check for a payload for.
        :return: Whether this channel contains the specified payload.
        """
        return self.has_channel(channel_type) and len(self.payload) > 0

    def set_payload(self,
                    payload_values: typing.Union[numpy.ndarray, typing.List],
                    pl_type: constants.PayloadType,
                    should_compute_stats=True):
        """
        sets the payload to an interleaved channel with step number of arrays interleaved together.
        :param should_compute_stats: Whether the statistics should be computed or not (optional default to True)
        :param payload_values: Interleaved payload values
        :param pl_type: payload type
        """
        # if len(payload_values) < 1:
        #     raise exceptions.ReaderException("Channel must not be empty and number of arrays must not be less than
        #     1.")

        # Convert to numpy array is necessary
        payload_values = reader_utils.to_array(payload_values)

        # clear all other payloads
        self.protobuf_channel.byte_payload.ClearField("payload")
        self.protobuf_channel.uint32_payload.ClearField("payload")
        self.protobuf_channel.uint64_payload.ClearField("payload")
        self.protobuf_channel.int32_payload.ClearField("payload")
        self.protobuf_channel.int64_payload.ClearField("payload")
        self.protobuf_channel.float32_payload.ClearField("payload")
        self.protobuf_channel.float64_payload.ClearField("payload")

        # set the payload based on the type of data
        if pl_type == constants.PayloadType.BYTE_PAYLOAD:
            self.protobuf_channel.byte_payload.payload = payload_values
        elif pl_type == constants.PayloadType.UINT32_PAYLOAD:
            self.protobuf_channel.uint32_payload.payload.extend(payload_values)
        elif pl_type == constants.PayloadType.UINT64_PAYLOAD:
            self.protobuf_channel.uint64_payload.payload.extend(payload_values)
        elif pl_type == constants.PayloadType.INT32_PAYLOAD:
            self.protobuf_channel.int32_payload.payload.extend(payload_values)
        elif pl_type == constants.PayloadType.INT64_PAYLOAD:
            self.protobuf_channel.int64_payload.payload.extend(payload_values)
        elif pl_type == constants.PayloadType.FLOAT32_PAYLOAD:
            self.protobuf_channel.float32_payload.payload.extend(payload_values)
        elif pl_type == constants.PayloadType.FLOAT64_PAYLOAD:
            self.protobuf_channel.float64_payload.payload.extend(payload_values)
        else:
            raise TypeError("Unknown payload type to set.")

        if len(payload_values) < 1:
            self.payload = payload_values
        else:
            self.payload = reader_utils.extract_payload(self.protobuf_channel)

            # calculate the means, std devs, and medians
            if should_compute_stats:
                self.update_stats()

    def set_interleaved_payload(self,
                                payloads: typing.List[typing.Union[typing.List, numpy.ndarray]],
                                pl_type: constants.PayloadType,
                                should_compute_stats: bool = True):
        """
        Interleaves multiple payloads together and sets the interleaved payload.
        :param payloads: Payloads of the same length to be interleaved.
        :param pl_type: The payload type
        :param should_compute_stats: Whether or not payload stats should be calculated.
        """
        interleaved = reader_utils.interleave_arrays(list(map(reader_utils.to_array, payloads)))
        self.set_payload(interleaved, pl_type, should_compute_stats)

    def get_payload(self, channel_type: int) -> numpy.ndarray:
        """
        Returns a deinterleaved payload of a given channel type or an empty array.
        :param channel_type: The channel type to extract/deinterleave from the payload.
        :return: A numpy array of floats or ints of a single channel type.
        """
        idx = self.channel_index(channel_type)
        if idx < 0:
            return reader_utils.empty_array()
        try:
            payload: numpy.ndarray = reader_utils.deinterleave_array(self.payload, idx, len(self.channel_types))
            return migrations.maybe_get_float(payload)
        except exceptions.ReaderException:
            return reader_utils.empty_array()

    def get_payload_type(self) -> str:
        """
        Returns the internal protobuf payload type.
        :return: The internal protobuf payload type.
        """
        return reader_utils.payload_type(self.protobuf_channel)

    def get_multi_payload(self, channel_types: typing.List[int]) -> numpy.ndarray:
        """
        Returns an interleaved payload with the given channel types.
        :param channel_types: Channel types to interleave into a single payload.
        :return: A numpy array of an interleaved payload.
        """
        channel_types_len = len(channel_types)
        if channel_types_len == 0:
            return reader_utils.empty_array()
        elif channel_types_len == 1:
            return self.get_payload(channel_types[0])

        payloads = list(map(self.get_payload, channel_types))
        return reader_utils.interleave_arrays(payloads)

    def get_value_mean(self, channel_type: int) -> float:
        """
        Returns the mean value for a single channel type.
        :param channel_type: The channel type to extract the mean from.
        :return: The mean value.
        """
        idx = self.channel_index(channel_type)
        if idx < 0 or len(self.value_means) == 0:
            raise exceptions.ReaderException("mean DNE, is the payload empty?")

        return self.value_means[idx]

    def get_value_std(self, channel_type: int) -> float:
        """
        Returns the standard deviation value for a single channel type.
        :param channel_type: The channel type to extract the std from.
        :return: The standard deviation.
        """
        idx = self.channel_index(channel_type)
        if idx < 0 or len(self.value_stds) == 0:
            raise exceptions.ReaderException("std DNE, is the payload empty?")

        return self.value_stds[idx]

    def get_value_median(self, channel_type: int) -> float:
        """
        Returns the median value for a single channel type.
        :param channel_type: The channel type to extract the median from.
        :return:The median value.
        """
        idx = self.channel_index(channel_type)
        if idx < 0 or len(self.value_medians) == 0:
            raise exceptions.ReaderException("median DNE, is the payload empty?")

        return self.value_medians[idx]

    def update_stats(self):
        """
        updates mean, std and median for all values
        """
        channel = self.payload
        step = len(self.channel_types)
        del self.protobuf_channel.value_means[:]
        del self.protobuf_channel.value_stds[:]
        del self.protobuf_channel.value_medians[:]
        for i in range(step):
            std, mean, median = stat_utils.calc_utils(reader_utils.deinterleave_array(channel, i, step))
            self.protobuf_channel.value_means.append(mean)
            self.protobuf_channel.value_stds.append(std)
            self.protobuf_channel.value_medians.append(median)
        self.value_stds = reader_utils.repeated_to_array(self.protobuf_channel.value_stds)
        self.value_means = reader_utils.repeated_to_array(self.protobuf_channel.value_means)
        self.value_medians = reader_utils.repeated_to_array(self.protobuf_channel.value_medians)

    def set_sensor_name(self, name: str):
        """
        sets the sensor name
        :param name: name of sensor
        """
        self.sensor_name = name
        self.protobuf_channel.sensor_name = name

    def set_metadata(self, data: typing.List[str]):
        """
        sets the metadata
        :param data: metadata as list of strings
        """
        del self.protobuf_channel.metadata[:]
        for meta in data:
            self.protobuf_channel.metadata.append(meta)
        self.metadata = reader_utils.repeated_to_list(self.protobuf_channel.metadata)

    def metadata_as_dict(self) -> typing.Dict[str, str]:
        """
        Returns any metadata as a dictionary of key-pair values.
        :return: Any metadata as a dictionary of key-pair values.
        """
        return reader_utils.get_metadata_as_dict(self.metadata)

    def __str__(self) -> str:
        """
        Returns a string representation of this interleaved channel.
        :return: A string representation of this interleaved chanel.
        """
        return "sensor_name: {}\n" \
               "channel_types: {}\n" \
               "len(payload): {}\n" \
               "payload_type: {}".format(self.sensor_name,
                                         list(map(
                                             reader_utils.channel_type_name_from_enum,
                                             self.channel_types)),
                                         len(self.payload),
                                         reader_utils.payload_type(
                                             self.protobuf_channel))


# created by Lokesh Narasani

from flask import Flask, request, jsonify
from flask_cors import CORS
from newsapi import NewsApiClient

app = Flask(__name__)
CORS(app)
newsapi = NewsApiClient(api_key='1cfd6989b86346a4a9d17c2bf73aed96')

@app.route('/news', methods=['GET'])
def getNews():
    top_headlines = newsapi.get_top_headlines(q='finance',
                                              category='business',
                                              language='en',
                                              country='us')
    print(top_headlines)
    return jsonify({'topheadlines': top_headlines})

if __name__ == "__main__":
    app.run()


### Quantiacs Trend Following Trading System Example
#Import necessary Packages below:
from datetime import date
import numpy as np
from scipy.signal import lfilter
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.decomposition import PCA
from sklearn.preprocessing import StandardScaler
import statsmodels.tsa.holtwinters
from statsmodels.tsa.statespace.sarimax import SARIMAX
from statsmodels.tsa.stattools import acf, pacf
from random import random
from warnings import filterwarnings
import quantiacsToolbox

#Settings for chosen market and importing data
market = ['F_ES']
data = quantiacsToolbox.loadData(marketList = market, dataToLoad = ['DATE', 'CLOSE', 'USA_CPI'], beginInSample = '20190101', endInSample = '2020930')

#Creating our dataframe
market_closing_prices = data['CLOSE']
time = data['DATE']
exog = data['USA_CPI']

#Setting order from data exploration done in r
order = (4,1,3)
seasonal_order = (0,0,0,0)

#Function to parse time for assignment to series index
def parse_time(time_array):
    new_time_array = []
    for date_object in time_array:
        parsed_date = date(year=int(str(date_object)[0:4]), month=int(str(date_object)[4:6]), day=int(str(date_object)[6:8]))
        new_time_array.append(parsed_date)
    return new_time_array
time = parse_time(time)

#Storing our endogenous and exogenous data for use
information_df = pd.Series(market_closing_prices.flatten(), index = time)
exog_df = pd.Series(exog.flatten(), index = time)
information_df.index.freq = 'D'

#Trading algorithmn now that data preparation is ready
def myTradingSystem(DATE, CLOSE, USA_CPI, settings):
    ''' This system uses trend following techniques to allocate capital into the desired equities'''
    nMarkets=CLOSE.shape[1] #Number of markets we are looking at (should we use > 1)
    x_1 = CLOSE[-1].flatten() #Latest endogenous data
    z_1 = USA_CPI[-1].flatten() #Latest exogenous data

    #Parsing time and setting indexes to that both series are aligned
    new_time = parse_time([DATE[-1],])
    new_data = pd.Series(x_1, index = new_time)
    new_exog = pd.Series(z_1, index = new_time)
    new_data.index.freq = 'D'
    new_exog.index.freq = 'D'
    global information_df
    global exog_df

    #Code to add in latest data to our respective storages
    information_df = information_df.append(new_data)
    exog_df = exog_df.append(new_exog)
    #print(set(information_df.index) == set(exog_df.index)) #test if indexes are equal

    #Creating and fitting model
    model = SARIMAX(information_df, exog_df ,order = order, seasonal_order = seasonal_order)
    model_fit = model.fit()

    #Forecasting next predicted CLOSE value
    pred = model_fit.forecast(exog = exog_df[-1])
    pred = pred.values[0]

    #Trading decisions
    #If predicted CLOSE > current CLOSE, long
    #If predicted CLOSE < current CLOSE, short
    pos=np.zeros(nMarkets)
    if (pred>x_1[0]):
        pos[0] = 1
    elif (pred< x_1[0]):
        pos[0] = -1

    weights = pos/np.nansum(abs(pos))
    return weights, settings

def mySettings():
    settings= {}
    #Market used is E-mini for S&P 500
    settings['markets'] = ['F_ES']
    settings['beginInSample'] = '20201001'
    settings['endInSample'] = '20201231'
    settings['lookback']= 2
    settings['budget']= 10**6
    settings['slippage']= 0.05
    return settings

# Evaluate trading system defined in current file.
if __name__ == '__main__':
    filterwarnings("ignore")
    results = quantiacsToolbox.runts(__file__)


import sys

from PyQt5.QtCore import pyqtSlot
from PyQt5.QtWidgets import QWidget, QPushButton, QApplication, QVBoxLayout, QHBoxLayout, QLineEdit, QCheckBox, \
    QTextEdit, QGroupBox

from model.model import LayerInfo, Layers

class LayerFrame(QWidget):
    def __init__(self, layers, layer_id):
        super().__init__()
        self.init_ui()
        self.layer_frame_id = layer_id
        self.layer_info = LayerInfo(self.layer_frame_id)
        layers.add_layer(layer_id, self.layer_info)

    def init_ui(self):
        # Layer Type Area
        self._cb1 = QCheckBox('&input layer')
        self._cb2 = QCheckBox('&hidden layer')
        self._cb3 = QCheckBox('o&utput layer')

        # Activation Function Area
        self._ac_fn_editor = QTextEdit()

        # Neurons Number Area, contains a QlineEdit & 2 button
        self._neurons_number_editor = QLineEdit()
        self._edit_btn = QPushButton('&edit')
        self._lock_btn = QPushButton('&ok')

        # new groups
        self._layer_type_group = QGroupBox('Layer Type')
        self._ac_fn_group = QGroupBox('Activation Function')
        self._neurons_number_group = QGroupBox('Neurons Number')

        # set widgets layout
        layout = QHBoxLayout()
        cb_layout = QVBoxLayout()
        fn_layout = QVBoxLayout()
        neurons_num_edit_layout = QVBoxLayout()
        cb_layout.addWidget(self._cb1)
        cb_layout.addWidget(self._cb2)
        cb_layout.addWidget(self._cb3)
        fn_layout.addWidget(self._ac_fn_editor)
        neurons_num_edit_layout.addWidget(self._neurons_number_editor)
        neurons_num_edit_layout.addWidget(self._edit_btn)
        neurons_num_edit_layout.addWidget(self._lock_btn)
        self._layer_type_group.setLayout(cb_layout)
        self._ac_fn_group.setLayout(fn_layout)
        self._neurons_number_group.setLayout(neurons_num_edit_layout)
        layout.addWidget(self._layer_type_group)
        layout.addWidget(self._ac_fn_group)
        layout.addWidget(self._neurons_number_group)
        self.setLayout(layout)

        self._cb1.clicked.connect(lambda x: self.set_exclusive_check_boxes(1))
        self._cb2.clicked.connect(lambda x: self.set_exclusive_check_boxes(2))
        self._cb3.clicked.connect(lambda x: self.set_exclusive_check_boxes(3))
        self._lock_btn.clicked.connect(lambda x: self.has_locked_layer_info_widgets(True))
        self._edit_btn.clicked.connect(lambda x: self.has_locked_layer_info_widgets(False))

    @pyqtSlot()
    def set_exclusive_check_boxes(self, box_id):
        self.layer_info.set_layer_type(box_id)
        if box_id == 1:
            self._cb1.setChecked(True)
            self._cb2.setChecked(False)
            self._cb3.setChecked(False)
        if box_id == 2:
            self._cb1.setChecked(False)
            self._cb2.setChecked(True)
            self._cb3.setChecked(False)
        if box_id == 3:
            self._cb1.setChecked(False)
            self._cb2.setChecked(False)
            self._cb3.setChecked(True)

    @pyqtSlot()
    def has_locked_layer_info_widgets(self, has_locked):
        self._cb1.setEnabled(not has_locked)
        self._cb2.setEnabled(not has_locked)
        self._cb3.setEnabled(not has_locked)
        self._ac_fn_editor.setEnabled(not has_locked)
        self._neurons_number_editor.setEnabled(not has_locked)
        self._edit_btn.setEnabled(has_locked)
        self._lock_btn.setEnabled(not has_locked)

        # only save layer info when clicked button 'ok'
        if has_locked:
            self.save_layer_info()

    def save_layer_info(self):
        self.layer_info.set_layer_ac_fn(self._ac_fn_editor.toPlainText())

        # [todo] input non number cause an error
        try:
            neurons_num = int(self._neurons_number_editor.text())
        except ValueError:
            pass
        else:
            self.layer_info.set_neurons_num(neurons_num)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    layer_frame = LayerFrame(Layers(), 1)
    layer_frame.show()
    app.exec_()


from django.http import HttpResponse
from django.template import Template, Context
from django.template import loader
from django.shortcuts import render

import os
import numpy as np
import csv
import pandas as pd
import math

clear = lambda: os.system("cls")
f = pd.read_csv("CoefAcu.csv")
pd.DataFrame(f)

reader = csv.reader(open('CoefAcu.csv'))

Materiales = {}
for row in reader:
   key = row[0]
   if key in Materiales:
       pass
   milistafloat = []
   for item in row[1:]:
       milistafloat.append(float(item))
   Materiales[key] = milistafloat

class room():
    def __init__(self, x, y, z):
        self.x = x
        self.y = y
        self.z = z
        self.v = x*y*z
        self.a = 2*(x*y+x*z+y*z)

    def Calcular(self,T, P, WF, WB, WL, WR, v, a, ssaW):
        self.sa    = T + P + WF + WB + WL + WR + ssaW
        self.RT60f = -0.161*v/(a*np.log(1-(self.sa/a)))
        self.RT60  = np.mean(self.RT60f)
        self.dev = (np.mean((self.RT60f-self.RT60)**2))**(1/2.0)

class wall():
    def __init__(self,Nombre,N,d1,d2):
        self.Nombre = Nombre
        self.Wall   = N
        self.Ancho  = d1
        self.Largo  = d2
        self.aW     = d1*d2

    def mat(self,m):
        self.MNombre  = m
        self.MCoefAcu = Materiales[self.MNombre]

    def saWall(self):
        self.saW = self.aW*np.array(self.MCoefAcu)

    def restaWall(self,aOW):
        self.aOW = aOW
        self.aW -= self.aOW

class objWall(wall):
    def mat(self,m):
        self.MNombre  = m
        self.MCoefAcu = Materiales[self.MNombre]

    def saWall(self ):
        self.saW = self.aW*np.array(self.MCoefAcu)

    def ssaWall(self,ssaW):
        ssaW = ssaW

def home(request):
    Index_Loader = loader.get_template('index.html')
    Index_Template = Index_Loader.render({})
    return HttpResponse(Index_Template)

def RT60(request):
    clear()
    RT60_Loader = loader.get_template('RT60.html')
    RT60_Template = RT60_Loader.render({"Materiales":Materiales})
    return HttpResponse(RT60_Template)

import json
def RT60_resp(request):
    clear()
    if request.method == "GET":

        R = room (float(request.GET.get("x")),
                  float(request.GET.get("y")),
                  float(request.GET.get("z")))

        T  = wall("Techo",          "T",      R.x,R.z)
        P  = wall("Piso",           "P",      R.x,R.z)
        WF = wall("Pared Frontal",  "WF",     R.x,R.y)
        WB = wall("Pared trasera",  "WB",     R.x,R.y)
        WL = wall("Pared izquierda","WL",     R.z,R.y)
        WR = wall("Pared derecha",  "WR",     R.z,R.y)

        T.mat(request.GET.get("Material_T"))
        P.mat(request.GET.get("Material_P"))
        WF.mat(request.GET.get("Material_WF"))
        WB.mat(request.GET.get("Material_WB"))
        WL.mat(request.GET.get("Material_WL"))
        WR.mat(request.GET.get("Material_WR"))

        ssaW = np.zeros([1,6])
        objetos = {}
        sobjsaW = []

        if (request.GET.get('Extras') != None):
            Extras = json.loads(request.GET.get('Extras'))

            n = 0
            i = 0
            while i < len(Extras):
                clear()
                obj = Extras[n]

                objx = objWall( obj[1],
                                obj[2],
                                float(obj[3]),
                                float(obj[4]))

                objx.mat(obj[5])

                if   objx.Wall == "Techo":              T.restaWall(objx.aW)
                elif objx.Wall == "Piso":               P.restaWall(objx.aW)
                elif objx.Wall == "Pared Frontal":      WF.restaWall(objx.aW)
                elif objx.Wall == "Pared Trasera":      WB.restaWall(objx.aW)
                elif objx.Wall == "Pared Izquierda":    WL.restaWall(objx.aW)
                elif objx.Wall == "Pared Derecha":      WR.restaWall(objx.aW)

                objx.saWall()

                objetos[n]=[objx.Nombre,objx.Wall,objx.aW,objx.MNombre,objx.saW]
                sobjsaW.append(np.around(objx.saW,2,None).tolist())

                ssaW += np.array(objx.saW)
                n += 1
                i += 1

        else:
            Extras = None

        T. saWall()
        P. saWall()
        WF.saWall()
        WB.saWall()
        WL.saWall()
        WR.saWall()

        saTnp = np.around(T .saW,2,None)
        saPnp = np.around(P .saW,2,None)
        saWFnp = np.around(WF .saW,2,None)
        saWBnp = np.around(WB .saW,2,None)
        saWLnp = np.around(WL .saW,2,None)
        saWRnp = np.around(WR .saW,2,None)

        saT = saTnp.tolist()
        saP = saPnp.tolist()
        saWF = saWFnp.tolist()
        saWB = saWBnp.tolist()
        saWL = saWLnp.tolist()
        saWR = saWRnp.tolist()

        R.Calcular( T.saW,
                    P.saW,
                    WF.saW,
                    WB.saW,
                    WL.saW,
                    WR.saW,

                    R.v,
                    R.a,
                    ssaW )

        RT60f = np.around(R.RT60f,2,None)
        RT60f = RT60f.tolist()

    datos = {
            "x"   :R.x,
            "y"   :R.y,
            "z"   :R.z,

            "v"   :np.around(R.v,2,None),
            "a"   :np.around(R.a,2,None),

            "sT"  :np.around(T.aW,2,None),
            "sP"  :np.around(P.aW,2,None),
            "sWF" :np.around(WF.aW,2,None),
            "sWB" :np.around(WB.aW,2,None),
            "sWL" :np.around(WL.aW,2,None),
            "sWR" :np.around(WR.aW,2,None),

            "lT"  :T.Largo,
            "lP"  :P.Largo,
            "lWF" :WF.Largo,
            "lWB" :WB.Largo,
            "lWL" :WL.Largo,
            "lWR" :WR.Largo,

            "aT"  :T.Ancho,
            "aP"  :P.Ancho,
            "aWF" :WF.Ancho,
            "aWB" :WB.Ancho,
            "aWL" :WL.Ancho,
            "aWR" :WR.Ancho,

            "matT"  :T.MNombre,
            "matP"  :P.MNombre,
            "matWF" :WF.MNombre,
            "matWB" :WB.MNombre,
            "matWL" :WL.MNombre,
            "matWR" :WR.MNombre,

            "saT" : saT,
            "saP" : saP,
            "saWF" : saWF,
            "saWB" : saWB,
            "saWL" : saWL,
            "saWR" : saWR,

            "cT"  :T.MCoefAcu,
            "cP"  :P.MCoefAcu,
            "cWF" :WF.MCoefAcu,
            "cWB" :WB.MCoefAcu,
            "cWL" :WL.MCoefAcu,
            "cWR" :WR.MCoefAcu,

            "ssaW":ssaW.tolist(),
            "sobjsaW":sobjsaW,
            "Extras": Extras,

            "RT60f":RT60f,
            "dev":round(R.dev,3),

            "RT60":round(R.RT60,3)
    }

    return HttpResponse(json.dumps(datos), content_type='application/json')

class Resonador():
    def __init__(self,fr,q):
        self.fr = fr
        self.q  = q
        self.bw = fr/q
        self.fh = fr + self.bw/2
        self.fl = fr - self.bw/2
        self.RT60 = 2.2/self.bw

class Difragmatico(Resonador):
    def panelD(self, b, h, m):
        self.b = b
        self.h = h
        self.m = m
        self.d = m/(b*h)
        self.e = ((6/self.fr)**2)/self.d

class Perforado(Resonador):
    def panelP(self, b, h, g, d, p):
        self.b = b
        self.h = h
        self.g = g
        self.d = d
        self.p = p
        self.s = ((78.5*(2.54*d)**2)/p)**(1/2.0)
        self.nb = round((b/self.s)-1)
        self.nh = round((h/self.s)-1)
        self.n = self.nb*self.nh
        self.e = ((548/self.fr)**2)*(p/(g+0.8*(2.54*d)))
        self.borde_altura = (self.b - (round(self.s,2)*(self.nb-1)))/2
        self.borde_base = (self.h - (round(self.s,2)*(self.nh-1)))/2

def Paneles(request):
    clear()
    Paneles_Loader = loader.get_template('Paneles.html')
    Paneles_Template = Paneles_Loader.render({})
    return HttpResponse(Paneles_Template)

def Paneles_resp(request):

    if request.method == "GET":
        clear()

        Tipo = request.GET.get("tipo")
        if Tipo == "Difragmático":
            P = Difragmatico(   float(request.GET.get("f")),
                                float(request.GET.get("q")))

            P.panelD(   float(request.GET.get("db")),
                        float(request.GET.get("dh")),
                        float(request.GET.get("dm"))
            )

            datos = {
                "f":P.fr,
                "q":P.q,

                "bw":round(P.bw,2),
                "fh":round(P.fh,2),
                "fl":round(P.fl,2),
                "RT60":round(P.RT60,3),

                "db":P.b,
                "dh":P.h,
                "dm":P.m,
                "dd":round(P.d*100000,2),
                "de":round(P.e,2)
            }

        if Tipo == "Perforado":
            P = Perforado(   float(request.GET.get("f")),
                            float(request.GET.get("q")))

            P.panelP(   float(request.GET.get("pb")),
                        float(request.GET.get("ph")),
                        float(request.GET.get("pg")),
                        float(request.GET.get("pd")),
                        float(request.GET.get("pp")),
            )

            print(P.borde_base)
            print(P.borde_altura)

            datos = {
                "f":P.fr,
                "q":P.q,
                "bw":round(P.bw,2),
                "fh":round(P.fh,2),
                "fl":round(P.fl,2),
                "RT60":round(P.RT60,3),

                "pb":P.b,
                "ph":P.h,
                "pg":P.g,
                "pd":P.d,
                "pp":P.p,
                "pnb":P.nb,
                "pnh":P.nh,

                "pn":round(P.n),
                "ps":round(P.s,2),
                "pe":round(P.e,2),

                "borde_base":round(P.borde_base,2),
                "borde_altura":round(P.borde_altura,2)
            }

    return HttpResponse(json.dumps(datos), content_type='application/json')

bw3 = 31.25
bw2 = bw3/2**(1/3.0)
bw1 = bw2/2**(1/3.0)

Tercios_Octava = np.array([bw1,bw2,bw3])
Tercios_Octava2 = np.array([bw1,bw2,bw3])
for i in range (2,6,1):
    Tercios_Octava2 = np.array(Tercios_Octava2*2)
    Tercios_Octava  = np.concatenate((Tercios_Octava,Tercios_Octava2),axis=None)

Tercios_Octava_Low  = Tercios_Octava/2**(1/6)
Tercios_Octava_High = Tercios_Octava*2**(1/6)

n = 20

class room_modos():
    def __init__ (self, x, y, z, RT60):
        self.x = x
        self.y = y
        self.z = z
        self.d = np.array([x,y,z])
        self.RT60 = RT60
        self.v = x*y*z
        self.max = max(x,y,z)
        self.f1	 = 343/(2*self.max)
        self.f2  = 1893*(self.RT60/self.v)**(1/2.0)
        self.f3	 = self.f2*4
        self.sf  = 2000*(self.RT60/self.v)**(1/2.0)
        self.bw  = 2.2/self.RT60

    def modo_axial(self):
        self.axial = np.ones((n,3))
        for i in range(1,n+1,1):
            x = 343/2*i/self.d
            self.axial[i-1] = x*self.axial[i-1]

    def conteo(self):
        self.conteo_modos = np.zeros((4,15))

        for s in range(0,3,1):
            for e in range(0,n,1):
                for i in range(0,15,1):
                    if self.axial[e,s] > Tercios_Octava_Low[i] and self.axial[e,s] < Tercios_Octava_High[i]:
                        self.conteo_modos[s,i] += 1
        for i in range(0,15,1):
            self.conteo_modos[3,i] = self.conteo_modos[0,i] + self.conteo_modos[1,i] + self.conteo_modos[2,i]

def Modos(request):
    clear()
    Modos_Loader = loader.get_template('Modos.html')
    Modos_Template = Modos_Loader.render({})
    return HttpResponse(Modos_Template)

def Modos_resp(request):
    clear()
    if request.method == "GET":

        R = room_modos (float(request.GET.get("x")),
                  float(request.GET.get("y")),
                  float(request.GET.get("z")),
                  float(request.GET.get("RT60"))
                  )

        R.modo_axial()

        axial = np.around(R.axial,1,None)

        axial_lista = axial.tolist()

        R.conteo()

        conteo = R.conteo_modos

        conteo_lista = conteo.tolist()

        header_a = np.array([['' ,'x','y','z']])
        axial_df = pd.DataFrame(data=R.axial,index=range(1,n+1,1),columns=header_a[0,1:])
        conteo_df = pd.DataFrame(data = R.conteo_modos, index = ['x','y','z','suma'] , columns = np.round(Tercios_Octava))

        axial_orden = R.axial.flatten()
        axial_orden = np.sort(axial_orden)
        axial_orden = np.around(axial_orden,2,None)
        axial_orden = axial_orden.tolist()

        print(axial_df)
        print(conteo_df)

        datos = {
            "x":R.x,
            "y":R.y,
            "z":R.z,
            "RT60":R.RT60,

            "f1":round(R.f1),
            "f2":round(R.f2),
            "f3":round(R.f3),
            "sf":round(R.sf,2),
            "bw":round(R.bw,2),

            "axial":axial_lista,
            "conteo":conteo_lista,
            "axial_orden":axial_orden
        }

    return HttpResponse(json.dumps(datos), content_type='application/json')