Giter Club home page Giter Club logo

python-arrayclass's Introduction

arrayclass

A small @dataclass-like decorator for python classes. The class will store its values in a single contiguous numpy array. It can also be converted to and from plain numpy arrays.

Installation

poetry add dataclasses or pip install dataclasses

Usage

def simulate(steps: int, fs: float) -> np.ndarray:
    # Define the state class.
    # Imagine we had 20 variables here...
    @arrayclasses.arrayclass
    class State:
        x: float
        y: float
    
    def step(t, xy):
        # normally, this would be `x, y, ... = xy`
        s = arrayclasses.from_array(State, xy)
        a = 1 - np.sqrt(s.x ** 2 + s.y ** 2)
        w = 2 * np.pi / (1 * fs)
        
        # normally, this would be `return (..., ...)`
        return State(
            x=a * s.x - w * s.y,
            y=a * s.y + w * s.x,
        )
    
    solved = integrate.solve_ivp(
        fun=step,
        y0=State(-1, 0),
        t_span=(0, steps),
        method="RK45"
    )
    return solved.y

Features

@arrayclasses.arrayclass(dtype=object)  # You can coerce the array dtype manually
class Object:
    x: int  # A single value.
    y: tuple[int, int]  # Will yield np.ndarray windows, not tuples. Should be np.ndarray[float, ...] but requires PEP 646 to work.

a = Object(x=5, y=(2, 3))
print(len(a))  # 3
print(tuple(a))  # (5, 2, 3)

Why would I need this?

You may be forced, or inclined, to use numpy arrays in some situations where classes would be more appropriate.

An example might be scipy.integrate - You are working with an array of numbers that really wants to be a class.

Packing and unpacking tuples is a common workaround. However, when you approach 10 or 20 variables, this gets quite messy fast. Now, you might prefer to use an @arrayclass to get nicer code that plays well with your IDE.

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.