Skip to content
Snippets Groups Projects
Select Git revision
  • 7c8c2d12a165d157ac4ba391e57eb6fd33733c6e
  • main default protected
  • waitfd
  • docker-perf
  • muhq-ma-results
5 results

gen_boxplots.py

Blame
  • gen_boxplots.py 1.90 KiB
    #!/usr/bin/env python3
    """generate boxplots from descriptive stats in yaml"""
    
    import sys
    
    import yaml
    
    
    def parse_data(stream):
        """Convert yamls string to dict"""
        return yaml.safe_load(stream)
    
    TIKZ_TEMPLATE = \
    """\\documentclass[]{standalone}
    \\usepackage{pgfplots}
    \\pgfplotsset{compat=1.17}
    \\usepgfplotslibrary{statistics}
    \\begin{document}
    \\begin{tikzpicture}
    \\begin{axis}[
        boxplot/draw direction=y,
        legend columns=2,
        area legend,
        legend style={
            draw=none,
            fill=none,
            at={(0.5,-0.1)},
            anchor=north
        },
    ]
    $PLOTS$
    \\end{axis}
    \\end{tikzpicture}
    \\end{document}
    """
    
    PLOT_TEMPLATE = \
    """\\addplot+[
        boxplot prepared={{
              lower whisker={lower_whisker},
              lower quartile={lower_quartile},
              median={median},
              average={mean},
              upper quartile={upper_quartile},
              upper whisker={upper_whisker},
          }},
    ] coordinates{{{outlier_cords}}};
    \\addlegendentry{{{variant}}}
    """
    
    
    def generate_boxplot(data) -> str:
        """generate a tikzboxplot"""
        plots = ''
        for variant, stats in data.items():
            outlier_cords = ''
            for outlier in stats['outliers']:
                outlier_cords += f'(0, {outlier}) '
            plots += PLOT_TEMPLATE.format(variant=variant.replace('_', '-'),
                                          outlier_cords=outlier_cords,
                                          **stats)
    
        return TIKZ_TEMPLATE.replace('$PLOTS$', plots)
    
    
    def main():
        """read yaml and print generated boxplots"""
        if len(sys.argv) == 1:
            data = parse_data(sys.stdin)
        else:
            with open(sys.argv[1], 'r', encoding='utf-8') as yaml_file:
                data = parse_data(yaml_file)
    
        tikz = generate_boxplot(data)
    
        if len(sys.argv) > 2:
            with open(sys.argv[2], 'w', encoding='utf-8') as saveto:
                print(tikz, file=saveto)
        else:
            print(tikz)
    
    
    if __name__ == '__main__':
        main()