- 
                Notifications
    
You must be signed in to change notification settings  - Fork 72
 
          Add @as_model decorator
          #268
        
          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
          
     Merged
      
      
    
  
     Merged
                    Changes from all commits
      Commits
    
    
            Show all changes
          
          
            10 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      bb48c88
              
                move marginal_model to model/
              
              
                theorashid 5c7d42a
              
                add model decorator
              
              
                theorashid c5f62f5
              
                expose to docs
              
              
                theorashid 38007c2
              
                no ... at end of codeblock
              
              
                theorashid 52a04e7
              
                cheaper test using point_logps
              
              
                theorashid 5c83a47
              
                fix doc link and imports
              
              
                theorashid f4aff55
              
                rename `model` as `as_model`
              
              
                theorashid 84306df
              
                us assert_equal instead of assert_array_equal
              
              
                theorashid b5210f7
              
                update docs to link to zaxtax blog
              
              
                theorashid d0a5ae0
              
                change import to from pymc import Model
              
              
                theorashid File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
              Empty file.
          
    
            File renamed without changes.
          
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| from functools import wraps | ||
| 
     | 
||
| from pymc import Model | ||
| 
     | 
||
| 
     | 
||
| def as_model(*model_args, **model_kwargs): | ||
| R""" | ||
| Decorator to provide context to PyMC models declared in a function. | ||
| This removes all need to think about context managers and lets you separate creating a generative model from using the model. | ||
| 
     | 
||
| Adapted from `Rob Zinkov's blog post <https://www.zinkov.com/posts/2023-alternative-frontends-pymc/>`_ and inspired by the `sampled <https://github.com/colcarroll/sampled>`_ decorator for PyMC3. | ||
| 
     | 
||
| Examples | ||
| -------- | ||
| .. code:: python | ||
| 
     | 
||
| import pymc as pm | ||
| import pymc_experimental as pmx | ||
| 
     | 
||
| # The following are equivalent | ||
| 
     | 
||
| # standard PyMC API with context manager | ||
| with pm.Model(coords={"obs": ["a", "b"]}) as model: | ||
| x = pm.Normal("x", 0., 1., dims="obs") | ||
| pm.sample() | ||
| 
     | 
||
| # functional API using decorator | ||
| @pmx.as_model(coords={"obs": ["a", "b"]}) | ||
| def basic_model(): | ||
| pm.Normal("x", 0., 1., dims="obs") | ||
| 
     | 
||
| m = basic_model() | ||
| pm.sample(model=m) | ||
| 
     | 
||
| """ | ||
| 
     | 
||
| def decorator(f): | ||
| @wraps(f) | ||
| def make_model(*args, **kwargs): | ||
| 
         There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would also add a name kwarg here to change model name There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can pass it to model_kwargs  | 
||
| with Model(*model_args, **model_kwargs) as m: | ||
| f(*args, **kwargs) | ||
| return m | ||
| 
     | 
||
| return make_model | ||
| 
     | 
||
| return decorator | ||
              Empty file.
          
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| import numpy as np | ||
| import pymc as pm | ||
| 
     | 
||
| import pymc_experimental as pmx | ||
| 
     | 
||
| 
     | 
||
| def test_logp(): | ||
| """Compare standard PyMC `with pm.Model()` context API against `pmx.model` decorator | ||
| and a functional syntax. Checks whether the kwarg `coords` can be passed. | ||
| """ | ||
| coords = {"obs": ["a", "b"]} | ||
| 
     | 
||
| with pm.Model(coords=coords) as model: | ||
| pm.Normal("x", 0.0, 1.0, dims="obs") | ||
| 
     | 
||
| @pmx.as_model(coords=coords) | ||
| def model_wrapped(): | ||
| pm.Normal("x", 0.0, 1.0, dims="obs") | ||
| 
     | 
||
| mw = model_wrapped() | ||
| 
     | 
||
| np.testing.assert_equal(model.point_logps(), mw.point_logps()) | 
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
Uh oh!
There was an error while loading. Please reload this page.