import os
import random
import uuid
import time
import numpy as np
from datetime import datetime
import math
# video edit
from moviepy.editor import ImageSequenceClip, TextClip, VideoFileClip,\
    concatenate_videoclips#, ImageClip, CompositeVideoClip
from moviepy.audio.AudioClip import AudioArrayClip
#face track
import cv2
#voice
import librosa
from AudioLib.AudioProcessing import AudioProcessing

#get facial classifiers
CCPATH = './Lib/site-packages/cv2/haar-cascade-files-master/'
face_cascade = cv2.CascadeClassifier(CCPATH +'haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier(CCPATH +'haarcascade_eye.xml')

class VideoEditor:

    DEFAULT_MASK_PATH = os.path.abspath("./masks/1.png")
    TEMP_FOLDER_PATH = os.path.abspath("./temp_files")
    DEFAULT_FILENAME = "output.mp4"
    BLURE_FULL_IMG = (120, 120)
    BLURE_OLD_FACES = (75, 75)
    CURRENT_DIR = None

    def __init__(self, input_video, mask_path = None, output_video = None):

        # current dir reset
        if VideoEditor.CURRENT_DIR is None:
            VideoEditor.CURRENT_DIR = os.getcwd()
        else:
            os.chdir(VideoEditor.CURRENT_DIR)

        # check if input video exist
        if not os.path.isfile(input_video):
            raise Exception('Error : input video path not found')
        self.input_video = os.path.abspath(input_video)

        # make tmp folder
        if not os.path.isdir(VideoEditor.TEMP_FOLDER_PATH):
            os.mkdir(VideoEditor.TEMP_FOLDER_PATH)
        now = datetime.now()
        self.tmp_folder = os.path.abspath(os.path.join(VideoEditor.TEMP_FOLDER_PATH,\
            now.strftime("%Y_%m_%d_%H_%M_%S___") + str(uuid.uuid4()) ))
        print("temp folder '{}'".format(self.tmp_folder))
        os.mkdir(self.tmp_folder)

        # output video path
        if output_video is None:
            filename = VideoEditor.DEFAULT_FILENAME
            output_video = os.path.join(self.tmp_folder, filename)
        else:
            output_video = os.path.abspath(output_video)
        self.output_video = output_video

        # mask
        if mask_path is None:
            mask_path = VideoEditor.DEFAULT_MASK_PATH
        print("-Reading mask ...")
        if not os.path.isfile(mask_path):
            raise Exception('Error : mask not found')
        self.mask = cv2.imread(mask_path)
        self.original_witch_h, self.original_witch_w,_ = self.mask.shape
        self.mask_gray = cv2.cvtColor(self.mask, cv2.COLOR_BGR2GRAY)
        _, self.original_mask = cv2.threshold(self.mask_gray, 10, 255, cv2.THRESH_BINARY_INV)
        self.original_mask_inv = cv2.bitwise_not(self.original_mask)

        # default blure area
        self.blure_x_max = self.blure_y_max = 0
        self.blure_x_min = self.blure_y_min = math.inf

        self.hide_full_frame = True
        self.last_faces = None

    def __del__(self):

        print("\n\n")
        # deleting temp folder if empty
        if not any(os.scandir(self.tmp_folder)):
            print('no files found !!')
            print('-deleting {}'.format(self.tmp_folder))
            os.rmdir(self.tmp_folder)

    def hide_face(self):

        # change current directory to temp folder
        os.chdir(self.tmp_folder)

        print("-Reading video ...")
        clip = VideoFileClip(self.input_video)

        print("-Edit video frames ...")
        new_frames = []
        count = 0
        count_process = 0
        clip_frames_count = clip.fps * clip.duration + 1
        proccess_at_fps = 5
        tmp = -1
        start_time = time.monotonic()
        for frame in clip.iter_frames():
            count += 1; tmp += 1

            if tmp == proccess_at_fps:
                tmp = 0

            process = False
            if tmp == 0 or self.last_faces is None:
                count_process += 1; process = True

            frame = self.hide_face_frame(frame, process)
            new_frames.append(frame)

            print("time: {} s - frames percentage: {} %    ".format(\
                round(time.monotonic() - start_time), \
                round(count / clip_frames_count * 100, 2)), end="\r")

        print("processed frames : ( {} / {} )             ".format(count_process,count))
        print("time taken : {} s ".format(round(time.monotonic() - start_time)))
        print("-Generate text ...")
        txt = TextClip("Hey you ! \n\n Join Fadfada \n Today", color='FloralWhite',
        size = clip.size, bg_color='DodgerBlue4',
        font = "Century-Schoolbook-Italic", fontsize= clip.w / 12)

        print("-concatinate ImageSequence ...")
        new_clip = ImageSequenceClip(new_frames, fps=clip.fps)

        # voice
        print("-voice processing ...")
        tasks_count = 4; current_task_id = 1
        print("Loading ( {} / {} )".format(current_task_id, tasks_count), end="\r"); current_task_id += 1
        fs = clip.audio.fps
        audio_arr = clip.audio.copy().to_soundarray()
        # to mono
        print("Loading ( {} / {} )".format(current_task_id, tasks_count), end="\r"); current_task_id += 1
        new_audio_arr = []
        for e in audio_arr:
            new_audio_arr.append((e[0] / 2) + (e[1] / 2))
        audio_arr = np.array(new_audio_arr)
        # effect
        print("Loading ( {} / {} )".format(current_task_id, tasks_count), end="\r"); current_task_id += 1
        y_shifted = librosa.effects.pitch_shift(audio_arr, fs, n_steps=random.randrange(-6, -3))
        sound = AudioProcessing(y_shifted, fs)
        # sound.set_bandpass(300, 3000)
        audio_arr = sound.get_data()
        # to dual
        print("Loading ( {} / {} )".format(current_task_id, tasks_count), end="\r"); current_task_id += 1
        new_audio_arr = []
        for e in audio_arr:
            new_audio_arr.append([e, e])
        audio_arr = np.array(new_audio_arr)
        audio = AudioArrayClip(audio_arr, fps=fs)

        print("-concatinate Video and Audio...")
        new_clip = concatenate_videoclips([new_clip, txt.set_duration(3)]).set_audio(audio)
        new_clip.write_videofile(self.output_video)
        # new_clip.write_videofile(self.output_video, bitrate="3000k")

        # reset current directory
        os.chdir(VideoEditor.CURRENT_DIR)

        return self.output_video

    def hide_face_frame(self, img, detect_faces = False):

        img = img.copy()

        # convert to gray
        img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

        # find faces in image using classifier
        faces = []
        if detect_faces:
            faces = face_cascade.detectMultiScale(img_gray, 1.3, 5)

        # if there no faces in current frame blur it
        if len(faces) == 0 and self.hide_full_frame:
            img = cv2.blur(img, VideoEditor.BLURE_FULL_IMG)
        else:
            self.hide_full_frame = False

        if self.blure_y_min != math.inf:
            img[self.blure_y_min:self.blure_y_max, self.blure_x_min:self.blure_x_max] =\
                cv2.blur(img[self.blure_y_min:self.blure_y_max, self.blure_x_min:self.blure_x_max],\
                    VideoEditor.BLURE_OLD_FACES)

        # put mask on each face
        for face in faces:
            img = self.put_mask_on_face(img, face)

        # last mask saved
        if len(faces) == 0 and self.last_faces is not None:
            for face in self.last_faces:
                img = self.put_mask_on_face(img, face)
        if len(faces) != 0:
            self.last_faces = faces

        return img

    def put_mask_on_face(self, img, face):

        #get shape of img
        img_h,img_w,_ = img.shape
        X, Y, W, H = face

        #retangle for testing purposes
        #img = cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)

        #coordinates of face region
        face_w = W
        face_h = H
        face_x1 = X
        face_x2 = face_x1 + face_w
        face_y1 = Y
        face_y2 = face_y1 + face_h

        #witch size in relation to face by scaling
        witch_width = int(1.5 * face_w)
        witch_height = int(witch_width * self.original_witch_h / self.original_witch_w)

        #setting location of coordinates of witch
        # witch_x1 = face_x2 - int(face_w/2) - int(witch_width/2)
        # witch_x2 = witch_x1 + witch_width
        # witch_y1 = face_y1 - int(face_h*1.25)
        # witch_y2 = witch_y1 + witch_height

        witch_x1 = face_x1 - int(face_w*0.11)
        witch_x2 = face_x2 + int(face_w*0.11)
        witch_y1 = face_y1 - int(face_h*0.3) #int(witch_height/6)
        witch_y2 = face_y2 + int(face_h*0.3) #face_y2 + int(witch_height/6)

        #check to see if out of frame

        if witch_x1 < 0:
            witch_x1 = 0
        if witch_y1 < 0:
            witch_y1 = 0
        if witch_x2 > img_w:
            witch_x2 = img_w
        if witch_y2 > img_h:
            witch_y2 = img_h

        mask = self.mask.copy()

        #Account for any out of frame changes
        witch_width = witch_x2 - witch_x1
        witch_height = witch_y2 - witch_y1

        #resize witch to fit on face
        mask = cv2.resize(mask, (witch_width,witch_height), interpolation = cv2.INTER_AREA)
        witch2 = cv2.resize(self.original_mask, (witch_width,witch_height), interpolation = cv2.INTER_AREA)
        mask_inv = cv2.resize(self.original_mask_inv, (witch_width,witch_height), interpolation = cv2.INTER_AREA)

        #take ROI for witch from background that is equal to size of witch image
        roi = img[witch_y1:witch_y2, witch_x1:witch_x2]

        #original image in background (bg) where witch is not present
        roi_bg = cv2.bitwise_and(roi,roi,mask = witch2)
        roi_fg = cv2.bitwise_and(mask,mask,mask=mask_inv)
        dst = cv2.add(roi_bg,roi_fg)

        self.blure_x_min = min(self.blure_x_min, witch_x1)
        self.blure_x_max = max(self.blure_x_max, witch_x2)
        self.blure_y_min = min(self.blure_y_min, witch_y1)
        self.blure_y_max = max(self.blure_y_max, witch_y2)
        #print("{} , {} , {} , {}".format(self.blure_x_min,self.blure_x_max,self.blure_y_min,self.blure_y_max))

        #put back in original image
        img[witch_y1:witch_y2, witch_x1:witch_x2] = dst

        return img

# test section
# all_start_time = time.monotonic()
# x=1
# if x==1:
#     test_video = VideoEditor("./videos/2.mp4","./masks/6.png","./temp_files/2-6.mp4")
#     test_video.hide_face()

#     test_video = VideoEditor("./videos/1.mp4","./masks/6.png","./temp_files/1-6.mp4")
#     test_video.hide_face()
# else:
#     test_video = VideoEditor("./videos/2.mp4","./masks/5.png","./temp_files/last.mp4")
#     img_ = cv2.imread('./imgs/1.jpg')
#     img_ = test_video.hide_face_frame(img_)

#     cv2.imshow('img',img_)
#     cv2.waitKey(0)
#     cv2.destroyAllWindows()
# print("all time taken : {} s".format(round(time.monotonic() - all_start_time)))


from pygame import mixer
from Voice import speakmodule
from actions.check_cmd import CheckCommand
import speech_recognition as sr
#from Ears.ears import Ears

class Brain():

	# def __init_(self):
	# 	self.print_welcome()

	def print_welcome(self):

		welcome_message =  """
		++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
		+++++++++++++++++++++  Welcome to V.P.D.A  +++++++++++++++++++++
		++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
		+                                                              +
		+           I am J.A.R.V.I.S, How can I help You!              +
		+                                                              +
		++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
			"""
		print (welcome_message)

	def text_mode(self):
		self.print_welcome()
		msg="Ready For your Command"
		#speakmodule.speak([msg],len(msg[0:]),mixer)
		CC = CheckCommand()
		while 1:
			cmd = input("> ")
			print(cmd)
			task = CC.check(cmd)
			if task == True:
				print("What else i can do for you")
			if task == False:
				print("Task is Not Complete, Please Can U type it Again!")

	def voice_mode(self):
		self.print_welcome()
		#ears = Ears()
		CC = CheckCommand()

		while 1:
			#cmd = ears.listen()
			r = sr.Recognizer()
			with sr.Microphone() as source:
				audio = r.adjust_for_ambient_noise(source)
				print("Say something!")
				audio = r.listen(source)
				print(audio)

			try:
				s = (r.recognize_google(audio))
				message = (s.lower())
				print (message)
				task=CC.check(message)
				if task == True:
					print("What else i can do for you")
				if task == False:
					print("Task is Not Complete, Please Can U Say it Again!")

			except sr.UnknownValueError:
				print("$could not understand audio")
			except sr.RequestError as e:
				print("Could not request results$; {0}".format(e))

	def remote_mode(self):
		pass
		self.print_welcome()


import os
import slack
import certifi
import ssl
from flask import Flask, request
from flask_cors import CORS, cross_origin
from flask_restful import Api, Resource

app = Flask(__name__)
api = Api(app)

CORS(app, support_credentials=True)

ssl_context = ssl.create_default_context(cafile=certifi.where())

SLACK_API_TOKEN = os.environ['SLACK_BOT_TOKEN']

client = slack.WebClient(token=SLACK_API_TOKEN, ssl=ssl_context)

def send_message_slack(fullname_arg: str, emailaddress_arg: str, cloud_pak_arg: str, gaia_arg: str, op_arg: str ):
    data = {
            'Full Name':fullname_arg,
            'Email Address':emailaddress_arg,
            'Cloud Pak':cloud_pak_arg,
            'GAIA':gaia_arg,
            'Opportunity Description':op_arg
        }
    client.chat_postMessage(channel="website-app",text=data)

class SendMessage(Resource):
    def post(self):
        data = request.json
        fullname: str = data['fullname']
        emailaddress: str = data['emailaddress']
        cloud_pak: str = data['cloudpakvalue']
        gaia: set = data['gaia']
        opportunity_description: str = data['requestdescription']
        try:
            send_message_slack(fullname, emailaddress, cloud_pak, gaia, opportunity_description)
            return True
        except Exception as identifier:
            return identifier

api.add_resource(SendMessage,'/api/v1/sendmessage/')


from selenium.webdriver import Chrome
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from urllib.parse import urlparse
from urllib.parse import parse_qs
import os, sys

login_url = 'https://blackboard.cpp.edu/webapps/bb-auth-provider-cas-bb_bb60/execute/casLogin?cmd=login&authProviderId=_164_1&redirectUrl=https%3A%2F%2Fblackboard.cpp.edu%2Fwebapps%2Fportal%2Fframeset.jsp'

url_templates = {
    'course' : 'https://blackboard.cpp.edu/webapps/blackboard/execute/launcher?type=Course&id={id}&url=',
    'grade_center' : 'https://blackboard.cpp.edu/webapps/gradebook/do/instructor/enterGradeCenter?course_id={id}&cvid=fullGC',
    'download_grades' : 'https://blackboard.cpp.edu/webapps/gradebook/do/instructor/downloadGradebook?dispatch=viewDownloadOptions&course_id={id}'}.items()

class Course:
    def __init__(self, description, url):
        parsed_description = description.split()
        parsed_crn = parsed_description[1].split('.')
        parsed_url = urlparse(url)
        query = parsed_url[4]
        parsed_query = parse_qs(query)

        self.department = parsed_description[0]
        self.crn = parsed_crn[0]
        self.section = parsed_crn[1]
        self.name = self.department+" "+self.crn+"."+self.section
        self.semester = parsed_description[2]
        self.id = parsed_query['id'][0]
        self.urls = {k: v.format(id=self.id) for k, v in url_templates}

class WebDriver:
    def __init__(self, headless=True):
        options = Options()
        options.headless = headless
        self.driver = Chrome(chrome_options=options)
        self.in_course = False
        self.driver.implicitly_wait(5)
        self.logged_in = False
        self.courses = {}

    def login(self, username, password):
        self.driver.delete_all_cookies()
        self.driver.get(login_url)
        username_element = self.driver.find_element_by_id('username')
        username_element.send_keys(username)
        passwd_element = self.driver.find_element_by_id('password')
        passwd_element.send_keys(password)
        print('Logging in ... ')
        passwd_element.send_keys(Keys.RETURN)
        self.logged_in = True
        print('Succesfully logged in.')
        raw_courses = self.driver.find_element_by_class_name('coursefakeclass')
        raw_courses_list = raw_courses.find_elements_by_xpath('li')
        for raw_course in raw_courses_list:
            raw_link = raw_course.find_element_by_xpath('a')
            description = raw_link.text
            url = raw_link.get_attribute('href')
            course = Course(description, url)
            self.courses[course.name] = course
        print('Succesfully loaded courses.')

    def enter_course(self, course_name):
        course = self.courses[course_name]
        self.driver.get(course.urls['course'])
        self.in_course = True
        name = course.department+' '+course.crn+'.'+course.section
        print('Entered course: ' + name +'.')

    def create_announcement(self, subject, announcement):
        if self.in_course:
            self.driver.find_element_by_xpath('//*[@id="nav"]/li/a').click()
            subject_field = self.driver.find_element_by_xpath('//*[@id="subject"]')
            subject_field.send_keys(subject)
            iframe = self.driver.find_element_by_xpath('//*[@id="messagetext_ifr"]')
            self.driver.switch_to.frame(iframe)
            html_input = self.driver.find_element_by_xpath('html/body')
            html_input.send_keys(announcement)
            self.driver.switch_to.default_content()
            button = self.driver.find_element_by_class_name('submit')
            button.click()
            print('Succesfully created and posted announcement.')

        else:
            print('You are not in any course. Can`t make the announcement.')

    def quit(self):
        self.driver.quit()


import numpy as np
import matplotlib.pyplot as plt
from sklearn.utils import shuffle
from sklearn.model_selection import GridSearchCV
import os
import sys
import pandas as pd

# Keras
import keras
from keras.models import Sequential
from keras.layers import Dense, Conv2D, MaxPooling2D, Dropout, Flatten
from keras.wrappers.scikit_learn import KerasClassifier

import scipy.io as sio
import matplotlib.patches as patches
from PIL import Image, ImageDraw
import numpy as np
import cv2
import torchvision
from torchvision.models.detection.faster_rcnn import FastRCNNPredictor
import torch
import matplotlib.pyplot as plt
import matplotlib

class YoloModel:
    def __init__(self, weights, config, coco):
        self.weights = weights
        self.config = config
        self.coco = coco
        self.output_layers = []
        self.net = self.load_model()
        self.classes = open(self.coco).read().split("\n")

    def load_model(self):
        net = cv2.dnn.readNetFromDarknet(self.config, self.weights)
        net.setPreferableBackend(cv2.dnn.DNN_BACKEND_OPENCV)
        layer_names = net.getLayerNames()
        self.output_layers = [layer_names[i[0] - 1] for i in net.getUnconnectedOutLayers()]
        return net

    def transform_bounding_box(self,box, shape=(416,416)):

        box = box * np.array([shape[1], shape[0], shape[0], shape[1]]) #width, height, width, height => scale the image
        box = [int(box[0] - (box[2] / 2)), int(box[1] - (box[3] / 2)), int(box[2]), int(box[3])] #xmin, ymin, width, height
        return box

    def get_label_score(self,scores, label="person"):
        label_index = self.classes.index(label)
        return scores[label_index]

    def plot_boxes(self, img, boxes):
        fig,ax = plt.subplots(1)
        ax.imshow(img)#.permute(1, 2, 0))
        for box in boxes:
            xmin = box[0]
            ymin = box[1]
            w = box[2]
            h = box[3]
            color = (1,0,1,0.99)
            rect = matplotlib.patches.Rectangle((xmin,ymin),xmin+w,ymin+h,edgecolor=color,facecolor='none')
            ax.add_patch(rect)
        plt.show()

    def predict(self, img, label="person", threshold=0.70, nms_threshold=0.4,plot=True):
        img = cv2.resize(img, (416,416))
        blob = cv2.dnn.blobFromImage(img, 1/255.0, (416, 416), swapRB=True, crop=True)
        self.net.setInput(blob)

        feed_forward_output = self.net.forward(self.output_layers)
        classes = open(self.coco).read().split("\n")
        person_index = self.classes.index("person")

        boxes = []
        scores = []

        for output in feed_forward_output:
            for detection in output: #shape of detection is (85,1) => index 0-4 for bounding box, index 5-85 for scores for each class in coco.names
                label_score = self.get_label_score(detection[5:],label)
                box = detection[0:4] #center X, center Y, width, height

                box = self.transform_bounding_box(box, (416,416))
                if(label_score > threshold):
                    boxes.append([box[0],box[1],box[2],box[3]]) #read somewhere that NMSBoxes only takes lists, not np arrays
                    scores.append(float(label_score))

        non_overlap_indices = cv2.dnn.NMSBoxes(boxes, scores, threshold, nms_threshold)
        if(type(non_overlap_indices) is not tuple):
            top_boxes = [boxes[int(i)] for i in non_overlap_indices.flatten()]
            if(plot):
                self.plot_boxes(img,top_boxes)

            return {"boxes": top_boxes, "label_count":len(top_boxes) }
        else:
            return {"boxes": [], "label_count":0}

# yolo = YoloModel("yolov3.weights", "yolov3.cfg", "coco.names")
# test = cv2.imread("../crowd_images/mask1.jpg")#, cv2.COLOR_BGR2RGB)
# print(yolo.predict(test)["label_count"])

COVID_Face Mask Detection in Crowd_PetersonWilliam_Garimella_Taylor/models/yolo_model.py


import json
import logging
import pytest
import urllib.parse

from server import app

LOG = logging.getLogger(__name__)

@pytest.fixture
def app_client():
    app_client = app.test_client()
    app_client.testing = True
    yield(app_client)

def test_root_endpoint(app_client):
    response = app_client.get('/')
    assert response.status_code == 200
    response.close()

def test_health_endpoint(app_client):
    result = json.loads(app_client.get('/v1/health').data)
    LOG.debug('/v1/health : ' + str(result))
    assert result['status'] == 'UP'

def test_calc_endpoint(app_client):
    url = '/v1/calculation?expr=' + urllib.parse.quote_plus('1 + 1')
    result = json.loads(app_client.get(url).data)
    LOG.debug(str(url) + ' : ' + str(result))
    assert result['result'] == 2


#!/usr/bin/env python3
import wikipedia
import geopandas as gpd
import pandas as pd
import numpy as np
import argparse
from pathlib import Path

# Set up command line arguments
parser = argparse.ArgumentParser(
    description='A script to gather popup and geometry information from Wikipedia for an input .csv list of assets')
parser.add_argument("input_file", default=None, type=str,
                    help="Input file with full path")
parser.add_argument("place_name", default=None, type=str,
                    help="The name of the city in which the assets are situated")

args = parser.parse_args()
input_file = args.input_file
place_name = args.place_name

# Define function to obtain coordinates for each asset. Handle exceptions for
# articles which do not have coordinate information should they arise.
# (Thanks to @AdamP for this one)
def getCoords(x):
    coords = ""
    try:
        coords = [wikipedia.WikipediaPage(title=x).coordinates]
    except:
        coords = np.nan
    return coords

# place_name = 'Bath'
# First read the csv of assets to a dataframe
df = pd.read_csv(input_file,
                 header=None, names=['assets'])

# Ensure data is type = string
df.assets = df.assets.astype(str)

# Pre-process the asset names for search. Append the place name to any asset
# that doesn't already have it to aid search.

df.loc[df['assets'].str.contains(place_name), 'append_place_name'] = df['assets']

df.loc[~df['assets'].str.contains(
    place_name), 'append_place_name'] = df['assets'] + ', ' + place_name

# Run a fuzzy search to return the actual Wikipedia article titles that should be searched against
df['suggested_article_title'] = [wikipedia.search(
    x, results=1, suggestion=False) for x in df['append_place_name']]

# Remove sqare brackets from results
df['suggested_article_title'] = df['suggested_article_title'].str.get(0)

# Re-construct the overall article URL and a hyperlink to be inserted at the end of the summary:
df['article_url'] = "https://en.wikipedia.org/wiki/" + \
    df['suggested_article_title'].astype(str).replace(' ', '_', regex=True)
df['article_hyperlink'] = "Read More"

# Obtain the summary paragraph for each article
df['summary'] = [wikipedia.summary(x) for x in df['suggested_article_title']]

# Append a read more link with the article URL to the end of the summary:
df['summary'] = df['summary'] + "\n\n" + df['article_hyperlink']

# Obtain the main image URL
df['image_url'] = [wikipedia.WikipediaPage(
    title=x).images[1] for x in df['suggested_article_title']]

# Obtain the coordinates of the asset
df['coordinates'] = [getCoords(x) for x in df['suggested_article_title']]

df['lat'] = df['coordinates'][0][0][0]
df['lon'] = df['coordinates'][0][0][1]

df['lat'] = df['lat'].astype(float)
df['lon'] = df['lon'].astype(float)

# Convert the dataframe to a geodataframe
gdf = gpd.GeoDataFrame(df[['assets', 'suggested_article_title', 'summary',
                           'image_url', 'lat', 'lon']], geometry=gpd.points_from_xy(df.lon, df.lat))

export_to_csv = gdf.to_csv(r'./data/export.csv')


import os
import cv2
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.preprocessing import MinMaxScaler
from tqdm.notebook import tqdm

try:
    from pybalu.feature_transformation import pca
    from pybalu.feature_selection import sfs
except:
    print('Can\'t import pybalu, some functions wont work')

N_PEOPLE_A = 16
N_PEOPLE_B = 40
N_PEOPLE_C = 100
N_PEOPLE_D = 166

BASE_FOLDER = 'FaceMask166'
PINERA_IMAGE = 'FM000020_03'

def load_image(image_name, gray=False, crop=False, flip=False, base_folder=BASE_FOLDER):
    """Carga una imagen con open-cv."""
    if not image_name.endswith('.jpg'):
        image_name = f'{image_name}.jpg'

    image_path = os.path.join(base_folder, image_name)
    image = cv2.imread(image_path, cv2.IMREAD_UNCHANGED)

    if image is None:
        raise Exception(f'Cant load image: {image_name}')

    # open-cv loads as BGR
    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

    if gray:
        image = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)

    if crop:
        image = image[:128, :]

    if flip:
        image = cv2.flip(image, 1) # Flip around y-axis

    return image

def load_dataset(n_images, gray=True, crop=True, aug_flip=False):
    """Carga un dataset de las primeras N imagenes.

    Returns:
        dataset lista de: [image_cropped, image_name, i_image (label), j (image_number)]
    """
    dataset = []
    image_base_name = 'F'
    for i_image in range(1, n_images+1):
        base_name = f'FM{i_image:06d}'

        for j in range(1, 6+1):
            # Load image
            image_name = f'{base_name}_{j:02d}'
            image = load_image(image_name, gray=gray, crop=crop)

            dataset.append((image, image_name, i_image, j))

            is_training = j <= 3
            if is_training and aug_flip:
                image_flipped = load_image(image_name, gray=gray, crop=crop, flip=True)
                dataset.append((image_flipped, f'{image_name}_flipped', i_image, j))

    return dataset

def create_df(dataset, merge=None):
    """Crea un dataframe a partir de un dataset.

    Args:
        dataset -- formato que retorna `load_dataset()`
    """
    data = [sample[2:] for sample in dataset]
    image_names = [sample[1] for sample in dataset]
    df = pd.DataFrame(data, index=image_names, columns=['label', 'image_number'])

    if merge is not None:
        df = df.merge(merge, right_index=True, left_index=True)

    return df

def filter_df_by_dataset(df, dataset_name, include_flipped=True):
    if dataset_name == 'A':
        n_people = N_PEOPLE_A
    elif dataset_name == 'B':
        n_people = N_PEOPLE_B
    elif dataset_name == 'C':
        n_people = N_PEOPLE_C
    elif dataset_name == 'D':
        n_people = N_PEOPLE_D
    else:
        raise Exception(f'Unknown dataset: {dataset_name}')

    image_basenames = [f'FM{i_image:06d}' for i_image in range(1, n_people+1)]

    images_from_dataset = set([
        image_name
        for image_name in list(df.index) if any(
            image_name.startswith(basename)
            for basename in image_basenames
        )
    ])

    if not include_flipped:
        images_from_dataset = [image_name for image_name in images_from_dataset if 'flipped' not in image_name]

    return df.loc[df.index.isin(images_from_dataset)]

