Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin_ssh/i38-generic-var-templates' i…
Browse files Browse the repository at this point in the history
…nto i39-single-val-per-month
  • Loading branch information
QSparks committed Oct 28, 2024
2 parents 4fa5cc8 + 18fd16f commit 7d026fc
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 10 deletions.
6 changes: 5 additions & 1 deletion tests/test_generic_scalar_stats.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ library(RUnit)
# Setup
wd <- "./"

# # compute.stat.scalar Tests
## compute.stat.scalar Tests

climdex.pcic.test.compute.stat.scalar.mean <- function() {
# Test data and dates
Expand Down Expand Up @@ -49,6 +49,7 @@ climdex.pcic.test.compute.stat.scalar.max <- function() {
checkEqualsNumeric(as.numeric(result[1]), expected_max)
}


climdex.pcic.test.scalar.exact.dates <- function() {
set.seed(123)

Expand Down Expand Up @@ -78,6 +79,7 @@ climdex.pcic.test.scalar.exact.dates <- function() {
"Expected exact date:", expected_max_date))
}


climdex.pcic.test.compute.stat.scalar.sum <- function() {

data <- c(1, 2, 3, 4, 5)
Expand All @@ -99,6 +101,7 @@ climdex.pcic.test.compute.stat.scalar.sum <- function() {
msg = paste("Computed sum:", result[1], "Expected sum:", expected_sum))
}


climdex.pcic.test.compute.stat.scalar.sd <- function() {

data <- c(2, 4, 6, 8, 10)
Expand All @@ -119,6 +122,7 @@ climdex.pcic.test.compute.stat.scalar.sd <- function() {
msg = paste("Computed SD:", result[1], "Expected SD:", expected_sd))
}


climdex.pcic.test.compute.stat.scalar.var <- function() {
# Test data and dates
data <- c(3, 6, 9, 12, 15)
Expand Down
9 changes: 8 additions & 1 deletion tests/test_generic_stat_helpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ climdex.pcic.test.convert.cartesian.to.polar <- function() {
checkTrue(is.na(result_zero$direction), "Expected direction to be NA when both u and v are 0")
}


climdex.pcic.test.convert.polar.to.cartesian <- function() {
speed <- sqrt(2)
direction <- 45
Expand All @@ -41,6 +42,7 @@ climdex.pcic.test.convert.polar.to.cartesian <- function() {

}


climdex.pcic.test.convert.degrees.to.cardinal <- function() {
# Test all 16 cardinal directions and boundary cases
degrees <- c(0, 22.5, 45, 67.5, 90, 112.5, 135, 157.5, 180,
Expand Down Expand Up @@ -72,9 +74,9 @@ climdex.pcic.test.convert.cardinal.to.degrees <- function() {
# Mixed-case input
mixed_case_result <- convert_cardinal_to_degrees(c('n', 'Ne', 'e', 'SE', 'S', 'sW', 'W', 'nw'))
checkEqualsNumeric(as.numeric(mixed_case_result), expected)

}


climdex.pcic.test.filter.by.direction.range <- function() {
primary_data <- 1:10
degrees <- seq(0, 360, length.out = 10)
Expand All @@ -101,6 +103,7 @@ climdex.pcic.test.filter.by.direction.range <- function() {
checkEquals(filtered$degrees, expected_degrees_cross_0, "Expected filtered degrees for range crossing 0 to have NA")
}


climdex.pcic.test.filter.by.direction.range.full.na <- function() {
primary_data <- 1:10
degrees <- seq(0, 360, length.out = 10) # 0 40 80 120 160 200 240 280 320 360
Expand All @@ -116,6 +119,7 @@ climdex.pcic.test.filter.by.direction.range.full.na <- function() {
checkEquals(filtered$degrees, expected_degrees, "Expected all degrees to be NA")
}


climdex.pcic.test.compute.circular.mean <- function() {
direction_degrees <- c(350, 10) # Should average to 0 degrees
date.factors <- 1
Expand All @@ -131,6 +135,7 @@ climdex.pcic.test.compute.circular.mean <- function() {
msg = "direction_degrees cannot be empty or NULL.")
}


climdex.pcic.test.compute.circular.mean.with.na <- function() {
direction_degrees <- c(350, 10, NA) # Should average to 0 degrees
date.factors <- 1
Expand All @@ -140,6 +145,7 @@ climdex.pcic.test.compute.circular.mean.with.na <- function() {
checkEqualsNumeric(result, expected, tolerance = 1e-6)
}


climdex.pcic.test.compute.circular.sd <- function() {
direction_degrees <- c(350, 10)
date.factors <- 1
Expand All @@ -152,6 +158,7 @@ climdex.pcic.test.compute.circular.sd <- function() {
msg = "direction_degrees cannot be empty or NULL.")
}


climdex.pcic.test.compute.circular.sd.with.na <- function() {
direction_degrees <- c(350, 10, NA)
date.factors <- 1
Expand Down
8 changes: 4 additions & 4 deletions tests/test_generic_variable_inputs.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
climdex.pcic.test.na_masks_with_missing_data_thresholds <- function() climdex.pcic.test.na_masks_with_missing_data_thresholds <- function() {
climdex.pcic.test.na_masks_with_missing_data_thresholds <- function() {
set.seed(123)
speed <- runif(366, 0, 20)
direction <- runif(366, 0, 360)
Expand Down Expand Up @@ -74,7 +74,6 @@ climdex.pcic.test.na_masks_with_missing_data_thresholds <- function() climdex.pc
}



climdex.pcic.test.scalar.raw.and.csv.construction <- function() {
set.seed(123)

Expand Down Expand Up @@ -112,6 +111,7 @@ climdex.pcic.test.scalar.raw.and.csv.construction <- function() {
checkTrue(all.equal(scalar_obj_csv, scalar_obj_raw), msg = "Scalar_obj built from csv is not identical to raw")
}


climdex.pcic.test.calendar.handling <- function() {
set.seed(123)

Expand Down Expand Up @@ -148,6 +148,7 @@ climdex.pcic.test.calendar.handling <- function() {
checkTrue(!any(format(scalar_obj_noleap@dates, "%m-%d") == "02-29"), "Leap day present in no-leap calendar dates.")
}


climdex.pcic.test.vector.raw.and.csv.construction <- function() {
set.seed(123)

Expand Down Expand Up @@ -259,6 +260,7 @@ climdex.pcic.test.vector.construction.validity <- function() {
checkEqualsNumeric(polar_data$direction, expected_polar$direction, tolerance = 1e-6, "Direction conversion from cartesian to polar incorrect.")
}


climdex.pcic.test.scalar_construction_with_malformed_inputs <- function() {
# Valid data
data <- c(10.5, 12.3, 11.2)
Expand Down Expand Up @@ -313,7 +315,6 @@ climdex.pcic.test.scalar_construction_with_malformed_inputs <- function() {
}



climdex.pcic.test.vector_construction_with_malformed_inputs <- function() {
# Setup valid data for comparison
valid_speed <- c(5, 10, 15)
Expand Down Expand Up @@ -434,6 +435,5 @@ climdex.pcic.test.vector_construction_with_malformed_inputs <- function() {
),
msg = "Error not raised for invalid calendar type."
)

}

13 changes: 9 additions & 4 deletions tests/test_generic_vector_stats.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ library(circular)
# Setup
wd <- "./"

# # compute.stat.vector Tests
## compute.stat.vector Tests
# Helper function to update primary and secondary values in a vector object. Dates need to be readded to avoid data-date length mismatch.
update_vector_obj <- function(base_vector_obj, primary, secondary, dates) {
return(climdexGenericVector.raw(
Expand Down Expand Up @@ -84,9 +84,6 @@ climdex.pcic.test.compute.stat.vector.circular.mean <- function() {
}





climdex.pcic.test.compute.stat.vector.circular.sd <- function() {

speed <- c(5, 5)
Expand Down Expand Up @@ -186,6 +183,7 @@ climdex.pcic.test.compute.stat.vector.mean.magnitude <- function() {
msg = paste("Result magnitude differs from expected:", result$magnitude))
}


climdex.pcic.test.compute.stat.vector.filtered.direction.range <- function() {

speed <- c(5, 10, 15, 20)
Expand Down Expand Up @@ -218,6 +216,7 @@ climdex.pcic.test.compute.stat.vector.filtered.direction.range <- function() {
checkEqualsNumeric(as.numeric(result$magnitude[1]), expected_max_magnitude)
}


climdex.pcic.test.compute.stat.vector.missing.values <- function() {
speed <- c(5, 10, NA, 20) # One NA speed
direction <- c(45, 90, 135, NA) # One NA direction
Expand Down Expand Up @@ -247,6 +246,7 @@ climdex.pcic.test.compute.stat.vector.missing.values <- function() {
msg = paste("Result magnitude differs from expected. Result:", result$magnitude))
}


climdex.pcic.test.compute.stat.vector.overlapping.years <- function() {
# Full winter season (Dec-Feb)
set.seed(123)
Expand Down Expand Up @@ -280,6 +280,7 @@ climdex.pcic.test.compute.stat.vector.overlapping.years <- function() {
"Expected seasonal mean:", expected_mean_speed))
}


climdex.pcic.test.compute.stat.vector.filtered.direction.crossing.360 <- function() {
speed <- c(5, 10, 15, 20)
direction <- c(350, 10, 135, 180) # Directions crossing the 360-degree boundary
Expand Down Expand Up @@ -309,6 +310,7 @@ climdex.pcic.test.compute.stat.vector.filtered.direction.crossing.360 <- functio
checkEqualsNumeric(as.numeric(result$magnitude[1]), expected_max_magnitude)
}


climdex.pcic.test.compute.stat.vector.no.data.in.direction.range <- function() {
speed <- c(5, 10, 15, 20)
direction <- c(45, 90, 135, 180)
Expand Down Expand Up @@ -338,6 +340,7 @@ climdex.pcic.test.compute.stat.vector.no.data.in.direction.range <- function() {
checkTrue(is.na(result$magnitude[1]), "Expected NA result when no data is in the specified direction range.")
}


climdex.pcic.test.compute.stat.vector.cartesian.format <- function() {
u <- c(5, 10, 15)
v <- c(5, 0, -5) # Cartesian components
Expand Down Expand Up @@ -386,6 +389,7 @@ climdex.pcic.test.compute.stat.vector.cartesian.format <- function() {
"Expected circular mean direction:", expected_circular_mean))
}


climdex.pcic.test.compute.stat.vector.cardinal.format <- function() {
speed <- c(5, 10, 15)
direction <- c('N', 'E', 'S') # Cardinal directions
Expand Down Expand Up @@ -415,6 +419,7 @@ climdex.pcic.test.compute.stat.vector.cardinal.format <- function() {
checkEqualsNumeric(as.numeric(result$magnitude[1]), expected_mean_speed, tolerance = 1e-6)
}


climdex.pcic.test.compute.stat.vector.inverted.direction.range <- function() {
speed <- c(5, 10, 15, 20)
direction <- c(45, 90, 135, 180)
Expand Down

0 comments on commit 7d026fc

Please sign in to comment.