<jupyter_start><jupyter_text>PCG timings - Figure 3This notebook creates the plots presented in Figure 3 of the FlyWire paper.<jupyter_code>import os
import dill
import warnings
import pandas as pd
import numpy as np

warnings.filterwarnings("ignore")
from flywirepaper import plotting<jupyter_output><empty_output><jupyter_text>Define relevant paths and create directories<jupyter_code>data_dir = f"{os.path.realpath('')}/../data"
figure_dir = f"{os.path.realpath('')}/../figures/figure_3/"
figure_source_dir = f"{os.path.realpath('')}/../figures/figure_source_3/"

if not os.path.exists(figure_dir):
    os.makedirs(figure_dir)
    
if not os.path.exists(figure_source_dir):
    os.makedirs(figure_source_dir)<jupyter_output><empty_output><jupyter_text>e & f: Speed measurements from server e & f show data as it was measured on the server from real requests during FlyWire's beta phase Loading data for plots e & f. The data is organized as a dictionary: The keys are the types of requests and the values are arrays with individual measruments in milliseconds.<jupyter_code>with open(f"{data_dir}/pcg_logged_times_betaphase.dl", "rb") as f:
    pcg_logged_time_dict = dill.load(f)
    
pcg_logged_time_dict.keys()<jupyter_output><empty_output><jupyter_text>Helper functions:<jupyter_code>def ks_string(ks):
    s = ks[0]
    for k in ks[1:]:
        s += f"_{k}"
    return s

label_remap = {"root": "leaves to root", "leaves": "root to leaves"}<jupyter_output><empty_output><jupyter_text>Plotting e:<jupyter_code>ks = ["merge", "split"]

plotting.comp_cumplot({label_remap.get(k, k): np.array(pcg_logged_time_dict[k]) / 1e3 for k in ks}, 
                      xlabel=r"Edit time (s)",
                      xlog=True,
                      color_list=[".5", "k"],
                      linestyles=["-", "--"],
                      xrange=[10**(-1), 10**1.5],
                      figsize=(5, 4),
                      yrange=[0, 1],
                      path=f"{figure_dir}/time_meas_{ks_string(ks)}.png")<jupyter_output>findfont: Font family ['sans-serif'] not found. Falling back to DejaVu Sans.
findfont: Font family ['sans-serif'] not found. Falling back to DejaVu Sans.
findfont: Font family ['sans-serif'] not found. Falling back to DejaVu Sans.<jupyter_text>Plotting f:<jupyter_code>ks = ["root", "leaves"]

plotting.comp_cumplot({label_remap.get(k, k): np.array(pcg_logged_time_dict[k]) / 1e3 for k in ks}, 
                      xlabel=r"Operation time (s)",
                      xlog=True,
                      color_list=[".5", "k"],
                      linestyles=["-", "--"],
                      xrange=[10**(-2), 10**.5],
                      figsize=(5, 4),
                      yrange=[0, 1],
                      path=f"{figure_dir}/time_meas_{ks_string(ks)}.png")<jupyter_output><empty_output><jupyter_text>Statistics:<jupyter_code>scenarios = list(pcg_logged_time_dict.keys())

stat_dict = {
    "N": [len(pcg_logged_time_dict[k]) for k in scenarios],
    "median": [np.median(pcg_logged_time_dict[k]) for k in scenarios],
    "mean": [np.mean(pcg_logged_time_dict[k]) for k in scenarios],
    "95th percentile": [np.percentile(pcg_logged_time_dict[k], 95) for k in scenarios],
    "99th percentile": [np.percentile(pcg_logged_time_dict[k], 99) for k in scenarios],
}
pd.DataFrame.from_dict(stat_dict, orient="index", columns=scenarios)
source_data_df = pd.DataFrame.from_dict({label_remap.get(k, k): pcg_logged_time_dict[k] for k in pcg_logged_time_dict}, 
                                        orient="index").T
source_data_df.to_csv(f"{figure_source_dir}/e_f_source.csv")<jupyter_output><empty_output><jupyter_text>g,h,i: Naive vs PCGg, h, i present data that was measured in a closed-off trial re-using real requests from FlyWire's beta phase<jupyter_code>meas_df = pd.read_csv(f"{data_dir}/pcg_vs_naive.csv")
meas_df.head(5)
plotting.scatter_w_binstats_plot(meas_df["n_sv_global"], meas_df["n_sv_local"], 
                      xrange=[10**1, 10**6],
                      yrange=[10**1, 10**6],
                      marker_size=2,
                      plot_diagonal=True,
                      xlog=True, ylog=True,
                      figsize=(7, 6.5),
                      color="k",
                      xlabel="Supervoxels in full component",
                      ylabel="Supervoxels in local region", alpha=.2,
                      path=f"{figure_dir}/sv_global_vs_sv_local.png")
plotting.scatter_w_binstats_plot(meas_df["read_naive"], meas_df["read_pcg"], 
                      xrange=[10**(-2), 10**2],
                      yrange=[10**(-2), 10**2],
                      marker_size=2,
                      plot_diagonal=True,
                      xlog=True, ylog=True,
                      figsize=(7, 6.5),
                      color="k",
                      xlabel="naive: reading edges (s)",
                      ylabel="ours: reading edges (s)", alpha=.2,
                      path=f"{figure_dir}/reading_pcg_vs_naive.png")
plotting.scatter_w_binstats_plot(meas_df["mincut_naive"], meas_df["mincut_pcg"], 
                      marker_size=2,
                      xrange=[10**(-2), 10**2],
                      yrange=[10**(-2), 10**2],
                      plot_diagonal=True,
                      xlog=True, ylog=True,
                      figsize=(7, 6.5),
                      color="k",
                      xlabel="naive: max-flow min-cut (s)",
                      ylabel="ours: max-flow min-cut (s)", alpha=.2,
                      path=f"{figure_dir}/mincut_pcg_vs_naive.png")
plotting.comp_hist_plot({"global": meas_df["n_sv_global"], "local": meas_df["n_sv_local"]}, 
                        figsize=(6, 3), normalize=True, color_list=["k", [.8, .3, .3]],
                        logbins=True, nbins=40, xlabel="Number of supervoxels",
                        data_range=[1, 10**8], 
                        xlabel_fontsize=30, tick_label_size=24,
                        path=f"{figure_dir}/sv_global_vs_sv_local_comp_hist.png")

plotting.comp_hist_plot({"naive": meas_df["read_naive"], "ours": meas_df["read_pcg"]}, 
                        figsize=(6, 3), normalize=True, color_list=["k", [.8, .3, .3]],
                        xlabel_fontsize=30, tick_label_size=24,
                        logbins=True, nbins=40, xlabel="Reading edges (s)",
                        path=f"{figure_dir}/reading_pcg_vs_naive_comp_hist.png")

plotting.comp_hist_plot({"naive": meas_df["mincut_naive"], "ours": meas_df["mincut_pcg"]}, 
                        figsize=(6, 3), normalize=True, color_list=["k", [.8, .3, .3]],
                        logbins=True, nbins=40, 
                        xlabel_fontsize=30, tick_label_size=24,
                        xlabel="Max-flow min-cut (s)",
                        path=f"{figure_dir}/mincut_pcg_vs_naive_comp_hist.png")
meas_df.to_csv(f"{figure_source_dir}/g_h_i_source.csv")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1379558887127, 'fraction_numerical': 0.01874008937581087, 'mean_word_length': 2.3943248532289627, 'pattern_counts': {'":': 13, '<': 32, '<?xml version=': 0, '>': 32, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '474415', 'license': 'Apache-2.0', 'n_tokens_mistral': 2473, 'n_tokens_neox': 2281, 'n_words': 416, 'path': 'flywirepaper/PCG timings - Figure 3.ipynb', 'repo_name': 'seung-lab/FlyWirePaper', 'size': '6937'}