# -*- coding: utf-8 -*-
from __future__ import print_function
import time
import os
import numpy as np
import matplotlib.pyplot as plt
import math

from cntk.layers import default_options, Convolution, MaxPooling, AveragePooling, Dropout, BatchNormalization, Dense, Sequential, For
from cntk.io import MinibatchSource, ImageDeserializer, StreamDef, StreamDefs
import cntk.io.transforms as xforms
from cntk.initializer import glorot_uniform, he_normal
from cntk import Trainer
from cntk.learners import nesterov, momentum_sgd, adagrad, adam, learning_rate_schedule, UnitType, momentum_as_time_constant_schedule, rmsprop
from cntk import cross_entropy_with_softmax, classification_error, relu, input_variable, softmax, element_times
#from cntk.ops import cross_entropy_with_softmax, classification_error, relu, input_variable, softmax, element_times
from cntk.logging import *
#from cntk.utils import *

image_height = 32
image_width  = 32
num_channels = 3
num_classes  = 10

#
# Define the reader for both training and evaluation action.
#
def create_reader(map_file, mean_file, train):
    if not os.path.exists(map_file) or not os.path.exists(mean_file):
        raise RuntimeError("Files for training/testing not exists.")

    # transformation pipeline for the features has jitter/crop only when training
    transforms = []
    if train:
        transforms += [
            xforms.crop(crop_type='randomside', side_ratio=0.8) # train uses data augmentation (translation only)
        ]
    transforms += [
        xforms.scale(width=image_width, height=image_height, channels=num_channels, interpolations='linear'),
        xforms.mean(mean_file)
    ]
    # deserializer
    return MinibatchSource(ImageDeserializer(map_file, StreamDefs(
        features = StreamDef(field='image', transforms=transforms), # first column in map file is referred to as 'image'
        labels   = StreamDef(field='label', shape=num_classes)      # and second as 'label'
    )))

#
# Train and evaluate the network.
#
def train_and_evaluate(reader_train, reader_test, max_epochs, model_func):
    start_time = time.time()
    print(start_time)
    # Input variables denoting the features and label data
    input_var = input_variable((num_channels, image_height, image_width))
    label_var = input_variable((num_classes))

    # Normalize the input
    feature_scale = 1.0 / 256.0
    input_var_norm = element_times(feature_scale, input_var)

    # apply model to input
    z = model_func(input_var_norm, out_dims=10)

    #
    # Training action
    #

    # loss and metric
    # surrogate loss objective
    ce = cross_entropy_with_softmax(z, label_var)
    # true objective
    pe = classification_error(z, label_var)

    # training config
    epoch_size     = 50000
    minibatch_size = 128

    # Set training parameters
    # maybe or decay the learning rate when loss stop to decrease for some epochs
    #lr_per_minibatch       = learning_rate_schedule([0.01]*20 + [0.003]*20 + [0.001]*20 + [0.0001] * 20 + [0.00001]*20 + [0.000001], UnitType.minibatch, epoch_size)
    # step epoch 20, base lr 0.01, step decay 0.1
    # summarize的方法...
    #lr_per_minibatch       = learning_rate_schedule(sum([[0.01 * lr] * 20 for lr in 0.1 ** np.arange(5)], []), UnitType.minibatch, epoch_size)
    lr_per_minibatch       = learning_rate_schedule([1.0]*80 + [0.1]*40 + [0.01], UnitType.minibatch, epoch_size)
    momentum_time_constant = momentum_as_time_constant_schedule(-minibatch_size/np.log(0.9))
    l2_reg_weight          = 0.0001

    # trainer object
    #learner = momentum_sgd(z.parameters,
    #                        lr = lr_per_minibatch, momentum = momentum_time_constant,
    #                        l2_regularization_weight=l2_reg_weight)
    #learner = adagrad(z.parameters, lr_per_minibatch, l2_regularization_weight=l2_reg_weight)
    learner = adam(z.parameters, lr_per_minibatch, momentum_time_constant, l2_regularization_weight=l2_reg_weight, adamax=True)
    #learner = nesterov(z.parameters, lr_per_minibatch, momentum_time_constant, l2_regularization_weight=l2_reg_weight)
    #learner = rmsprop(z.parameters, lr_per_minibatch, gamma=0.95, inc=1.5, dec=2, max=1, min=0.00001, l2_regularization_weight=l2_reg_weight)

    progress_printer = ProgressPrinter(tag='Training', num_epochs=max_epochs)

    # trainer这里为什么要第一个先输入z... 感觉这个语义是不是限制的有点死...
    # 看一看这边对data graph的eval是怎么resolve dependency. 然后trainer限制了哪些东西...
    # summary系统感觉有满多可以改...
    # tensorflow里train_step也是一个data graph中的节点. 所以也可以用sess.run(train_step)之类的来运行. 然后learning_rate也是一个Tensor(一个计算节点的输出)作为train_step的依赖. 所以可以很智能的计算learning rate.. 就有一堆可以根据global step或者训练error再计算learning rate的方便的Operation... 感觉这边就很局限. 因为trainer与数据流图分开了...trainer只是负责调用DAG数据流图上的backward, forward. 设计的时候并没有设计有一个动态learning rate的数据流图节点作为输入... 而是一个固定的schedule...不过这个可以做...是可以做的...确认一下Trainer里对learning rate schedule的访问...如果是按iterable方法访问的倒是可以继承patch...如果不是。就需要改Trainer里对leanring rate的获取, generalize成函数调用... (要传什么context呢)
    # 所以确实还是tensorflow的设计比较科学... learning rate和负责具体的parameter update strategy的trainer都是数据图中的节点. 并不是说trainer作为一个GOD来协调这样太不通用了. 要决定哪些东西可以动态改. 不同的training方法可能有不同的可以动态决定的参数. 都要这些子trainer 自己去adhoc的调用类似lr()的方法...
    # 如果统一到数据流图里. 每次动态得到lr之类的参数并用于training, 就是data graph的dependency解析和inference机制. 不需要自己做多余的事情...

    trainer = Trainer(z, (ce, pe), [learner], [progress_printer])

    # define mapping from reader streams to network inputs
    input_map = {
        input_var: reader_train.streams.features,
        label_var: reader_train.streams.labels
    }

    log_number_of_parameters(z) ; print()

    # perform model training
    batch_index = 0
    plot_data = {'batchindex':[], 'loss':[], 'error':[]}
    for epoch in range(max_epochs):       # loop over epochs
        sample_count = 0
        while sample_count < epoch_size:  # loop over minibatches in the epoch
            data = reader_train.next_minibatch(min(minibatch_size, epoch_size - sample_count), input_map=input_map) # fetch minibatch.
            trainer.train_minibatch(data)                                   # update model with it

            sample_count += data[label_var].num_samples                     # count samples processed so far

            # For visualization...
            # 这个summary感觉看起来不太智能...
            #
            plot_data['batchindex'].append(batch_index)
            plot_data['loss'].append(trainer.previous_minibatch_loss_average)
            plot_data['error'].append(trainer.previous_minibatch_evaluation_average)

            batch_index += 1
        trainer.summarize_training_progress()
    end_time = time.time()
    print(end_time)
    print("took {} ms".format((end_time-start_time)*1000.0))
    #
    # Evaluation action
    #
    epoch_size     = 10000
    minibatch_size = 16

    # process minibatches and evaluate the model
    metric_numer    = 0
    metric_denom    = 0
    sample_count    = 0
    minibatch_index = 0

    while sample_count < epoch_size:
        current_minibatch = min(minibatch_size, epoch_size - sample_count)

        # Fetch next test min batch.
        data = reader_test.next_minibatch(current_minibatch, input_map=input_map)

        # minibatch data to be trained with
        metric_numer += trainer.test_minibatch(data) * current_minibatch
        metric_denom += current_minibatch

        # Keep track of the number of samples processed so far.
        sample_count += data[label_var].num_samples
        minibatch_index += 1

    print("")
    print("Final Results: Minibatch[1-{}]: errs = {:0.1f}% * {}".format(minibatch_index+1, (metric_numer*100.0)/metric_denom, metric_denom))
    print("")

    # # Visualize training result:
    # window_width            = 32
    # loss_cumsum             = np.cumsum(np.insert(plot_data['loss'], 0, 0))
    # error_cumsum            = np.cumsum(np.insert(plot_data['error'], 0, 0))

    # # Moving average.
    # plot_data['batchindex'] = np.insert(plot_data['batchindex'], 0, 0)[window_width:]
    # plot_data['avg_loss']   = (loss_cumsum[window_width:] - loss_cumsum[:-window_width]) / window_width
    # plot_data['avg_error']  = (error_cumsum[window_width:] - error_cumsum[:-window_width]) / window_width

    # plt.figure(1)
    # plt.subplot(211)
    # plt.plot(plot_data["batchindex"], plot_data["avg_loss"], 'b--')
    # plt.xlabel('Minibatch number')
    # plt.ylabel('Loss')
    # plt.title('Minibatch run vs. Training loss ')

    # plt.subplot(212)
    # plt.plot(plot_data["batchindex"], plot_data["avg_error"], 'r--')
    # plt.xlabel('Minibatch number')
    # plt.ylabel('Label Prediction Error')
    # plt.title('Minibatch run vs. Label Prediction Error ')
    # plt.show()

    return softmax(z)

import PIL

def eval(pred_op, image_path):
    label_lookup = ["airplane", "automobile", "bird", "cat", "deer", "dog", "frog", "horse", "ship", "truck"]
    image_mean   = 133.0
    image_data   = np.array(PIL.Image.open(image_path), dtype=np.float32)
    image_data  -= image_mean
    image_data   = np.ascontiguousarray(np.transpose(image_data, (2, 0, 1)))

    result       = np.squeeze(pred_op.eval({pred_op.arguments[0]:[image_data]}))

    # Return top 3 results:
    top_count = 3
    result_indices = (-np.array(result)).argsort()[:top_count]

    print("Top 3 predictions:")
    for i in range(top_count):
        print("\tLabel: {:10s}, confidence: {:.2f}%".format(label_lookup[result_indices[i]], result[result_indices[i]] * 100))

def convolution_bn(input, filter_size, num_filters, strides=(1,1), init=he_normal(), activation=relu):
    if activation is None:
        activation = lambda x: x

    r = Convolution(filter_size, num_filters, strides=strides, init=init, activation=None, pad=True, bias=False)(input)
    r = BatchNormalization(map_rank=1)(r)
    r = activation(r)

    return r

def resnet_basic(input, num_filters):
    c1 = convolution_bn(input, (3,3), num_filters)
    c2 = convolution_bn(c1, (3,3), num_filters, activation=None)
    p  = c2 + input
    return relu(p)

# increase dimension, num_filters = 2 * previous_num_filters
def resnet_basic_inc(input, num_filters):
    c1 = convolution_bn(input, (3,3), num_filters, strides=(2,2))
    c2 = convolution_bn(c1, (3,3), num_filters, activation=None)

    # projection
    s = convolution_bn(input, (1,1), num_filters, strides=(2,2), activation=None)

    p = c2 + s
    return relu(p)

def resnet_basic_stack(input, num_filters, num_stack):
    assert (num_stack > 0)

    r = input
    for _ in range(num_stack):
        r = resnet_basic(r, num_filters)
    return r

def create_resnet_model(n):
    def func(input, out_dims):
        conv = convolution_bn(input, (3,3), 16)
        r1_1 = resnet_basic_stack(conv, 16, n) # 2n

        r2_1 = resnet_basic_inc(r1_1, 32) # mapping with projection
        r2_2 = resnet_basic_stack(r2_1, 32, n - 1)

        r3_1 = resnet_basic_inc(r2_2, 64)
        r3_2 = resnet_basic_stack(r3_1, 64, n - 1)

        # Global average pooling
        pool = AveragePooling(filter_shape=(8,8), strides=(1,1))(r3_2)
        net = Dense(out_dims, init=he_normal(), activation=None)(pool)
        return net
    func.func_name = "create_resnet_{}".format(n)
    return func

# data_path = os.path.join('data', 'CIFAR-10')
data_path = "/home/foxfi/homework/adavance-ml/Tutorials/data/CIFAR-10"
reader_train = create_reader(os.path.join(data_path, 'train_map.txt'), os.path.join(data_path, 'CIFAR-10_mean.xml'), True)
reader_test  = create_reader(os.path.join(data_path, 'test_map.txt'), os.path.join(data_path, 'CIFAR-10_mean.xml'), False)
# resnet-20, n = 3

pred = train_and_evaluate(reader_train, reader_test, max_epochs=200, model_func=create_resnet_model(3))


import yfinance as yf
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from pathlib import Path
from datetime import datetime
import random
num_portfolios = 5000
risk_free_rate = 0.0178
import streamlit as st
st.set_option('deprecation.showPyplotGlobalUse', False)
from scipy.stats import spearmanr
from sklearn.metrics import mean_squared_error
from sklearn.preprocessing import MinMaxScaler

class Analize_Portfolio(object):
    def __init__(self, stocks, nameSave):
        self.stocks = stocks
        self.sName = nameSave

    def situation(self):
        num_portfolios=1
        weights_record = []

        for i in range(num_portfolios):
            weights = np.random.random(len(self.stocks))
            weights /= np.sum(weights)
            weights_record.append(weights)

        d = zip(self.stocks, weights_record[0])
        portfolio_composition = list(d)

        returns = pd.DataFrame({})
        for t in portfolio_composition:
            name = t[0]
            ticker = yf.Ticker(name)
            data = ticker.history(interval="1d",start="2010-01-01",end="2020-12-31")
            # data = yf.download(name, start="2020-01-01")
            data['return_%s' % (name)] = data['Close'].pct_change(1)

            returns = returns.join(data[['return_%s' % (name)]],how="outer").dropna()
            returns
        mean_returns = returns.mean()
        cov_matrix = returns.cov()
        st.write(f"\n{'* * '*25}\n\n * Presenting The Covariance Matrix For This Portfolio:\n\n\n{'* * '*25}\n")
        st.dataframe(cov_matrix)

        def simulate_returns(historical_returns,forecast_days):
            return historical_returns.sample(
            n=forecast_days, replace = True).reset_index(drop = True)
        simulate_returns(returns['return_'+self.stocks[0]], 1000)

        def simulate_portfolio(historical_returns,composition,forecast_days):
            result = 0
            for t in composition:
                name,weight = t[0],t[1]
                s = simulate_returns(
                    historical_returns['return_%s' % (name)], forecast_days
                    )
                result = result + s * weight
            return(result)
        simulate_portfolio(returns,portfolio_composition,10)

        def portfolio_annualised_performance(weights, mean_returns, cov_matrix):
            Returns = np.sum(mean_returns*weights ) *252
            std = np.sqrt(np.dot(weights.T, np.dot(cov_matrix, weights))) * np.sqrt(252)
            return std, Returns

        def simulate_modified_returns(historical_returns, forecast_days, correct_mean_by):
            h = historical_returns.copy()
            new_series = h + correct_mean_by
            return new_series.sample(n=forecast_days,replace=True).reset_index(drop=True)

        def simulate_modified_portfolio(historical_returns,composition,forecast_days):
            result = 0
            for t in composition:
                name,weight,correction = t[0], t[1],t[2]
                s = simulate_modified_returns(
                historical_returns['return_%s' % (name)], forecast_days,correction
                )
                result = result + s * weight
            return(result)

        def simulation(historical_returns,composition,forecast_days,n_iterations):
            simulated_portfolios = None
            for i in range(n_iterations):
                sim = simulate_modified_portfolio(historical_returns,composition,forecast_days)
                sim_port = pd.DataFrame({'returns_%d' % (i) : sim})
                if simulated_portfolios is None:
                    simulated_portfolios = sim_port
                else:
                    simulated_portfolios = simulated_portfolios.join(sim_port)
            return simulated_portfolios

        st.write(f" * Presenting Individual Stock Returns:\n\n{'* * '*25}\n")
        st.dataframe(returns.mean(axis=0))

        correct_mean_by_record = []
        for idx, num in enumerate(portfolio_composition):
            y = list(portfolio_composition[idx])
            rr = random.uniform(-0.0001, -0.001)
            correct_mean_by_record.append(rr)
            y.append(rr)
            portfolio_composition[idx]=tuple(y)
        forecast_days = 20
        n_iterations = 200
        simulated_portfolios = simulation(returns,portfolio_composition,forecast_days,n_iterations)

        st.dataframe(simulated_portfolios.head())
        percentile_5th = simulated_portfolios.cumsum().apply(lambda x : np.percentile(x,5),axis=1)
        percentile_95th = simulated_portfolios.cumsum().apply(lambda x : np.percentile(x,95),axis=1)
        average_port = simulated_portfolios.cumsum().apply(lambda x : np.mean(x),axis=1)

        st.write(f"\n * Presentating Percentile Metrics For Portfolio:\n\n")
        st.write(f"     > 5th Percentile == \n")
        st.dataframe(percentile_5th.tail(1))
        st.write(f"     > Portfolio Average == \n")
        st.dataframe(average_port.tail(1))
        st.write(f"     > 95th Percentile == \n\n{'* * '*25}")
        st.dataframe(percentile_95th.tail(1))
        x = range(forecast_days)

        fig, ax = plt.subplots(nrows=1, ncols=1)
        plt.rcParams['figure.figsize'] = [10, 10]
        plt.plot(x, average_port, label="Average portfolio")
        plt.xlabel("Day")
        plt.ylabel("Portfolio return")
        plt.fill_between(x, percentile_5th, percentile_95th,alpha=0.2)
        plt.grid()
        plt.legend()
        plt.tight_layout()
        st.pyplot(fig)

        target_return = 0.02
        target_prob_port = simulated_portfolios.cumsum().apply(lambda x : np.mean(x > target_return),axis=1)
        st.write(f"\n   * Probability of Target Portfolio Outperforming Target Return of:")
        st.dataframe(target_return*100)
        st.dataframe(target_prob_port.tail(1))
        err_bars = np.sqrt(target_prob_port*(1-target_prob_port)/n_iterations)
        st.write(f"\n   * Probability of Modeling Errors In Iterations == >\n\n{'* * '*25}")
        st.dataframe(err_bars.tail(1))
        x = range(forecast_days)

        fig, ax = plt.subplots(nrows=1, ncols=1)
        plt.rcParams['figure.figsize'] = [10, 10]
        plt.bar(x,target_prob_port,yerr = err_bars)
        plt.xlabel("Day")
        plt.ylabel("Probability of return >= %.2f" % (target_return))
        plt.grid()
        plt.tight_layout()
        st.pyplot(fig)

        sharpe_indices = simulated_portfolios.apply(lambda x : np.mean(x)/np.std(x))

        fig, ax = plt.subplots(nrows=1, ncols=1)
        plt.hist(sharpe_indices,bins="rice")
        plt.xlabel("Sharpe ratio")
        plt.tight_layout()
        st.pyplot(fig)

        df = pd.DataFrame(portfolio_composition)
        df.columns = ['stock_tickers', 'random_wt','mean_return_var']
        df.set_index('stock_tickers', inplace=True)
        st.write(f"  >Mean Simulated Portfolio Mean Returns == \n\n{'* * '*25}\n")
        st.dataframe(np.mean(sharpe_indices))
        st.write(f"\n  > Portfolio:\n\n\n{'* * '*25}/n")
        st.dataframe(df)

if __name__ == '__main__':
    my_positions = pd.read_pickle('/home/gordon/gdp/code/portfolio/Market_Mapping/files/tickers/chuck_merged_ticker_lst.pkl')
    Analize_Portfolio(my_positions, 'chuck_merged').situation()


# -*- coding: utf-8 -*-
"""Testing GLSAR against STATA

Created on Wed May 30 09:25:24 2012

Author: Josef Perktold
"""

import numpy as np
from numpy.testing import assert_almost_equal, assert_allclose

from statsmodels.regression.linear_model import GLSAR
from statsmodels.tools.tools import add_constant
from statsmodels.datasets import macrodata

class CheckStataResultsMixin(object):

    def test_params_table(self):
        res, results = self.res, self.results
        assert_almost_equal(res.params, results.params, 3)
        assert_almost_equal(res.bse, results.bse, 3)
        #assert_almost_equal(res.tvalues, results.tvalues, 3) 0.0003
        assert_allclose(res.tvalues, results.tvalues, atol=0, rtol=0.004)
        assert_allclose(res.pvalues, results.pvalues, atol=1e-7, rtol=0.004)

class CheckStataResultsPMixin(CheckStataResultsMixin):

    def test_predicted(self):
        res, results = self.res, self.results
        assert_allclose(res.fittedvalues, results.fittedvalues, rtol=0.002)
        predicted = res.predict(res.model.exog) #should be equal
        assert_allclose(predicted, results.fittedvalues, rtol=0.0016)
        #not yet
        #assert_almost_equal(res.fittedvalues_se, results.fittedvalues_se, 4)

class TestGLSARCorc(CheckStataResultsPMixin):

    @classmethod
    def setup_class(self):
        d2 = macrodata.load().data
        g_gdp = 400*np.diff(np.log(d2['realgdp']))
        g_inv = 400*np.diff(np.log(d2['realinv']))
        exogg = add_constant(np.c_[g_gdp, d2['realint'][:-1]], prepend=False)

        mod1 = GLSAR(g_inv, exogg, 1)
        self.res = mod1.iterative_fit(5)

        from .results.macro_gr_corc_stata import results
        self.results = results

    def test_rho(self):
        assert_almost_equal(self.res.model.rho, self.results.rho, 3)  # pylint: disable-msg=E1101

        assert_almost_equal(self.res.llf, self.results.ll, 4)

if __name__=="__main__":
    import nose
    nose.runmodule(argv=[__file__,'-vvs','-x',#'--pdb', '--pdb-failure'
                         ],
                   exit=False)


import data_loader
import kernalized_svm
import numpy as np
from utils import log
from utils import log_csv
from pathlib import Path

# C = 1000000
# kernel_type = 'linear'

def make_model_name(name, label, c):
    return Path(str(name) + '_' + str(label) + '_' + str(c) + '.txt')

def make_numeric_labels(y, correct_label):
    """
    a list of 1's and -1's where each element of y is a 1 if it is the correct
    label, othewise it is a -1
    """
    numeric_labels = []
    for l in y:
        if l == correct_label:
            numeric_labels.append(1)
        else:
            numeric_labels.append(-1)
    return numeric_labels

def calc_acc(actual_labels, preds):
    """
    returns the decimal percentage of correct predictions
    """
    count = 0
    false_neg = 0
    false_pos = 0
    true_neg = 0
    true_pos = 0
    num_examples = len(actual_labels)
    for j in range(num_examples):
        if actual_labels[j] == preds[j]:
            count += 1
            if actual_labels[j] < 0:
                true_neg += 1
            else:
                true_pos += 1
        elif preds[j] < 0:
            false_neg += 1
        else:
            false_pos += 1
    acc = count / num_examples
    if true_pos + false_pos == 0:
        precision = 'NaN'
    else:
        precision = true_pos / (true_pos + false_pos)  # cost of false pos is high
    if true_pos + false_neg == 0:
        recall = 'NaN'
    else:
        recall = true_pos / (true_pos + false_neg)  # cost of false neg is high
    if precision == 'NaN' or recall == 'NaN':
        f1 = 'NaN'
    else:
        f1 = 2 * (precision * recall) / (precision + recall)
    # return count / num_examples, false_neg, false_pos, true_neg, true_pos
    return acc, precision, recall, f1

def train_and_validate_all(k_param):
    """
    trains an SVM for each face from the data from data_loader and returns
    a dictionary of these models. Also performs k-fold cross valdiation on each
    model.
    """
    xTr_partition, yTr_partition, xV_partition, yV_partition, label_set, xAll, yAll = data_loader.load_vectors(k_param)
    models = {}

    for correct_label in label_set:
        log('Training for: ' + correct_label)
        num_partitions = len(xTr_partition)
        cumulative_acc = 0

        for i in range(num_partitions):
            xTr = xTr_partition[i]
            yTr = yTr_partition[i]
            xV = xV_partition[i]
            yV = yV_partition[i]

            tr_numeric_labels = make_numeric_labels(yTr, correct_label)
            v_numeric_labels = make_numeric_labels(yV, correct_label)

            svm_classifier = kernalized_svm.create_svm(xTr, np.array(tr_numeric_labels), C, kernel_type)
            pred = svm_classifier(np.array(xV))

            partition_acc, _, _ = calc_acc(v_numeric_labels, pred)
            log('Accuracy for partition ' + str(i) + ': ' + str(partition_acc))
            cumulative_acc += partition_acc

        overall_acc = cumulative_acc / num_partitions
        log('Overall cross-validation accuracy: ' + str(overall_acc))

        tr_all_numeric_labels = make_numeric_labels(yAll, correct_label)

        final_svm_classifier = kernalized_svm.create_svm(xAll, np.array(tr_all_numeric_labels), C, kernel_type)
        models[correct_label] = final_svm_classifier

        final_preds = final_svm_classifier(xAll)

        training_acc, _, _, _ = calc_acc(tr_all_numeric_labels, final_preds)
        log("Final training accuracy: " + str(training_acc))
        log('')

    return models

def train_model_all(path_name, C, kernel_type, model_path):
    """
    Trains models for everyone in directory
    """

    xTr, yTr, label_set = data_loader.load_vectors(path_name, 'tr_data.txt')
    models = {}

    log('C: ' + str(C))
    log('kernel type: ' + kernel_type + '\n')

    for correct_label in label_set:
        log('Training for: ' + correct_label)
        tr_numeric_labels = make_numeric_labels(yTr, correct_label)
        svm_classifier = kernalized_svm.create_svm(xTr, np.array(tr_numeric_labels), C, kernel_type, make_model_name(model_path, correct_label, C))
        models[correct_label] = svm_classifier
        preds = svm_classifier(xTr)
        training_acc, precision, recall, f1 = calc_acc(tr_numeric_labels, preds)
        log("Training accuracy: " + str(training_acc))
        log('Precision: ' + str(precision))
        log('Recall: ' + str(recall))
        log('F1 score: ' + str(f1))
        log('')
        stats_list = [model_path, correct_label, C, kernel_type, training_acc, precision, recall, f1]
        log_csv(stats_list)

    return models

def train_model(pos_path_name, neg_path_name, C, kernel_type, model_path):

    xTr_pos, yTr_pos, pos_label = data_loader.load_vectors_of_class(pos_path_name, 'tr_data.txt', 1)
    xTr_neg, yTr_neg, neg_label = data_loader.load_vectors_of_class(neg_path_name, 'tr_data.txt', -1)
    xTr = np.concatenate((xTr_pos, xTr_neg))
    yTr = np.concatenate((yTr_pos, yTr_neg))

    log('C: ' + str(C))
    log('kernel type: ' + kernel_type + '\n')

    log('Training for: ' + pos_label)
    # tr_numeric_labels = make_numeric_labels(yTr, correct_label)
    svm_classifier = kernalized_svm.create_svm(xTr, yTr, C, kernel_type, make_model_name(model_path, pos_label, C))
    preds = svm_classifier(xTr)
    training_acc, precision, recall, f1 = calc_acc(yTr, preds)
    log("Training accuracy: " + str(training_acc))
    log('Precision: ' + str(precision))
    log('Recall: ' + str(recall))
    log('F1 score: ' + str(f1))
    log('')
    stats_list = [model_path, pos_label, C, kernel_type, training_acc, precision, recall, f1]
    log_csv(stats_list)

    return svm_classifier

def test_model_all(models, path_name, text_file, test_or_validation, model_path, C, kernel_type):
    xTest, yTest, label_set = data_loader.load_vectors(path_name, text_file)
    for correct_label in label_set:
        if correct_label in models:
            test_numeric_labels = make_numeric_labels(yTest, correct_label)
            test_pred = models[correct_label](np.array(xTest))
            # test_acc, false_neg, false_pos, true_neg, true_pos = calc_acc(test_numeric_labels, test_pred)
            # log(test_or_validation + ' accuracy for ' + correct_label + ': ' + str(test_acc))
            # log('False negatives for ' + correct_label + ': ' + str(false_neg))
            # log('False positives for ' + correct_label + ': ' + str(false_pos))
            # log('True negatives for ' + correct_label + ': ' + str(true_neg))
            # log('True positives for ' + correct_label + ': ' + str(true_pos))
            # log('')
            acc, precision, recall, f1 = calc_acc(test_numeric_labels, test_pred)
            log(test_or_validation + ' accuracy for ' + correct_label + ': ' + str(acc))
            log('Precision for ' + correct_label + ': ' + str(precision))
            log('Recall for ' + correct_label + ': ' + str(recall))
            log('F1 score for ' + correct_label + ': ' + str(f1))
            log('')
            stats_list = [model_path, path_name, correct_label, C, kernel_type, acc, precision, recall, f1]
            log_csv(stats_list)

def test_model(model, pos_path_name, neg_path_name, text_file, test_or_validation):
    xTest_pos, yTest_pos, pos_label = data_loader.load_vectors_of_class(pos_path_name, text_file, 1)
    xTest_neg, yTest_neg, neg_label = data_loader.load_vectors_of_class(neg_path_name, text_file, -1)
    xTest = np.concatenate((xTest_pos, xTest_neg))
    yTest = np.concatenate((yTest_pos, yTest_neg))

    test_pred = model(np.array(xTest))

    acc, precision, recall, f1 = calc_acc(yTest, test_pred)
    log(test_or_validation + ' accuracy for ' + pos_label + ': ' + str(acc))
    log('Precision for ' + pos_label + ': ' + str(precision))
    log('Recall for ' + pos_label + ': ' + str(recall))
    log('F1 score for ' + pos_label + ': ' + str(f1))
    log('')


#!/home/cachemoi/anaconda3/bin/python

#This is the version on the server

import sympy as sp
import numpy as np
import sys
import json
import os
import string
import random

def CalcScaleParam_bounce(mode,CIfact, percentage):
    """

    This will calculate the value of sigma and mu of a theoretical log normal distribution with an arbitrary amount of
    precision given a Confidence interval factor and a mode.

    The equation to find mu and sigma is a system of 2 equation.
        - The CDF
        - The mode

    The system has been rewritten so that we can find the sigma first, and then use this to determine the mu.

    """
    upperbound = sys.float_info.max
    lowerbound = sys.float_info.min
    precision = 0.00001

    Xmin = mode / CIfact
    Xmax = mode * CIfact

    s = sp.Symbol('s')

    eqn = (1 / 2 * sp.erf((sp.log(Xmax) - (sp.log(mode) + s ** 2)) / (sp.sqrt(2) * s)) - (
    1 / 2 * sp.erf((sp.log(Xmin) - (sp.log(mode) + s ** 2)) / (sp.sqrt(2) * s)))) - percentage

    while not ((upperbound -lowerbound) <= precision):
        testbound = lowerbound + (upperbound-lowerbound)/2

        f_lowerbound = eqn.evalf(subs={s: lowerbound})
        f_testbound = eqn.evalf(subs={s: testbound})

        if (f_lowerbound *f_testbound > 0):
            lowerbound=testbound
        else:
            upperbound=testbound
            sigma= float(testbound)

    mu = sp.log(mode) + sigma ** 2

    mu = float(mu.evalf())

    return (mu, sigma)

def MyLogRand(mu, sigma, sample_size):

    """

    :param mu:
     the mean of a population

    :param sigma:
    measure of variation and dispesion of a set of data value

    :sample_size:
    The desired number of samples

    :return:
    This function will return the desired number of random samples taken from the lognormal distribution with the
    specified mu and sigma.

    It also outputs a graph for now (debugging)

    """

    sample = np.random.lognormal(mu, sigma, sample_size)
    sample = sample.tolist()

    return sample

def ID_Generator(size=10, chars=string.ascii_uppercase + string.digits):

    """
    :param size: the length of the random string
    :param chars: the type of characters in the random string
    :return: a random string to add to our filename as unique ID
    """

    return ''.join(random.choice(chars) for _ in range(size))

def GenerateFileName (data_type, ID):

    """

    :param data_type: the file name in which the data will be saved
    :param ID: the ID of our file
    :return: the filepath in which our file will be saved
    """

    random_part = ID_Generator()

    path = "/home/cachemoi/Desktop/Programs/Web/SimParamNode/public/results/" + data_type + "/" + ID + "-" + random_part + ".csv"

    return path

def SaveSamples(param_ID, samples):

    """

    :param param_ID: The ID of the parameter we want to save
    :param samples: the array of samples we generated
    :return: This function will save the file
    """

    samples_filename = GenerateFileName("Samples", param_ID)

    with open(samples_filename, 'w', encoding='utf-8-sig') as file:
        file.write(str(param_ID) + ",\n")

        for value in samples:
            file.write(str(value) + ",\n")

def main(input):
    mode = float(commandIN["choices"]["mode"])
    confidence = float(commandIN["choices"]["confidence"])
    sample_size = float(commandIN["choices"]["sample_size"])
    percentage = float(commandIN["choices"]["percentage"])

    mu,sigma = CalcScaleParam_bounce(mode,confidence,percentage)
    sample = MyLogRand(mu, sigma, sample_size)

    param_ID = ID_Generator()

    SaveSamples(param_ID, sample)

    return sample

#mu, sigma = CalcScaleParam_bounce(2,2,0.95)

#sample = MyLogRand(mu, sigma, 10000)

commandIN =sys.stdin.read()
commandIN = json.loads(commandIN)
sample = main(commandIN)

print(json.dumps(sample))


from tkinter import *
import platform
master=Tk()
master.geometry("300x100")
master.title("Button Window")
def new_window():
    newwin=Toplevel(master)
    newwin.geometry("550x200")
    newwin.title("Display Window")
    quote="This is just a popup window"
    w1=Label(newwin,text=quote,font="Helvetica 12 bold")
    w1.pack()
b1=Button(master,text="Click Me..!",font='calibri 10 bold',pady='5',command=new_window)
b1.pack()
master.mainloop()


import os
import datetime
import smtplib
from email.mime.text import MIMEText
date= datetime.datetime.now() - datetime.timedelta(0,86400) #check for file associated with two hours ago- 7200 seconds
date = date.strftime('%Y-%m-%d %H')
year = date[:4]
month= date[5:7]
day= date[8:10]
hour = date[11:13]
DHG = year  + month  + day
fname = 'nwm.t18z.short_range.terrain_rt.f018.conus.nc'
dirname = '/data/nwm_data/short_range/'+DHG+'/t18/'
#print(dirname)
#filepath=os.path.join(dirname, DHG)
server = smtplib.SMTP('smtp.gmail.com:587')
server.ehlo()
server.starttls()
server.login('bop.harvard@gmail.com','433633ab') #need to change this to something else?

msg = MIMEText('File missing or less than 1 MB --> '+fname +' Related Date_Time: ' + DHG )
msg['Subject'] = 'NWM Harvester Email Alert'
msg['From'] = 'bop.harvard@gmail.com'
msg['To'] = 'devikakakkar29@gmail.com'

file_exists = os.path.isfile(os.path.join(dirname, fname))
#print(file_exists)
if not file_exists:
    server.send_message(msg)
    server.quit()
elif os.stat(os.path.join(dirname, fname)).st_size < 10^6: # if file size < 1MB
    server.send_message(msg)
    server.quit()
else:
    print("File found", fname)


from collections import Counter
# https://leetcode.com/problems/unique-number-of-occurrences/ 87.75%
class Solution:
    def uniqueOccurrences(self, arr: List[int]) -> bool:
        d = dict(Counter(arr))
        s = set()

        for ele in d.values():
            s.add(ele)
        if len(s) == len(d.keys()):
            return True

        return False
    ''' or
        d = dict(Counter(arr))
        return len(d) == len(set(d.values()))
    '''

HackerRank/Unique Number of Occurrences.py


#coding: utf8
#author: Tian Xia (SummerRainET2008@gmail.com)

from pa_nlp import common as nlp
import time
import multiprocessing as mp
from pa_nlp.audio.audio_helper import AudioHelper
import optparse
import collections
import os

def check_1(folder: str):
  mp3_files = [os.path.basename(fn).rpartition(".")[0]
               for fn in nlp.get_files_in_folder(folder, ["mp3"])]
  txt_files = [os.path.basename(fn).rpartition(".")[0]
               for fn in nlp.get_files_in_folder(folder, ["txt"])]

  mp3_counts = collections.Counter(mp3_files)
  txt_counts = collections.Counter(txt_files)
  if (len(mp3_files) == len(txt_files) and
    len(mp3_counts) == len(txt_counts)):
    return

  for mp3 in mp3_counts:
    if mp3 not in txt_files:
      print(f"ERR: no {mp3}.txt found")

  for txt in txt_files:
    if txt not in mp3_counts:
      print(f"ERR: no {mp3}.mp3 found")

def main():
  parser = optparse.OptionParser(usage="cmd [optons] ..]")
  #parser.add_option("-q", "--quiet", action="store_true", dest="verbose",
  parser.add_option("--data_folder")
  #default=False, help="")
  (options, args) = parser.parse_args()

  check_1(options.data_folder)

if __name__ == '__main__':
  main()


#!/usr/bin/env python
# coding=utf-8
#
# Author: Lucas
# Date: 2019-07-28 10:36:53

class Solution(object):
    def alphabetBoardPath(self, target):
        """
        :type target: str
        :rtype: str
        """
        board = ["abcde", "fghij", "klmno", "pqrst", "uvwxy", "z"]
        position = {}
        for i in range(len(board)):
            for j in range(len(board[i])):
                position[board[i][j]] = [i, j]

        i, j = 0, 0
        index = 0
        ans = []
        while index < len(target):
            c = target[index]
            index += 1
            next_i, next_j = position[c]
            if i > next_i:
                ans.extend(['U'] * (i - next_i))
            if next_i > i:
                if next_i == 5:
                    ans.extend(['D'] * (4 - i))
                else:
                    ans.extend(['D'] * (next_i - i))
            if j > next_j:
                ans.extend(['L'] * (j - next_j))
            if next_j > j:
                ans.extend(['R'] * (next_j - j))
            if next_i == 5 and next_i > i:
                ans.append('D')
            ans.append('!')
            i, j = next_i, next_j
        return ''.join(ans)

# TestCode
import unittest

class TestSolution(unittest.TestCase):
    def test_solution(self):
        self.assertEqual(
            Solution().alphabetBoardPath('leet'),
            "DDR!UURRR!!DDD!"
        )

    def test_solution2(self):
        self.assertEqual(
            Solution().alphabetBoardPath('code'),
            "RR!DDRR!UUL!R!"
        )

    def test_solution4(self):
        self.assertEqual(
            Solution().alphabetBoardPath('zdz'),
            "DDDDD!UUUUURRR!DDDDLLLD!"
        )

if __name__ == '__main__':
    unittest.main()


# -*- coding: utf-8 -*-
"""
Created on Fri Dec 18 2020

Laser ultrasonics: Semi-analytical model

@author: Romain HODE
"""

import numpy as np
import time as duration
from Dot_prod import dot4444, dot2444, dot2441, dot4441

def Homogeneous_solution(omega, k2, medium):

    # Elastic coefficients, density and thickness of the medium
    c11, c12, c22, c66, rho, h = medium
    # Terms of the matrix M
    a11 = rho*omega**2-k2**2*c66
    a22 = rho*omega**2-k2**2*c22
    a12 = k2*(c12+c66)
    # Coefficients of the quadratic equation
    a_coef = c11*c66
    b_coef = -(c11*a22+c66*a11+a12**2)
    c_coef = a11*a22
    # Solutions of the quadratic equation
    det = b_coef**2-4*a_coef*c_coef
    pow2_k1_t = (-b_coef+np.sqrt(det))/(2*a_coef)
    pow2_k1_l = (-b_coef-np.sqrt(det))/(2*a_coef)
    k1_t = np.sqrt(pow2_k1_t) # real part: positive, imaginary part: negative
    k1_l = np.sqrt(pow2_k1_l) # real part: positive, imaginary part: negative
    # Homogeneous solution
    u1h_l = a22-k1_l**2*c66
    u1h_t = a22-k1_t**2*c66
    u2h_l = -k1_l*a12
    u2h_t = -k1_t*a12

    return  u1h_l, u1h_t, u2h_l, u2h_t, k1_l, k1_t

def Particular_solution_gamma(omega, k2, medium_i, th_prop_i, d_i, T_i):

    # Thermal properties
    cp_1, lambda1_mi, lambda2_mi, C_alpha1_mi, C_alpha2_mi = th_prop_i
    # Elastic coefficients, density and thickness of the medium
    c11_i, c12_i, c22_i, c66_i, rho_i, h_i = medium_i
    # Terms of the matrix M
    a11 = rho_i*omega**2-k2**2*c66_i
    a22 = rho_i*omega**2-k2**2*c22_i
    a12 = k2*(c12_i+c66_i)
    # Mat a_t
    xi_11 = a11+c11_i*d_i**2
    xi_12 = 1j*a12*d_i
    xi_22 = a22+c66_i*d_i**2
    a_t = np.array([[xi_11, xi_12],[xi_12, xi_22]])
    # Vec b_t
    b_t = np.array([[-C_alpha1_mi*d_i*T_i], [-1j*k2*C_alpha2_mi*T_i]])
    #%% Solve np.linalg.solve
    A = np.swapaxes(np.swapaxes(a_t,0,2),1,3)
    B = np.swapaxes(np.swapaxes(b_t,0,2),1,3)
    sol = np.linalg.solve(A, B)
    up_1, up_2 = np.swapaxes(np.swapaxes(sol,0,2),1,3)[:,0,:,:]

    return up_1, up_2

def Particular_solution_beta(omega, k2, medium_i, th_prop_i, d_i, T_i, theta_i):

    # Thermal properties
    cp_1, lambda1_mi, lambda2_mi, C_alpha1_mi, C_alpha2_mi = th_prop_i
    # Elastic coefficients, density and thickness of the medium
    c11_i, c12_i, c22_i, c66_i, rho_i, h_i = medium_i
    # Terms of the matrix M
    a11 = rho_i*omega**2-k2**2*c66_i
    a22 = rho_i*omega**2-k2**2*c22_i
    a12 = k2*(c12_i+c66_i)
    # Mat a_t
    xi_11 = a11+c11_i*(d_i-1j*k2*np.tan(theta_i))**2
    xi_12 = 1j*a12*(d_i-1j*k2*np.tan(theta_i))
    xi_22 = a22+c66_i*(d_i-1j*k2*np.tan(theta_i))**2
    a_t = np.array([[xi_11, xi_12],[xi_12, xi_22]])
    # Vec b_t
    b_t = np.array([[-C_alpha1_mi*(d_i-1j*k2*np.tan(theta_i))*T_i], [-1j*k2*C_alpha2_mi*T_i]])
    #%% Solve np.linalg.solve
    A = np.swapaxes(np.swapaxes(a_t,0,2),1,3)
    B = np.swapaxes(np.swapaxes(b_t,0,2),1,3)
    sol = np.linalg.solve(A, B)
    up_1, up_2 = np.swapaxes(np.swapaxes(sol,0,2),1,3)[:,0,:,:]

    return up_1, up_2

def Boundary_conditions_Particular_solutions_Source_Term(omega, k2, medium, beta,
                                                         th_prop_m1, th_prop_m2, t_gamma, Tp, Th,
                                                         upf_beta_1, upb_beta_1, upf_gamma_m1, upb_gamma_m1,
                                                         upf_beta_2, upb_beta_2, upf_gamma_m2, upb_gamma_m2,
                                                         theta_1, theta_2, dh):
    #%% beta, t_gamma, Tp, Th
    beta_1, beta_2 = beta
    gamma_m1, gamma_m2 = t_gamma
    Tpf_beta_1, Tpb_beta_1, Tpf_beta_2, Tpb_beta_2 = Tp
    Thf_m1, Thb_m1_star, Thf_m2_star, Thb_m2_star = Th
    #%% Particular solution medium 1
    u1pf_beta_m1, u2pf_beta_m1 = upf_beta_1
    u1pb_beta_m1, u2pb_beta_m1 = upb_beta_1
    u1pf_gamma_m1, u2pf_gamma_m1 = upf_gamma_m1
    u1pb_gamma_m1, u2pb_gamma_m1 = upb_gamma_m1
    #%% Particular solution medium 2
    u1pf_beta_m2, u2pf_beta_m2 = upf_beta_2
    u1pb_beta_m2, u2pb_beta_m2 = upb_beta_2
    u1pf_gamma_m2, u2pf_gamma_m2 = upf_gamma_m2
    u1pb_gamma_m2, u2pb_gamma_m2 = upb_gamma_m2
    #%% Elastic coefficients, density and thickness of the medium
    c11_1, c12_1, c22_1, c66_1, rho_1, h_1 = medium[0]
    c11_2, c12_2, c22_2, c66_2, rho_2, h_2 = medium[1]
    H = h_1+dh+h_2 # Total thickness (mm)
    #%% Thermal properties of medium 1 and medium 2
    cp_1, lambda1_m1, lambda2_m1, C_alpha1_m1, C_alpha2_m1 = th_prop_m1
    cp_2, lambda1_m2, lambda2_m2, C_alpha1_m2, C_alpha2_m2 = th_prop_m2
    #%% Limit conditions - Particular solutions
    # Free surface x1=0, Medium 1
        # f: forward exp(j(omega*t-k*x1)), example: up1_gammaf_m1
        # b: backward exp(j(omega*t+k*x1)), example: up1_gammab_m1
    u1p0_m1 = u1pf_beta_m1+u1pb_beta_m1*np.exp(-beta_1*h_1)+\
                u1pf_gamma_m1+u1pb_gamma_m1*np.exp(-gamma_m1*h_1)
    du1p0_m1 = -(beta_1-1j*k2*np.tan(theta_1))*u1pf_beta_m1+(beta_1-1j*k2*np.tan(theta_1))*u1pb_beta_m1*np.exp(-beta_1*h_1)- \
                gamma_m1*u1pf_gamma_m1+gamma_m1*u1pb_gamma_m1*np.exp(-gamma_m1*h_1)
    u2p0_m1 = u2pf_beta_m1+u2pb_beta_m1*np.exp(-beta_1*h_1)+\
                u2pf_gamma_m1+u2pb_gamma_m1*np.exp(-gamma_m1*h_1)
    du2p0_m1 = -(beta_1-1j*k2*np.tan(theta_1))*u2pf_beta_m1+(beta_1-1j*k2*np.tan(theta_1))*u2pb_beta_m1*np.exp(-beta_1*h_1)- \
                gamma_m1*u2pf_gamma_m1+gamma_m1*u2pb_gamma_m1*np.exp(-gamma_m1*h_1)
    T0_m1 = Thf_m1+Thb_m1_star*np.exp(-gamma_m1*h_1)+Tpf_beta_1+Tpb_beta_1*np.exp(-beta_1*h_1)
    sigma11ps0_m1 = c11_1*du1p0_m1-1j*k2*c12_1*u2p0_m1-C_alpha1_m1*T0_m1
    sigma12ps0_m1 = c66_1*(-1j*k2*u1p0_m1+du2p0_m1)
    # Interface 1, x1=h1, Medium 1
    u1ph1_m1 = u1pf_beta_m1*np.exp(-beta_1*h_1)*np.exp(1j*k2*np.tan(theta_1)*h_1)+u1pb_beta_m1*np.exp(-1j*k2*np.tan(theta_1)*h_1)+\
                u1pf_gamma_m1*np.exp(-gamma_m1*h_1)+u1pb_gamma_m1
    du1ph1_m1 = -(beta_1-1j*k2*np.tan(theta_1))*u1pf_beta_m1*np.exp(-beta_1*h_1)*np.exp(1j*k2*np.tan(theta_1)*h_1)+\
                (beta_1-1j*k2*np.tan(theta_1))*u1pb_beta_m1*np.exp(-1j*k2*np.tan(theta_1)*h_1)- \
                gamma_m1*u1pf_gamma_m1*np.exp(-gamma_m1*h_1)+gamma_m1*u1pb_gamma_m1
    u2ph1_m1 = u2pf_beta_m1*np.exp(-beta_1*h_1)*np.exp(1j*k2*np.tan(theta_1)*h_1)+u2pb_beta_m1*np.exp(-1j*k2*np.tan(theta_1)*h_1)+\
                u2pf_gamma_m1*np.exp(-gamma_m1*h_1)+u2pb_gamma_m1
    du2ph1_m1 = -(beta_1-1j*k2*np.tan(theta_1))*u2pf_beta_m1*np.exp(-beta_1*h_1)*np.exp(1j*k2*np.tan(theta_1)*h_1)+\
                (beta_1-1j*k2*np.tan(theta_1))*u2pb_beta_m1*np.exp(-1j*k2*np.tan(theta_1)*h_1)- \
                gamma_m1*u2pf_gamma_m1*np.exp(-gamma_m1*h_1)+gamma_m1*u2pb_gamma_m1
    Th1_m1 = Thf_m1*np.exp(-gamma_m1*h_1)+Thb_m1_star+Tpf_beta_1*np.exp(-beta_1*h_1)*np.exp(1j*k2*np.tan(theta_1)*h_1)+Tpb_beta_1*np.exp(-1j*k2*np.tan(theta_1)*h_1)
    sigma11psh1_m1 = c11_1*du1ph1_m1-1j*k2*c12_1*u2ph1_m1-C_alpha1_m1*Th1_m1
    sigma12psh1_m1 = c66_1*(-1j*k2*u1ph1_m1+du2ph1_m1)
    # Interface 2, x1=h1+dh, Medium 2
    u1ph1dh_m2 = u1pf_beta_m2*np.exp(1j*k2*np.tan(theta_2)*(h_1+dh))+\
                u1pb_beta_m2*np.exp(-beta_2*h_2)*np.exp(-1j*k2*np.tan(theta_2)*(h_1+dh))+ \
                u1pf_gamma_m2+u1pb_gamma_m2*np.exp(-gamma_m2*h_2)
    du1ph1dh_m2 = -(beta_2-1j*k2*np.tan(theta_2))*u1pf_beta_m2*np.exp(1j*k2*np.tan(theta_2)*(h_1+dh))+\
                    (beta_2-1j*k2*np.tan(theta_2))*u1pb_beta_m2*np.exp(-beta_2*h_2)*np.exp(-1j*k2*np.tan(theta_2)*(h_1+dh))- \
                    gamma_m2*u1pf_gamma_m2+gamma_m2*u1pb_gamma_m2*np.exp(-gamma_m2*h_2)
    u2ph1dh_m2 = u2pf_beta_m2*np.exp(1j*k2*np.tan(theta_2)*(h_1+dh))+\
                    u2pb_beta_m2*np.exp(-beta_2*h_2)*np.exp(-1j*k2*np.tan(theta_2)*(h_1+dh))+\
                    u2pf_gamma_m2+u2pb_gamma_m2*np.exp(-gamma_m2*h_2)
    du2ph1dh_m2 = -(beta_2-1j*k2*np.tan(theta_2))*u2pf_beta_m2*np.exp(1j*k2*np.tan(theta_2)*(h_1+dh))+\
                    (beta_2-1j*k2*np.tan(theta_2))*u2pb_beta_m2*np.exp(-beta_2*h_2)*np.exp(-1j*k2*np.tan(theta_2)*(h_1+dh))-\
                    gamma_m2*u2pf_gamma_m2+gamma_m2*u2pb_gamma_m2*np.exp(-gamma_m2*h_2)
    Th1dh_m2 = Thf_m2_star+Thb_m2_star*np.exp(-gamma_m2*h_2)+\
                Tpf_beta_2*np.exp(1j*k2*np.tan(theta_2)*(h_1+dh))+Tpb_beta_2*np.exp(-beta_2*h_2)*np.exp(-1j*k2*np.tan(theta_2)*(h_1+dh))
    sigma11psh1dh_m2 = c11_2*du1ph1dh_m2-1j*k2*c12_2*u2ph1dh_m2-C_alpha1_m2*Th1dh_m2
    sigma12psh1dh_m2 = c66_2*(-1j*k2*u1ph1dh_m2+du2ph1dh_m2)
    # Free surface x1=H, Medium 2
    u1pH_m2 = u1pf_beta_m2*np.exp(-beta_2*h_2)*np.exp(1j*k2*np.tan(theta_2)*H)+\
                u1pb_beta_m2*np.exp(-1j*k2*np.tan(theta_2)*H)+ \
                u1pf_gamma_m2*np.exp(-gamma_m2*h_2)+u1pb_gamma_m2
    du1pH_m2 = -(beta_2-1j*k2*np.tan(theta_2))*u1pf_beta_m2*np.exp(-beta_2*h_2)*np.exp(1j*k2*np.tan(theta_2)*H)+\
                (beta_2-1j*k2*np.tan(theta_2))*u1pb_beta_m2*np.exp(-1j*k2*np.tan(theta_2)*H)- \
                gamma_m2*u1pf_gamma_m2*np.exp(-gamma_m2*h_2)+gamma_m2*u1pb_gamma_m2
    u2pH_m2 = u2pf_beta_m2*np.exp(-beta_2*h_2)*np.exp(1j*k2*np.tan(theta_2)*H)+\
                u2pb_beta_m2*np.exp(-1j*k2*np.tan(theta_2)*H)+\
                u2pf_gamma_m2*np.exp(-gamma_m2*h_2)+u2pb_gamma_m2
    du2pH_m2 = -(beta_2-1j*k2*np.tan(theta_2))*u2pf_beta_m2*np.exp(-beta_2*h_2)*np.exp(1j*k2*np.tan(theta_2)*H)+\
                (beta_2-1j*k2*np.tan(theta_2))*u2pb_beta_m2*np.exp(-1j*k2*np.tan(theta_2)*H)-\
                gamma_m2*u2pf_gamma_m2*np.exp(-gamma_m2*h_2)+gamma_m2*u2pb_gamma_m2
    TH_m2 = Thf_m2_star*np.exp(-gamma_m2*h_2)+Thb_m2_star+Tpf_beta_2*np.exp(-beta_2*h_2)*np.exp(1j*k2*np.tan(theta_2)*H)+\
            Tpb_beta_2*np.exp(-1j*k2*np.tan(theta_2)*H)
    sigma11psH_m2 = c11_2*du1pH_m2-1j*k2*c12_2*u2pH_m2-C_alpha1_m2*TH_m2
    sigma12psH_m2 = c66_2*(-1j*k2*u1pH_m2+du2pH_m2)

    return sigma11ps0_m1, sigma12ps0_m1, sigma11psH_m2, sigma12psH_m2, \
            u1ph1_m1, u2ph1_m1, sigma11psh1_m1, sigma12psh1_m1, \
            u1ph1dh_m2, u2ph1dh_m2, sigma11psh1dh_m2, sigma12psh1dh_m2

def Boundary_conditions_Homogeneous_solutions(omega, k2, medium, sh_m1, sh_m2):

    #%% Homogeneous solution medium 1
    u1h_lm1, u1h_tm1, u2h_lm1, u2h_tm1, k1_lm1, k1_tm1 = sh_m1
    #%% Homogeneous solution medium 2
    u1h_lm2, u1h_tm2, u2h_lm2, u2h_tm2, k1_lm2, k1_tm2 = sh_m2
    #%% Elastic coefficients, density and thickness of the medium
    c11_1, c12_1, c22_1, c66_1, rho_1, h_1 = medium[0]
    c11_2, c12_2, c22_2, c66_2, rho_2, h_2 = medium[1]
    #%% Medium 1
    rl_m1 = c11_1*k1_lm1*u1h_lm1-c12_1*k2*u2h_lm1
    rt_m1 = c11_1*k1_tm1*u1h_tm1-c12_1*k2*u2h_tm1
    ml_m1 = c66_1*(-1j*k2*u1h_lm1+1j*k1_lm1*u2h_lm1)
    mt_m1 = c66_1*(-1j*k2*u1h_tm1+1j*k1_tm1*u2h_tm1)
    sl1 = (1.-np.exp(-1j*k1_lm1*h_1))/(2.*1j)
    st1 = (1.-np.exp(-1j*k1_tm1*h_1))/(2.*1j)
    cl1 = (1.+np.exp(-1j*k1_lm1*h_1))/2.
    ct1 = (1.+np.exp(-1j*k1_tm1*h_1))/2.
    #%% Medium 2
    rl_m2 = c11_2*1j*k1_lm2*u1h_lm2-c12_2*1j*k2*u2h_lm2
    rt_m2 = c11_2*1j*k1_tm2*u1h_tm2-c12_2*1j*k2*u2h_tm2
    ml_m2 = c66_2*(-1j*k2*u1h_lm2+1j*k1_lm2*u2h_lm2)
    mt_m2 = c66_2*(-1j*k2*u1h_tm2+1j*k1_tm2*u2h_tm2)
    el_m2 = np.exp(-1j*k1_lm2*h_2)
    et_m2 = np.exp(-1j*k1_tm2*h_2)
    #%% Limit conditions - Homogeneous solutions
    # Free surface, x1 = 0, medium 1
    psi_0_m1_star = np.array([[rl_m1*sl1, rt_m1*st1, rl_m1*cl1, rt_m1*ct1],
                              [ml_m1*cl1, mt_m1*ct1, -ml_m1*sl1, -mt_m1*st1]])
    # Interface 1, x1 = h1, medium 1
    psi_h1_m1_star = np.array([[u1h_lm1*cl1, u1h_tm1*ct1, u1h_lm1*sl1, u1h_tm1*st1],
                               [1j*u2h_lm1*sl1, 1j*u2h_tm1*st1, -1j*u2h_lm1*cl1, -1j*u2h_tm1*ct1],
                               [-rl_m1*sl1, -rt_m1*st1, rl_m1*cl1, rt_m1*ct1],
                               [ml_m1*cl1, mt_m1*ct1, ml_m1*sl1, mt_m1*st1]])
    psi_h1_m1_u_star = psi_h1_m1_star[0:2,:,:,:]
    psi_h1_m1_sigma_star = psi_h1_m1_star[2::,:,:,:]
    # Interface 2, x1 = h1+dh, medium 2
    psi_h1dh_m2_star = np.array([[u1h_lm2*el_m2, u1h_lm2, u1h_tm2*et_m2, u1h_tm2],
                                 [u2h_lm2*el_m2, -u2h_lm2, u2h_tm2*et_m2, -u2h_tm2],
                                 [rl_m2*el_m2, -rl_m2, rt_m2*et_m2, -rt_m2],
                                 [ml_m2*el_m2, ml_m2, mt_m2*et_m2, mt_m2]])
    # Free surface, x1 = H, medium 2
    psi_H_m2_star = np.array([[rl_m2, -rl_m2*el_m2, rt_m2, -rt_m2*et_m2],
                              [ml_m2, ml_m2*el_m2, mt_m2, mt_m2*et_m2]])
    # Stress: Interface 1 (x1 = h1) + Stress: Free surface (x1 = 0)
    gamma_psi_h_star = np.zeros((4,4, len(omega[:, 0]), len(k2[0, :])),complex)
    gamma_psi_h_star[0:2,:,:,:] = psi_h1_m1_sigma_star
    gamma_psi_h_star[2::,:,:,:] = psi_0_m1_star
    #%% Inverse of Matrix gamma_psi_h_star
    gamma_psi_h_star_swap_axis = np.swapaxes(np.swapaxes(gamma_psi_h_star,0,2),1,3)
    inv_gamma_psi_h_star_swap_axis = np.linalg.inv(gamma_psi_h_star_swap_axis)
    inv_gamma_psi_h_star = np.swapaxes(np.swapaxes(inv_gamma_psi_h_star_swap_axis,0,2),1,3)

    return psi_h1_m1_u_star, gamma_psi_h_star, inv_gamma_psi_h_star, psi_h1dh_m2_star, psi_H_m2_star

def Interfacial_layer(omega, pow_kn, pow_kt):

    KN = np.power(10, pow_kn) #+1j*np.real(omega)*0.001 # kN/mm3 (multiply by 1e12 to obtain N/m3)
    KT = np.power(10, pow_kt) #+1j*np.real(omega)*0.001 # kN/mm3 (multiply by 1e12 to obtain N/m3)
    one = np.ones(np.shape(omega), complex)
    zero = np.zeros(np.shape(omega), complex)
    K = np.array([[one, zero, -one/KN, zero],
                  [zero, one, zero, -one/KT],
                  [zero, zero, one, zero],
                  [zero, zero, zero, one]])
    return K

def Perfect_coupling(omega):

    one = np.ones(np.shape(omega), complex)
    zero = np.zeros(np.shape(omega), complex)
    K = np.array([[one, zero, zero, zero],
                  [zero, one, zero, zero],
                  [zero, zero, one, zero],
                  [zero, zero, zero, one]])
    return K

def Orthotropic_layer(omega, k2, medium_i, sh_i):

    # Elastic coefficients, density and thickness of the medium
    c11_i, c12_i, c22_i, c66_i, rho, h_i = medium_i
    u1_l_i, u1_t_i, u2_l_i, u2_t_i, k1_l_i, k1_t_i = sh_i
    #%% rl, rt, ml, mt
    rl = c11_i*1j*k1_l_i*u1_l_i-c12_i*1j*k2*u2_l_i
    rt = c11_i*1j*k1_t_i*u1_t_i-c12_i*1j*k2*u2_t_i
    ml = c66_i*(-1j*k2*u1_l_i+1j*k1_l_i*u2_l_i)
    mt = c66_i*(-1j*k2*u1_t_i+1j*k1_t_i*u2_t_i)
    #%% Mat x1 = h
    e12 = np.exp(-1j*k1_l_i*h_i)
    e14 = np.exp(-1j*k1_t_i*h_i)
    psi_h_i = np.array([[u1_l_i, u1_l_i*e12, u1_t_i, u1_t_i*e14],
                         [u2_l_i, -u2_l_i*e12, u2_t_i, -u2_t_i*e14],
                         [rl, -rl*e12, rt, -rt*e14],
                         [ml, ml*e12, mt, mt*e14]])
    inv_psi_h_i_swap = np.linalg.inv(np.swapaxes(np.swapaxes(psi_h_i,0,2),1,3))
    inv_psi_h_i = np.swapaxes(np.swapaxes(inv_psi_h_i_swap,0,2),1,3)
    #%% Mat x1 = 0
    e11 = e12
    e13 = e14
    psi_0 = np.array([[u1_l_i*e11, u1_l_i, u1_t_i*e13, u1_t_i],
                         [u2_l_i*e11, -u2_l_i, u2_t_i*e13, -u2_t_i],
                         [rl*e11, -rl, rt*e13, -rt],
                         [ml*e11, ml, mt*e13, mt]])
    #%% Matrix B
    B = dot4444(psi_0, inv_psi_h_i)

    return B

def Coefficients_xis_xia_af_ab(omega, k2, L,
                               psi_h1_m1_u_star,gamma_psi_h_star,inv_gamma_psi_h_star,psi_h1dh_m2_star,psi_H_m2_star,
                               sigma11ps0_m1, sigma12ps0_m1, sigma11psH_m2, sigma12psH_m2,
                               u1ph1_m1, u2ph1_m1, sigma11psh1_m1, sigma12psh1_m1,
                               u1ph1dh_m2, u2ph1dh_m2, sigma11psh1dh_m2, sigma12psh1dh_m2):

    #%% Resolution of the two 4x4 systems
    # Coupling matrix
    L_u = L[0:2,:,:,:]
    L_sigma = L[2::,:,:,:]
    #%% Stress: Interface 1 (x1 = h1) + Stress: Free surface (x1 = 0)
    # gamma_Ah_star (4, 4, len(omega), len(k2))
    gamma_Ah_star = np.zeros((4,4, len(omega[:, 0]), len(k2[0, :])),complex)
    gamma_Ah_star[0:2,:,:,:] = dot2444(L_sigma, psi_h1dh_m2_star)
    # gamma_Ap_star (4, 1, len(omega), len(k2))
    gamma_Ap_star = np.zeros((4,1, len(omega[:, 0]), len(k2[0, :])),complex)
    usigmaph1dh_m2 = np.array([[u1ph1dh_m2],[u2ph1dh_m2],[sigma11psh1dh_m2],[sigma12psh1dh_m2]])
    gamma_Ap_star[0:2,:,:,:] = dot2441(L_sigma, usigmaph1dh_m2) \
                                -np.array([[sigma11psh1_m1],[sigma12psh1_m1]])
    gamma_Ap_star[2::,:,:,:] = -np.array([[sigma11ps0_m1],[sigma12ps0_m1]])
    #%% Displacement: Interface 2 (x1=h1+dh) + Stress: Free surface (x1=H)
    # phi_A_star (4, 4, len(omega), len(k2))
    phi_Ah_star = np.zeros((4,4, len(omega[:, 0]), len(k2[0, :])),complex)
    mat1 = dot2444(psi_h1_m1_u_star,inv_gamma_psi_h_star)
    phi_Ah_star[0:2,:,:,:] = dot2444(mat1,gamma_Ah_star)-dot2444(L_u,psi_h1dh_m2_star)
    phi_Ah_star[2::,:,:,:] = psi_H_m2_star
    # phi_P_star (4, 1, len(omega), len(k2))
    phi_Ap_star = np.zeros((4,1, len(omega[:, 0]), len(k2[0, :])),complex)
    phi_Ap_star[0:2,:,:,:] = -dot2441(mat1,gamma_Ap_star)+dot2441(L_u,usigmaph1dh_m2)- \
                            np.array([[u1ph1_m1],[u2ph1_m1]])
    phi_Ap_star[2::,:,:,:] = -np.array([[sigma11psH_m2],[sigma12psH_m2]])
    #%% Solve ab_lm2_star, af_lm2_star, ab_tm2_star, af_tm2_star
    # Solve np.linalg.solve
    A1 = np.swapaxes(np.swapaxes(phi_Ah_star,0,2),1,3)
    B1 = np.swapaxes(np.swapaxes(phi_Ap_star,0,2),1,3)
    sol1 = np.linalg.solve(A1, B1)
    ab_lm2_star, af_lm2_star, ab_tm2_star, af_tm2_star = np.swapaxes(np.swapaxes(sol1,0,2),1,3)[:,0,:,:]
    #%% Solve xis_lm1_star, xis_tm1_star, xia_lm1_star, xia_tm1_star
    mat2 = dot4441(gamma_Ah_star,np.array([[ab_lm2_star], [af_lm2_star], \
                                           [ab_tm2_star], [af_tm2_star]]))+gamma_Ap_star
    # Solve np.linalg.solve
    A2 = np.swapaxes(np.swapaxes(gamma_psi_h_star,0,2),1,3)
    B2 = np.swapaxes(np.swapaxes(mat2,0,2),1,3)
    sol2 = np.linalg.solve(A2, B2)
    xis_lm1_star, xis_tm1_star, xia_lm1_star, xia_tm1_star = np.swapaxes(np.swapaxes(sol2,0,2),1,3)[:,0,:,:]

    return xis_lm1_star, xis_tm1_star, xia_lm1_star, \
            xia_tm1_star, ab_lm2_star, af_lm2_star, ab_tm2_star, af_tm2_star

def CS_m1(x1, h_1, k1_lm1, k1_tm1):

    cl_m1 = (np.exp(1j*k1_lm1*(x1-h_1))+np.exp(-1j*k1_lm1*x1))/2
    ct_m1 = (np.exp(1j*k1_tm1*(x1-h_1))+np.exp(-1j*k1_tm1*x1))/2
    sl_m1 = (np.exp(1j*k1_lm1*(x1-h_1))-np.exp(-1j*k1_lm1*x1))/(2*1j)
    st_m1 = (np.exp(1j*k1_tm1*(x1-h_1))-np.exp(-1j*k1_tm1*x1))/(2*1j)

    return cl_m1, ct_m1, sl_m1, st_m1

def E_m2(x1, h_1, H, dh, k1_lm2, k1_tm2):

    elH_m2 = np.exp(1j*k1_lm2*(x1-H))
    etH_m2 = np.exp(1j*k1_tm2*(x1-H))
    elh1_m2 = np.exp(-1j*k1_lm2*(x1-(h_1+dh)))
    eth1_m2 = np.exp(-1j*k1_tm2*(x1-(h_1+dh)))

    return elH_m2, etH_m2, elh1_m2, eth1_m2

def Simu_multilayer(L, time, x2, delta, x1, omega, k2, medium, dh, sh_m1, sh_m2, beta, t_gamma,
                    upf_beta_1, upb_beta_1, upf_gamma_m1, upb_gamma_m1,
                    upf_beta_2, upb_beta_2, upf_gamma_m2, upb_gamma_m2,
                    psi_h1_m1_u_star,gamma_psi_h_star,inv_gamma_psi_h_star,psi_h1dh_m2_star,psi_H_m2_star,
                    sigma11ps0_m1, sigma12ps0_m1, sigma11psH_m2, sigma12psH_m2,
                    u1ph1_m1, u2ph1_m1, sigma11psh1_m1, sigma12psh1_m1,
                    u1ph1dh_m2, u2ph1dh_m2, sigma11psh1dh_m2, sigma12psh1dh_m2,
                    theta_1, theta_2):

    #%% beta, t_gamma, Tp, Th
    beta_1, beta_2 = beta
    gamma_m1, gamma_m2 = t_gamma
    #%% Homogeneous solution medium 1
    u1h_lm1, u1h_tm1, u2h_lm1, u2h_tm1, k1_lm1, k1_tm1 = sh_m1
    #%% Homogeneous solution medium 2
    u1h_lm2, u1h_tm2, u2h_lm2, u2h_tm2, k1_lm2, k1_tm2 = sh_m2
    #%% Particular solution medium 1
    u1pf_beta_m1, u2pf_beta_m1 = upf_beta_1
    u1pb_beta_m1, u2pb_beta_m1 = upb_beta_1
    u1pf_gamma_m1, u2pf_gamma_m1 = upf_gamma_m1
    u1pb_gamma_m1, u2pb_gamma_m1 = upb_gamma_m1
    #%% Particular solution medium 2
    u1pf_beta_m2, u2pf_beta_m2 = upf_beta_2
    u1pb_beta_m2, u2pb_beta_m2 = upb_beta_2
    u1pf_gamma_m2, u2pf_gamma_m2 = upf_gamma_m2
    u1pb_gamma_m2, u2pb_gamma_m2 = upb_gamma_m2
    #%% Elastic coefficients, density and thickness of the medium
    c11_1, c12_1, c22_1, c66_1, rho_1, h_1 = medium[0]
    c11_2, c12_2, c22_1, c66_1, rho_2, h_2 = medium[1]
    c11_i1, c12_i1, c22_i1, c66_i1, rho_i1, h_i1 = medium[2]
    H = h_1+h_2+dh # Total thickness
    #%% Coefficients xis_lm1, xis_tm1, xia_lm1, xia_tm1, af_lm2_star,
    #   af_tm2_star, ab_lm2_star, ab_tm2_star
    xis_lm1_star, xis_tm1_star, xia_lm1_star, \
    xia_tm1_star, ab_lm2_star, af_lm2_star, ab_tm2_star, af_tm2_star = \
    Coefficients_xis_xia_af_ab(omega, k2, L,
                               psi_h1_m1_u_star, gamma_psi_h_star, inv_gamma_psi_h_star, psi_h1dh_m2_star, psi_H_m2_star,
                               sigma11ps0_m1, sigma12ps0_m1, sigma11psH_m2, sigma12psH_m2,
                               u1ph1_m1, u2ph1_m1, sigma11psh1_m1, sigma12psh1_m1,
                               u1ph1dh_m2, u2ph1dh_m2, sigma11psh1dh_m2, sigma12psh1dh_m2)
    #%% Complete solution uc1, uc2
    # Medium 1
    if x1<=h_1:
        cl_m1, ct_m1, sl_m1, st_m1 = CS_m1(x1, h_1, k1_lm1, k1_tm1)
        # Homogeneous solutions
        u1_h = u1h_lm1*(xis_lm1_star*cl_m1+xia_lm1_star*sl_m1)+ \
                u1h_tm1*(xis_tm1_star*ct_m1+xia_tm1_star*st_m1)
#        u2_h = 1j*(u2h_lm1*(xis_lm1_star*sl_m1-xia_lm1_star*cl_m1)+ \
#                u2h_tm1*(xis_tm1_star*st_m1-xia_tm1_star*ct_m1))
        # Particular solutions
        u1_p = u1pf_beta_m1*np.exp(-beta_1*x1)*np.exp(1j*k2*np.tan(theta_1)*x1)+\
                u1pb_beta_m1*np.exp(beta_1*(x1-h_1))*np.exp(-1j*k2*np.tan(theta_1)*x1)+ \
                u1pf_gamma_m1*np.exp(-gamma_m1*x1)+u1pb_gamma_m1*np.exp(gamma_m1*(x1-h_1))
#        u2_p = u2pf_beta_m1*np.exp(-beta_1*x1)*np.exp(1j*k2*np.tan(theta_1)*x1)+\
#                u2pb_beta_m1*np.exp(beta_1*(x1-h_1))*np.exp(-1j*k2*np.tan(theta_1)*x1)+ \
#                u2pf_gamma_m1*np.exp(-gamma_m1*x1)+u2pb_gamma_m1*np.exp(gamma_m1*(x1-h_1))
        # Complete solutions
        uc1 = u1_h+u1_p
#        uc2 = u2_h+u2_p
    # Sublayer(s)
    if x1>h_1 and x1<h_1+dh:
        uc1 = np.zeros((np.shape(u1h_lm1)), complex)
#        uc2 = np.zeros((np.shape(u1h_lm1)), complex)
    # Medium 2
    if x1>h_1+dh:
        elH_m2, etH_m2, elh1_m2, eth1_m2 = E_m2(x1, h_1, H, dh, k1_lm2, k1_tm2)
        # Homogeneous solutions
        u1_h = u1h_lm2*(af_lm2_star*elh1_m2+ab_lm2_star*elH_m2)+ \
                u1h_tm2*(af_tm2_star*eth1_m2+ab_tm2_star*etH_m2)
#        u2_h = u2h_lm2*(-af_lm2_star*elh1_m2+ab_lm2_star*elH_m2)+
#                u2h_tm2*(-af_tm2_star*eth1_m2+ab_tm2_star*etH_m2)
        # Particular solutions
        u1_p = u1pf_beta_m2*np.exp(-beta_2*(x1-(h_1+dh)))*np.exp(1j*k2*np.tan(theta_2)*x1)+\
                u1pb_beta_m2*np.exp(beta_2*(x1-H))*np.exp(-1j*k2*np.tan(theta_2)*x1)+ \
                u1pf_gamma_m2*np.exp(-gamma_m2*(x1-(h_1+dh)))+u1pb_gamma_m2*np.exp(gamma_m2*(x1-H))
#        u2_p = u2pf_beta_m2*np.exp(-beta_2*(x1-(h_1+dh)))*np.exp(1j*k2*np.tan(theta_2)*x1)+\
#                u2pb_beta_m2*np.exp(beta_2*(x1-H))*np.exp(-1j*k2*np.tan(theta_2)*x1)+ \
#                u2pf_gamma_m2*np.exp(-gamma_m2*(x1-(h_1+dh)))+u2pb_gamma_m2*np.exp(gamma_m2*(x1-H))
        # Complete solutions
        uc1 = u1_h+u1_p
#        uc2 = u2_h+u2_p
    delta_t = (np.exp(delta*time)*np.ones((len(x2),1))).T
    uc1_temporal = 1./(2*np.pi)*np.real(delta_t*np.fft.irfft(np.fft.fftshift(
                      np.fft.fft(uc1, axis=1, norm='ortho'),axes=1), axis=0, norm='ortho'))

    return uc1, uc1_temporal


"""
models/gravity.py
=================

PyQt model for gravity change table.
--------------------------------------------------------------------------------

NB: PyQt models follow the PyQt CamelCase naming convention. All other
methods/functions in GSadjust use PEP-8 lowercase_underscore convention.

This software is preliminary, provisional, and is subject to revision. It is
being provided to meet the need for timely best science. The software has not
received final approval by the U.S. Geological Survey (USGS). No warranty,
expressed or implied, is made by the USGS or the U.S. Government as to the
functionality of the software and related material nor shall the fact of release
constitute any such warranty. The software is provided on the condition that
neither the USGS nor the U.S. Government shall be held liable for any damages
resulting from the authorized or unauthorized use of the software.
"""
import numpy as np
from PyQt5 import QtCore
from PyQt5.QtCore import Qt

class GravityChangeModel(QtCore.QAbstractTableModel):
    """
    Model to store gravity change between surveys.

    There is only one such model per campaign. Gravity change is calculated when the
    respective menu item is chosen.
    """

    def __init__(self, header, table, table_type="simple", parent=None):
        QtCore.QAbstractTableModel.__init__(self, parent)
        self._headers = {n: col for n, col in enumerate(header)}
        self.createArrayData(table, table_type)
        self.table_type = table_type

    def createArrayData(self, table, table_type):
        if table_type == "simple" or table_type == "list":
            array = np.array(table).transpose()
        elif table_type == "full":
            array = np.array(table)
        self.arraydata = array

    def rowCount(self, parent=None):
        return self.arraydata.shape[0]

    def columnCount(self, parent=None):
        return len(self._headers)

    def flags(self, index):
        return Qt.ItemIsEnabled | Qt.ItemIsSelectable

    def data(self, index, role):
        if not index.isValid():
            return None
        if role == Qt.DisplayRole:
            row = index.row()
            column = index.column()
            try:
                value = float(self.arraydata[row][column])
                if self.table_type != 'full':
                    return format(value, "0.1f")
                elif column != 1 and column != 2:
                    return format(value, "0.1f")
                else:
                    return format(value, "0.5f")
            except ValueError:
                return str(self.arraydata[row][column])

    def headerData(self, section, orientation, role=Qt.DisplayRole):
        if role == Qt.DisplayRole and orientation == Qt.Horizontal:
            return self._headers.get(section, section + 1)


"""
Utility for updating flaunch without having to reinstall
or manage it separately
"""
import os
import sys
import shutil

class TempTree(object):
    """
    Context utility to make the update process as much of a "transaction"
    as possible. This way, in the event of an error while updating, we
    can try to restore normal use as much as possible
    """
    def __init__(self, folder, temp_dir_name, log_error=True):
        self._folder = folder
        self._temp_dir_name = temp_dir_name
        self._log = log_error
        self._additional = []
        self._revert = False

    def add_temp_item(self, f, t):
        self._additional.append((f, t))
        os.rename(f, t)

    def revert_on(self):
        """
        Ingore the request
        """
        self._revert = True

    def __enter__(self):
        os.rename(self._folder, self._temp_dir_name)
        return self

    def __exit__(self, type, value, traceback):
        import logging
        if (type is not None) or self._revert:
            if self._log and not self._revert:
                logging.error("Error while updating! Attempting to revert any changes!")
            if os.path.exists(self._folder):
                shutil.rmtree(self._folder)
            os.rename(self._temp_dir_name, self._folder)

            for file, temp_item in self._additional:
                if os.path.isfile(file):
                    os.unlink(file)
                elif os.path.isdir(file):
                    shutil.rmtree(file)
                os.rename(temp_item, file)

        else:
            shutil.rmtree(self._temp_dir_name)
            for file, temp_item in self._additional:
                if os.path.isfile(temp_item):
                    os.unlink(temp_item)
                elif os.path.isdir(temp_item):
                    shutil.rmtree(temp_item)
            if self._log:
                logging.debug('Completed update process!')

def update_flaunch():
    """
    Assert that we're using the latest version of flaunch available. If we're
    not - let's update our code right away
    :param flaunch_info: dict of information sent from atom about this package
    :return: None
    """
    # First, we actually copy the current src files into a second location
    # so we can use them in case an upgrade is required
    this_dir = os.path.dirname(os.path.abspath(__file__))

    if os.path.exists(os.path.join(this_dir, '.git')):
        print ("fupdate is desgined for deployments of flaunch only. Not a git repo")
        return 1

    #
    # We need the src and py directories to run our update, but they'll be
    # things that we update, so we create a mirror of them for temporary
    # use.
    #
    with TempTree(this_dir + '/src', this_dir + '/src_old') as tt:
        tt.add_temp_item(this_dir + '/py', this_dir + '/py_old')

        sys.path.append(this_dir + '/src_old')
        sys.path.append(this_dir + '/py_old/flaunch_packages.zip')

        from launch import pkgrep
        from common import communicate
        from common import log
        log.start(True, None)
        import logging

        flaunch_info = communicate.get_flaunch_info()
        version_file = os.path.join(this_dir, 'version.txt')

        current_version = ''
        with open(version_file, 'r') as f:
            f.seek(0)
            current_version = f.readline().strip()

        if current_version == flaunch_info['version']:
            logging.info('flaunch is up to date! Version: {}'.format(current_version))
            tt.revert_on()
            return 0

        # -- We have the the wrong version, time to upgrade
        launch_json = pkgrep._get_package('flaunch', info=flaunch_info)

        new_version_dir = launch_json.path
        for file_or_dir in os.listdir(new_version_dir):
            logging.debug("Updating: {}".format(file_or_dir))

            this_path = os.path.join(this_dir, file_or_dir)
            if os.path.exists(this_path):
                tt.add_temp_item(this_path, os.path.join(this_dir, file_or_dir + '_old'))

            new_file = os.path.join(new_version_dir, file_or_dir)
            if os.path.isdir(new_file):
                shutil.copytree(new_file, this_path)
            else:
                if file_or_dir.startswith('fupdate') or file_or_dir == 'selfupdate.py':
                    # The self update files are manually rewritten to attempt
                    # keeping clean between uses
                    with open(this_path, 'w') as writeme:
                        with open(new_file, 'r') as readme:
                            writeme.write(readme.read())
                else:
                    shutil.copy2(new_file, this_path)

        # Last but not least, let's update the version number
        with open(version_file, 'w') as f:
            f.write(launch_json.version_number)

    return 0

if __name__ == '__main__':
    sys.exit(update_flaunch())


import os
import subprocess
from App import *
from uuid import uuid4

class GenericWorkGenerator:

    def __init__(self, generator_path, image_path, height = 2, width = 2):
        self.generator_path = generator_path
        self.image_path = image_path
        self.height = height
        self.width = width

    def create_work(self, app):
        image_folder = uuid4().hex
        subprocess.call(["mkdir", image_folder], cwd = os.path.abspath(os.path.join(self.generator_path, '..')))

        # se obtiene el nombre del ejecutable
        head, tail = os.path.split(self.generator_path)
        executable_name = tail

        # se obtiene el nombre de la imagen
        head, tail = os.path.split(self.image_path)
        image_name = tail

        #se llama al ejecutable que divide trabajo desde su directorio
        subprocess.call(["./" + executable_name, image_name, image_folder, str(self.height), str(self.width) ], cwd = head)

        #se listan las imagenes que se guardan en ese directorio
        images = os.listdir(os.path.join(os.path.abspath(os.path.join(self.generator_path, '..')),image_folder))
        for image in images:
            image_location = os.path.abspath(os.path.join(head,image_folder,image))
            app.create_work_unit([image_location], image + uuid4().hex)
        #limpiar los resultados que ya estan stagged
        subprocess.call(["rm","-rf",image_folder], cwd = head)


# coding: utf-8

import uuid
import logging
import json
import constants

class Session(object):
    """"""
    def __init__(self, request_handler_obj):
        # 先判断用户是否已经有了session_id
        self.request_handler = request_handler_obj
        self.session_id = request_handler_obj.get_secure_cookie("session_id")

        # 如果不存在session_id,生成session_id
        if not self.session_id:
            self.session_id = uuid.uuid4().hex
            self.data = {}
            request_handler_obj.set_secure_cookie("session_id", self.session_id)

        # 如果存在session_id,去redis中取出data
        else:
            global json_data
            try:
                json_data = request_handler_obj.redis.get("sess_%s" % self.session_id)
            except Exception as e:
                logging.error(e)
                self.data = {}
            if not json_data:
                self.data = {}
            else:
                self.data = json.loads(json_data)

    def save(self):
        json_data = json.dumps(self.data)
        try:
            self.request_handler.redis.setex("sess_%s" % self.session_id, constants.SESSION_EXPIRES_SECONDS, json_data)
        except Exception as e:
            logging.error(e)
            raise Exception("save session failed")

    def clear(self):
        try:
            self.request_handler.redis.delete("sess_%s" % self.session_id)
        except Exception as e:
            logging.error(e)
        self.request_handler.clear_cookie("session_id")


import numpy as np
import matplotlib.pyplot as plt
import random

def fft(fname):
    matrix = np.genfromtxt(fname, dtype=int, delimiter=";", skip_header=1)
    timestep = 0.01
    n = len(matrix)
    t = matrix.T[0]
    x = matrix.T[1]
    y = matrix.T[2]
    spd = matrix.T[5]
    t = t / 1000
    x = x/4095 * 9.81
    y = y/4095 * 9.81
    #spd = (3.3/2047)*(spd-248)*(32.4/1.6)
    freqs = np.fft.fftfreq(n, d=timestep)
    xf = np.fft.fft(x)
    xf = 2.0 * np.abs(xf/n)
    xf[0] = 0
    yf = np.fft.fft(y)
    yf = 2.0 * np.abs(yf/n)
    yf[0] = 0
    posPeakX = np.argmax(xf)
    peakX = freqs[posPeakX]
    posPeakY = np.argmax(yf)
    peakY = freqs[posPeakY]
#   print(str(fname)+"\t AvgSpeed "+str(round(np.average(spd),3))+"\t FreqX: "+str(round(peakX,3))+"\t FreqY: "+str(round(peakY,3))    )
    return [freqs, xf, yf, t, x, y,spd,peakX,peakY]

def crossCor(fft1,fft2,n):
    X1scaler = sum(fft1[1])/n
    Y1scaler = sum(fft1[2])/n
    X2scaler = sum(fft2[1])/n
    Y2scaler = sum(fft2[2])/n
    fft1[1] = fft1[1]/X1scaler
    fft2[1] = fft2[1]/X2scaler
    fft1[2] = fft1[2]/Y1scaler
    fft2[2] = fft2[2]/Y2scaler
    Xcor = 0
    Ycor = 0
    for samp in range(4096):
        # Xcor += ((fft1[1])[samp] * (fft2[1])[samp])
        Ycor += ((fft1[2])[samp] * (fft2[2])[samp])
    speedDif = abs(np.average(fft1[6]) - np.average(fft2[6]))
    cor = Ycor
    return [cor,speedDif]

def fullCompare(s1,n):
    corPlot = []
    spdDifPlot = []
    st = []
    added = []
    for inner in range(502):
        setName = str(inner)+".csv"
        setName2 = str(s1)+".csv"
        set_one = fft(setName2)
        set_two = fft(setName)
        [c,s] =  crossCor(set_one,set_two,n)
        corPlot.append(c)
        spdDifPlot.append(s*50)
        st.append(inner)
        added.append(c + s*50)
    #plt.figure()
    #plt.title("Set"+str(s1))
    #plt.plot(st,corPlot,'b')
    #plt.plot(st,spdDifPlot,'g')
    #plt.plot(st,added,'r')

def train():
    xf = [0]
    yf = [0]
    for outer in range(503):
        setName = str(outer)+".csv"
        thisSet = fft(setName)
        xf += (thisSet[1]*(1/502))
        yf += (thisSet[2]*(1/502))
    #plt.figure()
    #plt.plot(thisSet[0],xf,'b')
    #plt.plot(thisSet[0],yf,'orange')
    return[xf,yf]

def trainedCompare(s1,n):
    xf,yf = train()
    setName = str(s1)+".csv"
    fft2 = fft(setName)
    X1scaler = sum(xf)/n
    Y1scaler = sum(yf)/n
    X2scaler = sum(fft2[1])/n
    Y2scaler = sum(fft2[2])/n
    xf = xf/X1scaler
    yf = yf/Y1scaler
    fft2[1] = fft2[1]/X2scaler
    fft2[2] = fft2[2]/Y2scaler
    Xcor = 0
    Ycor = 0
    for samp in range(4096):
        Xcor += (xf[samp] * (fft2[1])[samp])
        Ycor += (yf[samp] * (fft2[2])[samp])
    print(Xcor)
    print(Ycor)
    plt.figure("X-axis")
    plt.plot(fft2[0],fft2[1])
    plt.plot(fft2[0],xf,'k')

    plt.figure("Y-axis")
    plt.plot(fft2[0],fft2[2])
    plt.plot(fft2[0],yf,'k')

    return [Xcor, Ycor]

n =4096
for outer in range(5):
    sNum = random.randint(0,503)
    trainedCompare(sNum,n)
    print(sNum)
#print(sNum)
#test = fft(str(sNum)+".csv")
#plt.plot(test[0],test[1])


# Laborator 2, LearnNumpy
import os

import numpy as np
from skimage import io

# Ex1
# a) Read images
path = "images"
fnames = os.listdir(path)
print(fnames)

images = np.zeros((len(fnames), 400, 600))
print(images)

for index in range(len(fnames)):
    # image = np.load("images/car_" + str(index) + ".npy")
    # sau
    image = np.load(os.path.join(path, fnames[index]))
    images[index] = image

print("----- a) -----\n",images.shape)

# b) suma imaginilor

S = np.sum(images)
print("----- b) -----\n",S)

# c) suma pixeli fiecare imagine

s_list = np.sum(images, axis=(1, 2))
print("----- c) -----\n",s_list)

# d) index suma maxima
ind_max = np.argmax(np.sum(images, axis=(1,2)))
print("----- d) -----\n",ind_max)

# e) img medie
meanImg = np.mean(images, axis=0)
io.imshow(meanImg.astype(np.uint8))
io.show()

# f) deviatia standard
stdDev = np.std(images)
print("----- f) -----\n", stdDev)

# g) normalize: subtract: images.shape = (9, 400, 600), mean_img = (400, 600)

normImgs = np.subtract(images, meanImg) / stdDev
print(normImgs)
for norm in normImgs:
    io.imshow(norm.astype(np.uint8))
    io.show()

# h) decupare imagini

cutImgs = images[:, 200:300, 280:400]
for cut in cutImgs:
    io.imshow(cut.astype(np.uint8))
    io.show()


# uncompyle6 version 3.7.4
# Python bytecode 2.7 (62211)
# Decompiled from: Python 3.6.9 (default, Apr 18 2020, 01:56:04)
# [GCC 8.4.0]
# Embedded file name: /usr/local/lib/python2.7/dist-packages/BinPy/tests/tree_tests.py
# Compiled at: 2014-04-21 09:30:38
from __future__ import print_function
from BinPy.Gates.tree import *
from BinPy.Gates.gates import *
from BinPy.Gates.connector import *
from BinPy.Combinational.combinational import *
from nose.tools import with_setup, nottest

def getTreeForDepthTesting(depth):
    g1 = AND(True, False)
    g2 = AND(True, False)
    g3 = AND(g1, g2)
    g4 = AND(True, False)
    g5 = AND(True, False)
    g6 = AND(g4, g5)
    g_final = AND(g3, g6)
    tree_inst = Tree(g_final, depth)
    tree_inst.backtrack()
    n1 = (
     g1, [True, False])
    n2 = (g2, [True, False])
    n4 = (g4, [True, False])
    n5 = (g5, [True, False])
    n3 = (
     g3, [n1, n2])
    n6 = (g6, [n4, n5])
    tree_testing = (g_final, [n3, n6])
    return (
     tree_inst, tree_testing)

def compareTrees(tree_inst, tree_testing, depth):
    if isinstance(tree_testing, tuple):
        assert tree_testing[0] == tree_inst.element or False
    if depth == 0:
        if len(tree_inst.sons) != 0:
            if not False:
                raise AssertionError
        else:
            for i in range(len(tree_testing[1])):
                compareTrees(tree_inst.sons[i], tree_testing[1][i], depth - 1)

    elif not (tree_testing == tree_inst.element or False):
        raise AssertionError

