Skip to content

Add ConstrainedPrior#218

Closed
thomasckng wants to merge 56 commits intojim-devfrom
add-prior-constraints
Closed

Add ConstrainedPrior#218
thomasckng wants to merge 56 commits intojim-devfrom
add-prior-constraints

Conversation

@thomasckng
Copy link
Collaborator

@thomasckng thomasckng commented May 9, 2025

Summary by CodeRabbit

  • New Features
    • Introduced a new constrained prior class, allowing users to define priors with custom parameter constraints.
  • Bug Fixes
    • Improved input validation and error handling for initial position sampling, ensuring invalid or NaN values are caught early.
  • Documentation
    • Enhanced and standardized documentation for all prior distribution classes, clarifying parameter domains and behaviors.
  • Refactor
    • Simplified and streamlined initialization logic for sampling and likelihood maximization, improving reliability and maintainability.
  • Tests
    • Added comprehensive tests for the new constrained prior class, including multi-dimensional and constraint cases, and improved assertions for prior sampling and log-probability calculations.

@coderabbitai
Copy link

coderabbitai bot commented May 9, 2025

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Walkthrough

This update introduces a new FullRangePrior class for constrained prior sampling, standardizes and expands docstrings across prior classes, and simplifies initial position sampling logic in both the sampler and likelihood maximization routines. Transform classes are refactored to remove unnecessary normalization parameters. Unit tests are enhanced for finiteness, range, and new constraint handling.

Changes

File(s) Change Summary
src/jimgw/prior.py Added FullRangePrior for constrained prior sampling; improved and standardized docstrings for all prior classes; added/clarified xmin/xmax attributes; refactored constructors to use superclass initialization; added constraints logic and sample filtering for new class.
src/jimgw/jim.py, src/jimgw/single_event/likelihood.py Simplified logic for initial position sampling: replaced iterative finite-checking with direct vectorized sampling and validation; added explicit shape and NaN checks for provided initial positions.
src/jimgw/single_event/transforms.py Removed normalization parameters (tc_min, tc_max, dL_min, dL_max) from transform constructors; updated transforms to map variables directly without bounded-to-unbounded conversion; changed output variable names to reflect direct mappings.
test/unit/test_prior.py Added utility functions for finiteness and range assertions; updated all prior tests to use these utilities; added comprehensive tests for FullRangePrior (1D and 2D cases), including constraint satisfaction, log probability checks, and JIT compatibility.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant FullRangePrior
    participant BasePrior

    User->>FullRangePrior: sample(n)
    loop until n valid samples
        FullRangePrior->>BasePrior: sample(batch_size)
        FullRangePrior->>FullRangePrior: eval_constraints(samples)
        FullRangePrior->>User: return valid samples
    end

    User->>FullRangePrior: log_prob(x)
    FullRangePrior->>FullRangePrior: eval_constraints(x)
    alt constraints satisfied
        FullRangePrior->>BasePrior: log_prob(x)
        FullRangePrior->>User: return log_prob
    else constraints violated
        FullRangePrior->>User: return -inf
    end
Loading

Suggested labels

enhancement

Suggested reviewers

  • kazewong
  • tsunhopang
  • CharmaineWONG2

Poem

In the warren of priors, constraints now appear,
With FullRangePrior keeping boundaries clear.
Transformations are simple, no bounds to unbind,
Tests hop through ranges, no NaN left behind.
🐇 Code grows more robust, as springtime draws near!


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@kazewong
Copy link
Owner

I am looking at this right now but FYI this is not in line with how the prior system is designed, and I don't think this PR will not be merged considering the direction this is going.

thomasckng added 20 commits May 12, 2025 10:06
…anceToSNRWeightedDistanceTransform to remove bound to unbound
…anceToSNRWeightedDistanceTransform to remove unbounded suffix from name mappings
…ccept SequentialTransformPrior and handle constraints more effectively
…ikelihoodFD classes for improved readability
… handling; add comprehensive tests for 1D and 2D scenarios
@thomasckng
Copy link
Collaborator Author

@kazewong @SSL32081 Ready for review. After merging the bugfix in #214, the pre-commit check should pass.

@thomasckng thomasckng marked this pull request as ready for review May 13, 2025 05:59
@thomasckng thomasckng changed the title Add FullRangePrior Add ConstrainedPrior May 15, 2025
@thomasckng thomasckng added the enhancement New feature or request label May 15, 2025
@CharmaineWONG2
Copy link
Collaborator

corner_plot_150914_new_prior
This is the corner plot for GW150914. In particular, the newly implemented SimpleConstrainedPrior is used for parameters d_L and t_c. All cogwheel transforms are used also.

@tsunhopang
Copy link
Collaborator

@CharmaineWONG2 thanks! Do you also have runs with all 3 detectors? E.g. GW170817 or GW170814?

@CharmaineWONG2
Copy link
Collaborator

GW170814_new_prior
This is the corner plot for GW170814.

Copy link
Collaborator

@tsunhopang tsunhopang left a comment

Choose a reason for hiding this comment

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

LGTM

while n_valid < n_samples:
rng_key, subkey = jax.random.split(rng_key)
new_samples = self.base_prior[0].sample(subkey, n_samples - n_valid)
new_mask = constraints(new_samples)
Copy link
Owner

Choose a reason for hiding this comment

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

I think this should be self.constraints instead?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

constraints was defined in line 294. It is basically a vmap version of self.constraints.

@thomasckng thomasckng removed their assignment Oct 30, 2025
@thomasckng thomasckng closed this Oct 30, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants