import sys
assert sys.version_info >= (3, 5) # make sure we have Python 3.5+
import json
from pyspark.sql import SparkSession, functions, types
from pyspark.sql.functions import col, length

# spark-submit data_join.py output_b output_r output_u output_join

business_schema = types.StructType([
types.StructField('business_star_rating', types.DoubleType()),
types.StructField('business_id', types.StringType()),
types.StructField('categories', types.StringType())
])

review_schema = types.StructType([
types.StructField('review_star_rating', types.DoubleType()),
types.StructField('user_id', types.StringType()),
types.StructField('business_id', types.StringType()),
types.StructField('review_id', types.StringType()),
types.StructField('length_of_review_text', types.IntegerType()),
types.StructField('review_text', types.StringType())
])

user_schema = types.StructType([
types.StructField('review_count', types.IntegerType()),
types.StructField('average_user_rating', types.DoubleType()),
types.StructField('user_id', types.StringType())
])

def main(inp1,inp2,inp3, output):

    df_b = spark.read.csv(inp1, schema=business_schema)
    df_b.printSchema()
    df_b.createOrReplaceTempView('df_b')

    df_r = spark.read.csv(inp2, schema=review_schema)
    df_r.printSchema()
    df_r.createOrReplaceTempView('df_r')

    df_u = spark.read.csv(inp3, schema=user_schema)
    df_u.printSchema()
    df_u.createOrReplaceTempView('df_u')

    joined_data = spark.sql("""SELECT df_r.user_id,df_u.review_count, average_user_rating,business_star_rating, length_of_review_text,categories,\
                            review_star_rating, review_id,df_b.business_id,review_text from df_u inner join df_r on df_r.user_id=df_u.user_id inner join df_b \
                            on df_r.business_id=df_b.business_id""")
    joined_data.write.csv(output, mode='overwrite')

if __name__ == '__main__':
    inp1 = sys.argv[1]
    inp2 = sys.argv[2]
    inp3 = sys.argv[3]
    output = sys.argv[4]
    spark = SparkSession.builder.appName('BD Project').getOrCreate()
    assert spark.version >= '3.0' # make sure we have Spark 3.0+
    spark.sparkContext.setLogLevel('WARN')
    sc = spark.sparkContext
    main(inp1,inp2,inp3,output)


import numpy as np
#correct spi mode is spi.mode = 0b01

class DAC_80508:
    """
    Class for the used Octal DAC 80508 from Ti
    Datasheet: https://www.ti.com/lit/ds/symlink/dac80508.pdf?ts=1619120994235&ref_url=https%253A%252F%252Fwww.ti.com%252Fproduct%252FDAC80508
    Initialises the DAC. Sets Internal Voltage Reference Gain to 2

    Args:
        cs_pin (int): The Output Line of the Multiplexer which is connected to the DAC
        spi (SpiDev spi): The SPI Interface to be used for communication
        muxer (Multiplexer Object): The Multiplexer which controls the CS Routing
    """
    def __init__(self,cs_pin,spi,muxer):
        """Initialises the DAC. Sets Internal Voltage Reference Gain to 2

        Args:
            cs_pin (int): The Output Line of the Multiplexer which is connected to the DAC
            spi (SpiDev spi): The SPI Interface to be used for communication
            muxer (Multiplexer Object): The Multiplexer which controls the CS Routing
        """
        self.cs_pin=cs_pin
        self.muxer=muxer
        self.spi=spi
        self.input_voltage=38.6#V
        self.gain=10
        self.spi_mode=0b01
        self.full_voltage_range=5.1#V
        self.get_device_id()
        self.set_all_gain(True)

    def read(self, reg):
        """Low Level one Register read function.
        Selects MUX Line
        Selects correct SPI Mode
        Gets the Data from the Register like described in the Datasheet

        Args:
            reg (int): The Register which should be read

        Returns:
            int: Value of the 16 bit Register as an Integer
        """
        self.muxer.set_cs(self.cs_pin)
        if self.spi.mode!=self.spi_mode:
            self.spi.mode=self.spi_mode
        self.spi.xfer2([reg|0x80,0x00,0x00])
        self.muxer.cs_high()
        self.muxer.cs_low()
        retval=self.spi.xfer2([reg|0x80,0x00,0x00])
        self.muxer.cs_high()
        if retval[0]!=(reg|0x80):
            raise RuntimeError
            print("Error in Read")
        return (retval[1]<<8)+retval[2]

    def write(self, reg, value):
        """Low Level Register Write Function.
        Selects MUX Line
        Selects correct SPI Mode
        Writes the Value to the specified Register like described in the Datasheet
        Args:
            reg (int): The Register to be written to
            value (int): The Value to be written to the Register
        """
        self.muxer.set_cs(self.cs_pin)
        if self.spi.mode!=self.spi_mode:
            self.spi.mode=self.spi_mode
        self.spi.xfer2([reg,value>>8,value&255])
        self.muxer.cs_high()

    def get_device_id(self):
        """Gets the Device ID of the DAC should always be 517
        """
        val=self.read(0x01)
        msg=val
        if (val>>2)!=517:#"00001000000101":
            print(f"Device ID Register Value {msg} device id {(val>>2):014b} {(val>>2)} does not match 00001000000101 517")
        self.device_id=val>>2
        self.version_id=val&3

    def voltage_to_LSB(self,voltage):
        """Converts the voltage to the nearest corresponding LSB Value.
        The LSB Value is estimated by the gain and full voltage range.

        Args:
            voltage (float): The voltage that should be converted to LSB

        Returns:
            int: LSB Value corresponding to Voltage
        """
        return int(voltage/(self.full_voltage_range*self.gain) *65535)

    def LSB_to_voltage(self,voltage_LSB):
        """Converts the voltage in LSB to a estimated float Voltage in Volts.

        Args:
            voltage_LSB (int): Voltage in LSB to be converted in Volts

        Returns:
            float: Voltage in Volts
        """
        return (voltage_LSB/65535*self.full_voltage_range)*self.gain

    def set_voltage(self, channel, voltage):
        """Sets the DAC channel (0-7) to the desired voltage

        Args:
            channel (int): Channel 0-7 which should be set
            voltage (float): Voltage the channel should be set to

        Returns:
            float: The estimated Voltage which is set
        """
        voltage_lsb=self.voltage_to_LSB(voltage)
        self.write(0x2, (1<<channel)<<8)
        self.write(0x6, voltage_lsb)
        voltage_lsb_ret=self.get_voltage_LSB(channel)
        if voltage_lsb!=voltage_lsb_ret:
            print(f"Could not set Channel {channel} to Voltage {voltage} Return Value of Register {0x8+channel} was {voltage_lsb_ret} tried to set it to {voltage_lsb}")
        return self.LSB_to_voltage(voltage_lsb_ret)

    def get_voltage_LSB(self,channel):
        """Gets the set voltage of a specified channel in LSB

        Args:
            channel (int): Channel of which the voltage Register should be read

        Returns:
            int: Value of Voltage Register of the specified channel
        """
        voltage_lsb_ret=self.read(0x8+channel)
        return self.read(0x8+channel)

    def get_voltage(self,channel):
        """Gets the estimated set voltage of a specified channel in Volts

        Args:
            channel (int): Channel of which the voltage Register should be read and converted

        Returns:
            float: in Volts Converted Value of Voltage Register of the specified channel
        """
        return self.LSB_to_voltage(self.get_voltage_LSB(channel))

    def get_all_voltages(self):
        """Gets the estimated set voltage of all channels in Volts

        Returns:
            list of float: in Volts Converted Values of Voltage Registers of all channels
        """
        voltages=np.zeros(8)
        for channel in range(8):
            voltages[channel]=self.get_voltage(channel)
        return voltages

    def set_all_gain(self,gain_x2=True):
        """Sets the gain to two of all DAC Channels -> Voltage Range between 0-5V using the internal Voltage Reference (2.5V)

        Args:
            gain_x2 (bool, optional): Whether to set the Gain to two or not. Defaults to True.
        """
        if gain_x2:
            self.write(0x04,0b11111111)
        else:
            self.write(0x04,0)


"""
coding_schedule.py
Input: Current weekday, derived from the datetime package within Python
 (called on line 14).
Output: A string that states the present day and declares whether that
day is a programming day or a day off.

"""

import datetime as dt

# variable defined as accurate, present weekday taken from python datetime
todays_day = dt.datetime.weekday(dt.datetime.now())

def convert_to_pronoun(day):
	"""Take input "day" as a datatype integer, and convert to datatype string"""
	if day == 0:
		return "Monday"
	if day == 1:
		return "Tuesday"
	if day == 2:
		return "Wednesday"
	if day == 3:
		return "Thursday"
	if day == 4:
		return "Friday"
	if day == 5:
		return "Saturday"
	if day == 6:
		return "Sunday"

# If/Else statement utilizing boolean variable to determine if present
# weekday is a coding day or not. Returns string.
if todays_day == 0 or 1 or 2 or 3 or 4 or 5:
	print("Today is " + convert_to_pronoun(todays_day) + \
		" and you should program!")
else:
	print("Today is Sunday and you have the day off!")


"""
Encode and Decode Strings
Design an algorithm to encode a list of strings to a string.
The encoded string is then sent over the network and is decoded back to the original list of strings.

Please implement encode and decode

Example
Example1

Input: ["lint","code","love","you"]
Output: ["lint","code","love","you"]
Explanation:
One possible encode method is: "lint:;code:;love:;you"
Example2

Input: ["we", "say", ":", "yes"]
Output: ["we", "say", ":", "yes"]
Explanation:
One possible encode method is: "we:;say:;:::;yes"
"""
"""
    @param: strs: a list of strings
    @return: encodes a list of strings to a single string.
    """
def encode(strs):
    # write your code here
    res =''
    for word in strs:
        res += str(len(word)) + "#" + word
    return res

"""
@param: str: A string
@return: dcodes a single string to a list of strings
"""
def decode(str_):
    # write your code here
    res, i = [], 0
    while i < len(str_):
        j = i
        while str_[j] != "#":
            j += 1
        length = int(str_[i:j])
        print(length)
        res.append(str_[j + 1: j + length + 1])
        i = j + length + 1
    return res
# def decode(str_):
#     res = []
#     i = 0

#     while i < len(str_):
#         j = i
#         while str_[j] != "#":
#             j += 1
#         length = int(str_[i:j])
#         segment = str_[j + 1: j + length + 1]
#         res.append(segment)
#         i = j + length + 1

#     return res

Input = ["we", "say", ":", "yes"]
encoded = encode(Input)
print(encoded)
print(decode(encoded))


# -*- coding: utf-8 -*-
"""
Created on Wed Dec  7 12:02:28 2022

@author: fabic
"""

# Import required libraries:
import os
import sys
import logging as log
import openpyxl
import numpy as np
import time
import datetime
#import scipy.io
import pandas as pd
import shutil
import uuid
import matplotlib.pyplot as plt
from matplotlib.lines import Line2D
import importlib
import getpass
from copy import deepcopy
from tqdm import tqdm
import scipy.stats
from scipy.interpolate import interp1d
from scipy.interpolate import make_interp_spline
import pylab
import pickle

import RECC_Paths # Import path file

log.getLogger('matplotlib.font_manager').disabled = True

#import re
__version__ = str('2.5')
##################################
#    Section 1)  Initialize      #
##################################
# add ODYM module directory to system path
sys.path.insert(0, os.path.join(os.path.join(RECC_Paths.odym_path,'odym'),'modules'))
### 1.1.) Read main script parameters
# Mylog.info('### 1.1 - Read main script parameters')
ProjectSpecs_Name_ConFile = 'RECC_Config.xlsx'
Model_Configfile = openpyxl.load_workbook(os.path.join(RECC_Paths.data_path,ProjectSpecs_Name_ConFile), data_only=True)
ScriptConfig = {'Model Setting': Model_Configfile['Cover'].cell(4,4).value}
Model_Configsheet = Model_Configfile[ScriptConfig['Model Setting']]
#Read debug modus:
DebugCounter = 0
while Model_Configsheet.cell(DebugCounter+1, 3).value != 'Logging_Verbosity':
    DebugCounter += 1
ScriptConfig['Logging_Verbosity'] = Model_Configsheet.cell(DebugCounter+1,4).value # Read loggin verbosity once entry was reached.
# Extract user name from main file
ProjectSpecs_User_Name     = getpass.getuser()

# import packages whose location is now on the system path:
import ODYM_Classes as msc # import the ODYM class file
importlib.reload(msc)
import ODYM_Functions as msf  # import the ODYM function file
importlib.reload(msf)
import dynamic_stock_model as dsm # import the dynamic stock model library
importlib.reload(dsm)

Name_Script        = Model_Configsheet.cell(6,4).value
if Name_Script != 'ODYM_RECC_Main':  # Name of this script must equal the specified name in the Excel config file
    raise AssertionError('Fatal: The name of the current script does not match to the sript name specfied in the project configuration file. Exiting the script.')
# the model will terminate if the name of the script that is run is not identical to the script name specified in the config file.
Name_Scenario            = Model_Configsheet.cell(7,4).value # Regional scope as torso for scenario name
StartTime                = datetime.datetime.now()
TimeString               = str(StartTime.year) + '_' + str(StartTime.month) + '_' + str(StartTime.day) + '__' + str(StartTime.hour) + '_' + str(StartTime.minute) + '_' + str(StartTime.second)
#DateString               = str(StartTime.year) + '_' + str(StartTime.month) + '_' + str(StartTime.day)
ProjectSpecs_Path_Result = os.path.join(RECC_Paths.results_path, Name_Scenario + '__' + TimeString )

if not os.path.exists(ProjectSpecs_Path_Result): # Create model run results directory.
    os.makedirs(ProjectSpecs_Path_Result)
# Initialize logger
if ScriptConfig['Logging_Verbosity'] == 'DEBUG':
    log_verbosity = eval("log.DEBUG")
log_filename = Name_Scenario + '__' + TimeString + '.md'
[Mylog, console_log, file_log] = msf.function_logger(log_filename, ProjectSpecs_Path_Result,
                                                      log_verbosity, log_verbosity)
# log header and general information
Time_Start = time.time()
ScriptConfig['Current_UUID'] = str(uuid.uuid4())
Mylog.info('# Simulation from ' + time.asctime())
Mylog.info('Unique ID of scenario run: ' + ScriptConfig['Current_UUID'])

### 1.2) Read model control parameters
Mylog.info('### 1.2 - Read model control parameters')
#Read control and selection parameters into dictionary
ScriptConfig = msf.ParseModelControl(Model_Configsheet,ScriptConfig)

Mylog.info('Script: ' + Name_Script + '.py')
Mylog.info('Model script version: ' + __version__)
Mylog.info('Model functions version: ' + msf.__version__())
Mylog.info('Model classes version: ' + msc.__version__())
Mylog.info('Current User: ' + ProjectSpecs_User_Name)
Mylog.info('Current Scenario: ' + Name_Scenario)
Mylog.info(ScriptConfig['Description'])
Mylog.debug('----\n')

### 1.3) Organize model output folder and logger
Mylog.info('### 1.3 Organize model output folder and logger')
#Copy Config file and model script into that folder
shutil.copy(os.path.join(RECC_Paths.data_path,ProjectSpecs_Name_ConFile), os.path.join(ProjectSpecs_Path_Result, ProjectSpecs_Name_ConFile))
#shutil.copy(Name_Script + '.py'      , os.path.join(ProjectSpecs_Path_Result, Name_Script + '.py'))

#####################################################
#     Section 2) Read classifications and data      #
#####################################################
Mylog.info('## 2 - Read classification items and define all classifications')
### 2.1) # Read model run config data
Mylog.info('### 2.1 - Read model run config data')
# Note: This part reads the items directly from the Exel master,
# will be replaced by reading them from version-managed csv file.
class_filename       = str(ScriptConfig['Version of master classification']) + '.xlsx'
Classfile            = openpyxl.load_workbook(os.path.join(RECC_Paths.data_path,class_filename), data_only=True)
Classsheet           = Classfile['MAIN_Table']
MasterClassification = msf.ParseClassificationFile_Main(Classsheet,Mylog)

Mylog.info('Read and parse config table, including the model index table, from model config sheet.')
IT_Aspects,IT_Description,IT_Dimension,IT_Classification,IT_Selector,IT_IndexLetter,PL_Names,PL_Description,PL_Version,PL_IndexStructure,PL_IndexMatch,PL_IndexLayer,PrL_Number,PrL_Name,PrL_Comment,PrL_Type,ScriptConfig = msf.ParseConfigFile(Model_Configsheet,ScriptConfig,Mylog)

Mylog.info('Define model classifications and select items for model classifications according to information provided by config file.')
ModelClassification  = {} # Dict of model classifications
for m in range(0,len(IT_Aspects)):
    ModelClassification[IT_Aspects[m]] = deepcopy(MasterClassification[IT_Classification[m]])
    EvalString = msf.EvalItemSelectString(IT_Selector[m],len(ModelClassification[IT_Aspects[m]].Items))
    if EvalString.find(':') > -1: # range of items is taken
        RangeStart = int(EvalString[0:EvalString.find(':')])
        RangeStop  = int(EvalString[EvalString.find(':')+1::])
        ModelClassification[IT_Aspects[m]].Items = ModelClassification[IT_Aspects[m]].Items[RangeStart:RangeStop]
    elif EvalString.find('[') > -1: # selected items are taken
        ModelClassification[IT_Aspects[m]].Items = [ModelClassification[IT_Aspects[m]].Items[i] for i in eval(EvalString)]
    elif EvalString == 'all':
        None
    else:
        Mylog.error('Item select error for aspect ' + IT_Aspects[m] + ' were found in datafile.')
        break

### 2.2) # Define model index table and parameter dictionary
Mylog.info('### 2.2 - Define model index table and parameter dictionary')
Model_Time_Start = int(min(ModelClassification['Time'].Items))
Model_Time_End   = int(max(ModelClassification['Time'].Items))
Model_Duration   = Model_Time_End - Model_Time_Start + 1

Mylog.info('Define index table dataframe.')
IndexTable = pd.DataFrame({'Aspect'        : IT_Aspects,  # 'Time' and 'Element' must be present!
                            'Description'   : IT_Description,
                            'Dimension'     : IT_Dimension,
                            'Classification': [ModelClassification[Aspect] for Aspect in IT_Aspects],
                            'IndexLetter'   : IT_IndexLetter})  # Unique one letter (upper or lower case) indices to be used later for calculations.

# Default indexing of IndexTable, other indices are produced on the fly
IndexTable.set_index('Aspect', inplace=True)

# Add indexSize to IndexTable:
IndexTable['IndexSize'] = pd.Series([len(IndexTable.Classification[i].Items) for i in range(0, len(IndexTable.IndexLetter))],
                                    index=IndexTable.index)

# list of the classifications used for each indexletter
IndexTable_ClassificationNames = [IndexTable.Classification[i].Name for i in range(0, len(IndexTable.IndexLetter))]

# 2.3) Define shortcuts for the most important index sizes:
Nt = len(IndexTable.Classification[IndexTable.index.get_loc('Time')].Items)
Ne = len(IndexTable.Classification[IndexTable.index.get_loc('Element')].Items)
Nc = len(IndexTable.Classification[IndexTable.index.get_loc('Cohort')].Items)
Nr = len(IndexTable.Classification[IndexTable.index.get_loc('Region32')].Items)
Nl = len(IndexTable.Classification[IndexTable.index.get_loc('Region11')].Items)
No = len(IndexTable.Classification[IndexTable.index.get_loc('Region1')].Items)
NG = len(IndexTable.Classification[IndexTable.set_index('IndexLetter').index.get_loc('G')].Items)
Ng = len(IndexTable.Classification[IndexTable.set_index('IndexLetter').index.get_loc('g')].Items)
Np = len(IndexTable.Classification[IndexTable.set_index('IndexLetter').index.get_loc('p')].Items)
NB = len(IndexTable.Classification[IndexTable.set_index('IndexLetter').index.get_loc('B')].Items)
NN = len(IndexTable.Classification[IndexTable.set_index('IndexLetter').index.get_loc('N')].Items) # varies: 24 for region-specific nrb and 4 for aggregated global resolution.
NI = len(IndexTable.Classification[IndexTable.set_index('IndexLetter').index.get_loc('I')].Items)
Na = len(IndexTable.Classification[IndexTable.set_index('IndexLetter').index.get_loc('a')].Items)
Nx = len(IndexTable.Classification[IndexTable.set_index('IndexLetter').index.get_loc('x')].Items)
#NA = len(IndexTable.Classification[IndexTable.set_index('IndexLetter').index.get_loc('A')].Items)
NS = len(IndexTable.Classification[IndexTable.index.get_loc('Scenario')].Items)
NR = len(IndexTable.Classification[IndexTable.index.get_loc('Scenario_RCP')].Items)
Nw = len(IndexTable.Classification[IndexTable.index.get_loc('Waste_Scrap')].Items)
Nm = len(IndexTable.Classification[IndexTable.index.get_loc('Engineering materials')].Items)
NX = len(IndexTable.Classification[IndexTable.index.get_loc('Extensions')].Items)
Nn = len(IndexTable.Classification[IndexTable.index.get_loc('Energy')].Items)
NV = len(IndexTable.Classification[IndexTable.set_index('IndexLetter').index.get_loc('V')].Items)
Ns = len(IndexTable.Classification[IndexTable.set_index('IndexLetter').index.get_loc('s')].Items)
#NT = len(IndexTable.Classification[IndexTable.set_index('IndexLetter').index.get_loc('T')].Items)
NL = len(IndexTable.Classification[IndexTable.set_index('IndexLetter').index.get_loc('L')].Items)
NO = len(IndexTable.Classification[IndexTable.set_index('IndexLetter').index.get_loc('O')].Items)
#IndexTable.loc['t']['Classification'].Items # get classification items

SwitchTime = Nc-Nt+1 # Index of first model year (2016)
# 2.4) Read model data and parameters.
Mylog.info('Read model data and parameters.')

ParFileName = os.path.join(RECC_Paths.data_path,'RECC_ParameterDict_' + ScriptConfig['RegionalScope'] + '.dat')
try: # Load Pickle parameter dict to save processing time
    ParFileObject = open(ParFileName,'rb')
    ParameterDict = pickle.load(ParFileObject)
    ParFileObject.close()
    Mylog.info('Model data and parameters were read from pickled file with pickle file /parameter reading sequence UUID ' + ParameterDict['Checkkey'])
except:
    ParameterDict = {}
    mo_start = 0 # set mo for re-reading a certain parameter
    for mo in range(mo_start,len(PL_Names)):
        #mo = 76 # set mo for re-reading a certain parameter
        #ParPath = os.path.join(os.path.abspath(os.path.join(ProjectSpecs_Path_Main, '.')), 'ODYM_RECC_Database', PL_Version[mo])
        ParPath = os.path.join(RECC_Paths.data_path, PL_Names[mo] + '_' + PL_Version[mo])
        Mylog.info('Reading parameter ' + PL_Names[mo])
        #MetaData, Values = msf.ReadParameter(ParPath = ParPath,ThisPar = PL_Names[mo], ThisParIx = PL_IndexStructure[mo], IndexMatch = PL_IndexMatch[mo], ThisParLayerSel = PL_IndexLayer[mo], MasterClassification,IndexTable,IndexTable_ClassificationNames,ScriptConfig,Mylog) # Do not change order of parameters handed over to function!
        # Do not change order of parameters handed over to function!
        MetaData, Values = msf.ReadParameterXLSX(ParPath, PL_Names[mo], PL_IndexStructure[mo], PL_IndexMatch[mo],
                                              PL_IndexLayer[mo], MasterClassification, IndexTable,
                                              IndexTable_ClassificationNames, ScriptConfig, Mylog, False)
        ParameterDict[PL_Names[mo]] = msc.Parameter(Name=MetaData['Dataset_Name'], ID=MetaData['Dataset_ID'],
                                                    UUID=MetaData['Dataset_UUID'], P_Res=None, MetaData=MetaData,
                                                    Indices=PL_IndexStructure[mo], Values=Values, Uncert=None,
                                                    Unit=MetaData['Dataset_Unit'])
        Mylog.info('Current parameter file UUID: ' + MetaData['Dataset_UUID'])
        Mylog.info('_')
    Mylog.info('Reading of parameters finished.')
    CheckKey = str(uuid.uuid4()) # generate UUID for this parameter reading sequence.
    Mylog.info('Current parameter reading sequence UUID: ' + CheckKey)
    Mylog.info('Entire parameter set stored under this UUID, will be reloaded for future calculations.')
    ParameterDict['Checkkey'] = CheckKey
    # Save to pickle file for next model run
    ParFileObject = open(ParFileName,'wb')
    pickle.dump(ParameterDict,ParFileObject)
    ParFileObject.close()

Mylog.info('_')
Mylog.info('_')

RECC_System = msc.MFAsystem(Name='RECC_SingleScenario',
                            Geogr_Scope='19 regions + 1 single country', #IndexTableR.Classification[IndexTableR.set_index('IndexLetter').index.get_loc('r')].Items,
                            Unit='Mt',
                            ProcessList=[],
                            FlowDict={},
                            StockDict={},
                            ParameterDict=ParameterDict,
                            Time_Start=Model_Time_Start,
                            Time_End=Model_Time_End,
                            IndexTable=IndexTable,
                            Elements=IndexTable.loc['Element'].Classification.Items,
                            Graphical=None)

#### Start computations of dynamic impact factors ###
ElecIndex        = IndexTable.Classification[IndexTable.index.get_loc('Energy')].Items.index('electricity')

# impact of electricity generation for diagnostic
impact_elect =  np.einsum('I,Ix->x',
                    RECC_System.ParameterDict['3_SHA_ElectricityMix_World'].Values[0,0,:,0],              # MJ industry/MJ el
                    RECC_System.ParameterDict['4_PE_ProcessExtensions_Industry'].Values[:,:,0,0]/3.6,     # impact/MJ industry
                     )  # Impact/MJ el
impact_elect_alu =  np.einsum('I,Ix->x',
                    RECC_System.ParameterDict['3_SHA_ElectricityMix_World_Alu'].Values[0,0,:,0],              # MJ industry/MJ el
                    RECC_System.ParameterDict['4_PE_ProcessExtensions_Industry'].Values[:,:,0,0]/3.6,     # impact/MJ industry
                     )  # Impact/MJ el

# reshape electricity mix: oRit->mi
Par_ElectricityMix_2015  = np.einsum('I,m->mI', RECC_System.ParameterDict['3_SHA_ElectricityMix_World'].Values[0,0,:,0], np.ones(Nm) )
# overwright aluminium energy mix
for mm in [4,5]:
    Par_ElectricityMix_2015[mm,:] = RECC_System.ParameterDict['3_SHA_ElectricityMix_World_Alu'].Values[0,0,:,0]

# Fuel contributions
fuel_production = np.einsum('nx,n,Pn->Px',
                     RECC_System.ParameterDict['4_PE_ProcessExtensions_EnergyCarriers'].Values[:,:,0,0], # impact/kg fuel
                     RECC_System.ParameterDict['3_EI_SpecificEnergy_EnergyCarriers'].Values[:,0],   # kg fuel/MJ fuel
                     RECC_System.ParameterDict['4_EI_ProcessEnergyIntensity'].Values[:,:,0,0,0],    # MJ fuel/kg mat
                     )  # Impact/kg mat

# Direct contributions
direct_impact = np.einsum('xn,Pn->Px',
                     RECC_System.ParameterDict['6_PR_DirectEmissions'].Values[:,:],                 # impact/MJ fuel
                     RECC_System.ParameterDict['4_EI_ProcessEnergyIntensity'].Values[:,:,0,0,0],    # MJ fuel/kg mat
                     )  # Impact/kg mat

# Electricity generation
elec_production = np.einsum('P,PI,Ix->Px',
                    RECC_System.ParameterDict['4_EI_ProcessEnergyIntensity'].Values[:,ElecIndex,0,0,0],    # MJ el/kg mat
                    Par_ElectricityMix_2015,              # MJ industry/MJ el
                    RECC_System.ParameterDict['4_PE_ProcessExtensions_Industry'].Values[:,:,0,0]/3.6,          # impact/MJ industry
                     )  # Impact/kg mat
# compute residuals
residuals = RECC_System.ParameterDict['4_PE_ProcessExtensions_Materials'].Values[:,:,0,0] - fuel_production - direct_impact - elec_production   # impact / kg mat

# define new parameter with residual only. This parameter replaces the old 4_PE_ProcessExtension
# msf.Parameter

# later, to get the total impact, sum contribution of:
    # residual (constant over time)
    # +fuel: material_demand*4_EI_ProcessEnergyIntensity*4_PE_ProcessExtensions_EnegryCarriers. ProcessEnergyIntensity in scenario dependent, 4_PE_ProcessExtensions_EnegryCarriers is constanst over time
    # +direct: material_demand*4_EI_ProcessEnergyIntensity*6_PR_DIrect emissions. ProcessEnergyIntensity in scenario dependent, &_DIrectEmissions is constant over time
    # +material_demand*4_EI_ProcessEnergyIntensity[electricity]*3_SHA_ElectricityMix*6_PE_ProcessExtensions_Industry. 3_SHA_ElectricityMix is RCP dependent, 6_PE_ProcessExtensions_Industry is constant over time

##  VISUALIZATION for DIAGNOSTOCS
labels = IndexTable.Classification[IndexTable.index.get_loc('Engineering materials')].Items
width = 0.35
fig, ax = plt.subplots()

i=1   # 0:gwp, 1:land, 2:water, 3:material
ax.bar(labels, fuel_production[:,i], width, label='fuel')
ax.bar(labels, direct_impact[:,i], width,  bottom=fuel_production[:,i], label='direct')
ax.bar(labels, elec_production[:,i], width,  bottom=fuel_production[:,i]+direct_impact[:,i], label='elec')
ax.bar(labels, residuals[:,i], width,  bottom=fuel_production[:,i]+direct_impact[:,i]+elec_production[:,i], label='residual')
ax.set_ylabel('contribution')

ax.set_title('Contributions')
plt.xticks(rotation=90)
ax.legend()
plt.show()

## percentages
tot = fuel_production + direct_impact + elec_production + residuals

prcg_fuel   = np.divide(fuel_production, tot, out=np.zeros_like(fuel_production), where=tot!=0)
prcg_direct = np.divide(direct_impact,   tot, out=np.zeros_like(direct_impact),   where=tot!=0)
prcg_elec   = np.divide(elec_production, tot, out=np.zeros_like(elec_production), where=tot!=0)
prcg_res    = np.divide(residuals,       tot, out=np.zeros_like(residuals),       where=tot!=0)
fig, ax = plt.subplots()
ax.bar(labels, prcg_fuel[:,i],   width, label='fuel')
ax.bar(labels, prcg_direct[:,i], width, bottom=prcg_fuel[:,i], label='direct')
ax.bar(labels, prcg_elec[:,i],   width, bottom=prcg_fuel[:,i] + prcg_direct[:,i], label='elec')
ax.bar(labels, prcg_res[:,i],    width, bottom=prcg_fuel[:,i] + prcg_direct[:,i] + prcg_elec[:,i], label='residual')
ax.set_ylabel('contribution')

ax.set_title('Contributions')
plt.xticks(rotation=90)
ax.legend()
plt.show()

print('Done!')


class Settings:
    """游戏的设置类"""

    def __init__(self):
        """初始化游戏设置"""
        self.screen_width = 1200
        self.screen_height = 800
        self.bg_color = (230, 230, 230)
        # 飞船的移动速度
        self.ship_speed = 1.5
        # 子弹的设置
        self.bullet_speed = 1.5
        self.ship_limit = 3
        self.bullet_width = 3
        self.bullet_height = 15
        self.bullet_color = (60, 60, 60)
        # 子弹的最大的数量
        self.bullets_allowed = 5

        # 外星人设置
        self.alien_speed = 1.0
        self.fleet_drop_speed = 10
        """这个参数等于1表示右边移动,等于-1表示左边"""
        self.fleet_direction = 1

        # 加快游戏的节奏
        self.speedup_scale = 1.1
        #外星人分数提高速度
        self.sccore_scale=1.5

        self.initialize_dynamic_settings()

    def initialize_dynamic_settings(self):
        """初始化随游戏进行而变化的设置"""
        self.ship_speed = 1.5
        self.bullet_speed = 3.0
        self.alien_speed = 1.0

        self.fleet_direction = 1
        """击杀得分"""
        self.alien_points = 50

    def increase_speed(self):
        self.ship_speed *= self.speedup_scale
        self.bullet_speed *= self.speedup_scale
        self.alien_speed *= self.speedup_scale
        self.alien_points=int(self.alien_points*self.sccore_scale)


