from math import cos, isclose, radians, sin, sqrt
from typing import Any, Dict, List, Tuple

from pydantic import BaseModel, root_validator

class C(BaseModel):
    """ Complex number as re + i*im """
    re: float # real part
    im: float # imaginary part

    @property
    def mag(self) -> float:
        """ Magnitude """
        return sqrt(self.re**2 + self.im**2)

    @property
    def prob(self) -> float:
        """ Probability of colapsing to this state (= mag**2). """
        return self.mag**2

    def __str__(self) -> str:
        if self.im == 0.0:
            return str(self.re)
        elif self.re == 0.0:
            return f'{self.im}i'
        else:
            return f'({self.re} + {self.im}i)'

    def __add__(self, x: 'C') -> 'C':
        if isinstance(x, C):
            return C(re=self.re+x.re, im=self.im+x.im)
        else:
            raise ValueError('huet addition: {x}')

    def __sub__(self, x: 'C') -> 'C':
        if isinstance(x, C):
            return C(re=self.re-x.re, im=self.im-x.im)
        else:
            raise ValueError('huet addition: {x}')

    def __mul__(self, x: [int, float, 'C']) -> 'C':
        if isinstance(x, C):
            return C(
                re=self.re*x.re - self.im*x.im,
                im=self.re*x.im + self.im*x.re,
            )
        else:
            return C(re=x*self.re, im=x*self.im)

    @property
    def conj(self) -> 'C':
        """ Get conjugate. """
        return C(re=self.re, im=self.im * -1)

class Q(BaseModel):
    """ Qubit as amp0*|0> + amp1*|1>  """
    amp0: C # Amplitude of 0
    amp1: C # Amplitude of 1

    def __str__(self) -> str:
        return f'{self.amp0}*|0> + {self.amp1}*|1>'

    @root_validator()
    def _has_prob_1(cls, values: Dict[str, Any]) -> Dict[str, Any]:
        """ Assert the probability of collapsing to 0 or 1 == 1. """
        prob = values['amp0'].prob + values['amp1'].prob
        if not isclose(prob, 1.0):
            raise ValueError(f'Probability is not 1: {values} {prob}')
        return values

    @property
    def vec(self) -> List[C]:
        """ Qbit to 2-dim vector. """
        return [self.amp0, self.amp1]

# Constants
MS2 = 1/sqrt(2) # Square root of 2 to the minus 1
C0 = C(re=0,im=0) # Complex 0
C1 = C(re=1,im=0) # Complex 1
Q0 = Q(amp0=C1.copy(), amp1=C0.copy()) # |0> aka [1, 0]T
Q1 = Q(amp0=C0.copy(), amp1=C1.copy()) # |1> aka [0, 1]T

def _assert_0_or_1(qubits: List[Q]) -> None:
    for q in qubits:
        if not (q == Q0 or q == Q1):
            raise ValueError(f'Qubit must be |0> or |1>: {q}')

def gen_eye(n: int) -> List[List[float]]:
    """ Make identity matrix. """
    eye = [[C0.copy() for _ in range(n)] for _ in range(n)]
    for i in range(n):
        eye[i][i] = C1.copy()
    return eye

def _round_qubit(q: Q) -> None:
    """ Bring values close to 0 or 1 to 0 or 1. """
    def _round(f: float) -> float:
        if isclose(f, 0, abs_tol=1e-9):
            return 0
        elif isclose(f, 1):
            return 1
        else:
            return f
    q.amp0.re = _round(q.amp0.re)
    q.amp0.im = _round(q.amp0.im)
    q.amp1.re = _round(q.amp1.re)
    q.amp1.im = _round(q.amp1.im)

def _apply_UT(q: Q, ut) -> None:
    """ Apply a 2x2 unitary transformation to qubit q. """
    #print(q)
    amp0 = q.amp0 * ut[0][0] + q.amp1 * ut[0][1]
    amp1 = q.amp0 * ut[1][0] + q.amp1 * ut[1][1]
    q.amp0 = amp0
    q.amp1 = amp1
    _round_qubit(q)
    #print(q)

class SuPos:
    """ Super position """
    def __init__(self, tensor: List[C]) -> None:
        self.reg = tuple(t for t in tensor)

def scale_mat(mat: 'Matrix', s: float) -> 'Matrix':
    """ Scale a matrix by a scalar <s>. """
    for row in mat:
        for j in range(len(row)):
            row[j] *= s
    return mat

def mat_dagger(mat: 'Matrix') -> 'Matrix':
    """ Get hermitian (conjugate) transpose of mat. """
    dagger = []
    for col_ix in range(len(mat[0])):
        new_row = [row[col_ix] for row in mat]
        dagger.append(new_row)
    return dagger

def join_mats(mats: List['Matrix'], axis: str) -> 'Matrix':
    ''' Join matrices horizontally or vertically. '''
    if axis == 'h':
        row_nums = set(len(mat) for mat in mats)
        if len(row_nums) > 1:
            raise ValueError('All matrices must have the same numnber of rows.')

        num_rows = list(row_nums)[0]
        joined = [[] for _ in range(num_rows)]
        for mat in mats:
            for row_ix in range(num_rows):
                joined[row_ix].extend(mat[row_ix])
        return joined
    elif axis == 'v':
        #V join is the same as: dagger --> H join --> dagger.
        daggered = join_mats([mat_dagger(mat) for mat in mats], 'h')
        return mat_dagger(daggered)
    else:
        raise ValueError('Unsupported axis: {axis}')

def tensor_mats(mats: List['Matrix']) -> 'Matrix':
    """ Make a tensor product of matrices. """

def tensor_vecs(vectors: List['Vector']) -> 'Vector':
    """ Make a tensor product of vectors. """
    if len(vectors) < 2:
        return vectors
    elif len(vectors) > 2:
        sub = tensor_vecs(vectors[1:])
        return tensor_vecs([vectors[0], sub])
    else:
        tensor = []
        for a0 in vectors[0]:
            for a1 in vectors[1]:
                tensor.append(a0 * a1)
        return tensor

def circuit(qs: Tuple[Q, Q, Q]) -> Tuple[Q, Q, Q]:
    q0, q1, q2 = qs
    g_HADAMARD(q2)
    g_CNOT(q1, q2)
    g_T_dgr(q2)
    g_CNOT(q0, q2)
    g_T(q2)
    g_CNOT(q1, q2)
    g_T_dgr(q2)
    g_CNOT(q0, q2)
    g_T_dgr(q1)
    g_T(q2)
    g_CNOT(q0, q1)
    g_HADAMARD(q2)
    g_T_dgr(q1)
    g_CNOT(q0, q1)
    g_T(q0)
    g_S(q1)

    return qs

# GATES
def g_NOT(q: Q) -> None:
    """ Reverse the amplitudes. """
    amp0 = q.amp0.copy()
    q.amp0 = q.amp1.copy()
    q.amp1 = amp0

def g_HADAMARD(q: Q) -> None:
    amp0 = (q.amp0 + q.amp1) * MS2
    amp1 = (q.amp0 - q.amp1) * MS2
    q.amp0 = amp0
    q.amp1 = amp1

def g_CNOT(control: Q, target: Q) -> None:
    """ Controlled NOT """
    #_assert_0_or_1([control, target])
    if control == Q1:
        g_NOT(target)

def g_CCNOT(cc: Q, c: Q, t: Q) -> None:
    """ Controlled CNOT """
    #_assert_0_or_1([cc, c, t])
    if cc == Q1:
        g_CNOT(c, t)

def g_SWAP(a: Q, b: Q) -> None:
    _assert_0_or_1([a, b])
    bak = a.copy()
    a.amp0 = b.amp0
    a.amp1 = b.amp1
    b.amp0 = bak.amp0
    b.amp1 = bak.amp1

def g_CSWAP(c: Q, a: Q, b: Q) -> None:
    _assert_0_or_1([c, a, b])
    if c == Q1:
        g_SWAP(a, b)

def g_ROT(q: Q, phi: float) -> None:
    """ Rotate by phi degrees counter-clockwise. """
    rad_phi = radians(phi)
    ut = [
        [cos(rad_phi), -sin(rad_phi)],
        [sin(rad_phi), cos(rad_phi)],
    ]
    _apply_UT(q, ut)

def g_S(q: Q) -> None:
    """ Multiply amp1 by i """
    ut = [
        [1, 0],
        [0, C(re=0, im=1)],
    ]
    _apply_UT(q, ut)

def g_T(q: Q) -> None:
    ut = [
        [1, 0],
        [0, C(re=MS2, im=MS2)],
    ]
    _apply_UT(q, ut)

def g_T_dgr(q: Q) -> None:
    """ T dagger gate """
    ut = [
        [1, 0],
        [0, C(re=MS2, im=-MS2)],
    ]
    _apply_UT(q, ut)

def g_Z(q: Q) -> None:
    """ Multiply amp1 by -1 """
    ut = [
        [1, 0],
        [0, -1],
    ]
    _apply_UT(q, ut)


from dataclasses import dataclass, field
from typing import Optional
from nova.beleg_request import BelegRequest

__NAMESPACE__ = "http://nova.voev.ch/services/v14/vertrieb"

@dataclass
class ErstelleBelege:
    """Request-Element für den 4.

    Klang
    """
    class Meta:
        name = "erstelleBelege"
        namespace = "http://nova.voev.ch/services/v14/vertrieb"

    beleg_request: Optional[BelegRequest] = field(
        default=None,
        metadata={
            "name": "belegRequest",
            "type": "Element",
            "required": True,
        }
    )


from interpreter.execute_expr import execute_expr
from multimethod.multimethod import multimethod
from interpreter.loxfunction import LoxFunction, FunctionType
from interpreter.loxclass import LoxClass
from environment.environment import LoxReturn
from ast.stmt import *

@multimethod
def execute_stmt(stmt: ExprStmt, env: Environment) -> None:
    execute_expr(stmt.expression, env)

@multimethod
def execute_stmt(stmt: PrintStmt, env: Environment) -> None:
    print(execute_expr(stmt.expression, env))

@multimethod
def execute_stmt(stmt: VarStmt, env: Environment) -> None:
    init_val = None
    if stmt.initializer:
        init_val = execute_expr(stmt.initializer, env)
    env.define(stmt.name.lexeme, init_val)

@multimethod
def execute_stmt(stmt: BlockStmt, env: Environment) -> None:
    stmt.env = Environment()
    stmt.env.outer_env = env
    for statement in stmt.statements:
        execute_stmt(statement, stmt.env)

@multimethod
def execute_stmt(stmt: IfStmt, env: Environment):
    if execute_expr(stmt.condition, env):
        return execute_stmt(stmt.then_branch, env)
    elif stmt.else_branch:
        return execute_stmt(stmt.else_branch, env)

@multimethod
def execute_stmt(stmt: WhileStmt, env: Environment) -> None:
    while execute_expr(stmt.condition, env):
        execute_stmt(stmt.body, env)

@multimethod
def execute_stmt(stmt: FunctionStmt, env: Environment) -> None:
    env.define(stmt.name.lexeme, LoxFunction(stmt, env, FunctionType.FUNCTION))

@multimethod
def execute_stmt(stmt: ReturnStmt, env: Environment):
    if stmt.value:
        ret_val = execute_expr(stmt.value, env)
    raise LoxReturn(ret_val)

@multimethod
def execute_stmt(stmt: ClassStmt, env: Environment):
    env.define(stmt.name.lexeme, None)
    superclass = None
    if stmt.superclass:
        superclass = execute_expr(stmt.superclass, env)
        env = Environment(env)
        env.define("super", superclass)
    methods = {method.name.lexeme: LoxFunction(method, env, FunctionType.METHOD) for method in stmt.methods}
    lox_class = LoxClass(stmt.name, superclass, methods)
    if stmt.superclass:
        env = env.outer_env
    env.assign(stmt.name, lox_class, 0)


import cv2
import numpy as np
import face_recognition
import os
from datetime import datetime as datetime

path = r'../image'
images = []
class_name = []
my_list = os.listdir(path)
print("Total Classes Detected:",len(my_list))

for x,image_name in enumerate(my_list):
    current_img = cv2.imread(f'{path}/{image_name}')
    images.append(current_img)
    class_name.append(os.path.splitext(image_name)[0])

print(f'Student in classes are {class_name}')

def find_encodings(images):
    encode_list = []
    for img in images:
        img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
        encode = face_recognition.face_encodings(img)[0]
        encode_list.append(encode)
    return encode_list

encode_list_registered_faces = find_encodings(images)
print('Encodings Complete!')

def check_attendance(name):
    with open('atten.csv','r+') as f:
        my_datalist = f.readlines()
        student_name_list = []
        for line in my_datalist:
            entry = line.split(',')
            student_name_list.append(entry[0])
        if name not in student_name_list:
            now = datetime.now()
            dt_string = now.strftime("%H:%M:%S")
            f.writelines(f'n{name},{dt_string}\n')

# Webcam
cap = cv2.VideoCapture(0)
while True:
    success, img = cap.read()
    img_from_webcam = cv2.resize(img, (0, 0), None, fx = 0.25, fy = 0.25)
    img_from_webcam = cv2.cvtColor(img_from_webcam, cv2.COLOR_BGR2RGB)

    faces_current_frame = face_recognition.face_locations(img_from_webcam)
    encodes_current_frame = face_recognition.face_encodings(img_from_webcam, faces_current_frame) # in case many faces, send face location

    for encode_face, face_location in zip(encodes_current_frame, faces_current_frame):
        matches = face_recognition.compare_faces(encode_list_registered_faces, encode_face)
        face_distance = face_recognition.face_distance(encode_list_registered_faces, encode_face)
        match_faces = np.argmin(face_distance)

        if matches[match_faces]:
            student_name = class_name[match_faces].upper()
            y1,x2,y2,x1 = face_location
            y1, x2, y2, x1 = y1 * 4, x2 * 4, y2 * 4, x1 * 4
            cv2.rectangle(img, (x1, y1), (x2, y2), (0, 255, 0), 2)
            cv2.rectangle(img, (x1, y2 - 35), (x2, y2), (0, 255, 0), cv2.FILLED)
            cv2.putText(img, student_name, (x1 + 6, y2 - 6), cv2.FONT_HERSHEY_DUPLEX, 1.0, (255, 255, 255), 2)
            check_attendance(student_name)

    cv2.imshow('Webcam',img)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

    # cap.release()


from flask import Flask, render_template, redirect
from flask_pymongo import PyMongo
import scrape_mars

app = Flask(__name__)

# Use PyMongo to establish Mongo connection
mongo = PyMongo(app, uri="mongodb://localhost:27017/scrape")

@app.route('/')
def index():
    return """

We are getting close !!!

    """

@app.route('/scrape')
def scrape():

    data = scrape_mars.scrape()

    # Update the Mongo database using update and upsert=True
    mongo.db.collection['mars'].update({}, data, upsert=True)

    return data


from superscaler.ai_simulator.simulator.network_simulator.flow import Flow
from superscaler.ai_simulator.simulator.network_simulator.link_manager import \
     LinkManager
from superscaler.ai_simulator.simulator.device import Device

class NetworkSimulator(Device):
    link_essential_data = {'link_id': int, 'source_name': str,
                           'dest_name': str}
    link_extra_data = {'capacity': str, 'latency': str}

    def __init__(self, name, links_spec, routing_info):
        '''Init NetworkSimulator with links (to generate topology) and routing
        info

        Args:
            links_spec: list of dict, [{'link_id': int, 'source_name': str,
                                        'dest_name': str, 'capacity': str,
                                        'latency': str}]
                src/dest_name format:
                    /server/hostname/DeviceType/DeviceIndex/
                    /switch/switch_name/
            routing_info: {(src_name, dest_name, path_index):[id0, id1..]}
        '''
        super().__init__(name)
        self.__flows = []
        # Init links
        self.__link_manager = LinkManager(links_spec, routing_info)

    def is_idle(self):
        '''Check whether the network simulator is idle
        '''
        return len(self.__flows) == 0

    def get_next_node(self):
        '''Return (next finish node, estimated finish time)
        '''
        if self.is_idle():
            return None, -1
        next_flow = min(self.__flows)
        return (next_flow.node, next_flow.get_estimated_finish_time())

    def enqueue_node(self, node, time_now):
        '''Enqueue a node and update all the flows. The node will first be
        turned into a Flow, then add to the list, and update all Flow's
        capacities

        Args:
            node: class Node, a Send op
                node.name format: ":send:src_name:dest_name:path_index:"
                ":send:/server/hostname1/GPU/0/:/server/hostname1/GPU/1/:0:"
            time_now: float, current time of simulation
        '''
        # Check whether there are flows need to be dequeued first
        if not self.is_idle() and self.get_next_node()[1] < time_now:
            raise ValueError(
                "There are flows need to be dequeued before "
                "enqueuing a new flow!")
        if node.get_op() == 'Send':
            # Enqueue and calculate Send nodes' communication cost
            new_flow = Flow(node, time_now)
            self.__flows.append(new_flow)
            # Add flow to all related links
            route_path = self.__link_manager.get_routing(
                new_flow.node.get_name())
            if route_path is None:
                raise ValueError(
                    "No routing info for current Node: {0})".format(
                        new_flow.node.get_index()))
            for link in route_path:
                link.add_flow(new_flow)
            # Calculate and update all flows' current capacities
            self.__update_all_flows_capacities(time_now)
        elif node.get_op() == "Recv":
            # Recv nodes only act as a dependency. Therefore, Recv nodes'
            # output_tensors should be []
            if node.get_tensors() != []:
                raise ValueError(
                    "Invalid op='Recv' Node, node index {0}".format(
                        node.get_index()
                    ))
            new_flow = Flow(node, time_now)
            # Refresh the estimated_finish_time of the flow, the available
            # bandwith could be any number larger than 0
            new_flow.set_available_bandwidth(float('inf'), time_now)
            self.__flows.append(new_flow)

    def dequeue_node(self):
        '''Dequeue the flow with the smallest estimated_finish_time'''
        if self.is_idle():
            return
        next_flow = min(self.__flows)
        self.__flows.remove(next_flow)
        if next_flow.node.get_op() == "Send":
            # Dequeue a Send Node
            # Delete next_flow in all links
            for link in self.__link_manager.get_links_dict().values():
                if next_flow in link.flows:
                    link.delete_flow(next_flow)
            time_now = next_flow.get_estimated_finish_time()
            # Calculate and update all flows' current capacities
            self.__update_all_flows_capacities(time_now)
        elif next_flow.node.get_op() == "Recv":
            # Dequeue a Recv Node
            pass

    def __update_all_flows_capacities(self, time_now):
        '''Calculate all flow's capacities, and update flows' status during
        [last_start_time, time_now)

        There are two events that will change the flows' capacities: enqueue
        and dequeue.
        '''
        unfinished_schedule_flow = set()
        flow_current_capacity = {}  # {flow_obj: capacity}
        flow_capacity_log = {}  # {flow_obj: [capacities]}

        # Init variables
        for flow in self.__flows:
            if flow.node.get_op() != "Send":
                # Only process Send Node
                continue
            # At first, each flow get 0 BW
            flow_current_capacity[flow] = 0
            flow_capacity_log[flow] = []
            unfinished_schedule_flow.add(flow)
        # Iterate to assign all flows' capacity/bandwidth
        while(len(unfinished_schedule_flow) != 0):
            # Schedule all links
            for link in self.__link_manager.get_links_dict().values():
                link_schedule_result = self.__schedule_link(
                    link,
                    flow_current_capacity,
                    unfinished_schedule_flow)
                # Log all flows capacity to a smaller one
                for flow_in_link, flow_capacity in \
                        link_schedule_result.items():
                    flow_capacity_log[flow_in_link].append(flow_capacity)
            # Log which flow is the bottleneck
            bottleneck_flow = None
            bottleneck_capacity = float('inf')
            for flow in flow_current_capacity:
                if flow not in unfinished_schedule_flow:
                    continue
                # Use minimum as its capacity
                flow_current_capacity[flow] = min(flow_capacity_log[flow])
                # Reset flow_capacity_logs
                flow_capacity_log[flow] = []
                if flow_current_capacity[flow] < bottleneck_capacity:
                    bottleneck_capacity = flow_current_capacity[flow]
                    bottleneck_flow = flow

            if bottleneck_flow is not None:
                # bottleneck_flow is finished, remove it from set.
                unfinished_schedule_flow.remove(bottleneck_flow)

        # Update bandwidth, and change flow status
        for flow in flow_current_capacity:
            flow.set_available_bandwidth(flow_current_capacity[flow], time_now)

    def __schedule_link(self, link, flow_current_capacity,
                        unfinished_schedule_flow):
        '''Schedule flows that in link.flows and in unfinished_schedule_flow,
        allocate remain capacities equally to these flows.
        return a dict {flow_obj: flow_schedule_capacity},
        which contains all flow in link.flows, denoting capacities.

        Args:
            link: class Link
            flow_current_capacity: dict, {flow_obj: capacity}, denoting the
                current capacity for each flow
            unfinished_schedule_flow: a set containing all flows who require
                more capacity
        '''
        scheduler_result = {}
        # Find current available capacity
        total_available_capacity = link.capacity
        unfinished_flows_num = 0
        # Calculate remain capacity in link, check how many flows need further
        # calculation
        for flow in link.flows:
            total_available_capacity -= flow_current_capacity[flow]
            if flow not in unfinished_schedule_flow:
                scheduler_result[flow] = flow_current_capacity[flow]
            else:
                unfinished_flows_num += 1
        # Schedule these unfinished flows
        for flow in link.flows:
            if flow in unfinished_schedule_flow:
                scheduler_result[flow] = flow_current_capacity[flow] \
                    + total_available_capacity / unfinished_flows_num
        return scheduler_result


#########################################################################
# Author: Andrés Herrera Poyatos
# Date: April, 2017
# LocalSearch algorithms for permutation based problems.
#########################################################################

from enum import Enum
from numba import int64, jit, jitclass, types, boolean
import numpy as np
from SimulatedAnnealing import *

from Solution import *
from Aux import *

LSType = Enum('LSType', '2opt 2optb sa none')

def codeLS(ls_type):
    if ls_type == LSType['2opt']:
        return 0
    elif ls_type == LSType['2optb']:
        return 1
    return -1

@jit(types.UniTuple(int64,2)(int64[:], int64, int64, int64[:,:], int64[:,:]), cache=True, nopython=True)
def improveSolution2opt(perm, evals, max_evals, weights, distances):
    """ For each transposition, check if applying it to perm improves the solution.
    If it is the case, then apply it and return how much the solution is improved."""
    for i in range(0, len(perm)):
        for j in range(i+1, len(perm)):
            change_ovalue = applyTranspositionQAP(perm, i, j, weights, distances)
            evals+=1
            if change_ovalue < 0:
                perm[i], perm[j] = exchange(perm[i], perm[j])
                return change_ovalue, evals
            if evals == max_evals:
                return 0, evals
    return 0, evals

@jit(types.UniTuple(int64,2)(int64[:], int64, int64, boolean[:], int64[:,:], int64[:,:]),
     cache=True, nopython=True)
def improveSolution2optBits(perm, evals, max_evals, bits, weights, distances):
    for i in range(0, len(perm)):
        if bits[i]:
            for j in range(0, len(perm)):
                if i != j:
                    change_ovalue = applyTranspositionQAP(perm, i, j, weights, distances)
                    evals+=1
                    if change_ovalue < 0:
                        perm[i], perm[j] = exchange(perm[i], perm[j])
                        bits[j] = 0
                        return change_ovalue, evals
                    if evals == max_evals:
                        return 0, evals
            bits[i] = False
    return 0, evals

@jit(types.UniTuple(int64,2)(int64[:], int64, boolean[:], int64, int64, int64, int64[:,:], int64[:,:]),
     cache=True, nopython=True)
def improveSolution(perm, ovalue, bits, ls_type, evals, max_evals, weights, distances):
    if ls_type == 0:
        diff, evals = improveSolution2opt(perm, evals, max_evals, weights, distances)
    elif ls_type == 1:
        diff, evals = improveSolution2optBits(perm, evals, max_evals, bits, weights, distances)
    return ovalue+diff, evals

@jit(types.UniTuple(int64,2)(int64[:], int64, int64, int64, int64[:,:], int64[:,:]),
     cache=True, nopython=True)
def numbaLocalSearch(perm, ovalue, ls_type, max_evals, weights, distances):
    evals = 0
    improved = True
    bits = np.ones(len(perm), dtype = np.bool_)
    while improved:
        novalue, evals = improveSolution(perm, ovalue, bits, ls_type, evals, max_evals, weights, distances)
        while novalue < ovalue and (max_evals == -1 or evals < max_evals):
            ovalue = novalue
            novalue, evals = improveSolution(perm, ovalue, bits, ls_type, evals, max_evals,
                                             weights, distances)
        if ls_type == 1:
            bits = np.ones(len(perm), dtype = np.bool_)
            novalue, evals = improveSolution(perm, ovalue, bits, 1, evals, max_evals,
                                             weights, distances)
        if novalue < ovalue and (max_evals == -1 or evals < max_evals):
            ovalue = novalue
        else:
            improved = False

    return novalue, evals

class LocalSearch:

    def localSearch(solution, ls_type, max_evals = -1):

        if ls_type == LSType['sa']:
            evals = SA.SALS(solution, max_evals if max_evals > 0 else 10*solution.problem.N*solution.problem.N)
        elif ls_type != LSType['none']:
            solution.ovalue, evals = numbaLocalSearch(solution.perm,
                                           solution.getObjectiveValue(),
                                           int(codeLS(ls_type)),
                                           max_evals,
                                           solution.problem.weights,
                                           solution.problem.distances)
        else:
            evals = 0
        return evals


from __future__ import annotations

import math
import numpy as np
import pytest
from numpy import testing as npt, copy

import mantidimaging.test_helpers.unit_test_helper as th
from mantidimaging.core.operations.rescale import RescaleFilter
from mantidimaging.test_helpers.qt_mocks import MockQSpinBox, MockQComboBox

@pytest.mark.parametrize('value', [255.0, 65535.0, 2147483647.0])
def test_rescale(value):
    images = th.generate_images((10, 100, 100))

    images.data[0:3] = -100
    images.data[3:6] = 0.5
    images.data[6:10] = 1.0

    expected_min_input = 0.0
    images = RescaleFilter.filter_func(images,
                                       min_input=expected_min_input,
                                       max_input=images.data.max(),
                                       max_output=value)

    # below min_input has been clipped to 0
    npt.assert_equal(0, images.data[0:3])
    npt.assert_equal(images.data[3:6], value / 2)
    npt.assert_equal(images.data[6:10], value)

def test_execute_wrapper_no_preset():
    min_input_value = 12
    max_input_value = 34

    min_input = MockQSpinBox(min_input_value)
    max_input = MockQSpinBox(max_input_value)
    max_output = MockQSpinBox(420.0)
    preset = MockQComboBox('None')
    partial = RescaleFilter.execute_wrapper(min_input, max_input, max_output, preset)
    assert partial.keywords['min_input'] == min_input_value
    assert partial.keywords['max_input'] == max_input_value
    assert partial.keywords['max_output'] == 420.0

@pytest.mark.parametrize('type, expected_max', [
    ('int8', 255.0),
    ('int16', 65535),
    ('int32', 2147483647.0),
])
def test_execute_wrapper_with_preset(type: str, expected_max: float):
    min_input_value = 12
    max_input_value = 34

    min_input = MockQSpinBox(min_input_value)
    max_input = MockQSpinBox(max_input_value)
    max_output = MockQSpinBox(420.0)  # this value is overridden by preset
    preset = MockQComboBox(type)
    partial = RescaleFilter.execute_wrapper(min_input, max_input, max_output, preset)  # type: ignore

    assert partial.keywords['min_input'] == min_input_value
    assert partial.keywords['max_input'] == max_input_value
    assert partial.keywords['max_output'] == expected_max

def test_scale_single_image():
    images = th.generate_images((2, 100, 100))

    images.data[0:2] = np.arange(-1, 1, step=0.0002).reshape(100, 100)

    scaled_image = RescaleFilter.filter_array(copy(images.data[0]),
                                              min_input=images.data[0].min(),
                                              max_input=images.data[0].max(),
                                              max_output=65535)
    assert scaled_image.min() == 0
    assert scaled_image.max() == 65535

def test_scale_single_image_bad_offset():
    images = th.generate_images((2, 100, 100))
    try:
        RescaleFilter.filter_array(copy(images.data[0]), min_input=-5000, max_input=5000, max_output=65535)
    except ValueError:
        pass
    except Exception as e:
        AssertionError(f"Unexpected exception was triggered: {e}")

@pytest.mark.parametrize('value', [255.0, 65535.0, 2147483647.0])
def test_rescale_ignores_nans(value):
    images = th.generate_images((10, 100, 100))

    images.data[0:3] = -100.0
    images.data[3:5] = 0.5
    images.data[6][0:10] = np.nan
    images.data[7:10] = 1.0

    expected_min_input = 0.0
    images = RescaleFilter.filter_func(images,
                                       min_input=expected_min_input,
                                       max_input=np.nanmax(images.data),
                                       max_output=value)

    # below min_input has been clipped to 0
    npt.assert_equal(0, images.data[0:3])

    npt.assert_equal(images.data[3:5], value / 2)
    npt.assert_equal(images.data[7:10], value)
    assert all(math.isnan(x) for x in images.data[6][0:10].flatten())

if __name__ == "__main__":
    import pytest

    pytest.main([__file__])


import fenics
nx, ny = 16, 16
mesh = fenics.UnitSquareMesh(nx, ny)

fenics.plot(mesh)

import matplotlib.pyplot as plt
plt.savefig("mesh.png", dpi=150)

V = fenics.FunctionSpace(mesh, "CG", 1)
x, y = fenics.SpatialCoordinate(mesh)
a, b = 0.5, 10.0
expr = (a - x)**2 + b*(y - x**2)**2
rosenbrock_field = fenics.project(expr, V)

plt.clf()
contours = fenics.plot(rosenbrock_field)
plt.colorbar(contours)
plt.savefig("rosenbrock.png", dpi=150)

point = (0.5, 0.5)
print(f"Value at {point} is {rosenbrock_field(point)}")

from fenics import dx, assemble

print(f"Value of the integral is {assemble(x*y*dx)}")
print(f"Value of the integral is {assemble(rosenbrock_field*dx)}")


import sys, getopt
import websocket
import json

def main(argv):
    input_file = argv[0]
    uaa = '''{
  "header": {
    "alg": "RS256",
    "kid": "legacy-token-key",
    "typ": "JWT"
  },
  "payload": {
    "jti": "d73ced66c357444595e71d9972ed7f76",
    "sub": "app_client_id",
    "scope": [
      "timeseries.zones.7de86dcd-074d-4383-88d1-d4f15746efe5.query",
      "uaa.resource",
      "openid",
      "uaa.none",
      "predix-asset.zones.ef39ad6c-9525-4209-8f18-f093ca50a9d0.user",
      "timeseries.zones.7de86dcd-074d-4383-88d1-d4f15746efe5.user",
      "timeseries.zones.7de86dcd-074d-4383-88d1-d4f15746efe5.ingest"
    ],
    "client_id": "app_client_id",
    "cid": "app_client_id",
    "azp": "app_client_id",
    "grant_type": "client_credentials",
    "rev_sig": "94597d8a",
    "iat": 1476899789,
    "exp": 1476942989,
    "iss": "https://6f54b399-ceb4-437e-8a94-b7f539c8e001.predix-uaa.run.aws-usw02-pr.ice.predix.io/oauth/token",
    "zid": "6f54b399-ceb4-437e-8a94-b7f539c8e001",
    "aud": [
      "timeseries.zones.7de86dcd-074d-4383-88d1-d4f15746efe5",
      "predix-asset.zones.ef39ad6c-9525-4209-8f18-f093ca50a9d0",
      "uaa",
      "openid",
      "app_client_id"
    ]
  }
}'''
    uri = "wss://gateway-predix-data-services.run.aws-usw02-pr.ice.predix.io/v1/stream/messages"
    zone_id = "7de86dcd-074d-4383-88d1-d4f15746efe5"
    origin = "run.aws-usw02-pr.ice.predix.io"
    elec_type = argv[1]
    with open(input_file) as data_file:
        data = json.load(data_file)
    openWSS(uaa, uri, zone_id, origin, data)

def send_first_data(ws, name):
    first_data = '''{
                      "messageId": ''' + name + ''',
                      "body":[
                         {
                            "name":"''' + name + '''",
                            "datapoints": ['''
    ws.send(first_data)

def send_datapoints(ws):
    datapoint = ""
    for n in PAYLOADS:
        datapoint = '''[
                        ''' + n[0] + '''
                        ''' + n[1] + '''
                        ''' + n[2] + '''
                       ]'''
        ws.send(datapoint)

def send_last_data(ws, elec_type):
    last_data = '''],
                "attributes":{
                              "type":"''' + elec_type + '''"
                              }
                           }
                        ]
                     }'''
    ws.send(last_data)

def sendPayload(ws):
    i = 0
    list_node = []
    timestamp_beg = 0
    timestamp_end = 0
    timestamp = 0
    name = ""
    value = 0
    quality = 0
    input_file = argv[0]
    payloads = []
    for dic in data:
        for series in data[i]:
            if (series == "ID"):
                name = data[i][series]
            elif (series == "TimeStampStart"):
                timestamp_beg = (int)(data[i][series])
                timestamp = timestamp_beg + (int)((timestamp_end - timestamp_beg) / 2)
            elif (series == "TimeStampEnd"):
                timestamp_end = (int)(data[i][series])
            elif (series == "y"):
                value = (float)(data[i][series])
            elif (series == "Quality"):
                quality = (int)(data[i][series])
        list_node = (timestamp, value, quality)
        if list_node:
            payloads += list_node
        i += 1

    send_first_data(ws, name)
    send_datapoints(ws, payloads)
    send_last_data(ws, elec_type)

def openWSS(uaaToken, tsUri, tsZone, origin, data):
    websocket.enableTrace(False)
    host = tsUri
    headers = {
                'Authorization:bearer ' + uaaToken,
                'Predix-Zone-Id:' + tsZone,
                'Origin:' + origin
    }
    ws = websocket.WebSocketApp(
                                host,
                                header = headers,
                                on_message = on_message,
                                on_error = on_error,
                                on_close = on_close
    )
    sendPayload(ws, data)

if __name__ == "__main__":
   main(sys.argv[1:])


import tensorflow as tf
import numpy as np

image = np.array([[[[4],[3],
                    [[2],[1]]]]], dtype=np.float32)
pool = tf.nn.max_pool(image, ksize=[1, 2, 2, 1],
                      strides=[1, 1, 1, 1], padding='SAME')
#ksize는 filter의 크기 2x2이며, stride의 경우 1x1로 1이다.
print(pool.shape)
print(pool.eval())
#결과는 (1, 2, 2, 1)이며, 각 값이 4. 3.
                                # 2. 1. 이다. []는 생략
#zero padding과 max_pooling과정을 모두 실행한 결과이다.

#★주의할점! 만약 28x28의 image라면 여기서 stride가 1, 2, 2, 1이면 2이다.
#28의 image가 2씩 진행이 되기에 zero padding로 그대로 유지가 되면 14x14이다.★


from setuptools import find_packages, setup

with open("README.md") as f:
    long_description = f.read()

setup(
    name="soft_moe",
    packages=find_packages(),
    version="0.0.1",
    license="Apache-2.0",
    description="PyTorch implementation of 'From Sparse to Soft Mixtures of Experts'",
    long_description=long_description,
    long_description_content_type="text/markdown",
    author="Ben Conrad",
    author_email="benwconrad@proton.me",
    url="https://github.com/bwconrad/soft-moe",
    keywords=[
        "transformers",
        "artificial intelligence",
        "computer vision",
        "deep learning",
    ],
    install_requires=[
        "timm >= 0.9.2",
        "torch >= 2.0.1",
    ],
    classifiers=[
        "Intended Audience :: Developers",
        "Intended Audience :: Science/Research",
        "Topic :: Scientific/Engineering :: Artificial Intelligence",
        "License :: OSI Approved :: Apache Software License",
        "Programming Language :: Python :: 3.10",
        "Programming Language :: Python :: 3.11",
    ],
    python_requires=">=3.10",
)


from setuptools import setup, find_packages
from pathlib import Path

lines = Path(".").joinpath("__init__.py")
version = "1.0.0"
for line in lines.read_text().split("\n"):
    if line.startswith("__version__ ="):
        version = line.split(" = ")[-1].strip('"')
        break

setup(
    name="BedloadSeasons",
    version=version,
    python_requires=">=3.6",
    author="sschwindt, beatriznegreiros",
    author_email="sebastian.schwindt@iws.uni-stuttgart.de",
    url="https://github.com/sschwindt/bedload-seasons",
    project_urls={
        "Documentation": "https://bedload-seasons.readthedocs.io/",
        "Funding": "https://hydro-informatics.com/",
        "Source": "https://github.com/sschwindt/bedload-seasons",
    },
    # this should be a whitespace separated string of keywords, not a list
    keywords="bedload, sediment transport, seasonality, timing, fall, spring, summer, winter, generalized extreme value, Weibull, snowmelt, glacier, climate change",
    description="Codes and data for analysis of enriched bedload dataset supporting manuscript findings",
    license="BSD License",
    long_description=Path("./README.md").read_text(),
    long_description_content_type="text/markdown",
    packages=find_packages(),
    install_requires=[
        "pyyaml",
        "docutils>=0.15",
        "sphinx",
        "click",
        "pydata-sphinx-theme~=0.4.1",
        "beautifulsoup4",
        'importlib-resources~=3.0.0; python_version < "3.7"',
    ],
    # dependency_links=[
    #     "git+https://github.com/ecohydraulics/flusstools-pckg#egg=flusstools-pckg"
    # ],
    include_package_data=True,
    extras_require={
        "code_style": ["pre-commit~=2.7.0"],
        "sphinx": [
            "folium",
            "numpy",
            "matplotlib",
            "ipywidgets",
            "openpyxl",
            "pandas",
            "nbclient",
            "myst-nb~=0.10.1",
            "sphinx-togglebutton>=0.2.1",
            "sphinx-copybutton",
            "seaborn",
            "sphinxcontrib-bibtex",
            "sphinx-thebe",
            "ablog~=0.10.11",
        ],
        "testing": [
            "myst_nb~=0.10.1",
            "sphinx_thebe",
            "coverage",
            "pytest~=6.0.1",
            "pytest-cov",
            "pytest-regressions~=2.0.1",
        ],
        "live-dev": ["sphinx-autobuild", "web-compile~=0.2.1"],
    },
    entry_points={
        "sphinx.html_themes": ["sphinx_book_theme = sphinx_book_theme"],
    },
    classifiers=[
        "Programming Language :: Python :: 3",
        "License :: OSI Approved :: BSD License",
        "Operating System :: OS Independent",
        "Programming Language :: Python",
        "Programming Language :: Python :: 3.6",
        "Programming Language :: Python :: 3.7",
        "Programming Language :: Python :: 3.8",
        "Programming Language :: Python :: 3.9",
        "Development Status :: 4 - Beta",
    ],
)


# 컴퓨터의 수
n = int(input())

# 컴퓨터 쌍의 수
m = int(input())

# 연결된 네트워크 입력
network = []
for i in range(m):
    nt = list(map(int, input().split()))
    network.append(nt)

    # 양방향 구현
    nt = [nt[1], nt[0]]
    network.append(nt)

# 컴퓨터 순서대로 정렬
network.sort(key=lambda x: x[0])

# 컴퓨터 방문 여부
computer = [False] * (n + 1)

result = -1

# DFS로 특정한 노드를 방문한 뒤에 연결된 모든 노드들도 방문
def dfs(x):
    global result
    # 현재 노드를 아직 방문하지 않았다면
    if computer[x] == False:
        # 해당 노드를 방문 처리
        result += 1
        computer[x] = True
        # 연결된 컴퓨터 차례대로 호출
        for c in network:
            if c[0] == x:
                dfs(c[1])

            if c[0] > x:
                break

# 1번 컴퓨터로 웜 뿌리기
dfs(1)

print(result)   # 정답 출력


#!/usr/bin/env python
# !-*-coding:utf-8 -*-
# !time :  11:18
# !@Author : SweetBeaner
# !@File :.py
# !@Information :

# 使用while循环计算1~100的累加和（包含1和100）
# i = 1
# sum = 0
# while i <= 100:
#     sum = sum + i
#     i += 1
# print(sum)

# 设计“过7游戏”的程序, 打印出1-100之间除了7和7的倍数之外的所有数字，
# 如果遇见7和7的倍数则打印“哈~”跳过本次循环。
# i = 1
# while i <= 100:
#     if i % 7 != 0:
#         print(i)
#         i += 1
#     else:
#         print('哈')
#         i += 1
#         continue
# 从键盘上输入一个字母，判断此字母是否在变量 a 中，
# 如果在则输出 找到了， 如果不在 则输出 查无此字母
# a = "itheima"
# inp = input('请输入一个字母')
# for i in a:
#     if i == inp:
#         print('找到了')
#         break
# else:
#     print('查无此字母')

# 要求用户输入一个字符串，遍历当前字符串并打印，如果遇见
# “q”,则跳出循环。如果遇见“ ”（空格）则跳过当前输出。

# inp = input('请输入一个字符串：')
# for i in inp:
#     if i=='q':
#         break
#     elif i==' ':
#         continue
#     print(i)

# 使用for循环计算从0到用户输入的值的累加和
inp = input('请输入一个数字')
sum = 0
for i in range(int(inp)+1):
    sum = sum+i
print(sum)


class Solution:
    def nearestValidPoint(self, x: int, y: int, points: List[List[int]]):
        res = -1
        min_dist = 99999999
        for n, i in enumerate(points):
            if i[0] == x or i[1] == y:
                _ = abs(x-i[0]) + abs(y-i[1])
                if _ < min_dist:
                    min_dist = _
                    res = n
        return res


import unittest
from pymongo import MongoClient
from Executor import Executor
from time import gmtime, mktime

class TestExecutor(unittest.TestCase):

    def test_should_be_able_to_get_a_query_from_the_database(self):
        db = MongoClient('localhost', 27017)['test']
        e = Executor(db)

        r = e.measure_result('0043', {'effective_date': int(mktime(gmtime(2010, 9, 19)))})

        self.assertEqual(r['population'], 3)
        self.assertEqual(r['numerator'], 1)
        self.assertEqual(r['denominator'], 2)
        self.assertEqual(r['exceptions'], 0)

if __name__ == '__main__':
    unittest.main()


class Solution:
    def sub(self, s, i, j):

        if(i==j):
            return 1
        elif(j==i+1 and s[i] == s[j]):
            return 2
        # if there's a match
        elif(s[i] == s[j]):
            return 2 + self.sub(s, i+1, j-1)
        else:
            return max(self.sub(s, i+1, j), self.sub(s, i, j-1))

    def longestPalindrome(self, s):
        # Fill this in.

        # this can be done with backtracking. sub(i,j) is the longest palindrome from i to j
        # The Solution is solved by running sub(0, n-1)
        # return self.sub(s, 0, len(s)-1)

        # DP solution

        n = len(s)
        LPS = [[0 for x in range(n)] for x in range(n)]
        for i in range(n):
            LPS[i][i] = 1
        for c in range(2, n+1):
            for i in range(n-c+1):
                j = i + c - 1
                if(c==2 and s[i] == s[j]):
                    LPS[i][j] =  2
                elif(s[i] == s[j]):
                    LPS[i][j] = 2 + LPS[i+1][j-1]
                else:
                    LPS[i][j] = max(LPS[i+1][j], LPS[i][j-1])
        return LPS[0][n-1]

# Test program
s = "doofood"
print(str(Solution().longestPalindrome(s)))
# racecar


# isiterable.py
# Good method to check if object s is iterable: iter(s) should not return an error.
# iter() works if s has only __getitem__, while for abc.Iterable it needs __iter__.
# 2021-05-18    PV

def is_iterable(s) -> bool:
    try:
        _ = iter(s)
    except TypeError:
        return False
    except Exception as ex:
        raise ex
    return True

if __name__ == '__main__':
    print(f'[1,2]:', is_iterable([1, 2]))
    print(f'123:', is_iterable(123))


from django.http import HttpResponse, JsonResponse
from django.shortcuts import get_object_or_404, render
from .models import Website, WebsiteCall, Publication, WebsiteStatus, CuratedWebsite
from collections import Counter, namedtuple, defaultdict
from django.core import serializers
from django.core.paginator import Paginator
from datetime import timedelta, date, datetime
import json
from django.contrib.postgres.aggregates import ArrayAgg, BoolOr
from django.db.models.functions import TruncDate, Cast
from django.db.models import Count, Q, BooleanField
from django.db import models
from cache_memoize import cache_memoize
from django.conf import settings

# cache for 6h
@cache_memoize(settings.CACHE_TIMEOUT)
def get_index_stats():
    context = {}
    context['website_count'] = Website.objects.count()
    context['paper_count'] = Publication.objects.count()
    context['online_count'] = Website.objects.filter(status=WebsiteStatus.ONLINE).count()
    context['temp_offline_count'] = Website.objects.filter(
        status=WebsiteStatus.TEMP_OFFLINE).count()
    context['offline_count'] = Website.objects.filter(status=WebsiteStatus.OFFLINE).count()
    return context

# cache for same SQL query and same websitecall
# that way, when we add new data we get a new key
def get_all_statistics_args_rewrite(pub_queryset, curated=False):
    return f"{str(pub_queryset.query)}{WebsiteCall.objects.last().id}{curated}"

# cache for 12h
@cache_memoize(12 * 60 * 60, args_rewrite=get_all_statistics_args_rewrite)
def get_all_statistics(pub_queryset, curated=False):
    # website count
    # paper count
    # online count
    # temp offline count
    # offline count
    # temporal online, offline, tmp offline
    # top 10 journals, online, offline, tmp offline
    # per year online, offline, tmp offline
    if curated:
        p_websites = pub_queryset.values_list("journal", "year", "website__id", "states", "status",
                                              "pubmed_id")
        num_pubs = len(p_websites)
        website_papers = defaultdict(list)
        website2states = dict()
        for e in p_websites:
            website_papers[e[2]].append((e[0], e[1]))
            website2states[e[2]] = (e[3], e[4], e[5])
    else:
        p_websites = pub_queryset.annotate(website_states=ArrayAgg("websites__states")).values_list(
            "journal", "year", "website_pks", "website_states", "status", "pubmed_id")
        num_pubs = len(p_websites)
        website_papers = defaultdict(list)
        website2states = dict()
        for e in p_websites:
            for w_id, w_states, w_status in zip(e[2], e[3], e[4]):
                website_papers[w_id].append((e[0], e[1]))
                website2states[w_id] = (w_states, w_status, e[5])

    context = dict()
    context['website_count'] = len(website2states)
    context['paper_count'] = num_pubs
    latest_time = WebsiteCall.objects.latest("datetime")
    temp_info_num_days = 15
    stat1_names = []
    for i in range(temp_info_num_days):
        c = latest_time.datetime - timedelta(days=temp_info_num_days - i - 1)
        stat1_names.append("{}.{}.{}".format(c.day, c.month, c.year))
    stat1_online = [0 for _ in range(temp_info_num_days)]
    stat1_offline = [0 for _ in range(temp_info_num_days)]
    stat1_tmp_offline = [0 for _ in range(temp_info_num_days)]

    tmp_offline_websites = set()
    online_websites = set()
    offline_websites = set()
    for w_id, (w_states, w_status, _) in website2states.items():
        for i in range(temp_info_num_days):
            if temp_info_num_days - i <= len(w_states):
                pos = len(w_states) - temp_info_num_days + i
                if w_states[pos]:
                    stat1_online[i] += 1
                else:
                    srange = w_states[(pos - settings.TEMP_OFFLINE_DAYS):pos + 1]
                    online_in_range = any(e is True for e in srange)
                    if online_in_range and (
                        w_status == WebsiteStatus.OFFLINE or w_status == WebsiteStatus.TEMP_OFFLINE):
                        stat1_tmp_offline[i] += 1
                    elif any(e is False for e in srange):
                        stat1_offline[i] += 1
        if w_status == WebsiteStatus.ONLINE:
            online_websites.add(w_id)
        elif w_status == WebsiteStatus.TEMP_OFFLINE:
            tmp_offline_websites.add(w_id)
        elif w_status == WebsiteStatus.OFFLINE:
            offline_websites.add(w_id)

    latest_date = latest_time.datetime
    if curated:
        website_states = [{"pubmed_id": e[5], "websites__states": e[3]} for e in p_websites]
        if CuratedWebsite.objects.exists():
            dates = CuratedWebsite.objects.first().dates
            latest_date = dates[len(dates) - 1]
    else:
        website_states = [{"pubmed_id": e[5], "websites__states": w_states} for e in p_websites for
                          w_states in e[3]]

    state_dates = [(latest_date - timedelta(days=day_delta)).date().strftime("%Y-%m-%d")
                   for
                   day_delta in range(settings.TEMPORAL_INFO_DAYS, -1, -1)]

    context["website_states"] = website_states
    context["state_dates"] = state_dates

    context['online_count'] = len(online_websites)
    context['offline_count'] = len(offline_websites)
    context['temp_offline_count'] = len(tmp_offline_websites)
    context['stat1_names'] = json.dumps(stat1_names)
    context['stat1_online'] = json.dumps(stat1_online)
    context['stat1_offline'] = json.dumps(stat1_offline)
    context['stat1_tmp_offline'] = json.dumps(stat1_tmp_offline)
    journal_counts = Counter(e[0] for papers in website_papers.values() for e in papers)
    context["top10_journals"] = [{"journal": e[0], "count": e[1]} for e in
                                 journal_counts.most_common(10)]
    journals2count = {e["journal"]: e["count"] for e in context["top10_journals"]}

    top_journals_online = Counter(
        e[0] for w in online_websites for e in website_papers[w] if e[0] in journals2count)
    tmp_offline_top_journals = Counter(
        e[0] for w in tmp_offline_websites for e in website_papers[w] if e[0] in journals2count)
    offline_top_journals = Counter(
        e[0] for w in offline_websites for e in website_papers[w] if e[0] in journals2count)

    journals = [j for j, _ in sorted(journals2count.items(), key=lambda e: e[1], reverse=True)]
    context["top10_journals_names"] = json.dumps(journals)
    context["top10_journals_online"] = json.dumps([top_journals_online.get(j, 0) for j in journals])
    context["top10_journals_tmp_offline"] = json.dumps(
        [tmp_offline_top_journals.get(j, 0) for j in journals])
    context["top10_journals_offline"] = json.dumps(
        [offline_top_journals.get(j, 0) for j in journals])

    year_counts = Counter(e[1] for papers in website_papers.values() for e in papers)
    context["publications_per_year"] = [{"year": e[0], "count": e[1]} for e in
                                        sorted(year_counts.items(), key=lambda e: e[0])]
    year2count = {e["year"]: e["count"] for e in context["publications_per_year"]}
    years_online = Counter(e[1] for w in online_websites for e in website_papers[w])
    tmp_offline_years = Counter(e[1] for w in tmp_offline_websites for e in website_papers[w])
    offline_years = Counter(e[1] for w in offline_websites for e in website_papers[w])
    years = sorted(year2count.keys())

    context["pubs_per_year_names"] = json.dumps(years)
    context["pubs_per_year_online"] = json.dumps([years_online[y] for y in years])
    context["pubs_per_year_tmp_offline"] = json.dumps([tmp_offline_years[y] for y in years])
    context["pubs_per_year_offline"] = json.dumps([offline_years[y] for y in years])
    return context


import cv2
import numpy as np

def grayscale(image: np.ndarray) -> np.ndarray:
    return cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

def threshold(image: np.ndarray, method: str = "otsu", thresh: int = 127) -> np.ndarray:
    if method == "otsu":
        _, binary_image = cv2.threshold(image, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
    else:
        _, binary_image = cv2.threshold(image, thresh, 255, cv2.THRESH_BINARY)
    return binary_image

def denoise(image: np.ndarray, method: str = "median", kernel_denoise: int = 3) -> np.ndarray:
    if method == "median":
        return cv2.medianBlur(image, kernel_denoise)
    else:
        return cv2.GaussianBlur(image, (kernel_denoise, kernel_denoise), 0)

def sharpen(image: np.ndarray, amount: float = 2, kernel_size: int = 3) -> np.ndarray:
    blurred_image = cv2.GaussianBlur(image, (0, 0), kernel_size)
    sharpened_image = cv2.addWeighted(image, 1 + amount, blurred_image, -amount, 0)
    return sharpened_image

def generate_grid(num_rows, num_cols, image_size=(1038, 2162)):
    grid = np.zeros((num_rows * image_size[0], num_cols * image_size[1], 3))
    for i in range(num_rows):
        for j in range(num_cols):
            img = cv2.imread('outputs/binary_image_{}.png'.format((1+i * num_cols + j)))
            grid[i * image_size[0]: (i + 1) * image_size[0], j * image_size[1]: (j + 1) * image_size[1]] = img
    cv2.imwrite('./binary_grid_.png', grid)

def main(kernel_size: int, kernel_denoise: int, amount: int):
    image = cv2.imread("example.png")
    if image is None:
        print("Error: Could not open the image file.")
        return

    gray_image = grayscale(image)
    denoised_image = denoise(gray_image, 'gauss', kernel_denoise=kernel_denoise)
    sharpened_image = sharpen(denoised_image, amount, kernel_size)
    binary_image = threshold(sharpened_image)

    # save the image
    # cv2.imwrite('outputs/gray_image.png', gray_image)
    # cv2.imwrite('outputs/denoised_image.png', denoised_image)
    # cv2.imwrite('outputs/sharpened_image.png', sharpened_image)
    cv2.imwrite('outputs/binary_image_{}.png'.format(kernel_size), binary_image)

for i in range(1, 21, 2):
    main(i, 5, 5)

generate_grid(4, 5, image_size=(1038, 2162))


import mxnet as mx

from ...base.layer.embed import BaseEmbedAPI

class MXNetEmbedAPI(BaseEmbedAPI):
    def __init__(self):
        BaseEmbedAPI.__init__(self)

    def embed(self, x, reference):
        vocab_size, channels = reference.shape
        channels_last = mx.nd.Embedding(
            data=x, weight=reference, input_dim=vocab_size, output_dim=channels,
            dtype=reference.dtype)
        ndim = channels_last.ndim
        axes = (0, ndim - 1) + tuple(range(1, ndim - 1))
        return mx.nd.transpose(channels_last, axes)


from langchain.document_loaders import CSVLoader
from langchain.llms import OpenAI
from langchain.prompts import PromptTemplate
from langchain.embeddings import OpenAIEmbeddings
from langchain.vectorstores import FAISS
from langchain.chains import RetrievalQA
from dotenv import load_dotenv
load_dotenv()
import pandas as pd
from langchain.callbacks import StdOutCallbackHandler

import os
llm=OpenAI(temperature=0.2,api_key=os.environ["OPENAI_API_KEY"])

vectordb_file_path = "vectordb"
instructor_embeddings = OpenAIEmbeddings(api_key=os.environ["OPENAI_API_KEY"])
def load_data():
    loader = CSVLoader(file_path="data_2.csv",source_column="Sentence")
    documents = loader.load()
    # Create a FAISS instance for vector database from 'data'
    vectordb = FAISS.from_documents(documents=documents,
                                    embedding=instructor_embeddings)

    # Save vector database locally
    vectordb.save_local(vectordb_file_path)

def get_response():
    vectordb = FAISS.load_local(vectordb_file_path, instructor_embeddings)
    retriever = vectordb.as_retriever(score_threshold=0.8,k=3)

    prompt_template = """Given the following context and a question, generate an answer based on this context only.
    In the answer try to provide as much text as possible from "response" section in the source document context without making much changes.
    If the answer is not found in the context, kindly state "I don't know." Don't try to make up an answer.  You are about to find any offers available for the question from the user and list the required content.
    You are searching for the best coupons available for the products
    you are looking for offers on the following products:

    CONTEXT: {context}

    QUESTION: {question}"""

    PROMPT = PromptTemplate(
        template=prompt_template, input_variables=["context", "question"]
    )

    response = RetrievalQA.from_chain_type(llm=llm,
                                         chain_type="stuff",
                                         input_key="query",
                                         retriever=retriever,
                                         return_source_documents=True,
                                         chain_type_kwargs={"prompt": PROMPT},
                                         callbacks=[StdOutCallbackHandler()],
                                         verbose=True,
                                         )

    return response

def getid(s_doc):
    df=pd.read_csv("data_2.csv")
    return [df.iloc[t.metadata["row"],-1]  for t in s_doc]

import re

def output_format(input_string):
    # Extract words from the input string

    words = re.findall(r'\b[\w\']+\b', input_string)

    # Join the extracted words into a single string
    result = ' '.join(words)
    return result

def results(query):
    response = get_response()
    out=response(query)
    return output_format(out["result"]),getid(out["source_documents"])

if __name__=="__main__":
    # load_data()
    res,source=results("do you have any products in apple and samsung?")
    # print(res)
    if not (re.search(" I don't know",res)):
        print(res)
        print("\n\n")
        print("You can look into other offers from the following results:\n")
        print(source)
    else:
        print("I don't know.")
        print("I cant suggest you other suggestions for your search")


# I take help of chatgpt while writting source code for Chat Template Formatter

from pyparsing import Literal, Word, alphas, alphanums, nestedExpr, ParseException

def format_template(input_text):

    user_start = "{{#user}}"#tags used to identify the starting and end seg of  paragraph where user and assistant speaks
    user_end = "{{/user}}"  ##
    assistant_start = "{{#assistant}}"
    assistant_end = "{{/assistant}}"

    Gen_cmd = "{{gen" + nestedExpr("{{", "}}") + "}}"  #nestedexpr help to match nested expression in curly braces
    assistant = assistant_start + Gen_cmd + assistant_end
    #combine gen cmd and assistant seg

    try:
        parsed = Gen_cmd.transformString(input_text)
        parsed = assistant.transformString(parsed)
    except ParseException:
        parsed = input_text
    #  if parsing fail indicated by parseexception the code falls back to using the original input text

    UserSeg = []
    assistSeg = ""
    #  lists holding user seg and assistant seg

    for seg in parsed.split(assistant_start):
        if assistant_end in seg:
            assistSeg = seg
        else:
            UserSeg.append(seg)

# if an assistant cmd is found inside a segm, the segm is considere part of the assistant response. else it is considered a user seg and added to the  user seg list

    if not assistSeg.endswith("{{gen 'write' }}{{/assistant}}"):
        assistSeg += "{{gen 'write' }}{{/assistant}}"
# if the assistant seg ends with a "{{gen 'write' }}{{/assistant}}" command. if it does not this command is added. This ensures that the assistant's response is properly concluded

    formatted_temp = " ".join([
        user_start + user_segment.strip() + user_end
        for user_segment in UserSeg
    ]) + " " + assistSeg

    return formatted_temp
# return formated template output

input_text = input("Enter the required text: ")
formated_output = format_template(input_text)
print("formated output : ", formated_output)


from django.contrib import admin

from .models import Request
# Register your models here.
class RequestAdmin(admin.ModelAdmin):
    fieldsets = [
        ('Name',            {'fields': ['name_text']}),
        ('Email',           {'fields': ['email_text']}),
        ('Card Name',       {'fields': ['card_name']}),
        ('Card Set',        {'fields': ['card_set']}),
        ('Card Quantity',   {'fields': ['card_quantity']}),
        ('Alter Type',      {'fields': ['alter_type']}),
        ('Card Provided?',  {'fields': ['card_provided']}),
    ]

admin.site.register(Request, RequestAdmin)


#!/usr/bin/env python
from gnuradio import gr, gru, audio, eng_notation, blocks
from gnuradio.eng_option import eng_option
from optparse import OptionParser

class app_top_block(gr.top_block):
    def __init__(self, options):
        gr.top_block.__init__(self)

        IN = blocks.file_source(gr.sizeof_char, options.input_file)
        bits_per_symbol = 2
        UNPACK = blocks.packed_to_unpacked_bb(bits_per_symbol, gr.GR_MSB_FIRST)
        OUT = blocks.file_sink(gr.sizeof_char, options.output)

        self.connect(IN, UNPACK, OUT)

def main():
    parser = OptionParser(option_class=eng_option)
    parser.add_option("-i", "--input-file", type="string", default="in.dat", help="specify the input file")
    parser.add_option("-o", "--output", type="string", default="out.dat", help="specify the output file")

    (options, args) = parser.parse_args()

    tb = app_top_block(options)
    try:
        tb.run()
    except KeyboardInterrupt:
        tb.stop()

if __name__ == "__main__":
    main()


from django.urls import path, include
from . import views
from .views import register
urlpatterns = [
    path('all_tickets/', views.all_tickets),
    path('mytickets/incident_user/<int:number>', views.incident_user),
    path('my_orders/order_user/<int:number>', views.order_user),
    path('all_tickets/incident_agent/<int:number>', views.incident_agent),
    path('mytickets/incident_agent/<int:number>', views.incident_agent),
    path('mytickets/', views.my_tickets),
    path('login/', views.sign_in),
    path('my_orders/', views.my_orders),
    path('signout/', views.signout),
    path('contact/', views.contact),
    path('create_ticket/', views.create_ticket),
    path('order/', views.order),
    path('admin_rights/', views.admin_rights),
    path('order_smartcard/', views.new_smartcard),
    path('fileshare_access/', views.fileshare_access),
    # path('contact/', views.contact),
    path('home/', views.index, name='home'),
    path('', views.index, name='index'),
    path('register/', register, name='signup'),
    # path('settings/', include(views.settings)),

]


import os.path
import requests
from tqdm import tqdm
from bs4 import BeautifulSoup
from pyld import jsonld
from urllib.parse import unquote

from io import StringIO
import sys

import xml.etree.ElementTree as ET
from music21 import converter, environment

W3C_HAS_SRC = "http://www.w3.org/ns/oa#hasSource"
NANOPUB_URL = "http://digitalduchemin.org:8080/nanopub-server"
LAST_PAGE = 11

# To access emaMXL module from inside tst folder
sys.path.append(os.path.abspath(os.path.join('..', 'emaMXL')))
from emaMXL.emaexp import EmaExp
from emaMXL import emaexp, emaexpfull, slicer

# For suppressing music21 warnings, so the tqdm progress bar is not reprinted upon warnings
class Capturing(list):
    def __enter__(self):
        self._stderr = sys.stderr
        sys.stderr = self._stringio = StringIO()
        return self

    def __exit__(self, *args):
        self.extend(self._stringio.getvalue().splitlines())
        del self._stringio    # free up some memory
        sys.stderr = self._stderr

#
# Scraping functions
#
def scrape_page_nanopubs(page_num, d={}, fails={}):
    """
    Scrapes all nanopubs from the page.
    Saves score xml + selection xml to disk.
    Returns dict[nanopub_num] = (score_name, ema_expression) for reference.
    """
    jsonlds = get_jsonlds(page_num)
    for i in tqdm(range(len(jsonlds))):
        nanopub_num = 1000*(page_num - 1) + i
        try:
            with Capturing() as output:
                score_name, expr_str = scrape_nanopub(nanopub_num, jsonlds[i])
                d[nanopub_num] = (score_name, expr_str)
        except Exception as ex:
            fails[nanopub_num] = ex

    return d, fails

def scrape_nanopub(nanopub_num, jsonld_filename):
    ema_url = ema_url_from_jsonld(jsonld_filename)
    mei_url = unquote(ema_url.split("/")[-4])
    expr_str = "/".join(ema_url.split("/")[-3:])
    score_name = mei_url.split("/")[-1].split(".")[0]
    #
    # Will warn "mei.base: WARNING: Importing <slur> without @startid and @endid is not yet supported."
    #
    # Downloads the MEI score, converts to XML, saves as a file. Use etree to load from file.
    score_path = f"data/scores/{score_name}.xml"
    if not os.path.exists(score_path):
        score = converter.parseURL(mei_url, format='mei')
        score.write("musicxml", fp=score_path)

    # Downloads the MEI selection, converts to XML, saves as a file, and stores etree in the dict.
    selection_path = f"data/selections/nanopub_{nanopub_num}.xml"
    if not os.path.exists(selection_path):
        selection_score = converter.parseURL(ema_url, format='mei')
        selection_score.write("musicxml", fp=selection_path)

    return score_name, expr_str

#
# Evaluation functions
#
def evaluate_ema2_page(page_num):
    jsonlds = get_jsonlds(page_num)
    for i in range(len(jsonlds)):
        nanopub_num = 1000 * (page_num - 1) + i
        ema_url = ema_url_from_jsonld(jsonlds[i])
        mei_url = unquote(ema_url.split("/")[-4])
        expr_str = "/".join(ema_url.split("/")[-3:])
        score_name = mei_url.split("/")[-1].split(".")[0]

        try:
            evaluate_ema2(score_name, expr_str, f"nanopub_{nanopub_num}")
        except Exception as ex:
            print(f"Exception for nanopub_{nanopub_num}: {ex}")

def evaluate_ema2(score_name, expr_str, truth_filename, print_fail_elem=False):
    # TODO: Maybe try downloading if we can't find it
    score_path = f"data/scores/{score_name}.xml"
    selection_path = f"data/selections/{truth_filename}.xml"
    if not os.path.exists(score_path):
        print(f"Skipping {truth_filename}; score not found.")
        return
    if not os.path.exists(selection_path):
        print(f"Skipping {truth_filename}; selection not found.")
        return

    print(f"Evaluating {truth_filename}")
    ema2_tree = slicer.slice_score_path(score_path, expr_str)
    omas_tree = ET.parse(selection_path)
    diff_test(ema2_tree.getroot(), omas_tree.getroot(), print_fail_elem)
    # For debugging
    ema2_tree.write("data/selection_temp.xml")
    return ema2_tree, omas_tree

# Suggested usage: Open Python console, run this function,
# then compare the content of selection_temp.xml and selections/nanopub_X.xml.
def evaluate_ema2_by_num(nanopub_num, print_fail_elem=False):
    """ Evaluates a single nanopub. """
    page_num = 1 + nanopub_num // 1000
    jsonlds = get_jsonlds(page_num)
    score_name, expr_str = scrape_nanopub(nanopub_num, jsonlds[nanopub_num % 1000])
    return evaluate_ema2(score_name, expr_str, f"nanopub_{nanopub_num}", print_fail_elem)
# List of failing nanopubs (but are okay to ignore)
# Selection on digital du chemin is incorrect (usually minor errors):
# 22, 31, 35, 63, 67, 70, 74, 106
# bad music21 conversion / malformed score:
# 40, 85

def diff_test(elem1: ET.Element, elem2: ET.Element, print_fail_elem):
    """ A simple recursive function that checks if the structure and tags of these two trees are generally the same. """
    if len(elem1) == len(elem2) and elem1.tag == elem2.tag:
        # print(f"Matched {root1.tag}: {len(root1)} children.")
        # TODO: Is it wise to sort? The nanopubs might have elements out of order
        #  (or maybe music21 conversion jumbled them up)
        # Leaving staves unsorted (by id) seems okay
        elem1 = sorted(list(elem1), key=lambda x: (x.tag, x.get('id', None) if x.tag != 'part' else None))
        elem2 = sorted(list(elem2), key=lambda x: (x.tag, x.get('id', None) if x.tag != 'part' else None))
        for i in range(len(elem1)):
            diff_test(elem1[i], elem2[i], print_fail_elem)
    else:
        print(f"Mismatch at {elem1.tag}, {elem1.attrib}: {len(elem1)} children vs. {elem2.tag}, {elem2.attrib}: {len(elem2)} children.")
        if print_fail_elem:
            print_elems_recursive(elem1)
            print_elems_recursive(elem2)

#
# Utility functions for tst and scraper
#
def get_jsonlds(page_num):
    """ Fetches a list of .jsonld file URLs on the specified page. """
    r = requests.get(f"{NANOPUB_URL}/nanopubs.html?page={page_num}")
    soup = BeautifulSoup(r.text, 'html.parser')
    results = soup.findAll("a", text="jsonld", attrs={"type": "application/ld+json"})
    file_names = [x.attrs["href"] for x in results]
    return file_names

def ema_url_from_jsonld(jsonld_filename):
    """ Takes a .jsonld filename and extracts the full EMA request URL. """
    nanopub = jsonld.load_document(f"{NANOPUB_URL}/{jsonld_filename}")
    for graph in nanopub["document"]:
        for item in graph["@graph"]:
            if W3C_HAS_SRC in item:
                return item[W3C_HAS_SRC][0]['@id']
    return None

def ema_exps_from_page(page_num):
    """ Constructs an EmaExp for every nanopub on the page. """
    file_names = get_jsonlds(page_num)
    ema_exps = []
    for file_name in file_names:
        ema_url = ema_url_from_jsonld(file_name)
        ema_exps.append(EmaExp(*ema_url.split("/")[:-3]))
    return ema_exps

def print_elems_recursive(elem, i=0):
    print(" "*i, elem)
    for child in elem:
        print_elems_recursive(child, i+4)

environment.set('autoDownload', 'allow')

if __name__ == '__main__' and len(sys.argv) == 2 and sys.argv[1] == "scrape":
    print(os.getcwd())
    if os.path.basename(os.getcwd()) == 'emaMXL':
        os.chdir('tst')

    if os.path.basename(os.getcwd()) != 'tst':
        print("\"scraper.py scrape\" should be run from within the emaMXL root folder or emaMXL/tst.")
        exit()

    if not os.path.exists("data"):
        os.mkdir('data')
        os.mkdir('data/selections')
        os.mkdir('data/scores')
    for p in range(1, LAST_PAGE+1):
        scrape_page_nanopubs(p)


class SRMIST:
    def __init__(self, school, dept1, dept2, dept3, dept4):
        self.school = school
        self.dept1 = dept1
        self.dept2 = dept2
        self.dept3 = dept3
        self.dept4 = dept4

    def print(self):
        print('The departments are:', self.dept1, self.dept2, self.dept3, self.dept4, 'in', self.school)

    def print_new(self, specialisation):
        self.specialisation = specialisation
        print('The departments are:', self.dept3, self.dept4, 'in', self.school,
              'with specialisation', self.specialisation)

obj = SRMIST('SRM', 'CSE', 'ECE', 'EEE', 'ME')

obj.print()
print("Deleting dept1 and dept2 ...")
del obj.dept1
del obj.dept2
obj.print_new('CORE')


import math as math
import random as rand
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np

def f(x1, x2, x3, x4, x5):
        return math.pow(x1, 7/11.0) + math.pow(x2, 8/11.0) + math.pow(x3, 9/11.0) + math.pow(x4, 10/11.0) + math.sin(x5)

samples = 1000000
results = []
for i in range(samples):
        a = rand.random()
        b = rand.random()
        c = rand.random()

        d = rand.random()
        e_range = math.sqrt(1-d*d)

        e = rand.uniform(0,e_range)

        dice = rand.random()

        if dice < 0.5:
                y = f(a, b, c, d, e)
        else:
                y = f(a, b, c, e, d)
        results.append(y)

area_approx = 0
for number in results:
        area_approx += number

print(area_approx/samples*3.14/4)

sns.distplot(results, hist=False, kde=True,
             kde_kws={'linewidth': 3})
plt.show()


import numpy as np

import logging
logging.basicConfig(format='[%(asctime)s]-[%(name)-1s-%(levelname)2s]: %(message)s')
_logger = logging.getLogger(__name__)
_logger.setLevel(logging.DEBUG)

def gaussian_pdf(x, mu, sigma):
    # x vector should be in column, but numpy treat 1-d array as vector so can save the extra step
    n = len(x)
    return (2*np.pi)**(-n/2) * (np.linalg.det(sigma) ** (-1/2) ) * np.exp(-1/2*((x-mu).T@np.linalg.inv(sigma)@(x-mu)))

def component_pdfs(x, mus, sigmas):
    """
    The component pdf p_k(x; mu_k, sigma_k)
        :param mus: Kxn array (n be the dimension of x vector and K be the num of components), mean of k n-dim gaussian distr.
        :param sigmas: Knxn array covariance of k n-dim gaussian distr.
        :return: K-dim array contain probability of each component pdf
    """
    n_components = mus.shape[0]
    return np.array([gaussian_pdf(x, mus[k,:], sigmas[k, :, :]) for k in range(n_components)])

def likelihood_function(X, taus, mus, sigmas):
    """
    The component pdf p_k(x; mu_k, sigma_k)
        :param taus: K-dim array contains the weight (or prior of hidden var) of each gaussian component
        :param mus: Kxn array (n be the dimension of x vector and K be the num of components), mean of k n-dim gaussian distr.
        :param sigmas: Knxn array covariance of k n-dim gaussian distr.
        :return: numeric between 0 and 1, the likelihood function value
    """
    N = X.shape[0] # number of data points
    get_component_prob = lambda x: component_pdfs(x, mus, sigmas)
    T = np.apply_along_axis(arr=X, func1d=get_component_prob, axis=1) # gaussian component probabilities in row format (NxK)
    taus_rep = np.tile(taus, reps=(N, 1)) # repeat tau along N-axis so elementwise product can work

    return np.sum(T*taus_rep, axis=1)

def e_step(X, taus, mus, sigmas):
    """
        E step of the EM algorithm, caculates the posterior T_{k, i}=P(z_i=k|x_i)
        it returns T_{k,i} in the form of a KxN T matrix where each element is T_{k, i}
        :param X: Nxn matrix represents N number of n-dim data points
        :param taus: K-dim vector, the weight of each component, or the prior of the hidden stats z
        :param mus: Kxn matrix (n be the dimension of x vector and K be the num of components), mean of k n-dim gaussian distr.
        :param sigmas: Kxnxn matrix covariance of k n-dim gaussian distr.
        :return: T_{k,i} in the form of a KxN T matrix where each element is T_{k, i}
    """
    K, N = mus.shape[0], X.shape[0] # dimensions, K: num of hidden component, N: number of data points
    get_component_prob = lambda x: component_pdfs(x, mus, sigmas)
    T = np.apply_along_axis(arr=X, func1d=get_component_prob, axis=1) # gaussian component probabilities in row format (NxK)
    taus_rep = np.tile(taus, reps=(N, 1)) # repeat tau along N-axis so elementwise product can work

    norm_const = np.sum(T*taus_rep, axis=1) # the normalisation factor \sum_{k=1}^K p_k * tau_k， and is currently estimated likelihood
    norm_const_rep = np.tile(norm_const, reps=(K, 1)).T # repeat normalisation constant along K-axis

    T = T*taus_rep/norm_const_rep # calculate the posterior
    return T.T #return the transposed matrix so that the index is matched

def m_step(X, T):
    """
        M step of the EM algorithm, caculates the MLE of taus, mus and sigmas
        :param X: Nxn matrix, the dataset, N number of n-dim data points
        :param T: KxN matrix, the T matrix is the posterior matrix where the i, j th component is the T_{k, i}
        :return: a 3-tuple:
            - taus: K-dim array, the estimated prior probability for each hidden variable z
            - mus: Kxn matrix, the estimated mean of the n-dim gaussian component, for each of the k component
            - sigmas: Kxnxn matrix, the covariance matrix of the n-dim gaussian component, for each of the k component
    """
    def get_sigma(X, muk, Tk):
        """
            function that calculate the covariance of the k-th component
            :param muk: n-dim vector, the k-th component's mean
            :param Tk: N-dim vector, the k-th component posterior of hidden state z_i, for each x_i
        """
        X_centred = X - muk
        X_weighted = X_centred * np.tile(Tk, reps=(X.shape[1],1)).T # repeat Tk in N-direction to match X's shape and weigh it
        return X_weighted.T@X_centred/np.sum(Tk) # weighted and centred are exchangable: we only need to weigh it by T_k once

    N, n = X.shape #  N: number of data points, n: dimension of the data point
    K = T.shape[0] # num of hidden component
    T_sum = np.sum(T, axis=1) # caculate the common term sum of T_{k, i} over all i, this is a k-dim vector

    taus = T_sum / N # average over i  for T_{k, i} gives MLE for all tau_k

    T_sum_rep = np.tile(T_sum, reps=(n, 1)).T # repeat T_sum n times in column
    mus = T@X/T_sum_rep # T@X gives a Kxn matrix with it's k, i th component be \sum_{i=1}^NT_{k, i}x_i then each row is divided by T_sum, gives MLE for all mu_k

    sigmas = np.array([get_sigma(X, mus[k, :], T[k, :]) for k in range(K)])
    return taus, mus, sigmas

class GaussianMixture():
    def __init__(self, n_hidden=2, max_iter=100, seed=None):
        """
            :param n_hidden: number of hidden variables (or the number of components)
            :param max_iter: maximum EM iteration allowed, default to 100
            :param seed: the random seed for initialisation
        """
        self.n_hidden = n_hidden
        self.max_iter = max_iter
        self.seed = seed
        self.taus, self.mus, self.sigmas = None, None, None

    def fit(self, X):
        """
            :param X: Nxn matrix (N be the num of data points and n be the dimension of each data point)
        """
        n_hid = self.n_hidden
        n_var = X.shape[1]

        np.random.seed(self.seed) # setup seed, if None means no seed
        mus = np.random.randn(n_hid, n_var)*10 # initialise means of k components
        np.random.seed(self.seed)
        # initialise  sigmas of k components with identity
        sigmas = np.array([np.eye(n_var) for _ in range(n_hid)])
        taus = np.ones(n_hid)/n_hid # assume uninformative prior

        for i in range(self.max_iter):
            _logger.debug(f"iter: {i}, objective: {np.sum(np.log(likelihood_function(X, taus, mus, sigmas)))}") # only show this in debug mode to reduce unnecessary evaluation
            T = e_step(X, taus, mus, sigmas)
            sigmas_prev = sigmas
            taus, mus, sigmas = m_step(X, T)
            if np.min(abs(sigmas - sigmas_prev) < 0.1):
                _logger.debug(f"break after iteration {i+1}")
                break
        self.taus, self.mus, self.sigmas = taus, mus, sigmas
        return self

    def predict_proba(self, X):
        T = e_step(X, self.taus, self.mus, self.sigmas)
        return T.T # transpose, so it's 1st dimension matches the X's dimension N.

    def predict(self, X):
        T = e_step(X, self.taus, self.mus, self.sigmas)
        return np.argmax(T.T, axis=1)

if __name__ == "__main__":

    # generate a dataset
    mean1, mean2 = np.array([0, 0]), np.array([10, 20])
    sigma1, sigma2 = np.array([[1, 0], [0, 1]]), np.array([[5, -5], [-5, 10]])

    np.random.seed(42)
    X1 = np.random.multivariate_normal(mean1, sigma1, 1000)
    np.random.seed(42)
    X2 = np.random.multivariate_normal(mean2, sigma2, 200)
    X = np.vstack([X1, X2])

    print("="*20, "data info", "="*20)
    print(f"cluster 1: mean={np.mean(X1, axis=0)}\nsigma={np.cov(X1.T)}")
    print('-'*8)
    print(f"cluster 2: mean={np.mean(X2, axis=0)}\nsigma={np.cov(X2.T)}")
    print("="*50)
    print("\n\n")

    n_components = 2
    gmm = GaussianMixture(n_hidden=n_components, seed=0)
    print("="*20, f"fitting Gaussian mixture model with {n_components} components", "="*20)
    gmm.fit(X)

    print(f"{'='*20}finished fitting{'='*20}")
    print(f"{'-'*5}estimated taus:\n{gmm.taus}")
    print(f"{'-'*5}estimated mus:\n{gmm.mus}")
    print(f"{'-'*5}estimated sigmas:\n{gmm.sigmas}")

    print("\n\n")
    print(f"{'='*20}test predictive power{'='*20}")
    # construct a new dataset for test, but with the same distribution
    np.random.seed(0)
    X1 = np.random.multivariate_normal(mean1, sigma1, 1000)
    np.random.seed(0)
    X2 = np.random.multivariate_normal(mean2, sigma2, 200)
    X_test = np.vstack([X1, X2])
    y_true = np.hstack([np.zeros(1000), np.ones(200)])

    print("-"*20, "test data info", "-"*20)
    print(f"cluster 1: mean={np.mean(X1, axis=0)}\nsigma={np.cov(X1.T)}")
    print('-'*8)
    print(f"cluster 2: mean={np.mean(X2, axis=0)}\nsigma={np.cov(X2.T)}")
    print("-"*8)

    # predict use the model trained
    y_pred = gmm.predict(X_test)
    accuracy = np.mean(y_true==y_pred)
    print(f"accuracy of the model on test is {accuracy}")


from random import random

import cv2

from config import USE_OPTIMAL_AI

class Paddle:
    def __init__(self, left, top, right, bottom):
        self.left = left
        self.right = right
        self.top = top
        self.bottom = bottom

        self._ai_direction = 0

    def collides_with_ball(self, ball):
        x_collides = (ball.x + 1 == self.left) or (ball.x == self.right)
        y_collides = (ball.y >= self.top) and (ball.y < self.bottom)
        return x_collides and y_collides

    def make_ai_move(self, board, ball):
        follow_ball = ((self.left < ball.x / 2) and (ball.v_x < 0)) \
                      or ((self.left > ball.x / 2) and (ball.v_x > 0))

        if not USE_OPTIMAL_AI and random() < 0.25:
            follow_ball = False

        if follow_ball:
            self.make_move_towards(board, ball.y)
        else:
            if USE_OPTIMAL_AI:
                self.make_move_towards(board, board.height / 2)
            else:
                direction_change = 0

                if random() < 0.5:
                    if self._ai_direction == 0:
                        direction_change = 1 if random() < 0.5 else -1
                    else:
                        direction_change = -self._ai_direction

                self._ai_direction += direction_change

                self.make_move(board, self._ai_direction)

    def make_move_towards(self, board, target_y):
        y_middle = (self.top + self.bottom) / 2

        if target_y > y_middle:
            velocity = 1
        elif target_y < y_middle:
            velocity = -1
        else:
            velocity = 0

        self.make_move(board, velocity)

    def make_move(self, board, y_diff):
        if self.top + y_diff < 0:
            return

        if self.bottom + y_diff > board.height:
            return

        self.top += y_diff
        self.bottom += y_diff

    def draw(self, frame, ratio, x_shift, y_shift):
        top_left = (int(self.left * ratio + x_shift), int(self.top * ratio + y_shift))
        bottom_right = (int(self.right * ratio + x_shift), int(self.bottom * ratio + y_shift))
        cv2.rectangle(frame, top_left, bottom_right, (255, 255, 255), -1)


import re
import scrapy
from scrapy.spiders import CrawlSpider, Rule
from scrapy.linkextractors import LinkExtractor
from ef_crawler.items import EfCrawlerItem

class EvfeSpider(CrawlSpider):
    name = 'evfe'
    allowed_domains = ["gep.or.kr"]
    start_urls = ['http://www.gep.or.kr/domestic-exhibition/exhibitions?pageIndex=1']

    rules = [
            Rule(LinkExtractor(allow=r'/exhibitions\?pageIndex=[1-25]'), callback='parse_item', follow = True)
    ]

    def parse_item(self, response):
        name = response.xpath('//*[@id="print_container"]/form[1]/div[5]/table/tbody/tr/td[2]/a/text()').extract()
        location = response.xpath('//*[@id="print_container"]/form[1]/div[5]/table/tbody/tr/td[4]/text()').extract()
        date = response.xpath('//*[@id="print_container"]/form[1]/div[5]/table/tbody/tr/td[3]/p/text()').extract()

        for item in zip(name, date, location):
            temp = item[2].strip()
            start_day, end_day = temp.split(' ~ ')

            x = start_day.split('-')
            if(int(x[0]) < 2019):
               continue
            scraped_info = {
                'name' : item[0].strip(),
                'location' : item[1].strip(),
                'start_day' : start_day,
                'end_day' : end_day,
            }
            yield scraped_info


#!/usr/bin/env python3

# Created by Malcolm Tompkins
# Created on May 29, 2021
# Calculates the volume of a cylinder

import math

def volume_cylinder(radius, height):
    volume = (radius * radius) * height * math.pi
    return volume

def main():
    user_input1 = (input("Enter radius of the cylinder (mm): "))
    user_input2 = (input("Enter height of cylinder (mm): "))
    try:
        user_radius = int(user_input1)
        try:
            user_height = int(user_input2)
            volume_cylinder(user_radius, user_height)
            final_volume = volume_cylinder(user_radius, user_height)
            print("{:.2f} mm² is the volume of your cylinder".format
                  (final_volume))
        except Exception:
            print("{} is not a dimension".format(user_input2))
    except Exception:
        print("{} is not a dimension".format(user_input1))
    finally:
        print("Done.")

if __name__ == "__main__":
    main()


import requests
import asyncio
from concurrent.futures import ThreadPoolExecutor
import random
import urllib3
import pdb
import sys
import os

user_agents = [
	'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.122 Safari/537.3', # Chrome, Windows
	'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.122 Safari/537.36', # Chrome, macOS
	'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.122 Safari/537.36', # Chrome, Linux
	'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:54.0) Gecko/20100101 Firefox/73.0', # Firefox, Windows
	'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.13; rv:61.0) Gecko/20100101 Firefox/73.0', # Firefox, macOS
	'Mozilla/5.0 (X11; Linux i586; rv:31.0) Gecko/20100101 Firefox/73.0', # Firefox, Linux
	'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.122 Safari/537.36 Edg/80.0.361.50' # Edge, Windows
	'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0 Safari/605.1.15' # Safari, macOS
]

url_list = []
urllib3.disable_warnings()
timeout = 3

async def process_urls():

	with ThreadPoolExecutor(max_workers=10) as executor:
		with requests.Session() as session:

			loop = asyncio.get_event_loop()

			tasks = [
				loop.run_in_executor(
					executor,
					fetch,
					*(session, url) # Allows us to pass in multiple arguments to `fetch`
				)
				for url in url_list
			]

			for response in await asyncio.gather(*tasks):
				pass

def fetch(session, url):

	try:

		response = session.get(url, timeout=timeout, headers={'User-agent' : f'{random.choice(user_agents)}'}, verify=False)

		if response.status_code == 200:

			if response.text != 'null':

				with open(f"{FOLDER_NAME}/{url.split('//')[1].split('.')[0]}", "w", encoding='utf8') as file:
					file.write(response.text)
					print(url)
			else:
				print(f"{url}: 200 OK, NULL response.")

	except Exception as e:
		print(f"Exception! {e}")

def build_list():

	for line in open(sys.argv[1], "r").readlines():
		url_list.append(f"https://{line.rstrip()}.firebaseio.com/.json")

FOLDER_NAME = "firebase_dump"

if len(sys.argv) < 2:
	print(f"Error: Specify a wordlist. \nUsage: \t {sys.argv[0]} <wordlist>")
	sys.exit(0)

if not os.path.isdir(FOLDER_NAME):
	try:
		os.makedirs(FOLDER_NAME)
	except Exception:
		print(f"Error creating folder '{FOLDER_NAME}'.")
		sys.exit(0)

build_list()

# async loop process_urls()
loop = asyncio.get_event_loop()
future = asyncio.ensure_future(process_urls())
loop.run_until_complete(future)


class Thing:
    __id = 0

    def __new__(cls, *args, **kwargs):
        Thing.__id += 1
        return object.__new__(cls)

    def __init__(self, name: str, price: float):
        self.id = Thing.__id
        # self.id = hash(self)
        self.name = name
        self.price = price
        self.weight = None
        self.dims = None
        self.memory = None
        self.frm = None

    def get_data(self):
        return self.name, self.price, self.weight, self.dims, self.memory, self.frm

class Table(Thing):
    def __init__(self, name, price, weight, dims):
        super().__init__(name, price)
        self.weight = weight
        self.dims = dims

class ElBook(Thing):
    def __init__(self, name, price, memory, frm):
        super().__init__(name, price)
        self.memory = memory
        self.frm = frm

table = Table("Круглый", 1024, 812.55, (700, 750, 700))
book = ElBook("Python ООП", 2000, 2048, 'pdf')
print(*table.get_data())
print(*book.get_data())

print(table.__dict__)
print(book.__dict__)


#!/usr/bin/env python
import random
import sys

class MontyPlayer(object):

    def __init__(self,door_count,switch=False):
        self.switch=switch
        #per-player list of doors
        self.doors=[]

        for i in range(0,door_count):
            self.doors.append(MontyDoor())
        winning_door=self.doors[random.randint(0,len(self.doors)-1)]
        winning_door.set_winning()

        self.choice=self._choose_door(self.doors)

        #player choice not available for revealing or choosing again
        self.doors.remove(self.choice)

    def wins_prize(self):
        if self.choice.outcome=="prize":
            return True
        else:
            return False

    def _choose_door(self,doors):
        return doors[random.randint(0,len(doors)-1)]

    def door_revealed(self,door):
        if not door in self.doors:
            raise Exception("Invalid door number.")
        self.doors.remove(door)
        if self.switch:
            #With three doors total, at this point there should be only one door remaining, which we will now choose.
            self.choice=self._choose_door(self.doors)
            self.doors.remove(self.choice)

class MontyDoor(object):

    def __init__(self):
        self.outcome="goat"

    def set_winning(self):
        self.outcome="prize"

class MontyGame(object):
    DOOR_COUNT=3

    def __init__(self,trials):
        self.switch_win_tally=0
        self.non_switch_win_tally=0
        for i in range(0,trials):
            players=[]
            switch_player=MontyPlayer(self.DOOR_COUNT,switch=True)
            players.append(switch_player)
            non_switch_player=MontyPlayer(self.DOOR_COUNT,switch=False)
            players.append(non_switch_player)
            self._run_trial(players)
            if switch_player.wins_prize():
                self.switch_win_tally+=1
            if non_switch_player.wins_prize():
                self.non_switch_win_tally+=1

    def _run_trial(self,players):
        for p in players:
            for door in p.doors:
                if door.outcome=="goat":
                    p.door_revealed(door)
                    break

def main(trials):
    game=MontyGame(trials)
    print("Trials: %d" % trials)
    print("Switching player wins: %d" % game.switch_win_tally)
    print("Non-switching player wins: %d" % game.non_switch_win_tally)

if __name__ == '__main__':
    try:
        trials=int(sys.argv[1])
    except:
        trials=1000
    main(trials)


# produit des digits d'un nombre

def persistence(n):
    c = 0
    while len(str(n)) > 1:
        p = 1
        for i in str(n):
            p *= int(i)
        print(p)
        n = str(p)
        c += 1
    return c


from datetime import datetime

from django.shortcuts import get_object_or_404
from django.utils.datetime_safe import datetime as d_datetime

from rest_framework import viewsets, permissions, response, status, filters, pagination
from rest_framework_simplejwt.views import TokenObtainPairView

from .models import Subject, Topic, SubTopic, Department, Bookmark, WatchHistory
from .serializer import SerializedSubjects, SerializedSubTopic, WatchHistorySerializer

class DashboardView(viewsets.ViewSet):
	permission_classes = [permissions.IsAuthenticated]

	def list(self, request):
		bookmark = Bookmark.objects.filter(user=request.user)

		my_subjects = 0
		completed_subjects = 0
		bookmarks = bookmark[0].topics.count() if bookmark.count() else 0
		questions_completed = 0
		activities = request.user.accountactivity_set.values()[:5]
		watch_list = request.user.watchhistory_set.values('id', 'subtopic__name', 'subtopic__slug')[:5]

		data = dict(
			my_subjects=my_subjects,
			completed_subjects=completed_subjects,
			bookmarks=bookmarks,
			questions_completed=questions_completed,
			watch_list=watch_list,
			activities=activities
		)

		return response.Response(data)

class SubjectsView(viewsets.ModelViewSet):
	permission_classes = [permissions.AllowAny]
	serializer_class = SerializedSubjects
	queryset = Subject.objects.all()
	filter_backends = [filters.SearchFilter]
	search_fields = ['department__slug']
	pagination_class = pagination.LimitOffsetPagination

	def retrieve(self, request, pk):

		queryset = get_object_or_404(Subject, slug=pk)
		serializer = SerializedSubjects(queryset).data

		"""
		Get three related subjects
		"""
		related_subjects_queryset = Subject.objects.filter(department=queryset.department).exclude(id=queryset.id)[:3]
		related_subject_serializer = SerializedSubjects(related_subjects_queryset, many=True)
		serializer['related_subjects'] = related_subject_serializer.data

		user_bookmarks = []
		user_watch_history = []

		if request.user.is_authenticated:
			bookmark_query = Bookmark.objects.filter(user=request.user)
			watchlist_query = request.user.watchhistory_set.all().only('id')

			if bookmark_query:
				user_bookmarks = [x[0] for x in bookmark_query[0].topics.values_list('id')]
			if watchlist_query:
				user_watch_history = [x.subtopic.id for x in watchlist_query]

		serializer['topics'] = tuple(
			{
				**topic,
				'sub_topics': tuple(
					dict(
						id=subtopic.id,
						name=subtopic.name,
						slug=subtopic.slug,
						ordering=subtopic.ordering,
						lock=subtopic.lock,
						bookmarked=subtopic.id in user_bookmarks,
						watched=subtopic.id in user_watch_history,
					) for subtopic in
					SubTopic.objects.filter(topic__name=topic["name"]).order_by('ordering')
				)
			} for topic in
			queryset.topic_set.values("id", "name", "ordering").order_by('ordering')
		)

		return response.Response(serializer, status=status.HTTP_200_OK)

	def update(self, request):
		return response.Response(status=status.HTTP_405_METHOD_NOT_ALLOWED)

	def destroy(self, request, *args, **kwargs):
		return response.Response(status=status.HTTP_405_METHOD_NOT_ALLOWED)

class DepartmentView(viewsets.ViewSet):
	permission_classes = [permissions.AllowAny]

	def list(self, request):
		queryset = Department.objects.values("id", "name", "slug")
		return response.Response(queryset, status=status.HTTP_200_OK)

class SubTopicView(viewsets.ViewSet):
	permission_classes = [permissions.IsAuthenticated]

	def retrieve(self, request, pk):
		data = get_object_or_404(SubTopic, slug=pk)

		if request.user.subscriptionplan.subscription_status in ['free-trial', 'paid']:
			if datetime.now().timetuple() <= request.user.subscriptionplan.expiry_date.timetuple():
				WatchHistory.objects.get_or_create(user=request.user, subtopic=data)
				serializer = SerializedSubTopic(data)
				return response.Response(serializer.data)
			return response.Response({'msg': "Current Subscription has Expired"}, status=status.HTTP_401_UNAUTHORIZED)

		return response.Response({'msg': "Subscribe to watch video"}, status=status.HTTP_401_UNAUTHORIZED)

class BookmarkView(viewsets.ViewSet):
	permission_classes = [permissions.IsAuthenticated]

	def list(self, request):
		data = get_object_or_404(Bookmark, user=request.user)
		data = data.topics.all()
		data = [
			dict(
				id=subtopic.pk, subject=subtopic.topic.subject.name, subject_slug=subtopic.topic.subject.slug,
				subtopic=subtopic.name, completed='40%', tumbnail=subtopic.topic_thumbnail.url,
				slug=subtopic.slug,
			)
			for subtopic in data
		]

		return response.Response(data)

	def create(self, request):
		subtopic = get_object_or_404(SubTopic, pk=request.data['pk'])
		Bookmark.add_bookmark(request.user, subtopic)

		return response.Response(status=status.HTTP_201_CREATED)

	def destroy(self, request, pk):
		subtopic = get_object_or_404(SubTopic, pk=pk)
		Bookmark.remove_bookmark(request.user, subtopic)

		return response.Response(status=status.HTTP_200_OK)

class WatchHistoryView(viewsets.ModelViewSet):
	permission_classes = [permissions.IsAuthenticated]
	queryset = WatchHistory.objects.all()
	serializer_class = WatchHistorySerializer
	pagination_class = pagination.LimitOffsetPagination

	def get_queryset(self):
		queryset = WatchHistory.objects.filter(user=self.request.user)
		return queryset


import tensorflow as tf
import os

def get_path_label_list(path_label_file):
  path_list = []
  label_list = []

  with open(path_label_file, 'r') as fr:
    lines = fr.readlines()

    for line in lines:
      line = line.strip()
      [path, label] = line.split(' ')
      label = int(label)

      # check path exist or not
      if not os.path.isfile(path):
        continue

      path_list.append(path)
      label_list.append(label)

  return path_list, label_list

def build_input(path_list, label_list, image_H, image_W, batch_size, mode):
  """Build [DATASET_NAME] image and labels.

  Args:
    path_list:
    label_list:
    image_H:
    image_W:
    batch_size:
    mode: Either 'train' or 'eval'.
  Returns:
    images: Batches of images. [batch_size, image_size, image_size, 3]
    labels: Batches of labels. [batch_size, num_classes]
  Raises:
    ValueError: when the specified dataset is not supported.
  """
  path_tensor_list = tf.cast(path_list, tf.string)
  label_tensor_list = tf.cast(label_list, tf.int32)
  depth = 3 # image color channel

  # make an input queue
  input_queue = tf.train.slice_input_producer([path_tensor_list, label_tensor_list])
  image_contents = tf.read_file(input_queue[0])
  image = tf.image.decode_jpeg(image_contents, channels=3)
  label = tf.reshape(input_queue[1], [1])

  if mode == 'train':
    #=====
    # Data augmentation
    #=====
    # image = tf.image.resize_image_with_crop_or_pad(
        # image, image_H+4, image_W+4)
    # image = tf.random_crop(image, [image_H, image_W, 3])
    # image = tf.image.random_flip_left_right(image)
    # Brightness/saturation/constrast provides small gains .2%~.5% on cifar.
    # image = tf.image.random_brightness(image, max_delta=63. / 255.)
    # image = tf.image.random_saturation(image, lower=0.5, upper=1.5)
    # image = tf.image.random_contrast(image, lower=0.2, upper=1.8)
    image = tf.image.per_image_standardization(image)

    example_queue = tf.RandomShuffleQueue(
        capacity=16 * batch_size,
        min_after_dequeue=8 * batch_size,
        dtypes=[tf.float32, tf.int32],
        shapes=[[image_H, image_W, depth], [1]])
    num_threads = 16
  else:
    # image = tf.image.resize_image_with_crop_or_pad(
        # image, image_H, image_W)
    image = tf.image.per_image_standardization(image)

    example_queue = tf.FIFOQueue(
        3 * batch_size,
        dtypes=[tf.float32, tf.int32],
        shapes=[[image_H, image_W, depth], [1]])
    num_threads = 1

  example_enqueue_op = example_queue.enqueue([image, label])
  tf.train.add_queue_runner(tf.train.queue_runner.QueueRunner(
      example_queue, [example_enqueue_op] * num_threads))

  # Read 'batch' labels + images from the example queue.
  images, labels = example_queue.dequeue_many(batch_size)
  labels = tf.reshape(labels, [batch_size])

  assert len(images.get_shape()) == 4
  assert images.get_shape()[0] == batch_size
  assert images.get_shape()[-1] == 3
  assert len(labels.get_shape()) == 1
  assert labels.get_shape()[0] == batch_size

  # Display the training images in the visualizer.
  tf.summary.image('images', images)
  return images, labels