Skip to content

Commit

Permalink
reducing abstraction in density.py
Browse files Browse the repository at this point in the history
aditya-kamath committed Jan 9, 2021
1 parent 8232192 commit 52fd1f3
Showing 1 changed file with 23 additions and 19 deletions.
42 changes: 23 additions & 19 deletions package/MDAnalysis/analysis/density.py
Original file line number Diff line number Diff line change
@@ -369,6 +369,7 @@ class DensityAnalysis(AnalysisBase):
.. versionchanged:: 2.0.0
:func:`_set_user_grid` is now a method of :class:`DensityAnalysis`.
"""

def __init__(self, atomgroup, delta=1.0,
metadata=None, padding=2.0,
gridcenter=None,
@@ -383,11 +384,17 @@ def __init__(self, atomgroup, delta=1.0,
self._ydim = ydim
self._zdim = zdim

def _prepare(self):
if len(self._atomgroup)==0:
def _checkinput(self):
if len(self._atomgroup) == 0:
msg = ("No atoms in selection over whole trajectory")
warnings.warn(msg)
logger.warning(msg)
return 1
else:
return 0

def _prepare(self):
if _checkinput(self) == 1:
return
coord = self._atomgroup.positions
if self._gridcenter is not None:
@@ -429,11 +436,8 @@ def _prepare(self):
self.density = None

def _single_frame(self):
if len(self._atomgroup)==0:
msg = ("No atoms in selection over whole trajectory")
warnings.warn(msg)
logger.warning(msg)
return
if _checkinput(self) == 1:
return
h, _ = np.histogramdd(self._atomgroup.positions,
bins=self._bins, range=self._arange,
normed=False)
@@ -444,10 +448,7 @@ def _single_frame(self):
self._grid += h

def _conclude(self):
if len(self._atomgroup)==0:
msg = ("No atoms in selection over whole trajectory")
warnings.warn(msg)
logger.warning(msg)
if _checkinput(self) == 1:
return
# average:
self._grid /= float(self.n_frames)
@@ -502,8 +503,8 @@ def _set_user_grid(gridcenter, xdim, ydim, zdim, smin, smax):
raise ValueError("xdim, ydim, and zdim must be numbers") from err

# Set min/max by shifting by half the edge length of each dimension
umin = gridcenter - xyzdim/2
umax = gridcenter + xyzdim/2
umin = gridcenter - xyzdim / 2
umax = gridcenter + xyzdim / 2

# Here we test if coords of selection fall outside of the defined grid
# if this happens, we warn users they may want to resize their grids
@@ -672,7 +673,7 @@ def __init__(self, *args, **kwargs):

parameters = kwargs.pop('parameters', {})
if (len(args) > 0 and isinstance(args[0], str) or
isinstance(kwargs.get('grid', None), str)):
isinstance(kwargs.get('grid', None), str)):
# try to be smart: when reading from a file then it is likely that
# this is a density
parameters.setdefault('isDensity', True)
@@ -710,7 +711,8 @@ def _check_set_unit(self, u):
# all this unit crap should be a class...
try:
for unit_type, value in u.items():
if value is None: # check here, too iffy to use dictionary[None]=None
# check here, too iffy to use dictionary[None]=None
if value is None:
self.units[unit_type] = None
continue
try:
@@ -778,7 +780,8 @@ def convert_length(self, unit='Angstrom'):
"""
if unit == self.units['length']:
return
cvnfact = units.get_conversion_factor('length', self.units['length'], unit)
cvnfact = units.get_conversion_factor(
'length', self.units['length'], unit)
self.edges = [x * cvnfact for x in self.edges]
self.units['length'] = unit
self._update() # needed to recalculate midpoints and origin
@@ -829,8 +832,8 @@ def convert_density(self, unit='Angstrom'):
if unit == self.units['density']:
return
try:
self.grid *= units.get_conversion_factor('density',
self.units['density'], unit)
self.grid *= units.get_conversion_factor(
'density', self.units['density'], unit)
except KeyError:
errmsg = (f"The name of the unit ({unit} supplied) must be one "
f"of:\n{units.conversion_factor['density'].keys()}")
@@ -842,4 +845,5 @@ def __repr__(self):
grid_type = 'density'
else:
grid_type = 'histogram'
return '<Density ' + grid_type + ' with ' + str(self.grid.shape) + ' bins>'
return '<Density ' + grid_type + ' with ' + \
str(self.grid.shape) + ' bins>'

0 comments on commit 52fd1f3

Please sign in to comment.