Best Python code snippet using ATX
Bidirection.py
Source:Bidirection.py
1import torch.nn as nn2from Attention import AttentionModel,AttentionModel_without_Residual,AttentionModel_for_single_side_attention3import torch4from Models import GC_Block, PostGCN5from torch.nn.parameter import Parameter6class MergeNet(nn.Module):7 def __init__(self, seq_len, pose_dim):8 super(MergeNet, self).__init__()9 self.block1 = GC_Block(pose_dim, p_dropout=0.3, bias=True, node_n=seq_len)10 self.block2 = GC_Block(pose_dim, p_dropout=0.3, bias=True, node_n=seq_len)11 self.block3 = GC_Block(pose_dim, p_dropout=0.3, bias=True, node_n=seq_len)12 self.post_net = PostGCN(pose_dim, 66, node_n=seq_len)13 #self.lin1 = nn.Linear(pose_dim, 256)14 self.lin1 = nn.Linear(66, 66)15 self.dp1 = nn.Dropout(0.3)16 self.act1 = nn.LeakyReLU(0.2)17 # self.lin2 = nn.Linear(256, 66)18 # self.dp2 = nn.Dropout(0.3)19 # self.act2 = nn.LeakyReLU(0.2)20 self.W1 = Parameter(torch.FloatTensor(25, 1))21 self.W2 = Parameter(torch.FloatTensor(25, 1))22 self.reset_parameters()23 def reset_parameters(self):24 self.W1.data.fill_(0.5);25 self.W2.data.fill_(0.5);26 def forward(self, x, y, for_resid):27 # x = torch.mul(x,self.W1)28 # y = torch.mul(y,self.W2)29 xy = torch.add(x, y)30 xy = xy/231 #xy = torch.cat((x, y), dim=-1)32 # xy = self.lin1(xy)33 # xy = self.dp1(xy)34 #xy = self.act1(xy)35 # xy = self.lin2(xy)36 # xy = self.dp2(xy)37 # xy = self.act2(xy)38 # xy = torch.cat((x, y), dim=-1)39 # xy = self.block1(xy)40 # xy = self.block2(xy)41 # xy = self.block3(xy)42 # xy = self.post_net(xy)43 #xy = xy + for_resid44 return xy45class MergeNet_without_Residual(nn.Module):46 def __init__(self, seq_len, pose_dim):47 super(MergeNet_without_Residual, self).__init__()48 self.block1 = GC_Block(pose_dim, p_dropout=0.3, bias=True, node_n=seq_len)49 self.block2 = GC_Block(pose_dim, p_dropout=0.3, bias=True, node_n=seq_len)50 self.block3 = GC_Block(pose_dim, p_dropout=0.3, bias=True, node_n=seq_len)51 self.post_net = PostGCN(pose_dim, 66, node_n=seq_len)52 def forward(self, x, y, for_resid):53 # xy = torch.add(x, y)54 # xy = xy/255 xy = torch.cat((x, y), dim=-1)56 xy = self.block1(xy)57 xy = self.block2(xy)58 xy = self.block3(xy)59 xy = self.post_net(xy)60 # xy = xy + for_resid61 return xy62class BidirectionModel(nn.Module):63 def __init__(self):64 super(BidirectionModel, self).__init__()65 self.model = AttentionModel()66 self.inverse_model = AttentionModel()67 self.merge = MergeNet(25, 66 * 2)68 def forward(self, x, z, for_resid, invx, invz, inv_for_resid):69 out = self.model(x, z, for_resid)70 inv_out = self.inverse_model(invz, invx, inv_for_resid)71 inv_inv_out = inv_out.flip(1)72 ret = self.merge(out, inv_inv_out, for_resid)73 return out, inv_out, ret74class BidirectionModel_for_single_side_attention(nn.Module):75 def __init__(self):76 super(BidirectionModel_for_single_side_attention, self).__init__()77 self.model = AttentionModel_for_single_side_attention()78 self.inverse_model = AttentionModel_for_single_side_attention()79 self.merge = MergeNet(25, 66 * 2)80 def forward(self, x, z, for_resid, invx, invz, inv_for_resid):81 out = self.model(x, z, for_resid)82 inv_out = self.inverse_model(invz, invx, inv_for_resid)83 inv_inv_out = inv_out.flip(1)84 ret = self.merge(out, inv_inv_out, for_resid)85 return out, inv_out, ret86class BidirectionModel_without_Residual(nn.Module):87 def __init__(self):88 super(BidirectionModel_without_Residual, self).__init__()89 self.model = AttentionModel_without_Residual()90 self.inverse_model = AttentionModel_without_Residual()91 self.merge = MergeNet_without_Residual(25, 66 * 2)92 def forward(self, x, z, for_resid, invx, invz):93 out = self.model(x, z,)94 inv_out = self.inverse_model(invz, invx)95 inv_inv_out = inv_out.flip(1)96 ret = self.merge(out, inv_inv_out, for_resid)...
test_pairs.py
Source:test_pairs.py
...3import networkx as nx4from base_test import BaseTestAttributeMixing,BaseTestDegreeMixing5class TestAttributeMixingXY(BaseTestAttributeMixing):6 def test_node_attribute_xy_undirected(self):7 attrxy=sorted(nx.node_attribute_xy(self.G,'fish'))8 attrxy_result=sorted([('one','one'),9 ('one','one'),10 ('two','two'),11 ('two','two'),12 ('one','red'),13 ('red','one'),14 ('blue','two'),15 ('two','blue')16 ])17 assert_equal(attrxy,attrxy_result)18 def test_node_attribute_xy_undirected_nodes(self):19 attrxy=sorted(nx.node_attribute_xy(self.G,'fish',20 nodes=['one','yellow']))21 attrxy_result=sorted( [22 ])23 assert_equal(attrxy,attrxy_result)24 def test_node_attribute_xy_directed(self):25 attrxy=sorted(nx.node_attribute_xy(self.D,'fish'))26 attrxy_result=sorted([('one','one'),27 ('two','two'),28 ('one','red'),29 ('two','blue')30 ])31 assert_equal(attrxy,attrxy_result)32 def test_node_attribute_xy_multigraph(self):33 attrxy=sorted(nx.node_attribute_xy(self.M,'fish'))34 attrxy_result=[('one','one'),35 ('one','one'),36 ('one','one'),37 ('one','one'),38 ('two','two'),39 ('two','two')40 ]41 assert_equal(attrxy,attrxy_result)42 def test_node_attribute_xy_selfloop(self):43 attrxy=sorted(nx.node_attribute_xy(self.S,'fish'))44 attrxy_result=[('one','one'),45 ('two','two')46 ]47 assert_equal(attrxy,attrxy_result)48class TestDegreeMixingXY(BaseTestDegreeMixing):49 def test_node_degree_xy_undirected(self):50 xy=sorted(nx.node_degree_xy(self.P4))51 xy_result=sorted([(1,2),52 (2,1),53 (2,2),54 (2,2),55 (1,2),56 (2,1)])57 assert_equal(xy,xy_result)58 def test_node_degree_xy_undirected_nodes(self):59 xy=sorted(nx.node_degree_xy(self.P4,nodes=[0,1,-1]))60 xy_result=sorted([(1,2),61 (2,1),])62 assert_equal(xy,xy_result)63 def test_node_degree_xy_directed(self):64 xy=sorted(nx.node_degree_xy(self.D))65 xy_result=sorted([(2,1),66 (2,3),67 (1,3),68 (1,3)])69 assert_equal(xy,xy_result)70 def test_node_degree_xy_multigraph(self):71 xy=sorted(nx.node_degree_xy(self.M))72 xy_result=sorted([(2,3),73 (2,3),74 (3,2),75 (3,2),76 (2,3),77 (3,2),78 (1,2),79 (2,1)])80 assert_equal(xy,xy_result)81 def test_node_degree_xy_selfloop(self):82 xy=sorted(nx.node_degree_xy(self.S))83 xy_result=sorted([(2,2),84 (2,2)])85 assert_equal(xy,xy_result)86 def test_node_degree_xy_weighted(self):87 G = nx.Graph()88 G.add_edge(1,2,weight=7)89 G.add_edge(2,3,weight=10)90 xy=sorted(nx.node_degree_xy(G,weight='weight'))91 xy_result=sorted([(7,17),92 (17,10),93 (17,7),94 (10,17)])...
convert-rgb-space-xyz.py
Source:convert-rgb-space-xyz.py
1#!/usr/bin/env python2#3# This program allows to generate matrices to convert between RGB spaces and XYZ4# All hardcoded values are directly taken from the ITU-R documents5#6# NOTE: When trying to convert from one space to another, make sure the whitepoint is the same,7# otherwise math gets more complicated (see Bradford transform).8#9# See also:10# http://www.brucelindbloom.com/index.html?Eqn_RGB_XYZ_Matrix.html11# https://ninedegreesbelow.com/photography/xyz-rgb.html12import numpy13def xy_to_XYZ(xy):14 return [xy[0] / xy[1], 1.0, (1.0 - xy[0] - xy[1]) / xy[1]]15def compute_rgb_to_zyx_matrix(whitepoint_XYZ, R, G, B):16 Xr = R[0] / R[1]17 Yr = 118 Zr = (1 - R[0] - R[1]) / R[1]19 Xg = G[0] / G[1]20 Yg = 121 Zg = (1 - G[0] - G[1]) / G[1]22 Xb = B[0] / B[1]23 Yb = 124 Zb = (1 - B[0] - B[1]) / B[1]25 m = numpy.array([26 [Xr, Xg, Xb],27 [Yr, Yg, Yb],28 [Zr, Zg, Zb]])29 m_inverse = numpy.linalg.inv(m)30 S = numpy.dot(m_inverse, whitepoint_XYZ)31 m = numpy.array([32 [S[0] * Xr, S[1] * Xg, S[2] * Xb],33 [S[0] * Yr, S[1] * Yg, S[2] * Yb],34 [S[0] * Zr, S[1] * Zg, S[2] * Zb]])35 return m36def d65_XYZ():37 d65_xy = [0.3127, 0.3290]38 return xy_to_XYZ(d65_xy)39def compute_srgb_to_xyz():40 R_xy = [0.640, 0.330]41 G_xy = [0.300, 0.600]42 B_xy = [0.150, 0.060]43 return compute_rgb_to_zyx_matrix(d65_XYZ(), R_xy, G_xy, B_xy)44def compute_rec709_rgb_to_xyz():45 R_xy = [0.640, 0.330]46 G_xy = [0.300, 0.600]47 B_xy = [0.150, 0.060]48 return compute_rgb_to_zyx_matrix(d65_XYZ(), R_xy, G_xy, B_xy)49def compute_rec2020_rgb_to_xyz():50 R_xy = [0.708, 0.292]51 G_xy = [0.170, 0.797]52 B_xy = [0.131, 0.046]53 return compute_rgb_to_zyx_matrix(d65_XYZ(), R_xy, G_xy, B_xy)54def compute_display_p3_rgb_to_xyz():55 R_xy = [0.680, 0.320]56 G_xy = [0.265, 0.690]57 B_xy = [0.150, 0.060]58 return compute_rgb_to_zyx_matrix(d65_XYZ(), R_xy, G_xy, B_xy)59def compute_full_transform(A_to_XYZ, B_to_XYZ):60 XYZ_to_B = numpy.linalg.inv(B_to_XYZ)61 A_to_B = numpy.matmul(XYZ_to_B, A_to_XYZ)62 print(f'M\n{A_to_B}')63 B_to_A = numpy.linalg.inv(A_to_B)64 print(f'M-1\n{B_to_A}')65if __name__ == '__main__':66 numpy.set_printoptions(precision = 10, suppress = True, floatmode = 'fixed')67 rgb_xyz = compute_srgb_to_xyz()68 xyz_rgb = numpy.linalg.inv(rgb_xyz)69 print(f'M\n{xyz_rgb}')...
5.1.py
Source:5.1.py
1# Import the data2f = open("5_data.txt")3data = []4# Process the data to a valid input array5for index, line in enumerate(f):6 # Split on the arrow part, the first part is from, second to.7 processed = line.rstrip('\n').split(' -> ')8 data.append(processed)9 # For debug purposes, limit the for loop.10 # if index > 10:11 # break12grid = {}13for item in data:14 # Process the from.15 # @todo Changes map() the order?16 xyFrom = list(map(lambda x: int(x), item[0].split(',')))17 xyTo = list(map(lambda x: int(x), item[1].split(',')))18 # Only process the items where x/y from and to are equal.19 if (xyFrom[0] == xyTo[0] or xyFrom[1] == xyTo[1]):20 # Do something with the data.21 if xyFrom[0] == xyTo[0]:22 startY = xyFrom[1] if xyFrom[1] < xyTo[1] else xyTo[1]23 endY = xyFrom[1] if xyFrom[1] > xyTo[1] else xyTo[1]24 # The y changes, while loop until values matches.25 while startY <= endY:26 i = ','.join([str(xyFrom[0]), str(startY)])27 if i not in grid:28 grid[i] = 029 grid[i] += 130 startY += 131 if xyFrom[1] == xyTo[1]:32 startX = xyFrom[0] if xyFrom[0] < xyTo[0] else xyTo[0]33 endX = xyFrom[0] if xyFrom[0] > xyTo[0] else xyTo[0]34 # The y changes, while loop until values matches.35 while startX <= endX:36 i = ','.join([str(startX), str(xyFrom[1])])37 if i not in grid:38 grid[i] = 039 grid[i] += 140 startX += 141# Filter out where the count is 142test = dict(filter(lambda x: x[1] > 1, grid.items()))43print('answer: ', len(test))44# 8022 (to low)...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!