Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_candidate_model_json_with_warning():
eemeter_warning = EEMeterWarning(
qualified_name="qualified_name", description="description", data={}
)
candidate_model = CalTRACKUsagePerDayCandidateModel(
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",
def test_plot_caltrack_candidate_with_range():
candidate_model = CalTRACKUsagePerDayCandidateModel(
model_type="intercept_only",
formula="formula",
status="QUALIFIED",
model_params={"intercept": 1},
)
ax = candidate_model.plot(temp_range=(10, 20))
data = ax.lines[0].get_xydata()
assert data.shape == (10, 2)
def test_model_results_json_with_nan_r_squared_adj():
candidate_model = CalTRACKUsagePerDayCandidateModel(
model_type="model_type",
formula="formula",
status="status",
r_squared_adj=np.nan,
)
model_results = CalTRACKUsagePerDayModelResults(
status="status",
method_name="method_name",
model=candidate_model,
r_squared_adj=np.nan,
)
assert model_results.json() == {
"candidates": None,
"interval": None,
"metadata": {},
"method_name": "method_name",
def candidate_model_cdd_only():
return CalTRACKUsagePerDayCandidateModel(
model_type="cdd_only",
formula="formula",
status="QUALIFIED",
model_params={"intercept": 1, "beta_cdd": 1, "cooling_balance_point": 65},
)
def test_model_results_json_with_model_metrics():
candidate_model = CalTRACKUsagePerDayCandidateModel(
model_type="model_type", formula="formula", status="status", r_squared_adj=0.5
)
model_results = CalTRACKUsagePerDayModelResults(
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
assert "totals_metrics" in json_result
assert "avgs_metrics" in json_result
def test_plot_caltrack_candidate_error():
candidate_model = CalTRACKUsagePerDayCandidateModel(
model_type="intercept_only",
formula="formula",
status="ERROR",
model_params={"intercept": 1},
)
ax = candidate_model.plot()
assert ax is None
model_type,
balance_point,
"cdd",
data[cdd_column],
period_days,
minimum_total_cdd,
)
)
degree_day_warnings.extend(
get_too_few_non_zero_degree_day_warning(
model_type, balance_point, "cdd", data[cdd_column], minimum_non_zero_cdd
)
)
if len(degree_day_warnings) > 0:
return CalTRACKUsagePerDayCandidateModel(
model_type=model_type,
formula=formula,
status="NOT ATTEMPTED",
warnings=degree_day_warnings,
)
try:
model = smf.wls(formula=formula, data=data, weights=weights)
except Exception as e:
return get_fit_failed_candidate_model(model_type, formula)
result = model.fit()
r_squared_adj = result.rsquared_adj
beta_cdd_p_value = result.pvalues[cdd_column]
# CalTrack 3.3.1.3
period_days,
minimum_total_hdd,
)
)
degree_day_warnings.extend(
get_too_few_non_zero_degree_day_warning(
model_type,
heating_balance_point,
"hdd",
data[hdd_column],
minimum_non_zero_hdd,
)
)
if len(degree_day_warnings) > 0:
return CalTRACKUsagePerDayCandidateModel(
model_type, formula, "NOT ATTEMPTED", warnings=degree_day_warnings
)
try:
model = smf.wls(formula=formula, data=data, weights=weights)
except Exception as e:
return get_fit_failed_candidate_model(model_type, formula)
result = model.fit()
r_squared_adj = result.rsquared_adj
beta_cdd_p_value = result.pvalues[cdd_column]
beta_hdd_p_value = result.pvalues[hdd_column]
# CalTrack 3.3.1.3
model_params = {
"intercept": result.params["Intercept"],
Returns
-------
candidate_model : :any:`eemeter.CalTRACKUsagePerDayCandidateModel`
Candidate model instance with status ``'ERROR'``, and warning with
traceback.
"""
warnings = [
EEMeterWarning(
qualified_name="eemeter.caltrack_daily.{}.model_results".format(model_type),
description=(
"Error encountered in statsmodels.formula.api.ols method. (Empty data?)"
),
data={"traceback": traceback.format_exc()},
)
]
return CalTRACKUsagePerDayCandidateModel(
model_type=model_type, formula=formula, status="ERROR", warnings=warnings
)
model_warnings.extend(
get_parameter_p_value_too_high_warning(
model_type,
model_params,
parameter,
beta_hdd_p_value,
beta_hdd_maximum_p_value,
)
)
if len(model_warnings) > 0:
status = "DISQUALIFIED"
else:
status = "QUALIFIED"
return CalTRACKUsagePerDayCandidateModel(
model_type=model_type,
formula=formula,
status=status,
warnings=model_warnings,
model_params=model_params,
model=model,
result=result,
r_squared_adj=r_squared_adj,
)