Skip to content

foapy.ma.order

order(X, return_alphabet=False)

Find array sequence in order of their appearance

Parameters:

Name Type Description Default

X

Array to get unique values.

required

return_alphabet

If True also return array's alphabet

False

Returns:

Name Type Description
result masked_array or Exception.

Exception if not d1 array, masked_array otherwise.

Examples:

----1----

>>> import foapy.ma as ma
>>> a = ['a', 'b', 'a', 'c', 'd']
>>> b = ma.order(a)
>>> b
[
[0, -- 0, -- --]
[-- 1 -- -- --]
[-- -- -- 2, --]
[-- -- -- -- 3]
]

----2----

>>> import foapy.ma as ma
>>> a = ['a', 'b', 'a', 'c', 'd']
>>> result, alphabet = ma.order(a, True)
>>> result
[
[0, -- 0, -- --]
[-- 1 -- -- --]
[-- -- -- 2, --]
[-- -- -- -- 3]
]
>>> alphabet
['a', 'b', 'c', 'd']

----3----

>>> import foapy.ma as ma
>>> a = [1, 4, 1000, 4, 15]
>>> b = ma.order(a)
>>> b
[
[0 -- -- -- --]
[-- 1 -- 1 --]
[-- -- 2 -- --]
[-- -- -- -- 3]
]

----4----

>>> import foapy.ma as ma
>>> a = ["a", "c", "c", "e", "d", "a"]
>>> b = ma.order(a)
>>> b
[
[0 -- -- -- -- 0]
[-- 1 1 -- -- --]
[-- -- -- 2 -- --]
[-- -- -- -- 3 --]
]

----5----

>>> import foapy.ma as ma
>>> a = [1, 2, 2, 3, 4, 1]
>>> b = ma.order(a)
>>> b
[
[0 -- -- -- -- 0]
[-- 1 1 -- -- --]
[-- -- -- 2 -- --]
[-- -- -- -- 3 --]
]

----6----

>>> import foapy.ma as ma
>>> a = ["ATC", "CGT", "ATC"]
>>> b = ma.order(a)
>>> b
[
[0 -- 0]
[-- 1 --]
]

----7----

>>> import foapy.ma as ma
>>> a = []
>>> b = ma.order(a)
>>> b
[]

----8----

>>> import foapy.ma as ma
>>> a = [[2, 2, 2], [2, 2, 2]]
>>> b = ma.order(a)
>>> b
Exception

----9----

>>> import foapy.ma as ma
>>> a = [[[1], [3]], [[6], [9]], [[6], [3]]]
>>> b = ma.order(a)
>>> b
Exception
Source code in .tox/docs-deploy/lib/python3.11/site-packages/foapy/ma/_order.py
def order(X, return_alphabet=False) -> np.ma.MaskedArray:
    """
    Find array sequence  in order of their appearance

    Parameters
    ----------
    X: masked_array
        Array to get unique values.

    return_alphabet: bool, optional
        If True also return array's alphabet

    Returns
    -------
    result: masked_array or Exception.
        Exception if not d1 array, masked_array otherwise.

    Examples
    --------

    ----1----
    >>> import foapy.ma as ma
    >>> a = ['a', 'b', 'a', 'c', 'd']
    >>> b = ma.order(a)
    >>> b
    [
    [0, -- 0, -- --]
    [-- 1 -- -- --]
    [-- -- -- 2, --]
    [-- -- -- -- 3]
    ]

    ----2----
    >>> import foapy.ma as ma
    >>> a = ['a', 'b', 'a', 'c', 'd']
    >>> result, alphabet = ma.order(a, True)
    >>> result
    [
    [0, -- 0, -- --]
    [-- 1 -- -- --]
    [-- -- -- 2, --]
    [-- -- -- -- 3]
    ]
    >>> alphabet
    ['a', 'b', 'c', 'd']

    ----3----
    >>> import foapy.ma as ma
    >>> a = [1, 4, 1000, 4, 15]
    >>> b = ma.order(a)
    >>> b
    [
    [0 -- -- -- --]
    [-- 1 -- 1 --]
    [-- -- 2 -- --]
    [-- -- -- -- 3]
    ]

    ----4----
    >>> import foapy.ma as ma
    >>> a = ["a", "c", "c", "e", "d", "a"]
    >>> b = ma.order(a)
    >>> b
    [
    [0 -- -- -- -- 0]
    [-- 1 1 -- -- --]
    [-- -- -- 2 -- --]
    [-- -- -- -- 3 --]
    ]

    ----5----
    >>> import foapy.ma as ma
    >>> a = [1, 2, 2, 3, 4, 1]
    >>> b = ma.order(a)
    >>> b
    [
    [0 -- -- -- -- 0]
    [-- 1 1 -- -- --]
    [-- -- -- 2 -- --]
    [-- -- -- -- 3 --]
    ]

    ----6----
    >>> import foapy.ma as ma
    >>> a = ["ATC", "CGT", "ATC"]
    >>> b = ma.order(a)
    >>> b
    [
    [0 -- 0]
    [-- 1 --]
    ]

    ----7----
    >>> import foapy.ma as ma
    >>> a = []
    >>> b = ma.order(a)
    >>> b
    []

    ----8----
    >>> import foapy.ma as ma
    >>> a = [[2, 2, 2], [2, 2, 2]]
    >>> b = ma.order(a)
    >>> b
    Exception

    ----9----
    >>> import foapy.ma as ma
    >>> a = [[[1], [3]], [[6], [9]], [[6], [3]]]
    >>> b = ma.order(a)
    >>> b
    Exception
    """

    if X.ndim > 1:  # Checking for d1 array
        raise Not1DArrayException(
            {"message": f"Incorrect array form. Expected d1 array, exists {X.ndim}"}
        )

    order, alphabet_values = general_order(ma.getdata(X), return_alphabet=True)

    power = len(alphabet_values)
    length = len(X)

    result_data = np.tile(order, power).reshape(power, length)
    alphabet_indecies = np.arange(power).reshape(power, 1)
    result_mask = result_data != alphabet_indecies

    indecies_selector = np.any(~np.logical_or(result_mask, ma.getmaskarray(X)), axis=1)

    if np.any(indecies_selector):
        result_data = result_data[indecies_selector]
        result_mask = result_mask[indecies_selector]
    else:
        # If all items are masked we need define empty array explicity
        # otherwise, the result shape would be (0, length)
        # that affect compare arrays
        # (test tests/test_ma_order.py::TestMaOrder::test_void_int_values_with_mask)
        result_data = []
        result_mask = []

    result = ma.masked_array(result_data, mask=result_mask)

    if return_alphabet:  # Checking for get alphabet (optional)
        return result, alphabet_values[indecies_selector]
    return result