Skip to content

Commit 2241e3c

Browse files
committed
fix and tests for Division by zero #123
1 parent 310d122 commit 2241e3c

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

.github/workflows/ci.yml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,14 @@ jobs:
5454
- name: Build strling help
5555
run: ./strling -h
5656

57-
- name: Unit tests
57+
- name: Run Nim unit tests
5858
run: nimble test -y
5959

60+
- name: Run Python doctests
61+
run: |
62+
conda env update -f environment.yml
63+
conda run -n strling python -m doctest scripts/strling-outliers.py
64+
6065
integration-e2e:
6166
# Only run end-to-end tests on pushes to master to keep PRs fast
6267
if: github.ref == 'refs/heads/master' || github.event_name == 'workflow_dispatch'

scripts/strling-outliers.py

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,11 +141,30 @@ def z_score(x, df):
141141
return (x - df['mu'][:,np.newaxis])/df['sd'][:,np.newaxis]
142142

143143
def p_adj_bh(x):
144-
'''Adjust p values using Benjamini/Hochberg method'''
145-
# Mask out nan values as they cause the multiptests algorithm to return all nan
144+
'''Adjust p values using Benjamini/Hochberg method
145+
146+
Tests
147+
-----
148+
>>> import numpy as np
149+
>>> p_adj_bh(np.array([np.nan, np.nan]))
150+
array([nan, nan])
151+
>>> p_adj_bh(np.array([np.inf, -np.inf]))
152+
array([ inf, -inf])
153+
>>> p_adj_bh(np.array([]))
154+
array([], dtype=float64)
155+
>>> out = p_adj_bh(np.array([0.01, np.nan, 0.05]))
156+
>>> bool(np.isclose(out[0], 0.03, atol=0.01))
157+
True
158+
>>> bool(np.isnan(out[1]))
159+
True
160+
>>> bool(np.isclose(out[2], 0.05, atol=0.01))
161+
True
162+
'''
146163
mask = np.isfinite(x)
147164
pval_corrected = x.copy()
148-
pval_corrected[mask] = multipletests(x[mask], method='fdr_bh', returnsorted = False)[1]
165+
if not np.any(mask) or np.sum(mask) < 1: # If no finite values, or less than 1, just return the input unchanged
166+
return pval_corrected
167+
pval_corrected[mask] = multipletests(x[mask], method='fdr_bh', returnsorted=False)[1]
149168
return pval_corrected
150169

151170
def glob_list(l):

0 commit comments

Comments
 (0)