#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""
Make statistics on score files (stored in JSON files).
"""

import common_functions as common

import argparse
import os

import matplotlib.pyplot as plt
import numpy as np

# histtype : [‘bar’ | ‘barstacked’ | ‘step’ | ‘stepfilled’]
HIST_TYPE='bar'
ALPHA=0.5


def extract_data_list(json_dict, key, exclude_aborted, aborted_only):
    io_list = json_dict["io"]

    if exclude_aborted:
        json_data = [image_dict[key] for image_dict in io_list if "error" not in image_dict]
    elif aborted_only:
        json_data = [image_dict[key] for image_dict in io_list if "error" in image_dict]
    else:
        json_data = [image_dict[key] for image_dict in io_list]

    return json_data


if __name__ == '__main__':

    # PARSE OPTIONS ###########################################################

    parser = argparse.ArgumentParser(description="Make statistics on score files (JSON files).")

    parser.add_argument("--key", "-k", required=True, metavar="STRING",
                        help='The key of the value to plot (e.g. "mc_energy", "npe", "telescope_id", ...)')

    parser.add_argument("--exclude-aborted", action="store_true", default=False,
                        help="Ignore values from aborted images")

    parser.add_argument("--aborted-only", action="store_true", default=False,
                        help="Only consider aborted images")

    parser.add_argument("--logx", "-l", action="store_true", default=False,
                        help="Use a logaritmic scale on the X axis")

    parser.add_argument("--logy", "-L", action="store_true", default=False,
                        help="Use a logaritmic scale on the Y axis")

    parser.add_argument("--tight", action="store_true", default=False,
                        help="Optimize the X axis usage")

    parser.add_argument("--output", "-o", default=None, metavar="FILE",
                        help="The output file path")

    parser.add_argument("--title", default=None, metavar="STRING",
                        help="The title of the plot")

    parser.add_argument("--quiet", "-q", action="store_true",
                        help="Don't show the plot, just save it")

    parser.add_argument("fileargs", nargs=1, metavar="FILE",
                        help="The JSON file to process")

    args = parser.parse_args()

    key = args.key
    exclude_aborted = args.exclude_aborted
    aborted_only = args.aborted_only
    logx = args.logx
    logy = args.logy
    tight = args.tight
    title = args.title
    quiet = args.quiet
    json_file_path = args.fileargs[0]

    if args.output is None:
        base_file_path = os.path.basename(json_file_path)
        base_file_path = os.path.splitext(base_file_path)[0]
        output_file_path = "{}_{}.pdf".format(base_file_path, key)
    else:
        output_file_path = args.output

    if exclude_aborted and aborted_only:
        raise Exception("--exclude-aborted and --aborted-only are not compatible")

    # FETCH SCORE #############################################################

    json_dict = common.parse_json_file(json_file_path)

    data_array = np.array(extract_data_list(json_dict, key, exclude_aborted, aborted_only))
    label = json_dict["label"]

    print("{} images".format(data_array.shape[0]))
    print("min:", data_array.min())
    print("max:", data_array.max())
    print("mean:", data_array.mean())

    # SET TITLE ###############################################################

    if title is None:
        if exclude_aborted:
            errors_str = "exclude errors"
        elif aborted_only:
            errors_str = "errors only"
        else:
            errors_str = None

        if errors_str is not None:
            title = "{} ({}) histogram for {}".format(key, errors_str, label)
        else:
            title = "{} histogram for {}".format(key, label)

    # PLOT STATISTICS #########################################################

    fig, ax1 = plt.subplots(nrows=1, ncols=1, figsize=(10, 6))

    common.plot_hist1d(ax1,
                       [data_array],
                       num_bins=50,
                       logx=logx,
                       logy=logy,
                       xlabel=key,
                       title=title,
                       tight=tight,
                       info_box_rms=False,
                       info_box_std=True)

    # SAVE FILE AND PLOT ######################################################

    plt.savefig(output_file_path, bbox_inches='tight')

    if not quiet:
        plt.show()


@@ -2206,24 +2206,323 @@
  save it%22)%0A%0A
+    parser.add_argument(%22--ratio%22, action=%22store_true%22, default=False,%0A                        help=%22Plot the ratio of the first input file to the second one (require exactly 2 input files)%22)%0A%0A    parser.add_argument(%22--notebook%22, action=%22store_true%22,%0A                        help=%22Notebook mode%22)%0A%0A
     parser.a
@@ -2870,16 +2870,73 @@
 s.quiet%0A
+    plot_ratio = args.ratio%0A    notebook = args.notebook%0A
     json
@@ -4392,12 +4392,12 @@
 e=(1
-0, 6
+6, 9
 ))%0A%0A
@@ -4755,16 +4755,62 @@
 std=True
+,%0A                       plot_ratio=plot_ratio
 )%0A%0A    #
@@ -4881,24 +4881,49 @@
 ##########%0A%0A
+    if not notebook:%0A    
     plt.save

Add two options ('notebook' and 'radio').