Example #1
0
def main():
    l = 15
    h = 0.1
    j = 1
    n_configs = 250
    n_usable_configs = 200
    temps = linspace(10, .5, 20)
    energy = zeros((len(temps), 1))
    initial_energy = ones((l, l))
    last_lattice = initial_energy

    for iTemp, temp in enumerate(temps):
        lattice = generate_configs(l, h, j, last_lattice, n_configs,
                                   n_usable_configs, temp)
        lattice_pad = zeros((l + 2, l + 2, n_usable_configs))
        for config in range(n_usable_configs):
            lattice_pad[:, :, config] = pad(lattice[:, :, config])

        north = lattice_pad[0:-2, 1:-1, :]
        south = lattice_pad[2:, 1:-1, :]
        east = lattice_pad[1:-1, 2:, :]
        west = lattice_pad[1:-1, 0:-2, :]

        f = -1 * h * sum(lattice)
        s = -1 * 0.5 * j * sum(lattice * (north + south + east + west))
        energy_temp = (f + s) / float(n_usable_configs)
        energy[iTemp] = energy_temp
        #print('energy for ', temp, ' = ', energy_temp)
        last_lattice = lattice[:, :, -1]
    #print(energy)
    print size(temps)
    print size(energy)
    plotxy(temps, energy)
Example #2
0
def evaluate(gamma, mu):
    l = 10
    k = 1
    n = l ** 2
    nu = 1
    h0 = np.zeros((l, l))
    rho = 1 + (k * np.exp((gamma / 2) * 2 - mu))

    # yes lambda is spelled wrong, but it is a reserved keyword in python
    lamda = 1 / (n * rho)
    num_tests = 10

    t = 0
    t_index = 1

    times = np.zeros((10 ** 6, 1))
    while t <= 20:
        times[t_index] = t
        t_index += 1
        time = -np.log(random()) / (1 / lamda)
        t += time
    times = times[:t_index]
    times = np.squeeze(times)

    avg_list = np.zeros((t_index, num_tests))
    rough_list = np.zeros((t_index, num_tests))
    mono_layer_list = np.zeros((t_index, num_tests))
    coverage_list = np.zeros((t_index, num_tests))

    for iTest in range(num_tests):
        grid = growth(t_index, h0, gamma, k, mu)
        h_pad = np.zeros((l + 2, l + 2, t_index))

        for i in range(t_index - 1):
            h_pad[:, :, i] = pad(grid[:, :, i])

        end_x = h_pad.shape[0] - 1
        end_y = h_pad.shape[1] - 1
        north = h_pad[0:end_x - 1, 1:end_y, :]
        south = h_pad[2:end_x + 1, 1:end_y, :]
        east = h_pad[1:end_x, 2:end_y + 1, :]
        west = h_pad[1:end_x, 0:end_y - 1, :]
        r = np.sum(
            np.abs(grid - north) +
            np.abs(grid - south) +
            np.abs(grid - east) +
            np.abs(grid - west), 0)
        rough = np.sum(r, 0) / (2 * (l ** 2))
        rough_list[:, iTest] = np.squeeze(np.squeeze(rough))

        grid[grid < 0] = 0
        avg_list[:, iTest] = np.squeeze(np.mean(np.mean(grid, 0), 0))
        mono_layer_list[:, iTest] = np.squeeze(np.floor((np.sum(np.sum(grid, 0), 0)) / l ** 2))
        coverage_list[:, iTest] = np.squeeze(np.sum(np.sum(grid >= nu, 0), 0) / l ** 2)

    avg_plot = Scatter(x=times, y=np.mean(avg_list, 1), name="gamma = " + str(gamma) + " mu = " + str(mu))
    mono_plot = Scatter(x=times, y=np.mean(mono_layer_list, 1), name="gamma = " + str(gamma) + " mu = " + str(mu))
    rough_plot = Scatter(x=times, y=np.mean(rough_list, 1), name="gamma = " + str(gamma) + " mu = " + str(mu))
    coverage_plot = Scatter(x=times, y=np.mean(coverage_list, 1), name="gamma = " + str(gamma) + " mu = " + str(mu))
    return [avg_plot, mono_plot, rough_plot, coverage_plot]
Example #3
0
def main():
    l = 15
    h = 0.1
    j = 1
    n_configs = 250
    n_usable_configs = 200
    temps = linspace(10,.5,20)
    energy = zeros((len(temps), 1))
    initial_energy = ones((l, l))
    last_lattice = initial_energy
    
    for iTemp, temp in enumerate(temps):
        lattice = generate_configs(l, h, j, last_lattice, n_configs, n_usable_configs, temp)
        lattice_pad = zeros((l+2, l+2, n_usable_configs))
        for config in range(n_usable_configs):
            lattice_pad[:,:,config] = pad(lattice[:,:,config])
        
        north = lattice_pad[0:-2, 1:-1, :]
        south = lattice_pad[2:, 1:-1, :]
        east = lattice_pad[1:-1, 2:, :]
        west = lattice_pad[1:-1, 0:-2, :]
        
        f = -1 * h * sum(lattice)
        s = -1 * 0.5*j * sum(lattice * 
            (north + south + east + west))
        energy_temp = (f+s)/float(n_usable_configs)
        energy[iTemp] = energy_temp
        #print('energy for ', temp, ' = ', energy_temp)
        last_lattice = lattice[:,:,-1]
    #print(energy)
    print size(temps)
    print size(energy)
    plotxy(temps, energy)
Example #4
0
def cbc_enc(pt, key, iv):
    ct = b''
    padded = pad(pt, 16)
    for i in range(len(padded) // 16):
        send2 = xor(padded[i * 16:(i + 1) * 16], iv)
        iv = ecb_enc(send2, key)
        ct += iv
    return ct
def evaluate(gamma, mu):
    l = 10
    k = 1
    n = l**2
    nu = 1
    h0 = zeros((l, l))
    rho = 1 + (k * exp((gamma / 2.0) * 2 - mu))

    beta = 1 / float(n * rho)
    num_tests = 10

    t = 0
    idx = 1

    times = zeros((10**5, 1))
    while t <= 20:
        times[idx] = t
        idx += 1
        time = -log(random()) / float(1 / float(beta))
        t += time
    times = times[:idx]
    times = squeeze(times)

    avg_list = zeros((idx, num_tests))
    rough_list = zeros((idx, num_tests))
    mono_layer_list = zeros((idx, num_tests))
    coverage_list = zeros((idx, num_tests))

    for iTest in range(num_tests):
        grid = growth_model(idx, h0, gamma, k, mu)
        h_pad = zeros((l + 2, l + 2, idx))

        for i in range(idx - 1):
            h_pad[:, :, i] = pad(grid[:, :, i])

        end_x = h_pad.shape[0] - 1
        end_y = h_pad.shape[1] - 1
        north = h_pad[0:end_x - 1, 1:end_y, :]
        south = h_pad[2:end_x + 1, 1:end_y, :]
        east = h_pad[1:end_x, 2:end_y + 1, :]
        west = h_pad[1:end_x, 0:end_y - 1, :]
        r = sum(
            abs(grid - north) + abs(grid - south) + abs(grid - east) +
            abs(grid - west), 0)
        rough = sum(r, 0) / float(2 * (l**2))
        rough_list[:, iTest] = squeeze(squeeze(rough))

        grid[grid < 0] = 0
        avg_list[:, iTest] = squeeze(mean(mean(grid, 0), 0))
        mono_layer_list[:, iTest] = squeeze(
            floor((sum(sum(grid, 0), 0) / float(l**2))))
        coverage_list[:,
                      iTest] = squeeze(sum(sum(grid >= nu, 0),
                                           0 / float(l**2)))

    title = "figure"  #"gamma-"+ str(gamma) + "mu-" + str(mu)
    plotxy(times, mean(mono_layer_list, 1), title)
Example #6
0
def init_pads():
    pad_game = pad(pygame, game_constants.pad_image)
    
    pad_game.rect.x = game_constants.game_size.width / 2 - game_constants.pad_size.width/2
    
    pad_game.rect.y = (game_constants.game_size.height - game_constants.pad_size.height)
    
    pad_list.add(pad_game)
    
    return pad_game
Example #7
0
def cbc_encrypt(key, plaintext, iv):
	encryptor = AES.new(key)
	paddedText = pad.pad(plaintext, 16)
	fullText = iv
	cText = iv
   
	for i in range(len(paddedText) / 16):
	   block = pad.XOR_text_key(paddedText[i * 16:16 + (i * 16)], cText)
	   cText = encryptor.encrypt(block)
	   fullText += cText
	return fullText
Example #8
0
def ecb_encrypt(key, plaintext, blocksize):
    encryptor = AES.new(key)
    paddedText = pad.pad(plaintext, blocksize)
    fullText = ""
    for i in range(len(paddedText) / blocksize):
        #print(pad.ascii_to_hex(paddedText[i * blocksize:blocksize + (i * blocksize)]) + '\n')
        block = paddedText[i * blocksize:blocksize + (i * blocksize)]
        ctext = encryptor.encrypt(block)
        #print(pad.ascii_to_hex(ctext))
        fullText += ctext
    return fullText
Example #9
0
def main():
    l = 15
    h = 0.1
    j = 1
    n_configs = 250
    n_usable_configs = 200
    temps = numpy.linspace(10, .5, 20) 
    specific_heat = numpy.zeros((len(temps), 1))
    stable_spins = numpy.zeros((len(temps),1))

    previous_lattice = [[round(random()) * 2 -1 for x in range(l)] for y in range(l)]
    previous_lattice = numpy.array(previous_lattice)

    for index, temp in enumerate(temps):
        print(temp)
        lattice = create_lattice(l,h,j, previous_lattice, n_configs, n_usable_configs, temp)
        lattice_pad = numpy.zeros((l+2, l+2, n_usable_configs))

        for config in range(n_usable_configs):
            lattice_pad[:,:,config] = pad(lattice[:,:,config])
        
        end_x = lattice_pad.shape[0]-1
        end_y = lattice_pad.shape[1]-1
        north = lattice_pad[0:end_x-1,1:end_y,:]
        south = lattice_pad[2:end_x+1,1:end_y,:]
        east = lattice_pad[1:end_x,2:end_y+1,:]
        west = lattice_pad[1:end_x,0:end_y-1,:]

        f = -1 * h * numpy.sum(lattice)
        s = -1 * j/2 * numpy.sum(lattice * 
                (north + south + east + west))
        energy = (f+s)/n_usable_configs
        heat = (energy**2/n_usable_configs - energy**2)/(temp**2) 
        specific_heat[index] = heat 
        
        #q5
        spin = (lattice==north) & (lattice==south) & (lattice==east) & (lattice==west)
        stable_spins[index] = numpy.sum(spin)/n_usable_configs
        
        previous_lattice = lattice[:,:,-1]
    
    plotly.offline.plot({
        "data": [ Scatter(x=temps, y=specific_heat, mode="markers") ],
        "layout": Layout(title="Specific Heat")
        }, filename="q2.html")
    fitted_line = scipy.stats.powerlaw.fit(stable_spins)
    power_eq = scipy.stats(power_eq)
    power_law = [power_eq(x) for x in temps]
    plotly.offline.plot({
        "data": [ Scatter(x=temps, y=stable_spins, mode="markers"),
            Scatter(x=temps, y=power_law, mode="lines")],
        "layout": Layout(title="Stable Spins")
        }, filename="q5.html")
def evaluate(gamma, mu):
	l = 10
	k = 1
	n = l ** 2
	nu = 1
	h0 = zeros((l, l))
	rho = 1 + (k * exp((gamma / 2.0)*2-mu))

	beta = 1 / float(n*rho)
	num_tests = 10

	t = 0
	idx = 1

	times = zeros((10**5, 1))
	while t <= 20:
		times[idx] = t
		idx += 1
		time = -log(random()) / float(1 / float(beta))
		t += time
	times = times[:idx]
	times = squeeze(times)

	avg_list = zeros((idx, num_tests))
	rough_list = zeros((idx, num_tests))
	mono_layer_list = zeros((idx, num_tests))
	coverage_list = zeros((idx, num_tests))

	for iTest in range(num_tests):
		grid = growth_model(idx, h0, gamma, k, mu)
		h_pad = zeros((l+2, l+2, idx))

		for i in range(idx-1):
			h_pad[:, :, i] = pad(grid[:, :, i])

		end_x = h_pad.shape[0] - 1
		end_y = h_pad.shape[1] - 1
		north = h_pad[0:end_x - 1, 1:end_y, :]
		south = h_pad[2:end_x + 1, 1:end_y, :]
		east = h_pad[1:end_x, 2:end_y + 1, :]
		west = h_pad[1:end_x, 0:end_y - 1, :]
		r = sum(abs(grid-north) + abs(grid-south) + abs(grid-east) + abs(grid-west), 0)
		rough = sum(r,0) / float(2*(l**2))
		rough_list[:, iTest] = squeeze(squeeze(rough))

		grid[grid < 0] = 0
		avg_list[:, iTest] = squeeze(mean(mean(grid, 0), 0))
		mono_layer_list[:, iTest] = squeeze(floor((sum(sum(grid, 0), 0) / float(l ** 2))))
		coverage_list[:, iTest] = squeeze(sum(sum(grid >= nu, 0), 0 / float(l**2)))
	
	title = "figure" #"gamma-"+ str(gamma) + "mu-" + str(mu)
	plotxy(times, mean(mono_layer_list, 1), title)
Example #11
0
def operation():
    l, H, J = 15, 0.1, 1
    nbr_configs, nbr_configs2use = 250, 200
    tt_list = [x / float(10) for x in range(100, 0, -5)]
    #print tt_list
    expg_sigma = zeros([size(tt_list), 1])

    m0 = around(random.rand(l, l), 0) * 2 - 1
    last_latt = m0

    for tt in range(size(tt_list)):
        latt3D = generate_configs(l, H, J, last_latt, nbr_configs,
                                  nbr_configs2use, tt_list[tt])
        #print latt3D.shape
        latt3D_pad = zeros(([l + 2, l + 2, nbr_configs2use]))
        #print latt3D_pad.shape
        for cind in range(nbr_configs2use):
            latt3D_pad[:, :, cind] = pad(latt3D[:, :, cind])

        north = latt3D_pad[0:-2, 1:-1, :]
        south = latt3D_pad[2:, 1:-1, :]
        east = latt3D_pad[1:-1, 2:, :]
        west = latt3D_pad[1:-1, 0:-2, :]
        #print latt3D==north
        #print latt3D_pad.shape
        #print north.shape
        #print north.shape
        stable_spins3D = (latt3D == north) & (latt3D == south) & (
            latt3D == east) & (latt3D == west)
        #print stable_spins3D[:,:,0]
        #print latt3D
        #print north
        #print sum(latt3D==east)
        #print sum(latt3D==west)
        g_sigma = sum(stable_spins3D)
        print g_sigma
        #print g_sigma
        #print south.shape
        #print east.shape
        #print west.shape
        expg_sigma[tt] = g_sigma / float(nbr_configs2use)
        last_latt = latt3D[:, :, -1]
    plotxy(tt_list, expg_sigma)
def generate_configs(l, h, j, initial_lattice, n_configs, n_usable_configs, t_tilde):
	lattice_full = zeros((l, l, n_configs))
	lattice = initial_lattice

	for x in range(0, n_configs):
		for t in range(0, l**2):
			r = floor(rand.random()*l)
			c = floor(rand.random()*l)
			new_lattice = lattice
			new_lattice[r, c] *= -1 #flip randomly selected lattice sites 
			padded_new_lattice = pad(new_lattice)
			[accepted, accepted_prob] = grid(h, j, padded_new_lattice, r, c, t_tilde)
			if accepted:
				lattice = new_lattice

		lattice_full[:, :, x] = lattice

	lattice_full = delete(lattice_full, range(0, n_configs - n_usable_configs), 2)
	return lattice_full
def generate_configs(l, h, j, initial_lattice, 
        n_configs, n_usable_configs, t_tilde):
    
    lattice_full = zeros((l, l, n_configs))

    for x in range(n_configs):
        for t in range(l**2):
            r = int(random.random() * l)
            c = int(random.random() * l)
            new_lattice = initial_lattice
            new_lattice[r, c] = new_lattice[r,c] * -1
	    #print new_lattice.shape
            padded_new_lattice = pad(new_lattice)
            [accepted, accepted_prob] = test_grid(h, j, padded_new_lattice, r, c, t_tilde)
            if accepted:
                lattice = new_lattice
        lattice_full[:,:,x] = lattice
    size_z = lattice_full.shape[2]
    lattice3D = lattice_full[:,:,size_z-n_usable_configs:size_z]
    return lattice3D
def create_lattice(l, h, j, initial_lattice,
                   n_configs, n_usable_configs, t_tilde):
    lattice_full = numpy.zeros((l, l, n_configs))
    lattice = initial_lattice

    for x in range(0, n_configs):
        for t in range(0, l ** 2):
            r = floor(random.random() * l)
            c = floor(random.random() * l)
            new_lattice = lattice
            new_lattice[r, c] *= -1
            padded_new_lattice = pad(new_lattice)
            [accepted, accepted_prob] = test_grid(h, j, padded_new_lattice, r, c, t_tilde)
            if accepted:
                lattice = new_lattice

        lattice_full[:, :, x] = lattice

    lattice_full = numpy.delete(lattice_full, range(0, n_configs - n_usable_configs), 2)
    return lattice_full
Example #15
0
def operation():
	l,H,J=15,0.1,1
	nbr_configs, nbr_configs2use = 250,200
	tt_list = [x/float(10) for x in range(100,0,-5)]
	#print tt_list
	expg_sigma = zeros([size(tt_list),1])
	
	m0 = around(random.rand(l,l),0)*2 -1
	last_latt = m0

	for tt in range(size(tt_list)):
		latt3D = generate_configs(l, H, J, last_latt, nbr_configs, nbr_configs2use, tt_list[tt])
		#print latt3D.shape
		latt3D_pad = zeros(([l+2, l+2, nbr_configs2use]))
		#print latt3D_pad.shape
		for cind in range(nbr_configs2use):
			latt3D_pad[:,:,cind] = pad(latt3D[:,:,cind])
		
		north = latt3D_pad[0:-2, 1:-1, :]
		south = latt3D_pad[2:, 1:-1, :]
		east = latt3D_pad[1:-1, 2:, :]
		west = latt3D_pad[1:-1, 0:-2, :]
		#print latt3D==north
		#print latt3D_pad.shape
		#print north.shape
		#print north.shape
		stable_spins3D = (latt3D==north) & (latt3D==south) & (latt3D==east) & (latt3D==west)
		#print stable_spins3D[:,:,0]
		#print latt3D
		#print north
		#print sum(latt3D==east)
		#print sum(latt3D==west)
		g_sigma = sum(stable_spins3D)
		print g_sigma
		#print g_sigma
		#print south.shape
		#print east.shape
		#print west.shape
		expg_sigma[tt] = g_sigma/float(nbr_configs2use)
		last_latt = latt3D[:,:,-1]
	plotxy(tt_list, expg_sigma)	
Example #16
0
def generate_configs(l, h, j, initial_lattice, n_configs, n_usable_configs,
                     t_tilde):

    lattice_full = zeros((l, l, n_configs))

    for x in range(n_configs):
        for t in range(l**2):
            r = int(random.random() * l)
            c = int(random.random() * l)
            new_lattice = initial_lattice
            new_lattice[r, c] = new_lattice[r, c] * -1
            #print new_lattice.shape
            padded_new_lattice = pad(new_lattice)
            [accepted, accepted_prob] = test_grid(h, j, padded_new_lattice, r,
                                                  c, t_tilde)
            if accepted:
                lattice = new_lattice
        lattice_full[:, :, x] = lattice
    size_z = lattice_full.shape[2]
    lattice3D = lattice_full[:, :, size_z - n_usable_configs:size_z]
    return lattice3D
def generate_configs(l, h, j, initial_lattice, n_configs, n_usable_configs,
                     t_tilde):
    lattice_full = zeros((l, l, n_configs))
    lattice = initial_lattice

    for x in range(0, n_configs):
        for t in range(0, l**2):
            r = floor(rand.random() * l)
            c = floor(rand.random() * l)
            new_lattice = lattice
            new_lattice[r, c] *= -1  #flip randomly selected lattice sites
            padded_new_lattice = pad(new_lattice)
            [accepted, accepted_prob] = grid(h, j, padded_new_lattice, r, c,
                                             t_tilde)
            if accepted:
                lattice = new_lattice

        lattice_full[:, :, x] = lattice

    lattice_full = delete(lattice_full, range(0, n_configs - n_usable_configs),
                          2)
    return lattice_full
Example #18
0
def main():
    l = 15
    h = 0.1
    j = 1
    n_configs = 250
    n_usable_configs = 200
    temps = numpy.linspace(10,.5,20)
    energy = numpy.zeros((len(temps), 1))
    initial_energy = numpy.ones((l, l))
    last_lattice = initial_energy
    
    for iTemp, temp in enumerate(temps):
        print(temp)
        lattice = create_lattice(l, h, j, last_lattice, n_configs, n_usable_configs, temp)
        lattice_pad = numpy.zeros((l+2, l+2, n_usable_configs))

        for config in range(n_usable_configs):
            lattice_pad[:,:,config] = pad(lattice[:,:,config])
        
        end_x = lattice_pad.shape[0]-1
        end_y = lattice_pad.shape[1]-1
        north = lattice_pad[0:end_x-1,1:end_y,:]
        south = lattice_pad[2:end_x+1,1:end_y,:]
        east = lattice_pad[1:end_x,2:end_y+1,:]
        west = lattice_pad[1:end_x,0:end_y-1,:]
        
        f = -1 * h * numpy.sum(lattice)
        s = -1 * j/2 * numpy.sum(lattice * 
            (north + south + east + west))
        energy_temp = (f+s)/n_usable_configs
        energy[iTemp] = energy_temp
        print("energy for ", temp, " = ", energy_temp)
        last_lattice = lattice[:,:,-1]
    print(energy)
    plotly.offline.plot({
        "data": [ Scatter(x=temps, y=energy, mode="markers") ],
        "layout": Layout(title="Energy Consumption")
        }, filename="q1.html")
Example #19
0
def main(onFile, nnFile, wDir):

    bDir = wDir + "~"

    if os.path.exists(bDir):
        if input("delete {0} (y/n)? ".format(bDir) ).lower() in ["1", "y"]:
            shutil.rmtree(bDir)
        else:
            errorAndExit("{0} already exists".format(bDir) )

    shutil.copytree(wDir, bDir)

    on = open(onFile)
    nn = open(nnFile)

    oldNames = []
    newNames = []
    oldNamesL = []
    newNamesL = []

    fileDict = collections.defaultdict(lambda: [False, ""])

    try:
        walker = pathwalker.PathWalker(wDir)
    except pathwalker.NotDirError:
        usageAndExit("directory doesn't exist")

    for old, new in zip(on, nn):

        if old:
            oldNames.append(old.rstrip("\n").strip('"').lower())
            oldNamesL.append(old.rstrip("\n").strip('"').lower())

        if new:
            newNames.append(new.rstrip("\n").strip('"'))
            newNamesL.append(new.rstrip("\n").strip('"').lower())

    if len(oldNames) != len(newNames):
        raise ValueError("old name count and new name count MUST be the same")

    nameTrans  = dict(zip(oldNames,  newNames)  )
    nameTransL = dict(zip(oldNamesL, newNamesL) )

    for name in nameTrans:
        pName  = pad(name, padChar='"')
        pName2 = pad(nameTrans[name], padChar='"')
        print("{0:<20} -> {1}".format(pName, pName2) )

    txtFiles = walker.walk(isText, abs=True)

    txtFiles = txtFiles.flattenFiles()

    for txt in txtFiles:

        cFile = open(txt)

        for line in cFile:

            testLine = line.rstrip("\n").lower()
            line = line.rstrip("\n")

            for n in nameTransL:

                fName = "\"{0}\"".format(nameTrans[n])


                for s in checkStrs:

                    s2 = s.format(n)
                    s3 = s.format(nameTrans[n])
                    l = len(s2)

                    if s2 in testLine:

                        print(testLine)

                        p = testLine.find(s2)
                        line = line[:p] + s3 + line[p+l:]

                        print(line)
                        fileDict[txt][0] = True
                        break

                for s in checkStrs2:

                    s2 = s.format(n)
                    s3 = s.format(fName)
                    l = len(s2)

                    if s2 in testLine:

                        print(testLine)


                        p = testLine.find(s2)
                        line = line[:p] + s3 + line[p+l:]

                        print(line)
                        fileDict[txt][0] = True
                        break

            fileDict[txt][1] = "".join([fileDict[txt][1], line + "\n"])

    fileDict[txt][1] += "\n"

    for txt in sorted(fileDict):

        if fileDict[txt][0]:
            cFile = open(txt, "w")
            cFile.write(fileDict[txt][1])
            cFile.flush()
            cFile.close()
Example #20
0
        opt.dataroot)

    if not opt.skip_generate_images:
        print("Generating images")
        os.system('java -jar pdffigures2.jar -e -q -a Table -m ' + png_folder +
                  '/ -d ' + json_folder + '/ ' + pdf_folder + '/')
        print("Padding images")
        for image in tqdm(os.listdir(png_folder)):
            img = np.asarray(imageio.imread(os.path.join(png_folder, image),
                                            pilmode='RGB'),
                             dtype=np.uint8)
            if img.shape[0] > 1024 or img.shape[1] > 1024:
                os.remove(os.path.join(png_folder, image))
                remove_from_json(json_folder, image)
                continue
            img = pad.pad(img, (1024, 1024, 3))
            imageio.imwrite(os.path.join(png_folder, image), img)

    if not opt.skip_predict:
        print("Predicting outlines")
        if opt.model == 'pix2pixHD':
            subprocess.call([
                'python', './pix2pixHD/predict.py', '--name', 'gen-tables',
                '--checkpoints_dir', opt.checkpoint_dir, '--dataroot',
                opt.dataroot, '--loadSize', '1024', '--fineSize', '1024',
                '--no_instance', '--label_nc', '0', '--results_dir',
                outlines_folder, '--mode', opt.mode, '--which_epoch', opt.epoch
            ])
        elif opt.model == 'encoder-decoder-skip':
            subprocess.call([
                'python', './segmentation/bulk_predict.py', '--input_folder',
                with open(
                        os.path.join(tex_path, path[1], str(idx)) + '-' +
                        str(i) + '.tex', 'w+') as tex_file:
                    tex_file.write(outline_tex)
                subprocess.call(
                    'latex -interaction=batchmode -output-directory=' +
                    os.path.join(png_path, path[1]) + ' ' +
                    os.path.join(tex_path, path[1],
                                 str(idx) + '-' + str(i)) + '.tex',
                    shell=True,
                    stdout=open(os.devnull, 'wb'))
                subprocess.call('dvipng -q* -T tight -o ' +
                                os.path.join(png_path, path[1],
                                             str(idx) + '-' + str(i)) +
                                '.png ' +
                                os.path.join(png_path, path[1],
                                             str(idx) + '-' + str(i)) + '.dvi',
                                shell=True,
                                stdout=open(os.devnull, 'wb'))

                cleanup(os.path.join(png_path, path[0]))
                cleanup(os.path.join(png_path, path[1]))

                if apply_padding:
                    pad(
                        os.path.join(png_path, path[0],
                                     str(idx) + '-' + str(i)) + '.png ', 1024)
                    pad(
                        os.path.join(png_path, path[1],
                                     str(idx) + '-' + str(i)) + '.png ', 1024)
Example #22
0
def run():
    os.system('hostname -I > output.txt'
              )  # these 6 commands reads ip address from linux OS
    ip_file = open('output.txt', 'r')
    n_ip = ip_file.readline()
    nodeIP = n_ip.rstrip(' \n')
    os.remove('output.txt')
    pad_value = 1
    mcast_recv1.kill = False
    n = e.cycle5(int(nodeIP[-1]) + 1)
    e.node = n
    recv_nodes = e.node_tracker()
    recv_stages = (1, 1, 2)
    threads = {}
    cache_q = queue.Queue(
    )  # use of the fifo queue structure ensures safe exchange of data between threads
    k = 1  # global counter
    for i in range(3):
        # create and start separate threads to receive from different nodes (node, stage, q, priority):
        threads[i] = threading.Thread(target=mcast_recv1.m_recv,
                                      args=(recv_nodes[i], recv_stages[i],
                                            cache_q, i + 1),
                                      daemon=False)
        threads[i].start()
        print('starting thread ', i + 1)

    while True:  # main running loop

        print('I am ', nodeIP)
        print('Waiting for Agg')
        # listen for info from aggregator.
        # determine node number (n) and total number of nodes (Num) from agg Xmission
        # N is list of node
        # win,tau,k,d,N,aggIP = node_server.server(node_ip)
        while True:
            info = node_server.server(nodeIP)
            if info == 'resend':
                print('## RESENDING PROTOCOL ##')
                s1_data = pad.pad(e.stage1_send(), pad_value)
                s2_data = pad.pad(e.stage2_send(), pad_value)
                mcast_send.send(n, 1, s1_data)  # send stage1 partition
                mcast_send.send(n, 2, s2_data)  # send stage2 partition
            else:
                break
        if info == 'restart':
            print('***Restarting Program***')
            break
        # n = info.node_dict[nodeIP] + 1  # node number (1,2,3,4,5)
        print('My node number is ', n)
        Num = len(info.node_dict)  # total number of nodes (should be 5)
        d = info.d  # number data points/node
        tau = info.tau  # number of local iterations
        k = info.k  # global iteration number
        K = info.K  # total global iterations
        pad_value = info.pad_value  # padding value of data for testing

        if k == 0:
            print('beginning session')
            e.set_flag = False
            mcast_recv1.prev = 0

        # we are starting over, we need to empty out the queue
        if not cache_q.empty():  #
            for i in range(cache_q.qsize()):
                cache_q.get()
        host = info.host  # aggregator IP
        shuff = info.shuff  # number of shuffles per global iteration

        # these can be pulled from the agg if desired
        weight = 1
        eta = .01
        lam = 1

        # pull data_pts for global update
        w = info.w
        D = d * Num  # Total data points
        filepath = r"train.csv"

        # if we didn't set the encoder, then set it here
        if not e.set_flag:
            e.create_workspace(n, filepath,
                               D)  # initialization of the workspace file
        shuffcount = 1  # compare to iterations
        # this variable keeps track of which data part is needed next for receive, there are 3 needed for each recv
        cpart = 1
        time_init = time.time()
        ans_data = [(0, 'no data'), (0, 'no data'), (0, 'no data')]
        # wait for other nodes to start receiving
        # time.sleep(0.1*(5-n))

        # this is the main running loop
        while shuffcount <= shuff:  # main coded shuffling running loop (for shuff iterations)
            s1_data = pad.pad(e.stage1_send(), pad_value)
            s2_data = pad.pad(e.stage2_send(), pad_value)
            mcast_send.send(n, 1, s1_data)  # send stage1 partition
            mcast_send.send(n, 2, s2_data)  # send stage2 partition
            data_pts = e.get_work_file(
            )  # data points for current iteration to use for training
            print('training')
            data_pts = data_pts.astype('float64')

            # Do the training time. If it gets hung up for too long, restart.
            w, fn = SVM.svm(w,
                            data_pts,
                            eta,
                            lam,
                            tau,
                            d,
                            weight,
                            shuff=0,
                            N=Num,
                            n=n - 1)  #train on tau iterations data_pts d
            try_time = time.time()

            # you need to receive all 3 parts from your queue. Loop until you have everything you need.
            while cpart < 4:
                cp = cpart  # need this to ensure fifo queue is completely cycled before searching for the next part
                for i in range(cache_q.qsize()
                               ):  # cycle through the queue for receive data
                    a = cache_q.get()  # tuple in form (part integer, DATA)
                    if a[0] == cp:  # if it is the part needed adds part to ans_data and increments to next part
                        cp = 'disabled'
                        ans_data[cpart - 1] = a
                        print('got part ', cpart)
                        cpart = cpart + 1
                    else:
                        cache_q.put(a)

                # Evaluate a timeout. If we timeout, break the loop.
                #if time.time() >= try_time + 5:  # resend if it doesnt find data after 5 seconds
                #  print("Resending, couldn't find part ", cpart)
                # break
            if time.time() >= (
                    time_init + 5 * 60
            ):  # times out 5*60s of not receiving all 3 pieces of data
                print("Error: threads timed out")
                exit(-1)

            # Nothing bad happened. We can use the data.
            if cpart >= 4:  # receive condition triggers when all data is ready
                print('data received')
                e.recv(pad.unpad(ans_data[0][1], int(d / 4)),
                       pad.unpad(ans_data[1][1], int(d / 4)),
                       pad.unpad(ans_data[2][1], int(d / 4)))
                cpart = 1
                time_init = time.time()
                shuffcount = shuffcount + 1
        # send w to agg
        print('sending cleanup data')
        s1_data = pad.pad(e.stage1_send(), pad_value)
        s2_data = pad.pad(e.stage2_send(), pad_value)
        mcast_send.send(n, 1, s1_data)  # send stage1 partition
        mcast_send.send(n, 2, s2_data)  # send stage2 partition
        node_client.client(w, fn, host)
        k = k + 1

    if not mcast_recv1.kill:  # kills threads by ending underlying function(s)
        mcast_recv1.kill = True
        print('Threads killed')
Example #23
0
    def forward(self, img, flow, mask, orisize):
        ## Confirm padding, make correct x
        #flow = concat backgound and foreground
        x = torch.cat((mask, flow), 1)
        x = pad(x,
                size=(x.shape[2], x.shape[3]),
                kernel_size=7,
                stride=2,
                dilation=1)
        x = self.conv1(x)
        x = nn.LeakyReLU(0.3)(x)

        x = pad(x,
                size=(x.shape[2], x.shape[3]),
                kernel_size=5,
                stride=2,
                dilation=1)
        x = self.conv2(x)
        x = nn.LeakyReLU(0.3)(x)

        x = pad(x,
                size=(x.shape[2], x.shape[3]),
                kernel_size=5,
                stride=2,
                dilation=1)
        x = self.conv3(x)
        x = nn.LeakyReLU(0.3)(x)

        x = pad(x,
                size=(x.shape[2], x.shape[3]),
                kernel_size=3,
                stride=1,
                dilation=1)
        x = self.conv4(x)
        x = nn.LeakyReLU(0.3)(x)

        x = pad(x,
                size=(x.shape[2], x.shape[3]),
                kernel_size=3,
                stride=2,
                dilation=1)
        x = self.conv5(x)
        x = nn.LeakyReLU(0.3)(x)

        x = pad(x,
                size=(x.shape[2], x.shape[3]),
                kernel_size=3,
                stride=1,
                dilation=1)
        x = self.conv6(x)
        x = nn.LeakyReLU(0.3)(x)

        x = pad(x,
                size=(x.shape[2], x.shape[3]),
                kernel_size=3,
                stride=2,
                dilation=1)
        x = self.conv7(x)
        x = nn.LeakyReLU(0.3)(x)

        x = pad(x,
                size=(x.shape[2], x.shape[3]),
                kernel_size=3,
                stride=1,
                dilation=1)
        x = self.conv8(x)
        x = nn.LeakyReLU(0.3)(x)

        x = pad(x,
                size=(x.shape[2], x.shape[3]),
                kernel_size=3,
                stride=2,
                dilation=1)
        x = self.conv9(x)
        x = nn.LeakyReLU(0.3)(x)
        ##############################################################################

        bconv1 = pad(img,
                     size=(img.shape[2], img.shape[3]),
                     kernel_size=7,
                     stride=2,
                     dilation=1)
        bconv1 = self.conv10(bconv1)
        bconv1 = nn.LeakyReLU(0.3)(bconv1)

        bconv2 = pad(bconv1,
                     size=(bconv1.shape[2], bconv1.shape[3]),
                     kernel_size=5,
                     stride=2,
                     dilation=1)
        bconv2 = self.conv11(bconv2)
        bconv2 = nn.LeakyReLU(0.3)(bconv2)

        bconv3 = pad(bconv2,
                     size=(bconv2.shape[2], bconv2.shape[3]),
                     kernel_size=5,
                     stride=2,
                     dilation=1)
        bconv3 = self.conv12(bconv3)
        bconv3 = nn.LeakyReLU(0.3)(bconv3)

        bconv31 = pad(bconv3,
                      size=(bconv3.shape[2], bconv3.shape[3]),
                      kernel_size=3,
                      stride=1,
                      dilation=1)
        bconv31 = self.conv13(bconv31)
        bconv31 = nn.LeakyReLU(0.3)(bconv31)

        bconv4 = pad(bconv31,
                     size=(bconv31.shape[2], bconv31.shape[3]),
                     kernel_size=3,
                     stride=2,
                     dilation=1)
        bconv4 = self.conv14(bconv4)
        bconv4 = nn.LeakyReLU(0.3)(bconv4)

        bconv41 = pad(bconv4,
                      size=(bconv4.shape[2], bconv4.shape[3]),
                      kernel_size=3,
                      stride=1,
                      dilation=1)
        bconv41 = self.conv15(bconv41)
        bconv41 = nn.LeakyReLU(0.3)(bconv41)

        bconv5 = pad(bconv41,
                     size=(bconv41.shape[2], bconv41.shape[3]),
                     kernel_size=3,
                     stride=2,
                     dilation=1)
        bconv5 = self.conv16(bconv5)
        bconv5 = nn.LeakyReLU(0.3)(bconv5)

        bconv51 = pad(bconv5,
                      size=(bconv5.shape[2], bconv5.shape[3]),
                      kernel_size=3,
                      stride=1,
                      dilation=1)
        bconv51 = self.conv17(bconv51)
        bconv51 = nn.LeakyReLU(0.3)(bconv51)

        bconv6 = pad(bconv51,
                     size=(bconv51.shape[2], bconv51.shape[3]),
                     kernel_size=3,
                     stride=2,
                     dilation=1)
        bconv6 = self.conv18(bconv6)
        bconv6 = nn.LeakyReLU(0.3)(bconv6)
        ###############################################################################

        conv6 = torch.cat((x, bconv6), 1)
        deconv5 = torch.nn.Upsample(
            (bconv51.shape[2], bconv51.shape[3]))(conv6)
        deconv5 = pad(deconv5,
                      size=(deconv5.shape[2], deconv5.shape[3]),
                      kernel_size=4,
                      stride=1,
                      dilation=1)
        deconv5 = self.conv19(deconv5)
        deconv5 = nn.LeakyReLU(0.3)(deconv5)
        concat5 = torch.cat((deconv5, bconv51), 1)

        flow5 = pad(concat5,
                    size=(concat5.shape[2], concat5.shape[3]),
                    kernel_size=3,
                    stride=1,
                    dilation=1)
        flow5 = self.conv20(flow5)
        m = nn.Identity(54, unused_argument1=0.1, unused_argument2=False)
        flow5 = m(flow5)

        deconv4 = torch.nn.Upsample(
            (bconv41.shape[2], bconv41.shape[3]))(concat5)
        deconv4 = pad(deconv4,
                      size=(deconv4.shape[2], deconv4.shape[3]),
                      kernel_size=4,
                      stride=1,
                      dilation=1)
        deconv4 = self.conv21(deconv4)
        deconv4 = nn.LeakyReLU(0.3)(deconv4)
        upflow4 = torch.nn.Upsample(
            (bconv41.shape[2], bconv41.shape[3]))(flow5)
        upflow4 = pad(upflow4,
                      size=(upflow4.shape[2], upflow4.shape[3]),
                      kernel_size=4,
                      stride=1,
                      dilation=1)
        upflow4 = self.conv22(upflow4)
        m = nn.Identity(54, unused_argument1=0.1, unused_argument2=False)
        upflow4 = m(upflow4)
        concat4 = torch.cat((deconv4, bconv41, upflow4), 1)

        flow4 = pad(concat4,
                    size=(concat4.shape[2], concat4.shape[3]),
                    kernel_size=3,
                    stride=1,
                    dilation=1)
        flow4 = self.conv23(flow4)
        m = nn.Identity(54, unused_argument1=0.1, unused_argument2=False)
        flow4 = m(flow4)

        deconv3 = torch.nn.Upsample(
            (bconv31.shape[2], bconv31.shape[3]))(concat4)
        deconv3 = pad(deconv3,
                      size=(deconv3.shape[2], deconv3.shape[3]),
                      kernel_size=4,
                      stride=1,
                      dilation=1)
        deconv3 = self.conv24(deconv3)
        deconv3 = nn.LeakyReLU(0.3)(deconv3)
        upflow3 = torch.nn.Upsample(
            (bconv31.shape[2], bconv31.shape[3]))(flow4)
        upflow3 = pad(upflow3,
                      size=(upflow3.shape[2], upflow3.shape[3]),
                      kernel_size=4,
                      stride=1,
                      dilation=1)
        upflow3 = self.conv25(upflow3)
        m = nn.Identity(54, unused_argument1=0.1, unused_argument2=False)
        upflow3 = m(upflow3)
        concat3 = torch.cat((deconv3, bconv31, upflow3), 1)

        flow3 = pad(concat3,
                    size=(concat3.shape[2], concat3.shape[3]),
                    kernel_size=3,
                    stride=1,
                    dilation=1)
        flow3 = self.conv26(flow3)
        m = nn.Identity(54, unused_argument1=0.1, unused_argument2=False)
        flow3 = m(flow3)

        deconv2 = torch.nn.Upsample(
            (bconv2.shape[2], bconv2.shape[3]))(concat3)
        deconv2 = pad(deconv2,
                      size=(deconv2.shape[2], deconv2.shape[3]),
                      kernel_size=4,
                      stride=1,
                      dilation=1)
        deconv2 = self.conv27(deconv2)
        deconv2 = nn.LeakyReLU(0.3)(deconv2)
        upflow2 = torch.nn.Upsample((bconv2.shape[2], bconv2.shape[3]))(flow3)
        upflow2 = pad(upflow2,
                      size=(upflow2.shape[2], upflow2.shape[3]),
                      kernel_size=4,
                      stride=1,
                      dilation=1)
        upflow2 = self.conv28(upflow2)
        m = nn.Identity(54, unused_argument1=0.1, unused_argument2=False)
        upflow2 = m(upflow2)
        concat2 = torch.cat((deconv2, bconv2, upflow2), 1)

        flow2 = pad(concat2,
                    size=(concat2.shape[2], concat2.shape[3]),
                    kernel_size=3,
                    stride=1,
                    dilation=1)
        flow2 = self.conv29(flow2)
        m = nn.Identity(54, unused_argument1=0.1, unused_argument2=False)
        flow2 = m(flow2)

        deconv1 = torch.nn.Upsample(
            (bconv1.shape[2], bconv1.shape[3]))(concat2)
        deconv1 = pad(deconv1,
                      size=(deconv1.shape[2], deconv1.shape[3]),
                      kernel_size=4,
                      stride=1,
                      dilation=1)
        deconv1 = self.conv30(deconv1)
        deconv1 = nn.LeakyReLU(0.3)(deconv1)
        upflow1 = torch.nn.Upsample((bconv1.shape[2], bconv1.shape[3]))(flow2)
        upflow1 = pad(upflow1,
                      size=(upflow1.shape[2], upflow1.shape[3]),
                      kernel_size=4,
                      stride=1,
                      dilation=1)
        upflow1 = self.conv31(upflow1)
        m = nn.Identity(54, unused_argument1=0.1, unused_argument2=False)
        upflow1 = m(upflow1)
        concat1 = torch.cat((deconv1, bconv1, upflow1), 1)

        flow1 = pad(concat1,
                    size=(concat1.shape[2], concat1.shape[3]),
                    kernel_size=5,
                    stride=1,
                    dilation=1)
        flow1 = self.conv32(flow1)
        m = nn.Identity(54, unused_argument1=0.1, unused_argument2=False)
        flow1 = m(flow1)

        flow0 = torch.nn.Upsample((orisize[0], orisize[1]))(flow1)

        return flow0
Example #24
0
from pad import pad

t = input("Enter temperature for PAD : ")
file = input("Plot filename to be saved as : ")

pad(float(t), filename=file)