import os
import sys
import argparse

latent_dim= 2
total_blocks= 2
learning_rate= 1e-3
batch_size= 64
train_size= 20000
plot_case= 'extrapolate'
latent_case= 'balls_supp_l_shape'
seed_grid= range(10)
input_normalization_grid= ['none']

method_grid= ['ae_base', 'ae_additive']
eval_latent_grid= ['balls_supp_l_shape', 'balls_supp_extrapolate']

#Disconnected support experiments
# latent_case= 'balls_supp_disconnected'
#method_grid= ['ae_base']

# Input Parsing
parser = argparse.ArgumentParser()
parser.add_argument('--case', type=str, default='train',
                   help= '')
parser.add_argument('--curr_method', type=str, default='none',
                   help= '')
parser.add_argument('--curr_seed', type=int, default=-1,
                    help='')

args = parser.parse_args()
case= args.case
curr_method= args.curr_method
curr_seed= args.curr_seed

if case == 'train':
    for method in method_grid:
        if method != curr_method and curr_method!='none':
            continue

        for seed in seed_grid:
            if seed != curr_seed and curr_seed!=-1:
                continue

            for input_normalization in input_normalization_grid:
                script= 'sbatch scripts/slurm_launcher.sh' \
                        + ' ' + str(latent_case) \
                        + ' ' + method \
                        + ' ' + str(latent_dim) \
                        + ' ' + str(total_blocks) \
                        + ' ' + str(train_size) \
                        + ' ' + str(seed) \
                        + ' ' + str(input_normalization) \
                        + ' ' + str(batch_size) \
                        + ' ' + str(learning_rate)
                os.system(script)

elif case == 'eval':
    for method in method_grid:
        if method != curr_method and curr_method!='none':
            continue

        for eval_latent in eval_latent_grid:

            for input_normalization in input_normalization_grid:

                script= 'sbatch scripts/slurm_launcher_eval.sh' \
                        + ' ' + str(latent_case) \
                        + ' ' + method \
                        + ' ' + str(latent_dim) \
                        + ' ' + str(total_blocks) \
                        + ' ' + str(train_size) \
                        + ' ' + str(input_normalization) \
                        + ' ' + str(batch_size) \
                        + ' ' + str(learning_rate) \
                        + ' ' + eval_latent \
                        + ' ' + plot_case
                os.system(script)


"""Helper functions for the pluginmanager plugin"""

import os
import shutil
import urllib.request
import json
import zipfile
import pkgutil
import importlib
import logging

from . import constants
from enki.core.core import core
from .constants import PLUGIN_DIR_PATH, REPO

# Data Definitions
# ==================
# userpluginEntry consist of
# {'module': Module
#  'plugin': Plugin or None,
#  'isloaded': Bool,
#  'name': String,
#  'author': String,
#  'version': String,
#  'doc': String}
#
# ListOfUserpluginEntry is one of:
# - []
# - lou.append(userpluginEntry)

def create_UE(module, isLoaded, modulename, pluginname,
              author, version, doc, plugin=None):
    """Create a new userpluginEntry"""
    return {'module': module,
            'plugin': plugin,
            'isLoaded': isLoaded,
            'modulename': modulename,
            'pluginname': pluginname,
            'author': author,
            'version': version,
            'doc': doc}

def loadPlugin(pluginEntry):
    """Consume a pluginEntry and load the plugin into core._loadedPlugins,
    based on it's 'isLoaded' property
    return pluginEntry
    """
    if pluginEntry['isLoaded'] is True:
        pluginEntry['plugin'] = pluginEntry['module'].Plugin()
        core.loadedPlugins().append(pluginEntry['plugin'])
    return pluginEntry

def unloadPlugin(pluginEntry):
    """Consume a pluginEntry and unload the plugin from core._loadedPlugins,
    based on it's 'isLoaded' property
    return pluginEntry
    """
    if pluginEntry['isLoaded'] is False and \
       pluginEntry['plugin'] is not None:
        idx = core.loadedPlugins().index(pluginEntry['plugin'])
        plugin = core.loadedPlugins().pop(idx)
        plugin.terminate()
        pluginEntry['plugin'] = None
    return pluginEntry

def deletePlugin(pluginEntry, pluginDirPath=PLUGIN_DIR_PATH):
    """Consume a pluginEntry and delete the plugin directory or file"""
    unloadPlugin(pluginEntry)
    dirpath = os.path.join(pluginDirPath, pluginEntry["modulename"])
    filepath = os.path.join(pluginDirPath, pluginEntry["modulename"] + '.py')
    if os.path.isdir(dirpath):
        shutil.rmtree(dirpath)
    elif os.path.exists(filepath):
        os.remove(filepath)
    else:
        logging.warning("Could not find module %s. Did not delete anything." %
              pluginEntry["modulename"])

def inUserPlugins(modulename, lope):
    """String ListOfUserpluginEntry -> Bool
    Consumes a modulename and a list of pluginEntrys
    return True if ListOfUserpluginEntry has PluginEntry with modulename
    """
    for pluginEntry in lope:
        if pluginEntry["modulename"] == modulename:
            return True
    return False

def isPluginInstalled(name, lope):
    """String ListOfUserpluginEntry -> Bool
    Consumes a plugin name and a list of pluginEntrys
    returns the plugin if ListOfUserpluginEntry has PluginEntry
            with pluginname name
    """
    for pluginEntry in lope:
        if pluginEntry["pluginname"] == name:
            return pluginEntry
    return False

def getRepo(repo=REPO):
    """Void -> Dictionary
    Loads the repo and returns it as a dictionary"""
    try:
        url = urllib.request.urlopen(repo)
    except Exception as e:
        repo = constants.EMPTY_REPO
    else:
        rawData = url.read().decode()
        repo = json.loads(rawData)
        logging.debug(repo)
    return repo

def downloadPlugin(url, tmpPath):
    """String String -> bool
    Consumes the url of a plugin archive and the path to save it
    return True if download succeeded, else False"""
    try:
        request = urllib.request.urlretrieve(url, tmpPath)
        logging.debug(request)
    except Exception as e:
        logging.exception("The download could not finish.")
        return False
    else:
        logging.info("The plugin has been downloaded")
        return tmpPath

def extractPlugin(filePath, pluginDirPath=PLUGIN_DIR_PATH):
    """String -> Bool
    Consumes the filePath of a plugin archive and extracts it to the
    userplugins-directory, return True if extract succeeded, els False"""
    try:
        zipref = zipfile.ZipFile(filePath, 'r')
        zipref.extractall(pluginDirPath)
        zipref.close()
    except Exception as e:
        logging.exception("Could not extract plugin")
        os.remove(filePath)
        return False
    else:
        logging.info("Plugin extracted")
        os.remove(filePath)
        logging.info("Plugin deleted")
        return True

def renamePluginFolder(oldName, newName, pluginDirPath=PLUGIN_DIR_PATH):
    """String String -> Bool
    Consumes the oldName and NewName of a plugin folder and renames it in the
    userplugins-directory, return True if rename succeeded, else False"""
    try:
        os.rename(os.path.join(pluginDirPath, oldName),
                  os.path.join(pluginDirPath, newName))
    except Exception as e:
        logging.exception("Could not rename plugin directory")
        return False
    else:
        logging.info("Plugin directory renamed to %s." % newName)
        return True

def getPlugins(pluginDirPath=PLUGIN_DIR_PATH):
    """String -> ListOfUserpluginEntry
    Loads all userplugins and returns them as a ListOfUserpluginEntry"""
    userPlugins = []
    for loader, name, isPackage in pkgutil.iter_modules([pluginDirPath]):
        if not inUserPlugins(name, userPlugins):
            userPlugin = getPlugin(name, pluginDirPath)
            if userPlugin:
                userPlugins.append(userPlugin)
    return userPlugins

def getPlugin(name, pluginDirPath=PLUGIN_DIR_PATH):
    """String (String) -> PluginEntry
    get plugin by it's module name and returns userpluginEntry
    """
    module = importlib.import_module('userplugins.%s' % name)
    try:
        pluginEntry = create_UE(
            module,
            shouldPluginLoad(name),
            name,
            module.__pluginname__,
            module.__author__,
            module.__version__,
            module.__doc__)
        return pluginEntry
    except AttributeError:
        logging.exception("Plugin %s misses required attributes." % name)
        return False

def initPlugins():
    """Loads all userplugins and returns them as a ListOfUserpluginEntry"""
    userPlugins = getPlugins()
    for up in userPlugins:
        loadPlugin(up)
    return userPlugins

def initPlugin(name):
    """Load plugin by it's module name
    returns userpluginEntry
    """
    return loadPlugin(getPlugin(name))

def shouldPluginLoad(name):
    """String Dict -> Bool
    Consumes a name of a plugin and the enki configuration and
    checks in the settings if it should be loaded.
    If no setting is available for the plugin, it gets created.
    Returns the setting (Bool)
    """
    if name not in core.config()["PluginManager"]["Plugins"]:
        core.config()["PluginManager"]["Plugins"][name] = {}
    if "Enabled" not in core.config()["PluginManager"]["Plugins"][name]:
        core.config()["PluginManager"]["Plugins"][name]["Enabled"] = False
    return core.config()["PluginManager"]["Plugins"][name]["Enabled"]


import os
import shutil

import pytest

from app import application

def rel_path(path):
    return os.path.relpath(
        os.path.abspath(os.path.join(os.path.dirname(__file__), path))
    )

@pytest.mark.main
def test_initialize_repo():
    try:
        shutil.rmtree("bitcointranscripts", ignore_errors=True)
    except OSError as e:
        print(f"Error occurred while removing directory: {e}")
        assert False

@pytest.mark.feature
def test_find_source_type():
    video = application.check_source_type("B0HW_sJ503Y") == "video"
    video1 = (
        application.check_source_type(
            "https://www.youtube.com/watch?v=B0HW_sJ503Y"
        )
        == "video"
    )
    video2 = (
        application.check_source_type("https://youtu.be/B0HW_sJ503Y") == "video"
    )
    video3 = (
        application.check_source_type("https://youtube.com/embed/B0HW_sJ503Y")
        == "video"
    )
    video4 = (
        application.check_source_type("youtube.com/watch?v=B0HW_sJ503Y")
        == "video"
    )
    video5 = (
        application.check_source_type(
            "www.youtube.com/watch?v=B0HW_sJ503Y&list"
        )
        == "video"
    )
    video6 = (
        application.check_source_type("https://youtube.com/watch?v=B0HW_sJ503Y")
        == "video"
    )

    playlist = (
        application.check_source_type("PLPQwGV1aLnTuN6kdNWlElfr2tzigB9Nnj")
        == "playlist"
    )
    playlist1 = (
        application.check_source_type(
            "https://www.youtube.com/playlist?"
            "list=PLPQwGV1aLnTuN6kdNWlElfr2tzigB9Nnj"
        )
        == "playlist"
    )
    playlist2 = (
        application.check_source_type(
            "www.youtube.com/playlist?list=PLPQwGV1aLnTuN6kdNWlElfr2tzigB9Nnj"
        )
        == "playlist"
    )
    playlist3 = (
        application.check_source_type(
            "https://youtube.com/playlist?list=PLPQwGV1aLnTuN6kdNWlElfr2tzigB9N"
            "nj"
        )
        == "playlist"
    )
    playlist4 = (
        application.check_source_type(
            "https://www.youtube.com/watch?v=B0HW_sJ503Y&list"
            "=PLPQwGV1aLnTuN6kdNWlElfr2tzigB9Nnj"
        )
        == "playlist"
    )
    audio = (
        application.check_source_type(
            "https://anchor.fm/s/12fe0620/podcast/play/32260353/https%3A%2F%2Fd"
            "3ctxlq1ktw2nl.cloudfront.net%2Fstaging%2F2021-3-26%2Fdc6f12e7-a547"
            "-d872-6ef6-7acfe755a692.mp3"
        )
        == "audio"
    )
    assert (
        audio
        and playlist
        and video
        and video1
        and video2
        and video3
        and video4
        and video5
        and video6
        and playlist1
        and playlist2
        and playlist3
        and playlist4
    )

def test_download_audio_file():
    if not os.path.isdir("tmp"):
        os.mkdir("tmp")
    audio = application.get_audio_file(
        "https://dcs.megaphone.fm/FPMN6776580946.mp3", "test"
    )
    print("audio", audio)
    assert os.path.isfile(audio)
    os.remove(audio)

def test_download_video_file():
    if not os.path.isdir("tmp"):
        os.mkdir("tmp")
    url = "https://www.youtube.com/watch?v=B0HW_sJ503Y"
    video = application.download_video(url)
    assert os.path.isfile(video) and os.path.isfile("tmp/videoFile.info.json")
    print()
    os.remove(video)
    os.remove("tmp/videoFile.info.json")
    shutil.rmtree("tmp")

@pytest.mark.main
def test_convert_video_to_audio():
    if not os.path.isdir("tmp/"):
        os.makedirs("tmp/")
    application.convert_video_to_mp3(rel_path("testAssets/test_video.mp4"))
    assert os.path.isfile("tmp/test_video.mp3")
    shutil.rmtree("tmp/")


import sys
input=sys.stdin.readline

def P_update(i,n):
 global x
 l=len(x)
 while i<l:
  x[i]+=n
  i+=i&-i

def P_sum(i):
 global x
 s=0
 while i>0:
  s+=x[i]
  i-=i&-i
 return s

n,m,k=map(int,input().split())
x=[0]*(n+1)
for i in range(1,n+1):
 P_update(i,int(input()))

for i in range(m+k):
 o,a,b=map(int,input().split())
 if o==1:
  diff=b-(P_sum(a)-P_sum(a-1))
  P_update(a,diff)
 else: print(P_sum(b)-P_sum(a-1))


__author__ = 'brayden'

from tornado.web import asynchronous
from tornado.web import authenticated
from tornado.ioloop import IOLoop
from . import BaseServerAjaxHandler
from Minecraft.backup import Local
from functools import partial
from datetime import timedelta

class BackupServerHandler(BaseServerAjaxHandler):
    @asynchronous
    @authenticated
    def post(self, server_id):
        destination = self.application.db.get_backup_destination_by_id(self.get_argument('destination_id'))
        if not destination.Remote:
            backup = Local(destination.ID, destination.Folder)
            server_name = self.application.process_prefix + server_id
            server_folder = self.application.config.get('minecraft', 'home') + '/%s%s/' % (self.application.process_prefix, server_id)
            if destination.Backup_Limit == 0 or self.application.usernames[self.current_user]['Is_Admin'] or not len(backup.get_backups_by_server(server_name)) >= destination.Backup_Limit:
                self.application.supervisor.run_command(server_name, 'save-off')
                IOLoop.instance().add_timeout(timedelta(seconds=2), partial(self.callback, backup, server_name, server_folder))
            else:
                self.finish({"result": {"message": "Reached backup limit.", "success": False}})
        else:
            self.finish({"result": {"message": "Invalid destination argument given.", "success": False}})

    def callback(self, backup, server_name, server_folder):
        if self.get_argument('backup_type') == 'tar':
            backup.backup_server_tar(server_name, server_folder)
            self.application.supervisor.run_command(server_name, 'save-on')
            self.finish({"result": {"message": None, "success": True}})
        elif self.get_argument('backup_type') == 'zip':
            backup.backup_server_zip(server_name, server_folder)
            self.application.supervisor.run_command(server_name, 'save-on')
            self.finish({"result": {"message": None, "success": True}})
        else:
            self.finish({"result": {"message": "Invalid backup_type given.", "success": False}})


#!/usr/bin/env python
from __future__ import absolute_import, print_function

import sys
import argparse

import numpy as np
import re
from numpy.linalg import norm

try:
    import matplotlib.pyplot as plt
except ImportError:
    print("Matplotlib not installed; do not use plotting functions.")

# PN modules from dislopy package
from dislopy.pn import pn_1D as pn1
from dislopy.pn import pn_2D as pn2
from dislopy.pn import fit_gsf as fg
from dislopy.pn import peierls_barrier as pb
from dislopy.pn import energy_coeff as coeff
from dislopy.pn import visualise_pn as vp
from dislopy.atomic import aniso
from dislopy.utilities.control_functions import control_file, from_mapping, change_type,  \
                                            to_bool, change_or_map, print_control

atomic_to_GPa =  160.2176487

def to_vector(in_str, datatype=float):
    '''Converts <in_str>, which is a list of numbers, in an array with elements
    of type float.
    '''

    new_vec = []
    for x in re.finditer('-?\d+\.?\d*', in_str):
        new_vec.append(datatype(x.group()))

    return np.array(new_vec, dtype=datatype)

def to_int_vector(in_str):
    '''Calls <to_vector> with datatype=int. Used for parsing the PN input file.
    '''

    arr = to_vector(in_str, datatype=int)
    return arr

def handle_pn_control(param_dict):
    '''handle each possible card in turn. If default value is <None>, the card is
    considered "mission critical" and the program will abort if a value is not
    provided.

    TO DO: List defining control parameters
    '''

    # cards for the <&control> namelist. If <run_sim> is False, we won't actually
    # run the PN simulation. Do this only if you want to visualise the results
    # without re-running the simulation
    control_cards = (('run_sim', {'default':True, 'type':to_bool}),
                     ('gsf_file', {'default':None, 'type':str}),
                     ('output', {'default':'dislopyPN.out', 'type':str}),
                     ('title_line', {'default':'Peierls-Nabarro model', 'type':str}),
                     ('n_iter', {'default':1, 'type':int}),
                     ('dimensions', {'default':2, 'type':int}),
                     ('max_x', {'default':100, 'type':int}),
                     ('n_funcs', {'default':np.nan, 'type':int}),
                     ('disl_type', {'default':'edge', 'type':str}),
                     ('use_sym', {'default':True, 'type':to_bool}),
                     ('plot', {'default':False, 'type':to_bool}),
                     ('plot_name', {'default':'pn_plot.tif', 'type':str}),
                     ('plot_both', {'default':False, 'type':to_bool}),
                     ('visualize', {'default':False, 'type':to_bool}),
                     ('record', {'default':True, 'type':to_bool}),
                     ('nplanes', {'default':20, 'type':int}),
                     ('noisy', {'default': False, 'type': to_bool}),
                   )

    # cards for the <&struc> namelist. Need to find some way to incorporate
    # anisotropic elasticity
    struc_cards = (('a', {'default':None, 'type':float}),
                   ('b', {'default':'map struc > a: x -> x', 'type':float}),
                   ('burgers', {'default':'map struc > a: x -> x', 'type':float}),
                   ('spacing', {'default':'map struc > a: x -> x', 'type':float}),
                   ('parameters', {'default':np.array([]), 'type':to_vector})
                  )

    # cards for the &elast namelist. <k_para> and <k_norm> allow the user to
    # supply predetermined elastic coefficients for the systems parallel and
    # perpendicular to the Burgers vector
    elast_cards = (('bulk', {'default':None, 'type':float}),
                   ('shear', {'default':None, 'type':float}),
                   ('poisson', {'default':None, 'type':float}),
                   ('k_parallel', {'default':None, 'type':float}),
                   ('k_normal', {'default':np.nan, 'type':float}),
                   ('cij', {'default':None, 'type':aniso.readCij}),
                   ('b_edge', {'default':np.array([1., 0., 0.]), 'type':to_vector}),
                   ('b_screw', {'default':np.array([0., 0., 1.]), 'type':to_vector}),
                   ('normal', {'default':np.array([0., 1., 0.]), 'type':to_vector}),
                   ('in_gpa', {'default':True, 'type':to_bool})
                  )

    # cards for the <&surface> namelist. Where dimensions == 1 but the gsf
    # file is two-dimensional (ie. a gamma surface), <map_ux> corresponds to the
    # gamma line direction. If <dimensions> == 2, <map_ux> (<max_uy>) is the
    # direction of the edge (screw) component of the displacement.
    surf_cards = (('x_length', {'default':'map struc > a: x -> x', 'type':float}),
                  ('y_length', {'default':'map struc > b: x -> x', 'type':float}),
                  ('angle', {'default':np.pi/2, 'type':float}),
                  ('map_ux', {'default':'remap: (ux, uy) -> ux', 'type':str}),
                  ('map_uy', {'default':'remap: (ux, uy) -> uy', 'type':str}),
                  ('gamma_shift', {'default':0., 'type':float}),
                  ('has_vacuum', {'default':True, 'type':to_bool}),
                  ('do_fourier', {'default':False, 'type':to_bool}),
                  ('fourier_N', {'default':3, 'type': int}),
                  ('fourier_M', {'default':-1, 'type':int})
                 )

    # cards for the <&properties> namelist
    prop_cards = (('max_rho', {'default':True, 'type':to_bool}),
                  ('width', {'default':True, 'type':to_bool}),
                  ('center', {'default':True, 'type':to_bool})
                 )

    # cards for the <&stress> namelist
    stress_cards = (('calculate_stress', {'default':True, 'type':to_bool}),
                    ('calculate_barrier', {'default':False, 'type':to_bool}),
                    ('dtau', {'default':0.0001, 'type':float}),
                    ('use_GPa', {'default':True, 'type':to_bool}),
                    ('threshold', {'default':0.5, 'type':float})
                   )

    # cards for the <&visualize> namelist
    vis_cards = (('xyz_name', {'default':'pn.xyz', 'type':str}),
                 ('unitcell', {'default':'', 'type':str}),
                 ('path_to_cell', {'default':'./', 'type':str}),
                 ('threshold', {'default':1., 'type':float}),
                 ('sym_thresh', {'default':0.3, 'type':float}),
                 ('radius', {'default':10., 'type':float}),
                 ('program', {'default':'', 'type':str}),
                 ('shift', {'default':np.zeros(3), 'type':to_vector}),
                 ('permutation', {'default':np.array([0, 1, 2], dtype=int), 'type':to_int_vector}),
                 ('thickness', {'default': 1, 'type':int})
                )

    # list of valid namelists
    namelists = ['control', 'struc', 'elast', 'surface', 'properties', 'stress',
                                                        'visualize']
    # check that all namelists were in the control file and add them as keys
    # (with empty dicts as values) in the control dictionary
    for name in namelists:
        if name not in param_dict.keys():
            param_dict[name] = dict()

    # handle the input namelists, except for the elasticity namelist, which
    # requires special handling.
    for i, cards in enumerate([control_cards, struc_cards, elast_cards, surf_cards,
                                               prop_cards, stress_cards, vis_cards]):
        if namelists[i] == 'elast':
            # handle the elastic parameters later
            continue
        # else
        for var in cards:
            try:
                change_or_map(param_dict, namelists[i], var[0], var[1]['type'])
            except ValueError:
                default_val = var[1]['default']
                # test to see if variable is "mission-critical" using None != None
                if default_val is None:
                    raise ValueError("No value supplied for mission-critical" +
                                                 " variable {}.".format(var[0]))
                else:
                    param_dict[namelists[i]][var[0]] = default_val
                    # if <default_val> is a mapping, needs to be evaluated
                    if type(default_val) == str and 'map' in default_val:
                        from_mapping(param_dict, namelists[i], var[0])

    # extract values from the &elast namelist
    for var in elast_cards:
        try:
            change_or_map(param_dict, 'elast', var[0], var[1]['type'])
        except ValueError:
            param_dict['elast'][var[0]] = var[1]['default']

    # test to make sure that all of the parameters required for one of isotropic
    # or anisotropic elasticity have been supplied. Should we also test to see
    # which function to use when calculating the energy coefficients?
    if not (param_dict['elast']['k_parallel'] is None):
        param_dict['elast']['coefficients'] = 'specific'
    elif not (param_dict['elast']['cij'] is None):
        # if an elastic constants tensor is given, use anisotropic elasticity
        param_dict['elast']['coefficients'] = 'aniso'
    elif not (param_dict['elast']['shear'] is None):
        # test to see if another isotropic elastic property has been provided.
        # Preference poisson's ratio over bulk modulus, if both have been provided
        if not (param_dict['elast']['poisson']  is None):
            param_dict['elast']['coefficients'] = 'iso_nu'
        elif not (param_dict['elast']['bulk'] is None):
            param_dict['elast']['coefficients'] = 'iso_bulk'
        else:
            raise AttributeError("No elastic properties have been provided.")
    else:
        raise AttributeError("No elastic properties have been provided.")

    return

class PNSim(object):
    # handles the control file for a PN simulation in <dislopy>

    def __init__(self, filename):
        self.sim = control_file(filename)
        handle_pn_control(self.sim)

        # functions to access values in namelists easily
        self.control = lambda card: self.sim['control'][card]
        self.struc = lambda card: self.sim['struc'][card]
        self.elast = lambda card: self.sim['elast'][card]
        self.surf = lambda card: self.sim['surface'][card]
        self.prop = lambda card: self.sim['properties'][card]
        self.stress = lambda card: self.sim['stress'][card]
        self.vis = lambda card: self.sim['visualize'][card]

        if self.control('run_sim'):
            # calculate the energy coefficients
            if self.elast('coefficients') == 'specific':
                self.K = coeff.predefined(self.elast('k_parallel'), self.elast('k_normal'),
                                                    using_atomic=not(self.elast('in_gpa')))
            elif self.elast('coefficients') == 'aniso':
                self.K = coeff.anisotropic_K(self.elast('cij'),
                                             self.elast('b_edge'),
                                             self.elast('b_screw'),
                                             self.elast('normal'),
                                             using_atomic=not(self.elast('in_gpa'))
                                            )
            elif self.elast('coefficients') == 'iso_nu':
                self.K = coeff.isotropic_nu(self.elast('poisson'), self.elast('shear'),
                                                 using_atomic=not(self.elast('in_gpa')))
            elif self.elast('coefficients') == 'iso_bulk':
                self.K = coeff.isotropic_K(self.elast('bulk'), self.elast('shear'),
                                               using_atomic=not(self.elast('in_gpa')))

            # if restricting calculation to one component of displacement, extract
            # the appropriate energy coefficient.
            if self.control('dimensions') == 1:
                # store a copy of the edge and screw energy coefficients
                self.K_store = self.K
                if self.control('disl_type').lower() == 'edge':
                    self.K = self.K[0]
                else:
                    # for the moment, default to shear
                    self.K = self.K[1]

            # construct the gamma surface/gamma line
            if self.control('noisy'):
                print("Constructing gamma surface/line")
            self.construct_gsf()

            # calculate fit parameters and energy of the dislocation
            if self.control('noisy'):
                print("Relaxing dislocation core structure.")

            self.run_sim()

            if self.control('noisy'):
                print("Core relaxation complete.")

            # calculate Peierls stress and Peierls barrier, if requested
            if self.control('noisy'):
                print('Calculating Peierls stress.')

            self.peierls()

            if self.control('noisy'):
                print('Peierls stress calculated.')

            # do post-processing and/or plotting, if requested by the user
            if self.control('noisy'):
                print('Doing post-processing.')
            self.post_processing()

            # write results to ouput
            self.write_output()

        if self.control('visualize'):
            if self.control('noisy'):
                print('Visualising core structure.')
            self.visualise()

    def run_sim(self):
        '''Calculates an optimum(ish) dislocation structure.
        '''

        # check to see if the user has provided parameters specifying a trial
        # disregistry profile (in the order A x0 c).
        if len(self.struc('parameters')) != 0:
            # use supplied parameters and perform only a single energy minimization
            inpar = self.struc('parameters')
            niter = 1
        else:
            # no parameters -> tell program that it should generate a trial
            # configuration
            inpar = None
            niter = self.control('n_iter')

        if self.control('dimensions') == 1:
            # calculate dislocation energy and disregistry profile
            self.E, self.par = pn1.run_monte1d(
                                               niter,
                                               self.control('n_funcs'),
                                               self.K,
                                               max_x=self.control('max_x'),
                                               energy_function=self.gsf,
                                               use_sym=self.control('use_sym'),
                                               b=self.struc('burgers'),
                                               spacing=self.struc('spacing'),
                                               noisy=self.control('noisy'),
                                               params=inpar
                                              ) # Done Monte Carlo 1D

        elif self.control('dimensions') == 2:
            self.E, self.par = pn2.run_monte2d(
                                               niter,
                                               self.control('n_funcs'),
                                               self.control('disl_type'),
                                               self.K,
                                               max_x=self.control('max_x'),
                                               energy_function=self.gsf,
                                               use_sym=self.control('use_sym'),
                                               b=self.struc('burgers'),
                                               spacing=self.struc('spacing'),
                                               noisy=self.control('noisy'),
                                               params=inpar
                                              ) # Done Monte Carlo 2D

        return

    def construct_gsf(self):
        '''Makes a function mapping disregistry values to misfit energies.
        '''

        gsf_grid, self.units = fg.read_numerical_gsf(self.control('gsf_file'))

        # determine which fitting function to use from the shape of <gsf_grid>
        # original test was self.control("dimensions")
        n_columns = np.shape(gsf_grid)[-1]
        if n_columns == 2:
            self.gsf = fg.spline_fit1d(gsf_grid, self.surf('x_length'),
                                                 self.surf('y_length'),
                                                 angle=self.surf('angle'),
                                                 units=self.units,
                                                 hasvac=self.surf('has_vacuum'),
                                                 do_fourier_fit=self.surf('do_fourier'),
                                                 n_order=self.surf('fourier_N'))

        elif n_columns == 3: # 2-dimensional misfit function
            # fit the gamma surface
            if self.control('dimensions') == 2:
                if self.surf('fourier_M') < 0:
                    m_order = self.surf('fourier_N')
                else:
                    m_order = self.surf('fourier_M')

                base_func = fg.spline_fit2d(gsf_grid, self.surf('x_length'),
                                                      self.surf('y_length'),
                                                   angle=self.surf('angle'),
                                                           units=self.units,
                                             hasvac=self.surf('has_vacuum'),
                                     do_fourier_fit=self.surf('do_fourier'),
                                             n_order=self.surf('fourier_N'),
                                             m_order=m_order)

                self.gsf = fg.new_gsf(base_func, self.surf('map_ux'), self.surf('map_uy'))

            elif self.control('dimensions') == 1:
                # begin by performing a spline fit to the surface
                base_func = fg.spline_fit2d(gsf_grid, self.surf('x_length'),
                                                      self.surf('y_length'),
                                                   angle=self.surf('angle'),
                                                           units=self.units,
                                             hasvac=self.surf('has_vacuum'),
                                                       do_fourier_fit=False)

                # remap input so that x and y correspond to ux and uy
                temp_gsf = fg.new_gsf(base_func, self.surf('map_ux'), self.surf('map_uy'))

                # project out the dislocation parallel component
                # determine which axis to use (0/x if edge, 1/y if screw)
                if self.control('disl_type') == 'edge':
                    use_axis = 0
                elif self.control('disl_type') == 'screw':
                    use_axis = 1

                temp_1d = fg.projection(temp_gsf, self.surf('gamma_shift'), use_axis)

                # set the correct periodicity
                temp_1d2 = lambda x: temp_1d(x % self.struc('burgers'))

                if self.surf('do_fourier'):
                    # fit a fourier series to <temp_1d2>.
                    self.gsf = fg.gline_fourier(temp_1d, self.surf('fourier_N'),
                                                           self.struc('burgers'))
                else:
                    self.gsf = temp_1d2

        else:
            raise ValueError("GSF grid has invalid number of dimensions")

        return

    def post_processing(self):
        '''Calculate the displacement field and dislocation density, record these
        values in appropriate output files, plot them (if requested to by the
        user), and then calculate the dislocation height and width.
        '''

        if (self.control('plot') or self.prop('max_rho') or self.prop('width') or
                                                        self.control('record')):

            if self.control('record'):
                # open output files for the disregistry and dislocation density
                outstreamu = open('disreg.{}'.format(self.control('output')), 'w')
                outstreamrho = open('rho.{}'.format(self.control('output')), 'w')

            # lattice points
            r = self.struc('spacing')*np.arange(-self.control('max_x'), self.control('max_x'))

            # calculate misfit profile and dislocation density and write to output
            # files
            if self.control('dimensions') == 1:
                self.ux = pn1.get_u1d(self.par, self.struc('burgers'),
                                   self.struc('spacing'), self.control('max_x'))

                # record disregistry and dislocation density
                if self.control('record'):
                    # header for output files
                    outstreamu.write('# r (\AA) u (\AA)\n')
                    outstreamrho.write('# r (\AA) rho\n')

                    # record values of rho and u at each lattice point
                    rho = pn1.rho(self.ux, r)
                    for i in range(len(r)):
                        outstreamu.write('{:.3f} {:.6f}\n'.format(r[i], self.ux[i]))
                        if i != 0:
                            # rho not defined for first lattice plane
                            # Record at intervals between lattice planes
                            ri = 0.5*(r[i]+r[i-1])
                            outstreamrho.write('{:.3f} {:.6f}\n'.format(ri, rho[i-1]))

                    outstreamu.close()
                    outstreamrho.close()

            elif self.control('dimensions') == 2:
                self.ux, self.uy = pn2.get_u2d(self.par, self.struc('burgers'),
                                    self.struc('spacing'), self.control('max_x'),
                                                      self.control('disl_type'))

                # record both components of disregistry and dislocation density
                if self.control('record'):
                    # header lines
                    outstreamu.write('# r (\AA) ux (\AA) uy (\AA)\n')
                    outstreamrho.write('# r (\AA) rhox  rhoy\n')

                    # record values of u and rho at each lattice point
                    rhox = pn1.rho(self.ux, r)
                    rhoy = pn1.rho(self.uy, r)
                    for i in range(len(r)):
                        outstreamu.write('{:.3f} {:.6f} {:.6f}\n'.format(r[i], self.ux[i],
                                                                              self.uy[i]))
                        if i != 0:
                            # as above, rho not defined for first lattice plane.
                            # Record at intervals between lattice planes
                            ri = 0.5*(r[i]+r[i-1])
                            outstreamrho.write('{:.3f} {:.6f} {:.6f}\n'.format(ri,
                                                                rhox[i-1], rhoy[i-1]))

                    outstreamu.close()
                    outstreamrho.close()

            # create plot of dislocation density and misfit profile
            if self.control('plot'):
                if self.control('dimensions') == 1:
                    self.fig, self.ax = pn1.plot_both(self.ux, r, self.struc('burgers'),
                                                        self.struc('spacing'),
                                                        nplanes=self.control('nplanes'))
                    plt.savefig(self.control('plot_name'))
                    plt.close()
                elif self.control('plot_both'): # dimension == 2
                    self.fig, self.ax = pn1.plot_both(self.ux, r, self.struc('burgers'),
                                                               self.struc('spacing'),
                                                        nplanes=self.control('nplanes'))
                    plt.savefig('edge.' + self.control('plot_name'))
                    plt.close()
                    self.fig, self.ax = pn1.plot_both(self.uy, r, self.struc('burgers'),
                                                               self.struc('spacing'),
                                                        nplanes=self.control('nplanes'))
                    plt.savefig('screw.' + self.control('plot_name'))
                    plt.close()
                else: # dimensions == 2
                    if self.control('disl_type') in 'edge':
                        self.fig, self.ax = pn1.plot_both(self.ux, r, self.struc('burgers'),
                                                               self.struc('spacing'),
                                                        nplanes=self.control('nplanes'))
                    else: # screw
                        self.fig, self.ax = pn1.plot_both(self.uy, r, self.struc('burgers'),
                                                               self.struc('spacing'),
                                                        nplanes=self.control('nplanes'))
                    plt.savefig(self.control('plot_name'))
                    plt.close()

            # calculate dislocation width and height
            if (self.control('disl_type') in 'edge'
                or self.control('dimensions') == 1):
                self.rhos = pn1.rho(self.ux, r)
            else: # screw
                self.rhos = pn1.rho(self.uy, r)

            if self.control('disl_type') in 'edge' or self.control('dimensions') == 1:
                self.dis_width = pn1.dislocation_width(self.ux, r, self.struc('burgers'))
            else: # screw dislocation with edge component of displacement
                self.dis_width = pn1.dislocation_width(self.uy, r, self.struc('burgers'))

            self.max_density = pn1.max_rho(self.rhos, self.struc('spacing'))

            # calculate the centre of mass
            if self.control('dimensions') == 1:
                self.com = pn1.com_from_pars(self.par, self.struc('burgers'),
                               self.struc('spacing'), self.control('max_x'))
            else:
                self.com = pn2.com_from_pars2d(self.par, self.struc('burgers'),
                                  self.struc('spacing'), self.control('max_x'),
                                                      self.control('disl_type'))

        return

    def peierls(self):
        '''Calculate the Peierls stress for the lowest-energy dislocation.
        '''

        if self.stress('calculate_barrier'):
            # calculate change in dislocation energy with position
            e_shifted, pars = pb.shift_energies(self.par,
                                          self.control('max_x'),
                                          self.gsf,
                                          self.K,
                                          self.struc('burgers'),
                                          self.struc('spacing'),
                                          dims=self.control('dimensions'),
                                          disl_type=self.control('disl_type'))

            # Check to see if any of the shifted energies is lower than the
            # original energy calculated for the dislocation. If so, use the
            # parameters for the shifted dislocation
            En = e_shifted[np.argmin(e_shifted[:, 1]), 1]
            if En < self.E:
                self.E = En
                self.par = pars[np.argmin(e_shifted[:, 1])]

            self.wp_shift = e_shifted[:, 1].max()-e_shifted[:, 1].min()

            # calculate the Peierls stress
            self.taup_shift = pb.sigmap_from_wp(self.struc('spacing'), e_shifted[:, 1],
                                                        self.struc('burgers'))
            if self.stress('use_GPa'):
                self.taup_shift *= atomic_to_GPa

            if self.control('record'):
                # retain a copy of the shifted dislocation energies
                outstream_e = open('e_peierls.{}'.format(self.control('output')), 'w')

                for x, E in e_shifted:
                    outstream_e.write('{:.3f} {:.6f}\n'.format(x, E))

                outstream_e.close()

        if self.stress('calculate_stress'):
            self.taup, self.taup_av = pb.taup(
                                              self.par,
                                              self.control('max_x'),
                                              self.gsf,
                                              self.K,
                                              self.struc('burgers'),
                                              self.struc('spacing'),
                                              self.control('dimensions'),
                                              self.control('disl_type'),
                                              dtau=self.stress('dtau'),
                                              in_GPa=self.stress('use_GPa'),
                                              thr=self.stress('threshold')
                                             )

            # calculate average and direction-dependent Peierls barriers
            self.wp_av = pb.peierls_barrier(self.taup_av, self.struc('burgers'),
                                                  in_GPa=self.stress('use_GPa'))

        return

    def write_output(self):
        '''Write the results of the PN calculation to the specified output file
        (<control('output')>)
        '''

        outstream = open(self.control('output'), 'w')

        # write front matter
        boiler_plate = 'Peierls-Nabarro calculation with dislopy - a Python' + \
                       ' interface for atomistic modelling of dislocations\n'
        outstream.write(boiler_plate)
        outstream.write('{}\n\n'.format(self.control('title_line')))

        # write parameters used in the simulation
        outstream.write('SIMULATION PARAMETERS:\n')
        outstream.write('Dimensions: {}\n'.format(self.control('dimensions')))
        outstream.write('Dislocation type: {}\n'.format(self.control('disl_type')))
        outstream.write('Burgers vector: {:.3f} ang.\n'.format(self.struc('burgers')))
        outstream.write('Interlayer spacing: {:.3f} ang.\n'.format(self.struc('spacing')))
        outstream.write('Number of iterations: {}\n'.format(self.control('n_iter')))
        outstream.write('Number of atomic planes used: {}\n'.format(2*self.control('max_x')+1))

        # write stacking fault energy at midpoint of gamma line
        if self.control('dimensions') == 1:
            sfe = self.gsf(self.struc('burgers')/2.)
        else:
            if self.control('disl_type') == 'edge':
                sfe = self.gsf(self.struc('burgers')/2., 0.)
            else:
                sfe = self.gsf(0., self.struc('burgers')/2.)

        if not self.surf('do_fourier'):
            sfe = float(sfe)

        outstream.write('Stacking fault energy: {:.4f} eV/ang.^2\n'.format(sfe))

        # energy coefficients -> only write relevant coefficient if 1D
        if self.control('dimensions') == 2:
            outstream.write('Edge energy coefficient: {:.4f} eV/ang.^3\n'.format(self.K[0]))
            outstream.write('Screw energy coefficient: {:.4f} eV/ang.^3\n'.format(self.K[1]))
        else: # dimensions == 1
            outstream.write('Edge energy coefficient: {:.4f} eV/ang.^3\n'.format(self.K_store[0]))
            outstream.write('Screw energy coefficient: {:.4f} eV/ang.^3\n'.format(self.K_store[1]))

        outstream.write('\n\n')

        # write fit parameters
        outstream.write('Fit parameters: \n')

        # needed in case user set input <parameters> manually
        N = len(self.par)/3

        if self.control('dimensions') == 2:
            # write parameters for the edge component of displacement
            outstream.write('----X1----\n')

            outstream.write('A\n')
            for A in self.par[:N/2]:
                outstream.write('{:.3f} '.format(A))
            outstream.write('\n')

            outstream.write('x0\n')
            for x0 in self.par[N:N+N/2]:
                outstream.write('{:.3f} '.format(x0))
            outstream.write('\n')

            outstream.write('c\n')
            for c in self.par[2*N:2*N+N/2]:
                outstream.write('{:.3f} '.format(c))
            outstream.write('\n')

            # write parameters for screw component of displacement
            outstream.write('----X2----\n')

            outstream.write('A\n')
            for A in self.par[N/2:N]:
                outstream.write('{:.3f} '.format(A))
            outstream.write('\n')

            outstream.write('x0\n')
            for x0 in self.par[N+N/2:2*N]:
                outstream.write('{:.3f} '.format(x0))
            outstream.write('\n')

            outstream.write('c\n')
            for c in self.par[2*N+N/2:]:
                outstream.write('{:.3f} '.format(c))
            outstream.write('\n')

        elif self.control('dimensions') == 1:
            outstream.write('----X----\n')

            outstream.write('A\n')
            for A in self.par[:N]:
                outstream.write('{:.3f} '.format(A))
            outstream.write('\n')

            outstream.write('x0\n')
            for x0 in self.par[N:2*N]:
                outstream.write('{:.3f} '.format(x0))
            outstream.write('\n')

            outstream.write('c\n')
            for c in self.par[2*N:]:
                outstream.write('{:.3f} '.format(c))
            outstream.write('\n')

        outstream.write('\n\n')

        # write results
        outstream.write('Results:\n')
        outstream.write('E = {:.3f} eV\n'.format(self.E))

        # write Peierls stress related stuff
        if self.stress('use_GPa'):
            units = 'GPa'
        else:
            units = 'eV/ang.^3'

        if self.stress('calculate_stress'):
            outstream.write('Using applied stress method:\n')

            outstream.write('Left Peierls stress: {:.3f} {}\n'.format(
                                                   self.taup[0], units))
            outstream.write('Right Peierls stress: {:.3f} {}\n'.format(
                                                    self.taup[1], units))

            # average Peierls stress with APPROXIMATE uncertainty
            e_taup = abs(self.taup[0] - self.taup_av)
            outstream.write('Average Peierls stress: {:.3f} +/- {:.3f}{}\n'.format(
                                                     self.taup_av, e_taup, units))

            outstream.write('Peierls barrier: {:.3f} eV/ang\n\n'.format(self.wp_av))

        if self.stress('calculate_barrier'):
            outstream.write('Using dislocation translation method:\n')
            outstream.write('Peierls stress: {:.3f} {}\n'.format(self.taup_shift, units))
            outstream.write('Peierls barrier: {:.3f} eV/ang.\n\n'.format(self.wp_shift))

        if self.prop('max_rho'):
            outstream.write('Maximum density: {:.3f}\n'.format(self.max_density))
        if self.prop('width'):
            outstream.write('Dislocation width: {:.3f} ang.\n'.format(abs(self.dis_width)))
        if self.prop('center'):
            outstream.write('Dislocation centre (/spacing): {:.3f}\n'.format(
                                            self.com/self.struc('spacing')))
            outstream.write('Dislocation centre (ang.): {:.3f}\n'.format(self.com))

        outstream.write('\n\n**Finished**')
        outstream.close()
        return

    def visualise(self):
        '''Constructs an atomistic representation of the dislocation core.
        '''

        if not self.control('run_sim'):
            # read in dislocation parameters
            dims, self.par = vp.import_pn_pars(self.control('output'))

        if self.vis('program') == '':
            raise AttributeError("Atomistic simulation code not specified")
        else:
            basestruc = vp.read_unit_cell(self.vis('unitcell'),
                                          self.vis('program'),
                                          self.vis('shift'),
                                          permutation=self.vis('permutation'),
                                          path=self.vis('path_to_cell'))

        # construct elastic displacement field function
        field = aniso.makeAnisoField(self.elast('cij'), n=self.elast('b_edge'),
                                        m=self.elast('normal'))

        vp.make_xyz(basestruc, self.par, self.control('dimensions'), self.struc('burgers'),
                                   self.struc('spacing'), self.control('disl_type'), field,
                       self.vis('radius'), self.vis('xyz_name'), thr=self.vis('threshold'),
                      sym_thr=self.vis('sym_thresh'), description=self.control('title_line'),
                                                        thickness=self.vis('thickness'))

