##encoding=utf8
##version =py27, py34
##author  =sanhe
##date    =2014-09-11

from __future__ import print_function
from HSH.LinearSpider.logger import Log

def unit_test():
    log = Log()
    log.write("request denied", ("www.python.org", 5))
    log.clear_all()

if __name__ == "__main__":
    unit_test()


from celery import shared_task
from matches.models import Match, Delivery
import logging
logger = logging.getLogger(__file__)

@shared_task
def upload_matches(matches):
    from matches.service.match_service import MatchService
    match_list = []
    logger.info('Match uploading started')
    for match in matches:
        if not Match.objects.filter(id=match['id']).exists():
            match_list.append(Match(**match))
    Match.objects.bulk_create(match_list)
    logger.info('Match uploading done')
    MatchService().clear_cache()

@shared_task
def upload_deliveries(deliveries):
    from matches.service.match_service import MatchService
    delivery_list = []
    logger.info('Delivery uploading started')
    for delivery in deliveries:
        delivery['match'] = Match.objects.get(id=delivery['match_id'])
        del delivery['match_id']
        if not Delivery.objects.filter(**delivery).exists():
            delivery_list.append(Delivery(**delivery))
    Delivery.objects.bulk_create(delivery_list)
    logger.info('Delivery uploading done')
    MatchService().clear_cache()


import time
import random

from src.fuzzy_asteroids.fuzzy_controller import *
from src.fuzzy_asteroids.fuzzy_asteroids import FuzzyAsteroidGame, Scenario

def timeout(input_data):
    # Function for testing the timing out by the simulation environment
    wait_time = random.uniform(0.02, 0.03)
    time.sleep(wait_time)

class T1Controller(ControllerBase):
    @property
    def name(self) -> str:
        return "Team 1 Controller"

    def actions(self, ownship: SpaceShip, input_data: Dict[str, Tuple]) -> None:
        # timeout(input_data)
        ship_list = input_data["ships"]

        # for ship in ship_list:
        #     # print(ship["id"], ship["team"], ship["position"])
        #     if ship["id"] != ownship.id:
        #         print("Other ship at: ", ship["position"])

        if ownship.team == 1:
            ownship.turn_rate = random.uniform(ownship.turn_rate_range[0]/2.0, ownship.turn_rate_range[1])
            ownship.thrust = random.uniform(ownship.thrust_range[0], ownship.thrust_range[1])
            # ownship.fire_bullet = random.uniform(0.45, 1.0) < 0.5
            ownship.shoot()
            # print(ownship)
        else:
            ownship.turn_rate = random.uniform(ownship.turn_rate_range[0] / 2.0, ownship.turn_rate_range[1])
            ownship.thrust = random.uniform(ownship.thrust_range[0], ownship.thrust_range[1])
            ownship.fire_bullet = random.uniform(0.0, 1.0) < 0.5

class T2Controller(ControllerBase):
    @property
    def name(self) -> str:
        return "Team 2 Controller"

    def actions(self, ownship: SpaceShip, input_data: Dict[str, Tuple]) -> None:
        # timeout(input_data)

        if ownship.team == 2:
            ownship.turn_rate = random.uniform(-ownship.turn_rate_range[0]/2.0, ownship.turn_rate_range[1])
            ownship.thrust = random.uniform(ownship.thrust_range[0]/2.0, ownship.thrust_range[1])
            # ownship.fire_bullet = random.uniform(0.0, 1.0) < 0.5
            ownship.shoot()
        else:
            ValueError("Wrong controller called, ship assigned team other than 2 but reached team 2 controller")

        # print(ownship.bullets_remaining)
#
# class Controllers(ControllerBase):
#     @property
#     def name(self) -> str:
#         return "Main Controller Class"
#
#     def __int__(self, t1_controller, t2_controller):
#         self.team1 = t1_controller
#         self.team2 = t2_controller
#
#     @staticmethod
#     def team1_controller():
#         return T1Controller()
#
#     @staticmethod
#     def team2_controller():
#         return T2Controller()

if __name__ == "__main__":
    # Available settings
    settings = {
        "frequency": 30,
        "real_time_multiplier": 2,
        "graphics_on": True,
        "sound_on": False,
        "prints": True,
        "full_dashboard": True
    }

    # Instantiate an instance of FuzzyAsteroidGame
    game = FuzzyAsteroidGame(settings=settings,
                             track_compute_cost=True,
                             controller_timeout=True,
                             ignore_exceptions=False)

    # scenario_ship = Scenario(name="Multi-Ship",
    #                          num_asteroids=4,
    #                          ship_states=[{"position": (300, 500), "angle": 180, "lives": 3, "team": 1},
    #                                       {"position": (500, 300), "angle": 180, "lives": 3, "team": 2},
    #                                       ],
    #                          ammo_limit_multiplier=0.05,
    #                          stop_if_no_ammo=True)

    # scenario_ship = Scenario(name="Multi-Ship",
    #                          num_asteroids=4,
    #                          ship_states=[{"position": (300, 500), "angle": 180, "lives": 3, "team": 1}],
    #                          ammo_limit_multiplier=0.5,
    #                          stop_if_no_ammo=True)

    scenario_ship = Scenario(name="Multi-Ship",
                             num_asteroids=1,
                             ship_states=[{"position": (300, 500), "angle": 180, "lives": 3, "team": 1},
                                          {"position": (500, 300), "angle": 180, "lives": 3, "team": 2},
                                          ],
                             ammo_limit_multiplier=0.9)

    # controllers = [T1Controller(), T2Controller()]
    controllers = {1: T1Controller(), 2: T2Controller()}
    # cd = Controllers()
    # score = game.run(controller=FuzzyController(), scenario=scenario_ship)
    score = game.run(controller=controllers, scenario=scenario_ship)
    print(score.bullets_hit_asteroids)
    print(score.bullets_fired)
    print(score.accuracy)


myList = ["apple", "banana", "cherry"]
newList = ["mango", "pineapple", "papaya"]

# ADDING AN ITEM TO THE END OF A LIST
myList.append("orange")
print(myList)

# TO INSERT AT A SPECIFIED INDEX
myList.insert(1, "strawberry")
print(myList)

# APPEND ELEMENTS FROM ANOTHER LIST
myList.extend(newList)
print(myList)


"""main"""
def main():
    """main docx"""
    price = int(input())
    cap = int(input())
    promo = int(input())
    money = int(input())
    milk, bottle = 0, 0
    while money >= price:
        money -= price
        milk += 1
    while milk >= cap:
        if cap == 0:
            break
        milk -= cap
        bottle += cap
        milk += promo
    print(bottle+milk)

main()


import os
import sys

sys.path.insert(0, "../../util/python")
import Cons
import Util

def Read(log_datetime):
	return Log(log_datetime)

class Log:
	def __init__(self, log_datetime):
		self.log_datetime = log_datetime
		self._GetStorageSizeCost()

	def _GetStorageSizeCost(self):
		# Note: What other info might be worth it from the Cassndra log file?
		# - When tablets migrate.
		# - Number of requests to each tablet.

		# Get hot and cold storage size
		fn = "%s/work/cassandra/mtdb/process-log/calc-cost-latency-plot-tablet-timeline/plot-data/%s-storage-size-by-time" \
				% (os.path.expanduser("~"), self.log_datetime)
		# Generate one, if it doesn't exist
		if not os.path.isfile(fn):
			cmd = "%s/work/cassandra/mtdb/process-log/calc-cost-latency-plot-tablet-timeline/plot-cost-latency-tablet-timelines.py %s" \
					% (os.path.expanduser("~"), self.log_datetime)
			Util.RunSubp(cmd)

		with open(fn) as fo:
			lines = []
			for line in fo.readlines():
				#Cons.P(line)
				line = line.rstrip()
				lines.append(line)
			lines = lines[-5:]
			t = lines[0].split()
			if len(t) != 6:
				raise RuntimeError("Unexpected format [%s]" % lines[0])

			# The storage size for the cold and the hot at the end of the
			# simulation.  The total size will be used to calculate the
			# fill-up-1.6TB-disks cost.
			self.hot_stg_usage  = float(t[1])
			self.cold_stg_usage = float(t[2])

			# Cumulative cost for the entire the simulation period
			self.hot_stg_cost   = float(t[3])
			self.cold_stg_cost  = float(t[4])

			#Cons.P(self.hot_stg_usage)
			#Cons.P(self.cold_stg_usage)
			#Cons.P(self.hot_stg_cost)
			#Cons.P(self.cold_stg_cost)

			# Some of the last simulated times are longer than 8 years because the
			# system was saturated.  Logs from unsaturated systems says 8 years.
			# For instance,
			# ~/work/apache-cassandra-2.2.3-src/mtdb/process-log/calc-cost-latency-plot-tablet-timeline/plot-data/160302-151257-storage-size-by-time

	def __str__(self):
		return " ".join("%s=%s" % item for item in vars(self).items())


# -*- coding: utf-8 -*-
"""
Created on Tue Mar 27 18:14:21 2018

@author: PavitrakumarPC
"""

import keras
import numpy as np
import cv2
import os
import pandas as pd
import matplotlib.pyplot as plt
from keras.layers import Conv2D, Dense, Input, Dropout, Flatten, Activation, MaxPooling2D, AveragePooling2D, BatchNormalization
from keras.models import Sequential, Model
from keras.optimizers import Adam, SGD
from keras import backend as K
from keras.utils.np_utils import to_categorical
from keras.models import load_model
from keras.preprocessing.image import ImageDataGenerator
from keras.callbacks import ModelCheckpoint
from keras.models import model_from_json
import json
K.set_image_dim_ordering('tf')

root_dir = ''

train_data = pd.read_hdf(os.path.join(root_dir,'data','train_data_processed.h5'),'table')

train_data = train_data[(train_data['num_digits']!=6) & (train_data['num_digits']!=5)]
#very less 6-digit/5-digit train samples, so we stop with 4

#training
train_img_size = (64,64)

train_data['top'] = train_data['top']*(train_img_size[0]/train_data['img_height'])
train_data['left'] = train_data['left']*(train_img_size[1]/train_data['img_width'])
train_data['bottom'] = train_data['bottom']*(train_img_size[0]/train_data['img_height'])
train_data['right'] = train_data['right']*(train_img_size[1]/train_data['img_width'])
train_data['width'] = train_data['right'] - train_data['left']
train_data['height'] = train_data['bottom'] - train_data['top']

#if low memory when testing, use below conversions:
train_data['top'] = train_data['top'].astype(np.int64)
train_data['left'] = train_data['left'].astype(np.int64)
train_data['bottom'] = train_data['bottom'].astype(np.int64)
train_data['right'] = train_data['right'].astype(np.int64)
train_data['width'] = train_data['width'].astype(np.int64)
train_data['height'] = train_data['height'].astype(np.int64)

train_labels = np.array(train_data[['top','left','width','height']])
tmp = []
train_images = train_data['img']
for img in train_images:
    tmp.append(cv2.normalize(cv2.cvtColor(cv2.resize(img,train_img_size), cv2.COLOR_BGR2GRAY).astype(np.float64), 0, 1, cv2.NORM_MINMAX)[...,np.newaxis])

train_images = np.array(tmp)
single_img_shape = train_images[0].shape

k_size = 3
cnn1 = Input(shape=single_img_shape)
#x = BatchNormalization()(cnn1)
x = Conv2D(32, k_size, k_size, activation='relu', border_mode='same')(cnn1)
x = BatchNormalization()(x)
x = Conv2D(32, k_size, k_size, activation='relu', border_mode='same')(x)
x = BatchNormalization()(x)
x = Dropout(0.2)(x)
x = MaxPooling2D(pool_size=(2,2))(x)
x = Conv2D(64, k_size, k_size, activation='relu', border_mode='same')(x)
x = BatchNormalization()(x)
x = Conv2D(64, k_size, k_size, activation='relu', border_mode='same')(x)
x = BatchNormalization()(x)
x = Dropout(0.2)(x)
x = MaxPooling2D(pool_size=(2,2))(x)
x = Conv2D(128, k_size, k_size, activation='relu', border_mode='same')(x)
x = BatchNormalization()(x)
x = Conv2D(128, k_size, k_size, activation='relu', border_mode='same')(x)
x = BatchNormalization()(x)
x = Dropout(0.2)(x)
x = Flatten()(x)
x = Dense(1024, activation='relu')(x)
x = BatchNormalization()(x)
x = Dense(1024, activation='relu')(x)
x = BatchNormalization()(x)
h = Dense(4)(x)
model = Model(input=cnn1, output=h)
model.compile(loss='mse', optimizer='adadelta')
model.fit(train_images, train_labels, epochs=50, batch_size=128, validation_split=0.2, verbose=1)
#final training loss: 0.9939 | val loss: 17.47 (val loss fluctuated between 15 and 25)

#~~~~~~~~~~~~~~~~~~~~~~~~~~testing~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
test_data = pd.read_hdf(os.path.join(root_dir,'data','test_data_processed.h5'),'table')

test_data = test_data[(test_data['num_digits']!=6) & (test_data['num_digits']!=5)]

test_img_size = (64,64)

test_labels = np.array(test_data[['top','left','width','height']])
tmp = []
test_images = test_data['img']
for img in test_images:
    tmp.append(cv2.normalize(cv2.cvtColor(cv2.resize(img,test_img_size), cv2.COLOR_BGR2GRAY).astype(np.float64), 0, 1, cv2.NORM_MINMAX)[...,np.newaxis])

test_images = np.array(tmp)

actual_labels = test_labels
preds = model.predict(test_images)

print('mean squared error difference',np.sum(np.power(actual_labels-preds,2))) #96777101.9255
print('mean error difference',np.sum(np.abs(actual_labels-preds))) #1198707.77919

#~~~~~~~~~~~~~~~~~~~~~~~~~~~saving~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Architecture
with open(os.path.join(root_dir,'cnn_models','digit_detection_cnn_layers.json'),"w") as f:
    f.write(model.to_json())

f.close()
# Weights
model.save_weights(os.path.join(root_dir,'cnn_models','digit_detection_cnn_weights.h5'))

#model.save(os.path.join(root_dir,'cnn_models','digit_detection_cnn_fullmodel.h5'))

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


from __future__ import annotations

import dataclasses as dc
import itertools as it
import typing as t
import heapq as hq
import pprint as pp

Amphipod = t.Literal["A"] | t.Literal["B"] | t.Literal["C"] | t.Literal["D"]
Stack = tuple[Amphipod, Amphipod]
PStack = tuple[Amphipod, ...]

costs: dict[Amphipod, int] = {"A": 1, "B": 10, "C": 100, "D": 1000}

@dc.dataclass(frozen=True)
class Arrangement:
    pos: tuple[Stack, Stack, Stack, Stack]

    @staticmethod
    def parse_line() -> list[Amphipod]:
        return [t.cast(Amphipod, c) for c in input() if c in costs.keys()]

    @staticmethod
    def parse():
        input()
        input()
        pos = t.cast(
            tuple[Stack, Stack, Stack, Stack],
            tuple(zip(Arrangement.parse_line(), Arrangement.parse_line())),
        )
        return Arrangement(pos)

    @staticmethod
    def terminals() -> t.Iterable[Arrangement]:
        for w, x, y, z in it.permutations("ABCD"):
            yield Arrangement(((w, w), (x, x), (y, y), (z, z)))

@dc.dataclass(frozen=True, order=True)
class Situation:
    pos: tuple[PStack, ...]
    hallway: tuple[Amphipod | t.Literal[""], ...] = tuple("" for _ in range(11))

    def neighbors(self) -> t.Iterable[tuple[int, Situation]]:
        # move out of room for the first time
        for i, p in enumerate(self.pos):
            if not p:  # nothing to move
                continue

            rest, mover = p[:-1], p[-1]

            # move right
            x = (1 + i) * 2
            for j in range(x + 1, 11):
                if j in [2, 4, 6, 8]:  # room slots
                    continue
                if self.hallway[j]:  # blocked
                    break
                # if len(p) == 2, then one step to enter hallway;
                # if 1, then two steps.
                cost = costs[mover] * ((3 - len(p)) + (j - x))
                new_pos = [*self.pos]
                new_pos[i] = rest
                new_hallway = list(self.hallway)  # TODO pyright error when [*]
                new_hallway[j] = mover
                assert cost > 0
                yield cost, Situation(tuple(new_pos), tuple(new_hallway))

            # move left
            for j in range(x - 1, -1, -1):
                if j in [2, 4, 6, 8]:  # room slots
                    continue
                if self.hallway[j]:  # blocked
                    break
                cost = costs[mover] * ((3 - len(p)) + (x - j))
                new_pos = [*self.pos]
                new_pos[i] = rest
                new_hallway = list(self.hallway)  # TODO pyright error when [*]
                new_hallway[j] = mover
                assert cost > 0
                yield cost, Situation(tuple(new_pos), tuple(new_hallway))

        # move into final room
        for i, a in enumerate(self.hallway):
            if not a:
                continue

            # target rooms on right
            for j in range(i + 1, 9):
                if j not in [2, 4, 6, 8]:
                    continue
                if self.hallway[j]:
                    break
                r = j // 2 - 1
                room = self.pos[r]
                if len(room) == 2:
                    continue
                # if room is 1, then takes 1 cost; if empty, then takes 2 steps
                cost = costs[a] * ((j - i) + (2 - len(room)))
                new_hallway = list(self.hallway)
                new_hallway[i] = ""
                new_pos = list(self.pos)
                new_pos[r] = room + (a,)
                assert cost > 0
                yield cost, Situation(tuple(new_pos), tuple(new_hallway))

            # target rooms on left
            for j in range(i - 1, 1, -1):
                if j not in [2, 4, 6, 8]:
                    continue
                if self.hallway[j]:
                    break
                r = j // 2 - 1
                room = self.pos[r]
                if len(room) == 2:
                    continue
                if room and room[0] != a:  # can't finalize
                    continue
                # if room is 1, then takes 1 cost; if empty, then takes 2 steps
                cost = costs[a] * ((i - j) + (2 - len(room)))
                new_hallway = list(self.hallway)
                new_hallway[i] = ""
                new_pos = list(self.pos)
                new_pos[r] = room + (a,)
                assert len(room) <= 2
                assert cost > 0
                yield cost, Situation(tuple(new_pos), tuple(new_hallway))

        # TODO account for refusal to re-leave the room

    def done(self) -> bool:
        return all(len(p) == 2 and p[0] == p[1] for p in self.pos)

    def pprint(self):
        print("#" * 13)
        print(f"#{''.join(c or '.' for c in self.hallway)}#")
        print(f"###{'#'.join(p[1] if len(p) == 2 else '.' for p in self.pos)}###")
        print(f"  #{'#'.join(p[0] if p else '.' for p in self.pos)}#")
        print("  #########")

    @staticmethod
    def from_arrangement(a: Arrangement) -> Situation:
        return Situation(a.pos)

def distance(a1: Arrangement) -> int:
    frontier: list[tuple[int, Situation]] = [(0, Situation.from_arrangement(a1))]
    done = {}
    while frontier:
        dist, parent = hq.heappop(frontier)
        print(dist, "parent")
        parent.pprint()
        print()
        done[parent] = dist

        if parent.done():
            return dist

        for cost, new in parent.neighbors():
            if new in done:
                continue
            # print("child", dist + cost)
            # new.pprint()
            # print()
            hq.heappush(frontier, (dist + cost, new))

    print(done)
    return -1

arr = Arrangement.parse()
print(distance(arr))


from PIL import Image

from .quality_constants import ANTIALIASING
from .transformation import resize

def apply_texture(image, texture):
    texture = Image.open(texture)
    texture = resize(texture, (texture.size[0] * ANTIALIASING, texture.size[1] * ANTIALIASING))
    texture = texture.point(lambda p: p * 2)
    for x in range(0, image.size[0], texture.size[0]):
        for y in range(0, image.size[1], texture.size[1]):
            image.paste(texture, (x, y), mask=image.crop((x, y, x + texture.size[0], y + texture.size[1])))


"""Day 1"""

from aoc.shared import read_chunked_ints

values = read_chunked_ints("year_2022/input_01.txt")

def part1() -> int:
    """Part 1."""

    max_elf = 0

    for elf in values:
        total = sum(elf)
        if total > max_elf:
            max_elf = total

    return max_elf

def part2() -> int:
    """Part 2."""

    max_elves = [0, 0, 0]

    for elf in values:
        total = sum(elf)
        if total > max_elves[-1]:
            max_elves = max_elves[:2] + [total]
            max_elves = sorted(max_elves, reverse=True)

    return sum(max_elves)

if __name__ == "__main__":
    print("Part 1:", part1())
    print("Part 2:", part2())


from Leetcode_Problems_Python.Solver_Interface import Solver
from typing import List

class BinarySearch_Sovler(Solver):

    def solve(self,  nums: List[int], target: int) -> int:

        # M: divide search space in half each iteration:
        i_s = 0
        i_e = len(nums)-1

        while i_s <= i_e:

            i_m = (i_e-i_s) // 2 + i_s  # M: Go into middle and divide space to left/right each iteration

            if nums[i_m] == target:
                return i_m
            if nums[i_m] > target:
                i_e = i_m -1
            if nums[i_m] <= target:
                i_s = i_m +1

        return -1

    def test_solve(self):
        nums = [-1,0,3,5,9,12]
        target = 9

        idx = self.solve(nums, target)
        print("Item was at idx: ", idx)


import time
import re
import sys
import trc_util

def error(msg):
    print(msg, file=sys.stderr)
    sys.exit(1)

class Output:
    """
    Helper class for writing header and source files.  Tracks indentation
    level to properly format the output text.
    """

    HEADER = 0
    SOURCE = 1

    def __init__(self, mode, file=None, indent='\t'):
        """
        Create a new output instance.

          mode can be either HEADER or SOURCE.
          file is an open output file (or None to use stdout)
          indent is the string used for each indentation level
        """
        assert mode in (Output.HEADER, Output.SOURCE)
        self.__mode = mode
        if file is None:
            file = sys.stdout
        self.__file = file
        self.__indent = indent
        self.__indentation = ''
        self.__namespaces = []

    def indent(self):
        """Increase the indentation level"""
        self.__indentation += self.__indent

    def dedent(self):
        """Decrease the indentation level"""
        self.__indentation = self.__indentation[:-len(self.__indent)]

    def __call__(self, object):
        """Write an object or text to the output file"""
        if isinstance(object, str):
            if object != '__IGNORE__':
                print('%s%s' % (self.__indentation, object), file=self.__file)
        elif isinstance(object, tuple):
            print('%s%s' % (self.__indentation, str(object)), file=self.__file)
        else:
            if self.__mode == Output.HEADER:
                object.writeHeader(self)
            else:
                object.writeSource(self)

    def newline(self):
        """Write a blank line to the output file"""
        print('', file=self.__file)

    def Header(file=None):
        """Static factory for creating an Output instance for header files."""
        return Output(Output.HEADER, file)
    Header = staticmethod(Header)

    def Source(file=None):
        """Static factory for creating an Output instance for source files."""
        return Output(Output.SOURCE, file)
    Source = staticmethod(Source)

class Toplevel:
    """
    An abstract base class representing a declaration that can occur outside
    of Message definitions.
    """

    def writeHeader(output):
        raise NotImplementedError

    def writeSource(output):
        raise NotImplementedError

class Include(Toplevel):
    """A class representing an #include statement"""

    def __init__(self, path):
        self.path = path

    def writeHeader(self, output):
        output('#include %s' % self.path)

    def writeSource(self, output):
        pass

class Header(Toplevel):
    """A class representing a #header statement"""

    def __init__(self, include):
        self.include = include
        self.header = '__%s__' % re.sub(r'[^a-zA-Z0-9_]', '_', include[1:-1]).upper()
        self.timestamp = time.ctime()

    def writeBanner(self, output):
        output('// Generated on %s' % self.timestamp)
        output('// This is a generated file --- DO NOT EDIT!')

    def writeHeader(self, output):
        self.writeBanner(output)
        output.newline()
        output('#ifndef %s' % self.header)
        output('#define %s' % self.header)
        output.newline()

    def writeSource(self, output):
        self.writeBanner(output)
        output.newline()
        output('#include %s' % self.include)
        output('#include <csp/csplib/net/TaggedRecordRegistry.h>')
        output.newline()

class Using(Toplevel):
    """
    A class representing a using statement, for importing qualified names
    into the current namespace.
    """

    def __init__(self, name):
        self.name = name

    def writeHeader(self, output):
        output.newline()
        output('using %s;' % self.name)

    def writeSource(self, output):
        pass

class Footer(Toplevel):
    """A class representing the end of the input file."""

    def writeHeader(self, output):
        output.newline()
        output('#endif')

    def writeSource(self, output):
        pass

class Namespace(Toplevel):
    """
    A class representing a namespace declaration.  Somewhat confusingly,
    a namespace can (and generally does) contain other toplevel instances.
    """

    def __init__(self, id, contents):
        self.id = id
        self.contents = contents

    def writeContents(self, output, callback):
        output.newline()
        output('namespace %s {' % self.id)
        output.indent()
        for item in self.contents:
            callback(item)
        output.dedent()
        output.newline()
        output('}  // namespace %s' % self.id)
        output.newline()

    def writeHeader(self, output):
        self.writeContents(output, lambda x: x.writeHeader(output))

    def writeSource(self, output):
        self.writeContents(output, lambda x: x.writeSource(output))

class Element:
    """
    A base class representing any element that can be found inside a message
    definition.
    """

    # Element subclasses may override this value if they require generated
    # variable names.  For example, a bitset declaration is anonymous (ie,
    # no id is specified in the input file), so the BitSet class sets
    # ANONYMOUS = 'bitset' to automatically generate field names of the
    # form "bitsetN' where N = 0, 1, 2, etc.
    ANONYMOUS = 0

    def __init__(self, id):
        self.id = id
        self.options = {}
        self.anonymous_count = {}
        self.required = 0
        self.deprecated = 0
        self.default = None
        self.hasflag = None

    def getHasFlag(self):
        assert(self.hasflag is not None)
        return self.hasflag

    def setHasFlag(self, hasflag):
        self.hasflag = hasflag

    def isGroup(self):
        return 0

    def setOption(self, option, value):
        self.options[option] = value

    def getId(self, parent=None):
        if self.id:
            return self.id
        assert(parent is not None)
        assert(self.__class__.ANONYMOUS)
        anon = self.__class__.ANONYMOUS
        idx = parent.anonymous_count.get(anon, 0)
        parent.anonymous_count[anon] = idx + 1
        self.id = '%s%d' % (anon, idx)
        return self.id

    def getType(self):
        assert 0

    def signature(self):
        return ''

    def writeClear(self, output):
        pass

    def addToParent(self, parent):
        parent._addChild(self)

    def getName(self, parent=None):
        return 'm_%s' % self.getId()

    def writeGetter(self, output):
        output('inline %s const & %s() const { return %s; }' % (self.getType(), self.getId(), self.getName()))

    def writeSetter(self, output):
        if not self.deprecated:
            output('void set_%s(%s const &value) {' % (self.getId(), self.getType()))
            output.indent()
            output('%s = value;' % self.getName())
            output(self.getHasFlag().getSetString())
            output.dedent()
            output('}\n')

    def writeInit(self, output, tail=''):
        if self.default is None:
            output('%s()%s' % (self.getName(), tail))
        else:
            output('%s(%s)%s' % (self.getName(), self.default, tail))

    def writeSerializeWriter(self, output):
        if self.isGroup():
            output('%s->serialize(writer);' % self.getName())
        else:
            output('writer.writer << %s;' % self.getName())

    def writeSerializeReader(self, output):
        if self.isGroup():
            output('%s = new %s;' % (self.getName(), self.type.name))
            output('%s->serialize(reader);' % self.getName())
        else:
            output('reader.reader >> %s;' % self.getName())

    def write(self, output):
        output(str(self))

    def writeSource(self, output):
        pass

    def writeScopedSource(self, output, scope):
        pass

class Option(Element):
    """A class representing a message option specifier (e.g. @id=3;)"""

    def __init__(self, name, value):
        Element.__init__(self, None)
        self.name = name
        self.value = value

    def __str__(self):
        return '@%s = %s' % (self.name, self.value)

    def addToParent(self, parent):
        parent.setOption(self.name, self.value)

class Compound(Element):
    """A base class for elements that can contain other elements."""

    def __init__(self, id, children=None):
        Element.__init__(self, id)
        self.access = 'private'
        self.children = []
        self.localTypes = []
        if children:
            self.addChildren(children)

    def _addChild(self, child):
        self.children.append(child)

    def _addLocalType(self, localType):
        self.localTypes.append(localType)

    def addChild(self, child):
        child.addToParent(self)

    def addChildren(self, children):
        for child in children:
            child.addToParent(self)

    def signature(self):
        sig = []
        for child in self.children:
            sig.append(child.signature())
        return ';'.join(sig)

    def writeChildTags(self, output):
        output('enum {')
        output.indent()
        idx = 8
        for child in self.children[:-1]:
            output('TAG_%s = %d,' % (child.getId(self), idx))
            idx += 1
        output('TAG_%s = %d' % (self.children[-1].getId(self), idx))
        output.dedent()
        output('};\n')

    def writeChildHasFields(self, output):
        has_count = 0
        has_var = ''
        for child in self.children:
            idx = has_count % 32
            if (idx == 0):
                has_var = 'm_has%d' % (has_count / 32)
                output('csp::uint32 %s;' % has_var)
            child.setHasFlag(BitFlag(has_var, idx))
            has_count += 1
        output('')

    def writeChildFields(self, output):
        for child in self.children:
            output('%s %s;' % (child.getType(), child.getName()))

    def writeChildAccessors(self, output):
        for child in self.children:
            output('inline bool has_%s() const { return %s; }' % (child.getId(), child.getHasFlag().getTestString()))
        output.newline()
        for child in self.children:
            output('inline void clear_%s() {' % child.getId())
            output.indent()
            output(child.getHasFlag().getClearString())
            child.writeClear(output)
            output.dedent()
            output('}\n')
        for child in self.children:
            child.writeGetter(output)
        output.newline()
        for child in self.children:
            child.writeSetter(output)

    def writeAccessLabel(self, output, label):
        if label == self.access:
            return
        output.dedent()
        output('%s:\n' % label)
        output.indent()
        self.access = label

    def writeLocalTypes(self, output):
        for localType in self.localTypes:
            self.writeAccessLabel(output, 'public')
            localType.write(output)
            output.newline()

    def writeConstructor(self, output):
        if self.children:
            output('%s() :' % self.type.name)
            output.indent()
            for idx in range((len(self.children)+31)//32):
                output('m_has%d(0),' % idx)
            for child in self.children[:-1]:
                child.writeInit(output, ',')
            self.children[-1].writeInit(output)
            output.dedent()
            output('{ }\n')
        else:
            output('%s() { }\n' % self.type.name)

    def writeSerialize(self, output):
        output('void serialize(csp::TagWriter &writer) const;')
        output('void serialize(csp::TagReader &reader);')
        output.newline()

    def writeSerializeSource(self, output, scope=''):
        typename = scope + self.type.name
        output('void %s::serialize(csp::TagWriter &writer) const {' % typename)
        output.indent()
        output('writer.beginCompound();')
        has_required = 0
        has_optional = 0
        for child in self.children:
            if child.required:
                has_required = 1
                if has_optional:
                    print('REQUIRED elements cannot follow OPTIONAL element.')
                    print('%s::%s' % (typename, child.getId()))
                    sys.exit(1)
                output('assert(has_%s());' % child.getId())
                child.writeSerializeWriter(output)
            else:
                has_optional = 1
                output('if (has_%s()) {' % child.getId())
                output.indent()
                output('writer.writeTag(TAG_%s);' % child.getId())
                child.writeSerializeWriter(output)
                output.dedent()
                output('}')
        output('writer.endCompound();')
        output.dedent()
        output('}\n')
        output('void %s::serialize(csp::TagReader &reader) {' % typename)
        output.indent()
        output('reader.beginCompound();')
        if has_required:
            for child in self.children:
                if child.required:
                    child.writeSerializeReader(output)
                    output(child.getHasFlag().getSetString())
        has_tags = 0
        for child in self.children:
            if not child.required:
                has_tags = 1
                break
        if has_tags:
            output('for (int tag = reader.nextTag(); tag != 0; tag = reader.nextTag()) {')
            output.indent()
            output('switch (tag) {')
            output.indent()
            for child in self.children:
                if child.required:
                    continue
                output('case TAG_%s: {' % child.getId())
                output.indent()
                child.writeSerializeReader(output)
                output(child.getHasFlag().getSetString())
                output('break;')
                output.dedent()
                output('}')
            output('default: break;')
            output.dedent()
            output('}')
            output.dedent()
            output('}')
        output('reader.endCompound();')
        output.dedent()
        output('}\n')

    def writeDump(self, output, name=None):
        output('void dump(std::ostream &, int) const;')
        output('void dump(csp::DumpWriter &out, const char *name=0) const;')
        output.newline()

    def writeDumpSource(self, output, scope=''):
        typename = scope + self.type.name
        output('void %s::dump(std::ostream &, int) const {}' % typename)
        output('void %s::dump(csp::DumpWriter &out, const char *name) const {' % typename)
        output.indent()
        output('if (name) {')
        output.indent()
        output('out.line() << name << ": %s {";' % self.type.name)
        output.dedent()
        output('} else {')
        output.indent()
        output('out.line() << "%s {";' % self.type.name)
        output.dedent()
        output('}')
        output('out.indent();')
        for child in self.children:
            child.writeDump(output)
        output('out.dedent();')
        output('out.line() << "}";')
        output.dedent()
        output('}\n')

    def writeRef(self, output):
        output('typedef csp::Ref<%s> Ref;' % self.type.name)

    def writeBoilerPlate(self, output):
        pass

    def writeChildren(self, output):
        output('static int m_CustomId;')
        self.writeLocalTypes(output)
        if self.children:
            self.writeAccessLabel(output, 'private')
            self.writeChildTags(output)
            self.writeChildHasFields(output)
            self.writeChildFields(output)
            self.writeAccessLabel(output, 'public')
            self.writeChildAccessors(output)
        self.writeAccessLabel(output, 'public')
        self.writeConstructor(output)
        self.writeSerialize(output)
        self.writeAccessLabel(output, 'public')
        self.writeDump(output)
        self.writeRef(output)
        self.writeBoilerPlate(output)

class Field(Element):
    """A class representing a simple (ie not compound) field (e.g. int32 x;)."""

    def __init__(self, type, name, array=0, default=None):
        Element.__init__(self, name)
        self.type = type
        self.name = name
        self.array = array
        self.default = default

    def isGroup(self):
        return self.type.category in ('MESSAGE', 'GROUP')

    def writeClear(self, output):
        if self.array:
            output('%s.clear();' % self.getName())
        elif self.isGroup():
            output('%s = 0;' % self.getName())

    def __str__(self):
        array = ''
        if self.array:
            array = '[]'
        pre = ''
        if self.deprecated:
            pre = 'DEP '
        elif self.required:
            pre = 'REQ '
        default = ''
        if self.default is not None:
            default = ' = %s' % str(self.default)
        return '%s%s%s %s%s;' % (pre, self.type, array, self.name, default)

    def getType(self):
        type = self.type.name
        if self.isGroup():
            type = '%s::Ref' % type
        if self.array:
            return 'std::vector<%s>' % type
        return type

    def signature(self):
        sig = ''
        if self.array:
            sig += '@'
        if self.required:
            sig += '!'
        sig = '%s%s,%s' % (sig, self.type.name, self.name)
        return sig

    def writeSetter(self, output):
        if not self.deprecated:
            Element.writeSetter(self, output)
            if self.array:
                output('%s &set_%s() {' % (self.getType(), self.getId()))
                output.indent()
                output(self.getHasFlag().getSetString())
                output('return %s;' % self.getName())
                output.dedent()
                output('}\n')

    def writeSerializeWriter(self, output):
        if self.array:
            output('writer.writeLength(%s.size());' % self.getName())
            if self.isGroup():
                output('for (unsigned i=0; i < %s.size(); ++i) {' % self.getName())
                output.indent()
                output('%s[i]->serialize(writer);' % self.getName())
                output.dedent()
                output('}')
            else:
                output('for (unsigned i=0; i < %s.size(); ++i) {' % self.getName())
                output.indent()
                output('writer.writer << %s[i];' % self.getName())
                output.dedent()
                output('}')
        else:
            Element.writeSerializeWriter(self, output)

    def writeSerializeReader(self, output):
        if self.array:
            output('unsigned len = reader.readLength();')
            output('%s.resize(len);' % self.getName())
            if self.isGroup():
                output('for (unsigned i=0; i < len; ++i) {')
                output.indent()
                output('%s[i] = new %s;' % (self.getName(), self.type.name))
                output('%s[i]->serialize(reader);' % (self.getName()))
                output.dedent()
                output('}')
            else:
                output('for (unsigned i=0; i < len; ++i) {')
                output.indent()
                output('reader.reader >> %s[i];' % (self.getName()))
                output.dedent()
                output('}')
        else:
            Element.writeSerializeReader(self, output)

    def writeDump(self, output):
        output('if (has_%s()) {' % self.getId())
        output.indent()
        if self.array:
            output('out.line() << "%s: [";' % self.getId())
            output('out.indent();')
            if self.isGroup():
                output('for (unsigned i=0; i < %s.size(); ++i) {' % self.getName())
                output.indent()
                output('out.prefix() << i << ": ";')
                output('%s[i]->dump(out);' % self.getName())
                output.dedent()
                output('}')
            else:
                output('for (unsigned i=0; i < %s.size(); ++i) {' % self.getName())
                output.indent()
                output('out.prefix() << i << ": ";')
                output('out.line() << %s[i];' % self.getName())
                output.dedent()
                output('}')
            output('out.dedent();')
            output('out.line() << "]";')
        else:
            if self.isGroup():
                output('%s->dump(out, "%s");' % (self.getName(), self.getId()))
            else:
                output('out.line() << "%s: " << %s;' % (self.getId(), self.getName()))
        output.dedent()
        output('}')

class Group(Compound):
    """
    A class representing a group definition.  Groups are csp::Group subclasses
    defined inside of messages or other groups, and local to the enclosing scope.
    """

    def __init__(self, name, body, state=None):
        Compound.__init__(self, name, body)
        if state is not None:
            state.types[name] = 'GROUP'
        self.type = Type('GROUP', name)
        self.name = name
        self.body = body
        self.deprecated = 0

    def addToParent(self, parent):
        parent._addLocalType(self)

    def write(self, output):
        output('class %s : public csp::TaggedGroup {' % (self.type.name))
        output.indent()
        self.writeChildren(output)
        output.dedent()
        output('};')

    def writeScopedSource(self, output, scope=''):
        self.writeSerializeSource(output, scope)
        self.writeDumpSource(output, scope)
        scope += self.type.name + '::' + scope
        for element in self.localTypes:
            element.writeScopedSource(output, scope)

    def writeClear(self, output):
        output('%s = 0;' % self.getName())

    def __str__(self):
        dep = ''
        if self.deprecated:
            dep = 'DEP '
        return '%sgroup %s {\n%s\n}' % (dep, self.name, '\n'.join(map(str, self.body)))

    def getType(self):
        return 'csp::Ref<%s>' % self.type.name

    def signature(self):
        return '%s%%%s' % (self.type.name, Compound.signature(self))

class Bit(Element):
    """
    A class representing a single boolean field in a BitSet.  Bits cannot be
    declared outside of BitSets.
    """

    def __init__(self, mask, name, default=None, deprecated=0):
        Element.__init__(self, name)
        if default is not None:
            default = bool(int(default))
        self.mask = mask
        self.name = name
        self.default = default
        self.deprecated = deprecated

    def writeGetter(self, output, bitset):
        name = bitset.getName()
        output('inline bool %s() const { return (%s & 0x%02x) != 0; }' % (self.getId(), name, self.mask))

    def writeSetter(self, output, bitset):
        name = bitset.getName()
        output('void set_%s(bool value) {' % self.getId())
        output.indent()
        output('%s = (value ? (%s | 0x%02x) : (%s & ~0x%02x));' % (name, name, self.mask, name, self.mask))
        output(bitset.getHasFlag().getSetString())
        output.dedent()
        output('}\n')

    def writeDump(self, output):
        output('out.line() << "%s: " << %s();' % (self.getId(), self.getId()))

    def __str__(self):
        dep = ''
        if self.deprecated:
            dep = 'DEP '
        default = ''
        if self.default is not None:
            default = ' = %s' % str(self.default)
        return '%sbit %s%s' % (dep, self.name, default)

class Bitset(Compound):
    """
    An anonymous, compound element used to group boolean flags into a single field.
    A BitSet can only contain boolean fields.
    """

    # automatically generate field names of the form "bitsetN" for N = 0, 1, 2, etc.
    ANONYMOUS = 'bitset'

    def __init__(self, body):
        Compound.__init__(self, None, body)
        if len(body) > 32:
            error('too many boolean fields in bitset (limit 32)')
        bits = []
        default = 0
        bitshift = 1
        for field in body:
            if field.type.category != 'BOOL_TYPE':
                error('BitSet contains a non-boolean field')
            bit = Bit(bitshift, field.name, field.default, field.deprecated)
            bits.append(bit)
            if bit.default:
                default = default | bitshift
            bitshift = bitshift << 1
        self.bits = bits
        self.default = default
        self.deprecated = 0

    def writeGetter(self, output):
        for bit in self.bits:
            bit.writeGetter(output, self)

    def writeSetter(self, output):
        if not self.deprecated:
            for bit in self.bits:
                bit.writeSetter(output, self)

    def writeDump(self, output):
        for bit in self.bits:
            bit.writeDump(output)

    def __str__(self):
        dep = ''
        if self.deprecated:
            dep = 'DEP '
        return '%sbitset = %d {\n%s\n}' % (dep, self.default, '\n'.join(map(str, self.bits)))

    def getType(self):
        if len(self.children) <= 8:
            return 'csp::uint8'
        if len(self.children) <= 16:
            return 'csp::uint16'
        assert(len(self.children) <= 32)
        return 'csp::uint32'

    def signature(self):
        return 'bits'

class Message(Toplevel, Compound):
    """
    A class representing a message definition that generates a subclass of
    csp::TaggedRecord.
    """

    def __init__(self, name, body, base=None, state=None):
        Compound.__init__(self, name, body)
        assert(state is not None)
        # TODO symbol lookup does not respect namespace
        if name in state.types:
            error('%s multiply defined' % name)
        self.type = Type('MESSAGE', name)
        self.name = name
        self.base = state.typedefs.get(base, base)
        state.types[name] = 'MESSAGE'
        state.messages[name] = self

    def writeBoilerPlate(self, output):
        Compound.writeBoilerPlate(self, output)
        version = int(self.options.get('@version', 0))
        id64 = trc_util.md5hash64('%s_%d' % (self.name, version))
        output('virtual Id getId() const { return _getId(); }')
        output('static const Id _Id = CSP_ULL(%d);' % id64)
        output('static inline Id _getId() { return _Id; }')
        output('virtual int getCustomId() const { return _getCustomId(); }')
        output('static inline int _getCustomId() { return m_CustomId; }')
        output('static inline void _setCustomId(int id) { m_CustomId = id; }')
        output('virtual int getVersion() const { return _getVersion(); }')
        output('static inline int _getVersion() { return %d; }' % version)
        output('virtual std::string getName() const { return _getName(); }')
        output('static inline std::string _getName() { return "%s"; }' % self.name)

    def writeHeader(self, output):
        output.newline()
        output.newline()
        if self.base:
            output('class %s : public %s {' % (self.name, self.base))
        else:
            output('class %s : public csp::TaggedRecord {' % (self.name))
        output.indent()
        self.writeChildren(output)
        output.dedent()
        output('};')

    def writeSource(self, output):
        id = int(self.options.get('@id', 0))
        output.newline()
        output('int %s::m_CustomId = %d;' % (self.name, id))
        # g++ allows preinitialized static const members to be redeclared outside
        # of the class, but msvc does not.  g++ is happy with either style.
        # output('const %s::Id %s::_Id;' % (self.name, self.name));
        output('namespace { csp::TaggedRecordFactory<%s> __%s_factory; }' % (self.name, self.name))
        output.newline()
        self.writeSerializeSource(output)
        self.writeDumpSource(output)
        scope = self.type.name + '::'
        for element in self.localTypes:
            element.writeScopedSource(output, scope)

    def __str__(self):
        if self.base:
            return 'message %s : %s {\n%s\n}' % (self.name, self.base, '\n'.join(map(str, self.body)))
        else:
            return 'message %s {\n%s\n}' % (self.name, '\n'.join(map(str, self.body)))

    def signature(self):
        siglist = ['%s%%%s' % (self.name, Compound.signature(self))]
        for type in self.localTypes:
            sig = type.signature()
            if sig:
                siglist.append(sig)
        return '&'.join(siglist)

class Enum(Element):
    """A class representing an enum declaration within a message or group."""

    def __init__(self, values):
        self.values = values
        index = 0
        for item in values:
            if item.value is None:
                item.value = index
            index = int(item.value) + 1

    def addToParent(self, parent):
        parent._addLocalType(self)

    def write(self, output):
        output('enum {')
        output.indent()
        for item in self.values[:-1]:
            output('%s = %d,' % (item.id, item.value))
        item = self.values[-1]
        output('%s = %d' % (item.id, item.value))
        output.dedent()
        output('};')
        output.newline()

# miscellaneous helper classes

class EnumValue:
    """A class representing a symbol in an enum declaration."""

    def __init__(self, id, value=None):
        self.id = id
        if value is not None:
            value = int(value)
        self.value = value

class Type:
    """A class representing a field type."""

    def __init__(self, category, name):
        self.category = category
        if name in ('int8', 'int16', 'int32', 'int64', 'uint8', 'uint16', 'uint32', 'uint64'):
            name = 'csp::%s' % name
        self.name = name

    def __str__(self):
        return '%s(%s)' % (self.name, self.category)

class BitFlag:
    """
    A helper class for generating code that indicates the presence or absence of
    a field in message or group.
    """

    def __init__(self, flagset, bit):
        self.flagset = flagset
        self.mask = (1 << bit)

    def getSetString(self):
        return '%s |= 0x%08x;' % (self.flagset, self.mask)

    def getClearString(self):
        return '%s &= ~0x%08x;' % (self.flagset, self.mask)

    def getTestString(self):
        return '(%s & 0x%08x) != 0' % (self.flagset, self.mask)


import tensorflow as tf
from tensorflow import keras

#################################################################################
#INPUT IS [224,224]
visible = tf.keras.layers.Input(shape=[224,224,1])
x=tf.keras.layers.Conv2D(64, kernel_size=4,padding='same',activation='relu',kernel_initializer='he_uniform',input_shape=[224,224,1])(visible)
x=tf.keras.layers.SpatialDropout2D(0.5)(x)
x=tf.keras.layers.MaxPool2D(pool_size=(2, 2),padding='same')(x)
x=tf.keras.layers.Conv2D(32,kernel_size=4,padding='same',activation='relu',kernel_initializer='he_uniform')(x)
x=tf.keras.layers.SpatialDropout2D(0.5)(x)
x=tf.keras.layers.MaxPool2D(pool_size=(2, 2),padding='same')(x)
x=tf.keras.layers.Conv2D(16,kernel_size=4,padding='same',activation='relu',kernel_initializer='he_uniform')(x)
x=tf.keras.layers.SpatialDropout2D(0.5)(x)
x=tf.keras.layers.MaxPool2D(pool_size=(2, 2),padding='same')(x)
x=tf.keras.layers.Conv2D(8, kernel_size=2,padding='same',activation='relu',kernel_initializer='he_uniform')(x)
x=tf.keras.layers.SpatialDropout2D(0.5)(x)
x=tf.keras.layers.MaxPool2D(pool_size=(2, 2),padding='same')(x)
x=tf.keras.layers.Conv2D(2, kernel_size=2,padding='same',activation='relu',kernel_initializer='he_uniform')(x)
x=tf.keras.layers.SpatialDropout2D(0.5)(x)
x=tf.keras.layers.MaxPool2D(pool_size=(2, 2),padding='same')(x)
x=tf.keras.layers.Flatten()(x)

model=tf.keras.Model(inputs=visible,outputs=x)

#model.summary()

##################################################################################
inp=tf.keras.Input(shape=(7,))

x=tf.keras.layers.Dense(14,kernel_initializer='he_uniform')(inp)
x=tf.keras.layers.BatchNormalization()(x)
x=tf.keras.layers.Activation("relu")(x)
x=tf.keras.layers.Dropout(0.5)(x)
x=tf.keras.layers.Dense(49,kernel_initializer='he_uniform')(x)
x=tf.keras.layers.BatchNormalization()(x)
x=tf.keras.layers.Activation("relu")(x)
x=tf.keras.layers.Dropout(0.5)(x)
x=tf.keras.layers.Dense(98,kernel_initializer='he_uniform')(x)
x=tf.keras.layers.BatchNormalization()(x)
x=tf.keras.layers.Activation("relu")(x)

feat_model=tf.keras.Model(inputs=inp,outputs=x)

###################################################################################
optimizer = tf.optimizers.Adam(learning_rate=1e-2,beta_1=0.99,beta_2=0.999,amsgrad=True)

###################################################################################
def create_combined(model_1, model_2):

  # combine the output of the two branches

  #combined = tf.keras.layers.concatenate([model_1.output, model_2.output])
  combined = tf.keras.layers.concatenate([model_1.output,model_2.output])

  # apply a FC layer and then a regression prediction on the
  # combined outputs
  #z_0 = tf.keras.layers.Dense(24, activation=keras.layers.LeakyReLU(alpha=0.1))(combined)
  z_0 = tf.keras.layers.Dense(24, activation=keras.layers.LeakyReLU(alpha=0.3),kernel_initializer='he_uniform')(combined)
  z_1 = tf.keras.layers.Dense(12, activation = keras.layers.LeakyReLU(alpha=0.3),kernel_initializer='he_uniform')(z_0)
  z_2 = tf.keras.layers.Dense(1, activation="sigmoid")(z_1)

  # our model will accept the inputs of the two branches and
  # then output a single value
  combined_model = tf.keras.Model(inputs=[model_1.input, model_2.input], outputs=z_2)

  #discriminator.trainable=False
  #gan_input = tf.keras.Input(shape=(100,))
  #x = generator(gan_input)
  #gan_output= discriminator(x)
  #gan= tf.keras.Model(inputs=gan_input, outputs=gan_output)
  combined_model.compile(loss='binary_crossentropy', optimizer='adam')
  #combined_model.compile(loss='binary_crossentropy', optimizer=adam_optimizer())
  return combined_model

combined_model = create_combined(model,feat_model)
#combined_model.summary()


from api import app,mysql
from api.models import *
from flask import jsonify

@app.route("/Quiz")
def CreateQuiz():
    nom_quiz = Quiz.getNom()
    questions = Question.getQuestions(1)

    quiz = {"questions" : questions }

    y = 0
    while y < len(quiz["questions"]):
       quiz["questions"][y]["choix"] = Choix.getChoix(questions[y]["id_question"])
       y = y+1

    return jsonify(quiz)


import os
import matplotlib.pyplot as plt
plt.rcParams['pdf.fonttype'] = 42
plt.rcParams['ps.fonttype'] = 42
plt.rcParams['font.family'] = 'serif'

from scipy.stats import truncnorm
import numpy as  np
# import ipdb
import json
import torch
from torch.nn import init
from torchvision.utils import save_image, make_grid
from types import SimpleNamespace
from tqdm import tqdm
import matplotlib.pyplot as plt
import re

class Stats:
    def __init__(self):
        self.items = []
    def add(self, x):
        self.items.append(x)
    def avg(self):
        return np.mean(self.items)

def extractMax(input):
    # get a list of all numbers separated by
    # lower case characters
    # \d+ is a regular expression which means
    # one or more digit
    # output will be like ['100','564','365']
    numbers = []
    for i in range(len(input)):
        temp = re.findall('\d+', input[i])
        if len(temp) > 0:
            for j in range(len(temp)):
                temp[j] = int(temp[j])
            numbers.append(max(temp))

    return max(numbers)

def save_args(args, fp):
    # Save config
    config_file = open(fp, "wt")
    config_file.write(str(args))
    config_file.flush()

def load_args(fp):
    return SimpleNamespace(**json.load(open(fp, 'r')))

def generate_n_samples(generator, generator_args, device, N):
    all_samples = []
    for start in tqdm(range(0, N, generator_args.num_gen_images), desc='generate_n_samples'):
        with torch.no_grad():
            fake = generator(torch.randn(generator_args.num_gen_images, generator_args.nz, 1, 1, device=device))
        all_samples.append(fake)
    return torch.cat(all_samples, 0)

def weights_init(model, method='N02'):
    for m in model.modules():
        classname = m.__class__.__name__
        if (isinstance(m, torch.nn.Conv2d)
            or isinstance(m, torch.nn.Linear)
            or isinstance(m, torch.nn.Embedding)):
            print(f'Initializing: {classname}')
            if method == 'ortho':
                init.orthogonal_(m.weight)
            elif method == 'N02':
                init.normal_(m.weight, 0, 0.02)
            elif method in ['glorot', 'xavier']:
                init.xavier_uniform_(m.weight)
            else:
                print('Init style not recognized...')
        elif classname.find('BatchNorm') != -1:
            m.weight.data.normal_(1.0, 0.02)
            m.bias.data.fill_(0)

def tonp(x):
    if not isinstance(x, np.ndarray):
        x = x.detach().cpu().numpy()
    return x

def mkdir(p):
    if not os.path.exists(p):
        os.makedirs(p)

def truncated_z_sample(batch_size, z_dim, truncation=0.5, seed=None):
    state = None if seed is None else np.random.RandomState(seed)
    values = truncnorm.rvs(-2, 2, size=(batch_size, z_dim), random_state=state)
    return truncation * values

def save_samples_grid(inputs, samples, save_path, imgSize, n=20, sample_size=5, nc=1):
    inputs = inputs.cpu().data.view(-1, sample_size, nc, imgSize, imgSize)[:n]
    reconstructions = samples.cpu().data.view(-1, 10, nc, imgSize, imgSize)[:n]

    grid_img = inputs.view(-1, nc, imgSize, imgSize)
    grid_input = make_grid(grid_img, padding=2, nrow=sample_size, pad_value=1)
    grid_img = reconstructions.view(-1, nc, imgSize, imgSize)
    grid_fake = make_grid(grid_img, padding=2, nrow=10, pad_value=1)
    save_image(grid_input, save_path + '_input.jpeg', normalize=True, nrow=1)
    save_image(grid_fake, save_path + '.jpeg', normalize=True)

def save_test_grid(inputs, samples, save_path, imgSize, n=10, sample_size=5, nc=1):

    inputs = inputs.cpu().data.view(-1, sample_size, nc, imgSize, imgSize)[:n]
    reconstructions = samples.cpu().data.view(-1, 10, nc, imgSize, imgSize)[:n]

    grid_img = inputs.view(-1,nc, imgSize, imgSize)
    grid_input = make_grid(grid_img, padding=2, nrow=sample_size, pad_value=1)
    grid_img = reconstructions.view(-1,nc, imgSize, imgSize)
    grid_fake = make_grid(grid_img, padding=2, nrow=10, pad_value=1)
    grids = make_grid([grid_input, grid_fake], padding=20, nrow=2, pad_value=1)
    save_image(grids, save_path, normalize=True, nrow=1)
    # save_image(grid_fake, save_path, normalize=True)

def save_sidebyside(inputs, samples, save_path, imgSize, n=10, nc=1):
    inputs = inputs.cpu().data.view(-1, 5, nc, imgSize, imgSize)[:n]
    reconstructions = samples.cpu().data.view(-1, 5, nc, imgSize, imgSize)[:n]
    images = inputs[:,0].view(-1, 1, nc, imgSize, imgSize)
    for i in range(reconstructions.size(1)-1):
        images = torch.cat((images, reconstructions[:,i].view(-1, 1, nc, imgSize, imgSize)), dim=1)
        images = torch.cat((images, inputs[:,i+1].view(-1, 1, nc, imgSize, imgSize)), dim=1)
    images = torch.cat((images, reconstructions[:,reconstructions.size(1)-1].view(-1, 1, nc, imgSize, imgSize)), dim=1).view(-1, nc, imgSize, imgSize)
    save_image(images, save_path, normalize=True, nrow=n)

def save_diversity(inputs, samples, save_path, imgSize, nc=1):
    inputs = inputs.cpu().data.view(-1, nc, imgSize, imgSize)        #real image
    reconstructions = samples.cpu().data.view(-1, 10, nc, imgSize, imgSize)
    images = inputs[0].view(1, nc, imgSize, imgSize)

    for i in range(reconstructions.size(0)-1):
        images = torch.cat((images, reconstructions[i].view(10, nc, imgSize, imgSize)), dim=0)
        images = torch.cat((images, inputs[i+1].view(1, nc, imgSize, imgSize)), dim=0)
    images = torch.cat((images, reconstructions[-1].view(10, nc, imgSize, imgSize)), dim=0)
    images = images.view(-1, nc, imgSize, imgSize)
    save_image(images, save_path, normalize=True, nrow=11)

def reconstruct_orig(x, drange):
    lo, hi = drange
    x = (x - lo) * (255 / (hi - lo))
    x = torch.clamp(x.round(), min=0, max=255)
    return x

def original_ISSA_reconstruct(x):
    x = (x + 1) / 2
    return x


from calculator.operators import to_operator, Operator
from calculator.tokenizer import tokenize

class Calculator:

    def __init__(self):
        self.variables: dict = {}

    def run(self):
        line = input().replace(' ', '')
        while line != '/exit':
            if line.startswith('/'):
                self.execute_command(line[1:])
            elif '=' in line:
                self.evaluate(line, self.assign_variable, 'assignment')
            elif line:
                self.evaluate(line, self.simplify_term, 'expression')
            line = input().replace(' ', '')
        print('Bye!')

    def simplify_term(self, line: str, print_it: bool = True) -> int:
        postfix = self.convert_to_postfix(line)
        result = self.calc(postfix)
        if print_it:
            print(result)
        return int(result)

    def assign_variable(self, line):
        variable, value = line.split('=')
        if not variable.isalpha():
            raise ValueError
        self.variables[variable] = self.simplify_term(value, print_it=False)

    def convert_to_postfix(self, line: str) -> str:
        postfix = ''
        operator_stack = []
        for token in tokenize(line):
            postfix += self.process_token(token, operator_stack)
        while operator_stack:
            postfix += f'{operator_stack.pop()} '
        return postfix.strip()

    def process_token(self, token: str, operator_stack: list) -> str:
        if token == '(':
            operator_stack.append(token)
            return ''
        if token.isalpha():
            return f'{self.variables[token]} '
        if to_operator(token):
            return self.process_operator(Operator(token), operator_stack)
        if token == ')':
            return self.process_closing_parenthesis(operator_stack)
        return f'{int(token)} '

    @staticmethod
    def evaluate(line: str, operation: callable, operation_text: str):
        try:
            operation(line)
        except (IndexError, ValueError):
            print('Invalid', operation_text)
        except KeyError:
            print('Unknown variable')

    @staticmethod
    def execute_command(command: str):
        if command == 'help':
            print('The program simplifies an arithmetic expression, that may contain variables')
        else:
            print('Unknown command')

    @staticmethod
    def process_operator(operator: Operator, operator_stack: list) -> str:
        contribution = ''
        while operator_stack and operator_stack[-1] != '(' and operator.precedence <= operator_stack[-1].precedence:
            contribution += f'{operator_stack.pop()} '
        operator_stack.append(operator)
        return contribution

    @staticmethod
    def process_closing_parenthesis(operator_stack: list) -> str:
        contribution = ''
        while operator_stack[-1] != '(':
            contribution += f'{operator_stack.pop()} '
        operator_stack.pop()
        return contribution

    def calc(self, postfix: str) -> int:
        operand_stack = []
        for token in postfix.split():
            if to_operator(token):
                operand_stack.append(self.calc_operation(token, operand_stack))
            else:
                operand_stack.append(int(token))
        return operand_stack.pop()

    @staticmethod
    def calc_operation(token: str, operand_stack: list[int]) -> int:
        y = operand_stack.pop()
        x = operand_stack.pop()
        return Operator(token).apply(x, y)

if __name__ == '__main__':
    Calculator().run()


# write a program to find factorial.
'''# change the value for a different result
num = 3
# To take input from the user
#num = int(input("Enter a number: "))
factorial = 1
# check if the number is negative, positive or zero
if num < 0:
   print("Sorry, factorial does not exist for negative numbers")
elif num == 0:
   print("The factorial of 0 is 1")
else:
   for i in range(1,num + 1):
       factorial = factorial*i
   print("The factorial of",num,"is",factorial)'''
'''def factorial():
    num=int(input("Enter number to find factorial"))
    fact=1
    for x in range(1,num+1):
            fact=fact*x
    print("The factorial is ",fact)
factorial()'''

def factorial():
    num=int(input("Enter the number"))
    fact=1
    a=num
    while a>0:
        fact=fact*a
        a=a-1
    return fact
a=factorial()
print("The factorial is ",a)


import torch.nn as nn
from collections import OrderedDict
import torch
import torch.nn.functional as F
import torchvision
import math

class VGGFeatureExtractor(nn.Module):
    def __init__(self,
                 feature_layer=34,
                 use_bn=False,
                 use_input_norm=True,
                 device=torch.device('cpu')):
        super(VGGFeatureExtractor, self).__init__()
        if use_bn:
            model = torchvision.models.vgg19_bn(pretrained=True)
        else:
            model = torchvision.models.vgg19(pretrained=True)
        self.use_input_norm = use_input_norm
        if self.use_input_norm:
            mean = torch.Tensor([0.485, 0.456, 0.406]).view(1, 3, 1, 1).to(device)
            # [0.485-1, 0.456-1, 0.406-1] if input in range [-1,1]
            std = torch.Tensor([0.229, 0.224, 0.225]).view(1, 3, 1, 1).to(device)
            # [0.229*2, 0.224*2, 0.225*2] if input in range [-1,1]
            self.register_buffer('mean', mean)
            self.register_buffer('std', std)
        self.features = nn.Sequential(*list(model.features.children())[:(feature_layer + 1)])
        # No need to BP to variable
        for k, v in self.features.named_parameters():
            v.requires_grad = False

    def forward(self, x):
        if self.use_input_norm:
            x = (x - self.mean) / self.std
        output = self.features(x)
        return output

def conv_layer(in_channels, out_channels, kernel_size, stride=1, dilation=1, groups=1):
    # 返回一个简单的卷积层，自带padding
    padding = int((kernel_size - 1) / 2) * dilation
    return nn.Conv2d(in_channels, out_channels, kernel_size, stride, padding=padding, bias=True, dilation=dilation,
                     groups=groups)

def norm(norm_type, nc):
    norm_type = norm_type.lower()
    if norm_type == 'batch':
        layer = nn.BatchNorm2d(nc, affine=True)
    elif norm_type == 'instance':
        layer = nn.InstanceNorm2d(nc, affine=False)
    else:
        raise NotImplementedError('normalization layer [{:s}] is not found'.format(norm_type))
    return layer

def pad(pad_type, padding):
    pad_type = pad_type.lower()
    if padding == 0:
        return None
    if pad_type == 'reflect':
        layer = nn.ReflectionPad2d(padding)
    elif pad_type == 'replicate':
        layer = nn.ReplicationPad2d(padding)
    else:
        raise NotImplementedError('padding layer [{:s}] is not implemented'.format(pad_type))
    return layer

def get_valid_padding(kernel_size, dilation):
    kernel_size = kernel_size + (kernel_size - 1) * (dilation - 1)
    padding = (kernel_size - 1) // 2
    return padding

def conv_block(in_nc, out_nc, kernel_size, stride=1, dilation=1, groups=1, bias=True, \
               pad_type='zero', norm_type=None, act_type='relu', mode='CNA'):
    '''
    Conv layer with padding, normalization, activation
    mode: CNA --> Conv -> Norm -> Act
        NAC --> Norm -> Act --> Conv (Identity Mappings in Deep Residual Networks, ECCV16)
    '''
    assert mode in ['CNA', 'NAC', 'CNAC'], 'Wrong conv mode [{:s}]'.format(mode)
    padding = get_valid_padding(kernel_size, dilation)
    p = pad(pad_type, padding) if pad_type and pad_type != 'zero' else None
    padding = padding if pad_type == 'zero' else 0

    c = nn.Conv2d(in_nc, out_nc, kernel_size=kernel_size, stride=stride, padding=padding, \
            dilation=dilation, bias=bias, groups=groups)
    a = activation(act_type) if act_type else None
    if 'CNA' in mode:
        n = norm(norm_type, out_nc) if norm_type else None
        return sequential(p, c, n, a)
    elif mode == 'NAC':
        if norm_type is None and act_type is not None:
            a = activation(act_type, inplace=False)
            # Important!
            # input----ReLU(inplace)----Conv--+----output
            #        |________________________|
            # inplace ReLU will modify the input, therefore wrong output
        n = norm(norm_type, in_nc) if norm_type else None
        return sequential(n, a, p, c)

def activation(act_type, inplace=True, neg_slope=0.05, n_prelu=1):
    '''
    返回一个激活函数，其中act_type = 'relu' or 'prelu' or 'lrelu'
    '''
    act_type = act_type.lower()
    if act_type == 'relu':
        layer = nn.ReLU(inplace)
    elif act_type == 'lrelu':
        layer = nn.LeakyReLU(neg_slope, inplace)
    elif act_type == 'prelu':
        layer = nn.PReLU(num_parameters=n_prelu, init=neg_slope)
    else:
        raise NotImplementedError('activation layer [{:s}] is not found'.format(act_type))
    return layer

class ShortcutBlock(nn.Module):
    def __init__(self, submodule):
        super(ShortcutBlock, self).__init__()
        self.sub = submodule

    def forward(self, x):
        #output = x + self.sub(x)
        return x, self.sub

    def __repr__(self):
        tmpstr = 'Identity + \n|'
        modstr = self.sub.__repr__().replace('\n', '\n|')
        tmpstr = tmpstr + modstr
        return tmpstr

def mean_channels(F):
    assert(F.dim() == 4)
    spatial_sum = F.sum(3, keepdim=True).sum(2, keepdim=True)
    return spatial_sum / (F.size(2) * F.size(3))

def stdv_channels(F):
    assert(F.dim() == 4)
    F_mean = mean_channels(F)
    F_variance = (F - F_mean).pow(2).sum(3, keepdim=True).sum(2, keepdim=True) / (F.size(2) * F.size(3))
    return F_variance.pow(0.5)

def sequential(*args):
    if len(args) == 1:
        if isinstance(args[0], OrderedDict):
            raise NotImplementedError('sequential does not support OrderedDict input.')
        return args[0]
    modules = []
    for module in args:
        if isinstance(module, nn.Sequential):
            for submodule in module.children():
                modules.append(submodule)
        elif isinstance(module, nn.Module):
            modules.append(module)
    return nn.Sequential(*modules)

class ESA(nn.Module):
    def __init__(self, n_feats, conv):
        super(ESA, self).__init__()
        f = n_feats // 4
        self.conv1 = conv(n_feats, f, kernel_size=1)
        self.conv_f = conv(f, f, kernel_size=1)
        self.conv_max = conv(f, f, kernel_size=3, padding=1)
        self.conv2 = conv(f, f, kernel_size=3, stride=2, padding=0)
        self.conv3 = conv(f, f, kernel_size=3, padding=1)
        self.conv3_ = conv(f, f, kernel_size=3, padding=1)
        self.conv4 = conv(f, n_feats, kernel_size=1)
        self.sigmoid = nn.Sigmoid()
        self.relu = nn.ReLU(inplace=True)

    def forward(self, x):
        c1_ = (self.conv1(x))
        c1 = self.conv2(c1_)
        v_max = F.max_pool2d(c1, kernel_size=7, stride=3)
        v_range = self.relu(self.conv_max(v_max))
        c3 = self.relu(self.conv3(v_range))
        c3 = self.conv3_(c3)
        c3 = F.interpolate(c3, (x.size(2), x.size(3)), mode='bilinear', align_corners=False)
        cf = self.conv_f(c1_)
        c4 = self.conv4(c3+cf)
        m = self.sigmoid(c4)

        return x * m

class RFDB(nn.Module):

    def __init__(self, in_channels, distillation_rate=0.25):
        super(RFDB, self).__init__()

        self.dc = self.distilled_channels = in_channels // 2
        self.rc = self.remaining_channels = in_channels

        # 左子树 输出通道为 dc
        self.c1_d = conv_layer(in_channels, self.dc, 1)
        self.c2_d = conv_layer(self.remaining_channels, self.dc, 1)
        self.c3_d = conv_layer(self.remaining_channels, self.dc, 1)

        # 右子树 输出通道为 rc
        self.c1_r = conv_layer(in_channels, self.rc, 3)
        self.c2_r = conv_layer(self.remaining_channels, self.rc, 3)
        self.c3_r = conv_layer(self.remaining_channels, self.rc, 3)

        # 右子树叶节点，输出通道为dc
        self.c4 = conv_layer(self.remaining_channels, self.dc, 3)

        # 将三个左子树和最后的右子树节点输出拼接后输出通道数为in_channels的特征图
        self.c5 = conv_layer(self.dc * 4, in_channels, 1)

        self.act = activation('lrelu', neg_slope=0.05)
        self.esa = ESA(in_channels, nn.Conv2d)

    def forward(self, input):

        distilled_c1 = self.act(self.c1_d(input))

        r_c1 = (self.c1_r(input))
        r_c1 = self.act(r_c1 + input)
        distilled_c2 = self.act(self.c2_d(r_c1))

        r_c2 = (self.c2_r(r_c1))
        r_c2 = self.act(r_c2 + r_c1)
        distilled_c3 = self.act(self.c3_d(r_c2))

        r_c3 = (self.c3_r(r_c2))
        r_c3 = self.act(r_c3 + r_c2)

        r_c4 = self.act(self.c4(r_c3))

        out = torch.cat([distilled_c1, distilled_c2, distilled_c3, r_c4], dim=1)
        out_fused = self.esa(self.c5(out))

        return out_fused

class ResNetBlock(nn.Module):
    '''
    ResNet Block, 3-3 style
    with extra residual scaling used in EDSR
    (Enhanced Deep Residual Networks for Single Image Super-Resolution, CVPRW 17)
    '''

    def __init__(self, in_nc, mid_nc, out_nc, kernel_size=3, stride=1, dilation=1, groups=1, \
            bias=True, pad_type='zero', norm_type=None, act_type='relu', mode='CNA', res_scale=1):
        super(ResNetBlock, self).__init__()
        conv0 = conv_block(in_nc, mid_nc, kernel_size, stride, dilation, groups, bias, pad_type, \
            norm_type, act_type, mode)
        if mode == 'CNA':
            act_type = None
        if mode == 'CNAC':  # Residual path: |-CNAC-|
            act_type = None
            norm_type = None
        conv1 = conv_block(mid_nc, out_nc, kernel_size, stride, dilation, groups, bias, pad_type, \
            norm_type, act_type, mode)
        # if in_nc != out_nc:
        #     self.project = conv_block(in_nc, out_nc, 1, stride, dilation, 1, bias, pad_type, \
        #         None, None)
        #     print('Need a projecter in ResNetBlock.')
        # else:
        #     self.project = lambda x:x
        self.res = sequential(conv0, conv1)
        self.res_scale = res_scale

    def forward(self, x):
        res = self.res(x).mul(self.res_scale)
        return x + res

def pixelshuffle_block(in_channels, out_channels, upscale_factor=2, kernel_size=3, stride=1):
    conv = conv_layer(in_channels, out_channels * (upscale_factor ** 2), kernel_size, stride)
    pixel_shuffle = nn.PixelShuffle(upscale_factor)
    return sequential(conv, pixel_shuffle)

def upconv_blcok(in_nc, out_nc, upscale_factor=2, kernel_size=3, stride=1, bias=True, \
                pad_type='zero', norm_type=None, act_type='relu', mode='nearest'):
    # Up conv
    # described in https://distill.pub/2016/deconv-checkerboard/
    upsample = nn.Upsample(scale_factor=upscale_factor, mode=mode)
    conv = conv_block(in_nc, out_nc, kernel_size, stride, bias=bias, \
                        pad_type=pad_type, norm_type=norm_type, act_type=act_type)
    return sequential(upsample, conv)

class CCALayer(nn.Module):
    def __init__(self, channel, reduction=16):
        super(CCALayer, self).__init__()

        self.contrast = stdv_channels
        self.avg_pool = nn.AdaptiveAvgPool2d(1)
        self.conv_du = nn.Sequential(
            nn.Conv2d(channel, channel // reduction, 1, padding=0, bias=True),
            nn.ReLU(inplace=True),
            nn.Conv2d(channel // reduction, channel, 1, padding=0, bias=True),
            nn.Sigmoid()
        )

    def forward(self, x):
        y = self.contrast(x) + self.avg_pool(x)
        y = self.conv_du(y)
        return x * y

class IMDModule(nn.Module):
    def __init__(self, in_channels, distillation_rate=0.25):
        super(IMDModule, self).__init__()
        self.distilled_channels = int(in_channels * distillation_rate)
        self.remaining_channels = int(in_channels - self.distilled_channels)
        self.c1 = conv_layer(in_channels, in_channels, 3)
        self.c2 = conv_layer(self.remaining_channels, in_channels, 3)
        self.c3 = conv_layer(self.remaining_channels, in_channels, 3)
        self.c4 = conv_layer(self.remaining_channels, self.distilled_channels, 3)
        self.act = activation('lrelu', neg_slope=0.05)
        self.c5 = conv_layer(in_channels, in_channels, 1)
        self.cca = CCALayer(self.distilled_channels * 4)

    def forward(self, input):
        out_c1 = self.act(self.c1(input))
        distilled_c1, remaining_c1 = torch.split(out_c1, (self.distilled_channels, self.remaining_channels), dim=1)
        out_c2 = self.act(self.c2(remaining_c1))
        distilled_c2, remaining_c2 = torch.split(out_c2, (self.distilled_channels, self.remaining_channels), dim=1)
        out_c3 = self.act(self.c3(remaining_c2))
        distilled_c3, remaining_c3 = torch.split(out_c3, (self.distilled_channels, self.remaining_channels), dim=1)
        out_c4 = self.c4(remaining_c3)
        out = torch.cat([distilled_c1, distilled_c2, distilled_c3, out_c4], dim=1)
        out_fused = self.c5(self.cca(out)) + input
        return out_fused

class ResidualDenseBlock_5C(nn.Module):
    '''
    Residual Dense Block
    style: 5 convs
    The core module of paper: (Residual Dense Network for Image Super-Resolution, CVPR 18)
    '''

    def __init__(self, nc, kernel_size=3, gc=32, stride=1, bias=True, pad_type='zero', \
            norm_type=None, act_type='leakyrelu', mode='CNA'):
        super(ResidualDenseBlock_5C, self).__init__()
        # gc: growth channel, i.e. intermediate channels
        self.conv1 = conv_block(nc, gc, kernel_size, stride, bias=bias, pad_type=pad_type, \
            norm_type=norm_type, act_type=act_type, mode=mode)
        self.conv2 = conv_block(nc+gc, gc, kernel_size, stride, bias=bias, pad_type=pad_type, \
            norm_type=norm_type, act_type=act_type, mode=mode)
        self.conv3 = conv_block(nc+2*gc, gc, kernel_size, stride, bias=bias, pad_type=pad_type, \
            norm_type=norm_type, act_type=act_type, mode=mode)
        self.conv4 = conv_block(nc+3*gc, gc, kernel_size, stride, bias=bias, pad_type=pad_type, \
            norm_type=norm_type, act_type=act_type, mode=mode)
        if mode == 'CNA':
            last_act = None
        else:
            last_act = act_type
        self.conv5 = conv_block(nc+4*gc, nc, 3, stride, bias=bias, pad_type=pad_type, \
            norm_type=norm_type, act_type=last_act, mode=mode)

    def forward(self, x):
        x1 = self.conv1(x)
        x2 = self.conv2(torch.cat((x, x1), 1))
        x3 = self.conv3(torch.cat((x, x1, x2), 1))
        x4 = self.conv4(torch.cat((x, x1, x2, x3), 1))
        x5 = self.conv5(torch.cat((x, x1, x2, x3, x4), 1))
        return x5.mul(0.2) + x

class RRDB(nn.Module):
    '''
    Residual in Residual Dense Block
    (ESRGAN: Enhanced Super-Resolution Generative Adversarial Networks)
    '''

    def __init__(self, nc, kernel_size=3, gc=32, stride=1, bias=True, pad_type='zero', \
            norm_type=None, act_type='leakyrelu', mode='CNA'):
        super(RRDB, self).__init__()
        self.RDB1 = ResidualDenseBlock_5C(nc, kernel_size, gc, stride, bias, pad_type, \
            norm_type, act_type, mode)
        self.RDB2 = ResidualDenseBlock_5C(nc, kernel_size, gc, stride, bias, pad_type, \
            norm_type, act_type, mode)
        self.RDB3 = ResidualDenseBlock_5C(nc, kernel_size, gc, stride, bias, pad_type, \
            norm_type, act_type, mode)

    def forward(self, x):
        out = self.RDB1(x)
        out = self.RDB2(out)
        out = self.RDB3(out)
        return out.mul(0.2) + x

# 3 Han
def default_conv(in_channels, out_channels, kernel_size, bias=True):
    return nn.Conv2d(
        in_channels, out_channels, kernel_size,
        padding=(kernel_size//2), bias=bias)

class MeanShift(nn.Conv2d):
    def __init__(
        self, rgb_range,
        rgb_mean=(0.4488, 0.4371, 0.4040), rgb_std=(1.0, 1.0, 1.0), sign=-1):

        super(MeanShift, self).__init__(3, 3, kernel_size=1)
        std = torch.Tensor(rgb_std)
        self.weight.data = torch.eye(3).view(3, 3, 1, 1) / std.view(3, 1, 1, 1)
        self.bias.data = sign * rgb_range * torch.Tensor(rgb_mean) / std
        for p in self.parameters():
            p.requires_grad = False

class Upsampler(nn.Sequential):
    def __init__(self, conv, scale, n_feats, bn=False, act=False, bias=True):

        m = []
        if (scale & (scale - 1)) == 0:    # Is scale = 2^n?
            for _ in range(int(math.log(scale, 2))):
                m.append(conv(n_feats, 4 * n_feats, 3, bias))
                m.append(nn.PixelShuffle(2))
                if bn:
                    m.append(nn.BatchNorm2d(n_feats))
                if act == 'relu':
                    m.append(nn.ReLU(True))
                elif act == 'prelu':
                    m.append(nn.PReLU(n_feats))

        elif scale == 3:
            m.append(conv(n_feats, 9 * n_feats, 3, bias))
            m.append(nn.PixelShuffle(3))
            if bn:
                m.append(nn.BatchNorm2d(n_feats))
            if act == 'relu':
                m.append(nn.ReLU(True))
            elif act == 'prelu':
                m.append(nn.PReLU(n_feats))
        else:
            raise NotImplementedError

        super(Upsampler, self).__init__(*m)


import pandas as pd
import numpy as np
import os,time,sys
from IPython import embed
import matplotlib.pyplot as plt
import torch
from mpl_toolkits.mplot3d import Axes3D
import copy
import glob
import tqdm
#import textract
import xml.etree.ElementTree as ET

def show_me_input_data(pd_data):
    del(pd_data['Temp'])
    assert isinstance(pd_data, pd.core.frame.DataFrame), "I would like pandas with column names"
    pd_target_data = pd.read_csv("/mfs/home/limengwei/friction_compensation/data/planning_simulator.csv",sep=' ',index_col=None)[:-1][pd_data.columns].astype(float)
    data = pd_data.values
    target_data = pd_target_data.values
    #embed()

    f1, ax1 = plt.subplots()
    f2, ax2 = plt.subplots()
    #Distribution:
    ax1.plot(data[::max(data.shape)//1000].T, c='r', linestyle='--', alpha=0.2)
    ax2.plot(target_data[::max(target_data.shape)//1000].T, c='b', linestyle='--', alpha=0.2)
    #Channel:
    ax1.plot(data.min(axis=0), 'k', label="train_min_max")
    ax1.plot(data.max(axis=0), 'k')
    ax2.plot(target_data.min(axis=0), c='k', label="simulator_min_max", alpha=0.8)
    ax2.plot(target_data.max(axis=0), c='k', alpha=0.8)
    #Misc:
    ax1.set_xticks(list(range(25-1)))   #Temp is del here.
    ax1.set_xticklabels(list(pd_data.columns), rotation=90, fontsize=5)
    ax2.set_xticks(list(range(25-1)))   #Temp is del here.
    ax2.set_xticklabels(list(pd_data.columns), rotation=90, fontsize=5)
    f1.legend()
    f2.legend()

    for idx,line in enumerate(ax1.lines):
        line.set_color(plt.cm.Spectral_r(np.linspace(0,1,100))[idx])
    lineobj = plt.plot(data.T[:,:])
    plt.legend(iter(lineobj), list(range(25)))
    return

#project friction compensation
def performance_shape(raw_plan, inputs, model_part1):
    ax1 = plt.subplot(221)
    ax2 = plt.subplot(222)
    ax3 = plt.subplot(212, projection='3d')
    #General:
    new = np.tile(inputs.detach().numpy().mean(axis=0), (1000,1))
    v_idx = 12
    p_idx = 13
    mannual_v = np.linspace(-inputs.detach().numpy().min(axis=0)[v_idx], inputs.detach().numpy().min(axis=0)[v_idx], 1000)
    mannual_p = np.linspace(-inputs.detach().numpy().min(axis=0)[p_idx], inputs.detach().numpy().min(axis=0)[p_idx], 1000)
    #F-v curve:
    new_v = copy.deepcopy(new)
    new_v[:,v_idx] = mannual_v
    output_v =  model_part1(torch.FloatTensor(new_v)).detach().numpy()
    ax1.plot(mannual_v, output_v)
    ax1.set_xlabel("Normalized speed")
    ax1.set_ylabel("Normalized compensation")
    ax1.set_title("v-F curve")
    #F-pos curve:
    new_p = copy.deepcopy(new)
    new_p[:,p_idx] = mannual_p
    output_p = model_part1(torch.FloatTensor(new_p)).detach().numpy()
    ax2.plot(mannual_p, output_p)
    ax2.set_xlabel("Normalized position")
    ax2.set_ylabel("Normalized compensation")
    ax2.set_title("p-F curve")
    #F-v-pos surface:
    new_vp = np.tile(new, (1000,1))
    new_vp[:,p_idx] = np.random.uniform(-inputs.detach().numpy().min(axis=0)[v_idx], inputs.detach().numpy().min(axis=0)[v_idx], 1000*1000)
    new_vp[:,v_idx] = np.random.uniform(-inputs.detach().numpy().min(axis=0)[p_idx], inputs.detach().numpy().min(axis=0)[p_idx], 1000*1000)
    output_vp = model_part1(torch.FloatTensor(new_vp)).detach().numpy()
    ax3.scatter3D(new_vp[:,v_idx][::100], new_vp[:,p_idx][::100], output_vp.flatten()[::100], s=0.5, alpha=0.5, c=output_vp.flatten()[::100], cmap='Spectral_r')
    ax3.set_xlabel("Normalized speed")
    ax3.set_ylabel("Normalized position")
    ax3.set_zlabel("Normalized compensation")
    ax3.set_title("v-p-F surface")
    plt.show()

    sys.exit()
    return

#Project car_face
def get(root, name):
    vars = root.findall(name)
    return vars
def get_and_check(root, name, length):
    vars = root.findall(name)
    if len(vars) == 0:
        raise NotImplementedError('Can not find %s in %s.'%(name, root.tag))
    if length > 0 and len(vars) != length:
        raise NotImplementedError('The size of %s is supposed to be %d, but is %d.'%(name, length, len(vars)))
    if length == 1:
        vars = vars[0]
    return vars
def show_bbox_sizes():
    BACK_xmls = glob.glob('./*/*/BACK*.xml')
    FRONT_xmls = list(set(glob.glob('./*/*/*.xml'))-set(BACK_xmls))
    #def convert(xml_dir, json_file, reassign_list, label_to_id):
    list_fp = FRONT_xmls
    list_fp = BACK_xmls
    json_dict = {"images":[], "type": "instances", "annotations": [],
                 "categories": []}
    heights = []
    widths = []
    categories = {}
    label_to_id = {"angle":1, "top":2, "head":3}
    bnd_id = 0
    for idx,line in tqdm.tqdm(enumerate(list_fp), total=len(list_fp)):
        line = line.strip()
        xml_f = line
        tree = ET.parse(xml_f)
        root = tree.getroot()
        path = get(root, 'path')
        if len(path) == 1:
            filename = os.path.basename(path[0].text)
        elif len(path) == 0:
            filename = get_and_check(root, 'filename', 1).text
        else:
            raise NotImplementedError('%d paths found in %s'%(len(path), line))
        if len(filename)>60:
            print("%s is too long, Might from windows? using %s instead"%(filename, filename.split('\\')[-1]), "you might want to check output.xml")
        filename = filename.split('\\')[-1]
        ## The filename must be a number
        #image_id = get_filename_as_int(filename)
        image_id = idx
        size = get_and_check(root, 'size', 1)
        width = int(get_and_check(size, 'width', 1).text)
        height = int(get_and_check(size, 'height', 1).text)
        image = {'file_name': filename, 'height': height, 'width': width,
                 'id':image_id}
        json_dict['images'].append(image)
        ## Cruuently we do not support segmentation
        #  segmented = get_and_check(root, 'segmented', 1).text
        #  aissert segmented == '0'
        for obj in get(root, 'object'):
            category = get_and_check(obj, 'name', 1).text
            if category not in categories:
                new_id = len(categories)
                categories[category] = new_id
                categories[category] = label_to_id[category]
                #print("Giving %s %s"%(category, label_to_id[category]))
            category_id = categories[category]
            bndbox = get_and_check(obj, 'bndbox', 1)
            xmin = int(get_and_check(bndbox, 'xmin', 1).text) - 1
            ymin = int(get_and_check(bndbox, 'ymin', 1).text) - 1
            xmax = int(get_and_check(bndbox, 'xmax', 1).text)
            ymax = int(get_and_check(bndbox, 'ymax', 1).text)
            assert(xmax > xmin)
            assert(ymax > ymin)
            o_width = abs(xmax - xmin)
            o_height = abs(ymax - ymin)
            widths.append(o_width)
            heights.append(o_height)
            #embed()
            ann = {'area': o_width*o_height, 'o_width':o_width, 'o_height':o_height, 'iscrowd': 0, 'image_id':
                   image_id, 'bbox':[xmin, ymin, o_width, o_height],
                   'category_id': category_id, 'id': bnd_id, 'ignore': 0,
                   'segmentation': []}
            json_dict['annotations'].append(ann)
            bnd_id = bnd_id + 1
    #For my statistics:
    areas = []
    o_widths = []
    o_heights = []
    for i in json_dict['annotations']:
        if i['category_id']==3:
            areas.append(i['area'])
            o_widths.append(i['o_width'])
            o_heights.append(i['o_height'])

    embed()

    return

if __name__ == "__main__":
    print("NN input Analyzer")
    print("Call me in your scripts...")
    show_bbox_sizes()


#Exercício 6- Leia uma string e um número N. Imprima as N primeiras letras do nome digitado. Caso N seja maior do que o tamanho do nome imprima uma mensagem de erro e encerre o programa sem fazer nada

nome = input('Digite um nome: ')
N = int(input('Digite a qtd de letras: '))
tam = len(nome)

if N > tam:
    print("O nome não possui tantas letras assim...")
else:
    for i in range(N):
     print(nome[i])


from utils.simple_logger import Log
from transformers import AutoModelForCausalLM, LlamaTokenizer, LlamaForCausalLM, BitsAndBytesConfig, AutoTokenizer, AutoModel
import torch

from langchain.embeddings import LlamaCppEmbeddings
from langchain.llms import LlamaCpp
from langchain import PromptTemplate, LLMChain
from langchain.callbacks.manager import CallbackManager
from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler
from langchain.llms import GPT4All

from text2vec import SentenceModel

import sys
from pathlib import Path
sys.path.append(str(Path(__file__).parent.parent.absolute()))
log = Log.get_logger(__name__)

# model processor class

class ModelProcessor:

    @staticmethod
    def load_gpt4all(args):
        log.debug(f'Loading model from {args}')

        try:
            # Callbacks support token-wise streaming
            callback_manager = CallbackManager(
                [StreamingStdOutCallbackHandler()])
            # Verbose is required to pass to the callback manager

            # Make sure the model path is correct for your system!
            model = GPT4All(
                model=args.model_name_or_path, callback_manager=callback_manager, verbose=True
            )
        except Exception as e:
            log.error(
                f'Failed to load the model from {args.model_name_or_path}')
            log.error(f'Exception: {e}')
            raise e
        return model

    @staticmethod
    def load_llamacpp(args):
        log.debug(f'Loading model from {args}')

        try:
            # Callbacks support token-wise streaming
            callback_manager = CallbackManager(
                [StreamingStdOutCallbackHandler()])
            # Verbose is required to pass to the callback manager

            # Make sure the model path is correct for your system!
            model = LlamaCpp(
                model_path=args.model_name_or_path, callback_manager=callback_manager, verbose=True
            )
            embeddings = LlamaCppEmbeddings(model_path=args.model_name_or_path)
        except Exception as e:
            log.error(
                f'Failed to load the model from {args.model_name_or_path}')
            log.error(f'Exception: {e}')
            raise e
        return model, embeddings

    # load the model
    @staticmethod
    def load_model(args):
        log.debug(f'Loading model from {args}')
        if args is None or not hasattr(args, 'model_name_or_path') or args.model_name_or_path is None:
            log.error(f'Invalid arguments or no model name path provided')
            raise ValueError(
                'Invalid arguments or no model name path provided')
        else:
            # load the model from the path
            try:
                if args.load_in_8bit:
                    log.debug("Model Loading in 8 bits")
                    model = AutoModelForCausalLM.from_pretrained(args.model_name_or_path,
                                                                 device_map="auto",
                                                                 quantization_config=BitsAndBytesConfig(
                                                                     load_in_8bit=True, llm_int8_enable_fp32_cpu_offload=True),
                                                                 trust_remote_code=args.trust_remote_code)
                else:
                    log.debug("Default model loading in fp16 or bf16")
                    if args.chatglm:
                        model = AutoModel.from_pretrained(
                            args.model_name_or_path,
                            device_map="auto",
                            trust_remote_code=args.trust_remote_code,
                            torch_dtype=torch.bfloat16 if args.bf16 else torch.float16)
                    else:
                        model = AutoModelForCausalLM.from_pretrained(args.model_name_or_path,
                                                                 device_map="auto",
                                                                 torch_dtype=torch.bfloat16 if args.bf16 else torch.float16,
                                                                 trust_remote_code=args.trust_remote_code)

                if type(model) is LlamaForCausalLM:
                    log.debug(f'Loading LlamaTokenizer')
                    tokenizer = LlamaTokenizer.from_pretrained(
                        args.model_name_or_path,
                        trust_remote_code=args.trust_remote_code,
                        clean_up_tokenization_spaces=True)
                else:
                    log.debug(f'Loading AutoTokenizer')
                    tokenizer = AutoTokenizer.from_pretrained(
                        args.model_name_or_path,
                        trust_remote_code=args.trust_remote_code,
                        clean_up_tokenization_spaces=True)

            except Exception as e:
                log.error(
                    f'Failed to load the model from {args.model_name_or_path}')
                log.error(f'Exception: {e}')
                raise e
            return model, tokenizer


# расчет промежуточных а-векторов
def get_a_vectors(matrix, q, ar):

    res = []
    s = 0

    for i in range(1, len(q)):
        for j in range(0, len(matrix)):
            if q[i] == j:
                for k in range(0, len(ar[0])):
                    s = s + ar[i-1][k] * matrix[k][j]
                res.append(s)
                s = 0
            else:
                res.append(0)
        ar.append(res)
        res = []

    return ar


# Databricks notebook source
# MAGIC %md
# MAGIC ##### Welcome  to Azurelib Academy By Deepak Goyal
# MAGIC ##### You can master the Databricks here: www.adeus.azurelib.com
# MAGIC ##### Author of this blog: Arud Seka Berne S in

# COMMAND ----------

# MAGIC %md
# MAGIC #### Pyspark Explode() Function in Azure Databricks

# COMMAND ----------

# MAGIC %md
# MAGIC ###### Gentle reminder:
# MAGIC In Databricks,
# MAGIC   - sparkSession made available as spark
# MAGIC   - sparkContext made available as sc
# MAGIC
# MAGIC In case, you want to create it manually, use the below code.

# COMMAND ----------

from pyspark.sql.session import SparkSession

spark = SparkSession.builder \
    .master("local[*]") \
    .appName("azurelib.com") \
    .getOrCreate()

sc = spark.sparkContext

# COMMAND ----------

# MAGIC %md
# MAGIC ##### a) Create manual PySpark DataFrame

# COMMAND ----------

from pyspark.sql.types import StructType, StructField, ArrayType, StringType

data = [
    ("Anand",["Apple", "Banana"],[["Simrun", "Kuspu"], ["Ajith", "Suriya"]]),
    ("Berne",["Pine Apple", "Orange"],[["Trisha", "Samantha"], ["Vijay", "Suriya"]]),
    ("Charan",["Grapes", "Pome"],[["Nayanthara", "Jothika"], ["Simbu", "Vishal"]]),
    ("Denish",["Starwberry", "Blueberry"],[["Asin", "Katrina"], ["Rajini", "Aswin"]]),
    ("Eren",None,[["Sherina", "Emi"], ["Kamal", "Sivaji"]])
]

columns = ["name","foods","actors"]
df = spark.createDataFrame(data, schema=columns)
df.printSchema()
df.show(truncate=False)

# COMMAND ----------

# MAGIC %md
# MAGIC ##### b) Create PySpark DataFrame by reading files

# COMMAND ----------

file_path = "/mnt/practice/date_diff.csv"
# replace the file_path with the source file location which you have downloaded.

df_2 = spark.read.format("csv").option("header", True).load(file_path)
df_2.printSchema()

# COMMAND ----------

# MAGIC %md
# MAGIC ##### Note: Here, I will be using the manually created dataframe

# COMMAND ----------

# MAGIC %md
# MAGIC ##### 1. explode()

# COMMAND ----------

from pyspark.sql.functions import explode

df.select("name", explode("foods").alias("explode")).show()

# COMMAND ----------

# MAGIC %md
# MAGIC ##### 2. explode_outer()

# COMMAND ----------

from pyspark.sql.functions import explode_outer

df.select("name", explode_outer("foods").alias("explode_outer")).show()

# COMMAND ----------

# MAGIC %md
# MAGIC ##### 3. posexplode()

# COMMAND ----------

from pyspark.sql.functions import posexplode

df.select("name", posexplode("foods").alias("index", "posexplode")).show()

# COMMAND ----------

# MAGIC %md
# MAGIC ##### 4. posexplode_outer()

# COMMAND ----------

from pyspark.sql.functions import posexplode_outer

df.select("name", posexplode_outer("foods").alias("index", "posexplode_outer")).show()

# COMMAND ----------

# MAGIC %md
# MAGIC ##### 5. flatten()

# COMMAND ----------

from pyspark.sql.functions import flatten

df.select("actors", flatten("actors")).show(truncate=False)

43. Pyspark Explode() Function/Pyspark Explode() Function - Notebook.py