numpy/numpy-user-dtypes

Exposing custom formatters for user dtypes

Opened this issue · 3 comments

Since numpy_quaddtype is a subtype of PyFloatingArrType_Type, NumPy decides the formatter for printing arrays as follows

 if formatter is not None:
        fkeys = [k for k in formatter.keys() if formatter[k] is not None]
        if 'all' in fkeys:
            for key in formatdict.keys():
                formatdict[key] = indirect(formatter['all'])
        if 'int_kind' in fkeys:
            for key in ['int']:
                formatdict[key] = indirect(formatter['int_kind'])
        if 'float_kind' in fkeys:
            for key in ['float', 'longfloat']:
                formatdict[key] = indirect(formatter['float_kind'])
        if 'complex_kind' in fkeys:
            for key in ['complexfloat', 'longcomplexfloat']:
                formatdict[key] = indirect(formatter['complex_kind'])
        if 'str_kind' in fkeys:
            formatdict['numpystr'] = indirect(formatter['str_kind'])
        for key in formatdict.keys():
            if key in fkeys:
                formatdict[key] = indirect(formatter[key])

File: https://github.com/numpy/numpy/blob/main/numpy/_core/arrayprint.py

The usage of default float_kind formatter cannot handle the quad precision values (as it casts them to float64). One workaround is to override this (which I currently use for testing) as

np.set_printoptions(formatter={'float_kind': lambda x: str(x)}) # or repr(x) for scientific notations

But this affects all float types. A better workaround would be allowing dtypes exposing their custom formatters.

cc: @seberg @ngoldbaum

In quaddtype str and repr are already dragon4 processed, we/any user-dtype can expose a custom __format__ method that can be utilized for this.

I'm not sure if timezones make it difficult, but it may be useful for you to come to today's NumPy community zoom meeting to chat about this stuff and your work on QuadDType.

Sorry I haven't been chiming in much lately, things have been hectic for me.

Here's a link to the numpy community calendar: https://scientific-python.org/calendars/numpy.ics

It maps to midnight in my time zone, so I might not be able to make it today
I’ll try to make up this time slot and join in future meets :)