Generators¶
Low-level generator functions that produce the actual time series arrays. These are dispatched automatically by the builder via the registry — most users interact with components instead.
Useful as a reference for the function signature contract when writing custom generators.
Dispatch¶
generate_component(component_type: str, n_timesteps: int, rng: np.random.RandomState, **kwargs) -> np.ndarray
¶
Generate a component vector using its registered generator function.
This acts as a dispatcher to the specific generate_... functions based on the
component_type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
component_type
|
str
|
The type of component to generate (e.g., 'constant', 'gaussian_noise').
Must be a key in the |
required |
n_timesteps
|
int
|
The nominal length of the time series context. |
required |
rng
|
RandomState
|
Random number generator instance. |
required |
**kwargs
|
Component-specific parameters, potentially including 'length', which will be passed to the underlying generator function. |
{}
|
Returns:
| Type | Description |
|---|---|
ndarray
|
np.ndarray: The generated component vector. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in xaitimesynth/generators.py
Signal Generators¶
generate_random_walk(n_timesteps: int, step_size: float = 0.1, rng: Optional[np.random.RandomState] = None, length: Optional[int] = None, **kwargs) -> np.ndarray
¶
Generate a random walk time series.
A random walk where each step is drawn from a normal distribution.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_timesteps
|
int
|
The nominal length of the time series context. The actual output
length is determined by the |
required |
step_size
|
float
|
Standard deviation of the random steps taken at each timestep. Defaults to 0.1. |
0.1
|
rng
|
Optional[RandomState]
|
Random number generator instance. If None,
a default |
None
|
length
|
Optional[int]
|
The exact desired length of the output time series array.
If provided, this overrides |
None
|
**kwargs
|
Catches unused parameters passed by TimeSeriesBuilder for compatibility. |
{}
|
Returns:
| Type | Description |
|---|---|
ndarray
|
np.ndarray: A 1D numpy array of the specified length representing a random walk. |
Example
Source code in xaitimesynth/generators.py
generate_gaussian_noise(n_timesteps: int, mu: float = 0.0, sigma: float = 0.1, rng: Optional[np.random.RandomState] = None, length: Optional[int] = None, **kwargs) -> np.ndarray
¶
Generate a time series with Gaussian (normal) noise.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_timesteps
|
int
|
The nominal length of the time series context. The actual output
length is determined by the |
required |
mu
|
float
|
Mean of the Gaussian distribution. Defaults to 0.0. |
0.0
|
sigma
|
float
|
Standard deviation of the Gaussian distribution. Defaults to 0.1. |
0.1
|
rng
|
Optional[RandomState]
|
Random number generator instance. If None,
a default |
None
|
length
|
Optional[int]
|
The exact desired length of the output time series array.
If provided, this overrides |
None
|
**kwargs
|
Catches unused parameters passed by TimeSeriesBuilder for compatibility. |
{}
|
Returns:
| Type | Description |
|---|---|
ndarray
|
np.ndarray: A 1D numpy array of the specified length with values drawn from N(mu, sigma^2). |
Example
Source code in xaitimesynth/generators.py
generate_uniform(n_timesteps: int, low: float = -0.1, high: float = 0.1, rng: Optional[np.random.RandomState] = None, length: Optional[int] = None, **kwargs) -> np.ndarray
¶
Generate a time series with uniform noise.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_timesteps
|
int
|
The nominal length of the time series context. The actual output
length is determined by the |
required |
low
|
float
|
Lower bound of the uniform distribution (inclusive). Defaults to -0.1. |
-0.1
|
high
|
float
|
Upper bound of the uniform distribution (exclusive). Defaults to 0.1. |
0.1
|
rng
|
Optional[RandomState]
|
Random number generator instance. If None,
a default |
None
|
length
|
Optional[int]
|
The exact desired length of the output time series array.
If provided, this overrides |
None
|
**kwargs
|
Catches unused parameters passed by TimeSeriesBuilder for compatibility. |
{}
|
Returns:
| Type | Description |
|---|---|
ndarray
|
np.ndarray: A 1D numpy array of the specified length with values drawn from U(low, high). |
Example
Source code in xaitimesynth/generators.py
generate_red_noise(n_timesteps: int, mean: float = 0.0, std: float = 1.0, phi: float = 0.9, rng: Optional[np.random.RandomState] = None, length: Optional[int] = None, **kwargs) -> np.ndarray
¶
Generate a red noise time series using an AR(1) process.
Red noise exhibits positive autocorrelation, meaning successive values are likely to be close to each other, resulting in smoother, slower fluctuations compared to white noise.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_timesteps
|
int
|
The nominal length of the time series context. The actual output
length is determined by the |
required |
mean
|
float
|
The mean value around which the noise oscillates. Defaults to 0.0. |
0.0
|
std
|
float
|
The overall standard deviation (amplitude) of the noise process. Defaults to 1.0. |
1.0
|
phi
|
float
|
The autocorrelation coefficient (-1 < phi < 1). Controls the "memory" or smoothness. Values closer to 1 result in stronger positive autocorrelation (smoother noise). Defaults to 0.9. |
0.9
|
rng
|
Optional[RandomState]
|
Random number generator instance. If None,
a default |
None
|
length
|
Optional[int]
|
The exact desired length of the output time series array.
If provided, this overrides |
None
|
**kwargs
|
Catches unused parameters passed by TimeSeriesBuilder for compatibility. |
{}
|
Returns:
| Type | Description |
|---|---|
ndarray
|
np.ndarray: A 1D numpy array of the specified length representing red noise. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If phi is not strictly between -1 and 1. |
Example
Source code in xaitimesynth/generators.py
generate_ecg_like(n_timesteps: int, heart_rate: float = 70.0, p_amplitude: float = 0.15, qrs_amplitude: float = 1.0, t_amplitude: float = 0.3, p_width: float = 0.09, qrs_width: float = 0.08, t_width: float = 0.16, pr_interval: float = 0.16, st_segment: float = 0.1, noise_level: float = 0.03, sampling_rate: float = 250.0, hr_variability: float = 0.05, baseline_wander: float = 0.02, rng: Optional[np.random.RandomState] = None, length: Optional[int] = None, **kwargs) -> np.ndarray
¶
Generate a synthetic ECG-like signal with physiologically plausible parameters.
Creates a simulated electrocardiogram (ECG) signal mimicking the characteristic P-QRS-T wave pattern. Allows customization of various ECG components.
Note
This simulation is for illustrative purposes and not medical diagnosis.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_timesteps
|
int
|
The nominal length of the time series context. The actual output
length is determined by the |
required |
heart_rate
|
float
|
Average heart rate in beats per minute (BPM). Defaults to 70.0. |
70.0
|
p_amplitude
|
float
|
Amplitude of the P wave (mV). Defaults to 0.15. |
0.15
|
qrs_amplitude
|
float
|
Amplitude of the QRS complex (mV). Defaults to 1.0. |
1.0
|
t_amplitude
|
float
|
Amplitude of the T wave (mV). Defaults to 0.3. |
0.3
|
p_width
|
float
|
Duration of the P wave (seconds). Defaults to 0.09. |
0.09
|
qrs_width
|
float
|
Duration of the QRS complex (seconds). Defaults to 0.08. |
0.08
|
t_width
|
float
|
Duration of the T wave (seconds). Defaults to 0.16. |
0.16
|
pr_interval
|
float
|
Time from P wave start to QRS start (seconds). Defaults to 0.16. |
0.16
|
st_segment
|
float
|
Duration of the isoelectric ST segment (seconds). Defaults to 0.1. |
0.1
|
noise_level
|
float
|
Standard deviation of additive Gaussian noise (mV). Defaults to 0.03. |
0.03
|
sampling_rate
|
float
|
Sampling frequency in Hz (samples per second). Defaults to 250.0. |
250.0
|
hr_variability
|
float
|
Factor controlling beat-to-beat interval variation (0-1). Defaults to 0.05. |
0.05
|
baseline_wander
|
float
|
Amplitude of low-frequency baseline drift (mV). Defaults to 0.02. |
0.02
|
rng
|
Optional[RandomState]
|
Random number generator instance. If None,
a default |
None
|
length
|
Optional[int]
|
The exact desired length of the output time series array.
If provided, this overrides |
None
|
**kwargs
|
Catches unused parameters passed by TimeSeriesBuilder for compatibility. |
{}
|
Returns:
| Type | Description |
|---|---|
ndarray
|
np.ndarray: A 1D numpy array of the specified length representing the synthetic ECG signal. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If heart_rate or sampling_rate result in a non-positive period. |
Example
Source code in xaitimesynth/generators.py
574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 | |
generate_pseudo_periodic(n_timesteps: int, period: float = 10.0, amplitude: float = 1.0, frequency_std: float = 0.05, amplitude_std: float = 0.1, rng: Optional[np.random.RandomState] = None, length: Optional[int] = None, **kwargs) -> np.ndarray
¶
Generate a pseudo-periodic signal with stochastic amplitude and frequency variations.
Produces a sine wave where the instantaneous frequency and amplitude vary randomly at each timestep. This creates a more realistic background signal than a perfectly periodic sine wave — useful when simulating real-world data where oscillations are not exactly regular (e.g. respiration, heart rate, seasonal economic cycles).
Ported and adapted from the PseudoPeriodic generator in the timesynth package
(MIT License, https://github.com/TimeSynth/TimeSynth).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_timesteps
|
int
|
The nominal length of the time series context. The actual output
length is determined by the |
required |
period
|
float
|
Mean number of timesteps per full cycle. Defaults to 10.0. |
10.0
|
amplitude
|
float
|
Mean amplitude of the oscillation. Defaults to 1.0. |
1.0
|
frequency_std
|
float
|
Standard deviation of per-step frequency perturbations,
expressed as a fraction of the base frequency ( |
0.05
|
amplitude_std
|
float
|
Standard deviation of per-step amplitude perturbations. Larger values produce more variable amplitude. Defaults to 0.1. |
0.1
|
rng
|
Optional[RandomState]
|
Random number generator for reproducibility. If None, a fresh unseeded generator is used. Defaults to None. |
None
|
length
|
Optional[int]
|
The exact desired length of the output array.
If provided, this overrides |
None
|
**kwargs
|
Catches unused parameters passed by TimeSeriesBuilder for compatibility. |
{}
|
Returns:
| Type | Description |
|---|---|
ndarray
|
np.ndarray: A 1D numpy array of the specified length. |
Example
Source code in xaitimesynth/generators.py
Feature Generators¶
generate_peak(n_timesteps: int, amplitude: float = 1.0, width: int = 3, rng: Optional[np.random.RandomState] = None, length: Optional[int] = None, **kwargs) -> np.ndarray
¶
Generate a time series with a single triangular peak centered within the length.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_timesteps
|
int
|
The nominal length of the time series context. The actual output
length is determined by the |
required |
amplitude
|
float
|
The height of the peak relative to the baseline (0). Defaults to 1.0. |
1.0
|
width
|
int
|
The width of the peak's base in timesteps. Should ideally be odd for a single central maximum point. Defaults to 3. |
3
|
rng
|
Optional[RandomState]
|
Random number generator instance. Included for API consistency but unused in this deterministic generator. Defaults to None. |
None
|
length
|
Optional[int]
|
The exact desired length of the output time series array.
If provided, this overrides |
None
|
**kwargs
|
Catches unused parameters passed by TimeSeriesBuilder for compatibility. |
{}
|
Returns:
| Type | Description |
|---|---|
ndarray
|
np.ndarray: A 1D numpy array of the specified length containing a centered peak. |
Example
Source code in xaitimesynth/generators.py
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 | |
generate_trough(n_timesteps: int, amplitude: float = 1.0, width: int = 3, rng: Optional[np.random.RandomState] = None, length: Optional[int] = None, **kwargs) -> np.ndarray
¶
Generate a time series with a single triangular trough centered within the length.
This function simply generates a peak using generate_peak and negates the result.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_timesteps
|
int
|
The nominal length of the time series context. The actual output
length is determined by the |
required |
amplitude
|
float
|
The depth of the trough relative to the baseline (0). The generated peak will have this amplitude, and the result will be negated. Defaults to 1.0. |
1.0
|
width
|
int
|
The width of the trough's base in timesteps. Should ideally be odd for a single central minimum point. Defaults to 3. |
3
|
rng
|
Optional[RandomState]
|
Random number generator instance. Passed to
|
None
|
length
|
Optional[int]
|
The exact desired length of the output time series array.
If provided, this overrides |
None
|
**kwargs
|
Catches unused parameters passed by TimeSeriesBuilder for compatibility. |
{}
|
Returns:
| Type | Description |
|---|---|
ndarray
|
np.ndarray: A 1D numpy array of the specified length containing a centered trough. |
Example
Source code in xaitimesynth/generators.py
generate_gaussian_pulse(n_timesteps: int, amplitude: float = 1.0, width_ratio: float = 1.0, center: float = 0.5, rng: Optional[np.random.RandomState] = None, length: Optional[int] = None, **kwargs) -> np.ndarray
¶
Generate a Gaussian pulse time series.
The pulse follows a Gaussian curve: amplitude * exp(-0.5 * ((x - center) / sigma)^2) The center is automatically snapped to the nearest discrete timestep to ensure exact amplitude and symmetric shape.
Width is controlled using the 6-sigma rule: 99.7% of pulse energy falls within the specified width_ratio, with amplitude dropping to ~1% at boundaries.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_timesteps
|
int
|
The nominal length of the time series context. The actual output
length is determined by the |
required |
amplitude
|
float
|
Peak amplitude of the Gaussian pulse. The maximum value will be exactly this amplitude. Defaults to 1.0. |
1.0
|
width_ratio
|
float
|
Pulse width as fraction of the output length. Using the 6-sigma rule, 99.7% of the pulse energy will be contained within this fraction of the total length, with the pulse amplitude dropping to ~1% at the boundaries. Must be between 0.0 and 1.0. Defaults to 1.0. |
1.0
|
center
|
float
|
Desired peak position within the output length, ranging from 0.0 (start) to 1.0 (end). Will be snapped to the nearest discrete timestep to ensure exact amplitude and symmetric shape. Defaults to 0.5 (middle). |
0.5
|
rng
|
Optional[RandomState]
|
Random number generator instance. Included for API consistency with other generators but unused here. Defaults to None. |
None
|
length
|
Optional[int]
|
The exact desired length of the output time series array.
If provided, this overrides |
None
|
**kwargs
|
Catches unused parameters passed by TimeSeriesBuilder for compatibility. |
{}
|
Returns:
| Type | Description |
|---|---|
ndarray
|
np.ndarray: A 1D numpy array containing the Gaussian pulse with exact amplitude. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If width_ratio is not between 0.0 and 1.0, or if center is not between 0.0 and 1.0. |
Example
Source code in xaitimesynth/generators.py
767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 | |
Versatile Generators¶
generate_constant(n_timesteps: int, value: float = 0.0, rng: Optional[np.random.RandomState] = None, length: Optional[int] = None, **kwargs) -> np.ndarray
¶
Generate a time series with a constant value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_timesteps
|
int
|
The nominal length of the time series context. The actual output
length is determined by the |
required |
value
|
float
|
The constant value to fill the series with. Defaults to 0.0. |
0.0
|
rng
|
Optional[RandomState]
|
Random number generator instance. Included for API consistency with other generators but unused here. Defaults to None. |
None
|
length
|
Optional[int]
|
The exact desired length of the output time series array.
If provided, this overrides |
None
|
**kwargs
|
Catches unused parameters passed by TimeSeriesBuilder for compatibility. |
{}
|
Returns:
| Type | Description |
|---|---|
ndarray
|
np.ndarray: A 1D numpy array of the specified length filled with the constant value. |
Example
Source code in xaitimesynth/generators.py
generate_seasonal(n_timesteps: int, period: int = 10, amplitude: float = 1.0, phase: float = 0.0, rng: Optional[np.random.RandomState] = None, length: Optional[int] = None, **kwargs) -> np.ndarray
¶
Generate a seasonal (sine wave) time series.
The function generates a sine wave with the specified period, amplitude, and phase offset. To ensure the exact amplitude is achieved even for short periods where discrete sampling might not hit the peak values, the signal is automatically scaled when necessary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_timesteps
|
int
|
The nominal length of the time series context. The actual output
length is determined by the |
required |
period
|
int
|
The number of timesteps in one full cycle of the sine wave. Defaults to 10. |
10
|
amplitude
|
float
|
The peak amplitude of the sine wave. The output is guaranteed to have exactly this maximum absolute value. Defaults to 1.0. |
1.0
|
phase
|
float
|
Phase offset in radians applied to the sine wave. Use |
0.0
|
rng
|
Optional[RandomState]
|
Random number generator instance. Included for API consistency but unused in this deterministic generator. Defaults to None. |
None
|
length
|
Optional[int]
|
The exact desired length of the output time series array.
If provided, this overrides |
None
|
**kwargs
|
Catches unused parameters passed by TimeSeriesBuilder for compatibility. |
{}
|
Returns:
| Type | Description |
|---|---|
ndarray
|
np.ndarray: A 1D numpy array of the specified length representing a seasonal pattern. The maximum absolute value is guaranteed to equal the specified amplitude. |
Note
For short periods relative to the signal length, discrete sampling of the continuous sine wave might not capture the exact peak values. This function automatically applies amplitude correction to ensure the specified amplitude is achieved.
Example
Source code in xaitimesynth/generators.py
generate_trend(n_timesteps: int, slope: float = 0.1, endpoints: Optional[List[float]] = None, rng: Optional[np.random.RandomState] = None, length: Optional[int] = None, **kwargs) -> np.ndarray
¶
Generate a linear trend time series.
Creates a series that increases or decreases linearly over time. The trend can be defined either by a slope (starting from 0) or by specifying the start and end values.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_timesteps
|
int
|
The nominal length of the time series context. The actual output
length is determined by the |
required |
slope
|
float
|
The slope of the trend (change per timestep). Used if |
0.1
|
endpoints
|
Optional[List[float]]
|
A list or tuple |
None
|
rng
|
Optional[RandomState]
|
Random number generator instance. Included for API consistency but unused in this deterministic generator. Defaults to None. |
None
|
length
|
Optional[int]
|
The exact desired length of the output time series array.
If provided, this overrides |
None
|
**kwargs
|
Catches unused parameters passed by TimeSeriesBuilder for compatibility. |
{}
|
Returns:
| Type | Description |
|---|---|
ndarray
|
np.ndarray: A 1D numpy array of the specified length representing a linear trend. |
Example
Source code in xaitimesynth/generators.py
generate_manual(n_timesteps: int, values: Optional[np.ndarray] = None, generator: Optional[Callable] = None, rng: Optional[np.random.RandomState] = None, length: Optional[int] = None, **kwargs) -> np.ndarray
¶
Generate a time series from provided values or a custom generator function.
Allows direct specification of the time series values or using a custom function for generation, providing flexibility beyond the standard generators.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_timesteps
|
int
|
The nominal length of the time series context. Used if |
required |
values
|
Optional[ndarray]
|
A numpy array of values to use directly for the
time series. If provided, |
None
|
generator
|
Optional[Callable]
|
A function to generate the values. Ignored if
|
None
|
rng
|
Optional[RandomState]
|
Random number generator instance. Passed to
|
None
|
length
|
Optional[int]
|
The exact desired length of the output time series array.
If provided, this overrides |
None
|
**kwargs
|
Additional keyword arguments passed directly to the |
{}
|
Returns:
| Type | Description |
|---|---|
ndarray
|
np.ndarray: A 1D numpy array of the specified length generated manually. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If neither |
ValueError
|
If provided |
Example
manual_vals = np.array([1, 1, 0, 0, 1])
generate_manual(n_timesteps=5, values=manual_vals)
# array([1, 1, 0, 0, 1])
def custom_gen(n_timesteps, rng, length, **kwargs):
return np.linspace(0, kwargs.get('max_val', 1), length)
rng_ = np.random.RandomState(4)
generate_manual(n_timesteps=10, generator=custom_gen, rng=rng_, length=4, max_val=3)
# array([0., 1., 2., 3.])
Source code in xaitimesynth/generators.py
493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 | |