Skip to content

FAQ-D/REF/ENH: change remove_data to allow for common attribute names in results and model #3840

Open

Description

@josef-pkt

It looks like the current base.model remove_data method always tries to remove model attributes also from the results instance. AFAIR, this was initially written when we used references in the results instance for attributes in the model instance as a user convenience.

This seems to break, i.e. cause a warning, when the results instance has a cached attribute with the same name as a (regular) model attribute. mu in GLM has this pattern

see comments starting here #3563 (comment)

None of the things that I tried with manipulating the lists of things to remove change anything in the warning. Making GLMResult.mu into a regular property or renaming it to mu2 removes the CacheWriteWarning.

  • Either we refactor remove_data to allow the case where the results instance has a cached attribute with the same name as a model attribute.
  • or, we disallow this case (i.e. separate what are model and what are results attribute, GLM should not attach mu to the model in fit_irls.)

Activity

  1. jbrockmendel commented on Oct 10, 2017

    @jbrockmendel
    Contributor

    _data_attr is an easy way to make mistakes, an easy thing to forget to update, etc. Ideally there would be a way to systematically determine at runtime what attributes belong in data_attr.

    AFAICT there are four cases. Names set in '_init' could automatically be identified in 'handle_data' when doing 'data.dict.pop'. Others are defined in cache_readonlys, could be marked with a specific cache_name. Having to get self.endog and self.model.endog and self.model.data.endog or whatever could be made easier by being more consistent about where those attributes get defined (eg I think generic likelihood results has self.endog while non-generic doesn't)

    Types with thumbs. Infer excuses as appropriate.

  2. josef-pkt commented on Oct 10, 2017

    @josef-pkt
    MemberAuthor

    The main reasons that I picked this design is because it is easy to understand, easy to debug (just check a few attributes), and easy to override in specific models. Furthermore, it is a fixed setup cost, to write it or add to the list needs to be done just once.

    Introspection is a pain to debug, difficult to override, and it's complicated to inspect all types of models.

    e.g. debug a case where someone complains that "something" is not removed or that too much has been removed. In one case I check a few lists, in the other case I have to figure out why introspection doesn't handle that case correctly.
    (partially related: I spent some (un)happy hours or days with inspect.inspect usage in scipy.)

  3. jbrockmendel commented on Oct 10, 2017

    @jbrockmendel
    Contributor

    Good point. Someone ought to explain that to the OP.

  4. josef-pkt commented on Oct 11, 2017

    @josef-pkt
    MemberAuthor

    The OP says that it's just a noisy warning and not a bug, and that it's not clear yet whom to "blame", i.e. do we fix GLM or do we fix remove_data?

    Either fix is most likely just one or two lines.

  5. jbrockmendel commented on Oct 11, 2017

    @jbrockmendel
    Contributor

    Fair enough. Three thoughts:

    debug a case where someone complains that "something" is not removed or that too much

    I've had similar issues with test_shrink_pickle throwing errors saying that pickle size is too big, but with no information about what it could be. One idea there could be a helper function in the test file to track down largeish arrays.

    I recognize that this is a pipe dream, but: ideally we would be able to separate econometrics from plumbing (plumbing is basically __reduce__, __getstate__, remove_data, _data_attrs, wrap_output, anything related to the relationships in the Results/Model/Data hierarchies). If we could systematically identify what attributes belong in _data_attrs (or data_in_cache, or _data_attr_model), it would be nice not to need to think about those things when working outside of base.

    Circling back to the original issue, there is an outside chance #4016 is related.

  6. ChadFulton commented on Oct 11, 2017

    @ChadFulton
    Member

    I'm very much a bystander in this area (very grateful that Kevin did the work getting state space models to pickle correctly), and I don't really know all the machinery that exists, but I wonder if save / load is an area where we might want to explicitly plan to do less rather than more?

    In state space models (which is maybe a special case), I notice that what it comes down to is that people mostly seem to just want to save everything (or more likely, they want to save everything except for the few things they don't want, but they don't know what they don't want).

    When there's a lot of data I guess pickle is unfortunately not a great format. But given limitations on developer time, this may not be our comparative advantage, as they say?

  7. josef-pkt commented on Oct 11, 2017

    @josef-pkt
    MemberAuthor

    One idea there could be a helper function in the test file to track down largeish arrays.

    That would be helpful. I thought about it but never tried to write a general version.
    It would most likely fit better into a testing utility module, as debugging helper, than in the actual tests.
    Usually I did manual inspection when debugging this, i.e. go through dir(...) and/or check recent changes.

    If we could systematically identify what attributes belong in _data_attrs (or data_in_cache, or _data_attr_model)

    That's difficult in general because our models have a lot of variety, e.g. the new zero inflated models are two part models and also have a second exog. AFAIR, we did not check remove_data for those models yet. One of the many places where support is still incomplete for these models.
    Hurdle models also contain two submodels, where we still have to add remove data to include submodels. (I haven't even started to look into that, and is not very high on my priority given the other things that those models are missing, e.g. full support of formula and joint missing handling)

  8. josef-pkt commented on Oct 11, 2017

    @josef-pkt
    MemberAuthor

    @ChadFulton Unfortunately for us, pickling is too important to ignore.

    e.g. https://stackoverflow.com/questions/46577285/can-trim-statsmodels-result-for-serialization

    The two main use cases are using an existing model for prediction and pickling for multiprocessing. I don't know anything about the multiprocessing use case.

    The main alternatives for pickling in prediction are standalone predict functions and rebuilding models for prediction, smooth and filter in the statespace case. We either don't support that or it is not well advertised or hasn't been popularized by blogs, tutorials, ...
    #3563 which gave rise to this issue, and pretty old #1590

  9. ChadFulton commented on Oct 11, 2017

    @ChadFulton
    Member

    Unfortunately for us, pickling is too important to ignore.

    Well it was worth a try :)

  10. jbrockmendel commented on Oct 11, 2017

    @jbrockmendel
    Contributor

    But given limitations on developer time, this may not be our comparative advantage, as they say?

    Regardless of the answer, Chad gets 6 points for asking the right question.

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

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions