import sys
import cv2
import base64
def exportImage():
# Carregar a cascata
# Load the cascade
    face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')

# Leia a imagem de entrada
# Read the input image
    img = cv2.imread('test2.jpg')

# Converter em escala de cinza
# Convert into grayscale
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
   # gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# Detectar rostos
# Detect faces
    faces = face_cascade.detectMultiScale(gray, 1.1, 4)
   # faces = face_cascade.detectMultiScale(gray, 1.1, 4)
# Desenhe um retângulo ao redor das faces
# Draw rectangle around the faces
    for (x, y, w, h) in faces:
        cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2)

   # for (x, y, w, h) in faces:
       # cv2.rectangle(img1, (x, y), (x + w, y + h), (255, 0, 0), 2)

# Exibe a saída
# Display the output return img
#     cv2.imshow('img', img)
    # cv2.waitKey()

#if args.get("video", None) is None:
	#vs = VideoStream(src=0).start()
	#time.sleep(2.0)

#else:
#	vs = cv2.VideoCapture(args["video"])
    path = 'new_image.jpg'
    cv2.imwrite(path, img)

   # path = 'new_image.jpg'
    #cv2.imwrite(path, img1)

    return path


import bisect

class Solution(object):
    def searchMatrix(self, matrix, target):
        """
        :type matrix: List[List[int]]
        :type target: int
        :rtype: bool
        """
        if not matrix or not matrix[0]:
            return False
        l = 0
        r = len(matrix)
        mid = 0
        if target < matrix[0][0]:
            return False
        if target > matrix[-1][-1]:
            return False
        while l < r:
            mid = (l + r )// 2
            if matrix[mid][0] <= target <= matrix[mid][-1]:
                break
            if matrix[mid][0] > target:
                r = mid
            else:
                l = mid + 1
        index = bisect.bisect_left(matrix[mid], target)
        if index == len(matrix[mid]):
            return False
        if matrix[mid][index] == target:
            return True
        return False

s = Solution()
print(s.searchMatrix(matrix = [
  [1,   3,  5,  7],
  [10, 11, 16, 20],
  [23, 30, 34, 50]
], target=13))

print(s.searchMatrix(matrix=[
  [1,   3,  5,  7],
  [10, 11, 16, 20],
  [23, 30, 34, 50]
], target=8))


import os
import tarfile
import tempfile
from operator import add

import h5py
import numpy as np
import pandas as pd
import wget

def get_name(index, hdf5_data):
    name = hdf5_data['/digitStruct/name']
    return ''.join([chr(v[0]) for v in hdf5_data[name[index][0]].value])

def get_bbox(index, hdf5_data):
    attrs = {}
    item = hdf5_data['digitStruct']['bbox'][index].item()
    for key in ['label', 'left', 'top', 'width', 'height']:
        attr = hdf5_data[item][key]
        values = [int(hdf5_data[attr.value[i].item()].value[0][0]) for i in range(len(attr))] if len(attr) > 1 else [int(attr.value[0][0])]
        attrs[key] = values
    return attrs

def img_boundingbox_data_constructor(data_folder, mode, csv_path):
    f = h5py.File(os.path.join(data_folder, "digitStruct.mat"),'r')
    row_list = []
    num_example = f['/digitStruct/bbox'].shape[0]
    logging_interval = num_example // 10
    print("found %d number of examples for %s" % (num_example, mode))
    for j in range(num_example):
        if j % logging_interval == 0:
            print("retrieving bounding box for %s: %f%%" % (mode, j/num_example*100))
        img_name = get_name(j, f)
        bbox = get_bbox(j, f)
        row_dict = {'image': os.path.join(mode, img_name),
                    'label': bbox["label"],
                    'x1': bbox["left"],
                    'y1': bbox["top"],
                    'x2': list(map(add, bbox["left"], bbox["width"])),
                    'y2': list(map(add, bbox["top"], bbox["height"]))}
        row_list.append(row_dict)
    bbox_df = pd.DataFrame(row_list, columns=['image','label','x1','y1','x2','y2'])
    bbox_df.to_csv(csv_path, index=False)
    return bbox_df

def load_data(path=None):
    if path is None:
        path = os.path.join(tempfile.gettempdir(), "FE_SVHN")
    if not os.path.exists(path):
        os.mkdir(path)
    train_csv = os.path.join(path, "train_data.csv")
    test_csv = os.path.join(path, "test_data.csv")
    if not (os.path.exists(os.path.join(path, "train.tar.gz")) and os.path.exists(os.path.join(path, "test.tar.gz"))):
        print("downloading data to %s" % path)
        wget.download('http://ufldl.stanford.edu/housenumbers/train.tar.gz', path)
        wget.download("http://ufldl.stanford.edu/housenumbers/test.tar.gz", path)
    if not (os.path.exists(os.path.join(path, "train")) and os.path.exists(os.path.join(path, "test"))):
        print(" ")
        print("extracting data...")
        test_file = tarfile.open(os.path.join(path, "test.tar.gz"))
        train_file = tarfile.open(os.path.join(path, "train.tar.gz"))
        test_file.extractall(path=path)
        train_file.extractall(path=path)
    if not (os.path.exists(train_csv) and os.path.exists(test_csv)):
        print("constructing bounding box data...")
        train_folder = os.path.join(path, "train")
        test_folder = os.path.join(path, "test")
        img_boundingbox_data_constructor(train_folder, "train", train_csv)
        img_boundingbox_data_constructor(test_folder, "test", test_csv)
    return train_csv, test_csv, path


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

import matplotlib.pyplot as plt
import numpy as np
from scipy.linalg import eigvals
import pickle
import parameters

from pyvib.nlforce import NL_force, NL_polynomial
from pyvib.forcing import sine, multisine, sinesweep, toMDOF
from pyvib.newmark import newmark_beta_nl as newmark_beta

plot = True
savedata = True
savefig = False

dof = 0

par = parameters.par

M = par['M']
C = par['C']
K = par['K']

ftype = 'sweep'
fdof = 0
vrms = 2
f1 = 0.001/2/np.pi
f2 = 5/np.pi
fs = 20*f2
nper = 2
nsper = 10000
vsweep = 0.01
inctype = 'lin'

inl = par['inl']
enl = par['enl']
knl = par['knl']
nl_pol = NL_polynomial(inl, enl, knl)
nl = NL_force(nl_pol)

x0, y0 = 0, 0
ndof = M.shape[0]
# get external forcing
if ftype == 'multisine':
    u, lines, freqs, t = multisine(f1=f1, f2=f2, N=nsper, P=nper)

elif ftype == 'sweep':
    u, t, finst = sinesweep(vrms, fs, f1, f2, vsweep, nper, inctype)
    # we dont use the first value, as it is not repeated when the force is
    # generated. This is taken care of in multisine.
    nsper = (len(u)-1) // nper
elif ftype == 'sine':
    u, t = sine(vrms, f=f1, fs=fs, ns=nsper)
else:
    raise ValueError('Wrong type of forcing', ftype)
fext = toMDOF(u, ndof, fdof)

dt = t[1] - t[0]
x, xd, xdd = newmark_beta(M, C, K, x0, y0, dt, fext, nl, sensitivity=False)

plt.figure()
plt.clf()
plt.plot(t, x[:,0], '-k', label=r'$x_1$')
plt.plot(t, x[:,1], '-r', label=r'$x_2$')
plt.xlabel('Time (t)')
plt.ylabel('Displacement (m)')
plt.title('Force type: {}, periods:{:d}'.format(ftype, nper))
plt.legend()

plt.show()


author = 'Anton Bobrov<bobrov@vl.ru>'
name = 'Goto dir'
desc = "Opens file's directory"

def init(manager):
    manager.add_shortcut('goto-dir', '<ctrl><alt>l', 'File', "Opens file's directory", goto_dir)

def goto_dir(editor):
    import subprocess
    import os.path

    subprocess.Popen(['/usr/bin/env', 'xdg-open', os.path.dirname(editor.uri)]).poll()
    editor.message('File manager started', 1000)


import pygame

class Reload_Animation():
    def __init__(self, screen, settings, gun):
        self.screen = screen
        self.settings = settings
        self.gun = gun
        self.width = 0

    def blit(self):
        pygame.gfxdraw.box(self.screen, (self.gun.rect.left, self.gun.rect.bottom + 2, self.width, 5), self.settings.reload_animation_color)
        self.width += .075

        if self.width >= self.gun.rect.width:
            self.gun.reload_animation = False
            self.width = 0


#!/usr/bin/env python
# -*- encoding: utf-8 -*-
"""
@File    :   QuickSort.py
@Contact :   70904372cecilia@gmail.com
@License :   (C)Copyright 2019-2020

@Modify Time      @Author    @Version    @Desciption
------------      -------    --------    -----------
2020/9/20 18:18   cecilia      1.0         None
"""

class QuickSort(object):

    def sort(self, array, l, r):
        if l < r:
            pivot = array[l]
            i, j = l, r
            while i < j:
                while j > i and array[j] > pivot:
                    j = j - 1
                if i < j:
                    array[i] = array[j]
                while i < j and array[i] <= pivot:
                    i = i + 1
                if i < j:
                    array[j] = array[i]
            array[i] = pivot
            self.sort(array, l, i)
            self.sort(array, i + 1, r)
        print(array)

    def quick_sort(self, array):
        self.sort(array, 0, len(array) - 1)

if __name__ == '__main__':
    array = [1, 4, 6, 9, 10, 2, 3, 5, 8, 7]
    qs = QuickSort()
    qs.quick_sort(array)


import os

import pandas as pd
import numpy as np

from PyQt5.QtCore import (QDir,
                          QSettings,
                          QCoreApplication)
from PyQt5.QtWidgets import (QApplication,
                             qApp,
                             QFileDialog,
                             QMainWindow,
                             QAction,
                             QStackedWidget)

from .widgets import DataWidget, VisualizationsWidget

class EpyvisApp(QApplication):

    def __init__(self, args):
        super().__init__([])

        if args.path is not None:
            if args.header_rows > 1:
                header = [k for k in range(args.header_rows)]
            else:
                header = 0
            self.loaded_path = args.path
            self.data = pd.read_csv(
                args.path,
                header=header)
            self.data_name = os.path.basename(args.path)

            # Clean up read_csv default Unnamed levels
            if isinstance(self.data.columns, pd.MultiIndex):
                for k, col in enumerate(self.data.columns.levels):
                    new_col = np.where(col.str.startswith("Unnamed"), "", col)
                    self.data.columns.set_levels(new_col,
                                                 level=k,
                                                 inplace=True)
            else:
                self.data.columns = ["" if str(col).startswith("Unnamed")
                                     else col for col in self.data.columns]
        else:
            self.loaded_path = None
            self.data = pd.DataFrame()
            self.data_name = None

        # if isinstance(self.data.index, pd.MultiIndex):
        #     for k, row in enumerate(self.data.index.levels):
        #         try:
        #             new_row = np.where(row.str.startswith("Unnamed"), "", row)
        #             self.data.index.set_levels(new_row, level=k, inplace=True)
        #         except AttributeError:
        #             pass

class EpyvisMainWindow(QMainWindow):

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

        self.data_widget = None
        self.visualizations_widget = None

        self.init_ui(theme)

    def init_ui(self, theme):
        self.setWindowTitle("Epyvis")

        style_sheet = ("background-color: {background};"
                       "color: {color};")
        self.setStyleSheet(style_sheet.format(**theme))

        self.create_actions()
        # self.create_menus()
        # self.create_toolbars()

        # Initialize the status bar
        self.statusBar().showMessage("")

        # Set the central widget
        self.init_widgets(theme)

        self.restore_settings()

        self.show()

    def init_widgets(self, theme):
        central_widget = QStackedWidget()

        self.data_widget = DataWidget(theme)
        central_widget.addWidget(self.data_widget)

        self.visualizations_widget = VisualizationsWidget()
        central_widget.addWidget(self.visualizations_widget)

        self.setCentralWidget(central_widget)

    def create_actions(self):
        # Ctrl+Q to exit
        self.addAction(QAction(parent=self,
                               shortcut="Ctrl+Q",
                               triggered=self.quit))
        # Ctrl+O to open
        self.addAction(QAction(parent=self,
                               shortcut="Ctrl+O",
                               triggered=self.open))

        # Tab toggles between data view and visualizations
        self.addAction(QAction(parent=self,
                               shortcut="Tab",
                               triggered=self.toggle_data))

    def create_menus(self):
        menu_bar = self.menuBar()
        file_menu = menu_bar.addMenu("&File")
        # edit_menu = menu_bar.addMenu("&Edit")
        # view_menu = menu_bar.addMenu("&View")

        # file_menu.addAction(self.open_action)
        file_menu.addSeparator()
        # file_menu.addAction(self.exit_action)

    def create_toolbars(self):
        tool_bar = self.addToolBar("Exit")
        tool_bar.addAction(self.exit_action)
        tool_bar.setObjectName("fluff")

    def open(self):
        file_name, _ = QFileDialog.getOpenFileName(self,
                                                   "OpenFile",
                                                   QDir.currentPath())
        self.open_file(file_name)

    def open_file(self, file_name):
        _, extension = os.path.splitext(file_name)

        main_app = QCoreApplication.instance()

        if extension == ".csv":
            main_app.data = pd.read_csv(file_name)
            main_app.data_name = os.path.basename(file_name)
        else:
            self.statusBar().setMessage("File type not supported")

        self.data_widget.refresh()

    def closeEvent(self, e):
        self.save_settings()
        super().closeEvent(e)

    def quit(self):
        self.save_settings()
        qApp.exit()

    def toggle_data(self):
        if self.centralWidget() is not None:
            central_widget = self.centralWidget()
            k = central_widget.currentIndex()
            central_widget.setCurrentIndex(1 - k)

    def save_settings(self):
        settings = QSettings("Epyvis", "Epyvis")
        settings.setValue("geometry", self.saveGeometry())
        settings.setValue("windowState", self.saveState())

    def restore_settings(self):
        settings = QSettings("Epyvis", "Epyvis")
        if settings.value("geometry") is not None:
            self.restoreGeometry(settings.value("geometry"))
        if settings.value("windowState") is not None:
            self.restoreState(settings.value("windowState"))


import sublime
import sublime_plugin
import os
import re
import subprocess

def sget(key, default=None):
    s = sublime.load_settings("SendText+.sublime-settings")
    return s.get(key, default)

def get_program(view):
    plat = sublime.platform()
    prog = sget("prog")
    if prog:
        return prog
    defaults = sget("defaults")
    pt = view.sel()[0].begin() if len(view.sel()) > 0 else 0

    for d in defaults:
        match_platform = plat == d.get("platform", plat)
        scopes = d.get("scopes", None)
        match_scopes = not scopes or any([view.score_selector(pt, s) > 0 for s in scopes])
        if match_platform and match_scopes and "prog" in d:
            return d.get("prog")
    return None

class TextSender:

    def __init__(self, view):
        self.view = view
        plat = sublime.platform()
        prog = get_program(view)
        function_str = "_dispatch_" + prog.lower().replace("-", "_")
        if getattr(self, function_str + "_" + plat, None):
            function_str = function_str + "_" + plat
        self.send_text = eval("self." + function_str)

    @classmethod
    def init(cls, view):
        return cls(view)

    @staticmethod
    def clean_cmd(cmd):
        cmd = cmd.expandtabs(4)
        cmd = cmd.rstrip('\n')
        if len(re.findall("\n", cmd)) == 0:
            cmd = cmd.lstrip()
        return cmd

    @staticmethod
    def escape_dquote(cmd):
        cmd = cmd.replace('\\', '\\\\')
        cmd = cmd.replace('"', '\\"')
        return cmd

    def _dispatch_terminal(self, cmd):
        cmd = self.clean_cmd(cmd)
        cmd = self.escape_dquote(cmd)
        args = ['osascript']
        args.extend(['-e',
                     'tell application "Terminal" to do script "' + cmd + '" in front window'])
        subprocess.Popen(args)

    @staticmethod
    def iterm_version():
        args = ['osascript', '-e', 'tell application "iTerm" to get version']
        ver = subprocess.check_output(args).decode().strip()
        return tuple((int(i) for i in re.split(r"\.", ver)[0:2]))

    def _dispatch_iterm(self, cmd):
        cmd = self.clean_cmd(cmd)
        if self.iterm_version() >= (2, 9):
            n = 1000
            chunks = [cmd[i:i+n] for i in range(0, len(cmd), n)]
            for chunk in chunks:
                subprocess.call([
                    'osascript', '-e',
                    'tell application "iTerm" to tell the current window ' +
                    'to tell current session to write text "' +
                    self.escape_dquote(chunk) + '" without newline'
                ])
            subprocess.call([
                'osascript', '-e',
                'tell application "iTerm" to tell the current window ' +
                'to tell current session to write text ""'
            ])
        else:
            subprocess.call([
                'osascript', '-e',
                'tell application "iTerm" to tell the current terminal ' +
                'to tell current session to write text "' +
                self.escape_dquote(cmd) + '"'
            ])

    def _dispatch_r_osx(self, cmd):
        cmd = self.clean_cmd(cmd)
        cmd = self.escape_dquote(cmd)
        args = ['osascript']
        args.extend(['-e', 'tell application "R" to cmd "' + cmd + '"'])
        subprocess.Popen(args)

    def _dispatch_rstudio_osx(self, cmd):
        cmd = self.clean_cmd(cmd)
        script = """
        on run argv
            tell application "RStudio"
                cmd item 1 of argv
            end tell
        end run
        """
        subprocess.call(['osascript', '-e', script, cmd])

    def _dispatch_chrome_rstudio(self, cmd):
        cmd = self.clean_cmd(cmd)
        cmd = self.escape_dquote(cmd)
        cmd = cmd.replace("\n", r"\n")
        # use window.desktopHooks.evaluateRCmd once
        # https://github.com/rstudio/rstudio/commit/3c11a669e7eaae0af40b935fb22cf1bf0e7d6b73
        # is released.
        script = """
        on run argv
            tell application "Google Chrome"
                set URL of front window's active tab to "javascript:{" & "
                    var input = document.getElementById('rstudio_console_input');
                    var textarea = input.getElementsByTagName('textarea')[0];
                    textarea.value += \\"" & item 1 of argv & "\\";
                    var e = document.createEvent('KeyboardEvent');
                    e.initKeyboardEvent('input');
                    textarea.dispatchEvent(e);
                    var e = document.createEvent('KeyboardEvent');
                    e.initKeyboardEvent('keydown');
                    Object.defineProperty(e, 'keyCode', {'value' : 13});
                    input.dispatchEvent(e);
                " & "}"
            end tell
        end run
        """
        subprocess.call(['osascript', '-e', script, cmd])

    def _dispatch_safari_rstudio(self, cmd):
        cmd = self.clean_cmd(cmd)
        cmd = self.escape_dquote(cmd)
        cmd = cmd.replace("\n", r"\n")
        script = """
        on run argv
            tell application "Safari"
                tell front window's current tab to do JavaScript "
                    var input = document.getElementById('rstudio_console_input');
                    var textarea = input.getElementsByTagName('textarea')[0];
                    textarea.value += \\"" & item 1 of argv & "\\";
                    var e = document.createEvent('KeyboardEvent');
                    e.initKeyboardEvent('input');
                    textarea.dispatchEvent(e);
                    var e = document.createEvent('KeyboardEvent');
                    e.initKeyboardEvent('keydown');
                    Object.defineProperty(e, 'keyCode', {'value' : 13});
                    input.dispatchEvent(e);
                "
            end tell
        end run
        """
        subprocess.call(['osascript', '-e', script, cmd])

    def _dispatch_chrome_jupyter(self, cmd):
        # remove possible ipython code block
        cmd = re.sub(r"%cpaste\n(.*)\n--", r"\1", cmd, flags=re.S)
        cmd = self.clean_cmd(cmd)
        cmd = self.escape_dquote(cmd)
        cmd = cmd.replace("\n", r"\n")
        script = """
        on run argv
            tell application "Google Chrome"
                set URL of front window's active tab to "javascript:{" & "
                    var mycell = IPython.notebook.get_selected_cell();
                    mycell.set_text(\\"" & item 1 of argv & "\\");
                    mycell.execute();
                    var nextcell = IPython.notebook.insert_cell_below();
                    IPython.notebook.select_next();
                    IPython.notebook.scroll_to_cell(IPython.notebook.find_cell_index(nextcell));
                " & "}"
            end tell
        end run
        """
        subprocess.call(['osascript', '-e', script, cmd])

    def _dispatch_safari_jupyter(self, cmd):
        # remove possible ipython code block
        cmd = re.sub(r"%cpaste\n(.*)\n--", r"\1", cmd, flags=re.S)
        cmd = self.clean_cmd(cmd)
        cmd = self.escape_dquote(cmd)
        cmd = cmd.replace("\n", r"\n")
        script = """
        on run argv
            tell application "Safari"
                tell front window's current tab to do JavaScript "
                    var mycell = IPython.notebook.get_selected_cell();
                    mycell.set_text(\\"" & item 1 of argv & "\\");
                    mycell.execute();
                    var nextcell = IPython.notebook.insert_cell_below();
                    IPython.notebook.select_next();
                    IPython.notebook.scroll_to_cell(IPython.notebook.find_cell_index(nextcell));
                "
            end tell
        end run
        """
        subprocess.call(['osascript', '-e', script, cmd])

    def _dispatch_tmux(self, cmd):
        tmux = sget("tmux", "tmux")
        cmd = self.clean_cmd(cmd) + "\n"
        n = 200
        chunks = [cmd[i:i+n] for i in range(0, len(cmd), n)]
        for chunk in chunks:
            subprocess.call([tmux, 'set-buffer', chunk])
            subprocess.call([tmux, 'paste-buffer', '-d'])

    def _dispatch_screen(self, cmd):
        screen = sget("screen", "screen")
        plat = sublime.platform()
        cmd = self.clean_cmd(cmd) + "\n"
        n = 200
        chunks = [cmd[i:i+n] for i in range(0, len(cmd), n)]
        for chunk in chunks:
            if plat == "linux":
                chunk = chunk.replace("\\", r"\\")
                chunk = chunk.replace("$", r"\$")
            subprocess.call([screen, '-X', 'stuff', chunk])

    @staticmethod
    def execute_ahk_script(script, cmd, args=[]):
        ahk_path = os.path.join(sublime.packages_path(),
                                'User', 'SendText+', 'bin', 'AutoHotkeyU32')
        ahk_script_path = os.path.join(sublime.packages_path(),
                                       'User', 'SendText+', 'bin', script)
        # manually add "\n" to keep the indentation of first line of block code,
        # "\n" is later removed in AutoHotkey script
        cmd = "\n" + cmd
        subprocess.Popen([ahk_path, ahk_script_path, cmd] + args)

    def _dispatch_cygwin(self, cmd):
        cmd = self.clean_cmd(cmd)
        self.execute_ahk_script("Cygwin.ahk", cmd)

    def _dispatch_cmder(self, cmd):
        cmd = self.clean_cmd(cmd)
        self.execute_ahk_script("Cmder.ahk", cmd)

    def _dispatch_r32_windows(self, cmd):
        cmd = self.clean_cmd(cmd)
        self.execute_ahk_script("Rgui.ahk", cmd, [sget("R32", "0")])

    def _dispatch_r64_windows(self, cmd):
        cmd = self.clean_cmd(cmd)
        self.execute_ahk_script("Rgui.ahk", cmd, [sget("R64", "1")])

    def _dispatch_rstudio_windows(self, cmd):
        cmd = self.clean_cmd(cmd)
        self.execute_ahk_script("RStudio.ahk", cmd)

    def _dispatch_sublimerepl(self, cmd):
        cmd = self.clean_cmd(cmd)
        window = sublime.active_window()
        view = window.active_view()
        external_id = view.scope_name(0).split(" ")[0].split(".", 1)[1]
        window.run_command(
            "repl_send", {"external_id": external_id, "text": cmd})

class TextGetter:

    def __init__(self, view):
        self.view = view

    @classmethod
    def init(cls, view):
        pt = view.sel()[0].begin() if len(view.sel()) > 0 else 0
        if view.score_selector(pt, "source.r"):
            getter = RTextGetter(view)
        elif view.score_selector(pt, "source.python"):
            getter = PythonTextGetter(view)
        elif view.score_selector(pt, "source.julia"):
            getter = JuliaTextGetter(view)
        else:
            getter = cls(view)

        return getter

    def expand_line(self, s):
        return self.view.line(s)

    def advance(self, s):
        view = self.view
        view.sel().subtract(s)
        pt = view.text_point(view.rowcol(s.end())[0]+1, 0)
        if sget("auto_advance_non_empty", False):
            nextpt = view.find(r"\S", pt)
            if nextpt.begin() != -1:
                pt = view.text_point(view.rowcol(nextpt.begin())[0], 0)
        view.sel().add(sublime.Region(pt, pt))

    def get_text(self):
        view = self.view
        cmd = ''
        moved = False
        for s in [s for s in view.sel()]:
            if s.empty():
                s = self.expand_line(s)
                if sget("auto_advance", True):
                    self.advance(s)
                    moved = True

            cmd += view.substr(s) + '\n'

        if moved:
            view.show(view.sel())

        return cmd

class RTextGetter(TextGetter):

    def expand_line(self, s):
        view = self.view
        # expand selection to {...}
        s = view.line(s)
        thiscmd = view.substr(s)
        if re.match(r".*\{\s*$", thiscmd):
            es = view.find(
                r"""^(?:.*(\{(?:(["\'])(?:[^\\]|\\.)*?\2|#.*$|[^\{\}]|(?1))*\})[^\{\}\n]*)+""",
                view.line(s).begin()
            )
            if s.begin() == es.begin():
                s = es
        return s

class PythonTextGetter(TextGetter):

    def expand_line(self, s):
        view = self.view
        s = view.line(s)
        thiscmd = view.substr(s)
        row = view.rowcol(s.begin())[0]
        prevline = view.line(s.begin())
        lastrow = view.rowcol(view.size())[0]
        if re.match(r"^(#\s%%|#%%|# In\[)", thiscmd):
            while row < lastrow:
                row = row + 1
                line = view.line(view.text_point(row, 0))
                m = re.match(r"^(#\s%%|#%%|# In\[)", view.substr(line))
                if m:
                    s = sublime.Region(s.begin(), prevline.end())
                    break
                else:
                    prevline = line

        elif re.match(r"^[ \t]*\S", thiscmd):
            indentation = re.match(r"^([ \t]*)", thiscmd).group(1)
            while row < lastrow:
                row = row + 1
                line = view.line(view.text_point(row, 0))
                m = re.match(r"^([ \t]*)([^\n\s]+)", view.substr(line))
                if m and len(m.group(1)) <= len(indentation) and \
                        (len(m.group(1)) < len(indentation) or
                            not re.match(r"else|elif|except|finally", m.group(2))):
                    s = sublime.Region(s.begin(), prevline.end())
                    break
                elif re.match(r"^[ \t]*\S", view.substr(line)):
                    prevline = line

        if row == lastrow:
            s = sublime.Region(s.begin(), prevline.end())
        return s

    def get_text(self):
        cmd = super(PythonTextGetter, self).get_text()
        cmd = cmd.rstrip("\n")
        if len(re.findall("\n", cmd)) > 0:
            cmd = "%cpaste -q\n" + cmd + "\n--"
        return cmd

class JuliaTextGetter(TextGetter):

    def expand_line(self, s):
        view = self.view
        s = view.line(s)
        thiscmd = view.substr(s)
        if (re.match(r"^\s*(?:function|if|for|while)", thiscmd) and
                not re.match(r".*end\s*$", thiscmd)) or \
                (re.match(r".*begin\s*$", thiscmd)):
            indentation = re.match("^(\s*)", thiscmd).group(1)
            end = view.find("^"+indentation+"end", s.begin())
            s = sublime.Region(s.begin(), view.line(end.end()).end())

        return s

class SendTextPlusCommand(sublime_plugin.TextCommand):

    def run(self, edit):
        view = self.view
        getter = TextGetter.init(view)
        cmd = getter.get_text()
        sender = TextSender.init(view)
        sender.send_text(cmd)

class SendTextPlusChangeDirCommand(sublime_plugin.TextCommand):

    def run(self, edit):
        view = self.view
        fname = view.file_name()
        if not fname:
            sublime.error_message("Save the file!")
            return

        dirname = os.path.dirname(fname)

        sender = TextSender.init(view)

        pt = view.sel()[0].begin() if len(view.sel()) > 0 else 0
        if view.score_selector(pt, "source.r"):
            cmd = "setwd(\"" + sender.escape_dquote(dirname) + "\")"
        elif view.score_selector(pt, "source.python"):
            cmd = "cd \"" + sender.escape_dquote(dirname) + "\""
        elif view.score_selector(pt, "source.julia"):
            cmd = "cd(\"" + sender.escape_dquote(dirname) + "\")"

        sender.send_text(cmd+"\n")

class SendTextPlusSourceCodeCommand(sublime_plugin.TextCommand):

    def run(self, edit):
        view = self.view
        fname = view.file_name()
        if not fname:
            sublime.error_message("Save the file!")
            return

        sender = TextSender.init(view)

        pt = view.sel()[0].begin() if len(view.sel()) > 0 else 0
        if view.score_selector(pt, "source.r"):
            cmd = "source(\"" + sender.escape_dquote(fname) + "\")"
        elif view.score_selector(pt, "source.python"):
            cmd = "%run \"" + sender.escape_dquote(fname) + "\""
        elif view.score_selector(pt, "source.julia"):
            cmd = "include(\"" + sender.escape_dquote(fname) + "\")"

        sender.send_text(cmd+"\n")

class SendTextPlusBuildCommand(sublime_plugin.WindowCommand):

    def run(self):
        self.window.active_view().run_command("send_text_plus_source_code")

class SendTextPlusChooseProgramCommand(sublime_plugin.WindowCommand):

    def show_quick_panel(self, options, done):
        sublime.set_timeout(lambda: self.window.show_quick_panel(options, done), 10)

    def run(self):
        plat = sublime.platform()
        if plat == 'osx':
            self.app_list = ["[Defaults]", "Terminal", "iTerm",
                             "R", "RStudio", "Chrome-RStudio", "Chrome-Jupyter",
                             "Safari-RStudio", "Safari-Jupyter",
                             "tmux", "screen", "SublimeREPL"]
        elif plat == "windows":
            self.app_list = ["[Defaults]", "Cmder", "Cygwin",
                             "R32", "R64", "RStudio", "SublimeREPL"]
        elif plat == "linux":
            self.app_list = ["[Defaults]", "tmux", "screen", "SublimeREPL"]
        else:
            sublime.error_message("Platform not supported!")

        self.show_quick_panel(self.app_list, self.on_done)

    def on_done(self, action):
        if action == -1:
            return
        settings = sublime.load_settings('SendText+.sublime-settings')
        settings.set("prog", self.app_list[action] if action > 0 else None)
        sublime.save_settings('SendText+.sublime-settings')


import cv2 as cv

def element_largest(image):
    image_copy = image.copy()
    gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
    edges = cv.Canny(gray, 110, 210)
    contours, hierarchy = cv.findContours(edges, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)
    cv.drawContours(image_copy, contours, -1, (0, 255, 0), 3)
    contours_list = []

    for i in contours:
        M = cv.contourArea(i)
        my_object = {'contour': i, 'area': M}
        contours_list.append(my_object)

    max = contours_list[0]['area']
    max_obj = contours_list[0]
    for obj in contours_list:
        if obj['area'] > max:
            max = obj['area']
            max_obj = obj

    cv.drawContours(image_copy, max_obj['contour'], -1, (0, 0, 255), 3)
    max_obj['envelop'] = image_copy

    return max_obj


def solution(m, n, puddles):
    Map = [[0] * (m+1) for _ in range(n+1)]
    Map[1][1] = 1
    for i in range(1, n+1):
        for j in range(1, m+1):
            if i == j == 1 or [j, i] in puddles:
                continue
            Map[i][j] = Map[i-1][j] + Map[i][j-1]
    print(Map)
    return Map[-1][-1] % 1000000007


"""
Reference free AF-based demultiplexing on pooled scRNA-seq
Jon Xu (jun.xu@uq.edu.au)
Lachlan Coin
Aug 2018
"""

import sys
import vcf  # https://pyvcf.readthedocs.io/en/latest/INTRO.html
import numpy as np
import pandas as pd
from scipy.sparse import csr_matrix
import datetime
import csv

class models:
    def __init__(self, base_calls_mtx, seed_vcf, num=2):
        """
        A complete model class containing SNVs, matrices counts, barcodes, model allele fraction with assigned cells

        Parameters:
             base_calls_mtx(list of Dataframes): SNV-barcode matrix containing lists of base calls
             all_POS(list): list of SNVs positions
             barcodes(list): list of cell barcodes
             num(int): number of total samples
             P_s_c(DataFrame): barcode/sample matrix containing the probability of seeing sample s with observation of barcode c
             lP_c_s(DataFrame): barcode/sample matrix containing the log likelihood of seeing barcode c under sample s, whose sum should increase by each iteration
             assigned(list): final lists of cell/barcode assigned to each cluster/model
             model_af(list): list of num model allele frequencies based on P(A)

        """

        err = 0.01  # error rate for model seeds
        dbl = 0.02  # doublet ratio assumption
        self.ref_bc_mtx = base_calls_mtx[0]
        self.alt_bc_mtx = base_calls_mtx[1]
        self.all_POS = base_calls_mtx[2].tolist()
        self.barcodes = base_calls_mtx[3].tolist()
        self.num = num + 1  # including an additional background state for doublets
        self.P_s_c = pd.DataFrame(0, index = self.barcodes, columns = range(self.num))
        self.lP_c_s = pd.DataFrame(0, index = self.barcodes, columns = range(self.num))
        self.assigned = []
        self.P_s = [dbl]  # assuming P_s[0], i.e doublet has 2% probability
        for _ in range(self.num):
            self.assigned.append([])
        self.model_af = pd.DataFrame(0, index=self.all_POS, columns=range(self.num))
        # set background alt count proportion as fixed allele fraction for each SNVs in the model, pseudo count is added for 0 counts on multi-base SNPs
        self.model_af.loc[:, 0] = (self.alt_bc_mtx.sum(axis=1) + 1) / (self.ref_bc_mtx.sum(axis=1) + self.alt_bc_mtx.sum(axis=1) + 2)
        for n in range(1, self.num):
            self.P_s.append((1 - dbl) / (self.num - 1))  # even initial distribution of P(s) across all other singlet samples
            # use total ref count and alt count on each position of csr_matrix to generate probability simulation using beta distribution
            N_A = self.alt_bc_mtx.sum(axis=1) + 1
            N_R = self.ref_bc_mtx.sum(axis=1) + 1
            N_T = N_A + N_R
            self.model_af.loc[:, n] = [item[0] for item in np.random.beta(100*N_A/N_T, 100*N_R/N_T)]

        last = ''
        in_vcf = vcf.Reader(open(seed_vcf, 'r'))
        for record in in_vcf:
            if (record.ID != last) & (record.ID in self.all_POS):   # only need those SNVs captured in our matrices
                for n in range(1, self.num):
                    self.model_af.loc[record.ID, n] = 0.5 * record.samples[n-1]['GP'][1] + record.samples[n-1]['GP'][2]
            last = record.ID

        self.model_af[self.model_af == 0] = err
        self.model_af[self.model_af == 1] = 1 - err

    def calculate_model_af(self):
        """
        Update the model allele fraction by distributing the alt and total counts of each barcode on a certain snv to the model based on P(s|c)

        """

        pseudo_count = 0.01  # pseudo count for zero entries
        N_ref = self.ref_bc_mtx.sum(axis=1)
        N_alt = self.alt_bc_mtx.sum(axis=1)
        N_ref[N_ref == 0] = pseudo_count
        N_alt[N_alt == 0] = pseudo_count
        k_ref = N_ref / (N_ref + N_alt)
        k_alt = N_alt / (N_ref + N_alt)
        self.model_af = pd.DataFrame((self.alt_bc_mtx.dot(self.P_s_c) + k_alt) / ((self.alt_bc_mtx + self.ref_bc_mtx).dot(self.P_s_c) + k_ref + k_alt),
                                        index = self.all_POS, columns = range(self.num))
        self.model_af.loc[:, 0] = self.model_af.loc[:, 1:(self.num-1)].mean(axis=1)   # reset the background AF

    def calculate_cell_likelihood(self):
        """
        Calculate cell|sample likelihood P(c|s) and derive sample|cell probability P(s|c)
        P(c|s_v) = P(N(A),N(R)|s) = P(g_A|s)^N(A) * (1-P(g_A|s))^N(R)
        log(P(c|s)) = sum_v{(N(A)_c,v*log(P(g_A|s)) + N(R)_c,v*log(1-P(g_A|s)))}
        P(s_n|c) = P(c|s_n) / [P(c|s_1) + P(c|s_2) + ... + P(c|s_n)]

        """

        for n in range(self.num):
            matcalc = self.alt_bc_mtx.T.multiply(self.model_af.loc[:, n].apply(np.log2)).T \
                    + self.ref_bc_mtx.T.multiply((1 - self.model_af.loc[:, n]).apply(np.log2)).T
            self.lP_c_s.loc[:, n] = matcalc.sum(axis=0).tolist()[0]  # log likelihood to avoid python computation limit of 1e-323/1e+308

        # log(P(s1|c) = log{1/[1+P(c|s2)/P(c|s1)]} = -log[1+P(c|s2)/P(c|s1)] = -log[1+2^(logP(c|s2)-logP(c|s1))]
        for i in range(self.num):
            denom = 0
            for j in range(self.num):
                denom += 2 ** (self.lP_c_s.loc[:, j] + np.log2(self.P_s[j]) - self.lP_c_s.loc[:, i] - np.log2(self.P_s[i]))
            self.P_s_c.loc[:, i] = 1 / denom

    def assign_cells(self):
        """
	    Final assignment of cells according to P(s|c) >= 0.9

	    """

        for n in range(self.num):
            self.assigned[n] = sorted(self.P_s_c.loc[self.P_s_c[n] >= 0.9].index.values.tolist())

def run_model(base_calls_mtx, seed_vcf, num_models):

    model = models(base_calls_mtx, seed_vcf, num_models)

    iterations = 0
    sum_log_likelihood = [1,2]

    # commencing E-M
    while sum_log_likelihood[-2] != sum_log_likelihood[-1]:
        iterations += 1
        progress = 'Iteration ' + str(iterations) + '   ' + str(datetime.datetime.now()) + '\n'
        with open('wip.log', 'a') as myfile: myfile.write(progress)
        model.calculate_cell_likelihood()  # E-step, calculate the expected cell origin likelihood with a function of model.model_af (theta)
        model.calculate_model_af()  # M-step, to optimise unknown model parameter model.model_af (theta)
        model.model_af.to_csv('model_af_' + str(iterations))
        # approximation due to python calculation limit
        sum_log_likelihood.append(model.lP_c_s.max(axis=1).sum())  # L = Prod_c[Sum_s(P(c|s))], thus LL = Sum_c{log[Sum_s(P(c|s))]}
        # sum_log_likelihood.append(((2**model.lP_c_s).sum(axis=1)+1e-323).apply(np.log2).sum())

    model.assign_cells()

    # generate outputs
    for n in range(num_models+1):
        with open('barcodes_{}.csv'.format(n), 'w') as myfile:
            for item in model.assigned[n]:
                myfile.write(str(item) + '\n')
    model.P_s_c.to_csv('P_s_c.csv')
    model.model_af.to_csv('model_af.csv')
    print(sum_log_likelihood)
    progress = 'scSplit finished at: ' + str(datetime.datetime.now()) + '\n'
    with open('wip.log', 'a') as myfile: myfile.write(progress)

def read_base_calls_matrix(ref_csv, alt_csv):

    """ Read in existing matrix from the csv files """

    ref = pd.read_csv(ref_csv, header=0, index_col=0)  # read ref matrix with header line and column
    alt = pd.read_csv(alt_csv, header=0, index_col=0)  # read alt matrix with header line and column
    ref_s = csr_matrix(ref.values)
    alt_s = csr_matrix(alt.values)
    base_calls_mtx = [ref_s, alt_s, ref.index, ref.columns]
    progress = 'AF matrices uploaded: ' + str(datetime.datetime.now()) + '\n'
    with open('wip.log', 'a') as myfile: myfile.write(progress)
    return base_calls_mtx

def main():

    num_models = 8          # number of models in each run

    # input and output files
    ref_csv = 'ref_filtered.csv'  # reference matrix
    alt_csv = 'alt_filtered.csv'  # alternative matrix
    seed_vcf = 'input8.vcf'  # vcf serving as model seed

    progress = 'Starting data collection: ' + str(datetime.datetime.now()) + '\n'
    with open('wip.log', 'a') as myfile: myfile.write(progress)

    base_calls_mtx = read_base_calls_matrix(ref_csv, alt_csv)

    run_model(base_calls_mtx, seed_vcf, num_models)

if __name__ == '__main__':
    main()


from . import SampleGenerator
import os
import pickle
from filelock import FileLock
import json

class SampleXdistGenerator():

    def __init__(self, addon_path, config_path=None, process_count=4):
        self.addon_path = addon_path
        self.process_count = process_count
        self.config_path = config_path

    def get_samples(self, store_events):
        if "PYTEST_XDIST_WORKER" in os.environ:
            file_path = os.environ.get("PYTEST_XDIST_TESTRUNUID") + "_events"
            with FileLock(str(file_path) + ".lock"):
                if os.path.exists(file_path):
                    with open(file_path, "rb") as file_obj:
                        store_sample = pickle.load(file_obj)
                else:
                    sample_generator = SampleGenerator(
                        self.addon_path, self.config_path)
                    tokenized_events = list(sample_generator.get_samples())
                    store_sample = {"conf_name" : SampleGenerator.conf_name, "tokenized_events" : tokenized_events}
                    if store_events:
                        self.store_events(tokenized_events)
                    with open(file_path, "wb") as file_obj:
                        pickle.dump(store_sample, file_obj)
        else:
            sample_generator = SampleGenerator(
                self.addon_path, self.config_path)
            tokenized_events = list(sample_generator.get_samples())
            store_sample = {"conf_name" : SampleGenerator.conf_name, "tokenized_events" : tokenized_events}
            if store_events:
                self.store_events(tokenized_events)
        return store_sample

    def store_events(self, tokenized_events):
        if not os.path.exists(os.path.join(os.getcwd(),".tokenized_events")):
            os.makedirs(os.path.join(os.getcwd(),".tokenized_events"))
        tokenized_samples_dict = {}
        for each_event in tokenized_events:
            if each_event.sample_name not in tokenized_samples_dict:
                if each_event.metadata.get("input_type") not in [
                        "modinput",
                        "windows_input",
                    ]:
                        expected_count = int(each_event.metadata.get("expected_event_count")) * int(each_event.metadata.get("sample_count"))
                else:
                    expected_count = each_event.metadata.get("expected_event_count")
                tokenized_samples_dict[each_event.sample_name] = {
                    "metadata": {
                        "host": each_event.metadata.get("host"),
                        "source": each_event.metadata.get("source"),
                        "sourcetype": each_event.metadata.get("sourcetype"),
                        "timestamp_type": each_event.metadata.get("timestamp_type"),
                        "input_type": each_event.metadata.get("input_type"),
                        "expected_event_count": expected_count,
                        "index": each_event.metadata.get("index", "main")
                    },
                    "events":[{
                        "event":each_event.event,
                        "key_fields":each_event.key_fields,
                        "time_values":each_event.time_values
                    }],
                }
            else:
                tokenized_samples_dict[each_event.sample_name]["events"].append({
                    "event":each_event.event,
                    "key_fields":each_event.key_fields,
                    "time_values":each_event.time_values
                })

        for sample_name,tokenized_sample in tokenized_samples_dict.items():
            with open("{}.json".format(os.path.join(os.getcwd(),".tokenized_events",sample_name)), "w") as eventfile:
                eventfile.write(json.dumps({sample_name:tokenized_sample}, indent="\t"))


# -*- coding: utf-8 -*-
# Copyright (c) 2011 University of Jyväskylä
#
# Authors:
#     Asko Soukka <asko.soukka@iki.fi>
#     Esa-Matti Suuronen <esa-matti@suuronen.org>
#
# This file is part of sauna.reload.
#
# sauna.reload is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# sauna.reload is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with sauna.reload.  If not, see <http://www.gnu.org/licenses/>.

"""Utilities for deferring autoinclude of selected paths"""

from pkg_resources import iter_entry_points

from z3c.autoinclude.dependency import DependencyFinder
from Zope2.App.zcml import load_config, load_string

import os
from pkg_resources import get_provider
from pkg_resources import working_set as ws
from zope.dottedname.resolve import resolve
from z3c.autoinclude.utils import DistributionManager
from z3c.autoinclude.utils import ZCMLInfo

DEFERRED_TARGET = "sauna.reload"

def defer_paths():
    """Defer autoincluding of given paths by changing *z3c.autoinclude.plugin*
    -entrypoints to target DEFERRED_TARGET instead of *plone*"""
    from sauna.reload import reload_paths

    for ep in iter_entry_points("z3c.autoinclude.plugin"):
        if ep.dist.location in reload_paths\
            and ep.dist.project_name != "sauna.reload":
            ep.module_name = DEFERRED_TARGET

def get_deferred_deps_info():
    """Return dictionary with lists of configuration files for the dependencies
    of the deferred eggs"""
    deferred = ["zc.table"]
    # FIXME: Assuming that all dependencies should be autoincluded will
    # probably get us into trouble, but let's see how big trouble. ``zc.table``
    # is an example of a dependency, whose ``configure.zcml`` will not run in
    # Plone environment. Some better solution than just a blacklist would be
    # welcome.
    from sauna.reload import reload_paths
    zcml_to_look_for = ("meta.zcml", "configure.zcml", "overrides.zcml")
    deps = ZCMLInfo(zcml_to_look_for)
    for ep in iter_entry_points("z3c.autoinclude.plugin"):
        if ep.module_name == DEFERRED_TARGET:
            deferred.append(ep.dist.project_name)
            # XXX: We cannot call DependencyFinder(ep.dist).includableInfo,
            # because it eventually imports also our development packages while
            # resolving existence of ``zcml_to_look_for``.
            finder = DependencyFinder(ep.dist)
            info = ZCMLInfo(zcml_to_look_for)
            for req in finder.context.requires():
                if ws.find(req).location in reload_paths:
                    continue
                dist_manager = DistributionManager(get_provider(req))
                for dotted_name in dist_manager.dottedNames():
                    module = resolve(dotted_name)
                    for candidate in zcml_to_look_for:
                        candidate_path = os.path.join(
                            os.path.dirname(module.__file__), candidate)
                        if os.path.isfile(candidate_path):
                            info[candidate].append(dotted_name)
            for key in deps:
                deps[key].extend(info.get(key, []))
    for key in deps:
        deps[key] = set([n for n in deps[key] if n not in deferred])
    return deps

def include_deferred_deps():
    # Build and execute a configuration file to include meta, configuration and
    # overrides for dependencies of the deferred development packages.
    deps = get_deferred_deps_info()
    config = u"""\
<configure
    xmlns="http://namespaces.zope.org/zope"
    xmlns:zcml="http://namespaces.zope.org/zcml">

    """ + u"".join([u"""<include
        zcml:condition="not-have disable-autoinclude"
        package="%s"
        file="meta.zcml"
        />""" % name for name in deps.get("meta.zcml", ())]) + """

    """ + u"".join([u"""<include
        zcml:condition="not-have disable-autoinclude"
        package="%s"
        file="configure.zcml"
        />""" % name for name in deps.get("configure.zcml", ())]) + """

    """ + u"".join([u"""<includeOverrides
        zcml:condition="not-have disable-autoinclude"
        package="%s"
        file="overrides.zcml"
        />""" % name for name in deps.get("overrides.zcml", ())]) + """

</configure>"""
    load_string(config)

def include_deferred():
    """Autoinclude deferred packages"""
    import sauna.reload
    load_config("autoinclude.zcml", sauna.reload)


# -*- coding: utf-8 -*-
"""
Problem A.
uses python 3.4.1

@author: Eric Kuritzky
"""
from collections import *
import itertools as ito
import operator as op
import functools as ft
from sys import argv, stdin, stdout, stderr, setrecursionlimit

#setrecursionlimit(1000000)

errprt = ft.partial(print, file=stderr)

def readcase(f):
    n, r, p, s = readints(f)
    return (r, p, s, n)

def solvecase(case):
    (r, p, s, n) = case
    if max(r, p, s) != min(r, p, s) + 1:
        return 'IMPOSSIBLE'
    return bestout(n, (p, r, s))

def bestout(n, prs):
    if n == 1:
        return ''.join(c for c, i in zip('PRS' ,prs) if i)
    if n & 1:
        i = prs.index(min(prs))
        k = prs[i]//2
        half = []
        for j in range(3):
            if i != j:
                prs1 = [k, k, k]
                prs1[j] += 1
                half.append(bestout(n-1, prs1))
    else:
        i = prs.index(max(prs))
        k = prs[i]//2
        half = []
        for j in range(3):
            if i != j:
                prs1 = [k, k, k]
                prs1[j] -= 1
                half.append(bestout(n-1, prs1))
    if half[0] > half[1]:
        half = reversed(half)
    return ''.join(half)

def readints(f):
    return list(map(int, f.readline().strip().split(' ')))

def readflds(f, types):
    if isinstance(types, tuple):
        return [typ(fld) for fld, typ
                in zip(f.readline().strip().split(),
                       ito.chain(types, ito.repeat(types[-1])))]
    else:
        return [types(fld) for fld in f.readline().strip().split()]

def main():
    with open(argv[1]) as f, open('out', 'w') as out:
        cases = int(f.readline())
        for ncase in range(1, cases+1):
            case = readcase(f)
            soln = solvecase(case)
            if len(argv) > 2:
                print('Case #%d: %s' % (ncase, soln))
            print('Case #%d: %s' % (ncase, soln), file=out)

from datetime import datetime

start = datetime.now()
print(str(start))
main()
stop = datetime.now()
print(str(stop-start))


# https://leetcode.com/problems/best-time-to-buy-and-sell-stock/

class Solution(object):
    def maxProfit(self, prices):
        """
        :type prices: List[int]
        :rtype: int
        """
        length = len(prices)
        profit = 0
        if length < 2:
            return profit
        min_price = prices[0]
        for i in range(1, length):
            val = prices[i]
            profit = max(profit, val - min_price)
            min_price = min(val, min_price)
        return profit

def test():
    prices = [7, 1, 5, 3, 6, 4]
    s = Solution()
    ans = s.maxProfit(prices)
    assert ans == 5


import turtle

size =  729	#The side length of the coloured square
			#Powers of 3 works best. Products of powers of 3 works ok
			#Any number can be used, but the squares will be rounded to integer pixel coordinates

tu = turtle.Turtle()
turtle.Screen().colormode(255)
tu.pencolor(200,10,255)
tu.fillcolor(200,10,255)
turtle.hideturtle()
def straightright(l):
	tu.fd(l)
	tu.right(90)

def square(l):
	for i in range(4):
		straightright(l)

def fillwhite(x, y, length, depht):
	if depht <= 0:
		return None
	elif length > 40:
		pass
		turtle.update()
	tu.up()
	tu.setx(x)
	tu.sety(y)

	length /= 3

	tu.fd(length)
	tu.rt(90)
	tu.fd(length)
	tu.lt(90)
	tu.down()
	tu.begin_fill()
	square(length)
	tu.end_fill()

	fillwhite(x, y, length, depht -1)
	fillwhite(x + length, y, length, depht -1)
	fillwhite(x + 2*length, y, length, depht -1)
	y -= length
	fillwhite(x, y, length, depht -1)
	fillwhite(x + 2*length, y, length, depht -1)
	y -= length
	fillwhite(x, y, length, depht -1)
	fillwhite(x + length, y, length, depht -1)
	fillwhite(x + 2*length, y, length, depht -1)

turtle.tracer(0,0)
tu.hideturtle()
turtle.setundobuffer(None)
tu.up()
tu.setpos(-size//2, size//2)
tu.down()
tu.begin_fill()
square(size)
tu.end_fill()
tu.pencolor(255,255,255)
tu.fillcolor(255,255,255)

fillwhite(-size//2,size//2,size, 5)
turtle.update()
turtle.mainloop()

DM550 - Introduction to Programming/Python_examples/Carpet.py


__author__ = 'andong'

import re

chars = {}
mode_str = '[\u4e00-\u9fa5]'
mode = re.compile(mode_str)

file = open('corpus/../corpus/全唐诗.out', 'r', encoding='utf-8')
content = file.read()
found = mode.findall(content)
len = len(found)
file.close()

for c in found:
    if c in chars:
        chars[c] += 1;
    else:
        chars[c] = 1;

s_chars = sorted(chars.items(), key=lambda d:d[1], reverse=True)

out = open('corpus/../corpus/char_count.txt', 'w', encoding='utf-8')
for c in s_chars:
    out.write(c[0]+" "+str(c[1])+'\n')

out.close()


import numpy as np
import matplotlib.pyplot as plt

with open('outdoor3.txt') as f:
   lines = f.readlines()
DataSize = 400
Data = np.zeros((4,6,6,DataSize))
means = np.zeros((4, 6, 6, DataSize))
alert = np.zeros((4, 6, 6, DataSize))
ignored = np.zeros((4, 6, 6, DataSize))
std = np.zeros((4, 6, 6, DataSize))
x = range(0, DataSize)
count = 0
index = 0
for line in lines:
   exploded = line.split(' ')
   if exploded[0] == 'Camera':
      value = 0
      camera = int(exploded[1])
      if len(exploded) > 15:
         i = int(exploded[3])
         j = int(exploded[4])
         value = int(exploded[6])
         mean = float(exploded[8])
         std[camera][i][j][index] = float(exploded[10])
         means[camera][i][j][index] = mean
         alert[camera][i][j][index] = int(exploded[14])
         ignored[camera][i][j][index] = int(exploded[15])
      #print camera, i, j, value
      #print , , , , stats float(exploded[8]), int(exploded[9]), float(exploded[10])
         Data[camera][i][j][index] = value
      count+=1
      if count == 36*4:
         index+= 1
         count = 0
#print std[2][3][3]
#print  alert[2][3][3]
plt.figure(1)
s = plt.subplot(221)
plt.plot(x, Data[2][3][3], 'r--', x, means[2][3][3], 'r')
s.set_title('IR Data (--) and running average (-) per Sample')
s = plt.subplot(222)
plt.plot(x, alert[2][3][3])
s.set_title('Sample alert')
s = plt.subplot(223)
plt.plot(x, std[2][3][3])
s.set_title('Standard Deviation')
s = plt.subplot(224)
plt.plot(x, ignored[2][3][3])
s.set_title('Sample ignored (1) or added to running average (0)')
'''
plt.plot(x, Data[2][0][0], '--',
         x, Data[2][0][1], '--',
         x, Data[2][0][2], '--',
         x, Data[2][0][3], '--',
         x, Data[2][0][4], '--',
         x, Data[2][0][5], '--')
plt.plot(x, Data[2][1][0], '--',
         x, Data[2][1][1], '--',
         x, Data[2][1][2], '--',
         x, Data[2][1][3], '--',
         x, Data[2][1][4], '--',
         x, Data[2][1][5], '--')
plt.plot(x, Data[2][2][0], '--',
         x, Data[2][2][1], '--',
         x, Data[2][2][2], '--',
         x, Data[2][2][3], '--',
         x, Data[2][2][4], '--',
         x, Data[2][2][5], '--')
plt.plot(x, Data[2][3][0], '--',
         x, Data[2][3][1], '--',
         x, Data[2][3][2], '--',
         x, Data[2][3][3], '--',
         x, Data[2][3][4], '--',
         x, Data[2][3][5], '--')
plt.plot(x, Data[2][4][0], '--',
         x, Data[2][4][1], '--',
         x, Data[2][4][2], '--',
         x, Data[2][4][3], '--',
         x, Data[2][4][4], '--',
         x, Data[2][4][5], '--')
plt.plot(x, Data[2][5][0], '--',
         x, Data[2][5][1], '--',
         x, Data[2][5][2], '--',
         x, Data[2][5][3], '--',
         x, Data[2][5][4], '--',
         x, Data[2][5][5], '--')

plt.plot(x, means[3][1][0], '--',
         x, means[3][1][1], '--',
         x, means[3][1][2], '--',
         x, means[3][1][3], '--',
         x, means[3][1][4], '--',
         x, means[3][1][5], '--')
'''
plt.show()


from django.db import models
from django.contrib.auth.models import User
from django.contrib.auth.signals import user_logged_in
from util.utils import drchrono_get, drchrono_post

TODO = 200
class Patient(models.Model):
    doctor = models.ForeignKey(User, related_name="patient")
    drchrono_id = models.IntegerField()
    first_name = models.CharField(max_length=TODO, blank=True, null=True)
    middle_name = models.CharField(max_length=TODO, blank=True, null=True)
    last_name = models.CharField(max_length=TODO, null=True)
    birthday = models.DateField(blank=True, null=True)
    email = models.CharField(max_length=TODO, blank=True, null=True)
    gender = models.CharField(max_length=TODO, default="")
    zip_code = models.CharField(max_length=TODO, blank=True, null=True)
    cell_phone = models.CharField(max_length=TODO, blank=True, null=True)
    photo = models.CharField(max_length=TODO, blank=True, null=True)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    @classmethod
    def create_or_update_patient(klass, user, data):
        patient = Patient.objects.filter(doctor=user, drchrono_id=data.get('id', -1)).first()
        if not patient:
            patient = Patient(doctor=user)
        patient.drchrono_id = data.get('id')
        patient.first_name = data.get('first_name')
        patient.middle_name = data.get('middle_name')
        patient.last_name = data.get('last_name')
        patient.birthday = data.get('date_of_birth')
        patient.email = data.get('email')
        patient.gender = data.get('gender')
        patient.photo = data.get('patient_photo')
        patient.zip_code = data.get('zip_code')
        patient.cell_phone = data.get('cell_phone')
        patient.save()
        return patient

    @classmethod
    def last_updated_date(klass, user=None):
        last_updated = Patient.objects.order_by('-updated_at')
        if user:
            last_updated = last_updated.filter(doctor=user)
        if not last_updated:
            return None
        return last_updated.first().updated_at

class BirthdayAlert(models.Model):
    send_text = models.BooleanField(default=False)
    send_email = models.BooleanField(default=False)
    custom_email_body = models.TextField()
    custom_text_body = models.CharField(max_length=160)
    birthday = models.DateField()
    doctor = models.ForeignKey(User, related_name="patient_alert")
    patient = models.ForeignKey(Patient, related_name="alert")
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

#-------------------------------------------------------------------------------
#-------------------------------------------------------------------------------

def synchronize_patients(sender, user, request, **kwargs):
    patients = []
    patients_url = 'https://drchrono.com/api/patients'
    last_updated_date = Patient.last_updated_date(user=user)
    params = {"since": last_updated_date}
    while patients_url:
        data = drchrono_get(request, patients_url, user=user, params=params)
        patients.extend(data['results'])
        patients_url = data['next']
    for patient in patients:
        Patient.create_or_update_patient(user, patient)

user_logged_in.connect(synchronize_patients)


from dataclasses import asdict

import numpy as np
import pytest

from napari_animation.interpolation import (
    interpolate_bool,
    interpolate_log,
    interpolate_num,
    interpolate_seq,
)

from ..utils import nested_assert_close

# Define some functions used for testing

# Actual tests
@pytest.mark.parametrize("a", [0.0, 0])
@pytest.mark.parametrize("b", [100.0, 100])
@pytest.mark.parametrize("fraction", [0, 0.0, 0.5, 1.0, 1])
def test_interpolate_num(a, b, fraction):
    """Check that interpolation of numbers produces valid output"""
    result = interpolate_num(a, b, fraction)
    assert isinstance(result, type(a))
    assert result == fraction * b

@pytest.mark.parametrize("a,b", [([0.0, 0.0, 0.0], [1.0, 1.0, 1.0])])
@pytest.mark.parametrize(
    "fraction,expected",
    [(0.0, [0.0, 0.0, 0.0]), (0.5, [0.5, 0.5, 0.5]), (1.0, [1.0, 1.0, 1.0])],
)
def test_interpolate_seq(a, b, fraction, expected):
    """Check that interpolation of sequences produces valid output"""
    result = interpolate_seq(a, b, fraction)
    assert isinstance(result, type(a))
    assert result == expected

@pytest.mark.parametrize("a", [True, False])
@pytest.mark.parametrize("b", [True, False])
@pytest.mark.parametrize("fraction", [0, 0.25, 0.75, 1])
def test_interpolate_bool(a, b, fraction):
    result = interpolate_bool(a, b, fraction)
    if fraction > 0.0:
        assert result == b
    else:
        assert result == a

@pytest.mark.parametrize("a", [1.0, 1])
@pytest.mark.parametrize("b", [10.0, 10])
@pytest.mark.parametrize("fraction", [0, 0.0, 0.5, 1.0, 1])
def test_interpolate_log(a, b, fraction):
    """Check that log interpolation produces valid output"""
    result = interpolate_log(a, b, fraction)
    assert result == np.power(10, fraction)

@pytest.mark.parametrize("fraction", [0, 0.2, 0.4, 0.6, 0.8, 1])
def test_interpolate_state(frame_sequence, fraction):
    """Check that state interpolation works"""
    initial_state = frame_sequence[0]
    final_state = frame_sequence[-1]
    result = frame_sequence._interpolate_state(
        initial_state, final_state, fraction
    )
    assert len(asdict(result)) == len(asdict(initial_state))
    if fraction == 0:
        # assert result == initial_state
        nested_assert_close(asdict(result), asdict(initial_state))
    elif fraction == 1:
        # assert result == final_state
        nested_assert_close(asdict(result), asdict(final_state))

    # else:
    # should find something else to test


# ----------------------------------------------
# -*- encoding=utf-8 -*-                      #
# __author__:'焉知飞鱼'                        #
# CreateTime:                                 #
#       2021/7/8 9:08                       #
#                                             #
#               天下风云出我辈，                 #
#               一入江湖岁月催。                 #
#               皇图霸业谈笑中，                 #
#               不胜人生一场醉。                 #
# ----------------------------------------------
# from google.colab import drive
# drive.mount('/content/drive')
# # 更改运行目录
# import os
# os.chdir("/content/drive/My Drive/Repeat Buyers Prediction/")

import gc
import pandas as pd
import numpy as np

#导入分析库
from sklearn.model_selection import train_test_split
from sklearn.model_selection import StratifiedKFold
from sklearn.ensemble import RandomForestClassifier
from sklearn.linear_model import LinearRegression
from sklearn.metrics import classification_report
import lightgbm as lgb
import xgboost as xgb
import catboost as cat

from sklearn.metrics import roc_auc_score, roc_curve, auc
# LOCAL_QUICK = True
LOCAL_QUICK = False
sample_percent = 0.1

MORE_FE = False
# MORE_FE = True
FE_V1 = False if MORE_FE else True

# 加载数据
# 用户行为，使用format1进行加载
user_log = pd.read_csv('./user_log_format1.csv', dtype={'time_stamp':'str'})

user_info = pd.read_csv('./user_info_format1.csv')

train_data1 = pd.read_csv('./train_format1.csv')

sub_data = pd.read_csv('./test_format1.csv')
data_train = pd.read_csv('./train_format2.csv')

if LOCAL_QUICK:
    print('Local quick test: {}, rate is {}'.format(
        LOCAL_QUICK, sample_percent))
    data = user_log.sample(int(len(user_log) * sample_percent))
    data1 = user_info.sample(int(len(user_info) * sample_percent))
    data2 = train_data1.sample(int(len(train_data1) * sample_percent))
    # submission = sub_data.sample(int(len(sub_data) * sample_percent))
    submission = sub_data.copy()

else:
    print('All sample train')
    data = user_log.copy()
    data1 = user_info.copy()
    data2 = train_data1.copy()
    submission = sub_data.copy()
    del user_log, user_info, train_data1, sub_data
print('---data shape---')
for df in [data, data1, data2, submission, data_train]:
    print(df.shape)

# 合并train,test,user_info
data2['origin'] = 'train'
submission['origin'] = 'test'
matrix = pd.concat([data2, submission], ignore_index=True, sort=False)
matrix.drop(['prob'], axis=1, inplace=True)
# 连接user_info表，通过user_id关联
matrix = matrix.merge(data1, on='user_id', how='left')
# 使用merchant_id（原列名seller_id）
data.rename(columns={'seller_id':'merchant_id'}, inplace=True)

# 格式化
data['user_id'] = data['user_id'].astype('int32')
data['merchant_id'] = data['merchant_id'].astype('int32')
data['item_id'] = data['item_id'].astype('int32')
data['cat_id'] = data['cat_id'].astype('int32')
data['brand_id'].fillna(0, inplace=True)
data['brand_id'] = data['brand_id'].astype('int32')
data['time_stamp'] = pd.to_datetime(data['time_stamp'], format='%H%M')
# 缺失值填充
matrix['age_range'].fillna(0, inplace=True)
matrix['gender'].fillna(2, inplace=True)

# # gender用众数填充 表现更差
# matrix['gender'].fillna(matrix['gender'].mode()[0],inplace=True)
# # 年龄用中位数填充
# matrix['age_range'].fillna(matrix['age_range'].median(),inplace=True)

matrix['age_range'] = matrix['age_range'].astype('int8')
matrix['gender'] = matrix['gender'].astype('int8')
matrix['label'] = matrix['label'].astype('str')
matrix['user_id'] = matrix['user_id'].astype('int32')
matrix['merchant_id'] = matrix['merchant_id'].astype('int32')

del data1, data2
gc.collect()

##### 特征处理
##### User特征处理
groups = data.groupby(['user_id'])
# 用户交互行为数量 u1
temp = groups.size().reset_index().rename(columns={0:'u1'})
matrix = matrix.merge(temp, on='user_id', how='left')
# 使用agg 基于列的聚合操作，统计唯一值个数 item_id, cat_id, merchant_id, brand_id
temp = groups['item_id'].agg([('u2', 'nunique')]).reset_index()
matrix = matrix.merge(temp, on='user_id', how='left')
temp = groups['cat_id'].agg([('u3', 'nunique')]).reset_index()
matrix = matrix.merge(temp, on='user_id', how='left')
temp = groups['merchant_id'].agg([('u4', 'nunique')]).reset_index()
matrix = matrix.merge(temp, on='user_id', how='left')
temp = groups['brand_id'].agg([('u5', 'nunique')]).reset_index()
matrix = matrix.merge(temp, on='user_id', how='left')
# 时间间隔特征 u6 按照小时
temp = groups['time_stamp'].agg([('F_time', 'min'), ('L_time', 'max')]).reset_index()
temp['u6'] = (temp['L_time'] - temp['F_time']).dt.seconds/3600
matrix = matrix.merge(temp[['user_id', 'u6']], on='user_id', how='left')
# 统计操作类型为0，1，2，3的个数
temp = groups['action_type'].value_counts().unstack().reset_index().rename(
    columns={0:'u7', 1:'u8', 2:'u9', 3:'u10'})
matrix = matrix.merge(temp, on='user_id', how='left')

del temp
gc.collect()

##### 商家特征处理
groups = data.groupby(['merchant_id'])
# 商家被交互行为数量 m1
temp = groups.size().reset_index().rename(columns={0:'m1'})
matrix = matrix.merge(temp, on='merchant_id', how='left')
# 统计商家被交互的user_id, item_id, cat_id, brand_id 唯一值
temp = groups['user_id', 'item_id', 'cat_id', 'brand_id'].nunique().reset_index().rename(columns={
    'user_id':'m2',
    'item_id':'m3',
    'cat_id':'m4',
    'brand_id':'m5'})
matrix = matrix.merge(temp, on='merchant_id', how='left')
# 统计商家被交互的action_type 唯一值
temp = groups['action_type'].value_counts().unstack().reset_index().rename(
    columns={0:'m6', 1:'m7', 2:'m8', 3:'m9'})
matrix = matrix.merge(temp, on='merchant_id', how='left')

del temp
gc.collect()

# 按照merchant_id 统计随机负采样的个数
temp = data_train[data_train['label']==-1].groupby(['merchant_id']).size().reset_index().rename(columns={0:'m10'})
matrix = matrix.merge(temp, on='merchant_id', how='left')

##### 用户+商户特征
groups = data.groupby(['user_id', 'merchant_id'])
temp = groups.size().reset_index().rename(columns={0:'um1'})
matrix = matrix.merge(temp, on=['user_id', 'merchant_id'], how='left')
temp = groups['item_id', 'cat_id', 'brand_id'].nunique().reset_index().rename(columns={
    'item_id':'um2',
    'cat_id':'um3',
    'brand_id':'um4'
})
matrix = matrix.merge(temp, on=['user_id', 'merchant_id'], how='left')
temp = groups['action_type'].value_counts().unstack().reset_index().rename(columns={
    0:'um5',
    1:'um6',
    2:'um7',
    3:'um8'
})
matrix = matrix.merge(temp, on=['user_id', 'merchant_id'], how='left')
temp = groups['time_stamp'].agg([('frist', 'min'), ('last', 'max')]).reset_index()
temp['um9'] = (temp['last'] - temp['frist']).dt.seconds/3600
temp.drop(['frist', 'last'], axis=1, inplace=True)
matrix = matrix.merge(temp, on=['user_id', 'merchant_id'], how='left')

del temp
gc.collect()

matrix['r1'] = matrix['u9']/matrix['u7'] # 用户购买点击比
matrix['r2'] = matrix['m8']/matrix['m6'] # 商家购买点击比
matrix['r3'] = matrix['um7']/matrix['um5'] #不同用户不同商家购买点击比

matrix.fillna(0, inplace=True)

# # 修改age_range字段名称为 age_0, age_1, age_2... age_8
temp = pd.get_dummies(matrix['age_range'], prefix='age')
matrix = pd.concat([matrix, temp], axis=1)
temp = pd.get_dummies(matrix['gender'], prefix='g')
matrix = pd.concat([matrix, temp], axis=1)
matrix.drop(['age_range', 'gender'], axis=1, inplace=True)

del temp
gc.collect()

# train、test-setdata
train_data = matrix[matrix['origin'] == 'train'].drop(['origin'], axis=1)
test_data = matrix[matrix['origin'] == 'test'].drop(['label', 'origin'], axis=1)

if not LOCAL_QUICK:
    if FE_V1:
        train_data.to_csv('train_data.csv')
        test_data.to_csv('test_data.csv')
    if MORE_FE:
        train_data.to_csv('train_data_moreFE.csv')
        test_data.to_csv('test_data_moreFE.csv')

del matrix
gc.collect()

# Load FeatureData
if not LOCAL_QUICK:
    if FE_V1:
        train_data = pd.read_csv('train_data.csv')
        test_data = pd.read_csv('test_data.csv')
    if MORE_FE:
        train_data = pd.read_csv('train_data_moreFE.csv')
        test_data = pd.read_csv('test_data_moreFE.csv')

    # FeatureSelect_QUICK = True # Feature Select
FeatureSelect_QUICK = False
if FeatureSelect_QUICK: # 使用部分样本进行快速特征选择
    train_data = train_data.sample(int(len(train_data) * sample_percent))

# train_data = train_data[train_col]
train_X, train_y = train_data.drop(['label'], axis=1), train_data['label']

del train_data

X_train, X_valid, y_train, y_valid = train_test_split(train_X, train_y, test_size=.2, random_state=42) # test_size=.3

#==================XGB Model
# get data
train_X, train_y = train_data.drop(['label'], axis=1), train_data['label']
del train_data
X_train, X_valid, y_train, y_valid = train_test_split(train_X, train_y, test_size=.2, random_state=42) # test_size=.3
def xgb_train(X_train, y_train, X_valid, y_valid, verbose=True):
    model_xgb = xgb.XGBClassifier(
        max_depth=10, # raw8
        n_estimators=1000,
        min_child_weight=300,
        colsample_bytree=0.8,
        subsample=0.8,
        eta=0.3,
        seed=42
    )

    model_xgb.fit(
        X_train,
        y_train,
        eval_metric='auc',
        eval_set=[(X_train, y_train), (X_valid, y_valid)],
        verbose=verbose,
        early_stopping_rounds=10 # 早停法，如果auc在10epoch没有进步就stop
    )
    print(model_xgb.best_score)
    return model_xgb
model_xgb = xgb_train(X_train, y_train, X_valid, y_valid, verbose=False)
prob = model_xgb.predict_proba(test_data)

submission['prob'] = pd.Series(prob[:,1])
# submission.drop(['origin'], axis=1, inplace=True)
submission.to_csv('submission_xgb.csv', index=False)

#=======================LGB Model
def lgb_train(X_train, y_train, X_valid, y_valid, verbose=True):
    model_lgb = lgb.LGBMClassifier(
        max_depth=10, # 8
        n_estimators=1000,
        min_child_weight=200,
        colsample_bytree=0.8,
        subsample=0.8,
        eta=0.3,
        seed=42
    )

    model_lgb.fit(
        X_train,
        y_train,
        eval_metric='auc',
        eval_set=[(X_train, y_train), (X_valid, y_valid)],
        verbose=verbose,
        early_stopping_rounds=10
    )

    print(model_lgb.best_score_['valid_1']['auc'])
    return model_lgb
model_lgb = lgb_train(X_train, y_train, X_valid, y_valid, verbose=False)
prob = model_lgb.predict_proba(test_data)
submission['prob'] = pd.Series(prob[:,1])
# submission.drop(['origin'], axis=1, inplace=True)
submission.to_csv('submission_lgb.csv', index=False)

#========================Cat Model
def cat_train(X_train, y_train, X_valid, y_valid, verbose=True):
    model_cat = cat.CatBoostClassifier(learning_rate=0.02, iterations=5000, eval_metric='AUC', od_wait=50,
                                       od_type='Iter', random_state=10, thread_count=8, l2_leaf_reg=1, verbose=verbose)
    model_cat.fit(X_train, y_train, eval_set=[(X_valid, y_valid)], early_stopping_rounds=50,
                  use_best_model=True)

    print(model_cat.best_score_['validation']['AUC'])
    return model_cat
model_cat = cat_train(X_train, y_train, X_valid, y_valid, verbose=False)
prob = model_cat.predict_proba(test_data)
submission['prob'] = pd.Series(prob[:,1])
# submission.drop(['origin'], axis=1, inplace=True)
submission.to_csv('submission_cat.csv', index=False)

# ==========================================StratifiedKFold
def get_train_testDF(train_df,label_df):
    skv = StratifiedKFold(n_splits=5, shuffle=True, random_state=42)
    trainX = []
    trainY = []
    testX = []
    testY = []
    for train_index, test_index in skv.split(X=train_df, y=label_df):
        train_x, train_y, test_x, test_y = train_df.iloc[train_index, :], label_df.iloc[train_index], \
                                           train_df.iloc[test_index, :], label_df.iloc[test_index]

        trainX.append(train_x)
        trainY.append(train_y)
        testX.append(test_x)
        testY.append(test_y)
    return trainX, testX, trainY, testY

# >>>>>>>>>>>>>>>>>>>>>lightgbm
# get data
if not LOCAL_QUICK:
    if FE_V1:
        train_data = pd.read_csv('train_data.csv')
        test_data = pd.read_csv('test_data.csv')
    if MORE_FE:
        train_data = pd.read_csv('train_data_moreFE.csv')
        test_data = pd.read_csv('test_data_moreFE.csv')

train_X, train_y = train_data.drop(['label'], axis=1), train_data['label']

del train_data

# Split Train&Valid Data
X_train, X_valid, y_train, y_valid = get_train_testDF(train_X, train_y)

# 将训练数据集划分分别训练5个lgbm,xgboost和catboost 模型
# lightgbm模型

pred_lgbms = []
for i in range(5):
    print('\n============================LGB training use Data {}/5============================\n'.format(i+1))
    model_lgb = lgb.LGBMClassifier(
        max_depth=10, # 8
        n_estimators=1000,
        min_child_weight=200,
        colsample_bytree=0.8,
        subsample=0.8,
        eta=0.3,
        seed=42
    )

    model_lgb.fit(
        X_train[i],
        y_train[i],
        eval_metric='auc',
        eval_set=[(X_train[i], y_train[i]), (X_valid[i], y_valid[i])],
        verbose=False,
        early_stopping_rounds=10
    )

    print(model_lgb.best_score_['valid_1']['auc'])

    pred = model_lgb.predict_proba(test_data)
    pred = pd.DataFrame(pred[:,1])
    pred_lgbms.append(pred)
pred_lgbms = pd.concat(pred_lgbms, axis=1)
print(pred_lgbms)

submission['prob'] = pred_lgbms.mean(axis=1)
# submission.drop(['origin'], axis=1, inplace=True)
submission.to_csv('submission_KFold_lgb.csv', index=False)

####0.6784

# >>>>>>>>>>>>>>>>>>>>>catgbm¶
# get data
if not LOCAL_QUICK:
    if FE_V1:
        train_data = pd.read_csv('train_data.csv')
        test_data = pd.read_csv('test_data.csv')
    if MORE_FE:
        train_data = pd.read_csv('train_data_moreFE.csv')
        test_data = pd.read_csv('test_data_moreFE.csv')

train_X, train_y = train_data.drop(['label'], axis=1), train_data['label']

del train_data

# Split Train&Valid Data
X_train, X_valid, y_train, y_valid = get_train_testDF(train_X, train_y)
# 将训练数据集划分分别训练5个lgbm,xgboost和catboost 模型
# catgbm模型

pred_cats = []
for i in range(5):
    print('\n============================CAT training use Data {}/5============================\n'.format(i+1))
    model_cat = cat.CatBoostClassifier(learning_rate=0.02, iterations=5000, eval_metric='AUC', od_wait=50,
                                       od_type='Iter', random_state=10, thread_count=8, l2_leaf_reg=1, verbose=False)
    model_cat.fit(X_train[i], y_train[i], eval_set=[(X_valid[i], y_valid[i])], early_stopping_rounds=50,
                  use_best_model=True)
    # print(model_cat.evals_result_)
    print(model_cat.best_score_['validation']['AUC'])

    pred = model_cat.predict_proba(test_data)
    pred = pd.DataFrame(pred[:,1])
    pred_cats.append(pred)
pred_cats = pd.concat(pred_cats, axis=1)

submission['prob'] = pred_cats.mean(axis=1)
# submission.drop(['origin'], axis=1, inplace=True)
submission.to_csv('submission_KFold_cat.csv', index=False)
#### 0.68001

# >>>>>>>>>>>>>>>>>>>>>xgboost¶
# get data
if not LOCAL_QUICK:
    if FE_V1:
        train_data = pd.read_csv('train_data.csv')
        test_data = pd.read_csv('test_data.csv')
    if MORE_FE:
        train_data = pd.read_csv('train_data_moreFE.csv')
        test_data = pd.read_csv('test_data_moreFE.csv')

train_X, train_y = train_data.drop(['label'], axis=1), train_data['label']

del train_data

# Split Train&Valid Data
X_train, X_valid, y_train, y_valid = get_train_testDF(train_X, train_y)

# 将训练数据集划分分别训练5个lgbm,xgboost和catboost 模型
# xgboost模型

pred_xgbs = []
for i in range(5):
    print('\n============================XGB training use Data {}/5============================\n'.format(i+1))
    model_xgb = xgb.XGBClassifier(
        max_depth=10, # raw8
        n_estimators=1000,
        min_child_weight=300,
        colsample_bytree=0.8,
        subsample=0.8,
        eta=0.3,
        seed=42
    )

    model_xgb.fit(
        X_train[i],
        y_train[i],
        eval_metric='auc',
        eval_set=[(X_train[i], y_train[i]), (X_valid[i], y_valid[i])],
        verbose=False,
        early_stopping_rounds=10 # 早停法，如果auc在10epoch没有进步就stop
    )

    print(model_xgb.best_score)

    pred = model_xgb.predict_proba(test_data)
    pred = pd.DataFrame(pred[:,1])
    pred_xgbs.append(pred)
pred_xgbs = pd.concat(pred_xgbs, axis=1)

# make submission
submission['prob'] = pred_xgbs.mean(axis=1)
# submission.drop(['origin'], axis=1, inplace=True)
submission.to_csv('submission_KFold_xgb.csv', index=False)
#### 0.6803

# Blending
lgb6812 = pd.read_csv("submission_lgb0.6812968.csv")
xgb6787 = pd.read_csv("submission_xgb0.6787.csv")
cat6777 = pd.read_csv("submission_cat-val0.6827785215-onling0.6777246.csv")
# 先构造一个矩阵
df = np.array([lgb6812.prob, xgb6787.prob, cat6777.prob])
# 计算协方差矩阵
np.corrcoef(df)
sub = lgb6812.copy()

sub.prob = 0.6*lgb6812.prob + 0.4*cat6777.prob # Online test score:0.6830807
sub.to_csv('./sub_blended11.csv', index=False)
####################################0.6833209################################
sub.prob = 0.5*lgb6812.prob + 0.3*cat6777.prob + 0.2*xgb6787.prob# Online test 0.6833209
sub.to_csv('./sub_blended12.csv', index=False)

sub.prob = 0.45*lgb6812.prob + 0.3*cat6777.prob + 0.25*xgb6787.prob# Online test 0.6832934
sub.to_csv('./sub_blended13.csv', index=False)
####################################0.6833171################################
sub.prob = 0.45*lgb6812.prob + 0.35*cat6777.prob + 0.2*xgb6787.prob# Online test 0.6833171
sub.to_csv('./sub_blended14.csv', index=False)


from opengeo.geoserver import util
from PyQt4 import QtGui, QtCore

class TreeItem(QtGui.QTreeWidgetItem):
    def __init__(self, element, icon = None, text = None):
        QtGui.QTreeWidgetItem.__init__(self)
        self.element = element
        self.setData(0, QtCore.Qt.UserRole, element)
        text = text if text is not None else util.name(element)
        self.setText(0, text)
        if icon is not None:
            self.setIcon(0, icon)
        self.setFlags(QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsSelectable)

    def refreshContent(self, explorer):
        self.takeChildren()
        if hasattr(self.element, "refresh"):
            self.element.refresh()
        explorer.run(self.populate, None, [])

    def descriptionWidget(self, tree, explorer):
        text = self.getDescriptionHtml(tree, explorer)
        self.description = QtGui.QTextBrowser()
        self.description.setOpenLinks(False)
        def linkClicked(url):
            self.linkClicked(tree, explorer, url)
        self.description.connect(self.description, QtCore.SIGNAL("anchorClicked(const QUrl&)"), linkClicked)
        self.description.setHtml(text)
        return self.description

    def getDescriptionHtml(self, tree, explorer):
        html = self._getDescriptionHtml(tree, explorer)
        html = u"""
            <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">

            %s

            """ % html
        return html

    def _getDescriptionHtml(self, tree, explorer):
        html = u'

  ' + self.text(0) + '

'
        actions = self.contextMenuActions(tree, explorer)
        for action in actions:
            if action.isEnabled():
                html += '
' + action.text() + '
\n'
        html += '
'
        return html

    def linkClicked(self, tree, explorer, url):
        actionName = url.toString()
        actions = self.contextMenuActions(tree, explorer)
        for action in actions:
            if action.text() == actionName:
                action.trigger()
                return

    def contextMenuActions(self, tree, explorer):
        return []

    def multipleSelectionContextMenuActions(self, tree, explorer, selected):
        return []

    def acceptDroppedItem(self, tree, explorer, item):
        return []

    def acceptDroppedItems(self, tree, explorer, items):
        explorer.setProgressMaximum(len(items))
        toUpdate = []
        for i, item in enumerate(items):
            toUpdate.extend(self.acceptDroppedItem(tree, explorer, item))
            explorer.setProgress(i + 1)
        explorer.resetActivity()
        return toUpdate

    def acceptDroppedUris(self, tree, explorer, uris):
        return []


import numpy as np
import xarray as xr
import pandas as pd
import sys
import json
import os
import datetime

from xarray.core.utils import (
    decode_numpy_dict_values,
    either_dict_or_kwargs,
    ensure_us_time_resolution,
)
from numpy.compat import (
    asbytes, asstr, asunicode, bytes, basestring, os_fspath, os_PathLike,
    pickle, contextlib_nullcontext
    )
from numpy.lib import format

class NumpyEncoder(json.JSONEncoder):
    def default(self, obj):
        if isinstance(obj, np.ndarray):
            return obj.tolist()
        if isinstance(obj, datetime.datetime):
            return obj.__str__()
        if isinstance(obj, np.datetime64):
            return obj.__str__()
        return json.JSONEncoder.default(self, obj)

def _is_string_like(obj):
    """
    Check whether obj behaves like a string.
    """
    try:
        obj + ''
    except (TypeError, ValueError):
        return False
    return True

def myJsonConverter(o):
    if isinstance(o, datetime.datetime):
        return o.__str__()

def save_npys(file, data, compress=False,min_dims_coord_npy = 2):
    if isinstance(data,xr.DataArray):
        _save_dataarray(file, data, compress=compress,min_dims_coord_npy=min_dims_coord_npy)
    elif isinstance(data,xr.Dataset):
        _save_dataset(file, data, compress=compress,min_dims_coord_npy=min_dims_coord_npy)
    else:
        raise BaseException('Unexpected type %'%str(type(data)))

class zip_file():
    def __init__(self,file, *args, **kwargs):
        """
        Create a ZipFile.

        Allows for Zip64, and the `file` argument can accept file, str, or
        pathlib.Path objects. `args` and `kwargs` are passed to the zipfile.ZipFile
        constructor.
        """
        if not hasattr(file, 'read'):
            file = os_fspath(file)
        import zipfile
        kwargs['allowZip64'] = True
        file_dir, file_prefix = os.path.split(file) if _is_string_like(file) else (None, 'tmp')
        self.file_dir = file_dir
        self.file_prefix = file_prefix
        self.zipf = zipfile.ZipFile(file, *args, **kwargs)

    def close(self):
        self.zipf.close()
    def open(self,x):
        return self.zipf.open(x)
    def read(self,x):
        return self.zipf.read(x)
    def namelist(self):
        return self.zipf.namelist()
    def add_bin_data(self,fname,data_bytes):
        if sys.version_info >= (3, 6):
            with self.zipf.open(fname, 'w', force_zip64=True) as fid:
                fid.write(data_bytes)
        else:
            import tempfile
            fd, tmpfile = tempfile.mkstemp(prefix=self.file_prefix, dir=self.file_dir, suffix=fname)
            os.close(fd)
            try:
                fid = open(tmpfile, 'wb')
                try:
                    fid.write(data_bytes)
                    fid.close()
                    fid = None
                    self.zipf.write(tmpfile, arcname=fname)
                except IOError as exc:
                    raise IOError("Failed to write to %s: %s" % (tmpfile, exc))
                finally:
                    if fid:
                        fid.close()
            finally:
                os.remove(tmpfile)

    def add_npy(self,fname,val):
        if sys.version_info >= (3, 6):
            with self.zipf.open(fname, 'w', force_zip64=True) as fid:
                    format.write_array(fid, np.asanyarray(val), allow_pickle=False, pickle_kwargs=None)
        else:
            import tempfile
            # Since target file might be big enough to exceed capacity of a global
            # temporary directory, create temp file side-by-side with the target file.
            fd, tmpfile = tempfile.mkstemp(prefix=self.file_prefix, dir=self.file_dir, suffix=fname)
            os.close(fd)
            try:
                fid = open(tmpfile, 'wb')
                try:
                    format.write_array(fid, np.asanyarray(val), allow_pickle=False, pickle_kwargs=None)
                    fid.close()
                    fid = None
                    self.zipf.write(tmpfile, arcname=fname)
                except IOError as exc:
                    raise IOError("Failed to write to %s: %s" % (tmpfile, exc))
                finally:
                    if fid:
                        fid.close()
            finally:
                os.remove(tmpfile)

def _save_dataarray(file, dataarray, compress=False, min_dims_coord_npy =2):#mostly copied from _savez in numpy\lib\npyio.py
    import zipfile

    if not hasattr(file, 'write'):
        file = os_fspath(file)
        if not file.endswith('.xar'):
            file = file + '.xar'
    if compress:
        compression = zipfile.ZIP_DEFLATED
    else:
        compression = zipfile.ZIP_STORED

    zipf = zip_file(file, mode="w", compression=compression)
    if dataarray.name is None:
        data_name = 'data'
    else:
        data_name = dataarray.name
    zipf.add_npy(data_name+'.npy',dataarray.values)

    d = dataarray.variable.to_dict(data=False)
    d['version'] = xr.__version__
    d.update({"coords": {}, "name": dataarray.name})
    for k in dataarray.coords:
        assert(k!=data_name)
        coord_var = dataarray.coords[k].variable
        item = {"attrs": decode_numpy_dict_values(coord_var.attrs), "dtype":str(coord_var.values.dtype)}# we save the type here
        if (coord_var.dims!=()) and( len(coord_var.dims)>1 or coord_var.dims[0]!=k): # we don't keep the dims if we have a dimension_coordinate or if dims is empty to keep the json more concise (see http://xarray.pydata.org/en/stable/data-structures.html#coordinates)
            item['dims'] = coord_var.dims
        if (coord_var.dims!=()) and len(coord_var.dims)>=min_dims_coord_npy:
            zipf.add_npy(k+'.npy',coord_var.values)
        else:
            item["data"] = ensure_us_time_resolution(coord_var.values)   # keeping coordinates data in the json
        d["coords"][k] = item

    json_str = json.dumps(d,cls=NumpyEncoder) + "\n"               # 2. string (i.e. JSON)
    json_bytes = json_str.encode('utf-8')
    zipf.add_bin_data('DataArray.json',json_bytes)
    zipf.close()

def _save_dataset(file, dataset, compress=False, min_dims_coord_npy = 2):#mostly copied from _savez in numpy\lib\npyio.py
    import zipfile

    if not hasattr(file, 'write'):
        file = os_fspath(file)
        if not file.endswith('.xar'):
            file = file + '.xar'

    if compress:
        compression = zipfile.ZIP_DEFLATED
    else:
        compression = zipfile.ZIP_STORED

    zipf = zip_file(file, mode="w", compression=compression)
    dataset_dict = dataset.to_dict(data = False)
    dataset_dict['version'] = xr.__version__
    for key, array in dict(dataset.data_vars).items():
            val = np.asanyarray(array.values)
            if val.ndim >= min_dims_coord_npy:
                zipf.add_npy('%s.npy'%key, val)
            else:
                dataset_dict['data_vars'][key]['data']=ensure_us_time_resolution(val)
    for key, array in dict(dataset.coords).items():
        val = np.asanyarray(array.values)
        if val.ndim >= min_dims_coord_npy:
            zipf.add_npy('%s.npy'%key, val)
        else:
            dataset_dict['coords'][key]['data']=ensure_us_time_resolution(val)
    json_str = json.dumps(dataset_dict,cls=NumpyEncoder) + "\n"
    json_bytes = json_str.encode('utf-8')
    zipf.add_bin_data('Dataset.json', json_bytes)
    zipf.close()

def load_npys(file):
    # TODO: Use contextlib.ExitStack once we drop Python 2
    if hasattr(file, 'read'):
        fid = file
        own_fid = False
    else:
        fid = open(os_fspath(file), "rb")
        own_fid = True

    if True:
        # Code to distinguish from NumPy binary files and pickles.
        _ZIP_PREFIX = b'PK\x03\x04'
        _ZIP_SUFFIX = b'PK\x05\x06' # empty zip files start with this
        N = len(format.MAGIC_PREFIX)
        magic = fid.read(N)
        # If the file size is less than N, we need to make sure not
        # to seek past the beginning of the file
        fid.seek(-min(N, len(magic)), 1)  # back-up
        if magic.startswith(_ZIP_PREFIX) or magic.startswith(_ZIP_SUFFIX):
            _zip = zip_file(fid)
            files = _zip.namelist()
            _data_dict={}
            _type = None
            for x in files:
                if x.endswith('.npy'):
                    bytes = _zip.open(x)
                    magic = bytes.read(len(format.MAGIC_PREFIX))
                    bytes.close()
                    assert( magic == format.MAGIC_PREFIX)
                    bytes = _zip.open(x)
                    _data_dict[x[:-4]] = format.read_array(bytes, allow_pickle=False, pickle_kwargs=None)
                elif x=='Dataset.json':
                    assert(_type is None)
                    _type = xr.Dataset
                    header = json.loads(_zip.read(x))
                elif x=='DataArray.json':
                    assert(_type is None)
                    _type = xr.DataArray
                    header = json.loads(_zip.read(x))
            if _type is None:
                raise IOError("Failed to read file")
            if _type ==  xr.DataArray:
                if 'name' in header and (header['name'] is not None):
                    data_name = header['name']
                else:
                    data_name = 'data'
                data = _data_dict[data_name]
                assert (data.dtype==header['dtype'])
                assert (data.shape==tuple(header['shape']))
                coords={}
                for k,coord in header['coords'].items():
                    if 'data' in coord:
                        coord_data = np.array(coord['data'],dtype=coord['dtype'])
                    else:
                        coord_data = _data_dict[k]
                    if 'dims' in coord:
                        dims=coord['dims']
                    elif coord_data.ndim==0:
                        dims=()
                    else:
                        dims= [k]
                    coords[k]=xr.DataArray(coord_data,dims=dims)
                return xr.DataArray(data, coords = coords, dims=header['dims'],attrs=header['attrs'],name=header['name'])
            else: # type is Dataset
                coords={}
                data_vars={}
                for k,d in header['coords'].items():
                    if 'data' in d:
                        data = np.array(d['data'],dtype=d['dtype'])
                    else:
                        data = _data_dict[k]
                    coords[k]=xr.DataArray(data, dims=d['dims'], attrs=d['attrs'])
                for k,d in header['data_vars'].items():
                    if  'data' in d:
                        data = np.array(d['data'],dtype=d['dtype'])
                    else:
                        data = _data_dict[k]
                    data_vars[k]=xr.DataArray(data, dims=d['dims'], attrs=d['attrs'])
                return xr.Dataset(data_vars, coords=coords,attrs=header['attrs'])
        else:
            raise IOError(
                    "Failed to interpret file %s as a zip" % repr(file))

    return None

def test():
    from xarray.testing import assert_identical
    data = np.random.rand(4, 3)

    locs = ['IA', 'IL', 'IN']
    times = pd.date_range('2000-01-01', periods=4)
    foo = xr.DataArray(data, coords=[times, locs], dims=['time', 'space'])
    v=foo.coords['time'].variable

    save_npys('foo',foo)

    foo_loaded = load_npys('foo.xar')
    assert_identical(foo,foo_loaded)

    temp = 15 + 8 * np.random.randn(2, 2, 3)
    lon = [[-99.83, -99.32], [-99.79, -99.23]]
    lat = [[42.25, 42.21], [42.63, 42.59]]
    da = xr.DataArray(temp,name='precipitations',dims=['x','y','time'],
                    coords={'long': (['x', 'y'], lon), 'lat': (['x', 'y'], lat), 'time': pd.date_range('2014-09-06', periods=3), 'reference_time': pd.Timestamp('2014-09-05')})
    save_npys('da',da)
    da_loaded=load_npys('da.xar')
    assert_identical(da,da_loaded)

    temp = 15 + 8 * np.random.randn(2, 2, 3)
    precip = 10 * np.random.rand(2, 2, 3)
    lon = [[-99.83, -99.32], [-99.79, -99.23]]
    lat = [[42.25, 42.21], [42.63, 42.59]]
    ds = xr.Dataset({'temperature'  : (['x', 'y', 'time'], temp),
                     'precipitation': (['x', 'y', 'time'], precip)},
                     coords={'long': (['x', 'y'], lon), 'lat': (['x', 'y'], lat), 'time': pd.date_range('2014-09-06', periods=3), 'reference_time': pd.Timestamp('2014-09-05')})

    save_npys('ds',ds,min_dims_coord_npy=1)
    ds_loaded= load_npys('ds.xar')
    assert_identical(ds, ds_loaded)

if __name__ == "__main__":
    test()


# MADx caller
#
#   --> New version that rewrites the madx file instead of calling strength files
#
#   Eugenio Senes
#   Created 14.02.2018
import numpy as np
import os
import multiprocessing

# golden values for the bump
bsw40              =    0.003498034648
bsw42              =    -0.01422287844
bsw43              =    0.01290524313
bsw44              =    -0.006001526439
BSS_max = 2*1.11828e-01 #from twiss

# build the parameter sweep lists
nstep = 50.

t = np.arange(0,nstep+1,1)/nstep * np.pi/2
t = np.append(t, t+np.pi/2)
# strengths for the simulation
#sextupole strength
BSS_val = BSS_max*np.cos(t)

#quadrupole strength
bsw40_val = bsw40*np.sin(t)
bsw42_val = bsw42*np.sin(t)
bsw43_val = bsw43*np.sin(t)
bsw44_val = bsw44*np.sin(t)

# this peice for with-sextupoles
with open('/afs/cern.ch/work/e/esenes/PS-injection-bump/madx_scan_parallel/runMAD/sweepBump_master.madx','r') as f:
    message = f.read()
    head = message[:1178]
    body1 = message[1178:1498] #up to just before the ptc_twiss
    body2 = message[1574:1667] # skip the ptc_twiss
    tail = message[1667:]
    f.close()

# what the workers will have to do
def myWorker(k): # k is the index of the for loop
    firstAdd = '\n\nBSStren := ' + str(BSS_val[k]) + ';\n' + 'bsw40 := ' + str(bsw40_val[k]) + ';\n' + 'bsw42 := ' + str(bsw42_val[k]) + ';\n' + 'bsw43 := ' + str(bsw43_val[k]) + ';\n'  + 'bsw44 := ' + str(bsw44_val[k]) + ';\n'

    customTwiss = '\nptc_twiss,closed_orbit,icase=56,no=4,summary_table=ptc_twiss_summary,file='+str(k)+'.twiss;\n'
    assignRow = 'Assign, echo='+str(k)+'.out;\n'
    # recompose the script adding the desired lines
    newMessage = head+firstAdd+body1+customTwiss+body2+assignRow+tail

    # create the madx file and run it
    with open('sweepBump'+str(k)+'.madx','w') as f:
        f.write(newMessage)
        f.close()
    os.system("chmod u+x ./sweepBump"+str(k)+".madx")
    os.system("/afs/cern.ch/eng/sl/MAD-X/pro/releases/5.02.00/madx-linux64 < sweepBump"+str(k)+".madx")

# la ciccia sta qui
jobs = []

for k in range(len(bsw40_val)):

    p = multiprocessing.Process(target=myWorker, args=(k,))
    jobs.append(p)
    p.start()