from django.db import models
from django.conf import settings
from ckeditor_uploader.fields import RichTextUploadingField

class GameTitleModel(models.Model):
    """ゲームタイトル名保存モデル"""

    name = models.CharField(verbose_name='ゲームタイトル名', max_length=50, unique=True)

    def __str__(self):
        return self.name

class TagModel(models.Model):
    """タグ名保存モデル"""

    name = models.CharField(verbose_name='タグ名', max_length=50, blank=True)

    def __str__(self):
        return self.name

class PostModel(models.Model):
    """記事投稿モデル"""

    title = models.CharField(
        verbose_name='タイトル', max_length=150
    )
    writer = models.ForeignKey(
        settings.AUTH_USER_MODEL, on_delete=models.PROTECT, verbose_name='投稿者', related_name='post_writer_set'
    )
    game = models.ForeignKey(
        GameTitleModel, on_delete=models.PROTECT, verbose_name='ゲームタイトル'
    )
    tags = models.ManyToManyField(
        TagModel, verbose_name='タグ', blank=True
    )
    content = RichTextUploadingField(
        verbose_name='本文', blank=True
    )
    likes = models.ManyToManyField(
        settings.AUTH_USER_MODEL, verbose_name='いいね', blank=True, related_name='post_likes_set'
    )
    bookmarks = models.ManyToManyField(
        settings.AUTH_USER_MODEL, verbose_name='ブックマーク', blank=True, related_name='post_bookmarks_set'
    )
    page_view = models.IntegerField(
        verbose_name='累計PV数', default=0
    )
    is_public = models.IntegerField(
        verbose_name='公開設定', choices=((0, '非公開'), (1, '公開'),), default=0
    )
    created_at = models.DateTimeField(
        verbose_name='作成日', auto_now_add=True
    )
    updated_at = models.DateTimeField(
        verbose_name='最終更新日', auto_now=True
    )

    def __str__(self):
        return 'タイトル{}:投稿者{}'.format(self.title[:20], self.writer)

class CommentModel(models.Model):
    """記事に対するコメント投稿モデル"""

    writer = models.ForeignKey(
        settings.AUTH_USER_MODEL, on_delete=models.CASCADE, verbose_name='コメントしたユーザー',
        related_name='comment_writer_set'
    )
    text = models.TextField(
        verbose_name='本文', max_length=1500
    )
    target = models.ForeignKey(
        PostModel, on_delete=models.CASCADE, verbose_name='対象記事'
    )
    likes = models.ManyToManyField(
        settings.AUTH_USER_MODEL, verbose_name='いいね', blank=True, related_name='comment_likes_set'
    )
    created_at = models.DateTimeField(
        verbose_name='作成日', auto_now_add=True
    )

    def __str__(self):
        return '{}さんの{}へのコメント'.format(self.writer, self.target)

class DailyTrendModel(models.Model):
    """24時間の人気記事モデル"""

    daily_post = models.OneToOneField(
        PostModel, on_delete=models.CASCADE
    )
    daily_pv = models.IntegerField(
        verbose_name='24時間PV数', default=0
    )
    path = models.URLField(
        verbose_name='URL', blank=True
    )

    def __str__(self):
        return '{}/{}views'.format(self.daily_post, self.daily_pv)

class WeeklyTrendModel(models.Model):
    """7日間の人気記事モデル"""

    weekly_post = models.OneToOneField(
        PostModel, on_delete=models.CASCADE
    )
    weekly_pv = models.IntegerField(
        verbose_name='7日間PV数', default=0
    )
    path = models.URLField(
        verbose_name='URL', blank=True
    )

    def __str__(self):
        return '{}/{}views'.format(self.weekly_post, self.weekly_pv)

class MonthlyTrendModel(models.Model):
    """30日間の人気記事モデル"""

    monthly_post = models.OneToOneField(
        PostModel, on_delete=models.CASCADE
    )
    monthly_pv = models.IntegerField(
        verbose_name='30日間PV数', default=0
    )
    path = models.URLField(
        verbose_name='URL', blank=True
    )

    def __str__(self):
        return '{}/{}views'.format(self.monthly_post, self.monthly_pv)


import unittest
from solid import Solid
from unittest.mock import MagicMock
import pathlib
from OCC.Display.SimpleGui import init_display
from OCC.Extend.ShapeFactory import make_edge
from OCCUtils.Common import random_color

from face_factory import FaceFactory
from util import *
from combiner import combine_faces
from solid_face_validator import SolidFaceValidator

display, start_display, _, _ = init_display()
# display, start_display, _, _ = MagicMock(), MagicMock(), None, None

class SolidTest(unittest.TestCase):
    def setUp(self):
        face_images_dir = pathlib.Path(__file__).parent / "test_data"
        face_factory = FaceFactory(face_images_dir)
        height_mm = 50
        self.face_H = face_factory.create_char('H', height_mm)
        self.face_E = face_factory.create_char('E', height_mm)
        self.face_G = face_factory.create_char('G', height_mm)
        self.face_V = face_factory.create_char('V', height_mm)
        self.face_T = face_factory.create_char('T', height_mm)
        self.face_Q = face_factory.create_char('Q', height_mm)
        self.face_4 = face_factory.create_char('4', height_mm)
        self.compound_HE = combine_faces(self.face_H, self.face_E, height_mm)
        self.compound_GE = combine_faces(self.face_G, self.face_E, height_mm)
        self.compound_VT = combine_faces(self.face_V, self.face_T, height_mm)
        self.compound_Q4 = combine_faces(self.face_Q, self.face_4, height_mm)

    def test_equality(self):
        solid = Solid(split_compound(self.compound_HE)[0])
        self.assertEqual(solid, solid)

    def test_equality_deep_copy(self):
        solid = Solid(split_compound(self.compound_HE)[0])
        self.assertEqual(solid, copy.deepcopy(solid))

    def test_hash_equality(self):
        solid = Solid(split_compound(self.compound_HE)[0])
        self.assertEqual(solid.__hash__(), copy.deepcopy(solid).__hash__())

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


# -*- coding: utf-8 -*-
"""App Module's app.py"""

import os

from flask import Flask

def create_app():
    """
    Flask Application Factory

    :return: app object
    :rtype: flask.app.Flask
    """

    app = Flask(__name__, instance_relative_config=True)

    # ensure the instance folder exists
    try:
        os.makedirs(app.instance_path)
    except OSError:
        pass

    register_extensions(app)
    # register_healthcheck(app)
    # register_blueprints(app)
    # register_errorhandlers(app)
    # register_shellcontext(app)
    # register_commands(app)

    @app.route("/")
    def index():
        return "Hello World!"

    return app

def register_extensions(app):
    """
    Register Flask Extensions

    :param:app Flask app object
    :return: None
    """

    from src.app.exts import extensions

    for ext in extensions:
        ext.init_app(app)

    return None

def register_blueprints(app):
    """
    Register Flask Blueprints

    :param:app Flask app object
    :return: None
    """

    from src.app import auth, main

    blueprints = [auth.bp, main.bp]

    for blueprint in blueprints:
        app.register_blueprint(blueprint)

    return None


from django.contrib import admin
from .models import *
from django.contrib.auth.models import Group, User
from django.forms import TextInput, Textarea, CharField, HiddenInput, BooleanField, Form
from django.utils.html import format_html

admin.site.site_header = admin.site.site_title = ("Namita's Fitness Hub")
admin.site.index_title=('Admin')

# admin.site.register(Achievements)
# admin.site.register(Anniversary)
# admin.site.register(Events)
# admin.site.register(Notices)
# admin.site.register(Testimonials)
# admin.site.register(Socials)
# admin.site.register(Carnivals)
# admin.site.register(Images)

admin.site.unregister(Group)
# admin.site.unregister(User)

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

class CustomActionForm(Form):
    action = CharField(widget=HiddenInput,
                             initial='delete_selected',
                             label='Delete Selected'
                             )
    select_across = BooleanField(
                                       label='',
                                       required=False,
                                       initial=0,
                                       widget=HiddenInput({'class': 'select-across'}),
                                       )

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

@admin.register(Achievements)
class AchievementsAdmin(admin.ModelAdmin):
    action_form = CustomActionForm
    list_display = ['title', 'year', 'type', 'institution']

    class Meta:
        model = Achievements

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

@admin.register(Notices)
class NoticesAdmin(admin.ModelAdmin):
    action_form = CustomActionForm
    list_display = ['notice']

    class Meta:
        model = Notices

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

@admin.register(Testimonials)
class TestimonialsAdmin(admin.ModelAdmin):
    action_form = CustomActionForm
    list_display = ['client_name', 'b_weight', 'a_weight', 'duration', 'total_loss', 'review']

    class Meta:
        model = Testimonials

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

class EventImagesAdmin(admin.StackedInline):
    model = EventImages

@admin.register(Events)
class EventsAdmin(admin.ModelAdmin):
    action_form = CustomActionForm
    inlines = [EventImagesAdmin]
    list_display = ['name', 'desc', 'date']

    class Meta:
        model = Events

# @admin.register(EventImages)
# class EventImagesAdmin(admin.ModelAdmin):
#     pass

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

class SocialImagesAdmin(admin.StackedInline):
    model = SocialImages

@admin.register(Socials)
class SocialsAdmin(admin.ModelAdmin):
    action_form = CustomActionForm
    inlines = [SocialImagesAdmin]
    list_display = ['name', 'desc', 'date']

    class Meta:
        model = Socials

# @admin.register(SocialImages)
# class SocialImagesAdmin(admin.ModelAdmin):
#     pass

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

class AnniversaryImagesAdmin(admin.StackedInline):
    model = AnniversaryImages

@admin.register(Anniversary)
class AnniversaryAdmin(admin.ModelAdmin):
    action_form = CustomActionForm
    inlines = [AnniversaryImagesAdmin]
    list_display = ['name', 'desc', 'date']

    class Meta:
        model = Anniversary

# @admin.register(AnniversaryImages)
# class AnniversaryImagesAdmin(admin.ModelAdmin):
#     pass

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

@admin.register(Exercises)
class ExercisesAdmin(admin.ModelAdmin):
    action_form = CustomActionForm
    list_display = ['name', 'desc', 'video_link']

    class Meta:
        model = Exercises

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


from flask import Flask, request, jsonify
import pickle
import pandas as pd

app = Flask(__name__)

# initiate model & columns
LABEL =['Pass','Fail']
datacol = ['code_module', 'code_presentation', 'gender', 'highest_education',
       'imd_band', 'age_band', 'num_of_prev_attempts', 'studied_credits',
       'disability']
with open('RF_hyper.pkl','rb') as f:
    model = pickle.load(f)

@app.route('/')
def welcome():
    return '
