import sys
input=sys.stdin.readline
str=list(input().rstrip())
str2=list(input().rstrip())
str=[0]+str
str2=[0]+str2

lenstr=len(str)
lenstr2=len(str2)

#print(str)
#print(str2)
dp=[[0 for i in range(lenstr2)] for j in range(lenstr)]
for i in range(1,lenstr):
    for j in range(1,lenstr2):
        if str[i]==str2[j]:
            dp[i][j]=dp[i-1][j-1]+1
        else:
            dp[i][j]=max(dp[i-1][j],dp[i][j-1])
print(dp[lenstr-1][lenstr2-1])


import numpy as np
from clustering_info_meta import load_graph
from sklearn.cluster import SpectralClustering

# returns the matrix to be used for spectral clustering
def get_affinity_matrix():

    original_graph = load_graph(False)

    # initialize matrix as 10,000x10,000 zeroes
    matrix = []
    dim = 6000
    for i in range(dim):
        matrix.append([0]*dim)

    # set edges that are in the graph to '1'
    for a, bs in original_graph.items():
        for b in bs:
            matrix[a][b] = 1

    return np.asmatrix(matrix)

def grade_against_seed(label_list):
    # get a mapping of seed to datapoints
    seeds = {}
    datapoints = []
    for line in open("data/Seed.csv"):
        line = line.rstrip()
        datapoint, cluster = tuple([int(x) for x in line.split(',')])
        datapoints.append(datapoint)
        if cluster in seeds:
            seeds[cluster].add(datapoint)
        else:
            seeds[cluster] = set([datapoint])

    # get mapping of calculated label to datapoints
    labels = {}
    for datapoint in datapoints:
        label = label_list[datapoint]
        if label not in labels:
            labels[label] = set([datapoint])
        else:
            labels[label].add(datapoint)

    # get a mapping of seed label to calculated label
    taken_seed_labels = set()
    correct = 0
    for calculated_label, calculated_set in labels.items():
        scores = []
        for seed_label, seed_set in seeds.items():
            if seed_label in taken_seed_labels:
                continue
            scores.append( (len(seed_set.intersection(calculated_set)), seed_label) )
        score, seed_label = max(scores)
        print("matching seed {} to label {} gives score {}".format(seed_label, calculated_label, score))
        taken_seed_labels.add(seed_label)
        correct += score

    for label, labelset in labels.items():
        print("Label: {} -> {}".format(label, labelset))
    for seed, seedset in seeds.items():
        print("Seed: {} -> {}".format(seed, seedset))

    print("Score: {}%".format(round(correct*100/60, 2)))

def main():
    data_matrix = np.ndarray.tolist(np.loadtxt(open("Scripts/Results/pca30.csv", "rb"), delimiter=","))
    print("Creating spectral matrix...")
    # spectral_matrix = get_affinity_matrix()
    print('Running Clustering...')
    clusterer = SpectralClustering(n_clusters=10, affinity='poly') # affinity='precomputed'
    labels = clusterer.fit_predict(data_matrix)

    grade_against_seed(labels)
    print("DONE")

if __name__ == '__main__':
    main()


"""
Authors: Wouter Van Gansbeke, Simon Vandenhende
Licensed under the CC BY-NC 4.0 license (https://creativecommons.org/licenses/by-nc/4.0/)
"""
import argparse
import os
import torch

from utils.config import create_config
from utils.common_config import get_train_dataset, get_train_transformations,\
                                get_val_dataset, get_val_transformations,\
                                get_train_dataloader, get_val_dataloader,\
                                get_optimizer, get_model, adjust_learning_rate,\
                                get_criterion
from utils.ema import EMA
from utils.evaluate_utils import get_predictions, hungarian_evaluate
from utils.train_utils import selflabel_train
from termcolor import colored

# Parser
parser = argparse.ArgumentParser(description='Self-labeling')
parser.add_argument('--config_env',
                    help='Config file for the environment')
parser.add_argument('--config_exp',
                    help='Config file for the experiment')
args = parser.parse_args()

def main():
    # Retrieve config file
    p = create_config(args.config_env, args.config_exp)
    print(colored(p, 'red'))

    # Get model
    print(colored('Retrieve model', 'blue'))
    model = get_model(p, p['scan_model'])
    print(model)
    model = torch.nn.DataParallel(model)
    model = model.cuda()

    # Get criterion
    print(colored('Get loss', 'blue'))
    criterion = get_criterion(p)
    criterion.cuda()
    print(criterion)

    # CUDNN
    print(colored('Set CuDNN benchmark', 'blue'))
    torch.backends.cudnn.benchmark = True

    # Optimizer
    print(colored('Retrieve optimizer', 'blue'))
    optimizer = get_optimizer(p, model)
    print(optimizer)

    # Dataset
    print(colored('Retrieve dataset', 'blue'))

    # Transforms
    strong_transforms = get_train_transformations(p)
    val_transforms = get_val_transformations(p)
    train_dataset = get_train_dataset(p, {'standard': val_transforms, 'augment': strong_transforms},
                                        split='train', to_augmented_dataset=True)
    train_dataloader = get_train_dataloader(p, train_dataset)
    val_dataset = get_val_dataset(p, val_transforms)
    val_dataloader = get_val_dataloader(p, val_dataset)
    print(colored('Train samples %d - Val samples %d' %(len(train_dataset), len(val_dataset)), 'yellow'))

    # Checkpoint
    if os.path.exists(p['selflabel_checkpoint']):
        print(colored('Restart from checkpoint {}'.format(p['selflabel_checkpoint']), 'blue'))
        checkpoint = torch.load(p['selflabel_checkpoint'], map_location='cpu')
        model.load_state_dict(checkpoint['model'])
        optimizer.load_state_dict(checkpoint['optimizer'])
        start_epoch = checkpoint['epoch']

    else:
        print(colored('No checkpoint file at {}'.format(p['selflabel_checkpoint']), 'blue'))
        start_epoch = 0

    # EMA
    if p['use_ema']:
        ema = EMA(model, alpha=p['ema_alpha'])
    else:
        ema = None

    # Main loop
    print(colored('Starting main loop', 'blue'))

    for epoch in range(start_epoch, p['epochs']):
        print(colored('Epoch %d/%d' %(epoch+1, p['epochs']), 'yellow'))
        print(colored('-'*10, 'yellow'))

        # Adjust lr
        lr = adjust_learning_rate(p, optimizer, epoch)
        print('Adjusted learning rate to {:.5f}'.format(lr))

        # Perform self-labeling
        print('Train ...')
        selflabel_train(train_dataloader, model, criterion, optimizer, epoch, ema=ema)

        # Evaluate (To monitor progress - Not for validation)
        print('Evaluate ...')
        predictions = get_predictions(p, val_dataloader, model)
        clustering_stats = hungarian_evaluate(0, predictions, compute_confusion_matrix=False)
        print(clustering_stats)

        # Checkpoint
        print('Checkpoint ...')
        torch.save({'optimizer': optimizer.state_dict(), 'model': model.state_dict(),
                    'epoch': epoch + 1}, p['selflabel_checkpoint'])
        torch.save(model.module.state_dict(), p['selflabel_model'])

    # Evaluate and save the final model
    print(colored('Evaluate model at the end', 'blue'))
    predictions = get_predictions(p, val_dataloader, model)
    clustering_stats = hungarian_evaluate(0, predictions,
                                class_names=val_dataset.classes,
                                compute_confusion_matrix=True,
                                confusion_matrix_file=os.path.join(p['selflabel_dir'], 'confusion_matrix.png'))
    print(clustering_stats)
    torch.save(model.module.state_dict(), p['selflabel_model'])

if __name__ == "__main__":
    main()


import re

class Substitutor3000(object):
    _mp = dict()
    _reVar = '\$\{[^\$]*?\}'
    _p = re.compile(_reVar)

    def put(self, key, value):
        self._mp[key] = value

    def get(self, key):
        value = self._mp.get(key)
        return self._replace(value) if value else ''

    def _replace(self, s):
        m = self._p.search(s)
        if m:
            g = m.group(0)
            x = g[2:-1]
            r = self._mp.get(x)
            if r is None:
                r = ''
            s2 = s.replace(g, r)
            if s != s2:
                s = self._replace(s2)
        return s


import random
import copy
import numpy as np
import sys

def roulette_wheel_selection(population):
    population_fitness = sum([i['fitness'] for i in population])
    probabilities = [i['fitness']/population_fitness for i in population]
    return random.choices(population, weights=probabilities)

def tournament(population, tsize=3):
    pool = random.sample(population, tsize)
    pool.sort(key=lambda i: i['fitness'])
    return copy.deepcopy(pool[0])

def doubletournamentsmall(population, tsize=6):
    pool = random.sample(population, tsize)
    pool.sort(key=lambda i: i['tree_depth'])
    pool = pool[0:round(tsize/2)]
    pool.sort(key=lambda i: i['fitness'])
    return copy.deepcopy(pool[0])

def doubletournamentlarge(population, tsize=6):
    pool = random.sample(population, tsize)
    pool.sort(key=lambda i: i['tree_depth'])
    pool = pool[-round(tsize/2):]
    pool.sort(key=lambda i: i['fitness'])
    return copy.deepcopy(pool[0])

def samesizeind(population, tsize, ind):
    pool = random.sample(population, tsize)
    pool.sort(key=lambda i: i['fitness'])
    if abs(pool[0]['tree_depth'] - ind['tree_depth']) < 3:
        return copy.deepcopy(pool[0])
    elif abs(pool[1]['tree_depth'] - ind['tree_depth']) < 3:
        return copy.deepcopy(pool[1])
    elif abs(pool[2]['tree_depth'] - ind['tree_depth']) < 3:
            return copy.deepcopy(pool[2])
    else:
        return copy.deepcopy(pool[0])

def lexicase_selection(population):
    """
    Given an entire population, choose an individualsthat does the best on
    randomly chosen training cases. Allows for selection of 'specialist' individuals
    that do very well on some training cases even if they do poorly on others.
    :param population: A population from which to select individuals.
    :return: A selected individual from lexicase selection -- allows
             repeated individuals
    """
    # Initialise list of lexicase selections
    winner = []

    # Max or min
    #maximise_fitness = params['FITNESS_FUNCTION'].maximise

    # The flag "INVALID_SELECTION" allows for selection of invalid individuals.
    #if params['INVALID_SELECTION']:
    #    available = population
    #else:
    #    available = [i for i in population if not i.fitness == sys.maxsize]

    available = [i for i in population if not i['fitness'] == sys.maxsize]

    # Basic ensure individuals have been tested on same number of test cases, and that there is at least one test case
    assert (len(available[0]['test_case_results']) == len(available[1]['test_case_results']))
    assert (len(available[0]['test_case_results']) > 0)

    #while len(winners) < params['GENERATION_SIZE']:
        # Random ordering of test cases
    random_test_case_list = list(range(len(available[0]['test_case_results'])))
    random.shuffle(random_test_case_list)

        # Only choose from a sample not from the entire available population

    #    if params['LEXICASE_TOURNAMENT']:
    #        candidates = sample(available, params['TOURNAMENT_SIZE'])
    #    else:
    candidates = available
    candidate_size = len(candidates)

    while candidate_size > 0:
        # Calculate best score for chosen test case from candidates
        scores = []
        for ind in candidates:
            scores.append(ind['test_case_results'][random_test_case_list[0]])
        best_score = min(scores)
        #best_score = max(scores)
        #print(best_score)

        #if maximise_fitness:
        #    best_score = max(scores)
        #else:
        #    best_score = min(scores)

        # Only retain individuals who have the best score for the test case
        remaining = []
        candidate_size = 0
        for ind in candidates:
            if ind['test_case_results'][random_test_case_list[0]] == best_score:
                remaining.append(ind)
                candidate_size += 1
        candidates = remaining

            # If only one individual remains, choose that individual
        if len(candidates) == 1:
            winner.append(candidates[0])
            break

            # If this was the last test case, randomly choose an individual from remaining candidates
        elif len(random_test_case_list) == 1:

            # Penalize longer solutions
            #min_nodes = params["MAX_TREE_NODES"] + 1
            #best_ind = None
            #for ind in candidates:
            #    if ind.nodes < min_nodes:
            #        best_ind = ind
            #        min_nodes = ind.nodes
            #winner.append(best_ind)

            # Choose randomly among solutions
            winner.append(random.sample(candidates, 1)[0])
            break

            # Go to next test case and loop
        else:
            random_test_case_list.pop(0)

    # Return the population of lexicase selections.
    return copy.deepcopy(winner[0])


"""
This provides the base class of any Gaussian factor model, such as
(probabilistic) PCA, supervised PCA, and factor analysis, as well as
supervised and adversarial derivatives.

Implementing an extension model requires that the following methods be
implemented
    fit - Learns the model given data (and optionally supervised/adversarial
                       labels
    get_covariance - Given a fitted model returns the covariance matrix

For the remaining shared methods computing likelihoods etc, there are two
options, compute the covariance matrix then use the default methods from
the covariance module, or use the Sherman-Woodbury matrix identity to
invert the matrix more efficiently. Currently only the first is implemented
but left the options for future implementation.

Objects
-------
BaseGaussianFactorModel(_BaseSGDModel)
    Base class of all factor analysis models implementing any shared
    Gaussian methods.

BaseSGDModel(BaseGaussianFactorModel)
    This is the base class of models that use Tensorflow stochastic
    gradient descent for inference. This reduces boilerplate code
    associated with some of the standard methods etc.

Methods
-------
None
"""
from abc import abstractmethod, ABC

import numpy as np
from bystro.covariance._base_covariance import (  # type: ignore
    _get_stable_rank,  # type: ignore
    _conditional_score,  # type: ignore
    _conditional_score_samples,  # type: ignore
    _marginal_score,  # type: ignore
    _marginal_score_samples,  # type: ignore
    _score,  # type: ignore
    _score_samples,  # type: ignore
    _entropy,  # type: ignore
    _entropy_subset,  # type: ignore
    _mutual_information,  # type: ignore
)  # type: ignore
from numpy import linalg as la
from datetime import datetime as dt
import pytz  # type: ignore
from bystro._template_sgd_np import BaseSGDModel  # type: ignore
import torch  # type: ignore

class BaseGaussianFactorModel(BaseSGDModel, ABC):
    def __init__(self, n_components=2):
        """
        This is the base class of the model. Will never be called directly.

        Parameters
        ----------
        n_components : int,default=2
            The latent dimensionality

        Sets
        ----
        creationDate : datetime
            The date/time that the object was created
        """
        self.n_components = int(n_components)
        self.creationDate = dt.now(pytz.utc)

    @abstractmethod
    def fit(self, *args):
        """
        Fits a model given covariates X as well as option labels y in the
        supervised methods

        Parameters
        ----------
        X : np.array-like,(n_samples,n_covariates)
            The data

        other arguments

        Returns
        -------
        self : object
            The model
        """

    @abstractmethod
    def get_covariance(self):
        """
        Gets the covariance matrix defined by the model parameters

        Parameters
        ----------
        None

        Returns
        -------
        covariance : np.array-like(p,p)
            The covariance matrix
        """

    def get_precision(self):
        """
        Gets the precision matrix defined as the inverse of the covariance

        Parameters
        ----------
        None

        Returns
        -------
        precision : np.array-like(p,p)
            The inverse of the covariance matrix
        """
        covariance = self.get_covariance()
        precision = la.inv(covariance)
        return precision

    def get_stable_rank(self):
        """
        Returns the stable rank defined as
        ||A||_F^2/||A||^2

        Parameters
        ----------
        None

        Returns
        -------
        srank : float
            The stable rank. See Vershynin High dimensional probability for
            discussion, but this is a statistically stable approximation to rank
        """
        covariance = self.get_covariance()
        srank = _get_stable_rank(covariance)
        return srank

    def transform(self, X):
        """
        This returns the latent variable estimates given X

        Parameters
        ----------
        X : np array-like,(N_samples,p
            The data to transform.

        Returns
        -------
        S : np.array-like,(N_samples,n_components)
            The factor estimates
        """
        prec = self.get_precision()
        coefs = np.dot(self.W_, prec)
        S = np.dot(X, coefs.T)
        return S

    def conditional_score(
        self, X, observed_feature_idxs, weights=None, sherman_woodbury=False
    ):
        """
        Returns the predictive log-likelihood of a subset of data.

        mean(log p(X[idx==1]|X[idx==0],covariance))

        Parameters
        ----------
        X : np.array-like,(N,sum(observed_feature_idxs))
            The data

        observed_feature_idxs: np.array-like,(sum(p),)
            The observation locations

        weights : np.array-like,(N,),default=None
            The optional weights on the samples

        Returns
        -------
        avg_score : float
            Average log likelihood
        """
        if sherman_woodbury is False:
            covariance = self.get_covariance()
            avg_score = _conditional_score(
                covariance, X, observed_feature_idxs, weights=weights
            )
        else:
            raise NotImplementedError(
                "Sherman-Woodbury matrix inversion not implemented yet"
            )
        return avg_score

    def conditional_score_samples(
        self, X, observed_feature_idxs, sherman_woodbury=False
    ):
        """
        Return the conditional log likelihood of each sample, that is

        log p(X[idx==1]|X[idx==0],covariance)

        Parameters
        ----------
        X : np.array-like,(N,p)
            The data

        observed_feature_idxs: np.array-like,(p,)
            The observation locations

        Returns
        -------
        scores : float
            Log likelihood for each sample
        """
        if sherman_woodbury is False:
            covariance = self.get_covariance()
            scores = _conditional_score_samples(
                covariance, X, observed_feature_idxs
            )
        else:
            raise NotImplementedError(
                "Sherman-Woodbury matrix inversion not implemented yet"
            )
        return scores

    def marginal_score(
        self, X, observed_feature_idxs, weights=None, sherman_woodbury=False
    ):
        """
        Returns the marginal log-likelihood of a subset of data

        Parameters
        ----------
        X : np.array-like,(N,sum(idxs))
            The data

        observed_feature_idxs: np.array-like,(sum(p),)
            The observation locations

        weights : np.array-like,(N,),default=None
            The optional weights on the samples

        Returns
        -------
        avg_score : float
            Average log likelihood
        """
        if sherman_woodbury is False:
            covariance = self.get_covariance()
            avg_score = _marginal_score(
                covariance, X, observed_feature_idxs, weights=weights
            )
        else:
            raise NotImplementedError(
                "Sherman-Woodbury matrix inversion not implemented yet"
            )
        return avg_score

    def marginal_score_samples(
        self, X, observed_feature_idxs, sherman_woodbury=False
    ):
        """
        Returns the marginal log-likelihood of a subset of data

        Parameters
        ----------
        X : np.array-like,(N,sum(observed_feature_idxs))
            The data

        observed_feature_idxs: np.array-like,(sum(p),)
            The observation locations

        Returns
        -------
        scores : float
            Average log likelihood
        """
        if sherman_woodbury is False:
            covariance = self.get_covariance()
            scores = _marginal_score_samples(
                covariance, X, observed_feature_idxs
            )
        else:
            raise NotImplementedError(
                "Sherman-Woodbury matrix inversion not implemented yet"
            )
        return scores

    def score(self, X, weights=None, sherman_woodbury=False):
        """
        Returns the average log liklihood of data.

        Parameters
        ----------
        X : np.array-like,(N,sum(p))
            The data

        weights : np.array-like,(N,),default=None
            The optional weights on the samples

        Returns
        -------
        avg_score : float
            Average log likelihood
        """
        if sherman_woodbury is False:
            covariance = self.get_covariance()
            avg_score = _score(covariance, X, weights=weights)
        else:
            raise NotImplementedError(
                "Sherman-Woodbury matrix inversion not implemented yet"
            )
        return avg_score

    def score_samples(self, X, sherman_woodbury=False):
        """
        Return the log likelihood of each sample

        Parameters
        ----------
        X : np.array-like,(N,sum(p))
            The data

        Returns
        -------
        scores : float
            Log likelihood for each sample
        """
        if sherman_woodbury is False:
            covariance = self.get_covariance()
            scores = _score_samples(covariance, X)
        else:
            raise NotImplementedError(
                "Sherman-Woodbury matrix inversion not implemented yet"
            )
        return scores

    def get_entropy(self):
        """
        Computes the entropy of a Gaussian distribution parameterized by
        covariance.

        Parameters
        ----------
        None

        Returns
        -------
        entropy : float
            The differential entropy of the distribution
        """
        covariance = self.get_covariance()
        entropy = _entropy(covariance)
        return entropy

    def get_entropy_subset(self, observed_feature_idxs):
        """
        Computes the entropy of a subset of the Gaussian distribution
        parameterized by covariance.

        Parameters
        ----------
        observed_feature_idxs: np.array-like,(sum(p),)
            The observation locations

        Returns
        -------
        entropy : float
            The differential entropy of the distribution
        """
        covariance = self.get_covariance()
        entropy = _entropy_subset(covariance, observed_feature_idxs)
        return entropy

    def mutual_information(
        self, observed_feature_idxs1, observed_feature_idxs2
    ):
        """
        This computes the mutual information bewteen the two sets of
        covariates based on the model.

        Parameters
        ----------
        observed_feature_idxs1 : np.array-like,(p,)
            First group of variables

        observed_feature_idxs2 : np.array-like,(p,)
            Second group of variables

        Returns
        -------
        mutual_information : float
            The mutual information between the two variables
        """
        covariance = self.get_covariance()
        mutual_information = _mutual_information(
            covariance, observed_feature_idxs1, observed_feature_idxs2
        )
        return mutual_information

class BasePCASGDModel(BaseGaussianFactorModel, ABC):
    def __init__(
        self, n_components=2, training_options=None, prior_options=None
    ):
        """
        This is the base class of models that use stochastic
        gradient descent for inference. This reduces boilerplate code
        associated with some of the standard methods etc.

        Parameters
        ----------
        n_components : int,default=2
            The latent dimensionality

        training_options : dict,default={}
            The options for gradient descent

        prior_options : dict,default={}
            The options for priors on model parameters

        Sets
        ----
        creationDate : datetime
            The date/time that the object was created
        """
        super().__init__(n_components=n_components)

        if training_options is None:
            training_options = {}
        if prior_options is None:
            prior_options = {}

        self.training_options = self._fill_training_options(training_options)
        self.prior_options = self._fill_prior_options(prior_options)

    def _fill_training_options(self, training_options):
        """
        This sets the default parameters for stochastic gradient descent,
        our inference strategy for the model.

        Parameters
        ----------
        training_options : dict
            The original options set by the user passed as a dictionary

        Options
        -------
        n_iterations : int, default=3000
            Number of iterations to train using stochastic gradient descent

        learning_rate : float, default=1e-4
            Learning rate of gradient descent

        method : string {'Nadam'}, default='Nadam'
            The learning algorithm

        batch_size : int, default=None
            The number of observations to use at each iteration. If none
            corresponds to batch learning

        gpu_memory : int, default=1024
            The amount of memory you wish to use during training
        """
        default_options = {
            "n_iterations": 3000,
            "learning_rate": 1e-2,
            "gpu_memory": 1024,
            "method": "Nadam",
            "batch_size": 100,
            "momentum": 0.9,
        }
        tops = {**default_options, **training_options}

        default_keys = set(default_options.keys())
        final_keys = set(tops.keys())

        expected_but_missing_keys = default_keys - final_keys
        unexpected_but_present_keys = final_keys - default_keys
        if expected_but_missing_keys:
            raise ValueError(
                "the following training options were expected but not found..."
            )
        if unexpected_but_present_keys:
            raise ValueError(
                "the following training options were unrecognized but provided..."
            )
        return tops

    def _initialize_save_losses(self):
        """
        This method initializes the arrays to track relevant variables
        during training at each iteration

        Sets
        ----
        losses_likelihood : np.array(n_iterations)
            The log likelihood

        losses_prior : np.array(n_iterations)
            The log prior

        losses_posterior : np.array(n_iterations)
            The log posterior
        """
        n_iterations = self.training_options["n_iterations"]
        self.losses_likelihood = np.empty(n_iterations)
        self.losses_prior = np.empty(n_iterations)
        self.losses_posterior = np.empty(n_iterations)

    def _save_losses(self, i, log_likelihood, log_prior, log_posterior):
        """
        Saves the values of the losses at each iteration

        Parameters
        -----------
        i : int
            Current training iteration

        losses_likelihood : torch.tensor
            The log likelihood

        losses_prior : torch.tensor
            The log prior

        losses_posterior : torch.tensor
            The log posterior
        """
        self.losses_likelihood[i] = log_likelihood.detach().numpy()
        if isinstance(log_prior, np.ndarray):
            self.losses_prior[i] = log_prior
        else:
            self.losses_prior[i] = log_prior.detach().numpy()
        self.losses_posterior[i] = log_posterior.detach().numpy()

    def _transform_training_data(self, *args):
        """
        Convert a list of numpy arrays to tensors
        """
        out = []
        for arg in args:
            out.append(torch.tensor(arg))
        return out

    @abstractmethod
    def _create_prior(self):
        """
        Creates a prior on the parameters taking your trainable variable
        dictionary as input

        Parameters
        ----------
        None

        Returns
        -------
        log_prior : function
            The function representing the negative log density of the prior
        """


import pyodbc
from abc import ABC

from src.configuration import Credenciais

class Database(ABC):
    def __init__(self, dbname):
        self.dbname = dbname

    def exec_command(self, sql):
        try:
            conn = pyodbc.connect(Credenciais.POSTGRESQL, autocommit=True)
            cursor = conn.cursor()
            cursor.execute(sql)
            cursor.close()
            conn.commit()
            conn.close()
            return True
        except Exception as e:
            print(f'[database.py] Error: {e}')
            return False

    def exec_query(self, sql):
        try:
            conn = pyodbc.connect(Credenciais.POSTGRESQL, autocommit=True)
            cursor = conn.cursor()
            cursor.execute(sql)
            res = [dict((cursor.description[i][0], value) for i, value in enumerate(row)) for row in cursor.fetchall()]
            cursor.close()
            conn.commit()
            conn.close()
            return res
        except Exception as e:
            print(f'Error: {e}')
            return {}


#!/usr/bin/python
import threading
import gtk
import random
import time

gtk.gdk.threads_init()

NUM_THREADS = 10

class PyApp(gtk.Window):
    def __init__(self, threads=None):
        super(PyApp, self).__init__()

        self.connect("destroy", self.quit)
        self.set_title("pyGTK Threads Example")

        vbox = gtk.VBox(False, 4)

        self.threads = []
        for i in range(NUM_THREADS + 1):
            pb = gtk.ProgressBar()
            vbox.pack_start(pb, False, False, 0)
            self.threads.append(ProgressThread(pb, random.uniform(0.01, 0.10)))

        self.add(vbox)
        self.show_all()

    def quit(self, obj):
        for t in self.threads:
            t.stop()

        gtk.main_quit()

class ProgressThread(threading.Thread):
    def __init__(self, progressbar, step_value):
        threading.Thread.__init__ (self)

        self.pb = progressbar
        self.step = step_value

        self.stopthread = threading.Event()

    def run(self):
        while not self.stopthread.isSet():
            cur_frac = self.pb.get_fraction()
            new_frac = cur_frac + self.step
            if new_frac > 1.0:
                new_frac = 1.0

            gtk.gdk.threads_enter()
            self.pb.set_fraction(new_frac)
            gtk.gdk.threads_leave()

            if self.pb.get_fraction() == 1.0:
                break

            time.sleep(0.1)

    def stop(self):
        self.stopthread.set()

if __name__ == "__main__":
    pyapp = PyApp()

    for t in pyapp.threads:
        t.start()

    gtk.gdk.threads_enter()
    gtk.main()
    gtk.gdk.threads_leave()


# -*- coding: utf-8 -*-
import sys
import time

import numpy as np
import pandas as pd

pd.set_option('display.max_rows', None)
pd.set_option('display.max_columns', None)
pd.set_option('display.width', None)

from foo import *

def to_np(stocks):
    # for var in stocks:
    #     print(var.open())

    """转化为numpy结构数组"""
    t_type = np.dtype(
        {
            "names": ["date", "证券代码", "open", "high", "close", "low"],
            "formats": ["U10", "U10", "d", "d", "d", "d"],
        }
    )
    return np.array(
        [(s.date(), s.code(), s.open(), s.high(), s.close(), s.low()) for s in stocks],
        dtype=t_type,
    )

def to_df(stocks):
    """转化为pandas的DataFrame"""
    return pd.DataFrame.from_records(to_np(stocks), index="date")

def main():
    ida = IndexDayAction.getInstance()
    print(type(ida))
    ida2 = IndexDayAction.getInstance()
    print(type(ida2))
    if ida == ida2:
        print("====================")
    idxs = ida.run("000001", "1989-01-01", "2019-01-01")
    print("---m-----")
    return idxs

def xdxr():
    x = Xdxr()
    # df = x.getXdxr()
    # dfx = x.getIdxs()
    # df = x.get_columns()
    df = x.getData()
    # df = x.hello()
    # df = x.getVec()
    return df

def stockmin():
    sa = StockMinAction.getInstance()
    ss = sa.toPython()
    # print(ss)
    return ss

if __name__ == "__main__":
    # ida = IndexDayAction.getInstance()
    # # try:
    # #     ida = IndexDayAction().getInstance()
    # # except Exception as e:
    # #     print(e)

    # idxs = main()
    # print('--------')
    # for idx in idxs:
    #     print("{} {}".format(idx.id(), idx.code()))
    # print('----2----')
    # time.sleep(3)
    # sys.exit(0)

    # df = xdxr()
    # print("-----rrrrrrrrrr------")
    # dnp = to_df(df)
    # # dnp = to_np(df)
    # print("--2222222---rrrrrrrrrr------")
    # print(dnp)

    # print("-----rrrrrrrrrr------")
    # for e in df:
    #     print("code: {}, open: {}, high: {}".format(e.code(), e.open(), e.high()))
    # print("--2222222---rrrrrrrrrr------")
    # print(df)
    s3 = stockmin()
    for e in s3:
        print("datetime: {} code: {}, open: {}, high: {}".format(e.datetime(), e.code(), e.open(), e.high()))


import random
import socket
import threading

# list of quotes
quotes = ["A day without sunshine is like, you know, night.",
          "Be the change you wish to see in the world.",
          "The only way to do great work is to love what you do.",]

def handle_client(client_socket):
    """Handles a single client's request for a quote"""
    # Send a random quote to the client
    client_socket.send(random.choice(quotes).encode())
    client_socket.close()

def main():
    # Create a TCP socket
    server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

    # Bind the socket to the port
    server_socket.bind(('192.168.169.136', 5555))

    # Listen for incoming connections
    server_socket.listen()

    while True:
        # Accept a new client connection
        client_socket, client_address = server_socket.accept()

        # Handle the client in a new thread
        client_thread = threading.Thread(target=handle_client, args=(client_socket,))
        client_thread.start()

if __name__ == '__main__':
    main()


# -*-coding:Utf-8 -*

import socket
import select
import time
from threading import Thread, RLock, Event
import src.param as param

class   Listener(Thread):
    """Create a thread that listen and print message on an openned connexion"""
    def __init__(self, connexion):
        Thread.__init__(self)
        self.connexion = connexion
    def run(self):
        msg = b""
        while msg.decode() != (param.command_char + param.exit_command):
            msg = self.connexion.recv(1024)
            if msg.decode() != (param.command_char + param.exit_command):
                print("\n" + msg.decode("utf-8") + "\n> ", end = "")
            else:
                print("Connexion avec le serveur terminé.")

class   Messenger(Thread):
    """Create a thread that send message on an openned connexion"""
    def __init__(self, connexion):
        Thread.__init__(self)
        self.connexion = connexion
        self._stop_event = Event()
    def stop(self):
        self._stop_event.set()
    def stopped(self):
        return self._stop_event.is_set()
    def run(self):
        msg = ""
        while msg != (param.command_char + param.exit_command):
            msg = input("> ")
            if self.stopped():
                break
            self.connexion.send(msg.encode("utf-8"))

def     init_server(hote, port):
    main_connexion = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    main_connexion.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    main_connexion.bind((hote, port))
    main_connexion.listen(5)
    print("Serveur en écoute sur le port {}".format(port))
    return main_connexion

def     terminate_connexions(connexion_list, main_connexion=None):
    for client in connexion_list:
        if main_connexion:
            client.send((param.command_char + param.exit_command).encode())
        client.close()
    if main_connexion:
        main_connexion.close()

def     connect_to_server(hote, port, timeout, player_id=-1):
    start = time.time()
    connected = False
    wait_time = 0
    while (wait_time < timeout and connected == False):
        try:
            connexion_to_server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            connexion_to_server.connect((hote, port))
        except ConnectionRefusedError:
            if wait_time == 0:
                print("(...timeout dans {} sec)".format(timeout))
            connected = False
            wait_time = time.time() - start
        else:
            print("else")
            connected = True
    if wait_time >= timeout:
        print("Timeout: echec de la connexion au serveur...")
        return None, None
    else:
        if player_id < 0:
            player_id = request_id(connexion_to_server)
        else:
            connexion_to_server.send("id = {}".format(player_id).encode("utf-8"))
        return connexion_to_server, player_id

def     request_id(connexion):
    connexion.send("request_id".encode("utf-8"))
    player_id = connexion.recv(1024).decode("utf-8")
    player_id = int(player_id)
    print("Vous êtes le joueur {}".format(player_id))
    return player_id


#!/usr/bin/env python

# Representing networks and models for interactive visualization on the web
# Daniel Klein, 8/1/2012

import numpy as np
import json

def dump_to_json(network):
    s = { 'nodes': [], 'links': [] }

    # Nodes
    raw = {}
    for name in network.names:
        raw[name] = { 'name': name }
    for cov_name in network.node_covariates:
        cov = network.node_covariates[cov_name]
        for name, val in zip(cov.names, cov.data):
            raw[name][cov_name] = val
    for name in network.names:
        s['nodes'].append(raw[name])

    # Edges
    raw = {}
    nnz_i, nnz_j = network.array.nonzero()
    for i, j in zip(nnz_i, nnz_j):
        i, j = int(i), int(j)
        s['links'].append({ 'source': i, 'target': j })

    return json.dumps(s)


import os
import json
import re
from collections import defaultdict
import time

from config import config
from config import columns
from dossier import Dossier, load_dossiers, GPTCleanedDiagnosis, save_gpt_cleaned_diagnoses
from serde.json import from_json

from langchain.llms import OpenAI
llm = OpenAI(model_name='gpt-4',
             temperature=0.3,
             max_tokens=2500,
             openai_api_key=os.getenv("OPENAI_API_KEY"))

#
# Print out some summary stats about the previous pipeline stages
#
dossiers = load_dossiers(config.DOSSIERS_FILE)
print(f'Dossiers: {len(dossiers)}')

unique_diagnoses = list(set([d for dossier in dossiers for d in dossier.clean_diagnosis]))
unique_diagnoses.sort()
print(f'Unique Diagnoses: {len(unique_diagnoses)}')

def build_prompt(diagnoses):
     # Very naive templating using %%SOME_STRING%%
    with open('prompts/clean_diagnosis.txt', 'r') as fin:
        content = fin.read()

        values = '\n'.join(['\t' + d for d in diagnoses])
        return content.replace('%%DIAGNOSIS%%' , values)

#
# Iterate through the diagnosis list in chunks, using OpenAI to clean
# and normalize each diagnosis
#
CHUNK_SIZE = 10
for i in range(0, len(unique_diagnoses), CHUNK_SIZE):

    time.sleep(0.5)
    chunk = unique_diagnoses[i:i+CHUNK_SIZE]
    prompt = build_prompt(chunk)
    print(f'Chunk [{i+1}..{i+CHUNK_SIZE}]: {", ".join(chunk)}')

    # Call the LLM to complete the prompt
    completion = llm(prompt)

    # Result will be something like this
    # [
    #        {
    #            "raw_field_diagnosis_french": "abces du cuir chevelure",
    #            "clean_field_diagnosis_french": "abcès du cuir chevelu",
    #            "shorthand": "",
    #            "is_probable": false,
    #            "to_investigate": false,
    #            "to_eliminate": false
    #        },
    # ]
    #
    gpt_cleaned_diagnoses = []

    for item in json.loads(completion):
        diagnosis = from_json(GPTCleanedDiagnosis, json.dumps(item))
        gpt_cleaned_diagnoses.append(diagnosis)

    save_gpt_cleaned_diagnoses(config.GPT_CLEANED_DIAGNOSES_FILE, gpt_cleaned_diagnoses)


from selenium import webdriver

browser = webdriver.Chrome()
browser.get("http://suninjuly.github.io/simple_form_find_task.html")
#найти элемент по id
button = browser.find_element_by_id("submit_button")

#закрыть браузер
browser.quit()


# import required classes

from PIL import Image, ImageDraw, ImageFont

# create Image object with the input image

image = Image.open('source/images/college_winner.png')

# initialise the drawing context with
# the image object as background

draw = ImageDraw.Draw(image)
# desired size

font = ImageFont.truetype('source/fonts/Roboto-Bold.ttf', size=15)

# starting position of the message

(x, y) = (280, 245)
message = "SHREE L. R. TIWARI COLLEGE OF ENGINEERING "
color = 'rgb(0, 0, 0)'  # black color
draw.text((x, y), message, fill=color, font=font)

# save the edited image
d_p_p = "export/nssday2020/po/"+message + ".png"
d_p_w = "export/nssday2020/po/winner/"+message + ".png"
d_v_p = "export/nssday2020/participants/"+message + ".png"
d_v_w = "export/nssday2020/participants/winner/"+message + ".png"
d_c = "export/nssday2020"+message + ".png"

image.save(d_c)


#父类
class LiuBei:
    #属性
    __wife = ('甘夫人','糜夫人','孙尚香')#私有化
    _money = '100'#受保护（不是语法是约定）
    skin = '黄色'# 公共
    #方法
    #吃饭
    def eat(self):#公共的
        print('打死我***也不吃~ 真香啊~')

    #学习
    def __study(self):#私有化
        print('好好学习天天向上')

    #做鞋
    def _makeshoes(self):
        print('编草鞋')

    #内部测试受保护成员
    def test(self):
        print(self._money)
        self._makeshoes()

#子类
class LiuShan(LiuBei):
    pass

#检测私有化和公共的封装
#实例化对象
ls = LiuShan()
#print(ls.wife) #私有成员无法在子类.子对象中调用
#ls.study()#私有成员无法在子类.子对象中调用

#print(ls.skin)#公共的成员可以在子类/子对象中调用
#ls.eat()#公共的成员可以在子类/子对象中调用

#调用受保护的成员
#实例化刘备的对象
lb = LiuBei()

#在类和对向外（随便访问）
print(lb._money)
lb._makeshoes()

#在类和对象的内部（随便访问）
lb.test()

#在子类和子对象中
print(ls._money)
ls._makeshoes()


from __future__ import print_function
import sys
from time import sleep

class FileReporter(object):

    def __init__(self, filename):
        self._filename = filename
        try:
            self._fil = open(self._filename, "w")
        except IOError as ioe:
            sys.stderr.write("While opening {}, caught IOError: {}\n".format(
                self._filename, repr(ioe)))
            sys.exit(1)

    def report(self, message):
        self._fil.write(message)

class ScreenReporter(object):

    def __init__(self, dest):
        self._dest = dest

    def report(self, message):
        self._dest.write(message)

def square(i):
    return i * i

def cube(i):
    return i * i * i

def processor(process, times, report_interval, reporter):
    result = 0
    for i in range(1, times + 1):
        result += process(i)
        sleep(0.1)
        if i % report_interval == 0:
            # This is the call to the callback method
            # that was passed to this function.
            reporter.report("Items processed: {}. Running result: {}.\n".format(i, result))

file_reporter = FileReporter("processor_report.txt")
processor(square, 20, 5, file_reporter)

stdout_reporter = ScreenReporter(sys.stdout)
processor(square, 20, 5, stdout_reporter)

stderr_reporter = ScreenReporter(sys.stderr)
processor(square, 20, 5, stderr_reporter)


from flask import Flask, request, jsonify
from flask_sqlalchemy import SQLAlchemy
from flask_cors import CORS
from os import environ
import json
import os
# import amqp_setup

from invokes import invoke_http

monitorBindingKey='*.notifications'

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = environ.get('dbURL')
#app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+mysqlconnector://is213@localhost:3306/notifications'

# to fix the kong bug
app.config['SQLALCHEMY_ENGINE_OPTIONS'] = {'pool_recycle': 299}

db = SQLAlchemy(app)

CORS(app)

class Notifications(db.Model):
    __tablename__ = 'notifications'

    notification_id = db.Column(db.Integer, primary_key=True)
    message = db.Column(db.String(1000), nullable=False)
    account_type = db.Column(db.String(10), nullable=False)
    facility_name = db.Column(db.String(64), nullable=False)

    def __init__(self, message, account_type, facility_name):

        self.message = message
        self.account_type = account_type
        self.facility_name = facility_name

    def json(self):
        return {"notification_id": self.notification_id, "message": self.message, "account_type": self.account_type, "facility_name": self.facility_name}

# def receiveNotifications ():
#     amqp_setup.check_setup()

#     #queue_name = "User" and "Admin"
#     # set up a consumer and start to wait for coming messages
#     amqp_setup.channel.basic_consume(queue="Admin", on_message_callback=callback, auto_ack=True)
#     amqp_setup.channel.basic_consume(queue="User", on_message_callback=callback, auto_ack=True)

#     amqp_setup.channel.start_consuming() # an implicit loop waiting to receive messages;
#     #it doesn't exit by default. Use Ctrl+C in the command window to terminate it.

# def callback(channel, method, properties, body): # required signature for the callback; no return
#     print("\nReceived a message by " + __file__)
#     processNotifications(body)
#     print()

# def processNotifications(notificationMsg):

#     print("Printing the notification:")
#     try:
#         notification = json.loads(notificationMsg)
#         print("--JSON:", notification)

#         #call the invoke(create_noti + admin/user, post, json=data)
#         receiver = notification["data"]["receiver"]
#         invoke_http("http://localhost:5007/notifications/" + receiver, method='POST', json=notification["data"])

#     except Exception as e:
#         print("--NOT JSON:", e)
#         print("--DATA:", notificationMsg)
#     print()

#retrieve all user/admin notification
#limit by 5 only
@app.route("/notifications/<string:account_type>", methods=['GET'])
def get_notifications(account_type):
    notificationList = Notifications.query.filter_by(account_type = account_type).order_by(Notifications.notification_id.desc()).limit(5).all()

    if len(notificationList):
        return jsonify(
            {
                "code": 200,
                "data": {
                    "notifications": [notifications.json() for notifications in notificationList]
                }
            }
        )
    return jsonify(
        {
            "code": 404,
            "message": "There are no such notifications."
        }
    ), 404

# #insert into notification
@app.route("/notifications/<string:account_type>", methods=['POST'])
def create_notification(account_type):

    data = request.get_json()

    message = data['message']
    facility_name = data['facility_name']
    print("ACCOUNT_TYPE HERE: " + str(account_type))
    print("DATA HERE: " + str(message))
    notifications = Notifications(account_type = account_type, message = message, facility_name = facility_name)

    try:
        db.session.add(notifications)
        db.session.commit()

    except Exception as e:
        print (e)
        return jsonify(
            {
                "code": 500,
                "data": {
                    "account_type": account_type
                },
                "message": "An error occurred creating the notifications."
            }
        ), 500

    return jsonify(
        {   #SUCCESS
            "code": 201,
            "data": notifications.json()
        }
    ), 201

if __name__ == '__main__':
    app.run(host='0.0.0.0',port=5007, debug=True)
    #app.port = 5007
    # print("\nThis is " + os.path.basename(__file__), end='')
    # print(": monitoring routing key '{}' in exchange '{}' ...".format(monitorBindingKey, amqp_setup.exchangename))
    # receiveNotifications()


import time

from acts.test_decorators import test_tracker_info
from acts.controllers.anritsu_lib._anritsu_utils import AnritsuError
from acts.controllers.anritsu_lib.md8475a import MD8475A
from acts.controllers.anritsu_lib.md8475a import BtsServiceState
from acts.controllers.anritsu_lib.md8475a import BtsPacketRate
from acts_contrib.test_utils.tel.TelephonyBaseTest import TelephonyBaseTest
from acts_contrib.test_utils.tel.anritsu_utils import set_system_model_lte_wcdma
from acts_contrib.test_utils.tel.anritsu_utils import set_usim_parameters
from acts_contrib.test_utils.tel.anritsu_utils import set_post_sim_params
from acts_contrib.test_utils.tel.tel_defines import NETWORK_MODE_LTE_GSM_WCDMA
from acts_contrib.test_utils.tel.tel_defines import RAT_FAMILY_LTE
from acts_contrib.test_utils.tel.tel_test_utils import ensure_network_rat
from acts_contrib.test_utils.tel.tel_test_utils import ensure_phones_idle
from acts_contrib.test_utils.tel.tel_test_utils import toggle_airplane_mode
from acts_contrib.test_utils.tel.tel_test_utils import toggle_cell_data_roaming
from acts_contrib.test_utils.tel.tel_test_utils import set_preferred_apn_by_adb
from acts_contrib.test_utils.tel.tel_test_utils import start_qxdm_loggers
from acts.utils import adb_shell_ping

PING_DURATION = 5  # Number of packets to ping
PING_TARGET = "192.168.1.2"  # Set to Eth0 IP address of MD8475A
TIME_TO_WAIT_BEFORE_PING = 10  # Time(sec) to wait before ping

