import numpy as np

from core.transforms_3D import rot_y_matrix, translation_matrix, transform

class Box2D:
    # Modes
    CORNER_CORNER = 0
    CORNER_DIM = 1
    CENTER_DIM = 2

    def __init__(self, values, mode, cls=None, confidence=None, text=None):
        self.cls = cls
        self.confidence = confidence
        self.text = text
        if mode == Box2D.CORNER_CORNER:
            self.x1, self.y1, self.x2, self.y2 = values
            self.cx, self.cy, self.w, self.h = (self.x1 + self.x2) / 2, (self.y1 + self.y2) / 2, self.x2 - self.x1, self.y2 - self.y1
        elif mode == Box2D.CENTER_DIM:
            self.cx, self.cy, self.w, self.h = values
            self.x1, self.y1, self.x2, self.y2 = self.cx - self.w / 2, self.cy - self.h / 2, self.cx + self.w / 2, self.cy + self.h / 2
        elif mode == Box2D.CORNER_DIM:
            self.x1, self.y1, self.w, self.h = values
            self.cx, self.cy, self.x2, self.y2 = self.x1 + self.w / 2, self.y1 + self.h / 2, self.x1 + self.w, self.y1 + self.h

    def corner_corner(self):
        return self.x1, self.y1, self.x2, self.y2

    def center_dim(self):
        return self.cx, self.cy, self.w, self.h

    def __str__(self):
        return "Center: (%.3f, %.3f)   H,W:  (%.3f, %.3f)" % (self.cx, self.cy, self.h, self.w)

class Box3D:
    def __init__(self, h, w, l, x, y, z, yaw, cls=None, confidence=None, text=None):
        # Copy params
        self.h, self.w, self.l = h, w, l
        self.x, self.y, self.z = x, y, z
        self.yaw = yaw
        self.cls = cls
        self.confidence = confidence
        self.text = text

        self.__corners = None
        self.__arrow_pts = None

    def get_corners(self):
        '''
            corners.T => (8, 3)
            (0:4) -> top
            (4:8) -> bottom

            axis 2: x
            axis 0: y
            axis 1: z

            (top,bottom)
                 |
                 V
            (1,5)----(2,6)
              |        |
              |        |
            (0,4)----(3,7)
        '''
        if self.__corners is None:
            # Compute corners
            self.__corners = np.array([[-self.l / 2, self.l / 2, self.l / 2, -self.l / 2, -self.l / 2, self.l / 2, self.l / 2, -self.l / 2],
                                       [0, 0, 0, 0, -self.h, -self.h, -self.h, -self.h],
                                       [-self.w / 2, -self.w / 2, self.w / 2, self.w / 2, -self.w / 2, -self.w / 2, self.w / 2, self.w / 2]], dtype=np.float32)
            H = np.dot(translation_matrix(self.x, self.y, self.z), rot_y_matrix(self.yaw))
            self.__corners = transform(H, self.__corners)
        return self.__corners

    def get_bev_box(self):
        return self.get_corners()[(2, 0), :4]

    def get_arrow_pts(self):
        if self.__arrow_pts is None:
            # Compute arrow from center pointing forward
            self.__arrow_pts = np.array([[0, (self.l / 2) + 1],
                                         [-self.h / 2, -self.h / 2],
                                         [0, 0]])
            H = np.dot(translation_matrix(self.x, self.y, self.z), rot_y_matrix(self.yaw))
            self.__arrow_pts = transform(H, self.__arrow_pts)
        return self.__arrow_pts

    def get_bev_diags(self):
        return self.get_bev_box().T[(0, 2),:], self.get_bev_box().T[(1, 3),:]

    def get_3d_diag(self):
        return  (self.get_corners()[(2, 0, 1), :].T[(0, 2), :], self.get_corners()[(2, 0, 1), :].T[(4, 6), :]), \
                (self.get_corners()[(2, 0, 1), :].T[(1, 3), :], self.get_corners()[(2, 0, 1), :].T[(5, 7), :])

    def get_bev_center(self):
        diag = self.get_bev_diags()[0]
        x = (diag[0][0] + diag[1][0]) / 2.
        y = (diag[0][1] + diag[1][1]) / 2.
        return (x, y)

    def __str__(self):
        return "Center: (%.3f, %.3f, %.3f)   HWL: (%.3f, %.3f, %.3f)  YAW: %.3f" % (self.x, self.y, self.z, self.h, self.w, self.l, self.yaw)

def get_corners_3D(box):
    corners = np.array([[-box.l / 2, box.l / 2, box.l / 2, -box.l / 2, -box.l / 2, box.l / 2, box.l / 2, -box.l / 2],
                        [0, 0, 0, 0, -box.h, -box.h, -box.h, -box.h],
                        [-box.w / 2, -box.w / 2, box.w / 2, box.w / 2, -box.w / 2, -box.w / 2, box.w / 2, box.w / 2]], dtype=np.float32)
    H = np.dot(translation_matrix(box.x, box.y, box.z), rot_y_matrix(box.yaw))
    return transform(H, corners)

def translate_box_3D(box, x, y, z):
    box.x += x
    box.y += y
    box.z += z
    return box


from django.urls import path

from events.apps import EventsConfig
from events import views

app_name = EventsConfig.name

urlpatterns = [
    path('', views.EventListView.as_view(), name='list'),
    path('create/', views.EventCreateView.as_view(), name='create'),
    path('<int:pk>/', views.EventGetView.as_view(), name='get'),
    path('update/<int:pk>/', views.EventUpdateView.as_view(), name='update'),
    path('delete/<int:pk>/', views.EventDeleteView.as_view(), name='delete'),
    path('register-attendee/', views.EventAttendeeRegisterView.as_view(), name='register-attendee'),
    path('unregister-attendee/<int:pk>/', views.EventAttendeeUnregisterView.as_view(), name='unregister-attendee'),
]


import logging
import os
import time
from concurrent.futures import ThreadPoolExecutor

from dicomweb_client import DICOMwebClient
from pydicom.dataset import Dataset
from pydicom.filereader import dcmread

from monailabel.utils.others.generic import md5_digest, run_command

logger = logging.getLogger(__name__)

def generate_key(patient_id: str, study_id: str, series_id: str):
    return md5_digest(f"{patient_id}+{study_id}+{series_id}")

def get_scu(query, output_dir, query_level="SERIES", host="127.0.0.1", port="4242", aet="MONAILABEL"):
    start = time.time()
    field = "StudyInstanceUID" if query_level == "STUDIES" else "SeriesInstanceUID"
    run_command(
        "python",
        [
            "-m",
            "pynetdicom",
            "getscu",
            host,
            port,
            "-P",
            "-k",
            f"0008,0052={query_level}",
            "-k",
            f"{field}={query}",
            "-aet",
            aet,
            "-q",
            "-od",
            output_dir,
        ],
    )
    logger.info(f"Time to run GET-SCU: {time.time() - start} (sec)")

def store_scu(input_file, host="127.0.0.1", port="4242", aet="MONAILABEL"):
    start = time.time()
    input_files = input_file if isinstance(input_file, list) else [input_file]
    for i in input_files:
        run_command("python", ["-m", "pynetdicom", "storescu", host, port, "-aet", aet, i])
    logger.info(f"Time to run STORE-SCU: {time.time() - start} (sec)")

def dicom_web_download_series(study_id, series_id, save_dir, client: DICOMwebClient, frame_fetch=False):
    start = time.time()

    # Limitation for DICOMWeb Client as it needs StudyInstanceUID to fetch series
    if not study_id:
        meta = Dataset.from_json(
            [
                series
                for series in client.search_for_series(search_filters={"SeriesInstanceUID": series_id})
                if series["0020000E"]["Value"] == [series_id]
            ][0]
        )
        study_id = str(meta["StudyInstanceUID"].value)

    os.makedirs(save_dir, exist_ok=True)
    if not frame_fetch:
        instances = client.retrieve_series(study_id, series_id)
        for instance in instances:
            instance_id = str(instance["SOPInstanceUID"].value)
            file_name = os.path.join(save_dir, f"{instance_id}.dcm")
            instance.save_as(file_name)
    else:
        # TODO:: This logic (combining meta+pixeldata) needs improvement
        def save_from_frame(m):
            d = Dataset.from_json(m)
            instance_id = str(d["SOPInstanceUID"].value)

            # Hack to merge Info + RawData
            d.is_little_endian = True
            d.is_implicit_VR = True
            d.PixelData = client.retrieve_instance_frames(
                study_instance_uid=study_id,
                series_instance_uid=series_id,
                sop_instance_uid=instance_id,
                frame_numbers=[1],
            )[0]

            file_name = os.path.join(save_dir, f"{instance_id}.dcm")
            logger.info(f"++ Saved {os.path.basename(file_name)}")
            d.save_as(file_name)

        meta_list = client.retrieve_series_metadata(study_id, series_id)
        logger.info(f"++ Saving DCM into: {save_dir}")
        with ThreadPoolExecutor(max_workers=2, thread_name_prefix="DICOMFetch") as executor:
            executor.map(save_from_frame, meta_list)

    logger.info(f"Time to download: {time.time() - start} (sec)")

def dicom_web_upload_dcm(input_file, client: DICOMwebClient):
    start = time.time()
    dataset = dcmread(input_file)
    result = client.store_instances([dataset])

    url = ""
    for elm in result.iterall():
        s = str(elm.value)
        logger.info(f"{s}")
        if "/series/" in s:
            url = s
            break

    series_id = url.split("/series/")[1].split("/")[0] if url else ""
    logger.info(f"Series Instance UID: {series_id}")

    logger.info(f"Time to upload: {time.time() - start} (sec)")
    return series_id

if __name__ == "__main__":
    import shutil

    from monailabel.datastore.dicom import DICOMwebClientX

    client = DICOMwebClientX(
        url="https://d1l7y4hjkxnyal.cloudfront.net",
        session=None,
        qido_url_prefix="output",
        wado_url_prefix="output",
        stow_url_prefix="output",
    )

    study_id = "1.2.840.113654.2.55.68425808326883186792123057288612355322"
    series_id = "1.2.840.113654.2.55.257926562693607663865369179341285235858"

    save_dir = "/local/sachi/Data/dicom"
    shutil.rmtree(save_dir, ignore_errors=True)
    os.makedirs(save_dir, exist_ok=True)

    dicom_web_download_series(study_id, series_id, save_dir, client, True)


# -*- coding: utf-8 -*-
"""
Created on Thr Jan 10 09:13:24 2018

@author: takata@innovotion.co.jp
@author: harada@keigan.co.jp
"""
import argparse
import sys
import os
import pathlib
from time import sleep

current_dir = pathlib.Path(__file__).resolve().parent
sys.path.insert(0, str(current_dir) + '/../') # give 1st priority to the directory where pykeigan exists

from pykeigan import usbcontroller
from pykeigan import utils

parser = argparse.ArgumentParser(description='モーター動作　トルク制御')
parser.add_argument('port',metavar='PORT',default='/dev/ttyUSB0',nargs='?',help='モーターのデバイスファイル指定 (default:/dev/ttyUSB0)')
args = parser.parse_args()

os.system('clear')
for i in range(6):
    print("　　　　　　　")
print("\033[5;1H","---------------------------------------")
sys.stdout.flush()

"""
----------------------
モーター接続し、各種情報の取得
----------------------
"""
torque=0
position=0

##ログ情報callback
def on_motor_log_cb(log):
    if log['error_codes']!='KM_SUCCESS':
        print('log {} '.format(log))

#接続
dev=usbcontroller.USBController(args.port,False)
dev.on_motor_log_cb=on_motor_log_cb

"""
----------------------
モーター動作　トルク制御
----------------------
モーターを手で回して行くとトルクが加算され、重くなる。45度毎で0.025N*m増加
"""
torque_level=0

##トルクを監視し45度毎にトルクを増加
def on_motor_measurement_cb(measurement):
    global torque_level
    torque=measurement['torque']
    position=measurement['position']
    now_torque_level=round(utils.rad2deg(position)/45)*0.025
    if torque_level!=now_torque_level:
        torque_level=now_torque_level
        dev.set_max_torque(abs(torque_level))

    print('\033[4;1H\033[2K','torque/max_torque:{0:.2f}/{1:.2f}'.format(torque,torque_level))
    sys.stdout.flush()

def stop_torque_control_like_closing_cap():
    if dev:
        dev.on_motor_measurement_value_cb=None
        dev.disable_action()
        dev.set_max_torque(10.0)

def start_torque_control_like_closing_cap():
    global torque_level
    print('\033[2;1H\033[2K', 'Please try to turn the motor by hand.')
    sys.stdout.flush()
    dev.disable_action()
    dev.preset_position(0)
    sleep(0.2)
    dev.enable_action()
    dev.move_to_pos(0,utils.rpm2rad_per_sec(10))
    torque_level=10
    dev.on_motor_measurement_value_cb = on_motor_measurement_cb

"""
Exit with key input
"""

sleep(0.5)
while True:
    print('\033[6;1H\033[2K')
    sys.stdout.flush()
    if sys.version_info<(3,0):
        inp = raw_input('Command input > Start:[s] Reset:[r] Exit:[Other key] >>')
    else:
        inp = input('Command input > Start:[s] Reset:[r] Exit:[Other key] >>')
    if inp == 's':
        start_torque_control_like_closing_cap()
    elif inp == 'r':
        stop_torque_control_like_closing_cap()
    elif inp !=None:
        print()
        dev.set_led(1, 100, 100, 100)
        dev.disable_action()
        dev.set_max_torque(10.0)
        sleep(0.2)
        dev.disconnect()
        break


"""
[删除字符串中的所有相邻重复项](https://leetcode-cn.com/problems/remove-all-adjacent-duplicates-in-string/)
利用栈数据结构，和单调栈有点异曲同工之妙
"""

def removeDuplicates(S) -> str:
    stack = list()
    for s in S:
        if stack and stack[-1] == s:
            stack.pop()
        else:
            stack.append(s)
    return "".join(stack)


from flask import request, jsonify, make_response, abort, Blueprint
from app import db
import uuid
import copy
import datetime
from .helpers import remove_item_from_location, add_item_to_location, change_item_location, get_unassigned

#Blueprints
user_bp = Blueprint("user", __name__, url_prefix="/users")
game_bp = Blueprint("game", __name__, url_prefix="/games")

### USERS ###
users_ref = db.collection('users')

#Create new user
@user_bp.route('', methods=['POST'])
def create_user():
    #Verify presence of name and email
    if 'email' not in request.json.keys():
        abort(make_response(jsonify({"message": "Email not found"}), 404))
    if 'name' not in request.json.keys():
        abort(make_response(jsonify({"message": "Name not found"}), 404))
    #Create user with id, games, and timestamp fields
    new_user = request.json
    user_id = request.json['uid']
    new_user['game_ids'] = []
    new_user['timestamp'] = str(datetime.datetime.now())
    users_ref.document(user_id).set(new_user)
    return jsonify(new_user), 200

#Read users (all or by ID)
@user_bp.route('', methods=['GET'])
def read_users():
    #Returns all users if no param "id", else searches for user by ID
    user_id = request.args.get('user_id')
    if user_id:
        user = users_ref.document(user_id).get()
        return jsonify(user.to_dict()), 200
    else:
        all_users = [doc.to_dict() for doc in users_ref.stream()]
        return jsonify(all_users), 200

#Delete user and remove them from all games
@user_bp.route('', methods=['DELETE'])
def delete_user():
    user_id = request.args.get('user_id')
    game_ids = users_ref.document(user_id).get().to_dict()['game_ids']
    for game_id in game_ids:
        #Remove user from game's list of user IDs for each game
        game = games_ref.document(game_id).get().to_dict()
        game['user_ids'] = list(filter(lambda x: x != user_id, game['user_ids']))
        games_ref.document(game_id).set(game)
    users_ref.document(user_id).delete()
    return jsonify({"success": True}), 200

#Add user to game
@user_bp.route('/games', methods=['PATCH'])
def add_game_to_user():
    game_id = request.args.get('game_id')
    user_id = request.args.get('user_id')
    #Add game ID to user's list of game IDs
    user = users_ref.document(user_id).get().to_dict()
    if game_id not in user['game_ids']:
        user['game_ids'].append(game_id)
        users_ref.document(user_id).set(user)
    #Add user ID to game's list of user IDs
    game = games_ref.document(game_id).get().to_dict()
    if user_id not in game['user_ids']:
        game['user_ids'].append(user_id)
        games_ref.document(game_id).set(game)
    return jsonify(game), 200

#Edit user's name
@user_bp.route('', methods=['PATCH'])
def rename_user():
    user_id = request.args.get('user_id')
    user = users_ref.document(user_id).get().to_dict()
    name = request.json['name']
    user['name'] = name
    users_ref.document(user_id).set(user)
    return jsonify(user), 200

### GAMES ###

games_ref = db.collection('games')

#Create a new game
@game_bp.route('', methods=['POST'])
def create_game():
    if 'name' not in request.json.keys():
        abort(make_response(jsonify({"message": "Game name not found"}), 404))
    new_game = request.json
    game_id = str(uuid.uuid4())
    new_game['gid'] = game_id
    new_game['user_ids'] = []
    new_game['timestamp'] = str(datetime.datetime.now())
    games_ref.document(game_id).set(new_game)

    #Create default 'unassigned' location for game
    loc_id = str(uuid.uuid4())
    loc_ref = games_ref.document(game_id).collection('locations')
    loc_data = {'lid':loc_id, 'gid': game_id, 'type': 'location', 'item_ids': [], 'name': 'Unassigned', 'timestamp': str(datetime.datetime.now())}
    loc_ref.document(loc_id).set(loc_data)
    return jsonify(new_game), 200

#Delete a game
@game_bp.route('', methods=['DELETE'])
def remove_game():
    game_id = request.args.get('game_id')
    user_ids = games_ref.document(game_id).get().to_dict()['user_ids']
    #Remove item and location collections
    loc_ref = games_ref.document(game_id).collection('locations')
    for doc in loc_ref.stream():
        loc_id = doc.to_dict()['lid']
        loc_ref.document(loc_id).delete()
    item_ref = games_ref.document(game_id).collection('items')
    for doc in item_ref.stream():
        item_id = doc.to_dict()['iid']
        item_ref.document(item_id).delete()
    #Remove game from each user
    for user_id in user_ids:
        user = users_ref.document(user_id).get().to_dict()
        user['game_ids'] = list(filter(lambda x: x != game_id, user['game_ids']))
        users_ref.document(user_id).set(user)
    games_ref.document(game_id).delete()
    return jsonify({"success": True}), 200

#Read games (all or by ID)
@game_bp.route('', methods=['GET'])
def read_games():
    game_id = request.args.get('game_id')
    if game_id:
        game = games_ref.document(game_id).get()
        return jsonify(game.to_dict()), 200
    else:
        all_games = [doc.to_dict() for doc in games_ref.stream()]
        return jsonify(all_games), 200

#Read games from specific user
@user_bp.route('/games', methods=['GET'])
def read_games_from_user():
    user_id = request.args.get('user_id')
    games_list = []
    for game_id in users_ref.document(user_id).get().to_dict()['game_ids']:
        game = games_ref.document(game_id).get()
        games_list.append(game.to_dict())
    return jsonify(games_list)

#Rename game
@game_bp.route('', methods=['PATCH'])
def rename_game():
    game_id = request.args.get('game_id')
    name = request.json['name']
    game = games_ref.document(game_id).get().to_dict()
    new_game = copy.deepcopy(game)
    new_game['name'] = name
    games_ref.document(game_id).set(new_game)
    return jsonify(new_game), 200

#Get list of users in game
@game_bp.route('/users', methods=['GET'])
def get_game_users():
    game_id = request.args.get('game_id')
    game = games_ref.document(game_id).get().to_dict()
    user_ids = [users_ref.document(user_id).get().to_dict() for user_id in game['user_ids']]
    return jsonify(user_ids), 200

#Remove user from game
@game_bp.route('/users', methods=['PATCH'])
def remove_user_from_game():
    game_id = request.args.get('game_id')
    user_id = request.args.get('user_id')
    #Handle game
    game = games_ref.document(game_id).get().to_dict()
    game['user_ids'] = [id for id in game['user_ids'] if id != user_id]
    games_ref.document(game_id).set(game)
    #Handle user
    user = users_ref.document(user_id).get().to_dict()
    user['game_ids'] = [id for id in user['game_ids'] if id != game_id]
    users_ref.document(user_id).set(user)
    return jsonify(game), 200

### LOCATIONS ###

#Create location within game
@game_bp.route('/locations', methods=['POST'])
def add_location():
    game_id = request.args.get('game_id')
    name = request.json['name']
    type_name = request.json['type']
    loc_id = str(uuid.uuid4())
    loc_ref = games_ref.document(game_id).collection('locations')
    loc_data = {'name': name, 'lid': loc_id, 'gid': game_id, 'type': type_name, 'item_ids': [], 'timestamp': str(datetime.datetime.now())}
    loc_ref.document(loc_id).set(loc_data)
    return jsonify(loc_data), 200

#Delete location and unassign all items
@game_bp.route('locations', methods=['DELETE'])
def delete_location():
    #Get all items from location
    game_id = request.args.get('game_id')
    loc_id = request.args.get('loc_id')
    loc_ref = games_ref.document(game_id).collection('locations')
    item_ids = loc_ref.document(loc_id).get().to_dict()['item_ids']
    unassigned_id = get_unassigned(loc_ref)
    #Move every item in location to 'Unassigned'
    for item_id in item_ids:
        change_item_location(games_ref, game_id, item_id, unassigned_id)
        add_item_to_location(games_ref, game_id, item_id, unassigned_id)
    loc_ref.document(loc_id).delete()
    return jsonify({'success': True}), 200

#Get location or list of locations
@game_bp.route('/locations', methods=['GET'])
def read_locations():
    game_id = request.args.get('game_id')
    loc_id = request.args.get('loc_id')
    loc_ref = games_ref.document(game_id).collection('locations')
    if loc_id:
        loc = loc_ref.document(loc_id).get()
        return jsonify(loc.to_dict()), 200
    else:
        all_locs = [doc.to_dict() for doc in loc_ref.stream()]
        return jsonify(all_locs), 200

#Rename location
@game_bp.route('/locations', methods=['PATCH'])
def rename_location():
    game_id = request.args.get('game_id')
    loc_id = request.args.get('loc_id')
    name = request.json['name']
    loc_ref = games_ref.document(game_id).collection('locations')
    new_loc = loc_ref.document(loc_id).get().to_dict()
    new_loc['name'] = name
    loc_ref.document(loc_id).set(new_loc)
    return jsonify(new_loc)

### ITEMS ###

#Create item w/ location ID (default to unassigned)
@game_bp.route('/items', methods=['POST'])
def create_item():
    game_id = request.args.get('game_id')
    loc_id = request.args.get('loc_id')
    loc_ref = games_ref.document(game_id).collection('locations')
    item_ref = games_ref.document(game_id).collection('items')
    item_id = str(uuid.uuid4())
    if not loc_id:
        loc_id = get_unassigned(loc_ref)
    #Add new item to items collection of game
    new_item = request.json
    new_item['lid'] = loc_id
    new_item['gid'] = game_id
    new_item['iid'] = item_id
    new_item['timestamp'] = str(datetime.datetime.now())
    item_ref.document(item_id).set(new_item)
    #Add item to location's item ID list
    add_item_to_location(games_ref, game_id, item_id, loc_id)
    return jsonify(new_item), 200

#Delete item
@game_bp.route('/items', methods=['DELETE'])
def delete_item():
    game_id = request.args.get('game_id')
    item_id = request.args.get('item_id')
    #Get loc ID of item
    item_ref = games_ref.document(game_id).collection('items')
    loc_id = item_ref.document(item_id).get().to_dict()['lid']
    remove_item_from_location(games_ref, game_id, item_id, loc_id)
    #Delete item
    item_ref.document(item_id).delete()
    return jsonify({'success': True}), 200

# Change location of item
@game_bp.route('/items/locations', methods=['PATCH'])
def update_item_location():
    game_id = request.args.get('game_id')
    item_id = request.args.get('item_id')
    loc_id = request.args.get('loc_id')
    new_loc = request.json['loc_id']
    item = change_item_location(games_ref, game_id, item_id, new_loc)
    remove_item_from_location(games_ref, game_id, item_id, loc_id)
    add_item_to_location(games_ref, game_id, item_id, new_loc)
    return jsonify(item), 200

# Change name/type of item
@game_bp.route('/items', methods=['PATCH'])
def update_item_fields():
    game_id = request.args.get('game_id')
    item_id = request.args.get('item_id')
    items = games_ref.document(game_id).collection('items')
    item = items.document(item_id).get().to_dict()
    data = request.json

    for field, value in data.items():
        item[field] = value

    items.document(item_id).set(item)

    return jsonify(item), 200

#Get item or list of items
@game_bp.route('/items', methods=['GET'])
def read_items():
    game_id = request.args.get('game_id')
    item_id = request.args.get('item_id')
    item_ref = games_ref.document(game_id).collection('items')
    if item_id:
        item = item_ref.document(item_id).get()
        return jsonify(item.to_dict()), 200
    else:
        all_items = [doc.to_dict() for doc in item_ref.stream()]
        return jsonify(all_items), 200

#Read items from location
@game_bp.route('/locations/items', methods=['GET'])
def read_items_from_location():
    game_id = request.args.get('game_id')
    loc_id = request.args.get('loc_id')
    loc_ref = games_ref.document(game_id).collection('locations')
    location = loc_ref.document(loc_id).get().to_dict()
    item_ref = games_ref.document(game_id).collection('items')
    item_ids = location['item_ids']
    items = []
    for item_id in item_ids:
        items.append(item_ref.document(item_id).get().to_dict())
    return jsonify(items), 200


#!/usr/bin/env python3
import requests
import spacy

url = "https://query.wikidata.org/sparql"
url_api = "https://www.wikidata.org/w/api.php"
params_entity = {'action': 'wbsearchentities', 'language': 'en', 'format': 'json'}
params_prop = {'action': 'wbsearchentities', 'language': 'en', 'format': 'json', 'type': 'property'}

nlp = spacy.load("en_core_web_sm")

def find_matches_ent(string):
    params_entity['search'] = string
    json = requests.get(url_api, params_entity).json()
    entities = []
    for result in json['search']:
        # print("{}\t{}\t{}".format(result['id'], result['label'], result['description']))
        entities.append(result['id'])
    return entities

def make_query(property, entity):
    answers = []
    query = '''
		SELECT ?answerLabel WHERE {
			wd:''' + entity + ''' wdt:''' + property + ''' ?answer.
			SERVICE wikibase:label {
				bd:serviceParam wikibase:language "en" .
			}
		}'''
    data = requests.get(url, params={'query': query, 'format': 'json'}).json()
    for item in data['results']['bindings']:
        for var in item:
            answers.append(item[var]['value'])
    if len(answers) == 0:
        return None
    else:
        return answers

def find_answer(properties, entities):
    if not entities or not properties:
        return None
    ans = None
    for entity in entities:
        for property in properties:
            ans = make_query(property, entity)
            if ans is not None:
                return ans
    return ans

def find_answer(properties, entities):
    ans = None
    for entity in entities:
        for property in properties:
            ans = make_query(property, entity)
            if ans is not None:
                return ans
    return ans

# What is the X of Y?
def create_and_fire_queryDueto(line):
    line = nlp(line.rstrip())  # removes newline
    subject = []
    property = []
    flag = 0
    group = []

    for token in line:
        # print(token.text, token.lemma_, token.dep_, token.head.lemma_)
        if token.dep_ == "ROOT" or token.dep_ == "pcomp":
            property.append(token.lemma_)
        if token.dep_ == "nsubj" or (token.dep_ == "compound" and token.head.dep_ == "nsubj"):
            subject.append(token.lemma_)

    prop = str(' '.join(property))
    ent = str(' '.join(subject))

    properties = ['P509']
    entities = find_matches_ent(ent)

    if len(entities) == 0 or len(properties) == 0:
        return 0

    answer = find_answer(properties, entities)
    if answer is None:
        return 0
    else:
        print("\t",'\t'.join(answer))

    return 1


list = []
n = int(input("Enter number of elements : "))

for i in range(0, n):
    l = int(input())

    list.append(l)
print("INPUT",list)
print("Output")
for num in list:

    if num >= 0:
        print(num, end = " ")


import csv
from dataclasses import dataclass
from typing import List

# def load_sales(sales_path='./sales.csv'):
#     sales = []
#     with open(sales_path, encoding='utf-8') as f:
#         for sale in csv.DictReader(f):
#             # 値の変換
#             try:
#                 sale['price'] = int(sale['price'])
#                 sale['amount'] = int(sale['amount'])
#             except (ValueError, TypeError, KeyError):
#                 continue
#             # 値のチェック
#             if sale['price'] <= 0:
#                 continue
#             if sale['amount'] <= 0:
#                 continue
#     # 売上の計算
#     sum_price = 0
#     for sale in sales:
#         sum_price += sale['amount'] * sale['price']
#     return sum_price, sales

# 売上(CSVの各行)を表すクラスに分離する
@dataclass
class Sale:
    id: int
    item_id: int
    amount: int
    price: int

    def validate(self):
        if self.price <= 0:
            raise ValueError('Invalid sale.price')
        if self.amount <= 0:
            raise ValueError('Invalid sale.amount')

    # 各売上の料金を計算する処理をSalesに実装
    # property:値を簡単に変更できない
    # @property
    def price(self):
        return self.amount * self.price

@dataclass
class Sales:
    data: List[Sale]

    @property
    def price(self):
        return sum(sale.price for sale in self.data)

    @classmethod
    def from_assert(cls, path='./sales.csv'):
        data = []
        with open(path, encoding='utf-8') as f:
            reader = csv.DictReader(f)
            for row in reader:
                try:
                    sale = Sale(**row)
                    sale.id = int(sale.id)
                    sale.item_id = int(sale.item_id)
                    sale.amount = int(sale.amount)
                    sale.price = int(sale.price)
                    sale.validate()
                except Exception as e:
                    print(e)
                    continue
                data.append(sale)
        return cls(data=data)


import service.control

from . import actions
from .actions import (
    create_index,
    delete_entities,
    search_v2,
    update_entities,
    track_recent,
    get_recents,
    delete_recent,
)

class Server(service.control.Server):
    service_name = 'search'

    actions = {
        'create_index': create_index.Action,
        'delete_entities': delete_entities.Action,
        'search': actions.Search,
        'search_v2': search_v2.Action,
        'update_entities': update_entities.Action,
        'track_recent': track_recent.Action,
        'get_recents': get_recents.Action,
        'delete_recent': delete_recent.Action,
    }


import os
import json
import time
import h5py
import logging
import numpy as np

from annoy import AnnoyIndex
from tensorflow.keras import optimizers
from tensorflow.keras.models import Model
from tensorflow.keras.preprocessing import image
from tensorflow.keras.applications.vgg16 import VGG16
from tensorflow.keras.applications.vgg16 import preprocess_input
from tensorflow.keras.layers import Dense, BatchNormalization, Activation, Dropout

logger = logging.getLogger()
logger.setLevel(logging.INFO)

ls_class = os.listdir("Data/TrainingData")
ls = []
for i in ls_class:
    path, dirs, files = next(os.walk("Data/TrainingData"+"/" +i))
    file_count = len(files)
    ls.append([i, file_count])

def load_paired_img_wrd(folder):
    class_names = [fold for fold in os.listdir(folder) if ".DS" not in fold]
    image_list = []
    labels_list = []
    paths_list = []
    for cl in class_names:
        splits = cl.split("_")
        subfiles = [f for f in os.listdir(folder + "/" + cl) if ".DS" not in f]
        for subf in subfiles:
            full_path = os.path.join(folder, cl, subf)
            img = image.load_img(full_path, target_size=(224, 224))
            x_raw = image.img_to_array(img)
            x_expand = np.expand_dims(x_raw, axis=0)
            x = preprocess_input(x_expand)
            image_list.append(x)
            paths_list.append(full_path)
    img_data = np.array(image_list)
    img_data = np.rollaxis(img_data, 1, 0)
    img_data = img_data[0]

    return img_data, paths_list

def load_headless_pretrained_model():
    pretrained_vgg16 = vgg16.VGG16(weights='imagenet', include_top=True)
    model = Model(inputs=pretrained_vgg16.input,
                  outputs=pretrained_vgg16.get_layer('fc2').output)
    return model

def generate_features(image_paths, model):
    print ("Generating features...")
    start = time.time()
    images = np.zeros(shape=(len(image_paths), 224, 224, 3))
    file_mapping = {i: f for i, f in enumerate(image_paths)}

    for i, f in enumerate(image_paths):
        img = image.load_img(f, target_size=(224, 224))
        x_raw = image.img_to_array(img)
        x_expand = np.expand_dims(x_raw, axis=0)
        images[i, :, :, :] = x_expand

    logger.info("%s images loaded" % len(images))
    inputs = preprocess_input(images)
    logger.info("Images preprocessed")
    images_features = model.predict(inputs)
    end = time.time()
    logger.info("Inference done, %s Generation time" % (end - start))
    return images_features, file_mapping

def save_features(features_filename, features, mapping_filename, file_mapping):
    print ("Saving features...")
    np.save('%s.npy' % features_filename, features)
    with open('%s.json' % mapping_filename, 'w') as index_file:
        json.dump(file_mapping, index_file)
    logger.info("Weights saved")

def load_features(features_filename, mapping_filename):
    print ("Loading features...")
    images_features = np.load('%s.npy' % features_filename)
    with open('%s.json' % mapping_filename) as f:
        index_str = json.load(f)
        file_index = {int(k): str(v) for k, v in index_str.items()}
    return images_features, file_index

