import pandas as pd
import six
import trafaret as t

from datarobot import errors
from datarobot.models.api_object import APIObject
from datarobot.utils import encode_utf8_if_py2, get_id_from_response
from datarobot.utils.pagination import unpaginate

class ShapMatrix(APIObject):
    """
    Represents SHAP based prediction explanations and provides access to score values.

    Attributes
    ----------
    project_id : str
        id of the project the model belongs to
    shap_matrix_id : str
        id of the generated SHAP matrix
    model_id : str
        id of the model used to
    dataset_id : str
         id of the prediction dataset SHAP values were computed for

    Examples
    --------
    .. code-block:: python

        import datarobot as dr

        # request SHAP matrix calculation
        shap_matrix_job = dr.ShapMatrix.create(project_id, model_id, dataset_id)
        shap_matrix = shap_matrix_job.get_result_when_complete()

        # list available SHAP matrices
        shap_matrices = dr.ShapMatrix.list(project_id)
        shap_matrix = shap_matrices[0]

        # get SHAP matrix as dataframe
        shap_matrix_values = shap_matrix.get_as_dataframe()
    """

    _path = "projects/{}/shapMatrices/"
    _converter = t.Dict(
        {
            t.Key("id"): t.String(),
            t.Key("project_id"): t.String(),
            t.Key("model_id"): t.String(),
            t.Key("dataset_id"): t.String(),
        }
    ).allow_extra("*")

    def __init__(self, project_id, id, model_id=None, dataset_id=None):
        self.project_id = project_id
        self.model_id = model_id
        self.dataset_id = dataset_id
        self.id = id

    def __repr__(self):
        template = u"{}(id={!r}, project_id={!r}, model_id={!r}, dataset_id={!r})"
        return encode_utf8_if_py2(
            template.format(
                type(self).__name__, self.id, self.project_id, self.model_id, self.dataset_id,
            )
        )

    @classmethod
    def create(cls, project_id, model_id, dataset_id):
        """ Calculate SHAP based prediction explanations against previously uploaded dataset.

        Parameters
        ----------
        project_id : str
            id of the project the model belongs to
        model_id : str
            id of the model for which prediction explanations are requested
        dataset_id : str
            id of the prediction dataset for which prediction explanations are requested (as
            uploaded from Project.upload_dataset)

        Returns
        -------
        job : ShapMatrixJob
            The job computing the SHAP based prediction explanations

        Raises
        ------
        ClientError
            If the server responded with 4xx status. Possible reasons are project, model or dataset
            don't exist, user is not allowed or model doesn't support SHAP based prediction
            explanations
        ServerError
            If the server responded with 5xx status
        """
        data = {"model_id": model_id, "dataset_id": dataset_id}
        url = "projects/{}/shapMatrices/".format(project_id)
        response = cls._client.post(url, data=data)
        job_id = get_id_from_response(response)
        from .shap_matrix_job import ShapMatrixJob

        return ShapMatrixJob.get(
            project_id=project_id, job_id=job_id, model_id=model_id, dataset_id=dataset_id
        )

    @classmethod
    def from_location(cls, location, model_id=None, dataset_id=None):
        head, tail = location.split("/shapMatrices/", 1)
        project_id, id = head.split("/")[-1], tail.split("/")[0]
        return cls(project_id=project_id, id=id, model_id=model_id, dataset_id=dataset_id)

    @classmethod
    def list(cls, project_id):
        """
        Fetch all the computed SHAP prediction explanations for a project.

        Parameters
        ----------
        project_id : str
            id of the project

        Returns
        -------
        List of ShapMatrix
            A list of :py:class:`ShapMatrix <datarobot.models.ShapMatrix>` objects

        Raises
        ------
        datarobot.errors.ClientError
            if the server responded with 4xx status
        datarobot.errors.ServerError
            if the server responded with 5xx status
        """
        data = unpaginate(
            initial_url=cls._path.format(project_id), initial_params=None, client=cls._client
        )
        result = [cls.from_server_data(item) for item in data]
        return result

    @classmethod
    def get(cls, project_id, id):
        """
        Retrieve the specific SHAP matrix.

        Parameters
        ----------
        project_id : str
            id of the project the model belongs to
        id : str
            id of the SHAP matrix

        Returns
        -------
        :py:class:`ShapMatrix <datarobot.models.ShapMatrix>` object representing specified record
        """
        return cls(project_id=project_id, id=id)

    def get_as_dataframe(self):
        """
        Retrieve SHAP matrix values as dataframe.

        Returns
        -------
        dataframe : pandas.DataFrame
            A dataframe with SHAP scores

        Raises
        ------
        datarobot.dse.errors.ClientError
            if the server responded with 4xx status.
        datarobot.dse.errors.ServerError
            if the server responded with 5xx status.
        """
        path = self._path.format(self.project_id) + "{}/".format(self.id)
        resp = self._client.get(path, headers={"Accept": "text/csv"}, stream=True)
        if resp.status_code == 200:
            content = resp.content.decode("utf-8")
            return pd.read_csv(six.StringIO(content), index_col=0, encoding="utf-8")
        else:
            raise errors.ServerError(
                "Server returned unknown status code: {}".format(resp.status_code),
                resp.status_code,
            )


resposta = input (
  'Em qual turno você estuda? \n M - Matutino \n V - Vespertino \n N - Noturno \n\n')

lista = [ 'm', 'v', 'n' ]

rs = resposta.lower()
if rs in lista:
  if rs.__eq__('m'):
    print('Bom dia!')
  elif rs.__eq__('v'):
    print('Boa tarde!')
  elif rs.__eq__('n'):
    print('Boa noite!')
else:
  print('você deve digitar M, V ou N ')


#!/usr/local/lcls/package/python/current/bin/python

def create_station_dict(base, alim):
    station_dict = {
        'mode': base + ':DETUNE_MODE',
        'interval': base + ':REP_PERIOD',
        'amp_set': base + ':AOPEN',
        'amp_get': base + ':AACT',
        'ph_set': base + ':POPEN',
        'ph_get': base + ':PACT',
        'a_lim': alim,
        'detune': base + ':DFACT',
        'diff_nom': base + ':FREQ_OFFSET',
        'freq_offset': base + ':DELTA_FREQDES'
        }

    return station_dict

STATIONS = {
    'GUN': create_station_dict('GUN:GUNB:100', 35000),
    'BUNCHER': create_station_dict('ACCL:GUNB:455', 25000)
}


from flask import Flask, render_template, request, jsonify, redirect, url_for
from data import Articles
import pandas as pd
import numpy as np
import re
#from XGBoostModel import *
import RandomForest_webapp as rf
import HTS_analysis as hts_a
import process_data as proc
import roc
from sklearn.ensemble import RandomForestClassifier
from flask import send_from_directory

#!!! pip install Flask-PyMongo
from flask_pymongo import PyMongo
from pymongo import MongoClient
#import urllib.request, json
import os
from werkzeug.utils import secure_filename

UPLOAD_FOLDER = 'uploads/'
ALLOWED_EXTENSIONS = set(['txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif'])
ALLOWED_EXTENSIONS2 = set(['csv'])

app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
mongo = PyMongo(app)
def allowed_file(filename):
    return '.' in filename and \
           filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS2

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

@app.route('/register', methods=['POST'])
def register():
    return render_template('home.html')

@app.route('/fraggle', methods=['GET', 'POST'])
def upload_file():
    if request.method == 'POST':
        # check if the post request has the file part
        if 'file' not in request.files:
            flash('No file part')
            return redirect(request.url)
        file = request.files['file']
        # if user does not select file, browser also
        # submit a empty part without filename
        if file.filename == '':
            flash('No selected file')
            return redirect(request.url)
        if file and allowed_file(file.filename):
            filename = secure_filename(file.filename)
            file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
            return redirect(url_for('uploaded_file', filename=filename))
    #Return frggle.html if no file has yet been uploaded
    return render_template('fraggle.html')

@app.route('/uploads/<filename>',methods=['GET', 'POST'])
def uploaded_file(filename):
    if request.method == 'POST':
        # check if the post request has the file part
        if 'file' not in request.files:
            flash('No file part')
            return redirect(request.url)
        file = request.files['file']
        # if user does not select file, browser also
        # submit a empty part without filename
        if file.filename == '':
            flash('No selected file')
            return redirect(request.url)
        if file and allowed_file(file.filename):
            filename = secure_filename(file.filename)
            print(filename)
            file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
            return redirect(url_for('uploaded_HTS', filename = filename))
    elif request.method == 'GET':
        # make a pd.dataframe of training data
        df = pd.read_csv(os.path.join(app.config['UPLOAD_FOLDER'], filename))
        # use all features and yfill (no NaNs, filled with 0)
        features, yfill = proc.features_yfill(df)
        #train test split at 20%
        X_train, X_test, y_train, y_test = rf.train_test_split(features, yfill, test_size=0.20, random_state=1, stratify =yfill)

        #Optional: oversampling of minority class for training purposes
        #X_train_over, y_train_over = proc.oversample(X_train,y_train, r = 0.3)
        #rffit, y_predict = rf.randomforest(X_train_over, X_test, y_train_over, y_test, num_est=50, cls_w = 'balanced_subsample')

        #fit the Random Forest classifier: would like to add in a grid search
        rffit, y_predict = rf.randomforest(X_train.values, X_test, y_train.values, y_test)

        # Use below to run a grid search .... takes to long to work right now
        #rffit, y_predict = rf.randomforest(X_train.values, X_test, y_train.values, y_test, grid_search = 'small')

        #pickle the fit model for use with test data
        proc._pickle(rffit, 'RFC_fit.pkl')

        #set_threshold_recall is a function which determines the threshold to set such that recall is optimized (the median of the available thresholds that return the second best recall (not 1.0))
        precision_list, recall_list, median_recall_index, medianrecall_threshold = rf.set_threshold_recall(rffit, X_train, X_test, y_train, y_test)

        #print_threshold uses the trained model and the selected threshold (in this case recall optimized) to return listed statistics
        precision, recall, fpr, fpr_test, tpr_test, cm = rf.print_threshold(rffit, X_train, X_test, y_train, y_test, medianrecall_threshold)
        r_cm = pd.DataFrame(cm)
        proc._pickle(medianrecall_threshold, 'medianrecall_threshold.pkl')

        #make a pd.dataframe of the stats for display
        recall_opt_stats = pd.DataFrame([[format(medianrecall_threshold, '.2f'),format(recall, '.2f'), format(fpr, '.2f'), format(precision, '.2f'), ]], columns = ['Suggested Threshold','True Positive Rate (Recall)', 'False Positive Rate (Fall-out)','Precision'])

        # repeat the threshold selection process for precision optimization
        p_precision, p_recall, p_median_precision, threshold_precision = rf.set_threshold_precision(rffit, X_train, X_test, y_train, y_test)
        p_precision, p_recall, p_fpr, p_fpr_test, p_tpr_test, p_cm = rf.print_threshold(rffit, X_train, X_test, y_train, y_test, threshold_precision)
        p_cm = pd.DataFrame(p_cm)
        precision_opt_stats = pd.DataFrame([[format(threshold_precision, '.2f'),format(p_recall, '.2f'), format(p_fpr, '.2f'), format(p_precision, '.2f'), ]], columns = ['Suggested Threshold','True Positive Rate (Recall)', 'False Positive Rate (Fall-out)','Precision'])

        #produce a ROC plot
        test_prob = rffit.predict_proba(X_test)
        roc.plot_roc(X_train.values, y_train.values, y_test, test_prob, 'Test', RandomForestClassifier,  max_depth = 10, max_features= 30, min_samples_leaf= 2, min_samples_split = 2)
        feature_description = rf.plot_features(features, rffit, 'Identifier',n=10)
        #option for overfit data
        #roc.plot_roc(X_train_over, y_train_over, y_test, test_prob, 'Test', RandomForestClassifier,  max_depth = 10, max_features= 30, min_samples_leaf= 2, min_samples_split = 2)
        #roc.simple_roc(y_test, test_prob, 'ROC_RFC')
        pd.set_option('display.max_colwidth', -1)

        return render_template("rock.html", data_recall_opt=recall_opt_stats.to_html(index = False, classes = "data_recall_opt"),data_precision_opt=precision_opt_stats.to_html(index = False, classes = "data_precision_opt"), rocname = 'Test',f_descrip = feature_description.to_html(index = False, classes = "f_descrip"), recall_cm = r_cm.to_html(classes = "cm"), precision_cm = p_cm.to_html(classes = "cm"))

#f_descrip = feature_description.to_html(classes = "table table-condensed")
@app.route('/hit')
def uploaded_HTS(filename = 'MTH1_HTS_5500.csv'):
    # Read in HTS data and get features (no scores to model live app)
    df = pd.read_csv(os.path.join(app.config['UPLOAD_FOLDER'], filename))
    features, y, ids = proc.features_yfill_HTS(df)

    rffit = proc._unpickle('RFC_fit.pkl')
    mrecall_threshold = proc._unpickle('medianrecall_threshold.pkl')
    scored_hts = hts_a.score_HTS(rffit, features, ids)
    top50 = scored_hts.iloc[:50,:]

    #Below is for validation purposes only (will only be seen for MTH1 dataset where I have to scored data)
    y_proba = rffit.predict_proba(features)
    roc.plotHTS_roc(features, y, y_proba, 'Test_HTS')
    precision, recall, fpr, fpr_test, tpr_test, cm = hts_a.print_threshold(rffit, features, y, mrecall_threshold)
    cm = pd.DataFrame(cm)
    recall_opt_stats = pd.DataFrame([[format(mrecall_threshold, '.2f'),format(recall, '.2f'), format(fpr, '.2f'), format(precision, '.2f'), ]], columns = ['Suggested Threshold','True Positive Rate (Recall)', 'False Positive Rate (Fall-out)','Precision'])

    return render_template('hit.html', articles = articles, data = recall_opt_stats.to_html(index = False, classes = "table table-condensed"), hits = top50.to_html(index = False, classes = "table table-condensed"), hts_cm = cm.to_html(classes = "table table-condensed") )

    '''@app.route('/uploads/<filename>',methods=['GET', 'POST'])
    def uploaded_HTS(filename):
        return send_from_directory(app.config['UPLOAD_FOLDER'],
                                   filename)
    '''
    '''
    client = MongoClient()
    #set database that collection is in
    db = client.fragments
    #add json call to mongo
    db.fragments.insert_many(df)
    #db.review.update(data, data, upsert=True)
    '''

@app.route('/about')
def articles():
    return render_template('about.html', articles = articles)

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


def heapify(arr,n,i):
    largest=i
    l=i*2+1
    r=i*2+2

    if l< n and arr[i]<arr[l]:
        largest=l
    if r< n and arr[largest]<arr[r]:
        largest=r

    if largest!=i:
        arr[largest],arr[i]=arr[i],arr[largest]

        return heapify(arr,n,largest)

def heapSort(arr):
    n=len(arr)

# Build a maxheap.
# Since last parent will be at ((n//2)-1) we can start at that location.
    for i in range(n // 2 - 1, -1, -1):
        heapify(arr, n, i)

# One by one extract elements
    for i in range(n-1, 0, -1):
        arr[i], arr[0] = arr[0], arr[i] # swap
        heapify(arr, i, 0)
    return arr

arr=[1,4,7,6,5,32,23]
print(heapSort(arr))


# Matthieu Brucher

# Last Change : 2008-04-11 12:43

from numpy.ctypeslib import ndpointer, load_library
import numpy
import ctypes
import sys

# Load the library
#if sys.platform == 'win32':
  #_cost_function = load_library('cost_function', "\\".join(__file__.split("\\")[:-1]) + "\\release")
#else:
_cost_function = load_library('_cost_function', __file__)

_cost_function.allocate_cost_function.restype = ctypes.c_void_p
_cost_function.allocate_cost_function.argtypes = [ctypes.POINTER(ctypes.c_double), ctypes.c_ulong, ctypes.c_ulong, ctypes.c_ulong, ctypes.c_double, ctypes.c_double, ctypes.c_double]

_cost_function.call_cost_function.restype = ctypes.c_double
_cost_function.call_cost_function.argtypes = [ctypes.c_void_p, ctypes.POINTER(ctypes.c_double), ctypes.c_ulong]

ALLOCATOR = ctypes.CFUNCTYPE(ctypes.c_long, ctypes.c_int, ctypes.POINTER(ctypes.c_int))
_cost_function.gradient_cost_function.argtypes = [ctypes.c_void_p, ctypes.POINTER(ctypes.c_double), ctypes.c_ulong, ALLOCATOR]

class CostFunction:
  """
  Wrapper with ctypes around the cost function
  """
  def __init__(self, distances, nb_coords = 2, epsilon = 0.0000001, sigma = 1, tau = 60, **kwargs):
    """
    Creates the correct cost function with the good arguments
    """
    sortedDistances = distances.flatten()
    sortedDistances.sort()
    sortedDistances = sortedDistances[distances.shape[0]:]

    self._nb_coords = nb_coords
    self._epsilon = epsilon
    self._sigma = sigma

    sigma = (sortedDistances[sigma * len(sortedDistances) // 100])
    self._x1 = tau
    tau = (sortedDistances[tau * len(sortedDistances) // 100]) ** 2
    del sortedDistances
    self.grad = None
    self.distances = distances.copy()
    self._cf = _cost_function.allocate_cost_function(self.distances.ctypes.data_as(ctypes.POINTER(ctypes.c_double)), distances.shape[0], distances.shape[1], nb_coords, epsilon, sigma, tau)

  def __del__(self, close_func = _cost_function.delete_cost_function):
    """
    Deletes the cost function
    """
    if not (self._cf == 0):
      close_func(self._cf)
      self._cf = 0

  def __call__(self, parameters):
    """
    Computes the cost of a parameter
    """
    parameters = parameters.copy()
    return _cost_function.call_cost_function(self._cf, parameters.ctypes.data_as(ctypes.POINTER(ctypes.c_double)), len(parameters))

  def __getinitargs__(self):
    return(self.distances, self._nb_coords, self._epsilon, self._sigma, self._x1)

  def __getstate__(self):
    return ()

  def __setstate__(self, state):
    pass

  def gradient(self, parameters):
    """
    Computes the gradient of the function
    """
    self.grad = None
    parameters = parameters.copy()
    _cost_function.gradient_cost_function(self._cf, parameters.ctypes.data_as(ctypes.POINTER(ctypes.c_double)), len(parameters), ALLOCATOR(self.allocator))
    return self.grad

  def allocator(self, dim, shape):
    """
    Callback allocator
    """
    self.grad = numpy.zeros(shape[:dim], numpy.float64)
    ptr = self.grad.ctypes.data_as(ctypes.c_void_p).value
    return ptr


"""
Developer: Adam M. Terwilliger
Version: March 1, 2019
Purpose: CSE 842 -- HW2
Details: Natural Language Processing course
            at Michigan State University
         Implemention of document classifier
            using DAN and RNN
Dataset: IMDb movie sentiment database
            1108 training docs
"""

import sys
import numpy as np
import torch
import torch.utils.data

from time import time

np.set_printoptions(threshold=np.nan)
np.set_printoptions(suppress=True)

sys.dont_write_bytecode = True

import cse842_hw2_utils as util
from cse842_hw2_dataset import Dataset
from cse842_hw2_dan import dan
from cse842_hw2_rnn import rnn

def main(params):

    num_epochs = 100
    batch_size = 512
    num_layers = 4
    hidden_size = 64
    emb_size = 100
    learning_rate = 0.01
    optim_type = "adam"
    mode = "rnn"

    util.Logger.log("num_epochs = {}".format(num_epochs))
    util.Logger.log("batch_size = {}".format(batch_size))
    util.Logger.log("num_layers = {}".format(num_layers))
    util.Logger.log("hidden_size = {}".format(hidden_size))
    util.Logger.log("emb_size = {}".format(emb_size))
    util.Logger.log("learning_rate = {}".format(learning_rate))
    util.Logger.log("optim_type = {}".format(optim_type))
    util.Logger.log("mode = {}".format(mode))

    random_seed = 7
    util.init_torch(random_seed,random_seed)

    train_dataset = Dataset('train', mode, emb_size)
    val_dataset = Dataset('val', mode, emb_size)
    test_dataset = Dataset('test', mode, emb_size)

    train_data_loader = torch.utils.data.DataLoader(train_dataset, batch_size=batch_size, shuffle=True, collate_fn=Dataset.collate)
    val_data_loader = torch.utils.data.DataLoader(val_dataset, batch_size=16, shuffle=False, collate_fn=Dataset.collate)

    if mode == "dan":
        model = dan(num_layers, emb_size, hidden_size)
    elif mode == "rnn":
        model = rnn(num_layers, emb_size, hidden_size)

    model = torch.nn.DataParallel(model)

    loss_function = torch.nn.CrossEntropyLoss()
    if optim_type == "sgd":
        optimizer = torch.optim.SGD(model.parameters(), lr=learning_rate)
    else:
        optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)

    util.print_weights(model)

    curr_iter = 0
    curr_loss = 0
    loss_iter = len(train_dataset)

    total = 0
    correct = 0

    start_time = time()

    for epoch in range(num_epochs+1):

        for data, labels in train_data_loader:

            # change to training mode
            model.train()

            model.zero_grad()

            pred = model(data)

            loss = loss_function(pred, labels)

            loss.backward()

            optimizer.step()

            # compute accuracy across C=2
            _, cls_pred = torch.max(pred, 1)

            # detach gradient
            # bring back to cpu / numpy
            labels = labels.detach().cpu().numpy()
            cls_pred = cls_pred.detach().cpu().numpy()

            total += len(labels)
            correct += np.sum(labels==cls_pred)

            #util.Logger.log(correct+total+correct/total)

            curr_iter += 1

            # don't need to calc gradient
            curr_loss += loss.detach()

        if epoch % 1 == 0 and epoch > 0:
            time_str, _ = util.compute_eta(start_time, epoch, num_epochs)
            util.Logger.log("-----------------------------------------")
            util.Logger.log("Epoch: {}, ETA: {}".format(epoch, time_str))
            util.Logger.log("Training loss: {:.4f}".format(curr_loss/curr_iter))
            util.Logger.log("Training accuracy: {:.4f}".format(correct/total))
            curr_loss = 0
            curr_iter = 0
            correct = 0
            total = 0

        # testing phase
        if epoch % 1 == 0 and epoch > 0:

            # change to testing mode
            model.eval()

            val_correct = 0
            val_total = 0

            for val_data, val_labels in val_data_loader:

                pred = model(val_data)

                # compute accuracy across C=2
                _, cls_pred = torch.max(pred, 1)

                # detach gradient
                # bring back to cpu / numpy
                val_labels = val_labels.detach().cpu().numpy()
                cls_pred = cls_pred.detach().cpu().numpy()

                val_total += len(val_labels)
                val_correct += np.sum(val_labels==cls_pred)

            util.Logger.log("\nValidation accuracy: {:.4f}".format(val_correct/val_total))

    util.Logger.log("num_epochs = {}".format(num_epochs))
    util.Logger.log("batch_size = {}".format(batch_size))
    util.Logger.log("num_layers = {}".format(num_layers))
    util.Logger.log("hidden_size = {}".format(hidden_size))
    util.Logger.log("emb_size = {}".format(emb_size))
    util.Logger.log("learning_rate = {}".format(learning_rate))
    util.Logger.log("optim_type = {}".format(optim_type))
    util.Logger.log("mode = {}".format(mode))

if __name__ == "__main__":
    main(sys.argv)


import numpy as np
import root_numpy
import pandas as pd
import sys
from os import path
from sklearn.preprocessing import OneHotEncoder, MinMaxScaler
from sklearn.model_selection import train_test_split
from keras.models import Sequential, Model
from keras.layers import *
from keras.optimizers import SGD, Adam
from keras.activations import relu
# np.set_printoptions(threshold=sys.maxsize)
from keras import layers
from sklearn.utils import shuffle

def prepareTrainingSet( sample , model_vars, label_dict ):
	data = sample[ model_vars ]
#	print(data)
	data = shuffle(data)
	target = data['Muon_genPdgId']
	target = abs(target)
	data = data.drop(columns='Muon_genPdgId')

	pt = data['Muon_pt']
#	data = data.drop(columns='Muon_pt')

	target = target.map(label_dict)
	norm = MinMaxScaler()
	data = norm.fit_transform(data)

	x_train, x_test, y_train, y_test = train_test_split(data, target, test_size = .35, random_state=1)
	pt_train, pt_test, _, _ = train_test_split(pt,target, test_size= .35, random_state=1)

	# x_train = x_train.to_numpy()
	# x_test = x_test.to_numpy()
	y_train = y_train.to_numpy()
	y_test = y_test.to_numpy()

	y_train = np.array([np.array(i) for i in y_train])
	y_test = np.array([np.array(i) for i in y_test])
	pt_train = pt_test.to_numpy()
	pt_test = pt_test.to_numpy()

	return x_train, x_test, y_train, y_test, pt_train, pt_test

def reportSample( sample):
                for i in range(len(sample)):
                        print("Sample "+str(i)+" Report")
                        print( sample[i].shape )

def expandList( df, columnNames):
                outDf = pd.DataFrame()
                for col in columnNames:
                        arr = pd.Series(np.array([j for i in df[col] for j in i]))
                        outDf[col] = arr
                return outDf

def makeData(filename,definedIds,treeName):
	# treeName = 'Events'
	# gPath = '/home/t3-ku/mlazarov/softMVA/CMSSW_10_6_11_patch1/src/KUSoftMVA/MuonAnalysis/test/OutputFiles/'
	data = root_numpy.root2array(filename+'.root',treeName)
	data = pd.DataFrame(data)
	#make gen pdg ID labels for reco muons
	#-999 if unmatched
	pdgIds = np.array([-999 if mu == -1 else data['GenPart_pdgId'][i][mu] for i, idxs in enumerate(data['Muon_genPartIdx']) for j, mu in enumerate(idxs)])
	#get rid of 0 muon events
	data = data.drop([i for i, nMu in enumerate(data['nMuon']) if nMu == 0])
	#expand the list so each row is a muon (not an event)
	muonMask = data.columns.str.contains('Muon_.*')
	expCols = data.loc[:,muonMask].columns
	data = expandList(data, expCols)
	#add in gen pgdIds and jet btags of reco muons
	data['Muon_genPdgId'] = pdgIds
	#drop muons with pt < 2
	data = data.drop([i for i, pt in enumerate(data['Muon_pt']) if pt < 2])
	data = data.reset_index()
	#drop muons matched to anything not in our defined classes: 13, 221, 321, 2211, or 999
	data = data.drop([i for i, ID in enumerate(data['Muon_genPdgId']) if abs(ID) not in definedIds])
	data = data.reset_index()
	# df = df.drop([i for i, ID in enumerate(df['genPdgId']) if abs(ID) not in definedIds])
	data.to_csv(filename+'.csv')
	return data

class DATA:

	def __init__(self,fname,name):
		definedIds = np.array([13,211,321,2212,999])
		self.name = name
		self.treeName = 'Events'
		self.fname = fname
		print(self.fname)
		#speeds up processing time
		if path.exists(self.fname+'.csv'):
			self.data = pd.read_csv(self.fname+'.csv')
		else:
			self.data = makeData(self.fname,definedIds,self.treeName)
		# self.tmp = root_numpy.root2array(self.fname,self.treeName)
		# self.data = pd.DataFrame(self.tmp)

		# self.pdgIds = [-999 if mu == -1 else self.data['GenPart_pdgId'][i][mu] for i, idxs in enumerate(self.data['Muon_genPartIdx']) for j, mu in enumerate(idxs)]
		# self.pdgIds = np.array(self.pdgIds)
		# self.data = self.data.drop([i for i, nMu in enumerate(self.data['nMuon']) if nMu == 0])

		# self.muonMask = self.data.columns.str.contains('Muon_.*')
		# self.expCols = self.data.loc[:,self.muonMask].columns

		# self.data = expandList(self.data, self.expCols)

		# self.data['Muon_genPdgId'] = self.pdgIds

		self.data1 = self.data[abs(self.data.Muon_genPdgId) == 13]
		self.data2 = self.data[self.data.Muon_genPdgId == -999]
		self.data3 = self.data[abs(self.data.Muon_genPdgId) == 11]
		self.data4 = self.data[abs(self.data.Muon_genPdgId) == 211]
		self.data5 = self.data[abs(self.data.Muon_genPdgId) == 321]
		self.data6 = self.data[abs(self.data.Muon_genPdgId) == 2212]

		self.datacoll = {'mu': self.data1, 'U':self.data2, 'e':self.data3, 'pi':self.data4, 'k':self.data5,'p':self.data6}

	def report(self):
		print("Report for data "+self.name)
		print("mu", self.data1.shape[0])
		print("unmatched", self.data2.shape[0])
		print("elec", self.data3.shape[0])
		print("pion", self.data4.shape[0])
		print("kaon", self.data5.shape[0])
		print("prot", self.data6.shape[0])

		print('Relative Frequencies of Classes (total):')
		print(abs(self.data.Muon_genPdgId).value_counts(normalize=True))

	def sample(self, keys, nsamples):
		return [ self.datacoll[k][:s] for i, (k,s) in enumerate(zip(keys,nsamples)) ]

##


def front_times(str, n):
  if n < 0:
    return 'Try with a positive number'
  else:
    return str[:3]*n

# Purposed solution:

def front_times(str, n):
  front_len = 3
  if front_len > len(str): #Why is this important? I mean if the len of the string is 2, string[:6] == string
    front_len = len(str)
  front = str[:front_len]

  result = ""
  for i in range(n):
    result = result + front
  return result


inp = input("Please enter three name with comma seperated:- ").split(',')
inplower = [f.lower() for f in inp]

people = open("people.txt","r")
peoplefrnds = [p.strip().lower() for p in people.readlines()]
people.close()

peoplefrns_set = set(peoplefrnds)
inp_set = set(inplower)

nearbyfrnds_set = inp_set.intersection(peoplefrns_set)

print(nearbyfrnds_set)

nearbyfrndsfile = open("newbyfrnds.txt",'w')

for f in nearbyfrnds_set:
    nearbyfrndsfile.write(f'{f.capitalize()} is near by... \n')

nearbyfrndsfile.close()

val='a'
val.join()


import json

class Checkpoint:

    def __init__(self,value,coordinates):
        self.value = value
        self.coordinates = coordinates

    def __lt__(self,other):
        return self.value < other.value

class Map:

    DEFAULT_SETTINGS = '{"wall_char": "#", "passable_char": "."}'

    def __init__(self,walls=None,checkpoints=None,settings=None):
        if walls == None:
            self.walls = [[]]
        else:
            self.walls = walls

        if checkpoints == None:
            self.checkpoints = []
        else:
            self.checkpoints = checkpoints

        if settings == None:
            self.settings = json.loads(Map.DEFAULT_SETTINGS)
        else:
            self.settings = json.loads(settings)

    def __str__(self):
        map_str_arr = []

        for i in range(0,len(self.walls)):
            row_arr = [ self.settings['wall_char'] if x else self.settings['passable_char'] \
                    for x in self.walls[i] ]
            map_str_arr.append(row_arr)

        for checkpoint in self.checkpoints:
            i, j = checkpoint.coordinates
            map_str_arr[i][j] = str(checkpoint.value)

        for i, line in enumerate(map_str_arr):
            map_str_arr[i] = ''.join(x for x in line)

        return '\n'.join(x for x in map_str_arr)

    @staticmethod
    def load_map_from_file(file_name,settings=None):
        map_file = open(file_name,'r')

        # Settings is in JSON format
        # See DEFAULT_SETTINGS for required data
        if settings == None:
            settings_data = json.loads(Map.DEFAULT_SETTINGS)
        else:
            settings_data = json.loads(settings)

        cur_line = map_file.readline()

        cols = len(cur_line) - 1
        rows = 0

        # Rewind file
        map_file.seek(0)

        walls = []
        checkpoints = []

        # Get the unppassable parts of the maps and the checkpoints
        for i, line in enumerate(map_file):

            # Check number of columns
            if len(cur_line) - 1 != cols:
                raise MapLoadingError("Expected {} columns, got {}".format(cols,len(cur_line)), i)

            row_walls = [False] * cols

            for j in range(0,cols):
                row_walls[j] = line[j] == settings_data["wall_char"]

                if line[j].isdigit():
                    checkpoint = Checkpoint(int(line[j]),(i,j))
                    checkpoints.append(checkpoint)

            walls.append(row_walls)
            checkpoints = sorted(checkpoints)

        map_file.close()

        return Map(walls,checkpoints,settings)

class MapError(Exception):
    pass

class MapLoadingError(MapError):

    def __init__(self, message, line):
        self.message = message
        self.line = line


arr = [85, 46, 27, 81, 94, 9, 27, 38, 43, 99, 37, 63, 31, 42, 14]

for i in arr:
    if i % 3 == 0:
        print(i)

#Each number includes a tuple of two numbers. The first number is its "Scalar"
#This allows us to differentiate in our function between seventy two and seventy two thousand
#The second number is the increment, which will be simply added to the number later.
#All "incremental" numbers should have a scalar of 1.
#All "scalar" numbers should have an appropraite scalar and an increment of 0.
#Scalar words must have another word preceding them or they will not be scaled.

#Will error out with numbers that should have combined scalars but do not, like "three hundred fifty two thousand" gets parsed as 52300.
#Or "two hundred and thirty three million" will be parsed as 33,000,200
#but "five hundred thousand" will be correctly parsed as "500,000"
nums = {
    'and': 0,
    'zero': 0,
    'one': 1,
    'two': 2,
    'three': 3,
    'four': 4,
    'five': 5,
    'six': 6,
    'seven': 7,
    'eight': 8,
    'nine': 9,
    'ten': 10,
    'eleven': 11,
    'twelve': 12,
    'thirteen': 13,
    'fourteen': 14,
    'fifteen': 15,
    'sixteen': 16,
    'seventeen': 17,
    'eighteen': 18,
    'nineteen': 19,
    'twenty': 20,
    'thirty': 30,
    'forty': 40,
    'fifty': 50,
    'sixty': 60,
    'seventy': 70,
    'eighty': 80,
    'ninety': 90}

scalar_words = {
    'dozen': 12,
    'score': 20,
    'hundred': 100,
    'gross': 144,
    'thousand': 1000,
    'million': 1000000,
    'billion': 1000000000,
    'trillion': 1000000000000
}

print("\n\n\n-=-=-\n\n\n")

def parse_num_string(numstr):
    current = 0
    result = 0
    scalar = 1
    for index, word in enumerate(numstr.split()[::-1]):
        if word in scalar_words.keys():
            result += current
            current = 0
            scalar *= scalar_words[word]
        elif (word in nums.keys()) and (scalar > 1):
            try:
                if (numstr.split()[::-1])[index+1] in nums.keys():
                    current += nums[word]
                else: #Scalar or nothing
                    current = (nums[word]+current) * scalar
                    result += current
                    current = 0
                    scalar = 1
            except: #List out of range error above. I know this isn't ideal.
                current = (nums[word] + current) * scalar
                result += current
                current = 0
                scalar = 1
        elif word in nums.keys(): #Should only hit if scalar == 1
            current = current + nums[word]

    return result+current

number_words = [
  "five",
  "twenty six",
  "nine hundred ninety nine",
  "twelve",
  "eighteen",
  "one hundred one",
  "fifty two",
  "forty one",
  "seventy seven",
  "six",
  "twelve",
  "four",
  "sixteen"
]

for item in number_words:
    if parse_num_string(item) % 3 == 0:
        print(item)


#import libraries of python opencv
import cv2
import numpy as np
import datetime
import math

#globals
width = 0
height = 0
EntranceCounter = 0
ExitCounter = 0
LineOffsets = 150

#Check if an object is entering in monitored zone
def CheckEntranceLineCrossing(y, CoorYEntranceLine, CoorYExitLine):
  AbsDistance = abs(y - CoorYEntranceLine)

  if ((AbsDistance <= 2) and (y < CoorYExitLine)):
        return 1
  else:
        return 0

#Check if an object is exiting from monitored zone
def CheckExitLineCrossing(y, CoorYEntranceLine, CoorYExitLine):
    AbsDistance = abs(y - CoorYExitLine)

    if ((AbsDistance <= 2) and (y > CoorYEntranceLine)):
        return 1
    else:
        return 0

#create VideoCapture object and read from video file
cap = cv2.VideoCapture(0)
#use trained cars XML classifiers
car_cascade = cv2.CascadeClassifier('cars.xml')

#read until video is completed
while True:
    #capture frame by frame
    ret, frame = cap.read()

    height = int(np.size(frame,0))
    width = int(np.size(frame,1))

    #This same thing can be done by modifying the while statement
    if not ret:
        continue

    #convert video into gray scale of each frames
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    #Draw entrance and exit lines
    CoorYEntranceLine = int((height / 2)-LineOffsets)
    CoorYExitLine = int((height / 2)+LineOffsets)
    cv2.line(frame, (0,CoorYEntranceLine), (width,CoorYEntranceLine), (255, 0, 0), int(2), int(1))
    cv2.line(frame, (0,CoorYExitLine), (width,CoorYExitLine), (0, 0, 255), int(2), int(1))

    #detect cars in the video
    cars = car_cascade.detectMultiScale(gray, 1.1, 3)

    #to draw arectangle in each cars
    for (x,y,w,h) in cars:
        cv2.rectangle(frame,(x,y),(x+w,y+h),(0,255,0),2)

        #find object's centroid
        CoordXCentroid = int((x+x+w)/2)
        CoordYCentroid = int((y+y+h)/2)
        ObjectCentroid = (CoordXCentroid,CoordYCentroid)
        cv2.circle(frame, ObjectCentroid, 1, (0, 0, 0), 5)

        if (CheckEntranceLineCrossing(CoordYCentroid,CoorYEntranceLine,CoorYExitLine)):
            EntranceCounter += 1

        if (CheckExitLineCrossing(CoordYCentroid,CoorYEntranceLine,CoorYExitLine)):
            ExitCounter += 1

    #Write entrance and exit counter values on frame and shows it
    cv2.putText(frame, "Entrances: {}".format(str(EntranceCounter)), (10, 50), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (250, 0, 1), 2)
    cv2.putText(frame, "Exits: {}".format(str(ExitCounter)), (10, 70), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2)
    #display the resulting frame
    cv2.imshow('video', frame)
    #press Q on keyboard to exit
    if cv2.waitKey(25) & 0xFF == ord('q'):
        break
#release the videocapture object
cap.release()
#close all the frames
cv2.destroyAllWindows()


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Nov 23 16:48:42 2020

@author: caghangir
"""

import numpy as np
import matplotlib.pyplot as plt
import mne
from mne.datasets import eegbci
from mne.io import concatenate_raws, read_raw_edf
from mne.time_frequency import tfr_multitaper
from mne.stats import permutation_cluster_1samp_test as pcluster_test
from mne.viz.utils import center_cmap
from mne.preprocessing import ICA
from mne.channels import make_standard_montage
import extremeEEGSignalAnalyzer as chetto_EEG
chetto_EEG = chetto_EEG.extremeEEGSignalAnalyzer()
from mne.preprocessing import  (ICA, create_eog_epochs, create_ecg_epochs, corrmap)
from mne.stats import bootstrap_confidence_interval
from mne.baseline import rescale
from lspopt.lsp import spectrogram_lspopt
from mne.stats import permutation_cluster_1samp_test as pcluster_test
from matplotlib.colors import LinearSegmentedColormap
from mne.viz.utils import center_cmap

# %matplotlib qt #plot in new window
#%% =========== Mapping ===========
mapping = {
    'Fc5.': 'FC5', 'Fc3.': 'FC3', 'Fc1.': 'FC1', 'Fcz.': 'FCz', 'Fc2.': 'FC2',
    'Fc4.': 'FC4', 'Fc6.': 'FC6', 'C5..': 'C5', 'C3..': 'C3', 'C1..': 'C1',
    'Cz..': 'Cz', 'C2..': 'C2', 'C4..': 'C4', 'C6..': 'C6', 'Cp5.': 'CP5',
    'Cp3.': 'CP3', 'Cp1.': 'CP1', 'Cpz.': 'CPz', 'Cp2.': 'CP2', 'Cp4.': 'CP4',
    'Cp6.': 'CP6', 'Fp1.': 'Fp1', 'Fpz.': 'Fpz', 'Fp2.': 'Fp2', 'Af7.': 'AF7',
    'Af3.': 'AF3', 'Afz.': 'AFz', 'Af4.': 'AF4', 'Af8.': 'AF8', 'F7..': 'F7',
    'F5..': 'F5', 'F3..': 'F3', 'F1..': 'F1', 'Fz..': 'Fz', 'F2..': 'F2',
    'F4..': 'F4', 'F6..': 'F6', 'F8..': 'F8', 'Ft7.': 'FT7', 'Ft8.': 'FT8',
    'T7..': 'T7', 'T8..': 'T8', 'T9..': 'T9', 'T10.': 'T10', 'Tp7.': 'TP7',
    'Tp8.': 'TP8', 'P7..': 'P7', 'P5..': 'P5', 'P3..': 'P3', 'P1..': 'P1',
    'Pz..': 'Pz', 'P2..': 'P2', 'P4..': 'P4', 'P6..': 'P6', 'P8..': 'P8',
    'Po7.': 'PO7', 'Po3.': 'PO3', 'Poz.': 'POz', 'Po4.': 'PO4', 'Po8.': 'PO8',
    'O1..': 'O1', 'Oz..': 'Oz', 'O2..': 'O2', 'Iz..': 'Iz'
}

#%% ========== Load and Preprocess ======
subject = 1  # use data from subject 1
# runs_imagery = [4, 8, 12]  # motor imagery: left vs. right hand
runs_action = [3, 7, 11]  # motor action: left vs. right hand

fnames = eegbci.load_data(subject, runs_action)
raws = [read_raw_edf(f, preload=True) for f in fnames]
raw = concatenate_raws(raws)

raw.rename_channels(mapping)
raw.set_montage('standard_1005')

raw.rename_channels(lambda x: x.strip('.'))  # remove dots from channel names

events_actions, _ = mne.events_from_annotations(raw, event_id=dict(T1=2, T2=3))
events_rest, _ = mne.events_from_annotations(raw, event_id=dict(T0=1))
events_all, _ = mne.events_from_annotations(raw, event_id=dict(T0=1, T1=2, T2=3))

#%% ============== Preprocess =================
# raw.filter(l_freq=0.1)

ica = ICA(n_components=30, random_state=97)
ica.fit(raw)
eog_inds, eog_scores = ica.find_bads_eog(raw, ch_name='Fp1')
# ica.plot_sources(raw) #delete 1st one
ica.exclude = [0]

ica.apply(raw)

# selected_channels = ["C3", "Cz", "C4"]
# selected_channels = ["C5", "C3", "C1", "Cz", "C2", "C4", "C6"]
selected_channels = ["FC5", "FC3", "FC1", "FCz", "FC2", "FC4", "FC6", "C5", "C3", "C1", "Cz", "C2", "C4", "C6",\
                      "CP5", "CP3", "CP1", "CPz", "CP2", "CP4", "CP6"]
raw.pick_channels(selected_channels)

raw._data = raw._data[:,0:8000]
raw._times = raw._times[0:8000]

raw_data = raw.get_data()
#%% ======== Non-epoching MNE ========
event_id = {'Whole_data':0}
events =  np.zeros(shape=(1,3)).astype(int)

time_second = np.size(raw.get_data(),1) / raw.info['sfreq'] - 1 / raw.info['sfreq']

#Deletion
raw.del_proj()
raw.set_annotations(None)

epochs = mne.Epochs(raw, events, event_id, tmin=0., tmax=time_second, baseline=None, preload=True, reject=None, \
                    reject_by_annotation=None)

print(epochs.drop_log)
#%% ================== Permutation Cluster Preparation Among Non-averaged Channel ERDS Maps =====================
freqs = np.arange(2, 36, 0.1)  # frequencies from 2-35Hz
n_cycles = freqs  # use constant t/f resolution
vmin, vmax = -1, 1.5  # set min and max ERDS values in plot
baseline_period = 672 / raw.info['sfreq'] #second
kwargs = dict(n_permutations=100, step_down_p=0.05, seed=1, buffer_size=100, out_type='mask', n_jobs=8)  # for cluster test
cmap = center_cmap(plt.cm.Reds, vmin=vmin, vmax=vmax)  # zero maps to white

#%% ========== Focus to ERD ==========
#best combination
n_cycles = freqs * 2  # use constant t/f resolution
power = tfr_multitaper(epochs, freqs=freqs, n_cycles=n_cycles, use_fft=True, return_itc=False, time_bandwidth = 8.0, decim=2)
power_data = power._data
baseline_period = 672 / epochs.info['sfreq'] #second
power.apply_baseline([0, baseline_period], mode='percent')

#%%======= Focus to ERD ==========
power_data = power._data

# power_data = power_data[:,:,0:4000]
# power.times = power.times[0:4000]

power_data[power_data > 0] = 0
power_data[power_data < 0] *= -1
power._data = power_data
#%% ======== Permutation Clustering by Windowing Power Spectrogram for Memory Saving ===========
window_size = int(epochs.info['sfreq'] * 5)
window_amount = int(np.size(power_data,2) / window_size) #1 minute data

total_mask = np.zeros((np.size(power_data,1),np.size(power_data,2)), dtype=bool)
for i in range(window_amount):
    # positive clusters
    _, c1, p1, _ = pcluster_test(power_data[:,:,i*window_size:(i+1)*window_size], tail=1, **kwargs)
    # negative clusters
    # _, c2, p2, _ = pcluster_test(power_data[:,:,i*window_size:(i+1)*window_size], tail=-1, **kwargs)

    # c = np.stack(c1 + c2, axis=2)  # combined clusters
    c = np.stack(c1, axis=2)
    # p = np.concatenate((p1, p2))  # combined p-values

    mask = c[..., p1 <= 0.01].any(axis=-1)
    print(np.sum(mask))
    total_mask[:,i*window_size:(i+1)*window_size] = mask
#%%averaging
avg_power = np.mean(power._data, 0)
power._data = np.expand_dims(avg_power, axis=0) #expand dimension from axis=0 [1,x,y]
#%% =============== ERD Plotting ==============
freq_thesholds = np.array([4,8,12,30])

fig, ax = plt.subplots(1, 2, figsize=(12, 4), gridspec_kw={"width_ratios": [10, 1]})
power.plot([0], vmin=np.min(avg_power), vmax=np.max(avg_power), cmap=(cmap, False), axes=ax[0], colorbar=False, show=False)

ax[0].axvline(0, linewidth=1, color="black", linestyle=":")  # event
ax[0].vlines(x=events_actions[:,0]/160, ymin=0, ymax=36, colors='purple', ls='--', lw=3, label='Action Onsets')
ax[0].hlines(y=freq_thesholds, xmin=0, xmax=time_second, colors='black', ls='--', lw=3, label='Brain waves')
ax[0].legend(fontsize=15)
fig.colorbar(ax[0].images[-1], cax=ax[-1])

ax[0].text(-13,3, 'Delta', horizontalalignment='center', verticalalignment='center', fontsize=15, color='Black')
ax[0].text(-13,6, 'Theta', horizontalalignment='center', verticalalignment='center', fontsize=15, color='Black')
ax[0].text(-13,10, 'Alpha', horizontalalignment='center', verticalalignment='center', fontsize=15, color='Black')
ax[0].text(-13,24, 'Beta', horizontalalignment='center', verticalalignment='center', fontsize=15, color='Black')
ax[0].text(-13,33, 'Gamma', horizontalalignment='center', verticalalignment='center', fontsize=15, color='Black')

# ax[0].set_xlim(0, 100)
plt.title('ERD Color Spectrum', fontsize=13)
plt.suptitle("Average Multitaper Spectrogram of Central 21 Channels", fontsize=20)
fig.show()
#%% =============== ERD Plotting with Permutation Cluster ==============
freq_thesholds = np.array([4,8,12,30])

fig, ax = plt.subplots(1, 2, figsize=(12, 4), gridspec_kw={"width_ratios": [10, 1]})
power.plot([0], vmin=np.min(avg_power), vmax=np.max(avg_power), cmap=(cmap, False), axes=ax[0], colorbar=False, show=False, mask=total_mask, mask_style="mask")

ax[0].axvline(0, linewidth=1, color="black", linestyle=":")  # event
ax[0].vlines(x=events_actions[:,0]/160, ymin=0, ymax=36, colors='purple', ls='--', lw=3, label='Action Onsets')
ax[0].hlines(y=freq_thesholds, xmin=0, xmax=time_second, colors='black', ls='--', lw=3, label='Brain waves')
ax[0].legend(fontsize=15)
fig.colorbar(ax[0].images[-1], cax=ax[-1])

ax[0].text(-13,3, 'Delta', horizontalalignment='center', verticalalignment='center', fontsize=15, color='Black')
ax[0].text(-13,6, 'Theta', horizontalalignment='center', verticalalignment='center', fontsize=15, color='Black')
ax[0].text(-13,10, 'Alpha', horizontalalignment='center', verticalalignment='center', fontsize=15, color='Black')
ax[0].text(-13,24, 'Beta', horizontalalignment='center', verticalalignment='center', fontsize=15, color='Black')
ax[0].text(-13,33, 'Gamma', horizontalalignment='center', verticalalignment='center', fontsize=15, color='Black')

# ax[0].set_xlim(0, 100)
plt.title('ERD Color Spectrum', fontsize=13)
plt.suptitle("ERD Permutation Clustered Multitaper Spectrogram Among Central 21 Channels", fontsize=20)
fig.show()
#%% ========== ERD Relative Difference Envelope ===========
# erd_rel_diff = np.zeros(np.size(avg_power, axis=1))
# for i in range(len(erd_rel_diff)-1):
#     erd_rel_diff[i+1] = np.sum(avg_power[:,i+1]) - np.sum(avg_power[:,i])

# plt.figure()
# plt.plot(erd_rel_diff)
# plt.title('ERD Relative Difference From Neighbour')
#%% ========== GFP ==============
time_second = np.size(raw.get_data(),1) / raw.info['sfreq']

iter_freqs = [
            ('Theta', 4, 7),
            ('Alpha', 8, 12),
            ('Beta', 13, 30),
            ('Gamma', 30, 45)
            ]
frequency_map = list()

for band, fmin, fmax in iter_freqs:
    # (re)load the data to save memory
    raw_copy = raw.copy()
    raw_copy.load_data()

    # bandpass filter
    raw_copy.filter(fmin, fmax, n_jobs=1,  # use more jobs to speed up.
               l_trans_bandwidth=1,  # make sure filter params are the same
               h_trans_bandwidth=1)  # in each band and skip "auto" option.

    # get analytic signal (envelope)
    raw_copy.apply_hilbert(envelope=True)
    frequency_map.append(((band, fmin, fmax), raw_copy))

#%% ========= GFP with Sav-gol Filter Per Brainwave ==========
Fs = int(raw.info['sfreq'])
titles = ['theta', 'alpha', 'beta', 'gamma']
gfp_envelopes = list()
# plt.figure()
# ax = plt.axes()
for i in range(4):
    hilbert_raw = frequency_map[i][1]
    times = hilbert_raw.times * 1e3
    gfp = np.sum(hilbert_raw.get_data() ** 2, axis=0)
    gfp = mne.baseline.rescale(gfp, times, baseline=(0, baseline_period), mode="percent")
    gfp_envelope = chetto_EEG.envelopeCreator(gfp, degree=3, intervalLength=Fs+1)
    gfp_envelopes.append(gfp_envelope)

#     ax.plot(np.arange(0,8000)/Fs,gfp_envelope, label=titles[i])
# ax.legend(loc='upper right', prop={"size":20})

#%% ============== Plot GFP ============
# Plot one freq bin one
# titles = ['theta', 'alpha', 'beta', 'gamma']
# colors = plt.get_cmap('winter_r')(np.linspace(0, 1, 5))
# for ((freq_name, fmin, fmax), average), color, title in zip(
#     frequency_map, colors[0:4], titles):

#     times = average.times * 1e3
#     gfp = np.sum(average.get_data() ** 2, axis=0)
#     gfp = mne.baseline.rescale(gfp, times, baseline=(0, baseline_period), mode="percent")
#     # gfp = gfp / np.max(gfp)
#     gfp_envelope = chetto_EEG.envelopeCreator(gfp, degree=3, intervalLength=161)

#     plt.figure()
#     ax = plt.axes()
#     ax.plot(np.arange(0,8000)/Fs, gfp, label=freq_name, color=color, linewidth=2.5)
#     ax.plot(np.arange(0,8000)/Fs, gfp_envelope, color='cyan', linewidth=2)

#     ax.axhline(0, linestyle='--', color='grey', linewidth=2)

#     # ci_low, ci_up = bootstrap_confidence_interval(average.get_data(), random_state=0,
#     #                                               stat_fun=chetto_EEG.stat_fun)
#     # ci_low = rescale(ci_low, np.arange(0,len(times)), baseline=(None, 0))
#     # ci_up = rescale(ci_up, np.arange(0,len(times)), baseline=(None, 0))

#     # ax.fill_between(np.arange(0,len(times)), gfp + ci_up, gfp - ci_low, color=color, alpha=0.3)
#     ax.grid(False)
#     ax.set_ylabel('GFP', size=20)
#     ax.set_xlabel('Time [Sec]', size=20)
#     ax.set_title('Global Field Power of Event-related Dynamics (' + title + ')' , size=25)

#     ax.vlines(x=events_actions[0:6,0]/Fs, ymin=0, ymax=max(gfp), colors='purple', ls='--', lw=2, label='Action Onsets')
#%% ========== GFP Affection to ERD ==================
# plt.figure()
# plt.plot(gfp_envelopes[1])
from scipy import signal

gfp_new_envelopes = gfp_envelopes.copy()

for i in range(4):
    gfp_new_envelopes[i] = signal.resample(gfp_new_envelopes[i], 4000)
    minimum = np.min(gfp_new_envelopes[i])
    if(minimum < 0 ):
        gfp_new_envelopes[i] += np.abs(minimum)
        gfp_new_envelopes[i] += 2 #min will be 2

    gfp_new_envelopes[i] = np.log2(gfp_new_envelopes[i])

alphabeta_mean_gfp = (gfp_new_envelopes[1] + gfp_new_envelopes[2]) / 2

# plt.plot(gfp_new_envelopes[1])

# print(np.max(gfp_new_envelopes[1]))
# print(np.min(gfp_new_envelopes[1]))

#GFP Filtering
new_avg_power = avg_power.copy()
# for i in range(np.size(new_avg_power,1)):
# new_avg_power[60:100,:] = new_avg_power[60:100,:] / gfp_new_envelopes[1]
# new_avg_power[100:280,:] = new_avg_power[100:280,:] / gfp_new_envelopes[2]
# new_avg_power[280:,:] = new_avg_power[280:,:] / gfp_new_envelopes[3]
# new_avg_power[0:60,:] = new_avg_power[0:60,:] / gfp_new_envelopes[0]
new_avg_power = new_avg_power / alphabeta_mean_gfp

print(np.min(new_avg_power))
print(np.max(new_avg_power))

#New power object creation
gfpfiltered_power = power.copy()
gfpfiltered_power._data = np.expand_dims(new_avg_power, axis=0)

#Plot
fig, ax = plt.subplots(1, 2, figsize=(12, 4), gridspec_kw={"width_ratios": [10, 1]})
gfpfiltered_power.plot([0], vmin=np.min(new_avg_power), vmax=np.max(new_avg_power), cmap=(cmap, False), axes=ax[0], colorbar=False, \
                       show=False, mask=total_mask, mask_style="mask")
ax[0].axvline(0, linewidth=1, color="black", linestyle=":")  # event
ax[0].vlines(x=events_actions[:,0]/160, ymin=0, ymax=36, colors='purple', ls='--', lw=3, label='Action Onsets')
ax[0].hlines(y=freq_thesholds, xmin=0, xmax=time_second, colors='black', ls='--', lw=3, label='Brain waves')
ax[0].legend(fontsize=15)
fig.colorbar(ax[0].images[-1], cax=ax[-1])

ax[0].text(-13,3, 'Delta', horizontalalignment='center', verticalalignment='center', fontsize=15, color='Black')
ax[0].text(-13,6, 'Theta', horizontalalignment='center', verticalalignment='center', fontsize=15, color='Black')
ax[0].text(-13,10, 'Alpha', horizontalalignment='center', verticalalignment='center', fontsize=15, color='Black')
ax[0].text(-13,24, 'Beta', horizontalalignment='center', verticalalignment='center', fontsize=15, color='Black')
ax[0].text(-13,33, 'Gamma', horizontalalignment='center', verticalalignment='center', fontsize=15, color='Black')

# ax[0].set_xlim(0, 100)
plt.title('ERD Color Spectrum', fontsize=13)
plt.suptitle("Global Field Power Enhanced ERD Permutation Clustered Multitaper Spectrogram", fontsize=20)
#%% ============ Final v.01 Amplification of ERD Algorithms ==================

#sharpening
# filt_avgpower = ndimage.gaussian_filter(avg_power, 1)
# alpha = 30
# sharpened = avg_power + alpha * (avg_power - filt_avgpower)

#%%==== unsharp mask =======
from skimage.filters import unsharp_mask
unsharped = unsharp_mask(new_avg_power, radius=5, amount=2)

copy_power = power.copy()
copy_power._data = np.expand_dims(unsharped, axis=0)

# new_mask = unsharped > unsharped.mean() + 0.1
unsharped[unsharped < unsharped.mean() + 0.2] = 0

fig, ax = plt.subplots(1, 2, figsize=(12, 4), gridspec_kw={"width_ratios": [10, 1]})
copy_power.plot([0], vmin=np.min(unsharped), vmax=np.max(unsharped), cmap=(cmap, False), axes=ax[0], colorbar=False, show=False, \
                mask=total_mask, mask_style="mask")

ax[0].axvline(0, linewidth=1, color="black", linestyle=":")  # event
ax[0].vlines(x=events_actions[:,0]/160, ymin=0, ymax=36, colors='purple', ls='--', lw=3, label='Action Onsets')
ax[0].hlines(y=freq_thesholds, xmin=0, xmax=time_second, colors='black', ls='--', lw=3, label='Brain waves')
ax[0].legend(fontsize=15)
fig.colorbar(ax[0].images[-1], cax=ax[-1])

ax[0].text(-13,3, 'Delta', horizontalalignment='center', verticalalignment='center', fontsize=15, color='Black')
ax[0].text(-13,6, 'Theta', horizontalalignment='center', verticalalignment='center', fontsize=15, color='Black')
ax[0].text(-13,10, 'Alpha', horizontalalignment='center', verticalalignment='center', fontsize=15, color='Black')
ax[0].text(-13,24, 'Beta', horizontalalignment='center', verticalalignment='center', fontsize=15, color='Black')
ax[0].text(-13,33, 'Gamma', horizontalalignment='center', verticalalignment='center', fontsize=15, color='Black')

# ax[0].set_xlim(0, 100)
plt.title('ERD Color Spectrum', fontsize=13)
plt.suptitle("Unsharp Masked of Global Field Power Enhanced ERD Permutation Clustered Multitaper Spectrogram", fontsize=20)
#==== unsharp mask =======

#%%================= edge detection =================
from scipy import ndimage

sx = ndimage.sobel(unsharped, axis=0, mode='constant')
sy = ndimage.sobel(unsharped, axis=1, mode='constant')
sob = np.hypot(sx, sy)

copy_power = power.copy()
copy_power._data = np.expand_dims(sob, axis=0)

fig, ax = plt.subplots(1, 2, figsize=(12, 4), gridspec_kw={"width_ratios": [10, 1]})
copy_power.plot([0], vmin=np.min(sob), vmax=np.max(sob), cmap=(cmap, False), axes=ax[0], colorbar=False, show=False, mask=total_mask, mask_style="mask")

ax[0].axvline(0, linewidth=1, color="black", linestyle=":")  # event
ax[0].vlines(x=events_actions[:,0]/160, ymin=0, ymax=36, colors='purple', ls='--', lw=3, label='Action Onsets')
ax[0].hlines(y=freq_thesholds, xmin=0, xmax=time_second, colors='black', ls='--', lw=3, label='Brain waves')
ax[0].legend(fontsize=15)
fig.colorbar(ax[0].images[-1], cax=ax[-1])

ax[0].text(-13,3, 'Delta', horizontalalignment='center', verticalalignment='center', fontsize=15, color='Black')
ax[0].text(-13,6, 'Theta', horizontalalignment='center', verticalalignment='center', fontsize=15, color='Black')
ax[0].text(-13,10, 'Alpha', horizontalalignment='center', verticalalignment='center', fontsize=15, color='Black')
ax[0].text(-13,24, 'Beta', horizontalalignment='center', verticalalignment='center', fontsize=15, color='Black')
ax[0].text(-13,33, 'Gamma', horizontalalignment='center', verticalalignment='center', fontsize=15, color='Black')

# ax[0].set_xlim(0, 100)
plt.title('ERD Color Spectrum', fontsize=13)
plt.suptitle("Unsharp Mask + Sobel Filtered of Global Field Power Enhanced ERD Permutation Clustered Multitaper Spectrogram", fontsize=20)
#================= edge detection =================

#%%============= Contrasting ================
# from skimage import exposure
# contrasted = exposure.equalize_hist(sob)
# # new_mask = contrasted > contrasted.max() * 0.9

# copy_power = power.copy()
# copy_power._data = np.expand_dims(contrasted, axis=0)

# fig, ax = plt.subplots(1, 2, figsize=(12, 4), gridspec_kw={"width_ratios": [10, 1]})
# copy_power.plot([0], vmin=np.min(contrasted), vmax=np.max(contrasted), cmap=(cmap, False), axes=ax[0], colorbar=False, show=False, mask=total_mask, mask_style="mask")
# # ax[0].contour(sob, [0.02], linewidth=2, colors='r')

# ax[0].axvline(0, linewidth=1, color="black", linestyle=":")  # event
# ax[0].vlines(x=events_actions[:,0]/160, ymin=0, ymax=36, colors='purple', ls='--', lw=3, label='Action Onsets')
# ax[0].hlines(y=freq_thesholds, xmin=0, xmax=time_second, colors='black', ls='--', lw=3, label='Brain waves')
# ax[0].legend(fontsize=15)
# fig.colorbar(ax[0].images[-1], cax=ax[-1])
# #============= Contrasting ================

# #Local Equalize
# # from skimage.filters import rank
# # from skimage.morphology import disk
# # selem = disk(30)
# # sob2 = (sob/np.max(sob) - 0.5) * 2
# # img_eq = rank.equalize(sob2, selem=selem)
# # img_eq = img_eq / np.max(img_eq)
#%% ========= Nearest =========
# nearest = plt.imshow(unsharped, interpolation='nearest', cmap=cmap).get_array()

# copy_power = power.copy()
# copy_power._data = np.expand_dims(nearest, axis=0)

# # new_mask = unsharped > unsharped.max() * 0.9

# fig, ax = plt.subplots(1, 2, figsize=(12, 4), gridspec_kw={"width_ratios": [10, 1]})
# copy_power.plot([0], vmin=np.min(unsharped), vmax=np.max(unsharped), cmap=(cmap, False), axes=ax[0], colorbar=False, show=False, mask=total_mask, mask_style="mask")

# ax[0].axvline(0, linewidth=1, color="black", linestyle=":")  # event
# ax[0].vlines(x=events_actions[:,0]/160, ymin=0, ymax=36, colors='purple', ls='--', lw=3, label='Action Onsets')
# ax[0].hlines(y=freq_thesholds, xmin=0, xmax=time_second, colors='black', ls='--', lw=3, label='Brain waves')
# ax[0].legend(fontsize=15)
# fig.colorbar(ax[0].images[-1], cax=ax[-1])
# #==== unsharp mask =======

# plt.figure()
# plt.imshow(unsharped, interpolation='nearest', cmap=cmap).get_array()

#%% ============ Area 51 ===========

#============== Interpolation ================
# methods = [None, 'none', 'nearest', 'bilinear', 'bicubic', 'spline16',
#            'spline36', 'hanning', 'hamming', 'hermite', 'kaiser', 'quadric',
#            'catrom', 'gaussian', 'bessel', 'mitchell', 'sinc', 'lanczos']
# # Fixing random state for reproducibility
# np.random.seed(19680801)

# fig, axs = plt.subplots(nrows=3, ncols=6, figsize=(9, 6),
#                         subplot_kw={'xticks': [], 'yticks': []})

# for ax, interp_method in zip(axs.flat, methods):
#     ax.imshow(unsharped[:,1000:1500], interpolation=interp_method, cmap=cmap)
#     ax.set_title(str(interp_method))

# plt.tight_layout()
# plt.show()
#============== Interpolation ================

#%%============== K-means =================
# from sklearn.cluster import KMeans
# kmeans = KMeans(n_clusters=9, random_state=0).fit(unsharped)
# kmeans_pic = kmeans.cluster_centers_[kmeans.labels_]

# plt.figure()
# plt.imshow(kmeans_pic)

# copy_power = power.copy()
# copy_power._data = np.expand_dims(kmeans_pic, axis=0)

# fig, ax = plt.subplots(1, 2, figsize=(12, 4), gridspec_kw={"width_ratios": [10, 1]})
# copy_power.plot([0], vmin=np.min(contrasted), vmax=np.max(contrasted), cmap=(cmap, False), axes=ax[0], colorbar=False, show=False, mask=total_mask, mask_style="mask")
# # ax[0].contour(sob, [0.02], linewidth=2, colors='r')

# ax[0].axvline(0, linewidth=1, color="black", linestyle=":")  # event
# ax[0].vlines(x=events_actions[:,0]/160, ymin=0, ymax=36, colors='purple', ls='--', lw=3, label='Action Onsets')
# ax[0].hlines(y=freq_thesholds, xmin=0, xmax=time_second, colors='black', ls='--', lw=3, label='Brain waves')
# ax[0].legend(fontsize=15)
# fig.colorbar(ax[0].images[-1], cax=ax[-1])
#============== K-means =================
#%% ============== Deep Learning Segmentation ==========
# import pixellib
# from pixellib.semantic import semantic_segmentation
# from pixellib.instance import instance_segmentation
# import time
# import os
# from PIL import Image
# import gc

# os.chdir('/home/caghangir/Desktop/PhD/LD_BCI/Codes/Segmentation Deep Learning Model')

# #====== Array to Image ======
# # plt.figure()
# # plt.imshow(unsharped)
# # plt.savefig('unsharped.jpg', pad_inches=1, bbox_inches='tight', dpi=400)
# # print('Figure has saved successfully!')
# # plt.close()
# unsharped_3d = np.array(Image.fromarray(contrasted, mode="RGB").convert('L'))
# unsharped_3d = np.stack((unsharped_3d,)*3, axis=-1)
# # unsharped_3d = np.expand_dims(unsharped_3d, axis=2)

# # plt.figure()
# # plt.imshow(unsharped_3d)

# # unsharped_3d.shape
# # x=Image.open('unsharped.jpg')

# # image = np.array(Image.open('unsharped.jpg'))
# #====== Array to Image ======

# %% Semantic Segmentation Pascal
# segment_image = semantic_segmentation()
# # segment_image.load_pascalvoc_model("pascal.h5")
# segment_image.load_pascalvoc_model("deeplabv3_xception_tf_dim_ordering_tf_kernels.h5")

# start = time.time()
# segment_image.segmentAsPascalvoc(image_path=unsharped_3d, arrayinput=True, overlay=True, \
#                                  output_image_name="pascal_semantic_segmented_output_contrasted.jpg.jpg")
# end = time.time()
# print(f"Inference Time: {end-start:.2f}seconds")

# gc.collect()
# del segment_image
# %% Instance Segmentation Ade20k Model
# segment_image = semantic_segmentation()
# # segment_image.load_pascalvoc_model("pascal.h5")
# segment_image.load_ade20k_model("deeplabv3_xception65_ade20k.h5")

# start = time.time()
# segment_image.segmentAsAde20k(image_path=unsharped_3d, arrayinput=True, output_image_name="ade20k_instance_segmented_output_contrasted.jpg", overlay=True)
# end = time.time()
# print(f"Inference Time: {end-start:.2f}seconds")

# gc.collect()
# %% Instance Segmentation Mask RCNN
# segment_image = instance_segmentation()
# segment_image.load_model("mask_rcnn_coco.h5")

# start = time.time()
# segment_image.segmentImage(image_path=unsharped_3d, arrayinput=True, output_image_name = "maskrcnn_instance_segmented_output_contrasted.jpg")
# end = time.time()
# print(f"Inference Time: {end-start:.2f}seconds")

# gc.collect()
# del segment_image

# from streamlit import caching
# caching.clear_cache()
#%% ========== Morphologic Snakes ========
from skimage import data, img_as_float
from skimage.segmentation import (morphological_chan_vese,
                                  morphological_geodesic_active_contour,
                                  inverse_gaussian_gradient,
                                  checkerboard_level_set)
import numpy as np
import matplotlib.pyplot as plt

def store_evolution_in(lst):
    """Returns a callback function to store the evolution of the level sets in
    the given list.
    """

    def _store(x):
        lst.append(np.copy(x))

    return _store

# methods = [avg_power, new_avg_power, unsharped, sob, contrasted]
# method_names = ['avg_power', 'new_avg_power', 'unsharped', 'edge detected', 'contrasted']
# fig, axs = plt.subplots(nrows=2, ncols=3, figsize=(9, 6), subplot_kw={'xticks': [], 'yticks': []})
# for ax, method, method_name in zip(axs.flat, methods, method_names):
#     # Morphological GAC
#     image = img_as_float(method)
#     gimage = inverse_gaussian_gradient(image)

#     init_ls = np.zeros(image.shape, dtype=np.int8)
#     init_ls[10:-10, 10:-10] = 1

#     evolution = []
#     callback = store_evolution_in(evolution)
#     ls = morphological_geodesic_active_contour(gimage, 230, init_ls,
#                                                smoothing=1, balloon=-1,
#                                                threshold=0.69,
#                                                iter_callback=callback)
#     ax.imshow(image)
#     ax.contour(ls, colors='r')
    # ax.set_title(method_name, size=15)

#%% ======== Edge Detection Morphologic Snake =====
image = img_as_float(sob)
gimage = inverse_gaussian_gradient(sob)

init_ls = np.zeros(image.shape, dtype=np.int8)
init_ls[10:-10, 10:-10] = 1
evolution = []
callback = store_evolution_in(evolution)
ls = morphological_geodesic_active_contour(gimage, 330, init_ls,
                                            smoothing=1, balloon=-1,
                                            threshold=0.69,
                                            iter_callback=callback)

copy_power = power.copy()
copy_power._data = np.expand_dims(sob, axis=0)

fig, ax = plt.subplots(1, 2, figsize=(12, 4), gridspec_kw={"width_ratios": [10, 1]})
copy_power.plot([0], vmin=np.min(sob), vmax=np.max(sob), cmap=(cmap, False), axes=ax[0], colorbar=False, show=False, \
                mask=ls, mask_style="contour")
ax[0].axvline(0, linewidth=1, color="black", linestyle=":")  # event
ax[0].vlines(x=events_actions[:,0]/160, ymin=0, ymax=36, colors='purple', ls='--', lw=3, label='Action Onsets')
ax[0].hlines(y=freq_thesholds, xmin=0, xmax=time_second, colors='black', ls='--', lw=3, label='Brain waves')
ax[0].legend(fontsize=15)
fig.colorbar(ax[0].images[-1], cax=ax[-1])

ax[0].text(-13,3, 'Delta', horizontalalignment='center', verticalalignment='center', fontsize=15, color='Black')
ax[0].text(-13,6, 'Theta', horizontalalignment='center', verticalalignment='center', fontsize=15, color='Black')
ax[0].text(-13,10, 'Alpha', horizontalalignment='center', verticalalignment='center', fontsize=15, color='Black')
ax[0].text(-13,24, 'Beta', horizontalalignment='center', verticalalignment='center', fontsize=15, color='Black')
ax[0].text(-13,33, 'Gamma', horizontalalignment='center', verticalalignment='center', fontsize=15, color='Black')

# ax[0].set_xlim(0, 100)
plt.title('ERD Color Spectrum', fontsize=13)
plt.suptitle("Filter Iterated ERD Permutation Clustered Multitaper Spectrogram with Morphology Snake Contours", fontsize=20)
#================= edge detection =================

#%% ============ Morphology Snake Contour Ordering ===========
contour_indexes = np.where(ls==1)
ls_boolean = np.array(ls == 1, dtype='bool')

import cv2
ls_image = ls.copy()
ls_image = ls_image.astype(np.uint8)
ls_image[ls_image == 1] = 255

# ======= Find Contour Area Size ===========
idx = cv2.findContours(ls_image, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)[0]
areas = np.zeros(len(idx))
for i in range(len(idx)):
    areas[i] = cv2.contourArea(idx[i])
# ======= Find Contour Area Size ===========

# ===== 3D to 2D =======
# idx_new = list()
# for i in range(len(idx)):
#     idx_new.append(np.reshape(idx[i], (np.size(idx[i], 0), np.size(idx[i], 2))))
# ===== 3D to 2D =======

#=== Sort Cluster Area Indexes =====
sorted_area_idx = np.argsort(areas * -1)
idx_sorted = list()
for i in range(len(idx)):
    idx_sorted.append(idx[sorted_area_idx[i]])
#=== Sort Cluster Area Indexes =====

#%% ========= Select only Big Contours ===========
new_mask = np.zeros_like(sob)
contours = idx_sorted[0:10]

#==== Corners of contours =====
left_right_corners_ofcontours = np.zeros((10,2))
for i in range(10):
    left_right_corners_ofcontours[i,0], left_right_corners_ofcontours[i,1] = np.min(contours[i][:,:,0]), np.max(contours[i][:,:,0])
#==== Corners of contours =====

cv2.drawContours(new_mask, contours, -1, (1),1)

# ========== Plot Power Data ==============
copy_power = power.copy()
copy_power._data = np.expand_dims(sob, axis=0)

fig, ax = plt.subplots(1, 2, figsize=(12, 4), gridspec_kw={"width_ratios": [10, 1]})
copy_power.plot([0], vmin=np.min(sob), vmax=np.max(sob), cmap=(cmap, False), axes=ax[0], colorbar=False, show=False, \
                mask=new_mask, mask_style="contour")
ax[0].axvline(0, linewidth=1, color="black", linestyle=":")  # event
ax[0].vlines(x=events_actions[:,0]/160, ymin=0, ymax=36, colors='purple', ls='--', lw=3, label='Action Onsets')
ax[0].vlines(x=left_right_corners_ofcontours[:,0]*2/160, ymin=0, ymax=36, colors='blue', ls=':', lw=4, label='Contour Points')
ax[0].hlines(y=freq_thesholds, xmin=0, xmax=time_second, colors='black', ls='--', lw=3, label='Brain waves')
ax[0].legend(fontsize=15)
fig.colorbar(ax[0].images[-1], cax=ax[-1])

ax[0].text(-13,3, 'Delta', horizontalalignment='center', verticalalignment='center', fontsize=15, color='Black')
ax[0].text(-13,6, 'Theta', horizontalalignment='center', verticalalignment='center', fontsize=15, color='Black')
ax[0].text(-13,10, 'Alpha', horizontalalignment='center', verticalalignment='center', fontsize=15, color='Black')
ax[0].text(-13,24, 'Beta', horizontalalignment='center', verticalalignment='center', fontsize=15, color='Black')
ax[0].text(-13,33, 'Gamma', horizontalalignment='center', verticalalignment='center', fontsize=15, color='Black')

# ax[0].set_xlim(0, 100)
plt.title('ERD Color Spectrum', fontsize=13)
plt.suptitle("Filter Iterated ERD Permutation Clustered Multitaper Spectrogram with Largest Morphology Snake Contours", fontsize=20)
# ========== Plot Power Data ==============

#%% =============== Spectrogram to 1-D Line ================
freq_low_threshold_index = np.where(freqs > 8)[0][0]
freq_high_threshold_index = np.where(freqs <= 30)[0][-1]
sob_interval = np.sum(sob[freq_low_threshold_index:freq_high_threshold_index], axis=0)

plt.figure()
x_axis = np.linspace(0,len(sob_interval)*2/Fs, len(sob_interval))
plt.plot(x_axis, sob_interval)
all_vlines = events_actions[:,0]/160
plt.vlines(x=all_vlines[0:6], ymin=0, ymax=max(sob_interval), colors='purple', ls='--', lw=3, label='Action Onsets')

plt.xlabel('Time (s)')
plt.ylabel('Sum Power')


#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Date    : 2019-08-07 18:24:01
# @Author  : Yaohui Zhu (you@example.org)
# @Link    :
# @Version : $Id$

import os,re,configparser
import xlrd,time
from path import myPath
import tool

#################################################################
###############此部分应用于将Tracker Excel文件读取至页面及处理######
#################################################################

class dealExcel():
    def __init__(self,excel_path,dic):
        self.excel_path=excel_path

        # to deal with excel_path to protocol path
        excel_path_2 = myPath(init_path=self.excel_path)
        self.path_init = excel_path_2.getPathOfWin(type='init')
        print(self.path_init)
        self.compTime()

        s_adam         = self.path_init+'/dev/crts/adam'
        s_log          = self.path_init+'/dev/pgm/tlfs'
        v_log          = self.path_init+'/val/pgm/tlfs'
        self.adam_time         = self.getTime(filePath = s_adam,  type='sas7bdat')
        self.log_inf_source = self.getTime(filePath = s_log,   type='log')
        self.log_inf_val    = self.getTime(filePath = v_log,   type='log')
        print(len(self.adam_time),len(self.log_inf_source),len(self.log_inf_val))
        # to get all  pdf and rtf files from folder
        # self.rtfFiles = getFileName(self.path_init,'rtf')
        # self.pdfFiles = getFileName(self.path_init,'pdf')
        # cfg         = tool.getCfg()#获得配置信息

        # dic         = cfg.cfg()
        # dic  = tool.cfg()
        dic_cols    = dic
        print(dic_cols)
        self.book   = xlrd.open_workbook(self.excel_path)
        self.table  = self.book.sheet_by_name("TFL")
        self.nrows  = self.table.nrows
        self.ncols  = self.table.ncols
        # self._type                = 1 -1
        # self.analysis             = 2 -1
        self.tfl_category         = int(dic_cols['tfl_category'])
        # self.population           = 4 -1
        self.titles               = int(dic_cols['titles'])
        self.output_number        = int(dic_cols['output_number'])
        self.base_output_name     = int(dic_cols['base_output_name'])
        self.source_program_name  = int(dic_cols['source_program_name'])
        self.source_data          = int(dic_cols['source_data'])
        self.source_programmer    = int(dic_cols['source_programmer'])
        self.qc_programmer        = int(dic_cols['qc_programmer'])
        self.qc_program_name      = int(dic_cols['qc_program_name'])

    #9代表ssource program 所在的列
    #18是总的列数
    def getNrows(self):
        return self.nrows-1

    def excelDisplay(self ):

        # self.nrows=100

        all_the_final=[]

        for row in range(1,self.nrows):

            new_row_data=[]

            row_data = self.table.row_values(row, start_colx=0, end_colx=None)

            # _type               =       row_data[self._type]
            # analysis            =       row_data[self.analysis ]
            tfl_category        =       row_data[self.tfl_category ]
            # population          =       row_data[self.population]
            titles              =       row_data[self.titles]
            output_number       =       str(row_data[self.output_number])
            base_output_name    =       row_data[self.base_output_name]
            source_program_name =       row_data[self.source_program_name]
            source_data         =       row_data[self.source_data]
            source_programmer   =       row_data[self.source_programmer]
            qc_programmer       =       row_data[self.qc_programmer]
            qc_program_name       =     row_data[self.qc_program_name ]

            # temp_row=[row_data[row] for row in [0,2]]+row_data[7:14]
            #######################################################################
            ######################参照m_run_trk.sas 处理 fileout RTF_out############
            #######################################################################

            if output_number.strip() != '':

                _output_number =  output_number.strip().replace(".", "-")

            if tfl_category.strip() != '':

                in_num=tfl_category.index(':')

                _tfl_category = tfl_category[0:in_num]

                # print(_tfl_category)

            if base_output_name.strip() != '':

                typef           =   base_output_name.replace(" ",'').lower()[0]

                _base_outname   =   base_output_name.lower().replace("_", "-").replace(' ','').replace('.rtf','')

                _base_outname   =   _base_outname.replace(".pdf",'')[2:]

            if self.excel_path.find('/iss/') > -1 and self.excel_path.find('/ise/') > -1:
                fileout=''
            else:
                if typef.strip() == "l"  and tfl_category.find('DO NOT') == -1:
                    fileout='16.2'
                elif typef.strip() in ['t','f']:
                    fileout='14'
                else:fileout=''

            if typef.strip() in ['t','l','f']  and _tfl_category.strip() != ''   and _output_number.strip != '' and \
            base_output_name.strip() != '':
                RTF_Out = typef +'-'+ fileout.replace(".", "-") +'-'+ _tfl_category +'-'+ _output_number +'-'+_base_outname
                RTF_Out = RTF_Out.lower()

            else:
                RTF_Out = '';

            if typef in ['t','l','f'] and _tfl_category.strip() != '' and output_number !='':
                if self.excel_path.find('/iss/') > -1 :
                    if typef =='l' :
                        fileout = "Listing " + _tfl_category.replace("-", ".").replace(' ','') + '.'+output_number.strip()
                    elif typef =='t':
                        fileout = "Table " + _tfl_category.replace("-", ".").replace(' ','') + '.'+output_number.strip()
                    elif typef== 'f':
                        fileout = "Figure " + _tfl_category.replace("-", ".").replace(' ','') + '.'+output_number.strip()

                else:
                    if typef == 'l' :
                        fileout="Listing " + fileout.strip() + '.' + _tfl_category.replace("-", ".").replace(' ','') + '.'+output_number.strip()
                    elif typef =='t':
                        fileout = "Table " + fileout.strip() + '.' + _tfl_category.replace("-", ".").replace(' ','') + '.'+output_number.strip()
                    elif typef== 'f':
                        fileout = "Figure " + fileout.strip() + '.' + _tfl_category.replace("-", ".").replace(' ','') + '.'+output_number.strip()

            #############################################end####################################
            new_row_data=[fileout,base_output_name,RTF_Out,source_program_name,source_data,source_programmer,qc_programmer,qc_program_name]
            # print(new_row_data)

            ####################################################################################
            #############################处理source data 及ADAM的日期############################
            ####################################################################################

            list_temp=self.deal_log(row_data, self.adam_time, self.log_inf_source, self.log_inf_val)

            # print(list_temp)

            new_row_data += list_temp

            new_row_data = self.get_rtf_pdf(new_row_data)

            new_row_data.insert(0,"<input id=\"%s\"  type='checkbox' name=\"check_id\" value=\"\" >"%row)
            # new_row_data += ['']

            all_the_final.append(new_row_data)

        # print(all_the_final)
        return all_the_final

    ####################################################################################
    #############################处理source data 及ADAM的日期############################
    ####################################################################################
    def deal_adam(self,adam_time,row_data):

        # source_data = row_data[self.source_data].upper()
        # source_list = re.findall('AD[A-Z]{2,6}',source_data)
        # time_list=[]
        # for pair in adam_time:
        #     for adam in source_list:
        #         if pair[0].strip().lower() == adam.lower():
        #             time_of_adam=time.mktime(time.strptime(pair[1],'%Y-%m-%d %H:%M'))
        #             time_list.append(time_of_adam)
        # if len(time_list)>1:
        #     temp_2 = max(time_list)
        # else:
        #     return ' '
        # timeArray = time.localtime(temp_2)
        # finalTime = time.strftime("'%Y-%m-%d %H:%M'", timeArray)
        # print('this is finalTime of adam')
        # print(finalTime)
        source_data = row_data[self.source_data].upper()
        source_list = re.findall('AD[A-Z]{2,6}',source_data)
        time_list=[]
        for pair in adam_time:
            for adam in source_list:
                if pair[0].strip().lower() == adam.lower():
                    time_of_adam=pair[1]
                    time_list.append(time_of_adam)
        if len(time_list)>1:
            temp_2 = max(time_list)
        else:
            return ' '
        timeArray = time.localtime(temp_2)
        finalTime = time.strftime("'%Y-%m-%d %H:%M'", timeArray)
        return finalTime

    def deal_log(self, row_data, adam_time, log_inf_source, log_inf_val):

        adam_time_new = self.deal_adam(adam_time,row_data)
        # print(adam_time_new)
        #/////////////////////////////////////////////////
        #根据log获得表run的时间
        #/////////////////////////////////////////////////
        log_inf_source = [x for x in log_inf_source if len(x)>1]
        #定义sas code的列
        source_log = 0
        log_link = ''
        for j in log_inf_source:
            if row_data[self.source_program_name ].strip()[-4:] == ".sas":
                temp = row_data[self.source_program_name ].strip()[:-4]
            else:
                temp = row_data[self.source_program_name ].strip()

            if temp == j[0]:
                source_log = j[1]#get time of log of source pgm
                log_link   =  "LOG"
        # #/////////////////////////////////////////////////
        # #根据log获得表QC的时间
        # #/////////////////////////////////////////////////
        val_log=0
        for j in log_inf_val:
            if row_data[self.qc_program_name].strip()[-4:] == ".sas":
                temp=row_data[self.qc_program_name].strip()[:-4]
            else:
                temp=row_data[self.qc_program_name].strip()
            if temp == j[0]:
                val_log = j[1]#get time of log of val pgm

        #///////////////////////////////////////
        # yy to html
        # row_data.insert(0,"<input id=\"%s\"  type='checkbox' name=\"check_id\" value=\"\" >"%i)

        #<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
        #compare source and validation time of logs
        check=""
        t1=0
        t2=0
        if val_log != 0 and source_log != 0 :
            # t1=time.mktime(time.strptime(source_log,'%Y-%m-%d %H:%M'))
            # t2=time.mktime(time.strptime(val_log,'%Y-%m-%d %H:%M'))
            if source_log>val_log:
                check += "/QC U"
        if val_log == 0:
            check += "/No Val Log"
        if source_log == 0:
            check += "/No Dev Log"

        ################################################################
        ###################比较ADAM时间与source 时间######################
        adam_time_n=0
        if adam_time_new != ' ':
            adam_time_n = time.mktime(time.strptime(adam_time_new.replace("'", ''),'%Y-%m-%d %H:%M'))
        if adam_time_n >= source_log:
            check += "/ADAM"

        return [source_log,val_log,check,log_link]

    def get_rtf_pdf(self,new_row_data):

        rtf_link = ''
        pdf_link = ''
        # print(new_row_data)
        # new_row_data[2] to get RTF_out in new_raw_data
        rtf_out = new_row_data[2]
        if rtf_out != '':
            file_type = rtf_out[0]
        else:
            new_row_data =new_row_data + ['','']
            return new_row_data
        # print(self.rtfFiles)
        time_rtf = 0
        time_pdf = 0
        if  rtf_out in self.rtfFiles.keys():
            time_rtf = self.rtfFiles[rtf_out]
            rtf_link = "RTF"
            if rtf_out.find('intext') != -1:
                rtf_link = "RTF"
        if rtf_out in self.pdfFiles.keys():
            time_pdf = self.pdfFiles[rtf_out]
            pdf_link = "PDF"

        time_source_log     = new_row_data[8] #now the source_log_time in column 7

        time_source         = max([time_rtf,time_pdf,time_source_log] )

        #如果log存在，但是时间早于rtf,pdf 那么需要提醒

        if time_source_log != 0 and time_source != time_source_log:
            new_row_data[10] += "log pdf"

        timeArray = time.localtime(time_source)

        otherStyleTime = time.strftime("%Y-%m-%d %H:%M", timeArray)

        new_row_data[8] = otherStyleTime
        #如果source log缺失，应该标记为缺失log，而不是三者的最大值
        if time_source_log == 0:
            new_row_data[8] = "No log"
        # ///val_log
        time_val_log     = new_row_data[9] #now the val_log_time in column 8

        new_row_data[9] = time.strftime("%Y-%m-%d %H:%M", time.localtime(time_val_log))
        if time_val_log == 0:

            new_row_data[9] = "No log"

        new_row_data += [rtf_link,pdf_link]

        return new_row_data

    #######################################################################################
    ##########################获取所有生成的PDF及RTF#########################################
    ##########################下一步要根据文档时间与log比较取最大值###########################
    #######################################################################################
    def getFileName(self,type):
        #use -4 to remove .pdf or .rtf
        filedir = self.path_init
        path_list = []
        path_list.append(filedir + '/dev/reports/tables')
        path_list.append(filedir + '/dev/reports/figures')
        path_list.append(filedir + '/dev/reports/listings')
        path_list.append(filedir + '/dev/reports/intext')

        file_list = []
        for filedir in path_list:
            temp_list = []
            for root, dirs, files in os.walk(filedir):
                for  filespath in files :
                    if str(filespath).endswith('%s'%type):
                        mtime = os.path.getmtime(os.path.join(root, filespath))
                        file  = filespath[:-4]
                        temp_list.append([file,mtime])
            file_list += temp_list
        return file_list if file_list else []

    #######################################################################################
    #######################################################################################
    ##########################比较文档改动日期，取最大值，保证一个不少########################
    #######################################################################################
    def compTime(self):

        _rtfFiles = self.getFileName('rtf')

        _pdfFiles = self.getFileName('pdf')

        dic_a=dict(_rtfFiles)
        dic_b=dict(_pdfFiles)
        self.rtfFiles = dic_a
        self.pdfFiles = dic_b

        # all1=[]

        # for key in dic_a.keys():
        #     if key in dic_b.keys():
        #         temp=max(dic_a[key],dic_b[key])
        #         all1.append([key,temp])
        #     else:
        #         all1.append([key,dic_a[key]])

        # in_b_not_a = list(set(dic_b.keys()).difference(set(dic_a.keys()))) # b中有而a中没有的

        # for key in in_b_not_a:
        #     all1.append([key,dic_b[key]])

        # return all1 #获取的all1是所有基于RTF_out的最大的日期

    def getTime(self,filePath,type):
        temp_list = []
        for  filename in os.listdir(filePath):
                if str(filename).endswith('%s'%type):
                    mtime = os.path.getmtime(os.path.join(filePath, filename))
                    file  = filename[:-4]
                    temp_list.append([file,mtime])
        return temp_list

    # def dependency(self ):

    #     all_the_final=[]

    #     for row in range(1,self.nrows):

    #         row_data = self.table.row_values(row, start_colx=0, end_colx=None)

    #         # _type               =       row_data[self._type]
    #         # analysis            =       row_data[self.analysis ]
    #         tfl_category        =       row_data[self.tfl_category ]
    #         # population          =       row_data[self.population]
    #         titles              =       row_data[self.titles]
    #         output_number       =       str(row_data[self.output_number])
    #         base_output_name    =       row_data[self.base_output_name]
    #         source_program_name =       row_data[self.source_program_name]
    #         source_data         =       row_data[self.source_data]
    #         source_programmer   =       row_data[self.source_programmer]
    #         qc_programmer       =       row_data[self.qc_programmer]


N, M = [int(n) for n in input().split()]

n = 1
answer = ''

for _ in range(N):
    for _ in range(M):
        answer+= str(n) + ' '
        n += 1
    answer+='\n'
answer = answer.replace(' \n', '\n')
print(answer)


import aiopg
from sqlalchemy import create_engine, MetaData

from db import user, contact
from settings import config

connection_str = "mysql+pymysql://{user}:{password}@{host}:{port}/{database}"

def create_tables(engine):
    meta = MetaData()
    meta.create_all(bind=engine, tables=[user, contact])

def sample_data(engine):
    conn = engine.connect()
    conn.execute(user.insert(), [
        {'name': 'huyendtp', 'phone': '0909090909'}
    ])
    conn.execute(contact.insert(), [
        {'name': 'huyendtp', 'address': '17 Ba Huyen Thanh Quan', 'user_id': 1},
        {'name': 'huyendtp', 'address': '740/8 Su Van Hanh', 'user_id': 1}
    ])
    conn.close()

if __name__ == '__main__':
    db_url = connection_str.format(**config['mysql'])
    engine = create_engine(db_url)

    create_tables(engine)
    sample_data(engine)


# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations
from django.conf import settings
import vlkjsonfield.fields

class Migration(migrations.Migration):

    dependencies = [
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
    ]

    operations = [
        migrations.CreateModel(
            name='Answer',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('answer', models.IntegerField()),
                ('created', models.DateTimeField(auto_now_add=True)),
                ('answered_by', models.ForeignKey(related_name=b'answers', to=settings.AUTH_USER_MODEL)),
            ],
            options={
            },
            bases=(models.Model,),
        ),
        migrations.CreateModel(
            name='Question',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('question', models.TextField()),
                ('answer_type', models.PositiveSmallIntegerField(choices=[(1, b'Numeric'), (2, b'Boolean'), (3, b'Choice')])),
                ('definitions', vlkjsonfield.fields.VLKJSONField(default={})),
            ],
            options={
            },
            bases=(models.Model,),
        ),
        migrations.CreateModel(
            name='SLA',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('name', models.CharField(max_length=127)),
                ('expires', models.DateTimeField(null=True, blank=True)),
                ('due_date', models.DateTimeField(null=True, blank=True)),
            ],
            options={
            },
            bases=(models.Model,),
        ),
        migrations.AddField(
            model_name='question',
            name='sla',
            field=models.ForeignKey(related_name=b'questions', to='slas.SLA'),
            preserve_default=True,
        ),
        migrations.AddField(
            model_name='answer',
            name='question',
            field=models.ForeignKey(related_name=b'answers', to='slas.Question'),
            preserve_default=True,
        ),
    ]


import boto3
from boto3.s3.transfer import TransferConfig

s3_client = boto3.client('s3')

S3_BUCKET = 'mybucket'
FILE_PATH = '/path/to/file/'
KEY_PATH = "/path/to/s3key/"

def uploadFileS3(filename):
    config = TransferConfig(multipart_threshold=1024*25, max_concurrency=10,
                        multipart_chunksize=1024*25, use_threads=True)
    file = FILE_PATH + filename
    key = KEY_PATH + filename
    s3_client.upload_file(file, S3_BUCKET, key,
    ExtraArgs={ 'ACL': 'public-read', 'ContentType': 'video/mp4'},
    Config = config,
    Callback=ProgressPercentage(file)
    )

uploadFileS3('upload.mp4')


import torch

import src.inferencers.infbase

class Inf1(src.inferencers.infbase.Inferencer):
    def __init__(self, dataloaderfactory, model, state_path):
        self.dataloaderfactory = dataloaderfactory
        self.config = src.util.config.Config()
        self.testloader = dataloaderfactory.gettestloader()
        self.model = model
        state = torch.load(state_path, map_location=lambda storage, loc: storage)
        self.model.load_state_dict(state["state_dict"])
        self.device = torch.device("cuda:0")
        self.model.to(self.device)

    def run(self):
        inputs, labels = next(iter(self.testloader))
        inputs = inputs.to(self.device)
        labels = labels.to(self.device)

        pred = self.model(inputs)
        pred = torch.sigmoid(pred)
        pred = pred.data.cpu().numpy()
        # # print(pred.shape)
        # if self.config.dataloader_print_inferencer_num > 0:
        #     self.dataloaderfactory.print_testexample(
        # inputs, pred, labels,
        # count=self.config.dataloader_print_inferencer_num,
        # printfolder=self.config.dataloader_printfolder, printfile="pred.png")
        # print("")

'''
        inputs, labels = next(iter(self.gettestloader()))
        inputs = inputs.to(device)
        labels = labels.to(device)

        pred = model(inputs)
        pred = torch.sigmoid(pred)
        pred = pred.data.cpu().numpy()
        print(pred.shape)

        # Change channel-order and make 3 channels for matplot
        input_images_rgb = [reverse_transform(x) for x in inputs.cpu()]

        # Map each channel (i.e. class) to each color
        target_masks_rgb = [helper.masks_to_colorimg(x) for x in labels.cpu().numpy()]
        pred_rgb = [helper.masks_to_colorimg(x) for x in pred]

        helper.plot_side_by_side([input_images_rgb, target_masks_rgb, pred_rgb])

import torch
import src.util.config

class InferenceModel:
    def __init__(self, dataloaderfactory, state_path, model):
        state = torch.load(state_path, map_location=lambda storage, loc: storage)
        model.load_state_dict(state["state_dict"])
        pass
        '''