~ubuntu-branches/ubuntu/wily/python-networkx/wily

« back to all changes in this revision

Viewing changes to networkx/utils.py

  • Committer: Bazaar Package Importer
  • Author(s): Cyril Brulebois
  • Date: 2008-03-02 01:06:32 UTC
  • mfrom: (1.2.1 upstream) (3.1.3 hardy)
  • Revision ID: james.westby@ubuntu.com-20080302010632-1lp6qe1orf59jl8b
* debian/control:
   + Replace python-setuptools with python-pkg-resources in the
     “Recommends:” since pkg_resources is now available in this
     separate package, thanks Matthias Klose (Closes: #468721).
* debian/copyright:
   + Use “© $years $name” instead of invalid “$name, $years” and
     “(C) $years, $name”, thanks to lintian.

Show diffs side-by-side

added added

removed removed

Lines of Context:
25
25
 
26
26
def is_string_like(obj): # from John Hunter, types-free version
27
27
    """Check if obj is string."""
28
 
    if hasattr(obj, 'shape'): return False # this is a workaround
 
28
#    if hasattr(obj, 'shape'): return False # this is a workaround
29
29
                                       # for a bug in numeric<23.1
30
30
    try:
31
31
        obj + ''
73
73
        if not isinstance(i,int): return False
74
74
    return True
75
75
 
 
76
def _get_fh(path, mode='r'):
 
77
    """ Return a file handle for given path.
 
78
 
 
79
    Path can be a string or a file handle.
 
80
 
 
81
    Attempt to uncompress/compress files ending in '.gz' and '.bz2'.
 
82
 
 
83
    """
 
84
    if is_string_like(path):
 
85
        if path.endswith('.gz'):
 
86
            import gzip
 
87
            fh = gzip.open(path,mode=mode)
 
88
        elif path.endswith('.bz2'):
 
89
            import bz2
 
90
            fh = bz2.BZ2File(path,mode=mode)
 
91
        else:
 
92
            fh = file(path,mode=mode)
 
93
    elif hasattr(path, 'seek'):
 
94
        fh = path
 
95
    else:
 
96
        raise ValueError('path must be a string or file handle')
 
97
    return fh
 
98
 
 
99
 
76
100
##def iterable(obj):
77
101
##  """ Return True if obj is iterable with a well-defined len()"""
78
102
##    try:
86
110
# some helpers for choosing random sequences from distributions
87
111
# uses scipy: www.scipy.org
88
112
 
89
 
def scipy_pareto_sequence(n,**kwds):
 
113
def scipy_pareto_sequence(n,exponent=1.0):
90
114
    """
91
115
    Return sample sequence of length n from a Pareto distribution.
92
116
 
97
121
        print "Import error: not able to import scipy"
98
122
        return
99
123
    random._inst = random.Random()
100
 
    exponent=kwds.get("exponent",1.0)
101
124
    stats.seed(random.randint(1,2**30),random.randint(1,2**30))
102
125
    return stats.pareto(exponent,size=n)
103
126
 
104
127
 
105
 
def scipy_powerlaw_sequence(n,**kwds):
 
128
def scipy_powerlaw_sequence(n,exponent=2.0):
106
129
    """
107
130
    Return sample sequence of length n from a power law distribution.
108
131
 
113
136
        print "Import error: not able to import scipy"
114
137
        return
115
138
    random._inst = random.Random()
116
 
    exponent=kwds.get("exponent",2.0)
117
139
    stats.seed(random.randint(1,2**30),random.randint(1,2**30))
118
140
    return stats.pareto(exponent-1,size=n)
119
141
 
120
142
 
121
 
def scipy_poisson_sequence(n,**kwds):
 
143
def scipy_poisson_sequence(n,mu=1.0):
122
144
    """
123
145
    Return sample sequence of length n from a Poisson distribution.
124
146
 
129
151
        print "Import error: not able to import scipy"
130
152
        return
131
153
    random._inst = random.Random()
132
 
    mu=kwds.get("mu",1.0)
133
154
    stats.seed(random.randint(1,2**30),random.randint(1,2**30))
134
155
    return stats.poisson(mu,size=n)
135
156
 
147
168
    stats.seed(random.randint(1,2**30),random.randint(1,2**30))
148
169
    return stats.uniform(size=n)
149
170
 
150
 
def scipy_discrete_sequence(n,**kwds):
 
171
def scipy_discrete_sequence(n,distribution=False):
151
172
    """
152
173
    Return sample sequence of length n from a given discrete distribution
153
174
 
160
181
        print "Import error: not able to import scipy"
161
182
        return
162
183
    import bisect
163
 
    random._inst = random.Random()
164
 
    p=kwds.get("distribution",False)
165
 
    if p is False:
 
184
    if not distribution:
166
185
        return "no distribution specified"
 
186
    p=distribution
 
187
    random._inst = random.Random()
167
188
 
168
189
    # make CDF out of distribution to use for sample
169
190
    cdf=[]
186
207
# note: gsl's default number generator is the same as Python's
187
208
# (Mersenne Twister)
188
209
 
189
 
def gsl_pareto_sequence(n,**kwds):
 
210
def gsl_pareto_sequence(n,exponent=1.0,scale=1.0,seed=None):
190
211
    """
191
212
    Return sample sequence of length n from a Pareto distribution.
192
213
 
198
219
        return
199
220
    rng=pygsl.rng.rng()
200
221
    random._inst = random.Random()
201
 
    seed=kwds.get("seed",random.randint(1,2**32-1))
 
222
    if seed is None:
 
223
        seed=random.randint(1,2**32-1)
202
224
    rng.set(seed)
203
225
 
204
 
    exponent=kwds.get("exponent",1.0)
205
 
    scale=kwds.get("scale",1.0)
206
226
    return rng.pareto(exponent,scale,n)
207
227
 
208
 
def gsl_powerlaw_sequence(n,**kwds):
 
228
def gsl_powerlaw_sequence(n,exponent=2.0,scale=1.0,seed=None):
209
229
    """
210
230
    Return sample sequence of length n from a power law distribution.
211
231
 
217
237
        return
218
238
    rng=pygsl.rng.rng()
219
239
    random._inst = random.Random()
220
 
    seed=kwds.get("seed",random.randint(1,2**32-1))
 
240
    if seed is None:
 
241
        seed=random.randint(1,2**32-1)
221
242
    rng.set(seed)
222
243
 
223
 
    exponent=kwds.get("exponent",2.0)
224
 
    scale=kwds.get("scale",1.0)
225
244
    return rng.pareto(exponent-1,scale,n)
226
245
 
227
 
def gsl_poisson_sequence(n,**kwds):
 
246
def gsl_poisson_sequence(n,mu=1.0,seed=None):
228
247
    """
229
248
    Return sample sequence of length n from a Poisson distribution.
230
249
 
236
255
        return
237
256
    rng=pygsl.rng.rng()
238
257
    random._inst = random.Random()
239
 
    seed=kwds.get("seed",random.randint(1,2**32-1))
 
258
    if seed is None:
 
259
        seed=random.randint(1,2**32-1)
240
260
    rng.set(seed)
241
261
 
242
 
    mu=kwds.get("mu",1.0)
243
262
    return rng.poisson(mu,n)
244
263
 
245
 
def gsl_uniform_sequence(n,**kwds):
 
264
def gsl_uniform_sequence(n,seed=None):
246
265
    """
247
266
    Return sample sequence of length n from a uniform distribution.
248
267
 
254
273
        return
255
274
    rng=pygsl.rng.rng()
256
275
    random._inst = random.Random()
257
 
    seed=kwds.get("seed",random.randint(1,2**32-1))
 
276
    if seed is None:
 
277
        seed=random.randint(1,2**32-1)
258
278
    rng.set(seed)
259
279
 
260
280
    return rng.uniform(n)
264
284
# uses Python's random module
265
285
# http://www.python.org/doc/current/lib/module-random.html
266
286
 
267
 
def pareto_sequence(n,**kwds):
 
287
def pareto_sequence(n,exponent=1.0):
268
288
    """
269
289
    Return sample sequence of length n from a Pareto distribution.
270
290
    """
271
 
    exponent=kwds.get("exponent",1.0)
272
291
    return [random.paretovariate(exponent) for i in xrange(n)]
273
292
 
274
293
 
275
 
def powerlaw_sequence(n,**kwds):
 
294
def powerlaw_sequence(n,exponent=2.0):
276
295
    """
277
296
    Return sample sequence of length n from a power law distribution.
278
297
    """
279
 
    exponent=kwds.get("exponent",2.0)
280
298
    return [random.paretovariate(exponent-1) for i in xrange(n)]
281
299
 
282
300