diff --git a/src/histogram_transfer/pdf_transfer_1d.py b/src/histogram_transfer/pdf_transfer_1d.py index 71e0925..e551c37 100644 --- a/src/histogram_transfer/pdf_transfer_1d.py +++ b/src/histogram_transfer/pdf_transfer_1d.py @@ -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