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

Support for displaying a trend line and embedding a right image #24

Open
wants to merge 7 commits into
base: master
Choose a base branch
from

Conversation

rahul-deepsource
Copy link

Thanks for the wonderful library!

I wanted to add the functionality to add a trend line which can graphically show how a metric has been varying over time, and thought that this would be a nice addition to this library as well!

Here are a bunch of changes:

  • Add show-trend argument which takes in comma-separated list of values (maximum: 10).
  • Add --right-image which would display an image (110 x 14) between left and right text.
  • Rename --left-color to --bg-color because this was also the background for the right_image, so it didn't really make sense to call it left-color.
  • Add a new left-color which would add a

Here is an example:

which was generated using the command:

python -m pybadges --left-text "badge trends" --right-text "2.1k" --whole-link "https://deepsource.io/gh/deepsourcelabs/asgard" --bg-color "#252525" --right-color "#63B439" --logo https://raw.githubusercontent.com/deepsourcelabs/brand-assets/master/logo-white.svg --show-trend 1311,2100,3333,598,800,1111,200 --left-color="#4E4E4E" --embed-logo

There are still a few tests failing (and docs missing). If you give me a green flag that you intend to merge these changes, I'd be happy to provide them as well! :)

Copy link
Collaborator

@brianquinlan brianquinlan left a comment

Choose a reason for hiding this comment

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

This is super cool! A few things:

  1. could you add tests for trend.py?
  2. could you add some golden images and the end to end tests for them?
  3. could you increase the version number by a major version?
  4. could you update the README (and add some of your examples!)

Once again: amazing work!

pybadges/__init__.py Outdated Show resolved Hide resolved
pybadges/__init__.py Outdated Show resolved Hide resolved
pybadges/__init__.py Outdated Show resolved Hide resolved
pybadges/__main__.py Outdated Show resolved Hide resolved


def fit_data(samples: List[int]) -> Tuple[List[int], List[int]]:
y = list(
Copy link
Collaborator

Choose a reason for hiding this comment

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

Does it make sense to do this or to interpolate?
e.g. with numpy.interp

Copy link
Author

Choose a reason for hiding this comment

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

The reason behind repeating these numbers it that the more samples we have, we can increase the polyfit order more liberally which smoothens the curve considerably.

I don't think that it would make much sense to first interpolate the curve and then fit it again.

Also, I found out about np.repeat. Removing this hacky repeat method.

itertools.chain.from_iterable(
itertools.repeat(sample, 10) for sample in samples))
xp = np.arange(len(y))
yp = normalize(np.poly1d(np.polyfit(xp, y, 15))(xp))
Copy link
Collaborator

Choose a reason for hiding this comment

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

So the bottom is always zero?

Copy link
Author

Choose a reason for hiding this comment

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

Ahm, I don't quite get the question.

If all the values in yp are equal, then we'd have an array of all 1's after normalization. Similarly for all zeroes, we'd have a flat line.

Polyfit might return negative numbers sometime (-1 at most). But, since we only scale up positive numbers (yp[yp > 0] *= ...), and use an offset of -1 when defining the origin, it shouldn't be a problem.

@rahul-deepsource
Copy link
Author

Hey, thanks for the review @brianquinlan! Apologies for the delay, I got held up with something else. I'll address the comments today.

@rahul-deepsource
Copy link
Author

I have another concern: the inline images in the SVG don't show up while viewing the raw image (served by https://raw.githubusercontent.com). For e.g., the image I linked in the example above looks like this:

Firefox:
Screenshot 2020-09-09 at 1 50 32 PM

Brave (Blink more generally), we get two ugly broken image icons:

Screenshot 2020-09-09 at 1 52 00 PM

I am not sure why this happens -- my guess is the Same origin policy? Is there a way to resolve this?

Copy link
Collaborator

@brianquinlan brianquinlan left a comment

Choose a reason for hiding this comment

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

I'm really sorry for the slow review process - life is kind of crazy right now ;-)

Could you add tests for trend.py, an example for test-badge.json, increment the major version number and README page?



def normalize(arr: np.ndarray) -> np.ndarray:
max_arr = np.max(arr)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Could you write a doc string explaining what this does?



def fit_data(samples: List[int]) -> Tuple[List[int], List[int]]:
width = WIDTH - X_OFFSET
Copy link
Collaborator

Choose a reason for hiding this comment

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

Could you write a doc string to explain what this does?



def trend(samples: List[int], stroke_color: str, stroke_width: int) -> str:
canvas = draw.Drawing(WIDTH, HEIGHT, origin=(0, -Y_OFFSET))
Copy link
Collaborator

Choose a reason for hiding this comment

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

Could you write a doc string explaining what this does?

@rahul-deepsource
Copy link
Author

Hey @brianquinlan! No worries! I can totally understand! I'm leaving this comment to show a sign of life, and that I'm still interested in getting this through. However, I won't be able to work on it atleast until the end of this week. I'll update it soon.

@brianquinlan
Copy link
Collaborator

Thanks. I don't have a tonne of bandwidth these days either ;-)

@brianquinlan
Copy link
Collaborator

OK, I tried patching in this PR and playing with it. Here are some of my thoughts:

  1. We could just decide that this change if out of scope since Github-style badges don't support embedded images. That would be unfortunate.

  2. We can make the API more generic and backwards compatible. We can add new parameters to badge without changing any existing parameters: center_image, center_link, center_color, center_title, embed_center_image.

    Additionally, we could add some functionality to:

    • Help build the trend line image (but don't change the badge function)
    • Make right_text optional i.e. if it is not set then there will be no content on the right side - this would allow you to have the trend line as the right-most element

    If we go with this approach, then we could have three smaller (and backwards-compatible) PRs:

    1. add center_* support
    2. add trend line support
    3. make right_text optional

What do you think? I'm happy to help with any of these.

@nshan651
Copy link
Contributor

Hello, I'm interested in helping out with making the pybadges API more generic with support for a center parameter. I'm working on project for creating longitudinal status badges of CI build metrics, and I think a more generic version of pybadges would be really useful! Please let me know if this is still in line with the project vision.

@brianquinlan
Copy link
Collaborator

Hello, I'm interested in helping out with making the pybadges API more generic with support for a center parameter. I'm working on project for creating longitudinal status badges of CI build metrics, and I think a more generic version of pybadges would be really useful! Please let me know if this is still in line with the project vision.

Hey @nshan651 , I'd be happy to make pybadges more generic! I think that this PR just stalled because the author ran out of bandwidth and I'll pretty picky about complex PRs ;-)

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.

3 participants