import numpy as np

import pandas as pd

import matplotlib.pyplot as plt

import os
# read_csv without arguments

nf = pd.read_csv('/kaggle/input/netflix-shows/netflix_titles.csv')

nf.head()
# Parse dates with "parse_dates" argument

nf = pd.read_csv('/kaggle/input/netflix-shows/netflix_titles.csv', parse_dates=['date_added'])

nf.head()
# Read selected columns using "usecols" argument

list_cols = ['show_id', 'type', 'title', 'country', 'date_added', 'rating', 'duration']

nf = pd.read_csv('/kaggle/input/netflix-shows/netflix_titles.csv', parse_dates=['date_added'], usecols = list_cols)

nf.head()
# Set a column as index by "index_col" argument - useful for making time series

nf = pd.read_csv('/kaggle/input/netflix-shows/netflix_titles.csv', parse_dates=['date_added'],

                 usecols = list_cols, index_col = 'show_id')

nf.head()
data_dir = '/kaggle/input/flowers-recognition/flowers/flowers'

# Dictionary of all images paths

flowers = {'sunflower':[], 'tulip':[], 'daisy':[], 'rose': [], 'dandelion': []}
# Populate the dictionary with image paths

for flower in flowers.keys():

    for dirname, _, filenames in os.walk(os.path.join(data_dir, flower)):

        for filename in filenames:

            flowers[flower].append((os.path.join(

                os.path.join(data_dir, flower), filename)))
# Showing 2 random images from each of the categories

from tensorflow.keras.preprocessing import image

for flower in list(flowers.keys()):

    plt.figure(figsize=(8, 5))

    flower_choice = np.random.choice(len(flowers[flower]),2) # Choose two images by random

    plt.subplot(1, 2, 1)

    img_path1 = flowers[flower][flower_choice[0]]

    img = image.load_img(img_path1)

    plt.imshow(img)

    plt.title(flower)

    plt.subplot(1, 2, 2)

    img_path2 = flowers[flower][flower_choice[1]]

    img = image.load_img(img_path2)

    plt.imshow(img)

    plt.title(flower)

plt.show()
# The dataset contains a metadata table

path = '../input/dickens/dickens'

meta = pd.read_csv(os.path.join(path, 'metadata.tsv'), delimiter='\t')

meta.head()
# Read the first part of one txt file

file1 = open(os.path.join(path, '924-0.txt'),'r')

_ = file1.read(500)

print(_)
files = {} # Create a dictionary of file names and contents

for f in os.listdir('/kaggle/input/dickens/dickens'):

    if f.endswith('.txt'):

        with open(os.path.join(path, f), "r") as file:

            files[f] = file.read()
# Convert to dataframe

_ = pd.DataFrame.from_dict(files, orient='index', columns=['content']).reset_index()

dickens = meta.merge(_, left_on='Path', right_on='index')

dickens.head()
from google.cloud import bigquery

client = bigquery.Client()

# Get dataset

hacker_ref = client.dataset('hacker_news', project='bigquery-public-data')

hacker = client.get_dataset(hacker_ref)

# List all table names

tables = list(client.list_tables(hacker))

for table in tables:

    print(table.table_id)
# Get a table

table_ref = hacker.table('full')

table = client.get_table(table_ref)
# Before loading the table, we can get some attributes

print ('No. of rows: ' + str(table.num_rows))

print ('Size (MB): ' + str(int(table.num_bytes / 1024768)))

print ('Columns:')

print (list(c.name for c in table.schema))
# Get first 100 rows

table_df = client.list_rows(table, max_results=100).to_dataframe()

table_df.head()
# Write SQL query to get data

# Get top 10 authors by number of articles

query = """

SELECT author, count(id) as stories

FROM `bigquery-public-data.hacker_news.stories`

GROUP BY author

ORDER BY count(id) DESC

"""

query_job = client.query(query)

iterator = query_job.result()

rows = list(iterator)

# Transform the rows into a nice pandas dataframe

top_authors = pd.DataFrame(data=[list(x.values()) for x in rows], columns=list(rows[0].keys()))

# Look at the first 10 headlines

top_authors.head(10)
img = pd.read_parquet('../input/bengaliai-cv19/train_image_data_0.parquet')

img.shape
img.head()
img2 = img.iloc[:,1:].values.reshape((-1,137,236,1))

row=3; col=4;

plt.figure(figsize=(20,(row/col)*12))

for x in range(row*col):

    plt.subplot(row,col,x+1)

    plt.imshow(img2[x,:,:,0])

plt.show()
import cv2

DIM = 64

img3 = np.zeros((img2.shape[0],DIM,DIM,1),dtype='float32')

for j in range(img2.shape[0]):

    img3[j,:,:,0] = cv2.resize(img2[j,],(DIM,DIM),interpolation = cv2.INTER_AREA)

row=3; col=4;

plt.figure(figsize=(20,(row/col)*12))

for x in range(row*col):

    plt.subplot(row,col,x+1)

    plt.imshow(img3[x,:,:,0])

plt.show()
# Free up memory

del img
import json

data  = []

with open("/kaggle/input/arxiv/arxiv-metadata-oai-snapshot.json", 'r') as f:

    for line in f:

        data.append(json.loads(line))

print("No. of records: {}".format(len(data)))
# See first item

data[0]
# Convert to dataframe - due to memory issue we only load the first 1000 items

df = pd.DataFrame(data[:1000])

df.head()
# Free up some memory

del data
from xml.etree import ElementTree

path = '../input/covid19-clinical-trials-dataset/COVID-19 CLinical trials studies/'

files = os.listdir(path)

print('Total Researches going on: ',len(files))
# Inspect the first element

file_path = os.path.join(path, files[0])

tree = ElementTree.parse(file_path)

root = tree.getroot()

print (root.tag, root.attrib)
# Look at all the children nodes

for child in root:

    print (child.tag, child.attrib)
df_temp = pd.DataFrame()

df = pd.DataFrame()

i = 0

for file in files[:10]: # Get the first 10 studies

    file_path = os.path.join(path, file)

    tree = ElementTree.parse(file_path)

    root = tree.getroot()

    trial = {} # Initialize dictionary

    # read tags using root.find() method

    trial['nct_id'] = root.find('id_info').find('nct_id').text

    trial['brief_title'] = root.find('brief_title').text

    trial['overall_status'] = root.find('overall_status').text

    df_temp  = pd.DataFrame(trial,index=[i])

    i=i+1

    df = pd.concat([df, df_temp])
df


from classic.sql_storage import TransactionContext
from sqlalchemy import create_engine

from exhauster.adapters import database, log, sensor_storage, settings, web_api
from exhauster.adapters.database import dashboard as dashboard_repos
from exhauster.application.dashboard import services

class Settings:
    db = database.Settings()
    common_settings = settings.Settings()
    web_api = web_api.Settings()
    influx = sensor_storage.Settings()

class Logger:
    log.configure(
        Settings.db.LOGGING_CONFIG,
        Settings.web_api.LOGGING_CONFIG,
    )

class DB:
    engine = create_engine(Settings.db.DATABASE_URL)
    context = TransactionContext(bind=engine, expire_on_commit=False)
    exhausters_repo = database.repositories.ExhausterRepo(context=context)
    rotor_repo = dashboard_repos.RotorRepo(context=context)

class Storage:
    influx = sensor_storage.InfluxClient(
        token=Settings.influx.TOKEN,
        url=Settings.influx.URL,
        org=Settings.influx.ORGANIZATION,
        buket=Settings.influx.BUCKET,
    )
    storage_db = sensor_storage.StorageDB(influxdb_client=influx)

class Application:
    exhausers_service = services.ExhausterService(
        exhausters_repo=DB.exhausters_repo,
        storage=Storage.storage_db,
        rotor_repo=DB.rotor_repo
    )
    graphic_service = services.GraphicService(
        exhausters_repo=DB.exhausters_repo, storage=Storage.storage_db
    )

class Aspects:
    services.join_points.join(DB.context)
    web_api.join_points.join(DB.context)

app = web_api.create_app(
    swagger_settings=Settings.web_api.SWAGGER,
    allow_origins=Settings.web_api.ALLOW_ORIGINS,
    exhauster_service=Application.exhausers_service,
    graphic_service=Application.graphic_service
)


#!/usr/bin/env python
#
# Author: Harrison Chapman
# This file will hold parent classes of types of models

from __future__ import with_statement

# GAE Imports
from google.appengine.api import memcache
from google.appengine.ext import db, ndb
from google.appengine.ext import blobstore
from google.appengine.api import files

# Local module imports
from passwd_crypto import hash_password, check_password

# Global python imports
import logging
import datetime
import logging
import itertools
from functools import wraps

import re
from unicodedata import normalize

_punct_re = re.compile(r'[!"#$%&\'()*/<=>?@\[\\\]^`{|},.]+')
_space_re = re.compile(r'[\t \-_]')

def slugify(text, delim=u'-'):
  """Generates an slightly worse ASCII-only slug."""
  result = []
  text = unicode(text)
  for word in _space_re.split(_punct_re.sub("", text.lower())):
    word = normalize('NFKD', word).encode('ascii', 'ignore')
    if word:
      result.append(word)
  return unicode(delim.join(result))

class ModelError(Exception):
  pass

class QueryError(ModelError):
  pass

class NoSuchEntry(QueryError):
  pass

def quantummethod(f):
  '''
  Class method decorator specific to the instance.

  It uses a descriptor to delay the definition of the
  method wrapper.
  '''
  class descript(object):
    def __init__(self, f):
      self.f = f

    def __get__(self, instance, klass):
      if instance is None:
        # Class method was requested
        return self.make_unbound(klass)
      return self.make_bound(instance)

    def make_unbound(self, klass):
      @wraps(self.f)
      def wrapper(*args, **kwargs):
        pass
      return wrapper

    def make_bound(self, instance):
      @wraps(self.f)
      def wrapper(*args, **kwargs):
        return self.f(instance, *args, **kwargs)
      # This instance does not need the descriptor anymore,
      # let it find the wrapper directly next time:
      #setattr(instance, self.f.__name__, wrapper)
      return wrapper

  return descript(f)

def is_key(obj):
  return (isinstance(obj, ndb.Key) or
          isinstance(obj, str) or
          isinstance(obj, unicode))

def as_key(obj):
  if isinstance(obj, str) or isinstance(obj, unicode):
    return ndb.Key(urlsafe=obj)
  if isinstance(obj, ndb.Key):
    return obj
  if isinstance(obj, ndb.Model):
    return obj.key
  if isinstance(obj, CachedModel):
    return obj.key
  return None

def as_keys(key_list):
  return filter(None, [as_key(key) for key in key_list])

class CacheItem(object):
  def __init__(self, cachekey):
    self._key = cachekey

  @classmethod
  def fetch(self, cachekey):
    pass
  def save(self, data):
    CachedModel.cache_set(data, self._key)

class CountTableCache(CacheItem):
  ''' Represents a table of keys/values in cache '''
  def __init__(self, cachekey, table=dict()):
    super(CountTableCache, self).__init__(cachekey)

class QueryCache(CacheItem):
  ''' An object representing a list of keys in the datastore
  pertaining to a query called commonly and worthy of caching.

  _dblen represents the most recent guesstimate of the (minimum)
  number of keys that an actual query would return'''
  def __init__(self, cachekey, data=None, cursor=None, more=True, keylen=0):
    if data is None: data = []
    super(QueryCache, self).__init__(cachekey)
    self.set(data=data, cursor=cursor, more=more, keylen=keylen)

  @classmethod
  def fetch(cls, cachekey):
    result = CachedModel.cache_get(cachekey)

    if result is None:
      return cls(cachekey)
    return cls(cachekey, **result)

  def save(self):
    logging.error(self._data)
    logging.error(self._cursor)
    logging.error(self._more)
    super(QueryCache, self).save({'data': self._data,
                                  'cursor': self._cursor,
                                  'more': self._more})

  def insert(self, idx, key):
    self._data.insert(idx, key)

  def append(self, key):
    self._data.append(key)

  def extend(self, keys):
    self._data.extend(keys)

  def prepend(self, key):
    self.insert(0, key)

  def __len__(self):
    return len(self._data)

  def set(self, data, cursor=None, more=True, keylen=0):
    ''' Set the data for this QueryCache with real results from
    a datastore query'''
    self._data = data
    self._cursor = cursor
    self._more = more

  def extend_by(self, data, cursor=None, more=True, keylen=0):
    ''' Extend the data for this QueryCache with real results from
    a datastore query'''
    self._data.extend(data)
    self._cursor = cursor
    self._more = more

  def need_fetch(self, num):
    ''' Determines if we need a new fetch from the datastore
    We need a new fetch if:
      a. We have fewer keys than the desired number AND
      b. We don't have maximal cache'''
    return (num > len(self) and self._more) or len(self) <= 0

  def __getitem__(self, key):
    return self._data[key][0]

  @property
  def results(self):
    return self._data

  @property
  def cursor(self):
    return self._cursor
  @cursor.setter
  def cursor(self, cursor):
    self._cursor = cursor

  @property
  def more(self):
    return self._more
  @more.setter
  def more(self, more):
    self._more = more

class SortedQueryCache(QueryCache):
  def __init__(self, cachekey, data=None, cursor=None, more=True, keylen=0,
               sort_fn=None):
    if data is None: data = []
    super(SortedQueryCache, self).__init__(cachekey, data=data, cursor=cursor,
                                           more=more, keylen=keylen)

  def ordered_unique_insert(self, new_key, new_val):
    """Run through data until we find where to add the new key. Don't
    add the key if no spot is found"""
    for i, (key, val) in enumerate(self._data):
      if new_val >= val:
        if key != new_key:
          self._data.insert(i, (new_key, new_val))
        break

  @property
  def results(self):
    if self._data:
      return zip(*self._data)[0]
    return []

class CachedModel(object):
  _RAW = None
  ''' A CachedModel is a database model which tries its best to keep
  its information synced in the memcache so that fewer database hits
  are used.
  '''

  UNCACHED_READ = "@@@noncached_count"

  LOG_SET_DEBUG = "Memcache set (%s, %s) required"
  LOG_SET_FAIL = "Memcache set(%s, %s) failed"
  LOG_DEL_ERROR = "Memcache delete(%s) failed with error code %s"

  @property
  def key(self):
    if self._dbentry:
      return self._dbentry.key
    elif self._dbkey:
      return self._dbkey
    else:
      return None

  @property
  def raw(self):
    if self._dbentry is None and self._dbkey is not None:
      self._dbentry = self._dbkey.get()
    return self._dbentry

  @classmethod
  def as_object(cls, thing):
    """ Tries really, really hard to make thing into an instance of cls. """
    if isinstance(thing, cls): return thing
    if isinstance(thing, cls._RAW): return cls(raw=thing)
    if isinstance(thing, str) or isinstance(thing, unicode):
      thing = ndb.Key(urlsafe=thing)
    if isinstance(thing, ndb.Key): return cls.get(thing)
    return None

  def __init__(self, raw=None, raw_key=None, parent=None, cached=True,
               **kwargs):
    self._cached = cached
    self._dbentry = None

    if raw is not None:
      if not isinstance(raw, self._RAW):
        raise Exception(
          "Passed raw object must be of type %s or a child. "
          "Raw object was of kind %s."%
          (self._RAWKIND, raw.__class__.__name__))
      self._dbentry = raw

    elif raw_key is not None:
      if not isinstance(raw_key, ndb.Key):
        raise Exception("raw_key must be of type ndb.Key")
      if raw_key.kind() != self._RAWKIND:
        raise Exception(
          "raw_key must point to an entry of type %s"%self._RAWKIND)
      self._dbkey = raw_key
      self._dbentry = None

    else:
      self._dbentry = self._RAW(parent=parent, **kwargs)

  @classmethod
  def cache_set(cls, value, cache_key, *args):
    '''
    Classmethod to access memcache.set

    Returns the cached value, for chaining purposes.
    '''
    logging.debug(cls.LOG_SET_DEBUG %(cache_key %args, value))
    if not memcache.set(cache_key %args, value):
      logging.error(cls.LOG_SET_FAIL % (cache_key %args, value))
    return value

  @classmethod
  def cache_delete(cls, cache_key, *args):
    '''
    Classmethod to access memcache.delete
    '''
    response = memcache.delete(cache_key %args)
    if response < 2:
      logging.error(cls.LOG_DEL_ERROR %(cache_key %args, response))

  @classmethod
  def cache_get(cls, cache_key, *args):
    '''
    Classmethod to access memcache.get
    '''
    result = memcache.get(cache_key %args)
    return result

  def add_to_cache(self):
    '''
    Populate the memcache with self. The base only stores the object
    associated to its key, but children may wish to also cache queries
    e.g. username.

    For chaining purposes, returns self
    '''
    return self

  def purge_from_cache(self):
    '''
    Remove self from the memcache where applicable. The opposite of
    "add_to_cache".
    '''
    return self

  @classmethod
  def get(cls, keys, use_datastore=True, one_key=False):
    '''
    Get the data for (a) model(s) by its key(s) using the cache first,
    and then possibly datastore if necessary
    '''
    # TODO: Don't idiot-proof this, make rest of code more observant
    if keys is None:
      return None
    if isinstance(keys, cls):
      return keys
    elif cls._RAW is not None and isinstance(keys, cls._RAW):
      return cls(raw=keys)

    if is_key(keys) or one_key:
      logging.error(keys.get())
      return cls(raw=keys.get())

    raw_objs = filter(None, ndb.get_multi(keys))
    rslt = [cls(raw=raw) for raw in raw_objs]
    return rslt

  @classmethod
  def get_by_index(cls, index, *args, **kwargs):
    keys_only = kwargs.get("keys_only") or False
    cached = cls.cache_get(index, *args)

    if cached is not None:
      if keys_only:
        return cached
      objs = cls.get(cached)
      if objs is not None:
        return objs

    return None

  @classmethod
  def get_cached_query(cls, index, *args, **kwargs):
    return QueryCache.fetch(index % args)

  def put(self):
    '''
    Update datastore with self, and then update memcache for self.
    '''
    if self._dbentry:
      ret_val = self._dbentry.put()
      if ret_val:
        self.add_to_cache()
      return ret_val

    raise Exception("Cannot put() nonexistant raw database model")

  def delete(self):
    '''
    Remove self from memcache, then from datastore.
    '''
    self.purge_from_cache()
    self.key.delete()

  @classmethod
  def delete_key(cls, key):
    cls(raw_key=key).delete()

class ApiModel(CachedModel):
  def to_json():
    pass


import pandas
import matplotlib.pyplot as plt
from fcmeans import FCM
from sklearn import metrics
from sklearn.metrics import silhouette_score
from sklearn.cluster import KMeans, SpectralClustering, AgglomerativeClustering
from sklearn.mixture import GaussianMixture
from scipy.cluster.hierarchy import dendrogram, linkage
from scipy.stats import ttest_ind

# Dataset should be in R^2 (After PCA)
def plot_data(dataset):
    principalDf = pandas.DataFrame(data=dataset, columns=['dem1', 'dem2'])
    plt.scatter(principalDf['dem1'], principalDf['dem2'], s=7)
    plt.show()

# Search for the correct number of clusters by using the 'Elbow Method'
# Dataset should be in R^2 (After PCA)
def elbow_method(dataset):
    distortions = []
    K = range(1, 18)
    for k in K:
        kmeanModel = KMeans(n_clusters=k)
        kmeanModel.fit(dataset)
        distortions.append(kmeanModel.inertia_)
    # Print plot
    plt.plot(K, distortions, 'bx-')
    plt.xlabel('k')
    plt.ylabel('Distortion')
    plt.title('The Elbow Method showing the optimal k')
    plt.show()

# Run the algorithm with the chosen k
# Dataset should be in R^2 (After PCA)
# Return the MI
def k_means_clustering(dataset, tags, k, show_plt=True):
    kmeans = KMeans(n_clusters=k)
    labels = kmeans.fit_predict(dataset)
    # Make plot of the result
    if show_plt:
        plt.title('K - Means')
        plt.scatter(dataset[:, 0], dataset[:, 1], c=labels, s=7, cmap='rainbow')
        centers = kmeans.cluster_centers_
        plt.scatter(centers[:, 0], centers[:, 1], c='black', s=15, alpha=0.3)
        plt.show()
    return metrics.adjusted_mutual_info_score(tags, labels)

# Run the algorithm with the chosen k
# Dataset should be in R^2 (After PCA)
# Return the MI
def fuzzy_cmeans_clustering(dataset, tags, k, show_plt=True):
    fcm = FCM(n_clusters=k)
    fcm.fit(dataset)
    labels = fcm.predict(dataset)
    if show_plt:
        plt.title('Fuzzy C Means')
        plt.scatter(dataset[:, 0], dataset[:, 1], c=labels, s=7, cmap='rainbow')
        plt.show()
    return metrics.adjusted_mutual_info_score(tags, labels)

# Search for the correct number of clusters by using the Silhouette Score
# Dataset should be in R^2 (After PCA)
def correct_clusters_choice(dataset, show_plt=True):
    max_score = -1
    best_match = 1
    scores = []
    r = range(2, 15)
    for k in r:
        kmeans = KMeans(n_clusters=k)
        labels = kmeans.fit_predict(dataset)
        score = silhouette_score(dataset, labels)
        scores.append(score)
        if score > max_score:
            max_score = score
            best_match = k
    if show_plt:
        plt.plot(r, scores)
        plt.title("Silhouette")
        plt.xlabel("Number Of Clusters")
        plt.ylabel("Silhouette Score")
        plt.show()
    return best_match

# Run the algorithm with the chosen k
# Dataset should be in R^2 (After PCA)
# Return the MI
def gmm_clustering(dataset, tags, k, show_plt=True):
    gmm = GaussianMixture(n_components=k)
    labels = gmm.fit_predict(dataset)
    if show_plt:
        plt.title('Gaussian Mixture Model')
        plt.scatter(dataset[:, 0], dataset[:, 1], c=labels, s=7, cmap='rainbow')
        plt.show()
    return metrics.adjusted_mutual_info_score(tags, labels)

def hierarchical_clustering(dataset):
    dendrogram(linkage(dataset, method="ward"))
    plt.title('Dendrogram')
    plt.show()