Selamat Datang di Program Backend Model Saya
'

@app.route("/predict", methods=["GET", "POST"])
def predict_result():
    if request.method == "POST":
        content = request.json
        try:
            new_data = {'code_module': content['codeModule'],
                        'code_presentation': content['codePresentation'],
                        'gender' : content['Gender'],
                        'highest_education' : content['highestEducation'],
                        'imd_band' : content['imdBand'],
                        'age_band' : content['ageBand'],
                        'num_of_prev_attempts' : content['numOfPrevAttempts'],
                        'studied_credits' : content['studiedCredits'],
                        'disability' : content['Disability']}
            new_data = pd.DataFrame([new_data])
            data = preprocesing.transform(new_data)
            res = model.predict(data)
            pred = []
            if res > 0.35:
                pred.append('Churn')
            else:
                pred.append('Not Churn')
            result = {'class':pred[0],
                      'class_name':pred[0]}
            response = jsonify(success=True,
                               result=result)
            return response, 200
        except Exception as e:
            response = jsonify(success=False,
                               message=str(e))
            return response, 400
    # return dari method get
    return "
Silahkan gunakan method POST untuk mode inference model
"

app.run(debug=True)


from rest_framework.views import APIView
from rest_framework.response import Response
import requests
from hashlib import sha1
import hmac
from .funciones_bodega import *
from .datos import *
import json
from django.shortcuts import render, render_to_response
from django.http.response import JsonResponse

name_sku_dict = {"Sesamo": "1011",
                "Nori_Entero": "1016",
                "Camaron": "1006",
                "Azucar": "1003",
                "Arroz_Grano_Corto": "1001"}

almacen_dict_id = { "5cc7b139a823b10004d8e6d3" : "recepcion",
                     "5cc7b139a823b10004d8e6d4" : "despacho",
                     "5cc7b139a823b10004d8e6d5" : "almacen_1",
                     "5cc7b139a823b10004d8e6d6" : "almacen_2",
                     "5cc7b139a823b10004d8e6d7" : "pulmon",
                     "5cc7b139a823b10004d8e6d8" : "cocina"}

sku_stock_dict = {  "1301" : 50, "1201" : 250, "1209" : 20, "1109" : 50,"1309" : 170,
                    "1106" : 400,"1114" : 50,"1215" : 20,"1115" : 30,"1105" : 50,
                    "1216" : 50,"1116" : 250,"1110" : 80,"1310" : 20,
                    "1210" : 150,"1112" : 130,"1108" : 10,"1407" : 40,"1207" : 20,
                    "1107" : 50,"1307" : 170,"1211" : 60}

def inventories_view(request):
    lista = stock()
    query = {"query": lista}
    return render_to_response('inventoriestotal.html', query)

def bodegas_view(request):
    lista = []
    for item in revisarBodega().json():
        dict = {"almacen": almacen_dict_id[item['_id']], "total" : item['totalSpace'], "used": item['usedSpace']}
        lista.append(dict)

    query = {"query": lista}
    return render_to_response('bodegas.html', query)

def estadisticas_view(request):
    datos = productos()
    lista_ = []
    lista = stock()
    for sku, information in datos.items():
        if information['stock_minimo'] is not None:
            dict = {"sku": sku, "stock_minimo": str(information['stock_minimo']), "name": information['nombre']}
            cantidad_actual = 0
            for items in lista:
                if items['sku'] == sku:
                    cantidad_actual += items["total"]

            dict["cantidad_actual"] = str(cantidad_actual)

            porcentaje = cantidad_actual * 100 / information['stock_minimo']
            dict["porcentaje"] = porcentaje
            lista_.append(dict)

    query = {"query": lista_}
    return render_to_response('index.html', query)

class InventoriesView(APIView):
    def get(self, request):
        #ESTA ES LA FUNCIÓN QUE HAY QUE MODIFICAR PARA LOS GET
        #SOLO SE MUESTRAN PRODUCTOS DE ALMACEN DESPACHO, ALMACENES GENERALES Y PULMON
        lista = stock_fixed()
        return JsonResponse(lista, status=200, safe=False)

# Cuando hagan post con POSTMAN hay que ponerle un / al final de la URL, así:
# http://127.0.0.1:8000/api/orders/
class OrdersView(APIView):
    def post(self, request):
        #ESTA ES LA FUNCION QUE HAY QUE MODIFICAR PARA LOS POST
        sku = request.data.get("sku")
        cantidad = request.data.get("cantidad")
        almacenId = request.data.get("almacenId")

        if not sku or not cantidad or not almacenId:
            return Response(data="No se creó el pedido por un error del cliente en la solicitud", status=status.HTTP_400_BAD_REQUEST)
        elif sku not in sku_producidos or cantidad > cantidad_producto(sku):
            return Response(data="Producto no se encuentra o cantidad no disponible", status=status.HTTP_404_NOT_FOUND)
        else:
            despachar_producto(sku, cantidad)
            mover_entre_bodegas(sku, cantidad, almacenId)
            dictionary = {"sku": sku, "cantidad": cantidad, "almacenId": almacenId, "grupoProveedor": "2", "aceptado": True, "despachado": True}
            return JsonResponse(dictionary, status=200, safe=False)


# the import section
import webapp2
import jinja2
import os

# this initializes the jinja2 environment
# this will be the same in every app that uses the jinja2 templating library
the_jinja_env = jinja2.Environment(
  loader=jinja2.FileSystemLoader(os.path.dirname(__file__)),
  extensions=['jinja2.ext.autoescape'],
  autoescape=True)

# other functions should go above the handlers or in a separate file

# the handler section
class MainHandler(webapp2.RequestHandler):
  def get(self):  # for a get request
    self.response.write('Greeting')  # the response

class ChinaHandler(webapp2.RequestHandler):
  def get(self):
    self.response.write( "China officially the People's Republic of China (PRC), is a country in East Asia \
      and the world's most populous country, with a population of around 1.404 billion.[10] \
      Covering approximately 9,600,000 square kilometers (3,700,000 sq mi), \
      it is the third- or fourth-largest country by total area.[k][16] Governed by the Communist Party of China, \
      the state exercises jurisdiction over 22 provinces, five autonomous regions, \
      four direct-controlled municipalities (Beijing, Tianjin, Shanghai, and Chongqing), and the special\
      administrative regions of Hong Kong and Macau.")

# the app configuration section
app = webapp2.WSGIApplication([
  #('/', MainPage),
  ('/', MainHandler),
  ('/China', ChinaHandler)
  ], debug=True)


from collections import Counter

def solution(want, number, discount):
    answer = 0
    dic = {}

    for i in range(len(want)): # Counter를 이용해서 dic을 초기화
        dic[want[i]] = number[i]

    for i in range(len(discount) - 9): # Q. 범위를 왜 이렇게 잡았지?
        if dic == Counter(discount[i:i+10]):
            answer += 1

    return answer

# want = ["banana", "apple", "rice", "pork", "pot"] # 원하는 제품을 나타내는 문자열 배열
# number = [3, 2, 2, 2, 1] # 원하는 제품의 수량을 나타내는 정수 배열
# discount = ["chicken", "apple", "apple", "banana", "rice", "apple", "pork",
#             "banana", "pork", "rice", "pot", "banana", "apple", "banana"]
# 마트에서 할인하는 제품을 나타내는 문자열 배열
#
# answer = solution(want, number, discount)
# print(answer)


import json
import sys
import traceback
import os

if __name__ == "__main__":
    sys.path.append('python3')

sys.path.append(os.path.dirname(__file__))

from serviceManager import textServiceMgr

class Client(object):
    def __init__(self, server):
        self.server = server
        self.service = None

    def init(self, msg):
        self.guid = msg["id"]
        self.isWindows8Above = msg["isWindows8Above"]
        self.isMetroApp = msg["isMetroApp"]
        self.isUiLess = msg["isUiLess"]
        self.isUiLess = msg["isConsole"]
        # create the text service
        self.service = textServiceMgr.createService(self, self.guid)
        return (self.service is not None)

    def handleRequest(self, msg): # msg is a json object
        method = msg.get("method")
        seqNum = msg.get("seqNum", 0)
        # print("handle message: ", str(id(self)), method, seqNum)
        service = self.service
        if service:
            # let the text service handle the message
            reply = service.handleRequest(msg)
        else:  # the text service is not yet initialized
            reply = {"seqNum": seqNum}
            success = False
            if method == "init": # initialize the text service
                success = self.init(msg)
            reply["success"] = success
        # print(reply)
        return reply

class Server(object):
    def __init__(self):
        self.clients = {}

    def run(self):
        while True:
            line = ""
            client_id = ""
            try:
                line = input().strip()
                if not line:
                    continue
                # parse PIME requests (one request per line):
                # request format: "<client_id>|<JSON string>\n"
                # response format: "PIME_MSG|<client_id>|<JSON string>\n"
                client_id, msg_text = line.split('|', maxsplit=1)
                msg = json.loads(msg_text)
                client = self.clients.get(client_id)
                if not client:
                    # create a Client instance for the client
                    client = Client(self)
                    self.clients[client_id] = client
                    print("new client:", client_id)
                if msg.get("method") == "close":  # special handling for closing a client
                    self.remove_client(client_id)
                else:
                    ret = client.handleRequest(msg)
                    # Send the response to the client via stdout
                    # one response per line in the format "PIME_MSG|<client_id>|<json reply>"
                    reply_line = '|'.join(["PIME_MSG", client_id, json.dumps(ret, ensure_ascii=False)])
                    print(reply_line)
            except EOFError:
                # stop the server
                break
            except Exception as e:
                print("ERROR:", e, line)
                # print the exception traceback for ease of debugging
                traceback.print_exc()
                # generate an empty output containing {success: False} to prevent the client from being blocked
                reply_line = '|'.join(["PIME_MSG", client_id, '{"success":false}'])
                print(reply_line)
                # Just terminate the python server process if any unknown error happens.
                # The python server will be restarted later by PIMELauncher.
                sys.exit(1)

    def remove_client(self, client_id):
        print("client disconnected:", client_id)
        try:
            del self.clients[client_id]
        except KeyError:
            pass

def main():
    server = Server()
    server.run()

if __name__ == "__main__":
    main()


# -*- coding: utf-8 -*-
from scrapy.spiders import Rule
from scrapy.linkextractors import LinkExtractor
from boardgamecrawler.items import Boardgame, BoardgameData
from boardgamecrawler.spiders import BoardgameSpider

