import tkinter as tk  # python 3
from tkinter import font as tkfont  # python 3
# import Tkinter as tk     # python 2
# import tkFont as tkfont  # python 2

from tkinter import font as tkfont
from tkinter import *
from tkinter import font, messagebox
import random, requests, os, sys
import PySimpleGUI as sg
from nav_bar import *
from subprocess import call, Popen, PIPE
import os

class AttackVectorEleven(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        framefont = tkfont.Font(family='Calibri', size=33, weight="bold")

        main_frame = tk.Frame(self)

        highlightFont = font.Font(family='Calibri', name='appHighlightFont11', size=18)

        global phpmyadminrce
        phpmyadminrce = tk.PhotoImage(file='resources/phpmyadminrce.png')

        def load_terminal():
            p1 = Popen("exo-open --launch TerminalEmulator", stdout=PIPE, universal_newlines=True, shell=True).stdout

        def change_to_Step1():
            text = (
                "\n\nStep 1: Setup environment (the following setup was used in this demonstration)\n\n"
                "o   phpMyAdmin 4.8.1\n\n"
                "o   Windows 7\n\n"
                "o   Kali Linux\n\n"
                "o   XAMPP 7.2.0 (used to run phpMyAdmin on the Windows 7 machine)\n\n"
                "o   Metasploit\n\n"
                "o   Windows 7 and Kali Linux is configured to be in the same internal network.\n\n"
            )
            step1frame = tk.Message(main_frame, text=text, fg='black', bg='white', font=('Calibri', 20), anchor='nw',
                                    aspect=350)
            step1frame.place(rely=0.2, relx=0.2, relheight=1, relwidth=1)

        def change_to_Step2():
            text = (
                "\n\nStep 2: Walkthrough Part 1\n\n"
                "o   On the windows machine, make sure that you are running and logged into phpMyAdmin.\n\n"
                "o   Open terminal on Kali Linux and run metasploit using this command:\n"
                "     msfconsole\n\n"
                "o   Load the exploit using:\n"
                "     use exploit/multi/http/phpmyadmin_lfi_rce\n\n"
            )
            step2frame = tk.Message(main_frame, text=text, fg='black', bg='white', font=('Calibri', 20), anchor='nw',
                                    aspect=350)
            step2frame.place(rely=0.2, relx=0.2, relheight=1, relwidth=1)
            terminalButton = tk.Button(step2frame, text="Terminal", bg="#E7E7E7", fg="black", font=highlightFont,
                                       command=load_terminal, relief='flat').place(rely=0.5, relx=0.14, relheight=0.05,
                                                                                relwidth=0.1)

        def change_to_Step3():
            text = (
                "\n\nStep 3: Walkthrough Part 2\n\n"
                "o  You are now required to enter the RHOSTS (Victim's IP address)\n"
                "and TARGETURI (name of the folder you have installed phpmyadmin 4.8.1 in).\n\n"
                "o  Use the following commands to set RHOSTS and TARGETURI:\n"
                "    set RHOSTS 192.168.68.117\n"
                "    set TARGETURI /phpmyadmin4.8.1/\n\n"
                "o  (IP address and folder name is just an example. Please modify accordingly)\n\n"
                "o  Finally, run the exploit using:\n"
                "    run\n\n"
                "o  If successfull, a meterpreter session should be created and the attack is complete.\n\n"
            )
            step3frame = tk.Message(main_frame, text=text, fg='black', bg='white', font=('Calibri', 20), anchor='nw',
                                    aspect=350)
            step3frame.place(rely=0.2, relx=0.2, relheight=1, relwidth=1)

        # creates blue bar as canvas below nav bar housing label containing title of page
        title_canvas = tk.Canvas(self, bg='#64C1DA', highlightthickness=0)
        title_canvas.place(rely=0.08, relheight=0.12, relwidth=1)
        title_label = tk.Label(self, text="phpMyAdmin 4.8.1 RCE", bg='#4D6C84', fg='white',
                               anchor="c", font=framefont)
        title_label.place(rely=0.08, relheight=0.12, relwidth=1)

        allscreenframe = tk.Label(main_frame, bg='white')
        allscreenframe.place(rely=0.2, relheight=1, relwidth=1)

        sidescreenframe = tk.Label(main_frame, text="\n         Steps", bg='#E7E7E7', font=('Calibri', 20, 'bold'),
                                   anchor='nw')
        sidescreenframe.place(rely=0.2, relheight=1, relwidth=0.2)

        phpmyadminrce_label = tk.Label(main_frame, image=phpmyadminrce)
        phpmyadminrce_label.place(rely=0.36, relx=0.4)

        #Buttons
        step1btn = tk.Button(main_frame, text="Step 1", bg="#E7E7E7", fg="black", font=highlightFont,
                             command=change_to_Step1, relief='flat').place(rely=0.3, relx=0.02, relheight=0.05,
                                                                           relwidth=0.1)
        step2btn = tk.Button(main_frame, text="Step 2", bg="#E7E7E7", fg="black", font=highlightFont,
                             command=change_to_Step2, relief='flat').place(rely=0.4, relx=0.02, relheight=0.05,
                                                                           relwidth=0.1)
        step2btn = tk.Button(main_frame, text="Step 3", bg="#E7E7E7", fg="black", font=highlightFont,
                             command=change_to_Step3, relief='flat').place(rely=0.5, relx=0.02, relheight=0.05,
                                                                           relwidth=0.1)

        display_nav_bar(self, controller)
        main_frame.pack(fill='both', expand=1)


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

"""
Preprocess Chigaco Taxi dataset from BigQuery to TFRecords using TensorFlow Transform
"""

import argparse
import pandas as pd
import numpy as np
import datetime
import os
import sys
import tensorflow as tf
import json
import apache_beam as beam
import tensorflow_transform as tft
from tensorflow.python.lib.io import file_io

from tensorflow_transform.beam import impl
from tensorflow_transform.tf_metadata import dataset_schema
from tensorflow_transform.tf_metadata import dataset_metadata
from tensorflow_transform.coders import example_proto_coder
from tensorflow_transform.tf_metadata import metadata_io
from tensorflow_transform.beam.tft_beam_io import transform_fn_io, beam_metadata_io

try:
    try:
        from apache_beam.options.pipeline_options import PipelineOptions
    except ImportError:
        from apache_beam.utils.pipeline_options import PipelineOptions
except ImportError:
    from apache_beam.utils.options import PipelineOptions

import logging
logging.basicConfig()
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)

def _query_trips(start_date, end_date):
    """ Private function which returns a query string """

    query_str = """
        SELECT
            pickup_community_area,
            EXTRACT(DATE from trip_start_timestamp) as date,
            EXTRACT(HOUR from trip_start_timestamp) as hour,
            COUNT(*) as n_trips
                FROM `bigquery-public-data.chicago_taxi_trips.taxi_trips`
                WHERE trip_start_timestamp >= '{start_date}'
                AND trip_start_timestamp <'{end_date}'
                AND pickup_community_area is NOT NULL
                AND trip_start_timestamp is NOT NULL
        GROUP BY date, hour, pickup_community_area
        ORDER BY date, hour, pickup_community_area ASC
    """.format(start_date=start_date, end_date=end_date)
    # LIMIT 10

    return query_str

class ParseRow(beam.DoFn):
    """ Parse BigQuery Row """

    def process(self, element):
        yield {'pickup_community_area': str(element['pickup_community_area']),
               'date': element['date'],
               'hour': int(element['hour']),
               'n_trips': float(element['n_trips'])
               }

def _read_data_from_bq(pipeline, start_date, end_date):
    """ Read raw dataset from BigQuery """
    query_str = _query_trips(start_date, end_date)
    raw_data = (pipeline |
                "Read data from BigQuery from {} to {}".format(start_date, end_date) >> beam.io.Read(
                    beam.io.BigQuerySource(query=query_str, use_standard_sql=True)) |
                "Parse BigQuery Row from {} to {}".format(start_date, end_date) >> beam.ParDo(ParseRow()))

    return raw_data

class GroupItemsByDate(beam.CombineFn):
    """ Group taxi rides hourly for each chicago community area """

    def __init__(self, community_area_list, date_range):
        super(GroupItemsByDate, self).__init__()
        self.community_area_list = community_area_list
        self.start = date_range[0]
        self.end = date_range[1]
        self.delta = self.end - self.start

    def create_accumulator(self):
        return {}

    def add_input(self, accumulator, element):

        date = element['date']
        hour = element['hour']
        community_area = element['pickup_community_area']
        n_trips = element['n_trips']

        if date not in accumulator:
            accumulator[date] = {}
        if hour not in accumulator[date]:
            accumulator[date][hour] = {}
        if community_area not in accumulator[date][hour]:
            accumulator[date][hour][community_area] = 0

        accumulator[date][hour][community_area] += n_trips

        return accumulator

    def merge_accumulators(self, accumulators):

        # Create empty accumulator
        output = {}
        for i in range(self.delta.days + 1):

            date_key = (self.start + datetime.timedelta(days=i)
                        ).strftime('%Y-%m-%d')

            output[date_key] = {}
            for hour in range(24):
                output[date_key][hour] = {}
                for ca in self.community_area_list:
                    output[date_key][hour][ca] = 0

        # Fill empty accumulator with existing ones
        for a in accumulators:
            for date, date_dict in a.items():
                for hour, hour_dict in date_dict.items():
                    for ca in hour_dict:
                        output[date][hour][ca] += hour_dict[ca]
        return output

    def extract_output(self, output):
        """ Flatten accumulator as a dict of list """

        flattened_dict = {
            'date': [],
            'hour': [],
            'community_area': [],
            'n_trips': []
        }

        for date, date_dict in output.items():
            for hour, hour_dict in date_dict.items():
                for ca in hour_dict:
                    flattened_dict['date'].append(date)
                    flattened_dict['hour'].append(hour)
                    flattened_dict['community_area'].append(ca)
                    flattened_dict['n_trips'].append(hour_dict[ca])

        return flattened_dict

class ExtractRawTimeseriesWindow(beam.DoFn):
    """
    Sort taxi rides by ascending date for each community area
    and return time series as a sliding window

    TODO: check if this can be done with Apache Beam windowning
    """

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

    def process(self, element):

        flattened_df = pd.DataFrame(element)
        flattened_df['date'] = pd.to_datetime(flattened_df['date'])
        flattened_df['hour'] = pd.to_numeric(flattened_df['hour'])
        flattened_df['day_of_month'] = flattened_df['date'].apply(
            lambda t: t.day)
        flattened_df['day_of_week'] = flattened_df['date'].apply(
            lambda t: t.dayofweek)
        flattened_df['month'] = flattened_df['date'].apply(lambda t: t.month)
        flattened_df['week_number'] = flattened_df['date'].apply(
            lambda t: t.weekofyear)

        for ca, trips_time_series in flattened_df.groupby('community_area'):

            # force sorting
            ts_df = trips_time_series.sort_values(
                ['date', 'hour'], ascending=True)

            for i in range(0, (len(ts_df)-self.window_size-1), 1):
                window = ts_df.iloc[i:(i+self.window_size+1)]

                window_dict = {
                    'hour': None,
                    'day_of_week': None,
                    'day_of_month': None,
                    'week_number': None,
                    'month': None,
                    'n_trips': None,
                }

                for k in window_dict:
                    window_dict[k] = window[k].values[:self.window_size]

                window_dict['community_area'] = [int(ca)] * self.window_size
                window_dict['community_area_code'] = int(ca)

                # Add target
                window_dict['target'] = window['n_trips'].values[self.window_size].astype(
                    np.float32)

                yield window_dict

def _scale_temporal_feature(value, period):
    """ Scale feature between [0,1] given a period """
    scaled_value = tf.divide(tf.cast(value, tf.float32), period)

    return scaled_value

def _process_temporal_features_sin(value, period):
    """ Get the sine of a temporal feature """
    scaled_value = _scale_temporal_feature(value, period)
    value_sin = tf.math.sin(2*np.pi*scaled_value)

    return value_sin

def _process_temporal_features_cos(value, period):
    """ Get the sin of a temporal feature """
    scaled_value = _scale_temporal_feature(value, period)
    value_cos = tf.math.cos(2*np.pi*scaled_value)

    return value_cos

def _preprocess_fn(features, window_size, znorm_stats):
    """
    TFT transfom function
    This function will be used to create the preprocessing graph
    to be used further on model serving.
    """

    output_features = {}

    # Transform temporal features
    output_features['hour_sin'] = _process_temporal_features_sin(
        features['hour'], 24)
    output_features['hour_cos'] = _process_temporal_features_cos(
        features['hour'], 24)
    output_features['day_of_week_sin'] = _process_temporal_features_sin(
        features['day_of_week'], 7)
    output_features['day_of_week_cos'] = _process_temporal_features_cos(
        features['day_of_week'], 7)
    output_features['day_of_month_sin'] = _process_temporal_features_sin(
        features['day_of_month'], 31)
    output_features['day_of_month_cos'] = _process_temporal_features_cos(
        features['day_of_month'], 31)
    output_features['week_number_sin'] = _process_temporal_features_sin(
        features['week_number'], 55)
    output_features['week_number_cos'] = _process_temporal_features_cos(
        features['week_number'], 55)
    output_features['month_sin'] = _process_temporal_features_sin(
        features['month'], 12)
    output_features['month_cos'] = _process_temporal_features_cos(
        features['month'], 12)

    output_features['community_area'] = features['community_area']
    # output_features['community_area_code'] = features['community_area_code']

    # Load z-norm mean and standard deviation into tensorflow lookup tables
    # This is the way to scale each community area time series with their own parameters

    # (TENSORFLOW 1.14)
    # lookup_mean = tf.lookup.StaticHashTable(
    #     tf.lookup.KeyValueTensorInitializer(keys=[np.int64(int(i)) for i in znorm_stats['pickup_community_area']],
    #                                         values=znorm_stats['mean'],
    #                                         key_dtype=tf.int64,
    #                                         value_dtype=tf.float32),
    #     default_value=0)

    # lookup_std = tf.lookup.StaticHashTable(
    #     tf.lookup.KeyValueTensorInitializer(keys=[np.int64(int(i)) for i in znorm_stats['pickup_community_area']],
    #                                         values=znorm_stats['std'],
    #                                         key_dtype=tf.int64,
    #                                         value_dtype=tf.float32),
    #     default_value=1)

    # (TENSORFLOW 1.13.1)
    lookup_mean = tf.contrib.lookup.HashTable(
        tf.contrib.lookup.KeyValueTensorInitializer(keys=[np.int64(int(i)) for i in znorm_stats['pickup_community_area']],
                                                    values=znorm_stats['mean'],
                                                    key_dtype=tf.int64,
                                                    value_dtype=tf.float32),
        default_value=0)

    lookup_std = tf.contrib.lookup.HashTable(
        tf.contrib.lookup.KeyValueTensorInitializer(keys=[np.int64(int(i)) for i in znorm_stats['pickup_community_area']],
                                                    values=znorm_stats['std'],
                                                    key_dtype=tf.int64,
                                                    value_dtype=tf.float32),
        default_value=1)

    # Get z-norm stats for a given community area
    znorm_tensor_mean = lookup_mean.lookup(
        keys=features['community_area_code'])
    znorm_tensor_std = lookup_std.lookup(keys=features['community_area_code'])

    # Force z-norm stats tensors to be 2D
    znorm_tensor_mean = tf.reshape(znorm_tensor_mean, [-1, 1])
    znorm_tensor_std = tf.reshape(znorm_tensor_std, [-1, 1])
    target = tf.reshape(features['target'], [-1, 1])

    # Do z-norm
    output_features['n_trips'] = tf.math.divide(
        tf.math.subtract(features['n_trips'], znorm_tensor_mean), znorm_tensor_std)
    output_features['target'] = tf.math.divide(
        tf.math.subtract(target, znorm_tensor_mean), znorm_tensor_std)

    # Reshape time series tensors to be 3D (batch size, window size, feature size)
    for k in ['hour_sin', 'hour_cos', 'day_of_week_sin',
              'day_of_week_cos', 'day_of_month_sin', 'day_of_month_cos',
              'week_number_sin', 'week_number_cos',
              'month_sin', 'month_cos', 'n_trips']:

        output_features[k] = tf.reshape(
            output_features[k], [-1, window_size, 1])

    return output_features

def _get_feature_spec(window_size):
    """
    Retrieve schema for input features
    VarLenFeatures for time series leads to lots of headaches,
    so FixedLenFeature is preferable
    """

    schema_dict = {
        'hour': tf.FixedLenFeature(shape=[window_size], dtype=tf.int64, default_value=None),
        'day_of_week': tf.FixedLenFeature(shape=[window_size], dtype=tf.int64, default_value=None),
        'day_of_month': tf.FixedLenFeature(shape=[window_size], dtype=tf.int64, default_value=None),
        'week_number': tf.FixedLenFeature(shape=[window_size], dtype=tf.int64, default_value=None),
        'month': tf.FixedLenFeature(shape=[window_size], dtype=tf.int64, default_value=None),
        'community_area': tf.FixedLenFeature(shape=[window_size], dtype=tf.int64, default_value=None),
        'n_trips': tf.FixedLenFeature(shape=[window_size], dtype=tf.float32, default_value=None),
        'community_area_code': tf.FixedLenFeature(shape=[], dtype=tf.int64, default_value=None),
        'target': tf.FixedLenFeature(shape=[], dtype=tf.float32, default_value=None)
    }

    schema = dataset_metadata.DatasetMetadata(
        dataset_schema.from_feature_spec(schema_dict))
    return schema

if __name__ == '__main__':

    parser = argparse.ArgumentParser()
    parser.add_argument('--tfrecord-dir', dest='tfrecord_dir', required=True)
    parser.add_argument('--tfx-artifacts-dir',
                        dest='tft_artifacts_dir', required=True)
    parser.add_argument('--project', dest='project', required=True)
    parser.add_argument('--window-size', dest='window_size',
                        type=int, required=False, default=24)
    parser.add_argument('--start-date', dest='start_date', required=True)
    parser.add_argument('--end-date', dest='end_date', required=True)
    parser.add_argument('--split-date', dest='split_date', required=True)
    parser.add_argument('--community-area-list-path',
                        dest='community_area_list_path', required=True)
    parser.add_argument('--znorm-stats-path',
                        dest='znorm_stats_path', required=True)
    parser.add_argument('--temp-dir', dest='temp_dir',
                        required=False, default='/tmp')
    known_args, pipeline_args = parser.parse_known_args()

    pipeline_args.append('--project')
    pipeline_args.append(known_args.project)

    start_datetime = datetime.datetime.strptime(
        known_args.start_date, '%Y-%m-%d')
    end_datetime = datetime.datetime.strptime(known_args.end_date, '%Y-%m-%d')
    split_datetime = datetime.datetime.strptime(
        known_args.split_date, '%Y-%m-%d')

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

    if 'DataflowRunner' in pipeline_args:
        pipeline_args.extend(['--project', known_args.project,
                              '--temp_location', known_args.temp_dir,
                              '--save_main_session',
                              '--setup_file', './setup.py',
                              '--job_name', 'chicago-taxi-trips-bq2tfrecords-{}'.format(datetime_now_str)])

    logger.info(pipeline_args)

    pipeline_options = PipelineOptions(flags=pipeline_args)

    community_area_list = file_io.FileIO(
        known_args.community_area_list_path, "r").read().strip().split(',')
    znorm_stats = json.load(file_io.FileIO(known_args.znorm_stats_path, "r"))

    train_tfrecord_path = os.path.join(known_args.tfrecord_dir, 'train')
    eval_tfrecord_path = os.path.join(known_args.tfrecord_dir, 'eval')
    eval_raw_tfrecord_path = os.path.join(known_args.tfrecord_dir, 'eval_raw')

    # Preprocess dataset
    with beam.Pipeline(options=pipeline_options) as pipeline:
        with impl.Context(known_args.temp_dir):

            # Process training data
            raw_data_train = _read_data_from_bq(
                pipeline, known_args.start_date, known_args.split_date)

            orders_by_date_train = (raw_data_train |
                                    "Merge - train" >> beam.CombineGlobally(GroupItemsByDate(community_area_list, (start_datetime, split_datetime))))

            ts_windows_train = (orders_by_date_train | "Extract timeseries windows - train" >>
                                beam.ParDo(ExtractRawTimeseriesWindow(known_args.window_size)) |
                                "Fusion breaker train" >> beam.Reshuffle()
                                )

            ts_windows_schema = _get_feature_spec(known_args.window_size)
            norm_ts_windows_train, transform_fn = ((ts_windows_train, ts_windows_schema) |
                                                   "Analyze and Transform - train" >> impl.AnalyzeAndTransformDataset(lambda t: _preprocess_fn(t,
                                                                                known_args.window_size,
                                                                                znorm_stats)))
            norm_ts_windows_train_data, norm_ts_windows_train_metadata = norm_ts_windows_train

            _ = norm_ts_windows_train_data | 'Write TFrecords - train' >> beam.io.tfrecordio.WriteToTFRecord(
                file_path_prefix=train_tfrecord_path,
                file_name_suffix=".tfrecords",
                coder=example_proto_coder.ExampleProtoCoder(norm_ts_windows_train_metadata.schema))

            # Process evaluation data
            raw_data_eval = _read_data_from_bq(
                pipeline, known_args.split_date,  known_args.end_date)

            orders_by_date_eval = (raw_data_eval |
                                   "Merge SKUs - eval" >> beam.CombineGlobally(GroupItemsByDate(community_area_list, (split_datetime, end_datetime))))

            ts_windows_eval = (orders_by_date_eval | "Extract timeseries windows - eval" >>
                               beam.ParDo(ExtractRawTimeseriesWindow(known_args.window_size)) |
                               "Fusion breaker eval" >> beam.Reshuffle())

            norm_ts_windows_eval = (((ts_windows_eval, ts_windows_schema), transform_fn) |
                                    "Transform - eval" >> impl.TransformDataset())

            norm_ts_windows_eval_data, norm_ts_windows_eval_metadata = norm_ts_windows_eval

            _ = norm_ts_windows_eval_data | 'Write TFrecords - eval' >> beam.io.tfrecordio.WriteToTFRecord(
                file_path_prefix=eval_tfrecord_path,
                file_name_suffix=".tfrecords",
                coder=example_proto_coder.ExampleProtoCoder(norm_ts_windows_eval_metadata.schema))

            # Dump raw eval set for further tensorflow model analysis
            _ = ts_windows_eval | 'Write TFrecords - eval raw' >> beam.io.tfrecordio.WriteToTFRecord(
                file_path_prefix=eval_raw_tfrecord_path,
                file_name_suffix=".tfrecords",
                coder=example_proto_coder.ExampleProtoCoder(ts_windows_schema.schema))

            # Dump transformation graph
            _ = transform_fn | 'Dump Transform Function Graph' >> transform_fn_io.WriteTransformFn(
                known_args.tft_artifacts_dir)

    # Dump parameters to be forwarded to the next pipeline step
    with open("/train_tfrecord_path.txt", "w") as f:
        f.write(train_tfrecord_path+'-*')

    with open("/eval_tfrecord_path.txt", "w") as f:
        f.write(eval_tfrecord_path+'-*')

    with open("/eval_raw_tfrecord_path.txt", "w") as f:
        f.write(eval_raw_tfrecord_path+'*')

    with open("/znorm_stats.txt", "w") as f:
        json.dump(znorm_stats, f)

    with open("/n_areas.txt", "w") as f:
        f.write(str(len(community_area_list)+2))

    with open("/n_windows_train.txt", "w") as f:
        n_windows_train = ((
            split_datetime - start_datetime).days)*24 - known_args.window_size
        logger.info("n_windows_train {}".format(n_windows_train))
        f.write(str(n_windows_train))

    with open("/n_windows_eval.txt", "w") as f:
        n_windows_eval = ((end_datetime - split_datetime).days)*24 - \
            known_args.window_size
        logger.info("n_windows_eval {}".format(n_windows_eval))
        f.write(str(n_windows_eval))

    with open("/tft_artifacts_dir.txt", "w") as f:
        f.write(known_args.tft_artifacts_dir)


#!/usr/bin/end python
import sys
import numpy as np
from Bio import SeqIO, Seq

from ssw import (
    SSW,
    force_align,
    format_force_align
)

def rev_comp(seq):
    c = dict(zip('ATCGNatcgn', 'TAGCNtagcn'))
    return ''.join(c.get(nucleotide, '') for nucleotide in reversed(seq))

def check_polya(sequence):
    len_slice = 12
    n = len(sequence)
    if n < len_slice:
        return -1
    for i in range(n - len_slice):
        slice_ = sequence[i: i + len_slice]
        k = slice_.count('A')

        if (k >= 0.75 * len(slice_)):
            return i + slice_.find("AA")

    return -1

def get_sequence_to_check(alignment):
    if alignment.seq is None or alignment.cigartuples is None:
        return -1

    cigar_tuples = alignment.cigartuples
    clipped_len = -1
    sequence_to_check = ''
    if len(cigar_tuples) > 1 and cigar_tuples[0][0] == 5 and cigar_tuples[1][0] == 4:
        # hard clipped
        clipped_len = cigar_tuples[1][1]
    elif cigar_tuples[0][0] == 4:
        # soft clipped
        clipped_len = cigar_tuples[0][1]
    if (clipped_len != -1):
        sequence_to_check = alignment.seq[
                            :clipped_len]  # str(Seq.Seq(alignment.seq[:clipped_len]).reverse_complement()).upper()

    clipped_len = -1

    if len(cigar_tuples) > 1 and cigar_tuples[-1][0] == 5 and cigar_tuples[-2][0] == 4:
        # hard clipped
        clipped_len = cigar_tuples[-2][1]
    elif cigar_tuples[-1][0] == 4:
        # soft clipped
        clipped_len = cigar_tuples[-1][1]

    sequence_to_check_end = ''
    if (clipped_len != -1):
        sequence_to_check_end = str((alignment.seq[-clipped_len:]).upper())

    return sequence_to_check, sequence_to_check_end

def is_reverse(sequence_to_check_start, sequence_to_check_end):
    sequence_to_check_start = str(Seq.Seq(sequence_to_check_start).reverse_complement()).upper()

    if check_polya(sequence_to_check_start) != -1:
        return 1
    elif check_polya(sequence_to_check_end) != -1:
        return 0

    return -1

bins = 20
all_qualities = [[] for i in range(bins*3)]

fastq_file_pb = sys.argv[1]
fastq_file_ont = sys.argv[2]
tsv_file = sys.argv[3]

pb_seqs = SeqIO.parse(fastq_file_pb, format="fastq")
ont_seqs = SeqIO.parse(fastq_file_ont, format="fastq")

reverse_reads = set()
pb_qual = {seq.id: (str(seq.seq), seq.letter_annotations["phred_quality"])  for seq in pb_seqs}
ont_qual = {seq.id: (str(seq.seq), seq.letter_annotations["phred_quality"]) for seq in ont_seqs}
all_qualities_ont = [[] for i in range(bins*3)]

a = SSW()
with open(tsv_file) as f:
    for line in f:
        _, _, pb_read, ont_read = line.split()
        if pb_read not in pb_qual or ont_read not in ont_qual: continue
        pb_s, ont_s = pb_qual[pb_read][0], ont_qual[ont_read][0]
        pb_q, ont_q = pb_qual[pb_read][1], ont_qual[ont_read][1]
        is_rev1 = is_reverse(pb_s[:100], pb_s[-100:])
        is_rev2 = is_reverse(ont_s[:100], ont_s[-100:])
        if is_rev1 == -1 or is_rev2 == -1: continue
        if len(pb_s) <= len(ont_s) and pb_s:
            if is_rev1 == is_rev2:
                a.setRead(pb_s)
            else:
                a.setRead(rev_comp(pb_s))
            a.setReference(ont_s)
            alignment = a.align()
            pb_start, pb_end = alignment.read_start, alignment.read_end
            ont_start, ont_end = alignment.reference_start, alignment.reference_end
        elif pb_s and ont_s:
            if is_rev1 == is_rev2:
                a.setRead(ont_s)
            else:
                a.setRead(rev_comp(ont_s))
            a.setReference(pb_s)
            alignment = a.align()
            pb_start,pb_end = alignment.reference_start, alignment.reference_end
            ont_start,ont_end = alignment.read_start, alignment.read_end
        prefixq,alnq,suffixq=pb_q[:pb_start], \
                             pb_q[pb_start:pb_end],\
                             pb_q[pb_end:]

        if is_rev1 == 1:
            prefixq,alnq,suffixq=suffixq[::-1],alnq[::-1],prefixq[::-1]
        step = len(prefixq) * 1.0 / bins
        for i in range(bins):
            all_qualities[i].append(np.mean(prefixq[int(i * step):int((i + 1) * step)]))
        step = max(1, len(alnq) * 1.0 / bins)
        for i in range(bins):
            all_qualities[bins + i].append(np.mean(alnq[int(i * step):int((i + 1) * step)]))
        step = max(1, len(suffixq) * 1.0 / bins)
        for i in range(bins):
            all_qualities[2 * bins + i].append(np.mean(suffixq[int(i * step):int((i + 1) * step)]))

        prefixq,alnq,suffixq=ont_q[:ont_start], \
                             ont_q[ont_start:ont_end],\
                             ont_q[ont_end:]

        if is_rev2 == 1:
            prefixq,alnq,suffixq=suffixq[::-1],alnq[::-1],prefixq[::-1]
        step = len(prefixq)*1.0/bins
        for i in range(bins):
            all_qualities_ont[i].append(np.mean(prefixq[int(i*step):int((i+1)*step)]))
        step = max(1,len(alnq)*1.0/bins)
        for i in range(bins):
            all_qualities_ont[bins+i].append(np.mean(alnq[int(i*step):int((i+1)*step)]))
        step = max(1,len(suffixq)*1.0/bins)
        for i in range(bins):
            all_qualities_ont[2*bins+i].append(np.mean(suffixq[int(i*step):int((i+1)*step)]))

data = dict()
data['x'] = list(range(bins*3)) + list(range(bins*3))
data['Quality'] = [np.nanmean(p) for p in all_qualities] + [np.nanmean(p) for p in all_qualities_ont]
data['Quality_std'] = [np.nanstd(p) for p in all_qualities] + [np.nanstd(p) for p in all_qualities_ont]
data['Platform'] = ['PacBio'] * len(all_qualities) + ['ONT'] * len(all_qualities_ont)

with open("metaread.txt", "w") as f:
    f.write(" ".join([str(s) for s in data['x']]))
    f.write("\n")
    f.write(" ".join([str(s) for s in data['Quality']]))
    f.write("\n")
    f.write(" ".join([str(s) for s in data['Quality_std']]))
    f.write("\n")
    f.write(" ".join([str(s) for s in data['Platform']]))
    f.write("\n")


import discord

from commonbot.user import UserLookup
from config import CMD_PREFIX
from forwarder import message_forwarder

async def get_userid(ul: UserLookup, mes: discord.Message, cmd: str, args: str = "") -> tuple[int | None, bool]:
    """
    Retrieves the user referenced in the given message.

    Uses an explicit user mention if available, otherwise uses the reply thread user if the command
    is in a reply thread.

    The second return value is useful in case the message needs to be stripped to access the args -
    it indicates how many words to strip:
     - True -> the user came from the message, strip two words (the command, and user reference)
     - False -> the user came from the reply thread, strip one word (the command)

    :param mes: Message to check.
    :param cmd: Command name for help output, if no user is found.
    :param args: Command args for help output, if no user is found.
    :return: The user id (or None if found), and whether the user came from the message (as opposed to from the reply thread).
    """
    # First check message
    userid = ul.parse_id(mes)
    if userid:
        return userid, True

    # If no user in the message, check if it's a reply thread
    thread_user = message_forwarder.get_userid_for_user_reply_thread(mes)
    if thread_user is not None:
        return thread_user, False

    outside_reply_thread = f"{CMD_PREFIX}{cmd} USER {args}".strip()
    inside_reply_thread = f"{CMD_PREFIX}{cmd} {args}".strip()

    # Otherwise send an error
    await mes.channel.send(f"I wasn't able to find a user anywhere based on that message. `{outside_reply_thread}` or `{inside_reply_thread}` in a reply thread")
    return None, False


#!/usr/bin/python
# -*- coding: utf-8 -*-
import argparse
import socket
import struct
from functools import lru_cache

def arguments():
    # Handle command line arguments
    parser = argparse.ArgumentParser(description='Adventofcode.')
    parser.add_argument('-f', '--file', required=True)

    args = parser.parse_args()

    return args

class StringToIP():
    def __init__(self):
        self.reset()

    def reset(self):
        self.strings = None
        self.ips = []

    @lru_cache(maxsize=None)
    def convert_string_to_ip(self):
        for row in self.strings:
            for n in range(int(row[0]), int(row[1]) + 1):
                self.ips.append(socket.inet_ntoa(struct.pack('>L', n)))

    @lru_cache(maxsize=None)
    def convert_ip_to_string(self, ip):
        return struct.unpack('>L', socket.inet_aton(ip))[0]

def main():
    args = arguments()
    with open(args.file) as file:
        record = []
        for line in file:
            a, b = [int(i) for i in line.strip().split("-")]
            record.append((a, b))

    record.sort()
    total, ip, index = 0, 0, 0
    while ip < 2**32:
        lower, upper = record[index]
        if ip >= lower:
            if ip <= upper:
                ip = upper + 1
                continue
            index += 1
        else:
            total += 1
            ip += 1

    print(total, ip, index)

if __name__ == '__main__':
    main()


import numpy as np
import cv2
import glob

def calibrate_camera(chessboard_size, square_size, images_path):
    objp = np.zeros((chessboard_size[0] * chessboard_size[1], 3), np.float32)
    objp[:, :2] = np.mgrid[0:chessboard_size[0], 0:chessboard_size[1]].T.reshape(-1, 2)
    objp *= square_size

    objpoints = []
    imgpoints = []

    images = glob.glob(images_path)

    gray = None

    for fname in images:
        img = cv2.imread(fname)
        gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

        # 체스 보드 코너를 찾자
        ret, corners = cv2.findChessboardCorners(gray, chessboard_size, None)

        if ret == True:
            objpoints.append(objp)
            corners2 = cv2.cornerSubPix(gray, corners, (11, 11), (-1, -1), (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001))
            imgpoints.append(corners2)
            img = cv2.drawChessboardCorners(img, chessboard_size, corners2, ret)
            cv2.imshow('img', img)
            cv2.waitKey(200)

    cv2.destroyAllWindows()
    ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(objpoints, imgpoints, gray.shape[::-1], None, None)

    return ret, mtx, dist

def project_points(points_3d, rvecs, tvecs, camera_matrix):
    R, _ = cv2.Rodrigues(rvecs) # 회전 벡터 -> 회전 행렬
    RT = np.concatenate((R, tvecs), axis=1) # 외부 매개변수 행렬
    P = np.matmul(camera_matrix, RT) # 투영 행렬

    points_3d_homogeneous = np.concatenate((points_3d, np.ones((points_3d.shape[0], 1))), axis=1) # 동차 좌표로 변환
    points_2d = np.matmul(P, points_3d_homogeneous.T).T # 평면에 투영

    # 정규화한다
    for i in range(points_2d.shape[0]):
        points_2d[i, 0] /= points_2d[i, 2]
        points_2d[i, 1] /= points_2d[i, 2]
        points_2d[i, 2] = 1

    return points_2d[:, :2]

def draw_cube(img, imgpts):
    imgpts = np.int32(imgpts).reshape(-1,2)
    color = (255, 0, 0)
    lines = [(0,1), (1,2), (2,3), (3,0), (0,4), (1,5), (2,6), (3,7), (4,5), (5,6), (6,7), (7,4)]

    for i, j in lines:
        img = cv2.line(img, tuple(imgpts[i]), tuple(imgpts[j]), color, 3)

    return img

# 카메라 캘리브레이션 설정
# camera_index = cv2.CAP_DSHOW # 노트북 웹캠
camera_index = cv2.CAP_DSHOW + 1 # USB 웹캠
chessboard_size = (4,4)
square_size = 1.0
calibration_images_path = 'calibration_images/*.jpg'  # 캘리브레이션 이미지 경로

ret, mtx, dist = calibrate_camera(chessboard_size, square_size, calibration_images_path)

# 웹캠 준비
camera = cv2.VideoCapture(camera_index)

while(True):
    # 캡처
    ret, frame = camera.read()

    if ret == True:
        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

        # 체스보드 코너 찾기
        ret, corners = cv2.findChessboardCorners(gray, chessboard_size, None)

        if ret == True:
            objp = np.zeros((chessboard_size[0] * chessboard_size[1], 3), np.float32)
            objp[:, :2] = np.mgrid[0:chessboard_size[0], 0:chessboard_size[1]].T.reshape(-1, 2)
            objp *= square_size

            # 큐브 꼭지점
            axis = np.float32([[0,0,0], [1,0,0], [1,1,0], [0,1,0],
                               [0,0,-1],[1,0,-1],[1,1,-1],[0,1,-1]])

            # 회전 및 변환 벡터를 계산
            _, rvecs, tvecs = cv2.solvePnP(objp, corners, mtx, dist)

            # 큐브의 꼭지점을 평면에 투영
            imgpts = project_points(axis, rvecs, tvecs, mtx)

            frame = draw_cube(frame, imgpts)

        cv2.imshow('frame', frame)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    else:
        break

camera.release()
cv2.destroyAllWindows()


from django.db.models import QuerySet
from gforce.models import CustomUser, Otp
from gforce.repositories.base import BaseRepository

class CustomUserRepository(BaseRepository):
    """
    Repository class for managing custom user data.
    """
    def __init__(
        self,
        *args,
        item: CustomUser = None,
        many: bool = False,
        item_list: QuerySet = None,
        **kwargs,
    ):
        super(CustomUserRepository, self).__init__(
            *args, model=CustomUser, item=item, many=many, item_list=item_list, **kwargs
        )

    @staticmethod
    def create_user(user_data: dict):
        """
        Registers/Creates a new user.

        Args:
            user_data (dict): Validated form data from the incoming request.

        Returns:
            Union[bool, tuple]: A tuple indicating the success status and either the created user object or an error message.
        """
        mobile_number = user_data.get("mobile")
        first_name = user_data.get("fname")
        last_name = user_data.get("lname")
        email = user_data.get("email")
        gender = user_data.get("gender")
        if type(email) == list:
            first_name = first_name[0]
            last_name = last_name[0]
            email = email[0]
            mobile_number = mobile_number[0]
            gender = gender[0]
        if not email or not first_name:
            return False, "Invalid params"
        user = CustomUser.objects.filter(email=email)
        if user:
            return False, "User already exists with provided email or mobile number"
        user = CustomUser.objects.create(
            mobile_number=mobile_number,
            first_name=first_name,
            last_name=last_name,
            email=email,
            gender=gender,
        )
        user.save()
        return True, user

    @staticmethod
    def create_otp_record(email, otp):
        """
        Creates an OTP record for the user.

        Args:
            email (str): Email address of the user.
            otp (str): One-Time Password generated for the user.

        Returns:
            Union[bool, tuple]: A tuple indicating the success status and either the created OTP record object or an error message.
        """
        otp_record = Otp.objects.filter(email=email).first()
        if otp_record:
            otp_record.otp = otp
            otp_record.save()
        else:
            otp_record = Otp.objects.create(email=email, otp=otp)
            otp_record.save()
        return True, otp_record

    @staticmethod
    def verify_otp(otp):
        """
        Verifies the provided OTP.

        Args:
            otp (str): One-Time Password to be verified.

        Returns:
            Union[bool, tuple]: A tuple indicating the success status and either a success message or an error message.
        """
        otp_record = Otp.objects.filter(otp=otp)
        if otp_record:
            return True, "Otp verified successfully"
        return False, "Invalid Otp"

    @staticmethod
    def validate_user(email):
        """
        Validates a user based on the provided email address.

        Args:
            email (str): Email address of the user.

        Returns:
            Union[bool, tuple]: A tuple indicating the success status and either a success message or an error message.
        """
        user = CustomUser.objects.filter(email=email)
        if not user:
            return False, "User not found"
        return True, "User validated successfully"


from mpi4py import MPI
import numpy as np
import adios2

# MPI
comm = MPI.COMM_WORLD
rank = comm.Get_rank()
size = comm.Get_size()

# User data
Nx = 10
Ny = 10

count = [Nx, Ny]
start = [rank * Nx, 0]
shape = [size * Nx, Ny]

temperatures = np.zeros(count, dtype=np.int32)

for i in range(0, Nx):
    for j in range(0, Ny):
        temperatures[i, j] = (start[0] + i) * shape[1] + (j + start[1])

# ADIOS write
adios = adios2.ADIOS(comm)
ioWrite = adios.DeclareIO("ioWriter")

varTemperature = ioWrite.DefineVariable("temperature2D", temperatures, shape,
                                        start, count, adios2.ConstantDims)
ioWrite.SetEngine("NULL")

nullWriter = ioWrite.Open('NULL_py.bp', adios2.Mode.Write)

assert (nullWriter.Type() == "NullWriter")

status = nullWriter.BeginStep()
assert (status == adios2.StepStatus.OK)

nullWriter.Put(varTemperature, temperatures)
nullWriter.EndStep()
nullWriter.Close()

# ADIOS2 read
ioRead = adios.DeclareIO("ioReader")
ioRead.SetEngine("null")
nullReader = ioRead.Open('NULL_py.bp', adios2.Mode.Read, MPI.COMM_SELF)

assert (nullReader.Type() == "NullReader")

inTemperatures = np.zeros(1, dtype=np.int32)

status = nullReader.BeginStep()
assert (status == adios2.StepStatus.EndOfStream)

var_inTemperature = ioRead.InquireVariable("temperature2D")

if (var_inTemperature is True):
    raise ValueError('var_inTemperature is not False')

# nullReader.Get(var_inTemperature, inTemperatures)

nullReader.PerformGets()
nullReader.EndStep()
nullReader.Close()


from apps.profiles.models import Profile, Post
from apps.tags.models import Tag
from libs import navHelpers, siteHelpers
from libs.siteEnums import Species, Tags, System

def siteProcessor(request):
    background = 'media/backgrounds/bg-' + str(request.session['page_background']) + '.jpg'
    request.session['page_banner'] = siteHelpers.bannerSelect(request.session['page_banner'])
    banner = 'media/banners/banner-' + str(request.session['page_banner']) + '.jpg'
    site_context = {
        'path': request.get_full_path(),
        'human_key': System.humanKey,
        'is_human': request.session['is_human'],
        'background': background,
        'banner': banner,
        'mb_size': System.mb
    }
    return site_context

def sessionProcessor(request):
    session_context = {}

    # CONSTANTS
    session_context['predator'] = Species.predator
    session_context['forager'] = Species.forager

    # SESSION KEYS
    tprof = Profile.objects.select_related('post_set').get(id=request.session['session_id'])
    session_context['session_object'] = tprof
    session_context['session_id'] = tprof.id
    session_context['session_name'] = tprof.fullName
    session_context['session_icon'] = tprof.prettyIcon
    session_context['session_species'] = tprof.species
    session_context['session_energy'] = tprof.energy
    session_context['species_meals'] = tprof.meals
    session_context['session_lock'] = request.session['session_lock']
    session_context['session_activity'] = tprof.recentActivity

    # HANDLING FOOD
    session_context['is_pred_prey'] = siteHelpers.isPredatorPrey(tprof.species)
    session_context['prey'] = siteHelpers.isPrey(request)
    session_context['kosher'] = siteHelpers.isKosher(request)
    session_context['is_post'] = siteHelpers.isPost(request)

    # HANDLING FRIENDS
    session_context['is_friend'] = siteHelpers.isFriend(request)

    # NAVIAGTION
    nav_context = {}
    currentPosition = request.session['nav_position']
    nav_context['nav_position'] = currentPosition
    nav_context['selected_trail'] = request.session['selected_trail']
    if nav_context['selected_trail'] != 'no':
        trail = Tag.objects.get(name=nav_context['selected_trail'])
        nav_context['selected_trail_id'] = trail.id
    else:
        trail = None

    # NAVIGATION KEYS
    nav_context['rand_profile'] = navHelpers.randomProfile(tprof, trail)
    nav_context['prev_profile'] = navHelpers.previousProfile(currentPosition, tprof, trail)
    nav_context['next_profile'] = navHelpers.nextProfile(currentPosition, tprof, trail)

    return dict(session_context.items() + nav_context.items())

def messageProcessor(request):
    message_context = {}
    message_context['message_flag'] = False
    current_notice = request.session['notification']
    if current_notice:
        message_context['message_flag'] = True
        message_context['message'] = siteHelpers.getTheMessage(request, current_notice)
    request.session['notification'] = None

    return message_context


from indico_toolkit.indico_wrapper import Datasets, Download
from indico_toolkit.pipelines import FileProcessing
from indico_toolkit import create_client

DATASET_ID = 1234
HOST = "app.indico.io"
API_TOKEN_PATH = "./indico_api_token.txt"

# Instantiate the datasets class
client = create_client(HOST, API_TOKEN_PATH)
datasets = Datasets(client, DATASET_ID)
downloader = Download(client)
"""
Example 1:

Upload files to an existing dataset in batches
"""
# Collect files to upload
fp = FileProcessing()
fp.get_file_paths_from_dir("./datasets/disclosures/")

# Upload files to dataset in batches
for paths in fp.batch_files(batch_size=2):
    datasets.add_files_to_dataset(paths)
    print(f"Uploaded {len(paths)} files")


from os import abort
from pymongo import MongoClient

def print_results(values):
    print("{0} | {1} | {2} | {3}".format(values[0], values[1], values[2], values[3]))

# creates an index based on the field passed in and puts it in ascending order.
def create_index(host, port, udb, ucollection, field):
    connection = MongoClient(host, port)
    db = connection[udb]
    collection = db[ucollection]

    collection.create_index(field)
    print('Index created for AuctionID...')

def find_bid(host, port, udb, ucollection, bid_id):
    connection = MongoClient(host, port)
    db = connection[udb]
    collection = db[ucollection]

    pipeline = [
        {"$match": {"AuctionID": bid_id}},
        {"$project": {"AuctionID": 1, "AuctionTitle": 1, "Fund": 1, "AuctionFeeTotal": 1, "_id": 0}}
    ]

    # Convert the pipeline values to a printable format
    cursor = collection.aggregate(pipeline)
    results = list(cursor)

    if not results:
        print("No bid found....")
        return None

    # Pull the dictionary object out of the list
    results_string = results[0]
    listed_values = []

    for i in results_string:
        value = results_string[i]
        listed_values.append(value)

    return listed_values

def create_bid(host, port, udb, ucollection):
    connection = MongoClient(host, port)
    db = connection[udb]
    collection = db[ucollection]

    # Counter for the check_bid below to only run for the first iteration
    counter = 0

    field_list = ["AuctionID", "AuctionTitle", "Fund", "AuctionFeeTotal"]
    new_bid = []

    for field in field_list:
        user_value = input("Enter {0}:  ".format(field))
        new_bid.append(user_value)

        if counter is 0:
            # Check if bid exists with AuctionID and stop creation if it does.
            check_bid = find_bid(host, port, udb, ucollection, int(user_value))
            counter += 1

        if check_bid:
            print("Bid with that AuctionID already exists...")
            return None

    bid = {"AuctionID": int(new_bid[0]),
           "AuctionTitle": new_bid[1],
           "Fund": new_bid[2],
           "AuctionFeeTotal": new_bid[3]}

    try:
        collection.insert_one(bid).inserted_id
        find_bid(host, port, udb, ucollection, int(new_bid[0]))
    except:
        abort(400, str("Error in process. Aborted."))

def update_bid(host, port, udb, ucollection, bid_id):
    connection = MongoClient(host, port)
    db = connection[udb]
    collection = db[ucollection]

    field_list = ["AuctionTitle", "Fund", "AuctionFeeTotal"]

    # Check if bid exists before trying to update it.
    check_bid = find_bid(host, port, udb, ucollection, int(bid_id))
    if not check_bid:
        return None

    for field in field_list:
        # Loop here is for input validation.
        loop_condition = True
        while loop_condition:
            user_choice = input("Update {0}... (Y/N) ".format(field))
            if user_choice is 'Y' or user_choice is 'y':
                try:
                    user_value = input("Enter a new value: ")
                    update = {field: user_value}
                    collection.update_one({"AuctionID": bid_id}, {"$set": update})
                except:
                    abort(400, str("Error in process. Aborted."))

                loop_condition = False
            elif user_choice is 'N' or user_choice is 'n':
                loop_condition = False
            else:
                print("Invalid input, please enter valid input")

def delete_bid(host, port, udb, ucollection, bid_id):
    connection = MongoClient(host, port)
    db = connection[udb]
    collection = db[ucollection]

    try:
        collection.delete_one({'AuctionID': bid_id})
        print("Bid successfully removed from the database")
    except:
        abort(400, str("Error in process. Aborted."))


import numpy as np
import gurobipy as gp
from gurobipy import GRB
from map import get_trip_time_matrix

class Controller(object):
    def __init__(self, num_stations: int, soc: int, horizon):
        self.n = num_stations
        self.horizon = horizon
        self.soc = soc
        # Type: np.array, trip time between every two stations
        self.tau = get_trip_time_matrix()
        self.tau_max = np.amax(self.tau, axis=1)
        # Charging speed relative to tau
        self.tau_c = 4
        # Depleting speed relative to tau, per 8 min consumes 2 kWh
        decharge = 1
        self.delta_b = self.tau * decharge
        # Tuning weights
        self.Vd = 1000
        self.Vr = 5
        self.Vx = 5
        self.Vp = 5
        self.Vc = 1

    def compute_actions(self, veh_idle, veh_charging, veh_occupied, reqs: np.array):
        # Number of requests from i to j in the horizon of the controller
        requests = np.copy(reqs)

        # Build a list of tuple oddtb (origin, destination, destination, time, SOC),
        # a list of tuple odtb (origin, destination, time, SOC) for rebalancing
        # a list of tuple otb (origin, time, SOC) for charging vehicles
        # and a list of tuple odt for request counting
        list_ijtb = []
        list_itb = []
        list_ijt = []
        list_ib = []
        for i in range(self.n):
            for j in range(self.n):
                for t in range(self.horizon + 1):
                    odt = i,j,t
                    list_ijt.append(odt)
                    for b in range(self.soc):
                        odtb = i,j,t,b
                        list_ijtb.append(odtb)

        for i in range(self.n):
            for b in range(self.soc):
                ob = i,b
                list_ib.append(ob)
                for t in range(self.horizon + 1):
                    otb = i,t,b
                    list_itb.append(otb)

        itb = gp.tuplelist(list_itb)
        ijt = gp.tuplelist(list_ijt)
        ijtb = gp.tuplelist(list_ijtb)

        mpc = gp.Model(name = 'AMoD_EV MILP')

        # objective function
        obj = 0
        # Control variables
        r = mpc.addVars(ijtb, vtype = GRB.INTEGER, name = 'r')
        c = mpc.addVars(itb, vtype = GRB.INTEGER, name = 'c')
        x_zo = mpc.addVars(ijtb, vtype = GRB.INTEGER, name = 'x_zo')
        d = mpc.addVars(ijt, vtype = GRB.INTEGER, lb = 0, name = 'd')

        mpc.addConstrs((x_zo[i, i, t, b] == 0 for i, t, b in itb), name = 'disallowed')

        for t in range(0, self.horizon + 1):
            for i in range(self.n):
                if t >= self.tau_max[i]:
                    available_j_0 = [j for j in range(self.n)]
                else:
                    available_j_0 = np.where((t - self.tau[i, :]) >= 0)[0].tolist()
                # for b = 0
                mpc.addConstrs((r[i, j, t, 0] == 0 for j in range(self.n)), name = 'disallowed')
                mpc.addConstrs((x_zo[i, j, t, 0] == 0 for j in range(self.n)), name = 'disallowed')
                if t == 0:
                    mpc.addConstr(veh_idle[i, 0, 0] + veh_charging[i, 0] == r[i, i, 0, 0] + c[i, 0, 0], name = 'Initial vaccant veh conservation')
                if t > 0:
                    mpc.addConstr(veh_idle[i, t, 0] == r[i, i, t, 0]+ c[i, t, 0], name = 'Initial vaccant veh conservation')

                for b in range(1, self.soc):
                    available_j = [j for j in available_j_0 if (b + self.delta_b[i, j]) < self.soc]
                    reachable_j = [j for j in range(self.n) if (b - self.delta_b[i, j]) > 0]
                    unreachable_j = [j for j in range(self.n) if j not in reachable_j]

                    mpc.addConstrs((r[i, j, t, b] == 0 for j in unreachable_j), name = 'disallowed')
                    mpc.addConstrs((x_zo[i, j, t, b] == 0 for j in unreachable_j), name = 'disallowed')

                    left_sum0 = 0
                    right_sum0 = 0
                    for j in reachable_j:
                        right_sum0 += r[i, j, t, b] + x_zo[i, j, t, b]
                    if t == 0:
                        mpc.addConstr(veh_idle[i, 0, b] + veh_charging[i, b] == right_sum0 + c[i, 0, b], name = 'Initial vaccant veh conservation')

                    # Constraints during the horizon
                    else:
                        for j in available_j:
                            left_sum0 += r[j, i, t-self.tau[i, j], b+self.delta_b[i, j]] + x_zo[j, i, t-self.tau[i, j], b+self.delta_b[i, j]]
                        c_sum = 0
                        if b == self.soc - 1:
                            for k in range(b):
                                if k + self.tau_c >= b:
                                    c_sum += c[i, t - 1, k]
                        if self.tau_c <= b < self.soc - 1:
                            c_sum = c[i, t - 1, b - self.tau_c]
                        mpc.addConstr(veh_idle[i, t, b] + c_sum + left_sum0 == c[i, t, b] + right_sum0, name = 'vaccant veh conservation')

                for j in range(self.n):
                    if t == 0:
                        mpc.addConstr(d[i, j, 0] == requests[i, j, 0] - x_zo.sum(i, j, 0, '*'), name = 'Remain requests t=0')
                        obj -= self.Vd * d[i, j, 0] + self.tau[i, j] * (self.Vr * r.sum(i, j, 0, '*') + self.Vx * x_zo.sum(i, j, 0, '*'))
                    else:
                        mpc.addConstr(d[i, j, t] == requests[i, j, t] + d[i, j ,t-1] - x_zo.sum(i, j, t, '*'), name = 'Remain requests')
                        obj -= self.Vd * d[i, j, t] + self.tau[i, j] * (self.Vr * r.sum(i, j, t, '*') + self.Vx * x_zo.sum(i, j, t, '*'))
                obj -= self.Vc * c.sum(i, t, '*')

        mpc.setObjective(obj, GRB.MAXIMIZE)
        mpc.Params.MIPFocus = 3
        mpc.Params.MIPGap = 0.01
        # Verify model formulation and save as MPS file
        mpc.write('AMoD_EV MILP.lp')
        # Run optimization engine
        mpc.optimize()

        # Output control policy
        x_zo_0 = np.zeros((self.n, self.n, self.soc))
        x_so_0 = np.zeros((self.n, self.n, self.n, self.soc))
        p_zo_0 = np.zeros((self.n, self.n, self.n, self.soc))
        p_so_0 = np.zeros((self.n, self.n, self.n, self.soc))
        r_0 = np.zeros((self.n, self.n, self.soc))
        c_0 = np.zeros((self.n, self.soc))

        for i in range(self.n):
            for b in range(self.soc):
                c_0[i, b] = c[i, 0, b].x
                for j in range(self.n):
                    r_0[i, j, b] = r[i, j, 0, b].x
                    x_zo_0[i, j, b] = x_zo[i, j, 0, b].x

        print('c_0 = ', np.where(c_0 != 0))
        print('r_0 = ', np.where(r_0 != 0))
        print('x_zo_0 = ', np.where(x_zo_0 != 0))

        return x_zo_0, x_so_0,p_zo_0, p_so_0, r_0, c_0