def main():
    '''Runs an Peierls-Nabarro simulation.
    '''

    parser = argparse.ArgumentParser()
    parser.add_argument('-i', type=str, nargs='?', dest='filename', default='0')

    args = parser.parse_args()
    if args.filename != 0:
        new_sim = PNSim(args.filename)
    else:
        # read in filename from the command line
        if sys.version_info.major == 2:
            filename = raw_input('Enter name of input file: ')
        elif sys.version_info.major == 3:
            filename = input('Enter name of input file: ')

        new_simulation = PNSim(filename)

if __name__ == "__main__":
    main()


import os.path
from django.conf import settings

from rest_framework.test import APITestCase

from store.models import Product

class ProductCreateTestCase(APITestCase):
    def test_create_product(self):
        initial_product_count = Product.objects.count()
        product_attrs = {
            'name': 'New Product',
            'description': 'Awesome product',
            'price': '123.45',
        }
        response = self.client.post('/api/v1/products/new', product_attrs)
        if response.status_code != 201:
            print(response.data)
        self.assertEqual(
            Product.objects.count(),
            initial_product_count + 1,
        )
        for attr, expected_value in product_attrs.items():
            self.assertEqual(response.data[attr], expected_value)
        self.assertEqual(response.data['is_on_sale'], False)
        self.assertEqual(
            response.data['current_price'],
            float(product_attrs['price']),
        )

class ProductDestroyTestCase(APITestCase):
    def test_delete_product(self):
        initial_product_count = Product.objects.count()
        product_id = Product.objects.first().id
        self.client.delete('/api/v1/products/{}/'.format(product_id))
        self.assertEqual(
            Product.objects.count(),
            initial_product_count - 1,
        )
        self.assertRaises(
            Product.DoesNotExist,
            Product.objects.get, id=product_id,
        )

class ProductListTestCase(APITestCase):
    def test_list_products(self):
        products_count = Product.objects.count()
        response = self.client.get('/api/v1/products/')
        self.assertIsNone(response.data['next'])
        self.assertIsNone(response.data['previous'])
        self.assertEqual(response.data['count'], products_count)
        self.assertEqual(len(response.data['results']), products_count)

class ProductUpdateTestCase(APITestCase):
    def test_update_product(self):
        product = Product.objects.first()
        response = self.client.patch(
            '/api/v1/products/{}/'.format(product.id),
            {
                'name': 'New Product',
                'description': 'Awesome product',
                'price': 123.45,
            },
            format='json',
        )
        updated = Product.objects.get(id=product.id)
        self.assertEqual(updated.name, 'New Product')

    def test_upload_product_photo(self):
        product = Product.objects.first()
        original_photo = product.photo
        photo_path = os.path.join(settings.MEDIA_ROOT, 'products', 'vitamin-iron.jpg')
        with open(photo_path, 'rb') as photo_data:
            response = self.client.patch('/api/v1/products/{}/'.format(product.id), {
                'photo': photo_data,
            }, format='multipart')
        self.assertEqual(response.status_code, 200)
        self.assertNotEqual(response.data['photo'], original_photo)
        try:
            updated = Product.objects.get(id=product.id)
            expected_photo = os.path.join(settings.MEDIA_ROOT, 'products', 'vitamin-iron')
            self.assertTrue(updated.photo.path.startswith(expected_photo))
        finally:
            os.remove(updated.photo.path)


## GAME OBJECTS ##

from bge import logic

from game3 import (base, keymap, attachment,
	weapon, vehicle, door, powerup)

class JetPack(attachment.CoreAttachment):

	NAME = "JetPack"
	SLOTS = ["Back"]
	SCALE = 0.74
	OFFSET = (0, 0.0, 0.0)
	ENABLE = True
	POWER = 16
	FUEL = 10000
	BURNRATE = 30
	CHARGERATE = 17

	def defaultData(self):
		self.power = self.createVector(3)
		dict = {"FUEL":self.FUEL}
		return dict

	def doEffects(self, state=None):
		if state in ["INIT", "STOP"]:
			self.randlist = [0]*3
			self.objects["Fire"].localScale = (1, 1, 0)
			return

		rand = 0

		for i in self.randlist:
			rand += i
		rand = (rand/3)

		self.objects["Fire"].localScale = (1, 1, rand)

		self.randlist.insert(0, (logic.getRandomFloat()*0.25)+self.power.length)
		self.randlist.pop()

	def ST_Startup(self):
		self.doEffects("INIT")

	def ST_Stop(self):
		self.doEffects("STOP")
		self.data["HUD"]["Stat"] = int( (self.data["FUEL"]/self.FUEL)*100 )
		self.data["HUD"]["Text"] = str(self.data["HUD"]["Stat"])

		if self.owning_player.jump_state == "JETPACK":
			self.owning_player.jump_state = "B_JUMP"

	## STATE IDLE ##
	def ST_Idle(self):
		plr = self.owning_player
		owner = self.objects["Root"]

		if self.data["ENABLE"] == False:
			self.ST_Stop()
			return

		self.power[0] = 0
		self.power[1] = 0
		self.power[2] = 0

		if plr.jump_state == "B_JUMP" and plr.objects["Root"] != None:
			plr.jump_state = "JETPACK"

		if plr.jump_state == "JETPACK":
			if self.data["FUEL"] > 0:
				self.power[2] = 1

				if plr.motion["Move"].length > 0.01:
					move = plr.motion["Move"].normalized()
					mref = plr.objects["VertRef"].getAxisVect((move[0], move[1], 0))
					self.power[0] = mref[0]
					self.power[1] = mref[1]

					self.power.normalize()

				plr.alignPlayer(factor=0.1)

				plr.objects["Root"].applyForce(self.power*self.POWER, False)
				self.data["FUEL"] -= self.BURNRATE
			else:
				plr.jump_state = "B_JUMP"

		elif plr.jump_state == "FLYING" and keymap.BINDS["PLR_JUMP"].active() == True:
			if self.data["FUEL"] > 0:
				self.power[2] = 1
				plr.objects["Root"].applyForce(self.power*self.POWER, True)
				self.data["FUEL"] -= self.BURNRATE

		else:
			self.randlist = [0]*3
			if self.data["FUEL"] < self.FUEL:
				self.data["FUEL"] += self.CHARGERATE
			else:
				self.data["FUEL"] = self.FUEL

		if self.data["FUEL"] < 0:
			self.data["FUEL"] = 0

		self.data["HUD"]["Stat"] = int( (self.data["FUEL"]/self.FUEL)*100 )
		self.data["HUD"]["Text"] = str(self.data["HUD"]["Stat"])

		self.doEffects()

class BasicStaff(weapon.CoreWeapon):

	NAME = "Glorified Stick"
	SLOTS = ["Shoulder_R", "Shoulder_L"]
	TYPE = "MELEE"
	OFFSET = (0.1, 0.0, 0.28)
	SCALE = 1.9

	def defaultData(self):
		self.ori_qt = [self.createMatrix().to_quaternion(), self.createMatrix(mirror="YZ").to_quaternion(), None]
		return {}

	def doPlayerAnim(self, frame=0):
		plr = self.owning_player
		anim = self.TYPE+plr.HAND[self.HAND]+self.owning_slot
		start = 0
		end = 20

		if frame == "LOOP":
			plr.doAnim(NAME=anim, FRAME=(end,end), LAYER=1, PRIORITY=2, MODE="LOOP")

		elif type(frame) is int:
			plr.doAnim(NAME=anim, FRAME=(start,end), LAYER=1)
			fac = (frame/self.WAIT)
			if frame < 0:
				fac = 1+fac
			plr.doAnim(LAYER=1, SET=end*fac)
			self.ori_qt[2] = self.ori_qt[1].slerp(self.ori_qt[0], fac)
			self.objects["Mesh"].localOrientation = self.ori_qt[2].to_matrix()

	def ST_Active(self):
		self.doPlayerAnim("LOOP")

class DemoStaff(BasicStaff):

	NAME = "Simple Pole Staff"
	SLOTS = ["Shoulder_R", "Shoulder_L"]
	TYPE = "MELEE"
	OFFSET = (0.1, 0.0, 0.28)
	SCALE = 1.9

class BasicSword(weapon.CoreWeapon):

	NAME = "Pirate Sword"
	NAME = "Sword"
	SLOTS = ["Hip_L", "Hip_R"]
	TYPE = "MELEE"
	OFFSET = (0, 0.2, 0.15)
	SCALE = 1

	def ST_Active(self):
		if self.data["COOLDOWN"] == 0:
			if keymap.BINDS["ATTACK_ONE"].tap() == True:
				hand = self.owning_player.HAND[self.HAND].split("_")[1]
				self.owning_player.doAnim(NAME="MeleeAttack"+hand, FRAME=(0,45), LAYER=1)
				self.data["COOLDOWN"] = 50
		else:
			self.data["COOLDOWN"] -= 1

		self.doPlayerAnim("LOOP")

class Lightsaber(BasicSword):

	NAME = "Lightsaber"
	OFFSET = (0,0,0)
	SCALE = 0.7
	WAIT = 15
	BLADETYPE = "GFX_LightSaber.Blade"
	BLADECOLOR = (1,1,1,1)
	BLADESIZE = 1

	def manageBlade(self):
		blade = self.objects["Blade"]

		if self.data["ENABLE"] == True:
			self.gfx_blade.visible = self.objects["Root"].visible
		else:
			self.gfx_blade.visible = False

		if self.active_state == self.ST_Active:
			blade.localScale[1] = 1
		elif self.active_state == self.ST_Draw:
			scale = abs(self.data["COOLDOWN"])/self.WAIT
			blade.localScale[1] = scale
		elif self.active_state == self.ST_Sheath:
			scale = abs(self.data["COOLDOWN"])/self.WAIT
			blade.localScale[1] = 1-scale
		else:
			blade.localScale[1] = 0

	def addBlade(self):
		blade = self.objects["Blade"]
		self.gfx_blade = blade.scene.addObject(self.BLADETYPE, blade, 0)
		self.gfx_blade.setParent(blade)
		self.gfx_blade.color = self.BLADECOLOR
		self.gfx_blade.visible = False
		self.gfx_blade["SIZE"] = self.BLADESIZE
		self.gfx_blade.localPosition = (0,0,0)
		self.gfx_blade.localOrientation = self.createMatrix()
		blade.localScale = (1,0,1)

	def ST_Startup(self):
		self.addBlade()
		self.active_post.append(self.manageBlade)

class LightsaberO(Lightsaber):

	NAME = "Knights' Lightsaber"
	BLADECOLOR = (0,0.3,1,1)

class LightsaberW(Lightsaber):

	NAME = "Masters' Lightsaber"
	BLADECOLOR = (0.4,0,1,1)

class LightsaberV(Lightsaber):

	NAME = "Ergonomic Lightsaber"
	BLADETYPE = "GFX_LightSaber.BladeToon"
	BLADECOLOR = (1,0,0,1)
	BLADESIZE = 0.8

class Buggy(vehicle.CoreCar):

	NAME = "All-Terrain Buggy"
	PLAYERACTION = "SeatLow"
	WHEELOBJECT = {"MESH":"Wheel.Buggy", "RADIUS":0.35}
	WHEELSETUP = {"FRONT":1.5, "REAR":-1.5, "WIDTH":1.1, "HEIGHT":0.2, "LENGTH":0.4}
	WHEELCONFIG = {"ROLL":0.1, "SPRING":40, "DAMPING":15, "FRICTION":7}
	DRIVECONFIG = {"POWER":20, "SPEED":0.5, "BRAKE":(0.5,0.2), "STEER":1.2, "DRIVE":2}
	CAMFIRST = {"POS":(0, -0.05, 0.6)}

	def ST_Startup(self):
		self.active_pre.append(self.doSuspensionRig)

	def createVehicle(self):
		self.vehicle_constraint = self.getConstraint()

		setup = self.WHEELSETUP

		FR = self.addWheel((setup["WIDTH"], setup["FRONT"], setup["HEIGHT"]))
		FL = self.addWheel((-setup["WIDTH"], setup["FRONT"], setup["HEIGHT"]))
		RR = self.addWheel((setup["WIDTH"], setup["REAR"], setup["HEIGHT"]))
		RL = self.addWheel((-setup["WIDTH"], setup["REAR"], setup["HEIGHT"]))

		self.objects["WheelObj"] = {"Wheel_FR":FR, "Wheel_FL":FL, "Wheel_RR":RR, "Wheel_RL":RL}

		self.doAnim(OBJECT=self.objects["Rig"], NAME="BuggyRigIdle", MODE="LOOP")

	def doSuspensionRig(self):
		for key in self.objects["WheelObj"]:
			self.objects[key].worldPosition = self.objects["WheelObj"][key].worldPosition.copy()
			self.objects["Rig"].channels[key].location = self.objects[key].localPosition.copy()

class Swing(door.CoreDoor):

	ANIM = {"OPEN":(0,60), "CLOSE":(60,0)}

class Slide(door.CoreDoor):

	ANIM = {"OPEN":(0,120), "CLOSE":(130,250)}

class Blast(door.CoreDoor):

	NAME = "Blast Door"
	TIME = 60
	ANIM = {"OPEN":(0,240), "CLOSE":(240,0)}

class SimpleKey(powerup.CoreKey):

	SCALE = (0.1, 0.2, 0.05)
	OFFSET = (0,0,0)
	LOCK = 1


import verilog as verilog

class PG:
    module_name = 'PG'

    def __init__(self):
        pass

    def inputs(self):
        return ['A_i', 'B_i']

    def outputs(self):
        return ['G_i', 'P_i']

    def verilog(self, file_path, file_name):
        m = verilog.Module(PG.module_name)

        # Inputs
        for input in self.inputs():
            m.input(input, 'input')
        # Outputs
        for output in self.outputs():
            m.output(output, 'output')

        # Start
        m.start()

        # Assignments
        m.stmt_assign('G_i', 'A_i & B_i')
        m.stmt_assign('P_i', 'A_i ^ B_i')

        # End
        m.end()

        # Write File
        m.write(file_path, file_name)

    def instantiation(self, instance_name, inputs, outputs):
        """
            inputs: [dict{ port: ? , connector: ?}]
            outputs: [dict{ port: ? , connector: ?}]
        """
        return verilog.Module.instantiate(module_name=PG.module_name,
                                          instance_name=instance_name,
                                          inputs=inputs,
                                          outputs=outputs)


import re, unicodedata, nltk, json, ssl
import pandas as uwu
import numpy as np
from nltk.corpus import stopwords
from flask import Flask, request, jsonify
from tensorflow.keras.models import model_from_yaml
import tensorflow as tf
from flask import jsonify

app = Flask(__name__)
try:
    _create_unverified_https_context = ssl._create_unverified_context
except AttributeError:
    pass
else:
    ssl._create_default_https_context = _create_unverified_https_context

print('Instalando librerias de NLTK')
nltk.download('punkt')
nltk.download('stopwords')

def remove_stop_words(list_tokens):
    stop = stopwords.words('spanish')
    for token in list_tokens:
        if token.lower() in stop:
            list_tokens.pop(list_tokens.index(token))
    return list_tokens

def remove_void_elements(tokens):
    for token in tokens:
        if token == '' or token == " " or token == "  ":
            tokens.pop(tokens.index(token))
    return tokens

def normalize_text(tokens):
    for token in tokens:
        pos = tokens.index(token)
        if token.isdigit() or token.isalpha() == False:
            tokens.pop(pos)
        else:
            token = re.sub(r'[^\w\s]', ' ', token)
            if token != ' ' or token != '':
                tokens[pos] = unicodedata.normalize('NFKD', token.lower()).encode('ascii', 'ignore').decode('utf-8','ignore')
            else:
                tokens.pop(pos)
    return tokens

def create_lemma_dict():
    lemmaDiccionario = {}
    with open('/app/Herramientas/lemma.txt', 'rb') as fichero:
        datos = (fichero.read().decode('utf8').replace(u'\r', u'').split(u'\n'))
        datos = ([avance.split(u'\t') for avance in datos])
    for avance in datos:
        if len(avance) > 1:
            lemmaDiccionario[avance[1]] = avance[0]
    return lemmaDiccionario

def lemmatize(lemmaDiccionario, word):
    return lemmaDiccionario.get(word, word + u'')

def lemmatize_words(words):
    lemmaDiccionario = create_lemma_dict()
    new_words = []
    for palabra in words:
        new_word = lemmatize(lemmaDiccionario, palabra)
        new_words.append(new_word)
    return new_words

def wordsToNumbers(tokens, vocabulary):
    number_array = np.zeros(10, dtype=int)
    for i in range(len(tokens)):
        if i < 10 and tokens[i] in vocabulary:
            number_array[i] = vocabulary.index(i) + 1
    return number_array

def processText(Text):
    list_text = nltk.word_tokenize(Text)
    list_text = remove_stop_words(list_text)
    list_text = remove_void_elements(list_text)
    list_text = normalize_text(list_text)
    list_text = lemmatize_words(list_text)
    if len(list_text) > 10:
        return list_text[:10]
    else:
        return list_text

def get_dimension_of_text(text, area):
    if not isinstance(text, str) or len(text) < 5:
        return 'Texto a clasificar mal ingresado o muy corto (minimo 5 palabras)', 200
    if area not in ['Gobierno Corporativo', 'Medio Ambiente', 'Social Externo', 'Social Interno']:
        return 'El area ingresada no coincide, puede ser Gobierno Corporativo, Medio Ambiente, Social Externo o Social Interno', 200
    list_words = processText(text)
    vocab = list(uwu.read_csv('./Herramientas/vocabulary.csv'))
    list_words_numeric = wordsToNumbers(list_words, vocab)
    if area == 'Gobierno Corporativo':
        df = uwu.read_csv('./Herramientas/dims_gob.csv', sep=";")
        RNNmodel = tf.keras.models.load_model('model_gob.h5')
    elif area == 'Medio Ambiente':
        df = uwu.read_csv('./Herramientas/dims_amb.csv', sep=";")
        RNNmodel = tf.keras.models.load_model('./model_amb.h5')
    elif area == 'Social Externo':
        df = uwu.read_csv('./Herramientas/dims_soce.csv', sep=";")
        RNNmodel = tf.keras.models.load_model('./model_soce.h5')
    elif area == 'Social Interno':
        df = uwu.read_csv('./Herramientas/dims_soci.csv', sep=";")
        RNNmodel = tf.keras.models.load_model('./model_soci.h5')

    print("\n\n\nello, como estas")

    dim = RNNmodel.predict_classes(np.array([list_words_numeric]))[0]
    print("\n\n\nello, como estas")

    dim_str = df.Dimension[dim]
    return dim, dim_str, list_words

@app.route('/classify/v1', methods=["POST"])
def classify_text():
    body = request.get_json()
    if 'area' in body:
        area = body['area']
    else:
        return 'Area no encontrada', 200
    if 'text' in body:
        text = body['text']
    else:
        return 'Texto no encontrado', 200
    dimension, dimension_string, palabras = get_dimension_of_text(text, area)
    response = {
            'dim_cod': int(dimension),
            'dim_str': dimension_string,
            'palabras': palabras
            }
    return response, 200

@app.route('/')
def presentation():
    return '''
    Desarrollo de Proyecto de tesis de Ricardo Alvarez Zambrano

    Consiste en un clasificador de texto de dimensiones ESG para ESG Compass

    Para clasificar texto se debe hacer una request "POST" a la ruta < /classify/v1 > y en el body incluir un json los siguientes campos

    {

        'area':'Gobierno Corporativo',

        'text':'El texto que se desea clasificar'

    }

    Las areas posibles que acepta el modelo son:

    - Gobierno Corporativo

    - Social Externo

    - Social Interno

    - Medio Ambiente

    '''

if __name__ == "__main__":
    app.run(host="0.0.0.0", debug=False, port=80)


# importing all the required modules
import PyPDF2

# creating an object
file = open('testing-pypdf/pdf.pdf', 'rb')

# creating a pdf reader object
pdf = PyPDF2.PdfFileReader(file)

# print the number of pages in pdf file
print(pdf.numPages)


def maior_num(numeros):
    maior = numeros[0]

    for i in numeros:
        if i > maior:
            maior = i

    return maior

def numeros_usuario():
    numero = []
    sim = "sim"

    while sim == "sim":
        numero.append(float(input("Digite um número: ")))
        sim = input("Deseja continuar colocando mais números (sim/nao): ")

        if sim == "nao":
            break

    return numero

numeros = numeros_usuario()
maior = maior_num(numeros)

print("O maior numero é", maior)


""" Module responsible for predicting the image modalities for images in db.
The updates can be performed on non-predicted figures (when importing
content from the pipeline), or to all the figures on a schema (when classifiers
are updated).
"""

from os import cpu_count
from typing import Dict, List, Tuple, Literal, Optional
from pathlib import Path
import logging
from pandas import DataFrame
from torch import cuda
from psycopg import sql, Cursor, connect  # https://github.com/PyCQA/pylint/issues/5273

from image_modalities_classifier.models.predict import ModalityPredictor, RunConfig
from biosearch_core.data.figure import SubFigureStatus, FigureType
from biosearch_core.db.model import ConnectionParams

class PredictManager:
    """Manage the predictions on subfigures"""

    def __init__(
        self, base_img_path: str, conn_params: ConnectionParams, classifiers: Dict
    ):
        self.params = conn_params
        self.schema = conn_params.schema

        device = "cuda:0" if cuda.is_available() else "cpu"
        self.run_config = RunConfig(
            batch_size=128, num_workers=cpu_count(), device=device
        )
        self.classifiers = classifiers
        self.base_img_path = Path(base_img_path)

    def fetch_subfigures_from_db(
        self,
        cursor: Cursor,
        status: Optional[
            Literal[
                SubFigureStatus.NOT_PREDICTED,
                SubFigureStatus.PREDICTED,
                SubFigureStatus.GROUND_TRUTH,
            ]
        ] = None,
    ) -> List[Tuple]:
        """Fetch not predicted subfigures from db"""
        valid_status = set(item.value for item in SubFigureStatus)
        if status is not None and status not in valid_status:
            # pylint: disable=C0209:consider-using-f-string
            raise ValueError("Figure status %s is invalid" % (status))

        query = f"""
          SELECT id, uri FROM {self.schema}.figures
          WHERE fig_type={FigureType.SUBFIGURE.value}
        """
        if status is not None:
            query += f" AND status={status}"
        cursor.execute(query)
        return cursor.fetchall()

    def _fetch_ids_with_ground_truth(
        self, cursor: Cursor, data: DataFrame
    ) -> List[int]:
        all_ids = ",".join([str(el) for el in data.id.values.tolist()])
        query_valid = f"""
            SELECT id FROM {self.schema}.figures
            WHERE id IN ({all_ids}) AND
                  status = '{SubFigureStatus.GROUND_TRUTH.value}'
        """
        results = cursor.execute(query_valid).fetchall()
        return [el[0] for el in results]

    def _flatten_and_update(self, cursor: Cursor, data: DataFrame, update_query: str):
        """Send the update queries as repeated statements because psycopg3 does
        not have the extra_values functions available in psycopg2."""
        tuples = data[["prediction", "id"]]
        tuples = list(tuples.itertuples(index=False, name=None))
        flattened_values = [item for sublist in tuples for item in sublist]

        all_queries = update_query * len(tuples)
        query = sql.SQL(all_queries.format(*flattened_values))
        cursor.execute(query)

    def _update_db(self, cursor, data: DataFrame) -> None:
        """Update subfigures during prediction phase. If figures in the data
        already contain ground truth data, do not update its status.
        """
        # only update status for figures without ground-truth
        ids_with_gt = self._fetch_ids_with_ground_truth(cursor, data)
        df_with_gt = data.loc[data.id.isin(ids_with_gt)]
        df_without_gt = data.loc[~data.id.isin(ids_with_gt)]

        if len(df_with_gt) > 0:
            update_query1 = f"UPDATE {self.schema}.figures SET "
            update_query1 += "label=('{}') WHERE id=({}); "
            self._flatten_and_update(cursor, df_with_gt, update_query1)

        if len(df_without_gt) > 0:
            update_query2 = f"UPDATE {self.schema}.figures SET status={SubFigureStatus.PREDICTED.value},"
            update_query2 += " label=('{}') WHERE id=({}); "
            self._flatten_and_update(cursor, df_without_gt, update_query2)

    def predict_and_update_db(
        self,
        status: Optional[
            Literal[
                SubFigureStatus.NOT_PREDICTED,
                SubFigureStatus.PREDICTED,
                SubFigureStatus.GROUND_TRUTH,
            ]
        ] = None,
    ):
        """Predict label and set status to predicted"""
        # pylint: disable=not-context-manager
        with connect(conninfo=self.params.conninfo(), autocommit=False) as conn:
            output_df = None
            with conn.cursor() as cursor:
                try:
                    # tuples id, img_path
                    rows = self.fetch_subfigures_from_db(cursor, status=status)
                    if len(rows) == 0:
                        return None

                    rel_img_paths = [elem[1] for elem in rows]
                    predictor = ModalityPredictor(self.classifiers, self.run_config)
                    # predict does not shuffle
                    output_df = predictor.predict(rel_img_paths, self.base_img_path)
                    output_df["id"] = [elem[0] for elem in rows]
                    self._update_db(cursor, output_df)
                    conn.commit()
                # pylint: disable=bare-except
                except:
                    logging.error("PREDICTION", exc_info=True)
        return output_df


import os
import random
import time
import torch
import torch.backends.cudnn as cudnn
import models
from utils.logger import Logger
import yaml
import argparse
from utils import utils
import sys

def add_learner_params():
    parser = argparse.ArgumentParser(description='ML')
    parser.add_argument('--problem', default='finetune',
                        help='The problem to train',
                        choices=models.REGISTERED_MODELS,
                        )
    parser.add_argument('--name', default='demo',
                        help='Name for the experiment')
    parser.add_argument('--nodes', default='',
                        help='slurm nodes for the experiment')
    parser.add_argument('--slurm_partition', default='',
                        help='slurm partitions for the experiment')
    # optimizer params
    parser.add_argument('--lr_schedule', default='warmup-const')
    parser.add_argument('--loss', default='ce')
    parser.add_argument('--opt', default='sgd',
                        help='Optimizer to use', choices=['sgd', 'adam', 'lars'])
    parser.add_argument('--iters', default=-1, type=int,
                        help='The number of optimizer updates')
    parser.add_argument('--warmup', default=0, type=float,
                        help='The number of warmup iterations in proportion to \'iters\'')
    parser.add_argument('--lr', default=0.1, type=float,
                        help='Base learning rate')
    parser.add_argument('--inner_lr', default=0.1, type=float,
                        help='inner learning rate')
    parser.add_argument('--q', default=0.66, type=float,
                        help='generalized ce loss q value')
    parser.add_argument('--wd', '--weight_decay',
                        default=1e-4, type=float, dest='weight_decay')
    # noise params
    parser.add_argument('--corruption_prob', type=float, default=0.9,
                        help='label noise')
    parser.add_argument('--corruption_type', '-ctype', type=str, default='unif',
                        help='Type of corruption ("unif" or "flip" or "flip2" or "asym").')

    # trainer params
    parser.add_argument('--save_freq', default=1000000000000,
                        type=int, help='Frequency to save the model')
    parser.add_argument('--log_freq', default=100,
                        type=int, help='Logging frequency')
    parser.add_argument('--eval_freq', default=100000000000,
                        type=int, help='Evaluation frequency')
    parser.add_argument('-j', '--workers', default=4, type=int,
                        help='The number of data loader workers')
    parser.add_argument('--seed', default=222, type=int, help='Random seed')
    # parallelizm params:
    parser.add_argument('--test_bs', default=256, type=int)
    parser.add_argument('--encoder_ckpt', default='',
                        help='Path to the encoder checkpoint')
    parser.add_argument('--encoder_type', default='simclr',
                        help='pretrained "imagenet" or "simclr" for simclr')
    parser.add_argument('--config', default='configs/cifar10.yaml', type=str,
                        help='the number of nodes (scripts launched)',
                        )
    #
    parser.add_argument('--cuda', action='store_true')
    parser.add_argument('--neptune', action='store_true')

    params = parser.parse_args()
    d = vars(params)
    if params.config:
        with open(params.config, 'r') as stream:
            try:
                params_2 = yaml.safe_load(stream)
            except yaml.YAMLError as exc:
                print(exc)
    for k, v in params_2.items():
        d[k] = v
    return params

def main():
    args = add_learner_params()
    if args.seed != -1:
        random.seed(args.seed)
        torch.manual_seed(args.seed)
        cudnn.deterministic = True
    args.root = 'logs/'+args.name+'/'

    if args.neptune:
        import neptune
        project = "arighosh/pretrain_noisy_label"
        neptune.init(project_qualified_name=project,
                     api_token=os.environ["NEPTUNE_API_TOKEN"])
        neptune.create_experiment(
            name=args.name, send_hardware_metrics=False, params=vars(args))
    fmt = {
        'train_time': '.3f',
        'val_time': '.3f',
        'train_epoch': '.1f',
        'lr': '.1e',
    }
    logger = Logger('logs', base=args.root, fmt=fmt)
    if torch.cuda.is_available():
        device = torch.device('cuda')
    else:
        device = torch.device('cpu')
    if args.cuda:
        assert device.type == 'cuda', 'no gpu found!'

    with open(args.root+'config.yml', 'w') as outfile:
        yaml.dump(vars(args), outfile, default_flow_style=False)

    # create model
    model = models.REGISTERED_MODELS[args.problem](args, device=device)
    cur_iter = 0
    # Data loading code
    model.prepare_data()

    continue_training = cur_iter < args.iters
    data_time, it_time = 0, 0
    best_acc = 0.
    best_valid_acc, best_acc_with_valid = 0, 0

    while continue_training:
        train_loader, test_loader, valid_loader, meta_loader = model.dataloaders(
            iters=args.iters)
        train_logs = []
        model.train()
        start_time = time.time()
        for _, batch in enumerate(train_loader):
            cur_iter += 1
            batch = [x.to(device) for x in batch]
            data_time += time.time() - start_time
            logs = {}
            if args.problem not in {'finetune'}:
                meta_batch = next(iter(meta_loader))
                meta_batch = [x.to(device) for x in meta_batch]
                logs = model.train_step(batch, meta_batch, cur_iter)
            else:
                logs = model.train_step(batch, cur_iter)

            # save logs for the batch
            train_logs.append({k: utils.tonp(v) for k, v in logs.items()})
            if cur_iter % args.eval_freq == 0 or cur_iter >= args.iters:
                test_start_time = time.time()
                test_logs, valid_logs = [], []
                model.eval()
                with torch.no_grad():
                    for batch in test_loader:
                        batch = [x.to(device) for x in batch]
                        logs = model.test_step(batch)
                        test_logs.append(logs)
                    for batch in valid_loader:
                        batch = [x.to(device) for x in batch]
                        logs = model.test_step(batch)
                        valid_logs.append(logs)
                model.train()
                test_logs = utils.agg_all_metrics(test_logs)
                valid_logs = utils.agg_all_metrics(valid_logs)
                best_acc = max(best_acc, float(test_logs['acc']))
                test_logs['best_acc'] = best_acc
                if float(valid_logs['acc']) > best_valid_acc:
                    best_valid_acc = float(valid_logs['acc'])
                    best_acc_with_valid = float(test_logs['acc'])
                test_logs['best_acc_with_valid'] = best_acc_with_valid
                #

                if args.neptune:
                    for k, v in test_logs.items():
                        neptune.log_metric('test_'+k, float(v))
                    for k, v in valid_logs.items():
                        neptune.log_metric('valid_'+k, float(v))
                    test_it_time = time.time()-test_start_time
                    neptune.log_metric('test_it_time', test_it_time)
                    neptune.log_metric('test_cur_iter', cur_iter)
                logger.add_logs(cur_iter, test_logs, pref='test_')
            it_time += time.time() - start_time
            if (cur_iter % args.log_freq == 0 or cur_iter >= args.iters):
                train_logs = utils.agg_all_metrics(train_logs)
                if args.neptune:
                    for k, v in train_logs.items():
                        neptune.log_metric('train_'+k, float(v))
                    neptune.log_metric('train_it_time', it_time)
                    neptune.log_metric('train_data_time', data_time)
                    neptune.log_metric(
                        'train_lr', model.optimizer.param_groups[0]['lr'])
                    neptune.log_metric('train_cur_iter', cur_iter)
                logger.add_logs(cur_iter, train_logs, pref='train_')
                logger.add_scalar(
                    cur_iter, 'lr', model.optimizer.param_groups[0]['lr'])
                logger.add_scalar(cur_iter, 'data_time', data_time)
                logger.add_scalar(cur_iter, 'it_time', it_time)
                logger.iter_info()
                logger.save()
                data_time, it_time = 0, 0
                train_logs = []
            if cur_iter >= args.iters:
                continue_training = False
                break
            start_time = time.time()

if __name__ == '__main__':
    main()


import os
import glob
import numpy as np
import sys
import re
from io import StringIO
import matplotlib.pyplot as plt
import math
import pickle

from outliers import smirnov_grubbs as grubbs

def Zscan_data_extract_singlechip(runID,result_path,save_path):
    chipdir=os.path.join(result_path,runID)
    print(chipdir)
    nameFolds=glob.glob(os.path.join(chipdir,'X*'))
    tileNum=len(nameFolds)
    print(len(nameFolds))
    if tileNum==0:
        print('No data in %s'%runID)
        sys.exit()
    if not os.path.exists(os.path.join(save_path,runID)):
        os.makedirs(os.path.join(save_path,runID))
    save_path1=os.path.join(save_path,runID)

    shift_info_wav=np.zeros((tileNum,3))
    shift_info_sml=np.zeros((tileNum,3))
    X=np.zeros((tileNum,1))
    Y=np.zeros((tileNum,1))
    Z=np.zeros((tileNum,1))
    Z_WDI=np.zeros((tileNum,1))
    Fv_max=np.zeros((tileNum,1))
    plt.figure(figsize=(35,20))
    for n in range(tileNum):
        fileName=nameFolds[n].split('\\')[-1]
        A=re.split(r'[XY,_]',fileName)
        X[n]=A[1]
        Y[n]=A[3]
        infoFile=os.path.join(nameFolds[n],'%s.txt'%fileName)
        with open(infoFile) as file:
            content=file.readlines()
            content=content[4:len(content)-2]
            C= np.loadtxt(content)
        if n<10:
            '''draw plot'''
            plt.subplot(5,4,2*(n+1)-1)
            plt.plot(C[:,0],C[:,1])
            plt.plot(C[:,0],C[:,3])
            max_value=np.max(C[:,3])
            max_pos=np.argmax(C[:,3],axis=0)
            plt.plot([C[:,0][max_pos],C[:,0][max_pos]],[0,max_value])
            Z[n]=C[:,0][max_pos]
            Fv_max[n]=max_value
            fv_shift1,fv_shift2,WDI_shift=F_getFvShift(C[:,1],C[:,3])
            plt.title('Fv_WAV_tile%d,shift:%.2f,%.2f,WDI_shift:%.2f'%(n,fv_shift1,fv_shift2,WDI_shift))
            shift_info_wav[n,:]=[fv_shift1,fv_shift2,WDI_shift]

            plt.subplot(5,4,2*(n+1))
            plt.plot(C[:,0],C[:,2])
            plt.plot(C[:,0],C[:,4])
            max_value=np.max(C[:,4])
            max_pos=np.argmax(C[:,4],axis=0)
            plt.plot([C[:,0][max_pos],C[:,0][max_pos]],[0,max_value])
            fv_shift1,fv_shift2,WDI_shift=F_getFvShift(C[:,2],C[:,4])
            plt.title('Fv_SML_tile%d,shift:%.2f,%.2f,WDI_shift:%.2f'%(n,fv_shift1,fv_shift2,WDI_shift))
            shift_info_sml[n,:]=[fv_shift1,fv_shift2,WDI_shift]
        max_value=np.max(C[:,3])
        max_pos=np.argmax(C[:,4],axis=0)
        Z[n]=C[:,0][max_pos]
        Z_WDI[n]=C[:,0][math.floor((len(C[:,0])+1)/2)]
        Fv_max[n]=max_value
        fv_shift1,fv_shift2,WDI_shift=F_getFvShift(C[:,1],C[:,3])
        shift_info_wav[n,:]=[fv_shift1,fv_shift2,WDI_shift]
        fv_shift1,fv_shift2,WDI_shift=F_getFvShift(C[:,2],C[:,4])
        shift_info_sml[n,:]=[fv_shift1,fv_shift2,WDI_shift]
    plt.tight_layout()
    plt.subplots_adjust(left=None, bottom=None, right=None, top=None,wspace=None, hspace=0.15)
    plt.savefig(os.path.join(save_path1,'fv_curve_'+runID+'.png'),dpi=500,bbox_inches = 'tight')
    plt.close()
    #plt.show()

    '''shift info figure'''
    range_shift1 = np.ptp(shift_info_wav[:, 0])
    range_shift2 = np.ptp(shift_info_wav[:, 1])
    range_wdishift = np.ptp(shift_info_wav[:, 2])

    shift1 = shift_info_wav[:, 0]
    shift2 = shift_info_wav[:, 1]
    wdi_shift = shift_info_wav[:, 2]
    shift1 = grubbs.test(shift1, alpha=.05)
    shift2 = grubbs.test(shift2, alpha=.05)
    wdi_shift = grubbs.test(wdi_shift, alpha=.05)

    std_shift1 = np.std(shift1, ddof=1)
    std_shift2 = np.std(shift2, ddof=1)
    std_WDI = np.std(wdi_shift, ddof=1)
    mean_shift1 = np.mean(shift1)
    mean_shift2 = np.mean(shift2)
    mean_WDI = np.mean(wdi_shift)
    plt.figure(figsize=(20,10))
    plt.subplot(3,2,1)
    line1,=plt.plot(shift_info_wav[:,0],marker='o',markerfacecolor='none',markersize = '5',linestyle='-',linewidth='1')
    plt.legend(handles=[line1], labels=['shift1,range %s std %s mean %s'%(range_shift1,std_shift1,mean_shift1)], loc='best',fontsize=8)
    plt.title('WAV offset summary')

    plt.subplot(3,2,3)
    line2,=plt.plot(shift_info_wav[:,1],marker='*',markerfacecolor='none',markersize = '5',linestyle='-',linewidth='1')
    plt.legend(handles=[line2], labels=['shift2,range %s std %s mean %s'%(range_shift2,std_shift2,mean_shift2)], loc='best',fontsize=8)

    plt.subplot(3,2,5)
    line3,=plt.plot(shift_info_wav[:,2],marker='^',markerfacecolor='none',markersize = '5',linestyle='-',linewidth='1')
    plt.plot([0,tileNum],[mean_WDI,mean_WDI],'r',linewidth='0.5')
    plt.legend(handles=[line3], labels=['WDI_s,range %s std %s mean %s'%(range_wdishift,std_WDI,mean_WDI)], loc='best',fontsize=8)

    range_shift1 = np.ptp(shift_info_sml[:, 0])
    range_shift2 = np.ptp(shift_info_sml[:, 1])
    range_wdishift = np.ptp(shift_info_sml[:, 2])
    shift1 = shift_info_sml[:, 0]
    shift2 = shift_info_sml[:, 1]
    wdi_shift = shift_info_sml[:, 2]
    shift1 = grubbs.test(shift1, alpha=.05)
    shift2 = grubbs.test(shift2, alpha=.05)
    wdi_shift = grubbs.test(wdi_shift, alpha=.05)

    std_shift1 = np.std(shift1, ddof=1)
    std_shift2 = np.std(shift2, ddof=1)
    std_WDI = np.std(wdi_shift, ddof=1)
    mean_shift1 = np.mean(shift1)
    mean_shift2 = np.mean(shift2)
    mean_WDI = np.mean(wdi_shift)
    plt.subplot(3,2,2)
    line4,=plt.plot(shift_info_sml[:,0],marker='o',markerfacecolor='none',markersize = '5',linestyle='-',linewidth='1')
    plt.legend(handles=[line4], labels=['shift1,range %s std %s mean %s'%(range_shift1,std_shift1,mean_shift1)], loc='best',fontsize=8)
    plt.title('SML offset summary')

    plt.subplot(3,2,4)
    line5,=plt.plot(shift_info_sml[:,1],marker='*',markerfacecolor='none',markersize = '5',linestyle='-',linewidth='1')
    plt.legend(handles=[line5], labels=['shift2,range %s std %s mean %s'%(range_shift2,std_shift2,mean_shift2)], loc='best',fontsize=8)

    plt.subplot(3,2,6)
    line6,=plt.plot(shift_info_sml[:,2],marker='^',markerfacecolor='none',markersize = '5',linestyle='-',linewidth='1')
    plt.plot([0,tileNum],[mean_WDI,mean_WDI],'r',linewidth='0.5')
    plt.legend(handles=[line6], labels=['WDI_s,range %s std %s mean %s'%(range_wdishift,std_WDI,mean_WDI)], loc='best',fontsize=8)

    plt.tight_layout()
    plt.subplots_adjust(left=None, bottom=None, right=None, top=None,wspace=None, hspace=0.15)
    plt.savefig(os.path.join(save_path1,'offset_summary_'+runID+'.png'),dpi=500,bbox_inches = 'tight')
    plt.close()
    #plt.show()

    '''shift spatial figure'''
    plt.figure(figsize=(40,10))
    ax1 = plt.subplot(1,5,1)
    '''关闭裁剪功能'''
    plt.scatter(X,Y,s=300,c=Z,clip_on = False)
    plt.gca().set_aspect(1)
    ax1.spines['top'].set_visible(False)
    ax1.spines['right'].set_visible(False)
    ax1.spines['bottom'].set_visible(False)
    ax1.spines['left'].set_visible(False)
    plt.colorbar()
    plt.title('focus_Z',fontsize=20)

    ax2 = plt.subplot(1,5,2)
    plt.scatter(X,Y,s=300,c=Z_WDI,clip_on = False)
    #XY轴单位长度相同
    plt.gca().set_aspect(1)
    ax2.spines['top'].set_visible(False)
    ax2.spines['right'].set_visible(False)
    ax2.spines['bottom'].set_visible(False)
    ax2.spines['left'].set_visible(False)
    plt.colorbar()
    plt.title('WDI_Z',fontsize=20)

    ax3 = plt.subplot(1,5,3)
    c=np.squeeze(shift_info_sml[:,2])
    #C的值不能是列向量并且和XY保持一致
    plt.scatter(X,Y,s=300,c=shift_info_sml[:,2].reshape(len(X),1),clip_on = False)
    #XY轴单位长度相同
    plt.gca().set_aspect(1)
    ax3.spines['top'].set_visible(False)
    ax3.spines['right'].set_visible(False)
    ax3.spines['bottom'].set_visible(False)
    ax3.spines['left'].set_visible(False)
    plt.colorbar()
    plt.title('WDI_offset',fontsize=20)

    ax4 = plt.subplot(1,5,4)
    plt.scatter(X,Y,s=300,c=shift_info_sml[:,0].reshape(len(X),1),clip_on = False)
    #XY轴单位长度相同
    plt.gca().set_aspect(1)
    ax4.spines['top'].set_visible(False)
    ax4.spines['right'].set_visible(False)
    ax4.spines['bottom'].set_visible(False)
    ax4.spines['left'].set_visible(False)
    plt.colorbar()
    plt.title('shift1',fontsize=20)

    ax5 = plt.subplot(1,5,5)
    plt.scatter(X,Y,s=300,c=Fv_max,clip_on = False,vmin=0.7,vmax=1.2)
    #XY轴单位长度相同
    plt.gca().set_aspect(1)
    ax5.spines['top'].set_visible(False)
    ax5.spines['right'].set_visible(False)
    ax5.spines['bottom'].set_visible(False)
    ax5.spines['left'].set_visible(False)
    plt.colorbar()
    plt.title('Fv_max',fontsize=20)

    plt.suptitle(runID.replace("_","-"))
    plt.tight_layout()
    plt.subplots_adjust(top=0.85)
    plt.savefig(os.path.join(save_path1,'offset_spatialinfo_'+runID+'.png'),dpi=500,bbox_inches = 'tight')
    plt.close()
    #plt.show()

    '''保存变量'''
    with open(os.path.join(save_path1,'Fv_max.txt'),'wb') as A1:
        pickle.dump(Fv_max,A1)
    with open(os.path.join(save_path1,'C.txt'),'wb') as A1:
        pickle.dump(C,A1)
    with open(os.path.join(save_path1,'shift_info_sml.txt'),'wb') as A1:
        pickle.dump(shift_info_sml,A1)
    with open(os.path.join(save_path1,'shift_info_wav.txt'),'wb') as A1:
        pickle.dump(shift_info_wav,A1)

def F_getFvShift(C_BF,C_FF):
    step=0.2
    indexLength=len(C_BF)
    FF_max_value=np.max(C_FF)
    FF_max_pos=np.argmax(C_FF)
    if FF_max_pos>0:
        BF_1=C_BF[0:FF_max_pos]
    else:
        BF_1 = C_BF[FF_max_pos]
    BF_2=C_BF[FF_max_pos:indexLength]
    BF_max_value1=np.max(BF_1)
    BF_max_pos1=np.argmax(BF_1)
    BF_max_value2=np.max(BF_2)
    BF_max_pos2=np.argmax(BF_2)
    '''python不用加1'''
    BF_max_pos2=BF_max_pos2+FF_max_pos
    fv_shift1=(FF_max_pos-BF_max_pos1)*step
    fv_shift2=(BF_max_pos2-FF_max_pos)*step
    '''这里需要加1'''
    WDI_shift=((indexLength+1)/2-(FF_max_pos+1))*step
    return fv_shift1,fv_shift2,WDI_shift

# runID = '123'
# result_path =r'D:\bost_ui\output\z_get_imge'
# save_path = r'D:\bost_ui\output\z_get_imge'
#
# Zscan_data_extract_singlechip(runID, result_path, save_path)


class Solution:
    def largeGroupPositions(self, s: str) -> List[List[int]]:
        answer = []
        i = 0 # 그룹의 시작
        for j in range(len(s)):
            if j == len(s) -1  or s[j] != s[j+1]:
                if j-i+1 >= 3:
                    answer.append([i,j])
                i = j+1
        return answer


from flask import Flask
from flask_cors import CORS
import torch
import numpy as np
import requests
import numpy as np
import requests
from data_loader import NERDataset
from torch.utils.data import DataLoader
from torchcrf import CRF

app = Flask(__name__)  # 如果是单独应用可以使用__name__，如果是module则用模块名
# Flask还有其他参数https://blog.csdn.net/YZL40514131/article/details/122730037
CORS(app, supports_credentials=True)  # 解决跨域

# 序列字典
tag2id = {
    "O": 0,
    "B-NAME": 1,
    "I-NAME": 2,
    "B-NOTIONAL": 4,
    "I-NOTIONAL": 5,
    "B-TICKER": 6,
    "I-TICKER": 7,
    '[CLS]': 10,
    '[SEP]': 11
}

id2tag = {_idx: _tag for _tag, _idx in list(tag2id.items())}

@app.route('/123')
def hello():
    return "hello world"

@app.route('/nlp/<string>')
def ner_function(string):

    device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
    #print (device)
    #device = 'cpu'
    model = torch.load('save_model.pth')
    #print(model)
    model.to(device)
    word2ix = np.load('vocab.npy', allow_pickle='TRUE').item() # 加载词典
    pred_tags = [] #

    split_word_list = string.split()
    low_word_list = [_t.lower() for _t in split_word_list] # 单词列表

    # sents = preprocess(word_list, word2ix) # 转化得来的向量
    label_list = []
    for i in range(0,len(low_word_list)):
        label_list.append('O')
    low_word_list = [low_word_list]
    label_list = [label_list]
    dataset = NERDataset(low_word_list, label_list, word2ix, tag2id, len(word2ix)-1)

    loader = DataLoader(dataset, batch_size=1,
                            shuffle=True, collate_fn=dataset.collate_fn)

    for idx, batch_samples in enumerate(loader):
        sentences, labels, masks, lens = batch_samples
        sentences = sentences.to(device)
        labels = labels.to(device)
        masks = masks.to(device)
        y_pred = model.forward(sentences)
        labels_pred = model.crf.decode(y_pred, mask=masks)
        pred_tags.extend([[id2tag.get(idx) for idx in indices] for indices in labels_pred])

    # labels_pred = model(sents)

    print(pred_tags)

    word_list = [[_t for _t in split_word_list]]
    results = entity(word_list, pred_tags) # 找到对应的实体

    return results

def preprocess(seq, to_ix):
    idxs = []
    for w in seq:
        if w in to_ix.keys():
            idxs.append(to_ix[w])
        else:
            idxs.append(len(to_ix))
    return torch.tensor(idxs, dtype=torch.long)

def entity(word_list, labels):
    # 变成实体返回
    """
        输入是字符
    """
    results = []
    for i in range(0,len(word_list[0])):
        if labels[0][i][0] == 'B':
            word = word_list[0][i]
            for j in range(i+1,len(word_list[0])):
                if labels[0][j][0] == 'I':
                    word += ' ' + word_list[0][j]
                else:
                    break
            label = labels[0][i].split('-')[1]

            results.append([word,label])

    return results
if __name__ == '__main__':
    # app.run()
    app.run(host='0.0.0.0')