import numpy as np
import cv2
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
import streamlit as st
from tensorflow import keras
from keras.models import model_from_json
from keras.preprocessing.image import img_to_array
from streamlit_webrtc import webrtc_streamer, VideoTransformerBase, RTCConfiguration, VideoProcessorBase, WebRtcMode

classifier = keras.models.load_model('Final_model_Custome_CNN.h5')

#load face
try:
    face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')

    emotion_labels = ['Angry','Disgust','Fear','Happy','Neutral', 'Sad', 'Surprise']  # Emotion that will be predicted

except Exception:
    st.write("Error loading cascade classifiers")

RTC_CONFIGURATION = RTCConfiguration({"iceServers": [{"urls": ["stun:stun.l.google.com:19302"]}]})

class Faceemotion(VideoTransformerBase):
    def transform(self, frame):
      label = []
      img = frame.to_ndarray(format="bgr24")
      face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
      emotion_labels = ['Angry','Disgust','Fear','Happy','Neutral', 'Sad', 'Surprise']

      #image gray
      img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
      faces = face_cascade.detectMultiScale(
          image=img_gray, scaleFactor=1.3, minNeighbors=1)

      for (x, y, w, h) in faces:
          a=cv2.rectangle(img=img, pt1=(x, y), pt2=(
              x + w, y + h), color=(255, 0, 0), thickness=2)
          roi_gray = img_gray[y:y + h, x:x + w]
          roi_gray = cv2.resize(roi_gray, (48, 48), interpolation=cv2.INTER_AREA)
          roi = roi_gray.astype('float')/255.0
          roi = img_to_array(roi)
          roi = np.expand_dims(roi,axis=0) ## reshaping the cropped face image for prediction
          prediction = classifier.predict(roi)[0]   #Prediction
          label=emotion_labels[prediction.argmax()]
          label_position = (x,y)
          b=cv2.putText(a,label,label_position,cv2.FONT_HERSHEY_SIMPLEX,1,(0,255,0),2)
          label_position = (x, y)

      return b

