import random

from clock import Clock
from panel_reverberation import PanelReverberation
from range import Range

class Interaction:
    def __init__(self, source_panel, interaction_config, panel_context):
        self.source_panel = source_panel
        self.interaction_config = interaction_config
        self.panel_context = panel_context

        self.clock = Clock()

        self.source_panel_reverberation = PanelReverberation(self.source_panel, self, self.interaction_config)
        self.panel_reverberation_lookup = self._build_panel_reverberations()
        self.panel_reverberations = list(filter(lambda panel_reverberation: panel_reverberation is not None, self.panel_reverberation_lookup))
        self.number_panel_reverberations = len(self.panel_reverberations)
        self.eligible_panel_reverberations = []

        self.active_reverberations = False
        self.panel_reverberations_still_active = False

    def to_dict(self):
        return {
            "sourcePanel": self.source_panel.to_dict(),
            "clock": self.clock.to_dict(),
            "activePanelReverberations": map(
                lambda panel_reverberation: panel_reverberation.to_dict(),
                self.eligible_panel_reverberations
            )
        }

    def _build_panel_reverberations(self):
        panel_reverberations = [None] * self.panel_context.number_of_panels
        panel_reverberations[self.source_panel.index] = self.source_panel_reverberation
        neighbor_panels = self._collect_all_neighbor_panels()
        for panel in neighbor_panels:
            panel_reverberations[panel.index] = PanelReverberation(panel, self, self.interaction_config)
        return panel_reverberations

    def _collect_all_neighbor_panels(self):
        from_index = self.source_panel.index
        reverberating_panels = []
        for i in range(self.interaction_config.max_reverberation_distance - 1):
            distance_from_index = i + 1
            left_panel_index = from_index - distance_from_index
            right_panel_index = from_index + distance_from_index

            if left_panel_index >= 0:
                reverberating_panels.append(self.panel_context.get_panel_at(left_panel_index))

            if right_panel_index <= len(self.panel_context.panels) - 1:
                reverberating_panels.append(self.panel_context.get_panel_at(right_panel_index))

        return reverberating_panels

    def start(self):
        if self.clock.running:
            raise Exception("Interaction is already started!")

        self.clock.start()
        # print("Starting interaction", self.to_dict())
        self._trigger_new_reverberation(True)
        # print("Started interaction", self.to_dict())

    def _trigger_new_reverberation(self, trigger_source_panel):
        self.current_reverberating_distance = self.interaction_config.get_reverberation_distance()
        # TODO: hrm, slow - two loops, but only when interactions start?
        self.eligible_panel_reverberations = [panel_reverberation for panel_reverberation in self.panel_reverberations
                                              if
                                              self.current_reverberating_distance >= panel_reverberation.distance_from_trigger]

        # print("triggering", self.current_reverberating_distance, "reverberating panels")
        for panel_reverberation in self.eligible_panel_reverberations:
            if trigger_source_panel or not panel_reverberation.is_source_interaction:
                panel_reverberation.start()

    def stop(self):
        if not self.clock.running:
            return

        self.clock.stop()

    def update(self):
        if not self.clock.running:
            return

        self.clock.next()

        self.panel_reverberations_still_active = False
        last_panel_reverberation_alive = None

        for panel_reverberation in self.eligible_panel_reverberations:
            if panel_reverberation.is_done():
                pass
            else:
                self._start_panel_reverberation(panel_reverberation)

                # Maybe it's done _now_ after update() is called
                if panel_reverberation.is_done():
                    panel_reverberation.stop()
                else:
                    self.panel_reverberations_still_active = True
                    if last_panel_reverberation_alive is None:
                        last_panel_reverberation_alive = panel_reverberation
                    elif last_panel_reverberation_alive is not None:
                        last_panel_reverberation_alive = False

        is_at_zero_point = self._calculate_is_at_zero_point(last_panel_reverberation_alive)

        self._maybe_revive_panel_reverberations(is_at_zero_point)

    def _start_panel_reverberation(self, panel_reverberation):
        reverberation_panel_delay_ticks = self.interaction_config.reverberation_panel_delay_ticks
        delay_for_panel_to_start_ticks = panel_reverberation.distance_from_trigger * reverberation_panel_delay_ticks
        if self.clock.ticks >= delay_for_panel_to_start_ticks:
            if not panel_reverberation.cycle.clock.running:
                # TODO: sometimes the PanelReverberation has not had start() called
                panel_reverberation.start()
            panel_reverberation.update()

    def _calculate_is_at_zero_point(self, last_calculated):
        return last_calculated and last_calculated is not None and last_calculated.current_value == 0

    def _maybe_revive_panel_reverberations(self, is_at_zero_point):
        if is_at_zero_point and self.source_panel.interaction_active and random.random() > 0.65:
            self.clock.restart()
            self._trigger_new_reverberation(False)

    def is_dead(self):
        if self.source_panel.interaction_active:
            return False

        return not self.panel_reverberations_still_active

    def get_distance_from_trigger_to_panel(self, panel):
        return abs(self.source_panel.index - panel.index)

    def get_value_for_panel(self, panel):
        panel_reverberation = self.panel_reverberation_lookup[panel.index]
        if panel_reverberation is None:
            return 0
        else:
            return panel_reverberation.current_value

class InteractionContext:
    def __init__(self, interaction_config, panel_context):
        self.interaction_config = interaction_config
        self.panel_context = panel_context

    def create_interaction(self, panel):
        return Interaction(panel, self.interaction_config, self.panel_context)

class InteractionConfig:
    def __init__(self,
                 min_reverberation_distance,
                 max_reverberation_distance,
                 reverberation_panel_delay_ticks,
                 min_trigger_panel_animation_loop_duration_ticks,
                 max_trigger_panel_animation_loop_duration_ticks,
                 max_interaction_threshold_percent,
                 max_interaction_duration_ticks,
                 max_interaction_amount_of_reverberation,
                 max_interaction_value_multiplier):
        self.max_reverberation_distance = max_reverberation_distance
        self.reverberation_distance_range = Range(min_reverberation_distance, max_reverberation_distance)
        self.reverberation_panel_delay_ticks = reverberation_panel_delay_ticks
        self.trigger_panel_animation_loop_duration_ticks_range = Range(
            min_trigger_panel_animation_loop_duration_ticks, max_trigger_panel_animation_loop_duration_ticks
        )
        self.initial_trigger_panel_animation_loop_duration_ticks = \
            (max_trigger_panel_animation_loop_duration_ticks + min_trigger_panel_animation_loop_duration_ticks) / 2
        self.max_interaction_threshold_percent = max_interaction_threshold_percent
        self.max_interaction_duration_ticks = max_interaction_duration_ticks
        self.max_interaction_amount_of_reverberation = max_interaction_amount_of_reverberation
        self.max_interaction_value_multiplier = max_interaction_value_multiplier

    def get_reverberation_distance(self):
        return self.reverberation_distance_range.random_int_between()

    def get_trigger_panel_animation_loop_duration_ticks(self):
        return self.trigger_panel_animation_loop_duration_ticks_range.random_int_between()

    def to_dict(self):
        return {
            'maxReverberationDistance': self.max_reverberation_distance,
            'reverberationPanelDelayTicks': self.reverberation_panel_delay_ticks,
            'maxInteractionThresholdPercent': self.max_interaction_threshold_percent
        }


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

import os
import re

moviesFolder = 'Scripts'
treatedFolder = 'TreatedScripts'
IntermediateFolder = 'IntermediateScripts'
movies = ['Cars2.txt']

cabecera = [14]

for x in range(len(movies)):
    indice = 0
    tratado = []
    dialogo = False

    if os.path.exists(os.path.join(os.path.join(os.getcwd(), moviesFolder), movies[x])):
        with open(os.path.join(os.path.join(os.getcwd(), moviesFolder), movies[x]), 'r') as reader:
            # Eliminamos las lineas introductorias
            for z in range(cabecera[x]):
                reader.readline()

            # Tratamos el fichero
            for line in reader:

                if line.strip() == '':
                    dialogo = False

                if line.strip() != '':
                    if dialogo:
                        tratado[indice - 1] = tratado[indice - 1] + \
                            ' ' + line.strip()

                    elif line.startswith('\t  '):
                        dialogo = True
                        tratado.append(line.strip()+': ')
                        indice += 1

        # Guardamos los resultados en un fichero
        with open(os.path.join(os.path.join(os.getcwd(), IntermediateFolder), movies[x]), 'w') as writer:
            for y in range(indice):
                writer.write(tratado[y]+'\n')

        # Movemos el fichero tratado
        os.rename(os.path.join(os.path.join(os.getcwd(), moviesFolder), movies[x]), os.path.join(
            os.path.join(os.getcwd(), treatedFolder), movies[x]))


### Simple neural network code to train on the CIFAR-10 network
### single model version of the code
### requires Keras and TensorFlow
### based on the MNIST implementation tutorial from Keras
### can either just run code with no arguments which gives 1 hidden layer with 2000 nodes
# otherwise, can put arguments. each number is size of successive layer, so to get 3 hidden nodes
# with 4000, 1000, and 4000 nodes, call 'python cifar_network.py 4000 1000 4000'

import sys
import numpy
import itertools
import cPickle
import keras
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense

def unpickle(file):
    with open(file, 'rb') as fo:
        dict = cPickle.load(fo)
    return dict

# structure of dictionaries
# possible keys:
# data
# labels
# batch_label
# filenames

# for example
# batch1['data'][0]

# load data
batch1 = unpickle("cifar-10-batches-py/data_batch_1")
batch2 = unpickle("cifar-10-batches-py/data_batch_2")
batch3 = unpickle("cifar-10-batches-py/data_batch_3")
batch4 = unpickle("cifar-10-batches-py/data_batch_4")
batch5 = unpickle("cifar-10-batches-py/data_batch_5")
test_batch = unpickle("cifar-10-batches-py/test_batch")

# parameters to choose
epochs = 20
numTrainSamples = 20000
activ = "relu"
learning_rate = .1

batch_size = 128
num_classes = 10
input_size = 3072
layer_sizes = sys.argv[1:]

# load data as list to concatenate
tx1 = list(batch1['data'])
tx2 = list(batch2['data'])
tx3 = list(batch3['data'])
tx4 = list(batch4['data'])
tx5 = list(batch5['data'])
ty1 = list(batch1['labels'])
ty2 = list(batch2['labels'])
ty3 = list(batch3['labels'])
ty4 = list(batch4['labels'])
ty5 = list(batch5['labels'])

## CHOOSE HOW MANY TRAINING / TEST SAMPLES
# build the actual training and test sets, and then clip them to appropriate size
x_train = numpy.array(tx1 + tx2 + tx3 + tx4 + tx5)
y_train = numpy.array(ty1 + ty2 + ty3 + ty4 + ty5)

x_test = test_batch['data']
y_test = test_batch['labels']

# reshape them
x_train = x_train.reshape(50000, input_size)
x_test = x_test.reshape(10000, input_size)
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train /= 255
x_test /= 255

x_train = x_train[0 : numTrainSamples]
y_train = y_train[0 : numTrainSamples]
print(x_train.shape[0], 'train samples')
print(x_test.shape[0], 'test samples')

# convert class vectors to binary class matrices
y_train = keras.utils.to_categorical(y_train, num_classes)
y_test = keras.utils.to_categorical(y_test, num_classes)

## HOW MANY LAYERS
model = Sequential()

## NUMBER OF NODES / LAYERS
default_number_nodes = 2000
if(len(layer_sizes) == 0):
	model.add(Dense(default_number_nodes, activation=activ, input_shape=(input_size,)))
