# Version 1
# September 25, 2018
# Input from the user 'height' defines the height of the diamond
# Using one loop, number of stars in each line is determined depending
# on whether the line is above or below the mid line.

import sys

def main():
    height = int(sys.argv[1])
    SPACE = ' '
    STAR = '*'

    for i in range(1, height+1):

        if(i <= round(height/2)):
            no_star =  2*i - 1
            no_space = int((height - no_star) / 2)
            print(SPACE*no_space + STAR*no_star + SPACE*no_space)

        else:
            no_star =  2*(height + 1 - i) - 1
            no_space = int((height - no_star) / 2)
            print(SPACE*no_space + STAR*no_star + SPACE*no_space)

main()


# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html

from sqlalchemy.orm import sessionmaker
from hrforecast_test.models import (HRForecastJobs,
                                    HRForecastTasks,
                                    HRForecastProfile,
                                    HRForecastNiceToHave,
                                    GazpromJobs,
                                    GazpromJobDuties,
                                    GazpromJobRequirements,
                                    db_connect,
                                    create_table)

class SQLPipeline(object):
    def __init__(self):
        engine = db_connect()
        create_table(engine)
        self.Session = sessionmaker(bind=engine)

    def process_item(self, item, spider):
        session = self.Session()

        def session_add(item):
            try:
                session.add(item)
                session.commit()
            except:
                session.rollback()
                raise

        # Join all lists with newline for Excel
        def listToExcel(item):
            for i in item:
                if type(item[i]) == list:
                    item[i] = '\n'.join(item[i])
            return item

        new_item = item.copy()
        # Sorry I don't wanna make another project
        if spider.name == 'hrforecast':
            tasks = new_item.pop('tasks')
            profiles = new_item.pop('your_profile')
            new_item.pop('crawled_date')
            try:
                nices = new_item.pop('nice_to_have')
            except:
                nices = None

            jobquote = HRForecastJobs(**new_item)
            session_add(jobquote)

            for task in tasks:
                taskquote = HRForecastTasks()
                taskquote.job_id = jobquote.job_id
                taskquote.job_task = task
                session_add(taskquote)

            for profile in profiles:
                profile_quote = HRForecastProfile()
                profile_quote.job_id = jobquote.job_id
                profile_quote.profile = profile
                session_add(profile_quote)

            if nices:
                for nice in nices:
                    nice_quote = HRForecastNiceToHave()
                    nice_quote.job_id = jobquote.job_id
                    nice_quote.nice = nice
                    session_add(nice_quote)

        elif spider.name == 'gazpromvacancy':
            duties = new_item.pop('job_duties')
            requirements = new_item.pop('job_requirements')
            new_item.pop('crawled_date')

            jobquote = GazpromJobs(**new_item)
            session_add(jobquote)

            for dutie in duties:
                dutiequote = GazpromJobDuties()
                dutiequote.job_id = jobquote.id
                dutiequote.dutie = dutie
                session_add(dutiequote)

            for requirement in requirements:
                requirementquote =  GazpromJobRequirements()
                requirementquote.job_id = jobquote.id
                requirementquote.requirement = requirement
                session_add(requirementquote)

        session.close()

        item = listToExcel(item)
        return item


'''
프로그래머스 팀에서는 기능 개선 작업을 수행 중입니다.
각 기능은 진도가 100%일 때 서비스에 반영할 수 있습니다.

또, 각 기능의 개발속도는 모두 다르기 때문에 뒤에 있는 기능이 앞에 있는 기능보다 먼저 개발될 수 있고,
이때 뒤에 있는 기능은 앞에 있는 기능이 배포될 때 함께 배포됩니다.

먼저 배포되어야 하는 순서대로 작업의 진도가 적힌 정수 배열 progresses와
각 작업의 개발 속도가 적힌 정수 배열 speeds가 주어질 때
각 배포마다 몇 개의 기능이 배포되는지를 return 하도록 solution 함수를 완성하세요.
'''
from collections import deque
from operator import add

def solution(progresses, speeds):
    answer = []

    que_progresses = deque(progresses)  # 작업 개수 리스트를 큐로 변환
    que_speeds = deque(speeds)  # 작업 속도 리스트를 큐로 변환

    while que_progresses:
        count = 0  # 배포마다 기능이 몇 개인지 저장하는 변수
        que_len = len(que_progresses)

        # 기능 개발이 한개만 남았을 경우 날짜와 상관없이 무조건
        # 하나만 배포하므로 answer에 1을 추가해주고 종료
        if que_len == 1:
            answer.append(1)
            break

        # 개발 진행도 + 개발 속도 -> 개발 진행도 갱신
        que_progresses = deque(map(add, que_progresses, que_speeds))

        # 개발진행이 완료 되었으면 배포
        while que_progresses:
            if que_progresses[0] >= 100:
                que_progresses.popleft()
                que_speeds.popleft()
                count += 1
            else:
                break

        if count != 0:
            answer.append(count)
    print(answer)
    return answer

if __name__ == '__main__':
    progresses = [95, 90, 99, 99, 80, 99]
    speeds = [1, 1, 1, 1, 1, 1]
    solution(progresses,speeds)


import json

import torch
from torch.utils.data import Dataset

class QADataset(Dataset):
    def __init__(
        self,
        file_name,
        tokenizer,
        split="train",
        max_length=256,
        truncate_promt=True,
        train=True,
    ):
        self.train = train
        self.pairs = []
        with open(file_name, "r") as f:
            data = json.load(f)
            self.pairs = data[split]

        self.tokenizer = tokenizer
        self.max_length = max_length
        self.truncate_promt = truncate_promt

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

    def __getitem__(self, idx):
        qa_pair = self.pairs[idx]
        if not self.train:
            return "Question: " + qa_pair["Question"], qa_pair["Answer"]
        else:

            sample, attention_mask, labels = self.promt_tokenize(
                qa_pair["Question"], qa_pair["Answer"]
            )

            return {
                "input_ids": sample,
                "attention_mask": attention_mask,
                "labels": labels,
            }

    def promt_tokenize(self, promt, completion):
        a_toks = self.tokenizer.encode(completion)

        if self.truncate_promt:
            q_toks = self.tokenizer.encode(promt)
            q_toks = q_toks[: self.max_length - len(a_toks) - 7]
            tmp = self.tokenizer.decode(q_toks).strip()
        else:
            tmp = promt

        tmp = "Question: " + tmp + "\nAnswer:"
        q_toks = self.tokenizer.encode(tmp)

        sample = torch.tensor(q_toks + a_toks, dtype=int).unsqueeze(0)
        sample = sample[0, : self.max_length]
        attention_mask = torch.ones(sample.shape, dtype=int)
        labels = sample.clone()
        labels[0, : len(q_toks)] = -100

        return sample, attention_mask, labels


# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def reorderList(self, head: Optional[ListNode]) -> None:
        """
        Do not return anything, modify head in-place instead.
        """
        # Find the middle
        slow, fast = head, head.next
        while fast and fast.next:
            slow = slow.next
            fast = fast.next.next

        # Splitting linked list into 2 pieces: regular and reversed. Prev is initialized as end of reversed list and slow.next is end of regular list
        second = slow.next
        prev = slow.next = None

        while second:
            # Use temporary var to store next node in reverse array
            tmp = second.next
            second.next = prev
            prev = second
            second = tmp

        # merge two halves
        first, second = head, prev
        while second:
            tmp1, tmp2 = first.next, second.next
            first.next = second
            second.next = tmp1
            first = tmp1
            second = tmp2

    # Time Complexity: O(n)
    # Space ComplexitY: O(1)
    # Datastructure(s): None
    # Algorithm(s): None
    # Pattern: Linked List

    # Link: https://leetcode.com/problems/reorder-list/


##
import pandas as pd
from datetime import timedelta
import re
df = pd.read_csv('Downloads/data_file (1).csv')
tstamp_df = pd.DataFrame(columns=['TIME STAMP'])
timestamps=[]
dic={'JAN':'01', 'FEB':'02', 'MAR':'03', 'APR':'04','MAY':'05','JUN':'06',
     'JUL':'07','AUG':'08','SEP':'09','OCT':'10','NOV':'11','DEC':'12'}
t_stamp= df.filter(['OPD_DATE','ACT_TIME'])
print(t_stamp['OPD_DATE'].head())
from datetime import datetime
for index in range(len(df['OPD_DATE'])):
    date=df['OPD_DATE'][index][0:2]
    month=dic[df['OPD_DATE'][index][3:6]]
    year=df['OPD_DATE'][index][7:10]
    time=df['ACT_TIME'][index]
    str_time = str(timedelta(seconds=int(time)))
    # str time can be '1 day 00:00:34'
    if 'day' in str_time:
        add_days = ''
        for i in range(len(str_time)):
            if str_time[i] == ' ':
                s = str_time[i + 1:]
                break
            add_days += str_time[i]
        date = int(date) + int(add_days)
        # remove 1 day from str_time
        str_time = re.sub("[^0-9:]", "", s)
    dt=str(date)+month+year+str_time
    tstamp=datetime.strptime(dt, '%d%m%y%H:%M:%S')
    timestamps.append(tstamp)

df.insert(2, "TIME STAMP", timestamps)
df.drop(columns=['OPD_DATE','ACT_TIME'],inplace=True, axis=1)

## For each trip id, have a single route no, service key and direction
## stop_df --> trip_id, vehicle_id, direction, service_key, route_id

groupby_trip = stopdf.groupby('trip_id')
groups = groupby_trip.groups.keys()
column_names = ['trip_id','vehicle_id','route_id', 'direction', 'service_key']
finaldf = pd.DataFrame(columns = column_names)
for group in groups:
    group_df = groupby_trip.get_group(group)
    groupby_labels =group_df.groupby(['route_id', 'direction', 'service_key'])
    size=max(groupby_labels.size())
    # Delete all the groups whose size is less than max size
    groupby_labels=groupby_labels.filter(lambda x: len(x) == size, dropna=True)
    finaldf=finaldf.append(groupby_labels, ignore_index = True)

finaldf = finaldf.drop_duplicates()

invalid_trip_id_list = []
    # TRANSFORMATION 4 : Change the value of direction to out and back if its 0 and 1 respectively
for index in range(len(finaldf['direction'])):
    if (pd.isnull(finaldf['direction'][index])):
        finaldf['direction'][index]=''
        invalid_trip_id_list.append(finaldf['trip_id'][index])
        #finaldf = finaldf.drop(finaldf.index[index])
    elif finaldf['direction'][index]=='1':
        finaldf['direction'][index]='Out'
    elif finaldf['direction'][index]=='0':
        finaldf['direction'][index]='Back'
    else:
        finaldf['direction'][index]=''
        invalid_trip_id_list.append(finaldf['trip_id'][index])
print(finaldf.head())

# TRANSFORMATION 5: Change W to Weekday, S to Saturday and U to Sunday
for index in range(len(finaldf['service_key'])):
    if (pd.isnull(finaldf['service_key'][index])):
        finaldf['service_key'][index] == ''
        invalid_trip_id_list.append(finaldf['trip_id'][index])
    if finaldf['service_key'][index]=='W':
        finaldf['service_key'][index]='Weekday'
    elif finaldf['service_key'][index]=='S':
        finaldf['service_key'][index]='Saturday'
    elif finaldf['service_key'][index]=='U':
        finaldf['service_key'][index]='Sunday'
    else:
        finaldf['service_key'][index]=''
        invalid_trip_id_list.append(finaldf['trip_id'][index])

newdf=tripdf.merge(finaldf, on=['trip_id','vehicle_id'], how='left')
newdf = newdf.drop(newdf.columns[[2, 3, 4]], axis=1)
print("NEWDF: \n",newdf.head())


from tkinter import *
from tkinter.filedialog import *
from tkinter.messagebox import *
from tkinter import ttk
from threading import Thread
from urllib.request import urlretrieve
from PIL import ImageTk, Image
import math

from installlib.installlib import *

class MainWindow(Tk):

    WIDTH = 700
    HEIGHT = 419
    IMAGE_URL = "https://intercraftmc.com/installer/background.png"
    IMAGE_NAME = "intercraftbg.png"

    def __init__(self):
        super(MainWindow, self).__init__()

        self.__screenWidth = self.winfo_screenwidth()
        self.__screenHeight = self.winfo_screenheight()

        x = (self.__screenWidth/2) - (MainWindow.WIDTH/2)
        y = (self.__screenHeight/2) - (MainWindow.HEIGHT/2)

        self.title("InterCraft Installer")
        self.resizable(False, False)
        self.geometry('%dx%d+%d+%d' % (MainWindow.WIDTH, MainWindow.HEIGHT, x, y))

        self.preInit()
        self.initGui()

        self.setDirectoryDisplay(self.__installer.getMcPath())

    def preInit(self):

        self.tk.call('wm', 'iconphoto', self._w, utils.image('icon.ico'))
        self.__image = utils.image('background.png')
        self.__installer = InstallLib()
        self.__installErrors = {}

    def initGui(self):

        self.configure(background = 'white')

        C = Canvas(self, bg = 'white', width = self.__screenWidth, height = self.__screenHeight)

        self.__imageLabel = Label(self, image = self.__image)
        self.__imageLabel.place(x=0, y=0, relwidth=1, relheight=1)
        C.pack()

        self.__finished = 0
        self.__progress = IntVar(0)
        self.__progressBar = ttk.Progressbar(self, orient='horizontal', length=300, variable=self.__progress)

        #self.__title = Label(self, text = "InterCraft Installer", font = ("Roboto", 20), background = 'white', foreground = '#292b2c')
        #self.__title.grid(row=9,column=9)

        self.__button1 = Button(self, text = "Install InterCraft", font = ("Helvetica", 20), cursor = "hand2")
        self.__button1.place(relx=0.5, rely=0.45, anchor=CENTER)

        self.__button2 = Button(self, text = "Change Minecraft directory", font = ("Helvetica", 12), cursor = "hand2")
        self.__button2.place(relx=0.5, rely=0.60, anchor=CENTER)

        self.__directoryDisp = Label(self, background = '#111111', foreground = 'white', font = ("Helvetica", 10), width = 87, height = 2)
        self.__directoryDisp.place(relx=0.5, rely=0.96, anchor=CENTER)

        #Button styling
        self.__button1.configure(foreground = 'white')
        self.__button1.configure(relief = 'flat')
        self.__button1.configure(background = '#008BFF')
        self.__button1.configure(activebackground = '#006ac2')
        self.__button1.configure(activeforeground = 'white')
        self.__button1.bind("<Enter>", self.onHover)
        self.__button1.bind("<Leave>", self.onLeave)
        self.__button1.bind("<ButtonRelease-1>", self.install)

        self.__button2.configure(foreground = 'white')
        self.__button2.configure(relief = 'flat')
        self.__button2.configure(background = '#008BFF')
        self.__button2.configure(activebackground = '#006ac2')
        self.__button2.configure(activeforeground = 'white')
        self.__button2.bind("<Enter>", self.onHover2)
        self.__button2.bind("<Leave>", self.onLeave2)
        self.__button2.bind("<ButtonRelease-1>", self.getDirectory)

    def clean(self):
        self.__installer.clean()

    def onHover(self, event):
        self.__button1.configure(background = '#006ac2')

    def onLeave(self, event):
        self.__button1.configure(background = '#008BFF')

    def onHover2(self, event):
        self.__button2.configure(background = '#006ac2')

    def onLeave2(self, event):
        self.__button2.configure(background = '#008BFF')

    def getDirectory(self, event):
        newDirectory = askdirectory()
        if newDirectory != "":
            self.__installer.setMcPath(newDirectory)
            self.setDirectoryDisplay(self.__installer.getMcPath())

    def setDirectoryDisplay(self, path):
        self.__directoryDisp.configure(text = "Minecraft location: " + utils.reformatPath(path))

    def install(self, event):
        self.__finished = 0
        self.__button1.configure(state = 'disabled')
        self.__button1.configure(cursor = 'arrow')
        self.__button1.configure(background = '#222222')
        self.__button1.unbind("<ButtonRelease-1>")
        self.__button1.unbind("<Enter>")
        self.__button1.unbind("<Leave>")
        self.__button2.place_forget()
        self.__progressBar.place(relx=0.5, rely=0.60, anchor=CENTER)
        print("Installing InterCraft...")
        print("Installing forge...")
        t1 = Thread(target = self.__installer.installForge, args = (self.installFinish,))
        t1.daemon = True
        t1.start()
        print("Installing mods...")
        t2 = Thread(target = self.__installer.installMods, args = (self.installFinish,))
        t2.daemon = True
        t2.start()
        print("Configuring profile...")
        self.__installer.installJson(self.installFinish)

    def installFinish(self, result, name = None, message = None):
        self.__finished += 1
        if not result:
            self.__installErrors[name] = message

        self.__progress.set(int(math.ceil(self.__finished/3.0*100)))

        if self.__finished == 3:
            print("install complete.")

            if len(self.__installErrors.keys()) == 0:
                showinfo("InterCraft Installer", "Install Complete.")
            else:
                errorString = ''
                for key, value in self.__installErrors.items():
                    errorString += '\n' + value
                showerror("InterCraft Installer - Errors occured", errorString[1:])
            self.quit()

    def quit(self):
        self.clean()
        super(MainWindow, self).quit()


def firstDuplicate(a):
    len_a = len(a)
    d = {}
    answer = -1
    for i,v in enumerate(a):
        if v in d.keys():
            answer = v
            break
        else:
            d[v] = i
    return answer


"""Removed machine sub command."""

import argparse
import sys

from docknv.logger import Logger, Fore

def _init(subparsers):
    cmd = subparsers.add_parser(
        "machine", help="removed machine command, use `service` instead"
    )
    cmd.add_argument("args", nargs=argparse.REMAINDER)

def _handle(args):
    str_args = " ".join(args.args)
    Logger.raw(
        "`machine` command has been removed. use `service` instead.",
        color=Fore.YELLOW,
    )
    Logger.raw(f"example: `docknv service {str_args}`", color=Fore.YELLOW)
    sys.exit(1)


import os
from application import upload_folder, dlload_folder

dir_list = [upload_folder, dlload_folder]

for directory in dir_list:

    print("Current dir:")
    print(directory)
    directory_name = directory

    test = os.listdir(directory_name)
    print("Files list in this dir:")
    print(test)

    for item in test:
        if item.endswith(".csv"):
            print(os.path.join(directory_name, item) + " deleted.")
            os.remove(os.path.join(directory_name, item))


import numpy as np
from numpy import genfromtxt
import matplotlib.pyplot as plt
import platform
import itertools

if(platform.system() == 'Windows'): #Windows
    class_1 = genfromtxt('..\TTT4275-project\\task_1\Iris_TTT4275\class_1', delimiter=',')
    class_2 = genfromtxt('..\TTT4275-project\\task_1\Iris_TTT4275\class_2', delimiter=',')
    class_3 = genfromtxt('..\TTT4275-project\\task_1\Iris_TTT4275\class_3', delimiter=',')
else:  #Linux
    class_1 = genfromtxt('task_1/Iris_TTT4275/class_1', delimiter=',')
    class_2 = genfromtxt('task_1/Iris_TTT4275/class_2', delimiter=',')
    class_3 = genfromtxt('task_1/Iris_TTT4275/class_3', delimiter=',')

##############################
# Extracting wanted features #
features=np.array([2,3]) #if you want to change features->CHANGE ME
class_1=class_1[:,features]
class_2=class_2[:,features]
class_3=class_3[:,features]
num_f=features.shape[0]

###
train_begin=0
train_end=30
####
test_begin=30
test_end=50
####

W = np.zeros((3,num_f+1))
tk_1 = np.zeros((3,1))
tk_1[0,0] = 1
tk_2 = np.zeros((3,1))
tk_2[1,0] = 1
tk_3 = np.zeros((3,1))
tk_3[2,0] = 1
norm_grad_MSE = 1
alpha = 0.01

def sigmoid(x):
   return 1/(1+np.exp(-x))
print(num_f)
i = 0
while(norm_grad_MSE > 0.001 and i < 5000):
    N=train_end - train_begin #how many to train on
    grad_MSE = np.zeros((3,num_f+1))

    for n in range(train_begin,train_end):
        for (_class,tk) in zip([class_1,class_2,class_3],[tk_1,tk_2,tk_3]):
            xk = np.append(_class[n,:],1).reshape((num_f+1,1))
            zk = W@xk
            gk = sigmoid(zk)
            temp = np.multiply(gk-tk,gk)
            temp = np.multiply(temp, np.ones((3,1))-gk)
            grad_MSE += alpha*temp@xk.T
    W -= grad_MSE
    norm_grad_MSE = np.linalg.norm(grad_MSE)
    i += 1
np.savetxt('w.txt',W)
W = np.loadtxt('w.txt')
print(W)
###############################
####### Classification ########
confusion = np.zeros([3,3])
for n in range(test_begin,test_end):
    i = 0
    for class_ in [class_1, class_2, class_3]:
        g = W@np.append(class_[n,:],1).reshape(num_f+1,1)
        classified_class = np.argmax(g, axis=0)
        confusion[i,classified_class] += 1
        i += 1
print("Confusion matrix:")
print(confusion)

############################
######## Error-rate ########
def find_error_rate(confusion):
    N=np.sum(confusion)
    error_counter=0
    for i in range(confusion.shape[0]):
        for j in range(confusion.shape[0]):
            if(i != j):
                error_counter += confusion[i,j]
    return(error_counter/N)

print(f'Error rate:{find_error_rate(confusion)}')


import numpy as np
import pyqtgraph as pg
import h5py
from pyqtgraph.dockarea import *
from pyqtgraph.parametertree import Parameter, ParameterTree
import json, os
from scipy.spatial import distance
import subprocess
import skimage.measure as sm

from psalgos.pypsalgos import PyAlgos  # replacement for: from ImgAlgos.PyAlgos import PyAlgos
from psocake import utils
from psocake import LaunchPeakFinder