def backtrack_depth_test():
    for i in range(6):
        tree_inst, tree_testing = getTreeForDepthTesting(i)
        compareTrees(tree_inst, tree_testing, i)

def set_depth_test():
    tree_inst, tree_testing = getTreeForDepthTesting(0)
    for i in range(1, 6):
        tree_inst.setDepth(i)
        tree_inst.backtrack()
        compareTrees(tree_inst, tree_testing, i)

def not_following_cycles_test():
    c1 = Connector(True)
    g1 = AND(c1, True)
    g2 = AND(g1, False)
    g2.setOutput(c1)
    t_no_cycle = Tree(g2, 5, False)
    t_cycle = Tree(g2, 5, True)
    t_no_cycle.backtrack()
    t_cycle.backtrack()
    assert t_no_cycle.sons[0].sons[0].sons[0].sons == []
    assert t_cycle.sons[0].sons[0].sons[0].sons[0].element == g1


from iso639 import languages

united_nations_style_languages = {'en', 'de', 'fr', 'es', 'pt', '中文', 'عربية'}

popular_languages_in_english = {
    'fijian', 'filipino', 'malagasy', 'samoan', 'tahitian', 'tongan',
    "abkhazian", "afar", "afrikaans", "akan", "albanian", "amharic","arabic", "aragonese", "armenian", "assamese","avaric","avestan","aymara","azerbaijani",
    "bambara","bashkir","basque","belarusian","bengali","bislama","bosnian","breton","bulgarian","burmese",
    "catalan","chamorro","chechen","chichewa","chinese","chuvash","cornish","corsican","cree","croatian","czech",
    "danish","divehi","dutch","dzongkha",
    "english","esperanto","estonian","ewe",
    "faroese","fijian","finnish","french","fulah",
    "galician","georgian","german","greek","guarani","gujarati",
    "haitian","hausa","hebrew","herero","hindi","hiri motu","hungarian",
    "indonesian","irish","igbo","inupiaq","ido","icelandic","italian","inuktitut",
    "japanese","javanese",
    "kalaallisut","greenlandic","kannada","kanuri","kashmiri","kazakh","central khmer","kikuyu","gikuyu","kinyarwanda","kirghiz","kyrgyz","komi","kongo","korean","kurdish","kuanyama",
    "latin","luxembourgish","ganda","limburgan","limburger","limburgish","lingala","lao","lithuanian","luba-katanga","latvian",
    "manx","macedonian","malagasy","malay","malayalam","maltese","maori","marathi","marshallese","mongolian",
    "nauru","navajo","navaho","north ndebele","nepali","ndonga","norwegian bokmål","norwegian nynorsk","norwegian",
    "sichuan yi","nuosu","south ndebele",
    "occitan","ojibwa","oromo","oriya","ossetian","ossetic",
    "punjabi","panjabi","pali","persian","polish","pashto","pushto","portuguese","quechua",
    "romansh","rundi","romanian","moldavian","moldovan","russian",
    "sanskrit","sardinian","sindhi","northern sami","samoan","sango","serbian","gaelic","scottish","shona","sinhala","sinhalese","slovak","slovenian","somali","southern sotho","spanish","castilian","sundanese","swahili","swati","swedish",
    "tamil","telugu","tajik","thai","tigrinya","tibetan","turkmen","tagalog","tswana","tonga","turkish","tsonga","tatar","twi","tahitian","uighur",
    "uyghur","ukrainian","urdu","uzbek",
    "venda","vietnamese","volapük","walloon","welsh","wolof","western frisian","xhosa",
    "yiddish","yoruba","zhuang","chuang","zulu",
}

popular_languages_in_russian = {
    'английский', 'арабский', 'африкаанс',
    'болгарский', 'боснийский (латиница)',
    'валлийский', 'венгерский', 'вьетнамский',
    'гаитянский креольский', 'голландский', 'греческий',
    'датский',
    'испанский', 'итальянский', 'иврит', 'индонезийский',
    'кантонский (традиционное письмо)',
    'каталанский', 'керетарский отоми', 'китайский традиционный',
    'китайский упрощенный', 'клингонский', 'корейский', 'керетарский отоми',
    'латышский', 'литовский',
    'малайский', 'мальтийский',
    'немецкий', 'норвежский',
    'персидский', 'польский', 'португальский',
    'румынский',
    'сербский (кириллица)', 'сербский (латиница)', 'словацкий', 'словенский', 'суахили',
    'тайский', 'турецкий',
    'украинский', 'урду',
    'финский', 'французский',
    'хинди', 'хмонг дау', 'хорватский',
    'чешский',
    'шведский',
    'эстонский', 'юкатекский майя',
    'японский'
}

popular_languages_in_native = {
"аҧсшәа",
"afaraf",
"afrikaans",
"akan",
"shqip",
"አማርኛ",
"العربية",
"aragonés",
"հայերեն",
"অসমীয়া",
"авар мацӏ",
"avesta",
"aymar aru",
"azərbaycan dili",
"bamanankan",
"башҡорт теле",
"euskara",
"беларуская мова",
"বাংলা",
"bislama",
"bosanski jezik",
"brezhoneg",
"български език",
"ဗမာစာ",
"català",
"chamoru",
"нохчийн мотт",
"chicheŵa",
"中文 ",
"чӑваш чӗлхи",
"kernewek",
"corsu",
"lingua corsa",
"ᓀᐦᐃᔭᐍᐏᐣ",
"hrvatski jezik",
"čeština",
"dansk",
"ދިވެހި",
"nederlands",
"རྫོང་ཁ",
"english",
"esperanto",
"eesti",
"eʋegbe",
"føroyskt",
"vosa vakaviti",
"suomi",
"français",
"fulfulde",
"galego",
"ქართული",
"deutsch",
"ελληνικά",
"avañe'ẽ",
"ગુજરાતી",
"kreyòl ayisyen",
"(hausa) هَوُسَ",
"עברית",
"otjiherero",
"हिन्दी",
"हिंदी",
"hiri motu",
"magyar",
"interlingua",
"bahasa indonesia",
"gaeilge",
"asụsụ igbo",
"iñupiaq",
"ido",
"íslenska",
"italiano",
"ᐃᓄᒃᑎᑐᑦ",
"日本語 ",
"ꦧꦱꦗꦮ",
"basa jawa",
"kalaallisut",
"kalaallit oqaasii",
"ಕನ್ನಡ",
"kanuri",
"कश्मीरी",
"شميري",
"қазақ тілі",
"ខ្មែរ",
"ខេមរភាសា",
"ភាសាខ្មែរ",
"gĩkũyũ",
"ikinyarwanda",
"кыргызча",
"коми кыв",
"kikongo",
"한국어",
" کوردی",
"kuanyama",
"latine",
"lëtzebuergesch",
"luganda",
"limburgs",
"lingála",
"ພາສາລາວ",
"lietuvių kalba",
"kiluba",
"latviešu valoda",
"gaelg",
"gailck",
"македонски јазик",
"fiteny malagasy",
"بهاس ملايو",
"മലയാളം",
"malti",
"te reo māori",
"मराठी",
"kajin m̧ajeļ",
"монгол хэл",
"dorerin naoero",
"diné bizaad",
"isindebele",
"नेपाली",
"owambo",
"norsk bokmål",
"norsk nynorsk",
"norsk",
"ꆈꌠ꒿ nuosuhxop",
"isindebele",
"occitan",
"ᐊᓂᔑᓈᐯᒧᐎᓐ",
"afaan oromoo",
"ଓଡ଼ିଆ",
"ирон ӕвзаг",
"ਪੰਜਾਬੀ",
"پنجابی",
"पालि",
"पाळि",
"فارسی",
"polski",
"پښتو",
"português",
"runa simi",
"kichwa",
"rumantsch grischun",
"ikirundi",
"română, moldovenească",
"русский",
"संस्कृतम्",
"𑌸𑌂𑌸𑍍𑌕𑍃𑌤𑌮𑍍",
"sardu",
"सिन्धी",
" سنڌي، سندھی",
"davvisámegiella",
"gagana fa'a samoa",
"yângâ tî sängö",
"српски језик",
"gàidhlig",
"chishona",
"සිංහල",
"slovenčina",
"slovenščina",
"soomaaliga",
"sesotho",
"español",
"basa sunda",
"kiswahili",
"siswati",
"svenska",
"தமிழ்",
"తెలుగు",
"тоҷикӣ",
" toçikī",
" تاجیکی",
"ไทย",
"ትግርኛ",
"བོད་ཡིག",
"türkmen",
"türk"
"түркмен",
"wikang tagalog",
"setswana",
"faka tonga",
"türkçe",
"xitsonga",
"татар теле",
"twi",
"reo tahiti",
"ئۇيغۇرچەuyghurche",
"українська",
"اردو",
"أۇزبېك",
"tshivenḓa",
"tiếng việt",
"volapük",
"walon",
"cymraeg",
"wollof",
"frysk",
"isixhosa",
"ייִדיש",
"yorùbá",
"saɯ cueŋƅ",
"isizulu",
"日本の",
"українська мова",
}

def is_human_language(l):
    if l is None:
        return False
    l = l.lower().strip()
    if l in united_nations_style_languages:
        return True
    if l in popular_languages_in_russian:
        return True
    if l in popular_languages_in_english:
        return True
    if l in popular_languages_in_native:
        return True
    if len(l) == 3:
        try:
            if languages.get(part3=l) is not None:
                return True
        except Exception as exp:
            pass
    return False


def partition(l,start,end):
    pivot=l[end]
    track=start-1
    for i in range(start,end):
        if l[i]<pivot:
           track=track+1
           l[i],l[track]=l[track],l[i]
    track=track+1
    l[track],l[end]=l[end],l[track]
    return track

def Qsort(l,start,end):
    if start<end:
       index=partition(l,start,end)
       Qsort(l,start,index-1)
       Qsort(l,index+1,end)

if __name__=="__main__":
   l=[12,14,11,10,7,9,3]
   Qsort(l,0,6)
   print(l)


from django.shortcuts import render
from django.views.generic import View
from django.http import HttpResponse

class Map(View):
    def get(self, request):
        return render(request, 'live_map/google_map.html')

class Danger(View):
    def get(self, request):
        return HttpResponse("return this string")
        return render(request, 'live_map/danger_area.html')


import FWCore.ParameterSet.Config as cms

process = cms.Process("ProcessFEDErrors")

# the source
process.source = cms.Source("PoolSource",
    fileNames = cms.untracked.vstring(
    '/store/data/CRAFT09/FEDMonitor/RAW/v1/000/112/597/EC0808A3-4D96-DE11-B2AC-001D09F2A49C.root'
    ),
    skipEvents=cms.untracked.uint32(0)    # events to skip before processing
)

process.maxEvents = cms.untracked.PSet( input = cms.untracked.int32(1) ) # events to be processed

process.load("DQMServices.Core.DQM_cfg")
process.DQM.collectorHost = 'localhost'
process.DQM.collectorPort = 9190

process.load("DQMServices.Components.DQMEnvironment_cfi")
process.dqmSaver.convention = "Online"
process.dqmSaver.dirName = "."
process.dqmSaver.producer = "DQM"
process.dqmSaver.saveByRun = 1
process.dqmSaver.saveAtJobEnd = True

process.dqmEnv.subSystemFolder = "DT"

process.load("Configuration.StandardSequences.Geometry_cff")
process.load("DQM.DTMonitorModule.dt_dqm_sourceclient_common_cff")
process.GlobalTag.globaltag = "GR09_31X_V2P::All"

process.dump = cms.EDAnalyzer("DumpFEDRawDataProduct",
                              feds = cms.untracked.vint32(770, 771, 772, 773, 774, 775, 776 ,777 ,778 ,779),
                              dumpPayload = cms.untracked.bool(True)
                              )

# message logger
process.MessageLogger = cms.Service("MessageLogger",
                                    debugModules = cms.untracked.vstring('*'),
                                    destinations = cms.untracked.vstring('cout'),
                                    categories = cms.untracked.vstring('DTDataIntegrityTask'),
                                    cout = cms.untracked.PSet(threshold = cms.untracked.string('WARNING'),
                                                              noLineBreaks = cms.untracked.bool(False),
                                                              DEBUG = cms.untracked.PSet(limit = cms.untracked.int32(0)),
                                                              INFO = cms.untracked.PSet(limit = cms.untracked.int32(0)),
                                                              DTDataIntegrityTask = cms.untracked.PSet(limit = cms.untracked.int32(-1))
                                                              )
                                    )

process.dqmmodules = cms.Sequence(process.dqmEnv + process.dqmSaver)
process.dtErrorStream = cms.Path(process.dtunpacker + process.dqmmodules + process.dump)


from .runner import Runner
from pathlib import Path

import numpy as np

class FileWriter(Runner):
    def __init__(self, path):
        self.path = Path(path).with_suffix('.caff')

    def run(self, generator):
        import aifc

        initialized = False
        g = generator.start()

        with aifc.open(str(self.path), 'wb') as out:
            while True:
                try:
                    sample = next(g)
                except StopIteration:
                    return

                if not initialized:
                    channels, frame_count = sample.shape
                    out.setframerate(generator.frame_rate)
                    out.setnchannels(channels)
                    out.setsampwidth(2)
                    initialized = True

                sample = (sample * (2 ** 15 - 1)).astype(dtype='>i2')
                out.writeframes(sample.tobytes())