Array is a functional mutable sequence inheriting from Python's built-in list.

Overview

funct.Array

Array is a functional mutable sequence inheriting from Python's built-in list. Array provides 100+ higher-order methods and more functionality to the built-in list, making operations on sequences simpler and one-liners neater with no third party packages required.

Array provides a combination of python built-ins, features found in NumPy arrays, and higher-order methods common to functional languages without the weird semantics of the builtins, still preserving the same functionality and the dynamic nature of the built-in list.

Documentation

funct.Array is available on PyPi and can be installed with pip

$ pip install funct

Array Creation

Arrays can be created either with multiple arguments or by providing a sequence as an argument.

>>> from funct import Array
>>> Array(1, 2, 3)
Array(1, 2, 3)
>>> Array([1, 2, 3])
Array(1, 2, 3)

An Array can also be initialized with the static zeros method or the pad method.

Python built-in sequences (including nested ones) lists, tuples and ranges are converted to Arrays on instantiation. However, other iterables e.g. generators and numpy ndarrays are converted to Arrays only if the argument consists of a single iterable. The elements can be converted to Arrays by calling the toArray method.

>>> Array(np.zeros(3))
Array(0.0, 0.0, 0.0)
>>> Array(np.zeros(3), np.zeros(3))
Array(array([0., 0., 0.]), array([0., 0., 0.])
>>> Array(np.zeros(3), np.zeros(3)).toArray()
Array(Array(0.0, 0.0, 0.0), Array(0.0, 0.0, 0.0))

Arrays provide static methods arange, linspace and logspace for creating linearly or logarithmically spaced Arrays.

Examples

Chaining multiple functions with Arrays result in cleaner code without multiple nested functions, e.g.

a.zip(b).map(func1).filter(func2).forall(func3)

# vs. in traditional python

all(map(func3, filter(func2, map(func1, zip(a, b)))))

where a & b are Arrays and func1, func2 & func3 some functions.

Multiplying elements in a sequence with a constant
#  In traditional python the multiplication could be implemented using list comprehensions as follows
>>> nums = [1, 2, 3, 4, 5]
>>> [a * 10 for a in nums]
[10, 20, 30, 40, 50]

#  With Arrays multiplication simplifies to
>>> from funct import Array
>>> nums = Array(nums)
>>> nums.mul(10)
Array(10, 20, 30, 40, 50)
Multiplying two sequences element-wise
#  Traditional python
>>> nums2 = [11, 12, 13, 14, 15]
>>> [a * b for a, b in zip(nums, nums2)]
[11, 24, 39, 56, 75]

#  With Arrays
>>> nums.mul(nums2)
Array(11, 24, 39, 56, 75)

Same syntax applies for all mathematical operators; add, pow, mod, gt, lt, etc.

Selecting values greater than some number
#  Traditional python
>>> n = 2
>>> nums1 = [1, 2, 3, 4, 5]
>>> [x for x in nums if x > n]
[3, 4, 5]

#  With Arrays
>>> nums[nums > n]
Array(3, 4, 5)
Finding idex-wise maximum of sequences
>>> nums1 = Array(1, 2, 3, 4, 5)
>>> nums2 = Array(5, 4, 3, 2, 1)
>>> nums1.zip(nums2).map(max)
Array(5, 4, 3, 4, 5)
Splitting an Array based on type
>>> arr = Array(1, 2, "a", "b")
>>> arr.groupBy(type)[:, 1]  # group by type and select the 2nd element of the tuples
Array(Array(1, 2), Array('a', 'b'))
Multithreading/processing

Arrays also support parallel and concurrent execution. Functions applied to Arrays can be parallelized with the parmap and parstarmap methods. The same methods can be run asynchronously with the asyncmap and asyncstarmap methods.

>>> Array(1, 2, 3).parmap(some_heavy_func)
>>> Array(1, 2, 3).asyncmap(some_other_func)

Indexing

Array indexing is a combination of standard Python sequence indexing and numpy-style indexing. Array supports

  • Standard Python indexing (single element indexing, slicing)
  • Index arrays
  • Boolean masking
  • Multidimensional indexing

Examples

Standard Indexing
>>> a = Array(1, 2, 3)
>>> a[0]
1
>>> a[:2]
Array(1, 2)
Index Arrays
>>> a = Array('a', 'b', 'c', 'd')
>>> a[[1, 3]]
Array('b', 'd')
Boolean masking
>>> a = Array(1, 2, 3, 4)
>>> a[[True, False, False, True]]
Array(1, 4)
Multidimensional indexing
>>> a = Array((1, 2), (3, 4), (5, 6))
>>> a[:, 0]
Array(1, 3, 5)

Note that when indexing 'ragged' nested Arrays multidimensional indexing may raise an IndexError, since Array does not care whether all the nested Arrays are the same size, as opposed to numpy ndarrays.

Full documentation available here.

Notes

  • Mathematical operations such as addition or multiplication can be done with the add and mul methods, not with the + and * operators to avoid confusion and to retain the behaviour of the built-in list.
  • Inplace operations are postfixed with an underscore (e.g. arr.abs_). However, methods for adding elements to Arrays (append, extend, insert, etc.) are inplace by default. (Note: To be changed. In the next release the operations are inplace if inplace=True is passed to the methods.)
  • Inplace operators are generally faster than out of place operations.
  • Even though Array preserves nearly the same functionality as the built-in list, there are a few differences in their behaviour, the most important of which are
    • == (__eq__) Returns element-wise comparison.
    • bool (__bool__) Returns whether all elements evaluate to True.
    • Arrays are hashable. Note that this is implemented by using the Array's tuple representation in __hash__.
You might also like...
Understanding and Improving Encoder Layer Fusion in Sequence-to-Sequence Learning (ICLR 2021)

Understanding and Improving Encoder Layer Fusion in Sequence-to-Sequence Learning (ICLR 2021) Citation Please cite as: @inproceedings{liu2020understan

[CVPR 2021] Rethinking Semantic Segmentation from a Sequence-to-Sequence Perspective with Transformers
[CVPR 2021] Rethinking Semantic Segmentation from a Sequence-to-Sequence Perspective with Transformers

[CVPR 2021] Rethinking Semantic Segmentation from a Sequence-to-Sequence Perspective with Transformers

Sequence-to-sequence framework with a focus on Neural Machine Translation based on Apache MXNet

Sequence-to-sequence framework with a focus on Neural Machine Translation based on Apache MXNet

Facebook AI Research Sequence-to-Sequence Toolkit written in Python.
Facebook AI Research Sequence-to-Sequence Toolkit written in Python.

Fairseq(-py) is a sequence modeling toolkit that allows researchers and developers to train custom models for translation, summarization, language mod

Sequence-to-Sequence Framework in PyTorch
Sequence-to-Sequence Framework in PyTorch

nmtpytorch allows training of various end-to-end neural architectures including but not limited to neural machine translation, image captioning and au

A highly sophisticated sequence-to-sequence model for code generation

CoderX A proof-of-concept AI system by Graham Neubig (June 30, 2021). About CoderX CoderX is a retrieval-based code generation AI system reminiscent o

Pervasive Attention: 2D Convolutional Networks for Sequence-to-Sequence Prediction

This is a fork of Fairseq(-py) with implementations of the following models: Pervasive Attention - 2D Convolutional Neural Networks for Sequence-to-Se

MASS: Masked Sequence to Sequence Pre-training for Language Generation
MASS: Masked Sequence to Sequence Pre-training for Language Generation

MASS: Masked Sequence to Sequence Pre-training for Language Generation

Sequence-to-Sequence learning using PyTorch

Seq2Seq in PyTorch This is a complete suite for training sequence-to-sequence models in PyTorch. It consists of several models and code to both train

Ptorch NLU, a Chinese text classification and sequence annotation toolkit, supports multi class and multi label classification tasks of Chinese long text and short text, and supports sequence annotation tasks such as Chinese named entity recognition, part of speech tagging and word segmentation.

Pytorch-NLU,一个中文文本分类、序列标注工具包,支持中文长文本、短文本的多类、多标签分类任务,支持中文命名实体识别、词性标注、分词等序列标注任务。 Ptorch NLU, a Chinese text classification and sequence annotation toolkit, supports multi class and multi label classification tasks of Chinese long text and short text, and supports sequence annotation tasks such as Chinese named entity recognition, part of speech tagging and word segmentation.

Code for the paper: Sequence-to-Sequence Learning with Latent Neural Grammars

Code for the paper: Sequence-to-Sequence Learning with Latent Neural Grammars

Sequence to Sequence Models with PyTorch
Sequence to Sequence Models with PyTorch

Sequence to Sequence models with PyTorch This repository contains implementations of Sequence to Sequence (Seq2Seq) models in PyTorch At present it ha

Sequence-to-Sequence learning using PyTorch

Seq2Seq in PyTorch This is a complete suite for training sequence-to-sequence models in PyTorch. It consists of several models and code to both train

Pervasive Attention: 2D Convolutional Networks for Sequence-to-Sequence Prediction

This is a fork of Fairseq(-py) with implementations of the following models: Pervasive Attention - 2D Convolutional Neural Networks for Sequence-to-Se

An implementation of a sequence to sequence neural network using an encoder-decoder
An implementation of a sequence to sequence neural network using an encoder-decoder

Keras implementation of a sequence to sequence model for time series prediction using an encoder-decoder architecture. I created this post to share a

Sequence lineage information extracted from RKI sequence data repo
Sequence lineage information extracted from RKI sequence data repo

Pango lineage information for German SARS-CoV-2 sequences This repository contains a join of the metadata and pango lineage tables of all German SARS-

Official repository of OFA. Paper: Unifying Architectures, Tasks, and Modalities Through a Simple Sequence-to-Sequence Learning Framework
Official repository of OFA. Paper: Unifying Architectures, Tasks, and Modalities Through a Simple Sequence-to-Sequence Learning Framework

Paper | Blog OFA is a unified multimodal pretrained model that unifies modalities (i.e., cross-modality, vision, language) and tasks (e.g., image gene

A NumPy-compatible array library accelerated by CUDA
A NumPy-compatible array library accelerated by CUDA

CuPy : A NumPy-compatible array library accelerated by CUDA Website | Docs | Install Guide | Tutorial | Examples | API Reference | Forum CuPy is an im

Creates a C array from a hex-string or a stream of binary data.

hex2array-c Creates a C array from a hex-string. Usage Usage: python3 hex2array_c.py HEX_STRING [-h|--help] Use '-' to read the hex string from STDIN.

Comments
  • Feature request: chunks

    Feature request: chunks

    First off, thanks for making this great library!

    What do you think of adding a chunks(n) function that splits an Array into n-sized Arrays?

    Something like..

    >>> Array(range(10)).chunks(5)
    Array(Array(0, 1, 2, 3, 4), Array(5, 6, 7, 8, 9))
    

    I'd be happy to contribute this feature as well.

    opened by mcastorina 6
  • Cannot flatten Array of strings

    Cannot flatten Array of strings

    >>> Array('10', Array('20', '30')).flatten()
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "$HOME/.local/lib/python3.9/site-packages/funct/Array.py", line 1009, in flatten
        return r.flatten
      File "$HOME/.local/lib/python3.9/site-packages/funct/Array.py", line 1009, in flatten
        return r.flatten
      File "$HOME/.local/lib/python3.9/site-packages/funct/Array.py", line 1009, in flatten
        return r.flatten
      [Previous line repeated 987 more times]
      File "$HOME/.local/lib/python3.9/site-packages/funct/Array.py", line 1007, in flatten
        r = Array(e for s in self for e in (s if isinstance(s, Iterable) else [s]))
      File "$HOME/.local/lib/python3.9/site-packages/funct/Array.py", line 42, in __init__
        args = list(args[0])
      File "$HOME/.local/lib/python3.9/site-packages/funct/Array.py", line 1007, in <genexpr>
        r = Array(e for s in self for e in (s if isinstance(s, Iterable) else [s]))
      File "/usr/lib/python3.9/abc.py", line 98, in __instancecheck__
        return _abc_instancecheck(cls, instance)
    RecursionError: maximum recursion depth exceeded in comparison
    

    Expected: Array('10', '20', '30') Version: Funct==0.9.2

    opened by mcastorina 1
Releases(v0.9.2)
  • v0.9.2(Feb 3, 2021)

    Release 0.9.2

    • New methods: windows and chunks.
    • inplace keyword for methods (standard for the next release).
    • isFinite returns boolean Array instead of a boolean.
    • Methods with optional keyword arguments as well as "computed" properties i.e. headOption, lastOption, toChar/Int/Str..., (arg)min, (arg)max, any, and all are no longer properties.
    • Warn of bool() of empty Array as it behaves differently from the built-in list.
    • Add FutureWarnings to certain functions as Array is switching to more pythonic naming convention and reserving the underscore postfix for lazy functions in the next release.

    Next release

    • Lazy evaluation.
    • No capital letters in methods.
    Source code(tar.gz)
    Source code(zip)
  • v0.9.1(Jan 10, 2021)

Python collections that are backended by sqlite3 DB and are compatible with the built-in collections

sqlitecollections Python collections that are backended by sqlite3 DB and are compatible with the built-in collections Installation $ pip install git+

Takeshi OSOEKAWA 11 Feb 3, 2022
An esoteric data type built entirely of NaNs.

NaNsAreNumbers An esoteric data type built entirely of NaNs. Installation pip install nans_are_numbers Explanation A floating point number is just co

Travis Hoppe 72 Jan 1, 2023
Standard mutable string (character array) implementation for Python.

chararray A standard mutable character array implementation for Python.

Tushar Sadhwani 3 Dec 18, 2021
Torch-mutable-modules - Use in-place and assignment operations on PyTorch module parameters with support for autograd

Torch Mutable Modules Use in-place and assignment operations on PyTorch module p

Kento Nishi 7 Jun 6, 2022
Rethinking Semantic Segmentation from a Sequence-to-Sequence Perspective with Transformers

Segmentation Transformer Implementation of Segmentation Transformer in PyTorch, a new model to achieve SOTA in semantic segmentation while using trans

Abhay Gupta 161 Dec 8, 2022
Facebook AI Research Sequence-to-Sequence Toolkit written in Python.

Fairseq(-py) is a sequence modeling toolkit that allows researchers and developers to train custom models for translation, summarization, language mod

null 20.5k Jan 8, 2023
Sequence-to-sequence framework with a focus on Neural Machine Translation based on Apache MXNet

Sockeye This package contains the Sockeye project, an open-source sequence-to-sequence framework for Neural Machine Translation based on Apache MXNet

Amazon Web Services - Labs 1.1k Dec 27, 2022
Facebook AI Research Sequence-to-Sequence Toolkit written in Python.

Fairseq(-py) is a sequence modeling toolkit that allows researchers and developers to train custom models for translation, summarization, language mod

null 11.3k Feb 18, 2021
Sequence-to-sequence framework with a focus on Neural Machine Translation based on Apache MXNet

Sockeye This package contains the Sockeye project, an open-source sequence-to-sequence framework for Neural Machine Translation based on Apache MXNet

Amazon Web Services - Labs 986 Feb 17, 2021
Implementation of SETR model, Original paper: Rethinking Semantic Segmentation from a Sequence-to-Sequence Perspective with Transformers.

SETR - Pytorch Since the original paper (Rethinking Semantic Segmentation from a Sequence-to-Sequence Perspective with Transformers.) has no official

zhaohu xing 112 Dec 16, 2022