Helper function to stop the profiling process and write out the profiled
    data into the given filename. Before this, sort the stats by the passed sorting.

    :param profiler: An already started profiler (probably by start_profiling).
    :type profiler: cProfile.Profile
    :param filename: The name of the output file to save the profile.
    :type filename: basestring
    :param sorting: The sorting of the statistics passed to the sort_stats function.
    :type sorting: basestring

    :return: None
    :rtype: None

    Start and stop the profiler with:

    >>> profiler = start_profiling()
    >>> # Do something you want to profile
    >>> end_profiling(profiler, "out.txt", "cumulative")

def end_profiling(profiler, filename, sorting=None):
    """
    Helper function to stop the profiling process and write out the profiled
    data into the given filename. Before this, sort the stats by the passed sorting.

    :param profiler: An already started profiler (probably by start_profiling).
    :type profiler: cProfile.Profile
    :param filename: The name of the output file to save the profile.
    :type filename: basestring
    :param sorting: The sorting of the statistics passed to the sort_stats function.
    :type sorting: basestring

    :return: None
    :rtype: None

    Start and stop the profiler with:

    >>> profiler = start_profiling()
    >>> # Do something you want to profile
    >>> end_profiling(profiler, "out.txt", "cumulative")
    """
    profiler.disable()
    s = six.StringIO()
    ps = pstats.Stats(profiler, stream=s).sort_stats(sorting)
    ps.print_stats()

    with open(filename, "w+") as f:
        _logger.info("[calculate_ts_features] Finished profiling of time series feature extraction")
        f.write(s.getvalue())