from __future__ import print_function

import os
import sys
from subprocess import check_output

from setuptools import find_packages, setup

def get_version():
    """Determine the most appropriate version number for this package."""

    # try to use git tag if building python package
    try:
        # append git sha to version
        return '0.0.' + check_output("git rev-list --count HEAD",shell=True).decode('utf8')
    except Exception as e:
        print("Failed to acquire version info from git: {e}".format(e=e), file=sys.stderr)
        return '0.0.0'

def requirements(extra=None):
    """Return list of required package names from requirements.txt."""
    # strip trailing comments, and extract package name from git urls.
    if extra:
        filename = 'requirements-' + extra + '.txt'
    else:
        filename = 'requirements.txt'
    requirements = [r.strip().split(';',1)[0].split(' ', 1)[0].split('egg=', 1)[-1]
                    for r in open(filename) if r.strip() and not r.strip().startswith('#')]
    return requirements

for x in requirements(extra='deploy'):
    print(x)

setup(
    name='dashboard',
    version=get_version(),
    packages=find_packages(),
    install_requires=requirements(),
    extras_require={
        'deploy': requirements(extra='deploy'),
    },
    entry_points={
        'console_scripts': [
            'dashboard = dashboard.manage:main',
        ],
    },
    include_package_data=True,
)


import astropy.units
import matplotlib.pyplot as plt
import numpy as np
import pytest

from mcalf.utils.plot import calculate_axis_extent, calculate_extent, class_cmap, hide_existing_labels

from ..helpers import figure_test

def test_hide_existing_labels():
    plot_settings = {
        'LineA': {'color': 'r', 'label': 'A'},
        'LineB': {'color': 'g', 'label': 'B'},
        'LineC': {'color': 'b', 'label': 'C'},
    }
    fig, ax = plt.subplots()
    ax.plot([0, 1], [0, 1], **plot_settings['LineA'])
    ax.plot([0, 1], [1, 0], **plot_settings['LineB'])
    hide_existing_labels(plot_settings)
    plt.close()
    ret = [x['label'] for x in plot_settings.values()]
    assert ret == ['_A', '_B', 'C']

def test_calculate_axis_extent():
    # Invalid `res`
    with pytest.raises(TypeError) as e:
        calculate_axis_extent('1 m', 1000)
    assert '`resolution` values must be' in str(e.value)

    # Invalid `px`
    with pytest.raises(TypeError) as e:
        calculate_axis_extent(250.0, 1000.5)
    assert '`px` must be an integer' in str(e.value)

    # Invalid `offset`
    with pytest.raises(TypeError) as e:
        calculate_axis_extent(250.0, 1000, [10])
    assert '`offset` must be an float or integer' in str(e.value)

    # Default `unit` used
    _, _, un = calculate_axis_extent(250.0, 1000, 10)
    assert un == 'Mm'  # Will fail if default value is changed
    _, _, un = calculate_axis_extent(250.0, 1000, 10, 'testunit')
    assert un == 'testunit'  # Will fail if default value is changed

    # Unit extracted
    _, _, un = calculate_axis_extent(250.0 * astropy.units.kg, 1000, 10, 'testunit')
    assert un == '$\\mathrm{kg}$'

    # Test good values
    f, l, _ = calculate_axis_extent(3., 7, -3.5)
    assert -10.5 == pytest.approx(f)
    assert 10.5 == pytest.approx(l)
    f, l, _ = calculate_axis_extent(2., 2, 1)
    assert 2. == pytest.approx(f)
    assert 6. == pytest.approx(l)

def test_calculate_extent():

    # Resolution of None should return None
    assert calculate_extent((100, 200), None) is None

    # Shape is 2-tuple
    for a in ((1, 1, 1), np.array([1, 1])):
        with pytest.raises(TypeError) as e:
            calculate_extent(a, resolution=(1, 1), offset=(1, 1))
        assert '`shape` must be a tuple of length 2' in str(e.value)

    # Resolution is 2-tuple
    for a in ((1, 1, 1), np.array([1, 1])):
        with pytest.raises(TypeError) as e:
            calculate_extent((100, 200), resolution=a, offset=(1, 1))
        assert '`resolution` must be a tuple of length 2' in str(e.value)

    # Offset is 2-tuple
    for a in ((1, 1, 1), np.array([1, 1])):
        with pytest.raises(TypeError) as e:
            calculate_extent((100, 200), resolution=(1, 1), offset=a)
        assert '`offset` must be a tuple of length 2' in str(e.value)

    # Test good value
    l, r, b, t = calculate_extent((7, 2), (2., 3.), (1, -3.5))
    assert 2. == pytest.approx(l)
    assert 6. == pytest.approx(r)
    assert -10.5 == pytest.approx(b)
    assert 10.5 == pytest.approx(t)

def plot_helper_calculate_extent(*args, dimension=None):
    fig, ax = plt.subplots()
    calculate_extent(*args, ax=ax, dimension=dimension)
    x, y = ax.get_xlabel(), ax.get_ylabel()
    plt.close(fig)
    return x, y

def test_calculate_extent_ax():

    # Common args
    args = ((7, 2), (2., 3.), (1, -3.5))

    # Dimension is 2-tuple or 2-list
    for d in ((1, 1, 1), np.array([1, 1]), [1, 1, 1]):
        with pytest.raises(TypeError) as e:
            plot_helper_calculate_extent(*args, dimension=d)
        assert '`dimension` must be a tuple or list of length 2' in str(e.value)

    # Different for both
    x, y = plot_helper_calculate_extent(*args, dimension=('test / one', 'test / two'))
    assert 'test / one (Mm)' == x
    assert 'test / two (Mm)' == y

    # Default values
    x, y = plot_helper_calculate_extent(*args)
    assert 'x-axis (Mm)' == x
    assert 'y-axis (Mm)' == y

    # Same for both
    x, y = plot_helper_calculate_extent(*args, dimension='test / same')
    assert 'test / same (Mm)' == x
    assert 'test / same (Mm)' == y

def test_class_cmap():

    # Incorrect `n` type
    with pytest.raises(TypeError) as e:
        class_cmap('original', 2.5)
    assert '`n` must be an integer' in str(e.value)

    # Correct `n` type
    class_cmap('original', 4)

@figure_test
def test_class_cmap_plot(pytestconfig):

    params = (
        ('original', 3),
        ('original', 5),
        ('original', 6),
        ('jet', 4),
    )

    fig, axes = plt.subplots(len(params))

    for ax, (style, n) in zip(axes, params):
        gradient = np.arange(-1, n + 1)
        gradient = np.vstack((gradient, gradient))
        cmap = class_cmap(style, n)
        ax.imshow(gradient, vmin=-0.5, vmax=n - 0.5, cmap=cmap)
        ax.set(xticks=[], yticks=[])


import utils
import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import multivariate_normal as mvn
from sklearn.mixture import BayesianGaussianMixture

def truncate_sample(x):
    """This function ensures that the obtained sample is between (0,1)
    """

    x = np.minimum(x, 1)
    x = np.maximum(x, 0)

    return x

class BayesClassifierGaussian:

    # No constructor is required

    def fit(self, X, Y):
        """Function fitting the gaussian
        X - inputs
        Y - classes
        """

        self.classes = len(set(Y)) # We assume classes are in (0...K-1)
        self.gaussians = []
        self.p_y = np.zeros(self.classes) # p(y)

        for individual_class in range(self.classes):

            X_class = X[ Y == individual_class ]

            self.p_y[individual_class] = len(X_class)

            mean = np.mean(X_class, axis=0)
            covariance = np.cov(X_class.T)
            gaussian = {'mean': mean, 'covariance': covariance}
            self.gaussians.append(gaussian)

        self.p_y = self.p_y / self.p_y.sum() # This normalizes p(y)

    def sample_given_y(self, y):
        """ Function drawing a random sample and calculating  p(x | y)
        Finds a gaussian corresponding to y and generates a sample
        """

        gaussian = self.gaussians[y]
        return truncate_sample( mvn.rvs(mean = gaussian['mean'], cov=gaussian['covariance']) )

    def sample(self):
        """ Function choosing a random class y and returns a sample
        """

        y = np.random.choice(self.classes, p= self.p_y)
        return truncate_sample( self.sample_given_y(y) )

class BayesClassifierGMM():

    # No constructor is required

    def fit(self, X, Y):
        """Function fitting the gaussian
        X - inputs
        Y - classes
        """

        self.classes = len(set(Y)) # We assume classes are in (0...K-1)
        self.gaussians = []
        self.p_y = np.zeros(self.classes) # p(y)

        for individual_class in range(self.classes):

            print("Fitting GMM for the %s class" %individual_class)

            X_class = X[ Y == individual_class ]

            self.p_y[individual_class] = len(X_class)

            # Each Gaussian is a Bayesian Gaussian Mixture Object
            # The 10 argument is the maximum number of clusters (chosen arbitrarily, could be more)

            GMM = BayesianGaussianMixture(10)

            # The fit function performs the variational inferance update (could take long, iterative algorithm)

            GMM.fit(X_class)

            self.gaussians.append(GMM)

            print("Finished fitting the GMM for the %s class" %individual_class)
            print("======================================================")

        self.p_y = self.p_y / self.p_y.sum() # This normalizes p(y)

    def sample_given_y(self, y):
        """ Function drawing a random sample and calculating  p(x | y)
        Finds a gaussian corresponding to y and generates a sample
        """

        gaussian_gmm = self.gaussians[y]

        sample = gaussian_gmm.sample()

        # The .sample() function returns a touple (the sample, the cluster)

        cluster_mean = gaussian_gmm.means_[sample[1]]

        return truncate_sample( sample[0].reshape(28,28) ), cluster_mean.reshape(28,28)

    def sample(self):
        """ Function choosing a random class y and returns a sample
        """

        y = np.random.choice(self.classes, p= self.p_y)
        return truncate_sample( self.sample_given_y(y) )

if __name__ == '__main__':

    X, Y = utils.load_MNIST()

    # As 2 classifier methods are employeed, uncomment the desired one.

    # classifier = BayesClassifierGaussian()
    classifier = BayesClassifierGMM()

    classifier.fit(X, Y)

    for individual_class in range(classifier.classes):
        if classifier == BayesClassifierGaussian():

            sample = classifier.sample_given_y(individual_class).reshape(28, 28)
            mean = classifier.gaussians[individual_class]['mean'].reshape(28, 28)

        else:

            sample, mean = classifier.sample_given_y(individual_class)

        plt.subplot(1,2,1)
        plt.imshow(sample, cmap='gray')
        plt.title("Sample")
        plt.subplot(1,2,2)
        plt.imshow(mean, cmap='gray')
        plt.title("Mean")
        plt.show()

    if classifier == BayesClassifierGaussian():

        random_sample = classifier.sample().reshape(28,28)

    else:

        random_sample = classifier.sample()

    plt.imshow(random_sample, cmap='gray')
    plt.title("Random Sample from Random Class")
    plt.show()


import gym
from Model import *
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

class Agent:
    def __init__(self,env_name='CartPole-v0'):
        self.env = gym.make(env_name)
        self.batch_size = 32
        num_states = self.env.observation_space.shape[0]
        num_actions = self.env.action_space.n

        self.agent = DQNAgent(num_states, num_actions)

    def train(self):
        # initialize gym environment and the agent
        episodes = 1000
        # Iterate the game
        for e in range(episodes):
            # reset state in the beginning of each game
            state = self.env.reset()
            state = np.reshape(state, [1, 4])
            rewards = 0
            # time_t represents each frame of the game
            # Our goal is to keep the pole upright as long as possible until score of 500
            # the more time_t the more score
            for time_t in range(500):

                # turn this on if you want to render
                # env.render()
                # Decide action
                action = self.agent.choose(state)
                # Advance the game to the next frame based on the action.
                # Reward is 1 for every frame the pole survived
                next_state, reward, done, _ = self.env.step(action)
                rewards += reward

                next_state = np.reshape(next_state, [1, 4])
                # Remember the previous state, action, reward, and done
                self.agent.transition(state, action, rewards, next_state, done)
                # make next_state the new current state for the next frame.
                state = next_state

                # done becomes True when the game ends
                # ex) The agent drops the pole

                if done:
                    # print the score and break out of the loop
                    print("episode: {}/{}, score: {}"
                          .format(e, episodes, time_t))
                    break
                if len(self.agent.memory) > self.batch_size:
                    self.agent.replay(self.batch_size)

            # train the agent with the experience of the episode
            print("episode: {}/{}, score: {}"
                  .format(e, episodes, rewards))

        self.final_position(self.agent.actions)
        self.agent.save()
    def test(self):
        # initialize gym environment and the agent
        self.agent.load()
        for i in range(10):
            state = self.env.reset()

            state = np.reshape(state, [1, 4])
            for j in range(500):
                self.env.render()
                action = self.agent.choose(state)
                # Advance the game to the next frame based on the action.
                # Reward is 1 for every frame the pole survived
                next_state, reward, done, _ = self.env.step(action)
                next_state = np.reshape(next_state, [1, 4])

                state = next_state

                if done:
                    # print the score and break out of the loop
                    print("game over")
                    break

    def final_position(self,positions):
        import time
        localtime = time.localtime()
        timeString = time.strftime("%m%d%H", localtime)
        timeString = str(timeString) + '.jpg'

        plt.figure(3, figsize=[10, 5])
        p = pd.Series(positions)
        ma = p.rolling(10).mean()
        plt.plot(p, alpha=0.8)
        plt.plot(ma)
        plt.xlabel('Epsiode')
        plt.ylabel('Poistion')
        plt.title('Final Position - Modified.jpg')
        plt.savefig(timeString)
        plt.show()


import json
from django.urls import reverse

def test_account_create(client):
    url = reverse("accounts:create")
    response = client.get(url)
    content = json.loads(response.content)
    assert response.status_code == 200
    assert content["create"] == "created"

def test_account_retrieve(client):
    url = reverse("accounts:retrieve")
    response = client.get(url)
    content = json.loads(response.content)
    assert response.status_code == 200
    assert content["retrieve"] == "retrieved"

def test_account_update(client):
    url = reverse("accounts:update")
    response = client.get(url)
    content = json.loads(response.content)
    assert response.status_code == 200
    assert content["update"] == "updated"

def test_account_delete(client):
    url = reverse("accounts:delete")
    response = client.get(url)
    content = json.loads(response.content)
    assert response.status_code == 200
    assert content["delete"] == "deleted"


import numpy as np

def predict_sales(radio, weight, bias):
    return weight*radio + bias

def cost_function(radio, sales, weight, bias):
    companies = len(radio)
    total_error = 0.0
    for i in range(companies):
        total_error += (sales[i] - (weight*radio[i] + bias))**2
    return total_error / companies

def update_weights(radio, sales, weight, bias, learning_rate):
    weight_deriv = 0
    bias_deriv = 0
    companies = len(radio)

    for i in range(companies):
        # Calculate partial derivatives
        # -2x(y - (mx + b))
        weight_deriv += 2*radio[i] * ((weight*radio[i] + bias) - sales[i])
        # -2(y - (mx + b))
        bias_deriv += 2*((weight*radio[i] + bias) - sales[i])

    # We subtract because the derivatives point in direction of steepest ascent
    weight_deriv = weight_deriv / companies
    bias_deriv = bias_deriv / companies
    print ("weight derivative: " + str(weight_deriv))
    weight -= (weight_deriv) * learning_rate
    bias -= (bias_deriv) * learning_rate

    return weight, bias

def train(radio, sales, weight, bias, learning_rate, iters):
    cost_history = []

    for i in range(iters):
        weight,bias = update_weights(radio, sales, weight, bias, learning_rate)

        #Calculate cost for auditing purposes
        cost = cost_function(radio, sales, weight, bias)
        cost_history.append(cost)

        # Log Progress
        print ("iter: "+str(i) + " weight: "+str(weight) + " bias: " + str(bias)  + " cost: " + str(cost))

    return weight, bias, cost_history

def main():
    radio = [1, 2, 3, 4, 5]
    sales = [3, 8, 9, 12, 15]
    weight = 2
    bias = 0
    weight, bias, cost_history = train(radio, sales, weight, bias, 0.001, 20)
    print (weight, bias)

if __name__ == "__main__":
    main()


'''
PyProcessor is a macro-based text manipulation program patterned off of the C preprocessor.
It's designed to be easy to use and powerful. It uses simple instructions, one per line at most, prefixed with !.
The ! should not interfere with any not symbols, as it only matches ones that are alone on a line and have a command immediately afterwards.
It operates recursively on replacements, to allow multiple macro-uses, however they will not effect the macros themselves.
Currently, it does not support call-replacements, however that is on the line. It will require some serious regex work, however.
It is not written in a super pythonic style, and it does not have a class-based implementation.
It is written as a script for running over a file, and that does show in locations.
I have tried to keep it as clean and organized as possible.
It is also hopefully very well commented.
'''
# -------- Definitions --------
'''
This will take include argv, which will allow command line harvesting.
'''
from sys import argv

'''
This is preprocessor storage. It is where all macros are listed, so that way all functions can easily check through it.
It's stored as a dictionary, where the macro name is the key and the value is the result of the macro.
'''
macros = {

    }
'''
    These are the function definitions. They include every part of the preprocessor code that isn't simply "glue" code.
    These are vital to the functioning of the program itself.
    Each function will have a comment explaining there algorithim at the start of the function.
    This will also list what it's inputs are and what they do.
'''
def fileToString(file):
    '''
    fileToString is a function meant to make copies of a file as a string.
    It works using the fact that files are iterators.
    It starts by generating an empty array, then follows it by iteraing through the file,
    appending each line.
    Finally, it returns the file.
    '''
    file_array = []
    for line in file:
        file_array.append(line)
    return file_array

def findAllMacros(file_array):
    '''
    findAllMacros iterates through the entire file.
    It splits each line, and checks the first word. If the first word is !DEFINE (which is not case sensitive, thanks to a call to str.Upper()),
    it takes the next word as a key, joins the rest of the line until new line, and assigns that as the value of the key.
    If it isn't !DEFINE, then it adds it to an array of non-preprocessor lines. The list of non-preprocessor lines is the return value of the program.
    '''
    non_preprocessed_lines = []
    for line in file_array:
        checked_line = line.split()
        checked_line.append('')
        if checked_line[0].upper() == '!DEFINE':
            macros[checked_line[1]] = ' '.join(checked_line[2:len(checked_line)])
        else:
            non_preprocessed_lines.append(line)
    return non_preprocessed_lines
def macroReplacement(file_array):
    '''
    macroReplacment takes the file array. It creates a 'new_file' array that will hold the preproccessed file.
    It then iterates through the original file, iterating through each macro and calling a .replace() on each line. It appends the replaced line to the new_file array.
    Finally, it returns the preproccessed file.
    '''
    new_file = []
    for line in file_array:
        new_line = line
        for macro in macros:
                new_line = new_line.replace(macro, macros[macro])
        new_file.append(new_line)
    return new_file
# -------- Main Block --------
'''
PyProcessor takes two command line arguements, the input and the output.
It does not complain about having MORE inputs, but it does not use them.
'''
if len(argv) < 3:
    '''
    If it doesn't have enough arguements, make sure to display a simple but descriptive error message.
    '''
    print('Error: PyProcessor takes two arguements (input file and output file).')
else:
    '''
    If the file does have enough inputs, open the two files.
    These two files are the input file, which is opened in read mode to allow the reading of the file,
    and the output file, which is opened in write mode to allow the modified version to be written there.
    It then writes the "input file" into a string called 'preprocessor_input_array'.
    Next, it finds the macros, then calls the replacement.
    Finally, it writes all the new lines into the output file.
    '''
    preprocessor_input = open(argv[1], 'r')
    preprocessor_output = open(argv[2], 'w')
    preprocessor_input_array = fileToString(preprocessor_input)
    preprocessor_input_array = findAllMacros(preprocessor_input_array)
    preprocessor_input_array = macroReplacement(preprocessor_input_array)
    for line in preprocessor_input_array:
        preprocessor_output.write(line)


import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--attn_type', type=str, default='channelwise')
args = parser.parse_args()

import pandas as pd
import numpy as np
from utils.paths import path_expt, path_expt_raw

n_all = len(list(path_expt_raw.glob('results_task*.txt')))
n_spatial = len(list(path_expt_raw.glob('results_task*_spatial.txt')))
n_tasks = n_spatial if args.attn_type == 'spatial' else n_all - n_spatial
training, weights, results = [], [], []

for i in range(n_tasks):
    task_id = f'task{i:04}_spatial' if args.attn_type == 'spatial' else f'task{i:04}'
    training_i = pd.read_csv(path_expt_raw/f'training_{task_id}.csv')
    weights_i = np.loadtxt(path_expt_raw/f'weights_{task_id}.txt')
    results_i = np.loadtxt(path_expt_raw/f'results_{task_id}.txt')
    training_i.insert(0, 'task', i)
    training.append(training_i)
    weights.append(np.reshape(weights_i, (1, -1)))
    results.append(np.reshape(results_i, (1, -1)))

training = pd.concat(training)
weights = np.vstack(weights)
results = np.vstack(results)
extension = '_spatial' if args.attn_type == 'spatial' else ''
training.to_csv(path_expt/f'training{extension}.csv', index=False)
np.savetxt(path_expt/f'weights{extension}.txt', weights, fmt='%.18f')
np.savetxt(path_expt/f'results{extension}.txt', results, fmt='%.18f')


import numpy as np
import pyqtgraph as pg
from pyqtgraph.Qt import QtCore, QtGui
import socket
import threading
import struct
import os

send_pred_sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)  # Internet  # UDP
OUT_UDP_PORT = 7777

HOST = "0.0.0.0"
PORT = 8001
CHUNK = 1000      # buffer size for socket
BUFFER_SIZE = 500   # 300/25 secs of data = 12
PHONE_KEYS = ['unix_timestamp', 'acc_x', 'acc_y', 'acc_z', 'quart_x', 'quart_y', 'quart_z', 'quart_w', 'grav_x', 'grav_y', 'grav_z', 'roll', 'pitch', 'yaw']

DATA = {}

time_buffer = np.zeros((BUFFER_SIZE, 1)) # init with zero
raw_acc_buffer = np.zeros((BUFFER_SIZE, 3)) # init with zero accel
raw_grav_buffer = np.zeros((BUFFER_SIZE, 3)) # init with zero accel
raw_ori_buffer = np.zeros((BUFFER_SIZE, 3)) # init with zero accel
raw_quat_buffer = np.array([[0, 0, 0, 1]] * BUFFER_SIZE) # init with identity rotations

save_data = []

window_open = True
data_len = 1000

base_colors = [(255, 255, 0, 200), (255, 146, 0, 200), (255, 185, 224, 200), (248, 225, 64, 200)]

app = pg.mkQApp("Plotting Example")
win = pg.GraphicsLayoutWidget(show=True, title="Basic plotting examples")
win.resize(1000,600)
win.setWindowTitle('pyqtgraph example: Plotting')

