TypeScript port of the Java networkanalysis package that provides data structures and algorithms for network analysis.
npm install networkanalysis-tsTSDoc format. The documentation is also available in a compiled format.
sh
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:
`typescript
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:
`typescript
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.
$3
To run networkanalysis-ts locally and to build production-ready bundles, Node.js and npm need to be installed on your system.
$3
Run
`sh
npm install
`
to install all required Node.js packages.
$3
Run
`sh
npm run dev
`
to build a development version of the demo app and serve it with hot reload at http://localhost:6800.
$3
Run
`sh
npm run build:lib
`
to build a deployment version of the package. The output is stored in the lib/ folder.
Run
`sh
npm run build:app
`
to build a deployment version of the demo app. The output is stored in the dist/ folder.
Run
`sh
npm run build
``