Data Tools

Created on Tue Mar 29 17:53:18 2016

@author: Jonas Eschle “Mayou36”

Contains several tools to convert, load, save and plot data

raredecay.tools.data_tools.adv_return(return_value, save_name=None)[source]

Save the value if save_name specified, otherwise just return input.

Can be wrapped around the return value. Without any arguments, the return of your function will be exactly the same. With arguments, the value can be saved (pickled) before it is returned.

Parameters:
  • return_value (any python object) – The python object which should be pickled.
  • save_name (str, None) –
    The (file-)name for the pickled file. File-extension will be added

    automatically if specified in raredecay.meta_config. | If None is passed, the object won’t be pickled.

Returns:

  • out (python object) – Return return_value without changes.

  • **Usage** – Instead of a simple return statement

    >>> return my_variable/my_object
    

    one can use the completely equivalent statement

    >>> return adv_return(my_variable/my_object)
    

    If the return value should be saved in addition to be returned, use

    >>> return adv_return(my_variable/my_object, save_name='my_object.pickle')
    

    (the .pickle ending is not required but added automatically if omitted)

    which returns the value and saves it.

raredecay.tools.data_tools.is_list(data_to_check)[source]

Check whether the given data is a list.

raredecay.tools.data_tools.is_ndarray(data_to_check)[source]

Check whether a given data is an ndarray.

raredecay.tools.data_tools.is_pickle(data_to_check)[source]

Check if the file is a pickled file (checks the ending).

raredecay.tools.data_tools.is_root(data_to_check)[source]

Check whether a given data is a root file. Needs dicts to be True.

raredecay.tools.data_tools.make_root_dict(path_to_rootfile, tree_name, branches)[source]

Returns a root_numpy compatible “root-dict” of a root-tree.

Parameters:
  • path_to_rootfile (str) – The exact path to the root-tree including the filename. Example: /home/user1/data/myRootTree1.root
  • tree_name (str) – The name of the tree
  • branches (str or list[str, str, str,.. ]) – The branches of the tree to use
raredecay.tools.data_tools.obj_to_string(objects, separator=None)[source]

Return a string containing all objects as strings, separated by the separator.

Useful for automatic conversion for different types. The following objects will automatically be converted:

  • None will be omitted
Parameters:
  • objects (any object or list(obj, obj, ..) with a string representation) – The objects will be converted to a string and concatenated, separated by the separator.
  • separator (str) – The separator between the objects. Default is ” - ”.
raredecay.tools.data_tools.to_list(data_in)[source]

Convert the data into a list. Does not pack lists into a new one.

If your input is, for example, a string or a list of strings, or a tuple filled with strings, you have, in general, a problem:

  • just iterate through the object will fail because it iterates through the characters of the string.
  • using list(obj) converts the tuple, leaves the list but splits the strings characters into single elements of a new list.
  • using [obj] creates a list containing a string, but also a list containing a list or a tuple, which you did not want to.

Solution: use to_list(obj), which creates a new list in case the object is a single object (a string is a single object in this sence) or converts to a list if the object is already a container for several objects.

Parameters:data_in (any obj) – So far, any object can be entered.
Returns:out – Return a list containing the object or the object converted to a list.
Return type:list
raredecay.tools.data_tools.to_ndarray(data_in, float_array=True)[source]

Convert data to numpy array (containing only floats).

Parameters:data_in (any reasonable data) – The data to be converted
raredecay.tools.data_tools.to_pandas(data_in, index=None, columns=None)[source]

Convert data from numpy or root to pandas dataframe.

Convert data safely to pandas, whatever the format is. :param data_in: The data to be converted :type data_in: any reasonable data

raredecay.tools.data_tools.try_unpickle(file_to_unpickle)[source]

Try to unpickle a file and return, otherwise just return input.