Skip to content

BUG: ZeroDivisionError when returning zero-length series/arrays #19

Description

@asher-pembroke

Cachey produces an unhelpful div/zero error when a cached function returns a zero-length array.

Examples:

from cachey import Cache
import numpy as np
import pandas as pd

cache = Cache(1e5)

@cache.memoize
def myfunc(x):
    return x

try:
    myfunc(pd.Series())
except ZeroDivisionError as m:
    print(m)

try:
    myfunc(np.array([]))
except ZeroDivisionError as m:
    print(m)
    
myfunc([]) #lists work fine
float division by zero
float division by zero

Out[39]:
[]

Activity

  1. changed the title [-]ZeroDivisionError when returning zero-length series/arrays[/-] [+]BUG: ZeroDivisionError when returning zero-length series/arrays[/+] on Oct 31, 2019
  2. jrbourbeau commented on Oct 31, 2019

    @jrbourbeau
    Member

    Thanks @asher-pembroke for the nice example! I'm able to reproduce the error. This originates from the cost function dividing by the number of bytes of an object:

    def cost(nbytes, time):
    return float(time) / nbytes / 1e9

    which is current 0 for things like empty NumPy arrays:

    In [1]: from cachey import nbytes
    
    In [2]: import numpy as np
    
    In [3]: nbytes(np.array([]))
    Out[3]: 0

    Do you have any thoughts on how this situation might be improved?

  3. accurrently commented on Nov 1, 2019

    @accurrently
    Contributor

    def cost(nbytes, time):
    return float(time) / nbytes / 1e9

    This should be somewhat easy to fix. Consider that nbytes ought to be an int (because you can't have a fractional byte), but divide by some fractional float if nbytes is 0:

    def cost(nbytes, time):
        return float(time) / max(nbytes, .1) / 1e9

    ZeroDivisionError is avoided, but you still have something smaller than a byte. Depending on how scoring should be affected, this can be an arbitrary, such that max(nbytes, x) where 0 < x < 1.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions