python-igraph manual

For using igraph from Python

   Home       Trees       Indices       Help   
Package igraph :: Module utils
[hide private]

Module utils

source code

Utility functions that cannot be categorised anywhere else.

@undocumented: _is_running_in_ipython


License: Copyright (C) 2006-2012 Tamás Nepusz <ntamas@gmail.com> Pázmány Péter sétány 1/a, 1117 Budapest, Hungary This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

Classes [hide private]
  multidict
A dictionary-like object that is customized to deal with multiple values for the same key.
Functions [hide private]
 
_is_running_in_ipython()
Internal function that determines whether igraph is running inside IPython or not.
source code
 
named_temporary_file(*args, **kwds)
Context manager that creates a named temporary file and returns its name.
source code
 
rescale(values, out_range=(0.0, 1.0), in_range=None, clamp=False, scale=None)
Rescales a list of numbers into a given range.
source code
 
str_to_orientation(value, reversed_horizontal=False, reversed_vertical=False)
Tries to interpret a string as an orientation value.
source code
 
consecutive_pairs(iterable, circular=False)
Returns consecutive pairs of items from the given iterable.
source code
 
safemax(iterable, default=0)
Safer variant of max() that returns a default value if the iterable is empty.
source code
 
safemin(iterable, default=0)
Safer variant of min() that returns a default value if the iterable is empty.
source code
Variables [hide private]
  dbl_epsilon = 2.22044604925e-16
  __package__ = 'igraph'

Imports: contextmanager, MutableMapping, chain, os, tempfile


Function Details [hide private]

named_temporary_file(*args, **kwds)

source code 

Context manager that creates a named temporary file and returns its name.

All parameters are passed on to tempfile.mkstemp, see its documentation for more info.

Decorators:
  • @contextmanager

rescale(values, out_range=(0.0, 1.0), in_range=None, clamp=False, scale=None)

source code 

Rescales a list of numbers into a given range.

out_range gives the range of the output values; by default, the minimum of the original numbers in the list will be mapped to the first element in the output range and the maximum will be mapped to the second element. Elements between the minimum and maximum values in the input list will be interpolated linearly between the first and second values of the output range.

in_range may be used to override which numbers are mapped to the first and second values of the output range. This must also be a tuple, where the first element will be mapped to the first element of the output range and the second element to the second.

If clamp is True, elements which are outside the given out_range after rescaling are clamped to the output range to ensure that no number will be outside out_range in the result.

If scale is not None, it will be called for every element of values and the rescaling will take place on the results instead. This can be used, for instance, to transform the logarithm of the original values instead of the actual values. A typical use-case is to map a range of values to color identifiers on a logarithmic scale. Scaling also applies to the in_range parameter if present.

Examples:

>>> rescale(range(5), (0, 8))
[0.0, 2.0, 4.0, 6.0, 8.0]
>>> rescale(range(5), (2, 10))
[2.0, 4.0, 6.0, 8.0, 10.0]
>>> rescale(range(5), (0, 4), (1, 3))
[-2.0, 0.0, 2.0, 4.0, 6.0]
>>> rescale(range(5), (0, 4), (1, 3), clamp=True)
[0.0, 0.0, 2.0, 4.0, 4.0]
>>> rescale([0]*5, (1, 3))
[2.0, 2.0, 2.0, 2.0, 2.0]
>>> from math import log10
>>> rescale([1, 10, 100, 1000, 10000], (0, 8), scale=log10)
[0.0, 2.0, 4.0, 6.0, 8.0]
>>> rescale([1, 10, 100, 1000, 10000], (0, 4), (10, 1000), scale=log10)
[-2.0, 0.0, 2.0, 4.0, 6.0]

str_to_orientation(value, reversed_horizontal=False, reversed_vertical=False)

source code 

Tries to interpret a string as an orientation value.

The following basic values are understood: left-right, bottom-top, right-left, top-bottom. Possible aliases are:

  • horizontal, horiz, h and lr for left-right
  • vertical, vert, v and tb for top-bottom.
  • lr for left-right.
  • rl for right-left.

reversed_horizontal reverses the meaning of horizontal, horiz and h to rl (instead of lr); similarly, reversed_vertical reverses the meaning of vertical, vert and v to bt (instead of tb).

Returns one of lr, rl, tb or bt, or throws ValueError if the string cannot be interpreted as an orientation.

consecutive_pairs(iterable, circular=False)

source code 

Returns consecutive pairs of items from the given iterable.

When circular is True, the pair consisting of the last and first elements is also returned.

Example:

>>> list(consecutive_pairs(range(5)))
[(0, 1), (1, 2), (2, 3), (3, 4)]
>>> list(consecutive_pairs(range(5), circular=True))
[(0, 1), (1, 2), (2, 3), (3, 4), (4, 0)]
>>> list(consecutive_pairs([]))
[]
>>> list(consecutive_pairs([], circular=True))
[]
>>> list(consecutive_pairs([0]))
[]
>>> list(consecutive_pairs([0], circular=True))
[(0, 0)]

safemax(iterable, default=0)

source code 

Safer variant of max() that returns a default value if the iterable is empty.

Example:

>>> safemax([-5, 6, 4])
6
>>> safemax([])
0
>>> safemax((), 2)
2

safemin(iterable, default=0)

source code 

Safer variant of min() that returns a default value if the iterable is empty.

Example:

>>> safemin([-5, 6, 4])
-5
>>> safemin([])
0
>>> safemin((), 2)
2

   Home       Trees       Indices       Help