Important: Read this before posting to this forum

  1. This forum is for questions related to the use of Apollo. We will answer some general choice modelling questions too, where appropriate, and time permitting. We cannot answer questions about how to estimate choice models with other software packages.
  2. There is a very detailed manual for Apollo available at http://www.ApolloChoiceModelling.com/manual.html. This contains detailed descriptions of the various Apollo functions, and numerous examples are available at http://www.ApolloChoiceModelling.com/examples.html. In addition, help files are available for all functions, using e.g. ?apollo_mnl
  3. Before asking a question on the forum, users are kindly requested to follow these steps:
    1. Check that the same issue has not already been addressed in the forum - there is a search tool.
    2. Ensure that the correct syntax has been used. For any function, detailed instructions are available directly in Apollo, e.g. by using ?apollo_mnl for apollo_mnl
    3. Check the frequently asked questions section on the Apollo website, which discusses some common issues/failures. Please see http://www.apollochoicemodelling.com/faq.html
    4. Make sure that R is using the latest official release of Apollo.
  4. If the above steps do not resolve the issue, then users should follow these steps when posting a question:
    1. provide full details on the issue, including the entire code and output, including any error messages
    2. posts will not immediately appear on the forum, but will be checked by a moderator first. This may take a day or two at busy times. There is no need to submit the post multiple times.

Jupyter IRkernel compatibility

Report bugs or highlight issues with Apollo functions. At a minimum, please include the part of the output where you believe the bug is manifested. Ideally, please share your model file.
Post Reply
kathoef
Posts: 1
Joined: 17 Aug 2022, 12:52

Jupyter IRkernel compatibility

Post by kathoef »

A colleague is preparing course materials for the upcoming semester, in which Apollo amongst other things is used with Jupyter notebooks and the R Jupyter kernel (https://github.com/IRkernel/IRkernel) on a JupyterHub system running at our university. Therein, he was experiencing problems with a dying Jupyter kernel. The server logs say:
[D 2022-08-17 13:20:55.100 ServerApp] Accepting token-authenticated connection from 172.17.0.1
[D 2022-08-17 13:20:55.101 ServerApp] 200 GET /api/kernels?1660735255082 (172.17.0.1) 1.47ms
Error in as.environment(pos) :
no item called "jupyter:irkernel" on the search list
Calls: <Anonymous> ... handle_shell -> <Anonymous> -> add_to_user_searchpath -> assign
In addition: Warning message:
In sink() : no sink to remove
[D 2022-08-17 13:20:55.726 ServerApp] activity on 327c5e71-f71f-444d-897e-78c677061646: status (busy)
Execution halted
Error in detach("jupyter:irkernel") : invalid 'name' argument
Calls: <Anonymous> -> <Anonymous> -> detach
[D 2022-08-17 13:20:55.727 ServerApp] activity on 327c5e71-f71f-444d-897e-78c677061646: execute_input
[D 2022-08-17 13:20:55.769 ServerApp] 304 GET /static/favicons/favicon-busy-1.ico (172.17.0.1) 1.07ms
[D 2022-08-17 13:20:55.856 ServerApp] Accepting token-authenticated connection from 172.17.0.1
The code works well in R Studio Server and also from the R console, but not from the R Jupyter kernel. All of the tests were done in the same Docker container environment, so there should be no impact from e.g. software enviroment version differences.

I think, I was able to pin down the problem to the apollo_initialise() function,

Code: Select all

apollo_initialise <- function()
{
  doDetach <- !grepl("^(.GlobalEnv|package:|tools:|Autoloads|CheckExEnv|TempEnv)", search())
  doDetach <- (search())[which(doDetach)]
  if(length(doDetach)>0) for(i in 1:length(doDetach)) detach(pos=(which(doDetach[i]==search()))[1])
  if(sink.number()>0) sink()
  cat("Apollo ignition sequence completed\n")
}
which apparently alters a few R environment things after R startup.

From using the R command line,

Code: Select all

$ R

R version 4.2.1 (2022-06-23) -- "Funny-Looking Kid"
Copyright (C) 2022 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)

> library("apollo")
Apollo 0.2.7
http://www.ApolloChoiceModelling.com
See url for a detailed manual, examples and a user forum.
Sign up to to user forum to receive updates on new releases.

Your version of Apollo is more than six months old.
Using the latest version will ensure you have all
 current functionality and bug fixes.
You can update to the latest version by typing:
 install.packages("apollo")
> doDetach <- !grepl("^(.GlobalEnv|package:|tools:|Autoloads|CheckExEnv|TempEnv)", search())
> doDetach <- (search())[which(doDetach)]
> doDetach
[1] "org:r-lib"
> search()
 [1] ".GlobalEnv"        "package:apollo"    "package:stats"    
 [4] "package:graphics"  "package:grDevices" "package:utils"    
 [7] "package:datasets"  "package:methods"   "Autoloads"        
[10] "org:r-lib"         "package:base"
and from using the R Jupyter kernel,

Code: Select all

[1] library("apollo")
Apollo 0.2.7
http://www.ApolloChoiceModelling.com
See url for a detailed manual, examples and a user forum.
Sign up to to user forum to receive updates on new releases.

Your version of Apollo is more than six months old.
Using the latest version will ensure you have all
 current functionality and bug fixes.
You can update to the latest version by typing:
 install.packages("apollo")
[2] doDetach <- !grepl("^(.GlobalEnv|package:|tools:|Autoloads|CheckExEnv|TempEnv)", search())
[3] doDetach <- (search())[which(doDetach)]
[4] doDetach
'jupyter:irkernel'
[5] search()
'.GlobalEnv''package:apollo''jupyter:irkernel''package:stats''package:graphics''package:grDevices''package:utils''package:datasets''package:methods''Autoloads''package:base'
one can see that by using the apollo_initialise() function, the "jupyter:irkernel" package gets detached, which I assume then causes the R Jupyter kernel death. For the sake of completeness, the R Jupyter kernel is started in the following way

Code: Select all

$ cat /usr/local/share/jupyter/kernels/ir/kernel.json 
{
  "argv": ["/usr/lib/R/bin/R", "--slave", "-e", "IRkernel::main()", "--args", "{connection_file}"],
  "display_name": "R",
  "language": "R"
}
I assume that changing the line

Code: Select all

doDetach <- !grepl("^(.GlobalEnv|package:|tools:|Autoloads|CheckExEnv|TempEnv)", search())
to something like

Code: Select all

doDetach <- !grepl("^(.GlobalEnv|package:|tools:|Autoloads|CheckExEnv|TempEnv|jupyter:irkernel)", search())
in the apollo_initialise() function would fix the problem, but since I am neither an R nor an Apollo user myself, I do not know about the implications. As a workaround, I will suggest to my colleague to not use the apollo_initialise() function in Jupyter notebooks. But of course, it would be really great, if we could soon install from an Apollo package version, in which this problem is fixed.
dpalma
Posts: 190
Joined: 24 Apr 2020, 17:54

Re: Jupyter IRkernel compatibility

Post by dpalma »

Hi,

Sorry for the long delay in replying.

We usually work on R through the RStudio IDE, so I am afraid we have no experience with Jupyter notebooks. So we very much appreciate your insight on this.

Indeed it looks like apollo_initialise is the problem. We will test your proposal for a future version of Apollo. For now, you can simply skip the call to apollo_initialise. That function is only there as a safeguard in case the user forgot to close a sink(), in which case they would not see any output to the console; or if there was a very dramatic interruption of the estimation process, which it really shouldn't happen. So I would say that you can safely skip the call to apollo_initialise.

Best wishes
David
Post Reply