Skip to content

style: add type hinting to variational distributions#261

Open
leahaeusel wants to merge 2 commits intoqueens-py:mainfrom
leahaeusel:add-type-hinting-to-variational-distributions
Open

style: add type hinting to variational distributions#261
leahaeusel wants to merge 2 commits intoqueens-py:mainfrom
leahaeusel:add-type-hinting-to-variational-distributions

Conversation

@leahaeusel
Copy link
Copy Markdown
Member

@leahaeusel leahaeusel commented Dec 8, 2025

Description and Context:
What and Why?

This PR adds type hinting to our variational distributions.

I have introduced new type aliases to indicate the shapes of numpy arrays, since the docstrings provided this information for some of the variables. In some cases, the shapes were outdated, though, as I realized when I checked the shapes with our tests. In my opinion, it would be really helpful to eventually replace all or most of the np.ndarrays in our code base with these shape-indicating aliases.

FYI: I had to adapt FullRankNormal.reconstruct_distribution_parameters() because mypy doesn't like functions with a varying number of returned variables.

Related Issues and Pull Requests

Interested Parties

@gilrrei

Note: More information on the merge request procedure in QUEENS can be found in the Submit a pull request section in the CONTRIBUTING.md file.

Copy link
Copy Markdown

Copilot AI left a comment

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 adds comprehensive type hinting to the variational distributions module in QUEENS, introducing two new type aliases (Array1xN and ArrayNxM) for better shape indication in numpy arrays. The PR also refactors FullRankNormal.reconstruct_distribution_parameters() to satisfy mypy's requirement for consistent return types by creating a separate method for cases where the Cholesky decomposition is needed, and enables mypy checking for the variational distributions module by removing it from exclusion lists.

Key Changes

  • Introduced new type aliases Array1xN and ArrayNxM in src/queens/utils/type_hinting.py to indicate array shapes
  • Added type hints throughout the variational distributions hierarchy including the base class and all concrete implementations
  • Split FullRankNormal.reconstruct_distribution_parameters() into two methods to resolve mypy's varying return count issue

Reviewed changes

Copilot reviewed 10 out of 10 changed files in this pull request and generated 27 comments.

Show a summary per file
File Description
src/queens/utils/type_hinting.py New file introducing Array1xN and ArrayNxM type aliases for shaped numpy arrays
src/queens/variational_distributions/_variational_distribution.py Added type hints to base class including Array1xN for variational parameters and proper return types
src/queens/variational_distributions/particle.py Added type hints to all methods, updated docstrings, and modified initialization to pass n_parameters
src/queens/variational_distributions/mixture_model.py Added type hints, improved variable naming (logpdf_lst, parameters), and initialized FIM properly
src/queens/variational_distributions/mean_field_normal.py Added type hints to all methods and updated docstrings for consistency
src/queens/variational_distributions/full_rank_normal.py Added type hints, split reconstruct_distribution_parameters() into two methods for mypy compatibility
src/queens/variational_distributions/joint.py Added type hints, fixed logpdf initialization to use proper numpy array instead of scalar
src/queens/variational_distributions/__init__.py Added type hints to __getattr__ function and imported base class for type checking
pyproject.toml Removed variational_distributions from mypy exclusion list to enable type checking
.pre-commit-config.yaml Removed variational_distributions from mypy exclusion pattern in pre-commit hooks

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/queens/variational_distributions/particle.py Outdated
Comment thread src/queens/variational_distributions/full_rank_normal.py Outdated
Comment thread src/queens/variational_distributions/full_rank_normal.py Outdated
Comment thread src/queens/variational_distributions/joint.py Outdated
Comment thread src/queens/variational_distributions/mixture_model.py Outdated
Comment thread src/queens/variational_distributions/mean_field_normal.py Outdated
Comment thread src/queens/variational_distributions/full_rank_normal.py Outdated
Comment thread src/queens/variational_distributions/joint.py Outdated
Comment thread src/queens/variational_distributions/joint.py Outdated
Comment thread src/queens/variational_distributions/particle.py Outdated
@leahaeusel leahaeusel force-pushed the add-type-hinting-to-variational-distributions branch from b16cf83 to b69c2b1 Compare December 9, 2025 08:22
@leahaeusel leahaeusel requested a review from Copilot December 9, 2025 08:23
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 10 out of 10 changed files in this pull request and generated 5 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/queens/variational_distributions/joint.py Outdated
Comment thread src/queens/utils/type_hinting.py Outdated
Comment thread src/queens/variational_distributions/mixture_model.py Outdated
Comment thread src/queens/variational_distributions/full_rank_normal.py Outdated
Comment thread src/queens/variational_distributions/_variational_distribution.py Outdated
style: add type hinting to variational distributions
@leahaeusel leahaeusel force-pushed the add-type-hinting-to-variational-distributions branch from b69c2b1 to 8dec669 Compare December 9, 2025 08:43
@leahaeusel leahaeusel marked this pull request as ready for review December 9, 2025 08:59
Copy link
Copy Markdown
Member

@gilrrei gilrrei left a comment

Choose a reason for hiding this comment

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

I think the return statements could be more precise :)

Comment thread src/queens/variational_distributions/_variational_distribution.py Outdated
Comment thread src/queens/variational_distributions/_variational_distribution.py Outdated
Comment thread src/queens/variational_distributions/_variational_distribution.py Outdated
@leahaeusel leahaeusel marked this pull request as draft December 9, 2025 12:09
@leahaeusel leahaeusel force-pushed the add-type-hinting-to-variational-distributions branch 3 times, most recently from d161ffc to 87ad5eb Compare December 10, 2025 12:36
@leahaeusel leahaeusel requested a review from Copilot December 10, 2025 12:37
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 9 out of 9 changed files in this pull request and generated 23 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/queens/variational_distributions/mixture_model.py
Comment thread src/queens/variational_distributions/joint.py Outdated
Comment thread src/queens/variational_distributions/mixture_model.py Outdated
Comment thread src/queens/variational_distributions/joint.py
Comment thread src/queens/variational_distributions/mean_field_normal.py
Comment thread src/queens/variational_distributions/mean_field_normal.py
Comment thread src/queens/variational_distributions/mixture_model.py Outdated
Comment thread src/queens/variational_distributions/_variational_distribution.py Outdated
Comment thread src/queens/variational_distributions/_variational_distribution.py
Comment thread src/queens/variational_distributions/full_rank_normal.py
@leahaeusel leahaeusel force-pushed the add-type-hinting-to-variational-distributions branch from 87ad5eb to 79692a8 Compare December 10, 2025 13:20
@leahaeusel leahaeusel marked this pull request as ready for review December 10, 2025 15:24
Comment thread src/queens/variational_distributions/_variational_distribution.py
Copy link
Copy Markdown
Contributor

@danielwolff1 danielwolff1 left a comment

Choose a reason for hiding this comment

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

Thanks a lot for providing the additional type hints in the variational distribution classes, that really must have a been quite some effort and sorry for taking so long to review it. I left a few comments to mark points where I'm unsure about because I'm not too familiar with variational distributions 🙈

Comment on lines +35 to +39
NDimsComponent = TypeVar("NDimsComponent", bound=int)
V = TypeVar("V", bound=Variational)
ArrayNSamplesXNDimsComponent: TypeAlias = np.ndarray[ # pylint: disable=invalid-name
tuple[NSamples, NDimsComponent], np.dtype[np.floating]
]
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.

since I am not familiar with the math here: Is this something that is super specific to a joint distribution or would it make sense to move this to _variational_distribution as well?

Comment on lines +253 to +254
within one sample. (Third dimension is empty and just added to keep slices two
dimensional.)
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.

Is the remark here about the third dimension still true? 🤔
Just wondering because then the typing annotation would technically not be correct, right, since we define ArrayNSamplesXNDims: TypeAlias = np.ndarray[tuple[NSamples, NDims], np.dtype[np.floating]], i.e., as an array with exactly two dimensions.

Gradients of the log-pdf w.r.t. the sample *x*. The first dimension of the array
corresponds to the different samples. The second dimension to different dimensions
within one sample. (Third dimension is empty and just added to keep slices
two-dimensional.)
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.

Same question as below / above (I never know in which order GitHub will display my comments 🙈): Is the remark here about the third dimension still true?

Variational,
)

NComponents = TypeVar("NComponents", bound=int)
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.

Is this from the meaning fundamentally different from the NDimsComponent type variable defined in joint.py? Or could they be unified and moved to _variational_distribution.py?

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.

5 participants