Ipython notebook crash using rmagic extension - ipython

I am trying to run the rmagic functions extension example, but ipython crashes with console error message '\u used without hex digits in character string starting "c:\u". I suspect that this is an R error message caused by rmagic passing c:\path instead of c:\ or c:/.
There is probably an easy way to fix this (IPython or Notebook startup parameters?) , but, as a newbie to rmagic and rpy2, I need some expert help please.
The following simple snippet from the example causes the kernel to die:
import rpy2
%load_ext rmagic
%R x=1
I am a windows user...

Found a solution to this ipython/windows problem here

I am a Windows user. I went to my IPython extention folder which for me is: C:\Python27\Lib\site-packages\IPython\extensions
and opened rmagic.py for editing. Found the line
self.r('png("%s/Rplots%%03d.png",%s)' % (tmpd, png_args))
and replaced it with the line:
self.r('png("%s/Rplots%%03d.png",%s, type="cairo")' % (tmpd.replace('\\', '/'), png_args))
The reason you do this is described here
and here

Related

Python 3.7 app seems to stop connecting to internet when I use cx_Freeze to turn the app into an executable

I'm trying to create a python app that will check for newer versions of itself on GitHub. The way it checks is by finding a txt file in the repository that has the latest version number on it. My code works just fine and does what I want it to when running it with PyCharm but when I use cx_Freeze and run the exe it has trouble.
I started by using requests which worked fine until freezing, then I switched to urllib3 which had the same result. I tried simply pinging google which worked after freezing so I know it's not an issue with connecting to the internet.
from urllib3 import PoolManager
version_url = "https://raw.githubusercontent.com/redscientific/CompanionApp/master/Version.txt"
def get_data():
mgr = PoolManager()
r = mgr.request("GET", version_url)
...then I parse the data etc.
Before I freeze it I get the results I need but after I freeze it it seems to have trouble at r = mgr.request("GET", version_url)
I don't know what error it's having because I don't know how to get errors back from a .exe other than printing lines to a file but it won't get to any lines after mgr.request() so I can't output anything after the error happens.
I guess my question is how do I get it to work after I freeze it?
The error it spits out is as follows:
urllib3.exceptions.SSLError: Can't connect to HTTPS URL because the SSL module is not available.
Apparently cx_Freeze does not automatically add a couple of required .dll files. A fix I have found upon more googling is described here.
Try to run your exe from a cmd prompt or terminal, you should be able to see the error message in the terminal window then.
If you still prefer to print the exception to a file from within the application, you can use a try ... except block to catch the exception and use then traceback.print_exc() to print the exception, see How to print the full traceback without halting the program?
Assuming the line r = mgr.request("GET", version_url) causes the exception, replace it by:
import traceback
try:
r = mgr.request("GET", version_url)
except Exception:
with open('logfile.txt', 'w') as f:
traceback.print_exc(file=f)

execute jupyter notebook and keep attached to existing kernel

I know about nbconvert and use it to generate static html or ipynb files with the results output. However, I want to be able to generate a notebook that stays attached to a kernel that I already have running, so that I can do further data exploration after all of the template cells have been run. Is there a way to do that?
Apparently, you can do this through the Python API. I didn't try it myself, but for someone who will be looking for a solution, this PR has an example in the comments:
from nbconvert.preprocessors.execute import executenb, ExecutePreprocessor
from nbformat import read as nbread
from jupyter_client.manager import start_new_kernel
nb = nbread('parsee.ipynb', as_version=4)
kernel_name = nb.metadata.get('kernelspec', {}).get('name', 'python')
km, kc = start_new_kernel(kernel_name=kernel_name)
executenb(nb, kernel=(km, kc))
kc.execute_interactive('a') # a is a variable defined in parsee.ipynb with 'a = 1'
Not quite sure about your purpose. But my general solutions are,
to execute the notebook in command line and see the execution at the same time,
jupyter nbconvert --debug --allow-errors --stdout --execute test.ipynb
this will show the execute through all cells in debug mode even exception happens. but I can't see the result until the end of the execution.
to output the result to a html file, and then open the html file to see the results. I found this is more convenient.
jupyter nbconvert --execute --allow-errors --stdout test.ipynb >> result.html 2>&1
if you open result.html, it will be,
and all the errors and results will be shown on the page.
I would like to learn other answers/solutions from you all. thank you.
If I understood correctly you wish to open a Python console, and connect Jupyter notebook to that kernel instance?
Perhaps your solution would be to edit jupyter scripts itself and run the server in separate thread/background task implementing some sort of connection between threads and work in the jupyter console? Currently it's impossible because main thread is running the server.
This would require some work and I don't have any solution as-is, but I will look into that and maybe edit this answer if I can make it work.
But it seems that the easiest solution is to simply add another field in the notebook and do whatever you wish to do there. Is there a reason for not doing that?

Lisp loading error: undefined character in dispatch macro

I decided my birthday would be a good time to try and fix my Lisp problems.
I've received a Lisp program from someone to run an experiment. It works well on his OS X environment. However, I can't get it to run on either Windows 7 or Ubuntu. The program uses Act-R 6.2 to run a model. When I try to load the .lisp file I receive the following error:
:Reader error on #<BASIC-FILE-CHARACTER-INPUT-STREAM ("*path*"/28 UTF-8)#xCFAD1AE, near position 1048, within "smile* (#initWithCo":
Undefined character #\/ in a #\# dispatch macro.
While executing: CCL:SIGNAL-READER-ERROR, in process listener(1)
The error is the exact same for Windows 7 and Ubuntu. Here is the console output if it is required:
http://i.stack.imgur.com/dFXVm.png
It seems load-turing-application.lisp loads multiple other files, one of which is turing-application-v1.lisp, in which the line (setf *smile* (#/initWithContentsOfFile: (#/alloc ns:ns-image)(ccl::%make-nsstring "smiley.jpg")))
seems to be the culprit. I suspect it has something to do with the #/-notation.
Any ideas would be appreciated.
This is answered in the comments, but #/ is a CCL syntax that only is available on OS X. This code is not expected to work on linux or windows.

Execute batch file using dos()

I got a problem when executing batch file commands through matlab. This batch file includes commands to run simulations in Adams. When I execute the batch file directly from DOS window, it works well. But if I use matlab to execute it (using command dos()), it gives error saying 'cannot check out the license for Adams'.
This confuses me: if the license is incorrect, it should not work no matter I execute the batch file directly in DOS or ask MATLAB to execute it. I also tried to execute other DOS commands through matlab using dos() and it worked well.
Does anyone know what the problem may be?
Such issues are commonly caused by some environment variables being changed or cleared by MATLAB. I have very similar experience on Linux and Mac OS X, where this causes havoc when using system or unix.
In Unix-like systems, MATLAB is started from a shell script where all of this happens. So you can either incorporate missing variables there or in the .matlab7rc.sh in your home directory (the latter is preserved when you upgrade MATLAB and it is much easier to use). I won't go into all the Unix details here.
An alternative workaround is to explicitly set those variables when you issue a system command (e.g. system('export variable=value ; ...')). It is quite a bit of work, but you can then use that MATLAB code on different computers with ease.
On Windows, I'm not completely sure about the exact location of the corresponding files (and whether MATLAB starts in quite a similar way as on Unix). But if they exist, you can probably find it in the MATLAB documentation.
Anyhow, the alternative fix should work here as well.
First you need to diagnose which system variables you need (likely a PATH or anything that has a name related to Adams).
To do so in Windows, run set from the Windows command prompt (cmd.exe) and from within MATLAB. Whatever differs in the output is a possible suspect for your problem.
To inspect just a single variable, you can use the command echo %variablename%.
I will assume that you have found that the suspect environment variable is missing and should be set to value.
The workaround fix is then to run your command in MATLAB as system('set suspect=value & ...') where you replace ... with your original command.

Simultaneously displaying and capturing standard out in IPython?

I'm interested in implementing a behavior in IPython that would be like a combination of ! and !!. I'm trying to use an IPython terminal as an adjunct to my (Windows) shell. For a long running command (e.g., a build script) I would like to be able to watch the output as it streams by as ! does. I would also like to capture the output of the command into the output history as !! does, but this defers printing anything until all output is available.
Does anyone have any suggestions as to how to implement something like this? I'm guessing that a IPython.utils.io.Tee() object would be useful here, but I don't know enough about IPython to hook this up properly.
Here is a snippet of code I just tried in iPython notebook v2.3, which seems to do what was requested:
import sys
import IPython.utils.io
outputstream = IPython.utils.io.Tee("outputfile.log", "w", channel="stdout")
outputstream.write("Hello worlds!\n")
outputstream.close()
logstream=open("outputfile.log", "r")
sys.stdout.write("Read back from log file:\n")
sys.stdout.write(logstream.read())
The log file is created in the same directory as the iPython notebook file, and the output from running this cell is displayed thus:
Hello worlds!
Read back from log file:
Hello worlds!
I haven't tried this in the iPython terminal, but see no reason it wouldn't work as well there.
(Researched and answered as part of the Oxford participation in http://aaronswartzhackathon.org)