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

networkanalysis-ts

v1.0.0

Published

TypeScript port of the Java networkanalysis package that provides data structures and algorithms for network analysis.

Downloads

1,651

Readme

networkanalysis-ts

This package is a TypeScript port of the networkanalysis package written in Java. The package provides algorithms and data structures for network analysis. Currently, the package focuses on clustering (or community detection) and layout (or mapping) of networks. In particular, the package contains an implementation of the Leiden algorithm and the Louvain algorithm for network clustering and the VOS technique for network layout. Only undirected networks are supported.

The networkanalysis-ts package was developed by Nees Jan van Eck at the Centre for Science and Technology Studies (CWTS) at Leiden University and benefited from contributions by Olya Stukova and Nikita Rokotyan from Interacta. The networkanalysis package written in Java on which networkanalysis-ts is based was developed by Vincent Traag, Nees Jan van Eck, and Ludo Waltman.

Documentation

Documentation of the source code of networkanalysis-ts is provided in the code in TSDoc format. The documentation is also available in a compiled format.

Installation

npm install networkanalysis-ts

Usage

The following code snippet demonstrates how the core classes in networkanalysis-ts can be used to create a network and to perform network normalization, clustering, and layout:

import { Clustering, GradientDescentVOSLayoutAlgorithm, Layout, LeidenAlgorithm, Network } from 'networkanalysis-ts'

const nRandomStarts = 10

// Construct network.
const nNodes = 6
const edges = [[0, 1, 2, 2, 3, 5, 4], [1, 2, 0, 3, 5, 4, 3]]
const network = new Network({
  nNodes: nNodes,
  setNodeWeightsToTotalEdgeWeights: true,
  edges: edges,
  sortedEdges: false,
  checkIntegrity: true,
})

// Perform network normalization.
const normalizedNetwork = network.createNormalizedNetworkUsingAssociationStrength()

// Perform clustering.
let bestClustering: Clustering | undefined
let maxQuality = Number.NEGATIVE_INFINITY
const clusteringAlgorithm = new LeidenAlgorithm()
clusteringAlgorithm.setResolution(0.2)
clusteringAlgorithm.setNIterations(50)
for (let i = 0; i < nRandomStarts; i++) {
  const clustering = new Clustering({ nNodes: normalizedNetwork.getNNodes() })
  clusteringAlgorithm.improveClustering(normalizedNetwork, clustering)
  const quality = clusteringAlgorithm.calcQuality(normalizedNetwork, clustering)
  if (quality > maxQuality) {
    bestClustering = clustering
    maxQuality = quality
  }
}
bestClustering?.orderClustersByNNodes()

// Perform layout.
let bestLayout: Layout | undefined
let minQuality = Number.POSITIVE_INFINITY
const layoutAlgorithm = new GradientDescentVOSLayoutAlgorithm();
layoutAlgorithm.setAttraction(2)
layoutAlgorithm.setRepulsion(1)
for (let i = 0; i < nRandomStarts; i++) {
  const layout = new Layout({ nNodes: normalizedNetwork.getNNodes() })
  layoutAlgorithm.improveLayout(normalizedNetwork, layout)
  const quality = layoutAlgorithm.calcQuality(normalizedNetwork, layout)
  if (quality < minQuality) {
    bestLayout = layout
    minQuality = quality
  }
}
bestLayout?.standardize(true)

The package also includes a run module that provides helper classes for running the network analysis algorithms in an easier way. The following code snippet demonstrates the use of the helper classes for constructing a network and for performing network clustering and layout:

import { Node, Link, NetworkClustering, NetworkLayout } from 'networkanalysis-ts/run'

// Construct network.
const nodes: Node[] = [
  { id: 'James' },
  { id: 'Mary' },
  { id: 'John' },
  { id: 'Linda' },
  { id: 'David' },
  { id: 'Karen' },
]
const links: Link[] = [
  { node1: nodes[0], node2: nodes[1] },
  { node1: nodes[1], node2: nodes[2] },
  { node1: nodes[2], node2: nodes[0] },
  { node1: nodes[2], node2: nodes[3] },
  { node1: nodes[3], node2: nodes[5] },
  { node1: nodes[5], node2: nodes[4] },
  { node1: nodes[4], node2: nodes[3] },
]

// Perform clustering.
new NetworkClustering()
  .data(nodes, links)
  .qualityFunction('CPM')
  .normalization('AssociationStrength')
  .resolution(0.2)
  .minClusterSize(1)
  .algorithm('Leiden')
  .randomStarts(10)
  .iterations(50)
  .randomness(0.01)
  .seed(0)
  .run()

// Perform layout.
new NetworkLayout()
  .data(nodes, links)
  .qualityFunction('VOS')
  .normalization('AssociationStrength')
  .attraction(2)
  .repulsion(1)
  .randomStarts(10)
  .seed(0)
  .run()

Demo app

The GitHub repository of networkanalys-ts also provides a Svelt demo app that uses the helper classes discussed above. The source code of the demo app is available in the app/ folder. The following screenshot shows the output of the demo app when applying it to a journal co-citation network:

License

The networkanalysis-ts package is distributed under the MIT license.

Issues

If you encounter any issues, please report them using the issue tracker on GitHub.

Contribution

You are welcome to contribute to the development of networkanalysis-ts. Please follow the typical GitHub workflow: Fork from this repository and make a pull request to submit your changes. Make sure that your pull request has a clear description and that the code has been properly tested.

Development and deployment

The latest stable version of the code is available from the main branch on GitHub. The most recent code, which may be under development, is available from the develop branch.

Requirements

To run networkanalysis-ts locally and to build production-ready bundles, Node.js and npm need to be installed on your system.

Setup

Run

npm install

to install all required Node.js packages.

Development

Run

npm run dev

to build a development version of the demo app and serve it with hot reload at http://localhost:6800.

Deployment

Run

npm run build:lib

to build a deployment version of the package. The output is stored in the lib/ folder.

Run

npm run build:app

to build a deployment version of the demo app. The output is stored in the dist/ folder.

Run

npm run build

to build a deployment version of both the package and the demo app.

References

Traag, V.A., Waltman, L., & Van Eck, N.J. (2019). From Louvain to Leiden: Guaranteeing well-connected communities. Scientific Reports, 9, 5233. https://doi.org/10.1038/s41598-019-41695-z

Van Eck, N.J., Waltman, L., Dekker, R., & Van den Berg, J. (2010). A comparison of two techniques for bibliometric mapping: Multidimensional scaling and VOS. Journal of the American Society for Information Science and Technology, 61(12), 2405-2416. https://doi.org/10.1002/asi.21421

Waltman, L., Van Eck, N.J., & Noyons, E.C.M. (2010). A unified approach to mapping and clustering of bibliometric networks. Journal of Informetrics, 4(4), 629-635. https://doi.org/10.1016/j.joi.2010.07.002

Van Eck, N.J., & Waltman, L. (2009). How to normalize co-occurrence data? An analysis of some well-known similarity measures. Journal of the American Society for Information Science and Technology, 60(8), 1635-1651. https://doi.org/10.1002/asi.21075

Blondel, V.D., Guillaume, J.-L., Lambiotte, R., & Lefebvre, E. (2008). Fast unfolding of communities in large networks. Journal of Statistical Mechanics: Theory and Experiment, 10, P10008. https://doi.org/10.1088/1742-5468/2008/10/P10008

Newman, M.E.J. & Girvan, M. (2004). Finding and evaluating community structure in networks. Physical Review E, 69(2), 026113, https://doi.org/10.1103/PhysRevE.69.026113.