Ask Your Question
0

problems with attach on Mac

asked 2017-01-14 19:10:09 -0500

mhfrey gravatar image

updated 2017-01-16 10:58:44 -0500

I am having problems with attaching a file to a notebook. At first Sage cannot find cython, when I removed the cython calls sage cannot find parallel. See the code fragments and error below.

The code works as written when contained in the notebook. Am I doing something wrong or is this a bug?

Thank you for your help.

Mike

The code is:

...
cython("""
import numpy as np
import scipy as sp
from scipy import linalg
cimport numpy as np
cdef extern from "math.h":
    float cosf(float theta)
    float sinf(float theta)
def shape_calc_cy(Omega_array_stuff):
    Omega_array_pt = Omega_array_stuff[0]
    rf_shape = Omega_array_stuff[1]
    t_step = Omega_array_stuff[2]
    phi_p = Omega_array_stuff[3]

    def pulse(Omega_0, omega_nut, t_p, phi_p):
        Brot = np.matrix([[0,-Omega_0,omega_nut*sinf(phi_p)],[Omega_0,0,-omega_nut*cosf(phi_p)],[-omega_nut*sinf(phi_p),omega_nut*cosf(phi_p),0]],dtype = np.complex128)
        return sp.linalg.expm(np.dot(Brot,t_p))
    MPs = np.array([0,0,1],dtype = np.complexfloating)
    for s in range(len(rf_shape)):
        tmp = MPs
        MPs = np.dot(pulse(Omega_array_pt, rf_shape[s], t_step, phi_p[s]),tmp)
    return MPs
""")
...

The error is:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "_sage_input_5.py", line 10, in <module>
    exec compile(u'open("___code___.py","w").write("# -*- coding: utf-8 -*-\\n" + _support_.preparse_worksheet_cell(base64.b64decode("dz13YWxsdGltZSgpCmF0dGFjaCAoIi9Vc2Vycy9taWNoYWVsL01IRl9CbG9jaF9uX1Bsb3Rfb3JpX2N5dGhvbi5zYWdlIikgIyBBdHRhY2ggbXkgQmxvY2ggcm91dGluZXMKYXR0YWNoZWRfZmlsZXMoKQ=="),globals())+"\\n"); execfile(os.path.abspath("___code___.py"))
  File "", line 1, in <module>

  File "/private/var/folders/j8/d2zfx1m16zv25klkgh71rjv40000gn/T/tmpcqrA7g/___code___.py", line 3, in <module>
    attach ("/Users/michael/MHF_Bloch_n_Plot_ori_cython.sage") # Attach my Bloch routines
  File "sage/misc/lazy_import.pyx", line 389, in sage.misc.lazy_import.LazyImport.__call__ (/Users/michael/sage-7.5/src/build/cythonized/sage/misc/lazy_import.c:4007)
  File "/Users/michael/sage-7.5/local/lib/python2.7/site-packages/sage/repl/attach.py", line 349, in attach
    load(filename, globals(), attach=True)
  File "/Users/michael/sage-7.5/local/lib/python2.7/site-packages/sage/repl/load.py", line 242, in load
    exec(code, globals)
  File "/Users/michael/.sage/temp/Michaels-MBP.freystuff.com/83858/MHF_Bloch_n_Plot_ori_cython.sagemdLt2C.py", line 193, in <module>
    cython("""
NameError: name 'cython' is not define

The parallel code:

import multiprocessing
@parallel('fork', ncpus = (multiprocessing.cpu_count()))
def shape_calc(Omega_array_stuff):
    return shape_calc_cy(Omega_array_stuff)

The parallel error is:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "_sage_input_7.py", line 10, in <module>
    exec compile(u'open("___code___.py","w").write("# -*- coding: utf-8 -*-\\n" + _support_.preparse_worksheet_cell(base64.b64decode("dz13YWxsdGltZSgpCmF0dGFjaCAoIi9Vc2Vycy9taWNoYWVsL01IRl9CbG9jaF9uX1Bsb3Rfbm9fY3l0aG9uLnNhZ2UiKSAjIEF0dGFjaCBteSBCbG9jaCByb3V0aW5lcwphdHRhY2hlZF9maWxlcygp"),globals())+"\\n"); execfile(os.path.abspath("___code___.py"))
  File "", line 1, in <module>

  File "/private/var/folders/j8/d2zfx1m16zv25klkgh71rjv40000gn/T/tmpuKkIzm/___code___.py", line 3, in <module>
    attach ("/Users/michael/MHF_Bloch_n_Plot_no_cython.sage") # Attach my Bloch routines
  File "sage/misc/lazy_import.pyx", line 389, in sage.misc.lazy_import.LazyImport.__call__ (/Users/michael/sage-7.5/src/build/cythonized/sage/misc/lazy_import.c:4007)
  File "/Users/michael/sage-7.5/local/lib/python2.7/site-packages/sage/repl/attach.py", line 349, in attach
    load(filename, globals(), attach=True)
  File "/Users/michael/sage-7.5/local/lib/python2.7/site-packages/sage/repl/load.py", line 242, in load
    exec(code, globals)
  File "/Users/michael/.sage/temp/Michaels-MBP.freystuff.com/83858/MHF_Bloch_n_Plot_no_cython.sagesTGITt.py", line 216, in <module>
    @parallel('fork', ncpus = (multiprocessing.cpu_count()))
NameError: name 'parallel' is not defined
edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted
0

answered 2017-01-16 19:35:29 -0500

mhfrey gravatar image

updated 2017-01-17 10:33:48 -0500

I removed the cython code, got past the parallel issue, and now have 2 other issues.

I have a couple of function definitions that are returning an error during attach when a default value is set:

def Eval_pulse(B1_field_ev, beta_ev, phi_p_ev, Rrangerf_ev, Omega_0_ev, Cpointsrf_ev, pname_ev, Tst_ev, Amp_ev, scaler_ev = 1):

Returns:

def Eval_pulse(B1_field_ev, beta_ev, phi_p_ev, Rrangerf_ev, Omega_0_ev, Cpointsrf_ev, pname_ev, Tst_ev, Amp_ev, scaler_ev = Integer(1)):
NameError: name 'Integer' is not defined

If I remove the default values the code imports but when I execute the code it returns:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "_sage_input_7.py", line 10, in <module>
    exec compile(u'open("___code___.py","w").write("# -*- coding: utf-8 -*-\\n" + _support_.preparse_worksheet_cell(base64.b64decode("I1B1bHNlIFNoYXBlIGlzIHNpbXBsZSBleHByZXNzaW9uCkJ3X2osIHBfaiwgcG1faiwgdF9qID0gRXZhbF9wdWxzZShCMV9maWVsZCwgYmV0YSwgcGhpX3AsIFJyYW5nZXJmLCBPbWVnYV8wLCBDcG9pbnRzcmYsIHBuYW1lLCBUc3QsIEFtcCwxLjAp"),globals())+"\\n"); execfile(os.path.abspath("___code___.py"))
  File "", line 1, in <module>

  File "/private/var/folders/j8/d2zfx1m16zv25klkgh71rjv40000gn/T/tmpIh729o/___code___.py", line 3, in <module>
    exec compile(u'Bw_j, p_j, pm_j, t_j = Eval_pulse(B1_field, beta, phi_p, Rrangerf, Omega_0, Cpointsrf, pname, Tst, Amp,_sage_const_1p0 )
  File "", line 1, in <module>

NameError: name 'Eval_pulse' is not defined

Eval_Pulse is defined in the file being attached.

What am I missing? The syntax and options for attached files appears to be very different from inline cope in a notebook.

Thank you for your help

Mike

edit flag offensive delete link more

Comments

I think I fixed the problem. I changed from attach(...) to sage.repl.load.load("/Users/michael/stuff_no_cython.sage",globals())

Thank you for your help.

Mike

mhfrey gravatar imagemhfrey ( 2017-01-16 19:42:11 -0500 )edit
0

answered 2017-01-16 11:00:54 -0500

updated 2017-01-16 11:03:52 -0500

Rather than using

cython(
import numpy as np
...
)

you should just have

import numpy as np
...

and save the file as "FILE.pyx". The "pyx" suffix will tell Sage that it is a Cython file.

For the @parallel issue, you need to add an import: add the line

from sage.parallel.decorate import parallel

at the top of your file.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2017-01-14 19:10:09 -0500

Seen: 75 times

Last updated: Jan 17