-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added basic statistics (mean/sum/var) for SparseNdarrays.
- Loading branch information
Showing
4 changed files
with
397 additions
and
8 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
from typing import List, Tuple | ||
import numpy | ||
|
||
|
||
def _find_useful_axes(ndim, axis) -> List[int]: | ||
output = [] | ||
if axis is not None: | ||
if isinstance(axis, int): | ||
if axis < 0: | ||
axis = ndim + axis | ||
for i in range(ndim): | ||
if i != axis: | ||
output.append(i) | ||
else: | ||
used = set() | ||
for a in axis: | ||
if a < 0: | ||
a = ndim + a | ||
used.add(a) | ||
for i in range(ndim): | ||
if i not in used: | ||
output.append(i) | ||
return output | ||
|
||
|
||
def _expected_sample_size(shape: Tuple[int, ...], axes: List[int]) -> int: | ||
size = 1 | ||
j = 0 | ||
for i, d in enumerate(shape): | ||
if j == len(axes) or i < axes[j]: | ||
size *= d | ||
else: | ||
j += 1 | ||
return size | ||
|
||
|
||
def _choose_output_type(dtype: numpy.dtype, preserve_integer: bool) -> numpy.dtype: | ||
# Mimic numpy.sum's method for choosing the type. | ||
if numpy.issubdtype(dtype, numpy.integer): | ||
if preserve_integer: | ||
xinfo = numpy.iinfo(dtype) | ||
if xinfo.kind == "i": | ||
pinfo = numpy.iinfo(numpy.int_) | ||
if xinfo.bits < pinfo.bits: | ||
dtype = numpy.dtype(numpy.int_) | ||
else: | ||
pinfo = numpy.iinfo(numpy.uint) | ||
if xinfo.bits < pinfo.bits: | ||
dtype = numpy.dtype(numpy.uint) | ||
else: | ||
dtype = numpy.dtype("float64") | ||
return dtype | ||
|
||
|
||
def _create_offset_multipliers(shape: Tuple[int, ...], axes: List[int]) -> List[int]: | ||
multipliers = [0] * len(shape) | ||
sofar = 1 | ||
for a in axes: | ||
multipliers[a] = sofar | ||
sofar *= shape[a] | ||
return multipliers |
Oops, something went wrong.