On Jun 22, 2011, at 6:55 AM, Ted Horst wrote: > Seem like it would be more consistent to raise an ImportError rather than > return None. That would also mean less change to existing code.
That would defeat the whole purpose of the function, except when we need to check for versions or you want to emit a warning. Remember that this is for *optional* dependencies, so it will always be used in the case where we catch the ImportError and do some handling. > Thinking a bit further, could this be done in an import hook (I haven't done > one myself, so not sure)? I've never heard of those. I'll check it out and see if it's any better. Aaron Meurer > > Ted > > On 2011-06-22, at 02:09, Aaron Meurer wrote: > >> So far, I've coded up an import_module function, that wraps the try, >> except ImportError logic into a single unified function. It has >> support for checking the module version and the Python version and >> returning None if they are too old, even if the module is installed, >> and it also supports raising warnings when the module cannot be used. >> You can see the code at >> https://github.com/asmeurer/sympy/tree/external. >> >> I've yet to apply this everywhere in SymPy (I will do it tomorrow). I >> still need to decide if it will be good enough to use >> import_module('numpy', min_python_version=(2, 6)) everywhere in the >> quantum module, or if I should centralize numpy importing in that >> module even further. >> >> But it should a *lot* less code, i.e., instead of >> >> import sys >> import warnings >> >> try: >> if sys.version_info < (2, 6): >> warnings.warn("Python version too old to use numpy") >> raise ImportError >> import numpy >> except ImportError: >> <do numpy not supported stuff> >> >> in every single quantum file that uses numpy (including the tests), we >> would just have >> >> from sympy.external import import_module >> numpy = import_module('numpy', min_python_version=(2, 6)) >> if not numpy: >> <do numpy not supported stuff> >> >> Aaron Meurer >> >> On Mon, Jun 20, 2011 at 6:11 PM, Aaron Meurer <asmeu...@gmail.com> wrote: >>> It should at least be in a separate file for the quantum code. I >>> didn't consider to make one file for it for all of SymPy. I'm waiting >>> to see what Brian Granger thinks, since he knows the most about the >>> quantum module. >>> >>> Aaron Meurer >>> >>> On Mon, Jun 20, 2011 at 6:06 PM, Ted Horst <ted.ho...@earthlink.net> wrote: >>>> Thanks for investigating, Aaron. Maybe numpy importing should be pulled >>>> out >>>> into its own module. This would probably be necessary anyway if someone >>>> implements the "numpy array for sympy" idea that was talked about a while >>>> back. >>>> >>>> Ted >>>> >>>> On 2011-06-20, at 00:42, Aaron Meurer wrote: >>>> >>>>> The way the quantum code is written, it's not very easy to disable >>>>> numpy globally. I could do it, but it would require copying the same >>>>> four lines checking the system version and emitting a warning in the >>>>> half dozen files that import numpy. >>>>> >>>>> So, to fix this right, I need advice from Brian and others who know >>>>> the quantum code. Where would be the best place to put some kind of >>>>> global parameter defining if numpy is supported for the quantum code >>>>> (similar to what the polys use for gmpy)? >>>>> >>>>> Aaron Meurer >>>>> >>>>> On Sun, Jun 19, 2011 at 8:38 PM, Aaron Meurer <asmeu...@gmail.com> wrote: >>>>>> >>>>>> OK, this is definitely a bug in numpy (or Python). I created a file >>>>>> numpyfloattest.py with the following contents: >>>>>> >>>>>> import numpy >>>>>> >>>>>> class A(object): >>>>>> def __float__(self): >>>>>> return 2.0 >>>>>> def __complex__(self): >>>>>> return 5.0j >>>>>> >>>>>> a = A() >>>>>> >>>>>> print numpy.matrix([[a]], dtype='complex') >>>>>> >>>>>> And ran: >>>>>> >>>>>> Aaron-Meurer:sympy aaronmeurer(0.7.0$%)$python2.5 numpyfloattest.py >>>>>> [[ 2.+0.j]] >>>>>> Aaron-Meurer:sympy aaronmeurer(0.7.0$%)$python2.7 numpyfloattest.py >>>>>> [[ 0.+5.j]] >>>>>> >>>>>> So for some reason, in Python 2.5, it runs complex(float()) on the >>>>>> data. Of course, this doesn't work very well if the numbers are >>>>>> complex, like sympy.I. >>>>>> >>>>>> I have created http://projects.scipy.org/numpy/ticket/1872 for this. >>>>>> In the meanwhile, I will just disable numpy in the quantum code in >>>>>> Python 2.5. >>>>>> >>>>>> Aaron Meurer >>>>>> >>>>>> On Sun, Jun 19, 2011 at 8:04 PM, Aaron Meurer <asmeu...@gmail.com> wrote: >>>>>>> >>>>>>> I think I have a lead. In numpy 1.6, this instead gives >>>>>>> >>>>>>>>>> numpy.matrix([[0, I]], dtype='complex') >>>>>>> >>>>>>> Traceback (most recent call last): >>>>>>> File "<stdin>", line 1, in <module> >>>>>>> File >>>>>>> "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/numpy/matrixlib/defmatrix.py", >>>>>>> line 257, in __new__ >>>>>>> arr = N.array(data, dtype=dtype, copy=copy) >>>>>>> File "sympy/core/expr.py", line 129, in __float__ >>>>>>> raise ValueError("Symbolic value, can't compute") >>>>>>> ValueError: Symbolic value, can't compute >>>>>>> >>>>>>> in Python 2.5 (and it works in Python 2.6 and 2.7). So I guess the >>>>>>> bug is in SymPy somewhere, and numpy 1.5 was catching that ValueError >>>>>>> and re-throwing it as a TypeError. >>>>>>> >>>>>>> Aaron Meurer >>>>>>> >>>>>>> On Sun, Jun 19, 2011 at 7:39 PM, Aaron Meurer <asmeu...@gmail.com> >>>>>>> wrote: >>>>>>>> >>>>>>>> On Sun, Jun 19, 2011 at 7:32 PM, Aaron Meurer <asmeu...@gmail.com> >>>>>>>> wrote: >>>>>>>>> >>>>>>>>> I don't know much about numpy, so this one has me stumped. For some >>>>>>>>> reason numpy.matrix([[0, I]], dtype='complex') fails in Python 2.5 >>>>>>>>> (with the above error), but not in Python 2.6 or 2.7 (I is sympy.I). >>>>>>>>> >>>>>>>>> Can someone with numpy knowledge give any insight here? Otherwise, I >>>>>>>>> think the solution might have to be to disable numpy support in the >>>>>>>>> quantum code in Python 2.5. >>>>>>>>> >>>>>>>>> I haven't looked at why it kills the test runner yet. I'll look at >>>>>>>>> that now. >>>>>>>> >>>>>>>> Oh, this is rather obvious. It's because the exception is raised at >>>>>>>> import time. If it were raised when running an actual test, it would >>>>>>>> be caught by the test runner. This is probably OK behavior (anyway, >>>>>>>> it's not top priority to change it). >>>>>>>> >>>>>>>> Aaron Meurer >>>>>>>> >>>>>>>>> >>>>>>>>> By the way, a workaround might be to do np.matrix([map(complex, i) for >>>>>>>>> i in m.tolist()], dtype=complex), i.e., manually convert the elements >>>>>>>>> of the Matrix to complex, but this is rather hackish and I would >>>>>>>>> prefer to aviod it if possible. >>>>>>>>> >>>>>>>>> Aaron Meurer >>>>>>>>> >>>>>>>>> >>>>>>>>> On Sun, Jun 19, 2011 at 5:36 PM, Aaron Meurer <asmeu...@gmail.com> >>>>>>>>> wrote: >>>>>>>>>> >>>>>>>>>> Thanks for reporting this. It seems you've uncovered two bugs (the >>>>>>>>>> second one is that the error killed the test runner). >>>>>>>>>> >>>>>>>>>> I'll see if I can figure out how to fix at least the numpy one for >>>>>>>>>> the >>>>>>>>>> release. Do you know how to fix it? If so, please send a pull >>>>>>>>>> request. >>>>>>>>>> >>>>>>>>>> Aaron Meurer >>>>>>>>>> >>>>>>>>>> On Sat, Jun 18, 2011 at 1:03 PM, Ted Horst <ted.ho...@earthlink.net> >>>>>>>>>> wrote: >>>>>>>>>>> >>>>>>>>>>> I'm a little late to the party, but I just tested 0.7.0 tip and got >>>>>>>>>>> this >>>>>>>>>>> error (stopped testing): >>>>>>>>>>> >>>>>>>>>>> This is with python 2.5.1, numpy 1.5.0, no scipy, no gmpy on OSX >>>>>>>>>>> 10.5.8 PPC >>>>>>>>>>> (big endian). >>>>>>>>>>> >>>>>>>>>>> I can get the error from just importing >>>>>>>>>>> sympy.physics.quantum.matrixcache. >>>>>>>>>>> It makes sense because we are passing sympy.I with dtype 'complex' >>>>>>>>>>> to >>>>>>>>>>> numpy. >>>>>>>>>>> >>>>>>>>>>> Ted >>>>>>>>>>> >>>>>>>>>>> Traceback (most recent call last): >>>>>>>>>>> File "setup.py", line 269, in <module> >>>>>>>>>>> 'audit' : audit, >>>>>>>>>>> File >>>>>>>>>>> >>>>>>>>>>> "/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/distutils/core.py", >>>>>>>>>>> line 151, in setup >>>>>>>>>>> dist.run_commands() >>>>>>>>>>> File >>>>>>>>>>> >>>>>>>>>>> "/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/distutils/dist.py", >>>>>>>>>>> line 974, in run_commands >>>>>>>>>>> self.run_command(cmd) >>>>>>>>>>> File >>>>>>>>>>> >>>>>>>>>>> "/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/distutils/dist.py", >>>>>>>>>>> line 994, in run_command >>>>>>>>>>> cmd_obj.run() >>>>>>>>>>> File "setup.py", line 161, in run >>>>>>>>>>> if sympy.test(): >>>>>>>>>>> File "sympy/utilities/runtests.py", line 181, in test >>>>>>>>>>> return t.test(sort=sort) >>>>>>>>>>> File "sympy/utilities/runtests.py", line 487, in test >>>>>>>>>>> self.test_file(f) >>>>>>>>>>> File "sympy/utilities/runtests.py", line 499, in test_file >>>>>>>>>>> execfile(filename, gl) >>>>>>>>>>> File >>>>>>>>>>> >>>>>>>>>>> "/users/ted/developer/opensource/sympy/sympy_github2/sympy/physics/quantum/tests/test_gate.py", >>>>>>>>>>> line 4, in <module> >>>>>>>>>>> from sympy.physics.quantum.gate import (XGate, YGate, ZGate, >>>>>>>>>>> random_circuit, >>>>>>>>>>> File "sympy/physics/quantum/gate.py", line 31, in <module> >>>>>>>>>>> from sympy.physics.quantum.matrixcache import matrix_cache >>>>>>>>>>> File "sympy/physics/quantum/matrixcache.py", line 92, in <module> >>>>>>>>>>> matrix_cache.cache_matrix('Y', Matrix([[0, -I], [I, 0]])) >>>>>>>>>>> File "sympy/physics/quantum/matrixcache.py", line 38, in >>>>>>>>>>> cache_matrix >>>>>>>>>>> self._numpy_matrix(name, m) >>>>>>>>>>> File "sympy/physics/quantum/matrixcache.py", line 72, in >>>>>>>>>>> _numpy_matrix >>>>>>>>>>> m = to_numpy(m, dtype=self.dtype) >>>>>>>>>>> File "sympy/physics/quantum/matrixutils.py", line 99, in to_numpy >>>>>>>>>>> return sympy_to_numpy(m, dtype=dtype) >>>>>>>>>>> File "sympy/physics/quantum/matrixutils.py", line 53, in >>>>>>>>>>> sympy_to_numpy >>>>>>>>>>> return np.matrix(m.tolist(), dtype=dtype) >>>>>>>>>>> File "numpy/matrixlib/defmatrix.py", line 257, in __new__ >>>>>>>>>>> TypeError: a float is required >>>>>>>>>>> >>>>>>>>>>> -- >>>>>>>>>>> You received this message because you are subscribed to the Google >>>>>>>>>>> Groups >>>>>>>>>>> "sympy" group. >>>>>>>>>>> To post to this group, send email to sympy@googlegroups.com. >>>>>>>>>>> To unsubscribe from this group, send email to >>>>>>>>>>> sympy+unsubscr...@googlegroups.com. >>>>>>>>>>> For more options, visit this group at >>>>>>>>>>> http://groups.google.com/group/sympy?hl=en. >>>>>>>>>>> >>>>>>>>>>> >>>>>>>>>> >>>>>>>>> >>>>>>>> >>>>>>> >>>>>> >>>>> >>>>> -- >>>>> You received this message because you are subscribed to the Google Groups >>>>> "sympy" group. >>>>> To post to this group, send email to sympy@googlegroups.com. >>>>> To unsubscribe from this group, send email to >>>>> sympy+unsubscr...@googlegroups.com. >>>>> For more options, visit this group at >>>>> http://groups.google.com/group/sympy?hl=en. >>>>> >>>> >>>> -- >>>> You received this message because you are subscribed to the Google Groups >>>> "sympy" group. >>>> To post to this group, send email to sympy@googlegroups.com. >>>> To unsubscribe from this group, send email to >>>> sympy+unsubscr...@googlegroups.com. >>>> For more options, visit this group at >>>> http://groups.google.com/group/sympy?hl=en. >>>> >>>> >>> >> >> -- >> You received this message because you are subscribed to the Google Groups >> "sympy" group. >> To post to this group, send email to sympy@googlegroups.com. >> To unsubscribe from this group, send email to >> sympy+unsubscr...@googlegroups.com. >> For more options, visit this group at >> http://groups.google.com/group/sympy?hl=en. >> > > -- > You received this message because you are subscribed to the Google Groups > "sympy" group. > To post to this group, send email to sympy@googlegroups.com. > To unsubscribe from this group, send email to > sympy+unsubscr...@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/sympy?hl=en. > -- You received this message because you are subscribed to the Google Groups "sympy" group. To post to this group, send email to sympy@googlegroups.com. To unsubscribe from this group, send email to sympy+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/sympy?hl=en.