Make pdf_transfer_1d more readable

This commit is contained in:
Andras Schmelczer 2024-07-08 07:39:01 +01:00
parent 51360171c4
commit a7cdd22619
No known key found for this signature in database
GPG key ID: FC8F2C3D3D1A718C

View file

@ -1,13 +1,24 @@
import numpy as np
def pdf_transfer_1d(pX: np.ndarray, pY: np.ndarray) -> np.ndarray:
PX = np.cumsum(pX + np.finfo(float).eps)
PX /= PX[-1]
EPSILON = np.finfo(float).eps
PY = np.cumsum(pY + np.finfo(float).eps)
PY /= PY[-1]
f = np.interp(PX, PY, np.arange(len(pX)))
def pdf_transfer_1d(
source_y: np.ndarray, target_y: np.ndarray, target_x: np.ndarray = None
) -> np.ndarray:
"""
return the best source_x-es
"""
cumulative_source = np.cumsum(source_y).astype(np.float64)
cumulative_source /= cumulative_source[-1]
return f
cumulative_target = np.cumsum(target_y).astype(np.float64)
cumulative_target /= cumulative_target[-1]
cumulative_transfered = np.interp(
cumulative_source,
cumulative_target,
np.arange(len(cumulative_source)) if target_x is None else target_x,
)
return cumulative_transfered