npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

jsbayes-viz

v0.0.8

Published

Simple Bayesian Belief Network (BBN) visualization and interaction library.

Downloads

8

Readme

jsbayes-viz

This JavaScript library is a Bayesian Belief Network (BBN) visualization and interaction tool. It is built on the following projects.

How do I use jsbayes-viz?

You should only be using this library on the client-side (e.g. with a browser). You may install this library using npm.

npm install jsbayes-viz --save

Since there are third party dependencies, you need to reference them in your HTML in the following order. Assuming you have used bower to install the library (and its dependencies), you may reference the libaries as the following.

<script src="node_modules/d3/d3.js"></script>
<script src="node_modules/lodash/lodash.js"></script>
<script src="node_modules/graphlib/dist/graphlib.core.js"></script>
<script src="node_modules/dagre/dist/dagre.core.js"></script>
<script src="node_modules/jsbayes/jsbayes.js"></script>
<script src="node_modules/jsbayes-viz/jsbayes-viz.js"></script>

jsbayes is the inference engine, so to use jsbayes-viz, first create a jsbayes graph.

var graph = jsbayes.newGraph();
var n1 = graph.addNode('n1', ['t', 'f']);
var n2 = graph.addNode('n2', ['t', 'f']);
var n3 = graph.addNode('n3', ['t', 'f']);
var n4 = graph.addNode('n4', ['t', 'f']);
var n5 = graph.addNode('n5', ['0', '1', '2']);

n2.addParent(n1);
n3.addParent(n2);
n3.addParent(n4);
n5.addParent(n4);

graph.reinit();
graph.sample(10000);

Then you create a corresponding jsbayes-viz graph from the jsbayes graph.

var g = jsbayesviz.fromGraph(graph);

Assuming on your HTML page, you have an SVG element like the following.

<svg id="bbn"></svg>

Then you can kick off the visualization as follows.

jsbayesviz.draw({
  id: '#bbn',
  width: 800,
  height: 800,
  graph: g,
  samples: 15000
});

You may also download the samples in JSON or CSV format. To download in JSON format, call the following method. Note the first parameter is the graph (jsbayes-viz graph, NOT the jsbayes graph), the second parameter specifies the format (true means JSON and false means CSV), and the last parameter are options. Options are only available for CSV format, namely, to specify row and field delimiters.

jsbayesviz.downloadSamples(graph, true, {});

An example of downloading samples as CSV is shown below.

jsbayesviz.downloadSamples(graph, false, { rowDelimiter: '\n', fieldDelimiter: ',' });

Styling

Each of the SVG components are now associated with a CSS class. You may apply a stylesheet to customize the look and feel of each of these SVG components/elements.

  • .node-group : all the elements belonging to a node
  • .node-name : the name of a node
  • .node-shape : the rectangle that encloses all the elements belonging to a node
  • .node-value : the node values
  • .node-pct : the node marginal probabilities correspoding to the values
  • .node-bar : the belief bars
  • .node-iqline : the interquartile lines (e.g. 25%, 50%, 75%)
  • .edge-line : the arc between two nodes
  • .edge-head : the arrow head at the end of an arc

Here's an example.

svg g rect.node-shape { border-radius: 5px; fill:#ffecb3; cursor: move; }
svg g text.node-name { font-weight: 800 }
svg g rect.node-bar { fill: green }
svg g text.node-value { fill:rgb(0,0,0); font-size: 15px; cursor: pointer; }
svg line.edge-line { stroke:rgb(0,0,0) }
svg path.edge-head { fill:rgb(0,0,0) }

Or better yet, copy and modify jsbayes-viz.scss and modify it to your needs (you will need to generate the corresponding css file).

Some gotcha's

If you have very long string literals as values for the node id/values, then these strings will be truncated to 5 characters at the current moment. Node names are also truncated to 15 characters.

Lastly

A working example is shown here on Plunker and you may fork the demo code by clicking here.

Here are some repositories of BBNs that you may use.

Unit Testing

To run the unit tests, make sure you have NodeJS and npm installed and type in the following.

npm install
make

Bower

Install bower.

npm install -g bower

Install libraries via bower.

bower install

Citation

@misc{vang_jsbayesviz_2016, 
title={jsbayes-viz}, 
url={https://github.com/vangj/jsbayes-viz/}, 
journal={GitHub},
author={Vang, Jee}, 
year={2016}, 
month={Jan}}

Copyright Stuff

Copyright 2016 Jee Vang

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.