from setuptools import setup, find_packages
from setuptools.command.test import test as TestCommand
import sys

class PyTest(TestCommand):
    def finalize_options(self):
        TestCommand.finalize_options(self)
        self.test_args = []
        self.test_suite = True

    def run_tests(self):
        import pytest
        errno = pytest.main(self.test_args)
        sys.exit(errno)

setup(
    name='trompe',
    version='0.0',
    description='Acoustics package for Python',
    author='Felipe Raimann',
    author_email='felipeacsi@gmail.com',
    package_dir={'.': 'trompe'},
    packages=find_packages(),
    install_requires=[
        'numpy',
        ],
    tests_require=['pytest'],
    cmdclass={'test': PyTest}
)


from flask import Flask, render_template, request
from classes import PostcodeTravelInfo

app = Flask(__name__)

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

@app.route("/busInfo")
def BusInfo():
    postcode = request.args.get('postcode')
    number_of_stops = int(request.args.get('number_of_stops'))
    number_of_buses = request.args.get('number_of_buses')
    postcode_travel_info = PostcodeTravelInfo(postcode, number_of_stops, number_of_buses)
    return render_template(
        'info.html', postcode=postcode, number_of_stops=number_of_stops, number_of_buses=number_of_buses,
        postcode_travel_info=postcode_travel_info
    )

if __name__ == "__main__": app.run()


import uuid
from contextlib import contextmanager
from typing import Iterable, Iterator

import sqlalchemy
from more_itertools import chunked
from sqlalchemy import Column, Index, UniqueConstraint, ForeignKey
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
from sqlalchemy.types import JSON, Integer, String

from commit_viewer import git

from .interface import ProjectNotFound, StorageError, StoragePort

BATCH_SIZE = 1000

Base = declarative_base()

class Project(Base):
    __tablename__ = "projects"
    project_id = Column(String, primary_key=True)
    url = Column(String, nullable=False)

    UniqueConstraint("ix_projects_url", url)

class Commit(Base):
    __tablename__ = "commits"
    project_id = Column(
        String,
        ForeignKey(column=Project.project_id, ondelete="cascade", onupdate="cascade"),
        primary_key=True,
    )
    hash = Column(String, primary_key=True)
    sequence = Column(Integer)

    # as we don't need to search and correlate over these fields I just dump it
    # as a JSON object and keep indexable fields available. Worst case a
    # migration can change all of this.
    commit_data = Column(JSON, nullable=False)

Index("ix_commits_project_sequence", Commit.project_id, Commit.sequence)

class AlchemyAdapter(StoragePort):
    def __init__(self, connection_string: str) -> None:
        # setup engine and create tables
        self.engine = sqlalchemy.create_engine(connection_string)
        self.sessionmaker = sessionmaker(bind=self.engine)
        Base.metadata.create_all(self.engine)

    def store_commits(self, url: str, commits: Iterable[git.Commit]) -> None:
        try:
            with session_scope(self.sessionmaker) as session:
                project = (
                    session.query(Project).filter(Project.url == url).one_or_none()
                )
                if project is None:
                    project = Project(project_id=uuid.uuid4().hex, url=url)
                    session.add(project)
                else:
                    session.query(Commit).filter(
                        Commit.project_id == project.project_id
                    ).delete()

                for commits_chunked in chunked(enumerate(commits), BATCH_SIZE):
                    session.bulk_insert_mappings(
                        mapper=Commit,
                        mappings=[
                            {
                                "hash": commit.hash,
                                "project_id": project.project_id,
                                "sequence": seq,
                                "commit_data": commit.asdict(),
                            }
                            for seq, commit in commits_chunked
                        ],
                    )
        except sqlalchemy.exc.SQLAlchemyError as exc:
            raise StorageError(f"Failed to store commits: {exc}") from exc

    def list_commits(self, url: str) -> Iterator[git.Commit]:
        try:
            with session_scope(self.sessionmaker) as session:
                project = (
                    session.query(Project).filter(Project.url == url).one_or_none()
                )
                if project is None:
                    raise ProjectNotFound(f"Project not cached: {url}")
                else:
                    for commit in (
                        session.query(Commit)
                        .filter(Commit.project_id == project.project_id)
                        .order_by(Commit.sequence)
                        .yield_per(BATCH_SIZE)
                    ):
                        yield git.Commit(**commit.commit_data)
        except sqlalchemy.exc.SQLAlchemyError as exc:
            raise StorageError(f"Failed to store commits: {exc}") from exc

@contextmanager
def session_scope(sessionmaker):
    """Provide a transactional scope around a series of operations."""
    session = sessionmaker()
    try:
        yield session
        session.commit()
    except:
        session.rollback()
        raise
    finally:
        session.close()


from twisted.internet import defer
import bnw.core.bnw_objects as objs
from bnw.web.base import BnwWebHandler

class LoginHandler(BnwWebHandler):
    @defer.inlineCallbacks
    def respond(self):
        key = self.get_argument('key', '')
        user = yield objs.User.find_one({'login_key': key})
        if user:
            if self.request.protocol == 'https':
                self.set_cookie(
                    'bnw_loginkey', key, expires_days=30, secure=True)
            else:
                self.set_cookie('bnw_loginkey', key, expires_days=30)
            self.redirect('/')
        else:
            defer.returnValue('Bad login key')

class LogoutHandler(BnwWebHandler):
    def respond(self):
        self.clear_all_cookies()
        self.redirect('/')

class AuthMixin(object):
    @defer.inlineCallbacks
    def get_auth_user(self):
        if not getattr(self, 'user', None):
            key = self.get_cookie('bnw_loginkey', '')
            self.user = yield objs.User.find_one({'login_key': key}) \
                if key else None
            if self.user:
                self.user.activity()
        defer.returnValue(self.user)

def requires_auth(fun):
    @defer.inlineCallbacks
    def newfun(self, *args, **kwargs):
        user = yield self.get_auth_user()
        if not user:
            self.set_status(403)
            defer.returnValue(self.render('noauth.html'))
        else:
            defer.returnValue((yield fun(self, *args, **kwargs)))
    return newfun


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

from django.db import models, migrations

class Migration(migrations.Migration):

    dependencies = [
        ('basketball', '0064_auto_20180407_1759'),
        ('base', '0013_contact_creation_date'),
    ]

    operations = [
        migrations.AddField(
            model_name='memberpermission',
            name='player',
            field=models.ForeignKey(on_delete=models.CASCADE, blank=True, null=True, to='basketball.Player'),
        ),
    ]


from nltk.stem.snowball import SnowballStemmer
import enchant

def featext(prep_res):
    """Processes pre processing results
    Extracts data features for grading"""
    #print(prep_res)

    # define essay properties
    essay_list = prep_res['essay_list']
    essay_set = prep_res['essay_set']
    no_punc_list = prep_res['no_punc_list']
    low_list = prep_res['low_list']
    no_stop_list = prep_res['no_stop_list']
    gradr_no_stop_list = prep_res['gradr_no_stop_list']

    lex_div = lexical_diversity(essay_set, essay_list)
    w_cnt = word_cnt(low_list, 0)
    lng_w_cnt = word_cnt(low_list, 6)
    spl = spell_err_check(low_list)
    dst_wrd_cnt = len(essay_set)
    stm_cnt = len(stem_(low_list))

    return {'lexical_div': lex_div, 'word_cnt': w_cnt, 'long_word_cnt': lng_w_cnt, 'spell_err_cnt': spl,
            'distinct_word_cnt': dst_wrd_cnt, 'stem_cnt': stm_cnt}

def lexical_diversity(essay_set, essay_list):
    """Calculates lexical diversity
    Lexical diversity is a measure of how many different words that are used in a text,
    while lexical density provides a measure of the proportion of lexical items
    (i.e. nouns, verbs, adjectives and some adverbs) in the text."""
    return len(essay_set) / len(essay_list)

def word_cnt(text, num):
    """Call with list of essay. For all words give num=0
    Doesn't count numerics"""
    text = [word for word in text if word.isalpha()]  # take alpha
    return len([word for word in text if len(word) > num])

def spell_err_check(text):
    """Checks spelling of essay ad returns number of misspelled words."""
    dct = enchant.Dict("en_US")

    text = [word for word in text if word.isalpha()]  # take alpha
    incor = [word for word in text if dct.check(word) == False]
    return len(incor)

def stem_(text):
    stemmer = SnowballStemmer("english")
    return [stemmer.stem(word) for word in text]


#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# tf_wgan_tdt4173.py: Rudimentary implementation of a wasserstein GAN that
# is to be used as a base for the second programming task in assignment 4.
#
import sys

import numpy as np
import tensorflow as tf

import helpers

import matplotlib.pyplot as plt

def plot_errors(error_lists, nb_e):
    plt.plot([x[0] for x in error_lists[0]], [x[1] for x in error_lists[0]], 'r')
    plt.plot([x[0] for x in error_lists[1]], [x[1] for x in error_lists[1]], 'b')
    plt.axis([0, 5000, -1.5, 1.5])
    plt.show()

# You might want to alter the learning rate, number of epochs, and batch size
lr = 0.0005
nb_epochs = 20000
batch_size = 200

# Set to `None` if you do not want to write out images
path_to_images = './generated_images'

z_size = 10
x_size = 28*28

# Defined at the top because we need it for initialising weights
def create_weights(shape):
    # See paper by Xavier Glorot and Yoshua Bengio for more information:
    # "Understanding the difficulty of training deep feedforward neural networks"
    # We employ the Caffe version of the initialiser: 1/(in degree)
    return tf.random_normal(shape, stddev=1/shape[0])

#
# Creation of generator and discriminator networks START here
# Task (a) is to improve the generator and discriminator networks as they
# currently do not do very much
#

# Define weight matrices for the generator
# Note: Input of the first layer *must* be `z_size` and the output of the
# *last* layer must be `x_size`
weights_G = {'w1': tf.Variable(create_weights((z_size, x_size))),
             'b1': tf.Variable(tf.zeros(x_size)),
             'w2': tf.Variable(create_weights((x_size, x_size))),
             'b2': tf.Variable(tf.zeros(x_size))}

def generator(z, weights):
    h1 = tf.nn.relu(tf.add(tf.matmul(z, weights['w1']), weights['b1']))
    out = tf.nn.sigmoid(tf.add(tf.matmul(h1, weights['w2']), weights['b2']))

    # Return model and weight matrices
    return out

# Define weight matrices for the discriminator
# Note: Input will always be `x_size` and output will always be 1
weights_D = {'w1': tf.Variable(create_weights((x_size, x_size))),
             'w2': tf.Variable(create_weights((x_size, 1))),
             'b1': tf.Variable(tf.zeros(x_size)),
             'b2': tf.Variable(tf.zeros(1))
             }

