Fix Typecheck Error - #6
Conversation
…at types about PEP484
|
PEP484でfloatの引数はintも受け入れるとあるので修正しました。 |
|
dist.alive = num_alive
dist.dead = num_death |
|
init_f: np.ndarray | None = None, |
|
list[int]にlistをappendしていたので型ヒントをlist[list[int]]に修正。 |
There was a problem hiding this comment.
Pull request overview
This PR addresses static type-check findings across the distribution utilities and an optional PyTorch helper by tightening type annotations and adjusting a few runtime checks.
Changes:
- Refined type annotations for distribution APIs (e.g.,
Literal[...]forinterpolate, scalar input handling). - Updated PyTorch helper type hints and added an assertion before checkpoint loading.
- Removed (commented out) dynamically-attached attributes on
CumulativeDistfromkaplan_meier_estimator.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 6 comments.
| File | Description |
|---|---|
src/cenreg/pytorch/cjd2F.py |
Adjusts constructor typing for init_f, refines an internal helper type, and adds an assertion before loading a checkpoint. |
src/cenreg/model/nonparametric.py |
Comments out dynamic attribute assignment on CumulativeDist to satisfy type checking. |
src/cenreg/distribution/quantile.py |
Broadens scalar handling to accept int as well as float. |
src/cenreg/distribution/cdf.py |
Tightens interpolate typing via Literal and broadens scalar handling to accept int as well as float. |
Comments suppressed due to low confidence (2)
src/cenreg/distribution/quantile.py:68
QuantileDist.cdfnow acceptsintand wraps scalars withnp.array([y]), which keeps an integer dtype. Becauseretis created vianp.zeros_like(y), it will also be integer-typed and any interpolated probabilities assigned later will be truncated (e.g., 0.3 -> 0). Cast the scalar/array to a float dtype (or createretwithdtype=float) to ensure CDF outputs remain floating-point.
if isinstance(y, int | float):
y = np.array([y])
if self.interpolate == "linear":
# linear interpolation implementation
src/cenreg/distribution/cdf.py:164
CumulativeDist.icdfnow acceptsintquantiles, butnp.array([quantiles])will produce an integer dtype array. Sinceretis later initialized withnp.zeros_like(quantiles), inverse-CDF results can be truncated to integers. Castquantilesto float when normalizing scalar inputs (or initializeretwithdtype=float).
if isinstance(quantiles, int | float):
quantiles = np.array([quantiles])
if np.any(quantiles < 0.0):
raise ValueError("quantiles must be non-negative.")
if np.any(quantiles > 1.0):
raise ValueError("quantiles must be at most 1.0.")
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
…d assertion for positive num_epochs in minimize_mse function
…nd add shape validation
型チェックで指摘されているところを修正しました。