text:
""" Contains classes and methods for ASWING geometry (configuration) definition
"""

""" Author: Michael Kapteyn, Aerospace Computational Design Lab, MIT
"""
from aswingpy.helpers import *
import numpy as np

class aswGeometry:
    def __init__(self, name=[], units=[], constants=[], reference=[], weights=[], sensors=[], engines=[], struts=[], joints=[], jangles=[], grounds=[], beams={}):
        self.name = name
        self.units = units
        self.constants = constants
        self.reference = reference
        self.weights = weights
        self.sensors = sensors
        self.engines = engines
        self.struts = struts
        self.joints = joints
        self.jangles = jangles
        self.grounds = grounds
        self.beams = beams

    def readaswfile(self, filepath):
        self.name = []
        self.units = []
        self.constants = []
        self.reference = []
        self.weights = []
        self.sensors = []
        self.engines = []
        self.struts = []
        self.joints = []
        self.jangles = []
        self.grounds = []
        self.beams = {} #dict

        with open(filepath) as fp:
            line = fp.readline()
            idx = 1
            while line:
                # Check for and read name block
                if isinline(line.lower(),"name",[1,4]):
                    print("\nReading Name: ")
                    while line:
                        line = fp.readline()
                        idx += 1
                        if islineignored(line):
                            pass
                        elif islineend(line):
                            break
                        else:
                            self.name = line.strip()
                            print("Name is:", self.name)
                    line = fp.readline()
                    idx += 1

                # Check for and read units block
                elif isinline(line.lower(),"unit",[1,4]):
                    print("\nReading Units:")
                    while line:
                        line = fp.readline()
                        idx += 1
                        if islineignored(line):
                            pass
                        elif islineend(line):
                            break
                        else:
                            splitline = line.split()
                            if splitline[0].lower() == "l":
                                lengthUnits = splitline[2]
                                print("Length unit is", lengthUnits)
                            elif splitline[0].lower() == "t":
                                timeUnits = splitline[2]
                                print("Time unit is", timeUnits)
                            elif splitline[0].lower() == "f":
                                forceUnits = splitline[2]
                                print("Force unit is", forceUnits)
                    line = fp.readline()
                    idx += 1
                    self.units = units(lengthUnits, timeUnits, forceUnits)

                # Check for and read constants block
                elif isinline(line.lower(), "cons", [1,4]):
                    print("\nReading Constants:")
                    while line:
                        line = fp.readline()
                        idx += 1
                        if islineignored(line):
                            pass
                        elif islineend(line):
                            break
                        else:
                            splitline = line.split()
                            g = asnum(splitline[0])
                            rhoSL = asnum(splitline[1])
                            SoSSL = asnum(splitline[2])
                            print("Gravity is", g)
                            print("Density is", rhoSL)
                            print("Speed of sound is", SoSSL)

                    self.constants = constants(g, rhoSL, SoSSL)
                    line = fp.readline()
                    idx += 1

                # Check for and read reference values block
                elif isinline(line.lower(), "refe", [1,4]):
                    print("\nReading Reference Values:")
                    while line:
                        line = fp.readline()
                        idx += 1
                        if islineignored(line):
                            pass
                        elif islineend(line):
                            break
                        else:
                            data, line, idx = parsedatablock(fp, line, idx, 3)
                            area = data[0,0]
                            chord = data[0,1]
                            span = data[0,2]
                            if data.shape[0] > 3:
                                moments = data[1,:]
                                acc = data[2,:]
                                vel = data[3,:]
                                self.reference = reference(area, chord, span, moments, acc, vel)
                            elif data.shape[0] > 2:
                                moments = data[1,:]
                                acc = data[2,:]
                                self.reference = reference(area, chord, span, moments, acc)
                            elif data.shape[0] > 1:
                                moments = data[1,:]
                                self.reference = reference(area, chord, span, moments)
                            elif data.shape[0] == 1:
                                self.reference = reference(area, chord, span)
                            else:
                                pass

                            print("Area is", area)
                            print("chord is", chord)
                            print("span is", span)
                            print("moments are", moments)
                            break

                    # self.reference = reference(area, chord, span, moments, acc, vel)
                    line = fp.readline()
                    idx += 1

                # Check for and read weight block
                elif isinline(line.lower(), "weig", [1,4]):
                    print("\nReading Weight Block:")
                    while line:
                        line = fp.readline()
                        idx += 1
                        if islineignored(line):
                            pass
                        elif islineend(line):
                            break
                        else:
                            data, line, idx = parsedatablock(fp, line, idx, 14, 1)
                            for row in data:
                                if len(row) == 11:
                                    self.weights.append(weight(row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8], row[9], row[10]))
                                else:
                                    self.weights.append(weight(row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8], row[9], row[10], row[11], row[12], row[13]))
                            print("Weight data", data)
                            break
                    line = fp.readline()
                    idx += 1

                # Check for and read sensor block
                elif isinline(line.lower(), "sens", [1,4]):
                    print("\nReading Sensor Block:")
                    while line:
                        line = fp.readline()
                        idx += 1
                        if islineignored(line):
                            pass
                        elif islineend(line):
                            break
                        else:
                            data, line, idx = parsedatablock(fp, line, idx, 12, 2)
                            for row in data:
                                if len(row) == 6:
                                    self.sensors.append(sensor(row[0], row[1], row[2], row[3], row[4], row[5]))
                                else:
                                    self.sensors.append(sensor(row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8], row[9], row[10], row[11]))
                            print("Sensor data", data)
                            break
                    line = fp.readline()
                    idx += 1

                # Check for and read engine block
                elif isinline(line.lower(), "engi", [1,4]):
                    print("\nReading Engine Block:")
                    while line:
                        line = fp.readline()
                        idx += 1
                        if islineignored(line):
                            pass
                        elif islineend(line):
                            break
                        else:
                            data, line, idx = parsedatablock(fp, line, idx, 25, 3)
                            for row in data:
                                if len(row) == 11:
                                    self.engines.append(engine(row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8], row[9], row[10]))
                                elif len(row) == 15:
                                    self.engines.append(engine(row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8], row[9], row[10], row[11], row[12], row[13], row[14]))
                                else:
                                    self.engines.append(engine(row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8], row[9], row[10], row[11], row[12], row[13], row[14], row[15], row[16], row[17], row[18], row[19], row[20], row[21], row[22], row[23], row[24]))
                            print("Engine data", data)
                            break
                    line = fp.readline()
                    idx += 1

                # Check for and read strut block
                elif isinline(line.lower(), "stru", [1,4]):
                    print("\nReading Strut Block:")
                    while line:
                        line = fp.readline()
                        idx += 1
                        if islineignored(line):
                            pass
                        elif islineend(line):
                            break
                        else:
                            data, line, idx = parsedatablock(fp, line, idx, 10, 1)
                            for row in data:
                                self.struts.append(strut(row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8], row[9]))
                            print("Strut data", data)
                            break
                    line = fp.readline()
                    idx += 1

                # Check for and read joint block
                elif isinline(line.lower(), "join", [1,4]):
                    print("\nReading Joint Block:")
                    while line:
                        line = fp.readline()
                        idx += 1
                        if islineignored(line):
                            pass
                        elif islineend(line):
                            break
                        else:
                            data, line, idx = parsedatablock(fp, line, idx, 5, 2)
                            for row in data:
                                if len(row) == 4:
                                    self.joints.append(joint(row[0], row[1], row[2], row[3]))
                                else:
                                    self.joints.append(joint(row[0], row[1], row[2], row[3], row[4]))
                            print("Joint data", data)
                            break
                    line = fp.readline()
                    idx += 1

                # Check for and read jangle blocks
                elif isinline(line.lower(), "jang", [1,4]):
                    print("\nReading Jangle Block:")
                    while line:
                        line = fp.readline()
                        idx += 1
                        if islineignored(line):
                            pass
                        elif islineend(line):
                            break
                        else:
                            data, line, idx = parsedatablock(fp, line, idx, 4, 2)
                            self.jangles.append(jangle(data[0,0], data[0,1], data[0,2], data[0,3], data[1:,0], data[1:,1]))
                            print("Jangle data", data)
                            break
                    line = fp.readline()
                    idx += 1

                # Check for and read ground block
                elif isinline(line.lower(), "grou", [1,4]):
                    print("\nReading Ground Block:")
                    while line:
                        line = fp.readline()
                        idx += 1
                        if islineignored(line):
                            pass
                        elif islineend(line):
                            break
                        else:
                            data, line, idx = parsedatablock(fp, line, idx, 3, 1)
                            for row in data:
                                if len(row) == 2:
                                    self.grounds.append(ground(row[0], row[1]))
                                else:
                                    self.grounds.append(ground(row[0], row[1], row[2]))
                            print("Ground data", data)
                            break
                    line = fp.readline()
                    idx += 1

                elif isinline(line.lower(), "beam", [1,4]):
                    print("\nReading Beam Blocks:")

                    #Incremenet the beam counter
                    # nbeam = nbeam+1
                    bvardict = {}
                    # Read beam number and optional physical index
                    splitline = line.split()
                    beamIdx = asnum(splitline[1])
                    if len(splitline) > 2:
                        physicalIdx = asnum(splitline[2])
                    else:
                        physicalIdx = beamIdx

                    #Read the beam name
                    line = fp.readline()
                    idx+=1

                    beam_name = line.strip("\n")
                    line = fp.readline()
                    idx += 1
                    line = fp.readline()
                    idx += 1
                    beam_data_end = False
                    while not beam_data_end:

                        bvar = line.split()
                        if bvar[0] != "t":
                            print("Warning: First beam variable must be t")
                        line = fp.readline()
                        idx += 1
                        data, line, idx, beam_data_end = parsebeamblock(fp, line, idx, len(bvar))
                        for i in range(1,len(bvar)):
                            if bvar[i] not in globalbvardict:
                                print("Warning: variable ", bvar[i] , " is not a valid variable")
                            else:
                                bvardict[bvar[i]] = data[:,[0, i]]
                        if beam_data_end:
                            break
                    self.beams[beam_name] = beam(beamIdx, physicalIdx, beam_name, bvardict)
                else:
                    pass

                line = fp.readline()
                idx += 1

    def writeaswfile(self, filepath=None):
        if filepath is None:
            filepath = "./inputfiles/"+self.name+".asw"

        comment = "#====================================\n"
        end = "End\n"
        with open(filepath, 'w+') as f:
            f.write(comment)

            # Write Name block
            f.write("Name\n")
            f.write(self.name + "\n")
            f.write(end)
            f.write(comment)

            # Write Units block
            f.write("Units\n")
            f.write("L 1.0 "+self.units.length + "\n")
            f.write("T 1.0 "+self.units.time + "\n")
            f.write("F 1.0 "+self.units.force + "\n")
            f.write(end)
            f.write(comment)

            # Write Constants block
            f.write("Constant\n")
            f.write("#\tg\trhoSL\tVsoSL\n")
            f.write(str(self.constants.gravity)+"\t"+str(self.constants.seaLevelAirDensity)+"\t"+str(self.constants.seaLevelSpeedOfSound)+"\n")
            f.write(end)
            f.write(comment)

            # Write Reference block
            f.write("Reference\n")
            f.write("#    Sref    Cref    Bref\n")
            writedata(f,(self.reference.area,self.reference.chord,self.reference.span))
            f.write("#\n# Xmom  Ymom  Zmom\n")
            writedata(f, self.reference.mom)
            f.write("#\n# Xvel  Yvel  Zvel\n")
            writedata(f, self.reference.vel)
            f.write("#\n# Xacc  Yacc  Zacc\n")
            writedata(f, self.reference.acc)
            f.write(end)
            f.write(comment)

            # Write Weight block
            f.write("Weight\n")
            f.write("# beamIdx  t    Xp   Yp   Zp   Weight CDA  Vol   Hxo  Hyo  Hzo  Ixx Iyy Izz Ixy Ixz Iyz\n")
            for weight in self.weights:
                writedata(f, (weight.beamIdx, weight.t, weight.Xp, weight.Yp, weight.Mg, weight.CDA, weight.Vol, weight.Hx, weight.Hy, weight.Hz, weight.Ix, weight.Iy, weight.Iz))
            f.write(end)
            f.write(comment)

            # Write Sensor Block
            f.write("Sensor\n")
            f.write("# sensorIdx  beamIdx  t    Xo   Yo   Zo    Vx   Vy   Vz     Ax   Ay   Az\n")
            for sensor in self.sensors:
                writedata(f, (sensor.sensorIdx, sensor.beamIdx, sensor.t, sensor.Xp, sensor.Yp, sensor.Zp, sensor.Vx, sensor.Vy, sensor.Vz, sensor.Ax, sensor.Ay, sensor.Az))
            f.write(end)
            f.write(comment)

            # Write Engine Block
            f.write("Engine\n")
            f.write("# engIdx IEtyp beamIdx  t    Xo   Yo   Zo    Tx  Ty  Tz    dFdPe  dMdPe  Rdisk Omega   cdA    cl     CLa    S0     C0     S1     C1     S2     C2     S3     C3\n")
            for engine in self.engines:
                writedata(f, (engine.engIdx, engine.engType, engine.beamIdx, engine.t, engine.Xp, engine.Yp, engine.Zp, engine.Tx, engine.Ty, engine.Tz, engine.dFdPe, engine.dMdPe, engine.Rdisk, engine.Omega, engine.cdA, engine.cl, engine.CLa, engine.S0, engine.C0, engine.S1, engine.C1, engine.S2, engine.C2, engine.S3, engine.C3))
            f.write(end)
            f.write(comment)

            # Write Strut block
            f.write("Strut\n")
            f.write("# beamIdx   t     Xp    Yp    Zp     Xw    Yw    Zw     dL     EAw\n")
            for strut in self.struts:
                writedata(f, (strut.beamIdx, strut.t, strut.Xp, strut.Yp, strut.Zp, strut.Xw, strut.Yw, strut.Zw, strut.dL, strut.EAw))
            f.write(end)
            f.write(comment)

            # Write Joint block
            f.write("Joint\n")
            f.write("#  beam1Idx  beam2Idx    t1    t2    jointType\n")
            for joint in self.joints:
                writedata(f, (joint.beam1Idx, joint.beam2Idx, joint.t1, joint.t2, joint.jointType))
            f.write(end)
            f.write(comment)

            # Write Jangle block
            for jangle in self.jangles:
                f.write("Jangle\n")
                f.write("#  jointIdx  hx   hy   hz\n")
                writedata(f, (jangle.jointIdx, jangle.hx, jangle.hy, jangle.hz))
                f.write("#\n")
                f.write("#  Momh    Angh\n")
                for i in range(0,len(jangle.Momh)):
                    writedata(f, (jangle.Momh[i], jangle.Angh[i]))
                f.write(end)
                f.write(comment)

            # Write Ground block
            f.write("Ground\n")
            f.write("#  beamIdx  t     groundType\n")
            for ground in self.grounds:
                writedata(f, (ground.beamIdx, ground.t, ground.groundType))
            f.write(end)
            f.write(comment)

            # write Beam blocks
            for name, beam in self.beams.items():
                f.write("Beam    "+str(beam.beamIdx)+"    "+str(beam.physicalIdx)+"\n")
                f.write(beam.name+"\n")
                f.write("#\n")
                for var, data in beam.spanwise.items():
                    if len(data) !=0:
                        f.write("    t    "+var+"\n")
                        writedata(f, data)
                        f.write("#\n")
                f.write(end)
                f.write(comment)

