Skip to content

Commit

Permalink
Increase test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
doccstat committed May 29, 2024
1 parent 528565f commit d82f00a
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 18 deletions.
2 changes: 1 addition & 1 deletion R/fastcpd_class.R
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ plot.fastcpd <- function( # nolint: cyclomatic complexity
...
) {
if (x@family == "custom") {
warning("Built-in plot should only work for built-in families.")
message("Built-in plot should only work for built-in families.")
}
if (x@family == "mean" && ncol(x@data) > 1) {
warning("Can not plot mean change points with p > 1.")
Expand Down
1 change: 0 additions & 1 deletion man/well_log.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 6 additions & 12 deletions src/fastcpd_class.cc
Original file line number Diff line number Diff line change
Expand Up @@ -115,22 +115,22 @@ Fastcpd::Fastcpd(

zero_data_c = (double **)malloc((data_n_rows + 1) * sizeof(double *));
if (zero_data_c == NULL) {
FATAL("Memory allocation failed.");
FATAL("Memory allocation failed."); // # nocov
}
unsigned int i = 0;
while (i < data_n_rows + 1) {
zero_data_c[i] = (double *)malloc(data_n_cols * sizeof(double));
if (zero_data_c[i] == NULL) {
break;
break; // # nocov
}
i++;
}
if (i < data_n_rows + 1) {
for (unsigned int j = 0; j < i; j++) {
for (unsigned int j = 0; j < i; j++) { // # nocov start
free(zero_data_c[j]);
}
free(zero_data_c);
FATAL("Memory allocation failed.");
FATAL("Memory allocation failed."); // # nocov end
}
for (unsigned int i = 0; i < data_n_rows + 1; i++) {
for (unsigned int j = 0; j < data_n_cols; j++) {
Expand Down Expand Up @@ -545,9 +545,7 @@ CostResult Fastcpd::get_optimized_cost(
) {
CostResult cost_result;
const mat data_segment = data.rows(segment_start, segment_end);
if (!(cost_gradient || cost_hessian)) {
cost_result = {{colvec()}, {colvec()}, as<double>((*cost)(data_segment))};
} else if (p == 1) {
if (p == 1) {
Environment stats = Environment::namespace_env("stats");
Function optim = stats["optim"];
List optim_result = optim(
Expand All @@ -570,7 +568,7 @@ CostResult Fastcpd::get_optimized_cost(
double value = as<double>(optim_result["value"]);
cost_result =
{{log(par / (1 - par))}, {colvec()}, exp(value) / (1 + exp(value))};
} else if (p > 1) {
} else {
Environment stats = Environment::namespace_env("stats");
Function optim = stats["optim"];
List optim_result = optim(
Expand All @@ -583,10 +581,6 @@ CostResult Fastcpd::get_optimized_cost(
);
cost_result =
{{as<colvec>(optim_result["par"])}, {colvec()}, optim_result["value"]};
} else {
// # nocov start
ERROR("This branch should not be reached.");
// # nocov end
}
return cost_result;
}
Expand Down
6 changes: 3 additions & 3 deletions src/fastcpd_class_nll.cc
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ CostResult Fastcpd::get_nll_pelt_lasso(
deviance = stats["deviance"];
List out = cv_glmnet(
data_segment.cols(1, data_segment.n_cols - 1),
data_segment.col(0),
data_segment.col(0), // # nocov
Named("family") = "gaussian"
);
colvec index_vec = as<colvec>(out["index"]),
Expand Down Expand Up @@ -546,9 +546,9 @@ CostResult Fastcpd::get_nll_pelt_meanvariance(
}

return {
{zeros<colvec>(p)},
{zeros<colvec>(p)}, // # nocov
{mat()},
(d * std::log(2.0 * M_PI) + d + log(det_value)) * (segment_length) / 2.0
(d * std::log(2.0 * M_PI) + d + log(det_value)) * segment_length / 2.0
};
}

Expand Down
1 change: 0 additions & 1 deletion tests/testthat/examples/data-well_log-quantile.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ if (requireNamespace("matrixStats", quietly = TRUE)) {
}

result@residuals <- matrix(residual)
result@family <- "mean"
result@data <- data.frame(x = c(well_log))
plot(result)
}
Expand Down

0 comments on commit d82f00a

Please sign in to comment.