def main():
    # Face Analysis Application #
    st.markdown("
Real Time Face Emotion Detection Application
", unsafe_allow_html=True)

    #add space
    st.markdown("
", unsafe_allow_html=True)

    #add paragraph text and position it in the center
    st.markdown("
Click on start to use webcam and detect your face emotion
", unsafe_allow_html=True)

    #add space
    st.markdown("
", unsafe_allow_html=True)

    webrtc_streamer(key="example", mode=WebRtcMode.SENDRECV, rtc_configuration=RTC_CONFIGURATION,
                        video_processor_factory=Faceemotion)
    #add space
    st.markdown("
", unsafe_allow_html=True)

    #add space
    st.markdown("
", unsafe_allow_html=True)

    html_temp4 = """

This Application is developed by Harish using Streamlit Framework, Opencv, Tensorflow and Keras library for demonstration purpose.

Thanks for Visiting

"""

    st.markdown(html_temp4, unsafe_allow_html=True)

if __name__ == "__main__":
    main()


# coding=utf-8
from flask import g
from flask_restplus import Namespace, abort, fields
from sqlalchemy import and_, or_

from disquotes.model import Permission
from disquotes.views.api.base import ResourceBase

ns = Namespace("permissions", "Permissions storage.")

permission_model = ns.model("Permission", {
    "permission": fields.String
})

@ns.route("/")
@ns.param("server", "Discord Server ID", type=int, required=True)
@ns.param("channel", "Discord Channel ID", type=int)
class PermissionResource(ResourceBase):
    def get(self):
        server, channel = self.get_server_channel()

        if channel:
            query = g.db.query(Permission.permission).filter(and_(
                Permission.server_id == server.id,
                or_(
                    Permission.channel_id == channel.id,
                    Permission.channel_id == None
                )
            ))
        else:
            query = g.db.query(Permission.permission).filter(and_(
                Permission.server_id == server.id,
                Permission.channel_id == None
            ))

        return {
            "permissions": [x[0] for x in query.all()]
        }

    @ns.param("perm", "Permission node", required=True)
    def put(self):
        server, channel = self.get_server_channel(create=True)

        if channel:
            query = g.db.query(Permission.permission).filter(and_(
                Permission.server_id == server.id,
                Permission.channel_id == channel.id
            ))
        else:
            query = g.db.query(Permission.permission).filter(and_(
                Permission.server_id == server.id,
                Permission.channel_id == None
            ))

        if query.filter(Permission.permission == self.get_field("perm")).scalar():
            abort(400, "Permission already exists for this channel/server.")

        new_perm = Permission(
            server_id=server.id,
            channel_id=channel.id if channel else None,
            permission=self.get_field("perm")
        )

        g.db.add(new_perm)

    @ns.param("perm", "Permission node", required=True)
    def delete(self):
        server, channel = self.get_server_channel()

        if channel:
            deleted_perm = g.db.query(Permission).filter(Permission.channel_id == channel.id)
        else:
            deleted_perm = g.db.query(Permission).filter(and_(
                Permission.server_id == server.id,
                Permission.channel_id == None
            ))

        deleted_perm = deleted_perm.filter(Permission.permission == self.get_field("perm")).delete()


# -*- coding: utf-8 -*-
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
from news.models import Column
from news.models import Article
from django.shortcuts import redirect
from django.http import JsonResponse
from django.template import RequestContext

def index(request):
    columns = Column.objects.all()
    # return redirect("http://www.ziqiangxuetang.com/django/django-cms-develop3.html")
    # return render(request, 'index.html', {'columns': columns})
    return render(request, 'index.html')

def column_detail(request, column_slug):
    # return HttpResponse('colmun slug: ' + column_slug)
    column = Column.objects.get(slug=column_slug)
    return render(request, 'news/column.html', {'column': column})

def article_detail(request, pk, article_slug):
    article = Article.objects.get(pk=pk)
    if article_slug != article.slug:
        return redirect("http://www.ziqiangxuetang.com/django/django-cms-develop3.html")
    return render(request, 'news/article.html', {'article': article})

def template_index_reports(request):
    reports_tmp_return = 'kkkkk'
    return JsonResponse(reports_tmp_return, safe=False)

def file_upload(request):
    if request.method == "POST":  # 请求方法为POST时，进行处理
        myFile = request.FILES.get("uploadBox", None)  # 获取上传的文件，如果没有文件，则默认为None
        if not myFile:
            return render(request, 'index.html', {'uploadResult':"no files for upload!"})
        import os
        destination = open(os.path.join("D:\\TDDOWNLOAD", myFile.name), 'wb+')  # 打开特定的文件进行二进制的写操作
        for chunk in myFile.chunks():  # 分块写入文件
            destination.write(chunk)
            destination.close()
            # return render(request, 'index.html', {'uploadResult':"glyphicon glyphicon-ok-circle"})
        return render(request, '', {'uploadResult': "file"})

def file_upload_json(request):
    if request.method == "POST":  # 请求方法为POST时，进行处理
        myFile = request.FILES.get("uploadBox", None)  # 获取上传的文件，如果没有文件，则默认为None
        if not myFile:
            result = 'none'
            return JsonResponse(result, safe=False)
        import os
        destination = open(os.path.join("D:\\TDDOWNLOAD", myFile.name), 'wb+')  # 打开特定的文件进行二进制的写操作
        result = 'success'
        for chunk in myFile.chunks():  # 分块写入文件
            destination.write(chunk)
            destination.close()
            # return render(request, 'index.html', {'uploadResult':"glyphicon glyphicon-ok-circle"})
        #return JsonResponse(result, safe=False)
        import json
        return HttpResponse(json.dumps(result))

def file_upload_fileinput(request):
    result = {"data": []}
    try:
        ret = -1
        myfile = request.FILES.get("file_data", None)
        if myfile:
            import os
            destination = open(os.path.join("D:\\TDDOWNLOAD", myfile.name), 'wb+')  # 打开特定的文件进行二进制的写操作
            if not myfile:
                ret = 1
            ret = 0
        return JsonResponse(result)
    except Exception:
        return JsonResponse(result)


# Dataset
# synthetic dataset for simple regression
from matplotlib import pyplot as plt
from sklearn.datasets import make_regression

# plt.figure()
# plt.title('Sample regression problem with one input variable')
X_R1, y_R1 = make_regression(n_samples=100, n_features=1,
                            n_informative=1, bias=150.0,
                            noise=30, random_state=0)
# plt.scatter(X_R1, y_R1, marker='o', s=50)
# plt.show()

from sklearn.neural_network import MLPRegressor
from sklearn.model_selection import train_test_split
import numpy as np

fig, subaxes = plt.subplots(2, 3, figsize=(11,8), dpi=70)

X_predict_input = np.linspace(-3, 3, 50).reshape(-1, 1)  # make up a test set

X_train, X_test, y_train, y_test = train_test_split(X_R1[0::5], y_R1[0::5], random_state = 0)

for thisaxisrow, thisactivation in zip(subaxes, ['tanh', 'relu']):
    for thisalpha, thisaxis in zip([0.0001, 1.0, 100], thisaxisrow):
        mlpreg = MLPRegressor(hidden_layer_sizes = [100,100],
                             activation = thisactivation,
                             alpha = thisalpha,
                             solver = 'lbfgs').fit(X_train, y_train)
        y_predict_output = mlpreg.predict(X_predict_input)
        thisaxis.set_xlim([-2.5, 0.75])
        thisaxis.plot(X_predict_input, y_predict_output,
                     '^', markersize = 10)
        thisaxis.plot(X_train, y_train, 'o')
        thisaxis.set_xlabel('Input feature')
        thisaxis.set_ylabel('Target value')
        thisaxis.set_title('MLP regression\nalpha={}, activation={})'
                          .format(thisalpha, thisactivation))
        plt.tight_layout()

plt.show()

Python Project/Neural networks: Regression.py


# list1 = ["Manisha", "Harry", "Larry", "Carry", "SkillF"]
# list1 = ("Manisha", "Harry", "Larry", "Carry", "SkillF")
# list1 = [["Manisha", 1], ["Harry", 1], ["Larry", 10], ["Carry", 0], ["SkillF", 3]]

# dic = dict(list1)
# print(dic)

# for item, lollypop in list1:
#     print(item, lollypop)

# for item, lollypop in dic.items():
#     print(item, lollypop)

# myDict = {
#     "name": "Manisha",
#     "age": 17,
#     "class": "Bsc"
# }
# for key in myDict:
# print(key)
# print(key)
# print(key, "=", myDict.get(key))

# QUICK QUIZ:- WRITE A PYTHON PROGRAM TO PRINT LIST ITEMS IF ITEM IS A NUMBER OR GREATER THAN 6

myList = [1, 4, "this", 89, 43, 2, 54, 6, "There", "Harry", "manisha", 7, 8, 9, int, float, False]
for item in myList:
    if str(item).isnumeric() and item >= 6:
        print(item)


#coding=utf8
#__author__=='luocheng'

TAG = 'cn'
start = 1
end =51
switch = 33
devide = 49.0
from collections import defaultdict

run2id2fls_ndcg = defaultdict(lambda:defaultdict(lambda:0))
run2id2sls_ndcg = defaultdict(lambda:defaultdict(lambda:0))
run2id2ndcg = defaultdict(lambda:defaultdict(lambda:0))

for l in open('./'+TAG+'.flseva.pertopic.csv').readlines()[1:]:
    segs=  l.strip().split(',')
    runname = segs[0]
    id = segs[1]
    dsharp = float(segs[3])
    run2id2fls_ndcg[runname][id] = dsharp

for l in open('./'+TAG+'.slseva.pertopic.csv').readlines()[1:]:
    segs=  l.strip().split(',')
    runname = segs[0]
    id = segs[1]
    dsharp = float(segs[3])
    run2id2sls_ndcg[runname][id] = dsharp

for l in open('./'+TAG+'.ndcg.pertopic.csv').readlines():
    segs=  l.strip().split(',')
    runname = segs[0]
    id = segs[1]
    ndcg= float(segs[2])
    run2id2ndcg[runname][id] = ndcg

def evaid():
    rtr = list()
    for i in range(start,end,1):
        if i <10:
            rtr.append('000'+str(i))
        if i >10 and i <100:
            rtr.append('00'+str(i))
        if i >=100:
            rtr.append('0'+str(i))
    return rtr
run2coarsesum =defaultdict(lambda:0.0)
run2finesum =defaultdict(lambda:0.0)

fout = open(TAG+'.dr.pertopic.csv','w')
fout.write('runname,id,coarse,fine,ndcg\n')
for r in sorted(list(run2id2ndcg.keys())):
    for id in evaid():
        if int(id) < switch:
            run2coarsesum[r] += run2id2fls_ndcg[r][id]
            run2finesum[r] += run2id2sls_ndcg[r][id]
        else:
            run2coarsesum[r] += run2id2ndcg[r][id]
            run2finesum[r] += run2id2ndcg[r][id]

        fout.write(r+','+id+','+str(run2id2fls_ndcg[r][id])+','+str(run2id2sls_ndcg[r][id])+','+str(run2id2ndcg[r][id])+'\n')
fout.close()

fout = open(TAG+'.dr.perrun.csv','w')
fout.write('runname,coarse-score,fine-score\n')
for r in sorted(list(run2id2fls_ndcg.keys())):
    fout.write(r+','+str(run2coarsesum[r]/devide)+','+str(run2finesum[r]/devide)+'\n')
fout.close()


from pyroborobo import Pyroborobo, Controller
from pyroborobo import SquareObject, CircleObject, Pyroborobo

class SwitchObject(CircleObject):
    def __init__(self, id, data):
        CircleObject.__init__(self, id)  # Do not forget to call super constructor
        self.regrow_time = data['regrowTimeMax']
        self.cur_regrow = 0
        self.triggered = False
        self.gate_id = data['sendMessageTo']
        self.rob = Pyroborobo.get()  # Get pyroborobo singleton

    def reset(self):
        self.show()
        self.register()
        self.triggered = False
        self.cur_regrow = 0

    def step(self):
        if self.triggered:
            self.cur_regrow -= 1
            if self.cur_regrow <= 0:
                self.show()
                self.register()
                self.triggered = False

    def is_walked(self, rob_id):
        self.triggered = True
        self.rob.objects[self.gate_id].open()
        self.cur_regrow = self.regrow_time
        self.hide()
        self.unregister()

    def inspect(self, prefix=""):
        return "I'm a switch!"

class GateObject(SquareObject):
    def __init__(self, id, data):
        SquareObject.__init__(self, id)
        self.triggered = False
        self.regrow_time = data['regrowTimeMax']
        self.cur_regrow = 0

    def reset(self):
        self.show()
        self.register()
        self.triggered = False
        self.cur_regrow = 0

    def step(self):
        if self.triggered:
            self.cur_regrow -= 1
            if self.cur_regrow <= 0:
                self.show()
                self.register()
                self.triggered = False

    def open(self):
        self.triggered = True
        self.hide()
        self.unregister()
        self.cur_regrow = self.regrow_time

    def inspect(self, prefix=""):
        return "I'm a gate!"

class ResourceObject(CircleObject):
    def __init__(self, id_, data):
        CircleObject.__init__(self, id_)  # Do not forget to call super constructor
        self.regrow_time = 100
        self.cur_regrow = 0
        self.triggered = False
        self.rob = Pyroborobo.get()  # Get pyroborobo singleton

    def reset(self):
        self.show()
        self.register()
        self.triggered = False
        self.cur_regrow = 0

    def step(self):
        if self.triggered:
            self.cur_regrow -= 1
            if self.cur_regrow <= 0:
                self.show()
                self.register()
                self.triggered = False

    def is_walked(self, rob_id):
        self.triggered = True
        self.cur_regrow = self.regrow_time
        self.hide()
        self.unregister()

    def inspect(self, prefix=""):
        return f"""I'm a ResourceObject with id: {self.id}"""

class HungryController(Controller):

    def __init__(self, wm):
        super().__init__(wm)
        self.rob = Pyroborobo.get()
        assert self.nb_sensors == 8, "SimpleController only works with 8 sensors"
        self.rotspeed = 0.5

    def reset(self):
        pass

    def step(self):
        self.set_translation(1)  # Let's go forward
        self.set_rotation(0)

        must_flee: bool = False  # have we encountered a wall and must prioritise avoiding obstacle

        if self.get_distance_at(1) < 1:  # if we see something on our right
            if self.get_object_at(1) != -1:  # And it is food
                self.set_rotation(-self.rotspeed)  # GO TOWARD IT
            else:
                self.set_rotation(self.rotspeed)  # flee it
                must_flee = True
        elif self.get_distance_at(2) < 1:  # if we see something in front of us
            if self.get_object_at(2) != -1 and not must_flee:  # If we are not avoiding obstacle and it's food
                self.set_rotation(0)
            else:
                self.set_rotation(self.rotspeed)  # turn left
                must_flee = True
        elif self.get_distance_at(3) < 1:  # Otherwise, if we see something on our right
            if self.get_object_at(3) != -1 and not must_flee:
                self.set_rotation(self.rotspeed)  # turn left
            else:
                self.set_rotation(-self.rotspeed)
                must_flee = True

    def inspect(self, prefix):
        output = ""
        for i in range(self.nb_sensors):
            output += f"""sensor {i}:
    dist: {self.get_distance_at(i)}
    id: {self.world_model.camera_objects_ids[i]}
    nbobjs: {len(self.rob.objects)}
    is_object: {self.get_object_at(i)}
    is_robot: {self.get_robot_id_at(i)}
    is_wall: {self.get_wall_at(i)}\n\n"""
        return output

if __name__ == "__main__":
    rob = Pyroborobo.create("config/pywander_pyobj_resource.properties",
                            controller_class=HungryController,
                            object_class_dict={'_default': ResourceObject, 'gate': GateObject, 'switch': SwitchObject})
    rob.start()
    rob.update(10000)
    rob.close()


from typing import List
import re
from collections import Counter

class Solution:
    def shortestCompletingWord(self, licensePlate: str,
                               words: List[str]) -> str:
        regex = re.compile('[^a-zA-Z]')
        license = regex.sub('', licensePlate)
        counter1 = Counter(license.lower())
        res = ''
        for word in words:
            if self.count(counter1, word):
                if res == '' or len(word) < len(res):
                    res = word
        return res

    def count(self, counter1, word):
        counter2 = Counter(word)
        counter2.subtract(counter1)
        return all([c >= 0 for c in counter2.values()])


import paramiko
import time

ssh_client = paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh_client.connect('40.40.40.1', port=22, username='admin', password='')

print('Anda Berhasil Login\n')

stdin,stdout,stderr = ssh_client.exec_command("interface wireless enable wlan1") #enable hotspot
stdin,stdout,stderr = ssh_client.exec_command("ip dhcp-client add interface=ether1 disabled=no")
stdin,stdout,stderr = ssh_client.exec_command("interface wireless set mode=ap-bridge numbers=wlan1") #Menjadikan wirelles menjadi mode apbridge
stdin,stdout,stderr = ssh_client.exec_command("interface wireless set wlan1 ssid=Bandi-Wifi") #Membuat ssid untuk hotspot
stdin,stdout,stderr = ssh_client.exec_command("ip firewall nat add chain=srcnat out-interface=ether1 action=masquerade") #agar router mendapat internet
stdin,stdout,stderr = ssh_client.exec_command("ip address add address=20.20.20.1/24 interface=ether2") #seting ip address interface untuk hotspot
time.sleep(1)
stdin,stdout,stderr = ssh_client.exec_command("ip pool add name=dhcp_hotspot ranges=20.20.20.2-20.20.20.254") #bikin pool untuk dhcp server
stdin,stdout,stderr = ssh_client.exec_command("ip dhcp-server add address-pool=dhcp_hotspot disabled=no interface=ether2 name=hotspot") #bikin dhcp-server pake pool yang sebelumnya dibuat
stdin,stdout,stderr = ssh_client.exec_command("ip dhcp-server network add address=20.20.20.0/24 dns-server=8.8.8.8 gateway=20.20.20.1") #masukin network dhcp-server kita , jangan sampai kelewat
stdin,stdout,stderr = ssh_client.exec_command("ip hotspot profile add dns-name=bandi.net hotspot-address=20.20.20.1 name=hotspot1") # buat profile buat hotspot
stdin,stdout,stderr = ssh_client.exec_command("ip hotspot add interface=ether2 name=hotspot2 address-pool=dhcp_hotspot profile=hotspot1 addresses-per-mac=2 disabled=no")
stdin,stdout,stderr = ssh_client.exec_command("ip hotspot add address-pool=dhcp_hotspot disabled=no interface=wlan1 name=hotspot1 profile=hotspot1 html-directory=hotspot") # buat hotspot

time.sleep(1)
stdin, stdout, stderr = ssh_client.exec_command('ip address print')

stdin, stdout, stderr = ssh_client.exec_command('log print')

output = stdout.readlines()

print('\n'.join(output))

ssh_client.close


#
# @lc app=leetcode id=152 lang=python3
#
# [152] Maximum Product Subarray
#

# @lc code=start
class Solution:
    def maxProduct(self, nums):
        if not nums:
            return 0
        if len(nums) == 1:
            return nums[0]
        helper = nums.copy()
        helper2 = nums.copy()
        for i in range(1, len(nums)):
            helper[i] = max(nums[i], helper[i - 1] * nums[i], helper2[i - 1] * nums[i])
            helper2[i] = min(nums[i], helper2[i - 1] * nums[i], helper[i - 1] * nums[i])
        return max(helper)

if __name__ == '__main__':
    a = Solution()
    b = a.maxProduct([2,-5,-2,-4,3])
    print(b)
# @lc code=end


#! /usr/bin/env python3
###############################################################################
#     File Name           :     d2q1.py
#     Created By          :     Félix Chiasson (7138723)
#     Creation Date       :     [2015-10-09 16:05]
#     Last Modified       :     [2015-10-27 15:57]
#     Description         :     Calculer l'indice de masse corporelle
###############################################################################
import sys

def calculBMI(w, h):
    """

    (float, float) -> float
    Retourne l'indice de masse corporelle avec le poids et la hauteur
    Restrictions: w, h >= 0

    """
    return w / (h * h)

def idealW(weight):
    """

    (float) -> None
    Retourne un message basé sur la valeur de weight.
    Restriction: weight must be a bmi value

    """
    if weight < 18.5:
        print("Maigreur!")
    elif 18.5 <= weight < 25.0:
        print("Poids idéal!")
    elif 25 <= weight < 30.0:
        print("Surpoids!")
    else:
        print("Obésité!")

positif = False
while not positif:
    try:
        poids = float(input("Veuillez entrer votre poids en kilogrammmes: "))
        taille = float(input("Veuillez entrer votre taille en mètres: "))

        if poids <= 0 or taille <= 0:
            print("Veuillez entrer un nombre positif non nul")

        else:
            bmi = calculBMI(poids, taille)
            print("Votre indice de masse corporelle est", bmi)
            idealW(bmi)
            positif = True      # La boucle arrête

    except ValueError:       # Si l'utilisateur entre une lettre ou un symbol
        print("Vous avez entré un caractère illégal!")
    except KeyboardInterrupt:   # Si l'utilisateur arrête avec ^C
        print("\nAurevoir...!")
        sys.exit()


from gray_level import gray_level_transformation
from min_max_median import min_max_median_filter
from laplacian_sobel import laplacian, sobel

if __name__ == "__main__":
    path = "images/profile.png"

    gray_level_transformation(path, 0, 100)
    gray_level_transformation(path, 155, 255)

    min_max_median_filter(path)
    min_max_median_filter(path, size=3)

    laplacian(path)
    laplacian(path, ksize=3)
    laplacian(path, ksize=5)

    sobel(path)
    sobel(path, ksize=5)


# classes and objects
# clases are templated to make objects

# making a coin object  using a class

from os import path

class Pound:
    # we have six states to define our class
    # object is an isinstance of class

    value=1.00
    colour="gold"
    num_edges=1
    diameter=22.5 #mm
    thickness=3.15

    heads=True

coin1=Pound()
# means coin 1 have class Pound...and it has all the properties of class Pound which we define in it

print(type(coin1))
print(coin1.value)
print(coin1.thickness)
print(coin1.colour)

# to change the value of the class
coin1.colour="greenish"
print(coin1.colour)

coin2=Pound()
print(coin2.colour)
# we get the colour of coin2 gold that is which we define in hte value of the class..
# this is the difference between object and classes

# classes are the base templets...the all new objects made from...once object is made each obj has unique chracter and independent on others...
# object behave independently each other


"""Tests for google3.third_party.java_src.tsunami.plugin_server.py.common.net.requests_http_client."""

from unittest import mock

from absl.testing import absltest
from absl.testing import parameterized
import requests

from tsunami.plugin_server.py.common.net.http.host_resolver_http_adapter import HostResolverHttpAdapter
from tsunami.plugin_server.py.common.net.http.http_header_fields import HttpHeaderFields
from tsunami.plugin_server.py.common.net.http.http_method import HttpMethod

class HostResolverHttpAdapterTest(parameterized.TestCase):

  @classmethod
  def setUpClass(cls):
    super().setUpClass()
    cls.custom_adapter = HostResolverHttpAdapter(5, 10)

  def setUp(self):
    super().setUp()
    self.addCleanup(mock.patch.stopall)
    # Mock of requests's HTTPAdapter
    response = requests.Response()
    response.status_code = 200
    mock.patch.object(
        requests.adapters.HTTPAdapter,
        'send',
        return_value=response,
    ).start()
    # Mock hostname lookup
    self.mock_getaddrinfo = mock.patch('socket.getaddrinfo').start()

  @parameterized.named_parameters(
      ('with_hostname', 'vuln-app.com'),
      ('with_ipv4', '199.21.82.88'),
      (
          'with_ipv6',
          '[2001:0db8:85a3:0000:0000:8a2e:0370:7334]',
      ),
  )
  def test_send_dispatches_with_host_header(self, host):
    url = 'http://{}:8080/send'.format(host)
    request = self._prepare_request(url)

    self.custom_adapter.send(request)

    requests.adapters.HTTPAdapter.send.assert_called_with(request)
    self.assertEqual(
        request.headers.get(HttpHeaderFields.HOST.value), '{}:8080'.format(host)
    )

  def test_send_without_target_ip_dispatches_default_hostname(self):
    url = 'http://vuln-app.com:8080/send'
    request = self._prepare_request(url)

    self.custom_adapter.send(request)

    requests.adapters.HTTPAdapter.send.assert_called_with(request)
    self.assertEqual(request.url, url)

  def test_send_when_hostname_resolves_to_ip_uses_default_hostname(self):
    url = 'http://vuln-app.com:8080/send'
    ip = '199.21.82.88'
    request = self._prepare_request(url)

    self.mock_getaddrinfo.return_value = [ip]
    self.custom_adapter.send(request, ip=ip)

    requests.adapters.HTTPAdapter.send.assert_called_once_with(request)
    self.assertEqual(
        request.headers.get(HttpHeaderFields.HOST.value), 'vuln-app.com:8080'
    )
    self.assertEqual(request.url, url)

  def test_send_when_hostname_is_the_ip_uses_default_hostname(self):
    ip = '2001:0db8:85a3:0000:0000:8a2e:0370:7334'
    url = 'http://[{}]:8080/send'.format(ip)
    request = self._prepare_request(url)

    self.custom_adapter.send(request, ip=ip)

    requests.adapters.HTTPAdapter.send.assert_called_once_with(request)
    self.assertEqual(
        request.headers.get(HttpHeaderFields.HOST.value),
        '[{}]:8080'.format(ip))
    self.assertEqual(request.url, url)

  def test_send_when_hostname_is_case_insensitive(self):
    url = 'http://vuln-APP.com:8080/send'
    ip = '199.21.82.88'
    request = self._prepare_request(url)

    self.custom_adapter.send(request, ip=ip)

    requests.adapters.HTTPAdapter.send.assert_called_once_with(request)
    self.assertEqual(
        request.headers.get(HttpHeaderFields.HOST.value), 'vuln-APP.com:8080'
    )
    self.assertEqual(request.url, 'http://199.21.82.88:8080/send')

  def test_send_when_hostname_does_not_resolve_to_ipv4_uses_ipv4(self):
    url = 'http://vuln-app.com:8080/send'
    ip = '199.21.82.88'
    request = self._prepare_request(url)

    self.mock_getaddrinfo.return_value = ['1.1.1.1']
    self.custom_adapter.send(request, ip=ip)

    requests.adapters.HTTPAdapter.send.assert_called_once_with(request)
    self.assertEqual(
        request.headers.get(HttpHeaderFields.HOST.value), 'vuln-app.com:8080'
    )
    self.assertEqual(request.url, 'http://199.21.82.88:8080/send')

  def test_send_when_hostname_does_not_resolve_to_ipv6_uses_ipv6(self):
    url = 'http://vuln-app.com:8080/send'
    ip = '2001:0db8:85a3:0000:0000:8a2e:0370:7334'
    request = self._prepare_request(url)

    self.mock_getaddrinfo.return_value = ['1.1.1.1']
    self.custom_adapter.send(request, ip=ip)

    requests.adapters.HTTPAdapter.send.assert_called_once_with(request)
    self.assertEqual(
        request.headers.get(HttpHeaderFields.HOST.value), 'vuln-app.com:8080'
    )
    self.assertEqual(
        request.url,
        'http://[2001:0db8:85a3:0000:0000:8a2e:0370:7334]:8080/send',
    )

  def _prepare_request(self, url):
    request = requests.Request(
        method=HttpMethod.GET, url=url, data=b'HTML content'
    )
    request = request.prepare()
    request.url = url
    return request

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


import io
import os
import shutil
from datetime import datetime
from functools import reduce
from typing import Optional, Union

import pandas as pd
import torch
import torch.nn as nn
from PIL import Image
from tensorboard.backend.event_processing.event_accumulator import EventAccumulator
from torch.optim import Optimizer

from sidewalk_widths_extractor.typing import _DEVICE, _PATH

def load_checkpoint(source_path: _PATH, module: Union[nn.Module, Optimizer]) -> Union[None, int]:
    """Load a nn.Module checkpoint.

    Args:
        source_path (_PATH): the path to the checkpoint file.
        module (nn.Module): nn.Module to-be-loaded from.

    Returns:
        Union[None, int]: returns the epoch number if there is one, otherwise return None.
    """
    d = torch.load(source_path)
    module.load_state_dict(d["state_dict"])
    return d["epoch"] if "epoch" in d else None

def save_checkpoint(
    target_path: _PATH,
    module: Union[nn.Module, Optimizer],
    epoch: Optional[int] = None,
    filename: str = "checkpoint",
    format_type: str = ".pth.tar",
) -> None:
    """Save a nn.Module checkpoint.

    Args:
        target_path (_PATH): the path to the target checkpoint file.
        module (nn.Module): nn.Module to-be-saved.
        epoch (Optional[int], optional): The epoch number. Defaults to None.
        filename (str, optional): checkpoint file's name. Defaults to "checkpoint".
        format_type (str, optional): file format. Defaults to ".pth.tar".
    """
    mkdir(target_path)
    cp = {"state_dict": module.state_dict()}
    if epoch:
        cp["epoch"] = epoch
    torch.save(
        cp,
        os.path.join(target_path, filename + format_type),
    )

def mkdir(target_path: _PATH) -> None:
    """
    Create a directory if it doesn't exist.

    Args:
        target_path (_PATH): The path to the directory
    """
    if not os.path.exists(target_path):
        os.makedirs(target_path)

def archive(source_path: _PATH, target_path: _PATH, archive_format: str = "zip") -> None:
    """
    Archive the given folder.

    Args:
        source_path (_PATH): The path to the source folder.
        target_path (_PATH): The path to the target folder.
        archive_format (str): The archive format
    """
    base = os.path.basename(target_path)
    name = base.split(".")[0]
    archive_from = os.path.dirname(source_path)
    archive_to = os.path.basename(source_path.strip(os.sep))
    shutil.make_archive(name, archive_format, archive_from, archive_to)
    shutil.move(f"{name}.{archive_format}", target_path)

def get_device() -> _DEVICE:
    """
    Get the available device.

    Returns:
        _DEVICE: device identifier
    """
    return (
        torch.device(f"cuda:{torch.cuda.current_device()}") if torch.cuda.is_available() else "cpu"
    )

def create_path_with_timestamp(
    root: Optional[_PATH], prefix: Optional[str] = None, postfix: Optional[str] = None
) -> str:
    """Create a path with current timestamp.

    Args:
        root (Optional[_PATH]): root path.
        prefix (Optional[str], optional): string to be added before the timestamp. Defaults to None.
        postfix (Optional[str], optional): string to be added after the timestamp. Defaults to None.

    Returns:
        str: path string
    """
    timestamp = datetime.now().strftime(r"%d-%m-%Y %H-%M-%S")
    out, name = [], None
    if root:
        out.append(root)
    if prefix:
        name = prefix
    if name:
        name += " " + timestamp
    else:
        name = timestamp
    if postfix:
        name += " " + postfix
    out.append(name)
    return os.path.join(*out)

def save_writer_scalars(log_path: _PATH, filename: str = "scalars.csv") -> None:
    event = EventAccumulator(path=log_path)
    event.Reload()
    tags = event.Tags()["scalars"]

    if len(tags) == 0:
        return

    dfs = []
    for tag in tags:
        dfs.append(
            pd.DataFrame(event.Scalars(tag))
            .rename(columns={"value": tag})
            .drop(["wall_time"], axis=1)
        )
    out = reduce(lambda left, right: pd.merge(left, right, on=["step"], how="outer"), dfs)
    out.to_csv(os.path.join(log_path, filename), index=False)

def save_writer_figures(log_path: _PATH, folder_name: str = "figures") -> None:
    event = EventAccumulator(path=log_path, size_guidance={"images": 0})
    event.Reload()
    tags = event.Tags()["images"]

    if len(tags) == 0:
        return

    figure_folder = os.path.join(log_path, folder_name)
    mkdir(figure_folder)

    for tag in tags:
        for item in event.Images(tag):
            step = item.step
            encoded = item.encoded_image_string
            img = Image.open(io.BytesIO(encoded))
            path = os.path.join(figure_folder, str(step))
            mkdir(path)
            img.save(os.path.join(path, f"{tag.replace('/','_')}.png"))


import torch.nn as nn
import torch.nn.functional as F

__all__ = ['AlexNet', 'AlexNet1']

class AlexNet(nn.Module):
    def __init__(self):
        super(AlexNet, self).__init__()
        self.conv = nn.Sequential(
            # part 1
            nn.Conv2d(3, 96, kernel_size=11, stride=4, padding=2),
            nn.ReLU(),
            nn.MaxPool2d(kernel_size=3, stride=2),
            nn.BatchNorm2d(96),
            # part 2
            nn.Conv2d(96,256, kernel_size=5, padding=2),
            nn.ReLU(),
            nn.MaxPool2d(kernel_size=3, stride=2),
            nn.BatchNorm2d(256),
            # part 3
            nn.Conv2d(256, 384, kernel_size=3, padding=1),
            nn.ReLU(),
            # part 4
            nn.Conv2d(384, 384, kernel_size=3, padding=1),
            nn.ReLU(),
            # part 5
            nn.Conv2d(384, 256, kernel_size=3, padding=1),
            nn.ReLU(),
            nn.MaxPool2d(kernel_size=3, stride=2)
        )
        self.fc = nn.Sequential(
            # part 6
            nn.Linear(6 * 6 * 256, 4096),
            nn.ReLU(),
            nn.Dropout(),
            # part 7
            nn.Linear(4096, 4096),
            nn.ReLU(),
            nn.Dropout(),
            # part 8
            nn.Linear(4096, 1000),
            nn.Softmax(dim=1),
        )

    def forward(self, input):
        output = self.conv(input)
        output = output.view(input.size(0), -1)
        output = self.fc(output)
        return output

# 下面这种写法更容易调试跟踪每个输出的size，容易debug
class AlexNet1(nn.Module):
    def __init__(self):
        super(AlexNet1, self).__init__()
        self.op1 = nn.Sequential(
            ConvReLU(3, 96, kernel_size=11, stride=4, padding=2),
            nn.MaxPool2d(kernel_size=3, stride=2),
            nn.BatchNorm2d(96)
        )
        self.op2 = nn.Sequential(
            ConvReLU(96, 256, kernel_size=5, padding=2),
            nn.MaxPool2d(kernel_size=3, stride=2),
            nn.BatchNorm2d(256)
        )
        self.op3 = ConvReLU(256, 384, kernel_size=3, padding=1)
        self.op4 = ConvReLU(384, 384, kernel_size=3, padding=1)
        self.op5 = nn.Sequential(
            ConvReLU(384, 256, kernel_size=3, padding=1),
            nn.MaxPool2d(kernel_size=3, stride=2)
        )
        self.fc = nn.Sequential(
            # part 1
            nn.Linear(6 * 6 * 256, 4096),
            nn.ReLU(),
            nn.Dropout(),
            # part 2
            nn.Linear(4096, 4096),
            nn.ReLU(),
            nn.Dropout(),
            # part 3
            nn.Linear(4096, 1000),
            nn.Softmax(dim=1),
        )

    def forward(self, input):
        output = self.op1(input)
        output = self.op2(output)
        output = self.op3(output)
        output = self.op4(output)
        output = self.op5(output)
        output = output.view(input.size(0), -1)
        output = self.fc(output)
        return output

class ConvReLU(nn.Module):
    def __init__(self, in_channels, out_channels, kernel_size, stride=1, padding=0):
        super(ConvReLU, self).__init__()
        self.conv = nn.Conv2d(in_channels, out_channels, kernel_size=kernel_size, stride=stride, padding=padding)

    def forward(self, x):
        x = self.conv(x)
        x = F.relu(x)
        return x


import os
import cv2
#import BlurDetection
from BlurDetection import BlurDetection

facefile = "/home/gubo/1/1/buqingchu/2-buqingchu/"
successfile = "/home/gubo/8/buqingchu/"
successfile1 = "/home/gubo/8/qingchu/"
successfile2 = "/home/gubo/8/0.33/"
successfile3 = "/home/gubo/8/0/"
def blur_detction(testImg):
    tmp = BlurDetection('')
    score = tmp._Tenengrad(testImg)
    return float(score)
piclist = os.listdir(facefile)
successlist = os.listdir(successfile)
# i = 0

piclist = os.listdir(facefile)
successlist = os.listdir(successfile)
for okname in piclist:
    if okname.endswith('jpg'):

        img = cv2.imread(facefile + okname)

        sp = img.shape
        y = sp[0] * 0.15
        #print(y)
        h = sp[0] * 0.70
        #print(h)
        x = sp[1] * 0.15

        w = sp[1] * 0.7
        testImg = img[int(y):int(y + h), int(x):int(x + w)]
        testImg = cv2.resize(testImg,(80,80),interpolation=cv2.INTER_LINEAR)
        cv2.resize()
        sp = testImg.shape
        #print(sp)
        #print(sp[0])
            #cal_score = Cal(testImg)
            #print(cal_score)
        score = blur_detction(testImg)
        score = round(score, 2)

        # print(str(score) +'-' + okname)
        #cv2.imwrite(successfile + str(score) + '-' + okname, img)
            #print(res_int)
        # os.system(
        #     'mv -f ' + ' ' + facefile + okname + ' ' + successfile + okname +'_' + str(score))


import sys # remove the path of ROS
if '/opt/ros/kinetic/lib/python2.7/dist-packages' in sys.path:
    sys.path.remove('/opt/ros/kinetic/lib/python2.7/dist-packages')
import os
import cv2
import inspect
import torch
import numpy as np
import importlib
from torch import nn
from os.path import join as jn
from torch.nn import functional as F
from collections import OrderedDict
import torch.optim.lr_scheduler as lrs
from warmup_scheduler import GradualWarmupScheduler

import pytorch_lightning as pl
from .metrics import compare_psnr, compare_ssim

class MInterface(pl.LightningModule):
    def __init__(self, model_name, loss, lr, **kargs):
        super().__init__()
        self.save_hyperparameters(ignore=["callbacks"])
        self.configure_loss()
        self.model = self.load_model()
        self.teacher_model = self.load_model()

        if kargs['train']:
            # training params
            self.loss_weight = kargs['loss_weight'] # [loss_BC, loss_SC, loss_TG, loss_SG]
            self.training_stage = kargs['training_stage']
            assert self.training_stage in [1, 2]

            # load teacher model during second stage training
            if self.training_stage == 2:
                state_dict = self.load_state_dict_from_checkpoint(kargs['ckpt_path'])
                self.model.load_state_dict(state_dict)
                # load teacher model
                self.teacher_model.load_state_dict(state_dict)
                self.teacher_model.freeze()
                self.teacher_model.eval()
        else:
            # test params
            self.save_path = kargs['save_path']
            self.has_gt = kargs['has_gt']

    def forward(self, event, blur):
        return self.model(event, blur)

    def training_step(self, batch, batch_idx):
        eger_blur2sharp, eger_largeblur2sharp, eger_largeblur2blur, blur, large_blur = batch
        B, ev_C, ev_H, ev_W = eger_blur2sharp.shape
        im_C, im_H, im_W = blur.shape[-3:]
        assert im_H / ev_H == im_W / ev_W

        # assemble inputs
        egers = torch.cat((eger_blur2sharp[:,None,...],  eger_largeblur2blur[:,None,...], eger_largeblur2sharp[:,None,...]), 1).reshape((B*3,ev_C,ev_H,ev_W)) # B*3,T*4,H,W
        blurs = torch.cat((blur[:,None,...], large_blur[:,None,...], large_blur[:,None,...]), 1).reshape((B*3,im_C,im_H,im_W))

        # process by network
        L, SAN_E = self(egers, blurs)
        L = L.clamp(0,1).reshape((B,3,im_C,im_H,im_W))
        SAN_E = SAN_E.reshape((B,3,im_C,im_H,im_W))

        ## first stage training
        if self.training_stage == 1:
            # compute loss
            loss_BC, loss_SC = self.compute_loss(L, SAN_E, blur)
            loss = self.loss_weight[0] * loss_BC + self.loss_weight[1] * loss_SC

        ## second stage training
        else:
            # generate pseudo-ground-truth by teacher model
            L_teacher, _ = self.teacher_model(eger_blur2sharp, blur)
            L_teacher = L_teacher.clamp(0,1)

            # generate down-sampled results
            rand_scale = ((np.random.randint(ev_H, im_H+1) // 32) * 32) / im_H
            lr_large_blur = self.image_downsampling(large_blur, rand_scale)
            lr_L_lb2s, _ = self(eger_largeblur2sharp, lr_large_blur)
            lr_L_lb2s = lr_L_lb2s.clamp(0,1)

            # generate down-sampled pseudo-ground-truth
            lr_L_teacher = self.image_downsampling(L_teacher, rand_scale)

            # compute loss
            loss_BC, loss_SC, loss_TG, loss_SG = self.compute_loss(L, SAN_E, blur, lr_L_lb2s, L_teacher, lr_L_teacher)
            loss = self.loss_weight[0] * loss_BC + self.loss_weight[1] * loss_SC +\
                 self.loss_weight[2] * loss_TG + self.loss_weight[3] * loss_SG

        self.log('loss', loss, on_step=False, on_epoch=True, prog_bar=True)
        return loss

    def test_step(self, batch, batch_idx):
        if self.has_gt:
            eger, blur, gt, prefix = batch
        else:
            eger, blur, prefix = batch

        # process by network
        B, N = eger.shape[:2]
        for n in range(N):
            L, _ = self(eger[:,n,...], blur[:,n,...])
            L = L.clamp(0,1)

            # save results
            if self.save_path != None:
                for b in range(B):

                    # save reconstruction
                    result = (L[b,...].permute(1,2,0).squeeze().cpu().detach().numpy() * 255).astype(np.uint8)
                    save_name = prefix[b] + '_%03d.png' %(n)
                    cv2.imwrite(jn(self.result_root, save_name), result)

                    # save blurry inputs
                    blurry_img = (blur[b,n,...].permute(1,2,0).squeeze().cpu().detach().numpy() * 255).astype(np.uint8)
                    cv2.imwrite(jn(self.blur_root, save_name), blurry_img)

                    # save gt and compute metrics
                    if self.has_gt:
                        groundtruth = (gt[b,n,...].permute(1,2,0).squeeze().cpu().detach().numpy() * 255).astype(np.uint8)
                        cv2.imwrite(jn(self.gt_root, save_name), groundtruth)

                        # compute metrics
                        psnr, ssim = compare_psnr(groundtruth, result), compare_ssim(groundtruth, result)
                        self.log('psnr', psnr, on_step=True, on_epoch=True, prog_bar=True)
                        self.log('ssim', ssim, on_step=True, on_epoch=True, prog_bar=True)

    def on_test_epoch_start(self):
        # ensure directories
        if self.save_path != None:
            self.result_root = jn(self.save_path, 'Result')
            if not os.path.exists(self.result_root):
                os.makedirs(self.result_root)
            self.blur_root = jn(self.save_path, 'Blur')
            if not os.path.exists(self.blur_root):
                os.makedirs(self.blur_root)

            if self.has_gt:
                self.gt_root = jn(self.save_path, 'GroundTruth')
                if not os.path.exists(self.gt_root):
                    os.makedirs(self.gt_root)

    def compute_loss(self, L, SAN_E, blur, lr_L_lb2s=None, L_teacher=None, lr_L_teacher=None):
        # seperate results
        L_b2s, L_lb2b, L_lb2s = L[:,0,...], L[:,1,...], L[:,2,...]
        SAN_E_b2s, SAN_E_lb2b, SAN_E_lb2s = SAN_E[:,0,...], SAN_E[:,1,...], SAN_E[:,2,...]

        # compute first-stage losses
        loss_BC = self.loss_function(L_lb2b, blur)
        loss_SC = self.loss_function(SAN_E_lb2s / SAN_E_b2s, SAN_E_lb2b)

        if self.training_stage == 1:
            return loss_BC, loss_SC
        else:
            # compute second-stage losses
            loss_TG = self.loss_function(L_teacher, L_lb2s)
            loss_SG = self.loss_function(lr_L_teacher, lr_L_lb2s) # may need to be modified
            return loss_BC, loss_SC, loss_TG, loss_SG

    def image_downsampling(self, img, scale_factor, mode='bicubic'):
        img = F.interpolate(img, scale_factor=scale_factor, mode=mode).clamp(0,1)
        return img

    def load_state_dict_from_checkpoint(self, checkpoint_path):
        state_dict = torch.load(checkpoint_path)['state_dict']
        new_state_dict = OrderedDict()
        for k, v in state_dict.items():
            if k[:6] == 'model.': # find keys starting with `model.`
                name = k[6:] # remove `model.`
                new_state_dict[name] = v
        return new_state_dict

    def configure_optimizers(self):
        if hasattr(self.hparams, 'weight_decay'):
            weight_decay = self.hparams.weight_decay
        else:
            weight_decay = 0
        optimizer = torch.optim.Adam(
            self.parameters(), lr=self.hparams.lr, weight_decay=weight_decay)

        if self.hparams.lr_scheduler is None:
            return optimizer
        else:
            if self.hparams.lr_scheduler == 'step':
                scheduler = lrs.StepLR(optimizer,
                                       step_size=self.hparams.lr_decay_steps,
                                       gamma=self.hparams.lr_decay_rate)
            elif self.hparams.lr_scheduler == 'cosine':
                scheduler = lrs.CosineAnnealingLR(optimizer,
                                                  T_max=self.hparams.lr_decay_steps,
                                                  eta_min=self.hparams.lr_decay_min_lr)
            else:
                raise ValueError('Invalid lr_scheduler type!')

            if self.hparams.warmup_epoch > 0:
                scheduler_warmup = GradualWarmupScheduler(optimizer, multiplier=1, total_epoch=self.hparams.warmup_epoch, after_scheduler=scheduler)
                return [optimizer], [scheduler_warmup]
            else:
                return [optimizer], [scheduler]

    def configure_loss(self):
        loss = self.hparams.loss.lower()
        if loss == 'mse':
            self.loss_function = F.mse_loss
        elif loss == 'l1':
            self.loss_function = F.l1_loss
        else:
            raise ValueError("Invalid Loss Type!")

    def load_model(self):
        name = self.hparams.model_name
        # Change the `snake_case.py` file name to `CamelCase` class name.
        # Please always name your model file name as `snake_case.py` and
        # class name corresponding `CamelCase`.
        camel_name = ''.join([i.capitalize() for i in name.split('_')])
        try:
            Model = getattr(importlib.import_module(
                '.'+name, package=__package__), camel_name)
        except:
            raise ValueError(
                f'Invalid Module File Name or Invalid Class Name {name}.{camel_name}!')

        return self.instancialize(Model)

    def instancialize(self, Model, **other_args):
        """ Instancialize a model using the corresponding parameters
            from self.hparams dictionary. You can also input any args
            to overwrite the corresponding value in self.hparams.
        """
        class_args = inspect.getargspec(Model.__init__).args[1:]
        inkeys = self.hparams.keys()
        args1 = {}
        for arg in class_args:
            if arg in inkeys:
                args1[arg] = getattr(self.hparams, arg)
        args1.update(other_args)
        return Model(**args1)


import os
import sys
sys.path.append(os.path.dirname(os.path.abspath(os.path.dirname(__file__))))
import tokens
import discord
from discord.ext import commands
from discord import app_commands
import datetime
import pytz
import requests
import asyncio

apiurl="https://developer-lostark.game.onstove.com/gamecontents/challenge-abyss-dungeons"
key=tokens.apikey

class GameContents(commands.Cog):
    def __init__(self, bot) -> None:
        self.bot = bot

    @app_commands.command(name="도비스", description="금주의 도전 어비스 던전을 확인합니다.")
    async def abyss(self, interaction: discord.Interaction) -> None:
        embed = self.abyssofweek()
        await interaction.response.send_message(embed=embed)

    def abyssofweek(self):
        url=apiurl
        headers={'accept': 'application/json', 'authorization': 'bearer ' + key,'Content-Type':'application/json'}
        try:
            response=requests.get(url,headers=headers,verify=False).json()
            dungeonlist=[]
            for dungeon in response:
                dungeonlist.append(dungeon.get("Name"))
            embed=discord.Embed(title="**:calendar_spiral: 금주의 도전 어비스 던전**", timestamp=datetime.datetime.now(pytz.timezone('UTC')), color=0x00ff00)
            embed.add_field(name=f':video_game: {str(dungeonlist[0])}',value="\n",inline="False")
            embed.add_field(name=f':video_game: {str(dungeonlist[1])}',value="\n",inline="False")
            embed.set_footer(text="Made by.ㅈㅇㅈ#0081")
            return embed
        except Exception as e:
            embed=discord.Embed(title="오류 발생",timestamp=datetime.datetime.now(pytz.timezone('UTC')),color=0x9B0000)
            embed.add_field(name=e,value="\n",inline=False)
            return embed

async def setup(bot) -> None:
    await bot.add_cog(GameContents(bot))


#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import division, unicode_literals

import os
import re
import string

from pycorenlp import *
import sys
# reload(sys)
# sys.setdefaultencoding("utf-8")

import codecs
import chardet
######################data preprocess#####################
def id_tag_dict():
	data_path = os.pardir + '/data'

	id_tag = data_path+'/androidAPI_id_tag.csv'

	#
	# make id_tag dictionary
	#
	file = open(id_tag,'r',encoding='utf-8')
	id_tag_dict = {}
	for i, line in enumerate(file):
		id = line.split(',')[0]
		tag = line.split(',')[1].strip()

		#
		# make dictionary
		#
		id_tag_dict[int(id)] = tag
	return id_tag_dict

# get the filename URL and ID
def get_filenamelistWithID():
    file_path=os.getcwd()
    file_filelist= open(file_path + '/filenamelist_copy.csv')
    # file_path + '/filenamelist_copy.csv'
    # /Users/Grand/Downloads/AndroidAPIKG/HDSKG/tempdata/filenamelist_copy.csv
    filename_list={}
    for filenameline in file_filelist:
        filenames=filenameline.split(',',1)
        if len(filenames)==2 :
            key,value=filenames[1].strip('\n'),filenames[0]
            filename_list[key]=value
    return filename_list

# down vote
# Here's a safe way for any iterable of delimiters, using regular expressions:
#
# >>> import re
# >>> delimiters = "a", "...", "(c)"
# >>> example = "stackoverflow (c) is awesome... isn't it?"
# >>> regexPattern = '|'.join(map(re.escape, delimiters))
# >>> regexPattern
# 'a|\\.\\.\\.|\\(c\\)'
# >>> re.split(regexPattern, example)
# ['st', 'ckoverflow ', ' is ', 'wesome', " isn't it?"]

def split(delimiters, string, maxsplit=0):
    import re
    regexPattern = '|'.join(map(re.escape, delimiters))
    return re.split(regexPattern, string, maxsplit)

def main():
	data_path = '/Users/Grand/Downloads/AndroidAPIKG/HDSKG/tempdata'
	allSent = []
	# file_name_list=get_filenamelistWithID()
	file_w = open(data_path+'/androidAPIallSentICSME.txt','w',encoding='utf-8')
	wiki = data_path+'/AndroidAPI_Doc_out_ascii.txt'
	all_Doc_file = open(wiki,'r',encoding='utf-8')

	for i, line in enumerate(all_Doc_file):

		line=line.strip('\n')
		idstr=line.split('\t')[0]
		otherstr=line.split('\t')[1:]
		id = int(idstr)

		listdot=[]

		listdot=(' '.join(otherstr)).strip().split('. ')

		listq = []
		listexclaim = []

		for dotsentence in listdot:
			if '? ' in dotsentence:
				questionlist=dotsentence.split('? ')
				qj=0
				for qsent in questionlist:
					if qj==len(questionlist)-1:
						listq.append(qsent)
					else :
						listq.append(qsent+'? ')
					qj=qj+1
			else:
				listq.append(dotsentence)

		for questionsentece in listq:
			if '! ' in questionsentece:
				excllist = questionsentece.split('! ')
				exj=0
				for exqsent in excllist:
					if exj == len(excllist) - 1:
						listexclaim.append(exqsent)
					else:
						listexclaim.append(exqsent + '! ')
					exj=exj+1
			else:
				listexclaim.append(questionsentece)

		onewiki=[]
		for oneS in listexclaim:
			if ('? ' in oneS) or ('! ') in oneS :
				onewiki.append(oneS)
			else:
				onewiki.append(oneS+'. ')

		# if sentence lower than 3 word, delete it
		#
		for j, oneSent in enumerate(onewiki):
			# print oneSent
			# print len(oneSent)

			if len(oneSent.replace('  ',' ').strip().split(' ')) < 3 or len(oneSent) < 3:
				# print oneSent
				del onewiki[j]
			#

			temptext=oneSent
			if type(oneSent) is bytes:
				temptext="".join(map(chr,oneSent))

			temptext=temptext.strip()
			if temptext is "":
				continue

			#清除非ASCII符号
			temptext=''.join([i if ord(i) < 128 else ' ' for i in temptext])
			dirty=temptext

			temptext=re.sub(r'[\0\200-\377]','',dirty)
			temptext=str(temptext)

			# print(type(temptext))

			temptext = ('%d_%d\t%s') % (id, j, temptext.strip())
			a1re=re.compile(r'\.+')
			temptext=a1re.sub('.',temptext)

			oneSent=temptext
			# print oneSent + '    after bbbbbbbbbbbbbb'
			# allSent.append(oneSent)
			file_w.write(oneSent + ' \n')
		print("constructing document index: "+str(i))
	file_w.close()
	all_Doc_file.close()

main()


#!/bin/python3

import math
import os
import random
import re
import sys

# Complete the aVeryBigSum function below.
def aVeryBigSum(ar):

    #default the val to zero
    arrayTotal = 0

    if len(ar) < 1 or len(ar) > 10:
        print("out of constraint range")
        exit()

    for i in range(len(ar)):

        #convert the array val to an int
        #ar_long = int(ar[i])
        if ar[i] < 0 or ar[i] > 10**10 :
            print("out of constraint range")
            exit()
        else:
            arrayTotal = arrayTotal + ar[i]

    return arrayTotal;

if __name__ == '__main__':
    fptr = open(os.environ['OUTPUT_PATH'], 'w')

    ar_count = int(input())

    ar = list(map(int, input().rstrip().split()))

    result = aVeryBigSum(ar)

    fptr.write(str(result) + '\n')

    fptr.close()


import embera
import networkx as nx
import matplotlib.pyplot as plt

bench_graph_list = embera.benchmark.topologies.embera_bench()

nrows = 7
ncols = len(bench_graph_list)//nrows + bool(len(bench_graph_list)%nrows)
fig, axs = plt.subplots(nrows, ncols, subplot_kw={'aspect':'equal'})
fig.set_size_inches(ncols , nrows)
for i, ax in enumerate(axs.flat):
    if i>=len(bench_graph_list): fig.delaxes(ax); continue
    Sg = bench_graph_list[i]
    pos = Sg.graph.setdefault('pos', nx.spring_layout(Sg))
    nx.draw(Sg, pos=pos, ax=axs.flat[i], node_size=1, width=0.2, edge_color='grey')

plt.subplots_adjust(left=0, right=1, bottom=0, top=1, wspace=0, hspace=0)

plt.show()


from gensim.models import LdaModel
from util import topic_match
from gensim import corpora

model_file = '.\\model\\lda_model'
lda = LdaModel.load(model_file)

corpus_file = '.\\data\\corpus_cut.txt'
sentences = []
for line in open(corpus_file, 'r', encoding='utf8'):
    line = line.strip()
    sentences.append(line.split(' '))

    #对文本进行处理，得到文本集合中的词表
dictionary = corpora.Dictionary(sentences)

comment_file = '.\\data\\comment.txt'
answer_file = '.\\data\\answer.txt'
result_file = 'result.txt'
topic_match(comment_file, answer_file, result_file, dictionary, lda)


# Создайте программу для игры в ""Крестики-нолики"".
from tabulate import tabulate

# a = [[1, 2, 3],
#      [4, 5, 6],
#      [7, 8, 9]]
# X = "X"
# O = "O"

b = [["." for i in range(3)] for j in range(3)]
b[1] = "123"

print(tabulate(b, tablefmt="heavy_grid"))

Task_5/3_Крестики нолики.py

3_Крестики нолики.py


import hashlib

h = hashlib.sha256()
file = "./rufus-3.20.exe"

file_hash = open("hash.txt","r").read()

with open(file,"rb") as f:
    content = f.read()
    h.update(content)

if(file_hash == h.hexdigest()):
    print("Software Authentic")

else:
    print("Software Not Authentic!")


from matplotlib.axes import Axes
import matplotlib.pyplot as plt
import pandas as pd

from base import Threaded
from data import store as store
from utils import ui, logger

_logger = logger.get_logger()

class Chart(Threaded):
    figure: plt.Figure
    ax: plt.Axes
    ticker: str
    history: pd.DataFrame

    def __init__(self, ticker: str, days: int = 365, live: bool = False):
        if not store.is_ticker(ticker):
            raise ValueError(f'Ticker {ticker} is invalid')
        if days <= 10:
            raise ValueError(f'Days must be greater than 10')

        self.ticker = ticker.upper()
        self.days = days
        self.live = live
        self.history = pd.DataFrame()
        self.company: dict = {}
        self.figure, self.ax = plt.subplots(figsize=ui.CHART_SIZE)

        plt.style.use(ui.CHART_STYLE)
        plt.margins(x=0.1)
        plt.subplots_adjust(bottom=0.15)

        self.figure.canvas.manager.set_window_title(f'{self.ticker}')
        self.ax.grid(which='major', axis='both')

    @Threaded.threaded
    def fetch_history(self):
        self.task_state = 'None'

        _logger.info(f'Fetching history for {self.ticker}...')

        self.task_ticker = self.ticker
        self.history = store.get_history(self.ticker, days=self.days, live=self.live)
        self.company = store.get_company(self.ticker)

        self.ax.set_title(f'{self.company["name"]} (daily)')

        self.task_state = 'Done'

    def plot_ohlc(self) -> tuple[plt.Figure, plt.Axes]:
        if self.history.empty:
            _logger.info(f'Need history for {self.ticker}. Fetching...')
            self.fetch_history()

        self.ax.secondary_yaxis('right')

        if self.history.iloc[-1]['close'] < 30.0:
            self.ax.yaxis.set_major_formatter('{x:.2f}')
        else:
            self.ax.yaxis.set_major_formatter('{x:.0f}')

        # Setup values
        interval = 10
        width1 = 0.5
        width2 = 0.05
        color_up = 'green'
        color_dn = 'red'
        length = len(self.history)

        # Ticks
        ticks = self.history.index[::interval]
        self.ax.set_xticks(ticks)
        self.ax.tick_params(axis='x', labelrotation=45)
        labels = [self.history.iloc[i]['date'].strftime(ui.DATE_FORMAT_MDY) for i in range(0, length, interval)]
        self.ax.set_xticklabels(labels)

        # Find up and down days
        up = self.history[self.history.close >= self.history.open]
        dn = self.history[self.history.close < self.history.open]

        # Up prices
        self.ax.bar(up.index, up.high - up.close, width2, bottom=up.close, color=color_up)
        self.ax.bar(up.index, up.close - up.open, width1, bottom=up.open,  color=color_up)
        self.ax.bar(up.index, up.low - up.open,   width2, bottom=up.open,  color=color_up)

        # Down prices
        self.ax.bar(dn.index, dn.high - dn.open,  width2, bottom=dn.open,  color=color_dn)
        self.ax.bar(dn.index, dn.close - dn.open, width1, bottom=dn.open,  color=color_dn)
        self.ax.bar(dn.index, dn.low - dn.close,  width2, bottom=dn.close, color=color_dn)

        return (self.figure, self.ax)

    def plot_history(self, close: bool = False) -> plt.Figure:
        if self.history.empty:
            _logger.info(f'Need history for {self.ticker}. Fetching...')
            self.fetch_history()

        self.ax.secondary_yaxis('right')

        if self.history.iloc[-1]['close'] < 30.0:
            self.ax.yaxis.set_major_formatter('{x:.2f}')
        else:
            self.ax.yaxis.set_major_formatter('{x:.0f}')

        length = len(self.history)
        dates = [self.history.iloc[index]['date'].strftime(ui.DATE_FORMAT_MDY) for index in range(length)]

        # Grid and ticks
        self.ax.set_xticks(range(0, length+1, int(length/12)))
        self.ax.tick_params(axis='x', labelrotation=45)

        # Plot Highs & Lows using simple lines
        if close:
            self.ax.plot(dates, self.history.close, '-b', linewidth=0.8)
        else:
            self.ax.plot(dates, self.history.high, '-g', linewidth=0.5)
            self.ax.plot(dates, self.history.low, '-r', linewidth=0.5)
            self.ax.fill_between(dates, self.history.high, self.history.low, facecolor='gray', alpha=0.4)

        return self.figure

if __name__ == '__main__':
    import sys
    import logging
    from utils import logger

    logger.get_logger(logging.DEBUG)

    if len(sys.argv) > 1:
        chart = Chart(sys.argv[1], days=180)
    else:
        chart = Chart('IBM')

    # figure = chart.plot_ohlc()
    # figure = chart.plot_history(close=True)
    figure = chart.plot_chart()
    plt.figure(figure)
    plt.show()


def get_inputs(path):
    inputs = []
    with open(path, 'r') as file:
        inputs =  [list(map(int, list(line.strip()))) for line in file]
    return inputs

def is_in_bounds(height_map, row, col):
    row_max = len(height_map)
    col_max = len(height_map[0])
    return row >= 0 and row < row_max and col >= 0 and col < col_max

def get_point(heigth_map, row, col):
    return int(heigth_map[row][col])

def is_lowpoint(height_map, row, col):
    row_up = row - 1
    row_down = row + 1
    col_left = col - 1
    col_right = col + 1

    #print("r, u, d", row, row_up, row_down)
    #print("c, l, r", col, col_left, col_right)

    current_point = height_map[row][col]# get_point(height_map, row, col)
    #print("Doing:", current_point, row, col)

    if is_in_bounds(height_map, row_up, col):
        if current_point >= height_map[row_up][col]:
            #print("up", current_point, height_map[row_up][col], row_up, col)
            return False

    if is_in_bounds(height_map, row_down, col):
        if current_point >= height_map[row_down][col]:
            #print("down", current_point, height_map[row_down][col], row_down, col)
            return False

    if is_in_bounds(height_map, row, col_left):
        if current_point >= height_map[row][col_left]:
            #print("left", current_point, height_map[row][col_left], row, col_left)
            return False

    if is_in_bounds(height_map, row, col_right):
        if current_point >= height_map[row][col_right]:
            #print("right", current_point, height_map[row][col_left], row, col_right)
            return False

    #print(f"up {height_map[row_up][col]} down {height_map[row_down][col]} left {height_map[row][col_left]} right {height_map[row][col_right]}")
    #print("Adding:", current_point, row, col)

    return True

def is_valid(row, col, height_map, visited):
    return is_in_bounds(height_map, row, col) and \
                not visited[row][col]

def all_visited(visited):
    for row in visited:
        for col in row:
            if not col:
                return False
    return True

def dfs(row, col, height_map, visited):
    if is_valid(row, col, height_map, visited):
        visited[row][col] = True
        if height_map[row][col] == 9:
            return 0
        else:
            sum = 1
            sum += dfs(row-1, col, height_map, visited)
            sum += dfs(row+1, col, height_map, visited)
            sum += dfs(row, col-1, height_map, visited)
            sum += dfs(row, col+1, height_map, visited)
            return sum
    else:
        return 0

def get_b(height_map, low_coords):
    visited = [[False for _ in range(len(height_map[0]))] for _ in range(len(height_map))]
    basin_value = []
    for row, col in low_coords:
        basin_value.append(dfs(row, col, height_map, visited))

    #print(basin_value)
    basin_value.sort()
    return basin_value[-1] * basin_value[-2] * basin_value[-3]

def get_a(height_map):
    lowpoints = []
    low_coords = []
    for row in range(len(height_map)):
        for col in range(len(height_map[0])):
            if is_lowpoint(height_map, row, col):
                lowpoints.append(height_map[row][col])
                low_coords.append((row, col))
    #print(lowpoints)
    return sum([s + 1 for s in lowpoints]), low_coords

if __name__ == "__main__":
    # height_map = get_inputs("test/test09")
    height_map = get_inputs("inputs/day09")
    #print(test_input)
    a_sum, low_coords = get_a(height_map)
    print(a_sum)
    print(get_b(height_map, low_coords))


import asyncio
import pickle
import grpc
import logging

from .grpc import (
    scheduler_pb2 as scheduler_worker_messages,
    scheduler_pb2_grpc as scheduler_worker_service
)

from apscheduler.schedulers.asyncio import AsyncIOScheduler
from sqlalchemy.orm import sessionmaker

from app.core.settings import get_settings
from app.db.base import create_session_factory
from app.schemas.task import Task
from .services.scheduler import add_task_to_scheduler, start_tasks
from .services.logger import init_logger

class SchedulerWorkerService(scheduler_worker_service.SchedulerWorkerServicer):
    def __init__(
        self,
        scheduler: AsyncIOScheduler,
        sessionmaker: sessionmaker,
    ):
        self.scheduler = scheduler
        self.sessionmaker = sessionmaker

    async def add_task(
        self,
        request: scheduler_worker_messages.AddTaskRequest,
        context: grpc.aio.ServicerContext,
    ) -> scheduler_worker_messages.ResponseStub:
        task_obj: Task = pickle.loads(request.task_obj)
        async with self.sessionmaker() as session:
            logger.info(f'add_task -> {task_obj.search_phrase}')
            await add_task_to_scheduler(
                task_obj=task_obj,
                session=session,
                scheduler=self.scheduler,
                timeout=request.timeout,
            )
        return scheduler_worker_messages.ResponseStub()

    async def remove_task(
        self,
        request: scheduler_worker_messages.TaskIdRequest,
        context: grpc.aio.ServicerContext,
    ) -> scheduler_worker_messages.RemoveResponse:
        logger.info(f'remove_task -> {request.task_id}')
        self.scheduler.remove_job(str(request.task_id))

        return scheduler_worker_messages.RemoveResponse(success=True)

async def serve_worker() -> None:

    # create basics for worker
    settings = get_settings()
    scheduler = AsyncIOScheduler()
    contextsession = create_session_factory(settings.postgres_uri)

    # setup server
    server = grpc.aio.server()
    scheduler_worker_service.add_SchedulerWorkerServicer_to_server(
        server=server,
        servicer=SchedulerWorkerService(
            scheduler=scheduler,
            sessionmaker=contextsession,
        ),
    )

    server.add_insecure_port(settings.worker_socket)
    logger.info("Starting server on %s", settings.worker_socket)
    await server.start()

    logger.info("Starting tasks from db")
    await start_tasks(contextsession, scheduler, settings.parse_timeout)

    logger.info("Starting scheduler")
    scheduler.start()

    await server.wait_for_termination()

if __name__ == "__main__":
    # simple logger
    file_name = __file__.split("/")[-1]
    init_logger(file_name, logging.DEBUG)
    logger = logging.getLogger(file_name)

    # run worker
    asyncio.run(serve_worker())


# Write an algorithm that takes an array and moves all of the zeros to the end,
# preserving the order of the other elements.
#
# move_zeros([1, 0, 1, 2, 0, 1, 3]) # returns [1, 1, 2, 1, 3, 0, 0]

def move_zeros(array):
    result = list(filter(lambda x: x != 0, array))
    for _ in range(array.count(0)): result.append(0)
    return result

print(move_zeros([1, 0, 1, 2, 0, 1, 3]))


import argparse
from tqdm import tqdm
import tensorflow as tf
from tensorflow import keras

from quality_net_utilities import *

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description='Trains a feature-aware normalization model.')

    parser.add_argument('--dataset_path',dest='dataset_path',
                        action='store',type=str,default=None,
                        help="Path to hdf5 dataset.")
    parser.add_argument('--input_height',dest = 'input_height',
                        action = 'store',type = int,default = 256,
                        help = 'The file extension for all images.')
    parser.add_argument('--input_width',dest = 'input_width',
                        action = 'store',type = int,default = 256,
                        help = 'The file extension for all images.')

    parser.add_argument('--log_every_n_steps',dest = 'log_every_n_steps',
                        action = 'store',type = int,default = 100,
                        help = 'How often are the loss and global step logged.')
    parser.add_argument('--save_summary_folder',dest = 'save_summary_folder',
                        action = 'store',type = str,default = 'summaries',
                        help = 'Directory where summaries are saved.')
    parser.add_argument('--save_checkpoint_folder',dest = 'save_checkpoint_folder',
                        action = 'store',type = str,default = 'checkpoints',
                        help = 'Directory where checkpoints are saved.')
    parser.add_argument('--batch_size',dest = 'batch_size',
                        action = 'store',type = int,default = 4,
                        help = 'Size of mini batch.')
    parser.add_argument('--number_of_epochs',dest = 'number_of_epochs',
                        action = 'store',type = int,default = 50,
                        help = 'Number of epochs in the training process.')
    parser.add_argument('--learning_rate',dest = 'learning_rate',
                        action = 'store',type = float,default = 0.001,
                        help = 'Learning rate for the SGD optimizer.')

    #Data augmentation
    for arg in [
        ['brightness_max_delta',8. / 255.,float],
        ['saturation_lower',0.9,float],['saturation_upper',1.1,float],
        ['hue_max_delta',0.2,float],
        ['contrast_lower',0.9,float],['contrast_upper',1.1,float],
        ['min_jpeg_quality',90,int],['max_jpeg_quality',100,int],
        ['probability',0.5,float]]:
        parser.add_argument('--{}'.format(arg[0]),dest=arg[0],
                            action='store',type=arg[2],default=arg[1])

    args = parser.parse_args()

    data_augmentation_params = {
        'brightness_delta':args.brightness_max_delta,
        'saturation_lower':args.saturation_lower,
        'saturation_upper':args.saturation_upper,
        'hue_delta':args.hue_max_delta,
        'contrast_lower':args.contrast_lower,
        'contrast_upper':args.contrast_upper,
        'min_jpeg_quality':args.min_jpeg_quality,
        'max_jpeg_quality':args.max_jpeg_quality,
        'probability':args.probability}

    print("Setting up network...")
    mobilenet_v2 = keras.applications.DenseNet121(
        input_shape=[args.input_height,args.input_width,3],
        include_top=False,classes=2,pooling='max')
    quality_net = quality_net_model(
        mobilenet_v2,args.input_height,args.input_width)

    print("Setting up data generator...")
    flipper = Flipper()
    colour_augmenter = ColourAugmentation(**data_augmentation_params)

    def augment(x):
        return flipper(colour_augmenter(x))

    data_generator = DataGenerator(args.dataset_path,augment)
    def load_generator():
        for image,label in data_generator.generate():
            yield image,label
    generator = load_generator
    output_types = (tf.float32,tf.float32)
    output_shapes = (
        tf.TensorShape((args.input_height,args.input_width,3)),
        tf.TensorShape((1)))
    tf_dataset = tf.data.Dataset.from_generator(
        generator,output_types=output_types,output_shapes=output_shapes)
    tf_dataset = tf_dataset.repeat()
    tf_dataset = tf_dataset.batch(args.batch_size)
    tf_dataset = tf_dataset.prefetch(args.batch_size*5)

    print("Setting up training...")
    auc = tf.keras.metrics.AUC()
    acc = Accuracy()
    rec = Recall()
    prec = Precision()
    loss_fn = keras.losses.BinaryCrossentropy()
    quality_net.compile(
        optimizer=keras.optimizers.Adam(learning_rate=args.learning_rate),
        loss=loss_fn, metrics=[auc,acc,rec,prec])
    steps_per_epoch = data_generator.n_images // args.batch_size

    print(steps_per_epoch)
    print("Training...")
    tensorboard_callback = keras.callbacks.TensorBoard(
        log_dir=args.save_summary_folder)
    checkpoint_callback = tf.keras.callbacks.ModelCheckpoint(
        args.save_checkpoint_folder + '-{epoch:02d}')
    lr_callback = tf.keras.callbacks.ReduceLROnPlateau(
        'loss',min_lr=1e-6)

    all_callbacks = [
        tensorboard_callback,checkpoint_callback,lr_callback]

    quality_net.fit(
        x=tf_dataset,batch_size=args.batch_size,epochs=args.number_of_epochs,
        callbacks=all_callbacks,steps_per_epoch=steps_per_epoch)


#!/usr/bin/env python
import logging
from pathlib import Path

from edc_constants.constants import IGNORE
from edc_test_utils import DefaultTestSettings, func_main

app_name = "edc_glucose"

base_dir = Path(__file__).absolute().parent

project_settings = DefaultTestSettings(
    calling_file=__file__,
    BASE_DIR=base_dir,
    APP_NAME=app_name,
    SUBJECT_VISIT_MODEL="edc_visit_tracking.subjectvisit",
    LIST_MODEL_APP_LABEL="edc_glucose",
    EDC_NAVBAR_VERIFY_ON_LOAD=IGNORE,
    EDC_AUTH_SKIP_SITE_AUTHS=True,
    EDC_AUTH_SKIP_AUTH_UPDATER=True,
    EXTRA_INSTALLED_APPS=[
        "edc_glucose.apps.AppConfig",
        "edc_dx.apps.AppConfig",
        "edc_dx_review.apps.AppConfig",
        "visit_schedule_app.apps.AppConfig",
    ],
    EDC_DX_LABELS=dict(dm="Diabetes"),
    EDC_DX_REVIEW_LIST_MODEL_APP_LABEL="edc_glucose",
    add_dashboard_middleware=True,
    add_lab_dashboard_middleware=True,
    use_test_urls=True,
).settings

def main():
    func_main(project_settings, *[f"{app_name}.tests"])

if __name__ == "__main__":
    logging.basicConfig()
    main()


import typing
from pathlib import Path

import numpy as np
import scipy.fftpack
from scipy import signal
import einops
import functools

import torch

import paderbox as pb
import padertorch as pt

from padertorch.contrib.cb.transform import stft as pt_stft, istft as pt_istft
from padertorch.contrib.cb import complex as pt_complex

def kaldi_stft(time_signal, size=512, shift=160, *, axis=-1, window_length=400, pad=False, fading=False):
    # ToDo: window
    return pb.transform.stft(**locals())

def kaldi_istft(stft_signal, size=512, shift=160, *, axis=-1, window_length=400, pad=False, fading=False):
    # ToDo: window
    return pb.transform.istft(**locals())

def stft_to_cepstrum(
        stft_signal,
        norm='ortho',
        eps=None,
):
    """
    Reference implementation to get the cepstrum: dft -> abs -> log -> dft
    >>> signal1 = np.array([1, 2, 3, 4])
    >>> signal2 = np.array([1, 2, 3, 4, 5, 6, 7, 8])

    >>> dft_signal = np.fft.fft(signal1)
    >>> np.fft.fft(np.log(np.abs(dft_signal)), norm='ortho')
    array([2.53758691+0.j, 0.80471896+0.j, 0.45814537+0.j, 0.80471896+0.j])
    >>> dft_signal = np.fft.fft(signal2)
    >>> np.fft.fft(np.log(np.abs(dft_signal)), norm='ortho')
    array([5.67812692+0.j, 1.21752299+0.j, 0.53177166+0.j, 0.33614941+0.j,
           0.28670713+0.j, 0.33614941+0.j, 0.53177166+0.j, 1.21752299+0.j])

    Implementation without redundant operations: rdft -> abs -> log -> dct
    >>> rdft_signal = np.fft.rfft(signal1)
    >>> stft_to_cepstrum(rdft_signal)
    array([2.53758691, 0.80471896, 0.45814537])

    >>> rdft_signal = np.fft.rfft(signal2)
    >>> stft_to_cepstrum(rdft_signal)
    array([5.67812692, 1.21752299, 0.53177166, 0.33614941, 0.28670713])

    Note: a scaling only influences the first value
    >>> rdft_signal = np.fft.rfft(signal1)
    >>> stft_to_cepstrum(rdft_signal * 2)
    array([3.92388127, 0.80471896, 0.45814537])

    >>> stft_to_cepstrum([0., 0, 0])
    array([-1416.79283706,     0.        ,     0.        ])
    >>> stft_to_cepstrum([0., 0, 0], eps=0)
    array([-inf,  nan,  nan])
    >>> stft_to_cepstrum([1., 1, 1])
    array([0., 0., 0.])
    >>> stft_to_cepstrum([1., 1, 1], eps=0)
    array([0., 0., 0.])
    >>> stft_to_cepstrum([0., 1, 1])
    array([-354.19820927, -354.19820927, -354.19820927])
    >>> stft_to_cepstrum([0., 1, 1], eps=0)
    array([-inf, -inf, -inf])
    """
    stft_signal = np.asarray(stft_signal)

    assert stft_signal.shape[-1] % 2 == 1, stft_signal.shape

    stft_signal = np.abs(stft_signal)

    if eps is None:
        eps = np.finfo(stft_signal.dtype).tiny

    if eps != 0:
        stft_signal = np.maximum(stft_signal, eps)

    cepstrum = scipy.fftpack.dct(np.log(stft_signal), type=1)
    if norm == 'ortho':
        cepstrum = cepstrum / np.sqrt(2 * (stft_signal.shape[-1] - 1))
    elif norm is None:
        pass
    else:
        raise ValueError(norm)
    return cepstrum

def sign(signal, *, axis=-1, eps=1e-4, eps_style='where', ord=None):
    """Unit normalization.

    Numpy handles complex numbers in the sign function in a strange way.
    See:  https://github.com/numpy/numpy/issues/13179

    Args:
        signal: STFT signal with shape (..., T, D).
        eps_style: in ['plus', 'max']
    Returns:
        Normalized STFT signal with same shape.

    >>> sign([1+1j, 0,  1e-4])
    array([0.70710678+0.70710678j, 0.        +0.j        ,
           1.        +0.j        ])
    """
    signal = np.asarray(signal)
    norm = np.abs(signal)
    if eps_style == 'plus':
        norm = norm + eps
    elif eps_style == 'max':
        norm = np.maximum(norm, eps)
    elif eps_style == 'where':
        norm = np.where(norm == 0, eps, norm)
    else:
        assert False, eps_style
    return signal / norm

def interchannel_phase_differences_op(a, b):
    return sign(a * b.conj())

def interchannel_phase_differences(signal, second_channel=None, concatenate=False):
    """
    Calculates the inter channel phase differences:

        cos(angle(channel1 * channel2.conj()))
        sin(angle(channel1 * channel2.conj()))

    Args:
        signal: The stft signal.
            Shape: (..., channels, frames, features)
        second_channel:
            The corresponding second channel. When not given, use random
            sampled channels.
        concatenate:

    Returns:
            If concatenate True, return the concaternation of abs, cos and sin
            on the last axis.
            Otherwise return the tuple (cos, sin)

    >>> np.random.seed(0)
    >>> signal = np.ones([6, 4, 5]) * np.exp(1j * np.random.uniform(0, 2*np.pi, [6, 1, 1])) * (np.arange(6)[:, None, None] + 1)
    >>> c, s = interchannel_phase_differences(signal)
    >>> c[0, :, :]
    array([[0.81966208, 0.81966208, 0.81966208, 0.81966208, 0.81966208],
           [0.81966208, 0.81966208, 0.81966208, 0.81966208, 0.81966208],
           [0.81966208, 0.81966208, 0.81966208, 0.81966208, 0.81966208],
           [0.81966208, 0.81966208, 0.81966208, 0.81966208, 0.81966208]])
    >>> c[:, 0, 0], s[:, 0, 0]
    (array([0.81966208, 0.76070789, 0.93459697, 0.93459697, 0.72366352,
           0.90670355]), array([-0.57284734,  0.64909438,  0.35570844, -0.35570844, -0.69015296,
           -0.42176851]))
    >>> sig = interchannel_phase_differences(signal, concatenate=True)
    >>> sig[-1, 0, :]
    array([6.        , 6.        , 6.        , 6.        , 6.        ,
           0.81966208, 0.81966208, 0.81966208, 0.81966208, 0.81966208,
           0.57284734, 0.57284734, 0.57284734, 0.57284734, 0.57284734])

    >>> sig[:, 0, 0]
    array([1., 2., 3., 4., 5., 6.])

    """
    import itertools, random

    if second_channel is None:
        try:
            D = signal.shape[-3]
        except IndexError:
            raise IndexError(signal.shape)
        assert D >= 2, (D, signal.shape)
        l = list(itertools.permutations(range(D), 2))
        np.random.shuffle(l)
        second_channel = np.array(sorted(dict(l).items()))[:, 1]

    sincos = interchannel_phase_differences_op(signal, signal[..., second_channel, :, :])

    if concatenate:
        return np.concatenate([np.abs(signal), sincos.real, sincos.imag], axis=-1)
    else:
        return sincos.real, sincos.imag

def cepstrum(
        time_signal,
        size: int=1024,
        shift: int=256,
        *,
        window: typing.Callable=signal.blackman,
        window_length: int=None,
        fading: bool=True,
        pad: bool=True,
        symmetric_window: bool=False,
        # dft_norm=None,
):
    stft_signal = pb.transform.stft(
        time_signal,
        size=size,
        shift=shift,
        axis=-1,
        window=window,
        window_length=window_length,
        fading=fading,
        pad=pad,
        symmetric_window=symmetric_window,
        # dft_norm=dft_norm,
    )
    return stft_to_cepstrum(stft_signal)

class AbstractFeatureExtractor(pt.Configurable):
    def __repr__(self):
        import inspect
        sig = inspect.signature(self.__class__)
        p: inspect.Parameter
        kwargs = ', '.join([
            f'{name}={getattr(self, name)!r}'
            for name, p in sig.parameters.items()
            if p.kind in [
                # inspect.Parameter.POSITIONAL_ONLY,  # (pos_only, /)
                inspect.Parameter.POSITIONAL_OR_KEYWORD,  # (pos_or_key)
                # inspect.Parameter.VAR_POSITIONAL,  # (*args)
                inspect.Parameter.KEYWORD_ONLY,  # (*, key_only)
                # inspect.Parameter.VAR_KEYWORD,  # (**kwargs)
            ]
        ])

        return f'{self.__class__.__name__}({kwargs})'

class STFT(AbstractFeatureExtractor):
    """
    Feature extractor properties:

        __call__:
            returns the features, allows files and array as input
        property output_size:
            length of the last features axis

    Feature extractor optional properties:

        method stft:
            returns the stft signal, where the number of frames matches with
            the number of frames of the features. The last axis may have a
            different length.

        method istft:
            Inverse of stft. Has argument samples to cut the signal to the
            original length.

    >>> fe = STFT()
    >>> fe
    STFT(size=1024, shift=256, window_length=1024, pad=True, fading=True, output_size=513, window='blackman')

    >>> def print_properties(array):
    ...     print(f'array(shape={array.shape}, dtype={array.dtype})')
    >>> file = '/net/vol/jenkins/python_unittest_data/timit/data/sample_1.wav'

    >>> print_properties(STFT()([file]))
    array(shape=(1, 186, 513), dtype=complex128)
    >>> print_properties(AbsSTFT()([file]))
    array(shape=(1, 186, 513), dtype=float64)
    >>> print_properties(Cepstrum()([file]))
    array(shape=(1, 186, 513), dtype=float64)
    >>> print_properties(KaldiSTFT()([file]))
    array(shape=(1, 290, 257), dtype=complex128)
    >>> print_properties(AbsKaldiSTFT()([file]))
    array(shape=(1, 290, 257), dtype=float64)
    """

    @classmethod
    def finalize_dogmatic_config(cls, config):
        if config['window_length'] is None:
            config['window_length'] = cls.from_config({**config}).window_length
        if config['output_size'] is None:
            config['output_size'] = cls.from_config({**config}).output_size

    def __init__(
            self,
            size=1024,
            shift=256,
            window_length=None,
            pad=True,
            fading=True,
            # dft_norm='ortho',
            output_size=None,
            window='blackman'
    ):
        super().__init__()
        self.size = size
        self.shift = shift
        if window_length is None:
            self.window_length = size
        else:
            self.window_length = window_length
        self.pad = pad
        self.fading = fading
        # self.dft_norm = dft_norm
        self.output_size = self._get_output_size(output_size)
        self.window = window

        if callable(window):
            self._window = window
        elif isinstance(window, str):
            self._window = {
                'blackman': signal.windows.blackman,
                'hann': signal.windows.hann,
                'boxcar': signal.windows.boxcar,
                'triang': signal.windows.triang,
                'hamming':  signal.windows.hamming,
                'parzen': signal.windows.parzen,
                'cosine': signal.windows.cosine,
                'blackmanharris': signal.windows.blackmanharris,
                'flattop': signal.windows.flattop,
                'tukey': signal.windows.tukey,
                'bartlett': signal.windows.bartlett,
                'bohman': signal.windows.bohman,
                'kaiser2': functools.partial(signal.windows.kaiser, beta=2),
                'kaiser3': functools.partial(signal.windows.kaiser, beta=2),
                'povey': lambda *args: signal.windows.hann(*args)**0.85,
            }[window]
        else:
            raise ValueError(window)

    def _get_output_size(self, output_size):
        if output_size is None:
            return self.frequencies
        else:
            assert output_size == self.frequencies, (output_size, self.frequencies)
            return output_size

    @property
    def frequencies(self):
        return self.size // 2 + 1

    def stft(self, signal):
        kwargs = dict(
            size=self.size,
            shift=self.shift,
            axis=-1,
            window_length=self.window_length,
            window=self._window,
            fading=self.fading,
            pad=self.pad,
            # symmetric_window=False,
        )

        if pt_complex.is_torch(signal):
            return pt_stft(signal, **kwargs)
        else:
            return pb.transform.stft(signal, **kwargs)

    def istft(self, signal, num_samples=None):
        """

        Args:
            signal:
            samples:

        Returns:

        >>> def print_properties(array):
        ...     print(f'array(shape={array.shape}, dtype={array.dtype})')

        >>> fe_stft = STFT()
        >>> fe_stft
        STFT(size=1024, shift=256, window_length=1024, pad=True, fading=True, output_size=513, window='blackman')

        >>> audio = pb.io.load('/net/vol/jenkins/python_unittest_data/timit/data/sample_1.wav')
        >>> print_properties(audio)
        array(shape=(46797,), dtype=float64)
        >>> samples, = audio.shape

        >>> print_properties(fe_stft([audio]))
        array(shape=(1, 186, 513), dtype=complex128)
        >>> print_properties(fe_stft.istft(fe_stft([audio])))
        array(shape=(1, 46848), dtype=float64)
        >>> print_properties(fe_stft.istft(fe_stft([audio]), num_samples=samples))
        array(shape=(1, 46797), dtype=float64)

        >>> print_properties(fe_stft.istft(fe_stft(np.zeros([1023]))))
        array(shape=(1024,), dtype=float64)
        >>> print_properties(fe_stft.istft(fe_stft(np.zeros([1024]))))
        array(shape=(1024,), dtype=float64)
        >>> print_properties(fe_stft.istft(fe_stft(np.zeros([1025]))))
        array(shape=(1280,), dtype=float64)
        >>> print_properties(fe_stft.istft(fe_stft(np.zeros([1023])), num_samples=1023))
        array(shape=(1023,), dtype=float64)
        >>> print_properties(fe_stft.istft(fe_stft(np.zeros([1024])), num_samples=1024))
        array(shape=(1024,), dtype=float64)
        >>> print_properties(fe_stft.istft(fe_stft(np.zeros([1025])), num_samples=1025))
        array(shape=(1025,), dtype=float64)

        """
        kwargs = dict(
            size=self.size,
            shift=self.shift,
            # axis=axis,
            window=self._window,
            window_length=self.window_length,
            fading=self.fading,
            pad=self.pad,
            # symmetric_window=False,
            # dft_norm=self.dft_norm,
            num_samples=num_samples,
        )

        if pt_complex.is_torch(signal):
            time_signal = pt_istft(signal, **kwargs)
        else:
            time_signal = pb.transform.istft(signal, **kwargs)

        return time_signal

    _load_audio = None

    def stft_to_feature(self, stft_signals):
        return stft_signals

    def __call__(self, signals):
        if self._load_audio is None:
            if isinstance(signals, (str, Path)):
                self._load_audio = True
            elif isinstance(signals, (tuple, list)) and isinstance(signals[0], (str, Path)):
                self._load_audio = True
            else:
                self._load_audio = False

        if self._load_audio:
            signals = pb.io.load(signals, list_to='array')

        return self.stft_to_feature(self.stft(signals))

class AbsSTFT(STFT):
    def stft_to_feature(self, stft_signals):
        # if pt_complex.is_torch(signal):
        # else:
        # Should work with numpy as torch
        return abs(stft_signals)

class AbsIPDSTFT(STFT):

    def _get_output_size(self, output_size):
        if output_size is None:
            return (self.size // 2 + 1) * 3
        else:
            assert output_size == self.frequencies * 3, (output_size, self.frequencies * 3)
            return output_size

    def append_interchannel_phase_differences(self, signals):
        #  (channels, ..., frequencies)
        return interchannel_phase_differences(
            signals, concatenate=True
        )

    def stft_to_feature(self, stft_signals):
        return self.append_interchannel_phase_differences(
            stft_signals
        )

class Log1pAbsSTFT(STFT):
    """
    >>> fe = Log1pAbsSTFT()
    >>> fe
    Log1pAbsSTFT(size=1024, shift=256, window_length=1024, pad=True, fading=True, output_size=513, window='blackman')
    >>> fe.stft_to_feature(np.array([1, 5, 3+4j, -5]))
    array([0.69314718, 1.79175947, 1.79175947, 1.79175947])
    >>> fe(np.ones(10_000)).shape
    (43, 513)
    """
    def stft_to_feature(self, stft_signals):
        if pt_complex.is_torch(stft_signals):
            return torch.log1p(abs(stft_signals))
        else:
            return np.log1p(abs(stft_signals))

class Log1pCosSinAbsSTFT(STFT):
    """
    >>> fe = Log1pCosSinAbsSTFT()
    >>> fe
    Log1pCosSinAbsSTFT(size=1024, shift=256, window_length=1024, pad=True, fading=True, output_size=1539, window='blackman')
    >>> fe.stft_to_feature(np.array([1, 5, 3+4j, -5]))
    array([ 6.93147181e-01,  1.79175947e+00,  1.79175947e+00,  1.79175947e+00,
            1.00000000e+00,  1.00000000e+00,  6.00000000e-01, -1.00000000e+00,
            0.00000000e+00,  0.00000000e+00,  8.00000000e-01,  1.22464680e-16])
    >>> fe(np.ones(10_000)).shape
    (43, 1539)
    """

    def _get_output_size(self, output_size):
        if output_size is None:
            return self.frequencies * 3
        else:
            assert output_size == self.frequencies * 3, (output_size, self.frequencies * 3)
            return output_size

    def stft_to_feature(self, stft_signals):
        if pt_complex.is_torch(stft_signals):
            raise NotImplementedError()
            return torch.log1p(abs(stft_signals))
        else:
            angle = np.angle(stft_signals)
            return np.concatenate([
                np.log1p(abs(stft_signals)),
                np.cos(angle),
                np.sin(angle),
            ], axis=-1)

class AbsRealImagSTFT(STFT):
    """
    >>> fe = AbsRealImagSTFT()
    >>> fe
    AbsRealImagSTFT(size=1024, shift=256, window_length=1024, pad=True, fading=True, output_size=1539, window='blackman')
    >>> fe.stft_to_feature(np.array([1, 5, 3+4j, -5]))
    array([ 1.,  5.,  5.,  5.,  1.,  5.,  3., -5.,  0.,  0.,  4.,  0.])
    >>> fe(np.ones(10_000)).shape
    (43, 1539)
    >>> fe.stft_to_feature(torch.tensor(np.array([1, 5, 3+4j, -5])))
    tensor([ 1.,  5.,  5.,  5.,  1.,  5.,  3., -5.,  0.,  0.,  4.,  0.],
           dtype=torch.float64)
    >>> fe(torch.tensor(np.ones(10_000))).shape
    torch.Size([43, 1539])
    """

    def _get_output_size(self, output_size):
        if output_size is None:
            return self.frequencies * 3
        else:
            assert output_size == self.frequencies * 3, (output_size, self.frequencies * 3)
            return output_size

    def stft_to_feature(self, stft_signals):
        if pt_complex.is_torch(stft_signals):
            concatenate = torch.cat
        else:
            concatenate = np.concatenate
        return concatenate([
            abs(stft_signals),
            stft_signals.real,
            stft_signals.imag,
        ], axis=-1)

class Cepstrum(STFT):
    def stft_to_feature(self, stft_signals):
        return stft_to_cepstrum(stft_signals)

class ScaleIndependentCepstrum(STFT):
    """
    >>> rng = np.random.RandomState(0)
    >>> a = rng.randn(17)
    >>> fe = ScaleIndependentCepstrum()
    >>> fe.stft_to_feature(a)
    array([ 1.53981744,  1.28939946, -0.51793477, -1.71679596, -0.98117105,
            1.32819865, -0.59023165,  0.49065686,  0.0707627 ,  0.04265497,
            1.60944661,  0.20507146, -0.89059183,  1.23656373, -0.00519145,
            0.52410475])
    >>> fe.stft_to_feature(a * 1000)
    array([ 1.53981744,  1.28939946, -0.51793477, -1.71679596, -0.98117105,
            1.32819865, -0.59023165,  0.49065686,  0.0707627 ,  0.04265497,
            1.60944661,  0.20507146, -0.89059183,  1.23656373, -0.00519145,
            0.52410475])
    """

    @property
    def frequencies(self):
        return super().frequencies - 1

    def stft_to_feature(self, stft_signals):
        return stft_to_cepstrum(stft_signals)[..., 1:]

class KaldiSTFT(STFT):
    def __init__(
            self,
            size=512,
            shift=160,
            window_length=400,
            pad=False,
            fading=False,
            output_size=None,
            # dft_norm='ortho',
    ):
        super().__init__(
            size=size,
            shift=shift,
            window_length=window_length,
            pad=pad,
            fading=fading,
            output_size=output_size,
            # dft_norm=dft_norm,
        )

class AbsKaldiSTFT(KaldiSTFT, AbsSTFT):
    pass


import unittest

class TestItem6(unittest.TestCase):
    def test_slicing(self):
        a = ['red', 'orange', 'yellow', 'green', 'blue', 'purple']
        odds = a[::2]
        evens = a[1::2]
        self.assertEqual(['red', 'yellow', 'blue'], odds)
        self.assertEqual(['orange', 'green', 'purple'], evens)

        # reverse a list
        b = [1, 2, 3]
        b2 = b[::-1]
        self.assertEqual([3, 2, 1], b2)

        # select every 2nd item starting from the end and move backwards
        c = [1, 2, 3, 4, 5]
        c2 = c[::-2]
        self.assertEqual([5, 3, 1], c2)

        # stride can become pretty confusing!
        # Avoid using start, end, and stride together in a single slice!


# -*- coding: utf-8 -*-
from dajaxice.decorators import dajaxice_register
from dajax.core import Dajax
from family.models import Member, postimage
from django.template.defaultfilters import linebreaksbr

def show_sex_in_chinese(sex):
      if sex == 'M':
             return u'男'
      else:
             return u'女'

def get_first_thumbnail_of_member(object):
         try:
           r=object.postimage_set.all().order_by('id')[0]
           if r:
                return r.thumbnail.url
         except IndexError:
           return 'https://lishefei.s3.amazonaws.com/lis/images/lhz/placeholder.png'

def get_nth_item_image_of_member(object,n):
    r=list(object.postimage_set.all().order_by('id')[n-1:n])
    if r:
     return r[0].image.url
    return ''

@dajaxice_register
def get_member(req, pk):
    dajax = Dajax()
    p = Member.objects.get(pk=pk)
    dajax.assign('#image', 'src', get_first_thumbnail_of_member(p))
    dajax.assign('#name', 'innerHTML', p.name)
    dajax.assign('#sex', 'innerHTML', show_sex_in_chinese(p.sex))
    dajax.assign('#desc', 'innerHTML', linebreaksbr(p.desc))
    dajax.assign('#image1', 'src', get_nth_item_image_of_member(p,2))
    dajax.assign('#image2', 'src', get_nth_item_image_of_member(p,3))
    dajax.assign('#image3', 'src', get_nth_item_image_of_member(p,4))
    dajax.assign('#image4', 'src', get_nth_item_image_of_member(p,5))

    return dajax.json()


from flask_pymongo import PyMongo
from flask import Flask
app=Flask(__name__)
app.config["MONGO_URI"]="mongodb://localhost:27017/Jovian"
#how to check if the database is connected
mongo=PyMongo(app)
print(mongo.db)
JOBS=mongo.db.Jobs.find() # JOBS is a cursor
jobs=[job for job in JOBS] # jobs is a list
print(jobs)


import numpy as np
import csv
import random
from networkx.generators.random_graphs import erdos_renyi_graph

def getGraph(v, i):
    #p=random.uniform(0.1, 0.3)
    g = erdos_renyi_graph(v, i) # erdos_renyi_graph produces a random graph with probability p. Setting it 0.5 means the edges are completely random
    return g

graphCount = 30
gsize = 90
for i in [0.1, 0.2, 0.3, 0.4, 0.5]:
    with open('graphs'+str(graphCount)+'size'+str(gsize)+'EdgeDensity'+str(i)+'.csv', 'w', newline='') as graphCsv:
        writer = csv.writer(graphCsv)
        for count in range(graphCount):
            graph = getGraph(gsize, i)
            indexedGraph = [count]+list(graph.edges())
            print(indexedGraph)
            writer.writerow(indexedGraph)


from rdflib import Graph

g = Graph()
g.parse("poc/rdfMetier.ttl", format="ttl")

def trouverMetier(tache):
    result = []
    resultQuery = g.query(
        'SELECT DISTINCT ?metier WHERE { ?metier :Fait ?action . ?action rdfs:label "' + tache + '" .}')
    for row in resultQuery:
        result.append(row)

    return result

def getLabel(uris):
    result = []
    for uri in uris:
        resultQuery = g.query('SELECT DISTINCT ?label WHERE { ' + uri.n3() + ' rdfs:label ?label .}')
        for row in resultQuery:
            result.append(row)

    return result