1818# ' \itemize{
1919# ' \item vector of `p` integers - manually selected initial points, can be output of
2020# ' previous another endmember extraction method, e.g. VCA
21+ # ' \item list of numeric vectors - multiple initializations, each element should be
22+ # ' a vector of `p` integers representing indices
23+ # ' \item function - a callable function that takes (data, p) as arguments and returns
24+ # ' either numeric indices or a list with 'indices' element (e.g. output of other methods)
2125# ' \item random - randomly selected points
2226# ' \item projections - selecting the two extreme points of the
2327# ' projections of the data onto random vectors
24- # ' \item coordinates - selecting the two extreme points of the
25- # ' projections of the data onto the coordinate axes
28+ # ' \item coordinates_sequence - selecting extreme points along coordinate axes
29+ # ' in a deterministic sequential manner
30+ # ' \item coordinates_random - selecting extreme points along coordinate axes
31+ # ' with randomization
2632# ' }
2733# ' Default: "projections" is used.
2834# '
@@ -136,18 +142,24 @@ nfindr <- function (x, ...) {
136142 paste0(" Trivial case: " , estimator , " - endmembers in the inner-most loop" ),
137143 {
138144 unmixR.options(debuglevel = 0L )
139- result <- nfindr(data , p , indices , iter = " endmembers" , estimator = estimator )
145+ suppressWarnings({
146+ result <- nfindr(data , p , indices , iter = " endmembers" , estimator = estimator )
147+ })
140148 expect_equal(sort(result $ indices ), best_indices )
141149 expect_equal(names(result ), c(" indices" ))
142150
143151 unmixR.options(debuglevel = 1L )
144- result <- nfindr(data , p , indices , iter = " endmembers" , estimator = estimator )
152+ suppressWarnings({
153+ result <- nfindr(data , p , indices , iter = " endmembers" , estimator = estimator )
154+ })
145155 expect_equal(result $ iterations_count , 2 )
146156 expect_equal(result $ replacements_count , 3 )
147157 expect_equal(names(result ), c(" indices" , " iterations_count" , " replacements_count" ))
148158
149159 unmixR.options(debuglevel = 2L )
150- result <- nfindr(data , p , indices , iter = " endmembers" , estimator = estimator )
160+ suppressWarnings({
161+ result <- nfindr(data , p , indices , iter = " endmembers" , estimator = estimator )
162+ })
151163 expect_equal(
152164 result $ replacements [[1 ]],
153165 rbind(
@@ -206,18 +218,24 @@ nfindr <- function (x, ...) {
206218 {
207219
208220 unmixR.options(debuglevel = 0L )
209- result <- nfindr(data , p , indices , iter = " both" , estimator = estimator )
221+ suppressWarnings({
222+ result <- nfindr(data , p , indices , iter = " both" , estimator = estimator )
223+ })
210224 expect_equal(sort(result $ indices ), best_indices )
211225 expect_equal(names(result ), c(" indices" ))
212226
213227 unmixR.options(debuglevel = 1L )
214- result <- nfindr(data , p , indices , iter = " both" , estimator = estimator )
228+ suppressWarnings({
229+ result <- nfindr(data , p , indices , iter = " both" , estimator = estimator )
230+ })
215231 expect_equal(result $ iterations_count , 4 )
216232 expect_equal(result $ replacements_count , 3 )
217233 expect_equal(names(result ), c(" indices" , " iterations_count" , " replacements_count" ))
218234
219235 unmixR.options(debuglevel = 2L )
220- result <- nfindr(data , p , indices , iter = " both" , estimator = estimator )
236+ suppressWarnings({
237+ result <- nfindr(data , p , indices , iter = " both" , estimator = estimator )
238+ })
221239 expect_equal(
222240 result $ replacements [[1 ]],
223241 rbind(
@@ -257,10 +275,21 @@ nfindr <- function (x, ...) {
257275 paste(" Non-trivial case:" , estimator , iterator ),
258276 # The iteration steps and the final solution must be the same as we use
259277 # straightforward volume calculation
260- expect_equal(
261- nfindr(data , p , indices , iter = iterator , estimator = estimator ),
262- nfindr(data , p , indices , iter = iterator , estimator = " volume" )
263- )
278+ {
279+ if ((estimator == " height" ) && (iterator %in% c(" endmembers" , " both" ))) {
280+ expect_warning(
281+ result <- nfindr(data , p , indices , iter = iterator , estimator = estimator ),
282+ " This combination of iterator and volume change estimator is not optimized"
283+ )
284+ } else {
285+ result <- nfindr(data , p , indices , iter = iterator , estimator = estimator )
286+ }
287+
288+ expect_equal(
289+ result ,
290+ nfindr(data , p , indices , iter = iterator , estimator = " volume" )
291+ )
292+ }
264293 )
265294 }
266295 }
@@ -278,6 +307,135 @@ nfindr <- function (x, ...) {
278307 })
279308 }
280309
310+ # # Test init ----
311+ unmixR.options(debuglevel = 2L )
312+
313+ # Test string-based initialization methods
314+ test_that(" String-based initialization methods" , {
315+ set.seed(123 )
316+
317+ # Test projections initialization
318+ result_proj <- nfindr(data , p , init = " projections" )
319+ expect_equal(length(result_proj $ indices ), p )
320+ expect_true(all(result_proj $ indices %in% 1 : nrow(data )))
321+
322+ # Test coordinates_sequence initialization
323+ result_coords_seq <- nfindr(data , p , init = " coordinates_sequence" )
324+ expect_equal(
325+ result_coords_seq $ replacements [[1 ]][1 ,],
326+ extreme_coordinates(data , p , random = FALSE )$ indices
327+ )
328+
329+ # Test coordinates_random initialization
330+ set.seed(1234 )
331+ result_coords_rand <- nfindr(data , p , init = " coordinates_random" )
332+ expect_equal(
333+ result_coords_rand $ replacements [[1 ]][1 ,],
334+ {set.seed(1234 ); extreme_coordinates(data , p , random = TRUE )$ indices }
335+ )
336+
337+ # Test random initialization
338+ set.seed(1234 )
339+ result_random <- nfindr(data , p , init = " random" )
340+ expect_equal(
341+ result_random $ replacements [[1 ]][1 ,],
342+ {set.seed(1234 ); sample(nrow(data ), p )}
343+ )
344+ })
345+
346+ # Test numeric vector initialization
347+ test_that(" Numeric vector initialization" , {
348+ manual_indices <- c(1 , 5 , 10 , 15 )
349+ result <- nfindr(data , p , init = manual_indices )
350+ expect_equal(result $ replacements [[1 ]][1 ,], manual_indices )
351+ })
352+
353+ # Test list initialization
354+ test_that(" List initialization" , {
355+ init_list <- list (
356+ c(1 , 5 , 10 , 15 ),
357+ c(2 , 8 , 12 , 18 ),
358+ c(3 , 7 , 11 , 16 )
359+ )
360+ expect_warning(
361+ result <- nfindr(data , p , init = init_list ),
362+ " n_init.*is ignored"
363+ )
364+ expect_equal(length(result $ replacements ), length(init_list ))
365+ expect_true(
366+ all(
367+ sapply(1 : length(init_list ), function (i ) result $ replacements [[i ]][1 ,] == init_list [[i ]])
368+ )
369+ )
370+ })
371+
372+ # Test function initialization
373+ test_that(" Function initialization" , {
374+ # Test function that returns numeric indices
375+ custom_init_numeric <- function (data , p ) {
376+ return (sample(1 : nrow(data ), p ))
377+ }
378+
379+ set.seed(456 )
380+ result_numeric <- nfindr(data , p , init = custom_init_numeric )
381+ expect_equal(length(result_numeric $ indices ), p )
382+ expect_true(all(result_numeric $ indices %in% 1 : nrow(data )))
383+
384+ # Test function that returns list with indices element
385+ custom_init_list <- function (data , p ) {
386+ indices <- sample(1 : nrow(data ), p )
387+ return (list (indices = indices ))
388+ }
389+
390+ set.seed(789 )
391+ result_list <- nfindr(data , p , init = custom_init_list )
392+ expect_equal(length(result_list $ indices ), p )
393+ expect_true(all(result_list $ indices %in% 1 : nrow(data )))
394+ })
395+
396+ # Test error conditions for initialization
397+ test_that(" Initialization error conditions" , {
398+ # Test invalid list elements
399+ invalid_list <- list (c(1 , 2 ), c(3 , 4 , 5 )) # Different lengths
400+ expect_error(nfindr(data , p , init = invalid_list , n_init = 2 ))
401+
402+ # Test function that returns invalid output
403+ invalid_function <- function (data , p ) {
404+ return (" invalid" )
405+ }
406+ expect_error(nfindr(data , p , init = invalid_function ))
407+
408+ # Test invalid string
409+ expect_error(nfindr(data , p , init = " invalid_method" ))
410+ })
411+
412+ # Test that n_init warnings are properly issued
413+ test_that(" n_init warnings" , {
414+ # Test with numeric vector
415+ expect_warning(
416+ nfindr(data , p , init = c(1 , 5 , 10 , 15 ), n_init = 3 ),
417+ " n_init.*is ignored"
418+ )
419+
420+ # Test with list
421+ expect_warning(
422+ nfindr(data , p , init = list (c(1 , 5 , 10 , 15 )), n_init = 3 ),
423+ " n_init.*is ignored"
424+ )
425+ })
426+
427+ # Test reproducibility with seed
428+ test_that(" Reproducibility with seed" , {
429+ set.seed(999 )
430+ result1 <- nfindr(data , p , init = " random" )
431+
432+ set.seed(999 )
433+ result2 <- nfindr(data , p , init = " random" )
434+
435+ expect_equal(result1 $ indices , result2 $ indices )
436+ })
437+
438+
281439 # # Test the formula interface ----
282440 # -> nfindr.formula has its own test
283441
0 commit comments