Skip to content

MAINT: delay numpy import during build - #3195

Closed
matthew-brett wants to merge 5 commits into
statsmodels:masterfrom
matthew-brett:delay-numpy-setup-import
Closed

matthew-brett wants to merge 5 commits into
statsmodels:masterfrom
matthew-brett:delay-numpy-setup-import

Conversation

@matthew-brett

Copy link
Copy Markdown
Contributor

Delay numpy import until we build the extensions. This allows commands
like egg_info to collect dependencies without crashing out with a
missing numpy.

Therefore, pip should be able to install numpy during the build process.

See: scipy/scipy.org#179 for discussion.

@bashtage

Copy link
Copy Markdown
Member

@josef-pkt Aside from needing a rebase, this seems ready.

@matthew-brett
matthew-brett force-pushed the delay-numpy-setup-import branch from a3b5282 to b5b8ac5 Compare February 11, 2017 12:43
Delay numpy import until we build the extensions.  This allows commands
like ``egg_info`` to collect dependencies without crashing out with a
missing numpy.

Therefore, pip should be able to install numpy during the build process.
Also - import function from os.path.
@josef-pkt

Copy link
Copy Markdown
Member

I thought it failed in several travis version where statespace cython code didn't find anymore some numpy math functions. But I don't see the travis links here anymore.
If it works and is green after rebase, then we can merge

@bashtage

Copy link
Copy Markdown
Member

@matthew-brett
matthew-brett force-pushed the delay-numpy-setup-import branch from b5b8ac5 to 3081b07 Compare February 11, 2017 17:38
@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.05%) to 90.237% when pulling 3081b07 on matthew-brett:delay-numpy-setup-import into c2acb1a on statsmodels:master.

@josef-pkt

Copy link
Copy Markdown
Member

python 3.6 failure is unrelated
but this errors with missing cython symbol
$ export PYTHON=3.3
$ export NUMPY=1.9
$ export SCIPY=0.14
$ export MATPLOTLIB=1.4
$ export PANDAS=0.14

@matthew-brett

Copy link
Copy Markdown
Contributor Author

Yes, I don't understand the logic for this code in setup.py:

try:
    from scipy.linalg import cython_blas
    ext_data.update(statespace_ext_data)
except ImportError:
    for name, data in statespace_ext_data.items():
        path = '.'.join([data["name"].split('.')[0], 'pyx.in'])
        append_cython_exclusion(path, CYTHON_EXCLUSION_FILE)

@josef-pkt

Copy link
Copy Markdown
Member

@ChadFulton knows the details and I never looked at them.

The general problem is that we are doing currently compilation conditional on the scipy version. Some parts of the code take advantage of more recent access to the underlying linear algebra libraries in scipy. But we still support the older scipy versions that don't have these enhancements.

@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.05%) to 90.237% when pulling 129e5f7 on matthew-brett:delay-numpy-setup-import into c2acb1a on statsmodels:master.

@ChadFulton

Copy link
Copy Markdown
Member

@ChadFulton knows the details and I never looked at them.

The general problem is that we are doing currently compilation conditional on the scipy version. Some parts of the code take advantage of more recent access to the underlying linear algebra libraries in scipy. But we still support the older scipy versions that don't have these enhancements.

Exactly. But I'm sure that the setup.py could be designed better, I just don't know how :)

@bashtage

Copy link
Copy Markdown
Member

An alternative would be to move min SciPy to 0.16. Then there is no more conditional compilation.

@matthew-brett

Copy link
Copy Markdown
Contributor Author

Updating the dependency to 0.16 would remove quite a lot of complexity here. For example, I noticed that, if you compile statsmodels with scipy < 0.16, and later install scipy >= 0.16, statsmodels crashes, because it thinks that the extra modules are compiled and tries to import them:

======================================================================
ERROR: Failure: ImportError (cannot import name '_representation')
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/mb312/.local/lib/python3.5/site-packages/nose/failure.py", line 39, in runTest
    raise self.exc_val.with_traceback(self.tb)
  File "/home/mb312/.local/lib/python3.5/site-packages/nose/loader.py", line 418, in loadTestsFromName
    addr.filename, addr.module)
  File "/home/mb312/.local/lib/python3.5/site-packages/nose/importer.py", line 47, in importFromPath
    return self.importFromDir(dir_path, fqname)
  File "/home/mb312/.local/lib/python3.5/site-packages/nose/importer.py", line 94, in importFromDir
    mod = load_module(part_fqname, fh, filename, desc)
  File "/usr/lib/python3.5/imp.py", line 234, in load_module
    return load_source(name, filename, file)
  File "/usr/lib/python3.5/imp.py", line 172, in load_source
    module = _load(spec)
  File "<frozen importlib._bootstrap>", line 693, in _load
  File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 673, in exec_module
  File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
  File "/home/mb312/dev_trees/statsmodels/statsmodels/tsa/statespace/tests/test_collapsed.py", line 17, in <module>
    from statsmodels.tsa.statespace.mlemodel import MLEModel
  File "/home/mb312/dev_trees/statsmodels/statsmodels/tsa/statespace/mlemodel.py", line 14, in <module>
    from .simulation_smoother import SimulationSmoother
  File "/home/mb312/dev_trees/statsmodels/statsmodels/tsa/statespace/simulation_smoother.py", line 10, in <module>
    from .kalman_smoother import KalmanSmoother
  File "/home/mb312/dev_trees/statsmodels/statsmodels/tsa/statespace/kalman_smoother.py", line 13, in <module>
    from statsmodels.tsa.statespace.representation import OptionWrapper
  File "/home/mb312/dev_trees/statsmodels/statsmodels/tsa/statespace/representation.py", line 10, in <module>
    from .tools import (
  File "/home/mb312/dev_trees/statsmodels/statsmodels/tsa/statespace/tools.py", line 207, in <module>
    set_mode(compatibility=None)
  File "/home/mb312/dev_trees/statsmodels/statsmodels/tsa/statespace/tools.py", line 61, in set_mode
    from . import (_representation, _kalman_filter, _kalman_smoother,
ImportError: cannot import name '_representation'

Use correct name for _statespace module, when labeling it as needing the
npymath includes / libraries.
@matthew-brett
matthew-brett force-pushed the delay-numpy-setup-import branch from 129e5f7 to 134e261 Compare February 13, 2017 18:00
@coveralls

coveralls commented Feb 13, 2017 •

Copy link
Copy Markdown

Coverage Status

Coverage remained the same at 90.297% when pulling 134e261 on matthew-brett:delay-numpy-setup-import into e757e8b on statsmodels:master.

@josef-pkt

Copy link
Copy Markdown
Member

An ImportError is not a crash, it's just an exception.

When Chad started to work on this refactoring, scipy 0.16 was still very recent. I don't know if we can drop scipy < 0.16 for a statsmodels 0.9 release.
So far this conditional compilation seems to have worked pretty well, better than I originally expected. But there are still open issues like making binaries/wheels conditional on scipy >= 0.16, but provide source distribution also for older scipy.

(Personally, I still don't feel completely friendly towards pip, even though it is now my default installer. It wasn't friendly to us for many years.)

@matthew-brett
matthew-brett force-pushed the delay-numpy-setup-import branch 2 times, most recently from 9b57774 to 3dd29c6 Compare February 14, 2017 01:09
@coveralls

coveralls commented Feb 14, 2017 •

Copy link
Copy Markdown

Coverage Status

Coverage remained the same at 90.297% when pulling 371fc91 on matthew-brett:delay-numpy-setup-import into e757e8b on statsmodels:master.

@matthew-brett

Copy link
Copy Markdown
Contributor Author

Sure, I was using "crash" in the loose sense of dumping out with an exception, and no helpful error message.

My impression is that this stuff will work OK when compiled on a later scipy, then used with an earlier scipy, is that right?

@ChadFulton

Copy link
Copy Markdown
Member

Updating the dependency to 0.16 would remove quite a lot of complexity here. For example, I noticed that, if you compile statsmodels with scipy < 0.16, and later install scipy >= 0.16, statsmodels crashes, because it thinks that the extra modules are compiled and tries to import them:

That's a good point. I'll make a PR to check that the modules are actually built before using trying to use them, even if scipy >= 0.16 is installed.

@matthew-brett
matthew-brett force-pushed the delay-numpy-setup-import branch from 3dd29c6 to 50c62b7 Compare February 14, 2017 01:33
@coveralls

coveralls commented Feb 14, 2017 •

Copy link
Copy Markdown

Coverage Status

Coverage remained the same at 90.297% when pulling 50c62b7 on matthew-brett:delay-numpy-setup-import into e757e8b on statsmodels:master.

@josef-pkt

Copy link
Copy Markdown
Member

@matthew-brett @bashtage
I just saw the pyproject.toml file of scipy for the first time. (I never heard of it before.)
scipy/scipy#7792
https://www.python.org/dev/peps/pep-0518/

As Kevin mentioned this should solve the main problems, and I like to follow scipy's lead in this kind of things.

So can we just add a pyproject.toml file, and assume that it is enough for the relevant use cases?

@josef-pkt

Copy link
Copy Markdown
Member

(and reading through scipy #7309 reminds me why I'd rather focus on statistics and econometrics)

@matthew-brett

Copy link
Copy Markdown
Contributor Author

Josef - I think that PEP isn't even agreed yet - and when it is, I suppose it will take some time for it to filter through into the build tools, so no, I don't think that's an immediate solution.

Yes, I think this one is mergeable after a rebase.

@matthew-brett

Copy link
Copy Markdown
Contributor Author

Oops - sorry - yes the PEP's agreed - but I stand by the comment about build tools.

@TomAugspurger

Copy link
Copy Markdown
Contributor

The related PEP 517 is still being hashed out.

but I stand by the comment about build tools.

Agreed. This PR would still be useful.

@rgommers rgommers added the build label Aug 27, 2017
@rgommers

Copy link
Copy Markdown
Member

Had a look at this, summary:

  • good idea in principle, and is fairly orthogonal to adding a pyproject.toml.
  • needs a rebase
  • this currently breaks including generated C sources from Cython code in the sdist, that needs fixing

Also +1 for scipy >= 0.16.1, which is 2 years old by now.

@josef-pkt

Copy link
Copy Markdown
Member

ping

this currently breaks including generated C sources from Cython code in the sdist, that needs fixing

bashtage added a commit to bashtage/statsmodels that referenced this pull request Sep 30, 2017
Add pyproject.toml to simplify installation into empty environments

xref statsmodels#3195
@bashtage bashtage mentioned this pull request Sep 30, 2017
tupui pushed a commit to tupui/statsmodels that referenced this pull request Nov 4, 2017
Add pyproject.toml to simplify installation into empty environments

xref statsmodels#3195
bashtage added a commit to bashtage/statsmodels that referenced this pull request Aug 21, 2018
Re-enable xdist
Small adjustment for windows testing

closes statsmodels#4635
closes statsmodels#4638
closes statsmodels#4651
closes statsmodels#4654
closes statsmodels#3207
closes statsmodels#3195
closes statsmodels#3977
bashtage added a commit to bashtage/statsmodels that referenced this pull request Aug 22, 2018
Re-enable xdist
Small adjustment for windows testing

closes statsmodels#4635
closes statsmodels#4638
closes statsmodels#4651
closes statsmodels#4654
closes statsmodels#3207
closes statsmodels#3195
closes statsmodels#3977
bashtage added a commit to bashtage/statsmodels that referenced this pull request Aug 23, 2018
Re-enable xdist
Small adjustment for windows testing

closes statsmodels#4635
closes statsmodels#4638
closes statsmodels#4651
closes statsmodels#4654
closes statsmodels#3207
closes statsmodels#3195
closes statsmodels#3977
@josef-pkt josef-pkt closed this in 6b0302f Aug 27, 2018
@rgommers

Copy link
Copy Markdown
Member

@josef-pkt closed this in 6b0302f 2 hours ago

That commit was unrelated, this is not fixed.

@rgommers rgommers reopened this Aug 27, 2018
@bashtage

bashtage commented Aug 27, 2018 via email

Copy link
Copy Markdown
Member

@bashtage

Copy link
Copy Markdown
Member

#4871

@rgommers

Copy link
Copy Markdown
Member

Thanks @bashtage. gh-4871 looks good to me.

@bashtage

bashtage commented Sep 9, 2018

Copy link
Copy Markdown
Member

Can be closed now -- this was merged after some rebase and modifications this weekend.

@josef-pkt

Copy link
Copy Markdown
Member

superseded by merged #4871

@josef-pkt josef-pkt closed this Sep 9, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants