text:
#Put local path where you want to create yur new python file1
file1= open(r"upscaled.py",'w')

#Basic Plotting Parameters
file1.write("import os\n")
file1.write("import sys\n")
file1.write("import fmipp\n")
file1.write("import optparse\n\n")
file1.write("# Import FMI++ Python library.\n\n")
file1.write("import matplotlib.pyplot as plt\n")
file1.write("import csv\n")
file1.write("import numpy as np\n\n")
file1.write("Set1Red='#e41a1c'\n")
file1.write("Set1Blue='#377eb8'\n")
file1.write("Set1Green='#4daf4a'\n")
file1.write("Set1Purple='#984ea3'\n")
file1.write("Set1Orange='#ff7f00'\n")
file1.write("Set1Yellow='#ffff33'\n")
file1.write("Set1Brown='#a65628'\n")
file1.write("Set1Pink='#f781bf'\n")
file1.write("Set1Gray='#999999'\n")
file1.write("lw=2\n")
file1.write("colorOM=Set1Red\n\n")
file1.write("# voltage control gain\n")
file1.write("Kv=0.0\n")
file1.write("# voltage control integrator gain\n")
file1.write("KiV=10\n")
file1.write("# per unit current limit of the converter\n")
file1.write("Ilim=1.1\n")
file1.write("# current limiting priority during normal operation\n")
file1.write("Iprio=2\n")
file1.write("# current limiting priority during FRT\n")
file1.write("IprioFRT=2\n")
file1.write("# current limiting priority during post-FRT\n")
file1.write("IprioPOSTFRT=1\n")
file1.write("# d-axis current slew rate (pu/s) during normal operation\n")
file1.write("RP=100\n")
file1.write("# d-axis current slew rate (pu/s) during post-FRT\n")
file1.write("POSTRP=0.5\n")
file1.write("# additional reactive current injection 'boosting' gain, kicks in during fault\n")
file1.write("KBOOST=1.0\n")
file1.write("# export results name\n")
file1.write("EXPname = 'KBOOST=1_Ilim=11_PRIO=2_POSTRP=05.csv'\n")
file1.write("# F M U  P A R A M E T E R S\n")
file1.write("work_dir=os.getcwd()\n")
file1.write("logging_on = False\n\n")

#Creating all 33 instances of FRT Controllers
for i in range(1,33):
    file1.write("#FRT Controller for WT_%i\n"%(i))
    file1.write("model_name_F_%i='FRTController_mar_v2_sf'\n"%(i))
    file1.write("stop_before_event = False\n")
    file1.write("event_search_precision = 1e-10\n")
    file1.write("integrator_type = fmipp.eu\n")

for i in range(1,33):
    file1.write("path_to_fmu_F_%i = os.path.join(work_dir, model_name_F_%i + '.fmu')\n"%(i,i))

for i in range(1,33):
    file1.write("# Extract FMU and retrieve URI to directory.\n")
    file1.write("uri_to_extracted_fmu_F_%i= fmipp.extractFMU( path_to_fmu_F_%i, work_dir )\n"%(i,i))

for i in range(1,33):
    file1.write("# using the FMI 2.0 specification\n")
    file1.write("fmuF_%i = fmipp.FMUModelExchangeV2(uri_to_extracted_fmu_F_%i, model_name_F_%i, logging_on, stop_before_event, event_search_precision, integrator_type )\n"%(i,i,i))

for i in range(1,33):
    file1.write("# Instantiate the FMU.\n")
    file1.write("status%i = fmuF_%i.instantiate( 'my_FRT_controller') # instantiate model\n"%(i,i))

for i in range(1,33):
    file1.write("assert status%i == fmipp.fmiOK\n"%(i))

for i in range(1,33):
    file1.write("# Initialize the FMU%i\n"%(i))
    file1.write("status%i = fmuF_%i.initialize() # initialize model\n"%(i,i))
    file1.write("assert status%i == fmipp.fmiOK\n\n\n"%(i))

#Creating all 33 instances of MATLAB Controllers
for i in range(1,33):
    file1.write("#Converter Controller for WT_%i\n"%(i))
    file1.write("model_name_M_%i='RmsConverterController_AAvdM_R2014b_sf'\n"%(i))
    file1.write("stop_before_event = False\n")
    file1.write("event_search_precision = 1e-10\n")
    file1.write("integrator_type = fmipp.dp\n")

for i in range(1,33):
    file1.write("path_to_fmu_M_%i = os.path.join(work_dir, model_name_M_%i + '.fmu')\n"%(i,i))

for i in range(1,33):
    file1.write("# Extract FMU and retrieve URI to directory.\n")
    file1.write("uri_to_extracted_fmu_M_%i= fmipp.extractFMU( path_to_fmu_M_%i, work_dir )\n"%(i,i))

for i in range(1,33):
    file1.write("# using the FMI 2.0 specification\n")
    file1.write("fmuM_%i = fmipp.FMUModelExchangeV2(uri_to_extracted_fmu_M_%i, model_name_M_%i, logging_on, stop_before_event, event_search_precision, integrator_type )\n"%(i,i,i))

for i in range(1,33):
    file1.write("status%i = fmuM_%i.instantiate( 'my_RMS_controller') # instantiate model\n"%(i,i))

for i in range(1,33):
    file1.write("assert status%i == fmipp.fmiOK\n\n\n"%(i))

#Creating PowerFactory FMU
file1.write("#Second FMI-CS from PowerFactory\n")
file1.write("model_name = 'upscaled'\n")
file1.write("time_diff_resolution = 1e-9\n")
file1.write("# Specify path to the FMU (located in subdirectory 'NineBusSystem-resources' of the current working directory).\n")
file1.write("path_to_fmu = os.path.join( work_dir, model_name + '.fmu' )\n")
file1.write("# Extract FMU and retrieve URI to directory.\n")
file1.write("uri_to_extracted_fmu = fmipp.extractFMU( path_to_fmu, work_dir )\n")
file1.write("# Create an instance of class FMUCoSimulation (handle for the FMU).\n")
file1.write("fmu = fmipp.FMUCoSimulationV1( uri_to_extracted_fmu, model_name, logging_on, time_diff_resolution )\n")
file1.write("# Instantiate the FMU.\n")
file1.write("start_time = 0.\n")
file1.write("instance_name = 'upscaled'\n")
file1.write("visible = False\n")
file1.write("interactive = False\n")
file1.write("status = fmu.instantiate( instance_name, start_time, visible, interactive )\n")
file1.write("assert status == fmipp.fmiOK\n")
file1.write("# Initialize the FMU.\n")
file1.write("stop_time = 2\n")
file1.write("stop_time_defined = True\n")
file1.write("step_size = 0.005\n")
file1.write("status = fmu.initialize( start_time, stop_time_defined, stop_time )\n")
file1.write("assert status == fmipp.fmiOK\n")
for i in range(1,33):
    file1.write("Pn_%i= fmu.getRealValue('ElmGenstat.WT_%i.e:Pnom')\n"%(i,i))

#FRT Control Parameters
for i in range(1,33):
    file1.write("# WT_%i FRT control\n"%(i))
    file1.write("# K_aRCI: boosting gain\n")
    file1.write("fmuM_%i.setRealValue('K_aRCI',KBOOST)\n"%(i))
    file1.write("# R_on: rate limiter switch 1.0: on, 0.0: off\n")
    file1.write("fmuM_%i.setRealValue('R_on',1.0)\n"%(i))
    file1.write("# R_p: maximum ramping rate\n")
    file1.write("fmuM_%i.setRealValue('R_p', 10.0)\n"%(i))
    file1.write("# I_lim: limitation priority 1.0: d-priority, 2.0: q-priority \n")
    file1.write("fmuM_%i.setRealValue('I_lim', Iprio)\n"%(i))
    file1.write("# d-axis voltage (pu)\n")
    file1.write("fmuM_%i.setRealValue('Qpcc', fmu.getRealValue('ElmGenstat.WT_%i.m:Q:bus1') / Pn_%i)\n"%(i,i,i))
    file1.write("# q-axis voltage (pu, unused)\n")
    file1.write("fmuM_%i.setRealValue('Vq', 0.0)\n"%(i))
    file1.write("# RMS voltage at PCC (pu)\n")
    file1.write("fmuM_%i.setRealValue('V_pcc_RMS', fmu.getRealValue('ElmGenstat.WT_%i.m:u1:bus1'))\n"%(i,i))
    file1.write("# reference value of Q (terminal)\n")
    file1.write("fmuM_%i.setRealValue('Qref_pu', fmu.getRealValue('ElmGenstat.WT_%i.m:Q:bus1') / Pn_%i)\n"%(i,i,i))
    file1.write("# dc voltage (pu)\n")
    file1.write("fmuM_%i.setRealValue('Vdc_pu', 1.0)\n"%(i))

#Limiter Values
file1.write("maxCurrent=Ilim\n")
for i in range(1,33):
    file1.write("fmuM_%i.setRealValue('Parameters.Converter_RMS_Controller.Saturation.UpperLimit',maxCurrent)\n"%(i))
    file1.write("fmuM_%i.setRealValue('Parameters.Converter_RMS_Controller.Saturation.LowerLimit',-maxCurrent)\n"%(i))
    file1.write("fmuM_%i.setRealValue('Parameters.Converter_RMS_Controller.PID_Controller.Saturate.UpperLimit',maxCurrent)\n"%(i))
    file1.write("fmuM_%i.setRealValue('Parameters.Converter_RMS_Controller.PID_Controller.Saturate.LowerLimit',-maxCurrent)\n"%(i))
    file1.write("fmuM_%i.setRealValue('Parameters.Converter_RMS_Controller.PID_Controller1.Saturate.UpperLimit',maxCurrent)\n"%(i))
    file1.write("fmuM_%i.setRealValue('Parameters.Converter_RMS_Controller.Riferimento_Q1.Saturation.UpperLimit',maxCurrent)\n"%(i))
    file1.write("fmuM_%i.setRealValue('Parameters.Converter_RMS_Controller.Riferimento_Q1.Saturation.LowerLimit',-maxCurrent)\n"%(i))
    file1.write("fmuM_%i.setRealValue('Parameters.Converter_RMS_Controller.V_nom.Value', fmu.getRealValue('ElmGenstat.WT_%i.m:u1:bus1'))\n\n"%(i,i))
    file1.write("fmuM_%i.setRealValue('Parameters.Converter_RMS_Controller.PID_Controller.Integral_Gain.Gain', 1)\n"%(i))
    file1.write("fmuM_%i.setRealValue('Parameters.Converter_RMS_Controller.PID_Controller.Proportional_Gain.Gain',0.0)\n"%(i))
    file1.write("fmuM_%i.setRealValue('Parameters.Converter_RMS_Controller.PID_Controller1.Integral_Gain.Gain',KiV)\n"%(i))
    file1.write("fmuM_%i.setRealValue('Parameters.Converter_RMS_Controller.PID_Controller1.Proportional_Gain.Gain',Kv)\n"%(i))
    file1.write("status%i = fmuM_%i.setRealValue('Parameters.Converter_RMS_Controller.Ramp_limitation.Rate_Limiter.RisingSlewLimit', RP)\n"%(i,i))

#Obtaining Vnom values
file1.write("#Obtaining Vnom values\n")
for i in range(1,33):
     file1.write("Vnom_%i = fmuM_%i.getRealValue('Parameters.Converter_RMS_Controller.V_nom.Value')\n"%(i,i))

#Obtaining Loadflow values from PowerFactory
file1.write("#Obtaining Loadflow values from PowerFactory\n")
for i in range(1,33):
    file1.write("P_0_%i = fmu.getRealValue('ElmGenstat.WT_%i.m:P:bus1')\n"%(i,i))
    file1.write("Q_0_%i = fmu.getRealValue('ElmGenstat.WT_%i.m:Q:bus1')\n"%(i,i))
    file1.write("V_0_%i = fmu.getRealValue('ElmGenstat.WT_%i.m:u1:bus1')\n"%(i,i))
    file1.write("theta_0_%i = fmu.getRealValue('ElmGenstat.WT_%i.m:phiu1:bus1')\n"%(i,i))
    file1.write("Pnom_%i= fmu.getRealValue('ElmGenstat.WT_%i.e:Pnom')\n"%(i,i))

#processes
file1.write("#processes\n")
for i in range(1,33):
    file1.write("i_d_0_%i = P_0_%i / Pnom_%i/ V_0_%i\n"%(i,i,i,i))
    file1.write("i_q_0_%i = - Q_0_%i / Pnom_%i / V_0_%i\n"%(i,i,i,i))

for i in range(1,33):
    file1.write("fmuM_%i.setRealValue('Parameters.Converter_RMS_Controller.PID_Controller.Integrator.InitialCondition', i_d_0_%i)\n"%(i,i))
    file1.write("fmuM_%i.setRealValue('Parameters.Converter_RMS_Controller.PID_Controller1.Integrator.InitialCondition', i_q_0_%i)\n"%(i,i))

#Initialisation of FMU's
file1.write("#Initialisation of FMU's\n")
for i in range(1,33):
    file1.write("status%i = fmuM_%i.initialize()\n"%(i,i))
    file1.write("assert status%i == fmipp.fmiOK\n"%(i))
    file1.write("Id_ref_%i = fmuM_%i.getRealValue('Id_ref')\n"%(i,i))
    file1.write("Iq_ref_%i = fmuM_%i.getRealValue('Iq_ref')\n"%(i,i))

#Controller Initialisation
file1.write("#Controller Initialisaton\n")
for i in range(1,33):
    file1.write("fmuF_%i.setRealValue('Udc',fmuM_%i.getRealValue('Vdc_pu'))\n"%(i,i))
    file1.write("fmuF_%i.setRealValue('VPCC',V_0_%i)\n"%(i,i))
    file1.write("fmuF_%i.setBooleanValue('release',True)\n"%(i))
    file1.write("fmuF_%i.setRealValue('Qref_pu',Q_0_%i/Pn_%i)\n"%(i,i,i))

file1.write("# dT: time step size of the simulation federate, check compatibility with co-simulation synchronisation step size\n")
for i in range(1,33):
    file1.write("fmuF_%i.setRealValue('Parameters.StateMachine_dT', step_size)\n"%(i))

file1.write("# RpS1: ramping rate when in state=1 (normal operation)\n")
for i in range(1,33):
    file1.write("fmuF_%i.setRealValue('Parameters.State_Machine.state_transition_RpS1', RP)\n"%(i))

file1.write("# RpS2: ramping rate when in state=2 (in FRT operation)\n")
for i in range(1,33):
    file1.write("fmuF_%i.setRealValue('Parameters.State_Machine.state_transition_RpS2', RP)\n"%(i))

file1.write("# RpS3: ramping rate when in state=3 (in post FRT operation), determines how long the converter will ramp up the active power and when it returns to normal operation, i.e., state=1\n")
for i in range(1,33):
    file1.write("fmuF_%i.setRealValue('Parameters.State_Machine.state_transition_RpS3', POSTRP)\n"%(i))

file1.write("# set the current limiting strategy for each of the states. 1.0: d-priority, 2.0: q-priority\n")
for i in range(1,33):
    file1.write("fmuF_%i.setRealValue('Parameters.State_Machine.state_transition_CLMODES1', Iprio)\n"%(i))
    file1.write("fmuF_%i.setRealValue('Parameters.State_Machine.state_transition_CLMODES2', IprioFRT)\n"%(i))
    file1.write("fmuF_%i.setRealValue('Parameters.State_Machine.state_transition_CLMODES3', IprioPOSTFRT)\n"%(i))

file1.write("# set the additional reactive current injection gain for each state\n")
for i in range(1,33):
    file1.write("fmuF_%i.setRealValue('Parameters.State_Machine.state_transition_KBS1', 0.0)\n"%(i))
    file1.write("fmuF_%i.setRealValue('Parameters.State_Machine.state_transition_KBS2', KBOOST)\n"%(i))
    file1.write("fmuF_%i.setRealValue('Parameters.State_Machine.state_transition_KBS3', KBOOST)\n"%(i))

#Outputs
file1.write("#Outputs\n")
for i in range(1,33):
    file1.write("State_%i = fmuF_%i.getRealValue('STATE')\n"%(i,i))
    file1.write("CLM_%i = fmuF_%i.getRealValue('CLMODE')\n"%(i,i))
    file1.write("KaRCI_%i = fmuF_%i.getRealValue('KaRCI')\n"%(i,i))
    file1.write("Rp_%i = fmuF_%i.getRealValue('Rp')\n"%(i,i))
    file1.write("print 'State = ', State_%i\n"%(i))
    file1.write("print 'current limit mode = ', CLM_%i\n"%(i))
    file1.write("print 'boosting gain = ', KaRCI_%i\n\n"%(i))

#Simulation Run
file1.write("time = 0.\n")
file1.write("step_size = 0.01\n")

#Data Container for results
file1.write("#Data Container for results\n")
file1.write("res_time = [] # time steps\n")
file1.write("res_G1_angle = [] # rotor angle of G1 relative to reference machine angle\n")
file1.write("res_G2_angle = [] # rotor angle of G2 relative to reference machine angle\n")
file1.write("res_G1_speed = [] # rotor speed of G1 relative to reference machine angle\n")
file1.write("res_G2_speed = [] # rotor speed of G2 relative to reference machine angle\n")
file1.write("res_Bus9_U= []\n")
file1.write("res_TR_P= []\n")

for i in range(1,33):
    file1.write("res_Bus_U_%i = []\n"%(i))
    file1.write("res_WT_%i_P_%i = []\n"%(i,i))
    file1.write("res_Id_ref_%i = []\n"%(i))
    file1.write("res_Iq_ref_%i = []\n"%(i))
    file1.write("res_I_out_%i = []\n"%(i))
    file1.write("res_State_%i = [] # value of FRT state\n"%(i))
    file1.write("res_CLM_%i = []\n\n"%(i))
    file1.write("res_Vpcc_%i=[]\n"%(i))
    file1.write("res_Vdiff_%i=[]\n"%(i))
    file1.write("res_Vnom_%i=[]\n"%(i))

file1.write("t0=True\n")

#Running Co-sim
file1.write("while ( time < stop_time ):\n")
file1.write("    new_step = True\n")
for i in range(1,33):
    file1.write("    tP_%i = fmuM_%i.integrate( time + step_size) # integrate model\n"%(i,i))

for i in range(1,33):
    file1.write("    tF_%i = fmuF_%i.integrate( time + step_size) # integrate model \n"%(i,i))

file1.write("    status = fmu.doStep( time, step_size, new_step )\n")
file1.write("    assert status == fmipp.fmiOK\n")

# Advance time.
file1.write("    time += step_size\n\n")

for i in range(1,33):
    file1.write("    controllerOut_%i = fmuM_%i.getRealValue('Id_ref')\n"%(i,i))

for i in range(1,33):
    file1.write("    Ppcc_%i = fmu.getRealValue('ElmGenstat.WT_%i.m:Psum:bus1') / Pnom_%i\n"%(i,i,i))
    file1.write("    Vpcc_%i = fmu.getRealValue( 'ElmGenstat.WT_%i.m:u1:bus1')\n"%(i,i))
    file1.write("    Qpcc_%i = fmu.getRealValue('ElmGenstat.WT_%i.m:Q:bus1') / Pnom_%i\n"%(i,i,i))
    file1.write("    iq_out_%i = fmuM_%i.getRealValue('Iq_ref')\n"%(i,i))
    file1.write("    State_%i = fmuF_%i.getRealValue('STATE')\n"%(i,i))
    file1.write("    CLM_%i = fmuF_%i.getRealValue('CLMODE')\n"%(i,i))
    file1.write("    KaRCI_%i = fmuF_%i.getRealValue('KaRCI')\n"%(i,i))
    file1.write("    Rp_%i = fmuF_%i.getRealValue('Rp')\n\n"%(i,i))

for i in range(1,33):
    file1.write("    fmuM_%i.setRealValue('V_pcc_RMS', Vpcc_%i)\n"%(i,i))
    file1.write("    fmuM_%i.setRealValue('Qpcc', Qpcc_%i)\n"%(i,i))
    file1.write("    status%i = fmuF_%i.setRealValue( 'VPCC', Vpcc_%i)\n\n"%(i,i,i))
    file1.write("    Vdef_%i=(Vnom_%i-Vpcc_%i)\n\n"%(i,i,i))

for i in range(1,33):
    file1.write("    print('|'+33*'-'+' t = %5.2f s ' %time + 33*'-' +'|')\n")
    file1.write("    print('terminal voltage of WT_%i = %%s pu' %%Vpcc_%i)\n"%(i,i))
    file1.write("    print('|'+77*'-'+'|')\n")

# Calculations
file1.write("    if time<0.18:\n")
for i in range(1,33):
    file1.write("        Pref_%i=P_0_%i/Pnom_%i\n"%(i,i,i))

file1.write("    else:\n")
for i in range(1,33):
    file1.write("        Pref_%i=P_0_%i/Pnom_%i\n"%(i,i,i))

file1.write("    # current limit mode: limitation priority 1.0: d-priority, 2.0: q-priority\n")
for i in range(1,33):
    file1.write("    fmuM_%i.setRealValue('I_lim', CLM_%i)\n"%(i,i))

file1.write("    # ramping rate from FRT controller to converter controller\n")
for i in range(1,33):
    file1.write("    fmuM_%i.setRealValue('Parameters.Converter_RMS_Controller.Ramp_limitation.Rate_Limiter.RisingSlewLimit', Rp_%i)\n"%(i,i))

file1.write("    # additional reactive current injection gain from FRT controller to convertere controller\n")
for i in range(1,33):
    file1.write("    fmuM_%i.setRealValue('K_aRCI',KaRCI_%i)\n"%(i,i))

for i in range(1,33):
    file1.write("    status%i = fmu.setRealValue( 'EvtParam.WTframe_%i.id_ref_in', controllerOut_%i - i_d_0_%i)\n"%(i,i,i,i))
    file1.write("    assert status%i == fmipp.fmiOK\n"%(i))

for i in range(1,33):
    file1.write("    status%i = fmu.setRealValue( 'EvtParam.WTframe_%i.iq_ref_in', iq_out_%i - i_q_0_%i)\n"%(i,i,i,i))
    file1.write("    assert status%i == fmipp.fmiOK \n"%(i))

file1.write("    # set reference value of active power infeed by static generator\n")
for i in range(1,33):
    file1.write("    fmuM_%i.setRealValue('Parameters.Converter_RMS_Controller.Vdc_ref__V_.Value', Pref_%i)\n"%(i,i))

file1.write("    # set measured value of active power infeed into FMU-ME\n")
for i in range(1,33):
    file1.write("    fmuM_%i.setRealValue('Vdc_pu', Ppcc_%i)\n"%(i,i))

#Getting Simulation Results
file1.write("    # Get simulation results\n")
file1.write("    res_time.append( time )\n")
file1.write("    res_G1_angle.append( fmu.getRealValue( 'ElmSym.G1.s:xphi' )*180.0/3.1415 )\n")
file1.write("    res_G2_angle.append( fmu.getRealValue( 'ElmSym.G2.s:xphi' )*180.0/3.1415 )\n")
file1.write("    res_G1_speed.append( fmu.getRealValue( 'ElmSym.G1.s:xspeed' ))\n")
file1.write("    res_G2_speed.append( fmu.getRealValue( 'ElmSym.G2.s:xspeed' ))\n")
file1.write("    res_Bus9_U.append( fmu.getRealValue( 'ElmTerm.Bus9.m:u' ))\n")
file1.write("    res_TR_P.append( fmu.getRealValue( 'ElmTr2.TWPP.m:Psum:buslv' ))\n")

for i in range(1,33):
    file1.write("    res_WT_%i_P_%i.append( fmu.getRealValue( 'ElmGenstat.WT_%i.m:P:bus1' ) )\n"%(i,i,i))
    file1.write("    res_Bus_U_%i.append( fmu.getRealValue( 'ElmGenstat.WT_%i.m:u1:bus1' ) )\n"%(i,i))
    file1.write("    res_Id_ref_%i.append(controllerOut_%i)\n"%(i,i))
    file1.write("    res_Iq_ref_%i.append(iq_out_%i)\n"%(i,i))
    file1.write("    res_I_out_%i.append(np.sqrt(controllerOut_%i**2 + iq_out_%i**2))\n"%(i,i,i))
    file1.write("    res_State_%i.append(fmuF_%i.getRealValue('STATE'))\n"%(i,i))
    file1.write("    res_CLM_%i.append(fmuF_%i.getRealValue('CLMODE'))\n"%(i,i))
    file1.write("    res_Vdiff_%i.append(Vdef_%i)\n"%(i,i))
    file1.write("    res_Vpcc_%i.append(Vpcc_%i)\n"%(i,i))
    file1.write("    res_Vnom_%i.append(Vnom_%i)\n"%(i,i))

#Writing results
file1.write("# uncomment code below to write simulation results to a csv file\n")
file1.write("with open(EXPname, 'wb') as f:\n")
file1.write("    writer = csv.writer(f)\n")
file1.write("    writer.writerow(res_time)\n")
file1.write("    writer.writerow(res_G1_angle)\n")
file1.write("    writer.writerow(res_G2_angle)\n")
file1.write("    writer.writerow(res_G2_speed)\n")
file1.write("    writer.writerow(res_G1_speed)\n")
file1.write("    writer.writerow(res_Bus9_U)\n")
file1.write("    writer.writerow(res_TR_P)\n")

for i in range(1,33):
    file1.write("    writer.writerow(res_WT_%i_P_%i)\n"%(i,i))
    file1.write("    writer.writerow(res_Id_ref_%i)\n"%(i))
    file1.write("    writer.writerow(res_Iq_ref_%i)\n"%(i))
    file1.write("    writer.writerow(res_I_out_%i)\n"%(i))
    file1.write("    writer.writerow(res_Bus_U_%i)\n"%(i))
    file1.write("    writer.writerow(res_State_%i)\n"%(i))
    file1.write("    writer.writerow(res_CLM_%i)\n"%(i))
    file1.write("    writer.writerow(res_Vdiff_%i)\n"%(i))
    file1.write("    writer.writerow(res_Vpcc_%i)\n"%(i))
    file1.write("    writer.writerow(res_Vnom_%i)\n"%(i))

#Plotting Results
file1.write("# Plot the results.\n")
file1.write("plt.figure(1)\n")
file1.write("plt.plot( res_time, res_G1_angle, 'b-',linewidth=4 )\n")
file1.write("plt.plot( res_time, res_G2_angle, 'r-',linewidth=4 )\n")
file1.write("plt.xlabel( 'simulation time in s' )\n")
file1.write("plt.ylabel( 'relative rotor angle of G1 and G2 in deg' )\n")
file1.write("plt.xlim( 0., stop_time)\n")
file1.write("plt.ylim( -100., 100. )\n")
file1.write('plt.legend(["G1","G2"] )\n')
file1.write(r"plt.savefig(r'C:\Users\LocalAdmin\Desktop\WG-TC1\Upscaledfinal\Upscaled_final Plots\fig1.png')")
file1.write("\n")

file1.write("plt.figure(2)\n")
file1.write("plt.plot( res_time, res_G1_speed, 'b-',linewidth=4 )\n")
file1.write("plt.plot( res_time, res_G2_speed, 'r-',linewidth=4 )\n")
file1.write("plt.xlabel( 'simulation time in s' )\n")
file1.write("plt.ylabel( 'rotor speed deviation of G1 and G2 in pu' )\n")
file1.write("plt.xlim( 0., stop_time)\n")
file1.write("plt.ylim( 0.95 , 1.05 )\n")
file1.write('plt.legend(["G1","G2"] )\n')
file1.write(r"plt.savefig(r'C:\Users\LocalAdmin\Desktop\WG-TC1\Upscaledfinal\Upscaled_final Plots\fig2.png')")
file1.write("\n")

count = 3
for i in range(1,33):
    file1.write("plt.figure(%i)\n"%(count))
    file1.write("plt.plot( res_time, res_WT_%i_P_%i, 'b-',linewidth=4 )\n"%(i,i))
    file1.write("plt.xlabel( 'simulation time in s' )\n")
    file1.write("plt.ylabel( 'wind turbine %i output power' )\n"%(i))
    file1.write("plt.xlim( 0., stop_time)\n")
    #file1.write("plt.ylim( 0., 140. )\n")
    file1.write(r"plt.savefig(r'C:\Users\LocalAdmin\Desktop\WG-TC1\Upscaledfinal\Upscaled_final Plots\fig%i.png')"%(count))
    file1.write("\n")
    count+=1

    file1.write("plt.figure(%i)\n"%(count))
    file1.write("plt.plot( res_time, res_Bus_U_%i, 'r-',linewidth=4)\n"%(i))
    file1.write("plt.xlabel( 'simulation time in s' )\n")
    file1.write("plt.ylabel( 'voltage at Bus GEN (WT_%i) in p.u.' )\n"%(i))
    #file1.write("plt.ylim( 0., 1.5 )\n")
    file1.write("plt.xlim( 0., stop_time)\n")
    file1.write(r"plt.savefig(r'C:\Users\LocalAdmin\Desktop\WG-TC1\Upscaledfinal\Upscaled_final Plots\fig%i.png')"%(count))
    file1.write("\n")
    count+=1

    file1.write("plt.figure(%i)\n"%(count))
    file1.write("plt.plot( res_time, res_Id_ref_%i, 'r-',linewidth=4)\n"%(i))
    file1.write("plt.plot( res_time, res_Iq_ref_%i, 'b-',linewidth=4)\n"%(i))
    file1.write("plt.plot( res_time, res_I_out_%i, 'k-',linewidth=4)\n"%(i))
    file1.write("plt.xlabel( 'simulation time in s' )\n")
    file1.write("plt.ylabel( 'WT_%i Currents in p.u.' )\n"%(i))
    file1.write("plt.xlim( 0., stop_time)\n")
    file1.write('plt.legend(["Id","Iq","Iout"] )\n')
    file1.write(r"plt.savefig(r'C:\Users\LocalAdmin\Desktop\WG-TC1\Upscaledfinal\Upscaled_final Plots\fig%i.png')"%(count))
    file1.write("\n")
    count+=1

    file1.write("plt.figure(%i)\n"%(count))
    file1.write("plt.plot( res_time, res_State_%i, 'b-',linewidth=4 )\n"%(i))
    file1.write("plt.xlabel( 'simulation time in s' )\n")
    file1.write("plt.ylabel( 'converter FRT state of WT_%i' )\n"%(i))
    file1.write("plt.xlim( 0., stop_time)\n")
    #file1.write("plt.ylim( -1., 5. )\n")
    file1.write(r"plt.savefig(r'C:\Users\LocalAdmin\Desktop\WG-TC1\Upscaledfinal\Upscaled_final Plots\fig%i.png')"%(count))
    file1.write("\n")
    count+=1

    file1.write("plt.figure(%i)\n"%(count))
    file1.write("plt.plot( res_time, res_CLM_%i, 'r-',linewidth=4)\n"%(i))
    file1.write("plt.xlabel( 'simulation time in s' )\n")
    file1.write("plt.ylabel( 'converter current limiting mode of WT_%i' )\n"%(i))
    file1.write("plt.xlim( 0., stop_time)\n")
    file1.write(r"plt.savefig(r'C:\Users\LocalAdmin\Desktop\WG-TC1\Upscaledfinal\Upscaled_final Plots\fig%i.png')"%(count))
    file1.write("\n")
    count+=1

    file1.write("plt.figure(%i)\n"%(count))
    file1.write("plt.plot( res_time, res_Vdiff_%i, 'r-',linewidth=4)\n"%(i))
    file1.write("plt.plot( res_time, res_Vpcc_%i, 'b-',linewidth=4)\n"%(i))
    file1.write("plt.plot( res_time, res_Vnom_%i, 'k-',linewidth=4)\n"%(i))
    file1.write("plt.xlabel( 'simulation time in s' )\n")
    file1.write("plt.ylabel( 'Voltages of WT_%i' )\n"%(i))
    file1.write("plt.xlim( 0., stop_time)\n")
    file1.write('plt.legend(["Vdiff","Vpcc","Vnom"] )\n')
    file1.write(r"plt.savefig(r'C:\Users\LocalAdmin\Desktop\WG-TC1\Upscaledfinal\Upscaled_final Plots\fig%i.png')"%(count))
    file1.write("\n")
    count+=1

#file1.write("plt.show()\n")

file1.close()


text:
import os
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
from mpl_toolkits.mplot3d import Axes3D
from sklearn.preprocessing import StandardScaler
from sklearn.decomposition import PCA
from sklearn.decomposition import KernelPCA
from sklearn.manifold import MDS
from sklearn.manifold import TSNE
pd.set_option('display.width', 999)

dataset_path = "../res/dataset_virtuale.csv"

def initDataframe():
    # Carico il dataser all'interno di un dataFrame
    dataset = pd.read_csv(dataset_path, index_col=False)
    datasetWithNan = pd.read_csv(dataset_path)
    # Converto i valori NaN con la media
    for column in dataset.columns:
        dataset[column] = pd.to_numeric(dataset[column])
        replace_with = dataset[column].mean()
        dataset[column].fillna(replace_with, inplace=True)
    # Split
    X = dataset.drop("Class", axis=1)
    Y = dataset["Class"]
    # Standardizzazione
    ss = StandardScaler()
    X = ss.fit_transform(X)
    return X, Y, datasetWithNan

def PCA_2D():
    # Eseguo la PCA
    pca = PCA(n_components=2)
    X_pca = pca.fit_transform(X)

    """
    # Ricerca dei data point appartenenti al cluster lineare centrale
    listFilter = []
    dataset_np = datasetWithNan.to_numpy()
    for i in range(X_pca.shape[0]):
        if(X_pca[i][0] > 7 and X_pca[i][0] < 10):
            listFilter.append(dataset_np[i])
    dataFilter_np = np.array(listFilter)
    pd.DataFrame(data=dataFilter_np, columns=['Anemia', 'Osteopenia', 'Diarrea Cronica', 'Mancata Crescita', 'Disturbi Genetici',
                                              'Madre Celiaca', 'POCT', 'IGA_totali', 'TTG_IGG', 'TTG_IGA', 'Esami del sangue', 'Class']).to_csv("dataset_filtrato.csv", index=False)
    """

    # Plot 2D
    plt.scatter(X_pca[:, 0], X_pca[:, 1], c=colormap[Y], alpha=alpha, edgecolors=edgecolors, linewidth=linewidth)
    plt.legend(handles=[pop_a, pop_b], loc='upper right')
    plt.title("PCA con n = 2 componenti")
    plt.xlabel("Prima componente principale")
    plt.ylabel("Seconda componente principale")
    plt.show()
    plt.savefig("../Articolo/img/PCA_2D.png")

def PCA_2D_with_size():
    # Eseguo la PCA
    pca = PCA(n_components=3)
    X_pca = pca.fit_transform(X)
    maximum = max(X_pca[:, 2])
    for i in range(len(X_pca)):
        jittered_x = np.random.rand() * 2 - 1
        X_pca[i][0] += jittered_x
        jittered_y = np.random.rand() * 1 - 0.50
        X_pca[i][1] += jittered_y
        X_pca[i][2] += abs(2 * min(X_pca[:, 2]))
        X_pca[i][2] *= (300 / maximum)
    # Plot 2D
    plt.scatter(X_pca[:, 0], X_pca[:, 1], s=X_pca[:, 2], c=colormap[Y],
                alpha=alpha, edgecolors=edgecolors, linewidth=linewidth)
    plt.legend(handles=[pop_a, pop_b], loc='upper right')
    plt.title("PCA con n = 3 componenti")
    plt.xlabel("Prima componente principale")
    plt.ylabel("Seconda componente principale")
    plt.show()
    plt.savefig("../Articolo/img/PCA_2D_with_size.png")

def PCA_i3D():
    # Eseguo la PCA
    pca = PCA(n_components=3)
    X_pca = pca.fit_transform(X)
    # Plot 3D
    fig = plt.figure()
    ax = fig.add_subplot(111, projection='3d')
    ax.scatter(X_pca[:, 0], X_pca[:, 1], X_pca[:, 2], c=colormap[Y], marker='o',
               alpha=alpha, edgecolors=edgecolors, linewidth=linewidth)
    plt.legend(handles=[pop_a, pop_b], loc='upper right')
    plt.title("PCA con n = 3 componenti")
    ax.set_xlabel("Prima componente principale")
    ax.set_ylabel("Seconda componente principale")
    ax.set_zlabel("Terza componente principale")
    plt.show()
    plt.savefig("../Articolo/img/PCA_i3D.png")

def PCA_SPLOM(n_params):
    # Eseguo la PCA
    pca = PCA(n_components=n_params)
    X_pca = pca.fit_transform(X)
    # Creo il dizionario dei componenti
    components = {}
    for i in range(n_params):
        components.update({'Componente{0}'.format(i + 1): X_pca[:, i]})
    # Converto il numpy array in un dataframe
    dataframe = pd.DataFrame(components)
    dataframe["Class"] = Y
    dataframe["Class"].replace(to_replace=0, value="Non Celiaco", inplace=True)
    dataframe["Class"].replace(to_replace=1, value="Celiaco", inplace=True)
    # SPLOM plot
    sns.set(style="ticks")
    sns.pairplot(dataframe, hue="Class", palette={"Non Celiaco": blue, "Celiaco": yellow}, plot_kws=dict(
        edgecolor=edgecolors, linewidth=linewidth, alpha=alpha))
    plt.show()
    plt.savefig("../Articolo/img/PCA_SPLOM.png")

def PCA_components_interpretation():
    pca = PCA().fit(X)
    components = pd.DataFrame(pca.components_)
    features = ["Anemia", "Osteopenia", "Diarrea Cronica", "Mancata Crescita", "Disturbi Genetici",
                "Madre Celiaca", "POCT", "IGA_totali", "TTG_IGG", "TTG_IGA", "Esami del sangue"]
    columns = dict()
    rows = dict()
    for i in range(0, len(components)):
        rows[i] = "PCA" + str(i + 1)
        columns[i] = features[i]
    components.rename(index=rows, columns=columns, inplace=True)
    print(pd.DataFrame(components.transpose()))

def PCA_generate_file(n_params):
    pca = PCA(n_components=n_params)
    X_PCA = pca.fit_transform(X)
    X_PCA = pd.DataFrame(data=X_PCA, columns=["Componente" + str(i) for i in range(1, len(X_PCA[0]) + 1)])
    X_PCA["Class"] = Y
    X_PCA.to_csv("../res/dataset_virtuale_PCA.csv", index=False)

def kernelPCA_2D():
    # Eseguo il KernelPCA
    kernelPCA = KernelPCA(n_components=2, kernel="sigmoid")
    X_kernelPCA = kernelPCA.fit_transform(X)
    # Plot 2D
    plt.scatter(X_kernelPCA[:, 0], X_kernelPCA[:, 1], c=colormap[Y],
                alpha=alpha, edgecolors=edgecolors, linewidth=linewidth)
    plt.legend(handles=[pop_a, pop_b], loc='upper right')
    plt.title("kernelPCA con n = 2 componenti")
    plt.xlabel("Prima componente principale")
    plt.ylabel("Seconda componente principale")
    plt.show()
    plt.savefig("../Articolo/img/kernelPCA_2D.png")

def kernelPCA_2D_with_size():
    # Eseguo la t-SNE
    kernelPCA = KernelPCA(n_components=3, kernel='sigmoid')
    X_kernelPCA = kernelPCA.fit_transform(X)
    minimum = min(X_kernelPCA[:, 2])
    maximum = max(X_kernelPCA[:, 2])
    for i in range(len(X_kernelPCA)):
        jittered_x = np.random.rand()
        X_kernelPCA[i][0] += jittered_x
        jittered_y = np.random.rand()
        X_kernelPCA[i][1] += jittered_y
        X_kernelPCA[i][2] += abs(2 * minimum)
        X_kernelPCA[i][2] *= (300 / maximum)
    # Plot 2D
    plt.scatter(X_kernelPCA[:, 0], X_kernelPCA[:, 1], s=X_kernelPCA[:, 2],
                c=colormap[Y], alpha=alpha, edgecolors=edgecolors, linewidth=linewidth)
    plt.legend(handles=[pop_a, pop_b], loc='upper right')
    plt.title("kernelPCA con n = 3 componenti")
    plt.xlabel("Prima componente principale")
    plt.ylabel("Seconda componente principale")
    plt.show()
    plt.savefig("../Articolo/img/kernelPCA_2D_with_size.png")

def kernelPCA_i3D():
    # Eseguo il kernelPCA
    kernelPCA = KernelPCA(n_components=3, kernel="sigmoid")
    X_kernelPCA = kernelPCA.fit_transform(X)
    # Plot 3D
    fig = plt.figure()
    ax = fig.add_subplot(111, projection='3d')
    ax.scatter(X_kernelPCA[:, 0], X_kernelPCA[:, 1], X_kernelPCA[:, 2], c=colormap[Y],
               marker='o', alpha=alpha, edgecolors=edgecolors, linewidth=linewidth)
    plt.legend(handles=[pop_a, pop_b], loc='upper right')
    plt.title("kernelPCA con n = 3 componenti")
    ax.set_xlabel("Prima componente principale")
    ax.set_ylabel("Seconda componente principale")
    ax.set_zlabel("Terza componente principale")
    plt.show()
    plt.savefig("../Articolo/img/kernelPCA_i3D.png")

def kernelPCA_SPLOM(n_params):
    # Eseguo la kernelPCA
    kernelPCA = KernelPCA(n_components=n_params)
    X_kernelPCA = kernelPCA.fit_transform(X)
    # Creo il dizionario dei componenti
    components = {}
    for i in range(n_params):
        components.update({'Componente{0}'.format(i + 1): X_kernelPCA[:, i]})
    # Converto il numpy array in un dataframe
    dataframe = pd.DataFrame(components)
    dataframe["Class"] = Y
    dataframe["Class"].replace(to_replace=0, value="Non Celiaco", inplace=True)
    dataframe["Class"].replace(to_replace=1, value="Celiaco", inplace=True)
    # SPLOM plot
    sns.set(style="ticks")
    sns.pairplot(dataframe, hue="Class", palette={"Non Celiaco": blue, "Celiaco": yellow}, plot_kws=dict(
        edgecolor=edgecolors, linewidth=linewidth, alpha=alpha))
    plt.show()
    plt.savefig("../Articolo/img/kernelPCA_SPLOM.png")

def kernelPCA_generate_file(n_params):
    kernelPCA = KernelPCA(n_components=n_params, kernel="sigmoid")
    X_kernelPCA = kernelPCA.fit_transform(X)
    X_kernelPCA = pd.DataFrame(data=X_kernelPCA, columns=["Componente" + str(i)
                                                          for i in range(1, len(X_kernelPCA[0]) + 1)])
    X_kernelPCA["Class"] = Y
    X_kernelPCA.to_csv("../res/dataset_virtuale_kernelPCA.csv", index=False)

def MDS_2D():
    # Eseguo MDS
    mds = MDS(n_components=2)
    X_mds = mds.fit_transform(X)
    # Plot 2D
    plt.scatter(X_mds[:, 0], X_mds[:, 1], c=colormap[Y], alpha=alpha, edgecolors=edgecolors, linewidth=linewidth)
    plt.legend(handles=[pop_a, pop_b], loc='upper right')
    plt.title("MDS con n = 2 componenti")
    plt.xlabel("Coordinata X")
    plt.ylabel("Coordinata Y")
    plt.show()
    plt.savefig("../Articolo/img/MDS_2D.png")

def MDS_2D_with_size():
    # Eseguo la MDS
    mds = MDS(n_components=3)
    X_mds = mds.fit_transform(X)
    minimum = min(X_mds[:, 2])
    maximum = max(X_mds[:, 2])
    for i in range(len(X_mds)):
        jittered_x = np.random.rand()
        X_mds[i][0] += jittered_x
        jittered_y = np.random.rand()
        X_mds[i][1] += jittered_y
        X_mds[i][2] += abs(2 * minimum)
        X_mds[i][2] *= (300 / maximum)
    # Plot 2D
    plt.scatter(X_mds[:, 0], X_mds[:, 1], s=X_mds[:, 2], c=colormap[Y],
                alpha=0.5, edgecolors=edgecolors, linewidth=linewidth)
    plt.legend(handles=[pop_a, pop_b], loc='upper right')
    plt.title("MDS con n = 3 componenti")
    plt.xlabel("Coordinata X")
    plt.ylabel("Coordinata Y")
    plt.show()
    plt.savefig("../Articolo/img/MDS_2D_with_size.png")

def MDS_i3D():
    # Eseguo MDS
    mds = MDS(n_components=3)
    X_mds = mds.fit_transform(X)
    # Plot 3D
    fig = plt.figure()
    ax = fig.add_subplot(111, projection='3d')
    ax.scatter(X_mds[:, 0], X_mds[:, 1], X_mds[:, 2], c=colormap[Y], marker='o',
               alpha=alpha, edgecolors=edgecolors, linewidth=linewidth)
    plt.legend(handles=[pop_a, pop_b], loc='upper right')
    plt.title("MDS con n = 3 componenti")
    ax.set_xlabel("Coordinata X")
    ax.set_ylabel("Coordinata Y")
    ax.set_zlabel("Coordinata Z")
    plt.show()
    plt.savefig("../Articolo/img/MDS_i3D.png")

def MDS_SPLOM(n_params):
    # Eseguo la MDS
    mds = MDS(n_components=n_params)
    X_mds = mds.fit_transform(X)
    # Creo il dizionario dei componenti
    components = {}
    for i in range(n_params):
        components.update({'Componente{0}'.format(i + 1): X_mds[:, i]})
    # Converto il numpy array in un dataframe
    dataframe = pd.DataFrame(components)
    dataframe["Class"] = Y
    dataframe["Class"].replace(to_replace=0, value="Non Celiaco", inplace=True)
    dataframe["Class"].replace(to_replace=1, value="Celiaco", inplace=True)
    # SPLOM plot
    sns.set(style="ticks")
    sns.pairplot(dataframe, hue="Class", palette={"Non Celiaco": blue, "Celiaco": yellow}, plot_kws=dict(
        edgecolor=edgecolors, linewidth=linewidth, alpha=alpha))
    plt.show()
    plt.savefig("../Articolo/img/MDS_SPLOM.png")

def MDS_generate_file(n_params):
    mds = MDS(n_components=3)
    X_MDS = mds.fit_transform(X)
    X_MDS = pd.DataFrame(data=X_MDS, columns=["Componente" + str(i) for i in range(1, len(X_MDS[0]) + 1)])
    X_MDS["Class"] = Y
    X_MDS.to_csv("../res/dataset_virtuale_MDS.csv", index=False)

def tSNE_2D():
    # Eseguo il t-SNE
    tsne = TSNE(n_components=2)
    X_tsne = tsne.fit_transform(X)
    # Plot 2D
    plt.scatter(X_tsne[:, 0], X_tsne[:, 1], c=colormap[Y], alpha=alpha, edgecolors=edgecolors, linewidth=linewidth)
    plt.legend(handles=[pop_a, pop_b], loc='upper right')
    plt.title("t-SNE con n = 2 componenti")
    plt.xlabel("Prima Componente")
    plt.ylabel("Seconda Componente")
    plt.show()
    plt.savefig("../Articolo/img/tSNE_2D.png")

def tSNE_2D_with_size():
    # Eseguo la t-SNE
    tsne = TSNE(n_components=3)
    X_tsne = tsne.fit_transform(X)
    minimum = min(X_tsne[:, 2])
    maximum = max(X_tsne[:, 2])
    for i in range(len(X_tsne)):
        jittered_x = np.random.rand()
        X_tsne[i][0] += jittered_x
        jittered_y = np.random.rand()
        X_tsne[i][1] += jittered_y
        X_tsne[i][2] += abs(2 * minimum)
        X_tsne[i][2] *= (300 / maximum)
    # Plot 2D
    plt.scatter(X_tsne[:, 0], X_tsne[:, 1], s=X_tsne[:, 2], c=colormap[Y],
                alpha=alpha, edgecolors=edgecolors, linewidth=linewidth)
    plt.legend(handles=[pop_a, pop_b], loc='upper right')
    plt.title("t-SNE con n = 3 componenti")
    plt.xlabel("Prima Componente")
    plt.ylabel("Seconda Componente")
    plt.show()
    plt.savefig("../Articolo/img/tSNE_2D_with_size.png")

def tSNE_i3D():
    # Eseguo il t-SNE
    tsne = TSNE(n_components=3)
    X_tsne = tsne.fit_transform(X)
    # Plot 3D
    fig = plt.figure()
    ax = fig.add_subplot(111, projection='3d')
    ax.scatter(X_tsne[:, 0], X_tsne[:, 1], X_tsne[:, 2], c=colormap[Y],
               marker='o', alpha=alpha, edgecolors=edgecolors, linewidth=linewidth)
    plt.legend(handles=[pop_a, pop_b], loc='upper right')
    plt.title("t-SNE con n = 3 componenti")
    ax.set_xlabel("Prima Componente")
    ax.set_ylabel("Seconda Componente")
    ax.set_zlabel("Terza Componente")
    plt.show()
    plt.savefig("../Articolo/img/tSNE_i3D.png")

def tSNE_SPLOM(n_params):
    # Eseguo la t-SNE
    tsne = TSNE(n_components=n_params)
    X_tsne = tsne.fit_transform(X)
    # Creo il dizionario dei componenti
    components = {}
    for i in range(n_params):
        components.update({'Componente{0}'.format(i + 1): X_tsne[:, i]})
    # Converto il numpy array in un dataframe
    dataframe = pd.DataFrame(components)
    dataframe["Class"] = Y
    dataframe["Class"].replace(to_replace=0, value="Non Celiaco", inplace=True)
    dataframe["Class"].replace(to_replace=1, value="Celiaco", inplace=True)
    # SPLOM plot
    sns.set(style="ticks")
    sns.pairplot(dataframe, hue="Class", palette={"Non Celiaco": blue, "Celiaco": yellow}, plot_kws=dict(
        edgecolor=edgecolors, linewidth=linewidth, alpha=alpha))
    plt.show()
    plt.savefig("../Articolo/img/tSNE_SPLOM.png")

def tSNE_generate_file(n_params):
    tsne = TSNE(n_components=n_params)
    X_tSNE = tsne.fit_transform(X)
    X_tSNE = pd.DataFrame(data=X_tSNE, columns=["Componente" + str(i) for i in range(1, len(X_tSNE[0]) + 1)])
    X_tSNE["Class"] = Y
    X_tSNE.to_csv("../res/dataset_virtuale_tSNE.csv", index=False)

def scree_plot():
    pca = PCA().fit(X)
    plt.xticks([x for x in range(0, 11)], [x for x in range(1, 12)])
    plt.plot(pca.explained_variance_ratio_)
    plt.xlabel("Numero di componenti")
    plt.ylabel("Autovalore")
    plt.show()
    plt.savefig("../Articolo/img/scree_plot.png")

def menu():
    os.system("clear")
    print("1. PCA")
    print("2. kernel PCA")
    print("3. MDS")
    print("4. t-SNE")
    print("5. Scree-Plot")
    print("0. Exit\n")
    temp = int(input())
    print()
    return temp

def sub_menu():
    os.system("clear")
    print("1. 2D (2 dimensioni)")
    print("2. 2D (3 dimensioni)")
    print("3. i3D")
    print("4. plot di Draftman")
    print("5. generazione dataset ")
    print("0. Exit\n")
    temp = int(input())
    print()
    return temp

blue = "#0000ff"
yellow = "#ffff00"
X, Y, datasetWithNan = initDataframe()
colormap = np.array([blue, yellow])
alpha = 0.5
edgecolors = "black"
pop_a = mpatches.Patch(color=blue, label='Non Celiaco')
pop_b = mpatches.Patch(color=yellow, label='Celiaco')
linewidth = 1
c = -1
while c != 0:
    c = menu()
    if c == 0:
        break
    elif c == 5:
        scree_plot()
    elif (c > 0) and (c < 5):
        sc = sub_menu()
        if c == 1:
            PCA_components_interpretation()
            if sc == 1:
                PCA_2D()
            elif sc == 2:
                PCA_2D_with_size()
            elif sc == 3:
                PCA_i3D()
            elif sc == 4:
                n_params = -1
                while(n_params <= 1 or n_params > 12):
                    print("Numero di parametri: ", end="")
                    n_params = int(input())
                    if(n_params <= 1 or n_params > 12):
                        print("Numero di parametri non ammesso\n")
                PCA_SPLOM(n_params)
            elif sc == 5:
                n_params = -1
                while(n_params <= 1 or n_params > 12):
                    print("Numero di parametri: ", end="")
                    n_params = int(input())
                    if(n_params <= 1 or n_params > 12):
                        print("Numero di parametri non ammesso\n")
                PCA_generate_file(n_params)
            elif c == 0:
                break
            else:
                print("Comando sconosciuto")
        elif c == 2:
            if sc == 1:
                kernelPCA_2D()
            elif sc == 2:
                kernelPCA_2D_with_size()
            elif sc == 3:
                kernelPCA_i3D()
            elif sc == 4:
                n_params = -1
                while(n_params <= 1 or n_params > 12):
                    print("Numero di parametri: ", end="")
                    n_params = int(input())
                    if(n_params <= 1 or n_params > 12):
                        print("Numero di parametri non ammesso\n")
                kernelPCA_SPLOM(n_params)
            elif sc == 5:
                n_params = -1
                while(n_params <= 1 or n_params > 12):
                    print("Numero di parametri: ", end="")
                    n_params = int(input())
                    if(n_params <= 1 or n_params > 12):
                        print("Numero di parametri non ammesso\n")
                kernelPCA_generate_file(n_params)
            elif c == 0:
                break
            else:
                print("Comando sconosciuto")
        elif c == 3:
            if sc == 1:
                MDS_2D()
            elif sc == 2:
                MDS_2D_with_size()
            elif sc == 3:
                MDS_i3D()
            elif sc == 4:
                n_params = -1
                while(n_params <= 1 or n_params > 12):
                    print("Numero di parametri: ", end="")
                    n_params = int(input())
                    if(n_params <= 1 or n_params > 12):
                        print("Numero di parametri non ammesso\n")
                MDS_SPLOM(n_params)
            elif sc == 5:
                n_params = -1
                while(n_params <= 1 or n_params > 12):
                    print("Numero di parametri: ", end="")
                    n_params = int(input())
                    if(n_params <= 1 or n_params > 12):
                        print("Numero di parametri non ammesso\n")
                MDS_generate_file(n_params)
            elif c == 0:
                break
            else:
                print("Comando sconosciuto")
        elif c == 4:
            if sc == 1:
                tSNE_2D()
            elif sc == 2:
                tSNE_2D_with_size()
            elif sc == 3:
                tSNE_i3D()
            elif sc == 4:
                n_params = -1
                while(n_params <= 1 or n_params > 3):
                    print("Numero di parametri: ", end="")
                    n_params = int(input())
                    if(n_params <= 1 or n_params > 3):
                        print("Numero di parametri non ammesso\n")
                tSNE_SPLOM(n_params)
            elif sc == 5:
                n_params = -1
                while(n_params <= 1 or n_params > 3):
                    print("Numero di parametri: ", end="")
                    n_params = int(input())
                    if(n_params <= 1 or n_params > 3):
                        print("Numero di parametri non ammesso\n")
                tSNE_generate_file(n_params)
            elif c == 0:
                break
            else:
                print("Comando sconosciuto")
    else:
        print("Comando sconosciuto")

