Skip to content

ENH: Implement cached_value, cached_data proof of concept - #4421

Merged
bashtage merged 4 commits into
statsmodels:masterfrom
jbrockmendel:data_attr
Jul 11, 2019
Merged

bashtage merged 4 commits into
statsmodels:masterfrom
jbrockmendel:data_attr

Conversation

@jbrockmendel

Copy link
Copy Markdown
Contributor

The existing approach to remove_data is haphazard, lets a lot of things fall through the cracks. For the purpose of this Proof Of Concept, the things falling through the cracks are GLMResults.resid_response, resid_deviance, resid_pearson, ... Because these are not listed in GLM.__init__ when extending self._data_attr, they do not get removed when remove_data is called.

On the flip side, there are a number of summary statistics that we would like to retain when pickling but will not if a cache_readonly attribute has not been evaluated.

The suggestion here is to add two variants of cache_readonly: cached_value and cached_data. cached_data attributes are treated as data-like for the purposes of remove_data, so will be removed. cached_value attributes on the other hand will be evaluated before any data is removed, so they can be recovered reliably.

Unit tests that would currently fail are included.

The actual implementation of cached_data and cached_value is a stub for demonstration purposes. A real version would need to avoid an import from pandas internals.

@coveralls

coveralls commented Apr 4, 2018 •

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.03%) to 85.136% when pulling 3d26a33 on jbrockmendel:data_attr into e56c404 on statsmodels:master.

@jbrockmendel

Copy link
Copy Markdown
Contributor Author

Some upsides to this approach besides improved thoroughness to remove_data:

  • it becomes feasible to implement __eq__ and __ne__ methods for models/results; then pickle-roundtrip tests can be made more useful.
  • when new property-like methods are added in the future, it is easy to forget to edit _data_attrs.
    Since cached_value/cached_data is explicit and adjacent to the new attribute, it is harder to forget.

@josef-pkt

Copy link
Copy Markdown
Member

maybe for 0.11

(I'm canceling part of appveyor queue)

@jbrockmendel

Copy link
Copy Markdown
Contributor Author

I’ll ping in a couple months.

@codecov

codecov Bot commented May 7, 2019

Copy link
Copy Markdown

Codecov Report

Merging #4421 into master will decrease coverage by 0.54%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #4421      +/-   ##
==========================================
- Coverage    82.1%   81.55%   -0.55%     
==========================================
  Files         587      569      -18     
  Lines       93446    89714    -3732     
  Branches    10372    10037     -335     
==========================================
- Hits        76722    73169    -3553     
+ Misses      14327    14273      -54     
+ Partials     2397     2272     -125
Impacted Files Coverage Δ
statsmodels/genmod/generalized_linear_model.py 90.7% <100%> (-0.46%) ⬇️
statsmodels/tools/decorators.py 89.41% <100%> (+1.41%) ⬆️
statsmodels/base/tests/test_shrink_pickle.py 97.9% <100%> (+0.19%) ⬆️
statsmodels/base/model.py 86.18% <100%> (-0.03%) ⬇️
statsmodels/tsa/kalmanf/kalmanfilter.py 36.08% <0%> (-53.01%) ⬇️
statsmodels/tools/_testing.py 59.09% <0%> (-33.02%) ⬇️
statsmodels/sandbox/utils_old.py 41.66% <0%> (-27.3%) ⬇️
statsmodels/tsa/vector_ar/svar_model.py 64.43% <0%> (-19.2%) ⬇️
statsmodels/tools/grouputils.py 61.2% <0%> (-6.19%) ⬇️
statsmodels/sandbox/distributions/multivariate.py 65.21% <0%> (-5%) ⬇️
... and 251 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 116cdd8...9b3dc48. Read the comment docs.

@codecov

codecov Bot commented May 7, 2019 •

Copy link
Copy Markdown

Codecov Report

Merging #4421 into master will increase coverage by <.01%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #4421      +/-   ##
==========================================
+ Coverage   82.61%   82.62%   +<.01%     
==========================================
  Files         598      598              
  Lines       94914    94948      +34     
  Branches    10463    10472       +9     
==========================================
+ Hits        78416    78450      +34     
  Misses      14079    14079              
  Partials     2419     2419
Impacted Files Coverage Δ
statsmodels/genmod/generalized_linear_model.py 91.13% <100%> (ø) ⬆️
statsmodels/tools/decorators.py 95.74% <100%> (+0.29%) ⬆️
statsmodels/base/tests/test_shrink_pickle.py 97.9% <100%> (+0.19%) ⬆️
statsmodels/base/model.py 86.84% <100%> (+0.29%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update e56c404...3d26a33. Read the comment docs.

@jbrockmendel
jbrockmendel force-pushed the data_attr branch 2 times, most recently from 5f1de96 to 8c029a2 Compare May 9, 2019 15:47
@jbrockmendel

Copy link
Copy Markdown
Contributor Author

De facto green

Comment thread statsmodels/tools/decorators.py Outdated
@@ -1,5 +1,7 @@
from __future__ import print_function
from statsmodels.tools.sm_exceptions import CacheWriteWarning
from statsmodels.compat.pandas import cache_readonly as CR

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why as CR?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess it isn't each to import CachedProperty from pandas.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you want as as to make it look like a class, then use as CachedProperty

@bashtage bashtage left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like a good idea. Neds follow up to make sure that this works widely.

Comment thread statsmodels/tools/decorators.py Outdated
@@ -1,5 +1,7 @@
from __future__ import print_function
from statsmodels.tools.sm_exceptions import CacheWriteWarning
from statsmodels.compat.pandas import cache_readonly as CR

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess it isn't each to import CachedProperty from pandas.

Comment thread statsmodels/tools/decorators.py Outdated
@@ -1,5 +1,7 @@
from __future__ import print_function
from statsmodels.tools.sm_exceptions import CacheWriteWarning
from statsmodels.compat.pandas import cache_readonly as CR

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you want as as to make it look like a class, then use as CachedProperty

@jbrockmendel

Copy link
Copy Markdown
Contributor Author

It seems like a good idea. Needs follow up to make sure that this works widely.

Yah. If/when this is accepted, I'll follow-up to standardize the usage.

@jbrockmendel

Copy link
Copy Markdown
Contributor Author

De facto green

@bashtage

Copy link
Copy Markdown
Member

@jbrockmendel What is the status of this? Rebase?

@jbrockmendel

Copy link
Copy Markdown
Contributor Author

Will rebase. This has been ready for a while. If merged, I'll follow up to standardize the usage across the code base.

@bashtage
bashtage merged commit 72f315d into statsmodels:master Jul 11, 2019
@jbrockmendel
jbrockmendel deleted the data_attr branch July 13, 2019 00:49
@bashtage bashtage changed the title implement cached_value, cached_data proof of concept ENH: implement cached_value, cached_data proof of concept Dec 17, 2019
@bashtage bashtage changed the title ENH: implement cached_value, cached_data proof of concept ENH: Implement cached_value, cached_data proof of concept Dec 17, 2019
@bashtage bashtage added this to the 0.11 milestone Dec 18, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants