###########################
Demo with dynbenchmark data
###########################

In this vignette, we will use `funkyheatmappy` to reproduce the figures by `Saelens et al. 2019 <https://www.nature.com/articles/s41587-019-0071-9>`_

*********
Load Data
*********

Import packages::

    from funkyheatmappy import funky_heatmap
    import pandas as pd


***************
Process results
***************

First we import the data generated by running the `data-raw/dynbenchmark_data.R <https://github.com/dynverse/funkyheatmap/blob/main/data-raw/dynbenchmark_data.R>`_ 
script::

    pies = pd.read_csv("../test/data/dynbenchmark_pie.csv", header=0, index_col=0)
    data = pd.read_csv("../test/data/dynbenchmark_data.csv")
    data["benchmark_overall_error_reasons"] = pies.to_dict("index").values()

It's possible to use `funky_heatmap()` to visualise the data frame without
providing additional metadata, but it will likely not have any of the desired
formatting.::

    funky_heatmap(data)


*******************
Process column info
*******************
Apart from the results themselves, the most important additional info is the
column info. This data frame contains information on how each column should be
formatted.::
    
    options = pd.read_csv("../test/data/dynbenchmark_options.csv")
    options = [
        {k: v for k, v in m.items() if pd.notnull(v)}
        for m in options.to_dict(orient="records")
    ]
    column_info = pd.read_csv(
        "../test/data/dynbenchmark_column_info.csv",
        index_col="id",
        keep_default_na=False,
        na_values=["NaN"],
    )
    column_info["options"] = options
    column_info.loc["method_priors_required_str", "options"]["legend"] = {
        "legend": {
            "": "None",
            "✕": "Weak: Start or end cells",
            "✖": "Strong: Cell grouping or time course",
        }
    }

With just the data and the column info, we can already get a pretty good 
funky heatmap::

    funky_heatmap(data, column_info=column_info)


****************************
Finetuning the visualization
****************************
The figure can be finetuned by grouping the columns and rows and specifying
custom palettes. 

Column grouping::

    column_groups = pd.read_csv("../test/data/dynbenchmark_column_groups.csv")

Row info::

    row_info = pd.read_csv("../test/data/dynbenchmark_row_info.csv", index_col="id")

Row grouping::

    row_groups = pd.read_csv("../test/data/dynbenchmark_row_groups.csv")

Palettes::

    palettes["colours"] = palettes.colours.apply(lambda x: x.split(", "))
    names_error_r = [
        "Memory limit exceeded",
        "Time limit exceeded",
        "Execution error",
        "Method error",
    ]
    palettes["colours"][5] = dict(zip(names_error_r, palettes["colours"][5]))


**********************
Generate funky heatmap
**********************
The resulting visualisation contains all of the results by `Saelens et al. 2019 <https://www.nature.com/articles/s41587-019-0071-9>`_
in a single plot.

Note that Figures 2 and 3 from the main paper and Supplementary Figure 2 were generated by making different subsets
of the `column_info` and `column_groups` objects.::

    funky_heatmap(
        data=data,
        column_info=column_info,
        column_groups=column_groups,
        row_info=row_info,
        row_groups=row_groups,
        palettes=palettes,
        col_annot_offset=3.2,
    )
    