Skip to main content

Getting Started with flip-utils

About flip-utils

flip-utils is the pip-installable distribution published from this repository. Its Python import package is flip, which contains the shared platform logic used by FLIP jobs and services including core training logic, NVFLARE components, Flower helpers, and utility helpers.

The FLIP platform uses this package to power federated learning applications across multiple job types: standard federated training, distributed evaluation, diffusion model training, and custom federated optimization.

Installation

Install the published package from PyPI:

pip install flip-utils
# or with uv
uv add flip-utils

To use the latest development version, clone the monorepo and install from source:

git clone https://github.com/londonaicentre/FLIP.git
cd FLIP/flip-utils
uv sync
# or
pip install .

To build a distributable wheel for development:

uv build

Package Structure & Modules

The flip package is organized into logical modules:

flip.core

Core classes and abstractions:

  • FLIPBase — Abstract base class with common FL logic

  • FLIPStandardProd — Production implementation using FLIP platform APIs

  • FLIPStandardDev — Development implementation using local CSV/filesystem

  • FLIP() factory — Automatically selects the correct implementation based on environment

flip.constants

Configuration and enumerations:

  • FlipConstants — Pydantic-settings configuration singleton

  • ResourceType — Enum for imaging resource types (DICOM, NIFTI, etc.)

  • ModelStatus — Enum for model training states

  • JobType — Enum for supported FL job types

  • PTConstants — PyTorch-specific constants and settings

flip.utils

Utility helpers:

  • Utils — General utility functions

  • model_weights_handling — Model weight aggregation and manipulation

flip.nvflare

NVFLARE-specific components:

  • controllers/ — Workflow controllers (ScatterAndGather, CrossSiteModelEval, etc.)

  • components/ — Event handlers, persistors, privacy filters, model locators, etc.

  • recipes/ — High-level NVFLARE job recipes

  • runtime.py — Runtime helpers for NVFLARE apps

  • metrics.py — Metrics collection and reporting

  • site_policy.py — Renders the trust’s site privacy policy at fl-client start (python -m flip.nvflare.site_policy)

flip.flower

Flower-specific helpers:

  • strategy.py — FlipFedAvg (FedAvg with FLIP hub telemetry and optional best-model selection) and min_clients_from_run_config

  • privacy.py — flip_local_dp_mod, the client-side local differential-privacy mod

  • selection.py — BestModelSelector, best-model tracking across rounds

  • metrics.py — Server-side metrics collection and reporting for Flower runs

  • progress.py — Progress/status reporting helpers for Flower runs

Using the FLIP Factory

The FLIP() factory automatically selects between development and production implementations based on the LOCAL_DEV environment variable:

from flip import FLIP

# Uses FLIPStandardProd in production or FLIPStandardDev in local dev
flip = FLIP()
df = flip.get_dataframe(project_id, query)

See the API reference for detailed method documentation.

Job Types

Pass the job type to the FLIP() factory (FLIP(job_type=...)). The JobType enum (flip.constants.job_types) defines the values recognised by FLIP():

Type

Description

standard

Federated training with FedAvg aggregation (default)

evaluation

Distributed model evaluation without training

diffusion_model

Two-stage training: VAE encoder followed by diffusion model training

fed_opt

Custom federated optimization with flexible aggregation strategies

The NVFLARE backend additionally ships a template directory under fl-apps/nvflare/ for each Client-API job type (standard, evaluation, diffusion_model, fed_opt); the template names match the JobType enum values above. The Flower backend ships its own standard and evaluation templates under fl-apps/flower/ — selected at the deploy layer by FL_BACKEND=flower.

Data Enrichment (flip.xnat)

flip.xnat uploads data-enrichment files — segmentation masks and other image-derived annotations — into a FLIP project’s XNAT, so that supervised apps find a label beside each pulled image. Labels that already exist in OMOP (a lab result, a coded report finding) do not need this: project them as a column of the cohort query instead.

Unlike the rest of this package, flip.xnat does not run in the FL client. It runs on the model developer’s workstation inside the Trust network (or as an XNAT Container Service job), authenticated as their own XNAT account; FL clients hold no XNAT credentials. It is the write-side counterpart to get_by_accession_number(), which reads the same scan resource.

A flip-xnat console script is installed with the package:

export XNAT_HOST=https://xnat.trust.example
export XNAT_USER=your-username
export XNAT_PASS=your-password

flip-xnat upload --flip-project-id <project-uuid> --manifest manifest.csv --dry-run

The manifest is a CSV of accession_id,file_path (plus an optional target_filename, which must be a bare filename). By default each uploaded file is named after the image already in the scan’s NIFTI resource, swapping the input_ prefix for label_, which is the pairing the apps rely on. Existing files are never replaced unless --overwrite is passed.

Every Trust in the project needs enriching — each Trust’s XNAT holds only its own studies — so repeat --credentials-file to cover the roster in one run:

flip-xnat upload --flip-project-id <project-uuid> --manifest manifest.csv \
  --credentials-file gstt.json --credentials-file kch.json

The same manifest goes to every Trust; an accession exists at exactly one site and the others report it as no matching scan. A run that resolves no destination anywhere exits non-zero, so an automated pipeline cannot mistake a wholly-skipped enrichment for a completed one; pass --allow-no-op when an empty run is genuinely expected. A roster where no Trust holds the project is the one case --allow-no-op does not cover: the image pull never ran there, so the run fails regardless.

The same operations are available as a Python API:

from flip.xnat import XnatClient, read_manifest, run_enrichment

clients = [XnatClient.from_config_file("gstt.json"), XnatClient.from_config_file("kch.json")]
report = run_enrichment(clients, read_manifest("manifest.csv"), flip_project_id="<project-uuid>")
print(report.render())
raise SystemExit(report.exit_code())

Run it only after the image pull and after DICOM-to-NIfTI conversion: the target filename is derived from the converted image, so running earlier skips every scan.

User Application Requirements

The job components dynamically import user-provided code from the job’s custom/ directory. On the platform that directory is assembled by the FL API, which merges the uploaded app files onto the matching fl-apps/nvflare/<template>/app template; in local SimEnv runs the tutorial’s job.py stages its app_files/ into the job’s custom/ directly.

File

Description

trainer.py

Training logic — a plain nvflare.client script

validator.py

Extra validation module where the job type requires one

evaluator.py

Evaluation logic — required by the evaluation job type

models.py

Model definitions — must export get_model() function

config.json

Job configuration — only job_type is required; platform keys such as LOCAL_ROUNDS are defaulted when absent, and app settings such as LEARNING_RATE are passed through untouched

transforms.py

Data transforms (optional)

Development Mode

To test FL applications locally before deploying to production:

  1. Set environment variables in .env.development:

    LOCAL_DEV=true
    DEV_IMAGES_DIR=../data/accession-resources
    DEV_DATAFRAME=../data/sample_get_dataframe.csv
  2. Place your application files in the tutorial’s app_files/ directory (e.g. fl-tutorials/nvflare/image_classification/xray_classification/app_files/). On the platform they are merged onto the matching fl-apps/nvflare/<template>/app/ template at submit time.

  3. Run one of the shipped tutorials against the NVFLARE simulator from the repository root:

    make -C fl-tutorials run-tutorial TUTORIAL=xray_classification
    # list every available tutorial with:
    make -C fl-tutorials list-tutorials

    Each tutorial’s make run delegates to make sim — its job.py driving a FLIP recipe on the NVFLARE simulator (SimEnv) from the flip-utils venv — configured per-tutorial via that tutorial’s .env.app.

Running Tests

Run unit tests for the flip package:

make unit-test
# or
uv run pytest -s -vv

Tests use pytest with coverage reporting and are located in tests/unit/.

Building the Docs Locally

flip-utils is documented as part of the FLIP documentation. From the repository root, run:

cd docs && make docs

The generated HTML site will be written to docs/build/html. To clean previous builds:

cd docs && make clean

How the API Reference is Generated

The API reference is built with sphinx-autoapi and points directly at the flip/ source tree. That keeps the reference pages aligned with the code without maintaining hand-written module stubs. See the API Reference section of the built documentation for complete coverage of all public classes and functions.

Metadata

Release files for flip-utils 0.7.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for flip-utils 0.7.0
File Size Uploaded
flip_utils-0.7.0.tar.gz 139.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for flip-utils 0.7.0
File Interpreter ABI Platform
flip_utils-0.7.0-py3-none-any.whl Python 3 none any Details

Total release size: 333.3 kB

Release files / flip_utils-0.7.0.tar.gz

Download URL flip_utils-0.7.0.tar.gz
Size 139.4 kB
Tags Source
SHA-256 checksum
How to use checksums
f2e0e908b4399701148d6f1168d0c0964fb7e7b568c8126e85d3f574b7ad6902
BLAKE2b-256 checksum
How to use checksums
c3e9f3366ffc361a5f142e42d6febcb988ec33fc69cd2544d8e80263babcfc8e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.12.17 {"installer":{"name":"uv","version":"0.12.17","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / flip_utils-0.7.0-py3-none-any.whl

Download URL flip_utils-0.7.0-py3-none-any.whl
Size 193.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
8dfbb84c047faa7d77033be555c885861af71b76d6f2fe32e13e5b2e9da9d218
BLAKE2b-256 checksum
How to use checksums
bd18daf108c5eec3763342a2f2473b6de6a1ea8f8141f2ccde1cff1a2e8895a0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.12.17 {"installer":{"name":"uv","version":"0.12.17","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release history Release notifications | RSS feed

0.11.0

2 release files

0.10.0

2 release files

0.9.0

2 release files

0.8.0

2 release files

This release

0.7.0 This release

2 release files

0.6.0

2 release files

0.5.0

2 release files

0.4.1

2 release files

0.1.8

2 release files

0.1.7

2 release files

0.1.6

2 release files

0.1.5

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page