Skip to content

Core SNM Functionality

This section covers the core set of functions and classes that implement the main spectral normative modeling algorithms and concepts. This includes classes for fitting univariate and multivariate (spectral) normative models, as well as classes used for defining covariates and how they influence the model.


spectranorm.snm

snm.py

Core implementation of spectral normative modeling (SNM).

This module provides the code base for using spectral normative models. It can be used to fit direct and spectral normative models to data and also to predict normative centiles using pre-trained models.

See full documentation at: https://sina-mansour.github.io/spectranorm

CovarianceModelSpec dataclass

General specification of a normative model for covariance.

This model aims to learn the relationships between two variables of interest for both of which a normative model is specified. This can capture patterns where the normative trends in two variables are related.

Attributes:

Name Type Description
variable_of_interest_1 str

str Name of the first variable of interest.

variable_of_interest_2 str

str Name of the second variable of interest.

covariates list[CovariateSpec]

list[CovariateSpec] Listing all model covariates and specifying how each covariate is modeled.

influencing_covariance list[str]

list[str] List of covariate names that influence the covariance between the two variables of interest.

Source code in src/spectranorm/snm.py
@dataclass
class CovarianceModelSpec:
    """
    General specification of a normative model for covariance.

    This model aims to learn the relationships between two variables of interest for
    both of which a normative model is specified. This can capture patterns where the
    normative trends in two variables are related.

    Attributes:
        variable_of_interest_1: str
            Name of the first variable of interest.
        variable_of_interest_2: str
            Name of the second variable of interest.
        covariates: list[CovariateSpec]
            Listing all model covariates and specifying how each covariate is modeled.
        influencing_covariance: list[str]
            List of covariate names that influence the covariance between the two
            variables of interest.
    """

    variable_of_interest_1: str
    variable_of_interest_2: str
    covariates: list[CovariateSpec]
    influencing_covariance: list[str]

    def __post_init__(self) -> None:
        if not isinstance(self.variable_of_interest_1, str):
            err = "variable_of_interest_1 must be a string."
            raise TypeError(err)
        if not isinstance(self.variable_of_interest_2, str):
            err = "variable_of_interest_2 must be a string."
            raise TypeError(err)
        if not isinstance(self.covariates, list):
            err = "covariates must be a list of CovariateSpec instances."
            raise TypeError(err)
        if not all(isinstance(cov, CovariateSpec) for cov in self.covariates):
            err = "All items in covariates must be CovariateSpec instances."
            raise TypeError(err)
        if not isinstance(self.influencing_covariance, list):
            err = "influencing_covariance must be a list of covariate names."
            raise TypeError(err)

CovarianceNormativeModel dataclass

Covariance normative model implementation.

This class implements covariance normative modeling, which models the covariance structure between a pair of variables as a normative random variable.

Attributes:

Name Type Description
spec CovarianceModelSpec

CovarianceModelSpec Specification of the covariance model including variables of interest, and list of covariates.

defaults dict[str, Any]

dict Default parameters for the model, including spline specifications, ADVI iterations, convergence tolerance, random seed, and Adam optimizer learning rates.

Source code in src/spectranorm/snm.py
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
@dataclass
class CovarianceNormativeModel:
    """
    Covariance normative model implementation.

    This class implements covariance normative modeling, which models the covariance
    structure between a pair of variables as a normative random variable.

    Attributes:
        spec: CovarianceModelSpec
            Specification of the covariance model including variables of interest,
            and list of covariates.
        defaults: dict
            Default parameters for the model, including spline specifications,
            ADVI iterations, convergence tolerance, random seed, and Adam optimizer
            learning rates.
    """

    spec: CovarianceModelSpec
    defaults: dict[str, Any] = field(
        default_factory=lambda: {
            "spline_df": DEFAULT_SPLINE_DF,
            "spline_degree": DEFAULT_SPLINE_DEGREE,
            "spline_extrapolation_factor": DEFAULT_SPLINE_EXTRAPOLATION_FACTOR,
            "advi_iterations": DEFAULT_ADVI_ITERATIONS,
            "advi_convergence_tolerance": DEFAULT_ADVI_CONVERGENCE_TOLERANCE,
            "random_seed": DEFAULT_RANDOM_SEED,
            "adam_learning_rate": DEFAULT_ADAM_LEARNING_RATE,
            "adam_learning_rate_decay": DEFAULT_ADAM_LEARNING_RATE_DECAY,
        },
    )

    def __repr__(self) -> str:
        """
        String representation of the CovarianceNormativeModel instance.
        """
        return f"CovarianceNormativeModel(\n\tspec={self.spec}\n)"

    @classmethod
    def from_direct_model(
        cls,
        direct_model: DirectNormativeModel,
        variable_of_interest_1: str,
        variable_of_interest_2: str,
        influencing_covariance: list[str] | None = None,
        defaults_overwrite: dict[str, Any] | None = None,
    ) -> CovarianceNormativeModel:
        """
        Initialize the model from a direct model instance, and two variable names.

        Args:
            direct_model: DirectNormativeModel
                This model will be used to instantiate a similar covariance model.
            variable_of_interest_1: str
                Name of the first target variable to model.
            variable_of_interest_2: str
                Name of the second target variable to model.
            influencing_covariance: list[str] | None
                List of covariates that influence the covariance structure. If not
                provided, this will be copied from the direct model's
                `influencing_variance`.

        Returns:
            CovarianceNormativeModel
                An instance of CovarianceNormativeModel initialized with the provided
                data.
        """
        # Validity checks for input parameters
        if not isinstance(direct_model, DirectNormativeModel):
            err = "direct_model must be an instance of DirectNormativeModel."
            raise TypeError(err)
        if not (
            isinstance(variable_of_interest_1, str)
            and isinstance(variable_of_interest_2, str)
        ):
            err = "Variables of interest must be strings."
            raise TypeError(err)

        # Substitute influencing_covariance if not provided
        if influencing_covariance is None:
            influencing_covariance = direct_model.spec.influencing_variance

        # Use the same setup as the direct model
        model = cls(
            spec=CovarianceModelSpec(
                variable_of_interest_1=variable_of_interest_1,
                variable_of_interest_2=variable_of_interest_2,
                covariates=direct_model.spec.covariates,
                influencing_covariance=influencing_covariance,
            ),
        )

        # update defaults
        model.defaults.update(direct_model.defaults)
        model.defaults.update(defaults_overwrite or {})

        return model

    def _validate_model(self) -> None:
        """
        Validate the covariance model instance.

        This method checks if the model instance is complete and valid.
        It raises errors if any required fields are missing or if there are
        inconsistencies in the model instance.
        """
        if self.spec is None:
            err = (
                "Model specification is not set. Please initialize the model,"
                " e.g., with 'from_dataframe'."
            )
            raise ValueError(err)
        if len(self.spec.covariates) == 0:
            err = (
                "No covariates specified in the model. "
                "Please add covariates to the specification."
            )
            raise ValueError(err)
        if len(self.spec.influencing_covariance) == 0:
            err = (
                "No covariates specified to influence the covariance "
                "between the variables of interest."
            )
            raise ValueError(err)

    def save_model(self, directory: Path, *, save_posterior: bool = False) -> None:
        """
        Save the fitted model and it's posterior to a directory.
        The model will be saved in a subdirectory named 'saved_model'.
        If this directory is not empty, an error is raised.

        Args:
            directory: Path
                Path to a directory to save the model.
            save_posterior: bool (default=False)
                If True, save the model's posterior trace inference data.
        """
        # Prepare the save directory
        directory = Path(directory)
        saved_model_dir = utils.general.prepare_save_directory(directory, "saved_model")

        model_dict = {
            "spec": self.spec,
            "defaults": self.defaults,
        }
        if hasattr(self, "model_params"):
            model_dict["model_params"] = self.model_params
            if hasattr(self, "model_inference_data") and save_posterior:
                self.model_inference_data.to_netcdf(
                    saved_model_dir / "model_inference_data.nc",
                )
        joblib.dump(model_dict, saved_model_dir / "model_dict.joblib")

    @classmethod
    def load_model(
        cls,
        directory: Path,
        *,
        load_posterior: bool = False,
    ) -> CovarianceNormativeModel:
        """
        Load the model and its posterior from a directory.
        The model will be loaded from a subdirectory named 'saved_model'.

        Args:
            directory: Path
                Path to the directory containing the model.
            load_posterior: bool (default=False)
                If True, load the model's posterior trace from the saved inference data.
        """
        # Validate the load directory
        directory = Path(directory)
        saved_model_dir = utils.general.validate_load_directory(
            directory,
            "saved_model",
        )

        # Load the saved model dict
        model_dict = joblib.load(saved_model_dir / "model_dict.joblib")

        # Create an instance of the class
        instance = cls(
            spec=model_dict["spec"],
        )

        # Set the attributes from the loaded model dictionary
        instance.defaults.update(model_dict["defaults"])
        if "model_params" in model_dict:
            instance.model_params = model_dict["model_params"]
            if load_posterior:
                instance.model_inference_data = az.from_netcdf(  # type: ignore[no-untyped-call]
                    saved_model_dir / "model_inference_data.nc",
                )

        return instance

    def _validate_dataframe_for_fitting(self, train_data: pd.DataFrame) -> None:
        """
        Validate the training DataFrame for fitting.
        """
        utils.general.validate_dataframe(
            train_data,
            (
                [cov.name for cov in self.spec.covariates]
                + [
                    self.spec.variable_of_interest_1,
                    self.spec.variable_of_interest_2,
                    f"{self.spec.variable_of_interest_1}_mu_estimate",
                    f"{self.spec.variable_of_interest_2}_mu_estimate",
                    f"{self.spec.variable_of_interest_1}_std_estimate",
                    f"{self.spec.variable_of_interest_2}_std_estimate",
                ]
            ),
        )

    def _build_model_coordinates(
        self,
        observations: npt.NDArray[np.integer[Any]],
    ) -> dict[str, Any]:
        """
        Build the model coordinates for the training DataFrame.
        """
        # Data coordinates
        model_coords = {"observations": observations, "scalar": [0]}

        # Additional coordinates for covariates
        for cov in self.spec.covariates:
            if cov.cov_type == "numerical":
                if cov.effect == "spline":
                    if cov.spline_spec is not None:  # to satisfy type checker
                        model_coords[f"{cov.name}_splines"] = np.arange(
                            cov.spline_spec.df,
                        )
                elif cov.effect == "linear":
                    model_coords[f"{cov.name}_linear"] = np.arange(1)
            elif cov.cov_type == "categorical":
                model_coords[cov.name] = cov.categories
            else:
                err = f"Invalid covariate type '{cov.cov_type}' for '{cov.name}'."
                raise ValueError(err)
        return model_coords

    def _model_linear_correlation_effect(
        self,
        train_data: pd.DataFrame,
        cov: CovariateSpec,
        effects_list: list[TensorVariable],
        sigma_prior: float = 0.1,
        adapt: dict[str, Any] | None = None,
    ) -> None:
        """
        Model a linear effect for a numerical covariate.
        """
        # Linear effect
        if adapt is None:  # Model fitting
            linear_beta = pm.Normal(
                f"linear_beta_{cov.name}",
                mu=0,
                sigma=sigma_prior,
                size=1,
                dims=(f"{cov.name}_linear",),
            )
            # Increment parameter count for linear effect
            self.model_params["n_params"] += 1
        else:  # Freeze during adaptation/fine-tuning
            linear_beta = pm.Deterministic(
                f"linear_beta_{cov.name}",
                pt.as_tensor_variable(
                    adapt["pretrained_model_params"]["posterior_means"][
                        f"linear_beta_{cov.name}"
                    ],
                ),
                dims=(f"{cov.name}_linear",),
            )
        if cov.moments is not None:  # to satisfy type checker
            effects_list.append(
                (
                    (
                        (cast("npt.NDArray[Any]", train_data[cov.name].to_numpy()))
                        - cov.moments[0]
                    )
                    / cov.moments[1]
                )
                * linear_beta,
            )

    def _model_spline_correlation_effect(
        self,
        train_data: pd.DataFrame,
        cov: CovariateSpec,
        effects_list: list[TensorVariable],
        spline_bases: dict[str, npt.NDArray[np.floating[Any]]],
        sigma_prior: float = 1,
        adapt: dict[str, Any] | None = None,
    ) -> None:
        """
        Model a spline effect for a numerical covariate.
        """
        # Spline effect
        spline_bases[cov.name] = spline_bases.get(
            cov.name,
            cov.make_spline_bases(
                cast("npt.NDArray[Any]", train_data[cov.name].to_numpy()),
            ),
        )
        if adapt is None:  # Model fitting
            spline_betas = pm.ZeroSumNormal(
                f"spline_betas_{cov.name}",
                sigma=sigma_prior,
                shape=spline_bases[cov.name].shape[1],
                dims=(f"{cov.name}_splines",),
            )
            # Note ZeroSumNormal imposes a centering constraint
            # (ensuring identifiability)
            # Increment parameter count for spline effects
            if cov.spline_spec is not None:  # to satisfy type checker
                self.model_params["n_params"] += cov.spline_spec.df - 1
        else:  # Freeze during adaptation/fine-tuning
            spline_betas = pm.Deterministic(
                f"spline_betas_{cov.name}",
                pt.as_tensor_variable(
                    adapt["pretrained_model_params"]["posterior_means"][
                        f"spline_betas_{cov.name}"
                    ],
                ),
                dims=(f"{cov.name}_splines",),
            )
        effects_list.append(pt.dot(spline_bases[cov.name], spline_betas.T))

    def _model_categorical_correlation_effect(
        self,
        train_data: pd.DataFrame,
        cov: CovariateSpec,
        effects_list: list[TensorVariable],
        category_indices: dict[str, npt.NDArray[np.integer[Any]]],
        sigma_prior: float = 1,
        hierarchical_sigma_prior: float = 0.1,
        adapt: dict[str, Any] | None = None,
    ) -> None:
        """
        Model the effect of a categorical covariate.
        """
        # Factorize categories
        category_indices[cov.name] = category_indices.get(
            cov.name,
            cov.factorize_categories(
                cast("npt.NDArray[Any]", train_data[cov.name].to_numpy()),
            ),
        )
        if adapt is None:  # Model fitting
            if cov.hierarchical:
                # Hierarchical categorical effect
                # Hyperpriors for category (Bayesian equivalent of random effects)
                sigma_intercept_category = pm.HalfNormal(
                    f"sigma_intercept_{cov.name}",
                    sigma=sigma_prior,
                    dims=("scalar",),
                )

                # Hierarchical intercepts for each category (using reparameterized form)
                categorical_intercept_offset = pm.ZeroSumNormal(
                    f"intercept_offset_{cov.name}",
                    sigma=hierarchical_sigma_prior,
                    dims=(cov.name,),
                )
                # Note ZeroSumNormal imposes a centering constraint
                # (ensuring identifiability)
                categorical_intercept = pm.Deterministic(
                    f"intercept_{cov.name}",
                    (
                        categorical_intercept_offset
                        * pt.reshape(sigma_intercept_category, (1,))  # pyright: ignore[reportPrivateImportUsage]
                    ),
                    dims=(cov.name,),
                )

                # Increment parameter count for hierarchical intercept
                self.model_params["n_params"] += 1

            else:
                # Non-hierarchical (linear) categorical effect
                categorical_intercept = pm.ZeroSumNormal(
                    f"intercept_{cov.name}",
                    sigma=sigma_prior,
                    dims=(cov.name,),
                )
                # Note ZeroSumNormal imposes a centering constraint
                # (ensuring identifiability)
            # Increment parameter count for categorical effects
            if cov.categories is not None:  # to satisfy type checker
                self.model_params["n_params"] += len(cov.categories) - 1
        elif cov.name != adapt["covariate_to_adapt"]:
            # Freeze during adaptation/fine-tuning
            if cov.hierarchical:
                # Hierarchical categorical effect
                # Hyperpriors for category (Bayesian equivalent of random effects)
                sigma_intercept_category = pm.Deterministic(
                    f"sigma_intercept_{cov.name}",
                    pt.as_tensor_variable(
                        adapt["pretrained_model_params"]["posterior_means"][
                            f"sigma_intercept_{cov.name}"
                        ],
                    ),
                    dims=("scalar",),
                )
                # Hierarchical intercepts for each category (using reparameterized form)
                categorical_intercept_offset = pm.Deterministic(
                    f"intercept_offset_{cov.name}",
                    pt.as_tensor_variable(
                        adapt["pretrained_model_params"]["posterior_means"][
                            f"intercept_offset_{cov.name}"
                        ],
                    ),
                    dims=(cov.name,),
                )
                categorical_intercept = pm.Deterministic(
                    f"intercept_{cov.name}",
                    (
                        categorical_intercept_offset
                        * pt.reshape(sigma_intercept_category, (1,))  # pyright: ignore[reportPrivateImportUsage]
                    ),
                    dims=(cov.name,),
                )
            else:
                # Non-hierarchical (linear) categorical effect
                categorical_intercept = pm.Deterministic(
                    f"intercept_{cov.name}",
                    pt.as_tensor_variable(
                        adapt["pretrained_model_params"]["posterior_means"][
                            f"intercept_{cov.name}"
                        ],
                    ),
                    dims=(cov.name,),
                )
        else:
            if cov.hierarchical:
                # Hierarchical categorical effect
                # Hyperpriors for category (Bayesian equivalent of random effects)
                # Hyperpriors are fixed during adaptation
                sigma_intercept_category = pm.Deterministic(
                    f"sigma_intercept_{cov.name}",
                    pt.as_tensor_variable(
                        adapt["pretrained_model_params"]["posterior_means"][
                            f"sigma_intercept_{cov.name}"
                        ],
                    ),
                    dims=("scalar",),
                )
                # Hierarchical intercepts for each category (using reparameterized form)
                # New categories get new parameters, old categories are fixed
                # Freeze old category parameters during adaptation
                fixed_categorical_intercept_offset = pm.Deterministic(
                    f"intercept_offset_{cov.name}_fixed",
                    pt.as_tensor_variable(
                        adapt["pretrained_model_params"]["posterior_means"][
                            f"intercept_offset_{cov.name}"
                        ],
                    ),
                )
                # Create new parameters for new categories
                new_category_count = len(adapt["new_category_names"])
                new_categorical_intercept_offset = pm.Normal(
                    f"intercept_offset_{cov.name}_adapt",
                    mu=0,
                    sigma=hierarchical_sigma_prior,
                    size=new_category_count,
                )
                # Combine fixed and new offsets
                categorical_intercept_offset = pm.Deterministic(
                    f"intercept_offset_{cov.name}",
                    pt.concatenate(
                        [
                            fixed_categorical_intercept_offset,
                            new_categorical_intercept_offset,
                        ],
                    ),
                    dims=(cov.name,),
                )
                categorical_intercept = pm.Deterministic(
                    f"intercept_{cov.name}",
                    (
                        categorical_intercept_offset
                        * pt.reshape(sigma_intercept_category, (1,))  # pyright: ignore[reportPrivateImportUsage]
                    ),
                    dims=(cov.name,),
                )
            else:
                # Non-hierarchical (linear) categorical effect
                # New categories get new parameters, old categories are fixed
                # Freeze old category parameters during adaptation
                fixed_categorical_intercept = pm.Deterministic(
                    f"intercept_{cov.name}_fixed",
                    pt.as_tensor_variable(
                        adapt["pretrained_model_params"]["posterior_means"][
                            f"intercept_{cov.name}"
                        ],
                    ),
                )
                # Create new parameters for new categories
                new_category_count = len(adapt["new_category_names"])
                new_categorical_intercept = pm.Normal(
                    f"intercept_{cov.name}_adapt",
                    mu=0,
                    sigma=sigma_prior,
                    size=new_category_count,
                )
                # Combine fixed and new intercepts
                categorical_intercept = pm.Deterministic(
                    f"intercept_{cov.name}",
                    pt.concatenate(
                        [
                            fixed_categorical_intercept,
                            new_categorical_intercept,
                        ],
                    ),
                    dims=(cov.name,),
                )
            self.model_params["n_params"] += new_category_count
        effects_list.append(
            categorical_intercept[category_indices[cov.name]],
        )

    def _model_all_correlation_effects(
        self,
        train_data: pd.DataFrame,
        spline_bases: dict[str, npt.NDArray[np.floating[Any]]],
        category_indices: dict[str, npt.NDArray[np.integer[Any]]],
        adapt: dict[str, Any] | None = None,
    ) -> list[TensorVariable]:
        """
        Model all covariate correlation effects.
        """
        # Create a list to contain the effects of covariates on
        # the z-transformed correlation
        z_transformed_correlation_effects = []

        # Model the z-transformed correlation between the variables of interest
        # Model the global intercept for z
        if adapt is None:
            global_intercept_z = pm.Normal(
                "global_intercept_z",
                mu=0,
                sigma=5,
                dims=("scalar",),
            )
            # Increment parameter count for global intercept
            self.model_params["n_params"] += 1
        else:
            # Use the pretrained global intercept
            global_intercept_z = pm.Deterministic(
                "global_intercept_z",
                pt.as_tensor_variable(
                    adapt["pretrained_model_params"]["posterior_means"][
                        "global_intercept_z"
                    ],
                ),
                dims=("scalar",),
            )
        z_transformed_correlation_effects.append(global_intercept_z)
        # Model additional covariate effects on the z estimate
        for cov in self.spec.covariates:
            if cov.name in self.spec.influencing_covariance:
                if cov.cov_type == "numerical":
                    if cov.effect == "linear":
                        self._model_linear_correlation_effect(
                            train_data=train_data,
                            cov=cov,
                            effects_list=z_transformed_correlation_effects,
                            adapt=adapt,
                        )
                    elif cov.effect == "spline":
                        self._model_spline_correlation_effect(
                            train_data=train_data,
                            cov=cov,
                            effects_list=z_transformed_correlation_effects,
                            spline_bases=spline_bases,
                            adapt=adapt,
                        )
                elif cov.cov_type == "categorical":
                    self._model_categorical_correlation_effect(
                        train_data=train_data,
                        cov=cov,
                        effects_list=z_transformed_correlation_effects,
                        category_indices=category_indices,
                        adapt=adapt,
                    )

                else:
                    err = f"Invalid covariate type '{cov.cov_type}' for '{cov.name}'."
                    raise ValueError(err)
        return z_transformed_correlation_effects

    def _combine_all_correlation_effects(
        self,
        z_transformed_correlation_effects: list[TensorVariable],
        combination_indices: npt.NDArray[np.integer[Any]],
        combination_weights: npt.NDArray[np.floating[Any]],
        standardized_vois: npt.NDArray[np.floating[Any]],
        standardized_vois_mu_estimate: npt.NDArray[np.floating[Any]],
        standardized_vois_std_estimate: npt.NDArray[np.floating[Any]],
    ) -> None:
        """
        Combine all effects to model the observed data likelihood from the list of
        correlation effects.
        """
        # Combine all covariance effects
        z_transformed_correlation_estimate = sum(z_transformed_correlation_effects)

        # Convert z-transformed score to correlation
        correlation_estimate = pt.tanh(z_transformed_correlation_estimate)

        # Now apply the random combinations to get final distribution estimates
        # Apply combination weights for mu estimate
        combined_mu_estimate = pt.sum(
            pt.mul(
                standardized_vois_mu_estimate[combination_indices, :],
                combination_weights,
            ),
            axis=1,
        )
        # Apply combination weights for sigma estimate
        combined_sigma_estimate = pt.mul(
            standardized_vois_std_estimate[combination_indices, :],
            combination_weights,
        )
        # Apply combination weights for correlation estimate
        combined_correlation_estimate = correlation_estimate[combination_indices]  # pyright: ignore[reportOptionalSubscript]
        # Now build the std estimate
        combined_std_estimate = pt.sqrt(
            combined_sigma_estimate[:, 0] ** 2  # pyright: ignore[reportOptionalSubscript]
            + combined_sigma_estimate[:, 1] ** 2  # pyright: ignore[reportOptionalSubscript]
            + (
                2
                * combined_sigma_estimate[:, 0]  # pyright: ignore[reportOptionalSubscript]
                * combined_sigma_estimate[:, 1]  # pyright: ignore[reportOptionalSubscript]
                * combined_correlation_estimate
            ),
        )

        # Apply combination to the variables of interest
        combined_variable_of_interest = pt.sum(
            pt.mul(
                standardized_vois[combination_indices, :],
                combination_weights,
            ),
            axis=1,
        )

        effective_sample_size = self.model_params["sample_size"]

        # Model likelihood estimation for covariance model
        _likelihood = pm.Normal(
            f"likelihood_cov_{self.spec.variable_of_interest_1}_{self.spec.variable_of_interest_2}",
            mu=combined_mu_estimate,
            sigma=combined_std_estimate,
            observed=combined_variable_of_interest,
            total_size=(effective_sample_size),
        )

    def _fit_model_with_advi(self, *, progress_bar: bool = True) -> None:
        """
        Fit the model using Automatic Differentiation Variational Inference (ADVI).
        """
        base_lr = self.defaults["adam_learning_rate"]
        decay = self.defaults["adam_learning_rate_decay"]
        lr = shared(base_lr)
        optimizer = pm.adam(learning_rate=cast("float", lr))

        # Adaptive learning rate schedule callback
        def update_learning_rate(_approx: Any, _loss: Any, iteration: int) -> None:
            lr.set_value(base_lr * (decay**iteration))

        # Run automatic differential variational inference to fit the model
        self._trace = pm.fit(
            method="advi",
            n=self.defaults["advi_iterations"],
            random_seed=self.defaults["random_seed"],  # For reproducibility
            obj_optimizer=optimizer,
            callbacks=[
                update_learning_rate,
                pm.callbacks.CheckParametersConvergence(
                    tolerance=self.defaults["advi_convergence_tolerance"],
                    diff="relative",
                ),
            ],
            progressbar=progress_bar,
        )

        # Sample from the posterior distribution and store the results
        self.model_inference_data = self._trace.sample(
            2000,
            random_seed=self.defaults["random_seed"],
        )

        # Compute posterior means and standard deviations
        posterior_means = self.model_inference_data.posterior.mean(
            dim=("chain", "draw"),
        )
        posterior_stds = self.model_inference_data.posterior.std(
            dim=("chain", "draw"),
        )

        # Store posterior means and stds as a dictionary in model parameters
        self.model_params["posterior_means"] = {
            x: posterior_means.data_vars[x].to_numpy()
            for x in posterior_means.data_vars
        }
        self.model_params["posterior_stds"] = {
            x: posterior_stds.data_vars[x].to_numpy() for x in posterior_stds.data_vars
        }

    def fit(
        self,
        train_data: pd.DataFrame,
        *,
        save_directory: Path | None = None,
        progress_bar: bool = True,
        adapt: dict[str, Any] | None = None,
    ) -> None:
        """
        Fit the normative model to the training data.

        This method implements the fitting logic for the normative model
        based on the provided training data and model specification.

        Args:
            train_data: pd.DataFrame
                DataFrame containing the training data. It must include the variable
                of interest, their predicted moments, and all specified covariates.
            save_directory: Path | None
                A path to a directory to save the model. If provided, the fitted model
                will be saved to this path.
            progress_bar: bool
                If True, display a progress bar during fitting. Defaults to True.
            adapt: dict[str, Any] | None
                If provided, adapt a pre-trained model to a new covariate.
                Note: We recommended using the `adapt_fit` method, and not directly
                changing this argument, unless you know what you are doing.
        """
        # Validation checks
        self._validate_model()
        self._validate_dataframe_for_fitting(train_data)

        # Extract the variables of interest
        variables_of_interest = train_data[
            [self.spec.variable_of_interest_1, self.spec.variable_of_interest_2]
        ].to_numpy()

        # A dictionary to hold the model parameters after fitting
        if adapt is None:
            self.model_params = {}
            self.model_params["mean_vois"] = variables_of_interest.mean(axis=0)
            self.model_params["std_vois"] = variables_of_interest.std(axis=0)
            self.model_params["sample_size"] = variables_of_interest.shape[0]
            # Initialize parameter count
            self.model_params["n_params"] = 0
        else:
            # Update the pretrained model parameters
            if not hasattr(self, "model_params") or self.model_params is None:
                self.model_params = copy.deepcopy(adapt["pretrained_model_params"])
            self.model_params["sample_size"] += variables_of_interest.shape[0]

        # Data preparation
        # Combination weights
        combination_weights = np.ones(shape=(train_data.shape[0], 2))

        # Data coordinates
        combination_indices = np.arange(train_data.shape[0])
        model_coords = self._build_model_coordinates(
            observations=combination_indices,
        )

        # Fitting logic
        with pm.Model(coords=model_coords) as self._model:
            # Standardize the variable of interest, and store mean and std
            # This is done to ensure that the model is not sensitive to
            # the scale of the variable
            standardized_vois = (
                variables_of_interest - self.model_params["mean_vois"]
            ) / self.model_params["std_vois"]
            variables_of_interest_mu_estimate = train_data[
                [
                    f"{self.spec.variable_of_interest_1}_mu_estimate",
                    f"{self.spec.variable_of_interest_2}_mu_estimate",
                ]
            ].to_numpy()
            standardized_vois_mu_estimate = (
                variables_of_interest_mu_estimate - self.model_params["mean_vois"]
            ) / self.model_params["std_vois"]
            variables_of_interest_std_estimate = train_data[
                [
                    f"{self.spec.variable_of_interest_1}_std_estimate",
                    f"{self.spec.variable_of_interest_2}_std_estimate",
                ]
            ].to_numpy()
            standardized_vois_std_estimate = (
                variables_of_interest_std_estimate / self.model_params["std_vois"]
            )

            # A dictionary for precomputed bspline basis functions
            spline_bases: dict[str, npt.NDArray[np.floating[Any]]] = {}

            # A dictionary for factorized categories
            category_indices: dict[str, npt.NDArray[np.integer[Any]]] = {}

            # Model the covariance between the variables of interest
            z_transformed_correlation_effects = self._model_all_correlation_effects(
                train_data,
                spline_bases,
                category_indices,
                adapt=adapt,
            )

            # Combine all covariance effects
            self._combine_all_correlation_effects(
                z_transformed_correlation_effects=z_transformed_correlation_effects,
                combination_indices=combination_indices,
                combination_weights=combination_weights,
                standardized_vois=standardized_vois,
                standardized_vois_mu_estimate=standardized_vois_mu_estimate,
                standardized_vois_std_estimate=standardized_vois_std_estimate,
            )

            # Fit the model using ADVI
            self._fit_model_with_advi(progress_bar=progress_bar)

        # Save the model if a save path is provided
        if save_directory is not None:
            self.save_model(Path(save_directory))

    def adapt_fit(
        self,
        covariate_to_adapt: str,
        new_category_names: npt.NDArray[np.str_],
        train_data: pd.DataFrame,
        *,
        pretrained_model_params: dict[str, Any] | None = None,
        save_directory: Path | None = None,
        progress_bar: bool = True,
    ) -> None:
        """
        Using a previously fitted model, adapt the model to a new batch.
        This method enables adaptation of the model to data from a new
        batch/site by freezing all fitted parameters, and only estimating
        new parameters for the new batch/site category.

        Args:
            covariate_to_adapt: str
                Name of the categorical covariate representing the batch/site
                to which the model should be adapted.
                Note: This covariate must have been specified in the original
                model.
            new_category_names: list[str]
                Names of the new categories in the covariate_to_adapt representing
                the new batch/site labels (e.g. names of the new site).
                Note: These names must not have been present in the original
                fitted model.
            train_data: pd.DataFrame
                DataFrame containing the training data for adaptation.
                It must include the variable of interest and all specified covariates.
                Note: The covariate_to_adapt column must only contain the
                new_category_names (no new data from previously trained batches).
            pretrained_model_params: dict[str, Any] | None
                The model parameters from a previously fitted model to adapt.
                If None, the model parameters from the current instance will be used
                (assuming fitting was done).
            save_directory: Path | None
                A path to a directory to save the adapted model. If provided,
                the fitted model will be saved to this path.
            progress_bar: bool
                If True, display a progress bar during fitting. Defaults to True.
        """
        # Validation checks
        self._validate_model()
        self._validate_dataframe_for_fitting(train_data)

        # Locate the covariate to adapt
        cov_to_adapt_index = [cov.name for cov in self.spec.covariates].index(
            covariate_to_adapt,
        )

        # Extend the covariate categories to include the new categories
        self.spec.covariates[cov_to_adapt_index].extend_categories(new_category_names)

        # Extract the pre-trained model parameters
        if pretrained_model_params is None:
            if not hasattr(self, "model_params") or self.model_params is None:
                err = (
                    "No pretrained model parameters found. "
                    "Please provide pretrained_model_params or fit the model first."
                )
                raise ValueError(err)
            pretrained_model_params = copy.deepcopy(self.model_params)

        # Fit the adapted model
        self.fit(
            train_data,
            save_directory=save_directory,
            progress_bar=progress_bar,
            adapt={
                "covariate_to_adapt": covariate_to_adapt,
                "new_category_names": new_category_names,
                "pretrained_model_params": pretrained_model_params,
            },
        )

    def predict(
        self,
        test_covariates: pd.DataFrame,
        model_params: dict[str, Any] | None = None,
        predict_without: list[str] | None = None,
    ) -> NormativePredictions:
        """
        Predict correlation for new data (from covariates) using the fitted model.

        Args:
            test_covariates: pd.DataFrame
                DataFrame containing the new covariate data to predict.
                This must include all specified covariates.
                Note: covariates listed in predict_without will be ignored and are
                hence not required.
            model_params: dict | None
                Optional dictionary of model parameters to use. If not provided,
                the stored parameters from model.fit() will be used.
            predict_without: list[str] | None
                Optional list of covariate names to ignore during prediction.
                This can be used to check the effect of removing certain covariates
                from the model.

        Returns:
            NormativePredictions: Object containing the predicted pairwise correlations
                for the variables of interest.
        """
        # Validate the new data
        validation_columns = [
            cov.name
            for cov in self.spec.covariates
            if cov.name not in (predict_without or [])
        ]
        utils.general.validate_dataframe(test_covariates, validation_columns)

        # Parameters
        model_params = model_params or self.model_params
        if model_params is None:
            err = "No model parameters found. Please provide model_params."
            raise ValueError(err)

        # Posterior means
        posterior_means = model_params["posterior_means"]

        # Calculate mean and variance effects
        z_transformed_correlation_estimate = np.zeros(test_covariates.shape[0]) + float(
            posterior_means["global_intercept_z"],
        )

        for cov in self.spec.covariates:
            if (cov.name in self.spec.influencing_covariance) and (
                cov.name not in (predict_without or [])
            ):
                if cov.cov_type == "numerical":
                    if cov.effect == "linear":
                        if cov.moments is None:
                            err = (
                                f"Covariate '{cov.name}' is missing moments for"
                                " standardization."
                            )
                            raise ValueError(err)
                        z_transformed_correlation_estimate += (
                            (
                                cast(
                                    "npt.NDArray[Any]",
                                    test_covariates[cov.name].to_numpy(),
                                )
                                - cov.moments[0]
                            )
                            / cov.moments[1]
                        ) * posterior_means[f"linear_beta_{cov.name}"]
                    elif cov.effect == "spline":
                        spline_bases = cov.make_spline_bases(
                            cast(
                                "npt.NDArray[Any]",
                                test_covariates[cov.name].to_numpy(),
                            ),
                        )
                        spline_betas = posterior_means[f"spline_betas_{cov.name}"]
                        z_transformed_correlation_estimate += (
                            spline_bases @ spline_betas
                        )
                elif cov.cov_type == "categorical":
                    category_indices = cov.factorize_categories(
                        cast("npt.NDArray[Any]", test_covariates[cov.name].to_numpy()),
                    )
                    categorical_intercept = None
                    if cov.hierarchical:
                        categorical_intercept = (
                            posterior_means[f"intercept_offset_{cov.name}"]
                            * posterior_means[f"sigma_intercept_{cov.name}"]
                        )
                    else:
                        categorical_intercept = posterior_means[f"intercept_{cov.name}"]
                    z_transformed_correlation_estimate += categorical_intercept[
                        category_indices
                    ]

        # Convert z-transformed score to correlation
        correlation_estimate = np.tanh(z_transformed_correlation_estimate)

        # Create a the predictions object and return
        return NormativePredictions({"correlation_estimate": correlation_estimate})

adapt_fit(covariate_to_adapt: str, new_category_names: npt.NDArray[np.str_], train_data: pd.DataFrame, *, pretrained_model_params: dict[str, Any] | None = None, save_directory: Path | None = None, progress_bar: bool = True) -> None

Using a previously fitted model, adapt the model to a new batch. This method enables adaptation of the model to data from a new batch/site by freezing all fitted parameters, and only estimating new parameters for the new batch/site category.

Parameters:

Name Type Description Default
covariate_to_adapt str

str Name of the categorical covariate representing the batch/site to which the model should be adapted. Note: This covariate must have been specified in the original model.

required
new_category_names NDArray[str_]

list[str] Names of the new categories in the covariate_to_adapt representing the new batch/site labels (e.g. names of the new site). Note: These names must not have been present in the original fitted model.

required
train_data DataFrame

pd.DataFrame DataFrame containing the training data for adaptation. It must include the variable of interest and all specified covariates. Note: The covariate_to_adapt column must only contain the new_category_names (no new data from previously trained batches).

required
pretrained_model_params dict[str, Any] | None

dict[str, Any] | None The model parameters from a previously fitted model to adapt. If None, the model parameters from the current instance will be used (assuming fitting was done).

None
save_directory Path | None

Path | None A path to a directory to save the adapted model. If provided, the fitted model will be saved to this path.

None
progress_bar bool

bool If True, display a progress bar during fitting. Defaults to True.

True
Source code in src/spectranorm/snm.py
def adapt_fit(
    self,
    covariate_to_adapt: str,
    new_category_names: npt.NDArray[np.str_],
    train_data: pd.DataFrame,
    *,
    pretrained_model_params: dict[str, Any] | None = None,
    save_directory: Path | None = None,
    progress_bar: bool = True,
) -> None:
    """
    Using a previously fitted model, adapt the model to a new batch.
    This method enables adaptation of the model to data from a new
    batch/site by freezing all fitted parameters, and only estimating
    new parameters for the new batch/site category.

    Args:
        covariate_to_adapt: str
            Name of the categorical covariate representing the batch/site
            to which the model should be adapted.
            Note: This covariate must have been specified in the original
            model.
        new_category_names: list[str]
            Names of the new categories in the covariate_to_adapt representing
            the new batch/site labels (e.g. names of the new site).
            Note: These names must not have been present in the original
            fitted model.
        train_data: pd.DataFrame
            DataFrame containing the training data for adaptation.
            It must include the variable of interest and all specified covariates.
            Note: The covariate_to_adapt column must only contain the
            new_category_names (no new data from previously trained batches).
        pretrained_model_params: dict[str, Any] | None
            The model parameters from a previously fitted model to adapt.
            If None, the model parameters from the current instance will be used
            (assuming fitting was done).
        save_directory: Path | None
            A path to a directory to save the adapted model. If provided,
            the fitted model will be saved to this path.
        progress_bar: bool
            If True, display a progress bar during fitting. Defaults to True.
    """
    # Validation checks
    self._validate_model()
    self._validate_dataframe_for_fitting(train_data)

    # Locate the covariate to adapt
    cov_to_adapt_index = [cov.name for cov in self.spec.covariates].index(
        covariate_to_adapt,
    )

    # Extend the covariate categories to include the new categories
    self.spec.covariates[cov_to_adapt_index].extend_categories(new_category_names)

    # Extract the pre-trained model parameters
    if pretrained_model_params is None:
        if not hasattr(self, "model_params") or self.model_params is None:
            err = (
                "No pretrained model parameters found. "
                "Please provide pretrained_model_params or fit the model first."
            )
            raise ValueError(err)
        pretrained_model_params = copy.deepcopy(self.model_params)

    # Fit the adapted model
    self.fit(
        train_data,
        save_directory=save_directory,
        progress_bar=progress_bar,
        adapt={
            "covariate_to_adapt": covariate_to_adapt,
            "new_category_names": new_category_names,
            "pretrained_model_params": pretrained_model_params,
        },
    )

fit(train_data: pd.DataFrame, *, save_directory: Path | None = None, progress_bar: bool = True, adapt: dict[str, Any] | None = None) -> None

Fit the normative model to the training data.

This method implements the fitting logic for the normative model based on the provided training data and model specification.

Parameters:

Name Type Description Default
train_data DataFrame

pd.DataFrame DataFrame containing the training data. It must include the variable of interest, their predicted moments, and all specified covariates.

required
save_directory Path | None

Path | None A path to a directory to save the model. If provided, the fitted model will be saved to this path.

None
progress_bar bool

bool If True, display a progress bar during fitting. Defaults to True.

True
adapt dict[str, Any] | None

dict[str, Any] | None If provided, adapt a pre-trained model to a new covariate. Note: We recommended using the adapt_fit method, and not directly changing this argument, unless you know what you are doing.

None
Source code in src/spectranorm/snm.py
def fit(
    self,
    train_data: pd.DataFrame,
    *,
    save_directory: Path | None = None,
    progress_bar: bool = True,
    adapt: dict[str, Any] | None = None,
) -> None:
    """
    Fit the normative model to the training data.

    This method implements the fitting logic for the normative model
    based on the provided training data and model specification.

    Args:
        train_data: pd.DataFrame
            DataFrame containing the training data. It must include the variable
            of interest, their predicted moments, and all specified covariates.
        save_directory: Path | None
            A path to a directory to save the model. If provided, the fitted model
            will be saved to this path.
        progress_bar: bool
            If True, display a progress bar during fitting. Defaults to True.
        adapt: dict[str, Any] | None
            If provided, adapt a pre-trained model to a new covariate.
            Note: We recommended using the `adapt_fit` method, and not directly
            changing this argument, unless you know what you are doing.
    """
    # Validation checks
    self._validate_model()
    self._validate_dataframe_for_fitting(train_data)

    # Extract the variables of interest
    variables_of_interest = train_data[
        [self.spec.variable_of_interest_1, self.spec.variable_of_interest_2]
    ].to_numpy()

    # A dictionary to hold the model parameters after fitting
    if adapt is None:
        self.model_params = {}
        self.model_params["mean_vois"] = variables_of_interest.mean(axis=0)
        self.model_params["std_vois"] = variables_of_interest.std(axis=0)
        self.model_params["sample_size"] = variables_of_interest.shape[0]
        # Initialize parameter count
        self.model_params["n_params"] = 0
    else:
        # Update the pretrained model parameters
        if not hasattr(self, "model_params") or self.model_params is None:
            self.model_params = copy.deepcopy(adapt["pretrained_model_params"])
        self.model_params["sample_size"] += variables_of_interest.shape[0]

    # Data preparation
    # Combination weights
    combination_weights = np.ones(shape=(train_data.shape[0], 2))

    # Data coordinates
    combination_indices = np.arange(train_data.shape[0])
    model_coords = self._build_model_coordinates(
        observations=combination_indices,
    )

    # Fitting logic
    with pm.Model(coords=model_coords) as self._model:
        # Standardize the variable of interest, and store mean and std
        # This is done to ensure that the model is not sensitive to
        # the scale of the variable
        standardized_vois = (
            variables_of_interest - self.model_params["mean_vois"]
        ) / self.model_params["std_vois"]
        variables_of_interest_mu_estimate = train_data[
            [
                f"{self.spec.variable_of_interest_1}_mu_estimate",
                f"{self.spec.variable_of_interest_2}_mu_estimate",
            ]
        ].to_numpy()
        standardized_vois_mu_estimate = (
            variables_of_interest_mu_estimate - self.model_params["mean_vois"]
        ) / self.model_params["std_vois"]
        variables_of_interest_std_estimate = train_data[
            [
                f"{self.spec.variable_of_interest_1}_std_estimate",
                f"{self.spec.variable_of_interest_2}_std_estimate",
            ]
        ].to_numpy()
        standardized_vois_std_estimate = (
            variables_of_interest_std_estimate / self.model_params["std_vois"]
        )

        # A dictionary for precomputed bspline basis functions
        spline_bases: dict[str, npt.NDArray[np.floating[Any]]] = {}

        # A dictionary for factorized categories
        category_indices: dict[str, npt.NDArray[np.integer[Any]]] = {}

        # Model the covariance between the variables of interest
        z_transformed_correlation_effects = self._model_all_correlation_effects(
            train_data,
            spline_bases,
            category_indices,
            adapt=adapt,
        )

        # Combine all covariance effects
        self._combine_all_correlation_effects(
            z_transformed_correlation_effects=z_transformed_correlation_effects,
            combination_indices=combination_indices,
            combination_weights=combination_weights,
            standardized_vois=standardized_vois,
            standardized_vois_mu_estimate=standardized_vois_mu_estimate,
            standardized_vois_std_estimate=standardized_vois_std_estimate,
        )

        # Fit the model using ADVI
        self._fit_model_with_advi(progress_bar=progress_bar)

    # Save the model if a save path is provided
    if save_directory is not None:
        self.save_model(Path(save_directory))

from_direct_model(direct_model: DirectNormativeModel, variable_of_interest_1: str, variable_of_interest_2: str, influencing_covariance: list[str] | None = None, defaults_overwrite: dict[str, Any] | None = None) -> CovarianceNormativeModel classmethod

Initialize the model from a direct model instance, and two variable names.

Parameters:

Name Type Description Default
direct_model DirectNormativeModel

DirectNormativeModel This model will be used to instantiate a similar covariance model.

required
variable_of_interest_1 str

str Name of the first target variable to model.

required
variable_of_interest_2 str

str Name of the second target variable to model.

required
influencing_covariance list[str] | None

list[str] | None List of covariates that influence the covariance structure. If not provided, this will be copied from the direct model's influencing_variance.

None

Returns:

Type Description
CovarianceNormativeModel

CovarianceNormativeModel An instance of CovarianceNormativeModel initialized with the provided data.

Source code in src/spectranorm/snm.py
@classmethod
def from_direct_model(
    cls,
    direct_model: DirectNormativeModel,
    variable_of_interest_1: str,
    variable_of_interest_2: str,
    influencing_covariance: list[str] | None = None,
    defaults_overwrite: dict[str, Any] | None = None,
) -> CovarianceNormativeModel:
    """
    Initialize the model from a direct model instance, and two variable names.

    Args:
        direct_model: DirectNormativeModel
            This model will be used to instantiate a similar covariance model.
        variable_of_interest_1: str
            Name of the first target variable to model.
        variable_of_interest_2: str
            Name of the second target variable to model.
        influencing_covariance: list[str] | None
            List of covariates that influence the covariance structure. If not
            provided, this will be copied from the direct model's
            `influencing_variance`.

    Returns:
        CovarianceNormativeModel
            An instance of CovarianceNormativeModel initialized with the provided
            data.
    """
    # Validity checks for input parameters
    if not isinstance(direct_model, DirectNormativeModel):
        err = "direct_model must be an instance of DirectNormativeModel."
        raise TypeError(err)
    if not (
        isinstance(variable_of_interest_1, str)
        and isinstance(variable_of_interest_2, str)
    ):
        err = "Variables of interest must be strings."
        raise TypeError(err)

    # Substitute influencing_covariance if not provided
    if influencing_covariance is None:
        influencing_covariance = direct_model.spec.influencing_variance

    # Use the same setup as the direct model
    model = cls(
        spec=CovarianceModelSpec(
            variable_of_interest_1=variable_of_interest_1,
            variable_of_interest_2=variable_of_interest_2,
            covariates=direct_model.spec.covariates,
            influencing_covariance=influencing_covariance,
        ),
    )

    # update defaults
    model.defaults.update(direct_model.defaults)
    model.defaults.update(defaults_overwrite or {})

    return model

load_model(directory: Path, *, load_posterior: bool = False) -> CovarianceNormativeModel classmethod

Load the model and its posterior from a directory. The model will be loaded from a subdirectory named 'saved_model'.

Parameters:

Name Type Description Default
directory Path

Path Path to the directory containing the model.

required
load_posterior bool

bool (default=False) If True, load the model's posterior trace from the saved inference data.

False
Source code in src/spectranorm/snm.py
@classmethod
def load_model(
    cls,
    directory: Path,
    *,
    load_posterior: bool = False,
) -> CovarianceNormativeModel:
    """
    Load the model and its posterior from a directory.
    The model will be loaded from a subdirectory named 'saved_model'.

    Args:
        directory: Path
            Path to the directory containing the model.
        load_posterior: bool (default=False)
            If True, load the model's posterior trace from the saved inference data.
    """
    # Validate the load directory
    directory = Path(directory)
    saved_model_dir = utils.general.validate_load_directory(
        directory,
        "saved_model",
    )

    # Load the saved model dict
    model_dict = joblib.load(saved_model_dir / "model_dict.joblib")

    # Create an instance of the class
    instance = cls(
        spec=model_dict["spec"],
    )

    # Set the attributes from the loaded model dictionary
    instance.defaults.update(model_dict["defaults"])
    if "model_params" in model_dict:
        instance.model_params = model_dict["model_params"]
        if load_posterior:
            instance.model_inference_data = az.from_netcdf(  # type: ignore[no-untyped-call]
                saved_model_dir / "model_inference_data.nc",
            )

    return instance

predict(test_covariates: pd.DataFrame, model_params: dict[str, Any] | None = None, predict_without: list[str] | None = None) -> NormativePredictions

Predict correlation for new data (from covariates) using the fitted model.

Parameters:

Name Type Description Default
test_covariates DataFrame

pd.DataFrame DataFrame containing the new covariate data to predict. This must include all specified covariates. Note: covariates listed in predict_without will be ignored and are hence not required.

required
model_params dict[str, Any] | None

dict | None Optional dictionary of model parameters to use. If not provided, the stored parameters from model.fit() will be used.

None
predict_without list[str] | None

list[str] | None Optional list of covariate names to ignore during prediction. This can be used to check the effect of removing certain covariates from the model.

None

Returns:

Name Type Description
NormativePredictions NormativePredictions

Object containing the predicted pairwise correlations for the variables of interest.

Source code in src/spectranorm/snm.py
def predict(
    self,
    test_covariates: pd.DataFrame,
    model_params: dict[str, Any] | None = None,
    predict_without: list[str] | None = None,
) -> NormativePredictions:
    """
    Predict correlation for new data (from covariates) using the fitted model.

    Args:
        test_covariates: pd.DataFrame
            DataFrame containing the new covariate data to predict.
            This must include all specified covariates.
            Note: covariates listed in predict_without will be ignored and are
            hence not required.
        model_params: dict | None
            Optional dictionary of model parameters to use. If not provided,
            the stored parameters from model.fit() will be used.
        predict_without: list[str] | None
            Optional list of covariate names to ignore during prediction.
            This can be used to check the effect of removing certain covariates
            from the model.

    Returns:
        NormativePredictions: Object containing the predicted pairwise correlations
            for the variables of interest.
    """
    # Validate the new data
    validation_columns = [
        cov.name
        for cov in self.spec.covariates
        if cov.name not in (predict_without or [])
    ]
    utils.general.validate_dataframe(test_covariates, validation_columns)

    # Parameters
    model_params = model_params or self.model_params
    if model_params is None:
        err = "No model parameters found. Please provide model_params."
        raise ValueError(err)

    # Posterior means
    posterior_means = model_params["posterior_means"]

    # Calculate mean and variance effects
    z_transformed_correlation_estimate = np.zeros(test_covariates.shape[0]) + float(
        posterior_means["global_intercept_z"],
    )

    for cov in self.spec.covariates:
        if (cov.name in self.spec.influencing_covariance) and (
            cov.name not in (predict_without or [])
        ):
            if cov.cov_type == "numerical":
                if cov.effect == "linear":
                    if cov.moments is None:
                        err = (
                            f"Covariate '{cov.name}' is missing moments for"
                            " standardization."
                        )
                        raise ValueError(err)
                    z_transformed_correlation_estimate += (
                        (
                            cast(
                                "npt.NDArray[Any]",
                                test_covariates[cov.name].to_numpy(),
                            )
                            - cov.moments[0]
                        )
                        / cov.moments[1]
                    ) * posterior_means[f"linear_beta_{cov.name}"]
                elif cov.effect == "spline":
                    spline_bases = cov.make_spline_bases(
                        cast(
                            "npt.NDArray[Any]",
                            test_covariates[cov.name].to_numpy(),
                        ),
                    )
                    spline_betas = posterior_means[f"spline_betas_{cov.name}"]
                    z_transformed_correlation_estimate += (
                        spline_bases @ spline_betas
                    )
            elif cov.cov_type == "categorical":
                category_indices = cov.factorize_categories(
                    cast("npt.NDArray[Any]", test_covariates[cov.name].to_numpy()),
                )
                categorical_intercept = None
                if cov.hierarchical:
                    categorical_intercept = (
                        posterior_means[f"intercept_offset_{cov.name}"]
                        * posterior_means[f"sigma_intercept_{cov.name}"]
                    )
                else:
                    categorical_intercept = posterior_means[f"intercept_{cov.name}"]
                z_transformed_correlation_estimate += categorical_intercept[
                    category_indices
                ]

    # Convert z-transformed score to correlation
    correlation_estimate = np.tanh(z_transformed_correlation_estimate)

    # Create a the predictions object and return
    return NormativePredictions({"correlation_estimate": correlation_estimate})

save_model(directory: Path, *, save_posterior: bool = False) -> None

Save the fitted model and it's posterior to a directory. The model will be saved in a subdirectory named 'saved_model'. If this directory is not empty, an error is raised.

Parameters:

Name Type Description Default
directory Path

Path Path to a directory to save the model.

required
save_posterior bool

bool (default=False) If True, save the model's posterior trace inference data.

False
Source code in src/spectranorm/snm.py
def save_model(self, directory: Path, *, save_posterior: bool = False) -> None:
    """
    Save the fitted model and it's posterior to a directory.
    The model will be saved in a subdirectory named 'saved_model'.
    If this directory is not empty, an error is raised.

    Args:
        directory: Path
            Path to a directory to save the model.
        save_posterior: bool (default=False)
            If True, save the model's posterior trace inference data.
    """
    # Prepare the save directory
    directory = Path(directory)
    saved_model_dir = utils.general.prepare_save_directory(directory, "saved_model")

    model_dict = {
        "spec": self.spec,
        "defaults": self.defaults,
    }
    if hasattr(self, "model_params"):
        model_dict["model_params"] = self.model_params
        if hasattr(self, "model_inference_data") and save_posterior:
            self.model_inference_data.to_netcdf(
                saved_model_dir / "model_inference_data.nc",
            )
    joblib.dump(model_dict, saved_model_dir / "model_dict.joblib")

CovariateSpec dataclass

Specification of a single covariate and how it should be modeled.

Attributes:

Name Type Description
name str

str Name of the covariate (e.g., 'age', 'site').

cov_type CovariateType

str Type of the covariate ('numerical' or 'categorical').

effect NumericalEffect | None

str For numerical covariates, how the effect is modeled ('linear' or 'spline').

categories NDArray[str_] | None

np.ndarray | None For categorical covariates, the category labels stored as a NumPy array.

hierarchical bool | None

bool For categorical covariates, whether to model with a hierarchical structure.

spline_spec SplineSpec | None

SplineSpec | None Optional SplineSpec instance for spline modeling; required if effect is 'spline'.

Validation
  • Numerical covariates must specify 'effect'.
  • If 'effect' is 'spline', 'spline_spec' must be provided.
  • Categorical covariates must specify 'hierarchical'.
  • Categorical covariates cannot have 'effect' or 'spline_spec'.
  • Categorical covariates must have categories listed.
Source code in src/spectranorm/snm.py
@dataclass
class CovariateSpec:
    """
    Specification of a single covariate and how it should be modeled.

    Attributes:
        name: str
            Name of the covariate (e.g., 'age', 'site').
        cov_type: str
            Type of the covariate ('numerical' or 'categorical').
        effect: str
            For numerical covariates, how the effect is modeled ('linear'
            or 'spline').
        categories: np.ndarray | None
            For categorical covariates, the category labels stored as a NumPy array.
        hierarchical: bool
            For categorical covariates, whether to model with a
            hierarchical structure.
        spline_spec: SplineSpec | None
            Optional SplineSpec instance for spline modeling;
            required if effect is 'spline'.

    Validation:
        - Numerical covariates must specify 'effect'.
        - If 'effect' is 'spline', 'spline_spec' must be provided.
        - Categorical covariates must specify 'hierarchical'.
        - Categorical covariates cannot have 'effect' or 'spline_spec'.
        - Categorical covariates must have categories listed.
    """

    name: str
    cov_type: CovariateType  # "categorical" or "numerical"
    effect: NumericalEffect | None = None  # Only if numerical
    categories: npt.NDArray[np.str_] | None = None  # Only if categorical
    hierarchical: bool | None = None  # Only if categorical
    spline_spec: SplineSpec | None = None  # Only for spline modeling
    moments: tuple[float, float] | None = None  # Only for linear effects

    def __repr__(self) -> str:
        """
        String representation of the CovariateSpec instance.
        """
        representation = f"CovariateSpec(name={self.name}, cov_type={self.cov_type}"
        if self.cov_type == "numerical":
            representation += f", effect={self.effect}"
        elif self.cov_type == "categorical":
            representation += f", hierarchical={self.hierarchical}"
            if self.categories is not None:
                representation += f", n_categories={len(self.categories.tolist())}"
        representation += ")"
        return representation

    # Validation checks for the covariate specification.
    def validate_numerical(self) -> None:
        if self.effect not in {"linear", "spline"}:
            err = (
                f"Numerical covariate '{self.name}' must specify effect as "
                "'linear' or 'spline'."
            )
            raise ValueError(err)
        if self.hierarchical is not None:
            err = (
                f"Numerical covariate '{self.name}' should not specify 'hierarchical'."
            )
            raise ValueError(err)
        if self.categories is not None:
            err = f"Numerical covariate '{self.name}' should not specify 'categories'."
            raise ValueError(err)
        if self.effect == "spline":
            if self.spline_spec is None:
                err = (
                    f"Numerical covariate '{self.name}' must have spline "
                    "specification if effect is 'spline'."
                )
                raise ValueError(err)
            if self.moments is not None:
                err = (
                    f"Numerical covariate '{self.name}' should not specify "
                    "moments if effect is 'spline'."
                )
                raise ValueError(err)
        if self.effect == "linear":
            if self.spline_spec is not None:
                err = (
                    f"Numerical covariate '{self.name}' should not have spline "
                    "specification unless effect is 'spline'."
                )
                raise ValueError(err)
            if self.moments is None:
                err = (
                    f"Numerical covariate '{self.name}' must specify moments "
                    "(mean and standard deviation) for linear effects."
                )
                raise ValueError(err)

    def validate_categorical(self) -> None:
        if self.effect is not None:
            err = (
                f"Categorical covariate '{self.name}' should not have a "
                "numerical effect type."
            )
            raise ValueError(err)
        if self.spline_spec is not None:
            err = (
                f"Categorical covariate '{self.name}' should not have spline "
                "specification."
            )
            raise ValueError(err)
        if self.hierarchical is None:
            err = (
                f"Categorical covariate '{self.name}' must specify whether "
                "it is hierarchical."
            )
            raise ValueError(err)
        if self.categories is None:
            err = f"Categorical covariate '{self.name}' must specify categories."
            raise ValueError(err)
        if not isinstance(self.categories, np.ndarray):
            err = (
                f"Categorical covariate '{self.name}' must specify categories "
                "as a NumPy array."
            )
            raise TypeError(err)

    def __post_init__(self) -> None:
        if self.cov_type == "numerical":
            self.validate_numerical()
        elif self.cov_type == "categorical":
            self.validate_categorical()
        else:
            err = f"Invalid covariate type '{self.cov_type}' for '{self.name}'."
            raise ValueError(err)

    def make_spline_bases(
        self,
        values: npt.NDArray[np.floating[Any]],
        *,
        include_intercept: bool = True,
    ) -> npt.NDArray[np.floating[Any]]:
        """
        Create B-spline basis expansion functions for a given covariate.

        Args:
            values (np.ndarray): The values to create the spline basis functions for.

        Returns:
            np.ndarray: The B-spline basis function expansion.
        """
        if self.effect != "spline" or self.spline_spec is None:
            err = f"Covariate '{self.name}' is not a spline covariate."
            raise ValueError(err)

        # Create fixed set of knots if not already defined
        if self.spline_spec.knots is None:
            knots = np.linspace(
                self.spline_spec.lower_bound,
                self.spline_spec.upper_bound,
                self.spline_spec.df - self.spline_spec.degree + 1,
            )[1:-1].tolist()
        else:
            knots = self.spline_spec.knots

        # Create B-spline basis functions
        return np.array(
            patsy.bs(  # pyright: ignore[reportAttributeAccessIssue]
                values,
                knots=knots,
                df=self.spline_spec.df,
                degree=self.spline_spec.degree,
                lower_bound=self.spline_spec.lower_bound,
                upper_bound=self.spline_spec.upper_bound,
                include_intercept=include_intercept,
            ),
        )

    def factorize_categories(
        self,
        values: npt.NDArray[np.str_],
    ) -> npt.NDArray[np.int_]:
        """
        Factorize categorical covariate values into numerical indices.

        Args:
            values (np.ndarray): The values to factorize.

        Returns:
            np.ndarray: The factorized numerical indices for the categories.
        """
        if self.cov_type != "categorical":
            err = (
                f"Covariate '{self.name}' is not a categorical "
                "covariate to be factorized."
            )
            raise ValueError(err)

        # Create a mapping from category values to indices
        if self.categories is None:  # to satisfy type checker
            err = f"Covariate '{self.name}' does not have categories defined."
            raise ValueError(err)
        category_mapping: dict[str, int] = {
            category: idx for idx, category in enumerate(self.categories)
        }
        # Factorize the values using the mapping
        return np.array([category_mapping[val] for val in values], dtype=int)

    def extend_categories(
        self,
        new_categories: npt.NDArray[np.str_],
    ) -> None:
        """
        Extend the categories of a categorical covariate with new categories.

        Args:
            new_categories (np.ndarray): The new categories to add.

        Returns:
            None
        """
        if self.cov_type != "categorical":
            err = f"Covariate '{self.name}' is not a categorical covariate to extend."
            raise ValueError(err)
        if self.categories is None:  # to satisfy type checker
            err = f"Covariate '{self.name}' does not have categories defined."
            raise ValueError(err)

        # Make sure categories are unique and new
        unique_new_categories = np.setdiff1d(new_categories, self.categories)
        if unique_new_categories.size < new_categories.size:
            err = (
                f"Some new categories are already present in the "
                f"covariate '{self.name}'."
            )
            raise ValueError(err)

        # Extend the categories array
        self.categories = np.concatenate((self.categories, unique_new_categories))

extend_categories(new_categories: npt.NDArray[np.str_]) -> None

Extend the categories of a categorical covariate with new categories.

Parameters:

Name Type Description Default
new_categories ndarray

The new categories to add.

required

Returns:

Type Description
None

None

Source code in src/spectranorm/snm.py
def extend_categories(
    self,
    new_categories: npt.NDArray[np.str_],
) -> None:
    """
    Extend the categories of a categorical covariate with new categories.

    Args:
        new_categories (np.ndarray): The new categories to add.

    Returns:
        None
    """
    if self.cov_type != "categorical":
        err = f"Covariate '{self.name}' is not a categorical covariate to extend."
        raise ValueError(err)
    if self.categories is None:  # to satisfy type checker
        err = f"Covariate '{self.name}' does not have categories defined."
        raise ValueError(err)

    # Make sure categories are unique and new
    unique_new_categories = np.setdiff1d(new_categories, self.categories)
    if unique_new_categories.size < new_categories.size:
        err = (
            f"Some new categories are already present in the "
            f"covariate '{self.name}'."
        )
        raise ValueError(err)

    # Extend the categories array
    self.categories = np.concatenate((self.categories, unique_new_categories))

factorize_categories(values: npt.NDArray[np.str_]) -> npt.NDArray[np.int_]

Factorize categorical covariate values into numerical indices.

Parameters:

Name Type Description Default
values ndarray

The values to factorize.

required

Returns:

Type Description
NDArray[int_]

np.ndarray: The factorized numerical indices for the categories.

Source code in src/spectranorm/snm.py
def factorize_categories(
    self,
    values: npt.NDArray[np.str_],
) -> npt.NDArray[np.int_]:
    """
    Factorize categorical covariate values into numerical indices.

    Args:
        values (np.ndarray): The values to factorize.

    Returns:
        np.ndarray: The factorized numerical indices for the categories.
    """
    if self.cov_type != "categorical":
        err = (
            f"Covariate '{self.name}' is not a categorical "
            "covariate to be factorized."
        )
        raise ValueError(err)

    # Create a mapping from category values to indices
    if self.categories is None:  # to satisfy type checker
        err = f"Covariate '{self.name}' does not have categories defined."
        raise ValueError(err)
    category_mapping: dict[str, int] = {
        category: idx for idx, category in enumerate(self.categories)
    }
    # Factorize the values using the mapping
    return np.array([category_mapping[val] for val in values], dtype=int)

make_spline_bases(values: npt.NDArray[np.floating[Any]], *, include_intercept: bool = True) -> npt.NDArray[np.floating[Any]]

Create B-spline basis expansion functions for a given covariate.

Parameters:

Name Type Description Default
values ndarray

The values to create the spline basis functions for.

required

Returns:

Type Description
NDArray[floating[Any]]

np.ndarray: The B-spline basis function expansion.

Source code in src/spectranorm/snm.py
def make_spline_bases(
    self,
    values: npt.NDArray[np.floating[Any]],
    *,
    include_intercept: bool = True,
) -> npt.NDArray[np.floating[Any]]:
    """
    Create B-spline basis expansion functions for a given covariate.

    Args:
        values (np.ndarray): The values to create the spline basis functions for.

    Returns:
        np.ndarray: The B-spline basis function expansion.
    """
    if self.effect != "spline" or self.spline_spec is None:
        err = f"Covariate '{self.name}' is not a spline covariate."
        raise ValueError(err)

    # Create fixed set of knots if not already defined
    if self.spline_spec.knots is None:
        knots = np.linspace(
            self.spline_spec.lower_bound,
            self.spline_spec.upper_bound,
            self.spline_spec.df - self.spline_spec.degree + 1,
        )[1:-1].tolist()
    else:
        knots = self.spline_spec.knots

    # Create B-spline basis functions
    return np.array(
        patsy.bs(  # pyright: ignore[reportAttributeAccessIssue]
            values,
            knots=knots,
            df=self.spline_spec.df,
            degree=self.spline_spec.degree,
            lower_bound=self.spline_spec.lower_bound,
            upper_bound=self.spline_spec.upper_bound,
            include_intercept=include_intercept,
        ),
    )

DirectNormativeModel dataclass

Direct normative model implementation.

This class implements the direct normative modeling approach, which directly models the variable of interest using the specified covariates. It can be used to fit a model to data and predict normative centiles.

Attributes:

Name Type Description
spec NormativeModelSpec

NormativeModelSpec Specification of the normative model including variable of interest, covariates, and data source.

defaults dict[str, Any]

dict Default parameters for the model, including spline specifications, ADVI iterations, convergence tolerance, random seed, and Adam optimizer learning rates.

