How to visualize a torch_geometric graph in Python? - graph

Let's consider as an example that I have the following adjacence matrix in coordinate format:
> edge_index.numpy() = array([[ 0, 1, 0, 3, 2],
[ 1, 0, 3, 2, 1]], dtype=int64)
which means that the node 0 is linked toward the node 1, and vice-versa, the node 0 is linked to 3 etc...
Do you know a way to draw this graph as in networkx with nx.draw() ?
Thank you.

import networkx as nx
edge_index = torch.tensor([[0, 1, 1, 2],
[1, 0, 2, 1]], dtype=torch.long)
x = torch.tensor([[-1], [0], [1]], dtype=torch.float)
data = torch_geometric.data.Data(x=x, edge_index=edge_index)
g = torch_geometric.utils.to_networkx(data, to_undirected=True)
nx.draw(g)

Related

R - How to run a GWAS analysis with no position data?

everyone!
I am trying to run a GWAS analysis in R on some very simple genetic data. It only contains the SNPs and one outcome variable (as well as an ID variable for each observation).
Everything I have found online includes chromosome and position data. I have that for the SNPs, but in a separate file. (My plan is to map the SNPs after the relevant ones have been selected).
How can I go about running a GWAS analysis on this data? Would I need to, or could I use another method to filter to only the most significant SNPs?
I tried this, but it didn't work, because my data is not a gData object.
# SNPs are in A/B notation, with 0 = AA, 1 = AB, and 2 = BB
library(statgenGWAS)
id <- c("person1", "person2", "person3", "person4", "person5", "person6", "person7", "person8", "person9", "person10")
snp1 <- c(0, 1, 2, 2, 1, 0, 0, 0, 1, 1)
snp2 <- c(2, 2, 2, 1, 1, 1, 0, 0, 0, 1)
snp3 <- c(0, 0, 2, 2, 0, 2, 1, 0, 2, 2)
diagnosis <- c(0, 1, 1, 0, 0, 1, 1, 0, 1, 1)
data <- as.data.frame(cbind(id, snp1, snp2, snp3, diagnosis))
gwas1a <- runSingleTraitGwas(gData = data,
traits = "diagnosis")
Any help here is appreciated.
Thank you!

variable binary is none - spread spectrum

so i want to make encoding using spread spectrum with audio. i was try using this web to do it, the website try to encoding with image. the string was input must be convert to binary. but i change input string to use bytes (the result from encrypt using AES). i was using python and the result from print bi it's none, why is it none?
here's the code i tried:
password = b'\xbb\xbaS`'
def password(password):
print(password)
g = password
le=len(g)
i=0
a=list()
for i in range(le):
a.append(0) #initializing
j=0
for i in g:
a[j]=i
j+=1
print(a) #ascii values
bi= [ [ 0 for i in range(8) ] for j in range(le) ]
j=0
i=7
while(j<le):
while(i>-1):
bi[j][i]=a[j]%2
a[j]=(a[j]-bi[j][i])//2
i-=1
j+=1
i=7
print(bi)#binary
i=0
j=0
while(j<le):
while(i<8):
if bi[j][i]==0:
bi[j][i]=-1
i+=1
j+=1
i=0
i=0
j=0
print(bi)#this is where 0s are converted to -1s
and here's the terminal result:
[0]
[0, 0]
[0, 0, 0]
[0, 0, 0, 0]
[187, 186, 83, 96]
[[1, 0, 1, 1, 1, 0, 1, 1], [1, 0, 1, 1, 1, 0, 1, 0], [0, 1, 0, 1, 0, 0, 1, 1], [0, 1, 1, 0, 0, 0, 0, 0]]
[[1, -1, 1, 1, 1, -1, 1, 1], [1, -1, 1, 1, 1, -1, 1, -1], [-1, 1, -1, 1, -1, -1, 1, 1], [-1, 1, 1, -1, -1, -1, -1, -1]]
None

what is the difference and what does each of the functions exactly do? Why aren't they the same?

polyEval should represent a polynomial equation. For example, polyEval(x = 2, a = c(2, 3, 1)) is supposed to return 12, whereas polyEval(1, c(0, 1, 0, 1, 0, 1)) is supposed to return 3. This is the case for the first polyEval function, but why isn't it for the second? What exactly is the second one doing wrong? Because in my understanding, they should work the same.
polyEval <- function(x,a) {
n <- 1:length(a)
return(sum(c(a[n]*x^(n-1))))
}
polyEval <- function(x,a) {
sum(a*(x^(0:(length(a)))))
}
After correction, both raise x to the power 0, 1, 2, ..., length(a)-1 and then multiply the resulting vector by a and sum.
In the second one length(a) should be length(a)-1.
polyEval2 <- function(x,a) {
sum(a*(x^(0:(length(a)-1))))
}
polyEval2(2, c(2, 3, 1))
## [1] 12
polyEval2(1, c(0, 1, 0, 1, 0, 1))
## [1] 3

visualize different binary matrices with different in R

Assume I have two binary matrices in R program.
A = matrix(c(1, 1, 1, 1, 0, 0, 0, 0, 0), nrow=3, ncol=3, byrow = TRUE)
B = matrix(c(0, 0, 0, 0, 0, 1, 1, 1, 1), nrow=3, ncol=3, byrow = TRUE)
Here, A and B are adjacency matrices for two different social networks. How can I visualize the above two matrices in the same plot but with different colors?
The plot will be something like this: The x-axis and y-axis have three users.
and the scatter point of A and B will be plotted with different colors.
Here is a simple example using igraph. There are lots of layout options, so you'll probably want to read the various help pages for igraph, but hopefully this will get you started.
library(igraph)
A = matrix(c(1, 1, 1, 1, 0, 0, 0, 0, 0), nrow=3, ncol=3, byrow = TRUE)
B = matrix(c(0, 0, 0, 0, 0, 1, 1, 1, 1), nrow=3, ncol=3, byrow = TRUE)
#define a layout so that both sets of nodes overlap
lay <- layout_in_circle(graph_from_adjacency_matrix(A))
plot(graph_from_adjacency_matrix(A),layout=lay,edge.color="blue")
plot(graph_from_adjacency_matrix(B),layout=lay,edge.color="red",add = TRUE)

Getting indexes of a duplicate neighbour element in R vectors

I am trying to write a function which will have a numeric vector "x" on input and will create on output a numeric vector of xi indexes, such that x(i) == x(i+1)
By far I wrote such function neighbor:
neighbor <- function(l) {
stopifnot(is.numeric(l))
w <- sapply(l, function(x) which(l[x]==l[x+1]))
w
}
So executing this instruction:
neighbor(c(1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 0))
Should produce a numeric vector:
1, 4, 5, 7, 9
But I cannot get it working. Any ideas?
I am searching for an elegant solution without control-flow and if-else instructions.
diff will help with this:
x <- c(1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 0)
which(diff(x) == 0)

Resources