Validation¶
Every generator validates its arguments before drawing anything, so an invalid call fails immediately rather than returning quietly wrong data.
All errors derive from ValidationError, which derives from ValueError — so
except ValueError catches them all, and except ValidationError catches only
this package's.
from gen_surv import generate
from gen_surv.validation import ValidationError
try:
generate(model="cphm", n=-1, beta=0.5, covariate_range=2.0,
model_cens="uniform", cens_par=1.0)
except ValidationError as exc:
print(exc)
validation
¶
Input validation utilities.
This module unifies the low-level validation helpers and the higher-level checks used by the data generators.
ValidationError
¶
Bases: ValueError
Base class for input validation errors.
PositiveIntegerError
¶
Bases: ValidationError
Raised when a value expected to be a positive integer is invalid.
Source code in gen_surv/validation.py
PositiveValueError
¶
Bases: ValidationError
Raised when a value expected to be positive is invalid.
Source code in gen_surv/validation.py
ChoiceError
¶
Bases: ValidationError
Raised when a value is not among an allowed set of choices.
Source code in gen_surv/validation.py
LengthError
¶
Bases: ValidationError
Raised when a sequence does not have the expected length.
Source code in gen_surv/validation.py
NumericSequenceError
¶
Bases: ValidationError
Raised when a sequence contains non-numeric elements.
Source code in gen_surv/validation.py
PositiveSequenceError
¶
Bases: ValidationError
Raised when a sequence contains non-positive elements.
Source code in gen_surv/validation.py
ListOfListsError
¶
Bases: ValidationError
Raised when a value is not a list of lists.
Source code in gen_surv/validation.py
ParameterError
¶
Bases: ValidationError
Raised when a parameter falls outside its allowed range.
Source code in gen_surv/validation.py
ensure_positive_int
¶
Ensure value is a positive integer.
ensure_finite
¶
Ensure value is a real number that is neither NaN nor infinite.
Every comparison with NaN is false, so a check written as value <= 0
silently admits it, and inf > 0 is true. Both then reach NumPy, where
they either surface as an unrelated error -- OverflowError: high - low
range exceeds valid bounds from a uniform draw -- or produce a frame
quietly full of NaN. Rejecting them here is what makes the message name the
argument the caller got wrong.
Source code in gen_surv/validation.py
ensure_positive
¶
Ensure value is a finite positive number.
Source code in gen_surv/validation.py
ensure_probability
¶
Ensure value lies in the closed interval [0, 1].
ensure_in_choices
¶
Ensure value is one of the allowed options.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
str
|
Value provided by the user. |
required |
name
|
str
|
Name of the argument being validated. Used in error messages. |
required |
choices
|
Iterable[str]
|
Iterable of valid string options. |
required |
Raises:
| Type | Description |
|---|---|
ChoiceError
|
If |
Source code in gen_surv/validation.py
ensure_sequence_length
¶
Ensure a sequence has an expected number of elements.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
seq
|
Sequence[Any]
|
Sequence-like object (e.g., |
required |
length
|
int
|
Required number of elements in |
required |
name
|
str
|
Parameter name for error reporting. |
required |
Raises:
| Type | Description |
|---|---|
LengthError
|
If |
Source code in gen_surv/validation.py
ensure_numeric_sequence
¶
Validate that a sequence consists solely of numbers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
seq
|
Sequence[Any]
|
Sequence whose elements should all be |
required |
name
|
str
|
Parameter name for error reporting. |
required |
Raises:
| Type | Description |
|---|---|
NumericSequenceError
|
If any element cannot be interpreted as a numeric value. |
Source code in gen_surv/validation.py
ensure_positive_sequence
¶
Validate that a sequence contains only positive numbers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
seq
|
Sequence[float]
|
Sequence of numeric values. |
required |
name
|
str
|
Parameter name for error reporting. |
required |
Raises:
| Type | Description |
|---|---|
PositiveSequenceError
|
If any element is less than or equal to zero. The offending value and its index are reported in the error message. |
Source code in gen_surv/validation.py
ensure_censoring_model
¶
Validate that the censoring model is supported.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_cens
|
str
|
Censoring model name provided by the user. |
required |
Raises:
| Type | Description |
|---|---|
ChoiceError
|
If |
Source code in gen_surv/validation.py
validate_gen_cphm_inputs
¶
validate_gen_cphm_inputs(
n: int,
model_cens: str,
cens_par: float,
covariate_range: float,
beta: float | None = None,
) -> None
Validate input parameters for CPHM data generation.
beta is a log hazard ratio and may be any sign, so no positivity check
reaches it. It still has to be a finite number: NaN propagates into every
drawn time, and the frame comes back the right shape and entirely NaN.
Source code in gen_surv/validation.py
validate_gen_cmm_inputs
¶
validate_gen_cmm_inputs(
n: int,
model_cens: str,
cens_par: float,
beta: Sequence[float],
covariate_range: float,
rate: Sequence[float],
) -> None
Validate inputs for generating CMM (Continuous-Time Markov Model) data.
Source code in gen_surv/validation.py
validate_gen_tdcm_inputs
¶
validate_gen_tdcm_inputs(
n: int,
dist: str,
corr: float,
dist_par: Sequence[float],
model_cens: str,
cens_par: float,
beta: Sequence[float],
lam: float,
) -> None
Validate inputs for generating TDCM (Time-Dependent Covariate Model) data.
Source code in gen_surv/validation.py
validate_gen_thmm_inputs
¶
validate_gen_thmm_inputs(
n: int,
model_cens: str,
cens_par: float,
beta: Sequence[float],
covariate_range: float,
rate: Sequence[float],
) -> None
Validate inputs for generating THMM (Time-Homogeneous Markov Model) data.
Source code in gen_surv/validation.py
validate_dg_biv_inputs
¶
Validate inputs for the :func:sample_bivariate_distribution helper.
Source code in gen_surv/validation.py
validate_gen_aft_log_normal_inputs
¶
validate_gen_aft_log_normal_inputs(
n: int,
beta: Sequence[float],
sigma: float,
model_cens: str,
cens_par: float,
) -> None
Validate parameters for the log-normal AFT generator.
Source code in gen_surv/validation.py
validate_gen_aft_weibull_inputs
¶
validate_gen_aft_weibull_inputs(
n: int,
beta: Sequence[float],
shape: float,
scale: float,
model_cens: str,
cens_par: float,
) -> None
Validate parameters for the Weibull AFT generator.
Source code in gen_surv/validation.py
validate_gen_aft_log_logistic_inputs
¶
validate_gen_aft_log_logistic_inputs(
n: int,
beta: Sequence[float],
shape: float,
scale: float,
model_cens: str,
cens_par: float,
) -> None
Validate parameters for the log-logistic AFT generator.
Source code in gen_surv/validation.py
validate_competing_risks_inputs
¶
validate_competing_risks_inputs(
n: int,
n_risks: int,
baseline_hazards: Sequence[float] | None,
betas: Sequence[Sequence[float]] | None,
covariate_dist: str,
max_time: float | None,
model_cens: str,
cens_par: float,
) -> None
Validate parameters for competing risks data generation.
Source code in gen_surv/validation.py
validate_piecewise_params
¶
Validate breakpoint and hazard rate sequences.
Source code in gen_surv/validation.py
validate_gen_piecewise_inputs
¶
validate_gen_piecewise_inputs(
n: int,
breakpoints: Sequence[float],
hazard_rates: Sequence[float],
n_covariates: int,
model_cens: str,
cens_par: float,
covariate_dist: str,
) -> None
Validate parameters for :func:gen_piecewise_exponential.
Source code in gen_surv/validation.py
validate_gen_mixture_inputs
¶
validate_gen_mixture_inputs(
n: int,
cure_fraction: float,
baseline_hazard: float,
n_covariates: int,
model_cens: str,
cens_par: float,
max_time: float | None,
covariate_dist: str,
) -> None
Validate parameters for :func:gen_mixture_cure.
Source code in gen_surv/validation.py
validate_gen_recurrent_events_inputs
¶
validate_gen_recurrent_events_inputs(
n: int,
process: str,
baseline: object,
baseline_params: dict[str, float] | None,
n_covariates: int,
stratum_effects: Sequence[float] | None,
max_events: int | None,
followup_time: float,
model_cens: str,
cens_par: float,
) -> None
Validate parameters for :func:gen_surv.recurrent.gen_recurrent_events.