ext: py

max_stars_repo_name: SimoCnt/VCDD

max_issues_repo_name: SimoCnt/VCDD

max_forks_repo_name: SimoCnt/VCDD

reason: import numpy


text:
import argparse

import h5py
import numpy as np

from config import CHANNELS
from policy import PolicyValueModelResNet as PolicyValueModel

def convert_pretrained_weights(
    src_weights_file,
    dst_weights_file,
    src_width=8,
    dst_width=15,
    src_height=8,
    dst_height=15,
):
    model_src = PolicyValueModel(src_width, src_height)
    model_src.build(input_shape=(None, src_width, src_height, CHANNELS))
    model_src.load_weights(src_weights_file)

    model_dst = PolicyValueModel(dst_width, dst_height)
    model_dst.build(input_shape=(None, dst_width, dst_height, CHANNELS))

    assert len(model_src.cnn_layers) == len(model_dst.cnn_layers)
    for i in range(len(model_src.cnn_layers)):
        layer_src = model_src.cnn_layers[i]
        layer_dst = model_dst.cnn_layers[i]
        layer_dst.set_weights(layer_src.get_weights())

    model_dst.save_weights(dst_weights_file)

def convert_pretrained_buffer(
    src_buffer_file,
    dst_buffer_file,
    src_width=8,
    dst_width=15,
    src_height=8,
    dst_height=15,
):
    assert dst_height >= src_height
    assert dst_width >= src_width
    f_src = h5py.File(src_buffer_file, "r")
    f_dst = h5py.File(dst_buffer_file, "w")

    states_src = f_src["states"][...]
    mcts_probs_src = f_src["mcts_probs"][...]

    buffer_length = states_src.shape[0]
    start_width_idx = (dst_width - src_width) // 2
    start_height_idx = (dst_height - src_height) // 2

    states_dst = np.zeros(
        shape=(buffer_length, dst_width, dst_height, CHANNELS),
        dtype=states_src.dtype,
    )
    states_dst[
        :,
        start_width_idx : start_width_idx + src_width,
        start_height_idx : start_height_idx + src_height,
    ] = states_src[:]

    # 最后一根轴只能是全 1 或全 0
    states_dst[:, :, :, -1] = states_src[:, 0:1, 0:1, -1]

    mcts_probs_dst = np.zeros(
        shape=(buffer_length, dst_width * dst_width),
        dtype=mcts_probs_src.dtype,
    )
    mcts_probs_dst = mcts_probs_dst.reshape((buffer_length, dst_width, dst_width))
    mcts_probs_dst[
        :,
        start_width_idx : start_width_idx + src_width,
        start_height_idx : start_height_idx + src_height,
    ] = mcts_probs_src[:].reshape((buffer_length, src_width, src_width))
    mcts_probs_dst = mcts_probs_dst.reshape((buffer_length, dst_width * dst_width))

    f_dst["states"] = states_dst
    f_dst["mcts_probs"] = mcts_probs_dst
    f_dst["rewards"] = f_src["rewards"][...]

    f_src.close()
    f_dst.close()

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="Gomoku AlphaZero Weights Converter")
    parser.add_argument("--src-width", default=8, type=int, help="源棋盘水平宽度")
    parser.add_argument("--src-height", default=8, type=int, help="源棋盘竖直宽度")
    parser.add_argument("--dst-width", default=15, type=int, help="目标棋盘水平宽度")
    parser.add_argument("--dst-height", default=15, type=int, help="目标棋盘竖直宽度")
    parser.add_argument("--src-weights", default="./data/model-8x8#5.h5", help="源预训练权重存储位置")
    parser.add_argument("--dst-weights", default="./data/model-15x15#5.h5", help="目标预训练权重存储位置")
    parser.add_argument("--src-buffer", default="./data/buffer-8x8#5.h5", help="源经验池存储位置")
    parser.add_argument("--dst-buffer", default="./data/buffer-15x15#5.h5", help="目标经验池存储位置")
    args = parser.parse_args()

    # 小棋盘预训练数据迁移到大棋盘
    convert_pretrained_weights(
        args.src_weights,
        args.dst_weights,
        src_width=args.src_width,
        dst_width=args.dst_width,
        src_height=args.src_height,
        dst_height=args.dst_height,
    )

    convert_pretrained_buffer(
        args.src_buffer,
        args.dst_buffer,
        src_width=args.src_width,
        dst_width=args.dst_width,
        src_height=args.src_height,
        dst_height=args.dst_height,
    )

max_stars_repo_name: zsync/gomoku-alphazero

max_issues_repo_name: zsync/gomoku-alphazero

max_forks_repo_name: zsync/gomoku-alphazero


text:
#!/usr/bin/env python3
#
# Synthesis-based resolution of features/enforcers interactions in CPS
# Copyright 2020 Carnegie Mellon University.
# NO WARRANTY. THIS CARNEGIE MELLON UNIVERSITY AND SOFTWARE ENGINEERING
# INSTITUTE MATERIAL IS FURNISHED ON AN "AS-IS" BASIS. CARNEGIE MELLON
# UNIVERSITY MAKES NO WARRANTIES OF ANY KIND, EITHER EXPRESSED OR IMPLIED,
# AS TO ANY MATTER INCLUDING, BUT NOT LIMITED TO, WARRANTY OF FITNESS FOR
# PURPOSE OR MERCHANTABILITY, EXCLUSIVITY, OR RESULTS OBTAINED FROM USE OF
# THE MATERIAL. CARNEGIE MELLON UNIVERSITY DOES NOT MAKE ANY WARRANTY OF ANY
# KIND WITH RESPECT TO FREEDOM FROM PATENT, TRADEMARK, OR COPYRIGHT
# INFRINGEMENT.
# Released under a BSD (SEI)-style license, please see license.txt or contact
# permission@sei.cmu.edu for full terms.
# [DISTRIBUTION STATEMENT A] This material has been approved for public
# release and unlimited distribution.  Please see Copyright notice for
# non-US Government use and distribution.
# This Software includes and/or makes use of the following Third-Party Software
# subject to its own license:
# 1. JsonCpp
# (https://github.com/open-source-parsers/jsoncpp/blob/master/LICENSE)
# Copyright 2010 Baptiste Lepilleur and The JsonCpp Authors.
# DM20-0762
#

import sys
import os
import numpy as np
import pathlib
import itertools
import random
import json

# Assume ego starts 0,0 always
# Just relative to ego at varying distances
enemy_start_positions = [
    '5,5',
    '5,0',
    '0,5',
    '-5,5',
    '5,-5',
    '-5,-5',
    '-5,0',
    '0,-5',

    '1,1',
    '1,0',
    '0,1',
    '-1,1',
    '1,-1',
    '-1,-1',
    '-1,0',
    '0,-1',

    '10,10',
    '10,0',
    '0,10',
    '-10,10',
    '10,-10',
    '-10,-10',
    '-10,0',
    '0,-10',
    ]

# Config values that will be changed
config_vals = {
    'ENEMY_DRONE_SPEED'    : [x for x in np.arange(1.2, 2.1, 0.1)],
    'WAYPOINT_SEED'        : [x for x in range(0, 999)], # Kinda dumb way to do this space-wise but it's fine
    'BOUNDARY_SIZE'        : [x for x in np.arange(10, 30, 1)],
    #        'SUGGEST_ACTION_RANGE' : [0,1]
}

# FLIGHT_WEIGHT stays constant
weight_vals = [
    # Equal weights
    {'BOUNDARY_WEIGHT' : 1,'RUNAWAY_WEIGHT'  : 1,  'MISSILE_WEIGHT'  : 1},

    # 1 : 1.5 : 2
    {'BOUNDARY_WEIGHT' : 1,'RUNAWAY_WEIGHT'  : 1.5,'MISSILE_WEIGHT'  : 2},
    {'BOUNDARY_WEIGHT' : 1,'MISSILE_WEIGHT'  : 1.5,'RUNAWAY_WEIGHT'  : 2},

    {'RUNAWAY_WEIGHT' : 1,'BOUNDARY_WEIGHT'  : 1.5,'MISSILE_WEIGHT'  : 2},
    {'RUNAWAY_WEIGHT' : 1,'MISSILE_WEIGHT'   : 1.5,'BOUNDARY_WEIGHT' : 2},

    {'MISSILE_WEIGHT' : 1,'RUNAWAY_WEIGHT'   : 1.5,'BOUNDARY_WEIGHT' : 2},
    {'MISSILE_WEIGHT' : 1,'BOUNDARY_WEIGHT'  : 1.5,'RUNAWAY_WEIGHT'  : 2},

    # 1 : 2 : 3
    {'BOUNDARY_WEIGHT' : 1,'RUNAWAY_WEIGHT'  : 2,'MISSILE_WEIGHT'  : 3},
    {'BOUNDARY_WEIGHT' : 1,'MISSILE_WEIGHT'  : 2,'RUNAWAY_WEIGHT'  : 3},

    {'RUNAWAY_WEIGHT' : 1,'BOUNDARY_WEIGHT'  : 2,'MISSILE_WEIGHT'  : 3},
    {'RUNAWAY_WEIGHT' : 1,'MISSILE_WEIGHT'   : 2,'BOUNDARY_WEIGHT' : 3},

    {'MISSILE_WEIGHT' : 1,'RUNAWAY_WEIGHT'   : 2,'BOUNDARY_WEIGHT' : 3},
    {'MISSILE_WEIGHT' : 1,'BOUNDARY_WEIGHT'  : 2,'RUNAWAY_WEIGHT'  : 3},
]

def make_config_file(base_config_file, outfile, vals):
    # Open input and output file
    with open(base_config_file, 'r') as base, open(outfile, 'w') as out:
        # Convert to list by space delim
        for line in base:
            line_lst = line.split(' ')

            # If this var is one we change, then write what's stored in vars
            if(line_lst[0] in vals):
                # Handle the case that it's a float differently bc annoying precision
                if isinstance(line_lst[0], np.float64):
                    out.write(line_lst[0] + ' ' + '{:.2f}'.format(vals[line_lst[0]]) + '\n')
                else:
                    out.write(line_lst[0] + ' ' + str(vals[line_lst[0]]) + '\n')
            # If this var is not one we change, write it as is
            else:
                out.write(line)

def default(o):
    if isinstance(o, np.int64): return int(o)
    raise TypeError

def make_files(config, rootdir, enemy_start_positions, num_configurations):
    newdir = ''

    # They're all the same at this point -- just get the vals for any coordinator
    vals = config["RobustnessCoordinator"]

    # Get all the combinations of variable-values we have
    combinations = [dict((zip(vals.keys(), t))) for t in itertools.product(*vals.values())]

    sample_size = num_configurations

    # Get a random sample of the combinations
    comb_sample = random.sample(combinations, sample_size)

    # Randomly assign weights to each case
    for entry in comb_sample:
        weights = random.choice(weight_vals)
        entry.update(weights)

    for coordinator in config:
        try:
            newdir = rootdir+'/'+coordinator
            os.makedirs(newdir, exist_ok=True)
        except OSError:
            print("Failed to create directory: %s" % newdir)

        i=0

        # Create a directory and a corresponding config file for each test case
        for entry in comb_sample:

            # Everything else is random so this is fine
            enemy_start_pos_str = enemy_start_positions[i%len(enemy_start_positions)]
            i = i+1

            # Need to add this manually bc it's not in config file params
            controlled_vars = entry.copy();
            controlled_vars["enemy_strt_pos"] = enemy_start_pos_str;

            dirname = ''
            for name in entry:
                if isinstance(entry[name], np.float64):
                    dirname+=name+'{:.2f}'.format(entry[name])+'-'
                else:
                    dirname+=name+str(entry[name])+'-'

            # trim the hyphen off the end
            dirname = dirname[0:-1]

            try:
                os.makedirs(rootdir+'/'+coordinator+'/'+dirname, exist_ok=True)
            except OSError:
                print("Failed to create directory: %s" % rootdir+'/'+coordinator+'/'+dirname)

            # Write config file
            make_config_file('./drone.cfg', rootdir+coordinator+'/'+dirname+'/'+'drone.cfg', entry)

            with open(rootdir+coordinator+'/'+dirname+'/'+'controlled_vars.json', 'w') as controlled_varsfile:
                json.dump(controlled_vars, controlled_varsfile, default=default)
            # Write positions
            with open(rootdir+coordinator+'/'+dirname+'/'+'enemy_start_pos', 'wb') as posfile:
                posfile.write(enemy_start_pos_str.encode('utf-8'))

def main():
    if(len(sys.argv) != 3):
        print("Usage: generate_tests.py <test_dir> <num_configurations>")
        exit()
    cwd = os.getcwd()
    os.makedirs(sys.argv[1], exist_ok=True)
    num_configurations = int(sys.argv[2])
    rootdir = cwd+'/'+sys.argv[1]+'/'

    if((cwd.split('/'))[-1] != 'missionapp'):
        print("Must be in missionapp directory to use this script. Given %s\n" % cwd)
        print(cwd.split('/'))
        exit()

    base_config_file = 'drone.cfg'

    coordinators = ['PriorityCoordinator', 'RobustnessCoordinator', 'SynthRobustnessCoordinator']

    config = { coord : config_vals.copy() for coord in coordinators }

    make_files(config, rootdir, enemy_start_positions, num_configurations)

if __name__ == "__main__":
    main()

max_stars_repo_name: cps-sei/cps-synth-resolultion

max_issues_repo_name: cps-sei/cps-synth-resolultion

max_forks_repo_name: cps-sei/cps-synth-resolultion


text:
#!/usr/bin/env python3
import numpy as np
import nori2

def main():

if __name__ == "__main__":
    main()
# vim: ts=4 sw=4 sts=4 expandtab


text:
from torch.utils.data.dataset import Dataset
import numpy as np

class MyDataSet(Dataset):
    """Dataset wrapping images and target labels for Kaggle - Planet Amazon from Space competition.

    Arguments:
        A CSV file path
        Path to image folder
        Extension of images
        PIL transforms
    """

    def __init__(self, npz_path = 'data.npz', train=True, transform=None, target_transform=None):
        self.train = train
        self.transform = transform
        self.target_transform = target_transform
        self.npz_path = npz_path
        npz_file = np.load(npz_path)

        # if self.train:
        self.train_X = np.array(npz_file['train_X'].astype(np.uint8))
        self.train_y = np.array(npz_file['train_y'].astype(np.int))
        # self.train_y = self.train_y.reshape(self.train_y.shape[0], 1)

        self.train_X = self.train_X.reshape(self.train_X.shape[0],1, 64, 64).transpose((0, 2, 3, 1))
        # else:
        self.test_X = np.array(npz_file['test_X'].astype(np.uint8))
        self.test_X = self.test_X.reshape(self.test_X.shape[0], 1, 64, 64).transpose((0, 2, 3, 1))

    def __getitem__(self, index):
        """
        Args:
            index (int): Index

        Returns:
            tuple: (image, target) where target is index of the target class.
        """
        if self.train:
            img, target = self.train_X[index], self.train_y[index]
        else:
            img, target = self.test_X[index], self.train_y[index] # notice the test_y don't filled here ,you cannot use the to predict

        # doing this so that it is consistent with all other datasets
        # to return a PIL Image
        # img = Image.fromarray(img)
        # 草拟吗啊，要转坐标轴啊 N * C * H * W

        if self.transform is not None:
            img = self.transform(img)
        else:
            img = img.transpose(2, 0, 1)

        if self.target_transform is not None:
            target = self.target_transform(target)

        return img, target

    def __len__(self):
        if self.train:
            return len(self.train_X)
        else:
            return len(self.test_X)


text:
#!/home/yy/library/anaconda3/bin/python
import sys
from pathlib import Path
import ctypes
# _cudart = ctypes.CDLL('libcudart.so')

print(str(Path(__file__).parent.parent.parent.parent))
sys.path.append(str(Path(__file__).parent.parent.parent.parent))

from spconv import tensorview as tv

from spconv.sparse import build
import numpy as np
from pathlib import Path
from spconv.spconv_ops_cc.sparse.all.ops import Point2Voxel
from spconv.spconv_ops_cc.sparse.all import SpconvOps

import time

def main():
    data = np.load("/home/yy/OneDrive/dev/spconv/test/data/benchmark-pc.npz")["pc"].astype(np.float32)
    print(data.shape, data.dtype)
    p2v = Point2Voxel([0.1, 0.1, 0.1], [-80, -80, -2, 80, 80, 6], 3, 150000, 1)
    gs = p2v.grid_size # zyx
    print(gs)
    # return
    data_tv = tv.from_numpy(data).cuda()
    for i in range(6):
        t = time.time()

        voxels, indices, num_per_voxel = p2v.point_to_voxel_hash(data_tv)

        print(time.time() - t)
    voxels, indices, num_per_voxel = p2v.point_to_voxel_hash(data_tv)
    print(voxels.shape, gs)
    gs_xyz = gs
    indices_np = indices.cpu().numpy()
    # indices_offset = indices_np[:, 0] * gs_xyz[1] * gs_xyz[2] + indices_np[:, 1] * gs_xyz[2] + indices_np[:, 2]
    # uq = np.unique(indices_offset)
    # print(uq.shape, indices_offset.shape, gs_xyz)
    # return
    ksize = [3] * 3
    kv = int(np.prod(ksize))
    indices_with_bs = np.zeros((indices_np.shape[0], 4), dtype=np.int32)
    indices_with_bs[:, 1:] = indices_np
    print(indices_with_bs.mean(), indices_with_bs.max(), indices_with_bs.min())

    indices = tv.from_numpy(indices_with_bs).cuda()
    out_indices = tv.zeros([indices.dim(0) * kv, 4], tv.int32, 0)
    indice_num_per_loc = tv.zeros([kv], tv.int32, 0)

    points = voxels.view([-1, 3])
    hashdata = tv.zeros([points.dim(0) * kv * 2], tv.custom64, 0)
    hashdata_subm = tv.zeros([points.dim(0) * 2], tv.custom64, 0)

    indice_pairs = tv.full([2, kv, indices.dim(0)], -1, tv.int32, 0)
    indice_pairs_uniq = tv.zeros([indice_pairs.size // 2 + 1], tv.int32, 0)

    # for i in range(10):
    #     indice_pairs.fill_int_(-1)
    #     np.random.shuffle(indices_with_bs)
    #     indices = tv.from_numpy(indices_with_bs).cuda()

    #     indice_num_per_loc.zero_()
    #     out_act = SpconvOps.generate_conv_inds(indices, hashdata, indice_pairs,
    #         indice_pairs_uniq, out_indices, indice_num_per_loc,
    #         1, gs, gs, [3, 3, 3], [1, 1, 1], [1, 1, 1], [1, 1, 1])
    #     indice_num_per_loc.zero_()
    #     out_act = SpconvOps.generate_subm_conv_inds(indices, hashdata_subm, indice_pairs,
    #         out_indices, indice_num_per_loc,
    #         1, gs, ksize, [1, 1, 1])
    #     indice_num_per_loc_cpu = indice_num_per_loc.cpu().numpy()
    #     indice_pairs_cpu = indice_pairs.cpu().numpy()
    #     indice_pairs_cpu_flat = indice_pairs_cpu.reshape(-1)
    #     uq, count = np.unique(indice_pairs_cpu_flat, return_counts=True)
    #     print(out_act, indice_pairs_cpu.shape, indice_pairs_cpu.mean(), indice_num_per_loc_cpu.tolist())
    #     print(indice_pairs_cpu[:, 13, :2])
    #     print(uq, count)

if __name__ == "__main__":

    main()


text:
# This module parses collected and refined dataset and divides it into
# Train, Validation and Test splits.
# First, we have to read the data
# Second, we have to place it into numpy array
import os, sys
import pathlib
import argparse
import numpy as np
import pandas as pd
# from subprocess import check_output
#print(check_output(["ls", "../input"]))
from PIL import Image
from time import time
from time import sleep
from scipy import ndimage
from keras.preprocessing.image import ImageDataGenerator, array_to_img, img_to_array, load_img
from sklearn.model_selection import train_test_split
import random
import math
import shutil

import ipdb

# arguments to pass in command line
parser = argparse.ArgumentParser(description='Rename images in the folder according to LFW format: Name_Surname_0001.jpg, Name_Surname_0002.jpg, etc.')
parser.add_argument('--dataset-dir', default='', help='Full path to the directory with peeople and their names, folder should denote the Name_Surname of the person')
#parser.add_argument('--target-dir', default='', help='Full path to the directory where our identified images should be saved.')

# reading the passed arguments
args = parser.parse_args()
data_dir = args.dataset_dir
tr_dir = '../train/tr_split'
val_dir = '../train/val_split'

cont = 0

for folder in os.listdir(data_dir):
    if not os.path.isdir(os.path.join(data_dir, folder)):
        continue
    i = 1
    # print(folder)
    fold = data_dir + '/' + folder
    cnt = 0     # count the number of files in a folder
    onlyfiles = []
    for img in os.listdir(fold):
        # print(img)

        if os.path.splitext(img)[1] == '.jpg':
            onlyfiles.append(img)
            cnt += 1
    print(cnt)
    percent = math.floor((cnt * 90) / 100)

    print(percent)

    train_folder = tr_dir + '/' + folder
    val_folder = val_dir + '/' + folder

    # create folder for current person's test images

    os.makedirs(train_folder, exist_ok = True)
    os.makedirs(val_folder, exist_ok = True)

    idx = [i for i in range(cnt)]
    random.seed(100)
    tr_idx = random.sample(idx, percent)
    for i, f in enumerate(onlyfiles):

        file_to_move = fold + '/' + f
        if i in tr_idx:
            new_file = train_folder + '/' + f
        else:
            new_file = val_folder + '/' + f

        shutil.copyfile(file_to_move, new_file)
        cont += 1

print(cont)

reason: import numpy,from scipy


text:
import sys
import numpy as np
import pandas as pd
from .template_rank import AbstractRanker

TAU_INF = 10000000

class GreedyRanker(AbstractRanker):

    def __init__(self,
                include_S = True,
                tau = TAU_INF,
                sec_neigh = False,
                lamb = 0.99):
        self.description = "class for tracing greedy inference of openABM loop"
        self.include_S = include_S
        self.tau = tau
        self.sec_neigh = sec_neigh
        self.lamb = lamb
        self.rng = np.random.RandomState(1)

    def init(self, N, T):
        self.contacts = []
        #dummy obs, needed if the first time you add only one element
        self.obs = [(0,-1,0)]
        self.T = T
        self.N = N
        self.rank_not_zero = np.zeros(T)

        return True

    def rank(self, t_day, daily_contacts, daily_obs, data):
        '''
        computing rank of infected individuals
        return: list -- [(index, value), ...]
        '''

        for obs in daily_obs:
            self.obs.append(obs)

        while len(self.contacts) > 0:
            if self.contacts[0][2] < t_day - self.tau:
                self.contacts.pop()
            else:
                break

        for (i,j,t,l) in daily_contacts:
            self.contacts.append([i,j,t,l])

        obs_df = pd.DataFrame(self.obs, columns=["i", "s", "t_test"])
        contacts_df = pd.DataFrame(self.contacts, columns=["i", "j", "t", "lambda"])

        if not self.include_S:
            obs_df = obs_df[obs_df.s != 0] # just infected
        if self.sec_neigh:
            rank_greedy = run_greedy_sec_neigh(self, obs_df, t_day, contacts_df, self.N, tau = self.tau, lamb = self.lamb,verbose=False)
        else:
            rank_greedy = run_greedy(self, obs_df, t_day, contacts_df, self.N, tau = self.tau, verbose=False)
        dict_greedy = dict(rank_greedy)
        self.rank_not_zero[t_day] =  sum([1 for x in rank_greedy if x[1] > 0])
        data["rank_not_zero"] = self.rank_not_zero
        rank = list(sorted(rank_greedy, key=lambda tup: tup[1], reverse=True))

        return rank

def run_greedy(self, observ, T, contacts, N, noise = 1e-3, tau = TAU_INF, verbose=True):

    observ = observ[(observ["t_test"] <= T)]
    contacts = contacts[(contacts["t"] <= T) & (contacts["t"] >= T-tau)]

    idx_R = observ[observ['s'] == 2]['i'].to_numpy() # observed R
    idx_I = observ[observ['s'] == 1]['i'].to_numpy() # observed I

    # debug
    #idx_I_at_T = observ[(observ['s'] == 1) & (observ['t_test'] == T)].to_numpy()
    #idx_I_assumed = np.setdiff1d(idx_I, idx_I_at_T)

    idx_S_anyT = observ[(observ['s'] == 0) & (observ['t_test'] < T)]['i'] # observed S at time < T
    idx_S = observ[(observ['s'] == 0) & (observ['t_test'] == T)]['i'].to_numpy() # observed S at T -> put them at the tail of the ranking

    idx_alli = contacts['i'].unique()
    idx_allj = contacts['j'].unique()
    idx_all = np.union1d(idx_alli, idx_allj)
    idx_non_obs = np.setdiff1d(range(0,N), idx_all) # these have no contacts -> tail of the ranking

    idx_to_inf = np.setdiff1d(idx_all, idx_I) # nor I anytime
    idx_to_inf = np.setdiff1d(idx_to_inf, idx_S) # nor S at time T
    idx_to_inf = np.setdiff1d(idx_to_inf, idx_R) # nor R anytime

    maxS = -1 * np.ones(N)
    minR = T * np.ones(N)
    for i, s, t_test in  observ[["i", "s", "t_test"]].to_numpy():
        if s == 0 and t_test < T:
            maxS[i] = max(maxS[i], t_test)
        if s == 2:
            minR[i] = min(minR[i], t_test)
        # I can consider a contact as potentially contagious if T > minR > t_contact > maxS,
        # the maximum time at which I am observed as S (for both infector and
        # infected)

    if verbose:
        print("! Assuming contacts as direct links !", file=sys.stderr)
        print("! Assuming that if i is infected at t < T (and not observed as R), it is still infected at T !", file=sys.stderr)

    Score = dict([(i, 0) for i in range(N)])
    print(f"all contacts: {len(contacts)}")
    contacts_cut = contacts[(contacts["i"].isin(idx_to_inf)) \
                           & (contacts["j"].isin(idx_I))]
    print(f"all contacts cut: {len(contacts_cut)}")

    for i, j, t in contacts_cut[["i", "j", "t"]].to_numpy():
        if t > max(maxS[i], maxS[j]):
            if t < minR[j]:
                Score[i] += 1

    for i in range(0,N):
        if verbose:
            if i % 1000 == 0:
                print("Done... "+ str(i) + "/" + str(N))
        if i in idx_non_obs:
            Score[i] = -1 + self.rng.rand() * noise
        if i in idx_I and i not in idx_R:
            Score[i] = N * observ[(observ['i'] == i) & (observ['s'] == 1)]['t_test'].max()
        elif i in idx_S: #at time T
            Score[i] = -1 + self.rng.rand() * noise
        elif i in idx_R: #anytime
            Score[i] = -1 + self.rng.rand() * noise
    sorted_Score = list(sorted(Score.items(),key=lambda item: item[1], reverse=True))
    return sorted_Score

def run_greedy_weighted(self, observ, T, contacts, N, noise = 1e-3, verbose=True):

    observ = observ[(observ["t_test"] <= T)]
    contacts = contacts[(contacts["t"] <= T)]

    idx_R = observ[observ['s'] == 2]['i'].to_numpy() # observed R
    idx_I = observ[observ['s'] == 1]['i'].to_numpy() # observed I

    # debug
    #idx_I_at_T = observ[(observ['s'] == 1) & (observ['t_test'] == T)].to_numpy()
    #idx_I_assumed = np.setdiff1d(idx_I, idx_I_at_T)

    idx_S_anyT = observ[(observ['s'] == 0) & (observ['t_test'] < T)]['i'] # observed S at time < T
    idx_S = observ[(observ['s'] == 0) & (observ['t_test'] == T)]['i'].to_numpy() # observed S at T -> put them at the tail of the ranking

    idx_alli = contacts['i'].unique()
    idx_allj = contacts['j'].unique()
    idx_all = np.union1d(idx_alli, idx_allj)
    idx_non_obs = np.setdiff1d(range(0,N), idx_all) # these have no contacts -> tail of the ranking

    idx_to_inf = np.setdiff1d(idx_all, idx_I) # nor I anytime
    idx_to_inf = np.setdiff1d(idx_to_inf, idx_S) # nor S at time T
    idx_to_inf = np.setdiff1d(idx_to_inf, idx_R) # nor R anytime

    maxS = dict()
    minR = dict()
    for i in range(0,N):
        if i in idx_S_anyT:
            maxS[i] = observ[(observ['i'] == i) & (observ['s'] == 0)]['t_test'].max()
        else:
            maxS[i] = -1
        if i in idx_R:
            minR[i] = observ[(observ['i'] == i) & (observ['s'] == 2)]['t_test'].min()
        else:
            minR[i] = T
        # I can consider a contact as potentially contagious if T > minR > t_contact > maxS,
        # the maximum time at which I am observed as S (for both infector and
        # infected)

    if verbose:
        print("! Assuming contacts as direct links !", file=sys.stderr)
        print("! Assuming that if i is infected at t < T (and not observed as R), it is still infected at T !", file=sys.stderr)

    Score = dict([(i, 0) for i in range(N)])
    print(f"all contacts: {len(contacts)}")
    contacts_cut = contacts[(contacts["i"].isin(idx_to_inf)) \
                           & (contacts["j"].isin(idx_I))]
    print(f"all contacts cut: {len(contacts_cut)}")

    for i, j, t, lamb in contacts_cut.to_numpy():
        if t > max(maxS[i], maxS[j]):
            if t < minR[j]:
                Score[i] += lamb

    for i in range(0,N):
        if verbose:
            if i % 1000 == 0:
                print("Done... "+ str(i) + "/" + str(N))
        if i in idx_non_obs:
            Score[i] = -1 + self.rng.rand() * noise
        if i in idx_I and i not in idx_R:
            Score[i] = N * observ[(observ['i'] == i) & (observ['s'] == 1)]['t_test'].max()
        elif i in idx_S: #at time T
            Score[i] = -1 + self.rng.rand() * noise
        elif i in idx_R: #anytime
            Score[i] = -1 + self.rng.rand() * noise
    sorted_Score = list(sorted(Score.items(),key=lambda item: item[1], reverse=True))
    return sorted_Score

def run_greedy_sec_neigh(self, observ, T, contacts, N, noise = 1e-7, tau = TAU_INF, lamb = 0.99, verbose=True):

    from collections import Counter

    observ = observ[(observ["t_test"] <= T)]
    contacts = contacts[(contacts["t"] <= T) & (contacts["t"] >= T-tau)]

    idx_R = observ[observ['s'] == 2]['i'].to_numpy() # observed R
    idx_I = observ[observ['s'] == 1]['i'].to_numpy() # observed I

    # debug
    #idx_I_at_T = observ[(observ['s'] == 1) & (observ['t_test'] == T)].to_numpy()
    #idx_I_assumed = np.setdiff1d(idx_I, idx_I_at_T)

    idx_S_anyT = observ[(observ['s'] == 0) & (observ['t_test'] < T)]['i'] # observed S at time < T
    idx_S = observ[(observ['s'] == 0) & (observ['t_test'] == T)]['i'].to_numpy() # observed S at T -> put them at the tail of the ranking

    idx_alli = contacts['i'].unique()
    idx_allj = contacts['j'].unique()
    idx_all = np.union1d(idx_alli, idx_allj)
    idx_non_obs = np.setdiff1d(range(0,N), idx_all) # these have no contacts -> tail of the ranking

    idx_to_inf = np.setdiff1d(idx_all, idx_I) # rm I anytime
    idx_to_inf = np.setdiff1d(idx_to_inf, idx_S) # rm S at time T
    idx_to_inf = np.setdiff1d(idx_to_inf, idx_R) # rm R anytime

    maxS = -1 * np.ones(N)
    minR = T * np.ones(N)
    for i, s, t_test, in  observ[["i", "s", "t_test"]].to_numpy():
        if s == 0 and t_test < T:
            maxS[i] = max(maxS[i], t_test)
        if s == 2:
            minR[i] = min(minR[i], t_test)
        # I can consider a contact as potentially contagious if T > minR > t_contact > maxS,
        # the maximum time at which I am observed as S (for both infector and
        # infected)

    if verbose:
        print("! Assuming contacts as direct links !", file=sys.stderr)
        print("! Assuming that if i is infected at t < T (and not observed as R), it is still infected at T !", file=sys.stderr)

    Score = dict([(i, 0) for i in range(N)])
    print(f"all contacts: {len(contacts)}")
    contacts_cut = contacts[(contacts["i"].isin(idx_to_inf)) \
                           & (contacts["j"].isin(idx_I))]

    print(f"first NN contacts cut: {len(contacts_cut)}")
    if len(contacts_cut) > 0:
        # (i,j) are both unknown
        contacts_cut2 = contacts[(contacts["i"].isin(idx_to_inf)) \
                                 & (contacts["j"].isin(idx_to_inf))]
    idxk = []
    sec_NN = 0
    for i, j, t in contacts_cut[["i", "j", "t"]].to_numpy():
        # i to be estimated, j is infected
        if t > max(maxS[i], maxS[j]):
            if t < minR[j]:
                Score[i] += lamb + self.rng.rand() * noise
                # get neighbors k from future contacts (i,k), from the set of the unknown nodes
                aux = contacts_cut2[(contacts_cut2["i"] == i) \
                                    & (contacts_cut2["t"] > max(t,maxS[i]))]["j"].to_numpy()
                # collect indices
                idxk = np.concatenate((idxk, aux), axis=None)

    sec_NN = len(idxk)
    value_occ = Counter(idxk).items()
    # upd sec NN scores using occurencies
    for (k, occk) in value_occ:
        Score[k] += lamb*lamb*occk + self.rng.rand() * noise

    print(f"second NN contacts: {sec_NN}")

    for i in range(0,N):
        if verbose:
            if i % 1000 == 0:
                print("Done... "+ str(i) + "/" + str(N))
        if i in idx_non_obs:
            Score[i] = -1 + self.rng.rand() * noise
        if i in idx_I and i not in idx_R:
            Score[i] = N * observ[(observ['i'] == i) & (observ['s'] == 1)]['t_test'].max() + self.rng.rand() * noise
        elif i in idx_S: #at time T
            Score[i] = -1 + self.rng.rand() * noise
        elif i in idx_R: #anytime
            Score[i] = -1 + self.rng.rand() * noise
    sorted_Score = list(sorted(Score.items(),key=lambda item: item[1], reverse=True))
    return sorted_Score


text:
import gym
import time
import math
import random
import numpy as np
from pprint import pprint
from gym.wrappers import AtariPreprocessing

import torch
import torch.nn as nn
import torch.optim as optim

from .replay import ReplayMemory
from .preprocess import ImageHistory
from rltorch.utils.rl_logger import RLLogger
from rltorch.utils.stat_utils import StatTracker
from rltorch.papers.DQN.hyperparams import AtariHyperparams as hp

RENDER_PAUSE = 0.01

class DQNAgent:

    def __init__(self, env_name, death_ends_episode=True):
        assert "NoFrameskip-v4" in env_name
        print(f"\n{hp.ALGO} for Atari: {env_name}")
        config = hp.get_all_hyperparams()
        pprint(config)

        torch.manual_seed(hp.SEED)
        np.random.seed(hp.SEED)

        self.env_name = env_name
        self.env = AtariPreprocessing(gym.make(env_name),
                                      scale_obs=hp.NORMALIZE)
        self.num_actions = self.env.action_space.n

        self.device = torch.device("cuda"
                                   if torch.cuda.is_available()
                                   else "cpu")
        print(f"Using device={self.device}")

        self.replay = ReplayMemory(hp.REPLAY_SIZE,
                                   hp.AGENT_HISTORY,
                                   hp.WIDTH,
                                   hp.HEIGHT,
                                   hp.REPLAY_S_DTYPE,
                                   self.device)
        self.replay.display_memory_usage()
        self.img_buffer = ImageHistory(hp.AGENT_HISTORY, (hp.HEIGHT, hp.WIDTH))

        self.logger = RLLogger(self.env_name, f"{hp.ALGO}_atari")
        self.logger.save_config(config)
        self.eval_logger = RLLogger(self.env_name, f"{hp.ALGO}_atari_eval")
        self.return_tracker = StatTracker()

        # Neural Network related attributes
        self.dqn = hp.MODEL(self.num_actions).to(self.device)
        self.target_dqn = hp.MODEL(self.num_actions).to(self.device)
        print(self.dqn)

        self.optimizer = hp.OPTIMIZER(self.dqn.parameters(),
                                      **hp.OPTIMIZER_KWARGS)
        print(self.optimizer)
        self.loss_fn = nn.SmoothL1Loss()

        # Training related attributes
        self.epsilon_schedule = np.linspace(hp.INITIAL_EXPLORATION,
                                            hp.FINAL_EXPLORATION,
                                            hp.FINAL_EXPLORATION_FRAME)
        self.death_ends_episode = death_ends_episode
        self.steps_done = 0
        self.updates_done = 0

    def load_model(self, file_path):
        self.dqn.load_DQN(file_path, device=self.device)
        self.update_target_net()

    def get_action(self, x):
        return self.get_egreedy_action(x, self.get_epsilon())

    def get_epsilon(self):
        if self.steps_done < hp.REPLAY_START_SIZE:
            return 1.0
        if self.steps_done < hp.FINAL_EXPLORATION_FRAME:
            return self.epsilon_schedule[self.steps_done]
        return hp.FINAL_EXPLORATION

    def get_egreedy_action(self, x, epsilon):
        if random.random() > epsilon:
            x = torch.from_numpy(x).to(self.device)
            return self.dqn.get_action(x).cpu().item()
        return random.randint(0, self.num_actions-1)

    def get_action_and_value(self, x):
        x = torch.from_numpy(x).to(self.device)
        q_vals = self.dqn(x).cpu()
        a = q_vals.max(1)[1]
        return q_vals.data, a.item()

    def optimize(self):
        if self.steps_done % hp.NETWORK_UPDATE_FREQUENCY != 0:
            return None

        if self.steps_done < hp.REPLAY_START_SIZE:
            return 0, 0, 0, 0

        batch = self.replay.sample_batch(hp.MINIBATCH_SIZE)
        s_batch, a_batch, next_s_batch, r_batch, d_batch = batch

        q_vals_raw = self.dqn(s_batch)
        q_vals = q_vals_raw.gather(1, a_batch).squeeze()

        with torch.no_grad():
            target_q_val = self.target_dqn(next_s_batch).max(1)[0]
            target = r_batch + hp.DISCOUNT*(1-d_batch)*target_q_val

        loss = self.loss_fn(target, q_vals)

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

        if self.updates_done % hp.TARGET_NETWORK_UPDATE_FREQ == 0:
            self.update_target_net()

        mean_v = q_vals_raw.max(1)[0].mean().item()
        max_v = q_vals.max().item()
        mean_td_error = (target - q_vals).abs().mean().item()
        return loss.item(), mean_v, max_v, mean_td_error

    def update_target_net(self):
        print(f"step={self.steps_done}, updates={self.updates_done}:"
              " updating target_net")
        self.target_dqn.load_state_dict(self.dqn.state_dict())

    def save_model(self):
        print("saving model")
        save_path = self.logger.get_save_path(ext=".pth")
        self.dqn.save_DQN(save_path)

    def train(self):
        print("Starting training")
        training_start_time = time.time()
        self.steps_done = 0
        num_episodes = 0
        steps_since_eval = 0
        steps_remaining = hp.TRAINING_FRAMES
        while steps_remaining > 0:
            start_time = time.time()
            ep_return, ep_steps = self.run_episode(steps_remaining)
            num_episodes += 1
            steps_remaining -= ep_steps
            steps_since_eval += ep_steps

            self.return_tracker.update(ep_return)
            training_time = time.time()-training_start_time
            self.logger.log("episode", num_episodes)
            self.logger.log("steps_done", self.steps_done)
            self.logger.log("updates_done", self.updates_done)
            self.logger.log("epsilon", self.get_epsilon())
            self.logger.log("episode_return", ep_return)
            self.logger.log("episode_return_moving_mean",
                            self.return_tracker.moving_mean)
            self.logger.log("episode_return_moving_min",
                            self.return_tracker.moving_min)
            self.logger.log("episode_return_moving_max",
                            self.return_tracker.moving_max)
            self.logger.log("episode_return_moving_stdev",
                            self.return_tracker.moving_stdev)
            self.logger.log("episode_return_overall_max",
                            self.return_tracker.max_val)
            self.logger.log("episode_time", time.time()-start_time)
            self.logger.log("total_training_time", training_time)

            display = num_episodes % hp.LOG_DISPLAY_FREQ == 0
            self.logger.flush(display)

            if steps_since_eval >= hp.EVAL_FREQ or steps_remaining <= 0:
                eval_logger_kwargs = {"training_step": self.steps_done,
                                      "training_episode": num_episodes,
                                      "training_time": training_time}
                self.run_eval(eval_logger_kwargs=eval_logger_kwargs)
                steps_since_eval = 0

        self.logger.flush(True)
        print("Training complete")

    def run_episode(self, step_limit, eval_run=False, render=False):
        xs = self.init_episode()
        done = False

        steps = 0
        episode_return = 0
        losses, mean_values, mean_td_errors = [], [], []
        overall_max_v = -math.inf

        if render:
            self.env.render()

        while not done and steps < step_limit:
            if eval_run:
                a = self.get_egreedy_action(xs, hp.EVAL_EPSILON)
            else:
                a = self.get_action(xs)

            next_x, r, done, _ = self.env.step(a)
            self.img_buffer.push(next_x)
            next_xs = self.img_buffer.get()

            if render:
                self.env.render()
                time.sleep(RENDER_PAUSE)

            if not eval_run:
                self.steps_done += 1
                clipped_r = np.clip(r, *hp.R_CLIP)
                self.replay.store(xs, a, next_x, clipped_r, done)

                result = self.optimize()
                if result is not None:
                    loss, mean_v, max_v, mean_td_error = result
                    losses.append(loss)
                    mean_values.append(mean_v)
                    overall_max_v = max(overall_max_v, max_v)
                    mean_td_errors.append(mean_td_error)

                if self.steps_done % hp.MODEL_SAVE_FREQ == 0:
                    self.save_model()

            xs = next_xs
            episode_return += r
            steps += 1

        if not eval_run:
            losses = np.array(losses)
            self.logger.log("episode_mean_loss", losses.mean())
            self.logger.log("episode_loss_max", losses.max())
            self.logger.log("episode_loss_min", losses.min())
            self.logger.log("episode_mean_v", np.array(mean_values).mean())
            self.logger.log("episode_max_v", overall_max_v)
            self.logger.log("episode_mean_td_error",
                            np.array(mean_td_errors).mean())
        return episode_return, steps

    def run_eval(self, eval_logger_kwargs=None, render=False):
        print("RUNNING EVALUATION")
        eval_steps_remaining = hp.EVAL_STEPS
        eval_tracker = StatTracker()
        eval_start_time = time.time()
        while eval_steps_remaining > 0:
            ep_return, ep_steps = self.run_episode(eval_steps_remaining,
                                                   eval_run=True,
                                                   render=render)
            eval_steps_remaining -= ep_steps
            print(f"Episode Fin. Return={ep_return}, "
                  f"eval_steps_remaining={eval_steps_remaining}")
            if eval_steps_remaining > 0:
                eval_tracker.update(ep_return)

        if eval_logger_kwargs is None:
            eval_logger_kwargs = {}
        self.eval_logger.log("training_step",
                             eval_logger_kwargs.get("training_step", 0))
        self.eval_logger.log("training_episode",
                             eval_logger_kwargs.get("training_episode", 0))
        self.eval_logger.log("training_time",
                             eval_logger_kwargs.get("training_time", 0))
        self.eval_logger.log("num_eval_episode", eval_tracker.n)
        self.eval_logger.log("episode_return_mean", eval_tracker.mean)
        self.eval_logger.log("episode_return_min", eval_tracker.min_val)
        self.eval_logger.log("episode_return_max", eval_tracker.max_val)
        self.eval_logger.log("episode_return_stdev", eval_tracker.stdev)
        self.eval_logger.log("eval_time", time.time() - eval_start_time)

        print("EVALUATION RESULTS:")
        self.eval_logger.flush(True)

    def init_episode(self):
        """Resets game, performs noops and returns first processed state """
        x = self.env.reset()
        self.img_buffer.push(x)
        # fill buffer
        for _ in range(hp.AGENT_HISTORY-1):
            x, _, _, _ = self.env.step(0)
            self.img_buffer.push(x)
        return self.img_buffer.get()

max_stars_repo_name: Jjschwartz/rltorch

max_issues_repo_name: Jjschwartz/rltorch

max_forks_repo_name: Jjschwartz/rltorch


text:
from facemesh import FaceData
import numpy as np
import argparse
import os
import matplotlib.pyplot as plt
import matplotlib
matplotlib.rcParams.update({'font.size': 16})

def cumsum_error(error, n_bins=1000):
	N_cnn, X_cnn = np.histogram(error, bins=n_bins)
	X_cnn = np.convolve(X_cnn, 0.5*np.ones(2))
	X_cnn = X_cnn[1:-1]

	factor_cnn = 100.0/error.shape[0]
	cumsum_N_cnn = np.cumsum(N_cnn)

	X_vec_cnn = np.zeros((n_bins+1,))
	X_vec_cnn[1:] = X_cnn

	yVec_cnn = np.zeros((n_bins+1,))
	yVec_cnn[1:] = factor_cnn*cumsum_N_cnn

	return X_vec_cnn, yVec_cnn

def save_error_plt(cnn, pca, test, fname, n_bins=1000):
	cnn_err = np.sqrt(np.sum((cnn - test)**2, axis=2))
	pca_err = np.sqrt(np.sum((pca - test)**2, axis=2))
	cnn_err = np.reshape(cnn_err, (-1,))
	pca_err = np.reshape(pca_err, (-1,))

	X_vec_pca, yVec_pca = cumsum_error(pca_err)
	X_vec_cnn, yVec_cnn = cumsum_error(cnn_err)

	plt.plot(X_vec_pca, yVec_pca, label="PCA")
	plt.plot(X_vec_cnn, yVec_cnn, label="Mesh Autoencoder")
	plt.ylabel('Percentage of Vertices')
	plt.xlabel('Euclidean Error norm (mm)')

	plt.legend(loc='lower right')
	plt.xlim(0, 6)
	plt.grid(True)  # ,color='grey', linestyle='-', linewidth=0.5)
	plt.savefig(fname)

parser = argparse.ArgumentParser()
parser.add_argument('--cnn', default='/is/ps/shared/aranjan/convFaces/results/iclr/bareteeth21_predictions.npy',
					help='path to dataset')
parser.add_argument('--data', default='/is/ps/shared/aranjan/convFaces/data/bareteeth', help='path to dataset')
parser.add_argument('--nz', type=int, default=8, help='size of the latent z vector')
parser.add_argument('--save', default='save', help='path to dataset')

opt = parser.parse_args()

if not os.path.exists(opt.save):
	os.makedirs(opt.save)
	os.makedirs(opt.save+'/cnn')
	os.makedirs(opt.save+'/pca')
	os.makedirs(opt.save+'/gt')
	os.makedirs(opt.save+'/plt')

reference_mesh_file = 'data/template.obj'
facedata = FaceData(nVal=100, train_file=opt.data+'/train.npy', test_file=opt.data+'/test.npy',
					reference_mesh_file=reference_mesh_file, pca_n_comp=opt.nz, fitpca=True)
nv = facedata.n_vertex*3
nTest = facedata.vertices_test.shape[0]

cnn_outputs = np.load(opt.cnn)
pca_outputs = facedata.pca.inverse_transform(
	facedata.pca.transform(np.reshape(facedata.vertices_test, (facedata.vertices_test.shape[0], facedata.n_vertex*3))))

cnn_vertices = (cnn_outputs * facedata.std) + facedata.mean
pca_vertices = (np.reshape(pca_outputs, (pca_outputs.shape[0], facedata.n_vertex, 3)) * facedata.std) + facedata.mean
test_vertices = (facedata.vertices_test * facedata.std) + facedata.mean

cnn_vertices = cnn_vertices*1000
pca_vertices = pca_vertices*1000
test_vertices = test_vertices*1000

save_error_plt(cnn=cnn_vertices, pca=pca_vertices, test=test_vertices,
			   fname=opt.save+'/plt/'+os.path.basename(opt.data)+'.png')
cnn_err = np.mean(np.sqrt(np.sum((cnn_vertices-test_vertices)**2, axis=2)))
pca_err = np.mean(np.sqrt(np.sum((pca_vertices-test_vertices)**2, axis=2)))

cnn_std = np.std(np.sqrt(np.sum((cnn_vertices-test_vertices)**2, axis=2)))
pca_std = np.std(np.sqrt(np.sum((pca_vertices-test_vertices)**2, axis=2)))

cnn_med = np.median(np.sqrt(np.sum((cnn_vertices-test_vertices)**2, axis=2)))
pca_med = np.median(np.sqrt(np.sum((pca_vertices-test_vertices)**2, axis=2)))

print('{} PCA Error: {:.3f}+{:.3f} | {:.3f} CoMA Error: {:.3f}+{:.3f} | {:.3f}'.format(
	os.path.basename(opt.data), pca_err, pca_std, pca_med, cnn_err, cnn_std,  cnn_med))

max_stars_repo_name: eosulliv/coma

max_issues_repo_name: eosulliv/coma

max_forks_repo_name: eosulliv/coma


text:
import os
import random

import numpy as np
import torch

RANDOM_SEED = 12345
random.seed(RANDOM_SEED)
np.random.seed(RANDOM_SEED)
torch.manual_seed(RANDOM_SEED)
if torch.cuda.is_available():
    torch.cuda.manual_seed_all(RANDOM_SEED)

class DataGenerator(object):
    def __init__(self, data_path, data_name, batch_size, tokenizer, split, device='cuda'):
        super(DataGenerator, self).__init__()
        self.data = []
        self.lengths = []

        self.fa = open(os.path.join(data_path, '{}/{}/a.toks'.format(data_name, split)))
        self.fb = open(os.path.join(data_path, '{}/{}/b.toks'.format(data_name, split)))
        self.fsim = open(os.path.join(data_path, '{}/{}/sim.txt'.format(data_name, split)))
        self.fid = open(os.path.join(data_path, '{}/{}/id.txt'.format(data_name, split)))

        for a, b, sim, ID in zip(self.fa, self.fb, self.fsim, self.fid):
            self.data.append([sim.replace('\n', ''), a.replace('\n', ''),
                              b.replace('\n', ''), \
                              ID.replace('\n', '')])
            self.lengths.append(len(b.replace('\n', '').split()))

        np.random.shuffle(self.data)
        self.data_i = 0
        self.data_size = len(self.data)
        self.data_name = data_name
        self.batch_size = batch_size
        self.device = device
        self.tokenizer = tokenizer
        self.start = True

    def get_instance(self):
        ret = self.data[self.data_i % self.data_size]
        self.data_i += 1
        return ret

    def epoch_end(self):
        return self.data_i % self.data_size == 0

    def tokenize_index(self, text):
        tokenized_text = self.tokenizer.tokenize(text)
        tokenized_text.insert(0, '[CLS]')
        tokenized_text.append('[SEP]')
        # Convert token to vocabulary indices
        indexed_tokens = self.tokenizer.convert_tokens_to_ids(tokenized_text)
        return indexed_tokens

    def tokenize_two(self, a, b):
        tokenized_text_a = ['[CLS]'] + self.tokenizer.tokenize(a) + ['[SEP]']
        tokenized_text_b = self.tokenizer.tokenize(b)
        tokenized_text_b = tokenized_text_b[:510 - len(tokenized_text_a)]
        tokenized_text_b.append('[SEP]')
        segments_ids = [0] * len(tokenized_text_a) + [1] * len(tokenized_text_b)
        tokenized_text = tokenized_text_a + tokenized_text_b
        # Convert token to vocabulary indices
        indexed_tokens = self.tokenizer.convert_tokens_to_ids(tokenized_text)
        return indexed_tokens, segments_ids

    def load_batch(self):
        test_batch, token_type_ids_batch, mask_batch, label_batch, qid_batch, docid_batch = [], [], [], [], [], []
        while True:
            if not self.start and self.epoch_end():
                self.start = True
                break
            self.start = False
            instance = self.get_instance()
            if self.data_name == 'robust04':
                label, a, b, qid, docid = instance
                qid = int(qid)
                docid = int(docid)
                qid_batch.append(qid)
                docid_batch.append(docid)
            else:
                label, a, b, ID = instance
                ls = ID.split()
                if len(ls) > 1:
                    qid, _, docid, _, _, _ = ls
                    docid = int(docid)
                    docid_batch.append(docid)
                else:
                    qid = ID
                qid_batch.append(int(qid))
            combine_index, segments_ids = self.tokenize_two(a, b)
            test_batch.append(torch.tensor(combine_index))
            token_type_ids_batch.append(torch.tensor(segments_ids))
            mask_batch.append(torch.ones(len(combine_index)))
            label_batch.append(int(label))
            if len(test_batch) >= self.batch_size or self.epoch_end():
                # Convert inputs to PyTorch tensors
                tokens_tensor = torch.nn.utils.rnn.pad_sequence(test_batch,
                                                                batch_first=True,
                                                                padding_value=0).to(
                    self.device)
                segments_tensor = torch.nn.utils.rnn.pad_sequence(
                    token_type_ids_batch, batch_first=True,
                    padding_value=0).to(self.device)
                mask_tensor = torch.nn.utils.rnn.pad_sequence(mask_batch,
                                                              batch_first=True,
                                                              padding_value=0).to(
                    self.device)
                label_tensor = torch.tensor(label_batch, device=self.device)
                qid_tensor = torch.tensor(qid_batch, device=self.device)
                docid_tensor = torch.tensor(docid_batch, device=self.device)
                return (tokens_tensor, segments_tensor, mask_tensor,
                        label_tensor, qid_tensor, docid_tensor)

        return None