Skip to content

Commit

Permalink
Merge pull request #259 from choderalab/np112
Browse files Browse the repository at this point in the history
Fix changes introduced from NumPy 1.12
  • Loading branch information
Lnaden committed Feb 20, 2017
2 parents dc0fd3f + a9da927 commit f8c61de
Show file tree
Hide file tree
Showing 13 changed files with 70 additions and 53 deletions.
2 changes: 1 addition & 1 deletion devtools/conda-recipe/meta.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package:
name: pymbar-dev
version: !!str 3.0.0.dev0
version: !!str 3.0.1.dev0

source:
path: ../../
Expand Down
10 changes: 5 additions & 5 deletions pymbar/__init__.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
##############################################################################
# pymbar: A Python Library for MBAR
#
# Copyright 2010-2014 University of Virginia, Memorial Sloan-Kettering Cancer Center
# Copyright 2010-2017 University of Virginia, Memorial Sloan-Kettering Cancer Center
#
# Authors: Michael Shirts, John Chodera
# Contributors: Kyle Beauchamp
# Contributors: Kyle Beauchamp, Levi Naden
#
# pymbar is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as
Expand All @@ -27,9 +27,9 @@
"""

__author__ = "Michael R. Shirts and John D. Chodera"
__license__ = "LGPL"
__maintainer__ = "Michael R. Shirts and John D. Chodera"
__email__ = "michael.shirts@virginia.edu,choderaj@mskcc.org"
__license__ = "LGPL 2.1"
__maintainer__ = "Levi N. Naden, Michael R. Shirts and John D. Chodera"
__email__ = "levi.naden@choderalab.org,michael.shirts@colorado.edu,john.chodera@choderalab.org"

from pymbar import timeseries, testsystems, confidenceintervals, version
from pymbar.mbar import MBAR
Expand Down
2 changes: 1 addition & 1 deletion pymbar/bar.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
##############################################################################
# pymbar: A Python Library for MBAR
#
# Copyright 2010-2014 University of Virginia, Memorial Sloan-Kettering Cancer Center
# Copyright 2010-2017 University of Virginia, Memorial Sloan-Kettering Cancer Center
#
# Authors: Michael Shirts, John Chodera
# Contributors: Kyle Beauchamp
Expand Down
2 changes: 1 addition & 1 deletion pymbar/confidenceintervals.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
##############################################################################
# pymbar: A Python Library for MBAR
#
# Copyright 2010-2014 University of Virginia, Memorial Sloan-Kettering Cancer Center
# Copyright 2010-2017 University of Virginia, Memorial Sloan-Kettering Cancer Center
#
# Authors: Michael Shirts, John Chodera
# Contributors: Kyle Beauchamp
Expand Down
2 changes: 1 addition & 1 deletion pymbar/exp.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
##############################################################################
# pymbar: A Python Library for MBAR
#
# Copyright 2010-2014 University of Virginia, Memorial Sloan-Kettering Cancer Center
# Copyright 2010-2017 University of Virginia, Memorial Sloan-Kettering Cancer Center
#
# Authors: Michael Shirts, John Chodera
# Contributors: Kyle Beauchamp
Expand Down
18 changes: 9 additions & 9 deletions pymbar/mbar.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
##############################################################################
# pymbar: A Python Library for MBAR
#
# Copyright 2010-2014 University of Virginia, Memorial Sloan-Kettering Cancer Center
# Copyright 2010-2017 University of Virginia, Memorial Sloan-Kettering Cancer Center
# Portions of this software are Copyright (c) 2006-2007 The Regents of the University of California. All Rights Reserved.
# Portions of this software are Copyright (c) 2007-2008 Stanford University and Columbia University.
#
# Authors: Michael Shirts, John Chodera
# Contributors: Kyle Beauchamp
# Contributors: Kyle Beauchamp, Levi Naden
#
# pymbar is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as
Expand Down Expand Up @@ -47,9 +47,9 @@
DEFAULT_SOLVER_PROTOCOL = mbar_solvers.DEFAULT_SOLVER_PROTOCOL
DEFAULT_SUBSAMPLING_PROTOCOL = mbar_solvers.DEFAULT_SUBSAMPLING_PROTOCOL

#=========================================================================
# =========================================================================
# MBAR class definition
#=========================================================================
# =========================================================================


class MBAR:
Expand Down Expand Up @@ -171,7 +171,7 @@ def __init__(self, u_kn, N_k, maximum_iterations=10000, relative_tolerance=1.0e-
if verbose:
print("K (total states) = %d, total samples = %d" % (K, N))

if np.sum(N_k) != N:
if np.sum(self.N_k) != N:
raise ParameterError(
'The sum of all N_k must equal the total number of samples (length of second dimension of u_kn.')

Expand All @@ -187,8 +187,8 @@ def __init__(self, u_kn, N_k, maximum_iterations=10000, relative_tolerance=1.0e-
self.x_kindices = np.arange(N, dtype=np.int64)
Nsum = 0
for k in range(K):
self.x_kindices[Nsum:Nsum+N_k[k]] = k
Nsum += N_k[k]
self.x_kindices[Nsum:Nsum+self.N_k[k]] = k
Nsum += self.N_k[k]

# verbosity level -- if True, will print extra debug information
self.verbose = verbose
Expand Down Expand Up @@ -219,7 +219,7 @@ def __init__(self, u_kn, N_k, maximum_iterations=10000, relative_tolerance=1.0e-
# Print number of samples from each state.
if self.verbose:
print("N_k = ")
print(N_k)
print(self.N_k)

# Determine list of k indices for which N_k != 0
self.states_with_samples = np.where(self.N_k != 0)[0]
Expand Down Expand Up @@ -1258,7 +1258,7 @@ def computePMF(self, u_n, bin_n, nbins, uncertainties='from-lowest', pmf_referen
>>> import numpy as np
>>> N_tot = N_k.sum()
>>> x_n_sorted = np.sort(x_n) # unroll to n-indices
>>> bins = np.append(x_n_sorted[0::(N_tot/nbins)], x_n_sorted.max()+0.1)
>>> bins = np.append(x_n_sorted[0::int(N_tot/nbins)], x_n_sorted.max()+0.1)
>>> bin_widths = bins[1:] - bins[0:-1]
>>> bin_n = np.zeros(x_n.shape, np.int64)
>>> bin_n = np.digitize(x_n, bins) - 1
Expand Down
2 changes: 1 addition & 1 deletion pymbar/old_mbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -1426,7 +1426,7 @@ def computePMF(self, u_n, bin_n, nbins, uncertainties='from-lowest', pmf_referen
>>> import numpy as np
>>> N_tot = N_k.sum()
>>> x_n_sorted = np.sort(x_n) # unroll to n-indices
>>> bins = np.append(x_n_sorted[0::(N_tot/nbins)], x_n_sorted.max()+0.1)
>>> bins = np.append(x_n_sorted[0::int(N_tot/nbins)], x_n_sorted.max()+0.1)
>>> bin_widths = bins[1:] - bins[0:-1]
>>> bin_n = np.zeros(x_n.shape, np.int32)
>>> bin_n = np.digitize(x_n, bins) - 1
Expand Down
12 changes: 6 additions & 6 deletions pymbar/testsystems/exponential_distributions.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,12 @@ def analytical_observable(self, observable = 'position'):
return np.ones(len(self.rates))

def analytical_entropies(self):
return self.analytical_observable(observable = 'potential energy') - self.analytical_free_energies()
return self.analytical_observable(observable='potential energy') - self.analytical_free_energies()

def analytical_x_squared(self):
return self.analytical_variances() + self.analytical_means() ** 2.

def sample(self, N_k=[10, 20, 30, 40, 50], mode='u_kln'):
def sample(self, N_k=(10, 20, 30, 40, 50), mode='u_kln'):
"""Draw samples from the distribution.
Parameters
Expand Down Expand Up @@ -123,7 +123,7 @@ def sample(self, N_k=[10, 20, 30, 40, 50], mode='u_kln'):
N_k[k] is the number of samples generated from state k
"""
N_k = np.array(N_k, np.float64)
N_k = np.array(N_k, np.int32)
if len(N_k) != self.n_states:
raise Exception("N_k has %d states while self.n_states has %d states." % (len(N_k), self.n_states))

Expand All @@ -148,12 +148,12 @@ def sample(self, N_k=[10, 20, 30, 40, 50], mode='u_kln'):
u_kn[l, index:(index + N)] = u
index += N

if (mode == 'u_kn'):
if mode == 'u_kn':
return x_n, u_kn, N_k, s_n
elif (mode == 'u_kln'):
elif mode == 'u_kln':
return x_kn, u_kln, N_k
else:
raise Exception("Unknown mode '%s'" % mode)
raise Exception("Unknown mode '{}'".format(mode))

return

Expand Down
6 changes: 3 additions & 3 deletions pymbar/testsystems/harmonic_oscillators.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class HarmonicOscillatorsTestCase(object):
"""

def __init__(self, O_k=[0, 1, 2, 3, 4], K_k=[1, 2, 4, 8, 16], beta=1.0):
def __init__(self, O_k=(0, 1, 2, 3, 4), K_k=(1, 2, 4, 8, 16), beta=1.0):
"""Generate test case with exponential distributions.
Parameters
Expand Down Expand Up @@ -66,7 +66,7 @@ def __init__(self, O_k=[0, 1, 2, 3, 4], K_k=[1, 2, 4, 8, 16], beta=1.0):
self.K_k = np.array(K_k, np.float64)

if len(self.K_k) != self.n_states:
raise ValueError('Lengths of K_k=%d and O_k=%d should be equal' % (len(self.O_k),len(self.K_k)))
raise ValueError('Lengths of K_k={} and O_k={} should be equal'.format(len(self.O_k), len(self.K_k)))

def analytical_means(self):
return self.O_k
Expand All @@ -77,7 +77,7 @@ def analytical_variances(self):
def analytical_standard_deviations(self):
return (self.beta * self.K_k) ** -0.5

def analytical_observable(self, observable = 'position'):
def analytical_observable(self, observable='position'):

if observable == 'position':
return self.analytical_means()
Expand Down
49 changes: 31 additions & 18 deletions pymbar/timeseries.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
##############################################################################
# pymbar: A Python Library for MBAR
#
# Copyright 2010-2014 University of Virginia, Memorial Sloan-Kettering Cancer Center
# Copyright 2010-2017 University of Virginia, Memorial Sloan-Kettering Cancer Center
# Portions of this software are Copyright (c) 2006-2007 The Regents of the University of California. All Rights Reserved.
# Portions of this software are Copyright (c) 2007-2008 Stanford University and Columbia University.
#
# Authors: Michael Shirts, John Chodera
# Contributors: Kyle Beauchamp
# Contributors: Kyle Beauchamp, Levi Naden
#
# pymbar is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as
Expand Down Expand Up @@ -50,29 +50,29 @@


__authors__ = "Michael R. Shirts and John D. Chodera."
__license__ = "GPL 2.0"
__license__ = "LGPL 2.1"

#=============================================================================================

# =============================================================================================
# IMPORTS
#=============================================================================================
# =============================================================================================
import math
import warnings
import numpy as np
import numpy.linalg
from pymbar.utils import ParameterError

#=============================================================================================
# =============================================================================================
# Issue warning on import.
#=============================================================================================
# =============================================================================================

LongWarning = "Warning on use of the timeseries module: If the inherent timescales of the system are long compared to those being analyzed, this statistical inefficiency may be an underestimate. The estimate presumes the use of many statistically independent samples. Tests should be performed to assess whether this condition is satisfied. Be cautious in the interpretation of the data."

#sys.stderr.write(LongWarning + '\n')

#=============================================================================================
# =============================================================================================
# METHODS
#=============================================================================================

#=============================================================================================
# =============================================================================================


def statisticalInefficiency(A_n, B_n=None, fast=False, mintime=3, fft=False):
Expand Down Expand Up @@ -793,7 +793,7 @@ def detectEquilibration(A_t, fast=True, nskip=1):
return (t, g, Neff_max)


def statisticalInefficiency_fft(A_n, mintime=3, memsafe=True):
def statisticalInefficiency_fft(A_n, mintime=3, memsafe=None):
"""Compute the (cross) statistical inefficiency of (two) timeseries.
Parameters
Expand All @@ -805,7 +805,7 @@ def statisticalInefficiency_fft(A_n, mintime=3, memsafe=True):
The algorithm terminates after computing the correlation time out to mintime when the
correlation function first goes negative. Note that this time may need to be increased
if there is a strong initial negative peak in the correlation function.
memsafe: bool, optional, default=True
memsafe: bool, optional, default=None (in depreciation)
If this function is used several times on arrays of comparable size then one might benefit
from setting this option to False. If set to True then clear np.fft cache to avoid a fast
increase in memory consumption when this function is called on many arrays of different sizes.
Expand Down Expand Up @@ -843,12 +843,25 @@ def statisticalInefficiency_fft(A_n, mintime=3, memsafe=True):
C_t = sm.tsa.stattools.acf(A_n, fft=True, unbiased=True, nlags=N)
t_grid = np.arange(N).astype('float')
g_t = 2.0 * C_t * (1.0 - t_grid / float(N))


"""
LNN Note:
Numpy 1.12 fft.fftpack swiched from using a Python dict for the cache to a LRU cache which auto prunes.
This makes the .clear() method undefined since the LRU does not have such a method.
Since the LRU *should* remove the problems which this code resolved (GH issue #168), I am removing this code from
execution. However, there may still be a problem with the LRU cache so I am leaving the code in case we need to
further test it
"""
#make function memory safe by clearing np.fft cache
#this assumes that statsmodels uses np.fft
if memsafe:
np.fft.fftpack._fft_cache.clear()
np.fft.fftpack._real_fft_cache.clear()
#if memsafe:
# np.fft.fftpack._fft_cache.clear()
# np.fft.fftpack._real_fft_cache.clear()
if memsafe is not None:
warnings.warn("NumPy's FFT pack now uses an LRU cache to fix the very problem that the memsafe keyword "
"was protecting. This argument no longer changes the code and will be removed in a future "
"version.", FutureWarning)

try:
ind = np.where((C_t <= 0) & (t_grid > mintime))[0][0]
Expand All @@ -858,7 +871,7 @@ def statisticalInefficiency_fft(A_n, mintime=3, memsafe=True):
g = 1.0 + g_t[1:ind].sum()
g = max(1.0, g)

return g #, g_t, C_t
return g # , g_t, C_t


def detectEquilibration_binary_search(A_t, bs_nodes=10):
Expand Down
12 changes: 8 additions & 4 deletions pymbar/utils.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
##############################################################################
# pymbar: A Python Library for MBAR
#
# Copyright 2010-2014 University of Virginia, Memorial Sloan-Kettering Cancer Center
# Copyright 2010-2017 University of Virginia, Memorial Sloan-Kettering Cancer Center
#
# Authors: Michael Shirts, John Chodera
# Contributors: Kyle Beauchamp
# Contributors: Kyle Beauchamp, Levi Naden
#
# pymbar is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as
Expand Down Expand Up @@ -43,6 +43,7 @@
class TypeCastPerformanceWarning(RuntimeWarning):
pass


def kln_to_kn(kln, N_k = None, cleanup = False):

""" Convert KxKxN_max array to KxN max array
Expand Down Expand Up @@ -84,6 +85,7 @@ def kln_to_kn(kln, N_k = None, cleanup = False):

return kn


def kn_to_n(kn, N_k = None, cleanup = False):

""" Convert KxN_max array to N array
Expand Down Expand Up @@ -123,6 +125,7 @@ def kn_to_n(kn, N_k = None, cleanup = False):
del(kn) # very big, let's explicitly delete
return n


def ensure_type(val, dtype, ndim, name, length=None, can_be_none=False, shape=None,
warn_on_cast=True, add_newaxis_on_deficient_ndim=False):
"""Typecheck the size, shape and dtype of a numpy array, with optional
Expand Down Expand Up @@ -326,6 +329,7 @@ def logsumexp(a, axis=None, b=None, use_numexpr=True):

return out


def check_w_normalized(W, N_k, tolerance = 1.0e-4):
"""Check the weight matrix W is properly normalized. The sum over N should be 1, and the sum over k by N_k should aslo be 1
Expand Down Expand Up @@ -367,9 +371,9 @@ def check_w_normalized(W, N_k, tolerance = 1.0e-4):

return

#=============================================================================================
# ============================================================================================
# Exception classes
#=============================================================================================
# =============================================================================================


class ParameterError(Exception):
Expand Down
4 changes: 2 additions & 2 deletions pymbar/utils_for_testing.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
##############################################################################
# pymbar: A Python Library for MBAR
#
# Copyright 2010-2014 University of Virginia, Memorial Sloan-Kettering Cancer Center
# Copyright 2010-2017 University of Virginia, Memorial Sloan-Kettering Cancer Center
#
# Authors: Michael Shirts, John Chodera
# Contributors: Kyle Beauchamp
# Contributors: Kyle Beauchamp, Levi Naden
#
# pymbar is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import six

##########################
VERSION = "3.0.0"
VERSION = "3.0.1"
ISRELEASED = False
__version__ = VERSION
##########################
Expand Down

0 comments on commit f8c61de

Please sign in to comment.