Skip to main content

Retinal analysis toolbox for Python

Project description

Updates

  • 10/06/2026: Updated README with more complete information on interpreting outputs and advanced use cases.
  • 04/06/2026: We have fixed some remaining missing dependencies that meant run-models was not working correctly with vascx.

VascX retinal vascular analysis

VascX was created to facilitate the extraction of retinal vascular biomarkers from color fundus image (CFI) segmentations. The instructions in this repository explain how to run the entire pipeline, which has two main parts:

  • CFI Segmentation. Extraction of optic disc, vessels and artery vein segmentation and fovea locations. Our model weights are publicly available in a huggingface repository. See instructions below for inference.
  • Biomarker computation. Extraction of biomarkers from the segmentations.

Features

  • Currently supported biomarkers / features: Central Retinal Equivalents, Calibers, Artery-Vein Ratio, Vascular Density, Bifurcation Angles, Tortuosity, Sparsity, and others.
  • Many of these biomarkers support multiple implementations and configuration arguments as detailed in our manuscript. The code for each biomarker also specificies the supported options.
  • VascX supports region-aware measurements, relative to the optic disc - fovea axis. If the region is not visible, the biomarker is not computed.
  • VascX can generate visualisations for every biomarker.

Attribution

If you use VascX, please consider citing our open access papers / preprints:

Installation

To install the entire fundus analysis pipeline including fundus preprocessing, model inference code and vascular biomarker extraction:

  1. Create a virtual environment, or otherwise ensure a clean environment.

  2. Install torch and torchvision that match your cuda environment. For example:

pip install torch torchvision torchaudio  # pip and CUDA 12
conda install pytorch torchvision torchaudio pytorch-cuda=12.1 -c pytorch -c nvidia # conda and CUDA 12
pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118 # pip and CUDA 11
conda install pytorch torchvision torchaudio pytorch-cuda=11.8 -c pytorch -c nvidia # conda and CUDA 11
  1. Install VascX and retinalysis-inference (for model inference):
pip install retinalysis-vascx retinalysis-inference

Usage

To run the two stages of VascX on a folder with input CFIs:

vascx run-models <PATH_TO_IMAGES> <PATH_TO_FOLDER_FOR_SEGMENTATIONS>
vascx calc-biomarkers <PATH_TO_FOLDER_FOR_SEGMENTATIONS> <PATH_TO_BIOMARKERS_CSV> --feature_set full_v3 --n-jobs 8 

[!TIP] Use vascx run-models --help or vascx calc-biomarkers --help to get help.

  • PATH_TO_IMAGES should point to a folder with input CFIs in standard imaging formats. DICOM is supported via pydicom. Alternatively PATH_TO_IMAGES may be the path to a CSV file with id and path columns, and one image per row. If provided in this way, each path should be an absolute path to a valid image file and the corresponding unique id will be used to name the output and intermediate results for that image. This is useful when the source images are not all in a single folder.

    Sample CSV file with inputs

    If you chose to use a CSV file as input to run-models, the format should be the following:

    id,path
    drive_left_22,/path/to/samples/fundus/original/DRIVE_22.png
    drive_right_40,/path/to/samples/fundus/original/DRIVE_40.png
    chasedb_left_08,/path/to/samples/fundus/original/CHASEDB1_08L.png
    chasedb_right_12,/path/to/samples/fundus/original/CHASEDB1_12R.png
    hrf_green_04,/path/to/samples/fundus/original/HRF_04_g.jpg
    hrf_dr_07,/path/to/samples/fundus/original/HRF_07_dr.jpg
    

    Note that paths should be absolute and IDs should be unique.

  • The folder PATH_TO_FOLDER_FOR_SEGMENTATIONS should initially be an empty or non-existent folder. The first stage (run-models) will write intermediate segmentations and other AI outputs to this folder.

  • The same folder should be passed to the second stage as input as shown above.

  • PATH_TO_BIOMARKERS should be the path to a CSV file where the final outputs will be stored. See below for details on how to interpret biomarker names.

  • The --feature_set is used to specify a set of biomarkers to extract. We recommend to use the latest "full" set which contains a comprehensive set.

  • The --n-jobs option indicates how many CPU jobs to start. For optimal performance, we recommend to set to the number of CPU threads available divided by two, as a rule of thumb.

  • By default, run-models picks the first available CUDA GPU, Apple MPS acceleration, or CPU. Override this with --device, passing any string accepted by torch.device (for example cuda:0, mps, or cpu).

  • An optional --logile PATH_TO_LOGFILE_TXT can be added to store logs. This can indicate if the code produces errors on some of the images.

Example

For example, to run VascX on the provided samples folder in our git repository:

git clone git@github.com:Eyened/retinalysis-vascx.git rtnls_vascx
cd rtnls_vascx
vascx run-models ./samples/fundus/original/ ./samples/fundus/segmentations
vascx calc-biomarkers ./samples/fundus/segmentations ./samples/fundus/biomarkers.csv --feature_set full_v3 --n-jobs 8

Outputs

vascx calc-biomarkers will write a CSV file at PATH_TO_BIOMARKERS containing a row per image and a column per biomarker. VascX parameter names typically follow the format:

[AGGREGATION]_[BIOMARKER]_[PARAMETERS]_[REGION]_[LAYER]

For example, for median_tort_dist_etdrs_arteries:

AGGREGATION = median - the vessel segments are aggregated using the median
BIOMARKER = tort (Tortuosity)
PARAMETERS = dist - this is the distance implementation of tortuosity
REGION = measured over the full ETDRS grid
LAYER = arteries - arterial biomarker - as opposed to veins or vessels (combined)

Some of the tokens may be ommited if they match defaults or if they do not apply. A CSV mapping between the biomarker names in the ouput CSV and more friendly display names can be generated with:

vascx write-mapping --feature_set full_v3 PATH_TO_MAPPING_CSV

vascx run-models will write segmentations and other model predictions to PATH_TO_FOLDER_FOR_SEGMENTATIONS, with the following structure:

/path/to/segmentations
  - preprocessed_rgb/ - preprocessed fundus images
  - artery_vein/ - artery-vein model segmentations
  - vessels/ - vessel model segmentations
  - disc/ - optic disc model segmentations
  - overlays/ - optional overlays showing the segmentations
  - bounds.csv - contains the bounds of the fundus image
  - fovea.csv - model predictions of the fovea locations for each image
  - quality.csv - model estimations of CFI quality

The folders above will contain images with matching filenames.

Notebooks

As an alternative, we also provide notebooks for running the three stages:

  1. Preprocessing. See this notebook. This step is CPU-heavy and benefits from parallelization (see notebook).

  2. Inference. See this notebook. All models can be ran in a single GPU with >10GB VRAM.

  3. Feature extraction. See this notebook. This step is CPU-heavy again and benefits from parallelization (see notebook). Advanced users who need to pass custom parameters per image can use the alternative advanced feature extraction notebook.

  4. Post-processing and normalisation. See our sample post-processing notebook showing how to post-process, and optionally normalise VascX exports.

[!IMPORTANT]

Biomarker post-processing and normalisation

While several VascX biomarkers such as tortuosity are unitless, others such as vessel calibers or CREs measure distances. By default, VascX reports these distance measurements in pixels. If an explicit per-image mm_per_pixel value is provided, VascX uses it for ETDRS grid scaling and multiplies caliber and CRE biomarkers into millimeters. Some derived biomarkers such as artery-vein ratio (AVR) may also be computed in a post-processing stage as the ratio between caliber or CRE (recommended) measurements. The sample post-processing notebook shows how to post-process and optionally normalise a VascX export.

Advanced Usage

Offline Usage

By default, vascx run-models loads model weights from the Hugging Face model repository. This is convenient on connected workstations, but it will fail on offline clusters unless the model files are provided locally.

For offline use, download the VascX model files on a machine with internet access, copy them to the cluster, and arrange them in this directory layout:

/path/to/vascx-models/
  - quality/
    - quality.pt
  - artery_vein/
    - av_july24.pt
  - vessels/
    - vessels_july24.pt
  - disc/
    - disc_july24.pt
  - fovea/
    - fovea_july24.pt

Then pass that directory to run-models:

vascx run-models /path/to/images /path/to/segmentations --model-dir /path/to/vascx-models

You can also set the model directory once in the environment:

export VASCX_MODEL_DIR=/path/to/vascx-models
vascx run-models /path/to/images /path/to/segmentations

For more control, provide individual model files directly with options --quality-model, --av-model, --vessels-model, --disc-model and --fovea-model. Per-model options take precedence over --model-dir and VASCX_MODEL_DIR.

Skipping steps during re-execution

The run-models command has several --no-* flags that are intended for re-executing part of a pipeline after some outputs already exist. These flags skip work; they do not change where downstream steps look for inputs. If a later step needs an output from a skipped step, that output must already be present in the same OUTPUT_PATH layout.

For example, to reuse images that were already preprocessed, place them in the output folder before running run-models and pass --no-preprocess:

/path/to/segmentations
  - preprocessed_rgb/
    - image_001.png
    - image_002.png
vascx run-models ./samples/fundus/original/ /path/to/segmentations --no-preprocess

In --no-preprocess mode, model inputs are read from /path/to/segmentations/preprocessed_rgb/*.png. Files must be RGB PNG images named <id>.png; the <id> filename stem is used for all downstream outputs, such as artery_vein/<id>.png, vessels/<id>.png, disc/<id>.png, and rows in fovea.csv and quality.csv. These images should have the same preprocessed format produced by VascX/fundusprep, namely a square, model-ready fundus crop, normally 1024x1024 pixels.

DATA_PATH is still required to be an existing path for CLI compatibility, but when --no-preprocess is used its contents are not used as model inputs. If you plan to run calc-biomarkers on the output folder, keep or provide the matching bounds.csv from preprocessing as well.

The other skip flags follow the same idea:

  • --no-vessels skips vessel and artery-vein segmentation. Reuse this only when vessels/<id>.png and artery_vein/<id>.png already exist for the IDs you need downstream.
  • --no-disc skips optic disc segmentation. Reuse this only when disc/<id>.png already exists for the IDs you need downstream.
  • --no-fovea skips fovea detection. Reuse this only when fovea.csv already exists if you will create overlays or run biomarker extraction.
  • --no-quality skips image quality estimation. Reuse this when quality.csv already exists or when you do not need quality predictions.
  • --no-overlay skips overlay creation. Reuse this when overlays already exist or when visualization overlays are not needed.

Only models for enabled steps are required. For example, a run with --no-quality --no-fovea does not need quality/quality.pt or fovea/fovea_july24.pt. If no local model options are provided, VascX keeps using the default Hugging Face model locations.

Advanced extraction

For most datasets, the standard feature extraction notebook and vascx calc-biomarkers command are the simplest way to compute biomarkers. Advanced users can create Retina objects manually when they need to pass custom parameters per image, such as an explicit scaling factor mm_per_pixel from an acquisition platform. The advanced feature extraction notebook is an alternative to 2_feature_extraction.ipynb that demonstrates this using the sample folder.

mm_per_pixel is interpreted as millimeters per pixel. When provided, VascX uses it to scale some grids defined in physical units (eg. ETDRS grid), and to convert caliber and CRE outputs from pixels to millimeters. Unitless features, densities, angles, and tortuosity values are not multiplied. If no mm_per_pixel value is provided, VascX keeps the legacy behavior: grid scaling is derived from the optic-disc to fovea distance using the 4.75 mm assumption, and caliber/CRE outputs remain in pixels. For full feature sets, also provide the ROI mask or preprocessing bounds; the advanced notebook shows how to convert VascX preprocessing bounds into an ROI mask.

Code changes

We welcome advanced users who want to do one of:

  • Implement new biomarkers or variants of them
  • Develop custom feature sets
  • Contribute to the code in any other way

For that we recommend to clone VascX repo and install in development mode (-e flag) in a clean environment:

git clone git@github.com:Eyened/retinalysis-vascx.git rtnls_vascx
cd rtnls_vascx
pip install -e .

After that, you are able to locally:

  • Change or add biomarker implementations by working on the features folder. Biomarkers must
  • Change or add "feature sets", or sets of biomarkers to extract from an image by working on the feature_sets folder.
  • Keep using the extraction notebooks and the vascx calc-biomarkers command to run experiments.

Implementation

VascX processes vessel segmentations through four main stages, each producing different data representations:

  • Input masks: np.ndarray[bool] per layer; optic disc and fovea metadata from segmentation models.

  • Stage 1 - Binary/skeleton:

    • binary: filled vessel mask after disc masking
    • binary_nodisc: vessel mask without disc region
    • skeleton: skeletonized vessel centerlines using skimage skeletonization
  • Stage 2 - Undirected graph:

    • NetworkX Graph with skeleton pixels as nodes
    • Segment objects stored on edges containing skeleton points and geometric properties
    • Each segment represents a vessel segment between junction points
  • Stage 3 - Directed digraph:

    • NetworkX DiGraph with flow direction from optic disc outward
    • trees: root nodes representing vessel trees emanating from disc
    • nodes: Endpoint and Bifurcation objects with spatial positions
    • segments: directed vessel segments with computed properties (diameter, length, etc.)
  • Stage 4 - Resolved vessels:

    • Merged vessel graph after running vessel resolution algorithm
    • resolved_segments: final vessel segments after merging short segments
    • Segment-to-pixel mapping for spatial feature computation

Biomarker families use different representations: mask-based features use binary; topology features use digraph and nodes; morphological features use segments with computed diameters; spatial features use segment-to-pixel mappings.

Biomarkers

VascX computes retinal vascular biomarkers from standardized representations (binary masks, undirected/directed graphs, resolved vessels). Below we describe each feature with the exact quantity being estimated and the equations used. Throughout, B denotes the stage‑1 binary vessel mask, S the set of eligible directed segments with lengths (\ell_i), and R an analysis region of interest; cardinalities count pixels, and distances are in pixels unless noted.

VascularDensity. The fraction of retinal area occupied by vessels in R, computed on the binary mask B:

$$ D = \frac{|B \cap R|}{|R|}. $$

BifurcationCount. The count of branching points in the directed graph (stage‑3). Let $\mathcal{B}$ be the set of bifurcation nodes with positions $p_b$:

$$ C = \sum_{b \in \mathcal{B}} \mathbf{1}[p_b \in R]. $$

BifurcationAngles. For each bifurcation $b$ at position $p_b$, outgoing branch directions are estimated by sampling the branches' splines at distance $\delta$ from the node along each branch at points $q_1$ and $q_2$. Unit vectors $(u_b, v_b)$ are defined from the bifurcation point to the sample points:

$$ u_b = \frac{q_1 - p_b}{|q_1 - p_b|}, \quad v_b = \frac{q_2 - p_b}{|q_2 - p_b|}, $$

and the bifurcation angle is defined as the angle between these vectors:

$$ \theta_b = \arccos(u_b \cdot v_b), \quad \theta_b \in [0^\circ, 180^\circ]. $$

Angles exceeding 160° are discarded as non-bifurcating continuations. Summary statistics (e.g., mean/median) are reported across valid nodes.

Caliber. For each segment $i$, diameters are sampled along a spline fitted to its skeleton by projecting spline normals to the vessel boundary on B. The per‑segment diameter is the median along its arclength. The reported caliber aggregates over eligible segments (length $\ell_i \ge \ell_{\min}$):

$$ C = g\big({ d_i : i \in S }\big), $$

where (g) is a robust statistic (typically the median).

Tortuosity. Three complementary measures are provided per segment (or per resolved vessel). Let $L_{\text{arc},i}$ be arclength and $L_{\text{chord},i}$ the end‑to‑end Euclidean distance.

  • Distance factor:

$$ T_i^{\text{DF}} = \frac{L_{\text{arc},i}}{L_{\text{chord},i}}. $$

  • Curvature‑based measure, using planar curvature $\kappa_i(s)$ and OD–fovea distance $d_{ODF}$ for scale normalization:

$$ T_i^{\kappa} = \frac{1}{L_{\text{arc},i}} \int_0^{L_{\text{arc},i}} \big|\kappa_i(s)\big| , ds ; \cdot ; d_{ODF}. $$

  • Inflection count (number of curvature sign changes along the centerline):

$$ T_i^{\text{INF}} = N^{(i)}_{\text{inflections}}. $$

When reporting a single score over multiple segments, length‑weighted aggregation may be used for normalization:

$$ T_{\text{tot}} = \sum_{i \in S} \left( \frac{\ell_i}{\sum_{j \in S} \ell_j} \right) t_i, $$

with $t_i$ any of the measures above.

CRE (Central Retinal Equivalents). Concentric circles centered at the optic disc are intersected with the vessel network. At each radius $r$, up to $M$ crossings with the largest segment median diameters are retained and recursively reduced via the Hubbard rule with a modality‑dependent constant $c$ (arteries: 0.88; veins: 0.95):

$$ d \leftarrow c,\sqrt{d_1^2 + d_2^2} $$

applied pairwise until a single equivalent caliber $d_r$ remains. The final CRE is the median of ${d_r}$ across radii.

TemporalAngle. On each concentric circle of radius (r), the two dominant temporal vessels are identified by diameter and spatial continuity. The angle at the disc center is

$$ \theta_r = \angle\big(\overline{OD,p_1(r)},, \overline{OD,p_2(r)}\big), $$

and the reported value is the median over radii.

Sparsity. Let $\mathrm{DT}(x)$ represent the distance transform over $R$, ie. the normalized Euclidean distance to the nearest vessel pixel (scaled by $d_{ODF}$). Over pixels in R we report either the mean or the largest local maximum:

$$ S_{\text{mean}} = \frac{1}{|R|} \sum_{x \in R} \mathrm{DT}(x), \qquad S_{\max} = \max_{x \in R \cap \text{local maxima}} \mathrm{DT}(x). $$

VarianceOfLaplacian. For the fundus image $I$ (grayscale), compute the discrete Laplacian $L = \Delta I$. Image sharpness is summarized as the variance over R:

$$ Var{ L(x) : x \in R }. $$

DiscFoveaDistance. With optic disc center $c_{OD}$ and fovea position $p_f$,

$$ d_{ODF} = \lVert c_{OD} - p_f \rVert_2. $$

Feature localisation

VascX localises feature computations using anatomical references and predefined grids:

  • Anatomical anchoring

    • The optic disc mask and fovea position orient geometry (e.g., OD–fovea axis) and define a retinal mask.
    • All region masks are intersected with the retinal mask; features operate only on visible retina.
  • Predefined grids (rtnls_enface/rtnls_enface/grids)

    • EllipseGrid: ellipse centered midway between disc and fovea, major axis along OD–fovea. Fields: FullGrid, Superior, Inferior.
    • CircleGrid: disc–fovea–centered circle (radius derived from OD–fovea distance and disc size). Fields: FullGrid, Superior, Inferior.
    • ETDRSGrid: classic ETDRS layout with rings (Center, Inner, Outer), quadrants (Superior, Inferior, Nasal, Temporal, plus Left/Right), and subfields (CSF, SIM, NIM, TIM, IIM, SOM, NOM, TOM, IOM).
    • HemifieldGrid: superior/inferior half-planes split relative to the OD–fovea axis. Fields: FullGrid, Superior, Inferior.
    • DiscCenteredGrid: disc-anchored rings (inner, center, outer) and quadrants (superior, inferior, nasal, temporal, plus left/right), taking laterality into account.
  • Bounds and visibility (CFI bounds)

    • For a chosen field, the platform evaluates the fraction within bounds using grid_field_fraction_in_bounds (and grid_field_masks_and_fraction).
    • If the fraction in-bounds is too small (typically < 0.5), many features skip computation and return None to avoid out-of-frame bias.
    • Visualizers plot the requested field overlayed on the image; computations always respect in-bounds masking.

Ready-to-run feature sets are available under vascx/fundus/feature_sets (e.g., full, bergmann, quality) and can be selected by name when using extract_in_parallel. To generate feature descriptions alongside extraction:

Ready-to-run feature sets are available under vascx/fundus/feature_sets (e.g., full, bergmann, quality) and can be selected by name when using extract_in_parallel. To generate feature descriptions alongside extraction:

df = extract_in_parallel(examples, "full", n_jobs=8, descriptions_output_path="feature_descriptions_full.txt")

Testing

Run the standard test suite with:

pytest

The biomarker regression tests compare current outputs against stored reference files. When a change intentionally updates biomarker outputs, refresh those references with:

pytest --accept-vascx-reference -m reference

The full CLI end-to-end test is opt-in because it runs run-models on samples/fundus/original, downloads or uses cached Hugging Face model weights, and then runs calc-biomarkers. Run it with:

pytest --run-cli-e2e -m cli_e2e tests/test_cli_e2e.py

With tox, pass the pytest arguments after --:

tox -- --run-cli-e2e -m cli_e2e tests/test_cli_e2e.py

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

retinalysis_vascx-1.2.0.tar.gz (49.3 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

retinalysis_vascx-1.2.0-py3-none-any.whl (107.7 kB view details)

Uploaded Python 3

File details

Details for the file retinalysis_vascx-1.2.0.tar.gz.

File metadata

  • Download URL: retinalysis_vascx-1.2.0.tar.gz
  • Upload date:
  • Size: 49.3 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.12

File hashes

Hashes for retinalysis_vascx-1.2.0.tar.gz
Algorithm Hash digest
SHA256 24d33e6ee43572f439eac1bb453e4946699815d88a011ba6e5b0d8aefd57a031
MD5 5cb2ca2e521b64624f66d7911d07803d
BLAKE2b-256 5166a1fe467e29b5d28cd6b00b9a7c52a015ea1da0d8f1445b9e60b2d2d3f26c

See more details on using hashes here.

File details

Details for the file retinalysis_vascx-1.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for retinalysis_vascx-1.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e4629cd329e51a55485af0dc19a4d55b66827faec732ebd948a565b2e59cb06e
MD5 3ccd11385c63f93aa9d12a1a4ea10164
BLAKE2b-256 b5d0fac604053942504d9989580632c7482b0e8c7048ad19b3de47c0a9a450d6

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page