def discriminator(x, weights):
    h1 = tf.nn.relu(tf.add(tf.matmul(x, weights['w1']), weights['b1']))
    out = tf.nn.sigmoid(tf.add(tf.matmul(h1, weights['w2']), weights['b2']))

    # Return model and weight matrices
    return out

#
# Creation of generator and discriminator networks END here
#

# Weight clipping (default `c` from the WGAN paper)
c = 0.01
clipped_D = [w.assign(tf.clip_by_value(w, -c, c)) for w in weights_D.values()]

# Definition of how Z samples are generated
z_sampler = lambda nb, dim: np.random.uniform(-1.0, 1.0, size=(nb, dim))

# Load MNIST
mnist = helpers.load_mnist_tf('./mnist')

# Define model entry-points (Z - generator, X - discriminator)
Z = tf.placeholder(tf.float32, shape=(None, z_size))
X = tf.placeholder(tf.float32, shape=(None, x_size))

# Define the different components of a GAN
sample = generator(Z, weights_G)
fake_hat = discriminator(sample, weights_D)
real_hat = discriminator(X, weights_D)

# Define error functions
error_G = - tf.reduce_mean(fake_hat)
error_D = tf.reduce_mean(real_hat) - tf.reduce_mean(fake_hat)

# Specify that we will use RMSProp (one optimiser for each model)
optimiser_G = tf.train.RMSPropOptimizer(lr).minimize(error_G, var_list=weights_G.values())
optimiser_D = tf.train.RMSPropOptimizer(lr).minimize(-error_D, var_list=weights_D.values())

# Generate Op that initialises global variables in the graph
init = tf.global_variables_initializer()

with tf.Session() as sess:
    # Initialise variables and start the session
    sess.run(init)

    if path_to_images:
        helpers.create_dir(path_to_images)

    # Run a set number of epochs (default `n_critic` from the WGAN paper)
    n_critic = 5
    errors = [[], []]
    for epoch in range(nb_epochs):
        for critic in range(n_critic):
            # Retrieve a batch from MNIST
            X_batch, critic = mnist.train.next_batch(batch_size)

            # Clip weights and run one step of the optimiser for D
            sess.run(clipped_D)
            sess.run(optimiser_D, feed_dict={Z: z_sampler(batch_size, z_size), X: X_batch})

        # Run one step of the optimiser for G
        sess.run(optimiser_G, feed_dict={Z: z_sampler(batch_size, z_size)})

        err_G = sess.run(error_G, feed_dict={Z: z_sampler(batch_size, z_size)})
        err_D = sess.run(error_D, feed_dict={Z: z_sampler(batch_size, z_size), X: X_batch})

        errors[0].append([epoch, err_G])
        errors[1].append([epoch, err_D])

        # Print out some information every nth iteration
        if epoch % 20 == 0:

            print('Epoch: ', epoch, '\t Generator error:\t {:.4f}'.format(err_G),
                  '\t Discriminator error:\t {:.4f}'.format(err_D))

        # Plot the image generated from 64 different samples to a directory
        if path_to_images and epoch % 500 == 0:
            samples = sess.run(sample, feed_dict={Z: z_sampler(64, z_size)})

            figure = helpers.plot_samples(samples)
            plt.savefig('{}/{}.png'.format(path_to_images, str(epoch)), bbox_inches='tight')
            plt.close()

    plot_errors(errors, nb_epochs)

del sess
sys.exit(0)


# %% md
# importing libaries

# %% codecell
import pandas as pd
import os
# import numpy as np
# from sklearn.preprocessing import OneHotEncoder
# from sklearn.compose import ColumnTransformer
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
pd.options.display.html.table_schema = True
pd.options.display.max_rows = None

# %% codecell
# importing dataset
def import_dataset():
    if '__file__' in globals() or os.path.basename(os.getcwd()) != 'MachineLearning_A-Z':
        path = os.getcwd() + '/Social_Network_Ads.csv'
    else:
        path = os.getcwd() + '/Machine_Learning_A-Z_Mine/Part 3 - Classification/Section 14 - Logistic Regression/Social_Network_Ads.csv'
    df = pd.read_csv(path)
    x = df.iloc[:, 2:4].values
    y = df.iloc[:, 4].values
    return x, y

"""
# Encode Categorial Data
def encode_categorical(x):
    ct = ColumnTransformer([('encoder', OneHotEncoder(), [3])], remainder='passthrough')
    x = np.array(ct.fit_transform(x))
    return x[:, 1:]
"""

# %% codecell
# Splitting the dataset into the Training set and Test set
def train_dataset(x, y):
    return train_test_split(x, y, test_size = .25, random_state = 0)

# %% codecell
# Feature Scaling
def scale_data(x_train, x_test):
    sc_x = StandardScaler()
    x_train = sc_x.fit_transform(x_train)
    x_test = sc_x.transform(x_test)
    #sc_y = StandardScaler()
    #y = y.reshape(-1, 1)
    #y = sc_y.fit_transform(y)
    return [x_train, x_test, sc_x]

# %% codecell
# main
def preprocess_data():
    x, y = import_dataset()
    #x = encode_categorical(x)
    x_train, x_test, y_train, y_test = train_dataset(x, y)
    x_train, x_test, sc_x = scale_data(x_train, x_test)
    return x_train, x_test, y_train, y_test, sc_x
    #return x, y

# %% codecell
if __name__ == '__main__':
    preprocess_data()

Machine_Learning_A-Z_Mine/Part 3 - Classification/Section 15 - K-Nearest Neighbors (K-NN)/data_preprocessing_template.py


import os
import datetime
from celloapi2 import CelloQuery, CelloResult

# Opimized Process Time Recording Start
starttime = datetime.datetime.now()
# Set our directory variables.
in_dir  = os.path.join(os.getcwd(), "input")
out_dir = os.path.join(os.getcwd(), "output")

modified_dir = "modified/"

# Set our input files.
chassis_name = "Eco1C1G1T1"
in_ucf  = f"{chassis_name}.UCF.json"
v_file  = "and.v"
options = "options.csv"
# input_sensor_file = f'{chassis_name}.input.json'  # this is original file
input_sensor_file  = f"{modified_dir}{chassis_name}.input.json"  # this is modified file
output_device_file = f"{chassis_name}.output.json"
q = CelloQuery(
    input_directory=in_dir,
    output_directory=out_dir,
    verilog_file=v_file,
    compiler_options=options,
    input_ucf=in_ucf,
    input_sensors=input_sensor_file,
    output_device=output_device_file,
)

# Submit our query to Cello. This might take a second.
q.get_results()
# Fetch our Results.
res = CelloResult(results_dir=out_dir)
endtime = datetime.datetime.now()
print("The Optimized Score is:",res.circuit_score)
print ("The Processing Time is:",(endtime - starttime).seconds,"Seconds")


from flask import Flask
from flask_praetorian import PraetorianError
from werkzeug.contrib.fixers import ProxyFix

from . import api, cli
from . import config

from .extensions import db, guard, ma, migrate, cors
from .models.user import User as UserModel

def create_app(config=None, testing=False, is_cli=False):
    """Application factory, used to create application."""
    app = Flask("eziscop")
    app.wsgi_app = ProxyFix(app.wsgi_app)

    configure_app(app, testing)
    configure_extensions(app, is_cli)
    register_blueprints(app)

    return app

def configure_app(app, testing=False):
    """Set configuration for application."""
    # default configuration
    app.config.from_object(config)

    if testing is True:
        # override with testing config
        app.config.from_object("configtest")
    else:
        # override with env variable, fail silently if not set
        app.config.from_envvar("EZISCOP_CONFIG", silent=True)

def configure_extensions(app, is_cli):
    """Configure flask extensions."""
    db.init_app(app)
    # ma must be initilized AFTER SQLAlchemy
    ma.init_app(app)

    guard.init_app(app, UserModel)
    PraetorianError.register_error_handler_with_flask_restplus(api.api)

    cors.init_app(app)
    cli.init_app(app)
    # if cli is True:
    migrate.init_app(app, db)

def register_blueprints(app):
    """Register all blueprints for application."""
    app.register_blueprint(api.blueprint)


import logging
import re

from time import time

from jinja2 import Environment
from bs4 import BeautifulSoup

logger = logging.getLogger(__name__)

nfo_template_str = """<musicvideo>
  <title>{{song.title}}</title>
  <artist>{{song.artist}}</artist>
  <album>{{song.artist}}</album>
  <genre>Pop</genre>
  <director></director>
  <composer></composer>
  <studio></studio>
  <year></year>
  <runtime></runtime>
  <mormuvid>
     <status>{{song.status}}</status>
     <updated_at>{{song.updated_at}}</updated_at>
     <video_watch_url>{{song.video_watch_url or ''}}</video_watch_url>
     <scouted_by>{{song.scouted_by or ''}}</scouted_by>
  </mormuvid>
</musicvideo>
"""

def _safe_strip(maybe_str):
    if maybe_str is None:
        return None
    else:
        return maybe_str.strip()

def _make_id(artist, title):
    raw_name = artist + " - " + title
    ascii_name = raw_name.encode('punycode')
    safe_name = re.sub(r"[^0-9A-Za-z .,;()_\-]", "_", ascii_name)
    return safe_name

class Song(object):
    """
    Represents a song.
    """
    def __init__(self, artist, title, song_id=None, scouted_by=None):
        self.status = 'SCOUTED'
        self.artist = artist
        # make album name up for now
        self.album = artist
        self.title = title
        if song_id is None:
            self.id = _make_id(artist,title);
        else:
            self.id = song_id
        self.video_watch_url = None
        self.scouted_by = scouted_by
        self.mark_updated()

    def mark_updated(self):
        self.updated_at = time()

    def mark_found(self, video_watch_url):
        self.status = 'FOUND'
        self.mark_updated()
        self.video_watch_url = video_watch_url

    def mark_find_queued(self):
        self.status = 'FIND_QUEUED'
        self.mark_updated()

    def mark_download_queued(self):
        self.status = 'DOWNLOAD_QUEUED'
        self.mark_updated()

    def mark_failed(self):
        self.status = 'FAILED'
        self.mark_updated()

    def mark_downloaded(self):
        self.status = 'COMPLETED'
        self.mark_updated()

    def is_queued(self):
        return self.status in ['FIND_QUEUED', 'DOWNLOAD_QUEUED']

    def get_base_file_name_wo_ext(self):
        return self.id

    def to_nfo_xml(self):
        global nfo_template_str
        env = Environment(autoescape = True)
        template = env.from_string(nfo_template_str)
        nfo_xml = template.render(song = self)
        return nfo_xml

    def is_stale(self):
        """
        Can we forget about this song?
        """
        status = self.status
        if status in ['COMPLETED']:
            # no, this is definitely still needed
            return False
        elif status in ['FIND_QUEUED', 'DOWNLOAD_QUEUED', 'FAILED', 'FOUND']:
            # these statuses should be temporary  - assume something
            # got stuck after a while and remove entry
            age_seconds = time() - self.updated_at
            retry_after_seconds = (24 * 60 * 60)
            return age_seconds > retry_after_seconds
        else:
            # unknown status - guess leave it alone.
            return False

    def __str__(self):
        return '"{}" by "{}"'.format(
            self.title.encode('unicode-escape'),
            self.artist.encode('unicode-escape')
        )

    @staticmethod
    def from_nfo_xml(nfo_xml, base_file_name_wo_ext=None):
        is_mormuvid_file = True
        soup = BeautifulSoup(nfo_xml)
        musicvideo = soup.musicvideo
        song = Song(_safe_strip(musicvideo.artist.string), _safe_strip(musicvideo.title.string), base_file_name_wo_ext)
        song.album = _safe_strip(musicvideo.album.string)
        mormuvid_info = soup.musicvideo.mormuvid
        if mormuvid_info is None:
            is_mormuvid_file = False
        else:
            status = mormuvid_info.find('status', recursive=False)
            if status is None:
                is_mormuvid_file = False
        if not is_mormuvid_file:
            song.status = 'COMPLETED'
            song.updated_at = None
            song.video_watch_url = None
            song.scouted_by = None
        else:
            song.status = _safe_strip(mormuvid_info.status.string)
            song.updated_at = float(_safe_strip(mormuvid_info.updated_at.string))
            song.video_watch_url = _safe_strip(mormuvid_info.video_watch_url.string)
            try:
                song.scouted_by = _safe_strip(mormuvid_info.scouted_by.string)
            except:
                song.scouted_by = None

        return song


import numpy as np
import matplotlib.pyplot as plt

# パラメータ
fs = 44100
f = 5

t = np.arange(0, fs) / fs
x = np.sin(2 * np.pi * f * t)
fft_size = int(2 ** np.ceil(np.log2(len(x))))
X1 = np.fft.fft(x)
X2 = np.fft.fft(x, fft_size)

w = np.arange(0, fft_size) * fs / fft_size

# 結果
plt.plot(w, abs(X2))
plt.xlim(0, 50)
plt.xlabel("frequency [Hz]")
plt.ylabel("amplitude")
plt.grid()

plt.show()


from django.shortcuts import render_to_response, redirect, get_object_or_404
from django.template import RequestContext
from django.contrib import messages
from utils.group_decorator import group_admin_required
from django.core.urlresolvers import reverse
from intranet.caffeine_manager.trays.models import Tray
from intranet.caffeine_manager.trays.forms import TrayForm
from intranet.caffeine_manager.views import fromLocations
import subprocess

def view(request):
    request.session['from']=fromLocations.TRAYS
    trays=Tray.objects.all().order_by('tray_number')
    is_caffeine_admin=request.user.is_group_admin('Caffeine')
    return render_to_response(
       'intranet/caffeine_manager/trays/trays.html',
       {
         'section':'intranet',
         'page':'caffeine',
         'sub_page':'trays',
         'trays':trays,
         'is_caffeine_admin':is_caffeine_admin
       },
       context_instance=RequestContext(request))

@group_admin_required(['Caffeine'])
def add_tray(request):
    tray_form=None;
    if request.method == 'POST':
        tray_form=TrayForm(request.POST)
        if tray_form.is_valid():
            tray_form.save()
            return redirect(reverse('cm_trays_view'))
    else:
        tray_form=TrayForm()

    return render_to_response(
       'intranet/caffeine_manager/trays/edit_tray.html',
       {
         'section':'intranet',
         'page':'caffeine',
         'form':tray_form
       }, context_instance=RequestContext(request))

#@group_admin_required(['Caffeine'])
def edit_tray(request, trayId):
    tray=get_object_or_404(Tray, pk=trayId)
    if request.method == 'POST':
        tray_form=TrayForm(request.POST, instance=tray)
        if tray_form.is_valid():
            tray_form.save()
            return redirect(reverse('cm_trays_view'))
    else:
        tray_form=TrayForm(instance=tray)

    return render_to_response(
       'intranet/caffeine_manager/trays/edit_tray.html',
       {
         'section':'intranet',
         'page':'caffeine',
         'form':tray_form,
         'id':trayId
       }, context_instance=RequestContext(request))

@group_admin_required(['Caffeine'])
def delete_tray(request, trayId):
    get_object_or_404(Tray, pk=trayId).delete()
    return redirect(reverse('cm_trays_view'))

@group_admin_required(['Caffeine'])
def force_vend(request, trayId):
    ret=subprocess.call(['ssh', 'nassri2@acm.illinois.edu', "echo 'not yet implemented'"]) # Requires a valid ssh key
    if ret == 0:
        messages.add_message(request, messages.SUCCESS, 'Force vend successful!')
    else:
        messages.add_message(request, messages.ERROR, 'Force vend failed.')
    return redirect(reverse('cm_trays_view'))


import pygame
import numpy as np
import random
import os
import settings
from agents import abstract_conv_agent

class HumanAgent(abstract_conv_agent.AbstractConvAgent):

    def __init__(self, name, input_shape, window_length, nr_actions, learning_rate, gamma, epsilon, min_epsilon,
                 epsilon_decay):

        super().__init__(self, name, input_shape, window_length, nr_actions,
                                                       learning_rate, gamma, epsilon, min_epsilon, epsilon_decay)
        print("Initializing Human Agent witch convolutional network")

    def decay_epsilon(self):
        pass

    def policy_out_of_event(self, event):
        action = settings.MOVE_FORWARD

        # Returns the action
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_RIGHT:
                action = settings.MOVE_RIGHT
            elif event.key == pygame.K_LEFT:
                action = settings.MOVE_LEFT
            elif event.key == pygame.K_SPACE:
                action = settings.BLAST
            else:
                action = settings.MOVE_FORWARD

        elif event.type == pygame.KEYUP:
            action = settings.MOVE_FORWARD

        return action

class SelfLearningAgent(abstract_conv_agent.AbstractConvAgent):

    def __init__(self, name, input_shape, window_length, nr_actions, learning_rate, gamma, epsilon, min_epsilon,
                 epsilon_decay, human_guided):

        super().__init__(self, name, input_shape, window_length, nr_actions,
                                                       learning_rate, gamma, epsilon, min_epsilon, epsilon_decay)
        self.human_guided = human_guided
        if self.human_guided:
            print("Initializing Human Guided Conv Agent")
        else:
            print("Initializing Conv Agent")

    def policy_out_of_event(self, event):
        return None

    def policy_out_of_experience(self, experience):

        actions = []

        if np.random.rand() <= self.epsilon:
            'Exploration'
            return random.randrange(self.nr_actions)
        else:
            'Exploitation'
            actions = self.model.predict(experience)

        # return max action if not a blast
        action = np.argmax(actions)

        return action

    def load_model(self, weights_file):
        """ Loads model architecture, weights, compilation information and optimizer. Doesnt copy the name of the weights
        if the model is human guided."""
        if os.path.isfile(weights_file):
            self.model.load_weights(weights_file)
            if not self.human_guided:
                self.name = os.path.splitext(os.path.basename(weights_file))[0]

class InteractiveAgent(abstract_conv_agent.AbstractConvAgent):

    def __init__(self, name, input_shape, window_length, nr_actions, learning_rate, gamma, epsilon, min_epsilon,
                 epsilon_decay):

        super().__init__(self, name, input_shape, window_length, nr_actions,
                                                       learning_rate, gamma, epsilon, min_epsilon, epsilon_decay)
        print("Initializing Interactive Agent with conv network")

    def policy_out_of_event(self, event):
        action = None

        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_SPACE:
                action = settings.BLAST
        return action

    def policy_out_of_experience(self, experience):

        actions = []

        if np.random.rand() <= self.epsilon:
            'Exploration'
            # Return a non-blast action
            return random.randrange(self.nr_actions - 1)
        else:
            'Exploitation'
            actions = self.model.predict(experience)

        # return max action if not a blast, if blast, return secondbest
        action = np.argmax(actions)

        if action == settings.BLAST:
            action = (np.argsort(actions))[0][self.nr_actions - 2]

        return action


#!/usr/bin/env python
"""
Save as get-tp.py, then run "python get-tp.py".

Input file : None
Output file: tasmin_20180101.nc
"""
from ecmwfapi import ECMWFDataServer
import datetime

starttime = datetime.datetime.strptime('2018-11-01','%Y-%m-%d')
endtime = datetime.datetime.strptime('2018-12-31','%Y-%m-%d')
date_series = [starttime + datetime.timedelta(days=x) for x in range(0,(endtime-starttime).days+1)]
for date in date_series:
    date_string = date.strftime("%Y-%m-%d")
    print(date_string)
    output_file = "/storage/data/projects/rci/data/winter_sports/ERA_INTERIM/tasmin/download/tasmin_00_"+date_string+".nc"
    serv_list = {
        "class"  : "ei",
        "dataset": "interim",
        "date"   : date_string,
        "expver" : "1",
        "grid"   : "0.75/0.75",
        "levtype": "sfc",
        "param"  : "202.128",
        "step"   : "3/6/9/12",
        "stream" : "oper",
        "time"   : "00:00:00",
        "type"   : "fc",
        "format" : "netcdf",
        "target" : output_file,
    }
    server = ECMWFDataServer()
    server.retrieve(serv_list)


from flask import jsonify, render_template, request, abort
from app.geojson import bp
from app import db

from sqlalchemy import text

@bp.route('/geojson/parking/<valueType>')
def geojson_parking(valueType):
    bbox = request.args.get('bbox', '')

    (southwest_lng, southwest_lat, northeast_lng, northeast_lat) = bbox.split(',')
    linestring = 'LINESTRING(' + southwest_lng + ' ' + southwest_lat + ',' + northeast_lng + ' ' + northeast_lat + ')'
    sql = text('SELECT *, ST_X(ST_Centroid(ST_Transform(geom, 4326))) AS lon, ST_Y(ST_Centroid(ST_Transform(geom, 4326))) AS lat FROM imposm3.view_parking WHERE ST_WITHIN(st_transform(geom,4326), ST_Envelope(ST_GeomFromText(:linestring, 4326) ) )')
    result = db.engine.execute(sql, {'linestring': linestring})

    return render_geojson_nodes(result, valueType)

@bp.route('/geojson/missing/<city>')
def geojson_missing(city):
    bbox = request.args.get('bbox', '')
    is_cluster = request.args.get('is_cluster', False)

    sql = text('SELECT * FROM extern.external_data WHERE city=:city')
    external_data = db.engine.execute(sql, {'city': city}).fetchone()

    if external_data is None:
        abort(404)

    suffix = external_data['suffix']
    if is_cluster is not False and external_data['is_cluster'] is False:
        abort(404)
    if is_cluster is not False:
        suffix += '_cluster'

    where_condition = ''
    filter_execute = {}
    lessContent = True
    if bbox != '':
        (southwest_lng, southwest_lat, northeast_lng, northeast_lat) = bbox.split(',')
        linestring = 'LINESTRING(' + southwest_lng + ' ' + southwest_lat + ',' + northeast_lng + ' ' + northeast_lat + ')'
        where_condition = ' WHERE ST_WITHIN(st_transform(geom,4326), ST_Envelope(ST_GeomFromText(:linestring, 4326) ) )'
        filter_execute = {'linestring': linestring}
        lessContent = False

    sql = text('SELECT *, ST_X(ST_Centroid(ST_Transform(geom, 4326))) AS lon, ST_Y(ST_Centroid(ST_Transform(geom, 4326))) AS lat FROM extern.missing_parking_' + suffix + where_condition)
    result = db.engine.execute(sql, filter_execute)

    return render_geojson_nodes_external(result, city, lessContent=lessContent)

@bp.route('/geojson/existing/<city>')
def geojson_existing(city):
    bbox = request.args.get('bbox', '')
    is_cluster = request.args.get('is_cluster', False)

    sql = text('SELECT * FROM extern.external_data WHERE city=:city')
    external_data = db.engine.execute(sql, {'city': city}).fetchone()

    if external_data is None:
        abort(404)

    suffix = external_data['suffix']
    if is_cluster is not False and external_data['is_cluster'] is False:
        abort(404)

    if is_cluster is not False:
        suffix += '_cluster'

    (southwest_lng, southwest_lat, northeast_lng, northeast_lat) = bbox.split(',')
    linestring = 'LINESTRING(' + southwest_lng + ' ' + southwest_lat + ',' + northeast_lng + ' ' + northeast_lat + ')'
    sql = text('SELECT existing_data.*, osm.osm_id int_osm_id, osm.typ int_typ, osm.name int_name, osm.bicycle_parking int_bicycle_parking, osm.access int_access, osm.capacity int_capacity, osm.covered int_covered, ST_X(ST_Centroid(ST_Transform(existing_data.geom, 4326))) AS lon, ST_Y(ST_Centroid(ST_Transform(existing_data.geom, 4326))) AS lat FROM extern.existing_parking_' + suffix + ' existing_data LEFT JOIN imposm3.view_parking osm ON osm.geom && ST_Expand(existing_data.geom, 50) WHERE ST_WITHIN(st_transform(existing_data.geom,4326), ST_Envelope(ST_GeomFromText(:linestring, 4326) ) ) ORDER BY st_distance(existing_data.geom,osm.geom)')
    result = db.engine.execute(sql, {'linestring': linestring})

    return render_geojson_nodes_external(result, city, True)

@bp.route('/geojson/value/<valueType>')
def get_values(valueType):
    json_result = []

    bbox = request.args.get('bbox', '')
    (southwest_lng, southwest_lat, northeast_lng, northeast_lat) = bbox.split(',')
    linestring = 'LINESTRING(' + southwest_lng + ' ' + southwest_lat + ',' + northeast_lng + ' ' + northeast_lat + ')'
    sql = text('SELECT * FROM imposm3.view_parking WHERE ST_WITHIN(st_transform(geom,4326), ST_Envelope(ST_GeomFromText(:linestring, 4326) ) )')

    result = db.engine.execute(sql, {'linestring': linestring})
    for row in result:
        if row[valueType] not in json_result and str(row[valueType]) != '':
            json_result.append(row[valueType])
    json_result.sort()

    return jsonify(json_result)

def render_geojson_nodes_external(result, city, existing=False, lessContent=False):
    features = []
    for row in result:
        if not lessContent:
            prop = {'popupContent': render_template('node_popup_external.html', node=row)}
        else:
            prop = {}
        for col_name in row.keys():
            if not lessContent or (row[col_name] is not None and col_name not in ['geom', 'lat', 'lon', 'x', 'y', 'long__mapinfo_', 'lat__mapinfo_', 'geojson', 'ogc_fid', 'int_osm_id', 'int_typ', 'int_name', 'int_access', 'int_bicycle_parking', 'int_capacity', 'int_covered', 'int_ranking']):
                prop[col_name] = row[col_name]
        if existing is True:
            prop['missing'] = 'no'
        geom = {'type': 'Point', 'coordinates': [row['lon'], row['lat']]}
        entry = {'type': 'Feature', 'properties': prop, 'geometry': geom}
#        if lessContent:
#            # Add id for maproulette
#            if prop['gml_id']:
#                entry['@id'] = prop['gml_id']
#            elif prop['stellplatz_nr']:
#                entry['@id'] = prop['stellplatz_nr']
#            elif prop['uuid']:
#                entry['@id'] = prop['uuid']
#            elif prop['ident']:
#                entry['@id'] = prop['ident']
#            elif prop['id']:
#                entry['@id'] = prop['id']

        features.append(entry)

    json_result = {'type': 'FeatureCollection', 'features': features}
    return jsonify(json_result)

def render_geojson_nodes(result, valueType):
    features = []
    for row in result:
        prop = {'osm_id': row['osm_id'],
                'requestedField': valueType,
                'requestedValue': row[valueType],
                'popupContent': render_template('node_popup.html', node=row)}
        geom = {'type': 'Point', 'coordinates': [row['lon'], row['lat']]}
        entry = {'type': 'Feature', 'properties': prop, 'geometry': geom}
        features.append(entry)

    json_result = {'type': 'FeatureCollection', 'features': features}
    return jsonify(json_result)


"""
This Module tends to create conference.
"""
from twilio.access_token import AccessToken, ConversationsGrant

from common.dbmanage import DbManage

class Conference():
    """
    # Class Conference tends to manage conference
    """
    def __init__(self):
        # My Master Account Sid and Auth Token from twilio_access.conf
        self.ins_db = DbManage()

    def check_access(self, classid, userid):
        """
        # Select sid from t1, according userid and classid
        """
        try:
            tx = 't1'
            collist = ['subaccountid']
            wherestr = "id='{0}' and classid='{1}' and statusid!={2}".format(
                userid, classid, '3')
            ds_sid = self.ins_db.get_table(tx, collist, wherestr)
            if len(ds_sid) == 1:
                sid = ds_sid[0][0]
                return sid
            else:
                print('classid or userid is not correct')
                return ''
        except ValueError as err:
            print(err)

    def get_accesstoken(self, classid, userid, username):
        """
        # This function builds jwt of access token with username
        """
        # Select sid from t1, according userid and classid
        sid = self.check_access(classid, userid)
        # Select authtoken, apikey, secret fro table t0.
        tx = 't0'
        collist = ['key', 'secret', 'configid']
        wherestrtoken = "id = '{0}'".format(sid)
        infoacc = self.ins_db.get_table(tx, collist, wherestrtoken)

        # Substitute your Twilio AccountSid and ApiKey details
        ACCOUNT_SID = sid
        API_KEY_SID = infoacc[0][0]
        API_KEY_SECRET = infoacc[0][1]
        CONFIGURATION_SID = infoacc[0][2]

        # Create an Access Token
        token = AccessToken(ACCOUNT_SID, API_KEY_SID, API_KEY_SECRET)

        # Set the Identity of this  token
        token.identity = username

        # Grant access to Conversations
        grant = ConversationsGrant()
        grant.configuration_profile_sid = CONFIGURATION_SID
        token.add_grant(grant)
        # Return token info as JSON
        return token


import itertools

def is_prime(num):
    d = 2
    while num % d != 0:
        d += 1
    return d == num

def primes():
    num = 2
    while True:
        if is_prime(num):
            yield num
        num += 1

print(list(itertools.takewhile(lambda x: x <= 31, primes())))
# [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31]


import requests
from django.shortcuts import render
from bs4 import BeautifulSoup
from requests.compat import quote_plus
from .models import Search
# Create your views here.

def home(request):
    return render(request, 'new_search.html')

def new_search(request):
    base_url = 'https://www.ebay.com/sch/i.html?_from=R40&_trksid=m570.l1313&_nkw={}'
    search = request.POST.get('search')
    Search.objects.create(search=search)
    final_url = base_url.format(quote_plus(search))

    response = requests.get(final_url)
    data = response.text
    soup = BeautifulSoup(data, 'html.parser')

    final_post = []

    post_listings = soup.find_all('div', {'class': 's-item__wrapper clearfix'})
    for item in post_listings:
        d = dict()

        d['post_title'] = item.find('a', {'class': 's-item__link'}).text
        d['post_url'] = item.find('a').get('href')
        d['post_image'] = item.find('img').get('src')

        if item.find('span',{'class': 's-item__price'}):
            d['post_price'] = item.find('span',{'class':'s-item__price'}).text
        else:
            d['post_price'] = 'Item does not have a price'

        final_post.append(d)

    return render(request, 'new_search.html', {'search': search, 'final_post':final_post})


#!/usr/bin/env python
import numpy as np
from keras import datasets
from keras.utils import np_utils

print("pkg import")

def data_preprocessing() :
	(_X_train, _Y_train), (_X_test, _Y_test) = datasets.mnist.load_data()
	_Y_train = np_utils.to_categorical(_Y_train)
	_Y_test  = np_utils.to_categorical(_Y_test)
	L, W, H  = _X_train.shape
	_X_train = _X_train.reshape(-1, W*H)
	_X_test  = _X_test.reshape(-1, W*H)
	_X_train = _X_train/255.
	_X_test  = _X_test/255.
	return (_X_train, _Y_train), (_X_test, _Y_test)

from keras import layers, models
class ANN_MDL(models.Sequential) :
	def __init__(self, Nin, Nh, Nout) :
		super().__init__()
		self.add(layers.Dense(Nh, activation='relu', input_shape=(Nin,)))
		self.add(layers.Dense(Nout, activation='softmax'))
		self.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])

def main() :
	Nin = 784
	Nh  = 30
	num_class = 10
	Nout  = num_class
	model = ANN_MDL(Nin, Nh, Nout)
	(X_train, Y_train), (X_test, Y_test) = data_preprocessing()
	history = model.fit(X_train, Y_train, epochs = 30, batch_size = 64, validation_split=0.2)
	performance_test = model.evaluate(X_test, Y_test, batch_size  = 40)
	print('Performance_test:', performance_test)
	model_json = model.to_json()
	with open("model.json", "w") as json_file :
		json_file.write(model_json)
	model.save_weights("model.h5")
	print("Saved model to disk")

if __name__=='__main__' :
	main()


import json
import requests
import pytest
"""Run python manage.py runserver to run the project from terminal.
 and run this file by using 'pytest filename.py' command from another terminal"""

