-
Notifications
You must be signed in to change notification settings - Fork 655
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
make scipy and matplotlib required dependencies (#1159) #1404
Changes from all commits
4eab969
a56ec2a
6779240
f2f45f8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -172,21 +172,13 @@ | |
from __future__ import print_function, division, absolute_import | ||
from six.moves import range, zip | ||
|
||
import MDAnalysis as mda | ||
import numpy as np | ||
import warnings | ||
import logging | ||
try: | ||
from scipy.stats import gaussian_kde | ||
except ImportError: | ||
gaussian_kde = None | ||
msg = "scipy.stats.gaussian_kde could not be imported. " \ | ||
"Dimensionality reduction ensemble comparisons will not " \ | ||
"be available." | ||
warnings.warn(msg, | ||
category=ImportWarning) | ||
logging.warn(msg) | ||
del msg | ||
|
||
import numpy as np | ||
import scipy.stats | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why not from scipy.stats import gaussian_kde? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Because that is evil. Honestly, function imports at the module level are bad. See comments above. See PEP something or other IIRC... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. PS: Please don't. |
||
|
||
import MDAnalysis as mda | ||
|
||
from ...coordinates.memory import MemoryReader | ||
from .confdistmatrix import get_distance_matrix | ||
|
@@ -460,18 +452,11 @@ def gen_kde_pdfs(embedded_space, ensemble_assignment, nensembles, | |
embedded_ensembles = [] | ||
resamples = [] | ||
|
||
if gaussian_kde is None: | ||
# hack: if we are running with minimal dependencies then scipy was | ||
# not imported and we have to bail here (see scipy import at top) | ||
raise ImportError("For Kernel Density Estimation functionality you" | ||
"need to import scipy") | ||
|
||
for i in range(1, nensembles + 1): | ||
this_embedded = embedded_space.transpose()[ | ||
np.where(np.array(ensemble_assignment) == i)].transpose() | ||
embedded_ensembles.append(this_embedded) | ||
kdes.append(gaussian_kde( | ||
this_embedded)) | ||
kdes.append(scipy.stats.gaussian_kde(this_embedded)) | ||
|
||
# # Set number of samples | ||
# if not nsamples: | ||
|
@@ -623,12 +608,6 @@ def cumulative_gen_kde_pdfs(embedded_space, ensemble_assignment, nensembles, | |
|
||
""" | ||
|
||
if gaussian_kde is None: | ||
# hack: if we are running with minimal dependencies then scipy was | ||
# not imported and we have to bail here (see scipy import at top) | ||
raise ImportError("For Kernel Density Estimation functionality you" | ||
"need to import scipy") | ||
|
||
kdes = [] | ||
embedded_ensembles = [] | ||
resamples = [] | ||
|
@@ -639,8 +618,7 @@ def cumulative_gen_kde_pdfs(embedded_space, ensemble_assignment, nensembles, | |
np.logical_and(ensemble_assignment >= ens_id_min, | ||
ensemble_assignment <= i))].transpose() | ||
embedded_ensembles.append(this_embedded) | ||
kdes.append( | ||
gaussian_kde(this_embedded)) | ||
kdes.append(scipy.stats.gaussian_kde(this_embedded)) | ||
|
||
# Resample according to probability distributions | ||
for this_kde in kdes: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -155,14 +155,16 @@ | |
from __future__ import division, absolute_import | ||
from six.moves import zip | ||
import numpy as np | ||
import scipy.optimize | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. again why not import the one function we need? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See above. Don't. |
||
|
||
import warnings | ||
|
||
from MDAnalysis.lib.log import ProgressMeter | ||
from MDAnalysis.lib.distances import distance_array, calc_angles, calc_bonds | ||
|
||
|
||
class HydrogenBondAutoCorrel(object): | ||
"""Perform a time autocorrelation of the hydrogen bonds in the system. | ||
"""Perform a time autocorrelation of the hydrogen bonds in the system. | ||
|
||
Parameters | ||
---------- | ||
|
@@ -421,8 +423,10 @@ def solve(self, p_guess=None): | |
Initial guess for the leastsq fit, must match the shape of the | ||
expected coefficients | ||
|
||
Continuous defition results are fitted to a double exponential, | ||
intermittent definition are fit to a triple exponential. | ||
|
||
Continuous defition results are fitted to a double exponential with | ||
:func:`scipy.optimize.leastsq`, intermittent definition are fit to a | ||
triple exponential. | ||
|
||
The results of this fitting procedure are saved into the *fit*, | ||
*tau* and *estimate* keywords in the solution dict. | ||
|
@@ -434,14 +438,14 @@ def solve(self, p_guess=None): | |
- *estimate* contains the estimate provided by the fit of the time | ||
autocorrelation function | ||
|
||
In addition, the output of the leastsq function is saved into the | ||
solution dict | ||
In addition, the output of the :func:`~scipy.optimize.leastsq` function | ||
is saved into the solution dict | ||
|
||
- *infodict* | ||
- *mesg* | ||
- *ier* | ||
|
||
""" | ||
from scipy.optimize import leastsq | ||
|
||
if self.solution['results'] is None: | ||
raise ValueError( | ||
|
@@ -498,9 +502,8 @@ def triple(x, A1, A2, tau1, tau2, tau3): | |
if p_guess is None: | ||
p_guess = (0.5, 10 * self.sample_time, self.sample_time) | ||
|
||
p, cov, infodict, mesg, ier = leastsq(err, p_guess, | ||
args=(time, results), | ||
full_output=True) | ||
p, cov, infodict, mesg, ier = scipy.optimize.leastsq( | ||
err, p_guess, args=(time, results), full_output=True) | ||
self.solution['fit'] = p | ||
A1, tau1, tau2 = p | ||
A2 = 1 - A1 | ||
|
@@ -512,9 +515,8 @@ def triple(x, A1, A2, tau1, tau2, tau3): | |
p_guess = (0.33, 0.33, 10 * self.sample_time, | ||
self.sample_time, 0.1 * self.sample_time) | ||
|
||
p, cov, infodict, mesg, ier = leastsq(err, p_guess, | ||
args=(time, results), | ||
full_output=True) | ||
p, cov, infodict, mesg, ier = scipy.optimize.leastsq( | ||
err, p_guess, args=(time, results), full_output=True) | ||
self.solution['fit'] = p | ||
A1, A2, tau1, tau2, tau3 = p | ||
A3 = 1 - A1 - A2 | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -245,7 +245,6 @@ | |
from six.moves import zip, cPickle | ||
import six | ||
|
||
import numpy as np | ||
import glob | ||
import os | ||
import errno | ||
|
@@ -258,6 +257,10 @@ | |
import logging | ||
from itertools import cycle | ||
|
||
import numpy as np | ||
import matplotlib | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. a common idom is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes, but we don't use it a lot and it's clearer to read and to grep for: if I am looking for matplotlib I use |
||
import matplotlib.pyplot as plt | ||
|
||
from MDAnalysis import Universe | ||
from MDAnalysis.exceptions import ApplicationError | ||
from MDAnalysis.lib.util import which, realpath, asiterable | ||
|
@@ -370,8 +373,6 @@ def save(self, filename="hole.pickle"): | |
cPickle.dump(self.profiles, open(filename, "wb"), cPickle.HIGHEST_PROTOCOL) | ||
|
||
def _process_plot_kwargs(self, kwargs): | ||
import matplotlib.colors | ||
|
||
kw = {} | ||
frames = kwargs.pop('frames', None) | ||
if frames is None: | ||
|
@@ -448,9 +449,6 @@ def plot(self, **kwargs): | |
Returns ``ax``. | ||
|
||
""" | ||
|
||
import matplotlib.pyplot as plt | ||
|
||
kw, kwargs = self._process_plot_kwargs(kwargs) | ||
|
||
ax = kwargs.pop('ax', None) | ||
|
@@ -517,8 +515,7 @@ def plot3D(self, **kwargs): | |
Returns ``ax``. | ||
|
||
""" | ||
|
||
import matplotlib.pyplot as plt | ||
# installed with matplotlib; imported here to enable 3D axes | ||
from mpl_toolkits.mplot3d import Axes3D | ||
|
||
kw, kwargs = self._process_plot_kwargs(kwargs) | ||
|
@@ -540,8 +537,7 @@ def plot3D(self, **kwargs): | |
rxncoord = profile.rxncoord | ||
else: | ||
# does not seem to work with masked arrays but with nan hack! | ||
# http://stackoverflow.com/questions/4913306/python-matplotlib-mplot3d-how-do-i-set-a-maximum-value | ||
# -for-the-z-axis | ||
# http://stackoverflow.com/questions/4913306/python-matplotlib-mplot3d-how-do-i-set-a-maximum-value-for-the-z-axis | ||
#radius = np.ma.masked_greater(profile.radius, rmax) | ||
#rxncoord = np.ma.array(profile.rxncoord, mask=radius.mask) | ||
rxncoord = profile.rxncoord | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why not from scipy import sparse?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like the full imports much better if we only use a few things. Much easier to grep. And really not much more to write. Why not be explicit with where you get functions from? And it keeps the name space tidier.
(Sidenote: I particular dislike function imports – I think they are really bad because you have no idea where they come from when reading code. )