a1 = win.addPlot(title="x")
a1.setLabel('left', "accel")
a2 = win.addPlot(title="y")
a3 = win.addPlot(title="z")
a1.enableAutoRange('xy', False)
a2.enableAutoRange('xy', False)
a3.enableAutoRange('xy', False)
a1.setXRange(0, BUFFER_SIZE, padding=0)
a2.setXRange(0, BUFFER_SIZE, padding=0)
a3.setXRange(0, BUFFER_SIZE, padding=0)
a1.setYRange(-3, 3, padding=0.1)
a2.setYRange(-3, 3, padding=0.1)
a3.setYRange(-3, 3, padding=0.1)
acc_curve1 = a1.plot(raw_acc_buffer[:, 0], pen=(0, 255, 0))
acc_curve2 = a2.plot(raw_acc_buffer[:, 1], pen=(0, 255, 0))
acc_curve3 = a3.plot(raw_acc_buffer[:, 2], pen=(0, 255, 0))

win.nextRow()

g1 = win.addPlot()
g1.setLabel('left', "gravity")
g2 = win.addPlot()
g3 = win.addPlot()
g1.enableAutoRange('xy', False)
g2.enableAutoRange('xy', False)
g3.enableAutoRange('xy', False)
g1.setXRange(0, BUFFER_SIZE, padding=0)
g2.setXRange(0, BUFFER_SIZE, padding=0)
g3.setXRange(0, BUFFER_SIZE, padding=0)
g1.setYRange(-1, 1, padding=0.1)
g2.setYRange(-1, 1, padding=0.1)
g3.setYRange(-1, 1, padding=0.1)
grav_curve1 = g1.plot(raw_grav_buffer[:, 0], pen=(255, 0, 255))
grav_curve2 = g2.plot(raw_grav_buffer[:, 1], pen=(255, 0, 255))
grav_curve3 = g3.plot(raw_grav_buffer[:, 2], pen=(255, 0, 255))

win.nextRow()

o1 = win.addPlot()
o1.setLabel('left', "euler")
o2 = win.addPlot()
o3 = win.addPlot()
o1.enableAutoRange('xy', False)
o2.enableAutoRange('xy', False)
o3.enableAutoRange('xy', False)
o1.setXRange(0, BUFFER_SIZE, padding=0)
o2.setXRange(0, BUFFER_SIZE, padding=0)
o3.setXRange(0, BUFFER_SIZE, padding=0)
o1.setYRange(-150, 150, padding=0.1)
o2.setYRange(-150, 150, padding=0.1)
o3.setYRange(-150, 150, padding=0.1)
ori_curve1 = o1.plot(raw_ori_buffer[:, 0], pen=(255, 255, 0))
ori_curve2 = o2.plot(raw_ori_buffer[:, 1], pen=(255, 255, 0))
ori_curve3 = o3.plot(raw_ori_buffer[:, 2], pen=(255, 255, 0))

send_sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)  # Internet  # UDP
OUT_UDP_IP = "127.0.0.1"

def save_data_file():
    global save_data

    outfile = "pocket_raw_data_" + time.strftime("%Y%m%d_%H%M%S")

    if(len(save_data) == 0):
        print('Saved nothing')
        return

    # Save the collected data
    sd = np.vstack(save_data)

    print("Collected data samples: ", sd.shape, "Saved in: ", outfile)
    np.save(outfile, sd)

    save_data = []

def process_data(message):
    global time_buffer, raw_acc_buffer, raw_grav_buffer, raw_quat_buffer, raw_ori_buffer

    """Receive data from socket.
    """
    message = message.strip()
    if not message:
        return
    message = message.decode('utf-8')
    if message == 'stop':
        return
    if ':' not in message:
        print(message)
        return

    try:
        device_id, raw_data_str = message.split(";")
        device_type, data_str = raw_data_str.split(':')
    except Exception as e:
        print(e, message)
        return

    data = []
    for d in data_str.strip().split(' '):
        try:
            data.append(float(d))
        except Exception as e:
            print(e)
            continue

    if len(data) != len(PHONE_KEYS):
        print("something is missing...skipping packet")
        return

    device_name = "phone"

    #print(device_type, data)

    # update the buffers
    curr_time = np.array(data[0]).reshape(1, 1)
    curr_acc = np.array(data[1:4]).reshape(1, 3)
    curr_quat = np.array(data[4:8]).reshape(1, 4)
    curr_grav = np.array(data[8:11]).reshape(1, 3)
    curr_ori = np.array(data[11:14]).reshape(1, 3) * (180 / np.pi)

    time_buffer = np.concatenate([time_buffer[1:], curr_time])
    raw_acc_buffer = np.concatenate([raw_acc_buffer[1:], curr_acc])
    raw_grav_buffer = np.concatenate([raw_grav_buffer[1:], curr_grav])
    raw_ori_buffer = np.concatenate([raw_ori_buffer[1:], curr_ori])
    raw_quat_buffer = np.concatenate([raw_quat_buffer[1:], curr_quat])

    z = np.hstack([time_buffer, raw_acc_buffer, raw_grav_buffer, raw_ori_buffer, raw_quat_buffer])
    save_data.append(z)

    return device_name

def get_data():
    with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s:  # UDP
        s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        s.bind((HOST, PORT))

        while True:
            try:
                data, addr = s.recvfrom(CHUNK)
                device_id = process_data(data)
            except KeyboardInterrupt:
                print('===== close socket =====')
                os._exit(0)
            except Exception as e:
                print(e)
                pass

def update():
    # Update all curve plots
    acc_curve1.setData(raw_acc_buffer[:, 0])
    acc_curve2.setData(raw_acc_buffer[:, 1])
    acc_curve3.setData(raw_acc_buffer[:, 2])
    grav_curve1.setData(raw_grav_buffer[:, 0])
    grav_curve2.setData(raw_grav_buffer[:, 1])
    grav_curve3.setData(raw_grav_buffer[:, 2])
    ori_curve1.setData(raw_ori_buffer[:, 0])
    ori_curve2.setData(raw_ori_buffer[:, 1])
    ori_curve3.setData(raw_ori_buffer[:, 2])

timer = QtCore.QTimer()
timer.timeout.connect(update)
timer.start()

if __name__ == '__main__':
    t1 = threading.Thread(target=get_data)
    t1.start()

    #pg.exec()
    import sys
    if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
        QtGui.QApplication.instance().exec_()

    window_open = False


# -*- coding: utf-8 -*-

import math
from my_game.models import System, Planet
from my_game.models import Flightplan, FlightplanFlight
from my_game.flightplan.create.calculation import calculation

def flight(*args):
    session_user = args[0]
    fleet = args[2]
    request = args[3]
    fleet_engine = fleet.fleet_engine
    city = request.POST.get('city')
    coordinate = request.POST.get('coordinate')
    message = ''
    if city:
        planet_system = int(request.POST.get('planet_system'))
        planet_planet = int(request.POST.get('planet_planet'))
        planet_giper = request.POST.get('planet_giper')
        planet_null = request.POST.get('planet_null')
        target_system = System.objects.filter(id=planet_system).first()
        target_planet = Planet.objects.filter(system=target_system, planet_num=planet_planet).first()
        if target_planet:
            flightplan_flight = FlightplanFlight.objects.filter(fleet=fleet).last()
            if flightplan_flight:
                xx1 = flightplan_flight.finish_x
                yy1 = flightplan_flight.finish_y
                zz1 = flightplan_flight.finish_z

            else:
                xx1 = int(fleet.x)
                yy1 = int(fleet.y)
                zz1 = int(fleet.z)

            if target_planet:
                xx2 = int(target_planet.global_x)
                yy2 = int(target_planet.global_y)
                zz2 = int(target_planet.global_z)

            else:
                distance = math.sqrt((xx1 - int(target_system.x)) ** 2 + (yy1 - int(target_system.y)) ** 2 + (
                    zz1 - int(target_system.z)) ** 2)
                xx2 = xx1 + (int(target_system.x) - xx1) * (distance - int(target_system.system_size)) / distance
                yy2 = yy1 + (int(target_system.y) - yy1) * (distance - int(target_system.system_size)) / distance
                zz2 = zz1 + (int(target_system.z) - zz1) * (distance - int(target_system.system_size)) / distance

            distance = math.sqrt((xx1 - xx2) ** 2 + (yy1 - yy2) ** 2 + (zz1 - zz2) ** 2)
            flightplan_flight = FlightplanFlight.objects.filter(fleet=fleet).last()
            if fleet.system_id != 0 and not flightplan_flight:
                system = System.objects.filter(id=fleet.system_id).first()
            else:
                system = System.objects.filter(id=flightplan_flight.system_id).first()

            if system:
                if int(system.id) == planet_system:
                    flight_time = math.sqrt(
                        distance / 2 * (int(fleet.ship_empty_mass)) / int(fleet_engine.system_power)) * 2
                    command_id = 1
                else:
                    answer = calculation(fleet, planet_giper, planet_null, distance)
                    command_id = answer['command_id']
                    flight_time = answer['flight_time']
            else:
                answer = calculation(fleet, planet_giper, planet_null, distance)
                command_id = answer['command_id']
                flight_time = answer['flight_time']

            if command_id != 1:
                system_flight = 0
            else:
                system_flight = 1

            flightplan = Flightplan(
                user=session_user,
                fleet=fleet,
                class_command=1,
                command_id=command_id,
                status=0,
            )
            flightplan.save()
            flightplan_flight = FlightplanFlight(
                user=session_user,
                fleet=fleet,
                flightplan=flightplan,
                command_id=command_id,
                start_x=xx1,
                start_y=yy1,
                start_z=zz1,
                finish_x=int(xx2),
                finish_y=int(yy2),
                finish_z=int(zz2),
                flight_time=flight_time,
                planet_id=planet_planet,
                system_id=planet_system,
                system_flight=system_flight
            )
            flightplan_flight.save()
        else:
            message = 'Планеты не существует'

    if coordinate:
        coordinate_x = float(request.POST.get('coordinate_x'))
        coordinate_y = float(request.POST.get('coordinate_y'))
        coordinate_z = float(request.POST.get('coordinate_z'))
        coordinate_giper = request.POST.get('coordinate_giper')
        coordinate_null = request.POST.get('coordinate_null')
        flightplan_flight = FlightplanFlight.objects.filter(fleet=fleet).last()

        if flightplan_flight:
            xx1 = flightplan_flight.finish_x
            yy1 = flightplan_flight.finish_y
            zz1 = flightplan_flight.finish_z
        else:
            xx1 = int(fleet.x)
            yy1 = int(fleet.y)
            zz1 = int(fleet.z)

        xx2 = coordinate_x
        yy2 = coordinate_y
        zz2 = coordinate_z
        distance = math.sqrt((xx1 - xx2) ** 2 + (yy1 - yy2) ** 2 + (zz1 - zz2) ** 2)

        answer = calculation(fleet, coordinate_giper, coordinate_null, distance)
        command_id = answer['command_id']
        flight_time = answer['flight_time']

        system_id = 0
        planet = Planet.objects.filter(global_x=coordinate_x, global_y=coordinate_y, global_z=coordinate_z).first()
        if planet:
            planet_id = planet.planet_num
            system_id = planet.system.id
        else:
            planet_id = 0

        if command_id != 1:
            system_flight = 0
        else:
            system_flight = 1

        flightplan = Flightplan(
            user=session_user,
            fleet=fleet,
            class_command=1,
            command_id=command_id,
            status=0,
        )
        flightplan.save()

        flightplan_flight = FlightplanFlight(
            user=session_user,
            fleet=fleet,
            flightplan=flightplan,
            command_id=command_id,
            start_x=xx1,
            start_y=yy1,
            start_z=zz1,
            finish_x=xx2,
            finish_y=yy2,
            finish_z=zz2,
            flight_time=flight_time,
            planet_id=planet_id,
            system_id=system_id,
            system_flight=system_flight
        )
        flightplan_flight.save()

    return message


