Ejemplo n.º 1
0
def test_candidate_model_json_with_warning():
    eemeter_warning = EEMeterWarning(qualified_name="qualified_name",
                                     description="description",
                                     data={})
    candidate_model = CandidateModel(
        model_type="model_type",
        formula="formula",
        status="status",
        warnings=[eemeter_warning],
    )
    assert candidate_model.json() == {
        "formula":
        "formula",
        "model_params": {},
        "model_type":
        "model_type",
        "r_squared_adj":
        None,
        "status":
        "status",
        "warnings": [{
            "data": {},
            "description": "description",
            "qualified_name": "qualified_name",
        }],
    }
Ejemplo n.º 2
0
def test_plot_caltrack_candidate_error():
    candidate_model = CandidateModel(
        model_type="intercept_only",
        formula="formula",
        status="ERROR",
        plot_func=plot_caltrack_candidate,  # no predict func
        model_params={"intercept": 1},
    )
    ax = candidate_model.plot()
    assert ax is None
Ejemplo n.º 3
0
def test_plot_caltrack_candidate_best():
    candidate_model = CandidateModel(
        model_type="intercept_only",
        formula="formula",
        status="QUALIFIED",
        predict_func=caltrack_predict,
        plot_func=plot_caltrack_candidate,
        model_params={"intercept": 1},
    )
    ax = candidate_model.plot(best=True)
    data = ax.lines[0].get_xydata()
    assert data.shape == (60, 2)
Ejemplo n.º 4
0
def test_plot_caltrack_candidate_qualified():
    candidate_model = CandidateModel(
        model_type="intercept_only",
        formula="formula",
        status="QUALIFIED",
        predict_func=caltrack_predict,
        plot_func=plot_caltrack_candidate,
        model_params={"intercept": 1},
    )
    ax = candidate_model.plot(candidate_model, title="title")
    data = ax.lines[0].get_xydata()
    assert data.shape == (60, 2)
    assert ax.get_title() == "title"
Ejemplo n.º 5
0
def test_candidate_model_with_predict_func():
    def predict_func(model_type, model_params, input_):
        return input_

    candidate_model = CandidateModel(
        model_type="model_type",
        formula="formula",
        status="status",
        predict_func=predict_func,
    )
    assert candidate_model.model_type == "model_type"
    assert candidate_model.formula == "formula"
    assert candidate_model.status == "status"
    assert candidate_model.predict("a") == "a"
Ejemplo n.º 6
0
def test_model_results_json_with_model_metrics():
    candidate_model = CandidateModel(model_type="model_type",
                                     formula="formula",
                                     status="status",
                                     r_squared_adj=0.5)
    model_results = ModelResults(
        status="status",
        method_name="method_name",
        model=candidate_model,
        r_squared_adj=np.nan,
    )
    model_metrics = ModelMetrics(observed_input=pd.Series([0, 1, 2]),
                                 predicted_input=pd.Series([1, 0, 2]))
    json_result = model_results.json()
    json.dumps(json_result)  # just make sure it's valid json
    json_result["metrics"] = {}  # overwrite because of floats
    assert json_result == {
        "candidates": None,
        "metadata": {},
        "method_name": "method_name",
        "metrics": {},
        "model": {
            "formula": "formula",
            "model_params": {},
            "model_type": "model_type",
            "r_squared_adj": 0.5,
            "status": "status",
            "warnings": [],
        },
        "settings": {},
        "status": "status",
        "r_squared_adj": None,
        "warnings": [],
    }
Ejemplo n.º 7
0
def test_model_results_json_with_nan_r_squared_adj():
    candidate_model = CandidateModel(
        model_type="model_type",
        formula="formula",
        status="status",
        r_squared_adj=np.nan,
    )
    model_results = ModelResults(
        status="status",
        method_name="method_name",
        model=candidate_model,
        r_squared_adj=np.nan,
    )
    assert model_results.json() == {
        "candidates": None,
        "metadata": {},
        "method_name": "method_name",
        "metrics": None,
        "model": {
            "formula": "formula",
            "model_params": {},
            "model_type": "model_type",
            "r_squared_adj": None,
            "status": "status",
            "warnings": [],
        },
        "settings": {},
        "status": "status",
        "r_squared_adj": None,
        "warnings": [],
    }
