import json
import os

import discord
from discord.ext import commands

import Music_Commands
import Standard_Commands
# import Chess_Commands
# import Minecraft_Commands
import Wordle_Commands

with open(os.path.dirname(os.path.realpath(__file__)) + '/config.json', 'r') as config:
    TOKEN = json.loads(config.read())['token']

description = ''' An all purpose discord bot for everyday use '''
client = commands.Bot(command_prefix=commands.when_mentioned_or("!"), description=description)

class Main(commands.Cog):
    def __init__(self, bot):
        self.bot = bot

    @commands.command(name="ping", description="pong")
    async def _ping(self, ctx):
        await ctx.send("pong")

@client.event
async def on_ready():
    await client.change_presence(activity=discord.Activity(type=discord.ActivityType.listening, name='!'))
    print('Logged in as')
    print(client.user.name)
    print(client.user.id)
    print('------')

if __name__ == "__main__":
    client.add_cog(Main(client))
    client.add_cog(Standard_Commands.Standard(client))
    client.add_cog(Music_Commands.Music(client))
    # client.add_cog(Chess_Commands.Chess(client))
    # client.add_cog(Minecraft_Commands.Minecraft(client))
    client.add_cog(Wordle_Commands.Wordle(client))
    client.run(TOKEN)


import re

import mock
import yaql

import murano.dsl.helpers as helpers
import murano.dsl.namespace_resolver as ns_resolver
import murano.dsl.yaql_expression as yaql_expression
from murano.tests.unit import base

ROOT_CLASS = 'io.murano.Object'

class TestNamespaceResolving(base.MuranoTestCase):

    def setUp(self):
        super(TestNamespaceResolving, self).setUp()

    def test_fails_w_empty_name(self):
        resolver = ns_resolver.NamespaceResolver({'=': 'com.example.murano'})

        self.assertRaises(ValueError, resolver.resolve_name, None)

    def test_fails_w_unknown_prefix(self):
        resolver = ns_resolver.NamespaceResolver({'=': 'com.example.murano'})
        name = 'unknown_prefix:example.murano'

        self.assertRaises(KeyError, resolver.resolve_name, name)

    def test_fails_w_prefix_wo_name(self):
        resolver = ns_resolver.NamespaceResolver({'=': 'com.example.murano'})
        name = 'sys:'

        self.assertRaises(NameError, resolver.resolve_name, name)

    def test_fails_w_excessive_prefix(self):
        ns = {'sys': 'com.example.murano.system'}
        resolver = ns_resolver.NamespaceResolver(ns)
        invalid_name = 'sys:excessive_ns:muranoResource'

        self.assertRaises(NameError, resolver.resolve_name, invalid_name)

    def test_cuts_empty_prefix(self):
        resolver = ns_resolver.NamespaceResolver({'=': 'com.example.murano'})
        # name without prefix delimiter
        name = 'some.arbitrary.name'

        resolved_name = resolver.resolve_name(':' + name)

        self.assertEqual(name, resolved_name)

    def test_resolves_specified_ns_prefix(self):
        ns = {'sys': 'com.example.murano.system'}
        resolver = ns_resolver.NamespaceResolver(ns)
        short_name, full_name = 'sys:File', 'com.example.murano.system.File'

        resolved_name = resolver.resolve_name(short_name)

        self.assertEqual(full_name, resolved_name)

    def test_resolves_current_ns(self):
        resolver = ns_resolver.NamespaceResolver({'=': 'com.example.murano'})
        short_name, full_name = 'Resource', 'com.example.murano.Resource'

        resolved_name = resolver.resolve_name(short_name)

        self.assertEqual(full_name, resolved_name)

    def test_resolves_explicit_base(self):
        resolver = ns_resolver.NamespaceResolver({'=': 'com.example.murano'})

        resolved_name = resolver.resolve_name('Resource', relative='com.base')

        self.assertEqual('com.base.Resource', resolved_name)

    def test_resolves_explicit_base_w_empty_namespaces(self):
        resolver = ns_resolver.NamespaceResolver({})

        resolved_name = resolver.resolve_name('File', 'com.base')

        self.assertEqual('com.base.File', resolved_name)

    def test_resolves_w_empty_namespaces(self):
        resolver = ns_resolver.NamespaceResolver({})

        resolved_name = resolver.resolve_name('Resource')

        self.assertEqual('Resource', resolved_name)

class Bunch(object):
    def __init__(self, **kwargs):
        super(Bunch, self).__init__()
        for key, value in kwargs.iteritems():
            setattr(self, key, value)

class TestHelperFunctions(base.MuranoTestCase):

    def setUp(self):
        super(TestHelperFunctions, self).setUp()

    def test_generate_id(self):
        generated_id = helpers.generate_id()

        self.assertTrue(re.match(r'[a-z0-9]{32}', generated_id))

    def test_evaluate(self):
        yaql_value = mock.Mock(spec=yaql_expression.YaqlExpression,
                               evaluate=lambda context: 'atom')
        complex_value = {yaql_value: ['some', (1, yaql_value), lambda: 'hi!'],
                         'sample': [yaql_value, xrange(5)]}
        complex_literal = {'atom': ['some', (1, 'atom'), 'hi!'],
                           'sample': ['atom', [0, 1, 2, 3, 4]]}
        # tuple(evaluate(list)) transformation adds + 1
        complex_literal_depth = 3 + 1

        context = yaql.create_context(False)
        evaluated_value = helpers.evaluate(yaql_value, context, 1)
        non_evaluated_value = helpers.evaluate(yaql_value, context, 0)
        evaluated_complex_value = helpers.evaluate(complex_value, context)
        non_evaluated_complex_value = helpers.evaluate(
            complex_value, context, complex_literal_depth)

        self.assertEqual('atom', evaluated_value)
        self.assertNotEqual('atom', non_evaluated_value)
        self.assertEqual(complex_literal, evaluated_complex_value)
        self.assertNotEqual(complex_literal, non_evaluated_complex_value)

    def test_needs_evaluation(self):
        testee = helpers.needs_evaluation
        parsed_expr = yaql.parse("string")
        yaql_expr = yaql_expression.YaqlExpression("string")

        self.assertTrue(testee(parsed_expr))
        self.assertTrue(testee(yaql_expr))
        self.assertTrue(testee({yaql_expr: 1}))
        self.assertTrue(testee({'label': yaql_expr}))
        self.assertTrue(testee([yaql_expr]))

class TestYaqlExpression(base.MuranoTestCase):

    def setUp(self):
        super(TestYaqlExpression, self).setUp()

    def test_expression(self):
        yaql_expr = yaql_expression.YaqlExpression('string')

        self.assertEqual('string', yaql_expr.expression)

    def test_evaluate_calls(self):
        string = 'string'
        expected_calls = [mock.call(string),
                          mock.call().evaluate(context=None)]

        with mock.patch('yaql.parse') as mock_parse:
            yaql_expr = yaql_expression.YaqlExpression(string)
            yaql_expr.evaluate()

        self.assertEqual(expected_calls, mock_parse.mock_calls)

    def test_match_returns(self):
        expr = yaql_expression.YaqlExpression('string')

        with mock.patch('yaql.parse'):
            self.assertTrue(expr.match('$some'))
            self.assertTrue(expr.match('$.someMore'))

        with mock.patch('yaql.parse') as parse_mock:
            parse_mock.side_effect = yaql.exceptions.YaqlGrammarException
            self.assertFalse(expr.match(''))

        with mock.patch('yaql.parse') as parse_mock:
            parse_mock.side_effect = yaql.exceptions.YaqlLexicalException
            self.assertFalse(expr.match(''))


import json

DATASET_NAME = "ontonotes_english"
DATASET_TYPE = "test"
ENTITY_TYPE_LIST_WITHOUT_PERSON = {"ontonotes_names": ['GPE', 'ORG', 'FAC', 'LOC', 'NORP', 'LAW', 'EVENT',
                                                       'WORK_OF_ART', 'PRODUCT', 'LANGUAGE'],
                                   "conll2003": ["ORG", "LOC"],
                                   "ontonotes_english": ['GPE', 'ORG', 'FAC', 'LOC', 'NORP', 'LAW', 'EVENT',
                                                         'WORK_OF_ART', 'PRODUCT', 'LANGUAGE']
                                   }

def load_class_instances():
    class_instances = {}
    counters = {}
    for ent_type in ENTITY_TYPE_LIST_WITHOUT_PERSON[DATASET_NAME]:
        class_instances[ent_type] = {}
        counters[ent_type] = 0
        with open(DATASET_NAME + "/" + DATASET_TYPE + "/class_instances/" + ent_type + ".jsonl", "r") as f:
            for line in f:
                data = json.loads(line.strip())
                counters[ent_type] += len(data['instances'].items())
                class_instances[ent_type][data['class_id']] = {"class_title": data['class_title'],
                                                               "instances": data['instances']}
    return class_instances, counters

def process_class_intances(class_instances):
    processed_class_instances = {}
    for ent_type in ENTITY_TYPE_LIST_WITHOUT_PERSON[DATASET_NAME]:
        processed_class_instances[ent_type] = {}
        for class_id, data in class_instances[ent_type].items():
            class_title = data['class_title']
            instances = data['instances']
            # processed_instances = {k: v for k, v in instances.items()
            #                        if k != v and all(ord(char) < 128 for char in v)}
            # if not processed_instances:
            #     print("No instances after processing", class_id, class_title)
            processed_class_instances[ent_type][class_id] = {"class_title": class_title,
                                                             "instance_num": len(instances),
                                                             "instances": instances}
    return processed_class_instances

def output_processed_class_instances(processed_class_instances):
    counters = {}
    for ent_type in ENTITY_TYPE_LIST_WITHOUT_PERSON[DATASET_NAME]:
        counters[ent_type] = 0
        processed_class_instances[ent_type] = {k: v for k, v in sorted(processed_class_instances[ent_type].items(), key=lambda x: x[1]['instance_num'], reverse=True)}
        with open(DATASET_NAME + "/" + DATASET_TYPE + "/processed_class_instances/" + ent_type + ".jsonl", "w+") as f:
            for class_id, data in processed_class_instances[ent_type].items():
                counters[ent_type] += len(data['instances'].items())
                f.write(json.dumps(dict(class_id=class_id,
                                        class_title=data['class_title'],
                                        instance_num=data['instance_num'],
                                        instances=data['instances']))
                        + '\n')
    return counters

def main():
    class_instances, counters = load_class_instances()
    processed_class_instances = process_class_intances(class_instances)
    processed_counters = output_processed_class_instances(processed_class_instances)
    for ent_type in ENTITY_TYPE_LIST_WITHOUT_PERSON[DATASET_NAME]:
        print(ent_type + '\t\t\t' + str(counters[ent_type]) + '\t' + str(processed_counters[ent_type]))

if __name__ == '__main__':
    main()


import os
import sys
from PIL import Image

def cutImage(image):
    """Accepts an image containing a whole comic and returns a dict of images, each a panel of the comic."""
    retDict = {}

    tlwidth = 244
    tmwidth = 129
    trwidth = 362

    blwidth = 194
    bmwidth = 298
    brwidth = 243

    theight = 243
    bheight = 244

    tlcoords = (0, 0, tlwidth, theight)
    tlimage = image.crop(tlcoords)
    tlimage.load()
    retDict["topleft"] = tlimage

    tmcoords = (tlwidth, 0, tlwidth + tmwidth, theight)
    tmimage = image.crop(tmcoords)
    tmimage.load()
    retDict["topmiddle"] = tmimage

    trcoords = (tlwidth + tmwidth, 0, tlwidth + tmwidth + trwidth, theight)
    trimage = image.crop(trcoords)
    trimage.load()
    retDict["topright"] = trimage

    blcoords = (0, theight, blwidth, theight + bheight)
    blimage = image.crop(blcoords)
    blimage.load()
    retDict["bottomleft"] = blimage

    bmcoords = (blwidth, theight, blwidth + bmwidth, theight + bheight)
    bmimage = image.crop(bmcoords)
    bmimage.load()
    retDict["bottommiddle"] = bmimage

    brcoords = (blwidth + bmwidth, theight, blwidth + bmwidth + brwidth, theight + bheight)
    brimage = image.crop(brcoords)
    brimage.load()
    retDict["bottomright"] = brimage

    return retDict

def dumpPanels(outdir, comicFileName, imageDict):
    for panelname, image in imageDict.items():
        outputdir = os.path.join(outdir, panelname)
        if not os.path.exists(outputdir):
            os.mkdir(outputdir)

        outfileName = os.path.join(outputdir, comicFileName) + "-" + panelname + ".png"
        image.save(outfileName)

def cutAllImagesInDir(indir, outdir):
    allFiles = os.listdir(indir)
    numImages = 0

    print("Looking for new files to cut...")

    for filename in allFiles:
        filePath = os.path.join(indir + filename)
        filenamePrefix = filename[0:filename.rfind(".")]
        tlPanelFileName = filenamePrefix + "-topleft.png"

        if not os.path.exists(os.path.join(outdir, "topleft", tlPanelFileName)):
            print('processing', filePath)

            image = Image.open(filePath)
            panels = cutImage(image)

            dirs, filename = os.path.split(filePath)
            filename, sep, ext = filename.rpartition(".")

            dumpPanels(outdir, filename, panels)
            numImages = numImages + 1

    return numImages

def cutAllImages():
    scriptDir = sys.path[0]
    comicsDir = os.path.join(scriptDir, "../data/comics/")
    panelsDir = os.path.join(scriptDir, "../public/panels/")
    processedImages = cutAllImagesInDir(comicsDir, panelsDir)

    print(processedImages, 'images processed\n')

if __name__ == "__main__":
    cutAllImages()


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

urlpatterns = [
    url(r'^$', views.post_list),
    url(r'home$', views.home),
    url(r'board$', views.board),
    url(r'cities$', views.cities),
    url(r'orgs$', views.orgs),
    ]


import torch
from abc import ABC, abstractmethod
from typing import Dict
from trainer.model import Model
import trainer.utils as utils
from trainer.logger import Logger
from rich.layout import Layout
from rich.console import Console
from rich.panel import Panel
from rich.progress import (Progress, MofNCompleteColumn, BarColumn, TextColumn,
                           TaskProgressColumn, TimeRemainingColumn, TimeElapsedColumn)
from rich.columns import Columns
from rich.live import Live
from collections import deque
from warnings import warn
from contextlib import nullcontext
# from trainer.utils import register_class
from trainer.storage import Storage
import tempfile

class Trainer(ABC):

    def __init__(self, config: Dict, model: Model = None, logger: Logger = None) -> None:
        self.config = self.parse_config(config)
        self._setup(config, model, logger)
        # self._register_trainer()

    def fit(self, num_train_steps: int=None, trainer_state=None) -> None:
        self.num_train_steps = num_train_steps or self.config['num_train_steps']

        self.before_train(trainer_state) # will load trainer checkpoint if needed
        with self.terminal_display:
            while not self.train_end:
                self.before_epoch()
                # Run training epoch
                for batch_idx, batch in enumerate(self.train_data):
                    self.before_step()
                    self.train_step(batch, batch_idx)
                    self.after_step()
                self.evaluate(self.eval_data, training_mode=True)
                self.after_epoch()

        self.after_train()

    def train_step(self, batch, batch_idx=None):
        train_step_output = self.model.training_step(batch, batch_idx)
        self.train_log.append({'step': self.step, 'log': train_step_output})

    def evaluate(self, eval_data=None, trainer_mode=False):
        raise NotImplementedError

    def set_model(self, model: Model) -> None:
        self.model = model

    def set_logger(self, logger: Logger) -> None:
        self.logger = logger

    @abstractmethod
    def setup_data(self, config: Dict) -> None:
        """
        Setup the dataset
        e.g.
        self.data = {
        'train': train_dataset/dataloader,
        'eval': eval_dataset/dataloader,
        'replay_buffer': replay_buffer,
        }
        """
        self.data = None

    def before_train(self, trainer_state: Dict =None, info=None):
        """
        Callbacks before training starts
        """
        self.init_trainer_state(trainer_state)

        # Make sure the model and logger are set
        assert self.model is not None, "Model not set. Use trainer.set_model(model) to do so."
        assert self.logger is not None, "Logger not set. Use trainer.set_logger(logger) to do so"
        # TODO: Should project/runs be defined before logger?
        self.project = self.logger.project
        self.run = self.logger.run_id
        self._set_storage()
        self.setup_evaluator()

        self._setup_terminal_display(self.config)
        self._set_seeds(self.config)
        # Optionally load from checkpoint
        if self.load_from_checkpoint:
            self._load_checkpoint()
        if self.progress is not None:
            # Initialize progress
            self.progress.update(self.progress_train, completed=self.step)
        self.logger.start()
        # Set model to train mode
        self.model.train()

        # Upload configs to storage
        self.output_storage.save(filename='trainer_config.yaml',
                                 data=self.config,
                                 filetype='yaml')
        self.output_storage.save(filename='model_config.yaml',
                                 data=self.model.config,
                                 filetype='yaml')
        self.output_storage.save(filename='logger_config.yaml',
                                 data=self.logger.config,
                                 filetype='yaml')

    def before_epoch(self, info=None):
        pass

    def before_step(self, info=None):
        self.model.train() # Set model to training mode

    def after_train(self, info=None):
        """
        Callbacks after training ends
        """
        # Close logger
        # Close any open connections
        self.log_train(info)
        self.logger.finish()

    def after_epoch(self, info=None):
        self.epoch += 1
        self.log_epoch(info)

        # Save checkpoint
        if (self.save_checkpoint_freq is not None) and (self.epoch % self.save_checkpoint_freq == 0) and (self.step > 0):
            self._save_checkpoint(ckpt_state_args={'save_optimizers': True})
            self.num_checkpoint_saves += 1
            self.logger.log(log_dict={'trainer_step': self.step, 'checkpoint/num_checkpoint_saves': self.num_checkpoint_saves})

    def after_step(self, info=None):
        # Update trainer state
        self.update_trainer_state()
        # Logging
        self.log_step(info)
        # Update progress
        if self.progress is not None:
            self.progress.update(self.progress_train, completed=self.step)

    def log_step(self, info=None):
        """
        Log using self.train_log and self.eval_log
        self.*_log = deque({'step': int, 'output': Dict})
        """
        pass

    def log_epoch(self, info=None):
        pass

    def log_train(self, info=None):
        pass

    def update_trainer_state(self):
        # Check if training is complete
        if self.step >= self.num_train_steps:
            self.train_end = True

    #############################

    # @property
    # def module_path(self):
    #     return None

    def _set_storage(self):
        # Update project and run_id for stroage defs
        for name in self.config['storage'].keys():
            self.config['storage'][name].update({
                'project': self.project,
                'run': self.run
            })

        # Set up model storage: model will be loaded from here
        self.input_storage = Storage(self.config['storage']['input'])
        # Set up output storage: evaluation outputs will be saved here
        self.output_storage = Storage(self.config['storage']['output'])
        # Create a temporary directory for temp storage
        self.tmp_root_dir = tempfile.TemporaryDirectory(prefix='trainer')
        self.tmp_storage = Storage({
            'type': 'local',
            'root_dir': self.tmp_root_dir.name,
            'project': self.project,
            'run': self.run
        })

    def parse_config(self, config: Dict) -> Dict:
        return config

    def init_trainer_state(self, state_dict=None):
        state_dict = state_dict or {}
        # Load from state_dict or initialize for start of training
        self.step = state_dict.get('step', 0)
        self.epoch = state_dict.get('epoch', 0)
        self.train_log = state_dict.get('train_log', deque(maxlen=self.log_length_train))
        self.eval_log = state_dict.get('eval_log', deque(maxlen=self.log_length_eval))
        self.train_end = state_dict.get('train_end', False)
        self.num_checkpoint_loads = state_dict.get('num_checkpoint_loads', 0)
        self.num_checkpoint_saves = state_dict.get('num_checkpoint_saves', 0)

    def _setup(self, config: Dict = None, model: Model = None, logger: Logger =None) -> None:

        self.device = torch.device(config.get('device',
                                              'cuda' if torch.cuda.is_available() else 'cpu')
                                              )

        self.load_from_checkpoint = config.get('load_from_checkpoint', False)
        self.save_checkpoints = config.get('save_checkpoints', True)
        self.save_checkpoint_freq = config.get('save_checkpoint_freq', 1)
        self.eval_freq = config.get('eval_freq', 2)
        self.async_eval = config.get('async_eval', False)
        self.log_length_train = config.get('log_length_train', 100)
        self.log_length_eval = config.get('log_length_eval', 100)

        self.model = model
        self.logger = logger
        self._set_seeds(config)
        self.setup_data(config)

    def setup_evaluator(self):
        raise NotImplementedError

    def _setup_terminal_display(self, config: Dict) -> None:

        if config.get('terminal_display') in ['rich', 'rich_minimal']:

            self._term_console = Console()
            self._term_layout = Layout()
            # Header: Run info panel
            self._term_logger_panel = Panel.fit(f"""Project: [orange4] {self.logger.project} [/] \nRun: [orange4] {self.logger.run_name}({self.logger.run_id})[/]\
                                                \nResumed: [orange4] {self.logger.resumed_run}[/] \nLogdir: [orange4] {self.logger.logdir}[/]""",
                                    title='Logger', border_style='orange4')

            # Body: Progress panel
            self.progress = Progress(
                TextColumn("Training Progress"),
                BarColumn(complete_style="dark_sea_green4"),
                TaskProgressColumn(),
                TimeElapsedColumn(),
                TimeRemainingColumn(),
                MofNCompleteColumn(),
                redirect_stdout=False
                )
            self.progress_train = self.progress.add_task("[dark_sea_green4] Training...", total=self.num_train_steps)

            if config.get('terminal_display') == 'rich_minimal':
                # Only a progress bar
                self.terminal_display = self.progress
            else:
                # Full terminal display
                self._term_progress_panel = Panel.fit(Columns([self.progress]), title="Training", border_style="dark_sea_green4")
                # Footer: Misc info
                self._term_footer_text = f"""[bold]Device[/]: {self.device}"""
                self._term_footer_panel = Panel(self._term_footer_text, title="Info", border_style='misty_rose1')
                # Add panels to layout
                self._term_layout.split(
                    Layout(self._term_logger_panel, size=6, name="logger"),
                    Layout(self._term_progress_panel, size=5, name="progress"),
                    Layout(self._term_footer_panel, size=3, name="footer"),
                )
                self.terminal_display = Live(self._term_layout,
                                            screen=False,
                                            refresh_per_second=config.get('terminal_refresh_rate', 1))

        else:
            self.terminal_display = nullcontext()
            self.progress = None

    def _set_seeds(self, config: Dict) -> None:
        """
        Set seeds for random, numpy, cuda, torch
        """
        seed = config.get('seed')
        if seed:
            utils.set_seed_everywhere(seed)

    def _load_checkpoint(self, chkpt_name='checkpoint', log_checkpoint=True):
        try:
            # Restore checkpoint: model,trainer,replay_buffer
            ckpt = super()._load_checkpoint_dict(chkpt_name=chkpt_name,
                                                 filenames=['model_ckpt.pt', 'trainer_ckpt.pt'],
                                                 filetypes=['torch', 'torch']
                                                 )
        except (UserWarning, Exception) as e:
            self.logger.log(log_dict={'trainer_step': self.step, 'checkpoint_load_error': 1})
            warn(f"Checkpoint load error: Could not load state dict. Error: {e.args}")
            return e

        # Update model and trainer states
        try:
            self.model.load_model(state_dict=ckpt['model_ckpt.pt'])
            self.init_trainer_state(ckpt['trainer_ckpt.pt'])
        except (UserWarning, Exception) as e:
            self.logger.log(log_dict={'trainer_step': self.step, 'checkpoint_load_error': 1})
            warn(f"Checkpoint load error: State dicts loaded, but could not update model or trainer. Error: {e.args}")
            return e

        if log_checkpoint:
            # Log the checkpoint load event
            self.num_checkpoint_loads += 1
            self.logger.log(key='checkpoint/num_checkpoint_loads', value=self.num_checkpoint_loads, step=self.logger._sw.step)

    def _load_checkpoint_dict(self, chkpt_name='checkpoint', filenames=None, filetypes=None):
        if filenames is None:
            # Assume only model and trainer checkpoints
            filenames = ['model_ckpt.pt', 'trainer_ckpt.pt']
        if filetypes is None:
            # Assume all files are torch-loadable
            filetypes = ['torch']*len(filenames)
        # Download checkpoint from logger
        if self.config['load_checkpoint_type'] == 'torch':
            ckpt = self.input_storage.load(f'model_{chkpt_name}.pt')
        elif self.config['load_checkpoint_type'] == 'zip':

            ckpt = self.input_storage.load_from_archive("ckpt.zip",
                                                        filenames=filenames,
                                                        filetypes=filetypes)

        return ckpt

    def _save_checkpoint(self, log_checkpoint=True, save_to_output_storage=True, ckpt_state_args=None, ckpt_name=None):
        """
        Zip the checkpoint folder and log it
        """

        if ckpt_name is None:
            ckpt_name = 'ckpt'

        # Create an archive of the checkpoint files
        self._create_checkpoint_files(archive=True, ckpt_state_args=ckpt_state_args, ckpt_name=ckpt_name)

        # Check if zip file was created properly
        if self.tmp_storage.archive_filenames(f'{ckpt_name}.zip') is None:
            warn("Checkpoint zip file was not created properly (in tmp storage). Checkpoint not saved in output storage.")
            self.logger.log(log_dict={'trainer_step': self.step, 'checkpoint_save_error': 1})
            return None

        if save_to_output_storage:
            # First make a backup copy of the checkpoint
            # if 'ckpt.zip' in self.output_storage.get_filenames():
            #     self.output_storage.copy('ckpt.zip', 'ckpt_backup.zip')

            # upload checkpoint to output storage as a temp file
            self.output_storage.upload(files=self.tmp_storage.storage_path(f'{ckpt_name}.zip'), new_dir=f'{ckpt_name}_temp')
            # Check if the new temp ckpt was created successfully
            if self.output_storage.archive_filenames(f'{ckpt_name}_temp/{ckpt_name}.zip') is None:
                # warn("The ckpt_temp.zip file was not uploaded properly (to output storage)")
                # Replace 'ckpt.zip' with the backup
                # self.output_storage.copy('ckpt_backup.zip', 'ckpt.zip')
                self.logger.log(log_dict={'trainer_step': self.step, 'checkpoint_save_error': 1})
                raise Exception(f"The {ckpt_name}_temp.zip file was not uploaded properly (to output storage)")
            else:
                # No errors in the new checkpoint, replace existing backup
                self.output_storage.copy(f'{ckpt_name}_temp/{ckpt_name}.zip', f'{ckpt_name}.zip')
                # Delete the temp file
                self.output_storage.delete(directory=f"{ckpt_name}_temp")

        if log_checkpoint:
            # Save checkpoint to logger (e.g. wandb)
            self.logger.log_checkpoint(filepath=self.tmp_storage.storage_path(f'{ckpt_name}.zip'))

    def _create_checkpoint_files(self, temporary_dir=True, archive=True, ckpt_state_args=None, **kwargs):
        """
        Save checkpoint files to storage
        """
        ckpt_name = kwargs.get('ckpt_name', 'ckpt')

        storage = self.tmp_storage if temporary_dir else self.output_storage
        # Get checkpoint state
        ckpt_state_args = ckpt_state_args or {}
        ckpt_state = self._get_checkpoint_state(**ckpt_state_args)

        for name,state in ckpt_state.items():
            storage.save(f"{ckpt_name}/{name}_{ckpt_name}.pt", state, 'torch')

        if archive:
            files=[f"{ckpt_name}/{name}_{ckpt_name}.pt" for name in ckpt_state.keys()]
            # Make the archive
            storage.make_archive(files, zipfile_name=f'{ckpt_name}.zip')
            # Delete the files once they've been archived
            storage.delete(directory=ckpt_name)

    def _get_trainer_state(self):
        """
        The current trainer state
        """
        return {'step': self.step,
                'epoch': self.epoch,
                'train_log': self.train_log,
                'eval_log': self.eval_log,
                'train_end': self.train_end,
                'num_checkpoint_saves': self.num_checkpoint_saves,
                'num_checkpoint_loads': self.num_checkpoint_loads,
            }

    def _get_checkpoint_state(self, save_optimizers, **kwargs):
        trainer_state = self._get_trainer_state()
        ckpt_state = {
            'trainer': trainer_state,
            'model': self.model.state_dict(save_optimizers=save_optimizers),
        }
        return ckpt_state

if __name__ == "__main__":
    import yaml
    with open('trainer/test_config.yaml') as f:
        config = yaml.load(f, Loader=yaml.FullLoader)

    model = Model(config['model'])
    logger = Logger(config['logger'])
    trainer = Trainer(config['trainer'], model, logger)

    trainer.train(config['trainer']['num_train_steps'])


import os
import random
import collections
from functools import reduce

import sympy

from sympy2code import java_code_method

from .sympy_formula import *
from .dir_const import java_module_dir, java_test_dir

java_code_dict = dict(
    add1 = add_1,
    minus1 = minus_1,
    mul1 = mul_1,
    div1 = div_1,
    floor1 = floor_1,
    complexFormula1 = complex_formula_1,
    hItemFromBigBoxMCrit =  h_item_from_big_box_m_crit
)

java_code_all = "\n\t".join(
    java_code_method(v, k) for k, v in java_code_dict.items()
).replace('\n', '\n\t')

with open(os.path.join(java_module_dir, "SympyCode.java"), "w", encoding="utf8") as f:
    f.write("package sympy;\n\n")
    f.write(
    """public class SympyCode {\n"""
    f"""{java_code_all}\n"""
    """}"""
    )

java_test_code = ""
for name, formula in java_code_dict.items():
    java_test_code += f"\t@Test\n\tpublic void {name}Test() {{\n"

    for _ in range(10):
        formula_symbols = collections.OrderedDict(
            sorted({s: random.randint(-100000, 100000) for s in formula.atoms() if isinstance(s, sympy.Symbol)}.items(), key=lambda x: x[0].__repr__())
        )
        variables_str = ", ".join(f"{v}"for k, v in formula_symbols.items())
        res = float(sympy.Float(reduce(lambda f, s: f.subs(s[0], s[1]), formula_symbols.items(), formula)).evalf())
        java_test_code += f"\t\tassertEquals(SympyCode.{name}({variables_str}), {res});\n"
    java_test_code += "\t};\n\n"

test_file_str = f"""\
package sympytest;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
import sympy.SympyCode;
class SympyJUnit5Test {{
{java_test_code}
}};
"""

with open(os.path.join(java_test_dir, "SympyTest.java"), "w", encoding="utf8") as f:
    f.write(test_file_str)


import torch
from proposed_utils import *
##Cross Validation
from sklearn.model_selection import KFold
import tensorflow.keras as keras
import tensorflow as tf
import os
import tqdm

# main
def train(dataloader, model, mode, lr, batch_size, decay, num_epochs, n_folds, workspace):
    results = {}
    folds = KFold(n_splits = n_folds)

    fff = ["fold1", "fold2", "fold3", "fold4", "fold5"]
    workspace = workspace + "/proposed_model" + "/" + "mode%d" % mode +"_1e-6_dice"

    # KFold Cross Validation
    for fold_, (trn_idx, val_idx) in enumerate(folds.split(dataloader)):
        print("File path with the name: %s" % workspace)
        if not os.path.isdir(os.path.join(workspace, fff[fold_])): os.makedirs(os.path.join(workspace, fff[fold_]))

        print("fold n°{}".format(fold_+1))
        ##Split by folder and load by dataLoader
        train_subsampler = torch.utils.data.SubsetRandomSampler(trn_idx)
        valid_subsampler = torch.utils.data.SubsetRandomSampler(val_idx)
        train_dataloader = torch.utils.data.DataLoader(dataloader, batch_size=batch_size, sampler=train_subsampler, drop_last=True)
        valid_dataloader = torch.utils.data.DataLoader(dataloader, batch_size=batch_size, sampler=valid_subsampler, drop_last=True)

        # Initialize Model
        train_vars = []
        train_vars += model.trainable_variables

        # Initialize optimizer
        lr_schedule = keras.optimizers.schedules.ExponentialDecay(lr, decay_steps=len(trn_idx) // batch_size, decay_rate=decay, staircase=True)
        optim = keras.optimizers.Adam(lr_schedule)

        # Model selection
        best_iou = 0.0
        for epoch in tqdm.trange(num_epochs):
            train_loss, train_iou = [], []
            valid_loss, valid_iou = [], []

            for batch_idx, (features,targets) in enumerate(train_dataloader):
                features, targets = tf.constant(features), tf.constant(targets)

                with tf.GradientTape() as tape:
                    logits = model({"in": features}, training=True)["out"]
                    dice_loss = DiceLoss(logits, targets)
                    bce_loss = K.mean(tf.keras.losses.binary_crossentropy(logits, targets))
                    total_loss = dice_loss + bce_loss

                grads = tape.gradient(total_loss, train_vars)
                optim.apply_gradients(zip(grads, train_vars))

                iou = iou_score(targets, logits)*100.0
                train_loss.append(total_loss)
                train_iou.append(iou)

                if not batch_idx % 80:
                    print ('Epoch: %03d/%03d | Batch %03d/%03d | Train Loss: %.4f | Train IoU: %.4f%% '
                           %(epoch+1, num_epochs, batch_idx,
                             len(train_dataloader),
                             np.mean(train_loss),
                             np.mean(train_iou))
                          )

            ##Valid
            for batch_idx, (features,targets) in enumerate(tqdm.tqdm(valid_dataloader)):
                features, targets = tf.constant(features), tf.constant(targets)

                logits = model({"in": features}, training=False)["out"]
                dice_loss = DiceLoss(logits, targets)
                bce_loss = tf.keras.losses.binary_crossentropy(logits, targets)
                total_loss = dice_loss + bce_loss
                iou = iou_score(targets, logits)*100.0

                ### LOGGING
                valid_loss.append(total_loss)
                valid_iou.append(iou)
            print('Epoch: %03d/%03d |  Valid Loss: %.4f | Valid IoU: %.4f%%' % (
                  epoch+1, num_epochs,
                  np.mean(valid_loss),
                  np.mean(valid_iou)))

            ## model save
            if best_iou <= np.mean(valid_iou):
                best_iou = np.mean(valid_iou)
                model.save(os.path.join(workspace, fff[fold_]) + "/epoch%03d_model" % epoch)
                results[fold_+1] = np.mean(valid_iou)

    # Print fold results
    print(f'\nK-FOLD CROSS VALIDATION RESULTS FOR {n_folds} FOLDS')
    print('--------------------------------')
    sum = 0.0
    for key, value in results.items():
        print(f'Fold {key}: {value} %')
        sum += value
    print(f'Average: {sum/len(results.items())} %')


import torch.nn as nn
import torch
import torch.nn.functional as F
import torchvision.models as models
from functools import partial
from ConvLSTM import ConvLSTM
from config import *
# from ACLNet.ConvLSTM import ConvLSTM
# from ACLNet.config import *

class ACLNet(nn.Module):
    def __init__(self):
        super(ACLNet,self).__init__()
        self.Vgg_feats=models.vgg16(pretrained=True).features[0:30]

        self.maxpool_atn1_1 = nn.MaxPool2d((2, 2), stride=(2, 2))
        self.conv_atn1_1 = nn.Conv2d(512, 64, 1)
        self.conv_atn1_2 = nn.Conv2d(64, 128, 3, padding=(1, 1))
        self.maxpool_atn1_2 = nn.MaxPool2d((2, 2), stride=(2, 2))
        self.conv_atn1_3 = nn.Conv2d(128, 64, 1)
        self.conv_atn1_4 = nn.Conv2d(64, 128, 3, padding=(1, 1))
        self.conv_atn1_5 = nn.Conv2d(128, 1, 1)
        self.upsampling1 = nn.UpsamplingNearest2d(scale_factor=4)

        self.convLSTM = ConvLSTM(input_channels=512, hidden_channels=[256], kernel_size=3, step=num_frames,
                        effective_step=[4]).cuda()
        self.conv_atn2_1 = nn.Conv2d(256, 1, 1)
        self.upsampling2_1 = nn.UpsamplingNearest2d(scale_factor=4)
        self.upsampling2_2 = nn.UpsamplingNearest2d(scale_factor=2)

    def forward(self,x):
        out_batch=[]
        # [batch, num, c, h, w]->[batch, c, h, w]
        for img_seq  in torch.unbind(x, dim=1):
            feats =self.Vgg_feats(img_seq)

            attention = self.maxpool_atn1_1(feats)
            attention = F.relu(self.conv_atn1_1(attention))
            attention = F.relu(self.conv_atn1_2(attention))
            attention = self.maxpool_atn1_2(attention)
            attention = F.relu(self.conv_atn1_3(attention))
            attention = F.relu(self.conv_atn1_4(attention))
            attention = torch.sigmoid(self.conv_atn1_5(attention))
            attention = self.upsampling1(attention)

            # f_attention = attention.repeat(1, 512, 1, 1)
            f_attention = attention.repeat(1, 512, 1, 1).cuda()
            m_outs = f_attention * feats
            outs = feats + m_outs

            outs = outs.type(torch.FloatTensor)

            outs = self.convLSTM(outs.cuda())[0][0]
            outs = torch.sigmoid(self.conv_atn2_1(outs))
            outs = self.upsampling2_1(outs)
            out_batch.append(outs)
        out_batch = torch.stack(out_batch, dim=1)
        # attention = self.upsampling2_2(attention)
        # return [outs, attention]
        return out_batch

class ACLLoss(nn.Module):
    def __init__(self):
        super(ACLLoss, self).__init__()
        return

    # def forward(self, y_pred, y_sal, y_fix):
    def forward(self, y_pred, y_sal):
        y_pred = F.normalize(y_pred, p=1, dim=[2, 3])
        y_sal = F.normalize(y_sal, p=1, dim=[2, 3])
        loss_kl = self.kl_divergence(y_sal, y_pred)
        loss_cc = self.correlation_coefficient(y_sal, y_pred)
        # loss_nss = self.nss(y_sal, y_fix)
        # loss = 10 * loss_kl + loss_cc + loss_nss
        # return loss, loss_kl, loss_cc, loss_nss
        loss =  10*loss_kl + 0.1*loss_cc   # 不应该是减去cc吗
        return loss

    def kl_divergence(self, y_sal, y_pred):
        loss = torch.sum(y_sal * torch.log((y_sal + 1e-7) / (y_pred + 1e-7)))
        return loss

    def correlation_coefficient(self, y_sal, y_pred):
        N = y_pred.size()[2] * y_pred.size()[3]
        sum_prod = torch.sum(y_sal * y_pred, dim=[2, 3])
        sum_x = torch.sum(y_sal, dim=[2, 3])
        sum_y = torch.sum(y_pred, dim=[2, 3])
        sum_x_square = torch.sum(y_sal**2, dim=[2, 3]) + 1e-7
        sum_y_square = torch.sum(y_pred**2, dim=[2, 3]) + 1e-7
        num = sum_prod - ((sum_x * sum_y) / N)
        den = torch.sqrt((sum_x_square - sum_x**2 / N) * (sum_y_square - sum_y**2 / N))
        loss = torch.sum(-2 * num / den)  #
        return loss

    def nss(self, y_fix, y_pred):
        y_pred = F.layer_norm(y_pred, normalized_shape=y_pred.size()[2:])
        loss = -torch.sum(((torch.sum(y_fix * y_pred, dim=[2, 3])) / (torch.sum(y_fix, dim=[2, 3]))))
        return loss


import numpy as np
import utils
import os

################################################################################
#
# BaseMask class --> If you have a custom mask, you should inherit from BaseMask
#
################################################################################
class BaseMask(object):
    """BaseMask - Basic functionality to read a mask and filter points. """

    name = ''
    """ Mask name. """

    path = ''
    """ Mask path. """

    data = None
    """ Numpy array containing the mask data. """

    # ------------------------------------------------------------------------ #
    def __init__(self, name = '', path = ''):
        """ Initialize mask structure, and if given a name/path, load data. """

        data = self.load(name, path)

    # ------------------------------------------------------------------------ #
    def load(self, name = None, path = None):
        """ load data from mask, and update name/path if necessary.

            Usage: Mask = BaseMask()
                   Mask.load(name, path)

            Input:
                name - Mask name
                path - Mask path

            Output:
                -NONE- but self.data is updated with the mask data
        """

        if name is not None:
            self.name = name

        if path is not None:
            self.path = path

        # BaseMask assumes we're reading masks from an image type
        file = os.path.join(self.path, self.name)
        if os.path.exists(file):
            self.data = np.asarray(Image.open(file))

    # ------------------------------------------------------------------------ #
    def filter(self, pts2D):
        """ Filter image points based on loaded mask.

        Usage: Mask = BaseMask('mask.png')
               FilteredPts = Mask.filter(pts)

        Input:
            pts - 2-by-N numpy array of 2D points

        Output:
            FilteredPts - 2-by-N numpy array of 2D points. Those that agree
            with the mask (i.e., those points [x, y] s.t. mask[x,y] = True)
            remain the same, while invalid points are zeroed.
        """

        # Find points that are not zero in image space
        idxPts = utils.find( np.logical_and(pts2D[0, :] > 0, pts2D[1,:] > 0) )

        # Get rounded and flattened (1D) pixel indexes for our 2D points
        idxPtsFlat = utils.ravel_index(pts2D[::-1, idxPts].astype(int), \
                                       self.data.shape)

        # Find invalid 2D points by comparing them to the mask
        idxInvalidPts = utils.find( self.data.flat[idxPtsFlat] == 0 )

        # Set invalid points to zero
        pts2D[:, idxPts[idxInvalidPts]] = 0

        return pts2D

################################################################################
#
# MatMask class --> Custom read mask from matlab .mat file
#
################################################################################
class MatMask(BaseMask):
    """MatMask - Read mask from Matlab and filter points. """

    # ------------------------------------------------------------------------ #
    def load(self, name = None, path = None):
        """ load data from mask, and update name/path if necessary. """

        if name is not None:
            self.name = name

        if path is not None:
            self.path = path

        # BaseMask assumes we're reading masks from an image type
        file = os.path.join(self.path, self.name)
        if os.path.exists(file):
            self.data = utils.load(file, 'mask', Matlab_compatible = True)

################################################################################
#
# ImgMask class
#
################################################################################

# ImgMask is the same as BaseClass so far...
ImgMask = BaseMask

def MaskFromType(type):
    """ Mask selector from mask type. Returns most suitable MaskReader class for
    your needs given the image type.

    Usage: MaskReader = MaskFromType(type)

    Input:
        type - mask file extension. Accepted types are 'jpg', 'png', 'bmp' and
               'mat'.
    Output:
        MaskReader - Mask reader class associated with your mask type.
        Currently, it is either ImgMask or MatMask.
    """

    # in case we enter something that is not a type, keep the last three chars
    type = type[-3:]

    if type in ['jpg', 'png', 'bmp']:
        return ImgMask

    elif type in ['mat']:
        return MatMask


#!/usr/local/bin/python3

# avenir-python: Machine Learning
# Author: Pranab Ghosh
#
# Licensed under the Apache License, Version 2.0 (the "License"); you
# may not use this file except in compliance with the License. You may
# obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied. See the License for the specific language governing
# permissions and limitations under the License.

# Package imports
import os
import sys
import matplotlib.pyplot as plt
import numpy as np
import sklearn as sk
import matplotlib
import random
import jprops
from io import StringIO
from sklearn.model_selection import cross_val_score
import joblib
from random import randint
from io import StringIO
from sklearn.linear_model import LinearRegression
sys.path.append(os.path.abspath("../lib"))
from util import *
from mlutil import *
from pasearch import *

class BaseRegressor(object):
	"""
	base regression class
	"""

	def __init__(self, configFile, defValues):
		"""
		intializer
		"""
		defValues["common.mode"] = ("train", None)
		defValues["common.model.directory"] = ("model", None)
		defValues["common.model.file"] = (None, None)
		defValues["common.scale.file.path"] = (None, "missing scale file path")
		defValues["common.preprocessing"] = (None, None)
		defValues["common.verbose"] = (False, None)
		defValues["train.data.file"] = (None, "missing training data file")
		defValues["train.data.fields"] = (None, "missing training data field ordinals")
		defValues["train.data.feature.fields"] = (None, "missing training data feature field ordinals")
		defValues["train.data.out.field"] = (None, "missing out field ordinal")

		self.config = Configuration(configFile, defValues)
		self.featData = None
		self.outData = None
		self.regressor = None
		self.verbose = self.config.getBooleanConfig("common.verbose")[0]
		self.mode = self.config.getBooleanConfig("common.mode")[0]
		logFilePath = self.config.getStringConfig("common.logging.file")[0]
		logLevName = self.config.getStringConfig("common.logging.level")[0]
		self.logger = createLogger(__name__, logFilePath, logLevName)
		self.logger.info("********* starting session")

	def initConfig(self, configFile, defValues):
		"""
		initialize config
		"""
		self.config = Configuration(configFile, defValues)

	def getConfig(self):
		"""
		get config object
		"""
		return self.config

	def setConfigParam(self, name, value):
		"""
		set config param
		"""
		self.config.setParam(name, value)

	def getMode(self):
		"""
		get mode
		"""
		return self.mode

	def train(self):
		"""
		train model
		"""
		#build model
		self.buildModel()

		# training data
		if self.featData is None:
			(featData, outData) = self.prepData("train")
			(self.featData, self.outData) = (featData, outData)
		else:
			(featData, outData) = (self.featData, self.outData)

		# parameters
		modelSave = self.config.getBooleanConfig("train.model.save")[0]

		#train
		self.logger.info("...training model")
		self.regressor.fit(featData, outData)
		rsqScore = self.regressor.score(featData, outData)
		coef = self.regressor.coef_
		intc = self.regressor.intercept_
		result = (rsqScore, intc, coef)

		if modelSave:
			self.logger.info("...saving model")
			modelFilePath = self.getModelFilePath()
			joblib.dump(self.regressor, modelFilePath)
		return result

	def validate(self):
		# create model
		self.prepModel()

		# prepare test data
		(featData, outDataActual) = self.prepData("validate")

		#predict
		self.logger.info("...predicting")
		outDataPred = self.regressor.predict(featData)

		#error
		rsqScore = self.regressor.score(featData, outDataActual)
		result = (outDataPred, rsqScore)
		return result

	def predict(self):
		"""
		predict using trained model
		"""
		# create model
		self.prepModel()

		# prepare test data
		featData = self.prepData("predict")[0]

		#predict
		self.logger.info("...predicting")
		outData = self.regressor.predict(featData)
		return outData

	def prepData(self, mode):
		"""
		loads and prepares data for training and validation
		"""
		# parameters
		key = mode  + ".data.file"
		dataFile = self.config.getStringConfig(key)[0]

		key = mode  + ".data.fields"
		fieldIndices = self.config.getStringConfig(key)[0]
		if not fieldIndices is None:
			fieldIndices = strToIntArray(fieldIndices, ",")

		key = mode  + ".data.feature.fields"
		featFieldIndices = self.config.getStringConfig(key)[0]
		if not featFieldIndices is None:
			featFieldIndices = strToIntArray(featFieldIndices, ",")

		if not mode == "predict":
			key = mode  + ".data.out.field"
			outFieldIndex = self.config.getIntConfig(key)[0]

		#load data
		(data, featData) = loadDataFile(dataFile, ",", fieldIndices, featFieldIndices)
		if (self.config.getStringConfig("common.preprocessing")[0] == "scale"):
			featData = sk.preprocessing.scale(featData)
		outData = None
		if not mode == "predict":
			outData = extrColumns(data, outFieldIndex)
		return (featData, outData)

	def prepModel(self):
		"""
		load saved model or train model
		"""
		useSavedModel = self.config.getBooleanConfig("predict.use.saved.model")[0]
		if (useSavedModel and not self.regressor):
			# load saved model
			self.logger.info("...loading saved model")
			modelFilePath = self.getModelFilePath()
			self.regressor = joblib.load(modelFilePath)
		else:
			# train model
			self.train()

class LinearRegressor(BaseRegressor):
	"""
	linear regression
	"""
	def __init__(self, configFile):
		defValues = {}
		defValues["train.normalize"] = (False, None)

		super(LinearRegressor, self).__init__(configFile, defValues)

	def buildModel(self):
		"""
		builds model object
		"""
		self.logger.info("...building linear regression model")
		normalize = self.config.getBooleanConfig("train.normalize")[0]
		self.regressor = LinearRegression(normalize=normalize)

class ElasticNetRegressor(BaseRegressor):
	"""
	elastic net regression
	"""
	def __init__(self, configFile):
		defValues = {}
		defValues["train.alpha"] = (1.0, None)
		defValues["train.loneratio"] = (0.5, None)
		defValues["train.normalize"] = (False, None)
		defValues["train.precompute"] = (False, None)
		defValues["train.max.iter"] = (1000, None)
		defValues["train.tol"] = (0.0001, None)
		defValues["train.random.state"] = (None, None)
		defValues["train.selection"] = ("cyclic", None)

		super(ElasticNetRegressor, self).__init__(configFile, defValues)

	def buildModel(self):
		"""
		builds model object
		"""
		self.logger.info("...building elastic net regression model")
		alpha = self.config.getFloatConfig("train.alpha")[0]
		loneratio = self.config.getFloatConfig("train.loneratio")[0]
		normalize = self.config.getBooleanConfig("train.normalize")[0]
		precompute = self.config.getBooleanConfig("train.precompute")[0]
		maxIter = self.config.getIntConfig("train.max.iter")[0]
		tol = self.config.getFloatConfig("train.tol")[0]
		randState = self.config.getIntConfig("train.random.state")[0]
		selection = self.config.getIntConfig("train.selection")[0]

		self.regressor =  ElasticNet(alpha=alpha, l1_ratio=loneratio, normalize=normalize, precompute=precompute,
		max_iter=maxIter, tol=tol, random_state=randState, selection=selection)


#! /usr/bin/env python

'''
Issue Extension for Python-Markdown
==================================

Replace #1234 with link to issue 1234.

Usage
-----

    >>> import markdown
    >>> src = 'Please see issue #1234.'
    >>> html = markdown.markdown(src, ['tracker.mdx.mdx_issue'])
    >>> print(html)

Please see issue #1234.

    >>> html = markdown.markdown(src, ['tracker.mdx.mdx_issue'], {'tracker.mdx.mdx_issue': {'absolute_url': True}})
    >>> print(html)

Please see issue #1234.

Dependencies
------------

* [Markdown 2.0+](http://www.freewisdom.org/projects/python-markdown/)
'''

import markdown
from markdown.inlinepatterns import SubstituteTagPattern
from markdown.inlinepatterns import Pattern
from markdown.util import etree

from django.urls import reverse
from django.conf import settings

ISSUE_RE = r'#([0-9]+)'

class IssuePattern(Pattern):

    def __init__(self, project_name, absolute_url=False):
        super(IssuePattern, self).__init__(ISSUE_RE)
        self.project_name = project_name
        self.absolute_url = absolute_url

    def handleMatch(self, m):
        el = etree.Element('a')
        issue_id = m.group(2)
        url = reverse('show-issue', args=[self.project_name, issue_id])
        if self.absolute_url:
            url = settings.BASE_URL + url
        el.set('href', url)
        el.text = '#{issue_id}'.format(issue_id=issue_id)
        return el

class IssueExtension(markdown.extensions.Extension):

    def __init__(self, **kwargs):
        self.config = {
            'project_name': ['project', 'Project name'],
            'absolute_url': [False, 'Use absolute URL'],
        }
        super(IssueExtension, self).__init__(**kwargs)

    def extendMarkdown(self, md, md_globals):
        pattern = IssuePattern(self.getConfig('project_name'), self.getConfig('absolute_url'))
        md.inlinePatterns.add('issue', pattern, '<not_strong')

def makeExtension(**configs):
    return IssueExtension(configs=configs)


from flask import Flask, jsonify, request, make_response
from flask_migrate import Migrate
from flask_sqlalchemy import SQLAlchemy
from flask_restful import Api, Resource

from models import Restaurant, Pizza, RestaurantPizza, db

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///app.db'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config['JSONIFY_PRETTYPRINT_REGULAR'] = True
app.json.compact = False

migrate = Migrate(app, db, render_as_batch=True)
db.init_app(app)

api = Api(app)

# "landing" page
class Home(Resource):
    def get(self):
        response_dict = {
            "Home page": "Welcome to the Restauraunt page",
        }
        response = make_response(
            jsonify(response_dict),
            200,
        )
        return response

api.add_resource(Home, '/')

class Restaurants(Resource):
    # GET for restaurants
    def get(self):
        response_dict_list = [n.to_dict() for n in Restaurant.query.all()]
        response = make_response(
            jsonify(response_dict_list),
            200,
        )
        return response

    # POST for restaurants
    def post(self):
        new_restaurant = Restaurant(
            name=request.form['name'],
            address=request.form['address'],
        )

        db.session.add(new_restaurant)
        db.session.commit()

        response_dict= new_restaurant.to_dict()

        response = make_response(
            jsonify(response_dict),
            201,
        )
        return response

api.add_resource(Restaurants, '/restaurants')

class RestaurantById(Resource):
    # GET for restaurantbyID
    def get(self, id):
        restaurant = Restaurant.query.filter_by(id=id).first()
        if restaurant:
            response = make_response(
                jsonify(restaurant.to_dict()),
                200,
            )
            return response
        else:
            response = jsonify({"message": "Not Found"})
            return response, 404

    # DELETE for restaurantbyID
    def delete(self, id):
        restaurant = Restaurant.query.get(id)

        if restaurant:
            Restaurant.query.filter_by(id=id).delete

            db.session.delete(restaurant)
            db.session.commit()

            response = jsonify({"message": "Restaurant successfully deleted"})
            return response, 200
        else:
            response = jsonify({"message": "Not Found"})
            return response, 404

api.add_resource(RestaurantById, '/restaurants/<int:id>')

class Pizzas(Resource):
    # GET for Pizzas
    def get(self):
        response_dict_list = [n.to_dict() for n in Pizza.query.all()]

        response = make_response(
            jsonify(response_dict_list),
            200,
        )
        return response

api.add_resource(Pizzas, '/pizzas')

class RestaurantPizzas(Resource):

    def get(self):
        response_dict_list = [n.to_dict() for n in RestaurantPizza.query.all()]
        response = make_response(
            jsonify(response_dict_list),
            200,
        )
        return response

    def post(self):
        data = request.get_json()

        price=data.get('price')
        pizza_id=data.get('pizza_id')
        restaurant_id=data.get('restaurant_id')

        if pizza_id and restaurant_id:
            new_restaurant_pizza = RestaurantPizza(
                price=price,
                pizza_id=pizza_id,
                restaurant_id=restaurant_id
            )

            db.session.add(new_restaurant_pizza)
            db.session.commit()

            pizza = Pizza.query.get(pizza_id)
            if pizza:
                response = make_response(
                    {"id": pizza.id,
                    "ingredients" : pizza.ingredients,
                    "name" : pizza.name},
                    201
                )
                return response

            else:
                response = jsonify({"message": "Not Found"})
                return response, 404

api.add_resource(RestaurantPizzas, '/restaurant_pizzas')

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


import pygame
from data.menu import font

SEED = ''
gameplaySprites = pygame.sprite.Group()

def startGame(screen, sizeOfScreen):
    pygame.draw.rect(screen, pygame.Color('gray'),
                     (sizeOfScreen * 0.1, sizeOfScreen * 0.1,
                      sizeOfScreen * 0.8, sizeOfScreen * 0.9))

def drawSeed(screen, sizeOfScreen):
    if SEED:
        pygame.draw.rect(screen, pygame.Color('black'),
                         (sizeOfScreen * 0.57, sizeOfScreen * 0.8,
                          sizeOfScreen * 0.4, sizeOfScreen * 0.1))
    text = font.render(SEED, False, (100, 255, 100))
    screen.blit(text, (sizeOfScreen * 0.58, sizeOfScreen * 0.805))

def addToGameplaySprites(image, x, y, xSize=None, ySize=None, colorkey=None):
    from data.imageManager import load_image
    sprite = pygame.sprite.Sprite(gameplaySprites)
    image = load_image(image, colorkey)
    if xSize and ySize:
        image = pygame.transform.scale(image, (xSize, ySize))
    sprite.image = image
    sprite.rect = sprite.image.get_rect()
    sprite.rect.x = x
    sprite.rect.y = y

def defineWinners(players):
    sp = list()
    for i in players:
        sp.append([i.name, i.treasure, i])
    sp.sort(key=lambda x: x[1], reverse=True)
    return sp

def endGameConclusion(players):
    from main import MAP_SIZE
    sp = list()
    with open('Statistics.txt', 'w', encoding='utf8') as file:
        for i in players:
            sp.append([i.name, i.treasure])
            file.write(f"{i.name}: \n")
            file.write(f'Накопил {i.treasure} золота\n')
            file.write(f'Завоевал {len(i.getTiles())} клеток\n')
            file.write(
                f'Оккупировано '
                f'{round(len(i.getTiles()) / ((MAP_SIZE + 1) ** 2 / 100), 2)}%'
                f' карты\n\n')
    sp.sort(key=lambda x: x[1], reverse=True)
    print('Финальные результаты:')
    for i in range(len(players)):
        print(f'{i + 1} место - {sp[i][0]}, с результатом в {sp[i][1]} золота')

def renderFinale(screen, players, sizeOfScreen):
    sp = list()
    font = pygame.font.Font(None, 50)
    for i in players:
        sp.append([i.name, i.treasure])
    sp.sort(key=lambda x: x[1], reverse=True)
    for i in range(len(players)):
        color = (255, 215, 0) if not i else (197, 201, 199)\
            if i == 1 else (205, 127, 50) if i == 2 else (255, 255, 255)
        line = f'{i + 1} место - {sp[i][0]}'
        text = font.render(line, True, color)
        screen.blit(text, (sizeOfScreen * 0.05, sizeOfScreen * 0.2 + 200 * i))
        line = f'{sp[i][1]} золота'
        text = font.render(line, True, color)
        screen.blit(text, (sizeOfScreen * 0.05,
                           sizeOfScreen * 0.2 + 200 * i + 70))
    drawFlagsFinale(screen, players, sizeOfScreen)

def drawFlagsFinale(screen, players, sizeOfScreen):
    winnerColor = defineWinners(players)[0][-1].getColor()
    pygame.draw.rect(screen, '#ffdc33', (
        0, sizeOfScreen * .01, sizeOfScreen, sizeOfScreen * .08))

    pygame.draw.rect(screen, winnerColor, (
        0, sizeOfScreen * .025, sizeOfScreen, sizeOfScreen * .05))

    sizeOfScreenY = sizeOfScreen + sizeOfScreen * 0.15

    pygame.draw.rect(screen, '#ffdc33', (
        0, sizeOfScreenY * .9, sizeOfScreen, sizeOfScreenY * .08))

    pygame.draw.rect(screen, winnerColor, (
        0, sizeOfScreenY * .915, sizeOfScreen, sizeOfScreenY * .05))

def slideEnd(screen, sizeOfScreen, cords, fps):
    if not cords:
        return
    if cords == [500, 0]:
        cords[0] = -sizeOfScreen
    if cords[0] > 0:
        return False
    pygame.draw.rect(
        screen, (0, 0, 0), (cords[0], cords[1], sizeOfScreen + 5,
                            sizeOfScreen + sizeOfScreen * 0.15))
    cords[0] += 500 / fps
    return cords

def addNumToSeed(num):
    global SEED
    if len(SEED) < 6:
        SEED += num

def delNumFromSeed():
    global SEED
    if len(SEED) > 0:
        SEED = SEED[:-1]


# # import datetime
from plyer import notification

import pync
import time
import os
from pync import Notifier
import datetime

operatingsystem = input(
    "PRESS W if you are using Windows and U if your using Apple, linux or Unix based operating system\n\n")

today = datetime.datetime.now().strftime("%H")
minutes = datetime.datetime.now().strftime("%M")
seconds = datetime.datetime.now().strftime("%S")
Year = datetime.datetime.now().strftime("%y")

print("The current time is "+f"{int(today)}" +
      ":"+minutes + ":"+seconds + "\n")
# print(Year)
water = input("Enter the when to remind you to drink water after every:\n")
print(f"You will be reminded after {water} hours to drink water\n")
waterr = str(int(water) + int(today))

# print(int(waterr))
# if(today == waterr):
#     print("omsairam")
d = ''
if(int(waterr) == 25) or (int(waterr) > 25):
    d = int(waterr) - 24
    waterr == d

# print(waterr)

if(int(waterr) == int(today)):

    if(operatingsystem == "U"):
        if __name__ == "__main__":
            pync.notify('It is so important to stay hydrated during our day, not only to replenish the water that we lose throughout the day, but drinking more water can aid in weight loss, manage diabetes, essential in pregnancy and also keeps your teeth, skin and gums healthy.',
                        title=f"Time to drink water its already {today}:{minutes}:{seconds} ",
                        activate='com.apple.Safari',
                        subtitle="Sip before every meal.",
                        open='http://github.com/',
                        execute='say "ok but do not forget to drink water"',

                        icon='pngfuel.com-19.png',
                        timeout=1
                        )
    elif(operatingsystem == "W"):
        if __name__ == "__main__":
            notification.notify(
                title=f"Time to drink water its already {today}:{minutes}:{seconds} ",
                message='It is so important to stay hydrated during our day, not only to replenish the water that we lose throughout the day, but drinking more water can aid in weight loss, manage diabetes, essential in pregnancy and also keeps your teeth, skin and gums healthy.',
                app_icon='pngfuel.com-19.png',
                timeout=3
            )

elif(int(waterr) > int(today)):
    time.sleep(int(waterr)*60*60)
    if(operatingsystem == "U"):
        if __name__ == "__main__":
            pync.notify('It is so important to stay hydrated during our day, not only to replenish the water that we lose throughout the day, but drinking more water can aid in weight loss, manage diabetes, essential in pregnancy and also keeps your teeth, skin and gums healthy.',
                        title=f"Time to drink water its already {today}:{minutes}:{seconds} ",
                        activate='com.apple.Safari',
                        subtitle="Sip before every meal.",
                        open='http://github.com/',
                        execute='say "ok but do not forget to drink water"',

                        icon='pngfuel.com-19.png',
                        timeout=1
                        )

    elif(operatingsystem == "W"):
        time.sleep(int(water)*60*60)
        if __name__ == "__main__":
            notification.notify(
                title=f"Time to drink water its already {today}:{minutes}:{seconds} ",
                message='It is so important to stay hydrated during our day, not only to replenish the water that we lose throughout the day, but drinking more water can aid in weight loss, manage diabetes, essential in pregnancy and also keeps your teeth, skin and gums healthy.',
                app_icon='pngfuel.com-19.png',
                timeout=3
            )

            # Random notes :)

    # if __name__ == "__main__":
    #     pync.notify('Hello World',
    #                 title="Omsai",
    #                 subtitle="hello this is developer king pro",

    #                 icon='pngfuel.com-19.png',
    #                 )

# pync.notify('Hello World', title='Python')
# pync.notify('Hello World', group=os.getpid())
# pync.notify('Hello World', activate='com.apple.Safari')
# pync.notify('Hello World', open='http://github.com/')
# pync.notify('Hello World', execute='say "OMG"')

'''
Notifier.remove(os.getpid())

Notifier.list(os.getpid())
'''
# d = input("enter the number\n")
# # print(type
# #       (d))
# numm = int(d)
# # print(type(numm))
# one = (numm * (numm + 1))
# print(one/2)
# a = str(0)
# print(a)

# # today = datetime.datetime.now().strftime("%d-%m-%y")
# # Year = datetime.datetime.now().strftime("%y")
# # print(today)
# # print(Year)

# # if __name__ == "__main__":
# #     notification.notify(
# #         title="Time to Have ",
# #         message="its better to have  than to seleep",
# #         app_icon="/Users/tarushj.reddy/Desktop/Water/pngfuel.com-19.png",
# #         timeout=3
# #     )

# '''this is an example system '''
# # import os

# # def notify(title, text):
# #     os.system("""
# #             osascript -e 'display notification "{Thsis jcsdojcosd}" with title "{}" sound name
# #             {}'""".format(text, title))

# # notify("Title", "Heres an alert")


from django import forms

from ...account.models import User

class CustomerForm(forms.ModelForm):
    def __init__(self, *args, **kwargs):
        self.user = kwargs.pop('user', None)
        super().__init__(*args, **kwargs)
        # disable 'is_active' checkbox if user edits his own account
        if self.user == self.instance:
            self.fields['is_active'].disabled = True

    class Meta:
        model = User
        fields = ['email', 'is_active']


from django.urls import path, include
from . import views
from rest_framework import routers

router = routers.DefaultRouter()
router.register('customer', views.CustomerView)
router.register('person', views.person)

urlpatterns = [

  path('class/', include(router.urls)),

  # Api for .Customer model using function INDIVIDUALLY
  path('customer_listind/', views.Customer_list_seprately),
  path('customer_add/', views.add),
  path('customer_update/<int:id>/', views.update),
  path('customer_delete/<int:id>/', views.delete),

# Api for .Customer model using function COMBINED
  path('customer/', views.Customer_list),
  path('customer/<int:id>/', views.update_delete),

  # Api for Crud.Person model using function COMBINED
  path('person_list/', views.person_list_function),
  path('person_list/<int:id>/', views.person_update_delete),

  ]


import pytest
from flask_sqlalchemy import SQLAlchemy

@pytest.fixture
def uses_sql_database(_db: SQLAlchemy) -> None:
    _db.session.commit()
    _db.drop_all()
    _db.create_all()

@pytest.fixture
def minimal_patient() -> dict:
    return {
        "accessibility_considerations": [],
        "allowed_to_text": True,
        "dh_products": [
            {
                "opened_date": "2019-04-29",
                "product_name": "GDM",
            }
        ],
        "dob": "1992-04-23",
        "email_address": "",
        "ethnicity": "185988007",
        "first_name": "Diane",
        "highest_education_level": "426769009",
        "hospital_number": "147777799",
        "last_name": "Smith",
        "locations": ["location_uuid_1"],
        "other_notes": "",
        "personal_addresses": [
            {
                "address_line_1": "School House Stony Bridge Park",
                "address_line_2": "",
                "address_line_3": "",
                "address_line_4": "",
                "lived_from": "2013-12-31",
                "locality": "Oxford",
                "postcode": "OX5 3NA",
                "region": "Oxfordshire",
            }
        ],
        "phone_number": "07123456789",
        "record": {},
        "sex": "248152002",
        "height_in_mm": 1666,
        "weight_in_g": 68800,
    }


from django.shortcuts import render, HttpResponse
from chats.audio.audioEncryption import audioEncrypt
from login.models import chatUsers
from django.http import HttpResponseRedirect
from chats.audio.keyGeneration import keyGeneration
from chats.models import KeyRoom
from chats.models import Chats
from kellanb_cryptography import aes
import json

# Create your views here.

def index(request, userId):
    if request.session.has_key("user") == False:
        return HttpResponseRedirect("/")
    context = {}
    context["User"] = chatUsers.objects.exclude(id=int(userId))
    context["Id"] = userId
    # return HttpResponse("This is Response")
    return render(request, "chats/chat.html", context)

def audio(request):
    if request.method == "POST":
        if request.FILES.get("myAudio", False):
            group_id = request.POST.get("id")
            handleUploadFile(request.FILES["myAudio"])
            keyObject = KeyRoom.objects.filter(groupId=group_id)
            if keyObject.exists:

                path = audioEncrypt(
                    keyObject[0].key)
            else:
                Key = keyGeneration()
                KeyRoom.objects.create(groupId=group_id, key=Key)
                path = audioEncrypt(Key)
            return HttpResponse(path)
        return HttpResponse("NO FILE FOUND")

def handleUploadFile(f):
    with open("chats/audio/" + f.name, "wb+") as destination:
        for chunk in f.chunks():
            destination.write(chunk)

def chats(request, groupId):
    chats = Chats.objects.filter(groupId=groupId)
    if chats.exists:
        data = list(chats.values())
        keyObject = KeyRoom.objects.filter(groupId=groupId)[0].key
        for i in range(len(data)):
            if data[i]['Audio'] == None:
                data[i]['EncryptedText'] = aes.decrypt_aes(
                    data[i]['EncryptedText'], keyObject)
        return HttpResponse(json.dumps(data))


import pandas as pd
import matplotlib
matplotlib.use('agg')
import seaborn as sns

class GraphGenerator():

    def bar_graph(self, data, filename, x, y, hue, y_label):
        graph = sns.catplot(x=x, y=y, hue=hue, data=data,
                        height=6, kind="bar", palette="muted")
        graph.despine(left=True)
        graph.set_ylabels(y_label)
        graph.fig.savefig(filename)


from builtins import print

from selenium import webdriver
import time
#1 Open browser
from selenium.webdriver import ActionChains

driver = webdriver.Chrome()
#2 Enter URL(bluestone)
driver.get("https://www.bluestone.com/")
driver.maximize_window()
driver.implicitly_wait(12)

#3 click on goldmine 10+1 Scheme
driver.find_element_by_xpath("//a[@title='Gold Mine 10+1 Monthly Plan']").click()
#Scroll Down to start now button
driver.execute_script("window.scrollTo(0,500)")
time.sleep(1)
#4 with out entering monthly amount and email address and click on start now
driver.find_element_by_xpath("//input[@id='gmsStart']").click()

#5 verify whether error message is displaying or not .Email_error
errormsg_amnt = driver.find_element_by_xpath("//ul[@id='amount_error']").text
errormsg_email = driver.find_element_by_xpath("//ul[@id='Email_error']").text
actualerror_amnt = "Amount is required"
actualerror_email = "Email is required"
if actualerror_amnt == errormsg_amnt and actualerror_email == errormsg_email:
    print("Error Message displayed successfully")
else:
    print("Error message not displayed")
print(errormsg_amnt)
print(errormsg_email)
driver.close()


from django.http import HttpResponse
from django.shortcuts import render
from pytube import YouTube
import os
def index(request):
    return render(request,"index.html")

def result(request):
    link=request.GET.get('text','default')
    url = link
    global url2
    url2 = link

    resolutions=[]
    # params={"vlink":link}
    yt = YouTube(link)
    al=yt.streams.filter(progressive=True,file_extension='mp4').all()
    for i in al:
        resolutions.append(i.resolution)
    # yt = yt.streams.filter(progressive=True, file_extension='mp4',res="360p").desc().first()
    # yt.download('a.mp4')
    resolutions=list(dict.fromkeys(resolutions))
    link = url.replace("watch?v=", "embed/")
    params={"reso":resolutions,"vlink":link}
    print(resolutions)
    return render(request,"result.html",params)

def download(request,res):
    global url2
    print(url2)
    homedir=os.path.expanduser("~")
    dirs=homedir + '/Downloads'
    print(dirs)

    YouTube(url2).streams.get_by_resolution(res).download(homedir + '/Downloads')
    return render(request,"download.html")


import cv2
import torch
import numpy as np
import pandas as pd
from torch.utils.data import Sampler, BatchSampler

# https://www.kaggle.com/gaborfodor/greyscale-mobilenet-lb-0-892
BASE_SIZE = 256
def list2drawing(raw_strokes, size=256, lw=6, time_color=False):
    img = np.zeros((BASE_SIZE, BASE_SIZE), np.uint8)
    for t, stroke in enumerate(raw_strokes):
        for i in range(len(stroke[0]) - 1):
            color = 255 - min(t, 10) * 13 if time_color else 255
            _ = cv2.line(img, (stroke[0][i], stroke[1][i]),
                         (stroke[0][i + 1], stroke[1][i + 1]), color, lw)
    if size != BASE_SIZE:
        return cv2.resize(img, (size, size), interpolation=cv2.INTER_LINEAR)
    else:
        return img

def drawing2tensor(drawing):
    rgb = cv2.cvtColor(drawing,cv2.COLOR_GRAY2RGB)
    rgb = rgb.transpose(2,0,1).astype(np.float32)
    return torch.from_numpy(rgb)

def channels2tensor(ch1, ch2, ch3):
    rgb = np.stack((ch1, ch2, ch3)).astype(np.float32)
    return torch.from_numpy(rgb)

# https://www.tensorflow.org/tutorials/sequences/recurrent_quickdraw
def drawing2seq(inkarray):
    stroke_lengths = [len(stroke[0]) for stroke in inkarray]
    total_points = sum(stroke_lengths)
    np_ink = np.zeros((total_points, 3), dtype=np.float32)
    current_t = 0
    if not inkarray:
        print("Empty inkarray")
        return None, None
    for stroke in inkarray:
        if len(stroke[0]) != len(stroke[1]):
            print("Inconsistent number of x and y coordinates.")
            return None, None
        for i in [0, 1]:
            np_ink[current_t:(current_t + len(stroke[0])), i] = stroke[i]
        current_t += len(stroke[0])
        np_ink[current_t - 1, 2] = 1    # stroke_end
    # Preprocessing.
    # 1. Size normalization.
    lower = np.min(np_ink[:, 0:2], axis=0)
    upper = np.max(np_ink[:, 0:2], axis=0)
    scale = upper - lower
    scale[scale == 0] = 1
    np_ink[:, 0:2] = (np_ink[:, 0:2] - lower) / scale
    # 2. Compute deltas.
    np_ink[1:, 0:2] -= np_ink[0:-1, 0:2]
    np_ink = np_ink[1:, :]
    return np_ink

# https://github.com/benhamner/Metrics/blob/master/Python/ml_metrics/average_precision.py
def apk(actual, predicted, k=10):
    if len(predicted)>k:
        predicted = predicted[:k]

    score = 0.0
    num_hits = 0.0

    for i,p in enumerate(predicted):
        if p in actual and p not in predicted[:i]:
            num_hits += 1.0
            score += num_hits / (i+1.0)

    if not actual:
        return 0.0

    return score / min(len(actual), k)

def mapk(actual, predicted, k=10):
    return np.mean([apk(a,p,k) for a,p in zip(actual, predicted)])

def map3(preds, targs):
    predicted_idxs = preds.sort(descending=True)[1]
    top_3 = predicted_idxs[:, :3]
    res = mapk([[t] for t in targs.cpu().numpy()], top_3.cpu().numpy(), 3)
    return torch.tensor(res)

def top_3_preds(preds): return np.argsort(preds.numpy())[:, ::-1][:, :3]

def top_3_pred_labels(preds, classes):
    top_3 = top_3_preds(preds)
    labels = []
    for i in range(top_3.shape[0]):
        labels.append(' '.join([classes[idx] for idx in top_3[i]]))
    return labels

def chunks(l, n):
    """Yield successive n-sized chunks from l."""
    for i in range(0, len(l), n):
        yield l[i:i + n]

class RandomSamplerWithEpochSize(Sampler):
    """Yields epochs of specified sizes. Iterates over all examples in a data_source in random
    order. Ensures (nearly) all examples have been trained on before beginning the next iteration
    over the data_source - drops the last epoch that would likely be smaller than epoch_size.
    """
    def __init__(self, data_source, epoch_size):
        self.n = len(data_source)
        self.epoch_size = epoch_size
        self._epochs = []
    def __iter__(self):
        return iter(self.next_epoch)
    @property
    def next_epoch(self):
        if len(self._epochs) == 0: self.generate_epochs()
        return self._epochs.pop()
    def generate_epochs(self):
        idxs = [i for i in range(self.n)]
        np.random.shuffle(idxs)
        self._epochs = list(chunks(idxs, self.epoch_size))[:-1]
    def __len__(self):
        return self.epoch_size

def create_submission(test_preds, test_dl, name, classes):
    key_ids = [path.stem for path in test_dl.dataset.x.items]
    labels = top_3_pred_labels(test_preds, classes)
    sub = pd.DataFrame({'key_id': key_ids, 'word': labels})
    sub.to_csv(f'subs/{name}.csv.gz', index=False, compression='gzip')


import numpy as np

from common import projection_back

EPS = 1e-9

def ilrma(mix, n_iter, n_basis=2, proj_back=True):
    """Implementation of ILRMA (Independent Low-Rank Matrix Analysis).
    This algorithm is called ILRMA1 in http://d-kitamura.net/pdf/misc/AlgorithmsForIndependentLowRankMatrixAnalysis.pdf
    It only works in determined case (n_sources == n_channels).

    Args:
        mix (numpy.ndarray): (n_frequencies, n_channels, n_frames)
            STFT representation of the observed signal.
        n_iter (int): Number of iterations.
        n_basis (int): Number of basis in the NMF model.
        proj_back (bool): If use back-projection technique.

    Returns:
        tuple[numpy.ndarray, numpy.ndarray]: Tuple of separated signal and
            separation matrix. The shapes of separated signal and separation
            matrix are (n_frequencies, n_sources, n_frames) and
            (n_sources, n_channels), respectively.
    """
    n_freq, n_src, n_frame = mix.shape

    sep_mat = np.stack([np.eye(n_src, dtype=mix.dtype) for _ in range(n_freq)])
    basis = np.abs(np.random.randn(n_src, n_freq, n_basis))
    act = np.abs(np.random.randn(n_src, n_basis, n_frame))
    sep = sep_mat @ mix
    sep_pow = np.power(np.abs(sep), 2)  # (n_freq, n_src, n_frame)
    model = basis @ act  # (n_src, n_freq, n_frame)
    m_reci = 1 / model

    eye = np.tile(np.eye(n_src), (n_freq, 1, 1))

    for _ in range(n_iter):
        for src in range(n_src):
            h = (sep_pow[:, src, :] * m_reci[src]**2) @ act[src].T
            h /= m_reci[src] @ act[src].T
            h = np.sqrt(h, out=h)
            basis[src] *= h
            np.clip(basis[src], a_min=EPS, a_max=None, out=basis[src])

            model[src] = basis[src] @ act[src]
            m_reci[src] = 1 / model[src]

            h = basis[src].T @ (sep_pow[:, src, :] * m_reci[src]**2)
            h /= basis[src].T @ m_reci[src]
            h = np.sqrt(h, out=h)
            act[src] *= h
            np.clip(act[src], a_min=EPS, a_max=None, out=act[src])

            model[src] = basis[src] @ act[src]
            m_reci[src] = 1 / model[src]

            h = m_reci[src, :, :, None] @ np.ones((1, n_src))
            h = mix.conj() @ (mix.swapaxes(1, 2) * h)
            u_mat = h.swapaxes(1, 2) / n_frame
            h = sep_mat @ u_mat + EPS * eye
            sep_mat[:, src, :] = np.linalg.solve(h, eye[:, :, src]).conj()
            h = sep_mat[:, src, None, :] @ u_mat
            h = (h @ sep_mat[:, src, :, None].conj()).squeeze(2)
            sep_mat[:, src, :] = (sep_mat[:, src, :] / np.sqrt(h).conj())

        np.matmul(sep_mat, mix, out=sep)
        np.power(np.abs(sep), 2, out=sep_pow)
        np.clip(sep_pow, a_min=EPS, a_max=None, out=sep_pow)

        for src in range(n_src):
            lbd = np.sqrt(np.sum(sep_pow[:, src, :]) / n_freq / n_frame)
            sep_mat[:, src, :] /= lbd
            sep_pow[:, src, :] /= lbd ** 2
            model[src] /= lbd ** 2
            basis[src] /= lbd ** 2

    # Back-projection technique
    if proj_back:
        z = projection_back(sep, mix[:, 0, :])
        sep *= np.conj(z[:, :, None])

    return sep, sep_mat


from .models import Message, Friend
from user.models import GroupPaticipant, CustomUser, CustomGroup
from django.db.models.signals import pre_save
from rest_framework import exceptions

def checkMessage(sender, instance, *args, **kwargs):

    try:
        user = CustomUser.objects.get(username=instance.sender)
        friend = CustomUser.objects.get(username=instance.receiver)
        Friend.objects.all().filter(user=user, friend=friend).exists()
    except CustomUser.DoesNotExist:
        try:
            group = CustomGroup.objects.get(name=instance.receiver)
            GroupPaticipant.objects.all().filter(group=group, user=user).exists()
        except CustomGroup.DoesNotExist:
            raise exceptions.ValidationError(f'friends with {instance.message} or group does not exist')

pre_save.connect(checkMessage, sender=Message)


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

class GCN(nn.Module):
    def __init__(self, in_features, hidden_features, out_features):
        super(GCN, self).__init__()
        self.embed1 = nn.Linear(in_features, hidden_features)
        self.embed2 = nn.Linear(in_features, hidden_features)
        self.relu = nn.ReLU()
        self.softmax = nn.Softmax(dim=-1)
        self.linear = nn.Linear(in_features, hidden_features)
        self.linear2 = nn.Linear(hidden_features, out_features)
        self.linear3 = nn.Linear(in_features, out_features)

    def forward(self, x):
        e1 = self.embed1(x) # (B,N,K,T)
        e2 = self.embed2(x).permute(0,1,3,2) # (B,N,T,K)
        adj = self.relu(torch.matmul(e1, e2))
        # x = self.linear(x)
        x = torch.matmul(adj, x) # (B,N,K,K)*(B,N,K,T) --> (B,N,K,T)
        x = self.linear3(x)
        # x = F.leaky_relu(x)
        # x = self.linear2(x)

        return x

if __name__ == '__main__':

    features = torch.randn(32,8,6,96)
    gcn = GCN(in_features=96, hidden_features=16, out_features=2)
    output = gcn(features)

    print(output.shape)


from flask import Flask
from random import randint

app = Flask(__name__)
rand = randint(0, 9)
@app.route("/")
def main():
    return '
Guess a number between 0 and 9
' \
           '' \
           '
Add /n in the url where n is your guessed number then hit enter
'

@app.route("/<int:number>")
def random_number(number):

    if rand == number:
        return '
You got it right!
' ''

    elif rand > number:
        return '
Too Low
' ''

    else:
        return '
Too High
' ''

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


import torch
from torch import nn
import torch.nn.functional as F
import math

class MSA(nn.Module):
    def __init__(self, dmodel, h):
        super().__init__()

        self.dmodel = dmodel
        self.h = h
        self.toK = nn.Linear(dmodel, dmodel, bias=False)
        self.toQ = nn.Linear(dmodel, dmodel, bias=False)
        self.toV = nn.Linear(dmodel, dmodel, bias=False)
        self.unify = nn.Linear(dmodel, dmodel)

    def forward(self, x):

        b, n, dmodel = x.size()
        h = self.h
        d = int(dmodel / h)

        K = self.toK(x).view(b, n, h, d)
        Q = self.toQ(x).view(b, n, h, d)
        V = self.toV(x).view(b, n, h, d)

        K = K.transpose(1, 2).contiguous().view(b * h, n, d)
        Q = Q.transpose(1, 2).contiguous().view(b * h, n, d)
        V = V.transpose(1, 2).contiguous().view(b * h, n, d)

        dot = torch.bmm(Q, K.transpose(1, 2))  # b*h x n x n = b*h x n x d @ b*h x d x n
        dot = dot / math.sqrt(d)  # b*h x n x n
        dot = F.softmax(dot, dim=2)  # b*h x n x n

        out = torch.bmm(dot, V).view(
            b, h, n, d
        )  # b*h x n x d = b*h x n x n @ b*h x n x d
        out = out.transpose(1, 2).contiguous().view(b, n, h * d)

        return self.unify(out)  # b x n x dmodel

    def forward_einsum(self, x):

        b, n, dmodel = x.size()
        h = self.h
        d = int(dmodel / h)

        K = self.toK(x).view(b, n, h, d)
        Q = self.toQ(x).view(b, n, h, d)
        V = self.toV(x).view(b, n, h, d)

        dot = torch.einsum("bthe,bihe->bhti", Q, K) / math.sqrt(d)  # b x h x n x n
        dot = F.softmax(dot, dim=-1)  # b x h x n x n

        out = torch.einsum("bhtd,bdhe->bthe", dot, V)  # b x n x h x d
        out = out.reshape(b, n, -1)  # b x n x h*d
        return self.unify(out)  # b x n x dmodel

class EncoderBlock(nn.Module):
    def __init__(self, dmodel: int, h: int, hidden_mult, dropout):
        super().__init__()

        self.ln1 = nn.LayerNorm(dmodel)
        self.msa = MSA(dmodel, h)
        self.do1 = nn.Dropout(dropout)

        self.ln2 = nn.LayerNorm(dmodel)
        self.mlp = nn.Sequential(
            nn.Linear(dmodel, hidden_mult * dmodel),
            nn.GELU(),
            nn.Linear(hidden_mult * dmodel, dmodel),
        )
        self.do2 = nn.Dropout(dropout)

    def forward(self, x):

        z = self.do1(self.msa(self.ln1(x))) + x
        return self.do2(self.msa(self.ln1(z))) + z

class PatchEmbeddings(nn.Module):
    def __init__(self, dmodel: int, patch_size: int, in_channels: int):
        super().__init__()

        self.conv = nn.Conv2d(in_channels, dmodel, patch_size, stride=patch_size)

    def forward(self, x):

        x = self.conv(x)
        b, dmodel, h, w = x.shape
        x = x.permute(0, 2, 3, 1)  # b x h x w x dmodel
        return x.view(b, h * w, dmodel)  # b x h*w x dmodel

class PositionalEmbeddings(nn.Module):
    def __init__(self, dmodel: int, max_len: int = 1000):
        super().__init__()

        self.pos_enc = nn.Parameter(torch.zeros(1, max_len, dmodel), requires_grad=True)

    def forward(self, x):  # b x h*w x dmodel

        seqlen = x.shape[1]
        return x + self.pos_enc[:, :seqlen, :]  # b x h*w x dmodel

class ClassificationHead(nn.Module):
    def __init__(self, dmodel: int, n_hidden: int, n_classes: int):
        super().__init__()

        self.linear1 = nn.Linear(dmodel, n_hidden)
        self.act = nn.ReLU()
        self.linear2 = nn.Linear(n_hidden, n_classes)

    def forward(self, x):  # b x dmodel

        return self.linear2(self.act(self.linear1(x)))  # b x n_classes (logits)

class VisionTransformer(nn.Module):
    def __init__(
        self,
        dmodel: int,
        # EncoderBlocks
        h: int,
        hidden_mult: int,
        dropout: float,
        L: int,
        # PatchEmbeddings
        patch_size: int,
        in_channels: int,
        # ClassificationHead
        n_hidden: int,
        n_classes: int,
    ):
        super().__init__()

        self.patch_emb = PatchEmbeddings(dmodel, patch_size, in_channels)
        self.pos_emb = PositionalEmbeddings(dmodel)
        self.classification = ClassificationHead(dmodel, n_hidden, n_classes)
        self.blocks = nn.ModuleList(
            [EncoderBlock(dmodel, h, hidden_mult, dropout) for _ in range(L)]
        )
        self.cls_emb = nn.Parameter(torch.randn(1, 1, dmodel), requires_grad=True)
        self.ln = nn.LayerNorm(dmodel)

    def forward(self, x):  # b x c x H x W

        x = self.patch_emb(x)  # b x h*w x dmodel
        x = self.pos_emb(x)  # b x h*w x dmodel

        b = x.shape[0]
        cls_emb = self.cls_emb.expand(b, -1, -1)  # b x 1 x dmodel
        z = torch.cat([cls_emb, x], dim=1)  # b x (h*w)+1 x dmodel

        for block in self.blocks:
            z = block(z)

        y = self.ln(z[:, 0, :])  # b x dmodel
        return self.classification(y)  # b x n_classes (logits)


#! /usr/bin/env python
# -*- coding: utf-8 -*-
from django.shortcuts import render, redirect
from django_celery_beat.models import PeriodicTask, IntervalSchedule, CrontabSchedule
from django_celery_results.models import TaskResult
from django.contrib.auth.decorators import login_required
from accounts.permission import permission_verify
from cmdb.api import get_object
from setup.forms import PeriodicTaskForm, IntervalForm, CrontabForm, TaskResultForm
from subprocess import Popen, PIPE
import os, time

@login_required
@permission_verify()
def index(request):
    jobs_info = PeriodicTask.objects.all()
    return render(request, 'setup/job_list.html', locals())

@login_required
@permission_verify()
def job_edit(request, ids):
    status = 0
    obj = get_object(PeriodicTask, id=ids)

    if request.method == 'POST':
        form = PeriodicTaskForm(request.POST, instance=obj)
        if form.is_valid():
            form.save()
            status = 1
        else:
            status = 2
    else:
        form = PeriodicTaskForm(instance=obj)

    return render(request, 'setup/job_edit.html', locals())

@login_required()
@permission_verify()
def job_add(request):
    if request.method == "POST":
        a_form = PeriodicTaskForm(request.POST)
        if a_form.is_valid():
            a_form.save()
            tips = u"增加成功！"
            display_control = ""
            return redirect("/setup/joblist/")
        else:
            tips = u"增加失败！"
            display_control = ""
            return render(request, "setup/job_add.html", locals())
    else:
        display_control = "none"
        a_form = PeriodicTaskForm()
        return render(request, "setup/job_add.html", locals())

@login_required()
@permission_verify()
def job_del(request):
    if request.method == 'POST':
        jobs = request.POST.getlist('idc_check', [])
        if jobs:
            for n in jobs:
                PeriodicTask.objects.filter(id=n).delete()
    jobs_info = PeriodicTask.objects.all()
    return render(request, "setup/job_list.html", locals())

@login_required()
@permission_verify()
def job_interval_list(request):
    interval_info = IntervalSchedule.objects.all()
    return render(request, 'setup/interval_list.html', locals())

@login_required
@permission_verify()
def job_interval_edit(request, ids):
    status = 0
    obj = get_object(IntervalSchedule, id=ids)

    if request.method == 'POST':
        form = IntervalForm(request.POST, instance=obj)
        if form.is_valid():
            form.save()
            status = 1
        else:
            status = 2
    else:
        form = IntervalForm(instance=obj)

    return render(request, 'setup/interval_edit.html', locals())

@login_required
@permission_verify()
def job_interval_del(request):
    if request.method == 'POST':
        intervals = request.POST.getlist('idc_check', [])
        if intervals:
            for n in intervals:
                IntervalSchedule.objects.filter(id=n).delete()
    interval_info = IntervalSchedule.objects.all()
    return render(request, "setup/interval_list.html", locals())

@login_required()
@permission_verify()
def job_interval_add(request):
    if request.method == "POST":
        a_form = IntervalForm(request.POST)
        if a_form.is_valid():
            a_form.save()
            tips = u"增加成功！"
            display_control = ""
            return redirect("/setup/jobintervallist/")
        else:
            tips = u"增加失败！"
            display_control = ""
            return render(request, "setup/interval_add.html", locals())
    else:
        display_control = "none"
        a_form = IntervalForm()
        return render(request, "setup/interval_add.html", locals())

@login_required()
@permission_verify()
def job_crontab_list(request):
    crontab_info = CrontabSchedule.objects.all()
    return render(request, 'setup/crontab_list.html', locals())

@login_required
@permission_verify()
def job_crontab_edit(request, ids):
    status = 0
    obj = get_object(CrontabSchedule, id=ids)

    if request.method == 'POST':
        form = CrontabForm(request.POST, instance=obj)
        if form.is_valid():
            form.save()
            status = 1
        else:
            status = 2
    else:
        form = CrontabForm(instance=obj)

    return render(request, 'setup/crontab_edit.html', locals())

@login_required
@permission_verify()
def job_crontab_del(request):
    if request.method == 'POST':
        crontabs = request.POST.getlist('idc_check', [])
        if crontabs:
            for n in crontabs:
                CrontabSchedule.objects.filter(id=n).delete()
    crontab_info = CrontabSchedule.objects.all()
    return render(request, "setup/crontab_list.html", locals())

@login_required()
@permission_verify()
def job_crontab_add(request):
    if request.method == "POST":
        a_form = CrontabForm(request.POST)
        if a_form.is_valid():
            a_form.save()
            tips = u"增加成功！"
            display_control = ""
            return redirect("/setup/jobcrontablist/")
        else:
            tips = u"增加失败！"
            display_control = ""
            return render(request, "setup/crontab_add.html", locals())
    else:
        display_control = "none"
        a_form = CrontabForm()
        return render(request, "setup/crontab_add.html", locals())

@login_required()
@permission_verify()
def job_result_list(request):
    result_info = TaskResult.objects.all().order_by("-id")
    return render(request, 'setup/result_list.html', locals())

@login_required
@permission_verify()
def job_result_edit(request, ids):
    status = 0
    obj = get_object(TaskResult, id=ids)

    if request.method == 'POST':
        form = TaskResultForm(request.POST, instance=obj)
        if form.is_valid():
            form.save()
            status = 1
        else:
            status = 2
    else:
        form = TaskResultForm(instance=obj)

    return render(request, 'setup/result_edit.html', locals())

@login_required
@permission_verify()
def job_result_del(request):
    if request.method == 'POST':
        results = request.POST.getlist('idc_check', [])
        if results:
            for n in results:
                TaskResult.objects.filter(id=n).delete()
    result_info = TaskResult.objects.all()
    return render(request, "setup/result_list.html", locals())

@login_required
@permission_verify()
def job_backend(request):
    celery_file = os.path.exists('/var/opt/adminset/pid/w1.pid')
    beat_file = os.path.exists('/var/opt/adminset/pid/beat.pid')
    if celery_file:
        celery_disable = "disabled"
        celery_stop_disable = ""
    else:
        celery_disable = ""
        celery_stop_disable = "disabled"
    if beat_file:
        beat_disable = "disabled"
        beat_stop_disable = ""
    else:
        beat_disable = ""
        beat_stop_disable = "disabled"
    return render(request, "setup/job_backend.html", locals())

@login_required
@permission_verify()
def job_backend_task(request, name, action):
    cmd = "service "+name+" "+action
    Popen(cmd, stdout=PIPE, stderr=PIPE, shell=True)
    time.sleep(3)
    return redirect("/setup/jobbackend/")


from rest_framework import serializers
from .models import Recipe, RecipeURL

class RecipeURLSerializer(serializers.ModelSerializer):
	class Meta:
		fields = "__all__"
		model = RecipeURL

class RecipeSerializer(serializers.ModelSerializer):
	ingredients = serializers.SerializerMethodField()
	nutritions = serializers.SerializerMethodField()
	class Meta:
		fields = '__all__'
		model = Recipe

	def get_ingredients(self, obj):
		return_data = None
		if type(obj.ingredients) == list:
			ingredients_list = []
			for item in obj.ingredients:
				ingredients_dict = item.__dict__
				for key in list(ingredients_dict.keys()):
					if key.startswith('_'):
						ingredients_dict.pop(key)
				ingredients_list.append(ingredients_dict)
			return_data = ingredients_list
		else:
			ingredients_dict = field.__dict__
			for key in list(ingredients_dict.keys()):
				if key.startswith('_'):
					ingredients_dict.pop(key)
			return_data = ingredients_dict
		return return_data

	def get_nutritions(self, obj):
		return_data = None
		if type(obj.nutritions) == list:
			nutritions_list = []
			for item in obj.nutritions:
				nutritions_dict = item.__dict__
				for key in list(nutritions_dict.keys()):
					if key.startswith('_'):
						nutritions_dict.pop(key)
				nutritions_list.append(nutritions_dict)
			return_data = nutritions_list
		else:
			nutritions_dict = field.__dict__
			for key in list(nutritions_dict.keys()):
				if key.startswith('_'):
					nutritions_dict.pop(key)
			return_data = nutritions_dict
		return return_data


from django.urls import path
from . import views

urlpatterns = [
    path('',views.Index),
    path('ViewProduct/<id>', views.Productdetail),
    path('ViewCart', views.ViewCart),
    path('showCart', views.showCart),
    path('removeItem', views.removeItem),
    path('search',views.search),

]