def index_features(features, n_trees=1000, dims=4096, is_dict=False):
    print ("Indexing features...")
    feature_index = AnnoyIndex(dims, metric='angular')
    for i, row in enumerate(features):
        vec = row
        if is_dict:
            vec = features[row]
        feature_index.add_item(i, vec)
    feature_index.build(n_trees)
    return feature_index

def search_index_by_key(key, feature_index, item_mapping, top_n=10):
    distances = feature_index.get_nns_by_item(key, top_n, include_distances=True)
    return [[a, item_mapping[a], distances[1][i]] for i, a in enumerate(distances[0])]

def get_index(input_image, file_mapping):
    for index, file in file_mapping.items():
        if file == input_image:
            return index
    raise ValueError("Image %s not indexed" % input_image)

def get_class_weights_from_vgg(save_weights=False, filename='class_weights'):
    model_weights_path = os.path.join(os.environ.get('HOME'),
                                      '.keras/models/vgg16_weights_tf_dim_ordering_tf_kernels.h5')
    weights_file = h5py.File(model_weights_path, 'r')
    weights_file.get('predictions').get('predictions_W_1:0')
    final_weights = weights_file.get('predictions').get('predictions_W_1:0')

    class_weights = np.array(final_weights)[:]
    weights_file.close()
    if save_weights:
        np.save('%s.npy' % filename, class_weights)
    return class_weights

def get_weighted_features(class_index, images_features):
    class_weights = get_class_weights_from_vgg()
    target_class_weights = class_weights[:, class_index]
    weighted = images_features * target_class_weights
    return weighted

def main():
    images, image_paths = load_paired_img_wrd('/Data/TrainingData')
    model = load_headless_pretrained_model()
    images_features, file_index = generate_features(image_paths, model)
    path = "image-to-image-search/Code"
    save_features(path, images_features, path, file_index)
    images_features, file_index = load_features(path, path)
    image_index = index_features(images_features, dims=4096)
    input_train = "/Data/TrainingData/class-điện thoại/465.jfif"
    search_key = get_index(input_train, file_index)
    results = search_index_by_key(search_key, image_index, file_index)

    for i in range(len(results)):
        im = results[i][1]
        img=mpimg.imread(im)
        if i==0:
            img_ = img.copy()
        else:
            img_ = np.concatenate((img_,img), axis=1)
    plt.figure(figsize=(15,15))
    plt.imshow(img_)
    plt.show()

if __name__ == 'main':
    main()


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Mar 28 13:35:15 2022