# -*- coding: utf-8 -*-
"""
Created on Sat Nov 23 19:25:20 2013

@author: yuankunluo
"""

def csvHandler(csv):
    """Handle all informations from csv.
    """
    data = readCsv(csv)
    # cut the head of csv out
    head = data[0]
    data = data[1:]
    # use a list to store usefull information
    result = []
    for tweets in data:
        time = tweets[3]
        tid = tweets[1]
        text = tweets[0]
        client = tweets[4]
        retweets = tweets[2]
        hashtags = tweets[5]
        result.append((time, tid,text,client,retweets,hashtags))
    result = dataBreaker(result)
    return result

def readCsv(c):
    """To read this csv data
    """
    import csv
    with open(c, "rb") as f:
        data = csv.reader(f,delimiter=';',quotechar='\"')
        result = []
        for row in data:
            result.append(row)
    return result

def dataBreaker(dataList):
    times = []
    hashtags = []
    words = []
    clients = []
    for data in dataList:
        time = data[0]
        word = data[2].split(" ")
        client = data[3]
        hashtag = data[5].split(" ")
        times.append(time)
        hashtags.extend(hashtag)
        words.extend(word)
        clients.append(client)
    return {u'times':times,u'hashtags': hashtags,
            u'words': words,'clients': clients}


import pygame
from Player import Player
from Monster import Mummy, Alien
from CometEvent import CometFallEvent
from Sounds import  SoundManager

class Game:
    def __init__(self):
        self.isPlaying = False
        #self.isPlaying = True
        self.allPlayers = pygame.sprite.Group()
        self.player = Player(self)
        self.allPlayers.add(self.player)
        self.pressed = {}
        self.allMonsters = pygame.sprite.Group()
        self.cometEvent = CometFallEvent(self)
        self.score = 0
        self.font = pygame.font.SysFont("monospace", 16)
        self.soundManager = SoundManager()

    def addScore(self, points=10):
        self.score += points
    def start(self):
        self.isPlaying = True
        self.spawnMonster(Mummy)
        self.spawnMonster(Mummy)
        self.spawnMonster(Alien)
    def gameOver(self):
        self.allMonsters = pygame.sprite.Group()
        self.cometEvent.allComets = pygame.sprite.Group()
        self.cometEvent.resetPercent()
        self.player.health = self.player.maxHealth
        self.isPlaying = False
        self.score = 0
        self.soundManager.play("gameOver")
    def update(self,screen):
        #score

        #font = pygame.font.Font("/asste/monospace", 16)
        scoreText = self.font.render(f"Score : {self.score}",1,(0,0,0))
        screen.blit(scoreText,(20,20))
        screen.blit(self.player.image, self.player.rect)
        self.player.updateHeathBar(screen)
        self.player.updateAnimation()
        self.cometEvent.updateBar(screen)
        for projectile in self.player.allProjectiles:
            projectile.move()
        for monster in self.allMonsters:
            monster.forward()
            monster.updateAnimation()
            monster.updateHeathBar(screen)
        for comet in self.cometEvent.allComets:
            comet.fall()
        self.allMonsters.draw(screen)
        self.player.allProjectiles.draw(screen)
        self.cometEvent.allComets.draw(screen)
        if self.pressed.get(pygame.K_RIGHT) and self.player.rect.x + self.player.rect.width < screen.get_width():
            self.player.move("right")
        if self.pressed.get(pygame.K_LEFT) and self.player.rect.x > 0:
            self.player.move("left")
    def spawnMonster(self,monsterClassName):
        self.allMonsters.add(monsterClassName.__call__(self))

    def checkCollison(self, sprite, group):

        return pygame.sprite.spritecollide(sprite, group, False, pygame.sprite.collide_mask)


from tkinter import *
import tkinter
import utils
from components.SearchBox import SearchBox
from components.TextInput import IntegerInput, TextInput, DoubleInput
from defines import DataType

class FormField:
    def __init__(self, field_name, data_type, must_fill=True, is_enabled=True, data=None):
        self.field_name = field_name
        self.data_type = data_type
        self.must_fill = must_fill
        self.is_enabled = is_enabled
        self.data = data

class Form:
    def __init__(self, parent, form_fields, submit_button_text, submit_callback=None, values_to_populate=None):
        self.parent = parent
        self.form_fields = form_fields
        self.submit_button_text = submit_button_text
        self.submit_callback = submit_callback
        self.values_to_populate = values_to_populate
        self.frame = None
        self.render()

    def render(self):
        if self.frame is None:
            self.frame = Frame(self.parent)
            self.frame.grid()
        else:
            for item in self.frame.winfo_children():
                item.destroy()
        self.text_boxes = []

        next_row = None
        for index in range(len(self.form_fields)):
            field = self.form_fields[index]
            label = field[0]
            data_type = field[1]
            must_fill = field[2]
            enabled = field[3]
            if data_type is DataType.INT:
                text_box = IntegerInput(self.frame, index, 0, label, must_fill, enabled,
                                        value=self.values_to_populate[index] if self.values_to_populate is not None else None)
            elif data_type is DataType.DOUBLE:
                text_box = DoubleInput(self.frame, index, 0, label, must_fill, enabled,
                                       value=self.values_to_populate[index] if self.values_to_populate is not None else None)
            elif data_type is DataType.TEXT:
                text_box = TextInput(self.frame, index, 0, label, must_fill, enabled,
                                     value=self.values_to_populate[index] if self.values_to_populate is not None else None)
            elif data_type is DataType.SEARCH_BOX:
                names = field[4]
                text_box = SearchBox(self.frame, index, 0, label, names, must_fill)

            self.text_boxes.append(text_box)
            next_row = index + 1

        self.submit_button = tkinter.Button(self.frame, text=self.submit_button_text, width=20,
                                            font=('Arial', 16, 'bold'),
                                            command=self.on_submit)

        self.submit_button.grid(sticky=E, row=next_row, column=1, pady=5, padx=0)

    def on_submit(self):
        submitted_values = []
        for text_box in self.text_boxes:
            ok, reason = text_box.finalize()
            if not ok:
                tkinter.messagebox.showerror("Input Error", reason)
                return
            submitted_values.append(text_box.get())

        if self.submit_callback is not None:
            self.submit_callback(submitted_values)


import sys
sys.stdin = open('input/boj_2748_피보나치 수 2.txt', 'r')

N = int(input())

result = [0]*(N+1)
result[1] = 1

for i in range(2, N+1):
    result[i] = result[i-1] + result[i-2]

print(result[N])


import numpy as np
import cv2

# input image array
img = np.array([[3, 4, 8, 1, 7],
                [9, 4, 2, 1, 6],
                [7, 8, 8, 1, 1]], np.uint8)

# kernel array
kernel = np.array([[0, 1, 0],
                   [1, 1, 0],
                   [0, 1, 0]], np.uint8)

# performing closing using dilation followed by erosion
dilation = cv2.dilate(img,kernel,iterations = 1)
erosion = cv2.erode(dilation,kernel,iterations = 1)

# performing closing using its function
closing = cv2.morphologyEx(img, cv2.MORPH_CLOSE, kernel)

print(erosion)
print(closing)
print(erosion==closing)

# Conclusion
# Closing is just another way of saying dilation followed by erosion


from sklearn.svm import SVC
import pandas as pd
import numpy as np
import gc, pickle, time
import openpyxl
from skopt import gp_minimize, dummy_minimize
from skopt.space import Real, Integer
from skopt.utils import use_named_args
from skopt.plots import plot_convergence
from sklearn.metrics import matthews_corrcoef, mean_squared_error
# Own Scripts
# from own_package.svr import  run_svr
from own_package.others import print_array_to_excel, create_excel_file, print_df_to_excel

