Search code examples
pythonnumbajit

how to extract class type from a jitclass and specify it alone


I have a jitclass say weather and save one of its attribute att = weather.class_type.instance_type to use in another jit function to specify its output style.

out_style = Tuple.from_types((ListType(att)))
@jit([out_style(nb.int64, nb.int64)], nopython=True)
def new_function():
    ......

This attribute is printed as below.

27725c129d0<_data:array(float64, 2d, A),_colmap:DictType[unicode_type,int64]<iv=None>,date0:datetime64[M],_tdelta:timedelta64[M]>

Now I want to remove this jitclass, still want to manually specify this attribute and use it. I see there are four data types, array(float64, 2d, A), DictType[unicode_type,int64], datetime64[M], and timedelta64[M]

I declare it this way, however, error is reported as below.

out_style = Tuple.from_types((ListType(nb.types.Array(nb.float64, 2, 'A'), DictType(nb.types.unicode_type, bb2), nb.types.NPDatetime('M'), nb.types.NPTimedelta('M')))


TypeError: __init__() takes 2 positional arguments but 5 were given

Please help to fix this issue. Thanks.

I'm not sure I understand it correctly. ListType() can contain only one argument. In my case, I added four in ListType(). It causes the error. Here type of att is classinstancetype. Since jitclass is removed as previously mentioned, which data type would be a good alternative, tuple?


Solution

  • nb.types.Tuple.from_types takes a list in parameter that should contain each type of the tuple. For example:

    nb.types.Tuple.from_types([
        nb.types.ListType(nb.types.int32), 
        nb.types.float64, 
        nb.types.int64
    ])
    

    This is a type of tuple containing 3 elements: a list of 32-bit integers, a 64-bit float and a 64-bit integer.

    Based on your code, I guess you want this:

    out_style = nb.types.Tuple.from_types([
        nb.types.ListType(nb.float64[:,:]), 
        nb.types.DictType(nb.types.unicode_type, bb2), 
        nb.types.NPDatetime('M'), 
        nb.types.NPTimedelta('M')
    ])