Skip to content

Main content

Node2Vec++ Transition Demonstrator Source Code

Browse the implementation files for Node2Vec++ Transition Demonstrator.
tool.ts typescript
import { parseFiniteNumber } from '../../parse/number/tool';

type WeightedCandidate = {
	connectedToPrevious: boolean;
	edgeWeight: number;
	isReturn: boolean;
	node: string;
};
export const calculateNode2vecPlusPlusTransitions = (
	candidatesInput: unknown,
	returnParameterInput: unknown,
	inOutParameterInput: unknown,
	connectionThresholdInput: unknown
): string => {
	const p = parseFiniteNumber(returnParameterInput);
	const q = parseFiniteNumber(inOutParameterInput);
	const threshold = parseFiniteNumber(connectionThresholdInput);
	if (
		p === null ||
		p <= 0 ||
		q === null ||
		q <= 0 ||
		threshold === null ||
		threshold < 0 ||
		typeof candidatesInput !== 'string'
	)
		return '';
	let candidates: unknown;
	try {
		candidates = JSON.parse(candidatesInput);
	} catch {
		return '';
	}
	if (
		!Array.isArray(candidates) ||
		!candidates.length ||
		candidates.some(
			(candidate) =>
				typeof candidate !== 'object' ||
				candidate === null ||
				typeof (candidate as WeightedCandidate).node !==
					'string' ||
				!Number.isFinite(
					(candidate as WeightedCandidate).edgeWeight
				) ||
				(candidate as WeightedCandidate).edgeWeight <= 0 ||
				typeof (candidate as WeightedCandidate).isReturn !==
					'boolean' ||
				typeof (candidate as WeightedCandidate)
					.connectedToPrevious !== 'boolean'
		)
	)
		return '';
	const weighted = (candidates as WeightedCandidate[]).map(
		(candidate) => {
			const stronglyConnected =
				candidate.connectedToPrevious &&
				candidate.edgeWeight >= threshold;
			const bias = candidate.isReturn
				? 1 / p
				: stronglyConnected
					? 1
					: 1 / q;
			return {
				node: candidate.node,
				weight: candidate.edgeWeight * bias
			};
		}
	);
	const total = weighted.reduce(
		(sum, candidate) => sum + candidate.weight,
		0
	);
	return JSON.stringify(
		{
			probabilities: weighted.map(({ node, weight }) => ({
				node,
				probability: Number((weight / total).toPrecision(10))
			})),
			threshold
		},
		null,
		2
	);
};

export const Tool: TypeTool = {
	name: 'Node2Vec++ Transition Demonstrator',
	slug: 'graph/node2vec-plus-plus-transition',
	intro:
		'Demonstrate weighted node2vec-style transitions with a threshold for strong connections to the previous node.',
	tool: calculateNode2vecPlusPlusTransitions,
	i: [
		{
			example:
				'[{"node":"a","edgeWeight":1,"isReturn":true,"connectedToPrevious":true},{"node":"b","edgeWeight":2,"isReturn":false,"connectedToPrevious":true},{"node":"c","edgeWeight":0.5,"isReturn":false,"connectedToPrevious":false}]',
			label: 'Weighted Candidates',
			placeholder: 'Candidate transitions as JSON',
			render: 'textarea',
			type: 'text'
		},
		{
			example: '1',
			label: 'Return Parameter p',
			placeholder: 'Positive p',
			type: 'number'
		},
		{
			example: '2',
			label: 'In–Out Parameter q',
			placeholder: 'Positive q',
			type: 'number'
		},
		{
			example: '1',
			label: 'Connection Threshold',
			placeholder: 'Nonnegative strong-edge threshold',
			type: 'number'
		}
	],
	o: [
		{
			example: '{ "probabilities": [] }',
			label: 'Weighted Transitions',
			placeholder: 'Probabilities will appear here...',
			render: 'textarea',
			type: 'text'
		}
	]
};
export default Tool;

Description

Browse the implementation files for Node2Vec++ Transition Demonstrator.

References

  1. Tool.io on GitHub
  2. Tool.io TypeScript tools library

Similar or alternative tools