ividja/tversky-fibers

0

stars

9

commits

Python

primary language

Jun 17, 2026

updated

README

Fibers of Asymmetric Similarity: A Framework for Clinical and Imaging Data

Asymmetric Tversky Similarity Fields

Prototype-based binary classification using asymmetric Tversky similarity fields. The method is general-purpose and applicable to any image or tabular classification task. It is evaluated here on two medical ultrasound benchmarks:

  • CARDIUM — fetal cardiac ultrasound, CHD vs. control (dataset)
  • ThyroidCineClip — thyroid cine-clip ultrasound, benign vs. malignant (dataset)

Two complementary pipelines are provided: one for image-based models and one for clinical tabular data.

Core Idea

Scheme Fig. 1 - Organ-wise Image Concepts and Patient-wise Concepts with Multiple-Instance Learning

Standard similarity metrics are symmetric — S(A, B) = S(B, A). Many classification problems are not. A borderline case may weakly resemble a positive-class prototype without that prototype resembling the borderline case. Asymmetric Tversky similarity captures this directional structure:

S(x → y) = |x ∩ y| / (|x ∩ y| + α|x \ y| + β|y \ x|)

α ≠ β breaks symmetry. During training, both S(x → prototype) and S(prototype → x) are computed and composed. The classifier is built from multiple composed similarity fields (Propositions 4 & 5): each field projects embeddings to a different subspace, learns its own prototype set, and the fields are combined via a learned convex weighting.


Image Pipeline

The image pipeline (train_similarity_field_v3_sigmoid.py) implements the multi-field Tversky classifier on top of frozen or fine-tuned pretrained encoders. Dataset-specific entry points wrap the same model code with the appropriate data loader and cross-validation strategy.

ScriptEvaluation datasetTask
train_similarity_field_v3_sigmoid.pyCARDIUMCHD vs. control
train_similarity_field_v3_sigmoid_thyroid.pyThyroidCineClipBenign vs. malignant

Architecture

Image → Pretrained Encoder → [num_fields × Projection Head] → Similarity Fields → Classification
  • Encoders: medvit_original, medvit, visionclip, dinov2 (frozen or fine-tuned)
  • Projection heads: mlp, grn, attention, se, bottleneck, cosine
  • Output activation: sigmoid, softplus, relu, relu_norm — constrains features to [0, 1] for Tversky compatibility
  • Prototypes: stored in logit space, decoded with sigmoid; separate positive (P1) and negative (P0) sets per field
  • Patient-level evaluation: image scores aggregated per patient (mean or gated attention) before metrics are computed

Key Design Choices

ComponentDefaultPurpose
Multi-field composition3 fieldsEnsemble of complementary feature subspaces
Dynamic α/βprogressiveStarts symmetric, increases asymmetry through training
Prototype initializationk-means / z-score coresetData-driven starting points
Stability anchorsoptionalPrevents prototype drift (Theorem 2)
Manifold anchoringoptionalPulls prototypes toward nearest embeddings
Feature attractionoptionalPulls embeddings toward nearest prototypes
Class-weighted hingeoptionalHandles class imbalance with sqrt-dampened weights

Quick Start

# CARDIUM
python train_similarity_field_v3_sigmoid.py \
    --data_dir /path/to/CARDIUM \
    --encoders dinov2 \
    --feature_dim 128 \
    --num_prototypes 7 \
    --num_fields 3 \
    --alpha_values 0.8 \
    --beta_values 0.2 \
    --margin_values 0.3 \
    --projection_head mlp \
    --output_activation sigmoid \
    --epochs 100 \
    --use_cv \
    --patient_level_eval \
    --differential_lr \
    --class_weighted \
    --out results.json

# ThyroidCineClip
python train_similarity_field_v3_sigmoid_thyroid.py \
    --data_dir ./thyroidultrasoundcineclip \
    --encoders dinov2 \
    --feature_dim 128 \
    --num_prototypes 5 \
    --use_cv \
    --num_cv_folds 5 \
    --out thyroid_results.json
bash run_experiments_v3_sigmoid.sh --gpus 0,1,2,3 --jobs-per-gpu 2

Sweeps encoders × feature dims × prototype counts × α/β × margin across GPUs and writes per-experiment JSON results.

Key Arguments

ArgumentDefaultDescription
--encodersefficientnetComma-separated encoder names
--feature_dim32Projection head output dimension
--num_prototypes5,7,10Prototype counts to sweep
--num_fields3Number of composed similarity fields
--alpha_values0.7,0.8,0.9Tversky α (weight on x\y)
--beta_values0.1,0.2,0.3Tversky β (weight on y\x)
--margin_values0.1,0.2,0.3Hinge loss margin
--combine_modegeometricField combination: geometric/arithmetic/forward_only/…
--projection_headmlpProjection architecture
--output_activationsigmoidOutput activation for [0,1] constraint
--differential_lroffSeparate LRs for encoder / projector / prototypes
--class_weightedoffSqrt-dampened positive class weighting
--patient_level_evaloffAggregate per patient before eval
--patient_aggmeanPatient aggregation: mean or attention
--augmentationdefaultdefault (AugMix) or geometric
--use_cvoffCross-validation
--dynamic_alpha_betaprogressiveα/β schedule: static/progressive/cyclical

Tabular Pipeline

The tabular pipeline treats clinical records as unordered sets of feature–value pairs, making the model invariant to measurement ordering and robust to variable-length records. The same architecture is evaluated on both benchmarks.

ScriptEvaluation datasetTask
benchmark_patient_level_v2.pyCARDIUMCHD vs. control
benchmark_thyroid_v2.pyThyroidCineClipBenign vs. malignant

Architecture

Clinical record (set of measurements)
    → Type-specific embedders (numeric / categorical / coded / temporal)
    → Permutation-invariant aggregation (Deep Sets: ρ ∘ Σφ)
    → [optional] Hypergraph layer (patient–feature–cell relationships)
    → Asymmetric Tversky similarity classifier

Type-specific modules:

ModuleFeature typeMethod
NumericEmbedderContinuous valuesGaussian normalization + MLP
CategoricalEmbedderDiscrete labelsLearned embeddings
CodedEmbedderICD / LOINC / drug codesHierarchical ontology embeddings
MonotonicEncoderOrdered featuresMonotone network (Lipschitz constrained)
HypergraphLayerAllHyperedge message passing over patient–feature graph

A clinical schema object encodes domain knowledge: valid ranges, feature types, monotonicity directions, and ontology hierarchy depths — but this can be replaced with a schema appropriate for any domain.

Quick Start

# CARDIUM
python benchmark_patient_level_v2.py \
    --data_dir /path/to/CARDIUM \
    --epochs 50 \
    --batch_size 32

# ThyroidCineClip
python benchmark_thyroid_v2.py \
    --data_dir ./thyroidultrasoundcineclip \
    --epochs 50 \
    --batch_size 32

Evaluation Datasets

The datasets below are used to evaluate the method; they are not requirements for using the code.

CARDIUM — fetal cardiac ultrasound + clinical records:

cardium_data/
├── cardium_images.tar.gz
├── delfos_clinical_data_woe_wnm_standarized_f_normalized.json
└── delfos_clinical_data_wnm_translated_final_cleaned.json

Download: https://github.com/BCV-Uniandes/Cardium

ThyroidCineClip — thyroid ultrasound cine-clips + metadata:

thyroidultrasoundcineclip/
├── dataset.hdf5
└── metadata.csv

Download: https://stanfordaimi.azurewebsites.net/datasets/thyroid-disease


Metrics

All evaluation is patient-level: image-level scores are aggregated per patient before computing metrics.

MetricDescription
AUC-ROCPrimary model selection criterion
F1 @ 0.5Balanced precision/recall
SensitivityRecall for positive-class patients
SpecificityTrue negative rate

Results are reported as mean ± std across cross-validation folds.

Contributors

ividja

9 commits

ividja/tversky-fibers

0

stars

9

commits

Python

primary language

Jun 17, 2026

updated

README

Fibers of Asymmetric Similarity: A Framework for Clinical and Imaging Data

Asymmetric Tversky Similarity Fields

Prototype-based binary classification using asymmetric Tversky similarity fields. The method is general-purpose and applicable to any image or tabular classification task. It is evaluated here on two medical ultrasound benchmarks:

  • CARDIUM — fetal cardiac ultrasound, CHD vs. control (dataset)
  • ThyroidCineClip — thyroid cine-clip ultrasound, benign vs. malignant (dataset)

Two complementary pipelines are provided: one for image-based models and one for clinical tabular data.

Core Idea

Scheme Fig. 1 - Organ-wise Image Concepts and Patient-wise Concepts with Multiple-Instance Learning

Standard similarity metrics are symmetric — S(A, B) = S(B, A). Many classification problems are not. A borderline case may weakly resemble a positive-class prototype without that prototype resembling the borderline case. Asymmetric Tversky similarity captures this directional structure:

S(x → y) = |x ∩ y| / (|x ∩ y| + α|x \ y| + β|y \ x|)

α ≠ β breaks symmetry. During training, both S(x → prototype) and S(prototype → x) are computed and composed. The classifier is built from multiple composed similarity fields (Propositions 4 & 5): each field projects embeddings to a different subspace, learns its own prototype set, and the fields are combined via a learned convex weighting.


Image Pipeline

The image pipeline (train_similarity_field_v3_sigmoid.py) implements the multi-field Tversky classifier on top of frozen or fine-tuned pretrained encoders. Dataset-specific entry points wrap the same model code with the appropriate data loader and cross-validation strategy.

ScriptEvaluation datasetTask
train_similarity_field_v3_sigmoid.pyCARDIUMCHD vs. control
train_similarity_field_v3_sigmoid_thyroid.pyThyroidCineClipBenign vs. malignant

Architecture

Image → Pretrained Encoder → [num_fields × Projection Head] → Similarity Fields → Classification
  • Encoders: medvit_original, medvit, visionclip, dinov2 (frozen or fine-tuned)
  • Projection heads: mlp, grn, attention, se, bottleneck, cosine
  • Output activation: sigmoid, softplus, relu, relu_norm — constrains features to [0, 1] for Tversky compatibility
  • Prototypes: stored in logit space, decoded with sigmoid; separate positive (P1) and negative (P0) sets per field
  • Patient-level evaluation: image scores aggregated per patient (mean or gated attention) before metrics are computed

Key Design Choices

ComponentDefaultPurpose
Multi-field composition3 fieldsEnsemble of complementary feature subspaces
Dynamic α/βprogressiveStarts symmetric, increases asymmetry through training
Prototype initializationk-means / z-score coresetData-driven starting points
Stability anchorsoptionalPrevents prototype drift (Theorem 2)
Manifold anchoringoptionalPulls prototypes toward nearest embeddings
Feature attractionoptionalPulls embeddings toward nearest prototypes
Class-weighted hingeoptionalHandles class imbalance with sqrt-dampened weights

Quick Start

# CARDIUM
python train_similarity_field_v3_sigmoid.py \
    --data_dir /path/to/CARDIUM \
    --encoders dinov2 \
    --feature_dim 128 \
    --num_prototypes 7 \
    --num_fields 3 \
    --alpha_values 0.8 \
    --beta_values 0.2 \
    --margin_values 0.3 \
    --projection_head mlp \
    --output_activation sigmoid \
    --epochs 100 \
    --use_cv \
    --patient_level_eval \
    --differential_lr \
    --class_weighted \
    --out results.json

# ThyroidCineClip
python train_similarity_field_v3_sigmoid_thyroid.py \
    --data_dir ./thyroidultrasoundcineclip \
    --encoders dinov2 \
    --feature_dim 128 \
    --num_prototypes 5 \
    --use_cv \
    --num_cv_folds 5 \
    --out thyroid_results.json
bash run_experiments_v3_sigmoid.sh --gpus 0,1,2,3 --jobs-per-gpu 2

Sweeps encoders × feature dims × prototype counts × α/β × margin across GPUs and writes per-experiment JSON results.

Key Arguments

ArgumentDefaultDescription
--encodersefficientnetComma-separated encoder names
--feature_dim32Projection head output dimension
--num_prototypes5,7,10Prototype counts to sweep
--num_fields3Number of composed similarity fields
--alpha_values0.7,0.8,0.9Tversky α (weight on x\y)
--beta_values0.1,0.2,0.3Tversky β (weight on y\x)
--margin_values0.1,0.2,0.3Hinge loss margin
--combine_modegeometricField combination: geometric/arithmetic/forward_only/…
--projection_headmlpProjection architecture
--output_activationsigmoidOutput activation for [0,1] constraint
--differential_lroffSeparate LRs for encoder / projector / prototypes
--class_weightedoffSqrt-dampened positive class weighting
--patient_level_evaloffAggregate per patient before eval
--patient_aggmeanPatient aggregation: mean or attention
--augmentationdefaultdefault (AugMix) or geometric
--use_cvoffCross-validation
--dynamic_alpha_betaprogressiveα/β schedule: static/progressive/cyclical

Tabular Pipeline

The tabular pipeline treats clinical records as unordered sets of feature–value pairs, making the model invariant to measurement ordering and robust to variable-length records. The same architecture is evaluated on both benchmarks.

ScriptEvaluation datasetTask
benchmark_patient_level_v2.pyCARDIUMCHD vs. control
benchmark_thyroid_v2.pyThyroidCineClipBenign vs. malignant

Architecture

Clinical record (set of measurements)
    → Type-specific embedders (numeric / categorical / coded / temporal)
    → Permutation-invariant aggregation (Deep Sets: ρ ∘ Σφ)
    → [optional] Hypergraph layer (patient–feature–cell relationships)
    → Asymmetric Tversky similarity classifier

Type-specific modules:

ModuleFeature typeMethod
NumericEmbedderContinuous valuesGaussian normalization + MLP
CategoricalEmbedderDiscrete labelsLearned embeddings
CodedEmbedderICD / LOINC / drug codesHierarchical ontology embeddings
MonotonicEncoderOrdered featuresMonotone network (Lipschitz constrained)
HypergraphLayerAllHyperedge message passing over patient–feature graph

A clinical schema object encodes domain knowledge: valid ranges, feature types, monotonicity directions, and ontology hierarchy depths — but this can be replaced with a schema appropriate for any domain.

Quick Start

# CARDIUM
python benchmark_patient_level_v2.py \
    --data_dir /path/to/CARDIUM \
    --epochs 50 \
    --batch_size 32

# ThyroidCineClip
python benchmark_thyroid_v2.py \
    --data_dir ./thyroidultrasoundcineclip \
    --epochs 50 \
    --batch_size 32

Evaluation Datasets

The datasets below are used to evaluate the method; they are not requirements for using the code.

CARDIUM — fetal cardiac ultrasound + clinical records:

cardium_data/
├── cardium_images.tar.gz
├── delfos_clinical_data_woe_wnm_standarized_f_normalized.json
└── delfos_clinical_data_wnm_translated_final_cleaned.json

Download: https://github.com/BCV-Uniandes/Cardium

ThyroidCineClip — thyroid ultrasound cine-clips + metadata:

thyroidultrasoundcineclip/
├── dataset.hdf5
└── metadata.csv

Download: https://stanfordaimi.azurewebsites.net/datasets/thyroid-disease


Metrics

All evaluation is patient-level: image-level scores are aggregated per patient before computing metrics.

MetricDescription
AUC-ROCPrimary model selection criterion
F1 @ 0.5Balanced precision/recall
SensitivityRecall for positive-class patients
SpecificityTrue negative rate

Results are reported as mean ± std across cross-validation folds.

Contributors

ividja

9 commits

Languages

Python

100.0%