import argparse
import json

import pandas as pd
import pypff
from bs4 import BeautifulSoup

def find_tasks_folder(root):
    for i, folder in enumerate(root.sub_folders):
        if folder.name == "Top of Personal Folders":
            for j, sub_folder in enumerate(folder.sub_folders):
                if sub_folder.name == "Tasks":
                    break
            break
    return root.sub_folders[i].sub_folders[j]

def get_subtasks(item):
    for en in item.record_sets[0].entries:
        try:
            output = en.data.decode("utf-16")
            if '{"Values":[' in output:
                output = json.loads(output)
                subtasks = [
                    {
                        "list": None,
                        "task": task["Subject"],
                        "description": None,
                        "creation_time": task["CreatedDateTime"],
                    }
                    for task in list(output.values())[0]
                ]
                return subtasks
        except (UnicodeDecodeError, AttributeError):
            pass
    return []

def recurse(folder, level=0):
    tasks = []
    for i, item in enumerate(folder.sub_items):
        if isinstance(item, pypff.folder):
            tasks.extend(recurse(item, level=level + 1))
        if isinstance(item, pypff.message):
            try:
                soup = BeautifulSoup(item.html_body, features="lxml")
                body = soup.body.div.string.rstrip()
            except (OSError, AttributeError, TypeError):
                body = None
            item_info = {
                "list": folder.name,
                "task": item.subject,
                "description": body,
                "creation_time": item.creation_time,
            }
            tasks.append(item_info)
            subtasks = get_subtasks(item)
            tasks.extend(subtasks)
    return tasks

if __name__ == "__main__":
    parser = argparse.ArgumentParser(
        description="Export tasks from a Microsoft To Do pst file to csv."
    )
    parser.add_argument("infile", type=argparse.FileType("r"), help="input file (.pst)")
    parser.add_argument(
        "-o",
        "--outfile",
        nargs="?",
        type=argparse.FileType("w"),
        help="output file (.csv)",
    )
    args = parser.parse_args()
    pst = pypff.file()
    pst.open(args.infile.name)
    root = pst.get_root_folder()
    tasks_folder = find_tasks_folder(root)
    tasks = recurse(tasks_folder)
    pst.close()

    tasks = pd.DataFrame(tasks)
    if args.output_file is None:
        print(tasks)
    else:
        tasks.to_csv(args.output_file, index=False)


from django.contrib import admin
from wagtail.contrib.modeladmin.options import (
    modeladmin_register,
    ModelAdmin,
    ModelAdminGroup,
)
from django.utils.translation import gettext_lazy as _

from users.models import Student, Teacher, Entrant, StudentCard, UserDocument, Profile, Achievement, UserAchievement, \
    UserContact
from users.snippets import UserTag, Quote

class StudentAdmin(ModelAdmin):
    model = Student
    menu_label = _("Students")
    menu_icon = "user"
    list_display = ("user", "group", "student_card")
    list_filter = ("group",)
    ordering = ("user__last_name", "user__first_name", "user__patronymic")

class StudentCardAdmin(ModelAdmin):
    model = StudentCard
    menu_label = _("Student Cards")
    menu_icon = "form"
    list_display = ("card_id", "student")
    ordering = (
        "student__user__last_name",
        "student__user__first_name",
        "student__user__patronymic",
        "card_id",
    )

class UserDocumentAdmin(ModelAdmin):
    model = UserDocument
    menu_label = _("User Documents")
    menu_icon = "doc-full-inverse"
    list_display = ("user", "name")
    ordering = ("user__last_name", "user__first_name", "user__patronymic", "name")

class TeacherAdmin(ModelAdmin):
    model = Teacher
    menu_label = _("Teachers")
    menu_icon = "user"
    list_display = ("user",)
    list_filter = ("subjects", "groups__group__name")
    ordering = (
        "user__last_name",
        "user__first_name",
        "user__patronymic",
        "groups__group__name",
        "subjects__name",
    )

class EntrantAdmin(ModelAdmin):
    model = Entrant
    menu_label = _("Entrants")
    menu_icon = "user"
    list_display = ("user",)
    ordering = ("user__last_name", "user__first_name", "user__patronymic")

class AchievementAdmin(ModelAdmin):
    model = Achievement
    menu_label = _("Achievements")
    menu_icon = "success"
    list_display = ("title", "short_description")
    ordering = ("title", )

@modeladmin_register
class UsersGroupAdmin(ModelAdminGroup):
    menu_label = _("Users")
    menu_icon = "group"
    add_to_settings_menu = False
    items = (
        StudentAdmin,
        StudentCardAdmin,
        EntrantAdmin,
        TeacherAdmin,
        UserDocumentAdmin,
        AchievementAdmin,
    )

class ProfileAdmin(ModelAdmin):
    model = Profile
    menu_label = _("Profiles")
    menu_icon = "user"
    list_display = ("user", )
    ordering = ("user__last_name", "user__first_name", "user__patronymic")

class UserAchievementAdmin(ModelAdmin):
    model = UserAchievement
    menu_label = _("User Achievements")
    menu_icon = "success"
    list_display = ("user_profile", "achievement", )
    ordering = ("achievement", "user_profile", )

class UserContactAdmin(ModelAdmin):
    model = UserContact
    menu_label = _("User Contacts")
    menu_icon = "group"
    list_display = ("user_profile", "title", "value", )
    ordering = ("title", "user_profile", )

@modeladmin_register
class ProfileGroupAdmin(ModelAdminGroup):
    menu_label = _("Profile Settings")
    menu_icon = "user"
    add_to_settings_menu = False
    items = (
        ProfileAdmin,
        UserAchievementAdmin,
        UserContactAdmin,
    )

admin.site.register(Student)
admin.site.register(Teacher)
admin.site.register(Entrant)
admin.site.register(StudentCard)
admin.site.register(UserDocument)
admin.site.register(UserTag)
admin.site.register(Quote)


_classifiers = """
Development Status :: 4 - Beta
Intended Audience :: Developers
License :: OSI Approved :: Apache Software License
Operating System :: OS Independent
Programming Language :: Python :: 2.6
Programming Language :: Python :: 2.7
Topic :: Database :: Front-Ends
Topic :: Scientfic/Engineering :: Interface Engine/Protocol Translator
"""

try:
    from setuptools import setup, find_packages
except ImportError:
    from ez_setup import use_setuptools
    use_setuptools()
    from setuptools import setup, find_packages

extra_opts = {}
try:
    with open('README.rst', 'r') as fd:
        extra_opts['long_description'] = fd.read()
except IOError:
    pass        # Install without README.rst

setup(
    name='pymongo-spark',
    version='0.1.dev0',
    author='MongoDB, Inc.',
    author_email='mongodb-user@googlegroups.com',
    description='Utilities for using Spark with PyMongo',
    keywords=['spark', 'mongodb', 'mongo', 'hadoop', 'pymongo'],
    license="http://www.apache.org/licenses/LICENSE-2.0.html",
    platforms=['any'],
    url='https://github.com/mongodb/mongo-hadoop',
    install_requires=['pymongo>=3.0.3'],
    packages=find_packages(exclude=('test',)),
    classifiers=_classifiers.splitlines(),
    test_suite='test',
    **extra_opts
)


import random

n = int(input('Enter number of rounds: '))

# Snake(s), Water(w), Gun(g)
options = ['s', 'w', 'g']

# Round numbers
rounds = 1

# Count of computer wins
computer_win = 0

# Count of player wins
user_win = 0

# For n rounds of game
while rounds <= n:

	# Display round
	print(f"Round :{rounds}\nSnake - 's'\nWater - 'w'\nGun - 'g'")

	# Exception handling
	try:
		player = input("Choose your option: ")
	except EOFError as e:
		print(e)

	# Control of bad inputs
	if player != 's' and player != 'w' and player != 'g':
		print("Invalid input, try again")
		continue

	# random.choice() will randomly choose
	computer = random.choice(options)

	# Conditions based on the game rule
	if computer == 's':
		if player == 'w':
			computer_win += 1
		elif player == 'g':
			user_win += 1

	elif computer == 'w':
		if player == 'g':
			computer_win += 1
		elif player == 's':
			user_win += 1

	elif computer == 'g':
		if player == 's':
			computer_win += 1
		elif player == 'w':
			user_win += 1

	# Announce winner of every round
	if user_win > computer_win:
		print(f"You Won round {rounds}")
	elif computer_win > user_win:
		print(f"Computer Won round {rounds}")
	else:
		print("Draw!!")

	rounds += 1

# Final winner based on the number of wons
if user_win > computer_win:
	print("Congratulations! You Won")
elif computer_win > user_win:
	print("You lose!!")
else:
	print("Match Draw!!")


# while

# i = 0
# sum = 0
# while i != 5 or sum != 275:
#     print(i)
#     i += 1

def _min(value1, value2):
    if value1 < value2:
        return value1
    else:
        return value2

print(_min(value2=30, value1=4))


# Distinct Prime Factors https://projecteuler.net/problem=47

from math import sqrt

def isPrime(n):
    if n > 1:
        for i in range(2, int(sqrt(n)) + 1):
            if n % i == 0:
                return False
        return True
    return False

def primeFactorization(n):
    factors = []
    number = n
    while not isPrime(number):
       for i in range(2, int(sqrt(number)) + 1):
            if number % i == 0:
                number /= i
                factors.append(i)
                break
    factors.append(int(number))
    return factors

found = False
i = 6
factors = primeFactorization(2)
factors2 = primeFactorization(3)
factors3 = primeFactorization(4)
factors4 = primeFactorization(5)
while not found:
    i += 1
    factors = factors2
    factors2 = factors3
    factors3 = factors4
    factors4 = primeFactorization(i)
    if len(set(factors)) == 4 and len(set(factors2)) == 4 and len(set(factors3)) == 4 and len(set(factors4)) == 4:
        found = True
        print(i-3)


from flask import Flask, render_template, request

import numpy as np
import os

from keras.preprocessing.image import load_img
from keras.preprocessing.image import img_to_array
from keras.models import load_model

#load model
model =load_model("model/redixolabs.h5")

print('@@ Model loaded')

def pred_cot_dieas(cott_plant):
  test_image = load_img(cott_plant, target_size = (150, 150))
  print("@@ Got Image for prediction")

  test_image = img_to_array(test_image)/255
  test_image = np.expand_dims(test_image, axis = 0)

  result = model.predict(test_image).round(3)
  print('@@ Raw result = ', result)

  pred = np.argmax(result)

  if pred == 0:
    return "Healthy Plant", 'healthy.html'
  elif pred == 1:
      return 'Diseased Plant', '1.html'
  elif pred == 2:
      return 'Diseased Plant', '2.html'
  else:
    return "Healthy Plant", 'healthy.html'

#------------>>pred_cot_dieas<<--end

app = Flask(__name__)

# render index.html page
@app.route("/", methods=['GET', 'POST'])
def home():
        return render_template('index.html')

# get input image from client then predict class and render respective .html page for solution
@app.route("/predict", methods = ['GET','POST'])
def predict():
     if request.method == 'POST':
        file = request.files['image']
        filename = file.filename
        print("@@ Input posted = ", filename)

        file_path = os.path.join('static/user uploaded', filename)
        file.save(file_path)

        print("@@ Predicting class......")
        pred, output_page = pred_cot_dieas(cott_plant=file_path)

        return render_template(output_page, pred_output = pred, user_image = file_path)

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


import constants as c

import copy

from cppn import CPPN

import matplotlib.pyplot as plt

from tree import TREE

cppns = {}

def Create_CPPNs():

    for panelIndex in range(1,c.numberOfCPPNs+1):

        cppns[panelIndex] = CPPN()

def Draw_CPPNs():

    fig = Prep_Drawing()

    for panelIndex in range(1,c.numberOfCPPNs+1):

        cppns[panelIndex].Draw(fig,panelIndex)

    plt.show()

def Mutate_CPPNs(choice):

    for panelIndex in range(1,c.numberOfCPPNs+1):

        if panelIndex != choice:

            cppns[panelIndex] = copy.deepcopy(cppns[choice])

            cppns[panelIndex].Mutate()

def Prep_Drawing():

    fig = plt.figure(figsize=(20,10))

    #ax = fig.add_subplot(339)

    #ax.set_xlim( c.vectorFieldMinimum - c.vectorFieldCellWidth  , c.vectorFieldMaximum + c.vectorFieldCellWidth)

    #ax.set_ylim( c.vectorFieldMinimum - c.vectorFieldCellHeight , c.vectorFieldMaximum + c.vectorFieldCellHeight)

    return fig

# -------------- Main function -------------------

Create_CPPNs()

Draw_CPPNs()

while True:

    choice = int( input('Which vector field to you like the best [1-'+str(c.numberOfCPPNs)+']: ') )

    Mutate_CPPNs(choice)

    Draw_CPPNs()

exit()

tree = TREE()

for i in range(10):

    tree.Update_Positions_Starting_At(0,0)

    #tree.Print()
    #tree.Count()
    tree.Draw()

    tree.Grow(cppn)


import WhiteBoxMath as whiteBoxMath
import WhiteBoxInit as init

import argparse
import azlmbr.legacy.general as general
import azlmbr.bus as bus
import azlmbr.whitebox.api as api

# usage: pyRunFile path/to/file/tetrahedron.py <radius>

# mathematically, a tetrahedron built with spherical coordinates will not be centered vertically at origin
# we need to calculate how far off it is to vertically center it
def calculate_offset_for_tetrahedron(radius):
    h1 = whiteBoxMath.spherical_to_cartesian(0.0, 0.0, radius).z
    h2 = whiteBoxMath.spherical_to_cartesian(109.5, 90.0, radius).z
    offset = (h1 + h2) * -0.5

    return whiteBoxMath.spherical_to_cartesian(0.0, 0.0, offset)

def create_tetrahedron(whiteBoxMesh, radius=0.75):
    # get coordinates for all the vertices using the internal angles of a tetrahedron
    offset = calculate_offset_for_tetrahedron(radius)
    pos1 = whiteBoxMath.spherical_to_cartesian(0.0, 0.0, radius).Add(offset)
    pos2 = whiteBoxMath.spherical_to_cartesian(109.5, 90.0, radius).Add(offset)
    pos3 = whiteBoxMath.spherical_to_cartesian(109.5, 210.0, radius).Add(offset)
    pos4 = whiteBoxMath.spherical_to_cartesian(109.5, 330.0, radius).Add(offset)

    # create vertices from all the coordinates
    v1 = whiteBoxMesh.AddVertex(pos1)
    v2 = whiteBoxMesh.AddVertex(pos2)
    v3 = whiteBoxMesh.AddVertex(pos3)
    v4 = whiteBoxMesh.AddVertex(pos4)

    # add polygons for each set of vertices
    fvh1 = whiteBoxMesh.AddTriPolygon(v1, v2, v3)
    fvh2 = whiteBoxMesh.AddTriPolygon(v1, v3, v4)
    fvh3 = whiteBoxMesh.AddTriPolygon(v1, v4, v2)
    fvh4 = whiteBoxMesh.AddTriPolygon(v2, v4, v3)

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description='Creates a tetrahedron.')
    parser.add_argument('radius', nargs='?', default=0.75, type=float, help='radius of the tetrahedron')
    args = parser.parse_args()

    # initialize whiteBoxMesh
    whiteBoxEntity = init.create_white_box_entity("WhiteBox-Tetrahedron")
    whiteBoxMeshComponent = init.create_white_box_component(whiteBoxEntity)
    whiteBoxMesh = init.create_white_box_handle(whiteBoxMeshComponent)

    # clear whiteBoxMesh to make a tetrahedron from scratch
    whiteBoxMesh.Clear()

    create_tetrahedron(whiteBoxMesh, args.radius)

    # update whiteBoxMesh
    init.update_white_box(whiteBoxMesh, whiteBoxMeshComponent)


import os
from pathlib import Path

cwd = Path.cwd()
home = Path.home()
root_dir = cwd.parents[0]

path_to_logs = root_dir / "logs"
path_to_figures = root_dir / "figures"

simulated_data = Path(os.environ['ASTRI_SIMULATED'])
astri_irf = Path(os.environ["ASTRI_IRF"],"astri_100_43_008_0502_C0_20_AVERAGE_50h_SC_v1.0.lv3.fits")


from conexion import *
from surprise import Dataset
from surprise import Reader
from surprise import SVD

def obtener_calificaciones():
    result = pd.read_sql("SELECT * FROM calificación", conexion_db)
    return result

def obtener_muebles_no_calificados(usuario):
    result = pd.read_sql("SELECT * FROM mueble LEFT JOIN ((SELECT * FROM calificación WHERE calificación.correo_usuario = \"%s\") AS tabla_calificacion) ON mueble.referencia = tabla_calificacion.referencia_mueble WHERE estado=1" % (usuario), conexion_db)
    return result[result.isnull().any(axis=1)]["referencia"]

def predecir(fila, usuario, modelo):
    prediccion = modelo.predict(usuario, fila["referencia"])
    return prediccion.est

def recomendar_colaborativo(usuario):
    datos_df = obtener_calificaciones()
    reader = Reader(rating_scale=(1, 10))

    datos = Dataset.load_from_df(datos_df[["correo_usuario", "referencia_mueble", "puntaje"]], reader)

    referencias_prediccion = pd.DataFrame.to_numpy(obtener_muebles_no_calificados(usuario))

    training_dataset = datos.build_full_trainset()

    svd = SVD()
    svd.fit(training_dataset)

    resultado = pd.DataFrame(referencias_prediccion, columns=["referencia"])
    resultado["calificacion"] = resultado.apply(predecir, axis=1, args=[usuario, svd])

    return resultado


#Matrix chain multiplications O(n3) , O(n2)

class Solution:
    def maxCoins(self, nums: List[int]) -> int:

        nums.insert(0,1)
        nums.append(1)
        n = len(nums)
        m = [[0]*n for i in range(n)]
        # for i in range(1,n):
        #     m[i][i] = 0
        for L in range(2,n):
            for i in range(1,n-L+1):
                j = i+L-1
                m[i][j]=0
                # print(i,j)
                for k in range(i,j):
                    # print(m[i][k]+m[k+1][j]+nums[i-1]*nums[k]*nums[j])
                    m[i][j] = max(m[i][j],m[i][k]+m[k+1][j]+nums[i-1]*nums[k]*nums[j])
        # print(nums,m)
        return m[1][n-1]


def main():
    print("Converts divisas to Pesos")

    moneda = input("Qué moneda desea comprar? (Euro/Dolar) ").lower()

    if moneda == "euro":
        cantidad = int(input("Ingrese la cantidad"))
        resultado = cantidad * 633
        print(f"El total es de ${resultado}")
    elif moneda == "dolar":
        cantidad = int(input("Ingrese la cantidad"))
        resultado = cantidad * 525
        print(f"El total es de ${resultado}")
    else:
        print("Ingrese una moneda válida por favor")
        #main()

main()


import socket  # Import socket module
import os
import time

port = 60000  # Reserve a port for your service every new transfer wants a new port or you must wait.
# s = socket.socket()  # Create a socket object
host = '127.0.0.1'  # Get local machine name
# s.bind((host, port))  # Bind to the port

s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
count = 1;
filecount = 0
videoNum = 100
byteRead = 2064 #total number of bytes read/send per transfer

# print(os.path.getsize('/home/amey/anaconda3/envs/pishang/newfiles/new6.mp4'))
starttime = time.time()

# We'll limit ourself to a 40KB/sec maximum send rate
maxSendRateBytesPerSecond = (videoNum*byteRead)

def ConvertSecondsToBytes(numSeconds):
   return numSeconds*maxSendRateBytesPerSecond

def ConvertBytesToSeconds(numBytes):
   return float(numBytes)/maxSendRateBytesPerSecond

# We'll add to this tally as we send() bytes, and subtract from
# at the schedule specified by (maxSendRateBytesPerSecond)
bytesAheadOfSchedule = 0

# Dummy data buffer, just for testing
dataBuf = bytearray(byteRead)

prevTime = None

while True:

    # s.listen(5)  # Now wait for client connection.
    # print('Server listening....')
    # conn, addr = s.accept()  # Establish connection with client.
    # print('Got connection from', addr)

    now = time.time()
    if (prevTime != None):
        bytesAheadOfSchedule -= ConvertSecondsToBytes(now - prevTime)
    prevTime = now

    filename = '/home/amey/anaconda3/envs/pishang/newfiles/new3.ts'  # In the same folder or path is this file running must the file you want to tranfser to be
    # print('filename ',filename)
    # filename = 'data/a.mp4'
    f = open(filename, 'rb')
    l = f.read(byteRead)

    start = time.time()

    # while (l):
    #     count += 1
    #     numBytesSent = l
    #     s.sendto(l, (host, port))
    #     l = f.read(byteRead)
    # f.close()

    s.sendto(l, (host, port))
    numBytesSent = byteRead
    print(numBytesSent)
    if (numBytesSent > 0):
        bytesAheadOfSchedule += numBytesSent
        if (bytesAheadOfSchedule > 0):
            time.sleep(ConvertBytesToSeconds(bytesAheadOfSchedule))
    else:
        print
        "Error sending data, exiting!"
        break
    # s.sendto(b'END', (host, port))  # send empty byte to inform about closing file
    filecount +=1

    if filecount == videoNum:
        print("entered ",filecount)
        break
    print("filecount",filecount)
# conn.close()

print('Count ', count)
# print(os.path.getsize('f1.mp4'))
print(time.time()-starttime)

x = 0
while(x<11):
    s.sendto(b'END', (host, port))
    x+=1


from django.conf.urls import patterns, include, url
from . import ajax, views

# See: https://docs.djangoproject.com/en/dev/topics/http/urls/
urlpatterns = patterns(
    '',
    url(r'^$', views.IndexView.as_view(), name='hotel_index'),
    url(r'^city/$', ajax.CityView.as_view(), name='city'),
    url(r'^city/(?P<city_pk>\d+)/hotel/$', ajax.HotelView.as_view(), name='hotel'),
)


"""
SimPype's pipe.

"""
import inspect
import simpy
import types

import simpype
import simpype.build

def __enqueue(func, pipe, message):
	assert isinstance(pipe, Pipe)
	assert isinstance(message, simpype.Message)
	message.location = pipe
	message.resource = pipe.resource
	if inspect.isgeneratorfunction(func):
		result = yield pipe.env.process(func(pipe, message))
	else:
		result = func(pipe, message)
	pipe.full()
	return result

def __dequeue(func, pipe):
	assert isinstance(pipe, Pipe)
	if inspect.isgeneratorfunction(func):
		result = yield pipe.env.process(func(pipe))
	else:
		result = func(pipe)
	return result

def enqueue(arg):
	""" Decorator for overloading the default :class:`Pipe` enqueue behavior.

	Args:
		arg (:class:`Pipe`)(``self``):
			The :class:`Pipe` instance.

	If the overloading is done in scripts, the :class:`Pipe` instance must be provided as decorator argument.

	.. code-block:: python

		myresource = sim.add_resource(id = 'myresource')
		myresource.add_queue(id = 'myqueue')

		@simpype.pipe.enqueue(myresource.pipe)
		def enqueue(self, message):
			return self.queue['myqueue'].push(message)

	If the overloading is done inside a Pipe subclass, the decorator must be called without any arguments. :class:`Pipe` instance is automatically provided through ``self``.

	.. code-block:: python

		class MyPipe(simpype.Pipe):
			def __init__(self, sim, resource, id):
				super().__init__(sim, resource, id)
				self.add_queue(id = 'myqueue')

			@simpype.pipe.enqueue
			def enqueue(self, message):
				return self.queue['myqueue'].push(message)

	"""
	if isinstance(arg, Pipe):
		pipe = arg
		def decorator(func):
			def wrapper(pipe, message):
				return __enqueue(func, pipe, message)
			pipe.enqueue = types.MethodType(wrapper, pipe)
			return wrapper
		return decorator
	else:
		func = arg
		def wrapper(pipe, message):
			return __enqueue(func, pipe, message)
		return wrapper

def dequeue(arg):
	""" Decorator for overloading the default :class:`Pipe` dequeue behavior.

	Args:
		arg (:class:`Pipe`)(``self``):
			The :class:`Pipe` instance.

	If the overloading is done in scripts, the :class:`Pipe` instance must be provided as decorator argument.

	.. code-block:: python

		myresource = sim.add_resource(id = 'myresource')
		myresource.add_queue(id = 'myqueue')

		@simpype.pipe.dequeue(myresource.pipe)
		def dequeue(self):
			return self.queue['myqueue'].pop()

	If the overloading is done inside a Pipe subclass, the decorator must be called without any arguments. :class:`Pipe` instance is automatically provided through ``self``.

	.. code-block:: python

		class MyPipe(simpype.Pipe):
			def __init__(self, sim, resource, id):
				super().__init__(sim, resource, id)
				self.add_queue(id = 'myqueue')

			@simpype.pipe.dequeue
			def dequeue(self):
				return self.queue['myqueue'].pop()

	"""
	if isinstance(arg, Pipe):
		pipe = arg
		def decorator(func):
			def wrapper(pipe):
				return __dequeue(func, pipe)
			pipe.dequeue = types.MethodType(wrapper, pipe)
			return wrapper
		return decorator
	else:
		func = arg
		def wrapper(pipe):
			return __dequeue(func, pipe)
		return wrapper

class Pipe:
	""" The pipe implements the queueing disciplines.

	Args:
		sim (:class:`Simulation`):
			The SimPype simulation object.
		resource (:class:`~simpype.resource.Resource`):
			The resource the pipe is associated to.
		id (str):
			The pipe id.

	Attributes:
		sim (:class:`Simulation`):
			The SimPype simulation object.
		env (simpy.Environment):
			The SimPy environment object.
		id (str):
			The pipe id.
		resource (:class:`~simpype.resource.Resource`):
			The resource the pipe is associated to.
		available (simpy.events.Event):
			Event signaling the presence of a :class:`~simpype.message.Message` in the pipe.
		queue (dict):
			The dictionary storing the :class:`~simpype.queue.Queue` instances associated to this pipe.

	"""
	def __init__(self, sim, resource, id):
		assert isinstance(sim, simpype.Simulation)
		assert isinstance(resource, simpype.Resource)
		self.sim = sim
		self.env = sim.env
		self.id = id
		self.resource = resource
		self.available = self.env.event()
		self.queue = {}
		self.log = True
		# Init
		self.a_wait_loop = self.env.process(self._wait_loop())

	@property
	def log(self):
		return self._log

	@log.setter
	def log(self, value):
		assert isinstance(value, bool)
		self._log = value
		for q in self.queue.values():
			q.log = value

	def _message_dropped(self, message, cause):
		assert isinstance(message, simpype.Message)
		assert message.location == self

	def _wait_loop(self):
		while True:
			yield self.resource.available & self.available
			self.available = self.env.event()
			self.resource.free()
			message = yield self.env.process(self.dequeue())
			if isinstance(message, simpype.Message):
				self.env.process(self._service(message))
			self.full()

	def _service(self, message):
		yield self.env.process(self.resource.service(message))
		self.resource.free()

	def add_queue(self, id, model = None):
		""" Add a new queue to the pipe.

		Args:
			id (str):
				The id of the new queue.
			model (str):
				The model of the new queue. If model is ``None``, the default queue model is created.

		Returns:
			:class:`~simpype.queue.Queue`

		"""
		queue = simpype.build.queue(self.sim, self, id, model)
		self.queue[queue.id] = queue
		return self.queue[queue.id]

	def full(self):
		""" Check if there is at least a :class:`~simpype.message.Message` in the pipe.

		Returns:
			bool

		"""
		tot = sum([len(q) for q in self.queue.values() if q.active.triggered])
		if not self.available.triggered and tot > 0:
			self.available.succeed()
			return True
		return False


#name: Vivek Mandava
#date: 10/2/2017
#email: VMandava_GPS@nec.edu
#purpose: Take home lab 5b week 5
#ex1

n=int(input("Enter the number that is to be checked: "))
#declaring function
def is_even(a):
	'''Returns true if even number number and false if odd'''
	#checking for even number
	if a%2 == 0:
		return ("true")
	else:
		return ("false")
#printing if the number is even or odd
if is_even(n) == ("true"):
	print("The number "+ str(n) +" is an even number")
else:
	print("The number "+ str(n)+ " is an odd number")


from django.db import router
from django.urls import path, include
from . import views
from django.contrib.auth import views as auth_views
from django.views.decorators.cache import cache_page
from rest_framework.routers import DefaultRouter

router = DefaultRouter()
router.register('contact', views.ContactViewSet)

urlpatterns = [
    path("", views.index.as_view(), name="index"),
    path("search/", views.search.as_view(), name="search"),
    path("find/", views.find.as_view(), name="find"),
    path("create/", views.CreateProfile.as_view(), name="create_profile"),
    path("register", views.Register.as_view(), name="register"),
    path("login", auth_views.LoginView.as_view(
        template_name="phonebook/login.html"), name="login"),
    path("logout", auth_views.LogoutView.as_view(
        template_name="phonebook/logout.html"), name="logout"),
    path("edit_profile/<int:pk>", views.EditProfile.as_view(), name="editprofile"),
    path("recent", views.Recent.as_view(), name="recent"),
    path("print/", views.PrintContact.as_view(), name="print"),
    path('api-auth/', include('rest_framework.urls')),
    path('api/v1/', include(router.urls))
]


# -*- coding: utf-8 -*-
import numpy as np

def Level_dataset(dirrect, level, start, end):
    dataset = []
    for i in range(start, end, 50):
        with open(dirrect+"\\"+level+"\\"+str(i)+"-"+str((i+50)-1)+".txt", "r") as f:
            for j in range(50):
                wdata = f.readline().split(":")
                number_word = wdata[0].split()
                if j < (50-1): #delete "\n"
                    dataset.append( [number_word[0], number_word[1], wdata[1][:-1]] )
                else: #last loop
                    dataset.append( [number_word[0], number_word[1], wdata[1]] )

    dataset = np.array(dataset)
    return dataset

def SentenceSplit(sentence):
    ans = False
    ecnt = slen = slencnt = 0

    for i in range(len(sentence)):
        if sentence[i] == " ":
            ecnt += 1
            if slen > 8:
                slencnt += 1

        elif sentence == ",":
            ans = True

        else:
            slen += 1

    if slencnt >= 2:
        ans = True

    return ans, ecnt


from pathlib import Path, PosixPath, WindowsPath
from typing import List, Tuple, Dict, Optional, Union

import imageio as iio
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import yaml

def convert_to_path(attribute: Union[str, Path]) -> Path:
    """Convert strings to Path and returns them."""
    if type(attribute) == PosixPath or type(attribute) == WindowsPath:
        return attribute
    elif type(attribute) == str:
        return Path(attribute)

def check_keys(dictionary: Dict, list_of_keys: List[str]) -> List:
    """
    Check, whether list_of_keys are in a dictionary and returns a list of
    keys missing in the dictionary.
    """
    missing_keys = []
    for key in list_of_keys:
        if key not in dictionary:
            missing_keys.append(key)
    return missing_keys

def read_config(path: Path) -> Dict:
    """Read structured config file defining a project."""
    path = convert_to_path(path)
    if path.exists() and path.suffix == ".yaml":
        with open(path, "r") as ymlfile:
            cfg = yaml.load(ymlfile, Loader=yaml.SafeLoader)
    else:
        raise FileNotFoundError(
            f"Could not open the yaml file at {path}\n"
            f"Please make sure the path is correct and the file exists!"
        )
    return cfg

class Coordinates:
    """
    Class to store xyz coordinates.

    Parameters
    ----------
    y_or_row: int
    x_or_column: int
    z: int

    Attributes
    __________
    x: int, column: int
        Represent x coordinates in a system and columns in an image.
    y, row: int
        Represent y coordinates in a system and rows in an image.
    z: int
        Represent z coordinates in a system.
    """
    def __init__(
            self, y_or_row: int, x_or_column: int, z: Optional[int] = None
    ) -> None:
        """
        Construct attributes for Class Coordinates.

        Parameters
        ----------
        y_or_row: int
        x_or_column: int
        z: int
        """
        self.y = y_or_row
        self.row = y_or_row
        self.x = x_or_column
        self.column = x_or_column
        self.z = z

def get_3D_df_keys(key: str) -> Tuple[str, str, str]:
    """
    Construct a tuple of strings representing keys in a aniposelib created 3
    dimensional DataFrame.
    """
    return key + "_x", key + "_y", key + "_z"

def create_calibration_key(
        videos: List[str], recording_date: str, calibration_index: int, iteration: Optional[int] = None,
) -> str:
    key = ""
    videos.sort()
    for elem in videos:
        key = key + "_" + elem
    if iteration is None:
        calibration_key = recording_date + "_" + str(calibration_index) + key
    else:
        calibration_key = recording_date + "_" + str(calibration_index) + key + "_" + str(iteration)
    return calibration_key

def get_3D_array(df: pd.DataFrame, key: str, index: Optional[int] = None) -> np.array:
    """
    Construct a ndarray of shape (3, N) representing one marker in a 3
    dimensional DataFrame with N number of indices.
    """
    x, y, z = get_3D_df_keys(key)
    if index is None:
        return np.array([df[x], df[y], df[z]])
    else:
        return np.array([df[x][index], df[y][index], df[z][index]])

def get_multi_index(markers: List) -> pd.MultiIndex:
    multi_index_column_names = [[], [], []]
    for marker_id in markers:
        for column_name in ("x", "y", "likelihood"):
            multi_index_column_names[0].append("annotated_markers")
            multi_index_column_names[1].append(marker_id)
            multi_index_column_names[2].append(column_name)
    return pd.MultiIndex.from_arrays(
        multi_index_column_names, names=("scorer", "bodyparts", "coords")
    )

def construct_dlc_output_style_df_from_dictionary(marker_predictions: Dict) -> pd.DataFrame:
    """
    Create a DataFrame from dictionary with DLC-like multi-index.

    Parameters
    ----------
    marker_predictions: {str: {str: list of int}}
        Dictionary containing markers as keys and dictionaries as values with
        x, y and z as keys and lists of int as values. The length of the lists
        is equivalent to the number of frames annotated.

    Returns
    -------
    df: pd.DataFrame
        The dataframe with DLC-like multiindex and data from input dictionary.
    """
    multi_index = get_multi_index(
        markers=marker_predictions.keys()
    )
    df = pd.DataFrame(data={}, columns=multi_index)
    for scorer, marker_id, key in df.columns:
        df[(scorer, marker_id, key)] = marker_predictions[
            marker_id
        ][key]
    return df

def load_image(filepath: Path, idx: int = 0) -> np.ndarray:
    iio_reader = iio.get_reader(filepath)
    return np.asarray(iio_reader.get_data(idx))

def load_single_frame_of_video(filepath: Path, frame_idx: int = 0) -> np.ndarray:
    return load_image(filepath=filepath, idx=frame_idx)

def plot_image(
        filepath: Path, idx: int = 0, plot_size: Tuple[int, int] = (9, 6)
) -> None:
    fig = plt.figure(figsize=plot_size, facecolor="white")
    image = load_image(filepath=filepath, idx=idx)
    plt.imshow(image)

def plot_single_frame_of_video(
        filepath: Path, frame_idx: int = 0, plot_size: Tuple[int, int] = (9, 6)
) -> None:
    plot_image(filepath=filepath, idx=frame_idx, plot_size=plot_size)

KEYS_TO_CHECK_PROJECT = [
    "valid_cam_ids",
    "paradigms",
    "animal_lines",
    "led_extraction_type",
    "led_extraction_filepath",
    "max_calibration_frames",
    "max_cpu_cores_to_pool",
    "max_ram_digestible_frames",
    "rapid_aligner_path",
    "use_gpu",
    "load_calibration",
    "calibration_tag",
    "calibration_validation_tag",
    "allowed_num_diverging_frames",
    "handle_synchro_fails",
    "default_offset_ms",
    "start_pattern_match_ms",
    "end_pattern_match_ms",
    "synchro_error_threshold",
    "synchro_marker",
    "led_box_size",
    "use_2D_filter",
    "score_threshold",
    'num_frames_to_pick',
    'triangulation_type'
]
"""
Keys
____
calibration_evaluation_filepath: {str: str}
    Dictionary with keys for all valid cam_ids, defining the filepath to the
    file to use for marker detection for calibration_validation per cam.
calibration_evaluation_type: {str: str}
    Dictionary with keys for all valid cam_ids, defining the type of marker
    detection to use per cam for calibration_evaluation files. Values: DLC, manual
processing_filepath: {str: str}
    Dictionary with keys for all valid cam_ids, defining the filepath to the
    file to use for marker detection for recordings per cam.
processing_type: {str: str}
    Dictionary with keys for all valid cam_ids, defining the type of marker
    detection to use per cam for recordings. Values: DLC, manual, exclude
led_extraction_filepath: {str: str}
    Dictionary with keys for all valid cam_ids, defining the filepath to the
    file to use for marker detection for synchronization per cam.
led_extraction_type: {str: str}
    Dictionary with keys for all valid cam_ids, defining the type of marker
    detection to use per cam for synchronization. Values: DLC, manual
animal_lines: list of str
    List of all animal_lines to search for in recording filenames.
    If the lines are numbers, you need to set the lines as str.
paradigms: list of str
    List of all paradigms to search for in filenames and directories.
valid_cam_ids: list of str
    List of all cam_ids to search for in filenames.
calibration_tag: str
    Filename tag to search for in the calibration files.
    "Calvin" is an invalid value.
calibration_validation_tag: str
    Filename tag to search for in the calibration_validation files.
    "Calvin" is an invalid value.
use_gpu: str, default ""
    Whether to restrict the usage of GPU for DLC analyses. Values: "", "prevent",
    "low", "full"
    "prevent" disables GPU usage
    "low" restricts GPU memory for synchronization
    "full" uses entire GPU capacity, equivalent to ""
intrinsic_calibration_directory: str
    The directory, in which the intrinsic calibration pickle .p files or the
    intrinsic calibration checkerboard videos are stored. Intrinsic calibration
    videos have to have "checkerboard" and a valid cam_id in their filename.
    They have to be recorded in same resolution as the recording/calibration
    videos without cropping, using a 6x6 checkerboard.
load_calibration: bool
    If True, then use this package to set the intrinsics of the cameras instead
    of using the aniposelib function to calibrate intrinsics.
    Requires checkerboard videos or .p pickle files for all cameras in
    intrinsic_calibration_directory.
triangulation_type: str, default "triangulate"
    Specify the method of aniposelib triangulation to use.
    Values: "triangulate", "triangulate_optim_ransac_True",
    "triangulate_optim_ransac_False"
allowed_num_diverging_frames: int
    Specify how many frames a synchronized file can differ from the median of
    all synchronized files before it will be excluded from analysis.
handle_synchro_fails: str, default error
    How to proceed if the first synchronisation try exceeds
    synchro_error_threshold. Values: "repeat", "default", "manual", "error"
    "repeat" run the same method of synchronisation again.
    "default" using default value default_offset_ms as synchro offset.
    "manual" using manual marker detection for synchronisation
    "error" raises an error and breaks the analysis
default_offset_ms: int
    Synchro offset to use if first synchro try fails and handle_synchro_fails is
    "default". In milliseconds.
start_pattern_match_ms: int, default 0
    Start of time range in which to search for matching synchro pattern. In milliseconds.
end_pattern_match_ms: int
    End of time range in which to search for matching synchro pattern. In milliseconds.
synchro_error_threshold: int, default 100
    Below this threshold, a synchro patter alignment error will be considered as
    good synchro, above, as failed synchro.
synchro_marker: str
    The marker to use for synchronisation. Has to be detectable by the
    led_extraction method.
led_box_size: int
    Pixel range around predicted synchro marker position to calculate mean pixel
    intensity for blinking pattern from.
use_2D_filter: bool, default True
    Whether to use filtering on 2D marker detection predictions in recordings.
    At the moment only available for DLC.
score_threshold: float, default 0.9
    Only predictions with likelihood above this threshold will be taken into
    account for triangulation.
num_frames_to_pick: int, default 5
    The number of frames to use to find optimal prediction for the synchro_marker.
max_ram_digestible_frames: int, default 3000
    Maximum number of frames to keep in RAM during writing of synchronised videos.
    Increase to speed up analysis, reduce to adapt to available RAM.
max_cpu_cores_to_pool: int, default 0
    If 0, then no multiprocessing will be used for writing of synchronised videos.
    Set to as many CPU cores, as you would like to use for multiprocessing.
max_calibration_frames: int
    Number of frames to take into account for intrinsic calibration. 300 works
    well, depending on CPU speed, it can be necessary to reduce.
rapid_aligner_path: str, default ""
    If "", then no rapid_aligner (GPU based pattern alignment) will be used.
    Insert path to locally installed clone of the rapid_aligner package to use
    GPU for pattern synchronisation.
"""

KEYS_TO_CHECK_RECORDING = [
    "led_pattern",
    "target_fps",
    "calibration_index",
    "recording_date",
]
"""
led_pattern: dict
    Blinking pattern to use for temporal synchronisation.
target_fps: int
    Fps rate, to which the videos should be synchronized.
calibration_index: int, default 0
    Index of a calibration.
    Together with recording_date, it creates a unique calibration key.
recording_date: str
    Date at which the calibration was done.
"""

# ToDo: default values
# fps not necessary if all cams have the same fps
# offsets not necessary if no cropping was performed or use_intrinsic_calibration False
# fisheye not necessary if no camera is fisheye
KEYS_TO_CHECK_CAMERA_RECORDING = ["fps", "offset_row_idx", "offset_col_idx",
                                  "flip_h", "flip_v", "fisheye"]
"""
fps: int
    The framerate of the camera.
offset_row_idx: int, default 0
    If cropping was performed, specify the row or y index initial here.
offset_col_idx: int, default 0
    If cropping was performed, specify the col or x index initial here.
flip_h: bool, default False
    Based on the size from uncropped intrinsic calibration videos, the row or y
    index end is calculated and used instead of offset_row_idx.
flip_v: bool, default False
    Based on the size from uncropped intrinsic calibration videos, the col or x
    index end is calculated and used instead of offset_col_idx.
fisheye: bool, default False
    If a fisheye lens was used, set True.
"""

KEYS_TO_CHECK_CAMERA_PROJECT = [
    "processing_type",
    "calibration_evaluation_type",
    "processing_filepath",
    "calibration_evaluation_filepath",
    "led_extraction_type",
    "led_extraction_filepath",
]

STANDARD_ATTRIBUTES_TRIANGULATION = ["all_cameras",
                                     "markers_excluded_manually",
                                     "calibration_toml_filepath",
                                     "csv_output_filepath",
                                     "markers",
                                     "triangulation_dlc_cams_filepaths",
                                     "project_config_filepath",
                                     "output_directory",
                                     "normalised_dataframe",
                                     "anipose_io",
                                     "video_plotting_config",
                                     "rotated_filepath",
                                     "rotation_error",
                                     "synchronization_individuals",
                                     "led_detection_individuals",
                                     "ground_truth_config",
                                     "triangulation_visualization",
                                     "video_start_s",
                                     "excluded_videos"]

STANDARD_ATTRIBUTES_CALIBRATION = ["camera_group",
                                   "report_filepath",
                                   "reprojerr",
                                   "valid_videos",
                                   "synchronized_charuco_videofiles",
                                   "camera_objects",
                                   "synchronization_individuals",
                                   "led_detection_individuals",
                                   "project_config_filepath",
                                   "output_directory",
                                   "excluded_videos"]

SYNCHRO_METADATA_KEYS = ["handle_synchro_fails",
                         "default_offset_ms",
                         "start_pattern_match_ms",
                         "end_pattern_match_ms",
                         "synchro_error_threshold",
                         "synchro_marker",
                         "use_2D_filter",
                         'num_frames_to_pick',
                         "rapid_aligner_path",
                         "use_gpu",
                         "led_box_size"]

KEYS_PER_CAM_PROJECT = ["processing_type", "calibration_evaluation_type", "processing_filepath",
                    "calibration_evaluation_filepath", "led_extraction_type", "led_extraction_filepath"]

KEYS_VIDEOMETADATA_PROJECT = ["valid_cam_ids", "paradigms", "load_calibration", "max_calibration_frames",
                          "max_ram_digestible_frames", "max_cpu_cores_to_pool", "animal_lines"]

# ToDo
# led pattern not necessary if no synchronisation necessary
KEYS_VIDEOMETADATA_RECORDING = ["led_pattern", "target_fps", "calibration_index"]


#操作excel的py文件

import xlrd
from xlutils.copy import copy  #把内容写入excel中，追加模式

class OperaExcel:

    def __init__(self,file_path=None,i=None):
        if file_path == None:
            self.file_path = "F:\AppiumPython_my\config\case.xls"
        else:
            self.file_path=file_path

        if i ==None:
            i=0

        #初始化下面这两个，让他进来就拿到是哪个sheets
        self.excel = self.get_excel()
        self.data = self.get_sheets(i)

    def get_excel(self):
        '''
        获取excel

        '''
        excel = xlrd.open_workbook(self.file_path)
        return excel

    def get_sheets(self,i):
        '''
        获取sheets的内容

        '''
        tables = self.excel.sheets()[i]
        return tables

    def get_lines(self):
        '''
        获取excel行数

        '''
        lines = self.data.nrows
        return lines

    def get_cell(self,row,cell):
        '''
        获取单元格的内容

        '''
        data = self.data.cell(row,cell).value
        return data

    def write_value(self,row,value):
        '''
        将用例的执行结果写入excel中，比如pass，fail

        '''
        read_value = self.excel #打开想要更改的excel文件
        write_data = copy(read_value) #将操作文件对象拷贝，变成可写的workbook对象

        write_save = write_data.get_sheet(0) #获得第一个sheet的对象 注意：get_sheet()是xlutils.copy自带的方法

        write_save.write(row,8,value) #写入数据
        write_data.save(self.file_path) #保存,注意excel后缀名必须是xls的

# if __name__ == "__main__":
    # opera = OperaExcel()
    # print(opera.get_lines())
    # print(opera.get_cell(7,3))
    # print(opera.write_value(6, 'zlb'))


"""add post column

Revision ID: 9d19aeb7a7f3
Revises: 6514f5b66818
Create Date: 2017-02-26 10:50:32.863887

"""
from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = '9d19aeb7a7f3'
down_revision = '6514f5b66818'
branch_labels = None
depends_on = None

def upgrade():
    op.add_column('posts', sa.Column('category_id', sa.Integer, nullable=False))

def downgrade():
    op.drop_column('posts', 'category_id')


#!/usr/bin/env python
# encoding: utf-8

"""
@description: 二分类问题

@version: 1.0
@author: BaoQiang
@license: Apache Licence
@contact: mailbaoqiang@gmail.com
@site: http://www.github.com/githubao
@software: PyCharm
@file: two_part.py
@time: 2016/11/4 23:01
"""

from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.linear_model.logistic import LogisticRegression

X = []
X.append("fuck you")
X.append("fuck you all")
X.append("hello everyone")

X.append("fuck me")
X.append("hello boy")

Y = [1, 1, 0]

def main():
    vectorizer = TfidfVectorizer()
    x_train = vectorizer.fit_transform(X[:-2])
    x_test = vectorizer.transform(X[-2:])

    classifier = LogisticRegression()
    classifier.fit(x_train, Y)

    predictions = classifier.predict(x_test)
    print(predictions)

if __name__ == '__main__':
    main()


from googlesearch import search
import os
import time
from bs4 import BeautifulSoup
import re
import requests
import warnings
import yfinance as yf

import langchain
from langchain import PromptTemplate
from langchain.llms import OpenAI
from langchain.chat_models import ChatOpenAI
from langchain.agents import load_tools, AgentType, Tool, initialize_agent
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain.chains.summarize import load_summarize_chain

# Streamlit
import streamlit as st

OPENAI_API_KEY = os.environ.get('OPEN_API_KEY')
warnings.filterwarnings("ignore")

# load LLM funciton
def load_llm():
    llm = ChatOpenAI(temperature=.25, openai_api_key=OPENAI_API_KEY )
    return llm

# check openAI key
def get_openai_api_key():
    input_text = st.text_input(label="OpenAI API Key (or set it as .env variable)",  placeholder="Ex: sk-2twmA8tfCb8un4...", key="openai_api_key_input")
    return input_text

# Start streamlit
st.set_page_config(page_title="Stock Analysis", page_icon="ЁЯдС")

# Start top information
st.header("GPT Stock Analysis ЁЯдС")

st.markdown("You want to save time analyse a stock? This tool is meant to help you decide whether to buy a stock or not.\
            \n\nThis tool is powered by [BeautifulSoup](https://beautiful-soup-4.readthedocs.io/en/latest/#), [markdownify](https://pypi.org/project/markdownify/), [LangChain](https://langchain.com/), and [OpenAI](https://openai.com)" \
            )

# Collect information about the person you want to research
ticker = st.text_input(label="Ticker",  placeholder="Ex: DIS", key="ticker")

# Fetch stock data from Yahoo Finance
def get_stock_price(ticker,history=5):
    '''
      input:
        - ticker
        - history: how many days of stock data to look at.
      output:
        - close price for the stock -> (str)
    '''
    # Avoid rate limit error
    # time.sleep(4)
    stock = yf.Ticker(ticker)
    df = stock.history(period="1y")
    df=df[["Close"]]
    # Get the date
    df.index=[str(x).split()[0] for x in list(df.index)]
    df.index.rename("Date",inplace=True)
    df.reset_index(inplace=True)
    df = df[-history:]
    return df

def get_stock_str(df):
    df_str = df.to_string(index=False, header=False, col_space=None).split('\n')
    # Adding a comma in between each value of list
    res = [': '.join(ele.split()) for ele in df_str]
    df_str = ','.join(res)
    return df_str

# Script to scrap top5 google news for given company name
def get_recent_stock_news(ticker):
    # time.sleep(4) #To avoid rate limit error
    texts = []
    search_term = ticker + ' news'
    # get links
    links = search(search_term, num_results=5, lang='en')
    headers={'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Safari/537.36'}

    # Get content into a texts list
    for link in links:
      #print(f"{result}")
      page = requests.get(link, headers=headers).text
      soup = BeautifulSoup(page, 'html.parser')

      # remove all script and style elements
      for script in soup(["script", "style"]):
          script.extract()

      page_text = soup.getText()
      #print(page_text)

      # break into lines and remove leading and trailing space on each
      lines = (line.strip() for line in page_text.splitlines())

      # break multi-headlines into a line each
      chunks = (phrase.strip() for line in lines for phrase in line.split("  "))

      # drop blank lines
      page_text = ' '.join(chunk for chunk in chunks if chunk)

      #print(page_text)
      page_text = page_text.replace(u'\xa0', ' ')

      texts.append(page_text)
      pass

    # make list into string
    news = "Top 5 recent news:\n"
    for i,n in enumerate(texts):
        news += f"News number {i}: {n}\n"

    return news
# Fetch financial statements from Yahoo Finance
def get_financial_statements(ticker):
    # time.sleep(4) #To avoid rate limit error
    company = yf.Ticker(ticker)

    # get revenue, income, eps from latest quarterly income statement
    income_stmt_table = company.quarterly_income_stmt.loc[['Total Revenue','Net Income','Diluted EPS']]
    income_stmt_table = income_stmt_table.iloc[:,0]
    total_rev = income_stmt_table['Total Revenue']
    net_income = income_stmt_table['Net Income']
    diluted_eps = income_stmt_table['Diluted EPS']

    # Get ROE and profit margin from latest quarterly balance sheet
    balance_stmt_table = company.quarterly_balance_sheet.loc['Stockholders Equity']
    st_equity = balance_stmt_table[0]
    roe = round(net_income/st_equity,3)
    profit_margin = round(net_income/total_rev *100,3)

    # return a short summery
    fin_stat = f"Total revenue:{total_rev} \n Net income:{net_income}\n Diluted EPS:{diluted_eps}\n ROE:{roe}\n Profit margin:{profit_margin}%"
    return fin_stat, profit_margin, total_rev, net_income, diluted_eps, roe

# Function to change long text about a topic into document
def split_news(info):
    # make splitter
    text_splitter = RecursiveCharacterTextSplitter(chunk_size=4000, chunk_overlap=50)

    # split info into docs
    spt_news  = text_splitter.create_documents([info])

    return spt_news

# Custom stock prompt
map_prompt = """You are a helpful financial expert that aids a user in stock investment.
Below is information about a stock {ticker}.
Information will include news about {ticker}
Your goal is to summerize the information below about {ticker} stock.

% START OF INFORMATION ABOUT {ticker}:
{text}
% END OF INFORMATION ABOUT {ticker}

Please respond with a summary based on the information above.

YOUR RESPONSE:"""

map_prompt_template = PromptTemplate(template=map_prompt, input_variables=["text", "ticker"])

# combine all document summaries.
combine_prompt = """
You are a helpful financial expert that aids a user in stock investment.\
Below is information about a stock {ticker}.\
Information will include news, stock price, and financial statistics about the stock.\

Generate a summary of the following information that includes the following elements:
* The company name and ticker of the stock.
* Three pros and three cons of investing the stock in bullet points.
* Advice for user to invest the stock or not.
The user is fully aware about the investment risk, dont include any kind of warning like 'It is recommended to conduct further research and analysis or consult with a financial advisor before making an investment decision' in the answer

% START OF INFORMATION ABOUT {ticker}:
NEWS: {text}\
{ticker}'s LATEST STOCK PRICE: {stock_price}\
{ticker}'s FINANCIAL STATISTICS: {fin_stat}
% END OF INFORMATION ABOUT {ticker}

YOUR RESPONSE:"""
combine_prompt_template = PromptTemplate(template=combine_prompt, input_variables=["text", "ticker",  "stock_price", "fin_stat"])

button_ind = st.button("*Generate Advice*", type='secondary', help="Click to generate stock analysis")

# Checking to see if the button_ind is true. If so, this means the button was clicked and we should process the links
if button_ind:
    if not ticker:
        st.warning('Please provide ticker', icon="тЪая╕П")
        st.stop()

    if not OPENAI_API_KEY:
        st.warning('Please insert OpenAI API Key. Instructions [here](https://help.openai.com/en/articles/4936850-where-do-i-find-my-secret-api-key)', icon="тЪая╕П")
        st.stop()

    if OPENAI_API_KEY == 'YourAPIKeyIfNotSet':
        # If the openai key isn't set in the env, put a text box out there
        OPENAI_API_KEY = get_openai_api_key()

    # Go get your data
    stock_df = get_stock_price(ticker)
    stock_price = get_stock_str(stock_df)
    st.markdown(f"#### Stock price: ")
    st.line_chart(stock_df, x='Date', y='Close')

    stats, profit_margin, total_rev, net_income, diluted_eps, roe =  get_financial_statements(ticker)
    st.markdown(f"#### Stock Stats: ")
    st.markdown(f"##### Profit margin: ")
    st.write(profit_margin)
    st.markdown(f"##### Diluted EPS: ")
    st.write(diluted_eps)
    st.markdown(f"##### Net income: ")
    st.write(net_income)
    st.markdown(f"##### Total revenue: ")
    st.write(total_rev)
    st.markdown(f"##### ROE: ")
    st.write(roe)

    st.write('Getting latest news...')
    news = get_recent_stock_news(ticker)
    text = split_news(news)

    # Write sequential chain sumemrize the webpage and combine with other stats

    llm = load_llm()

    chain = load_summarize_chain(llm,
                                 chain_type="map_reduce",
                                 map_prompt=map_prompt_template,
                                 combine_prompt=combine_prompt_template,
                                 # verbose=True
                                 )

    st.write("Generating analysis...")

    #Pass our user information we gathered, the persons name and the response type from the radio button
    output = chain({"input_documents": text, # The seven docs that were created before
                    "ticker": ticker,
                    "fin_stat": stats,
                    "stock_price": stock_price,
                    })

    st.markdown(f"#### GPT Analysis:")
    st.write(output['output_text'])


import cv2
import numpy as np
import os

cap = cv2.VideoCapture('input.avi')

try:
    if not os.path.exists('All Frames'):
        os.makedirs('All Frames')
except OSError:
    print ('Error: Creating directory of data')
currentFrame = 0
while(True):

    ret, frame = cap.read()
    name = './All Frames/frame' + str(currentFrame) + '.jpg'
    print ('Creating...' + name)
    cv2.imwrite(name, frame)
    currentFrame += 1
cap.release()
cv2.destroyAllWindows()


import pandas as pd
import numpy as np

def main():
    iris = pd.read_csv (r'../../data/iris.csv')

    X = iris.iloc[:, 0:4].values
    Y = iris.iloc[:, -1].values

    x_max = X.max(axis=0)
    x_min = X.min(axis=0)
    X = (X - x_min)/(x_max - x_min)

    Xt = np.hstack([X, np.ones((len(X), 1))])

    k = len(np.unique(Y))
    classes = list(np.unique(Y))

    Yt = np.zeros((len(Xt), k))
    for i, y in enumerate(Y):
        idx = classes.index(y)
        Yt[i][idx] = 1

    return regression(Xt, Yt, k)

def regression(Xt, Yt, k):
    _, d = Xt.shape

    alpha = 1e-5
    W = np.random.rand(k, d)

    S = softmax(np.matmul(Xt, W.T))
    erro = S - Yt
    grad = np.matmul(erro.T, Xt)
    norm = np.linalg.norm(grad.flatten())

    f = open("output_fixed_alpha.txt","w+")
    it = 0
    while norm > 1e-5 and it <= 1000:
        S = softmax(np.matmul(Xt, W.T))
        erro = S - Yt
        grad = np.matmul(erro.T, Xt)
        norm  = np.linalg.norm(grad.flatten())

        W = W - alpha * grad

        loss = cross_entropy(Yt, S)

        f.write(f"it: {it}, grad_norm: {norm}, cross_entropy: {loss}\n")
        it += 1

def softmax(S):
    expY = np.exp(S)
    return  expY / np.sum(expY, axis=1).reshape(S.shape[0], 1)

def cross_entropy(Yt, S):
    return -np.sum(np.multiply(Yt, np.log(S)))

main()


import os
import shutil

import pandas as pd
from openpyxl import Workbook, load_workbook
from openpyxl.utils import get_column_letter
from openpyxl.worksheet.table import Table

def create_directory(dir_name):
    """
    Creates a directory in the same parent directory with a given name.

    Parameters:
    dir_name (str): The name of the directory to create.
                    Default is "xlsx" for the converted Excel files.
    """
    if not os.path.exists(dir_name):
        os.makedirs(dir_name)
    else:
        shutil.rmtree(dir_name)
        os.makedirs(dir_name)

def create_sheet(output_workbook, sheet_name):
    """
    Creates a sheet in the Excel workbook with a given name

    Parameters:
    outbook_workbook (Workbook): The workbook where the sheet will be created.
    sheet_name (str): The name of the sheet to be created.

    Returns:
    Worksheet: Created sheet
    """
    output_sheet = output_workbook.create_sheet(sheet_name)
    return output_sheet

def convert_xls_to_xlsx(input_folder, output_folder):
    """
    Converts all .xls files in a directory, outputs
    .xlsx to another directory, and returns a list
    of the output file names.

    Parameters:
    input_folder (str): File path of directory containing .xls files.
    output_folder (str): File path of directory where .xlsx files are created.

    Returns:
    list: The list of .xlsx file names created

    Raises:
    ValueError: If there are no service files found in the directory.
    """
    xls_files = os.listdir(input_folder)
    xls_files = [file for file in xls_files
                 if file.startswith("Service_")
                 and file.endswith(".xls")]
    if not xls_files:
        raise ValueError(f"No service files found in the following directory: {input_folder}")

    for xls_file in xls_files:
        input_path = os.path.join(input_folder, xls_file)
        output_path = os.path.join(output_folder, xls_file.replace(".xls", ".xlsx"))

        # Copy over data into the new .xlsx file
        df_xls = pd.read_excel(input_path)

        wb_xlsx = Workbook()
        sheet_xlsx = wb_xlsx.active

        for row in df_xls.iterrows():
            sheet_xlsx.append(row[1].tolist())

        wb_xlsx.save(output_path)

    service_files = [file for file in os.listdir(output_folder)]

    return service_files

def extract_cell(file_path, cell_reference):
    """
    Returns data from a cell in the given file found
    through the input file path.

    Parameters:
    file_path (str): Relative path to the .xlsx input file.
    cell_reference (str): The location of the cell to extract.

    Returns:
    Various: The data from the cell extracted.
    """
    try:
        workbook = load_workbook(file_path)
        sheet = workbook.active
        if cell_reference == None:
            return None
        return sheet[cell_reference].value
    except Exception as e:
        print(f"Error: {e}")

