jitclass with object-mode static functions

92 views
Skip to first unread message

Mark Turiansky

unread,
Jun 23, 2020, 2:06:19 AM6/23/20
to Numba Public Discussion - Public
Hello,

I was wondering if it is possible with the jitclass interface to define "staticmethods", which are not compiled with nopython=True?

The reason I ask is because a very common code style that I run into is for data classes that read and write to a file. For example,
```
class Data:
    def __init__(self, fname, data):
        self.fname = fname
        self.data = data

    # stuff stuff stuff

    @staticmethod
    def from_file(filename):
        with open(filename, 'r') as f:
            data = []
            for line in f:
                data.extend([float(x) for x in line.split()])
        return Data(filename, data)
```
In this case, applying @jitclass compiles the class, and I can access all of the other variables/methods. However, when I try to do "Data.from_file(filename)", I get an error associated with the "with" statement. If I replace the statement with "f = open(filename, 'r')" and "f.close()", then I get an error "Untyped global name 'open': cannot determine Numba type of <class 'builtin_function_or_method'>". It would be nice if I could specify that this specific method doesn't need to be compiled or should just be in object mode.

Thanks for your help.

Best,
Mark

Valentin Haenel

unread,
Jun 23, 2020, 5:12:22 AM6/23/20
to numba...@continuum.io
Hi Mark,

thank you for using Numba, and thank you for asking about this.

It might be a bit of a longshot, but perhaps you could use an 'objmode' block: http://numba.pydata.org/numba-doc/latest/user/withobjmode.html?highlight=objmode%20block#the-objmode-context-manager - it says you can't use `with` in that block, but perhaps you can close the file manually?

Best wishes,

V-

--
You received this message because you are subscribed to the Google Groups "Numba Public Discussion - Public" group.
To unsubscribe from this group and stop receiving emails from it, send an email to numba-users...@continuum.io.
To view this discussion on the web visit https://groups.google.com/a/continuum.io/d/msgid/numba-users/57944e74-0ad7-4b4d-976d-6bdaccdf412co%40continuum.io.

Mark Turiansky

unread,
Jun 23, 2020, 5:13:15 PM6/23/20
to Numba Public Discussion - Public
Yes, thank you for the suggestion! I had meant to try this, but forgot. It is an effective work-around. Just for documentation sake, here is the example that worked for me:
```
    @staticmethod
    def from_file(filename):
        with objmode(data='float64[:]'):
            f = open(filename, 'r')
            data = []
            for line in f:
                data.extend([float(x) for x in line.split()])
            f.close()
        return Data(filename, data)
```
While this works fine, I find it to be a bit inelegant. Personally, I find the fact that numba works with decorators and minimal code modifications to be a beautiful feature. Do you think it is worth opening a pull request for this feature? I would imagine the interface would be something like ```@jitclass(spec, allow_fallback=['from_file', ...])```.

Best,
Mark

On Tuesday, June 23, 2020 at 2:12:22 AM UTC-7, Valentin Haenel wrote:
Hi Mark,

thank you for using Numba, and thank you for asking about this.

It might be a bit of a longshot, but perhaps you could use an 'objmode' block: http://numba.pydata.org/numba-doc/latest/user/withobjmode.html?highlight=objmode%20block#the-objmode-context-manager - it says you can't use `with` in that block, but perhaps you can close the file manually?

Best wishes,

V-

On Tue, Jun 23, 2020 at 8:06 AM Mark Turiansky <mark.t...@gmail.com> wrote:
Hello,

I was wondering if it is possible with the jitclass interface to define "staticmethods", which are not compiled with nopython=True?

The reason I ask is because a very common code style that I run into is for data classes that read and write to a file. For example,
```
class Data:
    def __init__(self, fname, data):
        self.fname = fname
        self.data = data

    # stuff stuff stuff

    @staticmethod
    def from_file(filename):
        with open(filename, 'r') as f:
            data = []
            for line in f:
                data.extend([float(x) for x in line.split()])
        return Data(filename, data)
```
In this case, applying @jitclass compiles the class, and I can access all of the other variables/methods. However, when I try to do "Data.from_file(filename)", I get an error associated with the "with" statement. If I replace the statement with "f = open(filename, 'r')" and "f.close()", then I get an error "Untyped global name 'open': cannot determine Numba type of <class 'builtin_function_or_method'>". It would be nice if I could specify that this specific method doesn't need to be compiled or should just be in object mode.

Thanks for your help.

Best,
Mark

--
You received this message because you are subscribed to the Google Groups "Numba Public Discussion - Public" group.
To unsubscribe from this group and stop receiving emails from it, send an email to numba...@continuum.io.

Valentin Haenel

unread,
Jun 24, 2020, 5:16:07 AM6/24/20
to numba...@continuum.io
Hi Mark,

thanks for reporting and great to hear that you managed to get it to. work! As for features and pull-requests, my personal take on such questions is that if it has value for you, it may have value for others and therefore it is worth submitting as a feature. Of course I can not guarantee that it will be merged, but it doesn't sound like something to be rejected outright either. Perhaps, if you want to discuss the API and hash out various alternatives to choose from, open a thread on our shiny new discourse: https://numba.discourse.group/ ?

Best wishes and thanks again for using Numba!

V-

To unsubscribe from this group and stop receiving emails from it, send an email to numba-users...@continuum.io.
To view this discussion on the web visit https://groups.google.com/a/continuum.io/d/msgid/numba-users/8501a0ed-4fdc-462c-8f01-7d1690bfa4c4o%40continuum.io.
Reply all
Reply to author
Forward
0 new messages