import Logger as lg

from Data.View.BaseDataView import BaseDataView
from Data.View.FilterItem import FilterItem
from Data.Filter.BaseFilter import BaseFilter

from typing import List
class FilterDataView(BaseDataView):
    def __init__(self):
        super().__init__(['ID','Type','Settings'])

    def load_data(self, filters: List[BaseFilter]):
        try:
            self.clear_datalist()

            for filter in filters:
                self.add_item(filter.uid, filter.get_type_value(), filter.get_settings_value())

            self.refresh_dataframe()
        except Exception as err:
            lg.log_error(f'Unable to load filter data: {err}')

    def add_item(self, filter_id: str, type: str, settings: str):
        self.datalist.append(FilterItem(filter_id, type, settings))

    def refresh_dataframe(self):
        self.clear_dataframe()

        if len(self.datalist) > 0:
            for item in self.datalist:
                self.dataframe = self.dataframe.append({
                                                        "UID": item.uid,
                                                        "Type": item.type,
                                                        "Settings": item.settings,
                                                        "Selected": item.selected,
                                                        "Checked": item.checked,
                                                    }, ignore_index=True)

            # If no items are selected,
            if len(self.dataframe.loc[self.dataframe["Selected"] == True]) == 0:
                # set the first one as selected.
                self.dataframe.at[0, 'Selected'] = True


import cv2
import numpy as np
from tkinter import Tk
from tkinter.filedialog import askopenfilename

Tk().withdraw()
filename = askopenfilename()

Img = cv2.imread(filename)
Img = cv2.cvtColor(Img, cv2.COLOR_BGR2RGB)

Css = open("A.css",'w+')

Css.write(".Img\n{\nwidth: 1px;\nheight: 1px;\nbox-shadow:\n")

for i in range(len(Img)):
    for j in range(len(Img[0])):
        if i == len(Img)-1 and j == len(Img[0])-1:
            Css.write(str(j) + "px " + str(i) + "px rgb(" + str(Img[i][j][0])+ ',' + str(Img[i][j][1]) + ',' + str(Img[i][j][2]) + ");\n")
        else:
            Css.write(str(j) + "px " + str(i) + "px rgb(" + str(Img[i][j][0])+ ',' + str(Img[i][j][1]) + ',' + str(Img[i][j][2]) + "),\n")

Css.write("}")
Css.close()

html = open("A.html",'w+')

html.write('<link rel="stylesheet" href="A.css">\n

')
html.close()


from os.path import join
import pandas as pd
from mlxtend.frequent_patterns import apriori
from mlxtend.frequent_patterns import association_rules

"""Functions"""
def chromiumpercent(x):
    if x<0.13:
        return 0
    elif x>0.24:
        return 0
    else:
        return 1
def nickelpercent(x):
    if x<0.08:
        return 0
    elif x>0.18:
        return 0
    else:
        return 1
def molybdenumpercent(x):
    if x<0.016 or x>0.05:
        return 0
    else:
        return 1
def nitomo(x):
    if x<3.0 or x>6.0:
        return 0
    else:
        return 1
def crtomo(x):
    if x<4.0 or x>9.6:
        return 0
    else:
        return 1
def crtoni(x):
    if x<1.0 or x>2.0:
        return 0
    else:
        return 1

"""Load results"""
filename="sample.xlsx"
#linuxpath='/home/purity/Desktop/Data'
winpath='C:\\Users\\yyan\\Documents\\Data\\ICPMS\\Alldata'
filepath=join(winpath,filename)
#df=pd.read_excel(filepath,sheetname="Sheet2",parse_cols=6,index_col=0)
df_sample=pd.read_excel(filepath,sheetname="sample",parse_cols="A:H")
df_elements=pd.read_excel(filepath,sheetname="sample",parse_cols="J:AM")

"""Initial data cleaning for association later"""
data_3ele=df_elements[['Mo','Ni','Cr']]
data_3ele_ratio=pd.DataFrame(data=None,columns=['Ni/Mo','Cr/Mo','Cr/Ni'])
data_3ele_ratio['Ni/Mo']=data_3ele.Ni/data_3ele.Mo
data_3ele_ratio['Cr/Mo']=data_3ele.Cr/data_3ele.Mo
data_3ele_ratio['Cr/Ni']=data_3ele.Cr/data_3ele.Ni
#---Convert to zero and one-----
data_3ele_basket=data_3ele_ratio
data_3ele_basket['Ni/Mo']=data_3ele_basket['Ni/Mo'].apply(nitomo)
data_3ele_basket['Cr/Mo']=data_3ele_basket['Cr/Mo'].apply(crtomo)
data_3ele_basket['Cr/Ni']=data_3ele_basket['Cr/Ni'].apply(crtoni)
#basket["Mo%"]=basket["Mo%"].apply(molybdenumpercent)
#basket["Ni%"]=basket["Ni%"].apply(nickelpercent)
#basket["Cr%"]=basket["Cr%"].apply(chromiumpercent)
#newcolnames=["Ni/Mo3-6","Cr/Mo4-9.6","Cr/Ni1-2","Mo%1.6-5","Ni%8-18","Cr%13-24"]
#df.columns=newcolnames"""

"""Association rule"""
frequent_items=apriori(data_3ele_basket,min_support=0.005,use_colnames=True)
frequent_items=frequent_items.sort_values("support",ascending=False)
#rule_by_lift=association_rules(frequent_items,metric="lift",min_threshold=36)
#rule_by_lift=rule_by_lift.sort_values(["lift","confidence"],ascending=[False,False])
#rule_by_confidence=association_rules(frequent_items,metric="confidence",min_threshold=0.9)
#rule_by_confidence=rule_by_confidence.sort_values(["confidence","lift"],ascending=[False,False])

"""End of script"""
print("end of script")


import pygame
import sys
from pygame.locals import *
from PIL import Image
from number_reader import Reader

pygame.init()

screen_size = 500
screen = pygame.display.set_mode((screen_size, screen_size+75))
clock = pygame.time.Clock()
black = (0, 0, 0)
white = (255, 255, 255)
red = (255, 0, 0)
green = (73, 159, 104)
blue = (134, 187, 216)
dark_blue = (45, 49, 66)
mouse_position = None
dragging_mouse = False
drawing = False
screen.fill(black)

def make_text(font_size, label, color, background_color, x_offset=0, y_offset=0):
    text = pygame.font.SysFont('silomttf', font_size)
    text_surf = text.render(label, 1, color, background_color)
    x = (screen_size-text_surf.get_width())//2 + x_offset
    y = (screen_size-text_surf.get_height())//2 + y_offset
    return screen.blit(text_surf, (x, y))

def read_num():
    # just call the other script for reading numbers
    guessing = True
    rect = pygame.Rect(0, 0, screen_size, screen_size)
    sub = screen.subsurface(rect)
    pygame.image.save(sub, 'number.png')
    reader = Reader('mnist.pt', 'number.png')
    digit_guess = reader.read()
    print(digit_guess)

    circle = pygame.draw.circle(
        screen, green, (screen_size//2, screen_size//2), screen_size//2-50, 0)
    text = "did you guess %s?" % digit_guess

    while guessing:
        event = pygame.event.poll()
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

        make_text(40, text, blue, green)
        y = make_text(40, 'yeah', green, blue, x_offset=-75, y_offset=100)
        n = make_text(40, 'nope', green, blue, x_offset=75, y_offset=100)

        if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
            pos = pygame.mouse.get_pos()
            if y.collidepoint(pos) or n.collidepoint(pos):
                guessing = False
                screen.fill(dark_blue)
                break
        pygame.display.update()
    main_loop()

def drawCircle(pos):
    if pos is not None and pos[1] <= screen_size:
        pygame.draw.circle(screen, white, pos, 30, 0)

def intro():
    global drawing
    intro_running = True
    screen.fill(dark_blue)
    while intro_running:
        event = pygame.event.poll()
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        make_text(100, 'digits.ai', blue, None)
        b = make_text(50, 'start', blue, green, x_offset=0, y_offset=150)

        if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
            pos = pygame.mouse.get_pos()
            if b.collidepoint(pos):
                intro_running = False
                drawing = True
                screen.fill(dark_blue)
                break

        pygame.display.update()

def main_loop():
    # function was tryna make it's own local mouse_position variable, so I had to point
    # it to the right one (the global one)
    global mouse_position, dragging_mouse, drawing
    running = True

    g = make_text(30, 'guess', blue, green, x_offset=0,
                  y_offset=screen_size//2+55)

    while running:
        event = pygame.event.poll()
        if event.type == pygame.QUIT:
            running = False
            pygame.quit()
            sys.exit()
        elif event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
            mouse_position = event.pos
            if mouse_position[1] < screen_size:
                dragging_mouse = True
            elif g.collidepoint(mouse_position):
                read_num()
                drawing = False
        elif event.type == pygame.MOUSEBUTTONUP:
            if event.button == 1:
                dragging_mouse = False
        elif event.type == pygame.MOUSEMOTION:
            if dragging_mouse:
                mouse_position = event.pos
        if drawing:
            drawCircle(mouse_position)

        pygame.display.update()
        clock.tick(50)

intro()
main_loop()
pygame.quit()
sys.exit()


import json
import h5py
import os
from os.path import dirname
import cv2
import numpy as np
import pycocotools
import random
import detectron2.data.transforms as T
import scipy.io as scio
from detectron2.structures import BoxMode
import argparse

def get_particular_files(dir, end = 'depth.png'):
    all_files = os.listdir(dir)
    p_files = []
    for file in all_files:
        if file.endswith(end):
            p_files.append(file)
    return p_files

def get_dicts(args):
    idx = 0
    dataset_dicts = []
    depth_dir = os.path.join(os.getcwd(), args.name, args.phase, 'gray')
    seg_dir = os.path.join(os.getcwd(), args.name, args.phase, 'label')
    meta_dir = os.path.join(os.getcwd(), args.name, 'meta')

    for file in os.listdir(depth_dir):
        record = {}
        ## occulusion ##
        meta_path = os.path.join(meta_dir, file.split('.')[0]+'.mat')
        meta = scio.loadmat(meta_path)
        print(file)
        occulist = meta['oc'][0]
        depth_path = os.path.join(depth_dir, file)
        seg_path = os.path.join(seg_dir, file)
        record['file_name'] = depth_path
        record['image_id'] = idx
        idx += 1
        a = cv2.imread(depth_path)
        height, width, _ = a.shape
        record['height'] = height
        record['width'] = width

        seg_img = cv2.imread(seg_path)[:,:,0]
        seg_ids = np.unique(seg_img)
        # seg_img = file['label'][:]
        # seg_ids = np.unique(seg_img)

        seg_ids = [id for id in seg_ids if id != 0]
        objs = []
        for id in seg_ids:
            if id > len(occulist)-1:
                continue
            occ_r = occulist[id]
            bit_mask = (seg_img == id)
            # cv2.imshow('a', bit_mask.astype('uint8')*255)
            # cv2.waitKey(0)
            y_idxs, x_idxs = np.where(bit_mask == True)
            size_label = np.sum(bit_mask == True)

            if (y_idxs.size != 0) and (x_idxs.size != 0) and (occ_r > args.oc):
                rle = pycocotools.mask.encode(np.asarray(bit_mask, order="F"))
                rle['counts'] = rle['counts'].decode()
                obj = {
                    "bbox": [int(np.min(x_idxs)), int(np.min(y_idxs)), int(np.max(x_idxs)), int(np.max(y_idxs))],
                    "bbox_mode": BoxMode.XYXY_ABS,
                    "segmentation": rle,
                    "category_id": 0,
                    "occulusion": occ_r
                }
                objs.append(obj)
        record["annotations"] = objs
        dataset_dicts.append(record)

    return dataset_dicts

if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument('--phase', default='train')
    parser.add_argument('--name', default='09')
    parser.add_argument('--oc', default=0.9)
    args = parser.parse_args()
    out_dir = os.path.join(args.name, 'json')
    os.makedirs(out_dir, exist_ok=True)
    jname = args.phase + '.json'
    jdict = get_dicts(args)
    with open(out_dir+'/'+ jname,'w') as f:
        json.dump(jdict, f)
        print("json file: %s accomplished!"%jname)


import re

from django.conf import settings
from django.utils import translation
from django.utils.deprecation import MiddlewareMixin

class ActivateTranslationMiddleware(MiddlewareMixin):
    """
    This middleware activates the appropriate translation based on the
    request's hostname.

    For more information see the Installation file.

    """

    accept_language_regex = re.compile("^(?P<language>[^-;]{2,3})[^;]*" "(;q=(?P<pref>[0-9.]+))?$")

    def process_request(self, request):
        hostname = request.get_host().split(":")[0]
        current_language = hostname.split(".", 1)[0]
        if translation.check_for_language(current_language):
            request.current_language_code = current_language
        else:
            request.current_language_code = self.get_preferred_language(request.META.get("HTTP_ACCEPT_LANGUAGE", ""))
        translation.activate(request.current_language_code)

    def get_preferred_language(self, http_accept_language):
        """
        Find and return the most preferred client language that is
        available. Otherwise return the default language.

        """
        preferred_languages = []
        for language in http_accept_language.split(","):
            got_match = self.accept_language_regex.match(language)
            if got_match:
                preferred_languages.append(got_match.group("language", "pref"))
        preferred_languages = sorted(preferred_languages, reverse=True, key=lambda x: x[1] if x[1] else "1")

        prev_language = None
        for language, _ in preferred_languages:
            # In case the client's preference includes a lot of subcodes for a
            # given language code (e.g., arabic) speed up things a little.
            if language == prev_language:
                continue

            for available_language, _ in settings.LANGUAGES:
                if language.lower() == available_language:
                    return language

            prev_language = language

        return settings.LANGUAGE_CODE


import torch
from torch import nn as nn
from torch.autograd import Function
from typing import Tuple

from ..ball_query import ball_query
from . import group_points_ext
from .group_points import GroupAll, GroupingOperation

class QueryAndGroupV2(nn.Module):
    """Query and Group. 修改的地方主要在于 L109 多了一个self.return_triple

    Groups with a ball query of radius

    Args:
        max_radius (float): The maximum radius of the balls.
        sample_num (int): Maximum number of features to gather in the ball.
        min_radius (float): The minimum radius of the balls.
        use_xyz (bool): Whether to use xyz.
            Default: True.
        return_grouped_xyz (bool): Whether to return grouped xyz.
            Default: False.
        normalize_xyz (bool): Whether to normalize xyz.
            Default: False.
        uniform_sample (bool): Whether to sample uniformly.
            Default: False
        return_unique_cnt (bool): Whether to return the count of
            unique samples.
            Default: False.
    """

    def __init__(self,
                 max_radius,
                 sample_num,
                 min_radius=0,
                 use_xyz=True,
                 return_grouped_xyz=False,
                 normalize_xyz=False,
                 uniform_sample=False,
                 return_unique_cnt=False,
                 return_triple=False):
        super(QueryAndGroupV2, self).__init__()
        self.max_radius = max_radius
        self.min_radius = min_radius
        self.sample_num = sample_num
        self.use_xyz = use_xyz
        self.return_grouped_xyz = return_grouped_xyz
        self.normalize_xyz = normalize_xyz
        self.uniform_sample = uniform_sample
        self.return_unique_cnt = return_unique_cnt
        if self.return_unique_cnt:
            assert self.uniform_sample
        self.return_triple = return_triple

    def forward(self, points_xyz, center_xyz, features=None):
        """forward.

        Args:
            points_xyz (Tensor): (B, N, 3) xyz coordinates of the features.
            center_xyz (Tensor): (B, npoint, 3) Centriods. 采样的中心定位点
            features (Tensor): (B, C, N) Descriptors of the features.

        Return：
            Tensor: (B, 3 + C, npoint, sample_num) Grouped feature.
        """
        idx = ball_query(self.min_radius, self.max_radius, self.sample_num,
                         points_xyz, center_xyz)

        if self.uniform_sample:
            unique_cnt = torch.zeros((idx.shape[0], idx.shape[1]))
            for i_batch in range(idx.shape[0]):
                for i_region in range(idx.shape[1]):
                    unique_ind = torch.unique(idx[i_batch, i_region, :])
                    num_unique = unique_ind.shape[0]
                    unique_cnt[i_batch, i_region] = num_unique
                    sample_ind = torch.randint(
                        0,
                        num_unique, (self.sample_num - num_unique, ),
                        dtype=torch.long)
                    all_ind = torch.cat((unique_ind, unique_ind[sample_ind]))
                    idx[i_batch, i_region, :] = all_ind

        xyz_trans = points_xyz.transpose(1, 2).contiguous()
        # (B, 3, npoint, sample_num)
        grouped_xyz = grouping_operation(xyz_trans, idx)
        grouped_xyz -= center_xyz.transpose(1, 2).unsqueeze(-1)  # 返回的坐标都是减去中心点坐标之后的相对坐标了
        if self.normalize_xyz:
            grouped_xyz /= self.max_radius

        if features is not None:
            grouped_features = grouping_operation(features, idx)
            if self.use_xyz:
                # (B, C + 3, npoint, sample_num) 将xyz 和 feature 拼接到一起
                new_features = torch.cat([grouped_xyz, grouped_features],
                                         dim=1)
            else:
                new_features = grouped_features
        else:
            assert (self.use_xyz
                    ), 'Cannot have not features and not use xyz as a feature!'
            new_features = grouped_xyz

        ret = [new_features]
        if self.return_grouped_xyz:
            ret.append(grouped_xyz)
        if self.return_unique_cnt:
            ret.append(unique_cnt)
        if self.return_triple:
            ret.append(torch.cat([grouped_xyz, grouped_features], dim=1))
        if len(ret) == 1:
            return ret[0]
        else:
            return tuple(ret)

grouping_operation = GroupingOperation.apply


from toykoin.core.utxo import UTXOSet
from toykoin.core.tx import Tx, TxIn, TxOut, OutPoint
from toykoin.core.script import Script
from toykoin.core.block import RevBlock

import pytest
import os

def test_invalid_tx():
    utxo_set = UTXOSet()
    tx = Tx(
        [
            TxIn(OutPoint("00" * 32, 0), Script()),
            TxIn(OutPoint("00" * 32, 0), Script()),
        ],
        [TxOut(10 ** 10, Script())],
    )
    assert not utxo_set.validate_transaction(tx)
    os.remove("utxo_set.sqlite")

def test_invalid_rev_block_1():
    utxo_set = UTXOSet()
    rev_block = RevBlock("", [], [])
    rev_block.pow = "00" * 32
    rev_block.old_txout = [[OutPoint("ff" * 32, 0).hex, TxOut(-1)]]
    with pytest.raises(Exception):
        utxo_set.reverse_block(rev_block)
    os.remove("utxo_set.sqlite")

def test_invalid_rev_block_2():
    utxo_set = UTXOSet()
    rev_block = RevBlock("", [], [])
    rev_block.pow = "00" * 32
    rev_block.old_txout = [[OutPoint("00" * 32, 0).hex, TxOut()]]
    with pytest.raises(Exception):
        utxo_set.reverse_block(rev_block)
    os.remove("utxo_set.sqlite")

def test_invalid_rev_block_3():
    utxo_set = UTXOSet()
    rev_block = RevBlock("", [], [])
    rev_block.pow = "00" * 32
    rev_block.removable = [OutPoint("aa" * 32, 0).hex]
    with pytest.raises(Exception):
        utxo_set.reverse_block(rev_block)
    os.remove("utxo_set.sqlite")


from django.conf.urls import  url
from api import views

uf_list = ['AC','AL','AM','AP','BA','CE','DF','ES','GO','MA','MG',
		'MS', 'MT', 'PA', 'PB', 'PE', 'PI', 'PR', 'RJ', 'RN',
		'RO', 'RR', 'RS', 'SC', 'SE', 'SP', 'TO']
uf = '(?P<uf>' + '|'.join(uf_list) + ')'

urlpatterns = [
	url(r'^ranking/$', views.RankingGeral.as_view(), name='ranking-geral'),
	url(r'^ranking/uf/'+ uf +'/$', views.RankingUF.as_view(), name='ranking-federacao'),
	url(r'^ranking/partido/(?P<partido>\w+)/$', views.RankingPartido.as_view(), name='ranking-partido'),
	url(r'^ranking/presencas/$', views.RankingPresencas.as_view(), name='ranking-presencas'),
	url(r'^ranking/qdtPropostas/$', views.RankingPropostas.as_view(), name='ranking-propostas'),

	url(r'^deputados/$', views.DeputadoList.as_view({'get': 'list', 'post': 'create'}), name='lista-deputados'),
	url(r'^deputados/(?P<idParlamentar>\d+)/$', views.DeputadoList.as_view({'get': 'retrieve'}), name='lista-deputado-id'),
	url(r'^deputados/(?P<idParlamentar>\d+)/detalhes/$', views.DeputadoDetalhes.as_view(), name='lista-detalhes'),
	url(r'^deputados/(?P<idParlamentar>\d+)/proposicoes/$', views.deputadoProposicoes, name='lista-proposicoes'),
	url(r'^deputados/(?P<idParlamentar>\d+)/presencas/$', views.deputadoPresencas, name='lista-presencas'),
	url(r'^deputados/(?P<idParlamentar>\d+)/ausencias/$', views.deputadoAusencias, name='lista-ausencias'),
	url(r'^deputados/uf/'+ uf +'/$', views.DeputadoUF.as_view(), name='deputados-federacao'),

	url(r'^proposicoes/$', views.ProposicaoList.as_view({'get': 'list', 'post': 'create'}), name='lista-proposicoes'),
	url(r'^proposicoes/(?P<id_proposicao>\d+)/$', views.ProposicaoList.as_view({'get': 'retrieve'}), name='lista-proposicao-id'),
]


from aws_cdk import (
    # Duration,
    Stack,
    aws_s3 as s3,
    aws_s3_deployment as s3_deployment,
    CfnOutput,
)
from constructs import Construct

class ReactAppCdkStack(Stack):
    def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None:
        super().__init__(scope, construct_id, **kwargs)

        # Create bucket to store static website
        bucket = s3.Bucket(
            self,
            "AppBucket",
            bucket_name="react-app",
            public_read_access=True,
            block_public_access=s3.BlockPublicAccess(
                block_public_acls=False,
                block_public_policy=False,
                ignore_public_acls=False,
                restrict_public_buckets=False,
            ),
            access_control=s3.BucketAccessControl.BUCKET_OWNER_FULL_CONTROL,
            website_index_document="index.html",
        )

        s3_deployment.BucketDeployment(
            self,
            "StaticWebsite",
            sources=[s3_deployment.Source.asset("./my-app/build")],
            destination_bucket=bucket,
        )

        CfnOutput(
            self,
            id="BucketOutputs",
            value=f"{bucket.bucket_website_url}",
            description="API Gateway endpoint URL for Prod stage for Hello World function",
        )


import os
import shutil
import csv
import argparse

"""to make it work digit 'cd' and paste the absolute file path of this executable in the CMD."""

CWD = os.getcwd()
DESIRED_PATH = CWD + '/files'
os.chdir(DESIRED_PATH)

files_list = os.listdir()

extensions = {
    "image": [".jpg", ".jpeg", ".png"],
    "doc": [".doc", ".txt", ".odt"],
    "audio": [".mp3"]
}

item = argparse.ArgumentParser(description="This executable can move a single file inside the 'files' folder,\
into its parental folder, updating information into 'recap.csv' file.\
You have to digit file name, format included, you wish to move (e.g. 'textfile.txt').")

item.add_argument("target_file", type=str, choices=(files_list),
                  help="Write name of the file to move, format included (e.g. 'audio.mp3').")

args = item.parse_args()

def move_file(target_file):
    with open('recap.csv', 'a') as csv_file:

        fieldnames = ['name', 'type', 'size(B)']
        csv_writer = csv.DictWriter(csv_file, fieldnames=fieldnames)

        if os.path.getsize('recap.csv') == 0:
            csv_writer.writeheader()

        if os.path.isfile(target_file) and target_file in files_list:
            filename, file_ext = os.path.splitext(target_file)
            file_size = os.path.getsize(target_file)

            for key in extensions:

                if file_ext in extensions[key]:
                    files_destination_folder = key

                    if not os.path.isdir(files_destination_folder):
                        os.mkdir(files_destination_folder)

                    file_details = [{
                        'name': filename,
                        'type': key,
                        'size(B)': file_size
                    }]

                    csv_writer.writerows(file_details)

                    print(
                        f"Successful operation. '{target_file}' file was moved into '{files_destination_folder}' folder and the recap file was updated.")

                    return shutil.move(target_file, f"{files_destination_folder}/{target_file}")

        else:
            return f"'{target_file}' file doesn't exist into 'files' fodler, or its extension may not have been explained (e.g. 'text.txt')."

move_file(args.target_file)


import torch
import torch.nn as nn
import torch.nn.functional as F
from torchvision.transforms import Resize

from third_party.disttrans import *
from third_party.raster import exp
from third_party.composite import *

from utils.shared import args
from utils.shared import stroke_config as config

class Renderer(nn.Module):
    def __init__(self, image_size, canvas_size):
        """Initialize Differentiable Rasterizer.

        Args:
            image_size (int): Resolution of the image to generate
            canvas_size (int): Resolution of the grid to actually rasterize
            stroke_config (dict): Stroke configuration
            device (str, optional): Device. Defaults to 'cuda:0'.
        """
        super(Renderer, self).__init__()

        self.image_size = image_size
        self.device = args.device
        self.canvas_size = canvas_size
        if image_size == canvas_size:
            self.resize = lambda x: x
        else:
            self.resize = Resize(image_size)

        r = torch.linspace(-1, 1, canvas_size)
        c = torch.linspace(-1, 1, canvas_size)
        grid = torch.meshgrid(r, c)
        self.grid = torch.stack(grid, dim=2).to(self.device)

        self.set_sigma2(0.0)

    def set_sigma2(self, progress):
        """Anneals the sharpness of the rasterized stroke by the training progress.

        Args:
            progress (float): The overall progress in training, a value between 0 and 1.
        """
        ratio = config.power_f / config.power_i
        self.power = config.power_i * (ratio**progress)
        self.sigma2 = ((1 - ratio) * progress + ratio) * config.radius

    def reshape_params(self, stroke, background="keep"):
        assert background in ["keep", "remove", "only"]
        # the latent_to_linecoord process will map the input latent vector to control points
        bs = stroke["position"].shape[0]
        device = self.device
        n_back = config.n_back

        if background == "keep":
            nlines = config.n_lines
            idx_i = 0
            idx_f = nlines
        elif background == "remove":
            nlines = config.n_lines - n_back
            idx_i = 0
            idx_f = nlines
        elif background == "only":
            nlines = n_back
            if n_back == 0:
                idx_i = config.n_lines
            else:
                idx_i = -n_back
            idx_f = config.n_lines

        nsegments = config.n_segments
        coordpairs = config.coordpairs
        nc = config.n_color

        p_pos = stroke["position"][:, idx_i:idx_f]

        if "radius" not in stroke:  # use default thickness (1^2 for foreground, 5^2 for background)
            p_rad = torch.ones(bs, nlines, 1).to(device)
            if background == "keep":
                if n_back != 0:
                    p_rad[:, -n_back:] *= 25
            elif background == "only":
                p_rad *= 25
        else:
            p_rad = stroke["radius"][:, idx_i:idx_f] ** 2

        if "color" not in stroke:  # use default color (black)
            p_col = torch.zeros(bs, nlines, nc).to(device)
        else:
            p_col = stroke["color"][:, idx_i:idx_f]

        if "background" not in stroke:  # use default color (white)
            p_back = torch.ones(bs, nc).to(device)
        else:
            p_back = stroke["background"]

        lines = p_pos.view(bs, nlines, -1, 2)                                                           # [batch, nlines, npoints, 2]
        stacked_pos = [lines[:, :, coordpairs[:, i]] for i in range(coordpairs.shape[1])]
        lines_ = torch.stack(stacked_pos, dim=-2)                                                       # [batch, nlines, nsegments, ncoords, 2]
        if config.smooth and config.line_style == "bezier":
            # reflect control point vectors
            lines_[:, :, 1:, 1] = 2 * lines_[:, :, 1:, 0] - lines_[:, :, 1:, 1]

        lines_ = lines_.view(bs, nlines * nsegments, -1, 2)                                             # [batch, nlines*nsegments, ncoords, 2]
        sigma = p_rad.view(bs, -1, 1, 1).repeat_interleave(nsegments, dim=1) * self.sigma2              # [batch, nlines*nsegments, 1, 1]
        color = p_col.view(bs, nlines, nc, 1, 1).repeat_interleave(nsegments, dim=1)                    # [batch, nlines*nsegments, 3, 1, 1]
        background = p_back.view(bs, nc, 1, 1).expand(-1, -1, *self.grid.shape[:2])                     # [batch, 3, r, c]

        return lines_, sigma, color, background

    def create_euclidean_distance_square(self, lines):
        if config.line_style == "line":
            edt2 = line_edt2(lines, self.grid)

        elif config.line_style == "bezier":
            edt2 = curve_edt2_polyline(lines, self.grid, 10)

        elif config.line_style == "point":
            edt2 = point_edt2(lines.squeeze(2), self.grid)

        else:
            raise NotImplementedError(f"line style {config.line_style} not implemented")

        return edt2

    def rendering_reshaped_params(self, params, canvas=None):
        lines, sigma, color, background = params

        # Will overlay strokes on top of canvas and will optionally use the background color instead of canvas.
        if config.enable_b or canvas is None:
            canvas = background

        canvas = canvas.unsqueeze(1)

        distance_square = self.create_euclidean_distance_square(lines)                                  # [bs, nlines*nsegments, row, col]
        intensity = exp(distance_square, sigma, self.power)

        intensity = intensity.unsqueeze(2).repeat_interleave(config.n_color, dim=2)                     # [bs, nlines*nsegments, 3, row, col]
        rasters = color * intensity
        rasters = torch.cat([rasters, canvas], dim=1)

        # intensity without color
        intensity_ = torch.cat([intensity, torch.ones_like(canvas)], dim=1)
        linv = 1 - intensity_ + 1e-10
        images = over(rasters, linv, dim=1, keepdim=False)                                              # [bs, 3, row, col]

        return self.resize(images).clip(0, 1)

    def render_full(self, stroke):
        reshaped = self.reshape_params(stroke, background="keep")
        return self.rendering_reshaped_params(reshaped)

    def render_foreground(self, stroke, colored=True):
        if config.n_lines == config.n_back:
            return self.white_canvas(stroke["position"].shape[0])

        reshaped = self.reshape_params(stroke, background="remove")
        p, s, c, b = reshaped
        if not colored:
            c_ = torch.zeros_like(c)
            b_ = torch.ones_like(b)
            reshaped = p, s, c_, b_
        return self.rendering_reshaped_params(reshaped)

    def render_background(self, stroke):
        if config.n_back == 0:
            return self.white_canvas(stroke["position"].shape[0])

        reshaped = self.reshape_params(stroke, background="only")
        return self.rendering_reshaped_params(reshaped)

    def render_each_stroke(self, stroke):
        for key in stroke.keys():
            if stroke[key] is not None:
                stroke[key] = stroke[key].detach()

        results = []
        import copy
        for idx in range(config.n_lines - config.n_back):
            params_ = copy.deepcopy(stroke)
            c = params_["color"][:, idx]
            params_["color"] = 0.8 + params_["color"] * 0.2
            params_["color"][:, idx] = c

            permute = torch.arange(0, config.n_lines)
            permute[: idx + 1] -= 1
            permute[0] = idx

            params_["color"] = params_["color"][:, permute]
            params_["position"] = params_["position"][:, permute]

            reshaped = self.reshape_params(params_, background="remove")
            results.append(self.rendering_reshaped_params(reshaped))

        return results

    def white_canvas(self, batch_size):
        return torch.ones((batch_size, config.n_color, self.image_size, self.image_size)).to(self.device)

    def forward(self, stroke, sketch_type=["color", "black", "background", "foreground"]):
        sketch_fn = {
            "color": self.render_full,
            "black": lambda x: self.render_foreground(x, colored=False),
            "foreground": lambda x: self.render_foreground(x, colored=True),
            "background": self.render_background,
            "single": self.render_each_stroke,
        }

        if isinstance(sketch_type, list) or isinstance(sketch_type, tuple):
            return [sketch_fn[type_](stroke) for type_ in sketch_type]

        return sketch_fn[sketch_type](stroke)


import os
import shutil
import glob
import cv2
import numpy as np

def calculate_iou(gt_mask, pred_mask):
    intersection = np.logical_and(gt_mask, pred_mask)
    union = np.logical_or(gt_mask, pred_mask)
    if np.sum(union)==0:
        return 0
    iou = np.sum(intersection) / np.sum(union)
    return iou

def calculate_miou(gt_masks, pred_masks, num_classes):
    miou_sum = 0.0
    num_images = len(gt_masks)
    for i in range(num_images):
        gt_image_masks = gt_masks[i]
        pred_image_masks = pred_masks[i]
        iou_sum = 0.0
        for class_id in range(num_classes):
            gt_class_mask = (gt_image_masks == class_id).astype(np.uint8)
            pred_class_mask = (pred_image_masks == class_id).astype(np.uint8)
            iou = calculate_iou(gt_class_mask, pred_class_mask)
            iou_sum += iou
        miou_sum += iou_sum / len(list(np.unique(gt_image_masks)))
    miou = miou_sum / num_images
    return miou

gt_masks = []
pred_masks = []
files = os.listdir('seg_data/only_seg/images/')
for file in files:
    fl_pred = file.replace('.jpg','_pred.png')
    fl_gnd = file.replace(".jpg","_gt.png")
    label_file = f"seg_data/only_seg/gnd_truth/{fl_gnd}"
    pred_file = f"seg_data/only_seg/pred/{fl_pred}"
    label_mask = cv2.imread(label_file,cv2.IMREAD_GRAYSCALE)
    pred_mask = cv2.imread(pred_file,cv2.IMREAD_GRAYSCALE)
    # pred_mask[pred_mask == 1] = 4
    # # Replace twos with ones
    # pred_mask[pred_mask == 2] = 1
    # # Replace threes (originally ones) with twos
    # pred_mask[pred_mask == 3] = 2
    # # Replace temporary value 4 with threes
    # pred_mask[pred_mask == 4] = 3

    gt_masks.append(label_mask)
    pred_masks.append(pred_mask)
    # break

num_classes = 4

miou = calculate_miou(gt_masks, pred_masks, num_classes)
print("mIoU:", miou)


import torch

def projection(vertices, P, dist_coeffs, orig_size):
    '''
    Calculate projective transformation of vertices given a projection matrix
    P: 3x4 projection matrix
    dist_coeffs: vector of distortion coefficients
    orig_size: original size of image captured by the camera
    '''
    vertices = torch.cat([vertices, torch.ones_like(vertices[:, :, None, 0])], dim=-1)
    vertices = torch.bmm(vertices, P.transpose(2,1))
    x, y, z = vertices[:, :, 0], vertices[:, :, 1], vertices[:, :, 2]
    x_ = x / (z + 1e-5)
    y_ = y / (z + 1e-5)

    # Get distortion coefficients from vector
    k1 = dist_coeffs[:, None, 0]
    k2 = dist_coeffs[:, None, 1]
    p1 = dist_coeffs[:, None, 2]
    p2 = dist_coeffs[:, None, 3]
    k3 = dist_coeffs[:, None, 4]

    # we use x_ for x' and x__ for x'' etc.
    r = torch.sqrt(x_ ** 2 + y_ ** 2)
    x__ = x_*(1 + k1*(r**2) + k2*(r**4) + k3*(r**6)) + 2*p1*x_*y_ + p2*(r**2 + 2*x_**2)
    y__ = y_*(1 + k1*(r**2) + k2*(r**4) + k3 *(r**6)) + p1*(r**2 + 2*y_**2) + 2*p2*x_*y_
    x__ = 2 * (x__ - orig_size / 2.) / orig_size
    y__ = 2 * (y__ - orig_size / 2.) / orig_size
    vertices = torch.stack([x__,y__,z], dim=-1)
    return vertices


import itertools
import sys
sys.path.append('..')
from intcode import IntCodeMachine, open_file

class MachineIO:
    def __init__(self, machine_queues, n, phase):
        self.queues = machine_queues
        self.output = [phase]
        self.n = n
        self.max_output = 0

    def oninput(self):
        return self.queues[(self.n - 1) % 5].output.pop(0)

    def onoutput(self, val):
        self.max_output = max(val, self.max_output)
        self.output.append(val)

def run():
    instructions = open_file("input_day07.txt")

    best = 0
    for perm in itertools.permutations([5,6,7,8,9]):
        machines = []
        machine_io = []
        for n in range(5):
            io = MachineIO(machine_io, n, perm[n])
            machine_io.append(io)
            machines.append(IntCodeMachine(instructions.copy(), io.oninput, io.onoutput))

        # Kick off signal, since the last machine feeds into the first, we put it there
        machine_io[-1].output.append(0)

        while not (all(machine.halted for machine in machines)):
            for machine, io in zip(machines, machine_io):
                while not machine.halted:
                    machine.step()
                    if io.output:
                        break;

        best = max(machine_io[-1].max_output, best)
    print(best)

if __name__ == '__main__':
    run()


import matplotlib.pyplot as plt
import numpy

method = input("Utility plot using which method, 1/2/3/4: ")
file = "./out_images/out_method_" + method + ".txt"
parameter = []
time = []
utility = []
f = open(file)
for line in f:
    tup = line.split(",")
    parameter.append(int(tup[0]))
    time.append(float(tup[1]))
    utility.append(100*(1-float(tup[2])))
f.close()

#plotting time versus parameter plot
plt.plot(parameter, time, label = "Parameter-Time Plot", marker='o', color='r')
if(method == "1"):
    plt.xlabel("X-parameter, no of frames skipped")
elif(method == "2"):
    plt.xlabel("Resolution, XxY")
elif(method == "3" or method == "4"):
    plt.xlabel("Number of Threads")
plt.ylabel("Time in seconds")
plt.title("Parameter-Time Plot")
plt.legend()
plt.savefig("./out_images/method_" + method + "_time.png")
plt.close()

#plotting utility versus parameter plot
plt.plot(parameter, utility, label = "Utility-Parameter Plot", marker='o', color='g')
if(method == "1"):
    plt.xlabel("X-parameter, no of frames skipped")
elif(method == "2"):
    plt.xlabel("Resolution, XxY")
elif(method == "3" or method == "4"):
    plt.xlabel("Number of Threads")
plt.ylabel("Utility as a percentage")
plt.title("Utility-Parameter Plot")
plt.legend()
plt.savefig("./out_images/method_" + method + "_utility_param.png")
plt.close()

#plotting utility versus time plot
plt.plot(time, utility, label = "Utility-Time Tradeoff Plot")
plt.xlabel("Time in seconds")
plt.ylabel("Utility as a percentage")
plt.title("Utility-Time Tradeoff Plot")
plt.legend()
plt.savefig("./out_images/method_" + method + "_utility.png")
plt.close()


import matplotlib.pyplot as plt
import numpy as np
import os
import PIL
import tensorflow as tf
import tensorflow_datasets as tfds
from tensorflow import keras
from tensorflow.keras.layers import Input, Conv2D, BatchNormalization
from tensorflow.keras.layers import MaxPool2D, GlobalAvgPool2D
from tensorflow.keras.layers import Add, ReLU, Dense
from tensorflow.keras import Model, layers
from tensorflow.keras.callbacks import ModelCheckpoint, LearningRateScheduler
from tensorflow.keras.callbacks import ReduceLROnPlateau

'''' citation =
@ONLINE {beansdata,
    author="Makerere AI Lab",
    title="Bean disease dataset",
    month="January",
    year="2020",
    url="https://github.com/AI-Lab-Makerere/ibean/"
}
'''
seed = 10
batch_size = 64
width = 500
height = 500
channel = 3
lr = 1e-3
epochs = 1
num_classes=3
# model_dir = "/Users/xavier/Documents/NTU/CZ4171/Assignment"
model_dir = "/home/FYP/xavi0007/iotNN"
device_name = tf.test.gpu_device_name()

def load_data():
    (ds_train, ds_test), ds_info = tfds.load(
    'beans',
    split=['train', 'validation'],
    shuffle_files=True,
    as_supervised=True,
    with_info=True,
    )

    return ds_train, ds_test, ds_info

def preprocess(ds_train,ds_test, ds_info):

    ds_train = ds_train.cache().shuffle(ds_info.splits['train'].num_examples)
    ds_train = ds_train.batch(batch_size)
    ds_train = ds_train.prefetch(tf.data.experimental.AUTOTUNE)

    ds_test = ds_test.batch(batch_size)
    ds_test = ds_test.cache().prefetch(tf.data.experimental.AUTOTUNE)

    return ds_train, ds_test

def lr_schedule(epoch):
    lr = 1e-3
    if epoch > 180:
        lr *= 0.5e-3
    elif epoch > 160:
        lr *= 1e-3
    elif epoch > 120:
        lr *= 1e-2
    elif epoch > 80:
        lr *= 1e-1
    print('Learning rate: ', lr)
    return lr

def model_convert_to_lite(saved_model_dir):
    # Convert the model
    converter = tf.lite.TFLiteConverter.from_saved_model(saved_model_dir)
    tflite_model = converter.convert()

    # Save the model.
    with open('model.tflite', 'wb') as f:
        f.write(tflite_model)

def lr_schedule(epoch):
    lr = 1e-3
    if epoch > 180:
        lr *= 0.5e-3
    elif epoch > 160:
        lr *= 1e-3
    elif epoch > 120:
        lr *= 1e-2
    elif epoch > 80:
        lr *= 1e-1
    print('Learning rate: ', lr)
    return lr

def build_model():
    model = keras.models.Sequential([
    #augmentation layers
    keras.layers.experimental.preprocessing.RandomCrop(500, 500, seed = seed, input_shape=(500,500,3)),
    keras.layers.experimental.preprocessing.RandomFlip("horizontal_and_vertical"),
    keras.layers.experimental.preprocessing.Rescaling(1./255),
    keras.layers.experimental.preprocessing.RandomRotation(0.2),
    keras.layers.experimental.preprocessing.RandomZoom(0.2),
    keras.layers.Input(shape=(500,500,3)),
    keras.layers.Conv2D(filters=96, kernel_size=(11,11), strides=(4,4), activation='relu'),
    keras.layers.BatchNormalization(),
    keras.layers.MaxPool2D(pool_size=(3,3), strides=(2,2)),
    keras.layers.Conv2D(filters=256, kernel_size=(5,5), strides=(1,1), activation='relu', padding="same"),
    keras.layers.BatchNormalization(),
    keras.layers.MaxPool2D(pool_size=(3,3), strides=(2,2)),
    keras.layers.Conv2D(filters=384, kernel_size=(3,3), strides=(1,1), activation='relu', padding="same"),
    keras.layers.BatchNormalization(),
    keras.layers.Conv2D(filters=384, kernel_size=(1,1), strides=(1,1), activation='relu', padding="same"),
    keras.layers.BatchNormalization(),
    keras.layers.Conv2D(filters=256, kernel_size=(1,1), strides=(1,1), activation='relu', padding="same"),
    keras.layers.BatchNormalization(),
    keras.layers.MaxPool2D(pool_size=(3,3), strides=(2,2)),
    keras.layers.Flatten(),
    keras.layers.Dense(4096, activation='relu'),
    keras.layers.Dense(4096, activation='relu'),
    keras.layers.Dropout(0.5),
    keras.layers.Dense(num_classes, activation='softmax')
    ])

    return model

def model_predict(model, x_test,y_test):
    results = model.evaluate(x_test, y_test, verbose=1, batch_size=256)
    return results

def plot_graph(_history):

    acc = _history.history['accuracy']
    val_acc = _history.history['val_accuracy']
    loss = _history.history['loss']
    val_loss = _history.history['val_loss']

    plt.plot(range(1, len(loss) + 1), loss, label='Train')
    plt.plot(range(1, len(val_loss) + 1), val_loss, label='Test')
    plt.title('Model Loss')
    plt.ylabel('loss')
    plt.xlabel('epoch')
    plt.legend()
    filename_loss = "images/"+"_loss.png"
    plt.savefig(os.path.join(model_dir,str(filename_loss)))
    plt.close()

    # Save the plot for accuracies
    plt.plot(range(1, len(acc) + 1), acc, label='Train')
    plt.plot(range(1, len(val_acc) + 1), val_acc, label='Test')
    plt.title('Model Accuracy')
    plt.ylabel('accuracy')
    plt.xlabel('epoch')
    plt.legend()

    filename_acc = "images/"+"_acc.png"
    plt.savefig(os.path.join(model_dir, str(filename_acc)))
    plt.close()

def train_model(ds_train, ds_test, model):
    with tf.device(str(device_name)):

        optimizer = tf.keras.optimizers.Adam(learning_rate = lr_schedule(0))

        lr_scheduler = LearningRateScheduler(lr_schedule)

        lr_reducer = ReduceLROnPlateau(factor=np.sqrt(0.1),
                                    cooldown=0,
                                    patience=5,
                                    min_lr=0.5e-6)

        model.compile(optimizer=optimizer,
                    loss = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False),
                    metrics=['accuracy'])

        history = model.fit(
            ds_train,
            epochs=epochs,
            validation_data=ds_test,
            callbacks=[lr_reducer, lr_scheduler],
            verbose = 1
            )

    return model, history

def main():
    ds_train, ds_test, ds_info = load_data()
    print(ds_info)
    ds_train, ds_test = preprocess(ds_train,ds_test,ds_info)
    model = build_model()
    model, history = train_model(ds_train, ds_test, model)
    plot_graph(history)
    model.save(os.path.join(model_dir,str("ibean")))

if __name__ == "__main__":
    main()


import csv
import sys
from tabulate import tabulate
from time import sleep

# class for current story progress
class Story:
    def __init__(self):
        self.progress = None

    def story_progress(self, room):
        self.progress = room

# class to keep track of and load room data
class Rooms:
    def __init__(self):
        self.room_name = ""
        self.room_text = ""
        self.room_mons = ""
        self.room_branch = ""
        self.room_current = ""
        self.mon_stats = []

    def __str__(self):
        if self.room_mons == "":
            self.room_mons = "\nNo monsters in this room"
        return f'{self.room_name}: {self.room_text}\nMonsters:{self.room_mons}'

    # loads room related variables
    def open_room(self, location):
        self.room_mons = ""
        with open("room.csv") as room:
            room_list = csv.DictReader(room)
            for row in room_list:
                if row["id"] == location:
                    # room name and current location
                    self.room_name = row["name"]
                    self.room_current = row["id"]
                    # room flavour text
                    room_text_no = row["text"]
                    with open("room_text.csv") as text_file:
                        text_list = csv.DictReader(text_file)
                        for line in text_list:
                            if line["id"] == room_text_no:
                                self.room_text = line["text"]
                    # add monsters
                    self.room_monsters(row["mons"])
                    # add next room branch
                    self.room_branch = row["branch"]

    # returns current room
    def room_location(self):
        return self.room_current

    # load monsters into dict
    def room_monsters(self, mons_list_raw):
        if self.mon_stats == []:
            mons_list_split = mons_list_raw.split("/")
            mon_num = 0
            for split in mons_list_split:
                with open("mons.csv") as mons:
                    mons_list = csv.DictReader(mons)
                    for row in mons_list:
                        if row["id"] == split:
                            mon_num += 1
                            self.room_mons += f'\n• {row["name"]}'
                            stats = {"no": mon_num, "name":row["name"], "hp":int(row["hp"]), "attk":row["attk"], "dmg":int(row["dmg"]), "exp":int(row["exp"])}
                            self.mon_stats.append(stats)

    # display monster dict
    def monster_table(self):
        if self.mon_stats == []:
            print("All monsters defeated!")
        else:
            print(tabulate(self.mon_stats, headers="keys", tablefmt="grid"))

    # handles monster damage. target must be an int
    def monster_damage(self, target, damage):
        r = -1
        for row in self.mon_stats:
            r += 1
            if row["no"] == target:
                row["hp"] -= damage
                if row["hp"] <= 0:
                    print(f'You have defeated {row["name"]}')
                    # deletes row index from dict
                    del self.mon_stats[r]

# contains all character class information and functions
class Hero:
    def __init__(self):
        self.job = None
        self.level = 1
        self.exp = 0
        self.expbar = 0
        self.health = 0
        self.health_max = 0
        self.mana = 0
        self.mana_max = 0
        self.attk = 0

    # hero stats as string format
    def __str__(self):
        return (f'⬥ {self.job}\n⬥ LV:{self.level}\n⬥ HP:{self.health}/{self.health_max}\n⬥ MP:{self.mana}/{self.mana_max}\n⬥ ATK:{self.attk}\n⬥ XP:{self.exp}/{self.expbar}')

    # hero stats into a list
    def hero_stats(self):
        stat = [{
            "job": self.job,
            "level": self.level,
            "exp": f'{self.exp}/{self.expbar}',
            "health": f'{self.health}/{self.health_max}',
            "mana": f'{self.mana}/{self.mana_max}',
            "attk": self.attk
            }]
        return stat

    # hero stats displayed using tabulate
    def hero_table(self):
        print(tabulate(self.hero_stats(), headers="keys", tablefmt="grid"))

    # stats used for battles only
    def hero_battle_stat(self):
        stat = [{
            "health": self.health,
            "health_max": self.health_max,
            "mana": self.mana,
            "mana_max": self.mana_max,
            "attk": self.attk
        }]
        return stat

    # only run on initial class selection
    def choose_job(self, job):
        valid_jobs = ("mage", "warrior")
        job_ck = job.lower().strip()
        if job_ck in valid_jobs:
            if job_ck == "mage":
                self.job = "Mage"
            if job_ck == "warrior":
                self.job = "Warrior"
            self.level_up(1)
        else:
            print("Invalid job")

    # function to add xp from fights. Triggers the level_up function
    def exp_gain(self, xp):
        self.exp += xp
        if self.exp >= self.expbar:
            self.level += 1
            print("YOU HAVE LEVELED UP!")
            self.exp = 0
            self.level_up(self.level)

    # Level character up from CSV. Also used to assign level 1 stats on class selection
    def level_up(self, level):
        if self.job == "Warrior":
            file = "warrior.csv"
        if self.job == "Mage":
            file = "mage.csv"
        with open(file) as c:
            character = csv.DictReader(c)
            for row in character:
                if int(row["level"]) == level:
                    self.expbar = int(row["xp"])
                    self.health += int(row["hp"])
                    self.health_max = int(row["hpx"])
                    self.mana += int(row["mp"])
                    self.mana_max = int(row["mpx"])
                    self.attk += int(row["attk"])
                    print(self)

    # damage calc
    def hero_damage(self, dmg):
        self.health -= dmg
        if self.health <= 0:
            print("You have died!")
            sys.exit("GAME OVER!")
        print(f'{self.health}/{self.health_max}HP')

    # healing calc and notices
    def hero_healing(self, heal):
        self.health += heal
        if self.health >= self.health_max:
            self.health == self.health_max
            print(f'{self.health}/{self.health_max}HP, fully healed')
        else:
            print(f'{self.health}/{self.health_max}HP')

    def hero_attack(self):
        return self.attk

def main():
    story = Story()
    room = Rooms()
    hero = Hero()

    # select initial class
    while hero.job == None:
        print("Welcome to the dungeon!")
        print("Are you a Mage or Warrior?")
        choose = input("class: ")
        ## sleep(1)
        hero.choose_job(choose)

    #  load first room
    while story.progress == None:
        sleep(1)
        print("Your adventure starts here!")
        story.progress = "1"

    # loop until story reaches end point branch 0
    while story.progress != "0":
        room.open_room(story.progress)
        sleep(1)
        if room.mon_stats != []:
            print(room)
            if battle_request(room.mon_stats):
                room.mon_stats, herostats, battle_exp = battling(hero.hero_battle_stat(), room.mon_stats)
                # update hero values
                hero.health = herostats[0]["health"]
                hero.mana = herostats[0]["mana"]
                # check if character has leveled up
                sleep(1)
                print(f'You earned {battle_exp}XP!')
                hero.exp_gain(battle_exp)
                sleep(1)
            print("You have defeated all the monsters")
            sleep(1)
            hero.health, hero.mana = recovery(hero.health, hero.health_max, hero.mana, hero.mana_max)
            sleep(1)
        print("Moving to next room")
        story.progress = room.room_branch

    if story.progress == "0":
        print("You have completed the adventure!")

# seperates the battle or flee request
def battle_request(encounter):
    if encounter != []:
        while True:
            sleep(1)
            reply = input("Do you want to fight the monsters Y/N?: ").lower().strip()
            if reply == "y":
                print("Lets battle!")
                sleep(1)
                return True
            if reply == "n":
                sys.exit("You have fled the dungeon\nYour adventure is over")

# function handling battles. Pass in character attack and monster table.
def battling(hero, monster):
    battle_exp = 0
    while monster != []:
        hero_turn = True
        while hero_turn == True:
            sleep(1)
            print(tabulate(monster, headers="keys", tablefmt="grid"))
            sleep(0.5)
            attack_stat = hero[0]["attk"]
            choice = input("What would you like to do?\n1.Attack\n2.Spell\n3.Flee\n4.Stats\n").strip()
            # sum exp from defeated monsters
            if choice == "1":
                monster, exp_sum = make_attack(monster, attack_stat)
                battle_exp += exp_sum
                hero_turn = False
            if choice == "2":
                monster, hero, exp_sum = cast_spell(monster, hero)
                battle_exp += exp_sum
                hero_turn = False
            if choice == "3":
                sys.exit("You flee the dungeon!\nGame over!")
            if choice == "4":
                print(tabulate(hero, headers="keys", tablefmt="grid"))
            if choice not in ["1", "2", "3", "4"]:
                print("Invalid choice")
        if monster != []:
            hero = monster_attack(monster, hero)
    # tuple with adjusted values
    return monster, hero, battle_exp

# validation for battle targeting
def make_attack(monster, damage):
    while True:
        target = input("Which monster do you want to attack? ")
        battle_exp = 0
        valid_targets = []
        for row in monster:
            valid_targets.append(row["no"])
        if target.isdigit():
            if int(target) in valid_targets:
                for row in monster:
                    if row["no"] == int(target):
                        row["hp"] -= damage
                        print(f'You deal {damage} damage to {row["name"]}!')
                        # check if monster defeated, delete row and add exp
                        if row["hp"] <= 0:
                            print(f'You have defeated {row["name"]}!')
                            battle_exp += row["exp"]
                            monster.remove(row)
                return monster, battle_exp
            else:
                print("Invalid target (type number)")
        else:
            print("Wrong input")

def cast_spell(monster, hero):
    while True:
        choice = input("Cast a spell:\n1.Heal 10HP 3MP \n2.Zap 20Dmg 2MP\n")
        options = ["1", "2"]
        battle_exp = 0
        if choice not in options:
            print("Invalid choice")
        # heal spell
        if choice == "1":
            # check mana (min 3)
            sleep(0.5)
            if hero[0]["mana"] < 3:
                print("Not enough mana. Your spell fails!")
                return monster, hero
            hero[0]["mana"] -= 3
            print("You cast Heal!")
            hero[0]["health"] += 10
            # stop health going above max
            if hero[0]["health"] > hero[0]["health_max"]:
                hero[0]["health"] = hero[0]["health_max"]
            print(tabulate(hero, headers="keys", tablefmt="grid"))
            return monster, hero, battle_exp
        if choice == "2":
            sleep(0.5)
            # check mana (min 2)
            if hero[0]["mana"] < 2:
                print("Not enough mana. Your spell fails!")
                return monster, hero, 0
            hero[0]["mana"] -= 2
            # treat as attack with set damage
            target = input("Which monster do you want to attack? ")
            valid_targets = []
            for row in monster:
                valid_targets.append(row["no"])
            if target.isdigit():
                if int(target) in valid_targets:
                    for row in monster:
                        if row["no"] == int(target):
                            row["hp"] -= 25
                            print(f'You deal 20 damage to {row["name"]}!')
                            # check if monster defeated, delete row and add exp
                            if row["hp"] <= 0:
                                print(f'You have defeated {row["name"]}!')
                                battle_exp += row["exp"]
                                monster.remove(row)
                    return monster, hero, battle_exp
                else:
                    print("Invalid target")
            else:
                print("Invalid target")

def monster_attack(monster, hero):
    sleep(1)
    for row in monster:
        sleep(0.5)
        print(f'• {row["name"]} attacks you with {row["attk"]} for {row["dmg"]} damage!')
        hero[0]["health"] -= int(row["dmg"])
        print(f'⬦ You have {hero[0]["health"]}/{hero[0]["health_max"]}HP')
        if hero[0]["health"] < 1:
            sys.exit("Your hero has died. Game over!")
    return hero

def recovery(hp, hpx, mp, mpx):
    hp_rec = int(hpx/2)
    mp_rec = int(mpx/2)
    while True:
        choice = input(f'Do you want to recover {hp_rec}HP or {mp_rec}MP?').lower().strip()
        if choice == "hp":
            hp += hp_rec
            if hp > hpx:
                hp = hpx
            return hp, mp
        if choice == "mp":
            mp += mp_rec
            if mp > mpx:
                mp = mpx
            return hp, mp
        else:
            print("Invalid choice, HP or MP")

if __name__ == "__main__":
    main()


from django.db import models
from wagtail.wagtailsnippets.models import register_snippet
from wagtail.wagtailcore.fields import RichTextField
from wagtail.wagtailcore.models import Page
from wagtail.wagtailadmin.edit_handlers import FieldPanel, PageChooserPanel
from wagtail.wagtailimages.edit_handlers import ImageChooserPanel

@register_snippet
class Sponsor(models.Model):
    name = models.CharField(max_length=100)
    url = models.URLField()
    image = models.ForeignKey(
        'wagtailimages.Image', related_name='+',
        on_delete=models.SET_NULL, null=True, blank=False)
    description = RichTextField()

    LEVEL_CHOICES = [
        ('1-platinum', 'Platinum'),
        ('2-gold', 'Gold'),
        ('3-standard', 'Standard'),
        ('4-contributor', 'Contributor'),
        ('5-in-kind', 'In-kind'),
        ('6-coffee', 'Coffee charity'),
    ]
    level = models.CharField(choices=LEVEL_CHOICES, max_length=20)

    panels = [
        FieldPanel('name'),
        FieldPanel('url'),
        ImageChooserPanel('image'),
        FieldPanel('description'),
        FieldPanel('level'),
    ]

    def __str__(self):
        return self.name

class SponsorPage(Page):
    body = RichTextField()

    content_panels = Page.content_panels + [
        FieldPanel('body'),
    ]

    subpage_types = []

    def get_context(self, request, *args, **kwargs):
        context = super().get_context(request)
        context.update({
            'sponsors': Sponsor.objects.all().order_by('level', 'name'),
        })
        return context


from bs4 import BeautifulSoup
import requests
import pandas as pd
import numpy as np
from tqdm import tqdm
import urllib
import re
import os

proxies = {
  'http': 'http://proxy-sen.noc.sony.co.jp:10080',
  'https': 'http://proxy-sen.noc.sony.co.jp:10080'
}

food_dict ={
    'meat': ['豚肉', '鶏肉', '鶏むね肉', '豚バラ肉', '豚の角煮', '鶏ささみ', '牛肉', '鶏もも肉'],
    'Vegetables': ['白菜', '里芋', '小松菜', 'れんこん', 'ほうれん草', 'かぼちゃ'],
    'fish': ['さんま', 'さば', '牡蠣', '鮭・サーモン', 'さわら'],
    'rice': ['カレー', 'おにぎり', '雑炊・おじや', 'オムライス', 'ドリア'],
    'pasta': ['カルボナーラ', 'グラタン', 'マカロニグラタン', 'ミートソース', '明太子・たらこスパ'],
    'noodle': ['うどん', 'カレーうどん', '煮込みうどん', '焼きうどん', 'にゅうめん', '焼きそば'],
    'soup': ['ビーフシチュー', 'クリームシチュー', 'シチュー', '中華スープ', 'けんちん汁'],
    'salad': ['生春巻き', '和風のサラダ', 'ポテトサラダ', 'かぼちゃのサラダ', '洋風のサラダ'],
    'fly': ['ポテトコロッケ', 'かぼちゃコロッケ', 'メンチカツ', '里芋コロッケ', 'さつまいもコロッケ'],
    'pot': ['おでん', 'もつ鍋', '豆乳鍋', 'しゃぶしゃぶ', '水炊き'],
    'japanese_fly': ['お好み焼き', 'チヂミ', 'たこ焼き', '広島風お好み焼き', '関西風お好み焼き'],
    'boal': ['おから', '豆腐', 'オムレツ', '豆腐ハンバーグ'],
    'side_food': ['ひじき', 'わかめ', '春雨', 'きくらげ', 'こんにゃく']
}
food_dict_light ={
    'rice': ['カレー', 'オムライス'],
}

def get_source(url, proxy = True):

    if proxy:
        res = requests.get(url, proxies=proxies)
    else:
        res = requests.get(url)

    sp = BeautifulSoup(res.content, "html.parser")

    if not sp:
        return 'empty'
    else:
        return sp

def get_title(sp):

    title = sp.find_all('h1', class_='ttl')

    if not title:
        return 'empty'
    else:
        r = re.search(r'\S*', title[0].contents[-1])
        return r.group()

def get_image(sp, url_root):

    div_img = sp.find_all('div', class_='recipe--detail-main')
    img = div_img[0].find_all('img')

    if not img:
        return 'empty'
    else:
        return urllib.parse.urljoin(url_root, img[0]['src'])

def get_calory(sp):

    result = ''
    calory = sp.find_all('div', class_='recipe')
    for n in calory:
        r = re.search(r'([0-9].*cal)', n.text)
        if r:
            result = r.group()

    if not result:
        return 'empty'
    else:
        return result

def get_salt(sp):

    result = ''
    salt = sp.find_all('div', class_='recipe')
    for n in salt:
        r = re.search(r'([0-9].*g)', n.text)
        if r:
            result = r.group()

    if not result:
        return 'empty'
    else:
        return result

def get_search_result(search_root, url_root, word, max_loop = 10, proxy=True):

    url = search_root.format(word)
    search_result = []
    for i in range(max_loop):
        result = []
        req_url = url + str(i+1)
        if proxy:
            res = requests.get(req_url, proxies=proxies)
        else:
            res = requests.get(req_url)
        sp = BeautifulSoup(res.content, 'html.parser')
        div = sp.find_all('div', class_='mk-tab-contents')
        div_child = div[0].find_all('div', class_='recipe--category-recipe')

        for div_c in div_child:
            result.append(urllib.parse.urljoin(url_root, div_c.attrs['data-url']))

        result = list(set(result))
        search_result += result

    return search_result

if __name__ == '__main__':

    search_root = 'https://www.kyounoryouri.jp/search/recipe?keyword={}&pg='
    url_root = 'https://www.kyounoryouri.jp/'
    save_file_name = 'kyouno.csv'

    max_search_loop = 10

    for key, value in tqdm(food_dict.items()):
        for word in tqdm(value):
            search_result = get_search_result(search_root, url_root, word, max_search_loop, proxy=False)

            category_list, food_type_list, url_list, title_list = [], [], [], []
            calory_list, salt_list, img_list = [], [], []

            for item in tqdm(search_result):
                url_list.append(item)
                sp = get_source(item, proxy=False)
                if sp:
                    category_list.append(key)
                    food_type_list.append(word)
                    title_list.append(get_title(sp))
                    calory_list.append(get_calory(sp))
                    salt_list.append(get_salt(sp))
                    img_list.append(get_image(sp, url_root))
            df = pd.DataFrame({'category': category_list,
                               'type': food_type_list,
                               'title': title_list,
                               'calorie': calory_list,
                               'salt': salt_list,
                               'img': img_list,
                               'url': url_list})
            if os.path.exists(save_file_name):
                df.to_csv(save_file_name, index=False, header=False, mode='a')
            else:
                df.to_csv(save_file_name, index=False, header=True)