universe.trajectory.n_frames AttributeError: 'property' object has no attribute 'n_frames'

22 views
Skip to first unread message

Falonne NKOU

unread,
Nov 21, 2022, 8:03:45 AM11/21/22
to MDnalysis discussion
Hi everyone,

I am new at MDAnalysis software and I want to plot the angle between two atoms and the z-axis. I use the cp2k MD trajectory files (XYZ). Here is the code I run and the error I get :

 > import MDAnalysis
> from MDAnalysis.analysis.waterdynamics import AngularDistribution as AD
> u = MDAnalysis.Universe('001_B_6glycol.xyz', 'md.xyz')
> selection = ('O', 'C')
> bins = 30
> import MDAnalysis as mda
> universe = mda.Universe
> AD_analysis = AD(universe,selection,bins)
> AD_analysis.run()

--------------------------------------------------------------------------- AttributeError Traceback (most recent call last) <ipython-input-13-a1515026352f> in <module> ----> 1 AD_analysis.run() ~/.local/lib/python3.6/site-packages/MDAnalysis/analysis/waterdynamics.py in run(self, **kwargs) 716 if self.nproc == 1: 717 selection = self._selection_serial( --> 718 self.universe, self.selection_str) 719 else: 720 # not implemented yet ~/.local/lib/python3.6/site-packages/MDAnalysis/analysis/waterdynamics.py in _selection_serial(self, universe, selection_str) 740 selection = [] 741 for ts in ProgressBar(universe.trajectory, verbose=True, --> 742 total=universe.trajectory.n_frames): 743 selection.append(universe.select_atoms(selection_str)) 744 return selection AttributeError: 'property' object has no attribute 'n_frames'


Can someone help me solve this error please ?
Falonne

Florian Leidner

unread,
Nov 21, 2022, 8:57:49 AM11/21/22
to mdnalysis-...@googlegroups.com
I'm not familiar with the AngularDistribution class but  in your code you  declare:

u = MDAnalysis.Universe('001_B_6glycol.xyz', 'md.xyz')
 
Which, I assume, is the trajectory you want to analyze.

A few line later you declare:

> import MDAnalysis as mda
> universe = mda.Universe

You run AD on universe

AD_analysis = AD(universe,selection,bins)
 
Which is just the universe class and does not contain any coordinates.
So as a first step I would modify your code to:

 > import MDAnalysis
> from MDAnalysis.analysis.waterdynamics import AngularDistribution as AD
> u = MDAnalysis.Universe('001_B_6glycol.xyz', 'md.xyz')
> selection = ('O', 'C')
> bins = 30
> import MDAnalysis as mda
> universe = mda.Universe
> AD_analysis = AD(u, selection,bins)
> AD_analysis.run()

After a quick glance at the waterdynamics module I also suspect that selection should be a valid atom selection string so if you want to select all atoms named "O" or "C" it should be something like: "name C or name O". 

Best,
Florian


--
You received this message because you are subscribed to the Google Groups "MDnalysis discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mdnalysis-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/mdnalysis-discussion/5c7e0401-5916-40b6-b918-90d6494456ban%40googlegroups.com.

Falonne NKOU

unread,
Nov 21, 2022, 11:20:24 AM11/21/22
to mdnalysis-...@googlegroups.com
Hi Florian,

Thank you for your contribution. I corrected my code and the error message becomes :

0%
0/10121 [00:00<?, ?it/s]
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-27-a1515026352f> in <module>
----> 1 AD_analysis.run()

~/.local/lib/python3.6/site-packages/MDAnalysis/analysis/waterdynamics.py in run(self, **kwargs)
    716         if self.nproc == 1:
    717             selection = self._selection_serial(
--> 718                 self.universe, self.selection_str)
    719         else:
    720             # not implemented yet

~/.local/lib/python3.6/site-packages/MDAnalysis/analysis/waterdynamics.py in _selection_serial(self, universe, selection_str)
    741         for ts in ProgressBar(universe.trajectory, verbose=True,
    742                               total=universe.trajectory.n_frames):
--> 743             selection.append(universe.select_atoms(selection_str))
    744         return selection
    745 

