Skip to main content

npstreams

Documentation Build Status PyPI Version Conda-forge Version DOI badge

npstreams is an open-source Python package for streaming NumPy array operations. The goal is to provide tested routines that operate on streams (or generators) of arrays instead of dense arrays.

Streaming reduction operations (sums, averages, etc.) can be implemented in constant memory, which in turns allows for easy parallelization.

This approach has been a huge boon when working with lots of images; the images are read one-by-one from disk and combined/processed in a streaming fashion.

This package is developed in conjunction with other software projects in the Siwick research group.

Motivating Example

Consider the following snippet to combine 50 images from an iterable source:

import numpy as np

images = np.empty( shape = (2048, 2048, 50) )
for index, im in enumerate(source):
    images[:,:,index] = im

avg = np.average(images, axis = 2)

If the source iterable provided 1000 images, the above routine would not work on most machines. Moreover, what if we want to transform the images one by one before averaging them? What about looking at the average while it is being computed? Let's look at an example:

import numpy as np
from npstreams import iaverage
from scipy.misc import imread

stream = map(imread, list_of_filenames)
averaged = iaverage(stream)

At this point, the generators map and iaverage are 'wired' but will not compute anything until it is requested. We can look at the average evolve:

import matplotlib.pyplot as plt
for avg in average:
    plt.imshow(avg); plt.show()

We can also use last to get at the final average:

from npstreams import last

total = last(averaged) # average of the entire stream

Streaming Functions

npstreams comes with some streaming functions built-in. Some examples:

  • Numerics : isum, iprod, isub, etc.
  • Statistics : iaverage (weighted mean), ivar (single-pass variance), etc.

More importantly, npstreams gives you all the tools required to build your own streaming function. All routines are documented in the API Reference on readthedocs.io.

Benchmarking

npstreams provides a function for benchmarking common use cases.

To run the benchmark with default parameters, from the interpreter:

from npstreams import benchmark
benchmark()

From a command-line terminal:

python -c 'import npstreams; npstreams.benchmark()'

The results will be printed to the screen.

Future Work

Some of the features I want to implement in this package in the near future:

  • Optimize the CUDA-enabled routines
  • More functions : more streaming functions borrowed from NumPy and SciPy.

API Reference

The API Reference on readthedocs.io provides API-level documentation, as well as tutorials.

Installation

The only requirement is NumPy. To have access to CUDA-enabled routines, PyCUDA must also be installed. npstreams is available on PyPI; it can be installed with pip.:

python -m pip install npstreams

npstreams can also be installed with the conda package manager, from the conda-forge channel:

conda config --add channels conda-forge
conda install npstreams

To install the latest development version from Github:

python -m pip install git+git://github.com/LaurentRDC/npstreams.git

Tests can be run using the pytest package.

Citations

If you find this software useful, please consider citing the following publication:

L. P. René de Cotret, M. R. Otto, M. J. Stern. and B. J. Siwick, An open-source software ecosystem for the interactive exploration of ultrafast electron scattering data, Advanced Structural and Chemical Imaging 4:11 (2018) DOI: 10.1186/s40679-018-0060-y.

Support / Report Issues

All support requests and issue reports should be filed on Github as an issue.

License

npstreams is made available under the BSD License, same as NumPy. For more details, see LICENSE.txt.

Release files for npstreams 1.7.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for npstreams 1.7.0
File Size Uploaded
npstreams-1.7.0.tar.gz 34.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for npstreams 1.7.0
File Interpreter ABI Platform
npstreams-1.7.0-py3-none-any.whl Python 3 none any Details

Total release size: 74.4 kB

Release files / npstreams-1.7.0.tar.gz

Download URL npstreams-1.7.0.tar.gz
Size 34.3 kB
Tags Source
SHA-256 checksum
How to use checksums
af584a5a8b1882161de2a3b25b4bbfbbee85e69da75678f1b470df0c91c157ee
BLAKE2b-256 checksum
How to use checksums
c99accac334d97fc155afd8f0184612fd032719db17027b159783c8cfb868719
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/5.1.1 CPython/3.12.7

Release files / npstreams-1.7.0-py3-none-any.whl

Download URL npstreams-1.7.0-py3-none-any.whl
Size 40.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
bb6d1a1de26274b23c4979eb81925697a80d48199d2a4a54878473158ca1ccb6
BLAKE2b-256 checksum
How to use checksums
5c5671d7c2e2a8c0654a2ea6836a004f01daac66250a571920f13b0d279b14ec
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.12.7

Release history Release notifications | RSS feed

This release

1.7.0 This release

2 release files

1.6.6

5 release files

1.6.5

2 release files

1.6.4

2 release files

1.6.3

2 release files

1.6.2

2 release files

1.6.1

2 release files

1.6

2 release files

1.5.2

2 release files

1.5.1

2 release files

1.5.0

2 release files

1.4.0

2 release files

1.3.0

2 release files

1.2.0

2 release files

1.1.0

1 release file

1.0.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page