text:
import cv2
import numpy as np
import matplotlib.pyplot as plt
import pprint
import math
import pickle
import scipy.stats as stats

testNum = 29

filename = 'Data1/t'+str(testNum)+'.mp4'

numberErrorsBk = 0
centerBk = 0; radiusBk = 0
centerPBk = 0; radiusPBk = 0;
numberErrorsFt = 0
centerPFt = 0; radiusPFt = 0;

frameNum = 0

Lbk = 240 #length of back flag, in pixels
centerArrayA = []; centerArrayB = [] ## Front Flag (red marker)
centerArrayX = []; centerArrayY = [] ## Back Flag (green marker)
thetaFlagFt = []; thetaFlagbk = [];

def nothing(x):
    pass

#Green
hlg = 40
slg = 45
vlg = 80
hhg = 100
shg = 255
vhg = 255

#Red
hlr = 150
slr = 45
vlr = 80
hhr = 255
shr = 255
vhr = 255

cv2.namedWindow('greenHSVMask')
# create trackbars for color change
cv2.createTrackbar('H_low_green','greenHSVMask',hlg,180,nothing)
cv2.createTrackbar('S_low_green','greenHSVMask',slg,255,nothing)
cv2.createTrackbar('V_low_green','greenHSVMask',vlg,255,nothing)
cv2.createTrackbar('H_high_green','greenHSVMask',hhg,180,nothing)
cv2.createTrackbar('S_high_green','greenHSVMask',shg,255,nothing)
cv2.createTrackbar('V_high_green','greenHSVMask',vhg,255,nothing)

cv2.namedWindow('redHSVMask')
# create trackbars for color change
cv2.createTrackbar('H_low_red','redHSVMask',hlr,180,nothing)
cv2.createTrackbar('S_low_red','redHSVMask',slr,255,nothing)
cv2.createTrackbar('V_low_red','redHSVMask',vlr,255,nothing)
cv2.createTrackbar('H_high_red','redHSVMask',hhr,180,nothing)
cv2.createTrackbar('S_high_red','redHSVMask',shr,255,nothing)
cv2.createTrackbar('V_high_red','redHSVMask',vhr,255,nothing)

def posToTheta(i):
    angle = math.acos( (Lbk - (max(centerArrayX) - i) )/Lbk )
    print angle
    return angle

def reject_outliers(data, m=2):
    """ From:
    http://stackoverflow.com/questions/11686720/is-there-a-numpy-builtin-to-reject-outliers-from-a-list
    """
    return data[abs(data - np.mean(data)) < m * np.std(data)]

def colorImgPreProcess(image):
    """
    Prepare images to be analyzed in binary form by appling generic filtering.
    This makes them easier to work with and the resulting image less noisy.

    INPUT: image for pre-processing. Should be in color, though b&w should work.
    OUTPUT: returns a RGB image which has been filtered and looks nicer.
    """
    #do processing on the image while it's still in color
    image = cv2.medianBlur(image, 3)  #kernal size must be odd
    #image = cv2.bilateralFilter(image, 9, 75, 75) #TODO: uncomment when it won't cause C++ errors with ROS
    #self.closeImages() #uncomment if showing output image
    return image

def make_image_mask_green(img, a=40, b=10, c=10, d=80, e=200, f=255):
    # B G R
    #lower_blue = np.array([10,70,10],  dtype=np.uint8)# np.array([60,30,55])
    #upper_blue = np.array([150,250,60],  dtype=np.uint8)

    hsv_img = cv2.cvtColor(img,cv2.COLOR_BGR2HSV)

    # Threshold the HSV image to get only blue colors
    img = colorImgPreProcess(hsv_img)

    lower_bound = np.array([a, b, c],  dtype=np.uint8)
    upper_bound = np.array([d, e, f],  dtype=np.uint8)
    print 'upper_bound: ', upper_bound

    try:
        thresh = cv2.inRange(img, lower_bound, upper_bound)
        #cv2.imshow('green mask', thresh)
        return thresh
    except:
        print "Fatal inRange error in make_image_mask_green!"
        return ''

def make_image_mask_red(img, a=40, b=10, c=10, d=80, e=200, f=255):
    hsv_img = cv2.cvtColor(img,cv2.COLOR_BGR2HSV)
    img = colorImgPreProcess(hsv_img)

    lower_bound = np.array([a, b, c],  dtype=np.uint8)
    upper_bound = np.array([d, e, f],  dtype=np.uint8)
    print 'red lower bound: ', lower_bound
    print 'red upper bound: ', upper_bound

    try:
        thresh = cv2.inRange(img, lower_bound, upper_bound)
        #cv2.imshow('Red Mask', thresh)
        return thresh
    except:
        print "Fatal inRange error in make_image_mask_red!"
        return ''

def make_image_mask_red_RGB(img):
    # B G R
    lower_blue = np.array([23,0,80],  dtype=np.uint8)# np.array([60,30,55])
    upper_blue = np.array([100,80,255],  dtype=np.uint8)

    # Threshold the HSV image to get only blue colors
    try:
        thresh = cv2.inRange(img, lower_blue, upper_blue)
        #cv2.imshow('Red RGB mask', thresh)
        return thresh
    except:
        print "Fatal inRange error in make_image_mask_red_RGB!"
        return ''