else:
	model.add(Dense(int(layer_sizes[0]), activation=activ, input_shape=(input_size,)))
	for l in layer_sizes[1:]:
		model.add(Dense(int(l), activation=activ))

### output layer
model.add(Dense(num_classes, activation='softmax'))

### print the shape of the model
model.summary()

opt = keras.optimizers.Adam()
model.compile(loss='categorical_crossentropy',
              optimizer=opt,
              metrics=['accuracy'])

### start training
history = model.fit(x_train, y_train,
                    batch_size=batch_size,
                    epochs=epochs,
                    verbose=1,
                    validation_data=(x_test, y_test))
### test it
model.summary()
score = model.evaluate(x_test, y_test, verbose=0)
print('Test loss:', score[0])
print('Test accuracy:', score[1])
print("Epochs: " + str(epochs))
print("Number of training samples: " + str(numTrainSamples))
print("Activation: " + activ)


"""
1552. Magnetic Force Between Two Balls
Medium

In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket.
Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.

Rick stated that magnetic force between two different balls at positions x and y is |x - y|.

Given the integer array position and the integer m. Return the required force.

Example 1:

Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6].
The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:

Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.

Constraints:

n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
"""
from typing import List

class Solution:
    def maxDistance(self, position: List[int], m: int) -> int:
        position.sort()
        def fn(d):
            """Return True if d is a feasible distance."""
            ans, prev = 0, float('-inf') # where previous ball is put
            for x in position:
                if x - prev >= d:
                    ans += 1
                    if ans == m: return True
                    prev = x
            return False

		# "last True" binary search (in contrast to "first True" binary search)
        lo, hi = 1, position[-1] - position[0]
        while lo < hi:
            mid = lo + hi + 1 >> 1
            if fn(mid): lo = mid
            else: hi = mid - 1
        return lo


import json
import os
import findspark
findspark.init()
from pyspark.sql import SparkSession
from pyspark.sql.types import StringType, StructField, StructType, IntegerType, TimestampType
from pyspark.sql.functions import from_json, col, expr
from transform import get_relevance_mark

with open('terraform/terraform.tfstate') as f:
    config = data = json.load(f)

KAFKA_BOOTSTRAP_SERVER = config['outputs']['kafka_endpoint']['value'].replace('SASL_SSL://', '')
KAFKA_KEY = config['outputs']['kafka_api_key_id']['value']
KAFKA_SECRET = config['outputs']['kafka_api_key_secret']['value']
GOOGLE_APPLICATION_CREDENTIALS = 'google-services.json'
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = GOOGLE_APPLICATION_CREDENTIALS
PROJECT_ID = config['outputs']['gcp_project_id']['value']

schema = StructType([
    StructField("id", StringType()),
    StructField("subreddit", StringType()),
    StructField("author", StringType()),
    StructField("subreddit_subscribers", IntegerType()),
    StructField("selftext", StringType()),
    StructField("title", StringType()),
    StructField("created_utc", TimestampType()),
    StructField("url", StringType()),
])

if __name__ == '__main__':
    packages = [
        'org.apache.spark:spark-sql-kafka-0-10_2.12:3.2.0',
        'org.apache.kafka:kafka-clients:3.2.0',
        "com.google.cloud.spark:spark-bigquery-with-dependencies_2.12:0.30.0",
    ]
    # Replace these with your own values
    dataset_id = "reddit_post"
    table_id = "post_ai"
    bucket = "data-reddit"
    # Set the Kafka topic and bootstrap servers
    topic_name = "reddit_ai"

    spark = SparkSession.builder \
        .appName("spark") \
        .config("spark.hadoop.fs.gs.impl", "com.google.cloud.hadoop.fs.gcs.GoogleHadoopFileSystem") \
        .config("spark.hadoop.fs.AbstractFileSystem.gs.impl", "com.google.cloud.hadoop.fs.gcs.GoogleHadoopFS") \
        .config("spark.hadoop.fs.gs.project.id", PROJECT_ID) \
        .config("spark.sql.execution.pythonUDF.arrow.enabled", "true") \
        .config("spark.hadoop.google.cloud.auth.service.account.enable", True) \
        .config("spark.hadoop.fs.gs.auth.service.account.json.keyfile", GOOGLE_APPLICATION_CREDENTIALS) \
        .config("spark.jars.packages", ",".join(packages)) \
        .config("spark.jars", "gcs-connector-hadoop3-2.2.10-shaded.jar") \
        .config("spark.executor.cores", "1") \
        .getOrCreate()

    # Read data from the Kafka topic
    df = spark.readStream \
        .format("kafka") \
        .option("kafka.bootstrap.servers", KAFKA_BOOTSTRAP_SERVER) \
        .option("kafka.sasl.jaas.config", f'org.apache.kafka.common.security.plain.PlainLoginModule required username="{KAFKA_KEY}" password="{KAFKA_SECRET}";') \
        .option("kafka.sasl.mechanism", 'PLAIN') \
        .option("kafka.security.protocol", 'SASL_SSL') \
        .option("subscribe", topic_name) \
        .load()

    spark.udf.register("relevance_score_udf", get_relevance_mark, IntegerType())
    # Cast the value column as a string
    df = df.select(col("value").cast("string").alias("value"))
    # Parse the JSON data
    df = df.select(from_json("value", schema).alias("data")) \
        .select(
        "data.id",
        "data.subreddit",
        "data.author",
        "data.subreddit_subscribers",
        "data.selftext",
        "data.title",
        "data.created_utc",
        "data.url"
    )

    query = df.writeStream \
        .format("bigquery") \
        .option("table", f"{PROJECT_ID}.{dataset_id}.{table_id}") \
        .option("checkpointLocation", "gs://{bucket}/checkpoints/") \
        .option("temporaryGcsBucket", "{bucket}/tmp") \
        .outputMode("append") \
        .start()

    query.awaitTermination()


from application.model.assignment import Assignment

class AssignmentController:
    """Represents  AssignmentController objects

    instance attributes:
        user_input: objects of UserInput class
        view: objects of View class
    """

    def __init__(self, user_input, view):
        """Instance Initializer"""
        self.user_input = user_input
        self.view = view

    def add_assignment_action(self):
        """Add Assignment object

        Returns:
            None
        """
        data = self.user_input.get_assignment_data()
        if data is None:
            return
        assignment = Assignment(*data)
        assignment.add_to_assignments()

        self.view.show_message("New assignment added!")
        self.user_input.press_enter_to_continue()

    def get_assignment(self, assignments):
        """Gets assignment object form class list

        Returns:
            assignment: list of Assignment object
        """
        group_name = "assignment's"
        index = self.user_input.get_index_input(len(assignments), group_name)
        return assignments[index]

    def show_assignments(self):
        """Show details of assignment object

        Returns:
            None
        """
        assignments = Assignment.get_assignments_list()
        self.view.show_assignments(assignments)
        group_name = "assignment's"
        user_aux_menu_decision = self.user_input.get_aux_menu_input(len(assignments), group_name)
        self.view.clear()
        if isinstance(user_aux_menu_decision, int):
            choosen_assignment = assignments[user_aux_menu_decision]
            self.view.show_assignment_details(choosen_assignment)
        self.user_input.press_enter_to_continue()


# https://leetcode.com/problems/sum-of-square-numbers/
import math

def judgeSquareSum(c: int):
    l = 0
    r = int(math.sqrt(c))

    while l <= r and l**2 + r**2 != c:
        if l**2 + r**2 > c:
            r -= 1
        else:
            l += 1

    return True if l <= r else False


from flask import Flask, request, render_template, jsonify
import utils

app = Flask(__name__)

@app.route('/api/setKeys', methods=['POST'])
def handle():
    data = request.json
    utils.saveConfig(data)
    return jsonify(isError= False,
                    message= "Success",
                    statusCode= 200), 200

@app.route('/', defaults={'path': ''})
@app.route('/<path:path>')
def index(path):
    return render_template("index.html")

app.run(debug=True)


import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.font_manager as fm
import numpy as np
import itertools
import json
import re
import pickle

from sklearn.decomposition import LatentDirichletAllocation
import pandas as pd

from data_loader import DataLoader

from collections import Counter
from wordcloud import WordCloud

from sklearn.feature_extraction.text import TfidfVectorizer
import matplotlib.pyplot as plt

from bareunpy import Tagger

import networkx as nx
import os
from matplotlib import rc
from tqdm import tqdm

rc('font', family='AppleGothic')
plt.rcParams['font.family'] = 'AppleGothic' # 사용할 한글 폰트명 입력
plt.rcParams['axes.unicode_minus'] = False # 마이너스 폰트 설정

class FrequencyAnalysis:
    def __init__(self):
        pass

    def cloud_visulize(self, noun_list, filename, white = True):
        if white == True:
            wordcloud = WordCloud(width=2000, height=1000, font_path = '/Library/Fonts/AppleGothic.ttf', collocations=True, background_color='white')
        else:
            wordcloud = WordCloud(width=2000, height=1000, font_path = '/Library/Fonts/AppleGothic.ttf', collocations=True)
        wordcloud.generate_from_frequencies(dict(noun_list))
        wordcloud.to_file(filename)
        wordcloud = None

    def cloud_visualize_idf(self, noun_list, filename):
        from sklearn.feature_extraction.text import TfidfVectorizer

    def wordcloud_Rank(self, token, N = 30, white = True, outdir = 'out', filename=''):
        if not os.path.exists(outdir + '/Wordcloud'):
            os.makedirs(outdir + '/Wordcloud')
        filename_png = outdir + f'/Wordcloud/{filename}.png'
        filename_csv = outdir + f'/Wordcloud/{filename}.csv'

        flatten_tokens = list(itertools.chain(*token))
        token_count = Counter(flatten_tokens)
        Top_token_count = token_count.most_common(N)

        word_count = pd.DataFrame(Top_token_count)
        word_count.columns = ['Word', 'Count']
        word_count.to_csv(filename_csv, index = False, encoding='euc-kr')

        self.cloud_visulize(Top_token_count, filename_png, white)

        return word_count

    def Top_Keyword_Edgelist(self, Token, Edge_list, N, outdir = '', node_size = 50, edge_size = 10, filename=''):
        if not os.path.exists(outdir + '/Graph'):
            os.makedirs(outdir + '/Graph')
        filename = outdir + f'/Graph/{filename}'
        flatten_tokens = list(itertools.chain(*Token))
        All_words = Counter(flatten_tokens)
        Top_words = All_words.most_common(N)
        Top_words = [word for (word, count) in Top_words]
        Top_Edge_list = list((x, y, v) for (x,y,v) in Edge_list if (x in Top_words and  y in Top_words))
        G_top = nx.Graph()

        for A,B, weight in Top_Edge_list: G_top.add_edge(A, B, weight=weight, distance = 1/weight)
        width_top = list(nx.get_edge_attributes(G_top, 'weight').values())
        degree = [All_words[node] * node_size for node in list(G_top.nodes)]
        edges_top = G_top.edges()
        weights = [1 for u,v in edges_top]

        with open(f'{filename}.json', 'w', encoding='utf-8') as f:
            json.dump(G_top.adj._atlas, f, ensure_ascii=False, indent=2)
        plt.figure(figsize=(20,20))
        pos = nx.spring_layout(G_top, k = 0.5)
        nx.draw(G_top, pos, width=weights, node_color= 'yellow', node_size = degree, edge_color= 'green', edge_cmap=plt.cm.Reds)
        nx.draw_networkx_labels(G_top, pos, font_size=32, font_family='AppleGothic', font_color='black')
        plt.savefig(f'{filename}.jpg')

    def make_Co_Keyword(self, Text):
        Co_Keyword = list(itertools.combinations(Text,2))
        Co_Keyword = [(x,y) for (x,y) in Co_Keyword if x != y]
        return Co_Keyword

    def make_Edgelist(self, Token):
        Co_Keyword = [self.make_Co_Keyword(text) for text in Token]
        Co_Keyword = list(itertools.chain(*Co_Keyword))
        Edge_list = list((x, y, v) for (x,y), v in Counter(Co_Keyword).items())
        return Edge_list

    def preprocess_text(self, lines):
        """
        불용어를 제거하기 위해 토큰화한다.
        :param lines: 문장 리스트
        :return: 토큰화된 문장 리스트
        """
        count_dict = {}
        tagger = Tagger('2AVZBLY-D7QUNYQ-XNCRIOA-NVP3TLA', 'localhost', port=5656)
        tokenized_lines = []
        for line in tqdm(lines):
            tagged = tagger.tags([line])
            nouns = []
            for p in tagged.pos():
                if len(p[0]) > 1:
                    if p[1] == 'NNG' or p[1] == 'NNP':
                        nouns.append(p[0])
            if len(nouns) != 0:
                for n in nouns:
                    if n in count_dict:
                        count_dict[n] += 1
                    else:
                        count_dict[n] = 1
                tokenized_lines.append(nouns)

        return tokenized_lines

def main(files):
    for file in files:
        if file.endswith('.txt'):
            texts = []
            basename = os.path.basename(file).split('.txt')[0]
            with open(file, 'r', encoding='utf-8') as f:
                for line in f:
                    texts.append(line.strip())
            join_text = "".join(texts)
            cleaned_text = re.sub(r"●|•|ㅇ|■", "", join_text)
            texts =  [t + '.' for t in cleaned_text.split(". ")]
            # 빈도 분석 객체 생성
            fa = FrequencyAnalysis()

            # 형태소 분석 명사 추출
            Tokens = fa.preprocess_text(texts)

            # 워드클라우드 생성
            word_count = fa.wordcloud_Rank(Tokens, 50, white = True, outdir = 'out', filename=basename)
            print(word_count.head(10))

            # 키워드 네트워크
            Edge_list = fa.make_Edgelist(Tokens)
            fa.Top_Keyword_Edgelist(Tokens, Edge_list, 50, outdir = 'out', node_size = 50, edge_size = 6, filename=basename)

def find_most_frequent_elements(strings):
    frequency = {}
    max_frequency = 0

    # Count the frequency of each element
    for string in strings:
        if string in frequency:
            frequency[string] += 1
        else:
            frequency[string] = 1

        # Update max_frequency if necessary
        if frequency[string] > max_frequency:
            max_frequency = frequency[string]

    most_frequent_elements = []

    # Find elements with maximum frequency
    for string, count in frequency.items():
        if count == max_frequency:
            most_frequent_elements.append(string)

    return most_frequent_elements

# Example usage
strings = ["apple", "banana", "apple", "orange", "banana", "apple"]
most_frequent = find_most_frequent_elements(strings)
print("Most frequent elements:", most_frequent)

if __name__ == '__main__':
    # 데이터 로딩
    dl = DataLoader()
    files = dl.get_file_list('data')

    # 분석
    main(files)

    # 문서와 라벨을 생성한다.
    documents = []
    if os.path.isfile('data/pkl/idf.pkl'):
        with open('data/pkl/idf.pkl', 'rb') as f:
           documents = pickle.load(f)
    else:
        for file in files:
            texts = []
            basename = os.path.basename(file).split('.txt')[0]
            with open(file, 'r', encoding='utf-8') as f:
                for line in f:
                    texts.append(line.strip())
            join_text = "".join(texts)
            cleaned_text = re.sub(r"●|•|ㅇ|■", "", join_text)
            texts =  [t + '.' for t in cleaned_text.split(". ")]
            # 빈도 분석 객체 생성
            fa = FrequencyAnalysis()

            # 형태소 분석 명사 추출
            temp_lines = []
            Tokens = fa.preprocess_text(texts)
            for t in Tokens:
                temp_lines.append(' '.join(t))
            documents.append((" ".join(temp_lines), basename))
        with open('data/pkl/idf.pkl', 'wb') as f:
            pickle.dump(documents, f, pickle.HIGHEST_PROTOCOL)
    # TF-IDF 벡터를 생성
    vectorizer = TfidfVectorizer()
    tfidf_matrix = vectorizer.fit_transform([doc[0] for doc in documents])
    outdir = 'out/tf-idf'
    # 각 문서의 중요한 단어를 추출
    for i, doc in enumerate(documents):
        if not os.path.exists(outdir):
            os.makedirs(outdir)
        filename = outdir + f'/{doc[1]}'
        tfidf_scores = dict(zip(vectorizer.get_feature_names_out(), tfidf_matrix[i].toarray().flatten()))
        sorted_scores = sorted(tfidf_scores.items(), key=lambda item: item[1], reverse=True)
        top_scores = sorted_scores[:20]  # 상위 n개 단어를 선택
        plot_title = f"대선별 선거 공약집에서 각 단어의 중요도({doc[1]})"
        plt.figure(figsize=(10, 5))
        plt.bar(*zip(*top_scores))
        plt.title(plot_title, y=1.05)
        plt.xlabel('상위 20개 단어', labelpad=10)
        plt.ylabel('TF-IDF 점수', labelpad=10)
        plt.savefig(f'{filename}.jpg')

    # # LDA 모델 생성
    # lda = LatentDirichletAllocation(n_components=10, random_state=0)
    # lda_doc = [d[0].split(' ') for d in documents]
    # # tf-idf 벡터에 대해 LDA 학습
    # from gensim import corpora
    # dictionary = corpora.Dictionary(lda_doc)
    # corpus = [dictionary.doc2bow(text) for text in lda_doc]
    # # print(corpus[1]) # 수행된 결과에서 두번째 뉴스 출력. 첫번째 문서의 인덱스는 0
    # import gensim
    # NUM_TOPICS = 10 # 20개의 토픽, k=20
    # ldamodel = gensim.models.ldamodel.LdaModel(corpus, num_topics = NUM_TOPICS, id2word=dictionary, passes=15)
    # topics = ldamodel.print_topics(num_words=4)
    # # for topic in topics:
    # #     print(topic)
    # # lda.fit(tfidf_matrix)
    # # lda 시각화

    # import pyLDAvis.gensim_models

    # pyLDAvis.enable_notebook()
    # vis = pyLDAvis.gensim_models.prepare(ldamodel, corpus, dictionary)
    # pyLDAvis.display(vis)
    # for i, topic_list in enumerate(ldamodel[corpus]):
    #     if i==5:
    #         break
    #     print(i,'번째 문서의 topic 비율은',topic_list)

    # def make_topictable_per_doc(ldamodel, corpus):
    #     topic_table = pd.DataFrame()

    #     # 몇 번째 문서인지를 의미하는 문서 번호와 해당 문서의 토픽 비중을 한 줄씩 꺼내온다.
    #     for i, topic_list in enumerate(ldamodel[corpus]):
    #         doc = topic_list[0] if ldamodel.per_word_topics else topic_list
    #         doc = sorted(doc, key=lambda x: (x[1]), reverse=True)
    #         # 각 문서에 대해서 비중이 높은 토픽순으로 토픽을 정렬한다.
    #         # EX) 정렬 전 0번 문서 : (2번 토픽, 48.5%), (8번 토픽, 25%), (10번 토픽, 5%), (12번 토픽, 21.5%),
    #         # Ex) 정렬 후 0번 문서 : (2번 토픽, 48.5%), (8번 토픽, 25%), (12번 토픽, 21.5%), (10번 토픽, 5%)
    #         # 48 > 25 > 21 > 5 순으로 정렬이 된 것.

    #         # 모든 문서에 대해서 각각 아래를 수행
    #         for j, (topic_num, prop_topic) in enumerate(doc): #  몇 번 토픽인지와 비중을 나눠서 저장한다.
    #             if j == 0:  # 정렬을 한 상태이므로 가장 앞에 있는 것이 가장 비중이 높은 토픽
    #                 topic_table = topic_table._append(pd.Series([int(topic_num), round(prop_topic,4), topic_list]), ignore_index=True)
    #                 # 가장 비중이 높은 토픽과, 가장 비중이 높은 토픽의 비중과, 전체 토픽의 비중을 저장한다.
    #             else:
    #                 break
    #     return(topic_table)
    # topictable = make_topictable_per_doc(ldamodel, corpus)
    # topictable = topictable.reset_index() # 문서 번호을 의미하는 열(column)로 사용하기 위해서 인덱스 열을 하나 더 만든다.
    # topictable.columns = ['문서 번호', '가장 비중이 높은 토픽', '가장 높은 토픽의 비중', '각 토픽의 비중']
    # topictable[:10]
    # # 각 문서에서의 주제 비율 추출
    # topic_proportions = lda.transform(tfidf_matrix)

    # # 주제 비율을 데이터프레임으로 변환
    # df_topic_proportions = pd.DataFrame(topic_proportions)

    # n_top_words = 10 # 각 주제에서 상위 10개 단어를 확인
    # feature_names = vectorizer.get_feature_names_out()

    # topic_labels = []
    # for topic_idx, topic in enumerate(lda.components_):
    #     top_features_ind = topic.argsort()[:-n_top_words - 1:-1]
    #     top_features = [feature_names[i] for i in top_features_ind]
    #     topic_labels.append(", ".join(top_features)) # 주제 라벨 생성
    # topic_labels = find_most_frequent_elements(topic_labels)
    # topic_labels = topic_labels[0].split(', ')
    # outdir = 'out/lda'
    # if not os.path.exists(outdir):
    #     os.makedirs(outdir)
    # filename = outdir + f'/{doc[1]}'

    # # 데이터프레임에서 각 문서의 주제 비율을 파이 차트로 그리기
    # for i in range(len(df_topic_proportions)):
    #     plt.figure(i)
    #     plt.pie(df_topic_proportions.iloc[i], labels=topic_labels)
    #     plt.title(f'Document {i+1} Topic Proportions')
    #     plt.savefig(f'{filename}.jpg')


###########################################################
# jvn.py
# Data gathering / re-formatting script for JVN Vulnerbility Information
# VulnData (https://github.com/moppoi5168/VulnData)
# Licensed by GPL License
###########################################################

from html.parser import HTMLParser

import glob
import os
import tqdm
import urllib.request
import xml.etree.ElementTree as ET

vulnid = []

class html_parser(HTMLParser):
    def __init__(self):
        HTMLParser.__init__(self)

    def handle_starttag(self, tag, attrs):
        if tag == "sec:references":
            attrs = dict(attrs)
            if 'source' in attrs and 'id' in attrs:
                if attrs['source'] == 'JVN iPedia':
                    vulnid.append(attrs['id'])

def main():
    print('[jvn.py] Gathering latest vulnerability data ......', end='\n\n')
    year_list = range(1998, 2020)
    month_list = range(1, 13)
    for year in year_list:
        for month in tqdm.tqdm(month_list, desc=str(year)):
            url = 'https://jvndb.jvn.jp/myjvn?method=getVulnOverviewList&feed=hnd&rangeDatePublished=n&rangeDateFirstPublished=n&datePublicStartY=' + str(year) + '&datePublicStartM=' + str(month) + '&datePublicEmdY=' + str(year) + '&datePublicEmdM=' + str(month)
            mem = urllib.request.urlopen(url).read().decode()
            tmp = mem.split('\n')
            parser = html_parser()
            for line in tmp:
                parser.feed(line)
            for vid in vulnid:
                url = 'https://jvndb.jvn.jp/myjvn?method=getVulnDetailInfo&feed=hnd&vulnId=' + vid
                a