class PlanszostrefaSpider(BoardgameSpider):
    name = 'planszostrefa'
    allowed_domains = ['https://www.planszostrefa.pl', 'planszostrefa.pl']
    start_urls = ['https://planszostrefa.pl']

    rules = (
        #Rule(LinkExtractor(allow=(r'/pl/c/[\w\-\.]*/[\d]+(/)*[\d]*$')), callback='parse_items', follow=True),
        Rule(LinkExtractor(allow=(r'/pl/c/CROWDFUNDING/[\d]+(/)*[\d]*$')), callback='parse_page', follow=True),
    )

    def parse_page(self, response):
        for product in response.css('div.product'):
            data = BoardgameData()
            data['name'] = product.css('span.productname::text').get()
            data['url'] = product.css('a.prodname::attr(href)').get()
            data['price'] = product.css('div.price em::text').get()
            data['oldprice'] = product.css('div.price del::text').get()
            avail = product.css('p.avail span.availability::text').getall()
            avail.append(product.css('span.delivery::text').get())
            data['availability'] = ' '.join([f.strip() for f in avail if f])
            data['add_to_cart_available'] = product.css('form.basket input::attr(value)').get()
            yield Boardgame.create(self.meta, data)


#!/usr/bin/env python

import sys
import logging
import pandas as pd
import numpy as np
import timeit
from sqlalchemy import Engine
from sqlalchemy.orm import Session

from core.helpers import DatabaseHelper
from core.helpers import LoggingHelper
from core.models import Unit
from core.models import Country
from core.models import Education
from core.models import FamilyRelationship
from core.models import Gender
from core.models import Income
from core.models import MaritalStatus
from core.models import Occupation
from core.models import Race
from core.models import WorkingClass

import config

def db_import(engine: Engine, num_rows_to_import: int | None = None):
    try:
        logging.info(f'Loading {config.CSV_FILE}')
        df = pd.read_csv(config.CSV_FILE)
        if num_rows_to_import is not None:
            df = df.head(num_rows_to_import)
        df.replace('?', np.nan, inplace=True)
        logging.info(f'{len(df)} row(s) loaded')

        logging.info(f'Creating new session')
        with Session(engine) as session:
            start = timeit.default_timer()
            logging.info(f'Importing ...')
            for _, row in df.iterrows():
                """
                Expected structure
                ----
                 age                               25
                 workclass                    Private
                 fnlwgt                        226802
                 education                       11th
                 educational-num                    7
                 marital-status         Never-married
                 occupation         Machine-op-inspct
                 relationship               Own-child
                 race                           Black
                 gender                          Male
                 capital-gain                       0
                 capital-loss                       0
                 hours-per-week                    40
                 native-country         United-States
                 income                         <=50K
                """

                # TODO: This piece of code needs to be reviewed and rewritten
                unit = Unit(
                    age=(
                        None if pd.isna(row['age'])
                        else int(row['age'])
                    ),
                    working_class=(
                        None if pd.isna(row['workclass'])
                        else DatabaseHelper.get_or_create(session, WorkingClass, working_class=row['workclass'])
                    ),
                    final_weight=(
                        None if pd.isna(row['fnlwgt'])
                        else int(row['fnlwgt'])),
                    # TODO: Warning! We are not checking fof NaN (but we should)
                    education=(
                        DatabaseHelper.get_or_create(session, Education, education=row['education'], years=row['educational-num'])
                    ),
                    marital_status=(
                        None if pd.isna(row['marital-status'])
                        else DatabaseHelper.get_or_create(session, MaritalStatus, marital_status=row['marital-status'])
                    ),
                    occupation=(
                        None if pd.isna(row['occupation'])
                        else DatabaseHelper.get_or_create(session, Occupation, occupation=row['occupation'])
                    ),
                    family_relationship=(
                        None if pd.isna(row['relationship'])
                        else DatabaseHelper.get_or_create(session, FamilyRelationship, family_relationship=row['relationship'])
                    ),
                    race=(
                        None if pd.isna(row['race'])
                        else DatabaseHelper.get_or_create(session, Race, race=row['race'])
                    ),
                    gender=(
                        None if pd.isna(row['gender'])
                        else DatabaseHelper.get_or_create(session, Gender, gender=row['gender'])
                    ),
                    capital_gain=(
                        None if pd.isna(row['capital-gain'])
                        else int(row['capital-gain'])),
                    capital_loss=(
                        None if pd.isna(row['capital-loss'])
                        else int(row['capital-loss'])),
                    hours_per_week=(
                        None if pd.isna(row['hours-per-week'])
                        else int(row['hours-per-week'])),
                    native_country=(
                        None if pd.isna(row['native-country'])
                        else DatabaseHelper.get_or_create(session, Country, country=row['native-country'])
                    ),
                    income=(
                        None if pd.isna(row['income'])
                        else DatabaseHelper.get_or_create(session, Income, income=row['income'])
                    ),
                )
                session.add(unit)
                session.commit()

        stop = timeit.default_timer()
        logging.info(f'Imported {len(df)} row(s) in {stop - start} second(s)')

    except Exception as e:
        logging.error(e)

if __name__ == '__main__':
    LoggingHelper.set_logging_options()

    limit = None
    try:
        limit = int(sys.argv[1])
    except:
        pass

    logging.info(f'Connecting to {config.SQLALCHEMY_URL}')
    db_helper = DatabaseHelper()

    db_import(db_helper.engine, limit)

    logging.info(f'All done')


# -*- coding: utf-8 -*-
"""
Created on Thu Dec  6 02:42:08 2018

@author: AYSE
"""
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

#verilerin yukleme
veriler= pd.read_csv("eksikveriler.csv")

print(veriler)

#veri on isleme
boy= veriler[["boy"]]
print(boy)
cinsiyet=veriler[["cinsiyet"]]
print(cinsiyet)

boykilo=veriler[["boy","kilo"]]
print(boykilo)

x=10
class insan:
    boy=130
    def kosmak(self,b):
        return b+10

ali= insan()
print(ali.boy)
print(ali.kosmak(90))

#eksik veriler

from sklearn.preprocessing import Imputer
imputer=Imputer(missing_values='NaN', strategy='mean', axis=0)
Yas=veriler.iloc[:,1:4].values
print(Yas)
imputer=imputer.fit(Yas[:,1:4])
Yas[:,1:4]=imputer.transform(Yas[:,1:4])
print(Yas)


from typing import List

import re

import discord
from discord import app_commands
from discord.ext import commands

from uqcsbot.bot import UQCSBot
from uqcsbot.yelling import yelling_exemptor

# value of all valid ascii values in morse code
MorseCodeDict = {
    "A": ". - ",
    "B": "- . . . ",
    "C": "- . - . ",
    "D": "- . . ",
    "E": ". ",
    "F": ". . - . ",
    "G": "- - . ",
    "H": ". . . . ",
    "I": ". . ",
    "J": ". - - - ",
    "K": "- . - ",
    "L": ". - . . ",
    "M": "- - ",
    "N": "- . ",
    "O": "- - - ",
    "P": ". - - . ",
    "Q": "- - . - ",
    "R": ". - . ",
    "S": ". . . ",
    "T": "- ",
    "U": ". . - ",
    "V": ". . . - ",
    "W": ". - - ",
    "X": "- . . - ",
    "Y": "- . - - ",
    "Z": "- - . . ",
    "1": ". - - - - ",
    "2": ". . - - - ",
    "3": ". . . - - ",
    "4": ". . . . - ",
    "5": ". . . . . ",
    "6": "- . . . . ",
    "7": "- - . . . ",
    "8": "- - - . . ",
    "9": "- - - - . ",
    "0": "- - - - - ",
    ",": "- - . . - - ",
    ".": ". - . - . - ",
    "?": ". . - - . . ",
    "/": "- . . - . ",
    "-": "- . . . . - ",
    "(": "- . - - . ",
    ")": "- . - - . - ",
    " ": "  ",
    ";": "_ . _ . _ . ",
    ":": "_ _ _ . . .",
    "'": ". _ _ _ _ . ",
    '"': ". _ . . _ . ",
    "_": ". . _ _ . _ ",
    "=": "_ . . . _ ",
    "+": ". _ . _ . ",
}

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

    @app_commands.command(name="morse")
    @app_commands.describe(message="The message to be converted to morse code")
    @yelling_exemptor(input_args=["message"])
    async def morse_command(
        self, interaction: discord.Interaction, message: str
    ) -> None:
        """
        Returns a string containing the argument converted to dots and dashes.
        """

        # Sanitise invalid characters from the message
        message = Morse.sanitise_illegals(message)

        # Sanitise message for discord emotes
        message = Morse.sanitise_emotes(message)

        # Convert all chars to lower case
        message = message.upper()

        # Then check they are valid morse code ascii
        invalid = Morse.check(message)
        if invalid != "":
            await interaction.response.send_message(
                f"Invalid morse code character/s in string: {invalid}"
            )
            return

        # encrypt message
        response = Morse.encrypt_to_morse(message)

        # Check if response is too long
        if len(response) > 2000:
            await interaction.response.send_message("Resulting message too long!")
            return

        await interaction.response.send_message(response)

    @staticmethod
    def sanitise_illegals(message: str) -> str:
        """
        Replaces all illegal characters in the message with their sanitised
        equivalent.
        """

        # Strip whitespace from either side of the message
        message = message.strip()

        # replace code blocks with their sanitised equivalent
        message = message.replace("`", "")

        return message

    @staticmethod
    def sanitise_emotes(message: str) -> str:
        """
        Replaces all emotes in the message with their emote name instead of
        their id.
        """

        # Regex to match emotes.
        emotes: List[str] = re.findall(r"<a?:\w+:\d+>", message)

        # Replace each emote with its name.
        for emote in emotes:
            emote_name: str = emote.split(":")[1].strip()
            message = message.replace(emote, f":{emote_name}:")

        return message

    @staticmethod
    def check(message: str) -> str:
        ret = ""
        for letter in message:
            if letter not in MorseCodeDict:
                ret += letter
        if len(ret) != 0:
            return ret
        return ""

    @staticmethod
    def encrypt_to_morse(message: str) -> str:
        cipher = ""
        for letter in message:
            cipher += MorseCodeDict[letter] + "  "
        return cipher

async def setup(bot: UQCSBot):
    cog = Morse(bot)
    await bot.add_cog(cog)


import os

from flask import render_template, redirect, request, session, flash
from flask_app import app

from flask_app.models.musicians_model import Musician

import spotipy
from spotipy.oauth2 import SpotifyClientCredentials

CLIENT_ID =  os.environ.get("CLIENT_ID")
CLIENT_SECRET =  os.environ.get("CLIENT_SECRET")

@app.route("/register/musicians")
def show_musician_register():
    return render_template("musician_registration.html")

@app.route("/register_musician", methods=["POST"])
def register_musician():
    print(request.form)

    if not Musician.validate_new(request.form):
        return redirect("/register/musicians")

    session["musician_id"] = Musician.create(request.form)

    return redirect("/musician/my_gigs")

# route to show musician login page
@app.route("/login/musicians")
def show_musician_login():
    return render_template("musician_login.html")

@app.route("/login_musician", methods=["POST"])
def login_musician():

    logged_in_musician = Musician.validate_login(request.form)

    if not logged_in_musician:
        return redirect("/login/musicians")

    session["musician_id"] = logged_in_musician.id
    return redirect("/musician/my_gigs")

@app.route("/logout_musician")
def logout_musician():
    session.pop("musician_id", None)
    return redirect("/login/musicians")

@app.route("/update_musician", methods=["POST"])
def update_musician():

    print(request.form)

    Musician.update(request.form)

    return redirect("/musician/my_gigs")

# route to show results of a given search query
@app.route("/search")
def search():

    if "user_id" not in session:
        flash("Must login to see this page.")
        return redirect("/login/users")

    # todo call class to run query to find matches

    search_query = request.args.get("search")

    results = Musician.search_by_stage_name({"stage_name": search_query})

    return render_template("search_results.html", search_query = search_query, results = results)

@app.route("/musician/profile/<int:musician_id>")
def show_musician(musician_id):

    if "user_id" not in session:
        flash("Must login to see this page.")
        return redirect("/login/users")

    musician = Musician.get_one_by_id({"id": musician_id})

    artist_id = get_artist_id(musician.stage_name)

    return render_template("musician_profile.html", musician = musician, artist_id = artist_id)

@app.route("/musician/my_gigs")
def my_gigs():

    # Todo ----- check session for id and either redirect
    # or query data

    if "musician_id" not in session:
        flash("Must log in to a musician account to view page")
        return redirect("/login/musicians")

    musician = Musician.get_one_by_id({"id" : session["musician_id"]})

    return render_template("my_gigs.html", musician = musician)

def get_spotify_client_cred():
    return SpotifyClientCredentials(client_id=CLIENT_ID, client_secret=CLIENT_SECRET)

def get_artist_id(artist_name):
    auth_manager = get_spotify_client_cred()
    sp = spotipy.Spotify(auth_manager = auth_manager)

    results = sp.search(artist_name, type = "artist")

    if not results["artists"]["items"]:
        return False

    artist_id = results["artists"]["items"][0]["id"]
    return artist_id


import json
import locale
import os
import socket
import sys

from car_info import CarInfo

sys.path.append(r'./forza_motorsport')
import numpy as np
from fdp import ForzaDataPacket
from matplotlib import axes
from matplotlib.pyplot import cm

import constants
import keyboard_helper
from constants import ConfigVersion

def nextFdp(server_socket: socket, format: str):
    """next fdp

    Args:
        server_socket (socket): socket
        format (str): format

    Returns:
        [ForzaDataPacket]: fdp
    """
    try:
        message, _ = server_socket.recvfrom(1024)
        return ForzaDataPacket(message, packet_format=format)
    except BaseException:
        return None

def plot_gear_ratio(forza: CarInfo, ax: axes.Axes = None, row: int = None, col: int = None):
    """plot gear ratio vs gear

    Args:
        forza (CarInfo): car info
        ax (axes.Axes, optional): figure axes. Defaults to None.
        row (int, optional): position of row. Defaults to None.
        col (int, optional): position of column. Defaults to None.
    """
    gear = np.array([item['gear'] for item in forza.records])
    time = np.array([item['time'] for item in forza.records])
    ratio = np.array([item['speed/rpm'] for item in forza.records])
    time0 = forza.records[0]['time']
    time = np.where(time > 0, time - time0, 0)

    ax[row, col].plot(time, ratio, label='speed/rpm', color='b')

    color = iter(cm.rainbow(np.linspace(0, 1, len(forza.gear_ratios))))
    for g, item in forza.gear_ratios.items():
        ax[row, col].hlines(item['ratio'], time[0], time[-1], label=f'gear {g} ratio', color=next(color), linestyles='dashed')

    ax[row, col].set_xlabel('time')
    ax[row, col].set_ylabel('ratio (km/h/rpm)', color='b')
    ax[row, col].tick_params('y', color='b')
    ax[row, col].legend()
    ax[row, col].set_title('Gear Ratio (speed/rpm)')

    ax0 = ax[row, col].twinx()
    ax0.plot(time, gear, label='gear', color='r')
    ax0.set_ylabel('gear', color='r')
    ax0.tick_params('y', colors='r')
    ax0.legend(loc=4)

def plot_torque_rpm(forza: CarInfo, ax: axes.Axes = None, row: int = None, col: int = None):
    """plot output torque vs rpm

    Args:
        forza (CarInfo): car info
        ax (axes.Axes, optional): figure axes. Defaults to None.
        row (int, optional): position of row. Defaults to None.
        col (int, optional): position of column. Defaults to None.
    """
    color = iter(cm.rainbow(np.linspace(0, 1, len(forza.gear_ratios))))
    for g, item in forza.rpm_torque_map.items():
        raw_records = forza.get_gear_raw_records(g)
        data = np.array([[i['rpm'], i['torque']] for i in raw_records[item['min_rpm_index']:item['max_rpm_index']]])
        data = np.sort(data, 0)
        c = next(color)

        rpms = np.array([item[0] for item in data])
        torque = np.array([item[1] for item in data])
        ratio = forza.gear_ratios[g]['ratio']

        ax[row, col].plot(rpms, torque / ratio, label=f'Gear {g} torque', color=c)
        ax[row, col].set_xlabel('rpm (r/m)')
        ax[row, col].set_ylabel('Torque (N/m)')
        ax[row, col].tick_params('y')

    ax[row, col].legend(loc='lower left')
    ax[row, col].set_title('Output Torque vs rpm')
    ax[row, col].grid(visible=True, color='grey', linestyle='--')

def plot_torque_speed(forza: CarInfo, ax: axes.Axes = None, row: int = None, col: int = None):
    """plot output torque vs speed

    Args:
        forza (CarInfo): car info
        ax (axes.Axes, optional): figure axes. Defaults to None.
        row (int, optional): position of row. Defaults to None.
        col (int, optional): position of column. Defaults to None.
    """
    color = iter(cm.rainbow(np.linspace(0, 1, len(forza.gear_ratios))))
    for g, item in forza.rpm_torque_map.items():
        raw_records = forza.get_gear_raw_records(g)
        data = np.array([[i['speed'], i['torque']] for i in raw_records[item['min_rpm_index']:item['max_rpm_index']]])
        data = np.sort(data, 0)
        c = next(color)

        speeds = np.array([item[0] for item in data])
        torque = np.array([item[1] for item in data])
        ratio = forza.gear_ratios[g]['ratio']

        ax[row, col].plot(speeds, torque / ratio, label=f'Gear {g} torque', color=c)
        ax[row, col].set_xlabel('speed (km/h)')
        ax[row, col].set_ylabel('Torque (N/m)')
        ax[row, col].tick_params('y')

    ax[row, col].legend(loc='upper right')
    ax[row, col].set_title('Output Torque vs Speed')
    ax[row, col].grid(visible=True, color='grey', linestyle='--')

def plot_rpm_speed(forza: CarInfo, ax: axes.Axes = None, row: int = None, col: int = None):
    """plot rpm vs speed

    Args:
        forza (CarInfo): car info
        ax (axes.Axes, optional): figure axes. Defaults to None.
        row (int, optional): position of row. Defaults to None.
        col (int, optional): position of column. Defaults to None.
    """
    color = iter(cm.rainbow(np.linspace(0, 1, len(forza.gear_ratios))))
    for g, item in forza.rpm_torque_map.items():
        raw_records = forza.get_gear_raw_records(g)
        data = np.array([[i['speed'], i['rpm']] for i in raw_records[item['min_rpm_index']:item['max_rpm_index']]])
        data = np.sort(data, 0)
        c = next(color)

        speeds = np.array([item[0] for item in data])
        rpm = np.array([item[1] for item in data])

        ax[row, col].plot(speeds, rpm, label=f'Gear {g} rpm', color=c)
        ax[row, col].set_xlabel('speed (km/h)')
        ax[row, col].set_ylabel('rpm (r/m)')
        ax[row, col].tick_params('y')

    ax[row, col].legend(loc='lower right')
    ax[row, col].set_title('rpm vs Speed')
    ax[row, col].grid(visible=True, color='grey', linestyle='--')

def convert(n: object):
    """variables to json serializable

    Args:
        n (object): object to parse

    Returns:
        serializable value
    """
    if isinstance(n, np.int32) or isinstance(n, np.int64):
        return n.item()

def dump_settings(forza: CarInfo):
    """
        write following variables from forza to settings.json:
        clutch
        upshift
        downshift

    Args:
        forza (CarInfo): car info
    """
    json_data = {
        'clutch':       forza.clutch,
        'upshift':      forza.upshift,
        'downshift':    forza.downshift,
    }

    settings_path = os.path.join(forza.config_folder, constants.setting_filename)
    forza.logger.info(f'saving program settings to {settings_path}')
    with open(settings_path, "w") as f:
        json.dump(json_data, f, default=convert, indent=4)

def dump_config(forza: CarInfo, config_version: ConfigVersion = constants.default_config_version):
    """dump config

    Args:
        forza (CarInfo): car info
    """
    try:
        forza.logger.debug(f'{dump_config.__name__} started')
        config_name = get_config_name(forza, config_version)
        forza.logger.info(f'saving config {config_name}')
        config = {
            # === dump data and result ===
            'version': config_version.name,
            'ordinal': forza.ordinal,
            'perf': forza.car_perf,
            'class': forza.car_class,
            'drivetrain': forza.car_drivetrain,
            'minGear': forza.minGear,
            'maxGear': forza.maxGear,
            'gear_ratios': forza.gear_ratios,
            'rpm_torque_map': forza.rpm_torque_map,
            'shift_point': forza.shift_point,
            'records': forza.records,
        }

        with open(os.path.join(forza.config_folder, config_name), "w") as f:
            json.dump(config, f, default=convert, indent=4)
    finally:
        forza.logger.debug(f'{dump_config.__name__} ended')

def get_config_version(forza: CarInfo, filename):
    """get config file version

    Args:
        forza (CarInfo): forza
        filename (_type_): config file name

    Returns:
        config version: config version
    """
    try:
        with open(os.path.join(forza.config_folder, filename), "r") as f:
            config = json.loads(f.read())

        if 'version' in config:
            if config['version'] == str(ConfigVersion.v2):
                return ConfigVersion.v2
            elif config['version'] == str(ConfigVersion.v1):
                return ConfigVersion.v1
            else:
                return ConfigVersion[config['version']]
        else:
            return ConfigVersion.v1
    except Exception as e:
        forza.logger.warning(f'failed to get version of {filename}: {e}')
        return ConfigVersion.v1

def get_config_name(forza: CarInfo, config_version: ConfigVersion = constants.default_config_version):
    """get config name

    Args:
        forza (CarInfo): carinfo
        config_version (ConfigVersion, optional): config version. Defaults to constants.default_config_version.

    Returns:
        str: config name
    """
    if config_version == ConfigVersion.v2:
        return f'{forza.ordinal}-{forza.car_perf}-{forza.car_drivetrain}.json'
    elif config_version == ConfigVersion.v1:
        return f'{forza.ordinal}.json'
    else:
        forza.logger.warning(f'Invalid config version {str(config_version)}')

def load_settings(forza: CarInfo):
    """
        overwrite following variables in forza:
        clutch
        upshift
        downshift

    Args:
        forza (CarInfo): car info
    """
    forza.logger.debug(f'{load_settings.__name__} started')
    settings_path = os.path.join(forza.config_folder, constants.setting_filename)
    try:
        valid_keys = keyboard_helper.keybind.keys()
        if os.path.exists(settings_path):
            forza.logger.info(f'loading program settings from {settings_path}')
            with open(settings_path, "r") as f:
                settings = json.loads(f.read())

            if 'clutch' in settings:
                clutch_shortcut = settings['clutch']
                if clutch_shortcut in valid_keys:
                    forza.clutch = clutch_shortcut
                else:
                    forza.logger.warning(f'clutch shortcut {clutch_shortcut} in {settings_path} is not valid')

            if 'upshift' in settings:
                upshift_shortcut = settings['upshift']
                if upshift_shortcut in valid_keys:
                    forza.upshift = upshift_shortcut
                else:
                    forza.logger.warning(f'upshift shortcut {upshift_shortcut} in {settings_path} is not valid')

            if 'downshift' in settings:
                downshift_shortcut = settings['downshift']
                if downshift_shortcut in valid_keys:
                    forza.downshift = downshift_shortcut
                else:
                    forza.logger.warning(f'downshift shortcut {downshift_shortcut} in {settings_path} is not valid')
    except Exception as e:
        forza.logger.warning(f'failed to load settings {settings_path}: {e}')
    finally:
        forza.logger.debug(f'{load_settings.__name__} ended')

def load_config(forza: CarInfo, path: str):
    """load config as carinfo

    Args:
        forza (CarInfo): car info
        path (str): config path
    """
    try:
        forza.logger.debug(f'{load_config.__name__} started')
        with open(os.path.join(forza.config_folder, path), "r") as f:
            config = json.loads(f.read())

        if 'ordinal' in config:
            forza.ordinal = int(config['ordinal'])

        if 'perf' in config:
            forza.car_perf = int(config['perf'])

        if 'class' in config:
            forza.car_class = int(config['class'])

        if 'drivetrain' in config:
            forza.car_drivetrain = int(config['drivetrain'])

        if 'minGear' in config:
            forza.minGear = config['minGear']

        if 'maxGear' in config:
            forza.maxGear = config['maxGear']

        if 'gear_ratios' in config:
            forza.gear_ratios = {int(key): value for key, value in config['gear_ratios'].items()}

        if 'rpm_torque_map' in config:
            forza.rpm_torque_map = {int(key): value for key, value in config['rpm_torque_map'].items()}

        if 'shift_point' in config:
            forza.shift_point = {int(key): value for key, value in config['shift_point'].items()}

        if 'records' in config:
            forza.records = config['records']
    except BaseException as e:
        forza.logger.exception(e)
    finally:
        forza.logger.debug(f'{load_config.__name__} ended')

def rgb(r, g, b):
    """generate rbg in hex

    Args:
        r
        g
        b

    Returns:
        rgb in hex
    """
    return "#%s%s%s" % tuple([hex(int(c * 255))[2:].rjust(2, "0") for c in (r, g, b)])

def get_sys_lang():
    try:
        lang = locale.getdefaultlocale()[0]
        return 1 if 'zh' in lang else 0
    except Exception:
        return constants.default_language

def create_socket(forza: CarInfo):
    forza.server_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    forza.server_socket.settimeout(1)
    forza.server_socket.bind((forza.ip, forza.port))
    forza.logger.info(f'listening on IP {forza.ip}, Port {forza.port}')

def close_socket(forza: CarInfo):
    forza.server_socket.close()
    forza.logger.info('socket closed')


# The base class of the model that can do the
import tensorflow as tf
import numpy as np
from gensim.models import Word2Vec
import gensim
from gensim.models import KeyedVectors
import sys
from newDataLoader import DataLoader
from time import time
from tqdm import tqdm
import os
from scipy.stats import spearmanr
from scipy.spatial.distance import cosine
import json
import pickle
import argparse

class Model:

    def __init__(self, data, args):
        # random initialize the weights

        self.data = data
        vocab_size = data.vocab_size
        self.vocab_size = data.vocab_size
        embed_size = self.embed_size = 300
        self.res = args.restrict
        self.weight = args.weight

        rel_length = args.relational_embedding_size

        with tf.variable_scope("model", reuse=tf.AUTO_REUSE):

            # The center embedding should be initialize during the training process by "assign"
            # Directly use const is not memory efficient, because the constant is saved multiple times in the memory
            # The trainable here is set for not trainable for now

            self.W_amod = tf.get_variable("amod_tranform_matrix", [rel_length, embed_size])
            self.W_nsubj = tf.get_variable("nsubj_tranform_matrix", [rel_length, embed_size])
            self.W_dobj = tf.get_variable("dobj_tranform_matrix", [rel_length, embed_size])

            self.center_emb = tf.get_variable("center_emb", [vocab_size, embed_size], trainable=args.center_trainable)

            self.small_amod_emb = tf.get_variable("amod_emb", [vocab_size, rel_length])
            self.small_nsubj_emb = tf.get_variable("nsubj_emb", [vocab_size, rel_length])
            self.small_dobj_emb = tf.get_variable("dobj_emb", [vocab_size, rel_length])

            self.amod_emb = tf.matmul(self.small_amod_emb, self.W_amod)
            self.nsubj_emb = tf.matmul(self.small_nsubj_emb, self.W_nsubj)
            self.dobj_emb = tf.matmul(self.small_dobj_emb, self.W_dobj)

            self.W_amod_context = tf.get_variable("amod_context_tranform_matrix", [rel_length, embed_size])
            self.W_nsubj_context = tf.get_variable("nsubj_context_tranform_matrix", [rel_length, embed_size])
            self.W_dobj_context = tf.get_variable("dobj_context_tranform_matrix", [rel_length, embed_size])

            self.center_emb_context = tf.get_variable("center_emb_context", [vocab_size, embed_size], trainable=args.center_trainable)

            self.small_amod_emb_context = tf.get_variable("amod_emb_context", [vocab_size, rel_length])
            self.small_nsubj_emb_context = tf.get_variable("nsubj_emb_context", [vocab_size, rel_length])
            self.small_dobj_emb_context = tf.get_variable("dobj_emb_context", [vocab_size, rel_length])

            self.amod_emb_context = tf.matmul( self.small_amod_emb_context, self.W_amod_context)
            self.nsubj_emb_context = tf.matmul( self.small_nsubj_emb_context, self.W_nsubj_context)
            self.dobj_emb_context = tf.matmul( self.small_dobj_emb_context, self.W_dobj_context)

            self.emb_placeholder = tf.placeholder(tf.float32, [vocab_size, embed_size])
            self.emb_init = self.center_emb.assign(self.emb_placeholder)

            self.emb_placeholder_context = tf.placeholder(tf.float32, [vocab_size, embed_size])
            self.emb_init_context = self.center_emb_context.assign(self.emb_placeholder_context)

            self.amod_restrict_emb = self.restrict(self.amod_emb)
            self.nsubj_restrict_emb = self.restrict(self.nsubj_emb)
            self.dobj_restrict_emb = self.restrict(self.dobj_emb)

            self.amod_restrict_emb_context = self.restrict(self.amod_emb_context)
            self.nsubj_restrict_emb_context = self.restrict(self.nsubj_emb_context)
            self.dobj_restrict_emb_context = self.restrict(self.dobj_emb_context)

            self.predicate_amod_ids = tf.placeholder(tf.int32, [None])
            self.argument_amod_ids = tf.placeholder(tf.int32, [None])
            self.argument_prime_amod_ids = tf.placeholder(tf.int32, [None])

            self.predicate_nsubj_ids = tf.placeholder(tf.int32, [None])
            self.argument_nsubj_ids = tf.placeholder(tf.int32, [None])
            self.argument_prime_nsubj_ids = tf.placeholder(tf.int32, [None])

            self.predicate_dobj_ids = tf.placeholder(tf.int32, [None])
            self.argument_dobj_ids = tf.placeholder(tf.int32, [None])
            self.argument_prime_dobj_ids = tf.placeholder(tf.int32, [None])

            self.add_pred_amod()
            self.add_pred_nsubj()
            self.add_pred_dobj()

            self.add_prediction()
            self.add_loss()
            self.add_optimize()

    def get_tensor_shape(self, x, dim):
        return x.get_shape()[dim].value or tf.shape(x)[dim]

    def add_prediction(self):

        self.accuracy_amod = tf.reduce_mean(tf.to_float(tf.greater(self.pred_amod, self.pred_amod_prime)))
        self.accuracy_nsubj = tf.reduce_mean(tf.to_float(tf.greater(self.pred_nsubj, self.pred_nsubj_prime)))
        self.accuracy_dobj = tf.reduce_mean(tf.to_float(tf.greater(self.pred_dobj, self.pred_dobj_prime)))

    def restrict(self, embeddings):
        num_examples = self.get_tensor_shape(embeddings, 0)
        embedding_norms = tf.norm(embeddings, axis=1)
        mask = tf.greater(embedding_norms, tf.ones([num_examples]) * self.res)
        normalized_embeddings = tf.nn.l2_normalize(embeddings, axis=1) * self.res
        return tf.where(mask, x=normalized_embeddings, y=embeddings)

    def softmax_loss(self, antecedent_scores, antecedent_labels):
        gold_scores = antecedent_scores + tf.log(tf.to_float(antecedent_labels))  # [k, max_ant + 1]
        marginalized_gold_scores = tf.reduce_logsumexp(gold_scores, [1])  # [k]
        log_norm = tf.reduce_logsumexp(antecedent_scores, [1])  # [k]
        return log_norm - marginalized_gold_scores  # [k]

    def add_pred_amod(self):
        # center embeddings
        predicate_embedding = (1 - self.weight) * tf.nn.embedding_lookup(self.center_emb, self.predicate_amod_ids) + self.weight * self.restrict(
            tf.nn.embedding_lookup(
                self.amod_emb, self.predicate_amod_ids))
        argument_embedding = (1 - self.weight) * tf.nn.embedding_lookup(self.center_emb, self.argument_amod_ids) + self.weight * self.restrict(
            tf.nn.embedding_lookup(
                self.amod_emb, self.argument_amod_ids))
        argument_prime_embedding = (1 - self.weight) * tf.nn.embedding_lookup(self.center_emb,
                                                          self.argument_prime_amod_ids) + self.weight * self.restrict(
            tf.nn.embedding_lookup(
                self.amod_emb, self.argument_prime_amod_ids))

        # context embeddings
        predicate_embedding_context = (1 - self.weight) * tf.nn.embedding_lookup(self.center_emb_context, self.predicate_amod_ids) + self.weight * self.restrict(
            tf.nn.embedding_lookup(
                self.amod_emb_context, self.predicate_amod_ids))
        argument_embedding_context = (1 - self.weight) * tf.nn.embedding_lookup(self.center_emb_context, self.argument_amod_ids) + self.weight * self.restrict(
            tf.nn.embedding_lookup(
                self.amod_emb_context, self.argument_amod_ids))
        argument_prime_embedding_context = (1 - self.weight) * tf.nn.embedding_lookup(self.center_emb_context,
                                                          self.argument_prime_amod_ids) + self.weight * self.restrict(
            tf.nn.embedding_lookup(
                self.amod_emb_context, self.argument_prime_amod_ids))

        self.overall_predicate_amod = predicate_embedding

        self.base_embedding_handler = tf.nn.embedding_lookup(self.center_emb, self.predicate_amod_ids)
        self.base_embedding_context_handler = tf.nn.embedding_lookup(self.center_emb_context, self.predicate_amod_ids)

        self.amod_embedding_handler = predicate_embedding
        self.amod_embedding_context_handler = argument_embedding_context

        # the score is calculated as pred * argu_context + argu * pred_context
        self.pred_amod = tf.reduce_sum(tf.multiply(predicate_embedding, argument_embedding_context), 1) + \
        tf.reduce_sum(tf.multiply(predicate_embedding_context, argument_embedding), 1)

        self.pred_amod_prime = tf.reduce_sum(tf.multiply(predicate_embedding, argument_prime_embedding_context), 1) + \
        tf.reduce_sum(tf.multiply(predicate_embedding_context, argument_prime_embedding), 1)

    def add_pred_nsubj(self):
        predicate_embedding = (1 - self.weight) * tf.nn.embedding_lookup(self.center_emb, self.predicate_nsubj_ids) + self.weight * self.restrict(
            tf.nn.embedding_lookup(
                self.nsubj_emb, self.predicate_nsubj_ids))
        argument_embedding = (1 - self.weight) * tf.nn.embedding_lookup(self.center_emb, self.argument_nsubj_ids) + self.weight * self.restrict(
            tf.nn.embedding_lookup(
                self.nsubj_emb, self.argument_nsubj_ids))
        argument_prime_embedding = (1 - self.weight) * tf.nn.embedding_lookup(self.center_emb,
                                                          self.argument_prime_nsubj_ids) + self.weight * self.restrict(
            tf.nn.embedding_lookup(self.nsubj_emb, self.argument_prime_nsubj_ids))

        # context embeddings
        predicate_embedding_context = (1 - self.weight) * tf.nn.embedding_lookup(self.center_emb_context, self.predicate_nsubj_ids) + self.weight * self.restrict(
            tf.nn.embedding_lookup(
                self.nsubj_emb_context, self.predicate_nsubj_ids))
        argument_embedding_context = (1 - self.weight) * tf.nn.embedding_lookup(self.center_emb_context, self.argument_nsubj_ids) + self.weight * self.restrict(
            tf.nn.embedding_lookup(
                self.nsubj_emb_context, self.argument_nsubj_ids))
        argument_prime_embedding_context = (1 - self.weight) * tf.nn.embedding_lookup(self.center_emb_context,
                                                          self.argument_prime_nsubj_ids) + self.weight * self.restrict(
            tf.nn.embedding_lookup(
                self.nsubj_emb_context, self.argument_prime_nsubj_ids))

        self.overall_predicate_nsubj = predicate_embedding

        self.nsubj_embedding_handler = predicate_embedding
        self.nsubj_embedding_context_handler = argument_embedding_context

        self.pred_nsubj = tf.reduce_sum(tf.multiply(predicate_embedding, argument_embedding_context), 1) + \
        tf.reduce_sum(tf.multiply(predicate_embedding_context, argument_embedding), 1)

        self.pred_nsubj_prime = tf.reduce_sum(tf.multiply(predicate_embedding, argument_prime_embedding_context), 1) + \
        tf.reduce_sum(tf.multiply(predicate_embedding_context, argument_prime_embedding), 1)

    def add_pred_dobj(self):
        predicate_embedding = (1 - self.weight) * tf.nn.embedding_lookup(self.center_emb, self.predicate_dobj_ids) + self.weight * self.restrict(
            tf.nn.embedding_lookup(
                self.dobj_emb, self.predicate_dobj_ids))
        argument_embedding = (1 - self.weight) * tf.nn.embedding_lookup(self.center_emb, self.argument_dobj_ids) + self.weight * self.restrict(
            tf.nn.embedding_lookup(
                self.dobj_emb, self.argument_dobj_ids))
        argument_prime_embedding = (1 - self.weight) * tf.nn.embedding_lookup(self.center_emb,
                                                          self.argument_prime_dobj_ids) + self.weight * self.restrict(
            tf.nn.embedding_lookup(self.dobj_emb, self.argument_prime_dobj_ids))

        # context embeddings
        predicate_embedding_context = (1 - self.weight) * tf.nn.embedding_lookup(self.center_emb_context, self.predicate_dobj_ids) + self.weight * self.restrict(
            tf.nn.embedding_lookup(
                self.dobj_emb_context, self.predicate_dobj_ids))
        argument_embedding_context = (1 - self.weight) * tf.nn.embedding_lookup(self.center_emb_context, self.argument_dobj_ids) + self.weight * self.restrict(
            tf.nn.embedding_lookup(
                self.dobj_emb_context, self.argument_dobj_ids))
        argument_prime_embedding_context = (1 - self.weight) * tf.nn.embedding_lookup(self.center_emb_context,
                                                          self.argument_prime_dobj_ids) + self.weight * self.restrict(
            tf.nn.embedding_lookup(
                self.dobj_emb_context, self.argument_prime_dobj_ids))

        self.overall_predicate_dobj = predicate_embedding

        self.dobj_embedding_handler = predicate_embedding
        self.dobj_embedding_context_handler = argument_embedding_context

        self.pred_dobj = tf.reduce_sum(tf.multiply(predicate_embedding, argument_embedding_context), 1) + \
        tf.reduce_sum(tf.multiply(predicate_embedding_context, argument_embedding), 1)

        self.pred_dobj_prime = tf.reduce_sum(tf.multiply(predicate_embedding, argument_prime_embedding_context), 1) + \
        tf.reduce_sum(tf.multiply(predicate_embedding_context, argument_prime_embedding), 1)

    def add_loss(self):
        num_examples = self.get_tensor_shape(self.pred_dobj, 0)

        self.prediction = tf.reshape(tf.concat([self.pred_amod, self.pred_nsubj, self.pred_dobj], 0), [3*num_examples, 1] )
        self.prediction_prime = tf.reshape(tf.concat([self.pred_amod_prime, self.pred_nsubj_prime, self.pred_dobj_prime], 0), [3*num_examples, 1])

        all_scores = tf.concat([self.prediction, self.prediction_prime], 1)  # [3 * num_examples, 2]
        all_labels = tf.concat([tf.ones([3 * num_examples, 1]), tf.zeros([3 * num_examples, 1], 1)], 1)
        self.loss = tf.reduce_sum(tf.nn.sigmoid_cross_entropy_with_logits(logits = all_scores, labels = all_labels))
    def add_optimize(self):
        optimizer = tf.train.GradientDescentOptimizer(0.5)
        self.optimize = optimizer.minimize(self.loss)

def test_keller_overall(sess, model, data):
    relation_names = ["amod", "dobj"]
    word2id = data.word2id

    for relation_name in relation_names:

        word_pairs = []
        gold_scores = []
        with open("SPA-dataset/" + relation_name + "_keller.txt") as amod_f:
            for line in amod_f:
                line_list = line.strip().split()

                try:
                    id1 = word2id[line_list[0]]
                except:
                    # print("Not in scope ", line_list[0])
                    continue

                try:
                    id2 = word2id[line_list[1]]
                except:
                    # print("Not in scope ", line_list[1])
                    continue

                score = float(line_list[2])
                gold_scores.append(score)
                word_pairs.append([id1, id2])
        word_pairs = np.array(word_pairs)

        feed_dict = {
            model.predicate_amod_ids: word_pairs[:, 0],
            model.argument_amod_ids: word_pairs[:, 0],
            model.predicate_dobj_ids: word_pairs[:, 0],
            model.argument_dobj_ids: word_pairs[:, 0],
            model.predicate_nsubj_ids: word_pairs[:, 0],
            model.argument_nsubj_ids: word_pairs[:, 0]
        }

        embeddings1 = {"base": {},
                       "amod": {},
                       "nsubj": {},
                       "dobj": {}
                      }

        result1 = sess.run([model.base_embedding_handler,
                                model.base_embedding_context_handler,
                                model.amod_embedding_handler,
                                model.amod_embedding_context_handler,
                                model.nsubj_embedding_handler,
                                model.nsubj_embedding_context_handler,
                                model.dobj_embedding_handler,
                                model.dobj_embedding_context_handler
                                ], feed_dict = feed_dict)

        embeddings1["base"]["h"],  embeddings1["base"]["t"], embeddings1["amod"]["h"],  embeddings1["amod"]["t"], \
        embeddings1["nsubj"]["h"],  embeddings1["nsubj"]["t"], embeddings1["dobj"]["h"],  embeddings1["dobj"]["t"] = result1

        feed_dict = {
            model.predicate_amod_ids: word_pairs[:, 1],
            model.argument_amod_ids: word_pairs[:, 1],
            model.predicate_dobj_ids: word_pairs[:, 1],
            model.argument_dobj_ids: word_pairs[:, 1],
            model.predicate_nsubj_ids: word_pairs[:, 1],
            model.argument_nsubj_ids: word_pairs[:, 1]
        }

        embeddings2 = {"base": {},
                       "amod": {},
                       "nsubj": {},
                       "dobj": {}
                      }

        result2 = sess.run([model.base_embedding_handler,
                                model.base_embedding_context_handler,
                                model.amod_embedding_handler,
                                model.amod_embedding_context_handler,
                                model.nsubj_embedding_handler,
                                model.nsubj_embedding_context_handler,
                                model.dobj_embedding_handler,
                                model.dobj_embedding_context_handler
                                ], feed_dict = feed_dict)

        embeddings2["base"]["h"],  embeddings2["base"]["t"], embeddings2["amod"]["h"],  embeddings2["amod"]["t"], \
        embeddings2["nsubj"]["h"],  embeddings2["nsubj"]["t"], embeddings2["dobj"]["h"],  embeddings2["dobj"]["t"] = result2

        base_word_score = [ 1 - cosine(embeddings1["base"]["h"][j], embeddings2["base"]["t"][j]) for j in range(embeddings1["base"]["h"].shape[0])]

        base_corr, _ = spearmanr(gold_scores, base_word_score)

        rel_word_score  = [ 1 - cosine(embeddings1[relation_name]["h"][j], embeddings2[relation_name]["t"][j]) for j in range(embeddings1[relation_name]["h"].shape[0])]
        rel_corr, _ = spearmanr(gold_scores, rel_word_score)

        print("Keller Overall: ", relation_name, "corr: %.4f  base corr:  %.4f" % (rel_corr, base_corr))