def update_User(id=1002):
    """This function helps to update User if known user Id is passed to this function"""
    data = {
        'User_name': 'Anil',
        'User_id': id,
        'User_email': 'anil.aanil@gmail.com',
    }
    resp = requests.put('http://127.0.0.1:8000/User_basic/api/', data=json.dumps(data))
    print(resp.json())

# update_User(1002)

def test_update_User(id=1002):
    """This function should pass because a known id is passed and it will get updated"""
    data = {
        'User_name': 'Anil',
        'User_id': id,
        'User_email': 'anil.aanil@gmail.com',
    }
    resp = requests.put('http://127.0.0.1:8000/User_basic/api/', data=json.dumps(data))
    assert resp.status_code == 200

def update_unknown_User(id=1100):
    """This function will not able to update user ID because it is not present in DB"""
    data = {
        'User_email': 'xxx@gmail.com',
        'user_id': id,
    }
    resp = requests.put('http://127.0.0.1:8000/student_basic/api/', data=json.dumps(data))
    print(resp.json())

# update_unknown_User()

def test_update_unknown_User(id=1100):
    """This function should fail as the id is not in DB. It won't get updated"""
    data = {
        'User_email': 'xxx@gmail.com',
        'user_id': id,
    }
    resp = requests.put('http://127.0.0.1:8000/student_basic/api/', data=json.dumps(data))
    assert resp.status_code == 200


"""sundayfunday URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/1.9/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  url(r'^$', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  url(r'^$', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.conf.urls import url, include
    2. Add a URL to urlpatterns:  url(r'^blog/', include('blog.urls'))
"""
from django.conf.urls import url
from django.contrib import admin

from sundayfunday.views import index
from sundayfunday.views import user
from sundayfunday.views import register
from sundayfunday.views import event

#admin.autodiscover()

urlpatterns = [
    url(r'^$', index.UserHomePageView.as_view(), name='home'),
    url(r'^login/$', user.LoginView.as_view(), name='login'),
    url(r'^logout/$', user.LogoutView.as_view(), name='logout'),
    url(r'^register/', register.RegisterUserView.as_view(), name='register'),
    url(r'^addevent/', event.AddEventView.as_view(), name='addevent'),
    url(r'^event/(?P<pk>[0-9]+)/$', event.EventDetailView.as_view(),
        name='event-detail'),
    url(r'^edit/(?P<pk>[0-9]+)/$', user.UserEditView.as_view(),
        name='user-edit'),
    url(r'^edit-event/(?P<pk>[0-9]+)/$', event.EventEditView.as_view(),
        name='edit-event'),
    url(r'^see-upcoming/(?P<pk>[0-9]+)/$', event.SeeUpcomingEventsView.as_view(),
        name='see-upcoming'),
    url(r'^admin/', admin.site.urls),
    url(r'^u/$', index.UserHomePageView.as_view(), name='user-homepage')
]


import dbutils
import os

def joinTableOpener(UserQuery, workingDB):
  joinType = 0
  exitFlag=0
  if ('LEFT OUTER JOIN' in UserQuery.upper()):
    joinType = 1

  # Removing unnecessary words from command
  selLower = dbutils.inputCleaner("SELECT * FROM ", UserQuery)
  selection = dbutils.inputCleaner("select * from ", selLower)
  selection = selection.replace("inner join", "").replace("left outer join", "")
  commandWords = selection.replace(",","").split()

  # Grabbing values from commands
  table1Name = commandWords[0]
  table2Name = commandWords[2]
  comparisonOperator = dbutils.getOperand(commandWords[6])

  # Importing tables into lists
  Table_1 = []
  Table_2 = []
  Table_Join = []
  TableListNames = [Table_1, Table_2]
  ActualTableNames = [table1Name, table2Name]
  if workingDB != None:
    for x in range(0,2):
      if dbutils.tableExistenceCheck(ActualTableNames[x], workingDB):
        f = open(f'{workingDB}/{ActualTableNames[x]}.txt', 'r')
        for line in f:
          TableListNames[x].append(line) #Turning tables into list of lines
        f.close()
      else:
        print(f"Could not query table {ActualTableNames[x]} because it does not exist.")
        exitFlag=1
  else:
    print("Please specify which database to use.")

  if (exitFlag==0):

    # Finding the index of columns to search
    table1Column = Table_1[0].index(commandWords[5].split(".")[1])
    table2Column = Table_2[0].index(commandWords[7].split(".")[1])

    # Performs comparisons on given data
    def joinOperandFunction(t1, t2):
      if (comparisonOperator == 0): #Equality
        if (type(Table_2[t2].split("|")[table2Column]) is str):
          if (Table_2[t2].split("|")[table2Column] == Table_1[t1].split("|")[table1Column]):
            Table_Join.append(f'{Table_1[t1]} | {Table_2[t2]}')
        else:
          if (float(Table_2[t2].split("|")[table2Column]) == float(Table_1[t1].split("|")[table1Column])):
            Table_Join.append(f'{Table_1[t1]} | {Table_2[t2]}')
      elif (comparisonOperator == 1): #Greater than
        if (Table_2[t2].split("|")[table2Column] > Table_1[t1].split("|")[table1Column]):
          Table_Join.append(f'{Table_1[t1]} | {Table_2[t2]}')
      elif (comparisonOperator == -1): #Less than
        if (Table_2[t2].split("|")[table2Column] < Table_1[t1].split("|")[table1Column]):
          Table_Join.append(f'{Table_1[t1]} | {Table_2[t2]}')
      elif (comparisonOperator == -3): #Inequality
        if (Table_2[t2].split("|")[table2Column] != Table_1[t1].split("|")[table1Column]):
          Table_Join.append(f'{Table_1[t1]} | {Table_2[t2]}')

    # Join function. Nested for loops to iterate through tables.
    def join():
      Table_1[0] = Table_1[0].rstrip('\n')
      Table_2[0] = Table_2[0].rstrip('\n')
      Table_Join.append(f"{Table_1[0]} | {Table_2[0]}")
      for t1 in range(1, len(Table_1)):
        Table_1[t1] = Table_1[t1].rstrip("\n")
        for t2 in range(1, len(Table_2)):
          Table_2[t2] = Table_2[t2].rstrip('\n')
          joinOperandFunction(t1, t2)
        # Left outer join. If program cannot find Table 1 value in the joined Table, we add it with null info for Table 2's values.
        if (joinType == 1):
          if (Table_1[t1].split("|")[table1Column] not in Table_Join[-1].split("|")[table1Column]):
            Table_Join.append(f"{Table_1[t1]} | |")
      for myTuple in Table_Join:
        print(myTuple)

    join()


import logging
from flask import json
from requests.auth import HTTPBasicAuth
import requests
from util import get_site_id_by_name, APIError

def create_appliance(api_auth, parameters, contexts):
    """
    Allows users to create an appliance on a site.
    In order for them to do so, we need to know the city, site name
    model and country code.

    Works by checking if the site exists. If it exists, it gets the parameters, and with the
    parameters, it calls the SteelConnectAPI, and creates the appliance

    Parameters:
    - api_auth: SteelConnect API object, it contains authentication log in details
    - parameters: The json parameters obtained from the Dialogflow Intent. It obtains the following:
        > city: In which city the site is located in
        > country_code: The country code of the country where the site is located
        > model: The model of the appliance the user wants to create
        > site_name: the name of the site the user wants to place the appliance on

    Returns:
    - speech: A string which has the response to be read/printed to the user

    Example Prompt:
    - Make an ewok appliance for charmeleon, in Darwin, Australia

    """
    try:
        city = parameters["City"]
        site_name = parameters["SiteName"]
        model = parameters["Model"]
        country_code = parameters["Country"]["alpha-2"]
        country_name = parameters["Country"]["name"]

    except KeyError as e:
        error_string = "Error processing create Appliance intent. {0}".format(e)
        logging.error(error_string)
        return error_string

    # Get all sites and check whether site exists
    # Currently, if site doesn't exist it will let the user know that the site doesn't exist
    try:
        site_id = get_site_id_by_name(api_auth, site_name, city,country_code)
    except APIError as E:
        return str(E)

    if site_id:         #If we find a site that exists
        # Call create_appliance in SteelConnectAPI
        res = api_auth.node.create_appliance(site=site_id, model=model)

        if res.status_code == 200:          #if successful
            speech = "The {} appliance was created for the {} site in {}, {}".format(model, site_name, city, country_name)
        elif res.status_code == 400:
            speech = "Invalid parameters: {}".format(res.json()["error"]["message"])
        elif res.status_code == 404:
            speech = "Error: Organization with given id does not exist"
        elif res.status_code == 500:        #If we couldn't create the appliance
            speech = "Error: We could not create the {} appliance for the {} site in {}, {}".format(model, site_name, city, country_name)
        else:
            speech = "Error: Could not connect to SteelConnect"

        logging.debug(speech)
    else:
        speech = "The site {} does not exist in {}, {}. No appliances were created.".format(site_name, city,country_name)
    return speech


from PySide6 import Qt
from PySide6.QtCore import *
from PySide6.QtGui import *
from PySide6.QtWidgets import *
import sys
import sqlite3
from sqlite3 import Error
from datetime import date
from reservation_page import Ui_window

class Reservation(QMainWindow):
    def __init__(self):
        super().__init__()
        self.ui = Ui_window()
        self.ui.setupUi(self)
        self.dbConnection = None
        self.initDBConnection()
        #list of information
        self.info =[]
        #check wheter user click display before confirm
        self.dis = False

        #combo box setup
        branches = ["rama 3", "Sathorn", "Ladkrabang", "Rangsit", "Siam"]
        for b in branches:
                self.ui.serviceBranch_comboBox.addItem(b)

        #model list setup
        self.showList()
        self.ui.display_button.clicked.connect(self.display)
        self.ui.confirm_button.clicked.connect(self.confirm)

    def initDBConnection(self):
        try:
            self.dbConnection = sqlite3.connect("/Users/nnevermine/Desktop/carRental.db")
            print("connected to database")
        except Error as e:
            print(e)

        return self.dbConnection

    def showList(self):
        result = self.dbConnection.cursor().execute("""SELECT carModel,available FROM carCatalog""")
        for row_no, row_data in enumerate(result):
            #show only the available car
            if(row_data[1] > 0):
                item = QListWidgetItem(row_data[0])
                self.ui.listWidget.addItem(item)
    def display(self):
        #mark that user has clicked the display button before confirm
        self.dis = True

        #car model
        model = str(self.ui.listWidget.currentItem().text())
        self.ui.model_lineEdit.setText(model)

        #price per hour
        result = self.dbConnection.cursor().execute("""SELECT car_id, pph FROM carCatalog WHERE carModel=?""",(model,))
        for row_no, row_data in enumerate(result):
            car_id = row_data[0]
            pph = row_data[1]
        self.ui.pph_lineEdit.setText(str(pph) + " baht")

        #interval
        startD = int(self.ui.pickup_dateEdit.date().day())
        startM = int(self.ui.pickup_dateEdit.date().month())
        startY = int(self.ui.pickup_dateEdit.date().year())
        start = str(startD) + "/" + str(startM) + "/" + str(startY)

        endD = int(self.ui.return_dateEdit.date().day())
        endM = int(self.ui.return_dateEdit.date().month())
        endY = int(self.ui.return_dateEdit.date().year())
        end = str(endD) + "/" + str(endM) + "/" + str(endY)

        interval = str(start) + " to " + str(end)
        self.ui.interval_lineEdit.setText(interval)

        #no. of days
        s_date = date(startY, startM, startD)
        e_date = date(endY, endM, endD)
        delta = e_date - s_date
        days = delta.days
        #return same date
        if days == 0:
            days = 1
        self.ui.days_lineEdit.setText(str(days))

        #service branch
        branch = self.ui.serviceBranch_comboBox.currentText()
        self.ui.branch_lineEdit.setText(branch)

        #price
        price = pph * days
        self.ui.price_lineEdit.setText(str(price) + " baht")

        #username
        name = self.ui.name_lineEdit.text()

        #time
        pickup_time = self.ui.pickup_timeEdit.time().toString()
        return_time = self.ui.return_timeEdit.time().toString()

        #gather all information(username, carmodel, interval, pick-up time, return time, branch, total income)
        self.info = [name,car_id,model,interval,pickup_time,return_time,branch,price]

    def confirm(self):
        #has to click display before confirm
        if self.dis == True:
            #update car avilability
            result = self.dbConnection.cursor().execute("""UPDATE carCatalog SET available = 0 WHERE car_id = ?""",(self.info[1],))
            self.dbConnection.commit()
            item = self.ui.listWidget.currentItem()
            self.ui.listWidget.takeItem(self.ui.listWidget.row(item))

            #insert data into the database
            adminTable = self.dbConnection.cursor().execute("""INSERT INTO admin(username, carId, carModel, interval, pickupTime, returnTime, branch, income)VALUES(?,?,?,?,?,?,?,?)""", tuple(self.info))
            self.dbConnection.commit()

            #insert data to userHistory
            adminTable = self.dbConnection.cursor().execute("""INSERT INTO userHistory(username, carId, carModel, interval, pickupTime, returnTime, branch, price)VALUES(?,?,?,?,?,?,?,?)""", tuple(self.info))
            self.dbConnection.commit()

            #clear lineEdits ready for other reservation
            self.ui.model_lineEdit.setText("")
            self.ui.pph_lineEdit.setText("")
            self.ui.interval_lineEdit.setText("")
            self.ui.days_lineEdit.setText("")
            self.ui.branch_lineEdit.setText("")
            self.ui.price_lineEdit.setText("")
            self.dis = False

            dialog = QMessageBox()
            dialog.setText("Reservation successful")
            dialog.exec_()


import librosa
import os
import numpy as np

path = "./"
items = 0
solves = []

def formatSol(id):
  tmp = [0,0,0,0]
  if id > 4:
    raise ValueError ("Wrong label!")
  else:
    tmp[id - 1] += 1
  solves.append(tmp)

with open(path + "music/solve.txt", "r") as indiv:
  for each in indiv:
    tmp = False
    solId = 0
    point = 0
    for i in each:
      if i == " " or i == "\n":
        pass
      elif i == ":":
        tmp = True
      elif tmp:
        solId *= 10
        solId += int(i)
      else:
        point *= 10
        point += int(i)
    formatSol(solId)

mfccdata = []

def setmfcc(id):
  audiopath = path + "music/" + str(id) + ".mp3"
  audio, sr = librosa.load(audiopath)
  mfccdata.append(librosa.feature.mfcc(audio))

for i in range(300):
  setmfcc(i + 1)
  print(i + 1)

resq = open(path + "res-q.txt", "w")
for each in mfccdata:
  resq.writelines(np.array_str(each) + "\n")
resq.close()

print(mfccdata[0].shape)


import cv2
import numpy as np
import matplotlib.pyplot as plt
import glob
import os

data_dir = 'D:\\img'
all_image_data_path = sorted(glob.glob(os.path.join(data_dir,"*")))
total_data = list(all_image_data_path)
data_length = len(total_data)
print(data_length)
for i in range(1, 65, 4):
    img1 = cv2.imread(total_data[i], 1)
    img2 = cv2.imread(total_data[i+1], 1)
    img3 = cv2.imread(total_data[i+2], 1)
    img4 = cv2.imread(total_data[i+3], 1)

    img1 = cv2.resize(img1, (480,282))
    img2 = cv2.resize(img2, (480,282))
    img3 = cv2.resize(img3, (480,282))
    img4 = cv2.resize(img4, (480,282))

    addh1 = cv2.hconcat([img1, img2])
    addh2 = cv2.hconcat([img3, img4])
    addv = cv2.vconcat([addh1, addh2])

    cv2.imwrite('D:/img/{}.jpg'.format(i), addv)
    i = 4*i+1

    # cv2.imshow('imgv',addv)
    # cv2.waitKey(0)
    # cv2.destroyAllWindows()


from collections import ChainMap

from defaultGeneration import DefaultValuesWrapper, DEFAULTS_VALUES_GENERATORS
from excelInputMappingRules import EXCEL_INPUT_MAPPING_RULES
from inputMappings import NonStrictInputMapping
from models.co2model import Co2Model
from models.erosionmodel import ErosionModel
from models.fertilisermodel import FertModel
from models.hmmodel import HmModel
from models.irrigationmodel import IrrigationModel
from models.lucmodel import LUCModel
from models.manuremodel import ManureModel
from models.nmodel import NModel
from models.otherorganicfertilisermodel import OtherOrganicFertModel
from models.packmodel import PackModel
from models.pmodel import PModel
from models.seedmodel import SeedModel
from outputMapping import OutputMapping

class ModelsSequence(object):
    def __init__(self, validatedInputs):
        self._intermediateValues = {}
        self.outputMapping = OutputMapping()
        self.allInputs = DefaultValuesWrapper(ChainMap(NonStrictInputMapping(validatedInputs, EXCEL_INPUT_MAPPING_RULES),
                                              self._intermediateValues, self.outputMapping.output), DEFAULTS_VALUES_GENERATORS)

    def executeSequence(self):
        self.outputMapping.mapAsIsOutput(self.allInputs)
        self.outputMapping.mapDictsToOutput(self.allInputs)
        self._computeFertiliser()
        self._computeManure()
        self._computeOtherOrganicFertiliser()
        self._computeSeed()
        self.outputMapping.mapSeeds(self.allInputs)
        if self.allInputs["cultivation_type"] != "open_ground":
            self._intermediateValues["eroded_soil"] = 0.0
        else:
            self._intermediateValues["eroded_soil"] = ErosionModel(self.allInputs).compute()["m_Erosion_eroded_soil"]
        self.outputMapping.mapIrrigationModel(IrrigationModel(self.allInputs).compute())
        self.outputMapping.mapIrrigationQuantities(self.allInputs)
        self.outputMapping.mapFertilizers(self.allInputs)
        self.outputMapping.mapOtherOrganicFertilizers(self.allInputs)
        self.outputMapping.mapCo2Model(Co2Model(self.allInputs).compute())
        self.outputMapping.mapNModel(NModel(self.allInputs).compute(), self.allInputs)
        self.outputMapping.mapPModel(PModel(self.allInputs).compute(), self.allInputs)
        self.outputMapping.mapHMModel(HmModel(self.allInputs).compute(), self.allInputs)
        self.outputMapping.mapPackModel(PackModel(self.allInputs).compute())
        self.outputMapping.mapLucModel(LUCModel(self.allInputs).compute(), self.allInputs)
        #self.outputMapping.mapUsedIntermidiateValues(self._intermediateValues)
        self.outputMapping.mapMachineries(self.allInputs)
        self.outputMapping.mapCODWasteWater(self.allInputs)
        self.outputMapping.mapPesticides(self.allInputs)
        return self.outputMapping.output;

    def _computeFertiliser(self):
        fertM = FertModel(self.allInputs)
        self._intermediateValues["ammonia_due_to_mineral_fert"] = fertM.computeNH3()
        self._intermediateValues["hm_from_mineral_fert"] = fertM.computeHeavyMetal()

    def _computeManure(self):
        manureM = ManureModel(self.allInputs)
        self._intermediateValues["nitrogen_from_all_manure"] = manureM.computeN()
        pres = manureM.computeP2O5()
        self._intermediateValues["p2o5_in_liquid_manure"] = pres[0]
        self._intermediateValues["p2o5_in_solid_manure"] = pres[1]
        self._intermediateValues["hm_from_manure"] = manureM.computeHeavyMetal();
        self._intermediateValues["ammonia_due_to_manure"] = manureM.computeNH3()

    def _computeOtherOrganicFertiliser(self):
        otherfertM = OtherOrganicFertModel(self.allInputs)
        self._intermediateValues["nitrogen_from_other_orga_fert"] = otherfertM.computeN()
        self._intermediateValues["p2o5_in_liquid_sludge"] = otherfertM.computeP2O5()
        self._intermediateValues["hm_from_other_organic_fert"] = otherfertM.computeHeavyMetal()
        self._intermediateValues["ammonia_due_to_other_orga_fert"] = otherfertM.computeNH3()

    def _computeSeed(self):
        seedM = SeedModel(self.allInputs)
        self._intermediateValues["hm_from_seed"] = seedM.computeHeavyMetal()


import sqlite3

# Create connection
connection = sqlite3.connect('movies.db')

# Create cursor
cursor = connection.cursor()

# Create a table
cursor.execute(
    '''
    CREATE TABLE IF NOT EXISTS Movies (
        Title TEXT,
        Director TEXT,
        Year INT
    )
    '''
)

connection.commit()
connection.close()


"""
Post processing on detected objects
"""
import pymongo
from pymongo import MongoClient
import time
import logging
logging.basicConfig(format='%(levelname)s :: %(asctime)s :: %(message)s', level=logging.DEBUG)
from joblib import Parallel, delayed
import click
from xgboost_model.inference import run_inference, PostprocessException
import os

def load_detected_pages(db, buffer_size):
    """
    """
    current_docs = []
    for doc in db.propose_pages.find({'postprocess': None, 'ocr': True}, no_cursor_timeout=True):
        current_docs.append(doc)
        if len(current_docs) == buffer_size:
            yield current_docs
            current_docs = []
    yield current_docs

def do_skip(page, client):
    db = client.pdfs
    coll = db.postprocess_pages
    return coll.count_documents({'pdf_name': page['pdf_name'], 'page_num': page['page_num']}, limit=1) != 0

def postprocess(db_insert_fn, num_processes, weights_pth, skip):
    logging.info('Starting post-processing over detected objects')
    start_time = time.time()
    client = MongoClient(os.environ["DBCONNECT"])
    logging.info(f'Connected to client: {client}.')
    db = client.pdfs
    for batch in load_detected_pages(db, 100):
        logging.info('Loaded next batch. Running postprocessing')
        try:
            pages = Parallel(n_jobs=num_processes)(delayed(run_inference)(page, weights_pth) for page in batch)
        except PostprocessException as e:
            logging.error(f'Postprocessing error in referenced page: {e.page}')
            logging.error(f'Original Exception: {e.original_exception}')
            continue

        db_insert_fn(pages, client)
    end_time = time.time()
    logging.info(f'Exiting post-processing. Time up: {end_time - start_time}')

def mongo_insert_fn(objs, client):
    db = client.pdfs
    for obj in objs:
        try:
            result = db.propose_pages.update_one({'_id': obj['_id']},
                                             {'$set':
                                                {
                                                    'pp_detected_objs': obj['pp_detected_objs'],
                                                    'postprocess': True
                                                }
                                             }, upsert=False)
            logging.info(f'Updated result: {result}')
        except pymongo.errors.WriterError as e:
            logging.error(f'Document write error: {e}\n Document id: obj["_id"]')

@click.command()
@click.argument("num_processes")
@click.argument("weights_pth")
@click.option('--skip/--no-skip')
def click_wrapper(num_processes, weights_pth, skip):
	postprocess(mongo_insert_fn, int(num_processes), weights_pth, skip)

if __name__ == '__main__':
	click_wrapper()


import os
import sys
import shutil
import numpy as np
import scipy.sparse as sp
import pickle
import copy
from operator import itemgetter
from sklearn import metrics
import torch
from typing import List, Union
import torch.utils.data

### mkdir
def mkdir(path):
    if os.path.exists(path):
        shutil.rmtree(path)
    if not os.path.exists(path):
        os.makedirs(path)

### sparse_to_tuple
def sparse_to_tuple(sparse_mx):
    if not sp.isspmatrix_coo(sparse_mx):
        sparse_mx = sparse_mx.tocoo()
    coords = np.vstack((sparse_mx.row, sparse_mx.col)).transpose()
    values = sparse_mx.data
    shape = sparse_mx.shape
    return coords, values, shape

### dropout_sparse
def dropout_sparse(x, dropout, noise_shape):
    i = x._indices()
    v = x._values()

    random_tensor = 1 - dropout
    random_tensor = random_tensor + torch.rand(noise_shape)
    dropout_mask = torch.floor(random_tensor).type(torch.bool)

    i = i[:, dropout_mask]
    v = v[dropout_mask]

    out = torch.sparse.FloatTensor(i, v, x.shape)
    out = out * (1. / (1 - dropout))

    return out

### scipy_sparse_to_torch_sparse
def scipy_sparse_to_torch_sparse(sparse_mx):
    sparse_mx = sparse_mx.tocoo()
    values = sparse_mx.data
    indices = np.vstack((sparse_mx.row, sparse_mx.col))
    i = torch.LongTensor(indices)
    v = torch.FloatTensor(values)
    shape = sparse_mx.shape
    x = torch.sparse.FloatTensor(i, v, torch.Size(shape))
    return x

### fixed_unigram_candidate_sampler
### https://github.com/YuutoOhnuki/decagon-pytorch/blob/master/decagon-pytorch/src/decagon_pytorch/sampling.py
def fixed_unigram_candidate_sampler(
    true_classes: Union[np.array, torch.Tensor],
    num_samples: int,
    unigrams: List[Union[int, float]],
    distortion: float = 1.):

    if isinstance(true_classes, torch.Tensor):
        true_classes = true_classes.numpy()
    if true_classes.shape[0] != num_samples:
        raise ValueError('true_classes must be a 2D matrix with shape (num_samples, num_true)')
    unigrams = np.array(unigrams)

    if distortion != 1.:
        unigrams = unigrams.astype(np.float64) ** distortion
    indices = np.arange(num_samples)
    result = np.zeros(num_samples, dtype=np.int64)
    tmp = indices
    while len(tmp) > 0:
        sampler = torch.utils.data.WeightedRandomSampler(unigrams, len(indices))
        candidates = np.array(list(sampler))
        candidates = np.reshape(candidates, (len(indices), 1))
        result[indices] = candidates.T
        mask = (candidates == true_classes[indices, :])
        mask = mask.sum(1).astype(np.bool)
        tmp = indices[mask]
    return result

# ###  hinge_loss
# def hinge_loss(pos, neg, margin):
#     """Maximum-margin optimization using the hinge loss."""
#     diff = torch.relu(torch.sub(neg, pos - margin))
#     loss = torch.sum(diff)
#     return loss

###  test
def test(prefix, epo_idx, net, dp, args, log, disease):

    net.eval()
    dp.is_train = False
    dp.dropout = 0.  # disable dropout for val

    edges = pickle.load(open(args.edg_pkl_path + "{}_edges.pkl".format(prefix), "rb"))
    false_edges = pickle.load(open(args.edg_pkl_path + "{}_false_edges.pkl".format(prefix), "rb"))

    auroc_list = []
    auprc_list = []
    apk_list = []
    auroc_avg_list = []
    auprc_avg_list = []
    apk_avg_list = []
    rel_type_tmp = []
    rel_type_list = []

    print_data = []
    #for rel_type, edge_lists in edges.items():
    rel_type = (0,1)
    edge_lists = edges[rel_type]
        # rel_type_tmp.append(rel_type)  # [(0, 1)] ; [(0, 1), (1, 0)]
        # rel_type_list.append(copy.deepcopy(rel_type_tmp))  # [[(0, 1)], [(0, 1), (1, 0)]]

    i, j = rel_type
    for rt_k, edge_list in enumerate(edge_lists):
        ### pos & neg edges
        pos_edge_list = edge_list
        neg_edge_list = false_edges[rel_type][rt_k]

        ### preds result
        pos_preds_all = []
        neg_preds_all = []

        ### test per batch or in one time
        if args.te_batch:
            """
            test the data per batch
            """
            ### edges number
            n_edges = len(pos_edge_list)
            n_batchs = n_edges // args.te_batch_size
            bt_idx = 0
            while True:
                ### start, end
                bt_s = bt_idx * args.te_batch_size
                bt_e = bt_s + args.te_batch_size
                if bt_s < n_edges and bt_e >= n_edges:
                    bt_e = n_edges
                ### pos edges
                pos_edges_array = np.array(pos_edge_list[bt_s:bt_e])
                pos_edges_tensor = torch.from_numpy(pos_edges_array)
                pos_edges_tensor = pos_edges_tensor.to(args.device)
                pos_inputs = ((i, j), rt_k, pos_edges_tensor)
                pos_preds, _ = net(pos_inputs)
                pos_preds = torch.sigmoid(pos_preds)
                pos_preds = pos_preds.detach().cpu().numpy().tolist()
                pos_preds_all += pos_preds
                ### neg edges
                # neg_edges_array = np.array(neg_edge_list[bt_s:bt_e])
                # neg_edges_tensor = torch.from_numpy(neg_edges_array)
                # neg_edges_tensor = neg_edges_tensor.to(args.device)
                # neg_inputs = ((i, j), rt_k, neg_edges_tensor)
                # neg_preds, _ = net(neg_inputs)
                # neg_preds = torch.sigmoid(neg_preds)
                # neg_preds = neg_preds.detach().cpu().numpy().tolist()
                # neg_preds_all += neg_preds

                ### bt_idx ++
                bt_idx += 1
                ### break
                if bt_idx > n_batchs:
                    break
        else:
            """
            test all data in one time
            """
            ### pos edges
            pos_edges_array = np.array(pos_edge_list)
            pos_edges_tensor = torch.from_numpy(pos_edges_array)
            pos_edges_tensor = pos_edges_tensor.to(args.device)
            pos_inputs = ((i, j), rt_k, pos_edges_tensor)
            pos_preds, _ = net(pos_inputs)
            pos_preds = torch.sigmoid(pos_preds)
            pos_preds_all = pos_preds.detach().cpu().numpy().tolist()
            ### neg edges
            neg_edges_array = np.array(neg_edge_list)
            neg_edges_tensor = torch.from_numpy(neg_edges_array)
            neg_edges_tensor = neg_edges_tensor.to(args.device)
            neg_inputs = ((i, j), rt_k, neg_edges_tensor)
            neg_preds, _ = net(neg_inputs)
            neg_preds = torch.sigmoid(neg_preds)
            neg_preds_all = neg_preds.detach().cpu().numpy().tolist()

        ### all
        # preds_all = np.hstack([pos_preds_all, neg_preds_all])
        # preds_all = np.nan_to_num(preds_all)
        # labels_all = np.hstack([np.ones(len(pos_preds_all)), np.zeros(len(neg_preds_all))])

        # actual = list(range(len(pos_preds_all)))
        # predicted = []
        # for idx, score in enumerate(preds_all):
        #     predicted.append((score, idx))
#######################################################################################################
        print_data_rtk = []
        for idx, score in enumerate(pos_preds_all):
            data = [(rel_type, rt_k), edge_list[idx], pos_preds_all[idx]]
            print_data_rtk.append(data)
        print_data.append(print_data_rtk)
#######################################################################################################
    #     predicted = list(zip(*sorted(predicted, reverse=True, key=itemgetter(0))))[1]

    #     ### roc, auprc, apk
    #     auroc = metrics.roc_auc_score(labels_all, preds_all)
    #     auprc = metrics.average_precision_score(labels_all, preds_all)
    #     apk = compute_apk(actual, predicted, k=args.ap_k)
        # log.info("[{}] | Epoch: [{}/{}] | Type: [{},{},{}] |"
        #          " AUROC: {:.5f} | AUPRC: {:.5F} | AP@{}: {:.5f}".format(
        #     prefix, epo_idx + 1, args.epoch, i, j, rt_k,
        #     auroc, auprc, args.ap_k, apk))
        log.info("[{}] | Epoch: [{}/{}] | Type: [{},{},{}] ".format(prefix, epo_idx + 1, args.epoch, i, j, rt_k))

    #     ### append
    #     auroc_list.append(auroc)
    #     auprc_list.append(auprc)
    #     apk_list.append(apk)

    # ### avg
    # auroc_avg = np.mean(auroc_list)
    # auprc_avg = np.mean(auprc_list)
    # apk_avg = np.mean(apk_list)
    # auroc_avg_list.append(auroc_avg)
    # auprc_avg_list.append(auprc_avg)
    # apk_avg_list.append(apk_avg)
#######################################################################################################
    # pickle.dump(print_data, open("../../out/predict_results.pkl", "wb"))
    dis = ['all', 'covid', 'cn', 'nsclc', 'escc']
    with open("../out/predict_results_{}.txt".format(dis[disease]), "w") as presults:
        for lines in print_data: #print_data_rtk
            for line in lines: #data
                rel_type = line[0][0]
                rel_type_k = str(line[0])
                cnum = line[1][0]
                gnum = line[1][1]
                score = line[2]
                presults.write("{}\t{}\t{}\t{}\n".format(rel_type_k, cnum, gnum, score))
#######################################################################################################
    # for rel_type, auroc_avg, auprc_avg, apk_avg in zip(rel_type_list, auroc_avg_list, auprc_avg_list, apk_avg_list):

    #     log.info("[{}] | Epoch: [{}/{}] | all >>> type: {} |"
    #              " avg AUROC: {:.5f} | avg AUPRC: {:.5F} | avg AP@{}: {:.5f}".format(
    #         prefix, epo_idx + 1, args.epoch, rel_type,
    #         auroc_avg, auprc_avg, args.ap_k, apk_avg))

    # return auroc_avg_list[0], auprc_avg_list[0], apk_avg_list[0]
    log.info("[{}] | Epoch: [{}/{}]".format(prefix, epo_idx + 1, args.epoch, rel_type))

### apk
def compute_apk(actual, predicted, k=10):
    """
    Computes the average precision at k.

    This function computes the average precision at k between two lists of
    items.

    Parameters
    ----------
    actual : list
             A list of elements that are to be predicted (order doesn't matter)
    predicted : list
                A list of predicted elements (order does matter)
    k : int, optional
        The maximum number of predicted elements

    Returns
    -------
    score : double
            The average precision at k over the input lists

    """
    if len(predicted)>k:
        predicted = predicted[:k]

    score = 0.0
    num_hits = 0.0

    for i, p in enumerate(predicted):
        if p in actual and p not in predicted[:i]:
            num_hits += 1.0
            score += num_hits / (i + 1.0)

    if not actual:
        return 0.0

    return score / min(len(actual), k)

### mapk
def compute_mapk(actual, predicted, k=10):
    """
    Computes the mean average precision at k.

    This function computes the mean average precision at k between two lists
    of lists of items.

    Parameters
    ----------
    actual : list
             A list of lists of elements that are to be predicted
             (order doesn't matter in the lists)
    predicted : list
                A list of lists of predicted elements
                (order matters in the lists)
    k : int, optional
        The maximum number of predicted elements

    Returns
    -------
    score : double
            The mean average precision at k over the input lists

    """
    return np.mean([compute_apk(a,p,k) for a, p in zip(actual, predicted)])


from apps.clients import client_login
from django.db import connection
from django.core.cache import cache
from apps.dashboard import dictfetchall
from django.shortcuts import render_to_response
from django.template import RequestContext, loader, Context
from django.http import HttpResponseRedirect
from django.shortcuts import get_object_or_404
from django.db.models import Q
from django.db import IntegrityError
from django.contrib import messages
from django.contrib.auth import authenticate, login as auth_login, logout as logout_login
from forms import MailCreateForm, SMSCreateForm, SmileForm
from django.http import HttpResponse
from django.views.decorators.csrf import csrf_protect
from django.forms.models import modelformset_factory, formset_factory
from custom_user.models import EmailUser
from apps.ladies.models import Lady
from apps.clients.models import Client
from apps.mails.models import Mail
from . import sendMail
from apps.dashboard.forms import EmailUserForm
from apps.dashboard import custom_pagination
from django.core.exceptions import PermissionDenied
from apps.workers import get_groups
from apps.dashboard.middleware.http import Http403
from apps.workers.models import Worker
import datetime
import json

@client_login
def send_mail(request):
    form = MailCreateForm(request.POST or None)
    to = request.GET.get('to', '')
    subject = request.POST.get('subject', '')
    content = request.POST.get('content', '')
    payment = request.GET.get('payment', '')
    client = get_object_or_404(Client, user=request.user)

    try:
        lady = Lady.objects.get(pk=to)
    except Exception:
        lady = None

    if request.method == 'POST':
        if form.is_valid():
            if lady is not None:
                if payment == 'free':
                    result = sendMail(subject=subject, content=content, fr=client, to=lady, free=True)
                else:
                    result = sendMail(subject=subject, content=content, fr=client, to=lady)
                message = result['message']
                messages.add_message(request, messages.SUCCESS, message)
                return HttpResponseRedirect('/ladies')
            else:
                message = 'Lady error!'
                messages.add_message(request, messages.ERROR, message)

    return render_to_response('send_mail.html', locals(), context_instance=RequestContext(request))

@client_login
def send_smile(request):
    to = request.GET.get('to', '')
    client = get_object_or_404(Client, user=request.user)
    subject = 'Smile'
    form = SmileForm(request.POST or None)

    try:
        lady = Lady.objects.get(pk=to)
    except Exception:
        lady = None
    if request.method == 'POST':
        if form.is_valid():
            if lady is not None:
                smile = request.POST.get('smile', '')
                content = '

' % (smile, smile,)
                new_message = Mail(subject=subject, fr=client.id, to=to, content=content, type='smile', type_send='client')
                new_message.save()
                message = 'Smile send!'
                messages.add_message(request, messages.SUCCESS, message)
                return HttpResponseRedirect('/ladies')
            else:
                message = 'Lady error!'
                messages.add_message(request, messages.ERROR, message)

    return render_to_response('send_smile.html', locals(), context_instance=RequestContext(request))

@client_login
def send_sms(request):
    form = SMSCreateForm(request.POST or None)
    subject = 'SMS'
    to = request.GET.get('to', '')
    content = request.POST.get('content', '')
    client = get_object_or_404(Client, user=request.user)

    try:
        lady = Lady.objects.get(pk=to)
    except Exception:
        lady = None

    if request.method == 'POST':
        if form.is_valid():
            if lady is not None:
                new_sms = Mail(subject=subject, fr=client.id, to=to, content=content, type='sms', type_send='client')
                if client.sms_credits < 1:
                    new_sms.active = False
                    new_sms.save()
                    message = 'Error! Not SMS credits'
                    messages.add_message(request, messages.ERROR, message)
                else:
                    client.sms_credits -= 1
                    client.save()
                    new_sms.active = True
                    new_sms.save()
                    message = 'SMS send'
                    messages.add_message(request, messages.ERROR, message)
                    return HttpResponseRedirect('/ladies')
            else:
                message = 'Lady error!'
                messages.add_message(request, messages.ERROR, message)

    return render_to_response('send_sms.html', locals(), context_instance=RequestContext(request))

@client_login
@csrf_protect
def load_inbox(request):
    data = dict()
    if request.is_ajax():
        client_id = int(request.POST.get('client_id', 0))
        count_to_page = int(request.POST.get('per_page', 15))
        page = int(request.POST.get('page', 1))
        sort = int(request.POST.get('sort', 1))
        type_load = str(request.POST.get('type', 'inbox'))

        if client_id:
            if sort == 1:
                sort_q = 'ORDER BY `date_send` DESC'
            elif sort == 2:
                sort_q = 'ORDER BY `first_name` ASC'
            elif sort == 3:
                sort_q = 'ORDER BY `ladies_id` ASC'
            else:
                sort_q = 'ORDER BY `birthday` ASC'

            if type_load == 'inbox':
                try:
                    with connection.cursor() as c:
                        c.execute('''SELECT `mails`.`id`, `ladies`.`id` AS `ladies_id`, `subject`, `fr`, `date_send`, `date_open`, `type`, `type_send`, `attachment`, `first_name`, `last_name`, `image_lady` FROM `mails` INNER JOIN `ladies` ON `mails`.`fr` = `ladies`.`id` WHERE `mails`.`active`=1 AND `to`=%s %s LIMIT %s OFFSET %s;''' % (str(client_id), sort_q, str(count_to_page), str((page-1)*count_to_page)))
                        mails = dictfetchall(c)
                    with connection.cursor() as k:
                        k.execute('''SELECT COUNT(`id`) FROM `mails` WHERE `active`=1 AND `to`=%s;''' % str(client_id))
                        count_mails = k.fetchone()[0]
                except Exception:
                    count_mails = 0
                    mails = None
            else:
                try:
                    with connection.cursor() as c:
                        c.execute('''SELECT `mails`.`id`, `ladies`.`id` AS `ladies_id`, `subject`, `fr`, `date_send`, `date_open`, `type`, `attachment`, `first_name`, `last_name`, `image_lady` FROM `mails` INNER JOIN `ladies` ON `mails`.`to` = `ladies`.`id` WHERE `mails`.`active`=1 AND `fr`=%s %s LIMIT %s OFFSET %s;''' % (str(client_id), sort_q, str(count_to_page), str((page-1)*count_to_page)))
                        mails = dictfetchall(c)
                    with connection.cursor() as k:
                        k.execute('''SELECT COUNT(`id`) FROM `mails` WHERE `active`=1 AND `fr`=%s;''' % str(client_id))
                        count_mails = k.fetchone()[0]
                except Exception:
                    count_mails = 0
                    mails = None

            if mails:
                for item in mails:
                    item['name'] = u'%s %s.' % (item['first_name'], item['last_name'][0])
                    item['date_send'] = item['date_send'].isoformat()
                    try:
                        item['date_open'] = item['date_open'].isoformat()
                    except AttributeError:
                        item['date_open'] = None

                if count_mails > count_to_page:
                    paginations_data = custom_pagination(page, count_mails, count_to_page)
                else:
                    paginations_data = []

                per_page_list = []
                if count_mails > 15:
                    per_page_list = [15]
                    if count_mails > 25:
                        per_page_list = [15, 25]
                    elif count_mails > 50:
                        per_page_list = [15, 25, 50]
                    elif count_mails > 100:
                        per_page_list = [15, 25, 50, 100]

                data = {'status': True, 'mails': mails, 'paginations_data': paginations_data, 'count_mails': count_mails, 'per_page_list': per_page_list}

    return HttpResponse(json.dumps(data), content_type="application/json")


try:
    import ujson as json
except ImportError:
    import json

def json_response(struct):
    body = json.dumps(struct)

    t = [
        b'HTTP/1.1 200 OK',
        b'Content-Type:application/json; charset=utf-8',
        b'',
        b'',
        b'\r\n']

    t[-2] = bytes(body, encoding='utf-8')
    t[-3] = bytes('Content-Length:'+str(len(body)+4) + "\r\n", encoding='utf-8')

    return b'\r\n'.join(t)