import asyncio
import logging
from typing import Mapping, Optional
from opentrons.drivers.mag_deck import (
    SimulatingDriver,
    MagDeckDriver,
    AbstractMagDeckDriver,
)
from opentrons.drivers.rpi_drivers.types import USBPort
from ..execution_manager import ExecutionManager
from . import update, mod_abc, types

log = logging.getLogger(__name__)

MAX_ENGAGE_HEIGHT = {
    # Distance from home position.
    # Measured in model-specific units (half-mm for GEN1, mm for GEN2).
    "magneticModuleV1": 45,
    "magneticModuleV2": 25,
}

# Measured in model-specific units (half-mm for GEN1, mm for GEN2).
# TODO(mc, 2022-06-13): the value for gen1 is off by ~1.5 mm
# The correct value is ~8.0 half-mm (4.0 mm)
# https://opentrons.atlassian.net/browse/RET-1242
OFFSET_TO_LABWARE_BOTTOM = {"magneticModuleV1": 5, "magneticModuleV2": 2.5}

def engage_height_is_in_range(model: str, height: float) -> bool:
    """Return whether or not a height would be valid to pass to `MagDeck.engage()`.

    Args:
        model: The model of Magnetic Module for which you want to check
            the engage height.
        height: A height that you would provide to `MagDeck.engage()`.
    """
    return 0 <= height <= MAX_ENGAGE_HEIGHT[model]

class MagDeck(mod_abc.AbstractModule):
    """Hardware control interface for an attached Temperature Module."""

    MODULE_TYPE = types.ModuleType.MAGNETIC
    FIRST_GEN2_REVISION = 20

    @classmethod
    async def build(
        cls,
        port: str,
        usb_port: USBPort,
        execution_manager: ExecutionManager,
        hw_control_loop: asyncio.AbstractEventLoop,
        poll_interval_seconds: Optional[float] = None,
        simulating: bool = False,
        sim_model: Optional[str] = None,
    ) -> "MagDeck":
        """Factory function."""
        driver: AbstractMagDeckDriver
        if not simulating:
            driver = await MagDeckDriver.create(port=port, loop=hw_control_loop)
        else:
            driver = SimulatingDriver(sim_model=sim_model)

        mod = cls(
            port=port,
            usb_port=usb_port,
            execution_manager=execution_manager,
            hw_control_loop=hw_control_loop,
            device_info=await driver.get_device_info(),
            driver=driver,
        )
        return mod

    def __init__(
        self,
        port: str,
        usb_port: USBPort,
        execution_manager: ExecutionManager,
        hw_control_loop: asyncio.AbstractEventLoop,
        driver: AbstractMagDeckDriver,
        device_info: Mapping[str, str],
    ) -> None:
        """Constructor"""
        super().__init__(
            port=port,
            usb_port=usb_port,
            hw_control_loop=hw_control_loop,
            execution_manager=execution_manager,
        )
        self._device_info = device_info
        self._driver = driver
        self._current_height = 0.0

    async def cleanup(self) -> None:
        await self._driver.disconnect()

    @classmethod
    def name(cls) -> str:
        """Get the module name."""
        return "magdeck"

    def firmware_prefix(self) -> str:
        """The prefix used for looking up firmware"""
        return "magnetic-module"

    def model(self) -> str:
        """Get the model."""
        return self._model_from_revision(self._device_info.get("model"))

    def bootloader(self) -> types.UploadFunction:
        """Get the bootloating method."""
        return update.upload_via_avrdude

    async def calibrate(self) -> None:
        """Calibration involves probing for top plate to get the plate height."""
        await self.wait_for_is_running()
        await self._driver.probe_plate()
        # return if successful or not?

    # TODO(mc, 2022-09-23): refactor this method to take real mm,
    # hardware API should abstract away the idea of "short millimeters"
    # https://opentrons.atlassian.net/browse/RET-1242
    async def engage(
        self,
        height: Optional[float] = None,
        height_from_base: Optional[float] = None,
        must_be_running: bool = True,
    ) -> None:
        """Move the magnet to a specific height, measured from home position.

        The units of position depend on the module model.
        For GEN1, it's half millimeters ("short millimeters").
        For GEN2, it's millimeters.
        """
        if height is None:
            assert height_from_base is not None, "An engage height must be specified"
            height = height_from_base + OFFSET_TO_LABWARE_BOTTOM[self.model()]

        if must_be_running:
            await self.wait_for_is_running()
        if not engage_height_is_in_range(self.model(), height):
            raise ValueError(
                f"Invalid engage height for {self.model()}: {height}. "
                f"Must be 0 - {MAX_ENGAGE_HEIGHT[self.model()]}."
            )
        await self._driver.move(height)
        self._current_height = await self._driver.get_mag_position()

    async def deactivate(self, must_be_running: bool = True) -> None:
        """Home the magnet."""
        if must_be_running:
            await self.wait_for_is_running()
        await self._driver.home()
        await self.engage(0.0, must_be_running=must_be_running)

    @property
    def current_height(self) -> float:
        """Get the current height."""
        return self._current_height

    @property
    def device_info(self) -> Mapping[str, str]:
        """

        Returns: a dict
            { 'serial': 'abc123', 'model': '8675309', 'version': '9001' }

        """
        return self._device_info

    @property
    def status(self) -> types.MagneticStatus:
        if self.current_height > 0:
            return types.MagneticStatus.ENGAGED
        else:
            return types.MagneticStatus.DISENGAGED

    @property
    def engaged(self) -> bool:
        if self.current_height > 0:
            return True
        else:
            return False

    @property
    def live_data(self) -> types.LiveData:
        return {
            "status": self.status,
            "data": {"engaged": self.engaged, "height": self.current_height},
        }

    @property
    def is_simulated(self) -> bool:
        return isinstance(self._driver, SimulatingDriver)

    # Internal Methods

    async def prep_for_update(self) -> str:
        await self._driver.enter_programming_mode()
        new_port = await update.find_bootloader_port()
        return new_port or self.port

    @staticmethod
    def _model_from_revision(revision: Optional[str]) -> str:
        """Defines the revision -> model mapping"""
        if not revision or "v" not in revision:
            log.error(f"bad revision: {revision}")
            return "magneticModuleV1"
        try:
            revision_num = float(revision.split("v")[-1])
        except (ValueError, TypeError):
            log.exception("bad revision: {revision}")
            return "magneticModuleV1"
        if revision_num < MagDeck.FIRST_GEN2_REVISION:
            return "magneticModuleV1"
        else:
            return "magneticModuleV2"


import itertools

filepath = '/home/bigdata/PycharmProjects/LDA/PyLDA/pubmed_bioontology/data/PreLresults.txt'

# convert the data into list
def read_words(words_file):
    with open(words_file, 'r') as f:
        return list(itertools.chain.from_iterable(line.split("\n") for line in f))

mylist = read_words(filepath)

# covert all list of elements to lower case
lowercaselist = []
for x in mylist:
    y = x.lower()
    lowercaselist.append(y)

# get only unique values from above list
uniqueoutput = []
for x in lowercaselist:
    if x not in uniqueoutput:
        uniqueoutput.append(x)

# sort the unique output

# convert the sorted output to string
output = '\n'.join(uniqueoutput)

# write the output to file
f = open("data/unique.txt", "a+")
f.write("%s" % output)
f.close()


from skimage.transform import hough_line, hough_line_peaks
from skimage.feature import canny
import matplotlib.pyplot as plt
from skimage.draw import line
from PIL import Image as im
from matplotlib import cm
from skimage import data
import numpy as np
import imageio
import sys
import os

class edgedetector:
  ''' A class implementation for line detection and blurring image if line is detected '''

  def open_image(self,imgpath) -> np.array:
    ''' a function that takes the image path,load the raw image and returns a numpy array '''
    assert os.path.exists(imgpath),'please enter a valid path'
    img   = np.array(im.open(imgpath))
    return img

  def image_shape(self,imgpath) -> tuple:
    ''' A function to return the iamge shape'''
    assert os.path.exists(imgpath),'please enter a valid path'
    return imageio.imread(imgpath).shape

  def display_img(self,img) -> None:
    imgdata = im.fromarray(img)
    plt.imshow(imgdata, cmap=plt.cm.gray)

  #Blurring an image by applying a gaussian filter
  def gaussian_filter(self,img) -> np.array:
    ken = np.array([[1.0,2.0,1.0], [2.0,4.0,2.0], [1.0,2.0,1.0]])
    ken = ken / np.sum(ken)
    arraylist = []
    for y in range(3):
        temparray = np.copy(img)
        temparray = np.roll(temparray, y - 1, axis=0)
        for x in range(3):
            temparray_X = np.copy(temparray)
            temparray_X = np.roll(temparray_X, x - 1, axis=1)*ken[y,x]
            arraylist.append(temparray_X)

    arraylist   = np.array(arraylist)
    img         = np.sum(arraylist, axis=0)
    return img/255.0

  def rgb2gray(self,gray_img) -> np.array:
    gray_img = np.zeros(img.shape)
    R = np.array(img[:, :, 0])
    G = np.array(img[:, :, 1])
    B = np.array(img[:, :, 2])

    R = (R *.299)
    G = (G *.587)
    B = (B *.114)

    Avg = (R+G+B)
    gray_img = img

    for i in range(3):
        gray_img[:,:,i] = Avg

    return gray_img[:,:,0]

  def binarize_img(self,grey_img,threshold=128,maxval=255) -> np.array:
    img   = (grey_img>threshold)*maxval
    return img.astype(np.uint8)

  def hough_line(self,img):
    # Rho and Theta ranges
    thetas = np.deg2rad(np.arange(-90.0, 90.0))
    width, height = img.shape
    diag_len = int(np.ceil(np.sqrt(width * width + height * height)))   # max_dist
    rhos = np.linspace(-diag_len, diag_len, diag_len * 2)

    # Cache some resuable values
    cos_t = np.cos(thetas)
    sin_t = np.sin(thetas)
    num_thetas = len(thetas)

    # Hough accumulator array of theta vs rho
    accumulator = np.zeros((2 * diag_len, num_thetas), dtype=np.uint64)
    y_idxs, x_idxs = np.nonzero(img)  # (row, col) indexes to edges

    # Vote in the hough accumulator
    for i in range(len(x_idxs)):
      x = x_idxs[i]
      y = y_idxs[i]

      for t_idx in range(num_thetas):
        # Calculate rho. diag_len is added for a positive index
        rho = round(x * cos_t[t_idx] + y * sin_t[t_idx]) + diag_len
        accumulator[rho, t_idx] += 1

    return accumulator, thetas, rhos

if __name__ == '__main__':
  img_path = sys.argv[1]
  ed  = edgedetector()
  img = ed.open_image(img_path)
  img = ed.rgb2gray(img)
  img = ed.binarize_img(img,threshold=200)
  ed.display_img(img)
  h, theta, d = ed.hough_line(img)

  fig, axes = plt.subplots(1, 3, figsize=(15, 6))
  ax = axes.ravel()

  ax[0].imshow(img, cmap=cm.gray)
  ax[0].set_title('Input image')
  ax[0].set_axis_off()

  angle_step = 0.5 * np.diff(theta).mean()
  d_step = 0.5 * np.diff(d).mean()
  bounds = [np.rad2deg(theta[0] - angle_step),
            np.rad2deg(theta[-1] + angle_step),
            d[-1] + d_step, d[0] - d_step]
  ax[1].imshow(np.log(1 + h), extent=bounds, cmap=cm.gray, aspect=1 / 1.5)
  ax[1].set_title('Hough transform')
  ax[1].set_xlabel('Angles (degrees)')
  ax[1].set_ylabel('Distance (pixels)')
  ax[1].axis('image')

  ax[2].imshow(img, cmap=cm.gray)
  ax[2].set_ylim((img.shape[0], 0))
  ax[2].set_axis_off()
  ax[2].set_title('Detected lines')

  plt.tight_layout()
  plt.show()


import numpy as np
import time
import datetime
# Creating 5x4 array
"""array1 = np.arange(1000)
array2 = np.arange(1000)

tempo_inicial = time.time()

for i in array1:
    for j in array2:
        print(i+j)

tempo_final = time.time()

resultado = tempo_final - tempo_inicial
print(round(resultado, 2))"""

time1 = datetime.datetime.now()

lista1 = list(range(1, 1000))
lista2 = list(range(1, 1000))

for i in range(len(lista1)):
    for j in range(len(lista2)):
        print(i+j)

time = datetime.datetime.now()
print(time-time1)


# -*- coding: utf-8 -*-
import sys
from os import path
import logging
import re
import numpy as np
from tqdm import tqdm

sys.path.append('./src/')
from utils import argparser as parser
from utils import utils

def get_covariances(embs_full, min_samples):
    covs, var = {}, {}
    for word, embs in embs_full.items():
        if embs.shape[0] > min_samples and len(word) > 1:
            covs[word] = {
                'covariance': np.cov(embs, rowvar=False),
                'n_samples': embs.shape[0]
            }
            var[word] = {
                'variance': np.var(embs, axis=0, ddof=1),
                'n_samples': embs.shape[0]
            }
    return covs, var

def save_covs(covs, file_path, file_key):
    tqdm.write('\tSaving file')
    filename = path.join(file_path, 'covs_%s.pickle.bz2' % (file_key))
    utils.write_pickle(filename, covs)

def get_filekeys(filename, data_type):
    base_name = '%s_%s.pickle.bz2' % (data_type, r'(.+?)')
    filename = filename.split('/')[-1]
    m = re.match(base_name, filename)
    return m.group(1)

def main():
    args = parser.parse_args()
    logging.info(args)

    var = {}
    filenames = utils.get_filenames(args.embeddings_merged_path)
    for i, filename in tqdm(list(enumerate(filenames))):
        tqdm.write('%d/%d Reading file: %s. Length of vars: %d' %
                   (i + 1, len(filenames), filename, len(var)))
        embs = utils.read_pickle(filename)
        covs, var_temp = get_covariances(embs, args.min_samples)
        var = dict(var, **var_temp)

        if len(covs) > 0:
            file_key = get_filekeys(filename, 'embs')
            save_covs(covs, args.embeddings_covariance_path, file_key)

    utils.write_pickle(args.embeddings_variance_file, var)

if __name__ == '__main__':
    main()


#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author: gaopeng
# @Date:   2018-01-22 16:52:53
# @Last modified by:   gaopeng
# @Last modified time: 2018-01-24 15:50:33

import logging; logging.basicConfig(level=logging.INFO)

import os, time
from datetime import datetime

from transwarp import db
from transwarp.web import WSGIApplication, Jinja2TemplateEngine

from config import configs

def datetime_filter(t):
    delta = int(time.time() - t)
    if delta < 60:
        return u'1分钟前'
    if delta < 3600:
        return u'%s分钟前' % (delta // 60)
    if delta < 86400:
        return u'%s小时前' % (delta // 3600)
    if delta < 604800:
        return u'%s天前' % (delta // 86400)
    dt = datetime.fromtimestamp(t)
    return u'%s年%s月%s日' % (dt.year, dt.month, dt.day)

db.create_engine(**configs.db)
wsgi = WSGIApplication(os.path.dirname(os.path.abspath(__file__)))
template_engine = Jinja2TemplateEngine(os.path.join(os.path.dirname(os.path.abspath(__file__)), 'templates'))

wsgi.template_engine = template_engine
# 把filter添加到jinjia2，filter名称为datetime，filter本身是一个函数对象:
template_engine.add_filter('datetime', datetime_filter)

import urls

wsgi.add_interceptor(urls.user_interceptor)
wsgi.add_interceptor(urls.manage_interceptor)
wsgi.add_module(urls)

if __name__ == '__main__':
    wsgi.run(10088, host='0.0.0.0')
else:
    application = wsgi.get_wsgi_application()


"""Extended base functionality for Django framework."""

from __future__ import unicode_literals

__version__ = "0.2.2"

from django.utils.functional import lazy
from django.core.urlresolvers import reverse, reverse_lazy
from django.contrib.auth.models import Group
from django.conf import settings
from django.middleware import csrf

def get_or_create_csrf_token(request):
    token = request.META.get('CSRF_COOKIE', None)
    if token is None:
        token = csrf._get_new_csrf_key()
        request.META['CSRF_COOKIE'] = token
        request.META['CSRF_COOKIE_USED'] = True
    return token

def get_config(key, default=None):
    """
    Get settings from django.conf if exists,
    return default value otherwise

    example:

    ADMIN_EMAIL = get_config('ADMIN_EMAIL', 'default@email.com')
    """
    return getattr(settings, key, default)

def get_group(name):
    """
    Get a django.contrib.auth group model object by group name.
    """
    return Group.objects.filter(name=name).first()

def user_has_group(user, group, superuser_skip=True):
    """
    Check if a user is in a certaing group.
    By default, the check is skipped for superusers.
    """

    if user.is_superuser and superuser_skip:
        return True

    return user.groups.filter(name=group).exists()

def resolve_class(class_path):
    """
    Load a class by a fully qualified class_path,
    eg. myapp.models.ModelName
    """

    modulepath, classname = class_path.rsplit('.', 1)
    module = __import__(modulepath, fromlist=[classname])
    return getattr(module, classname)


from importlib.abc import MetaPathFinder
from _frozen_importlib_external import PathFinder
import sys
import pathlib
import warnings
from typing import List, Any, Union, Iterable

PYODIDE = True

try:
    from js import XMLHttpRequest
except:
    warnings.warn("Pyodide not found, running in native mode")
    import requests

    PYODIDE = False

class PyFinder(MetaPathFinder):
    """Meta path finder for Pyodide which supports importing external Python files"""

    def __init__(
        self,
        import_path: Union[str, List[str]] = "",
        download_path: str = "",
        modules: Iterable[str] = None,
    ):
        self.import_path = self._prepare_import_path(import_path)
        self.download_path = self._to_abspath(download_path)
        self.modules = set(modules) if modules is not None else None

    def register(self):
        """
        Register the PyFinder to sys.meta_path
        """
        if not self._registered():
            sys.meta_path.append(self)

    def unregister(self):
        """
        Unregister the PyFinder from sys.meta_path
        """
        if self._registered():
            sys.meta_path.remove(self)

    def _registered(self):
        """
        Check whether the PyFinder is registered in sys.meta_path
        """
        return self in sys.meta_path

    def __enter__(self):
        self.register()

    def __exit__(self, type, value, traceback):
        self.unregister()

    def _prepare_import_path(self, paths: Union[str, List[str]]):
        if isinstance(paths, str):
            return [paths.rstrip("/") + "/"]
        elif isinstance(paths, list):
            return [path.rstrip("/") + "/" for path in paths]

    def _to_abspath(self, path: str):
        return pathlib.Path(path).resolve()

    def add_module(self, module: Union[str, List[str]]):
        """
        Add new module(s) to the whitelist which PyFinder import hook can import
        """
        if self.modules is None:
            self.modules = set()

        if isinstance(module, str):
            self.modules.add(module)
        else:
            self.modules.update(module)

    def available_modules(self):
        """
        Get the list of whitelisted modules which PyFinder import hook can import
        """
        return self.modules

    @staticmethod
    def invalidate_caches():
        """Call the invalidate_caches() method on all path entry finders
        stored in sys.path_importer_caches (where implemented)."""
        for name, finder in list(sys.path_importer_cache.items()):
            if finder is None:
                del sys.path_importer_cache[name]
            elif hasattr(finder, "invalidate_caches"):
                finder.invalidate_caches()

    def find_spec(self, fullname: str, path: List[str], target: Any = None):
        # Should find python packages according to PEP-420.
        raise NotImplementedError

class PyHTTPFinder(PyFinder):
    """Meta path finder for importing python files through HTTP(s)"""

    def __init__(
        self,
        import_path: Union[str, List[str]] = "",
        download_path: str = "",
        modules: Iterable[str] = None,
    ):
        super().__init__(import_path, download_path, modules)

    def _get(self, url: str):
        """
        Download python script through HTTP(s)
        """
        if PYODIDE:
            req = XMLHttpRequest.new()
            req.open("GET", url, False)
            req.send(None)
            return (req.status, req.responseText)
        else:
            # This enables testing this module outside of Pyodide
            resp = requests.get(url)
            return (resp.status_code, resp.text)

    def find_spec(self, fullname: str, path: List[str], target: Any = None):
        # Search order:
        # 1. <directory>/foo/__init__.py
        # 2. <directory>/foo.py
        # (-) Not supported: binary formats (pyc, pyd, so)
        # (-) Not supported: Namespace packages without __init__.py

        # module1.module2 ==> module1/module2
        modules = fullname.split(".")
        if self.modules is not None and modules[0] not in self.modules:
            return None

        fullname_aspath = fullname.replace(".", "/")

        import_subpaths = [
            f"{fullname_aspath}/__init__.py",  # Regular package
            f"{fullname_aspath}.py",  # Module package
        ]

        for base_path in self.import_path:
            for import_subpath in import_subpaths:
                status_code, content = self._get(f"{base_path}{import_subpath}")
                if status_code != 200:
                    continue

                package_path = self.download_path / import_subpath
                package_path.parent.mkdir(parents=True, exist_ok=True)
                with package_path.open("w") as f:
                    f.write(content)

                # We need to invalidate caches since PathFinder have already failed once and it is cached.
                self.invalidate_caches()
                return PathFinder.find_spec(fullname, path, target)

        return None


"""
    Main
"""

import json

from Modules.smb_client import SmbClient
from Modules.archive import Archive
from Modules.mail import Mail

CONFIG = "config.json"

client = SmbClient(CONFIG)
archive = Archive(CONFIG)
mail = Mail(CONFIG)

with open(CONFIG, encoding="utf8") as conf:
    data = json.load(conf)
    mail_json = data["mail"]

def main():
    """
    Main script of this project
    """
    body = ""
    subject=""

    archive.clear_log_file()
    archive.download_file()
    archive.extract_zip()
    archive.check_file()
    archive.create_tgz()
    archive.clean_zip()
    archive.clean_file()
    archive.send_tgz(client)
    archive.clean_tgz()
    archive.clean_server(client)
    body = archive.read_log_file()
    subject = archive.read_log_file_first_line()

    if mail_json["send_mode"] == "on":
        if mail_json["attach_mode"] == "on":
            mail.send_email_ssl_with_log(body, subject)
        else:
            mail.send_email_ssl(body, subject)

    archive.delete_log_file()

if __name__ == "__main__":
    main()


import tensorflow as tf
import matplotlib.pyplot as plt
import numpy as np

mnist= tf.keras.datasets.mnist

(x_train,y_train), (x_test,y_test) =mnist.load_data()

##### Normalize input features
x_train = tf.keras.utils.normalize(x_train,axis=1)
x_test = tf.keras.utils.normalize(x_test,axis=1)

##### Define NN Model
model = tf.keras.models.Sequential()
model.add(tf.keras.layers.Flatten())
model.add(tf.keras.layers.Dense(128, activation=tf.nn.relu))
model.add(tf.keras.layers.Dense(128, activation=tf.nn.relu))
model.add(tf.keras.layers.Dense(10, activation=tf.nn.softmax))

model.compile(optimizer='adam',loss='sparse_categorical_crossentropy', metrics=['accuracy'])

##### Train
model.fit(x_train,y_train,epochs=2)

##### Save Model

model.save('ganjNNModel.model')

##### Evaluate NNModel with test data
ganj_model= tf.keras.models.load_model('ganjNNModel.model')

val_loss, val_accuracy = ganj_model.evaluate(x_test,y_test)

##### Prediction
predictions = model.predict([x_test])
print("predicted Class is :")
print (np.argmax(predictions[5]))

plt.imshow(x_train[5])
plt.show()


from flask import request
from flask import Blueprint
import pdb
from . import db
import face_recognition
import pickle

bpAñadir = Blueprint('anadir', __name__)
@bpAñadir.route('/add', methods = ['POST'])
def add():

    if request.method == 'POST':
        # Desde la parte de params de postman, usamos args
        # id = request.args.get('idfoto')

        if 'file' not in request.files or 'iduser' not in request.form or 'idfoto' not in request.form:
            return db.error_400, 400

        idUser = request.form.get("iduser")
        idFoto = request.form.get("idfoto")
        file = request.files['file']

        if not db.allowed_file(file.filename):
            return db.error_415, 415

        #Cargamos la imagen en un array numpy
        img = face_recognition.load_image_file(file)

        #Puede ser que no se detecten caras en la imagen
        try:
            img_encoding = face_recognition.face_encodings(img)[0]
        except IndexError as e:
            print(e)
            return db.error_400_1, 400

        cur = db.get_db().cursor()

        fila = cur.execute("SELECT * FROM users WHERE idUser=" + idUser + " AND idFoto=" + idFoto).fetchall()
        if len(fila) != 0:
            cur.execute("DELETE FROM users WHERE  idUser=" + idUser + " AND idFoto=" + idFoto)
            cur.execute("INSERT INTO users (idUser, idFoto, stringFoto) VALUES (?,?,?)",
                    (idUser, idFoto, pickle.dumps(img_encoding)))
            db.get_db().commit()
            cur.close()
            return db.update, 200
        else:
            cur.execute("INSERT INTO users (idUser, idFoto, stringFoto) VALUES (?,?,?)",
                    (idUser, idFoto, pickle.dumps(img_encoding)))
            db.get_db().commit()
            cur.close()
            return db.add, 201


#=================================================
# external imports
#=================================================

from maya.OpenMaya import MGlobal
from pymel.core.animation import parentConstraint
from pymel.core.general import selected
from pymel.core.language import mel
import pymel.core.nodetypes as nt

#=================================================
# classes
#=================================================

class NameHelper(object):
	"""
	Helps establish a naming convention with the use of string formatting and regular
	expression matching and replacing.
	"""

	# TODO: Load the IkSpringSolver plugin
	# mod = LazyLoadModule('IkSpringSolver', globals())
	# sys.modules['IkSpringSolver'] = mod

	ntypes = { \
		nt.AimConstraint: 'AC',
		'bindJoint': 'BJ',
		'buffer': 'BUF',  # Same as NULL
		nt.Condition: 'CND',
		'control': 'CTRL',
		'controlJoint': 'JNT',
		nt.CurveInfo: 'CI',
		'distanceDimension': 'DD',
		'driveJoint': 'DJ',
		nt.IkEffector: 'EFF',
		nt.Follicle: 'FOL',
		nt.GeometryConstraint: 'GC',
		'group': 'GRP',
		nt.HairSystem: 'HS',
		'ikJoint': 'IKJ',
		nt.IkRPsolver: 'IKH',
		nt.IkSCsolver: 'IKH',
		nt.IkSplineSolver: 'IKH',
		#nt.IkSpringSolver: 'IKH',
		nt.Joint: 'JNT',
		nt.Locator: 'LOC',
		'module': 'MOD',
		nt.MultiplyDivide: 'MD',
		nt.NormalConstraint: 'NC',
		'null': 'NUL',
		nt.NurbsCurve: 'CRV',
		nt.OrientConstraint: 'OC',
		nt.ParentConstraint: 'PC',
		nt.PlusMinusAverage: 'PMA',
		nt.PointConstraint: 'XC',
		nt.PointOnPolyConstraint: 'POP',
		'poleVector': 'PV',
		nt.PoleVectorConstraint: 'PVC',
		'rotation': 'ROT',
		nt.SkinCluster: 'SC'
	}

	def __init__(self, *args, **kwargs):
		self.formats = None
		self.pattern = None
		self._segments = {}
		us = self.update_segment
		pat = re.split('\<(nt|s|ss|n|ns)\>',
			kwargs.pop('pat', '<nt>_<s><ss>_<n><ns>'))
		frags = dict( \
			nt=('s', '[A-Z]{2,4}'),
			s=('s', '[A-Z]'),
			ss=('d', '\d'),
			n=('s', '[A-Z][A-Za-z]*'),
			ns=(['02d','s'], '(\d{2}|Tip)'))
		for i in range(1, len(pat)-1, 2):
			key = pat[i]
			us(key, frags[key][0], frags[key][1], pat[i+1])
		del us, pat, frags

	def update_segment(self, key, fmt, pat, seg=''):
		order = len(self._segments)
		segment = {key: (order, fmt, pat, seg)}
		self._segments.update(segment)
		self.is_compiled = False
		self._pattern = None
		return segment

	def get_segment(self, key):
		return self._segments[key]

	def compile(self):
		"""Compiles the pattern, but also prepares the string formats."""
		if self.is_compiled:
			return self._pattern
		num_formats = 1
		segments = [None] * len(self._segments)
		for k, v in self._segments.items():
			i, fmt, pat, seg = v
			segments[i] = (k, fmt, pat, seg)
			# If any string formats are a list or a tuple then prepare the rename function
			# for multiple scenarios (e.g. '%02d' % 'Tip' fails, so then it would try the 2nd
			# scenario, which could be '%s' % 'Tip').
			if isinstance(fmt, list) or isinstance(fmt, tuple):
				if len(fmt) > num_formats:
					num_formats = len(fmt)
		self.pattern, self.formats = '', [''] * num_formats
		for k, fmt, pat, seg in segments:
			for i, sf in enumerate(self.formats):
				f = (isinstance(fmt, list) or isinstance(fmt, tuple)) and fmt[i] or fmt
				self.formats[i] += '%%(%s)%s%s' % (k, f, seg)
			self.pattern += '(?P<%s>%s)%s' % (k, pat, seg)
		self._pattern = re.compile(self.pattern)
		self.is_compiled = True
		return self._pattern

	def match(self, dag_node):
		"""
		Returns a match object that adheres to the supplied naming convention.
		"""
		self.compile()
		name = dag_node.nodeName()
		m = self._pattern.match(name)
		assert m, "Name '%s' does not match the required naming convention: %s." % \
			(name, self.pattern)
		return m

	def rename(self, dag_node, **kwargs):
		"""
		Returns a nicely-formatted name that adheres to the specified
		naming convention.
		"""
		self.compile()

		# Attempt each format until we find one that works.
		rename_to = None
		for f in self.formats:
			try:
				rename_to = f % kwargs
				break
			except TypeError:
				continue
		if not rename_to:
			raise TypeError('String format mismatch.')

		return rename(dag_node, rename_to)

#=================================================
# functions
#=================================================

def error(msg, showLineNumber=False):
	#return mel.error(msg, showLineNumber)
	return MGlobal.displayError(msg)

def warning(msg, showLineNumber=False):
	return mel.warning(msg, showLineNumber)

def info(msg):
	return MGlobal.displayInfo(msg)

def result(msg):
	return info('Result: ' + msg)

def flatten(nested):
	"""
	Recursive generator that flattens an arbitrary depth of a set of lists.
	"""
	try:
		# Don't iterate over string-like objects:
		try: nested + ''
		except TypeError: pass
		else: raise TypeError
		for sublist in nested:
			for element in flatten(sublist):
				yield element
	except TypeError:
		yield nested

def snap(*args, **kwargs):
	"""
	Snaps one or more source transforms to a target node's world
	position, rotation and (optionally) the scale as well.
	"""

	if not args: args = selected()
	target, sources = args[0], args[1:]

	kwargs['space'] = kwargs.pop('space', 'world')
	translate = kwargs.pop('t', kwargs.pop('translate', True))
	rotate = kwargs.pop('r', kwargs.pop('rotate', True))
	scale = kwargs.pop('s', kwargs.pop('scale', False))
	if translate:
		t = target.getTranslation(**kwargs)
		[s.setTranslation(t, **kwargs) for s in sources]
	if rotate:
		r = target.getRotation(**kwargs)
		[s.setRotation(r, **kwargs) for s in sources]
	if scale:
		s = target.getScale(**kwargs)
		[s.setScale(s, **kwargs) for s in sources]

def uniquify(seq):
	"""
	Not order preserving.
	"""

	keys = {}
	for e in seq:
		keys[e] = None
	return keys.keys()

def function_on_hierarchy(dag_nodes, fn, *args, **kwargs):
	for dn in dag_nodes:
		fn(dn, *args, **kwargs)
		function_on_hierarchy(dn.getChildren(), fn, *args, **kwargs)


"""microdatasets package installation metadata."""
from importlib.machinery import SourceFileLoader
from pathlib import Path

from setuptools import find_packages, setup

version = SourceFileLoader(
    "microdatasets.version",
    str(Path(__file__).parent / "src" / "microdatasets" / "version.py"),
).load_module()

with open(Path(__file__).with_name("README.md"), encoding="utf-8") as f:
    long_description = f.read()

setup(
    name="microdatasets",
    description="Simple datasets for evaluating DL algorithms.",
    long_description=long_description,
    long_description_content_type="text/markdown",
    packages=find_packages("src"),
    package_dir={"": "src"},
    version=version.__version__,
    license="MIT",
    author="Guillem Duran Ballester",
    author_email="guillem@fragile.tech",
    url="https://github.com/FragileTech/microdatasets",
    keywords=["Machine learning", "artificial intelligence"],
    test_suite="tests",
    tests_require=["pytest>=5.3.5", "hypothesis>=5.6.0"],
    extras_require={},
    install_requires=["matplotlib", "numpy", "scipy"],
    package_data={
        "": ["README.md"],
        "microdatasets": ["assets/**/*", "assets/**/.*", "tests/**/*", "tests/**/.*"],
    },
    include_package_data=True,
    classifiers=[
        "Development Status :: 3 - Alpha",
        "Intended Audience :: Developers",
        "License :: OSI Approved :: MIT License",
        "Programming Language :: Python :: 3.7",
        "Programming Language :: Python :: 3.8",
        "Programming Language :: Python :: 3.9",
        "Programming Language :: Python :: 3.10",
        "Topic :: Software Development :: Libraries",
    ],
)


import pandas as pd
from datetime import datetime

shape = 1000000
time_lst = []

col_1_and_2 = [i for i in range(shape)]
col_3 = ["a" for i in range(shape)]
col_4 = ["b" for i in range(shape)]
col_5 = ["c" for i in range(shape)]

df_dict = {
    "col_1": col_1_and_2,
    "col_2": col_1_and_2,
    "col_3": col_3,
    "col_4": col_4,
    "col_5": col_5
}

df = pd.DataFrame(df_dict)

## WRITE TO FILES
# HDF
start_time = datetime.now()
df.to_hdf("homework/week_5/data.h5", key="df")
end_time = datetime.now()
time_lst.append(end_time - start_time)

# FEATHER
start_time = datetime.now()
df.to_feather("homework/week_5/data.feather")
end_time = datetime.now()
time_lst.append(end_time - start_time)

# PARQUET
start_time = datetime.now()
df.to_parquet("homework/week_5/data.gzip")
end_time = datetime.now()
time_lst.append(end_time - start_time)

## WRITE TIME TO TXT
with open("homework/week_5/time_used.txt", "w") as f:
    f.write("HDF: " + str(time_lst[0]) + "\n")
    f.write("Feather: " + str(time_lst[1]) + "\n")
    f.write("Parquet: " + str(time_lst[2]) + "\n")


from main import app, db
from views import Form, feature_flag
from models.ticket import TicketType
from models.cfp import (
    TalkProposal, WorkshopProposal,
    InstallationProposal, ProposalDiversity,
)

from flask import (
    render_template, redirect, request,
    url_for, abort,
)
from flask.ext.login import current_user

from wtforms.validators import Required, Email
from wtforms import (
    BooleanField, TextField,
    FormField, TextAreaField, SelectField,
)

class DiversityForm(Form):
    age = TextField('Age')
    gender = TextField('Gender')
    ethnicity = TextField('Ethnicity')

class ProposalForm(Form):
    name = TextField("Name", [Required()])
    email = TextField("Email", [Email(), Required()])
    title = TextField("Title", [Required()])
    description = TextAreaField("Description", [Required()])
    # days = SelectMultipleField("I can only attend on some days",
    #                            choices=[('fri', 'Friday'),
    #                                     ('sat', 'Saturday'),
    #                                     ('sun', 'Sunday'),
    #                                     ])
    need_finance = BooleanField("I can't afford to buy a ticket without financial support")

    diversity = FormField(DiversityForm)

class TalkProposalForm(ProposalForm):
    type = 'talk'
    length = SelectField("Duration", default='30',
                         choices=[('< 10 mins', "Shorter than 10 minutes"),
                                  ('10 mins', "10 minutes"),
                                  ('30 mins', "30 minutes"),
                                  ('45 mins', "45 minutes"),
                                  ('> 45 mins', "Longer than 45 minutes"),
                                  ])
    experience = SelectField("Have you given a talk before?",
                             choices=[('none', "It's my first time"),
                                      ('some', "I've talked before"),
                                      ('repeat', "I've given this talk before"),
                                      ])
    one_day = BooleanField("I can only attend for the day I give my talk")

class WorkshopProposalForm(ProposalForm):
    type = 'workshop'
    length = TextField("Duration", [Required()])
    attendees = TextField("Attendees", [Required()])
    one_day = BooleanField("I can only attend for the day I give my workshop")

class InstallationProposalForm(ProposalForm):
    type = 'installation'
    size = TextField("Physical size", [Required()])

@app.route('/cfp')
@app.route('/cfp/<string:cfp_type>', methods=['GET', 'POST'])
@feature_flag('CFP')
def cfp(cfp_type='talk'):
    if cfp_type not in ['talk', 'workshop', 'installation']:
        abort(404)

    forms = [TalkProposalForm(), WorkshopProposalForm(), InstallationProposalForm()]
    (form,) = [f for f in forms if f.type == cfp_type]

    if request.method == 'POST':
        app.logger.info('Checking %s proposal for %s (%s)', cfp_type, form.name.data, form.email.data)

    if form.validate_on_submit():
        if cfp_type == 'talk':
            cfp = TalkProposal()
            cfp.length = form.length.data
            cfp.experience = form.experience.data
            cfp.one_day = form.one_day.data
        elif cfp_type == 'workshop':
            cfp = WorkshopProposal()
            cfp.length = form.length.data
            cfp.attendees = form.attendees.data
            cfp.one_day = form.one_day.data
        elif cfp_type == 'installation':
            cfp = InstallationProposal()
            cfp.size = form.size.data

        cfp.name = form.name.data
        cfp.email = form.email.data
        cfp.title = form.title.data
        cfp.description = form.description.data
        cfp.need_finance = form.need_finance.data

        cfp.diversity = ProposalDiversity()
        cfp.diversity.age = form.diversity.age.data
        cfp.diversity.gender = form.diversity.gender.data
        cfp.diversity.ethnicity = form.diversity.ethnicity.data

        db.session.add(cfp)
        db.session.commit()

        return redirect(url_for('cfp_complete'))

    if current_user.is_authenticated():
        form.name.data = current_user.name
        form.email.data = current_user.email

    full_price = TicketType.query.get('full').get_price('GBP')

    return render_template('cfp.html', full_price=full_price,
        forms=forms, active_cfp_type=cfp_type, has_errors=bool(form.errors))

@app.route('/cfp/complete')
@feature_flag('CFP')
def cfp_complete():
    return render_template('cfp_complete.html')


# -*- coding = utf-8 -*-

"""
    author:fengxi
    time:2023/2/17

    project name:pnlp2023
    file name:main.py
    function:
        chapter_1主方法：
            数据加载、预处理
            文本（短评）数据可视化
            数据分析与可视化
"""

import jieba
import pandas as pd
from preprocessing import load_stopwords
from visualization_comments import generate_wordcloud, get_cooccurrence_graph, generate_celebrity_graph
from visualization_data import analysis_location

# 加载自定义词典
jieba.load_userdict("./dict/custom_dictionary.txt")

def load_pre_data(filename):
    """
        加载全部数据
        对评论进行预处理
    :param filename:
    :return:
    """
    # === 加载全部数据
    df = pd.read_csv(filename, encoding="utf-8")

    # 提取每一列数据
    user_name = df["用户名"].values.tolist()
    video_status = df["电视剧观看状态"].values.tolist()
    comment_score = df["评论分数"].values.tolist()
    comment_level = df["评论等级"].values.tolist()
    comment_time = df["评论时间"].values.tolist()
    comment_location = df["评论位置"].values.tolist()
    comment_vote_count = df["评论点赞数"].values.tolist()
    comment_content = df["评论"].values.tolist()

    # === 对评论进行预处理
    # 加载停用词词典
    stopwords = load_stopwords()

    comment_seg_list = []  # 分词后的评论
    for comment in comment_content:
        # 简单预处理
        comment = comment.replace("\r", "")
        # 分词
        comment_seg = []
        for words in jieba.lcut(comment):
            # 去停用词（包括中文标点符号）
            if words in stopwords:
                continue
            # 过滤单个字符的词
            if len(words) < 2:
                continue
            # 过滤数字
            if words.isdigit():
                continue
            comment_seg.append(words)
        comment_seg_list.append(comment_seg)

    return user_name, video_status, comment_score, comment_level, comment_time, \
        comment_location, comment_vote_count, comment_seg_list

def main():
    """
        数据分析主流程：
            1、短评分析与可视化：词云图、词共现关系图、人物关系图谱
            2、数据分析与可视化
    :return:
    """
    # 加载和预处理数据
    user_name, video_status, comment_score, comment_level, comment_time, comment_location, \
    comment_vote_count, comment_content = load_pre_data("./data/狂飙.csv")

    # 生成词云图
    generate_wordcloud(comment_content)

    # 获得词共现关系图
    get_cooccurrence_graph(comment_content)

    # 构建人物关系图谱
    generate_celebrity_graph()

    # 统计分析评论位置分布情况
    analysis_location(comment_location)

if __name__ == '__main__':
    # 主方法
    main()
    print("数据分析完成...")

    pass


# -*- coding: utf-8 -*-
import os
import sys
import random
import numpy as np
import json
import time
import copy
import torch
import torch.nn as nn
import torch.optim as optim
import torch.nn.functional as F
import torchvision.transforms as T
import argparse
from torch_geometric.data import DataLoader
from tensorboardX import SummaryWriter
from tqdm import tqdm
from utils import Logger, set_random_seed, worker_init_fn, compute_grad_norm, GradualWarmupScheduler, wrap_dataloader, \
    load_model, load_model_info
from dataset import GamePatch

# if gpu is to be used
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

parser = argparse.ArgumentParser()
parser.add_argument('--data_path', type=str, required=True,
                    help='input the dataset name')
parser.add_argument('--model_path', type=str)
parser.add_argument('--model', type=str, default="pointconv",
                    help='model name')
parser.add_argument('--aggr', type=str, default='max',
                    help='aggregation function')
args = parser.parse_args()
BATCH_SIZE = 128
CROSS_ENTROPY = True
LOSS_BALANCE = True

data_path = args.data_path
with open(data_path, 'r') as f:
    data = json.load(f)

dataset = GamePatch(data["data"])
model_info = load_model_info(dataset, aggr=args.aggr)
policy_net = load_model(model=args.model, info=model_info)
policy_net.load_state_dict(torch.load(args.model_path)['policy_net'])
policy_net = policy_net.to(device)

train_dataset=dataset
train_dataloader = DataLoader(train_dataset,
                              batch_size=BATCH_SIZE,
                              shuffle=False,
                              drop_last=False,
                              num_workers=8,
                              worker_init_fn=worker_init_fn,
                              )

print("==> data size: {}\n".format(len(dataset)))

policy_net.eval()
true_sample, all_sample = 0, 0
eval_result = []
for i, data_batch in tqdm(enumerate(train_dataloader)):
    # copy data from cpu to gpu
    data_batch = data_batch.to('cuda')

    # forward
    with torch.no_grad():
        outputs = policy_net(data_batch)
        loss = 0
        q = outputs['q']

        actions = q.cpu().argmax(-1).numpy()
        gt_actions = data_batch.y.cpu().argmax(-1).numpy()
        true_flag = actions == gt_actions
        true_sample+=np.sum(true_flag)
        all_sample += actions.shape[0]
        #print(true_flag)
        eval_result.append(true_flag)
print('acc: ',true_sample/all_sample)
eval_result = np.concatenate(eval_result)
np.save(os.path.join(os.path.dirname(args.model_path), "eval_result.npy"), eval_result)


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Apr 12 10:52:20 2020

@author: aadityabhatia

Info: This module builds a hierarchical structure for brands and products where
Brands/ foler has multiple brands and
each brand has multiple product folders.
Brand folders have brand csvs and product folders have product csvs.
"""

import pandas as pd
import os
import spacy
import unidecode
from nltk.corpus import stopwords
import string
import argparse

nlp = spacy.load("en_core_web_sm", disable=['parser', 'tagger', 'ner'])
stops = stopwords.words("english")
punctuations = string.punctuation
for p in punctuations:
    stops.append(p)
stops.append('')

# arg parser
parser = argparse.ArgumentParser(description='Builds the models to test accuracy')
parser.add_argument('-d', '--dirName', default=None)
parser.add_argument('-l', '--lab_d', default='reference_data.csv')

args = parser.parse_args()
dirName = args.dirName
if not dirName:
    current_directory = os.getcwd()
    current_directory = current_directory.replace('\\', '/')
    dirName = f'{current_directory}/Data/'

lab_d = args.lab_d

def getLemma(comment):
    lemmatized = []
    tokens = nlp(unidecode.unidecode(str(comment).lower()))
    for token in tokens:
        token = token.lemma_
        if token not in stops:
            lemmatized.append(token)
    return ' '.join(lemmatized)

if __name__ == "__main__":
    lab_df = pd.read_csv(lab_d)

    # Preprocessing the labels file for future use:
    lab_df['query_lemma'] = lab_df['query'].apply(getLemma)

    if not os.path.exists(dirName):
        os.mkdir(dirName)

    # making brand path
    if not os.path.exists(dirName + 'Brands'):
        os.mkdir(dirName + 'Brands')
    if not os.path.exists(dirName + 'SavedModels'):
        os.mkdir(dirName + 'SavedModels')

    # lists for unprocessed brands (can save for if needed)
    unprocessed_brands = []
    unprocessed_products = []

    # making file structures in local dir
    for brand, brand_grouper in lab_df.groupby('brand'):
        brand = brand.replace('/', '-')
        brand_path = dirName + 'Brands/' + brand + '/'

        # making brand files
        if len(brand_grouper) > 5:
            if not os.path.exists(brand_path):
                os.mkdir(brand_path)
            brand_grouper.to_csv(brand_path + brand + '.csv')

            # making product files
            for product, product_grouper in brand_grouper.groupby('product_id'):
                product_path = brand_path + product
                if len(product_grouper) > 5:
                    if not os.path.exists(product_path):
                        os.mkdir(product_path)
                    product_grouper.to_csv(product_path + '/' + product + '.csv')
                else:
                    unprocessed_products.append(product)
        else:
            unprocessed_brands.append(brand)

    print('\n\n\n...Estimations')
    print(len(unprocessed_brands) / lab_df.brand_id.nunique() * 100, '% brands not processed')
    print(len(unprocessed_products) / lab_df.product_id.nunique() * 100, '% products not processed')


import matplotlib.pyplot as plt
import imageio
import numpy as np
import regex
from os import path
from .crop_images import crop

description = "Computes the Stokes images. It is computed from 6 images like " \
              "PREFIX_aXX_SUFFIX.EXT where XX is 0, 45, 90, 135, Dex are Lev. " \
              "FILENAME must match this structure."

def usage(msg=''):
    print("\n > " + (msg or description) + "\n")
    print(f"Usage: pyholo utils --script get_stokes --filename <myBeam_pXX_aXX_suffix.ext> "
          f"[--save <save_fn>] [--crop <Cx Cy Sx [Sy=Sx]>] [-v]\n\n"
          f"\t--filename: The template filename to find the six polarimetric images.\n"
          f"\t--save: The filename of the figure to be saved. If it starts or "
          f"ends with an underscore, it is understood as a suffix.\n"
          f"\t--crop: The cropping parameters.\n"
          f"\t-v: Verbose mode.\n")
    exit(int(bool(msg)))

def main(**kwargs):
    if kwargs.get('man'):
        usage()

    analizers = ('90', '0', '135', '45', 'Dex', 'Lev')  # The order is important
    cropping = kwargs.get('crop', None)
    filename = kwargs.get('filename', '')
    # print(prefix_fn)
    reg_exp_patt = (f"^(?P<prefix>[a-zA-Z0-9_\\\/.]+)"
                    f"_p(?P<polarizer>[a-zA-Z0-9]+)"  # put this in a separated regex
                    f"_a(?P<analizer>0|90|45|135|Dex|Lev)"
                    f"(?P<suffix>[a-zA-Z0-9_]*)"
                    f"(?P<extension>.[a-zA-Z]+)$")

    groups = [m for m in regex.finditer(reg_exp_patt, filename)]

    save_fn = kwargs.get('save', '')

    if not groups:
        usage("The filename does not match the pattern: ")
    else:
        gr_dict = groups[0].groupdict()
        # print(gr_dict)
        prefix_fn = gr_dict['prefix']  # + '_p' + polarizer
        parent_fn = path.dirname(prefix_fn)
        beam_name = path.basename(prefix_fn)
        polarizer = '_p' + gr_dict['polarizer']
        suffix_fn = gr_dict['suffix']
        ext_fn = gr_dict['extension']
        final = suffix_fn + ext_fn
        if save_fn.startswith('_') or save_fn.endswith('_'):
            fig_fn = prefix_fn + polarizer + '_' + save_fn.strip('_') + gr_dict['suffix'] + '.png'
        else:
            fig_fn = save_fn

    for curr_analizer in analizers:
        filename = prefix_fn + polarizer + '_a' + curr_analizer + final
        # print(filename)
        im = imageio.imread(filename).astype('float64')/4
        # print(im.max(), im.min())

        if True:
            ref_fn = path.basename(filename).strip('cropped_')
            print(ref_fn)
            ref = crop(path.join(path.dirname(filename), ref_fn),
                       926, 685, 200).astype('float64') / 4
            ref = np.average(ref)
            im /= ref

        # print(im)
        if cropping:
            im = crop(im, *cropping)

        if curr_analizer == '90':
            buff = im
        elif curr_analizer == '0':
            stokes0 = buff + im
            stokes1 = buff - im
        elif curr_analizer == '135':
            buff = im
        elif curr_analizer == '45':
            stokes2 = buff - im
        elif curr_analizer == 'Dex':
            buff = im
        elif curr_analizer == 'Lev':
            stokes3 = buff - im

    if kwargs.get('verbose', False) or fig_fn:
        fig, axes = plt.subplots(2, 2, figsize=(10, 10))
        stokes_list = np.array([stokes0/stokes0.max(), stokes1/stokes0.max(),
                                stokes2/stokes0.max(), stokes3/stokes0.max()])
        # beam_name = prefix_fn.split('\\')[-1]
        # beam_name = '_'.join(beam_name.split('_')[-2:])
        fig.suptitle(fr"Stokes images for '{beam_name+polarizer+suffix_fn}' beam")
        for idx, ax in enumerate(axes.flatten()):
            stoke = stokes_list[idx]
            im = ax.imshow(stoke, vmin=-1, vmax=1, cmap='seismic')
            ax.set(title=rf"$S_{idx} (min:{stoke.min():.2f} ; max:{stoke.max():.2f}$")

        fig.subplots_adjust(right=0.8)
        cbar_ax = fig.add_axes([0.85, 0.15, 0.05, 0.7])
        fig.colorbar(im, cax=cbar_ax)
        if fig_fn:
            plt.savefig(fig_fn)
            np.save(path.splitext(fig_fn)[0]+'.npy', stokes_list)
        plt.show()

        return im


#new - whole script added by instructor
from rest_framework import serializers
from rest_framework.validators import UniqueForYearValidator
from .models import Customer

class CustomerSerializer(serializers.ModelSerializer):

    class Meta:
        model = Customer
        fields = ['pk', 'name', 'email', 'created']

        validators = [
            UniqueForYearValidator(
                queryset=Customer.objects.all(),
                field='name',
                date_field='created'
            )
        ]


from __future__ import absolute_import, division, print_function
import pathlib
import random
import tensorflow as tf
from tensorflow import keras
from sklearn.preprocessing import LabelBinarizer
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report

import cv2

import numpy as np
#import matplotlib.pyplot as plt

tf.enable_eager_execution()

import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'

AUTOTUNE = tf.data.experimental.AUTOTUNE

#image and data pre-processing

#data directory for images
data_dir = "./ISIC_images"
#root paths for data
data_root = pathlib.Path(data_dir)

#pathways to images
for item in data_root.iterdir():
  print(item)

#pathway to each individual image
all_image_paths = list(data_root.glob('*/*'))
all_image_paths = [str(path) for path in all_image_paths]
random.shuffle(all_image_paths)

#verifier that pathways are correct
for image in all_image_paths:
  print(image)

#label_names created/generated from filenames
label_names = sorted(item.name for item in data_root.glob('*/') if item.is_dir())
print(label_names)

#unnecessary - corresponds each label to a give index
label_to_index = dict((name, index) for index, name in enumerate(label_names))
print(label_to_index)

#assigns given medical condition to each image
all_image_labels = list(pathlib.Path(path).parent.name for path in all_image_paths)
print("First 10 labels indices: ", all_image_labels[:10])

labels = []

#assigns an array of the corresponding labels
for label in all_image_labels:
  labels.append(label)

img_data = []
print(len(all_image_paths))
for image_path in all_image_paths:
  image = cv2.imread(image_path)
  #print(image)
  #image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
  image = cv2.resize(image, (32, 32)).flatten()
  print(image.shape)
  img_data.append(image)

img_data = np.array(img_data, dtype="float")/255.0
labels = np.array(labels)

(trainX, testX, trainY, testY) = train_test_split(img_data, labels, test_size=0.25, random_state=42)
lb = LabelBinarizer()
trainY = lb.fit_transform(trainY)
testY=lb.transform(testY)

train = []
for p in trainY:
  train.append(np.argmax(p))
print(train)

train = np.array(train)

model = keras.Sequential([
  keras.layers.Dense(1024, input_shape=(3072,), activation="relu"),
  keras.layers.Dense(4, activation="softmax" ),

])

sgd = keras.optimizers.SGD(lr=0.1, decay=1e-6, momentum=0.9, nesterov=True)

model.compile(optimizer='sgd', loss = 'sparse_categorical_crossentropy', metrics=['accuracy'])

model.fit(trainX, train, epochs=5, batch_size=32)

predictions = model.predict(testX, batch_size=32)

train = []
for p in testY:
  train.append(np.argmax(p))

train = np.array(train)

#print(classification_report(train.argmax(axis=1), predictions.argmax(axis=1), target_names=lb.classes_))

test_loss, test_acc = model.evaluate(testX, train)
print(test_acc)

for p in predictions:
  print(np.argmax(p))
print(train)


from actions.utils import create_action
from bookmarks.common.decorators import ajax_required
from bookmarks.common.utils import is_ajax
from django.contrib import messages
from django.contrib.auth.decorators import login_required
from django.core.exceptions import ObjectDoesNotExist
from django.core.paginator import (
    Paginator,
    EmptyPage,
    PageNotAnInteger,
)
from django.http import JsonResponse
from django.shortcuts import (
    render,
    redirect,
    get_object_or_404,
    HttpResponseRedirect,
    HttpResponse,
)
from django.views.decorators.http import require_POST

from .forms import ImageCreateForm
from .models import Image

@login_required
def image_list(request):
    images = Image.objects.all()
    paginator = Paginator(images, per_page=8)
    page = request.GET.get('page')
    ajax = is_ajax(request)
    try:
        images = paginator.page(page)
    except PageNotAnInteger:
        images = paginator.page(number=1)
    except EmptyPage:
        if ajax:
            return HttpResponse('')
        images = paginator.page(number=paginator.num_pages)
    if ajax:
        return render(request, 'images/image/list_ajax.html',
                      {'section': 'images', 'images': images})
    return render(request, 'images/image/list.html',
                  {'section': 'images', 'images': images})

@login_required
@require_POST
@ajax_required
def image_like(request):
    image_id = request.POST.get('id')
    action = request.POST.get('action')
    if image_id and action:
        try:
            image = Image.objects.get(id=image_id)
            if action == 'like':
                image.users_like.add(request.user)
                create_action(request.user, 'liked', image)
            else:
                image.users_like.remove(request.user)
            return JsonResponse({'status': 'ok'})
        except ObjectDoesNotExist:
            messages.error(request, 'An error occurred in adding like process')
            return HttpResponseRedirect('/account/dashboard/')

    return JsonResponse({'status': 'ok'})

def image_detail(request, id, slug):
    image = get_object_or_404(Image, id=id, slug=slug)
    return render(request, 'images/image/detail.html',
                  {'section': 'images', 'image': image})

@login_required
def image_create(request):
    if request.method == 'POST':
        image_form = ImageCreateForm(data=request.POST)
        if image_form.is_valid():
            new_image = image_form.save(commit=False)
            new_image.user = request.user
            new_image.save()
            create_action(request.user, 'added ', new_image)
            messages.success(request, 'Image has been added.')
            return redirect(new_image.get_absolute_url())
        else:
            messages.error(request, 'An error occurred while image was adding')
    else:
        image_form = ImageCreateForm(data=request.GET)
    return render(request, 'images/image/create.html',
                  {'section': 'gallery', 'image_form': image_form})


from ortools.linear_solver import pywraplp
from Components.Food import FoodItem, ListFoodItem, ConstraintList, Constraint
from Components.utils import update_json
import pandas as pd

import streamlit as st
import json

#Global variable
food_list = ListFoodItem([])
food_list.initialize_me()

constraint_list = ConstraintList([])
constraint_list.initialize_me()

def MinimizeFunction(dataframe, constraints):
    solver = pywraplp.Solver.CreateSolver('SCIP')

    foods = [solver.NumVar(0.0, solver.infinity(), name=name) for name in dataframe['name']]

    nvariables = solver.NumVariables()

    objective = solver.Objective()

    for constraint in constraints.constraintslist:
        solver.Add(solver.Sum([foods[i]*dataframe[constraint.name][i] for i in range(len(foods))]) >= constraint.value)

    for i ,food in enumerate(foods):
        objective.SetCoefficient(food, dataframe['cost'][i])

    solver.Add(solver.Sum([food for food in foods]) == 1)
    objective.SetMinimization()
    status = solver.Solve()

    if status == pywraplp.Solver.OPTIMAL:
        st.markdown('---------------------------------------')
        st.write('Solution:')
        st.write('Objective value =', solver.Objective().Value())
        for food in foods:
            if food.solution_value() > 0.01:
                st.write(food.name() , food.solution_value(),"kg")
    else:
        st.write('The problem does not have an optimal solution.')

    return {
        'nvariables':nvariables,
        'status':status
    }

# Instantiate a Glop solver and naming it.
def AddFoodForms(food_list):
    with st.expander("Add Food Forms 🥐"):
        #Initizalizing Forms
        form = st.form(key="add_form")

        #Food Inputs
        name = form.text_input("Name of the food")
        cost = form.number_input("Cost ($/kg)")
        protein = form.number_input("Protein")
        calcium = form.number_input("Calcium")

        #Submit button
        submit = form.form_submit_button('Submit')

        #onSubmit
        if submit:
            #Create and append the food to the foodlist
            newfood = FoodItem(name=name, cost=cost, protein= protein, calcium = calcium)
            food_list.foodlist.append(newfood)

            #Write item into the json file
            update_json(item = food_list,filename ='foods.json')

            return newfood
        else:
            return

def FoodFormsConstraints(constraint_list):
    with st.expander("Add the constraints ⛓"):
        form_2 = st.form(key="constraint_forms")

        calcium_constraint =  form_2.number_input("Maximum Calcium", key="calcium_constraint")
        protein_constraint = form_2.number_input("Maximum Protein", key="protein_constraint")

        submit_button = form_2.form_submit_button('Submit')

        if submit_button:
            constraint01 = Constraint(name="calcium", value=calcium_constraint)
            constraint02 = Constraint(name="protein", value=protein_constraint)

            constraint_list.update_constraint_list(constraint01)
            constraint_list.update_constraint_list(constraint02)

            update_json(item = constraint_list, filename ='constraints.json')
    return

def FoodMixesPages():

    FoodFormsConstraints(constraint_list)
    AddFoodForms(food_list)

    df = food_list.food_list_to_pandas()
    constraint_df = constraint_list.data_to_pandas()

    if not df.empty:
        st.markdown("## Dataframe with the food")
        st.dataframe(df)
        cleardataframe = st.button(" 🗑 Delete Dataframe")
        if cleardataframe:
            food_list.foodlist.clear()
            update_json(item=food_list, filename='foods.json')

        res = MinimizeFunction(dataframe=df, constraints=constraint_list)
        status = res['status']
        st.write(res["nvariables"])
    else:
        st.markdown("No item added to the dataframe 😓")

    if not constraint_df.empty:
        st.dataframe(constraint_df)
        clearconstraints = st.button(" 🗑 Delete Constraint")
        if clearconstraints:
            constraint_list.constraintslist.clear()
            update_json(item=constraint_list, filename='constraints.json')
    else:
        st.markdown("No constraint added")
    return


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# project = https://github.com/lowliness9/pytools.git
# author = lowliness9
# date: 2021-04-23
from concurrent.futures import ThreadPoolExecutor
import threading
import requests
import sys
import time
import os

requests.packages.urllib3.disable_warnings()

headers = {
    'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.79 Safari/537.36',
    'Connection':'close'
}

workDIR = sys.argv[1]

lock = threading.Lock()

todayFlag = time.strftime("%Y-%m-%d-", time.localtime())

aliveProxyList = []

def outputAliveProxy(proxy):
    lock.acquire()
    print(proxy)
    with open(os.path.join(workDIR,todayFlag + 'proxy.txt'),'a+') as f:
        if proxy not in aliveProxyList:
            f.write(proxy.strip()+'\n')
    lock.release()

def ProxyTest(proxyList):
    procotol = proxyList[0]
    IP = proxyList[1]
    PORT = proxyList[2]

    proxies = {"http": "{}://{}:{}".format(procotol,IP,PORT), "https": "{}://{}:{}".format(procotol,IP,PORT)}
    try:
        request = requests.get(url = 'http://icanhazip.com/', proxies = proxies,timeout = (5,3) , verify = False,headers = headers)
        if request.text.strip() == IP:
            lock.acquire()
            print(' '.join(proxyList))
            lock.release()
            aliveProxyList.append(' '.join(proxyList))
    except:
        pass

def todayProxyTest(proxyList):
    procotol = proxyList[0]
    IP = proxyList[1]
    PORT = proxyList[2]

    proxies = {"http": "{}://{}:{}".format(procotol,IP,PORT), "https": "{}://{}:{}".format(procotol,IP,PORT)}
    try:
        request = requests.get(url = 'http://icanhazip.com/', proxies = proxies,timeout = (5,3) , verify = False,headers = headers)
        if request.text.strip() == IP:
            outputAliveProxy(' '.join(proxyList))
    except:
        pass

def Builder():
    proxyList = []
    proxyfileList = open(os.path.join(workDIR,sys.argv[2]),'r+').readlines()
    for proxy in proxyfileList:
        sinProxy = proxy.split()
        if sinProxy:proxyList.append(sinProxy)
    return proxyList

def checkHistory():
    if not os.path.exists(os.path.join(workDIR,todayFlag + 'proxy.txt')):
        with open(os.path.join(workDIR,todayFlag + 'proxy.txt'),'a+') as f:
            for proxy in aliveProxyList:
                if proxy:
                    f.write(proxy+'\n')
        os.remove(os.path.join(workDIR, sys.argv[2]))
    else:
        todayProxyList = []
        for proxy in open(os.path.join(workDIR,todayFlag + 'proxy.txt'),'r+').readlines():
            proxyT = proxy.strip().split()
            if proxyT:todayProxyList.append(proxyT)
        os.remove(os.path.join(workDIR,todayFlag + 'proxy.txt'))
        with ThreadPoolExecutor(20) as executor:
            executor.map(todayProxyTest,todayProxyList)
        for proxy in aliveProxyList:
            with open(os.path.join(workDIR, todayFlag + 'proxy.txt'), 'a+') as f:
                f.write(proxy.strip() + '\n')
        os.remove(os.path.join(workDIR,sys.argv[2]))

if __name__ == '__main__':

    proxyList = Builder()
    with ThreadPoolExecutor(20) as executor:
        executor.map(ProxyTest,proxyList)
    checkHistory()


import os

from cffi import FFI

from quanser.communications import PollFlag, BooleanProperty, Timeout, StreamError

# region Setup

ffi = FFI()
ffi.cdef("""
    /* Type Definitions */

    typedef char                t_boolean;
    typedef unsigned int        t_uint;
    typedef unsigned short      t_ushort;
    typedef t_ushort            t_uint16;
    typedef t_uint              t_uint32;
    typedef signed signed int   t_int;
    typedef t_int               t_int32;
    typedef t_int               t_error;
    typedef double              t_double;
    typedef long long           t_long;

    typedef struct tag_stream * t_stream;

    typedef struct tag_timeout
    {
        t_long    seconds;
        t_int     nanoseconds;
        t_boolean is_absolute;
    } t_timeout;

    typedef enum tag_qcomm_boolean_property
    {
        QCOMM_PROPERTY_IS_READ_ONLY,
        QCOMM_PROPERTY_IS_WRITE_ONLY,
        QCOMM_PROPERTY_IS_EXCLUSIVE,
        QCOMM_PROPERTY_NO_READ_AHEAD,

        QCOMM_PROPERTY_SERIAL_DTR,
        QCOMM_PROPERTY_SERIAL_RTS,
        QCOMM_PROPERTY_SERIAL_DSR,
        QCOMM_PROPERTY_SERIAL_CTS
    } t_qcomm_boolean_property;

    typedef enum tag_stream_boolean_property
    {
        STREAM_PROPERTY_IS_READ_ONLY  = QCOMM_PROPERTY_IS_READ_ONLY,
        STREAM_PROPERTY_IS_WRITE_ONLY = QCOMM_PROPERTY_IS_WRITE_ONLY,
        STREAM_PROPERTY_IS_EXCLUSIVE  = QCOMM_PROPERTY_IS_EXCLUSIVE,
        STREAM_PROPERTY_NO_READ_AHEAD = QCOMM_PROPERTY_NO_READ_AHEAD,

        STREAM_PROPERTY_SERIAL_DTR    = QCOMM_PROPERTY_SERIAL_DTR,
        STREAM_PROPERTY_SERIAL_RTS    = QCOMM_PROPERTY_SERIAL_RTS,
        STREAM_PROPERTY_SERIAL_DSR    = QCOMM_PROPERTY_SERIAL_DSR,
        STREAM_PROPERTY_SERIAL_CTS    = QCOMM_PROPERTY_SERIAL_CTS
    } t_stream_boolean_property;

    /* Configuration Functions */

    t_error stream_listen(const char * uri, t_boolean non_blocking, t_stream * server_stream);

    t_error stream_connect(const char * uri, t_boolean non_blocking,
                           t_int send_buffer_size, t_int receive_buffer_size,
                           t_stream * client_stream);

    t_error stream_accept(t_stream server_stream,
                          t_int send_buffer_size, t_int receive_buffer_size,
                          t_stream * client_stream);

    t_int stream_poll(t_stream stream, const t_timeout * timeout, t_uint flags);

    t_error stream_shutdown(t_stream stream);

    t_error stream_close(t_stream stream);

    t_error stream_close_all(void);

    t_error stream_get_boolean_property(t_stream stream,
                                        const t_stream_boolean_property properties[], t_uint num_properties,
                                        t_boolean buffer[]);

    t_error stream_set_boolean_property(t_stream stream,
                                        const t_stream_boolean_property properties[], t_uint num_properties,
                                        const t_boolean buffer[]);

    t_int stream_send(t_stream stream, const void * buffer, t_int buffer_size);

    t_int stream_receive(t_stream stream, void * buffer, t_int buffer_size);

    t_error stream_flush(t_stream stream);
""")

communications_lib = ffi.dlopen("quanser_communications")

# endregion

# region Constants

_WOULD_BLOCK = 34

_BOOLEAN_ARRAY = "t_boolean[]"
_CHAR_ARRAY = "char[]"
_UINT_ARRAY = "t_uint[]"
_UINT32_ARRAY = "t_uint32[]"
_INT_ARRAY = "t_int[]"
_INT32_ARRAY = "t_int32[]"
_DOUBLE_ARRAY = "t_double[]"

_BOOLEAN_PROPERTY_ARRAY = "t_stream_boolean_property[]"

# endregion

# region Stream Classes

class Stream:
    """A Python wrapper for the Quanser Stream API.

    Example
    -------
    >>> from quanser.communications import Stream
    >>> stream = Stream()

    """

    # region Life Cycle

    def __init__(self):
        self._stream = None

    # endregion

    # region Implementation

    def connect(self, uri, non_blocking, send_buffer_size, receive_buffer_size):
        """Connects to a listening stream referenced by the given URI. The URI specifies the protocol, address, port,
        and options associated with the stream. The Stream API uses the protocol to load a protocol-specific driver.

        URI examples::

            tcpip://remotehost:17000                        - connect to remote host on port 17000 using TCP/IP
            shmem://mymemory:1?bufsize=8192                 - connect via an 8K shared memory buffer
            pipe:mypipe?bufsize=4096                        - connect via a 4K named pipe
            i2c://localhost:0?baud='100000';address='0x48'  - connect via I2C at a baud rate of 100000

        If the non_blocking flag is set to ``False``, then this function will block until the connection is made.
        If the non_blocking flag is set to ``True``, then this function will not block. If the connection cannot be
        completed immediately, then ``-QERR_WOULD_BLOCK`` is raised. In this case, the connection may be completed using
        `poll` with the ``PollFlag.CONNECT`` flag.

        Parameters
        ----------
        uri : string
            A URI indicating the listening stream to which to connect.

        non_blocking : boolean
            Set to True to make the client connection non-blocking.

        send_buffer_size : int
            The size of the buffer to use for sending data over the stream, in bytes

        receive_buffer_size : int
            The size of the buffer to use for receiving data over the stream, in bytes

        Raises
        ------
        StreamError
            On non-zero return code. A suitable error message may be retrieved using `get_error_message`.

        Returns
        -------
        bool
            ``True`` if connected; ``False`` if connection in progress.

        Example
        -------
        >>> from quanser.communications import Stream
        >>> stream = Stream()
        >>> is_connected = stream.connect("tcpip://localhost:5039", False, 64, 64)
        >>> # ...
        ...
        >>> stream.shutdown()
        >>> stream.close()

        """
        if self._stream is not None:
            self.close()

        client_stream = ffi.new("t_stream *")

        result = communications_lib.stream_connect(uri.encode('utf-8'),
                                                   b'\x01' if non_blocking else b'\x00',
                                                   send_buffer_size,
                                                   receive_buffer_size,
                                                   client_stream)

        if result < 0 and result != -_WOULD_BLOCK:
            raise StreamError(result)

        self._stream = client_stream[0]

        return result != -_WOULD_BLOCK

    def listen(self, uri, non_blocking):
        """Establishes a server stream which listens on the given URI. The URI specifies the protocol, address, port and
        options associated with the server stream. The Stream API uses the protocol to load a protocol-specific driver.
        For example:

        tcpip://localhost:17000             - listen on port 17000 using TCP/IP
        shmem://mymemory:1?bufsize=8192     - listen via shared memory buffer. Use 8K buffers by default.
        pipe:mypipe?bufsize=4096            - listen via a named pipe. Use 4K buffers for the pipe.

        Parameters
        ----------
        uri : string
            A URI indicating the stream on which to listen.

        non_blocking : bool
            Set to True to prevent stream_accept calls from blocking.

        Raises
        ------
        StreamError
            On non-zero return code. A suitable error message may be retrieved using `get_error_message`.

        Example
        -------
        >>> from quanser.communications import Stream
        >>> stream = Stream()
        >>> stream.listen("tcpip://localhost:5039", False)
        >>> # ...
        ...
        >>> stream.shutdown()
        >>> stream.close()

        """
        server_stream = ffi.new("t_stream *")

        result = communications_lib.stream_listen(uri.encode('utf-8'),
                                                  b'\x01' if non_blocking else b'\x00',
                                                  server_stream)

        if result < 0:
            raise StreamError(result)

        self._stream = server_stream[0]

    def accept(self, send_buffer_size, receive_buffer_size):
        """Accepts a connection to a listening communication stream by a client. The client connects using
        `connect`.

        If `listen` was called with `non_blocking` set to ``False``, then this call will block until a client connects.
        The client stream returned will also be a blocking stream.

        If `listen` was called with non_blocking set to ``True``, then this call will not block. If there is no pending
        client connection, then it will raise `-QERR_WOULD_BLOCK`. The `poll` function may be used with
        ``PollFlag.ACCEPT`` to determine when a client connection is pending. In this case, the client stream returned
        will also be a non-blocking stream.

        On POSIX systems this function should be interruptible by a signal so that arrival of a signal will cause a
        ``-QERR_INTERRUPTED`` error to be returned.

        Parameters
        ----------
        send_buffer_size : int
            The size of the buffer to use for sending data over the stream, in bytes

        receive_buffer_size : int
            The size of the buffer to use for receiving data over the stream, in bytes

        Raises
        ------
        StreamError
            On non-zero return code. A suitable error message may be retrieved using `get_error_message`.

        Returns
        -------
        Stream or None
            The client stream or ``None`` if there is no client stream.

        Example
        -------
        >>> from quanser.communications import Stream
        >>> uri = "tcpip://localhost:5000"
        >>> send_buffer_size = 64
        >>> receive_buffer_size = 64
        >>> server_stream = Stream()
        >>> client_stream = Stream()
        >>> server_stream.listen(uri, False)
        >>> client_stream.connect(uri, False, send_buffer_size, receive_buffer_size)
        >>> client_connection = server_stream.accept(send_buffer_size, receive_buffer_size)
        >>> # ...
        ...
        >>> client_connection.shutdown()
        >>> client_stream.shutdown()
        >>> client_connection.close()
        >>> client_stream.close()
        >>> server_stream.close()

        """
        client_stream = Stream()

        _client_stream = ffi.new("t_stream *")

        result = communications_lib.stream_accept(self._stream if self._stream is not None else ffi.NULL,
                                                  send_buffer_size,
                                                  receive_buffer_size,
                                                  _client_stream)

        if result == -_WOULD_BLOCK:
            return None

        if result < 0:
            raise StreamError(result)

        client_stream._stream = _client_stream[0]
        return client_stream

    def poll(self, timeout, flags):
        """Polls the stream to determine whether it is possible to send or receive or accept a connection without
        blocking. The flags argument determines the conditions for which to check. The return value indicates the
        conditions which occurred. This function returns after the given timeout with a value of 0 if none of the
        conditions occurs. If an error occurs, then it returns a negative error code. The function will return before
        the timeout if at least one of the conditions occurs prior to the timeout.

        Note that this function may return zero even if the timeout has not occurred, even when the timeout is infinite.
        This special case occurs when the `shutdown` method has been called on the stream. Once the stream is shut down,
        the timeout may be limited to the close timeout associated with the stream (for shmem, for instance). Hence,
        the stream may timeout in this  case and return 0 before the specified timeout, even if the timeout is infinite.

        Parameters
        ----------
        timeout : Timeout
            A timeout structure.

        flags : int
            A bit mask indicating the conditions for which to check. Valid flags are:
            ``PollFlag.RECEIVE`` = on a listening stream, check for connections pending from clients. On a client
            stream, check whether there is any data available to receive.
            ``PollFlag.SEND`` = not valid on a listening stream. On a client stream, check whether there is space in
            the stream buffer to store any data.
            ``PollFlag.FLUSH` = not valid on a listening stream. On a client stream, check whether it is possible to
            flush any more data without blocking.
            ``PollFlag.ACCEPT`` = not valid on a client stream. On a listening stream, check whether there is a pending
            client connection.
            ``PollFlag.CONNECT`` = not valid on a listening stream. On a client stream, check whether the connection
            has completed.

        Returns
        -------
        int
            A bit mask containing the conditions which were satisfied. It has the same definition as the flags argument.
            If none of the specified conditions occurs before the timeout, then 0 is returned.

        Raises
        ------
        StreamError
            On non-zero return code. A suitable error message may be retrieved using `get_error_message`.

        Example
        -------
        Poll a stream

        >>> from quanser.communications import Stream, PollFlag
        >>> timeout = Timeout(3)
        >>> stream = Stream()
        >>> stream.connect("tcpip://localhost:5000", False, 64, 64)
        >>> result = stream.poll(timeout, PollFlag.SEND | PollFlag.RECEIVE)
        >>> # ...
        ...
        >>> stream.shutdown()
        >>> stream.close()

        """
        _timeout = None
        if timeout is not None:
            _timeout = ffi.new("const t_timeout *")
            _timeout.seconds = timeout.seconds
            _timeout.nanoseconds = timeout.nanoseconds
            _timeout.is_absolute = b'\x01' if timeout.is_absolute else b'\x00'
        else:
            _timeout = ffi.NULL

        result = communications_lib.stream_poll(self._stream if self._stream is not None else ffi.NULL, _timeout, flags)

        if result < 0:
            raise StreamError(result)

        return result

    def shutdown(self):
        """Shuts down send and/or receives in preparation for closing the stream.

        Raises
        ------
        StreamError
            On non-zero return code. A suitable error message may be retrieved using `get_error_message`.

        Example
        -------
        >>> from quanser.communications import Stream
        >>> stream = Stream()
        >>> stream.connect("tcpip://localhost:5000", False, 64, 64)
        >>> # ...
        ...
        >>> stream.shutdown()
        >>> stream.close()

        """
        result = communications_lib.stream_shutdown(self._stream if self._stream is not None else ffi.NULL)

        if result < 0:
            raise StreamError(result)

    def close(self):
        """Closes the stream. All resources associated with the stream are freed.

        Raises
        ------
        StreamError
            On non-zero return code. A suitable error message may be retrieved using `get_error_message`.

        Example
        -------
        >>> from quanser.communications import Stream
        >>> stream = Stream()
        >>> stream.connect("tcpip://localhost:5000", False, 64, 64)
        >>> # ...
        ...
        >>> stream.shutdown()
        >>> stream.close()

        """
        result = communications_lib.stream_close(self._stream if self._stream is not None else ffi.NULL)

        if result < 0:
            raise StreamError(result)

        self._stream = None

    @staticmethod
    def close_all():
        """Closes all streams established using stream_listen, stream_connect or stream_accept.

        Raises
        ------
        StreamError
            On non-zero return code. A suitable error message may be retrieved using `get_error_message`.

        Example
        -------
        >>> from quanser.communications import Stream
        >>> uri = "tcpip://localhost:5000"
        >>> server = Stream()
        >>> client = Stream()
        >>> server.listen(uri, False)
        >>> client.connect(uri, False, 64, 64)
        >>> # ...
        ...
        >>> Stream.close_all()

        """
        result = communications_lib.stream_close_all()

        if result < 0:
            raise StreamError(result)

    def get_boolean_property(self, properties, num_properties, buffer):
        """Returns the value of the specified boolean properties. This function is optional in a driver. If the driver
        does not provide it, then this function returns ``-QERR_NOT_SUPPORTED``.

        Parameters
        ----------
        properties : array_like
            An array containing the properties to query.
        num_properties : int
            The number of properties in the `properties` array.
        buffer : array_like
            An array into which the property values are stored. It must have the same number of elements as the
            properties array.

        Raises
        ------
        StreamError
            On non-zero return code. A suitable error message may be retrieved using `get_error_message`.

        Example
        -------
        Determine if the given stream is in exclusive write-only mode.

        >>> from array import array
        >>> from quanser.communications import Stream, BooleanProperty
        >>> stream = Stream()
        >>> stream.connect("tcpip://localhost:5000", False, 64, 64)
        >>> properties = array('i', [BooleanProperty.IS_EXCLUSIVE, BooleanProperty.IS_WRITE_ONLY])
        >>> num_properties = len(properties)
        >>> buffer = array('b', [0] * num_properties)
        >>> stream.get_boolean_property(properties, num_properties, buffer)
        >>> # ...
        ...
        >>> stream.shutdown()
        >>> stream.close()

        """
        result = communications_lib.stream_get_boolean_property(self._stream if self._stream is not None else ffi.NULL,
                                                                ffi.from_buffer(_BOOLEAN_PROPERTY_ARRAY, properties) if properties is not None else ffi.NULL,
                                                                num_properties,
                                                                ffi.from_buffer(_BOOLEAN_ARRAY, buffer) if buffer is not None else ffi.NULL)
        if result < 0:
            raise StreamError(result)

    def set_boolean_property(self, properties, num_properties, buffer):
        """Sets the value of the specified boolean properties. This function is optional in a driver. If the driver
        does not provide it, then this function returns ``-QERR_NOT_SUPPORTED``.

        Parameters
        ----------
        properties : array_like
            An array containing the properties to query.
        num_properties : int
            The number of properties in the `properties` array.
        buffer : array_like
            An array into which the property values are stored. It must have the same number of elements as the
            properties array.

        Raises
        ------
        StreamError
            On non-zero return code. A suitable error message may be retrieved using `get_error_message`.

        Example
        -------
        Set the stream to exclusive write-only mode.

        >>> from array import array
        >>> from quanser.communications import Stream, BooleanProperty
        >>> stream = Stream()
        >>> stream.connect("tcpip://localhost:5000", False, 64, 64)
        >>> properties = array('i', [BooleanProperty.IS_EXCLUSIVE, BooleanProperty.IS_WRITE_ONLY])
        >>> num_properties = len(properties)
        >>> buffer = array('b', [0] * num_properties)
        >>> stream.set_boolean_property(properties, num_properties, buffer)
        >>> # ...
        ...
        >>> stream.shutdown()
        >>> stream.close()

        """
        result = communications_lib.stream_set_boolean_property(self._stream if self._stream is not None else ffi.NULL,
                                                                ffi.from_buffer(_BOOLEAN_PROPERTY_ARRAY, properties) if properties is not None else ffi.NULL,
                                                                num_properties,
                                                                ffi.from_buffer(_BOOLEAN_ARRAY, buffer) if buffer is not None else ffi.NULL)

        if result < 0:
            raise StreamError(result)

    def send(self, buffer, buffer_size):
        """Writes data to the stream buffer. It attempts to store `buffer_size` bytes in the stream buffer. If there is
        enough room available in the stream buffer, then it stores the data in the buffer and returns immediately. The
        data is not written to the actual communication channel until the stream is flushed using `flush` or there is no
        more room available in the stream buffer. If an error occurs, then an exception is raised. If the connection is
        closed, it is considered an error condition.

        If `listen` or `connect` was called with the non-blocking flag set to ``False``, then this function may block
        attempting to flush the stream buffer. All the data will be consumed and the total number of bytes sent is
        returned. Some of the data may remain in the stream buffer and not be sent until the next time `flush` is called
        or there is no more room available in the stream buffer. If an error occurs, then an exception is raised and the
        stream should be closed.

        If `listen` or `connect` was called with the non-blocking flag set to ``True``, then this function does not
        block. It returns the number of bytes sent successfully, which will be between 1 and `buffer_size` (unless
        `buffer_size` is zero). If no bytes could be sent without blocking, then the function raises an exception with
        ``-QERR_WOULD_BLOCK``. If an error occurs, then an exception is raised and the stream should be closed.

        This function does not support two threads calling `send` or `flush` at the same time; however, `send` or
        `flush` may be called by another thread at the same time as `receive`.

        The semantics of this function are comparable to the BSD `send()` socket function.

        Parameters
        ----------
        buffer : array_like
            A buffer of at least `buffer_size` bytes containing the data to be sent.
        buffer_size : int
            The number of bytes to send from the buffer.

        Returns
        -------
        int
            The number of bytes sent, which may be less than `buffer_size` bytes for non-blocking streams. If an error
            occurs, then an exception is raised. A value of zero is only returned if `buffer_size` is zero.

        Raises
        ------
        StreamError
            On non-zero return code. A suitable error message may be retrieved using `get_error_message`.

        Examples
        --------

        Send a text message immediately by writing it to the stream and then flushing the buffer.

        >>> from quanser.communications import Stream
        >>> message = "Hello".encode()
        >>> num_bytes = len(message)
        >>> stream = Stream()
        >>> stream.connect("tcpip://localhost:5000", False, 64, 64)
        >>> bytes_written = stream.send(message, num_bytes)
        >>> stream.flush()
        >>> # ...
        ...
        >>> stream.shutdown()
        >>> stream.close()

        Send 2 doubles immediately by writing them to the stream and then flushing the buffer.

        >>> import struct
        >>> from quanser.communications import Stream
        >>> data = bytearray(struct.pack("!dd", 1.2, 3.4))
        >>> num_bytes = len(data)
        >>> stream = Stream()
        >>> stream.connect("tcpip://localhost:5000", False, 64, 64)
        >>> bytes_written = stream.send(data, num_bytes)
        >>> stream.flush()
        >>> # ...
        ...
        >>> stream.shutdown()
        >>> stream.close()

        """
        result = communications_lib.stream_send(self._stream if self._stream is not None else ffi.NULL,
                                                ffi.from_buffer(buffer) if buffer is not None else ffi.NULL,
                                                buffer_size)

        if result < 0:
            raise StreamError(result)

        return result

    def receive(self, buffer, buffer_size):
        """Receives data over a client stream. It attempts to receive `buffer_size` bytes from the communication
        channel.

        If `listen` or `connect` was called with the non-blocking flag set to ``False``, then this function blocks until
        all the data is read. If the connection has been closed gracefully, then it returns 0 only once there is no more
        data to receive. Otherwise it returns the number of bytes read before the connection closed. Once all the data
        in the stream buffer is exhausted, it will return 0 to indicate the connection has been closed. If an error
        occurs, then it raises an exception.

        If `listen` or `connect` was called with the non-blocking flag set to ``True``, then this function does not
        block. If no data is available at all, then it raises `-QERR_WOULD_BLOCK`. In this case, the `poll` function
        may be used with ``PollFlag.STREAM`` to determine when data becomes available; otherwise, it returns the number
        of bytes received.

        Unlike the `receive_byte_array` function, this function does not require that the stream receive buffer be at
        least `buffer_size` bytes in length. Hence, it allows smaller stream buffers to be used.

        This function does not support two threads calling `receive` at the same time; however, `send` or `flush` may be
        called by another thread at the same time as `receive`.

        The semantics of this function differ from the BSD `recv()` socket function because it receives `buffer_size`
        bytes in blocking mode rather than the number of bytes that were sent in a single `send()` call at the peer. The
        semantics differ because this function attempts to "read ahead" by keeping the stream buffer full, thereby
        minimizing the number of receive operations performed on the internal connection. Also, due to buffering of the
        `send` operation, the number of `send()` calls made at the peer may not correspond to the number expected.

        Parameters
        ----------
        buffer : array_like
            A buffer of at least `buffer_size` bytes in which the received data will be stored.
        buffer_size : int
            The number of bytes available in the buffer.

        Returns
        -------
        int
            The number of bytes received, which may be less than `buffer_size` bytes for non-blocking streams. If no
            more data is available and the connection has been closed gracefully, then 0 is returned. If an error
            occurs, then an exception is raised.

        Raises
        ------
        StreamError
            On non-zero return code. A suitable error message may be retrieved using `get_error_message`.

        Example
        -------

        >>> from quanser.communications import Stream
        >>> buffer_size = 64
        >>> buffer = bytearray(buffer_size)
        >>> stream = Stream()
        >>> stream.connect("tcpip://localhost:5000", False, buffer_size, buffer_size)
        >>> bytes_read = stream.receive(buffer, buffer_size)
        >>> # ...
        ...
        >>> stream.shutdown()
        >>> stream.close()

        """
        result = communications_lib.stream_receive(self._stream if self._stream is not None else ffi.NULL,
                                                   ffi.from_buffer(buffer) if buffer is not None else ffi.NULL,
                                                   buffer_size)

        if result < 0:
            raise StreamError(result)

        return result

    def flush(self):
        """Flushes the stream buffer. It attempts to send the contents of the buffer over the communication channel. If
        an error occurs, then it raises an exception. If the connection is closed, it is considered an error condition.

        If `listen` or `connect` was called with the non-blocking flag set to ``False``, then this function blocks until
        all the data in the buffer is sent.

        If `listen` or `connect` was called with the non-blocking flag set to ``True``, then this function does not
        block. It attempts to send all the data remaining in the stream buffer. However, if this operation would block,
        then it raises ``-QERR_WOULD_BLOCK``, even if it has already sent some of the data. In this case, the `poll`
        function may be used with ``PollFlag.FLUSH`` to determine when at least one more bytes may be flushed.

        This function does not support two threads calling `send` or `flush` at the same time; however, `send` or
        `flush` may be called by another thread at the same time as `receive`.

        Raises
        ------
        StreamError
            On non-zero return code. A suitable error message may be retrieved using `get_error_message`.

        Example
        -------
        Flush the send buffer in order to send a message that is smaller than the size of the buffer.

        >>> from quanser.communications import Stream
        >>> message = "Hello".encode()
        >>> num_bytes = len(message)
        >>> send_buffer_size = 64
        >>> receive_buffer_size = 64
        >>> stream = Stream()
        >>> stream.connect("tcpip://localhost:5000", False, send_buffer_size, receive_buffer_size)
        >>> bytes_written = stream.send(message, num_bytes)
        >>> stream.flush()
        >>> stream.shutdown()
        >>> stream.close()

        """
        result = communications_lib.stream_flush(self._stream if self._stream is not None else ffi.NULL)

        if result < 0:
            raise StreamError(result)

    # endregion

# endregion


from django.dispatch import receiver
from django.db.models.signals import post_save
from users.models import Profile
from users.models import CustomUser

@receiver(post_save, sender=CustomUser)
def create_profile(sender, instance, created, **kwargs):
    if created:
        print('email'+instance.email)
        Profile.objects.create(id=instance.id,
                               email=instance.email,
                               user=instance,
                               first_name=instance.first_name,
                               last_name=instance.last_name)

@receiver(post_save, sender=CustomUser)
def save_profile(sender, instance, created, **kwargs):
    instance.profile.save()


from diffusers import DDPMPipeline
from PIL import Image
import os

image_pipe = DDPMPipeline.from_pretrained('cancer_diffusion_results')
image_pipe.to('cuda')
output_dir = 'output_cancer_images_512'

for i in range(300):
	images = image_pipe().images
	output_img_name = 'generated_cancer_' + str(i) + '.jpg'
	images[0].save(os.path.join(output_dir, output_img_name))


# ---------------------------------------------------------
# Utils Implementation
# Licensed under The MIT License [see LICENSE for details]
# Written by Cheng-Bin Jin
# Email: sbkim0407@gmail.com
# ---------------------------------------------------------

import os
import logging
import tensorflow as tf
import sys

def init_logger(logger, logDir, name, isTrain):
    logger.propagate = False  # solve print log multiple times problem
    fileHandler, streamHandler = None, None

    if isTrain:
        formatter = logging.Formatter(' - %(message)s')

        # File handler
        fileHandler = logging.FileHandler(os.path.join(logDir, name + '.log'))
        fileHandler.setFormatter(formatter)
        fileHandler.setLevel(logging.INFO)

        # Stream handler
        streamHandler = logging.StreamHandler()
        streamHandler.setFormatter(formatter)

        # Add handlers
        if not logger.handlers:
            logger.addHandler(fileHandler)
            logger.addHandler(streamHandler)

    return logger, fileHandler, streamHandler

def print_metrics(itr, kargs):
    print("*** Iteration {}  ====> ".format(itr))
    for name, value in kargs.items():
        print("{} : {}, ".format(name, value))
    print("")
    sys.stdout.flush()


from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware

from models.product import Product

app = FastAPI()

app.add_middleware(
  CORSMiddleware,
  allow_origins=["http://localhost:3000"],
  allow_methods=["*"],
  allow_headers=["*"],
  )

@app.get("/products")
def list_products():
  return [format(pk) for pk in Product.all_pks()]

def format(pk: str):
  product = Product.get(pk)

  return {
    "id": product.pk,
    "name": product.name,
    "price": product.price,
    "quantity": product.quantity
    }

@app.post("/products")
def create_products(product: Product):
  return product.save()

@app.get("/products/{pk}")
def get_one_product(pk: str):
  return Product.get(pk)

@app.delete("/products/{pk}")
def delete_one_product(pk: str):
  product = Product.get(pk)
  product_name = product.name

  Product.delete(pk)
  return f"{product_name} deleted"