class PeakFinding:
    def __init__(self, parent = None):
        self.parent = parent

        ## Dock: Peak finder
        self.dock = Dock("Peak Finder", size=(1, 1))
        self.win = ParameterTree()
        self.dock.addWidget(self.win)

        self.userUpdate = None
        self.doingUpdate = False

        # Peak finding
        self.hitParam_grp = 'Peak finder'
        self.hitParam_showPeaks_str = 'Show peaks found'
        self.hitParam_autoPeaks_str = 'Auto peak finder'
        self.hitParam_algorithm_str = 'Algorithm'
        self.hitParam_parameters_str = 'Parameters'
        # algorithm 0
        self.hitParam_algorithm0_str = 'None'
        # algorithm 1
        self.hitParam_alg1_npix_min_str = 'npix_min'
        self.hitParam_alg1_npix_max_str = 'npix_max'
        self.hitParam_alg1_amax_thr_str = 'amax_thr'
        self.hitParam_alg1_atot_thr_str = 'atot_thr'
        self.hitParam_alg1_son_min_str = 'son_min'
        self.hitParam_algorithm1_str = 'Droplet (a.k.a v4r3)'
        self.hitParam_alg1_thr_low_str = 'thr_low'
        self.hitParam_alg1_thr_high_str = 'thr_high'
        self.hitParam_alg1_rank_str = 'rank'
        self.hitParam_alg1_radius_str = 'radius'
        self.hitParam_alg1_dr_str = 'dr'
        # algorithm 2
        self.hitParam_alg2_npix_min_str = 'npix_min'
        self.hitParam_alg2_npix_max_str = 'npix_max'
        self.hitParam_alg2_amax_thr_str = 'amax_thr'
        self.hitParam_alg2_atot_thr_str = 'atot_thr'
        self.hitParam_alg2_son_min_str = 'son_min'
        self.hitParam_algorithm2_str = 'Adaptive (a.k.a v3r3)'
        self.hitParam_alg2_thr_str = 'thr'
        self.hitParam_alg2_r0_str = 'r0'
        self.hitParam_alg2_dr_str = 'dr'
        # algorithm 3
        self.hitParam_alg3_npix_min_str = 'npix_min'
        self.hitParam_alg3_npix_max_str = 'npix_max'
        self.hitParam_alg3_amax_thr_str = 'amax_thr'
        self.hitParam_alg3_atot_thr_str = 'atot_thr'
        self.hitParam_alg3_son_min_str = 'son_min'
        self.hitParam_algorithm3_str = 'szPeak'
        self.hitParam_alg3_thr_str = 'thr'
        self.hitParam_alg3_r0_str = 'r0'
        self.hitParam_alg3_dr_str = 'dr'

        self.hitParam_outDir_str = 'Output directory'
        self.hitParam_runs_str = 'Run(s)'
        self.hitParam_queue_str = 'queue'
        self.hitParam_cpu_str = 'CPUs'
        self.hitParam_psanaq_str = 'psanaq'
        self.hitParam_psnehq_str = 'psnehq'
        self.hitParam_psfehq_str = 'psfehq'
        self.hitParam_psnehprioq_str = 'psnehprioq'
        self.hitParam_psfehprioq_str = 'psfehprioq'
        self.hitParam_psnehhiprioq_str = 'psnehhiprioq'
        self.hitParam_psfehhiprioq_str = 'psfehhiprioq'
        self.hitParam_psdebugq_str = 'psdebugq'
        self.hitParam_psanagpuq_str = 'psanagpuq'
        self.hitParam_psanaidleq_str = 'psanaidleq'
        self.hitParam_ffbh1q_str = 'ffbh1q' # on-shift
        self.hitParam_ffbl1q_str = 'ffbl1q' # off-shift
        self.hitParam_ffbh2q_str = 'ffbh2q'
        self.hitParam_ffbl2q_str = 'ffbl2q'
        self.hitParam_ffbh3q_str = 'ffbh3q'
        self.hitParam_ffbl3q_str = 'ffbl3q'
        self.hitParam_anaq_str = 'anaq'  # For the week after the experiment
        self.hitParam_noQueue_str = 'N/A'
        self.hitParam_noe_str = 'Number of events to process'
        self.hitParam_threshold_str = 'Indexable number of peaks'
        self.hitParam_launch_str = 'Launch peak finder'
        #self.hitParam_launchAuto_str = 'Launch auto peak finder'
        self.hitParam_extra_str = 'Extra parameters'

        self.save_minPeaks_str = 'Minimum number of peaks'
        self.save_maxPeaks_str = 'Maximum number of peaks'
        self.save_minRes_str = 'Minimum resolution (pixels)'
        self.save_sample_str = 'Sample name'
        self.tag_str = '.cxi tag'

        if self.parent.mode == "sfx":
            self.showPeaks = True
        else:
            self.showPeaks = False
        self.turnOnAutoPeaks = False
        self.ind = None
        self.pairsFoundPerSpot = 0
        self.peaks = None
        self.numPeaksFound = 0
        self.algorithm = 2
        self.algInitDone = False
        self.peaksMaxRes = 0
        self.classify = False
        self.tag = ''

        self.hitParam_alg1_npix_min = 2.
        self.hitParam_alg1_npix_max = 30.
        self.hitParam_alg1_amax_thr = 300.
        self.hitParam_alg1_atot_thr = 600.
        self.hitParam_alg1_son_min = 10.
        self.hitParam_alg1_thr_low = 0.
        self.hitParam_alg1_thr_high = 0.
        self.hitParam_alg1_rank = 3
        self.hitParam_alg1_radius = 3
        self.hitParam_alg1_dr = 2
        self.hitParam_outDir = self.parent.psocakeDir
        self.hitParam_outDir_overridden = False
        self.hitParam_runs = ''
        self.hitParam_queue = self.hitParam_psanaq_str
        self.hitParam_cpus = 24
        self.hitParam_noe = -1
        self.hitParam_threshold = 15 # usually crystals with less than 15 peaks are not indexable
        self.minPeaks = 10
        self.maxPeaks = 2048
        self.minRes = -1
        self.sample = 'sample'
        self.profile = 0
        self.hitParam_extra = ''

        self.params = [
            {'name': self.hitParam_grp, 'type': 'group', 'children': [
                {'name': self.hitParam_showPeaks_str, 'type': 'bool', 'value': self.showPeaks,
                 'tip': "Show peaks found shot-to-shot"},
                #{'name': self.hitParam_autoPeaks_str, 'type': 'bool', 'value': self.turnOnAutoPeaks,
                # 'tip': "Automatically find peaks"},
                {'name': self.hitParam_algorithm_str, 'type': 'list', 'values': {self.hitParam_algorithm3_str: 3,

self.hitParam_algorithm2_str: 2,
                                                                                self.hitParam_algorithm1_str: 1,
                                                                                self.hitParam_algorithm0_str: 0},
                 'value': self.algorithm},
                {'name': self.hitParam_parameters_str, 'visible': True, 'expanded': False, 'type': 'str', 'value': "",
                 'readonly': True, 'children': [
                    {'name': self.hitParam_alg1_npix_min_str, 'type': 'float', 'value': self.hitParam_alg1_npix_min,
                     'tip': "Only keep the peak if number of pixels above thr_low is above this value"},
                    {'name': self.hitParam_alg1_npix_max_str, 'type': 'float', 'value': self.hitParam_alg1_npix_max,
                     'tip': "Only keep the peak if number of pixels above thr_low is below this value"},
                    {'name': self.hitParam_alg1_amax_thr_str, 'type': 'float', 'decimals': 7, 'value': self.hitParam_alg1_amax_thr,
                     'tip': "Only keep the peak if max value is above this value"},
                    {'name': self.hitParam_alg1_atot_thr_str, 'type': 'float', 'decimals': 7, 'value': self.hitParam_alg1_atot_thr,
                     'tip': "Only keep the peak if integral inside region of interest is above this value"},
                    {'name': self.hitParam_alg1_son_min_str, 'type': 'float', 'value': self.hitParam_alg1_son_min,
                     'tip': "Only keep the peak if signal-over-noise is above this value"},
                    {'name': self.hitParam_alg1_thr_low_str, 'type': 'float', 'decimals': 7, 'value': self.hitParam_alg1_thr_low,
                     'tip': "Grow a seed peak if above this value"},
                    {'name': self.hitParam_alg1_thr_high_str, 'type': 'float', 'decimals': 7, 'value': self.hitParam_alg1_thr_high,
                     'tip': "Start a seed peak if above this value"},
                    {'name': self.hitParam_alg1_rank_str, 'type': 'int', 'value': self.hitParam_alg1_rank,
                     'tip': "region of integration is a square, (2r+1)x(2r+1)"},
                    {'name': self.hitParam_alg1_radius_str, 'type': 'int', 'value': self.hitParam_alg1_radius,
                     'tip': "region inside the region of interest"},
                    {'name': self.hitParam_alg1_dr_str, 'type': 'float', 'value': self.hitParam_alg1_dr,
                     'tip': "background region outside the region of interest"},
                ]},
                {'name': self.save_minPeaks_str, 'type': 'int', 'decimals': 7, 'value': self.minPeaks,
                 'tip': "Index only if there are more Bragg peaks found"},
                {'name': self.save_maxPeaks_str, 'type': 'int', 'decimals': 7, 'value': self.maxPeaks,
                 'tip': "Index only if there are less Bragg peaks found"},
                {'name': self.save_minRes_str, 'type': 'int', 'decimals': 7, 'value': self.minRes,
                 'tip': "Index only if Bragg peak resolution is at least this"},
                {'name': self.save_sample_str, 'type': 'str', 'value': self.sample,
                 'tip': "Sample name saved inside cxi"},
                {'name': self.tag_str, 'type': 'str', 'value': self.tag,
                 'tip': "attach tag to cxi, e.g. cxitut13_0010_tag.cxi"},
                {'name': self.hitParam_outDir_str, 'type': 'str', 'value': self.hitParam_outDir},
                {'name': self.hitParam_runs_str, 'type': 'str', 'value': self.hitParam_runs},
                {'name': self.hitParam_queue_str, 'type': 'list', 'values': {self.hitParam_psfehhiprioq_str: 'psfehhiprioq',
                                                                             self.hitParam_psnehhiprioq_str: 'psnehhiprioq',
                                                                             self.hitParam_psfehprioq_str: 'psfehprioq',
                                                                             self.hitParam_psnehprioq_str: 'psnehprioq',
                                                                             self.hitParam_psfehq_str: 'psfehq',
                                                                             self.hitParam_psnehq_str: 'psnehq',
                                                                             self.hitParam_psanaq_str: 'psanaq',
                                                                             self.hitParam_psdebugq_str: 'psdebugq',
                                                                             self.hitParam_psanagpuq_str: 'psanagpuq',
                                                                             self.hitParam_psanaidleq_str: 'psanaidleq',
                                                                             self.hitParam_ffbh1q_str: 'ffbh1q',
                                                                             self.hitParam_ffbl1q_str: 'ffbl1q',
                                                                             self.hitParam_ffbh2q_str: 'ffbh2q',
                                                                             self.hitParam_ffbl2q_str: 'ffbl2q',
                                                                             self.hitParam_ffbh3q_str: 'ffbh3q',
                                                                             self.hitParam_ffbl3q_str: 'ffbl3q',
                                                                             self.hitParam_anaq_str: 'anaq'},
                 'value': self.hitParam_queue, 'tip': "Choose queue"},
                {'name': self.hitParam_cpu_str, 'type': 'int', 'decimals': 7, 'value': self.hitParam_cpus},
                {'name': self.hitParam_noe_str, 'type': 'int', 'decimals': 7, 'value': self.hitParam_noe,
                 'tip': "number of events to process, default=-1 means process all events"},
                {'name': self.hitParam_extra_str, 'type': 'str', 'value': self.hitParam_extra, 'tip': "Extra peak finding flags"},
                {'name': self.hitParam_launch_str, 'type': 'action'},
                #{'name': self.hitParam_launchAuto_str, 'type': 'action'},
            ]},
        ]
        self.p3 = Parameter.create(name='paramsPeakFinder', type='group', \
                                   children=self.params, expanded=True)
        self.win.setParameters(self.p3, showTop=False)
        self.p3.sigTreeStateChanged.connect(self.change)
        #self.parent.connect(self.launchBtn, QtCore.SIGNAL("clicked()"), self.findPeaks)

    def digestRunList(self, runList):
        runsToDo = []
        if not runList:
            print("Run(s) is empty. Please type in the run number(s).")
            return runsToDo
        runLists = str(runList).split(",")
        for list in runLists:
            temp = list.split(":")
            if len(temp) == 2:
                for i in np.arange(int(temp[0]),int(temp[1])+1):
                    runsToDo.append(i)
            elif len(temp) == 1:
                runsToDo.append(int(temp[0]))
        return runsToDo

    def updateParam(self):
        if self.userUpdate is None:
            if self.parent.psocakeRunDir is not None:
                peakParamFname = self.parent.psocakeRunDir + '/peakParam'
                if self.tag: peakParamFname += '_' + self.tag
                peakParamFname += '.json'
                if os.path.exists(peakParamFname):
                    with open(peakParamFname) as infile:
                        d = json.load(infile)
                        if d[self.hitParam_algorithm_str] == 1 or d[self.hitParam_algorithm_str] == 2:
                            # Update variables
                            try:
                                self.hitParam_alg1_npix_min = d[self.hitParam_alg1_npix_min_str]
                                self.hitParam_alg1_npix_max = d[self.hitParam_alg1_npix_max_str]
                                self.hitParam_alg1_amax_thr = d[self.hitParam_alg1_amax_thr_str]
                                self.hitParam_alg1_atot_thr = d[self.hitParam_alg1_atot_thr_str]
                                self.hitParam_alg1_son_min = d[self.hitParam_alg1_son_min_str]
                                self.hitParam_alg1_thr_low = d[self.hitParam_alg1_thr_low_str]
                                self.hitParam_alg1_thr_high = d[self.hitParam_alg1_thr_high_str]
                                self.hitParam_alg1_rank = int(d[self.hitParam_alg1_rank_str])
                                self.hitParam_alg1_radius = int(d[self.hitParam_alg1_radius_str])
                                self.hitParam_alg1_dr = d[self.hitParam_alg1_dr_str]
                                # Update GUI
                                self.doingUpdate = True
                                #self.p3.param(self.hitParam_grp, self.hitParam_algorithm_str).setValue(self.algorithm)
                                self.p3.param(self.hitParam_grp, self.hitParam_parameters_str, self.hitParam_alg1_npix_min_str).setValue(
                                    self.hitParam_alg1_npix_min)
                                self.p3.param(self.hitParam_grp, self.hitParam_parameters_str, self.hitParam_alg1_npix_max_str).setValue(
                                    self.hitParam_alg1_npix_max)
                                self.p3.param(self.hitParam_grp, self.hitParam_parameters_str, self.hitParam_alg1_amax_thr_str).setValue(
                                    self.hitParam_alg1_amax_thr)
                                self.p3.param(self.hitParam_grp, self.hitParam_parameters_str, self.hitParam_alg1_atot_thr_str).setValue(
                                    self.hitParam_alg1_atot_thr)
                                self.p3.param(self.hitParam_grp, self.hitParam_parameters_str, self.hitParam_alg1_son_min_str).setValue(
                                    self.hitParam_alg1_son_min)
                                self.p3.param(self.hitParam_grp, self.hitParam_parameters_str, self.hitParam_alg1_thr_low_str).setValue(
                                    self.hitParam_alg1_thr_low)
                                self.p3.param(self.hitParam_grp, self.hitParam_parameters_str, self.hitParam_alg1_thr_high_str).setValue(
                                    self.hitParam_alg1_thr_high)
                                self.p3.param(self.hitParam_grp, self.hitParam_parameters_str, self.hitParam_alg1_rank_str).setValue(
                                    self.hitParam_alg1_rank)
                                self.p3.param(self.hitParam_grp, self.hitParam_parameters_str, self.hitParam_alg1_radius_str).setValue(
                                    self.hitParam_alg1_radius)
                                self.doingUpdate = False
                                self.p3.param(self.hitParam_grp, self.hitParam_parameters_str, self.hitParam_alg1_dr_str).setValue(
                                    self.hitParam_alg1_dr)
                            except:
                                pass

    def writeStatus(self, fname, d):
        json.dump(d, open(fname, 'w'))

    # Launch peak finding
    def findPeaks(self):
        self.parent.autoPeakFinding = self.turnOnAutoPeaks
        self.parent.thread.append(LaunchPeakFinder.LaunchPeakFinder(self.parent)) # send parent parameters with self
        self.parent.thread[self.parent.threadCounter].launch(self.parent.experimentName, self.parent.detInfo)
        self.parent.threadCounter+=1
        # Save peak finding parameters
        runsToDo = self.digestRunList(self.hitParam_runs)
        for run in runsToDo:
            peakParamFname = self.parent.psocakeDir+'/r'+str(run).zfill(4)+'/peakParam'
            if self.tag: peakParamFname += '_'+self.tag
            peakParamFname += '.json'
            d = {self.hitParam_algorithm_str: self.algorithm,
                 self.hitParam_alg1_npix_min_str: self.hitParam_alg1_npix_min,
                 self.hitParam_alg1_npix_max_str: self.hitParam_alg1_npix_max,
                 self.hitParam_alg1_amax_thr_str: self.hitParam_alg1_amax_thr,
                 self.hitParam_alg1_atot_thr_str: self.hitParam_alg1_atot_thr,
                 self.hitParam_alg1_son_min_str: self.hitParam_alg1_son_min,
                 self.hitParam_alg1_thr_low_str: self.hitParam_alg1_thr_low,
                 self.hitParam_alg1_thr_high_str: self.hitParam_alg1_thr_high,
                 self.hitParam_alg1_rank_str: self.hitParam_alg1_rank,
                 self.hitParam_alg1_radius_str: self.hitParam_alg1_radius,
                 self.hitParam_alg1_dr_str: self.hitParam_alg1_dr}
            if not os.path.exists(self.parent.psocakeDir+'/r'+str(run).zfill(4)):
                os.mkdir(self.parent.psocakeDir+'/r'+str(run).zfill(4))
            self.writeStatus(peakParamFname, d)

    # If anything changes in the parameter tree, print a message
    def change(self, panel, changes):
        for param, change, data in changes:
            path = panel.childPath(param)
            if self.parent.args.v >= 1:
                print('  path: %s' % path)
                print('  change:    %s' % change)
                print('  data:      %s' % str(data))
                print('  ----------')
            self.paramUpdate(path, change, data)

    ##############################
    # Mandatory parameter update #
    ##############################
    def paramUpdate(self, path, change, data):
        if path[0] == self.hitParam_grp:
            if path[1] == self.hitParam_algorithm_str:
                self.algInitDone = False
                self.updateAlgorithm(data)
            elif path[1] == self.hitParam_showPeaks_str:
                self.showPeaks = data
                if self.showPeaks and self.doingUpdate is False:
                    self.updateClassification()
                if not self.showPeaks:
                    print("no need to index")
                    self.peaks = None
                    self.numPeaksFound = 0
                    self.drawPeaks()
                    self.parent.index.updateIndex()
            elif path[1] == self.hitParam_autoPeaks_str:
                ###########################
                self.turnOnAutoPeaks = data
                if self.showPeaks and self.doingUpdate is False:
                    self.updateClassification()
                ###########################
            elif path[1] == self.hitParam_outDir_str:
                self.hitParam_outDir = data
                self.hitParam_outDir_overridden = True
            elif path[1] == self.hitParam_runs_str:
                self.hitParam_runs = data
            elif path[1] == self.hitParam_queue_str:
                self.hitParam_queue = data
            elif path[1] == self.hitParam_cpu_str:
                self.hitParam_cpus = data
            elif path[1] == self.hitParam_noe_str:
                self.hitParam_noe = data
            elif path[1] == self.hitParam_threshold_str:
                self.hitParam_threshold = data
            elif path[1] == self.hitParam_launch_str:
                self.findPeaks()
            elif path[1] == self.save_minPeaks_str:
                self.minPeaks = data
            elif path[1] == self.save_maxPeaks_str:
                self.maxPeaks = data
            elif path[1] == self.save_minRes_str:
                self.minRes = data
            elif path[1] == self.save_sample_str:
                self.sample = data
            elif path[1] == self.tag_str:
                self.updateTag(data)
            elif path[1] == self.hitParam_extra_str:
                self.hitParam_extra = data
            elif path[2] == self.hitParam_alg1_npix_min_str and path[1] == self.hitParam_parameters_str:
                self.hitParam_alg1_npix_min = data
                self.algInitDone = False
                self.userUpdate = True
                if self.showPeaks and self.doingUpdate is False:
                    self.updateClassification()
            elif path[2] == self.hitParam_alg1_npix_max_str and path[1] == self.hitParam_parameters_str:
                self.hitParam_alg1_npix_max = data
                self.algInitDone = False
                self.userUpdate = True
                if self.showPeaks and self.doingUpdate is False:
                    self.updateClassification()
            elif path[2] == self.hitParam_alg1_amax_thr_str and path[1] == self.hitParam_parameters_str:
                self.hitParam_alg1_amax_thr = data
                self.algInitDone = False
                self.userUpdate = True
                if self.showPeaks and self.doingUpdate is False:
                    self.updateClassification()
            elif path[2] == self.hitParam_alg1_atot_thr_str and path[1] == self.hitParam_parameters_str:
                self.hitParam_alg1_atot_thr = data
                self.algInitDone = False
                self.userUpdate = True
                if self.showPeaks and self.doingUpdate is False:
                    self.updateClassification()
            elif path[2] == self.hitParam_alg1_son_min_str and path[1] == self.hitParam_parameters_str:
                self.hitParam_alg1_son_min = data
                self.algInitDone = False
                self.userUpdate = True
                if self.showPeaks and self.doingUpdate is False:
                    self.updateClassification()
            elif path[2] == self.hitParam_alg1_thr_low_str and path[1] == self.hitParam_parameters_str:
                self.hitParam_alg1_thr_low = data
                self.algInitDone = False
                self.userUpdate = True
                if self.showPeaks and self.doingUpdate is False:
                    self.updateClassification()
            elif path[2] == self.hitParam_alg1_thr_high_str and path[1] == self.hitParam_parameters_str:
                self.hitParam_alg1_thr_high = data
                self.algInitDone = False
                self.userUpdate = True
                if self.showPeaks and self.doingUpdate is False:
                    self.updateClassification()
            elif path[2] == self.hitParam_alg1_rank_str and path[1] == self.hitParam_parameters_str:
                self.hitParam_alg1_rank = data
                self.algInitDone = False
                self.userUpdate = True
                if self.showPeaks and self.doingUpdate is False:
                    self.updateClassification()
            elif path[2] == self.hitParam_alg1_radius_str and path[1] == self.hitParam_parameters_str:
                self.hitParam_alg1_radius = data
                self.algInitDone = False
                self.userUpdate = True
                if self.showPeaks and self.doingUpdate is False:
                    self.updateClassification()
            elif path[2] == self.hitParam_alg1_dr_str and path[1] == self.hitParam_parameters_str:
                self.hitParam_alg1_dr = data
                self.algInitDone = False
                self.userUpdate = True
                if self.showPeaks and self.doingUpdate is False:
                    self.updateClassification()

    def updateAlgorithm(self, data):
        self.algorithm = data
        self.algInitDone = False
        self.updateClassification()
        if self.parent.args.v >= 1: print("##### Done updateAlgorithm: ", self.algorithm)

    def updateTag(self, data):
        # tag can not have underscore
        if "_" in data:
            print(utils.highlight("Tag can not have underscore(s). Removing underscore(s).",status="r"))
            data = data.replace('_', '')
            self.p3.param(self.hitParam_grp, self.tag_str).setValue(data)
        if data.isdigit():
            print(utils.highlight("Tag can not just be a number. Appending a.",status="r"))
            data = data+"a"
            self.p3.param(self.hitParam_grp, self.tag_str).setValue(data)
        if self.tag:
            fname = self.parent.small.quantifier_filename.split("_"+self.tag+".cxi")[0]
        else:
            fname = self.parent.small.quantifier_filename.split(".cxi")[0]
        if data:
            fname += "_" + data + ".cxi"
        else:
            fname += ".cxi"
        self.tag = data
        self.parent.small.pSmall.param(self.parent.small.quantifier_grp, self.parent.small.quantifier_filename_str).setValue(fname)
        self.parent.small.reloadQuantifier()

    def saveCheetahFormat(self, arg):
        dim0, dim1 = self.parent.detDesc.tileDim

        if dim0 > 0:
            maxNumPeaks = 2048
            if self.parent.index.hiddenCXI is not None and self.peaks.shape[0] >= self.minPeaks:
                myHdf5 = h5py.File(self.parent.index.hiddenCXI, 'w')
                grpName = "/entry_1/result_1"
                dset_nPeaks = "/nPeaks"
                dset_posX = "/peakXPosRaw"
                dset_posY = "/peakYPosRaw"
                dset_atot = "/peakTotalIntensity"
                if grpName in myHdf5:
                    del myHdf5[grpName]
                myHdf5.create_group(grpName)
                myHdf5.create_dataset(grpName + dset_nPeaks, (1,), dtype='int')
                myHdf5.create_dataset(grpName + dset_posX, (1, maxNumPeaks), dtype='float32', chunks=(1, maxNumPeaks))
                myHdf5.create_dataset(grpName + dset_posY, (1, maxNumPeaks), dtype='float32', chunks=(1, maxNumPeaks))
                myHdf5.create_dataset(grpName + dset_atot, (1, maxNumPeaks), dtype='float32', chunks=(1, maxNumPeaks))

                myHdf5.create_dataset("/LCLS/detector_1/EncoderValue", (1,), dtype=float)
                myHdf5.create_dataset("/LCLS/photon_energy_eV", (1,), dtype=float)
                dset = myHdf5.create_dataset("/entry_1/data_1/data", (1, dim0, dim1), dtype=float)
                dsetM = myHdf5.create_dataset("/entry_1/data_1/mask", (dim0, dim1), dtype='int')

                # Convert calib image to cheetah image
                img = self.parent.detDesc.pct(self.parent.calib)
                mask = self.parent.detDesc.pct(self.parent.mk.combinedMask)
                dset[0, :, :] = img
                dsetM[:, :] = mask

                peaks = self.peaks.copy()
                nPeaks = peaks.shape[0]

                if nPeaks > maxNumPeaks:
                    peaks = peaks[:maxNumPeaks]
                    nPeaks = maxNumPeaks

                segs = peaks[:, 0]
                rows = peaks[:, 1]
                cols = peaks[:, 2]
                atots = peaks[:, 5]
                cheetahRows, cheetahCols = self.parent.detDesc.convert_peaks_to_cheetah(segs, rows, cols)
                myHdf5[grpName + dset_posX][0, :nPeaks] = cheetahCols
                myHdf5[grpName + dset_posY][0, :nPeaks] = cheetahRows
                myHdf5[grpName + dset_atot][0, :nPeaks] = atots
                myHdf5[grpName + dset_nPeaks][0] = nPeaks

                if self.parent.args.v >= 1: print("hiddenCXI clen (mm): ", self.parent.clen * 1000.)
                myHdf5["/LCLS/detector_1/EncoderValue"][0] = self.parent.clen * 1000.  # mm
                myHdf5["/LCLS/photon_energy_eV"][0] = self.parent.photonEnergy
                myHdf5.close()

    def updateClassification(self):
        if self.parent.calib is not None:
            if self.parent.mk.streakMaskOn:
                self.parent.mk.initMask()
                self.parent.mk.streakMask = self.parent.mk.StreakMask.getStreakMaskCalib(self.parent.evt)
                if self.parent.mk.streakMask is None:
                    self.parent.mk.streakMaskAssem = None
                else:
                    self.parent.mk.streakMaskAssem = self.parent.det.image(self.parent.evt, self.parent.mk.streakMask)
                self.algInitDone = False

            self.parent.mk.displayMask()

            # update combined mask
            self.parent.mk.combinedMask = np.ones_like(self.parent.calib)
            if self.parent.mk.streakMask is not None and self.parent.mk.streakMaskOn is True:
                self.parent.mk.combinedMask *= self.parent.mk.streakMask
            if self.parent.mk.userMask is not None and self.parent.mk.userMaskOn is True:
                self.parent.mk.combinedMask *= self.parent.mk.userMask
            if self.parent.mk.psanaMask is not None and self.parent.mk.psanaMaskOn is True:
                self.parent.mk.combinedMask *= self.parent.mk.psanaMask

            # Peak output (0-16):
            # 0 seg
            # 1 row
            # 2 col
            # 3 npix: no. of pixels in the ROI intensities above threshold
            # 4 amp_max: max intensity
            # 5 amp_tot: sum of intensities
            # 6,7: row_cgrav: center of mass
            # 8,9: row_sigma
            # 10,11,12,13: minimum bounding box
            # 14: background
            # 15: noise
            # 16: signal over noise
            if self.algorithm == 0: # No peak algorithm
                self.peaks = None
                self.drawPeaks()
            else:
                # Only initialize the hit finder algorithm once
                if self.algInitDone is False:
                    self.windows = None
                    self.alg = []
                    # set peak-selector parameters:
                    if self.algorithm == 1:
                        self.alg = PyAlgos(mask=None, pbits=0)
                        self.peakRadius = int(self.hitParam_alg1_radius)
                        self.alg.set_peak_selection_pars(npix_min=self.hitParam_alg1_npix_min, npix_max=self.hitParam_alg1_npix_max, \
                                                amax_thr=self.hitParam_alg1_amax_thr, atot_thr=self.hitParam_alg1_atot_thr, \
                                                son_min=self.hitParam_alg1_son_min)
                    elif self.algorithm == 2:
                        self.alg = PyAlgos(mask=None, pbits=0)
                        self.peakRadius = int(self.hitParam_alg1_radius)
                        self.alg.set_peak_selection_pars(npix_min=self.hitParam_alg1_npix_min, npix_max=self.hitParam_alg1_npix_max, \
                                                amax_thr=self.hitParam_alg1_amax_thr, atot_thr=self.hitParam_alg1_atot_thr, \
                                                son_min=self.hitParam_alg1_son_min)
                    elif self.algorithm == 3:
                        self.alg = PyAlgos(mask=None, pbits=0)
                        self.peakRadius = int(self.hitParam_alg1_radius)
                        self.alg.set_peak_selection_pars(npix_min=self.hitParam_alg1_npix_min, npix_max=self.hitParam_alg1_npix_max, \
                                                amax_thr=self.hitParam_alg1_amax_thr, atot_thr=self.hitParam_alg1_atot_thr, \
                                                son_min=self.hitParam_alg1_son_min)
                    ix = self.parent.det.indexes_x(self.parent.evt)
                    iy = self.parent.det.indexes_y(self.parent.evt)
                    self.iX = np.array(ix, dtype=np.int64)
                    self.iY = np.array(iy, dtype=np.int64)

                    self.algInitDone = True

                self.parent.calib = self.parent.calib * 1.0 # Neccessary when int is returned

                if self.algorithm == 1:
                    # v1 - aka Droplet Finder - two-threshold peak-finding algorithm in restricted region
                    #                           around pixel with maximal intensity.
                    if not self.turnOnAutoPeaks:
                        self.peakRadius = int(self.hitParam_alg1_radius)
                        self.peaks = self.alg.peak_finder_v4r3(self.parent.calib,
                                                               thr_low=self.hitParam_alg1_thr_low,
                                                               thr_high=self.hitParam_alg1_thr_high,
                                                               rank=int(self.hitParam_alg1_rank),
                                                               r0=self.peakRadius,
                                                               dr=self.hitParam_alg1_dr,
                                                               mask=self.parent.mk.combinedMask.astype(np.uint16))
                    else:
                        ################################
                        # Determine thr_high and thr_low
                        if self.ind is None:
                            with pg.BusyCursor():
                                powderSumFname = self.parent.psocakeRunDir + '/' + \
                                            self.parent.experimentName + '_' + \
                                            str(self.parent.runNumber).zfill(4) + '_' + \
                                            self.parent.detInfo + '_mean.npy'
                                if not os.path.exists(powderSumFname):
                                    # Generate powder
                                    cmd = "mpirun -n 6 generatePowder exp=" + self.parent.experimentName + \
                                          ":run=" + str(self.parent.runNumber) + " -d " + self.parent.detInfo + \
                                          " -o " + self.parent.psocakeRunDir
                                    cmd += " -n " + str(256)
                                    cmd += " --random"
                                    print("Running on local machine: ", cmd)
                                    process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
                                                               shell=True)
                                    out, err = process.communicate()
                                # Copy as background.npy
                                import shutil
                                shutil.copyfile(powderSumFname, self.parent.psocakeRunDir + '/background.npy')

                                # Read in powder pattern and calculate pixel indices
                                powderSum = np.load(powderSumFname)
                                powderSum1D = powderSum.ravel()
                                cy, cx = self.parent.det.indexes_xy(self.parent.evt)
                                #ipx, ipy = self.parent.det.point_indexes(self.parent.evt, pxy_um=(0, 0))
                                try:
                                    ipy, ipx = self.parent.det.point_indexes(self.parent.evt, pxy_um=(0, 0),
                                                                          pix_scale_size_um=None,
                                                                          xy0_off_pix=None,
                                                                          cframe=gu.CFRAME_PSANA, fract=True)
                                except AttributeError:
                                    ipx, ipy = self.parent.det.point_indexes(self.parent.evt, pxy_um=(0, 0))
                                r = np.sqrt((cx - ipx) ** 2 + (cy - ipy) ** 2).ravel().astype(int)
                                startR = 0
                                endR = np.max(r)
                                profile = np.zeros(endR - startR, )
                                for i, val in enumerate(np.arange(startR, endR)):
                                    ind = np.where(r == val)[0].astype(int)
                                    if len(ind) > 0: profile[i] = np.mean(powderSum1D[ind])
                                myThreshInd = np.argmax(profile)
                                print("###################################################")
                                print("Solution scattering radius (pixels): ", myThreshInd)
                                print("###################################################")
                                thickness = 10
                                indLo = np.where(r >= myThreshInd - thickness / 2.)[0].astype(int)
                                indHi = np.where(r <= myThreshInd + thickness / 2.)[0].astype(int)
                                self.ind = np.intersect1d(indLo, indHi)

                                ix = self.parent.det.indexes_x(self.parent.evt)
                                iy = self.parent.det.indexes_y(self.parent.evt)
                                self.iX = np.array(ix, dtype=np.int64)
                                self.iY = np.array(iy, dtype=np.int64)

                        calib1D = self.parent.calib.ravel()
                        mean = np.mean(calib1D[self.ind])
                        spread = np.std(calib1D[self.ind])
                        highSigma = 3.5
                        lowSigma = 2.5
                        thr_high = int(mean + highSigma * spread + 50)
                        thr_low = int(mean + lowSigma * spread + 50)

                        self.peaks = self.alg.findPeaks(self.parent.calib,
                                                        npix_min=self.hitParam_alg1_npix_min,
                                                        npix_max=self.hitParam_alg1_npix_max,
                                                        atot_thr=0, son_min=self.hitParam_alg1_son_min,
                                                        thr_low=thr_low,
                                                        thr_high=thr_high,
                                                        mask=self.parent.mk.combinedMask.astype(np.uint16))
                elif self.algorithm == 2:
                    # v2 - aka Adaptive peak finder
                    self.peaks = self.alg.peak_finder_v3r3(self.parent.calib,
                                                           rank=int(self.hitParam_alg1_rank),
                                                           r0=self.peakRadius,
                                                           dr=self.hitParam_alg1_dr,
                                                           nsigm=self.hitParam_alg1_son_min,
                                                           mask=self.parent.mk.combinedMask.astype(np.uint16))#)#thr=self.hitParam_alg2_thr, r0=self.peakRadius, dr=self.hitParam_alg2_dr)
                elif self.algorithm == 3:
                    # perform binning here
                    binr = 2
                    binc = 2
                    downCalib = sm.block_reduce(self.parent.calib, block_size=(1, binr, binc), func=np.max)
                    downWeight = sm.block_reduce(self.parent.mk.combinedMask, block_size=(1, binr, binc), func=np.max)
                    warr = np.zeros_like(downCalib, dtype='float32')
                    ind = np.where(downWeight > 0)
                    warr[ind] = downCalib[ind] / downWeight[ind]
                    upCalib = utils.upsample(warr, self.parent.calib.shape, binr, binc)
                    self.peaks = self.alg.peak_finder_v3r3(upCalib,
                                                           rank=int(self.hitParam_alg1_rank),
                                                           r0=self.peakRadius,
                                                           dr=self.hitParam_alg1_dr,
                                                           nsigm=self.hitParam_alg1_son_min,
                                                           mask=self.parent.mk.combinedMask.astype(np.uint16))
                self.numPeaksFound = self.peaks.shape[0]

                if self.numPeaksFound > self.minPeaks and self.numPeaksFound < self.maxPeaks:# and self.turnOnAutoPeaks:
                    cenX = self.iX[np.array(self.peaks[:, 0], dtype=np.int64),
                                   np.array(self.peaks[:, 1], dtype=np.int64),
                                   np.array(self.peaks[:, 2], dtype=np.int64)] + 0.5
                    cenY = self.iY[np.array(self.peaks[:, 0], dtype=np.int64),
                                   np.array(self.peaks[:, 1], dtype=np.int64),
                                   np.array(self.peaks[:, 2], dtype=np.int64)] + 0.5

                    x = cenX - self.parent.cx # args.center[0]
                    y = cenY - self.parent.cy # args.center[1]

                    pixSize = float(self.parent.det.pixel_size(self.parent.evt))
                    detdis = float(self.parent.detectorDistance)
                    z = detdis / pixSize * np.ones(x.shape)  # pixels
                    wavelength = 12.407002 / float(self.parent.photonEnergy)  # Angstrom
                    norm = np.sqrt(x ** 2 + y ** 2 + z ** 2)
                    qPeaks = (np.array([x, y, z]) / norm - np.array([[0.], [0.], [1.]])) / wavelength
                    [meanClosestNeighborDist, self.pairsFoundPerSpot] = self.calculate_likelihood(qPeaks)
                else:
                    self.pairsFoundPerSpot = 0
                if self.parent.args.v >= 1: print("Num peaks found: ", self.numPeaksFound, self.peaks.shape, self.pairsFoundPerSpot)

                # update clen
                self.parent.geom.updateClen(self.parent.facility)

                self.parent.index.clearIndexedPeaks()

                # Save image and peaks in cheetah cxi file
                self.saveCheetahFormat(self.parent.facility)

                if self.parent.index.showIndexedPeaks: self.parent.index.updateIndex()

                self.drawPeaks()
            if self.parent.args.v >= 1: print("Done updateClassification")

    def calculate_likelihood(self, qPeaks):

        nPeaks = int(qPeaks.shape[1])
        selfD = distance.cdist(qPeaks.transpose(), qPeaks.transpose(), 'euclidean')
        sortedSelfD = np.sort(selfD)
        closestNeighborDist = sortedSelfD[:, 1]
        meanClosestNeighborDist = np.median(closestNeighborDist)
        closestPeaks = [None] * nPeaks
        coords = qPeaks.transpose()
        pairsFound = 0.

        for ii in range(nPeaks):
            index = np.where(selfD[ii, :] == closestNeighborDist[ii])
            closestPeaks[ii] = coords[list(index[0]), :].copy()
            p = coords[ii, :]
            flip = 2 * p - closestPeaks[ii]
            d = distance.cdist(coords, flip, 'euclidean')
            sigma = closestNeighborDist[ii] / 4.
            mu = 0.
            bins = d
            vals = np.exp(-(bins - mu) ** 2 / (2. * sigma ** 2))
            weight = np.sum(vals)
            pairsFound += weight

        pairsFound = pairsFound / 2.
        pairsFoundPerSpot = pairsFound / float(nPeaks)

        return [meanClosestNeighborDist, pairsFoundPerSpot]

    def getMaxRes(self, posX, posY, centerX, centerY):
        maxRes = np.max(np.sqrt((posX-centerX)**2 + (posY-centerY)**2))
        if self.parent.args.v >= 1: print("maxRes: ", maxRes)
        return maxRes # in pixels

    def assemblePeakPos(self, peaks):
        self.ix = self.parent.det.indexes_x(self.parent.evt)
        self.iy = self.parent.det.indexes_y(self.parent.evt)
        if self.ix is None:
            (_, dim0, dim1) = self.parent.calib.shape
            self.iy = np.tile(np.arange(dim0), [dim1, 1])
            self.ix = np.transpose(self.iy)
        self.iX = np.array(self.ix, dtype=np.int64)
        self.iY = np.array(self.iy, dtype=np.int64)
        if len(self.iX.shape) == 2:
            self.iX = np.expand_dims(self.iX, axis=0)
            self.iY = np.expand_dims(self.iY, axis=0)

        cenX = self.iX[np.array(peaks[:, 0], dtype=np.int64), np.array(peaks[:, 1], dtype=np.int64), np.array(
            peaks[:, 2], dtype=np.int64)] + 0.5
        cenY = self.iY[np.array(peaks[:, 0], dtype=np.int64), np.array(peaks[:, 1], dtype=np.int64), np.array(
            peaks[:, 2], dtype=np.int64)] + 0.5
        return cenX, cenY

    def drawPeaks(self):
        self.parent.img.clearPeakMessage()
        if self.showPeaks:
            if self.peaks is not None and self.numPeaksFound > 0:
                cenX, cenY = self.assemblePeakPos(self.peaks)
                self.peaksMaxRes = self.getMaxRes(cenX, cenY, self.parent.cx, self.parent.cy)
                diameter = self.peakRadius*2+1
                self.parent.img.peak_feature.setData(cenY, cenX, symbol='s', \
                                          size=diameter, brush=(255,255,255,0), \
                                          pen=pg.mkPen({'color': "c", 'width': 4}), pxMode=False) #FF0
                # Write number of peaks found
                xMargin = -self.parent.data.shape[0]#5 # pixels
                yMargin = 5#0  # pixels
                maxX = np.max(self.ix) + xMargin
                maxY = np.max(self.iy) - yMargin
                myMessage = '<div style="text-align: center"><span style="color: cyan; font-size: 12pt;">Peaks=' + \
                            str(self.numPeaksFound) + \
                            ' <br>Res=' + str(int(self.peaksMaxRes)) + \
                            ' <br>Like=' + str(round(self.pairsFoundPerSpot,3)) + \
                            '<br></span></div>'
                self.parent.img.peak_text = pg.TextItem(html=myMessage, anchor=(0, 0))
                self.parent.img.win.getView().addItem(self.parent.img.peak_text)
                self.parent.img.peak_text.setPos(maxY, maxX)
            else:
                self.parent.img.peak_feature.setData([], [], pxMode=False)
                self.parent.img.peak_text = pg.TextItem(html='', anchor=(0, 0))
                self.parent.img.win.getView().addItem(self.parent.img.peak_text)
                self.parent.img.peak_text.setPos(0,0)
        else:
            self.parent.img.peak_feature.setData([], [], pxMode=False)

        if self.hitParam_extra.endswith('.cxi'): # Draw peaks from a cxi file
            # Read cxi file containing peaks
            f = h5py.File(self.hitParam_extra)
            npeaks = f['/entry_1/result_1/nPeaksAll'][self.parent.eventNumber]
            row2d = f['/entry_1/result_1/peakYPosRawAll'][self.parent.eventNumber, :npeaks]
            col2d = f['/entry_1/result_1/peakXPosRawAll'][self.parent.eventNumber, :npeaks]
            f.close()
            # Convert cheetah peaks to psana peaks
            s, r, c = self.parent.detDesc.convert_peaks_to_psana(row2d, col2d)
            if not isinstance(s, list): s = np.full(len(r), s)
            peaks = np.array([s, r, c]).T
            # Display peaks
            if peaks is not None and len(peaks) > 0:
                if self.parent.facility == self.parent.facilityLCLS:
                    cenX, cenY = self.assemblePeakPos(peaks)
                    diameter = int(self.hitParam_alg1_radius) * 2 + 1
                    self.parent.img.filePeak_feature.setData(cenY, cenX, symbol='s', \
                                                         size=diameter, brush=(255, 255, 255, 0), \
                                                         pen=pg.mkPen({'color': "y", 'width': 2}), pxMode=False)  # FF0
            else:
                self.parent.img.filePeak_feature.setData([], [], pxMode=False)
        else:
            self.parent.img.filePeak_feature.setData([], [], pxMode=False)
        if self.parent.args.v >= 1: print("Done drawPeaks")
        self.parent.geom.drawCentre()


import shutil
import os
import glob

directory_patch = r'C:\Users\Professional\Documents\Нерасортировано'
from_patch = r'C:\Users\Professional\Desktop'
extension = '*.mp3', "*.mp4", '*.docx', '*.doc', '*.mp4', '*.pdf', '*.jpg', '*.png', '*.rar'

while True:
    for e in extension:
        for i in glob.glob(from_patch + '\\' + e):
            shutil.move(i, directory_patch)


game_logic = {
    "rock": {
        "lose": "paper",
        "win": "scissors",
    },
    "paper": {
        "lose": "scissors",
        "win": "rock",
    },
    "scissors": {
        "lose": "rock",
        "win": "paper",
    },
}

player_a_input_dict = {
    "A": "rock",
    "B": "paper",
    "C": "scissors",
}

result_input_dict = {"X": "lose", "Y": "draw", "Z": "win"}

type_score = {"rock": 1, "paper": 2, "scissors": 3}

result_score = {
    "lose": 0,
    "draw": 3,
    "win": 6,
}

def _predict_item(opponent_item: str, result: str) -> str:
    if result == "draw":
        return opponent_item
    if result == "win":
        result = "lose"
    elif result == "lose":
        result = "win"
    return game_logic[opponent_item][result]

def _calculate_item_score(item: str) -> int:
    return type_score[item]

def _calculate_score(player_a_choose: str, predicted_result: str) -> int:
    opponent_item = player_a_input_dict[player_a_choose]
    result = result_input_dict[predicted_result]
    item = _predict_item(opponent_item, result)

    return _calculate_item_score(item) + result_score[result]

def calculate_score_from_input_file() -> int:
    with open("input") as input_file:
        total_score = 0
        for line in input_file:
            a_choose, b_choose = line.strip().split(" ")
            total_score += _calculate_score(a_choose, b_choose)
    return total_score

print(calculate_score_from_input_file())


# Importing libraries
from tkinter import Tk,END,DISABLED,NORMAL,RIGHT,BOTH,Entry,Menu,Frame,messagebox,HORIZONTAL,X
from tkinter.ttk import Scrollbar as ttk_scroll,Button as ttk_btn

root=Tk()
root.resizable(height=False,width=False) # Disable resizing of window abrubtly
root.iconbitmap('Calc_Icon.ico') # Sets Icon
root.title('Calculator') #Sets Title

# Don't touch keyboard
def keyboardused(e):
	entry_box.delete(0,END)
	entry_box['state']=DISABLED

# Create a screen
entry_box=Entry(root,font=('Agency FB',15,'bold'),justify=RIGHT)
entry_box.pack(pady=5,fill=BOTH)
entry_box['state']=DISABLED
entry_box.bind('<KeyPress>',keyboardused)

# Frame to keep buttons
button_frame=Frame(root,bd=0)
button_frame.pack(pady=5)

keys=[('ON/OFF',0,0),('C',0,1),('%',0,2),('DEL',0,3),('7',1,0),('8',1,1),('9',1,2),('÷',1,3),('4',2,0),('5',2,1),('6',2,2),('x',2,3),('1',3,0),('2',3,1),('3',3,2),('-',3,3),('0',4,0),('.',4,1),('=',4,2),('+',4,3)]

# Calculation function
def clicked(e):
	if e.widget.cget('text')=='=':
		try:
			raw_data=entry_box.get().replace('%','/100x')
			raw_data=raw_data.replace('x','*')
			raw_data=raw_data.replace('÷','/')
			data=eval(raw_data)
			entry_box.delete(0,END)
			entry_box.insert(0,f'{data}')
		except:
			entry_box.delete(0,END)
			entry_box.insert(0,'Error')

	elif e.widget.cget('text')=='C':
		entry_box.delete(0,END)

	elif e.widget.cget('text')=='ON/OFF':
		if entry_box.cget('state')=='normal':
			entry_box.delete(0,END)
			entry_box['state']=DISABLED
		else:
			entry_box['state']=NORMAL

	elif e.widget.cget('text')=='DEL':
		entry_box.delete(len(entry_box.get())-1,END)

	else:
		if entry_box.get()=='Error':
			entry_box.delete(0,END)
		curr_pressed_button=e.widget.cget('text')
		data=entry_box.get()+curr_pressed_button
		entry_box.delete(0,END)
		entry_box.insert(0,data)

# Key building area
for i in keys:
	k=ttk_btn(button_frame,text=i[0],takefocus=False)
	k.grid(row=i[1],column=i[2])
	k.bind('<ButtonRelease-1>',clicked)

# Create a scrollbar
entry_scroll=ttk_scroll(root,command=entry_box.xview,orient=HORIZONTAL)
entry_scroll.pack(fill=X)
entry_box.config(xscrollcommand=entry_scroll.set)

# Menu area
my_menu=Menu(root)
root.config(menu=my_menu)

option_menu=Menu(my_menu)

# Information popup
def showinfos():
	messagebox.showinfo('Information!','''Hello!\n\n1. Use "÷÷" to get only the integer value while dividing.\n\tFor example: 5÷÷2 = 2\n\n2. Use "**" to find the square,cube,... on a number.\n\tFor example:\n\t1**2 = 1\n\t2**2 = 4\n\t2**3 = 8\n\tand many more.''')

my_menu.add_command(label='Info',command=showinfos)
my_menu.add_command(label='Close',command=root.destroy)

root.mainloop()

# Thanks For Using


import streamlit as st
import pandas as pd
import numpy as np
import time

df = pd.DataFrame({
  'first column': [1, 2, 3, 4],
  'second column': [10, 20, 30, 40]
})

st.write("Here's our first attempt at using data to create a table:")

st.write(pd.DataFrame({
    'first column': [1, 2, 3, 4],
    'second column': [10, 20, 30, 40]
}))

st.table(df)

dataframe = pd.DataFrame(
    np.random.randn(10, 20),
    columns=(f"col{i}" for i in range(20))
)

st.dataframe(dataframe.style.highlight_max(axis=0))
st.table(dataframe.style.highlight_max(axis=0))

chart_data = pd.DataFrame(
     np.random.randn(20, 3),
     columns=['a', 'b', 'c'])

st.line_chart(chart_data)

map_data = pd.DataFrame(
    np.random.randn(1000, 2) / [50, 50] + [37.76, -122.4],
    columns=['lat', 'lon'])

#st.map(map_data)

x = st.slider('x')  # ðŸ‘ˆ this is a widget

st.write(x, "squared is", x * x)

st.text_input("Your name", key='name')
st.session_state.name

st.cache_data()
if st.checkbox('Show dataframe'):
    chart_data = pd.DataFrame(
       np.random.randn(20, 3),
       columns=['a', 'b', 'c'])

    chart_data

if st.checkbox("plotline"):
    st.line_chart(chart_data)

df = pd.DataFrame({
    "first column": [1,2,3,4],
    "second column": [10,20,30,40]
})

option = st.selectbox(
    "Which number do you like best?",
    df["first column"]
)

"you selected:", option

# Add a selectbox to the sidebar:
add_selectbox = st.sidebar.selectbox(
    'How would you like to be contacted?',
    ('Email', 'Home phone', 'Mobile phone')
)

# Add a slider to the sidebar:
add_slider = st.sidebar.slider(
    'Select a range of values',
    0.0, 100.0, (25.0, 75.0)
)

left_column, right_column = st.columns(2)

left_column.button("Press me")

with right_column:
    chosen = st.radio(
        'Sorting hat',
        ("Gryffindor", "Ravenclaw", "Hufflepuff", "Slytherin"))
    st.write(f"You are in {chosen} house!")

latest_iteration = st.empty()
bar = st.progress(0)

for i in range(100):
  # Update the progress bar with each iteration.
  latest_iteration.text(f'Iteration {i+1}')
  bar.progress(i + 1)
  time.sleep(0.01)

'...and now we\'re done!'

st.title('Uber pickups in NYC')

DATE_COLUMN = 'date/time'
DATA_URL = ('https://s3-us-west-2.amazonaws.com/'
            'streamlit-demo-data/uber-raw-data-sep14.csv.gz')

@st.cache_data
def load_data(nrows):
    data = pd.read_csv(DATA_URL, nrows=nrows)
    lowercase = lambda x: str(x).lower()
    data.rename(lowercase, axis='columns', inplace=True)
    data[DATE_COLUMN] = pd.to_datetime(data[DATE_COLUMN])
    return data

data_load_state = st.text('Loading data...')
data = load_data(10000)
data_load_state.text("Done! (using st.cache_data)")

if st.checkbox('Show raw data'):
    st.subheader('Raw data')
    st.write(data)

st.subheader('Number of pickups by hour')
hist_values = np.histogram(data[DATE_COLUMN].dt.hour, bins=24, range=(0,24))[0]
st.bar_chart(hist_values)

# Some number in the range 0-23
hour_to_filter = st.slider('hour', 0, 23, 17)
filtered_data = data[data[DATE_COLUMN].dt.hour == hour_to_filter]

st.subheader('Map of all pickups at %s:00' % hour_to_filter)
st.map(filtered_data)
# Draw a title and some text to the app:
'''
# This is the document title

This is some _markdown_.
'''

import pandas as pd
df = pd.DataFrame({'col1': [1,2,3]})
df  # ðŸ‘ˆ Draw the dataframe

x = 10
'x', x  # ðŸ‘ˆ Draw the string 'x' and then the value of x

# Also works with most supported chart types
import matplotlib.pyplot as plt
import numpy as np

arr = np.random.normal(1, 1, size=100)
fig, ax = plt.subplots()
ax.hist(arr, bins=20)

fig  # ðŸ‘ˆ Draw a Matplotlib chart


from myapp import myobj
from myapp import db
from myapp.forms import TopCities
from myapp.models import City
from flask import render_template, escape, flash, redirect

@myobj.route("/", methods=['GET', 'POST'])
def home():
    title = "Top Cities"
    name = "Vidhyut"
    top_cities = City.query.all()
    form = TopCities()
    if form.validate_on_submit():
        city = City(city_name = form.city_name.data, city_rank = form.city_rank.data, is_visited = form.is_visited.data)
        db.session.add(city)
        db.session.commit()
        flash(f'City {form.city_name.data} added into list!')
        return redirect("/")
    return render_template("home.html", title = title, name = name, top_cities = top_cities, form = form)


# -*- coding: utf-8 -*-

"""
Understand the problem. We'll look at each variable and do a philosophical analysis about their meaning and importance for this problem.
Univariable study. We'll just focus on the dependent variable ('SalePrice') and try to know a little bit more about it.
Multivariate study. We'll try to understand how the dependent variable and independent variables relate.
Basic cleaning. We'll clean the dataset and handle the missing data, outliers and categorical variables.
Test assumptions. We'll check if our data meets the assumptions required by most multivariate techniques.

1. What can we expect?
In order to understand our data, we can look at each variable and try to understand their meaning and relevance to this problem. I know this is time-consuming, but it will give us the flavour of our dataset.

In order to have some discipline in our analysis, we can create an Excel spreadsheet with the following columns:

Variable - Variable name.
Type - Identification of the variables' type. There are two possible values for this field: 'numerical' or 'categorical'. By 'numerical' we mean variables for which the values are numbers, and by 'categorical' we mean variables for which the values are categories.
Segment - Identification of the variables' segment. We can define three possible segments: building, space or location. When we say 'building', we mean a variable that relates to the physical characteristics of the building (e.g. 'OverallQual'). When we say 'space', we mean a variable that reports space properties of the house (e.g. 'TotalBsmtSF'). Finally, when we say a 'location', we mean a variable that gives information about the place where the house is located (e.g. 'Neighborhood').
Expectation - Our expectation about the variable influence in 'SalePrice'. We can use a categorical scale with 'High', 'Medium' and 'Low' as possible values.
Conclusion - Our conclusions about the importance of the variable, after we give a quick look at the data. We can keep with the same categorical scale as in 'Expectation'.
Comments - Any general comments that occured to us.

I went through this process and concluded that the following variables can play an important role in this problem:

OverallQual (which is a variable that I don't like because I don't know how it was computed; a funny exercise would be to predict 'OverallQual' using all the other variables available).
YearBuilt.
TotalBsmtSF.
GrLivArea.
"""
import pandas as pd
import numpy as np
import os
import warnings
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.preprocessing import StandardScaler
from scipy import stats

warnings.filterwarnings('ignore')
path = os.path.abspath("House_Price_Predection/Data/train.csv")

df_train  = pd.read_csv(path)

df_train.columns

"""
First things first: analysing 'SalePrice'
"""
df_train['SalePrice'].describe()

sns.distplot(df_train['SalePrice'])

"""
Deviate from the normal distribution.
Have appreciable positive skewness.
Show peakedness.

Normal Distribution :
    Skewness= 0
    if skewness is <-1 and > 1 distribution is highly skewed
    if skewness is between -1 and -0.5 | 0.5 and 1  distribution is moderately skewed
    if skewness is between -0.5 and 0.5 distribution is symmetrical

"""

# Print skewness and kurtosis
print("Skewnesss" , df_train['SalePrice'].skew())
print("Kurtosis" , df_train['SalePrice'].kurt())

#log
sns.distplot(np.log(df_train['SalePrice']))
print("Skewnesss" , np.log(df_train['SalePrice'].skew()))
print("Kurtosis" , np.log(df_train['SalePrice'].kurt()))

#scatter plot grlivarea/saleprice
df_train['GrLivArea']
var = 'GrLivArea'
data = pd.concat([df_train['SalePrice'], df_train[var]], axis=1)
data.plot.scatter(x=var, y='SalePrice' , ylim=(0, 800000))

#scatter plot totalbsmtsf/saleprice
var = 'TotalBsmtSF'
data = pd.concat([df_train['SalePrice'], df_train[var]], axis=1)
data.plot.scatter(x=var, y='SalePrice', ylim=(0,800000));

#Relationship with categorical features
#box plot overallqual/saleprice
var = 'OverallQual'
data = pd.concat([df_train['SalePrice'], df_train[var]], axis=1)
f, ax = plt.subplots(figsize=(8, 6))
fig = sns.boxplot(x=var, y="SalePrice", data=data)
fig.axis(ymin=0, ymax=800000);

var = 'YearBuilt'
data = pd.concat([df_train['SalePrice'], df_train[var]], axis=1)
f, ax = plt.subplots(figsize=(16, 8))
fig = sns.boxplot(x=var, y="SalePrice", data=data)
fig.axis(ymin=0, ymax=800000);
plt.xticks(rotation=90);

"""
Correlation matrix (heatmap style).
'SalePrice' correlation matrix (zoomed heatmap style).
Scatter plots between the most correlated variables (move like Jagger style).
"""
#Correlation matrix (heatmap style)

corrmat = df_train.corr()
f, ax = plt.subplots(figsize=(12, 9))
sns.heatmap(corrmat, vmax=.8, square=True);

"""
At first sight, there are two red colored squares that get my attention.
The first one refers to the 'TotalBsmtSF' and '1stFlrSF' variables, and the second one refers to the 'GarageX' variables. Both cases show how significant the correlation is between these variables. Actually, this correlation is so strong that it can indicate a situation of multicollinearity. If we think about these variables, we can conclude that they give almost the same information so multicollinearity really occurs. Heatmaps are great to detect this kind of situations and in problems dominated by feature selection, like ours, they are an essential tool.

Another thing that got my attention was the 'SalePrice' correlations.
We can see our well-known 'GrLivArea', 'TotalBsmtSF', and 'OverallQual' saying a big 'Hi!', but we can also see many other variables that should be taken into account. That's what we will do next.

'SalePrice' correlation matrix (zoomed heatmap style)
"""
#saleprice correlation matrix
k = 10 #number of variables for heatmap
cols = corrmat.nlargest(k, 'SalePrice')['SalePrice'].index
cm = np.corrcoef(df_train[cols].values.T)
sns.set(font_scale=1.25)
hm = sns.heatmap(cm, cbar=True, annot=True, square=True, fmt='.2f', annot_kws={'size': 10}, yticklabels=cols.values, xticklabels=cols.values)
plt.show()

"""
According to our crystal ball, these are the variables most correlated with 'SalePrice'. My thoughts on this:

'OverallQual', 'GrLivArea' and 'TotalBsmtSF' are strongly correlated with 'SalePrice'. Check!
'GarageCars' and 'GarageArea' are also some of the most strongly correlated variables. However, as we discussed in the last sub-point, the number of cars that fit into the garage is a consequence of the garage area. 'GarageCars' and 'GarageArea' are like twin brothers. You'll never be able to distinguish them. Therefore, we just need one of these variables in our analysis (we can keep 'GarageCars' since its correlation with 'SalePrice' is higher).
'TotalBsmtSF' and '1stFloor' also seem to be twin brothers. We can keep 'TotalBsmtSF' just to say that our first guess was right (re-read 'So... What can we expect?').
'FullBath'?? Really?
'TotRmsAbvGrd' and 'GrLivArea', twin brothers again. Is this dataset from Chernobyl?
Ah... 'YearBuilt'... It seems that 'YearBuilt' is slightly correlated with 'SalePrice'. Honestly, it scares me to think about 'YearBuilt' because I start feeling that we should do a little bit of time-series analysis to get this right. I'll leave this as a homework for you.
"""

"""
Scatter plots between 'SalePrice' and correlated variables (move like Jagger style)
Get ready for what you're about to see. I must confess that the first time I saw these scatter plots I was totally blown away! So much information in so short space... It's just amazing. Once more, thank you @seaborn! You make me 'move like Jagger'!
"""
#scatterplot
sns.set()
cols = ['SalePrice', 'OverallQual', 'GrLivArea', 'GarageCars', 'TotalBsmtSF', 'FullBath', 'YearBuilt']
sns.pairplot(df_train[cols], size = 2.5)
plt.show();

"""
lthough we already know some of the main figures, this mega scatter plot gives us a reasonable idea about variables relationships.

One of the figures we may find interesting is the one between 'TotalBsmtSF' and 'GrLiveArea'. In this figure we can see the dots drawing a linear line, which almost acts like a border. It totally makes sense that the majority of the dots stay below that line. Basement areas can be equal to the above ground living area, but it is not expected a basement area bigger than the above ground living area (unless you're trying to buy a bunker).

The plot concerning 'SalePrice' and 'YearBuilt' can also make us think. In the bottom of the 'dots cloud', we see what almost appears to be a shy exponential function (be creative). We can also see this same tendency in the upper limit of the 'dots cloud' (be even more creative). Also, notice how the set of dots regarding the last years tend to stay above this limit (I just wanted to say that prices are increasing faster now).

Ok, enough of Rorschach test for now. Let's move forward to what's missing: missing data!
"""
#4. Missing data¶
"""
How prevalent is the missing data?
Is missing data random or does it have a pattern?
The answer to these questions is important for practical reasons because missing data can imply a reduction of the sample size. This can prevent us from proceeding with the analysis. Moreover, from a substantive perspective, we need to ensure that the missing data process is not biased and hidding an inconvenient truth.
"""
#missing data
total = df_train.isnull().sum().sort_values(ascending=False)
percent = (df_train.isnull().sum()/df_train.isnull().count()).sort_values(ascending=False)
missing_data = pd.concat([total, percent], axis=1, keys=['Total', 'Percent'])
missing_data.head(20)

"""
We'll consider that when more than 15% of the data is missing, we should delete the corresponding variable and pretend it never existed. This means that we will not try any trick to fill the missing data in these cases. According to this, there is a set of variables (e.g. 'PoolQC', 'MiscFeature', 'Alley', etc.) that we should delete. The point is: will we miss this data? I don't think so. None of these variables seem to be very important, since most of them are not aspects in which we think about when buying a house (maybe that's the reason why data is missing?). Moreover, looking closer at the variables, we could say that variables like 'PoolQC', 'MiscFeature' and 'FireplaceQu' are strong candidates for outliers, so we'll be happy to delete them.

In what concerns the remaining cases, we can see that 'GarageX' variables have the same number of missing data. I bet missing data refers to the same set of observations (although I will not check it; it's just 5% and we should not spend 20 in5  problems). Since the most important information regarding garages is expressed by 'GarageCars' and considering that we are just talking about 5% of missing data, I'll delete the mentioned 'GarageX' variables. The same logic applies to 'BsmtX' variables.

Regarding 'MasVnrArea' and 'MasVnrType', we can consider that these variables are not essential. Furthermore, they have a strong correlation with 'YearBuilt' and 'OverallQual' which are already considered. Thus, we will not lose information if we delete 'MasVnrArea' and 'MasVnrType'.

Finally, we have one missing observation in 'Electrical'. Since it is just one observation, we'll delete this observation and keep the variable.

In summary, to handle missing data, we'll delete all the variables with missing data, except the variable 'Electrical'. In 'Electrical' we'll just delete the observation with missing data.
"""
#dealing with missing data
df_train = df_train.drop((missing_data[missing_data['Total'] > 1]).index,1)
df_train = df_train.drop(df_train.loc[df_train['Electrical'].isnull()].index)
df_train.isnull().sum().max() #just checking that there's no missing data missing...

"""
Out liars!
Outliers is also something that we should be aware of. Why? Because outliers can markedly affect our models and can be a valuable source of information, providing us insights about specific behaviours.

Outliers is a complex subject and it deserves more attention. Here, we'll just do a quick analysis through the standard deviation of 'SalePrice' and a set of scatter plots.

Univariate analysis
The primary concern here is to establish a threshold that defines an observation as an outlier. To do so, we'll standardize the data. In this context, data standardization means converting data values to have mean of 0 and a standard deviation of 1.
"""
#standardizing data
saleprice_scaled = StandardScaler().fit_transform(df_train['SalePrice'][:,np.newaxis]);
low_range = saleprice_scaled[saleprice_scaled[:,0].argsort()][:10]
high_range= saleprice_scaled[saleprice_scaled[:,0].argsort()][-10:]
print('outer range (low) of the distribution:')
print(low_range)
print('\nouter range (high) of the distribution:')
print(high_range)

"""
How 'SalePrice' looks with her new clothes:

Low range values are similar and not too far from 0.
High range values are far from 0 and the 7.something values are really out of range.
For now, we'll not consider any of these values as an outlier but we should be careful with those two 7.something values.

Bivariate analysis
We already know the following scatter plots by heart. However, when we look to things from a new perspective, there's always something to discover. As Alan Kay said, 'a change in perspective is worth 80 IQ points'.
"""
#bivariate analysis saleprice/grlivarea
var = 'GrLivArea'
data = pd.concat([df_train['SalePrice'], df_train[var]], axis=1)
data.plot.scatter(x=var, y='SalePrice', ylim=(0,800000));

"""
The two values with bigger 'GrLivArea' seem strange and they are not following the crowd. We can speculate why this is happening. Maybe they refer to agricultural area and that could explain the low price. I'm not sure about this but I'm quite confident that these two points are not representative of the typical case. Therefore, we'll define them as outliers and delete them.
The two observations in the top of the plot are those 7.something observations that we said we should be careful about. They look like two special cases, however they seem to be following the trend. For that reason, we will keep them.
"""

#deleting points
df_train.sort_values(by = 'GrLivArea', ascending = False)[:2]
df_train = df_train.drop(df_train[df_train['Id'] == 1299].index)
df_train = df_train.drop(df_train[df_train['Id'] == 524].index)
#bivariate analysis saleprice/grlivarea
var = 'TotalBsmtSF'
data = pd.concat([df_train['SalePrice'], df_train[var]], axis=1)
data.plot.scatter(x=var, y='SalePrice', ylim=(0,800000))


from django.contrib.auth.models import User
from django.db import models
from django.utils import timezone
from metrics.models import Currency
from tags.models import MetaDataMixin

# Create your models here.

class Priority(models.Model):

    label = models.CharField(max_length=32, unique=True, blank=True, null=True)
    color = models.CharField(max_length=6, unique=True)
    urgency = models.PositiveIntegerField(default=1)

    class Meta:

        ordering = ['urgency',]
        verbose_name = "priority label"
        verbose_name_plural = "priority labels"

    def __str__(self):
          return str(self.label)

class Status(models.Model):

    label = models.CharField(max_length=32, unique=True)

    class Meta:

        verbose_name = "status label"
        verbose_name_plural = "status labels"

    def __str__(self):
        return self.label

class GitBranch(models.Model):

    name = models.CharField(max_length=32, unique=True)
    repo_url = models.CharField(max_length=1024, unique=True)

    class Meta:

        verbose_name = "development branch"
        verbose_name_plural = "development branches"

    def __str__(self):
        return self.name

class Objective(MetaDataMixin, models.Model):

    completion_label = 'COMPLETE'
    commit_label = 'COMMITTED'

    summary = models.TextField(max_length=512)
    target_date = models.DateTimeField(blank=True, null=True)
    completion_date = models.DateTimeField(blank=True, null=True)
    committed = models.BooleanField(default=False)
    bounty_claimed = models.BooleanField(default=False)
    bounty = models.FloatField(blank=True, null=True)
    bounty_unit = models.ForeignKey(Currency, models.SET_NULL, blank=True, null=True)
    assigned_to = models.ForeignKey(
        User,
        models.SET_NULL,
        related_name='assigned_to',
        blank=True,
        null=True,
    )
    status = models.ForeignKey(Status, models.SET_NULL, blank=True, null=True)
    priority = models.ForeignKey(Priority, models.SET_NULL, blank=True, null=True)
    git_branch = models.ForeignKey(GitBranch, models.SET_NULL, blank=True, null=True)

    class Meta:

        ordering = ['-priority', 'target_date']
        verbose_name = "Project Objective"
        verbose_name_plural = "project objectives"

    def __str__(self):
        return self.summary

    def mark_complete(instance):
        '''
        Sets the priority and status of an Objective object to indicate
        that it is complete.

        Args:

        instance - an instance of an Objective database object
        '''
        priority, created = Priority.objects.get_or_create(
            label=None,
            color='666666',
            urgency=999999,
        )
        status, created = Status.objects.get_or_create(
            label=instance.completion_label
        )
        instance.priority = priority
        instance.status = status
        instance.completion_date = timezone.now()
        instance.save()

    def mark_committed(instance):
        '''
        Sets the commited boolean to True indicating that the Objective has
        been committed and pushed to its Git repository.

        Args:

        instance - an instance of an Objective database object
        '''
        if str(instance.status) == Objective.completion_label:
            status, created = Status.objects.get_or_create(
                label=Objective.commit_label
            )
            instance.status = status
            instance.committed = True
            instance.save()

    def mark_bounty_claimed(instance):
        '''
        Sets the bounty_claimed boolean to True indicating that the bounty for
        completing the Objective has been paid to the assigned user.

        Args:

        instance - an instance of an Objective database object
        '''
        if instance.status == Objective.completion_label and instance.committed == True:
            instance.committed = True
            instance.save()


import requests

url = 'http://choco.local.xxx.xxx.fr/service/rest/v1/assets'

def download(url,filename):
    filename = filename.replace('/','.')
    filename = filename+'.nupkg'
    r = requests.get(url, allow_redirects=True)
    open(filename, 'wb').write(r.content)

def getdatacontinuation(url,continuationToken):
    headers = {
    'accept': 'application/json',
    'X-Nexus-UI': 'true',
    }

    params = {
    'repository': 'chocolatey-hosted',
    'continuationToken': continuationToken
    }
    response = requests.get(url, params=params, headers=headers)
    return response.json()

# Each request gives a page 10 items - The next page can be found if the response has continuationToken

headers = {
    'accept': 'application/json',
    'X-Nexus-UI': 'true',
}

params = {
    'repository': 'chocolatey-hosted',
}

response = requests.get(url, params=params, headers=headers)
resp = response.json()
list = resp['items']
for item in list:
    print(item['downloadUrl'])
    print(item['path'])
    download(item['downloadUrl'],item['path'])

#End of first page

#Loop until the end of the Page

while resp['continuationToken']:
    resp = getdatacontinuation(url,resp['continuationToken'])
    list = resp['items']
    for item in list:
        print(item['downloadUrl'])
        print(item['path'])
        download(item['downloadUrl'],item['path'])