Skip to content
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

Add simple atomic distance analysis (#3654) #4105

Merged

Conversation

xhgchen
Copy link
Member

@xhgchen xhgchen commented Mar 30, 2023

  • Resolves add simple atomic distance analysis to analysis.distances #3654
  • Add class AtomicDistances to MDAnalysis.analysis.distances to calculate the distances
    |ag1[i] - ag2[i]| for all i from 0 to n_atoms - 1 for each frame over the trajectory
  • Allow periodic boundary conditions to be considered or ignored in class AtomicDistances by setting kwarg pbc to True or False
  • Add unit tests for class AtomicDistances to test_distances.py

The class itself is inspired by this comment in #3310 mentioned in #3654. How would I go about crediting them if this PR gets approved? I changed the list to a NumPy array to improve performance and added the pbc kwarg, as outlined above.

PR Checklist

  • Tests?
  • Docs?
  • CHANGELOG updated?
  • Issue raised/referenced?

📚 Documentation preview 📚: https://readthedocs-preview--4105.org.readthedocs.build/en/4105/

@github-actions
Copy link

github-actions bot commented Mar 30, 2023

Linter Bot Results:

Hi @xhgchen! Thanks for making this PR. We linted your code and found the following:

Some issues were found with the formatting of your code.

Code Location Outcome
main package ⚠️ Possible failure
testsuite ⚠️ Possible failure

Please have a look at the darker-main-code and darker-test-code steps here for more details: https://github.com/MDAnalysis/mdanalysis/actions/runs/4669873354/jobs/8268901006


Please note: The black linter is purely informational, you can safely ignore these outcomes if there are no flake8 failures!

@codecov
Copy link

codecov bot commented Mar 30, 2023

Codecov Report

Patch coverage: 100.00% and no project coverage change.

Comparison is base (210e05c) 93.59% compared to head (0cdabcf) 93.59%.

Additional details and impacted files
@@           Coverage Diff            @@
##           develop    #4105   +/-   ##
========================================
  Coverage    93.59%   93.59%           
========================================
  Files          192      193    +1     
  Lines        25134    25156   +22     
  Branches      4056     4058    +2     
========================================
+ Hits         23523    23546   +23     
  Misses        1092     1092           
+ Partials       519      518    -1     
Impacted Files Coverage Δ
package/MDAnalysis/analysis/atomicdistances.py 100.00% <100.00%> (ø)

... and 1 file with indirect coverage changes

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report in Codecov by Sentry.
📢 Do you have feedback about the report comment? Let us know in this issue.

@xhgchen xhgchen force-pushed the simple-atomic-distance-analysis branch 2 times, most recently from 4d87b70 to e575a99 Compare March 30, 2023 00:28
Copy link
Member

@richardjgowers richardjgowers left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a good start!

Comment on lines 220 to 221
conditions (PBCs). The unitcell dimensions of the system must be
orthogonal or triclinic for the calculations with PBCs to work.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need to mention what the unitcell must be here as we've previously enforced this

@@ -55,6 +55,8 @@

import warnings
import logging
from .base import AnalysisBase
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be cleaner to create a new file for this analysis class. It feels a little weird to import base into distances. Once you get circular imports things get confusing fast.

self.results = np.zeros((self.n_frames, self._ag1.atoms.n_atoms))

def _single_frame(self):
if (self._pbc):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a fun hack is to set a variable (box) to either .dimensions or None depending on self._pbc, then the call to calc_bonds can remain the same, and it's clearer what's happening (imho)

e.g.

box = self._ag1.dimensions if self._pbc else None

self.results = calc_bonds(self._ag1.positions, self._ag2.positions, box=box)

Comment on lines 229 to 232
n_frames : int
Number of frames included in the analysis.
n_atoms : int
Number of atoms in each atom group.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see where these variables are set?

Copy link
Member Author

@xhgchen xhgchen Mar 31, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for all the feedback! I learned a lot working through it, and I have pushed some more commits.

I added those variables because other classes seemed to have them (in the docstring) and I used those as reference.

results refers to self.results in def _prepare(self) and def _single_frame(self)
n_frames is used to make the NumPy array in def _prepare(self)
n_atoms is used to check that ag1 and ag2 have the same number of atoms and in def _prepare(self) to make the NumPy array

Would it be better to remove some/all of them (from the docstring)?

Comment on lines 243 to 244
def ad_u():
return MDAnalysis.Universe(GRO, XTC)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we use a mda.Universe.empty() here to avoid having to read files for a test?

For its positions, you could either use a seeded random number, or just something like np.arange(natoms * 3).reshape(natoms, -1) to generate arbitrary coordinates

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So in order to do this, I had to change the dist() tests to use calc_bonds() instead because the Universe does not have resids


@staticmethod
@pytest.fixture()
def expected_scipy(ad_ag1, ad_ag2):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure we need a check against scipy, the check against dist is fine

Comment on lines 302 to 313
# non-pbc x, y, z distances
dist = np.abs(ad_ag1.positions - ad_ag2.positions)

# box size (lx, ly, lz)
box = ad_ag1.dimensions[:3]

# apply minimum image convention i.e. take box - dist
# when dist > box / 2
dist = np.where(dist > box / 2, box - dist, dist)

# desired dist = sqrt((x2 - x1)^2 + (y2 - y1)^2 + (z2 - z1)^2)
expected[i] = np.sqrt(np.square(dist).sum(axis=1))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

again just use dist here. we're not testing that our distance calculations work here (we do that elsewhere). We're testing that this class is correctly applying those functions.

@richardjgowers richardjgowers self-assigned this Mar 30, 2023
@xhgchen xhgchen force-pushed the simple-atomic-distance-analysis branch from e575a99 to c3298f3 Compare March 31, 2023 23:49
@xhgchen
Copy link
Member Author

xhgchen commented Apr 1, 2023

Now that all the checks have passed, @richardjgowers could you take a look at the new files when you get the chance? I would appreciate input on the file name as well. I was torn between atomic_distance_analysis.py and atomicdistances.py but I went with the latter because the first option seemed like a mouthful.

@xhgchen xhgchen requested a review from richardjgowers April 1, 2023 00:53
@xhgchen
Copy link
Member Author

xhgchen commented Apr 4, 2023

Just updating the docs so they work properly :)

This is the first time I have ever written docs, but it was fun.

Edit: The 2 failing checks both seem to be from an issue uploading to Codecov. I would expect them to work with a re-run. Here is a direct link to the preview docs page for the new module: https://mdanalysis--4105.org.readthedocs.build/en/4105/documentation_pages/analysis/atomicdistances.html

Edit 2: Very minor wording fix. Hopefully the checks will run correctly this time.

xhgchen added 10 commits April 11, 2023 10:35
* Resolves MDAnalysis#3654
* Add class `AtomicDistances` to `MDAnalysis.analysis.distances` to
calculate the distances `|ag1[i] - ag2[i]|` for all `i` from
`0` to `n_atoms - 1` for each frame over the trajectory
* Allow periodic boundary conditions to be considered or ignored in
class `AtomicDistances` by setting kwarg `pbc` to `True` or `False`
* Add unit tests for class `AtomicDistances` to `test_distances.py`
* Add `atomicdistances.py` for class `AtomicDistances`
* Add `test_atomicdistances.py` to test class `AtomicDistances`
* Modify docs for clarity
* Use `box` variable to handle PBCs in `AtomicDistances` for clarity
* Remove file imports for tests and replace with mda.Universe.empty(),
use calc_bonds() instead of dist() for corresponding tests b/c no resid
* Remove unnecessary distance calculations in tests (SciPy, positions)
* Restore `distances.py` and `test_distances.py` to their original
state
* Modify docs so they can build successfully
* Change formatting and wording to look pleasant and consistent
* Add "the distances" before the math expression in first paragraph
@xhgchen xhgchen force-pushed the simple-atomic-distance-analysis branch from 4d68b34 to 0cdabcf Compare April 11, 2023 16:35
@xhgchen
Copy link
Member Author

xhgchen commented Apr 11, 2023

Since it has been a while since this was last reviewed, I was wondering when it might be looked at again? I realize that everyone is likely busy reviewing GSoC applications in addition to their other commitments and I am not in a hurry; it would just help me follow up more promptly when the time comes.

Also, the last push was just to resolve conflicts.

@richardjgowers richardjgowers merged commit 99db4e4 into MDAnalysis:develop May 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

add simple atomic distance analysis to analysis.distances
3 participants