nvflare.app_opt.feature_election package
Submodules
Module contents
Feature Election for NVIDIA FLARE
A plug-and-play horizontal federated feature selection framework for tabular datasets.
This module provides: - FeatureElection: High-level API for feature election - FeatureElectionController: Server-side FLARE controller - FeatureElectionExecutor: Client-side FLARE executor - Helper functions for quick deployment
Example
Basic usage:
from nvflare.app_opt.feature_election import quick_election
import pandas as pd
df = pd.read_csv("data.csv")
selected_mask, stats = quick_election(
df=df,
target_col='target',
num_clients=4,
fs_method='lasso',
freedom_degree=0.3
)
FLARE deployment:
from nvflare.app_opt.feature_election import FeatureElection
fe = FeatureElection(freedom_degree=0.5, fs_method='lasso')
config_paths = fe.create_flare_job(
job_name="feature_selection",
output_dir="./jobs"
)
- class FeatureElection(freedom_degree: float = 0.5, fs_method: str = 'lasso', aggregation_mode: str = 'weighted', auto_tune: bool = False, tuning_rounds: int = 5, eval_metric: str = 'f1', wait_time_after_min_received: int = 10, fs_params: Dict | None = None)[source]
Bases:
objectHigh-level interface for Feature Election in NVIDIA FLARE. Simplifies integration with tabular datasets for federated feature selection.
This class provides: - Easy data preparation and splitting - Local simulation for testing - Result management and persistence
- apply_mask(X: DataFrame | ndarray) DataFrame | ndarray[source]
Apply global feature mask to new data.
- create_flare_job(job_name: str = 'feature_election', output_dir: str = 'jobs/feature_election', min_clients: int = 2, num_rounds: int = 5, client_sites: List[str] | None = None) Dict[str, str][source]
Generate FLARE job configuration.
- class FeatureElectionController(freedom_degree: float = 0.5, aggregation_mode: str = 'weighted', min_clients: int = 2, num_rounds: int = 5, task_name: str = 'feature_election', train_timeout: int = 300, auto_tune: bool = False, tuning_rounds: int = 0, wait_time_after_min_received: int = 10)[source]
Bases:
ControllerThree-phase FL controller for federated feature selection and FedAvg training.
Phase 1 — Local Feature Selection: each client runs its configured FS method and returns a feature mask and per-feature scores.
Phase 2 — Tuning & Global Mask Distribution: the server optionally runs hill-climbing to find the optimal
freedom_degree, then aggregates client masks via weighted voting and distributes the global feature mask to all clients. If fewer thanmin_clientsclients acknowledge the mask, the entire workflow is aborted.Phase 3 — FedAvg Training: standard federated averaging on the reduced feature set for
num_roundsrounds.- Parameters:
freedom_degree – Threshold in [0, 1] controlling which features survive the vote. 0 = intersection (all clients must select), 1 = union (any client suffices).
aggregation_mode –
'weighted'weights each client by sample count;'uniform'treats all clients equally.min_clients – Minimum number of clients that must respond in each phase.
num_rounds – Number of FedAvg training rounds in Phase 3.
task_name – Must match the
task_nameconfigured onFeatureElectionExecutor.train_timeout – Per-phase timeout in seconds.
auto_tune – If
True, Phase 2 runs hill-climbing to optimisefreedom_degree. Has no effect whentuning_rounds=0(a warning is logged in that case).tuning_rounds – Number of hill-climbing iterations. Must be >= 2 for meaningful tuning;
tuning_rounds=0disables tuning (with a warning ifauto_tune=True);tuning_rounds=1is also disabled (same warning).wait_time_after_min_received – Seconds to wait for additional client responses after
min_clientshave already replied. Set to0only for local simulation; a non-zero value (default 10 s) prevents slower clients from being silently excluded in heterogeneous production networks.
Controller logic for tasks and their destinations.
Must set_communicator() to access communication related function implementations.
- Parameters:
task_check_period (float, optional) – interval for checking status of tasks. Applicable for WFCommServer. Defaults to 0.2.
- advance_tuning(score: float, first_step: bool = False) None[source]
Record a tuning-round score and update freedom_degree for the next round.
This is the public interface for the simulation path in
FeatureElection.simulate_election()so that the simulation does not need to mutate private controller state directly. The real FL path incontrol_flowuses the same internal helpers.- Parameters:
score – Weighted evaluation score for the current
freedom_degree.first_step –
Trueonly on the very first tuning round; passed through to_calculate_next_fdto seed the initial direction.
- aggregate_selections(client_selections: Dict[str, Dict]) ndarray[source]
Aggregate feature selections from all clients.
Freedom degree controls the blend between intersection and union: - FD=0: Intersection (only features selected by ALL clients) - FD=1: Union (features selected by ANY client) - 0<FD<1: Weighted voting based on scores
- process_result_of_unknown_task(client: Client, task_name: str, client_task_id: str, result: Shareable, fl_ctx: FLContext)[source]
Called when a result is received for an unknown task. This is a fallback - normally results come through task_done_cb.
- start_controller(fl_ctx: FLContext) None[source]
Starts the controller.
This method is called at the beginning of the RUN.
- Parameters:
fl_ctx – the FL context. You can use this context to access services provided by the
example (framework. For)
your (you can get Command Register from it and register)
modules. (admin command)
- stop_controller(fl_ctx: FLContext)[source]
Stops the controller.
This method is called right before the RUN is ended.
- Parameters:
fl_ctx – the FL context. You can use this context to access services provided by the
example (framework. For)
your (you can get Command Register from it and unregister)
modules. (admin command)
- class FeatureElectionExecutor(fs_method: str = 'lasso', fs_params: Dict | None = None, eval_metric: str = 'f1', task_name: str = 'feature_election')[source]
Bases:
ExecutorClient-side executor for the Feature Election federated workflow.
Handles four request types dispatched by
FeatureElectionController:feature_selection— runs the configured FS method on local data and returns a boolean feature mask and per-feature scores.tuning_eval— evaluates a candidate mask proposed by the controller during the hill-climbing phase and returns the local score.apply_mask— permanently slicesX_train/X_valto the selected features. Idempotent: if the same mask is received a second time (e.g. due to task retransmission) the call returnsOKimmediately without modifying data.train— performs one FedAvg round on the masked feature set and returns the updated model weights.
- Parameters:
fs_method – Feature selection algorithm. One of
'lasso','elastic_net','mutual_info','random_forest','pyimpetus'.fs_params – Extra keyword arguments forwarded to the FS algorithm.
eval_metric –
'f1'(weighted) or'accuracy', used for tuning eval and local scoring.task_name – Must match the
task_nameonFeatureElectionController.
Note
Call
set_data()before the executor is registered with the FL runtime.FeatureElectionExecutorhas noclient_idattribute; usefl_ctx.get_identity_name()inside_load_data_if_neededto retrieve the site name assigned by the FL platform.Init FLComponent.
The FLComponent is the base class of all FL Components. (executors, controllers, responders, filters, aggregators, and widgets are all FLComponents)
FLComponents have the capability to handle and fire events and contain various methods for logging.
- evaluate_model(X_train, y_train, X_val, y_val, scaler=None) float[source]
Helper method to train and evaluate a model locally. Required for the ‘simulate_election’ functionality and tests.
- Parameters:
scaler – Optional pre-fitted
StandardScaler. When provided the data is transformed (not fit-transformed), ensuring the same normalisation parameters are used as those established on the same feature set by the caller. WhenNonea fresh scaler is fitted onX_train.
- execute(task_name: str, shareable: Shareable, fl_ctx: FLContext, abort_signal: Signal) Shareable[source]
Executes a task.
- Parameters:
task_name (str) – task name.
shareable (Shareable) – input shareable.
fl_ctx (FLContext) – fl context.
abort_signal (Signal) – signal to check during execution to determine whether this task is aborted.
- Returns:
An output shareable.
- quick_election(df: DataFrame, target_col: str, num_clients: int = 3, freedom_degree: float = 0.5, fs_method: str = 'lasso', split_strategy: str = 'stratified', **kwargs) Tuple[ndarray, Dict][source]
Quick Feature Election for tabular data (one-line solution).
**kwargsare routed to eitherFeatureElectionorFeatureElection.prepare_data_splits()based on the parameter name. Recognised split parameters:split_ratios,random_state,dirichlet_alpha. All other kwargs are forwarded toFeatureElection(e.g.aggregation_mode,auto_tune,fs_params).