codon
codon copied to clipboard
`JITError: <jit>:3:5: name 'a' is not defined`
Upon running this code example from the docs in a JupyterLab notebook:
import codon
from time import time
def is_prime_python(n):
if n <= 1:
return False
for i in range(2, n):
if n % i == 0:
return False
return True
@codon.jit(debug=True)
def is_prime_codon(n):
if n <= 1:
return False
for i in range(2, n):
if n % i == 0:
return False
return True
t0 = time()
ans = sum(1 for i in range(100000, 200000) if is_prime_python(i))
t1 = time()
print(f'[python] {ans} | took {t1 - t0} seconds')
t0 = time()
ans = sum(1 for i in range(100000, 200000) if is_prime_codon(i))
t1 = time()
print(f'[codon] {ans} | took {t1 - t0} seconds')
I get the following traceback:
[codon::jit::execute] code:
def is_prime_codon(n):
if n <= 1:
return False
for i in range(2, n):
if n % i == 0:
return False
return True
-----
[python] 8392 | took 49.380319595336914 seconds
[python] is_prime_codon(['int'])
[codon::jit::executePython] wrapper:
@export
def __codon_wrapped__is_prime_codon_0(args: cobj) -> cobj:
a-----
---------------------------------------------------------------------------
JITError Traceback (most recent call last)
Input In [2], in <cell line: 27>()
24 print(f'[python] {ans} | took {t1 - t0} seconds')
26 t0 = time()
---> 27 ans = sum(1 for i in range(100000, 200000) if is_prime_codon(i))
28 t1 = time()
29 print(f'[codon] {ans} | took {t1 - t0} seconds')
Input In [2], in <genexpr>(.0)
24 print(f'[python] {ans} | took {t1 - t0} seconds')
26 t0 = time()
---> 27 ans = sum(1 for i in range(100000, 200000) if is_prime_codon(i))
28 t1 = time()
29 print(f'[codon] {ans} | took {t1 - t0} seconds')
File ~/.conda/envs/my_env/lib/python3.10/site-packages/codon/decorator.py:207, in jit.<locals>._decorate.<locals>.wrapped(*args, **kwargs)
202 if debug:
203 print(
204 "[python] {}({})".format(f.__name__, list(types)),
205 file=sys.stderr,
206 )
--> 207 return _jit.run_wrapper(
208 obj_name, types, f.__module__, pyvars, args, 1 if debug else 0
209 )
210 except JITError:
211 _reset_jit()
File ~/.conda/envs/my_env/lib/python3.10/site-packages/codon/jit.pyx:42, in codon.codon_jit.JITWrapper.run_wrapper()
JITError: <jit>:3:5: name 'a' is not defined
In fact, I get this exception for any function decorated with @codon.jit
when run in JupyterLab. It is slightly better when I use the decorator in .py
scripts that I call from the terminal (so it is likely related to the conda environment I'm working in): There, the above function will run, but others will yield the same error.
I tried Python versions 3.10.6 and 3.11.0.
Here is my uname -a
:
Linux XPS-9310-9VVH1J3 5.14.0-1054-oem #61-Ubuntu SMP Fri Oct 14 13:05:50 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux
i get the same error
That's an exciting tool! Thank you, dear developers :)
I get the same error as above, also running in a JupyterLab Notebook. Here's a minimal breaking example:
import codon
@codon.jit
def returns_n(n):
return n
returns_n(1)
with codon-jit==0.1.3
in python==3.9.5
on a Ubuntu machine.
Error message:
---------------------------------------------------------------------------
JITError Traceback (most recent call last)
<command-291970291081557> in <cell line: 7>()
5 return n
6
----> 7 returns_n(1)
/.../lib/python3.9/site-packages/codon/decorator.py in wrapped(*args, **kwargs)
213 file=sys.stderr,
214 )
--> 215 return _jit.run_wrapper(
216 obj_name, types, f.__module__, pyvars, args, 1 if debug else 0
217 )
/.../lib/python3.9/site-packages/codon/jit.pyx in codon.codon_jit.JITWrapper.run_wrapper()
JITError: <jit>:3:5: name 'a' is not defined
''' JITError:
:3:5: name 'a' is not defined
That is pretty funny because name 'a' does not even appear on your sample code. I was finding the same error and tried to debug but completely mislead by the following fact: given that I had used the fib.py example (the Non recurive one) where there is an ACTUAL variable named a . LOL
Thank you for this exciting tool! I get the same error...
Very exciting. Though I get the same error with @codon.jit when run is_prime_codon() in django. is_prime_codon() works when run in a .py file from shell.
I am getting the same error using the ubuntu 22.04 docker image, python 3.10.1 and latest codon version.
Code/Error
[codon::jit::execute] code:
def compute_theoretical_fragments(
sequence_length: int,
fragment_types: List[str],
charges: List[int] = [-1],
neutral_losses: List[str] = [],
internal: bool = True) -> List[str]:
ion_directions = {
"a": "n-term",
"b": "n-term",
"x": "c-term",
"y": "c-term",
"cdot": "n-term",
"c": "n-term",
"c-1": "n-term",
"c+1": "n-term",
"zdot": "c-term",
"z+1": "c-term",
"z+2": "c-term",
"z+3": "c-term",
}
n_term_ions = [ion_type for ion_type in fragment_types if ion_directions[ion_type] == "n-term"]
c_term_ions = [ion_type for ion_type in fragment_types if ion_directions[ion_type] == "c-term"]
n_term = ["t:" + ion_type for ion_type in n_term_ions]
c_term = [ion_type + ":t" for ion_type in c_term_ions]
# terminal fragments
n_term_frags = [
n_term_frag + "@1:" + str(i + 1) for n_term_frag in n_term for i in range(sequence_length - 1)
]
c_term_frags = [
c_term_frag + "@" + str(i) + ":" + str(sequence_length)
for c_term_frag in c_term
for i in range(2, sequence_length + 1)
]
charges_str = [
"(" + str(int(charge)) + ")" if int(charge) < 0 else "(+" + str(int(charge)) + ")"
for charge in charges
]
n_term_frags_with_charges = [
n_term_frag + charge for n_term_frag in n_term_frags for charge in charges_str
]
c_term_frags_with_charges = [
c_term_frag + charge for c_term_frag in c_term_frags for charge in charges_str
]
neutral_losses_str = ["[" + nl + "]" for nl in neutral_losses]
neutral_losses_str.append("")
n_term_frags_with_nl = [
n_term_frag + nl for n_term_frag in n_term_frags_with_charges for nl in neutral_losses_str
]
c_term_frags_with_nl = [
c_term_frag + nl for c_term_frag in c_term_frags_with_charges for nl in neutral_losses_str
]
internal_frags_with_nl = []
if internal:
# internal fragments
internal = [
n_term_ion + ":" + c_term_ion for n_term_ion in n_term_ions for c_term_ion in c_term_ions
]
internal_pos = [
str(i) + ":" + str(j)
for i in range(2, sequence_length)
for j in range(2, sequence_length)
if i <= j
]
internal_frags = [
internal_ions + "@" + internal_positions
for internal_ions in internal
for internal_positions in internal_pos
]
internal_frags_with_charges = [
internal_frag + charge for internal_frag in internal_frags for charge in charges_str
]
internal_frags_with_nl = [
internal_frag + nl
for internal_frag in internal_frags_with_charges
for nl in neutral_losses_str
]
return n_term_frags_with_nl + c_term_frags_with_nl + internal_frags_with_nl
-----
[python] compute_theoretical_fragments(['int', 'List[str]', 'List[int]', 'List[str]', 'bool'])
[codon::jit::executePython] wrapper:
@export
def __codon_wrapped__compute_theoretical_fragments_0(args: cobj) -> cobj:
a-----
2023-05-26 19:23:42.709 Uncaught app exception
Traceback (most recent call last):
File "/usr/local/lib/python3.10/dist-packages/streamlit/runtime/scriptrunner/script_runner.py", line 565, in _run_script
exec(code, module.__dict__)
File "/fragannot/gui/streamlit_app.py", line 212, in <module>
main()
File "/fragannot/gui/streamlit_app.py", line 209, in main
main_page()
File "/fragannot/gui/streamlit_app.py", line 125, in main_page
result = fragannot_call(spectrum_file,
File "/fragannot/gui/util/fragannot_call.py", line 31, in fragannot_call
fragannot_dict = frag.fragment_annotation(output_name_prefix + identifications_file.name,
File "/fragannot/gui/util/fragannot_codon.py", line 38, in fragment_annotation
return fragment_annotation(ident_file, spectra_file, tolerance,
File "/fragannot/gui/util/fragannot_codon.py", line 94, in fragment_annotation
theoretical_fragment_code = compute_theoretical_fragments(
File "/usr/local/lib/python3.10/dist-packages/codon/decorator.py", line 215, in wrapped
return _jit.run_wrapper(
File "codon/jit.pyx", line 42, in codon.codon_jit.JITWrapper.run_wrapper
codon.codon_jit.JITError: <jit>:3:5: name 'a' is not defined