def test_constructor():

    # Initialise an EvaluationTool
    evt = EvaluationTool()

    # Check that general properties have been set correctly
    assert evt.type is None
    assert evt.has_true_CPs is False
    assert evt.true_CP_location is None
    assert evt.true_CP_model_index is None
    assert evt.true_CP_model_label is None

    # Check that properties related to plotting functionality are of the correct type
    # (but we aren't concerned about precisely which colours/linestyles are used).
    possible_colours = ('b', 'g', 'r', 'c', 'm', 'y', 'k', 'w')
    possible_linestyles = ('-', '--', '-.', ':')
    assert evt.cushion > 0
    assert evt.CP_color in possible_colours
    assert evt.median_color in possible_colours
    assert evt.max_color in possible_colours
    assert all([c in possible_colours for c in evt.colors])
    assert all([l in possible_linestyles for l in evt.linestyle])

    # Check that the correct list of names has been prepared - results corresponding to this list should be added later
    assert evt.names == [
        "names", "execution time", "S1", "S2", "T", "trimmer threshold",
        "MAP CPs", "model labels", "run length log distribution",
        "model and run length log distribution",
        "one-step-ahead predicted mean", "one-step-ahead predicted variance",
        "all run length log distributions",
        "all model and run length log distributions",
        "all retained run lengths", "has true CPs", "true CP locations",
        "true CP model index", "true CP model labels"
    ]
def test_initialise_from_not_run_detector(example_detector):

    det = example_detector
    evt = EvaluationTool()

    # Todo: throw a more informative error rather than print statement + AttributeError
    with pytest.raises(AttributeError):
        evt.build_EvaluationTool_via_run_detector(det)
def test_save_and_initialise_from_results(example_detector, tmpdir):

    det = example_detector
    det.run()
    evt = EvaluationTool()
    evt.build_EvaluationTool_via_run_detector(det)

    # Save to HD
    evt.store_results_to_HD(tmpdir.join("test_save_evt.txt"))
    assert os.path.isfile(tmpdir.join("test_save_evt.txt"))

    # Load into a new evt
    evt_load = EvaluationTool()
    evt_load.build_EvaluationTool_via_results(tmpdir.join("test_save_evt.txt"))

    # Check that key quantities have been restored
    assert evt_load.type == 4  # Detector has already been run
    assert evt_load.results[1] == evt.results[1]  # execution time
    for rlld_l, rlld in zip(evt_load.results[8],
                            evt.results[8]):  # run length log distribution
        assert rlld_l == rlld
def test_add_true_cps(example_detector):

    det = example_detector
    det.run()

    true_cp_index = 50
    true_cp_model_index = 2

    # Add CPs to the EvT before setting it up with the detector:

    # Set up the EvT
    evt = EvaluationTool()
    assert evt.has_true_CPs is False
    evt.add_true_CPs(true_cp_index, true_cp_model_index)
    assert evt.has_true_CPs is True

    # Build the evt using the detector, then check that true CP is stored in results
    evt.build_EvaluationTool_via_run_detector(det)
    assert evt.results[evt.results[0].index("has true CPs")] is True
    assert evt.results[evt.results[0].index(
        "true CP locations")] == true_cp_index
    assert evt.results[evt.results[0].index(
        "true CP model index")] == true_cp_model_index
    assert evt.results[evt.results[0].index("true CP model labels")] is None

    # Add CPs to the EvT after setting it up with the detector:

    # Set up the EvT
    evt2 = EvaluationTool()
    evt2.build_EvaluationTool_via_run_detector(det)
    evt2.add_true_CPs(true_cp_index, true_cp_model_index)

    # Check that CPs are still stored correctly if added after the detector has been run
    assert evt2.has_true_CPs is True
    assert evt2.results[evt.results[0].index("has true CPs")] is True
    assert evt2.results[evt.results[0].index(
        "true CP locations")] == true_cp_index
    assert evt2.results[evt.results[0].index(
        "true CP model index")] == true_cp_model_index
    assert evt2.results[evt.results[0].index("true CP model labels")] is None
def test_initialise_from_run_detector(example_detector):

    det = example_detector
    det.run()
    evt = EvaluationTool()
    evt.build_EvaluationTool_via_run_detector(det)

    # If initialised with a detector that has already been run, the evt should be type 4 (of 4)
    assert evt.type == 4

    # Check that the evt created using this detector has stored the expected values
    assert 1.6232197650790918 == pytest.approx(np.mean(evt.MSE), 1e-8)
    assert 1.6059882853108407 == pytest.approx(
        np.mean(evt.negative_log_likelihood), 1e-8)

    # Check the structure of the list of results
    assert evt.names == evt.results[
        0]  # Names of quantities are duplicated; should be the same
    assert len(evt.results[0]) == len(
        evt.results
    )  # Check that the number of names corresponds to the no. results
with open(well_file) as csvfile:
    reader = csv.reader(csvfile)
    for row in reader:
        raw_data += row

raw_data_float = []
for entry in raw_data:
    raw_data_float.append(float(entry))
raw_data = raw_data_float

T = int(len(raw_data))
S1, S2 = 1,1 #S1, S2 give you spatial dimensions
data = np.array(raw_data).reshape(T,1,1)

"""STEP 2: Read in the data to create your EvT objects"""
EvTKL = EvaluationTool()
EvTKL.build_EvaluationTool_via_results(result_path + "//" + results_file_KL) 

EvTDPD = EvaluationTool()
EvTDPD.build_EvaluationTool_via_results(result_path + "//" + results_file_DPD) 


if plot1:
    """STEP 3: Set up the figure properties and plot"""
    height_ratio =[8,10] #10,10
    custom_colors = ["blue", "purple"] 
    fig, ((ax1, ax2),(ax3,ax4)) = plt.subplots(2, 2, 
                                 figsize=(12,5), 
                                 sharex = 'col', 
                                 sharey = 'row',# True, 
                                 gridspec_kw = {'height_ratios':height_ratio})
            """get the average for each 2h-interval for each weekday"""
            for _2h in range(0, 12 * 7):
                selection_2h = [False] * _2h + [
                    True
                ] + [False] * (12 * 7 - 1 - _2h)
                selection = (selection_2h * int(T / (7 * 12)) +
                             selection_2h[:(T - int(T / (7 * 12)) * 7 * 12)])
                mean_2h[_2h, station] = np.mean(data[selection, station])
                data[selection, station] = (data[selection, station] -
                                            mean_2h[_2h, station])

if normalize:
    data = (data - np.mean(data, axis=0)) / np.sqrt(np.var(data, axis=0))
    intercept_priors = np.mean(data, axis=0)
"""""STEP 2: READ RESULTS""" ""
EvTKL, EvTDPD = EvaluationTool(), EvaluationTool()
EvTKL.build_EvaluationTool_via_results(results_file_KL)
EvTDPD.build_EvaluationTool_via_results(results_file_DPD)
"""Get dates"""


def perdelta(start, end, delta, date_list):
    curr = start
    while curr < end:
        #yield curr
        date_list.append(curr)
        curr += delta


all_dates = []
#start_year, start_month, start_day, start_hour = 2002, 8, 17, 0