def group_by_envelopes(outcomes, outcome_to_plot, time, density, ax, ax_d, fill, group_labels, **kwargs): ''' Helper function, responsible for generating an envelope plot based on a grouping. :param outcomes: a dictonary containing the various outcomes to plot :param outcome_to_plot: the specific outcome to plot :param time: the name of the time dimension :param density: string, either hist, kde, or empty/None. :param ax: the ax on which to plot :param ax_d: the ax on which to plot the density :param fill: boolean, if true, fill the envelope. :param group_by_labels: order in which groups should be plotted :param kwargs: kwargs to be passed on to the helper function for plotting the density. ''' for j, key in enumerate(group_labels): value = outcomes[key] value = value[outcome_to_plot] try: plot_envelope(ax, j, time, value,fill) except ValueError: warning("value error when plotting for %s" % (key)) raise if density=='kde': kde_x, kde_y = determine_kde(value[:,-1]) plot_kde(ax_d, kde_x, kde_y, j, **kwargs) if density: if density=='hist': # rather nasty indexing going on here, outcomes[key] returns # a tuple, hence the[1] to get the dictonary with outcomes # out of this, we need the right outcome, and the final column # of values values = [outcomes[key][outcome_to_plot][:,-1] for key in group_labels] plot_histogram(ax_d, values, **kwargs) if density=='box plot': values = [outcomes[key][outcome_to_plot][:,-1] for key in group_labels] plot_boxplots(ax_d, values, group_labels, **kwargs) ax_d.get_yaxis().set_view_interval( ax.get_yaxis().get_view_interval()[0], ax.get_yaxis().get_view_interval()[1])
def group_by_lines(outcomes, outcome_to_plot, time, density, ax, ax_d, group_by_labels, **kwargs): ''' Helper function responsible for generating a grouped lines plot. :param outcomes: a dictonary containing the various outcomes to plot :param outcome_to_plot: the specific outcome to plot :param time: the name of the time dimension :param density: string, either hist, kde, or empty/None. :param ax: the ax on which to plot :param ax_d: the ax on which to plot the density :param group_by_labels: order in which groups should be plotted :param kwargs: kwargs to be passed on to the hlepr function for plotting the density. ''' for j, key in enumerate(group_by_labels): value = outcomes[key] value = value[outcome_to_plot] color = COLOR_LIST[j] ax.plot(time.T[:, np.newaxis], value.T, c=color, ms=1, markevery=5) if density=='kde': kde_x, kde_y = determine_kde(value[:,-1]) plot_kde(ax_d, kde_x, kde_y, j, **kwargs) if density: if density=='hist': values = [outcomes[key][outcome_to_plot][:,-1] for key in group_by_labels] plot_histogram(ax_d, values, **kwargs) if density=='box plot': values = [outcomes[key][outcome_to_plot][:,-1]\ for key in group_by_labels] plot_boxplots(ax_d, values, group_by_labels, **kwargs) ax_d.get_yaxis().set_view_interval( ax.get_yaxis().get_view_interval()[0], ax.get_yaxis().get_view_interval()[1])
def plot_lines_with_envelopes(results, outcomes_to_show = [], group_by = None, grouping_specifiers = None, density='', titles={}, ylabels={}, legend=True, experiments_to_show=None, **kwargs): ''' Helper function for generating a plot which contains both an envelope and lines. :param results: return from :meth:`perform_experiments`. :param outcomes_to_show: list of outcome of interest you want to plot. If empty, all outcomes are plotted. **Note**: just :param group_by: name of the column in the cases array to group results by. Alternatively, `index` can be used to use indexing arrays as the basis for grouping. :param grouping_specifiers: set of categories to be used as a basis for grouping by. Grouping_specifiers is only meaningful if group_by is provided as well. In case of grouping by index, the grouping specifiers should be in a dictonary where the key denotes the name of the group. :param density: boolean, if true, the density of the endstates will be plotted. :param titles: a way for controlling whether each of the axes should have a title. There are three possibilities. If set to None, no title will be shown for any of the axes. If set to an empty dict, the default, the title is identical to the name of the outcome of interest. If you want to override these default names, provide a dict with the outcome of interest as key and the desired title as value. This dict need only contain the outcomes for which you want to use a different title. :param ylabels: a way for controlling the ylablels. Works identical to titles. :param legend: boolean, if true, and there is a column specified for grouping, show a legend :param experiments_to_show: numpy array containing the indices of the experiments to be visualized. Defaults to None, implying that all experiments should be shown. :rtype: a `figure <http://matplotlib.sourceforge.net/api/figure_api.html>`_ instance Additional key word arguments will be passed along to the density function. ======== =============================== property description ======== =============================== log log scale the histogram or GKDE ======== =============================== ''' # make sure we have the data full_results = copy.deepcopy(results) experiments, outcomes = results experiments = experiments[experiments_to_show] new_outcomes={} for key, value in outcomes.items(): new_outcomes[key] =value[experiments_to_show] results = experiments, new_outcomes data = prepare_data(results, outcomes_to_show, group_by, grouping_specifiers) outcomes, outcomes_to_show, time, grouping_labels = data full_outcomes = prepare_data(full_results, outcomes_to_show, group_by, grouping_specifiers)[0] figure, grid = make_grid(outcomes_to_show, density) axes_dict = {} # do the plotting for i, outcome_to_plot in enumerate(outcomes_to_show): ax = figure.add_subplot(grid[i,0]) axes_dict[outcome_to_plot] = ax ax_d= None if density: ax_d = figure.add_subplot(grid[i,1], sharey=ax) axes_dict[outcome_to_plot+"_density"] = ax_d for tl in ax_d.get_yticklabels(): tl.set_visible(False) if group_by: # group_by_labels = sorted(outcomes.keys()) for j, key in enumerate(grouping_labels): full_value = full_outcomes[key][outcome_to_plot] plot_envelope(ax, j, time, full_value, fill=True) for j, key in enumerate(grouping_labels): value = outcomes[key][outcome_to_plot] full_value = full_outcomes[key][outcome_to_plot] ax.plot(time.T[:, np.newaxis], value.T, COLOR_LIST[j]) if density=='kde': simple_density(density, full_value, ax_d, ax, **kwargs) # kde_x, kde_y = determine_kde(full_value[:,-1]) # plot_kde(ax_d, kde_x, kde_y, j, **kwargs) if density: if density=='hist': values = [full_outcomes[key][outcome_to_plot][:,-1]\ for key in grouping_labels] plot_histogram(ax_d, values, **kwargs) if density=='box plot': values = [full_outcomes[key][outcome_to_plot][:,-1]\ for key in grouping_labels] plot_boxplots(ax_d, values, grouping_labels, **kwargs) ax_d.get_yaxis().set_view_interval( ax.get_yaxis().get_view_interval()[0], ax.get_yaxis().get_view_interval()[1]) else: value = full_outcomes[outcome_to_plot] plot_envelope(ax, 0, time, value, fill=True) if density: simple_density(density, value, ax_d, ax, **kwargs) value = outcomes[outcome_to_plot] ax.plot(time.T, value.T) ax.set_xlim(xmin=time[0] , xmax=time[-1]) ax.set_xlabel(TIME_LABEL) do_ylabels(ax, ylabels, outcome_to_plot) do_titles(ax, titles, outcome_to_plot) if legend and group_by: make_legend(grouping_labels, figure) if plotting_util.TIGHT: grid.tight_layout(figure) return figure, axes_dict