r-spatial / dtwSat

Time-Weighted Dynamic Time Warping for satellite image time series analysis
https://www.victor-maus.com/dtwSat/
GNU General Public License v3.0
131 stars 39 forks source link

error in dtwSat::twdtwApply() invalid type 'list' #38

Closed yesdavid closed 2 years ago

yesdavid commented 5 years ago

Dear Victor,
I am trying to execute your dtwSat::twdtwApply() function but I run into the following problem:

matches <- dtwSat::twdtwApply(x = abc, y = patt, weight.fun = log_fun, keep = TRUE)

Error in max(nrow(object), na.rm = TRUE) : invalid 'type' (list) of argument.

with:

str(abc)

Formal class 'twdtwTimeSeries' [package "dtwSat"] with 2 slots  
  ..@ timeseries:List of 1  
  .. ..$ ts1:‘zoo’ series from 2017-07-17 to 2019-04-18  
  Data: num [1:133] 0.241 0.286 0.327 0.359 0.369 ...  
  Index:  Date[1:133], format: "2017-07-17" "2017-07-22" "2017-07-27" "2017-08-01" ...  
  ..@ labels    : Factor w/ 1 level "ts1": 1  

and:

str(patt)

Formal class 'twdtwTimeSeries' [package "dtwSat"] with 2 slots
  ..@ timeseries:List of 4
  .. ..$ class_1:‘zoo’ series from 2018-04-15 to 2018-09-12
  Data: num [1:31] 0.153 0.162 0.177 0.2 0.232 ...
  Index:  Date[1:31], format: "2018-04-15" "2018-04-20" "2018-04-25" "2018-04-30" ...
  .. ..$ class_2:‘zoo’ series from 2017-09-15 to 2018-07-12
  Data: num [1:61] 0.171 0.219 0.287 0.376 0.418 ...
  Index:  Date[1:61], format: "2017-09-15" "2017-09-20" "2017-09-25" "2017-09-30" ...
  .. ..$ class_3:‘zoo’ series from 2018-10-15 to 2019-04-18
  Data: num [1:38] 0.195 0.204 0.222 0.249 0.281 ...
  Index:  Date[1:38], format: "2018-10-15" "2018-10-20" "2018-10-25" "2018-10-30" ...
  .. ..$ class_4:‘zoo’ series from 2017-10-15 to 2018-08-11
  Data: num [1:61] 0.24 0.267 0.294 0.324 0.358 ...
  Index:  Date[1:61], format: "2017-10-15" "2017-10-20" "2017-10-25" "2017-10-30" ...
  ..@ labels    : Factor w/ 4 levels "class_1","class_2",..: 1 2 3 4

I already noticed that the structure of my data differs from yours, as far as both your zoo-files have character format data (and not num and Date as mine).
Is this the problem?

Best, David

nk027 commented 5 years ago

Hey!

This is because the data in your objects only has one dimension. @vwmaus, I'm not sure if that is expected (in that case there should be an error message) or if that was an oversight.

I can replicate this when adjusting the README:

ts <- twdtwTimeSeries(MOD13Q1.ts[,1])
MOD13Q1.patterns.list[[1]] <- MOD13Q1.patterns.list[[1]][,1]
patt <- twdtwTimeSeries(MOD13Q1.patterns.list)
matches <- twdtwApply(x = ts, y = patt, weight.fun = log_fun, keep = TRUE)

When patt is unchanged (from the README) the error message is: dim(X) must have a positive length.

nk027 commented 5 years ago

As a temporary fix you can try the following:

ts <- twdtwTimeSeries(MOD13Q1.ts[,1])
MOD13Q1.patterns.list[[1]] <- MOD13Q1.patterns.list[[1]][,1]
MOD13Q1.patterns.list[[2]] <- MOD13Q1.patterns.list[[2]][,1]
MOD13Q1.patterns.list[[3]] <- MOD13Q1.patterns.list[[3]][,1]
patt <- twdtwTimeSeries(MOD13Q1.patterns.list)
ts <- resampleTimeSeries(ts, 93L)
patt <- resampleTimeSeries(patt, 23L)
matches <- twdtwApply(x = ts, y = patt, weight.fun = log_fun, keep = TRUE)

This works because you specifically provide the length - the data is then transformed to have two dimensions (e.g. from 23 to 23, 1).

vwmaus commented 5 years ago

subseting with [ ] drops a dimention from zoo objects if only one column is left. Just add drop=FALSE e.g.,

library(dtwSat)

ts <- twdtwTimeSeries(MOD13Q1.ts[,1,drop=FALSE])
MOD13Q1.patterns.list[[1]] <- MOD13Q1.patterns.list[[1]][,1,drop=FALSE]
MOD13Q1.patterns.list[[2]] <- MOD13Q1.patterns.list[[2]][,1,drop=FALSE]
MOD13Q1.patterns.list[[3]] <- MOD13Q1.patterns.list[[3]][,1,drop=FALSE]
patt <- twdtwTimeSeries(MOD13Q1.patterns.list)
matches <- twdtwApply(x = ts, y = patt, weight.fun = logisticWeight(-0.1, 100), keep = TRUE)