Source code in src/spectranorm/snm.py
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
@dataclass
class DirectNormativeModel:
    """
    Direct normative model implementation.

    This class implements the direct normative modeling approach, which
    directly models the variable of interest using the specified covariates.
    It can be used to fit a model to data and predict normative centiles.

    Attributes:
        spec: NormativeModelSpec
            Specification of the normative model including variable of interest,
            covariates, and data source.
        defaults: dict
            Default parameters for the model, including spline specifications,
            ADVI iterations, convergence tolerance, random seed, and Adam optimizer
            learning rates.
    """

    spec: NormativeModelSpec
    defaults: dict[str, Any] = field(
        default_factory=lambda: {
            "spline_df": DEFAULT_SPLINE_DF,
            "spline_degree": DEFAULT_SPLINE_DEGREE,
            "spline_extrapolation_factor": DEFAULT_SPLINE_EXTRAPOLATION_FACTOR,
            "advi_iterations": DEFAULT_ADVI_ITERATIONS,
            "advi_convergence_tolerance": DEFAULT_ADVI_CONVERGENCE_TOLERANCE,
            "random_seed": DEFAULT_RANDOM_SEED,
            "adam_learning_rate": DEFAULT_ADAM_LEARNING_RATE,
            "adam_learning_rate_decay": DEFAULT_ADAM_LEARNING_RATE_DECAY,
        },
    )

    def __repr__(self) -> str:
        """
        String representation of the DirectNormativeModel instance.
        """
        return f"DirectNormativeModel(spec={self.spec})"

    @staticmethod
    def _validate_init_args(
        model_type: ModelType,
        variable_of_interest: str,
        numerical_covariates: list[str],
        categorical_covariates: list[str],
        batch_covariates: list[str],
        nonlinear_covariates: list[str],
    ) -> None:
        # Validity checks for input parameters
        if model_type not in {"HBR", "BLR"}:
            err = f"Invalid model type '{model_type}'. Must be 'HBR' or 'BLR'."
            raise ValueError(err)
        for list_name, covariate_list in [
            ("numerical", numerical_covariates),
            ("categorical", categorical_covariates),
            ("batch", batch_covariates),
            ("nonlinear", nonlinear_covariates),
        ]:
            if not all(isinstance(item, str) for item in covariate_list):
                err = f"All covariate names must be strings: {list_name} covariates."
                raise TypeError(err)
        if not isinstance(variable_of_interest, str):
            err = "Variable of interest must be a string."
            raise TypeError(err)
        if not all(col in categorical_covariates for col in batch_covariates):
            err = "All batch covariates must be included in categorical covariates."
            raise ValueError(err)
        if not all(col in numerical_covariates for col in nonlinear_covariates):
            err = "All nonlinear covariates must be included in numerical covariates."
            raise ValueError(err)

    @classmethod
    def from_dataframe(
        cls,
        model_type: ModelType,
        dataframe: pd.DataFrame,
        variable_of_interest: str,
        numerical_covariates: list[str] | None = None,
        categorical_covariates: list[str] | None = None,
        batch_covariates: list[str] | None = None,
        nonlinear_covariates: list[str] | None = None,
        influencing_mean: list[str] | None = None,
        influencing_variance: list[str] | None = None,
        spline_kwargs: dict[str, Any] | None = None,
    ) -> DirectNormativeModel:
        """
        Initialize a normative model from a pandas DataFrame.

        Args:
            model_type: ModelType
                Type of the model to create, either "HBR" (Hierarchical Bayesian
                Regression) or "BLR" (Bayesian Linear Regression).
            dataframe: pd.DataFrame
                DataFrame containing the data.
            variable_of_interest: str
                Name of the target variable to model.
            numerical_covariates: list[str] | None
                List of numerical covariate names.
            categorical_covariates: list[str] | None
                List of categorical covariate names.
            batch_covariates: list[str] | None
                List of batch covariate names which should also be included in
                categorical_covariates.
            nonlinear_covariates: list[str] | None
                List of covariate names to be modeled as nonlinear effects.
                These should also be included in numerical_covariates.
            influencing_mean: list[str] | None
                List of covariate names that influence the mean of the variable
                of interest. These should be included in either numerical_covariates
                or categorical_covariates.
            influencing_variance: list[str] | None
                List of covariate names that influence the variance of the variable
                of interest. These should be included in either numerical_covariates
                or categorical_covariates.
            spline_kwargs: dict
                Additional keyword arguments for spline specification, such as
                `df`, `degree`, and `knots`. These are passed to the
                `create_spline_spec` method to create spline specifications for
                nonlinear covariates.

        Returns:
            DirectNormativeModel
                An instance of DirectNormativeModel initialized with the provided data.
        """
        # Set default values for optional parameters
        numerical_covariates = numerical_covariates or []
        categorical_covariates = categorical_covariates or []
        batch_covariates = batch_covariates or []
        nonlinear_covariates = nonlinear_covariates or []
        influencing_mean = influencing_mean or []
        influencing_variance = influencing_variance or []
        spline_kwargs = spline_kwargs or {}

        # Validity checks for input parameters
        cls._validate_init_args(
            model_type,
            variable_of_interest,
            numerical_covariates,
            categorical_covariates,
            batch_covariates,
            nonlinear_covariates,
        )
        utils.general.validate_dataframe(
            dataframe,
            [variable_of_interest, *numerical_covariates, *categorical_covariates],
        )

        # Create an instance of the class
        self = cls(
            spec=NormativeModelSpec(
                variable_of_interest=variable_of_interest,
                covariates=[],
                influencing_mean=influencing_mean,
                influencing_variance=influencing_variance,
            ),
        )

        # Populate the spline_kwargs with defaults if not provided
        spline_kwargs["df"] = spline_kwargs.get("df", self.defaults["spline_df"])
        spline_kwargs["degree"] = spline_kwargs.get(
            "degree",
            self.defaults["spline_degree"],
        )
        spline_kwargs["extrapolation_factor"] = spline_kwargs.get(
            "extrapolation_factor",
            self.defaults["spline_extrapolation_factor"],
        )

        # Start building the model specification
        # Add categorical covariates
        for cov_name in categorical_covariates:
            hierarchical = False
            if cov_name in batch_covariates and model_type == "HBR":
                hierarchical = True
            self.spec.covariates.append(
                CovariateSpec(
                    name=cov_name,
                    cov_type="categorical",
                    categories=dataframe[cov_name].unique(),
                    hierarchical=hierarchical,
                ),
            )
        for cov_name in numerical_covariates:
            if cov_name not in nonlinear_covariates:
                self.spec.covariates.append(
                    CovariateSpec(
                        name=cov_name,
                        cov_type="numerical",
                        effect="linear",
                        moments=(
                            dataframe[cov_name].mean(),
                            dataframe[cov_name].std(),
                        ),
                    ),
                )
            else:
                self.spec.covariates.append(
                    CovariateSpec(
                        name=cov_name,
                        cov_type="numerical",
                        effect="spline",
                        spline_spec=SplineSpec.create_spline_spec(
                            dataframe[cov_name],
                            **spline_kwargs,
                        ),
                    ),
                )
        return self

    def _validate_model(self) -> None:
        """
        Validate the model instance.

        This method checks if the model instance is complete and valid.
        It raises errors if any required fields are missing or if there are
        inconsistencies in the model specification.
        """
        if self.spec is None:
            err = (
                "Model specification is not set. "
                "Please initialize the model, e.g., with 'from_dataframe'."
            )
            raise ValueError(err)
        if len(self.spec.covariates) == 0:
            err = (
                "No covariates specified in the model. "
                "Please add covariates to the specification."
            )
            raise ValueError(err)
        if (len(self.spec.influencing_mean) == 0) and (
            len(self.spec.influencing_variance) == 0
        ):
            err = (
                "No covariates specified to influence the mean or "
                "variance of the variable of interest."
            )
            raise ValueError(err)

    def save_model(self, directory: Path, *, save_posterior: bool = False) -> None:
        """
        Save the fitted model and it's posterior to a directory.
        The model will be saved in a subdirectory named 'saved_model'.
        If this directory is not empty, an error is raised.

        Args:
            directory: Path
                Path to a directory to save the model.
            save_posterior: bool (default=False)
                If True, save the model's posterior trace inference data.
        """
        # Prepare the save directory
        directory = Path(directory)
        saved_model_dir = utils.general.prepare_save_directory(directory, "saved_model")

        model_dict = {
            "spec": self.spec,
            "defaults": self.defaults,
        }
        if hasattr(self, "model_params"):
            model_dict["model_params"] = self.model_params
            if hasattr(self, "model_inference_data") and save_posterior:
                self.model_inference_data.to_netcdf(
                    saved_model_dir / "model_inference_data.nc",
                )
        joblib.dump(model_dict, saved_model_dir / "model_dict.joblib")

    @classmethod
    def load_model(
        cls,
        directory: Path,
        *,
        load_posterior: bool = False,
    ) -> DirectNormativeModel:
        """
        Load the model and its posterior from a directory.
        The model will be loaded from a subdirectory named 'saved_model'.

        Args:
            directory: Path
                Path to the directory containing the model.
            load_posterior: bool (default=False)
                If True, load the model's posterior trace from the saved inference data.
        """
        # Validate the load directory
        directory = Path(directory)
        saved_model_dir = utils.general.validate_load_directory(
            directory,
            "saved_model",
        )

        # Load the saved model dict
        model_dict = joblib.load(saved_model_dir / "model_dict.joblib")

        # Create an instance of the class
        instance = cls(
            spec=model_dict["spec"],
        )

        # Set the attributes from the loaded model dictionary
        instance.defaults.update(model_dict["defaults"])
        if "model_params" in model_dict:
            instance.model_params = model_dict["model_params"]
            if load_posterior:
                instance.model_inference_data = az.from_netcdf(  # type: ignore[no-untyped-call]
                    saved_model_dir / "model_inference_data.nc",
                )

        return instance

    def _validate_dataframe_for_fitting(self, train_data: pd.DataFrame) -> None:
        """
        Validate the training DataFrame for fitting.
        """
        utils.general.validate_dataframe(
            train_data,
            (
                [cov.name for cov in self.spec.covariates]
                + [self.spec.variable_of_interest]
            ),
        )

    def _build_model_coordinates(
        self,
        observations: npt.NDArray[np.integer[Any]],
    ) -> dict[str, Any]:
        """
        Build the model coordinates for the training DataFrame.
        """
        # Data coordinates
        model_coords = {"observations": observations, "scalar": [0]}

        # Additional coordinates for covariates
        for cov in self.spec.covariates:
            if cov.cov_type == "numerical":
                if cov.effect == "spline":
                    if cov.spline_spec is not None:  # to satisfy type checker
                        model_coords[f"{cov.name}_splines"] = np.arange(
                            cov.spline_spec.df,
                        )
                elif cov.effect == "linear":
                    model_coords[f"{cov.name}_linear"] = np.arange(1)
            elif cov.cov_type == "categorical":
                model_coords[cov.name] = cov.categories
            else:
                err = f"Invalid covariate type '{cov.cov_type}' for '{cov.name}'."
                raise ValueError(err)
        return model_coords

    def _model_linear_mean_effect(
        self,
        train_data: pd.DataFrame,
        cov: CovariateSpec,
        effects_list: list[TensorVariable],
        sigma_prior: float = 10,
        adapt: dict[str, Any] | None = None,
    ) -> None:
        """
        Model a linear effect for a numerical covariate on the mean estimate.
        """
        # Linear effect
        if adapt is None:  # Model fitting
            linear_beta = pm.Normal(
                f"linear_beta_{cov.name}",
                mu=0,
                sigma=sigma_prior,
                size=1,
                dims=(f"{cov.name}_linear",),
            )
            # Increment parameter count for linear effect
            self.model_params["n_params"] += 1
        else:  # Freeze during adaptation/fine-tuning
            linear_beta = pm.Deterministic(
                f"linear_beta_{cov.name}",
                pt.as_tensor_variable(
                    adapt["pretrained_model_params"]["posterior_means"][
                        f"linear_beta_{cov.name}"
                    ],
                ),
                dims=(f"{cov.name}_linear",),
            )
        if cov.moments is not None:  # to satisfy type checker
            effects_list.append(
                (
                    cast("npt.NDArray[Any]", train_data[cov.name].to_numpy())
                    - cov.moments[0]
                )
                / cov.moments[1]
                * linear_beta,
            )

    def _model_spline_mean_effect(
        self,
        train_data: pd.DataFrame,
        cov: CovariateSpec,
        effects_list: list[TensorVariable],
        spline_bases: dict[str, npt.NDArray[np.floating[Any]]],
        sigma_prior: float = 10,
        adapt: dict[str, Any] | None = None,
    ) -> None:
        """
        Model a spline effect for a numerical covariate on the mean estimate.
        """
        # Spline effect
        spline_bases[cov.name] = spline_bases.get(
            cov.name,
            cov.make_spline_bases(
                cast("npt.NDArray[Any]", train_data[cov.name].to_numpy()),
            ),
        )
        if adapt is None:  # Model fitting
            # ZeroSumNormal imposes a centering constraint ensuring identifiability
            spline_betas = pm.ZeroSumNormal(
                f"spline_betas_{cov.name}",
                sigma=sigma_prior,
                shape=spline_bases[cov.name].shape[1],
                dims=(f"{cov.name}_splines",),
            )
            # Increment parameter count for spline effects
            if cov.spline_spec is not None:  # to satisfy type checker
                self.model_params["n_params"] += cov.spline_spec.df - 1
        else:  # Freeze during adaptation/fine-tuning
            spline_betas = pm.Deterministic(
                f"spline_betas_{cov.name}",
                pt.as_tensor_variable(
                    adapt["pretrained_model_params"]["posterior_means"][
                        f"spline_betas_{cov.name}"
                    ],
                ),
                dims=(f"{cov.name}_splines",),
            )
        effects_list.append(pt.dot(spline_bases[cov.name], spline_betas.T))

    def _model_categorical_mean_effect(
        self,
        train_data: pd.DataFrame,
        cov: CovariateSpec,
        effects_list: list[TensorVariable],
        category_indices: dict[str, npt.NDArray[np.integer[Any]]],
        sigma_prior: float = 10,
        hierarchical_sigma_prior: float = 1,
        adapt: dict[str, Any] | None = None,
    ) -> None:
        """
        Model the effect of a categorical covariate on the mean estimate.
        """
        # Factorize categories
        category_indices[cov.name] = category_indices.get(
            cov.name,
            cov.factorize_categories(
                cast("npt.NDArray[Any]", train_data[cov.name].to_numpy()),
            ),
        )
        if adapt is None:  # Model fitting
            if cov.hierarchical:
                # Hierarchical categorical effect
                # Hyperpriors for category (Bayesian equivalent of random effects)
                sigma_intercept_category = pm.HalfNormal(
                    f"sigma_intercept_{cov.name}",
                    sigma=sigma_prior,
                    dims=("scalar",),
                )

                # Hierarchical intercepts for each category (using reparameterized form)
                categorical_intercept_offset = pm.ZeroSumNormal(
                    f"intercept_offset_{cov.name}",
                    sigma=hierarchical_sigma_prior,
                    dims=(cov.name,),
                )
                # Note ZeroSumNormal imposes a centering constraint
                # (ensuring identifiability)
                categorical_intercept = pm.Deterministic(
                    f"intercept_{cov.name}",
                    (
                        categorical_intercept_offset
                        * pt.reshape(sigma_intercept_category, (1,))  # pyright: ignore[reportPrivateImportUsage]
                    ),
                    dims=(cov.name,),
                )

                # Increment parameter count for hierarchical intercept
                self.model_params["n_params"] += 1

            else:
                # Non-hierarchical (linear) categorical effect
                categorical_intercept = pm.ZeroSumNormal(
                    f"intercept_{cov.name}",
                    sigma=sigma_prior,
                    dims=(cov.name,),
                )
                # Note ZeroSumNormal imposes a centering constraint
                # (ensuring identifiability)
            # Increment parameter count for categorical effects
            if cov.categories is not None:  # to satisfy type checker
                self.model_params["n_params"] += len(cov.categories) - 1
        elif cov.name != adapt["covariate_to_adapt"]:
            # Freeze during adaptation/fine-tuning
            if cov.hierarchical:
                # Hierarchical categorical effect
                # Hyperpriors for category (Bayesian equivalent of random effects)
                sigma_intercept_category = pm.Deterministic(
                    f"sigma_intercept_{cov.name}",
                    pt.as_tensor_variable(
                        adapt["pretrained_model_params"]["posterior_means"][
                            f"sigma_intercept_{cov.name}"
                        ],
                    ),
                    dims=("scalar",),
                )
                # Hierarchical intercepts for each category (using reparameterized form)
                categorical_intercept_offset = pm.Deterministic(
                    f"intercept_offset_{cov.name}",
                    pt.as_tensor_variable(
                        adapt["pretrained_model_params"]["posterior_means"][
                            f"intercept_offset_{cov.name}"
                        ],
                    ),
                    dims=(cov.name,),
                )
                categorical_intercept = pm.Deterministic(
                    f"intercept_{cov.name}",
                    (
                        categorical_intercept_offset
                        * pt.reshape(sigma_intercept_category, (1,))  # pyright: ignore[reportPrivateImportUsage]
                    ),
                    dims=(cov.name,),
                )
            else:
                categorical_intercept = pm.Deterministic(
                    f"intercept_{cov.name}",
                    pt.as_tensor_variable(
                        adapt["pretrained_model_params"]["posterior_means"][
                            f"intercept_{cov.name}"
                        ],
                    ),
                    dims=(cov.name,),
                )
        else:  # Partial freezing (fit parameters for the new site only)
            if cov.hierarchical:
                # Hierarchical categorical effect
                # Hyperpriors for category (Bayesian equivalent of random effects)
                # Hyperpriors are fixed during adaptation
                sigma_intercept_category = pm.Deterministic(
                    f"sigma_intercept_{cov.name}",
                    pt.as_tensor_variable(
                        adapt["pretrained_model_params"]["posterior_means"][
                            f"sigma_intercept_{cov.name}"
                        ],
                    ),
                    dims=("scalar",),
                )
                # Hierarchical intercepts for each category (using reparameterized form)
                # New categories get new parameters, old categories are fixed
                # Freeze old category parameters during adaptation
                fixed_categorical_intercept_offset = pm.Deterministic(
                    f"intercept_offset_{cov.name}_fixed",
                    pt.as_tensor_variable(
                        adapt["pretrained_model_params"]["posterior_means"][
                            f"intercept_offset_{cov.name}"
                        ],
                    ),
                )
                # Create new parameters for new categories
                new_category_count = len(adapt["new_category_names"])
                pretrain_sigma_prior = adapt["pretrained_model_params"][
                    "posterior_means"
                ][f"variance_intercept_offset_{cov.name}"].std()
                new_categorical_intercept_offset = pm.Normal(
                    f"intercept_offset_{cov.name}_adapt",
                    mu=0,
                    sigma=pretrain_sigma_prior,
                    size=new_category_count,
                )
                # Combine fixed and new offsets
                categorical_intercept_offset = pm.Deterministic(
                    f"intercept_offset_{cov.name}",
                    pt.concatenate(
                        [
                            fixed_categorical_intercept_offset,
                            new_categorical_intercept_offset,
                        ],
                    ),
                    dims=(cov.name,),
                )
                categorical_intercept = pm.Deterministic(
                    f"intercept_{cov.name}",
                    (
                        categorical_intercept_offset
                        * pt.reshape(sigma_intercept_category, (1,))  # pyright: ignore[reportPrivateImportUsage]
                    ),
                    dims=(cov.name,),
                )
            else:
                # Non-hierarchical (linear) categorical effect
                # New categories get new parameters, old categories are fixed
                # Freeze old category parameters during adaptation
                fixed_categorical_intercept = pm.Deterministic(
                    f"intercept_{cov.name}_fixed",
                    pt.as_tensor_variable(
                        adapt["pretrained_model_params"]["posterior_means"][
                            f"intercept_{cov.name}"
                        ],
                    ),
                )
                # Create new parameters for new categories
                new_category_count = len(adapt["new_category_names"])
                new_categorical_intercept = pm.Normal(
                    f"intercept_{cov.name}_adapt",
                    mu=0,
                    sigma=sigma_prior,
                    size=new_category_count,
                )
                # Combine fixed and new offsets
                categorical_intercept = pm.Deterministic(
                    f"intercept_{cov.name}",
                    pt.concatenate(
                        [
                            fixed_categorical_intercept,
                            new_categorical_intercept,
                        ],
                    ),
                    dims=(cov.name,),
                )
            self.model_params["n_params"] += new_category_count
        effects_list.append(
            categorical_intercept[category_indices[cov.name]],
        )

    def _model_all_mean_effects(
        self,
        train_data: pd.DataFrame,
        spline_bases: dict[str, npt.NDArray[np.floating[Any]]],
        category_indices: dict[str, npt.NDArray[np.integer[Any]]],
        adapt: dict[str, Any] | None = None,
    ) -> list[TensorVariable]:
        """
        Model all covariate mean effects.
        """
        mean_effects = []
        # Model the global intercept
        if adapt is None:  # Model fitting
            global_intercept = pm.Normal(
                "global_intercept",
                mu=0,
                sigma=5,
                dims=("scalar",),
            )
            # Increment parameter count for global intercept
            self.model_params["n_params"] += 1
        else:  # Freeze during adaptation/fine-tuning
            global_intercept = pm.Deterministic(
                "global_intercept",
                pt.as_tensor_variable(
                    adapt["pretrained_model_params"]["posterior_means"][
                        "global_intercept"
                    ],
                ),
                dims=("scalar",),
            )
        mean_effects.append(global_intercept)
        # Model additional covariate effects on the mean
        for cov in self.spec.covariates:
            if cov.name in self.spec.influencing_mean:
                if cov.cov_type == "numerical":
                    if cov.effect == "linear":
                        self._model_linear_mean_effect(
                            train_data,
                            cov,
                            mean_effects,
                            sigma_prior=5,
                            adapt=adapt,
                        )
                    elif cov.effect == "spline":
                        self._model_spline_mean_effect(
                            train_data,
                            cov,
                            mean_effects,
                            spline_bases,
                            sigma_prior=5,
                            adapt=adapt,
                        )
                elif cov.cov_type == "categorical":
                    self._model_categorical_mean_effect(
                        train_data,
                        cov,
                        mean_effects,
                        category_indices,
                        sigma_prior=1,
                        hierarchical_sigma_prior=5,
                        adapt=adapt,
                    )
                else:
                    err = f"Invalid covariate type '{cov.cov_type}' for '{cov.name}'."
                    raise ValueError(err)
        return mean_effects

    def _model_linear_variance_effect(
        self,
        train_data: pd.DataFrame,
        cov: CovariateSpec,
        effects_list: list[TensorVariable],
        sigma_prior: float = 0.1,
        adapt: dict[str, Any] | None = None,
    ) -> None:
        """
        Model a linear effect for a numerical covariate on the variance estimate.
        """
        # Linear effect
        if adapt is None:  # Model fitting
            linear_beta = pm.Normal(
                f"variance_linear_beta_{cov.name}",
                mu=0,
                sigma=sigma_prior,
                size=1,
                dims=(f"{cov.name}_linear",),
            )
            # Increment parameter count for linear effect
            self.model_params["n_params"] += 1
        else:  # Freeze during adaptation/fine-tuning
            linear_beta = pm.Deterministic(
                f"variance_linear_beta_{cov.name}",
                pt.as_tensor_variable(
                    adapt["pretrained_model_params"]["posterior_means"][
                        f"variance_linear_beta_{cov.name}"
                    ],
                ),
                dims=(f"{cov.name}_linear",),
            )
        if cov.moments is not None:  # to satisfy type checker
            effects_list.append(
                (
                    cast("npt.NDArray[Any]", train_data[cov.name].to_numpy())
                    - cov.moments[0]
                )
                / cov.moments[1]
                * linear_beta,
            )

    def _model_spline_variance_effect(
        self,
        train_data: pd.DataFrame,
        cov: CovariateSpec,
        effects_list: list[TensorVariable],
        spline_bases: dict[str, npt.NDArray[np.floating[Any]]],
        sigma_prior: float = 0.1,
        adapt: dict[str, Any] | None = None,
    ) -> None:
        """
        Model a spline effect for a numerical covariate on the variance estimate.
        """
        # Spline effect
        spline_bases[cov.name] = spline_bases.get(
            cov.name,
            cov.make_spline_bases(
                cast("npt.NDArray[Any]", train_data[cov.name].to_numpy()),
            ),
        )
        if adapt is None:  # Model fitting
            spline_betas = pm.ZeroSumNormal(
                f"variance_spline_betas_{cov.name}",
                sigma=sigma_prior,
                shape=spline_bases[cov.name].shape[1],
                dims=(f"{cov.name}_splines",),
            )
            # Note ZeroSumNormal imposes a centering constraint
            # (ensuring identifiability)
            # Increment parameter count for spline effects
            if cov.spline_spec is not None:  # to satisfy type checker
                self.model_params["n_params"] += cov.spline_spec.df - 1
        else:  # Freeze during adaptation/fine-tuning
            spline_betas = pm.Deterministic(
                f"variance_spline_betas_{cov.name}",
                pt.as_tensor_variable(
                    adapt["pretrained_model_params"]["posterior_means"][
                        f"variance_spline_betas_{cov.name}"
                    ],
                ),
                dims=(f"{cov.name}_splines",),
            )
        effects_list.append(pt.dot(spline_bases[cov.name], spline_betas.T))

    def _model_categorical_variance_effect(
        self,
        train_data: pd.DataFrame,
        cov: CovariateSpec,
        effects_list: list[TensorVariable],
        category_indices: dict[str, npt.NDArray[np.integer[Any]]],
        sigma_prior: float = 0.1,
        hierarchical_sigma_prior: float = 0.1,
        adapt: dict[str, Any] | None = None,
    ) -> None:
        """
        Model the effect of a categorical covariate on the variance estimate.
        """
        # Factorize categories
        category_indices[cov.name] = category_indices.get(
            cov.name,
            cov.factorize_categories(
                cast("npt.NDArray[Any]", train_data[cov.name].to_numpy()),
            ),
        )
        if adapt is None:  # Model fitting
            if cov.hierarchical:
                # Hierarchical categorical effect
                # Hyperpriors for category (Bayesian equivalent of random effects)
                sigma_intercept_category = pm.HalfNormal(
                    f"variance_sigma_intercept_{cov.name}",
                    sigma=sigma_prior,
                    dims=("scalar",),
                )

                # Hierarchical intercepts for each category (using reparameterized form)
                categorical_intercept_offset = pm.ZeroSumNormal(
                    f"variance_intercept_offset_{cov.name}",
                    sigma=hierarchical_sigma_prior,
                    dims=(cov.name,),
                )
                # Note ZeroSumNormal imposes a centering constraint
                # (ensuring identifiability)
                categorical_intercept = pm.Deterministic(
                    f"variance_intercept_{cov.name}",
                    (
                        categorical_intercept_offset
                        * pt.reshape(sigma_intercept_category, (1,))  # pyright: ignore[reportPrivateImportUsage]
                    ),
                    dims=(cov.name,),
                )

                # Increment parameter count for hierarchical intercept
                self.model_params["n_params"] += 1

            else:
                # Non-hierarchical (linear) categorical effect
                categorical_intercept = pm.ZeroSumNormal(
                    f"variance_intercept_{cov.name}",
                    sigma=sigma_prior,
                    dims=(cov.name,),
                )
                # Note ZeroSumNormal imposes a centering constraint
                # (ensuring identifiability)
            # Increment parameter count for categorical effects
            if cov.categories is not None:  # to satisfy type checker
                self.model_params["n_params"] += len(cov.categories) - 1
        elif cov.name != adapt["covariate_to_adapt"]:
            # Freeze during adaptation/fine-tuning
            if cov.hierarchical:
                # Hierarchical categorical effect
                # Hyperpriors for category (Bayesian equivalent of random effects)
                sigma_intercept_category = pm.Deterministic(
                    f"variance_sigma_intercept_{cov.name}",
                    pt.as_tensor_variable(
                        adapt["pretrained_model_params"]["posterior_means"][
                            f"variance_sigma_intercept_{cov.name}"
                        ],
                    ),
                    dims=("scalar",),
                )
                # Hierarchical intercepts for each category (using reparameterized form)
                categorical_intercept_offset = pm.Deterministic(
                    f"variance_intercept_offset_{cov.name}",
                    pt.as_tensor_variable(
                        adapt["pretrained_model_params"]["posterior_means"][
                            f"variance_intercept_offset_{cov.name}"
                        ],
                    ),
                    dims=(cov.name,),
                )
                categorical_intercept = pm.Deterministic(
                    f"variance_intercept_{cov.name}",
                    (
                        categorical_intercept_offset
                        * pt.reshape(sigma_intercept_category, (1,))  # pyright: ignore[reportPrivateImportUsage]
                    ),
                    dims=(cov.name,),
                )
            else:
                categorical_intercept = pm.Deterministic(
                    f"variance_intercept_{cov.name}",
                    pt.as_tensor_variable(
                        adapt["pretrained_model_params"]["posterior_means"][
                            f"variance_intercept_{cov.name}"
                        ],
                    ),
                    dims=(cov.name,),
                )
        else:  # Partial freezing (fit parameters for the new site only)
            if cov.hierarchical:
                # Hierarchical categorical effect
                # Hyperpriors for category (Bayesian equivalent of random effects)
                # Hyperpriors are fixed during adaptation
                sigma_intercept_category = pm.Deterministic(
                    f"variance_sigma_intercept_{cov.name}",
                    pt.as_tensor_variable(
                        adapt["pretrained_model_params"]["posterior_means"][
                            f"variance_sigma_intercept_{cov.name}"
                        ],
                    ),
                    dims=("scalar",),
                )
                # Hierarchical intercepts for each category (using reparameterized form)
                # New categories get new parameters, old categories are fixed
                # Freeze old category parameters during adaptation
                fixed_categorical_intercept_offset = pm.Deterministic(
                    f"variance_intercept_offset_{cov.name}_fixed",
                    pt.as_tensor_variable(
                        adapt["pretrained_model_params"]["posterior_means"][
                            f"variance_intercept_offset_{cov.name}"
                        ],
                    ),
                )
                # Create new parameters for new categories
                new_category_count = len(adapt["new_category_names"])
                pretrain_sigma_prior = adapt["pretrained_model_params"][
                    "posterior_means"
                ][f"variance_intercept_offset_{cov.name}"].std()
                new_categorical_intercept_offset = pm.Normal(
                    f"variance_intercept_offset_{cov.name}_adapt",
                    mu=0,
                    sigma=pretrain_sigma_prior,
                    size=new_category_count,
                )
                # Combine fixed and new offsets
                categorical_intercept_offset = pm.Deterministic(
                    f"variance_intercept_offset_{cov.name}",
                    pt.concatenate(
                        [
                            fixed_categorical_intercept_offset,
                            new_categorical_intercept_offset,
                        ],
                    ),
                    dims=(cov.name,),
                )
                categorical_intercept = pm.Deterministic(
                    f"variance_intercept_{cov.name}",
                    (
                        categorical_intercept_offset
                        * pt.reshape(sigma_intercept_category, (1,))  # pyright: ignore[reportPrivateImportUsage]
                    ),
                    dims=(cov.name,),
                )
            else:
                # Non-hierarchical (linear) categorical effect
                # New categories get new parameters, old categories are fixed
                # Freeze old category parameters during adaptation
                fixed_categorical_intercept = pm.Deterministic(
                    f"variance_intercept_{cov.name}_fixed",
                    pt.as_tensor_variable(
                        adapt["pretrained_model_params"]["posterior_means"][
                            f"variance_intercept_{cov.name}"
                        ],
                    ),
                )
                # Create new parameters for new categories
                new_category_count = len(adapt["new_category_names"])
                new_categorical_intercept = pm.Normal(
                    f"variance_intercept_{cov.name}_adapt",
                    mu=0,
                    sigma=sigma_prior,
                    size=new_category_count,
                )
                # Combine fixed and new offsets
                categorical_intercept = pm.Deterministic(
                    f"variance_intercept_{cov.name}",
                    pt.concatenate(
                        [
                            fixed_categorical_intercept,
                            new_categorical_intercept,
                        ],
                    ),
                    dims=(cov.name,),
                )
            self.model_params["n_params"] += new_category_count
        effects_list.append(
            categorical_intercept[category_indices[cov.name]],
        )

    def _model_all_variance_effects(
        self,
        train_data: pd.DataFrame,
        spline_bases: dict[str, npt.NDArray[np.floating[Any]]],
        category_indices: dict[str, npt.NDArray[np.integer[Any]]],
        adapt: dict[str, Any] | None = None,
    ) -> list[TensorVariable]:
        """
        Model all covariate variance effects.
        """
        variance_effects = []
        # Model the global variance
        if adapt is None:
            global_variance_baseline = pm.Normal(
                "global_variance_baseline",
                mu=-0.0,
                sigma=0.5,
                dims=("scalar",),
            )
            # Increment parameter count for global variance
            self.model_params["n_params"] += 1
        else:
            global_variance_baseline = pm.Deterministic(
                "global_variance_baseline",
                pt.as_tensor_variable(
                    adapt["pretrained_model_params"]["posterior_means"][
                        "global_variance_baseline"
                    ],
                ),
                dims=("scalar",),
            )
        variance_effects.append(global_variance_baseline)
        # Model additional covariate effects on the variance
        for cov in self.spec.covariates:
            if cov.name in self.spec.influencing_variance:
                if cov.cov_type == "numerical":
                    if cov.effect == "linear":
                        self._model_linear_variance_effect(
                            train_data,
                            cov,
                            variance_effects,
                            sigma_prior=0.1,
                            adapt=adapt,
                        )
                    elif cov.effect == "spline":
                        self._model_spline_variance_effect(
                            train_data,
                            cov,
                            variance_effects,
                            spline_bases,
                            sigma_prior=0.1,
                            adapt=adapt,
                        )
                elif cov.cov_type == "categorical":
                    self._model_categorical_variance_effect(
                        train_data,
                        cov,
                        variance_effects,
                        category_indices,
                        sigma_prior=0.1,
                        hierarchical_sigma_prior=0.1,
                        adapt=adapt,
                    )
                else:
                    err = f"Invalid covariate type '{cov.cov_type}' for '{cov.name}'."
                    raise ValueError(err)
        return variance_effects

    def _combine_all_effects(
        self,
        mean_effects: list[TensorVariable],
        variance_effects: list[TensorVariable],
        standardized_voi: npt.NDArray[np.floating[Any]],
    ) -> None:
        """
        Combine all effects to model the observed data likelihood.
        """
        # Combine all mean and variance effects
        mu_estimate = sum(mean_effects)
        log_sigma_estimate = sum(variance_effects)
        sigma_estimate = pt.exp(log_sigma_estimate)

        effective_sample_size = self.model_params["sample_size"]

        # Model likelihood of the variable of interest
        _likelihood = pm.Normal(
            f"likelihood_{self.spec.variable_of_interest}",
            mu=mu_estimate,
            sigma=sigma_estimate,
            observed=standardized_voi,
            total_size=effective_sample_size,
        )

    def _fit_model_with_advi(self, *, progress_bar: bool = True) -> None:
        """
        Fit the model using Automatic Differentiation Variational Inference (ADVI).
        """
        base_lr = self.defaults["adam_learning_rate"]
        decay = self.defaults["adam_learning_rate_decay"]
        lr = shared(base_lr)
        optimizer = pm.adam(learning_rate=cast("float", lr))

        # Adaptive learning rate schedule callback
        def update_learning_rate(_approx: Any, _loss: Any, iteration: int) -> None:
            lr.set_value(base_lr * (decay**iteration))

        # Run automatic differential variational inference to fit the model
        self._trace = pm.fit(
            method="advi",
            n=self.defaults["advi_iterations"],
            random_seed=self.defaults["random_seed"],  # For reproducibility
            obj_optimizer=optimizer,
            callbacks=[
                update_learning_rate,
                pm.callbacks.CheckParametersConvergence(
                    tolerance=self.defaults["advi_convergence_tolerance"],
                    diff="relative",
                ),
            ],
            progressbar=progress_bar,
        )

        # Sample from the posterior distribution and store the results
        self.model_inference_data = self._trace.sample(
            2000,
            random_seed=self.defaults["random_seed"],
        )

        # Compute posterior means and standard deviations
        posterior_means = self.model_inference_data.posterior.mean(
            dim=("chain", "draw"),
        )
        posterior_stds = self.model_inference_data.posterior.std(dim=("chain", "draw"))

        # Store posterior means and stds as a dictionary in model parameters
        self.model_params["posterior_means"] = {
            x: posterior_means.data_vars[x].to_numpy()
            for x in posterior_means.data_vars
        }
        self.model_params["posterior_stds"] = {
            x: posterior_stds.data_vars[x].to_numpy() for x in posterior_stds.data_vars
        }

    def fit(
        self,
        train_data: pd.DataFrame,
        *,
        save_directory: Path | None = None,
        progress_bar: bool = True,
        adapt: dict[str, Any] | None = None,
    ) -> None:
        """
        Fit the normative model to the training data.

        This method implements the fitting logic for the normative model
        based on the provided training data and model specification.

        Args:
            train_data: pd.DataFrame
                DataFrame containing the training data. It must include the variable
                of interest and all specified covariates.
            save_directory: Path | None
                A path to a directory to save the model. If provided, the fitted model
                will be saved to this path.
            progress_bar: bool
                If True, display a progress bar during fitting. Defaults to True.
            adapt: dict[str, Any] | None
                If provided, adapt a pre-trained model to a new covariate.
                Note: We recommended using the `adapt_fit` method, and not directly
                changing this argument, unless you know what you are doing.
        """
        # Validation checks
        self._validate_model()
        self._validate_dataframe_for_fitting(train_data)

        # Extract the variable of interest
        variable_of_interest = train_data[self.spec.variable_of_interest].to_numpy()

        # A dictionary to hold the model parameters after fitting
        if adapt is None:
            self.model_params = {}
            self.model_params["mean_VOI"] = variable_of_interest.mean()
            self.model_params["std_VOI"] = variable_of_interest.std()
            self.model_params["sample_size"] = variable_of_interest.shape[0]
            # Initialize parameter count
            self.model_params["n_params"] = 0
        else:
            # Update the pretrained model parameters
            if not hasattr(self, "model_params") or self.model_params is None:
                self.model_params = copy.deepcopy(adapt["pretrained_model_params"])
            self.model_params["sample_size"] += variable_of_interest.shape[0]

        # Data preparation
        model_coords = self._build_model_coordinates(
            observations=np.arange(train_data.shape[0]),
        )

        # Fitting logic
        with pm.Model(coords=model_coords) as self._model:
            # Standardize the variable of interest, and store mean and std
            # This ensures that the model is not sensitive to the scale of the variable
            standardized_voi = (
                variable_of_interest - self.model_params["mean_VOI"]
            ) / self.model_params["std_VOI"]

            # A dictionary for precomputed bspline basis functions
            spline_bases: dict[str, npt.NDArray[np.floating[Any]]] = {}

            # A dictionary for factorized categories
            category_indices: dict[str, npt.NDArray[np.integer[Any]]] = {}

            # Model the mean of the variable of interest
            mean_effects = self._model_all_mean_effects(
                train_data,
                spline_bases,
                category_indices,
                adapt=adapt,
            )

            # Model the variance of the variable of interest
            variance_effects = self._model_all_variance_effects(
                train_data,
                spline_bases,
                category_indices,
                adapt=adapt,
            )

            # Combine all mean and variance effects
            self._combine_all_effects(
                mean_effects,
                variance_effects,
                standardized_voi,
            )

            # Fit the model using ADVI
            self._fit_model_with_advi(progress_bar=progress_bar)

        # Save the model if a save path is provided
        if save_directory is not None:
            self.save_model(Path(save_directory))

    def adapt_fit(
        self,
        covariate_to_adapt: str,
        new_category_names: npt.NDArray[np.str_],
        train_data: pd.DataFrame,
        *,
        pretrained_model_params: dict[str, Any] | None = None,
        save_directory: Path | None = None,
        progress_bar: bool = True,
    ) -> None:
        """
        Using a previously fitted model, adapt the model to a new batch.
        This method enables adaptation of the model to data from a new
        batch/site by freezing all fitted parameters, and only estimating
        new parameters for the new batch/site category.

        Args:
            covariate_to_adapt: str
                Name of the categorical covariate representing the batch/site
                to which the model should be adapted.
                Note: This covariate must have been specified in the original
                model.
            new_category_names: list[str]
                Names of the new categories in the covariate_to_adapt representing
                the new batch/site labels (e.g. names of the new site).
                Note: These names must not have been present in the original
                fitted model.
            train_data: pd.DataFrame
                DataFrame containing the training data for adaptation.
                It must include the variable of interest and all specified covariates.
                Note: The covariate_to_adapt column must only contain the
                new_category_names (no new data from previously trained batches).
            pretrained_model_params: dict[str, Any] | None
                The model parameters from a previously fitted model to adapt.
                If None, the model parameters from the current instance will be used
                (assuming fitting was done).
            save_directory: Path | None
                A path to a directory to save the adapted model. If provided,
                the fitted model will be saved to this path.
            progress_bar: bool
                If True, display a progress bar during fitting. Defaults to True.
        """
        # Validation checks
        self._validate_model()
        self._validate_dataframe_for_fitting(train_data)

        # Locate the covariate to adapt
        cov_to_adapt_index = [cov.name for cov in self.spec.covariates].index(
            covariate_to_adapt,
        )

        # Extend the covariate categories to include the new categories
        self.spec.covariates[cov_to_adapt_index].extend_categories(new_category_names)

        # Extract the pre-trained model parameters
        if pretrained_model_params is None:
            if not self.model_params:
                err = (
                    "No pretrained model parameters found. "
                    "Please provide pretrained_model_params or fit the model first."
                )
                raise ValueError(err)
            pretrained_model_params = copy.deepcopy(self.model_params)

        # Fit the adapted model
        self.fit(
            train_data,
            save_directory=save_directory,
            progress_bar=progress_bar,
            adapt={
                "covariate_to_adapt": covariate_to_adapt,
                "new_category_names": new_category_names,
                "pretrained_model_params": pretrained_model_params,
            },
        )

    def _predict_mu(
        self,
        test_covariates: pd.DataFrame,
        model_params: dict[str, Any],
        predict_without: list[str],
    ) -> npt.NDArray[np.floating[Any]]:
        """
        Internal method to predict the mean of the variable of interest.
        """
        # Calculate mean effect
        mu_estimate = np.full(
            test_covariates.shape[0],
            model_params["posterior_means"]["global_intercept"].item(),
        )

        for cov in self.spec.covariates:
            if (cov.name in self.spec.influencing_mean) and (
                cov.name not in predict_without
            ):
                if cov.cov_type == "numerical":
                    if cov.effect == "linear":
                        if cov.moments is not None:  # to satisfy type checker
                            mu_estimate += (
                                (
                                    cast(
                                        "npt.NDArray[Any]",
                                        test_covariates[cov.name].to_numpy(),
                                    )
                                    - cov.moments[0]
                                )
                                / cov.moments[1]
                            ) * model_params["posterior_means"][
                                f"linear_beta_{cov.name}"
                            ]
                    elif cov.effect == "spline":
                        spline_bases = cov.make_spline_bases(
                            cast(
                                "npt.NDArray[Any]",
                                test_covariates[cov.name].to_numpy(),
                            ),
                        )
                        spline_betas = model_params["posterior_means"][
                            f"spline_betas_{cov.name}"
                        ]
                        mu_estimate += np.dot(spline_bases, spline_betas)
                elif cov.cov_type == "categorical":
                    category_indices = cov.factorize_categories(
                        cast("npt.NDArray[Any]", test_covariates[cov.name].to_numpy()),
                    )
                    if cov.hierarchical:
                        categorical_intercept = (
                            model_params["posterior_means"][
                                f"intercept_offset_{cov.name}"
                            ]
                            * model_params["posterior_means"][
                                f"sigma_intercept_{cov.name}"
                            ]
                        )
                    else:
                        categorical_intercept = model_params["posterior_means"][
                            f"intercept_{cov.name}"
                        ]
                    mu_estimate += categorical_intercept[category_indices]

        return np.array(
            mu_estimate * model_params["std_VOI"] + model_params["mean_VOI"],
        )

    def _predict_std(
        self,
        test_covariates: pd.DataFrame,
        model_params: dict[str, Any],
        predict_without: list[str],
    ) -> npt.NDArray[np.floating[Any]]:
        """
        Internal method to predict the standard deviation of the variable of interest.
        """
        # Calculate deviation effect
        log_sigma_estimate = np.full(
            test_covariates.shape[0],
            model_params["posterior_means"]["global_variance_baseline"].item(),
        )

        for cov in self.spec.covariates:
            if (
                cov.name in self.spec.influencing_variance
                and cov.name not in predict_without
            ):
                if cov.cov_type == "numerical":
                    if cov.effect == "linear":
                        if cov.moments is not None:  # to satisfy type checker
                            log_sigma_estimate += (
                                (
                                    cast(
                                        "npt.NDArray[Any]",
                                        test_covariates[cov.name].to_numpy(),
                                    )
                                    - cov.moments[0]
                                )
                                / cov.moments[1]
                            ) * model_params["posterior_means"][
                                f"variance_linear_beta_{cov.name}"
                            ]
                    elif cov.effect == "spline":
                        spline_bases = cov.make_spline_bases(
                            cast(
                                "npt.NDArray[Any]",
                                test_covariates[cov.name].to_numpy(),
                            ),
                        )
                        variance_spline_betas = model_params["posterior_means"][
                            f"variance_spline_betas_{cov.name}"
                        ]
                        log_sigma_estimate += spline_bases @ variance_spline_betas
                elif cov.cov_type == "categorical":
                    category_indices = cov.factorize_categories(
                        cast("npt.NDArray[Any]", test_covariates[cov.name].to_numpy()),
                    )
                    if cov.hierarchical:
                        categorical_variance_intercept = (
                            model_params["posterior_means"][
                                f"variance_intercept_offset_{cov.name}"
                            ]
                            * model_params["posterior_means"][
                                f"variance_sigma_intercept_{cov.name}"
                            ]
                        )
                    else:
                        categorical_variance_intercept = model_params[
                            "posterior_means"
                        ][f"variance_intercept_{cov.name}"]
                    log_sigma_estimate += categorical_variance_intercept[
                        category_indices
                    ]

        return np.array(np.exp(log_sigma_estimate) * model_params["std_VOI"])

    def predict(
        self,
        test_covariates: pd.DataFrame,
        *,
        extended: bool = False,
        model_params: dict[str, Any] | None = None,
        predict_without: list[str] | None = None,
    ) -> NormativePredictions:
        """
        Predict normative moments (mean, std) for new data using the fitted model.

        Args:
            test_covariates: pd.DataFrame
                DataFrame containing the new covariate data to predict.
                This must include all specified covariates.
                Note: covariates listed in predict_without will be ignored and are
                hence not required.
            extended: bool
                If True, return additional stats such as log-likelihood, centiles, etc.
                Note that extended predictions require variable_of_interest to be
                provided in the test_covariates DataFrame.
            model_params: dict | None
                Optional dictionary of model parameters to use. If not provided,
                the stored parameters from model.fit() will be used.
            predict_without: list[str] | None
                Optional list of covariate names to ignore during prediction.
                This can be used to check the effect of removing certain covariates
                from the model.

        Returns:
            NormativePredictions: Object containing the predicted moments (mean, std)
                for the variable of interest.
        """
        # Validate the new data
        validation_columns = [
            cov.name
            for cov in self.spec.covariates
            if cov.name not in (predict_without or [])
        ]
        if extended:
            validation_columns.append(self.spec.variable_of_interest)
        utils.general.validate_dataframe(test_covariates, validation_columns)

        # Parameters
        model_params = model_params or self.model_params
        if model_params is None:
            err = "No model parameters found. Please provide model_params."
            raise ValueError(err)

        # Calculate mean and variance effects and store in the predictions object
        predictions = NormativePredictions(
            {
                "mu_estimate": self._predict_mu(
                    test_covariates,
                    model_params,
                    (predict_without or []),
                ),
                "std_estimate": self._predict_std(
                    test_covariates,
                    model_params,
                    (predict_without or []),
                ),
            },
        )

        # Check if extended predictions are requested
        if extended:
            # Add extended statistics to predictions (e.g. centiles, log loss, etc.)
            predictions.extend_predictions(
                variable_of_interest=(
                    cast(
                        "npt.NDArray[Any]",
                        test_covariates[self.spec.variable_of_interest].to_numpy(),
                    )
                ),
            )

        return predictions

    def evaluate(self, new_data: pd.DataFrame) -> NormativePredictions:
        """
        Evaluate the model on new data and return predictions.

        Args:
            new_data: pd.DataFrame
                DataFrame containing the new data to evaluate.
                It must include all specified covariates and the variable of interest.

        Returns:
            NormativePredictions: Object containing the predictions and evaluation
            metrics.
        """
        # Run extended predictions
        return self.predict(test_covariates=new_data).evaluate_predictions(
            variable_of_interest=(
                cast(
                    "npt.NDArray[Any]",
                    new_data[self.spec.variable_of_interest].to_numpy(),
                )
            ),
            train_mean=self.model_params["mean_VOI"],
            train_std=self.model_params["std_VOI"],
            n_params=self.model_params["n_params"],
        )

    def harmonize(
        self,
        data: pd.DataFrame,
        covariates_to_harmonize: list[str],
        *,
        model_params: dict[str, Any] | None = None,
    ) -> npt.NDArray[np.floating[Any]]:
        """
        Harmonize the variable of interest in the data to remove effects of
        certain covariates (e.g. batch).

        Args:
            data: pd.DataFrame
                DataFrame containing the data to harmonize.
                It must include all specified covariates and the variable of interest.
            covariates_to_harmonize: list[str]
                List of covariate names to harmonize.
                The partial effects of these covariates will be removed from the
                variable of interest, and the harmonized values will be returned.
            model_params: dict | None
                Optional dictionary of model parameters to use. If not provided,
                the stored parameters from model.fit() will be used.

        Returns:
            npt.NDArray[np.floating[Any]]: Array of harmonized values for the
                variable of interest.
        """
        # Validate the new data
        validation_columns = [cov.name for cov in self.spec.covariates]
        validation_columns.append(self.spec.variable_of_interest)
        utils.general.validate_dataframe(data, validation_columns)

        # Parameters
        if model_params is None:
            model_params = self.model_params

        # Predict the mean and std with all covariates
        full_predictions = self.predict(
            test_covariates=data,
            model_params=model_params,
            predict_without=[],
        )

        # Predict the mean and std without the covariates to harmonize
        reduced_predictions = self.predict(
            test_covariates=data,
            model_params=model_params,
            predict_without=covariates_to_harmonize,
        )

        # First standardize the variable of interest based on the full model
        voi_standardized = (
            (cast("npt.NDArray[Any]", data[self.spec.variable_of_interest].to_numpy()))
            - full_predictions.predictions["mu_estimate"]
        ) / full_predictions.predictions["std_estimate"]

        # Then return the harmonized values based on the reduced model
        return np.asarray(
            (
                voi_standardized * reduced_predictions.predictions["std_estimate"]
                + reduced_predictions.predictions["mu_estimate"]
            ),
            dtype=np.float64,
        )

adapt_fit(covariate_to_adapt: str, new_category_names: npt.NDArray[np.str_], train_data: pd.DataFrame, *, pretrained_model_params: dict[str, Any] | None = None, save_directory: Path | None = None, progress_bar: bool = True) -> None

Using a previously fitted model, adapt the model to a new batch. This method enables adaptation of the model to data from a new batch/site by freezing all fitted parameters, and only estimating new parameters for the new batch/site category.

Parameters:

Name Type Description Default
covariate_to_adapt str

str Name of the categorical covariate representing the batch/site to which the model should be adapted. Note: This covariate must have been specified in the original model.

required
new_category_names NDArray[str_]

list[str] Names of the new categories in the covariate_to_adapt representing the new batch/site labels (e.g. names of the new site). Note: These names must not have been present in the original fitted model.

required
train_data DataFrame

pd.DataFrame DataFrame containing the training data for adaptation. It must include the variable of interest and all specified covariates. Note: The covariate_to_adapt column must only contain the new_category_names (no new data from previously trained batches).

required
pretrained_model_params dict[str, Any] | None

dict[str, Any] | None The model parameters from a previously fitted model to adapt. If None, the model parameters from the current instance will be used (assuming fitting was done).

None
save_directory Path | None

Path | None A path to a directory to save the adapted model. If provided, the fitted model will be saved to this path.

None
progress_bar bool

bool If True, display a progress bar during fitting. Defaults to True.

True
Source code in src/spectranorm/snm.py
def adapt_fit(
    self,
    covariate_to_adapt: str,
    new_category_names: npt.NDArray[np.str_],
    train_data: pd.DataFrame,
    *,
    pretrained_model_params: dict[str, Any] | None = None,
    save_directory: Path | None = None,
    progress_bar: bool = True,
) -> None:
    """
    Using a previously fitted model, adapt the model to a new batch.
    This method enables adaptation of the model to data from a new
    batch/site by freezing all fitted parameters, and only estimating
    new parameters for the new batch/site category.

    Args:
        covariate_to_adapt: str
            Name of the categorical covariate representing the batch/site
            to which the model should be adapted.
            Note: This covariate must have been specified in the original
            model.
        new_category_names: list[str]
            Names of the new categories in the covariate_to_adapt representing
            the new batch/site labels (e.g. names of the new site).
            Note: These names must not have been present in the original
            fitted model.
        train_data: pd.DataFrame
            DataFrame containing the training data for adaptation.
            It must include the variable of interest and all specified covariates.
            Note: The covariate_to_adapt column must only contain the
            new_category_names (no new data from previously trained batches).
        pretrained_model_params: dict[str, Any] | None
            The model parameters from a previously fitted model to adapt.
            If None, the model parameters from the current instance will be used
            (assuming fitting was done).
        save_directory: Path | None
            A path to a directory to save the adapted model. If provided,
            the fitted model will be saved to this path.
        progress_bar: bool
            If True, display a progress bar during fitting. Defaults to True.
    """
    # Validation checks
    self._validate_model()
    self._validate_dataframe_for_fitting(train_data)

    # Locate the covariate to adapt
    cov_to_adapt_index = [cov.name for cov in self.spec.covariates].index(
        covariate_to_adapt,
    )

    # Extend the covariate categories to include the new categories
    self.spec.covariates[cov_to_adapt_index].extend_categories(new_category_names)

    # Extract the pre-trained model parameters
    if pretrained_model_params is None:
        if not self.model_params:
            err = (
                "No pretrained model parameters found. "
                "Please provide pretrained_model_params or fit the model first."
            )
            raise ValueError(err)
        pretrained_model_params = copy.deepcopy(self.model_params)

    # Fit the adapted model
    self.fit(
        train_data,
        save_directory=save_directory,
        progress_bar=progress_bar,
        adapt={
            "covariate_to_adapt": covariate_to_adapt,
            "new_category_names": new_category_names,
            "pretrained_model_params": pretrained_model_params,
        },
    )

evaluate(new_data: pd.DataFrame) -> NormativePredictions

Evaluate the model on new data and return predictions.

Parameters:

Name Type Description Default
new_data DataFrame

pd.DataFrame DataFrame containing the new data to evaluate. It must include all specified covariates and the variable of interest.

required

Returns:

Name Type Description
NormativePredictions NormativePredictions

Object containing the predictions and evaluation

NormativePredictions

metrics.

Source code in src/spectranorm/snm.py
def evaluate(self, new_data: pd.DataFrame) -> NormativePredictions:
    """
    Evaluate the model on new data and return predictions.

    Args:
        new_data: pd.DataFrame
            DataFrame containing the new data to evaluate.
            It must include all specified covariates and the variable of interest.

    Returns:
        NormativePredictions: Object containing the predictions and evaluation
        metrics.
    """
    # Run extended predictions
    return self.predict(test_covariates=new_data).evaluate_predictions(
        variable_of_interest=(
            cast(
                "npt.NDArray[Any]",
                new_data[self.spec.variable_of_interest].to_numpy(),
            )
        ),
        train_mean=self.model_params["mean_VOI"],
        train_std=self.model_params["std_VOI"],
        n_params=self.model_params["n_params"],
    )

fit(train_data: pd.DataFrame, *, save_directory: Path | None = None, progress_bar: bool = True, adapt: dict[str, Any] | None = None) -> None

Fit the normative model to the training data.

This method implements the fitting logic for the normative model based on the provided training data and model specification.

Parameters:

Name Type Description Default
train_data DataFrame

pd.DataFrame DataFrame containing the training data. It must include the variable of interest and all specified covariates.

required
save_directory Path | None

Path | None A path to a directory to save the model. If provided, the fitted model will be saved to this path.

None
progress_bar bool

bool If True, display a progress bar during fitting. Defaults to True.

True
adapt dict[str, Any] | None

dict[str, Any] | None If provided, adapt a pre-trained model to a new covariate. Note: We recommended using the adapt_fit method, and not directly changing this argument, unless you know what you are doing.

None
Source code in src/spectranorm/snm.py
def fit(
    self,
    train_data: pd.DataFrame,
    *,
    save_directory: Path | None = None,
    progress_bar: bool = True,
    adapt: dict[str, Any] | None = None,
) -> None:
    """
    Fit the normative model to the training data.

    This method implements the fitting logic for the normative model
    based on the provided training data and model specification.

    Args:
        train_data: pd.DataFrame
            DataFrame containing the training data. It must include the variable
            of interest and all specified covariates.
        save_directory: Path | None
            A path to a directory to save the model. If provided, the fitted model
            will be saved to this path.
        progress_bar: bool
            If True, display a progress bar during fitting. Defaults to True.
        adapt: dict[str, Any] | None
            If provided, adapt a pre-trained model to a new covariate.
            Note: We recommended using the `adapt_fit` method, and not directly
            changing this argument, unless you know what you are doing.
    """
    # Validation checks
    self._validate_model()
    self._validate_dataframe_for_fitting(train_data)

    # Extract the variable of interest
    variable_of_interest = train_data[self.spec.variable_of_interest].to_numpy()

    # A dictionary to hold the model parameters after fitting
    if adapt is None:
        self.model_params = {}
        self.model_params["mean_VOI"] = variable_of_interest.mean()
        self.model_params["std_VOI"] = variable_of_interest.std()
        self.model_params["sample_size"] = variable_of_interest.shape[0]
        # Initialize parameter count
        self.model_params["n_params"] = 0
    else:
        # Update the pretrained model parameters
        if not hasattr(self, "model_params") or self.model_params is None:
            self.model_params = copy.deepcopy(adapt["pretrained_model_params"])
        self.model_params["sample_size"] += variable_of_interest.shape[0]

    # Data preparation
    model_coords = self._build_model_coordinates(
        observations=np.arange(train_data.shape[0]),
    )

    # Fitting logic
    with pm.Model(coords=model_coords) as self._model:
        # Standardize the variable of interest, and store mean and std
        # This ensures that the model is not sensitive to the scale of the variable
        standardized_voi = (
            variable_of_interest - self.model_params["mean_VOI"]
        ) / self.model_params["std_VOI"]

        # A dictionary for precomputed bspline basis functions
        spline_bases: dict[str, npt.NDArray[np.floating[Any]]] = {}

        # A dictionary for factorized categories
        category_indices: dict[str, npt.NDArray[np.integer[Any]]] = {}

        # Model the mean of the variable of interest
        mean_effects = self._model_all_mean_effects(
            train_data,
            spline_bases,
            category_indices,
            adapt=adapt,
        )

        # Model the variance of the variable of interest
        variance_effects = self._model_all_variance_effects(
            train_data,
            spline_bases,
            category_indices,
            adapt=adapt,
        )

        # Combine all mean and variance effects
        self._combine_all_effects(
            mean_effects,
            variance_effects,
            standardized_voi,
        )

        # Fit the model using ADVI
        self._fit_model_with_advi(progress_bar=progress_bar)

    # Save the model if a save path is provided
    if save_directory is not None:
        self.save_model(Path(save_directory))

from_dataframe(model_type: ModelType, dataframe: pd.DataFrame, variable_of_interest: str, numerical_covariates: list[str] | None = None, categorical_covariates: list[str] | None = None, batch_covariates: list[str] | None = None, nonlinear_covariates: list[str] | None = None, influencing_mean: list[str] | None = None, influencing_variance: list[str] | None = None, spline_kwargs: dict[str, Any] | None = None) -> DirectNormativeModel classmethod

Initialize a normative model from a pandas DataFrame.

Parameters:

Name Type Description Default
model_type ModelType

ModelType Type of the model to create, either "HBR" (Hierarchical Bayesian Regression) or "BLR" (Bayesian Linear Regression).

required
dataframe DataFrame

pd.DataFrame DataFrame containing the data.

required
variable_of_interest str

str Name of the target variable to model.

required
numerical_covariates list[str] | None

list[str] | None List of numerical covariate names.

None
categorical_covariates list[str] | None

list[str] | None List of categorical covariate names.

None
batch_covariates list[str] | None

list[str] | None List of batch covariate names which should also be included in categorical_covariates.

None
nonlinear_covariates list[str] | None

list[str] | None List of covariate names to be modeled as nonlinear effects. These should also be included in numerical_covariates.

None
influencing_mean list[str] | None

list[str] | None List of covariate names that influence the mean of the variable of interest. These should be included in either numerical_covariates or categorical_covariates.

None
influencing_variance list[str] | None

list[str] | None List of covariate names that influence the variance of the variable of interest. These should be included in either numerical_covariates or categorical_covariates.

None
spline_kwargs dict[str, Any] | None

dict Additional keyword arguments for spline specification, such as df, degree, and knots. These are passed to the create_spline_spec method to create spline specifications for nonlinear covariates.

None

Returns:

Type Description
DirectNormativeModel

DirectNormativeModel An instance of DirectNormativeModel initialized with the provided data.

Source code in src/spectranorm/snm.py
@classmethod
def from_dataframe(
    cls,
    model_type: ModelType,
    dataframe: pd.DataFrame,
    variable_of_interest: str,
    numerical_covariates: list[str] | None = None,
    categorical_covariates: list[str] | None = None,
    batch_covariates: list[str] | None = None,
    nonlinear_covariates: list[str] | None = None,
    influencing_mean: list[str] | None = None,
    influencing_variance: list[str] | None = None,
    spline_kwargs: dict[str, Any] | None = None,
) -> DirectNormativeModel:
    """
    Initialize a normative model from a pandas DataFrame.

    Args:
        model_type: ModelType
            Type of the model to create, either "HBR" (Hierarchical Bayesian
            Regression) or "BLR" (Bayesian Linear Regression).
        dataframe: pd.DataFrame
            DataFrame containing the data.
        variable_of_interest: str
            Name of the target variable to model.
        numerical_covariates: list[str] | None
            List of numerical covariate names.
        categorical_covariates: list[str] | None
            List of categorical covariate names.
        batch_covariates: list[str] | None
            List of batch covariate names which should also be included in
            categorical_covariates.
        nonlinear_covariates: list[str] | None
            List of covariate names to be modeled as nonlinear effects.
            These should also be included in numerical_covariates.
        influencing_mean: list[str] | None
            List of covariate names that influence the mean of the variable
            of interest. These should be included in either numerical_covariates
            or categorical_covariates.
        influencing_variance: list[str] | None
            List of covariate names that influence the variance of the variable
            of interest. These should be included in either numerical_covariates
            or categorical_covariates.
        spline_kwargs: dict
            Additional keyword arguments for spline specification, such as
            `df`, `degree`, and `knots`. These are passed to the
            `create_spline_spec` method to create spline specifications for
            nonlinear covariates.

    Returns:
        DirectNormativeModel
            An instance of DirectNormativeModel initialized with the provided data.
    """
    # Set default values for optional parameters
    numerical_covariates = numerical_covariates or []
    categorical_covariates = categorical_covariates or []
    batch_covariates = batch_covariates or []
    nonlinear_covariates = nonlinear_covariates or []
    influencing_mean = influencing_mean or []
    influencing_variance = influencing_variance or []
    spline_kwargs = spline_kwargs or {}

    # Validity checks for input parameters
    cls._validate_init_args(
        model_type,
        variable_of_interest,
        numerical_covariates,
        categorical_covariates,
        batch_covariates,
        nonlinear_covariates,
    )
    utils.general.validate_dataframe(
        dataframe,
        [variable_of_interest, *numerical_covariates, *categorical_covariates],
    )

    # Create an instance of the class
    self = cls(
        spec=NormativeModelSpec(
            variable_of_interest=variable_of_interest,
            covariates=[],
            influencing_mean=influencing_mean,
            influencing_variance=influencing_variance,
        ),
    )

    # Populate the spline_kwargs with defaults if not provided
    spline_kwargs["df"] = spline_kwargs.get("df", self.defaults["spline_df"])
    spline_kwargs["degree"] = spline_kwargs.get(
        "degree",
        self.defaults["spline_degree"],
    )
    spline_kwargs["extrapolation_factor"] = spline_kwargs.get(
        "extrapolation_factor",
        self.defaults["spline_extrapolation_factor"],
    )

    # Start building the model specification
    # Add categorical covariates
    for cov_name in categorical_covariates:
        hierarchical = False
        if cov_name in batch_covariates and model_type == "HBR":
            hierarchical = True
        self.spec.covariates.append(
            CovariateSpec(
                name=cov_name,
                cov_type="categorical",
                categories=dataframe[cov_name].unique(),
                hierarchical=hierarchical,
            ),
        )
    for cov_name in numerical_covariates:
        if cov_name not in nonlinear_covariates:
            self.spec.covariates.append(
                CovariateSpec(
                    name=cov_name,
                    cov_type="numerical",
                    effect="linear",
                    moments=(
                        dataframe[cov_name].mean(),
                        dataframe[cov_name].std(),
                    ),
                ),
            )
        else:
            self.spec.covariates.append(
                CovariateSpec(
                    name=cov_name,
                    cov_type="numerical",
                    effect="spline",
                    spline_spec=SplineSpec.create_spline_spec(
                        dataframe[cov_name],
                        **spline_kwargs,
                    ),
                ),
            )
    return self

harmonize(data: pd.DataFrame, covariates_to_harmonize: list[str], *, model_params: dict[str, Any] | None = None) -> npt.NDArray[np.floating[Any]]

Harmonize the variable of interest in the data to remove effects of certain covariates (e.g. batch).

Parameters:

Name Type Description Default
data DataFrame

pd.DataFrame DataFrame containing the data to harmonize. It must include all specified covariates and the variable of interest.

required
covariates_to_harmonize list[str]

list[str] List of covariate names to harmonize. The partial effects of these covariates will be removed from the variable of interest, and the harmonized values will be returned.

required
model_params dict[str, Any] | None

dict | None Optional dictionary of model parameters to use. If not provided, the stored parameters from model.fit() will be used.

None

Returns:

Type Description
NDArray[floating[Any]]

npt.NDArray[np.floating[Any]]: Array of harmonized values for the variable of interest.

Source code in src/spectranorm/snm.py
def harmonize(
    self,
    data: pd.DataFrame,
    covariates_to_harmonize: list[str],
    *,
    model_params: dict[str, Any] | None = None,
) -> npt.NDArray[np.floating[Any]]:
    """
    Harmonize the variable of interest in the data to remove effects of
    certain covariates (e.g. batch).

    Args:
        data: pd.DataFrame
            DataFrame containing the data to harmonize.
            It must include all specified covariates and the variable of interest.
        covariates_to_harmonize: list[str]
            List of covariate names to harmonize.
            The partial effects of these covariates will be removed from the
            variable of interest, and the harmonized values will be returned.
        model_params: dict | None
            Optional dictionary of model parameters to use. If not provided,
            the stored parameters from model.fit() will be used.

    Returns:
        npt.NDArray[np.floating[Any]]: Array of harmonized values for the
            variable of interest.
    """
    # Validate the new data
    validation_columns = [cov.name for cov in self.spec.covariates]
    validation_columns.append(self.spec.variable_of_interest)
    utils.general.validate_dataframe(data, validation_columns)

    # Parameters
    if model_params is None:
        model_params = self.model_params

    # Predict the mean and std with all covariates
    full_predictions = self.predict(
        test_covariates=data,
        model_params=model_params,
        predict_without=[],
    )

    # Predict the mean and std without the covariates to harmonize
    reduced_predictions = self.predict(
        test_covariates=data,
        model_params=model_params,
        predict_without=covariates_to_harmonize,
    )

    # First standardize the variable of interest based on the full model
    voi_standardized = (
        (cast("npt.NDArray[Any]", data[self.spec.variable_of_interest].to_numpy()))
        - full_predictions.predictions["mu_estimate"]
    ) / full_predictions.predictions["std_estimate"]

    # Then return the harmonized values based on the reduced model
    return np.asarray(
        (
            voi_standardized * reduced_predictions.predictions["std_estimate"]
            + reduced_predictions.predictions["mu_estimate"]
        ),
        dtype=np.float64,
    )

load_model(directory: Path, *, load_posterior: bool = False) -> DirectNormativeModel classmethod

Load the model and its posterior from a directory. The model will be loaded from a subdirectory named 'saved_model'.

Parameters:

Name Type Description Default
directory Path

Path Path to the directory containing the model.

required
load_posterior bool

bool (default=False) If True, load the model's posterior trace from the saved inference data.

False
Source code in src/spectranorm/snm.py
@classmethod
def load_model(
    cls,
    directory: Path,
    *,
    load_posterior: bool = False,
) -> DirectNormativeModel:
    """
    Load the model and its posterior from a directory.
    The model will be loaded from a subdirectory named 'saved_model'.

    Args:
        directory: Path
            Path to the directory containing the model.
        load_posterior: bool (default=False)
            If True, load the model's posterior trace from the saved inference data.
    """
    # Validate the load directory
    directory = Path(directory)
    saved_model_dir = utils.general.validate_load_directory(
        directory,
        "saved_model",
    )

    # Load the saved model dict
    model_dict = joblib.load(saved_model_dir / "model_dict.joblib")

    # Create an instance of the class
    instance = cls(
        spec=model_dict["spec"],
    )

    # Set the attributes from the loaded model dictionary
    instance.defaults.update(model_dict["defaults"])
    if "model_params" in model_dict:
        instance.model_params = model_dict["model_params"]
        if load_posterior:
            instance.model_inference_data = az.from_netcdf(  # type: ignore[no-untyped-call]
                saved_model_dir / "model_inference_data.nc",
            )

    return instance

predict(test_covariates: pd.DataFrame, *, extended: bool = False, model_params: dict[str, Any] | None = None, predict_without: list[str] | None = None) -> NormativePredictions

Predict normative moments (mean, std) for new data using the fitted model.

Parameters:

Name Type Description Default
test_covariates DataFrame

pd.DataFrame DataFrame containing the new covariate data to predict. This must include all specified covariates. Note: covariates listed in predict_without will be ignored and are hence not required.

required
extended bool

bool If True, return additional stats such as log-likelihood, centiles, etc. Note that extended predictions require variable_of_interest to be provided in the test_covariates DataFrame.

False
model_params dict[str, Any] | None

dict | None Optional dictionary of model parameters to use. If not provided, the stored parameters from model.fit() will be used.

None
predict_without list[str] | None

list[str] | None Optional list of covariate names to ignore during prediction. This can be used to check the effect of removing certain covariates from the model.

None

Returns:

Name Type Description
NormativePredictions NormativePredictions

Object containing the predicted moments (mean, std) for the variable of interest.

Source code in src/spectranorm/snm.py
def predict(
    self,
    test_covariates: pd.DataFrame,
    *,
    extended: bool = False,
    model_params: dict[str, Any] | None = None,
    predict_without: list[str] | None = None,
) -> NormativePredictions:
    """
    Predict normative moments (mean, std) for new data using the fitted model.

    Args:
        test_covariates: pd.DataFrame
            DataFrame containing the new covariate data to predict.
            This must include all specified covariates.
            Note: covariates listed in predict_without will be ignored and are
            hence not required.
        extended: bool
            If True, return additional stats such as log-likelihood, centiles, etc.
            Note that extended predictions require variable_of_interest to be
            provided in the test_covariates DataFrame.
        model_params: dict | None
            Optional dictionary of model parameters to use. If not provided,
            the stored parameters from model.fit() will be used.
        predict_without: list[str] | None
            Optional list of covariate names to ignore during prediction.
            This can be used to check the effect of removing certain covariates
            from the model.

    Returns:
        NormativePredictions: Object containing the predicted moments (mean, std)
            for the variable of interest.
    """
    # Validate the new data
    validation_columns = [
        cov.name
        for cov in self.spec.covariates
        if cov.name not in (predict_without or [])
    ]
    if extended:
        validation_columns.append(self.spec.variable_of_interest)
    utils.general.validate_dataframe(test_covariates, validation_columns)

    # Parameters
    model_params = model_params or self.model_params
    if model_params is None:
        err = "No model parameters found. Please provide model_params."
        raise ValueError(err)

    # Calculate mean and variance effects and store in the predictions object
    predictions = NormativePredictions(
        {
            "mu_estimate": self._predict_mu(
                test_covariates,
                model_params,
                (predict_without or []),
            ),
            "std_estimate": self._predict_std(
                test_covariates,
                model_params,
                (predict_without or []),
            ),
        },
    )

    # Check if extended predictions are requested
    if extended:
        # Add extended statistics to predictions (e.g. centiles, log loss, etc.)
        predictions.extend_predictions(
            variable_of_interest=(
                cast(
                    "npt.NDArray[Any]",
                    test_covariates[self.spec.variable_of_interest].to_numpy(),
                )
            ),
        )

    return predictions

save_model(directory: Path, *, save_posterior: bool = False) -> None

Save the fitted model and it's posterior to a directory. The model will be saved in a subdirectory named 'saved_model'. If this directory is not empty, an error is raised.

Parameters:

Name Type Description Default
directory Path

Path Path to a directory to save the model.

required
save_posterior bool

bool (default=False) If True, save the model's posterior trace inference data.

False
Source code in src/spectranorm/snm.py
def save_model(self, directory: Path, *, save_posterior: bool = False) -> None:
    """
    Save the fitted model and it's posterior to a directory.
    The model will be saved in a subdirectory named 'saved_model'.
    If this directory is not empty, an error is raised.

    Args:
        directory: Path
            Path to a directory to save the model.
        save_posterior: bool (default=False)
            If True, save the model's posterior trace inference data.
    """
    # Prepare the save directory
    directory = Path(directory)
    saved_model_dir = utils.general.prepare_save_directory(directory, "saved_model")

    model_dict = {
        "spec": self.spec,
        "defaults": self.defaults,
    }
    if hasattr(self, "model_params"):
        model_dict["model_params"] = self.model_params
        if hasattr(self, "model_inference_data") and save_posterior:
            self.model_inference_data.to_netcdf(
                saved_model_dir / "model_inference_data.nc",
            )
    joblib.dump(model_dict, saved_model_dir / "model_dict.joblib")

NormativeModelSpec dataclass

General specification of a normative model.

Attributes:

Name Type Description
variable_of_interest str

str Name of the target variable to model (e.g., "thickness").

covariates list[CovariateSpec]

list[CovariateSpec] Listing all model covariates and specifying how each covariate is modeled.

influencing_mean list[str]

list[str] List of covariate names that influence the mean of the variable of interest.

influencing_variance list[str]

list[str] List of covariate names that influence the variance of the variable of interest.

Source code in src/spectranorm/snm.py
@dataclass
class NormativeModelSpec:
    """
    General specification of a normative model.

    Attributes:
        variable_of_interest: str
            Name of the target variable to model (e.g., "thickness").
        covariates: list[CovariateSpec]
            Listing all model covariates and specifying how each covariate is modeled.
        influencing_mean: list[str]
            List of covariate names that influence the mean of the variable of interest.
        influencing_variance: list[str]
            List of covariate names that influence the variance of the variable of
            interest.
    """

    variable_of_interest: str
    covariates: list[CovariateSpec]
    influencing_mean: list[str]
    influencing_variance: list[str]

    def __post_init__(self) -> None:
        if not isinstance(self.variable_of_interest, str):
            err = "variable_of_interest must be a string."
            raise TypeError(err)
        if not isinstance(self.covariates, list):
            err = "covariates must be a list of CovariateSpec instances."
            raise TypeError(err)
        if not all(isinstance(cov, CovariateSpec) for cov in self.covariates):
            err = "All items in covariates must be CovariateSpec instances."
            raise TypeError(err)
        if not isinstance(self.influencing_mean, list):
            err = "influencing_mean must be a list of covariate names."
            raise TypeError(err)
        if not isinstance(self.influencing_variance, list):
            err = "influencing_variance must be a list of covariate names."
            raise TypeError(err)

NormativePredictions dataclass

Container for the results of model.predict() function.

Attributes:

Name Type Description
predictions dict[str, NDArray[floating[Any]]]

dict Dictionary containing the model's predictions, including - Predictions of mean (mu_estimate). - Predictions of standard deviation (std_estimate). - [Optional] The observed variable of interest (the name of which is provided in the function argument). - [Optional] Additional evaluation metrics for the predictions.

Source code in src/spectranorm/snm.py
@dataclass
class NormativePredictions:
    """
    Container for the results of model.predict() function.

    Attributes:
        predictions: dict
            Dictionary containing the model's predictions, including
            - Predictions of mean (mu_estimate).
            - Predictions of standard deviation (std_estimate).
            - [Optional] The observed variable of interest (the name of which is
              provided in the function argument).
            - [Optional] Additional evaluation metrics for the predictions.
    """

    predictions: dict[str, npt.NDArray[np.floating[Any]]]
    evaluations: dict[str, npt.NDArray[np.floating[Any]] | float] = field(
        default_factory=dict,
    )

    def extend_predictions(
        self,
        variable_of_interest: npt.NDArray[np.floating[Any]],
        *,
        likelihood_censoring_quantile: float = 0.01,
    ) -> NormativePredictions:
        """
        Extend the NormativePredictions (predictions dictionary) with additional
        statistics.

        Args:
            variable_of_interest: np.ndarray
                The observed values for the variable(s) of interest.
            likelihood_censoring_quantile: float (default=0.01)
                Quantile below which log-likelihoods are censored for evaluation.
                Note: by default, a censored log-likelihood is computed to avoid
                extreme log-likelihood values for outliers. This affects several
                resulting metrics that are based on log-likelihoods (e.g. MSLL).
                If you want to compute the full (uncensored) log-likelihoods, set
                `likelihood_censoring_quantile` to 0.

        Returns:
            NormativePredictions
                Extended NormativePredictions with additional statistics.
        """
        self.predictions["z-score"] = (
            variable_of_interest - self.predictions["mu_estimate"]
        ) / self.predictions["std_estimate"]
        self.predictions["log-likelihood"] = (
            utils.stats.compute_censored_log_likelihood(
                variable_of_interest,
                self.predictions["mu_estimate"],
                self.predictions["std_estimate"],
                censored_quantile=likelihood_censoring_quantile,
            )
        )
        self.predictions["centiles"] = utils.stats.compute_centiles_from_z_scores(
            self.predictions["z-score"],
        )

        self.predictions["variable_of_interest"] = variable_of_interest

        return self

    def evaluate_predictions(
        self,
        variable_of_interest: npt.NDArray[np.floating[Any]],
        train_mean: npt.NDArray[np.floating[Any]],
        train_std: npt.NDArray[np.floating[Any]],
        n_params: int | None = None,
        msll_censored_quantile: float = 0.01,
    ) -> NormativePredictions:
        """
        Evaluate the predictions against the observed variable of interest.

        This function computes a battery of evaluation metrics implemented
        in `snm.utils.metrics`. Namely the evaluations include:
            - Mean Absolute Error (MAE)
            - Mean Squared Error (MSE)
            - Root Mean Squared Error (RMSE)
            - Mean Absolute Percentage Error (MAPE)
            - R-squared
            - Explained Variance Score
            - Mean Standardized Log Loss (MSLL)

        Args:
            variable_of_interest: np.ndarray
                The observed values for the variable(s) of interest.
            train_mean: np.ndarray
                Mean(s) of the variable(s) of interest from the training data.
            train_std: np.ndarray
                Standard deviation(s) of the variable(s) of interest from the training
                data.
            n_params: int
                Number of free parameters in the model.
            msll_censored_quantile: float (default=0.02)
                Quantile below which log-likelihoods are censored for MSLL.

        Returns:
            NormativePredictions
                Object containing the evaluation results.
        """
        self.extend_predictions(
            variable_of_interest,
            likelihood_censoring_quantile=msll_censored_quantile,
        )
        # Mean Absolute Error (MAE)
        self.evaluations["MAE"] = utils.metrics.compute_mae(
            y=self.predictions["variable_of_interest"],
            y_pred=self.predictions["mu_estimate"],
        )
        # Mean Squared Error (MSE)
        self.evaluations["MSE"] = utils.metrics.compute_mse(
            y=self.predictions["variable_of_interest"],
            y_pred=self.predictions["mu_estimate"],
        )
        # Root Mean Squared Error (RMSE)
        self.evaluations["RMSE"] = utils.metrics.compute_rmse(
            y=self.predictions["variable_of_interest"],
            y_pred=self.predictions["mu_estimate"],
        )
        # Mean Absolute Percentage Error (MAPE)
        self.evaluations["MAPE"] = utils.metrics.compute_mape(
            y=self.predictions["variable_of_interest"],
            y_pred=self.predictions["mu_estimate"],
        )
        # R-squared
        self.evaluations["R-squared"] = utils.metrics.compute_r2(
            y=self.predictions["variable_of_interest"],
            y_pred=self.predictions["mu_estimate"],
        )
        # Explained Variance Score
        self.evaluations["Explained Variance"] = utils.metrics.compute_expv(
            y=self.predictions["variable_of_interest"],
            y_pred=self.predictions["mu_estimate"],
        )
        # Mean Standardized Log Loss (MSLL)
        self.evaluations["MSLL"] = utils.metrics.compute_msll(
            model_log_likelihoods=self.predictions["log-likelihood"],
            baseline_log_likelihoods=utils.stats.compute_censored_log_likelihood(
                self.predictions["variable_of_interest"],
                train_mean,
                train_std,
                censored_quantile=msll_censored_quantile,
            ),
        )
        _ = n_params  # keep for future use (e.g. information criteria calculations)

        return self

    def to_array(self, keys: list[str] | None = None) -> npt.NDArray[np.floating[Any]]:
        """
        Return prediction results as a list of NumPy arrays.

        Args:
            keys: list[str]
                Optional list of keys to return.
                Defaults to ["mu_estimate", "std_estimate"].

        Returns:
            list[np.ndarray]
                NumPy arrays for the requested predictions
        """
        keys = keys or ["mu_estimate", "std_estimate"]
        return np.array([self.predictions[key] for key in keys])

    def to_dataframe(
        self,
        index: pd.Index[Any] | list[Any] | None = None,
    ) -> pd.DataFrame:
        """
        Return prediction results as a DataFrame.

        Args:
            index: pd.Index | list | None
                Optional index for the DataFrame (defaults to None)

        Returns:
            pd.DataFrame
                DataFrame containing the predictions
        """
        predictions = self.predictions.copy()
        # Flatten the predictions dictionary if multiple queries are predicted
        for key in predictions:
            if predictions[key].ndim > 1:
                if predictions[key].shape[1] == 1:
                    predictions[key] = predictions[key].flatten()
                else:
                    for i in range(predictions[key].shape[1]):
                        predictions[f"{key}_{i + 1}"] = predictions[key][:, i]
                    # delete the key
                    del predictions[key]

        # Make a new DataFrame for the predictions dictionary
        return pd.DataFrame(predictions, index=index)

evaluate_predictions(variable_of_interest: npt.NDArray[np.floating[Any]], train_mean: npt.NDArray[np.floating[Any]], train_std: npt.NDArray[np.floating[Any]], n_params: int | None = None, msll_censored_quantile: float = 0.01) -> NormativePredictions

Evaluate the predictions against the observed variable of interest.

This function computes a battery of evaluation metrics implemented in snm.utils.metrics. Namely the evaluations include: - Mean Absolute Error (MAE) - Mean Squared Error (MSE) - Root Mean Squared Error (RMSE) - Mean Absolute Percentage Error (MAPE) - R-squared - Explained Variance Score - Mean Standardized Log Loss (MSLL)

Parameters:

Name Type Description Default
variable_of_interest NDArray[floating[Any]]

np.ndarray The observed values for the variable(s) of interest.

required
train_mean NDArray[floating[Any]]

np.ndarray Mean(s) of the variable(s) of interest from the training data.

required
train_std NDArray[floating[Any]]

np.ndarray Standard deviation(s) of the variable(s) of interest from the training data.

required
n_params int | None

int Number of free parameters in the model.

None
msll_censored_quantile float

float (default=0.02) Quantile below which log-likelihoods are censored for MSLL.

0.01

Returns:

Type Description
NormativePredictions

NormativePredictions Object containing the evaluation results.

Source code in src/spectranorm/snm.py
def evaluate_predictions(
    self,
    variable_of_interest: npt.NDArray[np.floating[Any]],
    train_mean: npt.NDArray[np.floating[Any]],
    train_std: npt.NDArray[np.floating[Any]],
    n_params: int | None = None,
    msll_censored_quantile: float = 0.01,
) -> NormativePredictions:
    """
    Evaluate the predictions against the observed variable of interest.

    This function computes a battery of evaluation metrics implemented
    in `snm.utils.metrics`. Namely the evaluations include:
        - Mean Absolute Error (MAE)
        - Mean Squared Error (MSE)
        - Root Mean Squared Error (RMSE)
        - Mean Absolute Percentage Error (MAPE)
        - R-squared
        - Explained Variance Score
        - Mean Standardized Log Loss (MSLL)

    Args:
        variable_of_interest: np.ndarray
            The observed values for the variable(s) of interest.
        train_mean: np.ndarray
            Mean(s) of the variable(s) of interest from the training data.
        train_std: np.ndarray
            Standard deviation(s) of the variable(s) of interest from the training
            data.
        n_params: int
            Number of free parameters in the model.
        msll_censored_quantile: float (default=0.02)
            Quantile below which log-likelihoods are censored for MSLL.

    Returns:
        NormativePredictions
            Object containing the evaluation results.
    """
    self.extend_predictions(
        variable_of_interest,
        likelihood_censoring_quantile=msll_censored_quantile,
    )
    # Mean Absolute Error (MAE)
    self.evaluations["MAE"] = utils.metrics.compute_mae(
        y=self.predictions["variable_of_interest"],
        y_pred=self.predictions["mu_estimate"],
    )
    # Mean Squared Error (MSE)
    self.evaluations["MSE"] = utils.metrics.compute_mse(
        y=self.predictions["variable_of_interest"],
        y_pred=self.predictions["mu_estimate"],
    )
    # Root Mean Squared Error (RMSE)
    self.evaluations["RMSE"] = utils.metrics.compute_rmse(
        y=self.predictions["variable_of_interest"],
        y_pred=self.predictions["mu_estimate"],
    )
    # Mean Absolute Percentage Error (MAPE)
    self.evaluations["MAPE"] = utils.metrics.compute_mape(
        y=self.predictions["variable_of_interest"],
        y_pred=self.predictions["mu_estimate"],
    )
    # R-squared
    self.evaluations["R-squared"] = utils.metrics.compute_r2(
        y=self.predictions["variable_of_interest"],
        y_pred=self.predictions["mu_estimate"],
    )
    # Explained Variance Score
    self.evaluations["Explained Variance"] = utils.metrics.compute_expv(
        y=self.predictions["variable_of_interest"],
        y_pred=self.predictions["mu_estimate"],
    )
    # Mean Standardized Log Loss (MSLL)
    self.evaluations["MSLL"] = utils.metrics.compute_msll(
        model_log_likelihoods=self.predictions["log-likelihood"],
        baseline_log_likelihoods=utils.stats.compute_censored_log_likelihood(
            self.predictions["variable_of_interest"],
            train_mean,
            train_std,
            censored_quantile=msll_censored_quantile,
        ),
    )
    _ = n_params  # keep for future use (e.g. information criteria calculations)

    return self

extend_predictions(variable_of_interest: npt.NDArray[np.floating[Any]], *, likelihood_censoring_quantile: float = 0.01) -> NormativePredictions

Extend the NormativePredictions (predictions dictionary) with additional statistics.

Parameters:

Name Type Description Default
variable_of_interest NDArray[floating[Any]]

np.ndarray The observed values for the variable(s) of interest.

required
likelihood_censoring_quantile float

float (default=0.01) Quantile below which log-likelihoods are censored for evaluation. Note: by default, a censored log-likelihood is computed to avoid extreme log-likelihood values for outliers. This affects several resulting metrics that are based on log-likelihoods (e.g. MSLL). If you want to compute the full (uncensored) log-likelihoods, set likelihood_censoring_quantile to 0.

0.01

Returns:

Type Description
NormativePredictions

NormativePredictions Extended NormativePredictions with additional statistics.

Source code in src/spectranorm/snm.py
def extend_predictions(
    self,
    variable_of_interest: npt.NDArray[np.floating[Any]],
    *,
    likelihood_censoring_quantile: float = 0.01,
) -> NormativePredictions:
    """
    Extend the NormativePredictions (predictions dictionary) with additional
    statistics.

    Args:
        variable_of_interest: np.ndarray
            The observed values for the variable(s) of interest.
        likelihood_censoring_quantile: float (default=0.01)
            Quantile below which log-likelihoods are censored for evaluation.
            Note: by default, a censored log-likelihood is computed to avoid
            extreme log-likelihood values for outliers. This affects several
            resulting metrics that are based on log-likelihoods (e.g. MSLL).
            If you want to compute the full (uncensored) log-likelihoods, set
            `likelihood_censoring_quantile` to 0.

    Returns:
        NormativePredictions
            Extended NormativePredictions with additional statistics.
    """
    self.predictions["z-score"] = (
        variable_of_interest - self.predictions["mu_estimate"]
    ) / self.predictions["std_estimate"]
    self.predictions["log-likelihood"] = (
        utils.stats.compute_censored_log_likelihood(
            variable_of_interest,
            self.predictions["mu_estimate"],
            self.predictions["std_estimate"],
            censored_quantile=likelihood_censoring_quantile,
        )
    )
    self.predictions["centiles"] = utils.stats.compute_centiles_from_z_scores(
        self.predictions["z-score"],
    )

    self.predictions["variable_of_interest"] = variable_of_interest

    return self

to_array(keys: list[str] | None = None) -> npt.NDArray[np.floating[Any]]

Return prediction results as a list of NumPy arrays.

Parameters:

Name Type Description Default
keys list[str] | None

list[str] Optional list of keys to return. Defaults to ["mu_estimate", "std_estimate"].

None

Returns:

Type Description
NDArray[floating[Any]]

list[np.ndarray] NumPy arrays for the requested predictions

Source code in src/spectranorm/snm.py
def to_array(self, keys: list[str] | None = None) -> npt.NDArray[np.floating[Any]]:
    """
    Return prediction results as a list of NumPy arrays.

    Args:
        keys: list[str]
            Optional list of keys to return.
            Defaults to ["mu_estimate", "std_estimate"].

    Returns:
        list[np.ndarray]
            NumPy arrays for the requested predictions
    """
    keys = keys or ["mu_estimate", "std_estimate"]
    return np.array([self.predictions[key] for key in keys])

to_dataframe(index: pd.Index[Any] | list[Any] | None = None) -> pd.DataFrame

Return prediction results as a DataFrame.

Parameters:

Name Type Description Default
index Index[Any] | list[Any] | None

pd.Index | list | None Optional index for the DataFrame (defaults to None)

None

Returns:

Type Description
DataFrame

pd.DataFrame DataFrame containing the predictions

Source code in src/spectranorm/snm.py
def to_dataframe(
    self,
    index: pd.Index[Any] | list[Any] | None = None,
) -> pd.DataFrame:
    """
    Return prediction results as a DataFrame.

    Args:
        index: pd.Index | list | None
            Optional index for the DataFrame (defaults to None)

    Returns:
        pd.DataFrame
            DataFrame containing the predictions
    """
    predictions = self.predictions.copy()
    # Flatten the predictions dictionary if multiple queries are predicted
    for key in predictions:
        if predictions[key].ndim > 1:
            if predictions[key].shape[1] == 1:
                predictions[key] = predictions[key].flatten()
            else:
                for i in range(predictions[key].shape[1]):
                    predictions[f"{key}_{i + 1}"] = predictions[key][:, i]
                # delete the key
                del predictions[key]

    # Make a new DataFrame for the predictions dictionary
    return pd.DataFrame(predictions, index=index)

SpectralNormativeModel dataclass

Spectral normative model implementation.

This class implements the spectral normative modeling approach, which utilizes a base direct model to generalize normative modeling to any arbitrary variable of interest reconstructed from a graph spectral embedding. It can be used to fit a normative model to high-dimensional data and predict normative centiles for arbitrary variables of interest.

Attributes:

Name Type Description
eigenmode_basis EigenmodeBasis

utils.gsp.EigenmodeBasis The eigenmode basis used for spectral normative modeling. This should be an instance of utils.gsp.EigenmodeBasis.

base_model DirectNormativeModel

DirectNormativeModel The base (direct) normative model used for spectral normative modeling.

Source code in src/spectranorm/snm.py
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
@dataclass
class SpectralNormativeModel:
    """
    Spectral normative model implementation.

    This class implements the spectral normative modeling approach, which
    utilizes a base direct model to generalize normative modeling to any
    arbitrary variable of interest reconstructed from a graph spectral
    embedding. It can be used to fit a normative model to high-dimensional
    data and predict normative centiles for arbitrary variables of interest.

    Attributes:
        eigenmode_basis: utils.gsp.EigenmodeBasis
            The eigenmode basis used for spectral normative modeling. This should be an
            instance of utils.gsp.EigenmodeBasis.
        base_model: DirectNormativeModel
            The base (direct) normative model used for spectral normative modeling.
    """

    eigenmode_basis: utils.gsp.EigenmodeBasis
    base_model: DirectNormativeModel

    @classmethod
    def build_from_dataframe(
        cls,
        eigenmode_basis: utils.gsp.EigenmodeBasis,
        model_type: ModelType,
        covariates_dataframe: pd.DataFrame,
        numerical_covariates: list[str] | None = None,
        categorical_covariates: list[str] | None = None,
        batch_covariates: list[str] | None = None,
        nonlinear_covariates: list[str] | None = None,
        influencing_mean: list[str] | None = None,
        influencing_variance: list[str] | None = None,
        spline_kwargs: dict[str, Any] | None = None,
    ) -> SpectralNormativeModel:
        """
        Initialize SNM with an eigenmode basis and a base direct model built from a
        pandas DataFrame containing all covariates.

        This uses the from_dataframe method of the DirectNormativeModel class
        to populate the direct model specification of SNM. Given that SNM does not
        require a fixed variable of interest, this method assigns a dummy name
        to the variable_of_interest parameter of the DirectNormativeModel. As such,
        the provided dataframe should not contain a column with "dummy_VOI" as name.

        Essentially, the provided dataframe should contain all covariates as columns.

        Args:
            eigenmode_basis: utils.gsp.EigenmodeBasis
                The eigenmode basis to be used for spectral normative modeling.
            model_type: ModelType
                Type of the model to create, either "HBR" (Hierarchical Bayesian
                Regression) or "BLR" (Bayesian Linear Regression).
            covariates_dataframe: pd.DataFrame
                DataFrame containing the data for all covariates and all samples.
            numerical_covariates: list[str] | None
                List of numerical covariate names.
            categorical_covariates: list[str] | None
                List of categorical covariate names.
            batch_covariates: list[str] | None
                List of batch covariate names which should also be included in
                categorical_covariates.
            nonlinear_covariates: list[str] | None
                List of covariate names to be modeled as nonlinear effects.
                These should also be included in numerical_covariates.
            influencing_mean: list[str] | None
                List of covariate names that influence the mean of the variable
                of interest. These should be included in either numerical_covariates
                or categorical_covariates.
            influencing_variance: list[str] | None
                List of covariate names that influence the variance of the variable
                of interest. These should be included in either numerical_covariates
                or categorical_covariates.
            spline_kwargs: dict
                Additional keyword arguments for spline specification, such as
                `df`, `degree`, and `knots`. These are passed to the
                `create_spline_spec` method to create spline specifications for
                nonlinear covariates.

        Returns:
            SpectralNormativeModel
                An instance of SpectralNormativeModel with base model specs initialized
                based on the provided data.
        """
        # Add a dummy variable of interest to the covariates_dataframe
        covariates_dataframe = covariates_dataframe.copy()
        covariates_dataframe["dummy_VOI"] = 0.0  # Dummy variable of interest
        # Specify the base model from the dataframe
        return cls(
            eigenmode_basis=eigenmode_basis,
            base_model=DirectNormativeModel.from_dataframe(
                model_type=model_type,
                dataframe=covariates_dataframe,
                variable_of_interest="dummy_VOI",  # Dummy variable of interest
                numerical_covariates=(numerical_covariates or []),
                categorical_covariates=(categorical_covariates or []),
                batch_covariates=(batch_covariates or []),
                nonlinear_covariates=(nonlinear_covariates or []),
                influencing_mean=(influencing_mean or []),
                influencing_variance=(influencing_variance or []),
                spline_kwargs=(spline_kwargs or {}),
            ),
        )

    def save_model(self, directory: Path) -> None:
        """
        Save the fitted spectral normative model to the specified directory.

        Args:
            directory: Path
                Directory to save the fitted model. A subdirectory named
                "spectral_normative_model" will be created within this directory.
        """
        # Prepare the save directory
        directory = Path(directory)
        saved_model_dir = utils.general.prepare_save_directory(
            directory,
            "spectral_normative_model",
        )

        # Save the eigenmode basis separately
        self.eigenmode_basis.save(str(saved_model_dir / "eigenmode_basis.joblib"))

        # Save the model
        model_dict = {
            "spec": self.base_model.spec,
            "defaults": self.base_model.defaults,
        }
        if hasattr(self, "model_params"):
            model_dict["model_params"] = self.model_params
        joblib.dump(model_dict, saved_model_dir / "spectral_model_dict.joblib")

    @classmethod
    def load_model(
        cls,
        directory: Path,
        mmap_mode: MmapMode | None = "r",
    ) -> SpectralNormativeModel:
        """
        Load a spectral normative model instance from the specified save directory.

        Args:
            directory: Path
                Directory to load the fitted model from. A subdirectory named
                "spectral_normative_model" will be searched within this directory.
            mmap_mode: MmapMode | None
                Memory mapping mode for joblib (default: "r").
                You can set this to None to disable memory-mapping.
        """
        # Validate the load directory
        directory = Path(directory)
        saved_model_dir = utils.general.validate_load_directory(
            directory,
            "spectral_normative_model",
        )

        # Check if the pickled joblib file exists in this directory
        for filename in ["spectral_model_dict.joblib", "eigenmode_basis.joblib"]:
            if not (saved_model_dir / filename).exists():
                err = f"Model Load Error: Required file '{filename}' does not exist."
                raise FileNotFoundError(err)

        # Load the pickled model dictionary
        model_dict = joblib.load(saved_model_dir / "spectral_model_dict.joblib")

        # Load the eigenmode basis
        eigenmode_basis = utils.gsp.EigenmodeBasis.load(
            str(saved_model_dir / "eigenmode_basis.joblib"),
            mmap_mode=mmap_mode,
        )

        # Create an instance of the class
        instance = cls(
            eigenmode_basis=eigenmode_basis,
            base_model=DirectNormativeModel(
                spec=model_dict["spec"],
                defaults=model_dict["defaults"],
            ),
        )

        if "model_params" in model_dict:
            instance.model_params = model_dict["model_params"]

        return instance

    def _validate_fit_input(
        self,
        spectral_coeff_train_data: npt.NDArray[np.floating[Any]],
        n_modes: int,
    ) -> None:
        """
        Internal method to validate input data for fitting the spectral normative model.
        """
        # Validate the input data
        if not isinstance(spectral_coeff_train_data, np.ndarray):
            err = "spectral_coeff_train_data must be a numpy array."
            raise TypeError(err)
        if spectral_coeff_train_data.shape[1] < n_modes:
            err = (
                f"spectral_coeff_train_data must have at least"
                f" {n_modes} columns (n_modes)."
            )
            raise ValueError(err)
        if self.eigenmode_basis.n_modes < n_modes:
            err = (
                f"Eigenmode basis has only {self.eigenmode_basis.n_modes}"
                f" modes, while {n_modes} were requested."
            )
            raise ValueError(err)

    def identify_sparse_covariance_structure(
        self,
        data: npt.NDArray[np.floating[Any]],
        sparsity_threshold: float = 1,
    ) -> npt.NDArray[np.integer[Any]]:
        """
        Identify the sparse cross-basis covariance structure in the phenotype.
        This method analyzes the phenotype's spectral coefficients to determine the
        covariance pairs that need to be modeled.

        Note: if the batches become too small, this estimate can become less stable
        in which case it is recommended to provide the sparse covariance structure
        to the model instead.

        Args:
            data: np.ndarray
                Spectral coefficients of training data representing the phenotype in
                the graph frequency domain
                :math:`(T_{train} \\Psi_{(k)}) \\in R^{N_p \\times k}`
                as a numpy array (n_samples, n_modes).
            sparsity_threshold: float
                Number of strongest correlations to keep (proportional to the number
                of modes). Defaults to 1, meaning that the number of sparse covariance
                pairs will be equal to the number of modes. If set to a lower value,
                fewer covariance pairs will be retained.

        Returns:
            np.ndarray:
                A (N, 2) array: the rows and columns of the
                identified sparse covariance structure.
        """
        # Start with correlation structure across the whole sample
        correlations = np.corrcoef(data.T)

        # Remove self-correlations
        np.fill_diagonal(correlations, 0)

        # Extract the upper triangle of the correlation matrix
        upper_triangle_indices = np.triu_indices(correlations.shape[0], k=1)

        # Determine the number of correlations to keep
        n_correlations_to_keep = int(
            sparsity_threshold * correlations.shape[0],
        )

        # Find the cutoff value for the top correlations
        if n_correlations_to_keep < len(upper_triangle_indices[0]):
            cutoff_value = np.partition(
                np.abs(correlations[upper_triangle_indices]),
                -n_correlations_to_keep,
            )[-n_correlations_to_keep]
        else:
            cutoff_value = 0
            # Warn the user if they are keeping all correlations
            logger.warning(
                "Sparsity threshold is high, keeping all correlations.",
            )

        # Now compute the sparsity structure based on the resulting matrix
        rows, cols = np.where(np.abs(correlations) > cutoff_value)

        # Remove redundant and duplicate pairs
        rows_lim = rows[rows < cols]
        cols_lim = cols[rows < cols]

        return np.array([rows_lim, cols_lim]).T

    @staticmethod
    def _is_valid_covariance_structure(
        covariance_structure: npt.NDArray[np.integer[Any]] | float,
    ) -> bool:
        """
        Verify the validity of the sparse covariance structure.
        """
        # Check it's a 2D array with two columns
        expected_ndims = 2
        expected_ncols = 2
        if not (
            isinstance(covariance_structure, np.ndarray)
            and covariance_structure.ndim == expected_ndims
            and covariance_structure.shape[1] == expected_ncols
        ):
            return False
        return np.issubdtype(covariance_structure.dtype, np.integer)

    def fit_single_direct(
        self,
        variable_of_interest: npt.NDArray[np.floating[Any]],
        covariates_dataframe: pd.DataFrame,
        *,
        save_directory: Path | None = None,
        return_model_params: bool = True,
        adapt: dict[str, Any] | None = None,
    ) -> dict[str, Any] | None:
        """
        Fit a direct normative model for a single spectral eigenmode.
        This method fits the base direct model to the provided variable of interest
        and covariates dataframe, allowing for the model to be trained on a specific
        eigenmode of the spectral embedding.

        Args:
            variable_of_interest: np.ndarray
                The loading vector capturing the variance within training data that
                corresponds to a single eigenmode.
            covariates_dataframe: pd.DataFrame
                DataFrame containing the covariates for the samples.
            save_directory: Path | None
                Directory to save the fitted model. If None, the model is not saved.
            return_model_params: bool
                If True, return the fitted model parameters.
            adapt: dict[str, Any] | None
                Adaptation parameters from a previously fitted model. If provided,
                the model will be adapted using these parameters during fitting.

        Returns:
            dict:
                If `return_model_params` is True, return the fitted model parameters
                in a dictionary.
        """
        # Prepare the data for fitting
        train_data = covariates_dataframe.copy()
        # Add the mode loading as the variable of interest
        train_data["VOI"] = variable_of_interest

        # Instantiate a direct normative model from the base model
        direct_model = DirectNormativeModel(
            spec=NormativeModelSpec(
                variable_of_interest="VOI",  # Use the added VOI column
                covariates=self.base_model.spec.covariates,
                influencing_mean=self.base_model.spec.influencing_mean,
                influencing_variance=self.base_model.spec.influencing_variance,
            ),
            defaults=self.base_model.defaults,
        )

        # Fit the model silently
        with utils.general.suppress_output():
            direct_model.fit(
                train_data=train_data,
                save_directory=save_directory,
                progress_bar=False,
                adapt=adapt,
            )

        # Return the fitted model parameters if requested
        if return_model_params:
            return direct_model.model_params

        # If not returning model parameters, return None
        return None

    def fit_single_covariance(
        self,
        variable_of_interest_1: npt.NDArray[np.floating[Any]],
        variable_of_interest_2: npt.NDArray[np.floating[Any]],
        direct_model_params_1: dict[str, Any],
        direct_model_params_2: dict[str, Any],
        covariates_dataframe: pd.DataFrame,
        *,
        save_directory: Path | None = None,
        return_model_params: bool = True,
        defaults_overwrite: dict[str, Any] | None = None,
        adapt: dict[str, Any] | None = None,
    ) -> dict[str, Any] | None:
        """
        Fit a covariance normative model between a single pair of eigenmodes.
        This method fits a covariance model to the provided pair of variables
        and covariates dataframe, considering the direct model fits for each
        eigenmode, while allowing for the cross-eigenmode covariance to vary
        normatively.

        Args:
            variable_of_interest_1: np.ndarray
                The loading vector capturing the variance within training data that
                corresponds to a single eigenmode.
            variable_of_interest_2: np.ndarray
                The loading vector capturing the variance within training data that
                corresponds to a second eigenmode.
            direct_model_params_1: dict
                The parameters of the direct model fitted to the first eigenmode.
            direct_model_params_2: dict
                The parameters of the direct model fitted to the second eigenmode.
            covariates_dataframe: pd.DataFrame
                DataFrame containing the covariates for the samples.
            save_directory: Path | None
                Directory to save the fitted model. If None, the model is not saved.
            return_model_params: bool
                If True, return the fitted model parameters.
            defaults_overwrite: dict (default={})
                Dictionary of default values to overwrite in the model fitting process.
            adapt: dict[str, Any] | None = None
                Adaptation parameters from a previously fitted model. If provided,
                the model will be adapted using these parameters during fitting.

        Returns:
            dict:
                If `return_model_params` is True, return the fitted model parameters
                in a dictionary.
        """
        # Prepare the data for fitting
        train_data = covariates_dataframe.copy()
        # Add the respective mode loadings as the variables of interest
        train_data["VOI_1"] = variable_of_interest_1
        train_data["VOI_2"] = variable_of_interest_2
        train_data[["VOI_1_mu_estimate", "VOI_1_std_estimate"]] = (
            self.base_model.predict(
                train_data,
                model_params=direct_model_params_1,
            )
            .to_array()
            .T
        )  # Add the direct model predictions
        train_data[["VOI_2_mu_estimate", "VOI_2_std_estimate"]] = (
            self.base_model.predict(
                train_data,
                model_params=direct_model_params_2,
            )
            .to_array()
            .T
        )  # Add the direct model predictions

        # Instantiate a covariance normative model from the base model
        covariance_model = CovarianceNormativeModel.from_direct_model(
            self.base_model,
            variable_of_interest_1="VOI_1",
            variable_of_interest_2="VOI_2",
            defaults_overwrite=(defaults_overwrite or {}),
        )

        # Fit the model silently
        with utils.general.suppress_output():
            covariance_model.fit(
                train_data=train_data,
                save_directory=save_directory,
                progress_bar=False,
                adapt=adapt,
            )

        # Return the fitted model parameters if requested
        if return_model_params:
            return covariance_model.model_params

        # If not returning model parameters, return None
        return None

    def fit_all_direct(
        self,
        spectral_coeff_train_data: npt.NDArray[np.floating[Any]],
        covariates_dataframe: pd.DataFrame,
        *,
        n_modes: int = -1,
        n_jobs: int = -1,
        save_directory: Path | None = None,
        save_separate: bool = False,
        adapt: dict[str, Any] | None = None,
    ) -> None:
        """
        Fit the direct models for all specified eigenmodes.

        Args:
            spectral_coeff_train_data: np.ndarray
                Spectral coefficients of training data
                :math:`(T_{train} \\Psi_{(k)}) \\in R^{N_p \\times k}`
                as a numpy array (n_samples, n_modes).
            covariates_dataframe: pd.DataFrame
                DataFrame containing the covariates for the samples.
                It must include all specified covariates in the model specification.
            n_modes: int (default=-1)
                Number of eigenmodes to fit the model to. If -1, all modes are
                used. If a positive integer, only the first n_modes are used.
                Note that the spectral_coeff_train_data and the eigenmode basis should
                have at least n_modes columns/eigenvectors.
            n_jobs: int (default=-1)
                Number of parallel jobs to use for fitting the model. If -1, all
                available CPU cores are used. If 1, no parallelization is used.
            save_directory: Path | None
                Directory to save the fitted model. If None, the model is not saved.
                A subdirectory named "spectral_normative_model" will be created
                within the specified save_directory.
            save_separate: bool (default=False)
                Whether to save the fitted direct model parameters separately for each
                eigenmode as individual files. This is only applicable if
                `save_directory` is provided.
            adapt: dict[str, Any] | None
                Adaptation parameters from a previously fitted model. If provided,
                the model will be adapted using these parameters during fitting.
        """
        # Setup the save directory if needed
        if save_directory is not None:
            save_directory = Path(save_directory)

        # Evaluate the number of modes to fit
        if n_modes == -1:
            n_modes = self.eigenmode_basis.n_modes

        # Fit the base direct model for each eigenmode using parallel processing
        tasks = (
            joblib.delayed(self.fit_single_direct)(
                variable_of_interest=spectral_coeff_train_data[:, i],
                covariates_dataframe=covariates_dataframe,
                save_directory=(
                    utils.general.ensure_dir(
                        save_directory
                        / "spectral_normative_model"
                        / "direct_models"
                        / f"mode_{i + 1}",
                    )
                    if save_directory is not None and save_separate
                    else None
                ),
                adapt=(
                    None
                    if adapt is None
                    else {
                        "covariate_to_adapt": adapt["covariate_to_adapt"],
                        "new_category_names": adapt["new_category_names"],
                        "pretrained_model_params": adapt["pretrained_model_params"][
                            "direct_model_params"
                        ][i],
                    }
                ),
            )
            for i in range(n_modes)
        )
        self.direct_model_params = list(
            utils.parallel.ParallelTqdm(
                n_jobs=n_jobs,
                total_tasks=n_modes,
                desc="Fitting direct models",
            )(tasks),  # pyright: ignore[reportCallIssue]
        )

    def identify_covariance_structure(
        self,
        spectral_coeff_train_data: npt.NDArray[np.floating[Any]],
        covariates_dataframe: pd.DataFrame,
        n_modes: int,
        covariance_structure: npt.NDArray[np.floating[Any]] | float = 0.5,
        adapt: dict[str, Any] | None = None,
    ) -> None:
        """
        Identify and set the sparse covariance structure for the spectral normative
        model based on the provided training data and covariance structure input.

        Args:
            spectral_coeff_train_data: np.ndarray
                Spectral coefficients of training data
                :math:`(T_{train} \\Psi_{(k)}) \\in R^{N_p \\times k}`
                as a numpy array (n_samples, n_modes).
            covariates_dataframe: pd.DataFrame
                DataFrame containing the covariates for the samples.
            n_modes: int
                Number of eigenmodes to consider.
            covariance_structure: np.ndarray | float
                Sparse covariance structure to use for the model fitting. If a
                (2, n_pairs) array of row and column indices are provided, the model
                will use this structure. If float, the model will estimate the
                covariance structure based on the training data and the float value
                will be used as the sparsity threshold for the number of covariance
                pairs to keep proportional to the number of modes. Defaults to 0.5,
                meaning that the number of modeled sparse covariance pairs will be
                half the number of modes.
            adapt: dict[str, Any] | None
                Adaptation parameters from a previously fitted model. If provided,
                the sparse covariance structure from the pretrained model parameters
                will be used instead of estimating a new one.
        """
        if adapt is not None:
            covariance_structure = adapt["pretrained_model_params"][
                "sparse_covariance_structure"
            ]

        # Identify sparse covariance structure if a float value is given
        if isinstance(covariance_structure, float):
            # Use trained models to compute z-scores
            spectral_train_z_scores = np.array(
                [
                    self.base_model.predict(
                        test_covariates=covariates_dataframe,
                        model_params=self.direct_model_params[x],
                    )
                    .extend_predictions(
                        variable_of_interest=spectral_coeff_train_data[:, x],
                    )
                    .predictions["z-score"]
                    for x in range(n_modes)
                ],
            ).T

            self.sparse_covariance_structure = (
                self.identify_sparse_covariance_structure(
                    spectral_train_z_scores,
                    covariance_structure,
                )
            )
        else:
            self.sparse_covariance_structure = np.array(covariance_structure)

        # Verify that the covariance structure is valid
        if not self._is_valid_covariance_structure(self.sparse_covariance_structure):
            err = "Invalid sparse covariance structure."
            raise ValueError(err)

    def fit_all_covariance(
        self,
        spectral_coeff_train_data: npt.NDArray[np.floating[Any]],
        covariates_dataframe: pd.DataFrame,
        *,
        n_jobs: int = -1,
        save_directory: Path | None = None,
        save_separate: bool = False,
        adapt: dict[str, Any] | None = None,
    ) -> None:
        """
        Fit the direct models for all specified eigenmodes.

        Args:
            spectral_coeff_train_data: np.ndarray
                Spectral coefficients of training data
                :math:`(T_{train} \\Psi_{(k)}) \\in R^{N_p \\times k}`
                as a numpy array (n_samples, n_modes).
            covariates_dataframe: pd.DataFrame
                DataFrame containing the covariates for the samples.
                It must include all specified covariates in the model specification.
            n_jobs: int (default=-1)
                Number of parallel jobs to use for fitting the model. If -1, all
                available CPU cores are used. If 1, no parallelization is used.
            save_directory: Path | None
                Directory to save the fitted model. If None, the model is not saved.
                A subdirectory named "spectral_normative_model" will be created
                within the specified save_directory.
            save_separate: bool (default=False)
                Whether to save the fitted direct model parameters separately for each
                eigenmode as individual files. This is only applicable if
                `save_directory` is provided.
            adapt: dict[str, Any] | None
                Adaptation parameters from a previously fitted model. If provided,
                the model will be adapted using these parameters during fitting.
        """
        # Setup the save directory if needed
        if save_directory is not None:
            save_directory = Path(save_directory)

        # Fit the base covariance models for selected eigenmode pairs in parallel
        tasks = (
            joblib.delayed(self.fit_single_covariance)(
                variable_of_interest_1=spectral_coeff_train_data[
                    :,
                    self.sparse_covariance_structure[i, 0],
                ],
                variable_of_interest_2=spectral_coeff_train_data[
                    :,
                    self.sparse_covariance_structure[i, 1],
                ],
                direct_model_params_1=self.direct_model_params[
                    self.sparse_covariance_structure[i, 0]
                ],
                direct_model_params_2=self.direct_model_params[
                    self.sparse_covariance_structure[i, 1]
                ],
                covariates_dataframe=covariates_dataframe,
                save_directory=(
                    utils.general.ensure_dir(
                        save_directory
                        / "spectral_normative_model"
                        / "covariance_models"
                        / (
                            f"mode_{self.sparse_covariance_structure[i, 0] + 1},"
                            f"mode_{self.sparse_covariance_structure[i, 1] + 1}"
                        ),
                    )
                    if save_directory is not None and save_separate
                    else None
                ),
                adapt=(
                    None
                    if adapt is None
                    else {
                        "covariate_to_adapt": adapt["covariate_to_adapt"],
                        "new_category_names": adapt["new_category_names"],
                        "pretrained_model_params": adapt["pretrained_model_params"][
                            "covariance_model_params"
                        ][i],
                    }
                ),
            )
            for i in range(self.sparse_covariance_structure.shape[0])
        )
        self.covariance_model_params = utils.parallel.ParallelTqdm(
            n_jobs=n_jobs,
            total_tasks=self.sparse_covariance_structure.shape[0],
            desc="Fitting covariance models",
        )(tasks)  # pyright: ignore[reportCallIssue]

    def fit(
        self,
        spectral_coeff_train_data: npt.NDArray[np.floating[Any]],
        covariates_dataframe: pd.DataFrame,
        *,
        n_modes: int = -1,
        n_jobs: int = -1,
        save_directory: Path | None = None,
        save_separate: bool = False,
        covariance_structure: npt.NDArray[np.floating[Any]] | float = 0.5,
        adapt: dict[str, Any] | None = None,
    ) -> None:
        """
        Fit the spectral normative model to the provided spectral coefficient
        training data.

        Args:
            spectral_coeff_train_data: np.ndarray
                Spectral coefficients of training data
                :math:`(T_{train} \\Psi_{(k)}) \\in R^{N_p \\times k}`
                as a numpy array (n_samples, n_modes).
            covariates_dataframe: pd.DataFrame
                DataFrame containing the covariates for the samples.
                It must include all specified covariates in the model specification.
            n_modes: int (default=-1)
                Number of eigenmodes to fit the model to. If -1, all modes are
                used. If a positive integer, only the first n_modes are used.
                Note that the spectral_coeff_train_data and the eigenmode basis should
                have at least n_modes columns/eigenvectors.
            n_jobs: int (default=-1)
                Number of parallel jobs to use for fitting the model. If -1, all
                available CPU cores are used. If 1, no parallelization is used.
            save_directory: Path | None
                Directory to save the fitted model. If None, the model is not saved.
                A subdirectory named "spectral_normative_model" will be created
                within the specified save_directory.
            save_separate: bool (default=False)
                Whether to save the fitted direct model parameters separately for each
                eigenmode as individual files. This is only applicable if
                `save_directory` is provided.
            covariance_structure: np.ndarray | float
                Sparse covariance structure to use for the model fitting. If a
                (2, n_pairs) array of row and column indices are provided, the model
                will use this structure. If float, the model will estimate the
                covariance structure based on the training data and the float value
                will be used as the sparsity threshold for the number of covariance
                pairs to keep proportional to the number of modes. Defaults to 0.5,
                meaning that the number of modeled sparse covariance pairs will be
                half the number of modes.
                Note: If using a small number of nodes, it is advisable to increase
                the sparsity threshold to ensure a stable estimation of the covariance
                structure. In contrast, when using a large number of nodes, a lower
                sparsity threshold should be used to ensure sparse modeling of the
                covariance structure.
            adapt: dict[str, Any] | None (default=None)
                If provided, adapt a pre-trained model to a new covariate.
                Note: We recommended using the `adapt_fit` method, and not directly
                changing this argument, unless you know what you are doing.
        """
        logger.info("Starting SNM model fitting:")
        # Evaluate the number of modes to fit
        if n_modes == -1:
            n_modes = self.eigenmode_basis.n_modes
        # Validate the input data
        if not isinstance(spectral_coeff_train_data, np.ndarray):
            err = "spectral_coeff_train_data must be a numpy array."
            raise TypeError(err)
        if spectral_coeff_train_data.shape[1] < n_modes:
            err = (
                f"spectral_coeff_train_data must have at least {n_modes}"
                " columns (n_modes)."
            )
            raise ValueError(err)
        if self.eigenmode_basis.n_modes < n_modes:
            err = (
                f"Eigenmode basis has only {self.eigenmode_basis.n_modes}"
                f" modes, while {n_modes} were requested."
            )
            raise ValueError(err)

        # Setup the save directory if needed
        if save_directory is not None:
            # Prepare the save directory
            save_directory = Path(save_directory)
            utils.general.prepare_save_directory(
                save_directory,
                "spectral_normative_model",
            )

        logger.info("Step 1; direct models for each eigenmode (%s modes)", n_modes)

        self.fit_all_direct(
            spectral_coeff_train_data=spectral_coeff_train_data,
            covariates_dataframe=covariates_dataframe,
            n_modes=n_modes,
            n_jobs=n_jobs,
            save_directory=save_directory,
            save_separate=save_separate,
            adapt=adapt,
        )

        logger.info("Step 2; identify sparse covariance structure")

        self.identify_covariance_structure(
            spectral_coeff_train_data=spectral_coeff_train_data,
            covariates_dataframe=covariates_dataframe,
            n_modes=n_modes,
            covariance_structure=covariance_structure,
            adapt=adapt,
        )

        # Verify that the covariance structure is valid
        if not self._is_valid_covariance_structure(self.sparse_covariance_structure):
            err = "Invalid sparse covariance structure."
            raise ValueError(err)

        # Model cross basis sparse covariance structure
        logger.info(
            "Step 3; cross-eigenmode dependency modeling (%s pairs)",
            self.sparse_covariance_structure.shape[0],
        )

        self.fit_all_covariance(
            spectral_coeff_train_data=spectral_coeff_train_data,
            covariates_dataframe=covariates_dataframe,
            n_jobs=n_jobs,
            save_directory=save_directory,
            save_separate=save_separate,
            adapt=adapt,
        )

        # Save SNM model parameters
        sample_size = spectral_coeff_train_data.shape[0]
        if adapt is not None:
            sample_size += adapt["pretrained_model_params"]["sample_size"]
        self.model_params = {
            "n_modes": n_modes,
            "sample_size": sample_size,
            "direct_model_params": self.direct_model_params,
            "sparse_covariance_structure": self.sparse_covariance_structure,
            "covariance_model_params": self.covariance_model_params,
        }
        if (self.direct_model_params[0] is not None) and (
            "n_params" in self.direct_model_params[0]
        ):
            self.model_params["n_params"] = self.direct_model_params[0]["n_params"]
        else:
            err = "Direct model parameters are not valid."
            raise ValueError(err)

        # Save the model if a save path is provided
        if save_directory is not None:
            self.save_model(save_directory)

    def adapt_fit(
        self,
        covariate_to_adapt: str,
        new_category_names: npt.NDArray[np.str_],
        spectral_coeff_train_data: npt.NDArray[np.floating[Any]],
        covariates_dataframe: pd.DataFrame,
        *,
        pretrained_model_params: dict[str, Any] | None = None,
        n_jobs: int = -1,
        save_directory: Path | None = None,
        save_separate: bool = False,
    ) -> None:
        """
        Using a previously fitted spectral normative model, adapt to a new
        batch.
        This method enables adaptation (fine-tuning) of the model to data
        from a new batch/site by freezing all fitted parameters, and only
        estimating new parameters for the new batch/site category.

        Args:
            covariate_to_adapt: str
                Name of the categorical covariate representing the batch/site
                to which the model should be adapted.
                Note: This covariate must have been specified in the original
                model.
            new_category_names: list[str]
                Names of the new categories in the covariate_to_adapt representing
                the new batch/site labels (e.g. names of the new site).
                Note: These names must not have been present in the original
                fitted model.
            spectral_coeff_train_data: np.ndarray
                Spectral coefficients of training data
                :math:`(T_{train} \\Psi_{(k)}) \\in R^{N_p \\times k}`
                as a numpy array (n_samples, n_modes).
            covariates_dataframe: pd.DataFrame
                DataFrame containing the covariates for the samples.
                It must include all specified covariates in the model specification.
                Note: The covariate_to_adapt column must only contain the
                new_category_names (no new data from previously trained batches).
            pretrained_model_params: dict[str, Any] | None
                The model parameters from a previously fitted model to adapt.
                If None, the model parameters from the current instance will be used
                (assuming fitting was done).
            n_jobs: int (default=-1)
                Number of parallel jobs to use for fitting the model. If -1, all
                available CPU cores are used. If 1, no parallelization is used.
            save_directory: Path | None
                A path to a directory to save the adapted model. If provided,
                the fitted model will be saved to this path.
            save_separate: bool (default=False)
                Whether to save the fitted direct model parameters separately for each
                eigenmode as individual files. This is only applicable if
                `save_directory` is provided.
        """
        # Locate the covariate to adapt
        cov_to_adapt_index = [
            cov.name for cov in self.base_model.spec.covariates
        ].index(covariate_to_adapt)

        # Extend the covariate categories to include the new categories
        self.base_model.spec.covariates[cov_to_adapt_index].extend_categories(
            new_category_names,
        )

        # Extract the pre-trained model parameters
        if pretrained_model_params is None:
            if not hasattr(self, "model_params") or self.model_params is None:
                err = (
                    "No pretrained model parameters found. "
                    "Please provide pretrained_model_params or fit the model first."
                )
                raise ValueError(err)
            pretrained_model_params = copy.deepcopy(self.model_params)

        # Fit the adapted model
        self.fit(
            spectral_coeff_train_data,
            covariates_dataframe,
            n_modes=pretrained_model_params["n_modes"],
            n_jobs=n_jobs,
            save_directory=save_directory,
            save_separate=save_separate,
            covariance_structure=pretrained_model_params["sparse_covariance_structure"],
            adapt={
                "covariate_to_adapt": covariate_to_adapt,
                "new_category_names": new_category_names,
                "pretrained_model_params": pretrained_model_params,
            },
        )

    def _predict_from_spectral_estimates(
        self,
        encoded_query: npt.NDArray[np.floating[Any]],
        eigenmode_mu_estimates: npt.NDArray[np.floating[Any]],
        eigenmode_std_estimates: npt.NDArray[np.floating[Any]],
        rho_estimates: npt.NDArray[np.floating[Any]],
        model_params: dict[str, Any],
        n_modes: int,
    ) -> NormativePredictions:
        """
        Internal method to predict only the mean and sigma for new data using the fitted
        spectral moments.
        """
        # Constrain mu and std estimates to the number of modes
        eigenmode_mu_estimates = eigenmode_mu_estimates[:, :n_modes]
        eigenmode_std_estimates = eigenmode_std_estimates[:, :n_modes]

        # Prepare the predictions
        predictions_dict = {}
        predictions_dict["mu_estimate"] = eigenmode_mu_estimates @ encoded_query

        # Load sparse covariance structure
        row_indices = model_params["sparse_covariance_structure"][:, 0]
        col_indices = model_params["sparse_covariance_structure"][:, 1]

        # Select indices that are both within n_modes
        corr_index_valid = (row_indices < n_modes) & (col_indices < n_modes)

        # Mask valid values
        valid_rho_estimates = rho_estimates[:, corr_index_valid]
        valid_row_indices = row_indices[corr_index_valid]
        valid_col_indices = col_indices[corr_index_valid]

        # number of dimensions
        n_samples = eigenmode_mu_estimates.shape[0]
        n_queries = encoded_query.shape[1]

        # Pre-compute squared encoded query
        # to have shape of (n_modes, n_queries)
        # Avoids recomputing for every sample
        encoded_query_squared = encoded_query**2

        # Empty matrix to fill in loop
        sample_query_stds = np.empty((n_samples, n_queries), dtype=rho_estimates.dtype)

        # Implement the equivalent of sparse matrix multiplications methodically
        # to avoid large memory consumption and optimize speed
        # sparse multiplication requires (n_samples, n_modes, n_queries) memory
        for sample_idx in range(n_samples):
            # Build weighted mode stds on the fly — small memory (n_modes, n_queries)
            weighted_mode_stds_sample = (
                eigenmode_std_estimates[sample_idx, :, None] * encoded_query
            )

            # diagonal term (within mode variances)
            diagonal_term = (
                eigenmode_std_estimates[sample_idx] ** 2
            ) @ encoded_query_squared
            # off-diagonal term (cross-mode covariances)
            # weighted stds for valid pairs (n_edges, n_queries)
            weighted_mode_stds_sample_row = weighted_mode_stds_sample[valid_row_indices]
            weighted_mode_stds_sample_col = weighted_mode_stds_sample[valid_col_indices]
            off_diagonal_term = 2 * (
                valid_rho_estimates[sample_idx, :, None]
                * (weighted_mode_stds_sample_row * weighted_mode_stds_sample_col)
            ).sum(axis=0)
            # avoid negative values due to numerical issues
            # if it happens, ignore off-diagonal term
            try:
                sample_query_stds[sample_idx] = np.sqrt(
                    diagonal_term + off_diagonal_term,
                )
            except RuntimeWarning:
                sample_query_stds[sample_idx] = np.sqrt(diagonal_term)

        predictions_dict["std_estimate"] = sample_query_stds

        # Create a the predictions object
        return NormativePredictions(predictions=predictions_dict)

    @staticmethod
    def _predict_single_mode_estimates(
        direct_model_spec: NormativeModelSpec,
        direct_model_defaults: dict[str, Any],
        test_covariates: pd.DataFrame,
        model_params: dict[str, Any],
        predict_without: list[str] | None = None,
    ) -> npt.NDArray[np.floating[Any]]:
        """
        Internal method to predict single mode estimates for new data using the fitted
        spectral normative model.
        """
        # Instantiate a direct normative model from the base model
        direct_model = DirectNormativeModel(
            spec=NormativeModelSpec(
                variable_of_interest="VOI",  # Use the added VOI column
                covariates=direct_model_spec.covariates,
                influencing_mean=direct_model_spec.influencing_mean,
                influencing_variance=direct_model_spec.influencing_variance,
            ),
            defaults=direct_model_defaults,
        )
        return (
            direct_model.predict(
                test_covariates,
                model_params=model_params,
                predict_without=predict_without,
            )
            .to_array(["mu_estimate", "std_estimate"])
            .T
        )

    def _predict_all_mode_estimates(
        self,
        test_covariates: pd.DataFrame,
        model_params: dict[str, Any],
        n_modes: int,
        n_jobs: int = -1,
        predict_without: list[str] | None = None,
    ) -> tuple[
        npt.NDArray[np.floating[Any]],
        npt.NDArray[np.floating[Any]],
    ]:
        """
        Internal method to predict all direct estimates for new data using the fitted
        spectral normative model.
        """
        # direct normative predictions for each eigenmode
        tasks = (
            joblib.delayed(self._predict_single_mode_estimates)(
                self.base_model.spec,
                self.base_model.defaults,
                test_covariates,
                model_params=direct_model_params,
                predict_without=predict_without,
            )
            for direct_model_params in model_params["direct_model_params"][:n_modes]
        )

        results = list(
            utils.parallel.ParallelTqdm(
                n_jobs=n_jobs,
                total_tasks=n_modes,
                desc="Computing direct eigenmode estimates",
            )(tasks),  # pyright: ignore[reportCallIssue]
        )

        # Unpack results, estimates have a shape of (n_samples, n_modes)
        eigenmode_mu_estimates, eigenmode_std_estimates = np.array(results).T

        return eigenmode_mu_estimates, eigenmode_std_estimates

    @staticmethod
    def _predict_single_covariance_estimates(
        covariance_model_spec: CovarianceModelSpec,
        covariance_model_defaults: dict[str, Any],
        test_covariates: pd.DataFrame,
        model_params: dict[str, Any],
        predict_without: list[str] | None = None,
    ) -> npt.NDArray[np.floating[Any]]:
        """
        Internal method to predict single covariance estimates for new data using the
        fitted spectral normative model.
        """
        # create a dummy covariance model
        covariance_model = CovarianceNormativeModel(
            spec=CovarianceModelSpec(
                variable_of_interest_1="VOI_1",
                variable_of_interest_2="VOI_2",
                covariates=covariance_model_spec.covariates,
                influencing_covariance=covariance_model_spec.influencing_covariance,
            ),
            defaults=covariance_model_defaults,
        )
        return (
            covariance_model.predict(
                test_covariates,
                model_params=model_params,
                predict_without=predict_without,
            )
            .to_array(["correlation_estimate"])
            .T
        )

    def _predict_all_covariance_estimates(
        self,
        test_covariates: pd.DataFrame,
        model_params: dict[str, Any],
        n_modes: int,
        n_jobs: int = -1,
        predict_without: list[str] | None = None,
    ) -> npt.NDArray[np.floating[Any]]:
        """
        Internal method to predict all covariance estimates for new data using the
        fitted spectral normative model.
        """
        # create a dummy covariance model
        covariance_model = CovarianceNormativeModel.from_direct_model(
            self.base_model,
            variable_of_interest_1="dummy_VOI_1",  # Dummy variable of interest
            variable_of_interest_2="dummy_VOI_2",  # Dummy variable of interest
        )

        # Check sparse covariance structure
        row_indices = model_params["sparse_covariance_structure"][:, 0]
        col_indices = model_params["sparse_covariance_structure"][:, 1]
        # Select indices that are within n_modes
        corr_index_valid = (row_indices < n_modes) & (col_indices < n_modes)

        # cross-mode dependence structure for valid pairs
        tasks = (
            joblib.delayed(self._predict_single_covariance_estimates)(
                covariance_model.spec,
                covariance_model.defaults,
                test_covariates,
                model_params=covariance_model_params,
                predict_without=predict_without,
            )
            for i, covariance_model_params in enumerate(
                model_params["covariance_model_params"],
            )
            if corr_index_valid[i]
        )

        results = list(
            utils.parallel.ParallelTqdm(
                n_jobs=n_jobs,
                total_tasks=np.sum(corr_index_valid),
                desc="Computing cross-mode dependence estimates",
            )(tasks),  # pyright: ignore[reportCallIssue]
        )

        # Unpack results, (n_samples, n_valid_covariance_pairs)
        valid_rho_estimates = np.array(results).T[0]

        # Now fill in the full set of rho estimates with NaNs for the invalid pairs
        rho_estimates = np.full(
            (
                test_covariates.shape[0],
                model_params["sparse_covariance_structure"].shape[0],
            ),
            np.nan,
        )
        rho_estimates[:, corr_index_valid] = valid_rho_estimates
        # final estimates have a shape of (n_samples, n_covariance_pairs)

        return rho_estimates

    def compute_spectral_predictions(
        self,
        test_covariates: pd.DataFrame,
        *,
        model_params: dict[str, Any] | None = None,
        n_modes: int | None = None,
        n_jobs: int = -1,
        predict_without: list[str] | None = None,
    ) -> dict[str, npt.NDArray[np.floating[Any]]]:
        """
        Predict normative moments (mean, std) of the eigenmode basis for new data
        using the fitted spectral normative model.

        This function requires a dataframe of covariates (test_covariates) to compute
        a set of spectral predictions that can subsequently be combined to efficiently
        estimate normative predictions for any query(ies).

        Args:
            test_covariates: pd.DataFrame
                DataFrame containing the new covariate data to predict.
                This must include all specified covariates.
                Note: covariates listed in predict_without will be ignored and are
                hence not required.
            model_params: dict | None
                Optional dictionary of model parameters to use. If not provided,
                the stored parameters from model.fit() will be used.
            n_modes: int | None
                Optional number of modes to use for the prediction. If not provided,
                the number of modes from model_params will be used.
            n_jobs: int (default=-1)
                Number of parallel jobs to utilize. If -1, all available CPU cores are
                used. If 1, no parallelization is used.
            predict_without: list[str] | None
                Optional list of covariate names to ignore during prediction.
                This can be used to check the effect of removing certain covariates
                from the model.

        Returns:
            dict:
                A dictionary containing:
                - 'eigenmode_mu_estimates': np.ndarray (n_samples, n_modes)
                - 'eigenmode_std_estimates': np.ndarray (n_samples, n_modes)
                - 'rho_estimates': np.ndarray (n_samples, n_covariance_pairs)
        """
        # Parameters
        if model_params is None:
            model_params = self.model_params

        # Find n_modes
        if n_modes is None:
            n_modes = int(model_params["n_modes"])

        if self.base_model.spec is None:
            err = "The base model is not specified. Cannot predict new data."
            raise ValueError(err)

        # Validate the covariate data
        validation_columns = [
            cov.name
            for cov in self.base_model.spec.covariates
            if cov.name not in (predict_without or [])
        ]
        utils.general.validate_dataframe(test_covariates, validation_columns)

        # direct normative predictions for each eigenmode
        (
            eigenmode_mu_estimates,
            eigenmode_std_estimates,
        ) = self._predict_all_mode_estimates(
            test_covariates,
            model_params,
            n_modes,
            n_jobs=n_jobs,
            predict_without=predict_without,
        )  # estimates have a shape of (n_samples, n_modes)

        # cross-mode dependence structure
        rho_estimates = self._predict_all_covariance_estimates(
            test_covariates,
            model_params,
            n_modes,
            n_jobs=n_jobs,
            predict_without=predict_without,
        )  # estimates have a shape of (n_samples, n_covariance_pairs)

        return {
            "eigenmode_mu_estimates": eigenmode_mu_estimates,
            "eigenmode_std_estimates": eigenmode_std_estimates,
            "rho_estimates": rho_estimates,
        }

    def _validate_spectral_predictions(
        self,
        spectral_predictions: dict[str, npt.NDArray[np.floating[Any]]],
    ) -> None:
        """
        Internal method to validate the spectral predictions dictionary.
        """
        required_keys = [
            "eigenmode_mu_estimates",
            "eigenmode_std_estimates",
            "rho_estimates",
        ]
        if not all(key in spectral_predictions for key in required_keys):
            err = (
                "spectral_predictions must contain 'eigenmode_mu_estimates',"
                " 'eigenmode_std_estimates', and 'rho_estimates'."
            )
            raise ValueError(err)

    def predict(
        self,
        encoded_query: npt.NDArray[np.floating[Any]],
        *,
        spectral_predictions: dict[str, npt.NDArray[np.floating[Any]]] | None = None,
        test_covariates: pd.DataFrame | None = None,
        extended: bool = False,
        model_params: dict[str, Any] | None = None,
        spectral_coeff_test_data: npt.NDArray[np.floating[Any]] | None = None,
        n_modes: int | None = None,
        predict_without: list[str] | None = None,
    ) -> NormativePredictions:
        """
        Predict normative moments (mean, std) for new data using the fitted spectral
        normative model.
        Spectral normative modeling can estimate the normative distribution of any
        variable of interest defined as a spatial query encoded in the latent low-pass
        graph spectral space.

        As such, the predict method requires:
            - The encoded query(ies) defining the variable(s) of interest.

        In addition, the method requires either:
            - A dataframe of covariates (test_covariates) to be used for inference
              of a set of spectral predictions that will subsequently be combined
              to yield the normative predictions for the encoded query(ies).
            OR
            - A dictionary of precomputed spectral predictions (spectral_predictions)
              to be used for efficiently predicting the encoded query(ies).

        The precomputed spectral predictions can be obtained using the
        'compute_spectral_predictions' function. This is particularly useful when
        predicting multiple queries or when the same covariate set is used for
        multiple predictions, as it avoids redundant computations.

        Args:
            encoded_query: np.ndarray
                Encoded query data defining the normative variable of interest.
                Can be provided as:
                - shape = (n_modes) for a single query vector
                - shape = (n_modes, n_queries) for multiple queries predicted at once
            spectral_predictions: dict | None
                Optional dictionary of precomputed spectral predictions to use for
                the prediction. If not provided, test_covariates must be provided
                instead to compute the spectral predictions.
                The dictionary should contain:
                - 'eigenmode_mu_estimates': np.ndarray (n_samples, n_modes)
                - 'eigenmode_std_estimates': np.ndarray (n_samples, n_modes)
                - 'rho_estimates': np.ndarray (n_samples, n_covariance_pairs)
                This can be obtained using the 'compute_spectral_predictions' method.
            test_covariates: pd.DataFrame | None
                DataFrame containing the new covariate data to predict.
                This must include all specified covariates.
                Note: covariates listed in predict_without will be ignored and are
                hence not required.
            extended: bool (default: False)
                If True, return additional stats such as log-likelihood, centiles, etc.
                Note that extended predictions require spectral_coeff_test_data to be
                provided in addition to the covariates.
            model_params: dict | None
                Optional dictionary of model parameters to use. If not provided,
                the stored parameters from model.fit() will be used.
            spectral_coeff_test_data: np.ndarray | None
                Optional spectral coefficient of test data for the phenotype being
                modeled :math:`(T_{test} \\Psi_{(k)}) \\in R^{N_{test} \\times k}`
                (only required for extended predictions).
                Expects a numpy array (n_samples, n_modes)
            n_modes: int | None
                Optional number of modes to use for the prediction. If not provided,
                the number of modes from model_params will be used.
            predict_without: list[str] | None
                Optional list of covariate names to ignore during prediction.
                This can be used to check the effect of removing certain covariates
                from the model.

        Returns:
            pd.DataFrame: DataFrame containing the predicted moments (mean, std) for
                the variable of interest defined by the encoded query.
        """
        # Parameters
        if model_params is None:
            model_params = self.model_params

        # Find n_modes
        if n_modes is None:
            n_modes = int(model_params["n_modes"])

        if self.base_model.spec is None:
            err = "The base model is not specified. Cannot predict new data."
            raise ValueError(err)

        if spectral_predictions is None:
            if test_covariates is None:
                err = "Either test_covariates or spectral_predictions must be provided."
                raise ValueError(err)

            # Compute spectral predictions if not provided
            spectral_predictions = self.compute_spectral_predictions(
                test_covariates=test_covariates,
                model_params=model_params,
                n_modes=n_modes,
                predict_without=predict_without,
            )
        elif test_covariates is not None:
            logger.warning(
                "Both test_covariates and spectral_predictions are provided."
                " Ignoring test_covariates and using spectral_predictions.",
            )
            if predict_without is not None:
                logger.warning(
                    "predict_without is ignored when spectral_predictions"
                    " are provided directly.",
                )

        # Unpack spectral predictions
        self._validate_spectral_predictions(spectral_predictions)
        eigenmode_mu_estimates = spectral_predictions["eigenmode_mu_estimates"]
        eigenmode_std_estimates = spectral_predictions["eigenmode_std_estimates"]
        rho_estimates = spectral_predictions["rho_estimates"]

        # Reformat encoded queries (for efficiency)
        encoded_query = np.asarray(encoded_query[:n_modes])
        encoded_query = encoded_query.reshape(n_modes, -1, order="F")

        # Compute the predictions
        predictions = self._predict_from_spectral_estimates(
            encoded_query=encoded_query,
            eigenmode_mu_estimates=eigenmode_mu_estimates,
            eigenmode_std_estimates=eigenmode_std_estimates,
            rho_estimates=rho_estimates,
            model_params=model_params,
            n_modes=n_modes,
        )

        # Check if extended predictions are requested
        if extended:
            if spectral_coeff_test_data is None:
                err = (
                    "Extended predictions require spectral_coeff_test_data"
                    " to be provided."
                )
                raise ValueError(err)
            # Add extended statistics to predictions (e.g. centiles, log-loss, etc.)
            predictions.extend_predictions(
                variable_of_interest=spectral_coeff_test_data @ encoded_query,
            )

        return predictions

    def evaluate(
        self,
        encoded_query: npt.NDArray[np.floating[Any]],
        spectral_coeff_test_data: npt.NDArray[np.floating[Any]],
        *,
        spectral_predictions: dict[str, npt.NDArray[np.floating[Any]]] | None = None,
        test_covariates: pd.DataFrame | None = None,
        query_train_moments: npt.NDArray[np.floating[Any]] | None = None,
        model_params: dict[str, Any] | None = None,
        n_modes: int | None = None,
    ) -> NormativePredictions:
        """
        Evaluate the model on new data and return predictions along with evaluation
        metrics.

        Args:
            encoded_query: np.ndarray
                Encoded query data defining the normative variable of interest.
                Can be provided as:
                - shape = (n_modes) for a single query vector
                - shape = (n_modes, n_queries) for multiple queries predicted at once
            spectral_coeff_test_data: np.ndarray | None
                Spectral coefficient of test data for the phenotype being modeled
                :math:`(T_{test} \\Psi_{(k)}) \\in R^{N_{test} \\times k}`
                (only required for extended predictions).
                Expects a numpy array (n_samples, n_modes)
            spectral_predictions: dict | None
                Optional dictionary of precomputed spectral predictions to use for
                the evaluation. If not provided, test_covariates must be provided
                instead to compute the spectral predictions.
                The dictionary should contain:
                - 'eigenmode_mu_estimates': np.ndarray (n_samples, n_modes)
                - 'eigenmode_std_estimates': np.ndarray (n_samples, n_modes)
                - 'rho_estimates': np.ndarray (n_samples, n_covariance_pairs)
                This can be obtained using the 'compute_spectral_predictions' method.
            test_covariates: pd.DataFrame | None
                DataFrame containing the new covariate data to predict.
                This must include all specified covariates.
                Note: This is only required if spectral_predictions was not provided.
            query_train_moments: np.ndarray | None
                A (2, n_queries) array containing the query moments (mean, std) directly
                measured in the training data. While optional, providing these moments
                is strongly recommended for accurate evaluation of the model's MSLL.
                If not provided, the model will use the test data moments as an
                approximation, which may lead to overestimating MSLL. This is made
                optional to allow evaluating MSLL when the training data is not
                accessible (e.g. using a pre-trained model).
            model_params: dict | None
                Optional dictionary of model parameters to use. If not provided,
                the stored parameters from model.fit() will be used.
            n_modes: int | None
                Optional number of modes to use for the prediction. If not provided,
                the stored number of modes from model.fit() will be used.

        Returns:
            NormativePredictions:
                Object containing the predicted moments (mean, std) for
                the variable of interest defined by the encoded query, along with
                evaluation metrics.
        """
        # Find n_modes
        if n_modes is None:
            n_modes = int(self.model_params["n_modes"])

        # Parameters
        if model_params is None:
            model_params = self.model_params

        # Reformat encoded queries (for efficiency)
        encoded_query = np.asarray(encoded_query[:n_modes])
        encoded_query = encoded_query.reshape(n_modes, -1, order="F")

        # Run extended predictions
        predictions = self.predict(
            encoded_query=encoded_query,
            spectral_predictions=spectral_predictions,
            test_covariates=test_covariates,
            extended=True,
            model_params=model_params,
            spectral_coeff_test_data=spectral_coeff_test_data,
            n_modes=n_modes,
        )
        if query_train_moments is None:
            logger.warning(
                "Query moments not provided. Using test data moments as an"
                " approximation, which may lead to overestimating MSLL.",
            )
            query_train_moments = np.array(
                [
                    np.mean(spectral_coeff_test_data @ encoded_query, axis=0),
                    np.std(spectral_coeff_test_data @ encoded_query, axis=0, ddof=1),
                ],
            )
        return predictions.evaluate_predictions(
            variable_of_interest=spectral_coeff_test_data @ encoded_query,
            train_mean=query_train_moments[0],
            train_std=query_train_moments[1],
            n_params=model_params["n_params"],
        )

    def harmonize(
        self,
        encoded_query: npt.NDArray[np.floating[Any]],
        spectral_coeff_data: npt.NDArray[np.floating[Any]],
        *,
        covariates_to_harmonize: list[str] | None = None,
        covariates_dataframe: pd.DataFrame | None = None,
        spectral_predictions_full: dict[str, npt.NDArray[np.floating[Any]]]
        | None = None,
        spectral_predictions_partial: dict[str, npt.NDArray[np.floating[Any]]]
        | None = None,
        model_params: dict[str, Any] | None = None,
        n_modes: int | None = None,
    ) -> npt.NDArray[np.floating[Any]]:
        """
        Harmonize the variables of interest in the data to remove effects of
        certain covariates (e.g. batch). This method uses the spectral normative model
        to harmonize one or several variables of interest defined by the encoded query.

        The harmonization method can be used in two ways:
        - By providing a dataframe of covariates (covariates_dataframe) to compute the
          necessary spectral predictions for both the full model (all covariates)
          and the partial model (excluding covariates to harmonize). In this format,
          you should also provide the covariates_to_harmonize (list of covariate names).
        - By providing precomputed spectral predictions for both the full and partial
          models (spectral_predictions_full and spectral_predictions_partial). In this
          format, the partial spectral predictions should have been computed by
          excluding the covariates to harmonize using the predict_without parameter in
          the compute_spectral_predictions method.
          Note: In the latter, the method will not use the covariates_to_harmonize list.

        Args:
            encoded_query: np.ndarray
                Encoded query data defining the normative variable of interest.
                Can be provided as:
                - shape = (n_modes) for a single query vector
                - shape = (n_modes, n_queries) for multiple queries predicted at once
            spectral_coeff_data: np.ndarray | None
                Spectral coefficient of the the phenotype being modeled
                :math:`(T \\Psi_{(k)}) \\in R^{N_{p} \\times k}`.
                Expects a numpy array (n_samples, n_modes)
            covariates_to_harmonize: list[str] | None
                List of covariate names to harmonize.
                The partial effects of these covariates will be removed from the
                variable of interest, and the harmonized values will be returned.
                Note: This is only required if spectral_predictions_full and
                spectral_predictions_partial were not provided.
            covariates_dataframe: pd.DataFrame | None
                DataFrame containing covariate information for the data to harmonize.
                This must include all specified covariates. The dataframe is expected
                to have all covariates as columns and samples as rows.
                Note: This is only required if spectral_predictions_full and
                spectral_predictions_partial were not provided. Alternatively,
                if any of the aforementioned spectral predictions were previously
                computed, then they could be passed to this method to avoid
                recomputation.
            spectral_predictions_full: dict | None
                Optional dictionary of precomputed spectral predictions to use for
                the harmonization. If not provided, covariates_dataframe must be
                provided instead to compute the spectral predictions.
                These predictions use all set of covariates.
                The dictionary should contain:
                - 'eigenmode_mu_estimates': np.ndarray (n_samples, n_modes)
                - 'eigenmode_std_estimates': np.ndarray (n_samples, n_modes)
                - 'rho_estimates': np.ndarray (n_samples, n_covariance_pairs)
                This can be obtained using the 'compute_spectral_predictions' method.
            spectral_predictions_partial: dict | None
                Optional dictionary of precomputed spectral predictions to use for
                the harmonization. If not provided, covariates_dataframe must be
                provided instead to compute the partial spectral predictions.
                These predictions use all set of covariates except those to harmonize.
                The covariates to harmonize need to be partialed out using the
                predict_without parameter.
                The dictionary should contain:
                - 'eigenmode_mu_estimates': np.ndarray (n_samples, n_modes)
                - 'eigenmode_std_estimates': np.ndarray (n_samples, n_modes)
                - 'rho_estimates': np.ndarray (n_samples, n_covariance_pairs)
                This can be obtained using the 'compute_spectral_predictions' method.
            model_params: dict | None
                Optional dictionary of model parameters to use. If not provided,
                the stored parameters from model.fit() will be used.
            n_modes: int | None
                Optional number of modes to use for the prediction. If not provided,
                the stored number of modes from model.fit() will be used.

        Returns:
            npt.NDArray[np.floating[Any]]: Array of harmonized values for the
                variable of interest.
        """
        if (spectral_predictions_full is None) or (
            spectral_predictions_partial is None
        ):
            if (covariates_dataframe is None) or (covariates_to_harmonize is None):
                err = (
                    "Either [covariates_dataframe and covariates_to_harmonize] or "
                    "both spectral_predictions_full "
                    "and spectral_predictions_partial must be provided."
                )
                raise ValueError(err)
            # Validate the new data
            validation_columns = [cov.name for cov in self.base_model.spec.covariates]
            utils.general.validate_dataframe(covariates_dataframe, validation_columns)

        # Find n_modes
        if n_modes is None:
            n_modes = int(self.model_params["n_modes"])

        # Parameters
        if model_params is None:
            model_params = self.model_params

        # Reformat encoded queries (for efficiency)
        encoded_query = np.asarray(encoded_query[:n_modes])
        encoded_query = encoded_query.reshape(n_modes, -1, order="F")

        # Predict the mean and std with all covariates
        full_predictions = self.predict(
            encoded_query=encoded_query,
            spectral_predictions=spectral_predictions_full,
            test_covariates=covariates_dataframe,
            model_params=model_params,
            n_modes=n_modes,
            predict_without=[],
        )

        # Predict the mean and std without the covariates to harmonize
        reduced_predictions = self.predict(
            encoded_query=encoded_query,
            spectral_predictions=spectral_predictions_partial,
            test_covariates=covariates_dataframe,
            model_params=model_params,
            n_modes=n_modes,
            predict_without=covariates_to_harmonize,
        )

        # Reconstruct observed phenotype for query from spectral coefficients
        observed_phenotype = spectral_coeff_data @ encoded_query[:n_modes]

        # First standardize the variable of interest based on the full model
        vois_standardized = (
            observed_phenotype - full_predictions.predictions["mu_estimate"]
        ) / full_predictions.predictions["std_estimate"]

        # Then return the harmonized values based on the reduced model
        return np.asarray(
            (
                vois_standardized * reduced_predictions.predictions["std_estimate"]
                + reduced_predictions.predictions["mu_estimate"]
            ),
            dtype=np.float64,
        )

    def reduce_model(
        self,
        n_modes: int,
        *,
        inplace: bool = False,
    ) -> SpectralNormativeModel:
        """
        Create a reduced spectral normative model using only the first n_modes.

        Args:
            n_modes: int
                Number of modes to retain in the reduced model. Must be less than or
                equal to the current number of modes considered by the model.
            inplace: bool (default: False)
                If True, modify the current model instance to reduce its modes. If
                False, return a new SpectralNormativeModel instance with the reduced
                modes.
        Returns:
            SpectralNormativeModel
                A new SpectralNormativeModel instance with reduced number of modes.
        """
        if (n_modes > self.eigenmode_basis.n_modes) or (
            hasattr(self, "model_params") and n_modes > self.model_params["n_modes"]
        ):
            available_modes = self.eigenmode_basis.n_modes
            if hasattr(self, "model_params"):
                available_modes = min(available_modes, self.model_params["n_modes"])
            err = f"Cannot reduce to {n_modes} modes, only {available_modes} available."
            raise ValueError(err)

        # Create a reduced eigenbasis
        reduced_eigenbasis = self.eigenmode_basis.reduce(n_modes)

        if inplace:
            return_model = self
            return_model.eigenmode_basis = reduced_eigenbasis
        else:
            return_model = SpectralNormativeModel(
                base_model=self.base_model,
                eigenmode_basis=reduced_eigenbasis,
            )
            return_model.model_params = self.model_params  # Copy model parameters

        # Update model parameters to reflect reduced modes
        if hasattr(self, "model_params"):
            new_model_params: dict[str, Any] = {}
            new_model_params["n_modes"] = n_modes
            new_model_params["sample_size"] = self.model_params["sample_size"]
            new_model_params["direct_model_params"] = self.model_params[
                "direct_model_params"
            ][:n_modes]
            valid_cov_indices = np.where(
                (
                    return_model.model_params["sparse_covariance_structure"][:, 0]
                    < n_modes
                )
                & (
                    return_model.model_params["sparse_covariance_structure"][:, 1]
                    < n_modes
                ),
            )[0]
            new_model_params["sparse_covariance_structure"] = return_model.model_params[
                "sparse_covariance_structure"
            ][valid_cov_indices]
            new_model_params["covariance_model_params"] = [
                return_model.model_params["covariance_model_params"][i]
                for i in valid_cov_indices
            ]
            new_model_params["n_params"] = self.model_params.get("n_params", None)
            return_model.model_params = new_model_params

        return return_model

