from PyQt5.QtWidgets import *
from PyQt5 import uic
import sys
import os

loaded_data_view_file = os.path.join("ui", "loaded_data_view.ui")
ui_loadedDataView, QtBaseClass = uic.loadUiType(loaded_data_view_file)

class LoadedDataWindow(QMainWindow, ui_loadedDataView):
    """
    Shows a table with stored sim run data.
    Data can be saved, renamed, and loaded from this window
    """
    def __init__(self):
        super(LoadedDataWindow, self).__init__()
        self.setupUi(self)
        self.setWindowTitle("View Data")
        columns = ['date', 'data_id', 'sim_file', 'steps', '', '']
        self.loaded_data_table.setColumnCount(len(columns))
        self.loaded_data_table.setHorizontalHeaderLabels(columns)

    def add_row_to_table(self, row):
        """
        Add a row to the data table
        :param row: list of items to add to table
        """
        row_position = self.loaded_data_table.rowCount()
        self.loaded_data_table.insertRow(row_position)
        index = 0
        for item in row:
            if type(item) == QPushButton:
                self.loaded_data_table.setCellWidget(row_position, index, item)
            else:
                self.loaded_data_table.setItem(row_position, index, QTableWidgetItem(item))
            index = index + 1

    def clear_table(self):
        self.loaded_data_table.setRowCount(0)

if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = LoadedDataWindow()
    window.show()
    sys.exit(app.exec_())


'''A function which plots the data, the prediction line, and the loss'''

import numpy as np
from matplotlib import pyplot as plt
from vec_loss import loss_, is_vector_valid, is_theta_valid
from prediction import predict_

def plot_with_loss(x, y, theta):
    """Plot the data and prediction line from three non-empty numpy.ndarray.
    Args:
    x: has to be an numpy.ndarray, a vector of dimension m * 1.
    y: has to be an numpy.ndarray, a vector of dimension m * 1.
    theta: has to be an numpy.ndarray, a vector of dimension 2 * 1.
    Returns:
    Nothing.
    Raises:
    This function should not raise any Exception.
    """
    if not is_vector_valid(x) or not is_vector_valid(y) or \
            not is_theta_valid(theta) or x.size != y.size:
        print("Warning: plotting is impossible (wrong parameters)")
        return None

    y_hat = predict_(x, theta)
    plt.scatter(x, y)
    plt.plot(x, y_hat, 'r')
    for point, value in zip(x, y):
        plt.plot([point, point], [value, predict_(
            np.array([point]), theta)[0]], 'b--')
    plt.title(f'Cost: {2 * loss_(y, predict_(x, theta)):.6f}')
    plt.show()

if __name__ == "__main__":
    x = np.arange(1, 6)
    y = np.array([11.52434424, 10.62589482,
                 13.14755699, 18.60682298, 14.14329568])
    # Example 1:
    theta1 = np.array([18, -1])
    plot_with_loss(x, y, theta1)

    theta2 = np.array([14, 0])
    plot_with_loss(x, y, theta2)

    theta3 = np.array([12, 0.8])
    plot_with_loss(x, y, theta3)

    plot_with_loss(np.array([0, 1]), np.array([0, 1]), np.array([0, 1]))
    plot_with_loss(np.array([0, 1]), np.array([0, 1]), np.array([1, 1]))
    plot_with_loss(np.array([0, 2]), np.array([0, 0]), np.array([-1, 1]))


# 1389 - 케빈 베이컨의 6단계 법칙

from collections import deque

n, m = map(int, input().split())

friend = {}

for _ in range(m):
    a, b = map(int, input().split())

    if a not in friend:
        friend[a] = [b]
    else:
        friend[a].append(b)

    if b not in friend:
        friend[b] = [a]
    else:
        friend[b].append(a)

# print(friend)

score = [[0 for _ in range(n + 1)] for _ in range(n + 1)]
for i in range(n + 1):
    score[i][i] = -1
    score[i][0] = -1

# print(score)

def bfs(root):
    visited = []
    queue = deque([(root, 0)])

    while queue:
        person, level = queue.popleft()
        if person not in visited:
            if person != root:
                score[root][person] = level
            visited.append(person)
            for p in range(len(friend[person])):
                queue.append((friend[person][p], level + 1))

candidate = {}

for i in range(1, n + 1):
    bfs(i)
    candidate[i] = sum(score[i])

temp = list(candidate.values())
minimum = min(temp)

for k, v in candidate.items():
    if v == minimum:
        print(k)
        break


#!/usr/bin/env python
import parser
import app
import config
import random
import launch
import jobs
import logging
import argparse
import traceback
import settings
import signal
import flags

LOG_FORMAT = "%(asctime)-15s [%(process)d] %(name)s:%(levelname)s %(message)s"

logger = logging.getLogger("controller")

class Controller:
    def __init__(self, apps_list='apps.csv', configs_list='configs.csv', log_dir = "log/", slot_count = 16, batch_count = 100, video_mode = False, num_of_operations=1000, reuse_emulator=True):
        self.apps_list = apps_list
        self.configs_list = configs_list
        self.slot_count = slot_count
        self.batch_count = batch_count
        self.video_mode = video_mode
        self.num_of_operations = num_of_operations
        self.reuse_emulator = reuse_emulator
        self.log_dir = log_dir
        self.load()
        self.jobs = jobs.Jobs(slot_count)

        signal.signal(signal.SIGUSR1, self.handle_reload_sig)

    def handle_reload_sig(self, signum, frame):
        self.load()

    def load(self):
        parse = parser.Parser()
        self.apps = parse.parse_file(app.App, self.apps_list)
        self.configs = parse.parse_file(config.Config, self.configs_list)

    def pick_random(self, list):
        index = random.randrange(len(list))
        return list[index]

    def pick_pair(self):
        app = self.pick_random(self.apps)
        conf = self.pick_random(self.configs)
        while not app.has_libs(conf):
            app = self.pick_random(self.apps)
            conf = self.pick_random(self.configs)

        return (app, conf)

    def pick_and_start(self, job_id):
        (app, conf) = self.pick_pair()
        apk_file = app.get_apk_path()

        dev = conf.obtain_device()
        dev.check_config_template(True)
        logger.debug("config dev: %s" % dev.get_avd_name())

        if settings.use_app_template:
            app_conf = app.obtain_config(conf)
            app_dev = app_conf.obtain_device()
            logger.debug("app dev: %s" % app_dev.get_avd_name())

            if not app_dev.exists():
                logger.info("app template does not exist, create it")
                app_dev.create_from_config_template()
                launcher = launch.Launch(app_dev, apk_file)
                logger.info("app template created. installing components")
                launcher.prepare_app_template()
                logger.info("app template ready")

            config_line = app_conf.encode()
        else:
            config_line = conf.encode()

        self.launch_job(apk_file, config_line, job_id)

    def launch_job(self, apk_file, config_line, job_id):
        logger.debug("going to launch job")
        logger.debug("apk: %s" % apk_file)
        logger.debug("config line: %s" % config_line)
        if self.reuse_emulator:
            logger.debug("reusing emulator")
            cmd = "%s {0} 1 %s %d -f %s -d %s -n %d -b %d -p record" % (flags.get_base_path("rep.sh"), self.log_dir, job_id, apk_file, config_line, self.num_of_operations, self.batch_count)
        else:
            cmd = "%s {0} %d %s %d -f %s -d %s -n %d -p record" % (flags.get_base_path("rep.sh"), self.batch_count, self.log_dir, job_id, apk_file, config_line, self.num_of_operations)
        if settings.use_app_template:
            cmd += " -s"
        if not self.video_mode:
            cmd += " -c"

        logger.debug("command: %s" % cmd)
        self.jobs.launch_job(cmd)

    def stop(self):
        self.jobs.stop()

    def stop_when_done(self):
        self.jobs.stop_when_done()

def run_once():
    argparser = argparse.ArgumentParser()
    argparser.add_argument("-b", "--batch-count", help="number of runs in one batch",
            type=int, default=1)
    argparser.add_argument("-j", "--jobs-to-run", help="number of jobs to run",
            type=int, default=1)
    argparser.add_argument("-a", "--apps-list", help="list of applications",
            type=str, default="apps.csv")
    argparser.add_argument("-c", "--configs-list", help="list of configurations",
            type=str, default="configs.csv")
    argparser.add_argument("-l", "--log-dir", help="directory to store logs",
            type=str, default="log/")
    argparser.add_argument("-v", "--video-mode", help="show emulator window",
            action="store_true")
    argparser.add_argument("-n", "--num-of-operations", help="max number of operations before exit",
            type=int, default=1000)
    argparser.add_argument("-s", "--slot-count", help="number of slots",
            type=int, default=8)
    argparser.add_argument("-r", "--no-reuse-emulator", help="don't reuse emulator, recreate it",
            action="store_true")
    args = argparser.parse_args()

    logging.basicConfig(level=logging.DEBUG, format=LOG_FORMAT)

    controller = Controller(batch_count=args.batch_count, apps_list=args.apps_list,
            configs_list=args.configs_list, log_dir=args.log_dir, slot_count=args.slot_count,
            video_mode=args.video_mode, num_of_operations=args.num_of_operations, reuse_emulator=not args.no_reuse_emulator)
    for i in range(args.jobs_to_run):
        try:
            controller.pick_and_start(i)
        except:
            logger.error("got exception picking job: %s" % traceback.format_exc())
    controller.stop_when_done()
#    raw_input('enter to stop\n')
#    controller.stop()

if __name__ == "__main__":
    run_once()


import flask
from flask import Flask,request,render_template,jsonify
import os
import run
import sys

"""
This module start the server used by the web gui to elaborate the user's requests. It use as IP address the localhost,
and it take as parameter the listening port. If the port is omitted, the server will use the default port 5000.
"""

app = Flask(__name__, static_url_path='/static')
app.config['UPLOAD_FOLDER'] = os.path.join('resources','upload')

@app.route("/")
def index():
    """
    Generate the web gui's index page.
    :return: web gui's index page
    """
    return render_template('index.html', name="")

@app.route('/api/upload', methods=['POST', 'GET'])
def upload():
    """
    Given an uploaded file, save it and return a JSON containing parameters about the function that execute
    the algorithm.
    If it can not open that file, return an error message.
    :return: a JSON with the function call that execute the algorithm or a web page with an error message
    """
    if request.method == 'POST':
        file = request.files['file']
        if file:
            csv_file = os.path.join('..', app.config['UPLOAD_FOLDER'], file.filename)
            fd = file.save(csv_file)
            return flask.jsonify(run.main(csv_file, request.form))
    else:
        error = 'Invalid data'
        return render_template('index.html', error=error,meth="GET")

if __name__ == "__main__":
    if len(sys.argv) < 2:
        port = 5000
    else:
        port = int(sys.argv[1])
    app.run(debug=True, port=port)


from unittest import TestCase

from expects import (
    equal,
    expect,
    have_keys
)

from domain.models import Resource

class ResourceTestCase(TestCase):

    def setUp(self):
        self.correct_data = {
            'type': Resource.TYPE_MASTER,
            'name': 'How to fly a X-Wing',
            'description': 'Learn to fly a X-Wing and follow the resistance!',
            'duration': '2 weeks',
            'title': 'A beautiful diploma',
            'ects': 20,
            'price': '100 €',
            'url': 'http://followtheresistance.com',
            'date_init': '17 febrero 2021'
        }

    def test_it_raises_exception_when_type_value_is_unknown(self):
        wrong_data = self.correct_data.copy()
        wrong_data.update({'type': 'something wrong'})

        with self.assertRaises(ValueError):
            Resource(**wrong_data)

    def test_raises_exception_when_name_is_empty_or_blank(self):
        wrong_data = self.correct_data.copy()
        wrong_data.update({'name': '   '})

        with self.assertRaises(ValueError):
            Resource(**wrong_data)

    def test_it_exports_as_dict(self):
        resource = Resource(**self.correct_data)

        resource_dict = resource.as_dict()

        expect(resource_dict).to(have_keys(self.correct_data))

    def test_it_exports_as_list(self):
        resource = Resource(**self.correct_data)

        resource_list = resource.as_list()

        expect(resource_list).to(equal([
            Resource.TYPE_MASTER,
            'How to fly a X-Wing',
            'Learn to fly a X-Wing and follow the resistance!',
            '2 weeks',
            'A beautiful diploma',
            20,
            '100 €',
            'http://followtheresistance.com',
            '17 febrero 2021'
        ]))


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

from importlib import reload

import blpapi
reload(blpapi)

import pandas as pd

# A directory for files that are not tracked by git, to which we'll download data
TMP_DIR = '../tmp/'

# Tickers and ESG-related fields we want to download from Bloomberg's API using the BDP function
ESG_PARAMS = {
    'tickers' : [
        'NVDA US Equity',
        'AAPL US Equity'
        ],
    'flds' : [
        'ESG_DISCLOSURE_SCORE',
        'ENVIRON_DISCLOSURE_SCORE',
        'SOCIAL_DISCLOSURE_SCORE',
        'GOVNCE_DISCLOSURE_SCORE',
        'PERCENT_OF_DISCLOSURE',
        'EMISSIONS_DISCLOSURE_INDICATOR',
        'CDLI_SCORE'
        ]
}
HIST_ESG_PARAMS = {
    'tickers' : [
        'NVDA US Equity',
        'AAPL US Equity'
        ],
    'flds' : [
        'ESG_DISCLOSURE_SCORE',
        'ENVIRON_DISCLOSURE_SCORE',
        'SOCIAL_DISCLOSURE_SCORE',
        'GOVNCE_DISCLOSURE_SCORE',
        'PERCENT_OF_DISCLOSURE',
        'EMISSIONS_DISCLOSURE_INDICATOR',
        'CDLI_SCORE'
        ],
    'start_date' : '2021-09-01',
    'end_date' : '2021-09-30'
}
HIST_ESG_OVERRIDES = {
    'startDate' : 'FY 2021',
    'endDate' : 'FY 2021',
    'currency' : 'USD',
    'periodicityAdjustment' : 'FISCAL',
    'BEST_FPERIOD_OVERRIDE' : 'FY',
    'FILING_STATUS' : 'MR',
    'Sort' : 'A',
    'Dates' : 'H',
    'DateFormat' : 'P',
    'nonTradingDayFillMethod' : 'NIL_VALUE',
    'Direction' : 'H',
    'adjustmentFollowDPDF' : 'TRUE'
}

if __name__ == '__main__':

    ## ESG

    # Compose dictionary for our large ESG data request, using our new list of ESG fields with the same tickers as our original request
    esg_params = ESG_PARAMS.copy()
    esg_params['tickers'] = ['AAPL US Equity']

    # Use the default ESG_PARAMS to download a small ESG dataset
    esg_data = blp.bdp(**esg_params)

    # Download the small ESG dataset to a CSV file, using pandas.DataFrame's to_csv() method
    esg_data.to_csv(TMP_DIR + 'esg_data.csv')

    # Print the first 3 records from the DataFrame to stdout.
    print('Sample of esg_data:\n',esg_data.head(3),'\n')

    ## HIST ESG

    # Excel API call used as template
    # =BDH("AAPL US Equity","ESG_DISCLOSURE_SCORE","FY 2014","FY 2014","Currency=USD","Period=FY","BEST_FPERIOD_OVERRIDE=FY","FILING_STATUS=MR","Sort=A","Dates=H","DateFormat=P","Fill=—","Direction=H","UseDPDF=Y")

    # Compose dictionary for our large ESG data request, using our new list of ESG fields with the same tickers as our original request
    hist_esg_params = HIST_ESG_PARAMS.copy()
    hist_esg_params['tickers'] = ['AAPL US Equity']

    # Use the default ESG_PARAMS to download a small ESG dataset
    hist_esg_data = blp.bdh(**hist_esg_params)

    # Download the small ESG dataset to a CSV file, using pandas.DataFrame's to_csv() method
    hist_esg_data.to_csv(TMP_DIR + 'hist_esg_data.csv')

    # Print the first 3 records from the DataFrame to stdout.
    print('Sample of hist_esg_data:\n',hist_esg_data.head(3),'\n')

    ## TEST

    test_data = blp.bdh(tickers=['AAPL US Equity'], flds=['ESG_DISCLOSURE_SCORE','EMISSIONS_DISCLOSURE_INDICATOR'], **HIST_ESG_OVERRIDES) #start_date='2021-09-01', end_date='2021-09-30',
    print('Sample of test_data:\n',test_data.head(3),'\n')


from enum import IntEnum

import statefun
import json

class OrderStatus(IntEnum):
    RESERVE = 1
    RESERVED = 2
    PAID = 3
    CONFIRMED = 4
    DISPATCHED = 5
    NOSTOCK = 6
    NORESERVATION = 7
    CANCELLED = 8
    FAILED = 9
    REFUNDED = 10

# custom class as Types
class Book:
    TYPE: statefun.Type = statefun.simple_type(
        typename="com.bookstore.types/Book",
        serialize_fn=lambda book : json.dumps(book.__dict__).encode(),
        deserialize_fn=lambda serialized : Book(**json.loads(serialized)))

    def __init__(self, isbn, name, author):
        self.isbn = isbn
        self.name = name
        self.author = author

class Order:
    TYPE: statefun.Type = statefun.simple_type(
        typename="com.bookstore.types/Order",
        serialize_fn=lambda order : json.dumps(order.__dict__).encode(),
        deserialize_fn=lambda serialized : Order(**json.loads(serialized)))

    def __init__(self, buyer, isbn, status=OrderStatus.RESERVE, value=0.0):
        self.buyer = buyer
        self.isbn = isbn
        self.status = status
        self.value = value

# list as type
Reservations = []
RESERVATIONS_TYPE: statefun.Type = statefun.simple_type(
    typename="com.bookstore.types/Reservation",
    serialize_fn=lambda reservations : ','.join(reservations).encode(),
    deserialize_fn=lambda serialized : serialized.decode().split(',') if serialized else list())

# JSON as type
Payment: statefun.Type = statefun.make_json_type('com.bookstore.types/Payment') # {'id': isbn, 'user': buyer, 'value': order_value, 'status': PaymentStatus}

class PaymentStatus(IntEnum):
    DEBIT = 0
    DEBITED = 1
    CREDIT = 2
    CREDITED = 3
    REFUND = 4
    REFUNDED = 5


import numpy as np
import cv2
import math as m
import random as r
import scipy.linalg as la
import scipy.sparse as sp
import visual as vis

#Thanks to Stephen Dunlap for the following 2 assert functions!!!
def assert_np_matrix(m, shape):
    '''Verify that the variable is an np.ndarray, has the correct dimensionality,
    and is of the correct shape. Raise an exception if it fails any test.

    Args:
        m: Matrix to test
        shape (tuple(int,int)) or (tuple(int,)): Required shape (rows, cols).
            If one element is 0, ignores that dimension (allows for a 3XN array, where
            you don't know what N is at compile-time)

    Raises:
        ValueError: Wrong type, dimensionality or size
    '''

    if not type(m) is np.ndarray:
        raise ValueError('Wrong type', 'matrix must be np.ndarray')

    if not len(shape) == len(m.shape):
        raise ValueError('Wrong dimensions',
                         f'array must be {len(shape)}-dimensional, got {len(m.shape)}-d array')

    if len(shape)==1:
        if shape[0]!=0 and m.shape[0] != shape[0]:
            raise ValueError('Wrong shape', f'matrix must be a {shape} but is {m.shape}')
    else:
        if not shape[0] and not m.shape[1] == shape[1]:
            raise ValueError('Wrong shape', f'matrix must be a {shape} but is {m.shape}')

        if not shape[1] and not m.shape[0] == shape[0]:
            raise ValueError('Wrong shape', f'matrix must be a {shape} but is {m.shape}')

        if shape[0] and shape[1] and not m.shape == shape:
            #print('ERROR: set_translation(): w_T_c must be a 1x3 matrix')
            raise ValueError('Wrong shape', f'matrix must be a {shape} but is {m.shape}')

def assert_np_matrix_choice(m, shapes):
    '''
    Take in m and see if it is one of a list of shapes

    Args:
        m:  (numpy) matrix to test
        shapes: a list of tuples describing shapes.  Each
            must be either 1-D or 2-D (rows, cols)

    Raises:
        ValueError: Wrong type, dimensionality or size
    '''
    if not type(m) is np.ndarray:
        raise ValueError('Wrong type', 'matrix must be np.ndarray')

    passed = False
    for shape in shapes:
        try:
            assert_np_matrix(m, shape)
        except ValueError:
            pass
        else:
            passed = True
            break
    if not passed:
        raise ValueError('Wrong shape', f'matrix is {m.shape}, but needs to be one of {shapes}')

def assert_np_3vec(m):
    '''Alias for assert_np_matrix_choice(m, ((3,1),(1,3),(3,)) )'''
    assert_np_matrix_choice(m, ((3,),(3,1),(1,3)) )

def assert_np_2vec(m):
    '''Alias for assert_np_matrix_choice(m, ((2,1),(1,2),(2,)) )'''
    assert_np_matrix_choice(m, ((2,),(2,1),(1,2)) )

def assert_cam(cam):
    '''
    Assert that the input 'cam' is of type vis.Camera

    Raises:  ValueError
    '''
    if not type(cam) is vis.Camera:
        raise ValueError('Wrong type', 'input must be of type vis.Camera')

#Not really sure if this function should be here or in vis.rot
#Could raise InvalidDCM in vis.rot instead...?
def assert_3D_dcm(M):
    '''
    Takes in M and makes sure it is a 3D direction cosine matrix.

    Raises: ValueError
    '''
    assert_np_matrix(M,(3,3))
    if not m.isclose(np.linalg.det(M), 1.0):
        raise ValueError('Not DCM', 'input must have determinant = 1.0')
    for i in range(3):
        if not m.isclose(np.linalg.norm(M[i]), 1.0):
            raise ValueError('Not DCM', f'row {i} of M is not unit length')
        for j in range(i+1,3):
            if not m.isclose(M[i].dot(M[j]), 0.0, abs_tol=1E-9):
                raise ValueError('Not DCM',f'rows {i} and {j} are not orthogonal')

def create_skew_symm_mat ( axis_rots ):
    '''
    Creates a 3x3 skew symmetric matrix from a 3-vector. Note that this is
    the skew-symmetric for differential rotations about the axis, _not_ the
    matrix that allows you to do a vector cross-product. (Though it can be
    used to create an F or E matrix because the sign doesn't matter...)

    Args:
    axis_rots can be a list, tuple, or ndarray of one dimension.
        Anything that allows it to reference its elements as axis_rots[i],
        where i is 0, 1, or 2

    Returns:  3x3 numpy array
    '''
    assert(len(axis_rots)==3 and axis_rots.size == 3)

    return np.array([[0., axis_rots[2], -axis_rots[1]],
                     [-axis_rots[2], 0., axis_rots[0]],
                     [axis_rots[1], -axis_rots[0], 0.]])

def imshow_reduced(window_name, img, max_width=800, max_height=None):
    '''This function is an extension to cv2.imshow. If the passed in image
    is too big, it will be downsized (by factors of 2) until its width is less
    than "max_width".  If max_height is passed in, will also check the displayed
    image is smaller than max_height.  By default, no check of the height occurs.

    Need to use cv2.waitKey after calling this function (just like cv2.imshow)

    Args:
        First two args same as cv2.imshow
        window_name:  string that will be on the top of the window
        img: cv2 image structure (really a numpy array)
        max_width:  maximum width of image before it shows
        max_height:  (if used), maximum height of image before it shows
    '''

    im_shape = img.shape
    tmp_im = img
    while im_shape[1] > max_width:
        tmp_im = cv2.pyrDown(tmp_im)
        im_shape = tmp_im.shape
    if max_height is not None:
        while im_shape[0] > max_height:
            tmp_im = cv2.pyrDown(tmp_im)
            im_shape = tmp_im.shape

    cv2.imshow(window_name, tmp_im)

