# Copyright (c) 2008, Michael Lunnay <mlunnay@gmail.com.au>
#
# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

__all__ = ['FileTransfer', 'IDownloadManipulator', 'IUploadManipulator'
           'IListDir', 'makeDirectoryEntry']

import os
import stat
import zlib
import base64

from jsonrpc import serviceProcedure, String, Number, Boolean, Object, Array, ApplicationError, JSONRPCAssertionError
from plugin import Plugin, ExtensionPoint, Interface, implements
from crc16 import crc16
from servicepluginhandler import IRPCService

class IDownloadManipulator(Interface):
    """Extension point interface for plugins that need to do custom handling for file downloads."""

    def download(self, head, tail, rootpath):
        """Fetch the data to be sent.

        This should return a string with the data to be returned.

        @param head: A string with the requested path that precedes tail.
        @param tail: the requested path relative to rootpath.
        @param rootpath: A string with the local rootpath for the path of the request.
        """

    def handles(self, head, tail):
        """Utility method to tell if the plugin handles a particular request.

        This will allow a plugin to only handle graphic files by only returning
        true if the file extension is .jpg, .png, etc.

        @param head: A string with the requested path that precedes tail.
        @param tail: the requested path relative to rootpath.
        @return: True if this plugin will handle the request, False otherwise.
        """

class IUploadManipulator(Interface):
    """Extension point interface for plugins that need to do custom handling for file uploads."""

    def upload(self, head, tail, data, rootpath):
        """Handle the data sent to the server.

        @param head: A string with the requested path that precedes tail.
        @param tail: the requested path relative to rootpath.
        @param data: a string containing the data that is being uploaded.
        @param rootpath: A string with the local rootpath for the path of the request.
        """

    def handles(self, head, tail):
        """Utility method to tell if the plugin handles a particular request.

        This will allow a plugin to only handle graphic files by only returning
        true if the file extension is .jpg, .png, etc.

        @param head: A string with the requested path that precedes tail.
        @param tail: the requested path relative to rootpath.
        @return: True if this plugin will handle the request, False otherwise.
        """

class IListDir(Interface):
    """Extension point interface for plugins that need to do custom handling for file uploads."""

    def listDir(self, head, tail, rootpath):
        """Return a directory listing of the requested path.

        @param path: the path to list.
        @param rootpath: A string with the local rootpath for the path of the request.
        @return: a sequence of dictionaries with keys name, size, isDir and readonly.

        """

def makeDirectoryEntry(name, size, isdir = False, readonly = False):
    """Utility class for returning a dictionary of directory content information."""

    out = {"name": name,
           "size": size,
           "isDir": isdir,
           "readonly": readonly
           }

    return out

class FileTransfer(Plugin):
    """This is a JSON-RPC service that provides download and uploading capabilities."""

    _jsonrpcName = "file"

    implements(IRPCService)

    download_plugins = ExtensionPoint(IDownloadManipulator)
    upload_plugins = ExtensionPoint(IUploadManipulator)
    listdir_plugins = ExtensionPoint(IListDir)

    @serviceProcedure(summary="This method is used to request a file from the server.",
                      params=[String('file'), Boolean('compress')],
                      ret=Object())
    def download(self, file, compress = False):
        """This method is used to request a file from the server.

        @param file: path of the file to download.
        @param compress: if True the contents of the file will be compressed
            with zlib before sending.
        @return: a JSON-RPC Object with keys data, crc, and compressed.

        """

        cfg = getConfig(file)
        dl_plugins = dict([[x.__class__.__name__, x] for x in self.download_plugins])
        for plugin in cfg['plugins']:
            if dl_plugins.has_key(plugin):
                plugin = dl_plugins[plugin]
                if plugin.handles(cfg['head'], cfg['tail']):
                    data = plugin.download(cfg['head'], cfg['tail'], cfg['rootpath'])
                    break
            else:
                self.log.debug("FileTransfer.download: Skipping plugin %s, not found" % plugin)
        else:   # default download handling, just send the file
            path = os.path.normpath(os.path.join(cfg['rootpath'], cfg['tail']))
            if not os.path.exists(path):
                raise ApplicationError('IOError: No such file or directory: %s' % file)
            if not os.isfile(path):
                raise ApplicationError('IOError: %s is a directory' % file)
            data = open(path, 'rb').read()

        # compute the crc of the data
        crc = crc16(data)

        # compress the data if so requested
        if compress:
            data = zlib.compress(data)

        # encode the data in base64
        data = base64.b64encode(data)

        return {'data': data, 'crc': crc}

    @serviceProcedure(summary="This method uploads a file to the server.",
                      params=[String('data'), Number('crc'), Boolean('compress')],
                      ret=None)
    def upload(self, filename, data, crc, compressed = False):
        """This method uploads a file to the server.

        @param filename: file name of the file to download.
        @param data: the contents of the file being uploaded in base64 format.
        @param crc: the CRC-16-IBM (CRC16) Cyclic Redundancy Check for the data.
        @param compressed: if True the data will be decompressed with zlib.

        """

        # decode the base64 encoded string
        data_ = base64.b64decode(data)
        # decompress the data if needed
        if compressed:
            data_ = zlib.decompress(data)

        # make sure we recieved what we were expecting by computing the crc value
        if crc16(data_) != crc:
            self.log.debug('FileTransfer.upload: crc values did not match for request %s' % filename)
            raise JSONRPCAssertionError('crc value does not match')

        cfg = getConfig(file)
        ul_plugins = dict([[x.__class__.__name__, x] for x in self.upload_plugins])
        for plugin in cfg['plugins']:
            if ul_plugins.has_key(plugin):
                plugin = ul_plugins[plugin]
                if plugin.handles(cfg['head'], cfg['tail']):
                    plugin.upload(cfg['head'], cfg['tail'], data_, cfg['rootpath'])
                    break
            else:
                self.log.debug("FileTransfer.upload: Skipping plugin %s, not found" % plugin)
        else:   # default upload handling, just save the data
            path = os.path.normpath(os.path.join(cfg['rootpath'], cfg['tail']))
            if not os.path.exists(path):
                raise ApplicationError('IOError: No such file or directory: %s' % filename)
            if not os.isfile(path):
                raise ApplicationError('IOError: %s is a directory' % filename)

            f = open(path, 'wb')
            f.write(data_)
            f.close()

    @serviceProcedure(summary="Returns a directory listing of the given path.",
                      params=[String('path')],
                      ret=Array())
    def listDir(self, path):
        """Returns a directory listing of the given path.

        @param dir: the directory to list the contents of.
        @return: An Array of Objects each one listing the contents of the directory.
            Each Object has the keys name, size,
            dir (if true object is a directory),
            and readonly (if true object is read-only).

        """

        cfg = getConfig(path)
        list_plugins = dict([[x.__class__.__name__, x] for x in self.listdir_plugins])
        for plugin in cfg['plugins']:
            if list_plugins.has_key(plugin):
                plugin = list_plugins[plugin]
                data = plugin.listDir(cfg['head'], cfg['tail'], cfg['rootpath'])
                break
            else:
                self.log.debug("FileTransfer.listDir: Skipping plugin %s, not found" % plugin)
        else:   # default listDir handling
            path = os.path.normpath(os.path.join(cfg['rootpath'], cfg['tail']))
            if not os.path.exists(path):
                raise ApplicationError('IOError: No such file or directory: %s' % path)
            if not os.isdir(path):
                raise ApplicationError('IOError: %s is not a directory' % path)

            out = []
            for i in os.listdir(path):
                name = i
                size = os.path.getsize(os.path.join(path, i))
                isdir = os.path.isdir(os.path.join(path, i))
                readonly = os.access(os.path.join(path, i), os.W_OK)
                out.append(makeDirectoryEntry(name, size, isdir, readonly))

            return out

    def getConfig(self, path):
        """Get the configuration for the supplied path.

        @param path: the requested file path.
        @return: A dictionary with the configuration values for the given path.
            It has the keys rootpath, readonly and plugin from the directory
            configuration as well as the keys head and tail that contain the
            configuration path and the remainder of the path relative to
            rootpath.

        """
        tpath = path
        if tpath == '':
            tpath = '/'

        cfg = self.config.get('file', {})
        pathcfg = {}
        head = path
        tail = ''
        for p in cfg.get('directories', {}):
            prefix = os.path.commonprefix((tpath, p))
            if prefix != '' and len(p) == len(prefix):
                pathcfg = cfg['directories'][p]
                head = prefix
                tail = tpath[len(prefix):]
                if tail[0] == '/':
                    tail = tail[1:]
                break

        out = {}

        if pathcfg.has_key('rootpath'):
            out['rootpath'] = pathcfg['rootpath']
        else:
            out['rootpath'] = cfg.get('rootpath', 'servdocs')
            head = '/'
            tail = tpath[1:]

        if not os.path.isabs(out['rootpath']):
            out['rootpath'] = os.path.abspath(out['rootpath'])

        out['readonly'] = pathcfg.get('readonly', False)
        out['plugins'] = pathcfg.get('plugins', [])
        out['head'] = head
        out['tail'] = tail

        return out


import math
from LegServo.servo_ax12a import *

# Class definition of ax12a-controller class, defines interface to the robot
# ===============================================================================
# Implements the interface between leg- and servo class
# ------------------------------------------------------------------------------
# Provides all required methods that allow the leg class to control the servo
# Implements all necessary codomain conversion between leg- and servo values
# Limits values too valid servo values
# Servo uses ticks from 0 to 1023 for angle and speed
# Leg uses angles in radian and rotation per minit for speed
# todo: Defines zero angle as average of min- and max value -> positive and negativ angles are allowed!!!
# todo: Pass through Errors correctly
class JointDrive(ServoAx12a):
    # Definition of public class attributes
    # ----------------------------------------------------------------------

    # 1 time calculation for offsets
    # todo: second offset
    # todo: Zero angle offset of servo in radian
    _ANGLE_RADIAN_ZERO = (ServoAx12a._ANGLE_MAX_DEGREE - ServoAx12a._ANGLE_MIN_DEGREE) * math.pi / 360

    # Max angleoffset of servo in radian

    _ANGLE_RADIAN_MAX = ServoAx12a._ANGLE_MAX_DEGREE * 2 * math.pi / 360

    # Min angleoffset of servo in radian

    _ANGLE_RADIAN_MIN = ServoAx12a._ANGLE_MIN_DEGREE * math.pi / 360

    # Ticks per rad
    _ANGLE_UNIT = ServoAx12a._ANGLE_MAX_TICKS / (
                (ServoAx12a._ANGLE_MAX_DEGREE - ServoAx12a._ANGLE_MIN_DEGREE) * math.pi * 2 / 360)

    # Factor for speed to ticks
    _SPEED_TO_TICKS = ServoAx12a._SPEED_MAX_TICKS / ServoAx12a._SPEED_MAX_RPM

    # Factor for ticks to speed
    _TICKS_TO_SPEED = ServoAx12a._SPEED_MAX_RPM / ServoAx12a._SPEED_MAX_TICKS

    # Constructor, defines the following variables: counterClockWise, angleOffset, angleMax, angleMin
    def __init__(self, id, ccw=False, aOffset=0.0, aMax=math.pi * 2, aMin=-math.pi * 2):

        # id -> id of servo, cw -> rotating direction, aOffset -> angle offset,
        self.id = id
        self.counterClockWise = ccw
        self.aMax = JointDrive._ANGLE_RADIAN_MAX
        self.aMin = JointDrive._ANGLE_RADIAN_MIN

        # aMax -> maximum angle allowed, aMin -> minimum angle allowed
        if aMax > aMin:
            if aMax < self.aMax:
                self.aMax = aMax
            if aMin > self.aMin:
                self.aMin = aMin

        self.aOffset = aOffset
        self.curAngel = 0
        self.error = 0
        # access to Constructor to superclass (Servo)
        super().__init__(id)

    # Converts angle in radian to servo ticks
    # angle -> in radian, returns angle in servo ticks

    def __convertAngleToTicks(self, angle: float) -> int:

        '''if angle < self.aMin + self.aOffset or angle > self.aMax + self.aOffset:
            angle = self.aMin'''
        # control Servo from the right side
        if self.counterClockWise:
            angle = self._ANGLE_RADIAN_MAX - self._ANGLE_RADIAN_ZERO - angle - self.aOffset
        else:
            angle = self._ANGLE_RADIAN_MIN + self._ANGLE_RADIAN_ZERO + angle + self.aOffset

        return int(round(abs((self._ANGLE_UNIT * angle))))

   # def __convertByteToInt(nByte: list) -> int:
    #
     #   if nByte is not None and isinstance(nByte, list):
      #      nByte.reverse()
       #     n = len(nByte)
        #    res = 0
         #   for byte in nByte:
          #      res += byte << (8 * (n - 1))
           #     n -= 1
#
 #           return res
  #      else:
   #         return -1

    # Converts servo ticks to angle in radian
    # ticks -> servo ticks, returns angle in radian
    def __convertTicksToAngle(self, ticks: int) -> float:
        # convert bytes into one integer

        # if ccw
        if self.counterClockWise:
            angle = JointDrive._ANGLE_RADIAN_MAX - (ticks / self._ANGLE_UNIT) + self.aOffset
        else:
            angle = (ticks / self._ANGLE_UNIT) + self.aOffset

        return angle

    # Converts speed in rpm to servo ticks
    # speed -> value in rpm
    def __convertSpeedToTicks(self, speed: float) -> int:
        if speed >= self._SPEED_MAX_RPM:
            ticks = int(round(self._SPEED_MAX_TICKS))
        else:
            ticks = int(round(self._SPEED_TO_TICKS * speed))

        return ticks

    # Converts ticks to speed in rpm
    # ticks -> servo ticks
    def __convertTicksToSpeed(self, ticks: int) -> float:
        # Not needed Right now?
        # Check when its used -> when to use
        speed = ticks * JointDrive._TICKS_TO_SPEED
        return speed

    # Public methods
    # ----------------------------------------------------------------------
    # Get current angle of servo
    # returns angle in radian
    def getCurrentJointAngle(self) -> float:
        self.curAngel = self.__convertTicksToAngle(self.getPresentPosition())
        return self.curAngel

    # Set servo to desired angle
    # angle -> in radian,
    # speed -> speed of movement, speed < 0 -> no speed set, speed = 0 -> maximum speed
    def setDesiredJointAngle(self, angle: float, trigger: bool = False) -> bool:

        success = self.setGoalPosition(self.__convertAngleToTicks(angle), trigger)
        if success:
            self.curAngel = angle

        return success

    # Set servo to desired angle
    # angle -> in radian,
    # speed -> speed of movement in rpm, speed = 0 -> maximum speed
    def setDesiredAngleSpeed(self, angle: float, speed: float = 0, trigger: bool = False) -> bool:
        speed_in_ticks = self.__convertSpeedToTicks(speed)
        angle_in_ticks = self.__convertAngleToTicks(angle)

        success = self.setGoalPosSpeed(angle_in_ticks, speed_in_ticks, trigger)

        return success

    # Set speed value of servo
    # speed -> angle speed in rpm
    def setSpeedValue(self, speed, trigger=False):
        speed_in_ticks = self.__convertSpeedToTicks(speed)
        success = self.setMovingSpeed(speed_in_ticks, trigger)

        return success


import random

class Die:
    def __init__(self):
        self._faceValue=random.randint(1,6)
    def GetFacevalue(self):
        return self._faceValue

class DiceGame:
    def __init__(self):
        print("What is your name?")
        name=input(">")
        print("Hello,{0}!".format(name,))
        sum=0
        print("Rolling the dice...")
        for i in range(2):
            d=Die()
            v=d.GetFacevalue()
            sum+=v
            print("Die {0}: {1}".format(i+1,v))
        print("Total value: {0}".format(sum))
        if sum>=7:
#           print("You won!")
            print("{0} won!".format(name))
        else:
#           print("You lose")
            print("{0} lost".format(name))

if __name__=="__main__":
    DiceGame()


import json
import pandas as pd

def readJSON():
    file_path = "song5.json"
    with open(file_path, encoding='utf-8', mode='r') as json_file:
        json_data = json.load(json_file)

    data = json_data
    df = pd.DataFrame.from_dict(data, orient='columns')

    for i, row in df.iterrows():
        if len(row['artist_name_basket']) != 0:
            df.loc[i, 'artist'] = row['artist_name_basket'][0]

    del df['index']
    del df['genre']
    del df['id']
    del df['artist_name_basket']
    print(df.columns)
    print(df)

    # df.to_json('song5.json', orient='table')

if __name__ == '__main__':
    readJSON()


from .base_enums import AvatarColor

class HairColor(AvatarColor):
    """Hair/facial hair colors. You can use any other color using its hex code"""

    __install__ = True
    __enum_path__ = 'hair_colors.py'

    AUBURN = '#A55728'
    BLACK = '#2C1B18'
    BLONDE = '#B58143'
    BLONDE_GOLDEN = '#D6B370'
    BROWN = '#724133'
    BROWN_DARK = '#4A312C'
    PASTEL_PINK = '#F59797'
    PLATINUM = '#ECDCBF'
    RED = '#C93305'
    SILVER_GRAY = '#E8E1E1'


from django.conf.urls import include, url
from django.contrib import admin
from django.conf import settings
from django.conf.urls.static import static
from django.contrib.auth import views as auth_views

urlpatterns = [
    # Examples:
    # url(r'^$', 'buscoayuda4101.views.home', name='home'),
    # url(r'^blog/', include('blog.urls')),
    url(r'^', include('polls.urls', namespace="principal")),
    url(r'^admin/', include(admin.site.urls)),
    url(
        r'^login/$',
        auth_views.login,
        {
            'template_name': 'login.html'
        },
        name='login',
    ),

] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)


import math

def Change(m, coins):

    minCoins = []
    minCoins.append(0)

    for amount in range(1, m+1):

        minCoins.append(math.inf)

        for coin in coins:
            if amount >= coin:
                numCoins = minCoins[amount-coin]+1

                if numCoins < minCoins[amount]:
                    minCoins[amount] = numCoins

    return minCoins[m]

if __name__ == "__main__" :
    m = int(input())

    coins = [1, 3, 4]

    print(Change(m, coins))


"""
Data Collection | Cannlytics

Authors:
  Keegan Skeate <keegan@cannlytics.com>
  Charles Rice <charles@ufosoftwarellc.com>
Created: 6/15/2021
Updated: 8/10/2021
"""
try:

    # External imports
    import pandas as pd
    import xlwings

    # Internal imports
    from cannlytics.utils.utils import snake_case
    from cannlytics.firebase import update_document

except:
    pass # FIXME: Docs can't import.

def clean_column_names(df, column):
    """
    Args:
        df (DataFrame): A DataFrame with any column names.
    Returns:
        (DataFrame): A DataFrame with snake_case column names.
    """
    df[column] = df[column].str.strip()
    df[column] = df[column].str.rstrip('.)]')
    df[column] = df[column].str.replace('%',  'percent', regex=True)
    df[column] = df[column].str.replace('#',  'number', regex=True)
    df[column] = df[column].str.replace('[/,-]',  '_', regex=True)
    df[column] = df[column].str.replace('[.,(,)]',  '', regex=True)
    df[column] = df[column].str.replace("\'",'', regex=True)
    df[column] = df[column].str.replace("[[]",  '_', regex=True)
    df[column] = df[column].str.replace(r"[]]",  '', regex=True)
    df[column] = df[column].str.replace(' ',  '_', regex=True)
    df[column] = df[column].str.replace('β',  'beta', regex=True)
    df[column] = df[column].str.replace('Δ',  'delta', regex=True)
    df[column] = df[column].str.replace('δ',  'delta', regex=True)
    df[column] = df[column].str.replace('α',  'alpha', regex=True)
    df[column] = df[column].str.replace('__',  '', regex=True)
    df[column] = df[column].str.lower()
    return df

def import_data_model(directory):
    """Import analyses to Firestore from a .csv or .xlsx file.
    Args:
        filename (str): The full filename of a data file.
    """
    analyses = pd.read_excel(directory + 'analyses.xlsx')
    analytes = pd.read_excel(directory + 'analytes.xlsx')
    for index, analysis in analyses.iterrows():
        analyte_data = []
        analyte_names = analysis.analyte_keys.split(', ')
        for analyte_key in analyte_names:
            analyte_item = analytes.loc[analytes.key == analyte_key]
            analyte_data.append(analyte_item.to_dict(orient='records'))
        analyses.at[index, 'analytes'] = analyte_data
    analyses_data = analyses.to_dict(orient='records')
    for index, values in analyses_data.iterrows():
        doc_id = str(values.key)
        doc_data = values.to_dict()
        ref = ''
        update_document(ref, doc_data)
        # doc_data = data.to_dict(orient='index')
        # data_ref = create_reference(db, ref)
        # data_ref.document(doc_id).set(doc_data, merge=True)
        # data_ref.set(doc_data, merge=True)

    raise NotImplementedError

def import_measurements():
    """Import measurements taken by scientific instruments.
    Args:

    Returns:
    """

    print('Importing measurements...')

    raise NotImplementedError

def get_sample_name(df, sheetname='Sheet1', var='samplename'):
    """Return the sample name from a dictionary.
    Converts the first column of the first sheet to lowercase.
    Args:
        df (DataFrame): A DataFrame.
    Returns:
        (dict): The sample name as a key, value pair.
    """
    df[sheetname].ObjClass = df[sheetname].ObjClass.str.lower()
    samples = dict(df[sheetname][df[sheetname].ObjClass == var].values)
    return samples

def get_compound_dataframe(df, sheetname='Compound'):
    """ Rename the columns in the `Compound` sheet to match the required
    names. For simplicity, make a copy of the `Compound` sheet to
    handle NaN values.
    Args:
        df (DataFrame): A DataFrame.
    Returns:
        (DataFrame): A DataFrame with renamed compounds.
    """
    columns = {
        'Name':'analyte',
        'Amount': 'measurement',
    }
    df[sheetname].rename(columns=columns, inplace=True)
    compounds = df[sheetname].copy()
    criterion = (compounds.analyte.isnull()) & (compounds.measurement > 0)
    compounds.loc[criterion, 'analyte'] = 'wildcard'
    compounds.dropna(subset=['analyte'], inplace=True)
    return compounds

def import_agilent_gc_residual_solvents(file_name):
    """Read in all the excel sheets at one time, get the sample name,
    get and cleanup the compound df from the compound sheet,
    add the analyte names and measurements to an array of dictionaries,
    and add that to the main array.
    Args:
        df (DataFrame): A DataFrame.
    Returns:
        (dict): A dictionary of sample results.
    """
    df = pd.read_excel(file_name, sheet_name = None)
    samples = get_sample_name(df)
    compounds = get_compound_dataframe(df)
    samples['metrics'] = compounds[['analyte', 'measurement']].to_dict('records')
    return samples

def import_agilent_gc_terpenes(file_name):
    """Read in all the excel sheets at one time, get the sample name,
    get and cleanup the compound df from the compound sheet,
    replace special characters, add the analyte names and measurements
    to an array of dictionaries, and add that to the main array.
    Args:
        df (DataFrame): A DataFrame.
    Returns:
        (list): A list of measurements (dict).
    """
    df = pd.read_excel(file_name, sheet_name = None)
    samples = get_sample_name(df)
    compounds = get_compound_dataframe(df)
    compounds = clean_column_names(compounds, 'analyte')
    samples['metrics'] = compounds[['analyte', 'measurement']].to_dict('records')
    return samples

def import_agilent_cannabinoids(file_name):
    """This is the same as residual solvents routine.
    Args:
        df (DataFrame): A DataFrame.
    Returns:
        (dict): A dictionary of sample results.
    """
    return import_agilent_gc_residual_solvents(file_name)