from robosuite.devices import SpaceMouse
import time
import Pyro4
from rlkit.launchers import config

Pyro4.config.SERIALIZERS_ACCEPTED = set(['pickle','json', 'marshal', 'serpent'])
Pyro4.config.SERIALIZER='pickle'

nameserver = Pyro4.locateNS(host=config.SPACEMOUSE_HOSTNAME)
uri = nameserver.lookup("example.greeting")
device_state = Pyro4.Proxy(uri)
device = SpaceMouse()
while True:
    state = device.get_controller_state()
    print(state)
    time.sleep(0.1)
    device_state.set_state(state)


from IPython.display import SVG
from bs4 import BeautifulSoup
import os
from pandas import DataFrame
from pandas import ExcelFile

import numpy

datadir = "./Sketch_svg"

files = os.listdir(datadir)
categories=[]

for i in files:
    categories.append(i)

print(categories)
num_classes=len(categories)

map_svg = None
directory = "Sketch_svg/"

for c in range(240):
    file = categories[c]
    svg_file = directory + file

    with open(svg_file, 'r') as f:
        map_svg = f.read()

    soup = BeautifulSoup(map_svg)
    # paths = soup.select('path[id]')

##print(paths)
    #
    # colors = ['#6e36c9', '#e61710', '#f4f716', '#DF65B9', '#2ae615', '#2547cf']
    #
    # i=0
    #
    # for p in paths:
    #     i+=1
    #     if(i>5):
    #         i=0
    #     p['stroke'] = colors[i]
    #     print(p)
    # new_svg = soup.prettify()
    # SVG(new_svg)
    #
    # new_svg.__sizeof__()
    #
    # print(new_svg)
    #
    # file_svg= "Sketch_svg/"
    # files_name = file_svg + file
    # with open(files_name, 'w') as f:
    #     f.write(new_svg)

    from cairosvg import svg2png
    s=os.path.splitext(svg_file)
    s= os.path.split(s[0])
    file_png = ".png"
    files_ = s[1]+file_png
    files_ = "Sketch_line/" + files_
    svg2png(url=svg_file, write_to=(files_), output_height=1300, output_width=1300)


# LSGI3315 Lab 3 20016345D - Example 3: Calculate Spherical distance using geopy.great_circle
import geopy.distance as geod  # from <module> import <function>

def cal_distance(point1, point2):  # Define a Function of computing sphere distance from two points
    dis = geod.great_circle(point1, point2).meters  # using geopy.great_circle to compute point(latitude, longitude)
    return dis  # Return the spherical distance between two points

if __name__ == '__main__':
    HK_International_Airport_lon = 113.918480  # The longitude of the Hong Kong International Airport
    HK_International_Airport_lat = 22.308046   # The latitude of Hong Kong Disneyland
    HK_Disney_Land_lon = 114.041931  # The longitude of the Hong Kong International Airport
    HK_Disney_Land_lat = 22.312771   # The latitude of Hong Kong Disneyland
    point_begin = (HK_International_Airport_lat, HK_International_Airport_lon)  # The point begins at (long,lat)
    point_end = (HK_Disney_Land_lat, HK_Disney_Land_lon)  # The point ends at (long,lat)
    dist = cal_distance(point_begin, point_end)  # Call the function of "cal_distance"
    print('The distance between the Hong Kong International Airport and the Hong Kong Disneyland is %f meters' % dist)


import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns

T = 500
dt = 0.1
W0 = 0
np.random.seed(100)

dW = (dt ** 0.5) * np.random.normal(size=int(T / dt))
W = np.insert(np.cumsum(dW), 0, W0)
X = 1 / (1 - W)

hitting_time = np.where(np.abs(W - 1) <= 0.005)[0][0] / int(T / dt) * T
plt.axvline(x=hitting_time, linestyle='--',
            label='hitting time', color='k', linewidth=0.75)
plt.plot(np.linspace(0, T, len(X)), X)
plt.xlabel('t')
plt.legend()

def hitting(maxT):
    size = int(maxT / dt)
    dW = (dt ** 0.5) * np.random.normal(size=size)
    W = np.insert(np.cumsum(dW), 0, W0)
    try:
        time = np.where(np.abs(W - 1) <= 0.005)[0][0] / size * maxT
    except:
        time = maxT
    return time

trials = 1000
for T in [1000, 5000, 10000]:
    series = [hitting(T) for i in range(trials)]
    plt.figure()
    sns.distplot(series)
    est = sum(series) / trials
    print(est)
plt.show()


#!/usr/bin/env python3
""" Basic Flask app """""
from flask import Flask, render_template, request, g
from flask_babel import Babel

app = Flask(__name__)
babel = Babel(app)

class Config:
    """ Babel configuration """
    LANGUAGES = ["en", "fr"]
    BABEL_DEFAULT_LOCALE = "en"
    BABEL_DEFAULT_TIMEZONE = "UTC"

users = {
    1: {"name": "Balou", "locale": "fr", "timezone": "Europe/Paris"},
    2: {"name": "Beyonce", "locale": "en", "timezone": "US/Central"},
    3: {"name": "Spock", "locale": "kg", "timezone": "Vulcan"},
    4: {"name": "Teletubby", "locale": None, "timezone": "Europe/London"},
}

def get_user():
    """ returns a user dictionary or None
    if the ID cannot be found or if login_as was not passed """
    try:
        userId = request.args.get('login_as')
        return users[int(userId)]
    except Exception:
        return None

@app.before_request
def before_request():
    """Before request"""
    g.user = get_user()

@babel.localeselector
def get_locale():
    """ to determine the best match with our supported languages """
    localLang = request.args.get('locale')
    supportLang = app.config['LANGUAGES']
    if localLang in supportLang:
        return localLang
    userId = request.args.get('login_as')
    if userId:
        localLang = users[int(userId)]['locale']
        if localLang in supportLang:
            return localLang
    localLang = request.headers.get('locale')
    if localLang in supportLang:
        return localLang
    return request.accept_languages.best_match(app.config['LANGUAGES'])

app.config.from_object(Config)

@app.route('/')
def index():
    """ Basic Flask app"""
    return render_template('6-index.html')

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


import os
import math
import torch
import torch.nn as nn
import traceback

from .adabound import AdaBound
from .audio import Audio
from .evaluation import validate
from model.model import VoiceFilter
from model.embedder import SpeechEmbedder

def train(args, pt_dir, chkpt_path, trainloader, testloader, writer, logger, hp, hp_str):
    # load embedder
    embedder_pt = torch.load(args.embedder_path)
    embedder = SpeechEmbedder(hp).cuda()
    embedder.load_state_dict(embedder_pt)
    embedder.eval()

    audio = Audio(hp)
    model = VoiceFilter(hp).cuda()
    if hp.train.optimizer == 'adabound':
        optimizer = AdaBound(model.parameters(),
                             lr=hp.train.adabound.initial,
                             final_lr=hp.train.adabound.final)
    elif hp.train.optimizer == 'adam':
        optimizer = torch.optim.Adam(model.parameters(),
                                     lr=hp.train.adam)
    else:
        raise Exception("%s optimizer not supported" % hp.train.optimizer)

    step = 0

    if chkpt_path is not None:
        logger.info("Resuming from checkpoint: %s" % chkpt_path)
        checkpoint = torch.load(chkpt_path)
        model.load_state_dict(checkpoint['model'])
        optimizer.load_state_dict(checkpoint['optimizer'])
        step = checkpoint['step']

        # will use new given hparams.
        if hp_str != checkpoint['hp_str']:
            logger.warning("New hparams is different from checkpoint.")
    else:
        logger.info("Starting new training run")

    try:
        criterion = nn.MSELoss()
        while True:
            model.train()
            for dvec_mels, target_mag, mixed_mag in trainloader:
                target_mag = target_mag.cuda()
                mixed_mag = mixed_mag.cuda()

                dvec_list = list()
                for mel in dvec_mels:
                    mel = mel.cuda()
                    dvec = embedder(mel)
                    dvec_list.append(dvec)
                dvec = torch.stack(dvec_list, dim=0)
                dvec = dvec.detach()

                mask = model(mixed_mag, dvec)
                output = mixed_mag * mask

                # output = torch.pow(torch.clamp(output, min=0.0), hp.audio.power)
                # target_mag = torch.pow(torch.clamp(target_mag, min=0.0), hp.audio.power)
                loss = criterion(output, target_mag)

                optimizer.zero_grad()
                loss.backward()
                optimizer.step()
                step += 1

                loss = loss.item()
                if loss > 1e8 or math.isnan(loss):
                    logger.error("Loss exploded to %.02f at step %d!" % (loss, step))
                    raise Exception("Loss exploded")

                # write loss to tensorboard
                if step % hp.train.summary_interval == 0:
                    writer.log_training(loss, step)
                    logger.info("Wrote summary at step %d" % step)

                # 1. save checkpoint file to resume training
                # 2. evaluate and save sample to tensorboard
                if step % hp.train.checkpoint_interval == 0:
                    save_path = os.path.join(pt_dir, 'chkpt_%d.pt' % step)
                    torch.save({
                        'model': model.state_dict(),
                        'optimizer': optimizer.state_dict(),
                        'step': step,
                        'hp_str': hp_str,
                    }, save_path)
                    logger.info("Saved checkpoint to: %s" % save_path)
                    validate(audio, model, embedder, testloader, writer, step)
    except Exception as e:
        logger.info("Exiting due to exception: %s" % e)
        traceback.print_exc()


#_*_coding:utf-8_*_
from optparse import OptionParser
from optparse import OptionGroup

def my_opt():
    usage = "Usage: %prog -H h1 -g g1 --cmd 'df -TH'"
    parser = OptionParser(usage,version='%prog 1.0')
    parser.add_option('-H','--host',  action='store',help='参数后面接配置文件中hostname')
    parser.add_option('-c', '--cmd', action='store', help='参数后面接配置文件中系统命令')
    parser.add_option('-g', '--group', action='store', help='参数后面接配置文件中groupname')
    # group = OptionGroup(parser,'Group Options', '对组内用户起作用')
    # group.add_option('-g','--group',action='store_true',help='参数后面接配置文件中groupname')
    # parser.add_option_group(group)
    (options,args) = parser.parse_args()
    # print(options)
    # print(args)
    return options


# -*- coding: utf-8 -*-
import os
import sys
import re
from flask import Flask, render_template, send_from_directory, flash, request, redirect, session, abort
from shutil import rmtree, copyfile, copytree
from unicodedata import normalize

# 复制 werkzeug.utils import secure_filename
# 支持中文
def my_secure_filename(filename):
    text_type = str
    PY2 = sys.version_info[0] == 2
    _filename_ascii_strip_re = re.compile(r'[^A-Za-z0-9\u4e00-\u9fa5_.-]')
    _windows_device_files = ('CON', 'AUX', 'COM1', 'COM2', 'COM3', 'COM4', 'LPT1',
                             'LPT2', 'LPT3', 'PRN', 'NUL')

    if isinstance(filename, text_type):
        filename = normalize('NFKD', filename).encode('utf-8', 'ignore')
        if not PY2:
            filename = filename.decode('utf-8')
    for sep in os.path.sep, os.path.altsep:
        if sep:
            filename = filename.replace(sep, ' ')
    filename = str(_filename_ascii_strip_re.sub('', '_'.join(
        filename.split()))).strip('._')

    if os.name == 'nt' and filename and \
                    filename.split('.')[0].upper() in _windows_device_files:
        filename = '_' + filename

    return filename

def dir_list(path):
    fileList = []
    dirList = []
    for x in os.listdir(path):
        if os.path.isfile(os.path.join(path, x)):
            fileList.append(x)
        else:
            dirList.append(x)

    return fileList, dirList

def history(currentPath):
    isPre = session.get('isPre', False)
    isNext = session.get('isNext', False)

    if not session.get('pathHistory', False):
        session['pathHistory'] = []
        session['pathHistory'].append(currentPath)
        session['p'] = len(session['pathHistory']) - 1
    elif currentPath != session['pathHistory'][-1] and not isPre and not isNext:
        if len(session['pathHistory']) - 1 != session['p']:
            for key in session['pathHistory'][session['p'] + 1:]:
                session['pathHistory'].remove(key)
        session['pathHistory'].append(currentPath)
        session['p'] = len(session['pathHistory']) - 1

    session.pop('isPre', False)
    session.pop('isNext', False)

SECRET_KEY = 'SECRET_KEY'
CURRENT_DIR = os.getcwd()

app = Flask(__name__)

app.config.from_object(__name__)

# 懒得弄logo, 直接返回404
@app.route('/favicon.ico', methods=['GET'])
def favicon_ico():
    abort(404)

# 跳转到/ftp
@app.route('/', methods=['GET'])
def home_of_no_variable_get():
    return redirect('/ftp')

@app.route('/ftp/', methods=['GET'])
@app.route('/ftp/<path:path>', methods=['GET'])
def ftp_get(path = ''):
    currentPath = path
    path = os.path.join(CURRENT_DIR, currentPath)

    isUpload = session.get('isUpload', False)
    session.pop('isUpload', False)

    isSearchFile = session.get('isSearchFile', False)
    session.pop('isSearchFile', False)

    isSearchText = session.get('isSearchText', False)
    session.pop('isSearchText', False)

    isCreateDir = session.get('isCreateDir', False)
    session.pop('isCreateDir', False)

    isCreateFile = session.get('isCreateFile', False)
    session.pop('isCreateFile', False)

    isCopy = session.get('isCopy', False)

    isMov = session.get('isMov', False)

    searchFileRes = session.get('searchFileRes', False)
    session.pop('searchFileRes', False)

    searchTextRes = session.get('searchTextRes', False)
    session.pop('searchTextRes', False)

    if os.path.isfile(path):
        format = os.path.splitext(path)[1]

        if format in ['.png', '.jpg', '.jpeg', '.gif']:
            with open(path, 'rb') as f:
                res = app.make_response(f.read())
            res.headers['Content-Type'] = 'image'
            return res

        if format in ['.mp4']:
            with open(path, 'rb') as f:
                res = app.make_response(f.read())
            res.headers['Content-Type'] = 'video'
            return res

        if format in ['.mp3']:
            with open(path, 'rb') as f:
                res = app.make_response(f.read())
            res.headers['Content-Type'] = 'audio'
            return res

        if format in ['.txt', '.xml', '.py', '.html']:
            with open(path, 'rb') as f:
                res = app.make_response(f.read())
            res.headers['Content-Type'] = 'text/plain'
            return res

        if format in ['.pdf']:
            with open(path, 'rb') as f:
                res = app.make_response(f.read())
            res.headers['Content-Type'] = 'application/pdf'
            return res

        return send_from_directory(os.path.split(path)[0],
                                   os.path.split(path)[1],
                                   as_attachment=True)

    fileList, dirList = dir_list(path)

    history(currentPath)

    session['currentPath'] = currentPath

    if currentPath:
        path = currentPath + '/'
    else:
        path = currentPath

    return render_template('base.html',
                           title=currentPath,
                           flashPath=currentPath,
                           isUpload=isUpload,
                           isSearchFile=isSearchFile,
                           isSearchText=isSearchText,
                           isCreateDir=isCreateDir,
                           isCreateFile=isCreateFile,
                           searchFileRes=searchFileRes,
                           searchTextRes=searchTextRes,
                           path=path,
                           fileList=fileList,
                           dirList=dirList,
                           isCopy=isCopy,
                           isMov=isMov)

@app.route('/upload', methods=['GET'])
def upload_get():
    currentPath = session['currentPath']
    session['isUpload'] = True
    return redirect('/ftp/%s' % currentPath)

@app.route('/upload', methods=['POST'])
def upload_post():
    currentPath = session['currentPath']
    session.pop('isUpload', False)

    file = request.files['file']
    filename = file.filename
    if file:
        filename = my_secure_filename(filename)
        file.save(os.path.join(currentPath, filename))
        flash('%s 上传成功!' % filename)
    else:
        flash('%s 上传失败!' % filename)

    return redirect('/ftp/%s' % currentPath)

@app.route('/upPath', methods=['GET'])
def upPath_get():
    currentPath = session['currentPath']

    upPath = currentPath.split('/')
    upPath.pop(-1)
    upPath = '/'.join(upPath)

    return redirect('/ftp/%s' % upPath)

@app.route('/prePath', methods=['GET'])
def prePath_get():
    if session['p'] > 0:
        session['p'] -= 1

    prePath = session['pathHistory'][session['p']]

    session['isPre'] = True

    return redirect('/ftp/%s' % prePath)

@app.route('/nextPath', methods=['GET'])
def nextPath_get():
    if session['p'] < len(session['pathHistory']) - 1:
        session['p'] += 1

    nextPath = session['pathHistory'][session['p']]

    session['isNext'] = True

    return redirect('/ftp/%s' % nextPath)

@app.route('/searchFile', methods=['GET'])
def search_file_get():
    currentPath = session['currentPath']
    session['isSearchFile'] = True
    return redirect('/ftp/%s' % currentPath)

@app.route('/searchFile', methods=['POST'])
def search_file_post():
    currentPath = session['currentPath']
    session.pop('isSearchFile', False)

    if not currentPath:
        walkPath = '.'
    else:
        walkPath = currentPath

    session['searchFileRes'] = []
    searchText = request.form['search_text']
    for dirpath, dirnames, filenames in os.walk(walkPath):
        if searchText in dirpath:
            session['searchFileRes'].append(dirpath)
        for filename in filenames:
            if searchText in filename:
                filepath = os.path.join(dirpath, filename)
                session['searchFileRes'].append(filepath)

    return redirect('/ftp/%s' % currentPath)

@app.route('/searchText', methods=['GET'])
def search_text_get():
    currentPath = session['currentPath']
    session['isSearchText'] = True
    return redirect('/ftp/%s' % currentPath)

@app.route('/searchText', methods=['POST'])
def search_test_post():
    currentPath = session['currentPath']
    session.pop('isSearchText', False)

    if not currentPath:
        walkPath = '.'
    else:
        walkPath = currentPath

    session['searchTextRes'] = []
    searchText = request.form['search_text']
    for dirpath, dirnames, filenames in os.walk(walkPath):
        for filename in filenames:
            filepath = os.path.join(dirpath, filename)
            try:
                with open(filepath, 'r') as f:
                    if searchText in f.read():
                        session['searchTextRes'].append(filepath)
            except:
                pass

    return redirect('/ftp/%s' % currentPath)

@app.route('/createDir', methods=['GET'])
def create_dir_get():
    currentPath = session['currentPath']
    session['isCreateDir'] = True
    return redirect('/ftp/%s' % currentPath)

@app.route('/createDir', methods=['POST'])
def create_dir_post():
    currentPath = session['currentPath']
    session.pop('isCreateDir', False)

    dirname = my_secure_filename(request.form['dirname'])
    path = os.path.join(currentPath, dirname)

    try:
        os.mkdir(path)
    except:
        flash("文件夹已存在或权限不足")

    return redirect('/ftp/%s' % currentPath)

@app.route('/createFile', methods=['Get'])
def create_file_get():
    currentPath = session['currentPath']
    session['isCreateFile'] = True
    return redirect('/ftp/%s' % currentPath)

@app.route('/createFile', methods=['POST'])
def create_file_post():
    currentPath = session['currentPath']
    session.pop('isCreateFile', False)

    filetext = request.form['filetext']
    filename = my_secure_filename(request.form['filename'])
    if filename:
        path = os.path.join(currentPath, filename)
        if not os.path.exists(path):
            with open(path, 'w') as f:
                f.write(filetext)
        else:
            flash("文件%s已存在" % path)
    else:
        flash('请输入文件名!')

    return redirect('/ftp/%s' % currentPath)

@app.route('/copy', methods=['POST'])
def copy_post():
    currentPath = session['currentPath']

    choice = dict(request.form)
    choice.pop('copy')
    choice = list(choice)

    session['filePrePath'] = session['currentPath']
    session['fileList'] = choice
    session['isCopy'] = True

    session.pop('isMov', False)

    return redirect('/ftp/%s' % currentPath)

@app.route('/mov', methods=['POST'])
def mov_post():
    currentPath = session['currentPath']

    choice = dict(request.form)
    choice.pop('mov')
    choice = list(choice)

    session['filePrePath'] = session['currentPath']
    session['fileList'] = choice
    session['isMov'] = True

    session.pop('isCopy', False)

    return redirect('/ftp/%s' % currentPath)

@app.route('/paste', methods=['POST'])
def paste_post():
    currentPath = session['currentPath']

    fileList = session['fileList']
    for filename in fileList:
        src = os.path.join(session['filePrePath'], filename)
        dst = os.path.join(currentPath, filename)
        if os.path.isfile(src):
            copyfile(src, dst)
        else:
            copytree(src, dst)

    if dict(request.form).get('mov'):
        for filename in fileList:
            src = os.path.join(session['filePrePath'], filename)
            if os.path.isfile(src):
                os.remove(src)
            else:
                rmtree(src)

    session.pop('isCopy', False)
    session.pop('isMov', False)

    flash("%s 成功!" % fileList)

    return redirect('/ftp/%s' % currentPath)

@app.route('/del', methods=['POST'])
def del_post():
    currentPath = session['currentPath']

    choice = dict(request.form)
    choice.pop('del')
    choice = list(choice)

    for filename in choice:
        path = os.path.join(currentPath, filename)
        try:
            if os.path.isfile(path):
                os.remove(path)
            else:
                rmtree(path)
            flash("删除%s成功" % path)
        except:
            flash("删除%s失败，权限不足或者此文件不存在" % path)

    flash('删除完成!' % choice)
    return redirect('/ftp/%s' % currentPath)

# 取消
@app.route('/cancel', methods=['POST'])
def cancel_post():
    currentPath = session['currentPath']

    session.pop('isUpload', False)
    session.pop('isSearchFile', False)
    session.pop('searchFileRes', False)
    session.pop('isSearchText', False)
    session.pop('searchTextRes', False)
    session.pop('isCreateDir', False)
    session.pop('isCreateFile', False)
    session.pop('filePrePath', False)
    session.pop('fileList', False)
    session.pop('isCopy', False)
    session.pop('isMov', False)

    return redirect('/ftp/%s' % currentPath)

# 清除session
@app.route('/clear', methods=['GET'])
def clear_get():
    session.clear()
    return redirect('/')

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=8080, debug=True)


#library imports
import time
import random
import math
from graphics import *
from threading import *

class Food:
    # food class, gets eaten when or organism touches it, and transfers
    # it's tasty nutrition
    def __init__(self, x, y, environment):
        self.x = x
        self.y = y
        self.nutrition = random.randint(20, 150)
        self.environment = environment
        self.eaten = False
        self.radius = (random.random() * 5) + 1
        if (x < 750):
            self.body = Circle(Point(x, y), self.radius)
            self.body.setFill(color_rgb(0, self.nutrition, self.nutrition))
            self.body.draw(self.environment)

    # current re-sizing method inefficent

    def getEaten(self):
        if not self.eaten:
            self.radius -= 0.05
            #print(self.radius)

            if self.radius < 0:
                # food has died
                self.body.undraw()
                # remove from all lists
                self.eaten = True
            else:
                self.body = Circle(Point(self.x, self.y), self.radius)
                self.body.setFill(color_rgb(0, self.nutrition, self.nutrition))
                self.body.undraw()
                self.body.draw(self.environment)
        else:
            print("food already eaten")
        return self.eaten # returns if the food is eaten

    def grow(self):
        self.radius += 0.05
        self.body = Circle(Point(self.x, self.y), self.radius)
        self.body.setFill(color_rgb(0, self.nutrition, self.nutrition))
        #self.body.undraw()
        self.body.draw(self.environment)

class Corpse: # should inhert food
    # left behind after death
    # slowly rots - always has the same nutritional value, but slowly
    # becomes more dangerous to eat
    def __init__(self, x, y, radius, environment):
        self.x = x
        self.y = y
        self.nutrition = 120 # always 120
        self.radius = radius
        self.decay = 100 # slowly decays over time
        self.eaten = False
        self.body = Circle(Point(x, y), radius)
        self.body.setFill(color_rgb(255, self.decay, self.decay))
        self.body.draw(environment)

    def decay(self):
        self.decay -= 1
        self.body.setFill(color_rgb(255, self.decay, self.decay))
        self.body.draw(self.environment)

    def getEaten(self):
        if not self.eaten:
            self.radius -= 0.05
            #print(self.radius)

            if self.radius < 0:
                # food has died
                self.body.undraw()
                # remove from all lists
                self.eaten = True
            else:
                self.body = Circle(Point(self.x, self.y), self.radius)
                self.body.setFill(color_rgb(255, self.decay, self.decay))
                self.body.undraw()
                self.body.draw(self.environment)


from django.contrib.auth.forms import UserChangeForm, UserCreationForm

from users.models import User

class CustomUserCreationForm(UserCreationForm):
    """Custom User creation form."""

    class Meta:
        model = User
        fields = '__all__'

class CustomUserChangeForm(UserChangeForm):
    """Custom User edit form."""

    class Meta:
        model = User
        fields = '__all__'


#!/usr/bin/env python3

import roslib
import sys
import rospy
import cv2
import numpy as np
import message_filters
from std_msgs.msg import String
from sensor_msgs.msg import Image
from std_msgs.msg import Float64MultiArray, Float64
from cv_bridge import CvBridge, CvBridgeError

class image_converter:
    def __init__(self):
        # initialize the node named image_processing
        rospy.init_node('image_processing', anonymous=True)

        # initialize a publisher to send images from camera1 to a topic named image_topic1
        self.image_pub1 = rospy.Publisher("image_topic1", Image, queue_size=10)
        # initialize a subscriber to receive messages from a topic named /robot/camera1/image_raw
        self.image_sub1 = message_filters.Subscriber("/camera1/robot/image_raw", Image)
        # initialize a subscriber to receive messages from a topic named /robot/camera2/image_raw
        self.image_sub2 = message_filters.Subscriber("/camera2/robot/image_raw", Image)
        # Synchronize subscribers into one callback
        ts = message_filters.TimeSynchronizer([self.image_sub1, self.image_sub2], 10)
        ts.registerCallback(self.callback1)

        # initialize a publisher to send joints' angular position to a topic called joints_pos
        self.joints_pub = rospy.Publisher("joints_pos", Float64MultiArray, queue_size=10)
        # initialize a publisher to send robot end-effector position
        self.end_effector_pub = rospy.Publisher("end_effector_prediction", Float64MultiArray, queue_size=10)
        # initialize the bridge between openCV and ROS
        self.bridge = CvBridge()

        # define publishers
        self.joint1_pub = rospy.Publisher("joint_angle_1", Float64, queue_size=10)
        self.joint3_pub = rospy.Publisher("joint_angle_3", Float64, queue_size=10)
        self.joint4_pub = rospy.Publisher("joint_angle_4", Float64, queue_size=10)

        self.joint1_raw_pub = rospy.Publisher("joint1_angle_raw", Float64, queue_size=10)

        self.last_yellow_blue_link = np.zeros(3)

        # initialize variables
        self.last_green_1 = np.zeros(2)
        self.last_green_2 = np.zeros(2)
        self.last_yellow_1 = np.zeros(2)
        self.last_yellow_2 = np.zeros(2)
        self.last_blue_1 = np.zeros(2)
        self.last_blue_2 = np.zeros(2)
        self.last_red_1 = np.zeros(2)
        self.last_red_2 = np.zeros(2)

        self.last_joint1 = 0
        self.last_joint3 = 0

        self.last_joint1_queue = [0] * 60

        self.switch = 1

        # self.switch_sign = -1

        # hardcode the coordinate of green detected from camera since joint1 is fixed
        self.centre_green_detect = np.array([387, 399, 543])

    # find the centre of the green joint
    def detect_green(self, img):
        # create a green mask
        mask = cv2.inRange(img, (0, 100, 0), (0, 255, 0))
        # apply a dilate to make the binary region larger
        kernel = np.ones((5, 5), np.uint8)
        mask = cv2.dilate(mask, kernel, iterations=3)
        # get the moments of the mask
        M = cv2.moments(mask)

        if (img is self.cv_image1):
            if (M['m00'] != 0):
                cx = int(M['m10'] / M['m00'])
                cy = int(M['m01'] / M['m00'])
                self.last_green_1[0] = cx
                self.last_green_1[1] = cy
                return np.array([cx, cy])
            else:
                return self.last_green_1
        else:
            if (M['m00'] != 0):
                cx = int(M['m10'] / M['m00'])
                cy = int(M['m01'] / M['m00'])
                self.last_green_2[0] = cx
                self.last_green_2[1] = cy
                return np.array([cx, cy])
            else:
                return self.last_green_2

    # find the centre of the yellow joint
    def detect_yellow(self, img):
        # create a yellow mask
        mask = cv2.inRange(img, (0, 100, 100), (0, 255, 255))

        # apply a dilate to make the binary region larger
        kernel = np.ones((5, 5), np.uint8)
        mask = cv2.dilate(mask, kernel, iterations=3)

        # get the moments of the mask
        M = cv2.moments(mask)

        if (img is self.cv_image1):
            if (M['m00'] != 0):
                cx = int(M['m10'] / M['m00'])
                cy = int(M['m01'] / M['m00'])
                self.last_yellow_1[0] = cx
                self.last_yellow_1[1] = cy
                return np.array([cx, cy])
            else:
                return self.last_yellow_1
        else:
            if (M['m00'] != 0):
                cx = int(M['m10'] / M['m00'])
                cy = int(M['m01'] / M['m00'])
                self.last_yellow_2[0] = cx
                self.last_yellow_2[1] = cy
                return np.array([cx, cy])
            else:
                return self.last_yellow_2

    # find the centre of the blue joint
    def detect_blue(self, img):
        # create a blue mask
        mask = cv2.inRange(img, (100, 0, 0), (255, 0, 0))

        # apply a dilate to make the binary region larger
        kernel = np.ones((5, 5), np.uint8)
        mask = cv2.dilate(mask, kernel, iterations=3)

        # get the moments of the mask
        M = cv2.moments(mask)

        if (img is self.cv_image1):
            if (M['m00'] != 0):
                cx = int(M['m10'] / M['m00'])
                cy = int(M['m01'] / M['m00'])
                self.last_blue_1[0] = cx
                self.last_blue_1[1] = cy
                return np.array([cx, cy])
            else:
                return self.last_blue_1
        else:
            if (M['m00'] != 0):
                cx = int(M['m10'] / M['m00'])
                cy = int(M['m01'] / M['m00'])
                self.last_blue_2[0] = cx
                self.last_blue_2[1] = cy
                return np.array([cx, cy])
            else:
                return self.last_blue_2

    # find the centre of the red joint
    def detect_red(self, img):
        # create a red mask
        mask = cv2.inRange(img, (0, 0, 100), (0, 0, 255))

        # apply a dilate to make the binary region larger
        kernel = np.ones((5, 5), np.uint8)
        mask = cv2.dilate(mask, kernel, iterations=3)

        # get the moments of the mask
        M = cv2.moments(mask)

        if (img is self.cv_image1):
            if (M['m00'] != 0):
                cx = int(M['m10'] / M['m00'])
                cy = int(M['m01'] / M['m00'])
                self.last_red_1[0] = cx
                self.last_red_1[1] = cy
                return np.array([cx, cy])
            else:
                return self.last_red_1
        else:
            if (M['m00'] != 0):
                cx = int(M['m10'] / M['m00'])
                cy = int(M['m01'] / M['m00'])
                self.last_red_2[0] = cx
                self.last_red_2[1] = cy
                return np.array([cx, cy])
            else:
                return self.last_red_2

    def get_vector_length(self, vector):
        return np.linalg.norm(vector)

    # invert equation of dot product
    def get_vector_angle(self, vec1, vec2):
        return np.arccos(np.dot(vec1, vec2) / (self.get_vector_length(vec1) * self.get_vector_length(vec2)))

    # consider green joint as the origin of the graph
    def get_joint_centre(self):
        centre_yellow_1 = self.detect_yellow(self.cv_image1)
        centre_yellow_2 = self.detect_yellow(self.cv_image2)
        centre_yellow_x = centre_yellow_2[0] - self.centre_green_detect[0]
        centre_yellow_y = centre_yellow_1[0] - self.centre_green_detect[1]
        centre_yellow_z = -(centre_yellow_1[1] + centre_yellow_2[1] - 2 * self.centre_green_detect[2]) / 2
        centre_yellow = np.array([centre_yellow_x, centre_yellow_y, centre_yellow_z])

        centre_blue_1 = self.detect_blue(self.cv_image1)
        centre_blue_2 = self.detect_blue(self.cv_image2)
        centre_blue_x = centre_blue_2[0] - self.centre_green_detect[0]
        centre_blue_y = centre_blue_1[0] - self.centre_green_detect[1]
        centre_blue_z = -(centre_blue_1[1] + centre_blue_2[1] - 2 * self.centre_green_detect[2]) / 2
        centre_blue = np.array([centre_blue_x, centre_blue_y, centre_blue_z])

        centre_red_1 = self.detect_red(self.cv_image1)
        centre_red_2 = self.detect_red(self.cv_image2)
        centre_red_x = centre_red_2[0] - self.centre_green_detect[0]
        centre_red_y = centre_red_1[0] - self.centre_green_detect[1]
        centre_red_z = -(centre_red_1[1] + centre_red_2[1] - 2 * self.centre_green_detect[2]) / 2
        centre_red = np.array([centre_red_x, centre_red_y, centre_red_z])

        return centre_yellow, centre_blue, centre_red

    # main method to get the desired joint value
    def detect_joint_angles(self):

        # unit vecors
        x = np.array([1, 0, 0])
        y = np.array([0, 1, 0])
        z = np.array([0, 0, 1])

        centre_green = np.array([0, 0, 0])
        centre_yellow, centre_blue, centre_red = self.get_joint_centre()

        # print out the location of end effector
        print("x: ", (centre_red[0] * 0.038))
        print("y: ", (centre_red[1] * 0.038))
        print("z: ", (centre_red[2] * 0.038))

        # pull link out of vector
        green_yellow_link = centre_yellow - centre_green
        yellow_blue_link = centre_blue - centre_yellow
        blue_red_link = centre_red - centre_blue

        # get joints via angle of links and plane / axis
        joint3 = self.get_vector_angle(yellow_blue_link, green_yellow_link)
        joint3_alt = -joint3

        joint1 = self.get_vector_angle([yellow_blue_link[0], yellow_blue_link[1]], [0, -1])

        # switch sign if goes to negative
        if (yellow_blue_link[1] < 0):
            joint3 *= -1

        if (yellow_blue_link[0] < 0):
            joint1 *= -1

        joint1raw = joint1

        # distinguish two conditions of joint1 value
        # maybe distinglish 3 cases when joint1 angle = 0
        if (joint1 >= 0):
            joint1_alt = joint1 - np.pi

            # if there is sudden move, the flipped angle is better
            if ((abs(joint1_alt - self.last_joint1_queue[0]) < abs(joint1 - self.last_joint1_queue[0]))):
                joint1 = joint1_alt
                #
                self.last_joint1_queue[-20:] = [joint1] * 20

        else:
            joint1_alt = joint1 + np.pi

            if ((abs(joint1_alt - self.last_joint1_queue[0]) < abs(joint1 - self.last_joint1_queue[0]))):
                joint1 = joint1_alt
                self.last_joint1_queue[-20:] = [joint1] * 20

        # joint 4 calculation same as vision_1.py
        joint4 = self.get_vector_angle(yellow_blue_link, blue_red_link)
        if (joint4 > np.pi / 2):
            joint4 = np.pi - joint4

        x_transformed = np.cross(y, yellow_blue_link)
        projection = np.dot(x_transformed, blue_red_link)
        if (projection < 0):
            joint4 *= -1

        # store the history for comparison, prevent sudden changes
        self.last_yellow_blue_link = yellow_blue_link

        self.last_joint1 = joint1
        self.last_joint3 = joint3

        self.last_joint1_queue.pop(0)
        self.last_joint1_queue.append(joint1)

        return np.array([joint1, joint3, joint4, joint1raw])

    # Recieve data from camera 1 and camera 2, process it, and publish
    def callback1(self, data1, data2):
        # Recieve the image
        try:
            self.cv_image1 = self.bridge.imgmsg_to_cv2(data1, "bgr8")
            self.cv_image2 = self.bridge.imgmsg_to_cv2(data2, "bgr8")
        except CvBridgeError as e:
            print(e)

        image = np.concatenate((self.cv_image1, self.cv_image2), axis=1)
        im = cv2.imshow('camera1 and camera2', image)
        cv2.waitKey(1)

        joints_angle = self.detect_joint_angles()
        self.joint1 = Float64()
        self.joint1.data = joints_angle[0]
        self.joint3 = Float64()
        self.joint3.data = joints_angle[1]
        self.joint4 = Float64()
        self.joint4.data = joints_angle[2]

        self.joint1raw = Float64()
        self.joint1raw.data = joints_angle[3]

        # Publish the results
        try:
            self.image_pub1.publish(self.bridge.cv2_to_imgmsg(self.cv_image1, "bgr8"))
            self.joint1_pub.publish(self.joint1)
            self.joint3_pub.publish(self.joint3)
            self.joint4_pub.publish(self.joint4)

            self.joint1_raw_pub.publish(self.joint1raw)
        except CvBridgeError as e:
            print(e)

# call the class
def main(args):
    ic = image_converter()
    try:
        rospy.spin()
    except KeyboardInterrupt:
        print("Shutting down")
    cv2.destroyAllWindows()

# run the code if the node is called
if __name__ == '__main__':
    main(sys.argv)


import os
from aws_cdk import (
    aws_ec2 as _ec2,
    aws_ecs as _ecs,
    aws_rds as _rds,
    aws_efs as _efs,
    aws_ecr_assets as _ecr_assets,
    aws_elasticloadbalancingv2 as _elbv2,
    aws_logs as _logs,
    core as cdk
)

class MoodleFileSystemStackProperties:
    def __init__(
            self,
            vpc: _ec2.Vpc,
    ) -> None:

        self.vpc = vpc

class MoodleFileSystemStack(cdk.Stack):

    def __init__(self, scope: cdk.Construct, construct_id: str,
                 properties: MoodleFileSystemStackProperties, **kwargs) -> None:
        super().__init__(scope, construct_id, **kwargs)

        # define shared file system
        self.file_system = _efs.FileSystem(
            self, "MoodleFileSystem",
            vpc=properties.vpc,
            performance_mode=_efs.PerformanceMode.GENERAL_PURPOSE,
            throughput_mode=_efs.ThroughputMode.BURSTING
        )


class Codec:

    def encode(self, longUrl):
        """Encodes a URL to a shortened URL.

        :type longUrl: str
        :rtype: str
        """
        self.baseUrl = "http://tinyurl/"
        import hashlib, random
        self.store = {}
        m = hashlib.sha256(longUrl.encode('utf-8'))
        key = m[:5]
        import collections
        path = collections.defaultdict(0)

        while key in self.store:
            tmp = longUrl + str(random.random())
            m = hashlib.sha256(tmp.encode('utf-8'))
            key = m[:5]

        self.store[key] = longUrl
        return self.baseUrl + key

    def decode(self, shortUrl):
        """Decodes a shortened URL to its original URL.

        :type shortUrl: str
        :rtype: str
        """
        key = shortUrl.replace(self.baseUrl, '')
        return self.store[key]

s = Codec()
s.encode("https://leetcode.com/problems/design-tinyurl")


from pygame import *
backgrounds=[image.load("new"+str(i)+".png")for i in range(30)]
screen=display.set_mode((479,768*31))
running=True
while running:
    for e in event.get():
        if e.type==QUIT:
            running=False
    screen.blit(image.load("new29.png"),(0,0))
    for i in range(len(backgrounds)):
        screen.blit(backgrounds[i],(0,(i+1)*768))
    display.update()
    image.save(screen.subsurface(0,0,479,768*31),"final.png")
    display.flip()
    running=False
quit()


#We thank Jere et al (2019). for making their code available : https://arxiv.org/abs/1912.02316

from torch.utils.data import DataLoader
from tqdm import tqdm
import torchvision.utils as vutils
from timm.models import create_model
import torchvision.transforms as transforms
import torch
# from OnePatch import OnePatchAttack
from cPixelAttack import cOnePixel
import matplotlib.pyplot as plt
import  numpy as np
import torchvision.datasets as dataset
import wandb
from pytorch_grad_cam import GradCAM, ScoreCAM, GradCAMPlusPlus, AblationCAM, XGradCAM, EigenCAM, FullGrad
from pytorch_grad_cam.utils.model_targets import ClassifierOutputTarget
from pytorch_grad_cam.utils.image import show_cam_on_image, preprocess_image
import torch.nn as nn

#uncomment to log with wandb
wandb.init(project= "one_pixel_attack_vit_small_thesis")
artifact = wandb.Artifact("vit_small_2px_samples_" + str(wandb.run.id) , type="prediction")
columns = ["id", "scr_img", "adv_image", "ground_truth", "pred b4 attack", "confidence", "gradcam_scr", "gradcam_adv", "pred after attack", "adv confidence"]
log_table = wandb.Table(columns=columns)

def show_img(im):
    k = im.view(3, 224, 224)
    #k = im.squeeze(0).permute(1, 2, 0).detach().cpu()
    k = k.permute(1, 2, 0).detach().cpu()
    plt.imshow(k)
    plt.show()

device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
net = create_model('vit_small_patch16_224', pretrained=True)
#net =  create_model("small_patch16_224_hierarchical", pretrained=True)
net.to(device)
net.eval()

WIDTH = 224
mean = np.array([0.485, 0.456, 0.406])
std = np.array([0.229, 0.224, 0.225])
normalize = transforms.Normalize( mean=mean, std=std)
data_transform = transforms.Compose([
    transforms.Resize(WIDTH),
    transforms.CenterCrop(WIDTH),
    transforms.ToTensor(),
    #normalize
    ])
data_to_pil = transforms.ToPILImage()

def reshape_transform(tensor, height=14, width=14):
    result = tensor[:, 1 :  , :].reshape(tensor.size(0),
        height, width, tensor.size(2))
    result = result.transpose(2, 3).transpose(1, 2)
    return result

target_layers = [net.blocks[-1].norm1]
cam1 = GradCAM(model=net, target_layers=target_layers, use_cuda=True, reshape_transform=reshape_transform)
#cam2 = GradCAM(model=net, target_layers=target_layers, use_cuda=True, reshape_transform=reshape_transform)

path_to_resources = '/home/abass.abdulsalam/Documents/imgnet/processed_val/'
batch_size = 1
test_dataset = dataset.ImageFolder(root=path_to_resources, transform=data_transform)
test_loader = DataLoader(dataset=test_dataset, batch_size=batch_size,
                             num_workers=2, shuffle=True)

#scr = OnePatchAttack(net)
scr = cOnePixel(net)
with tqdm(test_loader, unit = 'batch', position = 0, leave=True) as p_bar:
    success = 0
    fool_rate = 0
    total = 0

    for batch_idx, (data, labels) in enumerate(p_bar):
        data = data.to(device)
        gt_label = labels.to(device)

        output =  net(data)
        #output = torch.vstack(output)
        maxval, pred_before_attack = output.data.max(1, keepdim=True)
        total += 1

        attacked_img = scr.forward(data, labels)
        adv_output = net(attacked_img)
        # adv_output = torch.vstack(adv_output)
        maxval_adv, pred_after_attack = adv_output.data.max(1, keepdim=True)

        if pred_after_attack != gt_label:
            success += 1

        if pred_after_attack != pred_before_attack:
            fool_rate += 1

        targets_cam = [ClassifierOutputTarget(gt_label.item())]

        grayscale_cam_scr = cam1(input_tensor=data, targets=targets_cam, aug_smooth=True)
        grayscale_cam_scr = grayscale_cam_scr[0, :]
        rgb_img_scr = np.float32(data.squeeze(0).permute(1, 2, 0).cpu())
        gradcam_img_scr = show_cam_on_image(rgb_img_scr, grayscale_cam_scr)

        grayscale_cam_adv = cam1(input_tensor=attacked_img, targets=targets_cam, aug_smooth=True)
        grayscale_cam_adv = grayscale_cam_adv[0, :]
        rgb_img_adv = np.float32(attacked_img.squeeze(0).permute(1, 2, 0).cpu())
        gradcam_img_adv = show_cam_on_image(rgb_img_adv, grayscale_cam_adv)

            # if batch_idx < 20:
            #     vutils.save_image(data.data, "./%s/%d_%d_original.png" % (
            #     "logs/one_patch/three", batch_idx, labels),
            #                         normalize=True)
            #     vutils.save_image(attacked_img.data, "./%s/%d_%d_adversarial_%d.png" % (
            #         "logs/one_patch/three", batch_idx, labels, label_adv),
            #                         normalize=True)

        # p_bar.set_description("Attack Success: {:.2f}, FR:{:.2f}, Queries: {}".format((success * 100 / total), \
        #                         (fool_rate * 100 /total), scr.required_iterations))

        wandb.log({"Attack Success" : (success * 100 / total),
                   "Fool rate" : (fool_rate * 100 /total),
                   #"queries" : scr.required_iterations[0],
                   "number of pixels":2})

        log_table.add_data(batch_idx, wandb.Image(data), wandb.Image(attacked_img), gt_label.item(), pred_before_attack.item(), maxval.item(),\
                           wandb.Image(gradcam_img_scr), wandb.Image(gradcam_img_adv), pred_after_attack.item(), maxval_adv.item())
    artifact.add(log_table, "predictions")
    wandb.run.log_artifact(artifact)
        #wandb.log({"Deit_tiny" : log_table})

#print(scr.required_iterations[-1])


import collections
import os
from pathlib import Path
from typing import List

import streamlit as st
from sentence_transformers import SentenceTransformer

from search.engine import Engine, Result
from search.model import load_minilm_model
from search.utils import get_memory_usage

os.environ["TOKENIZERS_PARALLELISM"] = "false"

_DATA_DIR = os.environ.get("DATA_DIR", "data/people_pm_minilm")

st.set_page_config(page_title="Search Engine", layout="wide")

st.markdown(
    """
<style>
.big-font {
    font-size:20px;
}
</style>
""",
    unsafe_allow_html=True,
)

@st.cache(allow_output_mutation=True)
def load_engine() -> Engine:
    engine = Engine(
        data_dir=Path(_DATA_DIR),
    )
    return engine

@st.cache(allow_output_mutation=True)
def load_model() -> SentenceTransformer:
    return load_minilm_model()

engine = load_engine()

model = load_model()

st.error("Create a text input for the query.")

st.error("Create a slider with the number of results to retrieve.")

with st.spinner("Querying index ..."):
    st.error("Get query embedding.")
    st.error("Search results (engine.search).")

# Show the results.
# You can use st.markdown to render markdown.
# e.g. st.markdown("**text**") will add text in bold font.

st.error("Render results")
st.markdown(f"**Mem Usage**: {get_memory_usage()}MB")


import collections
import sys

input = sys.stdin.readline

def main():
    n = int(input())
    a = list(map(int,input().split()))
    b = list(map(int,input().split()))
    c = list(map(int,input().split()))

    step = [a,b,c]

    cnt_a = [0]*46
    cnt_b = [0]*46
    cnt_c = [0]*46
    cnt = [cnt_a,cnt_b,cnt_c]

    for i,v_ in enumerate(step):
        for v in v_:
            val = v % 46
            cnt[i][val]+=1

    ans = 0

    for i in range(46):
        for j in range(46):
            for k in range(46):
                if cnt[0][i] != 0 and cnt[1][j] != 0 and cnt[2][k] !=0 and (i+j+k) % 46 == 0:
                    ans += cnt[0][i]*cnt[1][j]*cnt[2][k]

    print(ans)

if __name__ == '__main__':
   main()


from django.db import models

# Create your models here.
from client.models import Client
from accountant.models import Accountant
from utils.models import Subscribe
from django.utils import timezone
from utils.models import Packages

class TypeOfActivity(models.Model):
    id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=255, blank=True)

    def __str__(self):
        return self.name

class TaxationSystem(models.Model):
    id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=255, blank=True)

class Company(models.Model):
    id = models.AutoField(primary_key=True)
    client = models.ForeignKey(Client, blank=True, related_name='client_company', on_delete='cascade', default=None)
    logo = models.ImageField(blank=True, null=True)
    subscribe = models.ForeignKey(Subscribe, on_delete=models.CASCADE, blank=True, related_name='subscribe', null=True)
    name = models.CharField(max_length=255, blank=True)
    HVHH = models.CharField(max_length=255, blank=True)
    address = models.CharField(max_length=255, blank=True)
    accountant = models.ManyToManyField(Accountant, related_name="accountant_many_company", null=True, blank=True)
    created_date = models.DateTimeField(default=timezone.now, null=True)
    created_number = models.IntegerField(null=True, blank=True)
    director_full_name = models.TextField(null=True, blank=True)
    category = models.IntegerField(null=True, blank=True)
    count_employees = models.IntegerField(null=True, blank=True)
    is_deleted = models.BooleanField(default=False,blank=True)
    is_deleted_by_manager = models.BooleanField(default=False,blank=True)
    type_of_activity = models.ManyToManyField(TypeOfActivity, null=True, blank=True)
    packages = models.ForeignKey(Packages, null=True, blank=True, on_delete=models.DO_NOTHING,
                                 related_name="packages_company")
    taxation_system = models.ForeignKey(TaxationSystem, null=True, blank=True, on_delete=models.DO_NOTHING)

    def __str__(self):
        return self.name


import torch
import numpy as np

from torch.utils.data import Dataset
from tqdm import tqdm

from dashboard.model import DistilBertForSequenceClassification
from transformers import DistilBertConfig, DistilBertTokenizer
from sklearn.manifold import TSNE

TOKENIZER = DistilBertTokenizer.from_pretrained('distilbert-base-uncased')
DEVICE = torch.device("cpu")
MAX_SEQ_LENGTH = 256

class text_dataset(Dataset):
    def __init__(self, x, y=None, transform=None):
        self.x = x
        self.y = y
        self.transform = transform

    def __getitem__(self, index):
        tokenized_comment = TOKENIZER.tokenize(self.x[index])

        if len(tokenized_comment) > MAX_SEQ_LENGTH:
            tokenized_comment = tokenized_comment[:MAX_SEQ_LENGTH]

        ids_review  = TOKENIZER.convert_tokens_to_ids(tokenized_comment)
        padding = [0] * (MAX_SEQ_LENGTH - len(ids_review))
        ids_review += padding

        assert len(ids_review) == MAX_SEQ_LENGTH

        ids_review = torch.tensor(ids_review)
        if self.y is None:
            return ids_review

        hcc = self.y[index] # toxic comment
        list_of_labels = [torch.from_numpy(hcc)]

        return ids_review, list_of_labels[0]

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

def preds(model, test_loader):
    predictions = []
    for inputs in test_loader:
        inputs = inputs.to(DEVICE)
        with torch.no_grad():
            outputs = model(inputs)
            outputs = torch.sigmoid(outputs)
            if len(outputs) > 1:
                predictions += outputs.cpu().detach().numpy().tolist()
            else:
                predictions.append(outputs.cpu().detach().numpy().tolist())

    return predictions

def text_to_2d_plane(model, test_loader):
    embeds = []
    for inputs in tqdm(test_loader, total=len(test_loader)):
        inputs = inputs.to(DEVICE)
        with torch.no_grad():
            outputs = model.distilbert(inputs)
            if len(outputs) > 1:
                embeds += outputs[0][:, 0].squeeze().tolist()
            else:
                embeds.append(outputs[0][:, 0].squeeze().tolist())
            # embeds.append(outputs[0][:, 0].squeeze().tolist())
    return TSNE().fit_transform(embeds)

def main():
    config = DistilBertConfig(
        vocab_size=32000, hidden_dim=768,
        dropout=0.1, num_labels=6,
        n_layers=12, n_heads=12,
        intermediate_size=3072)
    state_dict = torch.load('models/distilbert_model_weights.pth', map_location=DEVICE)
    model = DistilBertForSequenceClassification(config)
    model.load_state_dict(state_dict=state_dict)
    model.to(DEVICE)
    comments = ['Fuck u', 'I like u', 'I hate u', 'I wish u were dead!',
    'U r a good man', 'I hope that you will die someday', 'Hate must be vanished',
    'Fuck yeah', 'Fucking aweasome', 'The government should have done somethinng more meaningful' ]
    test_dataset = text_dataset(comments)
    prediction_dataloader = torch.utils.data.DataLoader(test_dataset, batch_size=2, shuffle=False)
    planes = text_to_2d_plane(model=model, test_loader=prediction_dataloader)
    print(planes)

if __name__ == '__main__':
    main()