Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/wepy/resampling/distances/distance.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,28 @@ def image(self, state):
def image_distance(self, image_a, image_b):
return np.sqrt((image_a[0] - image_b[0]) ** 2 + (image_a[1] - image_b[1]) ** 2)

class ProjectorDistance(Distance):
"""Take a projector as input

"""
def __init__(self, projector):
"""Construct a distance metric.

Parameters
----------

projector : A Projector object, which implementes the project function
"""

self.projector = projector

def image(self, state):
return self.projector.project(state)

def image_distance(self, image_a, image_b):
return np.sqrt(np.sum(np.square(image_a - image_b)))


class AtomPairDistance(Distance):
"""Constructs a vector of atomic distances for each state.
Distance is the root mean squared distance between the vectors.
Expand Down
Empty file.
53 changes: 53 additions & 0 deletions src/wepy/resampling/projectors/centroid.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
"""Projector for determining centroid distances.
"""
# Standard Library
import logging

logger = logging.getLogger(__name__)

# Third Party Library
import numpy as np

from wepy.resampling.projectors.projector import Projector
from wepy.util.util import box_vectors_to_lengths_angles
from geomm.grouping import group_pair

class CentroidProjector(Projector):
"""Projects a state onto the centroid distance between two groups.
"""

def __init__(self, group1_idxs, group2_idxs, periodic=True):
"""Construct a centroid distance projector.

Parameters
----------

group1_idxs : list of int - indices of atoms in group1
group2_idxs : list of int - indices of atoms in group2
periodic : bool (default = True) - whether to use periodic boundary conditions to
minimize centroid distances
"""
self.group1_idxs = group1_idxs
self.group2_idxs = group2_idxs
self.periodic = periodic

def project(self, state):

# cut out only the coordinates we need
coords = np.concatenate([state['positions'][self.group1_idxs],state['positions'][self.group2_idxs]])
idxs1 = list(range(len(self.group1_idxs)))
idxs2 = list(range(len(self.group1_idxs),len(self.group1_idxs) + len(self.group2_idxs)))

if self.periodic:
# get the box lengths from the vectors
box_lengths, box_angles = box_vectors_to_lengths_angles(state["box_vectors"])
coords = group_pair(coords,box_lengths,idxs1,idxs2)

# determine coordinate centroids
c1 = coords[idxs1].mean(axis=0)
c2 = coords[idxs2].mean(axis=0)

# return the distance between the centroids
return np.sqrt(np.sum(np.square(c1-c2)))


43 changes: 43 additions & 0 deletions src/wepy/resampling/projectors/projector.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
"""Modular component for defining "projectors" that project a
walker state into a one- or low-dimensional subspace. These are
usable within different resamplers.

This module contains an abstract base class for Projector classes.

This is similar to the 'image' function in a Distance object
"""
# Standard Library
import logging

logger = logging.getLogger(__name__)

# Third Party Library
import numpy as np

class Projector(object):
"""Abstract Base class for Projector classes."""

def __init__(self):
"""Constructor for Projector class."""
pass

def project(self, state):
"""Compute the 'projection' of a walker state onto one
or more variables.

The abstract implementation is naive and just returns the
numpy array [1].

Parameters
----------
state : object implementing WalkerState
The state which will be transformed to an image

Returns
-------
projection : numpy array
The same state that was given as an argument.

"""

return np.ones((1))
Loading
Loading