math

pilot.util.math.add_lists(list1, list2)[source]

Add list1 and list2 and remove any duplicates. Example: list1=[1,2,3,4] list2=[3,4,5,6] add_lists(list1, list2) = [1, 2, 3, 4, 5, 6]

Parameters:
  • list1 – input list 1

  • list2 – input list 2

Returns:

added lists with removed duplicates

pilot.util.math.bytes2human(n, _format='%(value).1f %(symbol)s', symbols='customary')[source]

Convert n bytes into a human readable string based on format. symbols can be either “customary”, “customary_ext”, “iec” or “iec_ext”, see: http://goo.gl/kTQMs

>>> bytes2human(0)
'0.0 B'
>>> bytes2human(0.9)
'0.0 B'
>>> bytes2human(1)
'1.0 B'
>>> bytes2human(1.9)
'1.0 B'
>>> bytes2human(1024)
'1.0 K'
>>> bytes2human(1048576)
'1.0 M'
>>> bytes2human(1099511627776127398123789121)
'909.5 Y'
>>> bytes2human(9856, symbols="customary")
'9.6 K'
>>> bytes2human(9856, symbols="customary_ext")
'9.6 kilo'
>>> bytes2human(9856, symbols="iec")
'9.6 Ki'
>>> bytes2human(9856, symbols="iec_ext")
'9.6 kibi'
>>> bytes2human(10000, "%(value).1f %(symbol)s/sec")
'9.8 K/sec'
>>> # precision can be adjusted by playing with %f operator
>>> bytes2human(10000, _format="%(value).5f %(symbol)s")
'9.76562 K'
pilot.util.math.chi2(observed, expected)[source]

Return the chi2 sum of the provided observed and expected values.

Parameters:
  • observed – list of floats.

  • expected – list of floats.

Returns:

chi2 (float).

pilot.util.math.convert_mb_to_b(size)[source]

Convert value from MB to B for the given size variable. If the size is a float, the function will convert it to int.

Parameters:

size – size in MB (float or int).

Returns:

size in B (int).

Raises:

ValueError for conversion error.

pilot.util.math.diff_lists(list_a, list_b)[source]

Return the difference between list_a and list_b.

Parameters:
  • list_a – input list a.

  • list_b – input list b.

Returns:

difference (list).

pilot.util.math.float_to_rounded_string(num, precision=3)[source]

Convert float to a string with a desired number of digits (the precision). E.g. num=3.1415, precision=2 -> ‘3.14’.

Parameters:
  • num – number to be converted (float).

  • precision – number of desired digits (int)

Raises:

NotDefined – for undefined precisions and float conversions to Decimal.

Returns:

rounded string.

pilot.util.math.human2bytes(s, divider=None)[source]

Attempts to guess the string format based on default symbols set and return the corresponding bytes as an integer. When unable to recognize the format ValueError is raised.

If no digit passed, only a letter, it is interpreted as a one of a kind. Eg “KB” = “1 KB”. If no letter passed, it is assumed to be in bytes. Eg “512” = “512 B”

The second argument is used to convert to another magnitude (eg return not bytes but KB). It can be interpreted as a cluster size. Eg “512 B”, or “0.2 K”.

>>> human2bytes('0 B')
0
>>> human2bytes('3')
3
>>> human2bytes('K')
1024
>>> human2bytes('1 K')
1024
>>> human2bytes('1 M')
1048576
>>> human2bytes('1 Gi')
1073741824
>>> human2bytes('1 tera')
1099511627776
>>> human2bytes('0.5kilo')
512
>>> human2bytes('0.1  byte')
0
>>> human2bytes('1 k')  # k is an alias for K
1024
>>> human2bytes('12 foo')
Traceback (most recent call last):
    ...
ValueError: can't interpret '12 foo'
>>> human2bytes('1 M', 'K')
1024
>>> human2bytes('2 G', 'M')
2048
>>> human2bytes('G', '2M')
512
pilot.util.math.is_greater_or_equal(a, b)[source]

Is the numbered string a >= b? “1.2.3” > “1.2” – more digits “1.2.3” > “1.2.2” – rank based comparison “1.3.2” > “1.2.3” – rank based comparison “1.2.N” > “1.2.2” – nightlies checker, always greater

Parameters:
  • a – numbered string.

  • b – numbered string.

Returns:

boolean.

pilot.util.math.mean(data)[source]

Return the sample arithmetic mean of data.

Parameters:

data – list of floats or ints.

Returns:

mean value (float).

pilot.util.math.split_version(s)[source]

Split version string into parts and convert the parts into integers when possible. Any encountered strings are left as they are. The function is used with release strings. split_version(“1.2.3”) = (1,2,3) split_version(“1.2.Nightly”) = (1,2,”Nightly”)

The function can also be used for sorting: > names = [‘YT4.11’, ‘4.3’, ‘YT4.2’, ‘4.10’, ‘PT2.19’, ‘PT2.9’] > sorted(names, key=splittedname) [‘4.3’, ‘4.10’, ‘PT2.9’, ‘PT2.19’, ‘YT4.2’, ‘YT4.11’]

Parameters:

s – release string.

Returns:

converted release tuple.

pilot.util.math.sum_dev(x, y)[source]

Return sum of deviations of sequence data. Sum (x - x_mean)**(y - y_mean)

Parameters:
  • x – list of ints or floats.

  • y – list of ints or floats.

Returns:

sum of deviations (float).

pilot.util.math.sum_square_dev(data)[source]

Return sum of square deviations of sequence data. Sum (x - x_mean)**2

Parameters:

data – list of floats or ints.

Returns:

sum of squares (float).

pilot.util.math.tryint(x)[source]

Used by numbered string comparison (to protect against unexpected letters in version number).

Parameters:

x – possible int.

Returns:

converted int or original value in case of ValueError.