question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ENH: Add justify functionality

See original GitHub issue

I used this functionality quite often and I think would fit in numpy. It’s to shift values over an invalid value to a certain direction over certain axis.

Note: this code and functionality was provided by Divakar on SO

Reproducing code example:

a = np.array([[1, 0, 2, 0],
              [3, 0, 4, 0],
              [5, 0, 6, 0],
              [6, 7, 0, 8]])

def justify(a, invalid_val=0, axis=1, side='left'):    
    """
    Justifies a 2D array

    Parameters
    ----------
    A : ndarray
        Input array to be justified
    axis : int
        Axis along which justification is to be made
    side : str
        Direction of justification. It could be 'left', 'right', 'up', 'down'
        It should be 'left' or 'right' for axis=1 and 'up' or 'down' for axis=0.

    """

    if invalid_val is np.nan:
        mask = ~np.isnan(a)
    else:
        mask = a!=invalid_val
    justified_mask = np.sort(mask,axis=axis)
    if (side=='up') | (side=='left'):
        justified_mask = np.flip(justified_mask,axis=axis)
    out = np.full(a.shape, invalid_val) 
    if axis==1:
        out[justified_mask] = a[mask]
    else:
        out.T[justified_mask.T] = a.T[mask.T]
    return out

Output

In [473]: a # input array
Out[473]: 
array([[1, 0, 2, 0],
       [3, 0, 4, 0],
       [5, 0, 6, 0],
       [6, 7, 0, 8]])

In [474]: justify(a, axis=0, side='up')
Out[474]: 
array([[1, 7, 2, 8],
       [3, 0, 4, 0],
       [5, 0, 6, 0],
       [6, 0, 0, 0]])

In [475]: justify(a, axis=0, side='down')
Out[475]: 
array([[1, 0, 0, 0],
       [3, 0, 2, 0],
       [5, 0, 4, 0],
       [6, 7, 6, 8]])

In [476]: justify(a, axis=1, side='left')
Out[476]: 
array([[1, 2, 0, 0],
       [3, 4, 0, 0],
       [5, 6, 0, 0],
       [6, 7, 8, 0]])

In [477]: justify(a, axis=1, side='right')
Out[477]: 
array([[0, 0, 1, 2],
       [0, 0, 3, 4],
       [0, 0, 5, 6],
       [0, 6, 7, 8]])

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
eric-wiesercommented, Mar 29, 2020

I think this function should take mask directly, rather than an invalid value.

Once you make that change, it’s easy to reason this belongs in np.ma, likely as an nd generalization of compress. It might already be there.

front and back would be better names than left and right.

1reaction
rossbarcommented, Jul 13, 2020

AFAIK there is no NumPy gitter set up - the easiest way might be to simply submit a PR (you can mark it with “draft” status if you want to explicitly indicate that it’s primarily for discussion purposes).

Read more comments on GitHub >

github_iconTop Results From Across the Web

justify-content - CSS: Cascading Style Sheets - MDN Web Docs
The CSS justify-content property defines how the browser distributes space between and around content items along the main-axis of a flex ...
Read more >
MOSFET Amplifier Circuit using an Enhancement MOSFET
Electronics Tutorial about the MOSFET Amplifier Circuit which uses enhancement-mode mosfet to produce a common source mosfet amplifier.
Read more >
Species360 Terms of Use - ZIMS Training Academy
HOW TO REQUEST AN ENHANCEMENT (PENDO FEEDBACK/RECEPTIVE). How to request a new ... Adding desired functionality and data fields ... justify the need....
Read more >
WordArt Tools - Microsoft Support
See how to insert or delete WordArt in Publisher. ... how to insert and delete WordArt, and walk you through using the features...
Read more >
Human Enhancement: Scientific and Ethical Dimensions of ...
Human enhancement is at least as old as human civilization. People have been trying to enhance their physical and mental capabilities for ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found