class TelLabDataRoamingTest(TelephonyBaseTest):
    def setup_class(self):
        super().setup_class()
        self.ad = self.android_devices[0]
        self.ad.sim_card = getattr(self.ad, "sim_card", None)
        self.md8475a_ip_address = self.user_params[
            "anritsu_md8475a_ip_address"]
        self.wlan_option = self.user_params.get("anritsu_wlan_option", False)
        self.md8475_version = self.user_params.get("md8475", "A")
        if self.ad.sim_card == "VzW12349":
            set_preferred_apn_by_adb(self.ad, "VZWINTERNET")

        try:
            self.anritsu = MD8475A(self.md8475a_ip_address, self.wlan_option,
                                   self.md8475_version)
        except AnritsuError:
            self.log.error("Error in connecting to Anritsu Simulator")
            return False
        return True

    def setup_test(self):
        if getattr(self, "qxdm_log", True):
            start_qxdm_loggers(self.log, self.android_devices)
        ensure_phones_idle(self.log, self.android_devices)
        toggle_airplane_mode(self.log, self.ad, True)
        self.ad.adb.shell(
            "setprop net.lte.ims.volte.provisioned 1", ignore_status=True)
        return True

    def teardown_test(self):
        self.log.info("Stopping Simulation")
        self.anritsu.stop_simulation()
        toggle_airplane_mode(self.log, self.ad, True)
        return True

    def teardown_class(self):
        self.anritsu.disconnect()
        return True

    def phone_setup_data_roaming(self):
        return ensure_network_rat(
            self.log,
            self.ad,
            NETWORK_MODE_LTE_GSM_WCDMA,
            RAT_FAMILY_LTE,
            toggle_apm_after_setting=True)

    def LTE_WCDMA_data_roaming(self, mcc, mnc, lte_band, wcdma_band):
        try:
            [self.bts1, self.bts2] = set_system_model_lte_wcdma(
                self.anritsu, self.user_params, self.ad.sim_card)
            set_usim_parameters(self.anritsu, self.ad.sim_card)
            set_post_sim_params(self.anritsu, self.user_params,
                                self.ad.sim_card)
            self.bts1.mcc = mcc
            self.bts1.mnc = mnc
            self.bts2.mcc = mcc
            self.bts2.mnc = mnc
            self.bts1.band = lte_band
            self.bts2.band = wcdma_band
            self.bts2.packet_rate = BtsPacketRate.WCDMA_DLHSAUTO_REL8_ULHSAUTO
            self.anritsu.start_simulation()
            self.bts2.service_state = BtsServiceState.SERVICE_STATE_OUT
            self.log.info("Toggle Mobile Data On")
            self.ad.droid.telephonyToggleDataConnection(True)

            if not self.phone_setup_data_roaming():
                self.log.warning("phone_setup_func failed. Rebooting UE")
                self.ad.reboot()
                time.sleep(30)
                if self.ad.sim_card == "VzW12349":
                    set_preferred_apn_by_adb(self.ad, "VZWINTERNET")
                if not self.phone_setup_data_roaming():
                    self.log.error(
                        "Failed to set rat family {}, preferred network:{}".
                        format(RAT_FAMILY_LTE, NETWORK_MODE_LTE_GSM_WCDMA))
                    return False

            toggle_cell_data_roaming(self.ad, True)
            self.anritsu.wait_for_registration_state(1)  # for BTS1 LTE

            time.sleep(TIME_TO_WAIT_BEFORE_PING)
            for i in range(3):
                self.ad.log.info("Verify internet connection - attempt %d",
                                 i + 1)
                result = adb_shell_ping(self.ad, PING_DURATION, PING_TARGET)
                if result:
                    self.ad.log.info("PING SUCCESS")
                    break
                elif i == 2:
                    self.log.error(
                        "Test Fail: Phone {} can not ping {} with Data Roaming On"
                        .format(self.ad.serial, PING_TARGET))
                    return False

            toggle_cell_data_roaming(self.ad, False)
            time.sleep(TIME_TO_WAIT_BEFORE_PING)
            if adb_shell_ping(self.ad, PING_DURATION, PING_TARGET):
                self.log.error(
                    "Test Fail: Phone {} can ping {} with Data Roaming Off"
                    .format(self.ad.serial, PING_TARGET))
                return False

            toggle_airplane_mode(self.log, self.ad, True)
            time.sleep(2)
            self.bts2.service_state = BtsServiceState.SERVICE_STATE_IN
            self.bts1.service_state = BtsServiceState.SERVICE_STATE_OUT

            toggle_airplane_mode(self.log, self.ad, False)
            toggle_cell_data_roaming(self.ad, True)
            self.anritsu.wait_for_registration_state(2)  # for BTS2 WCDMA

            time.sleep(TIME_TO_WAIT_BEFORE_PING)
            for i in range(3):
                self.ad.log.info("Verify internet connection - attempt %d",
                                 i + 1)
                result = adb_shell_ping(self.ad, PING_DURATION, PING_TARGET)
                if result:
                    self.ad.log.info("PING SUCCESS")
                    break
                elif i == 2:
                    self.log.error(
                        "Test Fail: Phone {} can not ping {} with Data Roaming On"
                        .format(self.ad.serial, PING_TARGET))
                    return False

            toggle_cell_data_roaming(self.ad, False)
            time.sleep(TIME_TO_WAIT_BEFORE_PING)
            if adb_shell_ping(self.ad, PING_DURATION, PING_TARGET):
                self.log.error(
                    "Test Fail: Phone {} can ping {} with Data Roaming Off"
                    .format(self.ad.serial, PING_TARGET))
                return False

        except AnritsuError as e:
            self.log.error("Error in connection with Anritsu Simulator: " +
                           str(e))
            return False
        except Exception as e:
            self.log.error("Exception during data roaming: " + str(e))
            return False
        return True

    """ Tests Begin """

    @test_tracker_info(uuid="46d49bff-9671-4ab0-a90d-b49d870af6f0")
    @TelephonyBaseTest.tel_test_wrap
    def test_data_roaming_optus(self):
        """Data roaming test for Optus LTE and WCDMA networks

        Tests if data roaming enabled/disabled work correctly with
        Optus(Australia) LTE and WCDMA networks

        Steps:
        1. Setup Optus LTE network and make sure UE registers
        2. Turn on data roaming, and check if Ping succeeds
        3. Turn off data roaming, and check if Ping fails
        4. Turn Airplane Mode On in UE
        5. Setup Optus WCDMA network and make sure UE registers
        6. Turn on data roaming, and check if Ping succeeds
        7. Turn off data roaming, and check if Ping fails

        Expected Result:
        Ping succeeds when data roaming is turned on,
        and Ping fails when data roaming is turned off

        Returns:
            True if pass; False if fail
        """
        return self.LTE_WCDMA_data_roaming(
            mcc="505", mnc="02F", lte_band="3", wcdma_band="1")

    @test_tracker_info(uuid="68a6313c-d95a-4cae-8e35-2fdf3c94df56")
    @TelephonyBaseTest.tel_test_wrap
    def test_data_roaming_telus(self):
        """Data roaming test for Telus LTE and WCDMA networks

        Tests if data roaming enabled/disabled work correctly with
        Telus(Canada) LTE and WCDMA networks

        Steps:
        1. Setup Telus LTE network and make sure UE registers
        2. Turn on data roaming, and check if Ping succeeds
        3. Turn off data roaming, and check if Ping fails
        4. Turn Airplane Mode On in UE
        5. Setup Telus WCDMA network and make sure UE registers
        6. Turn on data roaming, and check if Ping succeeds
        7. Turn off data roaming, and check if Ping fails

        Expected Result:
        Ping succeeds when data roaming is turned on,
        and Ping fails when data roaming is turned off

        Returns:
            True if pass; False if fail
        """
        return self.LTE_WCDMA_data_roaming(
            mcc="302", mnc="220", lte_band="4", wcdma_band="2")

    @test_tracker_info(uuid="16de850a-6511-42d4-8d8f-d800477aba6b")
    @TelephonyBaseTest.tel_test_wrap
    def test_data_roaming_vodafone(self):
        """Data roaming test for Vodafone LTE and WCDMA networks

        Tests if data roaming enabled/disabled work correctly with
        Vodafone(UK) LTE and WCDMA networks

        Steps:
        1. Setup Vodafone LTE network and make sure UE registers
        2. Turn on data roaming, and check if Ping succeeds
        3. Turn off data roaming, and check if Ping fails
        4. Turn Airplane Mode On in UE
        5. Setup Vodafone WCDMA network and make sure UE registers
        6. Turn on data roaming, and check if Ping succeeds
        7. Turn off data roaming, and check if Ping fails

        Expected Result:
        Ping succeeds when data roaming is turned on,
        and Ping fails when data roaming is turned off

        Returns:
            True if pass; False if fail
        """
        return self.LTE_WCDMA_data_roaming(
            mcc="234", mnc="15F", lte_band="20", wcdma_band="1")

    @test_tracker_info(uuid="e9050f3d-b53c-4a87-9363-b88a842a3479")
    @TelephonyBaseTest.tel_test_wrap
    def test_data_roaming_o2(self):
        """Data roaming test for O2 LTE and WCDMA networks

        Tests if data roaming enabled/disabled work correctly with
        O2(UK) LTE and WCDMA networks

        Steps:
        1. Setup O2 LTE network and make sure UE registers
        2. Turn on data roaming, and check if Ping succeeds
        3. Turn off data roaming, and check if Ping fails
        4. Turn Airplane Mode On in UE
        5. Setup O2 WCDMA network and make sure UE registers
        6. Turn on data roaming, and check if Ping succeeds
        7. Turn off data roaming, and check if Ping fails

        Expected Result:
        Ping succeeds when data roaming is turned on,
        and Ping fails when data roaming is turned off

        Returns:
            True if pass; False if fail
        """
        return self.LTE_WCDMA_data_roaming(
            mcc="234", mnc="02F", lte_band="20", wcdma_band="1")

    @test_tracker_info(uuid="a3f56da1-6a51-45b0-8016-3a492661e1f4")
    @TelephonyBaseTest.tel_test_wrap
    def test_data_roaming_orange(self):
        """Data roaming test for Orange LTE and WCDMA networks

        Tests if data roaming enabled/disabled work correctly with
        Orange(UK) LTE and WCDMA networks

        Steps:
        1. Setup Orange LTE network and make sure UE registers
        2. Turn on data roaming, and check if Ping succeeds
        3. Turn off data roaming, and check if Ping fails
        4. Turn Airplane Mode On in UE
        5. Setup Orange WCDMA network and make sure UE registers
        6. Turn on data roaming, and check if Ping succeeds
        7. Turn off data roaming, and check if Ping fails

        Expected Result:
        Ping succeeds when data roaming is turned on,
        and Ping fails when data roaming is turned off

        Returns:
            True if pass; False if fail
        """
        return self.LTE_WCDMA_data_roaming(
            mcc="234", mnc="33F", lte_band="20", wcdma_band="1")

    @test_tracker_info(uuid="dcde16c1-730c-41ee-ad29-286f4962c66f")
    @TelephonyBaseTest.tel_test_wrap
    def test_data_roaming_idea(self):
        """Data roaming test for Idea LTE and WCDMA networks

        Tests if data roaming enabled/disabled work correctly with
        Idea(India) LTE and WCDMA networks

        Steps:
        1. Setup Idea LTE network and make sure UE registers
        2. Turn on data roaming, and check if Ping succeeds
        3. Turn off data roaming, and check if Ping fails
        4. Turn Airplane Mode On in UE
        5. Setup Idea WCDMA network and make sure UE registers
        6. Turn on data roaming, and check if Ping succeeds
        7. Turn off data roaming, and check if Ping fails

        Expected Result:
        Ping succeeds when data roaming is turned on,
        and Ping fails when data roaming is turned off

        Returns:
            True if pass; False if fail
        """
        return self.LTE_WCDMA_data_roaming(
            mcc="404", mnc="24F", lte_band="3", wcdma_band="1")

    """ Tests End """


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

async def sleep():
    i = 1
    while True:
        print("helloworld", i)
        i += 1
        await asyncio.sleep(1)

async def print_goodbye():
    while True:
        print("good bye")
        await asyncio.sleep(2)

# 创建携程对象
co1 = sleep()
co2 = print_goodbye()

# 获取事件循环
loop = asyncio.get_event_loop()
loop.run_until_complete(asyncio.gather(co1, co2))


from pipelines.pipeline_lianjia import *
from utils.db_oper import *

url_ershoufang = 'https://sh.lianjia.com/ershoufang/107100991859.html'
url_chengjiao = 'https://sh.lianjia.com/chengjiao/107000513345.html'
url_city_xiaoqu = 'https://sh.lianjia.com/xiaoqu/'
url_subdistrict = 'http://hz.lianjia.com/xiaoqu/hubin1/'