def sp10k_test_overall(sess, model, data, relation_length, restriction):
    relation_names = ["amod", "nsubj", "dobj"]
    word2id = data.word2id
    result_list = []
    for relation_name in relation_names:

        word_pairs = []
        gold_scores = []
        with open("SPA-dataset/" + relation_name + "_annotation.txt") as amod_f:
            for line in amod_f:
                line_list = line.strip().split()

                try:
                    id1 = word2id[line_list[0]]
                except:
                    # print("Not in scope ", line_list[0])
                    continue

                try:
                    id2 = word2id[line_list[1]]
                except:
                    # print("Not in scope ", line_list[1])
                    continue

                score = float(line_list[2])
                gold_scores.append(score)
                word_pairs.append([id1, id2])
        word_pairs = np.array(word_pairs)

        feed_dict = {
            model.predicate_amod_ids: word_pairs[:, 0],
            model.argument_amod_ids: word_pairs[:, 0],
            model.predicate_dobj_ids: word_pairs[:, 0],
            model.argument_dobj_ids: word_pairs[:, 0],
            model.predicate_nsubj_ids: word_pairs[:, 0],
            model.argument_nsubj_ids: word_pairs[:, 0]
        }

        embeddings1 = {"base": {},
                       "amod": {},
                       "nsubj": {},
                       "dobj": {}
                      }

        result1 = sess.run([model.base_embedding_handler,
                                model.base_embedding_context_handler,
                                model.amod_embedding_handler,
                                model.amod_embedding_context_handler,
                                model.nsubj_embedding_handler,
                                model.nsubj_embedding_context_handler,
                                model.dobj_embedding_handler,
                                model.dobj_embedding_context_handler
                                ], feed_dict = feed_dict)

        embeddings1["base"]["h"],  embeddings1["base"]["t"], embeddings1["amod"]["h"],  embeddings1["amod"]["t"], \
        embeddings1["nsubj"]["h"],  embeddings1["nsubj"]["t"], embeddings1["dobj"]["h"],  embeddings1["dobj"]["t"] = result1

        feed_dict = {
            model.predicate_amod_ids: word_pairs[:, 1],
            model.argument_amod_ids: word_pairs[:, 1],
            model.predicate_dobj_ids: word_pairs[:, 1],
            model.argument_dobj_ids: word_pairs[:, 1],
            model.predicate_nsubj_ids: word_pairs[:, 1],
            model.argument_nsubj_ids: word_pairs[:, 1]
        }

        embeddings2 = {"base": {},
                       "amod": {},
                       "nsubj": {},
                       "dobj": {}
                      }

        result2 = sess.run([model.base_embedding_handler,
                                model.base_embedding_context_handler,
                                model.amod_embedding_handler,
                                model.amod_embedding_context_handler,
                                model.nsubj_embedding_handler,
                                model.nsubj_embedding_context_handler,
                                model.dobj_embedding_handler,
                                model.dobj_embedding_context_handler
                                ], feed_dict = feed_dict)

        embeddings2["base"]["h"],  embeddings2["base"]["t"], embeddings2["amod"]["h"],  embeddings2["amod"]["t"], \
        embeddings2["nsubj"]["h"],  embeddings2["nsubj"]["t"], embeddings2["dobj"]["h"],  embeddings2["dobj"]["t"] = result2

        base_word_score = [ 1 - cosine(embeddings1["base"]["h"][j], embeddings2["base"]["t"][j]) for j in range(embeddings1["base"]["h"].shape[0])]

        base_corr, _ = spearmanr(gold_scores, base_word_score)

        rel_word_score  = [ 1 - cosine(embeddings1[relation_name]["h"][j], embeddings2[relation_name]["t"][j]) for j in range(embeddings1[relation_name]["h"].shape[0])]
        rel_corr, _ = spearmanr(gold_scores, rel_word_score)

        print("Overall ", relation_name, "corr: %.4f  base corr:  %.4f" % (rel_corr, base_corr))
        result_list.append(rel_corr)

    with open("traverse/sp10k_result_l_%d_r_%.3f.csv "%(relation_length, restriction), "a+") as fout:
        fout.write("%.4f,%.4f,%.4f\n" % tuple(result_list))

def ws_test(sess, model, data, simlex_corps, num_cross, relation_length, restriction):

    word2id = data.word2id

    corp_names = ["v", "n", "adj", "all"]
    scores = {}

    # with open("ws_gensim_result_c_%d_l_%d_r_%.3f.p "%(int(num_cross), relation_length, restriction), "wb") as fout:

    for i in range(len(simlex_corps)):
        simlex_corpora = simlex_corps[i]

        id_score_triplets = []
        gold_scores = []
        for words_score_trplet in simlex_corpora:
            try:
                context1 = word2id[words_score_trplet[0]]
                context2 = word2id[words_score_trplet[1]]
                gold_score = words_score_trplet[2]
                gold_scores.append(gold_score)

                id_score_triplets.append([context1, context2])
            except:
                pass
        id_score_triplets = np.array(id_score_triplets)

        feed_dict = {
            model.predicate_amod_ids: id_score_triplets[:, 0],
            model.argument_amod_ids: id_score_triplets[:, 0],
            model.predicate_dobj_ids: id_score_triplets[:, 0],
            model.argument_dobj_ids: id_score_triplets[:, 0],
            model.predicate_nsubj_ids: id_score_triplets[:, 0],
            model.argument_nsubj_ids: id_score_triplets[:, 0]
        }

        embeddings1 = {"base": {},
                       "amod": {},
                       "nsubj": {},
                       "dobj": {}
                      }

        result1 = sess.run([model.base_embedding_handler,
                                model.base_embedding_context_handler,
                                model.amod_embedding_handler,
                                model.amod_embedding_context_handler,
                                model.nsubj_embedding_handler,
                                model.nsubj_embedding_context_handler,
                                model.dobj_embedding_handler,
                                model.dobj_embedding_context_handler
                                ], feed_dict = feed_dict)

        embeddings1["base"]["h"],  embeddings1["base"]["t"], embeddings1["amod"]["h"],  embeddings1["amod"]["t"], \
        embeddings1["nsubj"]["h"],  embeddings1["nsubj"]["t"], embeddings1["dobj"]["h"],  embeddings1["dobj"]["t"] = result1

        feed_dict = {
            model.predicate_amod_ids: id_score_triplets[:, 1],
            model.argument_amod_ids: id_score_triplets[:, 1],
            model.predicate_dobj_ids: id_score_triplets[:, 1],
            model.argument_dobj_ids: id_score_triplets[:, 1],
            model.predicate_nsubj_ids: id_score_triplets[:, 1],
            model.argument_nsubj_ids: id_score_triplets[:, 1]
        }

        embeddings2 = {"base": {},
                       "amod": {},
                       "nsubj": {},
                       "dobj": {}
                      }

        result2 = sess.run([model.base_embedding_handler,
                                model.base_embedding_context_handler,
                                model.amod_embedding_handler,
                                model.amod_embedding_context_handler,
                                model.nsubj_embedding_handler,
                                model.nsubj_embedding_context_handler,
                                model.dobj_embedding_handler,
                                model.dobj_embedding_context_handler
                                ], feed_dict = feed_dict)

        embeddings2["base"]["h"],  embeddings2["base"]["t"], embeddings2["amod"]["h"],  embeddings2["amod"]["t"], \
        embeddings2["nsubj"]["h"],  embeddings2["nsubj"]["t"], embeddings2["dobj"]["h"],  embeddings2["dobj"]["t"] = result2

        for key in embeddings1.keys():
            embeddings1[key]["h+t"] = embeddings1[key]["h"] + embeddings1[key]["t"]
            embeddings1[key]["[h,t]"] = np.concatenate((embeddings1[key]["h"],  embeddings1[key]["t"]), axis = 1)

        for key in embeddings2.keys():
            embeddings2[key]["h+t"] = embeddings2[key]["h"] + embeddings2[key]["t"]
            embeddings2[key]["[h,t]"] = np.concatenate((embeddings2[key]["h"],  embeddings2[key]["t"]), axis = 1)

        relation_result = {}

        for relation_name in embeddings1.keys():
            types_result = {}
            for type_name in embeddings1[relation_name].keys():

                model_predition_score = [ 1 - cosine(embeddings1[relation_name][type_name][j], embeddings2[relation_name][type_name][j]) for j in range(embeddings1[relation_name][type_name].shape[0])]

                r, p = spearmanr(gold_scores, model_predition_score)
                types_result[type_name] = r

            relation_result[relation_name] = types_result
            print(corp_names[i], relation_name, types_result)
        scores[corp_names[i]] = relation_result
        # pickle.dump(scores, fout)

    return scores

def sd_test(sess, model, data, batch_size, c, l, r):
    batch = data.get_sd_test_batch(batch_size)

    feed_dict = {
                    model.predicate_amod_ids: batch["amod"][:, 0],
                    model.argument_amod_ids: batch["amod"][:, 1],
                    model.argument_prime_amod_ids: batch["amod"][:, 2],

                    model.predicate_nsubj_ids: batch["nsubj"][:, 0],
                    model.argument_nsubj_ids: batch["nsubj"][:, 1],
                    model.argument_prime_nsubj_ids: batch["nsubj"][:, 2],

                    model.predicate_dobj_ids: batch["dobj"][:, 0],
                    model.argument_dobj_ids: batch["dobj"][:, 1],
                    model.argument_prime_dobj_ids: batch["dobj"][:, 2],
                }

    pos_amod, pos_nsubj, pos_dobj, neg_amod, neg_nsubj, neg_dobj= sess.run([model.pred_amod, model.pred_nsubj, model.pred_dobj,
        model.pred_amod_prime, model.pred_nsubj_prime, model.pred_dobj_prime], feed_dict=feed_dict)

    # with open("pd_gensim_result_c_%d_l_%d_r_%.3f.p "%(int(c), l, r), "wb") as fout:

    amod_acc = np.sum(pos_amod > neg_amod)/ batch_size
    nsubj_acc = np.sum(pos_nsubj > neg_nsubj) / batch_size
    dobj_acc = np.sum(pos_dobj > neg_dobj) / batch_size
    total_acc = (np.sum(pos_amod > neg_amod) + np.sum(pos_nsubj > neg_nsubj) + np.sum(pos_dobj > neg_dobj))/ (batch_size * 3)

    #     # pickle.dump({"amod": amod_acc, "nsubj": nsubj_acc, "dobj": dobj_acc, "overall": total_acc}, fout)
    print("sd scores:", amod_acc, nsubj_acc, dobj_acc, total_acc)

    return amod_acc, nsubj_acc, dobj_acc, total_acc

def main():

    parser = argparse.ArgumentParser()
    parser.add_argument("-p", "--pretrain", action="store_true")
    parser.add_argument("-t", "--center_trainable", action="store_true")
    parser.add_argument("-w", "--weight", type=float, default="0.5")
    parser.add_argument("-r", "--restrict", type=float, default="1")
    parser.add_argument("-c", "--cross_validate", type=int, default=0)
    parser.add_argument("-g", "--GPU", type=str, default="1")
    parser.add_argument("-e", "--num_epoch", type=int, default=20)
    parser.add_argument("-l", "--relational_embedding_size", type=int, default=10)
    args = parser.parse_args()
    print(args)

    os.environ["CUDA_VISIBLE_DEVICES"]=args.GPU
    statsPath = "data/2018_corpus_stats.pkl"

    num_cross = str(args.cross_validate)

    train_data = DataLoader()
    train_data.read_stats(statsPath)
    train_data.load_pairs_counting("data/train_count.pkl")
    train_data.load_argument_sample_table("data/argument_sample_table.p")

    test_data = DataLoader()
    test_data.read_stats(statsPath)
    test_data.load_pairs_counting("data/test_count.pkl")
    test_data.load_argument_sample_table("data/argument_sample_table.p")

    modelPath = "data/stage_one.model"
    word2vecModel= Word2Vec.load(modelPath)

    context_wv = KeyedVectors(vector_size=300)
    context_wv.vocab = word2vecModel.wv.vocab
    context_wv.index2word = word2vecModel.wv.index2word
    context_wv.syn0 = word2vecModel.syn1neg

    pretrain_center_emb = list()
    pretrain_context_emb = list()

    counter = 0

    for i in range(len(train_data.id2word)):
        tmp_w = train_data.id2word[i]
        if tmp_w in context_wv.vocab:
            pretrain_center_emb.append(word2vecModel[tmp_w])
            pretrain_context_emb.append(context_wv[tmp_w])
        else:
            pretrain_center_emb.append(np.zeros(300))
            pretrain_context_emb.append(np.zeros(300))
            counter += 1

    print("empty count", counter)

    pretrain_center_emb = np.asarray(pretrain_center_emb)

    wordsim_dir = "./Word-similarity-dataset/Simlex/"

    with open(wordsim_dir + "verb.json", "r") as f:
        verb_list = json.load(f)

    with open(wordsim_dir + "noun.json", "r") as f:
        noun_list = json.load(f)

    with open(wordsim_dir + "adjective.json", "r") as f:
        adjective_list = json.load(f)

    with open(wordsim_dir + "all.json", "r") as f:
        all_list = json.load(f)

    simlex_corpora = [verb_list, noun_list, adjective_list, all_list]
    simlex_names = ["verb_list", "noun_list", "adjective_list", "all_list"]

    m = Model(train_data, args)

    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())

        if args.pretrain:

            # Initial assign
            sess.run(m.emb_init, feed_dict={m.emb_placeholder: pretrain_center_emb})
            sess.run(m.emb_init_context , feed_dict={m.emb_placeholder_context: pretrain_context_emb})

        ws_test(sess, m, test_data, simlex_corpora, num_cross, args.relational_embedding_size, args.restrict)
        sp10k_test_overall(sess, m, train_data, args.relational_embedding_size, args.restrict)
        test_keller_overall(sess, m, train_data)

        num_epoch = args.num_epoch
        num_batch = 256

        batch_size = 1024
        for epoch in range(num_epoch):

            print(" epoch:", str(epoch + 1), "/", num_epoch)

            process_bar = tqdm(range(num_batch))
            for i in process_bar:
                batch = train_data.get_sd_train_batch(batch_size)

                feed_dict = {
                    m.predicate_amod_ids: batch["amod"][:, 0],
                    m.argument_amod_ids: batch["amod"][:, 1],
                    m.argument_prime_amod_ids: batch["amod"][:, 2],

                    m.predicate_nsubj_ids: batch["nsubj"][:, 0],
                    m.argument_nsubj_ids: batch["nsubj"][:, 1],
                    m.argument_prime_nsubj_ids: batch["nsubj"][:, 2],

                    m.predicate_dobj_ids: batch["dobj"][:, 0],
                    m.argument_dobj_ids: batch["dobj"][:, 1],
                    m.argument_prime_dobj_ids: batch["dobj"][:, 2],
                }

                loss, _ = sess.run([m.loss, m.optimize], feed_dict=feed_dict)

                process_bar.set_description("Loss: %0.4f" % loss)

            # test
            ws_test(sess, m, test_data, simlex_corpora, num_cross, args.relational_embedding_size, args.restrict)
            sp10k_test_overall(sess, m, train_data, args.relational_embedding_size, args.restrict)
            test_keller_overall(sess, m, train_data)

        # save the center, relational, and transpose matrix

        # center_emb, amod_emb, nsubj_emb, dobj_emb = sess.run([m.center_emb, m.amod_emb, m.nsubj_emb, m.dobj_emb])
        # center_emb_context, amod_emb_context, nsubj_emb_context, dobj_emb_context = sess.run([m.center_emb_context, m.amod_emb_context, m.nsubj_emb_context, m.dobj_emb_context])

        # with open('center_embedding_add_context.txt', 'w') as file_:
        #     for i in range(train_data.vocab_size):
        #       embed = center_emb[i, :] + center_emb_context[i, :]
        #       word = train_data.id2word[i]
        #       file_.write('%s %s\n' % (word, ' '.join(map(str, embed))))

        # with open('center_embedding_concat_context.txt', 'w') as file_:
        #     for i in range(train_data.vocab_size):
        #       embed = np.concatenate((center_emb[i, :], center_emb_context[i, :]), axis=0)
        #       word = train_data.id2word[i]
        #       file_.write('%s %s\n' % (word, ' '.join(map(str, embed))))

        # with open('all_embedding_add_context.txt', 'w') as file_:
        #     for i in range(train_data.vocab_size):
        #       embed = np.concatenate(((center_emb[i, :] + center_emb_context[i, :]), (amod_emb[i, :] + amod_emb_context[i, :]), (nsubj_emb[i, :] + nsubj_emb_context[i, :]), (dobj_emb[i, :] + dobj_emb_context[i, :])), axis=0)
        #       word = train_data.id2word[i]
        #       file_.write('%s %s\n' % (word, ' '.join(map(str, embed))))

        # with open('all_embedding_concat_context.txt', 'w') as file_:
        #     for i in range(train_data.vocab_size):
        #       embed = np.concatenate((center_emb[i, :], amod_emb[i, :], nsubj_emb[i, :], dobj_emb[i, :], center_emb_context[i, :], amod_emb_context[i, :], nsubj_emb_context[i, :], dobj_emb_context[i, :]), axis=0)
        #       word = train_data.id2word[i]
        #       file_.write('%s %s\n' % (word, ' '.join(map(str, embed))))

if __name__ == "__main__":
    main()


import warnings

import setuptools

from . import _get_version
from . import Configuration
from .utils import do
from .utils import iter_entry_points
from .utils import trace

def version_keyword(dist: setuptools.Distribution, keyword, value):
    if not value:
        return
    if value is True:
        value = {}
    if getattr(value, "__call__", None):
        value = value()
    assert (
        "dist_name" not in value
    ), "dist_name may not be specified in the setup keyword "

    trace(
        "version keyword",
        vars(dist.metadata),
    )
    dist_name = dist.metadata.name
    config = Configuration(dist_name=dist_name, **value)
    dist.metadata.version = _get_version(config)

def find_files(path=""):
    for ep in iter_entry_points("setuptools_scm.files_command"):
        command = ep.load()
        if isinstance(command, str):
            # this technique is deprecated
            res = do(ep.load(), path or ".").splitlines()
        else:
            res = command(path)
        if res:
            return res
    return []

def infer_version(dist: setuptools.Distribution):
    trace(
        "finalize hook",
        vars(dist.metadata),
    )
    dist_name = dist.metadata.name
    try:
        config = Configuration.from_file(dist_name=dist_name)
    except FileNotFoundError as e:
        warnings.warn(str(e))
    else:
        dist.metadata.version = _get_version(config)


import os
import sys

import numpy as np
import cv2

def main(dir):
    face_cascade = cv2.CascadeClassifier('/facerec/project/thesis/static/haar_cascades/haarcascade_frontalface_alt2.xml')
    for i in os.listdir(dir):
        imgfile = os.path.join(dir, i)
        imgfilename, imgextension = os.path.splitext(imgfile)
        img = cv2.imread(imgfile)
        gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

        faces = face_cascade.detectMultiScale(gray, 1.1, 8)
        if len(faces) > 0:
            (x, y ,w, h) = faces[0]
            roi_face = img[y:y+h, x:x+w]
            cv2.imwrite(imgfile, roi_face)

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


import gevent
from gevent.local import local

'''
Gevent also allows you to specify data which is local to the greenlet context.
Internally, this is implemented as a global lookup which addresses a private
namespace keyed by the greenlet's getcurrent() value.
'''
stash = local()
stash.globalstash = 0

def f1():
	stash.f1 = 1
	print(stash.f1, stash.__dict__)

def f2():
	stash.f2 = 2
	print(stash.f2, stash.__dict__)

	try:
		stash.f1
	except AttributeError as e:
		print('f1 is not local to f2: {0} ({1})'.format(e, stash.__dict__))

def main():
	gevent.joinall([
		gevent.spawn(f1),
		gevent.spawn(f2)
	])

	print(stash.__dict__)

if __name__ == '__main__': main()