# Run the algorithm with the chosen k
# Dataset should be in R^2 (After PCA)
# Return the MI
def agglomerative_clustering(dataset, tags, k, show_plt=True):
    agg = AgglomerativeClustering(n_clusters=k)
    labels = agg.fit_predict(dataset)
    if show_plt:
        plt.title('Agglomerative Clustering')
        plt.scatter(dataset[:, 0], dataset[:, 1], c=labels, cmap='rainbow', s=7)
        plt.show()
    return metrics.adjusted_mutual_info_score(tags, labels)

# Run the algorithm with the chosen k
# Dataset should be in R^2 (After PCA)
# Return the MI
def spectral_clustering(dataset, tags, k, show_plt=True):
    sc = SpectralClustering(n_clusters=k, affinity='nearest_neighbors', random_state=0)
    labels = sc.fit_predict(dataset)
    if show_plt:
        plt.title('Spectral Clustering')
        plt.scatter(dataset[:, 0], dataset[:, 1], c=labels, cmap='rainbow', s=7)
        plt.show()
    return metrics.adjusted_mutual_info_score(tags, labels)

# Get the following information (dataset, tags, k) and the asked number of samples
# Return a list contain 5 lists of num_of_samples MI results of clustering_method
# Also return a dictionary that's maps the index with the clustering method
def get_mi_matrix(dataset, tags, k, num_of_samples):
    matrix_keys = {0: 'K-Means', 1: 'Fuzzy C Means', 2: 'Gaussian Mixture Model',
                   3: 'Agglomerative Clustering', 4: 'Spectral Clustering'}
    mi_matrix = [[], [], [], [], []]
    for i in range(num_of_samples):
        mi_matrix[0].append(k_means_clustering(dataset, tags, k, show_plt=False))
        mi_matrix[1].append(fuzzy_cmeans_clustering(dataset, tags, k, show_plt=False))
        mi_matrix[2].append(gmm_clustering(dataset, tags, k, show_plt=False))
        mi_matrix[3].append(agglomerative_clustering(dataset, tags, k, show_plt=False))
        mi_matrix[4].append(spectral_clustering(dataset, tags, k, show_plt=False))
    return mi_matrix, matrix_keys

# Check if the mean of the first algorithm's mi scores is greater than the second one
# Return the P-value
def t_test(mi_1, mi_2):
    _, p_value = ttest_ind(mi_1, mi_2, equal_var=False)
    mean1 = sum(mi_1) / len(mi_1)
    mean2 = sum(mi_2) / len(mi_2)
    if mean2 > mean1:
        return p_value / 2
    return 1 - p_value / 2

# Turn a list into a string
def list_to_str(li):
    s = "["
    for k in li:
        s += str(k) + ", "
    s += "]"
    return s

# Compare between the different clustering method using t_test (above)
# mi_matrix contain the mi scores of the 5 algorithms
# matrix_keys is a dic that's fit between row in the mi_matrix to clustering method's name
# Return the most accurate clustering method name and a .txt file with the information from the tests
def find_best_algorithm(mi_matrix, matrix_keys, current_dataset):
    # Create a text for documentation of the process
    txt = "-+-+-+-+-+-+-+-+-+-+-+-\n" \
          "Find Best Algorithm - Dataset {} \n" \
          "Significance level = 5% (Recommended)\n" \
          "-+-+-+-+-+-+-+-+-+-+-+-\n\n".format(current_dataset)
    i = 1
    best_algorithm = 0
    while i < len(mi_matrix):
        # Define the significance level as 5%
        txt += "Test #" + str(i) + ": " + matrix_keys[best_algorithm] + " & " + matrix_keys[i] + "\n"
        p_value = t_test(mi_matrix[best_algorithm], mi_matrix[i])
        txt += "\tP-value: " + str(p_value) + "\n"
        if p_value <= 0.05:
            txt += "\tFrom the P-value above we can infer that " + matrix_keys[i] + \
                   " is better than " + matrix_keys[best_algorithm] + "\n\n"
            best_algorithm = i
        else:
            # Else its stay the same
            txt += "\tFrom the P-value above we can infer that " + matrix_keys[best_algorithm] + \
                   " is better than " + matrix_keys[i] + "\n\n"
        i += 1
    txt += "For conclusion, the best algorithm for the current data (according to the T-test) is " \
           + matrix_keys[best_algorithm]
    file = open("Statistical Test For Dataset {}.txt".format(current_dataset), 'w')
    file.write(txt)
    file.close()
    return matrix_keys[best_algorithm]


import os
import pandas as pd
import warnings
from datetime import datetime
from typing import Union, Tuple

from constants import Models
from utils import read_dataframe_input, filter_df_between

warnings.simplefilter("ignore")

class DataBuilder:
    """Class to interact with data model for budget

    Attributes
    ----------
        min_date : datetime
            Start date for the budget
        max_date: datetime
            End date for the budget

    Methods
    -------
        build_data_model():
            Acquires data from multiple sources, consolidates
        get_df():
            Returns df filtered for model

    """

    def __init__(self, min_date: datetime, max_date: datetime):
        """Initializes DataBuilder class

        Parameters
        ----------
            min_date : datetime
                Start date for the budget
            max_date: datetime
                End date for the budget
        """
        self.min_date = min_date
        self.max_date = max_date

    def _get_dates(self):
        """Reads table of dates from Inputs file

        Reads table from excel file into pd.DataFrame format. Sets self.dates
        """
        dates = read_dataframe_input(**Models.BudgetDate)
        dates = filter_df_between(
            dates, "date", (self.min_date, self.max_date), Models.BudgetDate.IndexColumn
        )
        self.dates = dates

    def _get_date_list(self):
        """One-line description of function

        Multi-line expanded description of function
        """
        self.date_list = [
            f"{x['year']}-{x['month_number']}-{x['day_number']}"
            for _, x in self.dates.iterrows()
        ]

    def _get_items(self):
        """Reads table of items from Inputs file

        Reads table from excel file into pd.DataFrame format. Sets self.items
        """
        self.items = read_dataframe_input(**Models.BudgetItem)

    def _get_date_items(self):
        """Creates cartesian product of dates and items dataframes

        Combines two existing dataframes as a cross join (or cartesian product).
        Sorts by budget_item, then date ascending. Adds a 0-value field for budget amt,
        which we fill in next.
        """
        df = self.dates.merge(self.items, how="cross")
        df = df.sort_values(
            by=["budget_item_id", "date_id"], ascending=[True, True]
        ).reset_index(drop=True)
        df["budget_item_amount"] = 0.00
        self.date_items = df

    def _is_even_week(self, week_number: int) -> bool:
        """Boolean check if week_number is even

        Simple math function to check whether week is even or not
        This is used for frequencies such as 'bi-weekly' so we can understand
            when to budget for something correctly

        Parameters
        ----------
            week_number : int

        Returns
        -------
            bool
                is_even_week
        """
        return (week_number % 2) == 0

    def _validate_date(self, year: int, month: int, day: int) -> bool:
        """Boolean check of date validity

        Create date based on year, month, day params.
        Validate based on the date_list created by earlier dates data

        Parameters
        ----------
            year : int
                year of date
            month : int
                month of date
            day : int
                day in month

        Returns
        -------
            bool
                exists_in_list
        """
        return f"{year}-{month}-{day}" in self.date_list

    def _get_max_day_in_yearmonth(self, year: int, month: int) -> int:
        """Get max day_number for given month & year

        Uses existing date_items DF, filters to given year & month, returns maximum
            day_number based on those params.

        Parameters
        ----------
            year : int
                given_year
            month : int
                given_month

        Returns
        -------
            int
                last_day_in_yearmonth
        """
        return (
            self.date_items.groupby(["year", "month_number"])["day_number"]
            .max()
            .reset_index()
            .query(f"year == {year} & month_number == {month}")["day_number"]
        )

    def _get_date_attribs(self, date: datetime) -> Tuple[int, bool]:
        """Get additional date attributes

        Gets additional date attributes based on passed datetime.
        Start day of week (day # in week based on datetime passed) and
        whether the week was an even week. This is used in bi-weekly freq's
        to understand what cadence to budget on

        Parameters
        ----------
            date : datetime
                start_date provided to base cadence upon

        Returns
        -------
            Tuple[int, bool]
                day_of_week, is_even_week
        """
        iter_date = self.dates[self.dates["date"] == date].iloc[0]
        start_day_of_week = iter_date["day_of_week"]
        is_even_week = self._is_even_week(iter_date["week_number"])
        return (start_day_of_week, is_even_week)

    def _audit_date_frequencies(self):
        """Check validity of dates in date_items

        Frequency defines day of budget in some cases (i.e. always due on 30th of month)
        If month does not have 30 days, we need to account for this and move to
            the max existing day in month.
        Iterates over rows in date_items, skips rows w/o frequency_day
        If row has an invalid date, set to max_day_in_yearmonth
        """
        for index, row in self.date_items.iterrows():
            if pd.isnull(row["frequency_day"]):
                continue
            if self._validate_date(
                row["year"], row["month_number"], int(row["frequency_day"])
            ):
                continue
            else:
                new_day = self._get_max_day_in_yearmonth(
                    row["year"], row["month_number"]
                )
                self.date_items.at[index, "frequency_day"] = new_day

    def _calc_multiplier(self, row: dict) -> float:
        """Calculate multiplier for budget_amount

        Calculates multiplier applied to input budget value
        If income, keep number positive else negative (expense)
        If seasonality applied, add seasonality_multiplier on top

        Parameters
        ----------
            row : dict
                row: dict (pd.DataFrame row)

        Returns
        -------
            float
                multiplier
        """
        return (1.00 if row["item_type"] == "Income" else -1.00) * (
            row["seasonality_multiplier"] if row["is_seasonality"] else 1.00
        )

    def _validate_record(self, row: dict) -> bool:
        """Validate basic requirements for date_item

        A few criterae to check before running through freq_types
            1 - if inactive record, return false
            2 - if end_date exists and date is past end, return false
            3 - if start_date exists and date is before start, return false

        Parameters
        ----------
            row : dict
                row: dict (pd.DataFrame row)

        Returns
        -------
            bool
                is valid record
        """
        if not row["is_active"]:
            return False

        if not pd.isnull(row["end_date"]) and row["date"] > row["end_date"]:
            ## print(f"Skipping end date.. {row['endDate']}, {row['date']}")
            return False

        if not pd.isnull(row["start_date"]) and row["date"] < row["start_date"]:
            ## print(f"Skipping start date.. {row['startDate']}, {row['date']}")
            return False

        return True

    def _calculate_budget_amount(self, row: dict) -> float:
        """Calculate budget amount for record in date_items

        Performs validation_check on record
        Checks by frequency_type for matching criteria
        Returns item_amount * calc_multiplier for records we want to budget for
            else 0.00

        Parameters
        ----------
            row : dict
                row of pd.Dataframe

        Returns
        -------
            float
                budget_amount
        """
        if not self._validate_record(row):
            return 0.00

        ## Daily -> Return Budgeted Amount
        if row["frequency_type"] == "Daily":
            return row["item_amount"] * self._calc_multiplier(row)

        ## Weekly ->
        elif row["frequency_type"] == "Weekly":
            ## If Limited to a Day, Ignore other Days
            if (
                not pd.isnull(row["frequency_day"])
                and row["day_of_week"] != row["frequency_day"]
            ):
                return 0.00
            ## If same day as limited, or no limited day (and day_number == 1)
            elif (row["day_of_week"] == row["frequency_day"]) or (
                pd.isnull(row["frequency_day"]) and row["day_of_week"] == 1
            ):
                return row["item_amount"] * self._calc_multiplier(row)
            else:
                return 0.00

        ##Bi-Weekly ->
        elif row["frequency_type"] == "Bi-Weekly":
            ##If no start-date, raise exception
            if pd.isnull(row["start_date"]):
                raise ValueError(
                    "Need startDate for bi-weekly expense to determine alternating weeks."
                )
            ##If start date, validate start day of week & even/odd week increments
            else:
                start_day_of_week, is_even_week = self._get_date_attribs(
                    row["start_date"]
                )
                if (
                    self._is_even_week(row["week_number"]) == is_even_week
                    and row["day_of_week"] == start_day_of_week
                ):
                    return row["item_amount"] * self._calc_multiplier(row)
                else:
                    return 0.00

        ##Monthly ->
        elif row["frequency_type"] == "Monthly":
            ##If limited to day of month
            if not pd.isnull(row["frequency_day"]):
                ##If not same day, return 0
                if row["day_number"] != row["frequency_day"]:
                    return 0.00
                ##Otherwise, calc
                else:
                    return row["item_amount"] * self._calc_multiplier(row)

            ##If not limited to day of month & is first of month, return
            elif pd.isnull(row["frequency_day"]) and row["frequency_day"] == 1:
                return row["item_amount"] * self._calc_multiplier(row)
            ##Otherwise 0
            else:
                return 0.00

        ##Annual ->
        elif row["frequency_type"] == "Annual":
            if pd.isnull(row["frequency_date"]):
                raise ValueError("Issue with frequency_date")
            elif (
                not pd.isnull(row["frequency_date"])
                and row["date"] == row["frequency_date"]
            ):
                return row["item_amount"] * self._calc_multiplier(row)
            else:
                return 0.00

        ##One-Time ->
        elif row["frequency_type"] == "One-Time":
            if pd.isnull(row["frequency_date"]):
                raise ValueError("Issue with frequency_date")
            elif row["date"] == row["frequency_date"]:
                return row["item_amount"] * self._calc_multiplier(row)
            else:
                return 0.00

        print(row)
        print("Issue -- did not make it through an IF")
        return 0.00

    def _calc_budget_amounts(self):
        """Applies function to each row in date_items

        Calculates budget_amount field using above function
        """
        self.date_items["budget_item_amount"] = self.date_items.apply(
            lambda x: self._calculate_budget_amount(x), axis=1
        )

    def build_data_model(self):
        """Runs individual steps to create data model"""
        self._get_dates()
        self._get_date_list()
        self._get_items()
        self._get_date_items()
        self._audit_date_frequencies()
        self._calc_budget_amounts()

    def get_df(self) -> pd.DataFrame:
        """Get DF limited to fields for tool

        Returns
        -------
            pd.DataFrame
                data model
        """
        df = self.date_items.copy()
        df = df[Models.ExportData.Columns]
        return df


from PIL import Image
import os, sys

path = os.getcwd()
dirs = os.listdir( path )

def resize():
	for item in dirs:
		print(item)
		if item.endswith('.png'):
			im = Image.open(path+'/'+item)
			f, e = os.path.splitext(path+'/'+item)
			imResize = im.resize((480,344), Image.ANTIALIAS)
			imResize.save(f + '_resized.png', 'PNG', quality=90)

resize()


import sys
from comtypes.client import CreateObject
engine = CreateObject("SAPI.SpVoice")
stream = CreateObject("SAPI.SpFileStream")
from comtypes.gen import SpeechLib

sys.argv.append('CleanCode_ch5.txt')

#engine = CreateObject("SAPI.SpVoice")
#stream = CreateObject("SAPI.SpFileStream")
outfile = input("Enter output name: ")
if('.wav' not in outfile):
    outfile+=".wav"
stream.Open(outfile, SpeechLib.SSFMCreateForWrite)
engine.AudioOutputStream = stream
theText=""
if(len(sys.argv)>1):
    with open(sys.argv[1]) as f:
        theText=f.read()
else:
    theText=input("Enter text: ")
engine.speak(theText)
stream.Close()


import logging.config
import tornado
import os
from tornado.options import define, options

define('port', default=5000, help="run on the given port", type=int)
define("config", default=None, help="tornado config file")
define("debug", default=False, help="debug mode")

tornado.options.parse_command_line()

f = open(os.path.abspath(os.path.dirname(__file__)) + '/calculate_next_request_time.lua')

settings = {
    'debug': options.debug,
    'duration': 300,
    'limit': 10,
    'calculate_next_request_time': f.read()
}

f.close()

# Redis settings
REDIS = {
    'host': '127.0.0.1',
    'port': 6379,
    'selected_db': 0,
}

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'main_formatter': {
            'format': '%(levelname)s:%(name)s: %(message)s '
            '(%(asctime)s; %(filename)s:%(lineno)d)',
            'datefmt': "%Y-%m-%d %H:%M:%S",
        },
    },
    'handlers': {
        'console': {
            'level': 'DEBUG',
            'class': 'logging.StreamHandler',
            'formatter': 'main_formatter',
        },
    },
    'loggers': {
        '': {
            'handlers': ['console'],
            'level': 'DEBUG',
        }
    }
}

logging.config.dictConfig(LOGGING)

if options.config:
    tornado.options.parse_config_file(options.config)


import json
import os

import discord
from discord.ext import commands
from discord.utils import get

with open('config.json', 'r') as detailsFile:
    details_data = json.load(detailsFile)
    prefix = details_data.get('prefix')

class Welcome(commands.Cog):
    def __init__(self, bot):
        self.bot = bot

    @commands.Cog.listener()
    async def on_guild_join(self, guild):
        try:
            embed = discord.Embed(title=f"Hello! I am {self.bot.user.name}!", colour=discord.Color.blue())
            embed.description = f"I am pleased to have been invited to {guild.name}, " \
                                f"and I wanted to let the members and administrators of this server " \
                                f"know a few things about me."
            embed.add_field(name="Basic Information", value="My prefix is `bm-`, "
                                                            "and a list of my commands is available in `bm-help`.",
                            inline=False)
            embed.add_field(name="Changing my prefix", value="Use `bm-changeprefix [new prefix]` "
                                                             "to change my prefix for this server.",
                            inline=False)
            embed.add_field(name="Setting up my preferences for this server",
                            value="You can set a channel for welcome messages, botchat, madlibs, etc. "
                                  "using their respective commands.\n"
                                  "You can set a mute-role using the `setmuterole` command, "
                                  "or I could make one for this server using the `createmuterole` command. "
                                  "I set the permissions for the role automatically.\n"
                                  "You can set the default reason to be used for kicking/banning members "
                                  "using the `setkickreason` command.\n"
                                  "You can set a role to be assigned to new members "
                                  "using the `setmemberrole` command.\n",
                            inline=False)
            embed.set_thumbnail(url=self.bot.user.avatar_url)
            embed.set_footer(text="Have fun!")
            await guild.system_channel.send(embed=embed)
            embed.description = f"I am pleased to have been invited to {guild.name}, " \
                                f"and I wanted to let you, the owner of the server, " \
                                f"get to know a few things about me."
            await guild.owner.send(embed=embed)
        except:
            pass

    @commands.Cog.listener()
    async def on_member_join(self, member):
        if not os.path.exists(f'./configs/guild{member.guild.id}.json'):
            return
        with open(f'./configs/guild{member.guild.id}.json', 'r') as jsonFile:
            data = json.load(jsonFile)
            welcome_channel_id = dict(data).get('welcome_channel')
            if welcome_channel_id is None:
                return
            member_role_id = data.get('member_role')
        welcome_channel = self.bot.get_channel(id=int(welcome_channel_id))
        await welcome_channel.send(f'{member.mention} has joined **{member.guild.name}**! Say hi!')
        if not member.bot:
            # add the member role
            await member.add_roles(get(member.guild.roles, id=int(member_role_id)))
        else:
            # doesnt add the member role
            await welcome_channel.send('Oh, it\'s a bot')

    @commands.Cog.listener()
    async def on_member_remove(self, member):
        if member == self.bot.user:
            return
        if not os.path.exists(f'./configs/guild{member.guild.id}.json'):
            return
        with open(f'./configs/guild{member.guild.id}.json', 'r') as jsonFile:
            data = json.load(jsonFile)
            welcome_channel_id = dict(data).get('welcome_channel')
            if welcome_channel_id is None:
                return
        welcome_channel = self.bot.get_channel(id=int(welcome_channel_id))
        await welcome_channel.send(f'{member.mention} has left **{member.guild.name}**. Until Next time!')
        if member.bot:
            await welcome_channel.send("Oof, it's a bot!")

def setup(bot):
    bot.add_cog(Welcome(bot))


from django.conf.urls import url
from . import views

urlpatterns = [
    url(r'^$', views.home, name='home'),
    url(r'^routes/$', views.dailyRoutes, name='dailyRoutes'),
    url(r'^fees/$', views.feesTable, name='feesTable'),
    url(r'^routeAllocation$', views.routeAllocations, name='routeAllocation'),
    url(r'^vrpSolve$', views.vrpSolve, name='vrpSolve'),
    url(r'^profitability$', views.profitability, name='profitabilityAnalysis'),
]


from flask import Flask
from flask_login import LoginManager
import os, functools

# create application
application = Flask(__name__)
application.secret_key = os.urandom(24)

# create and configure LoginManager object
login_manager = LoginManager()
login_manager.login_view = 'auth.login'
login_manager.init_app(application)

from user import User
from procedures import Query

# instantiate Query object for database connection
database = Query()

# decorator opens and closes MySQLdb cursor around decorated function
def db_handler(kind):
    def w(f):
        @functools.wraps(f)
        def x(*args, **kwargs):
            database.kind = kind
            database.cursor = database.connection.cursor()
            val = f(*args, **kwargs)
            database.cursor.close()
            return val
        return x
    return w

@login_manager.user_loader
@db_handler("user")
def load_user(user_id):
    user_info = database.get("internal", user_id)
    return User(user_id, user_info[0], user_info[1])

# register blueprints
from main import main
application.register_blueprint(main, url_prefix='/')

from auth import auth
application.register_blueprint(auth, url_prefix='/auth')

# run application
if __name__ == '__main__':
    application.threaded=True
    application.run()


import numpy as np
import math
import random
import matplotlib
import matplotlib.pyplot as plt
import matplotlib.patches as patches
import matplotlib.mlab as mlab
from scipy.integrate import odeint
#import matplotlib.animation as animation

#fig=plt.figure()
#fig.set_dpi(100)
#fig.set_size_inches(7,6.5)

####  Datos a modificar del programa

m=1.0e-3 / 6.022e23  #masa de la primer ion
q=1.6e-19  #carga del primer ion

m2=2.0e-3 / 6.022e23   #masa del segundo ion
q2=1.6e-19           #carga del segundo ion

m3=3.0e-3 / 6.022e23 #masa del tercer ion
q3=1.6e-19  #carga del tercer ion

Bf=1.0e-2 #Campo magnético usado
LE=10.0e-2  #Distancia entre las placas
dV=15.0  #Diferencia de potencial entre las placas

Ef=dV/LE  #Campo eléctrico
LB=0.25  #Distancia horizontal considerada

tf=18e-6  # tiempo de simulación considerado

##########################

qm=q/m
par=[qm,Ef,Bf,LE]

def espectrometro(z,t,par):
    x,vx,y,vy=z

    if y < LE :
      cB=0.
      cE=1.
    else:
      cB=1.
      cE=0.

    if x > 0.001 and y < LE :
        vx=0
        vy=0

    dzdt=[vx,qm*vy*cB*Bf,vy,qm*cE*Ef-qm*vx*cB*Bf]
    return dzdt

qm2=q2/m2
par2=[qm2,Ef,Bf,LE]

def espectrometro2(z2,t,par2):
    x2,vx2,y2,vy2=z2

    if y2 < LE :
      cB2=0.
      cE2=1.
    else:
      cB2=1.
      cE2=0.

    if x2 > 0.001 and y2 < LE :
        vx2=0
        vy2=0

    dz2dt=[vx2,qm2*vy2*cB2*Bf,vy2,qm2*cE2*Ef-qm2*vx2*cB2*Bf]
    return dz2dt

qm3=q3/m3
par3=[qm3,Ef,Bf,LE]

def espectrometro3(z3,t,par3):
    x3,vx3,y3,vy3=z3

    if y3 < LE :
      cB3=0.
      cE3=1.
    else:
      cB3=1.
      cE3=0.

    if x3 > 0.001 and y3 < LE :
        vx3=0
        vy3=0

    dz3dt=[vx3,qm3*vy3*cB3*Bf,vy3,qm3*cE3*Ef-qm3*vx3*cB3*Bf]
    return dz3dt

# Llamada a la subrutina odeint que resuelva las ecuaciones de movimiento
# para los tres iones definidos con los campos resultantes
# el tiempo varía entre 0 y tf

nt=10000
z0=[0.0,0.0,0.000000001,0.0]
t=np.linspace(0,tf,nt)
abserr = 1.0e-8
relerr = 1.0e-6
z=odeint(espectrometro,z0,t,args=(par,),atol=abserr, rtol=relerr)
z2=odeint(espectrometro2,z0,t,args=(par2,),atol=abserr, rtol=relerr)
z3=odeint(espectrometro3,z0,t,args=(par3,),atol=abserr, rtol=relerr)

matplotlib.rc('xtick', labelsize=32)
matplotlib.rc('ytick', labelsize=32)
plt.rc('text', usetex=False)
plt.rc('font', family='serif')
plt.rc('font', size='40')

fig, ax = plt.subplots()

ax=plt.axes(xlim=(-0.2*LB,LB),ylim=(-0.2*LE,LB))

# Estas son las tres lineas de trayectorias que dibuja
# Cambiar los labels de acuerdo a lo calculado

line1, = ax.plot(z[:,0],z[:,2],'--', linewidth=2,
        label=r'$^1H^+ m_1$ = ' + "{:.2e}".format(m) + r' [kg]', )
line2, = ax.plot(z2[:,0],z2[:,2], '--', linewidth=2,
        label=r'$^2H^+ m_2$ = ' "{:.2e}".format(m2) + r' [kg]')
line3, = ax.plot(z3[:,0],z3[:,2], '--', linewidth=2,
        label=r'$^3H^+ m_3$ = ' + "{:.2e}".format(m3) + r' [kg]')

ax.legend(loc='upper center', fontsize=24)

# Create distintos rectangulos, flechas y circulo para el dibujo
rect = patches.Rectangle((-0.2*LB,0),0.4*LB,LE,linewidth=2,edgecolor='r',facecolor='none')
rect2 = patches.Rectangle((-0.02*LB,-0.05*LE),0.04*LB,LE*1.2,linewidth=2,edgecolor='none',facecolor='w')
rect3 = patches.Rectangle((-0.2*LB,LE*1.005),1.2*LB+0.1,0.6*LB,linewidth=2,edgecolor='g',facecolor='none')

arrow= patches.Arrow(0.1*LB,0.5*LE,0.0,0.2*LE,width=0.01)
arrow2= patches.Arrow(0.15*LB,0.5*LE,0.0,0.2*LE,width=0.01)
arrow3= patches.Arrow(-0.08*LB,0.5*LE,0.0,0.2*LE,width=0.01)
arrow4= patches.Arrow(-0.13*LB,0.5*LE,0.0,0.2*LE,width=0.01)

circle3=patches.Circle((-0.12*LB,LE+0.4*LB),0.05*LB,fill=False)
circle4=patches.Circle((-0.12*LB,LE+0.4*LB),0.01*LB)
circle5=patches.Circle((0.88*LB,LE+0.4*LB),0.05*LB,fill=False)
circle6=patches.Circle((0.88*LB,LE+0.4*LB),0.01*LB)

# agrega los Rectángulos, circulos, flechas y textos del dibujo
ax.add_patch(rect)
ax.add_patch(rect3)
ax.add_patch(rect2)
ax.add_patch(arrow)
ax.add_patch(arrow2)
ax.add_patch(arrow3)
ax.add_patch(arrow4)
ax.add_patch(circle3)
ax.add_patch(circle4)
ax.add_patch(circle5)
ax.add_patch(circle6)
ax.text(-0.05*LB,LE+0.38*LB, "B",fontsize=30)
ax.text(0.96*LB,LE+0.38*LB, "B",fontsize=30)
ax.text(0.1*LB, 0.3*LE, "E", fontsize=30)
ax.text(-0.12*LB, 0.3*LE, "E", fontsize=30)

#labels de los ejes x e y
ax.set_ylabel("y [m]", fontsize=32)
ax.set_xlabel("x [m]", fontsize=32)

# Esta parte agrega un grafico pequeño para ver mejor la posicion donde llegan los iones
# These are in unitless percentages of the figure size. (0,0 is bottom left)
# CAMBIAR
left, bottom, width, height = [0.58, 0.21, 0.33, 0.25]
ax = fig.add_axes([left, bottom, width, height])

line4, = ax.plot(z[:,0],z[:,2], '--', linewidth=4)
line5, = ax.plot(z2[:,0],z2[:,2], '--', linewidth=4)
line6, = ax.plot(z3[:,0],z3[:,2], '--', linewidth=4)

#intervalos de los ejes x e y para la grafica pequeña.
#CAMBIAR
ax.set_xlim(0.10,0.2)
ax.set_ylim(0.10,0.12)

#Donde pone los ticks y titulos de los ejes de la grafica pequeña
plt.xticks([0.1,0.15,0.2])
plt.yticks([0.10,0.11,0.12])
ax.set_ylabel("y [m]", fontsize=32)
ax.set_xlabel("x [m]", fontsize=32)

plt.show()


import numpy as np
from numpy.linalg import norm
import typing

from skimage.feature import peak_local_max

from defenses.detection.frequency.spectrum.utils import f_utils

from .ScoringStrategy import ScoringStrategy

class PeakDistanceStrategy(ScoringStrategy):
    """
    Wrapper for different scoring strategies where an image is given and a score is calculated based on some strategy
    """

    def __init__(self,
                 target_shape: typing.Tuple[int, int, int]):
        super().__init__(target_shape=target_shape)

    def strategy(self, f_img: np.ndarray) -> float:
        """
        Average distance of main peaks for peaks found by some peak finding algorithm in frequency space

        :param f_img: spectrogram for which to calculate score for
        :return: score
        """
        rows = f_img.shape[0]
        cols = f_img.shape[1]
        peaks = f_utils.get_main_peaks(f_img.shape, self.target_shape)

        distances = []
        for p in peaks:

            y_start = p[0] - self.target_shape[0] // 2
            y_end = p[0] + self.target_shape[0] // 2
            x_start = p[1] - self.target_shape[1] // 2
            x_end = p[1] + self.target_shape[1] // 2

            excerpt = f_img[max(y_start, 0): min(y_end, rows), max(x_start, 0): min(x_end, cols)]

            # it's a bit complicated to get the peak prediction right, as some excerpts may out of borders in parts
            local_prediction = np.array([self.target_shape[0] // 2, self.target_shape[1] / 2])
            if y_start < 0:
                local_prediction[0] = y_start + self.target_shape[0] // 2
            if x_start < 0:
                local_prediction[1] = x_start + self.target_shape[1] // 2

            coordinates = peak_local_max(excerpt, num_peaks=1)[0]  # is wrapped in extra dimension
            distance = norm(local_prediction - coordinates)
            distances.append(distance)

        return np.average(distances)

    def describe(self):
        print("ScoringStrategy={}:\n\ttarget_shape={}".format(self.__class__.__name__, self.target_shape))


import socket
import struct
import os
import threading
import time
import cv2
from PIL import Image,ImageFile
import cv2 as cv
import io
import numpy as np
import sys
from pynput import keyboard

ImageFile.LOAD_TRUNCATED_IMAGES = True # permit sending large file on multicast
MULTICAST_GROUP = '224.1.1.1'
SERVER_ADDRESS = ('', 1234)

class Multicast_to_vid:
    def __init__(self):
        self.SCREEN_DATA = b"" # bytes of png
        self.screen_size = [1000,800]
        self.listen_to_multicast() # creating socket and bind to multicast

    def save_img(self):
        """receive bytes of png and connect it to full image """
        while True:
            data,addr = self.sock.recvfrom(64000)
            if data:
                self.SCREEN_DATA+= data
            else:
                self.load_frame()   # if picture is full show it on screen with opencv

    def listen_to_multicast(self):
        """Create the socket"""

        self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

        # Bind to the server address
        self.sock.bind(SERVER_ADDRESS)

        group = socket.inet_aton(MULTICAST_GROUP)
        mreq = struct.pack('4sL', group, socket.INADDR_ANY)
        self.sock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq)

        return self.sock

    def load_frame(self):
        """load new image and show it on screen """

        image = Image.open(io.BytesIO(self.SCREEN_DATA))
        image = np.array(image)
        image = cv.resize(image,self.screen_size)
        cv.imshow('frame',image)
        self.SCREEN_DATA = b"" # reseting bytes of png after show it
        if cv2.waitKey(25) & 0xFF == ord('q'):
            cv2.destroyAllWindows()
            sys.exit(1)

    def change_screen_size(self,key):
        """ resizing screen by pressing '-'/'+' key """

        SIZE = 50
        key = str(key)
        if key == "'+'":
           self.screen_size[0]+=SIZE; self.screen_size[1]+=SIZE
        elif key == "'-'":
            self.screen_size[0]-=SIZE; self.screen_size[1]-=SIZE

cls = Multicast_to_vid()

listener = keyboard.Listener(on_press=cls.change_screen_size)
save_image = threading.Thread(target=cls.save_img)

if __name__ == '__main__':
    listener.start()
    save_image.start()


from django.shortcuts import render, HttpResponse
from .task import test_func, send_func
from send_mail_app.task import send_mail_func
from django.core.mail import send_mail
# Create your views here.

import requests
import json

from send_mail_app.models import RegisteredUser
# Create your views here.
def home(request):
    if request.method == 'POST':
        username = request.POST.get('username')
        email = request.POST.get('email')

        url = f'https://codeforces.com/api/user.rating?handle={username}'
        response = requests.get(url)
        data = response.text
        parsed = json.loads(data)

        result = parsed['result']
        lastContest = result[-1:]
        details = lastContest[0]

        RegisteredUser(username = username, email=email, oldrating = details['newRating']).save()
        send_mail('CF-rating','You have registered successfully', '', [email])
        return render(request, 'success.html')

    return render(request, 'index.html')

def send_mail_to_all(request):

    # send_mail('Codeforces Ratings Update','Your updated ratings are', '', ['paliwalap7@gmail.com'])
    # return HttpResponse("done")
    return render(request, 'test.html')


from aiogram import types
from aiogram.types import LabeledPrice
from loader import db
from utils.misc.product import Product

python_book = Product(
    title="Biror bir mahsulot bo'lishi garak adi",
    description="Mahsulotga to'lov qilish uchun quyidagi tugmani bosing.",
    currency="USD",
    prices=[
        LabeledPrice(
            label='Kitob',
            amount=500,  # 5.00$
        ),
        LabeledPrice(
            label='Yetkazib berish (7 kun)',
            amount=100,  # 1.00$
        ),
    ],
    start_parameter="create_invoice_python_book",
    photo_url='https://www.ferra.ru/thumb/1720x0/filters:quality(75):no_upscale()/imgs/2021/10/28/14/4989939/40e77b7bc45b7657f2b458a243c3052cd77ece32.png',
    photo_width=1280,
    photo_height=800,
    # photo_size=800,
    need_name=True,
    need_phone_number=True,
    need_shipping_address=True,  # foydalanuvchi manzilini kiritishi shart
    is_flexible=True,
)

REGULAR_SHIPPING = types.ShippingOption(
    id='post_reg',
    title="Pochta (3 kun)",
    prices=[
        LabeledPrice(
            'Maxsus quti', 100),
        LabeledPrice(
            '3 ish kunida yetkazish', 100),
    ]
)
FAST_SHIPPING = types.ShippingOption(
    id='post_fast',
    title='Express pochta (1 kun)',
    prices=[
        LabeledPrice(
            '1 kunda yetkazish', 100),
    ]
)

PICKUP_SHIPPING = types.ShippingOption(id='pickup',
                                       title="Do'kondan olib ketish",
                                       prices=[
                                           LabeledPrice("Yetkazib berishsiz", -100)
                                       ])


import torch
from diffusers import StableDiffusionInpaintPipeline
from PIL import Image
import pickle

# Load the model
pipe = StableDiffusionInpaintPipeline.from_pretrained(
    "runwayml/stable-diffusion-inpainting"#,
    # revision="fp16", #darsh custom
    # torch_dtype=torch.float16 #darsh custom
)
pipe.safety_checker = lambda images, **kwargs: (images, False) # disable the safety checker
# pipe.to("cuda:0")
pipe.to("cpu") #darsh custom

# Load and resize image and mask
image = "pics/iphone_story.png"
mask = "masks/iphone_story_mask.png"

image = Image.open(image)
mask = Image.open(mask) # white = inpaint, black = ignore
assert(image.size == mask.size)
orig_size = image.size
image = image.resize((512, 512))
mask = mask.resize((512, 512)).convert('L')

# Perform image cleaning
clean = pipe(prompt="", image=image, mask_image=mask).images[0] # use AI to inpaint mask
clean = Image.composite(clean, image, mask) # splice the inpainted area onto original image
clean = clean.resize(orig_size)
clean.save("outputs/iphone_story_bad1.png")


import unittest
from io import StringIO

from weblogo.seq_io import plain_io, table_io

class test_table_io(unittest.TestCase):
    def test_read(self) -> None:
        f = StringIO(table_io.example)
        seqs = table_io.read(f)
        self.assertEqual(len(seqs), 10)
        self.assertEqual(seqs[2].name, "EC0003")
        self.assertEqual(len(seqs[1]), 50)

    def test_read_fail(self) -> None:
        f = StringIO(plain_io.example)
        # Wrong alphabet
        self.assertRaises(ValueError, table_io.read, f)

    def test_write_seq(self) -> None:
        f = StringIO(table_io.example)
        seqs = table_io.read(f)

        fout = StringIO()
        table_io.write(fout, seqs)

        fout.seek(0)
        seqs2 = table_io.read(fout)

        self.assertEqual(seqs, seqs2)

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


from flask import Flask, render_template, request, flash, Response, url_for
import json
import pickle
import pandas as pd
import numpy as np
from appFunctions import *
import os
from dotenv import load_dotenv

load_dotenv()
app = Flask(__name__)
app.secret_key=os.getenv('FLASK_SECRET')

datasets=get_datasets(os.getenv('AWS_ACCESS_KEY_ID'), os.getenv('AWS_SECRET_ACCESS_KEY'))
clean_data = datasets[0]
feature_matrix = datasets[1]

@app.route("/", methods=['POST', 'GET'])
def index():
    if request.method == 'GET':
        flash("Welcome to Tasty Foods")
        flash("Begin typing to select your favorite recipe and receive recommendations")
    return render_template("index.html", visibility="hidden")

@app.route("/_autocomplete", methods=['GET'])
def autocomplete():
    recipes = get_all_recipes(clean_data)
    return Response(json.dumps(recipes), mimetype='application/json')

@app.route("/recommendations", methods=['POST', 'GET'])
def recommendations():
    results = recipe_recommendations(request.form['user_input'], clean_data, feature_matrix)
    tag_labels, tag_values = get_tag_data(feature_matrix)
    rating_labels, avgRating_values, numRating_values = get_rating_data(results)
    nutri_ids, nutri_labels, nutri_data = get_nutrition_data(results)
    return render_template("index.html", results=results, tag_labels=tag_labels, tag_values=tag_values,
                           rating_labels=rating_labels, avgRating_values=avgRating_values, numRating_values=numRating_values,
                           nutri_ids=nutri_ids, nutri_labels=nutri_labels, nutri_data=nutri_data,
                           visibility="visible")

@app.route("/_randomize", methods=['GET'])
def randomize():
    name = get_random_recipe(clean_data)
    return Response(name)

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


"""Jinja filter functions for use in configuration templates"""

import base64
import hashlib
import ipaddress
import re
from typing import Any, Callable, Optional, Union

# This global dict can be used to update the Jinja environment filters dict to include all
# registered template filter function
FILTERS = {}

def template_filter(name: Optional[str] = None) -> Callable:
    """Registers a template filter function in the FILTERS dict.

    Args:
        name: Optional alternative name to register the function as
    """

    def decorator(func):
        name_ = name if name else func.__name__
        FILTERS[name_] = func
        return func

    return decorator

@template_filter()
def ipwrap(address: Any) -> str:
    """Wraps the input argument in brackets if it looks like an IPv6 address. Otherwise, returns
    the input unchanged. This is useful in templates that need to build valid URLs from host name
    variables that can be either FQDNs or any IPv4 or IPv6 address.

    Args:
        address: Any string or object that can be cast to a string
    """
    try:
        if not isinstance(address, int):
            ipaddress.IPv6Address(address)
            return f"[{address}]"
    except ValueError:
        pass

    return str(address)

@template_filter()
def increment_ip(ip_string, increment=1):
    """Increment an IP address by a given value. Default increment value is 1.
    Args:
        ip_string: IP address string. Can be plain or with numeric /prefix
        increment: Optional increment step, defaults to 1
    Returns:
        String with the incremented IP address, with optional numeric prefix
    """
    if "/" in ip_string:
        # IP with prefix
        interface = ipaddress.ip_interface(ip_string)
        address = interface.ip + increment

        # ugly workaround for IPv4: incrementing an interface's address changes the prefix in some
        # cases.
        # Check to ensure that the incremented address is in the original network.
        if address not in interface.network:
            raise ValueError(f"IP address {address} is not in network {interface.network.with_prefixlen}")
        else:
            return f"{address}/{interface.network.prefixlen}"
    else:
        # plain IP
        ip = ipaddress.ip_address(ip_string)
        return format(ip + increment)

@template_filter()
def isofy_ipv4(ip_string, prefix=""):
    """Transform IPv4 address so it can be used as an ISO/NET address.
    All four blocks of the IP address are padded with zeros and split up into double octets.
    Example: 10.255.255.1 -> 0102.5525.5001.00
    With prefix: 10.255.255.1, 47.0023.0000.0001.0000 -> 47.0023.0000.0001.0000.0102.5525.5001.00
    Args:
        ip_string: a valid IPv4 address
        prefix: first part of the ISO address (optional)
    Returns:
        ISO address
    """
    ipaddress.IPv4Address(ip_string)  # fails for invalid IP

    if prefix != "":
        prefix_valid = bool(re.match(r"^.{2}(\..{4})*?$", prefix))
        if not prefix_valid:
            raise ValueError(f"{prefix} cannot be used as ISO prefix, please check formatting")
        prefix += "."
    # IP: split and fill with 0s
    ip_parts = ip_string.split(".")
    padded = [p.zfill(3) for p in ip_parts]
    joined = "".join(padded)
    # IP: split to chunks à 4 chars
    chunksize = 4
    ip_chunks = [joined[i : i + chunksize] for i in range(0, len(joined), chunksize)]
    # combine
    iso_address = prefix + ".".join(ip_chunks) + ".00"
    return iso_address

@template_filter()
def ipv4_to_ipv6(v6_network: Union[str, ipaddress.IPv6Network], v4_address: Union[str, ipaddress.IPv4Interface]):
    """Transforms an IPv4 address to an IPv6 interface address. This will combine an arbitrary
    IPv6 network address with the 32 address bytes of an IPv4 address into a valid IPv6 address
    + prefix length notation - the equivalent of dotted quad compatible notation.

    E.g.:
    >>> ipv6 = ipv4_to_ipv6("2001:700:dead:babe::/64", "127.0.0.1")
    >>> ipv6
    IPv6Interface('2001:700:dead:babe::7f00:1/64')
    >>> ipv6 == ipaddress.IPv6Interface('2001:700:dead:babe::127.0.0.1/64')
    True

    Args:
        v6_network: IPv6 network in prefix notation
        v4_address: IPv4 address
    Returns:
        An IPv6Address object on the given network
    """
    if isinstance(v6_network, str):
        v6_network = ipaddress.IPv6Network(v6_network)
    if isinstance(v4_address, str):
        v4_address = ipaddress.IPv4Address(v4_address)

    v6_address = v6_network[int(v4_address)]
    return ipaddress.IPv6Interface(f"{v6_address}/{v6_network.prefixlen}")

@template_filter()
def get_interface(
    network: Union[ipaddress.IPv6Interface, ipaddress.IPv4Interface, str], index: int
) -> Union[ipaddress.IPv6Interface, ipaddress.IPv4Interface]:
    """Returns a host address with a prefix length from its index in a network.

    Example:
    >>> get_interface("10.0.1.0/24", 2)
    ipaddress.IPv4Interface("10.0.1.2/24")

    Args:
        network: Network in CIDR notation
        index: The host address' index in the network prefix
    """
    if isinstance(network, str):
        network = ipaddress.ip_network(network)

    host = network[index]
    return ipaddress.ip_interface(f"{host}/{network.prefixlen}")

@template_filter()
def b64encode(s: str) -> str:
    """Returns base64 encoded string.

    Args:
        s: String to encode"""
    return base64.b64encode(s.encode()).decode()

@template_filter()
def b64decode(s: str) -> str:
    """Returns base64 decoded string.

    Args:
        s: String to decode"""
    return base64.b64decode(s.encode()).decode()

@template_filter()
def b16encode(s: str) -> str:
    """Returns base16 encoded string.

    Args:
        s: String to encode"""
    return base64.b16encode(s.encode()).decode()

@template_filter()
def b16decode(s: str) -> str:
    """Returns base16 decoded string.

    Args:
        s: String to decode"""
    return base64.b16decode(s.encode()).decode()

@template_filter()
def sha1(s: str) -> str:
    """Return SHA1 hexdigest of string s."""
    return hashlib.sha1(s.encode()).hexdigest()

@template_filter()
def sha256(s: str) -> str:
    """Return SHA256 hexdigest of string s."""
    return hashlib.sha256(s.encode()).hexdigest()

@template_filter()
def sha512(s: str) -> str:
    """Return SHA256 hexdigest of string s."""
    return hashlib.sha512(s.encode()).hexdigest()

@template_filter()
def md5(s: str) -> str:
    """Return SHA256 hexdigest of string s."""
    return hashlib.md5(s.encode()).hexdigest()


#!/usr/bin/python3
# -*- coding: utf-8 -*-

import urllib.request
import yaml
import sys
import html
import re
from bs4 import BeautifulSoup
from lxml import html

from libhtml import cleanSectionName, cleanProperty, cleanName, html2text, getValidSource, mergeYAML

URL = "https://www.pathfinder-fr.org/Wiki/Pathfinder-RPG.Points%20h%c3%a9ro%c3%afques.ashx"

PROPERTIES = [  "Condition", "Normal", "Avantage", "Spécial"]
FIELDS = ['Nom', 'Résumé', 'Catégorie', 'Conditions', 'ConditionsRefs', 'Avantage', 'AvantageHTML', 'Normal', 'Spécial', 'Source', 'Référence' ]
MATCH = ['Nom']

parsed_html = BeautifulSoup(urllib.request.urlopen(URL).read(),features="lxml")
feats = map(
    lambda node: node.find_parent("div", class_="BD"),
    parsed_html.body.find(id="PageContentDiv").find_all("b", text="Avantage")
)
spells = map(
    lambda node: node.find_parent("div", class_="BD"),
    parsed_html.body.find(id="PageContentDiv").find_all("b", text="Niveau")
)

liste = []
for content in feats:
    don = {}
    don['Nom']=cleanName(content.find("div", class_="BDtitre").text)
    don['Référence']=URL
    don['Source']="MJRA"
    don["Catégorie"]="Points héroïques"

    for key in PROPERTIES:
        attribute = content.find("b", text=key)
        if attribute is not None:
            text = ""
            for s in attribute.next_siblings:
                if s.string is not None:
                    text += " " + s.string
            if text.startswith(" . "):
                text = text[3:]

            if key == "Condition":
                key = "Conditions"

            don[key]=cleanProperty(text)

    liste.append(don)

print("Fusion avec fichier YAML existant...")

HEADER = ""

mergeYAML("../data/dons.yml", MATCH, FIELDS, HEADER, liste)

PROPERTIES = ['École', 'Niveau', 'Portée', 'Portée courte', 'Cible', 'Temps d’incantation', 'Composantes', 'Durée', 'Jet de sauvegarde', 'Résistance à la magie' ]
FIELDS = ['Nom', 'École', 'Niveau', 'Portée', 'Cible ou zone d\'effet', 'Temps d\'incantation', 'Composantes', 'Durée', 'Jet de sauvegarde', 'Résistance à la magie', 'Description', 'DescriptionHTML', 'Source', 'Référence' ]
MATCH = ['Référence']
IGNORE = ['Source']

liste = []
for content in spells:
    sort = {}
    sort['Nom']=cleanName(content.find("div", class_="BDtitre").text)
    print(sort['Nom'])
    sort['Référence']=URL + "#" + sort['Nom']
    sort['Source']="MJRA"

    key=""
    text=""
    for tag in content.find("div", class_="BDtitre").next_siblings:
        if tag.name == "b" or tag.name == "br":
            if key != "":
                if key in PROPERTIES:
                    if key == "Cible":
                        key = "Cible ou zone d'effet"
                    if key == "Temps d’incantation":
                        key = "Temps d'incantation"
                    if key == "Portée courte":
                        key = "Portée"
                        text = "courte " + text
                    sort[key]=cleanProperty(text)
                else:
                    print("- Skipping unknown property [%s]" % key)
                key=""
                text=""
        if tag.name == "b":
            key = tag.text
        if tag.name is None:
            text += tag.string
        if tag.name == "a":
            text += tag.text

    sort["Description"] = text.replace("\n", " ").strip()
    liste.append(sort)

print("Fusion avec fichier YAML existant...")

HEADER = ""

mergeYAML("../data/spells.yml", MATCH, FIELDS, HEADER, liste, IGNORE)


import logging
import re

from aiogram import Bot, Dispatcher, executor, types

from what_is_tak_bot.config import settings

# Configure logging
logging.basicConfig(format=settings.LOG_FORMAT, level=settings.LOG_LEVEL)
logger = logging.getLogger(__name__)

# Initialize bot and dispatcher
bot = Bot(token=settings.BOT_TOKEN)
dp = Dispatcher(bot)

@dp.message_handler(regexp=r'что\s+такое\s+([а-яА-Я ]+)\?*')
@dp.message_handler(regexp=r'([а-яА-Я ]+)[,\s-]+что\s+это\?*')
@dp.message_handler(regexp=r'что\s+значит\s+([а-яА-Я ]+)\?*')
@dp.message_handler(regexp=r'([а-яА-Я ]+)[,\s-]+что\s+значит\?*')
@dp.message_handler(regexp=r'([а-яА-Я ]+)[,\s-]+что\s+это\s+значит\?*')
@dp.message_handler(regexp=r'([а-яА-Я ]+)[,\s-]+что\s+это\s+может\s+значить\?*')
async def what_is(message: types.Message, regexp: re.Match):
    logger.info('got %s message in what_is', message.text)
    result = await search_needle(regexp.group(1).strip())
    if result is None:
        return
    await message.answer(result, reply=settings.BOT_REPLY)

@dp.message_handler(text_contains='?')
async def question_mark(message: types.Message):
    question_words = ('как', 'что', 'сколько', 'где', 'какой', 'кто', 'когда', 'почему', 'чем', 'чего', 'куда',
                      'который', 'кому', 'зачем', 'откуда', 'чей', 'чья', 'каков', 'отчего')
    for question_word in question_words:
        if question_word in message.text:
            logger.debug('skipping %s message in question_mark', message.text)
            return
    logger.info('got %s message in question_mark', message.text)
    result = await search_needle(message.text.strip('?'))
    await message.answer(result, reply=settings.BOT_REPLY)

async def search_needle(needle: str) -> str | None:
    return f'{needle} - это так, как лучше для Вас'

if __name__ == '__main__':
    executor.start_polling(dp, skip_updates=True)


# -*- coding: utf-8 -*-
"""
@author: Daniel
@contact: 511735184@qq.com
@file: util.py
@time: 2017/5/31 16:08
"""
import os
import codecs
import json
import yaml

from utils.i18n import gettext

def load_config(config_path, loader=yaml.Loader):
    if config_path is None:
        return {}
    if not os.path.exists(config_path):
        raise RuntimeError(gettext(u"config.yml not found in {config_path}").format(config_path))
    if ".json" in config_path:
        with codecs.open(config_path, encoding="utf-8") as f:
            json_config = f.read()
        config = json.loads(json_config)
    else:
        with codecs.open(config_path, encoding="utf-8") as stream:
            config = yaml.load(stream, loader)
    return config

if __name__ == '__main__':
    config = load_config(config_path='config.yml')
    print(config)


# Create your views here.
from django.shortcuts import render
from django.http.response import JsonResponse
from rest_framework import status
from .serializer import TokensSerializer
from .models import Tokens
from rest_framework.decorators import api_view
from rest_framework.generics import get_object_or_404
from django.core.exceptions import ObjectDoesNotExist
import secrets

@api_view(['POST'])
def generate_token(request):
    if request.method =='POST':
        token  = secrets.token_hex(32)
        token_serializer = TokensSerializer(data = {'token':token } )
        if token_serializer.is_valid():
            token_serializer.save()
            return JsonResponse({ 'status' : 0 , 'token':token, 'Message':'Token Created' }, status=status.HTTP_201_CREATED)
        return JsonResponse(token_serializer.errors, status=status.HTTP_400_BAD_REQUEST)

@api_view(['DELETE'])
def delete_token(request, token=None):
        try:
            if request.method == 'DELETE':
                token_obj = get_object_or_404(Tokens.objects.all() ,token=token)
                token_obj.delete()
                return JsonResponse({ 'status' : 0,'message' : 'Deleted!!','token':token }, status=200)
        except Exception as e:
            return JsonResponse( { 'status' : 0,'message' : 'Not Found!!' }, status=status.HTTP_404_NOT_FOUND)

@api_view(['POST'])
def assign_token(request):
    token = None
    try:
    	if request.method == 'POST':
            token = Tokens.objects.filter(is_block=False).order_by("?").first()
            data = {}
            if token:
                data['is_block'] = True
                data['token'] = token.token
                import time
                curr_time  = int(time.time()) + 20
                data['live_till'] = curr_time
                token_serializer = TokensSerializer(instance =token , data=data)
                if token_serializer.is_valid(raise_exception=True):
                    savetoken = token_serializer.save()
                    return JsonResponse({ 'status' : 0,'token' : savetoken.token,'Message':'Token Assigned' }, status=200)
            else:
                return JsonResponse(token_serializer.errors, status=status.HTTP_404_NOT_FOUND)
    except Exception as e:
        return JsonResponse( { 'status' : 1,'message' : e }, status=status.HTTP_404_NOT_FOUND)

@api_view(['POST'])
def unblock_token(request,token=None):
    try:
        if request.method == 'POST':
            token = Tokens.objects.filter(is_block=True,token=token).first()
            if token:
                data = {}
                data['is_block'] = False
                data['token'] = token.token
                token_serializer = TokensSerializer(instance =token , data=data)
                if token_serializer.is_valid(raise_exception=True):
                    token_obj = token_serializer.save()
                    return JsonResponse({ 'status' : 0,'token' : token.token,'Message':'Token has been released.' }, status=200)
    except Exception as e:
        return JsonResponse( { 'status' : 0,'message' : 'Not Found!!' }, status=status.HTTP_404_NOT_FOUND)

@api_view(['POST'])
def keep_alive_token(request,token=None):
    try:
        if request.method == 'POST':
            token_obj = Tokens.objects.get(token=token , is_block = True)
            data = {'token':token}
            if token_obj:
                generated_time  = token_obj.generated
                live_till       = token_obj.live_till
                import time
                curr_time  = int(time.time())
                message = ''
                if not token_obj.is_block and (curr_time - generated_time) > 300  :
                    token_obj.delete()
                    return JsonResponse({ 'status' : 0,'token' : token,'Message':'Token has already been expired!!.' }, status=200)
                elif(live_till - curr_time) > 0 :
                    data['live_till'] =  curr_time + 60 ;
                    message = 'Token is alive!!.'
                elif ( live_till - curr_time) <= 0:
                    data['is_block'] = False
                    data['live_till'] = None
                    message = 'Token is freed/released!!.'
                token_serializer =  TokensSerializer(instance=token_obj , data=data)
                if token_serializer.is_valid(raise_exception=True):
                    savetoken = token_serializer.save()
                    return JsonResponse({ 'status' : 0,'token' : savetoken.token,'Message':message }, status=200)
    except ObjectDoesNotExist:
        return JsonResponse( { 'status' : 0,'message' : 'Token is not alive' }, status=status.HTTP_404_NOT_FOUND)


import requests

from local.secrets import TIMESTATION_API_KEY

class Client:

    DEFAULT_OPTIONS = {
        'base_url': 'https://api.mytimestation.com/v1.1/',
    }

    def __init__(self, session=None, auth=None, **options):

        self.session = session or requests.Session()
        self.auth = auth
        self.headers = options.pop('headers', {})
        self.options = self._merge(self.DEFAULT_OPTIONS, options)

    def get(self, url, params={}, **options):
        """

        :param str url:
        :param dict params:
        :return:
        """
        response = requests.get(url, params, headers=self.headers)
        if response.status_code == 200:
            return response.json()
        else:
            self.handle_http_error(response)

    def post(self, url, params={}, **options):
        """

        :param str url:
        :param dict params:
        :return dict:
        """
        response = requests.post(url, params, headers=self.headers)
        if response.status_code == 201:
            return response.json()
        else:
            self.handle_http_error(response)

    def put(self, url, params={}, **options):
        """

        :param str url:
        :param dict params:
        :return dict:
        """
        response = requests.put(url, params, headers=self.headers)
        if response.status_code == 200:
            return response.json()
        else:
            self.handle_http_error(response)

    def delete(self, url, **options):
        """

        :param str url:
        :param dict params:
        :return dict:
        """
        response = requests.delete(url, headers=self.headers)
        if response.status_code == 200:
            return response.json()
        else:
            self.handle_http_error(response)

    @staticmethod
    def handle_http_error(response):
        error_message = response.json().get('error', {}).get('error_text', 'Unknown Error')
        raise KeyError(f"HTTP.Error.{response.status_code}: {error_message}")

    @staticmethod
    def _merge(*dicts):
        merged = {}
        for d in dicts:
            merged.update(d)
        return merged

if __name__ == '__main__':
    pass


import cv2
import numpy as np
import face_recognition
import os

class FaceRec:
    def __init__(self, path):
        images = []
        self.avgfd = []
        self.classNames = []
        myList = os.listdir(path)
        print(myList)
        for cl in myList:
            curImg = cv2.imread(f'{path}/{cl}')
            images.append(curImg)
            self.classNames.append(os.path.splitext(cl)[0])
            print(self.classNames)

        def findEncodings(images):
            encodeList = []
            for img in images:
                img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
                encode = face_recognition.face_encodings(img)[0]
                encodeList.append(encode)
            return encodeList

        self.encodeListKnown = findEncodings(images)
        print('Encoding Complete')

    def recognize(self, img):
        imgS = cv2.resize(img, (0, 0), None, 0.25, 0.25)
        imgS = cv2.cvtColor(imgS, cv2.COLOR_BGR2RGB)

        facesCurFrame = face_recognition.face_locations(imgS)
        encodesCurFrame = face_recognition.face_encodings(imgS, facesCurFrame)

        for encodeFace, faceLoc in zip(encodesCurFrame, facesCurFrame):
            # matches = face_recognition.\
            #     compare_faces(self.encodeListKnown, encodeFace)
            faceDis = face_recognition.\
                face_distance(self.encodeListKnown, encodeFace)
            # matchIndex = np.argmin(faceDis)

            self.avgfd.append(tuple([1-min(faceDis), img]))

        return img

    def clean_val(self):
        self.avgfd.clear()

    def get_result(self):
        print(self.avgfd)
        return max(self.avgfd, key=lambda i: i[0])


# Stdlib imports
# Core Django imports
from django.contrib.auth import get_user_model
from django.contrib.gis.db import models
from django.contrib.postgres.fields import CICharField
from django.db.models.signals import m2m_changed
from django.dispatch import receiver
from django.utils.translation import gettext_lazy as _
from django_extensions.db.fields import AutoSlugField

# Third-party app imports
# Imports from my apps
from src.campaigns.models import Campaign
from src.margins.models import Margin
from src.plots.models import StationsPlot

User = get_user_model()

# TODO look to add default Margin

class Station(models.Model):
    name = CICharField(
        max_length=50,
        unique=True,
        error_messages={
            "unique": _("That name already exists."),
        },
    )
    slug = AutoSlugField(
        "Station Adress",
        unique=True,
        # always_update=False,
        populate_from="name",
    )
    # GeoDjango-specific: a geometry field (MultiPolygonField)
    geom = models.PointField(srid=4326, dim=3)
    # geom = models.PointField(geography=True, default=geoPoint(0.0, 0.0))
    #
    created_at = models.DateTimeField(auto_now_add=True)
    created_by = models.ForeignKey(User, on_delete=models.SET_NULL, null=True)
    #
    station_id = CICharField(
        max_length=50,
        unique=True,
        blank=True,
        null=True,
        help_text="Station identifier",
        error_messages={
            "unique": _("That station_id already exists."),
        },
    )
    wmo_id = CICharField(
        max_length=5,
        unique=True,
        blank=True,
        null=True,
        help_text="WMO identifier",
        error_messages={
            "unique": _("That wmo_id already exists."),
        },
    )
    description = models.TextField(
        blank=True,
        help_text="Add information about the station",
    )
    margin = models.ForeignKey(
        Margin,
        on_delete=models.RESTRICT,
        related_name="stations",
        help_text="Offset around station location",
        # default=
    )
    margin_geom = models.PolygonField(dim=3, srid=4326)

    # Plots create for active station only
    is_active = models.BooleanField(default=True)

    # list of plots available for the station
    plots = models.ManyToManyField(
        StationsPlot,
        blank=True,
        related_name="stations",
    )

    # list of campaigns associated with the station
    campaigns = models.ManyToManyField(
        Campaign,
        blank=True,
        related_name="stations",
    )
    # which campaign is currently active
    active_campaign = models.IntegerField(blank=True, null=True)

    # Flexpart
    uses_flexpart = models.BooleanField(default=False)

    start_datetime = models.DateTimeField(
        null=True,
        blank=True,
        help_text="Starting date of the release",
    )
    end_datetime = models.DateTimeField(
        null=True,
        blank=True,
        help_text="Ending date of the release",
    )

    class AltUnit(models.IntegerChoices):
        METER = 1, _("m (a.g.l)")
        PASCAL = 2, _("m (a.s.l)")
        HECTOPASCAL = 3, _("hPa")

    alt_lower = models.DecimalField(
        default=0,
        max_digits=6,
        decimal_places=2,
    )
    alt_upper = models.DecimalField(
        default=0,
        max_digits=6,
        decimal_places=2,
        help_text="Upper altitude of the release",
    )
    alt_unit = models.IntegerField(
        default=1,
        choices=AltUnit.choices,
        help_text="Altitude unit",
    )
    numb_part = models.PositiveIntegerField(
        default=5000,
    )
    xmass = models.PositiveIntegerField(
        default=100,
    )
    number_grid = models.PositiveIntegerField(
        default=200,
    )
    # model = model to use for plot or list of model who contains this station ??

    class Meta:
        ordering = ["name"]

    # Returns the string representation of the model.
    def __str__(self):
        return self.name

    @property
    def longitude(self):
        return self.geom.x

    @property
    def latitude(self):
        return self.geom.y

    @property
    def altitude(self):
        return self.geom.z

    def save(self, *args, **kwargs):
        """ """
        super().save(*args, **kwargs)
        #
        if self.active_campaign:
            self.campaigns.add(self.active_campaign)
        from .util import download

        download()

    def delete(self, *args, **kwargs):
        """ """
        super().delete(*args, **kwargs)
        from .util import download

        download()

    @classmethod
    def disable_all(cls, campaign_id=None):
        if campaign_id:
            cls.objects.filter(campaigns=campaign_id).update(is_active=False)
        else:
            cls.objects.update(is_active=False)
        # for obj in cls.objects.all():
        #     obj.is_active = False
        #     obj.save()

    @classmethod
    def enable_all(cls, campaign_id=None):
        if campaign_id:
            cls.objects.filter(campaigns=campaign_id).update(is_active=True)
        else:
            cls.objects.update(is_active=True)

    @classmethod
    def active_campaign_is(cls, campaign_id=None):
        cls.objects.update(active_campaign=campaign_id)

@receiver(m2m_changed, sender=Station.plots.through)
def update_station_m2m(sender, instance, action, reverse, *args, **kwargs):
    """wait until change in Many2Many field get saved"""
    # https://stackoverflow.com/a/57308547
    if "post" in action:
        from .util import download

        download()


"""
Project by Clinton Anani
A simple intro to openCV
This project demonstrates reading images and videos, as
well as writing them to local storage

github: yendiDev
email: kceequan01@gmail.com

"""

import cv2

# load image
img = cv2.imread('lena.jpg', -1)

# show image
# cv2.imshow('Lena Image', img)
# cv2.waitKey(2000)
# cv2.destroyAllWindows()

# write image to new file
# cv2.imwrite('lena_copy.jpg', img)

# showing video
cap = cv2.VideoCapture(0)

# fourcc for video
# fourcc = cv2.VideoWriter_fourcc(*'XVID')

# video writer class
# writer = cv2.VideoWriter('recorded_video.avi', fourcc, 20.0, (640, 480))
print(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
print(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))

while cap.isOpened():
    return_val, frame = cap.read()
    if return_val:
        font = cv2.FONT_HERSHEY_SIMPLEX
        text = 'Width: '+str(cv2.CAP_PROP_FRAME_WIDTH) + ' Height: '+str(cv2.CAP_PROP_FRAME_HEIGHT)
        new_frame = cv2.putText(frame, text, (12, 312), font, 1, (255, 255, 255), 2, cv2.LINE_AA)
        cv2.imshow('Frame', frame)

        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    else:
        break

cap.release()
# writer.release()
cv2.destroyAllWindows()


from typing import Optional

class FhirSenderException(Exception):
    def __init__(
        self,
        exception: Exception,
        url: str,
        json_data: str,
        response_text: Optional[str],
        response_status_code: Optional[int],
        message: str,
    ) -> None:
        self.exception: Exception = exception
        self.url: str = url
        self.data: str = json_data
        super().__init__(
            f"FHIR send failed to {url} {response_status_code}: {json_data}.  {message} {response_text}"
        )