import neurals.data_generation_config as dgc
import neurals.distancefield_utils as df
import model.param as model_param

import copy
import numpy as np
import open3d as o3d
import os
from scipy.spatial.transform import Rotation as scipy_rot
import torch.utils.data

# Each file correspond to a pointcloud
class SmallDataset(torch.utils.data.dataset.Dataset):
    def __init__(self, seed_folder="seeds",
                       point_clouds=["pose_0_pcd", "pose_1_pcd", "pose_2_pcd", "pose_3_pcd", "pose_4_pcd"],
                       has_negative_grasp=False,
                       use_scale = False):
        self.point_clouds = []
        self.point_normals = []
        pose_index = []
        for point_cloud in point_clouds:
            pose_index.append(int(point_cloud[5:7]))
            pcd = o3d.io.read_point_cloud(f"data/{seed_folder}/pointclouds/{point_cloud}.ply")
            pcd_np = np.asarray(pcd.points)
            normal_np = np.asarray(pcd.normals)
            if pcd_np.shape[0] < 1024:
                print(pcd_np.shape[0],point_cloud)
                assert(False)
            idx = np.random.choice(pcd_np.shape[0], 1024, replace=False)
            self.point_clouds.append(pcd_np[idx])
            self.point_normals.append(normal_np[idx])

        self.scales = []
        if os.path.isdir(f"data/{seed_folder}/scales") and use_scale:
            for idx in pose_index:
                scale = np.load(f"data/{seed_folder}/scales/pose_{idx:02}.npy")
                self.scales.append(scale)

        self.positive_grasps = []
        self.positive_pcd_mapping = []
        positive_grasp_files = os.listdir(f"data/{seed_folder}/grasps/")
        positive_grasp_files.sort(key=lambda x: int(x[5:7]))
        positive_files = []
        for positive_grasp_file in positive_grasp_files:
            if int(positive_grasp_file[5]) in pose_index:
                positive_files.append(positive_grasp_file)
        for i,positive_grasp_file in enumerate(positive_files):
            self.positive_grasps.append(np.load(f"data/{seed_folder}/grasps/{positive_grasp_file}")[:,:,:3])
            self.positive_pcd_mapping += [i] * len(self.positive_grasps[-1])

        if has_negative_grasp:
            self.negative_grasps = []
            self.negative_pcd_mapping = []
            negative_grasp_files = os.listdir(f"data/{seed_folder}/bad_grasps/")
            negative_grasp_files.sort(key=lambda x: int(x[5:7]))
            negative_files = []
            for negative_grasp_file in negative_grasp_files:
                if int(negative_grasp_file[5]) in pose_index:
                    negative_files.append(negative_grasp_file)
            for i, negative_grasp_file in enumerate(negative_files):
                self.negative_grasps.append(np.load(f"data/{seed_folder}/bad_grasps/{negative_grasp_file}")[:,:,:3])
                self.negative_pcd_mapping += [i] * len(self.negative_grasps[-1])

        if has_negative_grasp:
            self.grasps = np.concatenate(self.positive_grasps+self.negative_grasps, axis=0)
            self.pcd_mapping = np.array(self.positive_pcd_mapping+self.negative_pcd_mapping)
            self.labels = np.array([1]*len(self.positive_pcd_mapping)+[0]*len(self.negative_pcd_mapping))
        else:
            self.grasps = np.concatenate(self.positive_grasps, axis=0)
            self.pcd_mapping = np.array(self.positive_pcd_mapping)
            self.labels = np.array([1]*len(self.grasps))
        self.grasps = self.grasps.reshape(len(self.grasps),-1)
        print(self.grasps.shape)
        self.point_clouds = np.asarray(self.point_clouds)

    def __len__(self):
        return len(self.pcd_mapping)

    def __getitem__(self, idx):
        ans = {}
        ans["point_cloud"] = self.point_clouds[self.pcd_mapping[idx]]
        ans["point_normals"] = self.point_normals[self.pcd_mapping[idx]]
        ans["fingertip_pos"] = self.grasps[idx]
        ans["intrinsic_score"] = self.labels[idx] # Good or bad pointclouds
        ans["label"] = self.pcd_mapping[idx]
        if self.scales != []:
            ans["scale"] = self.scales[self.pcd_mapping[idx]]
        return ans

# Dataset for score function, add noise tensor in order to prevent overfitting
class ScoreDataset(torch.utils.data.dataset.Dataset):
    """
    Remark: To avoid overfitting between each round, we need to recreate dataset after each
    optimization iteration.
    """
    def __init__(self, score_file="score_data", noise_scale = 0.0, has_distance_field=False):
        self.noise_scale = noise_scale
        self.has_distance_field = has_distance_field
        data = np.load(f"data/score_function_data/{score_file}.npz")
        self.scores = data["scores"]
        self.point_clouds = data["point_clouds"] + np.random.normal(size=data["point_clouds"].shape,
                                                                       scale=self.noise_scale)
        self.point_clouds = data["point_clouds"]
        self.conditions = data["conditions"]
        if has_distance_field:
            # Need to create different environments for computing distance field
            self.point_cloud_labels = data["point_cloud_labels"]
            self.envs = [env() for env in df.env_lists]
            self.load_env_configs()
            self.point_cloud_dfs = []
            for i, pcd in enumerate(self.point_clouds):
                self.point_cloud_dfs.append(self.compute_distance_field(pcd, int(self.point_cloud_labels[i])))
            self.point_cloud_dfs = np.asarray(self.point_cloud_dfs)

    def load_env_configs(self):
        self.pcd_to_env = []
        self.pcd_pos = []
        self.pcd_rotation = []
        filelist = os.listdir("data/seeds_scale/obj_pose")
        filelist.sort(key = lambda x: int(x[5]))
        for file in filelist:
            z = np.load(f"data/seeds_scale/obj_pose/{file}")
            self.pcd_to_env.append(int(z['env_id']))
            self.pcd_pos.append(z['trans'])
            self.pcd_rotation.append(z['rot'])

    def __len__(self):
        return len(self.scores)

    def compute_distance_field(self, pcd, pcd_id):
        """
        Assume pcd is a numpy array
        """
        pcd_o3d = o3d.geometry.PointCloud(o3d.utility.Vector3dVector(pcd))
        env_id = self.pcd_to_env[pcd_id]
        dist_env = self.envs[env_id]
        pcd_o3d.rotate(self.pcd_rotation[pcd_id])
        pcd_o3d.translate(self.pcd_pos[pcd_id])
        points = np.asarray(pcd_o3d.points)
        dist_field = dist_env.get_points_distance(points)
        return dist_field.numpy()

    def __getitem__(self, idx):
        ans = {}
        ans["point_cloud"] = self.point_clouds[idx] # np.random.normal(size=self.point_clouds[idx].shape, scale=self.noise_scale)
        ans["condition"] = self.conditions[idx]
        ans["score"] = self.scores[idx]
        if self.has_distance_field:
            ans["point_cloud_df"] = self.point_cloud_dfs[idx]
        return ans


from time import time
import pennylane as qml
from scipy.optimize import minimize
import networkx as nx
from .unitaries import (
    hypercube_mixer,
    phase_shift,
    circulant_mixer,
    diagonal_pauli_decompose,
    complete_eigenvalues,
)

def gen_graph(qubits, seed, prob = 0.25):
    # for smaller graphs, increase the probability of edge generation
    #prob = prob if prob * (qubits**2) > 1 else 2.0/qubits**2
    # ensure that there is at least one edge
    n_edges = 0
    s = seed
    while n_edges == 0:
        if type(s) is int:
            G = nx.fast_gnp_random_graph(qubits, p=prob, seed=s, directed=False)
            s += 1
        else:
            G = nx.fast_gnp_random_graph(qubits, p=prob, directed=False)
        n_edges = G.number_of_edges()
    return G

def maxcut_qualities(G, wires):
    """Return a Pauli-string representation of the maxcut cost-function.

    Parameters
    ----------
    G : networkx graph
        graph for which the maxcut problem is to be solved
    wires : list or range
        number of wires (qubits), must cover the number of graph vertices

    Returns
    -------
    Hamiltonian
        Pauli-string representation of the maxcut cost-function.
    """
    wires = list(wires)
    C = 0
    for i, j in G.edges:
        C += qml.Identity(wires) - qml.PauliZ(wires=i) @ qml.PauliZ(wires=j)
    return -0.5 * C

def qaoa_hypercube_maxcut_evolution(device, depth, n_expvals, seed):
    """State-evolution benchmark function for the QAOA with a hypercube (transverse-field) mixing operator.

    State evolution based on Hamiltonian simulation.

    Parameters
    ----------
    device :
        an initialised Pennylane device
    depth : int
        ansatz circuit depth
    n_expvals : int
        number of times to compute the state evolution and return the
        expectation value with random variational parameters

    Returns
    -------
    (float, float)
        the last computed expectation value and the time taken to evaluate
        n_expvals repeats of the circuit
    """

    qubits = len(device.wires)

    G = gen_graph(qubits, seed)

    wires = range(qubits)
    qualities_H = maxcut_qualities(G, wires)

    @qml.qnode(device)
    def circuit(gammas_ts):
        for wire in wires:
            qml.Hadamard(wires=wire)
        for gamma, t in zip(*qml.numpy.split(gammas_ts, 2)):
            phase_shift(gamma, wires, qualities_H)
            hypercube_mixer(t, wires)
        return qml.expval(qualities_H)

    start = time()
    rng = qml.numpy.random.default_rng(seed)
    gammas_ts = rng.uniform(size=2 * depth, low=0, high=2 * qml.numpy.pi)
    for _ in range(n_expvals):
        expval = circuit(gammas_ts)
    specs = qml.specs(circuit)
    gate_sizes = specs(gammas_ts)['resources'].gate_sizes
    end = time()
    specs = qml.specs(circuit)
    gate_sizes = specs(gammas_ts)['resources'].gate_sizes
    circuit_depth = specs(gammas_ts)['resources'].depth
    return float(expval), end - start, circuit_depth, gate_sizes[1], gate_sizes[2]

def qaoa_complete_maxcut_evolution(device, depth, n_expvals, seed):
    """State-evolution benchmark function for the QAOA with a complete-graph mixing operator.

    State evolution based on the QFT.

    Parameters
    ----------
    device :
        an initialised Pennylane device
    depth : int
        ansatz circuit depth
    n_expvals : int
        number of times to compute the state evolution and return the
        expectation value with random variational parameters

    Returns
    -------
    (float, float)
        the last computed expectation value and the time taken to evaluate
        n_expvals repeats of the circuit
    """

    qubits = len(device.wires)

    G = gen_graph(qubits, seed)

    wires = range(qubits)
    qualities_H = maxcut_qualities(G, wires)

    eigen_decomp = diagonal_pauli_decompose(complete_eigenvalues(2**qubits))

    @qml.qnode(device)
    def circuit(gammas_ts):
        for wire in wires:
            qml.Hadamard(wires=wire)
        for gamma, t in zip(*qml.numpy.split(gammas_ts, 2)):
            phase_shift(gamma, wires, qualities_H)
            circulant_mixer(t, wires, eigen_decomp)
        return qml.expval(qualities_H)

    start = time()
    rng = qml.numpy.random.default_rng(seed)
    gammas_ts = rng.uniform(size=2 * depth, low=0, high=2 * qml.numpy.pi)
    for _ in range(n_expvals):
        expval = circuit(gammas_ts)
    specs = qml.specs(circuit)
    gate_sizes = specs(gammas_ts)['resources'].gate_sizes
    end = time()
    specs = qml.specs(circuit)
    gate_sizes = specs(gammas_ts)['resources'].gate_sizes
    circuit_depth = specs(gammas_ts)['resources'].depth
    return float(expval), end - start, circuit_depth, gate_sizes[1], gate_sizes[2]

