# import the libraries as shown below

from tensorflow.keras.layers import Input, Lambda, Dense, Flatten,Conv2D
from tensorflow.keras.models import Model
from tensorflow.keras.applications.vgg19 import VGG19
from tensorflow.keras.applications.resnet50 import preprocess_input
from tensorflow.keras.preprocessing import image
from tensorflow.keras.preprocessing.image import ImageDataGenerator,load_img
from tensorflow.keras.models import Sequential
import numpy as np
from glob import glob
import matplotlib.pyplot as plt
print('Import successful')

# re-size all the images to this
IMAGE_SIZE = [224, 224]

train_path = './data/Train'
valid_path = './data/Test'

# Import the Vgg 16 library and add preprocessing layer to the front of VGG
# Here we will be using imagenet weights

mobilnet = VGG19(input_shape=IMAGE_SIZE + [3], weights='imagenet', include_top=False)

# don't train existing weights
for layer in mobilnet.layers:
    layer.trainable = False

# useful for getting number of output classes
folders = glob('./data/Train/*')
print(folders)

# our layers -
x = Flatten()(mobilnet.output)

prediction = Dense(len(folders), activation='softmax')(x)

# create a model object
model = Model(inputs=mobilnet.input, outputs=prediction)

# to check model summary
model.summary()

# compiling model
model.compile(
  loss='categorical_crossentropy',
  optimizer='adam',
  metrics=['accuracy']
)

# Use the Image Data Generator to import the images from the dataset
from tensorflow.keras.preprocessing.image import ImageDataGenerator

train_datagen = ImageDataGenerator(rescale = 1./255,
                                   shear_range = 0.2,
                                   zoom_range = 0.2,
                                   horizontal_flip = True)

test_datagen = ImageDataGenerator(rescale = 1./255)

# preparing training set for model
training_set = train_datagen.flow_from_directory('./data/Train',
                                                 target_size = (224, 224),
                                                 batch_size = 10,
                                                 class_mode = 'categorical')
#preparing the  test set
test_set = test_datagen.flow_from_directory('./data/Test',
                                            target_size = (224, 224),
                                            batch_size = 10,
                                            class_mode = 'categorical')


import Updated_Media_Analyzer as MA
import pytest
import tweepy

def test_get_keyword_input(monkeypatch):
    monkeypatch.setattr('builtins.input', lambda _:"Genshin,Boston University")
    keywords_input = input('give me keywords (use comma to split):')
    get_keywords = MA.get_keywords_from_input(keywords_input)
    assert get_keywords == ['Genshin','Boston University']

def test_get_keyword_param():
    get_keywords = MA.get_keywords_from_param()
    assert get_keywords == ['Media_Analyzer/Test_Updated_Media_Analyzer.py']


import open3d
import numpy as np
import math
import random
from scipy.spatial.transform import Rotation
import os
import torch

import matplotlib
matplotlib.use('TkAgg')
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.cm as cm

import kitti.options
import oxford.options
import nuscenes_t.options
from data.kitti_pc_img_pose_loader import KittiLoader
from data.oxford_pc_img_pose_loader import OxfordLoader
from data.nuscenes_pc_img_pose_loader import nuScenesLoader

from util import vis_tools

def transform_pc_np(P, pc_np):
    """

    :param pc_np: 3xN
    :param P: 4x4
    :return:
    """
    pc_homo_np = np.concatenate((pc_np,
                                 np.ones((1, pc_np.shape[1]), dtype=pc_np.dtype)),
                                axis=0)
    P_pc_homo_np = np.dot(P, pc_homo_np)
    return P_pc_homo_np[0:3, :]

if __name__ == '__main__':
    # root_path = '/ssd/jiaxin/point-img-feature/kitti/save/1.17-accu-node128'
    root_path = '/ssd/jiaxin/point-img-feature/oxford/save/1.16-fine-wGround-nocrop-0.5x384x640'
    # root_path = '/ssd/jiaxin/point-img-feature/nuscenes_t/save/3.3-160x320-accu'
    visualization_output_folder = 'visualization'
    visualization_output_path = os.path.join(root_path, visualization_output_folder)
    data_output_folder = 'data'
    data_output_path = os.path.join(root_path, data_output_folder)

    is_plot = False
    batch_size = 8
    H = 160  # kitti=160, oxford=288/192/384, nuscenes 160
    W = 320  # kitti=512, oxford=512/320/640, nuscenes 320
    fine_resolution_scale = 1 / 32.0

    batch_id = 605
    item_id = 4
    filename = '%06d_%02d' % (batch_id, item_id)

    if 'kitti' in root_path:
        dataset = 'kitti'
        opt = kitti.options.Options()
    elif 'oxford' in root_path:
        dataset = 'oxford'
        opt = oxford.options.Options()
    elif 'nuscenes' in root_path:
        dataset = 'nuscenes'
        opt = nuscenes_t.options.Options()
    opt.batch_size = batch_size

    if dataset == 'kitti':
        testset = KittiLoader(opt.dataroot, 'val', opt)
    elif dataset == 'oxford':
        testset = OxfordLoader(opt.dataroot, 'val', opt)
    elif dataset == 'nuscenes':
        testset = nuScenesLoader(opt.dataroot, 'val', opt)
    # testloader = torch.utils.data.DataLoader(testset, batch_size=opt.batch_size, shuffle=False,
    #                                          num_workers=opt.dataloader_threads, pin_memory=True)
    print('#testing point clouds = %d' % len(testset))

    point_data_np = np.load(os.path.join(data_output_path, filename + '_pc_label.npy'))

    pc_np = point_data_np[0:3, :].astype(np.float64)
    coarse_predictions_np = point_data_np[3, :].astype(np.int)
    coarse_labels_np = point_data_np[4, :].astype(np.int)
    fine_predictions_np = point_data_np[5, :].astype(np.int)
    fine_labels_np = point_data_np[6, :].astype(np.int)

    K_np = np.load(os.path.join(data_output_path, filename + '_K.npy')).astype(np.float64)
    P_gt_np = np.load(os.path.join(data_output_path, filename + '_P.npy')).astype(np.float64)
    if P_gt_np.shape[0] == 3:
        P_gt_np = np.concatenate((P_gt_np, np.identity(4)[3:4, :]), axis=0)

    pcd = open3d.geometry.PointCloud()
    pcd.points = open3d.utility.Vector3dVector(np.transpose(pc_np[0:3, :]))

    # height coloring
    # pc_np_y_min, pc_np_y_max = np.min(pc_np[1, :]), np.max(pc_np[1, :])
    # pc_np_y_interval = pc_np_y_max - pc_np_y_min
    # height_normalized = (pc_np[1, :] - pc_np_y_min) / pc_np_y_interval
    # height_normalized = np.clip(height_normalized, 0, 1)
    # height_colors = plt.cm.jet(height_normalized)
    # print(height_colors.shape)

    # label coloring
    tp_mask = np.logical_and(coarse_predictions_np == coarse_labels_np, coarse_predictions_np==1)  # green
    fp_mask = np.logical_and(coarse_predictions_np==1, coarse_labels_np==0)  # blue
    fn_mask = np.logical_and(coarse_predictions_np==0, coarse_labels_np==1)  # red
    other_mask = np.logical_not(np.logical_or(np.logical_or(tp_mask, fp_mask), fn_mask))

    color_np = np.zeros((pc_np.shape[1], 3))
    color_np[tp_mask, 1] = 1
    color_np[fp_mask, 2] = 1
    color_np[fn_mask, 0] = 1
    color_np[other_mask, :] = np.asarray([0.5, 0.5, 0.5])
    pcd.colors = open3d.utility.Vector3dVector(color_np)

    open3d.visualization.draw_geometries([pcd])

    # fig_gt = plt.figure(figsize=(9, 9))
    # plt.axis('off')
    # plt.grid(b=None)
    # ax_gt = Axes3D(fig_gt)
    # ax_gt.set_title("registration")
    # ax_gt.axis('off')
    # ax_gt.grid(b=None)
    # vis_tools.plot_pc(pc_np, color=coarse_labels_np, size=6, ax=ax_gt,
    #                   elev=0, azim=-90)
    #
    # plt.show()


#!/usr/bin/env python3
"""module for wait_n async function"""

import importlib.util
import asyncio
import random
from typing import List

# Specify the module name including the full path
module_name = '0-basic_async_syntax'
mod_file_pat = './0-basic_async_syntax.py'
# Use importlib.util.module_from_spec() to load the module
module_spec = importlib.util.spec_from_file_location(module_name, mod_file_pat)
module = importlib.util.module_from_spec(module_spec)
module_spec.loader.exec_module(module)

async def wait_n(n: int, max_delay: int) -> List[float]:
    """ function to return list of delays """
    delays: List[float] = []
    tasks = [module.wait_random(max_delay) for _ in range(n)]
    results = await asyncio.gather(*tasks)

    delays.extend(results)
    delays.sort()

    return delays

async def main() -> None:
    """ main function to run wait_n function """
    res: List[float] = await wait_n()
    return res

if __name__ == "__main__":
    """ initialisation of module """
    asyncio.run(main())


import numpy as np
import pickle
import msgpack
import csv
import json
import os

class NpEncoder(json.JSONEncoder):
    def default(self, obj):
        if isinstance(obj, np.integer):
            return int(obj)
        if isinstance(obj, np.floating):
            return float(obj)
        if isinstance(obj, np.ndarray):
            return obj.tolist()
        return super(NpEncoder, self).default(obj)

try:
    # Open file
    fileName = "animal-shelter-intakes-and-outcomes"
    openfile = open("assets/data/5/"+fileName+".json")

    allAnimals = np.array(json.load(openfile))

    # print(len(allAnimals)) # 40440 animals

    # print(allAnimals[1000]) # Example
    # {
    #     'animal_id': 'A506699',
    #     'animal_name': 'PUMPKIN',
    #     'animal_type': 'CAT',
    #     'primary_color': 'CALICO',
    #     'secondary_color': None,
    #     'sex': 'Female',
    #     'dob': '2009-09-09',
    #     'age': 14.0,
    #     'intake_date': '2017-06-09',
    #     'intake_cond': 'NORMAL',
    #     'intake_type': 'OWNER SURRENDER',
    #     'intake_subtype': 'OTC',
    #     'reason': 'MOVE',
    #     'outcome_date': '2017-06-24',
    #     'crossing': '3500 BLK OLEANDER ST, SEAL BEACH, CA 90740',
    #     'jurisdiction': 'SEAL BEACH',
    #     'outcome_type': 'ADOPTION',
    #     'outcome_subtype': 'REMOTE EVT',
    #     'intake_is_dead': 'Alive on Intake',
    #     'outcome_is_dead': 'FALSE',
    #     'was_outcome_alive': 1
    # }

    allCategories = []

    for animal in allAnimals:
        if(animal["animal_type"] not in allCategories):
            allCategories.append(animal["animal_type"])

    outputCategories = []

    for category in allCategories:
        total = 0
        totalFemale = 0
        totalMale = 0
        totalUnknownGender = 0
        totalDataHaveAge = 0
        totalValueAge = 0
        totalUnknownAge = 0
        maxAge = 0
        minAge = 99999
        allAgesNoNone = []
        nameOfAnimals = []
        dataAnimal = []

        for animal in allAnimals:
            if(animal["animal_type"] == category):
                total += 1
                if(animal["animal_name"] not in nameOfAnimals):
                    nameOfAnimals.append(animal["animal_name"])
                    totalUnknownAgeAnimal = 0
                    maxAgeAnimal = 0
                    minAgeAnimal = 99999

                    if(animal["age"] == None):
                        totalUnknownAgeAnimal = 1
                    else:

                        if(animal["age"] > maxAgeAnimal):
                            maxAgeAnimal = animal["age"]
                        if(animal["age"] < minAgeAnimal):
                            minAgeAnimal = animal["age"]

                    dataAnimal.append({
                        "name": animal["animal_name"],
                        "totalAnimals": 1,
                        "totalUnknownAge": totalUnknownAgeAnimal,
                        "maximumAge": maxAgeAnimal,
                        "minimumAge": minAgeAnimal,
                    })
                else:
                    for data in dataAnimal:
                        if(data["name"] == animal["animal_name"]):
                            data["totalAnimals"] += 1
                            if(animal["age"] == None):
                                data["totalUnknownAge"] += 1
                            else:
                                if(animal["age"] > data["maximumAge"]):
                                    data["maximumAge"] = animal["age"]
                                if(animal["age"] < data["minimumAge"]):
                                    data["minimumAge"] = animal["age"]

                if(animal["sex"] == "Female"):
                    totalFemale += 1
                elif(animal["sex"] == "Male"):
                    totalMale += 1
                else:
                    totalUnknownGender += 1

                if(animal["age"] == None):
                    totalUnknownAge += 1
                else:
                    totalDataHaveAge += 1
                    totalValueAge += animal["age"]
                    allAgesNoNone.append(animal["age"])
                    if(animal["age"] > maxAge):
                        maxAge = animal["age"]
                    if(animal["age"] < minAge):
                        minAge = animal["age"]

        averageAge = totalValueAge / totalDataHaveAge
        standartDeviationOfAge = np.std(allAgesNoNone)
        outputCategories.append({
            "category": category,
            "totalAnimals": total,
            "totalMale": totalMale,
            "totalFemale": totalFemale,
            "totalUnknownGender": totalUnknownGender,
            "totalUnknownAge": totalUnknownAge,
            "maximumAge": maxAge,
            "minimumAge": minAge,
            "averageAge": averageAge,
            "standartDeviationOfAge": standartDeviationOfAge,
            "animals": dataAnimal,
        })

    # Export the result to csv
    with open("assets/output/5/output_categories.csv", "w") as outfile:
        writer = csv.writer(outfile)
        writer.writerow(outputCategories)

    # Export the result to json
    with open("assets/output/5/output_categories.json", "w") as outfile:
        jsonOutputCategories = json.dumps(outputCategories, indent=4, cls=NpEncoder)
        outfile.write(jsonOutputCategories)

    # Export the result to pkl
    with open("assets/output/5/output_categories.pkl", "wb") as outfile:
        pickle.dump(outputCategories, outfile, protocol=pickle.HIGHEST_PROTOCOL)

    # Export the result to msgpack
    with open("assets/output/5/output_categories.msgpack", "wb") as outfile:
        packed = msgpack.packb(outputCategories)
        outfile.write(packed)

    # Get file size
    file_size_csv = os.path.getsize("assets/output/5/output_categories.csv")
    print("File Size using (csv) is :", file_size_csv, "bytes")
    file_size_json = os.path.getsize("assets/output/5/output_categories.json")
    print("File Size using (json) is :", file_size_json, "bytes")
    file_size_pkl = os.path.getsize("assets/output/5/output_categories.pkl")
    print("File Size using (pkl) is :", file_size_pkl, "bytes")
    file_size_msgpack = os.path.getsize("assets/output/5/output_categories.msgpack")
    print("File Size using (msgpack) is :", file_size_msgpack, "bytes")
except Exception as e:
    print(e)


#calibration
from __future__ import division
import time
import Adafruit_PCA9685

from numpy import *
from math import *
import time
import numpy as np

pwm = Adafruit_PCA9685.PCA9685()

def set_servo_pulse(channel, pulse):
	pulse_length = 1000000
	pulse_length //=60
	print('{0}us per bit' .format (pulse_length))
	pulse *= 1000
	pulse //= pulse_length
	pwm.set_pwm(channel, 0, pulse)
	pwm.set_pwm_freq(60)

while True:
	pwm.set_pwm(0, 0, 598-244) #port zero : right front tibia
	time.sleep(0.001)

	pwm.set_pwm(1, 0, 770+289-89) #port 1: right front femur
	pwm.set_pwm(2, 0, 610)                            #port 2: right hip

	pwm.set_pwm(3, 0, 576-367) #port 3: left front tibia  add 100 to this
	pwm.set_pwm(4, 0, 623) #port 4: left front femur
	pwm.set_pwm(5, 0, 610)                            #port 5: left hip

	pwm.set_pwm(8, 0, 597+375) #port 8: left back femur
	pwm.set_pwm(9, 0, 742-364) #port 9: left back tibia
	pwm.set_pwm(10, 0, 610)                            #port 10: left back hip

	pwm.set_pwm(12, 0, 536) #port 6: right back femur
	pwm.set_pwm(7, 0, 626) #port 7: right back tibia
	pwm.set_pwm(11, 0, 610)


name = input("Type your name ")
print("Welcome", name, "to the start of your journey!")

answer = input("You come to a fork in the road... Do you choose right or left? ").lower()

if answer == "left":

    answer = input("You have come to a long winding river... Do you choose to walk around or swim? walk/swim ").lower()
    if answer == "swim":
        print("You forgot you can't swim and drowned!")

    elif answer == "walk":
        print("Did you miss the part where it is a LONG winding river... You walked too long and died of exhaustion!")

    else:
        print("not a valid answer you lose")

elif answer == "right":

    answer = input("After walking for awhile you come to a bridge... Do you choose to cross or to turn back? cross/turn back ").lower()
    if answer == "cross":
        answer = input("You cross the bridge safely and then encounter a stranger... Do you choose to talk or walk past? talk/walk past ")

        if answer == "talk":
            print("The stranger gives you gold for the rest of your journey, you win.")
        elif answer == "walk past":
            print("You get shot with an arrow for trespassing.")

        else:
            print("not valid you lose")

    elif answer == "turn back":
        print("You end up back where you started. Game Over.")
    else:
        print("not a valid answer you lose")

else:
    print("you lose")

print("Thank you for playing.", name, "goodbye.")


from __future__ import print_function
import numpy as np
import database as db
from file_utils import load_json
from file_utils import load_pickle

class LMTD:

	def __init__(self):
		'''
			features_file: path the features dictionary
		'''
		self.__fetch_splits__()

	def load_precomp_features(self, features_file='../features/lmtd9_resnet152.pickle'):
		self.feature_dict = load_pickle(features_file)

	def get_split_ids(self, split='train'):
		self.ids, self.labels, self.genres = db.get_split(split)
		return self.ids, self.labels, self.genres

	def get_split(self, split='train'):

		print('loading {} ... '.format(split))

		self.ids, self.labels, self.genres = self.get_split_ids(split)
		self.features, self.lenghts = __align_split_features__(self.ids, self.feature_dict)

		return self.features, self.lenghts, self.labels, self.ids

	def get_data_by_trailer_ids(self, trailer_ids):
		return db.get_data_by_trailer_ids(trailer_ids)

	def binary_label_to_genre(self, binary_labels):

		binary_labels = np.asarray(binary_labels)
		get_gen_fn = lambda x: sorted([self.genres[x == 1]])

		if len(binary_labels.shape) == 2:
			genres = [list(get_gen_fn(x)) for x in binary_labels]
		else:
			genres = get_gen_fn(binary_labels)

		return genres

	def __fetch_splits__(self, ):
		self.train_ids, self.train_labels, self.genres = db.get_split('train')
		self.valid_ids, self.valid_labels, _ = db.get_split('valid')
		self.test_ids,  self.test_labels,  _ = db.get_split('test')

def __pad_sequence__(seq, maxlen):

	left = 0 if maxlen < len(seq) else (maxlen - len(seq))
	left_pad = np.zeros((left, len(seq[-1])))

	pad_feat = np.concatenate([seq, left_pad], 0)[:maxlen]
	return pad_feat

def __align_split_features__(ids, feature_dict, maxlen=240):
		feats = []
		lens = []

		for id in ids:

			feat = np.asarray(feature_dict[id])

			if len(feat.shape) != 2:
				print(id)
				continue

			_pad_feat = __pad_sequence__(feat, maxlen=maxlen)

			feats.append(_pad_feat)
			lens.append(len(feature_dict[id]))

		return np.asarray(feats), np.asarray(lens)


import json
import re
from dataclasses import dataclass
from typing import Dict, Optional, List, Union
import copy
from utils.util import *