adapt_fit(covariate_to_adapt: str, new_category_names: npt.NDArray[np.str_], spectral_coeff_train_data: npt.NDArray[np.floating[Any]], covariates_dataframe: pd.DataFrame, *, pretrained_model_params: dict[str, Any] | None = None, n_jobs: int = -1, save_directory: Path | None = None, save_separate: bool = False) -> None

Using a previously fitted spectral normative model, adapt to a new batch. This method enables adaptation (fine-tuning) of the model to data from a new batch/site by freezing all fitted parameters, and only estimating new parameters for the new batch/site category.

Parameters:

Name Type Description Default
covariate_to_adapt str

str Name of the categorical covariate representing the batch/site to which the model should be adapted. Note: This covariate must have been specified in the original model.

required
new_category_names NDArray[str_]

list[str] Names of the new categories in the covariate_to_adapt representing the new batch/site labels (e.g. names of the new site). Note: These names must not have been present in the original fitted model.

required
spectral_coeff_train_data NDArray[floating[Any]]

np.ndarray Spectral coefficients of training data :math:(T_{train} \Psi_{(k)}) \in R^{N_p \times k} as a numpy array (n_samples, n_modes).

required
covariates_dataframe DataFrame

pd.DataFrame DataFrame containing the covariates for the samples. It must include all specified covariates in the model specification. Note: The covariate_to_adapt column must only contain the new_category_names (no new data from previously trained batches).

required
pretrained_model_params dict[str, Any] | None

dict[str, Any] | None The model parameters from a previously fitted model to adapt. If None, the model parameters from the current instance will be used (assuming fitting was done).

None
n_jobs int

int (default=-1) Number of parallel jobs to use for fitting the model. If -1, all available CPU cores are used. If 1, no parallelization is used.

-1
save_directory Path | None

Path | None A path to a directory to save the adapted model. If provided, the fitted model will be saved to this path.

None
save_separate bool

bool (default=False) Whether to save the fitted direct model parameters separately for each eigenmode as individual files. This is only applicable if save_directory is provided.

False
Source code in src/spectranorm/snm.py
def adapt_fit(
    self,
    covariate_to_adapt: str,
    new_category_names: npt.NDArray[np.str_],
    spectral_coeff_train_data: npt.NDArray[np.floating[Any]],
    covariates_dataframe: pd.DataFrame,
    *,
    pretrained_model_params: dict[str, Any] | None = None,
    n_jobs: int = -1,
    save_directory: Path | None = None,
    save_separate: bool = False,
) -> None:
    """
    Using a previously fitted spectral normative model, adapt to a new
    batch.
    This method enables adaptation (fine-tuning) of the model to data
    from a new batch/site by freezing all fitted parameters, and only
    estimating new parameters for the new batch/site category.

    Args:
        covariate_to_adapt: str
            Name of the categorical covariate representing the batch/site
            to which the model should be adapted.
            Note: This covariate must have been specified in the original
            model.
        new_category_names: list[str]
            Names of the new categories in the covariate_to_adapt representing
            the new batch/site labels (e.g. names of the new site).
            Note: These names must not have been present in the original
            fitted model.
        spectral_coeff_train_data: np.ndarray
            Spectral coefficients of training data
            :math:`(T_{train} \\Psi_{(k)}) \\in R^{N_p \\times k}`
            as a numpy array (n_samples, n_modes).
        covariates_dataframe: pd.DataFrame
            DataFrame containing the covariates for the samples.
            It must include all specified covariates in the model specification.
            Note: The covariate_to_adapt column must only contain the
            new_category_names (no new data from previously trained batches).
        pretrained_model_params: dict[str, Any] | None
            The model parameters from a previously fitted model to adapt.
            If None, the model parameters from the current instance will be used
            (assuming fitting was done).
        n_jobs: int (default=-1)
            Number of parallel jobs to use for fitting the model. If -1, all
            available CPU cores are used. If 1, no parallelization is used.
        save_directory: Path | None
            A path to a directory to save the adapted model. If provided,
            the fitted model will be saved to this path.
        save_separate: bool (default=False)
            Whether to save the fitted direct model parameters separately for each
            eigenmode as individual files. This is only applicable if
            `save_directory` is provided.
    """
    # Locate the covariate to adapt
    cov_to_adapt_index = [
        cov.name for cov in self.base_model.spec.covariates
    ].index(covariate_to_adapt)

    # Extend the covariate categories to include the new categories
    self.base_model.spec.covariates[cov_to_adapt_index].extend_categories(
        new_category_names,
    )

    # Extract the pre-trained model parameters
    if pretrained_model_params is None:
        if not hasattr(self, "model_params") or self.model_params is None:
            err = (
                "No pretrained model parameters found. "
                "Please provide pretrained_model_params or fit the model first."
            )
            raise ValueError(err)
        pretrained_model_params = copy.deepcopy(self.model_params)

    # Fit the adapted model
    self.fit(
        spectral_coeff_train_data,
        covariates_dataframe,
        n_modes=pretrained_model_params["n_modes"],
        n_jobs=n_jobs,
        save_directory=save_directory,
        save_separate=save_separate,
        covariance_structure=pretrained_model_params["sparse_covariance_structure"],
        adapt={
            "covariate_to_adapt": covariate_to_adapt,
            "new_category_names": new_category_names,
            "pretrained_model_params": pretrained_model_params,
        },
    )

build_from_dataframe(eigenmode_basis: utils.gsp.EigenmodeBasis, model_type: ModelType, covariates_dataframe: pd.DataFrame, numerical_covariates: list[str] | None = None, categorical_covariates: list[str] | None = None, batch_covariates: list[str] | None = None, nonlinear_covariates: list[str] | None = None, influencing_mean: list[str] | None = None, influencing_variance: list[str] | None = None, spline_kwargs: dict[str, Any] | None = None) -> SpectralNormativeModel classmethod

Initialize SNM with an eigenmode basis and a base direct model built from a pandas DataFrame containing all covariates.

This uses the from_dataframe method of the DirectNormativeModel class to populate the direct model specification of SNM. Given that SNM does not require a fixed variable of interest, this method assigns a dummy name to the variable_of_interest parameter of the DirectNormativeModel. As such, the provided dataframe should not contain a column with "dummy_VOI" as name.

Essentially, the provided dataframe should contain all covariates as columns.

Parameters:

Name Type Description Default
eigenmode_basis EigenmodeBasis

utils.gsp.EigenmodeBasis The eigenmode basis to be used for spectral normative modeling.

required
model_type ModelType

ModelType Type of the model to create, either "HBR" (Hierarchical Bayesian Regression) or "BLR" (Bayesian Linear Regression).

required
covariates_dataframe DataFrame

pd.DataFrame DataFrame containing the data for all covariates and all samples.

required
numerical_covariates list[str] | None

list[str] | None List of numerical covariate names.

None
categorical_covariates list[str] | None

list[str] | None List of categorical covariate names.

None
batch_covariates list[str] | None

list[str] | None List of batch covariate names which should also be included in categorical_covariates.

None
nonlinear_covariates list[str] | None

list[str] | None List of covariate names to be modeled as nonlinear effects. These should also be included in numerical_covariates.

None
influencing_mean list[str] | None

list[str] | None List of covariate names that influence the mean of the variable of interest. These should be included in either numerical_covariates or categorical_covariates.

None
influencing_variance list[str] | None

list[str] | None List of covariate names that influence the variance of the variable of interest. These should be included in either numerical_covariates or categorical_covariates.

None
spline_kwargs dict[str, Any] | None

dict Additional keyword arguments for spline specification, such as df, degree, and knots. These are passed to the create_spline_spec method to create spline specifications for nonlinear covariates.

None

Returns:

Type Description
SpectralNormativeModel

SpectralNormativeModel An instance of SpectralNormativeModel with base model specs initialized based on the provided data.

Source code in src/spectranorm/snm.py
@classmethod
def build_from_dataframe(
    cls,
    eigenmode_basis: utils.gsp.EigenmodeBasis,
    model_type: ModelType,
    covariates_dataframe: pd.DataFrame,
    numerical_covariates: list[str] | None = None,
    categorical_covariates: list[str] | None = None,
    batch_covariates: list[str] | None = None,
    nonlinear_covariates: list[str] | None = None,
    influencing_mean: list[str] | None = None,
    influencing_variance: list[str] | None = None,
    spline_kwargs: dict[str, Any] | None = None,
) -> SpectralNormativeModel:
    """
    Initialize SNM with an eigenmode basis and a base direct model built from a
    pandas DataFrame containing all covariates.

    This uses the from_dataframe method of the DirectNormativeModel class
    to populate the direct model specification of SNM. Given that SNM does not
    require a fixed variable of interest, this method assigns a dummy name
    to the variable_of_interest parameter of the DirectNormativeModel. As such,
    the provided dataframe should not contain a column with "dummy_VOI" as name.

    Essentially, the provided dataframe should contain all covariates as columns.

    Args:
        eigenmode_basis: utils.gsp.EigenmodeBasis
            The eigenmode basis to be used for spectral normative modeling.
        model_type: ModelType
            Type of the model to create, either "HBR" (Hierarchical Bayesian
            Regression) or "BLR" (Bayesian Linear Regression).
        covariates_dataframe: pd.DataFrame
            DataFrame containing the data for all covariates and all samples.
        numerical_covariates: list[str] | None
            List of numerical covariate names.
        categorical_covariates: list[str] | None
            List of categorical covariate names.
        batch_covariates: list[str] | None
            List of batch covariate names which should also be included in
            categorical_covariates.
        nonlinear_covariates: list[str] | None
            List of covariate names to be modeled as nonlinear effects.
            These should also be included in numerical_covariates.
        influencing_mean: list[str] | None
            List of covariate names that influence the mean of the variable
            of interest. These should be included in either numerical_covariates
            or categorical_covariates.
        influencing_variance: list[str] | None
            List of covariate names that influence the variance of the variable
            of interest. These should be included in either numerical_covariates
            or categorical_covariates.
        spline_kwargs: dict
            Additional keyword arguments for spline specification, such as
            `df`, `degree`, and `knots`. These are passed to the
            `create_spline_spec` method to create spline specifications for
            nonlinear covariates.

    Returns:
        SpectralNormativeModel
            An instance of SpectralNormativeModel with base model specs initialized
            based on the provided data.
    """
    # Add a dummy variable of interest to the covariates_dataframe
    covariates_dataframe = covariates_dataframe.copy()
    covariates_dataframe["dummy_VOI"] = 0.0  # Dummy variable of interest
    # Specify the base model from the dataframe
    return cls(
        eigenmode_basis=eigenmode_basis,
        base_model=DirectNormativeModel.from_dataframe(
            model_type=model_type,
            dataframe=covariates_dataframe,
            variable_of_interest="dummy_VOI",  # Dummy variable of interest
            numerical_covariates=(numerical_covariates or []),
            categorical_covariates=(categorical_covariates or []),
            batch_covariates=(batch_covariates or []),
            nonlinear_covariates=(nonlinear_covariates or []),
            influencing_mean=(influencing_mean or []),
            influencing_variance=(influencing_variance or []),
            spline_kwargs=(spline_kwargs or {}),
        ),
    )