def main_args():

    import argparse

    parser = argparse.ArgumentParser(description = "Run a test QAOA circuit with random variational parameters.", epilog = "Simulation output: last expectation value, qubits, depth, repeats, backend, graph sparsity, simulation time")
    parser.add_argument("-q", "--qubits", default=4, type=int, dest='qubits', nargs='?', help = 'number of qubits, default = 4')
    parser.add_argument("-d", "--depth", default=2, type=int, dest='depth', nargs='?', help = 'number of ansatz iterations, default = 2')
    parser.add_argument("-r", "--repeats", default=1, type=int, dest='repeats', nargs='?', help = 'number of expectation value evaluations, default = 1')
    parser.add_argument("-b", "--backend", default="lightning.qubit", type=str, dest='backend', nargs='?', help = 'simulation backend, default = lightning.qubit')
    parser.add_argument("-o", "--options", default=[],  dest='options', nargs = '*', help = 'backend-specific keyword options for device creation')
    parser.add_argument("-s", "--seed", default=None, dest='seed', type = int, nargs = '?', help = 'seed random number generation')
    parser.add_argument("-p", "--plot", default=False, dest='plot', action = 'store_true', help = 'plot the cicuit, do not simulate')
    parser.add_argument("-g", "--graph", default=0.1, dest='graph', type=float, help = 'graph sparsity (0,1], default = 0.1')
    parser.add_argument("-c", "--circuit", default=None, dest='circuit', type=str,  help = 'if present, save the circuit to this path as a QASM file')

    return parser.parse_args()

if __name__ == "__main__":

    import sys

    args = main_args()

    if len(args.options) > 0:
        device_code = f"qml.device('{args.backend}', wires = {args.qubits}, {' ,'.join(args.options)})"
        device = eval(device_code)
    else:
        device = qml.device(args.backend, wires = args.qubits)

    G = gen_graph(args.qubits, args.seed, args.graph)

    wires = range(args.qubits)
    qualities_H = maxcut_qualities(G, wires)

    if not args.seed is None:
        rng = qml.numpy.random.default_rng(args.seed)
    else:
        rng = qml.numpy.random.default_rng()

    gammas_ts = rng.uniform(size = 2*args.depth)

    @qml.qnode(device)
    def circuit(gammas_ts):
        for wire in wires:
            qml.Hadamard(wires=wire)
        for gamma, t in zip(*qml.numpy.split(gammas_ts, 2)):
            phase_shift(gamma, wires, qualities_H)
            hypercube_mixer(t, wires)
        return qml.expval(qualities_H)

    if args.plot:
        drawer = qml.draw(circuit)
        print(drawer(gammas_ts))
        sys.exit(0)

    start = time()
    for _ in range(args.repeats):
        expval = circuit(gammas_ts)
    end = time()

    if not args.circuit is None:
        with open(args.circuit, 'w') as f:
            f.write(circuit.qtape.to_openqasm())

    print(f"{expval},{args.qubits},{args.depth},{args.repeats},{args.backend},{args.graph},{end - start}")


from concurrent.futures import ThreadPoolExecutor
from os.path import splitext
from PIL import Image, ImageFile
from tkinter.filedialog import *
import os.path

ImageFile.LOAD_TRUNCATED_IMAGES = True
files = askopenfilenames()

def convert_image(file):
    with Image.open(file) as im:
        output_path = splitext(file)[0] + '.jpg'
        if not os.path.exists(output_path):
            im = im.convert('RGB')
            im.save(output_path, "JPEG", quality=100)
        else:
            print(f'File {output_path} already exists.')

with ThreadPoolExecutor() as executor:
    executor.map(convert_image, files)


from msrest.service_client import ServiceClient
from msrest import Configuration, Serializer, Deserializer
from .version import VERSION
from .operations.int_model_operations import IntModelOperations
from . import models

class AutoRestIntegerTestServiceConfiguration(Configuration):
    """Configuration for AutoRestIntegerTestService
    Note that all parameters used to create this instance are saved as instance
    attributes.

    :param str base_url: Service URL
    """

    def __init__(
            self, base_url=None):

        if not base_url:
            base_url = 'http://localhost'

        super(AutoRestIntegerTestServiceConfiguration, self).__init__(base_url)

        self.add_user_agent('autorestintegertestservice/{}'.format(VERSION))

class AutoRestIntegerTestService(object):
    """Test Infrastructure for AutoRest

    :ivar config: Configuration for client.
    :vartype config: AutoRestIntegerTestServiceConfiguration

    :ivar int_model: IntModel operations
    :vartype int_model: fixtures.acceptancetestsbodyinteger.operations.IntModelOperations

    :param str base_url: Service URL
    """

    def __init__(
            self, base_url=None):

        self.config = AutoRestIntegerTestServiceConfiguration(base_url)
        self._client = ServiceClient(None, self.config)

        client_models = {k: v for k, v in models.__dict__.items() if isinstance(v, type)}
        self.api_version = '1.0.0'
        self._serialize = Serializer(client_models)
        self._deserialize = Deserializer(client_models)

        self.int_model = IntModelOperations(
            self._client, self.config, self._serialize, self._deserialize)


import mongoengine

ROOT_URLCONF = 'parkkeeper.urls'

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': ':memory:',
    }
}

MIDDLEWARE_CLASSES = [
    'django.middleware.common.CommonMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
]

TEMPLATE_CONTEXT_PROCESSORS = [
    'django.contrib.auth.context_processors.auth',
    'django.core.context_processors.debug',
    'django.core.context_processors.request',
]

INSTALLED_APPS = [
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'parkkeeper',
]

SECRET_KEY = "123"

TIME_ZONE = 'UTC'
USE_TZ = True

TEST_RUNNER = 'djutils.testrunner.TestRunnerWithMongo'

MONGODB = {
    'NAME': 'parkkeeper',
    'HOST': 'localhost',
}
mongoengine.connect(MONGODB['NAME'], tz_aware=True, host='mongodb://%s:27017/%s' % (MONGODB['HOST'], MONGODB['NAME']))

ZMQ_SERVER_ADDRESS = 'localhost'
ZMQ_WORKER_REGISTRATOR_PORT = 5548
ZMQ_EVENT_RECEIVER_PORT = 5549
ZMQ_EVENT_PUBLISHER_PORT = 5550


from typing import Optional
import unittest

class TreeNode:
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right

class Solution:
    def __init__(self):
        self.global_max = float("-inf")

    def postOrderTraversal(self, root: Optional[TreeNode]):
        if not root:
            return 0

        left_sum = max(self.postOrderTraversal(root.left), 0)
        right_sum = max(self.postOrderTraversal(root.right), 0)

        local_sum = left_sum + right_sum + root.val

        self.global_max = max(local_sum, self.global_max)

        return root.val + max(left_sum, right_sum)

    def maxPathSum(self, root: TreeNode) -> int:
        self.postOrderTraversal(root)
        global_max = int(self.global_max)
        self.global_max = float("-inf")
        return global_max

class Test(unittest.TestCase):
    def test(self):
        #   INITIAL TREE
        #       -10
        #     /     \
        #    9       20
        #           / \
        #          7   15
        tree = TreeNode(-10)
        tree.left = TreeNode(9)
        tree.right = TreeNode(20)
        tree.right.right = TreeNode(7)
        tree.right.left = TreeNode(15)

        s = Solution()

        self.assertEqual(s.maxPathSum(tree), 42)

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


import math
from argparse import ArgumentParser
from datetime import timedelta as delta

import numpy as np
import pytest

from parcels import (
    AdvectionEE,
    AdvectionRK4,
    AdvectionRK45,
    FieldSet,
    JITParticle,
    ParticleSet,
    ScipyParticle,
    Variable,
    timer,
)

ptype = {'scipy': ScipyParticle, 'jit': JITParticle}
method = {'RK4': AdvectionRK4, 'EE': AdvectionEE, 'RK45': AdvectionRK45}

def stommel_fieldset(xdim=200, ydim=200, grid_type='A'):
    """Simulate a periodic current along a western boundary, with significantly
    larger velocities along the western edge than the rest of the region

    The original test description can be found in: N. Fabbroni, 2009,
    Numerical Simulation of Passive tracers dispersion in the sea,
    Ph.D. dissertation, University of Bologna
    http://amsdottorato.unibo.it/1733/1/Fabbroni_Nicoletta_Tesi.pdf
    """
    a = b = 10000 * 1e3
    scalefac = 0.05  # to scale for physically meaningful velocities
    dx, dy = a / xdim, b / ydim

    # Coordinates of the test fieldset (on A-grid in deg)
    lon = np.linspace(0, a, xdim, dtype=np.float32)
    lat = np.linspace(0, b, ydim, dtype=np.float32)

    # Define arrays U (zonal), V (meridional) and P (sea surface height)
    U = np.zeros((lat.size, lon.size), dtype=np.float32)
    V = np.zeros((lat.size, lon.size), dtype=np.float32)
    P = np.zeros((lat.size, lon.size), dtype=np.float32)

    beta = 2e-11
    r = 1/(11.6*86400)
    es = r/(beta*a)

    for j in range(lat.size):
        for i in range(lon.size):
            xi = lon[i] / a
            yi = lat[j] / b
            P[j, i] = (1 - math.exp(-xi / es) - xi) * math.pi * np.sin(math.pi * yi) * scalefac
            if grid_type == 'A':
                U[j, i] = -(1 - math.exp(-xi / es) - xi) * math.pi ** 2 * np.cos(math.pi * yi) * scalefac
                V[j, i] = (math.exp(-xi / es) / es - 1) * math.pi * np.sin(math.pi * yi) * scalefac
    if grid_type == 'C':
        V[:, 1:] = (P[:, 1:] - P[:, 0:-1]) / dx * a
        U[1:, :] = -(P[1:, :] - P[0:-1, :]) / dy * b

    data = {'U': U, 'V': V, 'P': P}
    dimensions = {'lon': lon, 'lat': lat}
    fieldset = FieldSet.from_data(data, dimensions, mesh='flat')
    if grid_type == 'C':
        fieldset.U.interp_method = 'cgrid_velocity'
        fieldset.V.interp_method = 'cgrid_velocity'
    return fieldset

def UpdateP(particle, fieldset, time):
    if time == 0:
        particle.p_start = fieldset.P[time, particle.depth, particle.lat, particle.lon]
    particle.p = fieldset.P[time, particle.depth, particle.lat, particle.lon]

def AgeP(particle, fieldset, time):
    particle.age += particle.dt
    if particle.age > fieldset.maxage:
        particle.delete()

def simple_partition_function(coords, mpi_size=1):
    """A very simple partition function that assigns particles to processors (for MPI testing purposes))"""
    return np.linspace(0, mpi_size, coords.shape[0], endpoint=False, dtype=np.int32)

def stommel_example(npart=1, mode='jit', verbose=False, method=AdvectionRK4, grid_type='A',
                    outfile="StommelParticle.zarr", repeatdt=None, maxage=None, write_fields=True,
                    custom_partition_function=False):
    timer.fieldset = timer.Timer('FieldSet', parent=timer.stommel)
    fieldset = stommel_fieldset(grid_type=grid_type)
    if write_fields:
        filename = 'stommel'
        fieldset.write(filename)
    timer.fieldset.stop()

    # Determine particle class according to mode
    timer.pset = timer.Timer('Pset', parent=timer.stommel)
    timer.psetinit = timer.Timer('Pset_init', parent=timer.pset)
    ParticleClass = JITParticle if mode == 'jit' else ScipyParticle

    # Execute for 600 days, with 1-hour timesteps and 5-day output
    runtime = delta(days=600)
    dt = delta(hours=1)
    outputdt = delta(days=5)

    class MyParticle(ParticleClass):
        p = Variable('p', dtype=np.float32, initial=0.)
        p_start = Variable('p_start', dtype=np.float32, initial=0.)
        next_dt = Variable('next_dt', dtype=np.float64, initial=dt.total_seconds())
        age = Variable('age', dtype=np.float32, initial=0.)

    if custom_partition_function:
        pset = ParticleSet.from_line(fieldset, size=npart, pclass=MyParticle, repeatdt=repeatdt,
                                     start=(10e3, 5000e3), finish=(100e3, 5000e3), time=0,
                                     partition_function=simple_partition_function)
    else:
        pset = ParticleSet.from_line(fieldset, size=npart, pclass=MyParticle, repeatdt=repeatdt,
                                     start=(10e3, 5000e3), finish=(100e3, 5000e3), time=0)

    if verbose:
        print(f"Initial particle positions:\n{pset}")

    maxage = runtime.total_seconds() if maxage is None else maxage
    fieldset.add_constant('maxage', maxage)
    print("Stommel: Advecting %d particles for %s" % (npart, runtime))
    timer.psetinit.stop()
    timer.psetrun = timer.Timer('Pset_run', parent=timer.pset)
    pset.execute(method + pset.Kernel(UpdateP) + pset.Kernel(AgeP), runtime=runtime, dt=dt,
                 output_file=pset.ParticleFile(name=outfile, outputdt=outputdt))

    if verbose:
        print(f"Final particle positions:\n{pset}")
    timer.psetrun.stop()
    timer.pset.stop()

    return pset

@pytest.mark.parametrize('grid_type', ['A', 'C'])
@pytest.mark.parametrize('mode', ['jit', 'scipy'])
def test_stommel_fieldset(mode, grid_type, tmpdir):
    timer.root = timer.Timer('Main')
    timer.stommel = timer.Timer('Stommel', parent=timer.root)
    outfile = tmpdir.join("StommelParticle")
    psetRK4 = stommel_example(1, mode=mode, method=method['RK4'], grid_type=grid_type, outfile=outfile, write_fields=False)
    psetRK45 = stommel_example(1, mode=mode, method=method['RK45'], grid_type=grid_type, outfile=outfile, write_fields=False)
    assert np.allclose(psetRK4.lon, psetRK45.lon, rtol=1e-3)
    assert np.allclose(psetRK4.lat, psetRK45.lat, rtol=1.1e-3)
    err_adv = np.abs(psetRK4.p_start - psetRK4.p)
    assert (err_adv <= 1.e-1).all()
    err_smpl = np.array([abs(psetRK4.p[i] - psetRK4.fieldset.P[0., psetRK4.lon[i], psetRK4.lat[i], psetRK4.depth[i]]) for i in range(psetRK4.size)])
    assert (err_smpl <= 1.e-1).all()
    timer.stommel.stop()
    timer.root.stop()
    timer.root.print_tree()

def main(args=None):
    timer.root = timer.Timer('Main')
    timer.args = timer.Timer('Args', parent=timer.root)
    p = ArgumentParser(description="""
Example of particle advection in the steady-state solution of the Stommel equation""")
    p.add_argument('mode', choices=('scipy', 'jit'), nargs='?', default='jit',
                   help='Execution mode for performing computation')
    p.add_argument('-p', '--particles', type=int, default=1,
                   help='Number of particles to advect')
    p.add_argument('-v', '--verbose', action='store_true', default=False,
                   help='Print particle information before and after execution')
    p.add_argument('-m', '--method', choices=('RK4', 'EE', 'RK45'), default='RK4',
                   help='Numerical method used for advection')
    p.add_argument('-o', '--outfile', default='StommelParticle.zarr',
                   help='Name of output file')
    p.add_argument('-r', '--repeatdt', default=None, type=int,
                   help='repeatdt of the ParticleSet')
    p.add_argument('-a', '--maxage', default=None, type=int,
                   help='max age of the particles (after which particles are deleted)')
    p.add_argument('-wf', '--write_fields', default=True,
                   help='Write the hydrodynamic fields to NetCDF')
    p.add_argument('-cpf', '--custom_partition_function', default=False,
                   help='Use a custom partition_function (for MPI testing purposes)')
    args = p.parse_args(args)

    timer.args.stop()
    timer.stommel = timer.Timer('Stommel', parent=timer.root)
    stommel_example(args.particles, mode=args.mode, verbose=args.verbose, method=method[args.method],
                    outfile=args.outfile, repeatdt=args.repeatdt, maxage=args.maxage, write_fields=args.write_fields,
                    custom_partition_function=args.custom_partition_function)
    timer.stommel.stop()
    timer.root.stop()
    timer.root.print_tree()

if __name__ == "__main__":
    main()


"""empty message

Revision ID: 88f77742bc2d
Revises:
Create Date: 2017-07-03 13:15:12.714598

"""
from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = '88f77742bc2d'
down_revision = None
branch_labels = None
depends_on = None

def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table('redemption',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('coupon_id', sa.Integer(), nullable=True),
    sa.Column('redemption_date', sa.DateTime(), nullable=True),
    sa.ForeignKeyConstraint(['coupon_id'], ['coupon.id'], ),
    sa.PrimaryKeyConstraint('id')
    )
    # ### end Alembic commands ###

def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_table('redemption')
    # ### end Alembic commands ###


#!/usr/bin/python3

import os
import argparse
import time
import urllib.request
import email.mime.text
import socket
import getpass
import subprocess

def lookup_mac_vendor(mac_address):
    result = ""
    if mac_address:
        for i in range(3):
            vendor = None
            try:
                # Only first 3 octets are needed for manufacturer identification
                # The rest is ommited for privacy reasons
                response = urllib.request.urlopen("https://api.macvendors.com/" + mac_address[:8])
                vendor = response.read().decode(response.info().get_param('charset') or 'utf-8')
            except:
                time.sleep(1)
            if vendor:
                result = vendor
                break
    return(result)

def send_mail(vendor):
    msg = email.mime.text.MIMEText(
        "IP: {ip}\n"
        "Static: {is_static}\n"
        "MAC: {mac}\n"
        "Vendor: {vendor}\n"
        "Host: {host}\n"
        "Static host name: {static}".format(
            ip=options.ip,
            is_static="yes" if options.is_static == "1" else "no",
            mac=options.mac,
            vendor=vendor,
            host=options.host_name,
            static=options.static_host_name
        )
    )
    msg['From'] = getpass.getuser() + "@" + socket.gethostname()
    msg['To'] = options.mail_to
    msg['Subject'] = "New DHCP lease for {} on {}".format(options.host_name, socket.gethostname())
    proc = subprocess.Popen(["/usr/sbin/sendmail", "-t", "-oi"], stdin=subprocess.PIPE)
    proc.communicate(bytes(msg.as_string(), "UTF-8"))

def forked_main():
    vendor = lookup_mac_vendor(options.mac)
    mac_is_known = False
    if options.mac:
        if os.path.isfile(options.known_macs_file):
            with open(options.known_macs_file) as f:
                for line in f:
                    if line.rstrip().upper() == options.mac.upper():
                        mac_is_known = True
    if not mac_is_known:
        send_mail(vendor)
        if options.mac:
            with open(options.known_macs_file, "a") as f:
                f.write(options.mac + "\n")

parser = argparse.ArgumentParser(description="New DHCP lease event handler")
parser.add_argument("known_macs_file", help="Known MAC-address list file")
parser.add_argument("ip", nargs="?", default="", help="IP address")
parser.add_argument("mac", nargs="?", default="", help="MAC address")
parser.add_argument("host_name", nargs="?", default="", help="Name of the client")
parser.add_argument("static_host_name", nargs="?", default="", help="Matched static host name")
parser.add_argument("is_static", nargs="?", default="", help="IP is static (0 or 1)")
parser.add_argument('-n', '--no-fork', dest='no_fork', action='store_true',
    default=False, help='Do not fork a child process')
parser.add_argument('-m', '--mail-to', dest='mail_to', default="root",
    help='Mail message recipient (default: root)', metavar="mail")

options = parser.parse_args()

if options.no_fork:
    print("--no-fork option is specified. Running in foreground")
    print("IP: {}".format(options.ip))
    print("MAC: {}".format(options.mac))
    print("Mail to: {}".format(options.mail_to))
    forked_main()
elif os.fork() == 0:
    forked_main()
    os._exit(0)


from flask import jsonify, render_template
from app.device import bp, device
from app.models import DeviceModel, Project

@bp.route('/devices', methods=['GET', 'POST'])
def devices():
    connected = device.get_devices()
    registered = DeviceModel.query.all()
    return render_template('device/devices.html', title='All devices',
                           connected=connected, registered=registered)

# @bp.route('/test_devices', methods=['GET', 'POST'])
# def test_devices():
#     registered = Device.query.all()
#     return jsonify(Device.serialize_list(registered))

@bp.route('/devices/check', methods=['GET', 'POST'])
def check():
    return {}


import pandas as pd
import tqdm
import numpy as np
import codecs
import glob
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics import accuracy_score, f1_score
from sklearn.linear_model import LogisticRegression
import random
import matplotlib.pyplot as plt
import sys
from sklearn import preprocessing
from sklearn.model_selection import train_test_split
from sklearn.utils import shuffle

random.seed(16)
np.random.seed(16)

print("Without l1!")

liar_train = codecs.open("data/liar_xtrain.txt", 'r', 'utf-8').read().split('\n')
liar_train = [s.lower() for s in liar_train if len(s) > 1]
liar_train_labels = codecs.open('data/liar_ytrain.txt', 'r', 'utf-8').read().split('\n')
liar_train_lab = [s for s in liar_train_labels if len(s) > 1]

liar_dev = codecs.open("data/liar_xval.txt", 'r', 'utf-8').read().split('\n')
liar_dev = [s.lower() for s in liar_dev if len(s) > 1]
liar_dev_labels = codecs.open("data/liar_yval.txt", 'r', 'utf-8').read().split('\n')
liar_dev_lab = [s for s in liar_dev_labels if len(s) > 1]

liar_test = codecs.open("data/liar_xtest.txt", 'r', 'utf-8').read().split('\n')
liar_test = [s.lower() for s in liar_test if len(s) > 1]
liar_test_labels = codecs.open("data/liar_ytest.txt", 'r', 'utf-8').read().split('\n')
liar_test_lab = [s for s in liar_test_labels if len(s) > 1]

assert len(liar_train) == len(liar_train_lab)
assert len(liar_dev) == len(liar_dev_lab)
assert len(liar_test) == len(liar_test_lab)

le = preprocessing.LabelEncoder()
#classes = ['pants-fire','false','barely-true','half-true','mostly-true','true']
#le.fit_transform(classes)
liar_train_lab = le.fit_transform(liar_train_lab)
liar_dev_lab = le.transform(liar_dev_lab)
liar_test_lab = le.transform(liar_test_lab)

print(le.classes_) #['barely-true' 'false' 'half-true' 'mostly-true' 'pants-fire' 'true']
print(le.transform(le.classes_)) # [0 1 2 3 4 5]
# untrue classes (to be encoded as 0): 4, 1, 0
# true classes (to be encoded as 1): 2, 3, 5

def binarize_label(labels):
    labels_transformed = [1 if i in [2,3,5] else 0 for i in labels]
    return labels_transformed

def label_switch(labels):
    labels_transformed = [1 if i==0 else 0 for i in labels]
    return labels_transformed

liar_train_lab = binarize_label(liar_train_lab)
liar_dev_lab = binarize_label(liar_dev_lab)
liar_test_lab = binarize_label(liar_test_lab)

# Kaggle data
data = codecs.open("data/kaggle_trainset.txt", 'r', 'utf-8').read().split('\n')
data = data[:20800]
data = [s.lower() for s in data]

labels = codecs.open("data/kaggle_train_labels.txt", 'r', 'utf-8').read().split('\n')
labels = labels[:20800]
labels = [int(i) for i in labels]

tr, te, trlab, telab = train_test_split(data, labels, test_size=0.33, random_state=42)

kaggle_train = tr
kaggle_train_lab = label_switch(trlab)
kaggle_test = te
kaggle_test_lab = label_switch(telab)

