R函数名称似乎在脚本执行中途随机变化

时间:2016-03-17 16:19:50

标签: r ubuntu igraph

我遇到一个问题,一个函数名称似乎在执行一个长R脚本的过程中发生了变化......或者更确切地说,R似乎试图调用一个显然不存在的函数,而不是在我的代码中提到。这是我得到的错误:

Error: could not find function "get.shohortest.paths"
Execution halted

显然,你会想,这是一个愚蠢的错字,我肯定打算称之为'get.shortest.paths'。嗯,是的,那是我打算调用的函数,它是我在脚本中调用的函数。奇怪的是,脚本成功地通过几十次调用正确的函数,然后通过看似随机错误并尝试调用一个不存在的函数来抛出错误。 有趣的是,在我的脚本中大致相同的点上存在问题的另一种排列,但使用不同的数据集,其中错误略有不同(我删除了日志,所以我没有完全相同),如

Error: could not find function "get.srtest.paths"
Execution halted

在较小的测试数据集上,问题未呈现,执行已成功完成。

我的代码如下。我一直试图在远程Ubuntu机器上运行它,使用Rbatch在后台运行脚本,同时将输出转储到文本文件中。

nohup Rscript script_name.r > output.out 2>&1 &

我的图形(变量'g',igraph图形对象)具有200k到300k的边缘。本质上,我只是想找到不是自己节点之间最短路径的边。

##############################
# FIND SHORTEST PATHS
##############################

#create a field for storing whether this edge is part of 
# a shortest path. Default to False
E(g)$in_shortest = F

# run the process separately for each connected component 
# if there are more than one
for(comp in unique(E(g)$component)){

    print(paste('finding shortest paths for component',comp))

    # find the vertices of this component
    component_vertices = V(g)[inc(E(g)[E(g)$component==comp])]

    direct_edges = c()

    # Find some random shortest paths within each component
    for(i in 1:25){
        paths = get.shortest.paths(
            g,
            from=sample(component_vertices,1),
            # from random node to a sample of nodes
            to=sample(
                component_vertices,
                round(length(component_vertices)*0.015),
                replace=F
            ),
            output='epath'
        )
        # mark the paths in the graph which are definitely shortest
        for(path in paths$epath){
            direct_edges = union( direct_edges, as.vector(path) )
        }
        # how much did this iteration add?
        print(paste( 'known direct', length(direct_edges)/length(E(g)),'on i',i ))
    }
    # store the data in the graph
    E(g)[direct_edges]$in_shortest = T
}


print('measuring directness directly')
# default directness is 1
E(g)$directness = 1

# now iterate over those edges which are not yet known to be the shortest possible
check = E(g)[ !E(g)$in_shortest ]

for( i in 1:length(check) ){
    # occassional output
    if(i%%30==0){ print( length(check) - i) }

    edge = check[[i]]
    # get the nodes at either end of the edge
    inc_nodes = V(g)[ inc(edge) ]
    result = get.shortest.paths(
        g,
        inc_nodes[[1]],
        inc_nodes[[2]],
        output='epath'
    )
    # check to see if the path is identical to the edge
    if( length(result$epath[[1]])!=1 ){
        # edge is NOT shortest. 
        # assign directness value
        shortest_path = sum(result$epath[[1]]$weight)
        ratio = shortest_path / edge$weight
        E(g)[ as.vector(edge) ]$directness = ratio

    }else if( edge == result$epath[[1]] ){
        E(g)[ as.vector(edge) ]$in_shortest = T

    }else{
        # edge is NOT shortest. 
        # assign directness value
        shortest_path = result$epath[[1]]$weight
        ratio = shortest_path / edge$weight
        E(g)[ as.vector(edge) ]$directness = ratio
    }
}

0 个答案:

没有答案