Skip to content

Commit 9110a6e

Browse files
authored
[fmt] Format converters modules and tests (#4850)
1 parent 4f143fd commit 9110a6e

File tree

16 files changed

+1072
-709
lines changed

16 files changed

+1072
-709
lines changed

package/MDAnalysis/converters/OpenMM.py

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,17 @@ class OpenMMSimulationReader(base.SingleFrameReaderBase):
7777
"""
7878

7979
format = "OPENMMSIMULATION"
80-
units = {"time": "ps", "length": "nm", "velocity": "nm/ps",
81-
"force": "kJ/(mol*nm)", "energy": "kJ/mol"}
80+
units = {
81+
"time": "ps",
82+
"length": "nm",
83+
"velocity": "nm/ps",
84+
"force": "kJ/(mol*nm)",
85+
"energy": "kJ/mol",
86+
}
8287

8388
@staticmethod
8489
def _format_hint(thing):
85-
"""Can this reader read *thing*?
86-
"""
90+
"""Can this reader read *thing*?"""
8791
try:
8892
from openmm.app import Simulation
8993
except ImportError:
@@ -104,34 +108,38 @@ def _read_first_frame(self):
104108
self.ts.triclinic_dimensions = self.convert_pos_from_native(
105109
self.ts.triclinic_dimensions, inplace=False
106110
)
107-
self.ts.dimensions[3:] = _sanitize_box_angles(self.ts.dimensions[3:])
111+
self.ts.dimensions[3:] = _sanitize_box_angles(
112+
self.ts.dimensions[3:]
113+
)
108114
self.convert_velocities_from_native(self.ts._velocities)
109115
self.convert_forces_from_native(self.ts._forces)
110116
self.convert_time_from_native(self.ts.dt)
111117

112118
def _mda_timestep_from_omm_context(self):
113-
""" Construct Timestep object from OpenMM context """
119+
"""Construct Timestep object from OpenMM context"""
114120
try:
115121
import openmm.unit as u
116122
except ImportError: # pragma: no cover
117123
import simtk.unit as u
118124

119-
state = self.filename.context.getState(-1, getVelocities=True,
120-
getForces=True, getEnergy=True)
125+
state = self.filename.context.getState(
126+
-1, getVelocities=True, getForces=True, getEnergy=True
127+
)
121128

122129
n_atoms = self.filename.context.getSystem().getNumParticles()
123130

124131
ts = self._Timestep(n_atoms, **self._ts_kwargs)
125132
ts.frame = 0
126133
ts.data["time"] = state.getTime()._value
127134
ts.data["potential_energy"] = (
128-
state.getPotentialEnergy().in_units_of(u.kilojoule/u.mole)._value
135+
state.getPotentialEnergy().in_units_of(u.kilojoule / u.mole)._value
129136
)
130137
ts.data["kinetic_energy"] = (
131-
state.getKineticEnergy().in_units_of(u.kilojoule/u.mole)._value
138+
state.getKineticEnergy().in_units_of(u.kilojoule / u.mole)._value
132139
)
133140
ts.triclinic_dimensions = state.getPeriodicBoxVectors(
134-
asNumpy=True)._value
141+
asNumpy=True
142+
)._value
135143
ts.dimensions[3:] = _sanitize_box_angles(ts.dimensions[3:])
136144
ts.positions = state.getPositions(asNumpy=True)._value
137145
ts.velocities = state.getVelocities(asNumpy=True)._value
@@ -153,8 +161,7 @@ class OpenMMAppReader(base.SingleFrameReaderBase):
153161

154162
@staticmethod
155163
def _format_hint(thing):
156-
"""Can this reader read *thing*?
157-
"""
164+
"""Can this reader read *thing*?"""
158165
try:
159166
from openmm import app
160167
except ImportError:
@@ -163,8 +170,7 @@ def _format_hint(thing):
163170
except ImportError:
164171
return False
165172
else:
166-
return isinstance(thing, (app.PDBFile, app.Modeller,
167-
app.PDBxFile))
173+
return isinstance(thing, (app.PDBFile, app.Modeller, app.PDBxFile))
168174

169175
def _read_first_frame(self):
170176
self.n_atoms = self.filename.topology.getNumAtoms()
@@ -177,10 +183,12 @@ def _read_first_frame(self):
177183
self.ts.triclinic_dimensions = self.convert_pos_from_native(
178184
self.ts.triclinic_dimensions, inplace=False
179185
)
180-
self.ts.dimensions[3:] = _sanitize_box_angles(self.ts.dimensions[3:])
186+
self.ts.dimensions[3:] = _sanitize_box_angles(
187+
self.ts.dimensions[3:]
188+
)
181189

182190
def _mda_timestep_from_omm_app(self):
183-
""" Construct Timestep object from OpenMM Application object """
191+
"""Construct Timestep object from OpenMM Application object"""
184192

185193
omm_object = self.filename
186194
n_atoms = omm_object.topology.getNumAtoms()
@@ -198,7 +206,7 @@ def _mda_timestep_from_omm_app(self):
198206

199207

200208
def _sanitize_box_angles(angles):
201-
""" Ensure box angles correspond to first quadrant
209+
"""Ensure box angles correspond to first quadrant
202210
203211
See `discussion on unitcell angles <https://github.com/MDAnalysis/mdanalysis/pull/2917/files#r620558575>`_
204212
"""

package/MDAnalysis/converters/OpenMMParser.py

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,7 @@ class OpenMMTopologyParser(TopologyReaderBase):
8484

8585
@staticmethod
8686
def _format_hint(thing):
87-
"""Can this Parser read object *thing*?
88-
89-
"""
87+
"""Can this Parser read object *thing*?"""
9088
try:
9189
from openmm import app
9290
except ImportError:
@@ -98,7 +96,7 @@ def _format_hint(thing):
9896
return isinstance(thing, app.Topology)
9997

10098
def _mda_topology_from_omm_topology(self, omm_topology):
101-
""" Construct mda topology from omm topology
99+
"""Construct mda topology from omm topology
102100
103101
Can be used for any openmm object that contains a topology object
104102
@@ -130,9 +128,11 @@ def _mda_topology_from_omm_topology(self, omm_topology):
130128
try:
131129
from simtk.unit import daltons
132130
except ImportError:
133-
msg = ("OpenMM is required for the OpenMMParser but "
134-
"it's not installed. Try installing it with \n"
135-
"conda install -c conda-forge openmm")
131+
msg = (
132+
"OpenMM is required for the OpenMMParser but "
133+
"it's not installed. Try installing it with \n"
134+
"conda install -c conda-forge openmm"
135+
)
136136
raise ImportError(msg)
137137

138138
atom_resindex = [a.residue.index for a in omm_topology.atoms()]
@@ -168,19 +168,21 @@ def _mda_topology_from_omm_topology(self, omm_topology):
168168
if elem.symbol.capitalize() in SYMB2Z:
169169
validated_elements.append(elem.symbol)
170170
else:
171-
validated_elements.append('')
171+
validated_elements.append("")
172172
atomtypes.append(elem.symbol)
173173
masses.append(elem.mass.value_in_unit(daltons))
174174
else:
175-
validated_elements.append('')
175+
validated_elements.append("")
176176
masses.append(0.0)
177-
atomtypes.append('X')
177+
atomtypes.append("X")
178178

179179
if not all(validated_elements):
180180
if any(validated_elements):
181-
warnings.warn("Element information missing for some atoms. "
182-
"These have been given an empty element record ")
183-
if any(i == 'X' for i in atomtypes):
181+
warnings.warn(
182+
"Element information missing for some atoms. "
183+
"These have been given an empty element record "
184+
)
185+
if any(i == "X" for i in atomtypes):
184186
warnings.warn(
185187
"For absent elements, atomtype has been "
186188
"set to 'X' and mass has been set to 0.0. "
@@ -189,10 +191,12 @@ def _mda_topology_from_omm_topology(self, omm_topology):
189191
"to_guess=['masses', 'types']). "
190192
"(for MDAnalysis version 2.x "
191193
"this is done automatically,"
192-
" but it will be removed in 3.0).")
194+
" but it will be removed in 3.0)."
195+
)
193196

