本文整理汇总了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)
示例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
示例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)
示例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]))
示例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())
示例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)
示例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)
示例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))
示例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
示例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)
示例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)
示例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)
示例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())
示例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)
示例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)