import numpy as np
import cv2
import serial
import os

frame_width = 768
frame_height = 432

saveArray = [(00,00,00,00)]

def sendData(faceX, faceY, width, height):
	data = str(faceX) + "," + str(faceY) + "," + str(width) + "," + str(height) + "," + "\n"
	arduino.write(data.encode())
	print(data)

def outData(centerX, centerY, fWidth, fHeight):
	output = [0,0]
	output[0] = int(centerX + (fWidth/2))
	output[1] = int(centerY + (fWidth/2))
	return output

print("Developed by Waza. Pernambuco")
print("_______________________")
print("|Mars face recognition|")
print("_______________________")

while True:
	print("Press 'q' to start and exit:")
	key = input()
	if key == 'q':
		port = input("Type in the arduino port:")
		break

arduino = serial.Serial(port,9600) # Definir o port antes de mandar

frame_center = (int(frame_width/2) , int(frame_height/2))

face_cascade = cv2.CascadeClassifier("cascade/data/haarcascade_frontalface_alt2.xml")

cap = cv2.VideoCapture(0)

while True:
	ret, frame = cap.read()
	frame = cv2.resize(frame, (frame_width, frame_height))
	grayframe = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

	faces = face_cascade.detectMultiScale(grayframe, scaleFactor = 1.5, minNeighbors = 5)

	for (x,y,w,h) in faces:
		roi_color = frame[y:y+h, x:x+w]

		color = (0,255,0)
		stroke = 3
		face_width = x + w
		face_height = y + h
		print("Pos x:",x,"Pos y:",y)

		pos = outData(x,y,w,h)
		cv2.rectangle(frame, (x, y) , (face_width, face_height), color, stroke)
		sendData(pos[0], pos[1], frame_width, frame_height)

		saveArray += [(x,y,w,h)]

	cv2.imshow('frame',frame)
	if cv2.waitKey(20) & 0xFF == ord('q'):
		break

f = open("/Users/concretekite/Documents/Projetos/Mars Main Code/C/pyNumericData.txt","a+")

for (x,y,w,h) in saveArray:
	outputData = str(x) + " " + str(y) + " " + str(w) + " " + str(h) + "\n"
	f.write(outputData)

f.close()

cap.release()
cv2.destroyAllWindows()


from daxi.control.device.facilitator.config_tools.configuration_generator_mode7 import NIDAQDevicesConfigsGeneratorMode7
from daxi.control.device.facilitator.config_tools.plot_daq_devices_voltage_profiles import plot_daq_voltage_profiles, \
    plot_daq_voltage_profiles_single_cycle_dict
from daxi.control.process.facilitator.processes_facilitator import load_process_configs
from daxi.globals_configs_constants_general_tools_needbettername.constants import process_configs_yaml_path_mode7_short

process_configs = load_process_configs(path=process_configs_yaml_path_mode7_short)
acquisition_parameters = process_configs['process configs']['acquisition parameters']
daq_terminal_configs = process_configs['device configurations']['nidaq_terminals']
calibration_records = process_configs['device configurations']['calibration_records']
alignment_records = process_configs['device configurations']['alignment_records']
camera_core_configs = process_configs['device configurations']['camera_core_configs']

a = NIDAQDevicesConfigsGeneratorMode7(process_configs=process_configs,
                                      params=acquisition_parameters,
                                      nidaq_terminals=daq_terminal_configs,
                                      calibration_records=calibration_records,
                                      alignment_records=alignment_records)

a.get_configs_for_metronome()
a.get_configs_for_counter()
a.get_configs_do_task_bundle()
a.get_configs_ao_task_bundle()
a.get_configs_scanning_galvo(params=None)
a.get_configs_view_switching_galvo_1()
a.get_configs_view_switching_galvo_2()
a.get_configs_gamma_galvo_strip_reduction(params=None)
a.get_configs_beta_galvo_light_sheet_incident_angle(params=None)
a.get_configs_o3(params=None)
a.get_configs_405_laser(params=None)
a.get_configs_488_laser(params=None)
a.get_configs_561_laser(params=None)
a.get_configs_639_laser(params=None)
a.get_configs_bright_field(params=None)
a.get_configs_o1(params=None)

plot_daq_voltage_profiles(configs=a,
                          data_points_to_show=10000)

# now get single cycle dictionary
configs_daq_single_cycle_dict = \
    a.get_configs_single_cycle_dict(params=acquisition_parameters)

# and perhaps plot all the sequences for each cycle type
plot_daq_voltage_profiles_single_cycle_dict(configs_dict=configs_daq_single_cycle_dict['view1 colorbright_field'],
                                            data_points_to_show=30000,
                                            dict_key='view1 colorbright_field',
                                            process_configs=a.process_configs)


def k_freq(d, k):
    "Returns kth freq elements"
    res = []
    try:
        for i in range(k):
            res.append(d[i][0])
        return res
    except:
        return "K out of range"

if __name__ == "__main__":
    lis = [4, 4, 4, 4, 4, 5]
    dic = {}
    for i in lis:
        if i not in dic.keys():
            dic[i] = 1
        else:
            dic[i] += 1
    new = sorted(dic.items(), key=lambda x: x[1], reverse=True)
    print(k_freq(new, 3))


from drf_rw_serializers.generics import CreateAPIView
from drf_spectacular.utils import OpenApiResponse, extend_schema, inline_serializer
from rest_framework import serializers, status
from rest_framework.parsers import MultiPartParser
from rest_framework.response import Response

from webapi.serializers.images import ReadImageSerializer, WriteImageSerializer

class ImageCreateAPIView(CreateAPIView):
    parser_classes = (MultiPartParser,)
    read_serializer_class = ReadImageSerializer
    write_serializer_class = WriteImageSerializer

    @extend_schema(
        operation_id="uploadImage",
        request={
            "multipart/form-data": {
                "type": "object",
                "properties": {
                    "image": {
                        "type": "string",
                        "format": "binary",
                    },
                },
                "required": [
                    "image",
                ],
            },
        },
        responses={
            status.HTTP_200_OK: ReadImageSerializer,
            status.HTTP_400_BAD_REQUEST: OpenApiResponse(
                response=inline_serializer(
                    name="ImageBadRequestResponse",
                    fields={
                        "error_detail": serializers.CharField(required=True),
                    },
                ),
            ),
        },
        tags=["image"],
        description="画像アップロード",
    )
    def post(self, request, *args, **kwargs):
        return super().post(request, *args, **kwargs)

    def create(self, request, *args, **kwargs):
        write_serializer = self.get_write_serializer(data=request.data)
        write_serializer.is_valid(raise_exception=True)
        self.perform_create(write_serializer)

        read_serializer = self.get_read_serializer(write_serializer.instance)

        return Response(read_serializer.data, status=status.HTTP_200_OK)


import sys
from getpass import getpass
from pathlib import Path
from pprint import pprint
import subprocess
import json

DEPLOYMENT_NAME = "ekklesia-test"
KEEP_KEYS = True

def run(cmd):
    return subprocess.run(cmd, capture_output=True, check=True, shell=True)

def ret(cmd):
    return subprocess.run(cmd, capture_output=True, shell=True).returncode

def sh(cmd):
    print(cmd)
    run(cmd)

def secret(cmd_template, **secrets):
    print(cmd_template)
    run(cmd_template.format(**secrets))

def get_ekklesia_settings(vm_name):
    settings = json.loads(
        run(f"nixops show-option -d {DEPLOYMENT_NAME} {vm_name} services.ekklesia --json").stdout)

    #pprint(settings)
    return settings

def create_and_fetch_keys(server_number):
    vm_name = f"vvvote{server_number}"
    settings = get_ekklesia_settings(vm_name)
    keydir = Path(settings["vvvote"]["privateKeydir"])

    ssh = f"nixops ssh -d {DEPLOYMENT_NAME} {vm_name}"
    fetch = f"nixops scp -d {DEPLOYMENT_NAME} --from {vm_name}"

    if not KEEP_KEYS or ret(f"{ssh} stat {keydir.parent}/.dont_overwrite_keys") > 0:
        sh(f"{ssh} mkdir -p {keydir.parent}/voting-keys")
        sh(f"{ssh} mkdir -p {keydir}")
        sh(f"{ssh} vvvote-admin.sh createKeypair p {server_number} {keydir.parent}")
        sh(f"{ssh} vvvote-admin.sh createKeypair t {server_number} {keydir.parent}")
        sh(f"{ssh} mv {keydir.parent}/voting-keys/\*private\* {keydir}")
        sh(f"{ssh} touch /var/lib/vvvote/.dont_overwrite_keys")
    else:
        print(f"Keys are already set up for {vm_name}, refusing to create private keys. "
        + f"Remove {keydir.parent}/.dont_overwrite_keys to force key creation.")

    sh(f"{fetch} {keydir.parent}/voting-keys/\*public\* vvvote-public-keys/")

def push_public_keys(server_number):
    vm_name = f"vvvote{server_number}"
    settings = get_ekklesia_settings(vm_name)
    keydir = Path(settings["vvvote"]["settings"]["publicKeydir"])

    ssh = f"nixops ssh -d {DEPLOYMENT_NAME} {vm_name}"
    push = f"nixops scp -d {DEPLOYMENT_NAME} --to {vm_name}"

    sh(f"{ssh} mkdir -p {keydir}")
    keys = Path("vvvote-public-keys").glob("*.publickey.pem")
    for key in keys:
        sh(f"{push} {str(key)} {keydir}")

def set_up_secrets(server_number):
    vm_name = f"vvvote{server_number}"
    settings = get_ekklesia_settings(vm_name)
    oauth_path = settings["vvvote"]["oauthClientSecretFile"]
    notify_path = settings["vvvote"]["notifyClientSecretFile"]

    ssh = f"nixops ssh -d {DEPLOYMENT_NAME} {vm_name}"
    oauth_client_secret = getpass(f"oauth client secret for {vm_name} (Enter means no change):")
    notify_secret = getpass(f"notify client secret for {vm_name} (Enter means no change):")

    if oauth_client_secret:
        secret(f"{ssh} 'echo {{oauth_client_secret}} > {oauth_path}'", oauth_client_secret=oauth_client_secret)

    if notify_secret:
        secret(f"{ssh} 'echo {{notify_secret}} > {notify_path}'", notify_secret=notify_secret)

sh("mkdir -p vvvote-public-keys")

create_and_fetch_keys(1)
create_and_fetch_keys(2)
push_public_keys(1)
push_public_keys(2)
set_up_secrets(1)
set_up_secrets(2)


import json
import hashlib
import statistics
from flask import Blueprint, request, send_file
from urllib import parse
from zipfile import BadZipFile
from mongo import *
from mongo import engine
from mongo import sandbox
from .auth import *
from .utils import *
from mongo.utils import can_view_problem
from mongo.problem import *

__all__ = ['problem_api']

problem_api = Blueprint('problem_api', __name__)

@problem_api.route('/', methods=['GET'])
@login_required
@Request.args(
    'offset',
    'count',
    'problem_id',
    'tags',
    'name',
    'course',
)
def view_problem_list(
    user,
    offset,
    count,
    tags,
    problem_id,
    name,
    course,
):
    # casting args
    try:
        if offset is not None:
            offset = int(offset)
        if count is not None:
            count = int(count)
    except (TypeError, ValueError):
        return HTTPError(
            'offset and count must be integer!',
            400,
        )
    problem_id, name, tags, course = (parse.unquote(p or '') or None
                                      for p in (problem_id, name, tags,
                                                course))
    try:
        ks = {
            'user': user,
            'offset': offset,
            'count': count,
            'tags': tags and tags.split(','),
            'problem_id': problem_id,
            'name': name,
            'course': course,
        }
        ks = {k: v for k, v in ks.items() if v is not None}
        data = Problem.get_problem_list(**ks)
    except IndexError:
        return HTTPError('invalid offset', 400)
    data = [{
        'problemId': p.problem_id,
        'problemName': p.problem_name,
        'status': p.problem_status,
        'ACUser': p.ac_user,
        'submitter': p.submitter,
        'tags': p.tags,
        'type': p.problem_type,
        'quota': p.quota,
        'submitCount': Problem(p.problem_id).submit_count(user)
    } for p in data]
    return HTTPResponse('Success.', data=data)

@problem_api.route('/<int:problem_id>', methods=['GET'])
@problem_api.route('/view/<int:problem_id>', methods=['GET'])
@login_required
@Request.doc('problem_id', 'problem', Problem)
def view_problem(user: User, problem: Problem):
    if not can_view_problem(user, problem.obj):
        return HTTPError('Problem cannot view.', 403)
    # ip validation
    if not problem.is_valid_ip(get_ip()):
        return HTTPError('Invalid IP address.', 403)
    # filter data
    data = problem.detailed_info(
        'problemName',
        'description',
        'owner',
        'tags',
        'allowedLanguage',
        'courses',
        'quota',
        defaultCode='defaultCode',
        status='problemStatus',
        type='problemType',
        testCase='testCase__tasks',
    )
    if problem.obj.problem_type == 1:
        data.update({'fillInTemplate': problem.obj.test_case.fill_in_template})
    data.update({
        'submitCount': problem.submit_count(user),
        'highScore': problem.get_high_score(user=user),
    })
    return HTTPResponse('Problem can view.', data=data)

@problem_api.route('/manage', methods=['POST'])
@problem_api.route(
    '/manage/<int:problem_id>',
    methods=[
        'GET',
        'PUT',
        'DELETE',
    ],
)
@identity_verify(0, 1)
def manage_problem(user, problem_id=None):
    @Request.json('type', 'courses: list', 'status', 'type', 'description',
                  'tags', 'problem_name', 'quota')
    def modify_problem(**p_ks):
        if p_ks['type'] == 2:
            return modify_general_problem(**p_ks)
        else:
            return modify_coding_problem(**p_ks)

    @Request.json(
        'test_case_info',
        'can_view_stdout',
        'allowed_language',
        'default_code',
    )
    def modify_coding_problem(**p_ks):
        scores = (c['taskScore'] for c in p_ks['test_case_info']['tasks'])
        if sum(scores) != 100:
            return HTTPError("Cases' scores should be 100 in total", 400)
        return modify_general_problem(**p_ks)

    def modify_general_problem(**p_ks):
        if request.method == 'POST':
            pid = Problem.add(user=user, **p_ks)
            return HTTPResponse('Success.', data={'problemId': pid})
        elif request.method == 'PUT':
            Problem.edit_problem(
                user=user,
                problem_id=problem_id,
                **p_ks,
            )
            return HTTPResponse('Success.')

    @Request.files('case')
    def modify_problem_test_case(case):
        try:
            result = Problem.edit_problem_test_case(problem_id, case)
        except engine.DoesNotExist as e:
            return HTTPError(str(e), 404)
        except (ValueError, BadZipFile) as e:
            return HTTPError(str(e), 400)
        except BadTestCase as e:
            return HTTPError(str(e), 400, data=e.dict)
        return HTTPResponse('Success.', data=result)

    # get problem object from DB
    if request.method != 'POST':
        problem = Problem(problem_id).obj
        if problem is None:
            return HTTPError('Problem not exist.', 404)
        if user.role == 1 and problem.owner != user.username:
            return HTTPError('Not the owner.', 403)
    # return detailed problem info
    if request.method == 'GET':
        problem = Problem(problem_id)
        info = problem.detailed_info(
            'courses',
            'problemName',
            'description',
            'tags',
            'testCase',
            'ACUser',
            'submitter',
            'allowedLanguage',
            'canViewStdout',
            'quota',
            status='problemStatus',
            type='problemType',
        )
        info.update({'submitCount': problem.submit_count(user)})
        return HTTPResponse(
            'Success.',
            data=info,
        )
    # delete problem
    elif request.method == 'DELETE':
        Problem.delete_problem(problem_id)
        return HTTPResponse('Success.')
    # edit problem
    else:
        try:
            # modify problem meta
            if request.content_type.startswith('application/json'):
                return modify_problem()
            # upload testcase file
            elif request.content_type.startswith('multipart/form-data'):
                return modify_problem_test_case()
            else:
                return HTTPError(
                    'Unknown content type',
                    400,
                    data={'contentType': request.content_type},
                )
        except ValidationError as ve:
            return HTTPError(
                'Invalid or missing arguments.',
                400,
                data=ve.to_dict(),
            )
        except engine.DoesNotExist:
            return HTTPError('Course not found.', 404)

@problem_api.route('/<int:problem_id>/testcase', methods=['GET'])
@login_required
@identity_verify(0, 1)
def get_testcase(user, problem_id):
    problem = Problem(problem_id).obj
    if problem is None:
        return HTTPError(f'Unexisted problem id ({problem_id})', 404)
    return send_file(
        problem.test_case.case_zip,
        mimetype='application/zip',
        as_attachment=True,
        attachment_filename=f'testdata-{problem_id}.zip',
    )

# FIXME: Find a better name
@problem_api.route('/<int:problem_id>/testdata', methods=['GET'])
@Request.args('token: str')
def get_testdata(token: str, problem_id: int):
    if sandbox.find_by_token(token) is None:
        return HTTPError('Invalid sandbox token', 401)
    problem = Problem(problem_id)
    if not problem:
        return HTTPError(f'{problem} not found')
    return send_file(
        problem.test_case.case_zip,
        mimetype='application/zip',
        as_attachment=True,
        attachment_filename=f'testdata-{problem_id}.zip',
    )

@problem_api.route('/<int:problem_id>/checksum', methods=['GET'])
@Request.args('token: str')
def get_checksum(token: str, problem_id: int):
    if sandbox.find_by_token(token) is None:
        return HTTPError('Invalid sandbox token', 401)
    problem = Problem(problem_id)
    if not problem:
        return HTTPError(f'{problem} not found')
    meta = json.dumps({
        'tasks':
        [json.loads(task.to_json()) for task in problem.test_case.tasks]
    }).encode()
    content = problem.test_case.case_zip.read() + meta
    digest = hashlib.md5(content).hexdigest()
    return HTTPResponse(data=digest)

@problem_api.route('/<int:problem_id>/meta', methods=['GET'])
@Request.args('token: str')
def get_meta(token: str, problem_id: int):
    if sandbox.find_by_token(token) is None:
        return HTTPError('Invalid sandbox token', 401)
    problem = Problem(problem_id)
    if not problem:
        return HTTPError(f'{problem} not found')
    meta = {
        'tasks':
        [json.loads(task.to_json()) for task in problem.test_case.tasks]
    }
    return HTTPResponse(data=meta)

@problem_api.route('/<int:problem_id>/high-score', methods=['GET'])
@login_required
@Request.doc('problem_id', 'problem', Problem)
def high_score(user: User, problem: Problem):
    return HTTPResponse(data={
        'score': problem.get_high_score(user=user),
    })

@problem_api.route('/clone', methods=['POST'])
@identity_verify(0, 1)
@Request.json(vars_dict={'problem_id': 'problemId'})
def clone_problem(user, problem_id):
    problem = Problem(problem_id).obj
    if problem is None:
        return HTTPError('Problem not exist.', 404)
    if not can_view_problem(user, problem):
        return HTTPError('Problem can not view.', 403)
    Problem.copy_problem(user, problem_id)
    return HTTPResponse('Success.')

@problem_api.route('/publish', methods=['POST'])
@identity_verify(0, 1)
@Request.json(vars_dict={'problem_id': 'problemId'})
def publish_problem(user, problem_id):
    problem = Problem(problem_id).obj
    if problem is None:
        return HTTPError('Problem not exist.', 404)
    if user.role == 1 and problem.owner != user.username:
        return HTTPError('Not the owner.', 403)
    Problem.release_problem(problem_id)
    return HTTPResponse('Success.')

@problem_api.route('/<int:problem_id>/stats', methods=['GET'])
@login_required
@Request.doc('problem_id', 'problem', Problem)
def problem_stats(user: User, problem: Problem):
    if not can_view_problem(user, problem.obj):
        return HTTPError('Problem cannot view.', 403)
    ret = {}
    students = []
    for course in problem.courses:
        students += [User(name) for name in course.student_nicknames.keys()]
    students_high_scores = [problem.get_high_score(user=u) for u in students]
    # These score statistics are only counting the scores of the students in the course.
    ret['acUserRatio'] = [problem.get_ac_user_count(), len(students)]
    ret['triedUserCount'] = problem.get_tried_user_count()
    ret['average'] = None if len(students) == 0 else statistics.mean(
        students_high_scores)
    ret['std'] = None if len(students) <= 1 else statistics.pstdev(
        students_high_scores)
    ret['scoreDistribution'] = students_high_scores
    # However, submissions include the submissions of teacher and admin.
    ret['statusCount'] = problem.get_submission_status()
    params = {
        'user': user,
        'offset': 0,
        'count': 10,
        'problem': problem.id,
        'status': 0,
    }
    top_10_runtime_submissions = [
        s.to_dict() for s in Submission.filter(**params, sort_by='runTime')
    ]
    ret['top10RunTime'] = top_10_runtime_submissions
    top_10_memory_submissions = [
        s.to_dict() for s in Submission.filter(**params, sort_by='memoryUsage')
    ]
    ret['top10MemoryUsage'] = top_10_memory_submissions
    return HTTPResponse('Success.', data=ret)


from transformers import GPT2ForSequenceClassification, GPT2Tokenizer
import os
import fire

def build_model(model_dir="~/pretrained", model_name="gpt2"):
    model = GPT2ForSequenceClassification.from_pretrained(model_name)
    tokenizer = GPT2Tokenizer.from_pretrained(model_name)

    # Define a padding token
    tokenizer.pad_token = tokenizer.eos_token
    model.config.pad_token_id = tokenizer.pad_token_id

    save_dir = os.path.join(os.path.expanduser(model_dir), model_name)
    os.makedirs(save_dir, exist_ok=True)
    # Save the model and tokenizer in a directory
    model.save_pretrained(save_dir)
    tokenizer.save_pretrained(save_dir)

if __name__ == "__main__":
    fire.Fire(build_model)


from dataclasses import dataclass

@dataclass
class Bin:
    items: list
    value_sum: float

# Az optimum >= summa az i-re nézve ti ahol ti az egyes inputok
def FindBestBin(bins, item):
    bins.sort(key=lambda bin: bin.value_sum, reverse=True)

    for bin in bins:
        if bin.value_sum + item <= 1.0:
            return bin

    return None

# Annyit tudunkk, h legalább 3 ládát fog használni, mert kicsi átrendezéssel látható
def BestFit(items):
    bins = [Bin([], 0.0)]

    for i, item in enumerate(items):

        best_bin = FindBestBin(bins, item)
        if best_bin is not None:
            best_bin.items.append(item)
            best_bin.value_sum += item
        else:
            bins.append(Bin([item], item))

    return bins

items = [0.5, 0.6, 0.3, 0.4, 0.2, 0.5, 0.4]
bins = BestFit(items)

for idx, bin in enumerate(bins):
    print(f'Bin #{idx} Value sum: {bin.value_sum} {bin.items}')


# Definir a base de conhecimento
conhecimento = {
    'sem_combustivel': {
        'sintomas': ['motor não pega'],
        'solucao': 'abastecer o tanque'
    },
    'bateria_descarregada': {
        'sintomas': ['motor não pega', 'faróis fracos'],
        'solucao': 'recarregar ou substituir a bateria'
    },
    'falha_vela_ignicao': {
        'sintomas': ['motor falhando', 'ruído metálico'],
        'solucao': 'substituir a vela de ignição'
    },
    'falha_motor_partida': {
        'sintomas': ['motor não pega', 'barulho metálico'],
        'solucao': 'reparar ou substituir o motor de partida'
    }
}

# Obter informações do usuário
sintomas = []
sintoma = input('Qual o sintoma observado na ignição do veículo? ')
while sintoma != '':
    sintomas.append(sintoma)
    sintoma = input('Qual o próximo sintoma observado na ignição do veículo? ')

# Realizar o diagnóstico com base nas informações do usuário e na base de conhecimento
diagnostico = []
for falha, info in conhecimento.items():
    if all(sintoma in info['sintomas'] for sintoma in sintomas):
        diagnostico.append(info['solucao'])

# Apresentar o resultado do diagnóstico
if diagnostico:
    print('O problema pode ser resolvido com as seguintes soluções: ')
    for solucao in diagnostico:
        print(solucao)
else:
    print('Não foi possível determinar a causa do problema na ignição.')


import hashlib
import base58
from cryptography.hazmat.primitives.asymmetric import ec
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import serialization
# from ecdsa import  SECP256k1

def Bitcoin_KEYS():

    private_key = ec.generate_private_key(ec.SECP256K1 ,default_backend)

    public_key = private_key.public_key();

    return private_key, public_key

# hashed

def Hashed(value):
    return hashlib.sha256(value).digest();

# added
def HASHED_concatenate(x ,y):
    return x+y

# ripemd

def ripemd(value):
    return hashlib.new('ripemd160', value).digest()

# generate Bitcoin address

def generate_bitcoin_address(public_key):

    sha256_hash = Hashed(public_key)

    ripemd160_hash = ripemd(sha256_hash)

    version_byte = b'\x00'

    extended_ripe_hash = HASHED_concatenate(ripemd160_hash ,version_byte)

    sha256_hash2 = Hashed(extended_ripe_hash)

    sha256_hash3 = Hashed(sha256_hash2)

    checksum = sha256_hash3[:8]

    extendedripehash = HASHED_concatenate(extended_ripe_hash , checksum)

    bitcoinaddress = base58.b58encode(extendedripehash).decode()

    return bitcoinaddress

def MAIN():
    # Generate ECDSA keys
    private_key, public_key = Bitcoin_KEYS()

    # Convert public key bytes to hexadecimal representation
    publicbytes = public_key.public_bytes(encoding=serialization.Encoding.X962
    ,format=serialization.PublicFormat.UncompressedPoint);

    # Generate Bitcoin wallet address
    bitcoin_address = generate_bitcoin_address(publicbytes)
    print("Bitcoin Wallet Address is:", bitcoin_address)

MAIN()


"""
Question: https://leetcode.com/problems/sum-of-all-odd-length-subarrays/
"""

from typing import List

class Solution:
    def sumOddLengthSubarrays(self, arr: List[int]) -> int:

        prefixSum = [0]
        currSum = 0
        for num in arr:
            currSum += num
            prefixSum.append(currSum)

        idx = 3
        # return prefixSum
        result = 0
        while idx <= len(prefixSum):
            for j in range(idx, len(prefixSum)):
                result += (prefixSum[j] - prefixSum[j-idx])
            idx += 2

        return result + sum(arr)

testCases = [
    {
        "arr": [1, 4, 2, 5, 3],
        "output": 58
    },
    {
        "arr": [1, 2],
        "output": 3
    },
    {
        "arr": [10, 11, 12],
        "output": 66
    }
]

for testCase in testCases:
    runner = Solution()
    result = runner.sumOddLengthSubarrays(testCase["arr"])
    print(result)


#! /usr/bin/env python

from keras.layers import Dense, Convolution2D, MaxPooling2D, UpSampling2D, ZeroPadding2D
from keras.models import Sequential
from keras.models import mnist
from keras.preprocessing.image import ImageDataGenerator
import numpy as np
import glob
from skimage.data import imread
import pdb

X_train = []
y_train = []
X_test = []
y_test = []
datagen = []

def prepare_data(input_path, target_path, test_path):
    # Read input, target and test files
    for input_file in glob.glob(input_path + '/*.png'):
        img = imread(input_file)
        img = img.reshape(1, img.shape[0], img.shape[1])
        X_train.append(img)

    for target_file in glob.glob(target_path + '/*.png'):
        img = imread(input_file)
        img = img.reshape(1, img.shape[0], img.shape[1])
        y_train.append(img)

    for test_file in glob.glob(test_path + '/*.png'):
        img = imread(input_file)
        img = img.reshape(1, img.shape[0], img.shape[1])
        X_test.append(img)

    pdb.set_trace()

    # Pre-process data (std normalization - Z-score)
    #datagen = ImageDataGenerator(featurewise_center=True,
    #                             featurewise_std_normalization=True)

    #datagen.fit(X_train)
    #datagen.fit(y_train)
    #datagen.fit(X_test)

def model():
    model = Sequential()
    # border_mode = 'same' does padding
    model.add(Convolution2D(32, 5, 5, activation='relu', border_mode='same', input_shape=(1, 420, 540)))
    # Input at this stage = (32, 420, 540)
    model.add(MaxPooling2D((2, 2), border_mode='same'))
    # inp dim = (32, 210, 270)
    model.add(Convolution2D(16, 5, 5, activation='relu', border_mode='same'))
    # inp dim = (16, 210, 270)
    model.add(MaxPooling2D((2, 2), border_mode='same'))
    # NOTE : max pooling at this layer gives odd dimensions, can create
    # problems)
    # inp dim = (16, 105, 135)
    model.add(Convolution2D(16, 5, 5, activation='relu', border_mode='same'))
    # inp dim = (16, 105, 135)
    model.add(MaxPooling2D((2, 2), border_mode='same'))
    # inp dim = (16, 52, 67)
    model.add(Convolution2D(8, 5, 5, activation='relu', border_mode='same'))
    # inp dim = (8, 52, 67)
    #model.add(MaxPooling2D(2, 2))
    model.add(Convolution2D(8, 5, 5, activation='relu', border_mode='same'))
    # dim = (8, 52, 67)
    model.add(UpSampling2D((2, 2)))
    # dim = (8, 104, 134)
    model.add(ZeroPadding2D(padding=(1,1)))
    # dim = (8, 105, 135)
    model.add(Convolution2D(16, 5, 5, activation='relu', border_mode='same'))
    # dim = (16, 105, 135)
    model.add(UpSampling2D((2, 2)))
    # dim = (16, 210, 270)
    model.add(Convolution2D(32, 5, 5, activation='relu', border_mode='same'))
    # dim = (32, 210, 270)
    model.add(UpSampling2D((2, 2)))
    # dim = (32, 420, 540)
    model.add(Convolution2D(1, 5, 5, activation='relu', border_mode='same'))
    # dim = (1, 420, 540)

    model.compile(loss='mse', optimizer='adam')

    model.fit(X_train, y_train, nb_epoch=25, batch_size=128, shuffle=True)

def main(input_path, target_path, test_path):
    prepare_data(input_path, target_path, test_path)
    model()

if __name__ == "__main__":
    main('/home/shashank/data/kaggle_doc_denoising/train', '/home/shashank/data/kaggle_doc_denoising/train_cleaned', '/home/shashank/data/kaggle_doc_denoising/test')
    X_train = np.asarray(X_train)
    y_train = np.asarray(y_train)
    X_test = np.asarray(X_test)
    pdb.set_trace()


import json
import os
import random

import numpy as np
import tensorflow as tf
import cv2

from tensorflow.python.keras.callbacks import TensorBoard, CSVLogger
from tensorflow.python.keras.layers import Lambda
from tensorflow.python.keras.optimizer_v2.adam import Adam
from tensorflow.python.keras.backend import get_session
from tqdm import tqdm
from absl import logging

from backbones import DetectionBody
from evaluate import coco_api_eval
from utils.bbox import xywh2xymimx
from utils.coco import CocoUtil
from utils.dataset import tf_distort_image, tf_random_number, get_ds
from utils.image import read_img
from utils.inspect_util import draw_box
from utils.misc import get_dir, get_clean_dir
from prepare.write_tfrecord import parse_example_protobuf
from utils.pascal import TargetMissionUtil

def _loss_wrapper(mean_true):
    def _loss(y_true, y_predict):
        confidence_logits = y_predict[..., 0]
        delta_yx_logits = y_predict[..., 1:3]
        side_distance_logits = y_predict[..., 3:5]
        class_label = y_predict[..., 5:]

        alpha = .25
        gamma = 2.
        epsion = 1e-7

        # focal loss
        confidence_loss = -alpha * tf.pow(1 - tf.sigmoid(confidence_logits), gamma) * tf.math.log(
            epsion + tf.sigmoid(confidence_logits)) * y_true[..., 0] + \
                          -(1 - alpha) * tf.pow(tf.sigmoid(confidence_logits), gamma) * tf.math.log(
            epsion + 1 - tf.sigmoid(confidence_logits)) * (1 - y_true[..., 0])
        confidence_loss = tf.reduce_sum(confidence_loss)

        # confidence_loss = y_true[..., 0] * tf.nn.sigmoid_cross_entropy_with_logits(labels=y_true[..., 0], logits=confidence_logits) + \
        #                   (1 - y_true[..., 0]) * tf.nn.sigmoid_cross_entropy_with_logits(labels=y_true[..., 0], logits=confidence_logits)

        object_mask = tf.equal(y_true[..., 0], 1.)
        true_nums = tf.reduce_sum(tf.cast(object_mask, tf.float32))
        non_zero_true_nums = tf.cond(tf.equal(true_nums, 0), lambda: mean_true, lambda: true_nums)

        delta_yx_loss = tf.reduce_sum(tf.boolean_mask(
            tf.compat.v1.losses.huber_loss(y_true[..., 1:3], tf.tanh(delta_yx_logits), delta=0.5, reduction='none'),
            object_mask))

        side_distance_loss = tf.reduce_sum(tf.boolean_mask(
            tf.compat.v1.losses.huber_loss(y_true[..., 3:5], tf.sigmoid(side_distance_logits), delta=0.5, reduction='none'),
            object_mask))

        class_loss = tf.reduce_sum(tf.boolean_mask(
            tf.nn.sigmoid_cross_entropy_with_logits(labels=y_true[..., 5:], logits=class_label),
            object_mask))

        # confidence_loss = tf.math.divide(confidence_loss, non_zero_true_nums)
        # delta_yx_loss = tf.math.divide(delta_yx_loss, non_zero_true_nums)
        # side_distance_loss = tf.math.divide(side_distance_loss, non_zero_true_nums)
        # class_loss = tf.math.divide(class_loss, non_zero_true_nums)

        total_loss = confidence_loss + delta_yx_loss + 4 * side_distance_loss + 0.2 * class_loss

        # todo not work
        tf.compat.v1.summary.scalar("total_loss", total_loss)
        tf.compat.v1.summary.scalar("confidence_loss", confidence_loss)
        tf.compat.v1.summary.scalar("delta_yx_loss", delta_yx_loss)
        tf.compat.v1.summary.scalar("side_distance_oss", side_distance_loss)
        tf.compat.v1.summary.scalar("class_loss", class_loss)

        # total_loss = tf.Print(total_loss, [total_loss, non_zero_true_nums, confidence_loss, delta_yx_loss, side_distance_loss, class_loss], message='loss: ')
        return total_loss

    return _loss

def _image_preprocess(origin_image, input_shape):
    origin_h, origin_w = origin_image.shape[:-1]

    aspect_ratio = origin_h / origin_w

    input_h, input_w = input_shape

    if origin_w < origin_h:
        new_h = input_h
        new_w = new_h / aspect_ratio
    elif origin_w > origin_h:
        new_w = input_w
        new_h = new_w * aspect_ratio
    else:
        new_w = input_w
        new_h = input_h

    new_h = int(new_h)
    new_w = int(new_w)

    # resize image with keep the aspect ratio, then do padding
    image = cv2.resize(origin_image, (new_w, new_h), cv2.INTER_CUBIC)
    image = image.astype(np.float32)
    image = image / 255

    new_image = np.zeros((input_h, input_w, 3), dtype=np.float32)

    if origin_w < origin_h:
        offset_y = 0
        offset_x = int((input_w - new_w) / 2 - 1)
    elif origin_w > origin_h:
        offset_x = 0
        offset_y = int((input_h - new_h) / 2 - 1)
    else:
        offset_x = offset_y = 0
    new_image[offset_y:offset_y + image.shape[0], offset_x: offset_x + image.shape[1]] = image
    return new_image, (offset_x, offset_y), (new_w / origin_w, new_h / origin_h)