def update_xiaoqu_num_in_subdistrics(db_con):
    df = pd.read_sql('select city_abbr from city_map', con=cnx)
    cities = df['city_abbr'].tolist()

    for city in cities:
        if city not in ['aq', 'bj', 'sh', 'hz', 'sz', 'wh']:
            try:
                get_subdistrict_list_in_city(city)
            except Exception as e:
                print(e)

db_path = 'data/lianjia_data.db'
cnx = sqlite3.connect(db_path)

# get_city_list()
# get_xiaoqu_info_in_subdistict(url_subdistrict)
# get_xiaoqu_chengjiao_info(xiaoqu_id='5011000014141', city_abbr='sh')
# get_xiaoqu_ershoufang_info(xiaoqu_id='5011000014141', city_abbr='sh')
# get_xiaoqu_detailed_info(xiaoqu_id='57+999999011000014141', city_abbr='sh')


class Solution:
    def smallestChair(self, times: List[List[int]], targetFriend: int) -> int:
        n = len(times)
        available = list(range(n))
        mp = collections.defaultdict(int)
        seats = []

        for i, (t1,t2) in enumerate(times):
            seats.append((t1, 1, i))
            seats.append((t2, 0, i))

        seats.sort()

        for time, isArrival, friend in seats:
            if isArrival:
                seat = heapq.heappop(available)
                if friend == targetFriend: return seat
                mp[friend] = seat
            else:
                seat = mp[friend]
                heapq.heappush(available, seat)

        return mp[targetFriend]


# Definition for a binary tree node.
class TreeNode(object):

    def __init__(self, x):
        self.val = x
        self.left = None
        self.right = None

class Solution(object):

    def isSameTree(self, p, q):
        """
        :type p: TreeNode
        :type q: TreeNode
        :rtype: bool
        """
        if p is None and q is None:
            return True
        elif p is None or q is None:
            return False
        else:
            parent_nodes1 = [p]
            parent_nodes2 = [q]
            child_nodes1 = []
            child_nodes2 = []
            while len(parent_nodes1) != 0 and len(parent_nodes2) != 0:
                for i, j in zip(parent_nodes1, parent_nodes2):
                    if i.val != j.val:
                        return False
                    else:
                        if i.left is not None and j.left is not None:
                            child_nodes1.append(i.left)
                            child_nodes2.append(j.left)
                        elif i.left is None and j.left is None:
                            pass
                        else:
                            return False
                        if i.right is not None and j.right is not None:
                            child_nodes1.append(i.right)
                            child_nodes2.append(j.right)
                        elif i.right is None and j.right is None:
                            pass
                        else:
                            return False
                parent_nodes1, parent_nodes2 = child_nodes1, child_nodes2
                child_nodes1 = []
                child_nodes2 = []
        return True

if __name__ == '__main__':
    p = TreeNode(0)
    p.left = TreeNode(-5)
    q = TreeNode(0)
    q.left = TreeNode(-8)
    solution = Solution()
    print(solution.isSameTree(p, q))


import numpy as np
import dxfgrabber
import sys
if '/opt/ros/kinetic/lib/python2.7/dist-packages' in sys.path:
    sys.path.remove('/opt/ros/kinetic/lib/python2.7/dist-packages')
sys.path.append("..")
import cv2
from config.config import cfg
from src.Coordinate_Transformation import Coordinate_Transformation
import math
import argparse
from src.utils import check_collision, ray_trace
import os

def getMinXY(shapes):
    minX, minY = 99999, 99999
    for shape in shapes:
        if shape.dxftype == 'LINE':
            minX = min(minX, shape.start[0], shape.end[0])
            minY = min(minY, shape.start[1], shape.end[1])
        elif shape.dxftype == 'ARC':
            minX = min(minX, shape.center[0])
            minY = min(minY, shape.center[1])

    return minX, minY

def getMaxXY(shapes):
    maxX, maxY = -99999, -99999
    for shape in shapes:
        if shape.dxftype == 'LINE':
            maxX = max(maxX, shape.start[0], shape.end[0])
            maxY = max(maxY, shape.start[1], shape.end[1])
        elif shape.dxftype == 'ARC':
            maxX = max(maxX, shape.center[0])
            maxY = max(maxY, shape.center[1])

    return maxX, maxY

def FillHole(mask):
    contours, hierarchy = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
    len_contour = len(contours)

    for i in range(len_contour):
        if contours[i].shape[0] < 800:
            mask = cv2.drawContours(mask, contours, i, (255, 255, 255), -1)

    return mask

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    flag_parser = parser.add_mutually_exclusive_group(required=False)
    flag_parser.add_argument('--Generate_RC_data', dest='Generate_RC_data', action='store_true')
    flag_parser.add_argument('--no-Generate_RC_data', dest='Generate_RC_data', action='store_false')
    parser.set_defaults(Generate_RC_data=True)
    flag_parser = parser.add_mutually_exclusive_group(required=False)
    flag_parser.add_argument('--Testing', dest='Testing', action='store_true')
    flag_parser.add_argument('--no-Testing', dest='Testing', action='store_false')
    parser.set_defaults(Testing=False)
    flag_parser = parser.add_mutually_exclusive_group(required=False)
    flag_parser.add_argument('--Generate_GT_data', dest='Generate_GT_data', action='store_true')
    flag_parser.add_argument('--no-Generate_GT_data', dest='Generate_GT_data', action='store_false')
    parser.set_defaults(Testing=False)
    args = parser.parse_args()

    """
        Generate image of environment with dxf file.
    """
    Bicocca_dxf = dxfgrabber.readfile(cfg.Bicocca_dxf_path)
    shapes = Bicocca_dxf.entities.get_entities()
    x_min, y_min = getMinXY(shapes)
    x_max, y_max = getMaxXY(shapes)
    Bicocca_Coordinate_Transformation = Coordinate_Transformation(x_max, x_min, y_max, y_min)

    shapes = Bicocca_dxf.entities.get_entities()
    env_size = Bicocca_Coordinate_Transformation.get_env_size()
    env_size = Bicocca_Coordinate_Transformation.img2array(env_size,cfg.Bicocca_resolution)

    env = np.zeros((env_size[0],env_size[1],3), np.uint8)
    for shape in shapes:
        if shape.dxftype == 'LINE':
            start = shape.start[0:2]
            end = shape.end[0:2]
            x_start, y_start = Bicocca_Coordinate_Transformation.ori2array(start,cfg.Bicocca_resolution)
            x_end, y_end = Bicocca_Coordinate_Transformation.ori2array(end,cfg.Bicocca_resolution)

            env = cv2.line(env, (y_start,x_start), (y_end,x_end), (255,255,255), 1)
        elif shape.dxftype == 'ARC':
            center = shape.center
            x_center , y_center = Bicocca_Coordinate_Transformation.ori2array(center,cfg.Bicocca_resolution)
            radius = int(shape.radius / cfg.Bicocca_resolution)
            if (shape.start_angle > 180) and (shape.end_angle < shape.start_angle):
                env = cv2.ellipse(env, (y_center,x_center), (radius, radius), 180, int(shape.start_angle) - 180, 180 + int(shape.end_angle), (255,255,255), 1)
            else:
                env = cv2.ellipse(env, (y_center,x_center), (radius, radius), 0, int(360 - shape.start_angle), int(360 - shape.end_angle), (255,255,255), 1)

        elif shape.dxftype == 'POLYLINE':
            points = shape.points
            for i in range(len(points)-1):
                start = points[i][0:2]
                end = points[i+1][0:2]
                x_start, y_start = Bicocca_Coordinate_Transformation.ori2array(start, cfg.Bicocca_resolution)
                x_end, y_end = Bicocca_Coordinate_Transformation.ori2array(end, cfg.Bicocca_resolution)

            env = cv2.line(env, (y_start, x_start), (y_end, x_end), (255,255,255), 1)
    env = cv2.cvtColor(env, cv2.COLOR_BGR2GRAY)
    env = FillHole(env)
    env = cv2.cvtColor(env, cv2.COLOR_GRAY2BGR)
    """
        If we do not want to generate ray casting data set
    """
    if args.Testing:
        """
            Map laser scan onto image with ground truth pose.
        """
        Bicocca_gt_trajectory = np.loadtxt(cfg.Bicocca_gt_trajectory_path,
                                           delimiter=',')
        Bicocca_laser_scan = np.loadtxt(cfg.Bicocca_laser_scan_path,
                                        delimiter=',')
        index_laser = 0
        x_img_start, y_img_start, x_img_end, y_img_end = cfg.Bicocca_crop[0]
        for i in range(Bicocca_gt_trajectory.shape[0]-1):
            env_ = env.copy()
            time_stamp_now = Bicocca_gt_trajectory[i, 0]
            time_stamp_next = Bicocca_gt_trajectory[i + 1, 0]
            while (Bicocca_laser_scan[index_laser, 0] < time_stamp_now):
                index_laser += 1
            time_stamp_laser = Bicocca_laser_scan[index_laser, 0]

            pose_now = Bicocca_gt_trajectory[i,1:]
            x_now , y_now, theta_now = Bicocca_Coordinate_Transformation.Bicocca_trajectory2ori(pose_now)
            x_now , y_now= Bicocca_Coordinate_Transformation.ori2array((x_now , y_now), cfg.Bicocca_resolution)

            pose_next = Bicocca_gt_trajectory[i+1, 1:]
            x_next, y_next, theta_next = Bicocca_Coordinate_Transformation.Bicocca_trajectory2ori(pose_next)
            x_next, y_next = Bicocca_Coordinate_Transformation.ori2array((x_next, y_next), cfg.Bicocca_resolution)

            x = x_now + (x_next - x_now) / (time_stamp_next - time_stamp_now) * (time_stamp_laser - time_stamp_now)
            y = y_now + (y_next - y_now) / (time_stamp_next - time_stamp_now) * (time_stamp_laser - time_stamp_now)
            theta = theta_now + (theta_next - theta_now) / (time_stamp_next - time_stamp_now) * (time_stamp_laser - time_stamp_now)

            laser_scan = Bicocca_laser_scan[index_laser,1:]

            for j in range(len(laser_scan)):
                if laser_scan[j] < 30:
                    scan_theta_now = cfg.Bicocca_st_angle + j * cfg.Bicocca_angle_step
                    delta_x_axis_base = math.cos(scan_theta_now) * laser_scan[j] / cfg.Bicocca_resolution
                    delta_y_axis_base = math.sin(scan_theta_now) * laser_scan[j] / cfg.Bicocca_resolution
                    delta_x_axis_base_rot = delta_x_axis_base * math.cos(theta) - delta_y_axis_base * math.sin(theta)
                    delta_y_axis_base_rot = delta_x_axis_base * math.sin(theta) + delta_y_axis_base * math.cos(theta)

                    delta_x_axis_map = -1 * delta_y_axis_base_rot
                    delta_y_axis_map = delta_x_axis_base_rot

                    x_scan = int(x + delta_x_axis_map)
                    y_scan = int(y + delta_y_axis_map)

                    env_[x_scan, y_scan, 2] = 255
                    env_[x_scan, y_scan, 1] = 0
                    env_[x_scan, y_scan, 0] = 255

            if x > x_img_start and x < x_img_end and y > y_img_start and y < y_img_end:

                cv2.imwrite('/Users/chenrj/Desktop/1.png',env_)
                cv2.imshow('scan',env_)
                cv2.waitKey()
    elif args.Generate_RC_data:
        """
            Generate ray casting data set with the map.
            * First crop the map into submaps.
            * Generate ray casting data on submaps.
        """
        for i in range(len(cfg.Bicocca_crop)):
            env_crop = env.copy()
            x_img_start, y_img_start, x_img_end, y_img_end = cfg.Bicocca_crop[i]
            env_crop = env_crop[x_img_start:x_img_end,y_img_start:y_img_end]
            path = cfg.Bicocca_dataset_root_path + 'Data_Transformed_'+ cfg.Bicocca_dataset + '/' + str(i+1)
            map_env = env_crop

            count = 0

            for p in range(20):
                map_env_with_ob = map_env.copy()
                X , Y = map_env_with_ob.shape

                for j in range(60):
                    x = int(np.random.uniform(low=5, high=X - 5))
                    y = int(np.random.uniform(low=5, high=Y - 5))
                    cv2.rectangle(map_env_with_ob, (y, x), (y + 1, x + 1), (255), -1)
                    x = int(np.random.uniform(low=5, high=X - 5))
                    y = int(np.random.uniform(low=5, high=Y - 5))
                    cv2.circle(map_env_with_ob, (y, x), 1, (255), -1, 0)

                cv2.imwrite(path + '/map_ob_'+str(p+1)+'.png', map_env_with_ob)
                cv2.imwrite(path + '/map_.png', map_env)

                for XX in range(5, X-5, 2):
                    for YY in range(5, Y-5, 2):

                        if check_collision(XX, YY, map_env_with_ob):
                            pmap = np.zeros((X, Y))
                            pmap[XX, YY] = 255
                            pmap = cv2.GaussianBlur(pmap, (7, 7), 3)
                            cv2.normalize(pmap, pmap, 0, 255, cv2.NORM_MINMAX)

                            for k in range(10):
                                degree = np.random.uniform(low=cfg.rad_min, high=cfg.rad_max)
                                scan = ray_trace(XX, YY, map_env_with_ob, degree, cfg.Bicocca_laser_max_range / cfg.Bicocca_resolution,cfg.Bicocca_st_angle,cfg.Bicocca_scan_steps,cfg.Bicocca_angle_step)

                                if len(np.where(scan == 255)) > 0:
                                    cv2.imwrite(path + '/scan/' + str(count) + '.png', scan)
                                    cv2.imwrite(path + '/pmap/' + str(count) + '.png', pmap)
                                    cv2.imwrite(path + '/map/' + str(count) + '.png', map_env)
                                    np.savetxt(path + '/rotation/' + str(count) + '.txt', np.array([degree]))

                                    count += 1

                                    print(str(p+1) + 'th scene ---- ' + str(count) + ' in total')
    elif args.Generate_GT_data:
        """
            Generate ground truth data (odometry, observation and pose) with the map.
            * First crop the map into submaps.
            * Generate ray casting data on submaps.
        """
        Bicocca_gt_trajectory = np.loadtxt(cfg.Bicocca_gt_trajectory_path,
                                           delimiter=',')
        Bicocca_laser_scan = np.loadtxt(cfg.Bicocca_laser_scan_path,
                                        delimiter=',')

        for i in range(len(cfg.Bicocca_crop)):
            env_crop = env.copy()
            x_img_start, y_img_start, x_img_end, y_img_end = cfg.Bicocca_crop[i]
            env_crop = env_crop[x_img_start:x_img_end, y_img_start:y_img_end]
            gt_pose=[]
            laser=[]
            odo = []
            for j in range(Bicocca_gt_trajectory.shape[0]-1):
                index_laser = 0
                time_stamp_now = Bicocca_gt_trajectory[j, 0]
                time_stamp_next = Bicocca_gt_trajectory[j + 1, 0]
                pose_now = Bicocca_gt_trajectory[j, 1:]
                x_now, y_now, theta_now = Bicocca_Coordinate_Transformation.Bicocca_trajectory2ori(pose_now)
                x_now, y_now = Bicocca_Coordinate_Transformation.ori2array((x_now, y_now), cfg.Bicocca_resolution)
                pose_next = Bicocca_gt_trajectory[j + 1, 1:]
                x_next, y_next, theta_next = Bicocca_Coordinate_Transformation.Bicocca_trajectory2ori(pose_next)
                x_next, y_next = Bicocca_Coordinate_Transformation.ori2array((x_next, y_next),
                                                                             cfg.Bicocca_resolution)

                if x_now > x_img_start and x_now < x_img_end and y_now > y_img_start and y_now < y_img_end \
                        and  x_next > x_img_start and x_next < x_img_end and y_next > y_img_start and y_next < y_img_end:

                    while (Bicocca_laser_scan[index_laser, 0] < time_stamp_now):
                        index_laser += 1
                    time_stamp_laser = Bicocca_laser_scan[index_laser, 0]

                    x = x_now + (x_next - x_now) / (time_stamp_next - time_stamp_now) * (
                                time_stamp_laser - time_stamp_now)
                    y = y_now + (y_next - y_now) / (time_stamp_next - time_stamp_now) * (
                                time_stamp_laser - time_stamp_now)
                    theta = theta_now + (theta_next - theta_now) / (time_stamp_next - time_stamp_now) * (
                                time_stamp_laser - time_stamp_now)

                    laser_scan = Bicocca_laser_scan[index_laser, 1:]
                    count_laser_inside = 0
                    for j in range(len(laser_scan)):
                        if laser_scan[j] < 30:
                            scan_theta_now = cfg.Bicocca_st_angle + j * cfg.Bicocca_angle_step
                            delta_x_axis_base = math.cos(scan_theta_now) * laser_scan[j] / cfg.Bicocca_resolution
                            delta_y_axis_base = math.sin(scan_theta_now) * laser_scan[j] / cfg.Bicocca_resolution
                            delta_x_axis_base_rot = delta_x_axis_base * math.cos(theta) - delta_y_axis_base * math.sin(
                                theta)
                            delta_y_axis_base_rot = delta_x_axis_base * math.sin(theta) + delta_y_axis_base * math.cos(
                                theta)

                            delta_x_axis_map = -1 * delta_y_axis_base_rot
                            delta_y_axis_map = delta_x_axis_base_rot

                            x_scan = int(x + delta_x_axis_map)
                            y_scan = int(y + delta_y_axis_map)

                            if x_scan > x_img_start and x_scan < x_img_end and y_scan > y_img_start and y_scan < y_img_end:
                                count_laser_inside += 1
                            else:
                                laser_scan[j] = math.nan
                        else:
                            laser_scan[j] = math.nan
                    x = x - x_img_start
                    y = y - y_img_start
                    if count_laser_inside > 10:
                        laser.append(laser_scan)
                        gt_pose.append((x,y,theta))
                        if len(gt_pose)>1:
                            odo.append(np.array(gt_pose[-1])-np.array(gt_pose[-2]))

            laser = np.array(laser)
            gt_pose = np.array(gt_pose)
            odo = np.array(odo)
            path = cfg.Bicocca_dataset_root_path + 'Data_Transformed_' + cfg.Bicocca_dataset + '/Test_MCL/' + str(i + 1) + '/'
            os.makedirs(path,exist_ok=True)
            np.savetxt(path + 'laser.txt', laser)
            np.savetxt(path + 'gt_pose.txt', gt_pose)
            np.savetxt(path + 'odo.txt', odo)
            cv2.imwrite(path + 'global_map.png',env_crop)


