12

Notice: I checked for duplicate and nothing clearly answers my question. I trust you'll let me know if I missed something!

In an effort to clean up my code, I have been looking for a standard convention for importing SciPy and NumPy in my programs. I know there is no strict guideline and I can do it the way I want, but from time to time, I still find contradictory instructions.

For example, I've read somewhere that NumPy is meant to only implement the array object, while SciPy is there for every other scientific algorithms. So NumPy should be used for array operation and SciPy for everything else... On the other hand, SciPy imports every Numpy functions in its main namespace, such that scipy.array() is the same thing as numpy.array() (see this question), so NumPy should only be used when SciPy is not being used, as they are duplicates...

What is the recommended way to work with SciPy and NumPy? Being a scientist, sqrt(-1) should return a complex number, so I'm inclined to go with SciPy only.

Right now, my code starts with:

import numpy as np
from scipy import *
from matplotlib import pyplot as plt

I use scipy for mathematical operation (such as log10()) and numpy for array creation/operations (such as np.zeros()). Would it be fine to go all the way with SciPy and never import NumPy explicitly? Will a future update remove NumPy's array manipulation from SciPy?

Community
  • 1
  • 1
PhilMacKay
  • 865
  • 2
  • 10
  • 22
  • 3
    I would recommend getting those variants of the functions directly from `numpy.lib.scimath`, where they are defined, instead of from scipy. – Robert Kern Mar 20 '13 at 18:29

3 Answers3

6

I recommend doing something like

import numpy as np
import scipy as sp

instead. It is always dangerous to do from ... import * especially with large modules such as numpy and scipy. The following illustrates why:

>>> any(['foo'])
True
>>> from scipy import *
>>> any(['foo'])

Traceback (most recent call last):
  File "<pyshell#2>", line 1, in <module>
     any(['foo'])
  File "C:\Python27\lib\site-packages\numpy\core\fromnumeric.py", line 1575, in any
    return _wrapit(a, 'any', axis, out)
  File "C:\Python27\lib\site-packages\numpy\core\fromnumeric.py", line 37, in _wrapit
    result = getattr(asarray(obj),method)(*args, **kwds)
TypeError: cannot perform reduce with flexible type

What happens here? The standard python builtin function any is replaced by scipy.any which has different behavior. That might break any code that uses the standard any.

Johan Råde
  • 20,480
  • 21
  • 73
  • 110
  • I get the danger of `from ... import *`, but importing both scipy and numpy is redundant, as numpy is included in scipy, and scipy's better for scientific computing. – PhilMacKay Mar 20 '13 at 17:40
  • 1
    Then just import `scipy`, but don't do `from scipy import *`. – Johan Råde Mar 20 '13 at 17:42
  • I opted for your answer, as there's pretty much nothing else to add. In my case, since I do scientific computing all the time, I tested using only `import scipy as sp`, and use `sp.stuff` for everything. In more general computing environment, where scipy is not needed, I suppose numpy would be the way to go. I can't think of a situation where both woul dbe needed at the same time. – PhilMacKay Apr 05 '13 at 19:58
  • the problem with `import scipy as sp` is many sub modules won't be available. For instance, `sp.sparse` isn't available. Any idea how to import them all? – Royi Jun 14 '20 at 11:40
1

This post has some good information about the two modules (Relationship between scipy and numpy). It seems that Numpy's functionality is meant to be completely included within Scipy, although there are a few exceptions (see post). I would say it is safe to simply use Scipy for all of your needs since most important things like mathematical functions, arrays, and other things are included within Scipy.

Community
  • 1
  • 1
astromax
  • 6,001
  • 10
  • 36
  • 47
0

What about making classes and use just what you will need, fx: class one:

import cv2
from SIGBWindows import SIGBWindows
from SIGBAssg import *

class two:

import cv2
import numpy as np

from pylab import *
from scipy.cluster.vq import *
from scipy.misc import imresize

class three:

import cv2
import numpy as np

and Finally where we call the object:

import cv2
from SIGBWindows import SIGBWindows
from SIGBAssg import *

windows = SIGBWindows(mode="video")
windows.openVideo("somevideo.avi")
kmeans(windows)

I don't know if it is what you are looking for, but this approach it makes the code really clean and easy to add more features to it.

Jorge Y. C. Rodriguez
  • 3,394
  • 5
  • 38
  • 61
  • 1
    umm, what question are you answering? – MattDMo Mar 20 '13 at 16:52
  • Hmmm... That's not quite what I'm looking for. My main goal would be to keep the code as easy to read and simple as possible, since I want somebody else (or myself in a year) to be able to understand my algorithm easily. – PhilMacKay Mar 20 '13 at 17:07
  • yeah i follow you in that, but since both kind of array are needed for images processing, I think that doing it this way is the easiest way to make the code more easy to read. – Jorge Y. C. Rodriguez Mar 20 '13 at 17:38