~/.local/lib/python3.6/site-packages/MDAnalysis/core/universe.py in select_atoms(self, *args, **kwargs)
    638         :meth:`MDAnalysis.core.groups.AtomGroup.select_atoms`
    639         """
--> 640         return self.atoms.select_atoms(*args, **kwargs)
    641 
    642     @property

~/.local/lib/python3.6/site-packages/MDAnalysis/core/groups.py in select_atoms(self, sel, periodic, rtol, atol, updating, sorted, rdkit_kwargs, *othersel, **selgroups)
   3149                                                    sorted=sorted,
   3150                                                    rdkit_kwargs=rdkit_kwargs)
-> 3151                             for s in sel_strs))
   3152         if updating:
   3153             atomgrp = UpdatingAtomGroup(self, selections, sel_strs)

~/.local/lib/python3.6/site-packages/MDAnalysis/core/groups.py in <genexpr>(.0)
   3149                                                    sorted=sorted,
   3150                                                    rdkit_kwargs=rdkit_kwargs)
-> 3151                             for s in sel_strs))
   3152         if updating:
   3153             atomgrp = UpdatingAtomGroup(self, selections, sel_strs)

~/.local/lib/python3.6/site-packages/MDAnalysis/core/selection.py in parse(self, selectstr, selgroups, periodic, atol, rtol, sorted, rdkit_kwargs)
   1437         self.selectstr = selectstr
   1438         self.selgroups = selgroups
-> 1439         tokens = selectstr.replace('(', ' ( ').replace(')', ' ) ')
   1440         self.tokens = collections.deque(tokens.split() + [None])
   1441         parsetree = self.parse_expression(0)

AttributeError: 'tuple' object has no attribute 'replace'

I have a question, if I want to select a particular atom since the "selection" only accepts strings not integers, how should I do it ?

Regards,
Falonne



Irfan Alibay

unread,
Nov 21, 2022, 11:42:35 AM11/21/22
to MDnalysis discussion
Hi Falonne,

Welcome to MDAnalysis!

> I have a question, if I want to select a particular atom since the "selection" only accepts strings not integers, how should I do it ?

Indeed this is the source of your current error, the `selection` argument for AngularDistribution is looking for an MDAnalysis selection string: https://userguide.mdanalysis.org/stable/selections.html#selection-keywords

If you want to select specific atoms you can use the index keywords: https://userguide.mdanalysis.org/stable/selections.html#index (note that these use the MDAnalysis canonical atom indices as detailed here: https://userguide.mdanalysis.org/stable/topology_system.html#canonical-attributes, so it may not directly match the index written in your input file).

Best regards,

Irfan

Falonne NKOU

unread,
Nov 22, 2022, 8:34:39 AM11/22/22
to mdnalysis-...@googlegroups.com
Hi Irfan,

Thank you for your answer.
Even after defining another selection mode, I still get a selection error message. Can you help me please ?

> import MDAnalysis
>from MDAnalysis.analysis.waterdynamics import AngularDistribution as AD
>u = MDAnalysis.Universe('001_B_6glycol.xyz', 'md.xyz')
>selection=u.select_atoms('id 438' and 'id 435'and 'id 436')
> AD_analysis = AD(u,selection,bins)
> AD_analysis.run()

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-10-a1515026352f> in <module>
~/.local/lib/python3.6/site-packages/MDAnalysis/core/groups.py in __getattr__(self, attr)
   2500         elif attr == 'positions':
   2501             raise NoDataError('This Universe has no coordinates')
-> 2502         return super(AtomGroup, self).__getattr__(attr)
   2503 
   2504     @property

~/.local/lib/python3.6/site-packages/MDAnalysis/core/groups.py in __getattr__(self, attr)
    595                 raise NoDataError(err.format(singular=cls.singular))
    596         else:
--> 597             return super(GroupBase, self).__getattr__(attr)
    598 
    599     def __repr__(self):

~/.local/lib/python3.6/site-packages/MDAnalysis/core/groups.py in __getattr__(self, attr)
    385                 match = _TOPOLOGY_ATTRNAMES[clean]
    386                 err += 'Did you mean {match}?'.format(match=match)
--> 387             raise AttributeError(err)
    388 
    389     def get_connections(self, typename, outside=True):

AttributeError: AtomGroup has no attribute replace. 



You received this message because you are subscribed to a topic in the Google Groups "MDnalysis discussion" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/mdnalysis-discussion/T-ObBjzwU5o/unsubscribe.
To unsubscribe from this group and all its topics, send an email to mdnalysis-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/mdnalysis-discussion/bdcb7f29-f08c-4aa0-8807-acc189ec44den%40googlegroups.com.

Oliver Beckstein

unread,
Nov 22, 2022, 11:16:07 AM11/22/22
to mdnalysis-discussion
Hello,

The select_atoms() method takes one or more strings as input. I think you just need to write your selection string as a string:

selection = u.select_atoms('id 438 or id 435 or id 436')

You almost certainly want “or” because “and” would be looking for an atom with all three ids and that does not exist. The operators are boolean operators. You can also write it shorter as

selection = u.select_atoms('id 438 435 436')

which does an implicit “or”. 

The two approaches above will reorder the selection as 435, 436, 438, as described in the linked docs.

Note if you MUST maintain the order then use three individual strings:

selection = u.select_atoms('id 438’, 'id 435’, 'id 436’)

This will give you an AtomGroup with 438, 435, 436. If in doubt, use this one.

Best,
Oliver



--
Oliver Beckstein (he/his/him)

GitHub: @orbeckst

MDAnalysis – a NumFOCUS fiscally sponsored project





Reply all
Reply to author
Forward
0 new messages