-2

In a laboratory class at Uni, one version of my code failed, while another passed, and I was hoping someone could explain why these are any different?

The version that failed was:

import scipy as sc

def trapez(f, a, b, n)
    h = (b - a) / n
    c = list(range(1, n))
    return (h / 2) * (f(a) + f(b) + 2 * sum(f(a + i * h) for i in c))

def finderror(n)
    def f(x):
        return x ** 2
    l = sc.integrate.quad(f, -1, 2)
    return l[0] - trapez(f, -1, 2, n)

The version that passed was:

import scipy.integrate as sc

def trapez(f, a, b, n):
    h = (b - a) / n
    c = list(range(1, n))
    return (h / 2) * (f(a) + f(b) + 2 * sum(f(a + i * h) for i in c))

def finderror(n):
    def f(x):
        return x ** 2
    l = sc.quad(f, -1, 2)
    return l[0] - trapez(f, -1, 2, n)

Both of these returned the exact same answers, hence my confusion at one 'failing'.

Thank you in advance for any advice! (I have already completed this, so this isn't me trying to get answers)

  • 2
    What do you mean by "failed" or "passed"? – user2357112 Dec 12 '16 at 19:12
  • Post the error you got (with stack trace) – Mad Physicist Dec 12 '16 at 19:13
  • When I say failed or passed, it's an online email submission, that returns an email passing or failing each function. The Error that the email listed was: E AttributeError: module 'scipy' has no attribute 'integrate' –  Dec 12 '16 at 19:16
  • @JosephWard. `integrate` is a sub-module, not an attribute. – ekhumoro Dec 12 '16 at 19:30
  • @ekhumoro: Submodules of a package are stored as attributes of the package object at runtime. – user2357112 Dec 12 '16 at 19:31
  • Quoting from the [Introduction to the SciPy Tutorial](https://docs.scipy.org/doc/scipy/reference/tutorial/general.html#scipy-organization): "Scipy sub-packages need to be imported separately". Usually the only reason to import just `scipy` is to check the version stored as `scipy.__version__`. All the subpackages require explicit imports. (I'd make this an answer, but I'm sure the question is a duplicate, even though I didn't find an obvious candidate for the dupe in a brief search.) – Warren Weckesser Dec 12 '16 at 20:58
  • @user2357112. Nope. For example, the `encodings` package in the standard library contains dozens of sub-modules - but `import encodings` does not automatically import them all, so they are not available as attributes. Of course, sub-modules can be explicitly imported in the `__init__.py` of a package - which is why the *aliases.py* sub-module **is** automatically available as `encodings.aliases`. – ekhumoro Dec 12 '16 at 22:27
  • @ekhumoro: Let me rephrase that. *Once loaded*, submodules of a package are stored as attributes of the package object. – user2357112 Dec 13 '16 at 00:48
  • @user2357112. There doesn't seem to be a term to distinguish between attributes/variables that are defined by the module itself (roughly equivalent to `__all__`), and those that just end up in its namespace by other means. Anyway, that's what I was getting at in my original comment. – ekhumoro Dec 13 '16 at 01:11
  • Possible duplicate of [AttributeError: 'module' object (scipy) has no attribute 'misc'](http://stackoverflow.com/questions/13581593/attributeerror-module-object-scipy-has-no-attribute-misc) – ev-br Dec 13 '16 at 15:56

1 Answers1

2

If you just import scipy as sc, that doesn't load the scipy.integrate submodule. You need to import scipy.integrate explicitly.

If the first snippet seemed to work when you tried it, that's because you didn't test it in isolation. You tried it in an environment where scipy.integrate had already been loaded by other imports.

user2357112
  • 260,549
  • 28
  • 431
  • 505
  • This was suggested by someone on my course. I tried it later and it did work when I just imported scipy as sc, then used sc.integrate.quad(function and arguments –  Dec 14 '16 at 14:37