Ejemplo n.º 8
0
def test_plot_caltrack_candidate_cdd_hdd_model():
    candidate_model = CandidateModel(
        model_type="cdd_hdd",
        formula="formula",
        status="QUALIFIED",
        predict_func=caltrack_predict,
        plot_func=plot_caltrack_candidate,
        model_params={
            "beta_hdd": 1,
            "beta_cdd": 1,
            "cooling_balance_point": 65,
            "heating_balance_point": 65,
            "intercept": 1,
        },
    )
    ax = candidate_model.plot()
    data = ax.lines[0].get_xydata()
    assert data.shape == (60, 2)
Ejemplo n.º 9
0
def test_candidate_model_minimal():
    candidate_model = CandidateModel(model_type="model_type",
                                     formula="formula",
                                     status="status")
    assert candidate_model.model_type == "model_type"
    assert candidate_model.formula == "formula"
    assert candidate_model.status == "status"
    with pytest.raises(ValueError):
        candidate_model.predict("a") == "a"
    assert candidate_model.model_params == {}
    assert candidate_model.warnings == []
    assert str(candidate_model).startswith("CandidateModel")
    assert candidate_model.json() == {
        "formula": "formula",
        "model_params": {},
        "model_type": "model_type",
        "r_squared_adj": None,
        "status": "status",
        "warnings": [],
    }
Ejemplo n.º 10
0
def test_model_results_json_with_objects():
    candidate_model = CandidateModel(model_type="model_type",
                                     formula="formula",
                                     status="status")
    eemeter_warning = EEMeterWarning(qualified_name="qualified_name",
                                     description="description",
                                     data={})
    model_results = ModelResults(
        status="status",
        method_name="method_name",
        model=candidate_model,
        candidates=[candidate_model],
        warnings=[eemeter_warning],
    )
    assert model_results.json(with_candidates=True) == {
        "candidates": [{
            "formula": "formula",
            "model_params": {},
            "model_type": "model_type",
            "r_squared_adj": None,
            "status": "status",
            "warnings": [],
        }],
        "metadata": {},
        "method_name":
        "method_name",
        "metrics":
        None,
        "model": {
            "formula": "formula",
            "model_params": {},
            "model_type": "model_type",
            "r_squared_adj": None,
            "status": "status",
            "warnings": [],
        },
        "settings": {},
        "status":
        "status",
        "r_squared_adj":
        None,
        "warnings": [{
            "data": {},
            "description": "description",
            "qualified_name": "qualified_name",
        }],
    }
Ejemplo n.º 11
0
def test_plot_model_results(il_electricity_cdd_hdd_daily):
    candidate_model = CandidateModel(
        model_type="intercept_only",
        formula="formula",
        status="QUALIFIED",
        predict_func=caltrack_predict,
        plot_func=plot_caltrack_candidate,
        model_params={"intercept": 1},
    )
    model_results = ModelResults(
        status="status",
        method_name="method_name",
        model=candidate_model,
        candidates=[candidate_model],
    )
    ax = model_results.plot(title="title", with_candidates=True)
    data = ax.lines[0].get_xydata()
    assert data.shape == (70, 2)
    data = ax.lines[1].get_xydata()
    assert data.shape == (70, 2)
    assert ax.get_title() == "title"
Ejemplo n.º 12
0
def test_candidate_model_with_no_plot_func():
    candidate_model = CandidateModel(model_type="model_type",
                                     formula="formula",
                                     status="status")
    with pytest.raises(ValueError):
        candidate_model.plot("a")