Skip to content

Commit

Permalink
Add docs to workflow of conformer_generation
Browse files Browse the repository at this point in the history
Add rst files and clean up docstrings
  • Loading branch information
xiaoruiDong committed Sep 11, 2023
1 parent b4cef53 commit 2a0e19f
Show file tree
Hide file tree
Showing 10 changed files with 472 additions and 176 deletions.
1 change: 1 addition & 0 deletions docs/source/reference/conformer_generation/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ rdmc.conformer_generation
embedding_geometries/index
geometry_optimization/index
postprocessing/index
workflow/index
utils
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
rdmc.conformer_generation.generators
=====================================

.. automodule:: rdmc.conformer_generation.generators
:members:
:undoc-members:
:show-inheritance:
10 changes: 10 additions & 0 deletions docs/source/reference/conformer_generation/workflow/index.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Workflow
=====================

.. toctree::
:maxdepth: 2

generators
ts_generators
metrics
sampler
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
rdmc.conformer_generation.metrics
=====================================

.. automodule:: rdmc.conformer_generation.metrics
:members:
:undoc-members:
:show-inheritance:
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
rdmc.conformer_generation.sampler
=======================================

.. automodule:: rdmc.conformer_generation.sampler
:members:
:undoc-members:
:show-inheritance:
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
rdmc.conformer_generation.ts_generators
=======================================

.. automodule:: rdmc.conformer_generation.ts_generators
:members:
:undoc-members:
:show-inheritance:
202 changes: 159 additions & 43 deletions rdmc/conformer_generation/generators.py

Large diffs are not rendered by default.

64 changes: 49 additions & 15 deletions rdmc/conformer_generation/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,40 +6,57 @@
"""

import numpy as np
from typing import Optional
from typing import List, Optional

R = 0.0019872 # kcal/(K*mol)


class SCGMetric:
"""
A class to calculate and track the given metric ("entropy", "partition function", or "total conformers") for a molecule over time.
Args:
metric (str, optional): Metric to be calculated. Options are ``"entropy"``, ``"partition function"``, or ``"total conformers"``.
Defaults to ``"entropy"``.
window (int, optional): Window size to compute the change in metric (doesn't work when the metric is "total conformers").
Defaults to ``5``.
threshold (float, optional): Threshold for the change in metric to decide when to stop generating conformers.
Defaults to ``0.01``.
T (float, optional): Temperature for entropy or partition function calculations. Defaults to ``298`` K.
"""

def __init__(
self,
metric: Optional[str] = "entropy",
window: Optional[int] = 5,
threshold: Optional[float] = 0.01,
T: Optional[float] = 298,
):
def __init__(self,
metric: Optional[str] = "entropy",
window: Optional[int] = 5,
threshold: Optional[float] = 0.01,
T: Optional[float] = 298,
):
"""
Generate an SCGMetric instance.
Args:
metric (str): Metric to be calculated.
window (int): Window size to compute the change in metric (doesn't work when the metric is "total conformers").
threshold (float): Threshold for the change in metric to decide when to stop generating conformers.
T (float): Temperature for entropy or partition function calculations.
metric (str, optional): Metric to be calculated. Options are ``"entropy"``, ``"partition function"``, or ``"total conformers"``.
Defaults to ``"entropy"``.
window (int, optional): Window size to compute the change in metric (doesn't work when the metric is "total conformers").
Defaults to ``5``.
threshold (float, optional): Threshold for the change in metric to decide when to stop generating conformers.
Defaults to ``0.01``.
T (float, optional): Temperature for entropy or partition function calculations. Defaults to ``298`` K.
"""
self.metric = metric
self.window = window
self.threshold = threshold
self.T = T
self.metric_history = []

def calculate_metric(self, mol_data):
def calculate_metric(self,
mol_data: List[dict]):
"""
Calculate the metric for a given molecule. The calculated value will be appended to the ``metric_history`` list.
Args:
mol_data (List[dict]): A list of dictionaries with molecule conformers.
"""
if self.metric == "entropy":
metric_val = self.calculate_entropy(mol_data)

Expand All @@ -55,7 +72,12 @@ def calculate_metric(self, mol_data):
self.metric_history.append(metric_val)

def check_metric(self):
"""
Check if the change in metric is below the threshold.
Returns:
bool: ``True`` if the change in metric is below the threshold, ``False`` otherwise.
"""
if self.metric == "total conformers":
return False
else:
Expand All @@ -66,17 +88,29 @@ def check_metric(self):
)
return True if change <= self.threshold else False

def calculate_entropy(self, mol_data):
def calculate_entropy(self,
mol_data: List[dict]):
"""
Calculate the entropy of a molecule.
Args:
mol_data (List[dict]): A list of dictionaries with molecule conformers.
"""
energies = np.array([c["energy"] for c in mol_data])
energies = energies - energies.min()
_prob = np.exp(-energies / (R * self.T))
prob = _prob / _prob.sum()
entropy = -R * np.sum(prob * np.log(prob))
return entropy

def calculate_partition_function(self, mol_data):
def calculate_partition_function(self,
mol_data: List[dict]):
"""
Calculate the partition function of a molecule.
Args:
mol_data (List[dict]): A list of dictionaries with molecule conformers.
"""
energies = np.array([c["energy"] for c in mol_data])
energies = energies - energies.min()
prob = np.exp(-energies / (R * self.T))
Expand Down
Loading

0 comments on commit 2a0e19f

Please sign in to comment.