"""
Fields and methods used by the LMS and Studio.
BASICALLY CRIBBED BY @jbau from the edx-ora2 project at
https://raw.githubusercontent.com/edx/edx-ora2/master/apps/openassessment/xblock/lms_mixin.py
"""

from xblock.fields import String, Float, Scope, DateTime

class LmsCompatibilityMixin(object):
    """
    Extra fields and methods used by LMS/Studio.
    """
    # Studio the default value for this field to show this XBlock
    # in the list of "Advanced Components"
    display_name = String(
        default="js-parsons problem", scope=Scope.settings,
        help="Display name"
    )

    start = DateTime(
        default=None, scope=Scope.settings,
        help="ISO-8601 formatted string representing the start date of this assignment."
    )

    due = DateTime(
        default=None, scope=Scope.settings,
        help="ISO-8601 formatted string representing the due date of this assignment."
    )

    weight = Float(
        display_name="Problem Weight",
        help="Defines the number of points this problem is worth.",
        values={"min": 0, "step": .1},
        default=1.0,
        scope=Scope.settings
    )

    def has_dynamic_children(self):
        """Do we dynamically determine our children? No, we don't have any.

        The LMS wants to know this to see if it has to instantiate our module
        and query it to find the children, or whether it can just trust what's
        in the static (cheaper) children listing.
        """
        return False

    @property
    def has_score(self):
        """Are we a scored type (read: a problem). Yes.

        For LMS Progress page/grades download purposes, we're always going to
        have a score, even if it's just 0 at the start.
        """
        return True

    @property
    def icon_class(self):
        return 'problem'

    def max_score(self):
        """The maximum raw score of our problem.
        """
        return self.weight


#!/usr/bin/env python
# encoding: utf-8

import os
import ycm_core

flags = [ '-Wall',
        '-Werror',
        '-m32',
        '-c',
        '-ffreestanding',
        '-Ikernel',
        '-Ilibc',
        '-Icpu',
        '-Idrivers',
        ]

SOURCE_EXTENSIONS = ['.c', '.asm']

def FlagsForFile( filename, **kwargs ):
  return {
    'flags': flags,
    'do_cache': True
  }


import pandas as pd
import numpy as np
import re

def ProcessTextFile(path): #TXT expected
    """Expects .txt file, returns splitlines() of text file.
    Output to be passed to MakeBasicStenoDataFrame"""
    try:
        with open(path, 'r') as file:  # with statement automatically closes
            DictFile = file.read()
    except:
        print("Couldn't open file: {}".format(path))
        return None
    try:
        file.close()
    except:
        print('Could not close file: ', path)
        pass
    mydict = DictFile.splitlines()
    return mydict

def MakeBasicStenoDataFrame(StenoDictParam):
    """Returns a dataframe with Steno and Translation.
    Expects preprocessed file as input."""
    stenoDict= {}
    for i in StenoDictParam:
        #print(i)
        stenoDict[i[:i.find("=")].strip() ] = \
            i[i.find("=")+1:].strip()
    StenoDictDF = pd.DataFrame({"Steno":list(stenoDict.keys()),
                   "Translation": list(stenoDict.values())})

    def AddStrokesColumn(df):
        try:
            import Standardize_Stokes
        except:
            print('Couldn\'t import Standardize Stokes, returning none.')
            return None
        df = Standardize_Stokes.AddStrokesColumn(df)  # Strange call, the function just adds the column

        df['Strokes'] = df['Strokes'].apply(lambda x: Standardize_Stokes.StandardizeStrokes(x))
        return df

    StenoDictDF['Word'] = StenoDictDF['Translation'].apply(lambda x: x.lower()) # for standardization, merging
    #StenoDictDF["Word"] = stenoDict['word']

    StenoDictDF = AddStrokesColumn(StenoDictDF)

    return StenoDictDF

