Skip to content

downscale.base

Downscaler interface and the generic proportional implementation.

Region→country and country→bus are the same operation: distribute a coarse value over its finer members proportionally to proxy shares (population/GDP/SSP activity, ...). The shares are built by :mod:iampypsa.downscale.proxy.

Downscaler

Bases: ABC

Define how coarse values are distributed to finer units.

Source code in src/iampypsa/downscale/base.py
class Downscaler(ABC):
    """Define how coarse values are distributed to finer units."""

    @abstractmethod
    def downscale(self, coarse: pd.DataFrame, proxy_shares: pd.DataFrame) -> pd.DataFrame:
        """Distribute coarse values to finer units using proxy shares."""
        ...

downscale(coarse, proxy_shares) abstractmethod

Distribute coarse values to finer units using proxy shares.

Source code in src/iampypsa/downscale/base.py
@abstractmethod
def downscale(self, coarse: pd.DataFrame, proxy_shares: pd.DataFrame) -> pd.DataFrame:
    """Distribute coarse values to finer units using proxy shares."""
    ...

ProportionalDownscaler

Bases: Downscaler

Distribute coarse values to finer units in proportion to proxy shares.

Source code in src/iampypsa/downscale/base.py
class ProportionalDownscaler(Downscaler):
    """Distribute coarse values to finer units in proportion to proxy shares."""

    def downscale(
        self,
        coarse: pd.DataFrame,
        proxy_shares: pd.DataFrame,
        *,
        coarse_id: str = "region",
        fine_id: str = "fine_id",
        share_col: str = "share",
        value_col: str = "value",
    ) -> pd.DataFrame:
        """Multiply each coarse value by its members' proxy shares.

        ``proxy_shares`` has columns ``[coarse_id, fine_id, share]`` (shares summing to 1
        within each ``coarse_id``); returns the coarse frame re-keyed to ``fine_id`` with
        ``value`` split by share.
        """
        merged = coarse.merge(proxy_shares, on=coarse_id, how="left")
        merged[value_col] = merged[value_col] * merged[share_col]
        return merged.drop(columns=[coarse_id, share_col]).rename(columns={fine_id: coarse_id})

downscale(coarse, proxy_shares, *, coarse_id='region', fine_id='fine_id', share_col='share', value_col='value')

Multiply each coarse value by its members' proxy shares.

proxy_shares has columns [coarse_id, fine_id, share] (shares summing to 1 within each coarse_id); returns the coarse frame re-keyed to fine_id with value split by share.

Source code in src/iampypsa/downscale/base.py
def downscale(
    self,
    coarse: pd.DataFrame,
    proxy_shares: pd.DataFrame,
    *,
    coarse_id: str = "region",
    fine_id: str = "fine_id",
    share_col: str = "share",
    value_col: str = "value",
) -> pd.DataFrame:
    """Multiply each coarse value by its members' proxy shares.

    ``proxy_shares`` has columns ``[coarse_id, fine_id, share]`` (shares summing to 1
    within each ``coarse_id``); returns the coarse frame re-keyed to ``fine_id`` with
    ``value`` split by share.
    """
    merged = coarse.merge(proxy_shares, on=coarse_id, how="left")
    merged[value_col] = merged[value_col] * merged[share_col]
    return merged.drop(columns=[coarse_id, share_col]).rename(columns={fine_id: coarse_id})