Ejemplo n.º 1
0
def generate_line():
    line_data = _generate_line("line")
    line_data['type'] = "line"
    line_data['qa_pairs'] = generate_line_plot_questions(
        combine_source_and_rendered_data(line_data), color_map=color_map)
    visuals = _generate_visuals_for_line_plot(line_data['data'])

    # Add variation for line styles
    solid_only = True if np.random.random(
    ) <= data_config['line']['solid_pr'] else False
    if solid_only:
        line_styles = ["solid"] * len(line_data['data'])
    else:
        reference_styles = ["solid", "dashed", "dotted", "dotdash", "dashdot"]
        permuted_styles = list(np.random.permutation(reference_styles))
        line_styles = permuted_styles[:]

        while len(line_styles) < len(line_data['data']):
            line_styles += permuted_styles

        line_styles = line_styles[:len(line_data['data'])]

    visuals['line_styles'] = line_styles
    line_data['visuals'] = visuals

    return line_data
Ejemplo n.º 2
0
def generate_dot_line():
    line_data = _generate_line("dot_line")
    line_data['type'] = "dot_line"
    line_data['qa_pairs'] = generate_line_plot_questions(
        combine_source_and_rendered_data(line_data), color_map=color_map)
    line_data['visuals'] = _generate_visuals_for_line_plot(line_data['data'])

    return line_data
Ejemplo n.º 3
0
def generate_hbar_categorical():
    bar_data = _generate_bar_categorical("hbar_categorical")
    old_x = bar_data['data'][0]['x']
    bar_data['data'][0]['x'] = bar_data['data'][0]['y']
    bar_data['data'][0]['y'] = old_x
    bar_data['type'] = "hbar_categorical"
    bar_data['qa_pairs'] = generate_bar_graph_questions(
        combine_source_and_rendered_data(bar_data), color_map=color_map)
    return bar_data
Ejemplo n.º 4
0
def generate_figures(source_data_json,
                     destination_directory,
                     add_bboxes=False,
                     supplied_webdriver=None):

    # Setup dest dirs
    qa_json_dir = os.path.join(destination_directory, "json_qa")
    annotations_json_dir = os.path.join(destination_directory,
                                        "json_annotations")
    html_dir = destination_directory
    png_dir = os.path.join(destination_directory, "png")

    dirs = [destination_directory, qa_json_dir, annotations_json_dir, png_dir]

    if add_bboxes:
        bbox_img_dir = os.path.join(destination_directory, "bbox_png")
        dirs.append(bbox_img_dir)

    for dirpath in dirs:
        if not os.path.exists(dirpath):
            os.mkdir(dirpath)

    # Create web driver
    if supplied_webdriver:
        webdriver = supplied_webdriver
    else:
        webdriver = seldriver.PhantomJS()

    # Read in the synthetic data
    with open(source_data_json, 'r') as f:
        source_data_json = json.load(f)

    for fig_id, source in tqdm(iter(enumerate(source_data_json['data'])),
                               total=len(source_data_json['data']),
                               desc="Plotting figures"):

        point_sets = source['data']

        fig = None
        fig_type = source['type']

        if fig_type == 'vbar_categorical':
            fig = VBarGraphCategorical(point_sets[0], source['visuals'])
        elif fig_type == 'hbar_categorical':
            fig = HBarGraphCategorical(point_sets[0], source['visuals'])
        elif fig_type == 'line':
            fig = LinePlot(point_sets, source['visuals'])
        elif fig_type == 'dot_line':
            fig = DotLinePlot(point_sets, source['visuals'])
        elif fig_type == 'pie':
            fig = Pie(point_sets[0], source['visuals'])

        if not fig:
            continue

        html_file = os.path.join(html_dir, "%d_%s.html" % (fig_id, fig_type))
        png_file = os.path.join(png_dir, "%d_%s.png" % (fig_id, fig_type))

        # Export to HTML, PNG, and get rendered data
        rendered_data = export_png_and_data(fig.figure, png_file, html_file,
                                            webdriver)

        all_plot_data = combine_source_and_rendered_data(source, rendered_data)

        qa_json_file = os.path.join(qa_json_dir,
                                    "%s_%s.json" % (fig_id, fig_type))
        annotations_json_file = os.path.join(
            annotations_json_dir,
            "%d_%s_annotations.json" % (fig_id, fig_type))

        for qa in source['qa_pairs']:
            qa['image'] = os.path.basename(png_file)
            qa['annotations'] = os.path.basename(annotations_json_file)

        with open(qa_json_file, 'w') as f:
            json.dump(
                {
                    'qa_pairs':
                    source['qa_pairs'],
                    'total_distinct_questions':
                    source_data_json['total_distinct_questions'],
                    'total_distinct_colors':
                    source_data_json['total_distinct_colors']
                }, f)

        with open(annotations_json_file, 'w') as f:
            json.dump(all_plot_data, f)

        if add_bboxes:
            all_plot_data['image_index'] = fig_id
            generate_all_images_with_bboxes_for_plot(all_plot_data,
                                                     png_file,
                                                     bbox_img_dir,
                                                     'red',
                                                     load_image=True)

        # Cleanup
        os.remove(html_file)

    # Kill the newly created webdriver
    if not supplied_webdriver:
        webdriver.service.process.send_signal(signal.SIGTERM)
        try:
            RemoteWebDriver.quit(webdriver)
        except:
            pass
Ejemplo n.º 5
0
def generate_pie():
    config = data_config['pie']

    s, e = config['n_classes_range']
    n_classes = np.random.random_integers(s, e)

    widths = np.array([np.random.random() + 0.05 for i in range(n_classes)])
    widths_radians = 2 * np.pi * widths / np.sum(widths)
    starts = [0]
    for i in range(0, n_classes - 1):
        starts.append(starts[i] + widths_radians[i])
    ends = starts[1:] + [2 * np.pi]

    thetas = [
        starts[i] + (ends[i] - starts[i]) / 2 for i in range(len(starts))
    ]
    rad = 0.75
    x = [rad * np.cos(theta) for theta in thetas]
    y = [rad * np.sin(theta) for theta in thetas]

    # Get colors and labels
    all_color_pairs = []
    with open(os.path.normpath(config['color_sources'][0]), 'r') as f:
        for w in f.readlines():
            name, color = w.split(',')
            all_color_pairs.append((name.strip(), color.strip()))

    selected_color_pairs = random.sample(all_color_pairs, n_classes)

    pie_data = {
        'type':
        "pie",
        'data': [{
            'label_x': x,
            'label_y': y,
            'labels': [cp[0] for cp in selected_color_pairs],
            'colors': [cp[1] for cp in selected_color_pairs],
            'spans': widths_radians.tolist(),
            'starts': starts,
            'ends': ends,
        }]
    }

    # Add visuals and legend placement
    visuals = _generate_visuals_common()

    if visuals['draw_legend']:

        # Decide on legend orientation
        if n_classes <= common_config[
                'legend_horizontal_max_classes'] and np.random.random(
                ) <= common_config['legend_horizontal_pr']:
            visuals['legend_orientation'] = "horizontal"
            outside_possibilities = [('below', 'bottom_left'),
                                     ('below', 'bottom_center'),
                                     ('below', 'bottom_right')]

        else:
            visuals['legend_orientation'] = "vertical"
            outside_possibilities = [('right', 'bottom_right'),
                                     ('right', 'center_right'),
                                     ('right', 'top_right'),
                                     ('left', 'bottom_left'),
                                     ('left', 'center_left'),
                                     ('left', 'top_left')]

        legend_layout_position, legend_position = random.sample(
            outside_possibilities, 1)[0]
        visuals['legend_position'] = legend_position
        visuals['legend_layout_position'] = legend_layout_position

    pie_data['visuals'] = visuals
    pie_data['qa_pairs'] = generate_pie_chart_questions(
        combine_source_and_rendered_data(pie_data), color_map=color_map)

    return pie_data
Ejemplo n.º 6
0
def generate_vbar_categorical():
    bar_data = _generate_bar_categorical("vbar_categorical")
    bar_data['type'] = "vbar_categorical"
    bar_data['qa_pairs'] = generate_bar_graph_questions(
        combine_source_and_rendered_data(bar_data), color_map=color_map)
    return bar_data