NMRglue in TopSpin

172 views
Skip to first unread message

Jani Rahkila

unread,
Sep 14, 2018, 1:38:57 AM9/14/18
to nmrglue-discuss
Hi,

Has anyone been able to make NMRglue work from within topspin?

I am having some trouble importing the module, or more specifically the problem is with importing numpy. The path and working directory should be correct, but still one of the files fails.

This is the error I get:

Traceback (most recent call last):
  File "C:/Bruker/TopSpin3.5.b.88pl7/exp/stan/nmr/py/user/pulcon_test.py", line 15, in <module>
    import nmrglue
  File "C:\Users\jrahkila\AppData\Local\Programs\Python\Python35-32\Lib\site-packages\nmrglue\__init__.py", line 1, in <module>
    from .fileio import *
  File "C:\Users\jrahkila\AppData\Local\Programs\Python\Python35-32\Lib\site-packages\nmrglue\fileio\__init__.py", line 1, in <module>
    from . import bruker
  File "C:\Users\jrahkila\AppData\Local\Programs\Python\Python35-32\Lib\site-packages\nmrglue\fileio\bruker.py", line 34, in <module>
    import numpy as np
  File "C:\Users\jrahkila\AppData\Local\Programs\Python\Python35-32\Lib\site-packages\numpy\__init__.py", line 142, in <module>
    from . import add_newdocs
  File "C:\Users\jrahkila\AppData\Local\Programs\Python\Python35-32\Lib\site-packages\numpy\add_newdocs.py", line 13, in <module>
    from numpy.lib import add_newdoc
  File "C:\Users\jrahkila\AppData\Local\Programs\Python\Python35-32\Lib\site-packages\numpy\lib\__init__.py", line 8, in <module>
    from .type_check import *
ImportError: No module named type_check

The file type_check is in the lib folder and importing numpy outside of TopSpin works fine.

If I add the numpy\lib folder to sys.path, it works, but then gives me another error. I have tried with both python versions 3.5 and 2.7 .

Has anyone had a similar problem that they have been able to solve?

-Jani

Kaustubh Mote

unread,
Sep 26, 2018, 2:25:03 AM9/26/18
to nmrglue-discuss
It is not possible to do this natively in Topspin, since Topspin runs Jython rather than Cpython. Numpy and Scipy are hard requirements for NMRGlue, and are not compatible with Jython. That being said, there is a hack: You can use Jython to run an arbitrary script (using the subprocesses module). This can very well be a NMRGlue script (or MATNMR or NMRPipe). 

For a couple of examples on how to do this, see https://github.com/kaustubhmote/pulseseq/tree/master/python (The documentation is non-existent at this point, but I'll get around to writing it). Very simply, this Jython code uses the interface to Topspin to get some parameters from the user from within Topspin, writes a CPython script that actually processes the data, passes the user parameters as arguments to the Cpython script and executes it. If there are errors running either the Jython or the CPython code, they should be printed to the screen within Topspin.

You will need to make a one of change before this works for you: In the file 'base.py', change the line that points to the CPython that you want to use.
Put all of the files in the '<topspin>/exp/stan/nmr/py/user' folder and make sure nmrglue, numpy and scipy are accessible to the CPython you are using.
To be able to successfully read the files in topspin, you need to have (i) pdata folder inside your EXPNO and (ii) acqu(N) files in the folder along with the acqu(N)s. 
(i) is taken care by the Cpython script and (ii) is best done from within NMRGlue (I'll push a PR to do that soon). But in the meanwhile, you can just copy over the required files in the appropriate directory.

Kaustubh.

Jani Rahkila

unread,
Sep 26, 2018, 3:53:12 AM9/26/18
to nmrglue-discuss
Thank you very much for this.

I am getting the following error when running the script (I haven't thought about points i and ii yet, but I don't think it is related to that)

Cannot run program "C:\Python37-32" (in directory "C:\Bruker\TopSpin3.5.b.88pl7\prog\curdir\****-jrahkila"): CreateProcess error=5, Access is denied

It is the line p = Popen... that is causing it, and it shouldn't be a problem with permissions as I have given all users full access to that folder.

-Jani

Kaustubh Mote

unread,
Sep 26, 2018, 5:20:07 AM9/26/18
to nmrglue-discuss
I will troubleshoot this sometime later tomorrow, but I must say I didn't test this on Windows, although I did try and keep all the paths OS agnostic.

In the meanwhile, you can try:
1. Run just the CPython script (or some other python script) manually in the same folder and see if there are truly no issues with the permissions. 
2. Use 'shell=True' argument for Popen (this routes the program through 'cmd.exe'). So the corresponding line will be:
p = Popen([cpython, scriptname, curdir, iexpno, oexpno, phase], stdin=PIPE, stdout=PIPE, stderr=STDOUT, shell=True)

Let me know if this solves the immediate issue.
Kaustubh.

Jani Rahkila

unread,
Sep 26, 2018, 7:32:20 AM9/26/18
to nmrglue-discuss
Adding shell=True fixed the issue with permissions but gave another error that pointed me to the actual problem. I had to add python.exe at the end of the cpython path. Now it works without problems.

Is there a particular reason why you use a temporary file instead of just using the topspin script as sort of a general bootstrapper and then the actual nmrglue script as a separate file?

Btw, copying the files can easily be done within topspin with the 'wrpa' command which copies the parameter set and processed data to a new expno.

-Jani

Kaustubh Mote

unread,
Sep 26, 2018, 9:36:11 AM9/26/18
to nmrglue-discuss


On Wednesday, September 26, 2018 at 5:02:20 PM UTC+5:30, Jani Rahkila wrote:
Adding shell=True fixed the issue with permissions but gave another error that pointed me to the actual problem. I had to add python.exe at the end of the cpython path. Now it works without problems.

That is great. I will make a note of that in the script. I can have shell=True by default, although this is not considered a safe practice. But this should not be an issue in this case.
 
Is there a particular reason why you use a temporary file instead of just using the topspin script as sort of a general bootstrapper and then the actual nmrglue script as a separate file?

I was initially using Topspin-Jython as a wrapper from nmrglue, with a separate file in a 'cpython' folder. But I felt this was better because the nmrglue script itself can be dynamically changed from within the Jython script and you can process the same dataset in multiple ways and store it separately (ala some of the Bruker Python scripts for acquiring data). At some point I will make a function to write out the processing script in the pdata folders as well.
   

Btw, copying the files can easily be done within topspin with the 'wrpa' command which copies the parameter set and processed data to a new expno.

Ya, that works. I dont want to remember to execute a separate command from Topspin though. From the Jython script, WR() and WR_PATH() commands along with PUTPAR() may suffice for many cases, but it involves going repeatedly to the correct experiment number and executing these. Nmrglue API feels more natural for both writing and debugging (or maybe I am just used to it). So as a rule, I am going with all processing + file handling using nmrglue and using Jython only to pass the user defined parameters and display the output/errors.   
Reply all
Reply to author
Forward
0 new messages