def create_random_world_points(cam, num_pts, dist_range = None):
    '''Create num_pts random world locations within the view of the camera
    at location w_t_cam and with orientation c_R_w.  dist_range defines how
    far away from the camera the points can be.  Default is between 10 and 60 meters.

    Args:
        cam:  A vis.Camera object
        num_pts: How many (random) points to create
        dist_range:  A tuple specifying the range the points should be in away from the camera (default (10,60))

    This function returns a tuple of the world points and their pixel locations
    '''
    if dist_range is None:
        dist_range = (10.,60.)
    diff_dist = dist_range[1] - dist_range[0]

    d_rand = np.random.rand(num_pts)*diff_dist + dist_range[0]

    #Create some random image locations
    x_rand = np.random.rand(num_pts,2)
    x_rand[:,0] *= cam.im_size[0]
    x_rand[:,1] *= cam.im_size[1]
    x_d_tuples = list((x_rand[ii],d_rand[ii].item()) for ii in range(num_pts))
    #Took random image points and projected them into space
    return (cam.backproject_points(x_d_tuples), x_rand)

def create_random_camera(K=None, loc_range = (-200,200), angle_range=(-m.pi,m.pi)):
    '''
    This function creates a vis.Camera object at a random location and with a random
    orientation.  Both are uniformly distributed in the ranges passed in.

    Args:
    K: A calibration matrix.
    loc_range:  x,y, and z limits on where the camera can be.  Default is (-200,200)
    angle_range:  limits on rotation angles.  Default is (-pi,pi)

    Returns:  A vis.Camera object
    '''
    loc_dist = loc_range[1] - loc_range[0]
    angle_dist = angle_range[1] - angle_range[0]
    w_cam = np.random.rand(3)*loc_dist + loc_range[0]
    angles = np.random.rand(3)*angle_dist + angle_range[0]
    my_R = vis.rot.euler_angles(angles,[2,1,0])
    if K is not None:
        assert_np_matrix(K, (3,3))
        return vis.Camera(K,my_R,w_cam)
    else:
        return vis.Camera(c_R_w=my_R, w_cam=w_cam )

def create_circular_scenario(radius=100, n_cams = 72, n_points = 400, K=None):
    '''
    This function spaces cameras out around a circle, all looking in at
    the center of the circle.  The circle will be in the "x-z" plane.

    Args:
        radius:  The radius of the circle on which the cameras will lie (default=100)
        n_cams:  number of cameras on the circle.  Must be >= 8 (default=72)
        n_points: Number of points to create in scenario.  Will create the number
            passed in rounded up to the closest value dividable by 8. (default=400)
        K: Calibration matrix for the cameras to be created.  If None, will use the
            default K in the Camera class. (default = None)

    Returns: A tuple with a list of Camera objects and an Nx3 array of world points.
        Note that camera 0 will be at the origin and rotation will be identity.
    '''

    #First, set up the cameras.
    #Must be >= 8 for later code to work...
    assert n_cams >= 8

    angle_cams = 2*m.pi/n_cams

    #Now to generate the cameras
    cams = []
    for ii in range(n_cams):
        cam_loc = radius*np.array([m.cos(angle_cams*ii-m.pi/2), 0., m.sin(angle_cams*ii-m.pi/2)]) \
            + np.array([0.,0., radius])
        cam_rot = vis.rot.axis1(-angle_cams*ii)
        cams.append(vis.Camera(K, cam_rot, cam_loc))

    #%% Create the world points.  This will find world points from
    #8 different cameras set up (roughly) evenly around the circle
    # If n_cams is divisible by 8, it will be at (0, 45, 90, etc. degrees)

    #Used for generating points
    min_dist = .15*radius
    max_dist = 2*radius-min_dist
    n_pts_per_cam = m.ceil(n_points/8) #For 400 total points
    cam_step = m.floor(n_cams/8)

    #Actually generate the points
    w_pts = np.zeros((n_pts_per_cam*8,3))
    for ii in range(8):
        curr_cam = cams[ii*cam_step]
        #Note the [0] at the end to get world points (I am ignoring pixel points for now)
        w_pts[ii*n_pts_per_cam:(ii+1)*n_pts_per_cam] = \
            vis.utils.create_random_world_points(curr_cam, n_pts_per_cam, (min_dist,max_dist))[0]

    return (cams,w_pts)

def create_feats(cam, w_pts, found_percent=.8, rand_add_percent=.2, bad_desc_percent = .1,  sd_noise=1):
    '''
    This takes in the true world points and creates cv2.KeyPoint
    values for all that project to within the image as specified by
    the camera passed in.  Noise and problems are all specified by the
    parameters passed in.  It adds spurious points (not corresponding
    to any world point) as determined by rand_add_percent
    perc_outliers . It also adds noise to the projected points.  The
    "descriptors" currently passed out are the world point it corresponds
    to, or a negative number if it was an outlier.

    Args:
        cam: A vis.Camera object used to project the world points
        w_pts: The true world points
        percent_found:  How many of the features that project in the image to "find"
            and make actual features (default = .8)
        rand_add_percent: How many completely spurious points (not corresponding
            with any world points) to add.  1= the same number as there are inliers.
            .5 is half as many as inliers.  Actual number added is a random value.
            (default = .2)
        bad_desc_percent: How many features (probabilisticly) to assign the wrong
            descriptor to. This only permutes within the features found in the image,
            so it won't pull in some other random "descriptor"  (default = .05)
        sd_noise: Standard deviation of the (Gaussian isotropic) noise to add to
            pixel locations of the inliers (default = 1)

    Returns: Two lists.  One of cv2.KeyPoint values, the second of "descriptors"
    '''

    #This projects all points
    p_Xs = cam.project_points(w_pts)
    #only keep the ones that are in the image
    in_feats=[]
    in_desc=[]
    for i,p_x in enumerate(p_Xs):
        if cam.in_image(p_x) and r.random() < found_percent:
            in_feats.append(p_x)
            in_desc.append(i)
    in_feats = np.array(in_feats)
    in_feats = in_feats + np.random.randn(*in_feats.shape) * sd_noise
    in_kps = [cv2.KeyPoint(p_x[0],p_x[1],1.) for p_x in in_feats]

    if rand_add_percent > 0.:
        size_outliers = m.floor(rand_add_percent)*len(in_feats)
        my_percent = rand_add_percent - m.floor(rand_add_percent)
        #Have some randomness in the number of outliers...
        size_outliers += \
            np.count_nonzero(np.random.choice([False,True],
                         size=(len(in_feats),),
                         p=[1-my_percent, my_percent]))
        out_feats = np.random.rand(size_outliers,2)*cam.im_size
        out_kps = [cv2.KeyPoint(p_x[0],p_x[1],1.) for p_x in out_feats]
        in_kps.extend(out_kps)
        in_desc.extend( -np.arange(1,size_outliers+1) )

    in_desc=np.array(in_desc)
    if bad_desc_percent >= 0.:
        to_shuffle_idx = np.random.rand(len(in_desc)) <= bad_desc_percent
        in_desc[to_shuffle_idx] = np.random.permutation(in_desc[to_shuffle_idx])

    combined = list(zip(in_kps, in_desc))
    r.shuffle(combined)
    return tuple(zip(*combined))

def inv_sparse_block_diagonal(A, blk_size):
    '''
    Takes in a matrix that is block diagonal, with each
    block being of size blk_size X blk_size.  This can be a sparse matrix as
    long as you can index into it (i.e., A[0:blk_size,0:blk_size] is valid).
    Outputs the inverse as a scipy.sparse.bsr_matrix.

    Warning:  This does nothing to check that A is actually block diagonal,

    Args:
        A: The block diagonal matrix to invert
        blk_size:  The size of each block diagonal matrix

    Returns a scipy.sparse.bsr_matrix of the same size as A
    '''
    assert A.shape[0]==A.shape[1]
    assert (A.shape[0] % blk_size) == 0
    assert (A.shape[1] % blk_size) == 0
    assert sp.issparse(A)

    num_blks = int(round(A.shape[1]/blk_size))

    data = np.zeros((num_blks,blk_size,blk_size))
    idx = np.arange(num_blks)
    idx_ptr = np.arange(num_blks+1)

    for ii in range(num_blks):
        tmp = A[ii*blk_size:(ii+1)*blk_size,ii*blk_size:(ii+1)*blk_size]
        data[ii] = la.inv(tmp.toarray()) #No sparse-ness attempted here...

    return sp.bsr_matrix((data,idx,idx_ptr))

def align_pts(true_pts, test_pts):
    '''
    This function takes in two sets of points and scales, rotates, and translates
    the "test_pts" to best align with the true_pts.  Note that these are locations
    in the world, not full state vectors

    This is algorithm 1 in "A tutorial on quantitative trajectory evaluation for
    visual(-inertial) odometry" by Zichao Zhang and David Scaramuzza

    Args:
        true_pts -- set of points to align to
        test_pts -- set of points to align with true_pts

    Returns:  A scaled, rotated, and translated version of test_pts
    '''
    assert type(true_pts) is np.ndarray
    assert type(test_pts) is np.ndarray
    assert true_pts.shape==test_pts.shape

    mu_true = np.mean(true_pts,0)
    mu_test = np.mean(test_pts,0)
    # sig_true = np.var(true_pts)  #Not used later on
    sig_test = la.norm(test_pts-mu_test)**2.
    Sigma = np.zeros((true_pts.shape[1],true_pts.shape[1]))
    for true_v,test_v in zip(true_pts,test_pts):
        Sigma += np.outer(true_v-mu_true,test_v-mu_test)
    U,D,Vh = la.svd(Sigma)
    W = np.eye(true_pts.shape[1])
    if la.det(U)*la.det(Vh) < 0:
        W[-1,-1] = -1
    R = U.dot(W.dot(Vh))
    s = np.sum(D.dot(W))/sig_test #D is a vector, not a matrix from SVD
    # s=1
    t = mu_true - s * R.dot(mu_test)
    #R, s, and t found.  Now going_out = s*R*test + t
    return s*test_pts.dot(R.T) + t #Flipped the transpose rather than transposing test_pts then transposing again

## Some robust cost functions
class GemanMcClure:
    def __init__(self, s_sq=9):
        self.s_sq = s_sq
        self.count = 0

    def __call__(self, res):
        dist_sq = la.norm(res)**2
        if dist_sq > self.s_sq:
            self.count += 1
        return 1/(dist_sq/self.s_sq + 1)

    def resetCount(self):
        self.count = 0

    def getCount(self):
        return self.count

class Huber:
    def __init__(self, s=1):
        self.s = s
        self.s_sq = s*s

    def __call__(self, res):
        dist_sq = la.norm(res)**2
        if dist_sq <= self.s_sq:
            return 1.
        else:
            return m.sqrt(self.s/m.sqrt(dist_sq))
#### End robust cost functions


#!/usr/bin/python3
"""Defines unnittests for models/engine/file_storage.py."""
import os
import json
import pep8
import unittest
from datetime import datetime
from models.base_model import BaseModel
from models.user import User
from models.state import State
from models.city import City
from models.amenity import Amenity
from models.place import Place
from models.review import Review
from models.engine.file_storage import FileStorage

class TestFileStorage(unittest.TestCase):
    """Unittests for testing the FileStorage class."""

    @classmethod
    def setUpClass(cls):
        """FileStorage testing setup.

        Temporarily renames any existing file.json.
        Resets FileStorage objects dictionary.
        Creates instances of all class types for testing.
        """
        try:
            os.rename("file.json", "tmp")
        except IOError:
            pass
        FileStorage._FileStorage__objects = {}
        cls.storage = FileStorage()
        cls.base = BaseModel()
        key = "{}.{}".format(type(cls.base).__name__, cls.base.id)
        FileStorage._FileStorage__objects[key] = cls.base
        cls.user = User()
        key = "{}.{}".format(type(cls.user).__name__, cls.user.id)
        FileStorage._FileStorage__objects[key] = cls.user
        cls.state = State()
        key = "{}.{}".format(type(cls.state).__name__, cls.state.id)
        FileStorage._FileStorage__objects[key] = cls.state
        cls.place = Place()
        key = "{}.{}".format(type(cls.place).__name__, cls.place.id)
        FileStorage._FileStorage__objects[key] = cls.place
        cls.city = City()
        key = "{}.{}".format(type(cls.city).__name__, cls.city.id)
        FileStorage._FileStorage__objects[key] = cls.city
        cls.amenity = Amenity()
        key = "{}.{}".format(type(cls.amenity).__name__, cls.amenity.id)
        FileStorage._FileStorage__objects[key] = cls.amenity
        cls.review = Review()
        key = "{}.{}".format(type(cls.review).__name__, cls.review.id)
        FileStorage._FileStorage__objects[key] = cls.review

    @classmethod
    def tearDownClass(cls):
        """FileStorage testing teardown.

        Restore original file.json.
        Delete all test class instances.
        """
        try:
            os.remove("file.json")
        except IOError:
            pass
        try:
            os.rename("tmp", "file.json")
        except IOError:
            pass
        del cls.storage
        del cls.base
        del cls.user
        del cls.state
        del cls.place
        del cls.city
        del cls.amenity
        del cls.review

    def test_pep8_FileStorage(self):
        """Test pep8 styling."""
        style = pep8.StyleGuide(quiet=True)
        p = style.check_files(['models/engine/file_storage.py'])
        self.assertEqual(p.total_errors, 0, "fix pep8")

    def test_docstrings(self):
        """Check for docstrings."""
        self.assertIsNotNone(FileStorage.__doc__)
        self.assertIsNotNone(FileStorage.all.__doc__)
        self.assertIsNotNone(FileStorage.new.__doc__)
        self.assertIsNotNone(FileStorage.reload.__doc__)
        self.assertIsNotNone(FileStorage.delete.__doc__)

    def test_attributes(self):
        """Check for attributes."""
        self.assertEqual(str, type(FileStorage._FileStorage__file_path))
        self.assertEqual(dict, type(FileStorage._FileStorage__objects))

    def test_methods(self):
        """Check for methods."""
        self.assertTrue(hasattr(FileStorage, "all"))
        self.assertTrue(hasattr(FileStorage, "new"))
        self.assertTrue(hasattr(FileStorage, "reload"))
        self.assertTrue(hasattr(FileStorage, "delete"))

    def test_init(self):
        """Test initialization."""
        self.assertTrue(isinstance(self.storage, FileStorage))

    def test_all(self):
        """Test default all method."""
        obj = self.storage.all()
        self.assertEqual(type(obj), dict)
        self.assertIs(obj, FileStorage._FileStorage__objects)
        self.assertEqual(len(obj), 7)

    def test_all_cls(self):
        """Test all method with specified cls."""
        obj = self.storage.all(BaseModel)
        self.assertEqual(type(obj), dict)
        self.assertEqual(len(obj), 1)
        self.assertEqual(self.base, list(obj.values())[0])

    def test_new(self):
        """Test new method."""
        bm = BaseModel()
        self.storage.new(bm)
        store = FileStorage._FileStorage__objects
        self.assertIn("BaseModel." + bm.id, store.keys())
        self.assertIn(self.base, store.values())

    def test_save(self):
        """Test save method."""
        self.storage.save()
        with open("file.json", "r", encoding="utf-8") as f:
            save_text = f.read()
            self.assertIn("BaseModel." + self.base.id, save_text)
            self.assertIn("User." + self.user.id, save_text)
            self.assertIn("State." + self.state.id, save_text)
            self.assertIn("Place." + self.place.id, save_text)
            self.assertIn("City." + self.city.id, save_text)
            self.assertIn("Amenity." + self.amenity.id, save_text)
            self.assertIn("Review." + self.review.id, save_text)

    def test_reload(self):
        """Test reload method."""
        bm = BaseModel()
        with open("file.json", "w", encoding="utf-8") as f:
            key = "{}.{}".format(type(bm).__name__, bm.id)
            json.dump({key: bm.to_dict()}, f)
        self.storage.reload()
        store = FileStorage._FileStorage__objects
        self.assertIn("BaseModel." + bm.id, store)

    def test_reload_no_file(self):
        """Test reload method with no existing file.json."""
        try:
            self.storage.reload()
        except Exception:
            self.fail

    def test_delete(self):
        """Test delete method."""
        bm = BaseModel()
        key = "{}.{}".format(type(bm).__name__, bm.id)
        FileStorage._FileStorage__objects[key] = bm
        self.storage.delete(bm)
        self.assertNotIn(bm, FileStorage._FileStorage__objects)

    def test_delete_nonexistant(self):
        """Test delete method with a nonexistent object."""
        try:
            self.storage.delete(BaseModel())
        except Exception:
            self.fail

if __name__ == "__main__":
    unittest.main()


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

from datetime import datetime

from flask import *
from flask_peewee.utils import get_object_or_404

from app import app
from models import *

@app.route('/favicon.ico')
def favicon():
    return send_file("static/img/favicon.png", cache_timeout=86400)

@app.route('/')
def main(template_name="main.html"):
    return render_template(template_name)

@app.route('/frame')
def frame(template_name="frame.html"):
    return render_template(template_name)

@app.route('/signup', methods=['POST'])
def signup():
    full_name = request.form['FullName1']
    mobile = request.form['MobileNumber1']
    birth_date = request.form['BirthDate1']
    birth_date = datetime.strptime(birth_date, "%Y-%m-%d").date()
    pin_code = request.form['PinCode1']
    try:
        user = User.get(User.mobile==mobile)
    except:
        user = User(
            full_name=full_name,
            mobile=mobile,
            birth_date=birth_date,
            pin_code=pin_code)
        user.save()
    if user.pin_code == pin_code:
        session['uid'] = user.id
        return redirect('/docs')
    return redirect('/')

@app.route('/login', methods=['POST'])
def login():
    mobile = request.form['MobileNumber1']
    pin_code = request.form['PinCode1']
    try:
        user = User.get(User.mobile==mobile)
        if user.pin_code == pin_code:
            session['uid'] = user.id
            return redirect('/docs')
    except:
        pass
    return redirect('/')

@app.route('/logout')
def logout():
    session['uid'] = None
    return redirect('/')

@app.route('/docs')
def docs(template_name="docs.html"):
    user_id = session.get('uid')
    if not user_id:
        return redirect('/')
    user = User.get(User.id == user_id)
    travels = Travel.select().where(Travel.user == user)
    documents = Document.select().where(Document.user == user)
    return render_template(template_name,
        user=user, travels=travels, documents=documents)

@app.route('/the-problem/')
def problem(template_name="problem.html"):
    return render_template(template_name)

@app.route('/privacy-policy/')
def privacy(template_name="privacy.html"):
    return render_template(template_name)


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

def load_history(folder):
    file = folder + 'Simplex_History.txt'
    with open(file, 'r') as file:
        data = file.read().replace('\n', '')
    return ast.literal_eval(data)

def get_best_vals(dict):
    res = {}
    for key in dict.keys():
        if key not in ['steps', 'simplex_sd', 'chi**2']:
           res[key] = dict[key][-1]
    return res

def load_lc(lc_file = 'RS_CHA_lightcurve.txt', plot= False, npoints = 5000, points_step = 1, parts_of_lc = False):
    data = np.loadtxt(lc_file).T
    times = data[0]
    flux = data[1]
    try:
        sigmas = data[2]
    except:
        pass
    if parts_of_lc:
        times = times[:npoints:points_step]
        flux = flux[:npoints:points_step]
    if plot:
        fig, ax = plt.subplots(figsize=(10, 6))
        ax.plot(times, flux, 'ko', markersize = 0.75)
        plt.show()
        plt.close()
    try:
        return  times, flux, sigmas
    except:
        return times, flux, np.ones(len(flux))

def remove_binary_signal(folder, endurance_dir, compare_lc,  exp_time = False):
    history = load_history(folder)
    best_vals = get_best_vals(history)
    print('Load Light Curve')
    times, fluxes, sigmas = load_lc(endurance_dir + compare_lc, npoints=10, parts_of_lc=True )
    fig3 = plt.figure(constrained_layout=True, figsize=(15,8))
    gs = fig3.add_gridspec(2, 3)
    ax00 = fig3.add_subplot(gs[0, :-1])
    ax01 = fig3.add_subplot(gs[1, :-1])
    ax10 = fig3.add_subplot(gs[0, -1:])
    ax11 = fig3.add_subplot(gs[1, -1:])
    ax00.plot(times, fluxes, 'ko', markersize = 0.75)
    ax10.plot(times, fluxes, 'ko', markersize = 0.75)
    for ax in [ax00, ax01, ax10, ax11]:
        ax.set_xlabel('Time - 2457000 [BTJD days]')
        ax.set_ylabel('normalized flux')
    for ax in [ax10, ax11]:
        ax.set_xlim(np.min(times), np.min(times) + 3.2)
    print('Start Phoebe')
    chi2, mod_flux = phoebe_controller.chi_square_multi(best_vals, fluxes, times, sigmas, exp_time= exp_time)
    ax00.plot(times, mod_flux, 'r-')
    ax10.plot(times, mod_flux, 'r-')
    ax01.plot(times, mod_flux-fluxes, 'ko', markersize = 0.75)
    ax11.plot(times, mod_flux-fluxes, 'ko', markersize = 0.75)
    plt.tight_layout()
    # plt.savefig(folder + 'LC_p_binary_model.png')
    # np.savetxt(folder+ 'binary_model.txt', np.array([times, mod_flux]).T)
    # np.savetxt(folder+ 'residuals.txt', np.array([times, np.array(mod_flux)- np.array(fluxes)]).T)
    plt.show()
    plt.close()

plt.rcParams.update({'font.size': 20.0, 'xtick.labelsize': 'x-small'})
#print(plt.rcParams)
phoebe_controller.initialize_phoebe(mpi_ncores=16, logger=False)

endurance_dir = 'endurance/'
#remove_binary_signal(endurance_dir + 'iteration2/', endurance_dir, 'result_iteration1.txt', exp_time = False)
remove_binary_signal(endurance_dir + 'Removed_Pulsations_from_first_run_irrad_gravbol/', endurance_dir,  'Removed_Pulsations_from_first_run.txt', exp_time = True)


from datetime import datetime
import logging

class CommonFunction:

    @staticmethod
    def datetimediff(found_lineL,found_lineL1):
        try:
            ab = found_lineL.split()
            cd = found_lineL1.split()
            edt0 = ab[1][1:19]
            sdt0 = cd[1][1:19]

            edt = edt0.replace('T', ' ')
            sdt = sdt0.replace('T', ' ')

            d1 = datetime.strptime(edt, "%Y-%m-%d %H:%M:%S")
            d2 = datetime.strptime(sdt, "%Y-%m-%d %H:%M:%S")
            diff0 = d1 - d2
            diff = diff0.days
            return diff
        except:
            print('function not working')


# Projection Matrix Stencil Code
# Written by Eleanor Tursman, based on previous work by Henry Hu,
# Grady Williams, and James Hays for CSCI 1430 @ Brown and
# CS 4495/6476 @ Georgia Tech

import numpy as np
from sympy import Point3D
import cv2
import numpy as np
import os
import glob
from helpers import plot3dview, plot3dview2

def calculate_projection_matrix(Points_2D, Points_3D):

    print('Randomly setting matrix entries as a placeholder')
    M = np.array([[0.1768, 0.7018, 0.7948, 0.4613],
                  [0.6750, 0.3152, 0.1136, 0.0480],
                  [0.1020, 0.1725, 0.7244, 0.9932]])
    ##################
    #Solving using least square  AU=B
    #dimensons :
    # A->  (N*2,12)  |||| B->(N*2,1)   ||| U -> (12,1) to be rehsaped
    print(Points_2D)
    N=Points_3D.shape[0]          ## N
    A=np.zeros((N*2,11))
    B=np.zeros((N*2,1))
    i=0                     ## counter to iterate over marix
    #constructing A
    for j in range (N):
        ##buiding x row
        A[i,0:3]=Points_3D[j,:]   #first 3 elemnts in each row of A
        A[i,3]=1                  #fourth elemet in A
        last_elemnts=Points_2D[j,0]*Points_3D[j,:]  ##last 3 elements of each x row
        A[i,8:11]=-1*last_elemnts
        #A[i,11]=-1*Points_2D[j,0]

        ##Building y row
        A[i+1,4:7]=Points_3D[j,:]   #first 3 elemnts in each row of A
        A[i+1,7]=1                  #fourth elemet in A
        last_elemnts=Points_2D[j,1]*Points_3D[j,:]  ##last 3 elements of each x row
        A[i+1,8:11]=-1*last_elemnts
        #A[i+1,11]=-Points_2D[j,1]
        i+=2
    #print(A)
    B=np.reshape(Points_2D,(-1,1))
    AT_A=np.matmul(A.T,A )  # A^T *A
    inverse =np.linalg.inv(AT_A)
    AT_B=np.matmul(A.T,B)
    U=np.matmul(inverse,AT_B)
    U=np.append(U,1)
    M=np.reshape(U,(3,4))
    return M

# Returns the camera center matrix for a given projection matrix
# 'M' is the 3x4 projection matrix
# 'Center' is the 1x3 matrix of camera center location in world coordinates
def compute_camera_center(M):

    Center = np.array([1, 1, 1])
    Q=M[:,0:3]
    m4=M[:,3]
    Center=np.matmul(-1*np.linalg.inv(Q),m4)

    return Center

   ## Calibrate Camera function
def CameraCalibrate(o,ffname):

    ##inputs is :
    #1- o : number of image of wanted projection matrix
    #2- ffname : name of the folder containing the images
    print("Calibrating Camera ....")
    #Defining the dimensions of checkerboard
    CHECKERBOARD = (6,8)
    criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001)

    # Creating vector to store vectors of 3D points for each checkerboard image
    objpoints = []
    # Creating vector to store vectors of 2D points for each checkerboard image
    imgpoints = []

    # Defining the world coordinates for 3D points
    objp = np.zeros((1, CHECKERBOARD[0]*CHECKERBOARD[1], 3), np.float32)
    objp[0,:,:2] = np.mgrid[0:CHECKERBOARD[0], 0:CHECKERBOARD[1]].T.reshape(-1, 2)
    prev_img_shape = None

    # Extracting path of individual image stored in a given directory
    path='./'+ffname+'/*.jpg'
    images = glob.glob(path)

    #p#rint(images)
    p=0
    for fname in images:
        #print(fname)
        img = cv2.imread(fname)
        gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
        # Find the chess board corners
        # If desired number of corners are found in the image then ret = true
        ret, corners = cv2.findChessboardCorners(gray, CHECKERBOARD,cv2.CALIB_CB_ADAPTIVE_THRESH+
            cv2.CALIB_CB_FAST_CHECK+cv2.CALIB_CB_NORMALIZE_IMAGE)
       # print(ret)
        """
        If desired number of corner are detected,
        we refine the pixel coordinates and display
        them on the images of checker board
        """
        if ret == True:
            objpoints.append(objp)
            # refining pixel coordinates for given 2d points.
            corners2 = cv2.cornerSubPix(gray,corners,(11,11),(-1,-1),criteria)

            imgpoints.append(corners2)
            if p==o:
                points_3d= np.array(objp).reshape(-1,3)
                points_2d=np.array(corners2).reshape(-1,2)

            # Draw and display the corners
            img = cv2.drawChessboardCorners(img, CHECKERBOARD, corners2,ret)
            p+=1

    cv2.destroyAllWindows()
    ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(objpoints, imgpoints, gray.shape[::-1],None,None)
    print("Reprojection Error: \n",ret)
    Rotaion_matrix = (cv2.Rodrigues(rvecs[o]))[0]

    translation_vector = tvecs[o]
    ##conctaneting rotaion atrix and translaton vector
    Rotaion_translation = np.concatenate([Rotaion_matrix,translation_vector], axis=1) # [R|t]
    ##Calculating projection Matrix
    projection_matrix= np.dot(mtx,Rotaion_translation) # A[R|t]
    print("Done Calibrating.....")
    return projection_matrix,points_3d,points_2d,objpoints,imgpoints,mtx,dist,rvecs, tvecs,gray

def part_3 (folderName,E,f1,f2,M2,M3) :

    ##inputs
    #foldername containing the images
    #essiential matrix
    #f1: camera matrix 1
    #f2 : camera matrix 2
    # m2,m3 :projecton matrix of the two cameras
    print("part3 start....  ")
    path='./'+folderName+'/*.jpg'
   # path='./hh/*.jpg'
    images = glob.glob(path)
    i=0

    for fname in images:
        img = cv2.imread(fname)
        # convert to greyscale
        gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
        sift = cv2.SIFT_create()
        keypoints, descriptors = sift.detectAndCompute(img, None)
        #print(np.array(keypoints).shape)

        #print(int(str(keypoints[0][1]),16))
        if i==0:
            k1=keypoints
            d1=descriptors
            img1=img
            i+=1
        else:
             k2=keypoints
             d2=descriptors

        sift_image = cv2.drawKeypoints(gray, keypoints, img)
        # show the image
        cv2.imshow('image', sift_image)
        # save the image
       # cv2.imwrite("table-sift.jpg", sift_image)
        cv2.waitKey(0)
        cv2.destroyAllWindows()
            #sift detect

  #sift detect
    bf = cv2.BFMatcher()
    # FLANN parameters
    FLANN_INDEX_KDTREE = 0
    index_params = dict(algorithm = FLANN_INDEX_KDTREE, trees = 5)
    search_params = dict(checks=50)

    flann = cv2.FlannBasedMatcher(index_params,search_params)
    matches = flann.knnMatch(d1,d2,k=2)
    #matches = bf.knnMatch(d1,d2,k=2)
    #print((np.array(matches)).shape)
    #matchesarray=np.array(matches)
    #epipolar_matches=np.zeros((matchesarray.shape))
    #bf=cv2.BFMatcher()
    #matches=bf.match(d1,d2)
    #matches=sorted(matches, key= lambda x:x.distance)

    # Initialize lists
    #list_kp1 = []
    #list_kp2 = []

    # For each match...
    #for mat in matches:

        # Get the matching keypoints for each of the images
      #  img1_idx = mat.queryIdx
       # img2_idx = mat.trainIdx

        # x - columns
        # y - rows
        # Get the coordinates
       # (x1, y1) = k1[img1_idx].pt
        #(x2, y2) = k2[img2_idx].pt

        # Append to each list
        #print(x1,y1)
       # list_kp1.append((x1, y1,f1))
        #list_kp2.append((x2, y2,f2))

    #c1_co=np.array(list_kp1).reshape(-1,3)
    #c2_co=np.array(list_kp2).reshape(-1,3)

    #checking the epolar constrain
  #  for j in range (c1_co.shape[0]):
   #     con=np.matmul(c2_co[0,:].T,E)
    #    consrain=np.matmul(con,c1_co[0,:])
     #   if consrain==0:
      #      print("Match!!!")

    #print(f1,f2)
    #E=E/np.linalg.norm(E)
    m1=np.linalg.inv(f1)
    m2=np.linalg.inv(f2)
    img1_points=[]
    img2_points=[]
    good = []
    for m ,n in matches:
        pts1=np.zeros((1,3))
        pts2=np.zeros((1,3))
        (x1, y1) = k2[m.trainIdx].pt
        (x2, y2) = k1[m.queryIdx].pt
        img1_points.append([x1,y1])
        img2_points.append([x2,y2])
       # print(k2[m.trainIdx].pt)
        pts2=np.array([x2,y2,1])

        #pts2.append((f2))
        pts1=np.array([x1,y1,1])
        ##Normalizing the two points
        v2=np.matmul(m2,pts1)
        v1=np.matmul(m1,pts2)

        ##Epipolar cnstrains
        con=np.matmul(v2.T,E)
        consrain=np.matmul(con,v1)
        ##threshold to detecet if epipolar constraint is met
        if (consrain>-.2 and consrain<.2):
            print("Match!!!")
            good.append([m])

    img1_points=np.array(img1_points).reshape(-1,2)
    img2_points=np.array(img2_points).reshape(-1,2)
    img3 = cv2.drawMatchesKnn(img1,k1,img,k2,good,None,flags=cv2.DrawMatchesFlags_NOT_DRAW_SINGLE_POINTS)
    cv2.imshow('image', img3)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

    ##Reconstruction and Visualization

    points=cv2.triangulatePoints(M3,M2,img1_points.T,img2_points.T).T
    points = points[:, :3] / points[:, 3:]
    plot3dview2(points)


import numpy as np
import cv2

cap = cv2.VideoCapture(0) # paraméter: 0 -> a számítógép saját kameráját fogja érzékelni
cap.set(3,640) # szélesség
cap.set(4,480) # magasság
while(True):
    ret, frame = cap.read()

    cv2.imshow('frame', frame)

    k = cv2.waitKey(30) & 0xff
    if k == 27: # nyomd le az 'ESC' gombot a kilépéshez
        break
cap.release()
cv2.destroyAllWindows()


# -*- coding: utf-8 -*-
# @Time : 9/7/21 5:13 PM
# @Author : Tingfeng Li, <tl601@cs.rutgers.edu>, Rutgers University.

import torch
import numpy as np
from torch import Tensor
from util.anchors import generate_anchors

def sample(logits):
    return torch.multinomial(logits, 1)

def box_area(boxes: Tensor) -> Tensor:
    """
    Computes the area of a set of bounding boxes, which are specified by its
    (x1, y1, x2, y2) coordinates.
    Arguments:
        boxes (Tensor[N, 4]): boxes for which the area will be computed. They
            are expected to be in (x1, y1, x2, y2) format
    Returns:
        area (Tensor[N]): area for each box
    """
    return (boxes[:, 2] - boxes[:, 0]) * (boxes[:, 3] - boxes[:, 1])

# implementation from https://github.com/kuangliu/torchcv/blob/master/torchcv/utils/box.py
# with slight modifications
def box_iou(boxes1: Tensor, boxes2: Tensor) -> Tensor:
    """
    Return intersection-over-union (Jaccard index) of boxes.
    Both sets of boxes are expected to be in (x1, y1, x2, y2) format.
    Arguments:
        boxes1 (Tensor[N, 4])
        boxes2 (Tensor[M, 4])
    Returns:
        iou (Tensor[N, M]): the NxM matrix containing the pairwise IoU values for every element in boxes1 and boxes2
    """
    area1 = box_area(boxes1)
    area2 = box_area(boxes2)

    lt = torch.max(boxes1[:, None, :2], boxes2[:, :2])  # [N,M,2]
    rb = torch.min(boxes1[:, None, 2:], boxes2[:, 2:])  # [N,M,2]

    wh = (rb - lt).clamp(min=0)  # [N,M,2]
    inter = wh[:, :, 0] * wh[:, :, 1]  # [N,M]

    iou = inter / (area1[:, None] + area2 - inter)
    return iou

def _filter_boxes(boxes, min_size):
    """Remove all boxes with any side smaller than min_size."""
    ws = boxes[:, 2] - boxes[:, 0] + 1
    hs = boxes[:, 3] - boxes[:, 1] + 1
    keep = np.where((ws >= min_size) & (hs >= min_size))[0]
    return keep

def generate_boxes(base_size=28, min_box_side=21, feat_height=21, feat_width=21, img_size=84,
                   feat_stride=4, ratios=[0.5, 1.0, 2.0],
                   scales=np.array([1.0, 2., 2.5])):
    a = generate_anchors(base_size=base_size, ratios=ratios,
                         scales=scales)
    #print(a)
    #feat_height, feat_width = 21, 21 # 81/4
    shift_x = np.arange(0, feat_width) * feat_stride
    shift_y = np.arange(0, feat_height) * feat_stride
    shift_x, shift_y = np.meshgrid(shift_x, shift_y)
    shifts = torch.from_numpy(np.vstack((shift_x.ravel(), shift_y.ravel(),
                                         shift_x.ravel(), shift_y.ravel())).transpose())
    shifts = shifts.contiguous().float()
    #print('shifts ', shifts)

    A = a.shape[0]
    K = shifts.size(0)

    _anchors = torch.from_numpy(a)
    # anchors = self._anchors.view(1, A, 4) + shifts.view(1, K, 4).permute(1, 0, 2).contiguous()
    anchors = _anchors.view(1, A, 4) + shifts.view(K, 1, 4)
    #print('anchors ', anchors)
    anchors = anchors.view(K * A, 4)
    anchors = torch.clip(anchors, 0, img_size-1).float()

    # filter small boxes
    keep = _filter_boxes(anchors, min_size=min_box_side)
    anchors = anchors[keep]
    return anchors

def convert_image_np(inp, norm=True):
    """Convert a Tensor to numpy image."""
    inp = inp.numpy().transpose((1, 2, 0))
    if norm:
        mean = np.array([0.485, 0.456, 0.406])
        std = np.array([0.229, 0.224, 0.225])
        inp = std * inp + mean
    inp = np.clip(inp, 0, 1)
    return inp

def calculate_correct(diff_feat, diff_iou):
    mask = (torch.sign(diff_feat) == torch.sign(diff_iou))
    correct = mask.sum().item()
    return correct


import cv2
import numpy as np
import paho.mqtt.client as paho
import paho.mqtt as mqtt
import time
import sys

from merging import initiateBlob, continueBlob, doneBlob
from utils.easy import *

def on_message(client, userdata, msg):
    global flrmap
    global shoppers
    global filtr
    global data

    no_of_cams = 2

    if msg.topic == "strongSort/201" and len(data) == 0: #and len(data) == 0
            trackerA = operation(msg, proj_mat_201)
            data.append(trackerA)
            # data.append()
            # print("data1:     ", trackerA)

    if msg.topic == "strongSort/208" and len(data) == 1: #and len(data) == 1
            trackerB = operation(msg, proj_mat_208)
            data.append(trackerB)
            # print("data2:     ", trackerB)

    if len(data)==no_of_cams:
        # print(f'The data is {data}')
        camMapClone = flrmap.copy()

        if filtr and instanceCheck(data):
            # shoppers = initiateBlob(data)
            # shoppers = doneBlob(shoppers)
            # filtr = False

            shoppers = initiateBlob(data)
            shoppers = doneBlob(shoppers)
            filtr = False

        elif not(filtr) and instanceCheck(data):
            for val in data:
                if isinstance(val, np.ndarray):
                    shoppers = continueBlob(val, shoppers)
            shoppers = doneBlob(shoppers)

        camMapClone = drawShoppers(camMapClone, shoppers)
        # cv2.imshow("SmoothShop", camMapClone)
        # cv2.waitKey(50)
        result.write(camMapClone)
        data.clear()

class PahoMqtt():
    def __init__(self, client_name) -> None:
        self.client = paho.Client(client_id = client_name, userdata=None, protocol=paho.MQTTv5)
        self.client.on_connect = on_connect

        self.client.tls_set(tls_version=mqtt.client.ssl.PROTOCOL_TLS)
        self.client.username_pw_set("bumpeet", "Chetan@123")
        self.client.connect("0f1ffe2b526445fcb8f7309446340bd9.s2.eu.hivemq.cloud", 8883)

        self.client.on_subscribe = on_subscribe

        sub_list = [("strongSort/201", 0), ("strongSort/208", 0)]

        self.client.on_message = on_message
        self.client.subscribe(sub_list)

def check(data):
    global flrmap
    global filtr
    global shoppers

    if len(data)==2:
        print(f'The data is {data}')
        camMapClone = flrmap.copy()

        if filtr and instanceCheck(data):
            shoppers = initiateBlob(data)
            shoppers = doneBlob(shoppers)
            filtr = False

        elif not(filtr) and isinstance(data[0], np.ndarray) and isinstance(data[1], np.ndarray):
            for i in data:
                shoppers = continueBlob(i, shoppers)
            shoppers = doneBlob(shoppers)

        camMapClone = drawShoppers(camMapClone, shoppers)
        cv2.imshow("SmoothShop", camMapClone)
        cv2.waitKey(50)

def main(filePath: str):
    global flrmap
    global proj_mat_201
    global proj_mat_208

    proj_mat, cam_pos = load_mat(filePath)
    proj_mat_201, proj_mat_208 = proj_mat[0], proj_mat[1]

    for i, vecPx in enumerate(cam_pos):
        cv2.rectangle(flrmap, (vecPx[0]-20, vecPx[1]-20), (vecPx[0]+20, vecPx[1]+20), (255, 0, 0))
        cv2.putText(flrmap, f'cam_{i+1}', (vecPx[0], vecPx[1]), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 1, cv2.LINE_AA)

    clnt = PahoMqtt("reciever")
    clnt.client.loop_forever()

def verify(filePath: str):
    global flrmap
    global proj_mat_201
    global proj_mat_208
    global filtr
    global shoppers

    proj_mat, cam_pos = load_mat(filePath)
    proj_mat_201, proj_mat_208 = proj_mat[0], proj_mat[1]

    for vecPx in cam_pos:
        cv2.rectangle(flrmap, (vecPx[0]-20,vecPx[1]-20), (vecPx[0]+20, vecPx[1]+20), (255, 0, 0))

    n = 100

    np.random.seed(32)
    data = np.random.randint(60, 400, (n, 2, 2, 4))
    # ids = np.random.randint(0, 10, (n, 2))
    ids1 = np.zeros((100,))
    ids2 = np.ones((100,))
    ids3 = np.full((100,), 10)
    ids4 = np.full((100,), 20)

    # data[:, 0, :, 2], data[:, 1, :, 2] = np.resize(ids[:, 0], (n, 1)) , np.resize(ids[:, 1], (n, 1))
    data[:, 0, 0, 2], data[:, 0, 1, 2] = ids1, ids2
    data[:, 1, 0, 2], data[:, 1, 1, 2] = ids3, ids4

    data[:, 0, :, 3] = np.full((100,2), 201 )
    data[:, 1, :, 3] = np.full((100,2), 208 )

    for i, val in enumerate(data):
         check([val[0], val[1]])
         time.sleep(0.05)

if __name__=="__main__":

    # sys.stdout = open(r"D:\Chetan\Documents\python_scripts\smoothShop\goat.txt", "w")
    global flrmap
    global proj_mat_201
    global proj_mat_208
    global filtr
    global shoppers
    global data
    global write

    filtr = True
    shoppers = []
    data = []

    flrmap = cv2.imread(r"images\\floorMap.png")
    filePath = r"D:\Chetan\Documents\python_scripts\smoothShop\files"

    result = cv2.VideoWriter('filename.avi',
                        cv2.VideoWriter_fourcc(*'MJPG'),
                        5, (flrmap.shape[1], flrmap.shape[0]))

    main(filePath)
    # verify(filePath)
    cv2.destroyAllWindows()


import sys

import numpy as np
import pygame

ROW_COUNT = 6
COLUMN_COUNT = 7

def create_board():
    board = np.zeros((6, 7))
    return board

def drop_piece(board, row, col, piece):
    board[row][col] = piece

def is_valid_location(board, col):
    # if this condition is true we will let the use drop piece here.
    # if not true that means the col is not vacant
    return board[5][col] == 0

def get_next_open_row(board, col):
    for r in range(ROW_COUNT):
        if board[r][col] == 0:
            return r

def print_board(board):
    print(np.flip(board, 0))

def winning_move(board, piece):
    # Check horizontal locations for win
    for c in range(COLUMN_COUNT - 3):
        for r in range(ROW_COUNT):
            if board[r][c] == piece and board[r][c + 1] == piece and board[r][c + 2] == piece and \
                    board[r][c + 3] == piece:
                return True

    # Check vertical locations for win
    for c in range(COLUMN_COUNT):
        for r in range(ROW_COUNT - 3):
            if board[r][c] == piece and board[r + 1][c] == piece and board[r + 2][c] == piece and \
                    board[r + 3][c] == piece:
                return True

    # Check positively sloped diaganols
    for c in range(COLUMN_COUNT - 3):
        for r in range(ROW_COUNT - 3):
            if board[r][c] == piece and board[r + 1][c + 1] == piece and board[r + 2][c + 2] == piece and \
                    board[r + 3][c + 3] == piece:
                return True

    # Check negatively sloped diaganols
    for c in range(COLUMN_COUNT - 3):
        for r in range(3, ROW_COUNT):
            if board[r][c] == piece and board[r - 1][c + 1] == piece and board[r - 2][c + 2] == piece and \
                    board[r - 3][c + 3] == piece:
                return True
def draw_board(board):
    pass

board = create_board()
print_board(board)
game_over = False
turn = 0
pygame.init()
SQUARESIZE = 100
width = COLUMN_COUNT * SQUARESIZE
height = (ROW_COUNT + 1) * SQUARESIZE
size = (width, height)
screen = pygame.display.set_mode(size)
pygame.display.update()

while not game_over:
    for event in pygame.event.get():
        if event.type == pygame.quit():
            sys.exit()
        if event.type == pygame.MOUSEBUTTONDOWN:
            # Ask for player 1 input
            # if turn == 0:
            #     col = int(input("Player 1, Make your Selection(0-6):"))
            #     # Player 1 will drop a piece on the board
            #     if is_valid_location(board, col):
            #         row = get_next_open_row(board, col)
            #         drop_piece(board, row, col, 1)
            #         if winning_move(board, 1):
            #             print("Player 1 wins")
            #             game_over = True
            #
            # # Ask for player 2 input
            # else:
            #     col = int(input("Player 2, Make your Selection(0-6):"))
            #     # Player 2 will drop a piece on the board
            #     if is_valid_location(board, col):
            #         row = get_next_open_row(board, col)
            #         drop_piece(board, row, col, 2)
            #         if winning_move(board, 2):
            #             print("Player 2 wins")
                        #game_over = True

            print_board(board)

            turn += 1
            turn = turn % 2


import os
import numpy as np
import pandas as pd

import matplotlib.pyplot as plt
import seaborn as sns
import cv2

from tqdm import tqdm

from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score, roc_auc_score, confusion_matrix, classification_report
from sklearn.preprocessing import LabelEncoder

import tensorflow as tf
from tensorflow.keras.utils import to_categorical
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Conv2D, MaxPooling2D
from tensorflow.keras.layers import Flatten, Dropout, BatchNormalization
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.callbacks import ReduceLROnPlateau
from tensorflow.keras.preprocessing.image import ImageDataGenerator

import warnings; warnings.filterwarnings("ignore")

basePath = r"E:\python\flowers" #파일 가져오기
foldList = os.listdir(basePath)

flowerDict = dict(filepath=[], filename=[], label=[])
for folder in foldList:
    # print("\nfolder:", folder)
    foldPath = os.path.join(basePath, folder)
    fileName = os.listdir(foldPath)
    # print(f"file numbers:{len(fileName)} in {folder} folder")
    for file in fileName:
        filePath = os.path.join(foldPath, file)
        flowerDict["filepath"].append(filePath)
        flowerDict["filename"].append(file)
        flowerDict["label"].append(folder)
df = pd.DataFrame(flowerDict)
# print("df.shape:", df.shape)
# print(df.head())

imgsize = 150 #array로 변환하여 X, y로 분리
X = []
y = df["label"].values.tolist()
for fpath in tqdm(df["filepath"]):
    imgArray = cv2.imread(fpath, cv2.IMREAD_COLOR)
    imgArray = cv2.resize(imgArray, (imgsize, imgsize))
    X.append(imgArray)
# print(f"X length: {len(X)}")
# print(f"y length: {len(y)}")

#random 이미지 그리기
# fig = plt.figure(figsize=(10, 10))
# for i in range(6):
#     ranNum = np.random.randint(0, len(y))
#     plt.subplot(3, 2, i+1)
#     plt.imshow(X[ranNum])
#     plt.title(y[ranNum], size=15)
# plt.show()
# plt.tight_layout()

