我正在尝试使用R包igraph
来映射不同位置的mesh网络。你知道吗
执行此操作的数据位于MySQL数据库中。我可以查询数据库,将数据写入csv文件,并通过让R毫无问题地读入csv文件来生成所需的图形。你知道吗
我的问题是,我现在需要脚本这个过程,以便它可以在任意为所有50个地点,我们有。以前,我手动更改值以生成图形。你知道吗
我正在尝试使用Python作为我的脚本语言来将所有内容绑定在一起。你知道吗
下面是我的Python脚本的相关部分:
import mysql.connector
import csv
import subprocess
localConnection = mysql.connector.connect(xxxx)
....CODE BLOCK DEFINING SQL QUERIES OMITTED....
facilityCursor = localConnection.cursor()
wLinksCursor = localConnection.cursor()
wNodesCursor = localConnection.cursor()
gatewayCursor = localConnection.cursor()
facilityCursor.execute(facilityQuery)
facility = facilityCursor.fetchone()
while facility is not None:
print facility[0]
wLinksCursor.execute(weightedLinksQuery % facility[0])
wLinks = wLinksCursor.fetchall()
linkWriter = csv.writer(open('/tmp/weightedLinks.csv', 'w'))
for row in wLinks:
linkWriter.writerow(row)
wNodesCursor.execute(weightedNodesQuery % facility[0])
wNodes = wNodesCursor.fetchall()
nodeWriter = csv.writer(open('/tmp/weightedNodes.csv', 'w'))
for row in wNodes:
nodeWriter.writerow(row)
gatewayCursor.execute(determineGatewayQuery % facility[0])
gateways = gatewayCursor.fetchall()
gateWriter = csv.writer(open('/tmp/gatewayWriter.csv', 'w'))
for row in gateways:
gateWriter.writerow(row)
command = 'Rscript'
path2script = '/pathToScript/networkMapper2.R'
args = ['/tmp/weightedLinks.csv', '/tmp/weightedNodes.csv', '/tmp/gatewayWriter.csv', facility[0]]
cmd = [command, path2script] + args
subprocess.Popen(cmd)
facility = facilityCursor.fetchone()
现在调用的R脚本:
library(igraph)
wlinks <- read.csv(toString(args[1]), header=T)
wnodes <- read.csv(toString(args[2]), header=T)
gateways <- read.csv(toString(args[3]), header=T)
fac <- toString(args[4])
gateways <- as.vector(gateways[2:14,])
nodeList <- wnodes$address
func <- function(x) {x %in% gateways}
truthTable <- lapply(wnodes, func)
wnodes <- as.data.frame(cbind(wnodes,truthTable$address))
getColors <- function(x){if (x == TRUE) {x = 2} else {x = 1}}
wow <- as.matrix(lapply(wnodes$truthTable, getColors))
wnodes <- cbind(wnodes,wow)
net <- graph.data.frame(wlinks, wnodes, directed=T)
net <- simplify(net, remove.multiple = F, remove.loops = T)
E(net)$width <- E(net)$frequency/4
V(net)$size <- V(net)$frequency*0.04 + 1
V(net)$color <- gsub(TRUE, "blue", wnodes$wow)
g <- layout.fruchterman.reingold
l <- layout.kamada.kawai(net)
graphTitle <- paste("Network Visualization For ", fac, "On ", Sys.Date())
graphFileName <- paste("/tmp/", fac, "_Network_Visualization_", Sys.Date())
png(file=graphFileName, width=1500, height=1500)
plot(net, edge.color="azure4", vertex.frame.color="gray80", edge.arrow.size = 2,
layout=g, vertex.label=wnodes$address, vertex.label.degree=0, vertex.label.dist=0.25,
vertex.label.font = 2, vertex.label.cex = 1.2, vertex.label.color = "black", frame=TRUE,
main=graphTitle)
dev.off()
最后,当我尝试运行Python脚本时遇到的错误是:
Loading required package: methods
Attaching package: ‘igraph’
The following objects are masked from ‘package:stats’:
decompose, spectrum
The following object is masked from ‘package:base’:
union
Error in args[1] : object of type 'closure' is not subsettable
Calls: read.csv -> read.table -> toString
Execution halted
目前没有回答
相关问题 更多 >
编程相关推荐