def make_labels_level_img(img_path, level, prefix=None, show=False):
    """Replace labels in an image with their parents at the given level.
    
    Labels that do not fall within a parent at that level will remain in place.
    
    Args:
        img_path: Path to the base image from which the corresponding 
            registered image will be found.
        level: Ontological level at which to group child labels. 
        prefix: Start of path for output image; defaults to None to 
            use ``img_path`` instead.
        show: True to show the images after generating them; defaults to False.
    """
    # load original labels image and setup ontology dictionary
    labels_sitk = sitk_io.load_registered_img(img_path,
                                              config.RegNames.IMG_LABELS.value,
                                              get_sitk=True)
    labels_np = sitk.GetArrayFromImage(labels_sitk)
    ref = ontology.load_labels_ref(config.load_labels)
    labels_ref_lookup = ontology.create_aba_reverse_lookup(ref)

    ids = list(labels_ref_lookup.keys())
    for key in ids:
        keys = [key, -1 * key]
        for region in keys:
            if region == 0: continue
            # get ontological label
            label = labels_ref_lookup[abs(region)]
            label_level = label[ontology.NODE][config.ABAKeys.LEVEL.value]
            if label_level == level:
                # get children (including parent first) at given level
                # and replace them with parent
                label_ids = ontology.get_children_from_id(
                    labels_ref_lookup, region)
                labels_region = np.isin(labels_np, label_ids)
                print("replacing labels within", region)
                labels_np[labels_region] = region
    labels_level_sitk = sitk_io.replace_sitk_with_numpy(labels_sitk, labels_np)

    # generate an edge image at this level
    labels_edge = vols.make_labels_edge(labels_np)
    labels_edge_sikt = sitk_io.replace_sitk_with_numpy(labels_sitk,
                                                       labels_edge)

    # write and optionally display labels level image
    imgs_write = {
        config.RegNames.IMG_LABELS_LEVEL.value.format(level):
        labels_level_sitk,
        config.RegNames.IMG_LABELS_EDGE_LEVEL.value.format(level):
        labels_edge_sikt,
    }
    out_path = prefix if prefix else img_path
    sitk_io.write_reg_images(imgs_write, out_path)
    if show:
        for img in imgs_write.values():
            if img: sitk.Show(img)
def plot_region_development(metric, size=None, show=True):
    """Plot regions across development for the given metric.
    
    Args:
        metric (str): Column name of metric to track.
        size (List[int]): Sequence of ``width, height`` to size the figure; 
            defaults to None.
        show (bool): True to display the image; defaults to True.

    """
    # set up access to data frame columns
    id_cols = ["Age", "Condition"]
    extra_cols = ["RegionName"]
    cond_col = "Region"

    # assume that vol stats file is given first, then region IDs;
    # merge in region names and levels
    df_regions = pd.read_csv(config.filenames[1])
    df = pd.read_csv(config.filename).merge(
        df_regions[["Region", "RegionName", "Level"]], on="Region", how="left")

    # convert sample names to ages
    ages = ontology.rel_to_abs_ages(df["Sample"].unique())
    df["Age"] = df["Sample"].map(ages)

    # get large super-structures for normalization to brain tissue, where
    # "non-brain" are spinal cord and ventricles, which are variably labeled
    df_base = df[df["Region"] == 15564]
    ids_nonbr_large = (17651, 126651558)
    dfs_nonbr_large = [df[df["Region"] == n] for n in ids_nonbr_large]

    # get data frame with region IDs of all non-brain structures removed
    labels_ref_lookup = ontology.LabelsRef(
        config.load_labels).load().ref_lookup
    ids_nonbr = []
    for n in ids_nonbr_large:
        ids_nonbr.extend(ontology.get_children_from_id(labels_ref_lookup, n))

    label_id = config.atlas_labels[config.AtlasLabels.ID]
    if label_id is not None:
        # show only selected region and its children
        ids = ontology.get_children_from_id(labels_ref_lookup, label_id)
        df = df[np.isin(df["Region"], ids)]
    df_brain = df.loc[~df["Region"].isin(ids_nonbr)]

    levels = np.sort(df["Level"].unique())
    conds = df["Condition"].unique()

    # get aggregated whole brain tissue for normalization
    cols_show = (*id_cols, cond_col, *extra_cols, metric)
    if dfs_nonbr_large:
        # add all large non-brain structures
        df_nonbr = dfs_nonbr_large[0]
        for df_out in dfs_nonbr_large[1:]:
            df_nonbr = df_io.normalize_df(df_nonbr, id_cols, cond_col, None,
                                          [metric], extra_cols, df_out,
                                          df_io.df_add)
        # subtract them from whole organism to get brain tissue alone,
        # updating given metric in db_base
        df_base = df_io.normalize_df(df_base, id_cols, cond_col, None,
                                     [metric], extra_cols, df_nonbr,
                                     df_io.df_subtract)
    df_base.loc[:, "RegionName"] = "Brain tissue"
    print("Brain {}:".format(metric))
    df_io.print_data_frame(df_base.loc[:, cols_show], "\t")
    df_base_piv, regions = df_io.pivot_with_conditions(df_base, id_cols,
                                                       "RegionName", metric)

    # plot lines with separate styles for each condition and colors for
    # each region name
    linestyles = ("--", "-.", ":", "-")
    num_conds = len(conds)
    linestyles = linestyles * (num_conds // (len(linestyles) + 1) + 1)
    if num_conds < len(linestyles):
        # ensure that 1st and last styles are dashed and solid unless
        linestyles = (*linestyles[:num_conds - 1], linestyles[-1])
    lines_params = {
        "labels": (metric, "Post-Conceptional Age"),
        "linestyles": linestyles,
        "size": size,
        "show": show,
        "ignore_invis": True,
        "groups": conds,
        "marker": ".",
    }
    line_params_norm = lines_params.copy()
    line_params_norm["labels"] = ("Fraction", "Post-Conceptional Age")
    plot_2d.plot_lines(config.filename,
                       "Age",
                       regions,
                       title="Whole Brain Development ({})".format(metric),
                       suffix="_dev_{}_brain".format(metric),
                       df=df_base_piv,
                       **lines_params)

    for level in levels:
        # plot raw metric at given level
        df_level = df.loc[df["Level"] == level]
        print("Raw {}:".format(metric))
        df_io.print_data_frame(df_level.loc[:, cols_show], "\t")
        df_level_piv, regions = df_io.pivot_with_conditions(
            df_level, id_cols, "RegionName", metric)
        plot_2d.plot_lines(config.filename,
                           "Age",
                           regions,
                           title="Structure Development ({}, Level {})".format(
                               metric, level),
                           suffix="_dev_{}_level{}".format(metric, level),
                           df=df_level_piv,
                           **lines_params)

        # plot metric normalized to whole brain tissue; structures
        # above removed regions will still contain them
        df_brain_level = df_brain.loc[df_brain["Level"] == level]
        df_norm = df_io.normalize_df(df_brain_level, id_cols, cond_col, None,
                                     [metric], extra_cols, df_base)
        print("{} normalized to whole brain:".format(metric))
        df_io.print_data_frame(df_norm.loc[:, cols_show], "\t")
        df_norm_piv, regions = df_io.pivot_with_conditions(
            df_norm, id_cols, "RegionName", metric)
        plot_2d.plot_lines(
            config.filename,
            "Age",
            regions,
            units=(None, config.plot_labels[config.PlotLabels.X_UNIT]),
            title=("Structure Development Normalized to Whole "
                   "Brain ({}, Level {})".format(metric, level)),
            suffix="_dev_{}_level{}_norm".format(metric, level),
            df=df_norm_piv,
            **line_params_norm)