clustering-tfjs
Version:
High-performance TypeScript clustering algorithms (K-Means, Spectral, Agglomerative) with TensorFlow.js acceleration and scikit-learn compatibility
118 lines (117 loc) • 4.84 kB
JavaScript
;
/**
* Main entry point for the clustering library
*
* Provides initialization and configuration for multi-platform support.
*/
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __exportStar = (this && this.__exportStar) || function(m, exports) {
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Clustering = exports.findOptimalClusters = exports.pairwiseDistanceMatrix = exports.AgglomerativeClustering = exports.SpectralClustering = exports.KMeans = void 0;
const tf_backend_1 = require("./tf-backend");
const kmeans_1 = require("./clustering/kmeans");
const spectral_1 = require("./clustering/spectral");
const agglomerative_1 = require("./clustering/agglomerative");
// Re-export all clustering algorithms and utilities
__exportStar(require("./clustering/types"), exports);
var kmeans_2 = require("./clustering/kmeans");
Object.defineProperty(exports, "KMeans", { enumerable: true, get: function () { return kmeans_2.KMeans; } });
var spectral_2 = require("./clustering/spectral");
Object.defineProperty(exports, "SpectralClustering", { enumerable: true, get: function () { return spectral_2.SpectralClustering; } });
var agglomerative_2 = require("./clustering/agglomerative");
Object.defineProperty(exports, "AgglomerativeClustering", { enumerable: true, get: function () { return agglomerative_2.AgglomerativeClustering; } });
var pairwise_distance_1 = require("./utils/pairwise_distance");
Object.defineProperty(exports, "pairwiseDistanceMatrix", { enumerable: true, get: function () { return pairwise_distance_1.pairwiseDistanceMatrix; } });
var findOptimalClusters_1 = require("./utils/findOptimalClusters");
Object.defineProperty(exports, "findOptimalClusters", { enumerable: true, get: function () { return findOptimalClusters_1.findOptimalClusters; } });
// Detect platform at runtime
const detectPlatform = () => {
if (typeof window !== 'undefined' && typeof window.document !== 'undefined') {
return 'browser';
}
else if (typeof process !== 'undefined' && process.versions && process.versions.node) {
return 'node';
}
return 'unknown';
};
// Get platform features based on detected platform
const getPlatformFeatures = (platform) => {
switch (platform) {
case 'browser':
return {
gpuAcceleration: typeof WebGLRenderingContext !== 'undefined',
wasmSimd: typeof WebAssembly !== 'undefined' && 'validate' in WebAssembly,
nodeBindings: false,
webgl: typeof WebGLRenderingContext !== 'undefined',
};
case 'node':
return {
gpuAcceleration: false, // Will be updated after backend init
wasmSimd: false,
nodeBindings: true,
webgl: false,
};
default:
return {
gpuAcceleration: false,
wasmSimd: false,
nodeBindings: false,
webgl: false,
};
}
};
/**
* Main clustering namespace with platform awareness
*/
exports.Clustering = {
/**
* Current platform
*/
platform: detectPlatform(),
/**
* Platform features
*/
features: getPlatformFeatures(detectPlatform()),
/**
* Initialize the clustering library with the specified backend
*
* @param config - Backend configuration options
* @returns Promise that resolves when the backend is ready
*
* @example
* ```typescript
* // Auto-detect best backend
* await Clustering.init();
*
* // Use specific backend
* await Clustering.init({ backend: 'webgl' });
*
* // With custom flags
* await Clustering.init({
* backend: 'wasm',
* flags: { 'WASM_HAS_SIMD_SUPPORT': true }
* });
* ```
*/
async init(config = {}) {
await (0, tf_backend_1.initializeBackend)(config);
// Features are set at detection time
// Could be enhanced later to detect actual backend capabilities
},
// Re-export algorithms as properties for convenient access
KMeans: kmeans_1.KMeans,
SpectralClustering: spectral_1.SpectralClustering,
AgglomerativeClustering: agglomerative_1.AgglomerativeClustering,
};