class SVMmodel:
    def __init__(self, fl, gamma=1):
        # Model setup
        self.features_dim = fl.features_dim
        self.labels_dim = fl.labels_dim  # Assuming that each task has only 1 dimensional output
        self.model = SVC(kernel='rbf', gamma=gamma, probability=True)  # Set up SVM radial basis model

    def train_model(self, fl):
        training_features = fl.features
        training_labels = fl.labels
        self.model.fit(training_features, training_labels)
        return self

    def predict(self, eval_fl):
        features = eval_fl.features
        y_pred = self.model.predict(features)
        return y_pred

def run_classification(grid_fl_dir, write_dir, gamma):
    # Load grid fl
    with open(grid_fl_dir, 'rb') as handle:
        fl = pickle.load(handle)
    # Create 10 fold for cross validation
    fl_store = fl.create_kf(k_folds=10, shuffle=True)
    # Run k model instance to perform skf
    # Results dataframe has the columns: ['idx', 'fold', 'CNT', 'PVA', 'Label', 'Prediction']
    # For each fold, append the fold information to the following lists:
    val_idx = []
    folds = []
    val_features = []
    val_labels = []
    predicted_labels_store = []
    # fl_store is a 10 item list where each item is a tuple containing the train and val fl
    for fold, fl_tuple in enumerate(fl_store):
        instance_start = time.time()
        (ss_fl, i_ss_fl) = fl_tuple  # ss_fl is training fl, i_ss_fl is validation fl
        # Train model
        model = SVMmodel(fl=ss_fl, gamma=gamma)
        model.train_model(fl=ss_fl)
        # Evaluation
        predicted_labels = model.predict(i_ss_fl)
        # Saving model
        save_model_name = write_dir + '/models/svm_' + str(fold + 1) + '.pkl'
        print('Saving instance {} model in {}'.format(fold + 1, save_model_name))
        with open(save_model_name, 'wb') as handle:
            pickle.dump(model, handle, protocol=pickle.HIGHEST_PROTOCOL)
        # Preparing data to put into new_df that consists of all the validation dataset and its predicted labels
        val_idx.extend(i_ss_fl.idx)
        folds.extend([fold] * i_ss_fl.count)  # Make a col that contains the fold number for each example
        if len(val_features):
            val_features = np.concatenate((val_features, i_ss_fl.features), axis=0)
        else:
            val_features = i_ss_fl.features
        val_labels.extend(i_ss_fl.labels)
        predicted_labels_store.extend(predicted_labels)
        # Printing one instance summary.
        instance_end = time.time()
        print(
            '\nFor k-fold run {} out of {}. Each fold has {} examples. Time taken for '
            'instance = {}\n'
            '####################################################################################################'
                .format(fold + 1, 10, i_ss_fl.count, instance_end - instance_start))

    # Calculating metrics based on complete validation prediction
    mcc = matthews_corrcoef(y_true=val_labels, y_pred=predicted_labels_store)

    # Creating dataframe to print into excel later.
    results_df = np.concatenate((np.array(folds)[:, None],  # Convert 1d list to col. vector
                             val_features,
                             np.array(val_labels)[:, None],
                             np.array(predicted_labels_store)[:, None]) , axis=1)
    headers = ['folds'] + \
              ['CNT', 'PVA'] + \
              ['Labels'] + \
              ['Prediction']
    # val_idx is the original position of the example in the data_loader
    results_df = pd.DataFrame(data=results_df, columns=headers, index=val_idx)
    # Create excel file and print results to excel
    excel_file = create_excel_file(f'{write_dir}/classifier_results.xlsx')
    print('Writing into' + excel_file)
    wb = openpyxl.Workbook()
    # Create results sheet
    wb.create_sheet('results')
    ws = wb['results']
    # Print results df
    print_df_to_excel(df=results_df, ws=ws)
    # Writing hyperparameter information at the side
    start_col = len(results_df.columns) + 3
    headers = ['mcc', 'gamma']
    values = [mcc, gamma]
    print_array_to_excel(np.array(headers), (1, start_col + 1), ws, axis=1)
    print_array_to_excel(np.array(values), (2, start_col + 1), ws, axis=1)
    wb.save(excel_file)
    wb.close()

def svm_hparam_opt(grid_fl_dir, total_run, write_excel_dir):
    with open(grid_fl_dir, 'rb') as fp:
        fl = pickle.load(fp)

    run_count = 0
    gamma = Real(low=0.1, high=300, name='gamma')
    dimensions = [gamma]
    default_parameters = [130]

    fl_store = fl.create_kf(k_folds=10, shuffle=True)

    @use_named_args(dimensions=dimensions)
    def fitness(gamma):
        nonlocal run_count, fl_store
        run_count += 1
        # Run k model instance to perform skf
        predicted_labels_store = []
        val_labels = []
        for fold, fl_tuple in enumerate(fl_store):
            (ss_fl, i_ss_fl) = fl_tuple  # ss_fl is training fl, i_ss_fl is validation fl

            # Training
            model = SVMmodel(fl=ss_fl, gamma=gamma)
            model.train_model(fl=ss_fl)

            # Evaluation
            predicted_labels = model.predict(i_ss_fl).flatten().tolist()
            predicted_labels_store.extend(predicted_labels)
            val_labels.extend(i_ss_fl.labels.flatten().tolist())

        # Calculating metrics based on complete validation prediction
        mcc = matthews_corrcoef(y_true=val_labels, y_pred=predicted_labels_store)
        if run_count % 10 == 0:  # Print every 10 iteration
            print(f'Run Number {run_count}')
        return -mcc

    search_result = gp_minimize(func=fitness,
                                dimensions=dimensions,
                                acq_func='EI',  # Expected Improvement.
                                n_calls=total_run,
                                x0=default_parameters)
    print('Best Loss = {}'.format(search_result.fun))
    print('Best Gamma = {}'.format(search_result.x[0]))
    x = [x[0] for x in search_result.x_iters]
    results = pd.DataFrame([x] + [(-search_result.func_vals).tolist()]).T
    results.columns = ['Gamma', 'mcc']
    results = results.sort_values(by='mcc', ascending=False)

    write_excel_dir = create_excel_file(write_excel_dir)
    wb = openpyxl.load_workbook(write_excel_dir)
    ws = wb[wb.sheetnames[-1]]
    print_df_to_excel(df=results, ws=ws)
    wb.save(write_excel_dir)
    wb.close()


'''
В этой задаче вам нужно написать программу, которая будет выполнять следующий сценарий:

Открыть страницу http://suninjuly.github.io/explicit_wait2.html
Дождаться, когда цена дома уменьшится до $100 (ожидание нужно установить не меньше 12 секунд)
Нажать на кнопку "Book"
Решить уже известную нам математическую задачу (используйте ранее написанный код) и отправить решение
'''
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import math

link = r"http://suninjuly.github.io/explicit_wait2.html"
try:
    bro = webdriver.Chrome()
    bro.get(link)
    # easy method to wait - browser.implicitly_wait(5)
    price = WebDriverWait(bro, 12).until(EC.text_to_be_present_in_element((By.ID, "price"), "100"))
    bro.find_element_by_css_selector("button#book").click()
    x_val = bro.find_element_by_id("input_value")
    res = str(math.log(abs(12 * math.sin(int(x_val.text)))))
    bro.find_element_by_id("answer").send_keys(res)
    bro.find_element_by_id("solve").click()
    al = bro.switch_to.alert
    print(al.text)  # find answer for task
    al.accept()

finally:
    bro.quit()


from plover_vim.command_letter.defaults import shifted_symbols_aus, symbols

spelling = {
        "A": "a",
        "PW": "b",
        "KR": "c",
        "TK": "d",
        # "E": "e", plover
        "SK": "e",  # Abby
        "TP": "f",
        "TKPW": "g",
        "H": "h",
        # "EU": "i", plover
        # "AOEU": "i", # magnum
        "SKW": "i",  # Abby
        "SKWR": "j",
        # "SKWRAEU": "j", # magnum
        "K": "k",
        "HR": "l",
        "PH": "m",
        "TPH": "n",
        "O": "o",
        "P": "p",
        "KW": "q",
        "R": "r",
        "S": "s",
        "T": "t",
        # "U": "u", plover
        "WR": "u",  # Abby
        "SR": "v",
        "W": "w",
        "KP": "x",
        "KWR": "y",
        "STKPW": "z",
        # "STKPWHR": "z", # magnum
        }

prefixes = {
        "#": "control(j)",
        "E": "escape",  # EscapE
        "EU": "control(j) control(w)",  # vim splIt or wIn
        "#EU": "control(r)",  # regIster
        "U": "control(b)",  # tmUx
        "#E": "control(x)",  # Ex command
        "": "",
        }

defaults = {
        "disable_defaults": False,  # can be spelling, symbols etc ...
        # any dictionary entry overiddes the defaults
        "command_suffix": "{plover:clear_trans_state}",
        "spelling": spelling,
        "symbols": symbols,
        "shifted": shifted_symbols_aus,
        "unique_ender": "-LTZ",
        "prefixes": prefixes
        }