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

Small bugfix for potential divide by zero error during sparkline rendering. #113

Merged
merged 1 commit into from
Mar 18, 2022

Conversation

adewinter
Copy link
Contributor

Issue

Sometimes, when the same data is repeatedly added to a sparkline to be rendered a ZeroDivisionError is thrown.

Traceback (most recent call last):
  File "C:\Users\adewi\acode\perspective\python\main.py", line 101, in run_face_detector
    self.draw_detection_data(detection, image_height, image_width)
  File "C:\Users\adewi\acode\perspective\python\main.py", line 66, in draw_detection_data
    self.drawer.drawSparklines(self.rawPosition[0], self.position[0])
  File "C:\Users\adewi\acode\perspective\python\drawer.py", line 97, in drawSparklines
    cvui.sparkline(
  File "C:\Users\adewi\AppData\Local\Programs\Python\Python39\lib\site-packages\cvui\cvui.py", line 2746, in sparkline
    __internal.sparkline(aBlock, aValues, aX, aY, aWidth, aHeight, aColor)
  File "C:\Users\adewi\AppData\Local\Programs\Python\Python39\lib\site-packages\cvui\cvui.py", line 637, in sparkline
    self._render.sparkline(theBlock, theValues, aRect, aMin, aMax, theColor)
  File "C:\Users\adewi\AppData\Local\Programs\Python\Python39\lib\site-packages\cvui\cvui.py", line 1000, in sparkline
    y = (theValues[i] - theMin) / aScale * -(theRect.height - 5) + theRect.y + theRect.height - 5
ZeroDivisionError: float division by zero

Steps to recreate

Run this script:

import cv2
import cvui
import numpy as np


CVUI_WINDOW_NAME = "CVUI"
CVUI_FRAME = np.zeros((200, 600, 3), np.uint8)
cvui.init(CVUI_WINDOW_NAME)

sparkline_data = [1.23] * 10  # populate data with all same values

while True:
    cvui.sparkline(CVUI_FRAME, sparkline_data, 0, 0, 600, 200, 0x00FF00)

    cvui.imshow(CVUI_WINDOW_NAME, CVUI_FRAME)
    k = cv2.waitKey(1) & 0xFF
    if k == 27:
        break

Proposed solution

The problem can be traced to Line 1000 in cvui.py. Specifically: when the calculated value for aScale becomes zero, a ZeroDivisionError will be thrown. This occurs when both theMax and theMin are the same value which can be done by providing the call to cvui.sparkline(...) with a array of all the same values.

The proposed fix is to either use the calculated aScale value (i.e. theMax - theMin) but if that computes to zero, use 1 instead.

@Dovyski
Copy link
Owner

Dovyski commented Mar 18, 2022

That's great, thank you for the contribution! 🚀🥳

@Dovyski Dovyski merged commit 2f34f22 into Dovyski:master Mar 18, 2022
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