Skip to content

Commit 32f977f

Browse files
author
jparkpjw
committed
updated scipy deprecations
1 parent ba837c2 commit 32f977f

1 file changed

Lines changed: 27 additions & 26 deletions

File tree

diffnets/exmax.py

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
All other rights reserved.
66
"""
77

8-
from scipy import inf, asarray, array, rand, zeros, prod, where, allclose
8+
#from scipy import inf, asarray, array, rand, zeros, prod, where, allclose
9+
import numpy as np
910
from scipy.stats import pearsonr
1011

1112
def distribution_of_sum(P, ignore_idx=set()):
@@ -22,19 +23,19 @@ def distribution_of_sum(P, ignore_idx=set()):
2223
>>> P = [0.5, 0.25, 0.5]
2324
2425
>>> distribution_of_sum(P)
25-
array([ 0.1875, 0.4375, 0.3125, 0.0625])
26+
np.array([ 0.1875, 0.4375, 0.3125, 0.0625])
2627
2728
Ignoring the 2nd (1 in zero indexing) variable, we have...
2829
>>> distribution_of_sum(P, [1])
29-
array([ 0.25, 0.5 , 0.25, 0. ])
30+
np.array([ 0.25, 0.5 , 0.25, 0. ])
3031
"""
31-
P = asarray(P)
32-
N = P.shape[0]
32+
P = np.asarray(P)
33+
N = np.shape(P)[0]
3334
P1 = 1 - P #convenience variable that is 1 minus P
3435

3536
#starting distribution of the sum (no variables in summation) is D[0] = 1 and D[>0] = 0
3637
#in otherwords, we know the sum is zero.
37-
D = zeros(N + 1)
38+
D = np.zeros(N + 1)
3839
D[0] = 1
3940

4041

@@ -62,7 +63,7 @@ def expectation_range_CUBIC(P, lower, upper):
6263
Output is a vector E of expectations.
6364
O(N^3) time in length of P
6465
65-
>>> R = rand(10) # a random vector of probabilities 10 elements long.
66+
>>> R = np.random.rand(10) # a random vector of probabilities 10 elements long.
6667
>>>
6768
>>> lower, upper = 3, 6
6869
>>>
@@ -75,9 +76,9 @@ def expectation_range_CUBIC(P, lower, upper):
7576
7677
This shows that both versions yield results that are > 99% correlated.
7778
"""
78-
P = asarray(P)
79-
N = P.shape[0]
80-
E = zeros(N)
79+
P = np.asarray(P)
80+
N = np.shape(P)[0]
81+
E = np.zeros(N)
8182
if upper == 0:
8283
return E
8384

@@ -111,10 +112,10 @@ def expectation_range_EXP(P, lower, upper):
111112
This version suffers from floating point error, and should not be used
112113
for anything other than testing.
113114
"""
114-
P = asarray(P)
115-
N = P.shape[0]
115+
P = np.asarray(P)
116+
N = np.shape(P)[0]
116117
P1 = 1 - P
117-
E = zeros(N)
118+
E = np.zeros(N)
118119
if upper == 0:
119120
return E
120121

@@ -127,9 +128,9 @@ def expectation_range_EXP(P, lower, upper):
127128
if SUM >= upper or SUM <= lower:
128129
continue #skip the vectors without the right sum
129130

130-
S = array(S)
131+
S = np.array(S)
131132

132-
p = prod(where(S, P, P1)) #probability of S according to P
133+
p = np.prod(np.where(S, P, P1)) #probability of S according to P
133134

134135
E += p * S #summing up this vector's contribution to the final expectation
135136
D += p #sum up this contribution to the denominator
@@ -147,7 +148,7 @@ def expectation_or_LINEAR(P, E_or):
147148
148149
All the implementations should produce the same results.
149150
150-
>>> R = rand(10)
151+
>>> R = np.random.rand(10)
151152
>>>
152153
>>> EL = expectation_or_LINEAR(R, 1)
153154
>>> EC = expectation_or_CUBIC(R, 1)
@@ -157,7 +158,7 @@ def expectation_or_LINEAR(P, E_or):
157158
>>> correlation > .99 and pvalue < .01
158159
True
159160
160-
>>> allclose(EL, EE)
161+
>>> np.allclose(EL, EE)
161162
True
162163
>>> correlation, pvalue = pearsonr(EL, EE)
163164
>>> correlation > .99 and pvalue < .01
@@ -172,7 +173,7 @@ def expectation_or_LINEAR(P, E_or):
172173
>>> expectation_or([0.5, 0.5], .75)
173174
array([ 0.5, 0.5])
174175
"""
175-
P = asarray(P)
176+
P = np.asarray(P)
176177
# boundary case that is easy to compuate and would cause problems if we
177178
# let it pass through
178179
if any(P == 1):
@@ -182,7 +183,7 @@ def expectation_or_LINEAR(P, E_or):
182183
# others
183184
P1 = P
184185
# probability of failure at this index, but success at one or more others
185-
P0 = (1 - prod(1 - P) / (1 - P)) * (1 - P)
186+
P0 = (1 - np.prod(1 - P) / (1 - P)) * (1 - P)
186187
# given that at least one is success, what is the probability of success
187188
# for this index
188189
PS = P1 / (P1 + P0)
@@ -201,7 +202,7 @@ def expectation_or_CUBIC(P, E_or):
201202
alternate, equivalent implementation for error checking
202203
the problem with this implementation is that it is O(N^3) time
203204
"""
204-
return expectation_range(P, 1, inf) * E_or
205+
return expectation_range(P, 1, np.inf) * E_or
205206

206207

207208
def expectation_E_EXP(P, E_or):
@@ -215,16 +216,16 @@ def expectation_E_EXP(P, E_or):
215216
alternate, equivalent implementation for error checking
216217
the problem with this implementation is that it is exponential time
217218
"""
218-
P = asarray(P)
219+
P = np.asarray(P)
219220
P1 = 1 - P
220-
N = P.shape[0]
221-
E = zeros(N)
221+
N = np.shape(P)[0]
222+
E = np.zeros(N)
222223
import itertools
223224
for S in itertools.product(*tuple([[1, 0]] * N)): #iterate over all binary vectors of length N
224-
S = array(S)
225-
p = prod(where(S, P, P1)) #compute the probability according to P of vector
225+
S = np.array(S)
226+
p = np.prod(np.where(S, P, P1)) #compute the probability according to P of vector
226227
E += p * S #accumulate the probability-weighted average
227-
E = E * E_or / (1 - prod(P1)) #divide by the probability of getting at least 1 success and multiply times E_or
228+
E = E * E_or / (1 - np.prod(P1)) #divide by the probability of getting at least 1 success and multiply times E_or
228229
return E
229230

230231

0 commit comments

Comments
 (0)