from __future__ import annotations

import dataclasses
from typing import TYPE_CHECKING
from unittest.mock import MagicMock, PropertyMock

import pytest

from randovania.exporter import pickup_exporter
from randovania.game_description import default_database
from randovania.game_description.assignment import PickupTarget
from randovania.game_description.resources.pickup_index import PickupIndex
from randovania.games.dread.exporter.patch_data_factory import (
    DreadAcquiredMemo,
    DreadPatchDataFactory,
    get_resources_for_details,
)
from randovania.games.dread.layout.dread_cosmetic_patches import DreadCosmeticPatches, DreadMissileCosmeticType
from randovania.games.game import RandovaniaGame
from randovania.generator.pickup_pool import pickup_creator
from randovania.interface_common.players_configuration import PlayersConfiguration
from randovania.layout.base.ammo_pickup_state import AmmoPickupState
from randovania.layout.base.pickup_model import PickupModelStyle
from randovania.layout.base.standard_pickup_state import StandardPickupState
from randovania.layout.layout_description import LayoutDescription
from randovania.lib import json_lib

if TYPE_CHECKING:
    from randovania.layout.preset import Preset

@pytest.mark.parametrize(
    ("rdvgame_filename", "expected_results_filename", "num_of_players"),
    [
        ("starter_preset.rdvgame", "starter_preset.json", 1),  # starter preset
        ("crazy_settings.rdvgame", "crazy_settings.json", 1),  # crazy settings
        ("dread_dread_multiworld.rdvgame", "dread_dread_multiworld_expected.json", 2),  # dread-dread multi
        ("dread_prime1_multiworld.rdvgame", "dread_prime1_multiworld_expected.json", 2),  # dread-prime1 multi
        ("elevator_rando.rdvgame", "elevator_rando.json", 1),  # elevator_rando multi
    ],
)
def test_create_patch_data(test_files_dir, rdvgame_filename, expected_results_filename, num_of_players, mocker):
    # Setup
    rdvgame = test_files_dir.joinpath("log_files", "dread", rdvgame_filename)
    players_config = PlayersConfiguration(0, {i: f"Player {i + 1}" for i in range(num_of_players)})
    description = LayoutDescription.from_file(rdvgame)
    cosmetic_patches = DreadCosmeticPatches()
    mocker.patch(
        "randovania.layout.layout_description.LayoutDescription.shareable_word_hash",
        new_callable=PropertyMock,
        return_value="Words Hash",
    )
    mocker.patch(
        "randovania.layout.layout_description.LayoutDescription.shareable_hash",
        new_callable=PropertyMock,
        return_value="$$$$$",
    )

    # Run
    data = DreadPatchDataFactory(description, players_config, cosmetic_patches).create_data()

    # Expected Result
    expected_results_path = test_files_dir.joinpath("patcher_data", "dread", expected_results_filename)
    expected_data = json_lib.read_path(expected_results_path)

    # Uncomment to easily view diff of failed test
    # json_lib.write_path(expected_results_path, data); assert False

    assert data == expected_data

def _preset_with_locked_pb(preset: Preset, locked: bool):
    pickup_database = default_database.pickup_database_for_game(RandovaniaGame.METROID_DREAD)
    preset = dataclasses.replace(
        preset,
        configuration=dataclasses.replace(
            preset.configuration,
            ammo_configuration=preset.configuration.ammo_pickup_configuration.replace_state_for_ammo(
                pickup_database.ammo_pickups["Power Bomb Tank"],
                AmmoPickupState(requires_main_item=locked),
            ),
        ),
    )
    return preset

@pytest.mark.parametrize("locked", [False, True])
def test_pickup_data_for_pb_expansion(locked, dread_game_description, preset_manager):
    pickup_database = default_database.pickup_database_for_game(RandovaniaGame.METROID_DREAD)
    resource_db = dread_game_description.resource_database

    # Setup
    pickup = pickup_creator.create_ammo_pickup(
        pickup_database.ammo_pickups["Power Bomb Tank"],
        [2],
        locked,
        resource_db,
    )

    creator = pickup_exporter.PickupExporterSolo(DreadAcquiredMemo.with_expansion_text(), RandovaniaGame.METROID_DREAD)

    # Run
    details = creator.export(PickupIndex(0), PickupTarget(pickup, 0), pickup, PickupModelStyle.ALL_VISIBLE)
    result = get_resources_for_details(details.original_pickup, details.conditional_resources, details.other_player)

    # Assert
    assert result == [
        [
            {"item_id": "ITEM_WEAPON_POWER_BOMB_MAX", "quantity": 2},
        ]
        if locked
        else [
            {"item_id": "ITEM_WEAPON_POWER_BOMB_MAX", "quantity": 2},
            {"item_id": "ITEM_WEAPON_POWER_BOMB", "quantity": 1},
        ]
    ]

@pytest.mark.parametrize("locked", [False, True])
def test_pickup_data_for_main_pb(locked, dread_game_description, preset_manager):
    pickup_database = default_database.pickup_database_for_game(RandovaniaGame.METROID_DREAD)
    resource_db = dread_game_description.resource_database

    # Setup
    pickup = pickup_creator.create_standard_pickup(
        pickup_database.standard_pickups["Power Bomb"],
        StandardPickupState(included_ammo=(3,)),
        resource_database=resource_db,
        ammo=pickup_database.ammo_pickups["Power Bomb Tank"],
        ammo_requires_main_item=locked,
    )

    creator = pickup_exporter.PickupExporterSolo(DreadAcquiredMemo.with_expansion_text(), RandovaniaGame.METROID_DREAD)

    # Run
    details = creator.export(PickupIndex(0), PickupTarget(pickup, 0), pickup, PickupModelStyle.ALL_VISIBLE)
    result = get_resources_for_details(details.original_pickup, details.conditional_resources, details.other_player)

    # Assert
    assert result == [
        [
            {"item_id": "ITEM_WEAPON_POWER_BOMB", "quantity": 1},
            {"item_id": "ITEM_WEAPON_POWER_BOMB_MAX", "quantity": 3},
        ]
    ]

def test_pickup_data_for_recolored_missiles(dread_game_description, preset_manager):
    pickup_database = default_database.pickup_database_for_game(RandovaniaGame.METROID_DREAD)
    resource_db = dread_game_description.resource_database

    preset = preset_manager.default_preset_for_game(RandovaniaGame.METROID_DREAD).get_preset()
    description = MagicMock(spec=LayoutDescription)
    description.all_patches = {0: MagicMock()}
    description.get_preset.return_value = preset
    description.get_seed_for_player.return_value = 1000
    cosmetics = DreadCosmeticPatches(missile_cosmetic=DreadMissileCosmeticType.PRIDE)

    # Setup
    pickup = pickup_creator.create_ammo_pickup(
        pickup_database.ammo_pickups["Missile Tank"], (2,), False, resource_database=resource_db
    )

    factory = DreadPatchDataFactory(description, PlayersConfiguration(0, {0: "Dread"}), cosmetics)
    creator = pickup_exporter.PickupExporterSolo(DreadAcquiredMemo.with_expansion_text(), RandovaniaGame.METROID_DREAD)

    # Run
    details = creator.export(PickupIndex(0), PickupTarget(pickup, 0), pickup, PickupModelStyle.ALL_VISIBLE)
    result = factory._pickup_detail_for_target(details)

    # Assert
    assert result == {
        "pickup_type": "actor",
        "caption": "Missile Tank acquired.\nMissile capacity increased by 2.",
        "resources": [[{"item_id": "ITEM_WEAPON_MISSILE_MAX", "quantity": 2}]],
        "pickup_actor": {"scenario": "s010_cave", "layer": "default", "actor": "ItemSphere_ChargeBeam"},
        "model": ["item_missiletank_green"],
        "map_icon": {
            "icon_id": "item_missiletank",
            "original_actor": {"actor": "powerup_chargebeam", "layer": "default", "scenario": "s010_cave"},
        },
    }

def test_pickup_data_for_a_major(dread_game_description, preset_manager):
    pickup_database = default_database.pickup_database_for_game(RandovaniaGame.METROID_DREAD)
    resource_db = dread_game_description.resource_database

    preset = preset_manager.default_preset_for_game(RandovaniaGame.METROID_DREAD).get_preset()
    description = MagicMock(spec=LayoutDescription)
    description.all_patches = {0: MagicMock()}
    description.get_preset.return_value = preset
    description.get_seed_for_player.return_value = 1000

    # Setup
    pickup = pickup_creator.create_standard_pickup(
        pickup_database.standard_pickups["Speed Booster"],
        StandardPickupState(),
        resource_database=resource_db,
        ammo=None,
        ammo_requires_main_item=False,
    )

    factory = DreadPatchDataFactory(description, PlayersConfiguration(0, {0: "Dread"}), MagicMock())
    creator = pickup_exporter.PickupExporterSolo(DreadAcquiredMemo.with_expansion_text(), RandovaniaGame.METROID_DREAD)

    # Run
    details = creator.export(PickupIndex(0), PickupTarget(pickup, 0), pickup, PickupModelStyle.ALL_VISIBLE)
    result = factory._pickup_detail_for_target(details)

    # Assert
    assert result == {
        "pickup_type": "actor",
        "caption": "Speed Booster acquired.",
        "resources": [[{"item_id": "ITEM_SPEED_BOOSTER", "quantity": 1}]],
        "pickup_actor": {"scenario": "s010_cave", "layer": "default", "actor": "ItemSphere_ChargeBeam"},
        "model": ["powerup_speedbooster"],
        "map_icon": {
            "icon_id": "powerup_speedbooster",
            "original_actor": {"actor": "powerup_chargebeam", "layer": "default", "scenario": "s010_cave"},
        },
    }

@pytest.fixture()
def _setup_and_teardown_for_wrong_custom_spawn():
    # modify the default start to have no collision_camera (asset_id) and no vanilla
    # actor name for a start point
    game_desc = default_database.game_description_for(RandovaniaGame.METROID_DREAD)
    region = game_desc.region_list.region_with_name("Artaria")
    area = region.area_by_name("Intro Room")
    node = area.node_with_name("Start Point")
    modified_node = dataclasses.replace(node, extra={})
    area.nodes.remove(node)
    area.nodes.append(modified_node)
    asset_id = area.extra["asset_id"]
    del area.extra["asset_id"]
    yield
    area.nodes.remove(modified_node)
    area.nodes.append(node)
    area.extra["asset_id"] = asset_id

@pytest.mark.usefixtures("_setup_and_teardown_for_wrong_custom_spawn")
def test_create_patch_with_wrong_custom_spawn(test_files_dir, mocker):
    # test for a not createable spawn point
    file = test_files_dir.joinpath("log_files", "dread", "starter_preset.rdvgame")
    description = LayoutDescription.from_file(file)
    players_config = PlayersConfiguration(0, {0: "Dread"})
    cosmetic_patches = DreadCosmeticPatches()
    mocker.patch(
        "randovania.layout.layout_description.LayoutDescription.shareable_word_hash",
        new_callable=PropertyMock,
        return_value="Words Hash",
    )
    mocker.patch(
        "randovania.layout.layout_description.LayoutDescription.shareable_hash",
        new_callable=PropertyMock,
        return_value="$$$$$",
    )

    patcher = DreadPatchDataFactory(description, players_config, cosmetic_patches)
    with pytest.raises(
        KeyError,
        match="Artaria/Intro Room/Start Point has neither a start_point_actor_name nor the "
        "area has a collision_camera_name for a custom start point",
    ):
        patcher.create_data()

@pytest.fixture()
def _setup_and_teardown_for_custom_spawn():
    # modify a node to be a valid start point without a vanilla spawn
    game_desc = default_database.game_description_for(RandovaniaGame.METROID_DREAD)
    region = game_desc.region_list.region_with_name("Artaria")
    area = region.area_by_name("Charge Tutorial")
    node = area.node_with_name("Start Point")
    modified_node = dataclasses.replace(node, valid_starting_location=True)
    area.nodes.remove(node)
    area.nodes.append(modified_node)
    yield
    area.nodes.remove(modified_node)
    area.nodes.append(node)

@pytest.mark.usefixtures("_setup_and_teardown_for_custom_spawn")
def test_create_patch_with_custom_spawn(test_files_dir, mocker):
    # test for custom spawn point referenced by starting location and teleporters
    file = test_files_dir.joinpath("log_files", "dread", "custom_start.rdvgame")
    description = LayoutDescription.from_file(file)
    players_config = PlayersConfiguration(0, {0: "Dread"})
    cosmetic_patches = DreadCosmeticPatches()
    mocker.patch(
        "randovania.layout.layout_description.LayoutDescription.shareable_word_hash",
        new_callable=PropertyMock,
        return_value="Words Hash",
    )
    mocker.patch(
        "randovania.layout.layout_description.LayoutDescription.shareable_hash",
        new_callable=PropertyMock,
        return_value="$$$$$",
    )

    data = DreadPatchDataFactory(description, players_config, cosmetic_patches).create_data()

    # Expected Result
    expected_data = test_files_dir.read_json("patcher_data", "dread", "custom_start.json")

    # Update the file
    # json_lib.write_path(test_files_dir.joinpath("patcher_data", "dread", "custom_start.json"), data); assert False

    assert data == expected_data


# -*- coding: utf-8 -*-
"""
@author: Taoting
将用coco格式的json转化成labeime标注格式的json
"""

import json
import cv2
import numpy as np
import os

#用一个labelme格式的json作为参考，因为很多信息都是相同的，不需要修改。
def reference_labelme_json():
    ref_json_path = './bin/25.json'
    data=json.load(open(ref_json_path))
    return data

def labelme_shapes(data,data_ref,id):
    shapes = []
    label_num = {'box':0}#根据你的数据来修改
    for ann in data['annotations']:
        if id == ann['image_id']:
            shape = {}
            class_name = [i['name'] for i in data['categories'] if i['id'] == ann['category_id']]
            #label要对应每一类从_1开始编号
            label_num[class_name[0]] += 1
            shape['label'] = class_name[0]

            shape['points'] = []
            # ~ print(ann['segmentation'])
            if not type(ann['segmentation']) == list:
                continue
            else:
                x = ann['segmentation'][0][::2]#奇数个是x的坐标
                y = ann['segmentation'][0][1::2]#偶数个是y的坐标
                for j in range(len(x)):
                    shape['points'].append([x[j], y[j]])

                shape['shape_type'] =  data_ref['shapes'][0]['shape_type']
                shape['flags'] = data_ref['shapes'][0]['flags']
                shapes.append(shape)
    return shapes

def Coco2labelme(json_path,data_ref):
    with open(json_path,'r') as fp:
        data = json.load(fp)  # 加载json文件
        for img in data['images']:
            id = img['id']
            data_labelme={}
            data_labelme['version'] = data_ref['version']
            data_labelme['flags'] = data_ref['flags']

            data_labelme['shapes'] = labelme_shapes(data,data_ref,id)

            data_labelme['imagePath'] = img['file_name']

            data_labelme['imageData'] = None
            # ~ data_labelme['imageData'] = data_ref['imageData']

            data_labelme['imageHeight'] = img['height']
            data_labelme['imageWidth'] = img['width']
            file_name = data_labelme['imagePath']
            # 保存json文件
            json.dump(data_labelme,open('./%s.json' % file_name.split('.')[0],'w'),indent=4)
        return data_labelme

if __name__ == '__main__':
    root_dir = '/home/eason/PackSeg/'
    json_list = os.listdir(root_dir)
    #参考的json
    data_ref = reference_labelme_json()

    for json_path in json_list:
        if json_path.split('.')[-1] == 'json':
            print('当前文件： ', json_path)
            data_labelme= Coco2labelme(os.path.join(root_dir,json_path), data_ref)
            #file_name = data_labelme['imagePath']
            # 保存json文件
            #json.dump(data_labelme,open('./%s.json' % file_name.split('.')[0],'w'),indent=4)


import matplotlib.pyplot as plt
import scipy.signal as ss
import numpy as np
import math

window_size = 55
'------------------------------------------------------------------------------'

# opis: oblicza i zwraca pochodna sygnalu.
# nazwa funkcji: FUNC_diff
# parametry:
#               ecg_filtered - numpy 1D array
# zwraca:
#               diff - numpy 1D array
def FUNC_diff(ecg_filtered):
	N = len(ecg_filtered)
	'''POCHODNA---------------------------------------------------------------'''
	#diff = np.diff(ecg_filtered)
	diff = (2*ecg_filtered[5:N]+ecg_filtered[4:N-1]-ecg_filtered[2:N-3]-2*ecg_filtered[1:N-4])/8;
	'''Wektor wynikowy krotszy o 5 elementow'''

	return diff

# opis: oblicza i zwraca potege sygnalu.
# nazwa funkcji: FUNC_sqr
# parametry:
#               diff - numpy 1D array
# zwraca:
#               sqr - numpy 1D array
def FUNC_sqr(diff):
	'''POTEGA-----------------------------------------------------------------'''
	sqr = diff**2

	return sqr

# opis: calkuje sygnal w ruchomym oknie i zwraca go.
# nazwa funkcji: FUNC_signal_integration
# parametry:
#               sqr - numpy 1D array
#				fs - integer
# zwraca:
#               ecgmf - numpy 1D array
def FUNC_signal_integration(sqr, fs):
	dt = 1/float(fs)
	'''CALKOWANIE W RUCHOMYM OKNIE--------------------------------------------'''
	'''Utworzenie okna'''
	window = np.ones(window_size)
	'''Calkowanie'''
	temp = ss.lfilter(window,1,sqr)
	ecgmf = ss.medfilt(temp,9)
	ecgmf = ecgmf*dt
	'''Usuniecie opoznienia filtru'''
	delay = math.ceil(len(window) / 2)
	ecgmf = ecgmf[delay:len(ecgmf)]

	return ecgmf

# opis: wyszukuje i zwraca przyblizone granice QRS
#		potrzebne do wyszukania zalamka R (oraz zalamka S).
# nazwa funkcji: FUNC_prepare_for_maxima_search
# parametry:
#               ecgmf - numpy 1D array
# zwraca:
#               left_T - numpy 1D array
#				right_T - numpy 1D array
def FUNC_prepare_for_maxima_search(ecgmf):
	'''Wyszukanie najwyzszej amplitudy'''
	max_A = max(ecgmf)

	'''Budowa tablicy do przeszukiwania'''
	threshold = 0.2
	region_poz = ecgmf>(threshold*max_A)
	region_poz = region_poz*1

	region_poz = np.array([region_poz])

	'''Uzupelnienie zerem'''
	region_poz_LR = np.insert(region_poz, 0, 0)
	region_poz_RL = np.append(region_poz, 0)

	'''SZUKANE MAKSIMOW-------------------------------------------------------'''
	deltaLR = np.diff(region_poz_LR)
	deltaRL = np.diff(region_poz_RL)

	'''Wyznaczenie granic segmentow'''
	left  = np.where(deltaLR==1);
	right = np.where(deltaRL==-1);

	left_T = np.transpose(left)
	right_T = np.transpose(right)

	return left_T, right_T

# opis: znajduje i zwraca wartosci QRS na podstawie przyblizonych granic QRS.
# nazwa funkcji: FUNC_find_qrs_values
# parametry:
#               left_T - numpy 1D array
#				right_T - numpy 1D array
#				ecg_filtered - numpy 1D array
#				ecgmf - numpy 1D array
# zwraca:
#               qrs_left_values - numpy 1D array
#               qrs_right_values - numpy 1D array
#               qrs_left_values_ecgmf - numpy 1D array
#               qrs_right_values_ecgmf - numpy 1D array
def FUNC_find_qrs_values(left_T, right_T, ecg_filtered, ecgmf):
	qrs_left_values = np.empty(len(left_T))
	qrs_right_values = np.empty(len(left_T))
	qrs_left_values_ecgmf = np.empty(len(left_T))
	qrs_right_values_ecgmf = np.empty(len(left_T))

	ecg_filtered[12:len(ecg_filtered)]

	for i in range(0,len(left_T)):
		qrs_left_values[i] = ecg_filtered[left_T[i]]
		qrs_right_values[i] = ecg_filtered[right_T[i]]
		qrs_left_values_ecgmf[i] = ecgmf[left_T[i]]
		qrs_right_values_ecgmf[i] = ecgmf[right_T[i]]

	return qrs_left_values, qrs_right_values, qrs_left_values_ecgmf, qrs_right_values_ecgmf

# opis: znajduje lokalne maksima w przyblizonych granicach QRS.
#		Zwraca wartosci i indeksy wyszukanych maksimow.
# nazwa funkcji: FUNC_find_max
# parametry:
#               left_T - numpy 1D array
#				right_T - numpy 1D array
#				ecg_filtered - numpy 1D array
# zwraca:
#               max_value - numpy 1D array
#               max_index - numpy 1D array
def FUNC_find_max(left_T, right_T, ecg_filtered):
	max_index = np.empty(len(left_T))
	max_value = np.empty(len(left_T))
	#obciecie poczatku sygnalu oryginlanego w celu dopasownania indeksow po calkowaniu i pochodnej wzgledem oryginalnego
	#ecg_filtered = ecg_filtered[18:len(ecg_filtered)]

	#sygnal w trakcie calkownia jest scinany na koncu

	for i in range(0,len(left_T)):
		start = int(left_T[i])
		end = int(right_T[i])

		max_value[i] = ecg_filtered[start]
		max_index[i] = start

		for j in range(start,end):
			if ecg_filtered[j] > max_value[i]:
				max_value[i] = ecg_filtered[j]
				max_index[i] = j

		#max_index[i] = np.argmax(ecg_filtered[left_T[i]:right_T[i]])
		#max_index[i] = max_index[i]+left_T[i]
		#max_value[i] = ecg_filtered[max_index[i]]

	'''for i in range(0,len(left_T)):
		max_index[i] = np.argmax(ecg_filtered[left_T[i]:right_T[i]])
		max_index[i] = max_index[i]+left_T[i]
		max_value[i] = ecg_filtered[max_index[i]]'''

	return max_value, max_index

# opis: znajduje lokalne minima w przyblizonych granicach QRS.
#		Zwraca wartosci i indeksy wyszukanych minimow.
# nazwa funkcji: FUNC_find_min
# parametry:
#               left_T - numpy 1D array
#				right_T - numpy 1D array
#				ecg_filtered - numpy 1D array
# zwraca:
#               min_value - numpy 1D array
#               min_index - numpy 1D array
def FUNC_find_min(left_T, right_T, ecg_filtered):
	min_index = np.empty(len(left_T))
	min_value = np.empty(len(left_T))

	for i in range(0,len(left_T)):
		start = int(left_T[i])
		end = int(right_T[i])

		min_value[i] = ecg_filtered[start]
		min_index[i] = start

		for j in range(start,end):
			if ecg_filtered[j] < min_value[i]:
				min_value[i] = ecg_filtered[j]
				min_index[i] = j

	'''

	for i in range(0,len(left_T)):
		min_index[i] = np.argmin(ecg_filtered[left_T[i]:right_T[i]])
		min_index[i] = min_index[i]+left_T[i]
		min_value[i] = ecg_filtered[min_index[i]]'''

	return min_value, min_index

# opis: detekcja zalamka R. Zwraca wartosci i indeksy wyszukanych zalamkow R.
# nazwa funkcji: FUNC_r_detection
# parametry:
#				ecg_filtered - numpy 1D array
#				fs - integer
# zwraca:
#               r_value - numpy 1D array
#               r_index - numpy 1D array
def FUNC_r_detection(ecg_filtered, fs):
	stateDict = {'Done': 1,
				'Signal too short': -2,
				'Incorrect input': -3}

	try:
		if not len(ecg_filtered):
			stateFlag = stateDict['Incorrect input']
			return stateFlag, [[], []]
		dt = 1/float(fs)
		diff = FUNC_diff(ecg_filtered)
		sqr = FUNC_sqr(diff)
		ecgmf = FUNC_signal_integration(sqr, dt)
		left_T, right_T = FUNC_prepare_for_maxima_search(ecgmf)
		r_value, r_index = FUNC_find_max(left_T, right_T, ecg_filtered)

		stateFlag = stateDict['Done']

		return stateFlag, [r_value, r_index]

	except Exception as e:
		print(f'Module R_PEAKS failed: {e}')
		stateFlag = stateDict['Error']
		return stateFlag, [[], []]

# opis: detekcja zalamka S przy zalozeniu, ze osiaga minimum.
#		Zwraca wartosci i indeksy wyszukanych zalamkow S.
# nazwa funkcji: FUNC_s_detection
# parametry:
#				ecg_filtered - numpy 1D array
#				fs - integer
# zwraca:
#               s_value - numpy 1D array
#               s_index - numpy 1D array
def FUNC_s_detection(ecg_filtered, fs):
	dt = 1/float(fs)
	diff = FUNC_diff(ecg_filtered)
	sqr = FUNC_sqr(diff)
	ecgmf = FUNC_signal_integration(sqr, dt)
	left_T, right_T = FUNC_prepare_for_maxima_search(ecgmf)
	s_value, s_index = FUNC_find_min(left_T, right_T, ecg_filtered)

	return s_value, s_index

# opis: Rysuje wykresy poszczegolnych sygnalow z naniesionymi na nie punktami
# nazwa funkcji: PRINT_all
# parametry:
#				ecg_filtered - numpy 1D array
#				fs - integer
def PRINT_all(ecg_filtered, fs):
	diff = FUNC_diff(ecg_filtered)
	sqr = FUNC_sqr(diff)
	ecgmf = FUNC_signal_integration(sqr, fs)
	left_T, right_T = FUNC_prepare_for_maxima_search(ecgmf)
	r_value, r_index = FUNC_find_max(left_T, right_T, ecg_filtered)
	s_value, s_index = FUNC_find_min(left_T, right_T, ecg_filtered)
	qrs_left_values, qrs_right_values, qrs_left_values_ekgmf, qrs_right_values_ekgmf = FUNC_find_qrs_values(left_T, right_T, ecg_filtered, ecgmf)

	plt.figure(1)
	'Zaznaczono przyblizone granice QRS na przefiltrowanym sygnale'
	plt.subplot(411)
	plt.plot(ecg_filtered)
	plt.plot(left_T,qrs_left_values, marker='o', color='g', ls='')
	plt.plot(right_T,qrs_right_values, marker='o', color='y', ls='')
	plt.ylabel('ekg_filtered')

	plt.subplot(412)
	plt.plot(diff)
	plt.ylabel('diff')

	plt.subplot(413)
	plt.plot(sqr)
	plt.ylabel('sqr')

	'Zaznaczono przyblizone granice QRS na scalkowanym sygnale'
	plt.subplot(414)
	plt.plot(ecgmf)
	plt.plot(left_T,qrs_left_values_ekgmf, marker='o', color='g', ls='')
	plt.plot(right_T,qrs_right_values_ekgmf, marker='o', color='y', ls='')
	plt.ylabel('ecgmf')

	plt.figure(2)
	plt.plot(ecg_filtered)
	plt.plot(r_index,r_value, marker='x', color='r', ls='')
	plt.plot(s_index,s_value, marker='x', color='b', ls='')
	plt.plot(left_T,qrs_left_values, marker='o', color='g', ls='')
	plt.plot(right_T,qrs_right_values, marker='o', color='y', ls='')
	plt.xlabel('zalamki R, S oraz przyblizone granice QRS')

	plt.figure(3)
	plt.plot(ecg_filtered)
	plt.plot(r_index,r_value, marker='x', color='r', ls='')
	plt.xlabel('zalamki R')

	plt.show()

# opis: Zaznacza wykryte zalamki R na sygnale EKG
# nazwa funkcji: PRINT_r
# parametry:
#				ecg_filtered - numpy 1D array
#				r_index - numpy 1D array
#				r_value - numpy 1D array
def PRINT_r(ecg_filtered, r_index, r_value):
	plt.figure(4)
	plt.plot(ecg_filtered)
	plt.plot(r_index,r_value, marker='x', color='r', ls='')
	plt.xlabel('zalamki R')

	plt.show()


import pandas as pd

from sklearn import svm

import statistics

data = pd.read_csv('cleaned_LaptopDataset.csv')

t = statistics.median(data['latest_price'])

h = []

from sklearn import preprocessing

le = preprocessing.LabelEncoder()

for x in data.latest_price:

    if (x >= t):
        h.append(1)
    else:
        h.append(0)

data['latest_price'] = h

for col in data:
    data[col] = le.fit_transform(data[col])
########Train-test Dataset#######
x = data.drop('latest_price', axis=1)
y = data['latest_price']
from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.3)
######## SVM Using Linear Kernel #######
classifiere = svm.SVC(kernel="linear", C=1, gamma=1)
classifiere.fit(x_train, y_train)
predictions = classifiere.predict(x_test)
########SVM Accuracy#######
from sklearn.metrics import accuracy_score
acc = accuracy_score(y_test, predictions)
print("accuracy of SVM " , acc*100,"%")


from django import forms
from django.forms import modelformset_factory

from dashboard.forms.educator_account_form import EducatorAccountForm
from dashboard.models.educator_model import Educator

class EducatorForm(forms.ModelForm):
    class Meta:
        model = Educator
        fields = ['photo', 'name', 'title', 'email', 'about_me']

    def __init__(self, *args, accounts, educator_accounts, educator_not_accounts, **kwargs):
        super().__init__(*args, **kwargs)

        self.accounts = accounts

        self.EducatorAccountFormset = modelformset_factory(model=EducatorAccountForm.Meta.model,
                                                           form=EducatorAccountForm,
                                                           extra=len(educator_not_accounts),
                                                           validate_max=True,
                                                           max_num=len(accounts),
                                                           can_delete=True)

        self.accounts_formset = self.EducatorAccountFormset(args[0],
                                                            form_kwargs={'accounts': accounts},
                                                            queryset=educator_accounts,
                                                            initial=educator_not_accounts)


from typing import List
from torch  import optim
from torch.optim.optimizer      import Optimizer
from torch_geometric.data.data  import Data
from src.dataset    import citeSeer
from src.model      import GAT

import torch
import torch.nn.functional as F

from torch_geometric.data import Dataset

EPOCH = 200

# --- dataloader
'''
전체 graph를 사용하기 때문에 dataloader 불필요.
'''
citeSeer_ds = citeSeer('data')
citeseer = citeSeer_ds[0]

# --- Model
gat_model = GAT(citeSeer_ds.num_features,
        num_layers = 2,
        hidden_dim = [64],
        num_heads = [8],
        output_dim = citeSeer_ds.num_classes)

# --- Setting
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
gat_model.to(device)
citeseer = citeseer.to(device)
optimizer = torch.optim.Adam(gat_model.parameters(), lr = 0.005, weight_decay = 5e-4)
criterion = F.nll_loss

# --- training

def train(data : Dataset) -> None:
    gat_model.train()
    optimizer.zero_grad()
    out = gat_model(data.x, data.edge_index)
    loss = criterion(out[data.train_mask], data.y[data.train_mask])
    loss.backward()
    optimizer.step()

@torch.no_grad()
def test(data : Dataset) -> dict:
    gat_model.eval()
    out= gat_model(data.x, data.edge_index)
    acc_dic = {}
    for name, mask in data('train_mask', 'test_mask', 'val_mask'):
        acc = float((out[mask].argmax(-1) == data.y[mask]).sum() / mask.sum())
        acc_dic[name[:-5]] = acc
    return acc_dic

for epoch in range(EPOCH):
    train(citeseer)
    acc_dic = test(citeseer)
    print(f"Epoch : {epoch+1:03d}, Train : {acc_dic['train']:.4f}, Test : {acc_dic['test']:.4f}")


# -*- coding: utf-8 -*-
from flask import Flask, render_template, request,jsonify, redirect,url_for
from json import dumps
import celery , sys
from celeryconfig import appcelery
from Buscador import tasks
import time, json
app = Flask(__name__)

@app.route('/datos', methods=['GET', 'POST'])
def recibirInformacion():
    resultados=[]
    expedientes=[]
    expedientes=convertirAstring(request.args.get('expedientes'))
    informacion = tratamientosDatos(expedientes,request.args.get('array'))
    resultados=informacion.unirVectores()
    return  jsonify(resultados)

def convertirAstring(argumentos):
    argumentos=argumentos.replace(' ','')
    argumentos=argumentos.replace('"','')
    argumentos=argumentos.replace(']','')
    argumentos=argumentos.replace('[','')
    argumentos=argumentos.split(',',argumentos.count(','))
    return argumentos

class tratamientosDatos():
    def __init__(self, resultados_ncbi, resultados_array):
        self.datos_ncbi=resultados_ncbi[:]
        self.resultados_array=resultados_array
    def almacenar_datos_visualizacion_array(self):
        visualizacion_array=[]
        for i in appcelery.AsyncResult(self.resultados_array).get()['experiments']['experiment']:
            visualizacion_array.append({'id': i['id'],
            'accession': i['accession'],
            'name': i['name'], 'releasedate': i['releasedate'],
             'description': i['description'][0]['text'],'bd': 'arrayexpress', 'descarga': "null"  })

        return visualizacion_array
    def almacenar_datos_visualizacion_ncbi(self):
        tam_list=len(self.datos_ncbi)
        visualizacion_ncbi=[]
        for j in range(tam_list):
            i=appcelery.AsyncResult(self.datos_ncbi[j])
            identificador=i.get()['result']['uids'][0]
            visualizacion_ncbi.append({'id': identificador,
            'accession':i.get()['result'][identificador]['accession'],
             'name':  i.get()['result'][identificador]['title'],
              'releasedate': i.get()['result'][identificador]['pdat'],
               'description':  i.get()['result'][identificador]['summary'],
               'bd': 'ncbi_gds', 'descarga': i.get()['result'][identificador]['ftplink'] })
        return visualizacion_ncbi

    def unirVectores(self):
        vector_ncbi=[]
        vector_array=[]
        vector_ncbi=self.almacenar_datos_visualizacion_ncbi()
        vector_array=self.almacenar_datos_visualizacion_array()
        for i in vector_array:
            vector_ncbi.append(i)
        return vector_ncbi

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=80, debug=True)


from bokeh.layouts import column
from bokeh.models.widgets import RadioButtonGroup,Select, Div, Button,PreText
from bokeh.models import  TextInput, RadioGroup
from bokeh.plotting import curdoc

button_group = RadioButtonGroup(labels=["Physical parameters", "Geometric parameters", "Initial  conditions"], active=1)

## Add the elements inside each radio button

slope  = TextInput(value='25', title="slope:")

###### Drop down menus

Coefficient = Select(title="Coefficient:", value="chezy", options=["Manning", "chezy"])
Coefficient_value1  = TextInput(value=" ", title="chezy:" )
Coefficient_value2  = TextInput(value=" ", title="Manning:" )

#### Add geometric parameters
length  = TextInput(value=" ", title="Length:")
width  = TextInput(value=" ", title="Width:")

#LABELS = ["km", "m"]
#radio_group = RadioGroup(labels=LABELS, active=0)
#### Add ginitial conditiions
depth = TextInput(value=" ", title="Depth(m):")
Q = TextInput(value=" ", title="Discharge(m3/sec):")
texto = PreText(text="""Please click here""",width=500, height=100)
layout = ()
def button_group_change(active):
    ch = active
    if ch == 0:
       slope.visible=True
       Coefficient.visible=True
       Coefficient_value1.visible=True
       Coefficient_value2.visible=True
       length.visible=False
       width.visible=False
       depth.visible=False
       Q.visible=False
       # layout= column(slope, Coefficient,Coefficient_value1, Coefficient_value2)

    elif ch == 1:
         length.visible=True
         width.visible=True
         slope.visible=False
         Coefficient.visible=False
         Coefficient_value1.visible=False
         Coefficient_value2.visible=False
         depth.visible=False
         Q.visible=False
        # layout= column(length, width)

    elif ch == 2:
        depth.visible=True
        Q.visible=True
        slope.visible=False
        Coefficient.visible=False
        Coefficient_value1.visible=False
        Coefficient_value2.visible=False
        length.visible=False
        width.visible=False
        # layout= column(length, width )
    texto.text ='text' #str(layout)

    #curdoc().add_root(column(button_group,slope, Coefficient))
    #curdoc().add_root(row(button_group,length))
layout= column(slope, Coefficient,Coefficient_value1, Coefficient_value2,length, width,length)
button_group.on_click(button_group_change)
###show
curdoc().add_root(column(texto,button_group,layout))
#bokeh serve --show BWC.py


import json
import numpy as np
import util

class AudfprintAligner:

    matches = {}

    def __init__(self, matchfile):
        with open(matchfile) as f:
            for x, ys in json.load(f).iteritems():
                for y, m in ys.iteritems():
                    m = m[0]
                    if "Matched" in m:
                        d = float(self.between(m, "Matched ", " s "))
                        t1 = float(self.between(m, " at ", " s "))
                        f1 = self.between(m, " in ", " to ")
                        t2 = float(self.between(m, " time ", " s "))
                        f2 = self.between(m, " in ", " with ", 1)
                        if f1 not in self.matches:
                            self.matches[f1] = {}
                        self.matches[f1][f2] = [t1, t2, d]

    def between(self, string, s1, s2, index=0):
        string = string.split(s1)[index+1]
        return string[:string.find(s2)]

    def get_alignment_points(self, file, reffile):
        if file in self.matches:
            if reffile in self.matches[file]:
                t1, t2, d = self.matches[file][reffile]
                filedur = util.get_duration(file)
                refdur = util.get_duration(reffile)
                delta_start = t2-t1
                delta_end = delta_start+filedur #assume slope 1
                return [delta_start, delta_end], 1
        return None, 0


# Code adapted from https://www.codeproject.com/Articles/5297227/Deep-Learning-for-Fashion-Classification
# import tensorflow.keras as keras

import os
import matplotlib.pyplot as plt
import matplotlib.image as img
import tensorflow as tf
import keras
import numpy as np
from keras.preprocessing.image import ImageDataGenerator
from keras.preprocessing.image import ImageDataGenerator

config = tf.compat.v1.ConfigProto()
config.gpu_options.allow_growth = True
tf.compat.v1.keras.backend.set_session(tf.compat.v1.Session(config=config))

data = '/home/lunet/conce/Downloads/Codeproject/DeepFashion Custom/DeepFashion/Train'
os.chdir(data)
batch_size = 3

def DataLoad(shape, preprocessing):
    """Create the training and validation datasets for a given image shape."""
    img_data = ImageDataGenerator(
        preprocessing_function=preprocessing,
        horizontal_flip=True,
        validation_split=0.1,
    )

    height, width = shape

    train_dataset = img_data.flow_from_directory(
        os.getcwd(),
        target_size=(height, width),
        classes=['Blazer', 'Blouse', 'Cardigan', 'Dress', 'Jacket',
                 'Jeans', 'Jumpsuit', 'Romper', 'Shorts', 'Skirts', 'Sweater', 'Sweatpants'
            , 'Tank', 'Tee', 'Top'],
        batch_size=batch_size,
        subset='training',
    )

    val_dataset = img_data.flow_from_directory(
        os.getcwd(),
        target_size=(height, width),
        classes=['Blazer', 'Blouse', 'Cardigan', 'Dress', 'Jacket',
                 'Jeans', 'Jumpsuit', 'Romper', 'Shorts', 'Skirts', 'Sweater',
                 'Sweatpants', 'Tank', 'Tee', 'Top'],
        batch_size=batch_size,
        subset='validation'
    )

    return train_dataset, val_dataset

vgg16 = keras.applications.vgg16
conv_model = vgg16.VGG16(weights='imagenet', include_top=False)
conv_model.summary()

train_dataset, val_dataset = DataLoad((224, 224), preprocessing=vgg16.preprocess_input)
# Function for plots images with labels within jupyter notebook
X_train, y_train = next(train_dataset)

# Load ImageNet weights of this network, to be used during the transfer learning
conv_model = vgg16.VGG16(weights='imagenet', include_top=False, input_shape=(224, 224, 3))

# flatten the output of the convolutional part:
x = keras.layers.Flatten()(conv_model.output)

# three hidden layers
x = keras.layers.Dense(100, activation='relu')(x)
x = keras.layers.Dense(100, activation='relu')(x)
x = keras.layers.Dense(100, activation='relu')(x)

# final softmax layer with 15 categories
predictions = keras.layers.Dense(15, activation='softmax')(x)

# creating the full model:
full_model = keras.models.Model(inputs=conv_model.input, outputs=predictions)
full_model.summary()

for layer in conv_model.layers:
    layer.trainable = False

full_model.compile(loss='categorical_crossentropy', optimizer=keras.optimizers.Adamax(lr=0.001), metrics=['acc'])

history = full_model.fit_generator(
    train_dataset,
    validation_data=val_dataset,
    workers=0,
    epochs=7,
)

def plot_history(history, yrange):
    """Plot loss and accuracy as a function of the epoch, for the training and validation datasets.
    """
    acc = history.history['acc']
    val_acc = history.history['val_acc']
    loss = history.history['loss']
    val_loss = history.history['val_loss']

    # Get number of epochs
    epochs = range(len(acc))

    # Plot training and validation accuracy per epoch
    plt.plot(epochs, acc)
    plt.plot(epochs, val_acc)
    plt.title('Training and validation accuracy')
    plt.ylim(yrange)

    # Plot training and validation loss per epoch
    plt.figure()

    plt.plot(epochs, loss)
    plt.plot(epochs, val_loss)
    plt.title('Training and validation loss')

    plt.show()

plot_history(history, yrange=(0.9, 1))

test_data = '/home/lunet/conce/Downloads/Codeproject/DeepFashion Custom/DeepFashion/Train'
test_datagen = ImageDataGenerator()

test_generator = test_datagen.flow_from_directory(test_data, target_size=(224, 224), batch_size=3, class_mode='categorical')
# X_test, y_test = next(test_generator)

test_results = full_model.evaluate(test_generator)
print("test loss, test acc:", test_results)


from scipy import stats, signal
from collections import defaultdict
import numpy as np
from tqdm.notebook import tqdm
import pandas as pd

from src import config
from src.FFT import FFTAnalysis as FFT

def _extractTimeDomainFeatures(sig):
    ''' Extracts time domain features from one vibration signal'''

    # Get time features
    features = dict()

    if sig is not None:

        rmsVal  = np.sqrt(np.square(sig).mean())
        sigMax  = sig.max()
        absSig  = np.abs(sig)
        absMean = absSig.mean()

        features = {
            'mean':     sig.mean(),
            'min':      sig.min(),
            'max':      sigMax,
            'std':      np.std(sig),
            'skew':     stats.skew(sig),
            'kurt':     stats.kurtosis(sig),
            'rms':      rmsVal,
            'p2p':      sigMax - sig.min(),
            'crestF':   sigMax / rmsVal,
            'impulseF': absSig.max() / absMean,
            'shapeF':   rmsVal / absMean
            }

    return features

def _getSubBand(FFTfreqs, FFTlevels, band):
    ''''Extract portion of the FFT corresponding to the given frequency band '''

    idxKeep = (FFTfreqs >= band[0]) & (FFTfreqs < band[1])
    freqs   = FFTfreqs[idxKeep]
    levels  = FFTlevels[idxKeep]

    return freqs, levels

def _extractPeaks(freqs, levels, distance):
    ''' Extracts peaks from an FFT '''

    peakIdx, _ = signal.find_peaks(levels, distance = distance)
    peakLevels = levels[peakIdx]
    peakFreqs  = freqs[peakIdx]

    return peakFreqs, peakLevels

def _getTopPeaks(levels, freqs, noPeaks):
    ''' Extract top <n> peaks from an FFT '''

    # Sort peak indices from highest to lowest level
    sortIdx = np.argsort(levels)
    sortIdx = np.flip(sortIdx)

    # Grab top <n> peaks
    levels = levels[sortIdx[0:noPeaks]]
    freqs  = freqs[sortIdx[0:noPeaks]]

    return freqs, levels

def _extractFrequencyDomainFeatures(sig, FFTsettings,
                                    noPeaks  = config.FBAND_PEAKS,
                                    peakDist = config.PEAK_DIST,
                                    fs       = config.Fs,
                                    fBands   = config.FBANDS):
    ''' Extracts frequency domain features from one vibration signal'''

    FFTfreqs, FFTlevels = None, None
    features = defaultdict()

    if sig is not None:
        FFTfreqs, FFTlevels = FFT(sig, config.Fs, FFTsettings)

        # Split in bands
        for bandNo, band in enumerate(fBands):

            freqs, levels = _getSubBand(FFTfreqs, FFTlevels, band)
            freqs, levels = _extractPeaks(freqs, levels, peakDist)
            freqs, levels = _getTopPeaks(levels, freqs, noPeaks)

            # Add peaks from current band to the dictionary with the features
            for peakNo in range(noPeaks):
                featName           = f'band_{bandNo + 1}_peak_{peakNo+1}_level'
                features[featName] = levels[peakNo]
                featName           = f'band_{bandNo + 1}_peak_{peakNo+1}_freq'
                features[featName] = freqs[peakNo]

    return features

def _extractFeatures(sig, FFTsettings):
    ''''Extracts time- and frequency-domain features from one vibration signal'''

    feats     = _extractTimeDomainFeatures(sig)
    freqFeats = _extractFrequencyDomainFeatures(sig, FFTsettings)
    feats.update(freqFeats)

    return feats

def extractDatasetFeatures(df, FFTsettings = config.FFT_SETTINGS):
    ''' Extracts features from the entire dataset '''

    # Extract features from every experiment
    driveFeats, fanFeats = [], []
    for idx, record in tqdm(df.iterrows(), total = df.shape[0]):

        FFTsettings['HPCutoffFrequency'] = 20 * record['MotorSpeed_rpm'] / 60
        FFTsettings['LPCutoffFrequency'] = 10 * record['MotorSpeed_rpm'] / 60

        driveFeats.append(_extractFeatures(record['DriveVibs'], FFTsettings))
        fanFeats.append(_extractFeatures(record['FanVibs'], FFTsettings))

    # Make dataframes with the extracted features
    dfDrive = pd.DataFrame(driveFeats)
    dfFan   = pd.DataFrame(fanFeats)

    # Add corresponding labels
    dfDrive['label'] = df['DriveLabel']
    dfFan['label']   = df['FanLabel']

    # Remove rows with missing records for fan-end bearing
    dfFan.dropna(axis = 0, how = 'any', inplace = True)

    return dfDrive, dfFan


import argparse
import json
import os
import platform
import subprocess
from typing import List

HOST_MAGMA_ROOT = '../../../.'

def main() -> None:
    """ Run main"""
    args = _parse_args()

    if args.mount:
        _run(['up', '-d', 'test'])
        _run(['exec', 'test', 'bash'])
        _down(args)

    elif args.lint:
        _run(['up', '-d', 'test'])
        _run(['exec', 'test', 'make', 'lint'])
        _down(args)

    elif args.precommit:
        _run(['up', '-d', 'test'])
        _run(['exec', 'test', 'make', 'precommit'])
        _down(args)

    elif args.coverage:
        _run(['up', '-d', 'test'])
        _run(['exec', 'test', 'make', 'cover'])
        _down(args)

    elif args.tests:
        _run(['up', '-d', 'test'])
        _run(['exec', 'test', 'make', 'test'])
        _down(args)

    elif args.health:
        # _set_mac_env_vars is needed to override LOG_DRIVER for mac
        _set_mac_env_vars()
        _run(['-f', 'docker-compose.yml', '-f', 'docker-compose.override.yml', '-f', 'docker-compose.health.override.yml', 'up', '-d'])
        _run_health()
        _down(args)

    elif args.git:
        print(json.dumps(_run_get_git_vars(), indent=4, sort_keys=True))

    else:
        _run(['build'] + _get_default_build_args(args))
        _down(args)

