numpy / numpy/numpy-quaddtype

Exposing custom formatters for user dtypes

Open
#19 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
24
Forks
6
Avg merge
5d 41m
Merged PRs (30d)
3

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with numpy/_core/arrayprint.py and the formatter-selection logic quoted in the issue. Trace how NumPy selects formatters for subtype dtypes and identify the extension point needed for a custom dtype formatter. Done means quad-precision values can use their dtype-specific formatter without changing formatting for other float types.

Written by the indexing model from the issue text.

Assessment

Tech stack
numpy, python
Domain
data
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.