class units:
    def __init__(self, lengthUnits, timeUnits, forceUnits):
        self.length = lengthUnits
        self.time = timeUnits
        self.force = forceUnits

class constants:
    def __init__(self, g, rho, V):
        self.seaLevelAirDensity = rho
        self.seaLevelSpeedOfSound = V
        self.gravity = g

class reference:
    def __init__(self, area, chord, span, mom = np.zeros((1,3)), acc=np.zeros((1,3)), vel=np.zeros((1,3))):
        self.area = area
        self.chord = chord
        self.span = span
        self.mom = mom
        self.acc = acc
        self.vel = vel

class weight:
    #This allows specification of "point-masses", or point-objects to be more precise.
    def __init__(self, beamIdx, t, Xp, Yp, Zp, Mg, CDA, Vol, Hx, Hy, Hz, Ix = 0, Iy = 0, Iz = 0):
        #Each point-mass possesses mass, and also angular momentum,
        # aerodynamic drag-area, and aerodynamic volume.  Each is cantilevered from
        # a surface or fuselage beam via a massless rigid pylon.  The pylon attachment
        # point is at location t on the reference axis of beam "beamIdx"
        self.beamIdx = beamIdx
        self.t = t

        #The other free end of the pylon is specified by the cartesian
        # coordinates Xo, Yo, Zo, which is where the point-mass is located
        # in the undeformed state (i.e. when the beam has its jig shape).
        # The pylon is assumed to be rigid (infinitely stiff), and will wave
        # the point-mass around as the beam section at t moves and rotates
        # during deformation.  The angular momentum vector's direction will
        # also change accordingly.
        self.Xp = Xp
        self.Yp = Yp
        self.Zp = Zp

        # Weight (NOT mass) of the object
        self.Mg = Mg

        #The aero drag force vector D on the point mass is calculated from the
        # specified drag area CDA as
        #  _             _
        #  D = 0.5 rho V V  CDA
        #       _                                                     _
        # where V is the net local relative velocity vector, and V = |V|.
        self.CDA = CDA

        # Aerodynamic Volume
        self.Vol = Vol

        self.Hx = Hx
        self.Hy = Hy
        self.Hz = Hz

        self.Ix = Ix
        self.Iy = Iy
        self.Iz = Iz

class sensor:
    def __init__(self, sensorIdx, beamIdx, t, Xp, Yp, Zp, Vx = 1, Vy = 0, Vz = 0, Ax = 0, Ay = 0, Az = 1):
        self.sensorIdx = sensorIdx
        self.beamIdx = beamIdx
        self.t = t
        self.Xp = Xp
        self.Yp = Yp
        self.Zp = Zp
        self.Vx = Vx
        self.Vy = Vy
        self.Vz = Vz
        self.Ax = Ax
        self.Ay = Ay
        self.Az = Az

class engine:
    def __init__(self, engIdx, engType, beamIdx, t, Xp, Yp, Zp, Tx, Ty, Tz, dFdPe, dMdPe=0, Rdisk=0, Omega=0, cdA = 0, cl=0, CLa=0, S0=0, C0=0, S1=0, C1=0, S2=0, C2=0, S3=0, C3=0):

        self.engIdx = engIdx
        # Currently, aswing supports three engine types:
        #
        #  type 0: A simple proportional engine model.  Peng is an arbitrary control variable,
        #          and thrust and torque are set using the two gains:
        #             T = dFdPe*Peng
        #             Q = dMdPe*Peng
        #             F = ( Tx , Ty , Tz ) * T
        #             M = ( Ty , Ty , Tz ) * Q
        #
        #  type 1: An extended actuator-disk model, with thrust and torque set by the model
        #             T = f( Peng , rho , V , Rdisk )
        #         Q = Peng/Omega
        #
        # type 2: Same as type 1, with added P-factor terms for prop whirl prediction
        #
        # The Steady theory document asw.ps describes types 0 and 1.
        # The Unsteady theory document aswu.ps describes the added terms in type 2.
        self.engType = engType
        self.beamIdx = beamIdx
        self.t = t
        self.Xp = Xp
        self.Yp = Yp
        self.Zp = Zp
        self.Tx = Tx
        self.Ty = Ty
        self.Tz = Tz
        self.dFdPe = dFdPe
        self.dMdPe = dMdPe

        # for engine type 1
        self.Rdisk = Rdisk
        self.Omega = Omega
        self.cdA = cdA

        # for engine type 2
        self.cl = cl
        self.CLa = CLa
        self.S0 = S0
        self.C0 = C0
        self.S1 = S1
        self.C1 = C1
        self.S2 = S2
        self.C2 = C2
        self.S3 = S3
        self.C3 = C3

class strut:
    def __init__(self,beamIdx, t, Xp, Yp, Zp, Xw, Yw, Zw, dL, EAw):
        # This allows the specification of struts or wires, each of which is attached
        # at the end of a rigid pylon cantilevered from the specified t location
        # of beam number "beamIdx"
        self.beamIdx = beamIdx
        self.t = t

        # Xp, Yp, Zp, give the location for the other pylon
        # endpoint just like for the point-masses.  Here, this other endpoint is
        # where one end of the strut is attached.
        self.Xp = Xp
        self.Yp = Yp
        self.Zp = Zp

        # Xw, Yw, Zw give the location of the anchor at the other end of
        # the strut.  This other end is assumed to be grounded (i.e. fixed
        # in the x,y,z coordinate system).
        self.Xw = Xw
        self.Yw = Yw
        self.Zw = Zw
        self.dL = dL

        # EAw is the extensional stiffness of the strut.  It can be made huge
        # to simulate an effectively-rigid strut.
        #
        # As in the case of point-masses, multiple struts at the same location
        # have their contributions superimposed.  The net effective EA is simply
        # the sum of the individual EA values.
        self.EAw = EAw

class joint:
    def __init__(self, beam1Idx, beam2Idx, t1, t2, jointType = 0):
        # This allows specification of some number of "joints" between
        # pairs of beams.
        # A joint is a rigid pylon linking the two beams at the specified t locations.
        self.beam1Idx = beam1Idx
        self.beam2Idx = beam2Idx
        self.t1 = t1
        self.t2 = t2

        # The optional KJtype indicator specifies the type of joint:
        #
        # KJtype = 0:  Both translation and rotation matched    (rigid  joint)
        #  1:  Only translation matched                 (pinned joint)
        #  2:  Only rotation matched                    (roller joint)
        #  3:  translation matched, two angles matched,
        #      one remaining angle depends on moment    (sprung-hinge joint)
        self.jointType = jointType

class jangle:
    def __init__(self, jointIdx, hx, hy, hz, Momh, Angh):
        self.jointIdx = jointIdx
        self.hx = hx
        self.hy = hy
        self.hz = hz
        self.Momh = Momh
        self.Angh = Angh

class ground:
    def __init__(self, beamIdx, t, groundType =0):
        self.beamIdx = beamIdx
        self.t = t
        self.groundType = groundType

class beam:
    def __init__(self, beamIdx, physicalIdx, name, spanwise):
        self.beamIdx = beamIdx
        self.physicalIdx = physicalIdx
        self.name = name
        self.spanwise = spanwise


text:
import torch
import torchvision
import argparse
import json
import numpy as np
from PIL import Image
import torch.nn.functional as F

def parse_args():
    parser = argparse.ArgumentParser()
    parser.add_argument('image_path', metavar='image_path', type=str, default='flowers/test/10/image_07090.jpg')
    parser.add_argument('--class_names', action='store', dest='class_names', type=str, default='cat_to_name.json')
    parser.add_argument('checkpoint', metavar='checkpoint', type=str, default='train_checkpoint.pth')
    parser.add_argument('--top_k', action='store', dest="top_k", type=int, default=5)
    parser.add_argument('--gpu', action='store_true', default=False)
    return parser.parse_args()

def load_checkpoint(filepath):
    checkpoint = torch.load(filepath, map_location=lambda storage, loc: storage)
    model = getattr(torchvision.models, checkpoint['structure'])(pretrained=True)
    model.input_size= checkpoint['input_size']
    model.output_size = checkpoint['output_size']
    model.learning_rate = checkpoint['learning_rate']
    model.hidden = checkpoint['hidden_units']
    model.learning_rate = checkpoint['learning_rate']
    model.classifier= checkpoint['classifier']
    model.epochs = checkpoint['epochs']
    model.class_to_idx = checkpoint['class_to_idx']
    model.optimizer= checkpoint['optimizer']
    model.load_state_dict(checkpoint['state_dict'])
    return model

def process_image(image):
    resize = 256
    crop_size = 224
    (w,h) = image.size

    if h > w:
        h = int(max(h * resize / w, 1))
        w = int(resize)
    else:
        w = int(max(w * resize / h, 1))
        h = int(resize)

    img = image.resize((w, h))

    left = (w - crop_size) / 2
    top = (h - crop_size) / 2
    right = left + crop_size
    bottom = top + crop_size
    img = img.crop((left, top, right, bottom))

    img = np.array(img)
    img = img / 255
    mean = np.array([0.485, 0.456, 0.406])
    std = np.array([0.229, 0.224, 0.225])
    img = (img - mean) / std
    img = np.transpose(img, (2, 0, 1))
    return img

def predict(image_path, model, top_k, gpu):
    if torch.cuda.is_available():
        device = torch.device("cuda:0")
    else:
        device = torch.device("cpu")
    model.to(device)

    image = Image.open(image_path)
    image = process_image(image)
    image = torch.from_numpy(image).float()
    image = image.unsqueeze_(0)

    with torch.no_grad():
        output = model.forward(image.cuda())
    prob = F.softmax(output.data, dim=1)
    probs = np.array(prob.topk(top_k)[0][0])
    idx_to_class = {val: key for key, val in model.class_to_idx.items()}
    flowers = [np.int(idx_to_class[each]) for each in np.array(prob.topk(top_k)[1][0])]

    return probs, flowers, device

def load_names(class_names_file):
    with open(class_names_file) as file:
        class_names = json.load(file)
    return class_names

def main():
    args = parse_args()
    image_path = args.image_path
    checkpoint = args.checkpoint
    class_names = args.class_names
    top_k = args.top_k
    gpu = args.gpu

    model = load_checkpoint(checkpoint)
    probs, flowers, device = predict(image_path, model, top_k, gpu)
    class_names = load_names(class_names)
    labels = [class_names[str(index)] for index in flowers]

    print("prediction for image {}\n{}\n{}".format(image_path,labels,probs))
    for i in range(len(labels)):
        print("{} - {} with a probability of {}".format((i+1), labels[i], probs[i]))

if __name__ == "__main__":
    main()

ext: py

max_stars_repo_path: predict (1).py

max_issues_repo_path: predict (1).py

max_forks_repo_path: predict (1).py

reason: import numpy


text:
from ScopeFoundry.data_browser import DataBrowserView
import numpy as np
import h5py
import pyqtgraph as pg
from qtpy import QtWidgets

class APD_ASI_2DSlowScanView(DataBrowserView):

    name = 'APD_ASI_2DSlowScan'

    def setup(self):

        self.ui = self.imview = pg.ImageView()
        self.imview.getView().invertY(False) # lower left origin

        #self.graph_layout = pg.GraphicsLayoutWidget()
        #self.graph_layout.addPlot()

    def on_change_data_filename(self, fname):

        try:
            self.dat = h5py.File(fname, 'r')
            self.im_data = np.array(self.dat['measurement/APD_ASI_2DSlowScan/count_rate_map'][0,:,:].T) # grab first frame
            self.imview.setImage(self.im_data)
        except Exception as err:
            self.imview.setImage(np.zeros((10,10)))
            self.databrowser.ui.statusbar.showMessage("failed to load %s:\n%s" %(fname, err))
            raise(err)

    def is_file_supported(self, fname):
        return ("APD_ASI_2DSlowScan.h5" in fname)

max_stars_repo_name: ScopeFoundry/FoundryDataBrowser

max_issues_repo_name: ScopeFoundry/FoundryDataBrowser

max_forks_repo_name: ScopeFoundry/FoundryDataBrowser


text:
from io import BytesIO

# import numpy as np

from bw2calc.utils import get_seed, wrap_functional_unit, os, load_arrays, np
import multiprocessing
import pytest
import sys

MAX_INT_32 = 4294967295

@pytest.mark.skipif(sys.version_info < (3,0), reason="MP pool changes")
def test_get_seeds_different_under_mp_pool():
    with multiprocessing.Pool(processes=4) as pool:
        results = list(pool.map(get_seed, [None] * 10))
    assert sorted(set(results)) == sorted(results)

def test_wrap_functional_unit():
    given = {17: 42}
    expected = {'key': 17, 'amount': 42}
    assert wrap_functional_unit(given) == [expected]

    given = {('a', 'b'): 42}
    expected = {'database': 'a', 'code': 'b', 'amount': 42}
    assert wrap_functional_unit(given) == [expected]

    class Foo:
        def __getitem__(self, index):
            if index == 0:
                return 'a'
            elif index == 1:
                return 'b'

    given = {Foo(): 42}
    expected = {'database': 'a', 'code': 'b', 'amount': 42}
    assert wrap_functional_unit(given) == [expected]

def test_load_arrays_order(monkeypatch):
    monkeypatch.setattr(np, "load", lambda x, allow_pickle: x)
    monkeypatch.setattr(np, "hstack", lambda x: x)
    given = [1, "a", np.ones(5), 2, "b", np.zeros(3)]
    result = load_arrays(given)
    assert result[0].sum() == 5
    assert result[1].sum() == 0
    assert result[2:] == ["a", "b", 1, 2]

def test_load_arrays_string():
    # use the independent lca fixture data
    fpath = os.path.join(os.path.dirname(__file__), "fixtures", "independent", "ia.npy")

    dtype = [
        ('flow', np.uint32),
        ('row', np.uint32),
        ('col', np.uint32),
        ('geo', np.uint32),
        ('amount', np.float32),
    ]
    expected_array = np.asarray(
        [(10, MAX_INT_32, MAX_INT_32, 17, 1),
         (11, MAX_INT_32, MAX_INT_32, 17, 10)],
        dtype=dtype)

    actual_array = load_arrays([fpath])

    assert np.all(actual_array == expected_array), "Failed loading array from filepath"

def test_load_arrays_bytesio():
    dtype = [
        ('flow', np.uint32),
        ('row', np.uint32),
        ('col', np.uint32),
        ('geo', np.uint32),
        ('amount', np.float32),
    ]
    expected_array = np.asarray(
        [(10, MAX_INT_32, MAX_INT_32, 17, 1),
         (11, MAX_INT_32, MAX_INT_32, 17, 10.2)],
        dtype=dtype)

    with BytesIO() as b:
        np.save(b, expected_array, allow_pickle=False)
        b.seek(0)  # return to beginning of file
        actual_array = load_arrays([b])

    assert np.all(actual_array == expected_array), "Failed loading array from bytes"


text:
import keras.layers
import numpy as np
import random
import string
import tensorflow as tf

def random_string(length):
    """
    Generate a random string for the layer name.
    :param length: a length of required random string
    :return: generated random string
    """
    return ''.join(random.choice(string.ascii_letters) for _ in range(length))

def convert_conv(params, w_name, scope_name, inputs, layers, weights, short_names):
    """
    Convert convolution layer.

    Args:
        params: dictionary with layer parameters
        w_name: name prefix in state_dict
        scope_name: pytorch scope name
        inputs: pytorch node inputs
        layers: dictionary with keras tensors
        weights: pytorch state_dict
        short_names: use short names for keras layers
        short_names: use short names
    """
    print('Converting convolution ...')

    if short_names:
        tf_name = 'C' + random_string(7)
    else:
        tf_name = w_name + str(random.random())

    bias_name = '{0}.bias'.format(w_name)
    weights_name = '{0}.weight'.format(w_name)
    input_name = inputs[0]

    if len(weights[weights_name].numpy().shape) == 5: # 3D conv
        W = weights[weights_name].numpy().transpose(2, 3, 4, 1, 0)
        height, width, channels, n_layers, n_filters = W.shape

        if bias_name in weights:
            biases = weights[bias_name].numpy()
            has_bias = True
        else:
            biases = None
            has_bias = False

        if params['pads'][0] > 0 or params['pads'][1] > 0:
            padding_name = tf_name + '_pad'
            padding_layer = keras.layers.ZeroPadding3D(
                padding=(params['pads'][0],
                         params['pads'][1],
                         params['pads'][2]),
                name=padding_name
            )
            layers[padding_name] = padding_layer(layers[input_name])
            input_name = padding_name

        if has_bias:
            weights = [W, biases]
        else:
            weights = [W]

        conv = keras.layers.Conv3D(
            filters=n_filters,
            kernel_size=(channels, height, width),
            strides=(params['strides'][0],
                     params['strides'][1],
                     params['strides'][2]),
            padding='valid',
            weights=weights,
            use_bias=has_bias,
            activation=None,
            dilation_rate=params['dilations'][0],
            bias_initializer='zeros', kernel_initializer='zeros',
            name=tf_name
        )
        layers[scope_name] = conv(layers[input_name])
    elif len(weights[weights_name].numpy().shape) == 4:  # 2D conv
        if params['pads'][0] > 0 or params['pads'][1] > 0:
            padding_name = tf_name + '_pad'
            padding_layer = keras.layers.ZeroPadding2D(
                padding=(params['pads'][0], params['pads'][1]),
                name=padding_name
            )
            layers[padding_name] = padding_layer(layers[input_name])
            input_name = padding_name

        W = weights[weights_name].numpy().transpose(2, 3, 1, 0)
        height, width, channels_per_group, out_channels = W.shape
        n_groups = params['group']
        in_channels = channels_per_group * n_groups

        if n_groups == in_channels:
            print('Perform depthwise convolution: h={} w={} in={} out={}'
                .format(height, width, in_channels, out_channels))

            if bias_name in weights:
                biases = weights[bias_name].numpy()
                has_bias = True
            else:
                biases = None
                has_bias = False

            # We are just doing depthwise conv, so make the pointwise a no-op
            pointwise_wt = np.expand_dims(np.expand_dims(np.identity(out_channels), 0), 0)
            W = W.transpose(0, 1, 3, 2)
            if has_bias:
                weights = [W, pointwise_wt, biases]
            else:
                weights = [W, pointwise_wt]

            conv = keras.layers.SeparableConv2D(
                filters=out_channels,
                depth_multiplier=1,
                kernel_size=(height, width),
                strides=(params['strides'][0], params['strides'][1]),
                padding='valid',
                weights=weights,
                use_bias=has_bias,
                activation=None,
                bias_initializer='zeros', kernel_initializer='zeros',
                name=tf_name
            )
            layers[scope_name] = conv(layers[input_name])

        elif n_groups != 1:
            # Example from https://kratzert.github.io/2017/02/24/finetuning-alexnet-with-tensorflow.html
            # # Split input and weights and convolve them separately
            # input_groups = tf.split(axis=3, num_or_size_splits=groups, value=x)
            # weight_groups = tf.split(axis=3, num_or_size_splits=groups, value=weights)
            # output_groups = [convolve(i, k) for i, k in zip(input_groups, weight_groups)]

            # # Concat the convolved output together again
            # conv = tf.concat(axis=3, values=output_groups)
            def target_layer(x, groups=params['group'], stride_y=params['strides'][0], stride_x=params['strides'][1]):
                x = tf.transpose(x, [0, 2, 3, 1])

                convolve = lambda i, k: tf.nn.conv2d(i, k,
                                                     strides=[1, stride_y, stride_x, 1],
                                                     padding='VALID')

                input_groups = tf.split(axis=3, num_or_size_splits=groups, value=x)
                weight_groups = tf.split(axis=3, num_or_size_splits=groups, value=W.transpose(0, 1, 2, 3))
                output_groups = [convolve(i, k) for i, k in zip(input_groups, weight_groups)]

                layer = tf.concat(axis=3, values=output_groups)

                layer = tf.transpose(layer, [0, 3, 1, 2])
                return layer

            lambda_layer = keras.layers.Lambda(target_layer)
            layers[scope_name] = lambda_layer(layers[input_name])

        else:
            if bias_name in weights:
                biases = weights[bias_name].numpy()
                has_bias = True
            else:
                biases = None
                has_bias = False

            if has_bias:
                weights = [W, biases]
            else:
                weights = [W]

            conv = keras.layers.Conv2D(
                filters=out_channels,
                kernel_size=(height, width),
                strides=(params['strides'][0], params['strides'][1]),
                padding='valid',
                weights=weights,
                use_bias=has_bias,
                activation=None,
                dilation_rate=params['dilations'][0],
                bias_initializer='zeros', kernel_initializer='zeros',
                name=tf_name
            )
            layers[scope_name] = conv(layers[input_name])
    else:  # 1D conv
        W = weights[weights_name].numpy().transpose(2, 1, 0)
        width, channels, n_filters = W.shape

        if bias_name in weights:
            biases = weights[bias_name].numpy()
            has_bias = True
        else:
            biases = None
            has_bias = False

        padding_name = tf_name + '_pad'
        padding_layer = keras.layers.ZeroPadding1D(
            padding=params['pads'][0],
            name=padding_name
        )
        layers[padding_name] = padding_layer(layers[inputs[0]])
        input_name = padding_name

        if has_bias:
            weights = [W, biases]
        else:
            weights = [W]

        conv = keras.layers.Conv1D(
            filters=n_filters,
            kernel_size=width,
            strides=params['strides'][0],
            padding='valid',
            weights=weights,
            use_bias=has_bias,
            activation=None,
            dilation_rate=params['dilations'][0],
            bias_initializer='zeros', kernel_initializer='zeros',
            name=tf_name
        )
        layers[scope_name] = conv(layers[input_name])

def convert_convtranspose(params, w_name, scope_name, inputs, layers, weights, short_names):
    """
    Convert transposed convolution layer.

    Args:
        params: dictionary with layer parameters
        w_name: name prefix in state_dict
        scope_name: pytorch scope name
        inputs: pytorch node inputs
        layers: dictionary with keras tensors
        weights: pytorch state_dict
        short_names: use short names for keras layers
    """
    print('Converting transposed convolution ...')

    if short_names:
        tf_name = 'C' + random_string(7)
    else:
        tf_name = w_name + str(random.random())

    bias_name = '{0}.bias'.format(w_name)
    weights_name = '{0}.weight'.format(w_name)

    if len(weights[weights_name].numpy().shape) == 4:
        W = weights[weights_name].numpy().transpose(2, 3, 1, 0)
        height, width, n_filters, channels = W.shape

        if bias_name in weights:
            biases = weights[bias_name].numpy()
            has_bias = True
        else:
            biases = None
            has_bias = False

        input_name = inputs[0]

        if has_bias:
            weights = [W, biases]
        else:
            weights = [W]

        conv = keras.layers.Conv2DTranspose(
            filters=n_filters,
            kernel_size=(height, width),
            strides=(params['strides'][0], params['strides'][1]),
            padding='valid',
            output_padding=0,
            weights=weights,
            use_bias=has_bias,
            activation=None,
            dilation_rate=params['dilations'][0],
            bias_initializer='zeros', kernel_initializer='zeros',
            name=tf_name
        )

        layers[scope_name] = conv(layers[input_name])

        # Magic ad-hoc.
        # See the Keras issue: https://github.com/keras-team/keras/issues/6777
        layers[scope_name].set_shape(layers[scope_name]._keras_shape)

        pads = params['pads']
        if pads[0] > 0:
            assert(len(pads) == 2 or (pads[2] == pads[0] and pads[3] == pads[1]))

            crop = keras.layers.Cropping2D(
                pads[:2],
                name=tf_name + '_crop'
            )
            layers[scope_name] = crop(layers[scope_name])
    else:
        raise AssertionError('Layer is not supported for now')

def convert_flatten(params, w_name, scope_name, inputs, layers, weights, short_names):
    """
    Convert reshape(view).

    Args:
        params: dictionary with layer parameters
        w_name: name prefix in state_dict
        scope_name: pytorch scope name
        inputs: pytorch node inputs
        layers: dictionary with keras tensors
        weights: pytorch state_dict
        short_names: use short names for keras layers
    """
    print('Converting flatten ...')

    if short_names:
        tf_name = 'R' + random_string(7)
    else:
        tf_name = w_name + str(random.random())

    reshape = keras.layers.Reshape([-1], name=tf_name)
    layers[scope_name] = reshape(layers[inputs[0]])

def convert_gemm(params, w_name, scope_name, inputs, layers, weights, short_names):
    """
    Convert Linear.

    Args:
        params: dictionary with layer parameters
        w_name: name prefix in state_dict
        scope_name: pytorch scope name
        inputs: pytorch node inputs
        layers: dictionary with keras tensors
        weights: pytorch state_dict
        short_names: use short names for keras layers
    """
    print('Converting Linear ...')

    if short_names:
        tf_name = 'FC' + random_string(6)
    else:
        tf_name = w_name + str(random.random())

    bias_name = '{0}.bias'.format(w_name)
    weights_name = '{0}.weight'.format(w_name)

    W = weights[weights_name].numpy().transpose()
    input_channels, output_channels = W.shape

    keras_weights = [W]
    has_bias = False
    if bias_name in weights:
        bias = weights[bias_name].numpy()
        keras_weights = [W, bias]
        has_bias = True

    dense = keras.layers.Dense(
        output_channels,
        weights=keras_weights, use_bias=has_bias, name=tf_name, bias_initializer='zeros', kernel_initializer='zeros',
    )

    layers[scope_name] = dense(layers[inputs[0]])

def convert_avgpool(params, w_name, scope_name, inputs, layers, weights, short_names):
    """
    Convert Average pooling.

    Args:
        params: dictionary with layer parameters
        w_name: name prefix in state_dict
        scope_name: pytorch scope name
        inputs: pytorch node inputs
        layers: dictionary with keras tensors
        weights: pytorch state_dict
        short_names: use short names for keras layers
    """
    print('Converting pooling ...')

    if short_names:
        tf_name = 'P' + random_string(7)
    else:
        tf_name = w_name + str(random.random())

    height, width = params['kernel_shape']
    stride_height, stride_width = params['strides']
    padding_h, padding_w, _, _ = params['pads']

    input_name = inputs[0]
    padding = 'valid'
    if padding_h > 0 and padding_w > 0:
        if padding_h == height // 2 and padding_w == width // 2:
            padding = 'same'
        else:
            raise AssertionError('Custom padding isnt supported')

    pooling = keras.layers.AveragePooling2D(
        pool_size=(height, width),
        strides=(stride_height, stride_width),
        padding=padding,
        name=tf_name
    )

    layers[scope_name] = pooling(layers[input_name])

def convert_maxpool(params, w_name, scope_name, inputs, layers, weights, short_names):
    """
    Convert Max pooling.

    Args:
        params: dictionary with layer parameters
        w_name: name prefix in state_dict
        scope_name: pytorch scope name
        inputs: pytorch node inputs
        layers: dictionary with keras tensors
        weights: pytorch state_dict
        short_names: use short names for keras layers
    """

    print('Converting pooling ...')

    if short_names:
        tf_name = 'P' + random_string(7)
    else:
        tf_name = w_name + str(random.random())

    if 'kernel_shape' in params:
        height, width = params['kernel_shape']
    else:
        height, width = params['kernel_size']

    if 'strides' in params:
        stride_height, stride_width = params['strides']
    else:
        stride_height, stride_width = params['stride']

    if 'pads' in params:
        padding_h, padding_w, _, _ = params['pads']
    else:
        padding_h, padding_w = params['padding']

    input_name = inputs[0]
    if padding_h > 0 and padding_w > 0:
        padding_name = tf_name + '_pad'
        padding_layer = keras.layers.ZeroPadding2D(
            padding=(padding_h, padding_w),
            name=padding_name
        )
        layers[padding_name] = padding_layer(layers[inputs[0]])
        input_name = padding_name

    # Pooling type
    pooling = keras.layers.MaxPooling2D(
        pool_size=(height, width),
        strides=(stride_height, stride_width),
        padding='valid',
        name=tf_name
    )

    layers[scope_name] = pooling(layers[input_name])

def convert_maxpool3(params, w_name, scope_name, inputs, layers, weights, short_names):
    """
    Convert 3d Max pooling.

    Args:
        params: dictionary with layer parameters
        w_name: name prefix in state_dict
        scope_name: pytorch scope name
        inputs: pytorch node inputs
        layers: dictionary with keras tensors
        weights: pytorch state_dict
        short_names: use short names for keras layers
    """

    print('Converting pooling ...')

    if short_names:
        tf_name = 'P' + random_string(7)
    else:
        tf_name = w_name + str(random.random())

    if 'kernel_shape' in params:
        height, width, depth = params['kernel_shape']
    else:
        height, width, depth = params['kernel_size']

    if 'strides' in params:
        stride_height, stride_width, stride_depth = params['strides']
    else:
        stride_height, stride_width, stride_depth = params['stride']

    if 'pads' in params:
        padding_h, padding_w, padding_d, _, _ = params['pads']
    else:
        padding_h, padding_w, padding_d = params['padding']

    input_name = inputs[0]
    if padding_h > 0 and padding_w > 0 and padding_d > 0:
        padding_name = tf_name + '_pad'
        padding_layer = keras.layers.ZeroPadding3D(
            padding=(padding_h, padding_w, padding_d),
            name=padding_name
        )
        layers[padding_name] = padding_layer(layers[inputs[0]])
        input_name = padding_name

    # Pooling type
    pooling = keras.layers.MaxPooling3D(
        pool_size=(height, width, depth),
        strides=(stride_height, stride_width, stride_depth),
        padding='valid',
        name=tf_name
    )

    layers[scope_name] = pooling(layers[input_name])

def convert_dropout(params, w_name, scope_name, inputs, layers, weights, short_names):
    """
    Convert dropout.

    Args:
        params: dictionary with layer parameters
        w_name: name prefix in state_dict
        scope_name: pytorch scope name
        inputs: pytorch node inputs
        layers: dictionary with keras tensors
        weights: pytorch state_dict
        short_names: use short names for keras layers
    """
    print('Converting dropout ...')

    if short_names:
        tf_name = 'DO' + random_string(6)
    else:
        tf_name = w_name + str(random.random())

    dropout = keras.layers.Dropout(rate=params['ratio'], name=tf_name)
    layers[scope_name] = dropout(layers[inputs[0]])

def convert_batchnorm(params, w_name, scope_name, inputs, layers, weights, short_names):
    """
    Convert batch normalization layer.

    Args:
        params: dictionary with layer parameters
        w_name: name prefix in state_dict
        scope_name: pytorch scope name
        inputs: pytorch node inputs
        layers: dictionary with keras tensors
        weights: pytorch state_dict
        short_names: use short names for keras layers
    """
    print('Converting batchnorm ...')

    if short_names:
        tf_name = 'BN' + random_string(6)
    else:
        tf_name = w_name + str(random.random())

    bias_name = '{0}.bias'.format(w_name)
    weights_name = '{0}.weight'.format(w_name)
    mean_name = '{0}.running_mean'.format(w_name)
    var_name = '{0}.running_var'.format(w_name)

    if bias_name in weights:
        beta = weights[bias_name].numpy()

    if weights_name in weights:
        gamma = weights[weights_name].numpy()

    mean = weights[mean_name].numpy()
    variance = weights[var_name].numpy()

    eps = params['epsilon']
    momentum = params['momentum']

    if weights_name not in weights:
        bn = keras.layers.BatchNormalization(
            axis=1, momentum=momentum, epsilon=eps,
            center=False, scale=False,
            weights=[mean, variance],
            name=tf_name
        )
    else:
        bn = keras.layers.BatchNormalization(
            axis=1, momentum=momentum, epsilon=eps,
            weights=[gamma, beta, mean, variance],
            name=tf_name
        )
    layers[scope_name] = bn(layers[inputs[0]])

def convert_instancenorm(params, w_name, scope_name, inputs, layers, weights, short_names):
    """
    Convert instance normalization layer.

    Args:
        params: dictionary with layer parameters
        w_name: name prefix in state_dict
        scope_name: pytorch scope name
        inputs: pytorch node inputs
        layers: dictionary with keras tensors
        weights: pytorch state_dict
        short_names: use short names for keras layers
    """
    print('Converting instancenorm ...')

    if short_names:
        tf_name = 'IN' + random_string(6)
    else:
        tf_name = w_name + str(random.random())

    assert(len(inputs) == 3)

    gamma = layers[inputs[-2]]
    beta = layers[inputs[-1]]

    def target_layer(x, epsilon=params['epsilon'], gamma=gamma, beta=beta):
        layer = tf.contrib.layers.instance_norm(x,
            param_initializers={'beta': tf.constant_initializer(beta), 'gamma': tf.constant_initializer(gamma)},
            epsilon=epsilon, data_format='NCHW',
            trainable=False)
        return layer

    lambda_layer = keras.layers.Lambda(target_layer)
    layers[scope_name] = lambda_layer(layers[inputs[0]])

def convert_elementwise_add(
    params, w_name, scope_name, inputs, layers, weights, short_names
):
    """
    Convert elementwise addition.

    Args:
        params: dictionary with layer parameters
        w_name: name prefix in state_dict
        scope_name: pytorch scope name
        inputs: pytorch node inputs
        layers: dictionary with keras tensors
        weights: pytorch state_dict
        short_names: use short names for keras layers
    """
    print('Converting elementwise_add ...')
    model0 = layers[inputs[0]]
    model1 = layers[inputs[1]]

    if short_names:
        tf_name = 'A' + random_string(7)
    else:
        tf_name = w_name + str(random.random())

    add = keras.layers.Add(name=tf_name)
    layers[scope_name] = add([model0, model1])

def convert_elementwise_mul(
    params, w_name, scope_name, inputs, layers, weights, short_names
):
    """
    Convert elementwise multiplication.

    Args:
        params: dictionary with layer parameters
        w_name: name prefix in state_dict
        scope_name: pytorch scope name
        inputs: pytorch node inputs
        layers: dictionary with keras tensors
        weights: pytorch state_dict
        short_names: use short names for keras layers
    """
    print('Converting elementwise_mul ...')
    model0 = layers[inputs[0]]
    model1 = layers[inputs[1]]

    if short_names:
        tf_name = 'M' + random_string(7)
    else:
        tf_name = w_name + str(random.random())

    mul = keras.layers.Multiply(name=tf_name)
    layers[scope_name] = mul([model0, model1])

def convert_elementwise_sub(
    params, w_name, scope_name, inputs, layers, weights, short_names
):
    """
    Convert elementwise subtraction.

    Args:
        params: dictionary with layer parameters
        w_name: name prefix in state_dict
        scope_name: pytorch scope name
        inputs: pytorch node inputs
        layers: dictionary with keras tensors
        weights: pytorch state_dict
        short_names: use short names for keras layers
    """
    print('Converting elementwise_sub ...')
    model0 = layers[inputs[0]]
    model1 = layers[inputs[1]]

    if short_names:
        tf_name = 'S' + random_string(7)
    else:
        tf_name = w_name + str(random.random())

    sub = keras.layers.Subtract(name=tf_name)
    layers[scope_name] = sub([model0, model1])

def convert_sum(
    params, w_name, scope_name, inputs, layers, weights, short_names
):
    """
    Convert sum.

    Args:
        params: dictionary with layer parameters
        w_name: name prefix in state_dict
        scope_name: pytorch scope name
        inputs: pytorch node inputs
        layers: dictionary with keras tensors
        weights: pytorch state_dict
        short_names: use short names for keras layers
    """
    print('Converting Sum ...')

    def target_layer(x):
        import keras.backend as K
        return K.sum(x)

    lambda_layer = keras.layers.Lambda(target_layer)
    layers[scope_name] = lambda_layer(layers[inputs[0]])

def convert_concat(params, w_name, scope_name, inputs, layers, weights, short_names):
    """
    Convert concatenation.

    Args:
        params: dictionary with layer parameters
        w_name: name prefix in state_dict
        scope_name: pytorch scope name
        inputs: pytorch node inputs
        layers: dictionary with keras tensors
        weights: pytorch state_dict
        short_names: use short names for keras layers
    """
    print('Converting concat ...')
    concat_nodes = [layers[i] for i in inputs]

    if len(concat_nodes) == 1:
        # no-op
        layers[scope_name] = concat_nodes[0]
        return

    if short_names:
        tf_name = 'CAT' + random_string(5)
    else:
        tf_name = w_name + str(random.random())

    cat = keras.layers.Concatenate(name=tf_name, axis=params['axis'])
    layers[scope_name] = cat(concat_nodes)

def convert_relu(params, w_name, scope_name, inputs, layers, weights, short_names):
    """
    Convert relu layer.

   Args:
        params: dictionary with layer parameters
        w_name: name prefix in state_dict
        scope_name: pytorch scope name
        inputs: pytorch node inputs
        layers: dictionary with keras tensors
        weights: pytorch state_dict
        short_names: use short names for keras layers
    """
    print('Converting relu ...')

    if short_names:
        tf_name = 'RELU' + random_string(4)
    else:
        tf_name = w_name + str(random.random())

    relu = keras.layers.Activation('relu', name=tf_name)
    layers[scope_name] = relu(layers[inputs[0]])

def convert_lrelu(params, w_name, scope_name, inputs, layers, weights, short_names):
    """
    Convert leaky relu layer.

   Args:
        params: dictionary with layer parameters
        w_name: name prefix in state_dict
        scope_name: pytorch scope name
        inputs: pytorch node inputs
        layers: dictionary with keras tensors
        weights: pytorch state_dict
        short_names: use short names for keras layers
    """
    print('Converting lrelu ...')

    if short_names:
        tf_name = 'lRELU' + random_string(3)
    else:
        tf_name = w_name + str(random.random())

    leakyrelu = \
        keras.layers.LeakyReLU(alpha=params['alpha'], name=tf_name)
    layers[scope_name] = leakyrelu(layers[inputs[0]])

def convert_sigmoid(params, w_name, scope_name, inputs, layers, weights, short_names):
    """
    Convert sigmoid layer.

   Args:
        params: dictionary with layer parameters
        w_name: name prefix in state_dict
        scope_name: pytorch scope name
        inputs: pytorch node inputs
        layers: dictionary with keras tensors
        weights: pytorch state_dict
        short_names: use short names for keras layers
    """
    print('Converting sigmoid ...')

    if short_names:
        tf_name = 'SIGM' + random_string(4)
    else:
        tf_name = w_name + str(random.random())

    sigmoid = keras.layers.Activation('sigmoid', name=tf_name)
    layers[scope_name] = sigmoid(layers[inputs[0]])

def convert_softmax(params, w_name, scope_name, inputs, layers, weights, short_names):
    """
    Convert softmax layer.

   Args:
        params: dictionary with layer parameters
        w_name: name prefix in state_dict
        scope_name: pytorch scope name
        inputs: pytorch node inputs
        layers: dictionary with keras tensors
        weights: pytorch state_dict
        short_names: use short names for keras layers
    """
    print('Converting softmax ...')

    if short_names:
        tf_name = 'SMAX' + random_string(4)
    else:
        tf_name = w_name + str(random.random())

    softmax = keras.layers.Activation('softmax', name=tf_name)
    layers[scope_name] = softmax(layers[inputs[0]])

def convert_tanh(params, w_name, scope_name, inputs, layers, weights, short_names):
    """
    Convert tanh layer.

    Args:
        params: dictionary with layer parameters
        w_name: name prefix in state_dict
        scope_name: pytorch scope name
        inputs: pytorch node inputs
        layers: dictionary with keras tensors
        weights: pytorch state_dict
        short_names: use short names for keras layers
    """
    print('Converting tanh ...')

    if short_names:
        tf_name = 'TANH' + random_string(4)
    else:
        tf_name = w_name + str(random.random())

    tanh = keras.layers.Activation('tanh', name=tf_name)
    layers[scope_name] = tanh(layers[inputs[0]])

def convert_hardtanh(params, w_name, scope_name, inputs, layers, weights, short_names):
    """
    Convert hardtanh layer.

    Args:
        params: dictionary with layer parameters
        w_name: name prefix in state_dict
        scope_name: pytorch scope name
        inputs: pytorch node inputs
        layers: dictionary with keras tensors
        weights: pytorch state_dict
        short_names: use short names for keras layers
    """
    print('Converting hardtanh (clip) ...')

    def target_layer(x, max_val=float(params['max_val']), min_val=float(params['min_val'])):
        return tf.minimum(max_val, tf.maximum(min_val, x))

    lambda_layer = keras.layers.Lambda(target_layer)
    layers[scope_name] = lambda_layer(layers[inputs[0]])

def convert_selu(params, w_name, scope_name, inputs, layers, weights, short_names):
    """
    Convert selu layer.

    Args:
        params: dictionary with layer parameters
        w_name: name prefix in state_dict
        scope_name: pytorch scope name
        inputs: pytorch node inputs
        layers: dictionary with keras tensors
        weights: pytorch state_dict
        short_names: use short names for keras layers
    """
    print('Converting selu ...')

    if short_names:
        tf_name = 'SELU' + random_string(4)
    else:
        tf_name = w_name + str(random.random())

    selu = keras.layers.Activation('selu', name=tf_name)
    layers[scope_name] = selu(layers[inputs[0]])

def convert_transpose(params, w_name, scope_name, inputs, layers, weights, short_names):
    """
    Convert transpose layer.

   Args:
        params: dictionary with layer parameters
        w_name: name prefix in state_dict
        scope_name: pytorch scope name
        inputs: pytorch node inputs
        layers: dictionary with keras tensors
        weights: pytorch state_dict
        short_names: use short names for keras layers
    """
    print('Converting transpose ...')
    if params['perm'][0] != 0:
        # raise AssertionError('Cannot permute batch dimension')
        print('!!! Cannot permute batch dimension. Result may be wrong !!!')
        try:
            layers[scope_name] = layers[inputs[0]]
        except:
            pass
    else:
        if short_names:
            tf_name = 'PERM' + random_string(4)
        else:
            tf_name = w_name + str(random.random())
        permute = keras.layers.Permute(params['perm'][1:], name=tf_name)
        layers[scope_name] = permute(layers[inputs[0]])

def convert_reshape(params, w_name, scope_name, inputs, layers, weights, short_names):
    """
    Convert reshape layer.

   Args:
        params: dictionary with layer parameters
        w_name: name prefix in state_dict
        scope_name: pytorch scope name
        inputs: pytorch node inputs
        layers: dictionary with keras tensors
        weights: pytorch state_dict
        short_names: use short names for keras layers
    """
    print('Converting reshape ...')
    if short_names:
        tf_name = 'RESH' + random_string(4)
    else:
        tf_name = w_name + str(random.random())

    if len(inputs) > 1:
        if layers[inputs[1]][0] == -1:
            print('Cannot deduct batch size! It will be omitted, but result may be wrong.')

        reshape = keras.layers.Reshape(layers[inputs[1]][1:], name=tf_name)
        layers[scope_name] = reshape(layers[inputs[0]])
    else:
        reshape = keras.layers.Reshape(params['shape'][1:], name=tf_name)
        layers[scope_name] = reshape(layers[inputs[0]])

def convert_matmul(params, w_name, scope_name, inputs, layers, weights, short_names):
    """
    Convert matmul layer.

   Args:
        params: dictionary with layer parameters
        w_name: name prefix in state_dict
        scope_name: pytorch scope name
        inputs: pytorch node inputs
        layers: dictionary with keras tensors
        weights: pytorch state_dict
        short_names: use short names for keras layers
    """
    print('Converting matmul ...')

    if short_names:
        tf_name = 'MMUL' + random_string(4)
    else:
        tf_name = w_name + str(random.random())

    if len(inputs) == 1:
        weights_name = '{0}.weight'.format(w_name)

        W = weights[weights_name].numpy().transpose()
        input_channels, output_channels = W.shape

        keras_weights = [W]

        dense = keras.layers.Dense(
            output_channels,
            weights=keras_weights, use_bias=False, name=tf_name, bias_initializer='zeros', kernel_initializer='zeros',
        )
        layers[scope_name] = dense(layers[inputs[0]])
    elif len(inputs) == 2:
        weights_name = '{0}.weight'.format(w_name)

        W = weights[weights_name].numpy().transpose()
        input_channels, output_channels = W.shape

        keras_weights = [W]

        dense = keras.layers.Dense(
            output_channels,
            weights=keras_weights, use_bias=False, name=tf_name, bias_initializer='zeros', kernel_initializer='zeros',
        )
        layers[scope_name] = dense(layers[inputs[0]])
    else:
        raise AssertionError('Cannot convert matmul layer')

def convert_gather(params, w_name, scope_name, inputs, layers, weights, short_names):
    """
    Convert gather (embedding) layer.

   Args:
        params: dictionary with layer parameters
        w_name: name prefix in state_dict
        scope_name: pytorch scope name
        inputs: pytorch node inputs
        layers: dictionary with keras tensors
        weights: pytorch state_dict
        short_names: use short names for keras layers
    """
    print('Converting embedding ...')

    if short_names:
        tf_name = 'EMBD' + random_string(4)
    else:
        tf_name = w_name + str(random.random())

    weights_name = '{0}.weight'.format(w_name)

    W = weights[weights_name].numpy()
    input_channels, output_channels = W.shape

    keras_weights = [W]

    dense = keras.layers.Embedding(
        input_channels,
        weights=keras_weights, output_dim=output_channels, name=tf_name
    )
    layers[scope_name] = dense(layers[inputs[0]])

def convert_reduce_sum(params, w_name, scope_name, inputs, layers, weights, short_names):
    """
    Convert reduce_sum layer.

   Args:
        params: dictionary with layer parameters
        w_name: name prefix in state_dict
        scope_name: pytorch scope name
        inputs: pytorch node inputs
        layers: dictionary with keras tensors
        weights: pytorch state_dict
        short_names: use short names for keras layers
    """
    print('Converting reduce_sum ...')

    keepdims = params['keepdims'] > 0
    axis = params['axes']

    def target_layer(x, keepdims=keepdims, axis=axis):
        import keras.backend as K
        return K.sum(x, keepdims=keepdims, axis=axis)

    lambda_layer = keras.layers.Lambda(target_layer)
    layers[scope_name] = lambda_layer(layers[inputs[0]])

def convert_constant(params, w_name, scope_name, inputs, layers, weights, short_names):
    """
    Convert constant layer.

   Args:
        params: dictionary with layer parameters
        w_name: name prefix in state_dict
        scope_name: pytorch scope name
        inputs: pytorch node inputs
        layers: dictionary with keras tensors
        weights: pytorch state_dict
        short_names: use short names for keras layers
    """
    print('Converting constant ...')

    # params_list = params['value'].numpy().tolist()

    # def target_layer(x):
    #     import keras.backend as K
    #     return K.constant(params_list)

    # lambda_layer = keras.layers.Lambda(target_layer)
    # layers[scope_name] = lambda_layer(layers['input0']) # Temporary fix for nonexistent input name created by converter.py
    layers[scope_name] = params['value'].tolist()

def convert_upsample(params, w_name, scope_name, inputs, layers, weights, short_names):
    """
    Convert upsample_bilinear2d layer.

   Args:
        params: dictionary with layer parameters
        w_name: name prefix in state_dict
        scope_name: pytorch scope name
        inputs: pytorch node inputs
        layers: dictionary with keras tensors
        weights: pytorch state_dict
        short_names: use short names for keras layers
    """
    print('Converting upsample...')

    if params['mode'] != 'nearest':
        raise AssertionError('Cannot convert non-nearest upsampling')

    if short_names:
        tf_name = 'UPSL' + random_string(4)
    else:
        tf_name = w_name + str(random.random())

    scale = (params['height_scale'], params['width_scale'])
    upsampling = keras.layers.UpSampling2D(
        size=scale, name=tf_name
    )
    layers[scope_name] = upsampling(layers[inputs[0]])

def convert_padding(params, w_name, scope_name, inputs, layers, weights, short_names):
    """
    Convert padding layer.

    Args:
        params: dictionary with layer parameters
        w_name: name prefix in state_dict
        scope_name: pytorch scope name
        inputs: pytorch node inputs
        layers: dictionary with keras tensors
        weights: pytorch state_dict
        short_names: use short names for keras layers
    """
    print('Converting padding...')

    if params['mode'] == 'constant':
        # raise AssertionError('Cannot convert non-constant padding')

        if params['value'] != 0.0:
            raise AssertionError('Cannot convert non-zero padding')

        if short_names:
            tf_name = 'PADD' + random_string(4)
        else:
            tf_name = w_name + str(random.random())

        # Magic ordering
        padding_name = tf_name
        padding_layer = keras.layers.ZeroPadding2D(
            padding=((params['pads'][2], params['pads'][6]), (params['pads'][3], params['pads'][7])),
            name=padding_name
        )

        layers[scope_name] = padding_layer(layers[inputs[0]])
    elif params['mode'] == 'reflect':

        def target_layer(x, pads=params['pads']):
            # x = tf.transpose(x, [0, 2, 3, 1])
            layer = tf.pad(x, [[0, 0], [0, 0], [pads[2], pads[6]], [pads[3], pads[7]]], 'REFLECT')
            # layer = tf.transpose(layer, [0, 3, 1, 2])
            return layer

        lambda_layer = keras.layers.Lambda(target_layer)
        layers[scope_name] = lambda_layer(layers[inputs[0]])

def convert_adaptive_avg_pool2d(params, w_name, scope_name, inputs, layers, weights, short_names):
    """
    Convert adaptive_avg_pool2d layer.

    Args:
        params: dictionary with layer parameters
        w_name: name prefix in state_dict
        scope_name: pytorch scope name
        inputs: pytorch node inputs
        layers: dictionary with keras tensors
        weights: pytorch state_dict
        short_names: use short names for keras layers
    """
    print('Converting adaptive_avg_pool2d...')

    if short_names:
        tf_name = 'APOL' + random_string(4)
    else:
        tf_name = w_name + str(random.random())

    global_pool = keras.layers.GlobalAveragePooling2D()
    layers_global_pool = global_pool(layers[inputs[0]])

    def target_layer(x):
        return keras.backend.expand_dims(x)

    lambda_layer = keras.layers.Lambda(target_layer)
    layers[scope_name] = lambda_layer(layers_global_pool)

def convert_slice(params, w_name, scope_name, inputs, layers, weights, short_names):
    """
    Convert slice operation.

    Args:
        params: dictionary with layer parameters
        w_name: name prefix in state_dict
        scope_name: pytorch scope name
        inputs: pytorch node inputs
        layers: dictionary with keras tensors
        weights: pytorch state_dict
        short_names: use short names for keras layers
    """
    print('Converting slice ...')

    if len(params['axes']) > 1:
        raise AssertionError('Cannot convert slice by multiple dimensions')

    if params['axes'][0] not in [0, 1, 2, 3]:
        raise AssertionError('Slice by dimension more than 3 or less than 0 is not supported')

    def target_layer(x, axis=int(params['axes'][0]), start=int(params['starts'][0]), end=int(params['ends'][0])):
        if axis == 0:
            return x[start:end]
        elif axis == 1:
            return x[:, start:end]
        elif axis == 2:
            return x[:, :, start:end]
        elif axis == 3:
            return x[:, :, :, start:end]

    lambda_layer = keras.layers.Lambda(target_layer)
    layers[scope_name] = lambda_layer(layers[inputs[0]])

def convert_squeeze(params, w_name, scope_name, inputs, layers, weights, short_names):
    """
    Convert squeeze operation.

    Args:
        params: dictionary with layer parameters
        w_name: name prefix in state_dict
        scope_name: pytorch scope name
        inputs: pytorch node inputs
        layers: dictionary with keras tensors
        weights: pytorch state_dict
        short_names: use short names for keras layers
    """
    print('Converting squeeze ...')

    if len(params['axes']) > 1:
        raise AssertionError('Cannot convert squeeze by multiple dimensions')

    def target_layer(x, axis=int(params['axes'][0])):
        return tf.squeeze(x, axis=axis)

    lambda_layer = keras.layers.Lambda(target_layer)
    layers[scope_name] = lambda_layer(layers[inputs[0]])

AVAILABLE_CONVERTERS = {
    'onnx::Conv': convert_conv,
    'onnx::ConvTranspose': convert_convtranspose,
    'onnx::Flatten': convert_flatten,
    'onnx::Gemm': convert_gemm,
    'onnx::MaxPool': convert_maxpool,
    'max_pool2d': convert_maxpool,
    'aten::max_pool3d': convert_maxpool3,
    'aten::max_pool2d': convert_maxpool,
    'onnx::AveragePool': convert_avgpool,
    'onnx::Dropout': convert_dropout,
    'onnx::BatchNormalization': convert_batchnorm,
    'onnx::InstanceNormalization': convert_instancenorm,
    'onnx::Add': convert_elementwise_add,
    'onnx::Mul': convert_elementwise_mul,
    'onnx::Sub': convert_elementwise_sub,
    'onnx::Sum': convert_sum,
    'onnx::Concat': convert_concat,
    'onnx::Relu': convert_relu,
    'onnx::LeakyRelu': convert_lrelu,
    'onnx::Sigmoid': convert_sigmoid,
    'onnx::Softmax': convert_softmax,
    'onnx::Tanh': convert_tanh,
    'aten::hardtanh': convert_hardtanh,
    'onnx::Selu': convert_selu,
    'onnx::Transpose': convert_transpose,
    'onnx::Reshape': convert_reshape,
    'onnx::MatMul': convert_matmul,
    'onnx::Gather': convert_gather,
    'onnx::ReduceSum': convert_reduce_sum,
    'onnx::Constant': convert_constant,
    'onnx::Upsample': convert_upsample,
    'onnx::Pad': convert_padding,
    'aten::adaptive_avg_pool2d': convert_adaptive_avg_pool2d,
    'onnx::Slice': convert_slice,
    'onnx::Squeeze': convert_squeeze,
}


text:
import numpy as np
import matplotlib.pyplot as plt
import os
import os.path as osp

colours = ["red", "blue", "green", "cyan", "orange", "magenta"]
# names = ["eef_discrete", "eef_euclid", "eef_euclid_dist", "embed_discrete", "eef_embed", "embed_embed"]
names = ["eef_discrete", "embed_discrete", "eef_euclid", "embed_embed", "eef_embed"]
# names = ["embed_discrete", "embed_embed"]

def plot_data(filename, outdir, mus, vars, names):
    print(outdir)
    fig = plt.figure(figsize=(10,10))

    ax = fig.add_subplot(1, 1, 1)
    t = np.linspace(1, 1, num=len(mus[0]))
    ax.plot(t, lw=1, label='target mean', color='black', ls='--', linewidth=2)

    for mu, var, name, c in zip(mus, vars, names, colours):
        sigma = var

        if name == "eef_discrete":
            label = "EE_D"
        elif name == "eef_euclid":
            label = "EE_Eu"
        elif name == "eef_euclid_dist":
            label = "EE_EuD"
        elif name == "eef_embed":
            label = "EE_Em"
        elif name == "embed_discrete":
            label = "Em_D"
        elif name == "embed_embed":
            label = "Em_Em"

        # the n_sigma sigma upper and lower analytic population bounds
        lower_bound3 = mu - 3 * sigma
        upper_bound3 = mu + 3 * sigma

        n_sigma = 1
        lower_bound_n = mu - n_sigma * sigma
        upper_bound_n = mu + n_sigma * sigma

        ax.plot(mu, label=label, color=c, linewidth=2)
        ax.fill_between(np.arange(len(mus[0])), lower_bound_n, upper_bound_n, facecolor=c, alpha=0.2)

    if filename == "all":
        ax.set_title("All", fontsize=20, weight="bold")
    elif name == "eef_discrete":
        ax.set_title("State:Position, Reward: Discrete", fontsize=20, weight="bold")
    elif name == "eef_euclid":
        ax.set_title("State:Position, Reward: Position Cosine", fontsize=20, weight="bold")
    elif name == "eef_euclid_dist":
        ax.set_title("State:Position, Reward: Euclidean Dist", fontsize=20, weight="bold")
    elif name == "eef_embed":
        ax.set_title("State:Pose, Reward: Embed Cosine", fontsize=20, weight="bold")
    elif name == "embed_discrete":
        ax.set_title("State:Embed, Reward: Discrete", fontsize=20, weight="bold")
    elif name == "embed_embed":
        ax.set_title("State:Embed, Reward: Embed Cosine", fontsize=20, weight="bold")

    plt.legend(loc="lower right", prop={'size': 20})
    ax.set_ylabel('Enf-of-episode Reward', fontsize=20, weight="bold")
    ax.set_xlabel('# episodes', fontsize=20, weight="bold")
    ax.set_ylim(-1.1, 1.1)
    ax.set_xlim(0, len(t))

    for tick in ax.xaxis.get_major_ticks():
                tick.label.set_fontsize(14)

    for tick in ax.yaxis.get_major_ticks():
            tick.label.set_fontsize(14)

    ax.grid()

    if filename is not None:
        plt.savefig(osp.join(outdir, filename + "_stats.png"), bbox_inches="tight")
    else:
        plt.savefig(osp.join(outdir, name + "_stats.png"), bbox_inches="tight")
    plt.show()
    plt.close()

def plot(folder_name=None, trials=None, outdir=None, names=[]):

    episode_len = 15
    val_episodes = 10
    mus = []
    all = []
    diffs = []

    for index, trial in enumerate(trials):

        all.append([])

        data = np.load(osp.join(folder_name, trial, "training_stats.npz"))["stats"]
        diffs += list(np.load(osp.join(folder_name, trial, "training_stats.npz"))["diffs"][-val_episodes:])

        no_episodes = len(data) - val_episodes

        # no_episodes = 30
        train_data = data[:no_episodes]
        valid_data = data[no_episodes:]

        for i, x in enumerate(train_data):
            all[index].append(x[-1])

    mus = np.mean(all, axis=0)
    vars = np.std(all, axis=0)

    # print("Diffs", diffs)
    print("Means diffs", np.mean(diffs, axis=0))
    print("Std diffs", np.std(diffs, axis=0))

    plot_data(None, outdir, [mus], [vars], names)

    return mus, vars

if __name__ == "__main__":

    all_mus = []
    all_vars = []

    if os.listdir("images") == []:
        next_stats = 0
    else:
        next_stats = int(sorted([int(x) for x in os.listdir("images")])[-1]) + 1
        os.mkdir(osp.join("images", str(next_stats)))

    for name in names:
        no_last_trials = 10
        trials = sorted(os.listdir(osp.join("result_mock_embed", name, "right_arm_right")))[-no_last_trials:]

        prefix = osp.join("result_mock_embed", name, "right_arm_right")

        print(name)
        mus, vars = plot(folder_name=prefix, trials=trials, outdir=osp.join("images", str(next_stats)), names=[name])

        all_mus.append(mus)
        all_vars.append(vars)

    print("All")
    plot_data("all", osp.join("images", str(next_stats)), all_mus, np.zeros(np.array(all_vars).shape), names)


text:
import unittest

import numpy as np

from src.models import Frame, FrameBatch

NUM_FRAMES = 10

class FrameBatchTest(unittest.TestCase):

    def test_set_outcomes_method_should_set_the_predictions_with_udf_name(
            self):
        batch = FrameBatch(frames=[Frame(1, np.ones((1, 1)), None)], info=None)
        batch.set_outcomes('test', [None])
        self.assertEqual([None], batch.get_outcomes_for('test'))

    def test_get_outcome_from_non_existing_udf_name_returns_empty_list(self):
        batch = FrameBatch(frames=[Frame(1, np.ones((1, 1)), None)], info=None)
        self.assertEqual([], batch.get_outcomes_for('test'))

    def test_frames_as_numpy_array_should_frames_as_numpy_array(self):
        batch = FrameBatch(
            frames=[Frame(1, np.ones((1, 1)), None),
                    Frame(1, np.ones((1, 1)), None)],
            info=None)
        expected = list(np.ones((2, 1, 1)))
        actual = list(batch.frames_as_numpy_array())
        self.assertEqual(expected, actual)

    def test_return_only_frames_specified_in_the_indices(self):
        batch = FrameBatch(
            frames=[Frame(1, np.ones((1, 1)), None),
                    Frame(1, np.ones((1, 1)), None)],
            info=None)
        expected = FrameBatch(frames=[Frame(1, np.ones((1, 1)), None)],
                              info=None)
        output = batch[[0]]
        self.assertEqual(expected, output)

    def test_fetching_frames_by_index_should_also_return_outcomes(self):
        batch = FrameBatch(
            frames=[Frame(1, np.ones((1, 1)), None),
                    Frame(1, np.ones((1, 1)), None)],
            info=None,
            outcomes={'test': [[None], [None]]})
        expected = FrameBatch(frames=[Frame(1, np.ones((1, 1)), None)],
                              info=None,
                              outcomes={'test': [[None]]})
        self.assertEqual(expected, batch[[0]])

    def test_slicing_on_batched_should_return_new_batch_frame(self):
        batch = FrameBatch(
            frames=[Frame(1, np.ones((1, 1)), None),
                    Frame(1, 2 * np.ones((1, 1)), None)],
            info=None,
            outcomes={'test': [[None], [None]]})
        expected = FrameBatch(frames=[Frame(1, np.ones((1, 1)), None)],
                              info=None,
                              outcomes={'test': [[None]]})
        self.assertEqual(batch, batch[:])
        self.assertEqual(expected, batch[:-1])

    def test_slicing_should_word_for_negative_stop_value(self):
        batch = FrameBatch(
            frames=[Frame(1, np.ones((1, 1)), None),
                    Frame(1, 2 * np.ones((1, 1)), None)],
            info=None,
            outcomes={'test': [[None], [None]]})
        expected = FrameBatch(frames=[Frame(1, np.ones((1, 1)), None)],
                              info=None,
                              outcomes={'test': [[None]]})
        self.assertEqual(expected, batch[:-1])

    def test_slicing_should_work_with_skip_value(self):
        batch = FrameBatch(
            frames=[Frame(1, np.ones((1, 1)), None),
                    Frame(1, 2 * np.ones((1, 1)), None),
                    Frame(1, np.ones((1, 1)), None)], info=None,
            outcomes={'test': [[None], [None], [None]]})
        expected = FrameBatch(
            frames=[Frame(1, np.ones((1, 1)), None),
                    Frame(1, np.ones((1, 1)), None)],
            info=None,
            outcomes={'test': [[None], [None]]})
        self.assertEqual(expected, batch[::2])

max_stars_repo_name: imvinod/Eva

max_issues_repo_name: imvinod/Eva

max_forks_repo_name: asrayousuf/Eva


text:
import sys
import netCDF4

import os
from os.path import exists as _exists
from os.path import join as _join
import shutil
from subprocess import Popen, PIPE
from glob import glob
import numpy as np

if __name__ == "__main__":

    measures = ['rain', 'tmax', 'tmin', 'rad']

    for measure in measures:
        print(measure)

        ncs = glob('/geodata/au/agdc/monthly_series/{}/*.nc'.format(measure))

        print(ncs)

        agg = np.zeros((12, 681, 841))
        count = np.zeros((12, 681, 841))

        for i, _nc in enumerate(ncs):
            ds = netCDF4.Dataset(_nc)
            if i == 0:
                print(ds.variables)
                lat = ds.variables['latitude']
                lng = ds.variables['longitude']
                print(lat[0], lat[-1])
                print(lng[0], lng[-1])
            data = ds.variables['{}_month'.format(measure)]
            data = np.array(data)
            data = np.ma.masked_where(data <= -999.0, data)
            data = np.ma.masked_where(data > 1e30, data)

            print(_nc, i, len(ncs), np.min(data), np.max(data))
            agg += data
            count += 1 - data.mask

        indx = np.where(count > 0)
        agg[indx] = agg[indx] / count[indx]
        print(agg.shape, np.min(agg), np.max(agg))

        dst_dir = '/geodata/au/agdc/monthlies/{}'.format(measure)

        if _exists(dst_dir):
           shutil.rmtree(dst_dir)

        os.mkdir(dst_dir)

        for i in range(12):
            with netCDF4.Dataset(_join(dst_dir, '{:02}.nc'.format(i+1)), 'w') as dst:
                for name, dimension in ds.dimensions.items():
                    dst.createDimension(name, len(dimension) if not dimension.isunlimited() else None)

                for name, variable in ds.variables.items():

                    if name == '{}_month'.format(measure):
                        x = dst.createVariable(measure, np.float64, ('latitude', 'longitude'))
                        dst.variables[measure][:] = agg[i, :, :]

                    elif name == 'time':
                        continue

                    else:
                        x = dst.createVariable(name, variable.datatype, variable.dimensions)
                        dst.variables[name][:] = ds.variables[name][:]

            cmd = ['gdal_translate',
                   '-a_srs', '+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0',
                   '-of', 'GTIFF',
                   'NETCDF:{nc_fn}:{measure}'.format(nc_fn='{:02}.nc'.format(i+1), measure=measure),
                   '{:02}.tif'.format(i+1)]

            p = Popen(cmd, stdin=PIPE, stdout=PIPE, stderr=PIPE, cwd=dst_dir)
            p.wait()
            assert _exists(_join(dst_dir, '{:02}.tif'.format(i+1)))

max_stars_repo_name: hwbeeson/wepppy

max_issues_repo_name: hwbeeson/wepppy

max_forks_repo_name: hwbeeson/wepppy


text:
# Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import paddle
import paddle.fluid as fluid
from paddle.fluid import layers
import numpy as np
import unittest

class TestSoftmaxWithXe(unittest.TestCase):
    def setUp(self):
        self.initParameter()
        self.m, self.n = np.random.random_integers(
            low=100, high=2000, size=[2]).astype('int64')

    def initParameter(self):
        self.dtype = 'float32'
        self.soft_label = False

    def softmax_with_xe(self,
                        x,
                        y,
                        place,
                        inplace=True,
                        numeric_stable_mode=True):
        m, n = x.shape
        with fluid.program_guard(fluid.Program(), fluid.Program()):
            with fluid.scope_guard(fluid.Scope()):
                x_d = fluid.layers.data(
                    name='x',
                    shape=[m, n],
                    dtype=self.dtype,
                    append_batch_size=False)
                y_d = fluid.layers.data(
                    name='y',
                    shape=[m, 1] if not self.soft_label else [m, n],
                    dtype='int64' if not self.soft_label else self.dtype,
                    append_batch_size=False)
                z_d, s_d = fluid.layers.softmax_with_cross_entropy(
                    x_d,
                    y_d,
                    soft_label=self.soft_label,
                    return_softmax=True,
                    numeric_stable_mode=numeric_stable_mode)

                exe = fluid.Executor(place)

                exe.run(fluid.default_startup_program())

                build_strategy = fluid.BuildStrategy()
                build_strategy.enable_inplace = inplace
                prog = fluid.CompiledProgram(fluid.default_main_program(
                )).with_data_parallel(
                    build_strategy=build_strategy, places=place)

                if inplace:
                    fetch_list = [z_d.name, x_d.name]
                else:
                    fetch_list = [z_d.name, s_d.name]

                z, s = exe.run(prog,
                               feed={x_d.name: x,
                                     y_d.name: y},
                               fetch_list=fetch_list)
                return z, s

    def main_with_place(self, place):
        x = np.random.random(size=[self.m, self.n]).astype(self.dtype)
        x_range = [(-30, 30), (10, 20), (-1, 1), (2, 3), (0, 0.3), (-200, -100)]

        for a, b in x_range:
            x = ((b - a) * x + a).astype(self.dtype)
            if not self.soft_label:
                y = np.random.random_integers(
                    size=[self.m, 1], low=0, high=self.n - 1).astype('int64')
            else:
                y = np.random.random(size=[self.m, self.n]).astype(self.dtype)
                norm_y = np.broadcast_to(
                    np.reshape(
                        np.sum(y, axis=1), [-1, 1]), y.shape)
                y = y / norm_y

            z1, s1 = self.softmax_with_xe(
                x, y, place, inplace=False, numeric_stable_mode=False)
            z2, s2 = self.softmax_with_xe(
                x, y, place, inplace=True, numeric_stable_mode=False)

            self.assertTrue((z1 == z2).all())
            self.assertTrue((s1 == s2).all())

            z1, s1 = self.softmax_with_xe(
                x, y, place, inplace=False, numeric_stable_mode=True)
            z2, s2 = self.softmax_with_xe(
                x, y, place, inplace=True, numeric_stable_mode=True)
            self.assertTrue((z1 == z2).all())
            self.assertTrue((s1 == s2).all())

    def test_main(self):
        self.main_with_place(fluid.CPUPlace())
        if fluid.core.is_compiled_with_cuda():
            self.main_with_place(fluid.CUDAPlace(0))

class TestSoftmaxWithXe1(TestSoftmaxWithXe):
    def initParameter(self):
        self.dtype = 'float32'
        self.soft_label = True

class TestSoftmaxWithXe2(TestSoftmaxWithXe):
    def initParameter(self):
        self.dtype = 'float64'
        self.soft_label = False

class TestSoftmaxWithXe3(TestSoftmaxWithXe):
    def initParameter(self):
        self.dtype = 'float64'
        self.soft_label = True

if __name__ == '__main__':
    unittest.main()


text:
import numpy as np
import pandas as pd
import psycopg2.extensions
psycopg2.extensions.register_adapter(np.int64, psycopg2._psycopg.AsIs)

def convert_db_fetch_to_df(fetched, column_names=None):
    """
    This method converts the cursor.fetchall() output of SELECT query into a Pandas dataframe.
    :param fetched: the output of SELECT query
    :type fetched: list of row tuples
    :param column_names: column names to use for the dataframe
    :type column_names: list of column names
    :return: converted dataframe
    :rtype: Pandas dataframe
    """
    return pd.DataFrame(fetched, columns=column_names)

conn = psycopg2.connect(
    host='database-1.csuf8nkuxrw3.us-east-2.rds.amazonaws.com',
    port=5432,
    user='postgres',
    password='capstone',
    database='can2_etfs'
)
print('connected to postgres db')

conn.autocommit = True
cursor = conn.cursor()
sql = '''SELECT * FROM prices ORDER BY date ASC, ticker ASC LIMIT 10'''
cursor.execute(sql)
selected = cursor.fetchall()
print(convert_db_fetch_to_df(selected))
cursor.close()
print("end")


text:
import numpy as np

def highlow(data):
    x_data = list(range(data.index.size))
    y_data = data['Low']

    x = np.linspace(0, max(x_data), max(x_data) + 1)

    x_pol = np.polyfit(x_data, y_data, 60)
    y_pol = np.polyval(x_pol, x)

    l_min_max = np.diff(np.sign(np.diff(y_pol))).nonzero()[0] + 1
    l_min = (np.diff(np.sign(np.diff(y_pol))) > 0).nonzero()[0] + 1
    l_max = (np.diff(np.sign(np.diff(y_pol))) < 0).nonzero()[0] + 1
    return l_min_max, l_min, l_max


text:
import os.path
import numpy as np

from robot_properties_solo.config import Solo12Config

from solo.solo12_hwp_cpp import Solo12HWP

if __name__ == "__main__":
    solo12 = Solo12HWP()

    solo12.initialize(os.path.join(
        os.path.dirname(Solo12Config.dgm_yaml_path), 'dgm_parameters_solo12_nyu.yaml'))

    solo12.run()

    input("Press enter to start calibration.")

    solo12.calibrate_from_yaml()

max_stars_repo_name: caoliheng/solo

max_issues_repo_name: caoliheng/solo

max_forks_repo_name: caoliheng/solo


text:
from socket_pair.socket_pair import SockPairs
import time
import numpy as np

myself = 'Wrapper'
others = ['NU']

arr = np.random.rand(120, 1280, 1040, 3)

sockObj = SockPairs(myself, others, is_main=True)

# start
sockObj.sync_all()

sockObj.send(arr, to=others[0])


text:
import numpy as np
from numpy import random as rnd

import torch
import torch.nn as nn

def model_params(offdiag_val = 0.2,diag_val = 0.98,batch_size = 50,nonlinearity = 'relu',readout_nonlinearity = 'linear',n_in = 50,n_out = 1,n_hid =  500):
    """
        Return model kwargs to initialize recurrent model
    """
    model_kwargs = {
                'input_size': n_in,
                'output_size': n_out,
                'batch_size': batch_size,
                'core_kwargs':
                    {
                        'num_layers': 1,
                        'hidden_size': n_hid,
                        'nonlinearity': nonlinearity
                    },
                'param_init_fn': 'diag_init_',
                'param_init_kwargs':
                    {
                        'offdiag_val': offdiag_val / np.sqrt(n_hid),
                        'diag_val': diag_val
                    },
                'readout_nonlinearity': readout_nonlinearity,
                'reservoir_training' : False,
                'reservoir_training_params' :
                    {
                        'weight_lock_step' : 2
                    }
                }
    return model_kwargs

# parameter initialization functions
def diag_init(tensor: torch.Tensor, offdiag_val : np.float64, diag_val: np.float64) -> torch.Tensor:
    """
        Initialize parameters, separating onn and off diag
        In place operation
    """
    shape = tensor.shape
    if len(shape) != 2 or shape[0] != shape[1]:
        raise ValueError('LeInit initializer can only be used for 2D square matrices.')

    off_diag_part = offdiag_val * np.random.randn(shape[0], shape[1])
    tensor = torch.tensor(np.eye(shape[0]) * diag_val + off_diag_part - np.diag(np.diag(off_diag_part)))
    return tensor

def param_init_(recurrent_model):
    param_init_kwargs = recurrent_model.model_kwargs['param_init_kwargs']
#     with torch.no_grad():
    if recurrent_model.model_kwargs['param_init_fn'] == "diag_init_":
        # Init input-hidden according to Xavier Normal, g = 0.95
        nn.init.xavier_normal_(recurrent_model.core.weight_ih_l0,gain = 0.95).double()
        # Init hidden-hidden according to diag-off-diag decomposition method
        recurrent_model.core.weight_hh_l0 = torch.nn.Parameter(diag_init(recurrent_model.core.weight_hh_l0,param_init_kwargs['offdiag_val'],param_init_kwargs['diag_val'])).double()
        # Init hidden-output according to Xavier Normal, g = 0.95
        nn.init.xavier_normal_(recurrent_model.readout_linear.weight,gain = 0.95).double()
        nn.init.xavier_normal_(recurrent_model.readout_sigmoid.weight,gain = 0.95).double()

reason: import numpy,from numpy