Skip to content

statsmodels.graphics.functional pickle issue #4772

Closed

Description

@EricWrightAtWork

I have code that needs to pickle functions (i.e. for joblib) that are defined in modules where statsmodels.api is imported. The pickle attempt leads to an AttributeError from the statsmodels.graphics.functional module:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\[...]\lib\site-packages\statsmodels\graphics\functional.py", line 32, in _pickle_method
    if m.im_self is None:
AttributeError: 'function' object has no attribute 'im_self'

This can be simply reproduced by creating a short module with the following code:

import statsmodels.api as sm
class Dummy(object):
    def __init__(self):
        pass
    def get_add_constant(self):
        return sm.add_constant

If this code is saved in a file, test_mod.py, then I can trigger the exception by opening a python session and entering:

>>> from test_mod import Dummy
>>> dum = Dummy()
>>> pickle.dumps(dum.get_add_constant)

I have worked around the issue by narrowing my import to just the classes needed in my code, but this seems like a bug in graphics.functional that should be addressed.

Note: I observed this issue using statsmodels 0.9.0 in Windows.

Activity

  1. josef-pkt commented on Jul 5, 2018

    @josef-pkt
    Member

    @EricWrightAtWork Thanks for reporting.

    I can replicate it as soon as statsmodels.api is imported, even when it is not used.
    That is, copyreg in graphics changes the global registry which messes up pickling in other parts of statsmodels.

    I don't know yet what the fix is,
    Do we adjust the pickle_method in graphics functional or do we drop it completely?
    I don't think some specific module should change globals or global settings, i.e. if we need to change a global registry, then it should go into tools like exceptions and warnings.

  2. added this to the 0.10 milestone on Jul 5, 2018
  3. pseudotensor commented on Jul 26, 2018

    @pseudotensor

    I get same error for 0.9.0 but not 0.8.0.

        pickle.dump(obj=obj, file=f, protocol=4)
      File "/home/****/env/lib/python3.6/site-packages/statsmodels/graphics/functional.py", line 32, in _pickle_method
        if m.im_self is None:
    AttributeError: 'function' object has no attribute 'im_self'
    
    
  4. added 3 commits that reference this issue on Aug 22, 2018
    6a19d9a
    56b9766
    fb6787e
  5. added 3 commits that reference this issue on Sep 10, 2018
    bf0860f
    eaabf14
    064c5c3
  6. jfindlay commented on Nov 19, 2018

    @jfindlay

    Can we get a 0.9.1 for this bug? It is causing big problems for my team. Downgrading to 0.8.0 causes problems compiling a wheel that do not seem to be present with 0.9.0.

  7. jfindlay commented on Nov 20, 2018

    @jfindlay

    For anyone who encounters this bug, here is a workaround I am using for 0.9.0:

    # Workaround https://github.com/statsmodels/statsmodels/issues/4772.  Remove
    # when no longer using statsmodels<=0.9.0.
    def fix_statsmodels_pickle():
        try:
            import copyreg
        except ImportError:
            import copy_reg as copyreg
        import types
    
        if hasattr(copyreg, 'dispatch_table') \
        and isinstance(copyreg.dispatch_table, dict) \
        and types.MethodType in copyreg.dispatch_table:
            method_pickler = copyreg.dispatch_table.get(types.MethodType)
            if hasattr(method_pickler, '__module__') \
            and 'statsmodels.graphics.functional' in method_pickler.__module__ \
            and hasattr(method_pickler, '__name__') \
            and '_pickle_method' in method_pickler.__name__:
                del copyreg.dispatch_table[types.MethodType]
    import statsmodels.graphics.functional
    import mylib.compat ; mylib.compat.fix_statsmodels_pickle()
  8. mhooreman commented on Jan 14, 2019

    @mhooreman

    Hello,

    When will the fix be available on pip? I need that for production servers, where I had to force statsmodels<0.9.

    Thanks a lot.

  9. rebecca-palmer commented on Sep 19, 2019

    @rebecca-palmer
    Contributor

    pip has 0.10 which includes this fix; no incompatible changes are specifically listed in the release notes, but I haven't checked further.

    The fix (#4963) looks like it may well work in 0.9, but I haven't had time to test it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions