cythonuniquedim


Namecythonuniquedim JSON
Version 0.10 PyPI version JSON
download
home_pagehttps://github.com/hansalemaos/cythonuniquedim
SummaryFinds unique dimensions in multi-dimensional arrays, compresses and decompresses arrays ...
upload_time2023-12-09 04:16:35
maintainer
docs_urlNone
authorJohannes Fischer
requires_python
licenseGPL3
keywords cython arrays
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            
# Finds unique dimensions in multi-dimensional arrays, compresses and decompresses arrays ...


## pip install cythonuniquedim

### Tested against Windows / Python 3.11 / Anaconda


## Cython (and a C/C++ compiler) must be installed


This module provides functions for working with multi-dimensional arrays and performing operations
such as grouping rows with the same values, compressing and decompressing arrays, and
finding unique dimensions in multi-dimensional arrays.


```python
FUNCTIONS
    compress_rows_as_list(a, bitshift_dtype=None)
        Compress rows of a 2D numpy array into a list of integers using bit shifting.
        
        Args:
            a (numpy.ndarray): The input 2D array.
            bitshift_dtype (numpy.dtype, optional): The data type for bit shifting. Defaults to None (will find the fastest).
        
        Returns:
            Tuple[List[mpz], int, Tuple[int, int]]: A tuple containing the compressed list of integers,
            the bitshift value used, and the shape of the input array.
    
    group_same_rows_as_dict(a, bitshift_dtype=None, fullkeys=True)
        Group rows with the same values in a 2D numpy array and return the result as a dictionary.
        
        Args:
            a (numpy.ndarray): The input 2D array.
            bitshift_dtype (numpy.dtype, optional): The data type for bit shifting. Defaults to None (will find the fastest).
            fullkeys (bool, optional): If True, uses full keys (mpz) in the result dictionary.
                                       If False, uses integer keys. Defaults to True.
        
        Returns:
            Tuple[Dict[Union[int, mpz], List[int]], int, Tuple[int, int]]: A tuple containing the result
            dictionary, the bitshift value used, and the shape of the input array.
    
    uncompress_list(mpz_list, usedbitshift, elements_per_row, numpy_dtype=None)
        Decompress a list of integers into a 2D numpy array.
        
        Args:
            mpz_list (List[mpz]): The compressed list of integers.
            usedbitshift (int): The bitshift value used during compression.
            elements_per_row (int): The number of elements per row in the original array.
            numpy_dtype (numpy.dtype, optional): The desired data type of the output array. Defaults to None.
        
        Returns:
            numpy.ndarray: The decompressed 2D numpy array.
    
    unique_dimensions(a, last_dim, unordered=True, iterray=())
        Find unique dimensions in a multi-dimensional numpy array.
        
        Args:
            a (numpy.ndarray): The input multi-dimensional array.
            last_dim (int): The last dimension to consider when finding unique dimensions.
            unordered (bool, optional): If True, uses multiprocessing to create the index. Defaults to True.
            iterray (Any, optional): Custom iterator array. Defaults to an empty tuple.
        
        Returns:
            numpy.ndarray: The array containing unique dimensions based on the specified criteria.
import numpy as np

from cythonuniquedim import group_same_rows_as_dict, compress_rows_as_list, uncompress_list, unique_dimensions

shaha = (3, 5)
dtype = np.uint8
b = np.arange(np.product(shaha), dtype=dtype)
a = b.reshape(shaha)
a = np.tile(a, 3).T
acp = a.copy()
resultdict0, usedbitshift0, arrayviewshape0 = group_same_rows_as_dict(a, bitshift_dtype=None, fullkeys=True)
resultdict1, usedbitshift1, arrayviewshape1 = group_same_rows_as_dict(a, bitshift_dtype=None, fullkeys=False)
print(f'{resultdict0,usedbitshift0,arrayviewshape0=}')
print(f'{resultdict1,usedbitshift1,arrayviewshape1=}')

mpz_list100, mpz_list_usedbitshift100, arrayviewshape100 = compress_rows_as_list(a, bitshift_dtype=None, )
mpz_list111, mpz_list_usedbitshift111, arrayviewshape111 = compress_rows_as_list(a, bitshift_dtype=np.uint8, )
print(f'{mpz_list100,mpz_list_usedbitshift100,arrayviewshape100=}')
print(f'{mpz_list111,mpz_list_usedbitshift111,arrayviewshape111=}')
unclist = (
    uncompress_list(mpz_list=mpz_list111, usedbitshift=mpz_list_usedbitshift111, elements_per_row=arrayviewshape111[1],
                    numpy_dtype=np.uint32))
print(f'{unclist=}')

# resultdict0,usedbitshift0,arrayviewshape0=({mpz(656640): [0, 5, 10], mpz(722433): [1, 6, 11], mpz(788226): [2, 7, 12], mpz(854019): [3, 8, 13], mpz(919812): [4, 9, 14]}, 256, (15, 3))
# resultdict1,usedbitshift1,arrayviewshape1=({0: [0, 5, 10], 1: [1, 6, 11], 2: [2, 7, 12], 3: [3, 8, 13], 4: [4, 9, 14]}, 256, (15, 3))
# mpz_list100,mpz_list_usedbitshift100,arrayviewshape100=([mpz(656640), mpz(722433), mpz(788226), mpz(854019), mpz(919812), mpz(656640), mpz(722433), mpz(788226), mpz(854019), mpz(919812), mpz(656640), mpz(722433), mpz(788226), mpz(854019), mpz(919812)], 256, (15, 3))
# mpz_list111,mpz_list_usedbitshift111,arrayviewshape111=([mpz(656640), mpz(722433), mpz(788226), mpz(854019), mpz(919812), mpz(656640), mpz(722433), mpz(788226), mpz(854019), mpz(919812), mpz(656640), mpz(722433), mpz(788226), mpz(854019), mpz(919812)], 256, (15, 3))
# unclist=array([[ 0,  5, 10],
#        [ 1,  6, 11],
#        [ 2,  7, 12],
#        [ 3,  8, 13],
#        [ 4,  9, 14],
#        [ 0,  5, 10],
#        [ 1,  6, 11],
#        [ 2,  7, 12],
#        [ 3,  8, 13],
#        [ 4,  9, 14],
#        [ 0,  5, 10],
#        [ 1,  6, 11],
#        [ 2,  7, 12],
#        [ 3,  8, 13],
#        [ 4,  9, 14]], dtype=uint32)

shaha = (4, 3, 4, 3, 3, 2, 3)
dtype = np.float64
b = np.arange(np.product(shaha), dtype=dtype)
a = b.reshape(shaha)
a = np.tile(a, 3).T.copy()
unidims = unique_dimensions(a, last_dim=3, unordered=True)
print(f'{unidims=}')
print(f'{unidims.shape=}')

unidims1 = unique_dimensions(a, last_dim=4, unordered=True)
print(f'{unidims1=}')
print(f'{unidims1.shape=}')
unidims2 = unique_dimensions(a, last_dim=5, unordered=True)
print(f'{unidims2=}')
print(f'{unidims2.shape=}')
unidims3 = unique_dimensions(a, last_dim=6, unordered=True)
print(f'{unidims3=}')
print(f'{unidims3.shape=}')

# unidims=array([[[[[[0.000e+00, 6.480e+02, 1.296e+03, 1.944e+03],
#            [2.160e+02, 8.640e+02, 1.512e+03, 2.160e+03],
#            [4.320e+02, 1.080e+03, 1.728e+03, 2.376e+03]],
#           [[5.400e+01, 7.020e+02, 1.350e+03, 1.998e+03],
#            [2.700e+02, 9.180e+02, 1.566e+03, 2.214e+03],
#            [4.860e+02, 1.134e+03, 1.782e+03, 2.430e+03]],
#           [[1.080e+02, 7.560e+02, 1.404e+03, 2.052e+03],
#            [3.240e+02, 9.720e+02, 1.620e+03, 2.268e+03],
#            [5.400e+02, 1.188e+03, 1.836e+03, 2.484e+03]],
#           [[1.620e+02, 8.100e+02, 1.458e+03, 2.106e+03],
#            [3.780e+02, 1.026e+03, 1.674e+03, 2.322e+03],
#            [5.940e+02, 1.242e+03, 1.890e+03, 2.538e+03]]],
#          [[[1.800e+01, 6.660e+02, 1.314e+03, 1.962e+03],
#            [2.340e+02, 8.820e+02, 1.530e+03, 2.178e+03],
#            [4.500e+02, 1.098e+03, 1.746e+03, 2.394e+03]],
#           [[7.200e+01, 7.200e+02, 1.368e+03, 2.016e+03],
#            [2.880e+02, 9.360e+02, 1.584e+03, 2.232e+03],
#            [5.040e+02, 1.152e+03, 1.800e+03, 2.448e+03]],
#           [[1.260e+02, 7.740e+02, 1.422e+03, 2.070e+03],
#            [3.420e+02, 9.900e+02, 1.638e+03, 2.286e+03],
#            [5.580e+02, 1.206e+03, 1.854e+03, 2.502e+03]],
#           [[1.800e+02, 8.280e+02, 1.476e+03, 2.124e+03],
#            [3.960e+02, 1.044e+03, 1.692e+03, 2.340e+03],
#            [6.120e+02, 1.260e+03, 1.908e+03, 2.556e+03]]],
# ......
    
```

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/hansalemaos/cythonuniquedim",
    "name": "cythonuniquedim",
    "maintainer": "",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "",
    "keywords": "cython,arrays",
    "author": "Johannes Fischer",
    "author_email": "aulasparticularesdealemaosp@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/81/62/34e439eadfbfcb6bbb016e6c6073fa1fd7aac55b4cafae6a0b9d70aff6dd/cythonuniquedim-0.10.tar.gz",
    "platform": null,
    "description": "\r\n# Finds unique dimensions in multi-dimensional arrays, compresses and decompresses arrays ...\r\n\r\n\r\n## pip install cythonuniquedim\r\n\r\n### Tested against Windows / Python 3.11 / Anaconda\r\n\r\n\r\n## Cython (and a C/C++ compiler) must be installed\r\n\r\n\r\nThis module provides functions for working with multi-dimensional arrays and performing operations\r\nsuch as grouping rows with the same values, compressing and decompressing arrays, and\r\nfinding unique dimensions in multi-dimensional arrays.\r\n\r\n\r\n```python\r\nFUNCTIONS\r\n    compress_rows_as_list(a, bitshift_dtype=None)\r\n        Compress rows of a 2D numpy array into a list of integers using bit shifting.\r\n        \r\n        Args:\r\n            a (numpy.ndarray): The input 2D array.\r\n            bitshift_dtype (numpy.dtype, optional): The data type for bit shifting. Defaults to None (will find the fastest).\r\n        \r\n        Returns:\r\n            Tuple[List[mpz], int, Tuple[int, int]]: A tuple containing the compressed list of integers,\r\n            the bitshift value used, and the shape of the input array.\r\n    \r\n    group_same_rows_as_dict(a, bitshift_dtype=None, fullkeys=True)\r\n        Group rows with the same values in a 2D numpy array and return the result as a dictionary.\r\n        \r\n        Args:\r\n            a (numpy.ndarray): The input 2D array.\r\n            bitshift_dtype (numpy.dtype, optional): The data type for bit shifting. Defaults to None (will find the fastest).\r\n            fullkeys (bool, optional): If True, uses full keys (mpz) in the result dictionary.\r\n                                       If False, uses integer keys. Defaults to True.\r\n        \r\n        Returns:\r\n            Tuple[Dict[Union[int, mpz], List[int]], int, Tuple[int, int]]: A tuple containing the result\r\n            dictionary, the bitshift value used, and the shape of the input array.\r\n    \r\n    uncompress_list(mpz_list, usedbitshift, elements_per_row, numpy_dtype=None)\r\n        Decompress a list of integers into a 2D numpy array.\r\n        \r\n        Args:\r\n            mpz_list (List[mpz]): The compressed list of integers.\r\n            usedbitshift (int): The bitshift value used during compression.\r\n            elements_per_row (int): The number of elements per row in the original array.\r\n            numpy_dtype (numpy.dtype, optional): The desired data type of the output array. Defaults to None.\r\n        \r\n        Returns:\r\n            numpy.ndarray: The decompressed 2D numpy array.\r\n    \r\n    unique_dimensions(a, last_dim, unordered=True, iterray=())\r\n        Find unique dimensions in a multi-dimensional numpy array.\r\n        \r\n        Args:\r\n            a (numpy.ndarray): The input multi-dimensional array.\r\n            last_dim (int): The last dimension to consider when finding unique dimensions.\r\n            unordered (bool, optional): If True, uses multiprocessing to create the index. Defaults to True.\r\n            iterray (Any, optional): Custom iterator array. Defaults to an empty tuple.\r\n        \r\n        Returns:\r\n            numpy.ndarray: The array containing unique dimensions based on the specified criteria.\r\nimport numpy as np\r\n\r\nfrom cythonuniquedim import group_same_rows_as_dict, compress_rows_as_list, uncompress_list, unique_dimensions\r\n\r\nshaha = (3, 5)\r\ndtype = np.uint8\r\nb = np.arange(np.product(shaha), dtype=dtype)\r\na = b.reshape(shaha)\r\na = np.tile(a, 3).T\r\nacp = a.copy()\r\nresultdict0, usedbitshift0, arrayviewshape0 = group_same_rows_as_dict(a, bitshift_dtype=None, fullkeys=True)\r\nresultdict1, usedbitshift1, arrayviewshape1 = group_same_rows_as_dict(a, bitshift_dtype=None, fullkeys=False)\r\nprint(f'{resultdict0,usedbitshift0,arrayviewshape0=}')\r\nprint(f'{resultdict1,usedbitshift1,arrayviewshape1=}')\r\n\r\nmpz_list100, mpz_list_usedbitshift100, arrayviewshape100 = compress_rows_as_list(a, bitshift_dtype=None, )\r\nmpz_list111, mpz_list_usedbitshift111, arrayviewshape111 = compress_rows_as_list(a, bitshift_dtype=np.uint8, )\r\nprint(f'{mpz_list100,mpz_list_usedbitshift100,arrayviewshape100=}')\r\nprint(f'{mpz_list111,mpz_list_usedbitshift111,arrayviewshape111=}')\r\nunclist = (\r\n    uncompress_list(mpz_list=mpz_list111, usedbitshift=mpz_list_usedbitshift111, elements_per_row=arrayviewshape111[1],\r\n                    numpy_dtype=np.uint32))\r\nprint(f'{unclist=}')\r\n\r\n# resultdict0,usedbitshift0,arrayviewshape0=({mpz(656640): [0, 5, 10], mpz(722433): [1, 6, 11], mpz(788226): [2, 7, 12], mpz(854019): [3, 8, 13], mpz(919812): [4, 9, 14]}, 256, (15, 3))\r\n# resultdict1,usedbitshift1,arrayviewshape1=({0: [0, 5, 10], 1: [1, 6, 11], 2: [2, 7, 12], 3: [3, 8, 13], 4: [4, 9, 14]}, 256, (15, 3))\r\n# mpz_list100,mpz_list_usedbitshift100,arrayviewshape100=([mpz(656640), mpz(722433), mpz(788226), mpz(854019), mpz(919812), mpz(656640), mpz(722433), mpz(788226), mpz(854019), mpz(919812), mpz(656640), mpz(722433), mpz(788226), mpz(854019), mpz(919812)], 256, (15, 3))\r\n# mpz_list111,mpz_list_usedbitshift111,arrayviewshape111=([mpz(656640), mpz(722433), mpz(788226), mpz(854019), mpz(919812), mpz(656640), mpz(722433), mpz(788226), mpz(854019), mpz(919812), mpz(656640), mpz(722433), mpz(788226), mpz(854019), mpz(919812)], 256, (15, 3))\r\n# unclist=array([[ 0,  5, 10],\r\n#        [ 1,  6, 11],\r\n#        [ 2,  7, 12],\r\n#        [ 3,  8, 13],\r\n#        [ 4,  9, 14],\r\n#        [ 0,  5, 10],\r\n#        [ 1,  6, 11],\r\n#        [ 2,  7, 12],\r\n#        [ 3,  8, 13],\r\n#        [ 4,  9, 14],\r\n#        [ 0,  5, 10],\r\n#        [ 1,  6, 11],\r\n#        [ 2,  7, 12],\r\n#        [ 3,  8, 13],\r\n#        [ 4,  9, 14]], dtype=uint32)\r\n\r\nshaha = (4, 3, 4, 3, 3, 2, 3)\r\ndtype = np.float64\r\nb = np.arange(np.product(shaha), dtype=dtype)\r\na = b.reshape(shaha)\r\na = np.tile(a, 3).T.copy()\r\nunidims = unique_dimensions(a, last_dim=3, unordered=True)\r\nprint(f'{unidims=}')\r\nprint(f'{unidims.shape=}')\r\n\r\nunidims1 = unique_dimensions(a, last_dim=4, unordered=True)\r\nprint(f'{unidims1=}')\r\nprint(f'{unidims1.shape=}')\r\nunidims2 = unique_dimensions(a, last_dim=5, unordered=True)\r\nprint(f'{unidims2=}')\r\nprint(f'{unidims2.shape=}')\r\nunidims3 = unique_dimensions(a, last_dim=6, unordered=True)\r\nprint(f'{unidims3=}')\r\nprint(f'{unidims3.shape=}')\r\n\r\n# unidims=array([[[[[[0.000e+00, 6.480e+02, 1.296e+03, 1.944e+03],\r\n#            [2.160e+02, 8.640e+02, 1.512e+03, 2.160e+03],\r\n#            [4.320e+02, 1.080e+03, 1.728e+03, 2.376e+03]],\r\n#           [[5.400e+01, 7.020e+02, 1.350e+03, 1.998e+03],\r\n#            [2.700e+02, 9.180e+02, 1.566e+03, 2.214e+03],\r\n#            [4.860e+02, 1.134e+03, 1.782e+03, 2.430e+03]],\r\n#           [[1.080e+02, 7.560e+02, 1.404e+03, 2.052e+03],\r\n#            [3.240e+02, 9.720e+02, 1.620e+03, 2.268e+03],\r\n#            [5.400e+02, 1.188e+03, 1.836e+03, 2.484e+03]],\r\n#           [[1.620e+02, 8.100e+02, 1.458e+03, 2.106e+03],\r\n#            [3.780e+02, 1.026e+03, 1.674e+03, 2.322e+03],\r\n#            [5.940e+02, 1.242e+03, 1.890e+03, 2.538e+03]]],\r\n#          [[[1.800e+01, 6.660e+02, 1.314e+03, 1.962e+03],\r\n#            [2.340e+02, 8.820e+02, 1.530e+03, 2.178e+03],\r\n#            [4.500e+02, 1.098e+03, 1.746e+03, 2.394e+03]],\r\n#           [[7.200e+01, 7.200e+02, 1.368e+03, 2.016e+03],\r\n#            [2.880e+02, 9.360e+02, 1.584e+03, 2.232e+03],\r\n#            [5.040e+02, 1.152e+03, 1.800e+03, 2.448e+03]],\r\n#           [[1.260e+02, 7.740e+02, 1.422e+03, 2.070e+03],\r\n#            [3.420e+02, 9.900e+02, 1.638e+03, 2.286e+03],\r\n#            [5.580e+02, 1.206e+03, 1.854e+03, 2.502e+03]],\r\n#           [[1.800e+02, 8.280e+02, 1.476e+03, 2.124e+03],\r\n#            [3.960e+02, 1.044e+03, 1.692e+03, 2.340e+03],\r\n#            [6.120e+02, 1.260e+03, 1.908e+03, 2.556e+03]]],\r\n# ......\r\n    \r\n```\r\n",
    "bugtrack_url": null,
    "license": "GPL3",
    "summary": "Finds unique dimensions in multi-dimensional arrays, compresses and decompresses arrays ...",
    "version": "0.10",
    "project_urls": {
        "Homepage": "https://github.com/hansalemaos/cythonuniquedim"
    },
    "split_keywords": [
        "cython",
        "arrays"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "7ec9c2ed85ab27102df732288960fe4203433b212554bfe563504802b58a0811",
                "md5": "0f8a15a4e972e0b358d1cb2d2db53671",
                "sha256": "d585e50c5655d8009cc127ccfc26cab2a31bf2258c31f8efb53c64153b5c89f2"
            },
            "downloads": -1,
            "filename": "cythonuniquedim-0.10-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "0f8a15a4e972e0b358d1cb2d2db53671",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 81330,
            "upload_time": "2023-12-09T04:16:33",
            "upload_time_iso_8601": "2023-12-09T04:16:33.570825Z",
            "url": "https://files.pythonhosted.org/packages/7e/c9/c2ed85ab27102df732288960fe4203433b212554bfe563504802b58a0811/cythonuniquedim-0.10-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "816234e439eadfbfcb6bbb016e6c6073fa1fd7aac55b4cafae6a0b9d70aff6dd",
                "md5": "614818e8457ccc90f844acfd83e61119",
                "sha256": "c8b8ad53f5e1b23c115165d5d6f681a2ee8db60b1155b4553435ca16d2d8bcd4"
            },
            "downloads": -1,
            "filename": "cythonuniquedim-0.10.tar.gz",
            "has_sig": false,
            "md5_digest": "614818e8457ccc90f844acfd83e61119",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 79719,
            "upload_time": "2023-12-09T04:16:35",
            "upload_time_iso_8601": "2023-12-09T04:16:35.173343Z",
            "url": "https://files.pythonhosted.org/packages/81/62/34e439eadfbfcb6bbb016e6c6073fa1fd7aac55b4cafae6a0b9d70aff6dd/cythonuniquedim-0.10.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-12-09 04:16:35",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "hansalemaos",
    "github_project": "cythonuniquedim",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "requirements": [],
    "lcname": "cythonuniquedim"
}
        
Elapsed time: 0.20265s