if __name__ == "__main__":
    main()


"""
This script downloads source data from data.jmsv.me; this contains an unzipped
and filtered mirror of the http://www1.icsi.berkeley.edu/~demelo/etymwn dataset

Data was filtered using the script at: https://data.jmsv.me/etymwn-filterer.sh
"""

import csv
import hashlib
import os
import io
import json
import gc

import requests
import six
from clint.textui import progress

def prepare(source_dir):
    """
    Create data source directory if not exists
    """
    if not os.path.exists(source_dir):
        os.makedirs(source_dir)

def download_dataset(url, dl_path):
    """
    Download filtered etymwn from jmsv.me mirror, displaying progress bar
    """
    r = requests.get(url, stream=True)

    with open(dl_path, "wb") as f:
        total_length = int(r.headers.get("content-length"))
        chunk_size = 4096
        for chunk in progress.bar(
            r.iter_content(chunk_size=chunk_size),
            expected_size=(total_length / chunk_size) + 1,
        ):
            if chunk:
                f.write(chunk)
                f.flush()
    print("Downloaded to " + dl_path)

def verify_local_data(url, dl_path):
    """
    Compare actual file checksum with expected served checksum
    Return bool determines whether or not data is (re)downloaded
    :return: True if local file matches, otherwise False
    """
    try:
        with open(dl_path, "rb") as f:
            # Local file checksum
            actual = hashlib.md5(f.read()).hexdigest()
    except EnvironmentError:
        # Return False if file doesn't exit
        return False

    expected = requests.get("%s.checksum" % url).text.strip()
    return actual == expected

def fix_anomalous_lang_code(code):
    if code == "wit":
        return "wnw"
    return code

def split_elements(compound):
    """
    Split source tsv elements at colon
    e.g.: 'rel:etymology' => ['rel', 'etymology']
    :return: Elements as list
    """
    elements = [fix_anomalous_lang_code(e.strip()) for e in compound.split(":")]

    if len(elements) == 2:
        return elements

    return [fix_anomalous_lang_code(elements[0]), ":".join(elements[1:])]

def generate_json(source_path, dir):
    """
    Reads source tsv and restructures data as described:
    https://github.com/jmsv/ety-python/issues/24
    """
    result = {}

    print("Loading source tsv")
    with io.open(source_path, "r", newline="", encoding="utf-8") as source:
        reader = csv.reader(source, delimiter="\t")
        source_rows = list(reader)

    gc.collect()

    print("Structuring data")
    for row in progress.bar(source_rows):
        source_lang, source_word = split_elements(row[0])

        if source_lang not in result:
            result[source_lang] = {}
        if source_word not in result[source_lang]:
            result[source_lang][source_word] = []

        dest_lang, dest_word = split_elements(row[2])
        result[source_lang][source_word].append({dest_word: dest_lang})

        del source_lang, source_word, dest_lang, dest_word

    # Save data to seperate files for languages, may be required in the future
    # print('Saving language files')
    # for key in progress.bar(result):
    #     with io.open(os.path.join(dir, 'data/ety-%s.json' % key), 'w') as f:
    #         f.write(json.dumps(result[key], sort_keys=False))

    # Save data
    print("Writing etymologies file")
    with io.open(os.path.join(dir, "etymologies.json"), "w") as f:
        json.dump(result, f)

def main():
    """
    Define paths, download data if required, generate json dataset
    """
    dir = os.path.dirname(os.path.realpath(__file__))
    source_dir = os.path.join(dir, "source")
    source_path = os.path.join(source_dir, "etymwn.tsv")
    source_url = "https://data.jmsv.me/etymwn-filtered.tsv"

    # Exit if not Python 3
    if not six.PY3:
        print("Script should be run as Python 3, exiting")
        exit(1)

    prepare(source_dir)

    # (Re)download data if required
    if not verify_local_data(source_url, source_path):
        print("Downloading source data")
        download_dataset(source_url, source_path)

        # If checksum still doesn't match, exit
        if verify_local_data(source_url, source_path):
            print("Verified local source data")
        else:
            print("Error verifying local source data, exiting")
            exit(1)
    else:
        print("Verified local source data")

    generate_json(source_path, dir)

    print("Done")

if __name__ == "__main__":
    main()


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue May  9 11:37:59 2023