compute_spectral_predictions(test_covariates: pd.DataFrame, *, model_params: dict[str, Any] | None = None, n_modes: int | None = None, n_jobs: int = -1, predict_without: list[str] | None = None) -> dict[str, npt.NDArray[np.floating[Any]]]

Predict normative moments (mean, std) of the eigenmode basis for new data using the fitted spectral normative model.

This function requires a dataframe of covariates (test_covariates) to compute a set of spectral predictions that can subsequently be combined to efficiently estimate normative predictions for any query(ies).

Parameters:

Name Type Description Default
test_covariates DataFrame

pd.DataFrame DataFrame containing the new covariate data to predict. This must include all specified covariates. Note: covariates listed in predict_without will be ignored and are hence not required.

required
model_params dict[str, Any] | None

dict | None Optional dictionary of model parameters to use. If not provided, the stored parameters from model.fit() will be used.

None
n_modes int | None

int | None Optional number of modes to use for the prediction. If not provided, the number of modes from model_params will be used.

None
n_jobs int

int (default=-1) Number of parallel jobs to utilize. If -1, all available CPU cores are used. If 1, no parallelization is used.

-1
predict_without list[str] | None

list[str] | None Optional list of covariate names to ignore during prediction. This can be used to check the effect of removing certain covariates from the model.

None

Returns:

Name Type Description
dict dict[str, NDArray[floating[Any]]]

A dictionary containing: - 'eigenmode_mu_estimates': np.ndarray (n_samples, n_modes) - 'eigenmode_std_estimates': np.ndarray (n_samples, n_modes) - 'rho_estimates': np.ndarray (n_samples, n_covariance_pairs)

Source code in src/spectranorm/snm.py
def compute_spectral_predictions(
    self,
    test_covariates: pd.DataFrame,
    *,
    model_params: dict[str, Any] | None = None,
    n_modes: int | None = None,
    n_jobs: int = -1,
    predict_without: list[str] | None = None,
) -> dict[str, npt.NDArray[np.floating[Any]]]:
    """
    Predict normative moments (mean, std) of the eigenmode basis for new data
    using the fitted spectral normative model.

    This function requires a dataframe of covariates (test_covariates) to compute
    a set of spectral predictions that can subsequently be combined to efficiently
    estimate normative predictions for any query(ies).

    Args:
        test_covariates: pd.DataFrame
            DataFrame containing the new covariate data to predict.
            This must include all specified covariates.
            Note: covariates listed in predict_without will be ignored and are
            hence not required.
        model_params: dict | None
            Optional dictionary of model parameters to use. If not provided,
            the stored parameters from model.fit() will be used.
        n_modes: int | None
            Optional number of modes to use for the prediction. If not provided,
            the number of modes from model_params will be used.
        n_jobs: int (default=-1)
            Number of parallel jobs to utilize. If -1, all available CPU cores are
            used. If 1, no parallelization is used.
        predict_without: list[str] | None
            Optional list of covariate names to ignore during prediction.
            This can be used to check the effect of removing certain covariates
            from the model.

    Returns:
        dict:
            A dictionary containing:
            - 'eigenmode_mu_estimates': np.ndarray (n_samples, n_modes)
            - 'eigenmode_std_estimates': np.ndarray (n_samples, n_modes)
            - 'rho_estimates': np.ndarray (n_samples, n_covariance_pairs)
    """
    # Parameters
    if model_params is None:
        model_params = self.model_params

    # Find n_modes
    if n_modes is None:
        n_modes = int(model_params["n_modes"])

    if self.base_model.spec is None:
        err = "The base model is not specified. Cannot predict new data."
        raise ValueError(err)

    # Validate the covariate data
    validation_columns = [
        cov.name
        for cov in self.base_model.spec.covariates
        if cov.name not in (predict_without or [])
    ]
    utils.general.validate_dataframe(test_covariates, validation_columns)

    # direct normative predictions for each eigenmode
    (
        eigenmode_mu_estimates,
        eigenmode_std_estimates,
    ) = self._predict_all_mode_estimates(
        test_covariates,
        model_params,
        n_modes,
        n_jobs=n_jobs,
        predict_without=predict_without,
    )  # estimates have a shape of (n_samples, n_modes)

    # cross-mode dependence structure
    rho_estimates = self._predict_all_covariance_estimates(
        test_covariates,
        model_params,
        n_modes,
        n_jobs=n_jobs,
        predict_without=predict_without,
    )  # estimates have a shape of (n_samples, n_covariance_pairs)

    return {
        "eigenmode_mu_estimates": eigenmode_mu_estimates,
        "eigenmode_std_estimates": eigenmode_std_estimates,
        "rho_estimates": rho_estimates,
    }

evaluate(encoded_query: npt.NDArray[np.floating[Any]], spectral_coeff_test_data: npt.NDArray[np.floating[Any]], *, spectral_predictions: dict[str, npt.NDArray[np.floating[Any]]] | None = None, test_covariates: pd.DataFrame | None = None, query_train_moments: npt.NDArray[np.floating[Any]] | None = None, model_params: dict[str, Any] | None = None, n_modes: int | None = None) -> NormativePredictions

Evaluate the model on new data and return predictions along with evaluation metrics.

Parameters:

Name Type Description Default
encoded_query NDArray[floating[Any]]

np.ndarray Encoded query data defining the normative variable of interest. Can be provided as: - shape = (n_modes) for a single query vector - shape = (n_modes, n_queries) for multiple queries predicted at once

required
spectral_coeff_test_data NDArray[floating[Any]]

np.ndarray | None Spectral coefficient of test data for the phenotype being modeled :math:(T_{test} \Psi_{(k)}) \in R^{N_{test} \times k} (only required for extended predictions). Expects a numpy array (n_samples, n_modes)

required
spectral_predictions dict[str, NDArray[floating[Any]]] | None

dict | None Optional dictionary of precomputed spectral predictions to use for the evaluation. If not provided, test_covariates must be provided instead to compute the spectral predictions. The dictionary should contain: - 'eigenmode_mu_estimates': np.ndarray (n_samples, n_modes) - 'eigenmode_std_estimates': np.ndarray (n_samples, n_modes) - 'rho_estimates': np.ndarray (n_samples, n_covariance_pairs) This can be obtained using the 'compute_spectral_predictions' method.

None
test_covariates DataFrame | None

pd.DataFrame | None DataFrame containing the new covariate data to predict. This must include all specified covariates. Note: This is only required if spectral_predictions was not provided.

None
query_train_moments NDArray[floating[Any]] | None

np.ndarray | None A (2, n_queries) array containing the query moments (mean, std) directly measured in the training data. While optional, providing these moments is strongly recommended for accurate evaluation of the model's MSLL. If not provided, the model will use the test data moments as an approximation, which may lead to overestimating MSLL. This is made optional to allow evaluating MSLL when the training data is not accessible (e.g. using a pre-trained model).

None
model_params dict[str, Any] | None

dict | None Optional dictionary of model parameters to use. If not provided, the stored parameters from model.fit() will be used.

None
n_modes int | None

int | None Optional number of modes to use for the prediction. If not provided, the stored number of modes from model.fit() will be used.

None

Returns:

Name Type Description
NormativePredictions NormativePredictions

Object containing the predicted moments (mean, std) for the variable of interest defined by the encoded query, along with evaluation metrics.

Source code in src/spectranorm/snm.py
def evaluate(
    self,
    encoded_query: npt.NDArray[np.floating[Any]],
    spectral_coeff_test_data: npt.NDArray[np.floating[Any]],
    *,
    spectral_predictions: dict[str, npt.NDArray[np.floating[Any]]] | None = None,
    test_covariates: pd.DataFrame | None = None,
    query_train_moments: npt.NDArray[np.floating[Any]] | None = None,
    model_params: dict[str, Any] | None = None,
    n_modes: int | None = None,
) -> NormativePredictions:
    """
    Evaluate the model on new data and return predictions along with evaluation
    metrics.

    Args:
        encoded_query: np.ndarray
            Encoded query data defining the normative variable of interest.
            Can be provided as:
            - shape = (n_modes) for a single query vector
            - shape = (n_modes, n_queries) for multiple queries predicted at once
        spectral_coeff_test_data: np.ndarray | None
            Spectral coefficient of test data for the phenotype being modeled
            :math:`(T_{test} \\Psi_{(k)}) \\in R^{N_{test} \\times k}`
            (only required for extended predictions).
            Expects a numpy array (n_samples, n_modes)
        spectral_predictions: dict | None
            Optional dictionary of precomputed spectral predictions to use for
            the evaluation. If not provided, test_covariates must be provided
            instead to compute the spectral predictions.
            The dictionary should contain:
            - 'eigenmode_mu_estimates': np.ndarray (n_samples, n_modes)
            - 'eigenmode_std_estimates': np.ndarray (n_samples, n_modes)
            - 'rho_estimates': np.ndarray (n_samples, n_covariance_pairs)
            This can be obtained using the 'compute_spectral_predictions' method.
        test_covariates: pd.DataFrame | None
            DataFrame containing the new covariate data to predict.
            This must include all specified covariates.
            Note: This is only required if spectral_predictions was not provided.
        query_train_moments: np.ndarray | None
            A (2, n_queries) array containing the query moments (mean, std) directly
            measured in the training data. While optional, providing these moments
            is strongly recommended for accurate evaluation of the model's MSLL.
            If not provided, the model will use the test data moments as an
            approximation, which may lead to overestimating MSLL. This is made
            optional to allow evaluating MSLL when the training data is not
            accessible (e.g. using a pre-trained model).
        model_params: dict | None
            Optional dictionary of model parameters to use. If not provided,
            the stored parameters from model.fit() will be used.
        n_modes: int | None
            Optional number of modes to use for the prediction. If not provided,
            the stored number of modes from model.fit() will be used.

    Returns:
        NormativePredictions:
            Object containing the predicted moments (mean, std) for
            the variable of interest defined by the encoded query, along with
            evaluation metrics.
    """
    # Find n_modes
    if n_modes is None:
        n_modes = int(self.model_params["n_modes"])

    # Parameters
    if model_params is None:
        model_params = self.model_params

    # Reformat encoded queries (for efficiency)
    encoded_query = np.asarray(encoded_query[:n_modes])
    encoded_query = encoded_query.reshape(n_modes, -1, order="F")

    # Run extended predictions
    predictions = self.predict(
        encoded_query=encoded_query,
        spectral_predictions=spectral_predictions,
        test_covariates=test_covariates,
        extended=True,
        model_params=model_params,
        spectral_coeff_test_data=spectral_coeff_test_data,
        n_modes=n_modes,
    )
    if query_train_moments is None:
        logger.warning(
            "Query moments not provided. Using test data moments as an"
            " approximation, which may lead to overestimating MSLL.",
        )
        query_train_moments = np.array(
            [
                np.mean(spectral_coeff_test_data @ encoded_query, axis=0),
                np.std(spectral_coeff_test_data @ encoded_query, axis=0, ddof=1),
            ],
        )
    return predictions.evaluate_predictions(
        variable_of_interest=spectral_coeff_test_data @ encoded_query,
        train_mean=query_train_moments[0],
        train_std=query_train_moments[1],
        n_params=model_params["n_params"],
    )

fit(spectral_coeff_train_data: npt.NDArray[np.floating[Any]], covariates_dataframe: pd.DataFrame, *, n_modes: int = -1, n_jobs: int = -1, save_directory: Path | None = None, save_separate: bool = False, covariance_structure: npt.NDArray[np.floating[Any]] | float = 0.5, adapt: dict[str, Any] | None = None) -> None

Fit the spectral normative model to the provided spectral coefficient training data.

Parameters:

Name Type Description Default
spectral_coeff_train_data NDArray[floating[Any]]

np.ndarray Spectral coefficients of training data :math:(T_{train} \Psi_{(k)}) \in R^{N_p \times k} as a numpy array (n_samples, n_modes).

required
covariates_dataframe DataFrame

pd.DataFrame DataFrame containing the covariates for the samples. It must include all specified covariates in the model specification.

required
n_modes int

int (default=-1) Number of eigenmodes to fit the model to. If -1, all modes are used. If a positive integer, only the first n_modes are used. Note that the spectral_coeff_train_data and the eigenmode basis should have at least n_modes columns/eigenvectors.

-1
n_jobs int

int (default=-1) Number of parallel jobs to use for fitting the model. If -1, all available CPU cores are used. If 1, no parallelization is used.

-1
save_directory Path | None

Path | None Directory to save the fitted model. If None, the model is not saved. A subdirectory named "spectral_normative_model" will be created within the specified save_directory.

None
save_separate bool

bool (default=False) Whether to save the fitted direct model parameters separately for each eigenmode as individual files. This is only applicable if save_directory is provided.

False
covariance_structure NDArray[floating[Any]] | float

np.ndarray | float Sparse covariance structure to use for the model fitting. If a (2, n_pairs) array of row and column indices are provided, the model will use this structure. If float, the model will estimate the covariance structure based on the training data and the float value will be used as the sparsity threshold for the number of covariance pairs to keep proportional to the number of modes. Defaults to 0.5, meaning that the number of modeled sparse covariance pairs will be half the number of modes. Note: If using a small number of nodes, it is advisable to increase the sparsity threshold to ensure a stable estimation of the covariance structure. In contrast, when using a large number of nodes, a lower sparsity threshold should be used to ensure sparse modeling of the covariance structure.

0.5
adapt dict[str, Any] | None

dict[str, Any] | None (default=None) If provided, adapt a pre-trained model to a new covariate. Note: We recommended using the adapt_fit method, and not directly changing this argument, unless you know what you are doing.

None
Source code in src/spectranorm/snm.py
def fit(
    self,
    spectral_coeff_train_data: npt.NDArray[np.floating[Any]],
    covariates_dataframe: pd.DataFrame,
    *,
    n_modes: int = -1,
    n_jobs: int = -1,
    save_directory: Path | None = None,
    save_separate: bool = False,
    covariance_structure: npt.NDArray[np.floating[Any]] | float = 0.5,
    adapt: dict[str, Any] | None = None,
) -> None:
    """
    Fit the spectral normative model to the provided spectral coefficient
    training data.

    Args:
        spectral_coeff_train_data: np.ndarray
            Spectral coefficients of training data
            :math:`(T_{train} \\Psi_{(k)}) \\in R^{N_p \\times k}`
            as a numpy array (n_samples, n_modes).
        covariates_dataframe: pd.DataFrame
            DataFrame containing the covariates for the samples.
            It must include all specified covariates in the model specification.
        n_modes: int (default=-1)
            Number of eigenmodes to fit the model to. If -1, all modes are
            used. If a positive integer, only the first n_modes are used.
            Note that the spectral_coeff_train_data and the eigenmode basis should
            have at least n_modes columns/eigenvectors.
        n_jobs: int (default=-1)
            Number of parallel jobs to use for fitting the model. If -1, all
            available CPU cores are used. If 1, no parallelization is used.
        save_directory: Path | None
            Directory to save the fitted model. If None, the model is not saved.
            A subdirectory named "spectral_normative_model" will be created
            within the specified save_directory.
        save_separate: bool (default=False)
            Whether to save the fitted direct model parameters separately for each
            eigenmode as individual files. This is only applicable if
            `save_directory` is provided.
        covariance_structure: np.ndarray | float
            Sparse covariance structure to use for the model fitting. If a
            (2, n_pairs) array of row and column indices are provided, the model
            will use this structure. If float, the model will estimate the
            covariance structure based on the training data and the float value
            will be used as the sparsity threshold for the number of covariance
            pairs to keep proportional to the number of modes. Defaults to 0.5,
            meaning that the number of modeled sparse covariance pairs will be
            half the number of modes.
            Note: If using a small number of nodes, it is advisable to increase
            the sparsity threshold to ensure a stable estimation of the covariance
            structure. In contrast, when using a large number of nodes, a lower
            sparsity threshold should be used to ensure sparse modeling of the
            covariance structure.
        adapt: dict[str, Any] | None (default=None)
            If provided, adapt a pre-trained model to a new covariate.
            Note: We recommended using the `adapt_fit` method, and not directly
            changing this argument, unless you know what you are doing.
    """
    logger.info("Starting SNM model fitting:")
    # Evaluate the number of modes to fit
    if n_modes == -1:
        n_modes = self.eigenmode_basis.n_modes
    # Validate the input data
    if not isinstance(spectral_coeff_train_data, np.ndarray):
        err = "spectral_coeff_train_data must be a numpy array."
        raise TypeError(err)
    if spectral_coeff_train_data.shape[1] < n_modes:
        err = (
            f"spectral_coeff_train_data must have at least {n_modes}"
            " columns (n_modes)."
        )
        raise ValueError(err)
    if self.eigenmode_basis.n_modes < n_modes:
        err = (
            f"Eigenmode basis has only {self.eigenmode_basis.n_modes}"
            f" modes, while {n_modes} were requested."
        )
        raise ValueError(err)

    # Setup the save directory if needed
    if save_directory is not None:
        # Prepare the save directory
        save_directory = Path(save_directory)
        utils.general.prepare_save_directory(
            save_directory,
            "spectral_normative_model",
        )

    logger.info("Step 1; direct models for each eigenmode (%s modes)", n_modes)

    self.fit_all_direct(
        spectral_coeff_train_data=spectral_coeff_train_data,
        covariates_dataframe=covariates_dataframe,
        n_modes=n_modes,
        n_jobs=n_jobs,
        save_directory=save_directory,
        save_separate=save_separate,
        adapt=adapt,
    )

    logger.info("Step 2; identify sparse covariance structure")

    self.identify_covariance_structure(
        spectral_coeff_train_data=spectral_coeff_train_data,
        covariates_dataframe=covariates_dataframe,
        n_modes=n_modes,
        covariance_structure=covariance_structure,
        adapt=adapt,
    )

    # Verify that the covariance structure is valid
    if not self._is_valid_covariance_structure(self.sparse_covariance_structure):
        err = "Invalid sparse covariance structure."
        raise ValueError(err)

    # Model cross basis sparse covariance structure
    logger.info(
        "Step 3; cross-eigenmode dependency modeling (%s pairs)",
        self.sparse_covariance_structure.shape[0],
    )

    self.fit_all_covariance(
        spectral_coeff_train_data=spectral_coeff_train_data,
        covariates_dataframe=covariates_dataframe,
        n_jobs=n_jobs,
        save_directory=save_directory,
        save_separate=save_separate,
        adapt=adapt,
    )

    # Save SNM model parameters
    sample_size = spectral_coeff_train_data.shape[0]
    if adapt is not None:
        sample_size += adapt["pretrained_model_params"]["sample_size"]
    self.model_params = {
        "n_modes": n_modes,
        "sample_size": sample_size,
        "direct_model_params": self.direct_model_params,
        "sparse_covariance_structure": self.sparse_covariance_structure,
        "covariance_model_params": self.covariance_model_params,
    }
    if (self.direct_model_params[0] is not None) and (
        "n_params" in self.direct_model_params[0]
    ):
        self.model_params["n_params"] = self.direct_model_params[0]["n_params"]
    else:
        err = "Direct model parameters are not valid."
        raise ValueError(err)

    # Save the model if a save path is provided
    if save_directory is not None:
        self.save_model(save_directory)

fit_all_covariance(spectral_coeff_train_data: npt.NDArray[np.floating[Any]], covariates_dataframe: pd.DataFrame, *, n_jobs: int = -1, save_directory: Path | None = None, save_separate: bool = False, adapt: dict[str, Any] | None = None) -> None

Fit the direct models for all specified eigenmodes.

Parameters:

Name Type Description Default
spectral_coeff_train_data NDArray[floating[Any]]

np.ndarray Spectral coefficients of training data :math:(T_{train} \Psi_{(k)}) \in R^{N_p \times k} as a numpy array (n_samples, n_modes).

required
covariates_dataframe DataFrame

pd.DataFrame DataFrame containing the covariates for the samples. It must include all specified covariates in the model specification.

required
n_jobs int

int (default=-1) Number of parallel jobs to use for fitting the model. If -1, all available CPU cores are used. If 1, no parallelization is used.

-1
save_directory Path | None

Path | None Directory to save the fitted model. If None, the model is not saved. A subdirectory named "spectral_normative_model" will be created within the specified save_directory.

None
save_separate bool

bool (default=False) Whether to save the fitted direct model parameters separately for each eigenmode as individual files. This is only applicable if save_directory is provided.

False
adapt dict[str, Any] | None

dict[str, Any] | None Adaptation parameters from a previously fitted model. If provided, the model will be adapted using these parameters during fitting.

None
Source code in src/spectranorm/snm.py
def fit_all_covariance(
    self,
    spectral_coeff_train_data: npt.NDArray[np.floating[Any]],
    covariates_dataframe: pd.DataFrame,
    *,
    n_jobs: int = -1,
    save_directory: Path | None = None,
    save_separate: bool = False,
    adapt: dict[str, Any] | None = None,
) -> None:
    """
    Fit the direct models for all specified eigenmodes.

    Args:
        spectral_coeff_train_data: np.ndarray
            Spectral coefficients of training data
            :math:`(T_{train} \\Psi_{(k)}) \\in R^{N_p \\times k}`
            as a numpy array (n_samples, n_modes).
        covariates_dataframe: pd.DataFrame
            DataFrame containing the covariates for the samples.
            It must include all specified covariates in the model specification.
        n_jobs: int (default=-1)
            Number of parallel jobs to use for fitting the model. If -1, all
            available CPU cores are used. If 1, no parallelization is used.
        save_directory: Path | None
            Directory to save the fitted model. If None, the model is not saved.
            A subdirectory named "spectral_normative_model" will be created
            within the specified save_directory.
        save_separate: bool (default=False)
            Whether to save the fitted direct model parameters separately for each
            eigenmode as individual files. This is only applicable if
            `save_directory` is provided.
        adapt: dict[str, Any] | None
            Adaptation parameters from a previously fitted model. If provided,
            the model will be adapted using these parameters during fitting.
    """
    # Setup the save directory if needed
    if save_directory is not None:
        save_directory = Path(save_directory)

    # Fit the base covariance models for selected eigenmode pairs in parallel
    tasks = (
        joblib.delayed(self.fit_single_covariance)(
            variable_of_interest_1=spectral_coeff_train_data[
                :,
                self.sparse_covariance_structure[i, 0],
            ],
            variable_of_interest_2=spectral_coeff_train_data[
                :,
                self.sparse_covariance_structure[i, 1],
            ],
            direct_model_params_1=self.direct_model_params[
                self.sparse_covariance_structure[i, 0]
            ],
            direct_model_params_2=self.direct_model_params[
                self.sparse_covariance_structure[i, 1]
            ],
            covariates_dataframe=covariates_dataframe,
            save_directory=(
                utils.general.ensure_dir(
                    save_directory
                    / "spectral_normative_model"
                    / "covariance_models"
                    / (
                        f"mode_{self.sparse_covariance_structure[i, 0] + 1},"
                        f"mode_{self.sparse_covariance_structure[i, 1] + 1}"
                    ),
                )
                if save_directory is not None and save_separate
                else None
            ),
            adapt=(
                None
                if adapt is None
                else {
                    "covariate_to_adapt": adapt["covariate_to_adapt"],
                    "new_category_names": adapt["new_category_names"],
                    "pretrained_model_params": adapt["pretrained_model_params"][
                        "covariance_model_params"
                    ][i],
                }
            ),
        )
        for i in range(self.sparse_covariance_structure.shape[0])
    )
    self.covariance_model_params = utils.parallel.ParallelTqdm(
        n_jobs=n_jobs,
        total_tasks=self.sparse_covariance_structure.shape[0],
        desc="Fitting covariance models",
    )(tasks)  # pyright: ignore[reportCallIssue]

fit_all_direct(spectral_coeff_train_data: npt.NDArray[np.floating[Any]], covariates_dataframe: pd.DataFrame, *, n_modes: int = -1, n_jobs: int = -1, save_directory: Path | None = None, save_separate: bool = False, adapt: dict[str, Any] | None = None) -> None

Fit the direct models for all specified eigenmodes.

Parameters:

Name Type Description Default
spectral_coeff_train_data NDArray[floating[Any]]

np.ndarray Spectral coefficients of training data :math:(T_{train} \Psi_{(k)}) \in R^{N_p \times k} as a numpy array (n_samples, n_modes).

required
covariates_dataframe DataFrame

pd.DataFrame DataFrame containing the covariates for the samples. It must include all specified covariates in the model specification.

required
n_modes int

int (default=-1) Number of eigenmodes to fit the model to. If -1, all modes are used. If a positive integer, only the first n_modes are used. Note that the spectral_coeff_train_data and the eigenmode basis should have at least n_modes columns/eigenvectors.

-1
n_jobs int

int (default=-1) Number of parallel jobs to use for fitting the model. If -1, all available CPU cores are used. If 1, no parallelization is used.

-1
save_directory Path | None

Path | None Directory to save the fitted model. If None, the model is not saved. A subdirectory named "spectral_normative_model" will be created within the specified save_directory.

None
save_separate bool

bool (default=False) Whether to save the fitted direct model parameters separately for each eigenmode as individual files. This is only applicable if save_directory is provided.

False
adapt dict[str, Any] | None

dict[str, Any] | None Adaptation parameters from a previously fitted model. If provided, the model will be adapted using these parameters during fitting.

None
Source code in src/spectranorm/snm.py
def fit_all_direct(
    self,
    spectral_coeff_train_data: npt.NDArray[np.floating[Any]],
    covariates_dataframe: pd.DataFrame,
    *,
    n_modes: int = -1,
    n_jobs: int = -1,
    save_directory: Path | None = None,
    save_separate: bool = False,
    adapt: dict[str, Any] | None = None,
) -> None:
    """
    Fit the direct models for all specified eigenmodes.

    Args:
        spectral_coeff_train_data: np.ndarray
            Spectral coefficients of training data
            :math:`(T_{train} \\Psi_{(k)}) \\in R^{N_p \\times k}`
            as a numpy array (n_samples, n_modes).
        covariates_dataframe: pd.DataFrame
            DataFrame containing the covariates for the samples.
            It must include all specified covariates in the model specification.
        n_modes: int (default=-1)
            Number of eigenmodes to fit the model to. If -1, all modes are
            used. If a positive integer, only the first n_modes are used.
            Note that the spectral_coeff_train_data and the eigenmode basis should
            have at least n_modes columns/eigenvectors.
        n_jobs: int (default=-1)
            Number of parallel jobs to use for fitting the model. If -1, all
            available CPU cores are used. If 1, no parallelization is used.
        save_directory: Path | None
            Directory to save the fitted model. If None, the model is not saved.
            A subdirectory named "spectral_normative_model" will be created
            within the specified save_directory.
        save_separate: bool (default=False)
            Whether to save the fitted direct model parameters separately for each
            eigenmode as individual files. This is only applicable if
            `save_directory` is provided.
        adapt: dict[str, Any] | None
            Adaptation parameters from a previously fitted model. If provided,
            the model will be adapted using these parameters during fitting.
    """
    # Setup the save directory if needed
    if save_directory is not None:
        save_directory = Path(save_directory)

    # Evaluate the number of modes to fit
    if n_modes == -1:
        n_modes = self.eigenmode_basis.n_modes

    # Fit the base direct model for each eigenmode using parallel processing
    tasks = (
        joblib.delayed(self.fit_single_direct)(
            variable_of_interest=spectral_coeff_train_data[:, i],
            covariates_dataframe=covariates_dataframe,
            save_directory=(
                utils.general.ensure_dir(
                    save_directory
                    / "spectral_normative_model"
                    / "direct_models"
                    / f"mode_{i + 1}",
                )
                if save_directory is not None and save_separate
                else None
            ),
            adapt=(
                None
                if adapt is None
                else {
                    "covariate_to_adapt": adapt["covariate_to_adapt"],
                    "new_category_names": adapt["new_category_names"],
                    "pretrained_model_params": adapt["pretrained_model_params"][
                        "direct_model_params"
                    ][i],
                }
            ),
        )
        for i in range(n_modes)
    )
    self.direct_model_params = list(
        utils.parallel.ParallelTqdm(
            n_jobs=n_jobs,
            total_tasks=n_modes,
            desc="Fitting direct models",
        )(tasks),  # pyright: ignore[reportCallIssue]
    )

fit_single_covariance(variable_of_interest_1: npt.NDArray[np.floating[Any]], variable_of_interest_2: npt.NDArray[np.floating[Any]], direct_model_params_1: dict[str, Any], direct_model_params_2: dict[str, Any], covariates_dataframe: pd.DataFrame, *, save_directory: Path | None = None, return_model_params: bool = True, defaults_overwrite: dict[str, Any] | None = None, adapt: dict[str, Any] | None = None) -> dict[str, Any] | None

Fit a covariance normative model between a single pair of eigenmodes. This method fits a covariance model to the provided pair of variables and covariates dataframe, considering the direct model fits for each eigenmode, while allowing for the cross-eigenmode covariance to vary normatively.

Parameters:

Name Type Description Default
variable_of_interest_1 NDArray[floating[Any]]

np.ndarray The loading vector capturing the variance within training data that corresponds to a single eigenmode.

required
variable_of_interest_2 NDArray[floating[Any]]

np.ndarray The loading vector capturing the variance within training data that corresponds to a second eigenmode.

required
direct_model_params_1 dict[str, Any]

dict The parameters of the direct model fitted to the first eigenmode.

required
direct_model_params_2 dict[str, Any]

dict The parameters of the direct model fitted to the second eigenmode.

required
covariates_dataframe DataFrame

pd.DataFrame DataFrame containing the covariates for the samples.

required
save_directory Path | None

Path | None Directory to save the fitted model. If None, the model is not saved.

None
return_model_params bool

bool If True, return the fitted model parameters.

True
defaults_overwrite dict[str, Any] | None

dict (default={}) Dictionary of default values to overwrite in the model fitting process.

None
adapt dict[str, Any] | None

dict[str, Any] | None = None Adaptation parameters from a previously fitted model. If provided, the model will be adapted using these parameters during fitting.

None

Returns:

Name Type Description
dict dict[str, Any] | None

If return_model_params is True, return the fitted model parameters in a dictionary.

Source code in src/spectranorm/snm.py
def fit_single_covariance(
    self,
    variable_of_interest_1: npt.NDArray[np.floating[Any]],
    variable_of_interest_2: npt.NDArray[np.floating[Any]],
    direct_model_params_1: dict[str, Any],
    direct_model_params_2: dict[str, Any],
    covariates_dataframe: pd.DataFrame,
    *,
    save_directory: Path | None = None,
    return_model_params: bool = True,
    defaults_overwrite: dict[str, Any] | None = None,
    adapt: dict[str, Any] | None = None,
) -> dict[str, Any] | None:
    """
    Fit a covariance normative model between a single pair of eigenmodes.
    This method fits a covariance model to the provided pair of variables
    and covariates dataframe, considering the direct model fits for each
    eigenmode, while allowing for the cross-eigenmode covariance to vary
    normatively.

    Args:
        variable_of_interest_1: np.ndarray
            The loading vector capturing the variance within training data that
            corresponds to a single eigenmode.
        variable_of_interest_2: np.ndarray
            The loading vector capturing the variance within training data that
            corresponds to a second eigenmode.
        direct_model_params_1: dict
            The parameters of the direct model fitted to the first eigenmode.
        direct_model_params_2: dict
            The parameters of the direct model fitted to the second eigenmode.
        covariates_dataframe: pd.DataFrame
            DataFrame containing the covariates for the samples.
        save_directory: Path | None
            Directory to save the fitted model. If None, the model is not saved.
        return_model_params: bool
            If True, return the fitted model parameters.
        defaults_overwrite: dict (default={})
            Dictionary of default values to overwrite in the model fitting process.
        adapt: dict[str, Any] | None = None
            Adaptation parameters from a previously fitted model. If provided,
            the model will be adapted using these parameters during fitting.

    Returns:
        dict:
            If `return_model_params` is True, return the fitted model parameters
            in a dictionary.
    """
    # Prepare the data for fitting
    train_data = covariates_dataframe.copy()
    # Add the respective mode loadings as the variables of interest
    train_data["VOI_1"] = variable_of_interest_1
    train_data["VOI_2"] = variable_of_interest_2
    train_data[["VOI_1_mu_estimate", "VOI_1_std_estimate"]] = (
        self.base_model.predict(
            train_data,
            model_params=direct_model_params_1,
        )
        .to_array()
        .T
    )  # Add the direct model predictions
    train_data[["VOI_2_mu_estimate", "VOI_2_std_estimate"]] = (
        self.base_model.predict(
            train_data,
            model_params=direct_model_params_2,
        )
        .to_array()
        .T
    )  # Add the direct model predictions

    # Instantiate a covariance normative model from the base model
    covariance_model = CovarianceNormativeModel.from_direct_model(
        self.base_model,
        variable_of_interest_1="VOI_1",
        variable_of_interest_2="VOI_2",
        defaults_overwrite=(defaults_overwrite or {}),
    )

    # Fit the model silently
    with utils.general.suppress_output():
        covariance_model.fit(
            train_data=train_data,
            save_directory=save_directory,
            progress_bar=False,
            adapt=adapt,
        )

    # Return the fitted model parameters if requested
    if return_model_params:
        return covariance_model.model_params

    # If not returning model parameters, return None
    return None

fit_single_direct(variable_of_interest: npt.NDArray[np.floating[Any]], covariates_dataframe: pd.DataFrame, *, save_directory: Path | None = None, return_model_params: bool = True, adapt: dict[str, Any] | None = None) -> dict[str, Any] | None

Fit a direct normative model for a single spectral eigenmode. This method fits the base direct model to the provided variable of interest and covariates dataframe, allowing for the model to be trained on a specific eigenmode of the spectral embedding.

Parameters:

Name Type Description Default
variable_of_interest NDArray[floating[Any]]

np.ndarray The loading vector capturing the variance within training data that corresponds to a single eigenmode.

required
covariates_dataframe DataFrame

pd.DataFrame DataFrame containing the covariates for the samples.

required
save_directory Path | None

Path | None Directory to save the fitted model. If None, the model is not saved.

None
return_model_params bool

bool If True, return the fitted model parameters.

True
adapt dict[str, Any] | None

dict[str, Any] | None Adaptation parameters from a previously fitted model. If provided, the model will be adapted using these parameters during fitting.

None

Returns:

Name Type Description
dict dict[str, Any] | None

If return_model_params is True, return the fitted model parameters in a dictionary.

Source code in src/spectranorm/snm.py
def fit_single_direct(
    self,
    variable_of_interest: npt.NDArray[np.floating[Any]],
    covariates_dataframe: pd.DataFrame,
    *,
    save_directory: Path | None = None,
    return_model_params: bool = True,
    adapt: dict[str, Any] | None = None,
) -> dict[str, Any] | None:
    """
    Fit a direct normative model for a single spectral eigenmode.
    This method fits the base direct model to the provided variable of interest
    and covariates dataframe, allowing for the model to be trained on a specific
    eigenmode of the spectral embedding.

    Args:
        variable_of_interest: np.ndarray
            The loading vector capturing the variance within training data that
            corresponds to a single eigenmode.
        covariates_dataframe: pd.DataFrame
            DataFrame containing the covariates for the samples.
        save_directory: Path | None
            Directory to save the fitted model. If None, the model is not saved.
        return_model_params: bool
            If True, return the fitted model parameters.
        adapt: dict[str, Any] | None
            Adaptation parameters from a previously fitted model. If provided,
            the model will be adapted using these parameters during fitting.

    Returns:
        dict:
            If `return_model_params` is True, return the fitted model parameters
            in a dictionary.
    """
    # Prepare the data for fitting
    train_data = covariates_dataframe.copy()
    # Add the mode loading as the variable of interest
    train_data["VOI"] = variable_of_interest

    # Instantiate a direct normative model from the base model
    direct_model = DirectNormativeModel(
        spec=NormativeModelSpec(
            variable_of_interest="VOI",  # Use the added VOI column
            covariates=self.base_model.spec.covariates,
            influencing_mean=self.base_model.spec.influencing_mean,
            influencing_variance=self.base_model.spec.influencing_variance,
        ),
        defaults=self.base_model.defaults,
    )

    # Fit the model silently
    with utils.general.suppress_output():
        direct_model.fit(
            train_data=train_data,
            save_directory=save_directory,
            progress_bar=False,
            adapt=adapt,
        )

    # Return the fitted model parameters if requested
    if return_model_params:
        return direct_model.model_params

    # If not returning model parameters, return None
    return None

harmonize(encoded_query: npt.NDArray[np.floating[Any]], spectral_coeff_data: npt.NDArray[np.floating[Any]], *, covariates_to_harmonize: list[str] | None = None, covariates_dataframe: pd.DataFrame | None = None, spectral_predictions_full: dict[str, npt.NDArray[np.floating[Any]]] | None = None, spectral_predictions_partial: dict[str, npt.NDArray[np.floating[Any]]] | None = None, model_params: dict[str, Any] | None = None, n_modes: int | None = None) -> npt.NDArray[np.floating[Any]]

Harmonize the variables of interest in the data to remove effects of certain covariates (e.g. batch). This method uses the spectral normative model to harmonize one or several variables of interest defined by the encoded query.

The harmonization method can be used in two ways: - By providing a dataframe of covariates (covariates_dataframe) to compute the necessary spectral predictions for both the full model (all covariates) and the partial model (excluding covariates to harmonize). In this format, you should also provide the covariates_to_harmonize (list of covariate names). - By providing precomputed spectral predictions for both the full and partial models (spectral_predictions_full and spectral_predictions_partial). In this format, the partial spectral predictions should have been computed by excluding the covariates to harmonize using the predict_without parameter in the compute_spectral_predictions method. Note: In the latter, the method will not use the covariates_to_harmonize list.

Parameters:

Name Type Description Default
encoded_query NDArray[floating[Any]]

np.ndarray Encoded query data defining the normative variable of interest. Can be provided as: - shape = (n_modes) for a single query vector - shape = (n_modes, n_queries) for multiple queries predicted at once

required
spectral_coeff_data NDArray[floating[Any]]

np.ndarray | None Spectral coefficient of the the phenotype being modeled :math:(T \Psi_{(k)}) \in R^{N_{p} \times k}. Expects a numpy array (n_samples, n_modes)

required
covariates_to_harmonize list[str] | None

list[str] | None List of covariate names to harmonize. The partial effects of these covariates will be removed from the variable of interest, and the harmonized values will be returned. Note: This is only required if spectral_predictions_full and spectral_predictions_partial were not provided.

None
covariates_dataframe DataFrame | None

pd.DataFrame | None DataFrame containing covariate information for the data to harmonize. This must include all specified covariates. The dataframe is expected to have all covariates as columns and samples as rows. Note: This is only required if spectral_predictions_full and spectral_predictions_partial were not provided. Alternatively, if any of the aforementioned spectral predictions were previously computed, then they could be passed to this method to avoid recomputation.

None
spectral_predictions_full dict[str, NDArray[floating[Any]]] | None

dict | None Optional dictionary of precomputed spectral predictions to use for the harmonization. If not provided, covariates_dataframe must be provided instead to compute the spectral predictions. These predictions use all set of covariates. The dictionary should contain: - 'eigenmode_mu_estimates': np.ndarray (n_samples, n_modes) - 'eigenmode_std_estimates': np.ndarray (n_samples, n_modes) - 'rho_estimates': np.ndarray (n_samples, n_covariance_pairs) This can be obtained using the 'compute_spectral_predictions' method.

None
spectral_predictions_partial dict[str, NDArray[floating[Any]]] | None

dict | None Optional dictionary of precomputed spectral predictions to use for the harmonization. If not provided, covariates_dataframe must be provided instead to compute the partial spectral predictions. These predictions use all set of covariates except those to harmonize. The covariates to harmonize need to be partialed out using the predict_without parameter. The dictionary should contain: - 'eigenmode_mu_estimates': np.ndarray (n_samples, n_modes) - 'eigenmode_std_estimates': np.ndarray (n_samples, n_modes) - 'rho_estimates': np.ndarray (n_samples, n_covariance_pairs) This can be obtained using the 'compute_spectral_predictions' method.

None
model_params dict[str, Any] | None

dict | None Optional dictionary of model parameters to use. If not provided, the stored parameters from model.fit() will be used.

None
n_modes int | None

int | None Optional number of modes to use for the prediction. If not provided, the stored number of modes from model.fit() will be used.

None

Returns:

Type Description
NDArray[floating[Any]]

npt.NDArray[np.floating[Any]]: Array of harmonized values for the variable of interest.

Source code in src/spectranorm/snm.py
def harmonize(
    self,
    encoded_query: npt.NDArray[np.floating[Any]],
    spectral_coeff_data: npt.NDArray[np.floating[Any]],
    *,
    covariates_to_harmonize: list[str] | None = None,
    covariates_dataframe: pd.DataFrame | None = None,
    spectral_predictions_full: dict[str, npt.NDArray[np.floating[Any]]]
    | None = None,
    spectral_predictions_partial: dict[str, npt.NDArray[np.floating[Any]]]
    | None = None,
    model_params: dict[str, Any] | None = None,
    n_modes: int | None = None,
) -> npt.NDArray[np.floating[Any]]:
    """
    Harmonize the variables of interest in the data to remove effects of
    certain covariates (e.g. batch). This method uses the spectral normative model
    to harmonize one or several variables of interest defined by the encoded query.

    The harmonization method can be used in two ways:
    - By providing a dataframe of covariates (covariates_dataframe) to compute the
      necessary spectral predictions for both the full model (all covariates)
      and the partial model (excluding covariates to harmonize). In this format,
      you should also provide the covariates_to_harmonize (list of covariate names).
    - By providing precomputed spectral predictions for both the full and partial
      models (spectral_predictions_full and spectral_predictions_partial). In this
      format, the partial spectral predictions should have been computed by
      excluding the covariates to harmonize using the predict_without parameter in
      the compute_spectral_predictions method.
      Note: In the latter, the method will not use the covariates_to_harmonize list.

    Args:
        encoded_query: np.ndarray
            Encoded query data defining the normative variable of interest.
            Can be provided as:
            - shape = (n_modes) for a single query vector
            - shape = (n_modes, n_queries) for multiple queries predicted at once
        spectral_coeff_data: np.ndarray | None
            Spectral coefficient of the the phenotype being modeled
            :math:`(T \\Psi_{(k)}) \\in R^{N_{p} \\times k}`.
            Expects a numpy array (n_samples, n_modes)
        covariates_to_harmonize: list[str] | None
            List of covariate names to harmonize.
            The partial effects of these covariates will be removed from the
            variable of interest, and the harmonized values will be returned.
            Note: This is only required if spectral_predictions_full and
            spectral_predictions_partial were not provided.
        covariates_dataframe: pd.DataFrame | None
            DataFrame containing covariate information for the data to harmonize.
            This must include all specified covariates. The dataframe is expected
            to have all covariates as columns and samples as rows.
            Note: This is only required if spectral_predictions_full and
            spectral_predictions_partial were not provided. Alternatively,
            if any of the aforementioned spectral predictions were previously
            computed, then they could be passed to this method to avoid
            recomputation.
        spectral_predictions_full: dict | None
            Optional dictionary of precomputed spectral predictions to use for
            the harmonization. If not provided, covariates_dataframe must be
            provided instead to compute the spectral predictions.
            These predictions use all set of covariates.
            The dictionary should contain:
            - 'eigenmode_mu_estimates': np.ndarray (n_samples, n_modes)
            - 'eigenmode_std_estimates': np.ndarray (n_samples, n_modes)
            - 'rho_estimates': np.ndarray (n_samples, n_covariance_pairs)
            This can be obtained using the 'compute_spectral_predictions' method.
        spectral_predictions_partial: dict | None
            Optional dictionary of precomputed spectral predictions to use for
            the harmonization. If not provided, covariates_dataframe must be
            provided instead to compute the partial spectral predictions.
            These predictions use all set of covariates except those to harmonize.
            The covariates to harmonize need to be partialed out using the
            predict_without parameter.
            The dictionary should contain:
            - 'eigenmode_mu_estimates': np.ndarray (n_samples, n_modes)
            - 'eigenmode_std_estimates': np.ndarray (n_samples, n_modes)
            - 'rho_estimates': np.ndarray (n_samples, n_covariance_pairs)
            This can be obtained using the 'compute_spectral_predictions' method.
        model_params: dict | None
            Optional dictionary of model parameters to use. If not provided,
            the stored parameters from model.fit() will be used.
        n_modes: int | None
            Optional number of modes to use for the prediction. If not provided,
            the stored number of modes from model.fit() will be used.

    Returns:
        npt.NDArray[np.floating[Any]]: Array of harmonized values for the
            variable of interest.
    """
    if (spectral_predictions_full is None) or (
        spectral_predictions_partial is None
    ):
        if (covariates_dataframe is None) or (covariates_to_harmonize is None):
            err = (
                "Either [covariates_dataframe and covariates_to_harmonize] or "
                "both spectral_predictions_full "
                "and spectral_predictions_partial must be provided."
            )
            raise ValueError(err)
        # Validate the new data
        validation_columns = [cov.name for cov in self.base_model.spec.covariates]
        utils.general.validate_dataframe(covariates_dataframe, validation_columns)

    # Find n_modes
    if n_modes is None:
        n_modes = int(self.model_params["n_modes"])

    # Parameters
    if model_params is None:
        model_params = self.model_params

    # Reformat encoded queries (for efficiency)
    encoded_query = np.asarray(encoded_query[:n_modes])
    encoded_query = encoded_query.reshape(n_modes, -1, order="F")

    # Predict the mean and std with all covariates
    full_predictions = self.predict(
        encoded_query=encoded_query,
        spectral_predictions=spectral_predictions_full,
        test_covariates=covariates_dataframe,
        model_params=model_params,
        n_modes=n_modes,
        predict_without=[],
    )

    # Predict the mean and std without the covariates to harmonize
    reduced_predictions = self.predict(
        encoded_query=encoded_query,
        spectral_predictions=spectral_predictions_partial,
        test_covariates=covariates_dataframe,
        model_params=model_params,
        n_modes=n_modes,
        predict_without=covariates_to_harmonize,
    )

    # Reconstruct observed phenotype for query from spectral coefficients
    observed_phenotype = spectral_coeff_data @ encoded_query[:n_modes]

    # First standardize the variable of interest based on the full model
    vois_standardized = (
        observed_phenotype - full_predictions.predictions["mu_estimate"]
    ) / full_predictions.predictions["std_estimate"]

    # Then return the harmonized values based on the reduced model
    return np.asarray(
        (
            vois_standardized * reduced_predictions.predictions["std_estimate"]
            + reduced_predictions.predictions["mu_estimate"]
        ),
        dtype=np.float64,
    )

identify_covariance_structure(spectral_coeff_train_data: npt.NDArray[np.floating[Any]], covariates_dataframe: pd.DataFrame, n_modes: int, covariance_structure: npt.NDArray[np.floating[Any]] | float = 0.5, adapt: dict[str, Any] | None = None) -> None

Identify and set the sparse covariance structure for the spectral normative model based on the provided training data and covariance structure input.

Parameters:

Name Type Description Default
spectral_coeff_train_data NDArray[floating[Any]]

np.ndarray Spectral coefficients of training data :math:(T_{train} \Psi_{(k)}) \in R^{N_p \times k} as a numpy array (n_samples, n_modes).

required
covariates_dataframe DataFrame

pd.DataFrame DataFrame containing the covariates for the samples.

required
n_modes int

int Number of eigenmodes to consider.

required
covariance_structure NDArray[floating[Any]] | float

np.ndarray | float Sparse covariance structure to use for the model fitting. If a (2, n_pairs) array of row and column indices are provided, the model will use this structure. If float, the model will estimate the covariance structure based on the training data and the float value will be used as the sparsity threshold for the number of covariance pairs to keep proportional to the number of modes. Defaults to 0.5, meaning that the number of modeled sparse covariance pairs will be half the number of modes.

0.5
adapt dict[str, Any] | None

dict[str, Any] | None Adaptation parameters from a previously fitted model. If provided, the sparse covariance structure from the pretrained model parameters will be used instead of estimating a new one.

None
Source code in src/spectranorm/snm.py
def identify_covariance_structure(
    self,
    spectral_coeff_train_data: npt.NDArray[np.floating[Any]],
    covariates_dataframe: pd.DataFrame,
    n_modes: int,
    covariance_structure: npt.NDArray[np.floating[Any]] | float = 0.5,
    adapt: dict[str, Any] | None = None,
) -> None:
    """
    Identify and set the sparse covariance structure for the spectral normative
    model based on the provided training data and covariance structure input.

    Args:
        spectral_coeff_train_data: np.ndarray
            Spectral coefficients of training data
            :math:`(T_{train} \\Psi_{(k)}) \\in R^{N_p \\times k}`
            as a numpy array (n_samples, n_modes).
        covariates_dataframe: pd.DataFrame
            DataFrame containing the covariates for the samples.
        n_modes: int
            Number of eigenmodes to consider.
        covariance_structure: np.ndarray | float
            Sparse covariance structure to use for the model fitting. If a
            (2, n_pairs) array of row and column indices are provided, the model
            will use this structure. If float, the model will estimate the
            covariance structure based on the training data and the float value
            will be used as the sparsity threshold for the number of covariance
            pairs to keep proportional to the number of modes. Defaults to 0.5,
            meaning that the number of modeled sparse covariance pairs will be
            half the number of modes.
        adapt: dict[str, Any] | None
            Adaptation parameters from a previously fitted model. If provided,
            the sparse covariance structure from the pretrained model parameters
            will be used instead of estimating a new one.
    """
    if adapt is not None:
        covariance_structure = adapt["pretrained_model_params"][
            "sparse_covariance_structure"
        ]

    # Identify sparse covariance structure if a float value is given
    if isinstance(covariance_structure, float):
        # Use trained models to compute z-scores
        spectral_train_z_scores = np.array(
            [
                self.base_model.predict(
                    test_covariates=covariates_dataframe,
                    model_params=self.direct_model_params[x],
                )
                .extend_predictions(
                    variable_of_interest=spectral_coeff_train_data[:, x],
                )
                .predictions["z-score"]
                for x in range(n_modes)
            ],
        ).T

        self.sparse_covariance_structure = (
            self.identify_sparse_covariance_structure(
                spectral_train_z_scores,
                covariance_structure,
            )
        )
    else:
        self.sparse_covariance_structure = np.array(covariance_structure)

    # Verify that the covariance structure is valid
    if not self._is_valid_covariance_structure(self.sparse_covariance_structure):
        err = "Invalid sparse covariance structure."
        raise ValueError(err)

identify_sparse_covariance_structure(data: npt.NDArray[np.floating[Any]], sparsity_threshold: float = 1) -> npt.NDArray[np.integer[Any]]

Identify the sparse cross-basis covariance structure in the phenotype. This method analyzes the phenotype's spectral coefficients to determine the covariance pairs that need to be modeled.

Note: if the batches become too small, this estimate can become less stable in which case it is recommended to provide the sparse covariance structure to the model instead.

Parameters:

Name Type Description Default
data NDArray[floating[Any]]

np.ndarray Spectral coefficients of training data representing the phenotype in the graph frequency domain :math:(T_{train} \Psi_{(k)}) \in R^{N_p \times k} as a numpy array (n_samples, n_modes).

required
sparsity_threshold float

float Number of strongest correlations to keep (proportional to the number of modes). Defaults to 1, meaning that the number of sparse covariance pairs will be equal to the number of modes. If set to a lower value, fewer covariance pairs will be retained.

1

Returns:

Type Description
NDArray[integer[Any]]

np.ndarray: A (N, 2) array: the rows and columns of the identified sparse covariance structure.

Source code in src/spectranorm/snm.py
def identify_sparse_covariance_structure(
    self,
    data: npt.NDArray[np.floating[Any]],
    sparsity_threshold: float = 1,
) -> npt.NDArray[np.integer[Any]]:
    """
    Identify the sparse cross-basis covariance structure in the phenotype.
    This method analyzes the phenotype's spectral coefficients to determine the
    covariance pairs that need to be modeled.

    Note: if the batches become too small, this estimate can become less stable
    in which case it is recommended to provide the sparse covariance structure
    to the model instead.

    Args:
        data: np.ndarray
            Spectral coefficients of training data representing the phenotype in
            the graph frequency domain
            :math:`(T_{train} \\Psi_{(k)}) \\in R^{N_p \\times k}`
            as a numpy array (n_samples, n_modes).
        sparsity_threshold: float
            Number of strongest correlations to keep (proportional to the number
            of modes). Defaults to 1, meaning that the number of sparse covariance
            pairs will be equal to the number of modes. If set to a lower value,
            fewer covariance pairs will be retained.

    Returns:
        np.ndarray:
            A (N, 2) array: the rows and columns of the
            identified sparse covariance structure.
    """
    # Start with correlation structure across the whole sample
    correlations = np.corrcoef(data.T)

    # Remove self-correlations
    np.fill_diagonal(correlations, 0)

    # Extract the upper triangle of the correlation matrix
    upper_triangle_indices = np.triu_indices(correlations.shape[0], k=1)

    # Determine the number of correlations to keep
    n_correlations_to_keep = int(
        sparsity_threshold * correlations.shape[0],
    )

    # Find the cutoff value for the top correlations
    if n_correlations_to_keep < len(upper_triangle_indices[0]):
        cutoff_value = np.partition(
            np.abs(correlations[upper_triangle_indices]),
            -n_correlations_to_keep,
        )[-n_correlations_to_keep]
    else:
        cutoff_value = 0
        # Warn the user if they are keeping all correlations
        logger.warning(
            "Sparsity threshold is high, keeping all correlations.",
        )

    # Now compute the sparsity structure based on the resulting matrix
    rows, cols = np.where(np.abs(correlations) > cutoff_value)

    # Remove redundant and duplicate pairs
    rows_lim = rows[rows < cols]
    cols_lim = cols[rows < cols]

    return np.array([rows_lim, cols_lim]).T

load_model(directory: Path, mmap_mode: MmapMode | None = 'r') -> SpectralNormativeModel classmethod

Load a spectral normative model instance from the specified save directory.

Parameters:

Name Type Description Default
directory Path

Path Directory to load the fitted model from. A subdirectory named "spectral_normative_model" will be searched within this directory.

required
mmap_mode MmapMode | None

MmapMode | None Memory mapping mode for joblib (default: "r"). You can set this to None to disable memory-mapping.

'r'
Source code in src/spectranorm/snm.py
@classmethod
def load_model(
    cls,
    directory: Path,
    mmap_mode: MmapMode | None = "r",
) -> SpectralNormativeModel:
    """
    Load a spectral normative model instance from the specified save directory.

    Args:
        directory: Path
            Directory to load the fitted model from. A subdirectory named
            "spectral_normative_model" will be searched within this directory.
        mmap_mode: MmapMode | None
            Memory mapping mode for joblib (default: "r").
            You can set this to None to disable memory-mapping.
    """
    # Validate the load directory
    directory = Path(directory)
    saved_model_dir = utils.general.validate_load_directory(
        directory,
        "spectral_normative_model",
    )

    # Check if the pickled joblib file exists in this directory
    for filename in ["spectral_model_dict.joblib", "eigenmode_basis.joblib"]:
        if not (saved_model_dir / filename).exists():
            err = f"Model Load Error: Required file '{filename}' does not exist."
            raise FileNotFoundError(err)

    # Load the pickled model dictionary
    model_dict = joblib.load(saved_model_dir / "spectral_model_dict.joblib")

    # Load the eigenmode basis
    eigenmode_basis = utils.gsp.EigenmodeBasis.load(
        str(saved_model_dir / "eigenmode_basis.joblib"),
        mmap_mode=mmap_mode,
    )

    # Create an instance of the class
    instance = cls(
        eigenmode_basis=eigenmode_basis,
        base_model=DirectNormativeModel(
            spec=model_dict["spec"],
            defaults=model_dict["defaults"],
        ),
    )

    if "model_params" in model_dict:
        instance.model_params = model_dict["model_params"]

    return instance

predict(encoded_query: npt.NDArray[np.floating[Any]], *, spectral_predictions: dict[str, npt.NDArray[np.floating[Any]]] | None = None, test_covariates: pd.DataFrame | None = None, extended: bool = False, model_params: dict[str, Any] | None = None, spectral_coeff_test_data: npt.NDArray[np.floating[Any]] | None = None, n_modes: int | None = None, predict_without: list[str] | None = None) -> NormativePredictions

Predict normative moments (mean, std) for new data using the fitted spectral normative model. Spectral normative modeling can estimate the normative distribution of any variable of interest defined as a spatial query encoded in the latent low-pass graph spectral space.

As such, the predict method requires: - The encoded query(ies) defining the variable(s) of interest.

In addition, the method requires either: - A dataframe of covariates (test_covariates) to be used for inference of a set of spectral predictions that will subsequently be combined to yield the normative predictions for the encoded query(ies). OR - A dictionary of precomputed spectral predictions (spectral_predictions) to be used for efficiently predicting the encoded query(ies).

The precomputed spectral predictions can be obtained using the 'compute_spectral_predictions' function. This is particularly useful when predicting multiple queries or when the same covariate set is used for multiple predictions, as it avoids redundant computations.

Parameters:

Name Type Description Default
encoded_query NDArray[floating[Any]]

np.ndarray Encoded query data defining the normative variable of interest. Can be provided as: - shape = (n_modes) for a single query vector - shape = (n_modes, n_queries) for multiple queries predicted at once

required
spectral_predictions dict[str, NDArray[floating[Any]]] | None

dict | None Optional dictionary of precomputed spectral predictions to use for the prediction. If not provided, test_covariates must be provided instead to compute the spectral predictions. The dictionary should contain: - 'eigenmode_mu_estimates': np.ndarray (n_samples, n_modes) - 'eigenmode_std_estimates': np.ndarray (n_samples, n_modes) - 'rho_estimates': np.ndarray (n_samples, n_covariance_pairs) This can be obtained using the 'compute_spectral_predictions' method.

None
test_covariates DataFrame | None

pd.DataFrame | None DataFrame containing the new covariate data to predict. This must include all specified covariates. Note: covariates listed in predict_without will be ignored and are hence not required.

None
extended bool

bool (default: False) If True, return additional stats such as log-likelihood, centiles, etc. Note that extended predictions require spectral_coeff_test_data to be provided in addition to the covariates.

False
model_params dict[str, Any] | None

dict | None Optional dictionary of model parameters to use. If not provided, the stored parameters from model.fit() will be used.

None
spectral_coeff_test_data NDArray[floating[Any]] | None

np.ndarray | None Optional spectral coefficient of test data for the phenotype being modeled :math:(T_{test} \Psi_{(k)}) \in R^{N_{test} \times k} (only required for extended predictions). Expects a numpy array (n_samples, n_modes)

None
n_modes int | None

int | None Optional number of modes to use for the prediction. If not provided, the number of modes from model_params will be used.

None
predict_without list[str] | None

list[str] | None Optional list of covariate names to ignore during prediction. This can be used to check the effect of removing certain covariates from the model.

None

Returns:

Type Description
NormativePredictions

pd.DataFrame: DataFrame containing the predicted moments (mean, std) for the variable of interest defined by the encoded query.

Source code in src/spectranorm/snm.py
def predict(
    self,
    encoded_query: npt.NDArray[np.floating[Any]],
    *,
    spectral_predictions: dict[str, npt.NDArray[np.floating[Any]]] | None = None,
    test_covariates: pd.DataFrame | None = None,
    extended: bool = False,
    model_params: dict[str, Any] | None = None,
    spectral_coeff_test_data: npt.NDArray[np.floating[Any]] | None = None,
    n_modes: int | None = None,
    predict_without: list[str] | None = None,
) -> NormativePredictions:
    """
    Predict normative moments (mean, std) for new data using the fitted spectral
    normative model.
    Spectral normative modeling can estimate the normative distribution of any
    variable of interest defined as a spatial query encoded in the latent low-pass
    graph spectral space.

    As such, the predict method requires:
        - The encoded query(ies) defining the variable(s) of interest.

    In addition, the method requires either:
        - A dataframe of covariates (test_covariates) to be used for inference
          of a set of spectral predictions that will subsequently be combined
          to yield the normative predictions for the encoded query(ies).
        OR
        - A dictionary of precomputed spectral predictions (spectral_predictions)
          to be used for efficiently predicting the encoded query(ies).

    The precomputed spectral predictions can be obtained using the
    'compute_spectral_predictions' function. This is particularly useful when
    predicting multiple queries or when the same covariate set is used for
    multiple predictions, as it avoids redundant computations.

    Args:
        encoded_query: np.ndarray
            Encoded query data defining the normative variable of interest.
            Can be provided as:
            - shape = (n_modes) for a single query vector
            - shape = (n_modes, n_queries) for multiple queries predicted at once
        spectral_predictions: dict | None
            Optional dictionary of precomputed spectral predictions to use for
            the prediction. If not provided, test_covariates must be provided
            instead to compute the spectral predictions.
            The dictionary should contain:
            - 'eigenmode_mu_estimates': np.ndarray (n_samples, n_modes)
            - 'eigenmode_std_estimates': np.ndarray (n_samples, n_modes)
            - 'rho_estimates': np.ndarray (n_samples, n_covariance_pairs)
            This can be obtained using the 'compute_spectral_predictions' method.
        test_covariates: pd.DataFrame | None
            DataFrame containing the new covariate data to predict.
            This must include all specified covariates.
            Note: covariates listed in predict_without will be ignored and are
            hence not required.
        extended: bool (default: False)
            If True, return additional stats such as log-likelihood, centiles, etc.
            Note that extended predictions require spectral_coeff_test_data to be
            provided in addition to the covariates.
        model_params: dict | None
            Optional dictionary of model parameters to use. If not provided,
            the stored parameters from model.fit() will be used.
        spectral_coeff_test_data: np.ndarray | None
            Optional spectral coefficient of test data for the phenotype being
            modeled :math:`(T_{test} \\Psi_{(k)}) \\in R^{N_{test} \\times k}`
            (only required for extended predictions).
            Expects a numpy array (n_samples, n_modes)
        n_modes: int | None
            Optional number of modes to use for the prediction. If not provided,
            the number of modes from model_params will be used.
        predict_without: list[str] | None
            Optional list of covariate names to ignore during prediction.
            This can be used to check the effect of removing certain covariates
            from the model.

    Returns:
        pd.DataFrame: DataFrame containing the predicted moments (mean, std) for
            the variable of interest defined by the encoded query.
    """
    # Parameters
    if model_params is None:
        model_params = self.model_params

    # Find n_modes
    if n_modes is None:
        n_modes = int(model_params["n_modes"])

    if self.base_model.spec is None:
        err = "The base model is not specified. Cannot predict new data."
        raise ValueError(err)

    if spectral_predictions is None:
        if test_covariates is None:
            err = "Either test_covariates or spectral_predictions must be provided."
            raise ValueError(err)

        # Compute spectral predictions if not provided
        spectral_predictions = self.compute_spectral_predictions(
            test_covariates=test_covariates,
            model_params=model_params,
            n_modes=n_modes,
            predict_without=predict_without,
        )
    elif test_covariates is not None:
        logger.warning(
            "Both test_covariates and spectral_predictions are provided."
            " Ignoring test_covariates and using spectral_predictions.",
        )
        if predict_without is not None:
            logger.warning(
                "predict_without is ignored when spectral_predictions"
                " are provided directly.",
            )

    # Unpack spectral predictions
    self._validate_spectral_predictions(spectral_predictions)
    eigenmode_mu_estimates = spectral_predictions["eigenmode_mu_estimates"]
    eigenmode_std_estimates = spectral_predictions["eigenmode_std_estimates"]
    rho_estimates = spectral_predictions["rho_estimates"]

    # Reformat encoded queries (for efficiency)
    encoded_query = np.asarray(encoded_query[:n_modes])
    encoded_query = encoded_query.reshape(n_modes, -1, order="F")

    # Compute the predictions
    predictions = self._predict_from_spectral_estimates(
        encoded_query=encoded_query,
        eigenmode_mu_estimates=eigenmode_mu_estimates,
        eigenmode_std_estimates=eigenmode_std_estimates,
        rho_estimates=rho_estimates,
        model_params=model_params,
        n_modes=n_modes,
    )

    # Check if extended predictions are requested
    if extended:
        if spectral_coeff_test_data is None:
            err = (
                "Extended predictions require spectral_coeff_test_data"
                " to be provided."
            )
            raise ValueError(err)
        # Add extended statistics to predictions (e.g. centiles, log-loss, etc.)
        predictions.extend_predictions(
            variable_of_interest=spectral_coeff_test_data @ encoded_query,
        )

    return predictions

reduce_model(n_modes: int, *, inplace: bool = False) -> SpectralNormativeModel

Create a reduced spectral normative model using only the first n_modes.

Parameters:

Name Type Description Default
n_modes int

int Number of modes to retain in the reduced model. Must be less than or equal to the current number of modes considered by the model.

required
inplace bool

bool (default: False) If True, modify the current model instance to reduce its modes. If False, return a new SpectralNormativeModel instance with the reduced modes.

False

Returns: SpectralNormativeModel A new SpectralNormativeModel instance with reduced number of modes.

Source code in src/spectranorm/snm.py
def reduce_model(
    self,
    n_modes: int,
    *,
    inplace: bool = False,
) -> SpectralNormativeModel:
    """
    Create a reduced spectral normative model using only the first n_modes.

    Args:
        n_modes: int
            Number of modes to retain in the reduced model. Must be less than or
            equal to the current number of modes considered by the model.
        inplace: bool (default: False)
            If True, modify the current model instance to reduce its modes. If
            False, return a new SpectralNormativeModel instance with the reduced
            modes.
    Returns:
        SpectralNormativeModel
            A new SpectralNormativeModel instance with reduced number of modes.
    """
    if (n_modes > self.eigenmode_basis.n_modes) or (
        hasattr(self, "model_params") and n_modes > self.model_params["n_modes"]
    ):
        available_modes = self.eigenmode_basis.n_modes
        if hasattr(self, "model_params"):
            available_modes = min(available_modes, self.model_params["n_modes"])
        err = f"Cannot reduce to {n_modes} modes, only {available_modes} available."
        raise ValueError(err)

    # Create a reduced eigenbasis
    reduced_eigenbasis = self.eigenmode_basis.reduce(n_modes)

    if inplace:
        return_model = self
        return_model.eigenmode_basis = reduced_eigenbasis
    else:
        return_model = SpectralNormativeModel(
            base_model=self.base_model,
            eigenmode_basis=reduced_eigenbasis,
        )
        return_model.model_params = self.model_params  # Copy model parameters

    # Update model parameters to reflect reduced modes
    if hasattr(self, "model_params"):
        new_model_params: dict[str, Any] = {}
        new_model_params["n_modes"] = n_modes
        new_model_params["sample_size"] = self.model_params["sample_size"]
        new_model_params["direct_model_params"] = self.model_params[
            "direct_model_params"
        ][:n_modes]
        valid_cov_indices = np.where(
            (
                return_model.model_params["sparse_covariance_structure"][:, 0]
                < n_modes
            )
            & (
                return_model.model_params["sparse_covariance_structure"][:, 1]
                < n_modes
            ),
        )[0]
        new_model_params["sparse_covariance_structure"] = return_model.model_params[
            "sparse_covariance_structure"
        ][valid_cov_indices]
        new_model_params["covariance_model_params"] = [
            return_model.model_params["covariance_model_params"][i]
            for i in valid_cov_indices
        ]
        new_model_params["n_params"] = self.model_params.get("n_params", None)
        return_model.model_params = new_model_params

    return return_model

save_model(directory: Path) -> None

Save the fitted spectral normative model to the specified directory.

Parameters:

Name Type Description Default
directory Path

Path Directory to save the fitted model. A subdirectory named "spectral_normative_model" will be created within this directory.

required
Source code in src/spectranorm/snm.py
def save_model(self, directory: Path) -> None:
    """
    Save the fitted spectral normative model to the specified directory.

    Args:
        directory: Path
            Directory to save the fitted model. A subdirectory named
            "spectral_normative_model" will be created within this directory.
    """
    # Prepare the save directory
    directory = Path(directory)
    saved_model_dir = utils.general.prepare_save_directory(
        directory,
        "spectral_normative_model",
    )

    # Save the eigenmode basis separately
    self.eigenmode_basis.save(str(saved_model_dir / "eigenmode_basis.joblib"))

    # Save the model
    model_dict = {
        "spec": self.base_model.spec,
        "defaults": self.base_model.defaults,
    }
    if hasattr(self, "model_params"):
        model_dict["model_params"] = self.model_params
    joblib.dump(model_dict, saved_model_dir / "spectral_model_dict.joblib")

SplineSpec dataclass

Specification for spline basis construction.

Attributes:

Name Type Description
df int

int Degrees of freedom (number of basis functions).

degree int

int Degree of the spline (e.g., 3 for cubic splines).

lower_bound float

float Lower boundary for the spline domain.

upper_bound float

float Upper boundary for the spline domain.

knots list[float] | None

Optional[List[float]] Optional list of internal knot locations within the spline domain (excluding the boundary knots). Must be strictly increasing and contain exactly df - degree - 1 values. If unspecified, then equally spaced quantiles of the input data are used.

Source code in src/spectranorm/snm.py
@dataclass
class SplineSpec:
    """
    Specification for spline basis construction.

    Attributes:
        df: int
            Degrees of freedom (number of basis functions).
        degree: int
            Degree of the spline (e.g., 3 for cubic splines).
        lower_bound: float
            Lower boundary for the spline domain.
        upper_bound: float
            Upper boundary for the spline domain.
        knots: Optional[List[float]]
            Optional list of internal knot locations within the spline domain
            (excluding the boundary knots). Must be strictly increasing and
            contain exactly `df - degree - 1` values. If unspecified, then
            equally spaced quantiles of the input data are used.
    """

    lower_bound: float
    upper_bound: float
    df: int = DEFAULT_SPLINE_DF
    degree: int = DEFAULT_SPLINE_DEGREE
    knots: list[float] | None = None

    # Validation checks for the spline specification.
    def __post_init__(self) -> None:
        # Check that df (degrees of freedom) is greater than degree
        if self.df <= self.degree:
            err = "df (degrees of freedom) must be greater than degree."
            raise ValueError(err)
        # Check that degree is at least 1
        if self.degree < 1:
            err = "degree must be at least 1."
            raise ValueError(err)
        # Check that lower_bound and upper_bound are numeric
        if self.lower_bound >= self.upper_bound:
            err = "lower_bound must be less than upper_bound."
            raise ValueError(err)
        if self.knots is not None:
            if not all(isinstance(k, (int, float)) for k in self.knots):
                err = "All knots must be numeric (int or float)."
                raise TypeError(err)
            # Check if knots are strictly increasing
            if not all(x < y for x, y in zip(self.knots, self.knots[1:])):
                err = "Knots must be strictly increasing."
                raise ValueError(err)
            # Check if knots are within bounds
            if any(k < self.lower_bound or k > self.upper_bound for k in self.knots):
                err = (
                    "All knots must be within the bounds defined by "
                    "lower_bound and upper_bound."
                )
                raise ValueError(err)
            # Check if the number of knots is correct
            if len(self.knots) != (self.df - self.degree - 1):
                err = (
                    f"knots must contain exactly {self.df - self.degree - 1} "
                    f"values, got {len(self.knots)}."
                )
                raise ValueError(err)

    @classmethod
    def create_spline_spec(
        cls,
        values: pd.Series[float],
        df: int = DEFAULT_SPLINE_DF,
        degree: int = DEFAULT_SPLINE_DEGREE,
        knots: list[float] | None = None,
        extrapolation_factor: float = DEFAULT_SPLINE_EXTRAPOLATION_FACTOR,
        lower_bound: float | None = None,
        upper_bound: float | None = None,
    ) -> SplineSpec:
        """
        Create a spline specification from a pandas Series.

        Args:
            values: pd.Series
                The list of input values to make the spline.
            df: int
                Degrees of freedom for the spline (default is 5).
            degree: int
                Degree of the spline (default is 3).
            knots: list[float] | None
                [Optional] List of internal knot locations within the spline domain.
                If None, equally spaced quantiles of the input data are used.
            extrapolation_factor: float, positive, default is 0.1
                [Optional] Factor to extend the lower and upper bounds of the spline
                domain.
            lower_bound: float | None
                [Optional] Lower boundary for the spline domain. If None, it is set to
                `values.min() - extrapolation_factor * (values.max() - values.min())`.
            upper_bound: float | None
                [Optional] Upper boundary for the spline domain. If None, it is set to
                `values.max() + extrapolation_factor * (values.max() - values.min())`.

        Returns:
            SplineSpec
                The created spline specification.
        """
        extrapolation = extrapolation_factor * (values.max() - values.min())
        if lower_bound is None:
            lower_bound = values.min() - extrapolation
        if upper_bound is None:
            upper_bound = values.max() + extrapolation
        if knots is None:
            # Use equally spaced quantiles as knots
            knots = np.linspace(lower_bound, upper_bound, df - degree + 1)[
                1:-1
            ].tolist()
        return cls(
            lower_bound=lower_bound,
            upper_bound=upper_bound,
            df=df,
            degree=degree,
            knots=knots,
        )

create_spline_spec(values: pd.Series[float], df: int = DEFAULT_SPLINE_DF, degree: int = DEFAULT_SPLINE_DEGREE, knots: list[float] | None = None, extrapolation_factor: float = DEFAULT_SPLINE_EXTRAPOLATION_FACTOR, lower_bound: float | None = None, upper_bound: float | None = None) -> SplineSpec classmethod

Create a spline specification from a pandas Series.

Parameters:

Name Type Description Default
values Series[float]

pd.Series The list of input values to make the spline.

required
df int

int Degrees of freedom for the spline (default is 5).

DEFAULT_SPLINE_DF
degree int

int Degree of the spline (default is 3).

DEFAULT_SPLINE_DEGREE
knots list[float] | None

list[float] | None [Optional] List of internal knot locations within the spline domain. If None, equally spaced quantiles of the input data are used.

None
extrapolation_factor float

float, positive, default is 0.1 [Optional] Factor to extend the lower and upper bounds of the spline domain.

DEFAULT_SPLINE_EXTRAPOLATION_FACTOR
lower_bound float | None

float | None [Optional] Lower boundary for the spline domain. If None, it is set to values.min() - extrapolation_factor * (values.max() - values.min()).

None
upper_bound float | None

float | None [Optional] Upper boundary for the spline domain. If None, it is set to values.max() + extrapolation_factor * (values.max() - values.min()).

None

Returns:

Type Description
SplineSpec

SplineSpec The created spline specification.

Source code in src/spectranorm/snm.py
@classmethod
def create_spline_spec(
    cls,
    values: pd.Series[float],
    df: int = DEFAULT_SPLINE_DF,
    degree: int = DEFAULT_SPLINE_DEGREE,
    knots: list[float] | None = None,
    extrapolation_factor: float = DEFAULT_SPLINE_EXTRAPOLATION_FACTOR,
    lower_bound: float | None = None,
    upper_bound: float | None = None,
) -> SplineSpec:
    """
    Create a spline specification from a pandas Series.

    Args:
        values: pd.Series
            The list of input values to make the spline.
        df: int
            Degrees of freedom for the spline (default is 5).
        degree: int
            Degree of the spline (default is 3).
        knots: list[float] | None
            [Optional] List of internal knot locations within the spline domain.
            If None, equally spaced quantiles of the input data are used.
        extrapolation_factor: float, positive, default is 0.1
            [Optional] Factor to extend the lower and upper bounds of the spline
            domain.
        lower_bound: float | None
            [Optional] Lower boundary for the spline domain. If None, it is set to
            `values.min() - extrapolation_factor * (values.max() - values.min())`.
        upper_bound: float | None
            [Optional] Upper boundary for the spline domain. If None, it is set to
            `values.max() + extrapolation_factor * (values.max() - values.min())`.

    Returns:
        SplineSpec
            The created spline specification.
    """
    extrapolation = extrapolation_factor * (values.max() - values.min())
    if lower_bound is None:
        lower_bound = values.min() - extrapolation
    if upper_bound is None:
        upper_bound = values.max() + extrapolation
    if knots is None:
        # Use equally spaced quantiles as knots
        knots = np.linspace(lower_bound, upper_bound, df - degree + 1)[
            1:-1
        ].tolist()
    return cls(
        lower_bound=lower_bound,
        upper_bound=upper_bound,
        df=df,
        degree=degree,
        knots=knots,
    )