#! /usr/bin/env python
import argparse
import re
import numpy as np

def to_binary_string(value):
    """
    Converts F or L to zeros, and B or R to 1 and interprets the string as a binary value

    >>> to_binary_string("FBFBBFF")
    ('0101100', 44)

    >>> to_binary_string("RLR")
    ('101', 5)

    :param value:
    :return:
    """
    value = re.sub(r"[FL]", "0", value)
    value = re.sub(r"[BR]", "1", value)
    return value, int(value, 2)

def get_seat_id(value):
    """
    Splits the string into row and column parts and interprets each as binary locations. Then
    multiplies the row by 8 and adds the column.

    >>> get_seat_id("FBFBBFFRLR")
    357
    >>> get_seat_id("BFFFBBFRRR")
    567
    >>> get_seat_id("FFFBBBFRRR")
    119
    >>> get_seat_id("BBFFBBFRLL")
    820

    :param value:
    :return:
    """
    row = to_binary_string(value[:7])
    col = to_binary_string(value[7:])
    return row[1]*8 + col[1]

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description='Day 5 of Advent of Code 2020')
    parser.add_argument('file', metavar='filename', type=argparse.FileType('rt'),
                        help='filename to your personal inputs')
    parser.add_argument('--test', '-t', action='store_true')

    args = parser.parse_args()

    if args.test:
        import doctest

        doctest.testmod()

        print("Tests completed")
        exit(0)

    with args.file as FILE:
        file_content = FILE.readlines()

    seat_ids = [get_seat_id(line) for line in file_content]

    print(f"There are {len(seat_ids)} boardings cards in the the input, and the highest value is {np.max(seat_ids)}")

    for v in range(0, np.max(seat_ids)):
        if v not in seat_ids and v-1 in seat_ids and v+1 in seat_ids:
            print(f"The value {v} is not in the list, but {v-1} and {v+1} are")


import py
from rpython.flowspace.model import Constant
from rpython.rtyper.lltypesystem import lltype
from rpython.jit.codewriter.flatten import SSARepr, Label, TLabel, Register
from rpython.jit.codewriter.flatten import ListOfKind, IndirectCallTargets
from rpython.jit.codewriter.jitcode import SwitchDictDescr
from rpython.jit.metainterp.history import AbstractDescr

def format_assembler(ssarepr):
    """For testing: format a SSARepr as a multiline string."""
    from cStringIO import StringIO

    def repr(x):
        if isinstance(x, Register):
            return '%%%s%d' % (x.kind[0], x.index)    # e.g. %i1 or %r2 or %f3
        elif isinstance(x, Constant):
            if (isinstance(x.concretetype, lltype.Ptr) and
                isinstance(x.concretetype.TO, lltype.Struct)):
                return '$<* struct %s>' % (x.concretetype.TO._name,)
            return '$%r' % (x.value,)
        elif isinstance(x, TLabel):
            return getlabelname(x)
        elif isinstance(x, ListOfKind):
            return '%s[%s]' % (x.kind[0].upper(), ', '.join(map(repr, x)))
        elif isinstance(x, SwitchDictDescr):
            return '<SwitchDictDescr %s>' % (
                ', '.join(['%s:%s' % (key, getlabelname(lbl))
                           for key, lbl in x._labels]))
        elif isinstance(x, (AbstractDescr, IndirectCallTargets)):
            return '%r' % (x,)
        else:
            return '<unknown object: %r>' % (x,)

    seenlabels = {}
    for asm in ssarepr.insns:
        for x in asm:
            if isinstance(x, TLabel):
                seenlabels[x.name] = -1
            elif isinstance(x, SwitchDictDescr):
                for _, switch in x._labels:
                    seenlabels[switch.name] = -1
    labelcount = [0]
    def getlabelname(lbl):
        if seenlabels[lbl.name] == -1:
            labelcount[0] += 1
            seenlabels[lbl.name] = labelcount[0]
        return 'L%d' % seenlabels[lbl.name]

    output = StringIO()
    insns = ssarepr.insns
    if insns and insns[-1] == ('---',):
        insns = insns[:-1]
    for i, asm in enumerate(insns):
        if ssarepr._insns_pos:
            prefix = '%4d  ' % ssarepr._insns_pos[i]
        else:
            prefix = ''
        if isinstance(asm[0], Label):
            if asm[0].name in seenlabels:
                print >> output, prefix + '%s:' % getlabelname(asm[0])
        else:
            print >> output, prefix + asm[0],
            if len(asm) > 1:
                if asm[-2] == '->':
                    if len(asm) == 3:
                        print >> output, '->', repr(asm[-1])
                    else:
                        lst = map(repr, asm[1:-2])
                        print >> output, ', '.join(lst), '->', repr(asm[-1])
                else:
                    lst = map(repr, asm[1:])
                    if asm[0] == '-live-': lst.sort()
                    print >> output, ', '.join(lst)
            else:
                print >> output
    res = output.getvalue()
    return res

def assert_format(ssarepr, expected):
    asm = format_assembler(ssarepr)
    if expected != '':
        expected = str(py.code.Source(expected)).strip() + '\n'
    asmlines = asm.split("\n")
    explines = expected.split("\n")
    for asm, exp in zip(asmlines, explines):
        if asm != exp:
            msg = [""]
            msg.append("Got:      " + asm)
            msg.append("Expected: " + exp)
            lgt = 0
            for i in range(min(len(asm), len(exp))):
                if exp[i] == asm[i]:
                    lgt += 1
                else:
                    break
            msg.append("          " + " " * lgt + "^^^^")
            raise AssertionError('\n'.join(msg))
    assert len(asmlines) == len(explines)

def unformat_assembler(text, registers=None):
    # XXX limited to simple assembler right now
    #
    def unformat_arg(s):
        if s.endswith(','):
            s = s[:-1].rstrip()
        if s[0] == '%':
            try:
                return registers[s]
            except KeyError:
                num = int(s[2:])
                if s[1] == 'i': reg = Register('int', num)
                elif s[1] == 'r': reg = Register('ref', num)
                elif s[1] == 'f': reg = Register('float', num)
                else: raise AssertionError("bad register type")
                registers[s] = reg
                return reg
        elif s[0] == '$':
            intvalue = int(s[1:])
            return Constant(intvalue, lltype.Signed)
        elif s[0] == 'L':
            return TLabel(s)
        elif s[0] in 'IRF' and s[1] == '[' and s[-1] == ']':
            items = split_words(s[2:-1])
            items = map(unformat_arg, items)
            return ListOfKind({'I': 'int', 'R': 'ref', 'F': 'float'}[s[0]],
                              items)
        elif s.startswith('<SwitchDictDescr '):
            assert s.endswith('>')
            switchdict = SwitchDictDescr()
            switchdict._labels = []
            items = split_words(s[len('<SwitchDictDescr '):-1])
            for item in items:
                key, value = item.split(':')
                value = value.rstrip(',')
                switchdict._labels.append((int(key), TLabel(value)))
            return switchdict
        else:
            raise AssertionError("unsupported argument: %r" % (s,))
    #
    if registers is None:
        registers = {}
    ssarepr = SSARepr('test')
    for line in text.splitlines():
        line = line.strip()
        if not line:
            continue
        if line.startswith('L') and line.endswith(':'):
            ssarepr.insns.append((Label(line[:-1]),))
        else:
            try:
                opname, line = line.split(None, 1)
            except ValueError:
                opname, line = line, ''
            words = list(split_words(line))
            if '->' in words:
                assert words.index('->') == len(words) - 2
                extra = ['->', unformat_arg(words[-1])]
                del words[-2:]
            else:
                extra = []
            insn = [opname] + [unformat_arg(s) for s in words] + extra
            ssarepr.insns.append(tuple(insn))
    return ssarepr

def split_words(line):
    word = ''
    nested = 0
    for i, c in enumerate(line):
        if c == ' ' and nested == 0:
            if word:
                yield word
                word = ''
        else:
            word += c
            if c in '<([':
                nested += 1
            if c in '])>' and ('  '+line)[i:i+4] != ' -> ':
                nested -= 1
                assert nested >= 0
    if word:
        yield word
    assert nested == 0


import pandas as pd
from mega_analysis.crosstab.gif_sheet_names import gif_sheet_names
from mega_analysis.crosstab.file_paths import file_paths

def gif_lobes_from_excel_sheets():
    """
    sort the gif parcellations as per excel gif sheet lobes.
    e.g. GIF FL = GIF Frontal Lobe - has a list of gif parcellations
    which we want to see in 3D slicer, using the GUI
    """
    _, _, excel_path, _ = file_paths()
    GIF_SHEET_NAMES = gif_sheet_names()

    lobes_mapping = {}

    for gif_lobe in GIF_SHEET_NAMES:
        gif_parcellations = pd.read_excel(
            excel_path,
            header=None, usecols="A:B",
            sheet_name=gif_lobe, engine="openpyxl",
        )
        gif_parcellations.dropna(axis=0, how='any', inplace=True)
        gif_parcellations.dropna(axis=1, how='all', inplace=True)
        gifs = gif_parcellations.astype({1: 'uint16'})
        gifs = gifs.iloc[:, 1]
        lobes_mapping[gif_lobe] = gifs.values

    return lobes_mapping


import torch
from torchtext.datasets import AG_NEWS
from torchtext.data.utils import get_tokenizer
from torchtext.vocab import build_vocab_from_iterator
from torch.utils.data import DataLoader

UNK = '<unk>'

tok = get_tokenizer('basic_english')
train_iter = AG_NEWS(split='train')

def yield_tokens(data_iter, tokenizer):
    for _, text in data_iter:
        yield tokenizer(text)

vocab = build_vocab_from_iterator(yield_tokens(train_iter, tok), specials=['<unk>'])
vocab.set_default_index(vocab['<unk>'])

# print(vocab['my'])
# print(vocab(['this', 'is', 'my', 'car']))
# print(vocab['<unk>']) #0

text_pipeline = lambda x: vocab(tok(x))
label_pipeline = lambda x: int(x) - 1

# print(text_pipeline('this is my car'))
# print(vocab(['this is my car']))
# print(tok('this is my car'))

device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

def collate_batch(batch):
    label_list, text_list, offsets = [], [], [0]
    for (_label, _text) in batch:
        label_list.append(label_pipeline(_label)) # 0 ~ 3
        processed_text = torch.tensor(text_pipeline(_text), dtype=torch.int64) # int list to tensor
        text_list.append(processed_text) # tensor of int-list-tensor
        offsets.append(processed_text.size(0))
    label_list = torch.tensor(label_list, dtype=torch.int64)
    offsets = torch.tensor(offsets[:-1]).cumsum(dim=0)
    text_list = torch.cat(text_list)
    return label_list.to(device), text_list.to(device), offsets.to(device)

dataloader = DataLoader(train_iter, batch_size=8, shuffle=False, collate_fn=collate_batch)

collate_batch(train_iter)


import shutil

executables = [
    "flex",
    "yacc",
]

def is_available(exe: str) -> bool:
    return shutil.which(exe) is not None

def test_executables() -> None:
    missing = [exe
               for exe in executables
               if not is_available(exe)]
    assert not missing, f"Missing executables: {', '.join(missing)}"


import argparse
import numpy as np

from Data import Data
from Experiment import Experiment
from FrameStackExperiment import FrameStackExperiment

if __name__=='__main__':
    parser = argparse.ArgumentParser(description='Evaluate termination classifier performance')

    parser.add_argument('filepath', type=str, help='filepath of pkl file containing trajectories with RAM states and frames')
    parser.add_argument('dest', type=str, help='directory to write results and plots to')
    parser.add_argument('term_classifier', type=str, choices=['OneClassSVM', 'TwoClassSVM', 'FullCNN'], help='termination classifier to be used')
    parser.add_argument('feature_extractor', type=str, choices=['RawImage', 'DownsampleImage', 'RawRAM', 'MonteRAMState', 'MonteRAMXY', 'BOVW', 'RND', 'CNN'], help='feature extractor to be used')
    parser.add_argument('label_extractor', type=str, choices=['BeforeAfterExtractor', 'AfterExtractor', 'OracleExtractor', 'TransductiveExtractor', 'PositiveAugmentExtractor'], help='label extractor to be used')
    parser.add_argument('--extract_only_pos', default=False, action='store_true', help='whether label extractor should only extract positive egs')
    parser.add_argument('--frame_stack', default=False, action='store_true', help='whether states are frame stacks')

    args = parser.parse_args()

    #data = Data(args.filepath, train_skip=2000, train_num=200, test_skip=0, test_num=100)
    data = Data(args.filepath, train_skip=25, train_num=75, test_skip=25, test_num=25)

    # (player_x, player_y, screen) of good subgoals
    # [right plat, bottom of ladder of right plat, bottom of ladder of left plat,
    #  top of ladder of left plat, key, left door, right door]
    #subgoals = [(133, 192, 1), (132, 148, 1), (20, 148, 1), (20, 192, 1), (13, 198, 1), (24, 235, 1), (130, 235, 1)]
    #subgoals = [(24, 235, 1), (130, 235, 1)]
    #subgoals = [(52, 235, 1)]
    subgoals = [(133, 148, 1), (58, 192, 1), (35, 235, 1), (119, 235, 1), (49, 235, 1), (88, 192, 1), (142, 192, 1)]

    # Prepare hyperparams
    if args.label_extractor == 'OracleExtractor':
        window_sz_hyperparms = [None]
    else:
        #window_sz_hyperparms = range(0, 7)
        #window_sz_hyperparms = range(2, 3)
        window_sz_hyperparms = range(1, 2)

    if args.feature_extractor == 'BOVW':
        #num_clusters_hyperparams = range(110, 121, 10)
        #num_sift_keypoints_hyperparams = range(25, 40, 5)
        num_clusters_hyperparams = range(110, 111, 10)
        num_sift_keypoints_hyperparams = range(25, 26, 5)
    else:
        num_clusters_hyperparams = [None]
        num_sift_keypoints_hyperparams = [None]

    if args.term_classifier == 'OneClassSVM':
        nu_hyperparams = np.arange(0.3, 0.5, 0.1)
    else:
        nu_hyperparams = [None]

    if args.term_classifier == 'FullCNN':
        gamma_hyperparams = [None]
    else:
        #gamma_hyperparams = [0.0001, 0.001, 0.01, 0.1, 'scale', 'auto']
        #gamma_hyperparams = [0.001, 0.01, 'auto']
        #gamma_hyperparams = [0.001]
        #gamma_hyperparams = [0.1]
        #gamma_hyperparams = [0.1, 'auto']
        #gamma_hyperparams = ['scale']
        #gamma_hyperparams = [0.000001]
        gamma_hyperparams = [0.000000004]

    # Prepare information on each subgoal
    subgoals_info = {}
    for subgoal in subgoals:
        traj_idx, state_idx = data.find_first_instance(data.train_ram_trajs, subgoal)
        if traj_idx is None:
            continue

        subgoal_ram = data.train_ram_trajs[traj_idx][state_idx]
        ground_truth_idxs = data.filter_in_term_set(data.test_ram_trajs, subgoal_ram)
        subgoals_info[subgoal] = {'traj_idx': traj_idx,
                                  'state_idx': state_idx,
                                  'ground_truth_idxs': ground_truth_idxs}

    # Run experiments
    for num_clusters in num_clusters_hyperparams:
        for num_sift_keypoints in num_sift_keypoints_hyperparams:
            for window_sz in window_sz_hyperparms:
                for nu in nu_hyperparams:
                    for gamma in gamma_hyperparams:
                        for i in range(1):
                            print(f"[+] clusters={num_clusters}, kps={num_sift_keypoints}, window_sz={window_sz}, nu={nu}, gamma={gamma}")

                            if args.feature_extractor in ['RawImage', 'DownsampleImage', 'BOVW', 'RND', 'CNN'] or args.term_classifier == 'FullCNN':
                                train_trajs = data.train_frame_trajs
                                test_trajs = data.test_frame_trajs
                            elif args.feature_extractor in ['RawRAM', 'MonteRAMState', 'MonteRAMXY']:
                                train_trajs = data.train_raw_ram_trajs
                                test_trajs = data.test_raw_ram_trajs

                            # Run experiment
                            hyperparams = {
                                "num_sift_keypoints": num_sift_keypoints,
                                "num_clusters": num_clusters,
                                "window_sz": window_sz,
                                "nu": nu,
                                "gamma": gamma,
                            }

                            if args.frame_stack:
                                experiment = FrameStackExperiment(train_trajs, data.train_raw_ram_trajs, test_trajs, data.test_raw_ram_trajs,
                                                    subgoals, subgoals_info,
                                                    args, hyperparams)
                            else:
                                experiment = Experiment(train_trajs, data.train_raw_ram_trajs, test_trajs, data.test_raw_ram_trajs,
                                                    subgoals, subgoals_info,
                                                    args, hyperparams)
                            experiment.run()


import ctypes
import typing as t

from . import sdk
from .enum import Result
from .event import bind_events
from .exception import get_exception
from .model import UserAchievement

class AchievementManager:
    _internal: sdk.IDiscordAchievementManager = None
    _garbage: t.List[t.Any]
    _events: sdk.IDiscordAchievementEvents

    def __init__(self):
        self._garbage = []
        self._events = bind_events(
            sdk.IDiscordAchievementEvents,
            self._on_user_achievement_update
        )

    def _on_user_achievement_update(self, event_data, user_achievement):
        self.on_user_achievement_update(UserAchievement(copy=user_achievement.contents))

    def set_user_achievement(
        self,
        achievement_id: int,
        percent_complete: int,
        callback: t.Callable[[Result], None]
    ) -> None:
        """
        Updates the current user's status for a given achievement.

        Returns discordsdk.enum.Result via callback.
        """
        def c_callback(callback_data, result):
            self._garbage.remove(c_callback)
            result = Result(result)
            callback(result)

        c_callback = self._internal.set_user_achievement.argtypes[-1](c_callback)
        self._garbage.append(c_callback)  # prevent it from being garbage collected

        self._internal.set_user_achievement(
            self._internal,
            achievement_id,
            percent_complete,
            ctypes.c_void_p(),
            c_callback
        )

    def fetch_user_achievements(self, callback: t.Callable[[Result], None]) -> None:
        """
        Loads a stable list of the current user's achievements to iterate over.

        Returns discordsdk.enum.Result via callback.
        """
        def c_callback(callback_data, result):
            self._garbage.remove(c_callback)
            result = Result(result)
            callback(result)

        c_callback = self._internal.fetch_user_achievements.argtypes[-1](c_callback)
        self._garbage.append(c_callback)  # prevent it from being garbage collected

        self._internal.fetch_user_achievements(self._internal, ctypes.c_void_p(), c_callback)

    def count_user_achievements(self) -> int:
        """
        Counts the list of a user's achievements for iteration.
        """
        count = ctypes.c_int32()
        self._internal.count_user_achievements(self._internal, count)
        return count.value

    def get_user_achievement_at(self, index: int) -> UserAchievement:
        """
        Gets the user's achievement at a given index of their list of achievements.
        """
        achievement = sdk.DiscordUserAchievement()
        result = Result(self._internal.get_user_achievement_at(
            self._internal,
            index,
            achievement
        ))
        if result != Result.ok:
            raise get_exception(result)

        return UserAchievement(internal=achievement)

    def get_user_achievement(self, achievement_id: int) -> None:
        """
        Gets the user achievement for the given achievement id.
        """
        achievement = sdk.DiscordUserAchievement()
        result = Result(self._internal.get_user_achievement(
            self._internal,
            achievement_id,
            achievement
        ))
        if result != Result.ok:
            raise get_exception(result)

        return UserAchievement(internal=achievement)

    def on_user_achievement_update(self, achievement: UserAchievement) -> None:
        """
        Fires when an achievement is updated for the currently connected user
        """


# -*- coding: utf-8 -*-
# Time       : 2023/10/5 22:53
# Author     : QIN2DIM
# GitHub     : https://github.com/QIN2DIM
# Description:
import csv
from dataclasses import dataclass, field
from pathlib import Path
from typing import List

class Level:
    first = 1
    second = 2
    third = 3
    fourth = 4
    fifth = 5
    sixth = 6
    none = 0

    @staticmethod
    def get_bonus(level: int):
        level2bonus = {0: 0, 6: 5, 5: 10, 4: 200, 3: 3000, 2: -1, 1: -1}
        return level2bonus[level]

    @staticmethod
    def get_zh_level(level: int):
        level2zh = {0: "无", 6: "六等奖", 5: "五等奖", 4: "四等奖", 3: "三等奖", 2: "二等奖", 1: "一等奖"}
        return level2zh[level]

def is_bingo(red: int, blue: int):
    assert 0 <= red <= 6
    assert 0 <= blue <= 1

    if blue == 0:
        if red in [0, 1, 2, 3]:
            return Level.none
        if red == 4:
            return Level.fifth
        if red == 5:
            return Level.fourth
        if red == 6:
            return Level.second
    if blue == 1:
        if red in [0, 1, 2]:
            return Level.sixth
        if red == 3:
            return Level.fifth
        if red == 4:
            return Level.fourth
        if red == 5:
            return Level.third
        if red == 6:
            return Level.first

def compare_nums(mc: List[str], bingo_nums: List[str]):
    red = 0
    blue = 0
    for num_red in mc[:-1]:
        if num_red in bingo_nums[:-1]:
            red += 1
    if mc[-1] == bingo_nums[-1]:
        blue += 1

    return red, blue

@dataclass
class SSQResult:
    red: int = field(default=int)
    blue: int = field(default=int)
    level: int = field(default=int)
    bonus: int = field(default=int)
    zh_level: str = field(default=str)
    term: str = field(default=str)

class SSQNumsChecker:
    def __init__(self, my_nums: List[List[str]]):
        self.my_nums = my_nums

    def get_results(self, bingo_nums: List[str]):
        for i, mc in enumerate(self.my_nums):
            red, blue = compare_nums(mc, bingo_nums)
            level = is_bingo(red, blue)
            yield mc, SSQResult(
                red=red,
                blue=blue,
                level=level,
                bonus=Level.get_bonus(level),
                zh_level=Level.get_zh_level(level),
            )

    def trace_results(self, cache_path: Path):
        text = cache_path.read_text(encoding="utf8")
        reader = csv.reader([k for k in text.split("\n")[1:] if k])

        for j, tn in enumerate(reader):
            term = tn[0]
            bingo_nums = tn[1:]
            for i, mc in enumerate(self.my_nums):
                red, blue = compare_nums(mc, bingo_nums)
                level = is_bingo(red, blue)
                yield mc, SSQResult(
                    red=red,
                    blue=blue,
                    level=level,
                    bonus=Level.get_bonus(level),
                    zh_level=Level.get_zh_level(level),
                    term=term,
                )


import sys
import os
import string

def int_input():
    string = input()
    if not string:
        return (-10)
    elif (len(string) == 1) and (string[0] == '0'):
        return (0)
    else:
        try:
            numb = int(string)
            return (numb)
        except ValueError:
            return (-10)

psql = 'psql '
location = '-h localhost '
db_name = 'test_db '
user = 'test_user '

team=''

main_query = psql + location + db_name + user

'''
    list of standard queries
    they will be called  from func 'out_std_queries'
'''

std_queries = {
    1 : '../sql/std_queries/out_devices.sql',
    2 : '../sql/std_queries/out_employees.sql',
    3 : '../sql/std_queries/out_instrument.sql',
    4 : '../sql/std_queries/out_laptops.sql',
    5 : '../sql/std_queries/out_printers.sql',
    6 : '../sql/std_queries/out_projects.sql',
    7 : '../sql/std_queries/out_team_info.sql',
    8 : '../sql/std_queries/out_team_members.sql'
}

'''
    list of special queries
    they will be called  from func 'out_spec_queries'

    WARNING: printer query must added with parameters ':palette' and ':format'
'''

spec_queries = {
    1 : '''psql -h localhost test_db test_user -f ../sql/spec_queries/out_my_team_members.sql -v team_name="\'''',
    2 : '''psql -h localhost test_db test_user -f ../sql/spec_queries/out_next_deadlines.sql -v team_name="\'''',
    3 : '''psql -h localhost test_db test_user -f ../sql/spec_queries/out_all_team_projects.sql -v team_name="\'''',
    4 : '''psql -h localhost test_db test_user -f ../sql/spec_queries/out_free_laptops.sql''',
    5 : '''psql -h localhost test_db test_user -f ../sql/spec_queries/out_free_instrument.sql''',
    6 : '''psql -h localhost test_db test_user -f ../sql/spec_queries/out_var_printers.sql '''
}

'''
    This function get users query-choice between all standard queries
'''

def out_std_queries():
    os.system('clear')
    print(
'''   list of tables
-------------------------
 1 | devices
 2 | employees
 3 | instrument
 4 | laptops
 5 | printers
 6 | projects
 7 | team_info
 8 | team_members

 print '9' for exit
 print '-1' for exit from this menu
    ''')
    choice = -10
    while  choice == -10:
        choice = int_input()
        if (choice == -10):
            print('''Invalid Number
 Press ENTER to coninue''')
            input()
    if choice == 9:
        raise SystemExit
    if choice == -1:
        return
    if (choice < 1) or (choice > 8):
        print('''There no choice like this
 Press ENTER to coninue''')
        input()
        out_std_queries()
        return
    minor_query = main_query + '-f ' + std_queries[choice]
    os.system(minor_query)
    print(''' Press ENTER to coninue''')
    input()
    out_std_queries()

palette = {
    'RGB',
    'CMYK',
    'WB',
    'other'
}

paper_format={
    'A1',
    'A2',
    'A3',
    'A4',
    'A5',
    'custom'
}

'''
    This function get users choices about printers paper format and color palette
'''

def out_print():
    os.system('clear')
    print(''' Enter the paper format (A1, A2, A3, A4, A5, custom):''')
    format = input()
    if (format not in paper_format):
        print ("    There is no paper like this\n Press ENTER to coninue")
        input()
        return

    print(''' Enter the color palette (RGB, CMYK, WB, other):''')
    palette = input()
    if (palette not in palette):
        print ("    There is no palette like this\n Press ENTER to coninue")
        input()
        return
    minor_query = spec_queries[6] + '-v format="\'' + format + '\'"' + ' -v palette="\'' + palette + '\'"'
    print(minor_query)
    os.system(minor_query)
    print(''' Press ENTER to coninue''')
    input()

'''
    This function get users query-choice between all special queries
    If user choose printer-query, this function call out_print function
'''

def out_spec_queries():
    os.system('clear')
    print(
'''   list of special queries
-----------------------------------------------------
 1 | information about your teammates
 2 | information about next deadlines of your team
 3 | information about all projects of your team
 4 | information about free laptops, you can use
 5 | information about free instruments, you can use
 6 | information about printers in office

 print '9' for exit
 print '-1' for exit from this menu
    ''')
    choice = -10
    while  choice == -10:
        choice = int_input()
        if (choice == -10):
            print('''Invalid Number
 Press ENTER to coninue''')
            input()
    if choice == 9:
        raise SystemExit
    if choice == -1:
        return
    if (choice < 1) or (choice > 6):
        print('''There no choice like this
 Press ENTER to coninue''')
        input()
    else:
        if choice == 6:
            out_print()
        else :
            minor_query = spec_queries[choice]
            os.system(minor_query)
            print(''' Press ENTER to coninue''')
            input()
    out_spec_queries()

'''
    This function get users query-choice between std and spec queries (look in the head of document)
'''

def main_choice():
    os.system('clear')
    print('''   Choose queries you want to display:
-------------------------
 1 | standard queries (all information without redaction)
 2 | special queries (special redacted information)

 print '9' for exit
''')
    choice = -10
    while  choice == -10:
        choice = int_input()
        if (choice == -10):
            print('''Invalid Number
 Press ENTER to coninue''')
            input()
    if choice == 9:
        raise SystemExit
    if (choice < 1) or (choice > 2):
        print('''There no choice like this
 Press ENTER to coninue''')
        input()
        main_choice()
        return
    if choice == 1:
        out_std_queries()
    if choice == 2:
        out_spec_queries()
    main_choice()

'''
    This function get name of team, check it, fill call of sql-query and call main_choice function
'''

def main_info():
    os.system('psql -h localhost test_db test_user -f ../sql/std_queries/out_team_info.sql > .team_base')
    os.system('clear')
    f = open('.team_base')
    all_file = f.read()
    f.close()
    print(all_file)
    print(''' Enter your team''')
    team = input()
    if all_file.find('| ' + team + '''
''') == -1:
        print('ERROR: There is no team with this name')
        exit()
#adding team_name to sql_scripts
    spec_queries[1] +=  team + '\'"'
    spec_queries[2] +=  team + '\'"'
    spec_queries[3] +=  team + '\'"'
    main_choice()

main_info()


import json, requests, subprocess, sys, yaml

from pip._vendor.distlib.compat import raw_input

class JiraClient():
    board_status_to_env = {"Ready to Deploy": "QAX",
                           "QAX Done": "STGX",
                           "StgX Done": "PROD-EU",
                           "Prod EU Done": "PROD-US",
                           "Prod US Done": "UNKNOWN"}

    env_to_status_id = {"QAX": "21", "STGX": "31", "PROD-EU": "41", "PROD-US": "51"}

    def __init__(self, token):
        self.auth_token = token

    def build_headers(self):
        return {
            'Content-Type': "application/json",
            'Authorization': "Basic " + self.auth_token,
            'Cache-Control': "no-cache"
        }

    def fetch_tickets_to_deploy(self):
        payload = {
            "jql": "project = MBI AND issuetype = Story AND status in (\"Ready to Deploy\", \"StgX Done\", \"QAX Done\", \"Prod EU Done\")",
            "fields": ["summary"]}
        headers = self.build_headers()
        url = 'https://www.mulesoft.org/jira/rest/api/2/search'
        r = requests.post(url, data=json.dumps(payload), headers=headers)

        # Filter only the tickets required for the current deploy date...
        issues = r.json()['issues']
        return list(map(lambda x: x["key"], issues))

    def fetch_ticket_info(self, id):
        headers = self.build_headers()
        url = 'https://www.mulesoft.org/jira/rest/api/2/issue/' + id
        r = requests.get(url, headers=headers)
        return r.json()

    def fetch_subtask_from_id(self, id):
        ticket_info = self.fetch_ticket_info(id)

        subtasks_ids = ticket_info['fields']["subtasks"]
        return list(map(lambda x: x["key"], subtasks_ids))

    def fetch_artifact_from_info(self, sid):
        ticket_info = self.fetch_ticket_info(sid)
        comp = ticket_info["fields"]["components"]

        # Fetch component ...
        if len(comp) == 0:
            raise ValueError(sid + " must have component defined")
        artifact_id = comp[0]["name"]

        # Fetch version ...
        version = ticket_info["fields"]["versions"]
        if len(version) == 0:
            raise ValueError(sid + " must have version defined")
        artifact_version = version[0]["name"]

        if len(comp) == 0:
            raise ValueError(sid + " must have version defined")

        jira_key = ticket_info["key"]

        return {"jira_key": jira_key, "artifact_id": artifact_id, "version": artifact_version}

    def fetch_artifacts(self, date):
        # Fetch all the events ...
        all_stories_keys = self.fetch_tickets_to_deploy()

        # Filter events to be deployed ...
        story_keys = list(
            filter(lambda id: self.fetch_ticket_info(id)["fields"]["customfield_13861"] == date, all_stories_keys))

        # Fetch the first ticket to be deployed ...
        return list(map(lambda sid: self.story_to_deployment_unit(sid), story_keys))

    def story_to_deployment_unit(self, story_key):
        subtask_ids = self.fetch_subtask_from_id(story_key)

        # Fetch artifact version ...
        artifacts = list(map(lambda x: self.fetch_artifact_from_info(x), subtask_ids))

        # Fetch next environment ...
        next_env = self.fetch_next_env_to_deploy(story_key)

        return {"jira_key": story_key, "next_env_to_deploy": next_env, "artifacts": artifacts}

    def fetch_ticket_status(self, id):
        # Fetch ticket info ...
        ticket_info = self.fetch_ticket_info(id)
        # Get current state ...
        status = ticket_info["fields"]["status"]["name"]
        return status

    def fetch_next_env_to_deploy(self, sid):
        board_status = self.fetch_ticket_status(sid)
        return self.board_status_to_env.get(board_status)

    def fetch_stories(self):
        payload = {
            "jql": "project = MBI AND issuetype = Story",
            "fields": ["summary"]}
        headers = self.build_headers()
        url = 'https://www.mulesoft.org/jira/rest/api/2/search'
        r = requests.post(url, data=json.dumps(payload), headers=headers)

        # Filter only the tickets required for the current deploy date...
        issues = r.json()['issues']
        return list(map(lambda x: x["key"], issues))

    def move_next_stage(self, sid):
        # Fetch ticket status ...
        board_status = self.fetch_ticket_status(sid)
        print(board_status)

        next_status = self.board_status_to_env[board_status]

        # Move ticket to a new status ...
        status_id = self.env_to_status_id[next_status]

        payload = {
            "update": {
                "comment": [
                    {
                        "add": {
                            "body": "Automatic flow transitioning based on flow"
                        }
                    }
                ]
            },
            "transition": {
                "id": status_id
            }
        }

        headers = self.build_headers()
        url = 'https://www.mulesoft.org/jira/rest/api/2/issue/' + sid + '/transitions'

        # Move to next status ...
        requests.post(url, data=json.dumps(payload), headers=headers)

    def description_commit(self):
        pull = "git pull"
        diff = "git diff HEAD^ HEAD"
        processPull = subprocess.Popen(pull.split(), stdout=subprocess.PIPE)
        output, error = processPull.communicate()
        if (error is None):
            processDiff = subprocess.Popen(diff.split(), stdout=subprocess.PIPE)
            output, error = processDiff.communicate()
            if (error is None):
                return str(output.decode("utf-8"))
            else:
                return "error"
        else:
            return "error"

    def get_tag(self):
        tag = "git describe --tag"
        processPull = subprocess.Popen(tag.split(), stdout=subprocess.PIPE)
        output, error = processPull.communicate()
        if (error is None):
            return str(output.decode("utf-8"))

    def cli_mbi(self):
        project = input("Enter the project initials: ")
        description = self.description_commit()
        print("You have the followings MBI:")
        print(self.fetch_stories())
        issue = input("Enter MBI: ")
        version = input("The last version is " + self.get_tag()+ ". Enter Version:")
        component = self.find_component()
        if self.validate_input(project, issue, version, component):
            self.create_subtask(project, issue, description, component, version)
        else:
            print("Exit")

    def create_subtask(self, project, issue, description, component, version):
        payload = {
            "fields":
                {
                    "project":
                        {
                            "key": project
                        },
                    "parent":
                        {
                            "key": issue
                        },
                    "summary": "Change log " + issue,
                    "description": description,
                    "issuetype":
                        {
                            "name": "Sub-task"
                        },
                    "components": [
                        {
                            "name": component
                        }
                    ],
                    "versions": [
                        {
                            "name": version
                        }
                    ]
                }
        }
        headers = self.build_headers()
        url = 'https://www.mulesoft.org/jira/rest/api/2/issue/'
        try:
            r = requests.post(url, data=json.dumps(payload), headers=headers)
            resp = r.content.decode('utf-8')
            jiraKey = json.loads(resp)
            print("Issue created: " + jiraKey["key"])
        except r.exceptions.HTTPError as err:
            print(err)

    def validate_input(self, project, mbi, version, component):
        question1 = "Project: " + project + " \nMBI: " + mbi + "\nVersion: " + version + "\nComponent: " + component + "\nIt's correct? "
        return self.query_yes_no(question1)

    def query_yes_no(self, question, default="yes"):
        """Ask a yes/no question via raw_input() and return their answer.

        "question" is a string that is presented to the user.
        "default" is the presumed answer if the user just hits <Enter>.
            It must be "yes" (the default), "no" or None (meaning
            an answer is required of the user).

        The "answer" return value is True for "yes" or False for "no".
        """
        valid = {"yes": True, "y": True, "ye": True,
                 "no": False, "n": False}
        if default is None:
            prompt = " [y/n] "
        elif default == "yes":
            prompt = " [Y/n] "
        elif default == "no":
            prompt = " [y/N] "
        else:
            raise ValueError("invalid default answer: '%s'" % default)

        while True:
            sys.stdout.write(question + prompt)
            choice = raw_input().lower()
            if default is not None and choice == '':
                return valid[default]
            elif choice in valid:
                return valid[choice]
            else:
                sys.stdout.write("Please respond with 'yes' or 'no' "
                                 "(or 'y' or 'n').\n")

    def find_component(self):
        with open("popeye.yaml") as stream:
            try:
                file = (yaml.load(stream))
                return file["id"]
            except yaml.YAMLError as exc:
                print(exc)

    def update_comment(self, mib_key, comment):

        payload = {
            "body": comment
        }

        headers = self.build_headers()

        url = 'https://www.mulesoft.org/jira/rest/api/2/issue/' + mib_key + '/comment'

        response = requests.post(url, data=json.dumps(payload), headers=headers)


import numpy as np
from PIL import Image

# Goal: convert an image file from normal pixels to ANSI art made of dots "."
# of same color with canvas-like color background

# ANSI foreground color (n, 0-255) based on 256-bit -> \033[38;5;nm
# ANSI background color (n, 0-255) based on 256-bit -> \033[48;5;nm
# end with \033[m
# colors: https://en.wikipedia.org/wiki/ANSI_escape_code#8-bit
# example: print("\033[48;5;19m\033[38;5;214mHello world!\033[m")

# Open the image
def open_image_array(img_dir="razorback.png"):

    # img_dir = input("Name your picture's filename: ") # input image path
    img_rgb = Image.open(img_dir).convert('RGB') # convert from RGBA to RGB
    # img_rgb.show()

    # convert image into 3D array of 3 8-bit RGB values for each pixel
    rgb_array = np.array(img_rgb, dtype=np.uint8)
    size_d = list(rgb_array.shape) # pic dims [y, x, 3]
    size_d[2] = -1 # change 3 -> -1

    # convert 3D 8-bit color array to 2D int32 color array (each pixel has 1 ANSI color value)
    colorint32 = np.dstack((rgb_array, np.zeros(rgb_array.shape[:2], 'uint8'))).view('uint32').squeeze(-1)
    ansi_array = np.floor(colorint32**(1/3)) # cube root & round down to get 256 ANSI color codes

    # convert 2d int32 array back to 3D 8-bit array, if needed
    rgb_convert = colorint32.view('uint8').reshape(size_d)[:,:,:3]

    # ANSI array of colored dots based on ansi_colors array
    # BG = 230 # off-white background canvas color ANSI code
    ansi_list = ansi_array.astype('uint8').tolist() # convert array to list of lists
    for lst in ansi_list:
        dot_list = ['\033[48;5;230m'] # BG color
        for val in lst:
            dot = '\033[38;5;' + str(val) + 'm.' # add FG color values
            dot_list.append(dot)
        dot_list.append('\033[m')
        row = ''.join(dot_list)
        print(row)

    # Image.fromarray(canvas_array).show()
    # print("\033[48;5;230m\033[38;5;45m.\033[38;5;7m.\033[m")
    # print(len(canvas_list))
    # print(rgb_array)
    # print(size_d)
    # print(colorint32)
    # print(canvas_array)
    # print(canvas_list)

if __name__ == "__main__":
    open_image_array()


import cv2

thres = 0.45 # Threshold to detect object

# hog = cv2.HOGDescriptor()
# hog.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector())
#
# cv2.startWindowThread()
cap = cv2.VideoCapture(0)

### for IP CAM
# cap = cv2.VideoCapture('rtsp://admin:admin@192.168.1.108/',apiPreference=cv2.CAP_FFMPEG)

cap.set(3,1280)
cap.set(4,720)
cap.set(10,70)

classNames= []
classFile = 'coco.names'
with open(classFile,'rt') as f:
    classNames = f.read().rstrip('\n').split('\n')

configPath = 'ssd_mobilenet_v3_large_coco_2020_01_14.pbtxt'
weightsPath = 'frozen_inference_graph.pb'

net = cv2.dnn_DetectionModel(weightsPath,configPath)
net.setInputSize(320,320)
net.setInputScale(1.0/ 127.5)
net.setInputMean((127.5, 127.5, 127.5))
net.setInputSwapRB(True)

while True:
    success,img = cap.read()
    classIds, confs, bbox = net.detect(img,confThreshold=thres)
    # print(classIds,bbox)

    if classIds.__len__() != 0:
        for classId, confidence,box in zip(classIds.flatten(),confs.flatten(),bbox):
            cv2.rectangle(img,box,color=(0,255,0),thickness=2)
            cv2.putText(img,classNames[classId-1].upper(),(box[0]+10,box[1]+30),
                        cv2.FONT_HERSHEY_COMPLEX,1,(0,255,0),2)
            cv2.putText(img,str(round(confidence*100,2)),(box[0]+200,box[1]+30),
                        cv2.FONT_HERSHEY_COMPLEX,1,(0,255,0),2)

    cv2.imshow("Output",img)

    cv2.waitKey(ord('q'))


class Solution(object):
    def sortSentence(self, s):
        """
        :type s: str
        :rtype: str
        """
        tmp = []
        for sub in s.split(" "):
            tmp.append([sub[-1], sub[:-1]])

        tmp.sort()

        return ' '.join([x[1] for x in tmp])


import os, requests

index_file_path = input("Enter Index file path with extension m3u8 : ")

index_file = open(index_file_path,'r')

indexes = index_file.read()

index_file.close()

output_file_path = input("Enter output file path : ")

output_file = open(output_file_path,'wb')

folder_path = input("Enter folder path with extension m3u8_contents ('#<internet>' for get file from internet) : ")

if folder_path == '#<internet>' :

    indexes = indexes.split('http')[1:]

    indexes = ['http'+x.split('\n')[0] for x in indexes]

    for index in indexes :

        content = requests.get(index)

        output_file.write(content.content)

else :

    indexes = indexes.split('file:')[1:]

    indexes = [x.split('\n')[0].split('/')[-1] for x in indexes]

    for index in indexes :

        content_file = open(os.path.join(folder_path,index),'rb')

        output_file.write(content_file.read())

        content_file.close()

output_file.close()


import math

import centrosome.outline
import numpy
import numpy.testing
import pytest
import skimage.measure
import skimage.segmentation

import cellprofiler_core.image
import cellprofiler_core.measurement
from cellprofiler_core.constants.measurement import (
    EXPERIMENT,
    COLTYPE_FLOAT,
    C_LOCATION,
)

import cellprofiler_core.object
import cellprofiler_core.pipeline
import cellprofiler_core.preferences
import cellprofiler_core.workspace

cellprofiler_core.preferences.set_headless()

import plugins.measureobjectintensitymultichannel as momc

IMAGE_NAME = "MyImage"
OBJECT_NAME = "MyObjects"
N_CHANNELS = 4

@pytest.fixture(scope="function")
def image():
    return cellprofiler_core.image.Image()

@pytest.fixture(scope="function")
def measurements():
    return cellprofiler_core.measurement.Measurements()

@pytest.fixture(scope="function")
def module():
    module = momc.MeasureObjectIntensityMultichannel()

    module.images_list.value = IMAGE_NAME

    module.objects_list.value = OBJECT_NAME

    return module

@pytest.fixture(scope="function")
def objects(image):
    objects = cellprofiler_core.object.Objects()

    objects.parent_image = image

    return objects

@pytest.fixture(scope="function")
def workspace(image, measurements, module, objects):
    image_set_list = cellprofiler_core.image.ImageSetList()

    image_set = image_set_list.get_image_set(0)

    image_set.add(IMAGE_NAME, image)

    object_set = cellprofiler_core.object.ObjectSet()

    object_set.add_objects(objects, OBJECT_NAME)

    return cellprofiler_core.workspace.Workspace(
        cellprofiler_core.pipeline.Pipeline(),
        module,
        image_set,
        object_set,
        measurements,
        image_set_list,
    )

def test_init():
    x = momc.MeasureObjectIntensityMultichannel()

def assert_features_and_columns_match(measurements, module):
    object_names = [
        x
        for x in measurements.get_object_names()
        if x
        not in (
            "Image",
            EXPERIMENT,
        )
    ]

    features = [
        [f for f in measurements.get_feature_names(object_name) if f != "Exit_Status"]
        for object_name in object_names
    ]

    columns = module.get_measurement_columns(None)

    assert sum([len(f) for f in features]) == len(columns)

    for column in columns:
        index = object_names.index(column[0])

        assert column[1] in features[index]

        assert column[2] == COLTYPE_FLOAT

def test_supplied_measurements(module):
    """Test the get_category / get_measurements, get_measurement_images functions"""
    module.images_list.value = "MyImage"

    module.objects_list.value = "MyObjects1, MyObjects2"

    expected_categories = tuple(
        sorted(
            [
                momc.INTENSITY,
                C_LOCATION,
            ]
        )
    )

    assert (
        tuple(sorted(module.get_categories(None, "MyObjects1"))) == expected_categories
    )

    assert module.get_categories(None, "Foo") == []

    measurements = module.get_measurements(None, "MyObjects1", momc.INTENSITY)

    assert len(measurements) == len(momc.ALL_MEASUREMENTS)

    measurements = module.get_measurements(None, "MyObjects1", C_LOCATION)

    assert len(measurements) == len(momc.ALL_LOCATION_MEASUREMENTS)

    assert all([m in momc.ALL_LOCATION_MEASUREMENTS for m in measurements])

    assert (
        module.get_measurement_images(
            None,
            "MyObjects1",
            momc.INTENSITY,
            momc.MAX_INTENSITY,
        )
        == ["MyImage"]
    )

def test_get_measurement_columns(module):
    """test the get_measurement_columns method"""
    module.images_list.value = "MyImage"

    module.objects_list.value = "MyObjects1, MyObjects2"

    module.nchannels.value = N_CHANNELS

    columns = module.get_measurement_columns(None)

    assert len(columns) == N_CHANNELS * 2 * (
        len(momc.ALL_MEASUREMENTS) + len(momc.ALL_LOCATION_MEASUREMENTS)
    )

    for column in columns:
        assert column[0] in ("MyObjects1", "MyObjects2")

        assert column[2], COLTYPE_FLOAT

        category = column[1].split("_")[0]

        assert category in (
            momc.INTENSITY,
            C_LOCATION,
        )

        if category == momc.INTENSITY:
            assert column[1][column[1].find("_") + 1 :] in [
                m + "_MyImage" + f"_c{c+1}"
                for m in momc.ALL_MEASUREMENTS
                for c in range(N_CHANNELS)
            ]
        else:
            assert column[1][column[1].find("_") + 1 :] in [
                m + "_MyImage" + f"_c{c+1}"
                for m in momc.ALL_LOCATION_MEASUREMENTS
                for c in range(N_CHANNELS)
            ]

def test_zero(image, measurements, module, objects, workspace):
    """Make sure we can process a blank image"""
    image.pixel_data = numpy.zeros((10, 10, N_CHANNELS))

    objects.segmented = numpy.zeros((10, 10))

    module.nchannels.value = N_CHANNELS

    module.run(workspace)

    for category, features in (
        (
            momc.INTENSITY,
            momc.ALL_MEASUREMENTS,
        ),
        (
            C_LOCATION,
            momc.ALL_LOCATION_MEASUREMENTS,
        ),
    ):
        for meas_name in features:
            for c in range(N_CHANNELS):
                feature_name = "%s_%s_%s_c%s" % (category, meas_name, "MyImage", c + 1)

                data = measurements.get_current_measurement("MyObjects", feature_name)

                assert numpy.product(data.shape) == 0, (
                    "Got data for feature %s" % feature_name
                )

        assert_features_and_columns_match(measurements, module)

def test_masked(image, measurements, module, objects, workspace):
    """Make sure we can process a completely masked image
    Regression test of IMG-971
    """
    image.pixel_data = numpy.zeros((10, 10, N_CHANNELS))

    image.mask = numpy.zeros((10, 10), bool)

    objects.segmented = numpy.ones((10, 10), int)

    module.nchannels.value = N_CHANNELS

    module.run(workspace)

    for meas_name in momc.ALL_MEASUREMENTS:
        for c in range(N_CHANNELS):
            feature_name = "%s_%s_%s_c%s" % (
                momc.INTENSITY,
                meas_name,
                "MyImage",
                c + 1,
            )

            data = measurements.get_current_measurement("MyObjects", feature_name)

            assert numpy.product(data.shape) == 1

            assert numpy.all(numpy.isnan(data) | (data == 0))

    assert_features_and_columns_match(measurements, module)

def test_one(image, measurements, module, objects, workspace):
    """Check measurements on a 3x3 square of 1's"""
    data = numpy.array(
        [
            [0, 0, 0, 0, 0, 0, 0],
            [0, 0, 1, 1, 1, 0, 0],
            [0, 0, 1, 1, 1, 0, 0],
            [0, 0, 1, 1, 1, 0, 0],
            [0, 0, 0, 0, 0, 0, 0],
        ]
    )

    image.pixel_data = data.astype(float)

    objects.segmented = data.astype(int)

    module.nchannels.value = 1

    module.run(workspace)

    for category, meas_name, value in (
        (
            momc.INTENSITY,
            momc.INTEGRATED_INTENSITY,
            9,
        ),
        (
            momc.INTENSITY,
            momc.MEAN_INTENSITY,
            1,
        ),
        (
            momc.INTENSITY,
            momc.STD_INTENSITY,
            0,
        ),
        (
            momc.INTENSITY,
            momc.MIN_INTENSITY,
            1,
        ),
        (
            momc.INTENSITY,
            momc.MAX_INTENSITY,
            1,
        ),
        (
            momc.INTENSITY,
            momc.INTEGRATED_INTENSITY_EDGE,
            8,
        ),
        (
            momc.INTENSITY,
            momc.MEAN_INTENSITY_EDGE,
            1,
        ),
        (
            momc.INTENSITY,
            momc.STD_INTENSITY_EDGE,
            0,
        ),
        (
            momc.INTENSITY,
            momc.MIN_INTENSITY_EDGE,
            1,
        ),
        (
            momc.INTENSITY,
            momc.MAX_INTENSITY_EDGE,
            1,
        ),
        (
            momc.INTENSITY,
            momc.MASS_DISPLACEMENT,
            0,
        ),
        (
            momc.INTENSITY,
            momc.LOWER_QUARTILE_INTENSITY,
            1,
        ),
        (
            momc.INTENSITY,
            momc.MEDIAN_INTENSITY,
            1,
        ),
        (
            momc.INTENSITY,
            momc.UPPER_QUARTILE_INTENSITY,
            1,
        ),
        (
            C_LOCATION,
            momc.LOC_CMI_X,
            3,
        ),
        (
            C_LOCATION,
            momc.LOC_CMI_Y,
            2,
        ),
    ):
        feature_name = "%s_%s_%s_c%s" % (category, meas_name, "MyImage", 1)

        data = measurements.get_current_measurement("MyObjects", feature_name)

        assert numpy.product(data.shape) == 1

        assert data[0] == value, "%s expected %f != actual %f" % (
            meas_name,
            value,
            data[0],
        )

def test_one_masked(image, measurements, module, objects, workspace):
    """Check measurements on a 3x3 square of 1's"""
    img = numpy.array(
        [
            [0, 0, 0, 0, 0, 0, 0],
            [0, 0, 1, 1, 1, 0, 0],
            [0, 0, 1, 1, 1, 0, 0],
            [0, 0, 1, 1, 1, 0, 0],
            [0, 0, 0, 0, 0, 0, 0],
        ]
    )

    mask = img > 0

    image.pixel_data = img.astype(float)

    image.mask = mask

    objects.segmented = img.astype(int)

    module.run(workspace)

    for meas_name, value in (
        (momc.INTEGRATED_INTENSITY, 9),
        (momc.MEAN_INTENSITY, 1),
        (momc.STD_INTENSITY, 0),
        (momc.MIN_INTENSITY, 1),
        (momc.MAX_INTENSITY, 1),
        (momc.INTEGRATED_INTENSITY_EDGE, 8),
        (momc.MEAN_INTENSITY_EDGE, 1),
        (momc.STD_INTENSITY_EDGE, 0),
        (momc.MIN_INTENSITY_EDGE, 1),
        (momc.MAX_INTENSITY_EDGE, 1),
        (momc.MASS_DISPLACEMENT, 0),
        (momc.LOWER_QUARTILE_INTENSITY, 1),
        (momc.MEDIAN_INTENSITY, 1),
        (momc.MAD_INTENSITY, 0),
        (momc.UPPER_QUARTILE_INTENSITY, 1),
    ):
        feature_name = "%s_%s_%s_c%s" % (momc.INTENSITY, meas_name, "MyImage", 1)

        data = measurements.get_current_measurement("MyObjects", feature_name)

        assert numpy.product(data.shape) == 1

        assert data[0] == value, "%s expected %f != actual %f" % (
            meas_name,
            value,
            data[0],
        )

def test_intensity_location(image, measurements, module, objects, workspace):
    data = (
        numpy.array(
            [
                [0, 0, 0, 0, 0, 0, 0],
                [0, 1, 1, 1, 1, 1, 0],
                [0, 1, 1, 1, 1, 2, 0],
                [0, 1, 1, 1, 1, 1, 0],
                [0, 1, 1, 1, 1, 1, 0],
                [0, 1, 1, 1, 1, 1, 0],
                [0, 0, 0, 0, 0, 0, 0],
            ]
        ).astype(float)
        / 2.0
    )

    image.pixel_data = data

    labels = (data != 0).astype(int)

    objects.segmented = labels

    module.run(workspace)

    for feature, value in (
        (momc.LOC_MAX_X, 5),
        (momc.LOC_MAX_Y, 2),
    ):
        feature_name = "%s_%s_%s_c%s" % (C_LOCATION, feature, "MyImage", 1)

        values = measurements.get_current_measurement(OBJECT_NAME, feature_name)

        assert len(values) == 1

        assert values[0] == value

def test_mass_displacement(image, measurements, module, objects, workspace):
    """Check the mass displacement of three squares"""

    labels = numpy.array(
        [
            [0, 0, 0, 0, 0, 0, 0],
            [0, 1, 1, 1, 1, 1, 0],
            [0, 1, 1, 1, 1, 1, 0],
            [0, 1, 1, 1, 1, 1, 0],
            [0, 1, 1, 1, 1, 1, 0],
            [0, 1, 1, 1, 1, 1, 0],
            [0, 0, 0, 0, 0, 0, 0],
            [0, 2, 2, 2, 2, 2, 0],
            [0, 2, 2, 2, 2, 2, 0],
            [0, 2, 2, 2, 2, 2, 0],
            [0, 2, 2, 2, 2, 2, 0],
            [0, 2, 2, 2, 2, 2, 0],
            [0, 0, 0, 0, 0, 0, 0],
            [0, 3, 3, 3, 3, 3, 0],
            [0, 3, 3, 3, 3, 3, 0],
            [0, 3, 3, 3, 3, 3, 0],
            [0, 3, 3, 3, 3, 3, 0],
            [0, 3, 3, 3, 3, 3, 0],
            [0, 0, 0, 0, 0, 0, 0],
        ]
    )

    data = numpy.zeros(labels.shape, dtype=float)
    #
    # image # 1 has a single value in one of the corners
    # whose distance is sqrt(8) from the center
    #
    data[1, 1] = 1

    # image # 2 has a single value on the top edge
    # and should have distance 2
    #
    data[7, 3] = 1

    # image # 3 has a single value on the left edge
    # and should have distance 2
    data[15, 1] = 1

    image.pixel_data = data

    objects.segmented = labels

    module.run(workspace)

    feature_name = "%s_%s_%s_c%s" % (
        momc.INTENSITY,
        momc.MASS_DISPLACEMENT,
        "MyImage",
        1,
    )

    mass_displacement = measurements.get_current_measurement("MyObjects", feature_name)

    assert numpy.product(mass_displacement.shape) == 3

    numpy.testing.assert_almost_equal(mass_displacement[0], math.sqrt(8.0))

    numpy.testing.assert_almost_equal(mass_displacement[1], 2.0)

    numpy.testing.assert_almost_equal(mass_displacement[2], 2.0)

def test_mass_displacement_masked(image, measurements, module, objects, workspace):
    """Regression test IMG-766 - mass displacement of a masked image"""
    labels = numpy.array(
        [
            [0, 0, 0, 0, 0, 0, 0],
            [0, 1, 1, 1, 1, 1, 0],
            [0, 1, 1, 1, 1, 1, 0],
            [0, 1, 1, 1, 1, 1, 0],
            [0, 1, 1, 1, 1, 1, 0],
            [0, 1, 1, 1, 1, 1, 0],
            [0, 0, 0, 0, 0, 0, 0],
            [0, 2, 2, 2, 2, 2, 0],
            [0, 2, 2, 2, 2, 2, 0],
            [0, 2, 2, 2, 2, 2, 0],
            [0, 2, 2, 2, 2, 2, 0],
            [0, 2, 2, 2, 2, 2, 0],
            [0, 0, 0, 0, 0, 0, 0],
            [0, 3, 3, 3, 3, 3, 0],
            [0, 3, 3, 3, 3, 3, 0],
            [0, 3, 3, 3, 3, 3, 0],
            [0, 3, 3, 3, 3, 3, 0],
            [0, 3, 3, 3, 3, 3, 0],
            [0, 0, 0, 0, 0, 0, 0],
        ]
    )

    data = numpy.zeros(labels.shape, dtype=float)

    #
    # image # 1 has a single value in one of the corners
    # whose distance is sqrt(8) from the center
    #
    data[1, 1] = 1

    # image # 2 has a single value on the top edge
    # and should have distance 2
    #
    data[7, 3] = 1

    # image # 3 has a single value on the left edge
    # and should have distance 2
    data[15, 1] = 1

    mask = numpy.zeros(data.shape, bool)

    mask[labels > 0] = True

    image.pixel_data = data

    image.mask = mask

    objects.segmented = labels

    module.run(workspace)

    feature_name = "%s_%s_%s_c%s" % (
        momc.INTENSITY,
        momc.MASS_DISPLACEMENT,
        "MyImage",
        1,
    )

    mass_displacement = measurements.get_current_measurement("MyObjects", feature_name)

    assert numpy.product(mass_displacement.shape) == 3

    numpy.testing.assert_almost_equal(mass_displacement[0], math.sqrt(8.0))

    numpy.testing.assert_almost_equal(mass_displacement[1], 2.0)

    numpy.testing.assert_almost_equal(mass_displacement[2], 2.0)

def test_quartiles_uniform(image, measurements, module, objects, workspace):
    """test quartile values on a 250x250 square filled with uniform values"""
    labels = numpy.ones((250, 250), int)

    numpy.random.seed(0)

    data = numpy.random.uniform(size=(250, 250))

    image.pixel_data = data

    objects.segmented = labels

    module.run(workspace)

    feature_name = "%s_%s_%s_c%s" % (
        momc.INTENSITY,
        momc.LOWER_QUARTILE_INTENSITY,
        "MyImage",
        1,
    )

    data = measurements.get_current_measurement("MyObjects", feature_name)

    numpy.testing.assert_almost_equal(data[0], 0.25, 2)

    feature_name = "%s_%s_%s_c%s" % (
        momc.INTENSITY,
        momc.MEDIAN_INTENSITY,
        "MyImage",
        1,
    )

    data = measurements.get_current_measurement("MyObjects", feature_name)

    numpy.testing.assert_almost_equal(data[0], 0.50, 2)

    feature_name = "%s_%s_%s_c%s" % (momc.INTENSITY, momc.MAD_INTENSITY, "MyImage", 1)

    data = measurements.get_current_measurement("MyObjects", feature_name)

    numpy.testing.assert_almost_equal(data[0], 0.25, 2)

    feature_name = "%s_%s_%s_c%s" % (
        momc.INTENSITY,
        momc.UPPER_QUARTILE_INTENSITY,
        "MyImage",
        1,
    )

    data = measurements.get_current_measurement("MyObjects", feature_name)

    numpy.testing.assert_almost_equal(data[0], 0.75, 2)

def test_quartiles_one_pixel(image, module, objects, workspace):
    """Regression test a bug that occurs in an image with one pixel"""
    labels = numpy.zeros((10, 20))

    labels[2:7, 3:8] = 1

    labels[5, 15] = 2

    numpy.random.seed(0)

    data = numpy.random.uniform(size=(10, 20))

    image.pixel_data = data

    objects.segmented = labels

    # Crashes when pipeline runs in measureobjectintensity.py revision 7146
    module.run(workspace)

def test_quartiles_four_objects(image, measurements, module, objects, workspace):
    """test quartile values on a 250x250 square with 4 objects"""
    labels = numpy.ones((250, 250), int)

    labels[125:, :] += 1

    labels[:, 125:] += 2

    numpy.random.seed(0)

    data = numpy.random.uniform(size=(250, 250))

    #
    # Make the distributions center around .5, .25, 1/6 and .125
    #
    data /= labels.astype(float)

    image.pixel_data = data

    objects.segmented = labels

    module.run(workspace)

    feature_name = "%s_%s_%s_c%s" % (
        momc.INTENSITY,
        momc.LOWER_QUARTILE_INTENSITY,
        "MyImage",
        1,
    )

    data = measurements.get_current_measurement("MyObjects", feature_name)

    numpy.testing.assert_almost_equal(data[0], 1.0 / 4.0, 2)

    numpy.testing.assert_almost_equal(data[1], 1.0 / 8.0, 2)

    numpy.testing.assert_almost_equal(data[2], 1.0 / 12.0, 2)

    numpy.testing.assert_almost_equal(data[3], 1.0 / 16.0, 2)

    feature_name = "%s_%s_%s_c%s" % (
        momc.INTENSITY,
        momc.MEDIAN_INTENSITY,
        "MyImage",
        1,
    )

    data = measurements.get_current_measurement("MyObjects", feature_name)

    numpy.testing.assert_almost_equal(data[0], 1.0 / 2.0, 2)

    numpy.testing.assert_almost_equal(data[1], 1.0 / 4.0, 2)

    numpy.testing.assert_almost_equal(data[2], 1.0 / 6.0, 2)

    numpy.testing.assert_almost_equal(data[3], 1.0 / 8.0, 2)

    feature_name = "%s_%s_%s_c%s" % (
        momc.INTENSITY,
        momc.UPPER_QUARTILE_INTENSITY,
        "MyImage",
        1,
    )

    data = measurements.get_current_measurement("MyObjects", feature_name)

    numpy.testing.assert_almost_equal(data[0], 3.0 / 4.0, 2)

    numpy.testing.assert_almost_equal(data[1], 3.0 / 8.0, 2)

    numpy.testing.assert_almost_equal(data[2], 3.0 / 12.0, 2)

    numpy.testing.assert_almost_equal(data[3], 3.0 / 16.0, 2)

    feature_name = "%s_%s_%s_c%s" % (momc.INTENSITY, momc.MAD_INTENSITY, "MyImage", 1)

    data = measurements.get_current_measurement("MyObjects", feature_name)

    numpy.testing.assert_almost_equal(data[0], 1.0 / 4.0, 2)

    numpy.testing.assert_almost_equal(data[1], 1.0 / 8.0, 2)

    numpy.testing.assert_almost_equal(data[2], 1.0 / 12.0, 2)

    numpy.testing.assert_almost_equal(data[3], 1.0 / 16.0, 2)

def test_median_intensity_masked(image, measurements, module, objects, workspace):
    numpy.random.seed(37)

    labels = numpy.ones((10, 10), int)

    mask = numpy.ones((10, 10), bool)

    mask[:, :5] = False

    pixel_data = numpy.random.uniform(size=(10, 10, N_CHANNELS)).astype(numpy.float32)

    pixel_data[~mask, :] = 1

    image.pixel_data = pixel_data

    image.mask = mask

    objects.segmented = labels

    expected = [
        numpy.sort(pixel_data[mask, c])[numpy.sum(mask) // 2] for c in range(N_CHANNELS)
    ]

    module.nchannels.value = N_CHANNELS

    module.run(workspace)

    assert isinstance(measurements, cellprofiler_core.measurement.Measurements)

    for c, exp in enumerate(expected):
        values = measurements.get_current_measurement(
            OBJECT_NAME,
            "_".join((momc.INTENSITY, momc.MEDIAN_INTENSITY, IMAGE_NAME, f"c{c+1}")),
        )

        assert len(values) == 1

        assert exp == values[0]


# 두 요소의 위치를 바꿔주는 helper function
def swap_elements(my_list, index1, index2):
  tmpValue = my_list[index2]
  my_list[index2] = my_list[index1]
  my_list[index1] = tmpValue
  return my_list

# 퀵 정렬에서 사용되는 partition 함수
def partition(my_list, start, end):
  p = end # pivot 인덱스
  b = start # big 그룹 이동 인덱스
  i = start # 체크 이동 인덱스
  for i in range(start, end):
    # pivot과 해당값을 비교히고 파티셔닝
    if my_list[p] < my_list[i]:
      i += 1
    else:
      my_list = swap_elements(my_list, b, i)
      b += 1
      i += 1
  swap_elements(my_list, b, p)
  return b

# 테스트 1
list1 = [4, 3, 6, 2, 7, 1, 5]
pivot_index1 = partition(list1, 0, len(list1) - 1)
print(list1)
print(pivot_index1)

# 테스트 2
list2 = [6, 1, 2, 6, 3, 5, 4]
pivot_index2 = partition(list2, 0, len(list2) - 1)
print(list2)
print(pivot_index2)


from typing import List

class Solution:
    def highFive(self, items: List[List[int]]) -> List[List[int]]:
        items.sort(key=lambda x: (x[0], x[1]))

        ans = list()
        for i in range(len(items)):
            if i == len(items) - 1 or items[i][0] != items[i + 1][0]:
                temp = list()
                temp.append(items[i][0])
                sum = 0
                for j in range(5):
                    sum += items[i - j][1]
                sum //= 5
                temp.append(sum)
                ans.append(temp)

        return ans


""" Clean up of hail endpoint column in three steps:
- remove unused staging column
- remove now obsolete testing column
- make hail_endpoint_production non null

Revision ID: aa6d3d875f28
Revises: 8bd62cba881a
Create Date: 2020-11-17 09:28:10.910999

"""
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql

# revision identifiers, used by Alembic.
revision = 'aa6d3d875f28'
down_revision = '8bd62cba881a'
branch_labels = None
depends_on = None

def upgrade():
    op.drop_column('user', 'hail_endpoint_staging')
    op.drop_column('user', 'hail_endpoint_testing')
    op.execute(sa.text('''update "user" set hail_endpoint_production = '' where hail_endpoint_production is null'''))
    op.alter_column('user', 'hail_endpoint_production', existing_type=sa.VARCHAR(), server_default='', nullable=False)

def downgrade():
    op.alter_column('user', 'hail_endpoint_production',
               existing_type=sa.VARCHAR(),
               nullable=True)
    op.add_column('user', sa.Column('hail_endpoint_testing', sa.VARCHAR(), autoincrement=False, nullable=True))
    op.add_column('user', sa.Column('hail_endpoint_staging', sa.VARCHAR(), autoincrement=False, nullable=True))


from datetime import datetime
from binance_functions.client_functions.binance_client import CreateClient
from decouple import config
from settings.bot_settings import *
from multiprocessing import Pool

import timeit
import json

class HistoricalData:
    def __init__(self):
        if config('API_KEY') != None and config('SECRET_KEY') != None:
            self.api_key = config('API_KEY')
            self.secret_key = config('SECRET_KEY')
        else:
            self.api_key = ""
            self.secret_key = ""
        self.symbol_list = SYMBOL_LIST
        self.exchange_pair = EXCHANGE_PAIR
        self.interval = INTERVAL
        self.all_data = dict()
        self.my_client = CreateClient(self.api_key, self.secret_key).client()

    def historical(self, symbol):
        all_datas = self.my_client.get_historical_klines(symbol=symbol+self.exchange_pair, interval=self.interval, limit=1000)
        converted_datas = list()
        data_dict = dict()
        for value in all_datas:
            __date = datetime.fromtimestamp(int(str(value[6])[:10]))
            __date = __date.strftime('%d/%m/%Y %H:%M')
            __open_price = float(value[1])
            __high_price = float(value[2])
            __low_price = float(value[3])
            __close_price = float(value[4])
            __new_data = [__date, __open_price, __high_price, __low_price, __close_price]
            converted_datas.append(__new_data)
        converted_datas.pop()
        data_dict[symbol+self.exchange_pair] = converted_datas
        return data_dict

    def collect_historical(self):
        historical_process_start_time = timeit.default_timer()
        p = Pool()
        result = p.map(self.historical, self.symbol_list)

        p.close()
        p.join()

        historical_process_finish_time = timeit.default_timer()
        for data in result:
            self.all_data[list(data.keys())[0]] = list(data.values())[0]
        with open("./data/all_data.json", 'w') as file:
            json.dump(self.all_data, file)

        print("Collect Historical Data Process Take:",historical_process_finish_time - historical_process_start_time, "Seconds")
        return True


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

import warnings
warnings.filterwarnings("ignore")

import numpy as np
import os
import pandas as pd
from divexplorer_generalized.FP_Divergence import FP_Divergence

DATASET_DIRECTORY = os.path.join(os.path.curdir, "datasets")

# # Import data

def abbreviateValue(value, abbreviations={}):
    for k, v in abbreviations.items():
        if k in value:

            value = value.replace(k, v)
    #TODO
    if value[0:2] not in ["q_", "u_"]:
        value = value.replace("_", " ")
    return value

def abbreviate_dict_value(input_dict, abbreviations):

    conv ={}
    for k1, dict_i in input_dict.items():
        conv[k1] = { abbreviateValue(k, abbreviations): d for k, d in dict_i.items()}
    return conv

def get_predefined_color_labels(abbreviations = {}):
    color_labels = {}

    color_labels[abbreviateValue(f'entropy_base', abbreviations)]="#7fcc7f"
    color_labels[abbreviateValue(f'divergence_criterion_base', abbreviations)]="#009900"

    color_labels[abbreviateValue(f'entropy_generalized', abbreviations)]="mediumblue"
    color_labels[abbreviateValue(f'divergence_criterion_generalized', abbreviations)]="orangered"

    color_labels[abbreviateValue(f'entropy_base_pruned', abbreviations)]="yellow"
    color_labels[abbreviateValue(f'divergence_criterion_base_pruned', abbreviations)]="#C179EE"

    color_labels[abbreviateValue(f'entropy_generalized_pruned', abbreviations)]="gray"
    color_labels[abbreviateValue(f'divergence_criterion_generalized_pruned', abbreviations)]="#C01FB1"

    return color_labels

def run_pruning_experiemnt(dataset_name = 'wine', min_support_tree = 0.1,
    min_sup_divergences = [0.1, 0.15, 0.2],
    type_criterion="divergence_criterion",
    type_experiment = "one_at_time",
    metric = "d_fpr",
    save = True,
    output_dir = 'output_results_2',
    saveFig = True,
    dataset_dir = DATASET_DIRECTORY):

    print(dataset_name)
    print(min_sup_divergences)
    print(output_dir)

    if dataset_name == 'wine':
        from import_process_dataset import import_process_wine, train_classifier_kv

        df, class_map, continuous_attributes = import_process_wine()
        # # Train and predict with RF classifier

        df = train_classifier_kv(df)
    elif dataset_name== "compas":
        from import_process_dataset import import_compas

        df, class_map, continuous_attributes = import_compas()

    elif dataset_name== "adult":
        from import_process_dataset import import_process_adult

        df, class_map, continuous_attributes = import_process_adult()

        from sklearn.preprocessing import LabelEncoder

        attributes = df.columns.drop("class")
        X = df[attributes].copy()
        y = df["class"].copy()

        encoders = {}
        for column in attributes:
            if df.dtypes[column] == np.object:
                print(column)
                le = LabelEncoder()
                X[column] = le.fit_transform(df[column])
                encoders[column] = le

        from sklearn.model_selection import StratifiedKFold
        from sklearn.model_selection import cross_val_predict
        from sklearn.ensemble import RandomForestClassifier

        clf = RandomForestClassifier(random_state=42)
        cv = StratifiedKFold(n_splits=10, random_state=42, shuffle=True
                    )  # Added to fix the random state  #Added shuffle=True for new version sklearn, Value Error

        y_predicted = cross_val_predict(clf, X, y.values, cv=cv)
        df["predicted"] = y_predicted
    else:
        raise ValueError()

    # # Tree divergence

    true_class_name = "class"
    pred_class_name = "predicted"
    cols_c = [true_class_name, pred_class_name]

    df_analyze = df.copy()

    from tree_discretization import TreeDiscretization

    tree_discr = TreeDiscretization()

    # ## Extract tree
    generalization_dict, discretizations = tree_discr.get_tree_discretization(
        df_analyze,
        type_splitting=type_experiment,
        min_support=min_support_tree,
        metric=metric,
        class_map=class_map,
        continuous_attributes=list(continuous_attributes),
        class_and_pred_names=cols_c,
        storeTree=True,
        type_criterion=type_criterion,
        #minimal_gain = 0.0015
    )

    # # Extract patterns

    out_support = {}
    out_time = {}
    out_fp = {}

    from utils_extract_divergence_generalized import (
        extract_divergence_generalized,
    )
    import time

    for apply_generalization in [False, True]:
        type_gen = 'generalized' if apply_generalization else 'base'
        print(type_gen)
        for keep in [True, False]:
            if keep:
                keep_items = tree_discr.get_keep_items_associated_with_divergence()
                keep_str = "_pruned"
            else:
                keep_items = None
                keep_str = ""
            print(keep_str)
            for min_sup_divergence in min_sup_divergences:
                print(min_sup_divergence, end = " ")
                s_time = time.time()
                FP_fm = extract_divergence_generalized(
                    df_analyze,
                    discretizations,
                    generalization_dict,
                    continuous_attributes,
                    min_sup_divergence=min_sup_divergence,
                    apply_generalization=apply_generalization,
                    true_class_name=true_class_name,
                    predicted_class_name=pred_class_name,
                    class_map=class_map,
                    metrics_divergence = [metric],
                    FPM_type="fpgrowth",
                    save_in_progress = False,
                    keep_only_positive_divergent_items=keep_items
                )

                key = type_gen + keep_str

                out_time.setdefault(min_sup_divergence, {})[key] = time.time()-s_time

                print(f"({(time.time()-s_time):.2f})")

                fp_divergence_i = FP_Divergence(FP_fm, metric=metric)

                most_divergent = (
                    fp_divergence_i.getDivergence(th_redundancy=0)
                    .sort_values(
                        [fp_divergence_i.metric, fp_divergence_i.t_value_col], ascending=False
                    )
                    .head(1)
                )
                out_support.setdefault(min_sup_divergence, {})[key] = most_divergent

                out_fp.setdefault(min_sup_divergence, {})[key] = len(FP_fm)

    import os
    output_fig_dir = os.path.join(os.path.curdir, output_dir, "figures", "output_performance")

    if saveFig:

        from pathlib import Path

        Path(output_fig_dir).mkdir(parents=True, exist_ok=True)

    abbreviations = {"one_at_time":"indiv t.", \
                        "divergence_criterion":"g$\\Delta$", "entropy":"entr"}

    color_labels = get_predefined_color_labels(abbreviations)
    lines_style = {k:"-" for k in color_labels}
    lines_style.update({k:"--" for k in color_labels if( "base" in k and abbreviations["entropy"] in k)})
    lines_style.update({k:"-." for k in color_labels if( 'base' in k and abbreviations["divergence_criterion"] in k)})

    size_fig = (3,3)
    from utils_plot import plotDicts

    out_support_max = {}

    for sup in sorted(out_support.keys()):
        out_support_max[sup] = {}
        for type_gen in out_support[sup]:
            out_support_max[sup][type_gen] = out_support[sup][type_gen][metric].iloc[0]

    for info_i, results in [('time', out_time), (f"max_{metric}", out_support_max), ('FP', out_fp)]:

        info_plot = {}
        for sup in sorted(results.keys()):
            for type_gen in results[sup]:
                type_gen_str = abbreviateValue(f"{type_criterion}_{type_gen}", abbreviations)
                if type_gen_str not in info_plot:
                    info_plot[type_gen_str] = {}
                info_plot[type_gen_str][sup] = results[sup][type_gen]

        figure_name = os.path.join(output_fig_dir, f"{dataset_name}_stree_{min_support_tree}_{metric}_{info_i}.pdf")

        title, ylabel = '', ''
        if info_i == 'time':
            title = 'Execution time'
            ylabel="Execution time $(seconds)$"

        elif info_i == f"max_{metric}":
            ylabel="Max $\\Delta_{FPR}$"
            title="Highest $\\Delta_{FPR}$"

        elif info_i == 'FP':
            ylabel="#FP"
            title="#FP"

        plotDicts(info_plot, marker=True, \
                title = title, sizeFig=size_fig,\
                        linestyle=lines_style, color_labels=color_labels, \
                xlabel="Minimum support s",ylabel=ylabel  , labelSize=10.2,\
                outside=False,  saveFig=saveFig, nameFig = figure_name)

    # # Store performance results

    if save:
        import os

        output_results = os.path.join(os.path.curdir, output_dir, 'performance')
        from pathlib import Path

        Path(output_results).mkdir(parents=True, exist_ok=True)

        conf_name = f"{dataset_name}_{metric}_{type_criterion}_{min_support_tree}"

        import json
        with open(os.path.join(output_results, f'{conf_name}_time.json'), 'w') as output_file:
            output_file.write(json.dumps(out_time))

        import json
        with open(os.path.join(output_results, f'{conf_name}_fp.json'), 'w') as output_file:
            output_file.write(json.dumps(out_fp))

        out_support_max = {}

        for sup in sorted(out_support.keys()):
            out_support_max[sup] = {}
            for type_gen in out_support[sup]:
                out_support_max[sup][type_gen] = out_support[sup][type_gen][metric].iloc[0]

        with open(os.path.join(output_results, f'{conf_name}_div.json'), 'w') as output_file:
            output_file.write(json.dumps(out_support_max))

import argparse

if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument(
        "--name_output_dir",
        default="output_red",
        help="specify the name of the output folder",
    )

    parser.add_argument(
        "--dataset_dir",
        default=DATASET_DIRECTORY,
        help="specify the dataset directory",
    )

    parser.add_argument(
        "--no_show_figs",
        action="store_true",
        help="specify not_show_figures to vizualize the plots. The results are stored into the specified outpur dir.",
    )

    parser.add_argument(
        "--dataset_name",
        default="wine",
        help="specify the name of the dataset",
    )

    parser.add_argument(
        "--min_support_tree",
        type=float,
        default=0.1,
        help="specify the name of the dataset",
    )
    parser.add_argument(
        "--min_sup_divergences",
        type=float,
        nargs="*",
        default=[0.15, 0.2],
        help="specify the minimum support scores",
    )
    parser.add_argument(
        "--type_criterion",
        type=str,
        default="divergence_criterion",
        help="specify the split criterion",
    )
    parser.add_argument(
        "--metric",
        type=str,
        default='d_fpr',
        help="specify the metric",
    )

    args = parser.parse_args()

    run_pruning_experiemnt(min_support_tree = args.min_support_tree,
    min_sup_divergences = args.min_sup_divergences,
    type_criterion=args.type_criterion,
    metric = args.metric,
    #save = True,
    #saveFig = True,
        dataset_name = args.dataset_name,
        output_dir=args.name_output_dir,
        dataset_dir=args.dataset_dir,
    )


'''
Detects a grid.
'''
import ujson
import cv2
from math import floor
from operator import itemgetter
from argparse import ArgumentParser
from pytesseract import image_to_string

parser = ArgumentParser(description = 'Detect grid.')
parser.add_argument(
	"-f",
	"--filename",
	dest = "filename",
	help = "filename prefix of images"
)
parser.add_argument(
	"-d",
	"--dev",
	dest = "dev",
	help = "run in development mode (preview image)",
	action = "store_true"
)
parser.add_argument(
	"--parts",
	dest = "parts",
	help = "which parts to read",
	nargs = "+",
	default = ["grid", "rides", "day", "roads"]
)

args = parser.parse_args()
dev = args.dev
parts = args.parts

image = cv2.imread("screen.png")
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

(imageHeight, imageWidth, channels) = image.shape

def hasTheRightAspectRatio(boundingRect):
	[x, y, w, h] = boundingRect

	return round((w / h) * 10) == 12 or round((h / w) * 10) == 12

def isBigEnough(boundingRect):
	[x, y, w, h] = boundingRect

	return w > round(imageHeight / 30) and w < round(imageHeight / 5)

def topIsBrighterThanBottom(boundingRect, image):
	[x, y, w, h] = boundingRect

	centerX = x + (w / 2)
	centerY = y + (h / 2)

	centerYAbove = centerY - (h / 6)
	centerYBelow = centerY + (h / 6)

	pixelAbove = image[round(centerYAbove), round(centerX)]
	pixelBelow = image[round(centerYBelow), round(centerX)]

	return sum(pixelAbove) > sum(pixelBelow)

def isAHouse(boundingRect, image):
	return (
		hasTheRightAspectRatio(boundingRect) and
		isBigEnough(boundingRect) and
		topIsBrighterThanBottom(boundingRect, image)
	)

def drawBoundingRects(image, contours):
	for contour in contours:
		[contourX, contourY, w, h] = contour
		point1 = (contourX, contourY)
		point2 = (contourX + w, contourY + h)

		cv2.rectangle(image, point1, point2, (0, 0, 255), 2)

def cellFromBuilding(contour, offset, scale):
	[contourX, contourY, w, h] = contour
	[offsetX, offsetY] = offset
	[scaleX, scaleY] = scale

	newX = (contourX - (w * offsetX))
	newY = (contourY - (h * offsetY))
	newW = scale * w

	return {"x": newX, "y": newY, "width": newW}

def topLeftCellFromSample(cell, imageWidth, imageHeight, borders):
	x, y, width = itemgetter("x", "y", "width")(cell)

	topBorder, leftBorder = borders

	widthPadding = (imageWidth * leftBorder)
	heightPadding = (imageHeight * topBorder)

	usableWidth = x - widthPadding
	usableHeight = y - heightPadding

	newX = x - (floor(usableWidth / width) * width)

	newY = y - (floor(usableHeight / width) * width)

	return {"x": newX, "y": newY, "width": width}

def bottomRightCellFromSample(cell, imageWidth, imageHeight, borders):
	x, y, width = itemgetter("x", "y", "width")(cell)

	bottomBorder, rightBorder = borders

	remainingWidth = imageWidth - x - width
	remainingHeight = imageHeight - y - width

	widthPadding = (imageWidth * rightBorder)
	heightPadding = (imageHeight * bottomBorder)

	usableWidth = remainingWidth - widthPadding
	usableHeight = remainingHeight - heightPadding

	newX = x + ((floor(usableWidth / width)) * width)

	newY = y + ((floor(usableHeight / width)) * width)

	return {"x": newX, "y": newY, "width": width}

def getCells(contour, cellSettings, borderSettings):
	[contourX, contourY, w, h] = contour

	if w < h:
		w, h = h, w

	[offset, scale] = cellSettings

	sampleCell = cellFromBuilding([contourX, contourY, w, h], offset, scale)

	[
		topBorder,
		bottomBorder,
		leftBorder,
		rightBorder,
	] = borderSettings

	topLeftBorder = [topBorder, leftBorder]

	topLeftCell = topLeftCellFromSample(sampleCell, imageWidth, imageHeight, topLeftBorder)

	bottomRightBorder = [bottomBorder, rightBorder]

	bottomRightCell = bottomRightCellFromSample(sampleCell, imageWidth, imageHeight, bottomRightBorder)

	return [sampleCell, topLeftCell, bottomRightCell]

def drawCell(image, cell):
	x, y, width = itemgetter("x", "y", "width")(cell)
	point1 = tuple(map(round, (x, y)))
	point2 = tuple(map(round, (x + width, y + width)))

	cv2.rectangle(image, point1, point2, (0, 0, 255), 2)

def drawAll(image, cellSettings, borderSettings, contours):
	for contour in contours:
		cells = getCells(contour, cellSettings, borderSettings)
		for cell in cells:
			drawCell(image, cell)

def arithmeticMean(numbers):
	return sum(numbers) / len(numbers)

def reciprocal(number):
	return 1 / number

def reciprocalSum(numbers):
	return sum(list(map(reciprocal, numbers)))

def harmonicMean(numbers):
	return len(numbers) / reciprocalSum(numbers)

def selectPart(collection, i):
	return list(map(lambda c: c[i], collection))

def getAverageCells(cells):
	keys = ["x", "y", "width"]
	indices = [1, 2]

	if len(cells) == 0:
		return [
			{
				"x": 1,
				"y": 1,
				"width": 10
			},
			{
				"x": 1,
				"y": 1,
				"width": 10
			}
		]

	return [
		{
			key: harmonicMean(selectPart(selectPart(cells, i), key)) for key in keys
		} for i in indices
	]

def drawAverage(image, cellSettings, borderSettings, contours):
	cells = list(map(lambda c: getCells(c, cellSettings, borderSettings), contours))

	averageCells = getAverageCells(cells)

	# print(cells)
	# print(averageCells)

	# drawCell(image, cells[0]) # sampleCell
	drawCell(image, averageCells[0]) # average topLeftCell
	drawCell(image, averageCells[1]) # average bottomRightCell

def getGrid(topLeftCell, bottomRightCell):
	topLeftX, topLeftY, width = itemgetter("x", "y", "width")(topLeftCell)
	bottomRightX, bottomRightY = itemgetter("x", "y", "width")(bottomRightCell)[:2]

	gridWidth = (bottomRightX + width) - topLeftX
	gridHeight = (bottomRightY + width) - topLeftY

	return list(
		map(
			lambda y: list(
				map(
					lambda x: {"x": round(x), "y": round(y), "width": round(width)},
					list(range(round(topLeftX), round(gridWidth + width), round(width)))
				)
			),
			list(range(round(topLeftY), round(gridHeight + width), round(width)))
		)
	)

def drawGrid(image, grid):
	for row in grid:
		for cell in row:
			drawCell(image, cell)

def linearConversion(oldRange, newRange, value):
	[oldMin, oldMax] = oldRange
	[newMin, newMax] = newRange

	return (((value - oldMin) * (newMax - newMin)) / (oldMax - oldMin)) + newMin

sliderMax = 100
windowTitle = "slider"

defaultOffsetX = 120 / 1000
defaultOffsetY = 120 / 1000
defaultScaleX = 660 / 1000
defaultScaleY = 660 / 1000
defaultTopBorder = linearConversion([480, 1050], [15, 50], imageHeight) / 1000
defaultBottomBorder = linearConversion([480, 1050], [120, 140], imageHeight) / 1000
defaultLeftBorder = 30 / 1000
defaultRightBorder = 30 / 1000

borders = [
	["top", defaultTopBorder],
	["bottom", defaultBottomBorder],
	["left", defaultLeftBorder],
	["right", defaultRightBorder]
]

borderTrackbarNamesAndDefaults = list(map(lambda d: [f"{d[0]}Border", d[1]], borders))

def getSettings():
	[
		offsetX,
		offsetY,
		scaleX,
		scaleY,
		topBorder,
		bottomBorder,
		leftBorder,
		rightBorder
	] = [
		defaultOffsetX,
		defaultOffsetY,
		defaultScaleX,
		defaultScaleY,
		defaultTopBorder,
		defaultBottomBorder,
		defaultLeftBorder,
		defaultRightBorder
	]

	cellSettings = [[offsetX, offsetY], [scaleX, scaleY]]

	borderSettings = [topBorder, bottomBorder, leftBorder, rightBorder]

	return [cellSettings, borderSettings]

def cropCell(image, cell):
	x, y, width = itemgetter("x", "y", "width")(cell)

	crop = image.copy()[y:y + width, x:x + width]

	crop = cv2.cvtColor(crop, cv2.COLOR_BGR2RGB)

	(cropHeight, cropWidth, cropChannels) = crop.shape

	targetWidth = 64

	crop = cv2.resize(
		crop,
		(targetWidth, targetWidth),
		cv2.INTER_AREA if cropWidth > targetWidth else cv2.INTER_LINEAR
	)

	return crop.tolist()

def cropRides(image):
	return image.copy()[
		round(imageHeight / 40):round(imageHeight / 15),
		round(imageWidth - (imageWidth / 4)):round(imageWidth - imageWidth / 8.7)
	]

def getRides(image):
	tesseractConfig = ["eng", "--psm 7 --oem 1"]

	croppedRides = cropRides(image)

	(ret, croppedRides) = cv2.threshold(croppedRides, 127, 255, cv2.THRESH_TOZERO)

	croppedRides = ~croppedRides

	return image_to_string(croppedRides, *tesseractConfig).replace("\n", "").replace("\f", "")

def cropDay(image):
	return image.copy()[
		round(imageHeight / 40):round(imageHeight / 15),
		round(imageWidth - (imageWidth / 12)):round(imageWidth - (imageWidth / 22))
	]

def getDay(image):
	tesseractConfig = ["eng", "--psm 7 --oem 1"]

	croppedDay = cropDay(image)

	(ret, croppedDay) = cv2.threshold(croppedDay, 127, 255, cv2.THRESH_TOZERO)

	croppedDay = ~croppedDay

	return image_to_string(croppedDay, *tesseractConfig).replace("\n", "").replace("\f", "")

def cropRoads(image):
	return image.copy()[
		round(imageHeight - (imageHeight / 16)):round(imageHeight - (imageHeight / 21.5)),
		round((imageWidth / 2) + (imageWidth / 65)):round((imageWidth / 2) + (imageWidth / 33))
	]

def getRoads(image):
	tesseractConfig = ["eng", "--psm 7 --oem 1"]

	croppedRoads = cropRoads(image)

	(ret, croppedRoads) = cv2.threshold(croppedRoads, 127, 255, cv2.THRESH_TOZERO)

	croppedRoads = ~croppedRoads

	return image_to_string(croppedRoads, *tesseractConfig).replace("\n", "").replace("\f", "")

def cropGameOver(image):
	return image.copy()[
		round((imageHeight / 2) - (imageHeight / 3)):round((imageHeight / 2) - (imageHeight / 4)),
		round((imageWidth / 2) - (imageWidth / 7)):round((imageWidth / 2) + (imageWidth / 7))
	]

def getGameOver(image):
	tesseractConfig = ["eng", "--psm 7 --oem 1"]

	croppedGameOver = cropGameOver(image)

	(ret, croppedGameOver) = cv2.threshold(croppedGameOver, 127, 255, cv2.THRESH_TOZERO)

	croppedGameOver = ~croppedGameOver

	return image_to_string(croppedGameOver, *tesseractConfig).replace("\n", "").replace("\f", "")

def cropUpgrade(image):
	return image.copy()[
		round(
			(imageHeight / 2) - (imageHeight / 4.5)
		):
		round(
			(imageHeight / 2) - (imageHeight / 7)
		),
		round(
			(imageWidth / 2) - (imageWidth / 4.5)
		):
		round(
			(imageWidth / 2) + (imageWidth / 10)
		)
	]

def getUpgrade(image):
	tesseractConfig = ["eng", "--psm 7 --oem 1"]

	croppedUpgrade = cropUpgrade(image)

	(ret, croppedUpgrade) = cv2.threshold(croppedUpgrade, 127, 255, cv2.THRESH_TOZERO)

	croppedUpgrade = ~croppedUpgrade

	return image_to_string(croppedUpgrade, *tesseractConfig).replace("\n", "").replace("\f", "")

def detectGrid(value=0):
	[cellSettings, borderSettings] = getSettings()

	cells = list(map(lambda c: getCells(c, cellSettings, borderSettings), contours))

	averageCells = getAverageCells(cells)

	grid = getGrid(*averageCells)

	if dev:
		print(cellSettings)
		print(borderSettings)

		image = cv2.imread("screen.png")

		# drawGrid(image, grid)
		drawBoundingRects(image, contours)

		[cellSettings, borderSettings] = getSettings()

		drawAverage(image, cellSettings, borderSettings, contours)

		drawAll(image, cellSettings, borderSettings, contours)

	return grid

def getContours(image):
	div = 32
	quantized = image // div * div + div // 2
	hsv = cv2.cvtColor(quantized, cv2.COLOR_BGR2HSV)

	hsvThreshold = cv2.inRange(hsv, (0, 100, 150), (255, 255, 255))

	blurred = cv2.GaussianBlur(hsvThreshold, (3, 3), 0)
	canny = cv2.Canny(blurred, 120, 255, 1)

	# Find contours
	contours = cv2.findContours(canny, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
	contours = contours[0] if len(contours) == 2 else contours[1]

	contours = list(map(cv2.boundingRect, contours))

	contours = list(filter(lambda b: isAHouse(b, quantized), list(map(list, contours))))

	return contours

if dev:
	contours = getContours(image)

	cv2.namedWindow(windowTitle)

	grid = detectGrid()

	drawGrid(image, grid)
	drawBoundingRects(image, contours)

	[cellSettings, borderSettings] = getSettings()

	drawAverage(image, cellSettings, borderSettings, contours)

	drawAll(image, cellSettings, borderSettings, contours)

	cv2.imwrite(f"./images/test/{args.filename}.png", image)

else:
	gameOverString = getGameOver(gray)
	upgradeString = getUpgrade(gray)

	if gameOverString == "Game Over":
		print("GAME OVER")
	elif upgradeString.startswith("Woche"):
		print(upgradeString)
	else:
		if "grid" in parts:
			contours = getContours(image)

			[cellSettings, borderSettings] = getSettings()

			cells = list(map(lambda c: getCells(c, cellSettings, borderSettings), contours))

			grid = getGrid(*getAverageCells(cells))

			gridData = list(
				map(
					lambda row: list(
						map(
							lambda cell: {key: cell[key] for key in ["x", "y"]} | {"pixels": cropCell(image, cell)},
							row
						)
					),
					grid
				)
			)
		else:
			gridData = None

		if "rides" in parts:
			rides = getRides(gray)
		else:
			rides = None

		if "day" in parts:
			day = getDay(gray)
		else:
			day = None

		if "roads" in parts:
			roads = getRoads(gray)
		else:
			roads = None

		data = {
			"grid": gridData,
			"rides": rides,
			"day": day,
			"roads": roads
		}

		print(ujson.dumps(data))


from pathlib import Path
from uuid import uuid4

from django.core.validators import FileExtensionValidator
from django.db.models import (
    CASCADE,
    CharField,
    DateField,
    FileField,
    ForeignKey,
    IntegerChoices,
    IntegerField,
    Model,
    TextField,
    URLField,
)
from django.utils.translation import gettext_lazy as _

def get_file_name_in_storage(instance, filename):
    name = f"{uuid4()}-{filename}"
    return Path(instance._meta.model_name) / str(instance.kid.id) / name

class Kid(Model):
    class Color(IntegerChoices):
        BLACK = 1, _("Preto")
        BROWN = 2, _("Castanhos")
        BLONDE = 3, _("Loiro")
        RED = 4, _("Ruivo")
        BLUE = (
            5,
            _("Azul"),
        )
        SWARTHY = 6, _("Morena")
        WHITE = 7, _("Branca")

    # required fiels
    name = CharField("Nome", max_length=255, db_index=True, unique=True)
    url = URLField("URL")
    full_text = TextField()

    # optional indexed fields
    dob = DateField("Data de nascimento", null=True, blank=True, db_index=True)
    missing_since = DateField(
        "Desaparecida(o) desde", null=True, blank=True, db_index=True
    )
    eyes = CharField(
        "Cor dos olhos",
        max_length=50,
        choices=Color.choices,
        null=True,
        blank=True,
        db_index=True,
    )
    hair = CharField(
        "Cor dos cabelos",
        max_length=50,
        choices=Color.choices,
        null=True,
        blank=True,
        db_index=True,
    )
    skin = CharField(
        "Cor da pele",
        max_length=50,
        choices=Color.choices,
        null=True,
        blank=True,
        db_index=True,
    )

    # optional fields
    mother = CharField("Mãe", max_length=255, null=True, blank=True)
    father = CharField("Pai", max_length=255, null=True, blank=True)
    last_seen_at_city = CharField(
        "Cidade onde foi vista(o) pela última vez",
        max_length=255,
        null=True,
        blank=True,
        db_index=True,
    )
    last_seen_at_state = CharField(
        "UF onde foi vista(o) pela última vez",
        max_length=2,
        null=True,
        blank=True,
        db_index=True,
    )
    age_at_occurrence = IntegerField("Idade quando desapareceu", null=True, blank=True)

    class Meta:
        verbose_name = "criança"
        ordering = ("name",)

    def __str__(self):
        return self.name

class KidImage(Model):
    kid = ForeignKey(Kid, on_delete=CASCADE, verbose_name="criança")
    image = FileField(
        verbose_name="Foto",
        upload_to=get_file_name_in_storage,
        validators=(
            FileExtensionValidator(allowed_extensions=("jpg", "jpeg", "png", "gif")),
        ),
    )

    def __str__(self):
        return self.kid.name

    class Meta:
        verbose_name = "foto"
        verbose_name_plural = "fotos"


file_object = open("advent.of.code.03.txt", "r")
lines = file_object.readlines()
lines[-1] = lines[-1] + '\n'

treesmap = list(map(lambda s: s[:-1], lines))

class Position:
    x = 0
    y = 0

currentpos = Position()
treescount = 0

def getnextposfunc(rightmov, downmov, length):
    def getnextpos(position):
        result = Position()
        result.x = (position.x + rightmov) % length
        result.y = position.y + downmov
        return result
    return getnextpos

def istreeposition(position, map):
    return map[position.y][position.x] == '#'

nextpos = getnextposfunc(3, 1, len(treesmap[0]))

while currentpos.y < len(treesmap) - 1:
    currentpos = nextpos(currentpos)
    treescount += (1 if istreeposition(currentpos, treesmap) else 0)

print(treescount)