import numpy as np
from numpy import array
import json
import matplotlib.pyplot as plt
from os import listdir, path
import collections
import math
import pickle

dirname = 'val' # the default
regs = ['lasso', #'ridge',
        'enet', 'penalty',
        'wlasso', 'wridge', 'owl', 'eye']

def extract(record):
    def extractRecord(item):
        return list(map(lambda e: e[item],record))
    return extractRecord

def plotResult(fn, title=None, save=False, criteria="loss"):
    record = json.load(open(fn))

    extractRecord = extract(record)
    valc = extractRecord('validation/main/%s' % criteria)
    trc = extractRecord('main/%s' % criteria)

    plt.plot(valc, label='val %s' % criteria)
    plt.plot(trc, label='tr %s' % criteria)
    print(valc[-1])
    # plt.ylim([0,1])
    plt.legend()
    if title: plt.title(title + " %s" % criteria)
    else: plt.title(criteria)
    if save:
        plt.savefig(path.join(path.dirname(fn),"%s_plot.png" % criteria))
        plt.clf()
    else: plt.show()

# plots for seeing if training complete
def genplots(dirname=dirname):
    for fn in listdir(dirname):
        if not path.isdir(path.join(dirname, fn)) or\
           fn.startswith("montage"): continue
        print(fn)
        plotResult(path.join(dirname, fn, 'log'), title=fn, criteria="loss")
        plotResult(path.join(dirname, fn, 'log'), title=fn, criteria="accuracy")

def returnBest(methods=regs, criteria='validation/main/loss',
               minimize = True, dirname=dirname,
               report=['validation/main/loss',
                       'validation/main/accuracy',
                       'validation/main/auroc']):
    if type(methods) is str: methods = [methods]
    if criteria in report:
        index = report.index(criteria)
        report = report[:index] + report[index+1:]
    print(['args', criteria]+report)
    D = {}
    for fn in listdir(dirname):
        if not path.isdir(path.join(dirname, fn)) or\
           fn.startswith("montage"): continue

        param_index = fn.find('(')
        method = fn[:param_index]
        if method not in methods: continue

        args = fn[param_index:]

        #### special case for storing hashes, quick hack #######
        hashname = ""
        if args[-1] != ')':
            hashname = args[1:]
            args = hash2arg(hashname, dirname=dirname)
        else:
            args = eval(args)

        record = json.load(open(path.join(dirname, fn, 'log')))
        extractRecord = extract(record)
        key = extractRecord(criteria)

        try:
            printable_args = args
            if type(args[0]) is np.ndarray: # check for risk factor
                printable_args = args[1:]
            print(method, hashname, printable_args, key[-1],
                  *[extractRecord(r)[-1] for r in report])
        except:
            continue # key not exist

        if (math.isnan(key[-1]) or math.isinf(key[-1])): continue
        if D.get(method) is None:
            D[method] = (args, key[-1], *[extractRecord(r)[-1]
                                          for r in report])
        else:
            if (minimize and D[method][1] > key[-1]) or\
               (not minimize and D[method][1] < key[-1]):
                D[method] = (args, key[-1],
                             *[extractRecord(r)[-1]
                               for r in report])
    return D

_hashmap = None
def hash2arg(h, dirname=dirname):
    global _hashmap
    if not _hashmap:
        _hashmap = pickle.load(open(path.join(dirname, "hashmap"), 'rb'))
    return _hashmap[h]


import torch
from model.backbone.fpn import FPN, LastLevelMaxPool
from torchvision.ops import batched_nms

from gvcore.utils.types import TDataList
from gvcore.model import GenericModule

from model.backbone import make_backbone
from model.head.rpn import RPN
from model.head.roihead import ROIHead

class FasterRCNN(GenericModule):
    def __init__(self, cfg):
        super(FasterRCNN, self).__init__(cfg)

        # Components ========
        self.backbone = make_backbone(cfg)
        base_inc = self.backbone.inplanes // 8
        fpn_in_channels_list = [base_inc * 2 ** (i - 1) for i in range(1, 5)]
        self.fpn = FPN(
            in_features=["res2", "res3", "res4", "res5"],
            in_channels_list=fpn_in_channels_list,
            out_channels=256,
            strides=[4, 8, 16, 32],
            top_block=LastLevelMaxPool(),
        )
        self.rpn = RPN(cfg)
        self.roi_heads = ROIHead(cfg)

        # == Params ====================
        self.score_threshold = cfg.model.score_threshold
        self.nms_threshold = cfg.model.nms_threshold
        self.max_detections = cfg.model.max_detections

        self.feats = None

    def forward_train(self, data_list: TDataList):
        imgs = torch.stack([data.img for data in data_list], dim=0)
        labels = [data.label for data in data_list]
        img_sizes = [data.meta.cur_size for data in data_list]

        feats = self.backbone(imgs)
        feats = self.fpn(feats)
        self.feats = feats

        proposals, rpn_loss_dict = self.rpn(feats, img_sizes, labels)
        roi_loss_dict = self.roi_heads(feats, proposals, labels)

        loss_dict = {}
        loss_dict.update(rpn_loss_dict)
        loss_dict.update(roi_loss_dict)
        return loss_dict

    def forward_eval(self, data_list: TDataList):
        imgs = torch.stack([data.img for data in data_list], dim=0)
        img_sizes = [data.meta.cur_size for data in data_list]

        feats = self.backbone(imgs)
        feats = self.fpn(feats)
        self.feats = feats

        proposals = self.rpn(feats, img_sizes)
        detected_scores, detected_boxes = self.roi_heads(feats, proposals, img_sizes)

        detected_bboxes = []
        for score, box in zip(detected_scores, detected_boxes):
            filter_mask = torch.gt(score, self.score_threshold)
            cls = torch.nonzero(filter_mask, as_tuple=True)[1]
            box = box[filter_mask]
            score = score[filter_mask]

            keep = batched_nms(box, score, cls, self.nms_threshold)
            keep = keep[: self.max_detections]
            box, score, cls = box[keep], score[keep], cls[keep]
            detected_bbox = torch.cat((box, cls.view(-1, 1), score.view(-1, 1)), dim=1)
            detected_bboxes.append(detected_bbox)

        return detected_bboxes


from typing import List

from dependency_injector.wiring import Provide, inject
from fastapi import APIRouter, Depends, HTTPException

from ..containers.services import Application
from ..schemas.session import Session, SessionCreate, SessionUpdate
from ..services.session import SessionService

router = APIRouter(tags=["session"])

@router.post("/api/v1/session", response_model=Session)
@inject
async def create_session(
    session: SessionCreate,
    session_service: SessionService = Depends(
        Provide[Application.session.session_service]
    ),
):
    params = dict(session)
    params["user_date"] = params["user_date"].replace(tzinfo=None)

    checker = await session_service.get_session(**params) is not None
    if checker:
        raise HTTPException(status_code=400, detail="Number message exists")

    return await session_service.create_session(**params)

@router.put("/api/v1/session", response_model=Session)
@inject
async def update_session(
    session: SessionUpdate,
    session_service: SessionService = Depends(
        Provide[Application.session.session_service]
    ),
):
    params = dict(session)
    params["ai_date"] = params["ai_date"].replace(tzinfo=None)

    checker = await session_service.get_session(**params)

    if checker.first() is None:
        raise HTTPException(
            status_code=404, detail="Session or number message no exists"
        )

    update = await session_service.update_session(**params)

    return update

@router.get("/api/v1/session/{session_id}", response_model=Session | List[Session])
@inject
async def get_session(
    session_id: int,
    session_service: SessionService = Depends(
        Provide[Application.session.session_service]
    ),
):
    params = dict(session_id=session_id)

    session = await session_service.get_session(**params)
    session = [i[0] for i in session]

    if session is None:
        raise HTTPException(status_code=404, detail="Session not found")

    return session


import datetime

from django.test import TestCase
from django.urls import reverse
from django.utils import timezone

from .models import Question

def create_question(question_text: str, days: int):
    """
    Create a question with the given 'question_text', published given
    number of 'days' offset to now (negative for questions published in
    the past, while positive for questions yet to be published)
    """
    pub_date = timezone.now() + datetime.timedelta(days=days)
    return Question.objects.create(question_text=question_text, pub_date=pub_date)

class QuestionModelTests(TestCase):
    def test_was_published_recently_with_future_date(self):
        """was_published_recently() returns False for questions whose pub_date is in the future"""
        pub_date = timezone.now() + datetime.timedelta(days=30)
        future_question = Question(pub_date=pub_date)
        self.assertIs(future_question.was_published_recently(), False)

    def test_was_published_recently_with_old_question(self):
        """was_published_recently() returns False for questions whose pub_date is older than 1 day"""
        pub_date = timezone.now() - datetime.timedelta(days=1, seconds=1)
        old_question = Question(pub_date=pub_date)
        self.assertIs(old_question.was_published_recently(), False)

    def test_was_published_recently_with_recent_question(self):
        """was_published_recently() returns True for questions whose pub_date is within the last day"""
        pub_date = timezone.now() - datetime.timedelta(hours=23, minutes=59, seconds=59)
        recent_question = Question(pub_date=pub_date)
        self.assertIs(recent_question.was_published_recently(), True)

class QuestionIndexViewTests(TestCase):
    def test_no_questions(self):
        """If no questions exist, an appropriate message is displayed"""
        response = self.client.get(reverse("polls:index"))
        self.assertEqual(response.status_code, 200)
        self.assertContains(response, "no polls are available")
        self.assertQuerysetEqual(response.context["latest_questions"], [])

    def test_past_question(self):
        """Questions with a pub_date in the past are displayed on the index page"""
        question = create_question(question_text="Ye jhakkas dikhega re biddu", days=-1)
        response = self.client.get(reverse("polls:index"))
        self.assertQuerysetEqual(response.context["latest_questions"], [question])

    def test_future_question(self):
        """Questions with a pub_date in the future are not displayed on the index page"""
        create_question(question_text="Ye nhi dikhega re biddu", days=1)
        response = self.client.get(reverse("polls:index"))
        self.assertQuerysetEqual(response.context["latest_questions"], [])

    def test_future_and_past_questions(self):
        """If questions exist with both past and future pub_dates, only questions with past pub_dates are diplayed"""
        question = create_question(question_text="Aila, ye question dikhega", days=-5)
        create_question(question_text="magar ye nhi dikhega!", days=50)
        response = self.client.get(reverse("polls:index"))
        self.assertQuerysetEqual(response.context["latest_questions"], [question])

    def test_multiple_past_questions(self):
        """Index page may display multiple questions"""
        question1 = create_question(question_text="Mai dikhuga na bhaiya??", days=-100)
        question2 = create_question(
            question_text="Han chhote, bilkul meri tarah tu bhi dikhega", days=-10
        )
        response = self.client.get(reverse("polls:index"))
        # While asserting, it is important to order the test question objects according to how
        # the fetched queryset will be ordered. Hence, owing to this, an assertion on [question1, question2]
        # would fail, as the fetched queryset will be ordered by "pub_date" in a descending order
        self.assertQuerysetEqual(
            response.context["latest_questions"], [question2, question1]
        )

class QuestionDetailViewTests(TestCase):
    def test_future_question(self):
        """The detail view of a question with a future pub_date returns a 404 not found"""
        future_question = create_question(
            question_text="Ayy merko kaise request kr rha h be?", days=1
        )
        url = reverse(viewname="polls:detail", args=(future_question.pk,))
        response = self.client.get(url)
        self.assertEqual(response.status_code, 404)

    def test_past_question(self):
        """The detail view of a question with pub_date in the past returns the requested question object"""
        past_question = create_question(
            question_text="Accha theek h, merko kr skta h request!", days=-1
        )
        url = reverse(viewname="polls:detail", args=(past_question.pk,))
        response = self.client.get(url)
        self.assertEqual(response.context["question"], past_question)


def make_current_plot(savefig=False, plt_style='default'):
    ''' Queries NASA exoplanet archive and plots all confirmed exoplanets in semi-major axis
        vs mass, colored by discovery method.
        Inputs:
          savefig (bool): Set to true, write out figure (prompts for filename)
          plt_style (str): Specify the plotting style
        Returns
          figure
    '''
    from astropy import units as u
    import pandas as pd
    from astropy.table import QTable, Table
    import numpy as np
    from astroquery.nasa_exoplanet_archive import NasaExoplanetArchive
    import matplotlib.pyplot as plt
    from matplotlib import cm
    import astropy.constants as c

    print('Querying database...')
    eod_table = NasaExoplanetArchive.get_confirmed_planets_table()
    # Convert QTable to regular astropy Table object for easy referencing:
    e = Table(eod_table)
    # Convert to pandas df
    cols = e.colnames[0:len(e.colnames)-1]
    ee = e[cols].filled(-9999)
    eod_table = ee.to_pandas()

    d=eod_table.loc[np.where(eod_table['pl_discmethod']=='Imaging')[0]]
    r=eod_table.loc[np.where(eod_table['pl_discmethod']=='Radial Velocity')[0]]
    t=eod_table.loc[np.where(eod_table['pl_discmethod']=='Transit')[0]]
    m=eod_table.loc[np.where(eod_table['pl_discmethod']=='Microlensing')[0]]
    ind = [d,r,t,m]
    labels=['Imaging','Radial Velocity','Transit','Microlensing']

    print('Making plot...')
    from datetime import date, datetime
    now = str(date.today())

    plt.style.use(plt_style)
    figsize = (8,6)
    fig = plt.figure(figsize=figsize)
    start = 0.0
    stop = 1.0
    number_of_lines= 4
    cm_subsection = np.linspace(start, stop, number_of_lines)

    colors = [ cm.brg(x) for x in cm_subsection ]

    for i, color, label in zip(ind,colors,labels):
        plt.scatter(i['pl_orbsmax'],i['pl_bmassj'],alpha=0.5,marker='.',color=color,label=label)

        plt.scatter(5,1,alpha=0.8,marker='o',color='black')
        plt.annotate('J', xy=(5,1), xytext=(5, 5),\
                         textcoords='offset points', ha='right', va='bottom', color='black')
        plt.scatter(9.5,(95*u.M_earth).to(u.M_jup),alpha=0.8,marker='o',color='black')
        plt.annotate('S', xy=(9.5,0.298), xytext=(25, 5),\
                         textcoords='offset pixels', ha='right', va='bottom', color='black')
        plt.scatter(0.7,(0.817*u.M_earth).to(u.M_jup),alpha=0.8,marker='o',color='black')
        plt.annotate('V', xy=(0.7,0.00257), xytext=(25, 15),\
                         textcoords='offset pixels', ha='right', va='bottom', color='black')
        plt.scatter(1,0.003146,alpha=0.8,marker='o',color='black')
        plt.annotate('E', xy=(1,0.003146), xytext=(25, 5),\
                         textcoords='offset pixels', ha='right', va='bottom', color='black')
        plt.scatter(19.2,0.044,alpha=0.8,marker='o',color='black')
        plt.annotate('U', xy=(19.2,0.044), xytext=(25, 5),\
                         textcoords='offset pixels', ha='right', va='bottom', color='black')
        plt.scatter(30,0.053,alpha=0.8,marker='o',color='black')
        plt.annotate('N', xy=(30,0.053), xytext=(25, 5),\
                         textcoords='offset pixels', ha='right', va='bottom', color='black')
        ax=plt.gca()
        ax.set_xscale("log")
        ax.set_yscale("log")
        ax.set_ylim(ymin=0.001)
        ax.set_xlim(xmin=0.01)
        ax.grid(True,linestyle='dotted')
        plt.xlabel('Semi-major axis (AU)')
        plt.ylabel('Mass (M$_{jup}$)')
        plt.legend()
        if savefig == True:
            out = input('Enter path and filename for plot: ')
            plt.savefig(str(out)+now+'.jpg',dpi=300)
    plt.show()
    plt.close(fig)


import random
import math
import numpy as np

VARIANT_NUMBER = 2**(14 / 4)

class Matrix:
    def __init__(self) -> None:
        pass

    @staticmethod
    def display(matrix: list, matrix_name: str):
        output = ""
        output += "Matrix: " + matrix_name + "{\n"
        for i in matrix:
            for j in i:
                output += "{0:5.3f} ".format(j)
            output += "\n"
        output += "}"
        return output

    @staticmethod
    def transpose(matrix: list):
        matrix_size = len(matrix)
        temp_matrix = [[0] * matrix_size for _ in range(matrix_size)]
        for i in range(matrix_size):
            for j in range(matrix_size):
                temp_matrix[i][j] = matrix[j][i]
        return temp_matrix

    @staticmethod
    def copy(matrix: list):
        return [[matrix[i][j] for j in range(len(matrix[i]))] for i in range(len(matrix))]

    @staticmethod
    def sum(matrix_1: list, matrix_2: list):
        result_matrix = Matrix.copy(matrix_1)
        for i in range(len(matrix_1)):
            for j in range(len(matrix_1[i])):
                result_matrix[i][j] += matrix_2[i][j]
        return result_matrix

    @staticmethod
    def scalar_multiply(matrix_1: list, lyamda: list):
        result_matrix = Matrix.copy(matrix_1)
        for i in range(len(matrix_1)):
            for j in range(len(matrix_1[i])):
                result_matrix[i][j] *= lyamda
        return result_matrix

    @staticmethod
    def dot(matrix_1: list, matrix_2: list):
        final_matrix = []
        for i in range(len(matrix_1)):
            final_matrix.append([])
            for j in range(len(matrix_2[i])):
                temp_sum = 0
                for k in range(len(matrix_1[i])):
                    temp_sum += matrix_1[i][k] * matrix_2[k][j]
                final_matrix[i].append(temp_sum)
        return final_matrix

    @staticmethod
    def cubic_norm(matrix: list):
        norm = 0
        for row in matrix:
            row_sum = 0
            for element in row:
                row_sum += math.fabs(element)
            if row_sum > norm:
                norm = row_sum
        return norm

    @staticmethod
    def cubic_norm_index(vector: list):
        norm = vector[0][0]
        index = 0
        for i in range(len(vector)):
            if math.fabs(vector[i][0]) > norm:
                norm = vector[i][0]
                index = i
        return index

    @staticmethod
    def fill(matrix_size: int):
        matrix = [[0] * matrix_size for _ in range(matrix_size)]
        for i in range(matrix_size):
            aii = 0
            for j in range(i + 1, matrix_size):
                is_negative = random.randrange(-1, 2, 2)
                current_number = random.random() * VARIANT_NUMBER * is_negative
                matrix[i][j] = current_number
                matrix[j][i] = current_number
                aii += math.fabs(current_number)
            for j in range(0, i):
                aii += math.fabs(matrix[i][j])
            matrix[i][i] = aii + 1
        return matrix

    @staticmethod
    def fill_vector(vector_size: int):
        vector = []
        for _ in range(vector_size):
            is_negative = random.randrange(-1, 2, 2)
            vector.append([random.random() * VARIANT_NUMBER * is_negative])
        return vector

    @staticmethod
    def inverse(matrix: list):
        matrix_size = len(matrix)
        temp_matrix = Matrix.copy(matrix)
        inverse_matrix = [[0] * matrix_size for _ in range(matrix_size)]

        for i in range(matrix_size):
            inverse_matrix[i][i] = 1

        for i in range(matrix_size):
            for j in range(i + 1, matrix_size):
                leader_element = temp_matrix[j][i] / temp_matrix[i][i]
                for k in range(0, matrix_size):
                    temp_matrix[j][k] -= temp_matrix[i][k] * leader_element
                    inverse_matrix[j][k] -= inverse_matrix[i][k] * \
                        leader_element

        for i in range(matrix_size - 1, -1, -1):
            for j in range(i - 1, -1, -1):
                leader_element = temp_matrix[j][i] / temp_matrix[i][i]
                for k in range(matrix_size - 1, -1, -1):
                    temp_matrix[j][k] -= temp_matrix[i][k] * leader_element
                    inverse_matrix[j][k] -= inverse_matrix[i][k] * \
                        leader_element

        inverse_matrix = [[inverse_matrix[i][j] / temp_matrix[i][i]
                           for j in range(matrix_size)] for i in range(matrix_size)]

        return inverse_matrix

    @staticmethod
    def condition(matrix: list):
        return Matrix.cubic_norm(matrix) * Matrix.cubic_norm(Matrix.inverse(matrix))

    @staticmethod
    def vector_difference(vector_1: list, vector_2: list):
        answer = [0 for _ in range(len(vector_1))]
        for i in range(len(vector_1)):
            answer[i] = [vector_1[i][0] - vector_2[i][0]]
        return answer
    @staticmethod
    def write_matrix(matrix: list, filename: str):
        with open(filename, "w") as file:
            text = ""
            for i in range(len(matrix)):
                for j in range(len(matrix)):
                    text += str(matrix[i][j]) + " "
            file.write(text)

    @staticmethod
    def scan_matrix(filename: str, matrix_size: int):
        matrix = [[0] * matrix_size for _ in range(matrix_size)]
        with open(filename, "r") as file:
            text = file.read().split(" ")
            for i in range(matrix_size):
                for j in range(matrix_size):
                    matrix[i][j] = float(text[matrix_size * i + j])
        return matrix

    @staticmethod
    def normalize(matrix: list):
        norm = Matrix.cubic_norm(matrix)
        result_matrix = Matrix.copy(matrix)
        for i in range(len(result_matrix)):
            for j in range(len(result_matrix[i])):
                result_matrix[i][j] /= norm
        return result_matrix

    @staticmethod
    def max_eigen_value(matrix: list, y0: list, eps: float):
        ITER_COUNT = 100
        first_case = True
        answer_vectors = []
        y = Matrix.dot(matrix, y0)
        answer_vectors.append(y)
        prev_y_norm_index = Matrix.cubic_norm_index(y0)
        lyamda = answer_vectors[0][prev_y_norm_index][0] / y0[prev_y_norm_index][0]

        for i in range(1, ITER_COUNT):
            prev_y_norm_index = Matrix.cubic_norm_index(y)
            y = Matrix.dot(matrix, y)
            answer_vectors.append(y)
            lyamdai = answer_vectors[i][prev_y_norm_index][0] / answer_vectors[i - 1][prev_y_norm_index][0]

            if math.fabs(lyamdai - lyamda) < eps:
                return lyamdai, Matrix.normalize(answer_vectors[i])
            lyamda = lyamdai
        # return lyamda, Matrix.normalize(Matrix.sum(answer_vectors[len(answer_vectors) - 1], Matrix.scalar_multiply(answer_vectors[len(answer_vectors) - 2], lyamdai)))

        prev_y_norm_index = Matrix.cubic_norm_index(y0)
        lyamda = math.sqrt(math.fabs(answer_vectors[1][prev_y_norm_index][0] / y0[prev_y_norm_index][0]))

        for i in range(2, len(answer_vectors)):
            prev_y_norm_index = Matrix.cubic_norm_index(answer_vectors[i - 2])
            lyamdai = math.sqrt(math.fabs(answer_vectors[i][prev_y_norm_index][0] / answer_vectors[i - 2][prev_y_norm_index][0]))
            if math.fabs(lyamdai - lyamda) < eps:
                return lyamdai, Matrix.normalize(Matrix.sum(answer_vectors[len(answer_vectors) - 1], Matrix.scalar_multiply(answer_vectors[len(answer_vectors) - 2], lyamdai)))
            lyamda = lyamdai

        return lyamda, Matrix.normalize(Matrix.sum(answer_vectors[len(answer_vectors) - 1], Matrix.scalar_multiply(answer_vectors[len(answer_vectors) - 2], lyamdai)))


