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