
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "auto_examples/04-interpolation-based/04-plot_isi_advance_usage.py"
.. LINE NUMBERS ARE GIVEN BELOW.

.. only:: html

    .. note::
        :class: sphx-glr-download-link-note

        :ref:`Go to the end <sphx_glr_download_auto_examples_04-interpolation-based_04-plot_isi_advance_usage.py>`
        to download the full example code.

.. rst-class:: sphx-glr-example-title

.. _sphx_glr_auto_examples_04-interpolation-based_04-plot_isi_advance_usage.py:


IntraSiteInterpolation advance usage
====================================

.. GENERATED FROM PYTHON SOURCE LINES 7-9

Global Maximum Balancing
------------------------

.. GENERATED FROM PYTHON SOURCE LINES 9-24

.. code-block:: Python


    # Balance all sites to the single largest class count found anywhere:

    import numpy as np

    from uniharmony.datasets import make_multisite_classification
    from uniharmony.interpolation import IntraSiteInterpolation


    X, y, sites = make_multisite_classification(balance_per_site=[0.3, 0.7])
    isi = IntraSiteInterpolation(balance_strategy="global_max", interpolator="random", random_state=42)

    X_balanced, y_balanced = isi.fit_resample(X, y, sites=sites)
    print(f"Global target count: {isi.target_count_}")





.. rst-class:: sphx-glr-script-out

 .. code-block:: none

    2026-05-18 13:27:57 [info     ] Overall class balance across sites: [np.float64(0.5), np.float64(0.5)]
    2026-05-18 13:27:57 [debug    ] Total Samples to generate 1000
    2026-05-18 13:27:57 [debug    ] Total Samples to generate per site [500 500]
    2026-05-18 13:27:57 [info     ] For site 0
    2026-05-18 13:27:57 [info     ] Generating 500 samples
    2026-05-18 13:27:57 [debug    ] Balance 0.3 for site 0
    2026-05-18 13:27:57 [debug    ] Site 0, site effect strength [3.0, 3.0]
    2026-05-18 13:27:57 [info     ] For site 1
    2026-05-18 13:27:57 [info     ] Generating 500 samples
    2026-05-18 13:27:57 [debug    ] Balance 0.7 for site 1
    2026-05-18 13:27:57 [debug    ] Site 1, site effect strength [3.0, 3.0]
    2026-05-18 13:27:57 [info     ] Generated 1000 samples across 2 sites
    2026-05-18 13:27:57 [info     ] Class distribution: [500 500]
    2026-05-18 13:27:57 [info     ] Site distribution: [500 500]
    2026-05-18 13:27:57 [info     ] [ISI] Starting fit_resample
    2026-05-18 13:27:57 [debug    ] [ISI] N target for global_max strategy = 350
    2026-05-18 13:27:57 [info     ] [ISI] Processing site 0
    2026-05-18 13:27:57 [debug    ] [ISI] For site 0, N target for per_site strategy = 350
    2026-05-18 13:27:57 [info     ] [ISI] Processing site 1
    2026-05-18 13:27:57 [debug    ] [ISI] For site 1, N target for per_site strategy = 350
    Global target count: 350




.. GENERATED FROM PYTHON SOURCE LINES 25-30

Covariates
----------

Stratified Interpolation with Covariates
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

.. GENERATED FROM PYTHON SOURCE LINES 30-43

.. code-block:: Python


    # Preserve demographic distributions while balancing classes.
    # Synthetic samples are interpolated only between participants matching on all covariates:
    rng = np.random.default_rng(54)
    n_samples = 1000
    X, y, sites = make_multisite_classification(n_samples=n_samples, balance_per_site=[0.3, 0.7])
    sex = rng.integers(0, 2, (n_samples, 1))
    age = rng.standard_normal((n_samples, 1)) * 10 + 50
    isi = IntraSiteInterpolation(balance_strategy="per_site", random_state=42)

    X_balanced, y_balanced = isi.fit_resample(
        X, y, sites=sites, categorical_covariate=sex, continuous_covariate=age, n_bins_cont_cov=2
    )  # Age binned with 5 bins




.. rst-class:: sphx-glr-script-out

 .. code-block:: none

    2026-05-18 13:27:57 [info     ] Overall class balance across sites: [np.float64(0.5), np.float64(0.5)]
    2026-05-18 13:27:57 [debug    ] Total Samples to generate 1000
    2026-05-18 13:27:57 [debug    ] Total Samples to generate per site [500 500]
    2026-05-18 13:27:57 [info     ] For site 0
    2026-05-18 13:27:57 [info     ] Generating 500 samples
    2026-05-18 13:27:57 [debug    ] Balance 0.3 for site 0
    2026-05-18 13:27:57 [debug    ] Site 0, site effect strength [3.0, 3.0]
    2026-05-18 13:27:57 [info     ] For site 1
    2026-05-18 13:27:57 [info     ] Generating 500 samples
    2026-05-18 13:27:57 [debug    ] Balance 0.7 for site 1
    2026-05-18 13:27:57 [debug    ] Site 1, site effect strength [3.0, 3.0]
    2026-05-18 13:27:57 [info     ] Generated 1000 samples across 2 sites
    2026-05-18 13:27:57 [info     ] Class distribution: [500 500]
    2026-05-18 13:27:57 [info     ] Site distribution: [500 500]
    2026-05-18 13:27:57 [info     ] [ISI] Starting fit_resample
    2026-05-18 13:27:57 [debug    ] Using 1 categorical covariates
    2026-05-18 13:27:57 [debug    ] No tolerance specified, using exact matching
    2026-05-18 13:27:57 [debug    ] Using 1 continuous covariates with tolerance: [0.]
    2026-05-18 13:27:57 [info     ] [ISI] Processing site 0
    2026-05-18 13:27:57 [debug    ] [ISI] For site 0, N target for per_site strategy = 350
    2026-05-18 13:27:57 [info     ] [ISI] Processing site 1
    2026-05-18 13:27:57 [debug    ] [ISI] For site 1, N target for per_site strategy = 350





.. rst-class:: sphx-glr-timing

   **Total running time of the script:** (0 minutes 1.062 seconds)


.. _sphx_glr_download_auto_examples_04-interpolation-based_04-plot_isi_advance_usage.py:

.. only:: html

  .. container:: sphx-glr-footer sphx-glr-footer-example

    .. container:: sphx-glr-download sphx-glr-download-jupyter

      :download:`Download Jupyter notebook: 04-plot_isi_advance_usage.ipynb <04-plot_isi_advance_usage.ipynb>`

    .. container:: sphx-glr-download sphx-glr-download-python

      :download:`Download Python source code: 04-plot_isi_advance_usage.py <04-plot_isi_advance_usage.py>`

    .. container:: sphx-glr-download sphx-glr-download-zip

      :download:`Download zipped: 04-plot_isi_advance_usage.zip <04-plot_isi_advance_usage.zip>`


.. only:: html

 .. rst-class:: sphx-glr-signature

    `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.github.io>`_
