def yerrorbar(self, ax, X, Y, error, Z=None, color=Tango.colorsHex['mediumBlue'], label=None, error_kwargs=None, **kwargs): error_kwargs = error_kwargs or {} if (error.shape[0] == 2) and (error.ndim == 2): error_kwargs.update( dict(array=error[1], arrayminus=error[0], symmetric=False)) else: error_kwargs.update(dict(array=error, symmetric=True)) if Z is not None: return Scatter3d(x=X, y=Y, z=Z, mode='markers', error_y=ErrorY(color=color, **error_kwargs or {}), marker=Marker(size='0'), name=label, showlegend=label is not None, **kwargs) return Scatter(x=X, y=Y, mode='markers', error_y=ErrorY(color=color, **error_kwargs or {}), marker=Marker(size='0'), name=label, showlegend=label is not None, **kwargs)
def test_instantiate_error_y(): ErrorY() ErrorY(array=[1, 2, 3], arrayminus=[2, 1, 2], color='red', symmetric=False, thickness=2, type='percent', value=1, valueminus=4, visible=True, width=5)
def get_scatter(name, x, y, ye=None): if ye: return Scatter(x=x, y=y, # mode='markers', name=name, error_y=ErrorY(type='data', array=ye, visible=True) ) else: return Scatter(x=x, y=y, # mode='markers', name=name)
def plotCategoryAccuracies(self, trialAccuracies, trainSize): """ Shows the accuracy for the categories at a certain training size @param trialAccuracies (dict) A dictionary of dictionaries. For each train size, there is a dictionary that maps a category to a list of accuracies for that category. @param trainSize (list) Size of training set for each trial. """ sizes = sorted(set(trainSize)) size_sqrt = math.sqrt(len(sizes)) subplotDimension = int(math.ceil(size_sqrt)) rows = subplotDimension cols = subplotDimension if len(sizes) <= subplotDimension * (subplotDimension - 1): rows -= 1 fig = tls.make_subplots(rows=rows, cols=cols, shared_xaxes=True, shared_yaxes=True, print_grid=False) num_categories = 0 for i, s in enumerate(sizes): # 1-indexed col = i % cols + 1 row = (i - col + 1) / cols + 1 classificationAccuracies = trialAccuracies[s] num_categories = max(num_categories, len(classificationAccuracies.keys())) x = [] y = [] std = [] for label, acc in classificationAccuracies.iteritems(): x.append(label) y.append(numpy.mean(acc)) std.append(numpy.std(acc)) trace = Scatter(x=x, y=y, name=s, mode='markers', error_y=ErrorY(type='data', symmetric=False, array=std, arrayminus=std, visible=True)) fig.append_trace(trace, row, col) fig["layout"]["title"] = "Accuracies for category by training size" half_way_cols = int(math.ceil(cols / 2.0)) half_way_rows = int(math.ceil(rows / 2.0)) fig["layout"]["xaxis{}".format( half_way_cols)]["title"] = "Category Label" fig["layout"]["yaxis{}".format(half_way_rows)]["title"] = "Accuracy" for i in xrange(1, cols + 1): fig["layout"]["xaxis{}".format(i)]["tickangle"] = -45 fig["layout"]["xaxis{}".format(i)]["nticks"] = num_categories * 2 if i <= rows: fig["layout"]["yaxis{}".format(i)]["range"] = [-.1, 1.1] fig["layout"]["margin"] = {"b": 120} plot_url = py.plot(fig) print "Category Accuracies URL: ", plot_url
def sweep_charts(db_data, config_id, config_info, sweep_type, x_label, y_label=None): """ Given a set of db_data from DBUtils.fetch_run_config_sweep_by_network along with the config_id, and maximum amount of food, generates a food and moves taken sweep plot. Returns ready to embed URLs. """ plot_urls = {} is_3d = False # Determines if plot is 3d # Determine how to label the axes. if sweep_type == "selection": # Grab the x-axis labels for this plot. x_label_vals = [y[3] for y in [db_data[x][0] for x in db_data]] else: x_label_vals = sorted(db_data.keys()) chart_set_config = { "food": { "title": "Food vs. {0} Sweep".format(x_label), "db-idx": 0, "val-func": [max, np.average], "plot-mode": "lines", "xaxis": x_label.title(), "yaxis": "Food Consumed", "max-line": config_info["max_food"], "max-title": "Available", "label": ["max", "mean", "std"] }, "moves-taken": { "title": "Moves Taken vs. {0} Sweep".format(x_label), "db-idx": 1, "val-func": [min, np.average], "plot-mode": "lines", "xaxis": x_label.title(), "yaxis": "Moves Taken", "label": ["min", "mean", "std"] }, "num-runs": { "title": "Number of runs", "db-idx": 1, "val-func": [len], "plot-mode": "lines", "xaxis": x_label.title(), "yaxis": "Moves Taken", "label": ["min", "mean", "std"] }, } # Add the max line for moves if not "moves_limit" type. if sweep_type != "moves_limit": chart_set_config["moves-taken"]["max-line"] = ( config_info["moves_limit"]) chart_set_config["moves-taken"]["max-title"] = "Limit" if (sweep_type == "p_mutate_crossover" or sweep_type == "dl_length_hidden"): for curr_key in chart_set_config.keys(): chart_set_config[curr_key]["xaxis"] = x_label chart_set_config[curr_key]["yaxis"] = y_label chart_set_config[curr_key]["type"] = Heatmap if curr_key == "food": chart_set_config[curr_key]["zaxis"] = "Food Consumed" chart_set_config[curr_key]["title"] = "Food 3D Sweep" chart_set_config[curr_key]["val-func"] = [max] elif curr_key == "moves-taken": chart_set_config[curr_key]["zaxis"] = "Food Consumed" chart_set_config[curr_key][ "title"] = "Moves Taken 3D Sweep" chart_set_config[curr_key]["val-func"] = [min] elif curr_key == "num-runs": chart_set_config[curr_key]["zaxis"] = "Number of Runs" if sweep_type == "p_mutate_crossover": step_size = 0.1 else: step_size = 1.0 chart_set_config[curr_key]["step-size"] = step_size is_3d = True # TODO: Could multithread here to speed things up. for chart_type, settings in chart_set_config.items(): traces_list = [] for idx, this_func in enumerate(settings["val-func"]): x_vals = [] y_vals = [] z_vals = [] y_std_dev = [] if is_3d: y_vals = sorted(db_data.keys()) # Need to find the length of x and min/max x to # figure out the labels and empty spots on heat chart. len_y = len(y_vals) x_vals = [] for cy in y_vals: curr_x = sorted(db_data[cy].keys()) x_vals.extend(curr_x) x_vals = list(set(x_vals)) x_vals.sort() y_vals = list( np.around(np.arange(start=min(y_vals), stop=max(y_vals) + settings["step-size"], step=settings["step-size"]), decimals=4)) x_vals = list( np.around(np.arange(start=min(x_vals), stop=max(x_vals) + settings["step-size"], step=settings["step-size"]), decimals=4)) # Go through all of the y/x values and fill in z. for cy in y_vals: this_z = dict.fromkeys(x_vals) if cy in db_data: for cx in sorted(db_data[cy].keys()): this_z[cx] = this_func([ x[settings["db-idx"]] for x in db_data[cy][cx] ]) this_z = [myz[1] for myz in sorted(this_z.items())] z_vals.append(this_z) this_trace = settings["type"]( x=x_vals, y=y_vals, z=z_vals, name=settings["label"][idx].title()) else: for curr_x in sorted(db_data.keys()): y_vals.append( this_func([ x[settings["db-idx"]] for x in db_data[curr_x] ])) if this_func == np.average: y_std_dev.append( np.std([ x[settings["db-idx"]] for x in db_data[curr_x] ])) if this_func == np.average: this_trace = Scatter( x=x_label_vals, y=y_vals, mode=settings["plot-mode"], name=settings["label"][idx].title(), error_y=ErrorY( type='data', array=y_std_dev, visible=True, )) else: this_trace = Scatter( x=x_label_vals, y=y_vals, mode=settings["plot-mode"], name=settings["label"][idx].title()) traces_list.append(this_trace) # If desired, add the maximum line. if "max-line" in settings and not is_3d: y_val = np.empty(len(x_label_vals)) y_val.fill(settings["max-line"]) traces_list.append( Scatter(x=x_label_vals, y=y_val, mode="lines", line={"dash": "dash"}, name=settings["max-title"].title())) layout = Layout( title=settings["title"], xaxis=XAxis(title=settings["xaxis"]), yaxis=YAxis(title=settings["yaxis"]), ) fig = Figure(data=Data(traces_list), layout=layout) # Generate the URL. plot_urls[chart_type] = chart.__generate_plotly_url( fig, filename="apigen/sweep_{0}_{1}_{2}".format( ''.join(e for e in x_label if e.isalnum()), config_id, chart_type), fileopt="overwrite") return plot_urls