Author: | Sebastian Berg |
---|---|
Contact: | [email protected] |
Date: | 2015-07-14 |
Status: | draft |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
from collections import defaultdict as _dd | |
def rolling_window(array, window=(0,), asteps=None, wsteps=None, axes=None, toend=True): | |
"""Create a view of `array` which for every point gives the n-dimensional | |
neighbourhood of size window. New dimensions are added at the end of | |
`array` or after the corresponding original dimension. | |
Parameters | |
---------- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""Impressive little thing how einsum + stride_tricks can beat numpys build in C functions | |
for correlate (for large data). (ok depending on the implementation of np.correlate, the | |
comparison is not fair, but still rather impressive that you can get comparable speeds) | |
""" | |
import numpy as np | |
import stride_tricks as st # stride_tricks.py gist | |
a = np.random.random((100,100,100)).ravel() | |
stamp = np.random.random((3,3,3)).ravel() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def rolling_window(array, window=(0,), asteps=None, wsteps=None, axes=None, toend=True): | |
"""Create a view of `array` which for every point gives the n-dimensional | |
neighbourhood of size window. New dimensions are added at the end of | |
`array` or after the corresponding original dimension. | |
Parameters | |
---------- | |
array : array_like | |
Array to which the rolling window is applied. | |
window : int or tuple |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
from numpy import asarray, add, rollaxis, sort, arange | |
def percentile(a, q, limit=None, interpolation='linear', axis=None, | |
out=None, overwrite_input=False): | |
""" | |
Compute the qth percentile of the data along the specified axis. | |
Returns the qth percentile of the array elements. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
def object_einsum(string, *arrays): | |
"""Simplified object einsum, not as much error checking | |
does not support "..." or list input and will see "...", etc. as three times | |
an axes identifier, tries normal einsum first! | |
NOTE: This is untested, and not fast, but object type is | |
never really fast anyway... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# A Map reduce made from ufuncs and with nditer, a C-Api version | |
# would be very similar (but cleaner in some cases). This still creates | |
# some unnecessary temporaries, which may or may not slow down things a | |
# a bit for no reason | |
# | |
# WARNING : Mostly untested and I am not sure I would use it except to | |
# get the idea of how to implement a specialized version maybe. | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Simply run the script to try to guess some information about how numpy | |
is linked. | |
If there is something odd going on, run/import the script after | |
your real import of numpy. | |
All versions tested on Linux, MKL is confusing me a bit since both lower and | |
upper case versions exist. |
This table includes the promotion rules for the basic numeric types. The only suprising rule is that 8-byte integers are considered to cast "safely" to f8 (and complex), which also shows up in these promotion rules (If there was an int128, it would be allowed to safely cast to float128!):
i1 | u1 | i2 | u2 | i4 | u4 | i8 | u8 | f2 | f4 | f8 | f16 | c8 | c16 | c32 |
---|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import operator | |
def normalize_index(index, ndim): | |
"""Does basic index normalization like numpy, disregards advanced indexing. | |
Parameters | |
---------- | |
index : tuple | |
Indexing tuple to normalize | |
ndim : int |
OlderNewer