Skip to content

Commit d2b2960

Browse files
bberkeyUnabobalis
andauthored
Rhe ignore nans (#254)
Co-authored-by: Nabil Freij <[email protected]>
1 parent b083afa commit d2b2960

File tree

3 files changed

+12
-14
lines changed

3 files changed

+12
-14
lines changed

changelog/254.breaking.rst

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Removed "inplace" method for `sunkit_image.radial.rhef`.

changelog/254.bugfix.rst

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fixed NaN handling in `sunkit_image.radial.rhef`.

sunkit_image/radial.py

+10-14
Original file line numberDiff line numberDiff line change
@@ -101,37 +101,33 @@ def _normalize_fit_radial_intensity(radii, polynomial, normalization_radius):
101101
)
102102

103103
def _select_rank_method(method):
104-
# For now, we have more than one option for ranking the values
105104
def _percentile_ranks_scipy(arr):
106105
from scipy import stats
107106

108-
return stats.rankdata(arr, method="average") / len(arr)
107+
mask = ~np.isnan(arr)
108+
ranks = np.full(arr.shape, np.nan)
109+
ranks[mask] = stats.rankdata(arr[mask], method="average") / np.sum(mask)
110+
return ranks
109111

110112
def _percentile_ranks_numpy(arr):
113+
ranks = arr.copy()
111114
sorted_indices = np.argsort(arr)
112-
ranks = np.empty_like(sorted_indices)
113-
ranks[sorted_indices] = np.arange(1, len(arr) + 1)
114-
return ranks / float(len(arr))
115-
116-
def _percentile_ranks_numpy_inplace(arr):
117-
sorted_indices = np.argsort(arr)
118-
arr[sorted_indices] = np.arange(1, len(arr) + 1)
119-
return arr / float(len(arr))
115+
sorted_indices = sorted_indices[~np.isnan(arr[sorted_indices])]
116+
ranks[sorted_indices] = np.arange(1, len(sorted_indices) + 1)
117+
return ranks / float(len(sorted_indices))
120118

121119
def _pass_without_filtering(arr):
122120
return arr
123121

124122
method = method.lower()
125-
if method == "inplace":
126-
ranking_func = _percentile_ranks_numpy_inplace
127-
elif method == "numpy":
123+
if method == "numpy":
128124
ranking_func = _percentile_ranks_numpy
129125
elif method == "scipy":
130126
ranking_func = _percentile_ranks_scipy
131127
elif method == "none":
132128
ranking_func = _pass_without_filtering
133129
else:
134-
msg = f"{method} is invalid. Allowed values are 'inplace', 'numpy', 'scipy', or 'none'"
130+
msg = f"{method} is invalid. Allowed values are 'numpy', 'scipy', or 'none'"
135131
raise NotImplementedError(msg)
136132
return ranking_func
137133

0 commit comments

Comments
 (0)