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 Candidate = {
	edgeWeight: number;
	node: string;
	relation: 'in' | 'out' | 'return';
};
export const calculateNode2vecTransitions = (
	candidatesInput: unknown,
	returnParameterInput: unknown,
	inOutParameterInput: unknown
): string => {
	const returnParameter = parseFiniteNumber(
		returnParameterInput
	);
	const inOutParameter = parseFiniteNumber(inOutParameterInput);
	if (
		returnParameter === null ||
		returnParameter <= 0 ||
		inOutParameter === null ||
		inOutParameter <= 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 Candidate).node !== 'string' ||
				!(candidate as Candidate).node ||
				!Number.isFinite((candidate as Candidate).edgeWeight) ||
				(candidate as Candidate).edgeWeight <= 0 ||
				!['return', 'in', 'out'].includes(
					(candidate as Candidate).relation
				)
		)
	)
		return '';
	const weighted = (candidates as Candidate[]).map(
		(candidate) => ({
			...candidate,
			weight:
				candidate.edgeWeight *
				(candidate.relation === 'return'
					? 1 / returnParameter
					: candidate.relation === 'out'
						? 1 / inOutParameter
						: 1)
		})
	);
	const total = weighted.reduce(
		(sum, candidate) => sum + candidate.weight,
		0
	);
	return JSON.stringify(
		{
			probabilities: weighted.map(
				({ node, relation, weight }) => ({
					node,
					probability: Number((weight / total).toPrecision(10)),
					relation
				})
			),
			q: inOutParameter,
			p: returnParameter
		},
		null,
		2
	);
};

export const Tool: TypeTool = {
	name: 'Node2vec Transition Demonstrator',
	slug: 'graph/node2vec-transition',
	intro:
		'Calculate second-order node2vec transition probabilities from return, in-neighborhood, and outward candidates.',
	tool: calculateNode2vecTransitions,
	i: [
		{
			example:
				'[{"node":"a","edgeWeight":1,"relation":"return"},{"node":"b","edgeWeight":2,"relation":"in"},{"node":"c","edgeWeight":1,"relation":"out"}]',
			label: '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'
		}
	],
	o: [
		{
			example: '{ "probabilities": [] }',
			label: 'Transition Probabilities',
			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