-
Notifications
You must be signed in to change notification settings - Fork 4
add copyto, resize, histogram #106
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,9 @@ | |
# Contents of this module ends up in the main namespace via _funcs.py | ||
# where type annotations are used in conjunction with the @normalizer decorator. | ||
|
||
|
||
import builtins | ||
import math | ||
import operator | ||
from typing import Optional, Sequence | ||
|
||
import torch | ||
|
@@ -36,6 +38,13 @@ def copy(a: ArrayLike, order="K", subok: SubokLike = False): | |
return a.clone() | ||
|
||
|
||
def copyto(dst: NDArray, src: ArrayLike, casting="same_kind", where=NoValue): | ||
if where is not NoValue: | ||
raise NotImplementedError | ||
(src,) = _util.typecast_tensors((src,), dst.tensor.dtype, casting=casting) | ||
dst.tensor.copy_(src) | ||
|
||
|
||
def atleast_1d(*arys: ArrayLike): | ||
res = torch.atleast_1d(*arys) | ||
if isinstance(res, tuple): | ||
|
@@ -987,6 +996,65 @@ def tile(A: ArrayLike, reps): | |
return torch.tile(A, reps) | ||
|
||
|
||
def resize(a: ArrayLike, new_shape=None): | ||
# implementation vendored from | ||
# https://github.com/numpy/numpy/blob/v1.24.0/numpy/core/fromnumeric.py#L1420-L1497 | ||
if new_shape is None: | ||
return a | ||
|
||
if isinstance(new_shape, int): | ||
new_shape = (new_shape,) | ||
|
||
a = ravel(a) | ||
|
||
new_size = 1 | ||
for dim_length in new_shape: | ||
new_size *= dim_length | ||
if dim_length < 0: | ||
raise ValueError("all elements of `new_shape` must be non-negative") | ||
|
||
if a.numel() == 0 or new_size == 0: | ||
# First case must zero fill. The second would have repeats == 0. | ||
return torch.zeros(new_shape, dtype=a.dtype) | ||
|
||
repeats = -(-new_size // a.numel()) # ceil division | ||
a = concatenate((a,) * repeats)[:new_size] | ||
|
||
return reshape(a, new_shape) | ||
|
||
|
||
def _ndarray_resize(a: ArrayLike, new_shape, refcheck=False): | ||
# implementation of ndarray.resize. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. my my... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (I wanted to reference the next line) |
||
# NB: differs from np.resize: fills with zeros instead of making repeated copies of input. | ||
if refcheck: | ||
raise NotImplementedError( | ||
f"resize(..., refcheck={refcheck} is not implemented." | ||
) | ||
|
||
if new_shape in [(), (None,)]: | ||
return a | ||
|
||
# support both x.resize((2, 2)) and x.resize(2, 2) | ||
if len(new_shape) == 1: | ||
new_shape = new_shape[0] | ||
if isinstance(new_shape, int): | ||
new_shape = (new_shape,) | ||
|
||
a = ravel(a) | ||
|
||
if builtins.any(x < 0 for x in new_shape): | ||
raise ValueError("all elements of `new_shape` must be non-negative") | ||
|
||
new_numel = math.prod(new_shape) | ||
if new_numel < a.numel(): | ||
# shrink | ||
return a[:new_numel].reshape(new_shape) | ||
else: | ||
b = torch.zeros(new_numel) | ||
b[: a.numel()] = a | ||
return b.reshape(new_shape) | ||
|
||
|
||
# ### diag et al ### | ||
|
||
|
||
|
@@ -1811,3 +1879,47 @@ def common_type(*tensors: ArrayLike): | |
return array_type[1][precision] | ||
else: | ||
return array_type[0][precision] | ||
|
||
|
||
# ### histograms ### | ||
|
||
|
||
def histogram( | ||
a: ArrayLike, | ||
bins: ArrayLike = 10, | ||
range=None, | ||
normed=None, | ||
weights: Optional[ArrayLike] = None, | ||
density=None, | ||
): | ||
if normed is not None: | ||
raise ValueError("normed argument is deprecated, use density= instead") | ||
|
||
is_a_int = not (a.dtype.is_floating_point or a.dtype.is_complex) | ||
is_w_int = weights is None or not weights.dtype.is_floating_point | ||
if is_a_int: | ||
a = a.to(float) | ||
|
||
if weights is not None: | ||
weights = _util.cast_if_needed(weights, a.dtype) | ||
|
||
if isinstance(bins, torch.Tensor): | ||
if bins.ndim == 0: | ||
# bins was a single int | ||
bins = operator.index(bins) | ||
else: | ||
bins = _util.cast_if_needed(bins, a.dtype) | ||
|
||
if range is None: | ||
h, b = torch.histogram(a, bins, weight=weights, density=bool(density)) | ||
else: | ||
h, b = torch.histogram( | ||
a, bins, range=range, weight=weights, density=bool(density) | ||
) | ||
|
||
if not density and is_w_int: | ||
h = h.to(int) | ||
if is_a_int: | ||
b = b.to(int) | ||
|
||
return h, b |
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.