def import_heavy_metals(file_name):
    """
    Args:
        df (DataFrame): A DataFrame.
    Returns:
        (list): A list of measurements (dict).
    """

    # Read in the log sheet and summary sheets seperately to parsing easier.
    log_df = pd.read_excel(file_name, sheet_name = 'Log')
    summary_df = pd.read_excel(file_name, sheet_name = 'Quant Summary')

    # Drop the rows that do not contain sample ids.
    log_df.dropna(subset = ['Sample Mass (g)'], inplace = True)

    # Rename columns to make parsing clearer.
    summary_df.rename(columns = {'Analysis':'analyte','-':'mass'}, inplace = True)

    # Get list of samples.
    sample_ids = log_df['Sample ID'].tolist()

    # Parse measurements.
    samples = []
    measurements = {}
    for sample_id in sample_ids:
        sample = {}
        sample['sample_id'] = sample_id
        sample['sample_mass'] = log_df[log_df['Sample ID'] == sample_id]['Sample Mass (g)'].values[0]
        sample['sample_dilution'] = log_df[log_df['Sample ID'] == sample_id]['Sample Dilution'].values[0]

        analytes = []
        measurements['measurements'] = analytes
        index  = summary_df.index[(summary_df['analyte'] == 'ID:') & (summary_df['mass'] == sample_id)].tolist()
        for offset in range(index[0] + 20, index[0] + 27):
            analyte = {}
            analyte['analyte'] = summary_df.iloc[offset].analyte
            analyte['measurement'] = summary_df.iloc[offset+9].mass
            analytes.append(analyte)

        samples.append(sample)
        samples.append(measurements)

    return samples


import time

import config
from collections import namedtuple

from dqn_action_selection import get_state_after_action

def count_pieces_on_tile(player_no, state, tile_no):
    value = state[player_no][tile_no]
    return value * 4

def map_enemy_tile_id_to_player_0(i_enemy, tile_id):
    """ knowing enemy id and the tile that he sees
     check what is the tile in the coordinates of ai player """
    tile_ai = i_enemy*13 + tile_id
    if tile_ai >= 53:
        tile_ai -= 53
        tile_ai += 1
    if tile_id >= 54:
        tile_ai = 0
    return tile_ai

def get_max_reward_from_state(pieces_player_begin, dice, state_begin, possible_actions):
    """ need to check all the movable pieces, and calculate all the possible rewards and get the maximum one """
    from DQN_plays import get_state_after_action_g

    max_reward = 0
    for action in possible_actions:

        # get next state
        # state_new = get_state_after_action_g(game, action)  # todo: rewrite to not using game
        state_new = get_state_after_action(pieces_player_begin, state_begin, dice, action)

        # get reward
        # reward = get_reward(dice, state, action, state_new, pieces_player_begin=pieces_player_begin)
        reward = get_reward(dice=dice, state_begin=state_begin, piece_to_move=action, state_new=state_new,
                            pieces_player_begin=pieces_player_begin, actual_action=False)  # immediate reward

        # reward = get_reward(state, action, state_new)
        if reward >= max_reward:
            max_reward = reward

    return max_reward

def enemy_pieces_nearby(player_id, state, horizon):
    """ horizon - how far away pieces should be considered
    :return True when there are some pieces in danger """
    player_state = state[player_id]
    enemy_states = []
    for i in range(len(state)):
        if i != player_id:
            enemy_states.append(state[i])

    tiles_with_player_pieces = []
    for tile_id, value in enumerate(player_state):
        if value != 0:
            # ok we have some of our pieces here
            tiles_with_player_pieces.append(tile_id)
    tiles_with_player_pieces = set(tiles_with_player_pieces)

    tiles_to_check = []
    for tile_id in tiles_with_player_pieces:
        # get the range of tiles to check
        min_tile = tile_id - horizon
        max_tile = tile_id + horizon

        # select only dangerous positions
        tiles_to_consider = list(range(min_tile, max_tile))
        tiles_to_consider = [num for num in tiles_to_consider if num >= 2 and num <= 52]

    for enemy_state in enemy_states:
        for i in tiles_to_consider:
            if enemy_state[i] > 0:
                # there is an enemy in our horizon!
                # print("whole state:")
                # print(state)
                return True
    return False

def map_tile_id_p0_to_enemy(i_enemy, tile_id_p0_check):
    """ p0-1 -> e1-40, e2-27, e3-14
        p0-2 -> e2-41, e2-28, e3-15
    """
    if tile_id_p0_check == 0:
        return 0

    tile_enemy = i_enemy*(-13) + tile_id_p0_check
    if tile_enemy >= 53:
        tile_enemy -= 53
        # tile_enemy += 1
    if tile_enemy >= 54:
        tile_enemy = 0

    if tile_enemy < 1:
        tile_enemy += 53
        tile_enemy -= 1

    return tile_enemy

def did_loose_piece(state_begin, state_prev_new, pieces_player_begin):
    # check all the pieces and count how many in home
    if len(config.learning_info_data.whole_list) < 1 or int(config.learning_info_data.whole_list[-1]['round']) < 2:
        # if int(config.learning_info_data.whole_list[-1]['round']) < 2:
        # print("config.learning_info_data.whole_list['round']", config.learning_info_data.whole_list[-1]['round'])
        return False
    # print("config.learning_info_data.whole_list[-1]['round']", config.learning_info_data.whole_list[-1]['round'])
    no_piece_home_before = 0
    for i_piece, val_piece in enumerate(pieces_player_begin):
        if pieces_player_begin[i_piece] == config.home_tile:
            no_piece_home_before += 1

    # check the last positions of our pieces
    piece_positions_prev_new = []
    pieces_prev_new_home = 0
    # print("state_prev_new[0]", type(state_prev_new[0].tolist()), state_prev_new[0].tolist())
    for i_tile, value_tile in enumerate(state_prev_new[0]):
        # print("i_tile = ", i_tile, "value_tile = ", value_tile)
        if value_tile > 0:
            pieces_there = int(value_tile * 4)
            for i in range(pieces_there):
                piece_positions_prev_new.append(i_tile)

                if i_tile == 0:
                    pieces_prev_new_home += 1

    # print("state_prev_new[0]", state_prev_new[0])
    # print("state_begin[0]", state_begin[0])

    pieces_begin = pieces_player_begin.tolist()
    pieces_begin.sort()
    pieces_prev = piece_positions_prev_new
    pieces_prev.sort()

    # print("pieces_prev", pieces_prev)
    # print("pieces_begin", pieces_begin)
    # time.sleep(0.2)

    if pieces_begin != pieces_prev:
        # something changed in between!
        # check the number of pieces in home
        pieces_now_home = state_begin[0][config.home_tile]
        if pieces_prev_new_home > pieces_now_home:
            in_home_prev = pieces_prev.count(0)
            in_home_now = pieces_begin.count(0)
            if in_home_now > in_home_prev:

                # find the id of the tile that the piece was lost
                tiles_id_difference_p0 = list(set(pieces_prev).difference(set(pieces_begin)))
                # print("state_prev_new", state_prev_new)
                # print("state_begin", state_begin)

                # look in all of the enemies and check if the enemy moved there
                for i_enemy in range(1, len(state_begin)):

                    for tile_id_p0_check in tiles_id_difference_p0:

                        # state_enemy_from_player0 = get_enemy_mapped_state(i_enemy=i_enemy, state=state_begin)
                        enemy_begin_state = state_begin[i_enemy]
                        enemy_last_state = state_prev_new[i_enemy]
                        # todo:
                        # ok we have the tile where the enemy could have been - mapped to player_0
                        tile_id_player0_on_enemy = map_tile_id_p0_to_enemy(i_enemy, tile_id_p0_check)

                        # check if enemy moved to the tile
                        enemy_pieces_begin = enemy_begin_state[tile_id_player0_on_enemy]
                        enemy_pieces_last = enemy_last_state[tile_id_player0_on_enemy]

                        # print("pieces_begin", pieces_begin)
                        # print("tile_id_p0_check", tile_id_p0_check)
                        # print("state_prev_new", state_prev_new)
                        # print("state_begin", state_begin)
                        # print("state_begin[0][tile_id_p0_check]", state_begin[0][tile_id_p0_check])
                        # print("enemy_begin_state", enemy_begin_state)
                        # print("tile_id_player0_on_enemy", tile_id_player0_on_enemy)
                        # print("enemy_last_state[tile_id_player0_on_enemy]", enemy_last_state[tile_id_player0_on_enemy])
                        # print("enemy_begin_state[tile_id_player0_on_enemy]", enemy_begin_state[tile_id_player0_on_enemy])

                        # print("tile_id_p0_check = %d| state_prev_new[0][tile_id_p0_check]=%.2f->state_begin[0][tile_id_p0_check]=%.2f" % (tile_id_p0_check, state_prev_new[0][tile_id_p0_check], state_begin[0][tile_id_p0_check]))
                        # print("tile_id_p0_check = %d| i_enemy=%d, tile_id_player0_on_enemy=%d | enemy_pieces_last=%.2f->enemy_pieces_begin=%.2f" % (tile_id_p0_check, i_enemy, tile_id_player0_on_enemy, enemy_pieces_last, enemy_pieces_begin))
                        # exit()

                        # check if player lost the piece from tile
                        # if state_begin[i_enemy][tile_id_p0_check] > 0:
                        # if enemy_pieces_begin > 0:
                        if enemy_pieces_begin > 0 and enemy_pieces_begin > enemy_pieces_last:

                            # print("state_prev_new[i_enemy][tile_id_p0_check]", state_prev_new[i_enemy][tile_id_p0_check])
                            # print("state_begin[i_enemy][tile_id_p0_check]", state_begin[i_enemy][tile_id_p0_check])
                            # print("state_begin[i_enemy]", state_begin[i_enemy])
                            # print("state_prev_new[0][tile_id_p0_check]", state_prev_new[0][tile_id_p0_check])

                            # time.sleep(3)
                            # exit("piece lost")
                            # print("DETECTED LOOSING A PIECE!")
                            return True
                        else:
                            # print("Didnt detect loosing a piece!")
                            pass
                # print("checked all the enemies and couldnt find!")
                # exit("checked all the enemies and couldnt find!")
            else:
                # print("detected weird movement - probably using a star")
                pass
    else:
        pass
        # print("the positions are the same - all good")
        # time.sleep(1)

    return False

def get_reward(dice, state_begin, piece_to_move, state_new, pieces_player_begin, actual_action=False):
    """
        • 1.0 for winning a game.
        • 0.25 for releasing a piece from HOME.
        • 0.2 for defending a vulnerable piece.
        • 0.15 for knocking an opponent’s piece.
        • 0.1 for moving the piece that is closest to home - if moved from safe space then only 0.05
        • 0.05 for forming a blockade.
        • -0.25 for getting a piece knocked in the next turn.
        • -1.0 for losing a game.

        • go on globe (safe) 0.12
        • use a star (speed) 0.17
    """

    ids_where_pieces_are_safe = [1, range(53, 59)]
    player_i = 0
    state_diff = state_new[player_i] - state_begin[player_i]

    reward = 0

    knocked_pieces = 0

    enemies_already_won = False
    for player_i in range(1, 4):
        # first check knocking out enemies and if game already won by enemies
        # check if enemies return home - detect knocked opponents
        # in_home_before = count_pieces_on_tile(player_no=player_i, state=state_begin, tile_no=home_tile)
        in_home_before = state_begin[player_i][config.home_tile]
        in_home_after = state_new[player_i][config.home_tile]
        if in_home_after > in_home_before:
            reward += 0.15
            knocked_pieces += 1  # debug only
            if actual_action:
                config.rewards_detected['knock_opponent'] += 1
            # print("state_begin\n", state_begin)
            # print("state_new\n", state_new)
            # exit('Check kocking oponnent 39')

        # check if any of the opponents won the game
        # if count_pieces_on_tile(player_no=player_i, state=state_new, tile_no=config.finished_tile) == 4:
        if state_new[player_i][config.finished_tile] == 1:
            enemies_already_won = True

    # check leaving the home for current player and finishing the game
    player_i = 0
    # ai_pl_in_home_before = count_pieces_on_tile(player_no=player_i, state=state_begin, tile_no=config.home_tile)
    ai_pl_in_home_before = state_begin[0][config.home_tile]
    ai_pl_in_home_after = state_new[0][config.home_tile]
    # ai_pl_in_home_after = count_pieces_on_tile(player_no=player_i, state=state_new, tile_no=config.home_tile)

    # print("ai_pl_in_home_after", ai_pl_in_home_after)
    # print("ai_pl_in_home_before", ai_pl_in_home_before)

    if dice == 6:
        if state_new[0][config.home_tile] < state_begin[0][config.home_tile]:# and state_new[0][config.home_tile+1] > state_begin[0][config.home_tile+1]:
            reward += 0.25
            if actual_action:
                # print("releases before", config.rewards_detected['piece_release'])
                config.rewards_detected['piece_release'] += 1
                print("releases after", config.rewards_detected['piece_release'])

                # print("state_begin[0]", state_begin[0])
                # print("state_new[0]", state_new[0])
                # print("detected release!\n")
                # time.sleep(3)

                # if config.rewards_detected['piece_release'] >= 10:
                #     print("round now = ", len(config.learning_info_data.whole_list)+1 )
                #     # config.learning_info_data.whole_list['dice_now'], config.learning_info_data.whole_list['dice_now']
                #     print("config.rewards_detected['piece_release']", config.rewards_detected['piece_release'])
                #     print("state_begin[0]", state_begin[0])
                #     print("state_new[0]", state_new[0])
                #     exit("check print release")

    # check the end of the game
    if enemies_already_won:
        reward -= 1
        if actual_action:
            config.rewards_detected['ai_agent_lost'] += 1
    # elif count_pieces_on_tile(player_no=player_i, state=state_new, tile_no=config.finished_tile) == 4:
    elif state_new[player_i][config.finished_tile] == 1:
        # print("player 0 wins the game in this round")
        reward += 1
        if actual_action:
            config.rewards_detected['ai_agent_won'] += 1

    """ furthest piece away """
    # check if moved piece is the furthest away
    furthest_piece, furthest_dist = 0, 0
    # print("pieces_player_now", pieces_player_now)
    for piece in range(len(pieces_player_begin)):
        # print("pieces_player_now[piece]", pieces_player_now[piece])
        if pieces_player_begin[piece] >= furthest_dist and pieces_player_begin[piece] not in config.safe_corridor:
            furthest_dist = pieces_player_begin[piece]
            furthest_piece = piece
    # print("furthest_piece ", furthest_piece)
    if furthest_piece == piece_to_move and furthest_dist != 0:
        # if the piece was in safe zone - smaller reward
        piece_moved_from_safe_zone = False
        for tile_id, value in enumerate(state_diff):
            if value < 0 and tile_id in ids_where_pieces_are_safe:
                piece_moved_from_safe_zone = True
        if piece_moved_from_safe_zone:
            reward += 0.05
            if actual_action:
                config.rewards_detected['move_closest_safe'] += 1
        else:
            if not furthest_dist in config.safe_corridor:
                reward += 0.1
                if actual_action:
                    config.rewards_detected['move_closest_goal'] += 1
        # exit('chosen furthest one')

    """
    • -0.25 for getting a piece knocked in the next turn - next turn, not the next state
    for that will need to save the previous move's last state and see difference between state_new and state_begin of new turn
    save the new state
    """
    player_i = 0
    # pieces_last = count_pieces_on_tile(player_i, config.last_turn_state_new, 0)
    # pieces_now = count_pieces_on_tile(player_i, state_begin, 0)

    pieces_last = config.last_turn_state_new[0][0]
    pieces_now = state_begin[0][0]

    # if pieces_now > pieces_last: #and len(config.learning_info_data.whole_list) >= 2:
    if did_loose_piece(state_begin=state_begin, state_prev_new=config.last_turn_state_new,
                       pieces_player_begin=pieces_player_begin):
        reward -= 0.25
        if actual_action:
            config.rewards_detected['getting_piece_knocked_next_turn'] += 1
            # if config.rewards_detected['getting_piece_knocked_next_turn'] > config.rewards_detected['piece_release']:
            # print("pieces_last", pieces_last, config.last_turn_state_new)
            # print("pieces_now", pieces_now, state_begin)

            # time.sleep(1)
                # exit("can not loose more pieces than released!")
        # print("config.last_turn_state_new \n", config.last_turn_state_new)
        # print("state_begin \n", state_begin)
        # exit("check loosing piece in next round")

    # 0.05 for forming a blockade
    player_i = 0
    # print("begin_state", state_begin[player_i])
    # print("new_state", state_new[player_i])
    if state_begin[player_i][0] != 1:
        # print("state_diff", state_diff)
        for tile_id, value in enumerate(state_diff):
            # when new pawn moved and moved in actual dangerous position
            if value >= 0.25 and tile_id in range(1, 54):
                # if the actual number of the players on the tile increased
                # pieces_there_after = count_pieces_on_tile(player_i, state_new, tile_id)
                pieces_there_after = state_new[player_i][tile_id] * 4

                if pieces_there_after > 1:
                    # if pieces_there_after > count_pieces_on_tile(player_i, state_begin, tile_id):
                    if pieces_there_after > state_begin[player_i][tile_id]:
                        reward += 0.05
                        if actual_action:
                            config.rewards_detected['forming_blockade'] += 1
                        # print("MADE A BLOCKADE, SIR!")
                        #
                        # print("state_begin[0]", state_begin[0])
                        # print("state_new[0]", state_new[0])
                        # exit("blockade check")

                        # if there are enemy pieces in range +- 6 add 0.2 more reward
                        #     • 0.2 for defending a vulnerable piece.
                        if enemy_pieces_nearby(player_id=player_i, state=state_new, horizon=6):
                            reward += 0.15
                            if actual_action:
                                config.rewards_detected['defend_vulnerable'] += 1
                            # print("WE WERE IN DANGER, SIR!")
                            # exit("life saving blockade")

    """ • go on globe (safe) 0.12 """
    for globe_tile in config.globe_tiles:
        # pieces_globe_begin = count_pieces_on_tile(player_i, state_begin, globe_tile)
        # pieces_globe_new = count_pieces_on_tile(player_i, state_new, globe_tile)

        pieces_globe_begin = state_begin[player_i][globe_tile]
        pieces_globe_new = state_new[player_i][globe_tile]

        if pieces_globe_new > pieces_globe_begin:
            reward += 0.12
            if actual_action:
                config.rewards_detected['moved_on_safe_globe'] += 1

    """ • using a star (speed) 0.17 """
    for star_tile in config.star_tiles:
        # pieces_star_begin = count_pieces_on_tile(player_i, state_begin, star_tile)
        # pieces_star_new = count_pieces_on_tile(player_i, state_new, star_tile)

        pieces_star_begin = state_begin[player_i][star_tile]
        pieces_star_new = state_new[player_i][star_tile]

        if pieces_star_new > pieces_star_begin:
            reward += 0.17
            if actual_action:
                config.rewards_detected['speed_boost_star'] += 1

    config.last_turn_state_new = state_new
    # exit("test")
    # return reward, config.rewards_detected
    return reward


from imageai.Detection import ObjectDetection
#import os

#execution_path = os.getcwd()

detector = ObjectDetection()
detector.setModelTypeAsRetinaNet()
#detector.setModelPath( os.path.join(execution_path , "resnet50_coco_best_v2.0.1.h5"))
detector.setModelPath( "resnet50_coco_best_v2.0.1.h5")
detector.loadModel()
# detections = detector.detectObjectsFromImage(input_image=os.path.join(execution_path , "image.jpg"), output_image_path=os.path.join(execution_path , "imagenew.jpg"))
print('Object Detect using RetinaNet Model ........')
detections = detector.detectObjectsFromImage(input_image="image.jpg", output_image_path="image_out_retinaNet.jpg")

for eachObject in detections:
    print(eachObject["name"] , " : " , eachObject["percentage_probability"] )

detector = ObjectDetection()
detector.setModelTypeAsYOLOv3()
detector.setModelPath( "yolo.h5")
detector.loadModel()
print('Object Detect using YOLOv3 Model........')
detections = detector.detectObjectsFromImage(input_image="image.jpg", output_image_path="image_out_yolov3.jpg")

for eachObject in detections:
    print(eachObject["name"] , " : " , eachObject["percentage_probability"] )

detector = ObjectDetection()
detector.setModelTypeAsTinyYOLOv3()
detector.setModelPath( "yolo-tiny.h5")
detector.loadModel()
print('Object Detect using TinyYOLOv3 Model........')
detections = detector.detectObjectsFromImage(input_image="image.jpg", output_image_path="image_out_tinyyolov3.jpg")

for eachObject in detections:
    print(eachObject["name"] , " : " , eachObject["percentage_probability"] )


"""
  This file contents all auxiliar functions
"""

import numpy as np

def SafeSum(X):
	X[np.isnan(X)] = 0
	X[np.isinf(X)] = 0
	X[X<0.0] = 0
	return np.sum(X)

def KLDiv(X,Y):
	kl_xy = SafeSum(X*np.log(X/Y))
	kl_yx = SafeSum(Y*np.log(Y/X))
	return [kl_xy, kl_yx]

class ListTable(list):
    """ Overridden list class which takes a 2-dimensional list of
        the form [[1,2,3],[4,5,6]], and renders an HTML Table in
        IPython Notebook. """

    def _repr_html_(self):
        html = ["
"]
        for row in self:
            html.append("
")

            for col in row:
                html.append("{0}	".format(col))

            html.append("
")
        html.append("
")
        return ''.join(html)


from urllib import request
import json
from urllib import parse
import urllib
import time
#ZBH-FBABEE
def is_json(file):
    try:
        json.loads(file)
    except:
        return False
    return True
def str_json(data):
    data=json.loads(data)
    return data
def can_url_get_open(url,data):
    try:
        # data = json.dumps(data)
        # print(data)
        data = bytes(urllib.parse.urlencode(data), encoding='utf8')
        response=request.urlopen(url=url,data=data)
        #print("查看 response 响应信息类型: ",type(response))
        page=response.read()
        page=page.decode('utf-8')
        #url解码
        data=parse.unquote(page)
        #print("data@@@@@@@@@@@@@@@@@@@@@@@@",data)
        return data
    except:
        return 'error'
def url_get_open(data):
    '''
    如果打开网页,则返回打开网页的数据
    如果打不开网页,则返回error
    '''
    try:
        response=request.urlopen(data)
        #print("查看 response 响应信息类型: ",type(response))
        page=response.read()
        page=page.decode('utf-8')
        #url解码
        data=parse.unquote(page)
        #print("data@@@@@@@@@@@@@@@@@@@@@@@@",data)
        return data
    except:
        return 'error'

DATA={}
def init():
    '''
    1.1
    初始化
    '''
    global DATA
    aaa=url_get_open(WJY_URL)
    if is_json(aaa):
        data=str_json(aaa)
        #print("初始化相关data:",data)
        #print("--------------------")
        data1={}
        for key,value in data.items():
            DATA[key]=value
            # data1[key]=value
            # DATA.append(data1)
            # data1={}
init()

DATA1=[]
DATA3=''
SCHOOLNOTICE=''
ONLINE_URL=''
CLASSNOTICE=''
GETCLASSKQ=''
LEAVE_URL=''

def login():
    '''
    2.1
    login接口
    '''
    global DATA1,DATA3,ONLINE_URL,SCHOOLNOTICE,CLASSNOTICE,GETCLASSKQ,LEAVE_URL
    aaa=url_get_open(DATA["login"])

    if  is_json(aaa):
        aaa=str_json(aaa)
       # print("aaa:",aaa['data']["LEAVE_URL"])
        LEAVE_URL=aaa['data']["LEAVE_URL"]
        DATA3=aaa["data"]["schoolnotice"]
        SCHOOLNOTICE=aaa["data"]["schoolnotice"]
        CLASSNOTICE=aaa["data"]["classnotice"]
        GETCLASSKQ=aaa["data"]["getclasskq"]
        ONLINE_URL=aaa["data"]["onlineurl"]
        #print("login:",aaa["data"]["schoolnotice"])
        for i in aaa["data"]["classInfo"]:
            DATA1.append(i)

login()

DATA2=[]
def init_class():
    '''
    2.2
    class接口
    '''
    global DATA2

    for i in DATA1:
        data={
            "classId":i["classId"]
        }
        aaa=can_url_get_open(DATA["class"],data)
        aaa=str_json(aaa)
        DATA2.append(aaa)
    #print("aaa",aaa)
        #print("--------------------")
init_class()

# print("data2",DATA1)
# print("data2",DATA2)

def check():
    '''
    2.3
    check接口
    '''
    cc=''
    cc1=''
    for i in DATA2:
        cc=i["data"]["childs"][0]["signId"]

        cc1=i["data"]["todaytimeset"][0]["start"]
        #print(i["data"]["todaytimeset"][0]["start"])
    data={
        "signId":cc,
        "signTime":cc1,
    }
    ccc=can_url_get_open(DATA["check"],data)
    #print(ccc)
# check()

def banner():
    '''
    2.4banner接口
    '''
    ccc=url_get_open(DATA["banner"])
    #print("ccc",ccc)
# banner()

def roomlist():
    '''
    2.5roomlist
    '''
    #print(DATA["roomlist"])
    ccc=url_get_open(DATA["roomlist"])
    #print("ccc",ccc)
roomlist()

def schoolnotice():
    '''
    2.6schoolnotice接口
    '''
    data={
        "createtime":int(time.time())
    }
    ccc=can_url_get_open(SCHOOLNOTICE+'&macid=ZBH-9843EE',data)
   # print("schoolnotice接口:",ccc)
schoolnotice()

def classnotice():
    '''
    2.7classnotice接口
    '''
    data={
        "createtime":int(time.time())
    }
    ccc=can_url_get_open(CLASSNOTICE+'&macid=ZBH-9843EE',data)
    #print("classnotice接口:",ccc)
classnotice()

def getclasskq():

    data={
        "createtime":int(time.time())
    }
    ccc=can_url_get_open(GETCLASSKQ+'&macid=ZBH-9843EE',data)

getclasskq()
def getleave():
    import urllib.request
    import urllib.parse

    import time
    try:
        url=LEAVE_URL+'&macid=ZBH-9843EE'
        for i in DATA2:
            for j in i["data"]["childs"]:

                # if j["name"]:
                #     print("j",j["name"])
                if j["signId"]:

                    if '#' not in j["signId"]:
                        data_dict={
                        "signtime":int(time.time()),
                        "iccode":j["signId"]
                        }
                        data_string=urllib.parse.urlencode(data_dict)
                        last_data=bytes(data_string,encoding='utf-8')
                        response=urllib.request.urlopen(url,data=last_data)
                        res=response.read().decode('utf-8')

                        if is_json(res):
                            res=str_json(res)
                            res_code=res["data"]["openDoor"]

                            if res_code==1:
                                pass

                            if res_code==0:
                                print("请假人卡号:",j["signId"])
                                print("请假人卡号:",j["name"])
                                print("请假人照片:",j["card2icon"][j["signId"]])

                    if '#' in j["signId"]:
                        aaa=j["signId"].split('#')
                        for k in aaa:
                            if k:
                                data_dict={
                                    "signtime":int(time.time()),
                                    "iccode":k
                                }
                                from Armoury import sock
                                data_string=urllib.parse.urlencode(data_dict)
                                #将序列化后的字符串转换成二进制数据，因为post请求携带的是二进制参数
                                last_data=bytes(data_string,encoding='utf-8')
                                #如果给urlopen这个函数传递了data这个参数，那么它的请求方式则不是get请求，而是post请求
                                #print("*********",url,last_data)
                                response=urllib.request.urlopen(url,data=last_data)
                                #我们的参数出现在form表单中，这表明是模拟了表单的提交方式，以post方式传输数据
                                res=response.read().decode('utf-8')
                                # print(response.read().decode('utf-8'))
                                if is_json(res):
                                    res=str_json(res)
                                    res_code=res["data"]["openDoor"]
                                    # print(res_code)
                                    # print(type(res_code))
                                    if res_code==0:
                                        print("请假人卡号:",k)
                                        print("请假人名字:",j['name'])
                                        print("请假人照片:",j["card2icon"][k])
                                    if res_code==1:
                                        pass

                    #使用urlencode将字典参数序列化成字符串
    except:
        print("接口有问题")

getleave()
def online():
    '''
    3.0
    online接口
    '''
    data=url_get_open(ONLINE_URL+'&macid=ZBH-9843EE')
online()


import argparse

import gym
import torch.multiprocessing as mp

from src import Experiment
from src.agents import PoolManager, AgentPool, DqnAgent, MCTSAgent
from src.agents.policies import LinearlyDecayingPolicy, BoltzmannQPolicy
from src.wrappers import FlattenObsWrapper
from src.utils import learner_factory, mcts_factory

parser = argparse.ArgumentParser()
parser.add_argument('--run', '-n', default='', help='An optional name of this experiment run')
parser.add_argument('--env', default='Connect4Env-v0', help='The environment to train in')
parser.add_argument('--agent', default='DQN', help='The agent to use')
parser.add_argument('--num-learners', type=int, default=4, help='The number of learners to start')
parser.add_argument('--num-train-steps', type=int, default=10000)
parser.add_argument('--num-eval-episodes', type=int, default=10)
parser.add_argument('--mcts-rollouts', nargs='+', type=int, default=[32])
args = parser.parse_args()

dqn_config = {
    'train_policy': {
        'cls': LinearlyDecayingPolicy,
        'inner_policy': BoltzmannQPolicy,
        'config': {
            'attribute': 'tau',
            'value_max': 1.0,
            'value_min': 0.02,
            'decay_period': 1e4,
        },
    },
    'eval_policy': {
        'cls': BoltzmannQPolicy,
        'config': {'tau': 0.02}
    },
}

def env_fn():
    env_config = {}
    env_wrappers = [FlattenObsWrapper]
    env = gym.make(args.env, env_config=env_config)
    for wrapper_cls in env_wrappers:
        env = wrapper_cls(env)
    return env

env = env_fn()
obs_space, action_space = env.observation_space, env.action_space
trainable_agents = learner_factory(DqnAgent, obs_space, action_space, dqn_config, args.num_learners)
mcts_agents = mcts_factory(MCTSAgent, obs_space, action_space, args.mcts_rollouts)

agent_config = dqn_config
pool_config = {'obs_space': obs_space, 'action_space': action_space}
pool_manager = PoolManager({
    'train': AgentPool(
        trainable_agents,
        {
            **pool_config,
            'agent_cls': DqnAgent,
            'agent_config': agent_config
        }
    ),
    'eval': AgentPool(
        mcts_agents,
        pool_config
    ),
})

Experiment({
    'name': f'{args.num_learners}x{args.agent.upper()}-Coevolution',
    'resources': {
        'num_workers': 1,
    },
    'env': env_fn,
    'agent_pool_manager': pool_manager,
    'stop': {
        'trial/num_epochs': 250,
    },
    'phases': {
        'train': {
            'stop': {
                'num_steps': args.num_train_steps * args.num_learners,
            },
            'trainer': {
                'type': 'coevolution',
                'agent_pools': ['train'],
                # 'sampling': 'round_robin',
                'evaluation': 'elo',
                'selection': 'softmax',
                # 'tau': 0.4,
                # 'tau': 0.1,
                # 'tau': 1 / args.num_learners,
                'tau': 0.05,
            },
        },
        'eval': {
            'stop': {
                'num_episodes': args.num_eval_episodes * args.num_learners * len(mcts_agents) * 2,
            },
            'trainer': {
                'type': 'even',
                'agent_pools': ['train', 'eval'],
                'is_training': False,
            },
        },
    },
    'checkpoint': {
        # 'restore': '2020-02-16_13-21-39_CoevolutionTrainer',
        'freq': 10,
        'at_end': True,
    },
}).run()


#!/usr/bin/env python
# coding: utf-8

# In[182]:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

from sklearn import preprocessing
from sklearn.preprocessing import MinMaxScaler

# In[213]:

df = pd.read_csv('sarda_data.csv', index_col=0)
df1 = pd.read_csv('dataToBeUsed_sarda_rev1.csv', index_col=0)
len(df1.columns)

# In[214]:

#data = pd.read_csv('https://www.dropbox.com/s/4jgheggd1dak5pw/data_visualization.csv?raw=1', index_col=0)
data = pd.read_csv('dataToBeUsed_sarda_rev2.csv', index_col=0)
scaler = MinMaxScaler()
scaled_values = scaler.fit_transform(data)
data.loc[:,:] = scaled_values
data.shape

# In[185]:

corr = data.corr()
fig = plt.figure(figsize=(60,20))
ax = fig.add_subplot(111)
cax = ax.matshow(corr,cmap='coolwarm', vmin=-1, vmax=1)
fig.colorbar(cax)
ticks = np.arange(0,len(data.columns),1)
ax.set_xticks(ticks)
plt.xticks(rotation=90)
ax.set_yticks(ticks)
ax.set_xticklabels(data.columns)
ax.set_yticklabels(data.columns)
plt.show()

# In[215]:

t = data[data.columns[1:]].corr()['SP. Power KWH'][:]
t.to_csv('featureCorrelation_rev1.csv')

# In[152]:

#data[data.columns[1:]].corr()['Metal MT'][:]
#data[data.columns[1:]].corr()['Slag      MT'][:]
"""Top correlations for data frame"""
#data.corr().unstack().sort_values().drop_duplicates()
#data[data.columns[1:]].corr()['Coke::Coal'][:]
def str_div(num):
    split_num = num.split('/')
    return round(float(split_num[0])/float(split_num[1]),3)

def str_div(num):
    return round((float(num.split('/')[0]))/(float(num.split('/')[1]),3))

# In[ ]:

moistureGrade = pd.read_csv('EAF_moisture_grade.csv')

# In[157]:

moistureGrade_sum=moistureGrade.sum(axis = 1)

# In[159]:

moistureGrade_sum.to_csv('moistureGrade_sum.csv')

# In[112]:

# In[219]:

# Import pandas package
import pandas as pd

 #list(data) or
list(data.columns)

# In[149]:

# In[ ]:

# In[ ]:

# In[ ]:


import pageClass
from pageClass import localLinkParser
import urllib.request
import bs4
import html.parser
import glob
#A brower like client to grab the next page so all files under a CIK can be
#collected
textlinkscontaingxml = [] #global variable to contain final link to xml file
cik = "0001166559"
#cik = "0001416856" #click number that is looked up on sec.gov site
pagestoload = 4 #total pages to load, if the sec.gov site as a lot of history for
#the cik filing, it loads pages 100 entrys at a time so we will need to loop over the pages
#for instance if the page as 350 entries and we display 100 we will need 4 loops

def mainfunction(startin,countin): #start in should be 0 and progress by +1
#thats the start entry, countin is the id of the final items to display
    start = startin
    count = countin
    url = "https://www.sec.gov/cgi-bin/browse-edgar?action=getcompany&CIK="+cik+"&type=&dateb=&owner=include&start="+str(start)+"&count="+str(count)
    #Grabs the main site based on the CIK number
    newsite = pageClass.Page(url) #the newsite generates a link to each Pullbar(pullvar = "13F-HR") from the table
    #it them generates a list of linktoxml which containes the url which containes the txt xml
    #information.

    prefix = "https://www.sec.gov"

    for item in newsite.linktoxml: #from the linktoxml data in newsite pull out only the
    #items that contain what we are looking for in our case we sort by the keywords .xml Form and information
        url =  prefix + item
        nextpage = pageClass.Page(url)
        nextpagelinks = localLinkParser.handle_starttag(localLinkParser,nextpage.html)
        #if the links on contain text it prints them

        filedescription1 = [".xml","final","fim"]           #need to be changed based on different formats
        filedescription2 = [".xml","Form","information"]    #{".xml" , "Form" , "information"}
        descriptor = filedescription1                       #in nextpagelinks
        if descriptor[1] and descriptor[2] in nextpagelinks[0]: #check if descriptor is in one of the
             pass                                              #next page links
        else:
            descriptor = filedescription2                      #if not go to next descriptor continue
                                                             #continue for as many times as needed
        for item in nextpagelinks:                           #process items based on descriptor
            if descriptor[0] in item:                        #create a list of xml files for download
                if descriptor[1] in item:
                    if descriptor[2] in item:
                        textlinkscontaingxml.append(prefix+item)

x = 0
while(x<pagestoload):# runs the loop over the complete data set changing the get
#variables on the website and displaying new entries
    mainfunction(0+x*100,100)
    x = x + 1

for item in textlinkscontaingxml: #download the xml data from the entries and
#save them to a file for further processing
    try:
        urllib.request.urlretrieve(item, "./data/"+item.split("/")[9])  #download xml files for processing
    except:
        print("XML download file error look in line 49 of code")
        pass

file_list = glob.glob("./data/*") # this is the portion of the code that takes the xml to
#tab delimited format.
datawritefile = open("TabDelimited_"+cik,"w", encoding = 'utf-8') #make a custom file name for the tab delimited format
for datafile in file_list:
    datawritefile.write(str(datafile)+ "\n")
    with open(datafile) as f:
        soup = bs4.BeautifulSoup(f.read(),"html.parser")

    tablelist = []
    for line in soup.findAll("tr"):   #find all tables in xml file
        tablelist.append(line)

    tdlist = []
    cleanedlist = []
    for x in range(11, len(tablelist)): #11 is the start of the data, before that is just title information
        tdlist.append((tablelist[x].findAll("td"))) #find all entries in xml file
    for item in tdlist:
        for item in item:
            cleanedlist.append((str(item).split("<")[1].split(">")[1])) #remove <> to clean up data

    for x in range(0,int(len(cleanedlist)/12)): # formating in a row column format and printing in tab delimited format
        tabout = (cleanedlist[0+(x*12):11+(x*12)])
        stringout = str('\t'.join(map(str,tabout)))
        print(stringout)
        datawritefile.write(stringout + "\n") #include file name in console and file output
    print("\n") #newline for each page entry
    datawritefile.write("\n")


# QUADS IMPROVEMENT
def indices(lst, element):
    result = []
    offset = -1
    while True:
        try:
            offset = lst.index(element, offset+1)
        except ValueError:
            return result
        result.append(offset)

def card_number_list(lst):
    number_list = []
    for i in range(len(lst)):
        if lst[i][1].isnumeric() == True:
            # double digit number
            number = int(lst[i][0:2])
        else:  #if lst[i][1] == ' ':
            # single digit number
            number = int(lst[i][0])
        number_list.append(number)
    return number_list

def card_number(string):  # gets the (integer) number of a single card
    if string[1].isnumeric() == True:
        # double digit number
        number = string[0:2]
    else:  #if lst[i][1] == ' ':
        # single digit number
        number = string[0]
    return int(number)

player_hands2 = {"matt's_hand": {'cards': ['4 C', '4 S', '4 H', '4 D', '6 S', '6 D', '13 S'],
                 'handStr': [False, False, True, False, False, False, False, False, True, ["matt's_hand"]],
                 'high_card': [13, 6, 6, 4, 4]},
 "fox's_hand": {'cards': ['4 S', '4 H', '4 D', '6 S', '6 D', '6 C', '6 H'],
                'handStr': [False, False, True, False, False, False, True, False, False, ["matt's_hand"]],
                'high_card': [6, 6, 6, 6, 4]}}

print(player_hands2)

def Quads():
    for name in player_hands2:
        four_of_a_kind = False
        for num in range(2,15):  # num = 2,3,4,...,14
            if len(indices(card_number_list(player_hands2[name]['cards']), num)) == 4:
                print("4 OF A KIND BABY")
                four_of_a_kind = True

                # obtaining a list of 5 best cards at this moment
                last_quad_card_index = max({0,1,2,3,4,5,6} - set(indices(card_number_list(player_hands2[name]['cards']), num)))  # max index that is not already an index of one of the quad cards
                quad_cards = [player_hands2[name]['cards'][last_quad_card_index]]
                for index in indices(card_number_list(player_hands2[name]['cards']), num):
                    quad_cards.append(player_hands2[name]['cards'][index])
                    # need 1 more card added to the list - got the 4 cards for the 4 of a kind. need the next highest card.
                # sort into ascending order:
                quad_cards = sorted(quad_cards, key = card_number)
                print(quad_cards)

        player_hands2[name]['handStr'].append(four_of_a_kind)
        if four_of_a_kind == True:
            # adding the cards to SameHandStr_dic
            SameHandStr_dic[name]['Quads'] = quad_cards
    return

Quads()


import numpy as np
import os
from tensorforce.agents import PPOAgent, VPGAgent
from tensorforce.execution import Runner

# Load UnityEnvironment and my wrapper
from mlagents.envs import UnityEnvironment
from myEnv import UnityRlEnv
from FreezeGraph import freeze_graph

network_spec = [
    {
        "type": "conv2d",
        "size": 32,
        "window": (3,3),
        "stride": 2,
        "activation": 'relu'
    },
    {
        "type": "conv2d",
        "size": 32,
        "window": (3, 3),
        "stride": 2,
        "activation": 'relu'
    },
    {
        "type": "conv2d",
        "size": 32,
        "window": (3, 3),
        "stride": 2,
        "activation": 'relu'
    },
    {
        "type": "flatten"
    },
    {
        "type": "dense",
        "size": 32,
        "activation": 'relu'
    },
    {
        "type": "dense",
        "size": 32,
        "activation": 'relu'
    }
]

# Create a Proximal Policy Optimization agent
agent = PPOAgent(
    states={
        'shape': (8,8,1),
        'type': 'float'
    },
    actions={
        'type': 'int',
        'num_actions': 8
    },
    network=network_spec,
    # Agent
    states_preprocessing=None,
    reward_preprocessing=None,
    # MemoryModel
    update_mode=dict(
        unit='timesteps',
        # 10 episodes per update
        batch_size=5,
        # Every 10 episodes
        frequency=1
    ),
    memory=dict(
        type='latest',
        include_next_states=False,
        capacity=10000
    ),
    # DistributionModel
    distributions=None,
    entropy_regularization=0.01,
    # PGModel
    # baseline_mode='states',
    # baseline=dict(
    #     type='mlp',
    #     sizes=[32, 32]
    # ),
    # baseline_optimizer=dict(
    #     type='multi_step',
    #     optimizer=dict(
    #         type='adam',
    #         learning_rate=1e-3
    #     ),
    #     num_steps=5
    # ),
    # gae_lambda=0.97,
    # PGLRModel
    likelihood_ratio_clipping=0.2,
    # PPOAgent
    step_optimizer=dict(
        type='adam',
        learning_rate=1e-5
    ),
    subsampling_fraction=0.2,
    optimization_steps=25,
    execution=dict(
        type='single',
        session_config=None,
        distributed_spec=None
    )
)

directory = os.path.join(os.getcwd(), "RL-Experiments/saved/")
modelName = 'model2018-09-28 13:18:33.418464'
agent.restore_model(directory, modelName)

gameName = None

env = UnityRlEnv(UnityEnvironment(gameName, no_graphics=False))

s = env.reset()
while True:
    action = agent.act(s)
    s, done, r = env.execute(action)
    if done:
        env.reset()


""" Contains base Solver class for various PDE problems """
from abc import ABC, abstractmethod
import numpy as np
from fenics import *
from fenics_adjoint import *

class Solver(ABC):
    """ base solver class inherited by all solvers """

    def __init__(self, V, a):
        """ Creates mesh, function space V, and LHS of weak form a """
        self.mesh = V.mesh()
        self.V = V
        self.W = FunctionSpace(self.mesh, 'P', 1)
        self.v2d = vertex_to_dof_map(self.W)
        self.a = a

    @abstractmethod
    def solve(self, theta):
        """ Solve the PDE given parameters theta """

    def forward(self, theta_batch):
        """
        Computes batch solutions to the PDE
            arguments: theta batch array with shape (batch_size, theta_size)
            returns: solns array with shape (batch_size, soln_dim)
        """
        batch_solns = dict()
        self.controls = dict()
        self.solns = dict()

        # singleton batch case
        if theta_batch.ndim == 1:
            theta_batch = np.expand_dims(theta_batch, axis=0)
        batch_size = theta_batch.shape[0]

        # compute batch solutions
        for idx in range(batch_size):
            u, self.controls[idx] = self.solve(theta_batch[idx])
            self.solns[idx] = u
            batch_solns[idx] = u.compute_vertex_values(self.mesh)

        solns_array = np.array([u for u in batch_solns.values()])
        return solns_array

    @abstractmethod
    def solve_adjoint(out, data, controls):
        """ Compute loss J and loss gradients dJ/dtheta """

    def backward(self, Phi_batch):
        """
        Compute batch gradients dJ/dtheta
            argument: data batch array with shape (batch_size, soln_dim)
            returns: loss grads dJ/dtheta with shape (batch_size, theta_size)
        """

        grads = dict()

        # singleton batch case
        if Phi_batch.ndim == 1:
            Phi_batch = np.expand_dims(Phi_batch, axis=0)
        batch_size = Phi_batch.shape[0]

        for idx in range(batch_size):
            grads[idx] = self.solve_adjoint(self.solns[idx],
                                            Phi_batch[idx],
                                            self.controls[idx])

        grads_array = np.array([g for g in grads.values()])
        return grads_array


#!/usr/bin/env python
# Filename: svm.py
import sys
from sklearn import svm
from sklearn.cross_validation import train_test_split
from sklearn.grid_search import GridSearchCV
from sklearn.metrics import classification_report
import numpy as np
import matplotlib.pyplot as plt
import random

def shuffle(*args):
    rng_state = np.random.get_state()
    for arg in args:
        np.random.set_state(rng_state)
        np.random.shuffle(arg)

def load_data():
    lethal = list()
    with open('data/obtained/lethal.txt') as f:
        for line in f:
            lethal.append(line.strip())
    viable = list()
    with open('data/obtained/viable.txt') as f:
        for line in f:
            viable.append(line.strip())
    genes = list()
    genes_ids = dict()
    Y = list()
    g_id = 0
    for i in range(0, len(lethal)):
        genes.append(lethal[i])
        Y.append(1)
        genes_ids[lethal[i]] = g_id
        g_id += 1
    for i in range(0, len(viable)):
        genes.append(viable[i])
        Y.append(0)
        genes_ids[viable[i]] = g_id
        g_id += 1
    go_ids = dict()
    i_id = 0
    with open('data/obtained/gos.txt') as f:
        for line in f:
            line = line.strip()
            go_ids[line] = i_id
            i_id += 1
    X = list()
    gos_len = i_id
    for i in range(len(genes)):
        X.append([0] * gos_len)
    with open('data/obtained/gene_go.txt') as f:
        for line in f:
            line = line.strip()
            line = line.split(' ')
            gene_id = line[0]
            gos = line[1:]
            if gene_id in genes_ids:
                g_id = genes_ids[gene_id]
                for go_id in gos:
                    i_id = go_ids[go_id]
                    X[g_id][i_id] = 1
    return X, Y

def main():
    X, Y = load_data()
    # shuffle(X, Y)
    X_train, X_test, Y_train, Y_test = train_test_split(
         X, Y, test_size=0.2, random_state=0)

    clf = svm.LinearSVC(C=1000, penalty='l1', dual=False, random_state=25)
    clf.fit(X_train, Y_train)

    y_true, y_pred = Y_test, clf.predict(X_test)
    print(classification_report(y_true, y_pred))

    # # Set the parameters by cross-validation
    # tuned_parameters = [{'kernel': ['rbf'], 'gamma': [1e-3, 1e-4, 1e-5],
    #                      'C': [1, 10, 100, 1000]},
    #                     {'kernel': ['linear'], 'C': [1, 10, 100, 1000]}]

    # scores = ['precision', 'recall']

    # for score in scores:

    #     clf = GridSearchCV(svm.SVC(C=1), tuned_parameters, cv=5,
    #                        scoring='%s_weighted' % score,
    #                        fit_params={'sample_weight': weights_train})
    #     clf.fit(X_train, Y_train)

    #     print("Best parameters set found on development set:")
    #     print()
    #     print(clf.best_params_)
    #     print()
    #     print("Grid scores on development set:")
    #     print()
    #     for params, mean_score, scores in clf.grid_scores_:
    #         print("%0.3f (+/-%0.03f) for %r"
    #               % (mean_score, scores.std() * 2, params))
    #     print()

    #     print("Detailed classification report:")
    #     print()
    #     print("The model is trained on the full development set.")
    #     print("The scores are computed on the full evaluation set.")
    #     print()
    #     y_true, y_pred = Y_test, clf.predict(X_test)
    #     print(classification_report(y_true, y_pred))

if __name__ == '__main__':
    main()


#!/usr/bin/env python

###
# Copyright (c) 2002-2007 Systems in Motion
#
# Permission to use, copy, modify, and distribute this coin.Software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE coin.SoFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS coin.SoFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATcoin.SoEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS coin.SoFTWARE.
#

###
# This is an example from the Inventor Mentor,
# chapter 8, example 1.
#
# This example creates and displays a B-Spline curve.
# The curve is order 3 with 7 control points and a knot
# vector of length 10.  One of its knots has multiplicity
# 2 to illustrate a curve with a spike in it.
#

####################################################################
#        Modified to be compatible with  FreeCAD                   #
#                                                                  #
# Author : Mariwan Jalal  mariwan.jalal@gmail.com                  #
####################################################################

import os
import sys
import FreeCAD as App
import FreeCADGui as Gui
import pivy.coin as coin

floorData = """#Inventor V2.0 ascii
Separator {
   SpotLight {
      cutOffAngle 0.9
      dropOffRate 0.2
      location 6 12 2
      direction 0 -1 0
   }
   ShapeHints {
      faceType UNKNOWN_FACE_TYPE
   }
   Texture2Transform {
      #rotation 1.57
      scaleFactor 8 8
   }
   Texture2 {
      filename oak.rgb
   }
   NormalBinding {
        value  PER_PART
   }
   Material { diffuseColor 1 1 1 specularColor 1 1 1 shininess 0.4 }
   DEF FloorPanel Separator {
      DEF FloorStrip Separator {
         DEF FloorBoard Separator {
            Normal { vector 0 1 0 }
            TextureCoordinate2 {
               point [ 0 0, 0.5 0, 0.5 2, 0.5 4, 0.5 6,
                       0.5 8, 0 8, 0 6, 0 4, 0 2 ] }
            Coordinate3 {
               point [ 0 0 0, .5 0 0, .5 0 -2, .5 0 -4, .5 0 -6,
                       .5 0 -8, 0 0 -8, 0 0 -6, 0 0 -4, 0 0 -2, ]
            }
            FaceSet { numVertices 10 }
            BaseColor { rgb 0.3 0.1 0.0 }
            Translation { translation 0.125 0 -0.333 }
            Cylinder { parts TOP radius 0.04167 height 0.002 }
            Translation { translation 0.25 0 0 }
            Cylinder { parts TOP radius 0.04167 height 0.002 }
            Translation { translation 0 0 -7.333 }
            Cylinder { parts TOP radius 0.04167 height 0.002 }
            Translation { translation -0.25 0 0 }
            Cylinder { parts TOP radius 0.04167 height 0.002 }
         }
         Translation { translation 0 0 8.03 }
         USE FloorBoard
         Translation { translation 0 0 8.04 }
         USE FloorBoard
      }
      Translation { translation 0.53 0 -0.87 }
      USE FloorStrip
      Translation { translation 0.53 0 -2.3 }
      USE FloorStrip
      Translation { translation 0.53 0 1.3 }
      USE FloorStrip
      Translation { translation 0.53 0 1.1 }
      USE FloorStrip
      Translation { translation 0.53 0 -0.87 }
      USE FloorStrip
      Translation { translation 0.53 0 1.7 }
      USE FloorStrip
      Translation { translation 0.53 0 -0.5 }
      USE FloorStrip
   }
   Translation { translation 4.24 0 0 }
   USE FloorPanel
   Translation { translation 4.24 0 0 }
   USE FloorPanel
}"""

##############################################################
# CODE FOR The Inventor Mentor STARTS HERE

# The control points for this curve
pts = (
    (4.0, -6.0,  6.0),
    (-4.0,  1.0,  0.0),
    (-1.5,  5.0, -6.0),
    (0.0,  2.0, -2.0),
    (1.5,  5.0, -6.0),
    (4.0,  1.0,  0.0),
    (-4.0, -6.0,  6.0))

# The knot vector
knots = (1, 2, 3, 4, 5, 5, 6, 7, 8, 9)

# Create the nodes needed for the B-Spline curve.

def makeCurve():
    curveSep = coin.SoSeparator()

    # Set the draw style of the curve.
    drawStyle = coin.SoDrawStyle()
    drawStyle.lineWidth = 4
    curveSep.addChild(drawStyle)

    # Define the NURBS curve including the control points
    # and a complexity.
    complexity = coin.SoComplexity()
    controlPts = coin.SoCoordinate3()
    curve = coin.SoNurbsCurve()
    complexity.value = 0.8
    controlPts.point.setValues(0, 7, pts)
    curve.numControlPoints = 7
    curve.knotVector.setValues(0, 10, knots)
    curveSep.addChild(complexity)
    curveSep.addChild(controlPts)
    curveSep.addChild(curve)

    return curveSep

# CODE FOR The Inventor Mentor ENDS HERE
##############################################################

def BScurveExecute():
   # Initialize Inventor and Qt
   root = coin.SoSeparator()
   # Create the scene graph for the heart
   heart = coin.SoSeparator()
   curveSep = makeCurve()
   lmodel = coin.SoLightModel()
   clr = coin.SoBaseColor()
   lmodel.model = coin.SoLightModel.BASE_COLOR
   clr.rgb = (1.0, 0.0, 0.1)
   heart.addChild(lmodel)
   heart.addChild(clr)
   heart.addChild(curveSep)
   root.addChild(heart)
   # Create the scene graph for the floor
   floor = coin.SoSeparator()
   xlate = coin.SoTranslation()
   rot = coin.SoRotation()
   scale = coin.SoScale()
   input = coin.SoInput()
   input.setBuffer(floorData)
   result = coin.SoDB.readAll(input)
   xlate.translation = (-12.0, -5.0, -5.0)
   scale.scaleFactor = (2.0, 1.0, 2.0)
   rot.rotation.setValue(coin.SbRotation(coin.SbVec3f(0.0, 1.0, 0.0), 22/7/2.0))
   floor.addChild(rot)
   floor.addChild(xlate)
   floor.addChild(scale)
   floor.addChild(result)
   root.addChild(floor)
   # Create the scene graph for the heart's shadow
   shadow = coin.SoSeparator()
   shmdl = coin.SoLightModel()
   shmtl = coin.SoMaterial()
   shclr = coin.SoBaseColor()
   shxl = coin.SoTranslation()
   shscl = coin.SoScale()
   shmdl.model = coin.SoLightModel.BASE_COLOR
   shclr.rgb = (0.21, 0.15, 0.09)
   shmtl.transparency = 0.5
   shxl.translation = (0.0, -4.9, 0.0)
   shscl.scaleFactor = (1.0, 0.0, 1.0)
   shadow.addChild(shmtl)
   shadow.addChild(shmdl)
   shadow.addChild(shclr)
   shadow.addChild(shxl)
   shadow.addChild(shscl)
   shadow.addChild(curveSep)
   root.addChild(shadow)

   view = Gui.ActiveDocument.ActiveView
   sg = view.getSceneGraph()
   sg.addChild(root)


import os

from rest_framework import serializers
from backend.faktura.models import *
from backend.faktura.exceptions import *
from backend.faktura.extra.parser import Parser

import ntpath

def getFileType(file):
    _, file_extension = os.path.splitext(file.name)
    return file_extension

# ------------------------
# REGULAR SERIALIZERS
# ------------------------

class ProfileSerializer(serializers.ModelSerializer):
    class Meta:
        model = Profile
        fields = "__all__"

class AnalyseSerializer(serializers.ModelSerializer):
    class Meta:
        model = Analyse
        fields = "__all__"

class AnalysePrisSerializer(serializers.ModelSerializer):
    class Meta:
        model = AnalysePris
        fields = "__all__"

class AnalyseTypeSerializer(serializers.ModelSerializer):
    class Meta:
        model = AnalyseType
        fields = "__all__"

class RekvirentSerializer(serializers.ModelSerializer):
    class Meta:
        model = Rekvirent
        fields = "__all__"

class ParsingSerializer(serializers.ModelSerializer):
    class Meta:
        model = Parsing
        fields = "__all__"

    def validate(self, data):
        """ Check the filetype of the 'parsing' file is allowed types """
        file_ext = getFileType(data['data_fil'])
        if file_ext != '.xlsx':
            raise ParsingFileTypeValidation(
                'Parsing filetype must be .xlsx', 'data_fil', status_code=status.HTTP_400_BAD_REQUEST)

        return data

class FakturaSerializer(serializers.ModelSerializer):
    class Meta:
        model = Faktura
        fields = "__all__"

    def validate(self, data):
        """ Check the filetype of the 'faktura' file is allowed types """
        file_ext = getFileType(data['pdf_fil'])
        if file_ext != '.pdf':
            raise ParsingFileTypeValidation(
                'Faktura filetype must be .pdf', 'pdf_fil', status_code=status.HTTP_400_BAD_REQUEST)

        return data

class FakturaStatusSerializer(serializers.ModelSerializer):
    class Meta:
        model = FakturaStatus
        fields = "__all__"

# ------------------------
# NESTED SERIALIZERS
# ------------------------

class NestedAnalyseTypeSerializer(serializers.ModelSerializer):
    priser = AnalysePrisSerializer(many=True)

    class Meta:
        model = AnalyseType
        fields = "__all__"

class NestedAnalyseSerializer(serializers.ModelSerializer):
    analyse_type = NestedAnalyseTypeSerializer()

    class Meta:
        model = Analyse
        fields = "__all__"

class NestedRekvirentSerializer(serializers.ModelSerializer):
    fakturaer = FakturaSerializer(many=True)

    class Meta:
        model = Rekvirent
        fields = "__all__"

class NestedFakturaSerializer(serializers.ModelSerializer):
    analyser = NestedAnalyseSerializer(many=True)
    rekvirent = RekvirentSerializer()

    class Meta:
        model = Faktura
        fields = "__all__"

    def validate(self, data):
        """ Check the filetype of the 'faktura' file is allowed types """
        file_ext = getFileType(data['pdf_fil'])
        if file_ext != '.pdf':
            raise ParsingFileTypeValidation(
                'Faktura filetype must be .pdf', 'pdf_fil', status_code=status.HTTP_400_BAD_REQUEST)

        return data

class NestedParsingSerializer(serializers.ModelSerializer):
    #oprettet_af = ProfileSerializer()
    fakturaer = NestedFakturaSerializer(many=True, required=False)

    class Meta:
        model = Parsing
        fields = "__all__"

    def validate(self, data):
        """ Check the filetype of the 'parsing' file is allowed types """
        file_ext = getFileType(data['data_fil'])
        if file_ext != '.xlsx':
            raise ParsingFileTypeValidation(
                'Parsing filetype must be .xlsx', 'data_fil', status_code=status.HTTP_400_BAD_REQUEST)

        return data

    def create(self, validated_data):
        try:
            test = validated_data.get('fakturaer')

            if test is not None:
                validated_data.pop('fakturaer')
        except:
            print("No faktura list sent")

        parsing_obj = Parsing.objects.create(**validated_data)

        Parser.parse(parsing_obj)

        return parsing_obj


import sys

class TrackPoint:
		def __init__(self, hurricane, time, nature, latitude, longitude, wind, pressure, center, trackType):
			self.hurricane = hurricane
			self.time = time
			self.nature = nature
			self.latitude = latitude
			self.longitude = longitude
			self.wind = wind
			self.pressure = pressure
			self.center = center
			self.trackType = trackType

class Hurricane:

	def __init__(self, serialNumber, season, num, basin, subBasin, name):
		self.serialNumber = serialNumber
		self.season = season
		self.num = num
		self.basin = basin
		self.subBasin = subBasin
		self.name = name
		self.trackPoints = []

	def addTrackPoint(self, hurricane, time, nature, latitude, longitude, wind, pressure, center, trackType):
		self.trackPoints.append(TrackPoint(hurricane, time, nature, latitude, longitude, wind, pressure, center, trackType))

hurricaneList = []

with open(sys.argv[1],'r') as f:
	tempSerNum = ""
	hurrNum = -1
	for text in f:
		text = text.replace(" ","").split(",")
		if(text[0] != tempSerNum):
			hurrNum += 1
			hurricaneList.append(Hurricane(text[0], int(text[1]), int(text[2]), text[3], text[4], text[5]))
			tempSerNum = text[0]
		hurricaneList[hurrNum].addTrackPoint(hurricaneList[hurrNum], text[6], text[7], float(text[8]), float(text[9]), float(text[10]), float(text[11]), text[12], text[15])

maxWind = 0
minPressure = 2000
for hurr in hurricaneList:
	for point in hurr.trackPoints:
		if(point.wind > maxWind):
			maxWind = point.wind
			maxWindHurr = point.hurricane
		if(point.pressure < minPressure and point.pressure > 0):
			minPressure = point.pressure
			minPressureHurr = point.hurricane
print(maxWindHurr.name + " " + str(maxWindHurr.season) + ": " + str(maxWind) + " " + minPressureHurr.name + " " + str(minPressureHurr.season) + ": " + str(minPressure))


from urllib import request
from html.parser import HTMLParser

class Parser(HTMLParser):
    def __init__(self):
        HTMLParser.__init__(self)
        self.isHeadline = False

    def handle_starttag(self, tag, attrs):
        for attr in attrs:
                if attr[0] == "class" and attr[1] =="media__link":
                    self.isHeadline = True
                    return
        self.isHeadline = False

    def handle_endtag(self, tag):
        self.isHeadline = False

    def handle_data(self, data):
        if self.isHeadline:
            print(" ".join(data.split()).strip(' \\n').replace('\\', ""))
            print("-------------------------------")

parser = Parser()
website = "http://www.bbc.com/"
socket = request.urlopen(website)

html = socket.read()
parser.feed(str(html))
parser.close()


import numpy as np

def numerical_diff(fn, x):
    """
    numerical_differential
    함수 fn과 점 x가 주어졌을 때 x에서의 함수 fn의 미분 값(도함수 값)
    """
    h = 1e-04  # 0.0001 -> 너무 작으면 under flore 발생
    return (fn(x+h) - fn(x-h)) / (2*h)

def f1(x):
    return 0.001 * x**2 + 0.01 * x

def f1_prime(x):
    """ 근사값을 사용하지 않은 함수 f1의 도함수 """
    return 0.002 * x + 0.01

def f2(x):
    """ x =[x0, x1, ...] """
    return np.sum(x**2)   # x0**2 + x1**2 + ...

def _numerical_gradient(fn, x):
    """
    점 x = [x0, x1, x2, ..., xn]에서의 함수 fn(x0, x1, x2, ..., xn)의
    각 편미분(partial differential)값들의 배열을 리턴

    :param fn: 독립변수를 여러개 갖는 함수
    :param x: n차원 array,
    :return: 편미분 값들의 array
    """
    x = x.astype(np.float, copy=False)   # 실수 타입
    gradient = np.zeros_like(x)   # np.zeros(shape = x.shape)과 동일
    h = 1e-04  #0.0001
    for i in range(x.size):
        ith_value = x[i]
        x[i] = ith_value + h
        fh1 = fn(x)                 # x[i]가 아닌 다른 원소들은 상수 취급 -> 상수 미분해봤자 0
        x[i] = ith_value - h
        fh2 = fn(x)
        gradient[i] = (fh1 - fh2) / (2*h)
        x[i] = ith_value
    return gradient

def numerical_gradient(fn, x):
    """
    x = [[], [], ...] -> 2차원 배열
    """
    if x.ndim == 1:
        return _numerical_gradient(fn, x)
    else:
        grads = np.zeros_like(x)
        for i, x_i in enumerate(x):
            grads[i] = _numerical_gradient(fn, x_i)
        return grads

# def f3(x):
#     for i in range(x.size):
#         X.append(x**i+1)
#     return np.array(X)

# def  f4(x):
#     x = x.

if __name__ == '__main__':
    # f1 함수를 사용했을 경우
    estimate = numerical_diff(f1, 3)
    print('근사값:', estimate)    # 0.016000000000043757
    real = f1_prime(3)
    print('실제값:', real)        # 0.016

    # f2 함수를 사용했을 경우  ->  점 (3,4)에서의 편미분(변수 1개를 상수로 취급) 값
    estimate1 = numerical_diff(lambda x: x**2 + 4**2, 3)   # 3에서의 편미분 -> 4는 상수 취급하므로
    print(estimate1)
    estimate2 = numerical_diff(lambda x: x**2 + 3**2, 4)
    print(estimate2)

    gradient = numerical_gradient(f2, np.array([3,4]))
    print(gradient)

    # f3 = x0 + x1**2 + x2**3 함수에서 점 (1,1,1)에서의 각 편미분들의 값
    # df/dx0 = 1, df/dx1 = 2, df/dx2 = 3
    # gradient2 = numerical_gradient(f3, np.array([1,1,1]))
    # print(gradient2)
    #
    # print()
    # a = np.array([1,1,1])
    # print(a)
    # print(a[0])
    # print(a.size)

    # f4 = x0**2 + 2*x0*x1 + x1**2 함수에서 점(1,2)에서의 각 편미분들의 값
    # df/dx0 = 4, df/dx1 = 5


from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.

def upload(request):
    if request.method == 'POST':
        name = (request.FILES['file'].name)
        size = (request.FILES['file'].size)
        bytes = (request.FILES['file'].size)
        if size > 1048576:
            size = "{0:.2f}".format(size/1000000) + " MB"
        else:
            size = "{0:.2f}".format(size/1000) + " KB"
        type = (request.FILES['file'].content_type)
        rows = []
        rows.append('
%s	%s
' % ("File name: ", name))
        rows.append('
%s	%s
' % ("File size: ", size))
        rows.append('
%s	%s
' % ("File size in bytes: ", bytes))
        rows.append('
%s	%s
' % ("File type: ", type))
        table = "
 %s
" % "\n".join(rows)
        return HttpResponse(table)
    else:
        return render(request, 'filetester-main.html')


import numpy
import matplotlib.pyplot as plt

from gmmclassificationgeneralfunctions import gmmValues, computeClassifications,mcol,computeNewCovar,SplitGMM,logpdf_GMM,Estep

# ----  diagonal covariances
def DiagoSigma(gmm):
    gmmDiag = []
    for i in range (len(gmm)):
        Sigma_g = gmm[i][2]
        Sigma_g = Sigma_g * numpy.eye(Sigma_g.shape[0])
        gmmDiag.append((gmm[i][0],gmm[i][1],Sigma_g))
    return gmmDiag

def LBDiagVariance(x,gmm,alpha,iterations,minEigen):
    psi = minEigen
    gmmDiag = [(gmm[0][0],gmm[0][1],computeNewCovar(gmm[0][2],psi))]
    gmmDiag = EMDiagAlgorithm(x,gmmDiag)
    for i in range(iterations):
        gmmDiag = SplitGMM(gmmDiag,alpha)
        gmmDiag = EMDiagAlgorithm(x,gmmDiag)
    return gmmDiag

def DiagMstep(X, S, posterior):
    psi = 0.01
    # M-step: update the model parameters.
    Zg = numpy.sum(posterior, axis=1)  # 3

    Fg = numpy.zeros((X.shape[0], S.shape[0]))  # 4x3
    for g in range(S.shape[0]):
        tempSum = numpy.zeros(X.shape[0])
        for i in range(X.shape[1]):
            tempSum += posterior[g, i] * X[:, i]
        Fg[:, g] = tempSum

    Sg = numpy.zeros((S.shape[0], X.shape[0], X.shape[0]))
    for g in range(S.shape[0]):
        tempSum = numpy.zeros((X.shape[0], X.shape[0]))
        for i in range(X.shape[1]):
            tempSum += posterior[g, i] * numpy.dot(X[:, i].reshape(
                (X.shape[0], 1)), X[:, i].reshape((1, X.shape[0])))
        Sg[g] = tempSum
    # print(Sg)
    mu = Fg / Zg
    prodmu = numpy.zeros((S.shape[0], X.shape[0], X.shape[0]))
    for g in range(S.shape[0]):
        prodmu[g] = numpy.dot(mu[:, g].reshape((X.shape[0], 1)),
                           mu[:, g].reshape((1, X.shape[0])))
    # print(prodmu)
    # print(np.dot(mu, mu.T).reshape((1, mu.shape[0], mu.shape[0]))) NO, it is wrong
    cov = Sg / Zg.reshape((Zg.size, 1, 1)) - prodmu
    # The following two lines of code are used to model the constraints
    for g in range(S.shape[0]):
        cov[g] = cov[g] * numpy.eye(cov[g].shape[0])
        U, s, Vh = numpy.linalg.svd(cov[g])
        s[s < psi] = psi
        cov[g] = numpy.dot(U, mcol(s)*U.T)
    # print(cov)
    w = Zg/numpy.sum(Zg)
    # print(w)
    return (w, mu, cov)

def EMDiagAlgorithm(x,gmm):
    diffLLR =10
    threshold = 1e-6
    flag = True
    while flag :
        (logdens4, s4) = logpdf_GMM(x,gmm)

        oldLLR = numpy.sum(logdens4)/len(logdens4)

        #  E step
        responsabilities = Estep(logdens4, s4)

        # m step

        (w, mu, cov) = DiagMstep(x, s4, responsabilities)

        newGMM = gmmValues(mu,cov,w,gmm)

        (newlogdens4, s4) = logpdf_GMM(x,newGMM)
        newLLR = numpy.sum(newlogdens4)/len(newlogdens4)

        diffLLR = newLLR-oldLLR
        gmm =newGMM
        delta = abs(diffLLR) <  threshold
        if delta:
            flag = False

    finalGMM = newGMM

    return finalGMM

def KFoldValidationDiagGMMCovariance(D,L,alpha,minEigen,gmms):
    K = 8

    N = int(D.shape[1]/K)

    nWrongPrediction = 0
    numpy.random.seed(0)
    indexes = numpy.random.permutation(D.shape[1])
    errorRates=[]

    for i in range(K):

        idxTest = indexes[i*N:(i+1)*N]

        if i > 0:
            idxTrainLeft = indexes[0:i*N]
        elif (i+1) < K:
            idxTrainRight = indexes[(i+1)*N:]

        if i == 0:
            idxTrain = idxTrainRight
        elif i == K-1:
            idxTrain = idxTrainLeft
        else:
            idxTrain = numpy.hstack([idxTrainLeft, idxTrainRight])

        DTR = D[:, idxTrain]
        LTR = L[idxTrain]
        DTE = D[:, idxTest]
        LTE = L[idxTest]

        # separate from classes

        DTR0 =DTR[:,LTR ==0]
        DTR1 = DTR[:,LTR == 1]

        Dtotal = [DTR0,DTR1]
        errors = computeClassifications(gmms,Dtotal,LBDiagVariance,minEigen,alpha,DTE,LTE)
        errorRates.append(errors)
        print(errorRates)
    npErrors = numpy.array(errorRates)

    # emean precision for 1 gmms
    pre1= npErrors[:,0][:,1].sum()/len(npErrors[:,0][:,1])
    # emean precision for 2 gmms
    pre2= npErrors[:,1][:,1].sum()/len(npErrors[:,0][:,1])

    # emean precision for 4 gmms
    pre4= npErrors[:,2][:,1].sum()/len(npErrors[:,0][:,1])

    # emean precision for 8 gmms
    pre8= npErrors[:,3][:,1].sum()/len(npErrors[:,0][:,1])

    # emean precision for 16 gmms
    pre16= npErrors[:,4][:,1].sum()/len(npErrors[:,0][:,1])

    print("error full cov:\n")
    print("gmm1: "+ str(pre1))
    print("gmm2: "+ str(pre2))
    print("gmm4: "+ str(pre4))
    print("gmm8: "+ str(pre8))
    print("gmm16: "+ str(pre16))