需要 OpenCPU 和 igraph 输出格式的帮助
Need help for OpenCPU and igraph output format
我的数据邻接数组是
var g = [[10, 2], [15, 0], [18, 3], [19, 6], [20, 8.5], [25, 10], [30, 9], [35, 8], [40, 5], [45, 6], [50, 2.5]]
我的 OpenCPU 代码是
ocpu.call("centralization.closeness", {graph: g}, function(res){
// console.log(ocpu.seturl(res.output[0]));
$http.get("//public.opencpu.org/"+res.output[0]+"/json").success(function(data) {
console.log(data);
});
});
这是错误的
OpenCPU error HTTP 400 Not a graph object
In call: centralization.closeness(graph = g)
centralization.closeness
采用图形对象而不是数组
建议:
- 将数组转换为邻接矩阵
- 使用
graph_from_adjacency_matrix
将矩阵转换为图形。
- 将结果图传递给
centralization.closeness
编辑: 此处的解决方案:https://jsfiddle.net/bowofola/pskezhLq/2/
var graph = [
[0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1],
[1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1],
[1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[0, 1, 1, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0],
[1, 1, 0, 1, 0, 1, 0, 0, 0, 1, 1, 0, 1, 0],
[1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 1, 1, 1],
[0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1],
[0, 1, 1, 0, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1],
[0, 1, 1, 0, 0, 0, 0, 1, 0, 1, 1, 0, 0, 0],
[1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 0, 0, 0, 1],
[1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 0, 0, 1, 1],
[1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 0, 0, 1, 1],
[1, 1, 1, 0, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0],
[1, 1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 0, 0]
];
//set CORS to call igraph package
ocpu.seturl("https://public.opencpu.org/ocpu/library/igraph/R");
var graphSession;
$('#output').text(graph.toString());
$('#adjMatrix').click(function() {
ocpu.call("graph_from_adjacency_matrix", {
adjmatrix: graph,
mode: 'directed',
weighted: true
}, function(session) {
graphSession = session;
//retrieve session console (async)
graphSession.getConsole(function(outtxt) {
$("#output").text(outtxt);
$("#centralize").prop('disabled', false);
});
}).fail(function() {
alert("Error: " + req.responseText);
});
});
$('#centralize').click(function() {
var centralizeReq = ocpu.call("centralization.closeness", {
graph: graphSession,
mode: "all",
normalized: true
}, function(centralizeSession) {
centralizeSession.getConsole(function(outtxt) {
$("#output").text(outtxt);
});
}).fail(function() {
alert("Error: " + req.responseText);
});
});
<script src="https://code.jquery.com/jquery-1.11.1.min.js"></script>
<script src="https://cdn.opencpu.org/opencpu-0.4.js"></script>
<div>
<textarea name="" id="output" cols="60" rows="10"></textarea>
<br />
<button id="adjMatrix">Graph From Adj</button>
<button id="centralize" disabled>Centralize</button>
</div>
有关使用 open cpu 的更多示例,请前往此处:http://jsfiddle.net/user/opencpu/fiddles/
您希望 OpenCPU 运行 到底是什么?目前您正在运行宁此代码:
library(igraph)
g <- jsonlite::fromJSON('[[10, 2], [15, 0], [18, 3], [19, 6], [20, 8.5], [25, 10], [30, 9], [35, 8], [40, 5], [45, 6], [50, 2.5]]')
igraph::centralization.closeness(g)
当 运行在 R 中执行此操作时,您将遇到相同的错误。您需要编写一个包装函数,将矩阵转换为一种类型,然后可以将其传递给 centralization.closeness
.
我的数据邻接数组是
var g = [[10, 2], [15, 0], [18, 3], [19, 6], [20, 8.5], [25, 10], [30, 9], [35, 8], [40, 5], [45, 6], [50, 2.5]]
我的 OpenCPU 代码是
ocpu.call("centralization.closeness", {graph: g}, function(res){
// console.log(ocpu.seturl(res.output[0]));
$http.get("//public.opencpu.org/"+res.output[0]+"/json").success(function(data) {
console.log(data);
});
});
这是错误的
OpenCPU error HTTP 400 Not a graph object
In call: centralization.closeness(graph = g)
centralization.closeness
采用图形对象而不是数组
建议:
- 将数组转换为邻接矩阵
- 使用
graph_from_adjacency_matrix
将矩阵转换为图形。 - 将结果图传递给
centralization.closeness
编辑: 此处的解决方案:https://jsfiddle.net/bowofola/pskezhLq/2/
var graph = [
[0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1],
[1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1],
[1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[0, 1, 1, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0],
[1, 1, 0, 1, 0, 1, 0, 0, 0, 1, 1, 0, 1, 0],
[1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 1, 1, 1],
[0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1],
[0, 1, 1, 0, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1],
[0, 1, 1, 0, 0, 0, 0, 1, 0, 1, 1, 0, 0, 0],
[1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 0, 0, 0, 1],
[1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 0, 0, 1, 1],
[1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 0, 0, 1, 1],
[1, 1, 1, 0, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0],
[1, 1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 0, 0]
];
//set CORS to call igraph package
ocpu.seturl("https://public.opencpu.org/ocpu/library/igraph/R");
var graphSession;
$('#output').text(graph.toString());
$('#adjMatrix').click(function() {
ocpu.call("graph_from_adjacency_matrix", {
adjmatrix: graph,
mode: 'directed',
weighted: true
}, function(session) {
graphSession = session;
//retrieve session console (async)
graphSession.getConsole(function(outtxt) {
$("#output").text(outtxt);
$("#centralize").prop('disabled', false);
});
}).fail(function() {
alert("Error: " + req.responseText);
});
});
$('#centralize').click(function() {
var centralizeReq = ocpu.call("centralization.closeness", {
graph: graphSession,
mode: "all",
normalized: true
}, function(centralizeSession) {
centralizeSession.getConsole(function(outtxt) {
$("#output").text(outtxt);
});
}).fail(function() {
alert("Error: " + req.responseText);
});
});
<script src="https://code.jquery.com/jquery-1.11.1.min.js"></script>
<script src="https://cdn.opencpu.org/opencpu-0.4.js"></script>
<div>
<textarea name="" id="output" cols="60" rows="10"></textarea>
<br />
<button id="adjMatrix">Graph From Adj</button>
<button id="centralize" disabled>Centralize</button>
</div>
有关使用 open cpu 的更多示例,请前往此处:http://jsfiddle.net/user/opencpu/fiddles/
您希望 OpenCPU 运行 到底是什么?目前您正在运行宁此代码:
library(igraph)
g <- jsonlite::fromJSON('[[10, 2], [15, 0], [18, 3], [19, 6], [20, 8.5], [25, 10], [30, 9], [35, 8], [40, 5], [45, 6], [50, 2.5]]')
igraph::centralization.closeness(g)
当 运行在 R 中执行此操作时,您将遇到相同的错误。您需要编写一个包装函数,将矩阵转换为一种类型,然后可以将其传递给 centralization.closeness
.