0

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.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);
            });
        });

this is giving error

OpenCPU error HTTP 400 Not a graph object

In call: centralization.closeness(graph = g)

zx8754
  • 52,746
  • 12
  • 114
  • 209
Asad Ali Khan
  • 307
  • 4
  • 16

2 Answers2

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);
  });

});
<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>

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

Bowofola
  • 1,132
  • 9
  • 12
  • array is in adjacency matrix..... when i use "graph_from_adjacency_matrix" it gives me result in session.... but i cannot pass the session result into centralization.closeness as a graph object... http://stackoverflow.com/questions/43930580/opencpu-error-http-400-not-a-graph-object – Asad Ali Khan May 13 '17 at 07:40
  • graph_from_adjacency_matrix print something like this IGRAPH D-W- 14 119 -- \n+ attr: weight (e/n)\n+ edges:\n [1] 1-> 2 1-> 3 1-> 5 1-> 6 1->10 1->11 1->12 1->13 1->14 2-> 1\n[11] 2-> 3 2- – Asad Ali Khan May 13 '17 at 07:44
  • update the matrix in the question to the one in http://stackoverflow.com/questions/43930580/opencpu-error-http-400-not-a-graph-object – Bowofola May 13 '17 at 13:26
  • Yes..... this is the point i was missing..... ( centralization.closeness(graph = x06eddb341a::.val ) this is what SHOULD be pass to another function... and it is done as shown in example provided by Brother Bowofola at https://jsfiddle.net/bowofola/pskezhLq/2/ – Asad Ali Khan May 14 '17 at 06:32
  • One more small Advice... i can rplot my Graph But i cannot view it on broweser (http://public.opencpu.org/ocpu/tmp/x03c8281217/graphics/last/png?width=100&height=100) But i can view PNG if i access same URL like this (http://public.opencpu.org/ocpu/tmp/x03c8281217/graphics/last/png) – Asad Ali Khan May 14 '17 at 11:43
  • I have no idea bro. What format are you expecting...? – Bowofola May 14 '17 at 16:30
  • i m facing this same issues as describe here....... https://github.com/opencpu/opencpu/issues/200 – Asad Ali Khan May 15 '17 at 05:27
0

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.

Jeroen Ooms
  • 31,998
  • 35
  • 134
  • 207
  • i cannot pass the session result into centralization.closeness as a graph object. please view this http://stackoverflow.com/questions/43930580/opencpu-error-http-400-not-a-graph-object – Asad Ali Khan May 13 '17 at 09:36
  • Can you include an example of the R code that you would like to run (without opencpu) that makes it easier to show how you would do this via opencpu. – Jeroen Ooms May 13 '17 at 09:45
  • you can look at this....http://www.orgnodes.com/open.html in the mean while i provide R code... – Asad Ali Khan May 13 '17 at 09:48
  • i m using angular JS to generate adjacency matrix and than converting the matrix into graph using "graph_from_adjacency_matrix" and than want to pass on that "output" as graph object into "closeness" – Asad Ali Khan May 13 '17 at 09:51
  • Basic R CODE IS g <- graph.adjacency(m,mode = "directed", weighted = TRUE) get.adjacency(g, attr="weight") print(g) w_closeness <- centralization.closeness(g)$res – Asad Ali Khan May 13 '17 at 09:51
  • ocpu.call("graph_from_adjacency_matrix", {adjmatrix: graph, mode:'directed', weighted:true}, function(res){ $http.get("//public.opencpu.org/"+res.output[0]).success(function(data) { ocpu.call("centralization.closeness", {graph: data}, function(res){ $http.get("//public.opencpu.org/"+res.output[0]+"/json").success(function(data) { console.log(data); }); }); }); }); – Asad Ali Khan May 13 '17 at 09:56
  • what ever value i pass into "graph" param it gives me error Unparsable argument: {0:[1,2,3],1[1,2,3]} what is the param value that param graph take in function centralization.closeness – Asad Ali Khan May 13 '17 at 11:23