y = LabelEncoder().fit_transform(y) #labelencoder
y = to_categorical(y, 5) #onehot
X = np.array(X)
X = X/255.0 #scaling
print(f"X shape: {X.shape}")

x_train, x_test, y_train, y_test = train_test_split(X, y, test_size=.2, random_state=42) #split data
# print(f"x_train shape: {x_train.shape}")
# print(f"x_test shape: {x_test.shape}")
# print(f"y_train shape: {y_train.shape}")
# print(f"y_test shape: {y_test.shape}")

model = Sequential() #model 생성

model.add(Conv2D(filters = 32, kernel_size = (5,5),padding = 'Same',activation ='relu', input_shape = (150,150,3)))
model.add(MaxPooling2D(pool_size=(2,2)))

model.add(Conv2D(filters = 64, kernel_size = (3,3),padding = 'Same',activation ='relu'))
model.add(MaxPooling2D(pool_size=(2,2), strides=(2,2)))

model.add(Conv2D(filters =96, kernel_size = (3,3),padding = 'Same',activation ='relu'))
model.add(MaxPooling2D(pool_size=(2,2), strides=(2,2)))

model.add(Conv2D(filters = 96, kernel_size = (3,3),padding = 'Same',activation ='relu'))
model.add(MaxPooling2D(pool_size=(2,2), strides=(2,2)))

model.add(Flatten())
model.add(Dense(512))
model.add(Activation('relu'))
model.add(Dense(5, activation = "softmax"))

model.compile(optimizer=Adam(learning_rate=0.001),loss='categorical_crossentropy',metrics=['accuracy'])

redLr = ReduceLROnPlateau(monitor="val_acc", patience=3, verbose=1, factor=0.1) #과적합 예방 조기종료
dataGen = ImageDataGenerator(
    featurewise_center=False,
    samplewise_center=False,
    featurewise_std_normalization=False,
    samplewise_std_normalization=False,
    zca_whitening=False,
    rotation_range=10,
    zoom_range = 0.1,
    width_shift_range=0.2,
    height_shift_range=0.2,
    horizontal_flip=True,
    vertical_flip=False) #과적합 예방 데이터증강
dataGen.fit(x_train)

batch_size=128
epochs=50
history = model.fit_generator(
    dataGen.flow(x_train, y_train, batch_size=batch_size),
    epochs = epochs, validation_data=(x_test, y_test),
    verbose=1, steps_per_epoch=x_train.shape[0] //batch_size)#학습

plt.plot(history.history["accuracy"], label="train")
plt.plot(history.history["val_accuracy"], label="test")
plt.title("Model accuracy")
plt.ylabel("accuracy", fontsize=10)
plt.xlabel("epochs", fontsize=10)
plt.legend()
plt.show()

CLASSES = ["daisy", "dandelion", "rose", "sunflower", "tulip"]
pred = model.predict(x_test) #예측
pred_num = np.argmax(pred, axis=1) #예측 한 레이블
invy_test = np.argmax(y_test, axis=1) #y_test 레이블

confMat = confusion_matrix(invy_test, pred_num) #confusionMatrix

plt.figure(figsize=(8, 6))
sns.heatmap(confMat, cmap="Greens", annot=True, fmt="d", xticklabels=CLASSES, yticklabels=CLASSES, cbar=True, cbar_kws={"shrink": .5})
plt.title("Flower detection Confusion Matrix", size=20)
plt.xlabel("prediction")
plt.ylabel("actual")
plt.show()


import datetime

class Weather():
    def __init__(self,city,temp,visibility,pressure,temp_min,temp_max,humidity,sunrise,sunset,description):
        self.city = city # thanh pho
        self.temp = temp # nhiet do
        self.temp_min = temp_min
        self.temp_max = temp_max
        self.visibility = visibility # tam nhin xa
        self.pressure = pressure # ap suat khong khi
        self.humidity = humidity # độ ẩm
        self.sunrise = datetime.datetime.fromtimestamp(sunrise).strftime("%H:%M") # bình minh or mặt trời mọc
        self.sunset = datetime.datetime.fromtimestamp(sunset).strftime("%H:%M") # hoàng hôn or mặt trời lặn
        self.description = description # mô tả thời tiết

    def out(self):
        return f"""{self.city} hôm nay nhiệt độ là {self.temp} độ C
        trời hôm nay {self.description}
        tầm nhìn xa {int(self.visibility/1000)} km
        áp suất không khi {self.pressure}
        độ ẩm {self.humidity} %
        bình minh vào {self.sunrise}, hoàng hôn vào {self.sunset}
        """


import os
from time import time
from datetime import datetime
import numpy as np
import scipy.stats
import pandas as pd
import pandas_datareader as pdd
import yfinance
import matplotlib.pyplot as plt
import seaborn as sns
from itertools import product
from tqdm import tqdm
from sklearn.impute import KNNImputer

rnd=np.random.RandomState(0)
pd.options.display.precision=5
plt.rcParams['figure.figsize']=[12,5]
sns.set_theme(style="whitegrid",palette="bright",font="monospace")

intern={
"ci":"CPIAUCSL",
"pi":"PPIACO",
"ii":"PCEPILFE",
"pic":"PI",
"ahe":"CES0500000003",
"hi":"CSUSHPISA",
"cb":"BAMLH0A0HYM2",
"fs":"STLFSI4",
"ic":"ICSA",
"pr":"PAYEMS",
"fr":"DFF",
"nk":"NIKKEI225",
"fert":"PCU325311325311",
"iy":"DFII10",
"us02y":"DGS2",
"us10y":"DGS10",
"us30y":"DGS30",
"ur":"UNRATE",
"adp":"ADPMNUSNERSA",
"cci":"CPILFESL",
"ie":"T5YIE",
"djia":"DJIA",
"rs":"RSXFS",
"gdp":"GDPC1"
}
extern={
"zs":"ZS=F",
"zc":"ZC=F",
"zw":"ZW=F",
"hg":"HG=F",
"si":"SI=F",
"ng":"NG=F",
"cl":"CL=F",
"uj":"JPY=X",
"uy":"CNY=X",
"ue":"EURUSD=X",
"hs":"^HSI",
"ks":"^KS11",
"vn":"VNM",
"sp":"^GSPC",
"ndx":"^IXIC",
"us10y":"^TNX"
}

class t:
    def __init__(self):
        self.t0_datetime=datetime.now().strftime("%m-%d %H:%M")
        self.t0=time()
        print(f"{self.t0_datetime} ({self.t0})")
    def __repr__(self):
        t0=self.t0
        t1=time()
        t1_in=datetime.now().strftime("%m-%d %H:%M")
        return f"{self.t0_datetime} ---> {t1_in} ({round((t1-t0)/60,1)})"

def ntfys(series:pd.DataFrame,target:list=["sp","ks","cb"])->pd.DataFrame:
    ntfys=series["us02y"]-series["fr"]
    ntfys.name="ntfys"
    s_=pd.concat([series[target],ntfys],axis=1)
    return s_

def v_range(series:pd.DataFrame,v:float,ticker:str="ks",target:str="sp",mod=.015):
    s_=series[[ticker,target]].loc["2020":].dropna()
    s_=s_[(s_[target] >= v * (1-mod))&(s_[target] <= v * (1+mod))][ticker]
    min_=s_.min()
    mid_=s_.mean()
    max_=s_.max()
    std_=s_.std()
    return [round(q) for q in (min_,mid_,max_,std_)]

def theta(v:float)->float: # results intraday decaying per minute
    decayingPerHour=(v/(6.5*60))*10
    return round(decayingPerHour,2)

def rtn_(ticker:str,f:pd.DataFrame,days:int=20)->float:
    rtn=round((f[ticker].dropna().pct_change()*100)[-1*days:].std(),2)
    print(rtn,"%")
    return rtn

def apnd(path:str)->pd.DataFrame:
	if not path.endswith("/"):path+="/"
	return pd.concat([pd.read_csv(f"{path}{q.name}") for q in os.scandir(path) if ".csv" in q.name],axis=0)

def index_full_range(f:pd.DataFrame):
    return pd.DataFrame(index=pd.date_range(f.index.min(),f.index.max(),freq="D"))

def getdata(days_visit=100,save=False):
    f=pd.read_csv("c:/code/f.csv",index_col="date",converters={"date":pd.to_datetime})

    renew_data_end=pd.Timestamp("today").floor("d")

    if days_visit<0:
        renew_data_start=pd.Timestamp("1980-01-01")
        f=pd.DataFrame(index=pd.date_range("1980-01-01",renew_data_end,freq="D"))
    else:
        renew_data_start=f.index.max()-pd.Timedelta(days=days_visit)

    with open("c:/code/fred.key",encoding="utf-8-sig") as fredkey:
        fredkey=fredkey.readline()
    renew_data_fred=(pdd.DataReader(list(intern.values()),"fred",start=renew_data_start,end=renew_data_end,api_key=fredkey)
        .rename({q:w for w,q in intern.items()},axis=1))

    yfinance.pdr_override()
    renew_data_yahoo=(pdd.data.get_data_yahoo(list(extern.values()),start=renew_data_start,end=renew_data_end)
        .loc[:,"Close"]
        .rename({q:w for w,q in extern.items()},axis=1))

    renew_data=renew_data_fred.combine_first(renew_data_yahoo)

    if days_visit<0:
        f=renew_data
    else:
        f=f.reindex(pd.date_range(f.index.min(),renew_data_end,freq="D")).combine_first(renew_data)
        f.update(renew_data)
    f.index.name="date"

    print(f[["us02y","uj","ks","sp"]].tail(3))

    if save:
        f.to_csv("c:/code/f.csv")
        return f
    else:
        ask=input("input to save above: ")
        if ask in ("y","Y"):
            f.to_csv("c:/code/f.csv")

    return f

def mon(f:pd.DataFrame,start,stop)->np.ndarray:
    return np.asarray(sum([f.index.month==q for q in range(start,stop,1)]),dtype="bool")

def dtr(arr,o=1):
    # ii-a-i
    if any(np.isnan(arr)):
        raise ValueError(f"nan in the array")
    a=arr.copy()
    x=np.arange(len(a))
    q=np.polyval(np.polyfit(x,a,deg=o),x)
    a-=q
    return a

def arigeo(a)->tuple:
    # pp. 479
    a=a.dropna()
    mean_ari=np.mean(a)
    mean_geo=np.exp(np.mean(np.log(a)))
    median_ari=np.median(a)
    return (mean_ari,mean_geo,median_ari)

def distrb(f:pd.Series):
    # pp. 467
    trs={
    "noop":lambda x:x,
    "log" :np.log,
    "sqrt":np.sqrt,
    "cbrt":np.cbrt,
    "inv" :lambda x:-1/x,
    "sq"  :lambda x:x*x,
    "cb"  :lambda x:x*x*x,
    "diff":np.diff}
    deta_cont=f.dropna()
    deta_name=deta_cont.name
    fg,ax=plt.subplots(1,len(trs),figsize=(23,4),layout="constrained")
    fg.suptitle(f"transformations::{deta_name}")
    for q in enumerate(trs):
        ax[q[0]].hist(trs[q[1]](deta_cont),edgecolor="black")
        ax[q[0]].set_title(q[1])

# todo #
def deflator(f):
    fa=pd.concat([f.ci.ffill(),f.ie.interpolate().ffill()],axis=1).dropna()
    fb=fa.ci * (1+(fa.ie/100))
    return fb

def act(t,i=None):
    # if deflator is None, just calculate lzp
    if t.name=="ng" or t.name=="spr":
        a=t.dropna()
        a_=t.dropna()
    elif not i is None:
        a=pd.concat([t,i],axis=1).dropna()
        a_=a.iloc[:,0] # i
        a=a.iloc[:,0] / a.iloc[:,1] # i_
    else:
        a=t.dropna()
        a_=t.dropna()
    # using yeo-johnson
    if not int(np.sign(a).sum())==len(a):
        a_l=scipy.stats.yeojohnson(a)[0]
    else:
        if t.name=="zs":
            a_l=np.log(np.log(a))
        else:
            a_l=np.log(a)
    # de-ta
    a_ls   =scipy.stats.zscore(a_l)
    a_lsp  =scipy.stats.norm.cdf(a_ls,loc=a_ls.mean(),scale=a_ls.std(ddof=1))
    a_lsp_f=pd.Series(a_lsp,index=a.index)
    return pd.concat([a_,a,a_lsp_f],axis=1).set_axis([f"{t.name}",f"{t.name}_",f"{t.name}lp"],axis=1)

def nav(f:pd.DataFrame,i:str,v:float):
    rowidx=np.abs(f[f"{i}"]-v).argmin()
    colidx=f.columns.get_indexer([f"{i}"])[0]
    viewport=f.iloc[rowidx,colidx:colidx+2]
    cdf_i=viewport[1]
    print(f"{cdf_i*100:.5f}%")
    return viewport

def roll_pct(f,i,dur=5,start="2022",ax=None,figsize=(8,8),title=""):
    d=f[i].dropna().rolling(dur).mean().pct_change().iloc[1:]
    d_std=np.std(d)
    if ax is None:
        # creating plt canvas
        fg,ax=plt.subplots(figsize=figsize)
    else:
        # mutating input ax
        fg=False
    ax.plot(d[f"{start}":])
    ax.set_title(title)
    [ax.axhline(y=d_std*q,linestyle="dotted",alpha=.7,color="red") for q in (-2,-1,1,2)]
    return fg or None

def rng(f:pd.DataFrame,i:str,
        rng=(.05,3),test=False)->pd.DataFrame:
    f=f.copy()
    if not i in f.columns:
        raise NameError(f"{i} does not exist")
    col=f"{i}lzp"
    if test:
        rng=np.delete(
            np.round(np.flip(
            np.geomspace(rng[0],1,rng[1])),2),2)
    else:
        rng=np.round(np.flip(
            np.percentile(f[col].dropna(),(2,15,30,100))),2)
    f.loc[:,f"{col}rng"]=None
    for q in range(len(rng)):
        f.loc[:,f"{col}rng"]=np.where(
        (~pd.isna(f[col])) & (f[col]<=rng[q]),
        rng[q],
        f[f"{col}rng"])
    f_=f.loc[:,f"{col}rng"].astype("category").copy()
    f.update(f_)
    return f

def ns(f:pd.DataFrame,x:str,y:str):
    f=f[[x,y]].dropna()
    rowidx=np.amin((f.count()[x],f.count()[y]))
    return f[f.shape[0]-rowidx:]

def cx(f:pd.DataFrame,x:str,y:str,d=180,
       normed=True,save=True,detrend=None,test=False):
    # pp. 261
    f=ns(f,y,x)
    freq=f.index.freq.freqstr
    if detrend is None:
        detrend=lambda q:q
    if save:
        plt.figure(figsize=(22,14))
        xc=plt.xcorr(f[y],f[x],
            detrend=detrend,maxlags=d,normed=normed)
        plt.suptitle(f"{freq} {y},{x},{d}")
        plt.savefig(f"e:/capt/{freq}_{y}_{x}_{d}.png")
        plt.cla(),plt.clf(),plt.close()
        idx=abs(xc[1]).argmax()
        return xc[0][idx],xc[1][idx]
    fg,ax=plt.subplots(1,2)
    ac=ax[0].acorr(f[x],
        detrend=detrend,maxlags=d)
    xc=ax[1].xcorr(f[y],f[x],
        detrend=detrend,maxlags=d,normed=normed)
    fg.suptitle(f"{freq}_{freq} {y},{x},{d}")
    ac_=ac[0][abs(ac[1]).argmax()]
    xc_=xc[0][abs(xc[1]).argmax()]
    if test:
        return (ac[0],ac[1]),(xc[0],xc[1])
    return ac_,xc_

def cx_(f:pd.DataFrame,x,d=12):
    f=f[x]
    cache=[]
    for col in tqdm(list(product(x,x)),desc=f"cx-rel"):
        if not col[0] is col[1]:
            rslt=cx(f[[col[0],col[1]]],
                col[0],col[1],d=d,save=True)
            cache.append((col[0],col[1],rslt[0],np.round(rslt[1],2)))
    rslt=(pd.DataFrame(cache,columns=["x","y","dur","coef"])
            .sort_values(by="coef",ascending=False)
            .set_index(keys=["x","y"])
            .sort_index())
    return rslt

def impt(f:pd.DataFrame,x,n=10)->pd.DataFrame:
    return (pd.DataFrame(
        KNNImputer(n_neighbors=n,weights="distance").fit_transform(f),
            index=f.index,columns=x))

# innermost visualisations
def hm(f,mm=(-1,1),ax=None,cbar=False,title=None):
    # pp.201
    if title is None:
        title=f"{', '.join(f.columns)}"
    mask=np.triu(np.ones_like(f,dtype=bool))[1:,:-1]
    f=f.iloc[1:,:-1]
    cmap=sns.diverging_palette(250,10,as_cmap=True)
    if ax is None:
        plt.subplots(figsize=(22,20))
    plt.title(title)
    sns.heatmap(f,
        mask=mask,cmap=cmap,ax=ax,cbar=cbar,
        vmin=mm[0],vmax=mm[1],
        annot=True,center=0,square=True,linewidths=.5,fmt=".2f")

def hm_(f):
    if len(f.columns)>20:
        q=input(f"{len(f.columns)} columns:: ")
        if not q:
            raise ValueError(f"{len(f.columns)} is too much")
    _,ax=plt.subplots(1,3,figsize=(22,16))
    hm(f,
        title=f"",ax=ax[0])
    ax[0].title.set_text("org")
    hm(f.dropna(),
        title=f"",ax=ax[1])
    ax[1].title.set_text("dropna")
    hm(impt(f,f.columns),
        title=f"",ax=ax[2])
    ax[2].title.set_text("impt")

def pp(f,vars=None,l=False,hue=None):
    (sns.pairplot(data=f,vars=vars,hue=hue,
        dropna=False,kind="scatter",diag_kind="hist")
        .map_diag(sns.histplot,log_scale=l,
        multiple="stack",element="step"))

def vs(f,x:str,y:str):
    fg,ax=plt.subplots()
    ax0=ax.twinx()
    ax.plot(f.loc[:,x],label=x,color="red")
    ax0.plot(f.loc[:,y],label=y,color="blue")
    ax.set_ylabel(x)
    ax0.set_ylabel(y)
    h,l=ax.get_legend_handles_labels()
    fg.legend(h,(),loc="upper center")

def rp(f,x,y,
    hue=None,size=None,sizes=(20,200)):
    sns.relplot(data=f,x=x,y=y,hue=hue,size=size,sizes=sizes)


import os
from typing import Iterator

from utils.file_management.file_decompressor import FileDecompressor

class DirectoryIterator:
    @classmethod
    def expanded_subtree(cls, root_directory_path: os.PathLike) -> Iterator[os.PathLike]:
        visited_paths, unvisited_paths = set(), [root_directory_path]

        while unvisited_paths:
            directory_item_path = unvisited_paths.pop()
            if directory_item_path not in visited_paths:
                visited_paths.add(directory_item_path)

                if cls.__is_compressed_file(directory_item_path):
                    FileDecompressor.decompress_file(directory_item_path)
                    decompressed_directory_path, _ = os.path.splitext(directory_item_path)
                    directory_item_path = decompressed_directory_path

                if os.path.isdir(directory_item_path):
                    directory_items_paths = set(cls.subtree_first_level(directory_item_path))
                    directory_unvisited_paths = directory_items_paths - visited_paths
                    unvisited_paths.extend(directory_unvisited_paths)

                if os.path.isfile(directory_item_path):
                    yield directory_item_path

    @classmethod
    def subtree_first_level(cls, root_directory_path: os.PathLike | str) -> Iterator[os.PathLike | str]:
        directory_contents_basenames = os.listdir(root_directory_path)

        for directory_item_basename in directory_contents_basenames:
            directory_item_path = os.path.join(root_directory_path, directory_item_basename)

            yield directory_item_path

    @classmethod
    def __is_compressed_file(cls, item_path: os.PathLike | str) -> bool:
        if os.path.isfile(item_path):
            basename = os.path.basename(item_path)
            _, extension = os.path.splitext(basename)

            if extension == '.zip':
                return True

        return False


#!/usr/local/bin/python3.7
# -*- coding: utf-8 -*-
from typing import List, Callable

class MountainArray:
    def __init__(self, arr: List[int]):
        self.arr = arr

    def get(self, index: int) -> int:
        return self.arr[index]

    def length(self) -> int:
        return len(self.arr)

def findInMountainArray(target: int, mountain_arr: 'MountainArray') -> int:
    def findPeakIndex(arr: MountainArray):
        """返回峰顶索引"""
        length = arr.length()
        left, right = 0, length - 1
        while left < right:
            mid = left + (right - left) // 2
            if arr.get(mid) > arr.get(mid+1):
                right = mid
            else:
                left = mid + 1
        return left

    def findTargetIndex(arr: MountainArray, target: int, left: int, right: int, comp: Callable[[int, int], bool]):
        """二分求索引：数组升序&降序"""
        while left < right:
            mid = left + (right - left) // 2
            if comp(arr.get(mid), target):
                left = mid + 1
            else:
                right = mid
        return left if arr.get(left) == target else -1

    peakIndex = findPeakIndex(mountain_arr)
    if mountain_arr.get(peakIndex) == target: return peakIndex

    length = mountain_arr.length()
    left, right = 0, length - 1
    leftTargetIndex = findTargetIndex(mountain_arr, target, left, peakIndex, lambda x, y: x < y)
    if leftTargetIndex != -1: return leftTargetIndex
    rightTargetIndex = findTargetIndex(mountain_arr, target, peakIndex, right, lambda x, y: x > y)
    return rightTargetIndex

def main():
    param = [1,2,3,4,5,3,1,0]
    # param = [0,1,2,4,2,1]
    param = [1,5,2]
    mArr = MountainArray(param)
    ret = findInMountainArray(2, mArr)
    print(ret)

'''1095. 山脉数组中查找目标值

这是一个 交互式问题 ）

给你一个 山脉数组 mountainArr，请你返回能够使得 mountainArr.get(index) 等于 target 最小 的下标 index 值。

如果不存在这样的下标 index，就请返回 -1。

何为山脉数组？如果数组 A 是一个山脉数组的话，那它满足如下条件：

首先，A.length >= 3

其次，在 0 < i < A.length - 1 条件下，存在 i 使得：

A[0] < A[1] < ... A[i-1] < A[i]
A[i] > A[i+1] > ... > A[A.length - 1]

你将 不能直接访问该山脉数组，必须通过 MountainArray 接口来获取数据：

MountainArray.get(k) - 会返回数组中索引为k 的元素（下标从 0 开始）
MountainArray.length() - 会返回该数组的长度

注意：

对 MountainArray.get 发起超过 100 次调用的提交将被视为错误答案。此外，任何试图规避判题系统的解决方案都将会导致比赛资格被取消。

为了帮助大家更好地理解交互式问题，我们准备了一个样例 “答案”：https://leetcode-cn.com/playground/RKhe3ave，请注意这 不是一个正确答案。

示例 1：

输入：array = [1,2,3,4,5,3,1], target = 3
输出：2
解释：3 在数组中出现了两次，下标分别为 2 和 5，我们返回最小的下标 2。
示例 2：

输入：array = [0,1,2,4,2,1], target = 3
输出：-1
解释：3 在数组中没有出现，返回 -1。

提示：

3 <= mountain_arr.length() <= 10000
0 <= target <= 10^9
0 <= mountain_arr.get(index) <= 10^9

来源：力扣（LeetCode）
链接：https://leetcode-cn.com/problems/find-in-mountain-array
著作权归领扣网络所有。商业转载请联系官方授权，非商业转载请注明出处。
'''
if __name__ == '__main__':
    main()