Skip to content

Fix Typecheck Error - #6

Merged
SeiichiroYoshioka merged 9 commits into
mainfrom
fix/typecheck
May 26, 2026
Merged

SeiichiroYoshioka merged 9 commits into
mainfrom
fix/typecheck

Conversation

@SeiichiroYoshioka

@SeiichiroYoshioka SeiichiroYoshioka commented May 26, 2026

Copy link
Copy Markdown
Contributor

型チェックで指摘されているところを修正しました。

@SeiichiroYoshioka

Copy link
Copy Markdown
Contributor Author

PEP484でfloatの引数はintも受け入れるとあるので修正しました。

@SeiichiroYoshioka

Copy link
Copy Markdown
Contributor Author

src/cenreg/model/nonparametric.py で下記の変数がクラス属性として定義されてないのに代入されてました。
使ってなさそうなのでとりあえずコメントアウトしました。

dist.alive = num_alive
dist.dead = num_death

@SeiichiroYoshioka

Copy link
Copy Markdown
Contributor Author

cenreg/pytorch/cjd2F.py で下記の変数がNoneも受け取りうる一方で変数の型チェックなしにnumpy arrayの属性を参照してました。notebookの方のユースケースを見ると初期値をはめていたのでとりあえず必須でかつnumpy arrayを受け取るように修正しました。

init_f: np.ndarray | None = None,

@SeiichiroYoshioka

Copy link
Copy Markdown
Contributor Author

list[int]にlistをappendしていたので型ヒントをlist[list[int]]に修正。
Noneかどうかを確認せずに呼び出している箇所を修正。

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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[...] for interpolate, scalar input handling).
  • Updated PyTorch helper type hints and added an assertion before checkpoint loading.
  • Removed (commented out) dynamically-attached attributes on CumulativeDist from kaplan_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.cdf now accepts int and wraps scalars with np.array([y]), which keeps an integer dtype. Because ret is created via np.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 create ret with dtype=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.icdf now accepts int quantiles, but np.array([quantiles]) will produce an integer dtype array. Since ret is later initialized with np.zeros_like(quantiles), inverse-CDF results can be truncated to integers. Cast quantiles to float when normalizing scalar inputs (or initialize ret with dtype=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.

Comment thread src/cenreg/distribution/cdf.py
Comment thread src/cenreg/model/nonparametric.py
Comment thread src/cenreg/pytorch/cjd2F.py
Comment thread src/cenreg/pytorch/cjd2F.py
Comment thread src/cenreg/distribution/cdf.py Outdated
Comment thread src/cenreg/distribution/quantile.py
…d assertion for positive num_epochs in minimize_mse function
@SeiichiroYoshioka SeiichiroYoshioka self-assigned this May 26, 2026
@SeiichiroYoshioka
SeiichiroYoshioka marked this pull request as ready for review May 26, 2026 04:42
@SeiichiroYoshioka
SeiichiroYoshioka requested a review from yanagis May 26, 2026 04:47
Comment thread src/cenreg/pytorch/cjd2F.py

@yanagis yanagis left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I approve.

@SeiichiroYoshioka
SeiichiroYoshioka merged commit 002733c into main May 26, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants