【问题标题】:Need help for OpenCPU and igraph output format需要 OpenCPU 和 igraph 输出格式的帮助
【发布时间】:2017-05-13 05:00:36
【问题描述】:

我的数据邻接数组是

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 错误 HTTP 400 不是图形对象

通话中:centralization.closeness(graph = g)

【问题讨论】:

    标签: javascript angularjs r opencpu


    【解决方案1】:

    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/

    【讨论】:

    【解决方案2】:

    您希望 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的类型。

    【讨论】:

    • 我无法将会话结果作为图形对象传递给 centralization.closeness。请查看此stackoverflow.com/questions/43930580/…
    • 您能否包含一个您想要运行的 R 代码示例(不带 opencpu),以便更轻松地展示您如何通过 opencpu 执行此操作。
    • 你可以看看这个....orgnodes.com/open.html 在我提供 R 代码的同时...
    • 我使用 Angular JS 生成邻接矩阵,然后使用“graph_from_adjacency_matrix”将矩阵转换为图形,而不是将“输出”作为图形对象传递给“接近度”
    • 基本 R 代码是 g
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-23
    • 1970-01-01
    • 2012-05-22
    • 2022-01-01
    • 1970-01-01
    • 2023-02-22
    相关资源
    最近更新 更多