Giter Club home page Giter Club logo

jsnetworkx's Introduction

JSNetworkX Build Status

JSNetworkX allows you to build, process and analyze graphs in JavaScript. It can be used together with D3.js in the browser to create interactive graph visualizations.

It is a port of NetworkX (v1.6), a popular graph library for Python, to JavaScript. Extensive information can be found on:

Install

Node.js

Install from npm:

npm install jsnetworkx

Browser

Download jsnetworkx.js and include it in your page with

<script src="/path/to/jsnetworkx.js"></script>

This will create the global variable jsnx, with which all functions can be accessed.

Usage

JSNetworkX consists of multiple parts which work closely together:

  • Graph classes (Graph, DiGraph, MultiGraph and MultiDiGraph) to model the data
  • Graph generators for common graphs
  • Various graph algorithms
  • Graph visualization (in the browser)

Most classes and functions are available on the root object (jsnx in browsers, require('jsnetworkx') in Node).

Information about which algorithms are available and the API of the classes, can be found in the auto-generated API documentation.

Example

// var jsnx = require('jsnetworkx'); // in Node

// a tree of height 4 with fan-out 2
var G = jsnx.balancedTree(2, 4);

// Compute the shortest path between node 2 and 7
var path = jsnx.bidirectionalShortestPath(G, 2, 7);
// [ 2, 0, 1, 3, 7 ]

// or asynchronously
jsnx.genBidirectionalShortestPath(G, 2, 7).then(function(path) {
  // path = [ 2, 0, 1, 3, 7 ]
});

More examples can be found on the website.

Asynchronous computation

All the algorithms are implemented in a synchronous fashion (for now at least). However, many algorithms are also available as asynchronous version. Their names are gen<SyncFunctionName> (see example above) and they return a Promise.

This is achieved in browsers by creating a WebWorker. The WebWorker has to be passed the path to the jsnetworkx.js file. You have to set the path explicitly if the file is not located at the root:

jsnx.workerPath = '/path/to/jsnetworkx.js';

In Node, a subprocess will be spawned (no setup is required).

Caveat: In both cases the input data has to be serialized before it can be sent to the worker or subprocess. However, not every value can be serialized, in which case JSNetworkX will use the synchronous version instead. If you encounter a situation where a value is not serialized, but it should be serializable, please file an issue.

Iterables

Many methods return generators or Maps. In an ES2015 environment, these can be easily consumed with a for/of loop or Array.from.

If those are not available to you, JSNetworkX provides two helper methods for iterating iterables and converting them to arrays: jsnx.forEach and jsnx.toArray


How to contribute

You can contribute by:

  • Porting code from Python
  • Improving the documentation/website

If you plan on converting/porting a specific part, please create an issue beforehand.

Build JSNetworkX

JSNetworkX is written in ES2015 (ES6) and Babel is used to convert it to ES5. For the browser, all modules are bundled together with browserify.

To build JSNetworkX, all dependencies have to be installed via

npm install

Build for the browser

npm run build:browser

creates jsnetworkx.js, a minified version for production.

npm run build:browser:dev
npm run watch:browser

Creates jsnetworkx-dev.js, an unminified version with inline source maps for development. The second version automatically rebuilds the file on change.

Build for Node

npm run build:node

Transforms all modules to ES5 and saves them inside the node/ directory.

npm run build:node:dev

Same as above but with inline source maps. These modules are also used to tun the unit tests.

npm run watch:node

Incrementally transform modules when files change.

Create and run tests

Tests are stored in the respective __tests__ directories and have to follow the naming convention <testname>-test.js. The tests can be run with

npm test
# or
npm run test:fast # if you also run `npm run watch:node`

This will run all tests by default. To consider only those files whose path matches a specific string, pass the -g option:

# Runs all digraph tests but no graph tests
npm run test:fast -- -g digraph

The difference between npm test and npm run test:fast is that the former will always transplile all files from ES6 to ES5 first. This is slow and annoying during development. Therefore you can use

npm run watch:node

to automatically convert only the changed file and run npm run test:fast to quickly test them.

Ideally, every module has corresponding unit test. If you port a module from NetworkX, make sure to implement the same tests.

Run coverage

We use istanbul to generate a coverage report. We are not enforcing any coverage yet, but there should not be a regression. The report can be created via

npm run cover

and written to coverage/.

jsnetworkx's People

Contributors

duckpunch avatar fkling avatar midgleyf avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

jsnetworkx's Issues

Feedback wanted: Iterables, especially objects

Currently the helper functions to iterate over "collections" iterate over the property names of an object, not its values. That's how it is in Python as well. I'm wondering if this is expected behavior or surprising. We could also iterate over the property values instead.

Maybe we should even distinguish between iterables with key => value and index => value, i.e. KeyedIterable and IndexIterable.

Run tests in browser

Running the tests in node should be sufficient most of the time, but being able to run them in the browser as well would be an additional safe guard.

jsnx unresolved variable

Hello,

I've used the python version of Networkx so was interested in trying out this port.

I'm working on Windows 7 box and use the RubyMine IDE I've created a script tag

<script src="js/jsnetworkx.js"></script> <script src="js/main.js"></script>

However in my main.js if I declare

var G = jsnx.complete_graph(6);

The error jsnx unresolved is produced, can you advise what I'm doing wrong?

Brian

Need mantainer?

Hello there,
wondering if you need a mantainer to update some part of the codebase and be inline with the PRs,
ping me in case!

n

Recompiling with latest version of Closure Library breaks iterators

Hi, I've run into some trouble lately since I had to modify JSNetworkX. Recompiling with the latest version of the Closure Library (or maybe it's the newer Closure Compiler?) seems to cause .next() to be obfuscated on all iterators. Graph.nodes_iter() and jsnx.sentinelIterator() both return fully obfuscated objects. I checked both on my changes and on 031524a, but got the same result (.next() gets renamed to b). Any suggestions?

jsnx.allPairsDijkstraPath fails with complex node types

The following code will work when nodes are primitive objects, but not when complex objects.

function test(p) {
    var G = new jsnx.Graph();
    G.addStar(p, {weight: 1.0});
    return jsnx.allPairsDijkstraPath(G)
}

test([1,2,3,4]); // works
test([{name: "cat"},{name: "dog"},{name: "mouse"}]); // fails
test([[0,0],[0,1],[1,0]]); // fails

The error message is

Uncaught TypeError: Cannot read property 'toString' of undefined(โ€ฆ)
n @ jsnetworkx.js:1
d @ jsnetworkx.js:3
s @ jsnetworkx.js:3
b @ jsnetworkx.js:3

Note, that the code will work when allPairsDijkstraPath is replaced with allPairsShortestPath.

Tree generator not working as expected

Hi fkling,
What is the purpose of this part of the balanced tree generator code?

  if (r === 1) {
    n = 2;
  }

It appears to limit trees of degree one to only two nodes. For example, generating a tree of degree 1 and height 3 should give something like this:
expected tree

Instead, we get this:
untitled

Release v0.3

  • Update website examples to use the new API (changes are made but not pushed yet)
  • Add API documentation to website (#28)
  • Write changelog
  • Publish on NPM (#42)

how to add a label for every node ?

Hi ,I want to add a string on every node, but with_labels just add the node's name in the circle ,how to make a label beside the circle ? and I also want to set different size for every circle , thank you very much .

Publish to NPM

I intend to start publishing pre-release versions to NPM as soon as the API documentation generation system is in place, and the change to camelCase API is complete.

Tests: Shallow copies use different attrdicts?

Maybe I'm confused, but I was under the impression that the payload for nodes and edges was shallowly copied for the copy-constructor and deeply copied for the subgraph/to_directed/etc methods, but I see in test_0_graph.js:

BaseAttrGraphTester.prototype.is_shallow_copy = function(H, G) {
    this.graphs_equal(H, G);
    this.different_attrdict(H, G);
    this.shallow_copy_attrdict(H, G);
};

What am I missing?

sigmajs support

How easy would it be to integrate support for the sigmajs visualisation library?

I think the json graph representation that sigma.js accepts matches the one use by the d3.js network layout?

.gml parsing (AJAX)

After looking at the codebase, I haven't found this because well, it's probably not something someone really should do, meaning that they should really port it to some other representation.

But it might help those moving from NetworkX to JSNetworkX to easily adopt. It also helps those who are more dependent on Gephi for network science. This also could mean that JSNetworkX could potentially build a browser based version of Gephi should this project grow that way.

I thought I'd just begin the discussion.

Self loops are not drawn

Self loops are not shown in the graph. Furthermore, if the graph is drawn with labels, self loop labels appear in the upper left corner of the canvas.

Feedback wanted: Algorithms and promises

We cannot do CPU intensive computations without asynchronous processing (e.g. web workers). Should we change the API and always return promises from all algorithm functions, or provide an extra build which does that?

Error while computing eigenvectorCentrality

Hi,
I am trying to use jsnx for a project. I am successfully able to maintain and update the graph data structure and use the ported functions to get the values I need. However, when trying to compute the eigenvectorCentrality, I get the following error:

"Uncaught #" (jsnetworkx.js - Line 2)

I can't figure what the issue is since other functions seem to be working just fine with the same graph. This is the syntax I am using

jsnx.eigenvectorCentrality(networkXGraph)

where networkXGraph holds the graph I am using.

Any help with this would be greatly appreciated.

Why so much code duplication?

I've noticed that subclasses of Graph pretty much completely re-implement the Graph api, rather than delegating to internal methods and just overriding those internal methods.

Is there a reason for this? Performance?

Info: Development progress

I'm going to use this new issue as a way to document my progress of refactoring the code. I'm also happy to receive comments and suggestions.

(this is an experiment, I'm not sure if I'm disciplined enough to keep this up, but I will try my best :) )

Create a documentation generation system

Current idea: Run a separate site/service that automatically rebuilds the documentation after pushing to GitHub.

The service will just be a React site that gets the API information from a JSON blob. The plan is to generate documentation for master and different versions (minor versions before 1.0). E.g. now we would only have master. When v0.3 is released, we will have master and the latest v0.3.x. tag.

I will probably create a new repo for that.

Redesign drawing API

There currently is only the draw function which is like a one-of black box. It's not possible to make changes to the drawing config (without completely redrawing the graph), to extend it to support different kinds of visualization or different layouts.

I already started working on rewriting this part a long time ago and think that it's a good opportunity to introduce it with the next release.

The drawing API will be evolve around an abstract Visualization class which exposes an API that allows easily update an existing visualization. This class can be subclassed to provide different implements. I already have one for SVG output (with D3) and WebGL (experimental, with Three.js). The configuration options will mostly stay the same, but the API is richer.

Usage will look something like this:

var v = jsnx.draw(G, container, {/* some config here */}); // defaults to SVG visualization
// now or later
v.labels(true); // show labels
// or
v.labels(function(d) { return d.data.name; }); // show labels and use node data "name"
v.nodeStyle({style: {fill: 'red' }}); // style options depend on visualization

An implementation might not support every method. E.g. the WebGL implementation currently doesn't support labels, so calling v.labels() will just do nothing. On the other hand, implementations might offer additional methods, e.g. the WebGL implementation provides v.camera to the set the camera to be used.

closeness and degree centrality

Hey There, I am happy to help start knocking out some of the basic centrality algos.

I have a small contribution in the python library.

Nice work on the library - really love the implementation of ES6! JavaScript is really making it easier to do this kind of work.

Multiple tests fail in Firefox, especially those related to MultiDiGraph

JSNetworkX makes use of Google closures iterator module, which recognises objects with a __iterator__ a iterables.

Firefox' JavaScript version also supports this property, but for normal for...in loops, and thus, every for...in statement that is supposed to iterate over the properties of such an object breaks.

Porting of max_weighted_matching

I have a JavaScript version of the max_weighted_matching procedure here. It is not a port of the NetworkX version but of the original implementation made by Joris van Rantwijk. I do not know if this can help.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.