def auto_size_columns(worksheet):
    for column_cells in worksheet.columns:
        max_length = 0
        for cell in column_cells:
            if cell.value is not None:
                max_length = max(max_length, len(str(cell.value)))
        adjusted_width = (max_length + 2)  # Add a little extra padding
        column_letter = get_column_letter(cell.column)
        worksheet.column_dimensions[column_letter].width = adjusted_width

def duplicate_excel_file(source_path, destination_path):
    try:
        # Copy the source Excel file to the destination with a new name
        shutil.copy(source_path, destination_path)
        print(f"File duplicated as: {destination_path}")
    except Exception as e:
        print(f"Error: {e}")

def set_list_of_pivot_tables_refresh_on_load(workbook_path):
    workbook = load_workbook(workbook_path)
    for sheet in workbook:
        for pivot in sheet._pivots:
            pivot.cache.refreshOnLoad = True
    workbook.save(workbook_path)

def get_cell_reference(row, column):
    column_letter = get_column_letter(column)
    return f"{column_letter}{row}"

def get_sheet_dimensions(workbook_path, sheet_name):
    workbook = load_workbook(workbook_path)
    sheet = workbook[sheet_name]
    return (sheet.max_row, sheet.max_column)


def name():
    dsc = {}
    while True:
        name = input('Digite um nome: ')
        if name == '':
            break
        else:
            if name in dsc:
                dsc[name] += 1
            else:
                dsc[name] = 1
    for name in dsc:
        print('Há {} alunos chamados de {}.'.format(dsc[name],name))


import numpy as np
import time
import random
import itertools

# import os

print("")

def init_board(row=10, col=10):
    board = np.zeros((row, col), dtype=int)
    # print([random.randint(0, 9) for i in range(10)])
    rand_row = [random.randint(0, row - 1) for i in range(5)]
    rand_col = [random.randint(0, col - 1) for i in range(5)]
    for i, j in itertools.product(rand_row, rand_col):
        board[i][j] = 1
    # print(i, j)
    # board[5][5] = 1
    # board[4][5] = 1
    # board[6][5] = 1

    return board

def show_life(board):
    row, col = board.shape
    # for i in range(row):
    #     print(i, end="")
    # print("")
    for i in range(row):
        for j in range(col):
            if board[i][j] == 1:
                print("@", end="")
            else:
                print("+", end="")
        print(i)
    time.sleep(1)
    print("\033[11F")

def count_around(board, i, j):
    count = (
        board[i][j + 1]
        + board[i][j - 1]
        + board[i + 1][j]
        + board[i - 1][j]
        + board[i + 1][j + 1]
        + board[i + 1][j - 1]
        + board[i - 1][j + 1]
        + board[i - 1][j - 1]
    )
    # print("count is {}".format(count))
    return count

def next_age(board):
    row, col = board.shape
    next_board = np.zeros((row, col), dtype=int)
    for i in range(1, row - 1):
        for j in range(1, col - 1):
            around = count_around(board, i, j)
            # 誕生：死んでいるセルの隣接セルが生==3の時
            """
            if (
                board[i][j] == 0
                and board[i][j + 1]
                + board[i][j - 1]
                + board[i + 1][j]
                + board[i - 1][j]
                + board[i + 1][j + 1]
                + board[i + 1][j - 1]
                + board[i - 1][j + 1]
                + board[i - 1][j - 1]
                == 3
            ):
            """
            if board[i][j] == 0:
                if around == 3:
                    next_board[i][j] = 1
            else:
                if around >= 4:
                    next_board[i][j] = 0
                elif around <= 1:
                    next_board[i][j] = 0
                else:
                    next_board[i][j] = 1
    # print(next_board)
    return next_board

# board = np.empty((ROW,COL))
# board = np.zeros((ROW,COL), dtype=bool)
# print(board[0][0])
# board[0][0] = True
# board[1][0] = True
board = init_board()
# show_life(board)
# board = next_age(board)
# show_life(board)
# board = next_age(board)
# show_life(board)
for i in range(20):
    board = next_age(board)
    show_life(board)
# print(board.shape)
# print(board)


# A wrapper for integrating with the tarot 18 simulation. Only simulates a single step according to some current passed. Output voltages are saved in the filename given.

from .battery_electrochem_TarotT18 import BatteryElectroChem as Battery

class TarotBattery():
    def __init__(self, timeStep, filename):
        self.battery = Battery()
        # initialize to tarot. The zeros mean nothing. See line 233 of battery_electromchem_TarotT18 file
        self.x = self.battery.initialize(0, 0)
        self.t = 0
        self.timeStep = timeStep
        self.f = open(filename, 'w')

    # Step only a single timestep.
    def step(self, currentNeeded):
        self.t += self.timeStep
        self.x = self.battery.next_state(self.x, currentNeeded, self.timeStep)
        output = self.battery.output(self.x)['v']
        self.f.write(str(output) + "\n")
        return output


# boj 9663 n퀸 백트래킹

# row = [0,1,2,3] 이라 하면 4x4 체스판에
# 0행0열 1행1열 2행2열 3행3열에 퀸 배치한 것
import sys

def check(x) :
    for i in range(x) : # x행 i열에 놔도 될까요?
        if row[x] == row[i] :
            return False # 같은 열이라 안되
        if abs(row[x] - row[i]) == x - i :
            return False # 대각선이라 안대 (가로 세로 길이 같음)
    return True # 놔두대

def dfs(x) :
    global answer
    if x == N :
        answer += 1 # 체스판 완성될 때 마다 1 더해주기
    else :
        for i in range(N) :
            row[x] = i # 일단 놔버리기
            if check(x) : # 근데 놔도 되나 체크해보기
                dfs(x+1) # 놔도 되면 다음행 놓은채로 다음행 ㄱㄱ

N = int(sys.stdin.readline())

row = [0] * N
answer = 0
dfs(0)

print(answer)

Python_Algorithm/DP, 재귀, 백트래킹/boj 9663 백트래킹 dfs 엔퀸.py


# -*- coding: utf-8 -*-
"""
Created on Sat Dec 11 18:07:42 2021

@author: kshah
"""

####### DIGIT RECOGNIZER DATA SET - FORM DIGITS ################
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import math
from sklearn.metrics import confusion_matrix, ConfusionMatrixDisplay, classification_report
from sklearn.metrics import accuracy_score, f1_score, precision_score, recall_score

          # library for creating visualizations
#################################################################
from sklearn.feature_selection import f_classif
from sklearn.naive_bayes import GaussianNB
from sklearn.svm import SVC

df = pd.read_csv('trainfeatures42k.csv')

class_label = 'Label'
features = df.columns.to_list()
features.remove(class_label)

######### Data Processing ##################################################
def mahalanobis_dist(X: np.ndarray, mean: np.ndarray, cov: np.ndarray):
   """ Calculate the Mahalanobs distance for an array of n samples and m dimensions."""
   n = X.shape[0]
   X_c = X - mean
   X_cov_inv = np.linalg.inv(cov)
   left = np.matmul(X_c, X_cov_inv)
   # Below we calculate X_c*X_cov_inv*X_c'
   # size n x n
   dist_squared_matrix = np.matmul(left, X_c.T)
   diag = np.diagonal(dist_squared_matrix)
   return diag
 ##############################################################################

################## Finding Outliers using Mahanobis distance ######################
def find_outliers(X: np.ndarray, p:int) -> np.ndarray:
    X_mean = np.mean(X, axis=0)
    X_cov  = np.cov(X, rowvar = False)
    dist_squared = mahalanobis_dist(X,X_mean,X_cov)
    idx = np.flip(np.argsort(dist_squared)).tolist()
    dist_sorted = np.take(dist_squared, idx).tolist()
    return dist_sorted[0:p], idx[0:p]

def normalize(X: np.ndarray, a:float, b: float):
    min = X.min(axis = 0).T
    max = X.max(axis = 0).T
    X_norm = ((X - min)/(max - min))* (b-a)+a
    return X_norm

def fishers_LDR (labels,X:np.ndarray):
    classes = np.unique(labels)
    k = len(classes)
    m = X.shape[1]

    #Calculate standard deviation per class per feature

    std = np.zeros([k,m])
    for cl in classes:
        std[cl] = np.std(X[np.where(labels[:]==cl)[0],:], axis = 0)
    total_std = np.sum(std, axis =0)
    mean = np.zeros([k,m])
    W_temp = np.zeros([m,0])

    for cl in classes:
        mean[cl] = np.mean(X[np.where(labels[:] == cl)[0],:], axis = 0)
        mean_div = np.divide(mean[cl],total_std).reshape(-1,1)
        W_temp = np.concatenate((W_temp , mean_div), axis = 1)

    ind_temp = np.empty([m,0],dtype = int)
    cor_temp = np.empty([m,0])

    for j in range(0,k):
        WW_aux = np.concatenate((W_temp[:,0:j], W_temp[:, j+1:k]),axis = 1)
        WW_cur = np.repeat(W_temp[:,j].reshape(-1,1),k-1, axis =1)
        WW = (np.subtract(WW_cur,WW_aux))
        rankW = np.min(WW, axis = -1).reshape(-1,1)
        rankW_abs = -1 *np.abs(rankW.T)
        ind_temp = np.concatenate((ind_temp,np.argsort(rankW_abs).T), axis = 1)
        cor_temp = np.concatenate((cor_temp,np.sort(rankW_abs).T),axis = 1)

    ind_temp = ind_temp.T.flatten('F').reshape(-1,1).T
    cor_temp = cor_temp.T.flatten('F').reshape(-1,1).T

    cor_sort = np.sort(cor_temp)
    index = np.argsort(cor_temp)
    index_sort = ind_temp.take(index).reshape(1,-1)

    ind_temp2 = np.fliplr(index_sort)
    cor_temp2 = np.fliplr(cor_sort)

    u, v = np.unique(ind_temp2, return_index = True)
    w = np.sort(v).reshape(-1,1)
    s = np.argsort(v).reshape(-1,1)
    values = np.fliplr((cor_temp2.take(w)).T)
    rank_index = np.fliplr((u.take(s)).T)
    return values.flatten(), rank_index.flatten()

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

# find possible outliers per class (digit)
n_to_remove = 2 # number of outliers to remove (p in find_outliers)
outliers_dist = []
outliers_idx = []
for cl in  range(10):
    class_data = df.loc [df[class_label] == cl]
    #Xclass =  class_data.to_numpy()[:,1:]
    class_dist, class_idx = find_outliers(class_data.to_numpy()[:,1:], n_to_remove) # drop label and index columns

    df_idx = class_data.iloc[class_idx].index.tolist()
    outliers_dist = outliers_dist + class_dist
    outliers_idx  = outliers_idx + df_idx

outliers_df = df.iloc[outliers_idx].copy().drop(features, axis = 1)
outliers_df['mahalanobis']  = outliers_dist

# print the list of possible outliers and their mahalanobis distance
print(outliers_df)
# remove outliers
df.drop(outliers_idx, inplace = True)

X = df.iloc[:, 1:61].to_numpy()
X = normalize(X, 0,1)
labels = df[class_label].to_numpy(dtype = int).reshape(-1,1)
norm_data_with_labels = np.hstack((labels,X))
df_norm = pd.DataFrame(norm_data_with_labels, columns = df.columns.to_list())

vals, ranks_idx = fishers_LDR(labels,X)
ranked_features = np.array(features).take(ranks_idx)
print("The list of ranked features is : ", ranked_features)

###################################################################################################
########### K-Fold Cross Validation #########################################################

def kfold_cross_validation(num_data: int, num_folds: int, shuffle: bool):

    if shuffle :
        data = np.random.permutation(num_data)
    else:
        data = np.arange(num_data)

    test_data_size = math.ceil(num_data/num_folds)
    total_size = num_folds*test_data_size

    part = np.zeros((total_size,))
    part = range(num_data)
   # fill in the missing data with random samples
    if (total_size < num_data):
        part[total_size:num_data] = np.random.sample(range(1, num_data),total_size-num_data)

   #partition data for each fold . this will be the base for our test data
    part = np.reshape(part,(num_folds,test_data_size))

    test = []
    training = []
    for i in range(num_folds):
        test.append(data[part[i]])
        #Get the indices that are not part of the test data partition which makes the training data
        indices = part[[j for j in range (num_folds) if j != i]].flatten()
        training.append(data[indices])
    return test, training

# Run the  K-fold cross validation
num_folds = 4
# Get the test and training data
test,training = kfold_cross_validation(X.shape[0], num_folds, True)

  # Prepare Training and test sets