def ResolveEclipseConflicts(df, conflictSymbol = '\\', verbose = False):
    """I hate backslashes in python, ahhh!!!  TODO"""

    #Note: Do not touch the backslashes here!!! It works, I have no idea why.  Don't touch it!

    specialDelination = "|_|_"
    df2= df[df["Translation"].str.contains('\\\\')]
    print("There are {} conflicts in the entered dictionary.".format(len(df2)))
    Translations = list(df2['Translation'])
    #print(Translations[:7])
    TranslationsFixed = [i.replace('\\', specialDelination) for i in Translations]
    if verbose:
        print("TranslationsFixed:")
        print()
        print(TranslationsFixed[:5])

    TuplesList =[]
    for i in TranslationsFixed:
        #print(i)
        try:
            tranA = re.findall("[^_|]+", i)[0]
            #print(tranA)
            tranB = re.findall("[^_|]+", i)[1] #i[i.find(specialDelination) + len(specialDelination) + 1:]
            #print(tranB)
            TuplesList.append ( (tranA, tranB))
        except:
            if verbose:
                print("{} Didn't work!!".format(i))
            TuplesList.append((None, None) )

    if verbose:
        print()
        print("TuplesList:")
        print(TuplesList[:5])
    cols = list(df2.columns)
    if len(TuplesList) == len(df2):
        print("Length Check passed!")
    else:
        print("Length Check failed!")

    df3ListList = []

    for i in range(len(df2)):
        df3ListItem=[]
        for j in range(len(cols)):
            df3ListItem.append(df2.iloc[i,j])
        df3ListItem.append(TuplesList[i][0])

        df3ListList.append(df3ListItem)

        df3ListItem = []
        for j in range(len(cols)):
            df3ListItem.append(df2.iloc[i,j])
        df3ListItem.append(TuplesList[i][1]) #second tuple item this time
        df3ListList.append(df3ListItem)

    if verbose:
        if len(df3ListList) == len(df2) *2:
            print("df3ListList length passed!")
        else:
            print("df3ListList length failed!")

    df3 = pd.DataFrame()
    for i in range(len(cols)):
        df3[cols[i]] = [j[i] for j in df3ListList]

    #df3['Fixed'] = [i[-1] for i in df3ListList]
    df3['Translation'] = [i[-1] for i in df3ListList]

    #Almost there, fix this!
    df3.dropna(subset=['Translation'], inplace=True)
    df3['Word'] = [i.lower() for i in list(df3['Translation']) ]

    if verbose:
        print('df3ListList[:4] :')
        print(df3ListList[:4])

    if verbose:
        if len(df3) / 2 == len(df2):
            print("Final Length Check passed!")
        else:
            print("Final Length Check failed!")

    # df4 = pd.merge(df3, df[~df["Translation"].str.contains('\\\\') ], how = "outer", on ="Steno")
    # # df2= df[df["Translation"].str.contains('\\\\')]

    df4 = pd.concat([df3, df[~df["Translation"].str.contains('\\\\') ] ])

    return df4

    # #for now, only handle the first two conflicts, possibly to do increase to 3:
    # Translations = list(df2['Translation'])
    # for i in range(len(Translations)):
    #  #   if Translations[i][0] == conflictSymbol:
    #     #print(Translations[i])
    #     #print(Translations[i][0:2])
    #     if Translations[0] == conflictSymbol[0]:
    #         Translations[i] = Translations[i][1:]
    # ConflictTuples = []
    # for i in Translations:
    #     Tran1 = i[:i.find(conflictSymbol[0])].strip()
    #     Tran2 = i[i.find(conflictSymbol[0])+1:].strip()
    #     ConflictTuples.append( (Tran1, Tran2))
    #
    # if (len(ConflictTuples) == len(Translations) and verbose):
    #     print("Lengths are right...")
    #
    # results = []
    # Stenos = list(df2["Steno"])
    # for i in range(len(Stenos)):
    #     results.append( (Stenos[i], ConflictTuples[i][0]))
    #     results.append((Stenos[i], ConflictTuples[i][1])) #assuming only 2 conflicts each for now... TODO
    #
    # df3 = pd.DataFrame({
    #     "Steno": [i[0] for i in results] ,
    #     "Translation": [i[1] for i in results]
    # })
    # return df3

if __name__ == '__main__':
    import pandas as pd

    NoahDictFile = "Data Sources/NCollinDict1.txt"
    # put any another = separated dictionary here

    #[print(i) for i in mydict[:5]]
    df = MakeBasicStenoDataFrame(ProcessTextFile(NoahDictFile))
    #print(df.head())
    print(df.head())
    #print('ok')

#This portion tests that the StenoDictionary and Phonetics Dictioary can be compared and eventually merged.
    df2 = ResolveEclipseConflicts(df)
    quit()


import os
import sqlite3
from urllib.request import urlopen
import csv
import io
from intellistock.data import data

ARCHIVE_BASE_URL = "http://hunyadym.hu/stock"
ARCHIVE_LIST = "list.php"

def setup_database():
    if os.path.isfile(data.DB_PATH):
        data.conn = sqlite3.connect(data.DB_PATH)
        return

    global conn
    data.conn = sqlite3.connect(data.DB_PATH)
    data.conn.execute('''CREATE TABLE StockData (
                    id INTEGER PRIMARY KEY,
                    paper_name TEXT,
                    datetime INTEGER,
                    close REAL,
                    volume INTEGER);''')

    data.conn.execute('''CREATE TABLE Stocks (
                    paper_name TEXT);''')
    data.conn.commit()
    initialize_database()

    persist_stocks()

    data.conn.commit()

def initialize_database():
    urlpath = urlopen(ARCHIVE_BASE_URL + "/" + ARCHIVE_LIST)

    files = urlpath.read().decode('utf-8').split()

    for f in files:
        print("loading " + f)
        load_from_csv(f)

def load_from_csv(csv_path):
    global stocks

    urlpath = urlopen(ARCHIVE_BASE_URL + "/" + csv_path)

    file = urlpath.read().decode('ISO-8859-1')

    csv_reader = csv.reader(io.StringIO(file))
    next(csv_reader) #skip header

    for row in csv_reader:
        try:
            paper_name = row[0]
            date = int(row[2])
            time = int(row[3])
            close = float(row[4])
            volume = int(row[5])

            data.conn.execute("INSERT INTO StockData VALUES (?, ?, ?, ?, ?)",
                     (None, paper_name, date * 1000000 + time, close, volume))

            if paper_name not in data.stocks:
                data.stocks.add(paper_name)

        except ValueError:
            continue

def persist_stocks():
    for stock in data.stocks:
        data.conn.execute("INSERT INTO Stocks VALUES (?)", [stock])

setup_database()


import osrm
from openpyxl import Workbook
from openpyxl import load_workbook
import datetime
from datetime import date, timedelta
import numpy as np

excel_file = "dortmund_straßennamen_location_route.xlsx"
excel_file_new = "dortmund_straßennamen_location_route_distanz.xlsx"

wb = load_workbook(filename=excel_file, read_only=True)
ws = wb.active
wb_new = Workbook()
ws_new = wb_new.active
rows = list(ws.rows)[1:]
num_rows = len(rows)

MyConfig = osrm.RequestConfig("localhost:5000/v1/car")

row = ["", "uhrzeit","Straße Start","Nr Start",
    "Stadt Start","Lat Start","Lon Start","Straße Ziel",
    "Nr Ziel","Stadt Ziel","Lat Ziel","Lon Ziel",
    "OSRM Dauer", "OSRM Distanz"]
ws_new.append(row)

for i, row in enumerate(rows):
    print(i+1, "/", num_rows)
    row = [cell.value for cell in row]

    try:
        lat1 = float(row[5])
        long1 = float(row[6])
        lat2 = float(row[10])
        long2 = float(row[11])

        p1 = osrm.Point(
            latitude=lat1,
            longitude=long1)
        p2 = osrm.Point(
            latitude=lat2,
            longitude=long2)

        result = osrm.simple_route(p1, p2, url_config=MyConfig, output='route', geometry='wkt')[0]

        row += [result['duration']/60.0, result['distance']/1000.0]
        ws_new.append(row)

    except Exception as e:
        print("Error: ", e)

wb_new.save(excel_file_new)


import numpy as np
import torch
from captum.attr import (IntegratedGradients, LayerActivation,
                         LayerConductance, NeuronConductance, Occlusion)

from ..model import MyDataset
from . import TorchModel

def get_neurons_activation(model: TorchModel, data: MyDataset, only_prunable=True, **kwargs):
    """https://captum.ai/tutorials/Titanic_Basic_Interpret"""
    model.cuda()
    layers = model.prunable if only_prunable else tuple(model.layers.children())
    ig = LayerActivation(model, layers)
    X = model.safe_cast_input(data.X)
    X.requires_grad_()
    attr = ig.attribute(X)
    model.cpu()
    return dict(zip(layers, attr))

def get_neurons_importance(model: TorchModel, data: MyDataset, only_prunable=True, n_steps=1, **kwargs):
    model.cuda()
    layers = model.prunable if only_prunable else tuple(model.layers.children())
    X = model.safe_cast_input(data.X)
    ret = {}
    for l in layers:
        cond = LayerConductance(model, l)
        ret[l] = np.mean(cond.attribute(X, target=1, n_steps=n_steps).detach().cpu().numpy(), axis=0)
    model.cpu()
    return ret

def get_learned_features_per_neuron(model: TorchModel, data: MyDataset, only_prunable=True, n_steps=2, **kwargs):
    model.cuda()
    layers = model.prunable if only_prunable else tuple(model.layers.children())
    X = model.safe_cast_input(data.X)
    ret = {}
    for l in layers:
        neuron_cond = NeuronConductance(model, l)
        ret[l] = np.mean(neuron_cond.attribute(X, neuron_selector=1,
                         target=1, n_steps=n_steps).detach().cpu().numpy(), axis=0)
    model.cpu()
    return ret

def rank_gradient_captum(model: TorchModel, data: MyDataset, current_features, n_steps=1, **_):
    """Compute feature importance with integrated gradient method using captum"""
    model.cuda()
    positions = [i for i, x in enumerate(data.X.columns.values) if x in current_features]
    ig = IntegratedGradients(model)
    X = model.safe_cast_input(data.X)
    X.requires_grad_()
    attr = ig.attribute(X, target=1, n_steps=n_steps)
    attr = attr.detach().numpy()
    importances = np.mean(attr, axis=0)
    model.cpu()
    return dict(zip(current_features, [importances[i] for i in positions]))

def rank_perturbation_captum(model: TorchModel, data: MyDataset, current_features, **_):
    model.cuda()
    positions = [i for i, x in enumerate(data.X.columns.values) if x in current_features]
    occlusion = Occlusion(model)

    attributions_occ = occlusion.attribute(torch.tensor(data.X.values, device=model.device),
                                           target=1,
                                           sliding_window_shapes=(1,),
                                           baselines=0)
    attr = attributions_occ.detach().cpu().numpy()
    importances = np.mean(attr, axis=0)
    model.cpu()
    return dict(zip(current_features, [importances[i] for i in positions]))


a=[2,4,1,44,22,4,12]
count=0
for i in range(len(a)):
    for j in range(i+1,len(a)):
        if a[i]>a[j]:
            temp=a[i]
            a[i]=a[j]
            a[j]=temp
for i in range(len(a)):
    isPrime=0
    if a[i]<2:
        isPrime=1
    for j in range(2,a[i]//2+1):
        if a[i]%j==0:
            isPrime=1
            break
    if isPrime==0:
        print(a[i])
        count+=1
print(count,'count')


import requests
import time
from pytz import utc
from datetime import datetime
from pymongo import MongoClient
from apscheduler.schedulers.blocking import BlockingScheduler
import logging

client = MongoClient()
database = client['predictor']
collection = database['gdax']
tickCount = 0;

logging.basicConfig()

def tick():
    global tickCount
    start = time.clock()
    ticker = requests.get('https://api.pro.coinbase.com/products/BTC-USD/ticker').json()
    depth = requests.get('https://api.pro.coinbase.com/products/BTC-USD/book?level=2').json()
    request_time = time.clock() - start
    date = datetime.now()
    price = float(ticker['price'])
    v_bid = sum([float(bid[1]) for bid in depth['bids']])
    v_ask = sum([float(ask[1]) for ask in depth['asks']])
    collection.insert({'date': date, 'price': price, 'v_bid': v_bid, 'v_ask': v_ask})
    tickCount += 1;
    print("point: {} req_time: {} date: {} price: {} v_bid: {} v_ask: {}".format(tickCount, request_time, date, price, v_bid, v_ask))

def main():
    """Run tick() at the interval of every ten seconds."""
    scheduler = BlockingScheduler(timezone=utc)
    scheduler.add_job(tick, 'interval', seconds=30)
    scheduler.start()

if __name__ == '__main__':
    main()