vrunge / gfpop

Other
11 stars 7 forks source link

Uninformative error message when graph has factors #11

Open julianstanley opened 4 years ago

julianstanley commented 4 years ago

Today I imported a .csv file specifying a constraint graph and passed it through gfpop::graph(). I tried to use that graph to run gfpop, but got an error that was a bit confusing:

Error in x[[jj]][iseq] <- vjj : replacement has length zero

The problem is that, when I import a graph from file, I need to set stringsAsFactors to false. I saw the same problem and solution with read.csv and data.table::fread.

I worked through that issue here in gfpop-gui and @tdhock suggested posting here.

Example:

# This graph works perfectly
iso_graph <- gfpop::graph(penalty = 15, type = "isotonic")
data <- gfpop::dataGenerator(50, changepoints = c(1), parameters = c(1))
gfpop::gfpop(data = data, mygraph = iso_graph, type = "mean")$changepoints
# [1]  50

# If I export and re-import the graph, it gives a confusing error
write.csv(iso_graph, "graphtest.csv", row.names = FALSE)
iso_graph_import <- gfpop::graph(read.csv("graphtest.csv"))
gfpop::gfpop(data = gfpop_data$data, mygraph = iso_graph_import, type = "mean")$changepoints
# Error in x[[jj]][iseq] <- vjj : replacement has length zero

# Specifying that strings shouldn't be factors fixes the problem
iso_graph_import <- gfpop::graph(read.csv("graphtest.csv", stringsAsFactors = FALSE))
gfpop::gfpop(data = data, mygraph = iso_graph_import, type = "mean")$changepoints
# [1] 50
vrunge commented 4 years ago

Hi Julian,

I've just made a small modification of the gfpop R function to take in account your problem. Adding mygraph$state1 <- as.character(mygraph$state1) mygraph$state2 <- as.character(mygraph$state2) mygraph$type <- as.character(mygraph$type) into this function enforces the right type and you don't have to use the stringAsFactors option anymore. Thank you for noticing this message error. Are you ok with this solution?

Vincent

julianstanley commented 4 years ago

Hey Vincent,

Perfect! My previous example runs perfectly now:

> iso_graph <- gfpop::graph(penalty = 15, type = "isotonic")
> data <- gfpop::dataGenerator(50, changepoints = c(1), parameters = c(1))
> gfpop::gfpop(data = data, mygraph = iso_graph, type = "mean")$changepoints
[1] 50
> 
> write.csv(iso_graph, "graphtest.csv", row.names = FALSE)
> iso_graph_import <- gfpop::graph(read.csv("graphtest.csv"))
> gfpop::gfpop(data = data, mygraph = iso_graph_import, type = "mean")$changepoints
[1] 50

I'll write a quick test for this and pull request if that's okay?

vrunge commented 4 years ago

That's okay, thanks!