-
-
Notifications
You must be signed in to change notification settings - Fork 131
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1232 from larrybradley/bkg2d-refactor
Refactor background classes
- Loading branch information
Showing
8 changed files
with
1,043 additions
and
718 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 |
---|---|---|
|
@@ -6,3 +6,4 @@ | |
|
||
from .background_2d import * # noqa | ||
from .core import * # noqa | ||
from .interpolators import * # noqa |
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,77 @@ | ||
# Licensed under a 3-clause BSD style license - see LICENSE.rst | ||
""" | ||
This module defines nan-ignoring statistical functions, using bottleneck | ||
for performance if available. | ||
""" | ||
|
||
import numpy as np | ||
|
||
from ..utils._optional_deps import HAS_BOTTLENECK | ||
|
||
if HAS_BOTTLENECK: | ||
import bottleneck | ||
|
||
|
||
def move_tuple_axes_first(array, axis): | ||
""" | ||
Bottleneck can only take integer axis, not tuple, so this function | ||
takes all the axes to be operated on and combines them into the | ||
first dimension of the array so that we can then use axis=0. | ||
""" | ||
# Figure out how many axes we are operating over | ||
naxis = len(axis) | ||
|
||
# Add remaining axes to the axis tuple | ||
axis += tuple(i for i in range(array.ndim) if i not in axis) | ||
|
||
# The new position of each axis is just in order | ||
destination = tuple(range(array.ndim)) | ||
|
||
# Reorder the array so that the axes being operated on are at the | ||
# beginning | ||
array_new = np.moveaxis(array, axis, destination) | ||
|
||
# Collapse the dimensions being operated on into a single dimension | ||
# so that we can then use axis=0 with the bottleneck functions | ||
array_new = array_new.reshape((-1,) + array_new.shape[naxis:]) | ||
|
||
return array_new | ||
|
||
|
||
def nanmean(array, axis=None): | ||
""" | ||
A nanmean function that uses bottleneck if available. | ||
""" | ||
if HAS_BOTTLENECK: | ||
if isinstance(axis, tuple): | ||
array = move_tuple_axes_first(array, axis=axis) | ||
axis = 0 | ||
return bottleneck.nanmean(array, axis=axis) | ||
else: | ||
return np.nanmean(array, axis=axis) | ||
|
||
|
||
def nanmedian(array, axis=None): | ||
""" | ||
A nanmedian function that uses bottleneck if available. | ||
""" | ||
if HAS_BOTTLENECK: | ||
if isinstance(axis, tuple): | ||
array = move_tuple_axes_first(array, axis=axis) | ||
axis = 0 | ||
return bottleneck.nanmedian(array, axis=axis) | ||
else: | ||
return np.nanmedian(array, axis=axis) | ||
|
||
|
||
def nanstd(array, axis=None, ddof=0): | ||
""" | ||
A nanstd function that uses bottleneck if available. | ||
""" | ||
if HAS_BOTTLENECK: | ||
if isinstance(axis, tuple): | ||
array = move_tuple_axes_first(array, axis=axis) | ||
axis = 0 | ||
return bottleneck.nanstd(array, axis=axis, ddof=ddof) | ||
else: | ||
return np.nanstd(array, axis=axis, ddof=ddof) |
Oops, something went wrong.