Resample a list by a given factor. For integer factors, simply repeats the list. For non-integer factors, repeats the whole list for the integer part and randomly samples the fractional part using a deterministic seed. Args: lst: List to resample. factor: Resampling
(lst: List[T], factor: float, seed: int = 42)
| 463 | |
| 464 | |
| 465 | def _resample(lst: List[T], factor: float, seed: int = 42) -> List[T]: |
| 466 | """Resample a list by a given factor. |
| 467 | |
| 468 | For integer factors, simply repeats the list. For non-integer |
| 469 | factors, repeats the whole list for the integer part and randomly |
| 470 | samples the fractional part using a deterministic seed. |
| 471 | |
| 472 | Args: |
| 473 | lst: List to resample. |
| 474 | factor: Resampling factor. Must be positive. |
| 475 | - factor=1.0: returns original list |
| 476 | - factor=2.0: returns list repeated twice |
| 477 | - factor=1.5: returns list once + 50% random sample |
| 478 | seed: Random seed for reproducible sampling. Default: 42. |
| 479 | |
| 480 | Returns: |
| 481 | Resampled list. Returns empty list if input is empty. |
| 482 | |
| 483 | Raises: |
| 484 | ValueError: If factor <= 0. |
| 485 | |
| 486 | Examples: |
| 487 | >>> _resample([1, 2, 3], 2.0) |
| 488 | [1, 2, 3, 1, 2, 3] |
| 489 | >>> _resample([1, 2, 3], 1.5, seed=42) |
| 490 | [1, 2, 3, 2, 1] # Deterministic with same seed |
| 491 | """ |
| 492 | if factor <= 0: |
| 493 | raise ValueError(f"Resampling factor must be positive, got {factor}") |
| 494 | |
| 495 | if not lst: |
| 496 | return [] # Empty list resampled is still empty |
| 497 | |
| 498 | # Integer factor: simple repetition |
| 499 | if factor.is_integer(): |
| 500 | return lst * int(factor) |
| 501 | |
| 502 | # Non-integer factor: repeat whole + sample fractional part |
| 503 | result = [] |
| 504 | |
| 505 | # Add full copies for the integer part |
| 506 | int_part = int(factor) |
| 507 | for _ in range(int_part): |
| 508 | result.extend(lst) |
| 509 | |
| 510 | # Add random sample for the fractional part (deterministic) |
| 511 | frac_part = factor - int_part |
| 512 | num_samples = int(frac_part * len(lst)) |
| 513 | if num_samples > 0: |
| 514 | # Use local Random instance for reproducibility |
| 515 | rng = random.Random(seed) |
| 516 | residual = rng.sample(lst, num_samples) |
| 517 | result.extend(residual) |
| 518 | |
| 519 | return result |
searching dependent graphs…