# Formats long-form descriptions into a single thing
def combine_list_desc(
    base_description: str, listed_descriptions: Union[None, str, List[str]]
):
    long_desc = ""
    if mandate_list(listed_descriptions):
        li = []
        for i in listed_descriptions:
            body = i.get('description', "ERROR - UNPARSED DATA CONSULT PDF")
            if "name" in i:
                body = f"{i['name']}: {body}"
            li.append(f"
{body}
")
        long_desc = f"

{''.join(li)}
"
    return base_description + long_desc

class Processor:
    def __init__(self) -> None:
        # Tracks our top level data
        self.raw_traits = {}
        self.raw_units = {}
        self.created_summons = {}

    def ingest_all_raw(self) -> None:
        """
        Ingest all raw data to our raw_traits and raw_units properties
        """
        for path in walk_files(foe_root):
            if "traits.json" in path:
                with open(path) as f:
                    for rt in json.load(f):
                        rt = recursive_downcase(rt)
                        self.raw_traits[rt["name"]] = rt
            else:
                with open(path) as f:
                    loaded = json.load(f)
                    loaded = recursive_downcase(loaded)
                    self.raw_units[loaded["name"]] = loaded

    def inherit_into(self, target: Dict, source: Dict) -> Dict:
        """
        Merges source into target. Neither input is edited.
        Values from target are prioritized, but in the case of arrays will appear last
        """
        target = copy.deepcopy(target)
        source = copy.deepcopy(source)

        def _sub_inherit_list(key):
            target[key] = mandate_list(source.get(key)) + mandate_list(target.get(key))

        def _sub_inherit_attr(key):
            if key in source and key not in target:
                target[key] = source[key]

        # Combine traits, actions, conditional abilities
        _sub_inherit_list("traits")
        _sub_inherit_list("setup_traits")
        _sub_inherit_list("actions")
        _sub_inherit_list("conditional_abilities")

        # Inherit stats
        _sub_inherit_attr("faction")
        _sub_inherit_attr("class")
        _sub_inherit_attr("vitality")
        _sub_inherit_attr("speed")
        _sub_inherit_attr("defense")
        _sub_inherit_attr("fray_damage")
        _sub_inherit_attr("damage_die")

        # Finally handle recursion
        for super_source in mandate_list(source.get("inherits")):
            super_source = self.raw_units[super_source]
            target = self.inherit_into(target, super_source)

        return target

    # Lookup a trait or substitute a default
    def get_trait(self, trait):
        if trait in self.raw_traits:
            return copy.deepcopy(self.raw_traits[trait])
        else:
            return {"name": trait, "description": "No description."}

    def process(self):
        """
        Creates files for all intaken raw units
        """
        # Begin building
        for _name, data in self.raw_units.items():
            # Skip templates
            if data.get("type") == "Template":
                continue

            self.process_foe(data)

    def process_foe(self, data):
        """
        Processes an individual actor
        """
        ap = ActorProcessor(self, data)
        ap.process_as_foe()
        return ap

    def process_summon(self, data):
        """
        Processes an individual actor
        """
        key = data.get("name")
        if key in self.created_summons:
            return self.created_summons[key]
        ap = ActorProcessor(self, data)
        ap.process_as_summon()
        self.created_summons[key] = ap
        return ap

class ActorProcessor:
    """
    Stateful processor for ingesting actors
    """

    def __init__(self, processor: Processor, data: Dict) -> None:
        self.parent = processor
        self.data = data
        self.name = data.get("name", "Unknown")
        self.id = random_id()
        self.system = {}
        self.item_ids = []
        self.type = None

        # These accumulate and can be edited before actually being turned to items
        self.actions = []
        self.traits = []

    def raw_inherit(self):
        """
        Handle inheritance on our raw data
        """
        for super_source in mandate_list(self.data.get("inherits")):
            super_source = self.parent.raw_units[super_source]
            self.data = self.parent.inherit_into(self.data, super_source)

    def hydrate_traits(self):
        """
        Fills out the trait data on all traits on this
        """
        # Populate traits
        traits = [*set(mandate_list(self.data.get("traits")))]
        for t in traits:
            if isinstance(t, str):
                t = self.parent.get_trait(t)
            self.traits.append(t)

    def process_setup_info(self):
        """
        Processes our metadata for foe setup
        """
        # Add setup traits, post process to combine listed descriptions into descriptions
        setup = []
        for t in mandate_list(self.data.get("setup_traits")):
            base = f"
{t['name']}

" + t.get("description", "")
            full = combine_list_desc(base, t.get("listed_items"))
            setup.append(f"
{full}
")
        self.system["setup"] = "\n\n".join(setup)

    def process_conditional_abilities(self):
        for cond_ability in mandate_list(self.data.get("conditional_abilities")):
            # Check special classes, ignore those that don't apply
            add_reqs = []
            if cond_ability.get("is_special_classes"):
                add_reqs.append(f"Must be a {cond_ability['is_special_classes']}")
            if cond_ability.get("is_not_special_classes"):
                add_reqs.append(
                    f"Must not be a {cond_ability['is_not_special_classes']}"
                )
            if cond_ability.get("uses_special_template"):
                add_reqs.append(f"Must be a {cond_ability['uses_special_template']}")

            # If remov traits specified, don't actually remove, but instead mark as explicitly chaptered
            """
            for removal_target in mandate_list(cond_ability.get("remove_traits")):
                # Find the trait with the same name
                corr_trait = ([x for x in self.traits if x["name"] == removal_target])[0]
                # Mark it
                if "chapter" in cond_ability:
                    corr_trait["remove_at_chapter"] = cond_ability["chapter"]

            # Same for actions
            for removal_target in mandate_list(cond_ability.get("remove_actions")):
                # Find the trait with the same name
                corr_action = [x for x in self.actions if x["name"] == removal_target][0]
                # Mark it
                if "chapter" in cond_ability:
                    corr_action["remove_at_chapter"] = cond_ability["chapter"]
            """

            # Add traits, mark similarly
            for add_trait in mandate_list(cond_ability.get("traits")):
                if isinstance(add_trait, str):
                    add_trait = self.parent.get_trait(add_trait)
                if "chapter" in cond_ability:
                    chpt = cond_ability["chapter"]
                    add_trait["chapter"] = chpt
                add_trait["special_requirements"] = add_reqs
                self.traits.append(add_trait)

            # Add actions, mark similarly
            for add_action in mandate_list(cond_ability.get("actions")):
                if "chapter" in cond_ability:
                    chpt = cond_ability["chapter"]
                    add_action["chapter"] = chpt
                add_action["special_requirements"] = add_reqs
                self.actions.append(add_action)

    def process_as_summon(self):
        self.type = "summon"
        self.system["is_object"] = self.data.get("is_object", False)
        self.system["tags"] = self.data.get("tags", [])
        self.system["summon_effects"] = self.data.get("summon_effects", "")
        self.system["summon_actions"] = self.data.get("summon_actions", "")
        self.finalize()

    def process_as_foe(self):
        self.type = "foe"
        self.raw_inherit()
        self.hydrate_traits()

        # Do initial copying
        self.system["chapter"] = self.data.get("chapter", 1)
        self.system["faction"] = self.data.get("faction", "Unknown")
        foe_class = {}
        self.system["class"] = foe_class
        foe_class["color"] = self.data.get("class", "Special")
        foe_class["vitality"] = self.data.get("vitality", 4)
        foe_class["speed"] = self.data.get("speed", 4)
        foe_class["defense"] = self.data.get("defense", 4)
        foe_class["fray_damage"] = self.data.get("fray_damage", 1)
        foe_class["damage_die"] = self.data.get("damage_die", 6)
        self.actions.extend(mandate_list(self.data.get("actions")))

        foe_class["armor"] = 0
        if "Guard" in [t["name"] for t in self.traits]:
            foe_class["armor"] = 2

        self.process_setup_info()
        self.process_conditional_abilities()

        # Finally, convert our actions & traits
        for action in self.actions:
            self.process_action(action)

        for trait in self.traits:
            self.process_trait(trait)

        self.finalize()

    def process_action(self, data):
        """
        Uses an ItemProcessor to process the given item as an action
        """
        ip = ItemProcessor(self, data)
        ip.process_as_ability()

    def process_trait(self, data):
        """
        Uses an ItemProcessor to process the given item as an trait
        """
        ip = ItemProcessor(self, data)
        ip.process_as_trait()

    def finalize(self):
        """
        Create the actual document model and write it to a file
        """
        actor = {
            "name": self.name,
            "type": self.type,
            "_id": self.id,
            "system": self.system,
            "img": "icons/svg/mystery-man.svg",
            "items": self.item_ids,
            "effects": [],
            "folder": None,
            "sort": 0,
            "ownership": {"default": 0},
            "flags": {},
            "_stats": {
                "systemId": "icon",
                "systemVersion": "0.0.0",
                "coreVersion": "11.306",
                "createdTime": 1690238921300,
                "modifiedTime": 1690238921300,
                "lastModifiedBy": "pTpVgni8yGIuH7km",
            },
            "_key": f"!actors!{self.id}",
        }

        # Dump to appropriate file
        filename = f"{self.name}_{self.id[:3]}.json".replace(" ", "_")
        with open(pack_root / "foes" / "_source" / filename, "w") as f:
            f.write(json.dumps(actor, indent=4))

class ItemProcessor:
    """
    Stateful process for ingesting items
    """

    def __init__(self, actor: ActorProcessor, data: Dict) -> None:
        self.parent = actor
        self.data = data
        self.name = data.get("name", "Unknown Action")
        self.id = random_id()
        self.type = None
        self.system = {}

    def action_to_choices(self, action_data):
        # Convert an action to a choice (or, choices)
        choices = []
        all_tags = action_data.get("tags", [])
        ranges = []
        other_tags = []
        for tag in all_tags:
            if any(sym in tag for sym in ["range", "blast", "line", "arc", "burst"]):
                ranges.append(tag)
            else:
                other_tags.append(tag)

        # Siphon effects. Do it via entries rather than via keys so we preserve order
        effects = []
        for k, v in action_data.items():
            if k == "hit":
                effects.append(f"Hit: {v}")
            if k == "auto_hit":
                effects.append(f"Auto Hit: {v}")
            if k == "miss":
                effects.append(f"Miss: {v}")
            if k == "area_effect":
                effects.append(f"Area: {v}")
            if k == "effects":
                sub = mandate_list(v)
                effects.extend([f"Effect: {e}" for e in sub])
            if k == "description":
                effects.append(f"Effect: {v}")
            if k == "exceed":
                effects.append(f"Exceed: {v}")
            if k == "charge":
                effects.append(f"Charge: {v}")

        # Subprocess interrupts
        for interrupt in mandate_list(action_data.get("interrupts")):
            if interrupt.get("name", action_data["name"]) == action_data["name"]:
                interrupt["name"] += f"{action_data['name']} (interrupt)"  # Add a unique suffix if necessary
            choices = choices + self.action_to_choices(interrupt)

        # Subprocess combos
        if action_data.get("combo"):
            combo = action_data["combo"]
            combo["name"] += f"[COMBO]"  # Add a unique prefix
            combo["chapter"] = action_data.get("chapter", 1)  # Pass down chapter
            choices = choices + self.action_to_choices(action_data["combo"])

        # Subprocess summons
        summons = []
        for summon in mandate_list(action_data.get("summons")):
            summon["name"] += " " + CHPT[action_data.get("chapter", 1)]
            summon = self.parent.parent.process_summon(summon)
            uuid = f"Compendium.icon.foes.Actor.{summon.id}"
            summons.append(uuid)

        # Build the primary choice
        primary_choice = {
            "name": action_data.get("name"),
            "actions": action_data.get("action_cost", 0),
            "round_action": action_data.get("round_action", False),
            "tags": other_tags,
            "ranges": ranges,
            "trigger": action_data.get("trigger", ""),
            "combo": 0,
            "resolve": 0,
            "effects": effects,
            "summons": summons
        }
        return [primary_choice] + choices

    def process_as_ability(self):
        # Process our main data as an action
        self.type = "ability"

        self.system["choices"] = self.action_to_choices(self.data)
        self.system["chapter"] = self.data.get("chapter", 1)
        self.system["special_requirements"] = (
            self.data.get("special_requirements", []),
        )
        self.system["trait"] = False
        self.finalize()

    def process_as_trait(self):
        # Traits follow the same format as actions, actually
        self.type = "ability"
        choices = []
        if self.data.get("description"):
            description = combine_list_desc(self.data.get("description", "Unknown"), self.data.get("listed_items"))
            choices.append({
                "actions": None,
                "round_action": False,
                "tags": [],
                "ranges": [],
                "trigger": self.data.get("trigger", ""),
                "combo": 0,
                "resolve": 0,
                "effects": [description],
            })
        for action in mandate_list(self.data.get("actions")):
            choices += self.action_to_choices(action)

        # Give all of them the Trait tag
        for choice in choices:
            choice["tags"] = ["Trait"] + choice["tags"]

        self.system["choices"] = choices
        self.system["chapter"] = self.data.get("chapter", 1)
        self.system["special_requirements"] = (
            self.data.get("special_requirements", []),
        )
        self.system["trait"] = True
        self.finalize()

    def finalize(self):
        """
        Create the actual document model and write it to a file
        """
        item_sort = len(self.parent.item_ids) + 1
        self.parent.item_ids.append(self.id)
        item = {
            "name": self.name,
            "type": self.type,
            "_id": self.id,
            "system": self.system,
            "img": "icons/svg/combat.svg",
            "sort": item_sort,
            "ownership": {},
            "effects": [],
            "flags": {},
            "_key": f"!actors.items!{self.parent.id}.{self.id}",
        }
        item_filename = f"{self.parent.name}_{self.name}_{self.id[:3]}.json".replace(" ", "_")
        with open(pack_root / "foes" / "_source" / item_filename, "w") as f:
            f.write(json.dumps(item, indent=4))

def main():
    # Post process to combine conditional abilities
    p = Processor()
    p.ingest_all_raw()
    p.process()

    # Convert actions into appropriate items

main()


class CommentValidator:

    def validate_comment(self, data):
        if 'username' not in data.keys():
            response = {
                'message': 'The username field must be completed',
                'status': '246'
            }
            return response

        if 'post_uuid' not in data.keys():
            response = {
                'message': 'The post_uuid field must be completed',
                'status': '247'
            }
            return response

        if 'comment' not in data.keys():
            response = {
                'message': 'The comment field must be completed',
                'status': '247'
            }
            return response

        if len(data['comment']) < 1:
            response = {
                'message': 'The comment field must have at least 1 character',
                'status': '247'
            }
            return response

        return None


# 관심영역 지정을 위함 함수가 기본으로 제공이 된다.
# roi_crop_mouse.py 파일과 같이 직접 만들 필요없이 간단히 작성 가능.

import cv2
import numpy as np

img = cv2.imread("./img/sunset.jpg")

x,y,w,h = cv2.selectROI("img", img, False)
if w and h:
    roi = img[y:y+h, x:x+w]
    cv2.imshow("cropped", roi)          # ROI 지정 영역을 새창으로 표시
    cv2.moveWindow("cropped", 0,0)      # 새 창을 화면 좌측 상단으로 이동
    cv2.imwrite("./cropped2.jpg", roi)   # ROI 영역만 파일로 저장

cv2.imshow("img", img)
cv2.waitKey(0)
cv2.destroyAllWindows()


"""2016-11-05 01:04:44.434429 add invoice_id, refund_invoice_id to deliverable

Revision ID: 9d43294b905c
Revises: 66c9c7cb922e
Create Date: 2016-11-05 01:04:44.434429

"""

# revision identifiers, used by Alembic.
revision = '9d43294b905c'
down_revision = '66c9c7cb922e'

from alembic import op
import sqlalchemy as sa

def upgrade():
    ### commands auto generated by Alembic - please adjust! ###
    op.add_column('deliverable', sa.Column('invoice_id', sa.Integer(), nullable=True))
    op.add_column('deliverable', sa.Column('refund_invoice_id', sa.Integer(), nullable=True))
    op.create_foreign_key(None, 'deliverable', 'invoice', ['refund_invoice_id'], ['id'])
    op.create_foreign_key(None, 'deliverable', 'invoice', ['invoice_id'], ['id'])
    ### end Alembic commands ###

def downgrade():
    ### commands auto generated by Alembic - please adjust! ###
    op.drop_constraint(None, 'deliverable', type_='foreignkey')
    op.drop_constraint(None, 'deliverable', type_='foreignkey')
    op.drop_column('deliverable', 'refund_invoice_id')
    op.drop_column('deliverable', 'invoice_id')
    ### end Alembic commands ###


# pylint: disable=invalid-name
"""Sphinx configuration."""
from datetime import datetime

project = u"Accretion"

# Add any Sphinx extension module names here, as strings. They can be extensions
# coming with Sphinx (named 'sphinx.ext.*') or your custom ones.
extensions = [
    "sphinx.ext.autodoc",
    "sphinx.ext.doctest",
    "sphinx.ext.intersphinx",
    "sphinx.ext.todo",
    "sphinx.ext.coverage",
    "sphinx.ext.autosummary",
    "sphinx.ext.napoleon",
    "sphinx.ext.viewcode",
    "sphinx_autodoc_typehints",
    "sphinxcontrib.spelling",
]
napoleon_include_special_with_doc = False

# Add any paths that contain templates here, relative to this directory.
templates_path = ["_templates"]

source_suffix = ".rst"  # The suffix of source filenames.
master_doc = "index"  # The master toctree document.

copyright = u"%s, Amazon" % datetime.now().year  # pylint: disable=redefined-builtin

# List of directories, relative to source directory, that shouldn't be searched
# for source files.
exclude_trees = ["_build"]

pygments_style = "sphinx"

autoclass_content = "both"
autodoc_default_flags = ["show-inheritance", "members"]
autodoc_member_order = "bysource"

html_theme = "sphinx_rtd_theme"
html_static_path = ["_static"]
htmlhelp_basename = "%sdoc" % project

# Example configuration for intersphinx: refer to the Python standard library.
intersphinx_mapping = {"python": ("http://docs.python.org/", None)}

# autosummary
autosummary_generate = True

# Spellchecker
spelling_word_list_filename = "spelling_wordlist.txt"
spelling_lang = "en_US"
spelling_ignore_python_builtins = True
spelling_ignore_importable_modules = True


def suma():
    valor1 = 10
    valor2 =20
    resultado = valor1 + valor2
    print("El valor es: ",resultado)
    print(f"El valor es: {resultado}")

def imprimir():
    nombre = "Nicolas"
    print("El nombre es: ",nombre)
    print(f"El nombre es: {nombre}")

def resta():
    val1 = 66;
    val2= 10;
    resultadoResta = val1-val2;
    print("El valor de la resta es: ",resultadoResta)

def producto():
    val3=100*6;
    print("El valor del producto es: ",val3)

suma()
imprimir()
resta()
producto()


from PyQt5.QtCore import pyqtSlot, QRunnable
import pyqtgraph
import traceback
import sys

from OPTIMAQS.utils.signals import Signals

class Worker(QRunnable):
    '''
    From https://www.learnpyqt.com/courses/concurrent-execution/multithreading-pyqt-applications-qthreadpool/
    Inherits from QRunnable to handler worker thread setup, signals and wrap-up.
    :param callback: The function callback to run on this worker thread. Supplied args and
                     kwargs will be passed through to the runner.
    :type callback: function
    :param args: Arguments to pass to the callback function
    :param kwargs: Keywords to pass to the callback function

    '''
    def __init__(self, fn, *args, **kwargs):
        super(Worker, self).__init__()
        # Store constructor arguments (re-used for processing)
        self.fn = fn
        self.args = args
        self.kwargs = kwargs
        self.signals = Signals()

        # Add the callback to our kwargs
        #self.kwargs['progress_callback'] = self.signals.progress

    @pyqtSlot()
    def run(self):
        '''
        Initialise the runner function with passed args, kwargs.
        '''
        # Retrieve args/kwargs here; and fire processing using them
        try:
            result = self.fn(*self.args, **self.kwargs)
        except:
            traceback.print_exc()
            exctype, value = sys.exc_info()[:2]
            self.signals.error.emit((exctype, value, traceback.format_exc()))
        else:
            self.signals.result.emit(result)  # Return the result of the processing
        finally:
            self.signals.finished.emit()  # Done

    def progress_fn(self, n):
        print("%d%% done" % n)

    def print_output(self, s):
        print(s)

    def thread_complete(self):
        print("Thread Completed!")

    def print_test(self):
        print('testing_signals')


class Point2D:
    def __init__(self, x, y) -> None:
        self.x = x
        self.y = y

class Point3D:
    def __init__(self, x, y, z) -> None:
        self.x = x
        self.y = y
        self.z = z

class Vector2D:
    def __init__(self, x, y) -> None:
        self.x = x
        self.y = y

class Vector3D:
    def __init__(self, x, y, z) -> None:
        self.x = x
        self.y = y
        self.z = z

def test():
    points = [
        Point2D(0, 1),
        Point2D(0, 1),
        Point3D(0, 1, 2),
        Point3D(0, 1, 3),
        Vector2D(0, 1),
        Vector3D(0, 1, 4),
    ]

    z_objects = []
    # 过滤出含有'z'属性的对象，在过滤中打印出对应对象的'z'属性
    for p in points:
        if hasattr(p, 'z'):
            z = getattr(p, 'z')
            print('get z attr:', z)
            z_objects.append(p)

    for p in z_objects:
        print('x+y+z:', p.x+p.y+p.z)

if __name__ == '__main__':
    test()


# -*- coding: utf-8 -*-
"""
Created on Sun Dec 29 23:59:09 2019

@author: Dan J Hamilton
"""

# %% Program Class

class Program:
    """
    This class retrieves a program's information from the available
    programs dictionary.

    class Program
        Properties
            code
            description
            name

        Methods
            read_binary
    """

    def __init__(self, program):
        self.code = self.read_binary(program['binary'])
        self.description = program['description']
        self.name = program['name']

    def read_binary(self, program_binary):

        with open(program_binary, 'r', encoding='utf-8') as file:
            contents = file.read().split(',')

        contents = [int(_) for i, _ in enumerate(contents)]

        return contents


import os
from enum import Enum
from pathlib import Path
import matplotlib.pyplot as plt

# import matplotlib.pyplot as plt
import numpy as np
import scipy.fftpack
from scipy import signal
import pyedflib
import random

class States:
    rest, r_fist, l_fist, r_fist_im, l_fist_im, fists, fists_im, feet, feet_im = range(9)

class Tasks:
    T0, T1, T2 = range(3)

#structure to store signal information
class EEG_Sample:
	def __init__(self, signal, label, patient_num, record_num, sample_num):
		self.signal = signal
		self.label = label
		self.patient_num = patient_num
		self.record_num = record_num
		self.sample_num = sample_num

	def print_sample_info(self):
		print("signal shape: ", self.signal.shape, " ;label: ", self.label, " ;patient: ", self.patient_num, " ;record: ", self.record_num, " ;sample: ", self.sample_num)

#maps the record number and task type (T0, T1, T2) to the State
state_table = np.array([[States.rest,States.rest,States.rest],   		#1
						[States.rest,States.rest,States.rest],   		#2
						[States.rest,States.l_fist, States.r_fist],		#3
						[States.rest,States.l_fist_im,States.r_fist_im],#4
						[States.rest,States.fists,States.feet],    		#5
						[States.rest,States.fists_im,States.feet_im],   #6
						[States.rest,States.l_fist, States.r_fist],		#7
						[States.rest,States.l_fist_im,States.r_fist_im],#8
						[States.rest,States.fists,States.feet], 		#9
						[States.rest,States.fists_im,States.feet_im], 	#10
						[States.rest,States.l_fist, States.r_fist],		#11
						[States.rest,States.l_fist_im,States.r_fist_im],#12
						[States.rest,States.fists,States.feet], 		#13
						[States.rest,States.fists_im,States.feet_im]]	#14
						)

#returns array of EEG_Samples
def get_eeg_samples(db_dir_name, num_patients=109, num_records=14, channels=[9,11,13], sig_len=640, num_segs=2):
	# rootdir = os.path.join(str(Path.home()), 'SeniorProject/EEG_Dataset/')
	rootdir = os.path.join(str(Path.home()), db_dir_name)

	#the completed eeg data set to be sent to Tensorflow
	eeg_samples = []
	records1 = {'03', '07', '11'}
	records2 = {'04', '08', '12'}	#these are the records than contain the left/right fist data

	#iterate through every volunteer's directory (S001, S002,..., S109)
	for patient in range(1, num_patients  + 1):
		direc = rootdir + 'S%03d' % patient
		st = "VOLUNTEER #" + '%03d' % patient
		print (st)
		sample_count = 0
		#iterate through the EDF num_records in the directory (S001R01.edf, S001R02.edf,..., S001R14.edf)
		for record_num in records2:
			#create new EdfReader for the EDF record file
			edf_reader = create_edf_reader(direc, patient, record_num)

			#get the list of [starting values, states] for each task-sample
			record_data = parse_record_ann(direc, record_num)

			#iterate through the 4 second task-samples
			for sample_num in range(len(record_data)):
				start_sample = int(record_data[sample_num][0])
				sample_sig = read_eeg_signal(edf_reader, channels, start_sample, sig_len)
				segments = segment_signal(sample_sig, num_segs, sig_len)

				for segment in segments:
					# segment = segment.flatten()
					segment_data = EEG_Sample(segment, int(record_data[sample_num][1]) - 3, patient, record_num, sample_num)
					eeg_samples.append(segment_data)
					# segment_data.print_sample_info()
					sample_count += 1
		print("   sample count: ", sample_count)

	r = 0
	l = 0
	for sample in eeg_samples:
		if sample.label == 0:
			r += 1
		if sample.label == 1:
			l += 1

	print("right fist: ", r, ", left fist: ", l)

	return eeg_samples

def train_test_split_shuffle(eeg_samples, total_samples_used, train_test_percent=0.7):

	#shuffle all samples
	random.shuffle(eeg_samples)

	#shorten the list of samples
	eeg_samples = np.asarray(eeg_samples[:total_samples_used])
	# eeg_data = np.asarray([x.signal for x in eeg_samples])
	# eeg_labels = np.asarray([x.label - 1 for x in eeg_samples])

	# #split the list
	# train_test_split = np.random.rand(len(eeg_samples)) < train_test_percent
	# train_data = eeg_data[train_test_split]
	# train_labels = eeg_labels[train_test_split]
	# test_data = eeg_data[~train_test_split]
	# test_labels = eeg_labels[~train_test_split]

	train_test_split = np.random.rand(len(eeg_samples)) < train_test_percent
	train_samples = eeg_samples[train_test_split]
	test_samples = eeg_samples[~train_test_split]

	# return train_data, train_labels, test_data, test_labels
	return train_samples, test_samples

def train_test_split_patients(eeg_samples, patient_div):
	train_data = []
	train_labels = []
	test_data = []
	test_labels = []

	for sample in eeg_samples:
		if sample.patient_num <= patient_div:
			train_data.append(sample.signal)
			train_labels.append(sample.label)
		else:
			test_data.append(sample.signal)
			test_labels.append(sample.label)

	train_data, train_labels = randomize_data(train_data, train_labels)
	test_data, test_labels = randomize_data(test_data, test_labels)
	return np.asarray(train_data), np.asarray(train_labels), np.asarray(test_data), np.asarray(test_labels)

def randomize_data(data, labels):
	rng_state = np.random.get_state()
	np.random.shuffle(data)
	np.random.set_state(rng_state)
	np.random.shuffle(labels)
	return data, labels

def static_randomize_data(data, labels):
	np.random.seed(69)
	rlist = np.random.permutation(len(data))
	new_data = []
	new_labels = []

	for idx in rlist:
		new_data.append(data[idx])
		new_labels.append(labels[idx])

	return np.asarray(new_data), np.asarray(new_labels)

#create an pyedflib.EdfReader for the given record number
def create_edf_reader(direc, patient_num, record_num):
	edf_fname = 'S' + '%03d' % patient_num + 'R' + record_num + '.edf'
	edf_fname = os.path.join(direc, edf_fname)

	return pyedflib.EdfReader(edf_fname)

# parses through the ann.txt file for the given record number
# returns array of entries: [starting_sample_number, state_type]
def parse_record_ann(direc, record_num):
	record_data = []
	ann_fname = direc + '/ann' + record_num + '.txt'

	#create array of starting samples for each task(T0, T1, T2) in the ann-file (i.e sample# 0, 672, 1328,...)
	#create array of labels for each task in the ann-file (T0, T1, T2)
	with open(ann_fname) as fobj:
		for line in fobj:
			row = line.split()
			start_sample = row[1]
			state_type = state_table[int(record_num) - 1][int(row[2][1])]
			record_data.append([start_sample, state_type])

	# for i in range(len(record_data)):
	# 	if record_data[i][1] != States.l_fist and record_data[i][1] == States.r_fist:

	for sample_data in record_data:
		if sample_data[1] != States.l_fist_im and sample_data[1] != States.r_fist_im:
			record_data.remove(sample_data)

	return record_data

#returns array of signal values from EDF file
def read_eeg_signal(edf_reader, channels, start_sample, sig_len):

	#create empty numpy array of shape: [channel_num, signal_length]
	signal_arr = np.zeros((len(channels), sig_len))

	#iterate through all channels
	for l in range(len(channels)):
		signal_arr[l] = edf_reader.readSignal(int(channels[l]),start_sample, sig_len)

	return signal_arr

# divide input signal into equal length segments
# returns array of shape: [total_segments]
def segment_signal(sig, num_segs, sig_len):
	segment_len = int(sig_len / num_segs)
	segments = []

	for s in range(num_segs):
		start = s * segment_len
		end = start + segment_len
		segment = sig[:,start:end]
		segments.append(segment)

	segments = np.array(segments)
	# print("Signal Shape: ", sig.shape)
	# print("Segments Shape: ", segments.shape)
	return segments


class Solution:
    def minEatingSpeed(self, piles: List[int], h: int) -> int:
        l=1
        r=max(piles)
        eatingspeed=max(piles)
        while l<=r:
            mid=(l+r)//2
            f=0
            for p in piles:
                f+=math.ceil(p/mid)
            if f>h:
                l=mid+1
            else:
                eatingspeed=min(eatingspeed,mid)
                r=mid-1

        return eatingspeed


import collections

def score(dice):
    """
    Three 1's => 1000 points
    Three 6's =>  600 points
    Three 5's =>  500 points
    Three 4's =>  400 points
    Three 3's =>  300 points
    Three 2's =>  200 points
    One   1   =>  100 points
    One   5   =>   50 point
    """
    values = [[], [[3, 1000], [1, 100]], [[3, 200]], [[3, 300]], [[3, 400]], [[3, 500], [1, 50]], [[3, 600]]]
    c = collections.Counter(dice)
    res = 0
    for i in range(1, 7):
        if i in c:
            k = c[i]
            for n, v in values[i]:
                res += (k // n) * v
                k %= n
    return res

dice = [2, 4, 4, 5, 4]
print(score(dice))


#conditional if/else - according a condition execute or not code

#Conditionals

#struct of conditional: take two values true or false according to case
#Note: Boolean data - can take two values TRUE or FALSE

#if condition is correct: True
#   then do this
#
#otherwise: False
#   do this

#Python comparison Operator
#   1. Equals - a == b
#   2. Not equals: a != b
#   3. less than: a < b
#   4. less than or equal to: a <= b
#   5. greater than: a > b
#   6. greater than or equal to: a >= b

#Python arithmetic operator

#   1. Addition x + y
#   2. Subtraction x - y
#   3. Multiplication x*y
#   4. Division x/y
#   5. Modulus x%y
#   6. Exponentiation x**y
#   7. Floor division x//y

#create a function for know if a number is greater than 10
#recommend create function after call this

def greater(a):
    #convert string to number
    A = float(a) #float() convert string to float number
    print(A > 10) #return a value true or false
    print(type(A)) #type() - return data type
    if A > 10:
        return f"Number {A} is greater than 10"
    else:
        return f"Number {A} isn't greater than 10"

def give_numbers():
    number_1 = input(f"Number 1: ")
    Greater_10 = greater(number_1)
    print(Greater_10) #Greater_10 is variable local - scope

give_numbers()

#create a function for calculate the floor division between numbers
print("\n")

Number_3 = int(input("First number: ")) #other way convert string to number
Number_4 = int(input("Second number: "))

def floor_division(a, b):
    if b != 0:
        return a//b
    elif b == 0: #else if as c++ or javascript
        # = assigning a value to a variable
        # == check for equality
        return "Can't floor_division"

Floor_value = floor_division(Number_3, Number_4)
print(f"Result:  {Floor_value}")


def solution(priorities, location):
    answer = 0

    prints = [0 for _ in range(len(priorities))]
    prints[location] = 1

    while True:
        while priorities[0] != max(priorities):
            priorities.append(priorities[0])
            del priorities[0]

            prints.append(prints[0])
            del prints[0]

        answer += 1

        if prints[0] == 1:
            break
        del priorities[0]
        del prints[0]

    return answer

프로그래머스/프린터.py


import json
import random
import shutil

from os.path import exists
from typing import Generic, NamedTuple, TypeVar

Q = TypeVar('Q')

class QuestionList(Generic[Q]):
    """
    QuestionList represent a QOTD Bot's question storehouse.
    It takes requests from the Q Bot and passes back results, as needed.
    It also stores previously asked question and questions to be asked.
    """
    class Question(NamedTuple):
        ask_url: str    # URL of where the question was last asked - IMPLEMENT
        author: str     # Name of the author
        url: str        # URL of where the author's message - IMPLEMENT
        text: str       # URL of the question's text

    def __init__(self, name: str):
        """
        Creating QuestionList object.
        name - name of the QuestionList instance and
        the associated json file name.
        """
        self.name = name
        self.questions = dict()
        self.current_id = 0

        file_name = name + ".json"
        if (exists(file_name)):
            with open(file_name, 'r') as file:
                self.questions = json.load(file)

        if self.questions:
            self.current_id = list(self.questions)[-1]

    def make_question(self, author: str, text: str) -> Q:
        """
        Creates a question object.
        author - Represents the author of the question
        proposal id - Represents the original post
        """
        return self.Question(author=author, text=text, url=None, ask_url=None)

    def add(self, question: Question):
        """
        Adds a question to the QuestionList.
        question - An object containing the question.  Use make_question() to create this.
        """
        self.current_id = self.current_id + 1
        self.questions.update({self.current_id: question})

    def get(self, is_random=False) -> Q:
        """
        Returns a question. If no question is available, returns None.
        is_random (False default) - If true, asks random question from list.
        If false, asks the next one in the list.
        """
        question = None

        if self.questions:
            if is_random:
                key = random.choice(list(self.questions))
                question = self.questions.pop(key)
            else:
                question = list(self.questions)[0]

        return question

    def list(self) -> str:
        """
        Returns a string formatted list of the questions in this QuestionList.
        """
        formatted_list = f""
        for key in self.questions:
            question = self.questions[key]
            formatted_list += f"{question.author} wrote: " + \
            f"\"{question.text}\" at {question.url}\n" + \
            f"It was last asked at: {question.ask_url}\n"

        return formatted_list

    def move(self, question_id: int, other: Q):
        """
        Moves a question to another QuestionList
        question_id - ID number of question to be moved
        other - Other QuestionList to be moved to.
        """
        this_question = self.remove(question_id)
        other.add(this_question)

    def remove(self, question_id: int):
        """
        Removes a question from the QuestionList.
        question_id - ID number of question to be removed
        """
        if question_id not in self.questions:
            return ValueError(f"ID number not in {self.name}")

        return self.questions.pop(question_id)

    def save_list(self):
        """
        Saves a copy of the QuestionList to a file.
        Note, backs up one copy of the file as '<file_name>.bak'
        if one with the same name already exists.
        """
        file_name = self.name + ".json"
        backup_name = file_name + ".bak"
        if exists(file_name):
            shutil.move(file_name, backup_name)

        with open(file_name, 'w') as file:
            json.dump(self.questions, file)


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

students = [
    ('Alexey', '252504', 'KSIS'),
    ('Sergey', '455403', 'KSIS'),
    ('Maria', '455403', 'FITU'),
]

def init_db():
    conn = sqlite3.connect('students.db')
    cur = conn.cursor()

    try:
        cur.execute("""
            create table students(
                name varchar,
                group_number varchar,
                faculty varchar
            )
        """)
    except sqlite3.Error:
        pass

    for student in students:
        cur.execute("""
            insert into students(name, group_number, faculty) values (?, ?, ?)
        """, student)
        conn.commit()

if __name__ == '__main__':
    init_db()


import time
import numpy as np

WARN_MIN_TRIPLETS = 100  # Recommended number or triplets for an optimum result

def timeit(method):
    def timed(*args, **kw):
        ts = time.time()
        result = method(*args, **kw)
        te = time.time()
        if 'log_time' in kw:
            name = kw.get('log_name', method.__name__.upper())
            kw['log_time'][name] = int((te - ts) * 1000)
        else:
            print(f'Processed {method.__name__} in {(te - ts):5.2f} s')
        return result

    return timed

@timeit
def etc(q_hat):
    """
    Extended Triple Collocation (ETC).

    ETC estimates the variance of the noise error (RMSE_u)
    and correlation coefficients (r) of three measurement systems
    (e.g., satellite, in-situ and model-based products)
    with respect to the unknown true value of the variable being measured
    (e.g., turbulent fluxes, soil moisture, ...).

    Input variables from which covariance matrix is computed do not need to be
    re-scaled.

    Parameters
    ----------
    q_hat : 2D or 3D array
        Covariance matrix of shape (3, 3) or (3, 3, f) of the three
        unscaled spatially-collocated measurement systems.
        f is the number of different collocated points.

    Returns
    -------
    stderr : array
        Array of shape (3,) or (3, f) with standard errors for each of the
        measurement systems x, y and z.
    rho : array
        array of 3 correlation coefficients for each of the measurement systems
        (x, y and z) with respect to the unknown truth.
        It is also named sensitivity of the measurement system to the target variable.

    References
    ----------
    .. [McColl_2014] McColl, K.A., J. Vogelzang, A.G. Konings, D. Entekhabi,
        M. Piles, A. Stoffelen (2014).
        Extended Triple Collocation: Estimating errors and
        correlation coefficients with respect to an unknown target.
        Geophysical Research Letters 41:6229-6236
        https://doi.org/10.1002/2014GL061322

    .. [Gruber_2016] Gruber, A., Su, C.-H., Zwieback, S., Crow, W., Dorigo, W.,
        Wagner, W., 2016.
        Recent advances in (soil moisture) triple collocation analysis.
        International Journal of Applied Earth Observation and Geoinformation 45, 200–211.
        https://doi.org/10.1016/j.jag.2015.09.002

    """

    # System sensitivity to variations in the true signal
    # Eq. 7 of [Gruber_2016]_
    sensitivity = np.array([q_hat[0, 1] * q_hat[0, 2] / q_hat[1, 2],
                            q_hat[0, 1] * q_hat[1, 2] / q_hat[0, 2],
                            q_hat[0, 2] * q_hat[1, 2] / q_hat[0, 1]])

    # Eq. 5 of [McColl_2014]_
    errvar = np.asarray((q_hat[0, 0] - sensitivity[0],
                         q_hat[1, 1] - sensitivity[1],
                         q_hat[2, 2] - sensitivity[2]))

    no_valid = errvar < 0
    if np.any(no_valid):
        print("Warning: At least one calculated error variance is negative. "
                      "This can happen if the sample size is too small, "
                      "or if one of the assumptions of CTC is violated.")
    stderr = np.full_like(errvar, np.nan)
    stderr[~no_valid] = np.sqrt(errvar[~no_valid])

    # Eq. 9 of [McColl_2014]_
    rho = (np.sqrt(q_hat[0, 1] * q_hat[0, 2] / (q_hat[0, 0] * q_hat[1, 2])),
           np.sign(q_hat[0, 2] * q_hat[1, 2]) * np.sqrt(q_hat[0, 1]
                   * q_hat[1, 2] / (q_hat[1, 1] * q_hat[0, 2])),
           np.sign(q_hat[0, 1] * q_hat[1, 2]) * np.sqrt(q_hat[0, 2]
                   * q_hat[1, 2] / (q_hat[2, 2] * q_hat[0, 1]))
           )

    rho = np.asarray(rho)

    # Signal-to-Noise Ratio in decibels Eq. 14 of [Gruber_2016]_
    snr_db = [(q_hat[0, 0] * q_hat[1, 2] / (q_hat[0, 1] * q_hat[0, 2])) - 1,
              (q_hat[1, 1] * q_hat[0, 2] / (q_hat[1, 0] * q_hat[1, 2])) - 1,
              (q_hat[2, 2] * q_hat[0, 1] / (q_hat[2, 0] * q_hat[2, 1])) - 1]

    snr_db = np.asarray(snr_db)

    snr_db[snr_db < 0] = np.nan
    snr_db = -10 * np.log10(np.asarray(snr_db))

    return stderr, rho, snr_db, sensitivity

@timeit
def ctc(q_hat):
    """
    Correlated Triple Collocation (CTC).

    CTC estimates the variance of the noise error (RMSE_u)
    and correlation coefficient (r) between the noises in the two
    a priori correlated systems, with respect to the unknown true value of
    the variable being measured e.g., turbulent fluxes, soil moisture, ...).

    Input variables used in the covariance matrix should have been rescaled,
    so that $\alpha_{1,2}=\alpha_{1,3}=1$.

    Parameters
    ----------
    q_hat : 2D or 3D array
        Covariance matrix of shape (3, 3) or (3, 3, f) of the three
        rescaled spatially-collocated measurement systems.
        f is the number of different collocated points.

    Returns
    -------
    stderr : array
        Array of shape (3,) or (3, f) with standard errors for each of the
        measurement systems x, y and z.
    rho_xy: float or array
        correlation coefficient between the errors of x and y.

    References
    ----------
    .. [GonzalezGambau_2020] González-Gambau, V., Turiel, A., González-Haro, C.,
        Martínez, J., Olmedo, E., Oliva, R., Martín-Neira, M., 2020.
        Triple Collocation Analysis for Two Error-Correlated Datasets:
        Application to L-Band Brightness Temperatures over Land.
        Remote Sensing 12, 3381.
        https://doi.org/10.3390/rs12203381

    """

    # Linear transformation of x and y into two new variables u and v
    # with errors that are uncorrelated Eq. 6 & 7 of [GonzalezGambau_2020]_
    s_1_prime = q_hat[0, 0] + q_hat[1, 1] - 2 * q_hat[0, 1]  # Eq.7 of [GonzalezGambau_2020]_
    u = (q_hat[1, 1] - q_hat[0, 1]) / s_1_prime  # Eq.6 of [GonzalezGambau_2020]_
    v = (q_hat[0, 0] - q_hat[0, 1]) / s_1_prime  # Eq.6 of [GonzalezGambau_2020]_
    # Order-2 moments of the uncorrelated-error variables u and v
    # Eq. 7 of [GonzalezGambau_2020]_
    s_2_prime = u**2 * q_hat[0, 0] + v**2 * q_hat[1, 1] + 2 * u * v * q_hat[0, 1]
    s_23_prime = u * q_hat[0, 1] + v * q_hat[1, 2]

    # Estimates for the error variances Eq. 8 of [GonzalezGambau_2020]_
    errvar = np.asarray([v**2 * s_1_prime + s_2_prime - s_23_prime,
                         u**2 * s_1_prime + s_2_prime - s_23_prime,
                         q_hat[2, 2] - s_23_prime])

    no_valid = errvar < 0
    if np.any(no_valid):
        print("Warning: At least one calculated error variance is negative. "
                      "This can happen if the sample size is too small, "
                      "or if one of the assumptions of CTC is violated.")
    stderr = np.full_like(errvar, np.nan)
    stderr[~no_valid] = np.sqrt(errvar[~no_valid])

    # Covariance of errors between x and y Eq. 8 of [GonzalezGambau_2020]_
    cov_xy = -u * v * s_1_prime + s_2_prime - s_23_prime
    # Convert to Corrrelation of errors between x and y
    rho_xy = cov_xy / (stderr[0] * stderr[1])
    rho_xy = np.where(np.logical_and(rho_xy >= -1, rho_xy <= 1), rho_xy, np.nan)
    return stderr, rho_xy

@timeit
def lsetc(q_hat):
    """
    Least Squared Error Triple Collocation (CTC).

    LSETC estimates the variance of the noise error (RMSE_u)
    and correlation coefficient (r) between the noises in the two
    a priori correlated systems, with respect to the unknown true value of
    the variable being measured e.g., turbulent fluxes, soil moisture, ...).

    Input variables used in the covariance matrix should have been rescaled,
    so that $\alpha_{1,2}=\alpha_{1,3}=1$.

    Parameters
    ----------
    q_hat : 2D or 3D array
        Covariance matrix of shape (3, 3) or (3, 3, f) of the three
        rescaled spatially-collocated measurement systems.
        f is the number of different collocated points.

    Returns
    -------
    stderr : array
        Array of shape (3,) or (3, f) with standard errors for each of the
        measurement systems x, y and z.
    rho_xy: float or array
        correlation coefficient between the errors of x and y.

    References
    ----------
    .. [GonzalezGambau_2020] González-Gambau, V., Turiel, A., González-Haro, C.,
        Martínez, J., Olmedo, E., Oliva, R., Martín-Neira, M., 2020.
        Triple Collocation Analysis for Two Error-Correlated Datasets:
        Application to L-Band Brightness Temperatures over Land.
        Remote Sensing 12, 3381.
        https://doi.org/10.3390/rs12203381

    """
    # Estimates for the error variances Eq. A11 of [GonzalezGambau_2020]_
    truevar = 0.5 * (q_hat[0, 2] + q_hat[1, 2])
    errvar = np.array([q_hat[0, 0] - truevar,
                       q_hat[1, 1] - truevar,
                       q_hat[2, 2] - truevar])

    no_valid = errvar < 0
    if np.any(no_valid):
        print("Warning: At least one calculated error variance is negative. "
                      "This can happen if the sample size is too small, "
                      "or if one of the assumptions of CTC is violated.")
    stderr = np.full_like(errvar, np.nan)
    stderr[~no_valid] = np.sqrt(errvar[~no_valid])

    # Covariance of errors between x and y Eq. A11 of [GonzalezGambau_2020]_
    cov_xy = q_hat[0, 1] - truevar
    # Convert to Corrrelation of errors between x and y
    rho_xy = cov_xy / (stderr[0] * stderr[1])
    rho_xy = np.where(np.logical_and(rho_xy >= -1, rho_xy <= 1), rho_xy, np.nan)
    return stderr, rho_xy

@timeit
def scaling_factors(q_hat):
    """
    Compute scaling factors from a reference system based on triple collocation.

    Parameters
    ----------
    q_hat : 2D or 3D array
        Covariance matrix of shape (3, 3) or (3, 3, f) of the three
        rescaled spatially-collocated measurement systems. Rescaling factors will
        be computed with first system as reference.
        f is the number of different collocated points.

    Returns
    -------
    factors : array
        Array of shape (3,) or (3, f) with rescaling factors for each of the
        measurement systems x, y and z.
        Since z is the reference system, factors[2] = 1

    References
    ----------
    .. [Yilmaz_2014] Yilmaz, M.T., Crow, W.T., 2014.
        Evaluation of Assumptions in Soil Moisture Triple Collocation Analysis.
        Journal of Hydrometeorology 15, 1293–1302.
        https://doi.org/10.1175/JHM-D-13-0158.1

    """

    # Eqs. 4 & 5 of [Yilmaz_2014]_
    factors = np.array([np.ones_like(q_hat[1, 2]),
                        q_hat[0, 2] / q_hat[1, 2],
                        q_hat[0, 1] / q_hat[2, 1],
                        ])

    return factors

def covariance_matrix(x, y, z):
    """Compute the Triple Collocation covariance matrix

    Parameters
    ----------
    x, y, z : array_like
        Arrays of shape (N,) of spatially-collocated measurement systems.
        N is the sample size
    """

    # Remove of any existing NaN in any of the collocated systems
    valid = np.logical_and.reduce((np.isfinite(x),
                                   np.isfinite(y),
                                   np.isfinite(z)))

    n_valid = np.sum(valid)
    if n_valid < WARN_MIN_TRIPLETS:
        print(f"Warning: Sample size is too small (n={n_valid}, "
              "Consider increasing your sample size for robust TC metrics")

    if np.size(np.unique(x[valid])) <= 1 or np.size(np.unique(y[valid])) <= 1 \
            or np.size(np.unique(z[valid])) <= 1:
        print("ERROR: the sample variance of each of the variables x, y, z "
                      "must be non-zero. Increase your sample size or "
                      "reconsider using Triple Collocation Analysis")
        return None
    # Estimate covariance matrix of the three measurement systems
    q_hat = np.cov(np.stack((x[valid], y[valid], z[valid]), axis=0))

    return q_hat

@timeit
def covariance_matrix_vec(x, y, z):
    """Compute the vectorized Triple Collocation covariance matrix.

    Parameters
    ----------
    x, y, z : array_like
        Arrays of shape (N, f) elements of spatially-collocated measurement systems.
        N is the sample size, f is the number of collocated points

    Returns
    -------
    q_hat : 3D array
        Array of shape (3, 3, f) with the covariance matrix (3x3)
        for each collocated point.
    """

    # Check all triplets have either data or nan
    no_valid = np.logical_or.reduce((np.isnan(x), np.isnan(y), np.isnan(z)))
    x[no_valid] = np.nan
    y[no_valid] = np.nan
    z[no_valid] = np.nan

    q_hat = np.zeros((3, 3, x.shape[1]))
    n = np.isfinite(x)
    n_1 = np.sum(n.T, axis=1) - 1

    x_diff = x - np.nanmean(x, axis=0)
    y_diff = y - np.nanmean(y, axis=0)
    z_diff = z - np.nanmean(z, axis=0)
    del x, y, z

    # Estimate covariance matrix of the three measurement systems
    q_hat[0, 0] = np.nansum(x_diff**2, axis=0) / n_1
    q_hat[0, 1] = np.nansum(x_diff * y_diff, axis=0) / n_1
    q_hat[1, 0] = q_hat[0, 1]
    q_hat[0, 2] = np.nansum(x_diff * z_diff, axis=0) / n_1
    q_hat[2, 0] = q_hat[0, 2]
    q_hat[1, 1] = np.nansum(y_diff**2, axis=0) / n_1
    q_hat[1, 2] = np.nansum(y_diff * z_diff, axis=0) / n_1
    q_hat[2, 1] = q_hat[1, 2]
    q_hat[2, 2] = np.nansum(z_diff**2, axis=0) / n_1
    return q_hat

def apply_calibration(ref, x, factor):
    """Apply the calibration coefficient between a variable and its reference

    Parameters
    ----------
    ref : array_like
        Measurement system with shape (N,) or (N, f) used as reference for the calibration
        N is the sample size and f is the number of different collocated points.
    x : array_like
        Measurement system e ith shape (N,) or (N, f) that will be calibrated
        N is the sample size and f is the number of different collocated points.
    factor : float or 1D array
        Calibration correction factor between ``x`` and ``ref``.
        If 1D array its shape must be (f,).

    Returns
    -------
    x_prime : array_like
        Calibrated ``x`` measurement system with shape (N,) or (N, f).
        N is the sample size and f is the number of different collocated points.

    References
    ----------
    .. [Yilmaz_2013] Yilmaz, M.T., Crow, W.T., 2013.
    The Optimality of Potential Rescaling Approaches in Land Data Assimilation.
    Journal of Hydrometeorology 14, 650–660.
    https://doi.org/10.1175/JHM-D-12-052.1
    """

    # Eq. 3 of [Yilmaz_2013]_
    ref_mean = np.nanmean(ref, axis=0)
    x_mean = np.nanmean(x, axis=0)
    x_prime = ref_mean + factor * (x - x_mean)

    return x_prime

def tc_gruber(x, y, ref):
    """Calculate the recomended Triple Collocation Metrics by [Gruber_2016]_

    Parameters
    ----------
    x, y, z : array_like
        Arrays of shape (N,) or shape (N, f) of spatially-collocated measurement systems.
        N is the sample size and f is the number of different collocated points.

    Returns
    -------
    factors : tuple or 2D array
        Calibration correction factor between ``x``, ``y`` and ``ref``.
        If 1D array its shape must be (f,).
    stderr : tuple
        tuple of 3 standard errors for each of the measurement systems,
        corresponding to measurements x, y and z.
    snrs_db : tuple
        tuple of 3 Signal to Noise Ratio in decibels,
        after base10 logarithmic transformation.
    sens : tuple
        tuple of 3 System sensitivities to changes in the true signal
    """

    if x.ndim == 1:
        cov_matrix = covariance_matrix(x, y, ref)
    else:
        cov_matrix = covariance_matrix_vec(x, y, ref)

    stderr, cors, snrs_db, sens = etc(cov_matrix)

    return stderr, snrs_db, sens

def ctc_helper(x, y, z):
    if x.ndim == 1:
        cov_matrix = covariance_matrix(x, y, z)
    else:
        cov_matrix = covariance_matrix_vec(x, y, z)
    factors = scaling_factors(cov_matrix)

    y_corr = apply_calibration(x, y, factors[1])

    if x.ndim == 1:
        cov_matrix = covariance_matrix(x, y_corr, z)
    else:
        cov_matrix = covariance_matrix_vec(x, y_corr, z)

    stderr, rho_xy = lsetc(cov_matrix)

    stderr = stderr / factors

    return stderr, rho_xy


import pandas as pd
from pandas import Series, DataFrame
import matplotlib.pyplot as plt
import numpy as np

months = {1:'Jan', 2:'Feb', 3:'Mar', 4:'Apr', 5:'May', 6:'Jun', 7:'Jul', 8:'Aug', \
9:'Sep', 10:'Oct', 11:'Nov', 12:'Dec'}

plt.style.use('ggplot')

a_df = pd.read_csv('AviationDataEnd2016UP.csv',encoding = "ISO-8859-1")

a_df['times'] = a_df['Event.Date'].astype('datetime64[ns]')
a_df['month'] = a_df['times'].map(lambda x: months[x.month])
a_df['year'] = a_df['times'].map(lambda x: x.year)

def month_freq(df):
    fig, ax = plt.subplots(figsize=(8, 8))
    plt.title('Total Monthly Accidents (1908-2016)',fontsize=20)
    a_df['month'].value_counts().plot(ax=ax, kind='bar')
    plt.show()

def cause_of_accident(df):
    fig, ax = plt.subplots(figsize=(9, 6))
    plt.title('Stages of Flight',fontsize=20)
    a_df['Broad.Phase.of.Flight'].value_counts().plot(ax=ax, kind='bar')
    plt.subplots_adjust(bottom=0.40)
    plt.show()

def accidents_2016(df):
    fatal_years = df.drop_duplicates("Event.Id").groupby([df['year']])['Total.Fatal.Injuries'].sum()
    years = fatal_years[6:].index
    counts = fatal_years[6:].values
    fig, ax = plt.subplots(figsize=(9, 6))
    plt.title('Total Accidents (1982-2016)',fontsize=20)
    ax.plot(years, counts)
    plt.show()

def damage_taken(df):
    fig, ax = plt.subplots(figsize=(9, 6))
    plt.title('Overall Aircraft Damage',fontsize=20)
    a_df['Aircraft.Damage'].value_counts().plot(ax=ax, kind='bar')
    plt.subplots_adjust(bottom=0.40)
    plt.show()

def investigation_type(df):
    fatal_years = a_df[['year','Investigation.Type']].sort_values('year')[6:]

    accidents =  fatal_years.loc[fatal_years['Investigation.Type'] == 'Accident']
    incidents = fatal_years.loc[fatal_years['Investigation.Type'] == 'Incident']

    fig, ax = plt.subplots(figsize=(9, 6))
    plt.title('Accidents vs Incidents',fontsize=20)
    a = Series(accidents['year'].values,accidents['Investigation.Type'].values)
    i = Series(incidents['year'].values,incidents['Investigation.Type'].values)
    a.value_counts().plot(ax=ax)
    i.value_counts().plot(ax=ax, color='b')
    ax.legend(['Accidents','Incidents'], loc='upper right')
    plt.subplots_adjust(bottom=0.40)
    plt.show()

def main():
    #month_freq(a_df)
    #cause_of_accident(a_df)
    #accidents_2016(a_df)
    #damage_taken(a_df)
    investigation_type(a_df)

if __name__ == '__main__':
    main()


from visualization.visualization_tool import *
from additional_functions.additional_functions import *
from copy import deepcopy

def jarvis_convex_hull(points, epsilon=10 ** (-12), write_to_file=False, filename="jarvis_result"):
    length = len(points)
    lowest_point = points[0]
    for i in range(length):
        if abs(lowest_point[1] - points[i][1]) < epsilon:
            if lowest_point[0] > points[i][0]:
                lowest_point = points[i]
        elif lowest_point[1] > points[i][1]:
            lowest_point = points[i]
    convex_hull = [lowest_point]
    convex_hull_lines = []

    # creating scenes for visualization
    scenes = [Scene(points=[PointsCollection(deepcopy(points), color="black")])]

    first_vector_point = lowest_point
    second_vector_point = lowest_point
    for i in range(length):
        if abs(lowest_point[1] - points[i][1]) < epsilon:
            if second_vector_point[0] < points[i][0]:
                second_vector_point = points[i]
    if second_vector_point[0] == first_vector_point[0] and second_vector_point[1] == first_vector_point[1]:
        second_vector_point = first_vector_point
        first_vector_point = (lowest_point[0] - 1, lowest_point[1])
    else:
        convex_hull.append(second_vector_point)
        convex_hull_lines.append([convex_hull[0], convex_hull[1]])

        # adding scenes for visualization
        scenes.append(Scene(points=[PointsCollection(deepcopy(points), color='black'),
                                    PointsCollection(deepcopy(convex_hull), color="blue")],
                            lines=[LinesCollection(deepcopy(convex_hull_lines), color="blue")]))

    next_vector_point = None
    while next_vector_point is None or (
            next_vector_point[0] != lowest_point[0] or next_vector_point[1] != lowest_point[1]):
        next_vector_point = first_vector_point
        for point in points:
            orient = det(second_vector_point, next_vector_point, point)
            if orient < -epsilon:
                next_vector_point = point
            elif -epsilon < orient < epsilon:
                distance_from_next_point = (next_vector_point[1] - second_vector_point[1]) ** 2 + \
                                           (next_vector_point[0] - second_vector_point[0]) ** 2
                distance_from_point = (point[1] - second_vector_point[1]) ** 2 + \
                                      (point[0] - second_vector_point[0]) ** 2
                if distance_from_point > distance_from_next_point:
                    next_vector_point = point
        convex_hull.append(next_vector_point)
        convex_hull_lines.append([convex_hull[-2], convex_hull[-1]])

        # adding scenes for visualization
        scenes.append(Scene(points=[PointsCollection(deepcopy(points), color='black'),
                                    PointsCollection(deepcopy(convex_hull), color="blue")],
                            lines=[LinesCollection(deepcopy(convex_hull_lines), color="blue")]))

        first_vector_point = second_vector_point
        second_vector_point = next_vector_point
    convex_hull.append(next_vector_point)
    convex_hull_lines.append([convex_hull[-2], convex_hull[-1]])

    # adding scenes for visualization
    scenes.append(Scene(points=[PointsCollection(deepcopy(points), color='black'),
                                PointsCollection(deepcopy(convex_hull), color="blue")],
                        lines=[LinesCollection(deepcopy(convex_hull_lines), color="blue")]))

    if write_to_file:
        with open(f'{filename}.txt', 'w') as file:
            for item in convex_hull:
                file.write(f"{item}\n")
    return convex_hull[:-1], scenes


import argparse
import numpy as np
from tqdm import tqdm

import torch
import torch.optim as optim
from torch.nn import CrossEntropyLoss
from torch.utils.data import DataLoader
import torch.backends.cudnn as cudnn
import torchvision
import torchvision.transforms as transforms

from models.resnet_cifar10 import ResNet34
from optimizers import parse_optimizer, supported_optimizers

def parse_args(argv=None):
    """Parse command line arguments"""
    parser = argparse.ArgumentParser(description='PyTorch CIFAR10 Training')
    parser.add_argument('--model', default='resnet34', type=str, help='model',
                        choices=['resnet34'])
    parser.add_argument('--optim', type=str, help='optimizer', required=True,
                        choices=supported_optimizers())
    parser.add_argument('--seed', type=int, default=123, help='Random seed to use. default=123.')
    parser.add_argument('--epochs', type=int, default=200, help='Number of epochs')
    parser.add_argument('--milestones', type=int, default=[100, 150], nargs='?')
    parser.add_argument('--wandb-project', type=str, help='Project name on Weights&Biases')

    args, optim_args = parser.parse_known_args(argv)
    return args, optim_args

def build_dataset():
    """Build CIFAR10 train and test data loaders. Will download datasets if needed."""
    transform_train = transforms.Compose([
        transforms.RandomCrop(32, padding=4),
        transforms.RandomHorizontalFlip(),
        transforms.ToTensor(),
        transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010)),
    ])

    transform_test = transforms.Compose([
        transforms.ToTensor(),
        transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010)),
    ])

    trainset = torchvision.datasets.CIFAR10(root='./data', train=True, download=True,
                                            transform=transform_train)
    train_loader = DataLoader(trainset, batch_size=128, shuffle=True, num_workers=4)

    testset = torchvision.datasets.CIFAR10(root='./data', train=False, download=True,
                                           transform=transform_test)
    test_loader = DataLoader(testset, batch_size=100, shuffle=False, num_workers=4)

    # classes = ('plane', 'car', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck')

    return train_loader, test_loader

def build_model(model, device):
    net = {
        'resnet34': ResNet34,
    }[model]()
    net = net.to(device)

    if device == 'cuda':
        net = torch.nn.DataParallel(net)
        cudnn.benchmark = True
        cudnn.deterministict = True

    return net

def test(net, device, data_loader):
    net.eval()
    correct = 0
    total = 0
    with torch.no_grad():
        for batch_idx, (inputs, targets) in tqdm(enumerate(data_loader), total=len(data_loader)):
            inputs, targets = inputs.to(device), targets.to(device)
            outputs = net(inputs)

            _, predicted = outputs.max(1)
            total += targets.size(0)
            correct += predicted.eq(targets).sum().item()

    accuracy = 100. * correct / total
    print('Test acc %.3f' % accuracy)

    return accuracy

def train_epoch(net, epoch, device, data_loader, optimizer, criterion):
    net.train()
    train_loss = 0
    correct = 0
    total = 0
    for batch_idx, (inputs, targets) in tqdm(enumerate(data_loader), desc='Epoch {}'.format(epoch), total=len(data_loader)):
        inputs, targets = inputs.to(device), targets.to(device)
        optimizer.zero_grad()
        outputs = net(inputs)
        loss = criterion(outputs, targets)
        loss.backward()
        optimizer.step()

        train_loss += loss.item()
        _, predicted = outputs.max(1)
        total += targets.size(0)
        correct += predicted.eq(targets).sum().item()

    accuracy = 100. * correct / total
    print('train acc %.3f' % accuracy)
    print('train loss %.6f' % train_loss)

    return accuracy, train_loss

def train_cifar10(opt, optimizer_opts):
    # Set random seed
    np.random.seed(opt.seed)
    torch.manual_seed(opt.seed)

    train_loader, test_loader = build_dataset()

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

    net = build_model(opt.model, device)

    criterion = CrossEntropyLoss()
    optimizer, optimizer_run_name = parse_optimizer(opt.optim, optimizer_opts, net.parameters())
    scheduler = optim.lr_scheduler.MultiStepLR(optimizer, milestones=opt.milestones, gamma=0.1)

    run_name = 'cifar10_{}'.format(
        optimizer_run_name
    )

    train_accuracies = []
    test_accuracies = []
    train_loss_data = []

    if opt.wandb_project:
        import wandb
        wandb.init(project=opt.wandb_project, name=run_name, config=opt)
        wandb.watch(net)

    for epoch in range(opt.epochs):
        train_acc, train_loss = train_epoch(net, epoch, device, train_loader, optimizer, criterion)
        test_acc = test(net, device, test_loader)

        scheduler.step()

        if opt.wandb_project:
            wandb.log({
                'Training Loss': train_loss,
                'Training Accuracy': train_acc,
                'Test Accuracy': test_acc
            })

        train_accuracies.append(train_acc)
        test_accuracies.append(test_acc)
        train_loss_data.append(train_loss)

    if opt.wandb_project:
        # Update summary to the best value
        wandb.summary['Training Loss'] = min(train_loss_data)
        wandb.summary['Training Accuracy'] = max(train_accuracies)
        wandb.summary['Test Accuracy'] = max(test_accuracies)

if __name__ == '__main__':
    train_cifar10(*parse_args())


'''
Created on 27 Feb 2018

@author: danhbuithi
'''

import sys

from common.CommandArgs import CommandArgs
from sampling.RandomSplitter import RandomSplitter
from common.IOHelper import IOHelper

if __name__ == '__main__':
    config = CommandArgs({'input'   : ('', 'Path of input data'),
                          'test'   : ('', 'Path of test data.'),
                          'train'   : ('', 'Path of train data'),
                          'rate'    : ('', 'Rating of data')
                          })

    if not config.load(sys.argv):
        print ('Argument is not correct. Please try again')
        sys.exit(2)

    rate = float(config.get_value('rate'))
    train, test = RandomSplitter.split(config.get_value('input'), rate)
    IOHelper.write_file_in_lines(config.get_value('train'), train)
    IOHelper.write_file_in_lines(config.get_value('test'), test)

    print('Finished!!!')