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


Python util.Tree類代碼示例

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


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

示例1: test_update_new_brach

 def test_update_new_brach(self):
     tree = Tree()
     tree['a'].update({'b': 1, 'c': 2})
     self.assertIsInstance(tree, Tree)
     self.assertIsInstance(tree['a'], Tree)
     self.assertEqual(tree.getval(['a', 'b']), 1)
     self.assertEqual(tree.getval(['a', 'c']), 2)
開發者ID:Jeswang,項目名稱:icarus,代碼行數:7,代碼來源:test_tree.py

示例2: filter

 def filter(self, condition):
     """Return subset of results matching specific conditions
     
     Parameters
     ----------
     condition : dict
         Dictionary listing all parameters and values to be matched in the
         results set. Each parameter, i.e., each key of the dictionary must
         be an iterable object containing the path in the parameters tree
         to the required parameter 
     metrics : dict, optional
         List of metrics to be reported
     
     Returns
     -------
     filtered_results : ResultSet
         List of 2-tuples of filtered results, where the first element is a
         tree of all experiment parameters and the second value is 
         a tree with experiment results.
     """
     filtered_resultset = ResultSet()
     for parameters, results in self._results:
         parameters = Tree(parameters)
         if parameters.match(condition):
             filtered_resultset.add(parameters, results)
     return filtered_resultset
開發者ID:David-Loughnane,項目名稱:distributed-project,代碼行數:26,代碼來源:readwrite.py

示例3: test_setitem

 def test_setitem(self):
     tree = Tree()
     tree['a'] = {'b': 1, 'c': 2}
     self.assertIsInstance(tree, Tree)
     self.assertIsInstance(tree['a'], Tree)
     self.assertEqual(tree.getval(['a', 'b']), 1)
     self.assertEqual(tree.getval(['a', 'c']), 2)
開發者ID:Jeswang,項目名稱:icarus,代碼行數:7,代碼來源:test_tree.py

示例4: test_getval_empty

 def test_getval_empty(self):
     tree = Tree()
     _ = tree[1][2][3]
     self.assertIsNotNone(tree.getval([1]))
     self.assertIsNotNone(tree.getval([1, 2]))
     self.assertIsNone(tree.getval([1, 2, 3]))
     self.assertIsNone(tree.getval([1, 2, 3, 4]))
開發者ID:Jeswang,項目名稱:icarus,代碼行數:7,代碼來源:test_tree.py

示例5: test_paths

 def test_paths(self):
     tree = Tree()
     tree['b']['c']['e'] = 4
     tree['b']['v']['d'] = 3
     tree['a'] = 1
     expected = {('b', 'c', 'e'): 4, ('b', 'v', 'd'): 3, ('a',): 1}
     self.assertDictEqual(expected, tree.paths())
開發者ID:Jeswang,項目名稱:icarus,代碼行數:7,代碼來源:test_tree.py

示例6: test_nested_update

 def test_nested_update(self):
     tree = Tree()
     tree['a'].update({'b': {'c': 1}, 'd': 2})
     self.assertIsInstance(tree, Tree)
     self.assertIsInstance(tree['a'], Tree)
     self.assertIsInstance(tree['a']['b'], Tree)
     self.assertEqual(tree.getval(['a', 'b', 'c']), 1)
     self.assertEqual(tree.getval(['a', 'd']), 2)
開發者ID:Jeswang,項目名稱:icarus,代碼行數:8,代碼來源:test_tree.py

示例7: test_init_from_nested_dict

 def test_init_from_nested_dict(self):
     tree = Tree({'a': {'c': {'e': 1}}, 'b': {'d': 2}})
     self.assertEqual(tree.getval(['a', 'c', 'e']), 1)
     self.assertEqual(tree.getval(['b', 'd']), 2)
     self.assertIsInstance(tree, Tree)
     self.assertIsInstance(tree['a'], Tree)
     self.assertIsInstance(tree['a']['c'], Tree)
     self.assertIsInstance(tree.getval(['a', 'c']), Tree)
     self.assertIsInstance(tree['b'], Tree)
開發者ID:Jeswang,項目名稱:icarus,代碼行數:9,代碼來源:test_tree.py

示例8: test_match

 def test_match(self):
     t = {'a': {'b': 1}, 'c': 2, 'd': {'e': 3}}
     pos_match_equal = {'a': {'b': 1}, 'c': 2, 'd': {'e': 3}}
     pos_match_subset = {'a': {'b': 1}, 'd': {'e': 3}}
     neg_match_diff = {'a': {'b': 2}, 'c': 2, 'd': {'e': 3}}
     neg_match_superset = {'a': {'b': 1}, 'c': 2, 'd': {'e': 3}, 'f': 3}
     tree = Tree(t)
     self.assertTrue(tree.match(pos_match_equal))
     self.assertTrue(tree.match(pos_match_subset))
     self.assertFalse(tree.match(neg_match_diff))
     self.assertFalse(tree.match(neg_match_superset))
開發者ID:Jeswang,項目名稱:icarus,代碼行數:11,代碼來源:test_tree.py

示例9: results

 def results(self):
     results = Tree({
        'MEAN_RSN_ZERO_HOP':     np.mean(self.rsn_hit_ratio[0]),
        'MEAN_RSN_ONE_HOP':      np.mean(self.rsn_hit_ratio[1]),
        'MEAN_RSN_TWO_HOP':      np.mean(self.rsn_hit_ratio[2]),
        'MEAN_RSN_THREE_HOP':    np.mean(self.rsn_hit_ratio[3]),
               })
     results['MEAN_RSN_ALL'] = results['MEAN_RSN_ZERO_HOP'] + \
                               results['MEAN_RSN_ONE_HOP'] + \
                               results['MEAN_RSN_TWO_HOP'] + \
                               results['MEAN_RSN_THREE_HOP']  
     if self.cdf:
         results.update({
            'CDF_RSN_ZERO_HOP':      cdf(self.rsn_hit_ratio[0]),
            'CDF_RSN_ONE_HOP':       cdf(self.rsn_hit_ratio[1]),
            'CDF_RSN_TWO_HOP':       cdf(self.rsn_hit_ratio[2]),
            'CDF_RSN_THREE_HOP':     cdf(self.rsn_hit_ratio[3]),
                        })
     return results
開發者ID:oascigil,項目名稱:icarus-lanman,代碼行數:19,代碼來源:collectors.py

示例10: plot_cdf


#.........這裏部分代碼省略.........
         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,代碼行數:101,代碼來源:plot.py

示例11: plot_bar_chart


#.........這裏部分代碼省略.........
        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,代碼行數:101,代碼來源:plot.py

示例12: plot_lines


#.........這裏部分代碼省略.........
         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,代碼行數:101,代碼來源:plot.py

示例13: test_dict_2

 def test_dict_2(self):
     d = {'a': {'b': 'a'}, 'b': 'c', 'd': {'b': 'c'}}
     tree = Tree(d)
     self.assertEqual(d, tree.dict())
開發者ID:icarus-sim,項目名稱:icarus,代碼行數:4,代碼來源:test_tree.py

示例14: test_update_base

 def test_update_base(self):
     tree = Tree()
     tree.update({'b': 1, 'c': 2})
     self.assertIsInstance(tree, Tree)
     self.assertEqual(tree.getval(['b']), 1)
     self.assertEqual(tree.getval(['c']), 2)
開發者ID:Jeswang,項目名稱:icarus,代碼行數:6,代碼來源:test_tree.py

示例15: test_init_from_dict_kwargs

 def test_init_from_dict_kwargs(self):
     tree = Tree({'c': 3}, a=1, b=2)
     self.assertEqual(tree.getval(['a']), 1)
     self.assertEqual(tree.getval(['b']), 2)
     self.assertEqual(tree.getval(['c']), 3)
     self.assertIsInstance(tree, Tree)
開發者ID:Jeswang,項目名稱:icarus,代碼行數:6,代碼來源:test_tree.py


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