@author: Tejal
"""

import numpy as np

from flask import Flask, render_template, request
import jsonify
import requests
import pickle

import sklearn
from sklearn.preprocessing import StandardScaler

app = Flask(__name__, static_folder='static')
model = pickle.load(open('model/regression_rf_model_v1.pkl', 'rb'))
standard_to = StandardScaler()
scaler = pickle.load(open('model/scalar_v1.pkl','rb'))

@app.route('/',methods=['GET'])
def Home():
    return render_template('index.html')

@app.route("/predict", methods=['POST'])
def predict():

    if request.method == 'POST':

        Year = int(request.form['Year'])
        Year=2020-Year

        Present_Price=float(request.form['Present_Price'])
        Kms_Driven=float(request.form['Kms_Driven'])

        inputs = [[Year, Present_Price, Kms_Driven]]
        normalized_inputs = scaler.transform(inputs)

        Year2 = normalized_inputs[0][0]
        Present_Price2 = normalized_inputs[0][1]
        Kms_Driven2 = normalized_inputs[0][2]

        Owner=int(request.form['Owner'])

        Fuel_Type_Petrol=request.form['Fuel_Type_Petrol']
        if(Fuel_Type_Petrol=='Petrol'):
                Fuel_Type_Petrol=1
                Fuel_Type_Diesel=0
        elif(Fuel_Type_Petrol=='Diesel'):
            Fuel_Type_Petrol=0
            Fuel_Type_Diesel=1
        else :
            Fuel_Type_Petrol=0
            Fuel_Type_Diesel=0

        Seller_Type_Individual=request.form['Seller_Type_Individual']
        if(Seller_Type_Individual=='Individual'):
            Seller_Type_Individual=1
        else:
            Seller_Type_Individual=0

        Transmission_Manual=request.form['Transmission_Manual']
        if(Transmission_Manual=='Manual'):
            Transmission_Manual=1
        else:
            Transmission_Manual=0

        prediction=model.predict([[Present_Price2,Kms_Driven2,Owner,Year2,Fuel_Type_Diesel,Fuel_Type_Petrol,Seller_Type_Individual,Transmission_Manual]])

        output=round(prediction[0],2)
        if output<0:
            return render_template('index.html',prediction_texts="Sorry! Unfortunately you cannot sell this car.")
        else:
            return render_template('index.html',prediction_text="You can sell this car at {} lakh (INR)".format(output))
    else:
        return render_template('index.html')

if __name__=="__main__":
    app.run(debug=True)


#!/usr/bin/env python

import os
import re

import wx
import wx.animate
import wx.lib.delayedresult
import wx.lib.scrolledpanel as scrolled

from aqopa.gui.general_purpose_frame_gui import GeneralFrame
from aqopa.model import name_indexes

"""
@file       gui.py
@brief      GUI for the reputation analysis panel
@author     Damian Rusinek <damian.rusinek@gmail.com>
@date       created on 06-09-2013 by Damian Rusinek
@date       edited on 27-06-2014 by Katarzyna Mazur (visual improvements mainly)
"""

class SingleVersionPanel(wx.Panel):
    """
    Frame presenting results for one simulation.
    Simulator may be retrieved from module,
    because each module has its own simulator.
    """

    def __init__(self, module, *args, **kwargs):
        wx.Panel.__init__(self, *args, **kwargs)

        self.module = module
        self.versionSimulator = {}

        self.hostChoosePanels       = []  # Panels used to choose hosts for energy consumptions results
        self.checkBoxInformations   = []  # Tuples with host name, and index ranges widget
        self.hostCheckBoxes         = []  # List of checkboxes with hosts names used for hosts' selection

        #################
        # VERSION BOX
        #################

        versionBox = wx.StaticBox(self, label="Version")
        versionsLabel = wx.StaticText(self, label="Choose Version To See\nAnalysis Results:")
        self.versionsList = wx.ComboBox(self, style=wx.TE_READONLY)
        self.versionsList.Bind(wx.EVT_COMBOBOX, self.OnVersionChanged)
        versionBoxSizer = wx.StaticBoxSizer(versionBox, wx.HORIZONTAL)
        versionBoxSizer.Add(versionsLabel, 0, wx.ALL | wx.ALIGN_CENTER, 5)
        versionBoxSizer.Add(wx.StaticText(self), 1, wx.ALL | wx.EXPAND, 5)
        versionBoxSizer.Add(self.versionsList, 1, wx.ALL | wx.ALIGN_CENTER, 5)

        ##################################
        # ENERGY CONSUMPTION BOX
        ##################################

        self.consumptionsBox = wx.StaticBox(self, label="Reputation results")

        hostsBox, hostsBoxSizer = self._BuildHostsBoxAndSizer()

        reputationBoxSizer = wx.StaticBoxSizer(self.consumptionsBox, wx.VERTICAL)
        reputationHBoxSizer = wx.BoxSizer(wx.HORIZONTAL)
        reputationHBoxSizer.Add(hostsBoxSizer, 1, wx.ALL | wx.EXPAND)

        self.showReputationBtn = wx.Button(self, label="Show")
        self.showReputationBtn.Bind(wx.EVT_BUTTON, self.OnShowReputationButtonClicked)

        reputationBoxSizer.Add(reputationHBoxSizer, 0, wx.ALL | wx.EXPAND)

        buttonsBoxSizer = wx.BoxSizer(wx.HORIZONTAL)
        buttonsBoxSizer.Add(wx.StaticText(self), 1, wx.EXPAND, 5)
        buttonsBoxSizer.Add(self.showReputationBtn, 0, wx.ALL | wx.ALIGN_RIGHT)

        reputationBoxSizer.Add(buttonsBoxSizer, 1, wx.ALL | wx.EXPAND, 5)

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(versionBoxSizer, 0, wx.ALL | wx.EXPAND, 5)
        sizer.Add(reputationBoxSizer, 1, wx.ALL | wx.EXPAND, 5)
        self.SetSizer(sizer)

        self.SetVersionsResultsVisibility(False)

    #################
    # REACTIONS
    #################

    def AddFinishedSimulation(self, simulator):
        """ """
        version = simulator.context.version
        self.versionsList.Append(version.name)

        self.versionSimulator[version.name] = simulator

    def OnVersionChanged(self, event):
        """ """
        versionName = self.versionsList.GetValue()
        simulator = self.versionSimulator[versionName]

        self._BuildHostsChoosePanel(simulator)
        self.SetVersionsResultsVisibility(True)

    def OnShowReputationButtonClicked(self, event):
        """ """
        versionName = self.versionsList.GetValue()
        simulator = self.versionSimulator[versionName]
        hosts = self._GetSelectedHosts(simulator)

        if len(hosts) == 0:
            wx.MessageBox("Please select hosts.", 'Error', wx.OK | wx.ICON_ERROR)
            return

        self.ShowHostsReputation(simulator, hosts)

    def RemoveAllSimulations(self):
        """ """
        self.versionsList.Clear()
        self.versionsList.SetValue("")
        self.versionSimulator = {}

        self.hostChoosePanels = []
        self.checkBoxInformations = {}
        self.hostCheckBoxes = []
        self.hostsBoxSizer.Clear(True)

        self.SetVersionsResultsVisibility(False)

    #################
    # LAYOUT
    #################

    def _BuildHostsBoxAndSizer(self):
        """ """
        self.hostsBox = wx.StaticBox(self, label="Host(s)")
        self.hostsBoxSizer = wx.StaticBoxSizer(self.hostsBox, wx.VERTICAL)
        return self.hostsBox, self.hostsBoxSizer

    def _BuildHostsChoosePanel(self, simulator):
        """ """
        for p in self.hostChoosePanels:
            p.Destroy()
        self.hostChoosePanels = []
        self.checkBoxInformations = {}
        self.hostCheckBoxes = []

        self.hostsBoxSizer.Layout()

        hosts = simulator.context.hosts
        hostsIndexes = {}
        for h in hosts:
            name = h.original_name()
            indexes = name_indexes(h.name)
            index = indexes[0]

            if name not in hostsIndexes or index > hostsIndexes[name]:
                hostsIndexes[name] = index

        for hostName in hostsIndexes:

            panel = wx.Panel(self)
            panelSizer = wx.BoxSizer(wx.HORIZONTAL)

            ch = wx.CheckBox(panel, label=hostName, size=(120, 20))
            textCtrl = wx.TextCtrl(panel, size=(200, 20))
            textCtrl.SetValue("0")

            rangeLabel = "Available range: 0"
            if hostsIndexes[hostName] > 0:
                rangeLabel += " - %d" % hostsIndexes[hostName]
            maxLbl = wx.StaticText(panel, label=rangeLabel)

            panelSizer.Add(ch, 0, wx.ALL | wx.ALIGN_CENTER)
            panelSizer.Add(wx.StaticText(self), 1, wx.ALL | wx.ALIGN_RIGHT)
            panelSizer.Add(textCtrl, 0, wx.ALL | wx.ALIGN_CENTER)
            panelSizer.Add(maxLbl, 0, wx.ALL | wx.ALIGN_CENTER)
            panel.SetSizer(panelSizer)
            self.hostsBoxSizer.Add(panel, 1, wx.ALL)

            self.checkBoxInformations[ch] = (hostName, textCtrl)
            self.hostChoosePanels.append(panel)
            self.hostCheckBoxes.append(ch)

        self.hostsBoxSizer.Layout()
        self.Layout()

    def SetVersionsResultsVisibility(self, visible):
        """ """
        widgets = [self.consumptionsBox, self.hostsBox, self.showReputationBtn]

        for w in widgets:
            if visible:
                w.Show()
            else:
                w.Hide()

        self.Layout()

    #################
    # STATISTICS
    #################

    def _GetSelectedHosts(self, simulator):
        """ Returns list of hosts selected by user """

        def ValidateHostsRange(indexesRange):
            """ """
            return re.match(r'\d(-\d)?(,\d(-\d)?)*', indexesRange)

        def GetIndexesFromRange(indexesRange):
            """ Extracts numbers list of hosts from range text """
            indexes = []
            ranges = indexesRange.split(',')
            for r in ranges:
                parts = r.split('-')
                if len(parts) == 1:
                    indexes.append(int(parts[0]))
                else:
                    for i in range(int(parts[0]), int(parts[1])+1):
                        indexes.append(i)
            return indexes

        hosts = []
        for ch in self.hostCheckBoxes:
            if not ch.IsChecked():
                continue
            hostName, hostRangeTextCtrl = self.checkBoxInformations[ch]
            indexesRange = hostRangeTextCtrl.GetValue()
            if not ValidateHostsRange(indexesRange):
                wx.MessageBox("Range '%s' for host '%s' is invalid. Valid example: 0,12,20-25,30."
                              % (indexesRange, hostName), 'Error', wx.OK | wx.ICON_ERROR)
                break
            else:
                indexes = GetIndexesFromRange(indexesRange)
                for h in simulator.context.hosts:
                    hostIndexes = name_indexes(h.name)
                    if h.original_name() == hostName and hostIndexes[0] in indexes:
                        hosts.append(h)
        return hosts

    def ShowHostsReputation(self, simulator, hosts):
        """ """
        lblText = ""

        for h in hosts:
            lblText += "%s: " % (h.name,)
            error = h.get_finish_error()
            if error is not None:
                lblText += " (Not Finished - %s)" % error

            host_vars = self.module.get_host_vars(h)
            for var_name in host_vars:
                lblText = "%s: %s" % (var_name, unicode(host_vars[var_name]))
            lblText += "\n\n"

        # create a new frame to show time analysis results on it
        hostsReputationWindow = GeneralFrame(self, "Reputation Analysis Results", "Host's Reputation", "modules_results.png")

        # create scrollable panel
        hostsPanel = scrolled.ScrolledPanel(hostsReputationWindow)

        # create informational label
        lbl = wx.StaticText(hostsPanel, label=lblText)

        # sizer to align gui elements properly
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(lbl, 0, wx.ALL | wx.EXPAND, 5)
        hostsPanel.SetSizer(sizer)
        hostsPanel.SetupScrolling(scroll_x=True)
        hostsPanel.Layout()

        # add panel on a window
        hostsReputationWindow.AddPanel(hostsPanel)
        # center window on a screen
        hostsReputationWindow.CentreOnScreen()
        # show the results on the new window
        hostsReputationWindow.Show()

class MainResultsNotebook(wx.Notebook):
    """ """
    def __init__(self, module, *args, **kwargs):
        wx.Notebook.__init__(self, *args, **kwargs)

        il = wx.ImageList(24, 24)
        singleVersionImg = il.Add(wx.Bitmap(self.CreatePath4Resource('reputation.png'), wx.BITMAP_TYPE_PNG))
        self.AssignImageList(il)

        self.module = module

        self.oneVersionTab = SingleVersionPanel(self.module, self)
        self.AddPage(self.oneVersionTab, "Single Version")
        self.SetPageImage(0, singleVersionImg)
        self.oneVersionTab.Layout()

    def OnParsedModel(self):
        """ """
        self.oneVersionTab.RemoveAllSimulations()

    def OnSimulationFinished(self, simulator):
        """ """
        self.oneVersionTab.AddFinishedSimulation(simulator)

    def OnAllSimulationsFinished(self, simulators):
        """ """
        pass

    def CreatePath4Resource(self, resourceName):
        """
        @brief      creates and returns path to the
                    given file in the resource
                    ('assets') dir
        @return     path to the resource
        """
        tmp = os.path.split(os.path.dirname(__file__))
        # find last / character in path
        idx = tmp[0].rfind('/')
        # get substring - path for resource
        path = tmp[0][0:idx]
        return os.path.join(path, 'bin', 'assets', resourceName)

class ModuleGui(wx.EvtHandler):
    """
    Class used by GUI version of AQoPA.
    """

    def __init__(self, module):
        """ """
        wx.EvtHandler.__init__(self)

        self.module = module
        self.mainResultNotebook = None

    def get_name(self):
        return "Reputation"

    def get_configuration_panel(self, parent):
        """ Returns WX panel with configuration controls. """

        panel = wx.Panel(parent)

        sizer = wx.BoxSizer(wx.VERTICAL)
        text = wx.StaticText(panel, label="Module does not need to be configured.")
        sizer.Add(text, 0, wx.ALL | wx.EXPAND, 5)
        text = wx.StaticText(panel, label="All result options will be available after results are calculated.")
        sizer.Add(text, 0, wx.ALL | wx.EXPAND, 5)

        panel.SetSizer(sizer)
        return panel

    def get_results_panel(self, parent):
        """
        Create main result panel existing from the beginning
        which will be extended when versions' simulations are finished.
        """
        self.mainResultNotebook = MainResultsNotebook(self.module, parent)
        return self.mainResultNotebook

    def on_finished_simulation(self, simulator):
        """ """
        self.mainResultNotebook.OnSimulationFinished(simulator)

    def on_finished_all_simulations(self, simulators):
        """
        Called once for all simulations after all of them are finished.
        """
        self.mainResultNotebook.OnAllSimulationsFinished(simulators)

    def on_parsed_model(self):
        """ """
        self.mainResultNotebook.OnParsedModel()


import os
import sys

from src.logger import logging
from src.exception import CustomException
from src.utils import save_object

import numpy as np
import pandas as pd
from dataclasses import dataclass
from sklearn.impute import SimpleImputer
from sklearn.preprocessing import OneHotEncoder, StandardScaler

# Pipelines
from sklearn.pipeline import Pipeline
from sklearn.compose import ColumnTransformer

@dataclass
class DataTransformationConfig:
    preprocessor_obj_file_path: str = os.path.join("artifacts", "preprocessor.pkl")

class DataTransformation:
    def __init__(self):
        self.data_transformation_config = DataTransformationConfig()

    def get_data_transformation_obj(self):
        try:
            logging.info("Making data transformation object")
            # Define nominal and other features
            nominal_cols = ['SEX', 'EDUCATION', 'MARRIAGE']
            numerical_cols = ['LIMIT_BAL', 'AGE', 'PAY_0', 'PAY_2', 'PAY_3', 'PAY_4', 'PAY_5', 'PAY_6', 'BILL_AMT1', 'BILL_AMT2', 'BILL_AMT3', 'BILL_AMT4', 'BILL_AMT5', 'BILL_AMT6', 'PAY_AMT1', 'PAY_AMT2', 'PAY_AMT3', 'PAY_AMT4', 'PAY_AMT5', 'PAY_AMT6']

            logging.info("Initiating pipeline")

            # numerical pipeline
            numerical_pipeline = Pipeline(
                steps=[
                    ('imputer', SimpleImputer(strategy='median')),
                    ('scaler', StandardScaler())
                ]
            )

            # categorical pipeline
            categorical_pipeline = Pipeline(
                steps=[
                    ('imputer', SimpleImputer(strategy='most_frequent')),
                    ('nominalencoder', OneHotEncoder(sparse_output=False, handle_unknown='ignore')),
                    ('scaler', StandardScaler())
                ]
            )

            preprocessor = ColumnTransformer([
                ('numerical_pipeline', numerical_pipeline, numerical_cols),
                ('categorical_pipeline', categorical_pipeline, nominal_cols)
            ])

            logging.info("Pipeline completed")
            return preprocessor

        except Exception as e:
            logging.info("An exception has occurred in get_data_transformation_obj")
            raise CustomException(e, sys)

    def initiate_data_transformation(self, train_data_path, test_data_path):
        try:
            # Read train and test data
            train_df = pd.read_csv(train_data_path)
            test_df = pd.read_csv(test_data_path)

            logging.info("Reading train and test data completed")
            logging.info(f"Train Dataframe head : \n {train_df.head().to_string()}")
            logging.info(f"Test Dataframe head : \n {test_df.head().to_string()}")

            logging.info("Getting preprocessor object")
            preprocessor_obj = self.get_data_transformation_obj()

            target_col = 'default.payment.next.month'
            drop_cols = [target_col, 'ID']

            # Separating input and target features
            input_feature_train_df = train_df.drop(columns=drop_cols, axis=1)
            target_feature_train_df = train_df[target_col]

            input_feature_test_df = test_df.drop(columns=drop_cols, axis=1)
            target_feature_test_df = test_df[target_col]

            input_feature_train_arr = preprocessor_obj.fit_transform(input_feature_train_df)
            input_feature_test_arr = preprocessor_obj.transform(input_feature_test_df)

            logging.info("Applying preprocessing object on training and testing datasets.")

            train_arr = np.c_[input_feature_train_arr, np.array(target_feature_train_df)]
            test_arr = np.c_[input_feature_test_arr, np.array(target_feature_test_df)]

            save_object(
                file_path=self.data_transformation_config.preprocessor_obj_file_path,
                obj=preprocessor_obj
            )
            logging.info('Preprocessor pickle file saved')

            return (
                train_arr, test_arr, self.data_transformation_config.preprocessor_obj_file_path
            )

        except Exception as e:
            logging.info("An exception has occurred in initiate_data_transformation")
            raise CustomException(e, sys)


import matplotlib.pyplot as plt
import numpy as np
import matplotlib

matplotlib.rcParams['pdf.fonttype'] = 42
matplotlib.rcParams['ps.fonttype'] = 42
plt.rcParams["font.size"] = 16

runtimes=[]
lengths=[]
makespans = []
for i in range(10):
    with open("../build/logs/"+"29-29-40-"+str(i)+"-100.txt", "r") as res_file:
        runtime = []
        length = []
        makespan = []
        for _ in range(40):
            r, l, m = map(int, res_file.readline().split())
            runtime.append(r)
            length.append(l)
            makespan.append(m)
        runtimes.append(runtime)
        lengths.append(length)
        makespans.append(makespan)

runtimes = np.array(runtimes)/1000.
lengths = np.array(lengths)
makespans = np.array(makespans)

fig, ax = plt.subplots(figsize = (5, 4))

from matplotlib.ticker import ScalarFormatter
#plt.legend()
plt.xlim(1.9, 41)
#plt.ylim(16,43)
ax.set_xscale('log')
ax.set_yscale('log')
ax.yaxis.set_major_formatter(ScalarFormatter())
ax.yaxis.set_minor_formatter(ScalarFormatter())
#ax.set_aspect('equal')
plt.ylabel("Makespan")
plt.xlabel("Number of agents")
plt.errorbar(range(2,41),makespans[:,1:].mean(axis=0),makespans[:,1:].std(axis=0))
fig.subplots_adjust(left=0.16, right=0.98, bottom=0.16, top=0.95)
plt.savefig("./makespan.pdf")
plt.show()


import pandas as pd
import numpy as np
import sys
import warnings

warnings.filterwarnings("ignore")

file_in = str(sys.argv[1])
file_out = str(sys.argv[2])
path = "../results/C45-C.p2_in/"

df = pd.read_csv(path + file_in, sep="\s")
keep_cols = ["positive"]
new_df = df[keep_cols]
new_df.index += 1
new_df.index.name = 'id'
new_df.columns.values[0] = 'class'
new_df.to_csv(path + file_out)


from behave import given, when, then, register_type
import parse
from src.Rover import Rover
from src.Plateau import Plateau

@parse.with_pattern(r"\d+")
def parse_number(text):
    return int(text)

register_type(Number=parse_number)

@given(
    "the rover is at position ({initial_x:Number}, {initial_y:Number}) and facing '{direction}'"
)
def step_impl(context, initial_x: int, initial_y: int, direction: str):
    plateau = Plateau(4, 4)
    context.rover = Rover(initial_x, initial_y, direction, plateau)

@when("the rover moves forward one step")
def step_impl(context):
    context.rover.navigate("M")

@then("the rover is at position ({final_x:Number}, {final_y:Number})")
def step_impl(context, final_x: int, final_y: int):
    assert context.rover.position_x == final_x
    assert context.rover.position_y == final_y


# Given an array of integers nums and an integer target,
# return indices of the two numbers such that they add up to target.
# You may assume that each input would have exactly one solution,
# and you may not use the same element twice.
# You can return the answer in any order.

from typing import List

def twoSum(nums: List[int], target: int) -> List[int]:
    ref_dict = {}
    for i in range(len(nums)):
        search = target - nums[i]
        if ref_dict.get(search) is not None:
            return [ref_dict[search], i]
        else:
            ref_dict[nums[i]] = i

nums = [2,7,11,15]
target = 9
twoSum(nums, target)
# [0,1]

nums = [3,2,4]
target = 6
twoSum(nums, target)
# [1,2]

nums = [3,3]
target = 6
twoSum(nums, target)
# [0,1]


import logging
from pathlib import Path
from typing import Any, Dict, Set, Union

from constants import DASH,PACKAGE_NAME,Strategy
from calculator import Calculator
from s3id_result import S3IDResultMatch, S3IDResultMismatch

logging.basicConfig(level=logging.DEBUG)

log = logging.getLogger(f"{PACKAGE_NAME}.{__name__}")

class Comparator(Calculator):
    @classmethod
    def run(
        cls,
        etag: str,
        local_file_path: Path,
        threshold_in_bytes: int,
        partition_set_in_bytes: Set[int],
    ) -> Dict[str, Union[int, str]]:
        """
        Iteratively create ETag values over a range of chunk sizes in hopes of matching 'etag'
        and return a summary result of a match or non-match.
        """
        result: Dict[str, Any] = Comparator(
            etag,
            local_file_path,
            threshold_in_bytes,
            partition_set_in_bytes,
        ).summary()
        if result["match"]:
            log.info(f"S3 object etag matched local file: '{local_file_path}'")
            return result
        else:
            log.info(
                f"S3 object etag did NOT match local file: '{local_file_path}' with any chunk sizes"
            )
            return result

    def __init__(
        self,
        etag: str,
        local_file_path: Path,
        threshold_in_bytes: int,
        partition_set_in_bytes: Set[int],
    ) -> None:
        if not etag:
            raise ValueError("'etag' cannot be blank.")

        self.etag: str = etag
        super().__init__(local_file_path, (Strategy.MULTI_PART if DASH in self.etag else Strategy.SINGLE_PART), threshold_in_bytes)

        if not isinstance(partition_set_in_bytes, set) or not all(
            isinstance(x, int) for x in partition_set_in_bytes
        ):
            raise ValueError("'partition_set_in_bytes' must be a set of integers")

        self.partition_set_in_bytes = partition_set_in_bytes

    def summary(self) -> Dict[str, Union[int, str]]:
        """
        Calculate an ETag from the local file contents.  If a matching ETag has been
        found to the object_etag, return:
        { "match": True, "partition_set_in_bytes": <partition_set_in_bytes> }

        If no match is found, return: { "match": False }
        """
        for partition_in_bytes in sorted(self.partition_set_in_bytes):
            result = self.calculate(partition_in_bytes)
            signature = result["signature"]
            log.debug(
                f"Calculated ETag: {signature} with chunk size (bytes): {partition_in_bytes}"
            )
            if self.etag.replace('"', "") == signature.replace('"', ""):
                return S3IDResultMatch(
                    signature, result["strategy"], partition_in_bytes
                ).summary()

        return S3IDResultMismatch(signature).summary()


def showBill():#บิลคิดเงิน
    totalPrice=0
    print("-------My food-------")
    for number in range(len(listMenu)):
        totalPrice+=(listMenu[number][1])
        print(listMenu[number][0],"                  ",listMenu[number][1],"BATH")

    print("total price","          ",totalPrice,"BATH")

menuPrice= {"กระเพราหมูสับ":50,"ข้าวผัดหมู":45,"ก๋วยเตี๋ยวหมู":40,"น้ำเปล่า":10}#ราคาขออาหาร

print("Welcome to OAT RESTUARANT")
listMenu=[]

while True:
    print("input (exit) after your order finish.")
    menuCook=input("name of menu:")

    if menuCook.lower() == "exit":
        break
    else:

        listMenu.append([menuCook,menuPrice[menuCook]])

print(listMenu)

showBill()


#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:Haccer
import os
# os.system('calc')
cmd_res = os.system('dir')#输出到屏幕上，无内存存储
print('--->',cmd_res)
cmd_res = os.popen('dir').read()
print('--->>',cmd_res)
os.mkdir('test.py')
os.mkdir('os')


class Labyrinth:
    """ Base class to create labyrinth in the game """
    def __init__(self, map_file):
        """ It initializes map structure in nested list.
        Each line is a list into Labyrinth list"""
        with open(map_file) as file:
            self.labyrinth_structure = [
                    [letter for letter in line if letter != "\n"]
                    for line in file
                ]

    def display(self):
        """ It displays the map by removing unnecessary
        characters (, "" []) """
        for line in self.labyrinth_structure:
            print("".join(line))

    def find_letter_position(self, letter):
        """ It finds a letter (str) in the map nested list and return its
        coordinates y, x (int) in (tuple) """
        y = 0
        x = 0
        for line in self.labyrinth_structure:
            x = 0
            for c in line:
                if c == letter:
                    return (y, x)
                x += 1
            y += 1

    def retrieve_letter(self, y, x):
        """ It returns a letter from the map nested list based on
        its coordinates """
        return self.labyrinth_structure[y][x]

    def list_position_letter(self, letter):
        """ It returns in a list the coordinates of letter(s) from
        map nested list """
        position_list = []
        y = 0
        x = 0
        for line in self.labyrinth_structure:
            x = 0
            for c in line:
                if c == letter:
                    position_list.append((y, x))
                x += 1
            y += 1
        return position_list

    def replace_letter(self, y, x, letter):
        """ It replaces the letter at given coordinates by the letter given """
        self.labyrinth_structure[y][x] = letter


import os
import os.path

def clean():
	if not os.path.exists(os.path.realpath('./img')):
		os.mkdir(os.path.realpath('./img'))
	else:
		del_file(os.path.realpath('./img'))
	try_remove('ffmpeg.log')
	try_remove('video.mp3')
	try_remove('videos.gz')
	try_remove('videos_layout.bmp')
	try_remove('temp.zip')
	try_remove('videos_layout_decode.gz')
	try_remove('videos_layout_decode.jpg')

def del_file(path):
	for i in os.listdir(path):
		path_file = os.path.join(path,i)
		if os.path.isfile(path_file):
			os.remove(path_file)
		else:
			del_file(path_file)

def try_remove(name:str):
	if os.path.isfile(name):
		os.remove(name)


from typing import List, Optional

from sqlalchemy.orm import Query

from app.lib import db
from app.models import Student
from app.schemas import StudentCreate

def create(student: StudentCreate) -> Student:
    session = db.get_session()
    db_student = Student(**student.dict())
    session.add(db_student)
    session.commit()
    session.refresh(db_student)
    return db_student

def get(sort: str, name: Optional[str]) -> List[Student]:
    session = db.get_session()
    query = session.query(Student)
    query = _filter(query, name)
    return _sort(query, sort)

def get_one(id: int) -> Optional[Student]:
    session = db.get_session()
    return session.query(Student).filter(Student.id == id).first()

def update(id: int, student: StudentCreate) -> Student:
    session = db.get_session()
    db_student = session.query(Student).filter(Student.id == id).first()
    db_student.name = student.name
    session.commit()
    session.refresh(db_student)
    return db_student

def delete(id: int) -> None:
    session = db.get_session()
    db_student = session.query(Student).filter(Student.id == id).first()
    session.delete(db_student)
    session.commit()

def _filter(query: Query, name: Optional[str]) -> Query:
    if name:
        query = query.filter(Student.name == name)
    return query

def _sort(query: Query, sort: str) -> List[Student]:
    order_by = sort_params[sort]
    return query.order_by(*(order_by,)).all()

sort_params = {
    "name:asc": Student.name.asc(),
    "name:desc": Student.name.desc(),
}


class DES_KeySchedule():
    def __init__(self, key):
        self.offsets = [1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1]
        key_bits = self.__bytes_to_bits(key)
        if len(key_bits) > 64:
            key_bits = key_bits[:64]
        else:
            key_bits = key_bits.zfill(64)
        self.key_bits = key_bits

    def __bytes_to_bits(self, block):
        return ''.join('{:08b}'.format(i) for i in block)

    def __bits_to_bytes(self, block):
        splitted = [''.join(block[i:i+8]) for i in range(0, len(block), 8)]
        int_bytes = map(lambda x: int(x, 2), splitted)
        return b''.join(map(lambda x: bytes([x]), int_bytes))

    def __permuted_choice_1(self, key_block):
        pc1_table = [57, 49, 41, 33, 25, 17, 9,
                     1, 58, 50, 42, 34, 26, 18,
                     10, 2, 59, 51, 43, 35, 27,
                     19, 11, 3, 60, 52, 44, 36,
                     63, 55, 47, 39, 31, 23, 15,
                     7, 62, 54, 46, 38, 30, 22,
                     14, 6, 61, 53, 45, 37, 29,
                     21, 13, 5, 28, 20, 12, 4]
        permuted = [key_block[i-1] for i in pc1_table]
        return permuted

    def __permuted_choice_2(self, key_block):
        pc2_table = [14, 17, 11, 24, 1, 5,
                     3, 28, 15, 6, 21, 10,
                     23, 19, 12, 4, 26, 8,
                     16, 7, 27, 20, 13, 2,
                     41, 52, 31, 37, 47, 55,
                     30, 40, 51, 45, 33, 48,
                     44, 49, 39, 56, 34, 53,
                     46, 42, 50, 36, 29, 32, ]
        permuted = [key_block[i-1] for i in pc2_table]
        return permuted

    def __rotate_left(self, block, offset):
        return block[offset:] + block[:offset]

    def generate(self):
        key_block = self.key_bits
        keys = []

        pc1_block = self.__permuted_choice_1(key_block)
        left_block = pc1_block[:len(key_block)//2]
        right_block = pc1_block[len(key_block)//2:]
        for i in range(16):
            left_block = self.__rotate_left(left_block, self.offsets[i])
            right_block = self.__rotate_left(right_block, self.offsets[i])
            full_key = left_block + right_block
            round_key_bits = self.__permuted_choice_2(full_key)
            round_key = self.__bits_to_bytes(round_key_bits)
            keys.append(round_key)
        return keys


# File:            ex_8.13-linear_regression.py
# Date:            2019-12-20
# Author:          "Hannes Thiersen" <hannesthiersen@gmail.com>
# Version:         0.1
# Description:
#   Write a program that graphically plots a regression line, that is, the line
#   with best fit through a collection of points. First ask the user to
#   specify the data points by clicking on them in a graphics window. To find
#   the end of input, place a small rectangle labeled "Done" in the lower left
#   corner of the window; the program will stop gathering points when the user
#   clicks inside that rectangle.
#
#   The regression line is the line with the following equation:
#
#       y = y_bar + m(x - x_bar)
#
#   where
#
#       m = [sum(x_i*y_i) - n*x_bar*y_bar] / [sum(x_i**2)- n*x_bar**2]
#
#   x_bar is the mean of the x-values, y_bar is the mean of the y-values, and n
#   is the number of points.
#
#   As the user clicks on point, the program should draw then in the graphics
#   window and keep track of the count of input values and the running sum of
#   x, y, x**2 and xy. When the user clicks inside the "Done" rectangle, the
#   program then computes value of y (using the equation above) corresponding
#   to the x values at the left and right edges of the window. After the line
#   is drawn, the program will pause for another mouse click before closing the
#   window and quitting

#------------------------------------------------------------------------------
# IMPORTS
#------------------------------------------------------------------------------
from graphics import *

#------------------------------------------------------------------------------
# FUNCTIONS
#------------------------------------------------------------------------------
def drawWindow():
    width = 800
    height = 600
    win = GraphWin("Data Canvas", width, height)
    return win

#------------------------------------------------------------------------------
def clickPoints(window):

    llcorner = Point(710, 540)
    urcorner = Point(790, 590)
    button = Rectangle(llcorner, urcorner)
    button.draw(window)

    text = Text(button.getCenter(),"Done")
    text.draw(window)

    points = []
    click = window.getMouse()

    while not clickedDone(click, llcorner, urcorner):
        click.draw(window)
        points.append(click)
        click = window.getMouse()

    return points

#------------------------------------------------------------------------------
def clickedDone(position, lowerleft, upperright):

    if (
            lowerleft.getX() < position.getX() < upperright.getX() and
            lowerleft.getY() < position.getY() < upperright.getY()
            ):
        return True
    return False

#------------------------------------------------------------------------------
def drawRegression(win, points):

    m, c = calculateLine(points)
    p0 = Point(0, c)
    pn = Point(800, m*800 + c)
    bestfit = Line(p0, pn)
    bestfit.draw(win)
    return None

#------------------------------------------------------------------------------
def calculateLine(points):
    x = []
    y = []
    xy = []
    x2 = []
    amount = len(points)
    for point in points:
        px, py = point.getX(), point.getY()

        x.append(px)
        y.append(py)
        x2.append(px**2)
        xy.append(px*py)

    x_ave = sum(x)/amount
    y_ave = sum(y)/amount
    x2_ave = sum(x2)/amount

    slope = (sum(xy) - amount*x_ave*y_ave) / (sum(x2) - amount*x_ave**2)
    constant = y_ave - slope*x_ave

    return slope, constant

#------------------------------------------------------------------------------
def haltQuit(win):
    Text(Point(800/2, 590), "Click to exit").draw(win)
    confirm = win.getMouse()
    win.close()
    return None

#------------------------------------------------------------------------------
# MAIN
#------------------------------------------------------------------------------
def main():

    window = drawWindow()
    points = clickPoints(window)
    drawRegression(window, points)
    haltQuit(window)

#------------------------------------------------------------------------------
if __name__ == '__main__':
    main()


from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import tensorflow as tf
import data_provider
import models
import math

from menpo.visualize import print_progress
from pathlib import Path
from tensorflow.python.platform import tf_logging as logging

slim = tf.contrib.slim

FLAGS = tf.app.flags.FLAGS
tf.app.flags.DEFINE_integer('batch_size', 1, '''The batch size to use.''')
tf.app.flags.DEFINE_string('model', 'audio','''Which model is going to be used: audio, video, or both ''')
tf.app.flags.DEFINE_string('dataset_dir', 'Addresse/tf_records', 'The tfrecords directory.')
tf.app.flags.DEFINE_string('checkpoint_dir', 'ckpt/train/', 'The checkpoint directory.')
tf.app.flags.DEFINE_string('log_dir', 'ckpt/eval/', 'The directory to save the event files.')
tf.app.flags.DEFINE_integer('num_lstm_modules', 2, 'Number of LSTM modules to use.')
tf.app.flags.DEFINE_integer('num_examples', None, 'The number of examples in the given portion.')
tf.app.flags.DEFINE_string('eval_interval_secs', 300, 'The number of examples in the test set')
tf.app.flags.DEFINE_string('portion', 'devel', 'The portion of the dataset to use -- `train`, `devel`, or `test`.')
tf.app.flags.DEFINE_string('task', 'addressee', 'The task to execute. `addressee`, `cold` or `snore`')

def evaluate(data_folder):
  """Evaluates the audio model.

  Args:
     data_folder: The folder that contains the data to evaluate the audio model.
  """

  g = tf.Graph()
  with g.as_default():
    # Load dataset.
    provider = data_provider.get_provider(FLAGS.task)(data_folder)
    num_classes = provider.num_classes
    audio, labels, num_examples = provider.get_split(FLAGS.portion, FLAGS.batch_size)

    # Define model graph.
    with slim.arg_scope([slim.batch_norm],
                           is_training=False):
      predictions = models.get_model(FLAGS.model)(audio, num_classes=provider.num_classes, num_lstm_modules=FLAGS.num_lstm_modules)
      pred_argmax = tf.argmax(predictions, 1)
      lab_argmax = tf.argmax(labels, 1)

      metrics = {
          "eval/accuracy": slim.metrics.streaming_accuracy(pred_argmax, lab_argmax, name='accuracy')
      }

      for i in range(num_classes):
          name ='eval/recall_{}'.format(i)
          recall = slim.metrics.streaming_recall(
                  tf.to_int64(tf.equal(pred_argmax, i)),
                  tf.to_int64(tf.equal(lab_argmax, i)), name=name)
          metrics[name] = recall

      names_to_values, names_to_updates = slim.metrics.aggregate_metric_map(metrics)

      summary_ops = []
      metrics = dict()
      for name, value in names_to_values.items():
        op = tf.summary.scalar(name, value)
        op = tf.Print(op, [value], name)
        summary_ops.append(op)
        metrics[name] = value

      # Computing the unweighted average recall and add it into the summaries.
      uar = sum([metrics['eval/recall_{}'.format(i)] for i in range(num_classes)]) / num_classes
      op = tf.summary.scalar('eval/uar', uar)
      op = tf.Print(op, [uar], 'eval/uar')
      summary_ops.append(op)

      num_examples = FLAGS.num_examples or num_examples
      num_batches = math.ceil(num_examples / float(FLAGS.batch_size))
      logging.set_verbosity(1)

      # Setup the global step.
      slim.get_or_create_global_step()

      # How often to run the evaluation.
      eval_interval_secs = FLAGS.eval_interval_secs

      slim.evaluation.evaluation_loop(
          '',
          FLAGS.checkpoint_dir,
          FLAGS.log_dir,
          num_evals=num_batches,
          eval_op=list(names_to_updates.values()),
          summary_op=tf.merge_summary(summary_ops),
          eval_interval_secs=eval_interval_secs)

def main(_):
    evaluate(FLAGS.dataset_dir)

if __name__ == '__main__':
    tf.app.run()


'''
Created on Feb 5, 2016

@author: yumilceh
'''
import matplotlib.pyplot as plt
import numpy as np
from numpy import random as np_rnd

if __name__ == '__main__':

    ## Adding the projects folder to the path##
    import sys,random
    sys.path.append("../../")

    ## Adding libraries##
    from exploration.systems.trash.Parabola_Test import ParabolicRegion as System
    from exploration.algorithm.AlgorithmRandom import Algorithm_Random as Algorithm
    from exploration.algorithm.AlgorithmRandom import MODELS
    from exploration.models.GMM_SM import GMM_SM
    from exploration.models.GMM_SS import GMM_SS
    from exploration.data.PlotTools import initializeFigure

    ## Simulation Parameters ##
    n_initialization=50
    n_experiments=10000

    random_seed=1234

    k_sm = 30
    sm_step=1000
    alpha_sm=0.05

    k_ss = 6
    ss_step=100
    alpha_ss=0.05

    ## To guarantee reproductible experiments##
    random.seed(random_seed)
    np_rnd.seed(random_seed)

    ## Creating Agent ##
    system=System()
    system.params.sigma_noise = 0.0000000001

    ## Creating models ##
    models=MODELS()

    models.f_sm = GMM_SM(system,
                         k_sm,
                         sm_step=sm_step,
                         alpha=alpha_sm)
    models.f_ss = GMM_SS(system,
                         k_ss,
                         ss_step=ss_step,
                         alpha=alpha_ss)

    ## Creating Simulation object, running simulation and plotting experiments##
    file_prefix='Sinus_GMM_'
    simulation1=Algorithm(system,
                          models,
                          file_prefix=file_prefix,
                          n_experiments = n_experiments
                          )

    simulation1.runNonProprioceptiveAlgorithm()

    initialization_data_sm_ss=simulation1.data.initialization_data_sm_ss
    initialization_data_im=simulation1.data.initialization_data_im
    simulation_data=simulation1.data.simulation_data

    s_dims = 2
    data_as_matrix = simulation_data.sensor_data.data.as_matrix()
    somatodata_as_matrix =simulation_data.somato_data.data.as_matrix()

    fig1,ax1=initializeFigure();
    fig1.show()
    #===========================================================================
    # fig1.show()
    #===========================================================================
    fig1.canvas.draw()
    plt.scatter(data_as_matrix[:, 0], data_as_matrix[:, 1], 0.8, color='k')
    fig1.canvas.draw()
    plt.hold(True)
    ax1.autoscale_view()

    data_as_matrix = data_as_matrix[np.where(somatodata_as_matrix[:,0] == 0)[:], :][0]
    plt.scatter(data_as_matrix[:, 0], data_as_matrix[:, 1], 0.8, color='r')
    fig1.canvas.draw()

    #===========================================================================
    # fig1,ax1=simulation_data.plot_2D(fig1,ax1,'sensor', 0, 'sensor', 1,"or")
    # fig1, ax1 = simulation1.models.f_sm.model.plot_gmm_projection(fig1,ax1,2, 3)
    #===========================================================================

    min_distance = 0.2
    change = True

    while change: #No optimal
        change = False
        for i in range(data_as_matrix.shape[0]):
            if change == True:
                break
            for j in range(i+1,data_as_matrix.shape[0]):
                distance_ij = np.linalg.norm(data_as_matrix[i,:]-data_as_matrix[j,:])
                if distance_ij < min_distance:
                    data_as_matrix = np.delete(data_as_matrix, j, 0)
                    change = True
                    break

    plt.plot(data_as_matrix[:, 0], data_as_matrix[:, 1], "or")
    fig1.canvas.draw()

    import pandas as pd
    from DataManager.SimulationData import SimulationData
    validation_data_set = SimulationData(simulation1.agent)
    validation_data_set.sensor_data.data = pd.DataFrame(data_as_matrix)
    validation_data_set.save_data('parabola_dataset_2.h5')

    try:
        str_opt = raw_input("Press [enter] to continue or press 'Y' to keep plots...   ")
        if str_opt == 'Y':
            plt.show()
    except SyntaxError:
        plt.show()
        pass


from django.http import Http404, HttpResponseRedirect, HttpResponse
from django_mako_plus.controller import view_function
import homepage.models as hmod
from django_mako_plus.controller.router import get_renderer
from django import forms
from django.contrib.auth.decorators import permission_required

templater = get_renderer('homepage')

@view_function
@permission_required('homepage.view_product', login_url='/homepage/login/')
def process_request(request):
    params = {}

    params["products"] = hmod.Product.objects.all().order_by('product_specification__category', 'product_specification__name')

    return templater.render_to_response(request, 'product.html', params)

#########################################################################
### Edit a product

@view_function
# @permission_required('homepage.change_product', login_url='/homepage/login/')
def edit(request):
    params = {}
    product_id_str = request.urlparams[1]

    if request.urlparams[0] == 'new':
        # print("It is a new product")
        form = ProductEditForm()
    else:
        # print("I'm editing an existing product")
        try:
            product = hmod.Product.objects.get(id=product_id_str)
        except hmod.Product.DoesNotExist:
            return HttpResponseRedirect('/homepage/product/')

        form = ProductEditForm(initial={
            'name': product.product_specification.name,
            'description': product.product_specification.description,
            'category': product.product_specification.category,
            'price': product.product_specification.price,
            'qty_on_hand': product.qty_on_hand,
            # 'producer': product.producer,
        })

    if request.method == 'POST':
        form = ProductEditForm(request.POST)  # POST is a dictionary of all the data sent with the form

        if form.is_valid():
            name = form.cleaned_data['name']
            category_str = form.cleaned_data['category']
            desc = form.cleaned_data['description']
            price = form.cleaned_data['price']
            qty_on_hand = form.cleaned_data['qty_on_hand']
            # producer_str = form.cleaned_data['producer']

            try:
                category = hmod.Category.objects.get(name=category_str)
            except hmod.Category.DoesNotExist:
                raise forms.ValidationError("Category doesn't exist")

            if request.urlparams[0] == 'new':
                ps = hmod.ProductSpecification()
                ps.name = name
                ps.category = category
                ps.description = desc
                ps.price = price
                ps.save()

                product = hmod.Product()
                product.qty_on_hand = qty_on_hand
                product.product_specification = ps
                product.save()
            else:
                try:
                    product = hmod.Product.objects.get(id=product_id_str)
                except hmod.Product.DoesNotExist:
                    return HttpResponseRedirect('/homepage/product/')

                product.product_specification.name = name
                product.product_specification.category = category
                product.product_specification.description = desc
                product.product_specification.price = price
                product.product_specification.save()

                product.qty_on_hand = qty_on_hand
                product.save()

            # try:
            #     producer = hmod.Organization.objects.get(given_name=form.cleaned_data['producer'])
            # except hmod.Organization.DoesNotExist:
            #     raise forms.ValidationError("Producer doesn't exist")
            # product.producer = producer
            # product.save()
            return HttpResponseRedirect('/homepage/product/')

    params['form'] = form

    return templater.render_to_response(request, 'product.edit.html', params)

class ProductEditForm(forms.Form):
    name = forms.CharField(label="Name*", max_length=100)
    description = forms.CharField(max_length=100, required=False)
    category = forms.ModelChoiceField(label="Category*", queryset=hmod.Category.objects, empty_label=None)
    price = forms.DecimalField(label="Price*",max_digits=10, decimal_places=2)
    qty_on_hand = forms.IntegerField(label="Quantity On Hand*")
    # producer = forms.ModelChoiceField(queryset=hmod.Organization.objects)
    # empty_label in a ModelChoiceField will not let you continue on past the clean if that field is required

    # def clean_producer(self):
    #     try:
    #         producer = hmod.Organization.objects.get(given_name=self.cleaned_data['producer'])
    #     except hmod.Organization.DoesNotExist:
    #         raise forms.ValidationError('The producer you entered does not exist.')
    #
    #     if producer.city == 'temp':
    #         raise forms.ValidationError('You must enter a producer for this product.')
    #     return self.cleaned_data['producer']

# @view_function
# # @permission_required('homepage.add_product', login_url='/homepage/login/')
# def create(request):
#     product = hmod.Product()
#     product.name = ''
#     product.category = ''
#     product.current_price = 0
#
#     try:
#         product.producer = hmod.Organization.objects.get(city='temp')
#     except hmod.Product.DoesNotExist:
#         raise forms.ValidationError("The temporary producer doesn't exist.")
#
#     product.save()
#
#     return HttpResponseRedirect('/homepage/product.edit/{}/new/'.format(product.id))

@view_function
# @permission_required('homepage.add_product', login_url='/homepage/login/')
def delete(request):
    # delete a product before product specification

    try:
        product = hmod.Product.objects.get(id=request.urlparams[0])
    except hmod.Product.DoesNotExist:
        return HttpResponseRedirect('/homepage/product/')

    ps = product.product_specification

    product.delete()

    if hmod.Product.objects.filter(product_specification=ps).count() != 0:
        ps.delete()

    return HttpResponseRedirect('/homepage/product/')


import os
import sys
sys.path.insert(0, os.path.abspath('../'))

import torch
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

#
sns.set_context('paper')
dataset = 'pubmed'
ptb_rate = 0.05
select_range = [0]

for select_index in select_range:

    target_records = torch.load(f"{dataset}-budgets.pth")
    budget_dict = target_records['budget']
    budget_collect = dict()
    for n, b in budget_dict.items():
        if b not in budget_collect:
            budget_collect[b] = list()
        budget_collect[b].append(n)

budget_dict = dict()

for k, v in budget_collect.items():
    budget_dict[k] = len(v)

keys = sorted(budget_dict.keys())
values = [budget_dict[key] for key in keys]
print(keys)
print(values)

data = pd.DataFrame({f'{dataset}': keys, 'y': values})

plt.figure(figsize=(12, 8))
# sns.set(font_scale=10.0)
sns.set(style="white")
# Create the distribution plot
p = sns.histplot(
    data=data,
    bins=range(21),
    x=f'{dataset}', weights='y',
    color='#4c72b0',
    alpha=0.5,
    edgecolor='k')
p.set_ylabel("The number of nodes", fontsize=30)
p.set_xlabel(f"Targeted attack budgets", fontsize=30)

plt.xlim((0, 21))
# Set x-axis labels as dictionary keys
plt.xticks(range(21))

plt.savefig(f"../pictures/budget_plot_{dataset}.pdf", format='pdf', dpi=1200)
plt.show()


import re
from functools import reduce
from datetime import datetime as dt

from mpygit import mpygit, gitutil

from django.test import TestCase, Client

from mfgd_app.models import UserProfile, CanAccess, Repository

class ChainTestCase(TestCase):
    REGEX_CHAIN_ENTS = (
        r"""
\s*"""
        r""""""
            r"""<a href="/linear/info/(?P<big_hash>[A-z0-9]*)/?">"""
                r"""(?P<short_hash>[A-z0-9]*)"""
            r""""""
        r"""	"""
        r"""\s*(?P<commit_msg>.*)	"""
        r"""\s*(?P<author>.*)	"""
        r"""\s*"""
            r"""\s*<time datetime="[0-9-]*">(?P<date>[0-9-]*)</time>"""
        r"""\s*	"""
        r"""\s*
"""
    )

    def setUp(self):
        Repository.objects.create(name="simple", path="tests/repo/simple", isPublic=True)
        Repository.objects.create(name="linear", path="tests/repo/linear", isPublic=True)
        Repository.objects.create(name="n_merge", path="tests/repo/n_merge", isPublic=True)

        self.client = Client()

    def tearDown(self):
        Repository.objects.all().delete()

    def _get_chain(self, repo_name):
        db_repo = Repository.objects.get(name=repo_name)
        repo = mpygit.Repository(db_repo.path)

        response = self.client.get(f"/{repo_name}/chain/", follow=True)
        self.assertEquals(response.status_code, 200)
        return response.content.decode()

    def test_displays_commits(self):
        content = self._get_chain("linear")
        repo = mpygit.Repository(Repository.objects.get(name="linear").path)
        repo_walker = gitutil.walk(repo, repo.HEAD, 5)
        for fs_ent, match in zip(repo_walker, re.finditer(self.REGEX_CHAIN_ENTS, content)):
            oid, short_oid, msg, committer, timestamp = match.groups()
            self.assertEqual(fs_ent.oid, oid)
            self.assertEqual(fs_ent.short_oid, short_oid)
            self.assertEqual(fs_ent.message, msg)
            self.assertEqual(fs_ent.committer.name, committer)

            fs_timestamp = dt.utcfromtimestamp(fs_ent.committer.timestamp).strftime("%Y-%m-%d")
            self.assertEqual(fs_timestamp, timestamp)

    def test_correct_ordering(self):
        content = self._get_chain("linear")
        # you can't stop me
        is_sorted, _ = reduce(
            lambda acc, v: (acc[0] and acc[1] >= v, v),
            map(lambda x: x.group(5), re.finditer(self.REGEX_CHAIN_ENTS, content)),
            (True, "9999-99-99"))
        self.assertTrue(is_sorted)

    def test_n_way_merge_flattened(self):
        content = self._get_chain("n_merge")
        order = (5, 6, 4, 3, 2, 1)
        for n, match in zip(order, re.finditer(self.REGEX_CHAIN_ENTS, content)):
            self.assertEqual(f"commit #{n}", match.group(3))

    def test_entries_link_to_info(self):
        # this is ensured by displays_commits because it must match the regex which
        # then checks to see whether the long oid in the anchor tag is associated with
        # the short oid
        pass

    def test_empty_chain(self):
        db_repo = Repository.objects.get(name="simple")
        repo = mpygit.Repository(db_repo.path)

        response = self.client.get("/simple/chain/", follow=True)
        self.assertEquals(response.status_code, 200)
        content = response.content.decode()
        self.assertFalse(re.search(self.REGEX_CHAIN_ENTS, content))