def pyfunc_prepare_true(image, gts, labels, height, width, input_shape, num_classes, whether_flip,
                        stride_list, desire_area):
    input_h, input_w = input_shape

    new_image, (offset_x, offset_y), (w_resize_ratio, h_resize_ratio) = _image_preprocess(image, input_shape)

    # fit the gt into new image
    gts[:, [0, 2]] = gts[:, [0, 2]] * w_resize_ratio
    gts[:, [1, 3]] = gts[:, [1, 3]] * h_resize_ratio
    gts[:, 0] = gts[:, 0] + offset_x
    gts[:, 1] = gts[:, 1] + offset_y

    assert len(gts) == len(labels), "gt and labels must have same length"

    # convert from (xmin, ymin, width, height) to (ymin, xmin, ymax, xmax)
    gts = np.stack([gts[:, 1], gts[:, 0], (gts[:, 1] + gts[:, 3]), (gts[:, 0] + gts[:, 2])], axis=1)

    # print(whether_flip)
    if whether_flip:
        gts[:, [1, 3]] = input_w - gts[:, [3, 1]]

    y_true = []

    # choose the desire area for output, etc, the smallest output will predict the largest objects
    areas = (gts[:, 2] - gts[:, 0]) * (gts[:, 3] - gts[:, 1])

    for idx, stride in enumerate(stride_list):
        cur_desire_area = desire_area[idx]
        desired_gt_indice = np.where((areas >= cur_desire_area[0]) & (areas <= cur_desire_area[1]))

        cur_gt = gts[desired_gt_indice]
        cur_label = labels[desired_gt_indice]

        if len(desired_gt_indice) == 0:
            continue

        grid_h, grid_w = input_h // stride, input_w // stride
        cur_output_y_true = np.zeros((grid_h + 1, grid_w + 1, 1 + 2 + 2 + num_classes), dtype=np.float32)
        # ymin, xmin, ymax, xmax
        cur_gt = cur_gt / stride

        cur_gt_yx = np.stack([(cur_gt[:, 0] + cur_gt[:, 2]) / 2, (cur_gt[:, 1] + cur_gt[:, 3]) / 2], axis=1)

        all_kinds = []
        gts_yx_near_top_left = np.floor(cur_gt_yx).astype(int)
        all_kinds.append(gts_yx_near_top_left)
        if stride == 8:
            gts_yx_near_top_right = np.concatenate([np.floor(cur_gt_yx[:, 0:1]), np.ceil(cur_gt_yx[:, 1:2])],
                                               axis=1).astype(int)
            gts_yx_near_bottom_left = np.stack([np.ceil(cur_gt_yx[:, 0]), np.floor(cur_gt_yx[:, 1])], axis=1).astype(int)
            gts_yx_near_bottom_right = np.ceil(cur_gt_yx).astype(int)
            all_kinds.append(gts_yx_near_top_right)
            all_kinds.append(gts_yx_near_bottom_left)
            all_kinds.append(gts_yx_near_bottom_right)

        # grid_y = np.arange(0, grid_h + 1)
        # grid_x = np.arange(0, grid_w + 1)
        # shift_x, shift_y = np.meshgrid(grid_y, grid_x)
        # # (grid_x * grid_y, 2)
        # coordinates = np.vstack((shift_x.ravel(), shift_y.ravel())).transpose()
        #
        # grid_gaussin = np.exp(np.log(0.5) * 0.5 * np.sum(np.square(coordinates.reshape((-1, 1, 2)) - cur_gt_yx.reshape((1, -1, 2))), axis=2))
        # if grid_gaussin.shape[-1] != 0:
        #     cur_output_y_true[coordinates[..., 0], coordinates[..., 1], 0] = np.clip(
        #         np.max(grid_gaussin, axis=1),
        #         a_min=0., a_max=1.)

        for idx, one_kind in enumerate(all_kinds):
            # print(one_kind, cur_output_y_true.shape)
            # if len(one_kind) > 0:
            #     print(cur_output_y_true.shape, stride)
            #     print(cur_gt_yx, one_kind)

            cur_output_y_true[one_kind[..., 0], one_kind[..., 1], 0] = 1
            cur_output_y_true[one_kind[..., 0], one_kind[..., 1], 1:3] = cur_gt_yx - one_kind
            # left, top, right, bottom
            cur_output_y_true[one_kind[..., 0], one_kind[..., 1], 3:5] = np.stack(
                [(cur_gt_yx[:, 1] - cur_gt[:, 1]) / grid_w,
                 (cur_gt_yx[:, 0] - cur_gt[:, 0]) / grid_h], axis=1)
            cur_output_y_true[one_kind[..., 0], one_kind[..., 1], 5 + cur_label] = 1
        y_true.append(cur_output_y_true[:-1, :-1])

    ret = [new_image, *y_true]
    return ret

def tf_input_preprocess(image, bboxes, labels, height, width, input_shape, num_classes, is_training,
                        strides, desire_area):
    # do data augmentation on image with distort and flip
    if is_training:
        image = tf_distort_image(image)
        whether_flip = tf_random_number() < 0.5
        image = tf.cond(whether_flip, lambda: tf.image.flip_left_right(image), lambda: image)
    else:
        whether_flip = False

    ret_types = [tf.float32] + [tf.float32] * len(strides)

    py_ret = tf.numpy_function(pyfunc_prepare_true, [image, bboxes, labels, height, width,
                                                     input_shape, num_classes, whether_flip,
                                                     strides, desire_area], ret_types)

    return py_ret[0], tuple(py_ret[1:])

def _parse_record(serialized_example, input_shape=None, num_classes=None, is_training=True,
                  strides=None, desire_area=None):
    # extract raw image and annotation from serialized string
    # image tensor: [height, width, channel]
    # bboxes tensor: [N, 4] xmin, ymin, w, h
    # labels tensor: [N, ]
    image, bboxes, labels, height, width, bbox_nums, image_id = parse_example_protobuf(serialized_example,
                                                                                decode_image=True)
    labels = tf.cast(labels, tf.int32)

    # img, y1, y2, y3 = tf_input_preprocess(image, bboxes, labels, height, width, bbox_nums, input_shape, num_classes=num_classes)
    image, y_true = tf_input_preprocess(image, bboxes, labels, height, width, input_shape, num_classes, is_training,
                                        strides, desire_area)
    image.set_shape((None, None, None))
    for y in y_true: y.set_shape((None, None, None))
    # print(bbox_nums)
    bbox_nums = tf.squeeze(bbox_nums)
    return image, y_true

def tf_eval(outputs, confidence=0.3, strides=None, input_shape=None, from_logits=False):
    input_h, input_w = input_shape

    boxes = []
    scores = []
    labels = []
    boxes_confidence = []
    logging.info("%d output feature map", len(outputs))
    assert len(outputs) == len(strides), 'output nums must match the stride nums'

    for output, stride in zip(outputs, strides):
        if from_logits:
            box_confidence = tf.sigmoid(output[..., 0:1])
            delta_yx = tf.tanh(output[..., 1:3])
            side_distance = tf.sigmoid(output[..., 3:5])
            pred_label = tf.sigmoid(output[..., 5:])
        else:
            box_confidence = output[..., 0:1]
            delta_yx = output[..., 1:3]
            side_distance = output[..., 3:5]
            pred_label = output[..., 5:]

        valid_indice = tf.where(box_confidence >= confidence)

        valid_delta_yx = tf.gather_nd(delta_yx, valid_indice[..., :-1])
        valid_side_distance = tf.gather_nd(side_distance, valid_indice[..., :-1])
        valid_predict_label = tf.gather_nd(pred_label, valid_indice[..., :-1])
        valid_predict_label_sparse = tf.argmax(valid_predict_label, axis=-1)
        valid_predict_label_score = tf.reduce_max(valid_predict_label, axis=-1)

        valid_center_yx = tf.cast(valid_indice[..., 1:3], tf.float32) + valid_delta_yx
        valid_center_side = valid_side_distance * (input_h / stride, input_w / stride)
        # box on predict feature map
        # ymin, xmin, ymax, xmax
        pred_boxes = tf.concat(
            [valid_center_yx - valid_center_side[:, ::-1], valid_center_yx + valid_center_side[:, ::-1]], axis=1)
        pred_boxes *= stride

        # pred_boxes = tf.reshape(pred_boxes, (-1, 4))
        box_scores = tf.reshape(tf.gather_nd(box_confidence, valid_indice[..., :-1]), (-1,))

        boxes.append(tf.stack(
            [tf.clip_by_value(pred_boxes[..., 1], 0, input_w),
             tf.clip_by_value(pred_boxes[..., 0], 0, input_h),
             tf.clip_by_value(pred_boxes[..., 3], 0, input_w),
             tf.clip_by_value(pred_boxes[..., 2], 0, input_h)],
            axis=1))
        boxes_confidence.append(box_scores)
        labels.append(tf.reshape(valid_predict_label_sparse, (-1,)))
        scores.append(tf.reshape(valid_predict_label_score, (-1,)))

    boxes = tf.concat(boxes, axis=0)
    scores = tf.concat(scores, axis=0)

    labels = tf.concat(labels, axis=0)
    labels = tf.reshape(labels, (-1,))
    boxes_confidence = tf.concat(boxes_confidence, axis=0)
    return boxes, boxes_confidence, labels, scores

def _bbox_proprocess_soft(boxes, box_confidence, labels, label_score, image=None):
    all_labels = set(labels)

    _idx = []
    areas = (boxes[:, 2] - boxes[:, 0] + 1) * (boxes[:, 3] - boxes[:, 1] + 1)

    for cur_label in all_labels:
        cur_class_box_idx = np.where((labels == cur_label))[0]
        # draw_box(image.copy(), boxes[cur_class_box_idx], debug=True, title='before')
        keep_index = []
        while cur_class_box_idx.size > 0:
            cur_class_box_idx_sorted = np.array(
                sorted(cur_class_box_idx, key=lambda x: box_confidence[x], reverse=True))
            # print(box_confidence[cur_class_box_idx_sorted])

            if len(cur_class_box_idx_sorted) == 1:
                keep_index.append(cur_class_box_idx_sorted[0])
                break
            else:
                cur_idx = cur_class_box_idx_sorted[0]
                keep_index.append(cur_idx)

                cur = boxes[cur_idx]
                others = boxes[cur_class_box_idx_sorted[1:]]
                x1 = np.maximum(cur[0], others[:, 0])
                y1 = np.maximum(cur[1], others[:, 1])
                x2 = np.minimum(cur[2], others[:, 2])
                y2 = np.minimum(cur[3], others[:, 3])
                w = np.maximum(0.0, x2 - x1)
                h = np.maximum(0.0, y2 - y1)
                inter = w * h
                # 交/并得到iou值
                ovr = inter / (areas[cur_idx] + areas[cur_class_box_idx_sorted[1:]] - inter)
                out_thresh_idx = np.where(ovr <= 0.5)[0]

                # ti = image.copy()
                # print(ovr)
                # print(out_thresh_idx)
                # draw_box(ti, boxes[cur_idx], color=(0, 255, 0), title='dfs')
                # draw_box(ti, boxes[cur_class_box_idx_sorted[1:]], color=(255, 0, 255), debug=True, title='campare')

                cur_class_box_idx = cur_class_box_idx_sorted[out_thresh_idx + 1]

        _idx.extend(keep_index)
        # draw_box(image.copy(), boxes[keep_index], debug=True, title='after')

    # draw_box(image.copy(), boxes[_idx], title="final", debug=True)
    return boxes[_idx], box_confidence[_idx], labels[_idx], label_score[_idx]

class RcNet(object):
    # params
    num_classes = 80
    epochs = 50

    log_dir = 'log'
    ckpt_dir = 'checkpoint'

    wheather_freeze_backbone = False

    def __init__(self, training=True, data_dir=None, ckpt_path=None,
                 center_confidence=None, batch_size=None, gpu_nums=None,
                 detection_body: DetectionBody = None,
                 tu: TargetMissionUtil = None):

        logging.info("init rcnet under the configuration of %s task and %s detection backbone with %d num_classes" % (
            tu.name, detection_body.name, tu.num_classes
        ))

        self.lr = 1e-3
        self.start_epoch = 0

        self.training = training
        self.batch_size = batch_size
        self.gpu_nums = gpu_nums

        self.detection_body = detection_body
        self.task_util = tu
        self.backbone_name = detection_body.name
        self.input_shape = detection_body.input_shape
        self.output_channel = detection_body.output_channel
        self.strides = detection_body.strides
        self.desire_area = detection_body.desire_area
        self.pretrain_weights = detection_body.pretrain_path
        self.num_freez_backbone_layer = detection_body.num_freez_backbone_layer

        self.parse_args = dict(input_shape=self.input_shape, num_classes=self.num_classes, strides=self.strides,
                               desire_area=self.desire_area)

        if training:  # train
            assert data_dir is not None, "specify data dir for training"
            self.train_file_dir = os.path.join(data_dir, 'train*')
            self.val_file_dir = os.path.join(data_dir, 'val*')

            self.log_dir = get_dir(self.log_dir, "%s_%s" % (self.backbone_name, self.task_util.name))
            self.ckpt_dir = get_dir(self.ckpt_dir, "%s_%s" % (self.backbone_name, self.task_util.name))

            # output_map = {
            #     'output5': _loss_wrapper(3.08 * self.batch_size),
            #     'output1': _loss_wrapper(2.56 * self.batch_size),
            #     'output2': _loss_wrapper(1.31 * self.batch_size),
            #     'output3': _loss_wrapper(1.20 * self.batch_size),
            #     'output4': _loss_wrapper(1.08 * self.batch_size),
            # }

            output_map = {
                # mean true is dummy
                'output%d' % (x + 1): _loss_wrapper(1. * self.batch_size)
                for x in range(len(self.strides))
            }

            if self.wheather_freeze_backbone:
                logging.info("freeze backbone params")

            """ important """
            if self.gpu_nums > 1:
                logging.info("multi gpu training")

                # devices=["/device:GPU:0", "/device:GPU:1"]
                mirrored_strategy = tf.distribute.MirroredStrategy()
                with mirrored_strategy.scope():
                    self._model = self.detection_body.get_detection_body(output_layers_nums=5)
                    if self.wheather_freeze_backbone:
                        for i in range(self.num_freez_backbone_layer): self._model.layers[i].trainable = False
                    self._model.compile(optimizer=Adam(lr=self.lr), loss=output_map)
                    self._model_restore()
            else:
                logging.info("single gpu")
                self._model = self.detection_body.get_detection_body(output_layers_nums=5)
                if self.wheather_freeze_backbone:
                    for i in range(self.num_freez_backbone_layer): self._model.layers[i].trainable = False

                self._model_restore()
                self._model.compile(optimizer=Adam(lr=self.lr), loss=output_map)

        else:  # test
            self._model = detection_body.get_detection_body(output_layers_nums=5)
            if ckpt_path is None or not os.path.exists(ckpt_path):
                raise ValueError("ckeck ckpt file")

            if center_confidence is not None:
                self.center_confidence = center_confidence
            boxes, box_confidence, object_labels, object_scores = tf_eval(self._model.output, strides=self.strides,
                                                                          input_shape=self.input_shape,
                                                                          from_logits=True,
                                                                          confidence=self.center_confidence)
            logging.info("load ckpt from %s " % ckpt_path)
            self._model.load_weights(ckpt_path)

            self.input_image = self._model.input
            self._box = boxes
            self._box_confidence = box_confidence
            self._scores = object_scores
            self._labels = object_labels
        logging.info("detection backbone with %d output feature map" % len(self.strides))

    def _model_restore(self):
        if len(os.listdir(self.ckpt_dir)) >= 1:
            restore_epoch, last_ckpt = self._restore_epoch()
            self._model.load_weights(last_ckpt)
            logging.info('restore from exist model checkpoint')
        else:
            # using the parameter pretrained on ImageNet
            logging.info('find no exist model checkpoint, start from pretrained params')
            self._model.load_weights(self.pretrain_weights, by_name=True)
            logging.info('load pretrain weights')
            restore_epoch = 0

        self.start_epoch = restore_epoch

    def _restore_epoch(self):
        ckpts = os.listdir(self.ckpt_dir)

        last_ckpt = sorted(ckpts, key=lambda x: int(os.path.splitext(x)[0].split('_')[-1]))[-1]

        name = os.path.splitext(os.path.basename(last_ckpt))[0]
        logging.info(name)
        last_epoch = int(name.split('_')[-1])
        return last_epoch, os.path.join(self.ckpt_dir, last_ckpt)

    def input_test(self):
        train_ds, train_example_nums = get_ds(self.train_file_dir, batch_size=1,
                                              parse_fn=_parse_record, parse_record_args=self.parse_args,
                                              trainval_split=False)

        X, Y = train_ds.make_one_shot_iterator().get_next()

        print(X)
        print(Y)
        config = tf.ConfigProto(
            device_count={'GPU': 1}
        )

        box, box_score, label, label_score = tf_eval(Y, input_shape=self.input_shape, strides=self.strides,
                                                     confidence=0.6)

        with tf.Session(config=config) as sess:
            for _ in range(40):
                x, b, bc, l, ls, y = sess.run([X, box, box_score, label, label_score, Y[0]])
                # _box, _box_confidence, _label, _score = _bbox_proprocess_soft(b, bc, l, ls)
                print(len(b))
                print(b)
                # print(np.where(y == 1))
                show_tags = list(zip(bc, l))
                draw_box(x[0], b, debug=True, title='input_test', text=show_tags)

    def train(self):
        tb = TensorBoard(log_dir=self.log_dir, write_graph=True,
                         update_freq='epoch', histogram_freq=20)

        def scheduler(epoch):
            if epoch < 10:
                return self.lr
            elif 10 <= epoch < 15:
                return self.lr * np.exp(-0.3)
            elif 15 <= epoch < 20:
                return self.lr * np.exp(-0.4)
            elif 20 <= epoch < 35:
                return self.lr * np.exp(-0.45)
            else:
                return self.lr * np.exp(-0.4)
                # return self.lr * np.exp(0.4 * (10 - epoch))

        lr_schedule = tf.keras.callbacks.LearningRateScheduler(scheduler, verbose=1)

        ckpt = tf.keras.callbacks.ModelCheckpoint(
            filepath=os.path.join(self.ckpt_dir, 'rcnet-%s-%d*%d-l{loss:.1f}-vl{val_loss:.1f}_{epoch:03d}.h5' %
                                  (self.backbone_name, self.input_shape[0], self.input_shape[1])),
            monitor='val_loss',
            verbose=1,
            load_weights_on_restart=False,
            save_best_only=False,
            save_weights_only=True)
        csv_logger = CSVLogger('training.log')

        # get tf.data.Dataset
        train_ds, train_example_nums = get_ds(self.train_file_dir, batch_size=self.batch_size,
                                              epochs=self.epochs,
                                              parse_fn=_parse_record, parse_record_args=self.parse_args,
                                              trainval_split=False)
        val_ds, val_example_nums = get_ds(self.val_file_dir, batch_size=self.batch_size, trainval_split=False,
                                          parse_fn=_parse_record, epochs=self.epochs,
                                          parse_record_args=dict(is_training=False, **self.parse_args))

        logging.info("start from epoch %d, using gpu nums %d, batch size %d, initial lr %f, input_shape %s"
                     % (self.start_epoch + 1, self.gpu_nums, self.batch_size, self.lr, self.input_shape))

        self._model.fit(train_ds, validation_data=val_ds, initial_epoch=self.start_epoch,
                        callbacks=[tb, lr_schedule, ckpt, csv_logger], epochs=self.epochs,
                        steps_per_epoch=train_example_nums // self.batch_size, verbose=1,
                        validation_steps=val_example_nums // self.batch_size)
        self._model.save_weights(os.path.join(self.ckpt_dir, 'rcnet-%s-%d*%d_final.h5' %
                                              (self.backbone_name, self.input_shape[0], self.input_shape[1])))

    def predict(self, image, specific=True, sess: tf.compat.v1.Session = None):
        if sess is None:
            sess = get_session()
        if isinstance(image, str):
            origin_image = read_img(image)
        else:
            origin_image = image

        origin_h, origin_w = origin_image.shape[:-1]
        # resize image and return the image with model input size and offset and scale infomation
        input_image, offset_xy, scale_xy = _image_preprocess(origin_image, input_shape=self.input_shape)
        input_image = np.expand_dims(input_image, 0)

        # box [[xmin, ymin, xmax, ymax]...]
        # box scores [s1, s2, s3...] 0 <= s_i <= 1
        # labels etc [c1, c2, c3 ...] 0 <= c_i <= C
        # score etc  [t1, t2, t3 ...] 0 <= t_i <= 1
        b, bc, l, ls = sess.run(
            [self._box, self._box_confidence, self._labels, self._scores], feed_dict={
                self.input_image: input_image
            })

        b[:, [0, 2]] = b[:, [0, 2]] - offset_xy[0]
        b[:, [1, 3]] = b[:, [1, 3]] - offset_xy[1]

        b[:, [0, 2]] = b[:, [0, 2]] / scale_xy[0]
        b[:, [1, 3]] = b[:, [1, 3]] / scale_xy[1]

        b[:, [0, 2]] = np.clip(b[:, [0, 2]], 0, origin_w - 1)
        b[:, [1, 3]] = np.clip(b[:, [1, 3]], 0, origin_h - 1)
        # nms
        _box, _box_confidence, _label, _score = _bbox_proprocess_soft(b, bc, l, ls, image=origin_image)

        if specific:
            return _box, _box_confidence, _label, _score
        else:
            return _box, _label, _score

    def eval(self):
        sess = get_session()
        result_json_path = 'result/coco_result.json'
        result_dir = get_clean_dir('result', self.task_util.name)

        img_path = self.task_util.get_eval_imgs()
        predictions = []
        for i in tqdm(img_path[:]):
            predict_box, predict_label, predict_score = self.predict(i, sess=sess, specific=False)
            image_id = os.path.splitext(os.path.basename(i))[0].lstrip('0')
            predict_human_label = [self.task_util.label_classes[x] for x in predict_label]

            with open(os.path.join(result_dir, "%s.txt") % image_id, 'a') as f:
                if len(predict_box) == 0:
                    f.write("\n")

                for n, b, l, s in zip(predict_human_label, predict_box, predict_label, predict_score):
                    if 'coco' in self.task_util.name:
                        predictions.append({
                            'image_id': int(image_id),
                            'category_id': int(CocoUtil.label_to_category_id(l)),
                            'bbox': list(map(lambda x: float(x), [b[0], b[1], b[2] - b[0], b[3] - b[1]])),
                            'score': float(s)
                        })

                        f.write("%s %f %s %s %s %s\n" % (n, s, b[0], b[1], b[2], b[3]))

        if 'coco' in self.task_util.name:
            with open(result_json_path, 'w') as f:
                json.dump(predictions, f)
            coco_api_eval(self.task_util.coco_anno_dir, result_json_path)

    def eval_on_test_dev(self, imgs):
        sess = get_session()
        result_json_path = 'result/detections_test-dev2017_rcnet.json'

        predictions = []
        for i in tqdm(imgs):
            predict_box, predict_label, predict_score = self.predict(i, sess=sess, specific=False)
            image_id = os.path.splitext(os.path.basename(i))[0].lstrip('0')
            predict_human_label = [self.task_util.label_classes[x] for x in predict_label]

            for n, b, l, s in zip(predict_human_label, predict_box, predict_label, predict_score):
                if 'coco' in self.task_util.name:
                    predictions.append({
                        'image_id': int(image_id),
                        'category_id': int(CocoUtil.label_to_category_id(l)),
                        'bbox': list(map(lambda x: float(x), [b[0], b[1], b[2] - b[0], b[3] - b[1]])),
                        'score': float(s)
                    })

        with open(result_json_path, 'w') as f:
            json.dump(predictions, f)

    def interactive_inspect(self, show_anno=False, imgs=None):
        if imgs is None:
            img_path = self.task_util.get_eval_imgs()
        else:
            img_path = imgs

        for p in img_path:
            print(p)
            image = read_img(p)
            image_id = os.path.splitext(os.path.basename(p))[0].lstrip('0')

            anno_box, anno_human_label = self.task_util.get_image_anno(image_id)

            predict_box, predict_box_confidence, predict_label, predict_score = self.predict(image)
            predict_human_label = [self.task_util.label_classes[x] for x in predict_label]

            show_tags = list(zip(predict_human_label, ["%.2f %.2f" % (box_conf, label_score)
                                                       for box_conf, label_score in
                                                       zip(predict_box_confidence, predict_score)]))

            print('predict labels: %s' % str(show_tags))
            print('annotate label: %s' % anno_human_label)
            if show_anno and len(anno_box) != 0:
                draw_box(image.copy(), np.array(anno_box), debug=True, title='anno', text=anno_human_label)
            if len(predict_box) != 0:
                draw_box(image.copy(), predict_box, debug=True, title='predict', text=show_tags)


from django.contrib import admin

from .models import JobPost, Author, Skills, Location

class JobAdmin(admin.ModelAdmin):
    list_display=('title', 'expiry', 'salary', 'author')
    list_filter=('expiry','salary')
    search_fields=('title', 'description')
    search_help_text = "search in title or/and description fields"

    # ------- now detail view
    # fields = (("title", "description"), "salary") //or fieldset bellow, not both
    # exclude = ("expiry")

    fieldsets = (
            ('Basic Information', {
                'fields':('title', 'description')}
             ),
            ('More Information', {
                'classes':('collapse','wide'), # adding builting css stylings
                'fields':(('expiry', 'salary', 'author'), 'slug')}
             ),
    )
# Register your models here.
#admin.site.register(JobPost, JobAdmin)
admin.site.register(JobPost)
admin.site.register(Author)
#admin.site.register(Person)
admin.site.register(Skills)
admin.site.register(Location)


import sys
input = sys.stdin.readline

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

mine = []
block = n * m
for i in range(n):
    mine += list(map(int, input().split()))

max = (sum(mine) + b) // block

answer = 0
time, height = 9223372036854775807, 0
for h in range(0, max + 1) :
    for m in mine :
        if m < h :
            answer += h - m
        else :
            answer += (m - h) * 2
    if answer <= time :
        time = answer
        height = h
    answer = 0

print(time, height)

따로 푼 것/백준/백준 18111번(마인크래프트).py


import time
import RPi.GPIO as io
import socket
from multiprocessing import Process, Value, Array

def LED_Off(angle):
    print("resetting angle: ", angle)
    for p in range(4):
        io.output(4*angle+p,0)

def LED_On(angle):
    print("setting angle: ", angle)
    for p in range(4):
        io.output(4*angle+p,1)

def LED_Duty():
    has_read = False
    while True:
        print("LED_Duty time")
        if stren.value is -1 or angle.value is -1:
            time.sleep(0)
            continue

        ang = angle.value
        stg = stren.value

        print(ang)
        print(stg)

        print("LED_Duty begun")
        on_time = 0.1
        off_time = 1 - (0.1*stg)
        print(on_time)
        print(off_time)
        print("Enter loop")

        has_changed.value = False

        #while has_changed.value is False:# and has_read is False:
        #    LED_On(ang)
        #    time.sleep(on_time)
        #    LED_Off(ang)
        #    time.sleep(off_time)
        LED_On(ang)
        time.sleep(on_time)
        LED_Off(ang)
        time.sleep(off_time)
        print("Leave loop")

def Read_MSG(port):
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    host = 'Meenakshis-MacBook-Pro.local'

    print("Connecting")
    while True:
        try:
            s.connect((host, port))
            print("Connected to ", host)
            break
        except KeyboardInterrupt:
            quit()
        except:
            print("Not Connected")

    while True:
        print("new while loop")
        r_msg = s.recv(15).decode()
        msg = r_msg.split()

        print(msg)

        if len(msg) is 4:
            if msg[0] == 'START' and msg[3] == 'EOM':
                #print(msg[0])
                #print("Angle: ", msg[1])
                #print("Strength: ", msg[2])
                #print(msg[3])

                #s_msg = "RECEIVED"
                #s.send(s_msg.encode())

                if angle is not int(msg[1]) or stren is not int(msg[2]):
                    angle.value = int(msg[1])
                    stren.value = int(msg[2])

                    has_changed.value = True

                    time.sleep(0.1)

def init_port(port):
    io.setmode(io.BCM)
    io.setup(port,io.OUT)
    io.output(port,0)

if __name__ == "__main__":

    n_ports = 20

    print('Haptic Sensors Initialization')

    for p in range(n_ports):
        print("init port: ", p)
        init_port(p)

    #s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    #host = 'Meenakshis-MacBook-Pro.local'
    #port = int(input("Enter Port Number"))
    #
    #print("Connecting")
    #while True:
    #    try:
    #        s.connect((host, port))
    #        print("Connected to ", host)
    #        break
    #    except KeyboardInterrupt:
    #        quit()
    #    except:
    #        print("Not Connected")
    #print("Past while loop")

    port = int(input("Enter Port Number: "))

    angle = Value('i', -1)
    stren = Value('i', -1)
    has_changed = Value('b', False)

    Process(target=Read_MSG, args=(port,)).start()
    Process(target=LED_Duty).start()

#while True:
#    r_msg = s.recv(15).decode()
#    msg = r_msg.split()
#
#    print(msg)
#
#    #t0 = time.time()
#    #while (time.time() - t0 < 1):
#    #   a = 5
#    #msg = msg_cycle()
#
#    if len(msg) is 4:
#        if msg[0] == 'START' and msg[3] == 'EOM':
#            print(count,"\n")
#            print(msg[0])
#            print("Angle: ", msg[1])
#            print("Strength: ", msg[2])
#            print(msg[3])
#
#            #s_msg = "RECEIVED"
#            #s.send(s_msg.encode())
#
#            if angle is not int(msg[1]) or stren is not int(msg[2]):
#                angle = int(msg[1])
#                stren = int(msg[2])
#
#                has_changed = True
#
#                time.sleep(0.1)
#
#    LED_Duty(angle,stren)


#!/usr/bin/env python

from matplotlib import rc
from daft import PGM, Node, Plate
rc("font", family="serif", size=12)
rc("text", usetex=True)

pgm = PGM([8.5, 6.5], origin=[0., 0.2], observed_style='inner')

#pgm.add_node(Node('dispersion',r"\center{$\sigma_{Ia}$ \newline $\sigma_{!Ia}$}", 1,6,scale=1.2,aspect=1.8))
pgm.add_node(Node('rate',r"$r$",2,6))
#pgm.add_node(Node('theta_T',r"\center{$\alpha_{Ia}$, $\alpha_{!Ia}$ \newline $\beta_{Ia}$, $\beta_{!Ia}$}", 4,6,scale=1.4,aspect=1.8))
pgm.add_node(Node('theta_T',r"\center{$\Theta_{Ia}$ \newline $\Theta_{!Ia}$}", 4,6,scale=1.5,aspect=1.2))
pgm.add_node(Node('mu',r"$\Omega_M$, $w$", 5,6, scale=1.4))
pgm.add_node(Node('Transmission',r"$Z$", 7, 6))

pgm.add_node(Node('G_i',r"$g_i$", 3,5))
#pgm.add_node(Node('G_Ni',r"$g_{Ni}$", 8,5))

pgm.add_node(Node('theta_Ti',r"\center{$\theta_{Ia,i}$ \newline $\theta_{!Ia,i}$}", 1,4,scale=1.5,aspect=1.2))
pgm.add_node(Node('Type',r"$T_i$", 2, 4))
pgm.add_node(Node('Gals',r"$G$", 8,4, observed=True))

pgm.add_node(Node('Luminosity',r"$L_i(t,\lambda)$", 4, 3,  scale=1.4))
pgm.add_node(Node('HD',r"$\mu_i$", 5,3,fixed=True,offset=(10,-5)))
#pgm.add_node(Node('Flux_g',r"$f_{gi}(\lambda)$", 8, 3))

#pgm.add_node(Node('z',r"$z_i$", 3, 3, fixed=True,offset=(-10,-5)))

pgm.add_node(Node('Flux',r"$f_i(t,\lambda)$", 6, 2, scale=1.2,fixed=True,offset=(20,5)))
#pgm.add_node(Node('Counts',r"$\overline{n}_i$", 7, 2,scale=1.2,fixed=True,offset=(-15,0)))
#pgm.add_node(Node('Counts_g',r"$\overline{\mathit{ADU}}_{gi}$", 8, 2,scale=1.2,fixed=True,offset=(20,0)))
#pgm.add_node(Node("Summary",r"$S({\mathit{ADU}_i})$", 9, 1, fixed=True, offset=(0,-25)))

pgm.add_node(Node('Stheta',r"${\theta}_{Si}$", 1, 1,  observed=True))
pgm.add_node(Node('ST',r"${T}_{Si}$", 2, 1, observed=True))
pgm.add_node(Node('Sz',r"${z}_{Si}$", 3, 1, observed=True))
pgm.add_node(Node('^Counts',r"${n_i}$", 7, 1, observed=True))

#pgm.add_edge("^Counts","Summary")
pgm.add_edge("G_i","Gals")
#pgm.add_edge("G_Ni","Gals")
pgm.add_edge("rate","Type")
#pgm.add_edge("Type", "theta_Ti")
pgm.add_edge("mu","HD")
pgm.add_edge("theta_T", "theta_Ti")
pgm.add_edge("G_i","HD")
pgm.add_edge("G_i","Sz")
pgm.add_edge("G_i","Luminosity")
pgm.add_edge("HD","Flux")
#pgm.add_edge("z","Flux")
#pgm.add_edge("G_Ni","Flux_g")
#pgm.add_edge("HD","Flux_g")
#pgm.add_edge("z","Flux_g")
pgm.add_edge("G_i","Type")
pgm.add_edge("G_i","theta_Ti")
pgm.add_edge("Type","Luminosity")
pgm.add_edge("Luminosity","Flux")
pgm.add_edge("Type","Stheta")
pgm.add_edge("theta_T","Luminosity")
pgm.add_edge("theta_Ti","Luminosity")
pgm.add_edge("Flux","^Counts")
#pgm.add_edge("Flux_g","Counts_g")
#pgm.add_edge("Counts","^Counts")
#pgm.add_edge("Counts_g","^Counts")
pgm.add_edge("Transmission","^Counts")
#pgm.add_edge("Transmission","Counts_g")
pgm.add_edge("Type","ST")
pgm.add_edge("theta_Ti","Stheta")
#pgm.add_edge("Counts_g","^Counts")

# Big Plate: Galaxy
pgm.add_plate(Plate([0.5, 0.5, 7.2, 5.],
                    label=r"SNe $i = 1, \cdots, N_{SN}$",
                    shift=-0.2,label_offset=[10,2]))

# Render and save.
pgm.render()
# pgm.figure.text(0.2,0.98,r'\underline{UNIVERSE}',size='large')
# pgm.figure.text(0.45,0.98,r'\underline{OBSERVATORY}',size='large')
# pgm.figure.text(0.72,0.98,r'\underline{DATA}',size='large')

pgm.figure.savefig("../results/hdpgm.pdf")


import pyglet
import matplotlib.pyplot as plt
import math as mth
from player import *
from wall import *
from checkpiont import *
from bullet import *
import copy
import random as r

# globals

WIDTH = 1300
HEIGHT = 820

gen = 0

gen_table = []
avr_fitness_table = []
avr_score_table = []
best_score_table = []
best_fitness_table = []
max_laps = 0

amount_of_rays = 4
players = []
am_of_players = 50
cycles = 1
graph_cycles = 20

checkpoints = []
walls = []
sim_speed = .02

bullets = []

track_name = "empty"
# track_name = "what_is_this"
# track_name = "round"
track_file_name = "tracks/" + track_name + ".track"

window = pyglet.window.Window(width=WIDTH, height=HEIGHT)
pyglet.vsync = True
label = pyglet.text.Label("gen: 0, players: 100, max laps: 0", font_name="Comic Sans", font_size=25,
x=WIDTH/2, y=HEIGHT-25,
anchor_x="center", anchor_y="center"
)

# greate first players
for i in range(am_of_players):
    rot = 360/am_of_players * i
    players.append(Player([WIDTH/2 + mth.cos(mth.radians(rot)) * 200, HEIGHT/2 + mth.sin(mth.radians(rot)) * 200], rot, amount_of_rays, False))

def draw_line(xy1, xy2, color):
    pyglet.graphics.draw(2, pyglet.gl.GL_LINES,
        ('v2i', (int(xy1[0]), int(xy1[1]), int(xy2[0]), int(xy2[1]))),
        ('c4B', (color) * 2)
    )

def reset_players(players):
    for p in players:
        p.pos = [WIDTH/2, HEIGHT/2-100]
        p.rot = 0
        p.dead = False

def all_dead(players):
    for p in players:
        if not p.dead:
            return False
    return True

def calc_avr_score(players):
    tot_score = 0
    for p in players:
        tot_score += p.score
    avr = tot_score/len(players)
    return avr

def calc_avr_fitness(players):
    tot_fit = 0
    for p in players:
        tot_fit += p.fitness
    avr = tot_fit/len(players)
    return avr

def find_best_score(players):
    best_score = 0
    for p in players:
        if p.score > best_score:
            best_score = p.score
    return best_score

def find_best_fitt(players):
    best_fit = 0
    best_fit_player = None
    for p in players:
        if p.fitness > best_fit:
            best_fit = p.fitness
            best_fit_player = p
    return best_fit, best_fit_player

def find_best_players(players):
    best_f = 0
    bPs = []
    for p in players:
        if p.fitness > best_f:
            bPs = []
            best_f = p.fitness
            bPs.append(p)
        elif p.fitness == best_f:
            bPs.append(p)
    if len(bPs) > 3:
        bPs = bPs[:3]
    return bPs

def str2bool(v):
    return v.lower() in ("1", "true")

def load_track_file(file_name):
    with open(file_name, "r") as f:
        rawfiledata = f.read().split("ch\n")
    wall_data = rawfiledata[0].split("\n")
    wall_data.pop()
    for item in wall_data:
        dat = item.split(", ")
        walls.append(Wall([int(dat[0]), int(dat[1])], [int(dat[2]), int(dat[3])]))
    checkpt_data = rawfiledata[1].split("\n")
    checkpt_data.pop()
    for item in checkpt_data:
        dat = item.split(", ")
        checkpoints.append(Checkpoint([int(dat[0]), int(dat[1])], [int(dat[2]), int(dat[3])], int(dat[4]), str2bool(dat[5])))

def repopulate(players):
    calculate_fitness(players)

    avr_fit = calc_avr_fitness(players)
    avr_score = calc_avr_score(players)
    best_fit, best_player = find_best_fitt(players)
    best_score = find_best_score(players)

    best_score_table.append(best_score)
    best_fitness_table.append(best_fit)
    avr_fitness_table.append(avr_fit)
    avr_score_table.append(avr_score)

    new_players = []
#kaas is lekker
    # bPs = find_best_players(players)
    # for bP in bPs:
    #     p = Player([WIDTH/2, HEIGHT/2-100], 0, amount_of_rays, False)
    #     p.net = copy.deepcopy(bP.net)
    #     p.net.randomize_net(.00001)
    #     new_players.append(p)

    bP = Player([WIDTH/2, HEIGHT/2-100], 0, amount_of_rays, False)
    bP.net = copy.deepcopy(best_player.net)
    bP.color = (0, 150, 255, 255)
    global max_laps
    max_laps = bP.times_finished

    new_players.append(bP)

    for i in range(int(mth.ceil((len(players) - 1)*6/8))):
        p = pick_player(players)
        p.net.randomize_net(.1)
        p.dead = False
        new_players.append(p)
    # for i in range(int(((len(players) - 1)/8)):
    #     p1 = pick_player(players)
    #     p2 = pick_player(players)
    #     p3 = cross_players(p1, p2)
    #     p3.dead = False
    #     new_players.append(p3)
    for i in range(int((len(players) - 1)*2/8)):
        new_players.append(Player([WIDTH/2, HEIGHT/2-100], 0, amount_of_rays, False))

    players = []
    players = new_players
    return players

def get_am_of_life_players(players):
    sum = 0
    for p in players:
        if not p.dead:
            sum += 1
    return sum

def pick_player(players):
    index = 0
    rand = r.random()
    while rand > 0:
        rand -= players[index].fitness
        index += 1
    index -= 1
    child = Player([WIDTH/2, HEIGHT/2-100], 0, amount_of_rays, False)
    child.net = copy.deepcopy(players[index].net)
    return child

def cross_players(p1, p2):
    # make child with net of first parrent
    child = Player([WIDTH/2, HEIGHT/2-100], 0, amount_of_rays, False)
    child.net = copy.deepcopy(p1.net)
    for i in range(len(child.net.nodes)):
        if not i == 0:
            for n in range(len(child.net.nodes[i])):
                node = child.net.nodes[i][n]
                for w in range(len(node.weights)):
                    if r.randint(0,1):
                        weight = p2.net.nodes[i][n].weights[w]
    return child

def calculate_fitness(players):
    sum = 0
    for p in players:
        sum += p.score
    for p in players:
        # p.fitness = (p.check_piont + (len(checkpoints)+1) * p.times_finished) - 0
        p.fitness = p.score
        # (((mth.pow(p.check_piont, 3) * p.score) - p.rot/360) / sum) * am_of_players
        #(mth.pow(p.check_piont, 2) * p.score)
        #p.score / sum

load_track_file(track_file_name)

@window.event
def on_draw():
    # if gen % 10 == 0:
    window.clear()
    label.draw()
    for p in players:
        if not p.dead:
            p.sprite.draw()
            for line in p.boundries:
                    draw_line([ line[0], line[1] ], [ line[2], line[3] ], p.color)
                    pass
            for rp in p.ray_pts:
                # draw_line([p.pos[0], p.pos[1]], [rp[0], rp[1]], (255, 0, 255, 255))
                pass
    for w in walls:
        draw_line(w.start_pos, w.end_pos, w.color)
    for ch in checkpoints:
        draw_line(ch.start_pos, ch.end_pos, ch.color)
    for b in bullets:
        b.sprite.draw()
        for line in b.boundries:
            draw_line([ line[0], line[1] ], [ line[2], line[3] ], b.color)
            pass
    # else:
    #     window.clear()
    #     label.draw()

def update(delta_time):
    global gen
    global players
    for i in range(cycles):
        if all_dead(players):

            players = repopulate(players)
            gen += 1

            gen_table.append(gen-1)
            label.text = (str("gen: " + str(gen) + " players: " + str(len(players)) + " max laps: " + str(max_laps)))

            # plot of fitness and score
            # fig, axs = plt.subplots(2, 1)
            # fig.max_open_warning = 500
            # axs[0].plot(gen_table, avr_fitness_table, gen_table, best_fitness_table)
            # axs[0].set_xlim(0, len(gen_table)-1)
            # axs[0].set_xlabel('gen.')
            # axs[0].set_ylabel('avr. fitness, best fitness')
            # axs[0].grid(True)

            # axs[1].plot(gen_table, avr_score_table, gen_table, best_score_table)
            # axs[1].set_xlim(0, len(gen_table)-1)
            # axs[1].set_xlabel('gen.')
            # axs[1].set_ylabel('avr. score, best score')
            # axs[1].grid(True)

            # if gen % graph_cycles == 0:
            #     fig.tight_layout()
            #     fig.savefig("graph.png")

        lps = get_am_of_life_players(players)
        label.text = (str("gen: " + str(gen) + " players: " + str(lps) + " max laps: " + str(max_laps)))

        for p in players:
            if not p.dead:
                p.cast_rays(walls)
                p.set_net_input()
                p.get_dist_to_closest(players)
                p.move(sim_speed)
                bul = p.shoot(bullets)
                if not bul == None:
                    bullets.append(bul)

                p.check_for_hit(walls, checkpoints, bullets)
                p.out_off_bounds(WIDTH, HEIGHT)

        for b in bullets:
            if not b.dead:
                b.move(sim_speed)
                b.check_for_hit(walls)
                b.out_off_bounds(WIDTH, HEIGHT)
            else:
                bullets.remove(b)

pyglet.clock.schedule_interval(update, 1/60)

pyglet.app.run()


"""
    with box size
    UNet + part PointNet feature + sematnic one hot + instance one hot concatenated in bottleneck
    Input:
        a shape image:                  3 x 224 x 224
        a normalized part point-cloud:  Part Cnt x 1000 x 3
        sem one-hot:                    Part Cnt x K (num of semantics, e.g. Chair 57)
        instance one hot                Part Cnt x M (max number of similar parts, )
    Output:
        part mask:                      Part Cnt x 224 x 224
    Loss:
        neg Soft-IoU
"""

import torch
from torch import nn
from torch.autograd import Variable
import torch.nn.functional as F
from scipy.optimize import linear_sum_assignment

class UNet(nn.Module):

    def __init__(
        self,
        cond_feat_dim,
        in_channels=1,
        n_classes=2,
        depth=5,
        wf=5,
        padding=False,
        batch_norm=False,
        up_mode='upconv',
    ):
        """
        Implementation of
        U-Net: Convolutional Networks for Biomedical Image Segmentation
        (Ronneberger et al., 2015)
        https://arxiv.org/abs/1505.04597

        Using the default arguments will yield the exact version used
        in the original paper

        Args:
            in_channels (int): number of input channels
            n_classes (int): number of output channels
            depth (int): depth of the network
            wf (int): number of filters in the first layer is 2**wf
            padding (bool): if True, apply padding such that the input shape
                            is the same as the output.
                            This may introduce artifacts
            batch_norm (bool): Use BatchNorm after layers with an
                               activation function
            up_mode (str): one of 'upconv' or 'upsample'.
                           'upconv' will use transposed convolutions for
                           learned upsampling.
                           'upsample' will use bilinear upsampling.
        """
        super(UNet, self).__init__()
        assert up_mode in ('upconv', 'upsample')

        self.padding = padding
        self.depth = depth
        prev_channels = in_channels

        self.down_path = nn.ModuleList()
        for i in range(depth):
            self.down_path.append(
                UNetConvBlock(prev_channels, 2 ** (wf + i), padding, batch_norm)
            )
            prev_channels = 2 ** (wf + i)

        self.up_path = nn.ModuleList()
        for i in reversed(range(depth - 1)):
            if i == depth - 2:
                self.up_path.append(
                    UNetUpBlock(prev_channels, cond_feat_dim, 2 ** (wf + i), up_mode, padding, batch_norm)
                )
            else:
                self.up_path.append(
                    UNetUpBlock(prev_channels, 0, 2 ** (wf + i), up_mode, padding, batch_norm)
                )
            prev_channels = 2 ** (wf + i)

        self.last = nn.Conv2d(prev_channels, n_classes, kernel_size=1)

    def forward(self, x, feat):
        blocks = []
        for i, down in enumerate(self.down_path):
            x = down(x)
            if i != len(self.down_path) - 1:
                blocks.append(x)
                x = F.max_pool2d(x, 2)

        feat_size = x.shape[2]
        batch_size = feat.shape[0]
        feat = feat.reshape(batch_size, -1, 1, 1)
        feat = feat.repeat(1, 1, feat_size, feat_size)
        x = torch.cat([x, feat], dim=1)

        for i, up in enumerate(self.up_path):
            x = up(x, blocks[-i - 1])

        x = self.last(x)
        return x

class UNetConvBlock(nn.Module):

    def __init__(self, in_size, out_size, padding, batch_norm):
        super(UNetConvBlock, self).__init__()
        block = []

        block.append(nn.Conv2d(in_size, out_size, kernel_size=3, padding=int(padding)))
        block.append(nn.ReLU())
        if batch_norm:
            block.append(nn.BatchNorm2d(out_size))

        block.append(nn.Conv2d(out_size, out_size, kernel_size=3, padding=int(padding)))
        block.append(nn.ReLU())
        if batch_norm:
            block.append(nn.BatchNorm2d(out_size))

        self.block = nn.Sequential(*block)

    def forward(self, x):
        out = self.block(x)
        return out

class UNetUpBlock(nn.Module):

    def __init__(self, in_size, add_in_size, out_size, up_mode, padding, batch_norm):
        super(UNetUpBlock, self).__init__()

        if up_mode == 'upconv':
            self.up = nn.ConvTranspose2d(in_size + add_in_size, out_size, kernel_size=2, stride=2)
        elif up_mode == 'upsample':
            self.up = nn.Sequential(
                nn.Upsample(mode='bilinear', scale_factor=2),
                nn.Conv2d(in_size, out_size, kernel_size=1),
            )

        self.conv_block = UNetConvBlock(in_size, out_size, padding, batch_norm)

    def center_crop(self, layer, target_size):
        _, _, layer_height, layer_width = layer.size()
        diff_y = (layer_height - target_size[0]) // 2
        diff_x = (layer_width - target_size[1]) // 2
        return layer[
            :, :, diff_y : (diff_y + target_size[0]), diff_x : (diff_x + target_size[1])
        ]

    def forward(self, x, bridge):
        up = self.up(x)
        batch_size = up.shape[0]
        crop1 = self.center_crop(bridge, up.shape[2:])
        out = torch.cat([up, crop1], 1)
        out = self.conv_block(out)

        return out

class PointNet(nn.Module):
    def __init__(self, emb_dim):
        super(PointNet, self).__init__()

        self.conv1 = nn.Conv1d(3, 64, 1)
        self.conv2 = nn.Conv1d(64, 64, 1)
        self.conv3 = nn.Conv1d(64, 64, 1)
        self.conv4 = nn.Conv1d(64, 128, 1)
        self.conv5 = nn.Conv1d(128, emb_dim, 1)

        self.bn1 = nn.BatchNorm1d(64)
        self.bn2 = nn.BatchNorm1d(64)
        self.bn3 = nn.BatchNorm1d(64)
        self.bn4 = nn.BatchNorm1d(128)
        self.bn5 = nn.BatchNorm1d(emb_dim)

        self.mlp1 = nn.Linear(emb_dim, emb_dim)
        self.bn6 = nn.BatchNorm1d(emb_dim)

    def forward(self, x):
        x = x.permute(0, 2, 1)
        batch_size = x.shape[0]

        x = torch.relu(self.bn1(self.conv1(x)))
        x = torch.relu(self.bn2(self.conv2(x)))
        x = torch.relu(self.bn3(self.conv3(x)))
        x = torch.relu(self.bn4(self.conv4(x)))
        x = torch.relu(self.bn5(self.conv5(x)))

        x = x.max(dim=-1)[0]

        x = torch.relu(self.bn6(self.mlp1(x)))
        return x

class Network(nn.Module):

    def __init__(self, conf, partleaf_cnt ):
        super(Network, self).__init__()
        self.conf = conf
        self.img_size = conf.img_size
        self.sem_dim = partleaf_cnt
        self.ins_dim = conf.ins_dim
        self.pointnet = PointNet(conf.pointnet_emd_dim)
        self.box_dim = 3
        self.dim_len = conf.pointnet_emd_dim + conf.ins_dim
        self.unet = UNet(self.dim_len + 256, \
            in_channels=3, n_classes=1, padding=True, batch_norm=True)
        self.softmax = nn.Softmax(dim=0)

        self.mlp1 = nn.Linear(self.dim_len, 256)
        self.mlp2 = nn.Linear(256, 256)

    def forward(self, img, pc, ins_feat, part_cnt):
        '''
            input:
            img:            max_parts x 3 x H x W
            pc :            max_parts x N x 3
            sem_feat        max_parts x K
            ins_feat        max_parts x max_similar parts
            similar_cnt     max_parts x 2

            output:         part_cnt+1 x H x W

        '''
        pc_feat = self.pointnet(pc)

        if self.conf.normalize:
            img[:, 0] = (img[:, 0] - 0.485) / 0.229
            img[:, 1] = (img[:, 1] - 0.456) / 0.224
            img[:, 2] = (img[:, 2] - 0.406) / 0.225

        cond_feat = torch.cat([pc_feat, ins_feat], dim=1) #box_size

        global_part_feat = torch.relu(self.mlp1(cond_feat))

        global_feat = []
        t = 0

        for cnt in part_cnt:
            cur_global_feat = global_part_feat[t:t+cnt].max(dim=0, keepdim=True)[0]
            cur_global_feat = torch.relu(self.mlp2(cur_global_feat)).repeat(cnt, 1)
            global_feat.append(cur_global_feat)
            t += cnt

        global_feat = torch.cat(global_feat, dim=0)

        cond_feat = torch.cat( (cond_feat, global_feat), dim=1)

        masks = self.unet(img, cond_feat).squeeze(1)

        t = 0
        all_masks = ()
        for cnt in part_cnt:
            background = Variable(torch.zeros(1,  self.img_size, self.img_size, dtype = torch.float), requires_grad=True).to(self.conf.device)
            cur_mask = self.softmax( torch.cat( (masks[t:t+cnt], background), 0))
            all_masks = all_masks + (cur_mask,)
            t += cnt

        return all_masks

    def get_mask_loss(self, mask1, mask2):

        batch_size = mask1.shape[0]

        inter = (mask1 * mask2).mean(dim=[1, 2])
        union = mask1.mean(dim=[1, 2]) + mask2.mean(dim=[1, 2]) - inter
        loss_per_data = - inter / (union + 1e-12)
        return loss_per_data

    def linear_assignment(self, mask1, mask2, similar_cnt):
        '''
            mask1, mask 2:
                # part_cnt x 224 x 224
            similar cnt
                # shape: max_mask  x  2
                # first index is the index of parts without similar parts 1, 2, 3, 4, 4, 5, 5, 6, 6, 6, 6, ....
                # second index is the number of similar part count:       1, 1, 1, 2, 2, 2, 2, 4, 4, 4, 4, ....

        '''

        bids = []; ids1 = []; ids2 = []; inds1 = []; inds2 = [];
        img_size = mask1.shape[-1]
        t = 0
        with torch.no_grad():
            while t < similar_cnt.shape[0]:

                cnt = similar_cnt[t].item()
                bids = [t] * cnt
                cur_mask1 = mask1[t:t+cnt].unsqueeze(1).repeat(1, cnt, 1, 1).view(-1, img_size, img_size)
                cur_mask2 = mask2[t:t+cnt].unsqueeze(0).repeat(cnt, 1, 1, 1).view(-1, img_size, img_size)
                dist_mat = self.get_mask_loss(cur_mask1, cur_mask2)
                dist_mat = dist_mat.view(cnt, cnt)

                t += cnt
                rind, cind = linear_sum_assignment(dist_mat.cpu().numpy())

                ids1 = list(rind)
                ids2 = list(cind)
                inds1 += [bids[i] + ids1[i] for i in range(len(ids1))]
                inds2 += [bids[i] + ids2[i] for i in range(len(ids2))]
        return inds1, inds2


__author__ = 'Scott Businge'

# Remember: Functional tests are the ultimate indicator of whether your Project works or not,
# while Unit tests are the means to help you reach that end. This will all make sense soon.

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import unittest
# from django.core.urlresolvers import resolve
from .models import Quote

class QuotesTest(unittest.TestCase):
    def setUp(self):
        self.browser = webdriver.Firefox()
        self.browser.implicitly_wait(2)

    def tearDown(self):
        self.browser.quit()

    def test_to_check_functionality(self):
        # User opens browser

        self.browser.get('http://localhost:8000')

        body = self.browser.find_element_by_tag_name('body')
        self.assertIn('Django', body.text)

        # User opens admin page

        self.browser.get('http://localhost:8000/admin/')

        body = self.browser.find_element_by_tag_name('body')
        self.assertIn('Django administration', body.text)

        # User opens admin page

        self.browser.get('http://localhost:8000/admin/')

        body = self.browser.find_element_by_tag_name('body')
        self.assertIn('Django administration', body.text)

        # users types in username and passwords and presses enter

        username_field = self.browser.find_element_by_name('username')
        username_field.send_keys('quotes')
        password_field = self.browser.find_element_by_name('password')
        password_field.send_keys('quotes')
        password_field.send_keys(Keys.RETURN)

        # login credentials are correct, and the user is redirected to the main admin page

        body = self.browser.find_element_by_tag_name('body')
        self.assertIn('Site administration', body.text)

        # the user is redirected to the main admin page to ensure database models are present

        body = self.browser.find_element_by_tag_name('body')
        self.assertIn('Quotes', body.text)

        # to test string representation for models

        quote = Quote(title="Quote title")
        self.assertEqual(str(quote), quote.title)

        # to test for verbose plural name

        self.assertEqual(str(Quote._meta.verbose_name_plural), "quotes")

        # test to ensure user can add data

        # user clicks on the Quotes link

        quotes_links = self.browser.find_elements_by_link_text('Quotes')
        quotes_links[0].click()

        # user clicks on the Add Quote link

        add_quote_link = self.browser.find_element_by_link_text('Add')
        add_quote_link.click()

        # user fills out the form
        self.browser.find_element_by_name('title').send_keys(' "Stay hungry, Stay foolish" Steve Jobs')
        self.browser.find_element_by_name('submitter').send_keys("Quotes")
        self.browser.find_element_by_name('description').send_keys("What an amazing quote")

        # user clicks the save button

        self.browser.find_element_by_css_selector("input[value='Save']").click()

        # the Quote has been added

        body = self.browser.find_element_by_tag_name('body')
        self.assertIn('Stay hungry', body.text)

        # user returns to the main admin screen

        home_link = self.browser.find_element_by_link_text('Home')
        home_link.click()

        # user clicks on the Quotes link

        quotes_links = self.browser.find_elements_by_link_text('Quotes')
        quotes_links[0].click()

        # user clicks on the Add Quote link

        add_quote_link = self.browser.find_element_by_link_text('Add')
        add_quote_link.click()

        # user fills out the form
        self.browser.find_element_by_name('title').send_keys(' "You can be anything in this world" Apostle Grace L')
        self.browser.find_element_by_name('submitter').send_keys("Quotes")
        self.browser.find_element_by_name('description').send_keys("What an amazing quote")

        # user clicks the save button

        self.browser.find_element_by_css_selector("input[value='Save']").click()

        # the Quote has been added

        body = self.browser.find_element_by_tag_name('body')
        self.assertIn('You can be anything', body.text)

        # user returns to the main admin screen

        home_link = self.browser.find_element_by_link_text('Home')
        home_link.click()

        # user logs out

        self.browser.find_element_by_link_text('Log out').click()
        body = self.browser.find_element_by_tag_name('body')
        self.assertIn('Thanks for spending some quality time with the Web site today.', body.text)

        '''

        # index page for registering

        found = resolve('/')
        self.assertEqual(found.func, index)

        # registers

        found = resolve('/register/')
        self.assertEqual(found.func, register)

        # Home page

        found = resolve('/home/')
        self.assertEqual(found.func, home_page)

        # Contact us page

        found = resolve('/contact/')
        self.assertEqual(found.func, contact)

        # Enabled to add Quote

        found = resolve('/add_quote/')
        self.assertEqual(found.func, add_quote)

        # Quote saved

        # Favorite quote

        # Comment

        found = resolve('/comment/')
        self.assertEqual(found.func, comment)

        # Logout success

        found = resolve('/logout/')
        self.assertEqual(found.func, logout)

        # Resolved to login url

        found = resolve('/index/')
        self.assertEqual(found.func, index)

        '''

if __name__ == '__main__':
    unittest.main(warnings='ignore')


import grpc

import inference_pb2_grpc
import inference_pb2
import onnx_pb2

from transformers import GPT2Model, GPT2Tokenizer
import torch
import torch.nn.functional as F

import numpy as np

def post(out):
    logits = out[0,0, -1, :]
    log_probs = F.softmax(torch.tensor(logits), dim=-1)
    _, prev = torch.topk(log_probs, k=1, dim=-1)
    return prev.tolist()

def run_gpt2(host,maxlen):
    text="Tell me about IBM"
    print(text, end="")
    tokenizer = GPT2Tokenizer.from_pretrained('gpt2')
    tokens_ = tokenizer.encode(text)
    tokens = np.array(tokenizer.encode(text))
    for i in range(maxlen):

        options =  [('grpc.max_send_message_length', 512 * 1024 * 1024), ('grpc.max_receive_message_length', 512 * 1024 * 1024)]

        with grpc.insecure_channel(host,options = options) as channel:
            stub = inference_pb2_grpc.InferenceServiceStub(channel)
            tensor = onnx_pb2.TensorProto(data_type=7, dims=[1,1,len(tokens_)], int64_data=tokens_)
            response = stub.Inference(inference_pb2.InferenceRequest(tensor=[tensor], model_name='gpt2lm'))

        rdata = response.tensor[0].float_data

        prev = post(np.reshape(rdata, response.tensor[0].dims))
        print(tokenizer.decode(prev), end="")
        tokens_.append(prev[0])

if __name__ == '__main__':
    run_gpt2('10.1.20.99:50051', 15)