Code Monkey home page Code Monkey logo

arrex's Introduction

Arrex

support-version PyPI version shields.io Documentation Status

Arrex is a module that allows to create typed arrays much like numpy.ndarray and array.array, but resizeable and using any kind of element, not only numbers. Its dtype system is extremely flexible and makes it ideal to work and share structured data with compiled code.

The elements can be many different things, there is just 2 requirements:

  • they must be of a fixed binary size
  • they must be byte copiable, therefore without any reference or pointer to something else

interests

  • much smaller memory footprint (an arrex array is at most 30x smaller than pure python data storage)
  • content can be directly shared with compiled code which improves computation performances
  • slice & view without a copy
  • compatible with standard python libraries

basic usage:

>>> from arrex import *
>>> a = typedlist([
			myclass(...), 
			myclass(...),
			], dtype=myclass)
>>> a[0]
myclass(...)

in that example, myclass can be a primitive numpy type, like np.float64

>>> import arrex.numpy		# this is enabling numpy dtypes for arrex
>>> typedlist(dtype=np.float64)

it can be a more complex type, from module pyglm for instance

>>> import arrex.glm		# this is enabling glm dtypes for arrex
>>> typedlist(dtype=glm.vec4)

typedlist is a dynamically sized, borrowing array, which mean the internal buffer of data is reallocated on append, but can be used to view and extract from any buffer without a copy.

Use it as a list:

>>> a = typedlist(dtype=vec3)

# build from an iterable
>>> a = typedlist([], dtype=vec3)

# append some data
>>> a.append(vec3(1,2,3))

# extend with an iterable
>>> a.extend(vec3(i)  for i in range(5))

>>> len(a)	# the current number of elements
6

>>> a.owner	# the current data buffer
b'.........'

>>> a[0]
vec3(1,2,3)

Use it as a slice:

>>> myslice = a[:5]		# no data is copied
typedlist(....)

Use it as a view on top of a random buffer

>>> a = np.ones((6,3), dtype='f4')
>>> myslice = typedlist(a, dtype=vec3)

buffer protocol

It does support the buffer protocol, so it can be converted into a great variety of well known arrays, even without any copy

>>> np.array(typedlist([....]))

Which dtype is allowed

answer is: whatever you want, but here is some examples:

# an extension type, previously declared
typedlist(dtype=glm.vec3)

# a Struct
typedlist(dtype='ffxI')

# a ctype
typedlist(dtype=ctypes.c_int*5)

# a pure python class
class test_class:
    __packlayout__ = 'ff'
    _struct = struct.Struct(__packlayout__)

    def __init__(self, x, y):
        self.x = x
        self.y = y

    def __bytes__(self):
        return self._struct.pack(self.x, self.y)
    @classmethod
    def frombytes(cls, b):
        return cls(*cls._struct.unpack(b))

typedlist(dtype=test_class)

# and so much more !

performances

Time performances comparison between list, numpy.ndarray, and arrex.typedlist (see benchmark )

execution time (s) for 10k elements (dvec3)

set item
  list:         7.9847e-04 s
  numpy:        1.2727e-02 s
  arrex:        1.0481e-03 s  (10x faster than numpy)

get item
  creation:     1.0655e-03 s
  list:         5.1503e-04 s
  numpy:        1.8619e-03 s
  arrex:        8.0111e-04 s   (2x faster than numpy)

Roadmap

There is additionnal features planned, but no precise schedul yet:

  • typedarray

    a n-dim array view much like numpy arrays but using dtypes as in typedlist. Its purpose is mostly to access its items with n-dim indices and slices.

  • a ufunc system

    to collect and put defaults to any kind of array scale operations, like __add__, __mul__, __matmul__, ... The goal would be to have a standard way to apply any function to every element of one or more array, that defaults to the python implementation, but can be overloaded with a compiled implementation

  • maybe

    even extend to the complete API of numcy

arrex's People

Contributors

jimy-byerley avatar

Stargazers

Jeffrey Wardman avatar Antonio Jose Jimeno Yepes avatar Lev avatar felix-wang avatar edwincheong avatar Han Xiao avatar San Kilkis avatar  avatar  avatar

Watchers

James Cloos avatar Antonio Jose Jimeno Yepes avatar  avatar  avatar

arrex's Issues

Fails to build on Windows 10 (Using VS compiler)

I was installing MadCad via pip on PowerShell, when this dependency generated a build error.

arrex/dtypes.c(5259): error C2036: 'void *': unknown size

It appears that is issue is that you're doing arithmetic on a void type pointer, this isn't allowed by the c standard due to the fact that void types have no known size. Although gcc allows this, which is why this was likely not detected before.

`UnicodeDecodeError` during `pip` install

from @wociety

Hello.
I'm a newbie. Thanks for providing such a wonderful library. I would like to try pymadcad.
However, when trying to install pymadcad using pip in a Windows environment, the following error occurs.

Preparing metadata (setup.py) ... error
error: subprocess-exited-with-error

× python setup.py egg_info did not run successfully.
│ exit code: 1
╰─> [7 lines of output]
Traceback (most recent call last):
File "", line 2, in
File "", line 34, in
File "C:\Users\wocie\AppData\Local\Temp\pip-install-4_jccb79\arrex_a51c1d5dec5b445daf2e2749254cc72c\setup.py", line 40, in
long_description = open('README.md').read(),
^^^^^^^^^^^^^^^^^^^^^^^^
UnicodeDecodeError: 'cp949' codec can't decode byte 0xe2 in position 3861: illegal multibyte sequence
[end of output]

note: This error originates from a subprocess, and is likely not a problem with pip.
error: metadata-generation-failed

I guess this is an error reading the readme file. Is there any good way to solve this problem?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.