@talmolab/sleap-io.js 0.1.2 → 0.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -1,127 +1,5 @@
1
- declare class Node {
2
- name: string;
3
- constructor(name: string);
4
- }
5
- declare class Edge {
6
- source: Node;
7
- destination: Node;
8
- constructor(source: Node, destination: Node);
9
- at(index: number): Node;
10
- }
11
- declare class Symmetry {
12
- nodes: Set<Node>;
13
- constructor(nodes: Iterable<Node>);
14
- at(index: number): Node;
15
- }
16
- type NodeOrIndex = Node | string | number;
17
- declare class Skeleton {
18
- nodes: Node[];
19
- edges: Edge[];
20
- symmetries: Symmetry[];
21
- name?: string;
22
- private nameToNode;
23
- private nodeToIndex;
24
- constructor(options: {
25
- nodes: Array<Node | string>;
26
- edges?: Array<Edge | [NodeOrIndex, NodeOrIndex]>;
27
- symmetries?: Array<Symmetry | [NodeOrIndex, NodeOrIndex]>;
28
- name?: string;
29
- } | Array<Node | string>);
30
- rebuildCache(nodes?: Node[]): void;
31
- get nodeNames(): string[];
32
- index(node: NodeOrIndex): number;
33
- node(node: NodeOrIndex): Node;
34
- get edgeIndices(): Array<[number, number]>;
35
- get symmetryNames(): Array<[string, string]>;
36
- matches(other: Skeleton): boolean;
37
- addEdge(source: NodeOrIndex, destination: NodeOrIndex): void;
38
- addSymmetry(left: NodeOrIndex, right: NodeOrIndex): void;
39
- private edgeFrom;
40
- private symmetryFrom;
41
- }
42
-
43
- declare class Track {
44
- name: string;
45
- constructor(name: string);
46
- }
47
- type Point = {
48
- xy: [number, number];
49
- visible: boolean;
50
- complete: boolean;
51
- name?: string;
52
- };
53
- type PredictedPoint = Point & {
54
- score: number;
55
- };
56
- type PointsArray = Point[];
57
- type PredictedPointsArray = PredictedPoint[];
58
- declare function pointsEmpty(length: number, names?: string[]): PointsArray;
59
- declare function predictedPointsEmpty(length: number, names?: string[]): PredictedPointsArray;
60
- declare function pointsFromArray(array: number[][], names?: string[]): PointsArray;
61
- declare function predictedPointsFromArray(array: number[][], names?: string[]): PredictedPointsArray;
62
- declare class Instance {
63
- points: PointsArray;
64
- skeleton: Skeleton;
65
- track?: Track | null;
66
- fromPredicted?: PredictedInstance | null;
67
- trackingScore: number;
68
- constructor(options: {
69
- points: PointsArray | Record<string, number[]>;
70
- skeleton: Skeleton;
71
- track?: Track | null;
72
- fromPredicted?: PredictedInstance | null;
73
- trackingScore?: number;
74
- });
75
- static fromArray(points: number[][], skeleton: Skeleton): Instance;
76
- static fromNumpy(options: {
77
- pointsData: number[][];
78
- skeleton: Skeleton;
79
- track?: Track | null;
80
- fromPredicted?: PredictedInstance | null;
81
- trackingScore?: number;
82
- }): Instance;
83
- static empty(options: {
84
- skeleton: Skeleton;
85
- }): Instance;
86
- get length(): number;
87
- get nVisible(): number;
88
- getPoint(target: number | string | Node): Point;
89
- numpy(options?: {
90
- invisibleAsNaN?: boolean;
91
- }): number[][];
92
- toString(): string;
93
- get isEmpty(): boolean;
94
- overlapsWith(other: Instance, iouThreshold?: number): boolean;
95
- boundingBox(): [number, number, number, number] | null;
96
- }
97
- declare class PredictedInstance extends Instance {
98
- score: number;
99
- constructor(options: {
100
- points: PredictedPointsArray | Record<string, number[]>;
101
- skeleton: Skeleton;
102
- track?: Track | null;
103
- score?: number;
104
- trackingScore?: number;
105
- });
106
- static fromArray(points: number[][], skeleton: Skeleton, score?: number): PredictedInstance;
107
- static fromNumpy(options: {
108
- pointsData: number[][];
109
- skeleton: Skeleton;
110
- track?: Track | null;
111
- score?: number;
112
- trackingScore?: number;
113
- }): PredictedInstance;
114
- static empty(options: {
115
- skeleton: Skeleton;
116
- }): PredictedInstance;
117
- numpy(options?: {
118
- scores?: boolean;
119
- invisibleAsNaN?: boolean;
120
- }): number[][];
121
- toString(): string;
122
- }
123
- declare function pointsFromDict(pointsDict: Record<string, number[]>, skeleton: Skeleton): PointsArray;
124
- declare function predictedPointsFromDict(pointsDict: Record<string, number[]>, skeleton: Skeleton): PredictedPointsArray;
1
+ import { I as Instance, P as PredictedInstance, S as Skeleton, T as Track } from './instance-D_5PPN1y.js';
2
+ export { E as Edge, N as Node, k as NodeOrIndex, a as Point, c as PointsArray, b as PredictedPoint, d as PredictedPointsArray, j as Symmetry, p as pointsEmpty, f as pointsFromArray, h as pointsFromDict, e as predictedPointsEmpty, g as predictedPointsFromArray, i as predictedPointsFromDict } from './instance-D_5PPN1y.js';
125
3
 
126
4
  type VideoFrame = ImageData | ImageBitmap | Uint8Array | ArrayBuffer;
127
5
  interface VideoBackend {
@@ -363,10 +241,118 @@ declare class Mp4BoxVideoBackend implements VideoBackend {
363
241
  private addToCache;
364
242
  }
365
243
 
244
+ /**
245
+ * Streaming HDF5 file access via Web Worker.
246
+ *
247
+ * This module provides a high-level API for accessing remote HDF5 files
248
+ * using HTTP range requests for efficient streaming. The actual HDF5
249
+ * operations run in a Web Worker where synchronous XHR is allowed.
250
+ *
251
+ * @module
252
+ */
253
+ /**
254
+ * Options for opening a streaming HDF5 file.
255
+ */
256
+ interface StreamingH5Options {
257
+ /** URL to h5wasm IIFE bundle. Defaults to CDN. */
258
+ h5wasmUrl?: string;
259
+ /** Filename hint for the HDF5 file. */
260
+ filenameHint?: string;
261
+ }
262
+ /**
263
+ * A streaming HDF5 file handle that uses a Web Worker for range request access.
264
+ *
265
+ * This class provides an API similar to h5wasm.File but operates via message
266
+ * passing to a worker where createLazyFile enables HTTP range requests.
267
+ */
268
+ declare class StreamingH5File {
269
+ private worker;
270
+ private messageId;
271
+ private pendingMessages;
272
+ private _keys;
273
+ private _isOpen;
274
+ constructor();
275
+ private handleMessage;
276
+ private handleError;
277
+ private send;
278
+ /**
279
+ * Initialize the h5wasm module in the worker.
280
+ */
281
+ init(options?: StreamingH5Options): Promise<void>;
282
+ /**
283
+ * Open a remote HDF5 file for streaming access.
284
+ *
285
+ * @param url - URL to the HDF5 file (must support HTTP range requests)
286
+ * @param options - Optional settings
287
+ */
288
+ open(url: string, options?: StreamingH5Options): Promise<void>;
289
+ /**
290
+ * Whether a file is currently open.
291
+ */
292
+ get isOpen(): boolean;
293
+ /**
294
+ * Get the root-level keys in the file.
295
+ */
296
+ keys(): string[];
297
+ /**
298
+ * Get the keys (children) at a given path.
299
+ */
300
+ getKeys(path: string): Promise<string[]>;
301
+ /**
302
+ * Get an attribute value.
303
+ */
304
+ getAttr(path: string, name: string): Promise<unknown>;
305
+ /**
306
+ * Get all attributes at a path.
307
+ */
308
+ getAttrs(path: string): Promise<Record<string, unknown>>;
309
+ /**
310
+ * Get dataset metadata (shape, dtype) without reading values.
311
+ */
312
+ getDatasetMeta(path: string): Promise<{
313
+ shape: number[];
314
+ dtype: string;
315
+ }>;
316
+ /**
317
+ * Read a dataset's value.
318
+ *
319
+ * @param path - Path to the dataset
320
+ * @param slice - Optional slice specification (array of [start, end] pairs)
321
+ */
322
+ getDatasetValue(path: string, slice?: Array<[number, number] | []>): Promise<{
323
+ value: unknown;
324
+ shape: number[];
325
+ dtype: string;
326
+ }>;
327
+ /**
328
+ * Close the file and terminate the worker.
329
+ */
330
+ close(): Promise<void>;
331
+ }
332
+ /**
333
+ * Check if streaming via Web Worker is supported in the current environment.
334
+ */
335
+ declare function isStreamingSupported(): boolean;
336
+ /**
337
+ * Open a remote HDF5 file with streaming support.
338
+ *
339
+ * @param url - URL to the HDF5 file
340
+ * @param options - Optional settings
341
+ * @returns A StreamingH5File instance
342
+ */
343
+ declare function openStreamingH5(url: string, options?: StreamingH5Options): Promise<StreamingH5File>;
344
+
366
345
  type SlpSource = string | ArrayBuffer | Uint8Array | File | FileSystemFileHandle;
367
346
  type StreamMode = "auto" | "range" | "download";
368
347
  type OpenH5Options = {
348
+ /**
349
+ * Streaming mode for remote files:
350
+ * - "auto": Try range requests, fall back to download
351
+ * - "range": Use HTTP range requests (requires Worker support in browser)
352
+ * - "download": Always download the entire file
353
+ */
369
354
  stream?: StreamMode;
355
+ /** Filename hint for the HDF5 file */
370
356
  filenameHint?: string;
371
357
  };
372
358
 
@@ -436,4 +422,240 @@ declare function labelsFromNumpy(data: number[][][][], options: {
436
422
  declare function decodeYamlSkeleton(yamlData: string): Skeleton | Skeleton[];
437
423
  declare function encodeYamlSkeleton(skeletons: Skeleton | Skeleton[]): string;
438
424
 
439
- export { Camera, CameraGroup, Edge, FrameGroup, Instance, InstanceGroup, LabeledFrame, Labels, type LabelsDict, LabelsSet, Mp4BoxVideoBackend, Node, type NodeOrIndex, type Point, type PointsArray, PredictedInstance, type PredictedPoint, type PredictedPointsArray, RecordingSession, Skeleton, SuggestionFrame, Symmetry, Track, Video, type VideoBackend, type VideoFrame, decodeYamlSkeleton, encodeYamlSkeleton, fromDict, fromNumpy, labelsFromNumpy, loadSlp, loadVideo, makeCameraFromDict, pointsEmpty, pointsFromArray, pointsFromDict, predictedPointsEmpty, predictedPointsFromArray, predictedPointsFromDict, rodriguesTransformation, saveSlp, toDict, toNumpy };
425
+ /**
426
+ * Context passed to pre/post render callbacks.
427
+ */
428
+ declare class RenderContext {
429
+ /** The 2D canvas rendering context */
430
+ readonly canvas: CanvasRenderingContext2D;
431
+ /** Current frame index (0 for single images) */
432
+ readonly frameIdx: number;
433
+ /** Original frame size [width, height] */
434
+ readonly frameSize: [number, number];
435
+ /** Instances in this frame */
436
+ readonly instances: (Instance | PredictedInstance)[];
437
+ /** Skeleton edge connectivity as [srcIdx, dstIdx] pairs */
438
+ readonly skeletonEdges: [number, number][];
439
+ /** Node names from skeleton */
440
+ readonly nodeNames: string[];
441
+ /** Current scale factor */
442
+ readonly scale: number;
443
+ /** Offset for cropped views [x, y] */
444
+ readonly offset: [number, number];
445
+ constructor(
446
+ /** The 2D canvas rendering context */
447
+ canvas: CanvasRenderingContext2D,
448
+ /** Current frame index (0 for single images) */
449
+ frameIdx: number,
450
+ /** Original frame size [width, height] */
451
+ frameSize: [number, number],
452
+ /** Instances in this frame */
453
+ instances: (Instance | PredictedInstance)[],
454
+ /** Skeleton edge connectivity as [srcIdx, dstIdx] pairs */
455
+ skeletonEdges: [number, number][],
456
+ /** Node names from skeleton */
457
+ nodeNames: string[],
458
+ /** Current scale factor */
459
+ scale?: number,
460
+ /** Offset for cropped views [x, y] */
461
+ offset?: [number, number]);
462
+ /**
463
+ * Transform world coordinates to canvas coordinates.
464
+ */
465
+ worldToCanvas(x: number, y: number): [number, number];
466
+ }
467
+ /**
468
+ * Context passed to per-instance callbacks.
469
+ */
470
+ declare class InstanceContext {
471
+ /** The 2D canvas rendering context */
472
+ readonly canvas: CanvasRenderingContext2D;
473
+ /** Index of this instance within the frame */
474
+ readonly instanceIdx: number;
475
+ /** Keypoint coordinates as [[x0, y0], [x1, y1], ...] */
476
+ readonly points: number[][];
477
+ /** Skeleton edge connectivity */
478
+ readonly skeletonEdges: [number, number][];
479
+ /** Node names */
480
+ readonly nodeNames: string[];
481
+ /** Track ID (index in tracks array) */
482
+ readonly trackIdx: number | null;
483
+ /** Track name if available */
484
+ readonly trackName: string | null;
485
+ /** Instance confidence score */
486
+ readonly confidence: number | null;
487
+ /** Current scale factor */
488
+ readonly scale: number;
489
+ /** Offset for cropped views */
490
+ readonly offset: [number, number];
491
+ constructor(
492
+ /** The 2D canvas rendering context */
493
+ canvas: CanvasRenderingContext2D,
494
+ /** Index of this instance within the frame */
495
+ instanceIdx: number,
496
+ /** Keypoint coordinates as [[x0, y0], [x1, y1], ...] */
497
+ points: number[][],
498
+ /** Skeleton edge connectivity */
499
+ skeletonEdges: [number, number][],
500
+ /** Node names */
501
+ nodeNames: string[],
502
+ /** Track ID (index in tracks array) */
503
+ trackIdx?: number | null,
504
+ /** Track name if available */
505
+ trackName?: string | null,
506
+ /** Instance confidence score */
507
+ confidence?: number | null,
508
+ /** Current scale factor */
509
+ scale?: number,
510
+ /** Offset for cropped views */
511
+ offset?: [number, number]);
512
+ /**
513
+ * Transform world coordinates to canvas coordinates.
514
+ */
515
+ worldToCanvas(x: number, y: number): [number, number];
516
+ /**
517
+ * Get centroid of valid (non-NaN) points.
518
+ */
519
+ getCentroid(): [number, number] | null;
520
+ /**
521
+ * Get bounding box of valid points.
522
+ * Returns [x1, y1, x2, y2] or null if no valid points.
523
+ */
524
+ getBbox(): [number, number, number, number] | null;
525
+ }
526
+
527
+ /** RGB color as [r, g, b] with values 0-255 */
528
+ type RGB = [number, number, number];
529
+ /** RGBA color as [r, g, b, a] with values 0-255 */
530
+ type RGBA = [number, number, number, number];
531
+ /** Flexible color specification */
532
+ type ColorSpec = RGB | RGBA | string | number;
533
+ /** Available color schemes */
534
+ type ColorScheme = "track" | "instance" | "node" | "auto";
535
+ /** Built-in palette names */
536
+ type PaletteName = "standard" | "distinct" | "tableau10" | "viridis" | "rainbow" | "warm" | "cool" | "pastel" | "seaborn";
537
+ /** Marker shape types */
538
+ type MarkerShape = "circle" | "square" | "diamond" | "triangle" | "cross";
539
+ /** Render options for renderImage() */
540
+ interface RenderOptions {
541
+ colorBy?: ColorScheme;
542
+ palette?: PaletteName | string;
543
+ markerShape?: MarkerShape;
544
+ markerSize?: number;
545
+ lineWidth?: number;
546
+ alpha?: number;
547
+ showNodes?: boolean;
548
+ showEdges?: boolean;
549
+ scale?: number;
550
+ background?: "transparent" | ColorSpec;
551
+ image?: ImageData | null;
552
+ width?: number;
553
+ height?: number;
554
+ preRenderCallback?: (ctx: RenderContext) => void;
555
+ postRenderCallback?: (ctx: RenderContext) => void;
556
+ perInstanceCallback?: (ctx: InstanceContext) => void;
557
+ }
558
+ /** Video rendering options (extends RenderOptions) */
559
+ interface VideoOptions extends RenderOptions {
560
+ frameInds?: number[];
561
+ start?: number;
562
+ end?: number;
563
+ fps?: number;
564
+ codec?: string;
565
+ crf?: number;
566
+ preset?: string;
567
+ onProgress?: (current: number, total: number) => void;
568
+ }
569
+
570
+ /** Named CSS colors */
571
+ declare const NAMED_COLORS: Record<string, RGB>;
572
+ /** Built-in color palettes (port from Python sleap_io/rendering/colors.py) */
573
+ declare const PALETTES: Record<PaletteName, RGB[]>;
574
+ /**
575
+ * Get n colors from a named palette, cycling if needed.
576
+ */
577
+ declare function getPalette(name: PaletteName | string, n: number): RGB[];
578
+ /**
579
+ * Resolve flexible color specification to RGB tuple.
580
+ */
581
+ declare function resolveColor(color: ColorSpec): RGB;
582
+ /**
583
+ * Convert RGB to CSS color string.
584
+ */
585
+ declare function rgbToCSS(rgb: RGB, alpha?: number): string;
586
+ /**
587
+ * Determine color scheme based on context.
588
+ * - If tracks available: 'track'
589
+ * - Else if single image: 'instance'
590
+ * - Else: 'node' (prevents flicker in video)
591
+ */
592
+ declare function determineColorScheme(scheme: ColorScheme, hasTracks: boolean, isSingleImage: boolean): ColorScheme;
593
+
594
+ type DrawMarkerFn = (ctx: CanvasRenderingContext2D, x: number, y: number, size: number, fillColor: string, edgeColor?: string, edgeWidth?: number) => void;
595
+ /**
596
+ * Draw a circle marker.
597
+ */
598
+ declare function drawCircle(ctx: CanvasRenderingContext2D, x: number, y: number, size: number, fillColor: string, edgeColor?: string, edgeWidth?: number): void;
599
+ /**
600
+ * Draw a square marker.
601
+ */
602
+ declare function drawSquare(ctx: CanvasRenderingContext2D, x: number, y: number, size: number, fillColor: string, edgeColor?: string, edgeWidth?: number): void;
603
+ /**
604
+ * Draw a diamond marker (rotated square).
605
+ */
606
+ declare function drawDiamond(ctx: CanvasRenderingContext2D, x: number, y: number, size: number, fillColor: string, edgeColor?: string, edgeWidth?: number): void;
607
+ /**
608
+ * Draw a triangle marker (pointing up).
609
+ */
610
+ declare function drawTriangle(ctx: CanvasRenderingContext2D, x: number, y: number, size: number, fillColor: string, edgeColor?: string, edgeWidth?: number): void;
611
+ /**
612
+ * Draw a cross/plus marker.
613
+ */
614
+ declare function drawCross(ctx: CanvasRenderingContext2D, x: number, y: number, size: number, fillColor: string, _edgeColor?: string, edgeWidth?: number): void;
615
+ /** Map of marker shape names to drawing functions */
616
+ declare const MARKER_FUNCTIONS: Record<MarkerShape, DrawMarkerFn>;
617
+ /**
618
+ * Get the drawing function for a marker shape.
619
+ */
620
+ declare function getMarkerFunction(shape: MarkerShape): DrawMarkerFn;
621
+
622
+ /**
623
+ * Render poses on a single frame.
624
+ *
625
+ * @param source - Labels, LabeledFrame, or array of Instances to render
626
+ * @param options - Rendering options
627
+ * @returns ImageData with rendered poses
628
+ */
629
+ declare function renderImage(source: Labels | LabeledFrame | (Instance | PredictedInstance)[], options?: RenderOptions): Promise<ImageData>;
630
+ /**
631
+ * Convert ImageData to PNG buffer (Node.js only).
632
+ */
633
+ declare function toPNG(imageData: ImageData): Promise<Buffer>;
634
+ /**
635
+ * Convert ImageData to JPEG buffer (Node.js only).
636
+ */
637
+ declare function toJPEG(imageData: ImageData, quality?: number): Promise<Buffer>;
638
+ /**
639
+ * Convert ImageData to data URL.
640
+ */
641
+ declare function toDataURL(imageData: ImageData, format?: "png" | "jpeg"): string;
642
+ /**
643
+ * Save ImageData to a file.
644
+ */
645
+ declare function saveImage(imageData: ImageData, path: string): Promise<void>;
646
+
647
+ /**
648
+ * Check if ffmpeg is available in PATH.
649
+ */
650
+ declare function checkFfmpeg(): Promise<boolean>;
651
+ /**
652
+ * Render video with pose overlays.
653
+ * Requires ffmpeg to be installed and in PATH.
654
+ *
655
+ * @param source - Labels or array of LabeledFrames to render
656
+ * @param outputPath - Path to save the output video
657
+ * @param options - Video rendering options
658
+ */
659
+ declare function renderVideo(source: Labels | LabeledFrame[], outputPath: string, options?: VideoOptions): Promise<void>;
660
+
661
+ export { Camera, CameraGroup, type ColorScheme, type ColorSpec, FrameGroup, Instance, InstanceContext, InstanceGroup, LabeledFrame, Labels, type LabelsDict, LabelsSet, MARKER_FUNCTIONS, type MarkerShape, Mp4BoxVideoBackend, NAMED_COLORS, PALETTES, type PaletteName, PredictedInstance, type RGB, type RGBA, RecordingSession, RenderContext, type RenderOptions, Skeleton, StreamingH5File, SuggestionFrame, Track, Video, type VideoBackend, type VideoFrame, type VideoOptions, checkFfmpeg, decodeYamlSkeleton, determineColorScheme, drawCircle, drawCross, drawDiamond, drawSquare, drawTriangle, encodeYamlSkeleton, fromDict, fromNumpy, getMarkerFunction, getPalette, isStreamingSupported, labelsFromNumpy, loadSlp, loadVideo, makeCameraFromDict, openStreamingH5, renderImage, renderVideo, resolveColor, rgbToCSS, rodriguesTransformation, saveImage, saveSlp, toDataURL, toDict, toJPEG, toNumpy, toPNG };