def _run(cmd: List[str]) -> None:
    """ Run the required docker compose command """
    cmd = ['docker', 'compose', '--compatibility'] + cmd
    print("Running '%s'..." % ' '.join(cmd))
    try:
        subprocess.run(cmd, check=True)  # noqa: S603
    except subprocess.CalledProcessError as err:
        exit(err.returncode)

def _down(args: argparse.Namespace) -> None:
    if args.down:
        _run(['down'])

def _get_default_build_args(args: argparse.Namespace) -> List[str]:
    ret = []
    git_info = _run_get_git_vars()

    for arg, val in git_info.items():
        ret.append("--build-arg")
        ret.append("{0}={1}".format(arg, val))

    if args.nocache:
        ret.append('--no-cache')
    return ret

def _run_get_git_vars():
    try:
        cmd = "tools/get_version_info.sh"
        cmd_res = \
            subprocess.run(cmd, check=True, capture_output=True)  # noqa: S603
    except subprocess.CalledProcessError as err:
        print("Error _run_get_git_vars")
        exit(err.returncode)
    return json.loads(cmd_res.stdout)

def _run_health():
    try:
        cmd = "tools/docker_ps_healthcheck.sh"
        subprocess.run(cmd, check=True)  # noqa: S603
    except subprocess.CalledProcessError as err:
        print("Error _run_health")
        exit(err.returncode)

def _set_mac_env_vars():
    if (platform.system().lower() == "darwin"):
        os.environ['LOG_DRIVER'] = "json-file"

def _parse_args() -> argparse.Namespace:
    """ Parse the command line args """

    # There are multiple ways to invoke finer-grained control over which
    # images are built.
    #
    # (1) How many images to build
    #
    # all: all images
    # default: images required for minimum functionality
    #   - excluding metrics images
    #   - including postgres, proxy, etc
    #
    # (2) Of the core orc8r images, which modules to build
    #
    # Defaults to all modules, but can be further specified by targeting a
    # deployment type.

    parser = argparse.ArgumentParser(description='Orc8r build tool')

    # Run something
    parser.add_argument(
        '--tests', '-t',
        action='store_true',
        help='Run unit tests',
    )

    parser.add_argument(
        '--mount', '-m',
        action='store_true',
        help='Mount the source code and create a bash shell',
    )

    parser.add_argument(
        '--precommit', '-c',
        action='store_true',
        help='Mount the source code and run pre-commit checks',
    )

    parser.add_argument(
        '--coverage', '-o',
        action='store_true',
        help='Generate test coverage statistics',
    )

    parser.add_argument(
        '--lint', '-l',
        action='store_true',
        help='Run lint test',
    )

    parser.add_argument(
        '--health', '-e',
        action='store_true',
        help='Run health test',
    )

    # Run something
    parser.add_argument(
        '--git', '-g',
        action='store_true',
        help='Get git info',
    )

    # How to do it
    parser.add_argument(
        '--nocache', '-n',
        action='store_true',
        help='Build the images with no Docker layer caching',
    )
    parser.add_argument(
        '--down', '-down',
        action='store_true',
        default=False,
        help='Leave containers up after running tests',
    )

    return parser.parse_args()

if __name__ == '__main__':
    main()


#!/usr/bin/python3

import _thread
import re, time, cv2, serial

'''
ServoController interfaces with the arduino board to control the servo motor over
USB serial coms
'''

class ServoController:

    def __init__(self):
        self.ser = serial.Serial('com3', 9600, timeout=0.5)

    def __enter__(self):
        return self

    def __exit__(self, exc_type, exc_value, traceback):
        self.ser.close()

    def servo_read_position(self):
        self.ser.write(b'r')
        pos = str(self.ser.readline())
        # First read is always empty, sometimes the first seversl,
        # so recusively call until numbers come back from arduino
        while pos == "b''":
            pos = self.servo_read_position()
        print("pos_remote_read: " + str(pos))
        m = re.search('\d+', str(pos))
        pos = int(m.group(0))
        return pos

    def servo_set_position(self, pos):
        print("ad pos: " +str(pos))
        self.ser.write(bytes(str(pos), 'utf8'))
        self.ser.write(b'\n')
        print("pos_remote: " + str(self.ser.readline()))

'''
SerialServo controls camera rotation and motion detection
Currently written to sweep the camera, wait and detect motion, only continue sweeping once no motion detected
'''

class CameraController:

    def camera_frame_grab(self):
        ret, frame = self.cap.read()
        cv2.imshow(self.frameName, frame)
        cv2.waitKey(1)

    def camera_rotate(self, start, stop, step_delay=0.2):
        # If the stop angle bigger than start, increment, else decrement
        # If the stop angle is smaller, it cannot be negative, if it's bigger,
        # then it can't be bigger than 180
        if start <= stop:
            if stop < 25:
                stop = 25
            direction = 1
        else:
            if stop > 165:
                stop = 165
            direction = -1

        for pos in range(start, stop, direction):
            self.camera_frame_grab()
            self.sc.servo_set_position(pos)
            time.sleep(step_delay)

    def camera_sweep(self):
        self.camera_rotate(45, 180)
        self.camera_rotate(180, 45)

    def camera_left(self, degrees):
        pos = self.sc.servo_read_position()
        if pos <= 180:
            self.camera_rotate(pos, pos + degrees)

    def camera_right(self, degrees):
        pos = self.sc.servo_read_position()
        if pos > 0:
            self.camera_rotate(pos, pos - degrees)

    def detect_motion(self, duration):
        start_time = time.time()
        while (True):
            ret, frame = self.cap.read()
            if self.motionSense:
                end_time = time.time()
                # Wait detecting motion for duration, stop detecting once time has passed and no motion detected
                if(end_time - start_time) > duration and count == 1:
                    print("Finished recording " + str(duration))
                    break
                fgmask = self.fgbg.apply(frame)

                # Generate and prepare the threshold image, and find contours with it.
                ret, thresh = cv2.threshold(fgmask, 127, 255, 0)
                thresh = cv2.dilate(thresh, None, iterations=2)
                im2, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

                # Count contours with large areas, use them for movement simple detection
                count = 1
                for c in contours:
                    if cv2.contourArea(c) < 500:
                        continue
                    count += count
                    # optionally draw bounding boxes around the detected contours
                    if self.drawRect:
                        (x, y, w, h) = cv2.boundingRect(c)
                        cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)

            cv2.imshow(self.frameName, frame)
            k = cv2.waitKey(60) & 0xff
            if k == 27:
                break

    def __init__(self, camera, servo=0, drawRect=0, motionSense=1):
        if servo:
            self.sc = ServoController()
        self.drawRect = drawRect
        self.motionSense = motionSense
        self.frameName = "Camera " + str(camera)
        self.cap = cv2.VideoCapture(camera)
        self.fgbg = cv2.createBackgroundSubtractorMOG2()
        ret, frame = self.cap.read()
        cv2.imshow(self.frameName, frame)

    def __enter__(self):
        return self

    def __exit__(self, exc_type, exc_value, traceback):
        # When everything done, release the capture and serial port
        self.cap.release()
        cv2.destroyAllWindows()

def dynamic_camera(cam):
    with CameraController(cam, 1) as cc:
        while(True):
            cc.camera_right(25)
            cc.detect_motion(20)
            cc.camera_right(25)
            cc.detect_motion(20)
            cc.camera_right(25)
            cc.detect_motion(20)
            cc.camera_right(25)
            cc.detect_motion(20)
            cc.camera_right(25)
            cc.detect_motion(20)
            cc.camera_right(25)
            cc.detect_motion(20)
            cc.camera_left(25)
            cc.detect_motion(20)
            cc.camera_left(25)
            cc.detect_motion(20)
            cc.camera_left(25)
            cc.detect_motion(20)
            cc.camera_left(25)
            cc.detect_motion(20)
            cc.camera_left(25)
            cc.detect_motion(20)
            cc.camera_left(25)
            cc.detect_motion(20)
            cc.camera_left(25)
            cc.detect_motion(20)
            cc.camera_left(25)
            cc.detect_motion(20)

def static_camera(cam):
    print("Static Cam - no motion detection")
    with CameraController(cam, drawRect=1, motionSense=0) as cc:
        while (True):
            cc.detect_motion(20)

if __name__ == "__main__":
    try:
        _thread.start_new_thread(dynamic_camera, (1, ))
        _thread.start_new_thread(static_camera, (0, ))
    except:
        print("Error: unable to start thread")

    while 1:
        pass


import math
import random
import numpy as np
from itertools import combinations
from copy import deepcopy

class Node:
    def __init__(self):
        self.parent = None
        self.state = []
        self.children = []
        self.fully_expanded = False
        self.Q = 0
        self.N = 0

    def __str__(self):
        return f"node state: {self.state}, Q: {self.Q}, N: {self.N}, fully expanded: {self.fully_expanded}"

def ripple(v_sum, f_sum, f_leaves):
    return f_leaves - (f_sum - v_sum) * (f_leaves / f_sum) if f_sum != 0 else 0

def distance(v1, v2):
    return np.sqrt(np.sum(np.power(v1 - v2, 2)))

def ps(df, v, f, selections):
    a = np.copy(f)
    for selection in selections:
        v_sum = df.loc[selection, 'real'].sum()
        f_sum = df.loc[selection, 'predict'].sum()
        a[selection] = ripple(v_sum, f_sum, df.loc[selection, 'predict'])

    score = max(1 - distance(v, a) / distance(v, f), 0)
    return score

def gps(v, f, selections):
    a, b = [], []
    for selection in selections:
        selection_v = v[selection]
        selection_f = f[selection]
        with np.errstate(divide='ignore', invalid='ignore'):
            selection_a = f[selection] * (selection_v.sum() / selection_f.sum())
            selection_a = np.nan_to_num(selection_a)
        a.extend(np.abs(selection_v - selection_a))
        b.extend(np.abs(selection_v - selection_f))

    selection = np.logical_or.reduce(selections)
    non_selection_v = v[~selection]
    non_selection_f = f[~selection]

    a = np.mean(a)
    b = np.mean(b)
    c = np.nan_to_num(np.mean(np.abs(non_selection_v - non_selection_f)))
    score = 1 - ((a + c) / (b + c))
    return score

def get_unqiue_elements(df, cuboid):
    return {tuple(row) for row in df[cuboid].values}

def get_element_mask(df, cuboid, combination):
    return [np.logical_and.reduce([df[d] == e for d, e in zip(cuboid, c)]) for c in combination]

def ucb(node, C=math.sqrt(2.0)):
    best_child = None
    max_score = -1
    for child in node.children:
        if child.N > 0 and not child.fully_expanded:
            left = child.Q
            right = C * math.sqrt(math.log(node.N) / child.N)
            score = left + right
            if score > max_score:
                best_child = child
                max_score = score
    return best_child

def init_children(node, elements):
    children = [e for e in elements if e not in set(node.state)]
    for c in children:
        child = Node()
        child.state = node.state + [c]
        child.parent = node
        node.children.append(child)

def get_initial_scores(df, elements, cuboid, v, f, scoring):
    element_scores = dict()
    for leaf in elements:
        selections = get_element_mask(df, cuboid, [leaf])
        if scoring == 'ps':
            element_scores[leaf] = ps(df.copy(), v, f, selections)
        else:
            element_scores[leaf] = gps(v, f, selections)
    return element_scores

def sublist(lst1, lst2):
    return set(lst1) <= set(lst2)

def selection(node, elements):
    while len(node.state) < len(elements):
        if len(node.children) == 0:  # First time to search this node.
            init_children(node, elements)
            return node

        q_max = 0
        all_visit = True
        for child in node.children:
            q_max = max(q_max, child.Q)
            if child.N == 0:  # Not all children have been visited.
                all_visit = False

        if not all_visit and random.random() > q_max:
            return node  # Expand current node

        child_node = ucb(node)  # Select the best path got go deeper into the tree.
        if child_node is None:  # If all children are already fully expanded.
            if all_visit:
                node.fully_expanded = True
                if node.parent is None:
                    return node  # The tree is fully exanded.
                node = node.parent  # Continue again with parent node.
            else:
                return node  # Expand current node.
        else:
            node = child_node

    node.fully_expanded = True
    return node

def expand(node, element_scores):
    best_child = None
    max_score = -1
    for child in node.children:
        if child.N == 0:
            score = element_scores[child.state[-1]]
            if score > max_score:
                max_score = score
                best_child = child
    return best_child

def evaluate(df, selected_node, cuboid, v, f, scoring):
    selections = get_element_mask(df, cuboid, selected_node.state)
    if scoring == 'ps':
        score = ps(df.copy(), v, f, selections)
    else:
        score = gps(v, f, selections)
    return score

def backup(node, new_q):
    while node is not None:
        node.N += 1
        node.Q = max(node.Q, new_q)
        node = node.parent

def MCTS(df, elements, cuboid, v, f, pt, m, scoring):
    root = Node()
    max_q = -1
    best_selection = Node()

    element_scores = get_initial_scores(df, elements, cuboid, v, f, scoring)
    for i in range(m):
        node = selection(root, elements)
        if not node.fully_expanded:
            node = expand(node, element_scores)

        if root.fully_expanded:
            break

        new_q = evaluate(df, node, cuboid, v, f, scoring)
        backup(node, new_q)

        if new_q > max_q:
            max_q = root.Q
            best_selection = deepcopy(node)
        elif (new_q == max_q) and not sublist(node.state, best_selection.state) and len(node.state) < len(
                best_selection.state):
            max_q = root.Q
            best_selection = deepcopy(node)

        if max_q >= pt:
            break

    return best_selection.state, max_q

def hierarchical_pruning(elements, layer, cuboid, candidate_set):
    previous_layer_candidates = [candidate for candidate in candidate_set if candidate['layer'] == layer - 1]
    parent_selections = [cand['elements'] for cand in previous_layer_candidates if set(cand['cuboid']) < set(cuboid)]

    for parent_selection in parent_selections:
        elements = [e for e in elements if np.any([set(pe) < set(e) for pe in parent_selection])]
    return elements

def get_best_candidate(candidate_set):
    # Sort by score, layer, number of elements
    sorted_cands = sorted(candidate_set, key=lambda c: (c['score'], -c['layer'], -len(c['elements'])), reverse=True)
    return sorted_cands[0]

def hotspot(df, dimensions, pt=0.67, m=200, scoring='gps', debug=False):
    assert scoring in ['ps', 'gps'], "Supported scoring is 'ps' and 'gps'."

    # Hierarcical pruning does not seem to work well when using gps scoring
    use_pruning = scoring != 'gps'

    v = df['real'].values
    f = df['predict'].values

    candidate_set = []
    for layer in range(1, len(dimensions) + 1):
        if debug:
            print('Layer:', layer)

        cuboids = [list(c) for c in combinations(dimensions, layer)]
        for cuboid in cuboids:
            if debug:
                print('Cuboid:', cuboid)

            elements = get_unqiue_elements(df, cuboid)
            # if debug: print('Elements:', elements)

            if use_pruning and layer > 1:
                elements = hierarchical_pruning(elements, layer, cuboid, candidate_set)
                # if debug: print('Filtered elements:', elements)

            selected_set, score = MCTS(df, elements, cuboid, v, f, pt, m, scoring)
            if debug:
                print('Best subset:', selected_set, 'score', score)

            candidate = {
                'layer': layer,
                'cuboid': cuboid,
                'score': score,
                'elements': np.array(selected_set)
            }

            if candidate['score'] >= pt:
                return candidate

            candidate_set.append(candidate)

    return get_best_candidate(candidate_set)


from collections import OrderedDict
from itertools import chain

from .types import Vsn, MatrixID, PacketClass
from .patches import patch
from .cache import from_page, get_page
from .sources import version_urls
from .parsers import pre_versions, pre_packets, rel_version, rel_packets
from .parsers import first_heading

__all__ = ('version_packet_ids',)

# Returns matrix with matrix[version][packet_class] = matrix_id
@from_page(dep=(first_heading,pre_versions,pre_packets,rel_version,rel_packets),
rdoc='Recalculate the packet ID matrix. Give if the version_urls dict\n'
'or the code of version_packet_ids() have been changed.', doc_order=-2)
def version_packet_ids():
    """Return a dict mapping `Vsn' instances to dicts mapping `PacketClass'
       instances to `MatrixID' instances, giving the matrix of packet IDs as
       they vary across packets and across protocol versions."""
    used_patches = set()
    packet_classes = {}
    matrix = OrderedDict()
    prev_v = None
    for v, url in reversed(version_urls.items()):
        with get_page(url) as page:
            heading = first_heading(page)
            if heading == 'Pre-release protocol':
                vdiff = pre_versions(page, v)
                if (v, vdiff) in patch:
                    used_patches.add((v, vdiff))
                    vdiff = patch[v, vdiff]
                from_v, to_v = vdiff
                assert v == to_v, '%r != %r' % (v, to_v)
                matrix[v] = {}
                matrix.move_to_end(v, last=False)
                seen_names = {}
                all_pre_packets = pre_packets(page, v)
                if (v, None) in patch:
                    all_pre_packets = chain(all_pre_packets, patch[(v, None)])
                    used_patches.add((v, None))
                for packet in all_pre_packets:
                    if (v, packet) in patch:
                        used_patches.add((v, packet))
                        packet = packet.patch(patch[v, packet])
                    if packet is None: continue
                    assert packet.name not in seen_names, \
                        '[%s] Duplicate packet name:\n%s\n%s' % \
                        (v.name, seen_names[packet.name], packet)
                    seen_names[packet.name] = packet

                    packet_class = PacketClass(
                        name=packet.name, state=packet.state, bound=packet.bound)
                    if packet.name not in packet_classes:
                        packet_classes[packet.name] = packet_class
                    assert packet_class == packet_classes[packet.name], \
                        '[%s] %r != %r' % (v.name, packet_class, packet_classes[packet.name])

                    if packet.old_id is None:
                        assert packet_class not in matrix[from_v], \
                               '[%s] %r in matrix[%r]' % (v.name, packet_class, from_v)
                    else:
                        if packet_class not in matrix[from_v]:
                            msg = '[%s] [0x%02X] %r not in matrix[%r]' % (
                                  v.name, packet.old_id, packet_class, from_v)
                            for from_pcls, from_mid in matrix[from_v].items():
                                if (from_pcls.state, from_pcls.bound, from_mid.id) \
                                == (packet_class.state, packet_class.bound, packet.old_id):
                                    msg += '\n(however, matrix[%r][%r].id == 0x%02X)' % (
                                           from_v, from_pcls, packet.old_id)
                                    break
                            raise AssertionError(msg)
                        assert packet.old_id == matrix[from_v][packet_class].id, \
                            '[%s] 0x%02X != matrix[%r][%r].id == 0x%02X' % (
                            v.name, packet.old_id, from_v, packet_class,
                            matrix[from_v][packet_class].id)
                    if packet.url is not None:
                        url = packet.url
                    elif not packet.changed and from_v and packet_class in matrix[from_v]:
                        url = matrix[from_v][packet_class].url
                    else:
                        url = None
                    if packet.new_id is not None:
                        matrix[v][packet_class] = MatrixID(
                            id=packet.new_id, base_ver=from_v,
                            changed=packet.changed, html=packet.html, url=url)
                for packet_class, id in matrix[from_v].items():
                    if packet_class.name in seen_names: continue
                    matrix[v][packet_class] = id._replace(
                        base_ver=from_v, changed=False)
            elif heading == 'Protocol':
                rel_v = rel_version(page)
                if rel_v.name is None:
                    rel_v = Vsn(v.name, rel_v.protocol)
                assert v == rel_v, '%r != %r' % (v, rel_v)
                matrix[v] = {}
                seen_names = {}
                all_rel_packets = rel_packets(page, v)
                if (v, None) in patch:
                    all_rel_packets = chain(all_rel_packets, patch[(v, None)])
                    used_patches.add((v, None))
                for packet in all_rel_packets:
                    if (v, packet) in patch:
                        used_patches.add((v, packet))
                        packet = packet.patch(patch[v, packet])
                    if packet is None: continue
                    assert packet.name not in seen_names, \
                        '[%s] Duplicate packet name:\n%s\n%s.' \
                        % (v.name, seen_names[packet.name], packet)
                    seen_names[packet.name] = packet

                    packet_class = PacketClass(
                        name=packet.name, state=packet.state, bound=packet.bound)
                    if packet.name not in packet_classes:
                        packet_classes[packet.name] = packet_class
                    assert packet_classes[packet.name] == packet_class, \
                        '[%s] %r != %r' % (v.name,
                        packet_classes[packet.name], packet_class)

                    matrix[v][packet_class] = MatrixID(
                        id=packet.id, base_ver=v, changed=False, url=packet.url,
                        html=packet.html)
            else:
                raise AssertionError('Unrecognised article title: %r' % heading)

            state_bound_ids = {}
            for packet_class, matrix_id in matrix[v].items():
                key = (packet_class.state, packet_class.bound, matrix_id.id)
                assert key not in state_bound_ids, '[%s] Duplicate packet ID: ' \
                    '%s is used by packets %r and %r.' % (v.name,
                    '(%s, %s, 0x%02X)' % key, state_bound_ids[key], packet_class.name)
                state_bound_ids[key] = packet_class.name

            unused_patches = set(k for k in patch.keys() if k[0] == v and k not in used_patches)
            if unused_patches:
                raise AssertionError('Unused patches:\n'
                + '\n'.join('%r -> %r' % (p, patch[p]) for p in unused_patches))

        prev_v = v

    unused_patches = set(k for k in patch.keys() if k not in used_patches)
    if unused_patches:
        raise AssertionError('Unused patches:\n'
        + '\n'.join('%s -> %s' % (p, patch[p]) for p in unused_patches))

    return matrix


import torch
from torch import nn
import torch.nn.functional as F
from torch import optim
from torchvision import datasets, transforms, models
from workspace_utils import active_session
from collections import OrderedDict
import numpy as np
from PIL import Image
import argparse
import json

parser = argparse.ArgumentParser(description='Inference for classification')
parser.add_argument('-i','--image_path',type=str, metavar='', required=True, help='path to image to predict e.g. flowers/test/class/image')
parser.add_argument('-t','--top_k', type=int, metavar='', default=1, help='print out the top K classes along with associated probabilities')
parser.add_argument('-c','--category_names', type=str, metavar='', default='cat_to_name.json', help='load a JSON file that maps the class values to other category names')
parser.add_argument('-g','--gpu',action="store_true", default=False, help='choose training the model on a GPU')

args = parser.parse_args()

with open(args.category_names, 'r') as f:
    cat_to_name = json.load(f)

# a function that loads a checkpoint and rebuilds the model

def load_checkpoint(filepath):
    checkpoint = torch.load(filepath)

     # Load the saved file
    checkpoint = torch.load("checkpoint.pth")
    architecture = checkpoint['architecture']
    # Download pretrained model
    model = getattr(models, architecture)(pretrained=True);
    # Freeze parameters so we don't backprop through them
    for param in model.parameters():
        param.requires_grad = False

    hidden_units = checkpoint['hidden_units']
    model.class_to_idx = checkpoint['class_to_idx']
    model.classifier = checkpoint['classifier']
    model.load_state_dict(checkpoint['state_dict'])
    #optimizer = optim.Adam(model.classifier.parameters())
    #optimizer.load_state_dict(checkpoint['optimizer_state_dict'])
    epochs = checkpoint['epochs']

    return model

model = load_checkpoint('checkpoint.pth')

def process_image(image):
    ''' Scales, crops, and normalizes a PIL image for a PyTorch model,
        returns an Numpy array
    '''
    im = Image.open(image)
    size = 256,256
    im.thumbnail(size)
    #Crop
    left = (256-224)/2
    top = (256-224)/2
    right = (left + 224)
    bottom = (top + 224)
    im = im.crop((left, top, right, bottom))

    np_image = np.array(im)
    np_image = np_image / 255
    mean = [0.485, 0.456, 0.406]
    std = [0.229, 0.224, 0.225]
    np_image = (np_image - mean) / std

    np_image = np_image.transpose(2, 0, 1)
    return np_image

# Reversing idx to class
idx_to_class = {}
for key, value in model.class_to_idx.items():
    idx_to_class[value] = key

def predict(image_path, model, topk):
    ''' Predict the class (or classes) of an image using a trained deep learning model.
    '''
    # Use GPU if it's available
    if args.gpu:
        device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
    # TODO: Implement the code to predict the class from an image file
    #preprocess image
    image = process_image(image_path)
    image = torch.from_numpy(np.array([image])).float()

    # turn off dropout
    model.eval()

    #Load image and the model to cpu or gpu
    model.to(device)
    image = image.to(device)

    logps = model.forward(image)
    ps = torch.exp(logps)
    top_p, top_class = ps.topk(topk, dim=1)

    top_class = np.array(top_class)[0]
    top_p = np.array(top_p.detach())[0]

    # Mapping index to class
    top_classes = []
    for i in range(len(top_class)):
        top_classes.append(idx_to_class[top_class[i]])

    # Mapping class to flower name
    flower_names = []
    for i in range(len(top_classes)):
        flower_names.append(cat_to_name[top_classes[i]])

    return top_p, flower_names

probs, classes = predict(args.image_path, model, args.top_k)

print(f"class Probability: {probs}")
print(f"flower name: {classes}")


import openpyxl
import os
from setting import  get_file_path, get_file_name

file_path = get_file_path()
file_name = get_file_name()

# 切換到指定路徑
os.chdir(file_path)

# 讀進Excel檔案
wb = openpyxl.load_workbook(file_name)

# 取的Excel的第一個工作表
sheet = wb.worksheets[0]

etf_all = dict()

# 彙整全部的ETF清單
for columnNum in range(1, sheet.max_column + 1, 3):
    for rowNum in range(3, sheet.max_row + 1):
        if (sheet.cell(rowNum, columnNum).value == None):
            break
        if (etf_all.get(sheet.cell(rowNum, columnNum).value) == None):
            etf_all[sheet.cell(rowNum, columnNum).value] = {
                'name' : sheet.cell(rowNum, columnNum + 1).value,
                'content' : [sheet.cell(1, columnNum).value]
                }
        else:
            etf_all.get(sheet.cell(rowNum, columnNum).value)['content'].append(sheet.cell(1, columnNum).value)

sorted_list = sorted(etf_all.items(), key=lambda x:len(x[1]['content']), reverse=True)

# 輸出的結果
new_sheet = wb.create_sheet('result')
row = 1
column = 1
for t in sorted_list:
    new_sheet.cell(row, column).value = t[0]
    new_sheet.cell(row, column + 1).value = t[1]['name']
    new_sheet.cell(row, column + 2).value = len(t[1]['content'])
    new_sheet.cell(row, column + 3).value = ','.join(str(etf_id) for etf_id in t[1]['content'])
    row = row + 1

# 存檔
wb.save(file_name)


from flask import request
from flask_restx import Resource

from ..service.auth_service import Auth
from ..util.decorator import admin_token_required
from ..service.user_service import save_new_user, get_a_user
from ..util.dto import AuthDto

api = AuthDto.api
user_auth = AuthDto.user_auth
user_token = AuthDto.user_token

@api.route('/register')
class UserRegister(Resource):
    """
        User Register Resource
    """
    @api.doc('user register')
    @api.expect(user_auth, validate=True)
    def post(self):
        # get the post data
        data = request.json
        return save_new_user(data=data)

@api.route('/login')
class UserLogin(Resource):
    """
        User Login Resource
    """
    @api.doc('user login')
    @api.expect(user_auth, validate=True)
    def post(self):
        # get the post data
        post_data = request.json
        return Auth.login_user(data=post_data)

@api.route('/logout')
class LogoutAPI(Resource):
    """
    Logout Resource
    """
    @api.doc('logout a user')
    @api.doc(params={'Authorization': {'in': 'header', 'description': 'An authorization token'}})
    def post(self):
        """
        logout user
        """
        # get auth token
        auth_header = request.headers.get('Authorization')
        return Auth.logout_user(data=auth_header)

@api.route('/force-logout/<id>')
class ForceLogoutAPI(Resource):
    """
    Force Logout Resource
    """
    @api.doc('force logout a user')
    @api.doc(params={'Authorization': {'in': 'header', 'description': 'An authorization token'}})
    @api.response(404, 'User not found.')
    @admin_token_required
    def post(self, id):
        """
        force logout a user and blacklist all the tokens
        """
        user = user = get_a_user(id)
        if not user:
            api.abort(404)
        return Auth.force_logout_user(user)

@api.route('/expire-token')
class ExpireTokenAPI(Resource):
    """
    Expire Token Resource
    """
    @api.doc('expire a user token')
    @api.doc(params={'Authorization': {'in': 'header', 'description': 'An authorization token'}})
    @api.expect(user_token, validate=True)
    @admin_token_required
    def post(self):
        """
        expire a token passed in post body, admin only authorized
        """
        token = request.json['token']
        return Auth.expire_token(token)


'''Process Restricted Friend Requests
https://leetcode.com/problems/process-restricted-friend-requests/

You are given an integer n indicating the number of people in a network. Each person is labeled from 0 to n - 1.

You are also given a 0-indexed 2D integer array restrictions, where restrictions[i] = [xi, yi] means that person xi
and person yi cannot become friends, either directly or indirectly through other people.

Initially, no one is friends with each other. You are given a list of friend requests as a 0-indexed 2D integer array
requests, where requests[j] = [uj, vj] is a friend request between person uj and person vj.

A friend request is successful if uj and vj can be friends. Each friend request is processed in the given order (i.e.,
requests[j] occurs before requests[j + 1]), and upon a successful request, uj and vj become direct friends for all
future friend requests.

Return a boolean array result, where each result[j] is true if the jth friend request is successful or false if it is
not.

Note: If uj and vj are already direct friends, the request is still successful.

Example 1:

Input: n = 3, restrictions = [[0,1]], requests = [[0,2],[2,1]]
Output: [true,false]
Explanation:
Request 0: Person 0 and person 2 can be friends, so they become direct friends.
Request 1: Person 2 and person 1 cannot be friends since person 0 and person 1 would be indirect friends (1--2--0).
'''

from collections import defaultdict, deque
class Solution:
    def friendRequests(self, n: int, restrictions: List[List[int]], requests: List[List[int]]) -> List[bool]:

        def bfs(node):
            q = deque([node])
            visited = set()
            visited.add(node)
            relations = set()
            while q:
                node = q.popleft()
                relations.add(node)
                for neighbor in friend_mapping[node]:
                    if neighbor not in visited:
                        visited.add(neighbor)
                        q.append(neighbor)
            return relations

        banned_mapping = defaultdict(set)
        for u, v in restrictions:
            banned_mapping[u].add(v)
            banned_mapping[v].add(u)

        result = [False] * len(requests)
        friend_mapping = defaultdict(set)
        for index, req in enumerate(requests):
            u, v = req[0], req[1]
            if v in banned_mapping[u]:
                result[index] = False
            else:
                set1 = bfs(u)
                set2 = bfs(v)
                banned = False
                for key in set1:
                    for bannedId in banned_mapping[key]:
                        if bannedId in set2:
                            result[index] = False
                            banned = True
                if not banned:
                    result[index] = True
                    friend_mapping[u].add(v)
                    friend_mapping[v].add(u)
        return result

Graph/Process Restricted Friend Requests.py


from typing import Any, List

from fastapi import APIRouter, HTTPException, Depends

from apps.auth.model import User
from apps.bank.cruds import invoice
from apps.bank.schemas.invoice import InvoiceUpdate, InvoiceView, InvoiceCreate, InvoiceViewFull
from core.security import current_user_is_banker, get_current_user

router = APIRouter(prefix='/invoices', tags=['Invoices'])

@router.get('/', response_model=List[InvoiceViewFull],
            dependencies=[Depends(current_user_is_banker)])
async def list_invoices(skip: int = 0, limit: int = 100) -> Any:
    results = await invoice.get_list(skip=skip, limit=limit)
    return results

@router.get('/my', response_model=List[InvoiceViewFull])
async def list_my_invoices(user: User = Depends(get_current_user),
                           skip: int = 0, limit: int = 100) -> Any:
    results = await invoice.get_list(user=user, skip=skip, limit=limit)
    return results

@router.get('/{obj_id}', response_model=InvoiceViewFull)
async def get_invoice(obj_id: int, user: User = Depends(get_current_user)) -> Any:
    result = await invoice.get(id=obj_id, user=user)
    if not result:
        raise HTTPException(status_code=404, detail='Invoice not found!')
    return result

@router.post('/create', response_model=InvoiceView, status_code=201)
async def create_invoice(item: InvoiceCreate, user: User = Depends(get_current_user)) -> Any:
    result = await invoice.create_invoice(obj_in=item, user=user)
    return result

@router.put('/{obj_id}', response_model=InvoiceView)
async def update_invoice(obj_id: int, item: InvoiceUpdate,
                         user: User = Depends(get_current_user)) -> Any:
    obj_db = await invoice.get(id=obj_id, user=user)
    if not obj_db:
        raise HTTPException(status_code=404, detail='Invoice not found!')
    result = await invoice.update_invoice(obj_db=obj_db, obj_in=item, user=user)
    return result

@router.delete('/{obj_id}')
async def delete_invoice(obj_id: int, user: User = Depends(get_current_user)) -> Any:
    result = await invoice.remove(id=obj_id, user=user)
    return result


import requests
import bs4
import urllib

def spider(max_pages):
    for page in range(1, max_pages + 1):
        query = urllib.parse.urlencode({'query':u'대선후보'})
        url = 'http://news.naver.com/main/search/search.nhn?query=' + '%B4%EB%BC%B1%C8%C4%BA%B8'
        source_code = requests.get(url)
        plain_text = source_code.text
        soup = bs4.BeautifulSoup(plain_text, 'html.parser')
        content = soup.find(id='search_div')
        for result in content.select('ul > li > div'):
            print('############# Title')
            print(result.a.text)
            print('############# Content')
            print(result.p.text)

spider(1)

C Team(Hong, Heo)/Crowaling/Practice 1.py


# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations

class Migration(migrations.Migration):

    dependencies = [
        ('tapp', '0005_comment_end_i'),
    ]

    operations = [
        migrations.AlterModelOptions(
            name='essay',
            options={'permissions': (('view_essay', 'View essay'),)},
        ),
    ]


from django.urls import path

from . import views

urlpatterns = [
    path("", views.index, name="index"),
    path("login", views.login_view, name="login"),
    path("logout", views.logout_view, name="logout"),
    path("register", views.register, name="register"),
    path("newListing", views.newForm, name="new"),
    path("createListing", views.createNewListing, name="create"),
    path("<str:title>Listing", views.listing_view, name="listing"),
    path("Watchlist", views.watchlist, name="watchlist"),
    path("AddToWatchlist/<str:title>", views.add_to_watchlist, name="add"),
    path("RemoveFromWatchlist/<str:title>", views.remove_from_watchlist, name="remove"),
    path("placeBid/<str:title>", views.place_bid, name="bid"),
    path("closeListing/<str:title>", views.close_listing, name="close"),
    path("postComment/<str:title>", views.post_comment, name="postComment"),
    path("categories", views.categories_view, name="categories"),
    path("showCategory/<str:category>", views.single_category_view, name="showCategory")
]


import numpy as np
import matplotlib.pyplot as plt
import torch
import torch.nn as nn
import torch.nn.functional as F
# import snntorch
import pandas as pd
import tqdm
import argparse
from . import p_snu_layer

class SNN_Net(torch.nn.Module):
    def __init__(self, inputs_num = 4, hidden_num = 4, outputs_num = 3 ,l_tau = 0.8,num_time = 100, batch_size = 80 ,soft = False, rec = False, power = False, gpu = True):
        super().__init__()

        self.num_time = num_time
        self.batch_size = batch_size
        self.rec = rec
        self.power = power

        #parametr
        # self.neuron_0 = 4
        # self.neuron_1 = 24
        # self.neuron_2 = 24
        # self.neuron_3 = 4

        #my #hidden num = 24
        # self.l1 = p_snu_layer.P_SNU(inputs_num, hidden_num, l_tau = l_tau, soft = soft, gpu = gpu)
        # self.l2 = p_snu_layer.P_SNU(hidden_num, hidden_num, l_tau = l_tau, soft = soft, gpu = gpu)
        # self.l3 = p_snu_layer.P_SNU(hidden_num, hidden_num, l_tau = l_tau, soft = soft, gpu = gpu)
        # self.l4 = p_snu_layer.P_SNU(hidden_num, outputs_num, l_tau = l_tau, soft = soft, gpu = gpu)

        #my2 hidden num = 4
        self.l1 = p_snu_layer.P_SNU(inputs_num, hidden_num, l_tau = l_tau, soft = soft, gpu = gpu)
        self.l2 = p_snu_layer.P_SNU(hidden_num, hidden_num, l_tau = l_tau, soft = soft, gpu = gpu)
        self.l3 = p_snu_layer.P_SNU(hidden_num, outputs_num, l_tau = l_tau, soft = soft, gpu = gpu)

        # for 1 layer test
        # self.l4 = p_snu_layer.P_SNU(inputs_num, outputs_num, l_tau = l_tau, soft = soft, gpu = gpu)

    def reset_state(self):
        self.l1.reset_state()
        self.l2.reset_state()
        self.l3.reset_state()
        # self.l4.reset_state()

    def forward(self,x,y):
        # y = torch.tensor(y)
        losse = None
        accuracy = None
        sum_out = None #タイムステップ毎のスパイク数の累積をとる
        out_list = [] #各データ(120×4)のタイムステップ(100ms)における出力スパイクを時系列で挿入
        out_total_list = []
        membrane_out = torch.empty(100,3)
        mem1_out = torch.empty(100,4)
        mem2_out = torch.empty(100,4)
        spikes_ = torch.empty(100,4)

        self.reset_state()

        for time in range(self.num_time): #num_timeはデフォルトで100になる。
            # spike_encoded_neuron = x[time]
            # target_ = torch.reshape(y[time],(1,3))
            # spike_encoded_neuron = torch.reshape(x[time],(4,1))

            #4→4→3(network)
            spike_encoded_neuron = x[time]
            h1,mem1,u1 = self.l1(spike_encoded_neuron)
            h2,mem2,u2 = self.l2(h1)
            # out,mem = self.l3(h2)

            # 1 layer test 4→3(network)
            #膜電位と入出力スパイクの確認
            # out,thresh,spike = self.l4(spike_encoded_neuron)

            #normal
            # out = self.l4(spike_encoded_neuron)

            # sum_out = out if sum_out is None else sum_out + out
            mem1_out[time] = mem1
            mem2_out[time] = mem2
            # membrane_out[time] = out
            spikes_[time] = h1

        #出力を確認する
        # return sum_out,y

        #バッチ学習の場合
        # criterion = nn.CrossEntropyLoss()
        # losse = criterion(sum_out,y)

        #正解率
        # predicted_label = torch.argmax(sum_out)
        # accuracy = 1 if predicted_label == y else 0
        return spikes_,mem1_out,mem2_out
        return mem_out
        return sum_out,losse,accuracy


#
# demo.py
#
import argparse
import os
import numpy as np
import time

from modeling.deeplab import *
from dataloaders import custom_transforms as tr
from PIL import Image
from torchvision import transforms
from dataloaders.utils import *
from torchvision.utils import make_grid, save_image
torch.set_printoptions(profile="full")

def main():
    parser = argparse.ArgumentParser(description="PyTorch DeeplabV3Plus Training")
    parser.add_argument('--in-path', type=str, default=r'D:\PT\archive\Testingset10class\dataset\结果\test_96_label',
                        help='image to test')
    parser.add_argument('--out-path', type=str, default=r'D:\PT\archive\Testingset10class\dataset\结果\96', help='mask image to save')
    parser.add_argument('--backbone', type=str, default='mobilenet',
                        choices=['resnet', 'xception', 'drn', 'mobilenet'],
                        help='backbone name (default: resnet)')
    parser.add_argument('--ckpt', type=str, default=r'D:\PT\超分辨率语义分割\模型保存\10_class\dsrl\128/model_best.pth.tar',
                        help='saved model')
    parser.add_argument('--out-stride', type=int, default=16,
                        help='network output stride (default: 8)')
    parser.add_argument('--no-cuda', action='store_true', default=False,
                        help='disables CUDA training')
    parser.add_argument('--gpu-ids', type=str, default='0',
                        help='use which gpu to train, must be a \
                        comma-separated list of integers only (default=0)')
    parser.add_argument('--dataset', type=str, default='rockdataset',
                        choices=['pascal', 'coco', 'cityscapes', 'rockdataset'],
                        help='dataset name (default: pascal)')
    parser.add_argument('--crop-size', type=int, default=96,
                        help='crop image size')
    parser.add_argument('--num_classes', type=int, default=11,
                        help='crop image size')
    parser.add_argument('--sync-bn', type=bool, default=None,
                        help='whether to use sync bn (default: auto)')
    parser.add_argument('--freeze-bn', type=bool, default=False,
                        help='whether to freeze bn parameters (default: False)')

    args = parser.parse_args()
    args.cuda = not args.no_cuda and torch.cuda.is_available()
    if args.cuda:
        try:
            args.gpu_ids = [int(s) for s in args.gpu_ids.split(',')]
        except ValueError:
            raise ValueError('Argument --gpu_ids must be a comma-separated list of integers only')

    if args.sync_bn is None:
        if args.cuda and len(args.gpu_ids) > 1:
            args.sync_bn = True
        else:
            args.sync_bn = False
    composed_transforms = transforms.Compose([
        tr.Normalize(mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225)),
        tr.ToTensor()])
    for name in os.listdir(args.in_path):
        image = Image.open(args.in_path + "/" + name).convert('RGB')

        # image = Image.open(args.in_path).convert('RGB')
        target = Image.open(args.in_path + "/" + name)
        sample = {'image': image, 'label': target}
        tensor_in = composed_transforms(sample)['label'].unsqueeze(0)
        print(tensor_in.shape)

        grid_image = make_grid(decode_seg_map_sequence(tensor_in.detach().cpu().numpy()),
                               3, normalize=False, range=(0, 255))
        save_image(grid_image, args.out_path + "/" + "{}_label.png".format(name[0:-4]))
        # save_image(grid_image, args.out_path)
        # print("type(grid) is: ", type(grid_image))
        # print("grid_image.shape is: ", grid_image.shape)
    print("image save in in_path.")

if __name__ == "__main__":
    main()

# python demo.py --in-path your_file --out-path your_dst_file


import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm
from matplotlib.ticker import LinearLocator
from numpy.lib.shape_base import split
import math
import cmath

def get_MSI (matrix, f, tau, iterCnt):  #method of simple iterations
    n = np.size(f)
    B = np.diagflat([1] * n) - tau * matrix
    f = tau * f

    x = f
    for i in range (iterCnt):
        x = B.dot(x) + f

    return x

def get_MJ (matrix, f, iterCnt): # method of Jacobi
    u = f
    n = np.size(f)
    for cnt in range(iterCnt):
        uNext = np.zeros(n)
        for i in range (n):
            uNext[i] = (f[i] - matrix[i].dot(u)) / matrix[i][i]
        u = uNext

    return u

def get_MZ (matrix, f, iterCnt):
    u = f
    n = np.size(f)
    answ = np.zeros((n, iterCnt))
    for cnt in range(iterCnt):
        uNext = np.zeros(n)
        for i in range (n):
            uNext[i] = (f[i] -
                                matrix[i][:i - 1].dot(uNext[:i - 1]) -
                                                                        matrix[i][i + 1:].dot(u[i + 1:])) / matrix[i][i]
        u = uNext
        answ[:, cnt] = uNext

    return answ[:, -1]

eigenValuesFilename = "eigenValues15.dat"
#Вход задачи
N = 15  # число точек
h = 1 / (N - 1)
matrix = np.zeros (((N - 2) * (N - 2), N * N))

#построение матрицы
def elem (i, j):
            return N * j + i

for j in range (1, N - 1):
    for i in range (1, N - 1):
        eqID = (j - 1) * (N - 2) + i - 1  #equationID  - индекс строчки

        if (i > 1):
            matrix[eqID][elem(i - 1, j)] = -1

        if (j > 1):
            matrix[eqID][elem(i, j - 1)] = -1

        if (i < N - 2):
            matrix[eqID][elem(i + 1, j)] = -1

        if (j < N - 2):
            matrix[eqID][elem(i, j + 1)] = -1

        matrix[eqID][elem(i, j)] = 4

print ("half")

#Удаление пустых столбцов
zeroColumns = []
for i in range (N * N - 1, -1, -1):
    if i % N == 0 or \
            i < N or \
            i >= N * (N - 1) or \
            (i + 1) % N == 0:
        zeroColumns.append (i)

otherColumns = [x for x in range (N * N - 1) if x not in zeroColumns]
matrix = matrix[:, otherColumns]

print ("matrix is done")
# plt.spy(matrix)
# plt.show()

# поиск собственных чисел
# eigenValues = np.linalg.eigvals(matrix)
# print ("eighenvalues  are computed")
# with open (eigenValuesFilename, "w") as file:
#     for val in eigenValues:
#          file.write(str(val) + "\n")

minVal = 1e9
maxVal = -1e9
with open(eigenValuesFilename, 'r') as file:
    for line in file:
        val = cmath.polar(complex(line))[0]
        if (val > maxVal):
            maxVal = val
        elif val < minVal:
            minVal = val
# Формирование вектора правой части
v = np.zeros((N - 2) * (N - 2))
v[(N // 2) * (N - 2) + N // 2 - 1] = h**2
# v[N * N // 3 : N * N // 2] += h ** 2

#Решаем систему разными методами
# u = get_MSI(matrix, v, 2 / (maxVal + minVal), 100)
# u = get_MSI(matrix, v, 1, 100)
# print (matrix)
# u = get_MJ(matrix, v, 0)
u = get_MZ(matrix, v, 10)

# u = np.linalg.solve(matrix, v)

# Заполняем Матрицу Z для отображения
Z = np.zeros((N, N))
for (uVal, oldID) in zip(u, otherColumns):
    Z[oldID % N][oldID // N] = uVal

fig, ax = plt.subplots(subplot_kw={"projection": "3d"})

w = v = np.linspace(0, 1, N)
X, Y = np.meshgrid(w, v)

surf = ax.plot_surface(X, Y, Z, cmap=cm.coolwarm,
                       linewidth=0, antialiased=False)
ax.zaxis.set_major_locator(LinearLocator(10))
# A StrMethodFormatter is used automatically
# ax.zaxis.set_major_formatter('{x:.02f}')
# Add a color bar which maps values to colors.
fig.colorbar(surf, shrink=0.5, aspect=5)
plt.savefig("pic.png")
# plt.show()