Skip to content

Commit 907d7ca

Browse files
author
Charlles Abreu
authored
Removes self-inspection of constructor arguments (#91)
* Removed self-inspection of arguments * Added classes to __all__ to facilitate import
1 parent 677f7f6 commit 907d7ca

24 files changed

+161
-146
lines changed

cvpack/__init__.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,28 @@
2727
from .sheet_rmsd_content import SheetRMSDContent # noqa: F401
2828
from .torsion import Torsion # noqa: F401
2929
from .torsion_similarity import TorsionSimilarity # noqa: F401
30+
31+
__all__ = [
32+
"Angle",
33+
"AtomicFunction",
34+
"AttractionStrength",
35+
"CentroidFunction",
36+
"CollectiveVariable",
37+
"CompositeRMSD",
38+
"Distance",
39+
"HelixAngleContent",
40+
"HelixHBondContent",
41+
"HelixRMSDContent",
42+
"HelixTorsionContent",
43+
"MetaCollectiveVariable",
44+
"NumberOfContacts",
45+
"OpenMMForceWrapper",
46+
"PathInCVSpace",
47+
"RadiusOfGyration",
48+
"RadiusOfGyrationSq",
49+
"ResidueCoordination",
50+
"RMSD",
51+
"SheetRMSDContent",
52+
"Torsion",
53+
"TorsionSimilarity",
54+
]

cvpack/angle.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,9 @@ def __init__(
7272
super().__init__("theta")
7373
self.addAngle(atom1, atom2, atom3, [])
7474
self.setUsesPeriodicBoundaryConditions(pbc)
75-
self._registerCV(name, mmunit.radians, atom1, atom2, atom3, pbc)
75+
self._registerCV(
76+
name, mmunit.radians, atom1=atom1, atom2=atom2, atom3=atom3, pbc=pbc
77+
)
7678
self._registerPeriodicBounds(-np.pi, np.pi)
7779

7880

cvpack/atomic_function.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -150,11 +150,11 @@ def __init__(
150150
self._registerCV(
151151
name,
152152
unit,
153-
function,
154-
unit,
155-
groups,
156-
periodicBounds,
157-
pbc,
153+
function=function,
154+
unit=unit,
155+
groups=groups,
156+
periodicBounds=periodicBounds,
157+
pbc=pbc,
158158
**overalls,
159159
**perbonds,
160160
)

cvpack/attraction_strength.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -230,13 +230,13 @@ def __init__( # pylint: disable=too-many-locals
230230

231231
self._registerCV(
232232
name,
233-
None,
234-
group1,
235-
group2,
236-
nonbondedForce,
237-
contrastGroup,
238-
reference,
239-
contrastScaling,
233+
mmunit.dimensionless,
234+
group1=group1,
235+
group2=group2,
236+
nonbondedForce=nonbondedForce,
237+
contrastGroup=contrastGroup,
238+
reference=reference,
239+
contrastScaling=contrastScaling,
240240
)
241241

242242

cvpack/centroid_function.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -193,13 +193,13 @@ def __init__(
193193
self._registerCV(
194194
name,
195195
unit,
196-
function,
197-
unit,
198-
groups,
199-
collections,
200-
periodicBounds,
201-
pbc,
202-
weighByMass,
196+
function=function,
197+
unit=unit,
198+
groups=groups,
199+
collections=collections,
200+
periodicBounds=periodicBounds,
201+
pbc=pbc,
202+
weighByMass=weighByMass,
203203
**overalls,
204204
**perbonds,
205205
)

cvpack/collective_variable.py

Lines changed: 3 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@
77
88
"""
99

10-
import inspect
1110
import typing as t
12-
from collections import OrderedDict
1311

1412
import openmm
1513
import yaml
@@ -46,8 +44,7 @@ def __deepcopy__(self, _) -> "CollectiveVariable":
4644
def _registerCV(
4745
self,
4846
name: str,
49-
unit: t.Optional[mmunit.Unit],
50-
*args: t.Any,
47+
cvUnit: Unit,
5148
**kwargs: t.Any,
5249
) -> None:
5350
"""
@@ -63,18 +60,13 @@ def _registerCV(
6360
The unit of measurement of this collective variable. It must be a unit
6461
in the MD unit system (mass in Da, distance in nm, time in ps,
6562
temperature in K, energy in kJ/mol, angle in rad).
66-
args
67-
The arguments needed to construct this collective variable
6863
kwargs
6964
The keyword arguments needed to construct this collective variable
7065
"""
7166
self.setName(name)
72-
self._unit = Unit("dimensionless") if unit is None else unit
67+
self._unit = cvUnit
7368
self._mass_unit = Unit(mmunit.dalton * (mmunit.nanometers / self._unit) ** 2)
74-
arguments, _ = self._getArguments()
75-
arguments.pop("name")
76-
self._args = dict(zip(arguments, args))
77-
self._args["name"] = name
69+
self._args = {"name": name}
7870
self._args.update(kwargs)
7971

8072
def _registerPeriodicBounds(
@@ -99,40 +91,6 @@ def _registerPeriodicBounds(
9991
upper = upper.value_in_unit(self.getUnit())
10092
self._periodic_bounds = Quantity((lower, upper), self.getUnit())
10193

102-
@classmethod
103-
def _getArguments(cls) -> t.Tuple[OrderedDict, OrderedDict]:
104-
"""
105-
Inspect the arguments needed for constructing an instance of this collective
106-
variable.
107-
108-
Returns
109-
-------
110-
OrderedDict
111-
A dictionary with the type annotations of all arguments
112-
OrderedDict
113-
A dictionary with the default values of optional arguments
114-
115-
Example
116-
-------
117-
>>> import cvpack
118-
>>> args, defaults = cvpack.RadiusOfGyration._getArguments()
119-
>>> for name, annotation in args.items():
120-
... print(f"{name}: {annotation}")
121-
group: typing.Iterable[int]
122-
pbc: <class 'bool'>
123-
weighByMass: <class 'bool'>
124-
name: <class 'str'>
125-
>>> print(*defaults.items())
126-
('pbc', False) ('weighByMass', False) ('name', 'radius_of_gyration')
127-
"""
128-
arguments = OrderedDict()
129-
defaults = OrderedDict()
130-
for name, parameter in inspect.signature(cls).parameters.items():
131-
arguments[name] = parameter.annotation
132-
if parameter.default is not inspect.Parameter.empty:
133-
defaults[name] = parameter.default
134-
return arguments, defaults
135-
13694
def _setUnusedForceGroup(self, system: openmm.System) -> None:
13795
"""
13896
Set the force group of this collective variable to the one at a given position

cvpack/composite_rmsd.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,13 @@ def __init__(
135135
super().__init__(all_coords)
136136
for group in groups:
137137
self.addGroup(group)
138-
self._registerCV(name, mmunit.nanometers, defined_coords, groups, num_atoms)
138+
self._registerCV(
139+
name,
140+
mmunit.nanometers,
141+
referencePositions=defined_coords,
142+
groups=groups,
143+
numAtoms=num_atoms,
144+
)
139145

140146

141147
CompositeRMSD.registerTag("!cvpack.CompositeRMSD")

cvpack/distance.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def __init__(
5757
super().__init__("r")
5858
self.addBond(atom1, atom2, [])
5959
self.setUsesPeriodicBoundaryConditions(pbc)
60-
self._registerCV(name, mmunit.nanometers, atom1, atom2, pbc)
60+
self._registerCV(name, mmunit.nanometers, atom1=atom1, atom2=atom2, pbc=pbc)
6161

6262

6363
Distance.registerTag("!cvpack.Distance")

cvpack/helix_angle_content.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -129,13 +129,13 @@ def find_alpha_carbon(residue: mmapp.topology.Residue) -> int:
129129
self.setUsesPeriodicBoundaryConditions(pbc)
130130
self._registerCV(
131131
name,
132-
None,
133-
residues,
134-
pbc,
135-
thetaReference,
136-
tolerance,
137-
halfExponent,
138-
normalize,
132+
mmunit.dimensionless,
133+
residues=residues,
134+
pbc=pbc,
135+
thetaReference=thetaReference,
136+
tolerance=tolerance,
137+
halfExponent=halfExponent,
138+
normalize=normalize,
139139
)
140140

141141

cvpack/helix_hbond_content.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,13 @@ def find_atom(residue: mmapp.topology.Residue, pattern: t.Pattern) -> int:
113113
)
114114
self.setUsesPeriodicBoundaryConditions(pbc)
115115
self._registerCV(
116-
name, None, residues, pbc, thresholdDistance, halfExponent, normalize
116+
name,
117+
mmunit.dimensionless,
118+
residues=residues,
119+
pbc=pbc,
120+
thresholdDistance=thresholdDistance,
121+
halfExponent=halfExponent,
122+
normalize=normalize,
117123
)
118124

119125

cvpack/helix_rmsd_content.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,13 @@ def __init__(
150150
normalize,
151151
)
152152
self._registerCV(
153-
name, None, residues, numAtoms, thresholdRMSD, stepFunction, normalize
153+
name,
154+
mmunit.dimensionless,
155+
residues=residues,
156+
numAtoms=numAtoms,
157+
thresholdRMSD=thresholdRMSD,
158+
stepFunction=stepFunction,
159+
normalize=normalize,
154160
)
155161

156162

cvpack/helix_torsion_content.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -146,13 +146,13 @@ def find_atom(residue: mmapp.topology.Residue, name: str) -> int:
146146
self.setUsesPeriodicBoundaryConditions(pbc)
147147
self._registerCV(
148148
name,
149-
None,
150-
residues,
151-
pbc,
152-
phiReference,
153-
psiReference,
154-
tolerance,
155-
halfExponent,
149+
mmunit.dimensionless,
150+
residues=residues,
151+
pbc=pbc,
152+
phiReference=phiReference,
153+
psiReference=psiReference,
154+
tolerance=tolerance,
155+
halfExponent=halfExponent,
156156
)
157157

158158

cvpack/meta_collective_variable.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,10 +131,10 @@ def __init__(
131131
self._registerCV(
132132
name,
133133
unit,
134-
function,
135-
variables,
136-
unit,
137-
periodicBounds,
134+
function=function,
135+
variables=variables,
136+
unit=unit,
137+
periodicBounds=periodicBounds,
138138
**self._parameters,
139139
)
140140
if periodicBounds is not None:

cvpack/number_of_contacts.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -162,15 +162,15 @@ def __init__(
162162
self.setEnergyFunction(expression.replace("/1;", f"/{reference};"))
163163
self._registerCV(
164164
name,
165-
None,
166-
group1,
167-
group2,
168-
nonbondedForce,
169-
reference,
170-
stepFunction,
171-
thresholdDistance,
172-
cutoffFactor,
173-
switchFactor,
165+
mmunit.dimensionless,
166+
group1=group1,
167+
group2=group2,
168+
nonbondedForce=nonbondedForce,
169+
reference=reference,
170+
stepFunction=stepFunction,
171+
thresholdDistance=thresholdDistance,
172+
cutoffFactor=cutoffFactor,
173+
switchFactor=switchFactor,
174174
)
175175

176176

cvpack/openmm_force_wrapper.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,13 @@ def __init__( # pylint: disable=super-init-not-called
7777
unit = Unit(unit)
7878
self._wrapped_force = openmm.XmlSerializer.deserialize(openmmForce)
7979
self.this = self._wrapped_force.this
80-
self._registerCV(name, unit, openmmForce, unit, periodicBounds)
80+
self._registerCV(
81+
name,
82+
unit,
83+
openmmForce=openmmForce,
84+
unit=unit,
85+
periodicBounds=periodicBounds,
86+
)
8187
if periodicBounds is not None:
8288
self._registerPeriodicBounds(*periodicBounds)
8389

cvpack/path_in_cv_space.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from copy import deepcopy
1313

1414
import openmm
15+
from openmm import unit as mmunit
1516

1617
from .collective_variable import CollectiveVariable
1718
from .path import Metric, deviation, progress
@@ -173,12 +174,12 @@ def __init__( # pylint: disable=too-many-branches
173174
self.addCollectiveVariable(f"cv{i}", deepcopy(variable))
174175
self._registerCV(
175176
name,
176-
None,
177-
metric,
178-
variables,
179-
milestones.tolist(),
180-
sigma,
181-
scales,
177+
mmunit.dimensionless,
178+
metric=metric,
179+
variables=variables,
180+
milestones=milestones.tolist(),
181+
sigma=sigma,
182+
scales=scales,
182183
)
183184

184185

cvpack/radius_of_gyration.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,9 @@ def __init__(
9090
num_groups, f"sqrt(({sum_dist_sq})/{num_atoms})", group, pbc, weighByMass
9191
)
9292
self.addBond(list(range(num_groups)))
93-
self._registerCV(name, mmunit.nanometers, group, pbc, weighByMass)
93+
self._registerCV(
94+
name, mmunit.nanometers, group=group, pbc=pbc, weighByMass=weighByMass
95+
)
9496

9597

9698
RadiusOfGyration.registerTag("!cvpack.RadiusOfGyration")

cvpack/radius_of_gyration_sq.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,9 @@ def __init__(
8787
super().__init__(2, f"distance(g1, g2)^2/{num_atoms}", group, pbc, weighByMass)
8888
for atom in group:
8989
self.addBond([atom, num_atoms])
90-
self._registerCV(name, mmunit.nanometers**2, group, pbc, weighByMass)
90+
self._registerCV(
91+
name, mmunit.nanometers**2, group=group, pbc=pbc, weighByMass=weighByMass
92+
)
9193

9294

9395
RadiusOfGyrationSq.registerTag("!cvpack.RadiusOfGyrationSq")

0 commit comments

Comments
 (0)