# FakeNewsCorpus data (part of it, 25000 samples in each class)
FNC_fake = codecs.open("data/fake_part1.txt", 'r', 'utf-8').read().split('\n')
FNC_fake = FNC_fake[:25000]
FNC_true = codecs.open("data/true_part1.txt", 'r', 'utf-8').read().split('\n')
FNC_true = FNC_fake[:25000]
FNC_fake_labels = np.zeros(len(FNC_fake))
FNC_true_labels = np.ones(len(FNC_true))
FNC_samples = np.concatenate((FNC_fake, FNC_true))
FNC_labels = np.concatenate((FNC_fake_labels, FNC_true_labels))
assert len(FNC_samples) == len(FNC_labels)
FNC_samples, FNC_labels = shuffle(FNC_samples, FNC_labels, random_state=42)

FNC_Xtrain, FNC_Xtest, FNC_ytrain, FNC_ytest = train_test_split(FNC_samples, FNC_labels, test_size=0.33, random_state=42)

# FakeNewsNet Politifact data
# real = codecs.open("data/RealNewsContent.txt", 'r', 'utf-8').read().split('\n')
# real = [s.lower() for s in real if len(s) > 1]
# fake = codecs.open("data/FakeNewsContent.txt", 'r', 'utf-8').read().split('\n')
# fake = [s.lower() for s in fake if len(s) > 1]
# real_labels = np.ones(len(real))
# fake_labels = np.zeros(len(fake))
# FNN_labels = np.concatenate((real_labels, fake_labels))
# FNN_texts = np.concatenate((real, fake))
# assert len(FNN_labels) == len(FNN_texts)
# FNN_X, FNN_y = shuffle(FNN_texts,FNN_labels, random_state=42)

m= 5000 #number of feats 5000 or 10000
k=5#max ngram
v=1 #min mgram

# Vectorizing
print("Vectorizing...")
liar_vectorizer = TfidfVectorizer(ngram_range=(v,k), max_features=m)
X_train_liar = liar_vectorizer.fit_transform(liar_train)
X_test_liar = liar_vectorizer.transform(liar_test)
X_dev_liar = liar_vectorizer.transform(liar_dev)
liar_feats = ['_'.join(s.split()) for s in liar_vectorizer.get_feature_names()]

kaggle_vectorizer = TfidfVectorizer(ngram_range=(v,k), max_features=m)
X_train_kaggle = kaggle_vectorizer.fit_transform(kaggle_train)
X_test_kaggle = kaggle_vectorizer.transform(kaggle_test)
kaggle_feats = ['_'.join(s.split()) for s in kaggle_vectorizer.get_feature_names()]

FNC_vectorizer = TfidfVectorizer(ngram_range=(v,k), max_features=m)
FNC_Xtrain_vect = FNC_vectorizer.fit_transform(FNC_Xtrain)
FNC_Xtest_vect = FNC_vectorizer.transform(FNC_Xtest)
FNC_feats = ['_'.join(s.split()) for s in FNC_vectorizer.get_feature_names()]

print("Vectorizing done")
print("Fitting classifiers...")
# Classifiers
clf_liar=None
clf_liar = LogisticRegression(random_state=16, solver='saga',C=np.inf, max_iter=10000).fit(X_train_liar,liar_train_lab)
liar_coefs = clf_liar.coef_
allcoefs_liar = pd.DataFrame.from_records(liar_coefs, columns=liar_feats) #add ngrams as colnames
allcoefs_liar.to_csv('NEW_liar_coefs_final.csv', sep='\t', index=False)

clf_kaggle=None
clf_kaggle = LogisticRegression(random_state=16, solver='saga',C=np.inf, max_iter=10000).fit(X_train_kaggle,kaggle_train_lab)
kaggle_coefs = clf_kaggle.coef_
allcoefs_kaggle = pd.DataFrame.from_records(kaggle_coefs, columns=kaggle_feats) #add ngrams as colnames
allcoefs_kaggle.to_csv('NEW_kaggle_coefs_final.csv', sep='\t', index=False)

clf_FNC=None
clf_FNC = LogisticRegression(random_state=16, solver='saga',C=np.inf, max_iter=10000).fit(FNC_Xtrain_vect, FNC_ytrain)
FNC_coefs = clf_FNC.coef_
allcoefs_FNC = pd.DataFrame.from_records(FNC_coefs, columns=FNC_feats)
allcoefs_FNC.to_csv("NEW_FakeNewsCorpus_coefs.csv", sep="\t", index=False)

# get coefs from FakeNewsNet while your at it
# FNN_vectorizer = TfidfVectorizer(ngram_range=(v,k), max_features=m)
# FNN_vect = FNN_vectorizer.fit_transform(FNN_X)
# FNN_feats = ['_'.join(s.split()) for s in FNN_vectorizer.get_feature_names()]
# clf_FNN=None
# clf_FNN = LogisticRegression(random_state=16, solver='saga', penalty='l1', max_iter=10000).fit(FNN_vect,FNN_y)
# FNN_coefs = clf_FNN.coef_
# all_coefs_FNN = pd.DataFrame.from_records(FNN_coefs, columns=FNN_feats)
# all_coefs_FNN.to_csv('FakeNewsNet_coefs_final.csv', sep='\t', index=False)

print("Predicting...")
# Predicting
preds_liar_test = clf_liar.predict(X_test_liar)
preds_liar_valid = clf_liar.predict(X_dev_liar)
preds_liar_train = clf_liar.predict(X_train_liar)
#
preds_kaggle_test = clf_kaggle.predict(X_test_kaggle)
preds_kaggle_tain = clf_kaggle.predict(X_train_kaggle)

preds_FNC_test = clf_FNC.predict(FNC_Xtest_vect)
preds_FNC_train = clf_FNC.predict(FNC_Xtrain_vect)

# cross-dataset predictions
kaggle_test_vectorized_by_liar = liar_vectorizer.transform(kaggle_test)
kaggle_test_predicted_by_liar_classifier = clf_liar.predict(kaggle_test_vectorized_by_liar)
#
kaggle_train_vectorized_by_liar = liar_vectorizer.transform(kaggle_train)
kaggle_train_predicted_by_liar = clf_liar.predict(kaggle_train_vectorized_by_liar)

FNC_test_vectorized_by_liar = liar_vectorizer.transform(FNC_Xtest)
FNC_test_predicted_by_liar = clf_liar.predict(FNC_test_vectorized_by_liar)

FNC_test_vectorized_by_kaggle = kaggle_vectorizer.transform(FNC_Xtest)
FNC_test_predicted_by_kaggle = clf_kaggle.predict(FNC_test_vectorized_by_kaggle)

kaggle_test_vectorized_by_FNC = FNC_vectorizer.transform(kaggle_test)
kaggle_test_predicted_by_FNC = clf_FNC.predict(kaggle_test_vectorized_by_FNC)

liar_test_vectorized_by_FNC = FNC_vectorizer.transform(liar_test)
liar_test_predicted_by_FNC = clf_FNC.predict(liar_test_vectorized_by_FNC)

# using other models to predict on FakeNewsNet
# FNN_vectorized_by_liar = liar_vectorizer.transform(FNN_X)
# FNN_predicted_by_liar = clf_liar.predict(FNN_vectorized_by_liar)
#
# FNN_vectorized_by_kaggle = kaggle_vectorizer.transform(FNN_X)
# FNN_predicted_by_kaggle = clf_kaggle.predict(FNN_vectorized_by_kaggle)

def print_scores(y, y_hat, string):
    print(string)
    print("binary F1", f1_score(y, y_hat))
    print("micro f1:",f1_score(y, y_hat, average='micro'))
    print("macro f1:", f1_score(y, y_hat, average='macro'))
    print("weighted F1", f1_score(y, y_hat, average='weighted'))
    print("accuracy", accuracy_score(y, y_hat))
    print()

print_scores(liar_test_lab, preds_liar_test, "Liar Test Scores")
print_scores(liar_dev_lab, preds_liar_valid, "Liar Valid. Scores")
print_scores(liar_train_lab, preds_liar_train, "Liar Train Scores")
#
print_scores(kaggle_test_lab, preds_kaggle_test, "Kaggle Test Scores")
print_scores(kaggle_train_lab, preds_kaggle_tain, "Kaggle Train Scores")
#
print_scores(kaggle_test_lab, kaggle_test_predicted_by_liar_classifier, "Kaggle Test Set Predicted by Classifier Trained on Liar")
print_scores(kaggle_train_lab, kaggle_train_predicted_by_liar, "Kaggle Train Set Predicted by Classifier Trained on Liar")

print_scores(FNC_ytest, preds_FNC_test, "FakeNewsCorpus test prediction scores")
print_scores(FNC_ytrain, preds_FNC_train, "FakeNewsCorpus train prediction scores")
print_scores(FNC_ytest, FNC_test_predicted_by_liar, "FakeNewsCorpus test set predicted by Liar classifier")
print_scores(FNC_ytest, FNC_test_predicted_by_kaggle, "FakeNewsCorpus test set predicted by kaggle classifier")
print_scores(kaggle_test_lab, kaggle_test_predicted_by_FNC, "Kaggle Test Set Predicted by Classifier Trained on FakeNewsCorpus")
print_scores(liar_test_lab, liar_test_predicted_by_FNC, "Liar Test Set Predicted by Classifier Trained on FakeNewsCorpus")

# print_scores(FNN_y, FNN_predicted_by_liar, "FakeNewsNet Predicted by Classifier Trained on Liar")
# print_scores(FNN_y, FNN_predicted_by_kaggle, "FakeNewsNet Predicted by Classifier Trained on Kaggle")

# random liar test set
# random_liar_labels = liar_test_lab.copy()
# np.random.shuffle(random_liar_labels)
# print_scores(random_liar_labels,preds_liar_test, "Scores between predictions on Liar test set and randomized 'true' test labels. Classifier trained on Liar trains set")

print("Done")


# -*- codeing = utf-8 -*-
# @Time : 2021/5/9 21:41
# @File ：校内赛识别图形最终版本.py
# @Software : PyCharm
import cv2 as cv

def detectShape(img):
    # 查找轮廓，cv2.RETR_ExTERNAL=获取外部轮廓点, CHAIN_APPROX_NONE = 得到所有的像素点
    contours, hierarchy = cv.findContours(img, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_NONE)
    # 循环轮廓，判断每一个形状
    global count
    global objectType
    count = 0
    objectType = None
    for cnt in contours:
        # 获取轮廓面积
        area = cv.contourArea(cnt)
        # 当面积大于1500，代表有形状存在
        if area > 1500:
            # 绘制所有的轮廓并显示出来
            cv.drawContours(imgContour, cnt, -1, (255, 0, 0), 3)
            # 计算所有轮廓的周长，便于做多边形拟合
            peri = cv.arcLength(cnt, True)
            # 多边形拟合，获取每个形状的边
            approx = cv.approxPolyDP(cnt, 0.02 * peri, True)
            objCor = len(approx)
            # 获取每个形状的x，y，w，h
            x, y, w, h = cv.boundingRect(approx)
            # 计算出边界后，即边数代表形状，如三角形边数=3
            if objCor == 3:
                objectType = "Triangle"
                print("形状是%s,面积是%d" % (objectType, area))
                count += 1
            elif objCor == 4:
                objectType = "Rectangle"
                print("形状是%s,面积是%d" % (objectType, area))
                count += 1
            # 大于4个边的就是五边形
            elif 5 <= objCor < 7:
                objectType = "Pentagon"
                print("形状是%s,面积是%d" % (objectType, area))
                count += 1
            elif objCor == 8:
                objectType = "Circle"
                print("形状是%s,面积是%d" % (objectType, area))
                count += 1
            elif objCor > 8:
                objectType = "Star"
                print("形状是%s,面积是%d" % (objectType, area))
                count += 1
            # 绘制文本时需要绘制在图形附件
            cv.rectangle(imgContour, (x, y), (x + w, y + h), (0, 255, 0), 2)
            cv.putText(imgContour, objectType,
                       (x + (w // 2) - 10, y + (h // 2) - 10), cv.FONT_HERSHEY_COMPLEX, 0.7,
                       (0, 0, 0), 2)

# 调用笔记本内置摄像头，所以参数为0，官方摄像头为1
cap = cv.VideoCapture(0, cv.CAP_DSHOW)
while True:
    # 从摄像头读取图片
    success, img = cap.read()
    # 转换大小
    img = cv.resize(img, (1000, 700))
    # 灰度化
    imgGray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
    # 高斯平滑
    imgBlur = cv.GaussianBlur(imgGray, (7, 7), 1)
    # 边缘检测
    imgCanny = cv.Canny(imgBlur, 200, 200)
    # 膨胀
    kernel = cv.getStructuringElement(cv.MORPH_RECT, (2, 2))
    swell = cv.dilate(imgCanny, kernel=kernel)
    # 调用检测函数
    imgContour = img.copy()
    detectShape(swell)
    # 绘制文本
    cv.putText(imgContour, "%s,%d" % (objectType, count), (10, 50), cv.FONT_HERSHEY_PLAIN, 2.0,
               (0, 0, 0), 2)
    # 若参数delay≤0：表示一直等待按键；
    # 若delay取正整数：表示等待按键的时间，比如cv2.waitKey(100)，就是等待100毫秒
    k = cv.waitKey(100)
    # 保持画面的持续。
    cv.imshow("img", imgContour)
    if k == 27:
        # 通过esc键退出摄像
        cv.destroyAllWindows()
        break

# 关闭摄像头
cap.release()
cv.destroyAllWindows()

校内赛识别图形最终版本.py

校内赛识别图形最终版本.py


from win10toast import ToastNotifier
import feedparser
import time
toaster = ToastNotifier()

def getNews():
    url = "https://www.youm7.com/rss/SectionRss?SectionID=203" #url rss
    feed = feedparser.parse("https://www.youm7.com/rss/SectionRss?SectionID=203") #Get all feed from rss

    for item in feed["entries"]:
        toaster.show_toast("اليوم السابع",item["title"],duration=100)

    while toaster.notification_active():
        time.sleep(1000)

if __name__ == '__main__':
    getNews()


import numpy as np
import pandas as pd
import pickle
from flask import Flask ,app ,request ,jsonify ,render_template ,url_for
app=Flask(__name__)
reg_model=pickle.load(open('regm.pkl','rb'))        #load regression model
scaler=pickle.load(open('scale.pkl','rb'))          #load scaling model
@app.route('/')
def home():
    return render_template('home.html')             #redirect to home.html
@app.route('/predict_api',methods=['POST'])         #api with post request becuase we take input then generate output using it
def predict1():
    data=request.json['data']
    print(data)                                         #data is dictionory with elements in key value pair
    print(np.array(list(data.values())).reshape(1,-1))  #convert all inputs in a single file of data to add in prediction model
    transformed_data=scaler.transform(np.array(list(data.values())).reshape(1,-1))
    output=reg_model.predict(transformed_data)
    print(output[0])                                       #output recived is an 2d array
    return jsonify(output[0])                               #return jsonifoed output

@app.route('/predict',methods=['POST'])
def predict():
    input=[float(x) for x in request.form.values()]             #Get input from html form
    transformed_input=scaler.transform(np.array(input).reshape(1,-1))
    output=reg_model.predict(transformed_input)[0]
    return render_template("home.html",prediction_text="Predicted House Price is {}".format(output))

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


import curses
import math
import sys
import logging as log
from pprint import pformat

class Menu:

    DIRECTION_UP = 'UP'
    DIRECTION_DOWN = 'DOWN'

    def __init__(self, layout, border=True):
        self.win = curses.newwin(layout[0],
                                 layout[1],
                                 layout[2],
                                 layout[3])
        self.update()
        self.storage = []
        self.storage_event = []
        self.border = border
        # NOTE: Default setting
        self.selectable = False
        self.page_number = 1
        self.page_index = 0
        self.selected_index = 0
        self.draw_border()
        self.user_list = None
        self.buffered_line_count = 0
        # self.win.bkgd(' ', curses.color_pair(3))

    def set_user_list(self, user_list):
        self.user_list = user_list

    def update(self):
        self.win.refresh()

    def set_storage(self,data, index=None):
        if index:
            self.storage[index] = data
        else:
            self.storage = data

    def append_storage(self, data):
        self.storage.extend(data)

    def append_storage_event(self, events):
        self.storage_event.extend(events)

    def prepend_storage_event(self, events):
        self.storage_event = events + self.storage_event

    def prepend_storage(self, data):
        self.storage = data + self.storage

    def number_of_drawable_rows(self):
        return self.win.getmaxyx()[0]

    def clear(self):
        self.storage = []
        self.storage_event = []
        self.page_number = 1
        self.page_index = 0
        self.selected_index = 0
        self.buffered_line_count = 0
        self.win.clear()

    def draw_border(self):
        if self.border:
            self.win.border(0)

    def draw(self):
        self.win.clear()
        self.draw_border()
        self.write_storage()
        self.update()

    # TODO: Remove this at some point
    def write_clear(self):
        loc = self.draw_location()
        lines = self.page_length()
        blank_line = ' ' * self.draw_bounds()[1]
        for i in range(0, lines):
            draw_y = loc[0] + i
            draw_x = loc[1]
            if self.draw_inbounds(draw_y, draw_x):
                # TODO: Fix with try cache, because addstr \n places cursor, bottom right corner
                self.win.addstr(draw_y, draw_x, blank_line)

    def write_storage(self):
        loc = self.draw_location()
        lines = self.read_storage()
        local_selected = self.local_selected_index(self.selected_index);
        for i in range(0, len(lines)):
            draw_y = loc[0] + i
            draw_x = loc[1]
            line_to_draw = lines[i]
            line_width = self.draw_line_width()
            if len(line_to_draw) > line_width:
                line_to_draw = line_to_draw[0:line_width]

            if self.draw_inbounds(draw_y, draw_x):
                # TODO: Fix with try cache, because addstr \n places cursor, bottom right corner
                if i == local_selected and self.selectable:
                    self.win.addstr(draw_y, draw_x, line_to_draw, curses.color_pair(1))
                else:
                    self.win.addstr(draw_y, draw_x, line_to_draw)

    def local_selected_index(self, index):
        return abs(self.page_length() - ((self.page_length() * self.page_number)-self.selected_index))

    def read_storage(self):
        if self.page_number <= self.page_count():
            read = self.page_length() * self.page_number
            output = self.storage[self.page_index:read]
            return output
        return []

    def draw_location(self):
        if self.border:
            return (1,1)
        return (0,0)

    def draw_bounds(self):
        if self.border:
            y,x = self.win.getmaxyx()
            return (y - 2, x - 2)
        return self.win.getmaxyx()

    def draw_inbounds(self, y, x):
        b_y, b_x = self.draw_bounds()
        if y > b_y:
            return False
        if x > b_x:
            return False
        if y < 0:
            return False
        if x < 0:
            return False
        return True

    def draw_line_width(self):
        return self.draw_bounds()[1]

    def page_count(self):
        return math.ceil(len(self.storage)/self.draw_bounds()[0])

    def compute_page_count(self, lines):
        return math.ceil(len(lines)/self.draw_bounds()[0])

    def page_length(self):
        return self.draw_bounds()[0]

    def select_event(self):
        try :
            event = self.storage_event[self.selected_index]
            return event[0](event[1])
        except Exception as e:
            log.error(e)
            log.error('No Event Action off Select(enter)')

    def select_down(self):
        if self.selected_index < len(self.storage) - 1:
            self.selected_index = self.selected_index + 1
        if self.selected_index >= self.page_length() * self.page_number:
            self.scroll_down()

    def select_up(self):
        if self.selected_index > 0:
            self.selected_index = self.selected_index - 1
        if self.selected_index < self.page_length() * (self.page_number - 1):
            self.scroll_up()

    def select(self, direction):
        if direction == self.DIRECTION_UP:
            self.select_up()
        elif direction == self.DIRECTION_DOWN:
            self.select_down()

        self.write_clear()
        self.write_storage()
        self.update()

    def scroll_down(self):
        if self.page_number < self.page_count():
            self.page_index = self.page_index + self.page_length()
            self.selected_index = self.page_index
            self.page_number = self.page_number + 1

    def scroll_up(self):
        if self.page_number > 1:
            self.page_index = self.page_index - self.page_length()
            self.selected_index = self.page_index + self.page_length() - 1
            self.page_number = self.page_number - 1

    def scroll(self, direction):
        if direction == self.DIRECTION_UP:
            self.scroll_up()
        elif direction == self.DIRECTION_DOWN:
            self.scroll_down()

        self.draw()

    def scroll_to_bottom(self):
        self.page_index = self.page_length()
        self.selected_index = self.page_index
        self.page_number = self.page_count()
        self.draw()

    def focus(self):
        self.selectable = True
        self.draw()

    def defocus(self):
        self.selectable = False
        self.draw()

    def eat_key(self, c):
        if  c == ord('j'):
            self.select('DOWN')
            pass
        elif c == ord('k'):
            self.select('UP')
            pass
        elif c == 10:
            self.select_event()
            pass

    def buffer_lines(self, lines):
        number_of_pages = self.compute_page_count(lines)
        total_lines = number_of_pages * self.page_length()
        total_buffer = total_lines - len(lines)
        buffer = [' '] * total_buffer
        lines = buffer + lines
        return lines

    def scroll_to_last_page(self):
        self.page_index = self.page_length() * (self.page_count() - 1)
        self.selected_index = self.page_index
        self.page_number = self.page_count()

    def fake_window_scroll(self):
        self.clear_previous_scroll()
        page_length = self.page_length()
        line_count_in_window = len(self.read_storage())
        [self.prepend_storage([' ' * self.draw_line_width()]) for _ in range(page_length - line_count_in_window)]
        self.buffered_line_count = page_length - line_count_in_window

    def clear_previous_scroll(self):
        self.storage = self.storage[self.buffered_line_count:]
        self.buffered_line_count = 0


import tushare as ts
import numpy as np
import matplotlib.pyplot as plt
import random
import math

import datetime
import tushare as ts
import pymysql
import stock_one

# p = stock_one.oneday('sh.600123', '20150102', '20210218')
# print(p)
# exit()

starttime = '20170102'
endtime = '20210218'

all = []

a = ['SH.600206', datetime.date(2017, 1, 3), 10.0739, 496, datetime.date(2021, 2, 10), 11.33, 12.460511999999987, 236.9613760000003]
b = ['SH.600206', datetime.date(2017, 1, 3), 10.0739, 496, datetime.date(2021, 2, 10), 11.33, 12.460511999999987, 236.9613760000003]
all.append(a)
all.append(b)

x = []
y = []

db = pymysql.connect(host='127.0.0.1', user='root', passwd='', db='tushare', charset='utf8')
cursor = db.cursor()
try:
    sql = "select * from stock_basic order by ts_code;"
    cursor.execute(sql)
    stockbasic = np.array(cursor.fetchall())

    for i in range(stockbasic.shape[0]):
        isgo = 0
        #print(stockbasic[i,:][0], stockbasic[i,:][1])
        if int(stockbasic[i,:][1]) >= 600000 and int(stockbasic[i,:][1]) < 600589: #600589:
            isgo = 1
        if int(stockbasic[i,:][1]) >= 1 and int(stockbasic[i,:][1]) < 898:
            isgo = 1
        # if int(stockbasic[i,:][1]) == 600160:
        #     isgo = 1
        if isgo == 0:
            continue

        realcode = stockbasic[i,:][0][7]+stockbasic[i,:][0][8]+'.'+stockbasic[i,:][1]
        print(i, realcode)

        p = stock_one.oneday(realcode, starttime, endtime)
        print(p)
        all.append(p)

except Exception as err:
    print(err)

cursor.close()
db.close()

all = np.array(all)
v1 = np.sort(all[:,6])
v2 = np.sort(all[:,7])

x = np.arange(all.shape[0])

# out_list = np.array([list(item) for item in zip(x,y)])

# print(out_list)
# out_list = out_list[np.lexsort(out_list.T)]
# print(out_list)

np.savez('all_data', all)

plt.title("ALL")
plt.xlabel("x")
plt.ylabel("price")
plt.ylim([-100,300])
plt.plot(x,v1,linestyle='--',color='red')
plt.plot(x,v2,linestyle='--',color='green')
plt.show()


"""
Created on Fri Nov  8 23:23:22 2019

@author: Pratiksha
"""

# app.py for Scara Web App

from flask import Flask, render_template, request
from data import Articles
import cv2
from pyzbar import pyzbar

app = Flask(__name__)

Articles = Articles()
S = []

@app.route('/')
def index():
	return render_template('home.html')

@app.route('/about')
def about():
	return render_template('about.html')

@app.route('/articles')
def articles():
	return render_template('articles.html', articles = Articles)

@app.route("/submit", methods=['GET', 'POST'])
def submit():

	if request.method == 'POST':
		f = request.files['file']
		f.save(f.filename)
		image = cv2.imread(f.filename)
		barcodes = pyzbar.decode(image)
		data = ""
		for barcode in barcodes:
		# extract the bounding box location of the barcode and draw the
		# bounding box surrounding the barcode on the image
			(x, y, w, h) = barcode.rect
			cv2.rectangle(image, (x, y), (x + w, y + h), (0, 0, 255), 2)

		# the barcode data is a bytes object so if we want to draw it on
		# our output image we need to convert it to a string first
			data = barcode.data.decode("utf-8")

			for i in range(len(Articles)):
				if data==Articles[i]['id']:
					S.append([Articles[i]['title'],Articles[i]['mrp']])

	return render_template('bill.html', data=S)

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


'''
The following codes are from https://github.com/d-li14/mobilenetv2.pytorch
Some helper functions for PyTorch, including:
    - get_mean_and_std: calculate the mean and std value of dataset.
    - msr_init: net parameter initialization.
    - progress_bar: progress bar mimic xlua.progress.
'''

import errno
import os

import torch
from torch import nn
from torch.nn import init

__all__ = ['get_mean_and_std', 'init_params', 'mkdir_p', 'AverageMeter']

def get_mean_and_std(dataset):
    '''
    Compute the mean and std value of dataset.

    :param dataset: input data
    :return: (mean, std): mean value of dataset, and standard deviation value of dataset
    '''
    dataloader = torch.utils.data.DataLoader(dataset, batch_size=1, shuffle=True, num_workers=2)

    mean = torch.zeros(3)
    std = torch.zeros(3)
    print('==> Computing mean and std..')
    for inputs, _ in dataloader:
        for i in range(3):
            mean[i] += inputs[:,i,:,:].mean()
            std[i] += inputs[:,i,:,:].std()
    mean.div_(len(dataset))
    std.div_(len(dataset))
    return mean, std

def init_params(net):
    '''
    Init layer parameters.

    :param net: input network to be initialized
    '''
    for module in net.modules():
        if isinstance(module, nn.Conv2d):
            init.kaiming_normal(module.weight, mode='fan_out')
            if module.bias:
                init.constant(module.bias, 0)
        elif isinstance(module, nn.BatchNorm2d):
            init.constant(module.weight, 1)
            init.constant(module.bias, 0)
        elif isinstance(module, nn.Linear):
            init.normal(module.weight, std=1e-3)
            if module.bias:
                init.constant(module.bias, 0)

def mkdir_p(path):
    '''
    make dir if not exist

    :param path: directory path we make
    '''
    try:
        os.makedirs(path)
    except OSError as exc:  # Python >2.5
        if exc.errno == errno.EEXIST and os.path.isdir(path):
            pass
        else:
            raise

class AverageMeter:
    """
    Computes and stores the average and current value
       Imported from https://github.com/pytorch/examples/blob/master/imagenet/main.py#L247-L262
    """
    def __init__(self):
        self.val = 0
        self.avg = 0
        self.sum = 0
        self.count = 0

    def reset(self):
        """ reset function """
        self.val = 0
        self.avg = 0
        self.sum = 0
        self.count = 0

    def update(self, val, num=1):
        """
        update function

        :param val: input current average value
        :param n: number of items for val
        """
        self.val = val
        self.sum += val * num
        self.count += num
        self.avg = self.sum / self.count


import time
import argparse
import json
import tqdm
import torch
import numpy as np
import torch.nn.functional as F
from sklearn.metrics import roc_auc_score, f1_score
from model.model import Model
from model.data import get_data_frames, create_graph, create_hetero_graph, create_data_loaders

def train(model, optimizer, criterion, data_loader, device, run, epochs, eval_steps):
    train_loss, valid_scores, test_scores = [], [], []
    start_time = time.time()
    for epoch in range(1, epochs + 1):
        model.train()
        total_loss = total_examples = 0
        for batch_data in tqdm.tqdm(data_loader['train'], disable=True):
            optimizer.zero_grad()
            batch_data = batch_data.to(device)

            pred = model(batch_data)
            ground_truth = batch_data['user', 'item'].edge_label

            loss = criterion(pred, ground_truth)
            loss.backward()
            optimizer.step()
            total_loss += float(loss) * pred.numel()
            total_examples += pred.numel()

        epoch_loss = total_loss / total_examples
        train_loss.append(epoch_loss)

        if epoch % eval_steps == 0:
            print(f"\nEpoch: {epoch:03d}, Train Loss: {epoch_loss:.4f}")

            val_auc_score, val_f1_score = test(model, data_loader['val'], device)
            test_auc_score, test_f1_score = test(model, data_loader['test'], device)

            valid_scores.append((val_auc_score, val_f1_score))
            test_scores.append((test_auc_score, test_f1_score))

            spent_time = time.time() - start_time
            eval_results = (f'Run: {run + 1:02d}, '
                            f'Epoch: {epoch:02d}, '
                            f'Train Loss: {epoch_loss:.4f}, '
                            f'Valid AUC: {100 * val_auc_score:.2f}%, '
                            f'Valid F1: {100 * val_f1_score:.2f}%, '
                            f'Test AUC: {100 * test_auc_score:.2f}%, '
                            f'Test F1: {100 * test_f1_score:.2f}%')
            print(eval_results)
            print(f'---\nTraining Time Per Epoch: {spent_time / eval_steps: .4f} s\n---')
            start_time = time.time()

    return train_loss, valid_scores, test_scores

def test(model, data_loader, device):
    model.eval()
    predictions, ground_truths = [], []
    for batch_data in tqdm.tqdm(data_loader, disable=True):
        with torch.no_grad():
            batch_data = batch_data.to(device)
            predictions.append(model(batch_data))
            ground_truths.append(batch_data['user', 'item'].edge_label)

    predictions = torch.cat(predictions, dim=0).cpu().numpy()
    ground_truths = torch.cat(ground_truths, dim=0).cpu().numpy()

    out_sigmoid = torch.Tensor(predictions).sigmoid().cpu().numpy()
    out_probabilities = np.rint(out_sigmoid)

    auc_sc = roc_auc_score(ground_truths, out_sigmoid)
    f1_sc = f1_score(ground_truths, out_probabilities)

    return auc_sc, f1_sc

def main():
    parser = argparse.ArgumentParser(description='News Recommender GNN Model')
    parser.add_argument("--data", default="cit_pt", choices=["adressa_no","adressa_tr",
                                                             "cit_pt","cit_tr","cit_en",
                                                             "cit_pten","cit_tren",
                                                             "mind"],
                        help = "default: %(default)s")
    parser.add_argument('--plm', default="ptbert", choices=["mbert","enbert","nbbert",
                                                               "ptbert","xlm","gpt2"],
                        help = "plm model for item nodes, default: %(default)s")
    parser.add_argument('--use_seperate_test_data', action='store_true')
    parser.add_argument('--num_user_features', type=int, default=32,
                        help = "-1 for don't use user_features, default: %(default)i")
    parser.add_argument('--num_item_features', type=int, default=768,
                        help = "-1 for don't use item_features, default: %(default)i")
    parser.add_argument('--user_input_layer', default="lin", choices=["emb","lin"],
                        help = "input layer for user nodes, default: %(default)s")
    parser.add_argument('--item_input_layer', default="lin", choices=["emb","lin","emb+lin"],
                        help = "input layer for item nodes, default: %(default)s")
    parser.add_argument('--input_hidden_channels', type=int, default=64,
                        help = "default: %(default)i")
    parser.add_argument('--encoder', default="SAGE", choices=["SAGE","GAT","GCN"],
                        help = "default: %(default)s")
    parser.add_argument('--predictor', default="DOT", choices=["MLP","MLPDOT","DOT"],
                        help = "default: %(default)s")
    parser.add_argument('--encoder_layers', type=int, default=2,
                        help = "default: %(default)i")
    parser.add_argument('--predictor_layers', type=int, default=0,
                        help = "0 for DOT predictor, default: %(default)i")
    parser.add_argument('--encoder_hidden_channels', type=int, default=64,
                        help = "default: %(default)i")
    parser.add_argument('--predictor_hidden_channels', type=int, default=32,
                        help = "default: %(default)i")

    parser.add_argument('--dropout', type=float, default=0.0,
                        help = "default: %(default)f")
    parser.add_argument('--batch_size', type=int, default=1024,
                        help = "default: %(default)i")
    parser.add_argument('--lr', type=float, default=0.001,
                        help = "default: %(default)f")
    parser.add_argument('--epochs', type=int, default=-1,
                        help = "-1 to use model default value, default: %(default)i")
    parser.add_argument('--eval_steps', type=int, default=1,
                        help = "number of epochs at which logs printed, default: %(default)i")
    parser.add_argument('--device', type=int, default=0,
                        help = "default: %(default)i")
    parser.add_argument('--runs', type=int, default=1,
                        help = "default: %(default)i")
    args = parser.parse_args()
    print(str(args))

    # device setup
    device = f'cuda:{args.device}' if torch.cuda.is_available() else 'cpu'
    device = torch.device(device)
    print(f"Device: {device}")

    # get data
    with open('datasets/info.json', 'r') as f:
        datasets_info = json.load(f)

    users_csv = datasets_info[args.data]['users_csv']
    items_csv = datasets_info[args.data]['items_csv']
    user_naming = datasets_info[args.data]['user_naming']
    item_naming = datasets_info[args.data]['item_naming']
    feats_file = datasets_info[args.data]['plm'][args.plm]
    epochs = datasets_info[args.data]['epochs'] if args.epochs == -1 else args.epochs

    print(f'Loading user and item data frames for: {args.data}')
    df_users, df_items = get_data_frames(users_csv, items_csv, user_naming, item_naming)
    item_feature_tensor = torch.load(feats_file, map_location=device)
    user_features_init = '' if args.num_user_features == -1 else 'zero'

    disjoint_train_ratio = 0.3
    neg_sampling_ratio = 1.0
    num_neighbors = [10, 5]

    if args.use_seperate_test_data:
        print(f'Doing seperate user train and test data for {args.data}')
        users_train_csv = datasets_info[args.data]['users_train_csv']
        users_test_csv = datasets_info[args.data]['users_test_csv']

        print(f'Train:')
        df_users_train, _ = get_data_frames(users_train_csv, items_csv, user_naming, item_naming)
        train_edges_coo = create_graph(df_users_train, df_items, user_naming, item_naming)
        train_data = create_hetero_graph(edges_coo=train_edges_coo,
                                                user_features_init=user_features_init,
                                                user_feature_size=args.num_user_features,
                                                item_feature_tensor=item_feature_tensor)

        print(f'Test:')
        df_users_test, _ = get_data_frames(users_test_csv, items_csv, user_naming, item_naming)
        test_edges_coo = create_graph(df_users_test, df_items, user_naming, item_naming)
        test_data = create_hetero_graph(edges_coo=test_edges_coo,
                                        user_features_init=user_features_init,
                                        user_feature_size=args.num_user_features,
                                        item_feature_tensor=item_feature_tensor)

        val_ratio, test_ratio = 0.1, 0.1
        train_loader, val_loader, _ = create_data_loaders(data=train_data, val_ratio=val_ratio, test_ratio=test_ratio,
                                                                    disjoint_train_ratio=disjoint_train_ratio,
                                                                    num_neighbors=num_neighbors,
                                                                    neg_sampling_ratio=neg_sampling_ratio,
                                                                    batch_size=args.batch_size, shuffle=True)
        val_ratio, test_ratio = 0.0, 0.9
        _, _, test_loader = create_data_loaders(data=test_data, val_ratio=val_ratio, test_ratio=test_ratio,
                                                                    disjoint_train_ratio=disjoint_train_ratio,
                                                                    num_neighbors=num_neighbors,
                                                                    neg_sampling_ratio=neg_sampling_ratio,
                                                                    batch_size=args.batch_size, shuffle=True)
        data = train_data
    else:
        val_ratio, test_ratio = 0.1, 0.1
        edges_coo = create_graph(df_users, df_items, user_naming, item_naming)
        data = create_hetero_graph(edges_coo=edges_coo,
                                   user_features_init=user_features_init,
                                   user_feature_size=args.num_user_features,
                                   item_feature_tensor=item_feature_tensor)
        train_loader, val_loader, test_loader = create_data_loaders(data=data, val_ratio=val_ratio, test_ratio=test_ratio,
                                                                    disjoint_train_ratio=disjoint_train_ratio,
                                                                    num_neighbors=num_neighbors,
                                                                    neg_sampling_ratio=neg_sampling_ratio,
                                                                    batch_size=args.batch_size, shuffle=True)

    data_loader = {
        'train': train_loader,
        'val':   val_loader,
        'test':  test_loader
    }

    # TODO Pre-compute GCN normalization
    # if args.gnn_model == 'GCN': data.adj_t = gcn_normalization(data.adj_t)

    # data contains the whole graph
    num_user_nodes = data["user"].num_nodes if 'emb' in args.user_input_layer else -1
    num_item_nodes = data["item"].num_nodes if 'emb' in args.item_input_layer else -1
    graph_metadata = data.metadata()

    model = Model(
        graph_metadata=graph_metadata,
        num_user_feats=args.num_user_features,
        num_item_feats=args.num_item_features,
        num_user_nodes=num_user_nodes,
        num_item_nodes=num_item_nodes,
        user_input_layer=args.user_input_layer,
        item_input_layer=args.item_input_layer,
        input_hidden_channels=args.input_hidden_channels,
        encoder=args.encoder,
        predictor=args.predictor,
        num_encoder_layers=args.encoder_layers,
        num_predictor_layers=args.predictor_layers,
        encoder_hidden_channels=args.encoder_hidden_channels,
        predictor_hidden_channels=args.predictor_hidden_channels,
        dropout=args.dropout
    )
    model = model.to(device)
    print(model)

    for run in range(args.runs):
        # model.reset_parameters()
        optimizer = torch.optim.Adam(model.parameters(), lr=args.lr)
        criterion = F.binary_cross_entropy_with_logits
        train_loss, valid_scores, test_scores = train(model, optimizer, criterion,
                                                      data_loader, device, run,
                                                      epochs, args.eval_steps)

    total_params = sum(p.numel() for param in model.parameters() for p in param)
    print(f'Total number of model parameters is {total_params}')

if __name__ == "__main__":
    main()


import logging
from typing import List

from volatility3.framework import renderers, exceptions, interfaces
from volatility3.framework.configuration import requirements
from volatility3.framework.interfaces import plugins
from volatility3.framework.layers import intel

vollog = logging.getLogger(__name__)

class Statistics(plugins.PluginInterface):
    """Lists statistics about the memory space."""

    _required_framework_version = (2, 0, 0)

    @classmethod
    def get_requirements(cls) -> List[interfaces.configuration.RequirementInterface]:
        return [
            requirements.TranslationLayerRequirement(
                name="primary",
                description="Memory layer for the kernel",
                architectures=["Intel32", "Intel64"],
            )
        ]

    def _generator(self):
        # Do mass mapping and determine the number of different layers and how many pages go to each one
        layer = self.context.layers[self.config["primary"]]

        page_count = (
            swap_count
        ) = (
            invalid_page_count
        ) = (
            large_page_count
        ) = large_swap_count = large_invalid_count = other_invalid = 0

        if isinstance(layer, intel.Intel):
            page_addr = 0
            expected_page_size = 1 << layer.bits_per_register

            while page_addr < layer.maximum_address:
                try:
                    _, _, _, page_size, layer_name = list(
                        layer.mapping(page_addr, 2 * expected_page_size)
                    )[0]
                    if layer_name != layer.config["memory_layer"]:
                        swap_count += 1
                    else:
                        page_count += 1
                    if page_size > expected_page_size:
                        large_page_count += 1
                except exceptions.SwappedInvalidAddressException as excp:
                    swap_count += 1
                    page_size = 1 << excp.invalid_bits
                    if page_size != expected_page_size:
                        large_swap_count += 1
                except exceptions.PagedInvalidAddressException as excp:
                    invalid_page_count += 1
                    page_size = 1 << excp.invalid_bits
                    if page_size != expected_page_size:
                        large_invalid_count += 1
                except exceptions.InvalidAddressException as excp:
                    other_invalid += 1
                    page_size = expected_page_size
                    vollog.debug(
                        "A non-page lookup invalid address exception occurred at: {} in layer {}".format(
                            hex(excp.invalid_address), excp.layer_name
                        )
                    )

                page_addr += page_size
                self._progress_callback(
                    (page_addr * 100) / layer.maximum_address, "Reading memory"
                )

        yield (
            0,
            (
                page_count,
                large_page_count,
                swap_count,
                large_swap_count,
                invalid_page_count,
                large_invalid_count,
                other_invalid,
            ),
        )

    def run(self):
        return renderers.TreeGrid(
            [
                ("Valid pages (all)", int),
                ("Valid pages (large)", int),
                ("Swapped Pages (all)", int),
                ("Swapped Pages (large)", int),
                ("Invalid Pages (all)", int),
                ("Invalid Pages (large)", int),
                ("Other Invalid Pages (all)", int),
            ],
            self._generator(),
        )


from django.shortcuts import render
from trino.dbapi import connect
from trino.auth import BasicAuthentication
import pandas as pd
from core.models import Item
import numpy as np
# Create your views here.

conn = connect(host="tcp.cheerful-maggot.dataos.app",
               port="7432",
               auth=BasicAuthentication(
                   'balaji', "dG9rZW5fc2NhcmNlbHlfc2VyaW91c2x5X2ZyZXNoX2tpZC43YmJmMDIwZS0xMmJhLTRkNjEtYmFmZS0zNGQzNTcxZDZkOGQ="),
               http_scheme="https",
               http_headers={"cluster-name": "minervac"}  # eg:minervaa
               )

def create_prods(request):

    skus_df = pd.read_csv('./dbmigrate/skus/skus.csv')
    qr = "SELECT * FROM redshift.retail_accelerator.product WHERE sku_id IN ({0})".format(','.join(["'"+s+"'" for s in skus_df['sku_id']]))

    cat_qr = 'SELECT * FROM redshift.retail_accelerator.product_category'

    cat_df = pd.read_sql(cat_qr, conn)

    df = pd.read_sql(qr, conn)

    rename_dict = {'sku_id': 'id',
                   'product_name': 'title',
                   'list_price': 'price',
                   'sale_price': 'discount_price',
                   'product_category_id': 'category',
                   'product_subcategory_id': 'subcat_id',
                   'product_subcategory': 'subcat',
                   'product_description': 'description'
                   }

    df_out = df.rename(columns=rename_dict)
    df_out['slug'] = df_out['id'].copy()
    #df_out['discount_price'] = df_out['price'].apply(lambda x: float("{:.2f}".format(x*np.random.uniform(low = 0.6, high = 0.95))))

    df_out = df_out[list(rename_dict.values())+['slug']]

    df_out = df_out.merge(cat_df[['product_category', 'product_category_id']], left_on='category',
                          right_on='product_category_id', how='left')

    df_out.rename(columns={'product_category': 'image'}, inplace=True)
    df_out.drop(['product_category_id'], axis=1, inplace=True)

    df_out['image'] = df_out['image'].str.replace('mens', 'men')
    df_out['image'] = df_out['image'].str.replace('Mens', 'Men')

    df_out['image'] = df_out['image'].apply(lambda x: x+'.jpeg')
    df_out['label'] = ['P']*len(df_out)

    out_records = df_out.to_dict(orient='records')
    for r in out_records:
        Item.objects.create(id=r['id'], title=r['title'], price=r['price'],
                            discount_price=r['discount_price'], category=r['category'], slug=r['slug'], subcat_id = r['subcat_id'], subcat = r['subcat'], description=r['description'], image=r['image'], label=r['label'])

    return render(request, 'db_check.html')


#! /usr/bin/env python3
import os
import requests

#List all the files in /supplier-data/descriptions
for root, dirs, files in os.walk("./supplier-data/descriptions"):
    #Loop through the list of description files of fruits
    for file_name in files:
        with open(os.path.join(root,file_name)) as file:
            #Read through lines of the description file
            reader = file.readlines()
            #Store name, weight, description and image name in a dictionary
            description = {"name": reader[0],
                        "weight": int(reader[1].split(" ")[0]),
                        "description": reader[2],
                        "image_name": file_name.split(".")[0] + ".jpeg"
                        }
            #POST the dictionary to the webserver in JSON format
            response = requests.post("http://localhost/fruits/", json=description)
            response.raise_for_status()


# ================================================================================
# This script instantiates the llm object with the relevant parameters
# ================================================================================
from langchain.llms import CTransformers

# Local CTransformers wrapper for Llama-2-7B-Chat
llm = CTransformers(
    model="/Users/deveshparagiri/Downloads/models/sage-v2-q8_0.bin",
    model_type="llama",  # Model type Llama
    config={"max_new_tokens": 256, "temperature": 0.5},
)


import aiohttp
import urllib.parse as urlparse

from typing import Optional
from middleware.config import Settings
from middleware.exception import MiddlewareException
from middleware.external.base.application import Application as BaseApplication

class Application(BaseApplication):
    @staticmethod
    async def _get_internal_schema_info_response_callback(res: aiohttp.ClientResponse):
        if res.status != 200:
            response = await res.text()
            raise MiddlewareException("获取internal schema失败, url: {}, status: {}, response: {}".format(
                res.url.human_repr(), res.status, response
            ))
        result = await res.json()
        return result["link"]

    async def _get_internal_schema_info(self, tenant: str, name: str):
        result = await self.settings.session.get(
            self.settings.newest_schema_url.format(tenant=tenant, entityType=name),
            func=self._get_internal_schema_info_response_callback,
            ssl=False, timeout=120
        )
        result = urlparse.urlparse(result).path
        return result

    async def get_newest_schema_by_unique_name(self, tenant: str, name: str) -> Optional[str]:
        result = await self._get_internal_schema_info(tenant, name)
        return "{}#/properties/data".format(result)

def get_app():
    return Application(Settings)


#!/usr/bin/env python3
#
#     CLIMPORN
#
#  Prepare 2D maps (monthly) that will later become a movie!
#  NEMO output and observations needed
#
#    L. Brodeau, January 2019
#
import sys
import glob
#import argparse as ap
import numpy as nmp
from PIL import Image
#
# ClimPorn:
from   climporn.utils import chck4f
import climporn.nemo_hboxes as nhb

#nghost = 3

# About files to use:
fpref_p = './figs/CURLOF/CURLOF_TROPICO05_NST-_ALL_'
fsuff_p = '_on2.png'

fpref_c = './figs/CURLOF/CURLOF_CALEDO10-_ALL_'
fsuff_c = '_on2.png'

fpref_o = './figs/montage_CURLOF_TROPICO05-CALEDO10_'

nratio  = 5

ncrop_chld = 3
#ncrop_chld = 1
#ncrop_chld = 3
#ncrop_chld = 10

ipc = 35 ; # location (Fortran indexing) of bottom left corner AGRIF_FixedGrids.in)
jpc = 43 ; # of the child box into parent box in Agrif ! (as in

cdate = '2016-12-22_00'

# Final box to keep (mind that for images it's flipped upside down!):
j1b = 336 ; i1b = 0
j2b = 680 ; i2b = int(16./9.*float(j2b-j1b))

# COLOR FOR THE NESTING BOX LINE:
# (0-255)     R   G   B  transparency
#rgb_f = [ 0 , 0 , 0 , 255 ] ; # solid black
rgb_f = [ 255, 237, 0 , 255 ] ; # yellow ON (255,237,0)
npf = 2 ; # number of poins for frame...

list_im_p = glob.glob(fpref_p+"*"+fsuff_p)
list_im_c = glob.glob(fpref_c+"*"+fsuff_c)

nbf = len(list_im_c)
if len(list_im_p) != nbf:
    print(' Problem different number of images between parent and child!', len(list_im_p), nbf) ; sys.exit(0)

for j in range(nbf):

    cf_prnt = list_im_p[j]
    chck4f(cf_prnt)

    cdate = str.replace( cf_prnt, fpref_p, '')
    cdate = str.replace( cdate, fsuff_p, '')

    cf_chld =  fpref_c+cdate+fsuff_c ; # that's what we expect
    chck4f(cf_chld)

    cf_out = str.replace( cf_prnt, fpref_p, fpref_o)
    cf_out = str.replace( cf_out, fsuff_p, '.png')

    #print(cf_prnt,' ',cf_chld,' ', cdate, )
    #print(cf_out)
    #sys.exit(0)

    chld = Image.open(cf_chld)
    prnt = Image.open(cf_prnt)

    (nyc,nxc,nrgb_c) = nmp.shape(chld)
    (nyp,nxp,nrgb_p) = nmp.shape(prnt)

    #print(' *** Image array shape for child  = ', nyc,nxc,nrgb_c)
    #print(' *** Image array shape for parent = ', nyp,nxp,nrgb_p)

    if nrgb_c != 4: print(' Problem #1 with your child image, not what we expected!') ; sys.exit(0)
    if nrgb_p != 4: print(' Problem #1 with your parent image, not what we expected!') ; sys.exit(0)

    if nxp%nratio!=0 or nyp%nratio!=0: print(' Problem #1 with your parent image, it shoud be a multiple of '+str(nratio)+'!') ; sys.exit(0)

    xchld = nmp.array(chld)
    xprnt = nmp.array(prnt)

    chld.close()
    prnt.close()

    print("xchld", nmp.shape(xchld))
    print("xprnt", nmp.shape(xprnt))
    #print(xprnt[0,0,:])
    #print(xprnt[100,100,:])

    #ip = (ipc - 1) * nratio + nghost
    #jp = (jpc - 1) * nratio + nghost
    ip = (ipc + 1) * nratio + 2  # I have no fucking clue why ????
    jp = (jpc + 1) * nratio + 2  # I have no fucking clue why ????
    #ip = ipc * nratio
    #jp = jpc * nratio

    # Child array we keep after croping:
    if ncrop_chld >= 1:
        XC = xchld[ncrop_chld:-ncrop_chld,ncrop_chld:-ncrop_chld]
    else:
        XC = xchld[:,:]
    (Ny,Nx,Nr) = nmp.shape(XC)

    #print(" shape XC =", Ny,Nx,Nr)

    # Drawing  frame:
    for i in range(4):
        XC[     0:npf,:,i] = rgb_f[i]
        XC[Ny-npf:Ny ,:,i] = rgb_f[i]
        XC[:,     0:npf,i] = rgb_f[i]
        XC[:,Nx-npf:Nx ,i] = rgb_f[i]

    j1 = nyp-jp-Ny - ncrop_chld
    i1 = ip        + ncrop_chld
    xprnt[j1:j1+Ny,i1:i1+Nx,:] = XC[:,:,:]

    image_out  = Image.fromarray(xprnt[j1b:j2b,i1b:i2b,:])

    #cf_chld = './child.png'
    ## Then save it:
    #image_chld.save(cf_chld)
    #print(' *** Image '+cf_chld+' saved!\n')

    # Then save it:
    image_out.save(cf_out)
    print(' *** Image '+cf_out+' saved!\n')

    del XC, xprnt, xchld


import csv
from face import Face

data = csv.DictReader(open("fer2013/fer2013.csv"))
faces_train = []
faces_val = []
faces_test = []

for row in data:
    if row.get("Usage") == "Training":
        faces_train += [Face(row)]
    elif row.get("Usage") == "PublicTest":
        faces_val += [Face(row)]
    elif row.get("Usage") == "PrivateTest":
        faces_test += [Face(row)]

print((len(faces_train), len(faces_val), len(faces_test)))


"""create auth tables

Revision ID: 6e0daa4be1f8
Revises: cb42b1c187d9
Create Date: 2019-09-26 09:17:12.642904

"""
from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = '6e0daa4be1f8'
down_revision = 'cb42b1c187d9'
branch_labels = None
depends_on = None

def upgrade():
    op.create_table(
        'users',
        sa.Column('id', sa.Integer, nullable=False),
        sa.Column('login', sa.String(256), nullable=False),
        sa.Column('passwd', sa.String(256), nullable=False),
        sa.Column('is_superuser', sa.Boolean, nullable=False,
                  server_default='FALSE'),
        sa.Column('disabled', sa.Boolean, nullable=False,
                  server_default='FALSE'),

        # indices
        sa.PrimaryKeyConstraint('id', name='user_pkey'),
        sa.UniqueConstraint('login', name='user_login_key'),
    )

    op.create_table(
        'permissions',
        sa.Column('id', sa.Integer, nullable=False),
        sa.Column('user_id', sa.Integer, nullable=False),
        sa.Column('perm_name', sa.String(64), nullable=False),

        # indices
        sa.PrimaryKeyConstraint('id', name='permission_pkey'),
        sa.ForeignKeyConstraint(['user_id'], ['users.id'],
                                name='user_permission_fkey',
                                ondelete='CASCADE'),
    )

def downgrade():
    op.drop_table('users')
    op.drop_table('permissions')


from flask import Flask
from flask import request
import urllib.request
import re
from requests_html import HTMLSession

app = Flask(__name__)

#Makes all responses plaintext
@app.after_request
def treat_as_plain_text(response):
    response.headers["content-type"] = "text/plain"
    return response

@app.route('/')
def getList():
    url = request.args.get("listurl")
    return getPage(url)

@app.route('/sample')
def getSampleList():
    return getPage("https://letterboxd.com/tommypedersen/list/tommys-movie-collection/page/3/")

#url = "https://letterboxd.com/tommypedersen/list/2016/"
url = "https://letterboxd.com/tommypedersen/list/tommys-movie-collection/page/3/"

session = HTMLSession()
resp = session.get(url)

def getPage(url):
    try:
        response = urllib.request.urlopen(url)
        print("Established connection")
        data = response.read()
        page = data.decode("utf8")

        #Pure html solution.
        films = list(re.findall("alt=\"(.*?)\"", page))
        films.pop(0)
        i = 1
        listStr = ""
        for movie in films:
            print("[{}] {}".format(i, movie))
            listStr += movie + "\n"
            i += 1

        response.close()
        return listStr

    except Exception as e:
        print(e)
        print("Failed to retrieve info from webpage")
        return set()

myStr = getPage(url)


from sklearn.preprocessing import normalize
from sklearn.neighbors import NearestNeighbors
from tqdm import tqdm
import os
import pickle
from datetime import datetime

if not os.path.exists("saves/crude_embeddings.pkl"):
    raise ValueError("Please run --process first to generate embeddings")
else:
    with open("saves/crude_embeddings.pkl", "rb") as f:
        crude_embeddings = pickle.load(f)

def normalize_embeddings():
    middle_data = {}
    for ticker, date in crude_embeddings.keys():
        if ticker not in middle_data:
            middle_data[ticker] = {}
        middle_data[ticker][date] = crude_embeddings[(ticker, date)]

    for ticker in middle_data:
        # sort the dates in ascending order
        sorted_dates = sorted(
            middle_data[ticker], key=lambda x: datetime.strptime(x, "%Y-%m-%d")
        )

        # create a new dictionary with sorted dates
        sorted_data = {date: middle_data[ticker][date] for date in sorted_dates}

        # replace the original dictionary with the sorted one
        middle_data[ticker] = sorted_data

    normalized_embeddings = []
    for ticker in middle_data.keys():
        for date in list(middle_data[ticker].keys())[:-30]:
            normalized_embeddings.append(middle_data[ticker][date])

    normalized_embeddings = normalize(normalized_embeddings)

    embeddings_keys = {}
    i = 0
    for ticker in tqdm(middle_data.keys(), desc="Creating embeddings keys"):
        for date in list(middle_data[ticker].keys())[:-30]:
            embeddings_keys[tuple(normalized_embeddings[i])] = (ticker, date)
            i += 1

    return normalized_embeddings, embeddings_keys

def knn(normalized_embeddings, embeddings_keys):
    nbrs = NearestNeighbors(n_neighbors=1, algorithm="ball_tree")
    save = [nbrs, normalized_embeddings, embeddings_keys]
    # Save the model to disk
    with open("saves/knn.pkl", "wb") as f:
        pickle.dump(save, f)


import pathlib

import numpy as np
import pytest

import meshio

from . import helpers

@pytest.mark.parametrize(
    "mesh",
    [
        helpers.empty_mesh,
        helpers.tet_mesh,
        helpers.hex_mesh,
        helpers.tet_mesh,
        helpers.add_cell_sets(helpers.tet_mesh),
    ],
)
@pytest.mark.parametrize("binary", [False, True])
def test(mesh, binary, tmp_path):
    # mesh.write("out.f3grid")
    helpers.write_read(
        tmp_path,
        lambda f, m: meshio.flac3d.write(f, m, binary=binary),
        meshio.flac3d.read,
        mesh,
        1.0e-15,
    )

@pytest.mark.parametrize(
    "filename",
    ["flac3d_mesh_ex.f3grid", "flac3d_mesh_ex_bin.f3grid"],
)
def test_reference_file(filename):
    this_dir = pathlib.Path(__file__).resolve().parent
    filename = this_dir / "meshes" / "flac3d" / filename

    mesh = meshio.read(filename)

    # points
    assert np.isclose(mesh.points.sum(), 307.0)

    # cells
    ref_num_cells = [
        ("quad", 15),
        ("triangle", 3),
        ("hexahedron", 45),
        ("pyramid", 9),
        ("hexahedron", 18),
        ("wedge", 9),
        ("hexahedron", 6),
        ("wedge", 3),
        ("hexahedron", 6),
        ("wedge", 3),
        ("pyramid", 6),
        ("tetra", 3),
    ]
    assert [
        (cell_block.type, len(cell_block)) for cell_block in mesh.cells
    ] == ref_num_cells
    # Cell sets
    for arr in mesh.cell_sets.values():
        assert len(arr) == 12
    assert [len(arr) for arr in mesh.cell_sets.values()] == [12, 12, 12, 12, 12]


import pygame

class playerProjectiles(pygame.sprite.Sprite):
    def __init__(self, x):
        super().__init__()
        self.entity = pygame.image.load("../assets/playerbullet.png").convert_alpha()
        self.rect = self.entity.get_rect()
        self.rect = self.rect.move(x.rect.left+29, x.rect.top-50)
        self.entity = pygame.transform.scale(self.entity, (45,19))
        self.entity = pygame.transform.rotate(self.entity, 90)
        self.mask = pygame.mask.from_surface(self.entity)
        self.playershot = pygame.mixer.music.load('../assets/playershot.mp3')
        self.playershot = pygame.mixer.music.play(0)
    def checkBounds(self):
        if self.rect.top <= 0:
            return 1
class playerProjecticleList:
    def __init__(self, player):
        self.playerProjectileArray = []
        self.player = player
    def createBullet(self):
        self.playerProjectileArray.append(playerProjectiles(self.player))
    def moveAll(self):
        bullet = 0
        for bullet in self.playerProjectileArray:
            bullet.rect = bullet.rect.move(0, -15)

            if bullet.checkBounds() == 1:
                self.playerProjectileArray.remove(bullet)