當前位置: 首頁>>代碼示例>>Python>>正文


Python Tree.setval方法代碼示例

本文整理匯總了Python中icarus.util.Tree.setval方法的典型用法代碼示例。如果您正苦於以下問題:Python Tree.setval方法的具體用法?Python Tree.setval怎麽用?Python Tree.setval使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在icarus.util.Tree的用法示例。


在下文中一共展示了Tree.setval方法的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: plot_cdf

# 需要導入模塊: from icarus.util import Tree [as 別名]
# 或者: from icarus.util.Tree import setval [as 別名]

#.........這裏部分代碼省略.........
         from the conditions expressed in the filter parameter, which are
         global, these conditions are specific to one bar. Ech condition name,
         different from the filter parameter is a path to a condition to be
         checked, e.g. ('topology', 'name'). Values to be matched for this
         conditions are specified in ycondvals. This list must be as long as
         the number of lines to plot. If not specified, all lines are filtered
         by the conditions of filter parameter only, but in this case all
         ymetrics should be different.
     * ycondvals : list of tuples, optional
         List of values that the conditions of ycondnames must meet. This list
         must be as long as the number of lines to plot. If not specified,
         all lines are filtered by the conditions of filter parameter only,
         but in this case all ymetrics should be different.
     * xscale : str, optional
         The scale of x axis. Options allowed are 'linear' and 'log'. 
         Default value is 'linear'
     * yscale : str, optional
         The scale of y axis. Options allowed are 'linear' and 'log'.
         Default value is 'linear'
     * step : bool, optional
         If *True* draws the CDF with steps. Default value is *True*
     * line_style : dict, optional
         Dictionary mapping each value of yvals with a line style
     * legend : dict, optional
         Dictionary mapping each value of yvals with a legend label. If not
         specified, it is not plotted. If you wish to plot it with the
         name of the line, set it to put yvals or ymetrics, depending on which
         one is used
     * legend_loc : str, optional
         Legend location, e.g. 'upper left'
     * legend_args : dict, optional
         Optional legend arguments, such as ncol
     * plotempty : bool, optional
         If *True*, plot and save graph even if empty. Default is *True* 
    """
    fig = plt.figure()
    if 'title' in desc:
        plt.title(desc['title'])
    if 'xlabel' in desc:
        plt.xlabel(desc['xlabel'])
    plt.ylabel(desc['ylabel'] if 'ylabel' in desc else 'Cumulative probability')
    if 'xscale' in desc:
        plt.xscale(desc['xscale'])
    if 'yscale' in desc:
        plt.yscale(desc['yscale'])
    if 'filter' not in desc or desc['filter'] is None:
        desc['filter'] = {}
    step = desc['step'] if 'step' in desc else True
    plot_empty = desc['plotempty'] if 'plotempty' in desc else True
    ymetrics = desc['ymetrics']
    ycondnames = desc['ycondnames'] if 'ycondnames' in desc else None
    ycondvals = desc['ycondvals'] if 'ycondvals' in desc else None
    if ycondnames is not None and ycondvals is not None:
        if not len(ymetrics) == len(ycondnames) == len(ycondvals):
            raise ValueError('ymetrics, ycondnames and ycondvals must have the same length')
        # yvals is basically the list of values that differentiate each line
        # it is used for legends and styles mainly
        yvals = ycondvals if len(set(ymetrics)) == 1 else zip(ymetrics, ycondvals)
    else:
        yvals = ymetrics
    x_min = np.infty
    x_max = - np.infty
    empty = True
    for i in range(len(yvals)):
        condition = Tree(desc['filter'])
        if ycondnames is not None:
            condition.setval(ycondnames[i], ycondvals[i])      
        data = [v.getval(ymetrics[i])
                for _, v in resultset.filter(condition)
                if v.getval(ymetrics[i]) is not None]
        # If there are more than 1 CDFs in the resultset, take the first one
        if data:
            x_cdf, y_cdf = data[0]
            if step:
                x_cdf, y_cdf = step_cdf(x_cdf, y_cdf)
        else:
            x_cdf, y_cdf = [], []
        fmt = desc['line_style'][yvals[i]] if 'line_style' in desc \
              and yvals[i] in desc['line_style'] else '-'
        # This check is to prevent crashing when trying to plot arrays of nan
        # values with axes log scale
        if all(np.isnan(x) for x in x_cdf) or all(np.isnan(y) for y in y_cdf):
            plt.plot([], [], fmt)
        else:
            plt.plot(x_cdf, y_cdf, fmt)
            empty = False
            x_min = min(x_min, x_cdf[0])
            x_max = max(x_max, x_cdf[-1])
    if empty and not plot_empty:
        return
    plt.xlim(x_min, x_max)
    if 'legend' in desc:
        legend = [desc['legend'][l] for l in desc['yvals']]
        legend_args = desc['legend_args'] if 'legend_args' in desc else {}
        if 'legend_loc' in desc:
            legend_args['loc'] = desc['legend_loc']
        plt.legend(legend, prop={'size': LEGEND_SIZE}, **legend_args)
    plt.legend(legend, prop={'size': LEGEND_SIZE}, loc=desc['legend_loc'])
    plt.savefig(os.path.join(plotdir, filename), bbox_inches='tight')
    plt.close(fig)
開發者ID:David-Loughnane,項目名稱:distributed-project,代碼行數:104,代碼來源:plot.py

示例2: plot_bar_chart

# 需要導入模塊: from icarus.util import Tree [as 別名]
# 或者: from icarus.util.Tree import setval [as 別名]

#.........這裏部分代碼省略.........
        plt.xlabel(desc['xlabel'])
    if 'ylabel' in desc:
        plt.ylabel(desc['ylabel'])
    if 'filter' not in desc or desc['filter'] is None:
        desc['filter'] = {}
    plot_empty = desc['plotempty'] if 'plotempty' in desc else True
    
    ymetrics = desc['ymetrics']
    ycondnames = desc['ycondnames'] if 'ycondnames' in desc else None
    ycondvals = desc['ycondvals'] if 'ycondvals' in desc else None
    if ycondnames is not None and ycondvals is not None:
        if not len(ymetrics) == len(ycondnames) == len(ycondvals):
            raise ValueError('ymetrics, ycondnames and ycondvals must have the same length')
        # yvals is basically the list of values that differentiate each bar
        # it is used for legends and styles mainly
        yvals = ycondvals if len(set(ymetrics)) == 1 else zip(ymetrics, ycondvals)
    else:
        yvals = ymetrics
    placement = desc['placement'] if 'placement' in desc else 'grouped'
    if placement == 'grouped':
        placement = [1 for _ in range(len(yvals))]
    elif placement == 'stacked':
        placement = [len(yvals)]
    else:
        if sum(placement) != len(yvals):
            raise ValueError('Placement definition incorrect. '
                             'The sum of values of the list must be equal to '
                             'the number of y values')
    xticks = desc['xticks'] if 'xticks' in desc else desc['xvals']
    empty = True
    # Spacing attributes
    # width of a group of bars
    group_width = desc['group_width'] if 'group_width' in desc else 0.4 
    width = group_width/len(placement)          # width of a single bar
    separation = width/2                        # space between adjacent groups
    border = 0.6 * separation                   # left and right borders
        
    elem = collections.defaultdict(int)         # bar objects (for legend)
    # Select colors and hatches
    if 'bar_color' in desc and all(y in desc['bar_color'] for y in yvals):
        color = desc['bar_color']
    elif len(yvals) <= len(BW_COLOR_CATALOGUE):
        color = dict((y, BW_COLOR_CATALOGUE[yvals.index(y)]) for y in yvals)
    else:
        color = collections.defaultdict(lambda: None)
    if 'bar_hatch' in desc and desc['bar_hatch'] is None:
        hatch = collections.defaultdict(lambda: None)
    elif 'bar_hatch' in desc and all(y in desc['bar_hatch'] for y in yvals):
        hatch = desc['bar_hatch']
    elif len(yvals) <= len(BW_COLOR_CATALOGUE):
        hatch = dict((y, HATCH_CATALOGUE[yvals.index(y)]) for y in yvals)
    else:
        hatch = collections.defaultdict(lambda: None)
    # Plot bars
    left = border    # left-most point of the bar about to draw
    for i in range(len(desc['xvals'])):
        l = 0
        for x in placement:
            bottom = 0   # Bottom point of a bar. It is alway 0 if stacked is False
            for y in range(x):
                condition = Tree(desc['filter'])
                condition.setval(desc['xparam'], desc['xvals'][i])
                if ycondnames is not None:
                    condition.setval(ycondnames[l], ycondvals[l])
                data = [v.getval(ymetrics[i])
                        for _, v in resultset.filter(condition)
                        if v.getval(ymetrics[i]) is not None]
                confidence = desc['confidence'] if 'confidence' in desc else 0.95 
                meanval, err = means_confidence_interval(data, confidence)
                yerr = None if 'errorbar' in desc and not desc['errorbar'] else err
                if not np.isnan(meanval):
                    empty = False
                elem[yvals[l]] = plt.bar(left, meanval, width,
                                         color=color[yvals[l]], 
                                         yerr=yerr, bottom=bottom, ecolor='k',
                                         hatch=hatch[yvals[l]], label=yvals[l])
                bottom += meanval
                l += 1
            left += width
        left += separation
    if empty and not plot_empty:
        return
    n_bars = len(placement)
    plt.xticks(border + 0.5*(n_bars*width) + 
               (separation + n_bars*width)*np.arange(len(xticks)),
               xticks)
    if 'legend' in desc:
        legend = [desc['legend'][l] for l in yvals] if 'legend'in desc else yvals
        legend_args = desc['legend_args'] if 'legend_args' in desc else {}
        if 'legend_loc' in desc:
            legend_args['loc'] = desc['legend_loc']
        plt.legend([elem[x] for x in yvals], legend,
                   prop={'size': LEGEND_SIZE},
                   **legend_args)
    xmin, _ = plt.xlim()
    plt.xlim(xmin, left - separation + border)
    if 'ymax' in desc:
        plt.ylim(ymax=desc['ymax'])
    plt.savefig(os.path.join(plotdir, filename), bbox_inches='tight')
    plt.close(fig)
開發者ID:David-Loughnane,項目名稱:distributed-project,代碼行數:104,代碼來源:plot.py

示例3: plot_lines

# 需要導入模塊: from icarus.util import Tree [as 別名]
# 或者: from icarus.util.Tree import setval [as 別名]

#.........這裏部分代碼省略.........
         Dictionary mapping each value of yvals with a line style
     * plot_args : dict, optional
         Additional args to be provided to the Pyplot errorbar function.
         Example parameters that can be specified here are *linewidth* and
         *elinewidth*
     * legend : dict, optional
         Dictionary mapping each value of yvals with a legend label. If not
         specified, it is not plotted. If you wish to plot it with the
         name of the line, set it to put yvals or ymetrics, depending on which
         one is used
     * legend_loc : str, optional
         Legend location, e.g. 'upper left'
     * legend_args : dict, optional
         Optional legend arguments, such as ncol
     * plotempty : bool, optional
         If *True*, plot and save graph even if empty. Default is *True*
     * xmin, xmax: float, optional
        The limits of the x axis. If not specified, they're set to the min and
        max values of xvals
     * ymin, ymax: float, optional
        The limits of the y axis. If not specified, they're automatically
        selected by Matplotlib
    """
    fig = plt.figure()
    _, ax1 = plt.subplots()
    if 'title' in desc:
        plt.title(desc['title'])
    if 'xlabel' in desc:
        plt.xlabel(desc['xlabel'])
    if 'ylabel' in desc:
        plt.ylabel(desc['ylabel'])
    if 'xscale' in desc:
        plt.xscale(desc['xscale'])
    if 'yscale' in desc:
        plt.yscale(desc['yscale'])
    if 'filter' not in desc or desc['filter'] is None:
        desc['filter'] = {}
    xvals = sorted(desc['xvals'])
    if 'xticks' in desc:
        ax1.set_xticks(desc['xticks'])
        ax1.get_xaxis().set_major_formatter(matplotlib.ticker.ScalarFormatter())
        ax1.set_xticklabels([str(xtick) for xtick in desc['xticks']])
    if 'yticks' in desc:
        ax1.set_yticks(desc['yticks'])
        ax1.get_yaxis().set_major_formatter(matplotlib.ticker.ScalarFormatter())
        ax1.set_yticklabels([str(ytick) for ytick in desc['yticks']])
    ymetrics = desc['ymetrics']
    ycondnames = desc['ycondnames'] if 'ycondnames' in desc else None
    ycondvals = desc['ycondvals'] if 'ycondvals' in desc else None
    if ycondnames is not None and ycondvals is not None:
        if not len(ymetrics) == len(ycondnames) == len(ycondvals):
            raise ValueError('ymetrics, ycondnames and ycondvals must have the same length')
        # yvals is basically the list of values that differentiate each line
        # it is used for legends and styles mainly
        yvals = ycondvals if len(set(ymetrics)) == 1 else zip(ymetrics, ycondvals)
    else:
        yvals = ymetrics
    plot_args = desc['plot_args'] if 'plot_args' in desc else {}
    plot_empty = desc['plotempty'] if 'plotempty' in desc else True
    empty = True
    for i in range(len(yvals)):
        means = np.zeros(len(xvals))
        err = np.zeros(len(xvals))
        for j in range(len(xvals)):
            condition = Tree(desc['filter'])
            condition.setval(desc['xparam'], xvals[j])
            if ycondnames is not None:
                condition.setval(ycondnames[i], ycondvals[i])
            data = [v.getval(ymetrics[i])
                    for _, v in resultset.filter(condition)
                    if v.getval(ymetrics[i]) is not None]
            confidence = desc['confidence'] if 'confidence' in desc else 0.95 
            means[j], err[j] = means_confidence_interval(data, confidence)
        yerr = None if 'errorbar' in desc and not desc['errorbar'] or all(err == 0) else err
        fmt = desc['line_style'][yvals[i]] if 'line_style' in desc \
              and yvals[i] in desc['line_style'] else '-'
        # This check is to prevent crashing when trying to plot arrays of nan
        # values with axes log scale
        if all(np.isnan(x) for x in xvals) or all(np.isnan(y) for y in means):
            plt.errorbar([], [], fmt=fmt)
        else:
            plt.errorbar(xvals, means, yerr=yerr, fmt=fmt, **plot_args)
            empty = False
    if empty and not plot_empty:
        return
    x_min = desc['xmin'] if 'xmin' in desc else min(xvals)
    x_max = desc['xmax'] if 'xmax' in desc else max(xvals)
    plt.xlim(x_min, x_max)
    if 'ymin' in desc:
        plt.ylim(ymin=desc['ymin'])
    if 'ymax' in desc:
        plt.ylim(ymax=desc['ymax'])
    if 'legend' in desc:
        legend = [desc['legend'][l] for l in yvals]
        legend_args = desc['legend_args'] if 'legend_args' in desc else {}
        if 'legend_loc' in desc:
            legend_args['loc'] = desc['legend_loc']
        plt.legend(legend, prop={'size': LEGEND_SIZE}, **legend_args)
    plt.savefig(os.path.join(plotdir, filename), bbox_inches='tight')
    plt.close(fig)
開發者ID:David-Loughnane,項目名稱:distributed-project,代碼行數:104,代碼來源:plot.py

示例4: test_getset

# 需要導入模塊: from icarus.util import Tree [as 別名]
# 或者: from icarus.util.Tree import setval [as 別名]
 def test_getset(self):
     tree = Tree()
     tree.setval([1, 2, 3, 4], 5)
     self.assertEqual(tree.getval([1, 2, 3, 4]), 5)
開發者ID:Jeswang,項目名稱:icarus,代碼行數:6,代碼來源:test_tree.py


注:本文中的icarus.util.Tree.setval方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。