def get_red_circle(frame, hlr, slr, vlr, hhr, shr, vhr):
    global numberErrorsFt
    global centerFt
    global radiusFt

    side_mask = make_image_mask_red(frame, hlr, slr, vlr, hhr, shr, vhr)
    if type(side_mask) != type(''):
        contours, hierarchy = cv2.findContours(side_mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

        if len(contours):
            cnt = contours[0]
            (x,y),radiusFt = cv2.minEnclosingCircle(cnt)
            centerFt = (int(x),int(y))
            radiusFt = int(radiusFt)
            side_mask = cv2.cvtColor(side_mask, cv2.COLOR_GRAY2RGB)

            cv2.circle(side_mask, centerFt, radiusFt, np.array([0,0,255]), 10)
            cv2.imshow('Red mask', side_mask)

        else:
            numberErrorsFt = numberErrorsFt + 1

def get_green_circle(frame, hlg, slg, vlg, hhg, shg, vhg):
    global numberErrorsBk
    global centerBk
    global radiusBk

    side_mask = make_image_mask_green(frame, hlg, slg, vlg, hhg, shg, vhg) #
    if type(side_mask) != type(''):
        contours, hierarchy = cv2.findContours(side_mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

        if len(contours):
            cnt = contours[0]
            (x,y),radiusBk = cv2.minEnclosingCircle(cnt)
            centerBk = (int(x),int(y))
            radiusBk = int(radiusBk)
            side_mask = cv2.cvtColor(side_mask, cv2.COLOR_GRAY2RGB)

            cv2.circle(side_mask, centerBk, radiusBk, np.array([0,255,0]), 10)
            cv2.imshow('green mask', side_mask)
            #print 'center: ', center, 'radius: ', radius, ' found from side camera'

        else:
            print 'no contours found for green'
            numberErrorsBk = numberErrorsBk + 1
    else:
        print 'green string'

def findFtFlagMarker(frame, hlr, slr, vlr, hhr, shr, vhr):
    get_red_circle(frame, hlr, slr, vlr, hhr, shr, vhr)
    centerPFt, radiusPFt = centerFt, radiusFt
    if centerFt:
        centerArrayA.append(centerFt[0])
        centerArrayB.append(centerFt[1])
    else:
        centerArrayA.append(centerPFt[0])
        centerArrayB.append(centerPFt[1])

def findBkFlagMarker(frame, hlg, slg, vlg, hhg, shg, vhg):
    get_green_circle(frame, hlg, slg, vlg, hhg, shg, vhg)
    centerPBk, radiusPBk = centerBk, radiusBk

    if centerBk:
        centerArrayX.append(centerBk[0])
        centerArrayY.append(centerBk[1])
    else:
        'In P block. centerP is: ', centerPBk
        centerArrayX.append(centerPBk[0])
        centerArrayY.append(centerPBk[1])

def printSummaryStats():
    '''Display sumary stats: '''
    print 'SUMMARY: '
    print 'Back flag: ', numberErrorsBk, ' errors out of ', frameNum
    print '   ', 100*float(numberErrorsBk)/frameNum, ' % frames without a circle found'
    print 'Front flag: ', numberErrorsFt, ' errors out of ', frameNum
    print '   ', 100*float(numberErrorsFt)/frameNum, ' % frames without a circle found'

def plotBothFlagPositions():
    ##Plot data!:
    plt.plot(centerArrayX, centerArrayY, marker='.', markersize=10, alpha=.7, linestyle='None', label = 'Back Flag Position', color='#7A9E35', linewidth=6)
    plt.plot(centerArrayA, centerArrayB, marker='.', markersize=10, alpha=.7, linestyle='None', label = 'Front Flag Position', color='#AA3C39', linewidth=6)
    plt.xlabel('X Position (pixels)', fontsize = 18)
    plt.ylabel('Y Position (pixels)', fontsize = 18)
    plt.title('Position of Fluttering Sail Through Time', fontsize = 20)
    plt.axis('equal')   #HALIE I ADDED THESE SO THAT IT WOULD BE EASIER TO SEE CIRCLES, DELETE IF YOU WANT
    plt.legend()
    plt.show()

def plotXXPositions():
    plt.plot(centerArrayB, centerArrayY, marker='.', markersize=10, alpha=.7, linestyle='None', color='#7A9E35', linewidth=6)
    plt.xlabel('Front Flag Y Position (pixels)', fontsize = 18)
    plt.ylabel('Back Flag Y Position (pixels)', fontsize = 18)
    plt.title('Position of Fluttering Sail Through Time', fontsize = 20)
    #plt.legend()
    plt.axis('equal') #HALIE I ADDED THESE SO THAT IT WOULD BE EASIER TO SEE CIRCLES, DELETE IF YOU WANT
    plt.show()

##Open Video file:
cap = cv2.VideoCapture(filename)

while(cap.isOpened()):
    ret, frame = cap.read()
    height, width, channels = frame.shape
    frame = frame[450:550, 150:width-300] # Crop from x, y, w, h -> 100, 200, 300, 400
    cv2.imshow('Cropped image', frame)

    #findFtFlagMarker(frame)
    hlg = cv2.getTrackbarPos('H_low_green','greenHSVMask')
    slg = cv2.getTrackbarPos('S_low_green','greenHSVMask')
    vlg = cv2.getTrackbarPos('V_low_green','greenHSVMask')
    hhg = cv2.getTrackbarPos('H_high_green','greenHSVMask')
    shg = cv2.getTrackbarPos('S_high_green','greenHSVMask')
    vhg = cv2.getTrackbarPos('V_high_green','greenHSVMask')

    hlr = cv2.getTrackbarPos('H_low_red','redHSVMask')
    slr = cv2.getTrackbarPos('S_low_red','redHSVMask')
    vlr = cv2.getTrackbarPos('V_low_red','redHSVMask')
    hhr = cv2.getTrackbarPos('H_high_red','redHSVMask')
    shr = cv2.getTrackbarPos('S_high_red','redHSVMask')
    vhr = cv2.getTrackbarPos('V_high_red','redHSVMask')

    ## Testing only, make image masks
    #make_image_mask_green(frame, hlg, slg, vlg, hhg, shg, vhg)
    #make_image_mask_red(frame, hlr, slr, vlr, hhr, shr, vhr)

    ## Testing only, only find circles
    #get_red_circle(frame, hlr, slr, vlr, hhr, shr, vhr)
    #get_green_circle(frame, hlg, slg, vlg, hhg, shg, vhg)

    ## Production Use!!!
    findFtFlagMarker(frame, hlr, slr, vlr, hhr, shr, vhr)
    findBkFlagMarker(frame, hlg, slg, vlg, hhg, shg, vhg)

    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

    frameNum =frameNum + 1
    if frameNum >= cap.get(cv2.cv.CV_CAP_PROP_FRAME_COUNT):
        ## Now that we're done, let's save the data!
        pickle.dump(centerArrayX, open( "Xt"+str(testNum)+".p", "wb" ) )
        pickle.dump(centerArrayY, open( "Yt"+str(testNum)+".p", "wb" ) )
        pickle.dump(centerArrayA, open( "At"+str(testNum)+".p", "wb" ) )
        pickle.dump(centerArrayB, open( "Bt"+str(testNum)+".p", "wb" ) )

##        ##Filter out extreme values:
##        stdx = stats.tstd(centerArrayX)
##        meanx = stats.tmean(centerArrayX)
##        for x in centerArrayX:
##            if

#        centerArrayX = np.array(centerArrayX)
#        plt.plot(centerArrayX, label='2')
#        centerArrayX = reject_outliers(centerArrayX)
#        plt.plot(centerArrayX, label='3')

        printSummaryStats()
        plotBothFlagPositions()
        plotXXPositions()

        break

cap.release()
#cv2.destroyAllWindows()

max_stars_repo_name: hmurraydavis/Flutter-Sail

max_issues_repo_name: hmurraydavis/Flutter-Sail

max_forks_repo_name: hmurraydavis/Flutter-Sail

reason: import numpy,import scipy


text:
"""Tile filtering script"""
import imghdr
import os
import cv2
import numpy as np

# pylint: disable=all
# suppress cv2 has no imread member
from PIL.Image import Image

def delete_img(img, original):
    """delete all tile identical to the black/empty tile in the current directory"""
    if os.path.exists(img):

        # check that the file is a valid png
        if imghdr.what(img) != "png":
            return "file is not a valid png"

        # open the image
        original = cv2.imread(original)
        dup = cv2.imread(img)

        # compare image with an entirely black image
        if not np.bitwise_xor(original, dup).any():
            os.remove(img)
            return "successfully removed"
        else:
            return "image was not black"
    else:
        return "file does not exist"

def cleanup(directory, original):
    """Runs delete_img on all files in the current directory and corrects file naming errors"""
    # directory = os.fsencode("../../..")
    # print(directory)
    for file in os.listdir(directory):
        # fil = str(file).replace("'", '')
        # fil = fil.replace('b', '')
        delete_img(directory + "/" + file, original)

ext: py

reason: import numpy


text:
#!/usr/bin/env python

'''
    Read orbits data.
'''

import os
import numpy as np

class ReadOrbits(object):

    def __init__(self, I_file,
                 rawdata_path='/dgalex2/qinyj/data/3042_orbsave/'):

        rawdata_fname = rawdata_path + ('orbits%03u' % I_file) + '.res'

        print('\nLoading ', rawdata_fname, '...')
        print('  Huge file, may take a few minutes...')

        raw_file = open(rawdata_fname, 'rb')

        raw_orbits = np.fromfile(raw_file, dtype = np.float32)[4:]
        raw_orbits = np.transpose(np.reshape(raw_orbits, (5001, -1)))

        time_pts   = raw_orbits[1]
        delta_t    = (time_pts[-1] - time_pts[0]) / (time_pts.shape[0] - 1)
        raw_orbits = raw_orbits[2:-1]

        raw_file.close()

        self.raw_orbits = raw_orbits
        self.time_pts = time_pts
        self.delta_t = delta_t

        print('  Imported successfully.')
        print('delta_t is', delta_t)

    def get_orbit(self, I_orb, velocity_method='full', packed=True):

        # kernels for 2nd order numerical differentiation
        ctr_diff = -np.array([1. / 12., -2. / 3., 0., 2. / 3., -1. / 12.])
        fwd_diff = -np.array([-3. / 2., 2., -1. / 2.])
        bwd_diff = -np.array([1. / 2., -2., 3. / 2.])
        # Note the negative sign here!

        # read orbits from the big array
        pos = self.raw_orbits[3 * I_orb: 3 * I_orb + 3]

        if velocity_method == 'full':
            # return the entire orbit.

            if self.time_pts.size < 5:
                raise RuntimeError('Orbit is too short' \
                        + ' for numerical differentiation.')

            vel = np.zeros(shape = (3, pos.shape[1]))

            for i in (0, 1, 2): # velocity components,

                # first two points: interpolate with forward method.
                vel[i][  : 2] = np.convolve(pos[i][  :4], fwd_diff,
                        mode='valid') / self.delta_t

                vel[i][ 2:-2] = np.convolve(pos[i]      , ctr_diff,
                        mode='valid') / self.delta_t

                # last two points: using backward method.
                vel[i][-2:  ] = np.convolve(pos[i][-4: ], bwd_diff,
                        mode='valid') / self.delta_t

            if packed:
                return np.vstack((pos, vel, self.time_pts)).T
            else:
                return pos.T, vel.T, self.time_pts

        elif velocity_method == 'central':
            # using central diff. only, ignores first and last two points.

            vel = np.zeros(shape=(3, pos.shape[1] - 4))

            for i in (0, 1, 2):
                vel[i] = np.convolve(pos[i], ctr_diff,
                        mode = 'valid') / self.delta_t

            if packed:
                return np.vstack((pos[:, 2:-2], vel, self.time_pts[2:-2])).T
            else:
                return pos[:,2:-2].T, vel.T, self.time_pts[2:-2]

        else:
            raise RuntimeError('Invalid parameter: velocity_method')

max_stars_repo_name: shiaki/valses-nobles-et-sentimentales

max_issues_repo_name: shiaki/valses-nobles-et-sentimentales

max_forks_repo_name: shiaki/valses-nobles-et-sentimentales


text:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Mar 29 11:55:27 2021

@author: jgollub
"""
#general
import numpy as np
from pathlib import Path
import matplotlib.pyplot as plt
from datetime import date
from cycler import cycler
import matplotlib.gridspec as gridspec
from contextlib import ExitStack

#s-parameter networks
import skrf as rf
from skrf.vi.vna import PNA
from skrf.calibration import OnePort

#arduino control
import serial

rf.stylely

# %matplotlib inline
## created calibration class using

plt.rcParams['axes.prop_cycle'] = cycler(color=['b', 'r', 'g', 'y'])
# plt.rcParams['axes.prop_cycle'] = cycler(color=[.1, , 'r', 'y'])

#%% keysight power supply

#%% Measured response from TRL Cal

measurement_root_folder = Path('/Users/Zaber/Documents/data/scikit_measurements/')
cal_folder_name = 'TRL2021-04-23'

meas_folder = measurement_root_folder / cal_folder_name

THRU_S11 = np.load(meas_folder / 'THRU_S11.ntwk', allow_pickle = True)
THRU_S22 = np.load(meas_folder / 'THRU_S22.ntwk', allow_pickle = True)
THRU_S21 = np.load(meas_folder / 'THRU_S21.ntwk', allow_pickle = True)
THRU_S12 = np.load(meas_folder / 'THRU_S12.ntwk', allow_pickle = True)

REFLECT_S11 = np.load(meas_folder / 'REFLECT_S11.ntwk', allow_pickle = True)
REFLECT_S22 = np.load(meas_folder / 'REFLECT_S22.ntwk', allow_pickle = True)
REFLECT_S21 = np.load(meas_folder / 'REFLECT_S21.ntwk', allow_pickle = True)
REFLECT_S12 = np.load(meas_folder / 'REFLECT_S12.ntwk', allow_pickle = True)

LINE_S11 = np.load(meas_folder / 'LINE_S11.ntwk', allow_pickle = True)
LINE_S22 = np.load(meas_folder / 'LINE_S22.ntwk', allow_pickle = True)
LINE_S21 = np.load(meas_folder / 'LINE_S21.ntwk', allow_pickle = True)
LINE_S12 = np.load(meas_folder / 'LINE_S12.ntwk', allow_pickle = True)

SWITCH_TERMS = np.load(meas_folder / 'SWITCH_TERMS.ntwk', allow_pickle = True)

THRU = rf.two_port_reflect(THRU_S11, THRU_S22)
n_freq = len(THRU.f)
pad_zeros = np.zeros((n_freq, 2, 2))

THRU.s[:,0,1] = THRU_S12.s[:,0,0]
THRU.s[:,1,0] = THRU_S21.s[:,0,0]
THRU.z0  = (50+0j)*np.ones((n_freq, 4))

REFLECT = rf.two_port_reflect(REFLECT_S11, REFLECT_S22)
REFLECT.s[:,0,1] = REFLECT_S12.s[:,0,0]
REFLECT.s[:,1,0] = REFLECT_S21.s[:,0,0]
REFLECT.z0 = (50+0j)*np.ones((n_freq, 4))

LINE = rf.two_port_reflect(LINE_S11, LINE_S22)
LINE.s[:,0,1] = LINE_S12.s[:,0,0]
LINE.s[:,1,0] = LINE_S21.s[:,0,0]
LINE.z0 = (50+0j)*np.ones((n_freq, 4))

my_ideals = [None, -1, None]

my_measured = [\
               THRU,
               REFLECT,
               LINE,
        ]

#%%

## create a Calibration instance
trl = rf.TRL(\
        measured = my_measured,
        ideals = my_ideals,
        estimate_line = True,
        switch_terms = SWITCH_TERMS,
        )

# run calibration algorithm
trl.run()

#%% setup measurements

f_start = THRU.f[0]
f_stop = THRU.f[-1]
f_npoints = len(THRU.f)

# f_start = 8E9
# f_stop = 12E9
# f_npoints = int(101)

vna = PNA(address ='TCPIP0::10.236.73.132::inst0::INSTR')
vna.reset()
vna.scpi.set_trigger_manual
# vna.scpi.set_display_on(wnum=1,state=True)

meas_channel = 2

meas_S11 = 'S11'
vna.create_meas(meas_S11,'S11', channel = meas_channel)
meas_S22 = 'S22'
vna.create_meas(meas_S22,'S22', channel = meas_channel)
meas_S21 = 'S21'
vna.create_meas(meas_S21,'S21', channel = meas_channel)
meas_S12 = 'S12'
vna.create_meas(meas_S12,'S12', channel = meas_channel)

vna.set_frequency_sweep(f_start,
                        f_stop,
                        f_npoints,
                        f_unit = 'Hz',
                        channel = meas_channel,
                        )

#%% set voltages with arduino and take measurements

set_data_folder = Path('/Users/Zaber/Documents/data/scikit_measurements/channel4_251pts/')
set_data_folder.mkdir(exist_ok = True)

fig, (ax1,ax2)= plt.subplots(2,1)
port = 'COM4'
Vref = 5 #volts

voltage_settings = list(range(256)) #0-255 states
voltage_values =Vref*np.array(voltage_settings)/255
voltage_dict = dict(zip(voltage_settings, voltage_values))

# set_voltage_indx = 100

with ExitStack() as cm:
    arduino = cm.enter_context(serial.Serial(port=port,
                                             baudrate = 115200,
                                             timeout =.1,
                                             xonxoff =False,
                                             rtscts = False,
                                             dsrdtr= True,
                                             ))

    #set a
    n_dac=12

    for v_indx in voltage_settings:
        ts_temp=n_dac*[v_indx]
        address =15
        msg = '15'
        for jj in range(n_dac):
         #   msg=msg + ' ' + str(address+16*ts_temp[jj])
            msg=msg + ' ' + str(4096*address + 16*ts_temp[jj])
        msg=msg +' '

        byt = arduino.in_waiting;
        if (byt > 0):
            print('take out trash')
            arduino.readline()
        arduino.write(msg.encode('utf-8'))

        print(f'Voltage should be set to {voltage_dict[v_indx]}')

        vna.sweep
        # S11
        s11 = vna.get_measurement(mname = meas_S11)
        # print(f'{meas_S11} measured')
        filename = meas_folder / ('THRU_' + meas_S11 + '.ntwk')
        # rf.write(str(filename), measurement)
        # measurement.frequency.units = 'ghz'
        # measurement.plot_s_db(ax = ax1, label ='THRU S11')

        #S22
        s22 = vna.get_measurement(mname = meas_S22)
        # print(f'{meas_S22} measured')
        filename = meas_folder / ('THRU_' + meas_S22 + '.ntwk')
        # rf.write(str(filename), measurement)
        # measurement.frequency.units = 'ghz'
        # measurement.plot_s_db(ax = ax2, label ='THRU S22')

        #S21
        s21 = vna.get_measurement(mname = meas_S21)
        # print(f'{meas_S21} measured')
        filename = meas_folder / ('THRU_' + meas_S21 + '.ntwk')
        # rf.write(str(filename), measurement)
        # measurement.frequency.units = 'ghz'
        # measurement.plot_s_db(ax = ax1, label ='THRU S21')

        #S12
        s12 = vna.get_measurement(mname = meas_S12)
        # print(f'{meas_S12} measured')

        filename = meas_folder / ('THRU_' + meas_S12 + '.ntwk')
        # rf.write(str(filename), measurement)
        # measurement.frequency.units = 'ghz'
        # measurement.plot_s_db(ax = ax2, label ='THRU S12')

        dut_raw = rf.two_port_reflect(s11, s22)

        dut_raw.s[:,0,1] = s12.s[:,0,0]
        dut_raw.s[:,1,0] = s21.s[:,0,0]
        dut_raw.z0 = (50+0j)*np.ones((n_freq, 4))

        # dut_raw.plot_s_db(ax = ax)
        # dut_corrected.write_touchstone()

# apply it to a dut

        dut_raw.name = 'voltage_indx_' + str(v_indx)
        dut_corrected = trl.apply_cal(dut_raw)
        dut_corrected.name =  'trl_corrected_' + dut_raw.name

        # plot results
        # fig, (ax1,ax2)= plt.subplots(2,1)
        # dut_corrected.frequency.unit = 'ghz'

        # dut_corrected.plot_s_deg(ax = ax1)
        # dut_corrected.plot_s_db(ax = ax2)

        # save results

        dut_corrected.write_touchstone(dut_corrected.name, set_data_folder)

 #%%
# dict_o_ntwks = rf.read_all(set_data_folder, contains = 's2p')

# a0 = 14E-3
# b0 = 0.762E-3

# waveguide = rf.RectangularWaveguide(frequency = dict_o_ntwks[0].frequency, a = a0, b = b0, ep_r = 3.45, nports =1)
# left= waveguide.line(5E-3, 'm')
# right = left

# alpha = np.zeros((len(dut_corrected.f), 256), dtype = complex)
# for ii in range(256):
#     temp =dict_o_ntwks['trl_corrected_voltage_indx_' + str(ii)]
#     temp.z0 = waveguide.z0
#     element = left.inv ** temp ** right.inv
#     alpha_temp = -a0*b0*(1+element.s[:,0,0]-element.s[:,1,0])/waveguide.k0
#     alpha[:,ii] = alpha_temp/np.amax(np.abs(alpha_temp))
# # set up subplot grid

#%%
dict_o_ntwks = rf.read_all(set_data_folder, contains = '.s2p')

a0 = 14E-3
b0 = 0.762E-3

first_ntwk = dict_o_ntwks['trl_corrected_voltage_indx_0']
waveguide = rf.RectangularWaveguide(frequency = first_ntwk.frequency,
                                    a = a0,
                                    b = b0,
                                    ep_r = 3.45,
                                    nports =2,
                                    )
left= waveguide.line(5E-3, 'm')
right = left
alpha = np.zeros((len(first_ntwk.f), 256), dtype = complex)
for ii in range(256):
    meas_ntwk =dict_o_ntwks['trl_corrected_voltage_indx_' + str(ii)]

    s11_prime = meas_ntwk.s[:,0,0]/(np.exp(-1j*waveguide.k0*1E-2))
    s21_prime = meas_ntwk.s[:,1,0]/(np.exp(-1j*waveguide.k0*1E-2))
    alpha_temp = -a0*b0*1j*(1+s11_prime-s21_prime)/waveguide.k0
    # alpha_temp = -1j*(1+s11_prime-s21_prime)/waveguide.k0

    alpha[:,ii] = alpha_temp
alpha_max = (np.amax(np.abs(alpha), axis =(1)))
alpha_max = alpha_max[:,np.newaxis]

alpha = alpha/alpha_max

#%%
f_indx = 125
for f_indx in list(range(0,251,2)):
    print(f'freq = {first_ntwk.f[f_indx]}')
    alpha_temp  = alpha[f_indx,:]

    fig = plt.figure(f_indx)

    gridspec.GridSpec(2,2)
    plt.subplot2grid((2,2), (0,0))
    plt.plot(range(256), np.abs(alpha_temp))
    plt.title('Polarizability Mag (dB)', fontsize = 10)
    plt.tight_layout()

    plt.subplot2grid((2,2), (1,0))
    plt.plot(range(256), np.degrees(np.angle(alpha_temp)))
    plt.title('Polarizability Phase (deg)',fontsize = 10)
    plt.ylim(-180,180)
    plt.tight_layout()

    plt.subplot2grid((2,2), (0,1), colspan = 1, rowspan = 2)
    plt.scatter(np.real(alpha_temp), np.imag(alpha_temp))
    # plt.axis('equal')
    plt.xlim(-1,1)
    plt.ylim(-1,1)
    plt.title(f'Polarizability Real vs Imaginary, f= {first_ntwk.f[f_indx]/1E9: .2f} GHz', fontsize = 10)
    plt.tight_layout()
    filename = set_data_folder / f'extraceted_alpha__vs_voltage_{f_indx}.png'
    plt.savefig(filename)

#%%
# waveguide = rf.RectangularWaveguide(frequency = dut_corrected.frequency, a = 14E-3, b = 0.762E-3, ep_r = 3.45, nports =2)
# left= waveguide.line(0E-3, 'm', z0 = 50, embed=True)
# right = left
# point_alpha = left.inv ** dut_corrected ** right.inv

# alpha = -(1+point_alpha.s[:,0,0]-point_alpha.s[:,1,0])/waveguide.k0
# alpha =alpha/np.amax(np.abs(alpha))

# fig = plt.figure(1)
# # set up subplot grid
# gridspec.GridSpec(2,2)
# plt.subplot2grid((2,2), (0,0))
# plt.plot(dut_corrected.f, np.abs(alpha))
# plt.title('Polarizability Mag (dB)', fontsize = 10)
# plt.tight_layout()

# plt.subplot2grid((2,2), (1,0))
# plt.plot(dut_corrected.f, np.angle(alpha))
# plt.title('Polarizability Phase (deg)',fontsize = 10)
# plt.tight_layout()

# plt.subplot2grid((2,2), (0,1), colspan = 1, rowspan = 2)
# plt.scatter(np.real(alpha), np.imag(alpha))
# # plt.axis('equal')
# plt.xlim(-1,1)
# plt.ylim(-1,1)
# plt.title('Polarizability Real vs Imaginary ', fontsize = 10)
# plt.tight_layout()

max_stars_repo_name: metacept/scikit-rf

max_issues_repo_name: metacept/scikit-rf

max_forks_repo_name: metacept/scikit-rf


text:
"""
This script is provided to demonstrate the usage of SMAC's Black-box optimization with Random Forest model, where we have assumed the availability of related packages.
More details about SMAC can be found at https://github.com/automl/SMAC3
"""
import numpy as np

from ConfigSpace import ConfigurationSpace
from ConfigSpace.hyperparameters import UniformIntegerHyperparameter, UniformFloatHyperparameter, CategoricalHyperparameter
from smac.facade.smac_hpo_facade import SMAC4HPO
from smac.scenario.scenario import Scenario

def eval_fl_algo(x):
    from federatedscope.core.cmd_args import parse_args
    from federatedscope.core.auxiliaries.data_builder import get_data
    from federatedscope.core.auxiliaries.utils import setup_seed, update_logger
    from federatedscope.core.auxiliaries.worker_builder import get_client_cls, get_server_cls
    from federatedscope.core.configs.config import global_cfg
    from federatedscope.core.fed_runner import FedRunner

    init_cfg = global_cfg.clone()
    init_cfg.merge_from_file(
        "federatedscope/example_configs/single_process.yaml")
    # specify the configuration of interest
    init_cfg.merge_from_list([
        "optimizer.lr",
        float(x['lr']), "optimizer.weight_decay",
        float(x['wd']), "model.dropout",
        float(x["dropout"])
    ])

    update_logger(init_cfg, True)
    setup_seed(init_cfg.seed)

    # federated dataset might change the number of clients
    # thus, we allow the creation procedure of dataset to modify the global cfg object
    data, modified_cfg = get_data(config=init_cfg.clone())
    init_cfg.merge_from_other_cfg(modified_cfg)

    init_cfg.freeze()

    runner = FedRunner(data=data,
                       server_class=get_server_cls(init_cfg),
                       client_class=get_client_cls(init_cfg),
                       config=init_cfg.clone())
    results = runner.run()

    # so that we could modify cfg in the next trial
    init_cfg.defrost()

    return results['client_summarized_weighted_avg']['test_avg_loss']

def main():
    # Define your hyperparameters
    configspace = ConfigurationSpace()
    #configspace.add_hyperparameter(UniformIntegerHyperparameter("depth", 2, 100))
    configspace.add_hyperparameter(
        UniformFloatHyperparameter("lr", lower=1e-4, upper=1.0, log=True))
    configspace.add_hyperparameter(
        UniformFloatHyperparameter("dropout", lower=.0, upper=.5))
    configspace.add_hyperparameter(
        CategoricalHyperparameter("wd", choices=[0.0, 0.5]))

    # Provide meta data for the optimization
    scenario = Scenario({
        "run_obj": "quality",  # Optimize quality (alternatively runtime)
        "runcount-limit": 8,  # Max number of function evaluations (the more the better)
        "cs": configspace,
        'output_dir': "smac_rf",
    })

    # a summary of SMAC's facades: https://automl.github.io/SMAC3/master/pages/details/facades.html?highlight=random%20forest#facades
    smac = SMAC4HPO(scenario=scenario, tae_runner=eval_fl_algo)
    best_found_config = smac.optimize()
    print(best_found_config)
    #run_history = smac.get_runhistory()

if __name__ == "__main__":
    main()

max_stars_repo_name: alibaba/FederatedScope

max_issues_repo_name: alibaba/FederatedScope

max_forks_repo_name: alibaba/FederatedScope


text:
"""
Optuna example that optimizes multi-layer perceptrons using MXNet.

In this example, we optimize the validation accuracy of hand-written digit recognition using
MXNet and MNIST. We optimize the neural network architecture as well as the optimizer
configuration. As it is too time consuming to use the whole MNIST dataset, we here use a small
subset of it.

We have the following two ways to execute this example:

(1) Execute this code directly.
    $ python mxnet_simple.py

(2) Execute through CLI.
    $ STUDY_NAME=`optuna create-study --direction maximize --storage sqlite:///example.db`
    $ optuna study optimize mxnet_simple.py objective --n-trials=100 --study $STUDY_NAME \
      --storage sqlite:///example.db

"""

import logging

import mxnet as mx
import numpy as np

import optuna

N_TRAIN_EXAMPLES = 3000
N_TEST_EXAMPLES = 1000
BATCHSIZE = 128
EPOCH = 10

# Set log level for MXNet.
logger = logging.getLogger()
logger.setLevel(logging.INFO)

def create_model(trial):
    # We optimize the number of layers and hidden units in each layer.
    n_layers = trial.suggest_int("n_layers", 1, 3)

    data = mx.symbol.Variable("data")
    data = mx.sym.flatten(data=data)
    for i in range(n_layers):
        num_hidden = int(trial.suggest_loguniform("n_units_l{}".format(i), 4, 128))
        data = mx.symbol.FullyConnected(data=data, num_hidden=num_hidden)
        data = mx.symbol.Activation(data=data, act_type="relu")

    data = mx.symbol.FullyConnected(data=data, num_hidden=10)
    mlp = mx.symbol.SoftmaxOutput(data=data, name="softmax")

    return mlp

def create_optimizer(trial):
    # We optimize over the type of optimizer to use (Adam or SGD with momentum).
    # We also optimize over the learning rate and weight decay of the selected optimizer.
    weight_decay = trial.suggest_loguniform("weight_decay", 1e-10, 1e-3)
    optimizer_name = trial.suggest_categorical("optimizer", ["Adam", "MomentumSGD"])

    if optimizer_name == "Adam":
        adam_lr = trial.suggest_loguniform("adam_lr", 1e-5, 1e-1)
        optimizer = mx.optimizer.Adam(learning_rate=adam_lr, wd=weight_decay)
    else:
        momentum_sgd_lr = trial.suggest_loguniform("momentum_sgd_lr", 1e-5, 1e-1)
        optimizer = mx.optimizer.SGD(momentum=momentum_sgd_lr, wd=weight_decay)

    return optimizer

def objective(trial):
    # Generate trial model and trial optimizer.
    mlp = create_model(trial)
    optimizer = create_optimizer(trial)

    # Load the test and train MNIST dataset.
    mnist = mx.test_utils.get_mnist()
    rng = np.random.RandomState(0)
    permute_train = rng.permutation(len(mnist["train_data"]))
    train = mx.io.NDArrayIter(
        data=mnist["train_data"][permute_train][:N_TRAIN_EXAMPLES],
        label=mnist["train_label"][permute_train][:N_TRAIN_EXAMPLES],
        batch_size=BATCHSIZE,
        shuffle=True,
    )
    permute_test = rng.permutation(len(mnist["test_data"]))
    val = mx.io.NDArrayIter(
        data=mnist["test_data"][permute_test][:N_TEST_EXAMPLES],
        label=mnist["test_label"][permute_test][:N_TEST_EXAMPLES],
        batch_size=BATCHSIZE,
    )

    # Create our MXNet trainable model and fit it on MNIST data.
    model = mx.mod.Module(symbol=mlp)
    model.fit(
        train_data=train,
        eval_data=val,
        optimizer=optimizer,
        optimizer_params={"rescale_grad": 1.0 / BATCHSIZE},
        num_epoch=EPOCH,
    )

    # Compute the accuracy on the entire test set.
    test = mx.io.NDArrayIter(
        data=mnist["test_data"], label=mnist["test_label"], batch_size=BATCHSIZE
    )
    accuracy = model.score(eval_data=test, eval_metric="acc")[0]

    return accuracy[1]

if __name__ == "__main__":
    study = optuna.create_study(direction="maximize")
    study.optimize(objective, n_trials=100)

    print("Number of finished trials: ", len(study.trials))

    print("Best trial:")
    trial = study.best_trial

    print("  Value: ", trial.value)

    print("  Params: ")
    for key, value in trial.params.items():
        print("    {}: {}".format(key, value))


text:
import  torch
import torch.nn as nn
from torch.autograd import Variable
import functools
from torch.optim import lr_scheduler
import torch.nn.functional as F
import torchvision.models as py_models
import numpy
import copy
import global_options

B0minusB1Mode = global_options.B0minusB1Mode
pos_gen_mode = global_options.pos_gen_mode
vel_secret_mode = global_options.vel_secret_mode
z_delete_mode= global_options.z_delete_mode
dir_mode = global_options.dir_mode
pred2_mode = global_options.pred2_mode
####################################################################
#------------------------- next value prediction --------------------------
####################################################################
class E_pred_relation(nn.Module):
  def __init__(self, norm_layer=None, nl_layer=None, h = 64, w = 64, gpu= None):
    super(E_pred_relation, self).__init__()
    norm_layer_fc = LayerNorm_FC
    self.gpu = gpu
    init_fm = 64
    self.fc_vel = nn.Sequential(*[nn.Linear(init_fm, 128), norm_layer_fc(128), nn.ReLU(), nn.Linear(128, 128), norm_layer_fc(128), nn.ReLU(), nn.Linear(128, 128), norm_layer_fc(128), nn.ReLU()], nn.Linear(128, 32))

  def forward(self, vel_pred, vel_comp, pos_pred, pos_comp):
    # pos [b,2], vel [b, 32]
    batch_size = pos_pred.size()[0]

    pos_pred_whole = torch.zeros([batch_size, 16, 2]).cuda(self.gpu)
    cur_pos_pred = pos_pred.clone()
    vel_pred_view = vel_pred.view(-1,16,2)
    for i in range(0,16):
        cur_pos_pred = cur_pos_pred + vel_pred_view[:,i] * 0.01
        pos_pred_whole [:,i] = cur_pos_pred
    pos_pred_whole = pos_pred_whole.view(-1,32)

    pos_comp_whole = torch.zeros([batch_size, 16, 2]).cuda(self.gpu)
    cur_pos_comp = pos_comp.clone()
    vel_comp_view = vel_comp.view(-1,16,2)
    for i in range(0,16):
        pos_comp_whole [:,15-i] = cur_pos_comp
        cur_pos_comp = cur_pos_comp - vel_comp_view[:,15-i] * 0.01
    pos_comp_whole = pos_comp_whole.view(-1,32)

    cv_concat = torch.cat([pos_pred_whole, pos_comp_whole],1)

    output_vel = self.fc_vel(cv_concat)

    return output_vel

class E_pred(nn.Module):
  def __init__(self, input_dim_b, output_nc=8, norm_layer=None, nl_layer=None, h = 64, w = 64, gpu= None):
    super(E_pred, self).__init__()
    self.nz = output_nc
    ndf = 64
    n_blocks=4
    max_ndf = 4
    norm_layer_fc = LayerNorm_FC
    self.gpu = gpu

    if pred2_mode:
        init_fm = 32
    else:
        init_fm = 32 + 32 + 2
    self.fc_vel = nn.Sequential(*[nn.Linear(init_fm, 128), norm_layer_fc(128), nn.ReLU(), nn.Linear(128, 128), norm_layer_fc(128), nn.ReLU(), nn.Linear(128, 128), norm_layer_fc(128), nn.ReLU()], nn.Linear(128, 32))
    #self.fc_vel = nn.Sequential(*[nn.Linear(init_fm, 64), nn.ReLU(), nn.Linear(64, 128), nn.ReLU(), nn.Linear(128, 128), nn.ReLU(), nn.Linear(128, 32)]) # 181
    #self.fc_vel = nn.Sequential(*[nn.Linear(init_fm, 64), nn.ReLU(), nn.Linear(64, 128), nn.ReLU(), nn.Linear(128, 128), nn.ReLU(), nn.Linear(128, 128), nn.ReLU(), nn.Linear(128, 128), nn.ReLU(), nn.Linear(128, 32)]) # 183

  def forward(self, content= None, pos=None, vel=None, z=None, forward_pos = False, forward_future = False):
    if(forward_pos):
        return self.forward_pos(content)
    if(forward_future):
        return self.forward_future(pos,vel,z)
    # content [b,2], vel [b, 32], state [b, 32]

    if pred2_mode:
        batch_size = pos.size()[0]
        pos_pred_whole = torch.zeros([batch_size, 16, 2]).cuda(self.gpu)
        # this is current pos version
        cur_pos = pos.clone()
        # this is next pos version
        vel = vel.view(-1,16,2)
        for i in range(0,16):
            pos_pred_whole [:,15-i] = cur_pos
            cur_pos = cur_pos - vel[:,15-i] * 0.01
        pos_pred_whole = pos_pred_whole.view(-1,32)
        cv_concat = pos_pred_whole

    else:
        cv_concat = torch.cat([pos, vel, z],1) # [b, 2 + 32 + 32]
    output_vel = self.fc_vel(cv_concat)

    output_pos = output_vel
    output_z = output_vel

    return content, output_vel, output_pos, output_z

  def forward_pos(self, content):

    return content

  def forward_future(self, pos, vel, z):
    if pred2_mode:
        batch_size = pos.size()[0]
        pos_pred_whole = torch.zeros([batch_size, 16, 2]).cuda(self.gpu)
        # this is current pos version
        cur_pos = pos
        # this is next pos version
        vel = vel.view(-1,16,2)
        for i in range(0,16):
            pos_pred_whole [:,15-i] = cur_pos
            cur_pos = cur_pos - vel[:,15-i] * 0.01
        pos_pred_whole = pos_pred_whole.view(-1,32)
        cv_concat = pos_pred_whole
    else:
        cv_concat = torch.cat([pos, vel, z],1) # [b, 2 + 32 + 32]
        #cv_concat = torch.cat([pos, vel],1) # [b, 2 + 32 + 32]

    output_vel = self.fc_vel(cv_concat)

    output_pos = output_vel
    output_z = output_vel

    return output_vel, output_pos, output_z

####################################################################
#---------------------------- Encoders -----------------------------
####################################################################
class E_content(nn.Module):
  def __init__(self, input_dim_a, input_dim_b, norm_layer=None, nl_layer=None):
    super(E_content, self).__init__()

    ndf = 32 #32
    n_blocks=  4
    stride = 2
    max_ndf = 4
    output_nc = 2

    # content encoding
    conv_layers_B = [nn.ReflectionPad2d(1)]

    if(B0minusB1Mode):
        conv_layers_B += [nn.Conv2d(input_dim_b, ndf, kernel_size=3, stride=stride, padding=0, bias=True)]
    else:
        conv_layers_B += [nn.Conv2d(input_dim_b*2, ndf, kernel_size=3, stride=stride, padding=0, bias=True)]

    for n in range(1, n_blocks):
      input_ndf = ndf * min(max_ndf, n)  # 2**(n-1)
      output_ndf = ndf * min(max_ndf, n+1)  # 2**n
      conv_layers_B += [BasicBlock(input_ndf, output_ndf, norm_layer, nl_layer)]
    conv_layers_B += [nl_layer(), nn.AdaptiveAvgPool2d(1)] # AvgPool2d(13)

    self.fc_B = nn.Sequential(*[nn.Linear(output_ndf, output_ndf//2), nn.ReLU(), nn.Linear(output_ndf//2, output_ndf//4), nn.ReLU(), nn.Linear(output_ndf//4, output_nc)])
    #self.fc_B = nn.Sequential(*[nn.Linear(output_ndf, output_ndf), nn.ReLU(), nn.Linear(output_ndf, output_ndf), nn.ReLU(), nn.Linear(output_ndf, output_nc)])
    self.conv_B = nn.Sequential(*conv_layers_B)

  def forward(self, xprev, xb, forward_b=None):
    if(B0minusB1Mode):
        x_cat = xb-xprev
    else:
        x_cat = torch.cat([xprev,  xb], 1)

    # pos
    x_conv_B = self.conv_B(x_cat)
    conv_flat_B = x_conv_B.view(xb.size(0), -1)
    output_B = self.fc_B(conv_flat_B)

    return output_B

class E_attr_concat(nn.Module):
  def __init__(self, input_dim_b, output_nc=8, norm_layer=None, nl_layer=None):
    super(E_attr_concat, self).__init__()

    ndf = 32 #32
    n_blocks= 4
    stride = 2
    max_ndf = 4
    output_dir = output_nc
    # dir encoding
    dir_conv_layers = [nn.ReflectionPad2d(1)]

    if(B0minusB1Mode):
        dir_conv_layers += [nn.Conv2d(input_dim_b, ndf, kernel_size=3, stride=stride, padding=0, bias=True)]
    else:
        dir_conv_layers += [nn.Conv2d(input_dim_b*2, ndf, kernel_size=3, stride=stride, padding=0, bias=True)]

    for n in range(1, n_blocks):
      input_ndf = ndf * min(max_ndf, n)  # 2**(n-1)
      output_ndf = ndf * min(max_ndf, n+1)  # 2**n
      dir_conv_layers += [BasicBlock(input_ndf, output_ndf, norm_layer, nl_layer)]
    dir_conv_layers += [nl_layer(), nn.AdaptiveAvgPool2d(1)]
    self.fc_dir = nn.Sequential(*[nn.Linear(output_ndf, output_ndf), nn.ReLU(),  nn.Linear(output_ndf, output_ndf), nn.ReLU(), nn.Linear(output_ndf, output_nc), nn.Sigmoid()]) #[0,1]
    self.conv_dir = nn.Sequential(*dir_conv_layers)

    # content encoding
    conv_layers_B = [nn.ReflectionPad2d(1)]

    if(B0minusB1Mode):
        conv_layers_B += [nn.Conv2d(input_dim_b, ndf, kernel_size=3, stride=stride, padding=0, bias=True)]
    else:
        conv_layers_B += [nn.Conv2d(input_dim_b*2, ndf, kernel_size=3, stride=stride, padding=0, bias=True)]

    for n in range(1, n_blocks):
      input_ndf = ndf * min(max_ndf, n)  # 2**(n-1)
      output_ndf = ndf * min(max_ndf, n+1)  # 2**n
      conv_layers_B += [BasicBlock(input_ndf, output_ndf, norm_layer, nl_layer)]
    conv_layers_B += [nl_layer(), nn.AdaptiveAvgPool2d(1)] # AvgPool2d(13)

    #self.fc_B = nn.Sequential(*[nn.Linear(output_ndf, output_ndf), nn.ReLU(),  nn.Linear(output_ndf, output_ndf), nn.ReLU(), nn.Linear(output_ndf, output_nc), nn.ReLU()])
    self.fc_B = nn.Sequential(*[nn.Linear(output_ndf, output_ndf), nn.ReLU(),  nn.Linear(output_ndf, output_ndf), nn.ReLU(), nn.Linear(output_ndf, output_nc), nn.ReLU()])
    self.conv_B = nn.Sequential(*conv_layers_B)

  def forward(self, xprev, xb):

    if(B0minusB1Mode):
        x_cat = xb-xprev
    else:
        x_cat = torch.cat([xprev,  xb], 1)
    # dir
    if dir_mode:
        x_dir = self.conv_dir(x_cat)
        x_dir_flat = x_dir.view(x_cat.size(0), -1)
        output_dir = self.fc_dir(x_dir_flat)#.mul(2).sub(1)
    #output_dir = output_dir.view([x_cat.size(0), -1, 1, 1])
    #output_dir = output_dir.repeat(1,1,xb.size(2), xb.size(3))

    # vel
    x_conv_B = self.conv_B(x_cat)
    conv_flat_B = x_conv_B.view(xb.size(0), -1)
    output_B = self.fc_B(conv_flat_B)
    #outputVar_B = self.fcVar_B(conv_flat_B)

    # shutter speed
    #return  output_B, outputVar_B, output_dir # output_dir: [-1,1]
    if dir_mode:
        return  output_B, output_dir # output_dir: [-1,1]
    return output_B

####################################################################
#--------------------------- Generators ----------------------------
####################################################################
class G_concat(nn.Module):
  def __init__(self, output_dim_a, nz, h = 128, w = 128, gpu = None):
    super(G_concat, self).__init__()
    self.nz = nz
    self.h = h//2
    self.w = w//2
    tch = 256
    norm_layer_fc = LayerNorm_FC
    dec_share = []
    dec_fc = []
    self.gpu = gpu

    tch = (self.h//2) * (self.w//2)
    dec_fc += [nn.Linear(32, 32), norm_layer_fc(32), nn.ReLU(), nn.Linear(32, 128), norm_layer_fc(128), nn.ReLU(), nn.Linear(128, tch), norm_layer_fc(tch), nn.ReLU()]
    dec_share += [ReLUINSConv2d(1, 256 , kernel_size=1, stride=1, padding=0)]
    #dec_share += [INSResBlock(tch, tch)]
    tch = 256+1
    decB1 = []
    for i in range(0, 3):
      decB1 += [INSResBlock(tch, tch)]
    tch = tch + self.nz
    decB2 = ReLUINSConvTranspose2d(tch, tch//2, kernel_size=3, stride=2, padding=1, output_padding=1)
    tch = tch//2
    tch = tch + self.nz
    decB3 = ReLUINSConvTranspose2d(tch, tch//2, kernel_size=3, stride=2, padding=1, output_padding=1)
    tch = tch//2
    tch = tch + self.nz
    decB4 = [nn.ConvTranspose2d(tch, output_dim_a, kernel_size=1, stride=1, padding=0)]+[nn.Tanh()]

    self.dec_fc = nn.Sequential(*dec_fc)
    self.dec_share = nn.Sequential(*dec_share)
    self.decB1 = nn.Sequential(*decB1)
    self.decB2 = nn.Sequential(*[decB2])
    self.decB3 = nn.Sequential(*[decB3])
    self.decB4 = nn.Sequential(*decB4)

  def forward(self, x, z, shutter_speed):
    batch_size = x.size(0)
    pos_pred_whole = torch.zeros([batch_size, 16, 2]).cuda(self.gpu)
    # this is current pos version
    cur_pos = x
    # this is next pos version
    vel = z.view(-1,16,2)
    if pred2_mode:
        for i in range(0,16):
            pos_pred_whole [:,15-i] = cur_pos
            cur_pos = cur_pos - vel[:,15-i] * 0.01
    else:
        # 이거 뭔가 잘못된 것 같음
        for i in range(0,16):
            cur_pos = cur_pos + vel[:,i] * 0.01
            pos_pred_whole [:,i] = cur_pos
    pos_pred_whole = pos_pred_whole.view(-1,32)
    z = pos_pred_whole
    out0 = self.dec_fc(pos_pred_whole)
    out0 = out0.view(out0.size(0), 1, self.h//2, self.w//2)
    out0 = self.dec_share(out0)
    shutter_speed = shutter_speed.view(shutter_speed.size(0), shutter_speed.size(1), 1, 1).expand(shutter_speed.size(0), shutter_speed.size(1), self.h//2, self.w//2)
    x_and_z = torch.cat([out0, shutter_speed], 1)

    out1 = self.decB1(x_and_z)
    z_img2 = z.view(z.size(0), z.size(1), 1, 1).expand(z.size(0), z.size(1), out1.size(2), out1.size(3))
    x_and_z2 = torch.cat([out1, z_img2], 1)
    out2 = self.decB2(x_and_z2)
    z_img3 = z.view(z.size(0), z.size(1), 1, 1).expand(z.size(0), z.size(1), out2.size(2), out2.size(3))
    x_and_z3 = torch.cat([out2, z_img3], 1)
    out3 = self.decB3(x_and_z3)
    z_img4 = z.view(z.size(0), z.size(1), 1, 1).expand(z.size(0), z.size(1), out3.size(2), out3.size(3))
    x_and_z4 = torch.cat([out3, z_img4], 1)
    out4 = self.decB4(x_and_z4)

    return out4

class G_sharp(nn.Module):
  def __init__(self, output_dim_a, nz, h = 128, w = 128):
    super(G_sharp, self).__init__()
    self.nz = nz
    self.h = h//2
    self.w = w//2
    tch = 256
    norm_layer_fc = LayerNorm_FC
    dec_share = []
    dec_fc = []
    if(pos_gen_mode):
        tch = (self.h//2) * (self.w//2)
        dec_fc += [nn.Linear(2, 32), norm_layer_fc(32), nn.ReLU(), nn.Linear(32, 128), norm_layer_fc(128), nn.ReLU(), nn.Linear(128, tch), norm_layer_fc(tch), nn.ReLU()]
        dec_share += [ReLUINSConv2d(1, 256 , kernel_size=1, stride=1, padding=0)]
        #dec_share += [INSResBlock(tch, tch)]
        tch = 256
        decB1 = []
        for i in range(0, 3):
          decB1 += [INSResBlock(tch, tch)]
        tch = tch
        decB2 = ReLUINSConvTranspose2d(tch, tch//2, kernel_size=3, stride=2, padding=1, output_padding=1)
        tch = tch//2
        tch = tch
        decB3 = ReLUINSConvTranspose2d(tch, tch//2, kernel_size=3, stride=2, padding=1, output_padding=1)
        tch = tch//2
        tch = tch
        decB4 = [nn.ConvTranspose2d(tch, output_dim_a, kernel_size=1, stride=1, padding=0)]+[nn.Tanh()]
    else:
        dec_share += [INSResBlock(tch, tch)]
        tch = tch+self.nz+1
        decB1 = []
        for i in range(0, 3):
          decB1 += [INSResBlock(tch, tch)]
        tch = tch + self.nz
        decB2 = ReLUINSConvTranspose2d(tch, tch//2, kernel_size=3, stride=2, padding=1, output_padding=1)
        tch = tch//2
        tch = tch + self.nz
        decB3 = ReLUINSConvTranspose2d(tch, tch//2, kernel_size=3, stride=2, padding=1, output_padding=1)
        tch = tch//2
        tch = tch + self.nz
        decB4 = [nn.ConvTranspose2d(tch, output_dim_a, kernel_size=1, stride=1, padding=0)]+[nn.Tanh()]

    self.dec_fc = nn.Sequential(*dec_fc)
    self.dec_share = nn.Sequential(*dec_share)
    self.decB1 = nn.Sequential(*decB1)
    self.decB2 = nn.Sequential(*[decB2])
    self.decB3 = nn.Sequential(*[decB3])
    self.decB4 = nn.Sequential(*decB4)

  def forward(self, x):
    if(pos_gen_mode):
        #out0 = x.view(x.size(0), x.size(1), 1, 1).expand(x.size(0), x.size(1), self.h//2, self.w//2) # [b, 2]
        out0 = self.dec_fc(x)
        #out0 = out0.view(out0.size(0), out0.size(1), 1, 1).expand(out0.size(0), out0.size(1), self.h//2, self.w//2)
        out0 = out0.view(out0.size(0), 1, self.h//2, self.w//2)
        out0 = self.dec_share(out0)
        x_and_z = out0
        #x_and_z = self.dec_share(x_and_z)
    else:
        out0 = self.dec_share(x)
        z_img = z.view(z.size(0), z.size(1), 1, 1).expand(z.size(0), z.size(1), self.h//2, self.w//2)
        shutter_speed = shutter_speed.view(shutter_speed.size(0), shutter_speed.size(1), 1, 1).expand(shutter_speed.size(0), shutter_speed.size(1), self.h//2, self.w//2)
        x_and_z = torch.cat([out0,  z_img, shutter_speed], 1)

    out1 = self.decB1(x_and_z)
    out2 = self.decB2(out1)
    out3 = self.decB3(out2)
    out4 = self.decB4(out3)
    return out4

class G_manual():
  def __init__(self, sharp_gen):
    #super(G_manual, self).__init__()
    self.sharp_gen = sharp_gen

  def g(self, arr, gamma = 2.2, inverse=False):
    if(inverse):
        return arr.pow(gamma)
    else:
        return arr.pow(1/gamma)

  def forward(self, x, z, shutter_speed):
    batch_size = x.size(0)
    vel = z.view(-1,16,2)
    ret = torch.zeros([batch_size,self.ratio, 3,self.h,self.w]).cuda(self.gpu)
    # x: pos
    pos_x = (x[:,0] + 1) * self.w/2
    pos_y = (x[:,1] + 1) * self.h/2

    for i in range(self.ratio):
        ret[:, self.ratio-1-i] = self.draw(pos_x,pos_y, batch_size)
        pos_x = pos_x - vel[:,self.ratio-1-i,0] * 0.01
        pos_y = pos_y - vel[:,self.ratio-1-i,1] * 0.01
    return self.make_blur(ret, batch_size, shutter_speed)

class G_manual2():
  def __init__(self, nz, gpu, h = 128, w = 128):
    #super(G_manual, self).__init__()
    self.nz = nz
    self.ratio = nz//2
    self.h = h
    self.w = w
    self.scale = 1
    self.scaled_h = self.h * self.scale
    self.scaled_w = self.w * self.scale
    #self.size = torch.tensor(10.)
    #self.size = torch.nn.Parameter(self.size)
    self.size = 10
    self.gpu = gpu

  def draw(self, pos_x, pos_y, batch_size):
    ret = torch.zeros([batch_size,3,self.scaled_h, self.scaled_w]).cuda(self.gpu)
    pos_x = (pos_x * self.scale)#.int()
    pos_y = (pos_y * self.scale)#.int()
    pos_x_int = pos_x.int()
    pos_y_int = pos_y.int()

    radius = (self.size * self.scale)#.int()
    for b in range(batch_size):
        for x in range(pos_x_int[b]-radius, pos_x_int[b]+radius):
            for y in range(pos_y_int[b]-radius, pos_y_int[b]+radius):
                k = (x-pos_x[b]).pow(2) + (y-pos_y[b]).pow(2) - radius**2
                if ( k>0 and x >= 0 and y >=0 and x < self.scaled_w and y <self.scaled_h):
                    ret[b,:,y,x] = k
    # resize 필요
    return ret

  def g(self, arr, gamma = 2.2, inverse=False):
    if(inverse):
        return arr.pow(gamma)
    else:
        return arr.pow(1/gamma)

  def make_blur(self, ret, batch_size, shutter_speed):
    ret = self.g(ret, inverse=True)
    ret_final = torch.zeros([batch_size, 3,self.h,self.w]).cuda(self.gpu)

    for b in range(batch_size):
        for i in range(self.ratio):
            if((i/(self.ratio-1)) >= (0.5 - shutter_speed[b]*0.5)) and ((i/(self.ratio-1)) <= (0.5 + shutter_speed[b]*0.5)):
                ret_final[b] += ret[b,i]
    return self.g(ret_final)

  def forward(self, x, z, shutter_speed):
    batch_size = x.size(0)
    vel = z.view(-1,16,2)
    ret = torch.zeros([batch_size,self.ratio, 3,self.h,self.w]).cuda(self.gpu)
    # x: pos
    pos_x = (x[:,0] + 1) * self.w/2
    pos_y = (x[:,1] + 1) * self.h/2

    for i in range(self.ratio):
        ret[:, self.ratio-1-i] = self.draw(pos_x,pos_y, batch_size)
        pos_x = pos_x - vel[:,self.ratio-1-i,0] * 0.01
        pos_y = pos_y - vel[:,self.ratio-1-i,1] * 0.01
    return self.make_blur(ret, batch_size, shutter_speed)

####################################################################
#--------------------------- losses ----------------------------
####################################################################
class PerceptualLoss():
    def __init__(self, loss, gpu="cuda", p_layer=14):
        super(PerceptualLoss, self).__init__()
        self.criterion = loss
        self.gpu = gpu

        cnn = py_models.vgg19(pretrained=True).features
        cnn = cnn.cuda(self.gpu)
        model = nn.Sequential()
        model = model.cuda(self.gpu)
        for i,layer in enumerate(list(cnn)):
            model.add_module(str(i),layer)
            if i == p_layer:
                break
        self.contentFunc = model

    def getloss(self, fakeIm, realIm):
        if isinstance(fakeIm, numpy.ndarray):
            fakeIm = torch.from_numpy(fakeIm).permute(2, 0, 1).unsqueeze(0).float().cuda(self.gpu)
            realIm = torch.from_numpy(realIm).permute(2, 0, 1).unsqueeze(0).float().cuda(self.gpu)
        f_fake = self.contentFunc.forward(fakeIm)
        f_real = self.contentFunc.forward(realIm)
        f_real_no_grad = f_real.detach()
        loss = self.criterion(f_fake, f_real_no_grad)
        return loss
class PerceptualLoss16():
    def __init__(self, loss, gpu="cuda", p_layer=14):
        super(PerceptualLoss16, self).__init__()
        self.criterion = loss
        self.gpu = gpu
#         conv_3_3_layer = 14
        checkpoint = torch.load('/vggface_path/VGGFace16.pth')
        vgg16 = py_models.vgg16(num_classes=2622)
        vgg16.load_state_dict(checkpoint['state_dict'])
        cnn = vgg16.features
        cnn = cnn.cuda(gpu)
#         cnn = cnn.to(gpu)
        model = nn.Sequential()
        model = model.cuda(gpu)
        for i,layer in enumerate(list(cnn)):
#             print(layer)
            model.add_module(str(i),layer)
            if i == p_layer:
                break
        self.contentFunc = model
        del vgg16, cnn, checkpoint

    def getloss(self, fakeIm, realIm):
        if isinstance(fakeIm, numpy.ndarray):
            fakeIm = torch.from_numpy(fakeIm).permute(2, 0, 1).unsqueeze(0).float().cuda(self.gpu)
            realIm = torch.from_numpy(realIm).permute(2, 0, 1).unsqueeze(0).float().cuda(self.gpu)

        f_fake = self.contentFunc.forward(fakeIm)
        f_real = self.contentFunc.forward(realIm)
        f_real_no_grad = f_real.detach()
        loss = self.criterion(f_fake, f_real_no_grad)
        return loss

class GradientLoss():
    def __init__(self, loss, n_scale=3):
        super(GradientLoss, self).__init__()
        self.downsample = nn.AvgPool2d(3, stride=2, padding=1, count_include_pad=False)
        self.criterion = loss
        self.n_scale = n_scale

    def grad_xy(self, img):
        gradient_x = img[:, :, :, :-1] - img[:, :, :, 1:]
        gradient_y = img[:, :, :-1, :] - img[:, :, 1:, :]
        return gradient_x, gradient_y

    def getloss(self, fakeIm, realIm):
        loss = 0
        for i in range(self.n_scale):
            fakeIm = self.downsample(fakeIm)
            realIm = self.downsample(realIm)
            grad_fx, grad_fy = self.grad_xy(fakeIm)
            grad_rx, grad_ry = self.grad_xy(realIm)
            loss += pow(4,i) * self.criterion(grad_fx, grad_rx) + self.criterion(grad_fy, grad_ry)
        return loss

class l1GradientLoss():
    def __init__(self, loss, n_scale=3):
        super(l1GradientLoss, self).__init__()
        self.downsample = nn.AvgPool2d(3, stride=2, padding=1, count_include_pad=False)
        self.criterion = loss
        self.n_scale = n_scale

    def grad_xy(self, img):
        gradient_x = img[:, :, :, :-1] - img[:, :, :, 1:]
        gradient_y = img[:, :, :-1, :] - img[:, :, 1:, :]
        return gradient_x, gradient_y

    def getloss(self, fakeIm):
        loss = 0
        for i in range(self.n_scale):
            fakeIm = self.downsample(fakeIm)
            grad_fx, grad_fy = self.grad_xy(fakeIm)
            loss += self.criterion(grad_fx, torch.zeros_like(grad_fx)) + self.criterion(grad_fy, torch.zeros_like(grad_fy))

        return loss

####################################################################
#------------------------- Basic Functions -------------------------
####################################################################
def get_scheduler(optimizer, opts, cur_ep=-1):
  if opts.lr_policy == 'lambda':
    def lambda_rule(ep):
      lr_l = 1.0 - max(0, ep - opts.n_ep_decay) / float(opts.n_ep - opts.n_ep_decay + 1)
      return lr_l
    scheduler = lr_scheduler.LambdaLR(optimizer, lr_lambda=lambda_rule, last_epoch=cur_ep)
  elif opts.lr_policy == 'step':
    scheduler = lr_scheduler.StepLR(optimizer, step_size=opts.n_ep_decay, gamma=0.1, last_epoch=cur_ep)
  else:
    return NotImplementedError('no such learn rate policy')
  return scheduler

def meanpoolConv(inplanes, outplanes):
  sequence = []
  sequence += [nn.AvgPool2d(kernel_size=2, stride=2)]
  sequence += [nn.Conv2d(inplanes, outplanes, kernel_size=1, stride=1, padding=0, bias=True)]
  return nn.Sequential(*sequence)

def convMeanpool(inplanes, outplanes):
  sequence = []
  sequence += conv3x3(inplanes, outplanes)
  sequence += [nn.AvgPool2d(kernel_size=2, stride=2)]
  return nn.Sequential(*sequence)

def get_norm_layer(layer_type='instance'):
  if layer_type == 'batch':
    norm_layer = functools.partial(nn.BatchNorm2d, affine=True)
  elif layer_type == 'instance':
    norm_layer = functools.partial(nn.InstanceNorm2d, affine=False)
  elif layer_type == 'none':
    norm_layer = None
  else:
    raise NotImplementedError('normalization layer [%s] is not found' % layer_type)
  return norm_layer

def get_non_linearity(layer_type='relu'):
  if layer_type == 'relu':
    nl_layer = functools.partial(nn.ReLU, inplace=True)
  elif layer_type == 'lrelu':
    nl_layer = functools.partial(nn.LeakyReLU, negative_slope=0.2, inplace=False)
  elif layer_type == 'elu':
    nl_layer = functools.partial(nn.ELU, inplace=True)
  else:
    raise NotImplementedError('nonlinearity activitation [%s] is not found' % layer_type)
  return nl_layer
def conv3x3(in_planes, out_planes):
  return [nn.ReflectionPad2d(1), nn.Conv2d(in_planes, out_planes, kernel_size=3, stride=1, padding=0, bias=True)]

def gaussian_weights_init(m):
  classname = m.__class__.__name__
  if classname.find('Conv') != -1 and classname.find('Conv') == 0:
    m.weight.data.normal_(0.0, 0.02)

####################################################################
#-------------------------- Basic Blocks --------------------------
####################################################################

## The code of LayerNorm is modified from MUNIT (https://github.com/NVlabs/MUNIT)
class LayerNorm(nn.Module):
  def __init__(self, n_out, eps=1e-5, affine=True):
    super(LayerNorm, self).__init__()
    self.n_out = n_out
    self.affine = affine
    if self.affine:
      self.weight = nn.Parameter(torch.ones(n_out, 1, 1))
      self.bias = nn.Parameter(torch.zeros(n_out, 1, 1))
    return
  def forward(self, x):
    normalized_shape = x.size()[1:]
    if self.affine:
      return F.layer_norm(x, normalized_shape, self.weight.expand(normalized_shape), self.bias.expand(normalized_shape))
    else:
      return F.layer_norm(x, normalized_shape)

class LayerNorm_FC(nn.Module):
  def __init__(self, n_out, eps=1e-5, affine=True):
    super(LayerNorm_FC, self).__init__()
    self.n_out = n_out
    self.affine = affine
    if self.affine:
      self.weight = nn.Parameter(torch.ones(n_out))
      self.bias = nn.Parameter(torch.zeros(n_out))
    return
  def forward(self, x):
    normalized_shape = x.size()[1:]
    if self.affine:
      return F.layer_norm(x, normalized_shape, self.weight, self.bias)
    else:
      return F.layer_norm(x, normalized_shape)

class BasicBlock(nn.Module):
  def __init__(self, inplanes, outplanes, norm_layer=None, nl_layer=None):
    super(BasicBlock, self).__init__()
    layers = []
    if norm_layer is not None:
      layers += [norm_layer(inplanes)]
    layers += [nl_layer()]
    layers += conv3x3(inplanes, inplanes)
    if norm_layer is not None:
      layers += [norm_layer(inplanes)]
    layers += [nl_layer()]
    layers += [convMeanpool(inplanes, outplanes)]
    self.conv = nn.Sequential(*layers)
    self.shortcut = meanpoolConv(inplanes, outplanes)
  def forward(self, x):
    out = self.conv(x) + self.shortcut(x)
    return out

class LeakyReLUConv2d(nn.Module):
  def __init__(self, n_in, n_out, kernel_size, stride, padding=0, norm='None', sn=False):
    super(LeakyReLUConv2d, self).__init__()
    model = []
    model += [nn.ReflectionPad2d(padding)]
    if sn:
      model += [spectral_norm(nn.Conv2d(n_in, n_out, kernel_size=kernel_size, stride=stride, padding=0, bias=True))]
    else:
      model += [nn.Conv2d(n_in, n_out, kernel_size=kernel_size, stride=stride, padding=0, bias=True)]
    if 'norm' == 'Instance':
      model += [nn.InstanceNorm2d(n_out, affine=False)]
    model += [nn.LeakyReLU(inplace=True)]
    self.model = nn.Sequential(*model)
    self.model.apply(gaussian_weights_init)
    #elif == 'Group'
  def forward(self, x):
    return self.model(x)

class ReLUINSConv2d(nn.Module):
  def __init__(self, n_in, n_out, kernel_size, stride, padding=0):
    super(ReLUINSConv2d, self).__init__()
    model = []
    model += [nn.ReflectionPad2d(padding)]
    model += [nn.Conv2d(n_in, n_out, kernel_size=kernel_size, stride=stride, padding=0, bias=True)]
    model += [nn.InstanceNorm2d(n_out, affine=False)]
    model += [nn.ReLU(inplace=True)]
    self.model = nn.Sequential(*model)
    self.model.apply(gaussian_weights_init)
  def forward(self, x):
    return self.model(x)

class INSResBlock(nn.Module):
  def conv3x3(self, inplanes, out_planes, stride=1):
    return [nn.ReflectionPad2d(1), nn.Conv2d(inplanes, out_planes, kernel_size=3, stride=stride)]
  def __init__(self, inplanes, planes, stride=1, dropout=0.0):
    super(INSResBlock, self).__init__()
    model = []
    model += self.conv3x3(inplanes, planes, stride)
    model += [nn.InstanceNorm2d(planes)]
    model += [nn.ReLU(inplace=True)]
    model += self.conv3x3(planes, planes)
    model += [nn.InstanceNorm2d(planes)]
    if dropout > 0:
      model += [nn.Dropout(p=dropout)]
    self.model = nn.Sequential(*model)
    self.model.apply(gaussian_weights_init)
  def forward(self, x):
    residual = x
    out = self.model(x)
    out += residual
    return out

class MisINSResBlock(nn.Module):
  def conv3x3(self, dim_in, dim_out, stride=1):
    return nn.Sequential(nn.ReflectionPad2d(1), nn.Conv2d(dim_in, dim_out, kernel_size=3, stride=stride))
  def conv1x1(self, dim_in, dim_out):
    return nn.Conv2d(dim_in, dim_out, kernel_size=1, stride=1, padding=0)
  def __init__(self, dim, dim_extra, stride=1, dropout=0.0):
    super(MisINSResBlock, self).__init__()
    self.conv1 = nn.Sequential(
        self.conv3x3(dim, dim, stride),
        nn.InstanceNorm2d(dim))
    self.conv2 = nn.Sequential(
        self.conv3x3(dim, dim, stride),
        nn.InstanceNorm2d(dim))
    self.blk1 = nn.Sequential(
        self.conv1x1(dim + dim_extra, dim + dim_extra),
        nn.ReLU(inplace=False),
        self.conv1x1(dim + dim_extra, dim),
        nn.ReLU(inplace=False))
    self.blk2 = nn.Sequential(
        self.conv1x1(dim + dim_extra, dim + dim_extra),
        nn.ReLU(inplace=False),
        self.conv1x1(dim + dim_extra, dim),
        nn.ReLU(inplace=False))
    model = []
    if dropout > 0:
      model += [nn.Dropout(p=dropout)]
    self.model = nn.Sequential(*model)
    self.model.apply(gaussian_weights_init)
    self.conv1.apply(gaussian_weights_init)
    self.conv2.apply(gaussian_weights_init)
    self.blk1.apply(gaussian_weights_init)
    self.blk2.apply(gaussian_weights_init)
  def forward(self, x, z):
    residual = x
    z_expand = z.view(z.size(0), z.size(1), 1, 1).expand(z.size(0), z.size(1), x.size(2), x.size(3))
    o1 = self.conv1(x)
    o2 = self.blk1(torch.cat([o1, z_expand], dim=1))
    o3 = self.conv2(o2)
    out = self.blk2(torch.cat([o3, z_expand], dim=1))
    out += residual
    return out

class GaussianNoiseLayer(nn.Module):
  def __init__(self,):
    super(GaussianNoiseLayer, self).__init__()
  def forward(self, x):
    if self.training == False:
      return x
    noise = Variable(torch.randn(x.size()).cuda(x.get_device()))
    return x + noise

class ReLUINSConvTranspose2d(nn.Module):
  def __init__(self, n_in, n_out, kernel_size, stride, padding, output_padding):
    super(ReLUINSConvTranspose2d, self).__init__()
    model = []
    model += [nn.ConvTranspose2d(n_in, n_out, kernel_size=kernel_size, stride=stride, padding=padding, output_padding=output_padding, bias=True)]
    model += [LayerNorm(n_out)]
    model += [nn.ReLU(inplace=True)]
    self.model = nn.Sequential(*model)
    self.model.apply(gaussian_weights_init)
  def forward(self, x):
    return self.model(x)

####################################################################
#--------------------- Spectral Normalization ---------------------
#  This part of code is copied from pytorch master branch (0.5.0)
####################################################################
class SpectralNorm(object):
  def __init__(self, name='weight', n_power_iterations=1, dim=0, eps=1e-12):
    self.name = name
    self.dim = dim
    if n_power_iterations <= 0:
      raise ValueError('Expected n_power_iterations to be positive, but '
                       'got n_power_iterations={}'.format(n_power_iterations))
    self.n_power_iterations = n_power_iterations
    self.eps = eps
  def compute_weight(self, module):
    weight = getattr(module, self.name + '_orig')
    u = getattr(module, self.name + '_u')
    weight_mat = weight
    if self.dim != 0:
      # permute dim to front
      weight_mat = weight_mat.permute(self.dim,
                                            *[d for d in range(weight_mat.dim()) if d != self.dim])
    height = weight_mat.size(0)
    weight_mat = weight_mat.reshape(height, -1)
    with torch.no_grad():
      for _ in range(self.n_power_iterations):
        v = F.normalize(torch.matmul(weight_mat.t(), u), dim=0, eps=self.eps)
        u = F.normalize(torch.matmul(weight_mat, v), dim=0, eps=self.eps)
    sigma = torch.dot(u, torch.matmul(weight_mat, v))
    weight = weight / sigma
    return weight, u
  def remove(self, module):
    weight = getattr(module, self.name)
    delattr(module, self.name)
    delattr(module, self.name + '_u')
    delattr(module, self.name + '_orig')
    module.register_parameter(self.name, torch.nn.Parameter(weight))
  def __call__(self, module, inputs):
    if module.training:
      weight, u = self.compute_weight(module)
      setattr(module, self.name, weight)
      setattr(module, self.name + '_u', u)
    else:
      r_g = getattr(module, self.name + '_orig').requires_grad
      getattr(module, self.name).detach_().requires_grad_(r_g)

  @staticmethod
  def apply(module, name, n_power_iterations, dim, eps):
    fn = SpectralNorm(name, n_power_iterations, dim, eps)
    weight = module._parameters[name]
    height = weight.size(dim)
    u = F.normalize(weight.new_empty(height).normal_(0, 1), dim=0, eps=fn.eps)
    delattr(module, fn.name)
    module.register_parameter(fn.name + "_orig", weight)
    module.register_buffer(fn.name, weight.data)
    module.register_buffer(fn.name + "_u", u)
    module.register_forward_pre_hook(fn)
    return fn

def spectral_norm(module, name='weight', n_power_iterations=1, eps=1e-12, dim=None):
  if dim is None:
    if isinstance(module, (torch.nn.ConvTranspose1d,
                           torch.nn.ConvTranspose2d,
                           torch.nn.ConvTranspose3d)):
      dim = 1
    else:
      dim = 0
  SpectralNorm.apply(module, name, n_power_iterations, dim, eps)
  return module

def remove_spectral_norm(module, name='weight'):
  for k, hook in module._forward_pre_hooks.items():
    if isinstance(hook, SpectralNorm) and hook.name == name:
      hook.remove(module)
      del module._forward_pre_hooks[k]
      return module
  raise ValueError("spectral_norm of '{}' not found in {}".format(name, module))


text:
# ### Metrics classif
#     - [>]  accuracy (torch)
#     - [>]  topk_acc (torch)
#     - [>]  confusion_matrix
# ### Metric clustering
#     - [>]  purity
#     - [>]  entropy
#     - [>]  nmi
#     - [>]  nmi
#     - [>]  topk (numpy)

import numpy as np
import torch
from typing import Optional, Sequence, Tuple, Union

def generalized_distance_matrix(X,Y):
    """
    Returns the distances between all datapoints from X and Y,
    considering each datapoint to be a row vector in each matrix individually.
    """
    n_x,k_x = X.shape
    n_y,k_y = Y.shape

    assert k_x == k_y, 'Number of cols of data X is %d and of Y is %d.'%(k_x, k_y) # dimensionality of vector spaces must be equal

    diag_x = np.zeros((n_x, 1))
    diag_y = np.zeros((1, n_y))

    for i in range(n_x):
        diag_x[i] = np.dot(X[i], X[i])

    for j in range(n_y):
        diag_y[0, j] = np.dot(Y[j], Y[j])

    g1 = diag_x@np.ones((1,n_y))
    g2 = np.ones((n_x,1))@diag_y
    D = g1 + g2 - 2*X@Y.T

    # Ensure diagonal is zero for case where computing D(X,X)
    if n_x == n_y:
        di = np.diag_indices(n_x)
        # Set all values along diagonal to zero
        D[di] = 0

    # Check for small negative values
    mask = D < 0
    if D[mask].size > 0:
        tol_neg = 1e-5
        ix_neg = np.where(mask)
        reset_vals = D[ix_neg]*-1 # flip sign
        assert np.all(reset_vals < tol_neg), "There are negative values in the distance matrix."
        D[ix_neg] =reset_vals

    return np.sqrt(D)

def generalized_distance_matrix_torch(X,Y):
    """
    Returns the distances between all datapoints from X and Y,
    considering each datapoint to be a row vector in each matrix individually.

    Notes
    -----
    Let $X \in R^{n_x \times k}, Y \in R^{n_y \times k}$, the distance between all
    points in X to all points in Y is:

    $D = \mathrm{diag}(XX^T) \mathbf{1}^T_{n_y} + \mathbf{1}_{n_x} \mathrm{diag}(YY^T)^T -2 XY^T$

    where $D_{(i,j)} = || x_i - x_j ||^2$ , i.e. the squared euclidean distance.
    """
    n_x,k_x = X.shape
    n_y,k_y = Y.shape
    dev = X.device

    assert k_x == k_y, 'Number of cols of data X is %d and of Y is %d'%(k_x, k_y) # dimensionality of vector spaces must be equal

    diag_x = torch.zeros((n_x, 1)).to(dev)
    diag_y = torch.zeros((1, n_y)).to(dev)

    for i in range(n_x):
        diag_x[i] = torch.dot(X[i], X[i])

    for j in range(n_y):
        diag_y[0, j] = torch.dot(Y[j], Y[j])

    g1 = diag_x @ torch.ones((1, n_y), device=dev)
    g2 = torch.ones((n_x, 1), device=dev) @ diag_y

    D = g1 + g2 - 2*X@Y.T

    return torch.sqrt(D)

def accuracy(y_pred:torch.Tensor, y_true:torch.Tensor):
    "Returns the accuracy (fraction) between predicted and true labels."

    # if y_pred.shape[1] == 1:
    #     y_pred = y_pred.flatten()

    acc = torch.eq(y_true, y_pred).sum().item() / y_true.shape[0]
    return acc

def topk_acc(y_pred:torch.Tensor, y_true:torch.Tensor, k = 5):
    """
    Returns topk accuracy from multiclass classification.
    Expect that `y_pred` as logits of size (y_true.shape[0], classes).
    """
    # Get indices of top k predictions along axis 1
    top_k_ixs = y_pred.topk(k = k, dim = 1).indices
    acc = torch.eq(y_true.view(-1,1), top_k_ixs).sum().item() / y_true.shape[0]
    return acc

def confusion_matrix(pred_labels, true_labels):
    """
    Returns a confusion matrix from a multiclass classification
    set of labels. Expects labels to be integers between (0, n_classes).

    Params
    ------
    pred_labels (array-like):
        List of labels as predicted by a classification algorithm.

    true_labels (array-like):
        List of ground truth labels.

    Returns
    -------
    conf_mat (array-like):
        Confusion matrix.
    """

    n_labels = int(max(np.max(pred_labels), np.max(true_labels)) + 1)

    conf_mat = np.zeros(shape = (n_labels, n_labels))

    for (i, j) in zip(pred_labels, true_labels):
        conf_mat[i,j] +=1

    return conf_mat

def purity(cm:np.array)->float:
    """
    Returns clustering purity given a confusion matrix.

    Params
    ------
    cm (array)
        Confusion matrix. Expects to have row indices
        indicating predicted values, and columns indicating
        ground truth.

    Returns
    -------
    purity(float)
        Clustering purity. Bounded between [0,1].
    """
    #Indices of maximum values of ground truth labels
    max_ixs_gt = cm.argmax(axis = 1)

    # Dummy array to get the values per row
    row_indexer = np.arange(cm.shape[0])
    sum_max_values = cm[row_indexer, max_ixs_gt].sum()
    purity=sum_max_values/cm.sum()
    return purity

def element_wise_entropy(px):
    """
    Returns a numpy array with element wise entropy calculated as -p_i*log_2(p_i).

    Params
    ------
    px (np.array)
        Array of individual probabilities, i.e. a probability vector or distribution.

    Returns
    -------
    entropy (np.array)
        Array of element-wise entropies.
    """
    if isinstance(px, list):
        px = np.array(px)

    # Make a copy of input array
    entropy = px.copy()

    # Get indices of nonzero probability values
    nz = np.nonzero(entropy)

    # Compute -pi*log_2(p_i) element-wise
    entropy[nz] *= - np.log2(entropy[nz])

    return entropy

def entropy(ps):
    "Returns the entropy of a probability distribution `ps`."
    # Get nonzero indices
    nz = np.nonzero(ps)

    # Compute entropy for nonzero indices
    entropy = np.sum(-ps[nz]*np.log2(ps[nz]))

    return entropy

def nmi_from_labels(pred_labels, true_labels):
	"""
	Returns the normalized mutual information (NMI) of two clustering
	or classification labels X and Y as:

	NMI(X,Y) = (H(X) + H(Y)) / H(X,Y)

	The intuition is that a high NMI corresponds to a dependence between
	the labels, i.e. a good classification or clustering performance.

	Params
	------
	pred_labels : arrays to compute the NMI from.

	Returns
	-------
	nmi (float):
		Normalized mutual information NMI(X,Y).

	Notes
	-----
	The NMI is bounded between 1 and 2. That is,

	* If X = Y, H(X) = H(Y) = H(X,Y) = I(X,Y), thus NMI = 2

	* If $| X \cap Y | = 0$, then H(X, Y) = H(X) + H(Y), I(X,Y) = 0,
	  and NMI = 1

	  FMI = NMI -1.

	  There is another parametrization defined as:
	  NMI(X,Y) = I(X;Y) / ((H(X) + H(Y))/2)
	  From : https://nlp.stanford.edu/IR-book/html/htmledition/evaluation-of-clustering-1.html
	"""

	# Compute joint probability
	p_xy = confusion_matrix(pred_labels, true_labels)

	p_xy /= np.sum(p_xy)

	# Marginal distribution of the predicted labels
	p_x = p_xy.sum(axis = 1)

	# Marg. dist. of true_labels
	p_y = p_xy.sum(axis = 0)

	h_x = entropy(p_x)
	h_y = entropy(p_y)

	# Sum across both axes
	h_xy = np.sum(element_wise_entropy(p_xy))

	nmi = (h_x + h_y) / h_xy

	return nmi

def get_clus_metrics(y_pred, y_true):
    """
    Returns 3 clustering metrics.
    Designed to work for cases where ground truth is known
    or a comparison between sets is amenable.
    """
    nmi = normalized_mutual_info_score(
        y_true, y_pred
    )

    ari = adjusted_rand_score(
        y_true, y_pred
    )

    cm = confusion_matrix(y_pred, y_true)
    pur = purity(cm)

    print('NMI: %.2f'%nmi)
    print('Adjusted Rand Index: %.2f'%ari)
    print('Purity: %.2f'%pur)

    return nmi, ari, pur

def topk(arr, k = 2, axis = 1):
	"""
	Returns the top-k indices over an axis.
	Numpy analog of torch.topk(tensor,k,axis).indices

	Example
	-------
	foo = np.arange(12).reshape(4, 3)
	print(foo)
	>>> array([[ 0,  1,  2],
		       [ 3,  4,  5],
		       [ 6,  7,  8],
		       [ 9, 10, 11]])

	top_ixs = sc.topk(foo, k = 2, axis = 0)
	print(top_ixs)
	>> array([[3, 3, 3],
       		  [2, 2, 2]])

	print(foo[top_ixs[:, 0], 0])
	>>> array([9, 6])

	top_ixs_row = sc.topk(foo, k = 2, axis = 1)
	print(foo[1, top_ixs_row[1, :]])
	>>> array([5, 4])

	"""

	ixs = np.argpartition(arr, -k, axis = axis)

	if axis == 0:
		return np.flipud(ixs[-k:, :])
	elif axis == 1:
		return np.fliplr(ixs[:, -k:])
	else:
		print('Function available for 2D arrays only.')
		return None

max_stars_repo_name: manuflores/magma

max_issues_repo_name: manuflores/magma

max_forks_repo_name: manuflores/magma


text:
import numpy as np
import partitioning
import pickle
import h5py
import pandas as pd
import sys
VGGM = -5.24
VGGS = 8.17

def map_labels(labels):
    return labels - 1

def soften_ordinal_labels(labels, m=0.05):
    # this function softens the ordinal labels for better training.
    labels_ = labels.copy()
    labels_[labels==1] = 1.0 - m
    for l in range(labels.shape[0]):
        # assuming first dimension is batch and second dimension is classes
        maxindex = np.argmax(labels[l])
        if maxindex == 0:
            labels_[l,1] = m
        elif maxindex == labels.shape[1]-1:
            labels_[l,-2] = m
        else:
            labels_[l,maxindex-1] = m / 2.0
            labels_[l,maxindex+1] = m / 2.0
    return labels_.astype(np.float32)

def normalize_features(x):
    return (x - VGGM) / VGGS

class Dataset:
    def __init__(self, image_hdf5_file, label_csv_file, label_name, clabel_name=None):
        self.imf = h5py.File(image_hdf5_file, 'r')
        self.codes = self.imf['features']
        self.raw_label_data = pd.read_csv(label_csv_file)
        self.labels = map_labels(self.raw_label_data[label_name].to_numpy())
        self.vmap = self.raw_label_data[['img_id', 'pcd', 'oa11', 'lsoa11']].to_numpy()
        self.label_name=label_name
        # clabel_name is the name of the label that will be used to constrain partitioning.
        # e.g. Images coming from the same lsoa will not be separated during partitioning.
        # all of them will remain in the same partition.
        # if it is None, then no constrain is given.
        if clabel_name is not None:
            self.clabels = self.raw_label_data[clabel_name].to_numpy()
        else:
            self.clabels = None

        # population label distribution
        self.label_types, self.label_counts = np.unique(self.labels, return_counts=True)
        self.label_dist = self.label_counts / np.float(self.label_counts.sum())
        self.num_labels = self.label_types.size
        self.batch_inds = np.zeros(self.num_labels, dtype=np.int)
        print('Label types: {}'.format(self.label_types))
        print('Label counts: {}'.format(self.label_counts))
        print('Label distributions: {}'.format(self.label_dist))
        # = indices to keep track of which samples are used so far within
        # = training. important to count epochs, rather than iterations.
        self.batch_ind = 0
        self.batch_ind_test = 0
        self.batch_ind_valid = 0
        # = place holders
        self.train_part = []
        self.test_part = []
        self.validation_part = []

    # = this function gets features and labels of samples with ids in the
    # = list rows.
    def get_data_part(self, rows, noise_std=1):
        srows = sorted(rows)
        if np.isscalar(srows):
            srows = [srows]
        d1 = normalize_features(self.codes[srows, 0, :])
        d2 = normalize_features(self.codes[srows, 1, :])
        d3 = normalize_features(self.codes[srows, 2, :])
        d4 = normalize_features(self.codes[srows, 3, :])
        if noise_std is not None:
            d1 += np.random.normal(loc=0, scale=0.05, size=d1.shape)
            d2 += np.random.normal(loc=0, scale=0.05, size=d2.shape)
            d3 += np.random.normal(loc=0, scale=0.05, size=d3.shape)
            d4 += np.random.normal(loc=0, scale=0.05, size=d4.shape)
        l = np.zeros((d1.shape[0], self.num_labels), dtype=np.float)
        lpart = self.labels[srows]
        for lab_, k in zip(self.label_types, range(self.num_labels)):
            try:
                l[lpart == lab_, k] = 1
            except:
                print(srows, lab_, k, lpart, l)
                sys.exit(1)
        return d1, d2, d3, d4, l

    def get_train_batch(self, batch_size):
        rows = self.train_part[self.batch_ind: self.batch_ind + batch_size]
        d1, d2, d3, d4, l = self.get_data_part(rows)
        self.batch_ind += batch_size
        if self.batch_ind >= len(self.train_part):
            self.batch_ind = 0
        return d1, d2, d3, d4, l

    def get_balanced_train_batch(self, batch_size):
        rows = []
        lsize = self.label_types.size
        lbatch_size = np.int(batch_size / np.float(lsize))
        for l in self.label_types:
            lrows = np.random.permutation(np.where(self.labels[self.train_part] == l)[0])
            lrows = lrows[:lbatch_size].astype(np.int)
            rows += list(np.asarray(self.train_part)[lrows])
        d1, d2, d3, d4, l = self.get_data_part(rows)
        return d1, d2, d3, d4, l

    def get_train_data(self):
        rows = self.train_part
        d1, d2, d3, d4, l = self.get_data_part(rows)
        return d1, d2, d3, d4, l

    def get_validation_batch(self, batch_size):
        rows = self.validation_part[self.batch_ind_valid: self.batch_ind_valid + batch_size]
        d1, d2, d3, d4, l = self.get_data_part(rows)
        self.batch_ind_valid += batch_size
        if self.batch_ind_valid >= len(self.validation_part):
            self.batch_ind_valid = 0
        return d1, d2, d3, d4, l

    def get_balanced_validation_batch(self, batch_size):
        rows = []
        lsize = self.label_types.size
        lbatch_size = np.int(batch_size / np.float(lsize))
        for l in self.label_types:
            lrows = np.random.permutation(np.where(self.labels[self.validation_part] == l)[0])
            lrows = lrows[:lbatch_size].astype(np.int)
            rows += list(np.asarray(self.validation_part)[lrows])
        d1, d2, d3, d4, l = self.get_data_part(rows)
        return d1, d2, d3, d4, l

    def get_validation_data(self):
        rows = self.validation_part
        d1, d2, d3, d4, l = self.get_data_part(rows)
        return d1, d2, d3, d4, l

    def get_test_batch(self, batch_size):
        rows = self.test_part[self.batch_ind_test: self.batch_ind_test + batch_size]
        d1, d2, d3, d4, l = self.get_data_part(rows)
        self.batch_ind_test += batch_size
        if self.batch_ind_test >= len(self.test_part):
            self.batch_ind_test = 0
        return d1, d2, d3, d4, l

    def get_test_data(self):
        rows = self.test_part
        d1, d2, d3, d4, l = self.get_data_part(rows)
        return d1, d2, d3, d4, l

    def test_iterator(self, batch_num=1):
        num_iter = np.int(np.ceil(len(self.test_part) / batch_num))
        for n in range(num_iter):
            rows = self.test_part[n*batch_num : (n+1)*batch_num]
            yield self.get_data_part(rows), self.vmap[sorted(rows),:]

    def validation_iterator(self, batch_num=1):
        num_iter = np.int(np.ceil(len(self.validation_part) / batch_num))
        for n in range(num_iter):
            rows = self.validation_part[n*batch_num : (n+1)*batch_num]
            yield self.get_data_part(rows)

    def write_preds(self, preds, fname):
        srows = sorted(self.test_part)
        data_matrix=np.append(self.vmap[srows,:],self.labels[srows,np.newaxis],axis=1)
        data_matrix=np.append(data_matrix,preds[:,np.newaxis],axis=1)
        pred_matrix=pd.DataFrame(data=data_matrix,columns=['img_id', 'pcd', 'oa11', 'lsoa11',self.label_name,'predicted'])
        pred_matrix.to_csv(fname,index=False)

class Dataset_CrossValidation(Dataset):
    def __init__(self, image_hdf5_file, label_csv_file, label_name, clabel_name=None):
        Dataset.__init__(self,
                         image_hdf5_file,
                         label_csv_file,
                         label_name,
                         clabel_name=clabel_name)

    def pick_label(self, part_gen, part_file, part_kn=5, part_kp=0, vsize=0.1, seed=None):
        '''
            This runs at every creation instance
            label_type: 'cat' (categorial), 'cont' (continuous)
        '''
        if part_gen == 1:
            # = this part creates partitions from the data and saves them in a specified file.
            # = the partitioning is k-folds and stratified.
            # = it also allows constraints, see above comment, as clabels.
            print('==================================== generating partitions from selected classes =====================================')
            self.kpartitions=partitioning.partition_stratified_kfold(part_kn,
                                                                     self.labels,
                                                                     seed=seed,
                                                                     clabels=self.clabels)

            print('==================================== generating partitions =====================================')
            self.kpartitions=partitioning.partition_stratified_kfold(part_kn,
                                                                     self.labels,
                                                                     seed=seed,
                                                                     clabels=self.clabels)
            pickle.dump(self.kpartitions, open(part_file, 'wb'))
        else:
            # = reads a partitioning that was written before.
            # = e.g. if 5 fold cross-validation is used, then this file simply
            # = will have 5 partitions written in it. self.kpartitions is a
            # = list with 5 members and each member has a list of data sample
            # = ids.
            self.kpartitions=pickle.load(open(part_file, 'rb'))

        # = creates training and test part from the self.kpartitions.
        # = e.g. in 5 fold cross validation, it uses the fold part_kp (1...5)
        # = as test and combines the remaining 4 as training data.
        _train_part, self.test_part = partitioning.get_partition_stratified_kfold(part_kp, self.kpartitions)

        # = vsize indicates the portion of the training set that will be used as validation.
        # = the default value is set to 0.1, meaning 10% of all training examples.
        if vsize > 0.0:
            self.train_part, self.validation_part = partitioning.decimate_partition_stratified(_train_part, self.labels, psize=1.0 - vsize)
        else:
            self.train_part = _train_part

# = this class simply divides the dataset into three classes:
# == Train (T)
# == Validation (V)
# == Test (T)
class Dataset_TVT(Dataset):
    def __init__(self, image_hdf5_file, label_csv_file, label_name, clabel_name=None):
        Dataset.__init__(self,
                         image_hdf5_file,
                         label_csv_file,
                         label_name,
                         clabel_name=clabel_name)

    def pick_label(self, part_gen, part_file, train_size, valid_size, psize=1.0, seed=None):
        '''
            This runs at every creation instance
            label_type: 'cat' (categorial), 'cont' (continuous)
        '''
        if part_gen == 1:
            # = this part creates partitions from the data and saves them in a specified file.
            # = the partitioning is stratified and only 3 parts: train, test and validation.
            # = it also allows constrains, see above comment, as clabels.
            print('==================================== generating partitions =====================================')
            _train_part, self.validation_part, self.test_part = partitioning.partition_stratified_validation(self.labels,
                                                                                train_size,
                                                                                valid_size,
                                                                                seed=seed,
                                                                                clabels=self.clabels)
            pickle.dump(_train_part, open(part_file + '_train', 'wb'))
            pickle.dump(self.validation_part, open(part_file + '_validation', 'wb'))
            pickle.dump(self.test_part, open(part_file + '_test', 'wb'))
        else:
            # = reads a partitioning that was written before.
            # = there are three files: validation, test and train
            # = this part reads all of them.
            _train_part=pickle.load(open(part_file + '_train', 'rb'))
            self.validation_part=pickle.load(open(part_file + '_validation', 'rb'))
            self.test_part=pickle.load(open(part_file + '_test', 'rb'))

        # = psize indicates the percentage of training data to be used during
        # = training. If it is 1.0, then we use all the training data. So,
        # = self.train_part = _train_part
        # = if it is less then 1.0 then we take a subset of the training data
        # = with the same proportions of classes, i.e. stratified.
        # = Note that inside decimate_partition_stratified code, we randomly
        # = permute over the samples. So, every time you run this code,
        # = training will happen with another subset of size psize.
        if psize < 1.0:
            self.train_part = partitioning.decimate_partition_stratified(_train_part, self.labels, psize=psize)
        else:
            self.train_part = _train_part

    def write_preds_validation(self, preds, fname):
        srows = sorted(self.validation_part)
        data_matrix=np.append(self.vmap[srows,:],self.labels[srows,np.newaxis],axis=1)
        data_matrix=np.append(data_matrix,preds[:,np.newaxis],axis=1)
        pred_matrix=pd.DataFrame(data=data_matrix,columns=['img_id', 'pcd', 'oa11', 'lsoa11',self.label_name,'predicted'])
        pred_matrix.to_csv(fname,index=False)

# = this class simply divides the dataset into two classes:
# == Train (T)
# == Test (T)
class Dataset_TT(Dataset):
    def __init__(self, image_hdf5_file, label_csv_file, label_name, clabel_name=None):
        Dataset.__init__(self,
                         image_hdf5_file,
                         label_csv_file,
                         label_name,
                         clabel_name=clabel_name)

    def pick_label(self, part_gen, part_file, train_size, psize=1.0, seed=None):
        '''
            This runs at every creation instance
            label_type: 'cat' (categorial), 'cont' (continuous)
        '''

        if part_gen == 1:
            # = this part creates partitions from the data and saves them in a specified file.
            # = the partitioning is stratified and only 2 parts: train, and test .

            print('==================================== generating partitions =====================================')
            _train_part, self.test_part = partitioning.partition_stratified(self.labels,
                                                                            train_size,
                                                                            seed=seed,
                                                                            clabels=self.clabels)
            pickle.dump(_train_part, open(part_file + '_train', 'wb'))
            pickle.dump(self.test_part, open(part_file + '_test', 'wb'))
        else:
            # = reads a partitioning that was written before.
            # = there are three files: validation, test and train
            # = this part reads all of them.
            _train_part=pickle.load(open(part_file + '_train', 'rb'))
            self.test_part=pickle.load(open(part_file + '_test', 'rb'))

        # = psize indicates the percentage of training data to be used during
        # = training. If it is 1.0, then we use all the training data. So,
        # = self.train_part = _train_part
        # = if it is less then 1.0 then we take a subset of the training data
        # = with the same proportions of classes, i.e. stratified.
        # = Note that inside decimate_partition_stratified code, we randomly
        # = permute over the samples. So, every time you run this code,
        # = training will happen with another subset of size psize.
        if psize < 1.0:
            self.train_part = partitioning.decimate_partition_stratified(_train_part, self.labels, psize=psize)
        else:
            self.train_part = _train_part

    def get_validation_batch(self, batch_size):
        return self.get_train_batch(batch_size)

    def get_balanced_validation_batch(self, batch_size):
        return self.get_balanced_train_batch(batch_size)

    def write_preds_validation(self, preds, fname):
        srows = sorted(self.validation_part)
        data_matrix=np.append(self.vmap[srows,:],self.labels[srows,np.newaxis],axis=1)
        data_matrix=np.append(data_matrix,preds[:,np.newaxis],axis=1)
        pred_matrix=pd.DataFrame(data=data_matrix,columns=['img_id', 'pcd', 'oa11', 'lsoa11',self.label_name,'predicted'])
        pred_matrix.to_csv(fname,index=False)

# = this class simply divides the dataset into two classes using a class label:
# == Train (T)
# == Test (T) : test set consists of only the selected class label

class Dataset_TT_byclass(Dataset):
    def __init__(self, image_hdf5_file, label_csv_file, label_name, clabel_name=None, label_test=None):
        Dataset.__init__(self,
                         image_hdf5_file,
                         label_csv_file,
                         label_name,
                         clabel_name=clabel_name)
        self.label_test = label_test

    def pick_label(self, part_gen, part_file, train_size, psize=1.0, seed=None):
        '''
            This runs at every creation instance
            label_type: 'cat' (categorial), 'cont' (continuous)
        '''

        if part_gen == 1:
            # = this part creates partitions from the data and saves them in a specified file.
            # = the partitioning is stratified and only 2 parts: train, and test .

            print('==================================== generating partitions =====================================')
            _train_part, self.test_part = partitioning.partition_by_class(self.labels,
                                                                            self.label_test,
                                                                            seed=seed)
            pickle.dump(_train_part, open(part_file + '_train', 'wb'))
            pickle.dump(self.test_part, open(part_file + '_test', 'wb'))

        else:
            # = reads a partitioning that was written before.
            # = there are three files: validation, test and train
            # = this part reads all of them.
            _train_part=pickle.load(open(part_file + '_train', 'rb'))
            self.test_part=pickle.load(open(part_file + '_test', 'rb'))

        # = psize indicates the percentage of training data to be used during
        # = training. If it is 1.0, then we use all the training data. So,
        # = self.train_part = _train_part
        # = if it is less then 1.0 then we take a subset of the training data
        # = with the same proportions of classes, i.e. stratified.
        # = Note that inside decimate_partition_stratified code, we randomly
        # = permute over the samples. So, every time you run this code,
        # = training will happen with another subset of size psize.
        if psize < 1.0:
            self.train_part = partitioning.decimate_partition_stratified(_train_part, self.labels, psize=psize)
        else:
            self.train_part = _train_part

    def get_validation_batch(self, batch_size):
        return self.get_train_batch(batch_size)

    def get_balanced_validation_batch(self, batch_size):
        return self.get_balanced_train_batch(batch_size)

    def write_preds_validation(self, preds, fname):
        srows = sorted(self.validation_part)
        data_matrix=np.append(self.vmap[srows,:],self.labels[srows,np.newaxis],axis=1)
        data_matrix=np.append(data_matrix,preds[:,np.newaxis],axis=1)
        pred_matrix=pd.DataFrame(data=data_matrix,columns=['img_id', 'pcd', 'oa11', 'lsoa11',self.label_name,'predicted'])
        pred_matrix.to_csv(fname,index=False)

max_stars_repo_name: esrasuel/combiningSatellite-StreetView

max_issues_repo_name: esrasuel/combiningSatellite-StreetView

max_forks_repo_name: esrasuel/combiningSatellite-StreetView