def calculate_features(dataset, feature_fn, max_images=None, show=False, **kwargs):
    """Calcula features para imagenes de un dataset.

    Nota: esta funcion podria ser reutilizada en calculate_features_df()
    """
    print(f'Calculating features')
    features = []

    if max_images is None:
        data = dataset
    else:
        data = dataset[:max_images]

    # Cargar imagenes y calcular vectores de features
    for sample in tqdm(data, disable=not show):
        image = sample[0]

        image_features = feature_fn(image, **kwargs)
        features.append(image_features)

    return features

def features_to_df(dataset, features, feature_name):
    """Transforma un array de features a un DataFrame.

    Nota: esta funcion podria ser reutilizada en calculate_feature_df()

    Args:
        dataset -- formato que retorna `load_dataset()`
        features -- np.array de shape n_images, n_features
        feature_name -- string que se usa como nombre base.

    Returns:
        dataframe
    """
    image_names = [sample[1] for sample in dataset]

    n_images, n_feats = features.shape

    columns = [f'{feature_name}_{idx}' for idx in range(n_feats)]

    feature_df = pd.DataFrame(features, index=image_names, columns=columns)

    return feature_df

def calculate_feature_df(dataset, feature_fn, feature_name, max_images=None, show=False,
                         fix_len_mode='pad',
                         **kwargs):
    """Calcula un feature con `feature_fn()` para todas las imagenes de un dataset, retorna un DF.

    Args:
        dataset -- formato que retorna `load_dataset()`
        feature_fn -- funcion de firma `features = feature_fn(image, **kwargs)`
        feature_name -- string para usar como nombre base del feature
        **kwargs -- entregados a `feature_fn()`
    """
    print(f'Calculating {feature_name}')
    image_names = []
    features = []

    if max_images is None:
        data = dataset
    else:
        data = dataset[:max_images]

    # Cargar imagenes y calcular vectores de features
    for sample in tqdm(data, disable=not show):
        image = sample[0]
        image_names.append(sample[1])

        image_features = feature_fn(image, **kwargs)
        features.append(image_features)

    # Pass to numpy
    features = np.array(features)

    # DEPRECATED: see new functions for SIFT and SURF features
    #     if len(features.shape) == 1 and len(features[0].shape) == 1:
    #         # HACK: assume that in this case the image_features have different lengths, so pad to the greatest
    #         if fix_len_mode == 'pad':
    #             max_len = max(len(f) for f in features)
    #             features = np.array([
    #                 np.pad(f, pad_width=(0, max_len-len(f)), mode='constant', constant_values=0) for f in features
    #             ])
    #         elif fix_len_mode == 'crop':
    #             min_len = min(len(f) for f in features)
    #             features = np.array([f[:min_len] for f in features])

    if len(features.shape) != 2:
        print('Cannot parse to DF')
        # HACK: for cases where image_features have different lengths
        return features

    n_images, n_feats = features.shape

    # Nombres de los features, e.g. 'hog_0', 'hog_1', etc
    columns = [f'{feature_name}_{idx}' for idx in range(n_feats)]

    # Crear DataFrame, el index son nombres de las imagenes
    feature_df = pd.DataFrame(features, index=image_names, columns=columns)

    # Retornar DataFrame con features calculados
    return feature_df

def calculate_feature_multiple_df(dataset, feature_fn, configs, basename='', **kwargs):
    """Calculate multiple configurations of a feature."""
    df = None

    for name, params in configs.items():
        feature_name = f'{basename}-{name}' if basename else name

        feature_df = calculate_feature_df(dataset, feature_fn, feature_name, **kwargs, **params)

        if df is None:
            df = feature_df
        else:
            df = df.merge(feature_df, right_index=True, left_index=True)

    return df

def plot_cm(cm, title='', labels=None, colorbar=False, setsize=True):
    """Plotear una matriz de confusion.

    Args:
        cm -- matriz de n_classes x n_classes
        title -- string para el titulo
        labels -- lista de nombres de n_classes
        colorbar -- si incluir la barra de color o no
    """
    n_labels = len(cm)
    ticks = np.arange(n_labels)

    if labels is None:
        labels = ticks + 1

    if setsize:
        n_classes = len(labels)
        plt.figure(figsize=(0.375 * n_classes, 0.25 * n_classes))

    plt.imshow(cm, interpolation='nearest', cmap=plt.cm.Blues)
    if colorbar:
        plt.colorbar()

    plt.xticks(ticks, labels)
    plt.yticks(ticks, labels)
    plt.title(title)
    plt.xlabel('Predicción')
    plt.ylabel('Real')

    thresh = cm.max() / 2
    for row in range(n_labels):
        for col in range(n_labels):
            value = cm[row, col]
            color = 'white' if value > thresh else 'black'

            value_str = f'{int(value):d}'

            plt.text(col, row, value_str, ha='center', va='center', color=color)
    plt.show()

def normalize_df(df, keep_nonorm=True, ignore=['label', 'image_number']):
    """Normaliza los datos de un DataFrame.

    Args:
        df -- DataFrame con shape (n_imagenes, n_features)
        keep_nonorm -- Si es True, devuelve el dataframe incluyendo las columnas
            normalizadas y no normalizadas
        ignore -- set de columnas que NO son features
    Returns:
        DataFrame con columnas de features normalizadas
    """
    # Filter to normalize only with train data
    df_train = df.loc[df['image_number'] < 4]

    # Use only features
    feature_columns = [col for col in df.columns if col not in ignore and '_nonorm' not in col]

    # Fit normalization with training
    normalization = MinMaxScaler(feature_range=(0, 1))
    normalization.fit(df_train[feature_columns])

    # Normalize whole dataset
    data_norm = normalization.transform(df[feature_columns])
    df_norm = pd.DataFrame(data_norm, index=df.index, columns=feature_columns)

    # Keep both columns (normalized and not normalized)
    if keep_nonorm:
        # Rename no-normalized to '<col>_nonorm'
        rename = {col: f'{col}_nonorm' for col in feature_columns}
        df_nonorm = df.rename(columns=rename, inplace=False)

        # Merge norm and nonorm
        return df_nonorm.merge(df_norm, how='inner', right_index=True, left_index=True)

    # Add the ignored columns from the original dataset
    return df_norm.merge(df[ignore], how='inner', right_index=True, left_index=True)

def get_cols_startwith(df, name, suffix=None, color=False, norm=True):
    """Dado un DF retorna las columnas que empiezan con cierto nombre."""
    base_name = name
    if color:
        base_name += '-color'
    if suffix:
        base_name += f'-{suffix}'

    if norm:
        comply_norm = lambda col: not col.endswith('_nonorm')
    else:
        comply_norm = lambda col: col.endswith('_nonorm')

    return [col for col in df.columns if col.startswith(base_name) and compy_norm(col)]

def PCA(x_train, x_test, x_val, n_components):
    """
    Realiza la transformación PCA de los datos a tan solo 'n_components' características.
    n_components:   número de características.
    """
    x_train, _, A, Xm, _ = pca(x_train, n_components=n_components)
    x_test = np.matmul(x_test - Xm, A)
    x_val = np.matmul(x_val - Xm, A)
    return x_train, x_test, x_val

def SFS(x_train, y_train, x_test, x_val, n_features, method="fisher", show=False):
    """
    Realiza la selección de 'n_features' características secuencialmente.
    n_features:     número de características
    method:         método a ocupar en la selección
    show:           mostrar el progreso de la selección.
    """
    s_sfs = sfs(x_train, y_train, n_features=n_features, method="fisher", show=show)
    x_train = x_train[:, s_sfs]
    x_test = x_test[:, s_sfs]
    x_val = x_val[:, s_sfs]
    return x_train, x_test, x_val

##### SIFT ######
# Pickle sift stuff

import pickle

def pickle_keypoint(point):
    return (point.pt, point.size, point.angle, point.response, point.octave, point.class_id)

def unpickle_keypoint(t):
    (x, y), size, angle, response, octave, class_id = t
    return cv2.KeyPoint(x=x, y=y, _size=size, _angle=angle, _response=response, _octave=octave,
                        _class_id=class_id)

def pickle_sift(l, filepath):
    obj = [([pickle_keypoint(p) for p in keypoints], d) for keypoints, d in l]
    with open(filepath, 'wb') as f:
        pickle.dump(obj, f)

def unpickle_sift(filepath):
    with open(filepath, 'rb') as f:
        obj = pickle.load(f)

    l = [([unpickle_keypoint(p) for p in keypoints], d) for keypoints, d in obj]
    return l

def save_list_txt(l, filepath):
    with open(filepath, 'w') as f:
        for item in l:
            f.write(f'{item}\n')
    print(f'Saved to {filepath}')

def load_list_txt(filepath):
    with open(filepath, 'r') as f:
        return [l.strip() for l in f.readlines()]


from itertools import permutations
from math import log10, floor

subn = lambda l,d: int(''.join(map(lambda i: l[i], range(d, d+3))))

if __name__ == '__main__':
	rules, res = [2,3,5,7,11,13,17], 0

	for perm in permutations('1406375289'):
		if not any([subn(perm, d+1) % rule for d,rule in enumerate(rules)]):
			res += int(''.join(perm))

	'''
	#this one is more efficient because we
	#suddenly stop if a rule is not followed
	for perm in permutations('1406375289'):
		for d,rule in enumerate(rules):
			if subn(perm, d+1) % rule: break
			d+=1

		if d==7:
			res += int(''.join(perm))
	'''

	print('Result:', res)


import bpy
import gpu
import math
import time
import mathutils
from bgl import *
from mathutils import Vector
from math import cos, sin, pi, radians, degrees
from bpy_extras import view3d_utils
from gpu_extras.batch import batch_for_shader
from bpy.props import IntProperty, FloatProperty

from ..graphics.draw import render_text, render_quad, draw_border_lines
from ..utils.geo import get_blf_text_dims

from ...preferences import get_preferences
from ...addon.utility import method_handler
from ...utils.blender_ui import get_dpi, get_dpi_factor

handle = None

class Data():
    '''Global communications from operator to ui modal.'''
    # Fade Variables
    start_time = 0
    time_difference = 0
    hold_complete = False

    # Transitions
    active_count = 0

    # Display Data
    draw_data = []

    draw_bg = False
    draw_border = False

    # Trigger a global reset on the running modal
    reset_all = False

class Master():
    '''Used from the modal to launch the drawing.'''

    def receive_draw_data(self, draw_data=[]):

        Data.draw_data = draw_data

    def draw(self, draw_bg=False, draw_border=False):

        prefs = get_preferences()

        if prefs.ui.Hops_operator_display:

            Data.draw_bg = draw_bg
            Data.draw_border = draw_border

            # Reuse the same modal check
            if Data.active_count == 0:
                Data.active_count += 1
                Data.reset_all = False
                bpy.ops.hops.ui_draw('INVOKE_DEFAULT')
            else:
                Data.reset_all = True

class HOPS_OT_UI_Draw(bpy.types.Operator):
    '''UI Modal'''

    bl_idname = "hops.ui_draw"
    bl_label = "Drawing for operators"
    bl_options = {"INTERNAL"}

    def __init__(self):

        self.shader = None
        self.timer = None
        self.prefs = get_preferences()
        self.first_draw = False

    def invoke(self, context, event):

        # Globals
        Data.start_time = time.time()
        Data.hold_complete = False
        Data.time_difference = 0

        # Fail Safe
        self.start_time = time.time()
        self.max_time = self.prefs.ui.Hops_operator_display_time + self.prefs.ui.Hops_operator_display_time + 3

        #UI System
        self.shader = Shader(context)
        self.timer = context.window_manager.event_timer_add(0.025, window=context.window)

        context.window_manager.modal_handler_add(self)

        return {'RUNNING_MODAL'}

    def modal(self, context, event):

        # Reuse this modal if a new draw data is invoked
        if Data.reset_all == True:
            Data.reset_all = False
            Data.start_time = time.time()
            Data.hold_complete = False
            Data.time_difference = 0
            self.start_time = time.time()
            self.max_time = self.prefs.ui.Hops_operator_display_time + self.prefs.ui.Hops_operator_display_time + 3
            self.first_draw = False

        # Fail Safe
        if time.time() - self.start_time > self.max_time:
            self.finished(context)
            return {'FINISHED'}

        # Get the time difference
        Data.time_difference = time.time() - Data.start_time

        # Remove modal if another starts
        if Data.active_count > 1:
            self.finished(context)
            return {'FINISHED'}

        # Hold the UI on screen until display time is completed
        if Data.hold_complete == False:

            if Data.time_difference >= self.prefs.ui.Hops_operator_display_time:
                Data.hold_complete = True
                Data.time_difference = 0
                Data.start_time = time.time()

            elif Data.time_difference <= self.prefs.ui.Hops_operator_display_time:

                # Redraw the viewport
                if context.area != None:
                    if self.first_draw == False:
                        self.first_draw = True
                        context.area.tag_redraw()
                return {'PASS_THROUGH'}

        # Fade is finished
        if Data.time_difference > self.prefs.ui.Hops_operator_fade:
            self.finished(context)
            return {'FINISHED'}

        # Redraw the viewport
        if context.area != None:
            context.area.tag_redraw()

        return {'PASS_THROUGH'}

    def finished(self, context):
        '''Remove the timer, shader, and reset Data'''

        Data.time_difference = 0
        Data.active_count -= 1

        if self.timer != None:
            context.window_manager.event_timer_remove(self.timer)

        if self.shader != None:
            self.shader.destroy()
            del self.shader

        if context.area != None:
            context.area.tag_redraw()

class Shader():

    def __init__(self, context):

        self.context = context
        self.banner = Banner(context=self.context)
        self.setup_handle()

    def setup_handle(self):
        '''Setup the draw handle for the UI'''

        global handle
        handle = bpy.types.SpaceView3D.draw_handler_add(self.safe_draw, (self.context, ), "WINDOW", "POST_PIXEL")

    def safe_draw(self, context):
        method_handler(self.draw,
            arguments = (context,),
            identifier = 'UI Framework',
            exit_method = self.destroy)

    def draw(self, context):

        self.banner.draw()

    def destroy(self):

        global handle

        if handle != None:
            bpy.types.SpaceView3D.draw_handler_remove(handle, "WINDOW")
            handle = None
            if self.banner:
                del self.banner
            return True

        else:
            return False

class Banner():
    '''Drawing class for operator UI.'''

    def __init__(self, context):

        self.context = context
        self.prefs = get_preferences()
        self.scale_factor = get_dpi_factor()
        self.setup_colors()

        # Font sizes
        self.header_font_size = 24
        self.body_font_size = 14

        # Font dims
        self.header_font_dims = get_blf_text_dims(text="Sample", size=self.header_font_size)
        self.body_font_dims = get_blf_text_dims(text="Sample", size=self.body_font_size)

        # Paddings
        self.text_padding = 6 * self.scale_factor
        self.column_gap = 150 * self.scale_factor
        self.side_padding = 15 * self.scale_factor
        self.padding_to_bottom_line = 10 * self.scale_factor
        self.line_width = 2

        # Offsets
        self.prefs_offset = (
            self.prefs.ui.Hops_operator_ui_offset[0] * self.scale_factor,
            self.prefs.ui.Hops_operator_ui_offset[1] * self.scale_factor)

        self.height_to_bottom = self.prefs_offset[1]

        # Totals
        self.total_width = self.get_total_width()
        self.total_height =self.get_total_height()
        self.left_offset = (self.context.area.width * .5) - (self.total_width * .5) + self.prefs_offset[0]

    #######################################
    # SETUPS
    #######################################

    def get_total_width(self):
        '''Get the total wdth of the box.'''

        # Get the widest text width
        if Data.draw_data != []:

            # Only containts header
            if len(Data.draw_data) == 1:
                text = Data.draw_data[0][0]
                return get_blf_text_dims(text=text, size=self.header_font_size)[0] + self.side_padding * 2

            # Header plus other data
            else:
                longest = ""
                for i in range(len(Data.draw_data)):

                    # Only one item in list
                    if len(Data.draw_data[i]) == 1:
                        if len(Data.draw_data[i][0]) > len(longest):
                            longest = Data.draw_data[i][0]

                    # Two items in list
                    elif len(Data.draw_data[i]) == 2:
                        compare = str(Data.draw_data[i][0]) + str(Data.draw_data[i][1])
                        if len(compare) > len(longest):
                            longest = compare

                return get_blf_text_dims(text=str(longest), size=self.body_font_size)[0] + self.column_gap

    def get_total_height(self):
        '''Get the total height of the box.'''

        # Single line label only
        if len(Data.draw_data) == 1:
            return self.header_font_dims[1] + self.text_padding * 3

        # Full label
        else:

            # For each text add padding for top
            txt_padding_total = self.text_padding * (len(Data.draw_data) - 2)

            # Both divisor lines
            divisor_total = self.line_width * 2
            divisor_total += self.text_padding * 2
            divisor_total += self.padding_to_bottom_line * 2

            # All the text heights
            text_height_total = self.header_font_dims[1]
            text_height_total += self.body_font_dims[1] * (len(Data.draw_data) - 1)

            return text_height_total + txt_padding_total + divisor_total

    #######################################
    # COLORS
    #######################################

    def setup_colors(self):

        # Original
        self.prefs_colors = []
        self.prefs_colors.append(self.prefs.color.Hops_UI_text_color)
        self.prefs_colors.append(self.prefs.color.Hops_UI_secondary_text_color)
        self.prefs_colors.append(self.prefs.color.Hops_UI_text_highlight_color)
        self.prefs_colors.append(self.prefs.color.Hops_UI_cell_background_color)
        self.prefs_colors.append(self.prefs.color.Hops_UI_border_color)

    def set_colors(self):

        self.Hops_UI_text_color            = Vector((self.prefs_colors[0][0], self.prefs_colors[0][1], self.prefs_colors[0][2], self.prefs_colors[0][3]))
        self.Hops_UI_secondary_text_color  = Vector((self.prefs_colors[1][0], self.prefs_colors[1][1], self.prefs_colors[1][2], self.prefs_colors[1][3]))
        self.Hops_UI_text_highlight_color  = Vector((self.prefs_colors[2][0], self.prefs_colors[2][1], self.prefs_colors[2][2], self.prefs_colors[2][3]))
        self.Hops_UI_cell_background_color = Vector((self.prefs_colors[3][0], self.prefs_colors[3][1], self.prefs_colors[3][2], self.prefs_colors[3][3]))
        self.Hops_UI_border_color          = Vector((self.prefs_colors[4][0], self.prefs_colors[4][1], self.prefs_colors[4][2], self.prefs_colors[4][3]))

    def handle_fade(self):

        if Data.hold_complete == True:
            if self.prefs.ui.Hops_operator_fade > 0:

                original_alpha = self.prefs_colors[0][3]
                alpha = self.Hops_UI_text_color[3] - ((original_alpha / self.prefs.ui.Hops_operator_fade) * Data.time_difference)
                self.Hops_UI_text_color = Vector((self.prefs_colors[0][0], self.prefs_colors[0][1], self.prefs_colors[0][2], alpha))

                original_alpha = self.prefs_colors[1][3]
                alpha = self.Hops_UI_secondary_text_color[3] - ((original_alpha / self.prefs.ui.Hops_operator_fade) * Data.time_difference)
                self.Hops_UI_secondary_text_color = Vector((self.prefs_colors[1][0], self.prefs_colors[1][1], self.prefs_colors[1][2], alpha))

                original_alpha = self.prefs_colors[2][3]
                alpha = self.Hops_UI_text_highlight_color[3] - ((original_alpha / self.prefs.ui.Hops_operator_fade) * Data.time_difference)
                self.Hops_UI_text_highlight_color  = Vector((self.prefs_colors[2][0], self.prefs_colors[2][1], self.prefs_colors[2][2], alpha))

                original_alpha = self.prefs_colors[3][3]
                alpha = self.Hops_UI_cell_background_color[3] - ((original_alpha / self.prefs.ui.Hops_operator_fade) * Data.time_difference)
                self.Hops_UI_cell_background_color = Vector((self.prefs_colors[3][0], self.prefs_colors[3][1], self.prefs_colors[3][2], alpha))

                original_alpha = self.prefs_colors[4][3]
                alpha = self.Hops_UI_border_color[3] - ((original_alpha / self.prefs.ui.Hops_operator_fade) * Data.time_difference)
                self.Hops_UI_border_color = Vector((self.prefs_colors[4][0], self.prefs_colors[4][1], self.prefs_colors[4][2], alpha))

    #######################################
    # DRAW
    #######################################

    def draw(self):

        # Resets
        self.height_to_bottom = self.prefs_offset[1]
        self.total_width = self.get_total_width()
        self.total_height =self.get_total_height()
        self.left_offset = (self.context.area.width * .5) - (self.total_width * .5) + self.prefs_offset[0]

        if Data.draw_data != []:

            # Colors
            self.set_colors()
            self.handle_fade()

            # Draw header and lines
            if len(Data.draw_data) > 1:
                self.draw_background()
                self.draw_divisor_line()
                self.draw_body_text()
                self.draw_divisor_line()
                self.draw_header_text()

            # Draw only header
            else:
                self.draw_background()
                self.draw_header_text()

    def draw_background(self):

        # Setup for background and border : TL, BL, TR, BR
        quad = (
            (self.left_offset                   , self.height_to_bottom + self.total_height),
            (self.left_offset                   , self.height_to_bottom),
            (self.left_offset + self.total_width, self.height_to_bottom + self.total_height),
            (self.left_offset + self.total_width, self.height_to_bottom))

        # Draw background
        if Data.draw_bg:
            render_quad(quad=quad,
                        color=self.Hops_UI_border_color,
                        bevel_corners=True)

        # Draw border
        if Data.draw_border:
            draw_border_lines(vertices=quad, width=self.prefs.ui.Hops_operator_border_size, color=self.Hops_UI_cell_background_color)

    def draw_divisor_line(self):

        left_point  = (self.left_offset + self.side_padding                   , self.height_to_bottom + self.padding_to_bottom_line)
        right_point = (self.left_offset - self.side_padding + self.total_width, self.height_to_bottom + self.padding_to_bottom_line)

        self.height_to_bottom += self.line_width + self.text_padding + self.padding_to_bottom_line

        draw_divider_lines(
            vertices=[left_point, right_point],
            width=self.line_width,
            color=self.Hops_UI_cell_background_color,
            bevel_corners=False,
            format_lines=False)

    def draw_body_text(self):

        # Text items for the body
        for index, item in enumerate(Data.draw_data[1:]):
            # Left or Right
            count = 0
            for string in item:

                if type(string) == float:
                    string = "{0:.3f}".format(string)
                string = str(string)

                # Left Side
                if count == 0:
                    pos = (self.left_offset + self.side_padding, self.height_to_bottom)
                    render_text(
                        text=string,
                        position=pos,
                        size=self.body_font_size,
                        color=self.Hops_UI_text_color)

                # Right Side
                elif count == 1:
                    text_dims = get_blf_text_dims(text=string, size=self.body_font_size)
                    pos = (self.left_offset + self.total_width - text_dims[0] - self.side_padding, self.height_to_bottom)
                    render_text(
                        text=string,
                        position=pos,
                        size=self.body_font_size,
                        color=self.Hops_UI_secondary_text_color)

                count += 1

            self.height_to_bottom += self.body_font_dims[1] + self.text_padding
        self.height_to_bottom -= self.text_padding * 2

    def draw_header_text(self):

        string = Data.draw_data[0][0]
        pos = (self.left_offset + self.side_padding, self.height_to_bottom)

        # Single line label only
        if len(Data.draw_data) == 1:
            pos = (self.left_offset + self.side_padding, self.height_to_bottom + self.text_padding * 2)

        render_text(
            text=string,
            position=pos,
            size=self.header_font_size,
            color=self.Hops_UI_secondary_text_color)

def draw_divider_lines(vertices=[], width=1, color=(0,0,0,1), bevel_corners=True, format_lines=False):
    '''Render quads passed in. \n
        Top Left, Bottom Left, Top Right, Bottom Right
    '''

    shader = gpu.shader.from_builtin('2D_UNIFORM_COLOR')
    glEnable(GL_LINE_SMOOTH)
    glEnable(GL_BLEND)
    glLineWidth(width)
    batch = batch_for_shader(shader, 'LINE_STRIP', {"pos": vertices})
    shader.bind()
    shader.uniform_float("color", color)
    batch.draw(shader)

    glLineWidth(width)
    glDisable(GL_BLEND)

    del shader
    del batch

from bpy.app.handlers import persistent
@persistent
def remove_op_draw_shader(dummy):
    global handle
    if handle != None:
        bpy.types.SpaceView3D.draw_handler_remove(handle, "WINDOW")
        handle = None

    # Reset draw data
    Data.start_time = 0
    Data.time_difference = 0
    Data.hold_complete = False
    Data.active_count = 0
    Data.draw_data = []
    Data.draw_bg = False
    Data.draw_border = False
    Data.reset_all = False


import os
import sys
import platform
import subprocess

import pytest

import virtual_environments
from service_messages import ServiceMessage, assert_service_messages, has_service_messages

def construct_fixture():
    params = []

    if sys.version_info >= (2, 6):
        # latest version
        params.append(("pytest",))

    if (2, 5) <= sys.version_info < (2, 6):
        params.append(("pytest==2.5.0", "py==1.4.19"))

    if (2, 4) <= sys.version_info < (2, 5):
        params.append(("pytest==2.3.3", "py==1.4.12"))

    @pytest.fixture(scope='module', params=params)
    def venv(request):
        return virtual_environments.prepare_virtualenv(request.param)

    return venv

globals()['venv'] = construct_fixture()

def fix_slashes(s):
    if platform.system() == 'Windows':
        return s.replace('/', '\\')
    else:
        return s.replace('\\', '/')

if (sys.version_info[0] == 2 and sys.version_info >= (2, 7)) or (sys.version_info[0] == 3 and sys.version_info >= (3, 4)):
    def test_pytest_pep8(venv):
        venv_with_pep8 = virtual_environments.prepare_virtualenv(venv.packages + ("pytest-pep8",))

        output = run(venv_with_pep8, 'pep8_test.py', options="--pep8")
        pep8_test_name = "tests.guinea-pigs.pytest.pep8_test.PEP8"
        test_name = "tests.guinea-pigs.pytest.pep8_test.test_ok"
        ms = assert_service_messages(
            output,
            [
                ServiceMessage('testStarted', {'name': pep8_test_name}),
                ServiceMessage('testFailed', {'name': pep8_test_name}),
                ServiceMessage('testFinished', {'name': pep8_test_name}),
                ServiceMessage('testStarted', {'name': test_name}),
                ServiceMessage('testFinished', {'name': test_name}),
            ])

        assert ms[1].params["details"].find("E302 expected 2 blank lines, found 1") > 0

    def test_pytest_pylint(venv):
        venv_with_pylint = virtual_environments.prepare_virtualenv(venv.packages + ("pytest-pylint",))

        output = run(venv_with_pylint, 'pylint_test.py', options="--pylint")
        pylint_test_name = "tests.guinea-pigs.pytest.pylint_test.Pylint"
        test_name = "tests.guinea-pigs.pytest.pylint_test.test_ok"
        ms = assert_service_messages(
            output,
            [
                ServiceMessage('testStarted', {'name': pylint_test_name}),
                ServiceMessage('testStdErr', {'name': pylint_test_name}),
                ServiceMessage('testFailed', {'name': pylint_test_name}),
                ServiceMessage('testFinished', {'name': pylint_test_name}),
                ServiceMessage('testStarted', {'name': test_name}),
                ServiceMessage('testFinished', {'name': test_name}),
            ])

        assert ms[1].params["out"].find("No config file found") >= 0
        assert ms[2].params["details"].find("Unused import sys") > 0

def test_hierarchy(venv):
    output = run(venv, 'namespace')
    test_name = 'tests.guinea-pigs.pytest.namespace.pig_test.TestSmoke.test_smoke'
    assert_service_messages(
        output,
        [
            ServiceMessage('testStarted', {'name': test_name, 'flowId': test_name}),
            ServiceMessage('testFinished', {'name': test_name, 'flowId': test_name}),
        ])

def test_force_tc_reporting(venv):
    output = run(venv, 'namespace', options="--teamcity", set_tc_version=False)
    assert has_service_messages(output)

def test_tc_reporting(venv):
    output = run(venv, 'namespace')
    assert has_service_messages(output)

def test_no_reporting_when_no_teamcity(venv):
    output = run(venv, 'namespace', set_tc_version=False)
    assert not has_service_messages(output)

def test_reporting_disabled(venv):
    output = run(venv, 'namespace', set_tc_version=True, options="--no-teamcity")
    assert not has_service_messages(output)

def test_custom_test_items(venv):
    output = run(venv, 'custom')
    assert_service_messages(
        output,
        [
            ServiceMessage('testStarted', {'name': 'tests.guinea-pigs.pytest.custom.test_simple_yml.line1'}),
            ServiceMessage('testFinished', {'name': 'tests.guinea-pigs.pytest.custom.test_simple_yml.line1'}),
            ServiceMessage('testStarted', {'name': 'tests.guinea-pigs.pytest.custom.test_simple_yml.line2'}),
            ServiceMessage('testFinished', {'name': 'tests.guinea-pigs.pytest.custom.test_simple_yml.line2'}),
        ])

if sys.version_info >= (2, 6):
    @pytest.mark.parametrize("coverage_version, pytest_cov_version", [
        ("==3.7.1", "==1.8.1"),
        ("==4.0.1", "==2.2.0"),
        # latest
        ("", ""),
    ])
    def test_coverage(venv, coverage_version, pytest_cov_version):
        if coverage_version != "==3.7.1" and (3, 1) < sys.version_info < (3, 3):
            pytest.skip("coverage >= 4.0 dropped support for Python 3.2")

        venv_with_coverage = virtual_environments.prepare_virtualenv(
            venv.packages + (
                "coverage" + coverage_version,
                "pytest-cov" + pytest_cov_version))

        output = run(venv_with_coverage, 'coverage_test', options="--cov coverage_test")
        test_name = "tests.guinea-pigs.pytest.coverage_test.coverage_test.test_covered_func"
        assert_service_messages(
            output,
            [
                ServiceMessage('testStarted', {'name': test_name}),
                ServiceMessage('testFinished', {'name': test_name}),
                ServiceMessage('buildStatisticValue', {'key': 'CodeCoverageAbsLCovered', 'value': '9'}),
                ServiceMessage('buildStatisticValue', {'key': 'CodeCoverageAbsLTotal', 'value': '13'}),
                ServiceMessage('buildStatisticValue', {'key': 'CodeCoverageAbsLUncovered', 'value': '4'}),
            ])

def test_runtime_error(venv):
    output = run(venv, 'runtime_error_test.py')
    ms = assert_service_messages(
        output,
        [
            ServiceMessage('testStarted', {'name': 'tests.guinea-pigs.pytest.runtime_error_test.test_exception'}),
            ServiceMessage('testFailed', {'flowId': 'tests.guinea-pigs.pytest.runtime_error_test.test_exception'}),
            ServiceMessage('testFinished', {'name': 'tests.guinea-pigs.pytest.runtime_error_test.test_exception'}),
            ServiceMessage('testStarted', {'name': 'tests.guinea-pigs.pytest.runtime_error_test.test_error'}),
            ServiceMessage('testFailed', {}),
            ServiceMessage('testFinished', {'name': 'tests.guinea-pigs.pytest.runtime_error_test.test_error'}),
        ])
    assert ms[1].params["details"].find("raise Exception") > 0
    assert ms[1].params["details"].find("oops") > 0
    assert ms[4].params["details"].find("assert 0 != 0") > 0

def test_unittest_error(venv):
    output = run(venv, 'unittest_error_test.py')
    ms = assert_service_messages(
        output,
        [
            ServiceMessage('testStarted', {'name': 'tests.guinea-pigs.pytest.unittest_error_test.TestErrorFail.test_error'}),
            ServiceMessage('testFailed', {}),
            ServiceMessage('testFinished', {'name': 'tests.guinea-pigs.pytest.unittest_error_test.TestErrorFail.test_error'}),
            ServiceMessage('testStarted', {'name': 'tests.guinea-pigs.pytest.unittest_error_test.TestErrorFail.test_fail'}),
            ServiceMessage('testFailed', {}),
            ServiceMessage('testFinished', {'name': 'tests.guinea-pigs.pytest.unittest_error_test.TestErrorFail.test_fail'}),
        ])
    assert ms[1].params["details"].find("raise Exception") > 0
    assert ms[1].params["details"].find("oops") > 0
    assert ms[4].params["details"].find("AssertionError") > 0

def test_fixture_error(venv):
    output = run(venv, 'fixture_error_test.py')

    test1_name = 'tests.guinea-pigs.pytest.fixture_error_test.test_error1'
    test2_name = 'tests.guinea-pigs.pytest.fixture_error_test.test_error2'

    ms = assert_service_messages(
        output,
        [
            ServiceMessage('testStarted', {'name': test1_name, 'flowId': test1_name}),
            ServiceMessage('testFailed', {'name': test1_name,
                                          'message': 'test setup failed',
                                          'flowId': test1_name}),
            ServiceMessage('testFinished', {'name': test1_name}),

            ServiceMessage('testStarted', {'name': test2_name, 'flowId': test2_name}),
            ServiceMessage('testFailed', {'name': test2_name,
                                          'message': 'test setup failed',
                                          'flowId': test2_name}),
            ServiceMessage('testFinished', {'name': test2_name}),
        ])
    assert ms[1].params["details"].find("raise Exception") > 0
    assert ms[1].params["details"].find("oops") > 0
    assert ms[4].params["details"].find("raise Exception") > 0
    assert ms[4].params["details"].find("oops") > 0

@pytest.mark.skipif("sys.version_info < (2, 6)", reason="requires Python 2.6+")
def test_output(venv):
    output = run(venv, 'output_test.py')

    test_name = 'tests.guinea-pigs.pytest.output_test.test_out'

    assert_service_messages(
        output,
        [
            ServiceMessage('testStarted', {'name': test_name, 'flowId': test_name, 'captureStandardOutput': 'false'}),
            ServiceMessage('blockOpened', {'name': 'test setup', 'flowId': test_name}),
            ServiceMessage('testStdOut', {'name': test_name, 'flowId': test_name, 'out': 'setup stdout|n'}),
            ServiceMessage('testStdErr', {'name': test_name, 'flowId': test_name, 'out': 'setup stderr|n'}),
            ServiceMessage('blockClosed', {'name': 'test setup'}),
            ServiceMessage('testStdOut', {'name': test_name, 'flowId': test_name, 'out': 'test stdout|n'}),
            ServiceMessage('testStdErr', {'name': test_name, 'flowId': test_name, 'out': 'test stderr|n'}),
            ServiceMessage('testFinished', {'name': test_name, 'flowId': test_name}),
            ServiceMessage('blockOpened', {'name': 'test teardown', 'flowId': test_name}),
            ServiceMessage('testStdOut', {'name': test_name, 'flowId': test_name, 'out': 'teardown stdout|n'}),
            ServiceMessage('testStdErr', {'name': test_name, 'flowId': test_name, 'out': 'teardown stderr|n'}),
            ServiceMessage('blockClosed', {'name': 'test teardown'}),
        ])

@pytest.mark.skipif("sys.version_info < (2, 6)", reason="requires Python 2.6+")
def test_chunked_output(venv):
    output = run(venv, 'chunked_output_test.py')

    full_line = 'x' * 50000
    leftovers = 'x' * (1024 * 1024 - 50000 * 20)

    assert_service_messages(
        output,
        [ServiceMessage('testStarted', {})] +
        [ServiceMessage('testStdOut', {'out': full_line})] * 20 +
        [ServiceMessage('testStdOut', {'out': leftovers})] +
        [ServiceMessage('testStdErr', {'out': full_line})] * 20 +
        [ServiceMessage('testStdErr', {'out': leftovers})] +
        [ServiceMessage('testFinished', {})]
    )

def test_output_no_capture(venv):
    output = run(venv, 'output_test.py', options="-s")

    test_name = 'tests.guinea-pigs.pytest.output_test.test_out'

    assert_service_messages(
        output,
        [
            ServiceMessage('testStarted', {'name': test_name, 'flowId': test_name, 'captureStandardOutput': 'true'}),
            ServiceMessage('testFinished', {'name': test_name, 'flowId': test_name}),
        ])
    assert "setup stderr" in output
    assert "setup stdout" in output
    assert "test stderr" in output
    assert "test stdout" in output
    assert "teardown stderr" in output
    assert "teardown stdout" in output

def test_teardown_error(venv):
    output = run(venv, 'teardown_error_test.py')
    teardown_test_id = 'tests.guinea-pigs.pytest.teardown_error_test.test_error_teardown'
    ms = assert_service_messages(
        output,
        [
            ServiceMessage('testStarted', {'name': 'tests.guinea-pigs.pytest.teardown_error_test.test_error'}),
            ServiceMessage('testFinished', {'name': 'tests.guinea-pigs.pytest.teardown_error_test.test_error'}),
            ServiceMessage('testStarted', {'name': teardown_test_id, 'flowId': teardown_test_id}),
            ServiceMessage('testFailed', {'flowId': teardown_test_id,
                                          'message': fix_slashes('tests/guinea-pigs/pytest/teardown_error_test.py') + ':13 (test_error)'}),
            ServiceMessage('testFinished', {'name': teardown_test_id, 'flowId': teardown_test_id}),
        ])
    assert ms[3].params["details"].find("raise Exception") > 0
    assert ms[3].params["details"].find("teardown oops") > 0

def test_module_error(venv):
    output = run(venv, 'module_error_test.py')
    ms = assert_service_messages(
        output,
        [
            ServiceMessage('testStarted', {'name': 'tests.guinea-pigs.pytest.module_error_test.top_level_collect'}),
            ServiceMessage('testFailed', {}),
            ServiceMessage('testFinished', {'name': 'tests.guinea-pigs.pytest.module_error_test.top_level_collect'}),
        ])
    assert ms[1].params["details"].find("raise Exception") > 0
    assert ms[1].params["details"].find("module oops") > 0

def test_skip(venv):
    output = run(venv, 'skip_test.py')
    test_name = 'tests.guinea-pigs.pytest.skip_test.test_function'
    assert_service_messages(
        output,
        [
            ServiceMessage('testStarted', {'name': test_name}),
            ServiceMessage('testIgnored', {'message': 'Skipped: skip reason', 'flowId': test_name}),
            ServiceMessage('testFinished', {'name': test_name}),
        ])

def test_params(venv):
    output = run(venv, 'params_test.py')

    test1_name = 'tests.guinea-pigs.pytest.params_test.test_eval|[3+5-8|]'
    test2_name = "tests.guinea-pigs.pytest.params_test.test_eval|[|'1_5|' + |'2|'-1_52|]"
    test3_name = 'tests.guinea-pigs.pytest.params_test.test_eval|[6*9-42|]'

    assert_service_messages(
        output,
        [
            ServiceMessage('testStarted', {'name': test1_name}),
            ServiceMessage('testFinished', {'name': test1_name}),
            ServiceMessage('testStarted', {'name': test2_name}),
            ServiceMessage('testFinished', {'name': test2_name}),
            ServiceMessage('testStarted', {'name': test3_name}),
            ServiceMessage('testFailed', {'name': test3_name,
                                          'message': fix_slashes('tests/guinea-pigs/pytest/params_test.py') + ':3 (test_eval|[6*9-42|])'}),
            ServiceMessage('testFinished', {'name': test3_name}),
        ])

def test_xfail(venv):
    output = run(venv, 'xfail_test.py')
    ms = assert_service_messages(
        output,
        [
            ServiceMessage('testStarted', {'name': 'tests.guinea-pigs.pytest.xfail_test.test_unexpectedly_passing'}),
            ServiceMessage('testFailed', {}),
            ServiceMessage('testFinished', {'name': 'tests.guinea-pigs.pytest.xfail_test.test_unexpectedly_passing'}),
            ServiceMessage('testStarted', {'name': 'tests.guinea-pigs.pytest.xfail_test.test_expected_to_fail'}),
            ServiceMessage('testIgnored', {}),
            ServiceMessage('testFinished', {'name': 'tests.guinea-pigs.pytest.xfail_test.test_expected_to_fail'}),
        ])
    assert ms[4].params["message"].find("xfail reason") > 0

def run(venv, file_name, test=None, options='', set_tc_version=True):
    env = virtual_environments.get_clean_system_environment()

    if set_tc_version:
        env['TEAMCITY_VERSION'] = "0.0.0"

    if test is not None:
        test_suffix = "::" + test
    else:
        test_suffix = ""

    command = os.path.join(venv.bin, 'py.test') + " " + options + " " + \
        os.path.join('tests', 'guinea-pigs', 'pytest', file_name) + test_suffix
    print("RUN: " + command)
    proc = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, env=env, shell=True)
    output = "".join([x.decode() for x in proc.stdout.readlines()])
    print("OUTPUT: " + output.replace("#", "*"))
    proc.wait()

    return output


import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit as cf

class Mobility:
    DataAddress = ""
    sigma = 0.01
    title = ""
    x_label = "B-Field"
    x_units = "Teslas"
    y_label_nonlinear = "Rxx"
    y_label_linear = "Rxy"
    y_units = "Ohms"
    guess_xx = []
    guess_xy = []
    params_xx = []
    params_xy = []

    def __init__(self, address, guesscoefficientxx, guessoffsetxx, guesscoefficientxy, guessoffsetxy):
        self.DataAddress = address[:-8] + "_Rxx" + address[-8:]
        self.guess_xx = [guesscoefficientxx, guessoffsetxx]
        self.guess_xy = [guesscoefficientxy, guessoffsetxy]
        self.title = address[90:99]

    @staticmethod
    def linearfunc(bfield, coefficient, offset):
        return coefficient*bfield+offset

    @staticmethod
    def parabolicfunc(bfield, coefficient, offset):
        return coefficient*(bfield**2.0)+offset

    def chisquared(self, params, bfield, rxy):
        return np.sum((rxy - self.linearfunc(bfield, *params)) ** 2 / self.sigma ** 2)

    def pltinitial(self, data, xx, linearplot):
        bfieldguess = np.linspace(min(data[1, :]), max(data[1, :]), 500)
        if linearplot:
            rguess = self.linearfunc(bfieldguess, *self.guess_xy)
        else:
            rguess = self.parabolicfunc(bfieldguess, *self.guess_xx)

        plt.scatter(data[1, :], data[0, :], marker='.', label="measured data")
        plt.plot(bfieldguess, rguess, marker="", linestyle="-", linewidth=1, color="g", label="initial guess")
        plt.xlabel('{} [{}]'.format(self.x_label, self.x_units))
        if xx:
            plt.ylabel('{} [{}]'.format(self.y_label_nonlinear, self.y_units))
            plt.title(self.title + " Rxx Initial Guess")
        else:
            plt.ylabel('{} [{}]'.format(self.y_label_linear, self.y_units))
            plt.title(self.title + " Rxy Initial Guess")
        plt.legend(loc='best', numpoints=1)
        print('\nSaving plot 1 for ' + self.title)
        if "OutPlane" in self.DataAddress:
            if xx:
                address = self.DataAddress[:90] + "Graphs/" + self.title + "_InPlane_Rxx_Initial" + ".png"
            else:
                address = self.DataAddress[:90] + "Graphs/" + self.title + "_InPlane_Rxy_Initial" + ".png"
        elif xx:
            address = self.DataAddress[:90] + "Graphs/" + self.title + "_Rxx_Initial" + ".png"
        else:
            address = self.DataAddress[:90] + "Graphs/" + self.title + "_Rxy_Initial" + ".png"
        plt.savefig(address)
        plt.clf()

    def pltfit(self, data, xx, linearplot):
        if linearplot:
            self.params_xy, cov = cf(self.linearfunc, data[1, :], data[0, :],
                                     sigma=self.sigma * np.ones(len(data[1, :])), p0=self.guess_xy, maxfev=10 ** 5)
            chi2 = self.chisquared(self.params_xy, data[1, :], data[0, :])
            dof = len(data[1, :]) - len(self.params_xy)
        else:
            self.params_xx, cov = cf(self.parabolicfunc, data[1, :], data[0, :],
                                     sigma=self.sigma * np.ones(len(data[1, :])), p0=self.guess_xx, maxfev=10 ** 5)
            chi2 = self.chisquared(self.params_xx, data[1, :], data[0, :])
            dof = len(data[1, :]) - len(self.params_xx)

        print("\nGoodness of fit - chi square measure:")
        print("Chi2 = {}, Chi2/dof = {}\n".format(chi2, chi2 / dof))

        cov = cov * dof / chi2
        paramserr = np.sqrt(np.diag(cov))

        print("Fit parameters:")
        if linearplot:
            param_names = ['Slope', 'Offset']
            for i in range(len(self.params_xy)):
                print('{} = {:.3e} +/- {:.3e}'.format(param_names[i], self.params_xy[i], paramserr[i]))
            bfieldfit = np.linspace(min(data[1, :]), max(data[1, :]), len(data[1, :]) * 10)
            rxyfit = self.linearfunc(bfieldfit, *self.params_xy)
        else:
            param_names = ['Coefficient', 'Offset']
            for i in range(len(self.params_xx)):
                print('{} = {:.3e} +/- {:.3e}'.format(param_names[i], self.params_xx[i], paramserr[i]))
            bfieldfit = np.linspace(min(data[1, :]), max(data[1, :]), len(data[1, :]) * 10)
            rxyfit = self.parabolicfunc(bfieldfit, *self.params_xx)

        plt.scatter(data[1, :], data[0, :], marker='.', label="measured data")
        plt.plot(bfieldfit, rxyfit, marker="", linestyle="-", linewidth=2, color="r", label=" fit")
        plt.xlabel('{} [{}]'.format(self.x_label, self.x_units))
        if xx:
            plt.ylabel('{} [{}]'.format(self.y_label_nonlinear, self.y_units))
            plt.title(self.title + " Rxx Fitted Guess")
        else:
            plt.ylabel('{} [{}]'.format(self.y_label_linear, self.y_units))
            plt.title(self.title + " Rxy Fitted Guess")
        plt.legend(loc='best', numpoints=1)
        print('\nSaving plot 2 for ' + self.title)
        if "OutPlane" in self.DataAddress:
            if xx:
                address = self.DataAddress[:90] + "Graphs/" + self.title + "_InPlane_Rxx_Fitted" + ".png"
            else:
                address = self.DataAddress[:90] + "Graphs/" + self.title + "_InPlane_Rxy_Fitted" + ".png"
        elif xx:
            address = self.DataAddress[:90] + "Graphs/" + self.title + "_Rxx_Fitted" + ".png"
        else:
            address = self.DataAddress[:90] + "Graphs/" + self.title + "_Rxy_Fitted" + ".png"
        plt.savefig(address)
        plt.clf()

    def pltresiduals(self, data, xx, linearplot):
        if linearplot:
            rfit = self.linearfunc(data[1, :], *self.params_xy)
        else:
            rfit = self.parabolicfunc(data[1, :], *self.params_xx)
        residual = data[0, :] - rfit

        if xx:
            plt.title(self.title + " Rxx Residuals")
        else:
            plt.title(self.title + " Rxy Residuals")
        plt.scatter(data[1, :], residual, marker='.', label="residual (y-y_fit)")
        plt.hlines(0, np.min(data[1, :]), np.max(data[1, :]), lw=2, alpha=0.8)
        plt.xlabel('{} [{}]'.format(self.x_label, self.x_units))
        plt.ylabel('y-y_fit [{}]'.format(self.y_units))
        plt.legend(loc='best', numpoints=1)

        print('\nSaving plot 3 for ' + self.title)
        if "OutPlane" in self.DataAddress:
            if xx:
                plt.savefig(self.DataAddress[:90] + "Graphs/" + self.title + "_InPlane_Rxx_Residuals" + ".png")
            else:
                plt.savefig(self.DataAddress[:90] + "Graphs/" + self.title + "_InPlane_Rxy_Residuals" + ".png")
        elif xx:
            plt.savefig(self.DataAddress[:90] + "Graphs/" + self.title + "_Rxx_Residuals" + ".png")
        else:
            plt.savefig(self.DataAddress[:90] + "Graphs/" + self.title + "_Rxy_Residuals" + ".png")
        plt.clf()

    def main(self, rxyzero):
        rxxdata = np.loadtxt(self.DataAddress)
        address = list(self.DataAddress)
        address[-9] = 'y'
        rxyinit = np.loadtxt("".join(address))

        if self.title == "PST236 R5":
            rxxdataprime = rxxdata[:, np.where(np.abs(rxxdata[1]) < 1)][:, 0, :]
        elif not self.title == "PST380 I2":
            rxxdataprime = rxxdata[:, np.where(np.abs(rxxdata[1]) > 0.2)][:, 0, :]
            rxxdataprime = rxxdataprime[:, np.where(np.abs(rxxdataprime[1]) < 3)][:, 0, :]
        else:
            rxxdataprime = rxxdata

        self.pltinitial(rxxdataprime, True, False)
        self.pltfit(rxxdataprime, True, False)
        self.pltresiduals(rxxdataprime, True, False)

        rxyprime = rxyinit - (rxyzero / self.parabolicfunc(0, *self.params_xx)) * rxxdata

        self.pltinitial(rxyprime, False, True)
        self.pltfit(rxyprime, False, True)
        self.pltresiduals(rxyprime, False, True)

        return [self.params_xx, self.params_xy]

EP3/Mobility and Carrier Density/MobilityClass.py


import numpy as np
from hdf5storage import loadmat
from os.path import join

import sys
imset = sys.argv[1].lower().capitalize()

DS_PATH = 'D:/datasets/processed/voc2012'
PRED_PATH = join(DS_PATH, 'Deeplab_Prediction', imset)
GT_PATH = join(DS_PATH, 'Truth', imset)

ds_info = loadmat(join(DS_PATH, 'dataset_info.mat'))
num_img = ds_info['num_'+imset.lower()]
nc = 21
class_names = ds_info['class_labels']

SM_FMT = '%s_%06d_softmax.mat'
SM_MAT_NAME = 'softmax_img'

PRED_FMT = '%s_%06d_prediction.mat'
PRED_MAT_NAME = 'pred_img'

GT_FMT = '%s_%06d_pixeltruth.mat'
GT_MAT_NAME = 'truth_img'

def load_sm_pred_gt(idx):
	global PRED_PATH, SM_FMT, SM_MAT_NAME, GT_PATH, GT_FMT, GT_MAT_NAME, imset
	sm = loadmat(join(PRED_PATH, SM_FMT % (imset, idx)))[SM_MAT_NAME].flatten()
	pred = loadmat(join(PRED_PATH, PRED_FMT % (imset, idx)))[PRED_MAT_NAME].flatten()
	gt = loadmat(join(GT_PATH, GT_FMT % (imset, idx)))[GT_MAT_NAME].flatten()
	return sm, pred, gt

acc_res = 0.1
hists = []
histlen = int(1./acc_res)

for _ in range(nc):
	correct_hist = np.zeros((histlen), dtype=np.uint64)
	count_hist = np.zeros_like(correct_hist)
	hists.append((correct_hist, count_hist))

for i in range(1, num_img+1):
	scores, pred, gt = load_sm_pred_gt(i)
	bins = np.floor(scores/acc_res).astype(np.uint8)

	for c in range(nc):
		if (gt==c).sum() == 0:
			continue

		gt_mask = gt[gt==c]
		pred_mask = pred[gt==c]
		bin_mask = bins[gt==c]

		for binno in np.unique(bin_mask):
			pred_bin = pred_mask[bin_mask==binno]
			gt_bin = gt_mask[bin_mask==binno]
			hists[c][0][min(binno, histlen-1)] += np.sum(pred_bin==gt_bin)
			hists[c][1][min(binno, histlen-1)] += np.sum(bin_mask==binno)

for i, (correct_hist, count_hist) in enumerate(hists):
	acc_hist = correct_hist.astype(np.float64)/np.maximum(1e-7, count_hist)
	class_name = class_names[i]

	import matplotlib.pyplot as plt
	fig, ax = plt.subplots()
	ax.bar(range(acc_hist.shape[0]), [v*acc_res for v in range(acc_hist.shape[0])], align='edge', color='r', alpha=0.5)
	ax.bar(range(acc_hist.shape[0]), acc_hist, align='edge')
	ax.set_xlabel('Confidence')
	ax.xaxis.set_ticks(np.linspace(0, acc_hist.shape[0], num=6))
	ax.set_xticklabels(['%.1f' % v for v in np.linspace(0.0, 1.0, num=6)])
	ax.set_ylabel('Accuracy')
	ax.yaxis.set_ticks(np.linspace(0.0, 1.0, num=6))
	ax.set_title('%s' % class_name)
	fig.savefig('acc_hists/%s_acc_hist.png' % class_name, bbox_inches='tight')
	plt.close(fig)


import constants
from PyQt5.QtWidgets import QPushButton, QLabel, QRadioButton, QGroupBox
from PyQt5.QtCore import pyqtSlot

class button(QPushButton):
    c = constants.constants()
    def __init__(self):
        super().__init__()
        self.setText("Nakarm")
        self.setDefault(False)

        self.clicked.connect(self.button_clicked)

        self.show()

    def button_clicked(self):
        self.make_label()

    def make_label(self):
        self.label = QLabel()#"lol")
        self.label.resize(200, 100)
        self.label.move(self.c.POS_X + 150, self.c.POS_Y + 120)

        self.label.show()


#! /usr/bin/env python
# -*- coding: utf-8 -*-

"""
Created on Feb 20 2017
@author: amourleey

"""
import cv2
import numpy as np
import matplotlib.pyplot as plt

img = cv2.imread('data/images/meixi.png')
b, g, r = cv2.split(img)
img2 = cv2.merge([r, g, b])
# img2 = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

plt.subplot(121),plt.imshow(img)
plt.title('BGR'),plt.xticks([]),plt.yticks([])
plt.subplot(122),plt.imshow(img2)
plt.title('RGB'),plt.xticks([]),plt.yticks([])

plt.show()

cv2.imshow('bgr image', img)
cv2.imshow('rgb image', img2)
cv2.waitKey( 0 )
cv2.destroyAllWindows()


import pytest
from sklearn.exceptions import NotFittedError

from feature_engine.imputation import DropMissingData

def test_detect_variables_with_na(df_na):
    # test case 1: automatically detect variables with missing data
    imputer = DropMissingData(missing_only=True, variables=None)
    X_transformed = imputer.fit_transform(df_na)
    # init params
    assert imputer.missing_only is True
    assert imputer.variables is None
    # fit params
    assert imputer.variables_ == ["Name", "City", "Studies", "Age", "Marks"]
    assert imputer.n_features_in_ == 6
    # transform outputs
    assert X_transformed.shape == (5, 6)
    assert X_transformed["Name"].shape[0] == 5
    assert X_transformed.isna().sum().sum() == 0

def test_selelct_all_variables_with_na_when_variables_is_none(df_na):
    imputer = DropMissingData(missing_only=False, variables=None)
    X_transformed = imputer.fit_transform(df_na)
    assert imputer.n_features_in_ == 6
    assert imputer.variables_ == ["Name", "City", "Studies", "Age", "Marks", "dob"]
    assert X_transformed.shape == (5, 6)
    assert X_transformed[imputer.variables_].isna().sum().sum() == 0

def test_detect_variables_with_na_in_variables_entered_by_user(df_na):
    imputer = DropMissingData(
        missing_only=True, variables=["City", "Studies", "Age", "dob"]
    )
    X_transformed = imputer.fit_transform(df_na)
    assert imputer.variables == ["City", "Studies", "Age", "dob"]
    assert imputer.variables_ == ["City", "Studies", "Age"]
    assert X_transformed.shape == (6, 6)

def test_return_na_data_method(df_na):
    imputer = DropMissingData(
        missing_only=True, variables=["City", "Studies", "Age", "dob"]
    )
    imputer.fit(df_na)
    X_nona = imputer.return_na_data(df_na)
    assert X_nona.shape == (2, 6)

def test_error_when_missing_only_not_bool():
    with pytest.raises(ValueError):
        DropMissingData(missing_only="missing_only")

def test_non_fitted_error(df_na):
    with pytest.raises(NotFittedError):
        imputer = DropMissingData()
        imputer.transform(df_na)


#!/usr/bin/env python

import os
import sys
sys.path.append('/Users/bennet/python/module-list')
from os.path import join as pathjoin
from prettytable import PrettyTable
from pprint import pprint

def read_inc_file(root, file, modules_dict):
    with open(pathjoin(root, file), 'r') as incfile:
        whatis_dict = {'Description': ''}
        app = ''
        line = incfile.readline()
        while line:
            if line[:4] == 'proc':
                app = line.split('::')[2]
            else:
                parts = line.split('"')
                if parts[0].strip() == 'module-whatis':
                    lpart = parts[1].partition(':')
                    if lpart[2]:
                        whatis_dict[lpart[0]] = lpart[2].strip()
                    else:
                        whatis_dict['Description'] = lpart[0]
            line = incfile.readline()
        if app:
            if app not in modules_dict:
                modules_dict[app] = {'versions':[]}
            modules_dict[app]['whatis'] = whatis_dict

def read_module_file(root, file, modules_dict):
    with open(pathjoin(root, file), 'r') as modulefile:
        app = ''
        version = ''
        line = modulefile.readline()
        while line and (not app or not version):
            parts = line.split()
            if (len(parts) >= 3) and (parts[0] == 'set'):
                if parts[1] == 'app':
                    app = parts[2]
                elif parts[1] == 'version':
                    version = parts[2]
            line = modulefile.readline()
        if app:
            if app not in modules_dict:
                modules_dict[app] = {'versions':[]}
            modules_dict[app]['versions'].append(version)

module_trees = ['/usr/cac/rhel6/Modules/modulefiles',
                '/usr/flux/software/rhel6/Modules/modulefiles',
                '/usr/cac/rhel6/lsa/Modules/modulefiles',
                '/usr/cac/rhel6/med/Modules/modulefiles',
                '/usr/cac/rhel6/sph/Modules/modulefiles',
                '/usr/flux/software/rhel6/isr/Modules/modulefiles']

# with open('/home/ajying/misc/module-list.html', 'w') as html:
# with open('/afs/umich.edu/group/f/flux/Public/html/module-list.html', 'w') as html:
#    html.write('\n')

for tree_path in module_trees:
    header = ['Module', 'Versions', 'Description']
    table = PrettyTable(header)
    table.align = 'l'
    modules_dict = {}
    for root, dirs, files in os.walk(tree_path):
        for file in files:
            if 'inc.tcl' in file:
                read_inc_file(root, file, modules_dict)
            elif 'version' not in file:
                read_module_file(root, file, modules_dict)
    for module, data in sorted(modules_dict.items()):
        versions = data['versions']
        if len(versions) == 0:
            versions.append('')
        versions.sort()
        desc_lines = []
        desc_line_len = 50
        desc = ''
        if 'whatis' in data:
            desc = data['whatis']['Description']
        while len(desc) > desc_line_len:
            cutoff = desc.rfind(' ', 0, desc_line_len)
            if cutoff > 0:
                desc_lines.append(desc[:cutoff])
                desc = desc[cutoff:]
        desc_lines.append(desc)
        table.add_row([module[:20], versions[0][:20], desc_lines[0]])
        count = 1
        while (count < len(versions) or count < len(desc_lines)):
            row = ['', '', '']
            if count < len(versions):
                row[1] = versions[count][:20]
            if count < len(desc_lines):
                row[2] = desc_lines[count]
            table.add_row(row)
            count += 1
        table.add_row(['', '', ''])

#     print(tree_path)
#     print(table)
#     print('')

#    with open('/home/ajying/misc/module-list.html', 'a') as html:
#     with open('/afs/umich.edu/group/f/flux/Public/html/module-list.html', 'a') as html:
#         html.write('<h2>' + tree_path + '</h2>')
#         html.write(table.get_html_string(format=True).encode('utf8'))
#         html.write('\n')


from random import random

from pygame.math import Vector2

from logic import Game, Player

def level_1():
    g = Game()
    g.add_player(Player(position=Vector2(0.2, 0.5),
                        movement=Vector2(0, 0.002*random())))
    g.add_player(Player(position=Vector2(0.8, 0.5),
                        movement=Vector2(0, 0.002*random())))
    return g


# This will be the main class to pull the data from CoinBase. This might not be the
# best way to get the information, but CoinBase is the current App that i use to
# buy/sell crypto already

from coinbase.wallet.client import Client

api_key_file = open(".coinbase_key", "r")
api_secret_file = open(".coinbase_secret", "r")

api_key = api_key_file.readline()
api_secret = api_secret_file.readline()

api_key_file.close()
api_secret_file.close()

client = Client(api_key, api_secret)

def getCryptoBuyPrice(coin, currency):
    response = client.get_buy_price(currency_pair = "" + coin + "-" + currency)
    return response['amount']

def getCryptoSellPrice(coin, currency):
    response = client.get_sell_price(currency_pair = "" + coin + "-" + currency)
    return response['amount']

def getCryptoSpotPrice(coin, currency):
    response = client.get_spot_price(currency_pair = "" + coin + "-" + currency)
    return response['amount']

def getTime():
    return client.get_time()

print(getCryptoBuyPrice("BTC", "USD"))
print(getCryptoSellPrice("BTC", "USD"))
print(getCryptoSpotPrice("BTC", "USD"))
print(getTime())


import requests

class MyRequest(object):

    def __init__(self, data):
        self.method = data.get('request_type').strip()
        self.url = data.get('request_url').strip()
        self.params = {}
        for dic in data.get('request_params'):
            if dic.get('execute') == 1:
                self.params[dic['key']] = dic['value']
        self.headers = {}
        for dic in data.get('header_params'):
            if dic.get('execute') == 1:
                self.headers[dic['key']] = dic['value']

    def start(self):
        if self.method == 'get':
            response = requests.request(self.method, self.url, headers=self.headers, params=self.params, timeout=30)
        else:
            response = requests.request(self.method, self.url, headers=self.headers, data=self.params, timeout=30)

        return response


def rhs(u,t):
    N = len(u) - 1
    rhs = zeros(N+1)
    rhs[0] = dsdt(t)
    for i in range(1, N):
        rhs[i] = (beta/dx**2)*(u[i+1] - 2*u[i] + u[i-1]) + g(x[i], t)
    rhs[N] = (beta/dx**2)*(2*u[N-1] + 2*dx*dudx(t) - 2*u[N]) + g(x[N], t)
    return rhs

def u_exact(x,t):
    return (3*t + 2)*(x - L)

def dudx(t):
    return 0

def s(t):
    return 323

def dsdt(t):
    return 0

def g(x, t):
    return 0

from numpy import linspace,zeros
import time

dt = 0.00034375

L = 0.5
beta = 8.2E-5
N = 40
x = linspace(0, L, N+1)
dx = x[1] - x[0]
u = zeros(N+1)
U_0 = zeros(N+1)
U_0[0] = s(0)
U_0[1:] = 283

#t0 = time.process_time
from ode_system_FE import ode_FE
u, t = ode_FE(rhs, U_0, dt, T=1*60*60)
#t1 = time.process_time
#print('CPU time: %.1fs' % (t1 - t0))

# Make movie
import os
os.system('rm tmp_*.png')
import matplotlib.pyplot as plt
plt.ion()
y = u[0,:]
lines = plt.plot(x, y)
plt.axis([x[0], x[-1], 273, s(0)+10])
plt.xlabel('x')
plt.ylabel('u(x,t)')
counter = 0
# Plot each of the first 100 frames, then increase speed by 10x
change_speed = 100
for i in range(0, u.shape[0]):
    print(t[i])
    plot = True if i <= change_speed else i % 10 == 0
    lines[0].set_ydata(u[i,:])
    if i > change_speed:
        plt.legend(['t=%.0f 10x' % t[i]])
    else:
        plt.legend(['t=%.0f' % t[i]])
    plt.draw()
    if plot:
        plt.savefig('tmp_%04d.png' % counter)
        counter += 1


"""tango_with_django_project URL Configuration

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

from registration.backends.simple.views import RegistrationView

class MyRegView(RegistrationView):
    def get_success_url(self, user=None):
        return '/forms/'

urlpatterns = [
    url(r'^admin/', admin.site.urls),

    #bellow related to rango app

    # url(r'^$', views.index, name='index'),
    # url(r'^rango/', include('rango.urls')),
    url(r'^accounts/register/$', MyRegView.as_view(),name='registration_register'),
    url(r'^accounts/', include('registration.backends.simple.urls')),
    # url(r'^conversations/', include('conversations.urls')),
    url(r'^forms/', include('forms.urls')),
    # url(r'^analise/', include('analise.urls')),
]

urlpatterns += [
    url(r'^api-auth/', include('rest_framework.urls',
                               namespace='rest_framework')),
]