X_train = []
X_test = []
y_train = []
y_actual = []

top_features = ranked_features[0:30]  #Change to increase/reduce the number of features

for i in range(num_folds):
    X_train.append(df_norm.loc[training[i],top_features].to_numpy())
    X_test.append(df_norm.loc[test[i],top_features].to_numpy())
    y_train.append(df_norm.loc[training[i],[class_label]].to_numpy(dtype=int).ravel())
    y_actual.append(df_norm.loc[test[i],[class_label]].to_numpy(dtype=int).ravel())

print('length of X_train', len(X_train[:][0]))
print('length of X_test', len(X_test[:][0]))
print('length of y_train', len(y_train[:][0]))
print('length of y_actual', len(y_actual[:][0]))

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

#######  CLASSIFIERS RUN on the Processed Data

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

class GBayesClassifier:

    def __init__(self):
        self.train_prob_membership = None
        self.train_variances = None
        self.train_means = None
        self.num_features = None
        self.num_classes = None

    def train(self, x , y):
        classes = np.unique(y)

        self.num_classes = len(classes)
        self.num_features = x.shape[1]
        self.train_prob_membership = np.zeros(self.num_classes)
        self.train_variances = np.zeros((self.num_classes,self.num_features))
        self.train_means = np.zeros((self.num_classes,self.num_features))

        for idx, cls in enumerate(classes):
            self.train_means[idx] = x[np.where(y == cls)].mean(axis=0)
            self.train_variances[idx] = x[np.where(y == cls)].var(axis=0)
            self.train_prob_membership[idx] = len(np.where(y == cls)[0]) / len(y)

    def predict_prob(self,x):

       num_obs, num_feats = x.shape

       probs = np.zeros((num_obs, self.num_classes))
       for obs in range(num_obs):
           for cls in range(self.num_classes):
               p = self.train_prob_membership[cls]
               t1 = 1 / np.sqrt(2 * np.pi * self.train_variances[cls])
               t2 = (x[obs] - self.train_means[cls]) ** 2 / self.train_variances[cls]
               t3 = np.exp(-0.5 * t2)
               t4 = (t1 * t3).prod()
               probs[obs, cls] = t4 *p
           probs[obs,:] = probs[obs, :] / np.sum(probs[obs,:])

       return probs

    def predict (self,x):
        return np.argmax(self.predict_prob(x),axis=1)

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

figure, ax = plt.subplots(nrows = 1, ncols = num_folds, figsize = (25,10))
min_accuracy = 100
bayes_report = {}

for i in range(num_folds):

    gbc = GBayesClassifier()
    gbc.train(X_train[i], y_train[i])
    y_pred = gbc.predict(X_test[i])

    # gnb = GaussianNB()               # This is sklearn in-built Gaussian NAive Bayes Classifier
    # gnb.fit(X_train[i], y_train[i])
    # y_pred = gnb.predict(X_test[i])

    cm = confusion_matrix(y_actual[i], y_pred)
    disp = ConfusionMatrixDisplay(confusion_matrix =cm)
    #f1_score(y_actual, y_pred, labels=np.unique(y_pred))

    precision = precision_score(y_actual[i], y_pred, average = 'micro',  zero_division=0)
    print('Precision score: {0:0.2f}'.format(precision))

    recall = recall_score(y_actual[i], y_pred,average = 'micro', zero_division=0)
    print('Recall score: {0:0.2f}'.format(recall))

    f1 = f1_score(y_actual[i], y_pred, average = 'micro', zero_division=0)
    print('f1 score: {0:0.2f}'.format(f1))

    accuracy = accuracy_score(y_actual[i], y_pred, normalize = False)
    print('Accuracy: {0:0.2f}'.format(accuracy/100))
    #accuracy = (len(y_pred[y_pred == y_actual[i]])/len(y_actual[i])) *100
    #Keep the Classification report for the worst fold
    if (accuracy < min_accuracy):
        min_accuracy = accuracy
        bayes_report = classification_report(y_actual[i], y_pred, output_dict = True)
        print(bayes_report)
    ax[i].set_title("Custom, Fold" + str(i) + " Accuracy: "  +str(accuracy/100))
    disp.plot(ax = ax[i],colorbar = False)

###############################################################################
#         Support Vector Machine                                              #
###############################################################################
figure, ax = plt.subplots(nrows = 1, ncols = num_folds, figsize = (25,10))
min_accuracy = 100
bayes_report = {}

for i in range(num_folds):

   SVM = SVC(kernel='linear', random_state = 234)
   SVM.fit(X_train[i],y_train[i])
   y_pred = SVM.predict(X_test[i])

   cm = confusion_matrix(y_actual[i], y_pred)
   disp = ConfusionMatrixDisplay(confusion_matrix =cm)
    #f1_score(y_actual, y_pred, labels=np.unique(y_pred))

   precision = precision_score(y_actual[i], y_pred, average = 'micro',  zero_division=0)
   print('Precision score: {0:0.2f}'.format(precision))

   recall = recall_score(y_actual[i], y_pred,average = 'micro', zero_division=0)
   print('Recall score: {0:0.2f}'.format(recall))

   f1 = f1_score(y_actual[i], y_pred, average = 'micro', zero_division=0)
   print('f1 score: {0:0.2f}'.format(f1))

   accuracy = accuracy_score(y_actual[i], y_pred, normalize = False)
   print('Accuracy: {0:0.2f}'.format(accuracy/100))
    #accuracy = (len(y_pred[y_pred == y_actual[i]])/len(y_actual[i])) *100
    #Keep the Classification report for the worst fold
   if (accuracy < min_accuracy):
       min_accuracy = accuracy
       bayes_report = classification_report(y_actual[i], y_pred, output_dict = True)
       print(bayes_report)
   ax[i].set_title("Custom, Fold" + str(i) + " Accuracy: "  +str(accuracy/100))
   disp.plot(ax = ax[i],colorbar = False)


import pandas as pd
from Bio import SeqIO
import glob
from itertools import combinations, product
from collections import defaultdict
import numpy as np
import os

## Collect parameters
project_dir = ""
input_contig_dir = ""
contig_dir = ""
contig_extension = ""
output_prefix = ""
pop_infile_name = ""
ref_iso = ""
ref_contig = ""
focus_population = ""
len_block_threshold = 0
gap_prop_thresh = 0.0
window_size = 0
overlap = 0
MUGSY_source = ""
phyML_loc = ""
phyML_properties = ""
ape_loc = ""
percentile_threshold = 0.0
min_physplit_window_size = 0

parameter_file = open("phybreak_parameters.txt","r")
for line in parameter_file:
    line = line.strip().split(" = ")
    if len(line) > 1:
        if line[0] == "project_dir":
            project_dir = line[1].split(" #")[0]
        elif line[0] == "input_contig_dir":
            input_contig_dir = line[1].split(" #")[0]
        elif line[0] == "contig_dir":
            contig_dir = line[1].split(" #")[0]
        elif line[0] == "input_contig_extension":
            contig_extension = line[1].split(" #")[0]
        elif line[0] == "output_prefix":
            output_prefix = line[1].split(" #")[0]
        elif line[0] == "pop_infile_name":
            pop_infile_name = line[1].split(" #")[0]
        elif line[0] == "ref_iso":
            ref_iso = line[1].split(" #")[0]
        elif line[0] == "ref_contig":
            ref_contig = line[1].split(" #")[0]
        elif line[0] == "focus_population":
            focus_population = line[1].split(" #")[0]
        elif line[0] == "len_block_threshold":
            len_block_threshold = int(line[1].split(" #")[0])
        elif line[0] == "gap_prop_thresh":
            gap_prop_thresh = float(line[1].split(" #")[0])
        elif line[0] == "window_size":
            window_size = int(line[1].split(" #")[0])
        elif line[0] == "window_overlap":
            overlap = int(line[1].split(" #")[0])
        elif line[0] == "MUGSY_source":
            MUGSY_source = line[1].split(" #")[0]
        elif line[0] == "phyML_loc":
            phyML_loc = line[1].split(" #")[0]
        elif line[0] == "phyML_properties":
            phyML_properties = line[1].split(" #")[0]
        elif line[0] == "ape_loc":
            ape_loc = line[1].split(" #")[0]
        elif line[0] == "percentile_threshold":
            percentile_threshold = float(line[1].split(" #")[0])
        elif line[0] == "min_physplit_window_size":
            min_physplit_window_size = int(line[1].split(" #")[0])
parameter_file.close()

input_dir = project_dir+"align/"
phy_split_dir = input_dir+"phy_split/"

def count_divs(s1, s2):
    '''
    Given two strings counts the number of differences between them
    '''
    d = 0
    for b1, b2 in zip(s1, s2):
        if b1 != b2:
            d += 1
    return d * 1.0 / len(s1)

def calc_pop_div(pop, s_dict):
    '''
    Given a list of strains and a dictionary of sequences, calculates pi,
    the average nucleotide diversity between pairs of strains
    '''

    # pop is just a list of strains in the population of interest
    # s dict is a dictionary that maps strains/genomes to sequences
    aves = []
    for strain1, strain2 in combinations(pop, 2):
        if strain1 in s_dict.keys() and strain2 in s_dict.keys():
            aves.append(count_divs(s_dict[strain1], s_dict[strain2]))
            L = len(s_dict[strain1])
    return(np.average(aves), L)

def clean_id(clone):
    c = clone.strip().split(',')[0]
    return c

os.system('mkdir -p output')
df = pd.read_table(pop_infile_name, dtype=str)

# Loops over all alignment blocks and puts sequences into a dictionary
seqs = {}
for s in SeqIO.parse('%s/%s.core.fasta'%(input_dir, output_prefix), 'fasta'):
    seqs[s.id] = str(s.seq)

print('Calculating genome-wide diversity.')
# Calculate pi for each population
new_rows = []
for pop, popdf in df.groupby('Cluster_ID'):
    strains = [clean_id(clone) for clone in set(popdf.Clonal_complex)]
    pi, length = calc_pop_div(strains, seqs)
    new_rows.append([pop, pi])
population_pi = pd.DataFrame(new_rows, columns=['Cluster_ID', 'Pi'])
population_pi.to_csv('output/%s_pop_pi.csv'%output_prefix, index=False)
print('Done calculating genome-wide diversity.')

print('Calculating diversity of individual alignments')
# Split alignments of all 100 SNP trees into individual trees
tree_no = 1
new_lines = []
new_rows = []
for line in open('%s/%s.phy'%(phy_split_dir, output_prefix)):
    if line == '\n':
        if len(new_lines) > 0:
            all_seqs = {l.split()[0].strip(): l.split()[-1].strip() for l in new_lines}
            for pop, popdf in df.groupby('Cluster_ID'):
                strains = [clean_id(clone) for clone in set(popdf.Clonal_complex)]
                # Diversity within a population
                intra_pop_div, length = calc_pop_div(strains, all_seqs)
                new_rows.append([tree_no, pop, intra_pop_div, length])
            if tree_no%100 == 0:
                print('Finished calculation for %s alignments'%tree_no)
            tree_no += 1
        new_lines = []
    else:
        new_lines.append(line)

print('Done calculating individual alignment diversity')
intra_df = pd.DataFrame(new_rows,columns=['tree_no', 'Cluster_ID', 'block_pi', 'Length'])
intra_df.to_csv('output/%s_block_pi.csv'%output_prefix, index=False)


import numpy as np
from numpy.polynomial import polynomial as P

def polynomial_truncated_multiplication(polynomial_0, polynomial_1, term_count):
    '''
    multiply two polynomails and truncated the output to desired size

    inputs
    polynomial_0: the original polynomial, stored as numpy array
    polynomial_1: the polynomia to be mergedl, stored as numpy array
    term_count: number of output polynomial terms excluding the constant

    returns
    updated polynomial

'''
    updated_polynomial = P.polymul(polynomial_0, polynomial_1)
    updated_polynomial = updated_polynomial[:term_count + 1]
    return updated_polynomial

def makeJ(j, m):
    '''
    make an array of 1, j, j^2,..., j^m

    inputs
    j: int
       product iterator
    m: int
       number of terms kept

    Returns
    J: array
       [1, j, j^2,..., j^m]
    '''
    return np.power(j,range(m+1))

def generate_u_constants(c, m):
    '''
    make Uis from paper

    inputs
    c: int
       number of terms multiplied
    m: int
       number of terms kept

    Returns
    U: array
       Array containing U's from paper
    '''
    U = makeJ(2, m)
    for i in range(c+1)[3:]:
        U = U
        U = polynomial_truncated_multiplication(U, makeJ(i, m), m)
    return U

if __name__ == "__main__":

    U = generate_u_constants(3, 3)
    print(U)


#!/usr/bin/env python2.7
# -*- coding: utf-8 -*-

""" Create histogram of submission to start times on the CE.

"""

import csv
import time
import datetime
import calendar
import argparse

import numpy as np
import pandas as pd
import matplotlib.dates as mdates
import matplotlib.pyplot as plt

def get_args():
    """ Get command line arguments. """
    parser = argparse.ArgumentParser(
        description='Make time series of HPC accounting file'
    )
    parser.add_argument(
        '-i', '--input-file',
        dest='infile',
        required=True,
        help='Path to HPC accounting file'
    )
    parser.add_argument(
        '-o', '--output-dir',
        dest='outdir',
        required=True,
        help='Path to desired output dir'
    )
    return parser.parse_args()

def distill_input(f):
    """ Clean HPC acct file, return dict. """

    def map_job_name(jobname):
        jobname = str(jobname)
        if jobname.startswith('A_download_job_for_'):
            job_name = 'Downloader'
        elif jobname.startswith('Create_'):
            job_name = 'DbBackupProcess'
        elif jobname == 'Doing_a_denovo_assembly':
            job_name = 'Assembly'
        elif jobname.startswith('Doing_a_reference_mapping_against_'):
            job_name = 'RefMapper'
        elif jobname == 'Finding_alleles':
            job_name = 'AF_AlleleCaller'
        elif jobname == 'Performing_ANI-based_speciation':
            job_name = 'ANI'
        elif jobname == 'Performing_BLAST':
            job_name = 'AB_AlleleCaller'
        elif jobname == 'Performing_contamination_detection':
            job_name = 'Contamination'
        elif jobname == 'Performing_genotyping':
            job_name = 'Genotyper'
        elif jobname == 'Submitting_data_from_BaseSpace_to_NCBI':
            job_name = 'DataFetcher'
        elif jobname.startswith('Step'):
            job_name = 'InternalProcess'
        elif jobname == 'CheckInstall':
            job_name = 'InternalProcess'
        elif jobname == 'simple':
            job_name = 'InternalProcess'
        elif jobname.startswith('arrayjob'):
            job_name = 'InternalProcess'
        else:
            job_name = 'UnknownProcess'
        return job_name

    big_dict = {}
    with open(f, 'r') as fh:
        r = csv.reader(fh, delimiter='\t')
        next(r, None)
        for row in r:
            jobid = row[5]
            jobname = map_job_name(str(row[4]))
            subtime = datetime.datetime.fromtimestamp(int(row[8]))
            starttime = datetime.datetime.fromtimestamp(int(row[9]))
            endtime = datetime.datetime.fromtimestamp(int(row[10]))
            failed = int(row[11])
            exitstat = int(row[12])

            # Remove entries with failed exit status
            if failed != 0 or exitstat != 0:
                continue
            # Remove entries where submission-time > start-time
            elif subtime > starttime:
                continue
            # Remove duplicate jobids
            elif jobid in big_dict.keys():
                big_dict.pop(jobid)
                #print('Duplicate key found, removing both = {}'.format(jobid))
                continue
            else:
                big_dict[jobid] = [subtime, starttime, endtime, jobname]
                # big_dict[jobid] = {'sub': subtime, 'start': starttime, 'end': endtime, 'jobname': jobname}

    return big_dict

def grab_col_names(f):
    with open(f, 'r') as fh:
        r = csv.reader(fh, delimiter='\t')
        col_names = next(r, None)

    return col_names

def to_tuple(list):
    return (*list, )

def plot_sub_to_start(d, outdir, todays_date):
    ''' Plot time series of wait times after job sumbission '''
    job_times = []
    for k, v in d.items():
        sub = v[0]
        start = v[1]
        end = v[2]
        sub2start = start - sub
        job_times.append([sub, sub2start])

    sorted_job_times = sorted(job_times, key=lambda x: x[0])

    job_submit_times = []   # store submission-times only (datetime obj)
    times_to_start = []     # store sub2start-times only (timedelta obj)
    for i in sorted_job_times:
        job_submit_times.append(i[0])
        times_to_start.append(i[1])

    dates = mdates.date2num(job_submit_times)
    # Convert values (ns) to minutes
    in_minutes = []
    for t in times_to_start:
        a = np.timedelta64(t, 'ms')
        in_minutes.append(int(str(a.astype('timedelta64[m]')).split(' ')[0]))

    # Plot action
    # fig, ax = plt.subplots(nrows=1, ncols=1, figsize=(15, 10))
    fig, ax = plt.subplots(nrows=1, ncols=1, figsize=(70, 15))
    ax.plot(dates, in_minutes)

    myfmt = mdates.DateFormatter('%A (%m/%d)')
    ax.xaxis.set_major_formatter(myfmt)

    for ax in fig.axes:
        plt.sca(ax)
        plt.xticks(np.arange(min(dates), max(dates), 1.0))
        plt.setp(ax.xaxis.get_majorticklabels(), rotation=45, ha='right')

        for tick in ax.xaxis.get_major_ticks():
            tick.label.set_fontsize(4)

        # Add horizontal line
        plt.axhline(y=120, color='#1C6A5B', linestyle='--', linewidth=1)

    plt.savefig('{}/{}.sub2start.png'.format(outdir, todays_date))
    # plt.savefig('{}/{}.sub2start.png'.format(outdir, todays_date), dpi=500)

if __name__ == '__main__':

    args = get_args()
    outdir = args.outdir
    todays_date = datetime.datetime.today().isoformat(sep='T').split('T')[0]
    # f = '/Users/sim/Dropbox/EDLB/CalculationEngine/HPC/08-19-2019/accounting.txt'

    d = distill_input(args.infile)

    plot_sub_to_start(d, outdir, todays_date)


import json
import numpy as np
import scipy.ndimage as ndi
from skimage import segmentation
from bounding_box import BoundingBox

class Segmentor:
    @staticmethod
    def _slice_to_bbox(object_slice, label=None):
        left = object_slice[1].start
        right = object_slice[1].stop
        top = object_slice[0].start
        bottom = object_slice[0].stop
        return BoundingBox(left, right, top, bottom, label)

    @staticmethod
    def _make_labels(img):
        markers = np.zeros_like(img)
        markers[img < 100] = 1
        markers[img > 240] = 2
        segments = segmentation.watershed(img, markers)
        for ws in np.nditer(segments, op_flags=['readwrite']):
            if ws == 2:
                ws[...] = 0
        return ndi.measurements.label(segments)

    def _get_bboxes(self, label_matrix, max_label):
        object_slices = ndi.measurements.find_objects(label_matrix)
        proposed_bboxes = []
        labels = range(1, max_label + 1)
        for curr_slice, l in zip(object_slices, labels):
            bbox = self._slice_to_bbox(curr_slice, l)
            proposed_bboxes.append(bbox)
        return proposed_bboxes

    def segment(self, img):
        label_matrix, max_label = self._make_labels(img)
        bboxes = self._get_bboxes(label_matrix, max_label)
        return json.dumps([o.__dict__ for o in bboxes])


'''
Created on 30 Jul 2013

@author: hicksj
'''
import string
class PageUtils():

    @staticmethod
    def getPageUrl(pageId, parms={}):
        url = "/cgi-bin/page.py?id={pId}{extraParms}"
        parmList = string.join([k + "=" + v for k, v in parms], "&")
        answer = url.format(pId=pageId, extraParms=parmList)
        return answer


import logging
import random
import time

from kazoo.exceptions import (
    ConnectionClosedError,
    ConnectionLoss,
    KazooException,
    OperationTimeoutError,
    SessionExpiredError,
)

log = logging.getLogger(__name__)

class ForceRetryError(Exception):
    """Raised when some recipe logic wants to force a retry."""

class RetryFailedError(KazooException):
    """Raised when retrying an operation ultimately failed, after
    retrying the maximum number of attempts.
    """

class InterruptedError(RetryFailedError):
    """Raised when the retry is forcibly interrupted by the interrupt
    function"""

class KazooRetry(object):
    """Helper for retrying a method in the face of retry-able
    exceptions"""
    RETRY_EXCEPTIONS = (
        ConnectionLoss,
        OperationTimeoutError,
        ForceRetryError
    )

    EXPIRED_EXCEPTIONS = (
        SessionExpiredError,
    )

    def __init__(self, max_tries=1, delay=0.1, backoff=2, max_jitter=0.4,
                 max_delay=60.0, ignore_expire=True, sleep_func=time.sleep,
                 deadline=None, interrupt=None):
        """Create a :class:`KazooRetry` instance for retrying function
        calls.

        :param max_tries: How many times to retry the command. -1 means
                          infinite tries.
        :param delay: Initial delay between retry attempts.
        :param backoff: Backoff multiplier between retry attempts.
                        Defaults to 2 for exponential backoff.
        :param max_jitter: Percentage of jitter to apply to each retry's delay
                           to ensure all clients to do not hammer the server
                           at the same time. Between 0.0 and 1.0.
        :param max_delay: Maximum delay in seconds, regardless of other
                          backoff settings. Defaults to one minute.
        :param ignore_expire:
            Whether a session expiration should be ignored and treated
            as a retry-able command.
        :param interrupt:
            Function that will be called with no args that may return
            True if the retry should be ceased immediately. This will
            be called no more than every 0.1 seconds during a wait
            between retries.

        """
        self.max_tries = max_tries
        self.delay = delay
        self.backoff = backoff
        # Ensure max_jitter is in (0, 1)
        self.max_jitter = max(min(max_jitter, 1.0), 0.0)
        self.max_delay = float(max_delay)
        self._attempts = 0
        self._cur_delay = delay
        self.deadline = deadline
        self._cur_stoptime = None
        self.sleep_func = sleep_func
        self.retry_exceptions = self.RETRY_EXCEPTIONS
        self.interrupt = interrupt
        if ignore_expire:
            self.retry_exceptions += self.EXPIRED_EXCEPTIONS

    def reset(self):
        """Reset the attempt counter"""
        self._attempts = 0
        self._cur_delay = self.delay
        self._cur_stoptime = None

    def copy(self):
        """Return a clone of this retry manager"""
        obj = KazooRetry(max_tries=self.max_tries,
                         delay=self.delay,
                         backoff=self.backoff,
                         max_jitter=self.max_jitter,
                         max_delay=self.max_delay,
                         sleep_func=self.sleep_func,
                         deadline=self.deadline,
                         interrupt=self.interrupt)
        obj.retry_exceptions = self.retry_exceptions
        return obj

    def __call__(self, func, *args, **kwargs):
        """Call a function with arguments until it completes without
        throwing a Kazoo exception

        :param func: Function to call
        :param args: Positional arguments to call the function with
        :params kwargs: Keyword arguments to call the function with

        The function will be called until it doesn't throw one of the
        retryable exceptions (ConnectionLoss, OperationTimeout, or
        ForceRetryError), and optionally retrying on session
        expiration.

        """
        self.reset()

        while True:
            try:
                if self.deadline is not None and self._cur_stoptime is None:
                    self._cur_stoptime = time.time() + self.deadline
                return func(*args, **kwargs)
            except ConnectionClosedError:
                raise
            except self.retry_exceptions:
                # Note: max_tries == -1 means infinite tries.
                if self._attempts == self.max_tries:
                    raise RetryFailedError("Too many retry attempts")
                self._attempts += 1
                jitter = random.uniform(1.0-self.max_jitter,
                                        1.0+self.max_jitter)
                sleeptime = self._cur_delay * jitter

                if self._cur_stoptime is not None and \
                   time.time() + sleeptime >= self._cur_stoptime:
                    raise RetryFailedError("Exceeded retry deadline")

                if self.interrupt:
                    remain_time = sleeptime
                    while remain_time > 0:
                        # Break the time period down and sleep for no
                        # longer than 0.1 before calling the interrupt
                        self.sleep_func(min(0.1, remain_time))
                        remain_time -= 0.1
                        if self.interrupt():
                            raise InterruptedError()
                else:
                    self.sleep_func(sleeptime)
                self._cur_delay = min(sleeptime * self.backoff,
                                      self.max_delay)