API Documentation
Calculating path similarity — pathsimanalysis.psa
Added in version 0.10.0.
The module contains code to calculate the geometric similarity of trajectories using path metrics such as the Hausdorff or Fréchet distances [1]. The path metrics are functions of two paths and return a nonnegative number, i.e., a distance. Two paths are identical if their distance is zero, and large distances indicate dissimilarity. Each path metric is a function of the individual points (e.g., coordinate snapshots) that comprise each path and, loosely speaking, identify the two points, one per path of a pair of paths, where the paths deviate the most. The distance between these points of maximal deviation is measured by the root mean square deviation (RMSD), i.e., to compute structural similarity.
One typically computes the pairwise similarity for an ensemble of paths to produce a symmetric distance matrix, which can be clustered to, at a glance, identify patterns in the trajectory data. To properly analyze a path ensemble, one must select a suitable reference structure to which all paths (each conformer in each path) will be universally aligned using the rotations determined by the best-fit rmsds. Distances between paths and their structures are then computed directly with no further alignment. This pre-processing step is necessary to preserve the metric properties of the Hausdorff and Fréchet metrics; using the best-fit rmsd on a pairwise basis does not generally preserve the triangle inequality.
Note
The PSAnalysisTutorial outlines a typical application of PSA to a set of trajectories, including doing proper alignment, performing distance comparisons, and generating heat map-dendrogram plots from hierarchical clustering.
Helper functions and variables
The following convenience functions are used by other functions in this module.
- pathsimanalysis.psa.sqnorm(v, axis=None)[source]
Compute the sum of squares of elements along specified axes.
- Parameters:
v (numpy.ndarray) – coordinates
axes (None / int / tuple (optional)) – Axes or axes along which a sum is performed. The default (axes =
None
) performs a sum over all the dimensions of the input array. The value of axes may be negative, in which case it counts from the last axis to the zeroth axis.
- Returns:
the sum of the squares of the elements of v along axes
- Return type:
- pathsimanalysis.psa.get_msd_matrix(P, Q, axis=None)[source]
Generate the matrix of pairwise mean-squared deviations between paths.
The MSDs between all pairs of points in P and Q are calculated, each pair having a point from P and a point from Q.
P (Q) is a
numpy.ndarray
of \(N_p\) (\(N_q\)) time steps, \(N\) atoms, and \(3N\) coordinates (e.g.,MDAnalysis.core.groups.AtomGroup.positions
). The pairwise MSD matrix has dimensions \(N_p\) by \(N_q\).- Parameters:
P (numpy.ndarray) – the points in the first path
Q (numpy.ndarray) – the points in the second path
- Returns:
msd_matrix – matrix of pairwise MSDs between points in P and points in Q
- Return type:
numpy.ndarray
Notes
We calculate the MSD matrix
\[M_{ij} = ||p_i - q_j||^2\]where \(p_i \in P\) and \(q_j \in Q\).
- pathsimanalysis.psa.get_coord_axes(path)[source]
Return the number of atoms and the axes corresponding to atoms and coordinates for a given path.
The path is assumed to be a
numpy.ndarray
where the 0th axis corresponds to a frame (a snapshot of coordinates). The \(3N\) (Cartesian) coordinates are assumed to be either:all in the 1st axis, starting with the x,y,z coordinates of the first atom, followed by the x,*y*,*z* coordinates of the 2nd, etc.
in the 1st and 2nd axis, where the 1st axis indexes the atom number and the 2nd axis contains the x,*y*,*z* coordinates of each atom.
Classes, methods, and functions
- pathsimanalysis.psa.get_path_metric_func(name)[source]
Selects a path metric function by name.
- Parameters:
name (str) – name of path metric
- Returns:
path_metric – The path metric function specified by name (if found).
- Return type:
function
- pathsimanalysis.psa.hausdorff(P, Q)[source]
Calculate the symmetric Hausdorff distance between two paths.
The metric used is RMSD, as opposed to the more conventional L2 (Euclidean) norm, because this is convenient for i.e., comparing protein configurations.
P (Q) is a
numpy.ndarray
of \(N_p\) (\(N_q\)) time steps, \(N\) atoms, and \(3N\) coordinates (e.g.,MDAnalysis.core.groups.AtomGroup.positions
). P (Q) has either shape \(N_p \times N \times 3\) (\(N_q \times N \times 3\)), or \(N_p \times (3N)\) (\(N_q \times (3N)\)) in flattened form.Note that reversing the path does not change the Hausdorff distance.
- Parameters:
P (numpy.ndarray) – the points in the first path
Q (numpy.ndarray) – the points in the second path
- Returns:
the Hausdorff distance between paths P and Q
- Return type:
Example
Calculate the Hausdorff distance between two halves of a trajectory:
>>> import MDAnalysis as mda >>> import numpy >>> from MDAnalysis.tests.datafiles import PSF, DCD >>> import pathsimanalysis as psa >>> u = mda.Universe(PSF,DCD) >>> mid = int(len(u.trajectory)/2) >>> ca = u.select_atoms('name CA') >>> P = numpy.array([ ... ca.positions for _ in u.trajectory[:mid:] ... ]) # first half of trajectory >>> Q = numpy.array([ ... ca.positions for _ in u.trajectory[mid::] ... ]) # second half of trajectory >>> psa.hausdorff(P,Q) 4.778663899862152 >>> psa.hausdorff(P,Q[::-1]) # hausdorff distance w/ reversed 2nd trajectory 4.778663899862152
Notes
scipy.spatial.distance.directed_hausdorff()
is an optimized implementation of the early break algorithm of [2]; the latter code is used here to calculate the symmetric Hausdorff distance with an RMSD metric
- pathsimanalysis.psa.hausdorff_wavg(P, Q)[source]
Calculate the weighted average Hausdorff distance between two paths.
P (Q) is a
numpy.ndarray
of \(N_p\) (\(N_q\)) time steps, \(N\) atoms, and \(3N\) coordinates (e.g.,MDAnalysis.core.groups.AtomGroup.positions
). P (Q) has either shape \(N_p \times N \times 3\) (\(N_q \times N \times 3\)), or \(N_p \times (3N)\) (\(N_q \times (3N)\)) in flattened form. The nearest neighbor distances for P (to Q) and those of Q (to P) are averaged individually to get the average nearest neighbor distance for P and likewise for Q. These averages are then summed and divided by 2 to get a measure that gives equal weight to P and Q.- Parameters:
P (numpy.ndarray) – the points in the first path
Q (numpy.ndarray) – the points in the second path
- Returns:
the weighted average Hausdorff distance between paths P and Q
- Return type:
Example
>>> import MDAnalysis as mda >>> from MDAnalysis import Universe >>> from MDAnalysis.tests.datafiles import PSF, DCD >>> import pathsimanalysis as psa >>> u = mda.Universe(PSF,DCD) >>> mid = int(len(u.trajectory)/2) >>> ca = u.select_atoms('name CA') >>> P = numpy.array([ ... ca.positions for _ in u.trajectory[:mid:] ... ]) # first half of trajectory >>> Q = numpy.array([ ... ca.positions for _ in u.trajectory[mid::] ... ]) # second half of trajectory >>> psa.hausdorff_wavg(P,Q) 2.5669644353703447 >>> psa.hausdorff_wavg(P,Q[::-1]) # weighted avg hausdorff dist w/ Q reversed 2.5669644353703447
Notes
The weighted average Hausdorff distance is not a true metric (it does not obey the triangle inequality); see [1] for further details.
- pathsimanalysis.psa.hausdorff_avg(P, Q)[source]
Calculate the average Hausdorff distance between two paths.
P (Q) is a
numpy.ndarray
of \(N_p\) (\(N_q\)) time steps, \(N\) atoms, and \(3N\) coordinates (e.g.,MDAnalysis.core.groups.AtomGroup.positions
). P (Q) has either shape \(N_p \times N \times 3\) (\(N_q \times N \times 3\)), or \(N_p \times (3N)\) (\(N_q \times (3N)\)) in flattened form. The nearest neighbor distances for P (to Q) and those of Q (to P) are all averaged together to get a mean nearest neighbor distance. This measure biases the average toward the path that has more snapshots, whereas weighted average Hausdorff gives equal weight to both paths.- Parameters:
P (numpy.ndarray) – the points in the first path
Q (numpy.ndarray) – the points in the second path
- Returns:
the average Hausdorff distance between paths P and Q
- Return type:
Example
>>> import MDAnalysis as mda >>> from MDAnalysis.tests.datafiles import PSF, DCD >>> import pathsimanalysis as psa >>> u = mda.Universe(PSF,DCD) >>> mid = int(len(u.trajectory)/2) >>> ca = u.select_atoms('name CA') >>> P = numpy.array([ ... ca.positions for _ in u.trajectory[:mid:] ... ]) # first half of trajectory >>> Q = numpy.array([ ... ca.positions for _ in u.trajectory[mid::] ... ]) # second half of trajectory >>> psa.hausdorff_avg(P,Q) 2.5669646575869005 >>> psa.hausdorff_avg(P,Q[::-1]) # hausdorff distance w/ reversed 2nd trajectory 2.5669646575869005
Notes
The average Hausdorff distance is not a true metric (it does not obey the triangle inequality); see [1] for further details.
- pathsimanalysis.psa.hausdorff_neighbors(P, Q)[source]
Find the Hausdorff neighbors of two paths.
P (Q) is a
numpy.ndarray
of \(N_p\) (\(N_q\)) time steps, \(N\) atoms, and \(3N\) coordinates (e.g.,MDAnalysis.core.groups.AtomGroup.positions
). P (Q) has either shape \(N_p \times N \times 3\) (\(N_q \times N \times 3\)), or \(N_p \times (3N)\) (\(N_q \times (3N)\)) in flattened form.- Parameters:
P (numpy.ndarray) – the points in the first path
Q (numpy.ndarray) – the points in the second path
- Returns:
dictionary of two pairs of numpy arrays, the first pair (key “frames”) containing the indices of (Hausdorff) nearest neighbors for P and Q, respectively, the second (key “distances”) containing (corresponding) nearest neighbor distances for P and Q, respectively
- Return type:
Notes
Hausdorff neighbors are those points on the two paths that are separated by the Hausdorff distance. They are the farthest nearest neighbors and are maximally different in the sense of the Hausdorff distance [1].
scipy.spatial.distance.directed_hausdorff()
can also provide the hausdorff neighbors.
- pathsimanalysis.psa.discrete_frechet(P, Q)[source]
Calculate the discrete Fréchet distance between two paths.
P (Q) is a
numpy.ndarray
of \(N_p\) (\(N_q\)) time steps, \(N\) atoms, and \(3N\) coordinates (e.g.,MDAnalysis.core.groups.AtomGroup.positions
). P (Q) has either shape \(N_p \times N \times 3\) (\(N_q \times N \times 3\)), or :\(N_p \times (3N)\) (\(N_q \times (3N)\)) in flattened form.- Parameters:
P (numpy.ndarray) – the points in the first path
Q (numpy.ndarray) – the points in the second path
- Returns:
the discrete Fréchet distance between paths P and Q
- Return type:
Example
Calculate the discrete Fréchet distance between two halves of a trajectory.
>>> import MDAnalysis as mda >>> import numpy as np >>> from MDAnalysis.tests.datafiles import PSF, DCD >>> import pathsimanalysis as psa >>> u = mda.Universe(PSF,DCD) >>> mid = int(len(u.trajectory)/2) >>> ca = u.select_atoms('name CA') >>> P = np.array([ ... ca.positions for _ in u.trajectory[:mid:] ... ]) # first half of trajectory >>> Q = np.array([ ... ca.positions for _ in u.trajectory[mid::] ... ]) # second half of trajectory >>> psa.discrete_frechet(P,Q) 4.778663984013591 >>> psa.discrete_frechet(P,Q[::-1]) # frechet distance w/ 2nd trj reversed 2nd 6.842901117711383
Note that reversing the direction increased the Fréchet distance: it is sensitive to the direction of the path.
Notes
The discrete Fréchet metric is an approximation to the continuous Fréchet metric [3][4]. The calculation of the continuous Fréchet distance is implemented with the dynamic programming algorithm of [5][6].
- pathsimanalysis.psa.dist_mat_to_vec(N, i, j)[source]
Convert distance matrix indices (in the upper triangle) to the index of the corresponding distance vector.
This is a convenience function to locate distance matrix elements (and the pair generating it) in the corresponding distance vector. The row index j should be greater than i+1, corresponding to the upper triangle of the distance matrix.
- Parameters:
N (int) – size of the distance matrix (of shape N-by-N)
i (int) – row index (starting at 0) of the distance matrix
j (int) – column index (starting at 0) of the distance matrix
- Returns:
index (of the matrix element) in the corresponding distance vector
- Return type:
- class pathsimanalysis.psa.Path(universe, reference, select='name CA', path_select='all', ref_frame=0)[source]
Represent a path based on a
Universe
.Pre-process a
Universe
object: (1) fit the trajectory to a reference structure, (2) convert fitted time series to anumpy.ndarray
representation ofPath.path
.The analysis is performed with
PSAnalysis.run()
and stores the result in thenumpy.ndarray
distance matrixPSAnalysis.D
.PSAnalysis.run()
also generates a fitted trajectory and path from alignment of the original trajectories to a reference structure.Added in version 0.9.1.
- u_original
Universe
object with a trajectory
- u_reference
Universe
object containing a reference structure
- select
string, selection for
select_atoms()
to select frame fromPath.u_reference
- path_select
string, selection for
select_atoms()
to select atoms to composePath.path
- ref_frame
int, frame index to select frame from
Path.u_reference
- u_fitted
Universe
object with the fitted trajectory
- path
numpy.ndarray
object representation of the fitted trajectory
- fit_to_reference(filename=None, prefix='', postfix='_fit', rmsdfile=None, targetdir='.', weights=None, tol_mass=0.1)[source]
Align each trajectory frame to the reference structure
- Parameters:
filename (str (optional)) – file name for the RMS-fitted trajectory or pdb; defaults to the original trajectory filename (from
Path.u_original
) with prefix prependedprefix (str (optional)) – prefix for auto-generating the new output filename
rmsdfile (str (optional)) – file name for writing the RMSD time series [
None
]weights ({“mass”,
None
} or array_like (optional)) – choose weights. With"mass"
uses masses as weights; withNone
weigh each atom equally. If a float array of the same length as the selected AtomGroup is provided, use each element of the array_like as a weight for the corresponding atom in the AtomGroup.tol_mass (float (optional)) – Reject match if the atomic masses for matched atoms differ by more than tol_mass [0.1]
- Returns:
MDAnalysis.Universe
object containing a fitted trajectory- Return type:
Universe
Notes
Uses
MDAnalysis.analysis.align.AlignTraj
for the fitting.Deprecated since version 0.16.1: Instead of
mass_weighted=True
use newweights='mass'
; refactored to fit with AnalysisBase APIChanged in version 0.17.0: Deprecated keyword mass_weighted was removed.
- get_num_atoms()[source]
Return the number of atoms used to construct the
Path
.Must run
Path.to_path()
prior to calling this method.
- run(align=False, filename=None, postfix='_fit', rmsdfile=None, targetdir='.', weights=None, tol_mass=0.1, flat=False)[source]
Generate a path from a trajectory and reference structure.
As part of the path generation, the trajectory can be superimposed (“aligned”) to a reference structure if specified.
This is a convenience method to generate a fitted trajectory from an inputted universe (
Path.u_original
) and reference structure (Path.u_reference
).Path.fit_to_reference()
andPath.to_path()
are used consecutively to generate a new universe (Path.u_fitted
) containing the fitted trajectory along with the correspondingPath.path
represented as annumpy.ndarray
. The method returns a tuple of the topology name and new trajectory name, which can be fed directly into anMDAnalysis.Universe
object after unpacking the tuple using the*
operator, as inMDAnalysis.Universe(*(top_name, newtraj_name))
.- Parameters:
align (bool (optional)) – Align trajectory to atom selection
Path.select
ofPath.u_reference
. IfTrue
, a universe containing an aligned trajectory is produced withPath.fit_to_reference()
[False
]filename (str (optional)) – filename for the RMS-fitted trajectory or pdb; defaults to the original trajectory filename (from
Path.u_original
) with prefix prependedpostfix (str (optional)) – prefix for auto-generating the new output filename
rmsdfile (str (optional)) – file name for writing the RMSD time series [
None
]weights ({“mass”,
None
} or array_like (optional)) – choose weights. With"mass"
uses masses as weights; withNone
weigh each atom equally. If a float array of the same length as the selected AtomGroup is provided, use each element of the array_like as a weight for the corresponding atom in the AtomGroup.tol_mass (float (optional)) – Reject match if the atomic masses for matched atoms differ by more than tol_mass [0.1]
flat (bool (optional)) – represent
Path.path
with 2D (\(N_p\times 3N\))numpy.ndarray
; ifFalse
thenPath.path
is a 3D (\(N_p\times N\times 3\))numpy.ndarray
[False
]
- Returns:
topology_trajectory – A tuple of the topology name and new trajectory name.
- Return type:
Deprecated since version 0.16.1: Instead of
mass_weighted=True
use newweights='mass'
; refactored to fit with AnalysisBase APIChanged in version 0.17.0: Deprecated keyword mass_weighted was removed.
- to_path(fitted=False, select=None, flat=False)[source]
Generates a coordinate time series from the fitted universe trajectory.
Given a selection of N atoms from select, the atomic positions for each frame in the fitted universe (
Path.u_fitted
) trajectory (with \(N_p\) total frames) are appended sequentially to form a 3D or 2D (if flat isTrue
)numpy.ndarray
representation of the fitted trajectory (with dimensions \(N_p\times N\times 3\) or \(N_p\times 3N\), respectively).- Parameters:
fitted (bool (optional)) – construct a
Path.path
from thePath.u_fitted
trajectory; ifFalse
thenPath.path
is generated with the trajectory fromPath.u_original
[False
]select (str (optional)) – the selection for constructing the coordinates of each frame in
Path.path
; ifNone
thenPath.path_select
is used, else it is overridden by select [None
]flat (bool (optional)) – represent
Path.path
as a 2D (\(N_p\times 3N\))numpy.ndarray
; ifFalse
thenPath.path
is a 3D (\(N_p\times N\times 3\))numpy.ndarray
[False
]
- Returns:
representing a time series of atomic positions of an
MDAnalysis.core.groups.AtomGroup
selection fromPath.u_fitted.trajectory
- Return type:
numpy.ndarray
- class pathsimanalysis.psa.PSAPair(npaths, i, j)[source]
Generate nearest neighbor and Hausdorff pair information between a pair of paths from an all-pairs comparison generated by
PSA
.The nearest neighbors for each path of a pair of paths is generated by
PSAPair.compute_nearest_neighbors()
and stores the result in a dictionary (nearest_neighbors
): each path has anumpy.ndarray
of the frames of its nearest neighbors, and anumpy.ndarray
of its nearest neighbor distancesPSAnalysis.D
. For example, nearest_neighbors[‘frames’] is a pair ofnumpy.ndarray
, the first being the frames of the nearest neighbors of the first path, i, the second being those of the second path, j.The Hausdorff pair for the pair of paths is found by calling
find_hausdorff_pair()
(locates the nearest neighbor pair having the largest overall distance separating them), which stores the result in a dictionary (hausdorff_pair
) containing the frames (indices) of the pair along with the corresponding (Hausdorff) distance. hausdorff_pair[‘frame’] contains a pair of frames in the first path, i, and the second path, j, respectively, that correspond to the Hausdorff distance between them.Added in version 0.11.
- matrix_id
(int, int), (row, column) indices of the location of this
PSAPair
in the corresponding pairwise distance matrix
- pair_id
int, ID of this
PSAPair
(the pair_id:math:^text{th} comparison) in the distance vector corresponding to the pairwise distance matrix
- class pathsimanalysis.psa.PSAnalysis(universes, reference=None, select='name CA', ref_frame=0, path_select=None, labels=None, targetdir='.')[source]
Perform Path Similarity Analysis (PSA) on a set of trajectories.
The analysis is performed with
PSAnalysis.run()
and stores the result in thenumpy.ndarray
distance matrixPSAnalysis.D
.PSAnalysis.run()
also generates a fitted trajectory and path from alignment of the original trajectories to a reference structure.Added in version 0.8.
Changed in version 1.0.0:
save_result()
method has been removed. You can usenp.save()
onPSAnalysis.D
instead.- universes
list of
MDAnalysis.Universe
objects containing trajectories
- u_reference
MDAnalysis.Universe
object containing a reference structure
- select
string, selection for
select_atoms()
to select frame fromPSAnalysis.u_reference
- path_select
string, selection for
select_atoms()
to select atoms to composePath.path
- ref_frame
int, frame index to select frame from
Path.u_reference
- paths
list of
numpy.ndarray
objects representing the set/ensemble of fitted trajectories
- D
numpy.ndarray
which stores the calculated distance matrix
- cluster(dist_mat=None, method='ward', count_sort=False, distance_sort=False, no_plot=False, no_labels=True, color_threshold=4)[source]
Cluster trajectories and optionally plot the dendrogram.
This method is used by
PSAnalysis.plot()
to generate a heatmap- dendrogram combination plot. By default, the distance matrix,PSAnalysis.D
, is assumed to exist, converted to distance-vector form, and inputted tocluster.hierarchy.linkage()
to generate a clustering. For convenience in plotting arbitrary distance matrices, one can also be specify dist_mat, which will be checked for proper distance matrix form byspatial.distance.squareform()
- Parameters:
dist_mat (numpy.ndarray) – user-specified distance matrix to be clustered [
None
]method (str) – name of linkage criterion for clustering [
'ward'
]no_plot (bool) – if
True
, do not render the dendrogram [False
]no_labels (bool) – if
True
then do not label dendrogram [True
]color_threshold (float) – For brevity, let t be the color_threshold. Colors all the descendent links below a cluster node k the same color if k is the first node below the cut threshold t. All links connecting nodes with distances greater than or equal to the threshold are colored blue. If t is less than or equal to zero, all nodes are colored blue. If color_threshold is None or ‘default’, corresponding with MATLAB(TM) behavior, the threshold is set to 0.7*max(Z[:,2]). [
4
]]
- Returns:
Z – output from
scipy.cluster.hierarchy.linkage()
; list of indices representing the row-wise order of the objects after clusteringdgram – output from
scipy.cluster.hierarchy.dendrogram()
- generate_paths(align=False, filename=None, infix='', weights=None, tol_mass=False, ref_frame=None, flat=False, save=True, store=False)[source]
Generate paths, aligning each to reference structure if necessary.
- Parameters:
align (bool) – Align trajectories to atom selection
PSAnalysis.select
ofPSAnalysis.u_reference
[False
]filename (str) – strings representing base filename for fitted trajectories and paths [
None
]infix (str) – additional tag string that is inserted into the output filename of the fitted trajectory files [‘’]
weights ({“mass”,
None
} or array_like (optional)) – choose weights. With"mass"
uses masses as weights; withNone
weigh each atom equally. If a float array of the same length as the selected AtomGroup is provided, use each element of the array_like as a weight for the corresponding atom in the AtomGroup [None
]tol_mass (float) – Reject match if the atomic masses for matched atoms differ by more than tol_mass [
False
]ref_frame (int) – frame index to select frame from reference [
None
]flat (bool) – represent
Path.path
as a 2D (\(N_p\times 3N\))numpy.ndarray
; ifFalse
thenPath.path
is a 3D (\(N_p\times N\times 3\))numpy.ndarray
[False
]save (bool) – if
True
, pickle list of names for fitted trajectories [True
]store (bool) – if
True
then writes each path (numpy.ndarray
) inPSAnalysis.paths
to compressed npz (numpy) files [False
]
The fitted trajectories are written to new files in the “/trj_fit” subdirectory in
PSAnalysis.targetdir
named “filename(trajectory)XXX*infix*_psa”, where “XXX” is a number between 000 and 999; the extension of each file is the same as its original. Optionally, the trajectories can also be saved in numpy compressed npz format in the “/paths” subdirectory inPSAnalysis.targetdir
for persistence and can be accessed as the attributePSAnalysis.paths
.Deprecated since version 0.16.1: Instead of
mass_weighted=True
use newweights='mass'
; refactored to fit with AnalysisBase APIChanged in version 0.17.0: Deprecated keyword mass_weighted was removed.
Changed in version 1.0.0: Defaults for the store and filename keywords have been changed from True and fitted to False and None respectively. These now match the docstring documented defaults.
- get_num_atoms()[source]
Return the number of atoms used to construct the
Path
instances inPSA
.- Returns:
the number of atoms in any path
- Return type:
Note
Must run
PSAnalysis.generate_paths()
prior to calling this method.
- get_num_paths()[source]
Return the number of paths in
PSA
.Note
Must run
PSAnalysis.generate_paths()
prior to calling this method.- Returns:
the number of paths in
PSA
- Return type:
- get_pairwise_distances(vectorform=False, checks=False)[source]
Return the distance matrix (or vector) of pairwise path distances.
Note
Must run
PSAnalysis.run()
prior to calling this method.- Parameters:
vectorform (bool) – if
True
, return the distance vector instead [False
]checks (bool) – if
True
, check thatPSAnalysis.D
is a proper distance matrix [False
]
- Returns:
representation of the distance matrix (or vector)
- Return type:
numpy.ndarray
- get_paths()[source]
Return the paths in
PSA
.Note
Must run
PSAnalysis.generate_paths()
prior to calling this method.- Returns:
list of
numpy.ndarray
representations of paths inPSA
- Return type:
- property hausdorff_pairs
The Hausdorff pair for each (unique) pairs of paths.
This attribute contains a list of Hausdorff pair information (in distance vector order), where each element is a dictionary containing the pair of frames and the (Hausdorff) distance between a pair of paths. See
PSAnalysis.psa_pairs()
andPSAPair.hausdorff_pair
for more information about accessing Hausdorff pair data.Note
Must run
PSAnalysis.run_pairs_analysis()
withhausdorff_pairs=True
prior to calling this method.
- load()[source]
Load fitted paths specified by ‘psa_path-names.pkl’ in
PSAnalysis.targetdir
.All filenames are determined by
PSAnalysis
.See also
- property nearest_neighbors
The nearest neighbors for each (unique) pair of paths.
This attribute contains a list of nearest neighbor information (in distance vector order), where each element is a dictionary containing the nearest neighbor frames and distances between a pair of paths. See
PSAnalysis.psa_pairs()
andPSAPair.nearest_neighbors
for more information about accessing nearest neighbor data.Note
Must run
PSAnalysis.run_pairs_analysis()
withneighbors=True
prior to calling this method.
- plot(filename=None, linkage='ward', count_sort=False, distance_sort=False, figsize=4.5, labelsize=12)[source]
Plot a clustered distance matrix.
Usese method linkage and plots the corresponding dendrogram. Rows (and columns) are identified using the list of strings specified by
PSAnalysis.labels
.If filename is supplied then the figure is also written to file (the suffix determines the file type, e.g. pdf, png, eps, …). All other keyword arguments are passed on to
matplotlib.pyplot.matshow()
.- Parameters:
filename (str) – save figure to filename [
None
]linkage (str) – name of linkage criterion for clustering [
'ward'
]count_sort (bool) – see
scipy.cluster.hierarchy.dendrogram()
[False
]distance_sort (bool) – see
scipy.cluster.hierarchy.dendrogram()
[False
]figsize (float) – set the vertical size of plot in inches [
4.5
]labelsize (float) – set the font size for colorbar labels; font size for path labels on dendrogram default to 3 points smaller [
12
]
- Returns:
- plot_annotated_heatmap(filename=None, linkage='ward', count_sort=False, distance_sort=False, figsize=8, annot_size=6.5)[source]
Plot a clustered distance matrix.
Uses method linkage and plots annotated distances in the matrix. Rows (and columns) are identified using the list of strings specified by
PSAnalysis.labels
.If filename is supplied then the figure is also written to file (the suffix determines the file type, e.g. pdf, png, eps, …). All other keyword arguments are passed on to
matplotlib.pyplot.imshow()
.- Parameters:
filename (str) – save figure to filename [
None
]linkage (str) – name of linkage criterion for clustering [
'ward'
]count_sort (bool) – see
scipy.cluster.hierarchy.dendrogram()
[False
]distance_sort (bool) – see
scipy.cluster.hierarchy.dendrogram()
[False
]figsize (float) – set the vertical size of plot in inches [
4.5
]annot_size (float) – font size of annotation labels on heat map [
6.5
]
- Returns:
Note
This function requires the seaborn package, which can be installed with pip install seaborn or conda install seaborn.
Changed in version 1.0.0:
tick1On
,tick2On
,label1On
andlabel2On
changed totick1line
,tick2line
,label1
andlabel2
due to upstream deprecation (see #2493)
- plot_nearest_neighbors(filename=None, idx=0, labels=('Path 1', 'Path 2'), figsize=4.5, multiplot=False, aspect_ratio=1.75, labelsize=12)[source]
Plot nearest neighbor distances as a function of normalized frame number.
The frame number is mapped to the interval [0, 1].
If filename is supplied then the figure is also written to file (the suffix determines the file type, e.g. pdf, png, eps, …). All other keyword arguments are passed on to
matplotlib.pyplot.imshow()
.- Parameters:
filename (str) – save figure to filename [
None
]idx (int) – index of path (pair) comparison to plot [
0
]labels ((str, str)) – pair of names to label nearest neighbor distance curves [
('Path 1', 'Path 2')
]figsize (float) – set the vertical size of plot in inches [
4.5
]multiplot (bool) – set to
True
to enable plotting multiple nearest neighbor distances on the same figure [False
]aspect_ratio (float) – set the ratio of width to height of the plot [
1.75
]labelsize (float) – set the font size for colorbar labels; font size for path labels on dendrogram default to 3 points smaller [
12
]
- Returns:
ax
- Return type:
axes
Note
This function requires the seaborn package, which can be installed with pip install seaborn or conda install seaborn.
- property psa_pairs
The list of
PSAPair
instances for each pair of paths.psa_pairs
is a list of allPSAPair
objects (in distance vector order). The elements of aPSAPair
are pairs of paths that have been compared usingPSAnalysis.run_pairs_analysis()
. EachPSAPair
contains nearest neighbor and Hausdorff pair information specific to a pair of paths. The nearest neighbor frames and distances for aPSAPair
can be accessed in the nearest neighbor dictionary using the keys ‘frames’ and ‘distances’, respectively. E.g.,PSAPair.nearest_neighbors['distances']
returns a pair ofnumpy.ndarray
corresponding to the nearest neighbor distances for each path. Similarly, Hausdorff pair information can be accessed usingPSAPair.hausdorff_pair
with the keys ‘frames’ and ‘distance’.Note
Must run
PSAnalysis.run_pairs_analysis()
prior to calling this method.
- run(**kwargs)[source]
Perform path similarity analysis on the trajectories to compute the distance matrix.
A number of parameters can be changed from the defaults. The result is stored as the array
PSAnalysis.D
.- Parameters:
metric (str or callable) – selection string specifying the path metric to measure pairwise distances among
PSAnalysis.paths
or a callable with the same call signature ashausdorff()
['hausdorff'
]start (int) – start and stop frame index with step size: analyze
trajectory[start:stop:step]
[None
]stop (int)
step (int)
.. versionchanged:: 1.0.0 – store and filename have been removed.
- run_pairs_analysis(**kwargs)[source]
Perform PSA Hausdorff (nearest neighbor) pairs analysis on all unique pairs of paths in
PSAnalysis.paths
.Partial results can be stored in separate lists, where each list is indexed according to distance vector convention (i.e., element (i,j) in distance matrix representation corresponds to element \(s=N*i+j-(i+1)*(i+2)\) in distance vector representation, which is the \(s\)th comparison). For each unique pair of paths, the nearest neighbors for that pair can be stored in
NN
and the Hausdorff pair inHP
.PP
stores the full information of Hausdorff pairs analysis that is available for each pair of path, including nearest neighbors lists and the Hausdorff pairs.The pairwise distances are stored as the array
PSAnalysis.D
.- Parameters:
start (int) – start and stop frame index with step size: analyze
trajectory[start:stop:step]
[None
]stop (int)
step (int)
neighbors (bool) – if
True
, then stores dictionary of nearest neighbor frames/distances inPSAnalysis.NN
[False
]hausdorff_pairs (bool) – if
True
, then stores dictionary of Hausdorff pair frames/distances inPSAnalysis.HP
[False
]
- save_paths(filename=None)[source]
Save fitted
PSAnalysis.paths
to numpy compressed npz files.The data are saved with
numpy.savez_compressed()
in the directory specified byPSAnalysis.targetdir
.- Parameters:
filename (str) – specifies filename [
None
]- Returns:
filename
- Return type:
See also
References