@author: andres
"""

#El procesamiento de la información fue realizado en python y las estimaciiones en R
#Librerias utilizadas para el preprocesamiento
import pandas as pd
import numpy as np
from datetime import datetime, timedelta
from sklearn.preprocessing import MinMaxScaler
#%%

#Variables
#Control
#En este parte se separar las variables según el uso que se les daran (Control, separacion e índices)

otras = { 'DIAREAL':'DIAREAL', 'MESREAL':'MESREAL', 'IDENPA':'IDENPA'}

control = {'Gobierno_Opocicion':'PERPART',
           'Edad':'REEDAD', 'Educacion_Entrevistado': 'REEDUC.1',
           'Educacion_padres': 'REEDUC.2', 'Nivel_Socioeconomico (Entrevistado)':'S26',
           'Estudios_padres_2': 'S11', 'Estudio_entrevistado':'S10',
           'Raza(Negro)': 'S6', 'Edad_2':'EDAD', 'SEXO':'SEXO', 'LUZ': 'P31NI',
           'Clase_Sub': 'S1', 'Ocupación':'S14A', 'Ocupación activos':'S15',
           'Estado_Civil':'S23', 'Beneficiario': 'S25', 'Migracion_Cambio_1_2a_0_1':'S16', 'ponde':'WT',
           'Principal_ingreso':'S8'
           }
extras = ['S21', 'S12'] # Bienes, redes sociales

#Gobierno

gobierno = {'Confianza_Fuerzas_Armadas':'P15STGBSC.A','Confianza_Policía':'P15STGBSC.B',
            'Confianza_Iglesia':'P15STGBSC.C','Confianza_Congreso':'P15STGBSC.D',
            'Confianza_Gobierno':'P15STGBSC.E','Confianza_Poder_Judicial':'P15STGBSC.F',
            'Confianza_Partidos_Políticos':'P15STGBSC.G','Confianza_Electoral':'P15STGBSC.H',
            'Confianza_Bancos':'P16NC.F',
            'Aprobación_gobierno_presidente(nombre)':'P20STGBSC',
            }
#Economia
economia = {'Situacion_Economica':'P6STGBSC', 'Situacion_Economia_Futura':'P8STIC',
            'Confianza_interpersonal': 'P11STGBS', 'Satisfaccion_funcionamiento_economia':'P13STGBS.B',
            'Integracion_politca_sino':'P39ST.B',
            'Nac_menor_inter_mayor':'P52N.C'}
#%%
# En las partes de las funciones, se encuentran diferentes funcion que se basaron en la segmentacion
# para el pais, la fecha y el cambio en la denominacion de algúnas preguntas.
#Fuciones
def Transform_date(df):
    df.reset_index(inplace =True, drop =True)
    df['Fecha_lar'] = int()
    for i in range(df.shape[0]):
                day = df.at[i, 'DIAREAL']
                mes = df.at[i, 'MESREAL']
                año = 18
                dte = '{}-{}-{}'.format(day, mes, año)
                df.at[i, 'Fecha_lar'] = datetime.strptime(dte, '%d-%m-%y')

def PaisNum(df):
    df['IDENPA_num'] = int()
    for i in range(df.shape[0]):
        if df.at[i, 'IDENPA'] == 170:
            df.at[i, 'IDENPA_num'] = 1
        elif df.at[i, 'IDENPA'] == 218:
            df.at[i, 'IDENPA_num'] = 2
        elif df.at[i, 'IDENPA'] == 862:
            df.at[i, 'IDENPA_num'] = 3
        elif df.at[i, 'IDENPA'] == 604:
            df.at[i, 'IDENPA_num'] = 4

def FechaNum(dfx):
    #Nueva estructura del codigo:
    u = list(gobierno.values()) + list(economia.values()) + list(control.values())+list(otras.values())
    dta = data[u]
    Transform_date(dta)
    valores = dta['Fecha_lar'].value_counts()
    valores = valores.reset_index()
    valores.sort_values('index',inplace =True)
    valores = valores.reset_index(drop = True)
    valores = valores.reset_index(drop = False)
    dfx['Fecha_num'] = int()

    for i in range(valores.shape[0]):
        for j in range(dfx.shape[0]):
            if dfx.at[j, 'Fecha_lar'] ==  valores.at[i, 'index']:
                dfx.at[j,'Fecha_num'] = valores.at[i,'level_0']
def Dummy(df):
    df['T_C'] = int()
    df['B_A'] = int()
    df['hombre'] = int()
    df['mujer'] = int()
    df['SEXO'].replace(2, 0, inplace= True)
    df['Estrato'] = int()
    df['S25'].replace(2,0, inplace =True)
    fecha_importante = fecha_importante = '03-07-18'
    p = datetime.strptime(fecha_importante, '%d-%m-%y')
    for i in range(df.shape[0]):
        if df.at[i, 'Fecha_lar'] >= p:
            df.at[i, 'B_A'] = 1
        else:
            df.at[i, 'B_A'] = 0
        if df.at[i, 'IDENPA_num'] == 1:
            df.at[i, 'T_C'] = 1
        else:
            df.at[i, 'T_C'] = 0

        if df.at[i,'SEXO'] == 1:
            df.at[i, 'hombre'] = 1
            df.at[i, 'mujer'] = 0
        else:
            df.at[i, 'hombre'] = 0
            df.at[i, 'mujer'] = 1
        if df.at[i, 'S1'] >3: #Esto depende de la variable que se utilice S1. Subjetiva S26 Contestada por el encuestados
            df.at[i, 'Estrato'] = 1
        else:
            df.at[i, 'Estrato'] = 0
def Educ(df):
    df['prima'] = np.where(df['REEDUC.1'] ==1,1, 0)
    df['sec'] = np.where(df['REEDUC.1'] ==2,1, 0)
    df['Sup'] = np.where(df['REEDUC.1'] ==3,1, 0)

def Nulos(df):
    for i in df.columns:
        print('Col: {} / Num Nan: {}'.format(i, df[i].isnull().sum()))

#%%
data = pd.read_csv('data_baro.csv')
#Segmentacion de los datos con las nuevas variables
nombres = list(control.keys()) + list(gobierno.keys()) + list(economia.keys()) + list(otras.keys())

#%%

#Con la finalidad de estimar el impacto utilizando las preguntas por separado,
#se realizaron una base de datos por pregunta. La segmentacion se evidencia en el
#bloque de codigo de abajo.
#Tener una base de datos por preguntas.
lista = []
cols = list(gobierno.values()) + list(economia.values()) + list(control.values())
for i in cols:
    cols2 = np.append(np.array(list(otras.values())),i)
    cols2 = cols2
    d = pd.DataFrame(data[cols2])
    d = d[(d.IDENPA == 218) | (d.IDENPA == 170) | (d.IDENPA == 604) | (d.IDENPA == 862)]
    # scaler = MinMaxScaler().fit(pd.DataFrame(d[i]))
    # d[i] = pd.DataFrame(scaler.transform(pd.DataFrame(d[i])), columns = [i])
    for j in d.columns:
        #Remplazar por pregunta
        d[j] = d[j].replace([-1, -2, -3, -4], [None, None, None, None])
    d.dropna(inplace =True, how = 'any')
    d.reset_index(drop=True, inplace=True)
    if i == 'SEXO' or i == 'P20STGBSC':
        d[i].replace(2, 0, inplace= True)
    if i not in np.array(list(control.values())):
        d[i] = (d[i] - d[i].min()) / (d[i].max() - d[i].min())

    Transform_date(d)
    dt = d.groupby(['Fecha_lar', 'IDENPA']).mean()
    dt = pd.DataFrame(dt[i])
    lista.append(d)

for i in range(len(lista)):
    if i >= 24:
        break
    if i == 0:
        prueba = lista[0].merge(lista[1], how='left',left_index=True, right_index=True)
    prueba = prueba.merge(lista[i+2], how='left',left_index=True, right_index=True)

prueba.reset_index(inplace = True)

FechaNum(prueba)
PaisNum(prueba)
Dummy(prueba)
prueba = prueba[(prueba.Fecha_num >=8) &(prueba.Fecha_num <=30)]

prueba.to_excel('Base_Datos_01042022.xlsx')

#%%

#Bloque donde se evidencia la base de datos por pregunta
lista = []
cols = list(gobierno.values()) + list(economia.values())
for i in cols:
    cols2 = np.append(np.array(list(otras.values())+list(control.values())),i)
    cols2 = cols2
    d = pd.DataFrame(data[cols2])
    d = d[(d.IDENPA == 170) | (d.IDENPA == 604)]
    # scaler = MinMaxScaler().fit(pd.DataFrame(d[i]))
    # d[i] = pd.DataFrame(scaler.transform(pd.DataFrame(d[i])), columns = [i])
    for j in d.columns:
        #Remplazar por pregunta
        d[j] = d[j].replace([-1, -2, -3, -4], [None, None, None, None])
    d.dropna(inplace =True, how = 'any')
    d.reset_index(drop=True, inplace=True)
    if i not in list(control.values()):
        d[i] = (d[i] - d[i].min()) / (d[i].max() - d[i].min())
    Transform_date(d)
    PaisNum(d)
    FechaNum(d)
    p = datetime.strptime('03-07-18', '%d-%m-%y')
    d = d[(d.Fecha_lar >= p - timedelta(days=4)) & (d.Fecha_lar <= p + timedelta(days=5))]
    d.reset_index(drop=True, inplace =True)
    Dummy(d)
    d.to_excel('./Bases_Dif_n1/{}.xlsx'.format(i))

    lista.append(d)

#%%
#Buscando realizar le balance de las medias, se creo una base de datos para
#estimar las medias de diferentes variables demograficas (Control) para
#todos los individuos de la muestra.
#Crear una base de datos unicamente para realizar la medicion de las medias
import math
medias =  data[(data.IDENPA == 604) | (data.IDENPA == 170)]# |(data.IDENPA == 218) ]#data[(data.IDENPA == 218) | (data.IDENPA == 170) | (data.IDENPA == 604) | (data.IDENPA == 862)]
medias = medias[list(control.values())+list(otras.values())]
for j in medias.columns:
        medias[j] = medias[j].replace([-1, -2, -3, -4], [None, None, None, None])
medias['Id'] = np.where(medias['IDENPA'] !=170, 'Control', 'Tratment')
medias['SEXO'].replace(2,0, inplace =True)
# medias['P31NI'] =  np.log2(medias['P31NI'])
medias['S23'].replace([2,3], [0,0], inplace = True)
medias['S14A'].replace([2,3,4,5,6,7], [1,1,0,0,0,0], inplace =True)
medias['S25'].replace(2,0, inplace = True)
medias['S16'].replace([2], [0], inplace = True)
medias['S8'].replace(2,0, inplace = True)
Educ(medias)
medias.to_excel('Prueba_Medias.xlsx')

for i in medias.columns:
    print('Col: {} / Num Nan: {}'.format(i, medias[i].isnull().sum()))

#%%
#Indice con las pregumtas que fueron significativas
#Utilizando las variables que obtuvieron una significancia estadistica,
#se realizaron los indices sin tener en cuenta variables de control
#Primero las de gobierno, y luego economia
preguntas = {'Aprobacion(.)': 'P20STGBSC' ,'Confianza_Partidos(**)':'P15STGBSC.G',
             'Confianza_PoderJ(*)':'P15STGBSC.F', 'Confianza_FuerzasA(*)':'P15STGBSC.A'

      #'Aprobacion(.)': 'P20STGBSC',
             }
p_e= {'Situacion_eco_actual':'P6STGBSC', 'Situacion_futura':'P8STIC', 'Satisfaccion_Eco':'P13STGBS.B'}

def IndicePrep(dicc):

    indice = data[(data.IDENPA == 604) | (data.IDENPA == 170)]
    indice = indice[list(control.values())+list(otras.values())+list(dicc.values())]
    for j in indice.columns:
        #Remplazar por pregunta

        if j in list(dicc.values()):
            indice[j] = indice[j].replace([-1, -2, -3, -4], [None, None, None, None])
            indice.dropna(inplace =True, how = 'any')
            indice.reset_index(drop=True, inplace=True)
            indice[j] = (indice[j] - indice[j].min()) / (indice[j].max() - indice[j].min())
    return indice

indice_gov = IndicePrep(preguntas)

indice_gov['Gov'] = int()
indice_gov['Gov'] = (indice_gov['P20STGBSC']+ indice_gov['P15STGBSC.G']+ indice_gov['P15STGBSC.F']+ indice_gov['P15STGBSC.A'])/4

Transform_date(indice_gov)
FechaNum(indice_gov)
PaisNum(indice_gov)
Dummy(indice_gov)
Educ(indice_gov)
indice_gov = indice_gov[( indice_gov.Fecha_num>=15)&( indice_gov.Fecha_num<=26)]
indice_gov['Id'] = np.where(indice_gov['IDENPA'] !=170, 'Control', 'Tratment')
indice_gov['S23'].replace([2,3], [0,0], inplace = True)
indice_gov['S14A'].replace([2,3,4,5,6,7], [1,1,0,0,0,0], inplace =True)
indice_gov['S25'].replace(2,0, inplace = True)
indice_gov['S16'].replace([2], [0], inplace = True)
indice_gov['S8'].replace(2,0, inplace = True)

indice_gov.to_excel('indice_gov.xlsx')

indice_eco = IndicePrep(p_e)

indice_eco['Eco'] = (indice_eco['P6STGBSC']+ indice_eco['P8STIC']+ indice_eco['P13STGBS.B'])/3
Transform_date(indice_eco)
FechaNum(indice_eco)
PaisNum(indice_eco)
Dummy(indice_eco)
indice_eco= indice_eco[( indice_eco.Fecha_num>=15)&( indice_eco.Fecha_num<=26)]
indice_eco.to_excel('indice_eco.xlsx')

#%%
#Se realiza el mismo proceso de arriba pero teniendo en cuenta las variables de control
#Ahora una base de datos con los respectivos controles (Correr solo si se va a estimar la regresion con las variables de control)
control = {

            'Nivel_Socioeconomico (Entrevistado)':'S26',
           'Estudios_padres_2': 'S11', 'Estudio_entrevistado':'S10',
           'Raza(Negro)': 'S6', 'Edad_2':'EDAD', 'SEXO':'SEXO',
           'Clase_Sub': 'S1', 'Ocupación':'S14A',
           'Estado_Civil':'S23', 'Edu_seg':'REEDUC.1', 'Beneficiario':'S25', 'Principal_Ingreso':'S8'
           }
preguntas = {'Aprobacion(.)': 'P20STGBSC' ,'Confianza_Partidos(**)':'P15STGBSC.G',
             'Confianza_PoderJ(*)':'P15STGBSC.F', 'Confianza_FuerzasA(*)':'P15STGBSC.A'

      #'Aprobacion(.)': 'P20STGBSC',
             }
p_e= {'Situacion_eco_actual':'P6STGBSC', 'Situacion_futura':'P8STIC', 'Satisfaccion_Eco':'P13STGBS.B'}

# bienes_col = {'Casa propia':'S21.A', 'Computador':'S21.B', 'Lavarropas':'S21.C', 'Teléfono Red Fija':'S21.E',
# 'Teléfono celular/móvil':'S21.F', 'Auto':'S21.G', 'Agua caliente':'S21.I', ' Alcantarillado/Cloacas':'S21.J',
# 'Al menos una comida caliente al día':'S21.K', 'Agua potable':'S21.L', ' Smartphone':'S21.M',
# 'Conexión a Internet en el hogar':'S21.N', 'Conexión a Internet en el hogar':'S21.O', 'Calefaccion':'S21.P'}

def IndicePrep(dicc):

    indice = data[(data.IDENPA == 604) | (data.IDENPA == 170)]
    indice = indice[list(control.values())+list(otras.values())+list(dicc.values())]
    base = list(dicc.values()) + list(control.values())
    for j in indice.columns:
        #Remplazar por pregunta

        if j in base:

            indice[j] = indice[j].replace([-1, -2, -3, -4], [None, None, None, None])
            indice.dropna(inplace =True, how = 'any')
            indice.reset_index(drop=True, inplace=True)
            if j in list(dicc.values()):
                indice[j] = (indice[j] - indice[j].min()) / (indice[j].max() - indice[j].min())
    indice['S23'].replace([2,3], [0,0], inplace = True)
    indice['S14A'].replace([2,3,4,5,6,7], [1,1,0,0,0,0], inplace =True)

    return indice

indice_gov = IndicePrep(preguntas)

indice_gov['Gov'] = int()
indice_gov['Gov'] = (indice_gov['P20STGBSC']+ indice_gov['P15STGBSC.G']+ indice_gov['P15STGBSC.F']+ indice_gov['P15STGBSC.A'])/4

Transform_date(indice_gov)
FechaNum(indice_gov)
PaisNum(indice_gov)
Dummy(indice_gov)
indice_gov = indice_gov[( indice_gov.Fecha_num>=16)&( indice_gov.Fecha_num<=26)]
Educ(indice_gov)
indice_gov.to_excel('indice_gov_co.xlsx')
indice_eco = IndicePrep(p_e)

indice_eco['Eco'] = (indice_eco['P6STGBSC']+ indice_eco['P8STIC']+ indice_eco['P13STGBS.B'])/3
Transform_date(indice_eco)
FechaNum(indice_eco)
PaisNum(indice_eco)
Dummy(indice_eco)
indice_eco= indice_eco[( indice_eco.Fecha_num>=16)&( indice_eco.Fecha_num<=26)]
Educ(indice_eco)
indice_eco.to_excel('indice_eco_co.xlsx')
# medias['S23'].replace([2,3], [0,0], inplace = True)
# medias['S14A'].replace([2,3,4,5,6,7], [1,1,0,0,0,0], inplace =True)

#%%

#En esta parte se obtienen la base de datos sin valores nulos.

preguntas = {'Aprobacion(.)': 'P20STGBSC' ,'Confianza_Partidos(**)':'P15STGBSC.G',
             'Confianza_PoderJ(*)':'P15STGBSC.F', 'Confianza_FuerzasA(*)':'P15STGBSC.A'

      #'Aprobacion(.)': 'P20STGBSC',
             }
p_e= {'Situacion_eco_actual':'P6STGBSC', 'Situacion_futura':'P8STIC', 'Satisfaccion_Eco':'P13STGBS.B'}
def IndicePrep(dicc):
    lista = []
    indice = data[(data.IDENPA == 604) | (data.IDENPA == 170)]
    indice = indice[list(control.values())+list(otras.values())+list(dicc.values())]
    Transform_date(indice)
    PaisNum(indice)
    FechaNum(indice)
    Dummy(indice)
    pre = []
    for j in indice.columns:
        #Remplazar por pregunta

        if j in list(dicc.values()):
            indice[j] = indice[j].replace([-1, -2, -3, -4], [None, None, None, None])
            # indice.dropna(inplace =True, how = 'any')
            # indice.reset_index(drop=True, inplace=True)
            indice[j] = (indice[j] - indice[j].min()) / (indice[j].max() - indice[j].min())

            # u = pd.DataFrame(indice[[j,'Fecha_num', 'IDENPA_num', 'Fecha_lar', 'IDENPA']])
            # p = datetime.strptime('03-07-18', '%d-%m-%y')
            # u = u[(u.Fecha_lar >= p - timedelta(days=4)) & (u.Fecha_lar <= p + timedelta(days=5))]
            # u['Pregunta'] = '{}'.format(j)
            # u.reset_index(drop =True, inplace = True)
            pre.append(j)
    cul = pre + ['Fecha_num', 'T_C']
    base = indice[cul]

    return base
l = IndicePrep(p_e)
g = IndicePrep(preguntas)

l.to_excel('Economia.xlsx')
g.to_excel('Gobierno.xlsx')

#%%
#Paso extra donde queriamos ver la posibilidad de utilizar otras variables
#Base de datos de los bienes

bienes_col = {'Casa propia':'S21.A', 'Computador':'S21.B', 'Lavarropas':'S21.C', 'Teléfono Red Fija':'S21.E',
'Teléfono celular/móvil':'S21.F', 'Auto':'S21.G', 'Agua caliente':'S21.I', ' Alcantarillado/Cloacas':'S21.J',
'Al menos una comida caliente al día':'S21.K', 'Agua potable':'S21.L', ' Smartphone':'S21.M',
'Conexión a Internet en el hogar':'S21.N', 'Conexión a Internet en el hogar':'S21.O', 'Calefaccion':'S21.P'}

bienes = data[(data.IDENPA == 604) | (data.IDENPA == 170)]
bienes = bienes[bienes_col]
Nulos(bienes)
for j in bienes.columns:
    bienes[j] = bienes[j].replace([-1, -2, -3, -4], [None, None, None, None])
    bienes.dropna(inplace =True, how = 'any')
    bienes.reset_index(drop=True, inplace=True)
    bienes[j].replace(2,0, inplace =True)

#%%

#Agrupamos las medias de los valores por dia. Estos datos son utilizados para crear la grafica de tendencias paralelas.
colombia_gov = indice_gov[indice_gov.IDENPA == 170].groupby(['Fecha_lar']).mean()
peru_gov = indice_gov[indice_gov.IDENPA == 604].groupby(['Fecha_lar']).mean()

colombia_eco = indice_eco[indice_eco.IDENPA == 170].groupby(['Fecha_lar']).mean()
peru_eco = indice_eco[indice_eco.IDENPA == 604].groupby(['Fecha_lar']).mean()

colombia_gov.to_excel('col_gov.xlsx')
peru_gov.to_excel('per_gov.xlsx')

colombia_eco.to_excel('col_eco.xlsx')
peru_eco.to_excel('per_eco.xlsx')

#%%


# Self Number
# https://www.acmicpc.net/problem/4673

from functools import reduce

numbers = list(range(1, 10001))

def d(n):
    return n + reduce(lambda x, y: x + y, list(map(int, list(str(n)))))

for i in numbers:
    if i != 0:
        next = d(i)
        # numbers.remove(1)

        while True:
            if next > 10000:
                break
            numbers[next-1] = 0
            next = d(next)

for i in numbers:
    if i != 0:
        print(i)


import serial
import time
import calendar

connected = False

ser = serial.Serial("COM3", 9600)
ser.close()
ser.open()

while not connected:
  serin = ser.read()
  connected = True

#ser.write("1")

#while ser.read() == '1':
#  ser.read()

while True:
  string = "T" + str(calendar.timegm(time.localtime())) + "\n"
  ser.write(string)
  time.sleep(60*5)

ser.close()


#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2021/9/12 11:23
# @Author  : 1823218990@qq.com
# @File    : thread_performance
# @Software: Pycharm
"""
functinon pass:
Starting tests
non_threaded (1 iters)  0.000001 seconds
threaded (1 threads)    0.000110 seconds
Iterations complete
non_threaded (2 iters)  0.000002 seconds
threaded (2 threads)    0.000213 seconds
Iterations complete
non_threaded (4 iters)  0.000002 seconds
threaded (4 threads)    0.000372 seconds
Iterations complete
non_threaded (8 iters)  0.000005 seconds
threaded (8 threads)    0.000741 seconds
Iterations complete
"""
"""
function:
    a, b = 0, 1
    for i in range(10000):
        a, b = b, a + b
Starting tests
non_threaded (1 iters)  0.002149 seconds
threaded (1 threads)    0.002347 seconds
Iterations complete
non_threaded (2 iters)  0.004427 seconds
threaded (2 threads)    0.004963 seconds
Iterations complete
non_threaded (4 iters)  0.009055 seconds
threaded (4 threads)    0.009981 seconds
Iterations complete
non_threaded (8 iters)  0.020102 seconds
threaded (8 threads)    0.021710 seconds
Iterations complete

------------

Process finished with exit code 0

"""

from threading import Thread

class threads_object(Thread):
    def run(self):
        function_to_run()

class nothreads_object(object):
    def run(self):
        function_to_run()

def non_threaded(num_iter):
    funcs = []
    for i in range(int(num_iter)):
        funcs.append(nothreads_object())
    for i in funcs:
        i.run()

def threaded(num_threads):
    funcs = []
    for i in range(int(num_threads)):
        funcs.append(threads_object())
    for i in funcs:
        i.start()
    for i in funcs:
        i.join()

# def function_to_run():
#     fh = open("E:\\mountdisk.sh", "rb")
#     size = 4096
#     for i in range(10000):
#         fh.read(size)
def function_to_run():
    import urllib.request
    for i in range(10):
        with urllib.request.urlopen("https://www.baidu.com/")as f:
            f.read(1024)

def show_results(func_name, results):
    print("%-23s %4.6f seconds" % (func_name, results))

def main1():
    import sys
    from timeit import Timer
    repeat = 100
    number = 1
    num_threads = [1, 2, 4, 8]
    print('Starting tests')
    for i in num_threads:
        t = Timer("non_threaded(%s)" % i, "from __main__ import non_threaded")
        best_result = min(t.repeat(repeat=repeat, number=number))
        show_results("non_threaded (%s iters)" % i, best_result)
        t = Timer("threaded(%s)" % i, "from __main__ import threaded")
        best_result = min(t.repeat(repeat=repeat, number=number))
        show_results("threaded (%s threads)" % i, best_result)
        print('Iterations complete')

if __name__ == "__main__":
    main1()
    print("\n------------\n")


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

# # Converting categorical columns to binary

# In[ ]:

import os
import pandas as pd
dir=os.getcwd()
new_dir=dir[:-4]+'\data'
df9=pd.read_csv(new_dir+"\DB_1.csv")
df9['Winner']=(df9['Winner']==df9['Team1'])*1
df9['Toss_won']=(df9['Toss_won']==df9['Team1'])*1
df9['Decision']=(df9['Decision']=='bowl')*1
df9.to_csv(new_dir+"\DB.csv",index=False)


class Solution:
    def fourSum(self, nums: List[int], target: int) -> List[List[int]]:
        nums.sort()
        n = len(nums)
        res = []
        for i in range(n-3):
            if i > 0 and nums[i] == nums[i-1]:
                continue
            if nums[i] + nums[i+1] + nums[i+2] + nums[i+3] > target:
                break
            if nums[i] + nums[n-3] + nums[n-2] + nums[n-1] < target:
                continue
            for j in range(i+1, n-2):
                if j > i+1 and nums[j] == nums[j-1]:
                    continue
                if nums[i] + nums[j] + nums[j+1] + nums[j+2] > target:
                    break
                if nums[i] + nums[j] + nums[n-2] + nums[n-1] < target:
                    continue
                left, right = j+1, n-1
                while left < right:
                    curr_sum = nums[i] + nums[j] + nums[left] + nums[right]
                    if curr_sum == target:
                        res.append([nums[i], nums[j], nums[left], nums[right]])
                        while left < right and nums[left] == nums[left+1]:
                            left += 1
                        while left < right and nums[right] == nums[right-1]:
                            right -= 1
                        left += 1
                        right -= 1
                    elif curr_sum < target:
                        left += 1
                    else:
                        right -= 1
        return res


import rospy
from geometry_msgs.msg import PoseStamped, Point
from gazebo_msgs.msg import ModelStates, ModelState

class RealStatesSubPub(object):

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

        self.real_agent_pose_stamped_subs = {}
        self.all_agents_prev_pose_stamped = {}
        self.all_agents_model_state = {}
        self.agent_goals = {}

        self.real_agent_model_state_pubs = {}
        self.real_agent_goal_pubs = {}
        self.all_agents_pub = None

        self.setup_subs()
        self.setup_pubs()

    def setup_subs(self):

        for agent_name in self.agent_names:
            self.real_agent_pose_stamped_subs[agent_name] = rospy.Subscriber('/' + agent_name + '/pose', PoseStamped,
                                                                             self.get_agents, callback_args=agent_name)
            try:
                goal = rospy.get_param('/real_experiment_node/' + agent_name + '/goal')
            except KeyError:
                print("You need to set the ROS Parameter Server first for /real_experiment_node/" + agent_name +
                      "/goal\n")
                return
            self.agent_goals[agent_name] = goal

    def get_agents(self, msg, agent_name):

        # First time through callback, create ModelState in all agents dict
        if agent_name not in self.all_agents_model_state:
            self.all_agents_model_state[agent_name] = ModelState()

        curr_time = msg.header.stamp.secs + (msg.header.stamp.nsecs * 1e-9)
        # If it's not the first callback
        if agent_name in self.all_agents_prev_pose_stamped:
            prev_pose_stamped = self.all_agents_prev_pose_stamped[agent_name]
            dx = msg.pose.position.x - prev_pose_stamped.pose.position.x
            dy = msg.pose.position.y - prev_pose_stamped.pose.position.y
            prev_time = prev_pose_stamped.header.stamp.secs + (prev_pose_stamped.header.stamp.nsecs * 1e-9)
            dt = curr_time - prev_time
            self.all_agents_model_state[agent_name].twist.linear.x = dx / dt
            self.all_agents_model_state[agent_name].twist.linear.y = dy / dt

        self.all_agents_prev_pose_stamped[agent_name] = PoseStamped()
        self.all_agents_prev_pose_stamped[agent_name].header = msg.header
        self.all_agents_prev_pose_stamped[agent_name].pose = msg.pose

        self.all_agents_model_state[agent_name].pose = msg.pose

    def setup_pubs(self):
        for agent_name in self.agent_names:
            self.real_agent_model_state_pubs[agent_name] = rospy.Publisher('/' + agent_name + '/model_state',
                                                                           ModelState, queue_size=10)
            self.real_agent_goal_pubs[agent_name] = rospy.Publisher('/' + agent_name + '/goal', Point, queue_size=10)
        self.all_agents_pub = rospy.Publisher('/all_agents/model_states', ModelStates, queue_size=10)

    def publish_states(self):
        name_list = []
        pose_list = []
        twist_list = []

        for agent_name in self.agent_names:
            agent = self.all_agents_model_state[agent_name]

            name_list.append(agent.model_name)
            pose_list.append(agent.pose)
            twist_list.append(agent.twist)
            self.real_agent_model_state_pubs[agent_name].publish(agent)

        all_agents = ModelStates()
        all_agents.name = name_list
        all_agents.pose = pose_list
        all_agents.twist = twist_list
        self.all_agents_pub.publish(all_agents)

    def publish_goals(self):
        """
        Publishes the goals assigned at initializtion to /agent_name/goal. Does not publish goals that have been modified since
        initialization.
        """
        for agent_name in self.agent_goals:
            gx, gy = self.agent_goals[agent_name]
            goal = Point()
            goal.x = gx
            goal.y = gy
            self.real_agent_goal_pubs[agent_name].publish(goal)


from user.serialize import serialize_user_basic

def plustag_serialize(plustag):
    return {
        'id': plustag.pk,
        'user_receiver_plus': serialize_user_basic(plustag.user_receive_plus) if plustag.user_receive_plus else None,
        'user_send_plus': serialize_user_basic(plustag.user_send_plus) if plustag.user_send_plus else None,
        'status': plustag.status.id if plustag.status else None,
        'plustag_name': plustag.plustag_name,
        'votes': plustag.votes,
        'create_time': plustag.create_time.isoformat(),
        'uservotes': plustag_vote_serialize(plustag)
        }

def plustag_status_serialize(plustag):
    return {
        'id': plustag.pk,
        'user_send_plus': serialize_user_basic(plustag.user_send_plus) if plustag.user_send_plus else None,
        'status': plustag.status.id if plustag.status else None,
        'plustag_name': plustag.plustag_name,
        'votes': plustag.votes,
        'create_time': plustag.create_time.isoformat(),
        'uservotes': plustag_vote_serialize(plustag)
        }

def plustag_vote_serialize(plustag):
    pv = plustag.plustag_plustagvote_plustag.filter(been_vote = True)
    data = []
    for i in pv:
        data.append({
            'user_vote': serialize_user_basic(i.user_vote),
            'been_vote': i.been_vote,
            'time': i.time.isoformat()
            })
    return data


'''
https://www.codewars.com/kata/54d7660d2daf68c619000d95/python
'''
import math
import functools

def convert_fracts(lst):
    lcm = lambda a, b : abs(a*b) // math.gcd(a, b)
    tmp_list = list(map(lambda x : x[1] ,list(lst)))
    lcm_num = functools.reduce(lcm,tmp_list)
    return list(map(lambda x : [x[0] * lcm_num // x[1], lcm_num] , list(lst)))

'''
import math
def convert_fracts(lst):
    b = []
    if lst == b: return b
    greater = max([l[1] for l in lst if l[1]])
    greater2 = max([l[1] if l[1] != greater else 0 for l in lst if l[1]])
    greater3 = max([l[1] if l[1] != greater and greater2 else 0 for l in lst if l[1]])
    lcm = greater*greater2 // math.gcd(greater2 ,greater)
    if lcm%greater3 != 0: lcm = lcm*greater3 // math.gcd(greater3 ,lcm)
    for l in lst:
        l[0]=int(l[0]*lcm/l[1])
        l[1]=int(l[1]*lcm/l[1])
        b.append(l)
    return b
'''

'''
def convert_fracts(lst):
    b = []
    if lst == b: return b
    greater = max([l[1] for l in lst if l[1]])
    #print(greater)
    while True:
        if greater%lst[0][1] == 0 and greater%lst[1][1] == 0 and greater%lst[2][1] == 0:
            for l in lst:
                l[0]=int(l[0]*greater/l[1])
                l[1]=int(l[1]*greater/l[1])
                b.append(l)
            return b
        greater += 1
'''

'''
if lst[0][1] > lst[1][1]: greater = lst[0][1]
elif lst[0][1] > lst[2][1]: greater = lst[0][1]
elif lst[1][1] > lst[0][1]: greater = lst[1][1]
elif lst[1][1] > lst[2][1]: greater = lst[1][1]
elif lst[2][1] > lst[0][1]: greater = lst[2][1]
elif lst[2][1] > lst[1][1]: greater = lst[2][1]
'''

'''
def convert_fracts(lst):
    content = []
    for i in range(100000000000000000):
        if i:
            n = 0
            for lst_from_lst in lst:
                if i%lst_from_lst[1] == 0:
                    n += 1
            if n == len(lst):
                for l in lst:
                    l[0]=int(l[0]*i/l[1])
                    l[1]=int(l[1]*i/l[1])
                    content.append(l)
                return content
'''
a = [[1, 2], [1, 3], [1, 4]]
print(convert_fracts(a))


from django.contrib import admin
from django.urls import path
from .views import TopView, ClothingDetail, ClothingRegister, ClothingDelete, ClothingUpdate, ClothingList, signupfunc, loginfunc, logoutfunc, form, forecast

urlpatterns = [
    path('signup/', signupfunc, name='signup'),
    path('login/', loginfunc, name='login'),
    path('logout/', logoutfunc, name='logout'),
    path('top/', TopView.as_view(), name='top'),
    path('ClothingDetail/<int:pk>', ClothingDetail.as_view(), name='ClothingDetail'),
    path('ClothingRegister/', ClothingRegister.as_view(), name='ClothingRegister'),
    path('ClothingDelete/<int:pk>', ClothingDelete.as_view(), name='ClothingDelete'),
    path('ClothingUpdate/<int:pk>', ClothingUpdate.as_view(), name='ClothingUpdate'),
    path('Clothinglist/', ClothingList.as_view(), name='ClothingList'),
    path('signup/', signupfunc, name='signup'),
    path('', forecast, name='weather'),
    path('form', form, name='form')
]


from sqlalchemy.exc import InvalidRequestError

import logging
from multiprocessing import cpu_count, Pool
from pathlib import Path

from bitglitter.config.palettemodels import Palette
from bitglitter.config.readmodels.streamread import StreamRead
from bitglitter.read.process_state.videoframegenerator import video_frame_generator
from bitglitter.read.process_state.imageframeprocessor import ImageFrameProcessor
from bitglitter.read.process_state.multiprocess_state_generator import image_state_generator, video_state_generator
from bitglitter.read.process_state.videoframeprocessor import VideoFrameProcessor
from bitglitter.utilities.read import flush_inactive_frames

def frame_read_handler(input_path, output_directory, input_type, bad_frame_strikes, max_cpu_cores,
                       block_height_override, block_width_override, decryption_key, scrypt_n, scrypt_r, scrypt_p,
                       temp_save_directory, stop_at_metadata_load, auto_unpackage_stream, auto_delete_finished_stream,
                       save_statistics, valid_image_formats):
    logging.info(f'Processing {input_path}...')

    #  Initializing variables that will be in all frame_process() calls
    initializer_palette_a = Palette.query.filter(Palette.palette_id == '1').first()
    initializer_palette_b = Palette.query.filter(Palette.palette_id == '11').first()
    initializer_palette_a_color_set = initializer_palette_a.convert_colors_to_tuple()
    initializer_palette_b_color_set = initializer_palette_b.convert_colors_to_tuple()
    initializer_palette_a_dict = initializer_palette_a.return_decoder()
    initializer_palette_b_dict = initializer_palette_b.return_decoder()

    stream_palette = None
    stream_palette_dict = None
    stream_palette_color_set = None

    initial_state_dict = {'output_directory': output_directory, 'block_height_override': block_height_override,
                          'block_width_override': block_width_override, 'decryption_key': decryption_key, 'scrypt_n':
                          scrypt_n, 'scrypt_r': scrypt_r, 'scrypt_p': scrypt_p, 'temp_save_directory':
                          temp_save_directory, 'initializer_palette_a': initializer_palette_a,
                          'initializer_palette_a_color_set': initializer_palette_a_color_set,
                          'initializer_palette_b_color_set': initializer_palette_b_color_set,
                          'initializer_palette_a_dict': initializer_palette_a_dict, 'initializer_palette_b_dict':
                          initializer_palette_b_dict, 'auto_delete_finished_stream': auto_delete_finished_stream,
                          'stop_at_metadata_load': stop_at_metadata_load, 'save_statistics': save_statistics,
                          'auto_unpackage_stream': auto_unpackage_stream, 'sequential': True}

    # Multicore setup
    if max_cpu_cores == 0 or max_cpu_cores >= cpu_count():
        cpu_pool_size = cpu_count()
    else:
        cpu_pool_size = max_cpu_cores

    frame_strikes_this_session = 0
    image_strike_limit_hit = False
    image_metadata_checkpoint_data = None
    frame_read_results = {'active_sessions_this_stream': []}

    if input_type == 'video':

        frame_generator = video_frame_generator(input_path)
        total_video_frames = next(frame_generator)
        initial_state_dict['total_frames'] = total_video_frames
        logging.info(f'{total_video_frames} frame(s) detected in video file.')
        initial_state_dict['mode'] = 'video'

        # Processing frames in a single process until all metadata has been received, then switch to multicore
        logging.info('Starting single core sequential decoding until metadata captured...')
        for frame_data in frame_generator:
            initial_state_dict['frame'] = frame_data['frame']
            initial_state_dict['current_frame_position'] = frame_data['current_frame_position']
            video_frame_processor = VideoFrameProcessor(initial_state_dict)

            # Skip if all frames completed
            if video_frame_processor.skip_process:
                frame_read_results['active_sessions_this_stream'].append(video_frame_processor.stream_sha256)
                break

            # Errors
            if 'error' in video_frame_processor.frame_errors:
                # Session-ending error, such as a metadata frame being corrupted
                if 'fatal' in video_frame_processor.frame_errors:
                    logging.warning('Cannot continue.')
                    return {'error': True}
                if bad_frame_strikes:  # Corrupted frame, skipping to next one
                    frame_strikes_this_session += 1
                    logging.warning(f'Bad frame strike {frame_strikes_this_session}/{bad_frame_strikes}')
                    if frame_strikes_this_session >= bad_frame_strikes:
                        logging.warning('Reached frame strike limit.  Aborting...')
                        return {'error': True}

            if frame_data['current_frame_position'] == 1:
                stream_read = video_frame_processor.stream_read
                initial_state_dict['stream_read'] = stream_read

            # Metadata return
            if video_frame_processor.metadata and stream_read.stop_at_metadata_load:
                return {'metadata': video_frame_processor.metadata}

            # Stream palette load
            if video_frame_processor.stream_palette and video_frame_processor.stream_palette_loaded_this_frame:
                stream_palette = video_frame_processor.stream_palette
                stream_palette_dict = video_frame_processor.stream_palette_dict
                stream_palette_color_set = video_frame_processor.stream_palette_color_set
                initial_state_dict['stream_palette'] = stream_palette
                initial_state_dict['stream_palette_dict'] = stream_palette_dict
                initial_state_dict['stream_palette_color_set'] = stream_palette_color_set

            # Headers are decoded, can switch to multiprocessing
            if stream_read.palette_header_complete and stream_read.metadata_header_complete:
                frame_read_results['active_sessions_this_stream']\
                    .append(video_frame_processor.stream_read.stream_sha256)
                break

        # Begin multicore frame decode
        if not video_frame_processor.skip_process:
            with Pool(processes=cpu_pool_size) as worker_pool:
                logging.info(f'Metadata headers fully decoded, now decoding on {cpu_pool_size} CPU core(s)...')
                for multicore_read_results in worker_pool.imap(VideoFrameProcessor,
                                                           video_state_generator(frame_generator, stream_read,
                                                           save_statistics, initializer_palette_a,
                                                           initializer_palette_a_dict, initializer_palette_a_color_set,
                                                           total_video_frames, stream_palette, stream_palette_dict,
                                                           stream_palette_color_set)):

                    if 'error' in multicore_read_results.frame_errors:
                        if bad_frame_strikes:  # Corrupted frame, skipping to next one
                            frame_strikes_this_session += 1
                            logging.warning(f'Bad frame strike {frame_strikes_this_session}/{bad_frame_strikes}')
                            if frame_strikes_this_session >= bad_frame_strikes:
                                logging.warning('Reached frame strike limit.  Aborting...')
                                return {'error': True}

    elif input_type == 'image':

        # Normalizing the different forms of input path into a common list format format
        if isinstance(input_path, list):
            input_list = input_path
        elif isinstance(input_path, str):
            path = Path(input_path)
            if path.is_dir():
                input_list = []
                for file_format in valid_image_formats:
                    for whitelisted_file in path.rglob(file_format):
                        input_list.append(str(whitelisted_file))
            else:
                input_list = [input_path]

        # Begin multicore frame decode
        image_metadata_checkpoint_data = None
        image_strike_limit_hit = False
        with Pool(processes=cpu_pool_size) as worker_pool:
            logging.info(f'Decoding on {cpu_pool_size} CPU core(s)...')
            for multicore_read_results in worker_pool.imap(ImageFrameProcessor, image_state_generator(input_list,
                                                                                initial_state_dict)):
                if 'error' in multicore_read_results.frame_errors:
                    if bad_frame_strikes:  # Corrupted frame, skipping to next one
                        frame_strikes_this_session += 1
                        logging.warning(f'Bad frame strike {frame_strikes_this_session}/{bad_frame_strikes}'
                                        f' ({multicore_read_results.file_name})')
                        if frame_strikes_this_session >= bad_frame_strikes:
                            logging.warning('Reached frame strike limit.  Aborting...')
                            image_strike_limit_hit = True
                            break

                if multicore_read_results.stream_sha256:
                    if multicore_read_results.stream_sha256 not in frame_read_results['active_sessions_this_stream']:
                        frame_read_results['active_sessions_this_stream'].append(multicore_read_results.stream_sha256)

                if multicore_read_results.metadata and multicore_read_results.stream_read.stop_at_metadata_load:
                    image_metadata_checkpoint_data = multicore_read_results.metadata
                    break

    # Outside the scope of image multiprocessing to work properly
    if image_strike_limit_hit:
        return {'error': True}
    if image_metadata_checkpoint_data:
        return {'metadata': image_metadata_checkpoint_data}

    logging.info('Frame scanning complete.')

    #  Remove incomplete frames from db
    flush_inactive_frames()

    # Closing active sessions and unpackaging streams if its set to:
    extracted_file_count = 0
    active_reads_this_session = StreamRead.query.filter(StreamRead.active_this_session == True).all()
    unpackaging_this_session = False
    if active_reads_this_session:
        logging.info(f'{len(active_reads_this_session)} active stream(s) this session.')
        frame_read_results['unpackage_results'] = {}
        for stream_read in active_reads_this_session:
            stream_read.completed_frame_count_update()
            if stream_read.auto_unpackage_stream:
                unpackaging_this_session = True
                unpackage_results, total_unpackaged = stream_read.attempt_unpackage(temp_save_directory)
                extracted_file_count += total_unpackaged
                frame_read_results['unpackage_results'][stream_read.stream_sha256] = unpackage_results
                stream_read.autodelete_attempt()
            else:
                stream_read.check_file_eligibility()
            try:
                stream_read.session_activity(False)
            except InvalidRequestError:
                pass
        if unpackaging_this_session:
            logging.info('File unpackaging complete.')
    frame_read_results['extracted_file_count'] = extracted_file_count

    return {'frame_read_results': frame_read_results}


import numpy as np
from . import utils

def init():
    _gaug2013night = "".join(
        [
            "@article{gaug2013night,"
            "title={Night Sky Background Analysis for the "
            "Cherenkov Telescope Array using the Atmoscope instrument},"
            "author={Gaug, Markus and others},"
            "journal={arXiv preprint arXiv:1307.3053},"
            "year={2013}"
            "},"
            "Figure 4. The CTA icrc2013 proceeding claims the data (black solid line) "
            "of Figure 4 to be from 'Benn Ch.R., Ellison S.R., La Palma tech. note, "
            "2007' but actual I did not find ths data in the specified "
            "technical note 115. There is sth. similar but not down to this "
            "low wavelengths."
        ]
    )

    h = 6.626e-34  # [J s]
    c = 299792458

    # 1 Jy = 10^-26 J/(s m^2 Hz)
    # wavelength [nm], flux [Jy / (deg^2)] // [10^-26 J/(s m^2 Hz deg^2)]
    raw = np.array(
        [
            [240, 8],  # extrapolated
            [270, 11],  # extrapolated
            [300, 14.7],  # extrapolated
            [330, 15.7],
            [340, 20],
            [394, 30],
            [395, 145],
            [396, 30],
            [433, 40],
            [435, 81],
            [437, 40],
            [462, 50],
            [485, 60],
            [525, 80],
            [555, 97],
            [557.5, 948],
            [561, 93],
            [585, 105],
            [591, 424],
            [595, 146],
            [600, 130],
            [605, 106],
            [627, 161],
            [630, 851],
            [632, 145],
            [638, 371],
            [640, 105],
            [645, 105],
            [647, 140],
            [660, 140],
            [670, 105],
            [678, 105],
            [685, 217],
            [701, 140],
        ]
    )

    raw[:, 0] *= 1e-9  # from nm to m -> wavelength [m]

    raw[:, 1] *= 1e-26  # from Jy to J -> flux [J / (s m^2 Hz deg^2)]

    square_deg_2_steradian = (2.0 * np.pi / 360) ** 2.0
    raw[:, 1] /= square_deg_2_steradian
    # from deg^2 to sr -> flux [J / (s m^2 Hz sr)]

    f = c / raw[:, 0]
    # remove frequency
    raw[:, 1] *= f  # from Hz to 1 -> flux [J / (s m^2 1 sr)]

    lamb = raw[:, 0]
    raw[:, 1] /= lamb  # from Hz to 1 -> flux [J / (s m^2 m sr)]

    # differential flux
    raw[:, 1] /= (h * c) / raw[:, 0]  # flux [1 / (s m^2 m sr)]

    _la_palma_2013_benn = raw

    assert utils.is_strictly_monotonic_increasing(_la_palma_2013_benn[:, 0])

    return {
        "wavelength": _la_palma_2013_benn[:, 0],
        "value": _la_palma_2013_benn[:, 1],
        "units": ["m", "m^{-2} sr^{-1} s^{-1} m^{-1}"],
        "reference": _gaug2013night,
    }


import random
from logging import getLogger

import torch

logger = getLogger(__name__)

def set_seed(seed: int = 0) -> None:
    # set seed
    random.seed(seed)
    torch.manual_seed(seed)
    torch.cuda.manual_seed_all(seed)
    torch.backends.cudnn.deterministic = True

    logger.info("Finished setting up seed.")


# 84. Largest Rectangle in Histogram
# Hard

# Given an array of integers heights representing the histogram's bar height where the width of each bar is 1, return the area of the largest rectangle in the histogram.

# Input: heights = [2,1,5,6,2,3]
# Output: 10
# Explanation: The above is a histogram where width of each bar is 1.
# The largest rectangle is shown in the red area, which has an area = 10 units.

from typing import List

def largest(arr: List[int]) -> int:

    stack = []
    area = 0

    for i, h in enumerate(arr):
        start = i

        while stack and stack[-1][1] > h:
           index, last_bar_height = stack.pop()
           area = max(area, ((i - index) * last_bar_height))
           start = index
        stack.append((start, h))

    for i, h in stack:
        area = max(area, h * (len(arr) - i))

    return area

def test():
    heights = [2,1,5,6,2,3]
    assert largest(heights) == 10

def test2():
    heights = [2, 4]
    assert largest(heights) == 4

def test3():
    heights = [1, 1]
    assert largest(heights) == 2


import os
import signal
import subprocess

class ShellUtil(object):
    """
    - run shell command.
    - see also `bage_util.SshUtil`
    """

    @staticmethod
    def kill_processes(cmd):
        p = subprocess.Popen(['ps', '-ef'], stdout=subprocess.PIPE)
        out, _err = p.communicate()
        for line in out.splitlines():
            if line.count(' ' + cmd) > 0:
                pid = int(line.split(None, 2)[1])
                os.kill(pid, signal.SIGKILL)

    @staticmethod
    def call(cmd):
        try:
            p = subprocess.Popen(cmd.split(' '), stdout=subprocess.PIPE)
            out, _err = p.communicate()
            lines = []
            for line in out.splitlines():
                lines.append(line.decode('utf8'))
            return '\n'.join(lines)
        except FileNotFoundError as e:
            print("FileNotFoundError: ", e)

if __name__ == '__main__':
    # ShellUtil.kill_processes('vim')
    print(ShellUtil.call('ps -rf'))
    # ShellUtil.call('1')


import streamlit as st

# Function predicts house prices using the regression pipeline

#Credit to Ulrike Riemenschneider for providing the format for this
# page - link to repo at https://github.com/URiem/heritage-housing-PP5

def predict_price(X_live, features, sale_price_pipeline):

    # from live data, subset features related to this pipeline
    # the features are filtered using the list of features from the pipeline
    # this is to avoid a scilent fail in case the input features
    # are not in the same order as in the dataset used to train the model.
    X_live_sale_price = X_live.filter(features)

    # predict
    price_prediction = sale_price_pipeline.predict(X_live_sale_price)

    statement = (
        f"* Given the features provided for the property, the model has "
        f"  predicted a sale value of:"
    )

    # Format the value written to the page
    # Formatting taken from
    # https://github.com/t-hullis/milestone-project-heritage-housing-issues/tree/main
    if len(price_prediction) == 1:
        price = float(price_prediction.round(1))
        price = '${:,.2f}'.format(price)

        st.write(statement)
        st.write(f"**{price}**")
    else:
        st.write(
            f"* Given the features provided for the inherited properties, "
            f" the model has predicted sale values of:")
        st.write(price_prediction)

    return price_prediction


# -*- coding: utf-8 -*-
import scrapy
from jiandan.items import JiandanItem

class ArticleSpider(scrapy.Spider):
    name = 'article'
    allowed_domain = 'i.jandan.net'
    start_urls = ['http://i.jandan.net/']

    def parse(self, response):
        url_list = response.xpath("//h2[@class='thetitle']/a/@href").extract()
        for url in url_list:
            yield scrapy.Request(url, callback=self.parse_detail)

        next_url = response.xpath("//div[@class='wp-pagenavi']/a[last()]/@href").extract_first()
        if next_url is not None:
            next_url = response.urljoin(next_url)
            yield scrapy.Request(next_url, callback=self.parse)

    def parse_detail(self, response):
        item = JiandanItem()
        item["title"] = response.xpath("//h1[@class='thetitle']/a/text()").extract_first()
        item["name"] = response.xpath("//div[@class='postinfo']/text()").extract()[-1].split("@")[0].strip()
        item["date"] = response.xpath("//div[@class='postinfo']/text()").extract()[-1].split("@")[1].strip()
        item["content"] = ''.join(response.xpath("//div[@class='entry']/p/text()").extract()).replace("\n", "").strip()
        if '无聊图' in item["title"]:
            item["content"] = ';'.join(response.xpath("//div[@class='entry']/p/img/@data-original").extract())
        print(item)
        yield item


import sys
import time

import pyotp
import pyperclip

def run(token: str ='', _continue: bool=False):
	if token == '':
		with open('secret', 'r') as fin:
			token = fin.read().splitlines()[0]
	totp = pyotp.TOTP(token)

	if _continue:
		print('This program will continue copy totp code to clipboard,\npress Control+C to terminate is program.\n')

	betk = tk = totp.now()
	print(tk)
	pyperclip.copy(tk)
	while _continue:
		try:
			time.sleep(5)
		except KeyboardInterrupt:
			pyperclip.copy('')
			break
		tk = totp.now()
		if tk == betk:
			continue
		pyperclip.copy(tk)
		print(tk)
		betk = tk

if __name__ == "__main__":
	if len(sys.argv) == 1:
		run()
	elif len(sys.argv) == 2:
		if sys.argv[1] == '-t':
			run(_continue=True)
		else:
			run(sys.argv[1])
	else:
		run(sys.argv[1], '-t' in sys.argv)