-
Notifications
You must be signed in to change notification settings - Fork 14
Pyplot for token attributions (continued from PR #11) #13
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 hidden or 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 |
---|---|---|
@@ -0,0 +1,60 @@ | ||
from typing import Tuple, List, Any, Iterable, Union | ||
import matplotlib.pyplot as plt | ||
|
||
|
||
class TokenAttributions(list): | ||
def __init__(self, token_attributions: List[Tuple[str, float]]) -> None: | ||
super().__init__(token_attributions) | ||
self.token_attributions = token_attributions | ||
|
||
def __getitem__(self, item: Union[str, int]) -> Any: | ||
return getattr(self, item) if isinstance(item, str) else self.token_attributions[item] | ||
|
||
def __setitem__(self, key: Union[str, int], value: Any) -> None: | ||
setattr(self, key, value) | ||
|
||
def plot(self, plot_type: str = 'barh', title: str = 'Token Attributions', **plot_kwargs) -> None: | ||
''' | ||
Plot the token attributions to have a comparative view. | ||
Available plot types include bar chart, horizontal bar chart, and pie chart. | ||
''' | ||
tokens, attributions = list(zip(*self.token_attributions)) | ||
|
||
# get arguments from plot_kwargs | ||
xlabel = plot_kwargs.get('xlabel') | ||
ylabel = plot_kwargs.get('ylabel') | ||
title = plot_kwargs.get('title') or title | ||
|
||
if plot_type == 'bar': | ||
# Bar chart | ||
plt.bar(tokens, attributions) | ||
plt.xlabel(xlabel or 'tokens') | ||
plt.ylabel(ylabel or 'attribution value') | ||
|
||
elif plot_type == 'barh': | ||
# Horizontal bar chart | ||
plt.barh(tokens, attributions) | ||
plt.xlabel(xlabel or 'attribution value') | ||
plt.ylabel(ylabel or 'tokens') | ||
plt.gca().invert_yaxis() # to have the order of tokens from top to bottom | ||
|
||
elif plot_type == 'pie': | ||
# Pie chart | ||
plot_kwargs = { | ||
'startangle': 90, 'counterclock': False, 'labels': tokens, | ||
'autopct': '%1.1f%%', 'pctdistance': 0.8, | ||
**plot_kwargs | ||
} | ||
plt.pie(attributions, **plot_kwargs) | ||
if xlabel: | ||
plt.xlabel(xlabel) | ||
if ylabel: | ||
plt.ylabel(ylabel) | ||
|
||
else: | ||
raise NotImplementedError( | ||
f"`plot_type={plot_type}` is not implemented. Choose one of: ['bar', 'barh', 'pie']" | ||
) | ||
|
||
# set title | ||
plt.title(title) |
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.
Uh oh!
There was an error while loading. Please reload this page.