Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Signature converting functionality for (eventual) backend compatibility #31

Merged
merged 11 commits into from
Mar 24, 2025

Conversation

willGraham01
Copy link
Collaborator

@willGraham01 willGraham01 commented Mar 20, 2025

Introduces the convert_signature function, which (given suitable information) can be used to convert the call signature of one function to another. This is a stepping stone towards the Translation and Translator classes that build on top of this, see #33.

convert_signature allows us to automate the process of mapping the signatures of some backend object to the signatures our frontend objects expect. It provides a means for a user to programmatically incorporate an unsupported backend into the framework that causalprog requires for solving problems.

TLDR / example

Suppose some custom library offers a means of drawing samples from a normal distribution, using the syntax backend_sample(number, seed). This function can be used to draw samples, in place of our native distributions. However, whilst the functionality is provided, the syntax is incorrect; our frontend expects to draw samples via sample(rng_key, sample_shape).

All that needs to be associated is that the rng_key parameter in the frontend should be passed as the seed parameter to the backend, and similarly the sample_shape parameter needs to be passed as the number parameter. This can be done via convert_signature, which essentially will return the function

def sample(rng_key, sample_shape):
    number = sample_shape
    seed = rng_key
    return backend_sample(number, seed)

Caveats

There are certain assumptions that convert_signature has to make in order to cast one signature into another. The function also needs to be provided with the mappings between parameter names, and (optionally) how to handle parameters that might need to be dropped.

convert_signature (and what is built on top of it in #33) is effective for those who want a "plug-and-go" style approach to the package, and is quite effective for just "inserting" a backend to be used in one-or-two places. If a user is planning to exclusively use a particular backend, they are likely better off creating their own custom classes to automate the process (to save repeat-passing of the information into the various Distributions they'll need to create).

@willGraham01 willGraham01 force-pushed the wgraham/signature-converting branch from 5b28001 to 775489f Compare March 21, 2025 14:49
@willGraham01 willGraham01 marked this pull request as ready for review March 24, 2025 09:39
@willGraham01 willGraham01 requested review from mscroggs and removed request for mscroggs March 24, 2025 09:39
@willGraham01 willGraham01 merged commit 114beaa into main Mar 24, 2025
5 checks passed
@willGraham01 willGraham01 deleted the wgraham/signature-converting branch March 24, 2025 14:50
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.

None yet

2 participants