-
Notifications
You must be signed in to change notification settings - Fork 20
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
Refactor MonteCarlo to be a StatefulInterpretation #369
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
75bf7b3
Refactor MonteCarlo to be a StatefulInterpretation
fritzo 77a9f07
Fix state passing
fritzo dacde90
Merge branch 'master' into stateful-interpreter
fritzo 1b6e86e
Fix jax tests
fritzo d8f7905
Add missing import
fritzo 0b8ab86
Simplify examples
fritzo fa03d48
Work around Python 3.5 lack of __init_subclass__
fritzo 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 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 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 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 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 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 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 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 |
---|---|---|
|
@@ -6,7 +6,7 @@ | |
import os | ||
import re | ||
import types | ||
from collections import OrderedDict | ||
from collections import OrderedDict, namedtuple | ||
from contextlib import contextmanager | ||
from functools import singledispatch | ||
|
||
|
@@ -111,6 +111,7 @@ def interpretation(new): | |
assert callable(new) | ||
global _INTERPRETATION | ||
old = _INTERPRETATION | ||
new = InterpreterStack(new, old) | ||
try: | ||
_INTERPRETATION = new | ||
yield | ||
|
@@ -311,6 +312,14 @@ def reinterpret(x): | |
return recursion_reinterpret(x) | ||
|
||
|
||
class InterpreterStack(namedtuple("InterpreterStack", ["default", "fallback"])): | ||
def __call__(self, cls, *args): | ||
for interpreter in self: | ||
result = interpreter(cls, *args) | ||
if result is not None: | ||
return result | ||
|
||
|
||
def dispatched_interpretation(fn): | ||
""" | ||
Decorator to create a dispatched interpretation function. | ||
|
@@ -324,13 +333,54 @@ def dispatched_interpretation(fn): | |
return fn | ||
|
||
|
||
class StatefulInterpretationMeta(type): | ||
def __init__(cls, name, bases, dct): | ||
super().__init__(name, bases, dct) | ||
cls.registry = KeyedRegistry(default=lambda *args: None) | ||
cls.dispatch = cls.registry.dispatch | ||
|
||
|
||
class StatefulInterpretation(metaclass=StatefulInterpretationMeta): | ||
""" | ||
Base class for interpreters with instance-dependent state or parameters. | ||
|
||
Example usage:: | ||
|
||
class MyInterpretation(StatefulInterpretation): | ||
|
||
def __init__(self, my_param): | ||
self.my_param = my_param | ||
|
||
@MyInterpretation.register(...) | ||
def my_impl(interpreter_state, cls, *args): | ||
my_param = interpreter_state.my_param | ||
... | ||
|
||
with interpretation(MyInterpretation(my_param=0.1)): | ||
... | ||
""" | ||
|
||
def __call__(self, cls, *args): | ||
return self.dispatch(cls, *args)(self, *args) | ||
Comment on lines
+363
to
+364
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. This default implementation of |
||
|
||
if _DEBUG: | ||
@classmethod | ||
def register(cls, *args): | ||
return lambda fn: cls.registry.register(*args)(debug_logged(fn)) | ||
else: | ||
@classmethod | ||
def register(cls, *args): | ||
return cls.registry.register(*args) | ||
|
||
|
||
class PatternMissingError(NotImplementedError): | ||
def __str__(self): | ||
return "{}\nThis is most likely due to a missing pattern.".format(super().__str__()) | ||
|
||
|
||
__all__ = [ | ||
'PatternMissingError', | ||
'StatefulInterpretation', | ||
'dispatched_interpretation', | ||
'interpret', | ||
'interpretation', | ||
|
This file contains 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 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 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 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 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 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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess this is required for Python 3.5? Once we drop Python 3.5 support, we should remove this and other metaclasses where possible in favor of
__init_subclass__
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed, I was almost tempted to drop support for Python 3.5 in this PR, but that seemed like overkill.