# options.py

"""
This module is an integeral part of the program
MMA - Musical Midi Accompaniment.

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

Bob van der Poel <bob@mellowood.ca>

"""

import getopt
import sys
import os
import tempfile

import MMA.docs
import MMA.parse
import MMA.chords
import MMA.volume
import MMA.exits

from . import gbl
from MMA.common import *
from MMA.macro import macros

cmdSMF = None

def cmdLine(l):
    """ Try to take a mma input line and parse it like a command line.
        An empty CmdLine is just ignored. """

    if l:
        opts(l)

def cmdError(e):
    """ Illegal internal command line options."""

    error("CmdLine: the command line option '%s' is not permitted in a MMA script." % e)

def opts(l=None):
    """ Option parser.
         FIXME: this code segment is much too long!
    """

    if not l:
        l = sys.argv[1:]
        internal = False
    else:
        internal = True

    try:
        opts, args = getopt.gnu_getopt(
                         l, "b:B:dpsS:ri:wneom:f:M:cLgGvVD:01PT:I:x:", [])
    except getopt.GetoptError:
        usage()

    for o, a in opts:
        if o == '-b':
            setBarRange(a)

        elif o == '-B':
            setBarRange(a)
            gbl.barRange.append("ABS")

        elif o in ('-d', '-o', '-p', '-s', '-r', '-w', '-n', '-e', '-c'):
            import MMA.debug   # circular dep. problem
            MMA.debug.cmdLineDebug(o[-1])

        elif o == '-S':
            ln = a.split('=', 1)
            macros.setvar(ln)

        elif o == '-L':
            gbl.printProcessed = True

        elif o == '-f':
            import MMA.paths
            gbl.outfile = a
            if internal:
                warning("Output filename overwritten by -f CmdLine option.")
                MMA.paths.createOutfileName(".mid")

        elif o == '-i':
            import MMA.paths
            if internal:
                cmdError("-i")
            MMA.paths.setRC(a)

        elif o == '-g':
            if internal:
                cmdError("-g")
            gbl.makeGrvDefs = 1

        elif o == '-G':
            if internal:
                cmdError("-G")
            gbl.makeGrvDefs = 2

        elif o == '-m':
            try:
                a = int(a)
            except:
                error("Expecting -m arg to be a integer")
            gbl.maxBars = a

        elif o == '-v':
            print("%s" % gbl.version)
            if not internal:
                sys.exit(0)

        elif o == '-M':
            global cmdSMF
            if a in ['0', '1']:
                cmdSMF = a
            else:
                error("Only a '0' or '1' is permitted for the -M arg")

        elif o == '-T':   # set tracks to generate, mute all others
            gbl.muteTracks = a.upper().split(',')

        elif o == '-D':
            if internal:
                cmdError("-D..")
            if a == 'xl':
                gbl.createDocs = 1

            elif a == 'xh':
                gbl.createDocs = 2

            elif a == 's':
                gbl.createDocs = 3

            elif a == 'gh':
                gbl.createDocs = 4

            elif a == 'js':
                gbl.createDocs = 5

            elif a == 'bo':
                gbl.createDocs = 99

            elif a == 'k':
                import MMA.alloc
                # important! Needs a space before the trailing LF for mma.el
                print("Base track names: %s \n" %
                      ' '.join([a for a in sorted(MMA.alloc.trkClasses)]))
                print("Commands: %s BEGIN END DEFAULT\n" %
                      ' '.join([a for a in sorted(MMA.parse.simpleFuncs)]))
                print("TrackCommands: %s \n" %
                      ' '.join([a for a in sorted(MMA.parse.trackFuncs)]))
                print("Not complete ... subcommands, comments, chords...")
                sys.exit(0)

            else:
                print("Unknown option: '-D%s'." % a)
                usage()

        elif o == '-0':
            import MMA.sync
            MMA.sync.synchronize(['START'])

        elif o == '-1':
            import MMA.sync
            MMA.sync.synchronize(['END'])

        elif o == '-P':
            gbl.playFile = 1

        elif o == '-I':
            # We use -I for plugin help and overload it to discard
            # the plugin security. Use -II for security override.
            # It does mean you can't have plugin called "I", but
            # you could use "i" and it'll work.
            import MMA.regplug
            if a == 'I':
                MMA.regplug.secOverRide = True

            # Plugin help. Note we have not loaded any plugins at this
            # point. pluginHelp() will find the plugin, register it and
            # call its help function.
            else:
                MMA.regplug.pluginHelp(a)
                sys.exit(0)

        elif o == '-V':
            import MMA.file

            if internal:   # can't have a -V in a -V :)
                cmdError("-V")

            gbl.playFile = 2  # signal create and play groove
            if not args:
                error("-V: option requires Groove Name.")

            _, tfile = tempfile.mkstemp(prefix="MMA_", suffix=".mma")
            op = open(tfile, "w")
            groove = ''
            cmds = []
            chords = "I, vi, ii, V7"
            count = 4
            for g in args:
                if '=' in g:
                    c = g.split('=')
                    if c[0].upper() == 'CHORDS':
                        chords = c[1]
                    elif c[0].upper() == "COUNT":
                        count = c[1]
                        try:
                            count = int(count)
                        except:
                            error("-V: expecting integer for Count.")
                    else:
                        cmds.append(c)
                elif groove:
                    error("-V: Only one groove name permitted.")
                else:
                    groove = g
            if not groove:
                error("-V: no groove name specified.")

            op.write("Groove %s\n" % groove)
            for g in cmds:
                op.write("%s %s \n" % (g[0], g[1]))
            chords = chords.split(',')
            while len(chords) < count:
                chords += chords
            chords = chords[:count]
            for c in chords:
                op.write("%s\n" % c)

            op.close()

            # we can only have one scratch file, so no fear of overload.
            # otherwise we might need to explicity delete file here.
            MMA.exits.files.append(tfile)

            args = [tfile]  # fake the CLI so mma thinks the created file is yours

        elif o=='-x':  # any one of some xtra, seldom used, options
            import MMA.xtra
            MMA.xtra.xoption(a, args)

        else:
            usage()      # unreachable??

    if internal:
        return

    # a few sanity checks

    #if  MMA.writeMid.splitOutput:
    #    if gbl.playFile:
    #        error("The -P (play) option is not compatible with channel/track splitting.")
    #    if gbl.infile == 1:
    #        error("The '-' (read from stdin) is not compatible with channel/track splitting.")

    # we have processed all the args. Should just have a filename left

    if len(args) > 1:
        usage("Only one input filename is permitted, %s given on command line." % len(args))

    if gbl.infile:
        usage("Input filename already assigned ... should not happen.")

    if args:
        gbl.infile = args[0]

    # if a single '-' is left on the cmd line user want stdin. We set the
    # the input filename to numeric 1 which can't be entered.

    if gbl.infile == '-':
        gbl.infile = 1

        if not gbl.outfile:
            import MMA.debug   # circular dep. problem
            if not(MMA.debug.noOutput):
                error("Input from STDIN specified. Use -f to set an output filename.")

def usage(msg=''):
    """ Usage message. """

    txt = [
        "MMA - Musical Midi Accompaniment",
        "  Copyright 2003-22, Bob van der Poel. Version %s" % gbl.version,
        "  Distributed under the terms of the GNU Public License.",
        "  Usage: mma [opts ...] INFILE [opts ...]",
        "",
        "Options:",
        " -b <n> Limit compilation to n1-n2 bars (comment numbers)",
        " -B <n> Like -b but for absolute bar numbers",
        " -c    display default Channel assignments",
        " -d    enable lots of Debugging messages",
        " -Dk   print list of MMA keywords",
        " -Dxl  eXtract Latex doc blocks from file",
        " -Dxh  eXtract HTML doc blocks from file",
        " -Dgh  extract HTML Groove doc",
        " -Djs  extract JSON Groove information from file",
        " -Dbo  extract text for browser app",
        " -Ds   extract sequence lists from file",
        " -e    show parsed/Expanded lines",
        " -f <file>  set output Filename",
        " -g    update Groove dependency database",
        " -G    create Groove dependency database",
        " -i <file> specify init (mmarc) file",
        " -I <plugin> print docs for plugin if available",
        " -II   skip permissions test for plugins (Dangerous!)",
        " -L    show order of bars processed",
        " -m <x> set Maxbars (default == 500)",
        " -M <x> set SMF to 0 or 1",
        " -n    No generation of midi output",
        " -o    show complete filenames when Opened",
        " -p    display Patterns as they are defined",
        " -P    play song (don't save) with player",
        " -r    display Running progress",
        " -s    display Sequence info during run",
        " -S <var[=data]>  Set macro 'var' to 'data'",
        " -T <tracks> Limit generation to specified tracks",
        " -v    display Version number",
        " -V <groove [options]> preview play groove",
        " -w    disable Warning messages",
        " -xCHORDS=<chord list> test listed chords for validity",
        " -xNOCREDIT disable MMA credits in Midi Meta track",
        " -xCHECKFILE=<filename> check chords in file",
        " -xTSplit  create midi for each track",
        " -xCSplit  create MIDI for each channel",
        " -0    create sync at start of all channel tracks",
        " -1    create sync at end of all channel tracks",
        " -     a single hyphen signals to use STDIN instead of a file"]

    for a in txt:
        print(a)

    if msg:
        print("\n%s" % msg)

    sys.exit(1)

def setBarRange(v):
    """ Set a range of bars to compile. This is the -B/b option."""

    if gbl.barRange:
        error("Only one -b or -B permitted.")

    for ll in v.split(','):

        l = ll.split("-")

        if len(l) == 2:
            s, e = l
            try:
                s = int(s)
                e = int(e)
            except:
                usage("-B/b ranges must be integers, not '%s'." % l)

            for a in range(s, e + 1):
                gbl.barRange.append(str(a))

        elif len(l) == 1:
            try:
                s = int(l[0])
            except:
                usage("-B/b range must be an integer, not '%s'." % l[0])
            gbl.barRange.append(str(s))

        else:
            usage("-B/b option expecting N1-N2,N3... not '%s'." % v)


"""
Title           :tinyimagenet_tester.py
Description     :Easy Tiny ImageNet benchmark
Author          :Ilke Cugu
Date Created    :17-06-2019
Date Modified   :02-05-2020
version         :1.4.1
python_version  :3.6.6
"""
import os
import keras
import numpy as np
from overrides import overrides
from keras.utils.data_utils import get_file
from keras.callbacks import LearningRateScheduler
from keras.preprocessing.image import ImageDataGenerator
from keras.preprocessing.image import load_img, img_to_array
from applications.microresnet.MicroResNet import MicroResNet
from sklearn.model_selection import StratifiedShuffleSplit
from testers.Base_tester import Base_tester

class tinyimagenet_tester(Base_tester):
	def __init__(self, wait=False):
		"""
		Conditional constructor to enable creating testers without immediately triggering the dataset loader.

		# Arguments
			:param wait: (bool) If True, then the constructor will onyl create the instance and
								wait for manual activation to actually load the dataset
		"""
		super(tinyimagenet_tester, self).__init__()
		self.dataset_path = None
		self.x_train_mean = None
		self.label_dict = {}

		if not wait:
			self.activate()

	def activate(self):
		"""
		Call this function to manually start dataset deployment
		"""
		# Deploy the dataset
		self.dim_x = 64
		self.dim_y = 64
		self.dataset = self.load_data()
		(self.x_train, self.y_train), (self.x_val, self.y_val), (self.x_test, self.y_test) = self.dataset
		self.preprocess_dataset()

		# Split the train and validation sets
		self.training_set_size = int(self.x_train.shape[0])
		self.val_set_size = int(self.x_val.shape[0])
		self.test_set_size = int(self.x_test.shape[0])

		# Turn the labels into one-hot-vector form
		self.y_train = keras.utils.to_categorical(self.y_train, self.get_n_classes())
		self.y_val = keras.utils.to_categorical(self.y_val, self.get_n_classes())
		self.y_test = keras.utils.to_categorical(self.y_test, self.get_n_classes())

	@overrides
	def preprocess_dataset(self):
		# Normalize the data
		self.x_train = self.x_train.astype('float32') / 255.0
		self.x_val = self.x_val.astype('float32') / 255.0
		self.x_test = self.x_test.astype('float32') / 255.0

		self.x_train_mean = np.mean(self.x_train, axis=0)
		self.x_train -= self.x_train_mean
		self.x_val -= self.x_train_mean
		self.x_test -= self.x_train_mean

	def load_data(self):
		"""
		Loads tiny-imagenet dataset.

		:return: tuple of Numpy arrays: `(x_train, y_train), (x_test, y_test)`.
		"""
		dirname = 'tiny-imagenet-200.zip'
		origin = 'http://cs231n.stanford.edu/tiny-imagenet-200.zip'
		path = get_file(dirname, origin=origin, extract=True, archive_format="zip")
		path = path.split(".zip")[0]
		self.dataset_path = path

		num_train_samples = 100000
		num_test_samples = 10000

		x = np.empty((num_train_samples, self.dim_x, self.dim_y, 3), dtype='uint8')
		y = np.empty((num_train_samples,), dtype='uint8')

		x_test = np.empty((num_test_samples, self.dim_x, self.dim_y, 3), dtype='uint8')
		y_test = np.empty((num_test_samples,), dtype='uint8')

		# Deploy labels
		self.read_labels(path)
		reverse_label_dict = {v: k for k,v in self.label_dict.items()}

		# Gather training data
		counter = 0
		fpath = os.path.join(path, "train")
		for i in range(len(self.label_dict)):
			dir_path = os.path.join(fpath, self.label_dict[i], "images")
			files = os.listdir(dir_path)
			for file in files:
				if file.endswith("JPEG") or file.endswith("jpeg"):
					img_path = os.path.join(dir_path, file)
					img = img_to_array(load_img(img_path))
					x[counter, :, :, :] = img
					y[counter] = i
					counter += 1

		# Then split that into train/val sets
		train_val_split = StratifiedShuffleSplit(n_splits=1, test_size=0.1, random_state=13)
		for i_train, i_val in train_val_split.split(x, y):
			x_train, x_val = x[i_train], x[i_val]
			y_train, y_val = y[i_train], y[i_val]

		# Gather the original validation data as the new test set
		counter = 0
		fpath = os.path.join(path, "val")
		label_file_path = os.path.join(fpath, "val_annotations.txt")
		with open(label_file_path, "r") as label_file:
			for line in label_file:
				temp = line[:-1].split("\t")
				img_path = os.path.join(fpath, "images", temp[0])
				img = img_to_array(load_img(img_path))
				label = reverse_label_dict[temp[1]]
				x_test[counter, :, :, :] = img
				y_test[counter] = label
				counter += 1

		return (x_train, y_train), (x_val, y_val), (x_test, y_test)

	def load_test_data(self):
		x_test = np.empty((10000, self.dim_x, self.dim_y, 3), dtype='uint8')

		# Gather test data
		counter = 0
		test_img_names = []
		fpath = os.path.join(self.dataset_path, "test", "images")
		files = os.listdir(fpath)
		for file in files:
			if file.endswith("JPEG") or file.endswith("jpeg"):
				img_path = os.path.join(fpath, file)
				img = img_to_array(load_img(img_path))
				test_img_names.append(file)
				x_test[counter, :, :, :] = img
				counter += 1

		# Preprocess the test set
		x_test = x_test.astype('float32') / 255.0
		x_test -= self.x_train_mean

		return x_test, test_img_names

	def read_labels(self, path):
		fpath = os.path.join(path, "wnids.txt")
		with open(fpath, "r") as label_file:
			i = 0
			for line in label_file:
				self.label_dict[i] = line[:-1]
				i += 1

	def createImgGenerator(self):
		return ImageDataGenerator(
			# set input mean to 0 over the dataset
			featurewise_center=False,
			# set each sample mean to 0
			samplewise_center=False,
			# divide inputs by std of dataset
			featurewise_std_normalization=False,
			# divide each input by its std
			samplewise_std_normalization=False,
			# apply ZCA whitening
			zca_whitening=False,
			# epsilon for ZCA whitening
			zca_epsilon=1e-06,
			# randomly rotate images in the range (deg 0 to 180)
			rotation_range=0,
			# randomly shift images horizontally
			width_shift_range=0.1,
			# randomly shift images vertically
			height_shift_range=0.1,
			# set range for random shear
			shear_range=0.,
			# set range for random zoom
			zoom_range=0.,
			# set range for random channel shifts
			channel_shift_range=0.,
			# set mode for filling points outside the input boundaries
			fill_mode='nearest',
			# value used for fill_mode = "constant"
			cval=0.,
			# randomly flip images
			horizontal_flip=True,
			# randomly flip images
			vertical_flip=False,
			# set rescaling factor (applied before any other transformation)
			rescale=None,
			# set function that will be applied on each input
			preprocessing_function=None,
			# image data format, either "channels_first" or "channels_last"
			data_format="channels_last"
		)

	def lr_schedule(self, epoch):
		"""
		Learning rate scheduler for CIFAR-10 as part of callbacks during training

		Adopted from https://keras.io/examples/cifar10_resnet/

		# Arguments
			:param epoch: (int) # of epochs

		:return: new learning rate
		"""
		lr = self.base_lr
		if epoch > 180:
			lr *= 0.5e-3
		elif epoch > 160:
			lr *= 1e-3
		elif epoch > 120:
			lr *= 1e-2
		elif epoch > 80:
			lr *= 1e-1
		return lr

	@overrides
	def get_n_classes(self):
		return 200

	@overrides
	def evaluate(self, model):
		return model.evaluate(self.x_test, self.y_test, verbose=0)

	@overrides
	def predict(self, model):
		return model.predict(self.x_test, verbose=0)

	def print_predictions(self, model_name, preds, img_names):
		file_name = "%s_preds.txt" % model_name
		with open(file_name, "w+") as preds_file:
			for i in range(len(img_names)):
				entry = "%s %s\n" % (img_names[i], np.argmax(preds[i]))
				preds_file.write(entry)

	@overrides
	def run(self, model,
			optimizer="adam",
			lr=1e-3,
			momentum=None,
			decay=None,
			loss='categorical_crossentropy',
			batch_size=128,
			epochs=200,
			verbose=0,
			callbacks=None,
			schedule_lr=True,
			custom_lr_scheduler=None,
			print_preds_for_test_set=False):
		"""
		Runs the benchmark

		# Arguments
			:param model: Keras model (including MicroResNet)
			:param optimizer: (string) name of the selected Keras optimizer
			:param lr: (float) learning rate
			:param momentum: (float) only relevant for the optimization algorithms that use momentum
			:param decay: (float) only relevant for the optimization algorithms that use weight decay
			:param loss: (string) name of the selected Keras loss function
			:param batch_size: (int) # of inputs in a mini-batch
			:param epochs: (int) # of full training passes
			:param verbose: (int) Keras verbose argument
			:param callbacks: list of Keras callbacks
			:param schedule_lr: (bool) enable/disable learning rate scheduler (default or custom)
			:param custom_lr_scheduler: user defined learning rate scheduler
			:param print_preds_for_test_set: (bool) enables printing out the test set predictions of the trained model

		:return: (history, score)
		"""
		# tiny-imagenet Specific configurations
		self.base_lr = lr
		new_callbacks = [] if callbacks is None else callbacks
		if schedule_lr and custom_lr_scheduler is None:
			lr_scheduler = LearningRateScheduler(self.lr_schedule, verbose=verbose)
			new_callbacks.extend([lr_scheduler])
		elif schedule_lr and custom_lr_scheduler is not None:
			new_callbacks.append(custom_lr_scheduler)

		# Compile the model
		optimizer = self.get_optimizer(optimizer, lr=lr, momentum=momentum, decay=decay)
		model.compile(optimizer=optimizer, loss=loss, metrics=['accuracy'])

		# Training & validation data preparation
		trainImgGenerator = self.createImgGenerator()
		trainGenerator = trainImgGenerator.flow(self.x_train, self.y_train, batch_size=batch_size, shuffle=True, seed=13)

		# Train the model
		_model = model.model if isinstance(model, MicroResNet) else model
		hist = _model.fit_generator(
			trainGenerator,
			epochs=epochs,
			verbose=verbose,
			steps_per_epoch=self.training_set_size // batch_size,
			validation_data=(self.x_val, self.y_val),
			class_weight='auto',
			callbacks=new_callbacks
		)

		# Evaluate the model with the val data
		score = _model.evaluate(self.x_test, self.y_test, verbose=verbose)

		# Print out the model predictions for the test data to upload the tiny-imagenet test server
		if print_preds_for_test_set:
			new_x_test, img_names = self.load_test_data()
			preds = model.predict(new_x_test, verbose=verbose)
			self.print_predictions(model.name, preds, img_names)

		return hist, score


from datetime import datetime
import streamlit as st
import streamlit.components.v1 as components
import src.charts as charts
from src.utils import thousand_sep, dateformat, tested_percent, fetch_metadata

def local_css(file_name):
    with open(file_name) as f:
        st.markdown(f"", unsafe_allow_html=True)

def remote_css(url):
    st.markdown(f'<link href="{url}" rel="stylesheet">', unsafe_allow_html=True)

def hide_menu():
    hide_menu_style = """

            """
    st.markdown(hide_menu_style, unsafe_allow_html=True)

def main():
    st.markdown(
        """

c19norge.no

            @covid19norge

            @covid19norge

        """,
        unsafe_allow_html=True,
    )

    # metadata
    tested = fetch_metadata("tested")
    confirmed = fetch_metadata("confirmed")
    dead = fetch_metadata("dead")
    admissions = fetch_metadata("admissions")
    respiratory = fetch_metadata("respiratory")
    tested_pct_w1 = tested_percent()[0]
    tested_pct_w2 = tested_percent()[1]

    # totals
    tested_total = thousand_sep(tested.get("total"))
    confirmed_total = thousand_sep(confirmed.get("total", "-"))
    dead_total = thousand_sep(dead.get("total", "-"))
    admissions_total = thousand_sep(admissions.get("total", "-"))
    respiratory_total = thousand_sep(respiratory.get("total", "-"))

    # newToday
    tested_newToday = thousand_sep(tested.get("newToday", 0))
    confirmed_newToday = thousand_sep(confirmed.get("newToday", 0))
    dead_newToday = thousand_sep(dead.get("newToday", 0))
    admissions_newToday = admissions.get("changeToday", 0)
    respiratory_newToday = respiratory.get("changeToday", 0)

    # newYesterday
    tested_newYesterday = thousand_sep(tested.get("newYesterday", 0))
    confirmed_newYesterday = thousand_sep(confirmed.get("newYesterday", 0))
    dead_newYesterday = thousand_sep(dead.get("newYesterday", 0))
    admissions_newYesterday = admissions.get("newYesterday", 0)
    respiratory_newYesterday = respiratory.get("newYesterday", 0)

    if admissions_newToday > 0:
        admissions_newToday = f"+{admissions_newToday}"
    if respiratory_newToday > 0:
        respiratory_newToday = f"+{respiratory_newToday}"
    if admissions_newYesterday > 0:
        admissions_newYesterday = f"+{admissions_newYesterday}"
    if respiratory_newYesterday > 0:
        respiratory_newYesterday = f"+{respiratory_newYesterday}"

    # updated
    tested_updated = dateformat(tested.get("updated", {}).get("timestamp"))
    confirmed_updated = dateformat(confirmed.get("updated", {}).get("timestamp"))
    dead_updated = dateformat(dead.get("updated", {}).get("timestamp"))
    admissions_updated = dateformat(admissions.get("updated", {}).get("timestamp"))
    respiratory_updated = dateformat(respiratory.get("updated", {}).get("timestamp"))
    tested_pct_updated = tested_percent()[2]

    confirmed, dead, admissions, respiratory = st.beta_columns(4)
    tested, tested_pct = st.beta_columns(2)

    if datetime.now().hour in range(0, 11):
        desc = "Nye i går"
        desc_hospitalized = "Endring i går"
        tested_new = tested_newYesterday
        confirmed_new = confirmed_newYesterday
        dead_new = dead_newYesterday
        admissions_new = admissions_newYesterday
        respiratory_new = respiratory_newYesterday
    else:
        desc = "Nye i dag"
        desc_hospitalized = "Endring i dag"
        tested_new = tested_newToday
        confirmed_new = confirmed_newToday
        dead_new = dead_newToday
        admissions_new = admissions_newToday
        respiratory_new = respiratory_newToday

    # infoboxes
    with confirmed:
        st.markdown(
            f"""

Smittet

{confirmed_total}

{confirmed_new}

{desc}

{confirmed_updated}

        """,
            unsafe_allow_html=True,
        )

    with dead:
        st.markdown(
            f"""

Døde

{dead_total}

{dead_new}

{desc}

{dead_updated}

        """,
            unsafe_allow_html=True,
        )

    with tested:
        st.markdown(
            f"""

Testet

{tested_total}

{tested_new}

{desc}

{tested_updated}

        """,
            unsafe_allow_html=True,
        )

    with admissions:
        st.markdown(
            f"""

Innlagte

{admissions_total}

{admissions_new}

{desc_hospitalized}

{admissions_updated}

        """,
            unsafe_allow_html=True,
        )

    with respiratory:
        st.markdown(
            f"""

Respirator

{respiratory_total}

{respiratory_new}

{desc_hospitalized}

{respiratory_updated}

        """,
            unsafe_allow_html=True,
        )

    with tested_pct:
        st.markdown(
            f"""

Positive tester siste uke

{tested_pct_w1} %

{tested_pct_w2} %

Foregående uke

{tested_pct_updated}

        """,
            unsafe_allow_html=True,
        )

    st.markdown("___")

    # graphs
    period, category = st.beta_columns(2)

    PERIODS = {
        "Hele perioden": 0,
        "Siste 30 dager": 30,
        "Siste 14 dager": 14,
    }

    with category:
        cummulative = st.selectbox("", ["Nye per dag", "Total"])
    with period:
        selection = st.selectbox("", list(PERIODS.keys()))

    st.header("Smittet")
    st.altair_chart(
        charts.confirmed(cummulative, PERIODS[selection]), use_container_width=True
    )

    st.header("Døde")
    st.altair_chart(
        charts.dead(cummulative, PERIODS[selection]), use_container_width=True
    )

    st.header("Testet")
    st.altair_chart(
        charts.tested(cummulative, PERIODS[selection]), use_container_width=True
    )

    st.header("Sykehusinnleggelser")
    st.altair_chart(charts.hospitalized(PERIODS[selection]), use_container_width=True)

    # data sources
    with st.beta_expander("Datakilder"):
        with open("static/html/sources.html") as f:
            data = f.read()
            components.html(data, height=700)

    # footer
    st.markdown("")
    st.markdown("Laget av [Fredrik Haarstad](https://github.com/frefrik/)")

if __name__ == "__main__":
    st.set_page_config(
        page_title="c19norge.no",
        page_icon="static/img/coronavirus.png",
        initial_sidebar_state="collapsed",
    )

    local_css("static/css/style.css")
    remote_css(
        "https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css"
    )
    hide_menu()
    main()


# THIS CODE COMES FROM https://www.pueschel.dev/python,/dash,/plotly/2019/06/28/dash-logs.html

import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output, State
import logging
import sys

logger = logging.getLogger(__name__)

class DashLogger(logging.StreamHandler):
    def __init__(self, stream=None):
        super().__init__(stream=stream)
        self.logs = list()

    def emit(self, record):
        try:
            msg = self.format(record)
            self.logs.append(msg)
            self.logs = self.logs[-1000:]
            self.flush()
        except Exception:
            self.handleError(record)

dash_logger = DashLogger(stream=sys.stdout)
logger.addHandler(dash_logger)

external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']

app = dash.Dash(__name__, external_stylesheets=external_stylesheets)

app.layout = html.Div([
    html.Div(id='my-div'),
    html.Button('Submit', id='button'),
    dcc.Interval(
        id='log-update',
        interval=1 * 1000  # in milliseconds
    ),
    html.Div(id='log')
])

@app.callback(
    Output('log', 'children'),
    [Input('log-update', 'n_intervals')])
def update_logs(interval):
    return [html.Div(log) for log in dash_logger.logs]

@app.callback(Output('my-div', 'children'), [Input('button', 'n_clicks')])
def add_log(click):
    logger.warning("Important Message")

if __name__ == '__main__':
    app.run_server(debug=True)


param_1 = '\'Views\''
param_2 = '\'Likes\''
param_3 = '\'Comments\''
param_4 = '\'TikToks with the same sound\''

txt_positive = 'Positive correlationðŸ“ˆ:'
txt_negative = 'Negative correlationðŸ“‰:'

def txt_correlation(side, param_1, param_2, corr):
    if side == txt_positive:
        word = 'more'
    elif side == txt_negative:
        word = 'less'
    text = f'''The greater the <b>{param_1}</b>, the {word} {param_2}(corr = {corr})'''
    return text

ttest_info_list = [
    'When the video was filmed?',
    'Set the most suitable TikTok type group:',
    'What color prevails in the video?',
    'How many people are in the video?',
    'Music in TikTok is:',
    'When did you upload the video',
    'Are there any sun rays in the video?',
    'Is there any dance in the video?',
    'Is there any LED Lighting in the video?',
    'Do you have artificial or natural light',
    'Did you write something in a description field?',
    'Is there a text write on screen?',
    'Did you use TikTok Effects',
    'Did you use TikTok Filters',
    'Did you format the video not in TikTok app?',
    'What camera did you use?',
    'Did you use any hashtags?',
    'Choose the video dynamics:'
    ]

ttest_params_list = [
    'views',
    'likes',
    'comments',
    'sound_popularity',
    'when_filmed',
    'tiktok_ type',
    'tiktok_color',
    'people_number',
    'music_lang',
    'upload_time',
    'sunshine',
    'dance',
    'led_lights',
    'light',
    'description',
    'text',
    'tiktok_effects',
    'tiktok_filters',
    'format_outside',
    'camera',
    'hashtags',
    'dynamics'
]

ttest_param_1 = 'views'
ttest_param_2 = 'likes'


import sys
from itertools import accumulate as ac
input = sys.stdin.readline

N = int(input())
nums = [int(input()) for _ in range(N)]
nums += nums
s = list(ac(nums))

res = 0
for i in range(N, 2*N):
    for j in range(i-N+1, N):
        res = max(res, min(s[i]-s[j], s[j+N]-s[i]))

print(res)


from abc import abstractmethod
from sklearn.metrics.pairwise import haversine_distances

from .ODLHelper import OperatorFunction, OperatorModule
from .ODLHelperCustom import setup_forward_and_backward

from .Graph import *
from .Plotting import *
from .ToyImageGenerator import *
from .PytorchGATHelper import *
from .SNRHelper import add_noise_to_sinograms, find_SNR

import numpy as np
import wandb
import time
from torch_geometric.nn import GATConv, DataParallel
from torch_geometric.loader import DataListLoader
from torch_geometric.data import Data, Dataset
from torch_geometric.utils.random import erdos_renyi_graph
import torch
torch.pi = torch.acos(torch.zeros(1)).item() * 2

import utils.UNetModel as UNetModel
import os.path
################### Loss Enum ##############################
from enum import Enum
class Loss(Enum):
    """ Enum class for different types of losses.
        Sino : l2-distance between clean sinogram and denoised sinogram.
        Fbp: l2.distance between clean input images and fbp(denoised sinogram).
    """
    SINO = 0,
    FBP = 1

################### Denoiser class #########################
class GatBase():
    """ Abstract base class for GatDenoisers and GatValidator
    """
    def __init__(self, args):
        self.consumeArgs(args)
        self.loader = None
        self.unet = None

        if self.VERBOSE:
            self.time_dict = {}

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

        self.device0 = self.device = torch.device(
            'cuda:0' if torch.cuda.is_available() else 'cpu')

    ######################## Initializer for child classes ############################
    def consumeArgs(self, args):
        """ Consume all the required arguments

        Args:
            args: The GAT-Denoiser arguments.
        """
        self.args = args
        self.VERBOSE = args.verbose
        self.USE_WANDB = args.use_wandb
        if self.USE_WANDB:
            self.WAND_PROJECT = args.wandb_project

        self.N: int = args.graph_size
        self.K : int = args.k_nn

        self.RESOLUTION: int = args.resolution

        self.GAT_LAYERS: int = args.gat_layers
        self.GAT_HEADS: int = args.gat_heads
        self.GAT_DROPOUT: float = args.gat_dropout
        self.GAT_USE_CONV: bool = args.gat_use_conv
        self.UNET_REFINEMENT: bool = args.unet_refinement
        if self.UNET_REFINEMENT:
            self.UNET_PATH : str = args.unet_path

        if self.GAT_USE_CONV:
            self.GAT_CONV_KERNEL = args.gat_conv_kernel
            self.GAT_CONV_PADDING = args.gat_conv_padding
            self.GAT_CONV_N_LATENT = args.gat_conv_N_latent

    def consumeDenoiserArgs(self, args):
        """ Consume all the required arguments for denoiser instance of GAT-Denoiser
        Args:
            args: The GAT-Denoiser arguments.
        """
        self.M = args.samples
        self.EPOCHS: int = args.epochs

        self.GAT_ADAM_WEIGHTDECAY: float = args.gat_weight_decay
        self.GAT_ADAM_LR: float = args.gat_learning_rate
        self.UNET_TRAIN: bool = args.unet_train

        self.Loss = Loss[args.loss.upper()]

        # check if snr is fixed
        if args.gat_snr_upper is None or args.gat_snr_lower == args.gat_snr_upper:
            if self.VERBOSE:
                print("Using fixed snr!")
            self.SNR = args.gat_snr_lower
            self.FIXED_SNR = True
            self.SNR_LOWER = args.gat_snr_lower
            self.SNR_UPPER = args.gat_snr_upper
        else:
            self.FIXED_SNR = False
            self.SNR_LOWER = args.gat_snr_lower
            self.SNR_UPPER = args.gat_snr_upper

    @classmethod
    def create_validator(cls, args, model_state, run_name=None):
        """ Creates a validator instance.
        Args:
            args: Arguments for setting up the validator.
                The following arguments need to be present:
                    args.verbose : Determines if time is measured or not.
                    args.use_wandb : Determines if wandb is used
                    args.graph_size :  Number of observations in the forward model.
                    args.k_nn: K-NN parameter k for graph size.
                    args.resolution: Observation resolution
                    args.gat_layers: Number of GAT-Denoiser layers (same for convolution and GAT)
                    args.gat_heads: Number of heads
                    args.gat_dropout: GAT dropout
                    args.gat_use_conv: Activate Convolution
                    args.unet_refinement : Activate U-Net reconstruction
                Optional arguments:
                    args.wandb_project: Wandb project name
                    args.gat_conv_kernel : Convolution kernel
                    args.gat_conv_padding: Convolution Padding
                    args.gat_conv_N_latent: Convolution latent channels
                    args.unet_path: Path to U-Net model
        Returns:
            GatValidator: The created instance.
        """
        validator =  GatValidator(args)
        validator.UNET_TRAIN = False
        validator.__initialize_validator__(args, model_state, run_name)
        return validator

    @classmethod
    def create_toyimage_dynamic_denoiser(cls, args):
        """ Create a dynamic toyimage denoiser.
        During training, the denoiser will create uniformly created toy images for every epoch.

        Args:
            args: Arguments for setting up the denoiser.
                The following arguments need to be present:
                    args.verbose : Determines if time is measured or not.
                    args.use_wandb : Determines if wandb is used
                    args.graph_size :  Number of observations in the forward model.
                    args.samples: Number of samples
                    args.k_nn: K-NN parameter k for graph size.
                    args.resolution: Observation resolution

                    args.gat_layers: Number of GAT-Denoiser layers (same for convolution and GAT)
                    args.gat_heads: Number of heads
                    args.gat_dropout: GAT dropout
                    args.gat_use_conv: Activate Convolution
                    args.unet_refinement : Activate U-Net reconstruction
                    args.unet_train: Joint U-Net training
                    self.loss: Loss type

                    args.EPOCHS: Number of training epochs
                    args.gat_weight_decay: Adam optimizer weight decay
                    args.gat_learning_rate: Adam optimizer learning rate
                    args.gat_snr_lower: lower training snr boundary
                    args.gat_snr_upper: upper training snr boundard
                Optional arguments:
                    args.gat_conv_kernel : Convolution kernel
                    args.gat_conv_padding: Convolution Padding
                    args.gat_conv_N_latent: Convolution latent channels
                    args.unet_path: Path to U-Net model
        Returns:
            GatDenoiserToyImagesDynamic: The created instance.
        """
        denoiser = GatDenoiserToyImagesDynamic(args)
        denoiser.__initialize_denoiser__(args)

        return denoiser

    @classmethod
    def create_fixed_images_denoiser(cls, args, model_state = None, optimizer_state = None, run_name=None):
        """ Create a fixed image denoiser. In every epoch, the same training images will be used.

        Args:
            args: Arguments for setting up the denoiser.
            The following arguments need to be present:

        Returns:
            GatDenoiserImagesFixed: The created instance.
        """
        denoiser =  GatDenoiserImagesFixed(args)
        denoiser.__initialize_denoiser__(args, model_state, optimizer_state, run_name)
        return denoiser

    def __initialize_validator__(self, args, model_state, run_name):
        if self.USE_WANDB:
            self.__init_wandb__(self.WAND_PROJECT, args, run_name=run_name)

        self.__execute_and_log_time__(lambda: self.__init_graph_and_forward_backward(args), "init")
        self.__execute_and_log_time__(lambda: self.__prepare_model__(model_state), "prep model")

    def __initialize_denoiser__(self, args, model_state=None, optimizer_state=None, run_name=None):
        self.consumeDenoiserArgs(args)

        if self.USE_WANDB:
            self.__init_wandb__(self.WAND_PROJECT, args, run_name=run_name)

        self.__execute_and_log_time__(lambda: self.__init_graph_and_forward_backward(args), "init")
        self.__execute_and_log_time__(lambda: self.__prepare_model__(model_state), "prep model")
        self.__execute_and_log_time__(lambda: self.__prepare_optimizer__(optimizer_state), "prep optimizer")

    def __init_wandb__(self, wandb_name, args, wandb_user ="cedric-mendelin", run_name = None):
        config = args

        gpus = torch.cuda.device_count()
        config.gpu_count = gpus

        wandb.init(project=wandb_name, entity=wandb_user, config=config, reinit=False)

        if isinstance(self, GatDenoiserImagesFixed) or isinstance(self, GatDenoiserToyImagesDynamic):
            wandb.run.name = f"{wandb_name}-{gpus}-{self.RESOLUTION}-{self.N}-{self.M}-{self.K}-{self.EPOCHS}-{self.GAT_LAYERS}-{self.GAT_HEADS}-{self.GAT_DROPOUT}-{self.GAT_ADAM_WEIGHTDECAY}-{self.SNR_LOWER}-{self.SNR_UPPER}-{self.Loss}-{self.GAT_USE_CONV}-{self.UNET_REFINEMENT}"

        if run_name is not None :
            wandb.run.name = run_name

    ################## Graph - setup during initialization #############
    def __init_graph_and_forward_backward(self, args):
        # parameters for forward and backward operatior
        # Currently radon and filter_back_projection is used.
        self.radon, self.fbp, self.pad = setup_forward_and_backward(self.RESOLUTION, self.N)

        if self.UNET_REFINEMENT:
            checkpoint = torch.load(self.UNET_PATH, map_location=self.device)
            self.unet = UNetModel.UNet(nfilter=128).to(self.device)
            self.unet.load_state_dict(checkpoint['model_state_dict'])
            if self.UNET_TRAIN:
                self.unet.train()
            else:
                self.unet.eval()

        # Parameters for seeting up graph.
        # Currently a circle graph with k neighoburs is used.
        self.graph = self.__prepare_graph__()

    def __prepare_graph__(self, cache=True):
        graph_name = f"graphs/circle_{self.N}_{self.K}.npz"

        if self.K == 0:
            return erdos_renyi_graph(self.N, 0.01)

        # check if graph is cached
        if os.path.isfile(graph_name):
            loaded = np.load(graph_name)
            return torch.tensor(loaded["edges"]).type(torch.long)

        # if not, create graph and cache
        # sample equally spaced points on unit-circle
        angles_np = np.linspace(0, 2 * np.pi, self.N)
        points_np = np.array([np.cos(angles_np), np.sin(angles_np)]).T

        # calculate distances of points on unit-circle
        distances = haversine_distances(points_np, points_np)

        # create the k-nn graph from distances
        _, _, edges = generate_knn_from_distances_with_edges(distances, self.K, ordering='asc', ignoreFirst=True)

        if cache:
            np.savez(graph_name, edges=edges.T)

        return torch.tensor(edges.T).type(torch.long)

    ################### Model - setup during initialization ############
    def __prepare_model__(self, state=None):
        if self.GAT_USE_CONV:
            self.model = GAT(
                in_dim=self.RESOLUTION,
                out_dim=self.RESOLUTION,
                num_layers=self.GAT_LAYERS,
                heads=self.GAT_HEADS,
                dropout=self.GAT_DROPOUT,
                add_conv_before_gat=self.GAT_USE_CONV,
                conv_kernel=self.GAT_CONV_KERNEL,
                conv_padding=self.GAT_CONV_PADDING,
                conv_N_latent=self.GAT_CONV_N_LATENT)
        else:
            self.model = GAT(
                in_dim=self.RESOLUTION,
                out_dim=self.RESOLUTION,
                num_layers=self.GAT_LAYERS,
                heads=self.GAT_HEADS,
                dropout=self.GAT_DROPOUT,
                add_conv_before_gat=False)

        if state != None:
            self.model.load_state_dict(state)

        # model for pytorch.Geometric data parallel
        # this is needed to train on multiple GPUs
        self.model = DataParallel(self.model)
        self.model.to(self.device)

        self.fbp_with_gradient = OperatorModule(self.fbp)

    def __prepare_optimizer__(self, optimizer_state=None):
        # add unet parameters as well when training is activated
        if self.UNET_TRAIN:
            parameters = list(self.model.parameters()) + list(self.unet.parameters())
        else:
            parameters = self.model.parameters()

        self.optimizer = torch.optim.Adam(
            parameters,
            lr=self.GAT_ADAM_LR,
            weight_decay=self.GAT_ADAM_WEIGHTDECAY)

        if optimizer_state != None:
            self.optimizer.load_state_dict(optimizer_state)

    ################## Forward operator Operators ######################
    def __forward__(self, images):
        """ Uses odl for forwarding images.

        Args:
            images: images, shape: (N, Resolution, Resolution)
        Returns:
            numpy.array: sinograms of input images.
        """
        return OperatorFunction.apply(self.radon, images).data[:,:, self.RESOLUTION // 2:self.RESOLUTION //2 + self.RESOLUTION]

    ################## Forward operator Operators ######################
    def __backward__(self, sinograms, with_gradient=False, deactivate_unet = False):
        """ Uses odl for forwarding images.

        Args:
            images: images, shape: (batch_size, N, Resolution)
        Returns:
            numpy.array: denoised sinograms of input images.
        """
        if with_gradient:
            fbp = self.fbp_with_gradient(self.pad(sinograms))
        else:
            fbp = OperatorFunction.apply(self.fbp, self.pad(sinograms)).data

        if self.UNET_REFINEMENT and not deactivate_unet:
            fbp = self.unet(fbp.view(-1, 1, self.RESOLUTION, self.RESOLUTION))

        return fbp

    @abstractmethod
    def _prepare_training_data(self, batch_size, loss):
        pass

    @abstractmethod
    def _prepare_training_images(self, images):
        pass

    def _prepare_validation_data(self, snr, batch_size):
        validation_dataset = ValidationDataset(self.V, snr, self.validation_sinograms, self.graph)
        loader = DataListLoader(dataset=validation_dataset, batch_size=batch_size, shuffle=False)
        return loader

    def __prepare_validation_images__(self, validation_images):
        self.V: int = validation_images.shape[0]
        self.T_validation_images = torch.from_numpy(validation_images).type(torch.float)
        self.validation_sinograms = self.__forward__(self.T_validation_images)

    def train(self, images=None, batch_size=1, loss=Loss.SINO):
        self.__execute_and_log_time__(lambda: self.__train__(images, batch_size, loss), "training")
        return self.model, self.optimizer, self.unet

    def __train__(self, images, batch_size, loss):
        self._prepare_training_images(images)
        self.model.train()

        for epoch in range(self.EPOCHS):
            # get training data, if snr and images are fixed, will only be calculated once
            loader, snr = self._prepare_training_data(batch_size, loss)
            loss_epoch = 0

            for data in loader:
                self.optimizer.zero_grad()
                out_sinograms = self.model(data)
                batch_n = int(out_sinograms.size(dim=0) / self.N)

                # if loss : sino
                # y = clean_sinograms
                # if loss : fbp
                # y = clean input images
                y = torch.cat([d.y for d in data]).to(out_sinograms.device)

                if loss == Loss.SINO:
                    loss_training = torch.linalg.norm(out_sinograms - y)
                elif loss == Loss.FBP:
                    fbps = self.__backward__(out_sinograms.view(batch_n, self.N, self.RESOLUTION), True)
                    loss_training = torch.linalg.norm(fbps.view(batch_n, self.RESOLUTION, self.RESOLUTION) - y.view(batch_n, self.RESOLUTION, self.RESOLUTION))
                else:
                    raise RuntimeError("Unknown loss type!")

                loss_training.backward()
                self.optimizer.step()
                loss_epoch += loss_training

            print(f"epoch : {epoch} , epoch-snr: {snr}, epoch_loss : {loss_epoch}")
            if self.USE_WANDB:
                wandb.log({
                    "epoch": epoch,
                    "epoch_loss": loss_epoch,
                    "epoch_snr": snr
                })

    def validate(self, validation_images, validation_snrs, batch_size, seed=2022):
        self.__execute_and_log_time__(lambda: self.__validate__(
            validation_images, validation_snrs, batch_size, seed), "validation")

    def __validate__(self, validation_images, validation_snrs, batch_size, seed):
        torch.manual_seed(seed)
        self.__prepare_validation_images__(validation_images)

        self.model.eval()
        self.model.to(self.device0)

        if self.unet != None:
            self.unet.to(self.device0)
            if self.UNET_TRAIN:
                self.unet.eval()

        counter = 0
        image_index = 0

        for snr in validation_snrs:
            loader = iter(self._prepare_validation_data(snr, batch_size))

            for validation_data in loader:
                denoised_sinograms = self.model(validation_data) # denoised sino
                batch_n = int(denoised_sinograms.size(dim=0) / self.N)

                denoised_sinograms = denoised_sinograms.view(batch_n, self.N, self.RESOLUTION)
                noisy_sinograms = torch.cat([d.x for d in validation_data]).view(batch_n, self.N, self.RESOLUTION).to(denoised_sinograms.device) # noisy sino
                clean_sinograms = torch.cat([d.y for d in validation_data]).view(batch_n, self.N, self.RESOLUTION).to(denoised_sinograms.device)

                fbps_denoised = self.__backward__(denoised_sinograms).to(denoised_sinograms.device)
                fbps_noisy = self.__backward__(noisy_sinograms, deactivate_unet=True).to(denoised_sinograms.device)

                clean_images = self.T_validation_images[image_index: image_index + batch_n,:,:].to(denoised_sinograms.device)
                image_index = image_index + batch_n

                for i in range(batch_n):
                    if self.USE_WANDB:
                        current_idx = counter
                        counter += 1

                        wandb.log({
                            "val_idx" : current_idx,
                            "val_loss_sino_denoised": torch.linalg.norm(denoised_sinograms[i]- clean_sinograms[i]),
                            "val_loss_sino_noisy": torch.linalg.norm(noisy_sinograms[i] - clean_sinograms[i]),

                            "val_loss_reco_denoised": torch.linalg.norm(fbps_denoised[i] - clean_images[i]),
                            "val_loss_reco_noisy": torch.linalg.norm(fbps_noisy[i] - clean_images[i]),

                            "val_snr_sino_denoised": find_SNR(clean_sinograms[i], denoised_sinograms[i]),
                            "val_snr_sino_noisy": find_SNR(clean_sinograms[i], noisy_sinograms[i]),

                            "val_snr_reco_denoised": find_SNR(clean_images[i], fbps_denoised[i]),
                            "val_snr_reco_noisy": find_SNR(clean_images[i], fbps_noisy[i])
                        })

                        if counter < 100:
                            wandb.log({
                                "val_clean" : wandb.Image(clean_images[i].cpu().detach().numpy(), caption=f"Original object"),
                                "val_reco_denoised": wandb.Image(fbps_denoised[i].cpu().detach().numpy(), caption=f"Rec denoised - SNR: {snr} "),
                                "val_reco_noisy": wandb.Image(fbps_noisy[i].cpu().detach().numpy(), caption=f"Rec noisy - SNR: {snr}")
                            })

                torch.cuda.empty_cache()
            image_index = 0

    def finish_wandb(self, execution_time=None):
        if execution_time != None:
            wandb.log({"execution_time": execution_time})

        wandb.finish()

    def __execute_and_log_time__(self, action, name):
        if self.VERBOSE:
            t = time.time()

        action()

        if self.VERBOSE:
            self.time_dict[name] = time.time()-t
            print(f"{name} finished in :", self.time_dict[name])

            if self.USE_WANDB:
                wandb.log({f"{name}": self.time_dict[name]})

########################### Implementations ############################

class GatDenoiserToyImagesDynamic(GatBase):
    """ Special toy images denoiser.
        This denoiser will generate new images in every epoch.
    """
    def _prepare_training_images(self, images):
        return

    def _prepare_training_data(self, batch_size, loss):
        images = draw_uniform_toyimages(self.RESOLUTION, 13, self.M)
        sinos = self.__forward__(torch.tensor(images).type(torch.float))

        if self.FIXED_SNR:
            snr = self.SNR
        else:
            snr = torch.randint(self.SNR_LOWER, self.SNR_UPPER, (1,))

        noisy_sinos = add_noise_to_sinograms(sinos, snr)

        if loss == Loss.SINO:
            dataset = SinogramToSinogramDataset(self.M, sinos, noisy_sinos, self.graph)
        elif loss == Loss.FBP:
            dataset = SinogramToImageDataset(self.M, images, noisy_sinos, self.graph)
        else:
            raise RuntimeError("Unknown loss value!")

        loader = DataListLoader(dataset=dataset, batch_size=batch_size, shuffle=True)
        return loader, snr

class GatDenoiserImagesFixed(GatBase):
    """ GatDenoiser for fixed images during training.
        During training, in every epoch the same images are passed to the model.
        Clean sinograms need to be only calculated once, if SNR is fixed,
        noisy sinograms are calculated only once as well.
    """
    def _prepare_training_images(self, images):
        self.training_images = torch.tensor(images).type(torch.float)
        self.training_sinos = self.__forward__(self.training_images)
        self.M: int = images.shape[0]

    def _prepare_training_data(self, batch_size, loss):
        if self.FIXED_SNR:
            if self.loader is None:
                self.training_sinos_noisy = add_noise_to_sinograms(self.training_sinos, self.SNR)

                if loss == Loss.SINO:
                    self.dataset = SinogramToSinogramDataset(self.M, self.training_sinos, self.training_sinos_noisy, self.graph)
                elif loss == Loss.FBP:
                    self.dataset = SinogramToImageDataset(self.M, self.training_images, self.training_sinos_noisy, self.graph)
                else:
                    raise RuntimeError("Unknown loss value!")

                self.loader = DataListLoader(dataset=self.dataset, batch_size=batch_size, shuffle=True)

            return self.loader, self.SNR
        else:
            snr = torch.randint(self.SNR_LOWER, self.SNR_UPPER, (1,))
            noisy_sinograms = add_noise_to_sinograms(self.training_sinos, snr)
            if loss == Loss.SINO:
                dataset = SinogramToSinogramDataset(self.M, self.training_sinos, noisy_sinograms, self.graph)
            elif loss == Loss.FBP:
                dataset = SinogramToImageDataset(self.M, self.training_images, noisy_sinograms, self.graph)
            else:
                raise RuntimeError("Unknown loss value!")

            loader = DataListLoader(dataset=dataset, batch_size=batch_size, shuffle=True)
            return loader, snr

class GatValidator(GatBase):
    """ GatValidator class which is only able to run validator from given trained model.
        Validator can be logged to wandb as well.
    """
    def _prepare_training_images(self, images):
         raise RuntimeError("Validator cannot be trained")

    def _prepare_training_data(self, batch_size, images=None):
         raise RuntimeError("Validator cannot be trained")


import copy
n = int(input())

graph = [] # 2차원 리스트
graph_rg = []
for i in range(n):
    graph.append(list((input())))

graph_rg = copy.deepcopy(graph)
for i in range(n):
    for j in range(n):
        if graph_rg[i][j] == 'G':
            graph_rg[i][j] = 'R'

def dfs(x,y,value): # 적록색약 아닌사람
    if x <= -1 or x >= n or y <= -1 or y >= n:
        return False

    if graph[x][y] == value:
        graph[x][y] = 'X' # 방문 완료
        dfs(x - 1, y,value)
        dfs(x, y-1,value)
        dfs(x+1,y,value)
        dfs(x, y+1,value)
        return True

    return False

def dfs_rg(x,y,value): # 적록색약인 사람
    if x <= -1 or x >= n or y <= -1 or y >= n:
        return False
    if graph_rg[x][y] == value:
        graph_rg[x][y] = 'X'
        dfs_rg(x - 1, y,value)
        dfs_rg(x, y-1,value)
        dfs_rg(x+1,y,value)
        dfs_rg(x, y+1,value)
        return True

    return False

result = 0
result_rg = 0

for i in range(n):
    for j in range(n):
        if dfs(i,j,'R') == True:
            result += 1
        elif dfs(i,j,'G') == True:
            result += 1
        elif dfs(i,j,'B') == True:
            result += 1

for i in range(n):
    for j in range(n):
        if dfs_rg(i,j,'R') == True:
            result_rg += 1
        elif dfs_rg(i,j,'B') == True:
            result_rg += 1

print(result, ' ', result_rg)


import pika
import json
import time
import settings
from settings import logger

class Queue:

    # declare all the queues names in this var
    QUEUES = ['default', 'test']

    def __init__(self, host=settings.RABBIT["host"],
                 port=settings.RABBIT["port"], user=settings.RABBIT["user"],
                 passw=settings.RABBIT["passw"], json_serial=True):
        self.json_serial = json_serial

        credentials = pika.PlainCredentials(user, passw)
        parameters = pika.ConnectionParameters(host, port, '/', credentials)

        self.conn = pika.BlockingConnection(
            pika.ConnectionParameters(host=host))
        self.channel = self.conn.channel()
        for queue in self.QUEUES:
            self.channel.queue_declare(
                queue=queue, arguments={'x-max-priority': 10})

    def __del__(self):
        self.conn.close()

    @staticmethod
    def _valid_queue_(name):
        if name not in Queue.QUEUES:
            raise ValueError('Undeclared queue name used: %s' % name)

    @staticmethod
    def _encode(body):
        return json.dumps(body)

    @staticmethod
    def _decode(body):
        return json.loads(body)

    def add(self, queue='default', body=None, priority=1):
        self._valid_queue_(queue)
        if body:
            if self.json_serial:
                body = self._encode(body)
            self.channel.basic_publish(
                exchange='', routing_key=queue, body=body,
                properties=pika.BasicProperties(priority=priority))

    def purge(self, queue):
        self._valid_queue_(queue)
        self.channel.queue_purge(queue)

    def get_message_count(self, queue):
        self._valid_queue_(queue)
        queue = self.channel.queue_declare(
            queue=queue, arguments={'x-max-priority': 10})
        return int(queue.method.message_count)

class QueueConsumer(Queue):

    RECONNECTION_DELAY = 3

    def __init__(self, queue='default', *args, **kwargs):
        while True:
            try:
                super(QueueConsumer, self).__init__(*args, **kwargs)
                self.channel.basic_consume(
                    queue=queue, on_message_callback=self.callback,
                    auto_ack=True)
                print(' [*] Waiting for messages. To exit press CTRL+C')
                self.channel.start_consuming()
            # Don't recover if connection was closed by broker
            except pika.exceptions.ConnectionClosedByBroker as ex:
                logger.exception(ex)
                break
            # Don't recover on channel errors
            except pika.exceptions.AMQPChannelError as ex:
                logger.exception(ex)
                break
            # Recover on all other connection errors
            except pika.exceptions.AMQPConnectionError as ex:
                logger.exception(ex)
                time.sleep(self.RECONNECTION_DELAY)
                continue

    @staticmethod
    def callback(ch, method, properties, body):
        body = Queue._decode(body)
        print(" [x] Received %r" % body)


import shutil
import tempfile
from http import HTTPStatus

from django.urls import reverse
from django.test import Client, TestCase, override_settings
from django.core.files.uploadedfile import SimpleUploadedFile
from django.contrib.auth import get_user_model
from django.core.cache import cache
from django.conf import settings

from ..models import Post, Group, Comment
from ..forms import PostForm

# Создаем временную папку для медиа-файлов;
# на момент теста медиа папка будет переопределена
TEMP_MEDIA_ROOT = tempfile.mkdtemp(dir=settings.BASE_DIR)

User = get_user_model()

@override_settings(MEDIA_ROOT=TEMP_MEDIA_ROOT)
class PostCreateFormTest(TestCase):
    @classmethod
    def setUpClass(cls):
        super().setUpClass()
        cls.user = User.objects.create(username='TestUserForm')
        cls.group = Group.objects.create(
            title='Заголовок тестовой группы',
            slug='test-slug',
            description='Тестовое описание',
        )
        cls.other_group = Group.objects.create(
            title='Заголовок новой группы',
            slug='new-slug',
            description='Тестовое описание',
        )
        cls.post = Post.objects.create(
            author=cls.user,
            text='Тестовый пост',
            group=cls.group,
        )
        cls.form = PostForm()

    @classmethod
    def tearDownClass(cls):
        super().tearDownClass()
        # Модуль shutil - библиотека Python с удобными инструментами
        # для управления файлами и директориями:
        # создание, удаление, копирование, перемещение, изменение папок и файлов
        # Метод shutil.rmtree удаляет директорию и всё её содержимое
        shutil.rmtree(TEMP_MEDIA_ROOT, ignore_errors=True)

    def setUp(self):
        self.guest_client = Client()
        self.authorized_client = Client()
        self.authorized_client.force_login(PostCreateFormTest.user)
        cache.clear()

    def test_create_post(self):
        post_count = Post.objects.count()
        small_gif = (
            b'\x47\x49\x46\x38\x39\x61\x02\x00'
            b'\x01\x00\x80\x00\x00\x00\x00\x00'
            b'\xFF\xFF\xFF\x21\xF9\x04\x00\x00'
            b'\x00\x00\x00\x2C\x00\x00\x00\x00'
            b'\x02\x00\x01\x00\x00\x02\x02\x0C'
            b'\x0A\x00\x3B'
        )
        uploaded = SimpleUploadedFile(
            name='small.gif',
            content=small_gif,
            content_type='image/gif'
        )
        form_data = {
            'author': self.user,
            'text': self.post.text,
            'group': self.group.pk,
            'image': uploaded
        }
        response = self.authorized_client.post(
            reverse('posts:post_create'),
            data=form_data,
            follow=True
        )
        self.assertRedirects(response, reverse(
            'posts:profile',
            kwargs={'username': self.user}
        ))
        last_post = Post.objects.order_by('pub_date').last()
        self.assertTrue(
            Post.objects.filter(
                group=self.group.pk,
                text=self.post.text,
                image='posts/small.gif',
            ).exists()
        )
        self.assertEqual(last_post.image.name,
                         'posts/' + form_data['image'].name)
        self.assertEqual(last_post.text, form_data['text'])
        self.assertEqual(last_post.group.id, form_data['group'])
        self.assertEqual(Post.objects.count(), post_count + 1)

    def test_edit_text_in_post(self):
        post_count = Post.objects.count()
        form_data = {
            'text': 'Редактированный пост',
            'group': self.other_group.id
        }
        response = self.authorized_client.post(
            reverse('posts:post_edit', kwargs={'post_id': self.post.id}),
            data=form_data,
            follow=True,
        )
        self.assertRedirects(response, reverse(
            'posts:post_detail',
            kwargs={'post_id': self.post.id}
        ))
        post = Post.objects.get(id=self.post.id)
        self.assertEqual(form_data['text'], post.text)
        self.assertEqual(self.other_group, post.group)
        self.assertEqual(Post.objects.count(), post_count)

    def test_comment_for_auth_user(self):
        post = Post.objects.create(
            text='Test post.',
            author=self.user,
        )
        comments_count = post.comments.count()
        form_data = {
            'text': 'Тестовый комментарий',
            'post': post.id,
            'author': self.user.id
        }
        response = self.authorized_client.post(
            reverse('posts:add_comment', kwargs={'post_id': post.id}),
            data=form_data,
            follow=True,
        )
        self.assertEqual(response.status_code, HTTPStatus.OK)
        self.assertEqual(post.comments.count(), comments_count + 1)


import base64
import logging
import uuid
from urllib.parse import urljoin

import requests

logging.basicConfig(level=logging.INFO)

class PhageAIConnector:
    """
    Generic PhageAI API connector
    """

    REQUEST_TIMEOUT = 30

    BASE_URL = "aHR0cHM6Ly9jb3JlLnBoYWdlLmFpL2FwaS92MS9waGFnZWFpLXBhY2thZ2Uv"

    def __init__(self, access_token: str) -> None:
        if not access_token:
            raise ValueError(
                "[PhageAI] Token Error: Please provide correct access token. If you need more information, please check README."
            )
        if self._is_uuid(access_token):
            raise ValueError(
                "[PhageAI] Token Error: We have change our TOS and Policy. Please login to PhageAI Web platform (https://app.phage.ai/) and create new access token."
            )
        self.access_token = access_token
        self.result = {}

    @staticmethod
    def _is_uuid(value):
        try:
            uuid.UUID(value)
            return True
        except ValueError:
            return False

    def _create_auth_header(self) -> dict:
        return {
            "Authorization": f"Bearer {self.access_token}",
        }

    @staticmethod
    def _encode(value: str) -> str:
        return base64.b64encode(value.encode()).decode("utf-8")

    @staticmethod
    def _decode(value: str) -> str:
        return base64.b64decode(value).decode("utf-8")

    def _create_url(self, path) -> str:
        return urljoin(self._decode(self.BASE_URL), self._decode(path))

    @staticmethod
    def _check_status(response):
        http_error_msg = ""
        if isinstance(response.reason, bytes):
            # We attempt to decode utf-8 first because some servers
            # choose to localize their reason strings. If the string
            # isn't utf-8, we fall back to iso-8859-1 for all other
            # encodings. (See PR #3538)
            try:
                reason = response.reason.decode("utf-8")
            except UnicodeDecodeError:
                reason = response.reason.decode("iso-8859-1")
        else:
            reason = response.reason

        if 400 <= response.status_code < 500:
            http_error_msg = f"[PhageAI] Connection Client Error: Response Status Code - {response.status_code} Reason: {reason}"
        elif 500 <= response.status_code < 600:
            http_error_msg = f"[PhageAI] Connection Server Error: Response Status Code - {response.status_code} Reason: {reason}"

        if http_error_msg:
            raise requests.HTTPError(http_error_msg, response=response)

    def _make_request(self, path: str, method: str, **kwargs) -> requests.Response:
        """
        Generic PhageAI API request method
        """

        headers = self._create_auth_header()

        logging.info(f"[PhageAI] Method: {method}")

        response = getattr(requests, method)(
            url=self._create_url(path),
            headers=headers,
            timeout=self.REQUEST_TIMEOUT,
            **kwargs,
        )

        self._check_status(response)

        return response


import sqlite3

#SELECIONANDO ITENS DA TABELA

conn = sqlite3.connect('db.sqlite3')
cursor = conn.cursor()

# lendo os dados
cursor.execute("""
SELECT * FROM clientes_cliente;
""")

for linha in cursor.fetchall():
    print(linha)

conn.close()


dictionary = {'One': 'Один', 'Two': 'Два', 'Three': 'Три', 'Four': 'Четыре'}
text = []

with open(r"C:\Users\Катя\Desktop\text_4.txt", encoding='utf-8') as new_file_1:
    for line in new_file_1:
        line_list = line.split()
        insert = dictionary.get(line_list[0])
        line_list.insert(0, insert)
        line_list.remove(line_list[1])
        text.append(' '.join(line_list))
        text.append('\n')

with open('new_text_4.txt', 'w+', encoding='utf-8') as new_file_2:
    new_file_2.writelines(text)
    new_file_2.seek(0)
    print(new_file_2.read())


import sys

from oabot.extract import EMAIL
from oabot.post import create_replies

if __name__ == "__main__":
    if EMAIL is None:
        raise TypeError(
            "Set the EMAIL environment variable to your email "
            "address for polite usage of the APIs."
        )
    test_text = """

A fake Mastodon post

Here's an
interesting article!"""
    print(test_text)
    print()
    print(create_replies(test_text)[0])
    print()
    test_text_2 = """

Here's another post with multiple URLs in it:

This one is a DOI and
this one is a URL.

Both are closed access, the first one has a preprint and the second one a postprint.
"""
    print()
    for reply in create_replies(test_text_2):
        print(reply)


# from django.urls import path
from django.contrib import admin
from django.conf.urls import include, url, i18n
# from home.views import saveimg, getsubcategory, changepassword, update_user, register_user, ajaxlogin, check_email, getPromoData, deleteAccount
from home.views import *
from video.views import *
from teacher.views import *

#POSTS
urlpatterns = [
    url(r'^login/$', ajaxlogin, name='login'),
    url(r'^register-user', register_user, name='register'),
    url(r'^update-user', update_user, name='update'),
    url(r'^check-email', check_email, name='check email'),
    url(r'^saveimg/$', saveimg, name='saveimg'),
    url(r'^getsubcategory$', getsubcategory, name='getsubcategory'),
    url(r'^changepassword/$', changepassword, name='changepassword'),
    url(r'^search_course/$', search_course, name='search_course'),
    url(r'^search_course2/$', search_course2, name='search_course2'),
	url(r'^student_courses/$', student_courses, name='student_courses'),

	url(r'^student_Cart_courses/$', student_Cart_courses, name='student_Cart_courses'),
    url(r'^delete_Cart_course_single/$', delete_Cart_course_single, name='delete_Cart_course_single'),
    url(r'^delete_Cart_courses_all/$', delete_Cart_courses_all, name='delete Cart courses all'),

	url(r'^student_favourite_courses/$', student_Favourite_courses, name='student_favourite_courses'),
    url(r'^delete_favourite_course_single/$', delete_Favourite_course_single, name='delete_Favourite_course_single'),
    url(r'^delete_favourite_courses_all/$', delete_Favourite_courses_all, name='delete Favourite courses all'),

	url(r'^sort_by_category/$', sort_by_category, name='sort_by_category'),
	url(r'^add_comment/$', add_comment, name='add_comment'),
	url(r'^delete_comment/$', delete_comment, name='add_comment'),
    url(r'^student-performance/$', student_performance, name='teacher student-performance'),

    #add mesingle_category
    url(r'^getPromoData/$', getPromoData, name='get promo data'),
    url(r'^get_course_detail_by_id/$', getCourseDetailForPromo, name='get course detail for promo'),
    url(r'^deleteAccount/$', deleteAccount, name='delete account'),
    url(r'^deleteCourse/$', deleteCourse, name='delete a course'),

    url(r'^saveQuizAnswer/$', saveQuizAnswer, name='delete a course'),
    url(r'^saveQuizMark/$', saveQuizMark, name='get mark to a student'),

    url(r'^getCertificate/$', getCertificate, name='get certificate'),

    url(r'^viewProfile/(?P<id>[0-9]+)/$', viewProfile, name='view profile'),
    url(r'^teacherProfile/$', teacherProfile, name='show teacher profile '),
    url(r'^becomeTeacher/$', becomeTeacher, name='show teacher profile '),

    # VIDEOS
    url(r'^video/playground/(?P<id>[0-9]+)/$', playground, name='video playground'),
    url(r'^video/playground/(?P<id>[0-9]+)/addtoprogress/$', addtoprogress, name='video addtoprogress'),

    url(r'^video/quiz2/$', video_quiz2, name='teacher quiz2'),
    # url(r'^video/quiz2/(?P<id>[0-9]+)/$', video_quiz2, name='teacher quiz2'),
    url(r'^video/quiz3/(?P<id>[0-9]+)/$', video_quiz3, name='teacher quiz3'),

    url(r'^save_become_teacher/$', save_become_teacher, name='save become teacher'),

    # messages part
    url(r'^get_message_history/$', getMessageHistory, name='get message history'),
    url(r'^delete_message_history/$', deleteMessageHistory, name='delete message history'),
    url(r'^set_message_read/$', setMessageRead, name='set message read'),
    url(r'^get_user_by_id/$', getUserById, name='get user by id'),

    #notification part
    url(r'^delete_notification_by_id/$', deleteNotificationById, name='delete notification by id'),
    url(r'^delete_notification/$', deleteNotification, name='delete notification'),
    url(r'^edit_notification/$', editNotification, name='edit notification'),

    #spam & refund part
    url(r'^report_spam/$', reportSpam, name='report spam'),
    url(r'^refund/$', refund, name='refund'),

    url(r'^search_course_name/$', searchCourseName, name='search course name'),

    #payment card info store
    url(r'^save_card_info/$', saveCardInfo, name='save card info'),

    #set privacy for teacher
    url(r'^set_privacy/$', setPrivacy, name='set privacy'),

    # delete course by id
    url(r'^delete_course_by_id/$', deleteCourseById, name='delete course by id'),

    #payment (hold => transfer)
    url(r'^send_transfer_request/$', sendTransferRequest, name='send transfer request'),
    url(r'^check_payout_status/$', checkPayoutStatus, name='check payout status'),

]


# I=list(map(int,L))
# print(list(map(lambda x:x+1,I)))
import csv
empregados={}

with open('empregados.cvs') as f:
    reader=csv.reader(f,delimiter=";")
    for l in reader:
        print(l)
    # for linha in f:
    #     partes=linha.strip().split(';')


# Trick: every element in output =
# product of all element in the left TIMES
# product of all element in the right
# So two passes will solve the problem,
# by accumulating the product from left and from right each pass
class Solution:
    def productExceptSelf(self, nums: List[int]) -> List[int]:
        multiplyer = 1
        output = []
        for i in range(len(nums)):
            output.append(multiplyer)
            multiplyer *= nums[i]
        multiplyer = 1
        for i in range(len(nums) - 1, -1, -1):
            output[i] *= multiplyer
            multiplyer *= nums[i]
        return output


from django.shortcuts import render
from rest_framework.views import APIView
from .serializers import *
from rest_framework.response import Response
from administrator.models import *
from .permissions import *

# Create your views here.

# urlpatterns = [
#     path('admission_form/', Admission_view.as_view(), name = 'admission-form'),
#     path('application_form/', Application_view.as_view(), name = 'application-form'),
#     path('payment_form/<str:name
# >/', PaymentView.as_view(), name = 'payment_update'),
#     path('course/', CourseView.as_view(), name = 'courses'),

# ]

class Admission_view(APIView):

    def post(self, request):

        serializer = Admission_Forms_detailSerializer(data = request.data)
        serializer.is_valid(raise_exception = True)
        serializer.save()

        return Response(serializer.data)

    def get(self, request):
        # create permission file
        queryset = Admission_Forms_detail.objects.all()
        serializer = Admission_Forms_detailSerializer(queryset, many=True)

        for applicant in queryset:
            if applicant.status == 'Pending Approval' and int(applicant.grade) <= 15:
                applicant.status = 'Approved'
                applicant.save()

        return Response(serializer.data)

class Application_view(APIView):

    def post(self, request):

        serializer = Application_form_detailSerializer(data = request.data)
        serializer.is_valid(raise_exception = True)
        serializer.save()

        return Response(serializer.data)

    def get(self, request):
        # create permission file
        permission_classes = (Admin_priviledge)
        queryset = Application_Form_detail.objects.all()
        serializer = Application_form_detailSerializer(queryset, many=True)

        for applicant in queryset:
            if applicant.duration_of_work == 'Full time':
                applicant.status = 'Approved'
                applicant.save()

        return Response(serializer.data)

class PaymentView(APIView):

    def get(self, request, name):
        permission_classes = (Student_priviledge, Admin_priviledge)
        queryset = Student_Payment_Details.objects.filter(name = name)
        serializer = Student_Payment_DetailsSerializer(queryset, many = True)

        return Response(serializer.data)

    def patch(self, request, name):
        permission_classes = (Admin_priviledge)
        queryset = Student_Payment_Details.objects.get(name = name)
        serializer = Student_Payment_DetailsSerializer(instance = queryset, data = request.data, partial=True)

        serializer.is_valid()
        serializer.save()

        return Response(serializer.data)

class CourseView(APIView):

    def post(self, request):

        permission_classes = (Admin_priviledge)
        serializer = CourseSerializer(data = request.data)
        serializers.is_valid(raise_exception = True)
        serializer.save()

        return Response(serilizer.data)

class RegisterationView(APIView):

    def post(self, request):
        serializer = UserDataSerializer(data = request.data)
        serializer.is_valid(raise_exception = True)
        serializer.save()

        return Response(serializer.data)

class StudentRegisterationView(APIView):
    # check this functionaltity
    def post(self, request):

        student = 0
        for students in Admission_Forms_detail.objects.all():

            if students.admission_number == int(request.data.get('admission_number')) and students.status == 'Approved':
                student = int(request.data.get('admission_number'))

        if student == int(request.data.get('admission_number')):
            pass
        else:
            print(type(student))
            print(student)
            print(int(request.data.get('admission_number')))
            print(type(int(request.data.get('admission_number'))))

            return Response({'Your admission was not approved':'Contact the school authorities or apply next time. Thank you'})

        serializer = StudentRegisterationSerializer(data = request.data)
        serializer.is_valid(raise_exception = True)
        serializer.save()

        return Response(serializer.data)

class TeacherRegisterationView(APIView):

    def post(Self, request):

        teacher = 0
        for teachers in Application_Form_detail.objects.all():

            if teachers.application_number == int(request.data.get('application_number')) and teachers.status == 'Approved':
                teacher = int(request.data.get('application_number'))

        if teacher == int(request.data.get('application_number')):
            pass
        else:

            return Response({'Your application was not approved':'Contact the school authorities or apply next time. Thank you'})

        serializer = TeacherRegisterationSerializer(data = request.data)
        serializer.is_valid(raise_exception = True)
        serializer.save()

        return Response(serializer.data)


n, k = map(int, input().split())
result = 0

while True:
  # (N == K로 나누어 떨어지는 수)가 될 때까지 1씩 빼기
  target = (n // k) * k
  result += (n - target) # 1씩 빼야하는 경우 한번에 계산해줌
  if n < k:
    break
  result += 1
  n //= k

# 마지막으로 남은 수에 대하여 1씩 빼기
result += (n - 1)
print(result)


"""
- Basic Concpets of SVM.
- Concpets of "linear separability" and "maximum margin".
- XOR Problem by utilizing kernels to achieve non-linear separation.
"""

# Gram Matrix [ Kernel Matrix]
#A' = [K(A,A), K(A,B), K(A,C), K(A,D)]
#B' = [K(B,A), K(B,B), K(B,C), K(B,D)]
#C' = [K(C,A), K(C,B), K(C,C), K(C,D)]
#D' = [K(D,A), K(D,B), K(D,C), K(D,D)]

"""
#Types of Kernels
#Linear
K(x,y) = x(T)*y

#Polynomial
K(x,y) = (x(T)*y+c)^d

#Sigmoid
K(x,y) = tanh(gamma*x(T)*y+c)

#Radial Basis function(RBF):
K(x,y) = exp(-gamma*||x-y||^2)
"""

from sklearn.cross_validation import train_test_split
from sklearn.metrics import classification_report
from sklearn.svm import SVC
import numpy as np

# generate the XOR data
tl = np.random.uniform(size=(100, 2)) + np.array([-2.0, 2.0])
tr = np.random.uniform(size=(100, 2)) + np.array([2.0, 2.0])
br = np.random.uniform(size=(100, 2)) + np.array([2.0, -2.0])
bl = np.random.uniform(size=(100, 2)) + np.array([-2.0, -2.0])
X = np.vstack([tl, tr, br, bl])
y = np.hstack([[1] * len(tl), [-1] * len(tr), [1] * len(br), [-1] * len(bl)])

# construct the training and testing split by taking 75% of the data for training
# and 25% for testing
(trainData, testData, trainLabels, testLabels) = train_test_split(X, y, test_size=0.25,
	random_state=42)

# train the linear SVM model, evaluate it, and show the results
print("[RESULTS] SVM w/ Linear Kernel")
model = SVC(kernel="linear")
model.fit(trainData, trainLabels)
print(classification_report(testLabels, model.predict(testData)))
print("")

# train the SVM + poly. kernel model, evaluate it, and show the results
print("[RESULTS] SVM w/ Polynomial Kernel")
model = SVC(kernel="poly", degree=2, coef0=1)
model.fit(trainData, trainLabels)
print(classification_report(testLabels, model.predict(testData)))


#! /usr/bin/env python3

import copy

# Enumerate is a built-in function in python. Its usefulness can not be summarized in a single line.
# It allows us to loop over something and have an automatic counter.

MY_LIST = ['apple', 'banana', 'grapes', 'pear']
for c, val in enumerate(MY_LIST,1):
    print(c,val)

LIST = ['apple', 'banana', 'grapes', 'pear']
COUNTER_LIST = list(enumerate(LIST,1))
print(COUNTER_LIST)

# zip
# The purpose of zip() is to map the similar index of multiple containers so that they can be used
# just using as single entity.

# initializing lists
name = [ "Manjeet", "Nikhil", "Shambhavi", "Astha" ]
roll_no = [ 4, 1, 3, 2 ]
marks = [ 40, 50, 60, 70 ]

mapped = set(zip(name, roll_no, marks))
print(mapped)

# How to unzip?
# Unzipping means converting the zipped values back to the individual self as they were. This is done
# with the help of “*” operator.

# unzipping values
namz, roll_noz, marksz = zip(*mapped)
print(namz)
print(roll_noz)
print(marksz)

# Practical Applications : There are many possible applications that can be said to be exected using zip,
# be it student database or scorecard or any other utility that requires mapping of groups.

# Lambdas
# Lambdas are one line functions. They are also known as anonymous functions in some other languages.
# You might want to use lambdas when you don’t want to use a function twice in a program. They are
# just like normal functions and even behave like them.
add = lambda x, y: x + y
print(add(3, 5))

# list sorting
lis = [(1, 2), (4, 1), (9, 10), (13, -3)]
lis.sort(key=lambda x: x[1])
print(lis)

list1 = [2, 6, 8, 4]
list2 = [1, 5, 7, 3]
print(list1, list2)
data = zip(list1, list2)
temp_data = copy.deepcopy(data)
print(list(tsemp_data))
list1, list2 = map(lambda x: list(x), zip(*data))
print(list1, list2)

# In Python, Assignment statements do not copy objects, they create bindings between a target and an object.
# When we use = operator user thinks that this creates a new object; well, it doesn’t. It only creates a new
# variable that shares the reference of the original object. Sometimes a user wants to work with mutable
# objects, in order to do that user looks for a way to create “real copies” or “clones” of these objects.
# Or, sometimes a user wants copies that user can modify without automatically modifying the original at
# the same time, in order to do that we create copies of objects.

# A copy is sometimes needed so one can change one copy without changing the other. In Python, there are two
# ways to create copies :
#   Deep copy
#   Shallow copy
# In order to make these copy, we use copy module. We use copy module for shallow and deep copy operations.

# Deep copy is a process in which the copying process occurs recursively. It means first constructing a new
# collection object and then recursively populating it with copies of the child objects found in the original.
# In case of deep copy, a copy of object is copied in other object. It means that any changes made to a copy
# of object do not reflect in the original object. In python, this is implemented using “deepcopy()” function.

# A shallow copy means constructing a new collection object and then populating it with references to the
# child objects found in the original. The copying process does not recurse and therefore won’t create copies
# of the child objects themselves. In case of shallow copy, a reference of object is copied in other object.
# It means that any changes made to a copy of object do reflect in the original object. In python, this is
# implemented using “copy()” function.


from collections import deque
def solution(maps):
    answer = []
    visited = [[False] * len(maps[0]) for _ in range(len(maps))]
    dx, dy = [-1,1,0,0], [0,0,-1,1]

    for i in range(len(maps)):
        for j in range(len(maps[0])):
            if not visited[i][j] and maps[i][j] != "X":
                visited[i][j] = True
                temp = int(maps[i][j])
                q = deque()
                q.append([i,j])
                while q:
                    x, y = q.popleft()
                    for k in range(4):
                        nx, ny = x + dx[k], y + dy[k]
                        if 0 <= nx < len(maps) and 0 <= ny < len(maps[0]):
                            if not visited[nx][ny] and maps[nx][ny] != "X":
                                temp += int(maps[nx][ny])
                                q.append([nx,ny])
                                visited[nx][ny] = True
                answer.append(temp)
    return sorted(answer) if answer != [] else [-1]


#!/share/data1/zhangy/software/miniconda/envs/bio37/bin/python
import re
import sys
import argparse

def get_args():
    parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawTextHelpFormatter)
    parser.add_argument("i", metavar="", help="download the result of search for ko number")
    args = parser.parse_args()
    return args

def main(file_):
    f = open(file_, 'r')
    for line in f:
        # 找定位行
        if re.search(r"div\s+style=.width:600px.>", line.strip()):
            # 解析
            pattern = re.compile(r'
(.*?)

\s+(K\d{5})\s+(.*?)

')
            gene_iter = re.finditer(pattern, line.strip())
            for gene in gene_iter:
                print(f'{gene.group(1)}\t{gene.group(2)}\t{gene.group(3)}')
    f.close()

if __name__ == "__main__":
    args = get_args()
    input_file = args.i
    main(input_file)


import sys
import os
sys.path.append(os.path.join(os.path.abspath(__file__).rsplit('/', 2)[0], 'accessory'))  # LLaMA2-Accessory/accessory

import numpy as np
import torch
import torch.distributed as dist
import multiprocessing as mp

from fairscale.nn.model_parallel import initialize as fs_init

from model.meta import MetaModel

from util.misc import setup_for_distributed
from util.tensor_parallel import load_tensor_parallel_model_list
from util.tensor_type import default_tensor_type
from data.transform import get_transform
from data.conversation.lib import conv_templates, SeparatorStyle
from PIL import Image

# *********************** Begin Configuration ***********************
# todo Please modify the following variables to fit your environment and needs

SPHINX_TYPE = "SPHINX-1k"  # "SPHINX" or "SPHINX-1k"
TOKENIZER_PATH = "/PATH/TO/tokenizer.model"  # path to llama tokenizer
PRETRAINED_PATH = "/PATH/TO/PRETRAINED"

IMAGE_PATH = "examples/1.jpg"  # image for inference, can be None as SPHINX also supports text-only dialog
INSTRUCTION = "Please describe the image in detail."

MODEL_PARALLEL_SIZE = 2  # equal to the number of GPUs to use for inference, current either 1 or 2 is supported

# ************************ End Configuration ************************

if SPHINX_TYPE == "SPHINX-1k":
    LLAMA_TYPE = "llama_ens5" # SPHINX-1k
elif SPHINX_TYPE == "SPHINX":
    LLAMA_TYPE = "llama_ens"
else:
    raise ValueError(f"unknown SPHINX type {SPHINX_TYPE}")

def main(world_size=1, rank=0) -> None:
    # ****************** Begin Environment Setup ******************

    # SPHINX model definition relies on fairscale model parallel
    # so distributed process groups need to be initialized even if when only one gpu is used
    dist.init_process_group(
        backend="nccl", rank=rank, world_size=world_size,
        init_method=f"tcp://127.0.0.1:23560",
    )
    fs_init.initialize_model_parallel(model_parallel_size_=world_size)
    torch.cuda.set_device(rank)

    torch.manual_seed(1)
    np.random.seed(1)

    # block output from ranks other than rank 0
    setup_for_distributed(rank == 0)

    # ******************* End Environment Setup *******************

    # ******************** Begin Create Model *********************

    with default_tensor_type(dtype=torch.float16, device="cuda"):
        model = MetaModel(
            LLAMA_TYPE, llama_config=[], tokenizer_path=TOKENIZER_PATH,
            with_visual=True, max_seq_len=4096,
        )
    print("Loading pretrained weights ...")
    load_result = load_tensor_parallel_model_list(model, [PRETRAINED_PATH])
    print("load result:\n", load_result)
    assert load_result == {'missing_keys': [], 'unexpected_keys': []}, "checkpoint and model mismatch"
    model.eval()

    # ********************* End Create Model *********************

    # ****************** Begin Construct Input *******************

    if IMAGE_PATH is not None:
        image = Image.open(IMAGE_PATH)
        image = image.convert("RGB")
        target_size = getattr(model.llma, 'image_size', 224)  # 448 for SPHINX-1k, 224 for SPHINX
        image = get_transform("padded_resize", target_size)(image).unsqueeze(0).cuda().half()
    else:
        image = None  # SPHINX also supports text-only dialog

    conv = conv_templates["v1"].copy()

    conv.append_message("Human", INSTRUCTION)
    conv.append_message("Assistant", None)

    prompt = conv.get_prompt()
    print(prompt)

    # ******************* End Construct Input ********************

    # ********************* Begin Inference **********************

    conv_sep = (
        conv.sep
        if conv.sep_style == SeparatorStyle.SINGLE
        else conv.sep2
    )  # conv_sep marks the end of response

    with torch.cuda.amp.autocast(dtype=torch.float16):

        for stream_response in model.stream_generate(
            prompt, image, max_gen_len=512, temperature=0.1, top_p=0.75
        ):
            end_pos = stream_response["text"].find(conv_sep)
            if end_pos != -1:  # response ends
                stream_response["text"] = (
                    stream_response['text'][:end_pos].rstrip() + "\n"
                )
                break

    print(stream_response['text'])

    # ********************** End Inference ***********************

if __name__ == "__main__":
    if MODEL_PARALLEL_SIZE == 1:
        main(world_size=1, rank=0)
    elif MODEL_PARALLEL_SIZE == 2:
        mp.set_start_method("spawn")
        for rank in range(MODEL_PARALLEL_SIZE):
            process = mp.Process(
                target=main,
                args=(MODEL_PARALLEL_SIZE, rank),
            )
            process.start()
    else:
        raise ValueError("Currently only 1 or 2 is supported for MODEL_PARALLEL_SIZE")


import tensorflow as tf
import numpy as np

def relation_embedding(bag_rel, rel_tot, embedding_dim=5, var_scope=None):
    with tf.variable_scope(var_scope or 'relation_embedding', reuse=tf.AUTO_REUSE):
        relation_matrix = tf.get_variable('relation_matrix', shape=[rel_tot, embedding_dim], dtype=tf.float32,
                                          initializer=tf.contrib.layers.xavier_initializer())
        bag_rel = tf.cast(bag_rel, dtype=tf.float32)
        x = tf.matmul(bag_rel, relation_matrix)
    return x

def init_relation_embedding(train_data_loader, var_scope=None, relation_embedding_dim=50, level=1):
    rel2id = train_data_loader.rel2id
    word2id = train_data_loader.word2id
    word_vec_mat = train_data_loader.word_vec_mat
    with tf.variable_scope(var_scope or 'relation_embedding', reuse=tf.AUTO_REUSE):
        rel_tol = len(rel2id)
        relation_embedding = np.zeros(shape=[rel_tol, relation_embedding_dim], dtype=np.float)

        for rel, idx in rel2id.items():
            rel_key = rel.split('/')[-1]
            if rel_key in word2id:
                key_id = word2id[rel_key]
                relation_embedding[idx, :] = word_vec_mat[key_id, :]
            else:
                relation_embedding[idx, :] = np.random.normal(scale=0.1, size=relation_embedding_dim)
        x = tf.get_variable('relation_embedding', initializer=relation_embedding.astype(np.float32), dtype=tf.float32)
    return x

def word_embedding(word, word_vec_mat, var_scope=None, word_embedding_dim=50, add_unk_and_blank=True):
    with tf.variable_scope(var_scope or 'word_embedding', reuse=tf.AUTO_REUSE):
        word_embedding = tf.get_variable('word_embedding', initializer=word_vec_mat, dtype=tf.float32)
        if add_unk_and_blank:
            word_embedding = tf.concat([word_embedding,
                                        tf.get_variable("unk_word_embedding", [1, word_embedding_dim], dtype=tf.float32,
                                                        initializer=tf.contrib.layers.xavier_initializer()),
                                        tf.constant(np.zeros((1, word_embedding_dim), dtype=np.float32))], 0)
        x = tf.nn.embedding_lookup(word_embedding, word)
        return x

def pos_embedding(pos1, pos2, var_scope=None, pos_embedding_dim=5, max_length=120):
    with tf.variable_scope(var_scope or 'pos_embedding', reuse=tf.AUTO_REUSE):
        pos_tot = max_length * 2

        pos1_embedding = tf.get_variable('real_pos1_embedding', [pos_tot, pos_embedding_dim], dtype=tf.float32,
                                         initializer=tf.contrib.layers.xavier_initializer())
        # pos1_embedding = tf.concat([tf.zeros((1, pos_embedding_dim), dtype=tf.float32), real_pos1_embedding], 0)
        pos2_embedding = tf.get_variable('real_pos2_embedding', [pos_tot, pos_embedding_dim], dtype=tf.float32,
                                         initializer=tf.contrib.layers.xavier_initializer())
        # pos2_embedding = tf.concat([tf.zeros((1, pos_embedding_dim), dtype=tf.float32), real_pos2_embedding], 0)

        input_pos1 = tf.nn.embedding_lookup(pos1_embedding, pos1)
        input_pos2 = tf.nn.embedding_lookup(pos2_embedding, pos2)
        x = tf.concat([input_pos1, input_pos2], -1)
        return x

def rel_embedding(rel, relation_embedding, var_scope=None, embedding_dim=50, level=1):
    if len(rel.shape) != 2:
        raise NotImplementedError
    with tf.variable_scope(var_scope or 'relation_embedding', reuse=tf.AUTO_REUSE):
        relation_embedding = tf.get_variable('relation_embedding', initializer=relation_embedding, dtype=tf.float32)
        x = tf.matmul(rel, relation_embedding)
        # x = tf.expand_dims(rel, 1)
        # x = tf.tile(x, [1, embedding_dim, 1])
        # x = tf.map_fn(lambda x_: tf.reduce_sum(relation_embedding * tf.transpose(x_), 0), x)
        return x

def word_position_embedding(word, word_vec_mat, pos1, pos2, var_scope=None, word_embedding_dim=50, pos_embedding_dim=5,
                            max_length=120, add_unk_and_blank=True):
    w_embedding = word_embedding(word, word_vec_mat, var_scope=var_scope, word_embedding_dim=word_embedding_dim,
                                 add_unk_and_blank=add_unk_and_blank)
    p_embedding = pos_embedding(pos1, pos2, var_scope=var_scope, pos_embedding_dim=pos_embedding_dim,
                                max_length=max_length)
    return tf.concat([w_embedding, p_embedding], -1)


import cv2
import numpy as np

balon_cascade = cv2.CascadeClassifier('/media/yesill/Yerel Disk/Programlama/Projeler/uav/cascades/cascades/91p2381n16stage.xml')

cap = cv2.VideoCapture(0)

while cap.isOpened():
    success, frame = cap.read()
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    objects = balon_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=8)
    for (x,y,w,h) in objects:
        cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 3)
        cv2.putText(frame, f"balon", (x, y+5), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (0, 255, 0), 2)
        roi_color = frame[y:y+h, x:x+w]

    cv2.imshow("title", frame)

    command = cv2.waitKey(1)
    if command & 0xFF == ord('q'):
        print(f'program sonlandirildi')
        break
    elif command & 0xFF == ord('r'):
        print(f'kirmizi balonlağg tespit ediliyoğg')
    elif command & 0xFF == ord('g'):
        print(f'yeschill balonlağg tespit ediliyoğg')
    elif command & 0xFF == ord('b'):
        print(f'movi balonlağg tespit ediliyoğg')

cap.release()
cv2.destroyAllWindows()