anvaka / ngraph.forcelayout

Force directed graph layout
https://anvaka.github.io/ngraph.forcelayout/
BSD 3-Clause "New" or "Revised" License
163 stars 31 forks source link

Error: Node mass should be a number #40

Open AydinChavez opened 2 years ago

AydinChavez commented 2 years ago

Hi all,

when using this module as part of layouting the graph via

console.log('Loading graph');
var graph = require('./loadgraph.js')();
console.log('Loaded ' + graph.getNodesCount() + ' nodes; ' + graph.getLinksCount() + ' edges');

var createLayout = require('ngraph.offline.layout');
var layout = createLayout(graph);
console.log('Starting layout');
layout.run();

I get following error:

Loading graph
Loaded 79566 nodes; 255149 edges
/Users/aydin/Workspace/ngraph_native_suite/node_modules/ngraph.forcelayout/index.js:357
      throw new Error('Node mass should be a number')
      ^

Error: Node mass should be a number
    at updateBodyMass (/Users/aydin/Workspace/ngraph_native_suite/node_modules/ngraph.forcelayout/index.js:357:13)
    at initBody (/Users/aydin/Workspace/ngraph_native_suite/node_modules/ngraph.forcelayout/index.js:290:7)
    at /Users/aydin/Workspace/ngraph_native_suite/node_modules/ngraph.forcelayout/index.js:265:7
    at Object.forEachNode (/Users/aydin/Workspace/ngraph_native_suite/node_modules/ngraph.graph/index.js:543:11)
    at initPhysics (/Users/aydin/Workspace/ngraph_native_suite/node_modules/ngraph.forcelayout/index.js:264:11)
    at Function.createLayout [as get2dLayout] (/Users/aydin/Workspace/ngraph_native_suite/node_modules/ngraph.forcelayout/index.js:35:3)
    at createLayout (/Users/aydin/Workspace/ngraph_native_suite/node_modules/ngraph.forcelayout3d/index.js:21:23)
    at createLayout (/Users/aydin/Workspace/ngraph_native_suite/node_modules/ngraph.offline.layout/index.js:19:16)
    at Object.<anonymous> (/Users/aydin/Workspace/ngraph_native_suite/bin/layout.js:6:14)
    at Module._compile (internal/modules/cjs/loader.js:1085:14)

Any idea how to fix this? This does not happen with ngraph-binary layouting (which had another unrelated sideffects in the result)

dholth commented 2 years ago

I had this problem related to using newer ngraph.graph with the offline layout. Depending on the version of ngraph.graph .length or .size has to be used, when calculating mass

node_modules/ngraph.forcelayout/index.js: return 1 + (links.length || links.size) / 3.0;

anvaka commented 1 year ago

@dholth is correct.

Can you please run the following script to collect versions of your packages?

echo "ngraph.graph" && cat node_modules/ngraph.graph/package.json | grep version
echo "ngraph.offline.layout" && cat node_modules/ngraph.offline.layout/package.json | grep version
echo "ngraph.forcelayout3d" && cat node_modules/ngraph.forcelayout3d/package.json | grep version
echo "ngraph.forcelayout" && cat node_modules/ngraph.forcelayout/package.json | grep version

I do have checks in the current version of the library https://github.com/anvaka/ngraph.forcelayout/blob/04e521d8f7485ffdd3cedbf8469247df7f7f88ae/index.js#L23 and trying my best to figure out the version, but something seem to be missing.

AtomBoy commented 1 year ago

I'm using the graph and forcelayout via unplug.com and I'm seeing the same error with the current versions. Using graph v 19.1.0 works.

graph is 20.0.1 and force layout is 3.3.1.

<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8" />
    <title>ngraph error</title>
    <!-- new version 20.1 of ngraph.graph has a bug when adding an edge. reverting to 19.1.0 for now. -->
    <!-- script src="https://unpkg.com/ngraph.graph@19.1.0/dist/ngraph.graph.min.js" --></script>
    <script src="https://unpkg.com/ngraph.graph/dist/ngraph.graph.min.js"></script>
    <script src="https://unpkg.com/ngraph.forcelayout/dist/ngraph.forcelayout.min.js"></script>
  </head>

  <body> See console </body>

  <script defer>
    let g = createGraph();
    let l = ngraphCreateLayout(g);
    g.addNode('a');
    g.addNode('b');
    g.addLink('a', 'b'); /* <-- Throws [Error] Error: Node mass should be a number
    updateBodyMass (ngraph.forcelayout.min.js:1:5124)
    initLink (ngraph.forcelayout.min.js:1:4480)
    onGraphChanged (ngraph.forcelayout.min.js:1:3346)
    fire (ngraph.graph.min.js:1:8685)
    exitModificationReal (ngraph.graph.min.js:1:6575)
    addLink (ngraph.graph.min.js:1:3826)
    Global Code (127.0.0.1:58)
  */
  </script>
</html>

Thanks for the terrific libraries!

xela1601 commented 8 months ago

Hello @anvaka, first of all thanks a lot for your awesome open source contributions. Really appreciate.

I do have checks in the current version of the library [...] and trying my best to figure out the version, but something seem to be missing.

I also had the same error and i think i know what is the issue: the latest version of package ngraph.forcelayout3d (1.2.0) has dependency "ngraph.forcelayout": "^1.2.0". In that version of ngraph.forcelayout the check and both functions defaultArrayNodeMass and defaultSetNodeMass were not yet implemented.