radis.misc.basics module

Created on Wed Nov 5 12:59:37 2014.

@author: Erwan Small functions used in other procedures ——————————————————————————-

all_in(keys, L)[source]

Returns whether all items in keys are in list L.

any_in(keys, L)[source]

Returns whether any of the items in keys are in list L.

compare_dict(d1, d2, verbose='if_different', compare_as_paths=[], return_string=False, df1_str='Left', df2_str='Right', ignore_keys=[])[source]

Returns ratio of equal keys [0-1] If verbose, also print all keys and values on 2 columns :Parameters: d1, d2 (dict) – two dictionaries to compare

Other Parameters
  • compare_as_paths (list of keys) – compare the values corresponding to given keys as path (irrespective of forward / backward slashes, or case )

  • verbose (boolean, or 'if_different') – 'if_different' means results will be shown only if there is a difference.

  • return_string (boolean) – if True, returns message instead of just printing it (useful in error messages) Default False

  • ignore_keys (list) – do not compare these keys

Returns

  • out (float [0-1]) – ratio of matching keys

  • if return_string

  • out, string (float [0-1], str) – ratio of matching keys and comparison message

compare_lists(l1, l2, verbose='if_different', return_string=False)[source]

Compare 2 lists of elements that may not be of the same length, irrespective of order. Returns the ratio of elements [0-1] present in both lists. If verbose, prints the differences :Parameters: * l1, l2 (list-like)

  • verbose (boolean, or ‘if_different’) – ‘if_different’ means results will be shown only if there is a difference. function is called twice

Other Parameters
  • verbose (boolean, or 'if_different') – 'if_different' means results will be shown only if there is a difference.

  • return_string (boolean) – if True, returns message instead of just printing it (useful in error messages) Default False

Returns

out – ratio of matching keys

Return type

float [0-1]

compare_paths(p1, p2)[source]

Compare 2 paths p1 and p2.

exec_file(afile, globalz=None, localz=None)[source]
expand_metadata(df, metadata)[source]

Turn metadata from a float to a column.

For some reason metadata are sometimes not copied when a DataFrame is sliced or copied, even if they explicitely figure in the df.attrs attribute. Here we add them as column before such operations.

Parameters

df (pandas DataFrame) – …

Returns

df modified in place

Return type

None

flatten(*args)[source]

Flatten list of lists of floats.

in_all(key, list_of_list)[source]

Returns true if key is in all lists.

is_float(a)[source]

Returns True if a has float-like type: float, np.float64, np.int64, etc.)

is_list(a)[source]

Returns True if a has list-like type: list, np.array, tuple, set, etc.)

is_number(s)[source]

Return True if s is a number.

Works for strings, floats, int, and is compatible with Python 2/3

key_max_val(d)[source]

Return the dictionary key with max value.

list_if_float(a)[source]
make_folders(path, folders)[source]

Make folders if not there :Parameters: * path (str) – where to create folders

  • folders (list or str) – folders to create

merge_lists(lists)[source]

Merge a list of lists and return a list with unique elements.

merge_rename_columns(df, columns1, columns2, merged_names)[source]

Merge all columns under easier names. Only keep the useful ones Returns a new dataframe :Parameters: * df (pandas Dataframe)

  • columns1 (list) – list of columns names

  • columns2 (list) – list of columns names, whose index match columns 1

  • merged_names (list) – new names

Example

df = merge_rename_columns(df1, [‘lvl_u’, ‘ju’, ‘Eu’, ‘nu’, ‘gu’, ‘grotu’],

[‘lvl_l’, ‘jl’, ‘El’, ‘nl’, ‘gl’, ‘grotl’], [‘lvl’, ‘j’, ‘E’, ‘n’, ‘g’, ‘grot’] )

partition(pred, iterable)[source]

Use a predicate to partition entries into false entries and true entries :returns: Returns two lists :rtype: positive, and negative

Example

>>> partition(is_odd, range(10))
--> [0 2 4 6 8], [1 3 5 7 9]
print_series(a)[source]

Print a pandas series a , explicitely showing all rows.

remove_duplicates(l)[source]

Remove duplicates from a list, without changing the order.

Note that if the order doesn’t matter you could just do set(l)

round_off(n)[source]
stdpath(p)[source]

Convert path p in standard path (irrespective of slash / backslash, or case)

to_str(a)[source]
transfer_metadata(df1, df2, metadata)[source]

Transfer metadata between a DataFrame df1 and df2.

For some reason metadata are sometimes not copied when a DataFrame is sliced or copied, even if they explicitely appear in the df.attrs attribute. See https://github.com/pandas-dev/pandas/issues/28283

Here we copy them back. Attributes can be :
  • keys of the df1.attrs dictionary

  • simple attributes of df1, i.e., df1.X

Parameters
  • df1 (pandas DataFrame) – copy from df1

  • df2 (pandas DataFrame) – copy to df2