from math import log2

import numpy as np
import helpers as hp
import statistics as st

def informationBenefit(docs_category_with_word, \
                       docs_not_category_with_word, \
                       docs_category_without_word, \
                       docs_not_category_without_word, \
                       count, categories_count):
    first_coeffictient = (docs_category_with_word + docs_category_without_word) / count
    second_coeficient = (docs_category_with_word + docs_not_category_with_word) / count
    third_coefficient = docs_category_with_word / count
    fourth_coefficient = (docs_category_without_word + docs_not_category_without_word) / count
    fifth_coefficient = docs_category_without_word / count
    benefit = -first_coeffictient * log2(first_coeffictient) + \
              second_coeficient * third_coefficient * log2(third_coefficient) + \
              fourth_coefficient * fifth_coefficient * log2(fifth_coefficient)
    return benefit

def mutualInformation(docs_category_with_word, \
                      docs_not_category_with_word, \
                      docs_category_without_word, \
                      docs_not_category_without_word, \
                      count, categories_count):
    first_coeffictient = (docs_category_with_word + docs_category_without_word)  # A + C
    second_coeficient = (docs_category_with_word + docs_not_category_with_word)  # A + B
    third_coefficient = (docs_not_category_with_word + docs_not_category_without_word)  # B + D
    fourth_coefficient = (docs_category_without_word + docs_not_category_without_word)  # C + D
    info = (docs_category_with_word / count) * \
           log2((docs_category_with_word * count) / (first_coeffictient * second_coeficient)) + \
           (docs_category_without_word / count) * \
           log2((docs_category_without_word * count) / (first_coeffictient * fourth_coefficient)) + \
           (docs_not_category_with_word / count) * \
           log2((docs_not_category_with_word * count) / (second_coeficient * third_coefficient)) + \
           (docs_not_category_without_word / count) * \
           log2((docs_not_category_without_word * count) / (third_coefficient * fourth_coefficient))
    return info

def khi2Value(docs_category_with_word, \
              docs_not_category_with_word, \
              docs_category_without_word, \
              docs_not_category_without_word, \
              count, categories_count):
    first_coeffictient = (docs_category_with_word + docs_category_without_word)  # A + C
    second_coeficient = (docs_category_with_word + docs_not_category_with_word)  # A + B
    third_coefficient = (docs_not_category_with_word + docs_not_category_without_word)  # B + D
    fourth_coefficient = (docs_category_without_word + docs_not_category_without_word)  # C + D
    fifth_coefficient = abs(docs_category_with_word * docs_not_category_without_word - \
                            docs_category_without_word * docs_not_category_with_word)  # AD - CB
    khi2 = (count * (fifth_coefficient - 0.5 * count) ** 2.0) / (first_coeffictient * second_coeficient * \
                                                                 third_coefficient * fourth_coefficient)
    return khi2

def informativnessCriteriums(raw_data, data_categories, \
                             categories, words_bag, criteriums):
    count = len(raw_data[0])
    categories_count = len(categories)
    words_count = len(words_bag)
    words_information = np.array([[0.] * words_count for i in range(len(categories))])
    categories_information = [0.] * categories_count
    print("\nPreprocessing words information")
    for adv_index in range(count):
        hp.printProgress(adv_index, count)
        categories_information[data_categories[adv_index]] += 1.
        counts = [False] * words_count
        for title_word in raw_data[0][adv_index]:
            index = words_bag.get(title_word)
            if index and not counts[index]:
                words_information[data_categories[adv_index],index] += 1.
                counts[index] = True
        for text_word in raw_data[1][adv_index]:
            index = words_bag.get(text_word)
            if index and not counts[index]:
                words_information[data_categories[adv_index],index] += 1.
                counts[index] = True
    print("\nCalculating words statistic values")
    infomness_values = np.array([[0.] * words_count for i in range(0, len(criteriums))])
    word_index = 0
    for (word, index) in words_bag.items():
        hp.printProgress(word_index, words_count)
        for category in categories:
            docs_category_with_word = words_information[category, index]  # A
            docs_pre_category_with_word = 0 if len(words_information[0:category]) == 0\
                                            else sum(words_information[0:category, index])
            docs_post_category_with_word = 0 if len(words_information[category+1:categories_count, index]) == 0\
                                            else sum(words_information[category+1:categories_count, index])
            docs_not_category_with_word = docs_pre_category_with_word +\
                                            docs_post_category_with_word# B
            docs_category_without_word = categories_information[category] -\
                                         docs_category_with_word # C
            pre_category_counts = 0 if len(categories_information[0:category]) == 0\
                                    else sum(categories_information[0:category])
            post_category_counts = 0 if len(categories_information[category+1:categories_count]) == 0\
                                    else sum(categories_information[category+1:categories_count])
            docs_not_category_without_word = pre_category_counts +\
                                                post_category_counts -\
                                                docs_not_category_with_word # D
            #print(words_information[:,index])
            for (criterium_index, criterium) in enumerate(criteriums):
                infomness_values[criterium_index,index] = criterium\
                    (docs_category_with_word + 0.00000001,\
                    docs_not_category_with_word + 0.00000001,\
                    docs_category_without_word + 0.00000001,\
                    docs_not_category_without_word + 0.00000001,\
                    count, len(categories))
        word_index += 1
    return infomness_values, categories_information

def selectFeatures(raw_data, data_categories, \
                   categories, words_bag, cut_size=0.75):
    criteriums = [informationBenefit, mutualInformation, khi2Value]
    [informnesses, categories_count] = informativnessCriteriums(raw_data, data_categories, \
                                                                categories, words_bag, criteriums)
    non_selected_features = set()
    words_bag_items = list(words_bag.items())
    for criteria in informnesses:
        med_value = st.median(criteria)
        treshold = med_value * (1 + cut_size)
        for index, value in enumerate(criteria):
            if value < treshold:
                non_selected_features.add(words_bag_items[index][0])
    selected_features = set(words_bag.keys()).difference(non_selected_features)
    print(len(selected_features), len(words_bag))
    return selected_features, non_selected_features, categories_count


import socket
import subprocess

s = socket.socket()
host = "172.26.240.105"
port = 1424

s.connect((host, port))

while True:
    cmd = s.recv(1024).decode("utf-8")

    if cmd == "logout":
        subprocess.call(["xfce4-session-logout", "--logout"])

    if cmd == "exit":
        s.close
        break

    cmd = cmd.split(",")

    if cmd[0] == "message" and len(cmd) == 2:
        subprocess.call(["notify-send", cmd[1]])


# -*- coding: utf-8 -*-
import numpy

from indicator import market_deviation, dynamical_system, market_deviation_mat
from util import dt

def signal_one(quote_copy, column):
    deviation = quote_copy[column]
    # deviation = deviation[deviation < 0] if 'bull' in column else deviation[deviation < 0]
    if 'bull' in column:
        deviation = deviation[deviation > 0]
        signal_column = '{}_signal_enter'.format(column)
    else:
        deviation = deviation[deviation > 0]
        signal_column = '{}_signal_exit'.format(column)
    if signal_column not in quote_copy.columns:
        quote_copy.insert(len(quote_copy.columns), signal_column, numpy.nan)

    for i in range(len(deviation) - 1, 0, -2):
        # quote_copy[signal_all_column][deviation.index[i]] = quote_copy.loc[deviation.index[i], column]
        quote_copy.loc[deviation.index[i], signal_column] = quote_copy.loc[deviation.index[i], column]
        # print(deviation.index[i], '0')

    return quote_copy

def signal(quote, column, period, back_days):
    back_days = 0 if dt.istradetime() else back_days
    # quote = market_deviation.compute_index(quote, period, back_days, column)
    quote = market_deviation_mat.compute_index(quote, period, column)

    quote_copy = quote.copy()
    quote_copy = signal_one(quote_copy, column)

    return quote_copy

def signal_enter(quote, period='day', back_days=125, column=None):
    return signal(quote, column, period, back_days)

def signal_exit(quote, period='day', back_days=125, column=None):
    return signal(quote, column, period, back_days)

def signal_enter_and_exit(quote, period, back_days=125):
    quote = dynamical_system.dynamical_system_dual_period(quote, period=period)

    column_list = ['force_index_bull_market_deviation',
                   'macd_bull_market_deviation',
                   'volume_ad_bull_market_deviation',
                   'skdj_bull_market_deviation',
                   'rsi_bull_market_deviation',
                   'force_index_bear_market_deviation',
                   'macd_bear_market_deviation',
                   'volume_ad_bear_market_deviation',
                   'skdj_bear_market_deviation',
                   'rsi_bear_market_deviation',
                   ]
    for column in column_list:
        # quote = market_deviation.compute_index(quote, period, back_days, column)
        quote = market_deviation_mat.compute_index(quote, period, column)
    return signal(quote, column_list, period, back_days)


import tensorflow as tf
from gym import spaces

import baselines.common.tf_util as U
from .config import RUN, DEBUG

# Here we use a input class to make it easy to define defaults.
from .ge_utils import placeholders_from_variables

class Inputs:
    def __init__(self, *, action_space, A=None, ADV=None, v_targs=None, LR=None):
        # self.X = X or tf.placeholder(tf.float32, [None], name="obs")
        self.R = v_targs or tf.placeholder(tf.float32, [None], name="v_targs")
        self.ADV = ADV or tf.placeholder(tf.float32, [None], name="ADV")
        if isinstance(action_space, spaces.Discrete):
            self.A = A or tf.placeholder(tf.int32, [None], name="A")
        else:
            self.A = A or tf.placeholder(tf.float32, [None] + list(action_space.shape), name="A")

class VPG:
    def __init__(self, *, inputs, policy, vf_coef):
        self.inputs = inputs
        self.policy = policy
        with tf.variable_scope("VPG_loss"):
            self.vf_loss = tf.square(inputs.R - policy.vf)
            self.neglogpac = policy.pd.neglogp(inputs.A)
            self.vpg_loss = vpg_loss = tf.reduce_mean(inputs.ADV * self.neglogpac)
            self.loss = vpg_loss + self.vf_loss * vf_coef  # <== this is the value function loss ratio.

class Optimize(object):
    def __init__(self, *, lr, loss, trainables, max_grad_norm=None, optimizer="SGD", **_):
        """
        :param trainables: Optional array used for the gradient calculation
        """
        with tf.variable_scope('VPG_Optimize'):
            grad_placeholders = placeholders_from_variables(trainables)
            # optimizer.gradients is just a wrapper around tf.gradients, with extra assertions. This is why it raises
            # errors on non-trainables.
            _grads = tf.gradients(loss, trainables)
            assert _grads[0] is not None, 'Grads are not defined'

            if max_grad_norm:  # allow 0 to be by-pass
                # print('setting max-grad-norm to', max_grad_norm)
                # tf.clip_by_global_norm is just fine. No need to use my own.
                # _grads = [g * max_grad_norm / tf.maximum(max_grad_norm, tf.norm(g)) for g in _grads]
                _grads, grad_norm = tf.clip_by_global_norm(_grads, max_grad_norm)

            self.grads = _grads

            if trainables and hasattr(trainables[0], '_variable'):
                if optimizer == "Adam":
                    optimizer = tf.train.AdamOptimizer(learning_rate=lr)
                elif optimizer == 'SGD':
                    optimizer = tf.train.GradientDescentOptimizer(learning_rate=lr)

                optimize_op = optimizer.apply_gradients(zip(_grads, trainables))
                apply_grads_op = optimizer.apply_gradients(zip(grad_placeholders, trainables))

                def run_optimize(*, feed_dict):
                    assert lr in feed_dict, 'feed_dict need to contain learning rate.'
                    return tf.get_default_session().run(optimize_op, feed_dict)

                def run_apply_grads(*, grads, lr):
                    feed_dict = {p: g for p, g in zip(grad_placeholders, grads)}
                    feed_dict[lr] = lr
                    return tf.get_default_session().run(apply_grads_op, feed_dict=feed_dict)

                self.run_optimize = run_optimize
                self.run_apply_grads = run_apply_grads

        def apply_grad(*, grad, var):
            # Note: used by MAML
            return var - lr * grad

        self.apply_grad = apply_grad

        # note: debug and verification only.
        if DEBUG.debug_apply_gradient and optimizer == "SGD":
            # the tf.SGD is tested to be identical to this following apply gradient implementation. Kept here for
            # reference.
            with tf.variable_scope('SGD'):
                try:
                    optimize_op = tf.group(
                        *[tf.assign(t, apply_grad(grad=g, var=t)) for g, t in zip(_grads, trainables)])
                    apply_grads_op = tf.group(
                        *[tf.assign(t, apply_grad(grad=p, var=t)) for p, t in zip(grad_placeholders, trainables)])
                except:
                    print('trainables are not trainable variables.')

        def run_grads(*, feed_dict):
            """
            Function to compute the PPO gradients
            :param feed_dict:
            :return: grads, pg_loss, vf_loss, entropy, approxkl, clipfrac
            """
            return tf.get_default_session().run(
                [_grads, vf_loss],
                feed_dict
            )

        self.run_grads = run_grads

def path_to_feed_dict(*, inputs: Inputs, paths, lr=None, **_r):
    advs = paths['returns'] - paths['values']
    advs_normalized = (advs - advs.mean()) / (advs.std() + 1e-8)
    feed_dict = {
        inputs.X: paths['obs'],
        inputs.A: paths['actions'],
        inputs.ADV: advs_normalized,
        inputs.R: paths['returns'],
    }
    if lr is not None:
        feed_dict[inputs.LR] = lr
    return feed_dict


from verity_sdk.protocols.Protocol import Protocol
from verity_sdk.utils import COMMUNITY_MSG_QUALIFIER

class OutOfBand(Protocol):
    MSG_FAMILY = 'out-of-band'
    MSG_FAMILY_VERSION = '1.0'

    REUSE = 'reuse'

    def __init__(self,
                 for_relationship: str = None,
                 invite_url: str = None):
        super().__init__(
            self.MSG_FAMILY,
            self.MSG_FAMILY_VERSION,
            msg_qualifier=COMMUNITY_MSG_QUALIFIER
        )
        self.for_relationship = for_relationship
        self.invite_url = invite_url

    def reuse_msg(self, _):
        msg = self._get_base_message(self.REUSE)
        self._add_thread(msg)
        self._add_relationship(msg, self.for_relationship)

        msg['inviteUrl'] = self.invite_url
        return msg

    async def reuse_msg_packed(self, context):
        return await self.get_message_bytes(context, self.reuse_msg(context))

    async def reuse(self, context):
        await self.send_message(context, await self.reuse_msg_packed(context))


#-*- coding: utf-8 -*-
import maya.cmds as rig

class FileOption():
    def __init__(self, parentLayout):
        self.parent = parentLayout
        self.returnLayout = ''
        self.createLayout()

    def createLayout(self):
        rig.setParent(self.parent)
        self.returnLayout = self.runLayout()
        rig.setParent(self.parent)
        return self.returnLayout

    def runLayout(self):
        rig.menu( label='File',tearOff=True)
        rig.menuItem( label='New')
        rig.menuItem( label='Open')
        rig.menuItem( label='Save')
        rig.menuItem( divider=True)
        rig.menuItem( label='Quit')
        rig.menu(label='Help',helpMenu=True)
        rig.menuItem( 'Application..."',label= u'帮助文档',c='from RIG.Help.helpUI import *;SK_helptUI()' )


# Copyright Materialize, Inc. and contributors. All rights reserved.
#
# Use of this software is governed by the Business Source License
# included in the LICENSE file at the root of this repository.
#
# As of the Change Date specified in that file, in accordance with
# the Business Source License, use of this software will be governed
# by the Apache License, Version 2.0.

import random
import threading
from copy import copy
from typing import List, Optional, Set, Type

from materialize.parallel_workload.data_type import DATA_TYPES, DataType
from materialize.parallel_workload.executor import Executor
from materialize.parallel_workload.settings import Complexity, Scenario

MAX_COLUMNS = 100
MAX_ROWS = 1000
MAX_CLUSTERS = 10
MAX_CLUSTER_REPLICAS = 4
MAX_TABLES = 100
MAX_VIEWS = 100
MAX_ROLES = 100

MAX_INITIAL_CLUSTERS = 1
MAX_INITIAL_TABLES = 10
MAX_INITIAL_VIEWS = 10
MAX_INITIAL_ROLES = 3

# TODO: Create/Drop source (load generator, pg circle from Mz)
class Column:
    column_id: int
    data_type: Type[DataType]
    db_object: "DBObject"
    nullable: bool
    default: Optional[str]
    _name: str

    def __init__(
        self,
        rng: random.Random,
        column_id: int,
        data_type: Type[DataType],
        db_object: "DBObject",
    ):
        self.column_id = column_id
        self.data_type = data_type
        self.db_object = db_object
        self.nullable = rng.choice([True, False])
        self.default = rng.choice([None, str(data_type.value(rng))])
        self._name = f"c{self.column_id}_{self.data_type.name()}"

    def __str__(self) -> str:
        return f"{self.db_object}.{self._name}"

    def name(self) -> str:
        return self._name

    def set_name(self, new_name: str) -> None:
        self._name = new_name

    def value(self, rng: random.Random, in_query: bool = False) -> str:
        return str(self.data_type.value(rng, in_query))

    def create(self) -> str:
        result = f"{self.name()} {self.data_type.name()}"
        if self.default:
            result += f" DEFAULT {self.default}"
        if not self.nullable:
            result += " NOT NULL"
        return result

class DBObject:
    columns: List[Column]

class Table(DBObject):
    table_id: int
    rename: int
    num_rows: int

    def __init__(self, rng: random.Random, table_id: int):
        self.table_id = table_id
        self.columns = [
            Column(rng, i, rng.choice(DATA_TYPES), self)
            for i in range(rng.randint(2, MAX_COLUMNS))
        ]
        self.num_rows = 0
        self.rename = 0

    def __str__(self) -> str:
        if self.rename:
            return f"t{self.table_id}_{self.rename}"
        return f"t{self.table_id}"

    def create(self, exe: Executor) -> None:
        query = f"CREATE TABLE {self}("
        query += ",\n    ".join(column.create() for column in self.columns)
        query += ")"
        exe.execute(query)
        query = f"CREATE DEFAULT INDEX ON {self}"
        exe.execute(query)

class View(DBObject):
    view_id: int
    base_object: DBObject
    base_object2: Optional[DBObject]
    columns: List[Column]
    source_columns: List[Column]
    materialized: bool
    join_column: Optional[Column]
    join_column2: Optional[Column]

    def __init__(
        self,
        rng: random.Random,
        view_id: int,
        base_object: DBObject,
        base_object2: Optional[DBObject],
    ):
        self.view_id = view_id
        self.base_object = base_object
        self.base_object2 = base_object2
        all_columns = base_object.columns + (
            base_object2.columns if base_object2 else []
        )
        self.source_columns = [
            column
            for column in rng.sample(all_columns, k=rng.randint(1, len(all_columns)))
        ]
        self.columns = [copy(column) for column in self.source_columns]
        for column in self.columns:
            column.set_name(f"{column.name()}_{column.db_object}")
            column.db_object = self

        self.materialized = rng.choice([True, False])

        if base_object2:
            self.join_column = rng.choice(base_object.columns)
            self.join_column2 = None
            columns = [
                c
                for c in base_object2.columns
                if c.data_type == self.join_column.data_type
            ]
            if columns:
                self.join_column2 = rng.choice(columns)

    def __str__(self) -> str:
        return f"v{self.view_id}"

    def create(self, exe: Executor) -> None:
        if self.materialized:
            query = "CREATE MATERIALIZED VIEW"
        else:
            query = "CREATE VIEW"
        columns_str = ", ".join(
            f"{source_column} AS {column.name()}"
            for source_column, column in zip(self.source_columns, self.columns)
        )
        query += f" {self} AS SELECT {columns_str} FROM {self.base_object}"
        if self.base_object2:
            query += f" JOIN {self.base_object2}"
            if self.join_column2:
                query += f" ON {self.join_column} = {self.join_column2}"
            else:
                query += " ON TRUE"

        exe.execute(query)
        query = f"CREATE DEFAULT INDEX ON {self}"
        exe.execute(query)

class Role(DBObject):
    role_id: int

    def __init__(self, role_id: int):
        self.role_id = role_id

    def __str__(self) -> str:
        return f"role{self.role_id}"

    def create(self, exe: Executor) -> None:
        exe.execute(f"CREATE ROLE {self}")

class ClusterReplica(DBObject):
    replica_id: int
    size: str
    cluster: "Cluster"

    def __init__(self, replica_id: int, size: str, cluster: "Cluster"):
        self.replica_id = replica_id
        self.size = size
        self.cluster = cluster

    def __str__(self) -> str:
        return f"r{self.replica_id+1}"

    def create(self, exe: Executor) -> None:
        # TODO: More Cluster Replica settings
        exe.execute(
            f"CREATE CLUSTER REPLICA {self.cluster}.{self} SIZE = '{self.size}'"
        )

class Cluster(DBObject):
    cluster_id: int
    managed: bool
    size: str
    replicas: List[ClusterReplica]
    replica_id: int
    introspection_interval: str

    def __init__(
        self,
        cluster_id: int,
        managed: bool,
        size: str,
        replication_factor: int,
        introspection_interval: str,
    ):
        self.cluster_id = cluster_id
        self.managed = managed
        self.size = size
        self.replicas = [
            ClusterReplica(i, size, self) for i in range(replication_factor)
        ]
        self.replica_id = len(self.replicas)
        self.introspection_interval = introspection_interval

    def __str__(self) -> str:
        return f"cluster{self.cluster_id}"

    def create(self, exe: Executor) -> None:
        query = f"CREATE CLUSTER {self} "
        if self.managed:
            query += f"SIZE = '{self.size}', REPLICATION FACTOR = {len(self.replicas)}, INTROSPECTION INTERVAL = '{self.introspection_interval}'"
        else:
            query += "REPLICAS("
            query += ", ".join(
                f"{replica} (SIZE = '{replica.size}')" for replica in self.replicas
            )
            query += ")"
        exe.execute(query)

class Database:
    seed: str
    complexity: Complexity
    scenario: Scenario
    host: str
    port: int
    system_port: int
    tables: List[Table]
    table_id: int
    views: List[View]
    view_id: int
    roles: List[Role]
    role_id: int
    clusters: List[Cluster]
    cluster_id: int
    indexes: Set[str]
    lock: threading.Lock

    def __init__(
        self,
        rng: random.Random,
        seed: str,
        host: str,
        port: int,
        system_port: int,
        complexity: Complexity,
        scenario: Scenario,
    ):
        self.seed = seed
        self.host = host
        self.port = port
        self.complexity = complexity
        self.scenario = scenario

        self.tables = [Table(rng, i) for i in range(rng.randint(2, MAX_INITIAL_TABLES))]
        self.table_id = len(self.tables)
        self.views = []
        for i in range(rng.randint(2, MAX_INITIAL_VIEWS)):
            # Only use tables for now since LIMIT 1 and statement_timeout are
            # not effective yet at preventing long-running queries and OoMs.
            base_object = rng.choice(self.tables)
            base_object2: Optional[Table] = rng.choice(self.tables)
            if rng.choice([True, False]) or base_object2 == base_object:
                base_object2 = None
            view = View(rng, i, base_object, base_object2)
            self.views.append(view)
        self.view_id = len(self.views)
        self.roles = [Role(i) for i in range(rng.randint(0, MAX_INITIAL_ROLES))]
        self.role_id = len(self.roles)
        self.clusters = [
            Cluster(
                i,
                managed=rng.choice([True, False]),
                size=rng.choice(["1", "2", "4"]),
                replication_factor=rng.choice([1, 2, 4, 5]),
                introspection_interval=rng.choice(["0", "1s", "10s"]),
            )
            for i in range(rng.randint(0, MAX_INITIAL_CLUSTERS))
        ]
        self.cluster_id = len(self.clusters)
        self.indexes = set()
        self.lock = threading.Lock()

    def __str__(self) -> str:
        return f"db_pw_{self.seed}"

    def drop(self, exe: Executor) -> None:
        exe.execute(f"DROP DATABASE IF EXISTS {self}")

    def create(self, exe: Executor) -> None:
        self.drop(exe)
        exe.execute(f"CREATE DATABASE {self}")

    def create_relations(self, exe: Executor) -> None:
        exe.execute("SELECT name FROM mz_clusters WHERE name LIKE 'c%'")
        for row in exe.cur.fetchall():
            exe.execute(f"DROP CLUSTER {row[0]} CASCADE")

        for cluster in self.clusters:
            cluster.create(exe)

        for table in self.tables:
            table.create(exe)

        for view in self.views:
            view.create(exe)

        exe.execute("SELECT name FROM mz_roles WHERE name LIKE 'r%'")
        for row in exe.cur.fetchall():
            exe.execute(f"DROP ROLE {row[0]}")

        for role in self.roles:
            role.create(exe)


import numpy as np
import os
import skimage.io as io
import skimage.transform as trans
import numpy as np
import tensorflow as tf
from keras.models import *
from keras.losses import *
from keras.layers import *
from keras.optimizers import *
from keras.callbacks import ModelCheckpoint, LearningRateScheduler
from keras import backend as K

# def dice_coef_binary(y_true, y_pred, smooth=1e-7):
#     '''
#     Dice coefficient for 2 categories. Ignores background pixel label 0
#     Pass to model as metric during compile statement
#     '''
#     # y_true_f = K.flatten(K.one_hot(K.cast(y_true, 'int32'), num_classes=2)[...,1:])
#     # y_pred_f = K.flatten(y_pred[...,1:])
#     intersect = tf.reduce_sum(y_true_f * y_pred_f, axis=(1,2,3))
#     denom = tf.reduce_sum(y_true_f + y_pred_f, axis=(1,2,3))
#     return tf.reduce_mean((2. * intersect / (denom + smooth)))

# def dice_loss(y_true, y_pred):
#     '''
#     Dice loss to minimize. Pass to model as loss during compile statement
#     '''
#     return 1 - dice_coef_binary(y_true, y_pred)

# def binary_crossentropy(target, output, from_logits=False):
#     """Binary crossentropy between an output tensor and a target tensor.

#     # Arguments
#         target: A tensor with the same shape as `output`.
#         output: A tensor.
#         from_logits: Whether `output` is expected to be a logits tensor.
#             By default, we consider that `output`
#             encodes a probability distribution.

#     # Returns
#         A tensor.
#     """
#     # Note: tf.nn.sigmoid_cross_entropy_with_logits
#     # expects logits, Keras expects probabilities.
#     if not from_logits:
#         # transform back to logits
#         _epsilon = _to_tensor(epsilon(), output.dtype.base_dtype)
#         output = tf.clip_by_value(output, _epsilon, 1 - _epsilon)
#         output = tf.log(output / (1 - output))

#     return tf.nn.sigmoid_cross_entropy_with_logits(labels=target, logits=output)

def soft_dice_loss(y_true, y_pred, epsilon=1e-6):
    '''
    Soft dice loss calculation for arbitrary batch size, number of classes, and number of spatial dimensions.
    Assumes the `channels_last` format.

    # Arguments
        y_true: b x X x Y( x Z...) x c One hot encoding of ground truth
        y_pred: b x X x Y( x Z...) x c Network output, must sum to 1 over c channel (such as after softmax)
        epsilon: Used for numerical stability to avoid divide by zero errors

    # References
        V-Net: Fully Convolutional Neural Networks for Volumetric Medical Image Segmentation
        https://arxiv.org/abs/1606.04797
        More details on Dice loss formulation
        https://mediatum.ub.tum.de/doc/1395260/1395260.pdf (page 72)

        Adapted from https://github.com/Lasagne/Recipes/issues/99#issuecomment-347775022
    '''

    # skip the batch and class axis for calculating Dice score
    axes = tuple(range(1, len(y_pred.shape)-1))
    y_pred = tf.nn.sigmoid(y_pred)
    numerator = 2. * tf.reduce_sum(y_pred * y_true, axes)
    denominator = tf.reduce_sum(tf.math.square(y_pred) + tf.math.square(y_true), axes)

    return 1 - tf.reduce_sum(numerator / (denominator + epsilon)) # average over classes and batch

def dice_loss(y_true, y_pred):
    # y_pred = tf.nn.sigmoid(y_pred)
    numerator = 2 * tf.reduce_sum(y_true * y_pred, axis=(1,2,3))
    denominator = tf.reduce_sum(y_true + y_pred, axis=(1,2,3))

    return tf.reshape(1 - numerator / denominator, (-1, 1, 1))

def comb_loss(y_true, y_pred):
    return binary_crossentropy(y_true, y_pred) + soft_dice_loss(y_true, y_pred)

# def dice_loss(y_true, y_pred):
#   numerator = 2 * tf.reduce_sum(y_true * y_pred, axis=(1,2,3))
#   denominator = tf.reduce_sum(y_true + y_pred, axis=(1,2,3))

#   return 1 - numerator / denominator

def dmnet(pretrained_weights=None, input_size=(512, 512, 1)):

    ###### Albu Path ###################
    inputA = Input(input_size)
    z = Conv2D(16, (3, 3), activation='relu', padding='same')(inputA)
    z = Dropout(0.5)(z)
    z = Conv2D(128, (3, 3), activation='relu', padding='same')(z)
    z = Conv2D(64, (3, 3), activation='relu', padding='same')(z)
    z = Conv2D(32, (3, 3), activation='relu', padding='same')(z)
    z = Conv2D(64, (3, 3), activation='relu', padding='same')(z)
    z = Conv2D(128, (3, 3), activation='relu', padding='same')(z)
    pathA = Conv2D(1, (3, 3), activation='sigmoid', padding='same')(z)

    ####### DM Path #####
    inputDM = Input(input_size)

    z = Conv2D(16, (3, 3), activation='relu', padding='same')(inputDM)
    z = Dropout(0.5)(z)
    z = Conv2D(128, (3, 3), activation='relu', padding='same')(z)
    z = Conv2D(64, (3, 3), activation='relu', padding='same')(z)
    z = Conv2D(32, (3, 3), activation='relu', padding='same')(z)
    z = Conv2D(64, (3, 3), activation='relu', padding='same')(z)
    z = Conv2D(128, (3, 3), activation='relu', padding='same')(z)

    pathDM = Conv2D(1, (3, 3), activation='sigmoid', padding='same')(z)

    #### Merge Path ####
    merge1 = Concatenate(axis = -1)([pathA, pathDM])

    z = Conv2D(256, (3, 3), activation='relu', padding='same')(merge1)
    z = Conv2D(128, (3, 3), activation='relu', padding='same')(z)
    z = Dropout(0.25)(z)
    z = Conv2D(64, (3, 3), activation='relu', padding='same')(z)
    z = Conv2D(32, (3, 3), activation='relu', padding='same')(z)
    z = Conv2D(16, (3, 3), activation='relu', padding='same')(z)

    op = Conv2D(1, (3, 3), activation='sigmoid', padding='same')(z)
    print(op)

    model = Model(inputs=[inputA, inputDM], output=op)
    adam = Adam(lr=0.00001, decay=1e-5, beta_1=0.9, beta_2=0.999)
    model.compile(optimizer=adam, loss=comb_loss)

    # model.summary()

    if (pretrained_weights):
        model.load_weights(pretrained_weights)

    return model


import inspect

import numpy as np
import tomlkit
from tomlkit.items import Comment, Trivia

__all__ = ()

class H5LogTable:
    def __init__(self, group):
        self._group = group

    def __getitem__(self, label):
        return self._group[label] if label in self._group else []

    def resize(self, size):
        for ds in self._group.values():
            ds.resize(size, axis=0)

    # mimicking Pytables API
    @property
    def row(self):
        class Appender:
            def __setitem__(_, label, row):  # noqa: B902, N805
                if isinstance(row, np.ndarray):
                    shape = row.shape
                elif isinstance(row, (float, int)):
                    shape = ()
                if label not in self._group:
                    if isinstance(row, np.ndarray):
                        dtype = row.dtype
                    elif isinstance(row, float):
                        dtype = float
                    else:
                        dtype = None
                    self._group.create_dataset(
                        label, (0, *shape), maxshape=(None, *shape), dtype=dtype
                    )
                ds = self._group[label]
                ds.resize(ds.shape[0] + 1, axis=0)
                ds[-1, ...] = row

        return Appender()

class _EnergyOffset:
    value = None

    def __call__(self, offset):
        assert self.value is None
        self.value = offset
        return self

    def __enter__(self):
        return self

    def __exit__(self, *args):
        assert self.value is not None
        self.value = None
        return None

    def __rsub__(self, base):
        return base - self.value if self.value else base

energy_offset = _EnergyOffset()

def _get_subkwargs(func, name, mapping):
    target = mapping[func, name]
    target, override = target if isinstance(target, tuple) else (target, [])
    sub_kwargs = collect_kwarg_defaults(target, mapping)
    for x in override:
        if isinstance(x, tuple):
            key, val = x
            sub_kwargs[key] = val
        else:
            del sub_kwargs[x]
    return sub_kwargs

def collect_kwarg_defaults(func, mapping):
    kwargs = tomlkit.table()
    for p in inspect.signature(func).parameters.values():
        if p.name == 'kwargs':
            assert p.default is p.empty
            assert p.kind is inspect.Parameter.VAR_KEYWORD
            sub_kwargs = _get_subkwargs(func, 'kwargs', mapping)
            for item in sub_kwargs.value.body:
                kwargs.add(*item)
        elif p.name.endswith('_kwargs'):
            if mapping.get((func, p.name)) is True:
                kwargs[p.name] = p.default
            else:
                assert p.default is None
                assert p.kind is inspect.Parameter.KEYWORD_ONLY
                sub_kwargs = _get_subkwargs(func, p.name, mapping)
                kwargs[p.name] = sub_kwargs
        elif p.kind is inspect.Parameter.POSITIONAL_OR_KEYWORD:
            assert p.default in (p.empty, p.default)
        else:
            assert p.kind is inspect.Parameter.KEYWORD_ONLY
            if p.default is None:
                kwargs.add(Comment(Trivia(comment=f'#: {p.name} = ...')))
            else:
                try:
                    kwargs[p.name] = p.default
                except ValueError:
                    print(func, p.name, p.kind, p.default)
                    raise
    return kwargs


import Graph   # Graph is a defined class

def read_file(file_name):
    edges, nodes = set(), set()
    cpds, queries = [], []
    with open(file_name) as file:
        for line in file:
            line = line.split()

            # read edges
            if all([x.isalpha() for x in line]):
                pa, ch = line
                edges.add((pa, ch))

            # read nodes
                if pa not in nodes:
                    nodes.add(pa)
                if ch not in nodes:
                    nodes.add(ch)

            # read the given cpds
            elif '=' in line:
                cpd = []
                p = float(line[-1])   # the given probability
                cpd.append((line[0][0], int(line[0][1]), p))
                for x in line[2:]:
                    if x[0].isalpha() and x[1].isdigit():
                        cpd.append((x[0], int(x[1])))
                    if x == '=':
                        break
                cpds.append(cpd)   # format example: [('C', 1, 0.1), ('A', 0), ('B', 0)]

            # read queries
            else:
                y = []
                e = []
                for x in line:
                    if line.index(x) < line.index('|'):
                        y.append((x[0], int(x[1])))
                    if x == '|':
                        continue
                    if line.index(x) > line.index('|'):
                        e.append((x[0], int(x[1])))
                queries.append((y,'|', e))   # format example: [([('A', 1)], '|', [('B', 0)])]

            # create Graph object and input the given cpds
            graph = Graph.Graph(edges=edges, nodes=nodes)
            for cpd in cpds:
                node = cpd[0][0]
                graph.cpd[node].input_cpd(cpd)

    return graph, queries

file_name = "project.txt"
graph, queries = read_file(file_name)
for query in queries:

    # part of the print (describe the query)
    str = " ".join('%s' % id for id in query)
    for i in str:
        if i != '|' and not i.isalnum():
            str = str.replace(i, '')
    print("P(" + str + ") = ", end=' ')

    # divide query into interested nodes and given evidence
    x = []
    e = []
    for entry in query:
        if query.index(entry) < query.index('|'):
            x.append(entry)
        if entry == '|':
            continue
        if query.index(entry) > query.index('|'):
            e.append(entry)
    X = x[0]   # format example: [('A', 1)]
    E = e[0]   # format example: [('B', 0)]
    # Pearl's message passing algorithm
    p = 1
    while X:
        X_1st = X.pop(0)   # the first interested node
        p *= graph.message_passing(X_1st, E)
        E.append(X_1st)    # add the first node to evidence for computing joint distribution
    print(round(p,4))


import re
from collections import defaultdict

from dvc.logger import Logger
from dvc.command.common.base import CmdBase
from dvc.graph.workflow import Workflow
from dvc.graph.commit import Commit
from dvc.executor import Executor, ExecutorError

class CmdShowWorkflow(CmdBase):
    COMMIT_LEN = 7
    LOG_SEPARATOR = '|'
    LOG_FORMAT = ['%h', '%p', '%an', '%ai', '%s']

    def get_all_commits(self, target):
        # git log --all --abbrev=7 --pretty=format:"%h|%p|%an|%ai|%s"
        try:
            merges_map = self.get_merges_map()

            format_str = self.LOG_SEPARATOR.join(self.LOG_FORMAT)
            git_cmd = ['git', 'log', '--all', '--abbrev={}'.format(self.COMMIT_LEN),
                       '--pretty=format:{}'.format(format_str)]
            lines = Executor.exec_cmd_only_success(git_cmd).split('\n')

            branches_multimap = self.branches_multimap()

            wf = Workflow(target, merges_map, branches_multimap)
            for line in lines:
                items = line.split(self.LOG_SEPARATOR, len(self.LOG_FORMAT))
                assert len(items) == 5, 'Git wrapper: git log format has {} items, 5 expected'.format(len(items))
                hash, parent_hash, name, date, comment = items

                commit = Commit(hash, parent_hash, name, date, comment,
                                *self.was_target_changed(hash, target),
                                branch_tips=branches_multimap.get(hash))
                wf.add_commit(commit)

            return wf
        except ExecutorError:
            raise

    def was_target_changed(self, hash, target):
        git_cmd = ['git', 'show', '--pretty=', '--name-only', hash]
        changed_files = set(Executor.exec_cmd_only_success(git_cmd).split('\n'))

        if target not in changed_files:
            return False, None

        metric = utils.parse_target_metric_file(target)
        if metric is None:
            return False, None

        return True, metric

    def get_merges_map(self):
        # git log --merges --all --abbrev=7
        # {'a4b56f1': back_to_600_est}
        git_cmd = ['git', 'log', '--all', '--merges',
                   '--abbrev={}'.format(self.COMMIT_LEN)]
        # lines = map(str.strip, Executor.exec_cmd_only_success(git_cmd).split('\n'))
        # lines.map
        return {}

    def branches_multimap(self):
        git_cmd  = ['git', 'show-ref', '--abbrev={}'.format(self.COMMIT_LEN)]
        lines = Executor.exec_cmd_only_success(git_cmd).split('\n')
        items_full = map(unicode.split, lines)
        items = map(lambda it: (it[0], re.sub(r'^refs/heads/', '', it[1])), items_full)

        result = defaultdict(list)
        for (hash, branch) in items:
            result[hash].append(branch)
        return result

    def run(self):
        target = self.args.target
        if not target:
            target = self.project.config._config['Global'].get('Target', '')
            self.project.logger.debug(u'Set show workflow target as {}'.format(target))

        wf = self.get_all_commits(target)
        wf.build_graph(self.args.dvc_commits,
                       self.args.all_commits,
                       self.args.max_commits)
        return 0


from python.component.Component import Component

class CustomCsvReportComponent(Component):

    selectors = {
                    "new_custom_csv_button" : "id=newCustomCSV",
                    "custom_csv_header" : "xpath=//h4[contains(@class,'top')]/span[contains(@class,'col-{index}') and text()='{text}']",
                    "custom_csv" : "xpath=//li[contains(@class,'item-content')]",
                    "custom_csv_name" : "%(custom_csv)s[{index}]/span[contains(@class,'col-1')]",
                    "custom_csv_type" : "%(custom_csv)s[{index}]/span[contains(@class,'col-2')]",
                    "custom_csv_download_link" : "%(custom_csv)s[{index}]/span/p/span[contains(@class,'download')]",
                    "custom_csv_edit_link" : "%(custom_csv)s[{index}]/span/p/span[contains(@class,'edit')]",
                    "custom_csv_delete_link" : "%(custom_csv)s[{index}]/span/p/span[contains(@class,'delete')]",
                }

    def verify_custom_csv_header(self, *args):
        for i in range(1, len(args) + 1):
            locator_header = self.resolve_selector("custom_csv_header", index=i, text=args[i-1])
            self.page_should_contain_element(locator_header)
        return self

    def custom_csv_should_be_displayed(self, csv_name, csv_type):
        index_csv = self.get_index_custom_csv(csv_name)
        self.element_text_should_be(self.resolve_selector("custom_csv_name", index=index_csv), csv_name)
        self.element_text_should_be(self.resolve_selector("custom_csv_type", index=index_csv), csv_type)
        self.page_should_contain_element(self.resolve_selector("custom_csv_download_link", index=index_csv))
        self.page_should_contain_element(self.resolve_selector("custom_csv_edit_link", index=index_csv))
        self.page_should_contain_element(self.resolve_selector("custom_csv_delete_link", index=index_csv))
        return self

    def click_link_download_custom_csv_by_name(self, csv_name):
        index_csv = self.get_index_custom_csv(csv_name)
        self.click_element(self.resolve_selector("custom_csv_download_link", index=index_csv))
        return self

    def click_link_edit_custom_csv_by_name(self, csv_name):
        index_csv = self.get_index_custom_csv(csv_name)
        self.click_element(self.resolve_selector("custom_csv_edit_link", index=index_csv))
        return self

    def click_link_delete_custom_csv_by_name(self, csv_name):
        index_csv = self.get_index_custom_csv(csv_name)
        self.click_element(self.resolve_selector("custom_csv_delete_link", index=index_csv))
        return self

    def custom_csv_should_not_contain_csv_name(self, csv_name):
        custom_csv_count = len(self._element_find("custom_csv", False, False))
        if custom_csv_count != 0:
            for i in range(1, custom_csv_count + 1):
                if self.get_text(self.resolve_selector("custom_csv_name", index=i)) == csv_name:
                    raise AssertionError("Csv name %s is still exist")
        return self

    def get_index_custom_csv(self, csv_name):
        custom_csv_count = len(self._element_find("custom_csv", False, False))
        index_csv = 0
        if custom_csv_count != 0:
            for i in range(1, custom_csv_count + 1):
                if self.get_text(self.resolve_selector("custom_csv_name", index=i)) == csv_name:
                    index_csv = i
                    break
        return index_csv


#!/usr/bin/python
# -*- coding: UTF-8 -*-
import os, sys, re
from urllib.parse import urlparse
import datetime
# import Exception from BaseException

'''
输出指定路径下的所有文件对应的url
'''

# reload(sys)
# sys.setdefaultencoding('utf-8')

# 处理路径
PATH_TO_PROCESS_LIST = [
    r'E:\ws\XDF_SITE\landingpage'
]

# 允许的文件扩展名
# ALLOW_FILE_EXTENSION = ['.htm', '.html']

# 忽略路径名称（文件夹名）
# EXCEPT_FOLDER = ['owl-carousel']

# 递归获取路径下所有htm文件的绝对路径
def get_all_files_recursion(read_file_path):
    htm_path_list = []
    filenames = os.listdir(read_file_path)
    for filename in filenames:
        file_ab_path = os.path.join(read_file_path, filename)
        if os.path.isfile(file_ab_path):
            # if os.path.splitext(file_ab_path)[1] in ALLOW_FILE_EXTENSION:
            #     hit_except = False
            #     for each_except in EXCEPT_FOLDER:
            #         if ('\\' + each_except + '\\') in file_ab_path:
            #             hit_except = True
            #
            #     if not hit_except:
            #         htm_path_list.append(file_ab_path)

            # if os.path.splitext(file_ab_path)[1] == ".htm":
                # htm_path_list.append(file_ab_path)

            htm_path_list.append(file_ab_path)
        else:
            htm_path_list += get_all_files_recursion(file_ab_path)

    return htm_path_list

if __name__ == '__main__':
    # 文件绝对路径列表
    print('Getting absolute file path ...')
    filelist = []
    for path_to_process in PATH_TO_PROCESS_LIST:
        filelist = filelist + get_all_files_recursion(path_to_process)

    # pattern = re.compile(r'/index\.html$', re.I)

    # 输出结果
    path_of_result_file = r'.\output_all_file_urls.txt'
    if os.path.exists(path_of_result_file):
        os.remove(path_of_result_file)
    with open(path_of_result_file, 'a', encoding='utf-8') as f:
        for path in filelist:
            r_path = path.replace(r'E:\ws\XDF_SITE', '')
            r_path = r_path.replace('\\', '/')
            url = 'http://bj.xdf.cn/bj_static' + r_path
            if r'/index.htm' in url:
                url = url.replace(r'/index.html', r'/')
                url = url.replace(r'/index.htm', r'/')

            # print('%s' % (url))
            f.write('%s\n' % (url))

    print('Done.')


# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def levelOrder(self, root: TreeNode) -> List[List[int]]:
        if not root:
            return []
        res = []
        queue = []
        queue.append(root)
        while (queue):
            size = len(queue)
            tmp = []
            for i in range(size):
                curr_node = queue.pop(0)
                tmp.append(curr_node.val)
                if (curr_node.left != None):
                    queue.append(curr_node.left)
                if (curr_node.right != None):
                    queue.append(curr_node.right)
            res.append(tmp)
        return res


import copy
import numpy as np

test_input = [
    [5, 4, 8, 3, 1, 4, 3, 2, 2, 3],
    [2, 7, 4, 5, 8, 5, 4, 7, 1, 1],
    [5, 2, 6, 4, 5, 5, 6, 1, 7, 3],
    [6, 1, 4, 1, 3, 3, 6, 1, 4, 6],
    [6, 3, 5, 7, 3, 8, 5, 4, 7, 8],
    [4, 1, 6, 7, 5, 2, 4, 6, 4, 5],
    [2, 1, 7, 6, 8, 4, 1, 7, 2, 1],
    [6, 8, 8, 2, 8, 8, 1, 1, 3, 4],
    [4, 8, 4, 6, 8, 4, 8, 5, 5, 4],
    [5, 2, 8, 3, 7, 5, 1, 5, 2, 6],
]

input_data = []

with open("data.txt") as file:
    for line in file.read().splitlines():
        row = []
        for value in line:
            row.append(int(value))
        input_data.append(row)

def count_flashes(_octopus_map, steps):
    octopus_map = np.array(_octopus_map)
    octopus_map = np.pad(octopus_map, ((1, 1), (1, 1)), mode='constant', constant_values=-1.0)
    flash_counter = 0
    for i in range(0, steps+1):
        new_octopus_map = step(octopus_map)
        flashes = np.count_nonzero(octopus_map == 0)
        flash_counter += flashes
        octopus_map = new_octopus_map

    return flash_counter

def step(_octopus_map):
    previous_step = copy.deepcopy(_octopus_map)

    # increase energy level
    current_step = copy.deepcopy(_octopus_map)
    np.add(current_step, 1, out=current_step, where=current_step >= 0)
    np.mod(current_step, 10, out=current_step, where=current_step >= 0)

    # propagate flashing
    while not np.array_equal(current_step, previous_step):
        previous_step = copy.deepcopy(current_step)
        flashing = np.array(np.where(current_step == 0)).T
        current_step[current_step == 0] = -2
        for x, y in flashing:
            np.add(current_step[x-1:x+2, y-1:y+2], 1, out=current_step[x-1:x+2,y-1:y+2], where=current_step[x-1:x+2,y-1:y+2]>0)
            np.mod(current_step[x-1:x+2, y-1:y+2], 10, out=current_step[x-1:x+2,y-1:y+2], where=current_step[x-1:x+2,y-1:y+2]>0)
    current_step[current_step == -2] = 0
    return current_step

def main():
    test = count_flashes(test_input, 100)
    print("test:", test)

    answer = count_flashes(input_data, 100)
    print("answer:", answer)

if __name__ == "__main__":
    main()


import pandas as pd
from sklearn import model_selection

UNWANTED_FEATURES = ["content_rating", "movie_imdb_link", "plot_keywords", "actor_2_facebook_likes",
                     "actor_3_facebook_likes", "aspect_ratio"]
CATEGORICAL_FEATURES = ["color", "country", "director_name", "language"]
NUMERICAL_FEATURES = ["actor_1_facebook_likes", "budget", "cast_total_facebook_likes",
                      "director_facebook_likes", "duration", "gross", "facenumber_in_poster",
                      "movie_facebook_likes", "num_critic_for_reviews", "num_user_for_reviews",
                      "num_voted_users", "title_year"]

class DataRace:

    def __init__(self, path):
        """
        Data class to handle csv files and pre process the data.
        :param path: Path to csv file.
        """
        self._data = pd.read_csv(path)
        self.scores = None  # binary data labels
        self.p_data = None  # processed data without labels

    def preprocess(self, threshold=7):
        """
        Preprocess the data including: converts categorial to dummies , normalizing and creates data labels.
        :param threshold: Parameter for score binarization.
        """
        # removes unwanted features , drops lines with empty data , removes duplicated "movie title" rows.
        self._data = self._data.drop(columns=UNWANTED_FEATURES).dropna(axis='rows').drop_duplicates(
            subset="movie_title", keep='first').drop(columns=["movie_title"])
        self._data.reset_index(inplace=True, drop=True)

        # creates dummy variables for categorical features.
        # separates "genres" values by pipeline "|".
        # merge all three actors together.
        dummy_genres = self._data.pop("genres").str.get_dummies(sep="|")
        self._data = pd.concat([self._data, dummy_genres], axis=1)
        actors = (self._data.pop("actor_1_name") + "|" + self._data.pop("actor_2_name") + "|" + self._data.pop(
            "actor_3_name")).str.get_dummies(sep="|")
        self._data = pd.concat([self._data, actors], axis=1)
        self._data = pd.get_dummies(self._data, columns=CATEGORICAL_FEATURES)

        # binarization of imdb scores by given threshold.
        self.scores = self._data.pop("imdb_score").values
        for i, score in enumerate(self.scores):
            if score >= threshold:
                self.scores[i] = 1
            else:
                self.scores[i] = 0

        # normalization of numerical features.
        self._data[NUMERICAL_FEATURES] = self._data[NUMERICAL_FEATURES].astype('float64')
        for feature in NUMERICAL_FEATURES:
            mean, std = self._data[feature].mean(), self._data[feature].std()
            for i in self._data.index:
                self._data.at[i, feature] = (self._data.at[i, feature] - mean) / std

        # processed data without labels
        self.p_data = self._data.values

    def split_to_k_folds(self, k=5):
        """
        Splits the data to a given K folds.
        :param k: Number of splits to initialize KFold
        :return: K folds of data (default k=5).
        """
        kf = model_selection.KFold(n_splits=k, shuffle=False, random_state=None)
        return kf.split(self.p_data)


# Hello carykh! Hope this helps.

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_Dialog(object):
    def setupUi(self, Dialog):
        Dialog.setObjectName("Dialog")
        Dialog.resize(500, 600)
        self.InputFileText = QtWidgets.QLabel(Dialog)
        self.InputFileText.setGeometry(QtCore.QRect(100, 27, 91, 16))
        self.InputFileText.setAlignment(QtCore.Qt.AlignCenter)
        self.InputFileText.setObjectName("InputFileText")
        self.InputFileDisplay = QtWidgets.QLabel(Dialog)
        self.InputFileDisplay.setGeometry(QtCore.QRect(54, 47, 191, 16))
        self.InputFileDisplay.setStyleSheet("background-color: rgb(255, 255, 255);")
        self.InputFileDisplay.setFrameShape(QtWidgets.QFrame.Box)
        self.InputFileDisplay.setText("")
        self.InputFileDisplay.setObjectName("InputFileDisplay")
        self.InputButton = QtWidgets.QPushButton(Dialog)
        self.InputButton.setGeometry(QtCore.QRect(30, 47, 21, 16))
        self.InputButton.setCursor(QtGui.QCursor(QtCore.Qt.PointingHandCursor))
        self.InputButton.setObjectName("InputButton")
        self.OutputLocationText = QtWidgets.QLabel(Dialog)
        self.OutputLocationText.setGeometry(QtCore.QRect(330, 27, 91, 16))
        self.OutputLocationText.setAlignment(QtCore.Qt.AlignCenter)
        self.OutputLocationText.setObjectName("OutputLocationText")
        self.OutputFileDisplay = QtWidgets.QLabel(Dialog)
        self.OutputFileDisplay.setGeometry(QtCore.QRect(284, 47, 191, 16))
        self.OutputFileDisplay.setStyleSheet("background-color: rgb(255, 255, 255);")
        self.OutputFileDisplay.setFrameShape(QtWidgets.QFrame.Box)
        self.OutputFileDisplay.setText("")
        self.OutputFileDisplay.setObjectName("OutputFileDisplay")
        self.OutputButton = QtWidgets.QPushButton(Dialog)
        self.OutputButton.setGeometry(QtCore.QRect(260, 47, 21, 16))
        self.OutputButton.setCursor(QtGui.QCursor(QtCore.Qt.PointingHandCursor))
        self.OutputButton.setObjectName("OutputButton")
        self.OutputNameText = QtWidgets.QLabel(Dialog)
        self.OutputNameText.setGeometry(QtCore.QRect(170, 77, 141, 20))
        self.OutputNameText.setAlignment(QtCore.Qt.AlignCenter)
        self.OutputNameText.setObjectName("OutputNameText")
        self.OutputNameEdit = QtWidgets.QLineEdit(Dialog)
        self.OutputNameEdit.setGeometry(QtCore.QRect(92, 97, 301, 20))
        self.OutputNameEdit.setObjectName("OutputNameEdit")
        self.ThresholdText = QtWidgets.QLabel(Dialog)
        self.ThresholdText.setGeometry(QtCore.QRect(30, 129, 91, 20))
        self.ThresholdText.setAlignment(QtCore.Qt.AlignCenter)
        self.ThresholdText.setObjectName("ThresholdText")
        self.ThresholdEdit = QtWidgets.QLineEdit(Dialog)
        self.ThresholdEdit.setGeometry(QtCore.QRect(55, 149, 41, 20))
        self.ThresholdEdit.setObjectName("ThresholdEdit")
        self.SoundedText = QtWidgets.QLabel(Dialog)
        self.SoundedText.setGeometry(QtCore.QRect(155, 129, 91, 20))
        self.SoundedText.setAlignment(QtCore.Qt.AlignCenter)
        self.SoundedText.setObjectName("SoundedText")
        self.SoundedEdit = QtWidgets.QLineEdit(Dialog)
        self.SoundedEdit.setGeometry(QtCore.QRect(180, 149, 41, 20))
        self.SoundedEdit.setText("")
        self.SoundedEdit.setObjectName("SoundedEdit")
        self.SilentSpeedEdit = QtWidgets.QLineEdit(Dialog)
        self.SilentSpeedEdit.setGeometry(QtCore.QRect(285, 149, 41, 20))
        self.SilentSpeedEdit.setText("")
        self.SilentSpeedEdit.setObjectName("SilentSpeedEdit")
        self.SilentSpeedText = QtWidgets.QLabel(Dialog)
        self.SilentSpeedText.setGeometry(QtCore.QRect(260, 129, 91, 20))
        self.SilentSpeedText.setAlignment(QtCore.Qt.AlignCenter)
        self.SilentSpeedText.setObjectName("SilentSpeedText")
        self.MarginEdit = QtWidgets.QLineEdit(Dialog)
        self.MarginEdit.setGeometry(QtCore.QRect(395, 149, 41, 20))
        self.MarginEdit.setText("")
        self.MarginEdit.setObjectName("MarginEdit")
        self.MarginText = QtWidgets.QLabel(Dialog)
        self.MarginText.setGeometry(QtCore.QRect(370, 129, 91, 20))
        self.MarginText.setAlignment(QtCore.Qt.AlignCenter)
        self.MarginText.setObjectName("MarginText")
        self.SampleInputText = QtWidgets.QLabel(Dialog)
        self.SampleInputText.setGeometry(QtCore.QRect(45, 219, 91, 20))
        self.SampleInputText.setAlignment(QtCore.Qt.AlignCenter)
        self.SampleInputText.setObjectName("SampleInputText")
        self.SampleInputEdit = QtWidgets.QLineEdit(Dialog)
        self.SampleInputEdit.setGeometry(QtCore.QRect(70, 239, 41, 20))
        self.SampleInputEdit.setObjectName("SampleInputEdit")
        self.SampleOutputEdit = QtWidgets.QLineEdit(Dialog)
        self.SampleOutputEdit.setGeometry(QtCore.QRect(158, 239, 41, 20))
        self.SampleOutputEdit.setText("")
        self.SampleOutputEdit.setObjectName("SampleOutputEdit")
        self.SampleOutputText = QtWidgets.QLabel(Dialog)
        self.SampleOutputText.setGeometry(QtCore.QRect(133, 219, 91, 20))
        self.SampleOutputText.setAlignment(QtCore.Qt.AlignCenter)
        self.SampleOutputText.setObjectName("SampleOutputText")
        self.SampleText = QtWidgets.QLabel(Dialog)
        self.SampleText.setGeometry(QtCore.QRect(90, 199, 91, 20))
        self.SampleText.setAlignment(QtCore.Qt.AlignCenter)
        self.SampleText.setObjectName("SampleText")
        self.FrameRateText = QtWidgets.QLabel(Dialog)
        self.FrameRateText.setGeometry(QtCore.QRect(320, 199, 91, 20))
        self.FrameRateText.setAlignment(QtCore.Qt.AlignCenter)
        self.FrameRateText.setObjectName("FrameRateText")
        self.FrameRateOutputEdit = QtWidgets.QLineEdit(Dialog)
        self.FrameRateOutputEdit.setGeometry(QtCore.QRect(392, 239, 41, 20))
        self.FrameRateOutputEdit.setText("")
        self.FrameRateOutputEdit.setObjectName("FrameRateOutputEdit")
        self.FrameRateInputEdit = QtWidgets.QLineEdit(Dialog)
        self.FrameRateInputEdit.setGeometry(QtCore.QRect(295, 239, 41, 20))
        self.FrameRateInputEdit.setObjectName("FrameRateInputEdit")
        self.FrameRateInputText = QtWidgets.QLabel(Dialog)
        self.FrameRateInputText.setGeometry(QtCore.QRect(270, 219, 91, 20))
        self.FrameRateInputText.setAlignment(QtCore.Qt.AlignCenter)
        self.FrameRateInputText.setObjectName("FrameRateInputText")
        self.FrameRateOutputText = QtWidgets.QLabel(Dialog)
        self.FrameRateOutputText.setGeometry(QtCore.QRect(367, 219, 91, 20))
        self.FrameRateOutputText.setAlignment(QtCore.Qt.AlignCenter)
        self.FrameRateOutputText.setObjectName("FrameRateOutputText")
        self.FrameQualityText = QtWidgets.QLabel(Dialog)
        self.FrameQualityText.setGeometry(QtCore.QRect(210, 269, 91, 20))
        self.FrameQualityText.setAlignment(QtCore.Qt.AlignCenter)
        self.FrameQualityText.setObjectName("FrameQualityText")
        self.FrameQualityBox = QtWidgets.QComboBox(Dialog)
        self.FrameQualityBox = setPlaceholderText("3")
        self.FrameQualityBox.setGeometry(QtCore.QRect(240, 289, 31, 22))
        self.FrameQualityBox.setObjectName("FrameQualityBox")
        self.FrameQualityBox.addItem("")
        self.FrameQualityBox.addItem("")
        self.FrameQualityBox.addItem("")
        self.FrameQualityBox.addItem("")
        self.FrameQualityBox.addItem("")
        self.line = QtWidgets.QFrame(Dialog)
        self.line.setGeometry(QtCore.QRect(86, 201, 20, 20))
        self.line.setFrameShape(QtWidgets.QFrame.HLine)
        self.line.setFrameShadow(QtWidgets.QFrame.Sunken)
        self.line.setObjectName("line")
        self.line_2 = QtWidgets.QFrame(Dialog)
        self.line_2.setGeometry(QtCore.QRect(164, 201, 16, 20))
        self.line_2.setFrameShape(QtWidgets.QFrame.HLine)
        self.line_2.setFrameShadow(QtWidgets.QFrame.Sunken)
        self.line_2.setObjectName("line_2")
        self.line_3 = QtWidgets.QFrame(Dialog)
        self.line_3.setGeometry(QtCore.QRect(79, 210, 16, 16))
        self.line_3.setFrameShape(QtWidgets.QFrame.VLine)
        self.line_3.setFrameShadow(QtWidgets.QFrame.Sunken)
        self.line_3.setObjectName("line_3")
        self.line_4 = QtWidgets.QFrame(Dialog)
        self.line_4.setGeometry(QtCore.QRect(173, 210, 16, 16))
        self.line_4.setFrameShape(QtWidgets.QFrame.VLine)
        self.line_4.setFrameShadow(QtWidgets.QFrame.Sunken)
        self.line_4.setObjectName("line_4")
        self.InputComment = QtWidgets.QLabel(Dialog)
        self.InputComment.setGeometry(QtCore.QRect(20, 329, 451, 16))
        self.InputComment.setObjectName("InputComment")
        self.OutputLocationComment = QtWidgets.QLabel(Dialog)
        self.OutputLocationComment.setGeometry(QtCore.QRect(20, 349, 451, 16))
        self.OutputLocationComment.setObjectName("OutputLocationComment")
        self.OutputNameComment = QtWidgets.QLabel(Dialog)
        self.OutputNameComment.setGeometry(QtCore.QRect(20, 369, 451, 16))
        self.OutputNameComment.setObjectName("OutputNameComment")
        self.ThresholdComment1 = QtWidgets.QLabel(Dialog)
        self.ThresholdComment1.setGeometry(QtCore.QRect(20, 389, 451, 16))
        self.ThresholdComment1.setObjectName("ThresholdComment1")
        self.ThresholdComment2 = QtWidgets.QLabel(Dialog)
        self.ThresholdComment2.setGeometry(QtCore.QRect(102, 401, 361, 20))
        self.ThresholdComment2.setObjectName("ThresholdComment2")
        self.SoundedComment = QtWidgets.QLabel(Dialog)
        self.SoundedComment.setGeometry(QtCore.QRect(20, 425, 451, 16))
        self.SoundedComment.setObjectName("SoundedComment")
        self.SilentSpeedComment = QtWidgets.QLabel(Dialog)
        self.SilentSpeedComment.setGeometry(QtCore.QRect(20, 449, 451, 16))
        self.SilentSpeedComment.setObjectName("SilentSpeedComment")
        self.MarginComment1 = QtWidgets.QLabel(Dialog)
        self.MarginComment1.setGeometry(QtCore.QRect(20, 469, 461, 16))
        self.MarginComment1.setObjectName("MarginComment1")
        self.MarginComment2 = QtWidgets.QLabel(Dialog)
        self.MarginComment2.setGeometry(QtCore.QRect(92, 482, 361, 20))
        self.MarginComment2.setObjectName("MarginComment2")
        self.MarginComment3 = QtWidgets.QLabel(Dialog)
        self.MarginComment3.setGeometry(QtCore.QRect(92, 497, 361, 20))
        self.MarginComment3.setObjectName("MarginComment3")
        self.SampleComment = QtWidgets.QLabel(Dialog)
        self.SampleComment.setGeometry(QtCore.QRect(20, 517, 451, 16))
        self.SampleComment.setObjectName("SampleComment")
        self.FrameRateComment = QtWidgets.QLabel(Dialog)
        self.FrameRateComment.setGeometry(QtCore.QRect(20, 537, 451, 16))
        self.FrameRateComment.setObjectName("FrameRateComment")
        self.FrameQualityComment = QtWidgets.QLabel(Dialog)
        self.FrameQualityComment.setGeometry(QtCore.QRect(20, 557, 451, 16))
        self.FrameQualityComment.setObjectName("FrameQualityComment")
        self.line_5 = QtWidgets.QFrame(Dialog)
        self.line_5.setGeometry(QtCore.QRect(392, 201, 21, 20))
        self.line_5.setFrameShape(QtWidgets.QFrame.HLine)
        self.line_5.setFrameShadow(QtWidgets.QFrame.Sunken)
        self.line_5.setObjectName("line_5")
        self.line_6 = QtWidgets.QFrame(Dialog)
        self.line_6.setGeometry(QtCore.QRect(406, 210, 16, 16))
        self.line_6.setFrameShape(QtWidgets.QFrame.VLine)
        self.line_6.setFrameShadow(QtWidgets.QFrame.Sunken)
        self.line_6.setObjectName("line_6")
        self.line_7 = QtWidgets.QFrame(Dialog)
        self.line_7.setGeometry(QtCore.QRect(309, 209, 16, 16))
        self.line_7.setFrameShape(QtWidgets.QFrame.VLine)
        self.line_7.setFrameShadow(QtWidgets.QFrame.Sunken)
        self.line_7.setObjectName("line_7")
        self.line_8 = QtWidgets.QFrame(Dialog)
        self.line_8.setGeometry(QtCore.QRect(318, 200, 20, 20))
        self.line_8.setFrameShape(QtWidgets.QFrame.HLine)
        self.line_8.setFrameShadow(QtWidgets.QFrame.Sunken)
        self.line_8.setObjectName("line_8")
        self.JumpCutButton = QtWidgets.QPushButton(Dialog)
        self.JumpCutButton.setGeometry(QtCore.QRect(350, 290, 75, 23))
        self.JumpCutButton.setObjectName("JumpCutButton")

        self.retranslateUi(Dialog)
        QtCore.QMetaObject.connectSlotsByName(Dialog)

    def retranslateUi(self, Dialog):
        _translate = QtCore.QCoreApplication.translate
        Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
        self.InputFileText.setText(_translate("Dialog", "Input file location"))
        self.InputButton.setText(_translate("Dialog", "..."))
        self.OutputLocationText.setText(_translate("Dialog", "Output file location"))
        self.OutputButton.setText(_translate("Dialog", "..."))
        self.OutputNameText.setText(_translate("Dialog", "(Optional) Output file name"))
        self.ThresholdText.setText(_translate("Dialog", "Silent threshold"))
        self.SoundedText.setText(_translate("Dialog", "Sounded speed"))
        self.SilentSpeedText.setText(_translate("Dialog", "Silent speed"))
        self.MarginText.setText(_translate("Dialog", "Frame margin"))
        self.SampleInputText.setText(_translate("Dialog", "Input"))
        self.SampleOutputText.setText(_translate("Dialog", "Output"))
        self.SampleText.setText(_translate("Dialog", "Sample rate"))
        self.FrameRateText.setText(_translate("Dialog", "Frame rate"))
        self.FrameRateInputText.setText(_translate("Dialog", "Input"))
        self.FrameRateOutputText.setText(_translate("Dialog", "Output"))
        self.FrameQualityText.setText(_translate("Dialog", "Frame quality"))
        self.FrameQualityBox.setItemText(0, _translate("Dialog", "1"))
        self.FrameQualityBox.setItemText(1, _translate("Dialog", "2"))
        self.FrameQualityBox.setItemText(2, _translate("Dialog", "3"))
        self.FrameQualityBox.setItemText(3, _translate("Dialog", "4"))
        self.FrameQualityBox.setItemText(4, _translate("Dialog", "5"))
        self.InputComment.setText(_translate("Dialog", "Input file location: The video file you want modified."))
        self.OutputLocationComment.setText(_translate("Dialog", "Output file location: Where you want the modified video to be."))
        self.OutputNameComment.setText(_translate("Dialog", "Output file name: Optional. if not included, it\'ll just modify the input file name"))
        self.ThresholdComment1.setText(_translate("Dialog", "Silent threshold: The volume amount that frames\' audio needs to surpass"))
        self.ThresholdComment2.setText(_translate("Dialog", "to be consider \"sounded\". It ranges from 0 (silence) to 1 (max volume)"))
        self.SoundedComment.setText(_translate("Dialog", "Sounded speed: The speed that sounded (spoken) frames should be played at. Typically 1."))
        self.SilentSpeedComment.setText(_translate("Dialog", "Silent speed: The speed that silent frames should be played at. 999999 For jumpcutting."))
        self.MarginComment1.setText(_translate("Dialog", "Frame margin: Some silent frames adjacent to sounded frames are included to prodive context."))
        self.MarginComment2.setText(_translate("Dialog", "How many frames on either the side of speech should be included?"))
        self.MarginComment3.setText(_translate("Dialog", "That\'s this variable."))
        self.SampleComment.setText(_translate("Dialog", "Sample rate: Sample rate of the input and output videos"))
        self.FrameRateComment.setText(_translate("Dialog", "Frame rate: Frame rate of the input and output videos. optional"))
        self.FrameQualityComment.setText(_translate("Dialog", "Frame quality: Quality of frames to be extracted from input video. Range: (1-5), (3 default)."))
        self.JumpCutButton.setText(_translate("Dialog", "Jump-cut"))

if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    Dialog = QtWidgets.QDialog()
    ui = Ui_Dialog()
    ui.setupUi(Dialog)
    Dialog.show()
    sys.exit(app.exec_())


# Download the Python helper library from twilio.com/docs/python/install
import os
from twilio.rest import Client

# Your Account Sid and Auth Token from twilio.com/user/account
# To set up environmental variables, see http://twil.io/secure
account_sid = os.environ['TWILIO_ACCOUNT_SID']
auth_token = os.environ['TWILIO_AUTH_TOKEN']
client = Client(account_sid, auth_token)

key = client.new_keys \
            .create(friendly_name="User Joey")

print(key.sid)
print(key.secret)


def input_to_list(filename):
    angel = []
    for vrstica in open(filename):
        list = vrstica.strip("\n").split(')')
        lover = []
        for i in list: lover.append(i)
        angel.append(lover)
    return angel

def count_the_satellites(dict, key):
    if dict[key] not in satellites.keys(): return 1
    else: return 1 + count_the_satellites(satellites, satellites[key])

def append_the_sattelites(dict, key, list):
    if dict[key] not in satellites.keys():
        return list.append()
    else:
        return list

orbits, satellites, sean, you, san = input_to_list("test_inputs.txt"), {}, 0, [], []

for i in reversed(range(0, len(orbits), 1)): satellites[orbits[i][1]] = orbits[i][0]

for i in reversed(range(0, len(orbits), 1)):
    if orbits[i][1] in satellites.keys():
        sean += count_the_satellites(satellites, orbits[i][1])

for i in range(len(orbits)):
    if orbits[i][1] == "YOU":
        you.append(append_the_sattelites(satellites, orbits[i][0], you))
        """
    if orbits[i][1] == "SAN":
        you.append(append_the_sattelites(satellites, orbits[i][0], san))
        """


# -*- coding: utf-8 -*-
# -----------------------------------------------------------------------------
# Copyright (c) 2014, Vispy Development Team. All Rights Reserved.
# Distributed under the (new) BSD License. See LICENSE.txt for more info.
# -----------------------------------------------------------------------------

import re
from subprocess import CalledProcessError

from ..wrappers import run_subprocess

def _get_dpi_from(cmd, pattern, func):
    """Match pattern against the output of func, passing the results as
    floats to func.  If anything fails, return None.
    """
    try:
        out, _ = run_subprocess([cmd])
    except (OSError, CalledProcessError):
        pass
    else:
        match = re.search(pattern, out)
        if match:
            return func(*map(float, match.groups()))

def get_dpi():
    """Get screen DPI from the OS.
    """

    from_xdpyinfo = _get_dpi_from(
        'xdpyinfo', r'(\d+)x(\d+) dots per inch',
        lambda x_dpi, y_dpi: (x_dpi + y_dpi) / 2)
    if from_xdpyinfo is not None:
        return from_xdpyinfo

    from_xrandr = _get_dpi_from(
        'xrandr', r'(\d+)x(\d+).*?(\d+)mm x (\d+)mm',
        lambda x_px, y_px, x_mm, y_mm: 25.4 * (x_px / x_mm + y_px / y_mm) / 2)
    if from_xrandr is not None:
        return from_xrandr

    raise RuntimeError('could not determine DPI')


import math

def find_factors(n):
    """
    Finds a list of factors of a number
    """
    factList = {1, n}
    for i in range(2, int(n ** 0.5) + 1):
        if (n % i == 0):
            factList.add(i)
            factList.add(n // i)
    return sorted(factList)

def find_divisors(n):
    """
    Find all the divisors of a positive number n, including 1 and n.
    Example: find_divisors(12) -> {1, 2, 3, 4, 6, 12}
    """
    divisors = {1}
    for i in range(2, math.ceil(n**0.5) + 1):
        if n%i == 0:
            divisors.add(i)
            divisors.add(n//i)
    divisors.add(n)
    return divisors

def perfect_number(n):
    """
    Check if a positive integer is a perfect number.
    A perfect number is a positive integer that is equal
    to the sum of its positive divisors, excluding the number itself.
    Example: perfect_number(6) -> True
        since the divisors of 6 (excluding itself) are 1 + 2 + 3
    """
    divisors = find_divisors(n)
    divisors.remove(n)
    sum_divisors = sum(divisors)
    return sum_divisors == n

def perfect_square(a):
    """
    Checks if called number is a perfect square
    """
    if int(math.sqrt(a) + 0.5) ** 2 == a:
        return True
    else:
        return False

def factorial(num):
    """
    The factorial of a number.
    On average barely quicker than product(range(1, num))
    """
    # Factorial of 0 equals 1
    if num == 0:
        return 1

    # if not, it is the product from 1...num
    product = 1
    for integer in range(1, num + 1):
        product *= integer
    return product

def louville_distance(precision):
    """
    Finds the distance between in zeroes between 2 '1's in the
    louville number.
    Will return distances, so if you sum earlier numbers together
    you get the index at which there would have been a '1'
    """
    precision += 1  # To get far enough
    distances = []

    factorial_cache = {}

    def factorial_(num):
        if num not in factorial_cache:
            factorial_cache[num] = factorial(num)
        return factorial_cache[num]

    for index in range(1, precision):
        distances.append(factorial(index) - factorial(index - 1))

    distances[0] = 1  # Because of the way the list is structured
    return distances

def get_common_divisors(n1, n2):
    """
    Non-Recursive function to return the GCD of n1 and n2
    """
    if n1 < n2:
        n1, n2 = n2, n1
    while n1 % n2:
        n1, n2 = n2, n1 % n2
    return n2

def get_common_divisors_recursive(n1, n2):
    """
    Recursive function to return the GCD of n1 and n2
    """
    if n1 == 0:
        return n2
    return get_common_divisors(n2 % n1, n1)

def get_least_common_divisors(n1, n2):
    """
    Function to return the lcm of two numbers
    """
    return (n1 * n2) / get_common_divisors(n1, n2)

def armstrong_number(number):
    """
    Check if number is Armstrong number
    """
    calc = number
    sum_ = 0
    while calc > 0:
        dig = calc % 10
        sum_ += dig ** 3
        calc //= 10
    if number == sum_:
        return True
    else:
        return False

"""
Amicable numbers are numbers where the sum of the diviors equals the other nummber,
where the divisors would be every number that divides a number n except n itself (includes 1).

An example of amicable numbers:
(220, 284)
They are amicable because the divisors of 220 are 1, 2, 4, 5, 10, 11, 20, 22, 44, 55 and 110, of which the sum is 284;
and the divisors of 284 are 1, 2, 4, 71 and 142, of which the sum is 220.
"""

def sum_proper_divisors(x):
    """
    Calculate the sum of x's proper divisors
    """

    s = 1
    for i in range(2, x // 2 + 1):
        if x % i == 0:
            s += i
    return s

def amicable_numbers(x, y):
    """"
    Checking if x and y are amicable numbers
    """

    if y == sum_proper_divisors(x) and x == sum_proper_divisors(y):
        k = True
    else:
        k = False
    return k

def incompletely_amicable_numbers(x, y):
    """"
    Checking if x and y are incompletely amicable numbers
    In other words, checks if the sum of the divisors is equal
    """

    if sum_proper_divisors(x) == sum_proper_divisors(y):
        k = True
    else:
        k = False
    return k

if __name__ == '__main__':
    import doctest
    doctest.testmod()


import cv2 as cv

img = cv.imread("img.png",0)
# print(img)
cap  = cv.VideoCapture(0)

if (cap.isOpened()==False):
    print("error")
while (cap.isOpened()):
    ret, frame = cap.read()
    if ret == True:
        cv.imshow("Frame", frame)
        if cv.waitKey(25) & 0xFF == ord('q'):
          break
cap.release()

# cv.imshow("img", img)
# cv.waitKey(0)
cv.destroyAllWindows()


"""
Quick debug script for slicing and assembling kernels in multiple convolutional layers.
"""

import numpy as np
import tensorflow as tf

# constants
_BATCH_SIZE = 1
# representation tensors
_DIM_R_H = 16
_DIM_R_W = 16
_DIM_R_A = 256 # slice A
_DIM_R_S = 263 # slice S
_DIM_R_B = 256 # slice B
# convolutional kernels
_DIM_K_H = 5
_DIM_K_W = 5
_DIM_K_N = 1 # number of filters: 256

# shared part between representations
r_s = tf.placeholder(shape=[_BATCH_SIZE, _DIM_R_H, _DIM_R_W, _DIM_R_S], dtype=tf.float32)
# representation p
r_a_p = tf.placeholder(shape=[_BATCH_SIZE, _DIM_R_H, _DIM_R_W, _DIM_R_A], dtype=tf.float32)
r_b_p = tf.placeholder(shape=[_BATCH_SIZE, _DIM_R_H, _DIM_R_W, _DIM_R_B], dtype=tf.float32)
r_p = tf.concat([r_a_p, r_s, r_b_p], axis=3)
# representation q
r_a_q = tf.placeholder(shape=[_BATCH_SIZE, _DIM_R_H, _DIM_R_W, _DIM_R_A], dtype=tf.float32)
r_b_q = tf.placeholder(shape=[_BATCH_SIZE, _DIM_R_H, _DIM_R_W, _DIM_R_B], dtype=tf.float32)
r_q = tf.concat([r_a_q, r_s, r_b_q], axis=3)

# kernel initializers
dim_r = _DIM_R_A + _DIM_R_S + _DIM_R_B
# k_p_init = np.random.rand(_DIM_K_H, _DIM_K_W, dim_r, _DIM_K_N).astype(np.float32)
k_p_init = np.ones([_DIM_K_H, _DIM_K_W, dim_r, _DIM_K_N], dtype=np.float32)
k_s_init = k_p_init[:, :, _DIM_R_A : _DIM_R_A + _DIM_R_S, :]
k_a_p_init = k_p_init[:, :, 0 : _DIM_R_A, :]
k_b_p_init = k_p_init[:, :, _DIM_R_A + _DIM_R_S : dim_r, :]
# k_q_init = np.random.rand(_DIM_K_H, _DIM_K_W, dim_r, _DIM_K_N).astype(np.float32)
k_q_init = np.ones([_DIM_K_H, _DIM_K_W, dim_r, _DIM_K_N], dtype=np.float32)
k_a_q_init = k_q_init[:, :, 0 : _DIM_R_A, :]
k_b_q_init = k_q_init[:, :, _DIM_R_A + _DIM_R_S : dim_r, :]

# full kernel
k_p = tf.Variable(k_p_init)
# shared part between kernels
k_s = tf.Variable(k_s_init)
# kernel parts for representation p
k_a_p = tf.Variable(k_a_p_init)
k_b_p = tf.Variable(k_b_p_init)
# assembled kernel for representation p
k_c_p = tf.concat([k_a_p, k_s, k_b_p], axis=2)
# kernel parts for representation q
k_a_q = tf.Variable(k_a_q_init)
k_b_q = tf.Variable(k_b_q_init)
# assembled kernel for representation q
k_c_q = tf.concat([k_a_q, k_s, k_b_q], axis=2)

# full convolutions
conv_k_f_p = tf.nn.conv2d(r_p, k_p, [1, 1, 1, 1], 'VALID')
conv_k_c_p = tf.nn.conv2d(r_p, k_c_p, [1, 1, 1, 1], 'VALID')
conv_k_c_q = tf.nn.conv2d(r_q, k_c_q, [1, 1, 1, 1], 'VALID')
# partial convolutions
conv_k_a_p = tf.nn.conv2d(r_a_p, k_a_p, [1, 1, 1, 1], 'VALID')
conv_k_s = tf.nn.conv2d(r_s, k_s, [1, 1, 1, 1], 'VALID')
conv_k_b_p = tf.nn.conv2d(r_b_p, k_b_p, [1, 1, 1, 1], 'VALID')
conv_sum_p = tf.add_n([conv_k_a_p, conv_k_s, conv_k_b_p])

# run a session and print results
sess = tf.Session()
sess.run(tf.initialize_all_variables())

# randomize representation
# r_s_init = np.random.rand(_BATCH_SIZE, _DIM_R_H, _DIM_R_W, _DIM_R_S)
# r_a_p_init = np.random.rand(_BATCH_SIZE, _DIM_R_H, _DIM_R_W, _DIM_R_A)
# r_b_p_init = np.random.rand(_BATCH_SIZE, _DIM_R_H, _DIM_R_W, _DIM_R_B)
# r_a_q_init = np.random.rand(_BATCH_SIZE, _DIM_R_H, _DIM_R_W, _DIM_R_A)
# r_b_q_init = np.random.rand(_BATCH_SIZE, _DIM_R_H, _DIM_R_W, _DIM_R_B)

scale = -9 * 10e-2
r_s_init = np.ones([_BATCH_SIZE, _DIM_R_H, _DIM_R_W, _DIM_R_S], dtype=np.float32) * scale
r_a_p_init = np.ones([_BATCH_SIZE, _DIM_R_H, _DIM_R_W, _DIM_R_A], dtype=np.float32) * scale
r_b_p_init = np.ones([_BATCH_SIZE, _DIM_R_H, _DIM_R_W, _DIM_R_B], dtype=np.float32) * scale
r_a_q_init = np.zeros([_BATCH_SIZE, _DIM_R_H, _DIM_R_W, _DIM_R_A], dtype=np.float32) * scale
r_b_q_init = np.zeros([_BATCH_SIZE, _DIM_R_H, _DIM_R_W, _DIM_R_B], dtype=np.float32) * scale

o_kfp, o_sum, o_kcp, o_kcq = sess.run(
    fetches=[conv_k_f_p, conv_sum_p, conv_k_c_p, conv_k_c_q],
    feed_dict={
        r_s : r_s_init,
        r_a_p : r_a_p_init,
        r_b_p : r_b_p_init,
        r_a_q : r_a_q_init,
        r_b_q : r_b_q_init,
    }
)

err1 = np.sum(np.abs(o_kcp - o_kfp))
print(err1)
err2 = np.sum(np.abs(o_sum - o_kfp))
print(err2)
diff = np.sum(np.abs(o_kcp - o_kcq))
print(diff)


from tkinter import *
from tkinter import ttk
import time
import numpy
from datetime import datetime
from PIL import Image, ImageTk

import paho.mqtt.client as mqtt

from threading import Thread
import queue
import os

# MQTT properties
BROKER_URL = "localhost"
BROKER_PORT = 1883

# use of Quality-of-service type 2 to ensure that messages reach remote endpoint
QOS = 2

# MQTT topics
TOPIC_SUBSCRIPTION = "motion-detection/motion"
TOPIC_BASEFRAME = "motion-detection/baseframe"
TOPIC_THRESHOLD = "motion-detection/threshold"
TOPIC_ALARM = "motion-detection/alarm"

# timestamp format
DATE_FORMAT = "%Y-%m-%d %H:%M:%S"

# image resolution
RES_X = 320
RES_Y = 240

# payload sizes
PAYLOAD_MOTION = RES_X * RES_Y * 3 + 8
PAYLOAD_FULL = PAYLOAD_MOTION + PAYLOAD_MOTION + 8

#### Helpers ####

def gettime(sep1, millis, sep2):
    return datetime.fromtimestamp(millis / 1000.0).strftime(sep1 + DATE_FORMAT + sep2)

def console_prefix(topic):
    return topic + " " + datetime.now().strftime("[" + DATE_FORMAT + "]") + ": "

#### UI setup ####

# Thread-safe Tkinter console that handles
# the asynchronous messages from MQTT
class ThreadSafeConsole(Text):
    def __init__(self, master, **options):
        Text.__init__(self, master, **options)
        # queue stores the messages to pass on to the console
        self.queue = queue.Queue()
        self.update()
    def write(self, line):
        # write to console
        self.queue.put(line)
    def clear(self):
        # clear console
        self.queue.put(None)
    def update(self):
        try:
            while 1:
                line = self.queue.get_nowait()
                if line is None:
                    self.delete(1.0, END)
                else:
                    self.insert(END, str(line))
                self.see(END)
                self.update_idletasks()
        except queue.Empty:
            pass
        self.after(100, self.update)

#### Event handlers ####

def on_set_baseframe_click():
    print("setting baseframe")
    client.publish(TOPIC_BASEFRAME, "Renew baseframe", QOS)

def on_set_threshold_click():
    threshold = threshold_input.get()

    # do nothing it threshold input is empty
    if threshold != "":
        # parse threshold value and determine if valid integer
        try:
            threshold = int(threshold)
            print("setting threshold to " + str(threshold))
            client.publish(TOPIC_THRESHOLD, threshold.to_bytes(1, "big"), QOS)
        except(ValueError):
            log.write("INVALID THRESHOLD VALUE \"" + threshold + "\"")

def on_trigger_alarm_click():
    print("trigger alarm")
    client.publish(TOPIC_ALARM, "Trigger alarm", QOS)

#### MqTT setup ####

def on_connect(client, userdata, flags, rc):
    print("Connected with result code " + str(rc))
    client.subscribe(TOPIC_SUBSCRIPTION)

def on_message(client, userdata, msg):
    print("Message received")

    topic = msg.topic
    data = msg.payload

    if len(data) == PAYLOAD_FULL:
        # load data from buffer and split up bytes
        # here the first two blocks define the frames
        # and the last two blocks (8 bytes each) contain the timestamps
        motion_frame = numpy.frombuffer(data[:3 * RES_X * RES_Y], dtype=numpy.uint8).reshape(RES_Y, RES_X, 3)
        acc_frame = numpy.frombuffer(data[3 * RES_X * RES_Y:2 * 3 * RES_X * RES_Y], dtype=numpy.uint8).reshape(RES_Y, RES_X, 3)
        acc_frame_time = int.from_bytes(data[2 * 3 * RES_X * RES_Y:2 * 3 * RES_X * RES_Y + 8], "big")
        acc_time = int.from_bytes(data[2 * 3 * RES_X * RES_Y + 8:2 * 3 * RES_X * RES_Y + 2 * 8], "big")
        motion_frame_time = int.from_bytes(data[2 * 3 * RES_X * RES_Y + 2 * 8:], "big")
        log.write(console_prefix(topic) + "The observed object was moved at " + gettime("", acc_time, "") +  ". Suspected motion occured at " + gettime("", acc_frame_time, "") + ", motion_frame_time: " + gettime("", motion_frame_time, "") + "\n")

        lab_motion_time.config(text=gettime("[", motion_frame_time, "]"))
        lab_acc_time.config(text=gettime("[", acc_frame_time, "]"))

        img1 = Image.fromarray(motion_frame)
        tkImg1 = ImageTk.PhotoImage(img1)
        motion_img.configure(image=tkImg1)
        motion_img.image = tkImg1

        img2 = Image.fromarray(acc_frame)
        tkImg2 = ImageTk.PhotoImage(img2)
        acc_img.configure(image=tkImg2)
        acc_img.image = tkImg2

    elif len(data) == PAYLOAD_MOTION:
        # first block contains motion frame
        # last 8 bytes contain time stamps
        motion_frame = numpy.frombuffer(data[:3 * RES_X * RES_Y], dtype=numpy.uint8).reshape(RES_Y, RES_X, 3)
        motion_frame_time = int.from_bytes(data[3 * RES_X * RES_Y:], "big")

        lab_motion_time.config(text=gettime("[", motion_frame_time, "]"))

        img1 = Image.fromarray(motion_frame)
        tkImg1 = ImageTk.PhotoImage(img1)
        motion_img.configure(image=tkImg1)
        motion_img.image = tkImg1
    else:
        log.write(console_prefix(topic) + "Received unknown message \"" + str(msg.payload) + "\"\n")

#### Main program ####

root = Tk()
root.title("PI motion detection")
root.resizable(False, False)

threshold_input = Entry(root)
threshold_input.grid(row=0, column=0, sticky=W+E+N+S)

set_threshold = Button(root, text="Set threshold", command=on_set_threshold_click)
set_threshold.grid(row=0, column=1, sticky=W+E+N+S)

set_baseframe = Button(root, text="Renew baseframe", command=on_set_baseframe_click)
set_baseframe.grid(row=1, column=0, columnspan=2, sticky=W+E+N+S)

lab1 = Label(root, text="Motion frame")
lab1.grid(row=2, column=0, sticky=W+E+N+S)

lab2 = Label(root, text="Acceleration frame")
lab2.grid(row=2, column=1, sticky=W+E+N+S)

# use frames to prevent Tkinter from shrinking empty labels
# (as the labels initially only contain text descriptions)
f = Frame(root, height=RES_Y, width=RES_X)
f.pack_propagate(0) # don't shrink
f.grid(row=3, column=0, sticky=W+E+N+S)

motion_img = Label(f, text="No motion image")
motion_img.pack(fill=BOTH, expand=1)

# use frames to prevent Tkinter from shrinking empty labels
f = Frame(root, height=RES_Y, width=RES_X)
f.pack_propagate(0) # don't shrink
f.grid(row=3, column=1, sticky=W+E+N+S)

acc_img = Label(f, text="No acceleration image")
acc_img.pack(fill=BOTH, expand=1)

lab_motion_time = Label(root)
lab_motion_time.grid(row=4, column=0, sticky=W+E+N+S)

lab_acc_time = Label(root)
lab_acc_time.grid(row=4, column=1, sticky=W+E+N+S)

log = ThreadSafeConsole(root)
log.grid(row=5, column=0, columnspan = 2, sticky=W+E+N+S)

trigger_alarm = Button(root, text="Trigger alarm", command=on_trigger_alarm_click)
trigger_alarm.grid(row=6, column=0, columnspan=2, sticky=W+E+N+S)

# initialize MQTT connection and set callbacks
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message

# connect asynchronously to start UI loop in primary thread
client.connect_async(BROKER_URL, BROKER_PORT, 60)

try:
    # start MQTT listener loop asynchronously
    client.loop_start()
    # start main UI loop
    root.mainloop()
except (KeyboardInterrupt, SystemExit):
    client.loop_stop()
    sys.exit()


import sys

from .global_switch import global_switch

def perror(*msg):
    ## don't exit here
    print("Error:  ", msg)
    sys.exit()

def pwarning(*msg):
    print("Warning:  ", msg)

if global_switch.debug:
    def pdebug(*args):
        # Print each argument separately so caller doesn't need to
        # stuff everything to be printed into a single string
        "Automatically log the current function details."
        import inspect, logging
        # Get the previous frame in the stack, otherwise it would
        # be this function!!!
        func = inspect.currentframe().f_back.f_code
        # Dump the message + the name of this function to the log.
        logging.debug("%s: %s in %s:%i" % (
            args,
            func.co_name,
            func.co_filename,
            func.co_firstlineno
        ))
else:
    pdebug = lambda *a: None