omnetpypy.utilities

A set of utility classes and functions that are used across the package.

class omnetpypy.utilities.FutureMetric(name, vector, mean, median, std, var, min, max, count, percentiles, type, columns)

Bases: tuple

A type that describes the metrics to collect during simulations. The fields, apart from the name, are booleans indicating the statistics to collect for the metric. Such fields are:

  • vector: the complete list of samples of the metric

  • mean: the mean of the values

  • median: the median of the values

  • std: the standard deviation of the values

  • var: the variance of the values

  • min: the minimum value

  • max: the maximum value

  • count: the number of samples

  • percentiles: the percentiles of the values (1, 5, 25, 75, 95, 99)

columns

Alias for field number 11

count

Alias for field number 8

max

Alias for field number 7

mean

Alias for field number 2

median

Alias for field number 3

min

Alias for field number 6

name

Alias for field number 0

percentiles

Alias for field number 9

std

Alias for field number 4

type

Alias for field number 10

var

Alias for field number 5

vector

Alias for field number 1

class omnetpypy.utilities.MultiRandom(seeds=44)[source]

Bases: Random

This class is a wrapper around the standard library random.Random class, that allows to use multiple random number generators with different seeds.

Parameters:
seedslist or int

A list of seeds for the random number generators. The length of the list determines the number of generators. If a single integer is provided, a single generator is created.

betavariate(alpha, beta, generator=0)[source]

Return a random floating point number N from a beta distribution.

Parameters:
alphafloat

The first shape parameter of the distribution. Can be any positive number.

betafloat

The second shape parameter of the distribution. Can be any positive number.

generatorint, optional

The index of the generator to use. Defaults to 0.

Returns:
float

The next random floating point number.

See also

random.Random.betavariate()
choice(seq, generator=0)[source]

Return a random element from the non-empty sequence seq.

Parameters:
seqiterable

A non-empty sequence.

generatorint, optional

The index of the generator to use. Defaults to 0.

Returns:
object

A random element from the sequence.

See also

random.Random.choice()
choices(sequence, weights=None, cum_weights=None, k=1, generator=0)[source]

Return a k sized list of elements chosen from the population with replacement.

Parameters:
sequenceiterable

A non-empty sequence.

weightslist, optional

A list of weights to be used in the selection. If None, the weights are assumed to be equal. The relative weights determine the probability of selecting each element.

cum_weightslist, optional

A list of cumulative weights to be used in the selection. If None, the weights are assumed to be equal. The relative weights determine the probability of selecting each element.

kint, optional

The number of elements to choose. Defaults to 1.

generatorint, optional

The index of the generator to use. Defaults to 0.

Returns:
list

A list of k elements chosen from the population.

See also

random.Random.choices()
expovariate(lambd=1.0, generator=0)[source]

Return a random floating point number N from a normal (Gaussian) distribution.

Parameters:
lambdfloat

The rate of the exponential distribution.

generatorint, optional

The index of the generator to use. Defaults to 0.

Returns:
float

The next random floating point number.

See also

random.Random.expovariate()
gammavariate(alpha, beta, generator=0)[source]

Return a random floating point number N from a gamma distribution.

Parameters:
alphafloat

The shape of the distribution. Can be any positive number.

betafloat

The scale of the distribution. Can be any positive number.

generatorint, optional

The index of the generator to use. Defaults to 0.

Returns:
float

The next random floating point number.

See also

random.Random.gammavariate()
gauss(mu=0.0, sigma=1.0, generator=0)[source]

Return a random floating point number N from a normal (Gaussian) distribution.

Parameters:
mufloat

The mean of the distribution.

sigmafloat

The standard deviation of the distribution.

generatorint, optional

The index of the generator to use. Defaults to 0.

Returns:
float

The next random floating point number.

See also

random.Random.gauss()
geometric(p, size=None, generator=0)[source]

Return a random integer N from a geometric distribution.

This is the only method that uses the numpy random number generator (numpy.random.Generator) instead of the standard library random number generator (random.Random).

Parameters:
pfloat

The probability of success.

sizeint or tuple of ints or None, optional

The shape of the output. If None, a single value is returned.

generatorint, optional

The index of the generator to use. Defaults to 0.

See also

numpy.random.Generator.geometric()
getstate(generator=0)[source]

Return the internal state of the random number generator.

Parameters:
generatorint, optional

The index of the generator to use. Defaults to 0.

See also

random.Random.getstate()
lognormvariate(mu=0.0, sigma=1.0, generator=0)[source]

Return a random floating point number N from a lognormal distribution.

Parameters:
mufloat

The mean of the distribution.

sigmafloat

The standard deviation of the distribution.

generatorint, optional

The index of the generator to use. Defaults to 0.

Returns:
float

The next random floating point number.

See also

random.Random.lognormvariate()
normalvariate(mu=0.0, sigma=1.0, generator=0)[source]

Return a random floating point number N from a normal (Gaussian) distribution.

Parameters:
mufloat

The mean of the distribution.

sigmafloat

The standard deviation of the distribution.

generatorint, optional

The index of the generator to use. Defaults to 0.

Returns:
float

The next random floating point number.

See also

random.Random.normalvariate()
paretovariate(alpha, generator=0)[source]

Return a random floating point number N from a Pareto distribution.

Parameters:
alphafloat

The shape of the distribution. Can be any positive number.

generatorint, optional

The index of the generator to use. Defaults to 0.

Returns:
float

The next random floating point number.

See also

random.Random.parentovariate()
randint(a, b, generator=0)[source]

Return the next random integer N such that a <= N <= b.

Parameters:
aint

The lower bound of the random integer.

bint

The upper bound of the random integer.

generatorint, optional

The index of the generator to use. Defaults to 0.

Returns:
int

The next random integer.

See also

random.Random.randint()
random(generator=0)[source]

Return the next random floating point number uniformly distributed in the range [0.0, 1.0).

Parameters:
generatorint, optional

The index of the generator to use. Defaults to 0.

Returns:
float

The next random floating point number.

See also

random.Random.random()
sample(population, k, generator=0)[source]

Return a k length list of unique elements chosen from the population sequence or set.

Parameters:
populationiterable

A non-empty sequence or set.

kint

The number of unique elements to choose.

generatorint, optional

The index of the generator to use. Defaults to 0.

Returns:
list

A list of unique elements chosen from the population.

See also

random.Random.sample()
setstate(state, generator=0)[source]

Set the internal state of the random number generator.

Parameters:
statetuple

The internal state of the random number generator.

generatorint, optional

The index of the generator to use. Defaults to 0.

See also

random.Random.setstate()
shuffle(x, generator=0)[source]

Shuffle the sequence x in place.

Parameters:
xiterable

The sequence to shuffle.

generatorint, optional

The index of the generator to use. Defaults to 0.

See also

random.Random.shuffle()
triangular(low=0.0, high=0.0, mode=None, generator=0)[source]

Return a random floating point number N such that low <= N <= high and with the specified mode between bounds.

Parameters:
lowfloat

The lower bound of the random number.

highfloat

The upper bound of the random number.

modefloat or None, optional

The mode of the distribution. If None, the mode is the midpoint between the bounds.

generatorint, optional

The index of the generator to use. Defaults to 0.

See also

random.Random.triangular()
uniform(a, b, generator=0)[source]

Return a random floating point number N such that a <= N <= b for a <= b and b <= N <= a for b < a.

Parameters:
afloat

The lower bound of the random number.

bfloat

The upper bound of the random number.

generatorint, optional

The index of the generator to use. Defaults to 0.

Returns:
float

The next random floating point number.

See also

random.Random.uniform()
vonmisesvariate(mu, kappa, generator=0)[source]

Return a random floating point number N from a von Mises distribution.

Parameters:
mufloat

The mean of the distribution.

kappafloat

The concentration of the distribution.

generatorint, optional

The index of the generator to use. Defaults to 0.

Returns:
float

The next random floating point number.

See also

random.Random.vonmisesvariate()
weibullvariate(alpha, beta, generator=0)[source]

Return a random floating point number N from a Weibull distribution.

Parameters:
alphafloat

The shape of the distribution. Can be any positive number.

betafloat

The scale of the distribution. Can be any positive number.

generatorint, optional

The index of the generator to use. Defaults to 0.

Returns:
float

The next random floating point number.

See also

random.Random.weibullvariate()
omnetpypy.utilities.get_metrics(metric, df)[source]

Compute the statistics for a metric and return them as a dictionary. Not used in the current version of the package because it requires all samples to be loaded in memory.

Parameters:
metricFutureMetric

The metric to collect.

dfpandas.DataFrame

The sampled data of the metric.

Returns:
dict

A dictionary containing the requested statistics for the metric.

omnetpypy.utilities.get_metrics_from_csv(metric, filename)[source]

Compute the statistics for a metric and return them as a dictionary.

Parameters:
metricFutureMetric

The metric to collect.

filenamestring

The name of the file containing the sampled data of the metric.

Returns:
dict

A dictionary containing the requested statistics for the metric.

omnetpypy.utilities.time_unit_factor(unit)[source]

Return the factor to convert a time unit to seconds. For example, if the unit is “ms”, the factor is 1e-3.

Parameters:
unitstr

The time unit to convert. Can be one of the following:

  • “s” for seconds

  • “ms” for milliseconds

  • “us” for microseconds

  • “ns” for nanoseconds