# coding=utf-8
from arrow import Arrow
from models.scenic_point import ScenicPoint
from models.tour_plan import TourPlan
from utils.baidu_map_utils import get_route_matrix
from utils.dao_utils.mongo_utils import get_db_inst

__author__ = 'jayvee'

def route_planner(point_list, start_point, start_time=None):
    """
    根据选出的景点进行计划规划，本模块的核心函数
    :param point_list: ScenicPoint类型的数组
    :param start_point: ScenicPoint类型的实例
    :return: TourPlan类型实例数组
    """
    if not start_time:
        start_time = Arrow.utcnow()
    pls = _gen_seq(point_list, start_point)
    route_mat = get_route_matrix(pls[0][:-1])
    for p_seq in pls:
        plan = TourPlan(start_time, p_seq)
        cost = _calc_plan_cost(plan, route_mat)

def _create_scenic_point_by_title(title):
    """
    根据title生成ScenicPoint对象实例
    :param title:
    :return:
    """
    db_sp = get_db_inst('ProjectNavi', 'ScenicPoins')
    find_result = db_sp.find_one({'title': title})
    sp = ScenicPoint(find_result)
    return sp

def _calc_plan_cost(plan, route_matrix):
    """
    计算某个计划的损失值
    :param plan:
    :param route_matrix:
    :return:
    """
    cur_time = plan.start_time
    sps = plan.get_scenic_points()
    last = sps[0]
    cost = 0
    for sp in sps[1:-1]:
        duration = route_matrix[last.md5][sp.md5]['duration']
        cur_time = cur_time.replace(minute=+duration)
        cur_hour = cur_time.hour() # todo 转换ScenicPoint中的时间范围变成datetime

def _gen_seq(point_list, start_point):
    """
    枚举所有可能的顺序序列
    :param point_list: 形如[ScenicPoint]的数组
    :param start_point:
    :return:
    """
    # plist = []
    # plist.remove()
    def dfs(plist):
        """
        生成全排列
        :param plist:
        :return:
        """
        if len(plist) > 0:
            res = []
            if len(plist) > 1:
                for i in xrange(len(plist)):
                    # tmp.append(plist[i])
                    t = plist[i]
                    # tt = plist[0]
                    plist[i] = plist[0]
                    plist[0] = t
                    r = dfs(plist[1:])
                    tmp = [t]
                    for j in r:
                        tmp.extend(j)
                        res.append(tmp)
                        tmp = [t]
                        # tmp = tmp[:-1]
                return res
            else:
                return [[plist[0]]]
        else:
            return []

    a_seq = dfs(point_list)
    result = []
    for seq in a_seq:
        result.append([start_point] + seq + [start_point])
    return result

if __name__ == '__main__':
    # s1 = _create_scenic_point_by_title('北京大学')
    # s2 = _create_scenic_point_by_title('颐和园')
    # s3 = _create_scenic_point_by_title('玉渊潭公园')
    # home = _create_scenic_point_by_title('天安门广场')
    # pls = _gen_seq([s1, s2, s3], home)
    # plan = TourPlan(Arrow.utcnow(), pls[0])
    # _calc_plan_cost(plan)
    pass


# Tab1
# MATH

import node_calculator.core as noca

a = noca.Node("A_geo")
b = noca.Node("B_geo")
c = noca.Node("C_geo")

# Simple math operation
a.tx + 2

# Nodes are setup automatically
a.tx - 2

# Multiple math operations
a.tx * 2 + 3

# Assignment of math operation result
c.ty = a.tx * 2 + 3

# Tab2
# MATH

import node_calculator.core as noca

a = noca.Node("A_geo")
b = noca.Node("B_geo")
c = noca.Node("C_geo")

# Correct order of operations. Thanks, Python!
c.ty = a.tx + b.ty / 2
c.ty = (a.tx + b.ty) / 2

# Works with 1D, 2D & 3D. Mixes well with 1D attrs!
c.t = b.t * a.t - [1, 2, 3]
c.t = b.t * a.ty

# Intermediate results ("bad" isn't necessarily bad)
a_third = a.t / 3
c.t = a_third

# Tab3
# MATH

import node_calculator.core as noca

a = noca.Node("A_geo")
b = noca.Node("B_geo")
c = noca.Node("C_geo")

# Additional operations via Op-class
c.tx = noca.Op.length(a.t, b.t)

# Available Operators
help(noca.Op)
noca.Op.available(full=False)
help(noca.Op.length)

# Conditionals are fun
c.t = noca.Op.condition(2 - b.ty > 3, b.t, [0, 3, 0])

# Warning: It's Python!
# Or: "The reason for Nodes of values/lists".
c.t = [a.ty, a.tx, 2] - 5
c.t = [a.ty, a.tx, 2] * [1, 4, b.tx]


# https://school.programmers.co.kr/learn/courses/30/lessons/178870

def solution(sequence, k):
    total = 0
    start = 0
    end = 0
    st_record = 0
    isAddedLast = False
    sq_len = len(sequence)
    while start < len(sequence):
        if not isAddedLast:
            s = sequence[end]
            total += s
        elif total <= k:
            break
        # print(start, end, sq_len, total)
        if k < total:
            total -= sequence[start] + s
            start += 1
            continue

        if k == total:
            l = end - start
            if l < sq_len:
                # print('updated!', sq_len, 'to', l)
                sq_len = l
                st_record = start
        if end + 1 < len(sequence):
            end += 1
        else:
            isAddedLast = True
    return [st_record, sq_len + st_record]


# -*- coding: utf-8 -*-
"""
Created on Tue May  7 06:07:24 2019

@author: Anupam Sharma
"""

"""
Code Challenge
  Name:
    Replacing of Characters
  Filename:
    restart.py
  Problem Statement:
    In a hardcoded string RESTART, replace all the R with $ except the first occurrence and print it.
  Input:
    RESTART
  Output:
    RESTA$T
"""
a = "RESTART"
f = a.rfind("R")
print(a[:f] + "$" + a[f+1:])


import pyfiglet

import numpy as np
from Environments.base import Environment

# Autoencoder for font denoising!

"""     #######
        #       #  ####  #      ###### #####
        #       # #    # #      #        #
        #####   # #      #      #####    #
        #       # #  ### #      #        #
        #       # #    # #      #        #
        #       #  ####  ###### ######   #      """

# http://www.figlet.org/

class FigletFonts(Environment):

    def __init__(self, font='banner', noise=0., autoencoder=False, ascii_vals=None):
        self.noise = noise
        self.autoencoder = autoencoder

        self.data = []
        self.character_set = set()

        self.max_width = 0

        if not ascii_vals:
            ascii_vals = [i for i in range(32, 127)]
        self.ascii_vals = ascii_vals

        # Initial pass to determine letter width
        for letter in ascii_vals:
            raw = pyfiglet.figlet_format(chr(letter), font=font)

            width = len(raw.splitlines()[0])
            self.max_width = self.max_width if self.max_width > width else width

            numerical = []
            for line in raw.splitlines():
                numerical.append([ord(cha) for cha in line])

            processed = np.array(numerical)
            self.character_set.update(np.unique(processed))
            self.data.append(np.array(processed))

        self.character_set = {idx: char for idx, char in enumerate(self.character_set)}

        # Convert stimuli to padded categorical flattened arrays
        for idx in range(len(self.data)):
            for classidx, char_class in self.character_set.items():
                self.data[idx][np.where(self.data[idx] == char_class)] = classidx

            self.data[idx] = np.pad(self.data[idx], ((0, 0), (0, self.max_width - self.data[idx].shape[1])), 'constant')
            self.data[idx] = self.data[idx].flatten()

        self.data = np.array(self.data)
        self.expected = np.eye(len(ascii_vals))

    def sample(self, quantity=1):
        samples = np.random.randint(len(self.ascii_vals), size=quantity)

        if self.noise:
            generated_noise = np.random.normal(
                0., scale=len(self.character_set) // 2,
                size=self.data[samples].shape).astype(int)

            mask = np.random.binomial(
                1, self.noise,
                size=self.data[samples].shape)

            stimulus = np.mod(
                self.data[samples] + generated_noise * mask,
                len(self.character_set))
        else:
            stimulus = self.data[samples]

        stimulus = np.atleast_3d(stimulus)
        expected = np.atleast_3d(self.data[samples])

        return {'stimulus': stimulus, 'expected': expected}

    def survey(self, quantity=None):
        if not quantity:
            quantity = len(self.ascii_vals)
        # samples = np.linspace(0, len(self.ascii_vals) - 1, quantity).astype(int)  # Size changes error granularity
        samples = np.random.randint(len(self.ascii_vals), size=quantity)

        if self.noise:
            generated_noise = np.random.normal(
                0., scale=len(self.character_set) // 2,
                size=self.data[samples].shape).astype(int)

            mask = np.random.binomial(
                1, self.noise,
                size=self.data[samples].shape)

            stimulus = np.mod(
                self.data[samples] + generated_noise * mask,
                len(self.character_set))
        else:
            stimulus = self.data[samples]

        stimulus = np.atleast_3d(stimulus)
        expected = np.atleast_3d(self.data[samples])

        return {'stimulus': stimulus, 'expected': expected}

    def output_nodes(self, tag=None):
        return self.data.shape[-1]

    def plot(self, plt, predict):
        # Do not attempt to plot an image
        pass

    def error(self, expect, predict):
        if self.autoencoder:
            x = np.random.randint(0, expect.shape[1])
            print(self.reformat(predict[x]))
            print(self.reformat(expect[x]))
            return np.linalg.norm(expect - predict)

        predict_id = np.argmax(predict, axis=0)
        expect_id = np.argmax(expect, axis=0)

        return int((1.0 - np.mean((predict_id == expect_id).astype(float))) * 100)

    def reformat(self, data):
        data = np.round(np.clip(data.reshape((-1, self.max_width)), 0, len(self.character_set) - 1)).astype(int)
        ascii_valued = np.zeros(data.shape)
        for classidx, char_class in self.character_set.items():
            ascii_valued[np.where(data == classidx)] = char_class

        output = ''
        for line in ascii_valued:
            output += ''.join([chr(int(round(symbol))) for symbol in line]) + '\n'
        return output


import struct

from src.helpers.conversion_helper import macFromBytes, capabilityFromBytes
from src.models.lldp_model import *
from src.models.eth_model import ETH_frame

def parseLLDPFrame(frame, intName):
    ethType, = struct.unpack('!H', frame[12:14])
    # Checking LLDP protocol
    if ethType != LLDP_PROTO_ID:
        return None
    parsedFrame = ETH_frame(macFromBytes(frame[6:12]))

    frameLength = len(frame)
    lldp = frame[14:frameLength]

    capability = "-"
    while True:
        tl, = struct.unpack('!H', lldp[0:2])
        tlvType = tl >> LLDP_TLV_LEN_BIT_LEN
        tlvLen = (tl & LLDP_TLV_LEN_MASK)
        if tlvType == LLDP_TLV_TYPE_PDUEND:
            break
        elif tlvType == LLDP_TLV_TYPE_CHASSISID:
            chasisID = macFromBytes(lldp[3:9])
            lldp = lldp[9:]
        elif tlvType == LLDP_TLV_TYPE_PORTID:
            portID = lldp[3:2 + tlvLen].decode()
            lldp = lldp[2 + tlvLen:]
        elif tlvType == LLDP_TLV_TYPE_CAP:
            capability = capabilityFromBytes(lldp[3:2 + tlvLen])
            lldp = lldp[2 + tlvLen:]
        else:
            lldp = lldp[2 + tlvLen:]
    return LLDP_entry(chasisID, intName, portID, capability)


from flask import Flask, render_template, request, send_file
from werkzeug import secure_filename
import pandas
import geopy
from geopy import Nominatim
from geopy.exc import GeocoderTimedOut

geolocator = Nominatim(user_agent="my-application")

def do_geocode(address):
    try:
        return geolocator.geocode(address)
    except GeocoderTimedOut:
        return do_geocode(address)

app=Flask(__name__)

@app.route("/")
def index():
    return render_template("index.html")

@app.route("/success", methods=['POST'])
def success():
    global file
    global df
    try:
        if request.method=='POST':
            file=request.files["file"]
            file.save(secure_filename("uploaded"+file.filename))
            df = pandas.read_csv("uploaded"+file.filename)
            #df = df.iloc[1:5,2:]
            if ("Address" in df.columns) or ("address" in df.columns):
                df["city_coord"] = df["address"].apply(do_geocode)
                df['latitude'] = df['city_coord'].apply(lambda x: x.latitude if x != None else None )
                df['longitude'] = df['city_coord'].apply(lambda x: x.longitude if x != None else None)
                df = df.drop(columns="city_coord")
                df.to_csv("uploaded"+file.filename)
                return render_template("index.html", btn="download.html", data=df.to_html())
            else:
                return render_template("index.html", text="No column with header address or Address present")
    except Exception as e:
        return render_template("index.html", text=str(e))

@app.route("/download")
def download():
    return send_file("uploaded"+file.filename, attachment_filename="yourfile.csv", as_attachment=True)

if __name__ == '__main__':
    app.debug=True
    app.run()


import time
import re
import click
import subprocess
import questionary
import usb.core
import usb.util
from photo_buddy.data.data_loader import UsbId
from photo_buddy.backup import UsbDevices
import photo_buddy.gphoto2_utils as gputils
import pymtp
from gi.repository import GLib, Gio
from pathlib import Path

def get_usb_devices_list(type='imaging'):
    usbid = UsbId()
    if type == 'imaging':
        devices_raw = usb.core.find(find_all=True, custom_match=find_class(6))
    else:
        devices_raw = usb.core.find(find_all=True)
    devices = []
    for d in devices_raw:
        ret = usbid.get_usbid_names("{0:0{1}x}".format(d.idVendor, 4), "{0:0{1}x}".format(d.idProduct, 4))
        if ret:
            sn = usb.util.get_string(d, d.iSerialNumber)

            devices.append(' '.join(
                [v for v in ret if v is not None] +
                [sn if sn else '']
            ))

    return devices

class QuestionaryOption(click.Option):

    def __init__(self, param_decls=None, **attrs):
        click.Option.__init__(self, param_decls, **attrs)
        if not isinstance(self.type, click.Choice):
            raise Exception('ChoiceOption type arg must be click.Choice')

    def prompt_for_value(self, ctx):
        val = questionary.select(self.prompt, choices=get_usb_devices_list()).unsafe_ask()
        return val

class find_class(object):
    def __init__(self, class_):
        self._class = class_
    def __call__(self, device):
        # first, let's check the device
        if device.bDeviceClass == self._class:
            return True
        # ok, transverse all devices to find an
        # interface that matches our class
        for cfg in device:
            # find_descriptor: what's it?
            intf = usb.util.find_descriptor(
                                        cfg,
                                        bInterfaceClass=self._class
                                )
            if intf is not None:
                return True

        return False

@click.group()
def run():
    pass

@run.command()
@click.option('--src', '-s', prompt='Device', type=click.Choice(['sony-xa2', 'icloud']), cls=QuestionaryOption)
@click.option('--des', '-d', type=click.Choice(['syno']), cls=QuestionaryOption)
@click.option('--uid', default='1000')
def backup(src, des, uid):
    if src == 'sony-xa2':
        pfind = subprocess.Popen(
            ['find', f'/var/run/user/{uid}/gvfs/', '-name', 'mtp:*'],
            stderr=subprocess.DEVNULL
        )
        out, err = pfind.communicate()
        click.echo(out)

@run.command()
@click.option('--usb-device', '-u', help="add usb device", is_flag=True)
@click.option('--imaging', '-i', help="filter usb imaging devices", is_flag=True, default=True)

def add_backup_point(usb_device, imaging):
    """ scanning connected devices
    """
    if usb_device:
        val = questionary.select("devices found(vendor, product, serial number)", choices=UsbDevices.get_usb_devices_list()).unsafe_ask()

@run.command()
def mtp():
    device = pymtp.MTP()
    device.connect()
    print(device)

@run.command()
@click.option('--list-devices', '-l', help="list connected PTP devices", is_flag=True)
def ptp(list_devices):
    pshowdevices = subprocess.Popen(
        ['gphoto2', '--auto-detect'],
        stderr=subprocess.DEVNULL
    )
    out, err = pshowdevices.communicate()
    click.echo(out)
    pass

def callback_unmount_gio(obj, result):
    pass
    click.echo('callback' + result)
    return True

def umount_gphoto2_usb_gio(usb_addr):
    """unmount if already mounted(path exists)

    Args:
        usb_addr:

    Returns:

    """
    addr1, addr2 = usb_addr.split(',')
    # try to unmount
    punmount = subprocess.Popen(
        ['gio', 'mount', '-uf', f'gphoto2://%5Busb%3A{addr1}%2C{addr2}%5D'],
        stderr=subprocess.DEVNULL
    )
    out, err = punmount.communicate()
    if out:
        click.echo(out)

@run.command()
@click.option('--list-devices', '-l',
              help="list connected devices supporting gphoto2"
                   "(hint:connect your device in PTP)",
              is_flag=True)
@click.option('--auto-detect', '-a',
              help="auto detect camera connected with gphoto2"
                   "(hint:connect your device in PTP)"
                   " and apply further commands",
              is_flag=True)
@click.option('--list-files', '-f',
              help="missing text",
              is_flag=True)
@click.option('--download-all', '-o',
              help="download all files",
              is_flag=True)
@click.option('--des', '-d',
              help="download destination",
              type=Path)
def gp2(list_devices, auto_detect, list_files,download_all, des):
    """ gphoto 2

    Args:
        list_devices:
        auto_detect:
        list_files:

    Returns:

    """
    if list_devices:
        cameras = gputils.get_camera_list()
        click.echo('\n'.join([c[0] for c in cameras]))
    elif auto_detect:
        cameras = gputils.get_camera_list()
        if len(cameras) > 1 :
            camera_str = questionary.select(
                "devices found(name, addr)",
                choices=[c[0] + ', ' + c[1] for c in cameras]).unsafe_ask()
        else:
            camera_str = cameras[0][0] + ', ' + cameras[0][1]
            click.echo(f'Select found camera, {camera_str}')

    camera_usbaddr = [c[1] for c in cameras if c[1] in camera_str][0]
    #unmount
    umount_gphoto2_usb_gio(camera_usbaddr.split(':')[1])
    #kill processes
    # pkill -f gphoto2
    click.echo(f'Init camera and load meta data')
    camera = gputils.init_camera(*[c[1] for c in cameras if c[1] in camera_str])

    if list_files:
        click.echo(gputils.get_file_list(camera))

    if download_all and not des or not download_all and des:
        click.echo("Missing argument, either --des or --download-all")

    if download_all and des:
        gputils.download_all(camera, des)

    camera.exit()

if __name__ ==  '__main__':
    pass
    import sys
    run(sys.argv[1:])


_base_ = [
    '../../universenet/models/universenet50_2008.py',
    '../../_base_/datasets/coco_detection_mstrain_480_960.py',
    '../../_base_/schedules/schedule_1x.py', '../../_base_/default_runtime.py'
]
pretrained_ckpt = 'https://shanghuagao.oss-cn-beijing.aliyuncs.com/res2net/res2net50_26w_4s-06e79181.pth'  # noqa
model = dict(
    backbone=dict(
        type='Res2Net',
        depth=50,
        scales=4,
        base_width=26,
        num_stages=4,
        out_indices=(0, 1, 2, 3),
        deep_stem=False,
        avg_down=False,
        frozen_stages=1,
        norm_cfg=dict(type='SyncBN', requires_grad=True),
        norm_eval=False,
        style='pytorch',
        dcn=dict(type='DCN', deform_groups=1, fallback_on_stride=False),
        stage_with_dcn=(False, False, False, True),
        init_cfg=dict(type='Pretrained', checkpoint=pretrained_ckpt)))

data = dict(samples_per_gpu=4)

optimizer = dict(type='SGD', lr=0.01, momentum=0.9, weight_decay=0.0001)
optimizer_config = dict(
    _delete_=True, grad_clip=dict(max_norm=35, norm_type=2))
lr_config = dict(warmup_iters=1000)

fp16 = dict(loss_scale=512.)


#
#   Statistics test
#

import numpy as np
from scipy.stats.distributions import chi2
import warnings
from functools import reduce
from operator import mul

# chisquare_trend_contingency

def chisquare_trend_contingency (observed, tendencies_values, axis = None):

    """
        chisquare_trend_contingency
        Apply a Chi2 square Cochran-Armitage trend test with variable in a contingency table.
        It measure the effect of an tendencie variable on a evaluated variable.
        The evaluated variable should be a categorial variable with 2 modalities. It should be of shape 2.
        The tendencies values should be of shape >= 2 an be numeric.

        Validation conditions
        ----------
            - Monotonous evolution of the tendencie (not checked by the function)
            - Theorical value >= 5 : checked by the function

        Parameters
        ----------
            observed, numpy matrix : contingency table of observed value. Should have an axis with a shape of 2 as the test is designed to evaluate tendencies on 2 modalities categorial variable. If only an axis is of shape 2, this axis is used for applying the test, otherwise you should specify the axis containing the tested variable with the axis parameter.
            tendencies_values, numpy vector : values of the tendencie variable, should be an 1D vector of shape >= 2

        Returns
        ----
        chi2 : float
            The test statistic.
        p : float
            The p-value of the test
        dof : int
            Degrees of freedom
        expected : ndarray, same shape as `observed`
            The expected frequencies, based on the marginal sums of the table.
    """

    # Making all numpy
    observed = np.array(observed)
    tendencies_values = np.array(tendencies_values)

    # Checking data

    ## Observed
    observed_shape = np.array(observed.shape)
    if ((observed_shape == 2).sum() == 0):
        # Exception if absence of shape 2
        raise Exception("Observed should contains at list of element of shape 2.")

    ## Tendencies values
    if(tendencies_values.ndim != 1):
        # Exception if dim different of 1
        raise Exception("Tendencies values should be an 1D array")
    elif(tendencies_values.shape not in observed_shape):
        raise Exception("Tendencies values should be contains the same number of element than observed")

    # Calculating parameters
    n = observed.sum() # Number of observations

    ## Getting axis containing the evaluated variable
    tmp_axis_id = (np.array(observed.shape) == 2) ## Check which axis is of shape 2
    if (tmp_axis_id.sum() == 1):
        # If there is only one axis with shape 2 : it is the evaluated variable
        axis_id = np.where(tmp_axis_id)[0][0]
    else:
        # Taking the content of the axis variables
        axis_id = axis

        if (type(axis_id) != type(int())):
            raise Exception("Observed values are of shape (2,2), you should specify the axis of the evaluated variable with the axis parameter (0 or 1)")

    # Getting theorical values
    expected = (observed.sum(axis = 1)/n).reshape(-1, 1)*observed.sum(axis = 0)

    # Getting dof
    dof = 1 # By desing in this test, this is a 1 dof

    # Checking condition parameter
    ## Every expected value should be >= 5
    if ((expected < 5).sum() != 0):
        warnings.warn("Test condition not respected, every expected value should be >= 5")

    # Getting test parameter
    chi2_parameter = (
        (
            np.power(n, 3)
            *np.square(sum(tendencies_values*(np.take(observed, 0, axis_id)-np.take(expected, 0, axis_id))))
        )/(
            reduce(mul, observed.sum(axis = (1-axis_id)))*
            (
                n*(observed.sum(axis = axis_id)*np.square(tendencies_values)).sum()
                -np.square((observed.sum(axis = axis_id)*(tendencies_values)).sum())
            )
        )
    )

    # Getting p
    p = chi2.sf(chi2_parameter, dof)

    # Returning result
    return(chi2_parameter, p, dof, expected)


#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 30/11/2021 22:19
# @Author  : Mr. Y
# @Site    :
# @File    : similarity_indicators.py
# @Software: PyCharm

import numpy as np
np.seterr(divide='ignore',invalid='ignore')
import time
import os
from utils import Initialize
os.environ['KMP_DUPLICATE_LIB_OK'] = 'TRUE'

def Calculation_AUC(MatrixAdjacency_Train, MatrixAdjacency_Test, Matrix_similarity, MaxNodeNum):
    AUC_TimeStart = time.perf_counter()
    print('    Calculation AUC......')
    AUCnum = 672400

    Matrix_similarity = np.triu(Matrix_similarity - Matrix_similarity * MatrixAdjacency_Train)
    Matrix_NoExist = np.ones(MaxNodeNum) - MatrixAdjacency_Train - MatrixAdjacency_Test - np.eye(MaxNodeNum)

    Test = np.triu(MatrixAdjacency_Test)
    NoExist = np.triu(Matrix_NoExist)

    Test_num = len(np.argwhere(Test == 1))
    NoExist_num = len(np.argwhere(NoExist == 1))

    Test_rd = [int(x) for index, x in enumerate((Test_num * np.random.rand(1, AUCnum))[0])]
    NoExist_rd = [int(x) for index, x in enumerate((NoExist_num * np.random.rand(1, AUCnum))[0])]

    TestPre = Matrix_similarity * Test
    NoExistPre = Matrix_similarity * NoExist

    TestIndex = np.argwhere(Test == 1)
    Test_Data = np.array([TestPre[x[0], x[1]] for index, x in enumerate(TestIndex)]).T
    NoExistIndex = np.argwhere(NoExist == 1)
    NoExist_Data = np.array([NoExistPre[x[0], x[1]] for index, x in enumerate(NoExistIndex)]).T

    Test_rd = np.array([Test_Data[x] for index, x in enumerate(Test_rd)])
    NoExist_rd = np.array([NoExist_Data[x] for index, x in enumerate(NoExist_rd)])

    n1, n2 = 0, 0
    for num in range(AUCnum):
        if Test_rd[num] > NoExist_rd[num]:
            n1 += 1
        elif Test_rd[num] == NoExist_rd[num]:
            n2 += 0.5
        else:
            n1 += 0
    auc = float(n1 + n2) / AUCnum
    print('    AUC指标为：%f' % auc)
    AUC_TimeEnd = time.perf_counter()
    print('    AUCTime：%f s' % (AUC_TimeEnd - AUC_TimeStart))
    return auc

def AA(MatrixAdjacency_Train):
    similarity_StartTime = time.perf_counter()

    logTrain = np.log(sum(MatrixAdjacency_Train))
    logTrain = np.nan_to_num(logTrain)
    logTrain.shape = (logTrain.shape[0], 1)
    MatrixAdjacency_Train_Log = MatrixAdjacency_Train / logTrain
    MatrixAdjacency_Train_Log = np.nan_to_num(MatrixAdjacency_Train_Log)

    Matrix_similarity = np.dot(MatrixAdjacency_Train, MatrixAdjacency_Train_Log)

    similarity_EndTime = time.perf_counter()
    print("    SimilarityTime: %f s" % (similarity_EndTime - similarity_StartTime))
    return Matrix_similarity

def Jaccavrd(MatrixAdjacency_Train):
    similarity_StartTime = time.perf_counter()

    Matrix_similarity = np.dot(MatrixAdjacency_Train, MatrixAdjacency_Train)

    deg_row = sum(MatrixAdjacency_Train)
    deg_row.shape = (deg_row.shape[0], 1)
    deg_row_T = deg_row.T
    tempdeg = deg_row + deg_row_T
    temp = tempdeg - Matrix_similarity

    Matrix_similarity = Matrix_similarity / temp

    similarity_EndTime = time.perf_counter()
    print("    SimilarityTime: %f s" % (similarity_EndTime - similarity_StartTime))
    return Matrix_similarity

def RWR(MatrixAdjacency_Train):
    similarity_StartTime = time.perf_counter()

    Parameter = 0.85

    Matrix_TransitionProbobility = MatrixAdjacency_Train / sum(MatrixAdjacency_Train)
    Matrix_EYE = np.eye(MatrixAdjacency_Train.shape[0])

    Temp = Matrix_EYE - Parameter * Matrix_TransitionProbobility.T
    INV_Temp = np.linalg.inv(Temp)
    Matrix_RWR = (1 - Parameter) * np.dot(INV_Temp, Matrix_EYE)
    Matrix_similarity = Matrix_RWR + Matrix_RWR.T

    similarity_EndTime = time.perf_counter()
    print("    SimilarityTime: %f s" % (similarity_EndTime - similarity_StartTime))
    return Matrix_similarity

def Cn(MatrixAdjacency_Train):
    similarity_StartTime = time.perf_counter()
    Matrix_similarity = np.dot(MatrixAdjacency_Train, MatrixAdjacency_Train)

    similarity_EndTime = time.perf_counter()
    print("    SimilarityTime: %f s" % (similarity_EndTime - similarity_StartTime))
    return Matrix_similarity

def Salton(MatrixAdjacency_Train):
    similarity_StartTime = time.clock()
    similarity = np.dot(MatrixAdjacency_Train, MatrixAdjacency_Train)
    deg_row = sum(MatrixAdjacency_Train)
    deg_row.shape = (deg_row.shape[0], 1)
    deg_row_T = deg_row.T
    tempdeg = np.dot(deg_row, deg_row_T)
    temp = np.sqrt(tempdeg)
    Matrix_similarity = np.nan_to_num(similarity / temp)
    similarity_EndTime = time.clock()
    print("    SimilarityTime: %f s" % (similarity_EndTime - similarity_StartTime))

    return Matrix_similarity


from Game.Effects.effect_runner import PerformEffect
from Game.Effects.gain_power import GainPower

class GainPowerForCost:
    """ Represents an effect that gains power for the playing player """

    def __init__(self, zoneType):
        """ Initialize the Effect with the zoneType to gain power from """
        self.zoneType = zoneType

    def perform(self, context):
        """ Perform the Game Effect """
        for card in context.loadZone(self.zoneType):
            coroutine = PerformEffect(GainPower(card.cost), context)
            response = yield coroutine.next()
            while True:
                response = yield coroutine.send(response)


def WriteOutMultiplication(x,y):
    newStr = ""
    total = 0
    newStr += "\n" + (str("{:,}".format(x)) + " X " + str("{:,}".format(y)) + " = ") + "\n"
    for i in range(y):
        if i == 0:
            newStr += str("{:,}".format(x))
        else:
            newStr += " + " + str("{:,}".format(x))
        total += x
    newStr += " = " + str(total)
    return newStr

while True:
    firstNumber = int(input("Please type the first number that you want to multiply: "))
    secondNumber = int(input("Pleaser type what you would like to multiply your previous number by: "))
    print()
    print(WriteOutMultiplication(firstNumber, secondNumber))
    print()


import inspect
import textwrap
import types
from importlib.util import spec_from_file_location, module_from_spec

from package_1 import function_1
from package_2 import function_2

def compose_module(choice):
    if choice == "1":
        def main():
            function_1()
            function_2()

    else:
        def main():
            function_2()
            function_1()

    new_function = types.FunctionType(main.__code__, globals())
    content = inspect.getsource(new_function)
    content = textwrap.dedent(content)

    function_1_content = inspect.getsource(function_1)
    function_1_content = textwrap.dedent(function_1_content)

    function_2_content = inspect.getsource(function_2)
    function_2_content = textwrap.dedent(function_2_content)
    with open("test.py", 'w') as f:
        f.write(function_1_content + "\n\n")
        f.write(function_2_content + "\n\n")
        f.write(content + "\n\n")

def get_py_module():
    spec = spec_from_file_location("test", "./test.py")
    test_module = module_from_spec(spec)
    spec.loader.exec_module(test_module)
    return test_module

def main():
    choice = input("Type 1 or 2\n")
    compose_module(choice)
    module = get_py_module()
    module.main()

if __name__ == '__main__':
    main()


import pytest
from django.conf import settings
from mock import Mock

from monitor_web.collecting.constant import CollectStatus
from monitor_web.models.uptime_check import UptimeCheckTask

pytestmark = pytest.mark.django_db

def mock_create_flow():
    # 不提供订阅id
    task = UptimeCheckTask(pk=123, bk_biz_id=123, subscription_id=0)
    task.save = Mock()
    # 走新增流程
    task.create_subscription = Mock()
    task.create_subscription.return_value = {"subscription_id": 3}
    return task

def mock_update_flow():
    # 提供订阅id
    task = UptimeCheckTask(pk=123, bk_biz_id=123, subscription_id=123)
    task.save = Mock()
    # 走更新流程
    task.switch_off_subscription = Mock()
    task.update_subscription = Mock()
    task.switch_on_subscription = Mock()

    return task

def mock_others(mocker):
    strategy_func = mocker.patch("monitor_web.uptime_check.resources.SwitchStrategyByTaskIDResource.perform_request")
    append_metric_list_cache = mocker.patch("monitor_web.tasks.append_metric_list_cache.delay")
    subscription_instance_status = mocker.patch(
        "api.node_man.default.SubscriptionInstanceStatusResource.perform_request"
    )
    subscription_instance_status.return_value = [{"status": CollectStatus.SUCCESS}]
    return strategy_func, append_metric_list_cache, subscription_instance_status

# @pytest.mark.django_db
# class TestTaskDeploy(object):
#     # 测试插件测试流程是否正常执行
# def test_create(self, mocker):
#     settings.CELERY_ALWAYS_EAGER = True
#     task = mock_create_flow()
#     strategy_func, append_metric_list_cache, subscription_instance_status = mock_others(mocker)
#
#     strategy_func.start()
#     append_metric_list_cache.start()
#     subscription_instance_status.start()
#     result = task.deploy()
#     assert result == "success"
#     strategy_func.assert_called_once()
#     append_metric_list_cache.assert_called_once()
#     assert task.save.call_count == 2
#     task.create_subscription.assert_called_once()
#
#     strategy_func.stop()
#     append_metric_list_cache.stop()
#     subscription_instance_status.stop()
#
# # 测试部署流程是否正常执行
# def test_update(self, mocker):
#     settings.CELERY_ALWAYS_EAGER = True
#     task = mock_update_flow()
#     strategy_func, append_metric_list_cache, subscription_instance_status = mock_others(mocker)
#
#     strategy_func.start()
#     append_metric_list_cache.start()
#     subscription_instance_status.start()
#     result = task.deploy()
#     assert result == "success"
#     strategy_func.assert_called_once()
#     append_metric_list_cache.assert_called_once()
#     assert task.save.call_count == 2
#     task.switch_off_subscription.assert_called_once()
#     task.switch_on_subscription.assert_called_once()
#     task.update_subscription.assert_called_once()
#
#     strategy_func.stop()
#     append_metric_list_cache.stop()
#     subscription_instance_status.stop()