Skip to content
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

Issue241 rcm param and ducktyping for models #248

Conversation

zf109
Copy link

@zf109 zf109 commented Jun 16, 2019

Description

Solving Issue 241, please see details there.

Motivation and Context

A feature request: allows duck typing for instantiate TopicModel class and allow passing rc_params to the drew_termite_plot function so that the termite plot can be more flexible.

How Has This Been Tested?

  • for allow duck typing, as per in here
  • for allow passing params, these were tested manually by plotting termite plot within Jupyter notebook and change the rc_params and see if the plot changed as expected.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist:

  • My code follows the code style of this project.
  • My change requires a change to the documentation, and I have updated it accordingly. (inline documentation only though)

@zf109
Copy link
Author

zf109 commented Jun 18, 2019

@bdewilde hey, as promised, sent over a pull request about the two enhancement mentioned. Cheers,
zf

@bdewilde
Copy link
Collaborator

Hey @zf109 , thanks for the PR! I've been busy packing / moving / unpacking my life the past couple weeks, so please bear with me a little longer as I get back up to speed. Thanks again, will review this soon. 👍

Copy link
Collaborator

@bdewilde bdewilde left a comment

Choose a reason for hiding this comment

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

Thanks for your patience in awaiting code review — it's been a busy couple weeks. :) There are a few minor bits to address, but otherwise this should be good to merge.

@@ -138,6 +141,10 @@ def draw_termite_plot(
raise ValueError(msg)
highlight_colors = {hc: COLOR_PAIRS[i] for i, hc in enumerate(highlight_cols)}

if rc_params:
Copy link
Collaborator

Choose a reason for hiding this comment

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

This block overwrites parts of the global RC_PARAMS with whatever is passed in the function call, but it would be safer to modify a local version of the params. Could you do something like

_rc_params = RC_PARAMS.copy()
if rc_params:
    _rc_params.update(rc_params)

with plt.rc_context(_rc_params):
    ...

instead?

Copy link
Author

Choose a reason for hiding this comment

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

@bdewilde yeah sure, it's better indeed :)

textacy/viz/termite.py Outdated Show resolved Hide resolved
@@ -87,6 +88,8 @@ def draw_termite_plot(
of (light/dark) matplotlib-friendly colors used to highlight a single
column; if not specified (default), a good set of 6 pairs are used
save (str, optional): give the full /path/to/fname on disk to save figure
rc_params (dict, optional): allow passing parameters to rc_context in matplotlib.plyplot,
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: plyplot => pyplot

Copy link
Author

Choose a reason for hiding this comment

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

ah.. missed this one... sorry

@@ -392,6 +398,8 @@ def termite_plot(
the default ("seriation") groups similar terms together, which
facilitates cross-topic assessment
save (str): give the full /path/to/fname on disk to save figure
rc_params (dict, optional): allow passing parameters to rc_context in matplotlib.plyplot,
Copy link
Collaborator

Choose a reason for hiding this comment

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

same comments as on the other docstring

Copy link
Author

Choose a reason for hiding this comment

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

addressed

def __init__(self, model, n_topics=10, **kwargs):
if isinstance(model, (NMF, LatentDirichletAllocation, TruncatedSVD)):
self.model = model
elif all([hasattr(model, required_attr) for required_attr in self._required_trained_model_attr]):
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: you can drop the brackets on the list ([ and ])

Copy link
Author

Choose a reason for hiding this comment

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

addressed

@@ -103,9 +104,13 @@ class TopicModel(object):
- http://scikit-learn.org/stable/modules/generated/sklearn.decomposition.TruncatedSVD.html
"""

_required_trained_model_attr = {"transform", "components_", "n_topics"}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Scanning through the code, it looks like models also need a .fit() method and n_components attribute. Is that doable?

Copy link
Author

Choose a reason for hiding this comment

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

agree with the n_components, but would be good to leave .fit method a bit optional, essentially it allows to initialize with a trained model, so we don't need to call the training method. But up to you I guess.

if rc_params:
for key in rc_params:
RC_PARAMS[key] = rc_params[key]
_rc_params.update(rc_params)

with plt.rc_context(RC_PARAMS):
Copy link
Collaborator

Choose a reason for hiding this comment

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

Almost there! You forgot to change this line: RC_PARAMS => _rc_params

@bdewilde
Copy link
Collaborator

Actually, I'm just going to merge this in and make that minor hotfix in develop. Thanks very much for contributing! :shipit:

@bdewilde bdewilde merged commit 7130936 into chartbeat-labs:master Jul 14, 2019
@bdewilde
Copy link
Collaborator

Welp, looks like it actually went into master. I'll figure it out. 😅

@zf109
Copy link
Author

zf109 commented Aug 3, 2019

@bdewilde ha, great, sorry was a bit busy last few weeks and on holiday then so didn't see your comments... thanks a lot for merging it :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants