import os
import json
from requests import Session
import numpy as np
import sys
import argparse
from datetime import datetime

class Alliance:
    def __init__(self,
            color,
            team1,
            team2,
            auto,
            tele,
            endgame,
            score,
            auto_cone_pts,
            auto_low,
            auto_med,
            auto_high,
            signal_park,
            tele_cone_pts,
            tele_ground,
            tele_low,
            tele_med,
            tele_high,
            beacons,
            owned_junctions,
            circuit_pts):
        self.color = color
        self.team1 = team1
        self.team2 = team2
        self.auto = auto
        self.tele = tele
        self.endgame = endgame
        self.score = score
        self.auto_cone_pts = auto_cone_pts
        self.auto_low = auto_low
        self.auto_med = auto_med
        self.auto_high = auto_high
        self.signal_park = signal_park
        self.tele_cone_pts = tele_cone_pts
        self.tele_ground = tele_ground
        self.tele_low = tele_low
        self.tele_med = tele_med
        self.tele_high = tele_high
        self.beacons = beacons
        self.owned_junctions = owned_junctions
        self.circuit_pts = circuit_pts

class Match:
    def __init__(self, num, red, blue):
        self.num = num
        self.red = red
        self.blue = blue

def get_api_key():
    file = os.path.join("api_key")
    with open(file, 'r') as f:
        api_key = f.read().strip()
        return api_key

def scrape_event(event_code):
    api_key = get_api_key()
    session = Session()
    session.headers.update({"Authorization": "Basic {}".format(api_key), "content-type": "application/json"})
    event_url = 'https://ftc-api.firstinspires.org/v2.0/2022/matches/{}'.format(event_code)
    with open('data/{}-event.json'.format(event_code), 'w') as f:
        response = session.get(event_url)
        f.write(json.dumps(response.json()))

def scrape_team_list(event_code):
    api_key = get_api_key()
    session = Session()
    session.headers.update({"Authorization": "Basic {}".format(api_key), "content-type": "application/json"})
    event_url = 'https://ftc-api.firstinspires.org/v2.0/2022/teams?eventCode={}'.format(event_code)
    response = session.get(event_url)
    teams = list(map(lambda x: "{} {}".format(str(x['teamNumber']), x['nameShort']), response.json()['teams']))
    with open('data/{}-teams.json'.format(event_code), 'w') as f:
        for team in teams:
            f.write(str(team) + '\n')

def scrape_score(event_code):
    api_key = get_api_key()
    session = Session()
    session.headers.update({"Authorization": "Basic {}".format(api_key), "content-type": "application/json"})
    event_url = 'https://ftc-api.firstinspires.org/v2.0/2022/scores/{}/qual'.format(event_code)
    response = session.get(event_url)
    print(response.json()['MatchScores'][0]['alliances'][0])

def scrape_matches(event_code):
    api_key = get_api_key()
    session = Session()
    session.headers.update({"Authorization": "Basic {}".format(api_key), "content-type": "application/json"})
    event_url = 'https://ftc-api.firstinspires.org/v2.0/2022/scores/{}/qual'.format(event_code)
    response = session.get(event_url)
    with open('data/{}-matches.json'.format(event_code), 'w') as f:
        f.write(json.dumps(response.json()))

def load_matches(event_code):
    with open('data/{}-event.json'.format(event_code), 'r') as f:
        s = f.read()
        data = json.loads(s)
        matches = []
        for match_num, loaded_match in enumerate(data['matches']):
            teams = list(map(lambda x: x['teamNumber'], loaded_match['teams']))
            red1, red2 = int(teams[0]), int(teams[1])
            blue1, blue2 = int(teams[2]), int(teams[3])
            red_score = int(loaded_match['scoreRedFinal'])
            red_auto = int(loaded_match['scoreRedAuto'])
            blue_score = int(loaded_match['scoreBlueFinal'])
            blue_auto = int(loaded_match['scoreBlueAuto'])
            red = Alliance('red', red1, red2, red_auto, red_score)
            blue = Alliance('blue', blue1, blue2, blue_auto, blue_score)
            final_match = Match(match_num+1, red, blue)
            matches.append(final_match)
        return matches

def build_alliance(color, team1, team2, match_data):
    return Alliance(
            color,
            team1,
            team2,
            match_data['autoPoints'],
            match_data['dcPoints'],
            match_data['endgamePoints'],
            match_data['prePenaltyTotal'],
            match_data['autoJunctionConePoints'],
            match_data['autoJunctionCones'][1],
            match_data['autoJunctionCones'][2],
            match_data['autoJunctionCones'][3],
            match_data['signalBonusPoints'],
            match_data['dcJunctionConePoints'],
            match_data['dcJunctionCones'][0],
            match_data['dcJunctionCones'][1],
            match_data['dcJunctionCones'][2],
            match_data['dcJunctionCones'][3],
            match_data['beacons'],
            match_data['ownedJunctions'],
            match_data['circuitPoints']
            )

def load_matches_new(event_code):
    with open('data/{}-matches.json'.format(event_code), 'r') as a, open('data/{}-event.json'.format(event_code), 'r') as b:
        s = a.read()
        t = b.read()
        match_data = json.loads(s)
        event_data = json.loads(t)
        matches = []
        if len(match_data['MatchScores']) == 0:
            print('skipping', event_code)
            return None
        for i, m in enumerate(match_data['MatchScores']):
            teams = list(map(lambda x: x['teamNumber'], event_data['matches'][i]['teams']))
            red1, red2 = int(teams[0]), int(teams[1])
            blue1, blue2 = int(teams[2]), int(teams[3])
            blue = build_alliance('blue', blue1, blue2, m['alliances'][0])
            red = build_alliance('red', red1, red2, m['alliances'][1])
            built_match = Match(i+1, red, blue)
            matches.append(built_match)
        return matches

def load_teams(event_code):
    with open('data/{}-teams.json'.format(event_code), 'r') as f:
        teams = {}
        for l in f.readlines():
            cleaned = l.strip()
            i = cleaned.index(" ")
            num = int(cleaned[:i])
            name = str(cleaned[i+1:])
            teams[num] = name
        return teams

def build_matrix(teams, matches):
    M = []
    for m in matches:
        r = []
        for team in teams:
            if m.red.team1 == team or m.red.team2 == team: r.append(1)
            else: r.append(0)
        M.append(r)
        b = []
        for team in teams:
            if m.blue.team1 == team or m.blue.team2 == team: b.append(1)
            else: b.append(0)
        M.append(b)
    return M

def build_new_data(matches):
    auto, tele, endgame, score, auto_cone_pts, auto_low, auto_med, auto_high, signal_park, tele_cone_pts, tele_ground, tele_low, tele_med, tele_high, beacons, owned_junctions, circuit_pts = [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], []
    for m in matches:
        auto.append([m.red.auto])
        tele.append([m.red.tele])
        endgame.append([m.red.endgame])
        score.append([m.red.score])
        auto_cone_pts.append([m.red.auto_cone_pts])
        auto_low.append([m.red.auto_low])
        auto_med.append([m.red.auto_med])
        auto_high.append([m.red.auto_high])
        signal_park.append([m.red.signal_park])
        tele_cone_pts.append([m.red.tele_cone_pts])
        tele_ground.append([m.red.tele_ground])
        tele_low.append([m.red.tele_low])
        tele_med.append([m.red.tele_med])
        tele_high.append([m.red.tele_high])
        beacons.append([m.red.beacons])
        owned_junctions.append([m.red.owned_junctions])
        circuit_pts.append([m.red.circuit_pts])

        auto.append([m.blue.auto])
        tele.append([m.blue.tele])
        endgame.append([m.blue.endgame])
        score.append([m.blue.score])
        auto_cone_pts.append([m.blue.auto_cone_pts])
        auto_low.append([m.blue.auto_low])
        auto_med.append([m.blue.auto_med])
        auto_high.append([m.blue.auto_high])
        signal_park.append([m.blue.signal_park])
        tele_cone_pts.append([m.blue.tele_cone_pts])
        tele_ground.append([m.blue.tele_ground])
        tele_low.append([m.blue.tele_low])
        tele_med.append([m.blue.tele_med])
        tele_high.append([m.blue.tele_high])
        beacons.append([m.blue.beacons])
        owned_junctions.append([m.blue.owned_junctions])
        circuit_pts.append([m.blue.circuit_pts])

    return auto, tele, endgame, score, auto_cone_pts, auto_low, auto_med, auto_high, signal_park, tele_cone_pts, tele_ground, tele_low, tele_med, tele_high, beacons, owned_junctions, circuit_pts

def solve_new(M, auto, tele, endgame, score, auto_cone_pts, auto_low, auto_med, auto_high, signal_park, tele_cone_pts, tele_ground, tele_low, tele_med, tele_high, beacons, owned_junctions, circuit_pts):
    M = np.matrix(M)
    auto = np.matrix(auto)
    tele = np.matrix(tele)
    endgame = np.matrix(endgame)
    score = np.matrix(score)
    auto_cone_pts = np.matrix(auto_cone_pts)
    auto_low = np.matrix(auto_low)
    auto_med = np.matrix(auto_med)
    auto_high = np.matrix(auto_high)
    signal_park = np.matrix(signal_park)
    tele_cone_pts = np.matrix(tele_cone_pts)
    tele_ground = np.matrix(tele_ground)
    tele_low = np.matrix(tele_low)
    tele_med = np.matrix(tele_med)
    tele_high = np.matrix(tele_high)
    beacons = np.matrix(beacons)
    owned_junctions = np.matrix(owned_junctions)
    circuit_pts = np.matrix(circuit_pts)

    psuedoinverse = np.linalg.pinv(M)

    auto = np.matmul(psuedoinverse, auto)
    tele = np.matmul(psuedoinverse, tele)
    endgame = np.matmul(psuedoinverse, endgame)
    score = np.matmul(psuedoinverse, score)
    auto_cone_pts = np.matmul(psuedoinverse, auto_cone_pts)
    auto_low = np.matmul(psuedoinverse, auto_low)
    auto_med = np.matmul(psuedoinverse, auto_med)
    auto_high = np.matmul(psuedoinverse, auto_high)
    signal_park = np.matmul(psuedoinverse, signal_park)
    tele_cone_pts = np.matmul(psuedoinverse, tele_cone_pts)
    tele_ground = np.matmul(psuedoinverse, tele_ground)
    tele_low = np.matmul(psuedoinverse, tele_low)
    tele_med = np.matmul(psuedoinverse, tele_med)
    tele_high = np.matmul(psuedoinverse, tele_high)
    beacons = np.matmul(psuedoinverse, beacons)
    owned_junctions = np.matmul(psuedoinverse, owned_junctions)
    circuit_pts = np.matmul(psuedoinverse, circuit_pts)

    return auto, tele, endgame, score, auto_cone_pts, auto_low, auto_med, auto_high, signal_park, tele_cone_pts, tele_ground, tele_low, tele_med, tele_high, beacons, owned_junctions, circuit_pts

def matrix_to_list(m):
    res = []
    for x in m: res.append(round(float(x), 2))
    return res

def format_team(num, name, score, auto, tele, endgame, auto_cone_pts, auto_low, auto_med, auto_high, signal_park, tele_cone_pts, tele_ground, tele_low, tele_med, tele_high, beacons, owned_junctions, circuit_pts):
    return f'{num : ^10}{name[:min(len(name), 20)] : ^30}{score : ^15}{auto: ^15}{tele: ^15}{endgame: ^15}{auto_cone_pts: ^15}{auto_low: ^15}{auto_med: ^15}{auto_high: ^15}{signal_park: ^15}{tele_cone_pts: ^15}{tele_ground: ^15}{tele_low: ^15}{tele_med: ^15}{tele_high: ^15}{beacons: ^15}{owned_junctions: ^15}{circuit_pts: ^15}'

def print_header():
    print(format_team('TEAM', 'NAME', 'OPR', 'AUTO', 'TELE', 'END', 'A_CONE_PTS', 'A_L', 'A_M', 'A_H', 'PARK', 'T_CONE_PTS', 'T_G', 'T_L', 'T_M', 'T_H', 'BEACON', 'OWNED', 'CIRCUIT'))

def print_team(num, name, auto, tele, endgame, score, auto_cone_pts, auto_low, auto_med, auto_high, signal_park, tele_cone_pts, tele_ground, tele_low, tele_med, tele_high, beacons, owned_junctions, circuit_pts):
    print(format_team(num, name, score, auto, tele, endgame, auto_cone_pts, auto_low, auto_med, auto_high, signal_park, tele_cone_pts, tele_ground, tele_low, tele_med, tele_high, beacons, owned_junctions, circuit_pts))

def print_all_teams(teams, teams_list, sorted_teams, sorted_auto, sorted_tele, sorted_endgame, sorted_score, sorted_auto_cone_pts, sorted_auto_low, sorted_auto_med, sorted_auto_high, sorted_signal_park, sorted_tele_cone_pts, sorted_tele_ground, sorted_tele_low, sorted_tele_med, sorted_tele_high, sorted_beacons, sorted_owned_junctions, sorted_circuit_pts):
        for i in range(len(teams_list)):
            team_num = sorted_teams[i]
            print_team(team_num, teams[team_num],sorted_auto[i], sorted_tele[i], sorted_endgame[i], sorted_score[i], sorted_auto_cone_pts[i], sorted_auto_low[i], sorted_auto_med[i], sorted_auto_high[i], sorted_signal_park[i], sorted_tele_cone_pts[i], sorted_tele_ground[i], sorted_tele_low[i], sorted_tele_med[i], sorted_tele_high[i], sorted_beacons[i], sorted_owned_junctions[i], sorted_circuit_pts[i])

def sort_teams(teams, auto, tele, endgame, score, auto_cone_pts, auto_low, auto_med, auto_high, signal_park, tele_cone_pts, tele_ground, tele_low, tele_med, tele_high, beacons, owned_junctions, circuit_pts):
    teams_list, sorted_teams = [], []
    sorted_auto, sorted_tele, sorted_endgame, sorted_score, sorted_auto_cone_pts, sorted_auto_low, sorted_auto_med, sorted_auto_high, sorted_signal_park, sorted_tele_cone_pts, sorted_tele_ground, sorted_tele_low, sorted_tele_med, sorted_tele_high, sorted_beacons, sorted_owned_junctions, sorted_circuit_pts = [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], []
    for team in teams: teams_list.append(team)
    while len(sorted_teams) < len(teams_list):
        auto_list = matrix_to_list(auto)
        tele_list = matrix_to_list(tele)
        endgame_list = matrix_to_list(endgame)
        score_list = matrix_to_list(score)
        auto_cone_pts_list = matrix_to_list(auto_cone_pts)
        auto_low_list = matrix_to_list(auto_low)
        auto_med_list = matrix_to_list(auto_med)
        auto_high_list = matrix_to_list(auto_high)
        signal_park_list = matrix_to_list(signal_park)
        tele_cone_pts_list = matrix_to_list(tele_cone_pts)
        tele_ground_list = matrix_to_list(tele_ground)
        tele_low_list = matrix_to_list(tele_low)
        tele_med_list = matrix_to_list(tele_med)
        tele_high_list = matrix_to_list(tele_high)
        beacons_list = matrix_to_list(beacons)
        owned_junctions_list = matrix_to_list(owned_junctions)
        circuit_pts_list = matrix_to_list(circuit_pts)

        for i in range(len(teams_list)):
            if teams_list[i] not in sorted_teams:
                best_team = teams_list[i]
                best_auto = auto_list[i]
                best_tele = tele_list[i]
                best_endgame = endgame_list[i]
                best_score = score_list[i]
                best_auto_cone_pts = auto_cone_pts_list[i]
                best_auto_low = auto_low_list[i]
                best_auto_med = auto_med_list[i]
                best_auto_high = auto_high_list[i]
                best_signal_park = signal_park_list[i]
                best_tele_cone_pts = tele_cone_pts_list[i]
                best_tele_ground = tele_ground_list[i]
                best_tele_low = tele_low_list[i]
                best_tele_med = tele_med_list[i]
                best_tele_high = tele_high_list[i]
                best_beacons = beacons_list[i]
                best_owned_junctions = owned_junctions_list[i]
                best_circuit_pts = circuit_pts_list[i]

        for i in range(len(teams_list)):
            if score_list[i] > best_score and teams_list[i] not in sorted_teams:
                best_team = teams_list[i]
                best_auto = auto_list[i]
                best_tele = tele_list[i]
                best_endgame = endgame_list[i]
                best_score = score_list[i]
                best_auto_cone_pts = auto_cone_pts_list[i]
                best_auto_low = auto_low_list[i]
                best_auto_med = auto_med_list[i]
                best_auto_high = auto_high_list[i]
                best_signal_park = signal_park_list[i]
                best_tele_cone_pts = tele_cone_pts_list[i]
                best_tele_ground = tele_ground_list[i]
                best_tele_low = tele_low_list[i]
                best_tele_med = tele_med_list[i]
                best_tele_high = tele_high_list[i]
                best_beacons = beacons_list[i]
                best_owned_junctions = owned_junctions_list[i]
                best_circuit_pts = circuit_pts_list[i]

        sorted_teams.append(best_team)
        sorted_auto.append(best_auto)
        sorted_tele.append(best_tele)
        sorted_endgame.append(best_endgame)
        sorted_score.append(best_score)
        sorted_auto_cone_pts.append(best_auto_cone_pts)
        sorted_auto_low.append(best_auto_low)
        sorted_auto_med.append(best_auto_med)
        sorted_auto_high.append(best_auto_high)
        sorted_signal_park.append(best_signal_park)
        sorted_tele_cone_pts.append(best_tele_cone_pts)
        sorted_tele_ground.append(best_tele_ground)
        sorted_tele_low.append(best_tele_low)
        sorted_tele_med.append(best_tele_med)
        sorted_tele_high.append(best_tele_high)
        sorted_beacons.append(best_beacons)
        sorted_owned_junctions.append(best_owned_junctions)
        sorted_circuit_pts.append(best_circuit_pts)

    return [teams, teams_list, sorted_teams, sorted_auto, sorted_tele, sorted_endgame, sorted_score, sorted_auto_cone_pts, sorted_auto_low, sorted_auto_med, sorted_auto_high, sorted_signal_park, sorted_tele_cone_pts, sorted_tele_ground, sorted_tele_low, sorted_tele_med, sorted_tele_high, sorted_beacons, sorted_owned_junctions, sorted_circuit_pts]

def get_orange_api_key():
    file = os.path.join('orange_api_key')
    with open(file, 'r') as f:
        api_key = f.read().strip()
        return api_key

def strip_timestamp(t):
    return datetime.fromisoformat(t[:t.index('T')])

def check_doesnt_exist(event, name):
    return not os.path.exists('data/{}-{}.json'.format(event, name))

def check_needs_scrape(event):
    names = ['matches', 'teams', 'event']
    needs_scrape = False
    for name in names:
        needs_scrape = needs_scrape or check_doesnt_exist(event, name)
    return needs_scrape

def scrape(event):
    scrape_event(event)
    scrape_team_list(event)
    scrape_matches(event)

def analyze(event, should_print):
    matches = load_matches_new(event)
    if matches == None: return None
    teams = load_teams(event)
    M = build_matrix(teams, matches)
    auto, tele, endgame, score, auto_cone_pts, auto_low, auto_med, auto_high, signal_park, tele_cone_pts, tele_ground, tele_low, tele_med, tele_high, beacons, owned_junctions, circuit_pts = build_new_data(matches)
    auto, tele, endgame, score, auto_cone_pts, auto_low, auto_med, auto_high, signal_park, tele_cone_pts, tele_ground, tele_low, tele_med, tele_high, beacons, owned_junctions, circuit_pts = solve_new(M, auto, tele, endgame, score, auto_cone_pts, auto_low, auto_med, auto_high, signal_park, tele_cone_pts, tele_ground, tele_low, tele_med, tele_high, beacons, owned_junctions, circuit_pts)
    event_data = sort_teams(teams, auto, tele, endgame, score, auto_cone_pts, auto_low, auto_med, auto_high, signal_park, tele_cone_pts, tele_ground, tele_low, tele_med, tele_high, beacons, owned_junctions, circuit_pts)
    if should_print: print_all_teams(*event_data)
    return event_data

def get_team_event_response(team):
    api_key = get_api_key()
    session = Session()
    session.headers.update({"Authorization": "Basic {}".format(api_key), "content-type": "application/json"})
    event_url = 'https://ftc-api.firstinspires.org/v2.0/2022/events?teamNumber={}'.format(team)
    response = session.get(event_url).json()['events']
    event_codes = list(map(lambda x: x['code'], response))
    start_dates = list(map(lambda x: strip_timestamp(x['dateStart']), response))
    zipped = zip(event_codes, start_dates)
    res = list(map(lambda x: x[0], sorted(zipped, key=lambda x: x[1], reverse=True)))
    return res

def analyze_all_events(team):
    event_codes, start_dates = get_team_event_response(team)
    today = datetime.today()
    num_future_timestamps = sum([x > today for x in start_dates])
    prev_events = event_codes[num_future_timestamps + 1:]
    if len(prev_events) == 0: return []
    return prev_events

def analyze_recent_event(team):
    event_codes, start_dates = get_team_event_response(team)
    event_time_pairs = zip(event_codes, start_dates)
    event_time_pairs = sorted(event_time_pairs, key=lambda x: x[1], reverse=True)
    today = datetime.today()
    num_future_timestamps = sum([x > today for x in start_dates])
    print(num_future_timestamps)
    prev_events = list(map(lambda x: x[0], event_time_pairs))[num_future_timestamps:]
    return prev_events

def get_team_highest_opr(team):
    # events = get_team_event_response(team)
    # # best_opr = -100000 # surely no team will be below this opr
    # # best_event = []
    # # failed_event = True
    # for event in events:
    #     if check_needs_scrape(event):
    #         print('scraping', event)
    #         scrape(event)
    #     event_stats = analyze(event, False)
    #     if event_stats == None: continue
    #     idx = event_stats[2].index(team)
    #     team_stats = [team, event_stats[0][team]] + [x[idx] for x in event_stats[3:]]
    #     # team_opr = team_stats[5]
    #     return team_stats
    #    events = get_team_event_response(team)
    #  raise Exception('All events failed')
    events = get_team_event_response(team)
    # print(events)
    # best_opr = -100000 # surely no team will be below this opr
    best_event = []
    for event in events:
        if check_needs_scrape(event):
            print('scraping', event)
            scrape(event)
        event_stats = analyze(event, False)
        if event_stats == None: continue
        idx = event_stats[2].index(team)
        team_stats = [team, event_stats[0][team]] + [x[idx] for x in event_stats[3:]]
        # team_opr = team_stats[5]
        return team_stats
        # if team_opr > best_opr:
        #     best_opr = team_opr
        #     best_event = team_stats
    return best_event

def print_teams_best_events(best_events):
    print_header()
    best_events.sort(reverse=True, key=lambda x: x[5])
    for team in best_events:
        print_team(*team)

def scout_event(event_code):
    if not os.path.exists('{}-teams.json'.format(event_code)):
        scrape_team_list(event_code)
    teams = list(load_teams(event_code))
    best_events = list(filter(lambda x: len(x) > 0, map(lambda x: get_team_highest_opr(x), teams)))
    print_teams_best_events(best_events)

def main():
    # parser = argparse.ArgumentParser()
    # parser.add_argument('event')
    # parser.add_argument('-t', '--teams', action='store_true')
    # args = parser.parse_args()
    # print(args.event, args.teams)
    # get_team_highest_opr(21232)
    # if len(sys.argv) > 1: event = str(sys.argv[1])
    # scrape(event)
    # analyze(event)
    # scout_event('USCHSCMPVIO')
    scout_event('USTXCECMPJHNS')
    # analyze_recent_event(21232)

if __name__ == "__main__":
    main()


from flask import Flask, jsonify, request
import json
from ata_fir import ata_fir_function

app = Flask(__name__)

@app.route("/", methods=["GET"])
def index():
    '''
    - It uses an input json through GET. It must contain certain parameters. Check the Readme.
    - It Delivers ATA in json format.
    Adapted from https://www.youtube.com/watch?v=s_ht4AKnWZg&lc=z220gpfjoxnlurv4304t1aokg42ehhj3eurkyxdt4iyebk0h00410
    '''
    input_data= request.get_json()

    return jsonify({"Inferred ATA":ata_fir_function(input_data)})

if __name__ == "__main__":
    app.run(debug=True)


import csv
data1 = []
data2 = []

with open("final.csv","r") as f:
    csv_reader = csv.reader(f)
    for i in csv_reader:
        data1.append(i)

with open("archive_dataset.csv","r") as f:
    csv_reader= csv.reader(f)
    for i in csv_reader:
        data2.append(i)

header_1 = data1[0]
planet_data_1 = data1[1:]
header_2 = data2[0]
planet_data_2 = data2[1:]

header = header_1+header_2
planet_data=[]
for index, data_row in enumerate(planet_data_1):
    planet_data.append(planet_data_1[index]+planet_data_2[index])

with open("merged.csv","a+") as f:
    csv_writer = csv.writer(f)
    csv_writer.writerow(header)
    csv_writer.writerows(planet_data)


f = dict()
t = dict()
c = ''

with open('./unique-terms.txt', 'r') as u:
  for line in u.readlines():
    if ':' in line:
      c = line.strip()[:-1]
      f[c] = []
    elif line.strip() != '':
      f[c].append(line.strip())

with open('./all-terms.txt', 'r') as a:
  for i in a.readlines():
    k, v = i.split(' | ')
    t[k] = v

for k, v in f.items():
  with open(f'sorted/{k.replace(" ", "-")}.txt', 'w') as final:
    final.writelines([f'{i} | {t[i]}' for i in v if i in t])

for l in f.values():
  for v in l:
    if v in t.keys():
      del t[v]

with open('./missing-terms.txt', 'w') as missing:
    missing.writelines([k + '\n' for k in t.keys()])


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

# 라즈베리파이 GPIO 패키지
import RPi.GPIO as GPIO
from time import sleep

import sys
from socket import *
from select import *
import cv2

HOST = '127.0.0.1'
PORT = 10001
BUFSIZE = 1024
ADDR = (HOST, PORT)

# 소켓 생성
serverSocket = socket(AF_INET, SOCK_STREAM)

# 소켓 주소 정보 할당
serverSocket.bind(ADDR)
print('bind')

# 연결 수신 대기 상태
serverSocket.listen(100)
print('listen')

# 연결 수락
clientSocekt, addr_info = serverSocket.accept()
print('accept')
print('--client information--')
print(clientSocekt)
sys.stdout.flush()

# 클라이언트로부터 메시지를 가져옴

# 모터 상태
STOP  = 0
FORWARD  = 1
BACKWORD = 2

# 모터 채널
CH1 = 0
CH2 = 1

# PIN 입출력 설정
OUTPUT = 1
INPUT = 0

# PIN 설정
HIGH = 1
LOW = 0

# 실제 핀 정의
#PWM PIN
ENA = 26  #37 pin
ENB = 0   #27 pin

#GPIO PIN
IN1 = 19  #37 pin
IN2 = 13  #35 pin
IN3 = 6   #31 pin
IN4 = 5   #29 pin

# 핀 설정 함수
def setPinConfig(EN, INA, INB):
    GPIO.setup(EN, GPIO.OUT)
    GPIO.setup(INA, GPIO.OUT)
    GPIO.setup(INB, GPIO.OUT)
    # 100khz 로 PWM 동작 시킴
    pwm = GPIO.PWM(EN, 100)
    # 우선 PWM 멈춤.
    pwm.start(0)
    return pwm

# 모터 제어 함수
def setMotorContorl(pwm, INA, INB, speed, stat):

    #모터 속도 제어 PWM
    pwm.ChangeDutyCycle(speed)

    if stat == FORWARD:
        GPIO.output(INA, HIGH)
        GPIO.output(INB, LOW)

    #뒤로
    elif stat == BACKWORD:
        GPIO.output(INA, LOW)
        GPIO.output(INB, HIGH)

    #정지
    elif stat == STOP:
        GPIO.output(INA, LOW)
        GPIO.output(INB, LOW)

# 모터 제어함수 간단하게 사용하기 위해 한번더 래핑(감쌈)
def setMotor(ch, speed, stat):
    if ch == CH1:
        #pwmA는 핀 설정 후 pwm 핸들을 리턴 받은 값이다.
        setMotorContorl(pwmA, IN1, IN2, speed, stat)
    else:
        #pwmB는 핀 설정 후 pwm 핸들을 리턴 받은 값이다.
        setMotorContorl(pwmB, IN3, IN4, speed, stat)

# GPIO 모드 설정
GPIO.setmode(GPIO.BCM)

#모터 핀 설정
#핀 설정후 PWM 핸들 얻어옴
pwmA = setPinConfig(ENA, IN1, IN2)
pwmB = setPinConfig(ENB, IN3, IN4)

#제어 시작
n = 0
speed1 = 70
speed2 = 70
try:
# 앞으로 100프로 속도로
    while True:
        setMotor(CH1, speed1, FORWARD)
        setMotor(CH2, speed2, FORWARD)
        sleep(0.01)
        data = clientSocekt.recv(1000)
        print('recieve data : ',data.decode())
        msg = data.decode()
        if msg == 'person':
            setMotor(CH1, 0, FORWARD)
            setMotor(CH2, 0, FORWARD)
            sleep(3)
        elif msg == 'stop sign' :
            setMotor(CH1, 0, FORWARD)
            setMotor(CH2,0, FORWARD)
            sleep(5)
        else :
            speed1 = 70
            speed2 = 70
        msg = "default"

        if cv2.waitKey(1) == ord('q'):
            break

except KeyboardInterrupt:
    sys.exit(1)

clientSocekt.close()
serverSocket.close()
'''
# 뒤로 40프로 속도로
setMotor(CH1, 40, FORWARD)
setMotor(CH2, 40, FORWARD)
sleep(5)

# 뒤로 100프로 속도로
setMotor(CH1, 50, FORWARD)
setMotor(CH2, 50, BACKWORD)
sleep(5)

#정지
setMotor(CH1, 80, STOP)
setMotor(CH2, 80, STOP)
# 종료
'''
GPIO.cleanup()


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Sep 12 17:21:37 2021

@author: lihongyi
"""

import numpy as np
from sklearn import tree
from data_schema import get_train_test_ml_set
import matplotlib.pyplot as plt
plt.ioff()

def train_tree(independent_matrix, dependent_vector, depth, ccp_alpha=0):
    clf = tree.DecisionTreeClassifier(criterion='entropy', max_depth=depth, ccp_alpha=ccp_alpha)
    clf = clf.fit(independent_matrix, dependent_vector)
    return clf

def error_rate(independent_matrix, dependent_vector, clf):
    pred = clf.predict(independent_matrix)
    miss = pred != dependent_vector
    err = sum(miss) / len(miss)
    return err

def cv_err_by_tree_depth_alpha(train_set, depth, ccp_alpha=0, k=10):
    n = len(train_set.dependent_vector)

    errs_validate = []
    for i in range(k):
        idx_map = np.array([True] * n)
        if i == k-1:
            idx_map[(n // k)*i: ] = False
        else:
            idx_map[(n // k)*i: (n // k)*(i+1)] = False

        clf = train_tree(train_set.independent_matrix[idx_map], train_set.dependent_vector[idx_map], depth, ccp_alpha=ccp_alpha)
        err_validate = error_rate(train_set.independent_matrix[~idx_map], train_set.dependent_vector[~idx_map], clf)
        errs_validate.append(err_validate)

    return np.mean(errs_validate)

def best_tree_depth_by_cv(train_set, min_depth=1, max_depth=20, k=10, plot_name=None):
    depths = []
    cv_errs = []
    for d in range(min_depth, max_depth+1):
        depths.append(d)
        cv_errs.append(cv_err_by_tree_depth_alpha(train_set, d, k=k))
        print(d)
    best_d = depths[np.argmin(np.round(cv_errs, 5))]

    if plot_name is not None:
        fig = plt.figure()
        ax = plt.axes()
        ax.plot(depths, cv_errs)
        plt.axvline(x=best_d, color='red', linestyle='--')
        ax.set(xlabel='tree max depth', ylabel='cross-validation mean error rate',
               title=f'{k}-fold cross validation for decision tree on {plot_name}')
        fig.savefig(f'{plot_name}_dt_d_cv.png')
        plt.clf()
        plt.close()
    return best_d

def best_ccp_alpha_by_cv(train_set, depth, k=10, plot_name=None):
    clf = tree.DecisionTreeClassifier(criterion='entropy', max_depth=depth)
    path = clf.cost_complexity_pruning_path(train_set.independent_matrix, train_set.dependent_vector)
    ccp_alphas = path.ccp_alphas[:-1]

    cv_errs = []
    for a in ccp_alphas:
        cv_errs.append(cv_err_by_tree_depth_alpha(train_set, depth, ccp_alpha=a, k=k))
    best_a = ccp_alphas[np.argmin(np.round(cv_errs, 5))]

    if plot_name is not None:
        fig = plt.figure()
        ax = plt.axes()
        ax.plot(ccp_alphas, cv_errs)
        plt.axvline(x=best_a, color='red', linestyle='--')
        ax.set(xlabel='tree ccp alpha', ylabel=f'cross-validation mean error rate at depth {depth}',
               title=f'{k}-fold cross validation for decision tree on {plot_name}')
        fig.savefig(f'{plot_name}_dt_a_cv.png')
        plt.clf()
        plt.close()
    return best_a

def run(data_set_name):
    train_set, test_set = get_train_test_ml_set(data_set_name)
    best_d = best_tree_depth_by_cv(train_set, plot_name=data_set_name)
    best_a = best_ccp_alpha_by_cv(train_set, best_d, plot_name=data_set_name)
    clf = train_tree(train_set.independent_matrix, train_set.dependent_vector, best_d, best_a)
    train_err = error_rate(train_set.independent_matrix, train_set.dependent_vector, clf)
    with open(f'{data_set_name}_dt_train_err.txt', 'w') as f:
        f.write('%.5f' % train_err)
    test_err = error_rate(test_set.independent_matrix, test_set.dependent_vector, clf)
    with open(f'{data_set_name}_dt_test_err.txt', 'w') as f:
        f.write('%.5f' % test_err)

def main():
    run('abalone')
    run('bank-additional')

if __name__ == '__main__':
    main()


#!/home/aurmr/anaconda3/envs/rlgpu3/bin/python

from turtle import end_poly
from isaacgym import gymapi
from isaacgym import gymutil
from isaacgym import gymtorch
from isaacgym.torch_utils import *
import math
import numpy as np
import torch
from torch.nn.functional import grid_sample
from torch.nn import ReplicationPad2d
import random
import time
from torchvision.utils import save_image
from pytorch3d.common.workaround import symeig3x3
def quaternion_rotation_matrix(Q):
    """
    Covert a quaternion into a full three-dimensional rotation matrix.

    Input
    :param Q: A 4 element array representing the quaternion (q0,q1,q2,q3)

    Output
    :return: A 3x3 element matrix representing the full 3D rotation matrix.
             This rotation matrix converts a point in the local reference
             frame to a point in the global reference frame.
    """
    # Extract the values from Q
    q0 = Q[:, 0]
    q1 = Q[:, 1]
    q2 = Q[:, 2]
    q3 = Q[:, 3]

    # First row of the rotation matrix
    r00 = 2 * (q0 * q0 + q1 * q1) - 1
    r01 = 2 * (q1 * q2 - q0 * q3)
    r02 = 2 * (q1 * q3 + q0 * q2)

    # Second row of the rotation matrix
    r10 = 2 * (q1 * q2 + q0 * q3)
    r11 = 2 * (q0 * q0 + q2 * q2) - 1
    r12 = 2 * (q2 * q3 - q0 * q1)

    # Third row of the rotation matrix
    r20 = 2 * (q1 * q3 - q0 * q2)
    r21 = 2 * (q2 * q3 + q0 * q1)
    r22 = 2 * (q0 * q0 + q3 * q3) - 1

    # 3x3 rotation matrix
    rot_matrix = torch.stack([torch.stack([r00, r01, r02], -1),
                           torch.stack([r10, r11, r12], -1),
                           torch.stack([r20, r21, r22], -1)], -1)
    # print(rot_matrix.shquaternion_to_matrixape)
    return rot_matrix

#If seg_map is not none, crop around the segmap mask
def backproject_tensor(proj_mat, inv_view_mat, depth_image, seg_map=None, crop_size=64):

    xs = torch.linspace(0, depth_image.shape[2]-1, depth_image.shape[2])
    ys = torch.linspace(0, depth_image.shape[1]-1, depth_image.shape[1])
    (v, u) = torch.meshgrid(ys, xs)

    u = u.unsqueeze(0).expand(depth_image.shape[0], -1, -1).to(depth_image.device)
    v = v.unsqueeze(0).expand(depth_image.shape[0], -1, -1).to(depth_image.device)

    cam_height = depth_image.shape[1]
    cam_width = depth_image.shape[2]

    if seg_map is not None:
        centroid_x = (torch.sum(u*seg_map, (1,2))/torch.count_nonzero(u*seg_map, (1,2))).to(torch.int).unsqueeze(-1).to(torch.float)
        centroid_y = (torch.sum(v*seg_map, (1,2))/torch.count_nonzero(v*seg_map, (1,2))).to(torch.int).unsqueeze(-1).to(torch.float)
        grid_x = batch_tensor_linspace(centroid_x - crop_size//2, centroid_x + crop_size//2-1, steps=crop_size)
        grid_y = batch_tensor_linspace(centroid_y - crop_size//2, centroid_y + crop_size//2-1, steps=crop_size)
        grid = torch.cat((grid_x.expand(-1, crop_size, -1).unsqueeze(-1), grid_y.transpose(1,2).expand(-1, -1, crop_size).unsqueeze(-1)), dim=-1)

        grid[..., 0] = (grid[..., 0].to(torch.float) - cam_width//2)/(cam_width//2)
        grid[..., 1] = (grid[..., 1].to(torch.float) - cam_height//2)/(cam_height//2)
        # box_left = centroid_x - crop_size//2
        # box_top = centroid_y - crop_size//2

        # pre = seg_map[0].cpu().to(torch.float)*255
        # pre[(centroid_x[0][0].to(torch.int)-64):(centroid_x[0][0].to(torch.int)+64), (centroid_y[0][0].to(torch.int)-64):(centroid_y[0][0].to(torch.int)+64)] = .5 + \
        #     pre[(centroid_x[0][0].to(torch.int)-64):(centroid_x[0][0].to(torch.int)+64), (centroid_y[0][0].to(torch.int)-64):(centroid_y[0][0].to(torch.int)+64)]/2
        # save_image(pre, "/tmp/pre.bmp")
        depth_image = grid_sample(depth_image.unsqueeze(1), grid).squeeze()
        seg_map = grid_sample(seg_map.unsqueeze(1).to(torch.float), grid).squeeze().to(torch.int)
        # save_image(seg_map[0].cpu().to(torch.float)*255, "/tmp/post.bmp")
        u = grid_sample(u.unsqueeze(1).to(torch.float), grid).squeeze().to(torch.int)
        v = grid_sample(v.unsqueeze(1).to(torch.float), grid).squeeze().to(torch.int)

    # u_tmp = u.clone()
    # u = v
    # v = u_tmp

    fx = proj_mat[:, 0, 0].unsqueeze(1).unsqueeze(2)
    fy = proj_mat[:, 1, 1].unsqueeze(1).unsqueeze(2)

    u = -(2*u-cam_width)/cam_width
    v = (2*v-cam_height)/cam_height
    x_e = ((u) * depth_image) / fx
    y_e = ((v) * depth_image) / fy

    xyz_img = torch.stack([x_e, y_e, depth_image, torch.ones(depth_image.shape).to(depth_image.device)], dim=1)
    xyz_img = xyz_img.flatten(2,3).swapaxes(1,2)

    pointcloud = torch.bmm(xyz_img, inv_view_mat)
    pointcloud = pointcloud[:, :, 0:3]/(pointcloud[:, :, 3].unsqueeze(-1).expand((-1, -1, 3)))

    organized_pc = pointcloud[:, :, 0:3].view(seg_map.shape[0], seg_map.shape[1], seg_map.shape[2], 3)

    return (pointcloud[:, :, 0:3], seg_map, organized_pc)

def batch_cov(points, sample_size=9):
    size = points.size()
    B = int(torch.prod(torch.Tensor(list(size[:-2]))))
    D = int(size[-1])
    N = int(size[-2])
    final_size = list(size)
    # final_size.append(N)
    final_size.append(D)
    mean = torch.mean(points, dim=-2).unsqueeze(-2)
    diffs = (points - mean).reshape(B*N, D)
    prods = torch.bmm(diffs.unsqueeze(2), diffs.unsqueeze(1)).reshape(*(final_size))
    bcov = prods.sum(dim=-3) / (N - 1)  # Unbiased estimate
    return bcov  # (B, D, D)

def estimate_pc_normals(organized_points, kernel_size=3):
    width = organized_points.shape[1]
    height = organized_points.shape[2]

    xs = torch.arange(0, width, 1).to(organized_points.device)
    ys = torch.arange(0, height, 1).to(organized_points.device)
    grid = torch.stack(torch.meshgrid((xs, ys)), dim=-1).to(organized_points.device)
    grid_offset = grid + kernel_size-1
    grid_slices = batch_tensor_linspace(grid.to(torch.float), grid_offset.to(torch.float), kernel_size).to(torch.long).unsqueeze(-1).expand(-1, -1, -1, -1, kernel_size).to(organized_points.device).clone()
    grid_slices[:, :, 1, :, :] = grid_slices[:, :, 1, :, :].transpose(-2, -1)
    pc_mean = torch.mean(organized_points, dim=(1,2)).unsqueeze(1).unsqueeze(1)
    points_centered = organized_points - pc_mean
    pad = ReplicationPad2d(kernel_size//2).to(organized_points.device)
    org_pc_padded = pad(points_centered.permute((0, 3, 2, 1))).permute((0, 3, 2, 1))
    point_neighborhoods = org_pc_padded[:, grid_slices[:, :, 0, :, :].flatten(), grid_slices[:, :, 1, :, :].flatten(), :]
    point_neighborhoods = point_neighborhoods.reshape(-1, width, height, kernel_size*kernel_size, 3)

    point_neighborhoods = point_neighborhoods*1000
    cov = batch_cov(point_neighborhoods)
    curvatures, local_coord_frames = symeig3x3(cov, eigenvectors=True)
    normals = local_coord_frames[..., 0]
    return normals.view(normals.shape[0], -1, 3), local_coord_frames.view(normals.shape[0], -1, 3, 3)

def match_planes(normals, plane_points, points):

    MIN_DIST = .01

    #expand tensors into the correct shape
    points = points.unsqueeze(1).expand(-1, plane_points.shape[1], -1, -1)
    plane_points = plane_points.unsqueeze(2).expand(-1, -1, points.shape[2], -1)
    normals = normals.unsqueeze(2).expand(-1, -1, points.shape[2], -1)

    v = points-plane_points
    dists = torch.abs(torch.bmm(v.contiguous().view(-1, 1, 3), normals.contiguous().view(-1, 3, 1))).reshape(v.shape[0:-1])
    # matching_planes = torch.argmin(dists, 1)
    matching_planes = dists < MIN_DIST
    return matching_planes

def batch_tensor_linspace(start, end, steps=10):
    """
    Vectorized version of torch.linspace.
    Inputs:
    - start: Tensor of any shape
    - end: Tensor of the same shape as start
    - steps: Integer
    Returns:
    - out: Tensor of shape start.size() + (steps,), such that
      out.select(-1, 0) == start, out.select(-1, -1) == end,
      and the other elements of out linearly interpolate between
      start and end.
    """
    assert start.size() == end.size()

    start_w = torch.linspace(1, 0, steps=steps).to(start)
    end_w = torch.linspace(0, 1, steps=steps).to(start)

    expanded_dims = ([-1]*start.dim())
    expanded_dims.append(steps)
    start = start.unsqueeze(-1).expand(*expanded_dims)
    end = end.unsqueeze(-1).expand(*expanded_dims)

    out = start_w * start + end_w * end

    return out

def orientation_error(desired, current):
    cc = quat_conjugate(current)
    q_r = quat_mul(desired, cc)
    return q_r[:, 0:3] * torch.sign(q_r[:, 3]).unsqueeze(-1)


#
# Linear algebra subroutines
#
# Originally written in C++ by Ben Cumming, CSCS
# Ported to Python by Vasileios Karakasis, CSCS

import collections
import numba
import numpy as np
import sys

import operators

# epsilon value use for matrix-vector approximation
EPS = 1.0e-8
EPS_INV = 1.0 / EPS

CGStatus = collections.namedtuple('CGStatus',
                                  ['converged', 'iters', 'residual'])

@numba.njit(cache=True)
def cg(x, x_old, b, boundary, options, tolerance, maxiters):

    # Initialize temporary storage
    Fx = np.zeros_like(x)
    Fxold = np.zeros_like(x)
    xold = np.copy(x)

    # matrix vector multiplication is approximated with
    # A*v = 1/epsilon * ( F(x+epsilon*v) - F(x) )
    #     = 1/epsilon * ( F(x+epsilon*v) - Fxold )
    # we compute Fxold at startup
    # we have to keep x so that we can compute the F(x+exps*v)
    operators.diffusion(x, Fxold, x_old, boundary, options)

    v = (1. + EPS) * x

    # Fx = F(v)
    operators.diffusion(v, Fx, x_old, boundary, options)

    # r = b - A*x
    # where A*x = (Fx-Fxold)/eps
    r = b - EPS_INV * (Fx - Fxold)

    # p = r
    p = np.copy(r)

    # rold = <r,r>
    rold = r @ r
    rnew = rold

    if np.sqrt(rold) < tolerance:
        return CGStatus(True, 0, np.sqrt(rnew))

    for it in range(1, maxiters + 1):
        # Ap = A*p
        v = xold + EPS * p
        operators.diffusion(v, Fx, x_old, boundary, options)

        Ap = EPS_INV * (Fx - Fxold)

        # alpha = rold / p'*Ap
        alpha = rold / (p @ Ap)

        x += alpha * p

        r -= alpha * Ap

        # find new norm
        rnew = r @ r

        residual = np.sqrt(rnew)
        if (residual < tolerance):
            return CGStatus(True, it, residual)

        p = r + (rnew / rold) * p
        rold = rnew

    return CGStatus(False, it, residual)


"""
Python Data Structures - A Game-Based Approach
A Star Algorithm maze solver.
Robin Andrews - https://compucademy.net/
Uses a priority queue containing f-values and (i, j) tuples along with dictionaries for
predecessors and g-values.
"""
import os
from helpers import get_path, offsets, is_legal_pos, read_maze
from priority_queue import PriorityQueue

def heuristic(a, b):
    """
    Calculates the Manhattan distance between two pairs of grid coordinates.
    """
    x1, y1 = a
    x2, y2 = b
    return abs(x1 - x2) + abs(y1 - y2)

def a_star(maze, start, goal):
    """
    create pq
    add start to pq
    predecessor start: none
    g value start: 0
    add while statement for empty pq
    check current_cell is goal
    get path of cell
    for direction loop
    offset the row column
    check neighbor validity
    add g value neighbor = heuristic(start, current_cell)
    add predecessor neighbor = current cell
    add to pq neighbor, priority(current_cell, goal)

    """
    print(os.getcwd())

    pq = PriorityQueue()
    # The initial priority value doesn't matter since it will be the first one popped
    pq.put(start, 0)

    g = {start: 0}
    predecessors = {start: None}

    while not pq.is_empty():
        current_cell = pq.get()

        if current_cell == goal:
            path = get_path(predecessors, start, goal)
            return path

        for direction in ["up", "right", "down", "left"]:
            row_offset, col_offset = offsets[direction]
            neighbor = (current_cell[0]+row_offset, current_cell[1]+col_offset)

            if is_legal_pos(maze, neighbor) and neighbor not in predecessors:
                # heuristic of neighbor to goal + distance from start
                # should be the weight
                h = heuristic(neighbor, goal)
                new_cost = g[current_cell] + 1
                f_value = h + new_cost

                pq.put(neighbor, f_value)

                g[neighbor] = new_cost
                predecessors[neighbor] = current_cell

    return None

if __name__ == "__main__":
    # Test 1
    maze = [[0] * 3 for row in range(3)]
    start_pos = (0, 0)
    goal_pos = (2, 2)
    result = a_star(maze, start_pos, goal_pos)
    assert result == [(0, 0), (0, 1), (0, 2), (1, 2), (2, 2)]

    # Test 2
    maze = read_maze("08_03_begin/mazes/mini_maze_bfs.txt")
    for row in maze:
        print(row)
    start_pos = (0, 0)
    goal_pos = (2, 2)
    result = a_star(maze, start_pos, goal_pos)
    assert result == [(0, 0), (1, 0), (1, 1), (1, 2), (2, 2)]

    # Test 3
    maze = read_maze("08_03_begin/mazes/mini_maze_bfs.txt")
    for row in maze:
        print(row)
    start_pos = (0, 0)
    goal_pos = (3, 3)
    result = a_star(maze, start_pos, goal_pos)
    assert result is None


# -*- coding: utf-8 -*-
import ast
import collections
import sys
import six
from abc import (
    ABC,
    ABCMeta,
)

import unreal

from .pipeline import (
    create_publish_instance,
    imprint,
    ls_inst,
    UNREAL_VERSION
)
from openpype.lib import (
    BoolDef,
    UILabelDef
)
from openpype.pipeline import (
    Creator,
    LoaderPlugin,
    CreatorError,
    CreatedInstance
)

@six.add_metaclass(ABCMeta)
class UnrealBaseCreator(Creator):
    """Base class for Unreal creator plugins."""
    root = "/Game/Ayon/AyonPublishInstances"
    suffix = "_INS"

    @staticmethod
    def cache_subsets(shared_data):
        """Cache instances for Creators to shared data.

        Create `unreal_cached_subsets` key when needed in shared data and
        fill it with all collected instances from the scene under its
        respective creator identifiers.

        If legacy instances are detected in the scene, create
        `unreal_cached_legacy_subsets` there and fill it with
        all legacy subsets under family as a key.

        Args:
            Dict[str, Any]: Shared data.

        Return:
            Dict[str, Any]: Shared data dictionary.

        """
        if shared_data.get("unreal_cached_subsets") is None:
            unreal_cached_subsets = collections.defaultdict(list)
            unreal_cached_legacy_subsets = collections.defaultdict(list)
            for instance in ls_inst():
                creator_id = instance.get("creator_identifier")
                if creator_id:
                    unreal_cached_subsets[creator_id].append(instance)
                else:
                    family = instance.get("family")
                    unreal_cached_legacy_subsets[family].append(instance)

            shared_data["unreal_cached_subsets"] = unreal_cached_subsets
            shared_data["unreal_cached_legacy_subsets"] = (
                unreal_cached_legacy_subsets
            )
        return shared_data

    def create(self, subset_name, instance_data, pre_create_data):
        try:
            instance_name = f"{subset_name}{self.suffix}"
            pub_instance = create_publish_instance(instance_name, self.root)

            instance_data["subset"] = subset_name
            instance_data["instance_path"] = f"{self.root}/{instance_name}"

            instance = CreatedInstance(
                self.family,
                subset_name,
                instance_data,
                self)
            self._add_instance_to_context(instance)

            pub_instance.set_editor_property('add_external_assets', True)
            assets = pub_instance.get_editor_property('asset_data_external')

            ar = unreal.AssetRegistryHelpers.get_asset_registry()

            for member in pre_create_data.get("members", []):
                obj = ar.get_asset_by_object_path(member).get_asset()
                assets.add(obj)

            imprint(f"{self.root}/{instance_name}", instance.data_to_store())

            return instance

        except Exception as er:
            six.reraise(
                CreatorError,
                CreatorError(f"Creator error: {er}"),
                sys.exc_info()[2])

    def collect_instances(self):
        # cache instances if missing
        self.cache_subsets(self.collection_shared_data)
        for instance in self.collection_shared_data[
                "unreal_cached_subsets"].get(self.identifier, []):
            # Unreal saves metadata as string, so we need to convert it back
            instance['creator_attributes'] = ast.literal_eval(
                instance.get('creator_attributes', '{}'))
            instance['publish_attributes'] = ast.literal_eval(
                instance.get('publish_attributes', '{}'))
            created_instance = CreatedInstance.from_existing(instance, self)
            self._add_instance_to_context(created_instance)

    def update_instances(self, update_list):
        for created_inst, changes in update_list:
            instance_node = created_inst.get("instance_path", "")

            if not instance_node:
                unreal.log_warning(
                    f"Instance node not found for {created_inst}")
                continue

            new_values = {
                key: changes[key].new_value
                for key in changes.changed_keys
            }
            imprint(
                instance_node,
                new_values
            )

    def remove_instances(self, instances):
        for instance in instances:
            instance_node = instance.data.get("instance_path", "")
            if instance_node:
                unreal.EditorAssetLibrary.delete_asset(instance_node)

            self._remove_instance_from_context(instance)

@six.add_metaclass(ABCMeta)
class UnrealAssetCreator(UnrealBaseCreator):
    """Base class for Unreal creator plugins based on assets."""

    def create(self, subset_name, instance_data, pre_create_data):
        """Create instance of the asset.

        Args:
            subset_name (str): Name of the subset.
            instance_data (dict): Data for the instance.
            pre_create_data (dict): Data for the instance.

        Returns:
            CreatedInstance: Created instance.
        """
        try:
            # Check if instance data has members, filled by the plugin.
            # If not, use selection.
            if not pre_create_data.get("members"):
                pre_create_data["members"] = []

                if pre_create_data.get("use_selection"):
                    utilib = unreal.EditorUtilityLibrary
                    sel_objects = utilib.get_selected_assets()
                    pre_create_data["members"] = [
                        a.get_path_name() for a in sel_objects]

            super(UnrealAssetCreator, self).create(
                subset_name,
                instance_data,
                pre_create_data)

        except Exception as er:
            six.reraise(
                CreatorError,
                CreatorError(f"Creator error: {er}"),
                sys.exc_info()[2])

    def get_pre_create_attr_defs(self):
        return [
            BoolDef("use_selection", label="Use selection", default=True)
        ]

@six.add_metaclass(ABCMeta)
class UnrealActorCreator(UnrealBaseCreator):
    """Base class for Unreal creator plugins based on actors."""

    def create(self, subset_name, instance_data, pre_create_data):
        """Create instance of the asset.

        Args:
            subset_name (str): Name of the subset.
            instance_data (dict): Data for the instance.
            pre_create_data (dict): Data for the instance.

        Returns:
            CreatedInstance: Created instance.
        """
        try:
            if UNREAL_VERSION.major == 5:
                world = unreal.UnrealEditorSubsystem().get_editor_world()
            else:
                world = unreal.EditorLevelLibrary.get_editor_world()

            # Check if the level is saved
            if world.get_path_name().startswith("/Temp/"):
                raise CreatorError(
                    "Level must be saved before creating instances.")

            # Check if instance data has members, filled by the plugin.
            # If not, use selection.
            if not instance_data.get("members"):
                actor_subsystem = unreal.EditorActorSubsystem()
                sel_actors = actor_subsystem.get_selected_level_actors()
                selection = [a.get_path_name() for a in sel_actors]

                instance_data["members"] = selection

            instance_data["level"] = world.get_path_name()

            super(UnrealActorCreator, self).create(
                subset_name,
                instance_data,
                pre_create_data)

        except Exception as er:
            six.reraise(
                CreatorError,
                CreatorError(f"Creator error: {er}"),
                sys.exc_info()[2])

    def get_pre_create_attr_defs(self):
        return [
            UILabelDef("Select actors to create instance from them.")
        ]

class Loader(LoaderPlugin, ABC):
    """This serves as skeleton for future Ayon specific functionality"""
    pass


from copy import deepcopy
from pathlib import Path

import pydantic
import pytest

from nrlf.core.validators import json_load
from nrlf.producer.fhir.r4.model import DocumentReference

PATH_TO_TEST_DATA = Path(__file__).parent / "data"
TEST_DATA_RELATIVE_PATHS = ["nrlf", "www.hl7.org/fhir/R4", "www.hl7.org/fhir/R4B"]

def read_test_data(relative_path: str):
    with open(
        PATH_TO_TEST_DATA / relative_path / "documentreference-example.json"
    ) as f:
        return json_load(f)

@pytest.mark.parametrize("relative_path", TEST_DATA_RELATIVE_PATHS)
def test_good_nrlf_document_is_valid(relative_path):
    document_reference = read_test_data(relative_path=relative_path)
    DocumentReference(**document_reference)

@pytest.mark.parametrize("relative_path", TEST_DATA_RELATIVE_PATHS)
def test_bad_nrlf_document_is_valid(relative_path):
    document_reference = deepcopy(read_test_data(relative_path=relative_path))
    document_reference["type"] = "an invalid value!"
    with pytest.raises(pydantic.ValidationError):
        DocumentReference(**document_reference)


import numpy as np

def secante_tabla(fx,xa,tolera):
    dx = 4*tolera
    xb = xa + dx
    tramo = dx
    tabla = []
    while (tramo>=tolera):
        fa = fx(xa)
        fb = fx(xb)
        xc = xa - fa*(xb-xa)/(fb-fa)
        tramo = abs(xc-xa)

        tabla.append([xa,xb,xc,tramo])
        xb = xa
        xa = xc

    tabla = np.array(tabla)
    return(tabla)

# ---------------------
# INGRESO
fx = lambda x: x**3 - np.exp(x)

a  = -1
b  = 2
xa = 1.5
tolera = 0.001
tramos = 100

# PROCEDIMIENTO
tabla = secante_tabla(fx,xa,tolera)
n = len(tabla)
raiz = tabla[n-1,2]

# SALIDA
np.set_printoptions(precision=4)
print('[xa ,  xb ,    xc ,    tramo]')
for i in range(0,n,1):
    print(tabla[i])
print('\nraiz en: ', raiz)

# ~ Mar Liz ~


from transformers import GPT2LMHeadModel, GPT2Tokenizer, TextDataset, DataCollatorForLanguageModeling, Trainer, TrainingArguments
import pdfplumber

# Replace with the path to your PDF file
pdf_path = r"C:\Users\Lenovo\PycharmProjects\LawModel\COI.pdf"

# Function to extract text from a PDF and save it as a text file
def extract_text_and_save_to_text_file(pdf_path, output_text_file):
    with pdfplumber.open(pdf_path) as pdf:
        extracted_text = ""
        for page in pdf.pages:
            extracted_text += page.extract_text()

    # Save the extracted text to a text file
    with open(output_text_file, "w", encoding="utf-8") as text_file:
        text_file.write(extracted_text)

# Replace with the desired output text file name
output_text_file = "output_text.txt"

# Call the function to extract text and save it to the text file
extract_text_and_save_to_text_file(pdf_path, output_text_file)

print(f"Text extracted from {pdf_path} and saved to {output_text_file}.")

# Load pre-trained GPT-2 model and tokenizer
model_name = "gpt2"  # You can choose different variants like "gpt2-medium" for larger models
tokenizer = GPT2Tokenizer.from_pretrained(model_name)
model = GPT2LMHeadModel.from_pretrained(model_name)

# Load your custom dataset
dataset_path = "output_text.txt"
train_dataset = TextDataset(tokenizer=tokenizer, file_path=dataset_path, block_size=128)

# Data collator for language modeling
data_collator = DataCollatorForLanguageModeling(tokenizer=tokenizer, mlm=False)

# Training arguments
training_args = TrainingArguments(
    output_dir="./my_custom_model",
    overwrite_output_dir=True,
    num_train_epochs=3,
    per_device_train_batch_size=64,
    save_steps=10_000,
    save_total_limit=2,
)

# Trainer
trainer = Trainer(
    model=model,
    args=training_args,
    data_collator=data_collator,
    train_dataset=train_dataset,
)

# Start fine-tuning
trainer.train()

# Save the fine-tuned model
trainer.save_model()


import logging
import plugin_registry
import os
import gitlab
import urllib.parse
import re

MY_SCHEME_NAME = "gitlab"

class GitLab(plugin_registry.contract.IPlugin):
    _url_resolver: plugin_registry.IUrlResolver

    def __init__(self, logger: logging.Logger) -> None:
        super().__init__(logger)
        self._url_resolver = UrlResolver(logger)
        logger.info(__class__.__name__ + " plugin loaded")

    def getUrlResolver(self, scheme: str) -> plugin_registry.IUrlResolver:
        return self._url_resolver if scheme == MY_SCHEME_NAME else None

class UrlResolver(plugin_registry.IUrlResolver):
    def __init__(self, logger: logging.Logger) -> None:
        super().__init__(logger)
        self.isVersioningSupported = True
        self._gls = {}
        self._repository_compare_cache = {}

    def _getGL(self, url: str):
        url_parsed = urllib.parse.urlparse(url)
        if url_parsed.hostname in self._gls:
            gl = self._gls[url_parsed.hostname]
        else:
            gl = gitlab.Gitlab(
                f"https://{url_parsed.hostname}",
                os.getenv("GITLAB_TOKEN"),  # TODO: Provide token as plugin config
            )
            if self._logger.isEnabledFor(logging.DEBUG):
                gl.enable_debug()  # TODO: Token leaks to output
            self._gls[url_parsed.hostname] = gl
        return gl

    def diff(self, url: str) -> plugin_registry.contract.IDiff | bool | None:
        gl = self._getGL(url)

        url_parsed = urllib.parse.urlparse(url)
        match = re.match(
            # Examples:
            # gitlab://mygitlab.io/user/project/-/blob/main/some/path/file1.txt@7e38559d
            r"/(?P<project_id>.+)/-/blob/(?P<ref_to>[^/]+)/(?P<file_path>[^@#]+)@(?P<ref_from>[a-fA-F0-9]+)",
            url_parsed.path,
        )
        project_id = match.group("project_id")
        file_path = match.group("file_path")
        ref_from = match.group("ref_from")
        ref_to = match.group("ref_to")

        try:
            if url_parsed.path not in self._repository_compare_cache:
                project = gl.projects.get(project_id)
                self._repository_compare_cache[
                    url_parsed.path
                ] = project.repository_compare(from_=ref_from, to=ref_to)
            elif self._repository_compare_cache[url_parsed.path] is None:
                return None
            compare_result = self._repository_compare_cache[url_parsed.path]
        except gitlab.GitlabGetError as e:
            self._logger.warning(f"{e.error_message}: {url}")
            self._repository_compare_cache[url_parsed.path] = None
            return None

        diff_entry_arr = [
            item for item in compare_result["diffs"] if item["old_path"] == file_path
        ]  # Expect 0 or 1 element

        if len(diff_entry_arr) == 0:
            return False  # No changes in this file

        match = re.match(
            # Examples:
            # L2
            # L2-5
            r"L(?P<url_first_line_number>[0-9]+)(-(?P<url_last_line_number>[0-9]+))?",
            url_parsed.fragment,
        )
        url_first_line_number = int(match.group("url_first_line_number"))
        if match.group("url_last_line_number") is None:
            url_last_line_number = url_first_line_number
        else:
            url_last_line_number = int(match.group("url_last_line_number"))

        diff_str = diff_entry_arr[0]["diff"]
        diff_split = re.split(r"^(@@.+@@).*$\n", diff_str, flags=re.MULTILINE)
        new_url_fragment = None
        new_first_line_number = url_first_line_number
        new_last_line_number = url_last_line_number
        new_lines_content: str = None
        was_lines_content: str = None
        for i in range(
            # The 0th element is always empty string
            1,
            len(diff_split),
            2,
        ):
            diff_chunk_header = diff_split[i]
            diff_chunk_content = diff_split[i + 1]
            match = re.match(
                # Example: @@ -2,8 +36,12 @@
                r"@@ -(?P<chunk_first_line_number>[0-9]+),(?P<chunk_line_count>[0-9]+) \+(?P<chunk_new_first_line_number>[0-9]+)(,(?P<chunk_new_line_count>[0-9]+))?",
                diff_chunk_header,
            )
            chunk_first_line_number = int(match.group("chunk_first_line_number"))
            chunk_line_count = int(match.group("chunk_line_count"))
            chunk_new_first_line_number = int(
                match.group("chunk_new_first_line_number")
            )
            chunk_new_line_count = (
                int(match.group("chunk_new_line_count"))
                if match.group("chunk_new_line_count") is not None
                else 0
            )

            shift = (chunk_new_first_line_number - chunk_first_line_number) + (
                chunk_new_line_count - chunk_line_count
            )
            if (
                url_first_line_number >= chunk_first_line_number + chunk_line_count
            ):  # The chunk is fully before the line range in URL
                new_first_line_number = url_first_line_number + shift
                new_last_line_number = url_last_line_number + shift
                continue

            if (
                url_last_line_number < chunk_first_line_number
            ):  # The chunk is fully after the line range in URL
                break

            chunk_lines_arr = diff_chunk_content.split("\n")
            in_current_line_number = chunk_first_line_number
            out_current_line_number = chunk_new_first_line_number
            new_lines_arr = []
            was_lines_arr = []
            for current_line_content in chunk_lines_arr[
                :-1
            ]:  # The last element is always empty string
                if in_current_line_number > url_last_line_number + 1:
                    break

                diff_line_change_indicator = current_line_content[0]

                if diff_line_change_indicator == " ":  # no change
                    new_lines_arr.append(current_line_content[1:])
                    was_lines_arr.append(current_line_content[1:])

                    if url_first_line_number == in_current_line_number:
                        new_first_line_number = out_current_line_number
                    if url_last_line_number == in_current_line_number:
                        new_last_line_number = out_current_line_number
                    in_current_line_number += 1
                    out_current_line_number += 1
                    if in_current_line_number > url_last_line_number:
                        break

                elif diff_line_change_indicator == "-":  # line gets removed
                    was_lines_arr.append(current_line_content[1:])
                    if url_first_line_number == in_current_line_number:
                        new_first_line_number = out_current_line_number
                    if url_last_line_number == in_current_line_number:
                        new_last_line_number = out_current_line_number - 1
                    in_current_line_number += 1

                else:  # diff_line_change_indicator == "+" # line gets inserted
                    new_lines_arr.append(current_line_content[1:])
                    if url_last_line_number <= in_current_line_number:
                        new_last_line_number = out_current_line_number
                    out_current_line_number += 1

            tmp = "\n".join(
                new_lines_arr[
                    new_first_line_number - chunk_new_first_line_number
                    if new_first_line_number - chunk_new_first_line_number > 0
                    else None : new_last_line_number - chunk_new_first_line_number + 1
                    if new_last_line_number - chunk_new_first_line_number
                    < len(new_lines_arr)
                    else None
                ]
            )
            new_lines_content = (
                new_lines_content + "..." + tmp
                if new_lines_content is not None
                else tmp
            )

            tmp = "\n".join(
                was_lines_arr[
                    url_first_line_number - chunk_first_line_number
                    if url_first_line_number - chunk_first_line_number > 0
                    else None : url_last_line_number - chunk_first_line_number + 1
                    if url_last_line_number - chunk_first_line_number
                    < len(was_lines_arr)
                    else None
                ]
            )
            was_lines_content = (
                was_lines_content + "..." + tmp
                if was_lines_content is not None
                else tmp
            )

            if url_last_line_number >= chunk_first_line_number + chunk_line_count:
                new_last_line_number = url_last_line_number + shift

            pass

        if new_first_line_number > new_last_line_number:
            new_url_fragment = url_parsed.fragment + "<-lines deleted"
        else:
            new_url_fragment = "L" + str(new_first_line_number)
            if new_last_line_number > new_first_line_number:
                new_url_fragment += "-"
                new_url_fragment += str(new_last_line_number)

        if (
            new_url_fragment is None or new_url_fragment == url_parsed.fragment
        ) and was_lines_content == new_lines_content:  # No changes detected
            return False

        url_parsed = url_parsed._replace(fragment=new_url_fragment)._replace(
            path=re.sub(
                r"@[a-fA-F0-9]+$",
                "@" + compare_result["commit"]["short_id"],
                url_parsed.path,
            )
        )
        if was_lines_content == new_lines_content:
            return plugin_registry.contract.IDiffLinesMoved(
                updated_url=url_parsed.geturl(), current_lines_content=new_lines_content
            )
        else:
            return plugin_registry.contract.IDiffContentChanged(
                updated_url=url_parsed.geturl(),
                current_lines_content=new_lines_content,
                was_lines_content=was_lines_content,
            )

    def resolveToContent(
        self, url: str
    ) -> plugin_registry.contract.IVersionedContent | None:
        gl = self._getGL(url)
        url_parsed = urllib.parse.urlparse(url)

        match = re.match(
            r"/(?P<project_id>.+)/-/blob/(?P<ref>[^/]+)/(?P<file_path>.+)",
            url_parsed.path,
        )
        project = gl.projects.get(match.group("project_id"))
        try:
            gitlab_file = project.files.get(
                file_path=match.group("file_path"), ref=match.group("ref")
            )
            all_lines = gitlab_file.decode()

            m = re.match(r"L(?P<from>\d+)(-(?P<to>\d+))?", url_parsed.fragment)
            from_line: int = int(m.group("from"))
            to_line: int = int(m.group("to")) if m.group("to") else None
            lines = all_lines.split(b"\n")[
                from_line - 1 : to_line if to_line else from_line
            ]

            return plugin_registry.contract.IVersionedContent(
                content=b"\n".join(lines),
                last_commit_id=gitlab_file.last_commit_id[0:8],
            )

        except gitlab.GitlabGetError as e:
            self._logger.warning(f"{e.error_message}: {url}")
            return None


import torch.nn as nn
import torch
import torch.nn.functional as F

class Net(nn.Module):

    def __init__(self):
        super(Net, self).__init__()
        self.conv1 = nn.Conv2d(3, 6, 5)
        self.pool1 = nn.MaxPool2d(2, 2)
        self.conv2 = nn.Conv2d(6, 16, 5)
        self.pool2 = nn.MaxPool2d(2, 2)
        self.fc1 = nn.Linear(16*5*5, 120)
        self.fc2 = nn.Linear(120, 84)
        self.fc3 = nn.Linear(84, 10)

    def forward(self, x):
        x = self.pool1(F.relu(self.conv1(x)))
        x = self.pool2(F.relu(self.conv2(x)))
        x = x.view(-1, 16*5*5)
        x = F.relu(self.fc1(x))
        x = F.relu(self.fc2(x))
        x = self.fc3(x)
        return x

    def initialize_weights(self):
        for m in self.modules(): # 遍历网络的每一层，判断各层属于什么结构，对于不同的结构，设定不同的权值初始化方法
            if isinstance(m, nn.Conv2d):
                torch.nn.init.xavier_normal_(m.weight.data) # 对 w（权重）进行初始化
                if m.bias is not None: # 如果有 b（偏置），则 set b = 0
                    m.bias.data.zero_()
            elif isinstance(m, nn.BatchNorm2d):
                m.weight.data.fill_(1)
                m.bias.data.zero_()
            elif isinstance(m, nn.Linear):
                torch.nn.init.normal_(m.weight.data, 0, 0.01)
                m.bias.data.zero_()

if __name__ == '__main__':
    model  = Net()
    model.initialize_weights() # 初始化权重
    for parameter in model.state_dict():
        print(parameter, model.state_dict()[parameter].size())


class Solution(object):
    def canPermutePalindrome(self, s):
        """
        :type s: str
        :rtype: bool
        """
        if s == '':
            return True
        dic = {}
        for letter in s:
            if letter in dic:
                del dic[letter]
            else:
                dic[letter] = 1
        return len(dic)<=1


from asyncio.windows_events import NULL
from turtle import color
import numpy as np
import cv2
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.cluster import KMeans
from collections import Counter
import colorsys
from operator import itemgetter
import json

def color_quantization(image, k):
    """Performs color quantization using K-means clustering algorithm"""
#    cv2.imshow('img', image)
#    cv2.waitKey(0)
    row,col,channel = image.shape
    # Transform image into 'data':
    data = np.float32(image).reshape((-1, 3))
#    print(data.shape)
#    print(type(data))
    # Define the algorithm termination criteria (the maximum number of iterations and/or the desired accuracy):
    # In this case the maximum number of iterations is set to 20 and epsilon = 1.0
    criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 20, 1.0)

    #making a numpy with 5 dimensions
    fivedimensionallist = []

    for xlocation in range(0,row):
            for ylocation in range(0,col):
                b,g,r = image[xlocation][ylocation]
                fivedimensionallist.append([b,g,r,abs((row//2 - xlocation)),abs((col//2 - ylocation))])
    reshapeimgarray = np.array(fivedimensionallist, dtype=np.uint8)
    reshapeimgarray = np.float32(reshapeimgarray)
    singlefileline = reshapeimgarray.shape

    # Apply K-means clustering algorithm:
    ret, label, center = cv2.kmeans(data, k, None, criteria, 10, cv2.KMEANS_RANDOM_CENTERS)
    retwlocation, labelwlocation, centerwlocation = cv2.kmeans(reshapeimgarray, k, None, criteria, 10, cv2.KMEANS_RANDOM_CENTERS)

   # centerwlocation = np.uint8(centerwlocation)

 #   print(centerwlocation)
    print(labelwlocation.shape)

    centroid_mask =[]
    for eachcentroid in centerwlocation:
            centroid_mask.append(np.array([eachcentroid[0],eachcentroid[1],eachcentroid[2]],dtype=np.uint8))
    centroid_mask = np.array(centroid_mask)
  #  print('yo', centroid_mask)
    # At this point we can make the image with k colors
    # Convert center to uint8:
    centerwlocation = np.uint8(centerwlocation)
    center = np.uint8(center)
    #centerwlocation = np.uint8(centerwlocation).tolist()
    #print(centerwlocation)
    #print(centerwlocation)

    #orderedcenterwlocation = [centerwlocation[i][0:3] for i in range(0,3)]

    #print("ahhh", orderedcenterwlocation)
#    centerwlocation = np.array(centerwlocation)
#    print(ret1, '\n', label1, '\n', center1, '\n')
    # Replace pixel values with their center value:
    result1 = center[label.flatten()]
    print(result1.shape)
    result1 = result1.reshape(image.shape)
    result = centerwlocation[labelwlocation.flatten()]
    print('help', result1.shape)
    result = result.reshape(singlefileline)
    result = np.delete(result,-1,1)
 # print(result.shape)
    result = np.delete(result,-1,1)
    print('help', result.shape)

    print(result)
    result = result.reshape(image.shape)

  #  result1 = center1[label1.flatten()]
  #  print("what's happening \n", type(label))
  #  centerwlocation = centerwlocation.sort(key = lambda centerwlocation:centerwlocation[-1])
    ind=np.argsort(centerwlocation[:,-1])
    centerwlocation = centerwlocation[ind]
    print(centerwlocation)
   # cv2.imshow('res', result1)
   # cv2.waitKey(0)
    cv2.imshow('res', result)
    cv2.waitKey(0)

    return result1, centerwlocation

def findrgbpixelcolor(arg1,dataarg2, colors, minimum,colorname):
    b,g,r = arg1
    B,G,R = dataarg2
  #  print(round(H*179),round(S*255),round(255*V))
 #   refH, refS, refV = dataarg2
    d =  ((abs(b-B))**2 + (abs(g-G))**2 + (abs(r-R))**2)**0.5
  #  d =  ((abs(b-B)) + (abs(g-G)) + (abs(r-R)))
    #print( b, g, r, B, G, R)
   # print("argument", arg1)
    if (d<= minimum):
        minimum = d
        colorname = colors
    return (colorname,minimum)

if __name__ == "__main__":
    image = cv2.imread("C:\\Users\\kirva\\Desktop\\forge\\winter2023\\blurtests\\circletestset1\\circleblursample-17whitered.jpg")
    cv2.imshow('res', image)
    cv2.waitKey(0)
    rgbimage = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
   # rgbimage = cv2.cvtColor(image, cv2.COLOR_RGB2HSV)
    cv2.imshow('res', rgbimage)
    cv2.waitKey(0)
    threemaskimage, centroid = color_quantization(rgbimage,3)
    print(centroid)
    cv2.imshow('res', threemaskimage)
    cv2.waitKey(0)

    # colordetection = color_mask(threemaskimage, centroid)
    # colordetection = "letter": ["color", "npimg"]

    print(threemaskimage)
    letter_upper_rgb = np.array(centroid[2][0:3])
   # letter_upper_rgb[0] = 255
    letter_low_rgb = letter_upper_rgb.copy()
  #  shape_upper_rgb = np.array(centroid[1][0:3])
  #  shape_low_rgb = shape_upper_rgb.copy()

  #  letter_upper_rgb[letter_upper_rgb <=255] += 1
    letter_low_rgb[letter_low_rgb >0] -= 1
  #  shape_upper_rgb[letter_upper_rgb <255] += 1
  #  shape_low_rgb[letter_low_rgb >0] -= 1
    print(letter_low_rgb,letter_upper_rgb)
    lettercolormask = cv2.inRange(threemaskimage, letter_low_rgb,letter_upper_rgb)
    letterresult = cv2.bitwise_and(threemaskimage,threemaskimage,mask=lettercolormask)
    letterresult = cv2.bitwise_not(letterresult)
    letterresult = cv2.cvtColor(letterresult, cv2.COLOR_BGR2GRAY)
    ret,letterbw = cv2.threshold(letterresult,254,255,cv2.THRESH_BINARY)
    letterbw = cv2.bitwise_not(letterbw)
    cv2.imshow('res3000', letterbw)

    cv2.waitKey(0)
  #  print(threemaskimage, '\n', centroid)


from django.shortcuts import render
from .models import *

global stream
global correct
correct = []
global questions
global q
global s
s=0
# Create your views here.

def index(request):
    return render(request, 'home.html', {'streams': Stream.objects.all()})

def ques(request, qid):
    global questions
    global q
    qu = questions[qid]
    q = qid
    return render(request, 'indexQ.html', {'q': qu})

def mark(request):
    r = request.POST.get('answer')
    global q
    global correct
    global s
    if r == questions[q].opcorrect:
        correct.append(questions[q])
    q += 1
    if q == questions.count():
        sum = 0
        for st in correct:
            if st.type == 'd' or st.type == 'D':
                sum += 1
            else:
                sum += 3
        print(sum)
        if sum < (s*3/5):
            mssg = 'Are you sure you want to continue this stream?'
        else:
            mssg = 'You are good. Continue if you like this stream.'
        return render(request, 'result.html', {'mssg': mssg, 'result': sum})

    return render(request, 'indexQ.html', {'q': questions[q]})

def instructions(request,sid):
    global stream
    global questions
    global s
    stream = sid
    for st in Stream.objects.filter(pk=sid):
        questions = st.questions_set.all().order_by('?')[:7]
    for k in questions:
        if k.type=='d' or k.type=='D':
            s+=1
        else:
            s+=3
    return render(request, 'instructions.html', {'s': s})


import pygame
from Object import Object

class StanImage(Object):
    def __init__(self, x, y, surf, group, life_time):
        pygame.sprite.Sprite.__init__(self)
        self.image = surf
        self.rect = self.image.get_rect(center=(x, y))
        self.add(group)
        self.life_time = life_time

    def update(self, vector):
        super().update(vector)
        self.life_time -= 1
        if self.life_time == 0:
            self.kill()

    def do_stan(self, target, stan_time):
        target.stanned = stan_time


import datetime
import json
from enum import Enum

from django.conf import settings
from django.shortcuts import render, redirect, reverse
from django.http.response import HttpResponse
from django.views.decorators.csrf import csrf_exempt

from front_end.models import PlanSubscriptionMovie, PurchasedMovies, Product
from my_auth.models import Profile
from .utils import validate_params_in_billing_api

class PaymentType(str, Enum):
    BUY = "buy"
    RENT = "rent"
    SUBSCRIBE = "subs"

@csrf_exempt
def callback(request, provider):
    body = ""

    if request.method == "POST":
        body_unicode = request.body.decode("utf-8")
        body = json.loads(body_unicode)

    api_url = settings.BILLING_CALLBACK
    params = (request.get_full_path(),)  # /api/robo/callback?params=value
    data = validate_params_in_billing_api(api_url, params[0], provider, body)

    ### Block save to DB
    usr = Profile.objects.get(uuid=data.get("user_id"))
    if data.get("payment_type") == PaymentType.BUY:
        curr_mov = Product.objects.get(uuid=data.get("content"))
        pur_mov = PurchasedMovies.objects.create(user=usr.user, movie_id=curr_mov)
        pur_mov.save()
    elif data.get("payment_type") == PaymentType.SUBSCRIBE:
        subs_plan = PlanSubscriptionMovie.objects.get(uuid=data.get("content"))
        usr.subscription_plan = subs_plan
        usr.subscription_status = 'enable'
        usr.subscription_last_payment_date = datetime.datetime.utcnow()
        usr.save()
    ###
    return HttpResponse(status=200)

def success(request, provider):
    api_url = settings.BILLING_SUCCESS
    params = (request.get_full_path(),)  # /api/robo/success?params=value
    data = validate_params_in_billing_api(api_url, params[0], provider)

    ### Block save to DB
    usr = Profile.objects.get(user__pk=request.user.pk)
    if data.get("payment_type") == "buy":
        curr_mov = Product.objects.get(uuid=data.get("content"))
        pur_mov = PurchasedMovies.objects.create(user=request.user, movie_id=curr_mov)
        pur_mov.save()
    elif data.get("payment_type") == "subs":
        subs_plan = PlanSubscriptionMovie.objects.get(uuid=data.get("content"))
        usr.subscription_plan = subs_plan
        usr.subscription_status = 'enable'
        usr.subscription_last_payment_date = datetime.datetime.utcnow()
        usr.save()
    ###

    ### Block redirect to purchaised movie or home page
    return redirect(data.get("redirect_url"))

def fail(request, provider):
    """Не готовая функция. Требует доработки"""
    api_url = settings.BILLING_FAIL
    # params = (request.get_full_path(),)  # /api/robo/success?params=value
    # data = validate_params_in_billing_api(api_url, params, provider)
    return redirect(reverse("front_end:home"))


from collections import defaultdict
import sys
sys.stdin = open('input.txt', 'r')

N = int(input())
graph = defaultdict(list)

for _ in range(N):
  node, left, right = input().rstrip().split()
  graph[node].append(left)
  graph[node].append(right)

def preOrderTraverse(node):
  if node == '.': return

  print(node, end="")
  preOrderTraverse(graph[node][0])
  preOrderTraverse(graph[node][1])

def inOrderTraverse(node):
  if node == '.': return

  inOrderTraverse(graph[node][0])
  print(node, end="")
  inOrderTraverse(graph[node][1])

def postOrderTravers(node):
  if node == '.': return

  postOrderTravers(graph[node][0])
  postOrderTravers(graph[node][1])
  print(node, end="")

preOrderTraverse('A')
print()
inOrderTraverse('A')
print()
postOrderTravers('A')
print()


import operator, string

# get the bill_dict as a dict
bills_txt = open("bill_dict.txt", "r")
bill_string = bills_txt.read()
bill_dict = eval(bill_string)

#get the most frequent words in the vote titles
words = {}
for bill in bill_dict:
    vote_title = bill_dict[bill]['vote_title']
    word_list = vote_title.split(" ")

    #count word frequency
    for w in word_list:
        for c in string.punctuation:
            if c == '.':
                continue
            else:
                w = w.replace(c,"")
        if not w.isdigit() and len(w)>2 and w[:1] != 'S.':
            if w not in words:
                words[w] = 1
            else:
                words[w] += 1

#Elimate top 100 words and names
top_100_txt = open("100_common_words.txt", "r")
top_100 = [line.strip('\n') for line in top_100_txt.readlines()]
top_100 = [line.strip('\t') for line in top_100]

for key in top_100:
    try:
        del words[key]
    except:
        continue

freq_dict = {}
for key in words:
    freq = words[key]
    if freq not in freq_dict:
        freq_dict[freq] = [key,]
    else:
        freq_dict[freq].append(key)

print(freq_dict)

# freqs = sorted(freq_dict.keys(),reverse=True)


#! /usr/bin/env python
#-*- coding:utf-8 -*-
# Author:   Masayuki Hirose
# Created:  2017-06-12

from logging import getLogger, StreamHandler, DEBUG
logger = getLogger(__name__)
handler = StreamHandler()
handler.setLevel(DEBUG)
logger.setLevel(DEBUG)
logger.addHandler(handler)

import sys
import os
import javalang
from javalang.tree import *
import subprocess
from py4j.java_gateway import JavaGateway
import time
import configparser

# configファイルの読み込み
inifile = configparser.ConfigParser()
inifile.read("config.ini")

class Get_Base_Info():
    _instance = None
    class_path = inifile["Enviroment"]["py4j_jar_path"]
    jar_path = inifile["Get_Base_Info"]["jar_path"]
    result_dir = inifile["Get_Base_Info"]["result_dir"]

    def __init__(self, template_maker):
        self.stat = template_maker.stat["Get_Base_Info"]

        # クラスパスを指定して実行
        args = (["java","-cp", self.class_path,
                 '-jar', self.jar_path])
        self.process = subprocess.Popen(args)

        # サーバー起動前に処理が下へ行くのを防ぐ
        time.sleep(3)
        self.gateway = JavaGateway()
        self.entry_point = self.gateway.entry_point

    @classmethod
    def get_instance(cls, template_maker):
        if cls._instance is None:
            cls._instance = Get_Base_Info(template_maker)
        return cls._instance

    def shutdown(self):
        self._instance = None
        self.gateway.shutdown()

    def run(self, template_id,
            q_path, a_path, q_start, q_end,
            a_start, a_end, q_exclusions, a_exclusions):
        if not os.path.exists(self.result_dir):
            os.makedirs(self.result_dir)
        if not os.path.exists(self.result_dir+"/"+template_id):
            os.makedirs(self.result_dir+"/"+template_id)
        exclusions1 = "-1" if len(q_exclusions) == 0 else ",".join(map(str, q_exclusions))
        exclusions2 = "-1" if len(a_exclusions) == 0 else ",".join(map(str, a_exclusions))
        print((q_path, a_path))
        self.entry_point.run(q_path, a_path, self.result_dir+"/"+template_id,
                             q_start, q_end,
                             a_start, a_end,
                             exclusions1, exclusions2)


class Employee:
    def __init__(self, id, name):
        self.id = id
        self.name = name

    def display(self):
        print(f"id: {self.id}, name: {self.name}\n")

emp = Employee(1, "coder")
emp.display()

del emp.id

try:
    print(emp.id)

except Exception as e:
    print('id is not found')

del emp

try:
    print(emp.name)
    emp.display()

except Exception as e:
    print('the object emp is not found')


import sys

from lxml import etree

from my_model.resource_string import ResourceString

class AndroidStringsXmlParser(object):
    """
    Parser utility for Android's strings.xml file
    """

    def __init__(self, xml_file):
        """
        constructor for AndroidXmlParser object

        :param xml_file: path of the Android's string resource (strings.xml) file to be parsed
        """
        path = xml_file
        self.root = etree.parse(path, etree.XMLParser(encoding='utf-8', recover=True)).getroot()

    def get_all_elements(self):
        return list(self.root.iter())

    def get_string_resources(self, value_filter=None):
        """
        Gets all string and string-array elements from Android's strings.xml resource files

        Example:

            resources = test_instance.get_all_string_resources()

            for res in resources:
                print("{0} {1} {2}".format(res.row_type, res.name, res.value))

        :return: a list of ResourceString object.
        :rtype: list
        """
        resources = []
        for element in self.root.findall("string"):
            resource = ResourceString(ResourceString.TYPE_STRING, element.get("name"), element.text)
            if value_filter is None or value_filter(resource):
                resources.append(resource)

        for element in self.root.findall("string-array"):
            array_name = element.get("name")
            for item in element.findall("item"):
                resource = ResourceString(ResourceString.TYPE_ARRAY_ELEMENT, array_name, item.text)
                if value_filter is None or value_filter(resource):
                    resources.append(resource)
        return resources

def main(argv):
    if len(argv) != 2 and len(argv) != 3:
        print("Usage: python {0} path/xml/file.xml [substring_to_find]".format(argv[0]))
        return
    test_instance = AndroidStringsXmlParser(argv[1])
    if len(argv) == 2:
        resources = test_instance.get_string_resources()
    else:
        def value_filter(s):
            if argv[2] in s:
                return True
            return False

        resources = test_instance.get_string_resources(value_filter)
    for res in resources:
        print(res.name)
        print(res.value.decode('utf-8'))
        # print("{0} {1} {2}".format(res.row_type, res.name, res.value))

if __name__ == '__main__':
    main(sys.argv)


import asyncio
import json
import time
import traceback
from contextlib import suppress
from datetime import datetime

from notebook.utils import url_path_join

class IdleChecker(object):
    def __init__(self):
        self.interval = 10  # frequency for checking idle sessions in seconds
        self._running = False
        self.count = 0
        self.task = None
        self.errors = None
        self.idle_time = 7200  # default idle time in seconds
        self.ignore_connections = True
        self.tornado_client = None
        self._xsrf_token = None
        self.base_url = None
        self.app_url = "http://0.0.0.0:8888"
        self.keep_terminals = False
        self.inservice_apps = {}

    # Function to GET the xsrf token
    async def fetch_xsrf_token(self):
        url = url_path_join(self.app_url, self.base_url, "tree")
        self.log.info("URL: " + str(url))
        response = await self.tornado_client.fetch(url, method="GET")
        self.log.info("response headers: " + str(response.headers))
        if "Set-Cookie" in response.headers:
            return response.headers["Set-Cookie"].split(";")[0].split("=")[1]

        return None

    # Invoke idle_checks() function
    async def run_idle_checks(self):
        while True:
            self.count += 1
            await asyncio.sleep(self.interval)
            try:
                self._xsrf_token = await self.fetch_xsrf_token()
                await self.idle_checks()
            except Exception:
                self.errors = traceback.format_exc()
                self.log.error(self.errors)

    # Entrypoint function to get the value from handlers(POST API call) and start background job
    def start(self, base_url, log_handler, client, idle_time, keep_terminals):
        self.idle_time = idle_time
        self.tornado_client = client
        self.base_url = base_url
        self.log = log_handler
        self.keep_terminals = keep_terminals
        self.errors = None  # clear error array at start

        if not self._running:
            self.count += 1
            self._running = True
            self.task = asyncio.ensure_future(self.run_idle_checks())

    async def stop(self):
        if self._running:
            self._running = False
            if self.task:
                self.task.cancel()
                with suppress(asyncio.CancelledError):
                    await self.task

    def get_runcounts(self):
        return self.count

    def get_runerrors(self):
        return self.errors

    # Function to check if the notebook is in Idle state
    def is_idle(self, last_activity, seconds=False):
        last_activity = datetime.strptime(last_activity, "%Y-%m-%dT%H:%M:%S.%fz")
        self.log.info(
            "comparing idle time limit "
            + str(self.idle_time)
            + " and elapsed time "
            + str((datetime.now() - last_activity).total_seconds())
        )
        if (datetime.now() - last_activity).total_seconds() > self.idle_time:
            self.log.info(
                "Notebook is idle. Last activity time = " + str(last_activity)
            )
            return True
        else:
            self.log.info(
                "Notebook is not idle. Last activity time = " + str(last_activity)
            )
            return False

    # Function to get the list of Kernel sessions
    async def get_sessions(self):
        url = url_path_join(self.app_url, self.base_url, "api", "sessions")
        response = await self.tornado_client.fetch(url, method="GET")
        sessions = json.loads(response.body)
        self.log.info(" Kernel Session is = " + str(sessions))
        return sessions

    # Function to get the list of System Terminals
    async def get_terminals(self):
        terminal_url = url_path_join(self.app_url, self.base_url, "api", "terminals")
        terminal_response = await self.tornado_client.fetch(terminal_url, method="GET")
        terminals = json.loads(terminal_response.body)
        return terminals

    # Function to get the list of running Apps
    async def get_apps(self):
        url = url_path_join(self.app_url, self.base_url, "sagemaker", "api", "apps")
        response = await self.tornado_client.fetch(url, method="GET")
        apps = json.loads(response.body)
        self.log.info(" Running App name is = " + str(apps))
        return apps

    # Function to build app information ( kernel sessions and image terminals)
    async def build_app_info(self):
        apps = await self.get_apps()
        apps_info = {}
        for app in apps:
            apps_info[app["app_name"]] = {"app": app, "sessions": [], "terminals": []}

        sessions = await self.get_sessions()
        for notebook in sessions:
            if notebook["kernel"]:
                notebook_app_name = notebook["kernel"]["app_name"]
                apps_info[notebook_app_name]["sessions"].append(notebook)

        terminals = await self.get_terminals()
        for terminal in terminals:
            if terminal["name"].find("arn:") != 0:
                continue

            env_arn, terminal_id, instance_type, *other = terminal["name"].split("__")

            self.log.info("Env Arn = " + str(env_arn))
            self.log.info("Terminal Id = " + str(terminal_id))
            self.log.info("Instance Type = "+ str(instance_type))

            for app in apps:
                if (
                    app["environment_arn"] == env_arn
                    and app["instance_type"] == instance_type
                ):
                    apps_info[app["app_name"]]["terminals"].append(terminal)
                    break

        self.log.info(str(apps_info))
        return apps_info

    # Function to delete a kernel session
    async def delete_session(self, session):
        headers = {}
        headers["X-Xsrftoken"] = self._xsrf_token
        headers["Cookie"] = "_xsrf=" + self._xsrf_token
        kernel_id = session["kernel"]["id"]
        self.log.info("deleting kernel : " + str(kernel_id))
        url = url_path_join(
            self.app_url, self.base_url, "api", "kernels", str(kernel_id)
        )
        deleted = await self.tornado_client.fetch(url, method="DELETE", headers=headers)
        self.log.info("Delete kernel response: " + str(deleted))

    # Function to delete an application
    async def delete_application(self, app_id):
        headers = {}
        headers["X-Xsrftoken"] = self._xsrf_token
        headers["Cookie"] = "_xsrf=" + self._xsrf_token
        self.log.info("deleting app : " + str(app_id))
        url = url_path_join(
            self.app_url, self.base_url, "sagemaker", "api", "apps", str(app_id)
        )
        deleted_apps = await self.tornado_client.fetch(
            url, method="DELETE", headers=headers
        )
        self.log.info("Delete App response: " + str(deleted_apps))
        if deleted_apps.code == 204 or deleted_apps.code == 200:
            self.inservice_apps.pop(app_id, None)

    # Function to check the notebook status
    def check_notebook(self, notebook):
        terminate = True
        if notebook["kernel"]["execution_state"] in ("idle", "starting"):
            self.log.info("found idle/starting session:" + str(notebook))
            if not self.ignore_connections:
                if notebook["kernel"]["connections"] == 0:
                    if not self.is_idle(notebook["kernel"]["last_activity"]):
                        terminate = False
                else:
                    terminate = False
            else:
                if not self.is_idle(notebook["kernel"]["last_activity"]):
                    terminate = False
        else:
            terminate = False
        return terminate

    # Run idle checks apps and image terminals
    async def idle_checks(self):
        apps_info = await self.build_app_info()
        inservice_apps = self.inservice_apps
        deleted_apps = list(set(inservice_apps.keys()).difference(set(apps_info.keys())))
        for deleted_app in deleted_apps:
            inservice_apps.pop(deleted_app, None)
            self.log.info("inservice app not inservice anymore : " + str(deleted_app))

        for app_name, app in apps_info.items():
            num_sessions = len(app["sessions"])
            num_terminals = len(app["terminals"])

            if num_sessions > 0 or num_terminals > 0:
                self.log.info(
                    "# of sessions: "
                    + str(num_sessions)
                    + "; # of terminals: "
                    + str(num_terminals)
                )

            if num_sessions == 0 and num_terminals == 0:
                # Check if app is active and kill
                # Check if the current app is part of the in service apps
                if app_name not in inservice_apps:
                    # Regsiter a new inservice app
                    inservice_apps[app_name] = time.time()

                else:
                    if int(time.time() - inservice_apps[app_name]) > self.idle_time:
                        self.log.info(
                            "Keep alive time for terminal reached : " + str(app_name)
                        )
                        await self.delete_application(app_name)

            # elif num_sessions < 1 and num_terminals > 0 and self.keep_terminals == True:
            elif num_sessions < 1 and num_terminals > 0 and self.keep_terminals:
                self.log.info("keep terminals flag is True. Not killing the terminals.")
                pass

            elif (
                # num_sessions < 1 and num_terminals > 0 and self.keep_terminals == False
                num_sessions < 1
                and num_terminals > 0
                and not self.keep_terminals
            ):
                self.log.info("keep terminals flag: " + str(self.keep_terminals))
                # Wait for the inservice app
                self.log.info("New inservice app found : " + str(app_name))

                # Check if the current app is part of the in service apps
                if app_name not in inservice_apps:
                    # Regsiter a new inservice app
                    inservice_apps[app_name] = time.time()

                else:
                    if int(time.time() - inservice_apps[app_name]) > self.idle_time:
                        self.log.info(
                            "Keepalive time for terminal reached : " + str(app_name)
                        )
                        await self.delete_application(app_name)

            elif num_sessions > 0:
                # let's check if we have idle notebooks to kill
                nb_deleted = 0
                for notebook in app["sessions"]:
                    if self.check_notebook(notebook):
                        # handle kernel sessions which are stuck in "starting" state
                        if notebook["kernel"]["execution_state"] == "starting":
                            nb_deleted += 1
                        else:
                            await self.delete_session(notebook)
                            nb_deleted += 1
                if num_sessions == nb_deleted and (not self.keep_terminals or num_terminals == 0):
                    await self.delete_application(app_name)