#-------------------------------------------------------------------------
# AUTHOR: Christopher Tomes
# FILENAME: decision_tree.py
# SPECIFICATION: This code implements a decision tree classifier using scikit-learn's decision tree classifier on "contact_lens.csv". The categorical data in the file is mapped to integers, fit into the model, then plotted using matplotlib.
# FOR: CS 4210- Assignment #1
# TIME SPENT: the coding portion for this assignment was around 1 hour.
#-----------------------------------------------------------*/

#IMPORTANT NOTE: DO NOT USE ANY ADVANCED PYTHON LIBRARY TO COMPLETE THIS CODE SUCH AS numpy OR pandas. You have to work here only with standard
# dictionaries, lists, and arrays

#importing some Python libraries
from sklearn import tree
import matplotlib.pyplot as plt
import csv

#MacThings
import os
import platform
db = []
X = []
Y = []

#MacThings:
##################################################
#small change to alow me to have it work on my Mac
#also replaced name of csv with the var string_path
if platform.system() == 'Darwin':
  string_path = os.getcwd() + '/contact_lens.csv'
else:
  string_path = 'contact_lens.csv'
###################################################

#reading the data in a csv file
with open(string_path, 'r') as csvfile:
  reader = csv.reader(csvfile)
  for i, row in enumerate(reader):
      if i > 0: #skipping the header
         db.append (row)
         #print(row)

#transform the original categorical training features into numbers and add to the 4D array X. For instance Young = 1, Prepresbyopic = 2, Presbyopic = 3
# so X = [[1, 1, 1, 1], [2, 2, 2, 2], ...]]
#--> add your Python code here

# Create a dictionary to store map from unique elements to integers
element_to_int = {}
int_to_element = {}

# Iterate over the columns
for column in zip(*db):
    # Get the unique elements
    unique_elements = set(column)

    # Iterate over the unique elements
    for element in unique_elements:
        if element not in element_to_int:
            element_to_int[element] = len(element_to_int)
            int_to_element[len(int_to_element)] = element

# Create a new 2D array to store the mapped values
mapped_arr = []

# Iterate over the rows of to convert via new vals
for row in db:
    mapped_row = []

    # Iterate over the elements in a row
    for element in row:
        # Replace each element with new corresponding integer value we got earlier
        mapped_element = element_to_int[element]
        mapped_row.append(mapped_element)

    # Add the mapped row to the mapped array
    mapped_arr.append(mapped_row)

#print(mapped_array)

# X =
#transform the original categorical training classes into numbers and add to the vector Y. For instance Yes = 1, No = 2, so Y = [1, 1, 2, 2, ...]
#--> add your Python code here
# Y =

#combined steps

# Iterate over the rows in the mapped array to assign into your defined data structues X/Y
for row in mapped_arr:
    # Extract the vals
    values = row[:-1]
    last_value = row[-1]
    # Add the vals to the first n-1 columns to
    X.append(values)
    # Add last column to Y
    Y.append(last_value)

#print(X)
#print(Y)

#fitting the decision tree to the data
clf = tree.DecisionTreeClassifier(criterion = 'entropy')
clf = clf.fit(X, Y)

#plotting the decision tree
tree.plot_tree(clf, feature_names=['Age', 'Spectacle', 'Astigmatism', 'Tear'], class_names=['Yes','No'], filled=True, rounded=True)
plt.show()


import numpy as np
import sklearn.svm

def dataset3Params(X, y, Xval, yval):
    """returns your choice of C and sigma. You should complete
    this function to return the optimal C and sigma based on a
    cross-validation set.
    """

# You need to return the following variables correctly.
    C = 1
    sigma = 0.3

# ====================== YOUR CODE HERE ======================
# Instructions: Fill in this function to return the optimal C and sigma
#               learning parameters found using the cross validation set.
#               You can use svmPredict to predict the labels on the cross
#               validation set. For example,
#                   predictions = svmPredict(model, Xval)
#               will return the predictions on the cross validation set.
#
#  Note: You can compute the prediction error using
#        mean(double(predictions ~= yval))
#
    C_vec = np.array([0.01, 0.03, 0.1, 0.3, 1.0, 3.0, 10.0, 30.0])
    sig_vec = np.array([0.01, 0.03, 0.1, 0.3, 1.0, 3.0, 10.0, 30.0])
    lenth = len(C_vec)
    pred_mat = np.ones((lenth,lenth))
    for i in range(lenth):
        for j in range(lenth):
            gamij = 1.0 / (2.0 * sig_vec[j] ** 2)
            clf = sklearn.svm.SVC(C=C_vec[i], kernel='rbf', tol=1e-3, max_iter=200, gamma=gamij)
            model = clf.fit(X,y)
            predictions = model.predict(Xval)
            pred_mat[i,j] = np.mean(np.double(predictions != yval))
    min_mat = np.argwhere(pred_mat == np.min(pred_mat))
    C_i = min_mat[0,0]
    sig_j = min_mat[0,1]
    C = C_vec[C_i]
    sigma = sig_vec[sig_j]

# =========================================================================
    return C, sigma


import cv2, math, numpy as np
# --------------------- START PARAMETERS -----------------------
# h,s,v range of the object to be tracked - get these values with hsv_color_picker.py
color_values = 128,119,55,157,255,126
#color_values = 115,110,45,175,255,186
color_values = 126,107,7,206,255,152

# Capture your source video
cap = cv2.VideoCapture('/home/stephen/Desktop/642.MP4')
# Pick a path to save the data
output_path = '/home/stephen/Desktop/data/1.csv'
# How big is a ball? This is the dark blue circle (9 to 60)
ball_size = 15
# How big of a colored swatch is necessary to deam it as a ball (25 to 81 is good)
contour_ball_size = 15
global max_snap_distance, max_direction_deviation
# The tracker 'snaps' onto the ball's position. 45 is good - light blue circle
max_snap_distance = 20
# The ball shouldn't change direction drastically 9 is good for 120fps
max_direction_deviation = 35
deviation_warning = 5
# If the tracker gets lost, the last few tracking values are erroneous
# Preserving the history allows the user can skip back a bit when a tracker error occurs
p_imgs, history_length, history_idx = [], 30, 0
# -------------------- END PARAMETERS ----------------------
# Parameters for the text in the user instructions
font, scale, color, thick = cv2.FONT_HERSHEY_SIMPLEX, .5, (255,0,0), 1
# Takes an image, and a lower and upper bound
# Returns only the parts of the image in bounds
def only_color(frame, color_valeus, p_position):
    # Create a mask
    mask = np.zeros((frame.shape[0], frame.shape[1]), np.uint8)
    # Draw a circle around the last known position of the ball
    # Everything outside the circle is value=0
    # Only objects inside the circle are possible juggling balls
    mask = cv2.circle(mask, p_position, max_snap_distance, 255, -1)
    # Mask out the image
    frame = cv2.bitwise_and(frame, frame, mask=mask)
    # Convert BGR to HSV
    b,r,g,b1,r1,g1 = color_values
    hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
    # Define range of blue color in HSV
    lower, upper = np.array([b,r,g]), np.array([b1,r1,g1])
    # Threshold the HSV image to get only blue colors
    mask = cv2.inRange(hsv, lower, upper)
    # Bitwise-AND mask and original image
    res = cv2.bitwise_and(frame,frame, mask= mask)
    #cv2.imshow('mask', mask)
    return res, mask
# Finds the largest contour in a list of contours
# Returns a single contour
def largest_contour(contours):
    c = max(contours, key=cv2.contourArea)
    return c[0]

# Takes an image and the threshold value returns the contours
def get_contours(im):
    imgray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
    _ ,thresh = cv2.threshold(imgray,0,255,0)
    contours, _ = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
    return contours

# Finds the center of a contour
# Takes a single contour
# Returns (x,y) position of the contour
def contour_center(c):
    M = cv2.moments(c)
    try: center = int(M['m10']/M['m00']), int(M['m01']/M['m00'])
    except: center = 0,0
    return center

# Finds the most likely position of the ball in an image using color
# Returns xy coordinates
def find_ball_by_color(img, color_values, ball_position):
    # Filter the image for the specific ball color
    img, mask = only_color(img, color_values, ball_position)
    # Find the contours in the image
    contours = get_contours(img)
    # Start with values that are definitely out of the snap range
    nearest_ball = 100000000
    nearest_snap_possibility = (10000,10000)
    # Iterate through the contours
    for contour in contours:
        # Check if a contour is big enough to be considered a ball
        if cv2.contourArea(contour)>contour_ball_size:
            # Get the center of the contour
            center = contour_center(contour)
            # This contour is the nearest one to the ball, it is the most likely snap position
            if distance(center, ball_position) < nearest_ball:
                # Save this as the nearest likely ball
                nearest_snap_possibility = center
    # Return the mostly likely location of the ball in the image
    return nearest_snap_possibility

def distance(a,b): return math.sqrt((a[0]-b[0])**2+(a[1]-b[1])**2)

# Parameters of lk optical flow
lk_params = dict(winSize = (ball_size, ball_size),
                 maxLevel = 5,
                 criteria = (cv2.TERM_CRITERIA_EPS |
                             cv2.TERM_CRITERIA_COUNT, 5, 0.03))

# Mouse callback function
global click_list
positions, click_list = [], []
def callback(event, x, y, flags, param):
    if event == 1: click_list.append((x,y))
cv2.namedWindow('img')
cv2.setMouseCallback('img', callback)

# Initialize frame_number
frame_number = 0
wait_time = 0

# Initialize tracker value, this will get reset by the user
p0 = np.array([[[0,0]]], np.float32)
fails = 0
# Main loop
while True:

    # Program crashes if user tries to use the history before it is built completely
    if len(p_imgs)< history_length - history_idx: print("Rebuilding history...")

    # Read frame of video (either from the source or the history)
    if history_idx == 0:
        ret, img = cap.read()
        try:
            #img = cv2.resize(img, (1908, 1080)) #resize the image so it fills screen
            #img = cv2.blur(img, (15,15))
            pass
        except: break
        # Save the frame to the history
        if ret: p_imgs.append(img.copy())
    else:
        # Read a frame from the history
        img = p_imgs.pop(0)
        history_idx -= 1

    # Break if the video is over
    try:h,w,e = img.shape
    except: break

    # Optical flow works in single channel, so save the previous gray image
    try: old_gray = img_gray.copy()
    except: old_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    try: img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    except: break

    # Track using optical flow
    # p1 is the value produced by the optical flow tracker
    p1, _, _ = cv2.calcOpticalFlowPyrLK(old_gray, img_gray,p0, None, **lk_params)

    # Convert the coordinates to integers for drawing
    xy  = int(p1[0][0][0]), int(p1[0][0][1])

    # Get the nearest likely snap position
    snap_position = find_ball_by_color(img, color_values, xy)
    # Draw a circle around the snap position showing the max snap distance and max directional deviation
    cv2.circle(img, snap_position, max_snap_distance, (255,255,0), 1)
    cv2.circle(img, snap_position, max_direction_deviation, (0,255,255), 1)
    # Is the likely position found using color close to the optical flow tracker
    if distance(snap_position, xy) < max_snap_distance:
        # The color value is a good one, so snap to that
        xy = snap_position
        p1 = np.array([[[snap_position[0], snap_position[1]]]], np.float32)

    # Update the predicted tracking point
    p0 = p1

    # If there are at least two positions, use those positions to calculate a likely position
    # where the ball will be in the future, if the ball isn't there, tracker failure!
    if len(positions)>2:
        # Get the last two tracking locations
        past, present = positions[-2:]
        # Calculate the predicted future position of the ball
        dx, dy = present[0]-past[0], present[1]-past[1]
        future = present[0]+dx, present[1]+dy
        #print(dx,dy, distance(future, xy))
        cv2.circle(img, future, 2, (255, 255, 255), -1)
        # If the tracking location and the predicted location don't match, stop the tracker and tell the user
        if distance(future, xy) > deviation_warning:
            print("Direction Warning: " , distance(future, xy))
        else:
            if fails > 0: fails -=1
        if distance(future, xy) > max_direction_deviation:
            print("Direction Fail: ", distance(future, xy))
            wait_time = 0

    # Make circle around ball to show the tracking point
    cv2.circle(img, xy, ball_size, (255,2,1), 1)

    # Write instructions to user
    if wait_time == 0:
        cv2.putText(img, "Click on ball and press F5 to reset tracker", (50,50), font, scale, color, thick, cv2.LINE_AA)
        if len(positions)>0:
            cv2.putText(img, "...or press F6 to continue to the next frame", (50,80), font, scale, color, thick, cv2.LINE_AA)
            cv2.putText(img, "...or press F7 to autotrack slowly", (50,110), font, scale, color, thick, cv2.LINE_AA)
            cv2.putText(img, "...or press F8 to autotrack at max speed", (50,140), font, scale, color, thick, cv2.LINE_AA)
            cv2.putText(img, "...or press F4 to use the predicted value (white dot)", (50,170), font, scale, color, thick, cv2.LINE_AA)
    if wait_time == 1 or wait_time == 25:
        cv2.putText(img, "Auto-Tracking", (50,50), font, scale, color, thick, cv2.LINE_AA)
        cv2.putText(img, "Press any key on mistake", (50,80), font, scale, color, thick, cv2.LINE_AA)
        cv2.putText(img, "Press F6 to pause", (50,110), font, scale, color, thick, cv2.LINE_AA)

    # Draw the previous ball positions on the screen
    idx = 0
    while idx < 20 and idx < len(positions)-3:
        a,b = positions[-(idx+2)], positions[-(idx+1)]
        cv2.line(img, a,b, (0,255,86), 4)
        idx += 1

    # Show frame and wait
    cv2.imshow('img', img)
    k = cv2.waitKey(wait_time)
    if k ==27: break

    # User presses F4, use the the predicted value
    if k == 193:
        xy = future
        p0 = np.array([[xy]], np.float32)
    # User Presses F6, advance to the next frame and wait
    if k == 195: wait_time = 0
    # User presses F7, play slowly
    if k == 196: wait_time = 65
    # User presses F8, max computer resources
    if k == 197: wait_time = 1
    # User wants to reset the tracker to the last click position
    if k == 194: #F5
        # Get the user's input
        xy = click_list[-1]
        # Set the tracker to this user input
        p0 = np.array([[xy]], np.float32)
        # Advance to the next frame, but pause
        wait_time = 0

    # Add tracked location to data
    positions.append(xy)

    # If there are enough items in the history, remove the oldest one
    if len(p_imgs)>history_length: p_imgs.pop(0)

    # User has paused any other key because the tracker has gotten lost
    if k < 190 and k >10:
        # Pause
        wait_time = 0
        # Get the history length
        history_idx = history_length
        # Remove the last few positions, that are errors from a broken tracker
        positions = positions[:-history_length]

# Close the window and release the source video
cv2.destroyAllWindows()
cap.release()

# Write data to a csv (spreadsheet) file
import csv
with open(output_path, 'w') as csvfile:
    fieldnames = ['x_position', 'y_position']
    writer = csv.DictWriter(csvfile, fieldnames = fieldnames)
    writer.writeheader()
    for position in positions:
        x, y = position[0], position[1]
        writer.writerow({'x_position': x, 'y_position': y})
print( 'finished writing data')


from django.urls import path
from .import views

urlpatterns = [
    path('', views.home, name='home'),
    path('trending/', views.trending, name='trending'),
    path('search/', views.search, name='search'),
    path('product_details/<slug>', views.details, name='product_details'),
    path('about/', views.about, name='about')
    # path('productBycat/', views.productBycat, name='productBycat'),
]


from django import forms
from django.contrib.auth.models import User
from profiles.models import *
from projects.models import Project, Upload

class ProfileForm(forms.ModelForm):
    def __init__(self, *args, **kwargs):
        super(ProfileForm, self).__init__(*args, **kwargs)
        self.fields['pic'].label = 'Picture'

    class Meta:
        model = Academic
        fields = ('user','affiliation', 'pic', 'bio', 'url', 'linkedIn', 'twitter')

class ProjectForm(forms.ModelForm):

    class Meta:
        model = Project
        fields = ('title', 'description', 'supervisor1', 'supervisor2')

class AddPortfolioForm(forms.ModelForm):

    class Meta:
        model = Upload
        fields = ('type', 'label', 'upload', 'URL', 'public')

class EditPortfolioForm(forms.ModelForm):

    class Meta:
        model = Upload
        fields = ('type', 'label', 'upload', 'URL', 'public')


import ConfigParser
import os

import music21

def time_signature_equality(a, b):
    return a.numerator == b.numerator and a.denominator == b.denominator

def rhythms(song_path, sig):
    """Traverse the file whose handle is supplied and return a collection of
    usable rhythms.

    This method will look at all the rhythms that are in the piece, so not just
    the guitar voice. It will discard ghost notes. The result
    may contain duplicates, which will need to be filtered out later on."""
    if sig == '128':
        sig = music21.meter.TimeSignature('12/8')
    elif sig == 'common':
        sig = music21.meter.TimeSignature('4/4')
    else:
        sig = None
    assert sig is not None
    discovered_rhythms = []
    song_score = music21.converter.parse(song_path)
    for part in song_score.getElementsByClass(music21.stream.Part):
        for measure in part.getElementsByClass(music21.stream.Measure):
            measure_replacement = music21.stream.Measure()
            measure_replacement.timeSignature = sig
            for index in range(0, len(measure)):
                if(isinstance(measure[index], music21.note.Note) or
                   isinstance(measure[index], music21.chord.Chord)):
                    sound_replacement = music21.note.Note('a4')
                    sound_replacement.duration = measure[index].duration
                    measure_replacement.append(sound_replacement)
                    measure_contains_notes = True
                else:
                    measure_replacement.append(measure[index])
            measure_replacement.offset = 0
            discovered_rhythms.append(measure_replacement)
    return discovered_rhythms

def time_signature(path):
    """Return the time signature of a piece.

    The known types of time signature are "common", "128" and "unknown".
    If a piece contains measures with different signatures, the result is
    "unknown"."""
    song_score = music21.converter.parse(path)
    # note: __equals__ does not behave as expected for TimeSignature!
    # use time_signature_equality instead.
    common_time = music21.meter.TimeSignature('4/4')
    twelve_eighths = music21.meter.TimeSignature('12/8')
    guess = None
    part = song_score.getElementsByClass(music21.stream.Part)[0]
    for measure in part.getElementsByClass(music21.stream.Measure):
        if measure.timeSignature:
            if time_signature_equality(measure.timeSignature, common_time):
                if(guess is None or time_signature_equality(measure.timeSignature, guess)):
                    guess = common_time
                else:
                    return 'unknown'
            elif time_signature_equality(measure.timeSignature, twelve_eighths):
                if(guess is None or time_signature_equality(measure.timeSignature, guess)):
                    guess = twelve_eighths
                else:
                    return 'unknown'
        else:
            continue # if the signature is unchanged, no problem
    if guess is common_time:
        return 'common'
    elif guess is twelve_eighths:
        return '128'
    else:
        return 'unknown'

def create_measure_files(in_folder, out_folder_root):
    r"""
    Given the path the source MusicXML files as `in_folder` and the desired
    output location for subfolders containing extracted rhythms organized
    per time signature as `out_folder_root`, create a MusicXML file for each
    meaningful rhythm.

    **This function does not eliminate duplicate rhythms and it does not leave
    out rhythms played by non-guitar instruments, nor does it ignore
    whole-note rests.** Such postprocessing is handled by consumer functions.
    """
    for file_entry in enumerate(os.listdir(in_folder)):
        source_path = in_folder + os.sep + file_entry[1]
        sig = time_signature(source_path) # common, 128 or unknown
        if sig == 'unknown':
            continue
        out_folder = out_folder_root + os.sep + sig
        sep = os.sep
        file_num = file_entry[0]
        for entry in enumerate(rhythms(source_path, sig)):
            rhythm_num = entry[0]
            out_path = '{out_folder}{sep}{file_num}_{rhythm_num}.xml'
            entry[1].write(fmt='musicxml', fp=out_path.format(**locals()))

if __name__ == '__main__':
    config = ConfigParser.ConfigParser()
    here = os.path.dirname(os.path.abspath(__file__))
    param_path = os.sep.join((here, 'params.ini'))
    with open(param_path) as param_fh:
        config.readfp(param_fh)
    musicxml_dir = config.get('Analysis', 'musicxml_dir')
    rhythm_measures_dir = config.get('Analysis',
                                     'intermediate_rhythm_measures_dir')
    create_measure_files(musicxml_dir, rhythm_measures_dir)


# coding: utf-8
"""
-------------------------------------------------
   File Name:      VTK_1
   Author :        Scar
   E-mail :        scarforhere@gmail.com
   Date:           2021-05-18 04:28 PM
-------------------------------------------------
Description :

    Print info with under format:

"""
import vtk

# Quader anlegen (Source, Mapper, Actor)
quaderSource = vtk.vtkCubeSource()
quaderSource.SetXLength(0.3)
quaderSource.SetYLength(0.1)
quaderSource.SetZLength(0.1)

# Mapper
quaderMapper = vtk.vtkPolyDataMapper()
quaderMapper.SetInputConnection(quaderSource.GetOutputPort())

# Actor
quader = vtk.vtkLODActor()
quader.SetMapper(quaderMapper)

# Kugel anlegen (Source, Mapper, Actor)
kugelSource = vtk.vtkSphereSource()
kugelSource.SetRadius(0.08)
kugelSource.SetThetaResolution(10) # Diskretisierung
kugelSource.SetPhiResolution(10)

# Mapper
kugelMapper = vtk.vtkPolyDataMapper()
kugelMapper.SetInputConnection(kugelSource.GetOutputPort())

# Actor
kugel = vtk.vtkLODActor()
kugel.SetMapper(kugelMapper)

# Renderer
ren = vtk.vtkRenderer()

# Actors zum Renderer hinzufügen
ren.AddActor(quader)
ren.AddActor(kugel)

# RenderWindow und RenderWindowInteractor
renWin = vtk.vtkRenderWindow()
renWin.AddRenderer(ren)
iren = vtk.vtkRenderWindowInteractor()
iren.SetRenderWindow(renWin)

# Hintergrundfarbe und Fenstergröße
ren.SetBackground(0.1, 0.2, 0.4)
renWin.SetSize(500, 500)

# Mausmanipulator anpassen, Fenster mit Visualisierung initialisieren
iren.SetInteractorStyle(vtk.vtkInteractorStyleTrackballCamera())
iren.Initialize()

import vtk.util.colors as colors
kugel.GetProperty().SetColor(colors.red)
quader.GetProperty().SetOpacity(0.5)

# Fenster öffnen
iren.Start()

# Schließt das Vtk Fenster wieder (q drücken oder Fenster mit Maus schließen)
iren.GetRenderWindow().Finalize()


import asyncio
import nest_asyncio # must be before any imports
nest_asyncio.apply()

import json
from functools import partial
import http
import http.server
import socketserver
import os
import sys

import discord

current_dir = os.path.dirname(__file__)

DISCORD_CLIENT_TOKEN_KEY = "discordClientToken"
DISCORD_CHANNEL_ID_KEY = "discordChannelId"
POSSIBLE_PORTS_KEY = "possiblePorts"

TEMP_VARIABLE_L_SP_PAREN = "<<<<<ae05d0454a05221d2705b1e8b3df7997"
TEMP_VARIABLE_R_SP_PAREN = ">>>>>b61d275e529583db4f8f94f82ef96851"

TEMP_VARIABLE_L_PAREN = "<<<<<1c67bbc1e3d67fcb0f99cbe596ddc6bf"
TEMP_VARIABLE_R_PAREN = ">>>>>5adc29d9c262b3b393f1effce07f15d5"

POST_SEPERATOR = "<<|>>"

def quit(message=None):
	if message is None:
		sys.exit()
	else:
		sys.exit(message)

def fill_template(templ_string, fill_dict):
	temp_main_template = templ_string.replace("{%", TEMP_VARIABLE_L_SP_PAREN)
	temp_main_template = temp_main_template.replace("%}", TEMP_VARIABLE_R_SP_PAREN)

	temp_main_template = temp_main_template.replace("{", TEMP_VARIABLE_L_PAREN)
	temp_main_template = temp_main_template.replace("}", TEMP_VARIABLE_R_PAREN)

	temp_main_template = temp_main_template.replace(TEMP_VARIABLE_L_SP_PAREN, "{")
	temp_main_template = temp_main_template.replace(TEMP_VARIABLE_R_SP_PAREN, "}")

	temp_main_template = temp_main_template.format(**fill_dict)

	temp_main_template = temp_main_template.replace(TEMP_VARIABLE_L_PAREN, "{")
	temp_main_template = temp_main_template.replace(TEMP_VARIABLE_R_PAREN, "}")

	return temp_main_template

def filenames_in_directory(direc):
	for (_, _, filenames) in os.walk(direc):
			return filenames

def build_player_data_json():
	out_arr = []
	for file in filenames_in_directory(os.path.join(current_dir, "saved")):
		if (file != '.gitignore'):
			with open(os.path.join(current_dir, f"saved/{file}"), "r") as f:
				out_arr.append(json.load(f))
	return json.dumps(out_arr)

def get_html():
	with open(os.path.join(current_dir, "templates/main.tmpl"), "r") as f:
		main_template = f.read()

	with open(os.path.join(current_dir, "templates/style.css"), "r") as f:
		style = f.read()

	with open(os.path.join(current_dir, "default.json"), "r") as f:
		default_info = json.load(f)

	player_data = build_player_data_json()

	fill_dict = {
		"style_info":style,
		"default_player_data":json.dumps(default_info),
		"player_data":player_data,
		"post_seperator":POST_SEPERATOR
	}

	return fill_template(main_template, fill_dict)

def create_roll_command(data):
	command = []

	if data["greens"] > 0:
		command.append(f"{data['greens']}g")

	if data["yellows"] > 0:
		command.append(f"{data['yellows']}y")

	if data["blues"] > 0:
		command.append(f"{data['blues']}b")

	if data["purples"] > 0:
		command.append(f"{data['purples']}p")

	if data["blacks"] > 0:
		command.append(f"{data['blacks']}k")

	if data["reds"] > 0:
		command.append(f"{data['reds']}r")

	if len(command) > 0:
		return "!r " + " ".join(command)
	else:
		return None

def save_json_to_file(data):
	with open(os.path.join(current_dir, f"saved/{data['base']['name']}.json"), "w") as f:
		json.dump(data, f)

def delete_json_file(data):
	os.remove(f"saved/{data['base']['name']}.json")

class HttpRequestHandler(http.server.SimpleHTTPRequestHandler):
	def __init__(self, discord_client, discord_channel_id, *args, **kwargs):
		self.discord_client = discord_client
		self.discord_channel = discord_client.get_channel(discord_channel_id)
		super().__init__(*args, **kwargs)

	def do_GET(self):
		self.send_response(200)
		self.send_header("Content-type", "text/html")
		self.end_headers()

		html = get_html()
		self.wfile.write(bytes(html, "utf8"))

	def do_POST(self):
		content_length = int(self.headers['Content-Length'])
		post_data = self.rfile.read(content_length).decode("utf-8")
		if post_data == "shutdown":
			print("Shutting Down")
			quit()
		elif post_data == "destiny":
			self.send_message("!destiny roll")
		else:
			command, data = post_data.split(POST_SEPERATOR)
			json_post_data = json.loads(data)
			if command == "roll":
				roll_command = create_roll_command(json_post_data)
				if roll_command is not None:
					self.send_message(roll_command)
			if command == "save":
				save_json_to_file(json_post_data)

		self.send_response(200)
		self.send_header('Content-Type', 'application/json')
		self.end_headers()

	def send_message(self, message):
		self.discord_client.loop.run_until_complete(self.discord_channel.send(message))

async def serve(discord_client, discord_channel_id, possible_ports):
	handler = partial(HttpRequestHandler, discord_client, discord_channel_id)
	found_port = False
	for p in possible_ports:
		try:
			with socketserver.TCPServer(("", p), handler) as my_server:
				found_port = True
				print(f"starting server on {my_server.server_address[0]}:{my_server.server_address[1]}")
				await discord_client.wait_until_ready()
				await my_server.serve_forever()
				my_server.server_close()
				break
		except OSError:
			print(f"port {p} already in use moving on...")
			continue

	if not found_port:
		print("could not find open port in range")
		quit()

class DiscordClient(discord.Client):
		async def on_ready(self):
			print('Logged on as', self.user)

def main():
	try:
		with open(os.path.join(current_dir, "config.json"), "r") as f:
			config = json.load(f)
	except json.decoder.JSONDecodeError:
		quit("'config.json' could not be read, or is not filled in correctly.")

	if all(config[x] is not None for x in [DISCORD_CLIENT_TOKEN_KEY, DISCORD_CHANNEL_ID_KEY]):
		client = DiscordClient()
		client.loop.create_task(serve(client, config[DISCORD_CHANNEL_ID_KEY], config[POSSIBLE_PORTS_KEY]))
		client.run(config[DISCORD_CLIENT_TOKEN_KEY], bot=False)
	else:
		print(f"Could not find {DISCORD_CLIENT_TOKEN_KEY} or {DISCORD_CHANNEL_ID_KEY} in config.json.")
		print("Shutting Down.")
		quit()

if __name__ == "__main__":
	main()


import numpy as np
from utils.predictors.predictor import StatePredictor
from utils.optimizers.simple import solve
from random import random
from utils.state import State

class Predictor(StatePredictor):
    def __init__(self):
        self.A = None
        self.b = None
    def predict(self, state):
        print(state.shape)
        if self.A is None or self.b is None:
            return state+np.random.uniform(-100,100,state.shape)
        return state+self.A@state+self.b
    def train(self, input_states, output_states):
        S1 = np.array(input_states).transpose()
        S2 = np.array(output_states).transpose()
        try:
            B = solve(S1,S2-S1)
            self.A = B[:,:-1]
            self.b = B[:,-1]
            print("adjusted")
        except:
            print("failed")


import re
from dataclasses import dataclass, field
from typing import List

@dataclass
class Region:
    x_min: int
    x_max: int
    y_min: int
    y_max: int
    z_min: int
    z_max: int
    holes: List['Region'] = field(default_factory=list)
    on: bool = False

    def count_on(self):
        if self.on:
            volume = (self.x_max + 1 - self.x_min) * (self.y_max + 1 - self.y_min) * (self.z_max + 1 - self.z_min)
            for region in self.holes:
                if not region.on:
                    volume -= region.count_on()
            return volume
        volume = 0
        for region in self.holes:
            if region.on:
                volume += region.count_on()
        return volume

    def is_within_region(self, other: 'Region'):
        within = (
            other.x_min <= self.x_min <= self.x_max <= other.x_max
        and other.y_min <= self.y_min <= self.y_max <= other.y_max
        and other.z_min <= self.z_min <= self.z_max <= other.z_max
        )
        if within:
            return True
        return False

    def get_common_region(self, other: 'Region'):
        if self.x_max < other.x_min or self.x_min > other.x_max:
            return None
        if self.y_max < other.y_min or self.y_min > other.y_max:
            return None
        if self.z_max < other.z_min or self.z_min > other.z_max:
            return None

        x_min = max(self.x_min, other.x_min)
        x_max = min(self.x_max, other.x_max)
        y_min = max(self.y_min, other.y_min)
        y_max = min(self.y_max, other.y_max)
        z_min = max(self.z_min, other.z_min)
        z_max = min(self.z_max, other.z_max)
        return Region(
            x_min=x_min,
            x_max=x_max,
            y_min=y_min,
            y_max=y_max,
            z_min=z_min,
            z_max=z_max,
            on=other.on
        )

@dataclass
class Command:
    action: str
    region: Region

def get_commands(lines):
    command_pattern = r"(on|off) x=(-?[0-9]+)..(-?[0-9]+),y=(-?[0-9]+)..(-?[0-9]+),z=(-?[0-9]+)..(-?[0-9]+)"
    commands = []
    for line in lines:
        command_search = re.search(command_pattern, line)
        if not command_search:
            raise ValueError(f"Command {line} is not a valid command.")
        action = command_search.group(1)
        x_min = int(command_search.group(2))
        x_max = int(command_search.group(3))
        y_min = int(command_search.group(4))
        y_max = int(command_search.group(5))
        z_min = int(command_search.group(6))
        z_max = int(command_search.group(7))
        region = Region(
            x_min=x_min,
            x_max=x_max,
            y_min=y_min,
            y_max=y_max,
            z_min=z_min,
            z_max=z_max,
            on = True if action == "on" else False
        )
        command = Command(
            action=action,
            region=region
        )
        commands.append(command)
    return commands

def execute_commands(commands, initial_region: Region):
    regions = [initial_region]
    for command in commands:
        new_regions = []
        for region in regions:
            if command.region.is_within_region(region) and command.region.on == region.on:
                continue  # skip steps which won't change the status of any cubes
            if any(command.region.is_within_region(hole) for hole in region.holes):
                continue
            new_region = region.get_common_region(command.region)
            if not new_region:
                continue
            region.holes.append(new_region)
            new_regions.append(new_region)
        regions.extend(new_regions)
    return regions

def main():
    with open("test.txt") as f:
        lines = [l.replace("\n", "") for l in f.readlines()]
    commands = get_commands(lines)
    maximum_area = 50
    initial_region = Region(
        x_min=-maximum_area,
        x_max=maximum_area,
        y_min=-maximum_area,
        y_max=maximum_area,
        z_min=-maximum_area,
        z_max=maximum_area,
        on=False
    )
    regions = execute_commands(commands, initial_region)
    print(initial_region.count_on())

if __name__ == "__main__":
    main()


from tensorforce.execution import Runner
from environment import SnakeEnvironment
import dqn
from logger import get_logger

MAX_EPISODES = 5000
MAX_TIMESTEPS = 100000

# https://playsnake.org/ 800x600 at top left
PLAYSNAKE_L = 190
PLAYSNAKE_T = 200
PLAYSNAKE_W = 380
PLAYSNAKE_H = 256
PLAYSNAKE_RESTART = {'action': (480, 376), 'wait_for': 'go'}
PLAYSNAKE_OVER = ["game", "ouer", "over", "best", "score"]
PLAYSNAKE_FIELD = {'top': PLAYSNAKE_T, 'left': PLAYSNAKE_L,
                   'width': PLAYSNAKE_W, 'height': PLAYSNAKE_H}

# http://patorjk.com/games/snake/ 800x600 at top left

def is_jssnake_over(image):
    return (image[200,250] == [0,0,0]).all()

import cv2
def jssnake_preprocess(image):
    hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)

    hsv_channels = cv2.split(hsv)

    rows = image.shape[0]
    cols = image.shape[1]

    for i in range(rows):
        for j in range(cols):
            h = hsv_channels[0][i][j]

            if h > 90 and h < 130:
                hsv_channels[2][i][j] = 255
            else:
                hsv_channels[2][i][j] = 0
    return cv2.merge(hsv_channels)

JSSNAKE_L = 30
JSSNAKE_T = 105
JSSNAKE_W = 740
JSSNAKE_H = 440
JSSNAKE_RESTART = {'action': 'enter'}
JSSNAKE_OVER = is_jssnake_over
JSSNAKE_FIELD = {'top': JSSNAKE_T, 'left': JSSNAKE_L,
                 'width': JSSNAKE_W, 'height': JSSNAKE_H}

def get_environment(name):
    if name == 'jssnake':
        return SnakeEnvironment(JSSNAKE_FIELD, JSSNAKE_OVER, JSSNAKE_RESTART,preprocess=jssnake_preprocess)
    if name == 'playsnake':
        return SnakeEnvironment(PLAYSNAKE_FIELD, PLAYSNAKE_OVER, PLAYSNAKE_RESTART)

def main():
    log = get_logger()

    env = get_environment('jssnake')
    agent = dqn.get_agent()

    runner = Runner(agent, env)

    report_episodes = 5

    def episode_finished(r):
        if r.episode % report_episodes == 0:
            log_episode_info(log, r, 100)
        return True

    log.info("Starting {agent} for Environment '{env}'".format(
        agent=agent, env=env))

    try:
        runner.run(num_episodes=MAX_EPISODES,
                   episode_finished=episode_finished)
    except KeyboardInterrupt:
        log.info("Interrupted!")
    except Exception as e:
        log.error(e)
    finally:
        agent.save_model("./models/")
        runner.close()
        log_episode_info(log, runner, len(runner.episode_rewards))
        log.info("Learning finished.")

def log_episode_info(logger, r, n_rewards):
    logger.info("Finished episode {ep} after {ts} timesteps".format(
        ep=r.episode, ts=r.timestep))
    logger.info("Episode reward: {}".format(r.episode_rewards[-1]))
    logger.info("Average of last {} rewards: {}".format(
        n_rewards, sum(r.episode_rewards[-n_rewards:]) / n_rewards))

if __name__ == '__main__':
    main()


import argparse
import datetime
import logging
import os
import random
import numpy as np
from sklearn.metrics import f1_score, accuracy_score
import torch.nn as nn
import torch.nn.functional as F
import torchquantum as tq
import torchquantum.functional as tqf
import torch
from torch.utils.data import DataLoader, dataset
from torch.autograd import Variable

from ExpDataset import MustardDataset
from hyper_mt import HyperModel
from m2model_train import M2Model

class Q_Model(nn.Module):
  def __init__(self,bszi):
    super().__init__()
    self.encoder= tq.GeneralEncoder([
        {'input_idx': [0], 'func': 'rx', 'wires': [0]},
        {'input_idx': [1], 'func': 'rx', 'wires': [1]},
        {'input_idx': [2], 'func': 'rx', 'wires': [2]},
        {'input_idx': [3], 'func': 'rx', 'wires': [3]},
        {'input_idx': [4], 'func': 'rx', 'wires': [4]},
        {'input_idx': [5], 'func': 'rx', 'wires': [5]},
      ])

    self.sar_L=nn.Linear(3,3)
    self.sen_L=nn.Linear(3,3)

    self.measure = tq.MeasureAll(tq.PauliZ)

    self.re_sar_L=nn.Linear(3,2)
    self.re_sen_L=nn.Linear(3,3)

  def forward(self,sa,sen):

    device=tq.QuantumDevice(n_wires=6,bsz=sa.shape[0],device='cuda')
    # sa,sen=self.classM(txt,img,audio)
    sa=self.sar_L(sa)
    sen=self.sen_L(sen)

    qin=torch.concat([sen,sa],1)
    self.encoder(device,qin)

    tq.cx(device,[0,1])
    tq.cx(device,[1,2])
    tq.cx(device,[2,0])

    tq.cx(device,[3,4])
    tq.cx(device,[4,5])
    tq.cx(device,[5,3])

    x=self.measure(device)
    sar=x[:,3:6]

    sar=self.re_sar_L(sar)
    sen=x[:,0:3]

    sen=self.re_sen_L(sen)
    return sar,sen

def trainEval():

    batchsize = 500
    epochs = 40

    all_epochs=280

    now = datetime.datetime.now().strftime('%Y-%m-%d-%H-%M-%S')

    stateSavePATH = f'result/state/{all_epochs}_Epochs_{now}/state.pt'
    modelSavePATH = f'result/state/{all_epochs}_Epochs_{now}/model.pt'

    data = MustardDataset(datatye='train')
    valData = MustardDataset(datatye='dev')

    data_loader = DataLoader(
        data,
        batch_size=batchsize,
        shuffle=True,
        pin_memory=True
        )
    val_loader = DataLoader(
        valData,
        batch_size=50,
        shuffle=True,
        pin_memory=True,
        )
    class_model=torch.load('result/state/80_Epochs_2023-06-24-12-51-18/model.pt')
    class_model.eval()
    model = Q_Model(bszi=batchsize).cuda()

    lossFun = nn.CrossEntropyLoss().cuda()
    lossFun_val = nn.CrossEntropyLoss().cuda()

    optimizer = torch.optim.Adam(
        model.parameters(), lr=0.05, weight_decay=0.015)

    total = sum([param.nelement() for param in model.parameters()])
    print(f"Number of parameter: {total}")

    train_step = 0
    os.mkdir(f'result/state/{all_epochs}_Epochs_{now}')
    for _ in range(epochs):
        logger.info('epoch:'+str(_))

        for batch in data_loader:
            train_step += 1

            model.train()
            textInput = batch[0][0].cuda().to(torch.float32)
            imageInput = batch[0][1].to(torch.float32).cuda()
            audioInput = batch[0][2].to(torch.float32).cuda()

            sarLabel = batch[1][0].to(torch.float32).cuda()
            sentLabel = batch[1][1].to(torch.float32).cuda()

            with torch.no_grad():
               sar, sent= class_model.Qin(textInput, imageInput,audioInput)

            sar,sent=model(sar,sent)

            sarArgmax = torch.argmax(sarLabel, dim=-1)
            sentArgmax = torch.argmax(sentLabel, dim=-1)
            # emoArgmax = torch.argmax(emoLabel, dim=-1)
            # logger.info(f'sar:{sarArgmax} sent:{sentArgmax} emo:{emoArgmax}\n')
            loss1 = lossFun(sar, sarArgmax)
            loss2 = lossFun(sent, sentArgmax)
            # loss3 = lossFun(emo, emoArgmax)
            loss = (loss1 + loss2 )/2

            loss.requires_grad_(True)

            logger.info('loss1:%f loss2:%f loss:%f\n'
                        % (loss1.item(),
                            loss2.item(),
                            loss.item()))

            if train_step%(epochs-1) == 0:
                label_sar = np.argmax(
                    sarLabel.cpu().detach().numpy(), axis=-1)
                label_sent = np.argmax(
                    sentLabel.cpu().detach().numpy(), axis=-1)
                # label_emo = np.argmax(
                #     emoLabel.cpu().detach().numpy(), axis=-1)
                pred_sar = np.argmax(
                    sar.cpu().detach().numpy(), axis=1)
                pred_sent = np.argmax(
                    sent.cpu().detach().numpy(), axis=1)

                sar_f1 = f1_score(label_sar, pred_sar, average='micro')
                sent_f1 = f1_score(label_sent, pred_sent, average='micro')
                # emo_f1 = f1_score(label_emo, pred_emo, average='micro')
                sar_acc = accuracy_score(
                    label_sar, pred_sar)
                sent_acc = accuracy_score(
                    label_sent, pred_sent)

                logger.info(('train result sar-f1:%f sent-f1:%f ' +
                            'sar-acc:%f sent-acc:%f \n')
                            % (sar_f1, sent_f1,
                            sar_acc, sent_acc))

                model.eval()
                with torch.no_grad():
                    outputsar, outputsent, outputemo = [], [], []
                    tarsar, tarsent, taremo = [], [], []
                    for batch in val_loader:
                        textInput = batch[0][0].cuda().to(torch.float32)
                        imageInput = batch[0][1].cuda().to(torch.float32)
                        audioInput = batch[0][2].cuda().to(torch.float32)

                        sarLabel = batch[1][0].to(torch.float32).cuda()
                        sentLabel = batch[1][1].to(torch.float32).cuda()

                        with torch.no_grad():
                           sar, sent=class_model.Qin(textInput, imageInput,audioInput)

                        sar,sent=model(sar,sent)

                        sarArgmax = torch.argmax(sarLabel, dim=-1)
                        sentArgmax = torch.argmax(sentLabel, dim=-1)

                        loss1_val = lossFun_val(sar, sarArgmax)
                        loss2_val = lossFun_val(sent, sentArgmax)

                        loss_val = (loss1_val + loss2_val)/2

                        logger.info('val loss1:%f loss2:%f loss:%f\n'
                        % (loss1_val.item(),
                            loss2_val.item(),
                            loss_val.item()))

                        label_sar = np.argmax(
                            sarLabel.cpu().detach().numpy(), axis=-1)
                        label_sent = np.argmax(
                            sentLabel.cpu().detach().numpy(), axis=-1)

                        pred_sar = np.argmax(
                            sar.cpu().detach().numpy(), axis=1)
                        pred_sent = np.argmax(
                            sent.cpu().detach().numpy(), axis=1)

                        outputsar.append(pred_sar)
                        outputsent.append(pred_sent)

                        tarsar.append(label_sar)
                        tarsent.append(label_sent)

                    outputsar = np.concatenate(
                        np.array(outputsar),dtype=np.int64)
                    outputsent = np.concatenate(
                        np.array(outputsent),dtype=np.int64)

                    tarsar = np.concatenate(
                        np.array(tarsar),dtype=np.int64)
                    tarsent = np.concatenate(
                        np.array(tarsent),dtype=np.int64)

                    sar_f1 = f1_score(
                        tarsar, outputsar, average='micro')
                    sent_f1 = f1_score(
                        tarsent, outputsent, average='micro')

                    sar_acc = accuracy_score(
                        tarsar, outputsar)
                    sent_acc = accuracy_score(
                        tarsent, outputsent)

                    logger.info(('val-result sar-f1:%f sent-f1:%f' +
                                ' sar-acc:%f sent-acc:%f\n')
                                % (sar_f1, sent_f1,
                                sar_acc, sent_acc))

            optimizer.zero_grad()
            loss.backward()
            optimizer.step()

        torch.save(model.state_dict(), stateSavePATH)
        torch.save(model, modelSavePATH)

def testModel(modelPATH):
    testData = MustardDataset(datatye='test')
    batchsize = 32
    data_loader = DataLoader(
        testData,
        batch_size=batchsize,
        shuffle=True,
        pin_memory=True
        )
    classmodel=torch.load(ClassModelPath)

    model=torch.load(modelPATH)
    model.eval()
    classmodel.eval()
    with torch.no_grad():
        outputsar, outputsent, outputemo = [], [], []
        tarsar, tarsent, taremo = [], [], []
        for batch in data_loader:
            textInput = batch[0][0].cuda().to(torch.float32)
            imageInput = batch[0][1].cuda().to(torch.float32)
            wavInput = batch[0][2].cuda().to(torch.float32)

            sarLabel = batch[1][0].to(torch.float32).cuda()
            sentLabel = batch[1][1].to(torch.float32).cuda()
            emoLabel = batch[1][2].to(torch.float32).cuda()
            with torch.no_grad():
                sar, sent=classmodel(textInput, wavInput)
            inq=torch.cat((sar,sent),dim=1).cuda()
            sar,sent=model(inq)

            label_sar = np.argmax(
                sarLabel.cpu().detach().numpy(), axis=-1)
            label_sent = np.argmax(
                sentLabel.cpu().detach().numpy(), axis=-1)

            pred_sar = np.argmax(
                sar.cpu().detach().numpy(), axis=1)
            pred_sent = np.argmax(
                sent.cpu().detach().numpy(), axis=1)

            outputsar.append(pred_sar)
            outputsent.append(pred_sent)

            tarsar.append(label_sar)
            tarsent.append(label_sent)

        outputsar = np.concatenate(
            np.array(outputsar, dtype=object))
        outputsent = np.concatenate(
            np.array(outputsent, dtype=object))

        tarsar = np.concatenate(
            np.array(tarsar, dtype=object))
        tarsent = np.concatenate(
            np.array(tarsent, dtype=object))

        sar_f1 = f1_score(
            tarsar, outputsar, average='micro')
        sent_f1 = f1_score(
            tarsent, outputsent, average='micro')

        sar_acc = accuracy_score(
            tarsar, outputsar)
        sent_acc = accuracy_score(
            tarsent, outputsent)

        print('test tarsar:', tarsar)
        print('test outputsar:', outputsar)
        print('test tarsent:', tarsent)
        print('test outputsent:', outputsent)
        logger.info(('test-result sar-f1:%f sent-f1:%f' +
                    'sar-acc:%f sent-acc:%f \n')
                    % (sar_f1, sent_f1,
                    sar_acc, sent_acc))

if __name__ == "__main__":
    logging.basicConfig(filemode='w')
    logger = logging.getLogger(__name__)
    logger.setLevel(level=logging.INFO)
    handler = logging.FileHandler(
        "result/log/new_log.txt")
    handler.setLevel(logging.INFO)
    formatter = logging.Formatter(
        '%(asctime)s - %(name)s - %(levelname)s - %(message)s')
    handler.setFormatter(formatter)
    logger.addHandler(handler)

    logger.info("**********Start print log**********")

    trainEval()
    # testModel('result/state/10_Epochs_2023-06-18-05-41-09/model.pt')


import cv2
import time
import math
import numpy as np

# my modules import
import hand_tracking_module as htm

cam_width, cam_height = 640, 480

cap = cv2.VideoCapture(0)
cap.set(3, cam_width)
cap.set(4, cam_height)
prev_time = 0

detector = htm.handleDetector(detection_conf=0.5)

while True:
    success, img = cap.read()
    img = detector.locateHands(img)
    lm_list = detector.locatePosition(img)
    if len(lm_list) != 0:
        # get tips of both thumb and index => 4and8 respectively
        print(lm_list[4], lm_list[8])

        x1, y1 = lm_list[4][1], lm_list[4][2]
        x2, y2 = lm_list[8][1], lm_list[8][2]

        # get center of line
        cx, cy = (x1 + x2) // 2, (y1 + y2) // 2

        cv2.circle(img, (x1, y1), 10, (255, 0, 255), cv2.FILLED)
        cv2.circle(img, (x2, y2), 10, (255, 0, 255), cv2.FILLED)
        cv2.line(img, (x1, y1), (x2, y2), (255, 0, 255), 3)
        cv2.circle(img, (cx, cy), 10, (255, 0, 255), cv2.FILLED)

        # get length of line
        length = math.hypot(x2-x1,y2-y1)
        print(length)

        if length < 50:
            cv2.circle(img, (cx, cy), 10, (0, 255,0), cv2.FILLED)

    curr_time = time.time()
    fps = 1 / (curr_time - prev_time)
    prev_time = curr_time

    cv2.putText(img, f"fps{int(fps)}", (40, 70), cv2.FONT_HERSHEY_COMPLEX, 1, (255, 0, 255), 3)

    cv2.imshow("ImageGC", img)
    cv2.waitKey(1)


from django.db.models.signals import post_save, post_init
from django.dispatch import receiver

from ico.models import Phase, Rate

@receiver(post_save, sender=Phase)
def create_rate(sender, instance, created, **kwargs):
    """
    Automatically create Rate objects foor all company currencies when a
    new Phase is successfully created
    """
    if not created:
        return

    for currency in instance.ico.company.currency_set.all():
        rate, _ = Rate.objects.get_or_create(phase=instance, currency=currency)
        rate.refresh_rate()


"""
@Author: Alex Nguyen
Gettysburg College
"""
# %%

from __future__ import print_function, division
import os
import gc
import torch
import pandas as pd
from skimage import io, transform
import numpy as np
import matplotlib.pyplot as plt
from torch.utils.data import Dataset, DataLoader
from torchvision import utils
from torchvision import transforms as T

import faiss

import cv2
import tensorflow as tf
from tensorflow.keras.applications import EfficientNetB0
print('TF',tf.__version__)

from sklearn.decomposition import PCA
from sklearn.feature_extraction.text import TfidfVectorizer, CountVectorizer

# Ignore warnings
import warnings
warnings.filterwarnings("ignore")

plt.ion()   # interactive mode

from core.data import ShopeeDataset, ShopeeTorch

DATASET_PATH = "./dataset/shopee-product-matching"
ds = ShopeeDataset(DATASET_PATH)

# Dataset shortcut
# All data column is string excpet for label_group
# is integer
posting_id = "posting_id"
image_path = "image"
phash = "image_phash"
title = "title"
label_group = "label_group"

def get_text_embeddings(df, max_features = 15000, n_components = 5000, verbose: bool=False):
    model = TfidfVectorizer(binary = True, max_features = max_features)
    text_embeddings = model.fit_transform(df['title']).toarray()
    pca = PCA(n_components = n_components)
    text_embeddings = pca.fit_transform(text_embeddings).get()
    if verbose:
        print(f'Our title text embedding shape is {text_embeddings.shape}')
    del model, pca
    gc.collect()
    return text_embeddings

IMG_FOLDER = os.path.join(DATASET_PATH, "train_images")

def displayDF(train, random=False, COLS=6, ROWS=4, path=IMG_FOLDER):
    for k in range(ROWS):
        plt.figure(figsize=(20,5))
        for j in range(COLS):
            if random: row = np.random.randint(0,len(train))
            else: row = COLS*k + j
            name = train.iloc[row,1]
            title = train.iloc[row,3]
            title_with_return = ""
            for i,ch in enumerate(title):
                title_with_return += ch
                if (i!=0)&(i%20==0): title_with_return += '\n'
            img = cv2.imread(os.path.join(path, name))
            plt.subplot(1,COLS,j+1)
            plt.title(title_with_return)
            plt.axis('off')
            plt.title(title_with_return)
            plt.axis('off')
            plt.imshow(img)
        plt.show()

# %%

# Display counts

groups = ds.df.label_group.value_counts()
plt.figure(figsize=(20,5))
plt.plot(np.arange(len(groups)),groups.values)
plt.ylabel('Duplicate Count',size=14)
plt.xlabel('Index of Unique Item',size=14)
plt.title('Duplicate Count vs. Unique Item Count',size=16)
plt.show()

plt.figure(figsize=(20,5))
plt.bar(groups.index.values[:50].astype('str'),groups.values[:50])
plt.xticks(rotation = 45)
plt.ylabel('Duplicate Count',size=14)
plt.xlabel('Label Group',size=14)
plt.title('Top 50 Duplicated Items',size=16)
plt.show()

for k in range(5):
    print('#'*40)
    print('### TOP %i DUPLICATED ITEM:'%(k+1),groups.index[k])
    print('#'*40)
    top = ds.df.loc[ds.df.label_group==groups.index[k]]
    displayDF(top, random=False, ROWS=2, COLS=4)

# %%

displayDF(ds.df, random=True)

# %%

ds.df.head()

# %%

ds[label_group]
ds.df.head()

# %%

"""
Reference: https://www.kaggle.com/cdeotte/rapids-cuml-tfidfvectorizer-and-knn

"""

# %%

model = TfidfVectorizer(stop_words='english', binary=True)
text_embeddings = model.fit_transform(ds.df.title).toarray()
print('text embeddings shape is',text_embeddings.shape)

# %%

dim = text_embeddings.shape[1]
n = text_embeddings.shape[0]

xb = np.random.normal(size=(n, dim)) # normal distribution with mean 0 std 1

index = faiss.IndexFlatL2(dim)   # build the index, d=size of vectors
# here we assume xb contains a n-by-d numpy matrix of type float32
index.add(xb)                  # add vectors to the index
print(index.ntotal)

# %%

# list of text documents
text = ["The quick brown fox jumped over the lazy dog."]
# create the transform
vectorizer = CountVectorizer()
# tokenize and build vocab
vectorizer.fit(text)
# summarize
print(vectorizer.vocabulary_)
# encode document
vector = vectorizer.transform(text)
# summarize encoded vector
print(vector.shape)
print(type(vector))
print(vector.toarray())

# %%

from sklearn.neighbors import NearestNeighbors
KNN = 50
model = NearestNeighbors(n_neighbors=KNN)
model.fit(text_embeddings)
distances, indices = model.kneighbors(text_embeddings)

# %%

knn_model_folder = "./models/knn"
np.save(os.path.join(knn_model_folder, "distances_50_centroids.npy"), distances)
np.save(os.path.join(knn_model_folder, "indices_50_centroids.npy"), indices)

# %%

###############  Image training Pytorch

# configs:

torch_train_dataset = ShopeeTorch()

# Number of classes in the dataset
num_classes = torch_train_dataset.n_labels()

# Flag for feature extracting. When False, we finetune the whole model,
#   when True we only update the reshaped layer params
feature_extract = True

data_transforms = {
    'train': T.Compose([
        T.RandomResizedCrop(224),
        T.RandomHorizontalFlip(),
        T.ToTensor(),
        T.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
    ]),
    'val': T.Compose([
        T.Resize(256),
        T.CenterCrop(224),
        T.ToTensor(),
        T.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
    ]),
}

def data_transforms_2(image_size: int) -> (object, object):
    '''
        Return transformations to be applied.
        Input:
            image_size: int
        Output:
            train_transformations: transformations to be applied on the training set
            valid_tfms: transformations to be applied on the validation or test set
    '''

    # imagenet_stats = ([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
    # https://www.kaggle.com/ronaldokun/multilabel-stratification-cv-and-ensemble

    mean = torch.tensor([0.05438065, 0.05291743, 0.07920227])
    std = torch.tensor([0.39414383, 0.33547948, 0.38544176])

    train_trans = [
        T.Resize(image_size + 4),
        T.CenterCrop(image_size),
        T.RandomRotation(40),
        T.RandomAffine(
            degrees=10,
            translate=(0.01, 0.12),
            shear=(0.01, 0.03),
        ),
        T.RandomHorizontalFlip(),
        T.RandomVerticalFlip(),
        T.ToTensor(),
        T.Normalize(mean, std, inplace=True),
        T.RandomErasing(inplace=True)
    ]

    val_trans = [
        T.Resize(image_size),
        T.ToTensor(),
        T.Normalize(mean, std, inplace=True)
    ]

    train_transformations = T.Compose(train_trans)
    valid_tfms = T.Compose(val_trans)

    return train_transformations, valid_tfms

# pretrained models
from efficientnet_pytorch import EfficientNet

# Models to choose from [resnet, alexnet, vgg, squeezenet, densenet, inception]
model_name = "squeezenet"

def initialize_model(model_name, num_classes, feature_extract, use_pretrained=True):
    # Initialize these variables which will be set in this if statement. Each of these
    #   variables is model specific.
    model_ft = None
    input_size = 0

    if model_name == "resnet":
        """ Resnet18
        """
        model_ft = models.resnet18(pretrained=use_pretrained)
        set_parameter_requires_grad(model_ft, feature_extract)
        num_ftrs = model_ft.fc.in_features
        model_ft.fc = nn.Linear(num_ftrs, num_classes)
        input_size = 224

    elif model_name == "alexnet":
        """ Alexnet
        """
        model_ft = models.alexnet(pretrained=use_pretrained)
        set_parameter_requires_grad(model_ft, feature_extract)
        num_ftrs = model_ft.classifier[6].in_features
        model_ft.classifier[6] = nn.Linear(num_ftrs,num_classes)
        input_size = 224

    elif model_name == "vgg":
        """ VGG11_bn
        """
        model_ft = models.vgg11_bn(pretrained=use_pretrained)
        set_parameter_requires_grad(model_ft, feature_extract)
        num_ftrs = model_ft.classifier[6].in_features
        model_ft.classifier[6] = nn.Linear(num_ftrs,num_classes)
        input_size = 224

    elif model_name == "squeezenet":
        """ Squeezenet
        """
        model_ft = models.squeezenet1_0(pretrained=use_pretrained)
        set_parameter_requires_grad(model_ft, feature_extract)
        model_ft.classifier[1] = nn.Conv2d(512, num_classes, kernel_size=(1,1), stride=(1,1))
        model_ft.num_classes = num_classes
        input_size = 224

    elif model_name == "densenet":
        """ Densenet
        """
        model_ft = models.densenet121(pretrained=use_pretrained)
        set_parameter_requires_grad(model_ft, feature_extract)
        num_ftrs = model_ft.classifier.in_features
        model_ft.classifier = nn.Linear(num_ftrs, num_classes)
        input_size = 224

    elif model_name == "inception":
        """ Inception v3
        Be careful, expects (299,299) sized images and has auxiliary output
        """
        model_ft = models.inception_v3(pretrained=use_pretrained)
        set_parameter_requires_grad(model_ft, feature_extract)
        # Handle the auxilary net
        num_ftrs = model_ft.AuxLogits.fc.in_features
        model_ft.AuxLogits.fc = nn.Linear(num_ftrs, num_classes)
        # Handle the primary net
        num_ftrs = model_ft.fc.in_features
        model_ft.fc = nn.Linear(num_ftrs,num_classes)
        input_size = 299

    else:
        print("Invalid model name, exiting...")
        exit()

    return model_ft, input_size

# Transfer learning model
models = {
#     "EfficientNet-b0": EfficientNet.from_pretrained('efficientnet-b0'),
    "EfficientNet-b1": EfficientNet.from_pretrained('efficientnet-b1'),
#     "EfficientNet-b2": EfficientNet.from_pretrained('efficientnet-b2'),
#     "EfficientNet-b3": EfficientNet.from_pretrained('efficientnet-b3'),
}

# There are these model [resnet, alexnet, vgg, squeezenet, densenet, inception]
image_sizes = {
    "EfficientNet-b0": 224,
    "EfficientNet-b1": 240,
    "EfficientNet-b2": 260,
    'EfficientNet-b3': 300,
    'EfficientNet-b4': 380,
}

batch_sizes = {
    "EfficientNet-b0": 150,
    "EfficientNet-b1": 100,
    "EfficientNet-b2": 64,
    'EfficientNet-b3': 50,
    'EfficientNet-b4': 20
}

# %%

# Initialize the model for this run
model_ft, input_size = initialize_model(model_name, num_classes, feature_extract, use_pretrained=True)

# Print the model we just instantiated
print(model_ft)

# %%


# coding: utf-8

from __future__ import unicode_literals, absolute_import, print_function, division

import os
import numpy as np
import pandas as pd
import logging

logger = logging.getLogger('stata')

# flake8: noqa: C901
def use_data_actions(df, actions, working_dir=None):
    for action in actions:
        if action['type'] == 'select':
            df = df[action['columns']]
            logger.info(f'use_data_config df: action={action["type"]} shape={df.shape}')
        elif action['type'] == 'filter_by_contains':
            df = df[df[action['field']].str.contains(action['value'])]
            logger.info(f'use_data_config df: action={action["type"]} shape={df.shape}')
        elif action['type'] == 'extend_by_field':
            _items = []
            for _, row in df.iterrows():
                value = row[action['field']]
                extend_values = [i.strip() for i in str(value).split(',') if i.strip() and i.strip() != 'nan']
                for v in extend_values:
                    _items.append({
                        **row,
                        action['new_field']: v
                    })
            df = pd.DataFrame.from_records(_items)
            logger.info(f'use_data_config df: action={action["type"]} shape={df.shape}')
        elif action['type'] == 'filter_by_value':
            df = df[df[action['field']] == action['value']]
            logger.info(f'use_data_config df: action={action["type"]} shape={df.shape}')
        elif action['type'] == 'filter':
            if action['filter_type'] == '>':
                df = df[df[action['field']] > action['value']]
            elif action['filter_type'] == '>=':
                df = df[df[action['field']] >= action['value']]
            elif action['filter_type'] == '<':
                df = df[df[action['field']] < action['value']]
            elif action['filter_type'] == '<=':
                df = df[df[action['field']] <= action['value']]
            elif action['filter_type'] == '=':
                df = df[df[action['field']] == action['value']]
            else:
                raise ValueError(action['filter_type'])
            logger.info(f'use_data_config df: action={action["type"]} shape={df.shape}')
        elif action['type'] == 'rename':
            df = df.rename(columns={**{k: k for k in df.columns}, **action['columns_map']})
            logger.info(f'use_data_config df: action={action["type"]} shape={df.shape}')
        elif action['type'] == 'dropna':
            df = df.dropna(subset=action['columns'])
            logger.info(f'use_data_config df: action={action["type"]} shape={df.shape}')
        elif action['type'] == 'map_true_false':
            df.replace({False: 0, True: 1}, inplace=True)
            logger.info(f'use_data_config df: action={action["type"]} shape={df.shape}')
        elif action['type'] == 'map_value':
            df[action['field']] = df[action['field']].replace(action['values_map'])
        elif action['type'] == 'create_by_apply':

            def _test(condition, value):
                if condition[0] == '>':
                    return value > condition[1]
                elif condition[0] == '>=':
                    return value >= condition[1]
                elif condition[0] == '<':
                    return value < condition[1]
                elif condition[0] == '<=':
                    return value <= condition[1]
                elif condition[0] == '=':
                    return value == condition[1]
                else:
                    return False

            def _f(row):
                value = row[action['field']]
                for target, conditions in action['values_map'].items():
                    if all(_test(condition, value) for condition in conditions):
                        return target
                return action.get('value_default', value)

            df[action['new_field']] = df.apply(_f, axis=1)

        elif action['type'] == 'df.apply.column_percent':
            _sum = df[action['field']].sum()
            df[action['new_field']] = df[action['field']].apply(lambda x: x / _sum)

        elif action['type'] == 'pd.pivot_table':
            params = action['params']
            _table = pd.pivot_table(
                df[action['fields']],
                values=params.get('values'),
                index=params.get('index'),
                columns=params.get('columns'),
                aggfunc={
                    'count': len,
                    'sum': np.sum,
                    'mean': np.mean,
                }.get(params.get('aggfunc'), len),
            )
            if action.get("actions"):
                _table = use_data_actions(_table, action['actions'], working_dir)
            if working_dir:
                _table.to_csv(os.path.join(working_dir, action['output']))
            return _table

    return df

def use_data_config(data, working_dir):
    df = pd.read_csv(data['from'], low_memory=False)
    logger.info(f'use_data_config df: start shape={df.shape}')

    df = use_data_actions(df, data['actions'], working_dir)
    df.to_csv(os.path.join(working_dir, 'use-data.csv'), index=False)
    return df


import re,os
from tqdm import tqdm
from pypinyin.contrib.tone_convert import to_tone

path=os.path.abspath('create-database\cedict_1_0_ts_utf-8_mdbg.txt')
opath=os.path.abspath('create-database\cedict_1_0_ts_utf-8_mdbg.csv')

with open(path, 'r',encoding='utf-8-sig') as file:
    with open(opath, 'w',encoding='utf-8-sig') as ofile:
        lines = file.readlines()
        for line in tqdm(lines):
            line=line.lower()
            a=[i+1 for i,x in enumerate(line) if x=='[']
            b=[i for i,x in enumerate(line) if x==']']
            c=[line[a[i]:b[i]] for i in range(len(a))]

            for i in range(len(c)):
                b=list(c[i].split(" "))
                e= ''.join(to_tone(i)+' ' if index != (len(b)-1) else to_tone(i) for index,i in enumerate(b)  )
                e=f"[{e}]"
                # print("e",e)
                f= ''.join((i)+' ' if index != (len(b)-1) else i for index,i in enumerate(b)  )
                f=f"\[{f}\]"
                # print("f",f)
                line=re.sub(f,e,line)
                # print("line A",line)

            line = re.sub(",",";",line)
            line = re.sub("\s",",",line,2)
            line = re.sub(",,",",",line)
            line = re.sub("] ","],",line,1)

            ofile.write(line)


import os
import sys
import tempfile
import subprocess
import argparse
import shutil
import nibabel as nib
from scipy import ndimage as nd
from nipype.interfaces import fsl
from pkg_resources import resource_filename, Requirement

def run_shell_cmd(cmd, cwd=[]):
    """
    Run a command in the shell using Popen
    """
    if cwd:
        process = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, cwd=cwd)
    else:
        process = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
    for line in process.stdout:
        print(line.strip())

    process.wait()

def main():

    # T1w template and face mask locations
    T1w_template = resource_filename(Requirement.parse("pydeface"), "pydeface/data/ConteCore2_50_T1w_2mm.nii.gz")
    facemask = resource_filename(Requirement.parse("pydeface"), "pydeface/data/ConteCore2_50_T1w_2mm_deface_mask.nii.gz")

    try:
        assert os.path.exists(T1w_template)
    except:
        raise Exception('*** Missing template {}'.format(T1w_template))

    try:
        assert os.path.exists(facemask)
    except:
        raise Exception('*** Missing facemask {}'.format(facemask))

    # Check that FSLDIR is set
    if 'FSLDIR' in os.environ:
        FSLDIR=os.environ['FSLDIR']
    else:
        print('FSL must be installed and FSLDIR environment variable must be defined')
        sys.exit(2)

    # Command line argument parser
    parser = argparse.ArgumentParser(description='Remove facial structure from MRI images')
    parser.add_argument('-i', '--infile', required=True,
                        help='T1w input image (Nifti or Minc')
    parser.add_argument('-o', '--outfile', required=False,
                        help='Defaced output image [<infile>_defaced.<ext>]')
    parser.add_argument('-s', '--scalefactor', required=False,
                        help='Voxelation scale factor [8.0]')
    parser.add_argument('-im', '--inmask', required= False,
                        help='Use this Nifti face mask to perform defacing')
    parser.add_argument('-om', '--outmask', required= False,
                        help='Save registered face mask to this Nifti file')
    parser.add_argument('-r', '--replace', required=False, default=False, action='store_true',
                        help='Backup original image and replace with defaced version')
    parser.add_argument('--overwrite', required=False, default=False, action='store_true',
                        help='Overwrite existing defaced output')

    # Parse command line arguments
    args = parser.parse_args()
    in_fname = args.infile

    print('Input faced image    : {}'.format(in_fname))

    # Save full image extension
    # Handle double-extension for compressed Nifti files
    if not in_fname.endswith('.nii.gz'):
        print('* pydeface currently only supports .nii.gz images - exiting')
        sys.exit(1)

    # Output defaced image filename
    if args.outfile:
        out_fname = args.outfile
    else:
        out_fname = in_fname.replace('.nii.gz', '_defaced.nii.gz')

    # Protect existing output file
    if os.path.isfile(out_fname) and not args.overwrite:
        print('{} already exists - remove it first'.format(out_fname))
        sys.exit(1)

    print('Output defaced image : {}'.format(out_fname))

    if args.scalefactor:
        vox_sf = float(args.scalefactor)
    else:
        vox_sf = 8.0

    print('Defacing {}'.format(in_fname))

    # Load input image
    print('Loading {}'.format(in_fname))
    in_nii = nib.load(in_fname)
    in_img = in_nii.get_fdata()

    # Voxelate input image
    # 1. Cubic downsample
    # 2. Nearest neighbor upsample
    print('Voxelating faced image : scale factor %0.1f' % vox_sf)

    print('  Spline downsampling')
    in_vox_img = nd.interpolation.zoom(in_img, zoom=1.0/vox_sf, order=3)

    print('  Nearest neighbor upsampling')
    in_vox_img = nd.interpolation.zoom(in_vox_img, zoom=vox_sf, order=0)

    if args.inmask:

        # Load precalculated individual space face mask
        ind_deface_mask_nii = nib.load(args.inmask)
        ind_deface_mask_img = ind_deface_mask_nii.get_fdata()

    else:

        # Create temporary directory for FLIRT outputs
        tmp_dir_obj = tempfile.TemporaryDirectory()
        tmp_dir = tmp_dir_obj.name
        flirt_tx = os.path.join(tmp_dir, 'flirt_tx.mat')
        dummy_mat = os.path.join(tmp_dir, 'dummy.mat')
        dummy_img = os.path.join(tmp_dir, 'dummy.nii.gz')
        deface_mask_fname = os.path.join(tmp_dir, 'deface_mask.nii.gz')

        # Register template to infile
        print('Registering template to individual space')
        flirt = fsl.FLIRT()
        flirt.inputs.cost_func='mutualinfo'
        flirt.inputs.in_file = T1w_template
        flirt.inputs.out_matrix_file = flirt_tx
        flirt.inputs.reference = in_fname
        flirt.inputs.out_file = dummy_img
        flirt.terminal_output='none'
        flirt.run()

        # Affine transform facemask to infile
        print('Resampling face mask to individual space')
        flirt = fsl.FLIRT()
        flirt.inputs.in_file = facemask
        flirt.inputs.in_matrix_file = flirt_tx
        flirt.inputs.apply_xfm = True
        flirt.inputs.reference = in_fname
        flirt.inputs.out_file = deface_mask_fname
        flirt.inputs.out_matrix_file = dummy_mat
        flirt.terminal_output='none'
        flirt.run()

        # Load computed individual space deface mask
        ind_deface_mask_nii = nib.load(deface_mask_fname)
        ind_deface_mask_img = ind_deface_mask_nii.get_fdata()

    # Replace face area with voxelated version
    # Note that the face mask is 0 in the face region, 1 elsewhere.
    print('Anonymizing face area')
    out_img = in_img * ind_deface_mask_img + in_vox_img * (1 - ind_deface_mask_img)

    # Save defaced image
    print('Saving defaced image to {}'.format(out_fname))
    defaced_nii = nib.Nifti1Image(out_img, in_nii.affine, in_nii.header)
    defaced_nii.to_filename(out_fname)

    # Backup and replace original if requested
    if args.replace:

        bak_fname = in_fname.replace('.nii.gz', '_bak.nii.gz')

        print('Backup up {} to {}'.format(in_fname, bak_fname))
        shutil.move(in_fname, bak_fname)

        print('Renaming {} to {}'.format(out_fname, in_fname))
        shutil.move(out_fname, in_fname)

    # Save mask if requested
    if args.outmask:
        print('Saving registered face mask to {}'.format(args.outmask))
        ind_deface_mask_nii.to_filename(args.outmask)

if __name__ == "__main__":
    main()


import uuid

from django.conf import settings
from django.test import TestCase
from unittest.mock import MagicMock

from pillow_retry.models import PillowError
from pillowtop import get_all_pillow_configs
from pillowtop.dao.exceptions import DocumentMissingError
from pillowtop.feed.interface import Change

class ExceptionA(Exception):
    pass

class PillowtopRetryAllPillowsTests(TestCase):
    @classmethod
    def setUpClass(cls):
        super(PillowtopRetryAllPillowsTests, cls).setUpClass()
        cls._PILLOWTOPS = settings.PILLOWTOPS
        if not settings.PILLOWTOPS:
            # assumes HqTestSuiteRunner, which blanks this out and saves a copy here
            settings.PILLOWTOPS = settings._PILLOWTOPS

    @classmethod
    def tearDownClass(cls):
        settings.PILLOWTOPS = cls._PILLOWTOPS
        super(PillowtopRetryAllPillowsTests, cls).tearDownClass()

    def tearDown(self):
        PillowError.objects.all().delete()

    def test_all_pillows_handle_errors(self):
        all_pillow_configs = list(get_all_pillow_configs())
        for pillow_config in all_pillow_configs:
            self._test_error_logging_for_pillow(pillow_config)

    def _test_error_logging_for_pillow(self, pillow_config):
        pillow = _pillow_instance_from_config_with_mock_process_change(pillow_config)
        if pillow.retry_errors:
            exc_class = Exception
            exc_class_string = 'builtins.Exception'
        else:
            exc_class = DocumentMissingError
            exc_class_string = 'pillowtop.dao.exceptions.DocumentMissingError'

        pillow.process_change = MagicMock(side_effect=exc_class(pillow.pillow_id))
        doc = self._get_random_doc()
        pillow.process_with_error_handling(
            Change(id=doc['id'], sequence_id='3', document=doc),
        )

        errors = PillowError.objects.filter(pillow=pillow.pillow_id).all()
        self.assertEqual(1, len(errors), pillow_config)
        error = errors[0]
        self.assertEqual(error.doc_id, doc['id'], pillow_config)
        self.assertEqual(exc_class_string, error.error_type)
        self.assertIn(pillow.pillow_id, error.error_traceback)

    def _get_random_doc(self):
        return {
            'id': uuid.uuid4().hex,
            'doc_type': 'CommCareCase',
            'type': 'mother',
            'domain': 'pillow-retry-domain',
        }

def _pillow_instance_from_config_with_mock_process_change(pillow_config):
    pillow_class = pillow_config.get_class()
    if pillow_config.instance_generator is None:
        instance = pillow_class()
    else:
        instance = pillow_config.get_instance()

    return instance


from operator import add
from unicodedata import name
from django.urls import path
from .views import (
    add_doctor,
    delete_doctor,
    doctors,
    dashboard,
    update_doctor,
    update_doctor_page,
    doctors_report,

)

urlpatterns = [

    path('doctordashboard',dashboard,name='doctordashboard'),
    path('add-doctor', add_doctor, name='add-doctor'),
    path('doctors', doctors, name='doctors'),
    path('update-doctor/?<id>',update_doctor_page, name='update-doctor'),
    path('updatedoctor/?<id>', update_doctor, name='updatedoctor'),
    path('doctors-report', doctors_report, name='doctors-report'),
    path('delete-doctor/?<id>', delete_doctor, name='delete-doctor'),

]


import sys
from io import StringIO
import unittest

class TestClass(unittest.TestCase):
    def assertIO(self, input, output):
        stdout, stdin = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(input)
        resolve()
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, stdin
        self.assertEqual(out, output)

    def test_Sample_Input_1(self):
        input = """accept"""
        output = """3"""
        self.assertIO(input, output)

    def test_Sample_Input_2(self):
        input = """atcoder"""
        output = """0"""
        self.assertIO(input, output)

    def test_Sample_Input_3(self):
        input = """anerroroccurred"""
        output = """16"""
        self.assertIO(input, output)

    def test_Sample_Input_4(self):
        input = """aacbbbc"""
        output = """6"""
        self.assertIO(input, output)

    def test_Sample_Input_5(self):
        input = """aacbbbcc"""
        output = """8"""
        self.assertIO(input, output)

def resolve():
  from collections import defaultdict
  # 右から順に数えていく。
  # インデックスを減らしていくのは少し手間なので逆順に並べ替え
  S = list(reversed(list(input())))
  ans = 0
  # 現在見ている文字より左側の文字をカウントする。
  count = defaultdict(int)
  count[S[0]] += 1
  for i in range(1, len(S)):
    if S[i-1] == S[i]:
      # 変更する文字を数える。(現在のインデックスから左側の文字の数 - 現在のインデックスから左側に含まれる現在見ている文字と同じ文字の数)
      ans += i - count[S[i]]
      # 現在の index より左側は全て同じ文字になるため、 count をクリアして現在見てる文字分カウントする。
      count.clear()
      count[S[i]] += i+1
    else:
      count[S[i]] += 1
  print(ans)

resolve()

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


from django.shortcuts import render, HttpResponse, redirect
from main.models import *
from django.contrib import messages
from django.http import JsonResponse
from django.db.models import Q
import bcrypt
from main.decorators import loginauth

# Create your views here.

def friends(request):
    context= {
        'usuario': User.objects.get(id=request.session['user']),
        'usuarios': User.objects.all().exclude(id=request.session['user']),
        'amigos': User.objects.get(id=request.session['user']).friends.all()
    }
    return render(request, 'friends.html',context)

def profile(request, id):
    context= {
        'usuario': User.objects.get(id=request.session['user']),
        'profile': User.objects.get(id=id)
    }
    return render(request, 'profile.html', context)

def newfriend(request):
    userA= User.objects.get(id=request.session['user'])
    userB= User.objects.get(id=request.POST['id'])
    userA.friends.add(userB)
    userB.friends.add(userA)
    return redirect('/friends')

def delete(request):
    userA= User.objects.get(id=request.session['user'])
    userB= User.objects.get(id=request.POST['id'])
    userA.friends.remove(userB)
    return redirect('/friends')


from yiku.nets.BackBone import hgnetv2l,hgnetv2x
from .bubbliiiing_pspnet import _PSPModule
from torch import nn
import torch.nn.functional as F
class pspNet(nn.Module):
    def __init__(self, num_classes, downsample_factor, backbone="hgnetv2l", pretrained=True, aux_branch=False):
        super(pspNet, self).__init__()
        norm_layer = nn.BatchNorm2d
        if backbone == "hgnetv2l":
            self.backbone = hgnetv2l(pretrained=pretrained)
            aux_channel = self.backbone.low_ch
            out_channel = self.backbone.feature_ch
        elif backbone == "mobilenet":pass
            # ----------------------------------#
            #   获得两个特征层
            #   f4为辅助分支    [30,30,96]
            #   o为主干部分     [30,30,320]
            # ----------------------------------#
            # self.backbone = MobileNetV2(downsample_factor, pretrained)
            # aux_channel = 96
            # out_channel = 320
        else:
            raise ValueError('Unsupported backbone - `{}`, Use mobilenet, resnet50.'.format(backbone))

        # --------------------------------------------------------------#
        #	PSP模块，分区域进行池化
        #   分别分割成1x1的区域，2x2的区域，3x3的区域，6x6的区域
        #   30,30,320 -> 30,30,80 -> 30,30,21
        # --------------------------------------------------------------#
        self.master_branch = nn.Sequential(
            _PSPModule(out_channel, pool_sizes=[1, 2, 3, 6], norm_layer=norm_layer),
            nn.Conv2d(out_channel // 4, num_classes, kernel_size=1)
        )

        self.aux_branch = aux_branch

        if self.aux_branch:
            # ---------------------------------------------------#
            #	利用特征获得预测结果
            #   30, 30, 96 -> 30, 30, 40 -> 30, 30, 21
            # ---------------------------------------------------#
            self.auxiliary_branch = nn.Sequential(
                nn.Conv2d(aux_channel, out_channel // 8, kernel_size=3, padding=1, bias=False),
                norm_layer(out_channel // 8),
                nn.ReLU(inplace=True),
                nn.Dropout2d(0.1),
                nn.Conv2d(out_channel // 8, num_classes, kernel_size=1)
            )

        self.initialize_weights(self.master_branch)

    def forward(self, x):
        input_size = (x.size()[2], x.size()[3])
        x_aux, x = self.backbone(x)
        output = self.master_branch(x)
        output = F.interpolate(output, size=input_size, mode='bilinear', align_corners=True)
        if self.aux_branch:
            output_aux = self.auxiliary_branch(x_aux)
            output_aux = F.interpolate(output_aux, size=input_size, mode='bilinear', align_corners=True)
            return output_aux, output
        else:
            return output

    def initialize_weights(self, *models):
        for model in models:
            for m in model.modules():
                if isinstance(m, nn.Conv2d):
                    nn.init.kaiming_normal_(m.weight.data, nonlinearity='relu')
                elif isinstance(m, nn.BatchNorm2d):
                    m.weight.data.fill_(1.)
                    m.bias.data.fill_(1e-4)
                elif isinstance(m, nn.Linear):
                    m.weight.data.normal_(0.0, 0.0001)
                    m.bias.data.zero_()


from PyQt5.QtWidgets import QWidget, QHBoxLayout, QPushButton, QLineEdit, QCalendarWidget
from src.views.widgets.CustomLabel import RegularLabel
from src.models.SessionWrapper import SessionWrapper
from googleapiclient.discovery import build
import os.path
from google.auth.transport.requests import Request
from google_auth_oauthlib.flow import InstalledAppFlow
import pickle
import datetime

class MyCalendarForm(QWidget):
    def __init__(self, parent, **kwargs):
        super().__init__()
        self.setObjectName("my_calendar_page")
        self.parent = parent
        self.clinic_id = SessionWrapper.clinic_id
        self.pages_count = 6
        self.landing_layout = QHBoxLayout()
        self.landing_layout.setContentsMargins(0, 0, 0, 0) #(left, top, right, bottom)
        self.landing_layout.setSpacing(0)
        self.textbox = QLineEdit()
        self.textbox.move(20, 20)
        self.textbox.resize(200,50)
        self.pc_width = SessionWrapper.get_dimension('main_window_width')
        self.pc_height = SessionWrapper.get_dimension('main_window_height')
        self.initUI()

    def googleInit(self):
        SCOPES = ['https://www.googleapis.com/auth/calendar.readonly']
        api_cred = "resources/credentials.json"
        token_file = 'resources/token.pickle'
        creds = None
        # The file token.pickle stores the user's access and refresh tokens, and is
        # created automatically when the authorization flow completes for the first
        # time.
        if os.path.exists(token_file):
            with open(token_file, 'rb') as token:
                creds = pickle.load(token)
        # If there are no (valid) credentials available, let the user log in.
        if not creds or not creds.valid:
            if creds and creds.expired and creds.refresh_token:
                creds.refresh(Request())
            else:
                flow = InstalledAppFlow.from_client_secrets_file(
                    api_cred, SCOPES)
                creds = flow.run_local_server(port=0)
            # Save the credentials for the next run
            with open('resources/token.pickle', 'wb') as token:
                pickle.dump(creds, token)

        service = build('calendar', 'v3', credentials=creds)

        # Call the Calendar API
        now = datetime.datetime.utcnow().isoformat() + 'Z'  # 'Z' indicates UTC time
        print('Getting the upcoming 100 events')
        events_result = service.events().list(calendarId='primary', timeMin=now,
                                              maxResults=100, singleEvents=True,
                                              orderBy='startTime').execute()
        self.events = events_result.get('items', [])

        self.lista = []
        if not self.events:
            print('No upcoming events found.')
        for event in self.events:
            start = event['start'].get('dateTime', event['start'].get('date'))
            # print(start, event['summary'])
            print(event)
            self.lista.append(start + event['summary'])

    def on_click(self):
        self.textbox.setText(str([x for x in self.lista]).strip("']["))

    def initUI(self):
        # lbl = RegularLabel("This will be My Calendar page.. coming soon")
        test = RegularLabel(self.googleInit())
        # self.landing_layout.addWidget(lbl)
        self.landing_layout.addWidget(test)
        self.button = QPushButton("Apasa pentru a vedea daca ai evenimente in urmatoarele 10 zile")
        self.button.move(2, 20)
        self.setLayout(self.landing_layout)
        self.landing_layout.addWidget(self.button)
        self.landing_layout.addWidget(self.textbox)

        self.button.clicked.connect(self.on_click)
        print(self.lista)

        #Calendar

        calendar = QCalendarWidget()
        calendar.setGridVisible(True)
        calendar.move(20, 20)
        self.landing_layout.addWidget(calendar)


from utils.normalize import denormalize
import torch
import librosa
from args import stft_kwargs, mel_kwargs

def hum_to_guitar(h_s, model, db_mm,stft_kwargs, mel_kwargs, return_mel=False):
    model.eval()
    with torch.no_grad():
        h_ = model(h_s).detach().cpu().squeeze().numpy()
    h_ = denormalize(h_, db_mm)
    h_ = librosa.db_to_power(h_)
    sig_h = librosa.feature.inverse.mel_to_audio(h_,
                                                 center=True,
                                                 n_fft=stft_kwargs['n_fft'],hop_length=stft_kwargs['hop_length'], win_length=stft_kwargs['win_length'],
                                                 fmax=mel_kwargs['fmax'], fmin=mel_kwargs['fmin']
                                                 )
    if return_mel:
        return sig_h, h_
    else:
        return sig_h


from os import system, name
from time import sleep
import sys
import os.path
import json
import random
import requests

def clear():
    if name == 'nt':
        _ = system('cls')
    else:
        _ = system('clear')

def loadDict(dictFile):
    if not os.path.exists(dictFile):
        file = open(dictFile,"w")
        json.dump({},file)
        file.close()
    file = open(dictFile,"r")
    dictionary = json.load(file)
    file.close()
    return dictionary

def learn(dict,inp):
    tokens = inp.split(" ")
    for i in range(0,len(tokens)-1):
        currWord = tokens[i]
        nextWord = tokens[i+1]
        if currWord not in dict:
            dict[currWord] = {nextWord:1}
        else:
            # exist in dict
            allNextWords = dict[currWord]

            if nextWord not in allNextWords:
                #add new state
                dict[currWord][nextWord] = 1
            else:
                dict[currWord][nextWord] = dict[currWord][nextWord]+1
    return dict

def updateFile(fileName, dictionary):
    file = open(fileName,"w")
    json.dump(dictionary,file)
    file.close()
    print("--- OK - ZAPISANO ZAWARTOŚĆ SŁOWNIKA: "+fileName+" ---")

def getNextWord(lastWord,dictionary):
    if lastWord not in dictionary:
        #wez nowe
        newWord = randomWord(dictionary)
        return newWord
    else:
        candidates = dictionary[lastWord]
        candidatesNormalized = []

        for word in candidates:
            freq = candidates[word]
            for i in range(0,freq):
                candidatesNormalized.append(word)
        rnd = random.randint(0,len(candidatesNormalized)-1)
        return candidatesNormalized[rnd]

def randomWord(dict):
    randNum = random.randint(0,len(dict)-1)
    newWord = list(dict.keys())[randNum]
    return newWord


import os
import multiprocessing
from typing import Any
from torch.utils.data import Dataset, Subset, random_split
from torchvision import transforms
from torchvision.transforms import *
from PIL import Image

from base_dataset import MaskSKFSplitByProfileDataset, GenderLabels, AgeLabels
from label_changer import labelChanger

class KSSample():
    def __call__(self, *args: Any, **kwds: Any) -> Any:
        print('hello im kwansik')

class KSAgeDataset(MaskSKFSplitByProfileDataset):
    num_classes = 3

    def __init__(self, data_dir, mean=(0.548, 0.504, 0.479), std=(0.237, 0.247, 0.246), val_ratio=0.2):
        super().__init__(data_dir, mean, std, val_ratio)

    def __getitem__(self, index):
        assert self.transform is not None, ".set_tranform 메소드를 이용하여 transform 을 주입해주세요"

        image = self.read_image(index)
        image_transform = self.transform(image)

        age_label = self.get_age_label(index)

        return image_transform, age_label

class KSAgeAdjDataset(MaskSKFSplitByProfileDataset):
    num_classes = 3

    def __init__(self, data_dir, mean=(0.548, 0.504, 0.479), std=(0.237, 0.247, 0.246), val_ratio=0.2):
        super().__init__(data_dir, mean, std, val_ratio)

    def __getitem__(self, index):
        assert self.transform is not None, ".set_tranform 메소드를 이용하여 transform 을 주입해주세요"

        image = self.read_image(index)
        image_transform = self.transform(image)

        age_label = self.get_age_label(index)

        return image_transform, age_label

    def setup(self):
        profiles = os.listdir(self.data_dir)
        profiles = [profile for profile in profiles if not profile.startswith(".")]
        split_profiles = self._split_profile(profiles, self.val_ratio)

        cnt = 0
        for phase, indices in split_profiles.items():
            for _idx in indices:
                profile = profiles[_idx]
                img_folder = os.path.join(self.data_dir, profile)
                for file_name in os.listdir(img_folder):
                    _file_name, ext = os.path.splitext(file_name)
                    if _file_name not in self._file_names:  # "." 로 시작하는 파일 및 invalid 한 파일들은 무시합니다
                        continue

                    img_path = os.path.join(self.data_dir, profile, file_name)  # (resized_data, 000004_male_Asian_54, mask1.jpg)

                    id, gender, race, age = profile.split("_")

                    mask_label, gender, age = labelChanger(profile,_file_name, gender, age, self._file_names)

                    gender_label = GenderLabels.from_str(gender)
                    age_label = AgeLabels.from_number(self._adjust_age(int(age)) if phase == 'train' else age)

                    self.image_paths.append(img_path)
                    self.mask_labels.append(mask_label)
                    self.gender_labels.append(gender_label)
                    self.age_labels.append(age_label)

                    self.indices[phase].append(cnt)
                    cnt += 1

    def _adjust_age(self, age):
        if age >= 59:
            return 60
        else:
            return age

class KSGenderDataset(MaskSKFSplitByProfileDataset):
    num_classes = 2

    def __init__(self, data_dir, mean=(0.548, 0.504, 0.479), std=(0.237, 0.247, 0.246), val_ratio=0.2):
        super().__init__(data_dir, mean, std, val_ratio)

    def __getitem__(self, index):
        assert self.transform is not None, ".set_tranform 메소드를 이용하여 transform 을 주입해주세요"

        image = self.read_image(index)
        image_transform = self.transform(image)

        gender_label = self.get_gender_label(index)

        return image_transform, gender_label

class KSMaskDataset(MaskSKFSplitByProfileDataset):
    num_classes = 3
    def __init__(self, data_dir, mean=(0.548, 0.504, 0.479), std=(0.237, 0.247, 0.246), val_ratio=0.2):
        super().__init__(data_dir, mean, std, val_ratio)

    def __getitem__(self, index):
        assert self.transform is not None, ".set_tranform 메소드를 이용하여 transform 을 주입해주세요"

        image = self.read_image(index)
        image_transform = self.transform(image)

        mask_label = self.get_mask_label(index)

        return image_transform, mask_label

class KSTestDataset(Dataset):
    def __init__(self, img_paths, resize, mean=(0.548, 0.504, 0.479), std=(0.237, 0.247, 0.246)):
        self.img_paths = img_paths
        self.mean = mean
        self.std = std

        # self.transform = transforms.Compose([
        #     Resize(resize, Image.BILINEAR),
        #     ToTensor(),
        #     Normalize(mean=mean, std=std),
        # ])

    def __getitem__(self, index):
        image = Image.open(self.img_paths[index])

        if self.transform:
            image = self.transform(image)
        return image

    def __len__(self):
        return len(self.img_paths)

    def set_transform(self, transform):
        self.transform = transform


from skills import *
from weapons import *
from helmets import *
from gloves import *
from clothes import *
import wx

class Job(object):

  def __init__(self, userName):
    self.userName = userName
    self.level = 1
    self.level = 1
    self.hp = 20
    self.max_hp = 20
    self.mp = 20
    self.max_mp = 20
    self.money = 1000
    self.experience = 0
    self.required_experience = 30
    self.moves = [BasicAttack()]
    self.potential_moves = {}

    self.required_experiences = self.required_experiences()

    self.equipments = {"weapon": Mokdo(), "helmet": BasicHelmet(),
                       "glove": BasicGlove(), "cloth": BasicCloth()}

    #items = { "Equipment" : [Mokdo, DragonFirst],
    #          "Recipes" : {"Silver Branch" : 3, "Gold Plate" : 2},
    #          "Potions" : {"RedHp10" : 21, "BlueMp10" : 10, "RedHp30" : 20}
    #        }
    self.items = {"Equipment" : [Mokdo(), BasicHelmet()],
                  "Recipe" : {},
                  "Potions" : {"RedHp10" : 5, "BlueMp10" : 5}
                  }

    self.weapon = self.equipments["weapon"]
    self.helmet = self.equipments["helmet"]
    self.glove = self.equipments["glove"]
    self.cloth = self.equipments["cloth"]

    self.update_stats()

  def update_stats(self):
    self.attack_damage = self.weapon.damage + self.helmet.damage + self.glove.damage + self.cloth.damage
    self.defense = self.weapon.defense + self.helmet.defense + self.glove.defense + self.cloth.defense

  def required_experiences(self):
    exp = [0, 0]
    for i in range(2, 200):
      exp.append(i**2 * (i * 100))
    return exp

  def add_move(self):
    if self.level in self.potential_moves:
      self.moves.append(self.potential_moves[self.level])

  def check_level_up(self):
    if self.experience >= self.required_experience:
      self.level_up()

  def level_up(self):
    self.level += 1
    self.required_experience = self.required_experiences[self.level + 1]
    self.update_stats()
    wx.MessageBox("You leveled up to %d !" % self.level, 'Level up!', wx.OK)
    if self.level in self.potential_moves:
      move = self.potential_moves[self.level]
      self.moves.append(move)
      wx.MessageBox("You learned new skill, %s !" % move.name, 'New Skill', wx.OK)

class Rogue(Job):

  def __init__(self, userName):
    super(Rogue, self).__init__(userName)
    self.job = "Rogue"
    self.potential_moves = self.gen_potential_moves()

  """ Returns potential moves for Rogues job. Hashmap of {Lvl: Skill object}"""
  def gen_potential_moves(self):
    skills = {}
    skills[1] = BasicAttack()
    return skills

class Warrior(Job):
  def __init__(self, userName):
    super(Warrior, self).__init__(userName)
    self.job = "Warrior"
    self.potential_moves = self.gen_potential_moves()

  """ Returns potential moves for Warriors job. Hashmap of {Lvl: Skill object}"""
  def gen_potential_moves(self):
    skills = {}
    skills[1] = BasicAttack()
    return skills

class Bowman(Job):
  def __init__(self, userName):
    super(Bowman, self).__init__(userName)
    self.job = "Bowman"
    self.potential_moves = self.gen_potential_moves()

  """ Returns potential moves for Bowman job. Hashmap of {Lvl: Skill object}"""
  def gen_potential_moves(self):
    skills = {}
    skills[1] = BasicAttack()
    return skills

class Magician(Job):
  def __init__(self, userName):
    super(Magician, self).__init__(userName)
    self.job = "Magician"
    self.potential_moves = self.gen_potential_moves()

  """ Returns potential moves for Magician job. Hashmap of {Lvl: Skill object}"""
  def gen_potential_moves(self):
    skills = {}
    skills[1] = BasicAttack()
    skills[2] = EnergyBolt()
    return skills