Skip to content Skip to sidebar Skip to footer

Need Help For Opencpu And Igraph Output Format

My data Adjancy array is 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]] and my OpenCPU code is ocpu

Solution 1:

centralization.closeness takes a graph object and not an array

Suggestion:

  • convert the array to an adjacency matrix
  • convert matrix to graph using graph_from_adjacency_matrix.
  • pass resulting graph to centralization.closeness

EDIT: solution here: 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);
  });

});
<scriptsrc="https://code.jquery.com/jquery-1.11.1.min.js"></script><scriptsrc="https://cdn.opencpu.org/opencpu-0.4.js"></script><div><textareaname=""id="output"cols="60"rows="10"></textarea><br /><buttonid="adjMatrix">Graph From Adj</button><buttonid="centralize"disabled>Centralize</button></div>

for more examples on using open cpu, head here: http://jsfiddle.net/user/opencpu/fiddles/

Solution 2:

What exactly is it you expect OpenCPU to run? Currently you are running this code:

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)

When running this in R you will get the same error. You need to write a wrapper function which transforms the matrix into a type which can then be passed to centralization.closeness.

Post a Comment for "Need Help For Opencpu And Igraph Output Format"