https://github.com/ivorforce/python-arrayclass

A python class decorator similar to dataclass that uses a contiguous numpy array to store values.

https://github.com/ivorforce/python-arrayclass

Science Score: 26.0%

This score indicates how likely this project is to be science-related based on various indicators:

  • CITATION.cff file
  • codemeta.json file
    Found codemeta.json file
  • .zenodo.json file
    Found .zenodo.json file
  • DOI references
  • Academic publication links
  • Academic email domains
  • Institutional organization owner
  • JOSS paper metadata
  • Scientific vocabulary similarity
    Low similarity (8.1%) to scientific vocabulary

Keywords

dataclass numpy python
Last synced: 6 months ago · JSON representation

Repository

A python class decorator similar to dataclass that uses a contiguous numpy array to store values.

Basic Info
Statistics
  • Stars: 4
  • Watchers: 1
  • Forks: 0
  • Open Issues: 0
  • Releases: 0
Topics
dataclass numpy python
Created almost 3 years ago · Last pushed almost 3 years ago
Metadata Files
Readme License

README.md

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

```py 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

```py @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.

Owner

  • Name: Lukas Tenbrink
  • Login: Ivorforce
  • Kind: user
  • Location: Germany

Computer Scientist at day, Computer Scientist at night.

GitHub Events

Total
  • Watch event: 1
Last Year
  • Watch event: 1

Issues and Pull Requests

Last synced: 6 months ago

All Time
  • Total issues: 0
  • Total pull requests: 0
  • Average time to close issues: N/A
  • Average time to close pull requests: N/A
  • Total issue authors: 0
  • Total pull request authors: 0
  • Average comments per issue: 0
  • Average comments per pull request: 0
  • Merged pull requests: 0
  • Bot issues: 0
  • Bot pull requests: 0
Past Year
  • Issues: 0
  • Pull requests: 0
  • Average time to close issues: N/A
  • Average time to close pull requests: N/A
  • Issue authors: 0
  • Pull request authors: 0
  • Average comments per issue: 0
  • Average comments per pull request: 0
  • Merged pull requests: 0
  • Bot issues: 0
  • Bot pull requests: 0
Top Authors
Issue Authors
Pull Request Authors
Top Labels
Issue Labels
Pull Request Labels

Packages

  • Total packages: 1
  • Total downloads:
    • pypi 52 last-month
  • Total dependent packages: 0
  • Total dependent repositories: 0
  • Total versions: 5
  • Total maintainers: 1
pypi.org: arrayclasses

Analogue to dataclass that uses a numpy-backed array to store values.

  • Versions: 5
  • Dependent Packages: 0
  • Dependent Repositories: 0
  • Downloads: 52 Last month
Rankings
Dependent packages count: 7.3%
Average: 27.9%
Forks count: 30.5%
Stargazers count: 32.5%
Dependent repos count: 41.4%
Maintainers (1)
Last synced: 6 months ago