Skip to content

Commit d96245e

Browse files
committed
changes requested by IAlibay
Changed the if/if/if for reading masses and charges to if/elif/else. Will now raise an AttributeError if invalid grouping is chosen. 'residues', 'segments' and 'fragments' now use the same logic (elem.atoms.total_mass()).
1 parent 389eb1c commit d96245e

File tree

1 file changed

+6
-19
lines changed

1 file changed

+6
-19
lines changed

package/MDAnalysis/analysis/lineardensity.py

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
"""
2424
Linear Density --- :mod:`MDAnalysis.analysis.lineardensity`
2525
===========================================================
26-
2726
A tool to compute mass and charge density profiles along the three
2827
cartesian axes [xyz] of the simulation cell. Works only for orthorombic,
2928
fixed volume cells (thus for simulations in canonical NVT ensemble).
@@ -37,7 +36,6 @@
3736

3837
class LinearDensity(AnalysisBase):
3938
"""Linear density profile
40-
4139
Parameters
4240
----------
4341
select : AtomGroup
@@ -51,7 +49,6 @@ class LinearDensity(AnalysisBase):
5149
profile (smaller --> higher resolution)
5250
verbose : bool, optional
5351
Show detailed progress of the calculation if set to ``True``
54-
5552
Attributes
5653
----------
5754
results.x.dim : int
@@ -66,37 +63,28 @@ class LinearDensity(AnalysisBase):
6663
standard deviation of the charge density in [xyz] direction
6764
results.x.slice_volume : float
6865
volume of bin in [xyz] direction
69-
7066
Example
7167
-------
7268
First create a ``LinearDensity`` object by supplying a selection,
7369
then use the :meth:`run` method. Finally access the results
7470
stored in results, i.e. the mass density in the x direction.
75-
7671
.. code-block:: python
77-
7872
ldens = LinearDensity(selection)
7973
ldens.run()
8074
print(ldens.results.x.pos)
81-
82-
8375
.. versionadded:: 0.14.0
84-
8576
.. versionchanged:: 1.0.0
8677
Support for the ``start``, ``stop``, and ``step`` keywords has been
8778
removed. These should instead be passed to :meth:`LinearDensity.run`.
8879
The ``save()`` method was also removed, you can use ``np.savetxt()`` or
8980
``np.save()`` on the :attr:`LinearDensity.results` dictionary contents
9081
instead.
91-
9282
.. versionchanged:: 1.0.0
9383
Changed `selection` keyword to `select`
94-
9584
.. versionchanged:: 2.0.0
9685
Results are now instances of
9786
:class:`~MDAnalysis.core.analysis.Results` allowing access
9887
via key and attribute.
99-
10088
.. versionchanged:: 2.2.0
10189
Fixed a bug that caused LinearDensity to fail if grouping="residues"
10290
or grouping="segments" were set.
@@ -154,14 +142,14 @@ def _prepare(self):
154142
self.masses = self._ags[0].masses
155143
self.charges = self._ags[0].charges
156144

157-
if self.grouping in ["residues", "segments"]:
145+
elif self.grouping in ["residues", "segments", "fragments"]:
158146
self.masses = np.array([elem.atoms.total_mass() for elem in group])
159147
self.charges = np.array(
160148
[elem.atoms.total_charge() for elem in group])
161149

162-
if self.grouping == "fragments":
163-
self.masses = np.array([elem.total_mass() for elem in group])
164-
self.charges = np.array([elem.total_charge() for elem in group])
150+
else:
151+
raise AttributeError(
152+
f"{self.grouping} is not a valid value for grouping.")
165153

166154
self.totalmass = np.sum(self.masses)
167155

@@ -174,7 +162,8 @@ def _single_frame(self):
174162
positions = self._ags[0].positions # faster for atoms
175163
else:
176164
# Centre of geometry for residues, segments, fragments
177-
positions = np.array([elem.atoms.centroid() for elem in self.group])
165+
positions = np.array(
166+
[elem.atoms.centroid() for elem in self.group])
178167

179168
for dim in ['x', 'y', 'z']:
180169
idx = self.results[dim]['dim']
@@ -193,7 +182,6 @@ def _single_frame(self):
193182
key = 'char'
194183
key_std = 'char_std'
195184
# histogram for positions weighted on charges
196-
197185
hist, _ = np.histogram(positions[:, idx],
198186
weights=self.charges,
199187
bins=self.nbins,
@@ -202,7 +190,6 @@ def _single_frame(self):
202190
self.results[dim][key] += hist
203191
self.results[dim][key_std] += np.square(hist)
204192

205-
206193
def _conclude(self):
207194
k = 6.022e-1 # divide by avodagro and convert from A3 to cm3
208195

0 commit comments

Comments
 (0)