# -*- coding: utf-8 -*-
"""
Created on Sun May 10 10:35:14 2020

@author: Santosh
"""

import cv2
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm

#loading the image
img = cv2.imread('Nadia_Murad.jpg')

# importing the cascade classifier (putting it the the desired directory)
eye_cascade = cv2.CascadeClassifier('haarcascades/haarcascade_eye.xml')

#function to detect eyes
def detect_eyes(img):

    face_img = img.copy()

    # returns coordinates of starting point & w , h of detetcted eyes
    eyes_rects = eye_cascade.detectMultiScale(face_img, scaleFactor = 1.2, minNeighbors=5)

    #drawing the rectangle
    for (x,y,w,h) in eyes_rects:
        cv2.rectangle(face_img, (x,y), (x+w,y+h), (255,0,0), 10 )

    return face_img

#now call the function to detect eyes
detected_faces = detect_eyes(img)

#displaying detected faces
while True:

    cv2.imshow('Detected Faces', detected_faces)

    k = cv2.waitKey(1)
    if k == 27:
        break

cv2.destroyAllWindows()


#!/usr/bin/env python3
# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.

from typing import Any, Dict, Optional, Tuple

import numpy as np
from ax.core.observation import ObservationFeatures
from ax.modelbridge.base import ModelBridge
from ax.plot.base import AxPlotConfig, AxPlotTypes, PlotData
from ax.plot.color import BLUE_SCALE, GREEN_PINK_SCALE, GREEN_SCALE
from ax.plot.helper import (
    TNullableGeneratorRunsDict,
    get_fixed_values,
    get_grid_for_parameter,
    get_plot_data,
    get_range_parameter,
    get_range_parameters,
)

# type aliases
ContourPredictions = Tuple[
    PlotData, np.ndarray, np.ndarray, np.ndarray, np.ndarray, Dict[str, bool]
]

def _get_contour_predictions(
    model: ModelBridge,
    x_param_name: str,
    y_param_name: str,
    metric: str,
    generator_runs_dict: TNullableGeneratorRunsDict,
    density: int,
    slice_values: Optional[Dict[str, Any]] = None,
) -> ContourPredictions:
    """
    slice_values is a dictionary {param_name: value} for the parameters that
    are being sliced on.
    """
    x_param = get_range_parameter(model, x_param_name)
    y_param = get_range_parameter(model, y_param_name)

    plot_data, _, _ = get_plot_data(model, generator_runs_dict or {}, {metric})

    grid_x = get_grid_for_parameter(x_param, density)
    grid_y = get_grid_for_parameter(y_param, density)
    scales = {"x": x_param.log_scale, "y": y_param.log_scale}

    grid2_x, grid2_y = np.meshgrid(grid_x, grid_y)

    grid2_x = grid2_x.flatten()
    grid2_y = grid2_y.flatten()

    fixed_values = get_fixed_values(model, slice_values)

    param_grid_obsf = []
    for i in range(density ** 2):
        parameters = fixed_values.copy()
        parameters[x_param_name] = grid2_x[i]
        parameters[y_param_name] = grid2_y[i]
        param_grid_obsf.append(ObservationFeatures(parameters))

    mu, cov = model.predict(param_grid_obsf)

    f_plt = mu[metric]
    sd_plt = np.sqrt(cov[metric][metric])
    return plot_data, f_plt, sd_plt, grid_x, grid_y, scales

def plot_contour(
    model: ModelBridge,
    param_x: str,
    param_y: str,
    metric_name: str,
    generator_runs_dict: TNullableGeneratorRunsDict = None,
    relative: bool = False,
    density: int = 50,
    slice_values: Optional[Dict[str, Any]] = None,
    lower_is_better: bool = False,
) -> AxPlotConfig:
    """Plot predictions for a 2-d slice of the parameter space.

    Args:
        model: ModelBridge that contains model for predictions
        param_x: Name of parameter that will be sliced on x-axis
        param_y: Name of parameter that will be sliced on y-axis
        metric_name: Name of metric to plot
        generator_runs_dict: A dictionary {name: generator run} of generator runs
            whose arms will be plotted, if they lie in the slice.
        relative: Predictions relative to status quo
        density: Number of points along slice to evaluate predictions.
        slice_values: A dictionary {name: val} for the fixed values of the
            other parameters. If not provided, then the status quo values will
            be used if there is a status quo, otherwise the mean of numeric
            parameters or the mode of choice parameters.
        lower_is_better: Lower values for metric are better.
    """
    if param_x == param_y:
        raise ValueError("Please select different parameters for x- and y-dimensions.")
    data, f_plt, sd_plt, grid_x, grid_y, scales = _get_contour_predictions(
        model=model,
        x_param_name=param_x,
        y_param_name=param_y,
        metric=metric_name,
        generator_runs_dict=generator_runs_dict,
        density=density,
        slice_values=slice_values,
    )
    config = {
        "arm_data": data,
        "blue_scale": BLUE_SCALE,
        "density": density,
        "f": f_plt,
        "green_scale": GREEN_SCALE,
        "green_pink_scale": GREEN_PINK_SCALE,
        "grid_x": grid_x,
        "grid_y": grid_y,
        "lower_is_better": lower_is_better,
        "metric": metric_name,
        "rel": relative,
        "sd": sd_plt,
        "xvar": param_x,
        "yvar": param_y,
        "x_is_log": scales["x"],
        "y_is_log": scales["y"],
    }
    return AxPlotConfig(config, plot_type=AxPlotTypes.CONTOUR)

def interact_contour(
    model: ModelBridge,
    metric_name: str,
    generator_runs_dict: TNullableGeneratorRunsDict = None,
    relative: bool = False,
    density: int = 50,
    slice_values: Optional[Dict[str, Any]] = None,
    lower_is_better: bool = False,
) -> AxPlotConfig:
    """Create interactive plot with predictions for a 2-d slice of the parameter
    space.

    Args:
        model: ModelBridge that contains model for predictions
        metric_name: Name of metric to plot
        generator_runs_dict: A dictionary {name: generator run} of generator runs
            whose arms will be plotted, if they lie in the slice.
        relative: Predictions relative to status quo
        density: Number of points along slice to evaluate predictions.
        slice_values: A dictionary {name: val} for the fixed values of the
            other parameters. If not provided, then the status quo values will
            be used if there is a status quo, otherwise the mean of numeric
            parameters or the mode of choice parameters.
        lower_is_better: Lower values for metric are better.
    """
    range_parameters = get_range_parameters(model)
    plot_data, _, _ = get_plot_data(model, generator_runs_dict or {}, {metric_name})

    # TODO T38563759: Sort parameters by feature importances
    param_names = [parameter.name for parameter in range_parameters]

    is_log_dict: Dict[str, bool] = {}
    grid_dict: Dict[str, np.ndarray] = {}
    for parameter in range_parameters:
        is_log_dict[parameter.name] = parameter.log_scale
        grid_dict[parameter.name] = get_grid_for_parameter(parameter, density)

    # pyre: f_dict is declared to have type `Dict[str, Dict[str, np.ndarray]]`
    # pyre-fixme[9]: but is used as type `Dict[str, Dict[str, typing.List[]]]`.
    f_dict: Dict[str, Dict[str, np.ndarray]] = {
        param1: {param2: [] for param2 in param_names} for param1 in param_names
    }
    # pyre: sd_dict is declared to have type `Dict[str, Dict[str, np.
    # pyre: ndarray]]` but is used as type `Dict[str, Dict[str, typing.
    # pyre-fixme[9]: List[]]]`.
    sd_dict: Dict[str, Dict[str, np.ndarray]] = {
        param1: {param2: [] for param2 in param_names} for param1 in param_names
    }
    for param1 in param_names:
        for param2 in param_names:
            _, f_plt, sd_plt, _, _, _ = _get_contour_predictions(
                model=model,
                x_param_name=param1,
                y_param_name=param2,
                metric=metric_name,
                generator_runs_dict=generator_runs_dict,
                density=density,
                slice_values=slice_values,
            )
            f_dict[param1][param2] = f_plt
            sd_dict[param1][param2] = sd_plt

    config = {
        "arm_data": plot_data,
        "blue_scale": BLUE_SCALE,
        "density": density,
        "f_dict": f_dict,
        "green_scale": GREEN_SCALE,
        "green_pink_scale": GREEN_PINK_SCALE,
        "grid_dict": grid_dict,
        "lower_is_better": lower_is_better,
        "metric": metric_name,
        "rel": relative,
        "sd_dict": sd_dict,
        "is_log_dict": is_log_dict,
        "param_names": param_names,
    }
    return AxPlotConfig(config, plot_type=AxPlotTypes.INTERACT_CONTOUR)


#!/usr/local/bin/python3

from cgitb import enable
enable()

def show_comments():
    from cgi import FieldStorage
    import pymysql as db
    from os import environ

    user_comments = ''
    url = environ.get('SCRIPT_NAME')
    try:
        connection = db.connect('cs1.ucc.ie','dr13','chujohqu','csdipact2017_dr13')
        cursor = connection.cursor(db.cursors.DictCursor)
        form_data = FieldStorage()
        if len(form_data) != 0:
            username = form_data.getfirst('username')
            new_comment = form_data.getfirst('new_comment')
            cursor.execute("""INSERT INTO user_comments (username, url, comment)
                              VALUES (%s, %s, %s)""", (username, url, new_comment))
            connection.commit()
        cursor.execute("""SELECT * FROM user_comments
                          WHERE url = %s
                          ORDER BY comment_id DESC""", (url))
        for row in cursor.fetchall():
            user_comments += '

%s

%s

' % (row['username'], row['comment'])
        cursor.close()
        connection.close()
    except db.Error:
        user_comments = '
User Comments cannot be displayed. Please try again later.
'

    return """

Did you like the Quiz? Have you ideas for a quiz?

            <form action="%s" method="post">
                <fieldset>
                    <legend>Tell us below</legend>
                    Name:
                    <input type="text" name="username" id="username" />
                    Comment:
                    <textarea name="new_comment" id="new_comment" rows="5" cols="50">
                    </textarea>
                    <input type="submit" id="x"/>
                </fieldset>
            </form>

User Comments

            %s

""" % (url, user_comments)


# -*- coding: utf-8 -*-
from south.utils import datetime_utils as datetime
from south.db import db
from south.v2 import SchemaMigration
from django.db import models

class Migration(SchemaMigration):

    def forwards(self, orm):
        # Adding field 'MainContentArea.language_code'
        db.add_column(u'widgy_i18n_maincontentarea', 'language_code',
                      self.gf('django.db.models.fields.CharField')(default=u'fi', max_length=2),
                      keep_default=False)

        # Adding field 'SidebarArea.language_code'
        db.add_column(u'widgy_i18n_sidebararea', 'language_code',
                      self.gf('django.db.models.fields.CharField')(default=u'fi', max_length=2),
                      keep_default=False)

    def backwards(self, orm):
        # Deleting field 'MainContentArea.language_code'
        db.delete_column(u'widgy_i18n_maincontentarea', 'language_code')

        # Deleting field 'SidebarArea.language_code'
        db.delete_column(u'widgy_i18n_sidebararea', 'language_code')

    models = {
        u'widgy_i18n.i18nlayout': {
            'Meta': {'object_name': 'I18NLayout'},
            'container_classes': ('django.db.models.fields.CharField', [], {'max_length': '255', 'null': 'True', 'blank': 'True'}),
            u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
            'language_code': ('django.db.models.fields.CharField', [], {'default': "u'fi'", 'max_length': '2'})
        },
        u'widgy_i18n.i18nlayoutcontainer': {
            'Meta': {'object_name': 'I18NLayoutContainer'},
            'container_classes': ('django.db.models.fields.CharField', [], {'max_length': '255', 'null': 'True', 'blank': 'True'}),
            u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'})
        },
        u'widgy_i18n.maincontentarea': {
            'Meta': {'object_name': 'MainContentArea'},
            'container_classes': ('django.db.models.fields.CharField', [], {'max_length': '255', 'null': 'True', 'blank': 'True'}),
            u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
            'language_code': ('django.db.models.fields.CharField', [], {'default': "u'fi'", 'max_length': '2'})
        },
        u'widgy_i18n.multilanguagelayout': {
            'Meta': {'object_name': 'MultiLanguageLayout', '_ormbases': [u'widgy_i18n.I18NLayout']},
            u'i18nlayout_ptr': ('django.db.models.fields.related.OneToOneField', [], {'to': u"orm['widgy_i18n.I18NLayout']", 'unique': 'True', 'primary_key': 'True'})
        },
        u'widgy_i18n.multilanguagelayoutcontainer': {
            'Meta': {'object_name': 'MultiLanguageLayoutContainer', '_ormbases': [u'widgy_i18n.I18NLayoutContainer']},
            u'i18nlayoutcontainer_ptr': ('django.db.models.fields.related.OneToOneField', [], {'to': u"orm['widgy_i18n.I18NLayoutContainer']", 'unique': 'True', 'primary_key': 'True'})
        },
        u'widgy_i18n.sidebararea': {
            'Meta': {'object_name': 'SidebarArea'},
            'container_classes': ('django.db.models.fields.CharField', [], {'max_length': '255', 'null': 'True', 'blank': 'True'}),
            u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
            'language_code': ('django.db.models.fields.CharField', [], {'default': "u'fi'", 'max_length': '2'})
        }
    }

    complete_apps = ['widgy_i18n']


import psycopg2
from flask import Flask, jsonify, request
from flask_cors import CORS

app = Flask(__name__)
CORS(app)

conn = psycopg2.connect(
        database="postgres", user="postgres", password="*****", host="*****", port="5432")

@app.route('/rest/anon/tasks',methods=['POST'])
def create():
    cur = conn.cursor()
    data = request.get_json()
    cur.execute("insert into taskthree(performer, description, begintime, endtime, state, title) values(%s,%s,%s,%s,%s,%s)",
             (data['performer'], data['description'], data['begintime'], data['endtime'], data['state'], data['title']))
    conn.commit()
    return "1"

@app.route('/rest/anon/tasks',methods=['GET'])
def read():
    cur = conn.cursor()
    cur.execute(
        "select * from taskthree")
    rows = cur.fetchall()
    l = []
    for row in rows:
        print(row)
        dic= {'id': str(row[0]),'description': str(row[1]),'begintime':str(row[2]),'endtime':str(row[3]),'performer':str(row[4]),'state':str(row[5]),'title':str(row[6])}
        l.append(dic)
    return jsonify(l)

@app.route('/rest/anon/tasks/<string:id>', methods=['PUT'])
def update(id):
    cur = conn.cursor()
    data = request.get_json()

    cur.execute("UPDATE taskthree SET performer = '{}', title = '{}', description = '{}', state = '{}', begintime = '{}', endtime = '{}'  WHERE id = {}"
        .format(data['performer'],data['title'],data['description'],data['state'],data['begintime'],data['endtime'], id))

    conn.commit()
    return "1"

@app.route('/rest/anon/tasks/<string:id>', methods=['DELETE'])
def delete(id):
    cur = conn.cursor()
    # cur.execute("DELETE from tasktwo where id=%s", id)
    cur.execute("DELETE from taskthree where id="+ id)
    conn.commit()
    return "1"

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


import torch.nn as nn
import torch
import torch.nn.functional as F
from parameters import *

class ModelFeat(nn.Module):
    def __init__(self):
        super().__init__()
        # self.encoder = nn.LSTM(input_size = FEATURE_DIM, hidden_size = HIDDEN_SIZE, num_layers = NUM_LAYERS, dropout = LSTM_DROPOUT, batch_first = True)

        self.F = nn.Sequential(
              nn.Linear(FEATURE_DIM, D1_DIM),
              nn.ReLU(),
              nn.Dropout(),
              nn.Linear(D1_DIM, D2_DIM),
              nn.ReLU(),
        )

        self.S = nn.Sequential()

        self.classifier = nn.Sequential(
              nn.Linear(D2_DIM * 2, D2_DIM // 4),
              nn.ReLU(),
              nn.Linear(D2_DIM // 4, 1),
              nn.Sigmoid()
       )

    def forward(self, features, caption):
        f_output = self.F(features)
        s_output = self.S(caption)
        #print(f_output.shape,s_output.shape)
        c_input = torch.cat((f_output, s_output), dim = 1)
        c_output = self.classifier(c_input)
        return c_output


# Run with
#   "blender -b RenderScene.blend -P render_stimuli_single_view.py stimuli_path obj_filename
# Expects an obj file with the 3D model. Specify filename without the
# extension obj.
# Stimuli path is the folder containing obj files.
#   We assume that obj files are under stimuli_path/obj folder.
#
# 15 Oct 2015
# Goker Erdogan

import bpy
from math import pi
import sys

# get stimuli path
spath = sys.argv[6]
# get obj filename
fname = sys.argv[7]
objname = "{0:s}/obj/{1:s}.obj".format(spath, fname)
# get view orientation
rotz = int(sys.argv[8])

# import the 3D model. NOTE the axis mappings.
bpy.ops.import_scene.obj(filepath=objname, axis_forward='-X', axis_up='Z')

# rotate object and render
# rotation around x and y are fixed. we rotate viewpoint around z axis.
# INVERT THE OBJECT (top becomes bottom)
radx = 135.0 * pi / 180.0
# INVERT THE OBJECT (top becomes bottom)
rady = 180.0 * pi / 180.0
print('.'),
radz = rotz * pi / 180.0
bpy.data.objects['Empty'].rotation_euler = (radx, rady, radz)
imgname = "{0:s}/single_view_inverted/{1:s}.png"
bpy.context.scene.render.filepath = imgname.format(spath, fname)
bpy.ops.render.render(write_still=True)


"""schoolproj URL Configuration

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

urlpatterns = [
    path('admin/', admin.site.urls),
    path('home/', views.home_view),
    path('contact/', views.contact_view),
    path('student_info/', views.student_info_view),
    path('enquiry/', views.enquiry_info_view),
    path('ereport/', views.enquiry_report_view),
    path('smupload/', views.SM_upload_view),
    path('rvupload/', views.RV_upload_view),
    path('rlupload/', views.Rlink_upload_view),
    path('auploadset/', views.SM_upload_formset_view),

    path('smreport/', views.SM_report_view),
    path('asreport/', views.AS_report_query_form_view),
    path('rvreport/', views.RV_report_view),
    path('rlreport/', views.Rlink_report_view),
    path('logout/', views.logout_view),
    path('shome/', views.student_home_view),

    path('shomesm/', views.student_home_study_material),
    path('smaths/', views.student_subject_Maths_view), #study material Maths
    path('sscience/', views.student_subject_Science_view), #study material Science
    path('senglish/', views.student_subject_English_view), #study material English
    path('shindi/', views.student_subject_Hindi_view),  #study material Hindi
    path('stelugu/', views.student_subject_Telugu_view), #study material Telugu

    path('sassign/', views.student_assignments_view),
    path('smathsam/', views.student_assignments_maths_view), #assingment  Maths
    path('senglisham/', views.student_assignments_english_view), #assingment  English
    path('sscienceam/', views.student_assignments_science_view), #assingment  Science
    path('shindiam/', views.student_assignments_hindi_view),  #assingment  Hindi
    path('steluguam/', views.student_assignments_telugu_view), #assingment  Telugu

    path('sassignsub/', views.assignment_submission_view),
    path('smathsasm/', views.assignment_submission_maths_view), #assingment submission Maths
    path('senglishasm/', views.assignment_submission_english_view), #assingment submission English
    path('sscienceasm/', views.assignment_submission_science_view), #assingment submission Science
    path('shindiasm/', views.assignment_submission_hindi_view), #assingment submission Hindi
    path('steluguasm/', views.assignment_submission_telugu_view), #assingment submission Telugu

    path('sasreport/', views.assignment_completed_report_view),

    path('testvp/', views.test_video_play_view),

    path('sonline/', views.student_online_view),
    path('accounts/', include('django.contrib.auth.urls')),

]

if settings.DEBUG:
    urlpatterns+=static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)


import gym
import gym_minigrid
import datetime
import numpy as np
import torch
import random
import cv2
import pygame
from memory import Memory
from embedder import Embedder
import os
import copy
import atexit
pygame.init()

## Reinforcement Learning Coach
###
### Handles OpenAI Gym environments
#
class Coach():
    def __init__(self, env, loss_fn, optim, gym=gym, preprocess_func=None, utility_func=None, embed=False, flatten=False, frameskip=1, batch_size=32, cuda=False, ale=False, init_skip=0, mid_skip=0, caution=0.0, render_skip=1):

        self.n_agents = 1

        if utility_func == None:
            raise Exception("Need utility function")
        if preprocess_func == None:
            raise Exception("Need preprocess function")
        if self.n_agents < 1:
            raise Exception("Need at least 1 agent.")
        if self.n_agents > 1:
            raise NotImplementedError

        super(Coach, self).__init__()
        print("Initializing Coach")

        self.ale = ale
        self.name = env
        self.gym = gym
        self.dones = [False for _ in range(self.n_agents)]
        self.length = 0
        self.memory = Memory(self.n_agents)
        self.episode = []
        self.embedder = Embedder()
        self.embedding = embed
        self.flatten = flatten
        self.best_episode = []
        self.p_obs = torch.zeros(1)
        self.values = []
        self.frameskip = frameskip
        self.loss_fn=loss_fn
        self.optim = optim
        self.preview = False
        self.single_step = False
        self.norm = True
        self.render = False
        self.debug_render = False
        self.caution = caution#0.95
        self.life = -1
        self.life = -1
        self.init_skip = init_skip#70
        self.render_frameskip = render_skip
        self.mid_skip = mid_skip#50
        self.steps = 0
        self.batch_size = batch_size
        self.preprocess_frame = preprocess_func
        self.update = utility_func
        self.cuda = cuda

        atexit.register(self.end)
        #create environments
        self.envs = [self.gym.make(self.name) for _ in range(self.n_agents)]
        self.env = 0

        self.actions = self.envs[0].action_space.n

        os.system("toilet {}".format(self.name))

    def weights_init(self, m):
        classname = m.__class__.__name__
        if classname.find('Linear') != -1:
            torch.nn.init.Xavier(m.weight.data, 0.0, 0.05)
            torch.nn.fill(m.weight.bias, 1.0)
        if classname.find('Conv2d') != -1:
            torch.nn.init.Xavier(m.weight.data, 1.0, 0.05)
            torch.nn.fill(m.weight.bias, 0.0)

    def set_agent(self, agent, lr):
        self.agent = agent
        self.weights_init(self.agent)
        self.target_agent = copy.copy(agent)
        print(self.agent)

        if lr > 0.0:
            self.optimizer = self.optim(lr=lr, weight_decay=0.99, params=self.target_agent.parameters())
        self.memory.set_keys(agent.transition_keys)

    def set_rate(self, lr):
        print("Learning rate: {}".format(lr))
        self.optimizer = self.optim(lr=lr, params=self.agent.parameters())

    def replay_episode(self, episode):
        for t in episode:
            t = cv2.resize(t, (512, 512))
            cv2.imshow("replay", t)
            cv2.waitKey(2)

    #reset all environments
    def reset(self):
        if self.agent == None:
            raise("Need agent")
        states = []
        for env in self.envs:
            env.render()

            state=env.reset()
            for _ in range(self.init_skip):
                state,_,_,info = env.step(1)
            states.append(state)

        if self.ale and ("ale.lives" in info.keys()):
            self.life = info["ale.lives"]
            self.life = self.life
        return states

    def end(self):
        print("\nClosing gym")
        [env.close() for env in self.envs]

    #environment step
    def step(self, actions):
        obs = []
        rewards = []
        dones = []
        infos = []
        #take step for each agent/environment
        for i,env in enumerate(self.envs):
            reward = 0.0
            for _ in range(self.frameskip):
                obv, r, done, info = env.step(int(actions[i]))
                reward += r

            obs.append(obv)
            rewards.append(reward)
            dones.append(done)
            infos.append(info)

        #send each return separately
        return obs, rewards, dones, infos

    def get_keys(self):
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                break
            if event.type == pygame.KEYDOWN:
                # gets the key name
                key = pygame.key.name(event.key)
                if key == 'v':
                    self.debug_render = not self.debug_render
                    print("Render {}".format(self.debug_render))
                if key == 'r':
                    self.replay_episode(self.best_episode)
                if key == 'f':
                    if self.render_frameskip == 1:
                        self.render_frameskip = 10
                        print("Frameskip True")
                    else:
                        self.render_frameskip = 1
                        print("Frameskip False")
                if key == 's':
                    self.single_step = not self.single_step
                    self.debug_render = self.single_step
                    print("Render {}".format(self.debug_render))
                    print("Single step {}".format(self.single_step))

    def render_env(self):
        if self.steps % self.render_frameskip == 0:
            for env in self.envs:
                env.render()

    def learn(self, gamma):
        return self.update(gamma, self.memory, self.optimizer)

    def target_net_update(self):
        print("Agent parameter update.")
        self.agent.load_state_dict(self.target_agent.state_dict())

    def run_episode(self, render=False, gamma=0.99, max_steps=100, explore=False):

        self.render = render

        for env in self.envs:
            env._max_episode_steps = max_steps

        self.last_episode = []

        state = self.reset()
        observation = self.preprocess_frame(state)
        steps = np.zeros(self.n_agents)
        rewards = np.expand_dims(np.zeros(self.n_agents),axis=0)
        score = 0
        confidence = 1.0

        for _ in range(max_steps):
            self.get_keys()

            action, memory_info = self.agent.step(observation, self.memory, confidence, self.render, explore, self.single_step)

            confidence = memory_info['confidence']

            obs, reward, done, info = self.step(action)
            score += reward[0]

            if self.render or self.debug_render:
                self.render_env()

            observation = self.preprocess_frame(obs)

            confidence *= self.caution
            confidence += reward[0]
            confidence = max(min(confidence, 1), 0.2)

            self.memory.add_value(("action",action[0]))

            dones = [not d for d in done]

            steps += dones * np.ones(self.n_agents)
            rewards += np.array(dones) * np.array(reward[0])

            if np.prod(done, 0) or (self.ale and info[0]["ale.lives"] < self.life):
                reward[0] = -1.0
                score -= 1
                for i, r in enumerate(reward):
                    self.memory.add_value(("reward", r))

                self.confidence = 1.0
                self.memory.finish_episode(gamma)
                self.life -= 1
                states = []
                for env in self.envs:
                    for _ in range(self.mid_skip):
                        state, _, _, info = env.step(0)
                    states.append(state)
            else:
                for i, r in enumerate(reward):
                    self.memory.add_value(("reward", r))

            if np.prod(done, 0) or self.life == 0:
                break

        self.agent.steps = 0
        return int(score), int(steps)

    def discount(self, gamma):
        R = 0.0

        values = [[] for _ in range(self.n_agents)]
        for step in reversed(self.memory.rewards):
            for i,r in enumerate(step):
                R = r + R * gamma
                values[i].insert(0,R)

        values = torch.tensor(np.array(values))
        return values


import data_helpers as dh
import time
import numpy as np
from collections import Counter
from DCAutoencoder import DCAutoencoder
from DNNetwork import DNNetwork
from Logger import Logger

models_to_train = 11

ae_input_shape = (90,90,3)
ae_dropout_prob = 0.2
ae_load_path = 'best_autoencoder.h5'
ae_batch_size = 64

dn_input_shape = (5,5,64)
dn_nb_classes = 4
dn_dropout_prob = 0.2
dn_batch_size = 64
dn_nb_epochs = 5
dn_valid_freq = 15

logger = Logger()

AE = DCAutoencoder(
		ae_input_shape,
		dropout_prob=ae_dropout_prob,
		load_path=ae_load_path,
		logger=logger
	)

for model_nb in range(models_to_train):
	logger.log('######################\n' +
			   'MODEL ' + str(model_nb+1) + ' of ' + str(models_to_train) +
			   '\n######################')

	DN = DNNetwork(
		input_shape=dn_input_shape,
		output_size=dn_nb_classes,
		dropout_prob=dn_dropout_prob,
		logger=logger
	)

	batches = dh.batch_iterator(
			dn_batch_size,
			dn_nb_epochs,
			'id_train_rot.csv',
			image_size=ae_input_shape[:2],
			nb_classes=dn_nb_classes,
			logger=logger
		)

	valid_batch = batches.next() #First batch generated by the generator is the validation batch and the data in it won't be used in training
	iteration = 0
	# Train the network
	t = time.time() # To monitor execution time
	for batch in batches:
		ids, images, labels = zip(*batch)
		loss, accuracy = DN.train(AE.encode(images), labels)
		iteration += 1
		logger.log(
			'Iteration ' + str(iteration) +
			' - Loss:' + str(loss) +
			'; Accuracy: ' + str(accuracy) +
			' - Elapsed time: ' + str(time.time() - t)
		)
		logger.to_csv('raw_data_'+str(model_nb)+'.csv', [iteration, loss, accuracy, 0])
		# Validation testing
		if not iteration % dn_valid_freq:
			ids, images, labels = zip(*valid_batch)
			loss, accuracy = DN.test(AE.encode(images), labels)
			logger.log(
				'### Validation testing ###\n' +
				'\tLoss:' + str(loss) +
				'; Accuracy: ' + str(accuracy) +
				'\n###')
			logger.to_csv('raw_data_'+str(model_nb)+'.csv', [iteration, loss, accuracy, 1])
		t = time.time()  # To monitor execution time

	# Evaluate test set
	logger.log('Done.\n############################################\nEvaluating test set...')
	batches = dh.batch_iterator(
		dn_batch_size,
		1,
		'sample_submission4.csv',
		image_size=ae_input_shape[:2],
		nb_classes=dn_nb_classes,
		shuffle=False,
		validation=False,
		logger=logger
	)
	output = []
	# Run the network on the test dataset
	for batch in batches:
		ids, images, labels = zip(*batch)
		y = DN.predict(AE.encode(images))
		output.append(zip(ids, y))

	logger.log('Done.\nWriting to csv...')
	output_file = open(logger.path + 'output_'+str(model_nb)+'.csv', 'w')
	output_pred_file = open(logger.path + 'output_pred_'+str(model_nb)+'.csv', 'w')
	output_file.write('Id,label\n')
	for batch in output:
		for prediction in batch:
			output_file.write(str(prediction[0]) + ',' + str(np.argmax(prediction[1]) + 1) + '\n')
			output_pred_file.write(str(prediction[0]) + ',' + ','.join([str(i) for i in prediction[1]]) + '\n')
	output_file.close()
	output_pred_file.close()

predictions = {}
# initialize prediction dictionary
output = dh.parse_csv(logger.path + 'output_0.csv', dn_nb_classes)
for p in output:
	predictions[p[0]] = []

logger.log('Calculating majority voting over all models...')
for model_nb in range(models_to_train):
	output = dh.parse_csv(logger.path + 'output_'+str(model_nb)+'.csv', dn_nb_classes)
	for p in output:
		predictions[p[0]].append(np.argmax(p[1]))

logger.to_csv('weighted_output.csv', ['Id,label'])
for k, v in predictions.iteritems():
	logger.to_csv('weighted_output.csv', [k, Counter(v).most_common(1)[0][0]])
logger.log('Done\n#########################################\n#########################################')


"""
RHEVM3-10951 - Import export domain
https://polarion.engineering.redhat.com/polarion/#/project/RHEVM3/
workitem?id=RHEVM3-10951
"""
import pytest
from rhevmtests.storage import config
from art.rhevm_api.tests_lib.low_level import (
    storagedomains as ll_sd,
    vms as ll_vms,
)
from art.rhevm_api.tests_lib.high_level import storagedomains as hl_sd
from art.test_handler.tools import polarion
from art.test_handler.settings import ART_CONFIG
from art.unittest_lib import (
    tier2,
)
from art.unittest_lib.common import StorageTest as TestCase, testflow
import rhevmtests.storage.helpers as storage_helpers
from rhevmtests.storage.storage_full_import_export.fixtures import (
    remove_export_domain_setup, fetch_golden_template_name
)
from rhevmtests.storage.fixtures import (
    export_template, export_vm, create_vm, remove_vms, create_export_domain,
    remove_export_domain,
)
from rhevmtests.storage.fixtures import remove_vm  # noqa

@pytest.fixture(scope='module', autouse=True)
def deactivate_and_detach_export_domain(request):
    """
    Deactivate and detach storage domain
    """
    def finalizer():
        """
        Attach and active export domain
        """
        testflow.teardown("Attach export domain %s", config.EXPORT_DOMAIN_NAME)
        assert hl_sd.attach_and_activate_domain(
            config.DATA_CENTER_NAME, config.EXPORT_DOMAIN_NAME
        ), "Unable to attach and activate export domain %s" % (
            config.EXPORT_DOMAIN_NAME
        )

    request.addfinalizer(finalizer)
    testflow.teardown("Detach export domain %s", config.EXPORT_DOMAIN_NAME)
    assert hl_sd.detach_and_deactivate_domain(
        config.DATA_CENTER_NAME, config.EXPORT_DOMAIN_NAME,
        engine=config.ENGINE
    ), "Failed to detach export domain %s" % config.EXPORT_DOMAIN_NAME

@pytest.mark.usefixtures(
    create_vm.__name__,
    remove_vms.__name__,
    create_export_domain.__name__,
    remove_export_domain.__name__,
    fetch_golden_template_name.__name__,
    export_template.__name__,
    export_vm.__name__,
    remove_export_domain_setup.__name__,
)
class BaseTestCase(TestCase):
    """
    Base class handle import of a export domain
    """
    exclusive = 'true'

    @polarion("RHEVM3-10951")
    @tier2
    def test_import_export_storage_domain(self):
        """
        Import an export storage domain
        Import the vm
        """
        testflow.step("Import export domain %s", self.export_domain)
        assert ll_sd.addStorageDomain(
            True, host=self.spm, type=config.EXPORT_TYPE,
            **self.storage_domain_kwargs
        ), "Unable to import an export domain %s" % self.export_domain
        testflow.step(
            "Attach export domain %s to data-center %s",
            self.export_domain, config.DATA_CENTER_NAME
        )
        assert ll_sd.attachStorageDomain(
            True, config.DATA_CENTER_NAME, self.export_domain
        ), "Unable to attach export domain %s to data center %s" % (
            self.export_domain, config.DATA_CENTER_NAME
        )
        imported_vm_name = storage_helpers.create_unique_object_name(
            self.__class__.__name__, config.OBJECT_TYPE_VM
        )
        testflow.step(
            "Import VM %s with name %s",
            self.vm_name, imported_vm_name
        )
        assert ll_vms.importVm(
            True, self.vm_name, self.export_domain,
            self.storage_domain, config.CLUSTER_NAME, imported_vm_name
        ), "Unable to import VM %s from storage domain %s" % (
            self.vm_name, self.export_domain
        )
        self.vm_names.append(imported_vm_name)
        testflow.step("Start VM %s", imported_vm_name)
        assert ll_vms.startVm(True, imported_vm_name, config.VM_UP), (
            "Unable to start VM %s after been imported from an imported "
            "storage domain" % imported_vm_name
        )

class TestCase10951_GLUSTER(BaseTestCase):
    __test__ = config.STORAGE_TYPE_GLUSTER in ART_CONFIG['RUN']['storages']
    storages = set([config.STORAGE_TYPE_GLUSTER])

    storage_domain_kwargs = {
        'storage_type': config.STORAGE_TYPE_GLUSTER,
        'vfs_type': config.ENUMS['vfs_type_glusterfs'],
        'address': config.GLUSTER_DOMAINS_KWARGS[0]['address'],
        'path': config.GLUSTER_DOMAINS_KWARGS[0]['path']
    }

class TestCase10951_POSIX(BaseTestCase):
    # Since we don't run with POSIX make sure this test runs in NFS
    __test__ = config.STORAGE_TYPE_NFS in ART_CONFIG['RUN']['storages']
    storages = set([config.STORAGE_TYPE_NFS])

    storage_domain_kwargs = {
        'storage_type': config.STORAGE_TYPE_POSIX,
        'vfs_type': config.STORAGE_TYPE_NFS,
        'address': config.NFS_DOMAINS_KWARGS[0]['address'],
        'path': config.NFS_DOMAINS_KWARGS[0]['path']
    }


import networkx as nx
from matplotlib import pyplot

def main():

    g = nx.star_graph(5)

    # パスを追加する
    g.add_path([5, 6, 7, 8, 9, 10])
    g.add_path([5, 20, 10])

    # 3 から 10 までの最短経路を計算する
    print(nx.shortest_path(g, 3, 10)) #=> [3, 0, 5, 20, 10]

    nx.draw(g)
    pyplot.show()

if __name__ == '__main__':
    main()


import telebot
from telebot.types import InlineKeyboardMarkup, InlineKeyboardButton

import config
import utils

bot = telebot.TeleBot(config.token)

def main_menu_keyboard():
    """Main menu in Telegram bot"""
    main_menu = InlineKeyboardMarkup(row_width=1)  # 1 = each button on a new line
    main_menu.add(InlineKeyboardButton('Send keys', callback_data='send_keys'),
                  InlineKeyboardButton('Which keys are right for me?', callback_data='about_keys'),
                  InlineKeyboardButton('Help', callback_data='help')
                  )
    return main_menu

# Bot commands
@bot.message_handler(commands=['start'])
def send_start(message):
    bot.send_message(message.chat.id, 'bot@nod32keys:~$ main_menu', reply_markup=main_menu_keyboard())

@bot.message_handler(commands=['help'])
def send_help(message):
    bot.send_message(message.chat.id, 'The bot can send keys for the Eset NOD32 antivirus. '
                                      'The keys are updated every 8 hours.\nTo start work, send the /start command')

# Events on pressing menu buttons
@bot.callback_query_handler(func=lambda call: True)
def callback_query(call):
    if call.data == 'send_keys':
        bot.send_message(call.from_user.id, utils.send_keys(), parse_mode='Markdown')
    elif call.data == 'about_keys':
        bot.send_message(call.from_user.id, utils.ABOUT_KEYS_MESSAGE, parse_mode='Markdown')
    elif call.data == 'help':
        bot.send_message(call.from_user.id, 'The bot can send keys for the Eset NOD32 antivirus. '
                                            'The keys are updated every 8 hours.\nTo start work, send the /start command')

# Directs to the menu when entering unfamiliar text
@bot.message_handler(content_types=['text'])
def answer_unknown_text(message):
    bot.send_message(message.from_user.id, 'I don\'t understand you. To call the menu write /start')

if __name__ == "__main__":
    bot.polling()


#!/usr/bin/env python
# -*- coding: utf-8 -*-
import threading
from datetime import datetime
from time import sleep
from logging import getLogger, config
import orders
import price_ladders
import configparser

# ログ設定
config.fileConfig('./config/logging.conf')
logger = getLogger(__name__)

# 定期的に実行する処理
def interval_task():
    price.print_price()

# メイン
if __name__ == "__main__":
  logger.info('start.')
  # 設定値取得
  config_ini = configparser.ConfigParser()
  config_ini.read('config/config.ini', encoding='utf-8')
  read_default = config_ini['DEFAULT']
  token = read_default.get('token')
  secret = read_default.get('secret')
  # Price取得
  price = price_ladders.PriceLadders()
  price.set_liquidtap_price()
  # Order取得
  order = orders.Orders(token, secret)
  order.set_liquidtap_orders()
  # 基準時刻を作る
  base_timing = datetime.now()
  interval_sec = 1
  # 無限ループ
  while True:
    try:
      # 基準時刻と現在時刻の剰余を元に、次の実行までの時間を計算する
      current_timing = datetime.now()
      elapsed_sec = (current_timing - base_timing).total_seconds()
      sleep_sec = interval_sec - (elapsed_sec % interval_sec)
      # 次の実行までsleep
      sleep(max(sleep_sec, 0))
      # スレッド実行
      t = threading.Thread(target=interval_task)
      t.start()
    # キーボード中断
    except KeyboardInterrupt:
      logger.info("Ctrl+Cで停止しました")
      break
    # その他のエクセプション
    except:
      pass
  logger.info('end.')


import uuid
from datetime import datetime
from decimal import Decimal
from flask_script import Command, Manager, Option

from app import db
from app.dao.monthly_billing_dao import (
    create_or_update_monthly_billing,
    get_monthly_billing_by_notification_type,
    get_service_ids_that_need_billing_populated
)
from app.models import PROVIDERS, User, SMS_TYPE, EMAIL_TYPE
from app.dao.services_dao import (
    delete_service_and_all_associated_db_objects,
    dao_fetch_all_services_by_user
)
from app.dao.provider_rates_dao import create_provider_rates
from app.dao.users_dao import (delete_model_user, delete_user_verify_codes)

class CreateProviderRateCommand(Command):

    option_list = (
        Option('-p', '--provider_name', dest="provider_name", help='Provider name'),
        Option('-c', '--cost', dest="cost", help='Cost (pence) per message including decimals'),
        Option('-d', '--valid_from', dest="valid_from", help="Date (%Y-%m-%dT%H:%M:%S) valid from")
    )

    def run(self, provider_name, cost, valid_from):
        if provider_name not in PROVIDERS:
            raise Exception("Invalid provider name, must be one of ({})".format(', '.join(PROVIDERS)))

        try:
            cost = Decimal(cost)
        except:
            raise Exception("Invalid cost value.")

        try:
            valid_from = datetime.strptime('%Y-%m-%dT%H:%M:%S', valid_from)
        except:
            raise Exception("Invalid valid_from date. Use the format %Y-%m-%dT%H:%M:%S")

        create_provider_rates(provider_name, valid_from, cost)

class PurgeFunctionalTestDataCommand(Command):

    option_list = (
        Option('-u', '-user-email-prefix', dest='user_email_prefix', help="Functional test user email prefix."),
    )

    def run(self, service_name_prefix=None, user_email_prefix=None):
        if user_email_prefix:
            users = User.query.filter(User.email_address.like("{}%".format(user_email_prefix))).all()
            for usr in users:
                # Make sure the full email includes a uuid in it
                # Just in case someone decides to use a similar email address.
                try:
                    uuid.UUID(usr.email_address.split("@")[0].split('+')[1])
                except ValueError:
                    print("Skipping {} as the user email doesn't contain a UUID.".format(usr.email_address))
                else:
                    services = dao_fetch_all_services_by_user(usr.id)
                    if services:
                        for service in services:
                            delete_service_and_all_associated_db_objects(service)
                    else:
                        delete_user_verify_codes(usr)
                        delete_model_user(usr)

class CustomDbScript(Command):

    option_list = (
        Option('-n', '-name-of-db-function', dest='name_of_db_function', help="Function name of the DB script to run"),
    )

    def run(self, name_of_db_function):
        db_function = getattr(self, name_of_db_function, None)
        if callable(db_function):
            db_function()
        else:
            print('The specified function does not exist.')

    def backfill_notification_statuses(self):
        """
        This will be used to populate the new `Notification._status_fkey` with the old
        `Notification._status_enum`
        """
        LIMIT = 250000
        subq = "SELECT id FROM notification_history WHERE notification_status is NULL LIMIT {}".format(LIMIT)
        update = "UPDATE notification_history SET notification_status = status WHERE id in ({})".format(subq)
        result = db.session.execute(subq).fetchall()

        while len(result) > 0:
            db.session.execute(update)
            print('commit {} updates at {}'.format(LIMIT, datetime.utcnow()))
            db.session.commit()
            result = db.session.execute(subq).fetchall()

    def update_notification_international_flag(self):
        # 250,000 rows takes 30 seconds to update.
        subq = "select id from notifications where international is null limit 250000"
        update = "update notifications set international = False where id in ({})".format(subq)
        result = db.session.execute(subq).fetchall()

        while len(result) > 0:
            db.session.execute(update)
            print('commit 250000 updates at {}'.format(datetime.utcnow()))
            db.session.commit()
            result = db.session.execute(subq).fetchall()

        # Now update notification_history
        subq_history = "select id from notification_history where international is null limit 250000"
        update_history = "update notification_history set international = False where id in ({})".format(subq_history)
        result_history = db.session.execute(subq_history).fetchall()
        while len(result_history) > 0:
            db.session.execute(update_history)
            print('commit 250000 updates at {}'.format(datetime.utcnow()))
            db.session.commit()
            result_history = db.session.execute(subq_history).fetchall()

    def fix_notification_statuses_not_in_sync(self):
        """
        This will be used to correct an issue where Notification._status_enum and NotificationHistory._status_fkey
        became out of sync. See 979e90a.

        Notification._status_enum is the source of truth so NotificationHistory._status_fkey will be updated with
        these values.
        """
        MAX = 10000

        subq = "SELECT id FROM notifications WHERE cast (status as text) != notification_status LIMIT {}".format(MAX)
        update = "UPDATE notifications SET notification_status = status WHERE id in ({})".format(subq)
        result = db.session.execute(subq).fetchall()

        while len(result) > 0:
            db.session.execute(update)
            print('Committed {} updates at {}'.format(len(result), datetime.utcnow()))
            db.session.commit()
            result = db.session.execute(subq).fetchall()

        subq_hist = "SELECT id FROM notification_history WHERE cast (status as text) != notification_status LIMIT {}" \
            .format(MAX)
        update = "UPDATE notification_history SET notification_status = status WHERE id in ({})".format(subq_hist)
        result = db.session.execute(subq_hist).fetchall()

        while len(result) > 0:
            db.session.execute(update)
            print('Committed {} updates at {}'.format(len(result), datetime.utcnow()))
            db.session.commit()
            result = db.session.execute(subq_hist).fetchall()

    def link_inbound_numbers_to_service(self):
        update = """
        UPDATE inbound_numbers SET
        service_id = services.id,
        updated_at = now()
        FROM services
        WHERE services.sms_sender = inbound_numbers.number AND
        inbound_numbers.service_id is null
        """
        result = db.session.execute(update)
        db.session.commit()

        print("Linked {} inbound numbers to service".format(result.rowcount))

class PopulateMonthlyBilling(Command):
        option_list = (
            Option('-y', '-year', dest="year", help="Use for integer value for year, e.g. 2017"),
        )

        def run(self, year):
            service_ids = get_service_ids_that_need_billing_populated(
                start_date=datetime(2016, 5, 1), end_date=datetime(2017, 8, 16)
            )
            start, end = 1, 13
            if year == '2016':
                start = 4

            for service_id in service_ids:
                print('Starting to populate data for service {}'.format(str(service_id)))
                print('Starting populating monthly billing for {}'.format(year))
                for i in range(start, end):
                    print('Population for {}-{}'.format(i, year))
                    self.populate(service_id, year, i)

        def populate(self, service_id, year, month):
            create_or_update_monthly_billing(service_id, datetime(int(year), int(month), 1))
            sms_res = get_monthly_billing_by_notification_type(
                service_id, datetime(int(year), int(month), 1), SMS_TYPE
            )
            email_res = get_monthly_billing_by_notification_type(
                service_id, datetime(int(year), int(month), 1), EMAIL_TYPE
            )
            print("Finished populating data for {} for service id {}".format(month, str(service_id)))
            print('SMS: {}'.format(sms_res.monthly_totals))
            print('Email: {}'.format(email_res.monthly_totals))


from game_object import GameObject
from pygame import Rect, Surface, Vector2, draw

# Définition de la classe Ball
class Ball(GameObject):

    # Constructeur de la classe
    def __init__(self, pos=Vector2(0, 0)):
        # La position de la balle (Vecteur)
        self.pos = pos
        # La direction de la balle (Vecteur normalisé)
        self.dir = Vector2(1, 1)
        # La vitesse de déplacement de la balle
        self.speed = 0.1
        # Le rayon de la balle en pixel
        self.size = 5

    # Méthode d'initialisation de l'objet, à exécuter une fois au début
    def init(self, screen: Surface):
        self.screen = screen
        self.pos = Vector2(screen.get_width()/2, screen.get_height()/2)

    # Méthode de mise à jour de l'objet, à exécuter à chaque image
    def update(self):

        self.pos += self.dir*self.speed

        if self.pos.x >= 800 and self.dir.y == 1:
            self.dir = Vector2(-1,1)
        if self.pos.x >= 800 and self.dir.y == -1:
            self.dir = Vector2(-1,-1)

        if self.pos.x <= 0 and self.dir.y == 1:
            self.dir = Vector2(1,1)
        if self.pos.x <= 0 and self.dir.y == -1:
            self.dir = Vector2(1,-1)

        if self.pos.y >= 600 and self.dir.x == 1:
            self.dir = Vector2(1,-1)
        if self.pos.y >= 600 and self.dir.x == -1:
            self.dir = Vector2(-1,-1)

        if self.pos.y <= 0 and self.dir.x == 1:
            self.dir = Vector2(1,1)
        if self.pos.y <= 0 and self.dir.x == -1:
            self.dir = Vector2(-1,1)

        # Après on la dessine
        draw.circle(self.screen, (255, 255, 255), self.pos, self.size)

    # Renvoi un rectangle aux même dimensions et position que la balle
    def as_rect(self):
        return Rect(self.pos, (self.size, self.size))


from selenium.webdriver.support.ui import WebDriverWait
from appium.webdriver.common.mobileby import MobileBy
from selenium.webdriver.support import expected_conditions as ec

# Importing BaseScreen class
from .base_screen import *
import time

# ------------------------------------------------------------------------------------------------------

class TutorialScreen(BaseScreen):

    def view_full_tutorial(self):
        view_full_tutorial_button_location = (MobileBy.ID, 'com.migrainemonitor.dev:id/btn_full_tutorial')
        view_full_tutorial_button = WebDriverWait(self.driver, 20).until(ec.element_to_be_clickable(view_full_tutorial_button_location))
        view_full_tutorial_button.click()

    def home_screen_navigation_tutorial(self):
        home_screen_navigation_tutorial_button_location = (MobileBy.ID, 'com.migrainemonitor.dev:id/btn_home_tutorial')
        home_screen_navigation_tutorial_button = WebDriverWait(self.driver, 20).until(ec.element_to_be_clickable(home_screen_navigation_tutorial_button_location))
        home_screen_navigation_tutorial_button.click()

    def entering_headache_tutorial(self):
        entering_headache_tutorial_button_location = (MobileBy.ID, 'com.migrainemonitor.dev:id/btn_headache_tutorial')
        entering_headache_tutorial_button = WebDriverWait(self.driver, 20).until(ec.element_to_be_clickable(entering_headache_tutorial_button_location))
        entering_headache_tutorial_button.click()

    def generating_report_tutorial(self):
        generating_report_tutorial_button_location = (MobileBy.ID, 'com.migrainemonitor.dev:id/btn_reports_tutorial')
        generating_report_tutorial_button = WebDriverWait(self.driver, 20).until(ec.element_to_be_clickable(generating_report_tutorial_button_location))
        generating_report_tutorial_button.click()

    def close_popup(self):

        # Closing pop-up window
        close_button_location = (MobileBy.ID, 'com.migrainemonitor.dev:id/iv_close')
        close_button = WebDriverWait(self.driver, 20).until(ec.element_to_be_clickable(close_button_location))
        close_button.click()


import pymongo
from luigi.mongo import MongoTarget
import datetime
import unittest

host = 'localhost'
port = 27017
database = 'testdatabase'
collection = 'testcollection'

class MongoTargetTest(unittest.TestCase):
    """ testing the mongo-target extension """
    def setUp(self):
        self.target = MongoTarget(database, collection)
        self.doc = { "timestamp":datetime.datetime.now() }

    def test_insert(self):
        self.target.insert(self.doc)

    def test_exists(self):
        self.target.exists()


from requester import Requester
from common import config_reader, config_intervaltime
import post
import time
import rediscache
import logging

class TiebaCrawler(Requester):
    """ Post crawler  , gather information of posts in given bar
        can't get image submmited in post
    """

    def __init__(self, tieba_name="steam", cookie=None):
        """Initialize Crawler
        :param tieba_name:
        :param cookie:
        """
        Requester.__init__(self, tieba_name, cookie)

        self.__available_check()

    def __available_check(self):
        """
        Checking cookie available , tieba existence , manage rights validity
        :return boolean:
        """
        logging.info("Doing available_check")

        response = self.get_content(self.tieba_base)

        if "参与本吧讨论请先" in response:
            logging.warning("Doing available_check : FAILED")
            raise Exception("User Cookie not available")

        logging.info("Checked")

        logging.info("Checking tieba existence......")
        if "尚未建立" in response:
            logging.warning("Checking tieba existence : FAILED")
            raise Exception("Tieba doesn't exist")

        logging.info("Checked")

        logging.info("All check done")
        return True

    def get_posts(self):
        """Get all posts on first page of tieba , and generate a post objects list
        :return list of Post object:
        """
        soup = self.get_content(self.tieba_base)

        post_a = self.__get_posts_a(soup)

        url_list = [self.url_base + tag.get('href') for tag in post_a]

        post_dict = self.__get_content_list(url_list)
        post_list = [post.Post(url, soup) for url, soup in post_dict.items()]

        return post_list

    @rediscache.postcache
    def __get_content_list(self, url_list):
        """Get post content with given url list
        :param url_list:
        :return dict:
        """
        content_list = {}

        for url in url_list:
            content = self.get_content(url)
            if content:
                content_list[url] = content

            time.sleep(config_intervaltime())

        return content_list

    def __get_posts_a(self, soup):
        """Get all post url from the soup of first page of tieba's first page
        :param soup:
        :return list of posts' url:
        """
        posts_list = soup.findAll('div', {'class': 'i'})
        posts_list = [tag.find('a') for tag in posts_list if not tag.find('span', {'class': 'light'})]
        return posts_list

if __name__ == "__main__":
    cookie, _ = config_reader()
    tieba_worker = TiebaCrawler(cookie=cookie, tieba_name='dota2提问')
    posts = tieba_worker.get_posts()
    print(len(list(map(str, posts))))
    posts = tieba_worker.get_posts()
    print(len(list(map(str, posts))))
    # print(list(map(str, posts[0].reply_list)))


""" Read SAC files in 1,2,3 system, correct to N, E, Z and write to new SAC files"""

import pandas as pd
from obspy import read, Stream
from obspy.io.sac import SACTrace
import os
from helper_functions import rotate_to_zne
import sys
from glob import glob

def main(data_dir, output_dir):

    # Get borehole geode info
    stats = pd.read_csv("walkaround_borehole_orientation_stats.csv")

    # Loop over stations/geodes
    for sta in ['BH001', 'BH002', 'BH004', 'BH005', 'BH006', 'BH008', 'BH011', 'BH012', 'BH013', 'BH014', 'BH015', 'BH016', 'BH017', 'BH018']:  # geodes.Geode:

        print("Processing station %s" % sta)
        # Rotation matrix for correction
        inc_corr = - stats.loc[stats['geode'] == sta, 'inclination'].values[0]
        baz_corr = - stats.loc[stats['geode'] == sta, 'mean'].values[0]

        # Read data
        flist1 = glob(os.path.join(data_dir, "8O.%s..DP1*" % sta))
        flist2 = glob(os.path.join(data_dir, "8O.%s..DP2*" % sta))
        flist3 = glob(os.path.join(data_dir, "8O.%s..DP3*" % sta))
        if not flist1 or not flist2 or not flist3:
            continue

        # Look for suffix
        file = os.path.split(flist1[0])[1]
        if file.find("unit") != -1:
            idx = file.find("unit")
            suffix = "_" + file[idx:].split(".")[0]
        else:
            suffix = ""

        st = Stream()
        st += read(os.path.join(data_dir, "8O.%s..DP1*" % sta), format="SAC")[0]
        st += read(os.path.join(data_dir, "8O.%s..DP2*" % sta), format="SAC")[0]
        st += read(os.path.join(data_dir, "8O.%s..DP3*" % sta), format="SAC")[0]

        if len(st) == 0:
            continue

        st.resample(500.0)
        starttime = st[0].stats.starttime
        endtime = st[0].stats.endtime

        # Save rotated coordinates
        st3 = st.copy()
        compN, compE, compZ = rotate_to_zne(st3[0].data, st3[1].data, st3[2].data, inc_corr, baz_corr)
        trN = SACTrace.from_obspy_trace(st3[0])
        trN.data = compN
        trN.kcmpnm = "DPN"
        trN.write(os.path.join(output_dir,
                               "8O.%s..DPN.%s_%s%s.sac" % (sta, starttime.strftime("%Y%m%d%H%M%S"), endtime.strftime("%Y%m%d%H%M%S"), suffix)))
        trE = SACTrace.from_obspy_trace(st3[1])
        trE.data = compE
        trE.kcmpnm = "DPE"
        trE.write(os.path.join(output_dir,
                               "8O.%s..DPE.%s_%s%s.sac" % (sta, starttime.strftime("%Y%m%d%H%M%S"), endtime.strftime("%Y%m%d%H%M%S"), suffix)))
        trZ = SACTrace.from_obspy_trace(st3[2])
        trZ.data = compZ
        trZ.kcmpnm = "DPZ"
        trZ.write(os.path.join(output_dir,
                               "8O.%s..DPZ.%s_%s%s.sac" % (sta, starttime.strftime("%Y%m%d%H%M%S"), endtime.strftime("%Y%m%d%H%M%S"), suffix)))

if __name__ == '__main__':
    folder = sys.argv[1]
    #data_dir = "/home/gilbert_lab/cami_frs/borehole_data/sac_hourly_125Hz/%s" % folder
    data_dir = "/home/gilbert_lab/cami_frs/borehole_data/sac_daily_500Hz/%s" % folder
    output_dir = "/home/gilbert_lab/cami_frs/borehole_data/sac_daily_nez_500Hz/%s" % folder
    if not os.path.exists(output_dir):
        os.makedirs(output_dir)
    main(data_dir, output_dir)


# coding: utf-8
import numpy as np

from sklearn import svm
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import random

"""
采用 sklearn 中的svm ，对数据进行分类
"""
# 导入csv数据
def loadDataSet(data):
    attribute=[]
    for key in data.keys():
        attribute.append(key)
    dataset=[]

    for n in range(len(data[attribute[0]])):
        a=[]

        for key in data.keys():
            a.append(data[key][n])
        dataset.append(a)

    oringal_data=[]
    for i in data.keys():
        oringal_data.append(list(map(eval,data[i])))
    return dataset,oringal_data

def splitDataset(dataset, splitRatio):
	trainSize = int(len(dataset) * splitRatio)
	trainSet = []
	copy = list(dataset)
	while len(trainSet) < trainSize:
		index = random.randrange(len(copy))
		trainSet.append(copy.pop(index))
	return [trainSet, copy]

def main(data,penalty,split):
    dataSet,oringal_data = loadDataSet(data)
    trainingSet, testSet = splitDataset(dataSet, split)

    data=[]
    label=[]
    data_test=[]
    label_test=[]
    for i in range(len(trainingSet)):
        data.append(trainingSet[i][:-1])
        label.append(trainingSet[i][-1])
    for n in range(len(testSet)):
        data_test.append(testSet[n][:-1])
        label_test.append(testSet[n][-1])

    cls = svm.SVC(kernel="linear", C=penalty)
    cls.fit(data, label)
    score = cls.score(data_test,label_test)
    sv_idx = cls.support_  # 支持向量索引

    global w
    w = cls.coef_  # 方向向量 W

    global b
    b = cls.intercept_

    global ax
    ax = plt.subplot(111, projection='3d') # 创建一个3d的画布
    x = np.arange(min(oringal_data[0]),max(oringal_data[0]),0.1)
    y = np.arange(min(oringal_data[1]),max(oringal_data[1]),0.1)
    x, y = np.meshgrid(x, y) # 将向量x和y定义的区域转换成矩阵X和Y
    z = (w[0,0]*x + w[0,1]*y + b) / (-w[0,2]) # 构建分类平面表达式

    surf = ax.plot_surface(x, y, z, rstride=5, cstride=5, alpha=0.6)

    # 绘制三维散点图
    x_array = np.array(data, dtype=float)
    y_array = np.array(label, dtype=int)
    setosa = x_array[np.where(y_array==1)]
    versicolor = x_array[np.where(y_array==0)]
    ax.scatter(setosa[:,0], setosa[:,1], setosa[:,2], c='r', label='setosa')
    ax.scatter(versicolor[:,0], versicolor[:,1], versicolor[:,2], c='b', label='versicolor')
    X = np.array(data,dtype=float)
    for i in range(len(sv_idx)):
        ax.scatter(X[sv_idx[i],0], X[sv_idx[i],1], X[sv_idx[i],2],s=50,
                   c='',marker='o', edgecolors='g')

    ax.set_zlabel('Z')    # 坐标轴
    ax.set_ylabel('Y')
    ax.set_xlabel('X')
    ax.set_zlim([min(oringal_data[2]),max(oringal_data[2])])
    plt.legend(loc='upper left')

    ax.view_init(35,300)
    plt.savefig('E:/Anaconda/Scripts/CorsApi/snippets/static/picture/svm.jpg',format='jpg')
    return score

def predict(svm_predict):
    # 以下为用户输入

    z1 = (w[0, 0] * svm_predict[0] + w[0, 1] * svm_predict[1] + b) / (-w[0, 2])
    if svm_predict[2]>z1:
        User_y = [0]
        x_array = np.array(svm_predict, dtype=float)
        ax.scatter(svm_predict[0], svm_predict[1], svm_predict[2], c='b', marker='x',s=30)
    else:
        User_y = [1]
        x_array = np.array(svm_predict, dtype=float)
        ax.scatter(svm_predict[0], svm_predict[1], svm_predict[2], c='r', marker='x',s=30)

    plt.savefig('E:/Anaconda/Scripts/CorsApi/snippets/static/picture/svm_predict.jpg',format='jpg')


# Copyright 2017 Bracket Computing, Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License").
# You may not use this file except in compliance with the License.
# A copy of the License is located at
#
# https://github.com/brkt/brkt-cli/blob/master/LICENSE
#
# or in the "license" file accompanying this file. This file is
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
# CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and
# limitations under the License.

import base64
import inspect
import os
import unittest

from brkt_cli import (
    brkt_env_from_domain,
    make_user_data
)
from brkt_cli.config import CLIConfig
from brkt_cli.instance_config_args import instance_config_args_to_values

class TestMakeUserData(unittest.TestCase):

    def setUp(self):
        super(TestMakeUserData, self).setUp()
        name_fields = self.id().split('.')
        self.test_name = name_fields[-1]
        my_filename = inspect.getfile(inspect.currentframe())
        my_dir = os.path.dirname(my_filename)
        self.testdata_dir = os.path.join(my_dir, 'testdata')
        self.maxDiff = None # show full diff with knowngood multi-line strings

    def check_output_vs_knowngood(self, output, knowngood_file):
        self.assertTrue(os.path.exists(knowngood_file), msg=
            'knowngood file does not exist: %s\ntest_output:\n---\n%s\n---\n'
            % (knowngood_file, output)
        )
        with open(knowngood_file, 'r') as f:
            knowngood = f.read()

        self.assertMultiLineEqual(output, knowngood)

    def run_cmd(self, values):
        config = CLIConfig()
        env = brkt_env_from_domain('foo.com')
        config.set_env('test', env)
        config.set_current_env('test')

        output = make_user_data.make(values, config)

        knowngood_file = os.path.join(self.testdata_dir,
                                      self.test_name + ".out")
        self.check_output_vs_knowngood(output, knowngood_file)
        return output

    def _init_values(self):
        values = instance_config_args_to_values('')
        values.make_user_data_brkt_files = None
        values.make_user_data_guest_fqdn = None
        values.make_user_data_guest_files = None
        values.unencrypted_guest = False
        values.ssh_public_key_file = None
        values.base64 = False
        return values

    def test_token_and_one_brkt_file(self):
        values = self._init_values()
        values.token = 'THIS_IS_NOT_A_JWT'
        infile = os.path.join(self.testdata_dir, 'logging.yaml')
        values.make_user_data_brkt_files = [ infile ]
        self.run_cmd(values)

    def test_add_one_brkt_file(self):
        values = self._init_values()
        infile = os.path.join(self.testdata_dir, 'logging.yaml')
        values.make_user_data_brkt_files = [ infile ]
        self.run_cmd(values)

    def test_add_one_binary_brkt_file(self):
        values = self._init_values()
        infile = os.path.join(self.testdata_dir, 'rand_bytes.bin')
        values.make_user_data_brkt_files = [ infile ]
        self.run_cmd(values)

    def test_proxy_and_one_brkt_file(self):
        values = self._init_values()
        values.proxies = [ '10.2.3.4:3128' ]
        infile = os.path.join(self.testdata_dir, 'colors.json')
        values.make_user_data_brkt_files = [ infile ]
        self.run_cmd(values)

    def test_add_two_brkt_files(self):
        values = self._init_values()
        infile1 = os.path.join(self.testdata_dir, 'logging.yaml')
        infile2 = os.path.join(self.testdata_dir, 'colors.json')
        values.make_user_data_brkt_files = [ infile1, infile2 ]
        self.run_cmd(values)

    def test_guest_fqdn(self):
        values = self._init_values()
        values.make_user_data_guest_fqdn = 'instance.foo.bar.com'
        self.run_cmd(values)

    def test_guest_userdata(self):
        values = self._init_values()
        infile = os.path.join(self.testdata_dir, 'user-script')
        values.make_user_data_guest_files = [ infile + ':x-shellscript' ]
        self.run_cmd(values)

    def test_brkt_files_with_guest_userdata(self):
        values = self._init_values()
        values.token = 'THIS_IS_NOT_A_JWT'
        infile = os.path.join(self.testdata_dir, 'logging.yaml')
        values.make_user_data_brkt_files = [ infile ]
        infile1 = os.path.join(self.testdata_dir, 'user-script')
        infile2 = os.path.join(self.testdata_dir, 'cloud-config')
        values.make_user_data_guest_files = [
            infile1 + ':x-shellscript', infile2 + ':cloud-config' ]
        self.run_cmd(values)

    def test_ssh_public_key(self):
        values = self._init_values()
        values.ssh_public_key_file = os.path.join(
            self.testdata_dir, 'pub-key')
        self.run_cmd(values)

    def test_unencrypted_guest(self):
        values = self._init_values()
        values.unencrypted_guest = True
        self.run_cmd(values)

    def test_encrypted_guest_and_service_domain(self):
        values = self._init_values()
        values.service_domain = 'stage.mgmt.brkt.com'
        self.run_cmd(values)

    def test_encrypted_guest_and_service_domain_and_proxy(self):
        values = self._init_values()
        values.service_domain = 'stage.mgmt.brkt.com'
        values.proxies = ['10.2.3.4:3128']
        self.run_cmd(values)

    def test_base64(self):
        values = self._init_values()
        values.base64 = True
        # Throw in a brkt-file, for good measure.
        infile = os.path.join(self.testdata_dir, 'logging.yaml')
        values.make_user_data_brkt_files = [ infile ]
        output = self.run_cmd(values)
        decoded_output = base64.b64decode(output)
        decoded_knowngood_file = os.path.join(self.testdata_dir,
                                      self.test_name + ".out.decoded")
        self.check_output_vs_knowngood(decoded_output,
                                       decoded_knowngood_file)


import sys
sys.path.append('..')
from backtest import *
from lib import dataapi
import pandas as pd
import scipy.stats as st
from sklearn.preprocessing import StandardScaler
import warnings
warnings.filterwarnings("ignore")

ss = StandardScaler()

#显示所有列
pd.set_option('display.max_columns', None)
#显示所有行
pd.set_option('display.max_rows', None)

data=pd.read_csv('btcusdt_'+ "result_df_all_all" +'_stat.csv', index_col=0)

data = data.replace([-np.inf, np.inf], 0).fillna(value=0)
eva_columns=["ann_ret", 'max_drawdown', 'annualVolatility', 'win_r', "sharpe"]
data_eva=data
data_eva.index=data_eva["param"]

data_eva=data_eva[eva_columns]

data_eva["max_drawdown_inverse"]=1/data_eva['max_drawdown']
data_eva['annualVolatility_inverse']=1/data_eva['annualVolatility']
data_eva = data_eva.replace([-np.inf, np.inf], 0).fillna(value=0)
data_eva[["ann_ret",'max_drawdown_inverse','annualVolatility_inverse','win_r']]=ss.fit_transform(data_eva[["ann_ret",'max_drawdown_inverse','annualVolatility_inverse','win_r']])
data_eva["sum"]=data_eva[["ann_ret",'max_drawdown_inverse','annualVolatility_inverse','win_r']].apply(lambda x: x.sum(), axis=1)

param_list=list(data_eva.index)
params_filter=[s[:s.rfind("_")] for s in param_list]
params_filter=list(set(params_filter))

column_list=["sum_hvlv",'sum_hv','sum_lv','ann_ret_hv','ann_ret_lv','win_r_hv','win_r_lv','sharpe_hv','sharpe_lv','max_drawdown_hv','max_drawdown_lv','annualVolatility_hv','annualVolatility_lv']
value_list=[]
for x in params_filter:
    sum_hvlv=data_eva.ix[[x+"_hv",x+'_lv']]['sum'].sum()
    sum_hv=data_eva.ix[x+'_hv']['sum']
    sum_lv=data_eva.ix[x+'_lv']['sum']
    ann_ret_hv=data_eva.ix[x+'_hv']['ann_ret']
    ann_ret_lv = data_eva.ix[x + '_lv']['ann_ret']
    win_r_hv=data_eva.ix[x+'_hv']['win_r']
    win_r_lv = data_eva.ix[x + '_lv']['win_r']
    sharp_hv=data_eva.ix[x + '_hv']['sharpe']
    sharp_lv = data_eva.ix[x + '_lv']['sharpe']
    max_drawdown_hv=data_eva.ix[x + '_hv']['max_drawdown']
    max_drawdown_lv = data_eva.ix[x + '_lv']['max_drawdown']
    annualVolatility_hv=data_eva.ix[x + '_hv']['annualVolatility']
    annualVolatility_lv = data_eva.ix[x + '_lv']['annualVolatility']
    value_list.append([sum_hvlv,sum_hv,sum_lv,ann_ret_hv,ann_ret_lv,win_r_hv,win_r_lv,sharp_hv,sharp_lv,max_drawdown_hv,max_drawdown_lv,annualVolatility_hv,annualVolatility_lv])

df=pd.DataFrame(value_list,index=params_filter,columns=column_list)
#print(df.sort_values(by='sum_hvlv',ascending=False))

df=df.sort_values(by='sum_hvlv',ascending=False)
#df.to_csv("all_factor_eva.csv")
#print(df[(df["sharpe_hv"]>1) | (df["sharpe_lv"]>1)])
print(len(df[(df["sharpe_hv"]>1) | (df["sharpe_lv"]>1)]))

result_df=df[(df["sharpe_hv"]>1) | (df["sharpe_lv"]>1)]
print(result_df.head())

index_list=list(result_df.index)
print(index_list)
alpha_list=[]
param_list=[]
for x in index_list:
    if "rank" in x:
        alpha_list.append(x[:x.find("_")])
        param_list.append(x[:x.rfind("_")])
    else:
        alpha_list.append(x[:x.find("_")]+"_gtja")
        param_list.append(x[:x.rfind("_")])
print(alpha_list)
print(param_list)
print(len(alpha_list))
print(len(param_list))
aa

result_list=[]
for i in range(len(alpha_list)):
    df_data_x = pd.read_csv('../factor_writedb/btcusdt_' + alpha_list[i] + '.csv', index_col=0)
    factor_x=df_data_x[param_list[i]]
    tem_list=[]
    for v in range(len(alpha_list)):
        df_data_y=pd.read_csv('../factor_writedb/btcusdt_' + alpha_list[v] + '.csv', index_col=0)
        factor_y=df_data_y[param_list[v]]
        df_data_xy=pd.DataFrame({"x":factor_x.values,"y":factor_y.values},index=factor_y.index)
        df_data_xy=df_data_xy.dropna()
        df_data_xy=df_data_xy.replace([-np.inf, np.inf], 0)
        tem_re,_=st.pearsonr(df_data_xy["x"],df_data_xy["y"])
        tem_list.append(tem_re)
    result_list.append(tem_list)
#print(result_list)
df_corr=pd.DataFrame(result_list,columns=index_list,index=index_list)
print(df_corr)
#df_corr.to_csv("corr_all_factor_1.csv")


import logging
import uuid

from django.core.files.base import ContentFile

import requests
from avatar.models import Avatar

from apps.family.models import Family, Parent, Kid

from .models import User

logger = logging.getLogger(__name__)

def create_user(strategy, backend, details, user=None, *args, **kwargs):
    if user:
        return {'is_new': False}

    user = User.objects.create(email=details.get('email'))

    if not backend.invite_code:
        family = Family.objects.create()
        Parent.objects.create(family=family, user=user,
                              first_name=details.get('first_name'),
                              last_name=details.get('last_name'))
    else:
        kid = Kid.objects.get(pk=int(backend.invite_code))
        kid.user = user
        kid.save()

    response = kwargs['response']
    url = 'http://graph.facebook.com/%s/picture?type=large' % response['id']
    avatar_file = ContentFile(requests.get(url).content)
    avatar = Avatar(user=user, primary=True)
    file_name = str(uuid.uuid4())
    avatar.avatar.save(file_name, avatar_file)

    return {
        'is_new': True,
        'user': user
    }


import openpyxl
from Logger import cls_logger

class cls_excel:
    __log_tag = 'excel'
    __inst=None

    def __init__(self):
        self.__workbook__ = None
        self.__sheet_name__ = None
        self.__tcid_column__ = None
        self.__result_column__ = None
        self.__filePath__ = None
        self.__comment_column__=None

    def excel_Initialize(self,filePath,sheet_name,tcid_column,result_column,comment_column):
        if(not self.__inst):
            try:
                self.__inst = cls_excel()
                self.__write_log("cls_excel.__init__(), excel file path =" + filePath)
                self.__inst.__workbook__ = openpyxl.load_workbook(filePath)
                self.__write_log(filePath + "is opened")
                self.__inst.__sheet_name__ = sheet_name
                self.__inst.__tcid_column__ = tcid_column
                self.__inst.__result_column__ = result_column
                self.__inst.__comment_column__ = comment_column
                self.__inst.__filePath__ = filePath
            except RuntimeError as error:
                raise RuntimeWarning(message='run excel_Initialize fail: \r\n'+str(error.message))

    def excel_read_cell(self,coordinate,sheetname='Sheet1'):
        '''
        Get the value of excel cell
        :param coordinate: Target cell coordinate, for example: A1,B3,C11...
        :param sheetname: Worksheet name
        :return: value of target cell
        '''
        if(self.__inst is None):
            return None
        else:
            ws = self.__inst.__workbook__.get_sheet_by_name(str(sheetname))
            return ws[str(coordinate)].value

    def excel_write_cell(self,coordinate,inputvalue,sheetname='Sheet1'):
        '''
        Write value to excel cell
        :param coordinate: Target cell coordinate, for example: A1,B3,C11...
        :param inputvalue: Value to write
        :param sheetname: Worksheet name
        :return: None
        '''
        if (self.__inst is None):
            return None
        else:
            self.__write_log("excel_write_cell,worksheet["+str(sheetname)+"]: coordinate= "+str(coordinate)+", inputvalue= "+str(inputvalue))
            ws = self.__inst.__workbook__.get_sheet_by_name(str(sheetname))
            ws[str(coordinate)]=inputvalue
            self.__inst.__workbook__.save(str(self.__inst.__filePath__))
            self.__write_log(str(self.__inst.__filePath__) + ' is updated')

    def excel_update_test_result(self,tcid,test_result,comment=None,startRow=1,endRow=None):
        '''
        Write the testing result of test case to the result column
        :param tcid: Test Case ID
        :param test_result:  Testing result
        :param tcid_column:  The column of Test Case ID
        :param test_result_column: The column of  Testing result
        :param startRow: Starting index of available data
        :param endRow: Last index of available data
        :param sheetName: Data sheet name
        :return: Is updating result success?
        '''
        if (self.__inst is None):
            return False
        else:
            index = self.__search_equal_value_in_column__(tcid,self.__inst.__tcid_column__,startRow,endRow,self.__inst.__sheet_name__)
            if(index is not None):
                result_coordinate = str(self.__inst.__result_column__)+str(index)
                comment_coordinate = str(self.__inst.__comment_column__)+str(index)
                self.__write_log("write value=" + str(test_result) + ' to ' + str(result_coordinate))
                self.excel_write_cell(result_coordinate,test_result,self.__inst.__sheet_name__)
                if comment is not None:
                    self.__write_log("write value=" + str(comment) + ' to ' + str(comment_coordinate))
                    self.excel_write_cell(comment_coordinate, comment, self.__inst.__sheet_name__)
                return True
            else:
                return False

    def __search_equal_value_in_column__(self,keyword,kw_columnName='A',startRow=1,endRow=None,sheetname='Sheet1'):
        '''
        Seach the first value of cell equals to keyword, and return the index of row
        :param keyword: Target value
        :param columnName: Searching target column
        :param startRow: Starting index of search
        :param endRow: Ending index of searching
        :param sheetname: Worksheet name of workbook
        :return: The index of the first matched cell, or return "None" when there are non-matched cell in the searching range.
        '''
        if (self.__inst is None):
            return None
        else:
            self.__write_log("search_equal_value_in_column, keyword="+str(keyword))
            index=None
            ws = self.__inst.__workbook__.get_sheet_by_name(str(sheetname))
            columnName = str(kw_columnName).upper()
            self.__write_log("search_equal_value_in_column, columnName="+str(columnName))
            colIndex = int(ord(columnName)-ord('A'))+1
            self.__write_log("search_equal_value_in_column, colIndex="+str(colIndex))
            if(endRow==None):
                endRow = ws.max_row
                self.__write_log("search_equal_value_in_column, row length = "+str(endRow))
            startCoordinate = str(columnName)+str(startRow)
            endCoordinate = str(columnName)+str(endRow)
            self.__write_log("search_equal_value_in_column, startCoordinate="+str(startCoordinate))
            self.__write_log("search_equal_value_in_column, endCoordinate="+str(endCoordinate))

            for row in ws.iter_rows(str(startCoordinate)+':'+str(endCoordinate)):
                for cell in row:
                    if (cell.value==keyword):
                        index = cell.row
            self.__write_log("search_equal_value_in_column, return value="+str(index))
            return index

    def __write_log(self,message):
        cls_logger.write_log(cls_excel.__log_tag,message)


import numpy as np
from sklearn.metrics.pairwise import cosine_similarity
import scipy
import math
import time
import urllib.request
import pandas as pd

def weeds_precision(hyperv, hypov):
    '''
    Inclusion metric: returns the weeds precision metric for these two columns of a context matrix

    Input:
    hyperv - a hypernym word vector from a row in the distributional matrix
    hypov - a hyponym word vector from a row in the distributional matrix

    Output: a float in [0,1]
    '''

    sum_match = 0
    sum_hypo = 0

    #iterate through, if both are non null. This can (SHOULD) be optimized more for sparse matrices.
    for i in range(len(hyperv)):
        if((hyperv[i]>0) and (hypov[i]>0)):
            sum_match += hypov[i]
            sum_hypo += hypov[i]
        elif(hypov[i]>0):
            sum_hypo += hypov[i]

    return sum_match/sum_hypo

def cde(hyperv, hypov):
    '''
    Inclusion metric: returns the cde measure for inclusion for these two word embeddings

    Input:
    hyperv - a hypernym word vector from a row in the distributional matrix
    hypov - a hyponym word vector from a row in the distributional matrix

    Output: a float in [0,1]

    '''
    min_overlaps = np.minimum(hyperv, hypov).sum()
    sum_hypo = hypo_vector.sum()

    return min_overlaps/sum_hypo

def invCL(hyperv, hypov):
    '''
    Inclusion metric: returns the inverse CL (similar to CDE) for these two word embeddings

    Input:
    hyperv - a hypernym word vector from a row in the distributional matrix
    hypov - a hyponym word vector from a row in the distributional matrix

    Output: an integer in [0,1]
    '''
    try:
        icl = (cde(hyperv, hypov)*(1-cde(hyperv, hypov)))**0.5
    except:
        return 0.0

    return icl

def cos_sim(v1, v2, matrix=None):
    from sklearn.metrics.pairwise import cosine_similarity

    '''
    Similarity metric: Given any two vectors (can be DIVE or entries of dist matrix), calculates the cosine similarity and returns it as a float.
    If matrix is not None, computes cosine similarities of all entries in the table and returns a matrix.
    '''
    if(matrix is not None):
        sims = cosine_similarity(matrix)
        return sims
    else:
        sim = cosine_similarity(v1, v2)
        return sim[0,0]

def top_context_entropies(context_matrix, pmi_matrix, top_c=100):
    '''
    Given a matrix and its pointwise mutual information matrix, returns the top context entropies for each row, useful for SQLS metrics.
    Returns a dictionary of the median entropy of each row of the context matrix.

    Input:
    top_c - the number of top contexts to use. We sort by PPMI.
    pmi_matrix - a positive pmi matrix computer previously
    context_matrix - some sort of semantic matrix, stored as a lil_matrix (sparse bag of words)

    Output:
    mean_entropy_dict - the average entropy of the top contexts of this word.
    '''

    start = time.time()
    top_pmi_dict = {}
    top_contexts_set = set([])

    #get top contexts for each word, save in dict
    for i in range(context_matrix.shape[0]):
        sorted_pmi = np.argsort(-1*pmi_matrix[i,:].toarray().reshape(-1), axis=0)
        top_pmis = sorted_pmi[:top_c]
        top_pmi_dict[i] = top_pmis
        top_contexts_set = top_contexts_set.union(set(top_pmis))

    #for each of these contexts, get entropy. Transpose the matrix since it's faster to iterate over rows.
    context_matrix_t = context_matrix.transpose()
    entropy_vals = {}
    for k in top_contexts_set:
        row_nz = context_matrix_t.getrow(k).nonzero()
        row = context_matrix_t.getrow(k)[row_nz].toarray()[0]
        row_sum = row.sum()
        entropy_row = -sum([((entry/row_sum) * math.log((entry/row_sum),2)) for entry in row])
        entropy_vals[k] = entropy_row

    #now have entropy of all contexts
    mean_entropy_dict = {}
    for k in top_pmi_dict.keys():
        mean_entropy_dict[k] = np.mean([entropy_vals[z] for z in top_pmi_dict[k]])

    print('sqls row took:', time.time()-start)
    return mean_entropy_dict

def top_target_entropies(context_matrix):
    '''
    Given a context matrix, computes the entropies for each row and returns this as a dict.

    Input:
    context_matrix - a lil_matrix context matrix, rows are target words

    Output:
    target_ents - a dict with entropies for all rows of matrix
    '''
    target_ents = {}

    for k in range(context_matrix.shape[0]):
        row_nz = context_matrix.getrow(k).nonzero()
        row = context_matrix.getrow(k)[row_nz].toarray()[0]
        row_sum = row.sum()
        entropy_row = -sum([((entry/row_sum) * math.log((entry/row_sum),2)) for entry in row])
        target_ents[k] = entropy_row

    return target_ents

def sqls_sub(edict, hyper, hypo):
    '''
    Generality metric: given a dictionary of top context entropy values, and 2 indices (integers) of hyper and hyponyms, returns the SQLS sub metric.
    Input:
    edict - a dictionary mapping word integers to the mean entropy of their top contexts
    hyper/hypo - integer values corresponding to the indices of the words we're working with.

    Output:
    sqls_sub - an integer.
    '''
    return edict[hyper] - edict[hypo]

def sqls_row(target_edict, hyper, hypo):
    '''
    Generality metric: Given a dictionary of top target entropy values, and 2 indices (integers) of hyper and hyponyms, returns the SQLS row metric.
    Input:
    target_edict - a dictionary mapping target word indices to the entropy of their context distributions
    hyper/hypo - integer values corresponding to the indices of the words we're working with.

    Output:
    sqls_row - an integer.
    '''

    return 1 - target_edict[hyper]/target_edict[hypo]

def dif_sum(v1, v2):
    '''
    Generality metric: given two word vectors, returns the difference of their summations.

    Input:
    v1, v2: numpy arrays representing word vectors (can be DIVe or context vectors)

    Output: a float (unbounded)
    '''
    return v1.sum()-v2.sum()

def dif_norm(v1, v2):
    '''
    Generality metric: given two word vectors, returns the difference of their 2nd norms.

    Input:
    v1, v2: numpy arrays representing word vectors (DIVE or context)

    Output: a float (unbounded)
    '''
    return np.linalg.norm(v1, 2)-np.linalg.norm(v2,2)

def asymmetric_l1(hyperv, hypov, w0=5):
    '''
    Inclusion metric: returns the asymmetric l1 distance between two word vectors. This is kind of tricky
    (proof to derive the estimator is in the appendix)

    Input:
    hyperv/hypov - word vectors for the hypernym and hyponym. Shape of (1,length(context)).
    w0 - hyperparameter specified in the paper.

    Output:
    asymmetric_l1 - a float [0,1].
    '''
    #first, normalize both to get a proba distribution over contexts. Reshape too (to get)
    hyperv = hyperv.reshape(hyperv.shape[1])
    hypov = hypov.reshape(hypov.shape[1])
    hyperv_norm = hyperv/hyperv.sum()
    hypov_norm = hypov/hypov.sum()
    hypov_norm[hypov_norm==0] = 10**(-100) #replace 0 values with very small values, as specified in paper.

    #sort the elements by the values of v1_norm/v2_norm
    sorter = np.divide(-1*hyperv_norm, hypov_norm) #we add a negative to sort in reverse
    arr1inds = sorter.argsort()
    sorter_sorted = sorter[arr1inds[::-1]]*-1
    hypov_norm_sorted = hypov_norm[arr1inds[::-1]]

    s=0 ; c=0
    u=np.zeros(len(hyperv))
    while((s <= 1) and (c<len(hyperv))):
        u_c = min(1-s, (w0+1)*hypov_norm_sorted[c])
        s += u_c
        u[c]=u_c
        c += 1

    asymmetric_l1 = 1 - np.sum(np.multiply(u,sorter_sorted))

    return asymmetric_l1

def read_evalution():
    '''
    Reads the evalution dataset, and returns the result as pandas dataframe with shape (7491,5) and with columns:
    ['relator', 'relation', 'relatum', 'pos_2', 'pos_1']

    '''
    target_url_relations = 'https://raw.githubusercontent.com/esantus/EVALution/master/EVALution_1.0/RELATIONS.txt'
    target_url_relata = 'https://raw.githubusercontent.com/esantus/EVALution/master/EVALution_1.0/RELATA.txt'
    dfs = []

    for target_url in [target_url_relations, target_url_relata]:
        fp = urllib.request.urlopen(target_url) # it's a file like object and works just like a file
        mybytes = fp.read()
        data = mybytes.decode('utf8')
        fp.close()
        data_l = data.split('\n')
        data_l_t = [k.split('\t') for k in data_l]
        dfs.append(pd.DataFrame(data_l_t))

    data_pd_relations, data_pd_relata = tuple(dfs)
    cols_relations = ['word', 'relation', 'relatum', 'tags', 'sent', 'vote_1', 'vote_2', 'vote_3', 'vote_4', 'vote_5', 'agreement',
           'subj_num', 'avg_score', 'var', 'score_min_v', 'source', 'source_score']
    data_pd_relations.columns = cols_relations

    cols_relata = ['relatum', 'tags', 'freq', 'dom_pos', 'pos_dist', 'form_dist', 'pos_dist_detailed']
    data_pd_relata.columns = cols_relata
    data_pd_relata_pos = data_pd_relata['dom_pos'].str.split('-', expand=True)
    data_pd_relata['pos'] = data_pd_relata_pos[1]

    relations_sub = data_pd_relations.loc[:, ['word', 'relation', 'relatum']]
    relata_sub = data_pd_relata.loc[:, ['relatum', 'pos']]
    _relations_relata = relations_sub.merge(relata_sub, on='relatum')
    relations_relata = _relations_relata.merge(relata_sub, left_on='word', right_on='relatum')
    relations_relata.drop('relatum_y', inplace=True, axis=1)
    relations_relata.columns = ['relator', 'relation', 'relatum', 'pos_2', 'pos_1']
    return relations_relata

def read_evalution2():
    '''
    Reads the second evalution dataset with the right number of samples
    '''
    target_url = 'https://raw.githubusercontent.com/bacumin/UnsupervisedHypernymy/master/datasets/EVALution.test'
    fp = urllib.request.urlopen(target_url) # it's a file like object and works just like a file
    mybytes = fp.read()
    data = mybytes.decode('utf8')
    fp.close()
    data_l = data.split('\n')
    data_l_t = [k.split('\t') for k in data_l]
    df_pd = pd.DataFrame(data_l_t)

    df_pd.columns = ['word1', 'word2', 'hyp', 'relation']
    return df_pd

School/Homeworks/Vector Embedding for Hypernym Detection/evaluation/evaluation_functions.py


import requests
from PIL import Image
from io import BytesIO
import re
from immkdir import mkdir
import xiangceList

url = ''
host = 'https://www.nvshens.com/'
headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36'}

def getpage(url):  # 获取网页源码
    r = requests.get(url, headers=headers)
    r.encoding = "utf-8"
    page_code = r.text
    return page_code

def get_page_name_num() -> object:  # 获取相册页数、名字、相片数量
    page_code = getpage(url)
    reg = r"(.*?)"
    reg = re.compile(reg, re.S)
    num = re.findall(reg, page_code)
    num = num[0]
    num = int(re.findall("\d+", num)[0])
    if num % 3 == 0:
        page = int(num/3)
    else:
        page = int(num/3+1)
    reg = r"

(.*?)
"
    reg = re.compile(reg, re.S)
    name = re.findall(reg, page_code)
    name = name[0]
    if name == "":
        name = "无名"
    mkdir(name)
    print('{}的相册，有{}页，共有{}张相片。'.format(name,page,num))
    return name, num, page

def get_page_link():  # 页面地址拼接并返回拼接后的列表
    page_links = []
    page_num = get_page_name_num()
    page_num = page_num[2]
    for i in range(1, page_num+1):
        page_links.append('{}{}.html'.format(url, i))
    return page_links

def get_img_link():
    page_links = get_page_link()
    page_code = getpage(page_links[0])
    reg = r""
    reg = re.compile(reg, re.S)
    urlname = re.findall(reg, page_code)
    return urlname

def get_Img(imgLink,imgName):
    img = requests.get(imgLink, headers=headers)
    image = Image.open(BytesIO(img.content))
    image.save('D:/1/{}/{}.jpg'.format(name,imgName))
xiangceList.url1 = 'https://www.nvshens.com/gallery/qizhi/'
for j in xiangceList.get_typePageLink(3):
    url = j
    for i in get_page_link():
        url = i
        temp = get_page_name_num()
        name = temp[0]
        for j in get_img_link():
            imgUrl =j[0]
            a =imgUrl.replace('/s/','/')
            get_Img(a, j[1])
            print("正在保存‘{}’".format(j[1]))
    print('下载完毕！')