Skip to content

Commit

Permalink
Fixed #328, by using the backported as_strided()
Browse files Browse the repository at this point in the history
  • Loading branch information
madsbk committed Oct 13, 2017
1 parent 1cb5a36 commit 5feed5e
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 11 deletions.
3 changes: 2 additions & 1 deletion bridge/npbackend/bohrium/array_manipulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from . import bhary
from . import _util
from .bhary import fix_biclass_wrapper
from . import numpy_backport


@fix_biclass_wrapper
Expand Down Expand Up @@ -154,7 +155,7 @@ def diagonal(ary, offset=0, axis1=0, axis2=1):
ary = ary[..., :diag_size, offset:(offset + diag_size)]

ret_strides = ary.strides[:-2] + (ary.strides[-1] + ary.strides[-2],)
return numpy.lib.stride_tricks.as_strided(ary, shape=ret_shape, strides=ret_strides)
return numpy_backport.as_strided(ary, shape=ret_shape, strides=ret_strides)


@fix_biclass_wrapper
Expand Down
3 changes: 2 additions & 1 deletion bridge/npbackend/bohrium/bhary.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ If not, see <http://www.gnu.org/licenses/>.
import sys
from ._util import dtype_equal, dtype_support, dtype_in
from . import target
from . import numpy_backport
import operator
import functools
import numpy_force as numpy
Expand Down Expand Up @@ -234,7 +235,7 @@ def get_bhc(ary):
# All this is simply a hack to reinterpret 'ary' as a complex view of the 'base'
offset = (get_cdata(ary) - get_cdata(base)) // base.itemsize
cary = numpy.frombuffer(base, dtype=base.dtype, offset=offset * base.itemsize)
cary = numpy.lib.stride_tricks.as_strided(cary, ary.shape, ary.strides, subok=True)
cary = numpy_backport.as_strided(cary, ary.shape, ary.strides)

# if the view/base offset is aligned with the complex dtype, we know that the
# 'ary' is a view of the real part of 'base'
Expand Down
10 changes: 5 additions & 5 deletions bridge/npbackend/bohrium/reorganization.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
"""
import warnings
import numpy_force as numpy
from numpy.lib.stride_tricks import as_strided
from . import bhary
from ._util import is_scalar
from .bhary import fix_biclass_wrapper, get_bhc
from . import target
from . import array_create
from . import array_manipulation
from . import ufuncs
from . import numpy_backport


@fix_biclass_wrapper
Expand Down Expand Up @@ -311,11 +311,11 @@ def put(a, ind, v, mode='raise'):
if indexes.size > values.size:
if values.size == 1:
# When 'values' is a scalar, we can broadcast it to match 'indexes'
values = as_strided(values, shape=indexes.shape, strides=(0,))
values = numpy_backport.as_strided(values, shape=indexes.shape, strides=(0,))
else: # else we repeat 'values' enough times to be larger than 'indexes'
values = as_strided(values,
shape=(indexes.size // values.size + 2, values.size),
strides=(0, values.itemsize))
values = numpy_backport.as_strided(values,
shape=(indexes.size // values.size + 2, values.size),
strides=(0, values.itemsize))
values = array_manipulation.flatten(values, always_copy=False)

# When 'values' is too large, we simple cut the end off
Expand Down
6 changes: 3 additions & 3 deletions bridge/npbackend/bohrium/signal.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
"""
import numpy_force as numpy
from numpy_force.lib.stride_tricks import as_strided
from . import array_create
from . import bhary
from . import ufuncs
from . import linalg
from . import summations
from . import _util
from . import numpy_backport


# 1d
Expand All @@ -34,8 +34,8 @@ def _correlate_and_convolve_body(vector, filter, d, mode):
padded[0:filter.size - 1] = 0
padded[filter.size - 1:vector.size + filter.size - 1] = vector
padded[vector.size + filter.size - 1:] = 0
s = as_strided(padded, shape=(padded.shape[0] - filter.size + 1, filter.size),
strides=(padded.strides[0], padded.strides[0]))
s = numpy_backport.as_strided(padded, shape=(padded.shape[0] - filter.size + 1, filter.size),
strides=(padded.strides[0], padded.strides[0]))
result = linalg.dot(s, filter)
if mode == 'same':
return result[d:vector.size + d]
Expand Down
3 changes: 2 additions & 1 deletion bridge/npbackend/bohrium/target/target_numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"""
from .. import bhc
from .._util import dtype_name
from .. import numpy_backport
import numpy as np
import mmap
import time
Expand Down Expand Up @@ -53,7 +54,7 @@ class View(interface.View):
def __init__(self, ndim, start, shape, strides, base):
super(View, self).__init__(ndim, start, shape, strides, base)
buf = np.frombuffer(self.base.mmap, dtype=self.dtype, offset=self.start)
self.ndarray = np.lib.stride_tricks.as_strided(buf, shape, self.strides)
self.ndarray = numpy_backport.as_strided(buf, shape, self.strides)


def views2numpy(views):
Expand Down

0 comments on commit 5feed5e

Please sign in to comment.