transplant_blinks#
- pypillometry.signal.transplant_blinks(sy, window, edge=None, merge_distance=0, min_len=1, max_len=None, n=None, pad=0, mode='additive', shrink=True, missing=None, avoid=None, blink_val=0, source=None, cores=None, seed=None)[source]#
Transplant the blinks of a signal into its clean stretches.
The blinks are found as runs of missing samples (their cores, see
blink_cores()). The blinks that are surrounded by signal are cut out with a window around the core (blink_donors(),extract_blink_artifacts()) and put at random places where the signal is clean (clean_stretches(),place_blink_artifacts(),insert_blink_artifacts()). There, the signal without the blink is known, so a reconstruction of the transplanted blinks can be compared to it (seetransplant_error()).The window around a blink does not only contain its artifact but also the fluctuations of the signal there, which would be transplanted with it. With shrink, they are removed where the blinks do not differ from each other (see
shrink_blink_artifacts()), so the window can be generous. Close to the cores, where the blinks differ, the fluctuations and the noise are still transplanted with the artifacts.- Parameters:
sy (np.ndarray) – pupil data (the blinks are missing samples: NaN or blink_val)
window (int or tuple of int) – number of samples (before, after) the core of a blink that are transplanted with it; should be generous, so that it includes the whole artifact
edge (int, optional) – number of samples at each end of the window for the baseline and the taper (see
extract_blink_artifacts())merge_distance (int) – missing samples separated by at most this many samples are one blink (see
blink_cores())min_len (int) – minimum and maximum number of samples of the cores that are transplanted (see
blink_donors())max_len (int) – minimum and maximum number of samples of the cores that are transplanted (see
blink_donors())n (int, optional) – number of blinks to transplant; default (and at most): as many as fit
pad (int) – number of clean samples to keep free on each side of each window
mode (str) –
"additive"(default) or"multiplicative"(seeextract_blink_artifacts())shrink (bool) – remove the fluctuations of the signal from the artifacts where the blinks do not differ (default); needs clean signal in source to estimate the fluctuations, otherwise the artifacts are used as they are
missing (np.ndarray of bool, optional) – further samples without usable signal (e.g., masked for other reasons); the blinks next to them are not transplanted and nothing is transplanted next to them
avoid (np.ndarray of bool, optional) – samples of sy where no blinks are transplanted, e.g. other artifacts (in addition to the window around all missing samples)
blink_val (float) – “missing value” code in addition to NaN (default 0). The transplanted cores get this value if the signal uses it (and has no NaN), otherwise NaN.
source (np.ndarray, optional) – signal to take the blinks from; default: sy itself. E.g., for transplanting the blinks of a recording into a simulated signal.
cores (np.ndarray, optional) – cores of the blinks to transplant (n x 2, end-exclusive), e.g. a subset of the blinks; default: all blinks (see
blink_cores()). Those that are not surrounded by signal are left out.seed (int or np.random.Generator, optional) – seed of the random numbers
- Return type:
- Returns:
the signal with the transplanted blinks, the clean signal and where the blinks are
Examples
>>> rng = np.random.default_rng(1) >>> y = 1000 + np.cumsum(rng.normal(0, 1, 5000)) >>> y[980:1000] -= np.linspace(0, 500, 20) # a blink at samples 1000-1050 >>> y[1050:1090] -= np.linspace(500, 0, 40) >>> y[1000:1050] = np.nan >>> tr = transplant_blinks(y, window=(100, 150), n=5, seed=1) >>> len(tr.blinks) 5 >>> filled = np.where(np.isnan(tr.signal), 1000, tr.signal) # some reconstruction >>> rmse = tr.error(filled)