194-
attrs.append(Elements(np.array(validated_elements,
195-
dtype=object)))
197+
attrs.append(
198+
Elements(np.array(validated_elements, dtype=object))
199+
)
196200

197201
else:
198202
wmsg = (
@@ -205,7 +209,8 @@ def _mda_topology_from_omm_topology(self, omm_topology):
205209
"These can be guessed using "
206210
"universe.guess_TopologyAttrs("
207211
"to_guess=['masses', 'types']) "
208-
"See MDAnalysis.guessers.")
212+
"See MDAnalysis.guessers."
213+
)
209214

210215
warnings.warn(wmsg)
211216
else:
@@ -239,9 +244,7 @@ class OpenMMAppTopologyParser(OpenMMTopologyParser):
239244

240245
@staticmethod
241246
def _format_hint(thing):
242-
"""Can this Parser read object *thing*?
243-
244-
"""
247+
"""Can this Parser read object *thing*?"""
245248
try:
246249
from openmm import app
247250
except ImportError:
@@ -252,10 +255,7 @@ def _format_hint(thing):
252255
else:
253256
return isinstance(
254257
thing,
255-
(
256-
app.PDBFile, app.Modeller,
257-
app.Simulation, app.PDBxFile
258-
)
258+
(app.PDBFile, app.Modeller, app.Simulation, app.PDBxFile),
259259
)
260260

261261
def parse(self, **kwargs):

0 commit comments

Comments
 (0)