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

using numba with "from collections import Counter" #5535

Closed
oussamabnr opened this issue Apr 9, 2020 · 2 comments
Closed

using numba with "from collections import Counter" #5535

oussamabnr opened this issue Apr 9, 2020 · 2 comments
Labels
question Notes an issue as a question

Comments

@oussamabnr
Copy link

oussamabnr commented Apr 9, 2020

Hi, could you help me please to run this function using numba .

`from collections import Counter
@njit(nopython=True)
def TE(C):
pxxy = Counter(literal_unroll(map(tuple,C)))
pxy = Counter(map(tuple,C[:,1:]))
pxx = Counter(map(tuple,C[:,:2]))
px = Counter(map(tuple,C[:,1:2]))

    A = pxxy.keys()
    
    tot = float(sum(pxxy.values()))

    return sum((pxxy[k]/tot)* np.log2( (pxxy[k]/float(pxy[k[1:]])) / (pxx[k[:2]]/float(px[k[1:2]])) ) for k in A)`

It generates an error C is a matrix of 0 and 1 with 3 columns

@gmarkall gmarkall added the question Notes an issue as a question label Apr 9, 2020
@gmarkall
Copy link
Member

gmarkall commented Apr 9, 2020

Thanks for the question - a few remarks on the above:

  • You can write blocks of code in Markdown using three backticks on a line, instead of just the one - see e.g. https://github.github.com/gfm/#example-89
  • Using nopython=True with njit is redundant - njit is just a shorthand for @jit(nopython=True)
  • It would be a bit easier to look into if the code provided were executable to demonstrate the issue, with some explanation of the expected outcome from running it.

I've had a guess at what the executable reproducer might be:

from numba import njit, literal_unroll
from collections import Counter
import numpy as np


@njit
def TE(C):
    pxxy = Counter(literal_unroll(map(tuple, C)))
    pxy = Counter(map(tuple, C[:, 1:]))
    pxx = Counter(map(tuple, C[:, :2]))
    px = Counter(map(tuple, C[:, 1:2]))

    A = pxxy.keys()

    tot = float(sum(pxxy.values()))

    return sum(
        (pxxy[k] / tot) * np.log2((pxxy[k] / float(pxy[k[1:]])) /
                                  (pxx[k[:2]] / float(px[k[1:2]])))
        for k in A
    )

c = np.random.rand(5,5,5)

TE(c)

There are some difficulties with using this with Numba directly:

@stuartarchibald
Copy link
Contributor

Closing this issue as it seems to be resolved. If this is not the case please re-open with a comment about any item that appears to be unresolved. Many thanks.

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

No branches or pull requests

3 participants