openforcefield.typing.engines.smirnoff.parameters.
BondHandler
(allow_cosmetic_attributes=False, skip_version_check=False, **kwargs)[source]¶Handle SMIRNOFF <Bonds>
tags
Warning
This API is experimental and subject to change.
A descriptor for ParameterType
attributes.
The descriptors allows associating to the parameter a default value, which makes the attribute optional, a unit, and a custom converter.
Because we may want to have None
as a default value, required
attributes have the default
set to the special type UNDEFINED
.
Converters can be both static or instance functions/methods with respective signatures
converter(value): -> converted_value converter(instance, parameter_attribute, value): -> converted_value
A decorator syntax is available (see example below).
When specified, the descriptor makes this attribute optional by attaching a default value to it.
When specified, only quantities with compatible units are allowed
to be set, and string expressions are automatically parsed into a
Quantity
.
An optional function that can be used to convert values before setting the attribute.
A parameter attribute with multiple terms.
Create a parameter type with an optional and a required attribute.
>>> class MyParameter:
... attr_required = ParameterAttribute()
... attr_optional = ParameterAttribute(default=2)
...
>>> my_par = MyParameter()
Even without explicit assignment, the default value is returned.
>>> my_par.attr_optional
2
If you try to access an attribute without setting it first, an exception is raised.
>>> my_par.attr_required
Traceback (most recent call last):
...
AttributeError: 'MyParameter' object has no attribute '_attr_required'
The attribute allow automatic conversion and validation of units.
>>> from simtk import unit
>>> class MyParameter:
... attr_quantity = ParameterAttribute(unit=unit.angstrom)
...
>>> my_par = MyParameter()
>>> my_par.attr_quantity = '1.0 * nanometer'
>>> my_par.attr_quantity
Quantity(value=1.0, unit=nanometer)
>>> my_par.attr_quantity = 3.0
Traceback (most recent call last):
...
openforcefield.utils.utils.IncompatibleUnitError: attr_quantity=3.0 dimensionless should have units of angstrom
You can attach a custom converter to an attribute.
>>> class MyParameter:
... # Both strings and integers convert nicely to floats with float().
... attr_all_to_float = ParameterAttribute(converter=float)
... attr_int_to_float = ParameterAttribute()
... @attr_int_to_float.converter
... def attr_int_to_float(self, attr, value):
... # This converter converts only integers to float
... # and raise an exception for the other types.
... if isinstance(value, int):
... return float(value)
... elif not isinstance(value, float):
... raise TypeError(f"Cannot convert '{value}' to float")
... return value
...
>>> my_par = MyParameter()
attr_all_to_float accepts and convert to float both strings and integers
>>> my_par.attr_all_to_float = 1
>>> my_par.attr_all_to_float
1.0
>>> my_par.attr_all_to_float = '2.0'
>>> my_par.attr_all_to_float
2.0
The custom converter associated to attr_int_to_float converts only integers instead. >>> my_par.attr_int_to_float = 3 >>> my_par.attr_int_to_float 3.0 >>> my_par.attr_int_to_float = ‘4.0’ Traceback (most recent call last): … TypeError: Cannot convert ‘4.0’ to float
A descriptor for ParameterType
attributes.
The descriptors allows associating to the parameter a default value, which makes the attribute optional, a unit, and a custom converter.
Because we may want to have None
as a default value, required
attributes have the default
set to the special type UNDEFINED
.
Converters can be both static or instance functions/methods with respective signatures
converter(value): -> converted_value converter(instance, parameter_attribute, value): -> converted_value
A decorator syntax is available (see example below).
When specified, the descriptor makes this attribute optional by attaching a default value to it.
When specified, only quantities with compatible units are allowed
to be set, and string expressions are automatically parsed into a
Quantity
.
An optional function that can be used to convert values before setting the attribute.
A parameter attribute with multiple terms.
Create a parameter type with an optional and a required attribute.
>>> class MyParameter:
... attr_required = ParameterAttribute()
... attr_optional = ParameterAttribute(default=2)
...
>>> my_par = MyParameter()
Even without explicit assignment, the default value is returned.
>>> my_par.attr_optional
2
If you try to access an attribute without setting it first, an exception is raised.
>>> my_par.attr_required
Traceback (most recent call last):
...
AttributeError: 'MyParameter' object has no attribute '_attr_required'
The attribute allow automatic conversion and validation of units.
>>> from simtk import unit
>>> class MyParameter:
... attr_quantity = ParameterAttribute(unit=unit.angstrom)
...
>>> my_par = MyParameter()
>>> my_par.attr_quantity = '1.0 * nanometer'
>>> my_par.attr_quantity
Quantity(value=1.0, unit=nanometer)
>>> my_par.attr_quantity = 3.0
Traceback (most recent call last):
...
openforcefield.utils.utils.IncompatibleUnitError: attr_quantity=3.0 dimensionless should have units of angstrom
You can attach a custom converter to an attribute.
>>> class MyParameter:
... # Both strings and integers convert nicely to floats with float().
... attr_all_to_float = ParameterAttribute(converter=float)
... attr_int_to_float = ParameterAttribute()
... @attr_int_to_float.converter
... def attr_int_to_float(self, attr, value):
... # This converter converts only integers to float
... # and raise an exception for the other types.
... if isinstance(value, int):
... return float(value)
... elif not isinstance(value, float):
... raise TypeError(f"Cannot convert '{value}' to float")
... return value
...
>>> my_par = MyParameter()
attr_all_to_float accepts and convert to float both strings and integers
>>> my_par.attr_all_to_float = 1
>>> my_par.attr_all_to_float
1.0
>>> my_par.attr_all_to_float = '2.0'
>>> my_par.attr_all_to_float
2.0
The custom converter associated to attr_int_to_float converts only integers instead. >>> my_par.attr_int_to_float = 3 >>> my_par.attr_int_to_float 3.0 >>> my_par.attr_int_to_float = ‘4.0’ Traceback (most recent call last): … TypeError: Cannot convert ‘4.0’ to float
known_kwargs
List of kwargs that can be parsed by the function.
parameters
The ParameterList that holds this ParameterHandler’s parameter objects
A descriptor for ParameterType
attributes.
The descriptors allows associating to the parameter a default value, which makes the attribute optional, a unit, and a custom converter.
Because we may want to have None
as a default value, required
attributes have the default
set to the special type UNDEFINED
.
Converters can be both static or instance functions/methods with respective signatures
converter(value): -> converted_value converter(instance, parameter_attribute, value): -> converted_value
A decorator syntax is available (see example below).
When specified, the descriptor makes this attribute optional by attaching a default value to it.
When specified, only quantities with compatible units are allowed
to be set, and string expressions are automatically parsed into a
Quantity
.
An optional function that can be used to convert values before setting the attribute.
A parameter attribute with multiple terms.
Create a parameter type with an optional and a required attribute.
>>> class MyParameter:
... attr_required = ParameterAttribute()
... attr_optional = ParameterAttribute(default=2)
...
>>> my_par = MyParameter()
Even without explicit assignment, the default value is returned.
>>> my_par.attr_optional
2
If you try to access an attribute without setting it first, an exception is raised.
>>> my_par.attr_required
Traceback (most recent call last):
...
AttributeError: 'MyParameter' object has no attribute '_attr_required'
The attribute allow automatic conversion and validation of units.
>>> from simtk import unit
>>> class MyParameter:
... attr_quantity = ParameterAttribute(unit=unit.angstrom)
...
>>> my_par = MyParameter()
>>> my_par.attr_quantity = '1.0 * nanometer'
>>> my_par.attr_quantity
Quantity(value=1.0, unit=nanometer)
>>> my_par.attr_quantity = 3.0
Traceback (most recent call last):
...
openforcefield.utils.utils.IncompatibleUnitError: attr_quantity=3.0 dimensionless should have units of angstrom
You can attach a custom converter to an attribute.
>>> class MyParameter:
... # Both strings and integers convert nicely to floats with float().
... attr_all_to_float = ParameterAttribute(converter=float)
... attr_int_to_float = ParameterAttribute()
... @attr_int_to_float.converter
... def attr_int_to_float(self, attr, value):
... # This converter converts only integers to float
... # and raise an exception for the other types.
... if isinstance(value, int):
... return float(value)
... elif not isinstance(value, float):
... raise TypeError(f"Cannot convert '{value}' to float")
... return value
...
>>> my_par = MyParameter()
attr_all_to_float accepts and convert to float both strings and integers
>>> my_par.attr_all_to_float = 1
>>> my_par.attr_all_to_float
1.0
>>> my_par.attr_all_to_float = '2.0'
>>> my_par.attr_all_to_float
2.0
The custom converter associated to attr_int_to_float converts only integers instead. >>> my_par.attr_int_to_float = 3 >>> my_par.attr_int_to_float 3.0 >>> my_par.attr_int_to_float = ‘4.0’ Traceback (most recent call last): … TypeError: Cannot convert ‘4.0’ to float
A descriptor for ParameterType
attributes.
The descriptors allows associating to the parameter a default value, which makes the attribute optional, a unit, and a custom converter.
Because we may want to have None
as a default value, required
attributes have the default
set to the special type UNDEFINED
.
Converters can be both static or instance functions/methods with respective signatures
converter(value): -> converted_value converter(instance, parameter_attribute, value): -> converted_value
A decorator syntax is available (see example below).
When specified, the descriptor makes this attribute optional by attaching a default value to it.
When specified, only quantities with compatible units are allowed
to be set, and string expressions are automatically parsed into a
Quantity
.
An optional function that can be used to convert values before setting the attribute.
A parameter attribute with multiple terms.
Create a parameter type with an optional and a required attribute.
>>> class MyParameter:
... attr_required = ParameterAttribute()
... attr_optional = ParameterAttribute(default=2)
...
>>> my_par = MyParameter()
Even without explicit assignment, the default value is returned.
>>> my_par.attr_optional
2
If you try to access an attribute without setting it first, an exception is raised.
>>> my_par.attr_required
Traceback (most recent call last):
...
AttributeError: 'MyParameter' object has no attribute '_attr_required'
The attribute allow automatic conversion and validation of units.
>>> from simtk import unit
>>> class MyParameter:
... attr_quantity = ParameterAttribute(unit=unit.angstrom)
...
>>> my_par = MyParameter()
>>> my_par.attr_quantity = '1.0 * nanometer'
>>> my_par.attr_quantity
Quantity(value=1.0, unit=nanometer)
>>> my_par.attr_quantity = 3.0
Traceback (most recent call last):
...
openforcefield.utils.utils.IncompatibleUnitError: attr_quantity=3.0 dimensionless should have units of angstrom
You can attach a custom converter to an attribute.
>>> class MyParameter:
... # Both strings and integers convert nicely to floats with float().
... attr_all_to_float = ParameterAttribute(converter=float)
... attr_int_to_float = ParameterAttribute()
... @attr_int_to_float.converter
... def attr_int_to_float(self, attr, value):
... # This converter converts only integers to float
... # and raise an exception for the other types.
... if isinstance(value, int):
... return float(value)
... elif not isinstance(value, float):
... raise TypeError(f"Cannot convert '{value}' to float")
... return value
...
>>> my_par = MyParameter()
attr_all_to_float accepts and convert to float both strings and integers
>>> my_par.attr_all_to_float = 1
>>> my_par.attr_all_to_float
1.0
>>> my_par.attr_all_to_float = '2.0'
>>> my_par.attr_all_to_float
2.0
The custom converter associated to attr_int_to_float converts only integers instead. >>> my_par.attr_int_to_float = 3 >>> my_par.attr_int_to_float 3.0 >>> my_par.attr_int_to_float = ‘4.0’ Traceback (most recent call last): … TypeError: Cannot convert ‘4.0’ to float
Methods
|
A SMIRNOFF bond type |
|
Add a cosmetic attribute to this object. |
|
Add a parameter to the forcefield, ensuring all parameters are valid. |
|
Assign parameters for the given Topology to the specified System object. |
|
Checks whether this ParameterHandler encodes compatible physics as another ParameterHandler. |
|
Delete a cosmetic attribute from this object. |
|
Find the elements of the topology/molecule matched by a parameter type. |
|
Return the parameters in this ParameterHandler that match the parameter_attrs argument |
|
Allow the force to perform a a final post-processing pass on the System following parameter assignment, if needed. |
|
Convert this ParameterHandler to an OrderedDict, compliant with the SMIRNOFF data spec. |
create_force |
__init__
(self, allow_cosmetic_attributes=False, skip_version_check=False, **kwargs)¶Initialize a ParameterHandler, optionally with a list of parameters and other kwargs.
Whether to permit non-spec kwargs. If True, non-spec kwargs will be stored as attributes of this object and can be accessed and modified. Otherwise an exception will be raised if a non-spec kwarg is encountered.
If False, the SMIRNOFF section version will not be checked, and the ParameterHandler will be initialized with version set to _MAX_SUPPORTED_SECTION_VERSION.
The dict representation of the SMIRNOFF data source
Methods
|
Initialize a ParameterHandler, optionally with a list of parameters and other kwargs. |
|
Add a cosmetic attribute to this object. |
|
Add a parameter to the forcefield, ensuring all parameters are valid. |
|
Assign parameters for the given Topology to the specified System object. |
|
Checks whether this ParameterHandler encodes compatible physics as another ParameterHandler. |
|
|
|
Delete a cosmetic attribute from this object. |
|
Find the elements of the topology/molecule matched by a parameter type. |
|
Return the parameters in this ParameterHandler that match the parameter_attrs argument |
|
Allow the force to perform a a final post-processing pass on the System following parameter assignment, if needed. |
|
Convert this ParameterHandler to an OrderedDict, compliant with the SMIRNOFF data spec. |
Attributes
|
A descriptor for |
|
A descriptor for |
List of kwargs that can be parsed by the function. |
|
The ParameterList that holds this ParameterHandler’s parameter objects |
|
|
A descriptor for |
|
A descriptor for |
BondType
(smirks, allow_cosmetic_attributes=False, **kwargs)[source]¶A SMIRNOFF bond type
Warning
This API is experimental and subject to change.
A descriptor for ParameterType
attributes.
The descriptors allows associating to the parameter a default value, which makes the attribute optional, a unit, and a custom converter.
Because we may want to have None
as a default value, required
attributes have the default
set to the special type UNDEFINED
.
Converters can be both static or instance functions/methods with respective signatures
converter(value): -> converted_value converter(instance, parameter_attribute, value): -> converted_value
A decorator syntax is available (see example below).
When specified, the descriptor makes this attribute optional by attaching a default value to it.
When specified, only quantities with compatible units are allowed
to be set, and string expressions are automatically parsed into a
Quantity
.
An optional function that can be used to convert values before setting the attribute.
A parameter attribute with multiple terms.
Create a parameter type with an optional and a required attribute.
>>> class MyParameter:
... attr_required = ParameterAttribute()
... attr_optional = ParameterAttribute(default=2)
...
>>> my_par = MyParameter()
Even without explicit assignment, the default value is returned.
>>> my_par.attr_optional
2
If you try to access an attribute without setting it first, an exception is raised.
>>> my_par.attr_required
Traceback (most recent call last):
...
AttributeError: 'MyParameter' object has no attribute '_attr_required'
The attribute allow automatic conversion and validation of units.
>>> from simtk import unit
>>> class MyParameter:
... attr_quantity = ParameterAttribute(unit=unit.angstrom)
...
>>> my_par = MyParameter()
>>> my_par.attr_quantity = '1.0 * nanometer'
>>> my_par.attr_quantity
Quantity(value=1.0, unit=nanometer)
>>> my_par.attr_quantity = 3.0
Traceback (most recent call last):
...
openforcefield.utils.utils.IncompatibleUnitError: attr_quantity=3.0 dimensionless should have units of angstrom
You can attach a custom converter to an attribute.
>>> class MyParameter:
... # Both strings and integers convert nicely to floats with float().
... attr_all_to_float = ParameterAttribute(converter=float)
... attr_int_to_float = ParameterAttribute()
... @attr_int_to_float.converter
... def attr_int_to_float(self, attr, value):
... # This converter converts only integers to float
... # and raise an exception for the other types.
... if isinstance(value, int):
... return float(value)
... elif not isinstance(value, float):
... raise TypeError(f"Cannot convert '{value}' to float")
... return value
...
>>> my_par = MyParameter()
attr_all_to_float accepts and convert to float both strings and integers
>>> my_par.attr_all_to_float = 1
>>> my_par.attr_all_to_float
1.0
>>> my_par.attr_all_to_float = '2.0'
>>> my_par.attr_all_to_float
2.0
The custom converter associated to attr_int_to_float converts only integers instead. >>> my_par.attr_int_to_float = 3 >>> my_par.attr_int_to_float 3.0 >>> my_par.attr_int_to_float = ‘4.0’ Traceback (most recent call last): … TypeError: Cannot convert ‘4.0’ to float
A descriptor for ParameterType
attributes.
The descriptors allows associating to the parameter a default value, which makes the attribute optional, a unit, and a custom converter.
Because we may want to have None
as a default value, required
attributes have the default
set to the special type UNDEFINED
.
Converters can be both static or instance functions/methods with respective signatures
converter(value): -> converted_value converter(instance, parameter_attribute, value): -> converted_value
A decorator syntax is available (see example below).
When specified, the descriptor makes this attribute optional by attaching a default value to it.
When specified, only quantities with compatible units are allowed
to be set, and string expressions are automatically parsed into a
Quantity
.
An optional function that can be used to convert values before setting the attribute.
A parameter attribute with multiple terms.
Create a parameter type with an optional and a required attribute.
>>> class MyParameter:
... attr_required = ParameterAttribute()
... attr_optional = ParameterAttribute(default=2)
...
>>> my_par = MyParameter()
Even without explicit assignment, the default value is returned.
>>> my_par.attr_optional
2
If you try to access an attribute without setting it first, an exception is raised.
>>> my_par.attr_required
Traceback (most recent call last):
...
AttributeError: 'MyParameter' object has no attribute '_attr_required'
The attribute allow automatic conversion and validation of units.
>>> from simtk import unit
>>> class MyParameter:
... attr_quantity = ParameterAttribute(unit=unit.angstrom)
...
>>> my_par = MyParameter()
>>> my_par.attr_quantity = '1.0 * nanometer'
>>> my_par.attr_quantity
Quantity(value=1.0, unit=nanometer)
>>> my_par.attr_quantity = 3.0
Traceback (most recent call last):
...
openforcefield.utils.utils.IncompatibleUnitError: attr_quantity=3.0 dimensionless should have units of angstrom
You can attach a custom converter to an attribute.
>>> class MyParameter:
... # Both strings and integers convert nicely to floats with float().
... attr_all_to_float = ParameterAttribute(converter=float)
... attr_int_to_float = ParameterAttribute()
... @attr_int_to_float.converter
... def attr_int_to_float(self, attr, value):
... # This converter converts only integers to float
... # and raise an exception for the other types.
... if isinstance(value, int):
... return float(value)
... elif not isinstance(value, float):
... raise TypeError(f"Cannot convert '{value}' to float")
... return value
...
>>> my_par = MyParameter()
attr_all_to_float accepts and convert to float both strings and integers
>>> my_par.attr_all_to_float = 1
>>> my_par.attr_all_to_float
1.0
>>> my_par.attr_all_to_float = '2.0'
>>> my_par.attr_all_to_float
2.0
The custom converter associated to attr_int_to_float converts only integers instead. >>> my_par.attr_int_to_float = 3 >>> my_par.attr_int_to_float 3.0 >>> my_par.attr_int_to_float = ‘4.0’ Traceback (most recent call last): … TypeError: Cannot convert ‘4.0’ to float
A descriptor for ParameterType
attributes.
The descriptors allows associating to the parameter a default value, which makes the attribute optional, a unit, and a custom converter.
Because we may want to have None
as a default value, required
attributes have the default
set to the special type UNDEFINED
.
Converters can be both static or instance functions/methods with respective signatures
converter(value): -> converted_value converter(instance, parameter_attribute, value): -> converted_value
A decorator syntax is available (see example below).
When specified, the descriptor makes this attribute optional by attaching a default value to it.
When specified, only quantities with compatible units are allowed
to be set, and string expressions are automatically parsed into a
Quantity
.
An optional function that can be used to convert values before setting the attribute.
A parameter attribute with multiple terms.
Create a parameter type with an optional and a required attribute.
>>> class MyParameter:
... attr_required = ParameterAttribute()
... attr_optional = ParameterAttribute(default=2)
...
>>> my_par = MyParameter()
Even without explicit assignment, the default value is returned.
>>> my_par.attr_optional
2
If you try to access an attribute without setting it first, an exception is raised.
>>> my_par.attr_required
Traceback (most recent call last):
...
AttributeError: 'MyParameter' object has no attribute '_attr_required'
The attribute allow automatic conversion and validation of units.
>>> from simtk import unit
>>> class MyParameter:
... attr_quantity = ParameterAttribute(unit=unit.angstrom)
...
>>> my_par = MyParameter()
>>> my_par.attr_quantity = '1.0 * nanometer'
>>> my_par.attr_quantity
Quantity(value=1.0, unit=nanometer)
>>> my_par.attr_quantity = 3.0
Traceback (most recent call last):
...
openforcefield.utils.utils.IncompatibleUnitError: attr_quantity=3.0 dimensionless should have units of angstrom
You can attach a custom converter to an attribute.
>>> class MyParameter:
... # Both strings and integers convert nicely to floats with float().
... attr_all_to_float = ParameterAttribute(converter=float)
... attr_int_to_float = ParameterAttribute()
... @attr_int_to_float.converter
... def attr_int_to_float(self, attr, value):
... # This converter converts only integers to float
... # and raise an exception for the other types.
... if isinstance(value, int):
... return float(value)
... elif not isinstance(value, float):
... raise TypeError(f"Cannot convert '{value}' to float")
... return value
...
>>> my_par = MyParameter()
attr_all_to_float accepts and convert to float both strings and integers
>>> my_par.attr_all_to_float = 1
>>> my_par.attr_all_to_float
1.0
>>> my_par.attr_all_to_float = '2.0'
>>> my_par.attr_all_to_float
2.0
The custom converter associated to attr_int_to_float converts only integers instead. >>> my_par.attr_int_to_float = 3 >>> my_par.attr_int_to_float 3.0 >>> my_par.attr_int_to_float = ‘4.0’ Traceback (most recent call last): … TypeError: Cannot convert ‘4.0’ to float
A descriptor for ParameterType
attributes.
The descriptors allows associating to the parameter a default value, which makes the attribute optional, a unit, and a custom converter.
Because we may want to have None
as a default value, required
attributes have the default
set to the special type UNDEFINED
.
Converters can be both static or instance functions/methods with respective signatures
converter(value): -> converted_value converter(instance, parameter_attribute, value): -> converted_value
A decorator syntax is available (see example below).
When specified, the descriptor makes this attribute optional by attaching a default value to it.
When specified, only quantities with compatible units are allowed
to be set, and string expressions are automatically parsed into a
Quantity
.
An optional function that can be used to convert values before setting the attribute.
A parameter attribute with multiple terms.
Create a parameter type with an optional and a required attribute.
>>> class MyParameter:
... attr_required = ParameterAttribute()
... attr_optional = ParameterAttribute(default=2)
...
>>> my_par = MyParameter()
Even without explicit assignment, the default value is returned.
>>> my_par.attr_optional
2
If you try to access an attribute without setting it first, an exception is raised.
>>> my_par.attr_required
Traceback (most recent call last):
...
AttributeError: 'MyParameter' object has no attribute '_attr_required'
The attribute allow automatic conversion and validation of units.
>>> from simtk import unit
>>> class MyParameter:
... attr_quantity = ParameterAttribute(unit=unit.angstrom)
...
>>> my_par = MyParameter()
>>> my_par.attr_quantity = '1.0 * nanometer'
>>> my_par.attr_quantity
Quantity(value=1.0, unit=nanometer)
>>> my_par.attr_quantity = 3.0
Traceback (most recent call last):
...
openforcefield.utils.utils.IncompatibleUnitError: attr_quantity=3.0 dimensionless should have units of angstrom
You can attach a custom converter to an attribute.
>>> class MyParameter:
... # Both strings and integers convert nicely to floats with float().
... attr_all_to_float = ParameterAttribute(converter=float)
... attr_int_to_float = ParameterAttribute()
... @attr_int_to_float.converter
... def attr_int_to_float(self, attr, value):
... # This converter converts only integers to float
... # and raise an exception for the other types.
... if isinstance(value, int):
... return float(value)
... elif not isinstance(value, float):
... raise TypeError(f"Cannot convert '{value}' to float")
... return value
...
>>> my_par = MyParameter()
attr_all_to_float accepts and convert to float both strings and integers
>>> my_par.attr_all_to_float = 1
>>> my_par.attr_all_to_float
1.0
>>> my_par.attr_all_to_float = '2.0'
>>> my_par.attr_all_to_float
2.0
The custom converter associated to attr_int_to_float converts only integers instead. >>> my_par.attr_int_to_float = 3 >>> my_par.attr_int_to_float 3.0 >>> my_par.attr_int_to_float = ‘4.0’ Traceback (most recent call last): … TypeError: Cannot convert ‘4.0’ to float
A descriptor for ParameterType
attributes.
The descriptors allows associating to the parameter a default value, which makes the attribute optional, a unit, and a custom converter.
Because we may want to have None
as a default value, required
attributes have the default
set to the special type UNDEFINED
.
Converters can be both static or instance functions/methods with respective signatures
converter(value): -> converted_value converter(instance, parameter_attribute, value): -> converted_value
A decorator syntax is available (see example below).
When specified, the descriptor makes this attribute optional by attaching a default value to it.
When specified, only quantities with compatible units are allowed
to be set, and string expressions are automatically parsed into a
Quantity
.
An optional function that can be used to convert values before setting the attribute.
A parameter attribute with multiple terms.
Create a parameter type with an optional and a required attribute.
>>> class MyParameter:
... attr_required = ParameterAttribute()
... attr_optional = ParameterAttribute(default=2)
...
>>> my_par = MyParameter()
Even without explicit assignment, the default value is returned.
>>> my_par.attr_optional
2
If you try to access an attribute without setting it first, an exception is raised.
>>> my_par.attr_required
Traceback (most recent call last):
...
AttributeError: 'MyParameter' object has no attribute '_attr_required'
The attribute allow automatic conversion and validation of units.
>>> from simtk import unit
>>> class MyParameter:
... attr_quantity = ParameterAttribute(unit=unit.angstrom)
...
>>> my_par = MyParameter()
>>> my_par.attr_quantity = '1.0 * nanometer'
>>> my_par.attr_quantity
Quantity(value=1.0, unit=nanometer)
>>> my_par.attr_quantity = 3.0
Traceback (most recent call last):
...
openforcefield.utils.utils.IncompatibleUnitError: attr_quantity=3.0 dimensionless should have units of angstrom
You can attach a custom converter to an attribute.
>>> class MyParameter:
... # Both strings and integers convert nicely to floats with float().
... attr_all_to_float = ParameterAttribute(converter=float)
... attr_int_to_float = ParameterAttribute()
... @attr_int_to_float.converter
... def attr_int_to_float(self, attr, value):
... # This converter converts only integers to float
... # and raise an exception for the other types.
... if isinstance(value, int):
... return float(value)
... elif not isinstance(value, float):
... raise TypeError(f"Cannot convert '{value}' to float")
... return value
...
>>> my_par = MyParameter()
attr_all_to_float accepts and convert to float both strings and integers
>>> my_par.attr_all_to_float = 1
>>> my_par.attr_all_to_float
1.0
>>> my_par.attr_all_to_float = '2.0'
>>> my_par.attr_all_to_float
2.0
The custom converter associated to attr_int_to_float converts only integers instead. >>> my_par.attr_int_to_float = 3 >>> my_par.attr_int_to_float 3.0 >>> my_par.attr_int_to_float = ‘4.0’ Traceback (most recent call last): … TypeError: Cannot convert ‘4.0’ to float
Methods
|
Add a cosmetic attribute to this object. |
|
Delete a cosmetic attribute from this object. |
|
Convert this object to dict format. |
add_cosmetic_attribute
(self, attr_name, attr_value)¶Add a cosmetic attribute to this object.
This attribute will not have a functional effect on the object in the Open Force Field toolkit, but can be written out during output.
Warning
The API for modifying cosmetic attributes is experimental
and may change in the future (see issue #338).
Name of the attribute to define for this object.
The value of the attribute to define for this object.
delete_cosmetic_attribute
(self, attr_name)¶Delete a cosmetic attribute from this object.
Warning
The API for modifying cosmetic attributes is experimental
and may change in the future (see issue #338).
Name of the cosmetic attribute to delete.
to_dict
(self, discard_cosmetic_attributes=False)¶Convert this object to dict format.
The returning dictionary contains all the ParameterAttribute
and IndexedParameterAttribute
as well as cosmetic attributes
if discard_cosmetic_attributes
is False
.
Whether to discard non-spec attributes of this object
The SMIRNOFF-compliant dict representation of this object.
check_handler_compatibility
(self, other_handler)[source]¶Checks whether this ParameterHandler encodes compatible physics as another ParameterHandler. This is called if a second handler is attempted to be initialized for the same tag.
The handler to compare to.
add_cosmetic_attribute
(self, attr_name, attr_value)¶Add a cosmetic attribute to this object.
This attribute will not have a functional effect on the object in the Open Force Field toolkit, but can be written out during output.
Warning
The API for modifying cosmetic attributes is experimental
and may change in the future (see issue #338).
Name of the attribute to define for this object.
The value of the attribute to define for this object.
add_parameter
(self, parameter_kwargs)¶Add a parameter to the forcefield, ensuring all parameters are valid.
The kwargs to pass to the ParameterHandler.INFOTYPE (a ParameterType) constructor
assign_parameters
(self, topology, system)¶Assign parameters for the given Topology to the specified System object.
The Topology for which parameters are to be assigned. Either a new Force will be created or parameters will be appended to an existing Force.
The OpenMM System object to add the Force (or append new parameters) to.
delete_cosmetic_attribute
(self, attr_name)¶Delete a cosmetic attribute from this object.
Warning
The API for modifying cosmetic attributes is experimental
and may change in the future (see issue #338).
Name of the cosmetic attribute to delete.
find_matches
(self, entity)¶Find the elements of the topology/molecule matched by a parameter type.
Topology to search.
matches[particle_indices]
is the ParameterType
object
matching the tuple of particle indices in entity
.
get_parameter
(self, parameter_attrs)¶Return the parameters in this ParameterHandler that match the parameter_attrs argument
The attrs mapped to desired values (for example {“smirks”: “[:1]~[#16:2]=,:[#6:3]~[:4]”, “id”: “t105”} )
A list of matching ParameterType objects
known_kwargs
¶List of kwargs that can be parsed by the function.
parameters
¶The ParameterList that holds this ParameterHandler’s parameter objects
postprocess_system
(self, topology, system, **kwargs)¶Allow the force to perform a a final post-processing pass on the System following parameter assignment, if needed.
The Topology for which parameters are to be assigned. Either a new Force will be created or parameters will be appended to an existing Force.
The OpenMM System object to add the Force (or append new parameters) to.
to_dict
(self, discard_cosmetic_attributes=False)¶Convert this ParameterHandler to an OrderedDict, compliant with the SMIRNOFF data spec.
Whether to discard non-spec parameter and header attributes in this ParameterHandler.
SMIRNOFF-spec compliant representation of this ParameterHandler and its internal ParameterList.