Skip to content

Commit

Permalink
Merge branch 'feature/v4_docs_review' into feature/version4
Browse files Browse the repository at this point in the history
# Conflicts:
#	tests/unit_tests/dimension_reduction/test_karcher.py
  • Loading branch information
dimtsap committed Apr 6, 2022
2 parents 9e37f6f + 1132c56 commit dae5ab2
Show file tree
Hide file tree
Showing 51 changed files with 747 additions and 661 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,8 @@

# %%

dmaps = DiffusionMaps.build_from_data(data=X, alpha=1, n_eigenvectors=5,
kernel=GaussianKernel(epsilon=0.3))

# %% md
#
# Use the method `mapping` to compute the diffusion coordinates assuming `epsilon=0.3`.

# %%

dmaps.fit()
dmaps = DiffusionMaps(data=X, alpha=1, n_eigenvectors=5,
kernel=GaussianKernel(epsilon=0.3))

# %% md
#
Expand All @@ -90,4 +82,3 @@
ax = fig.gca(projection='3d')
ax.scatter(x, y, z, c=color, cmap=plt.cm.Spectral, s=8)
plt.show()

Original file line number Diff line number Diff line change
Expand Up @@ -42,30 +42,25 @@
# Case 1: Find the optimal parameter of the Gaussian kernel scale epsilon
kernel = GaussianKernel()

dmaps_object = DiffusionMaps.build_from_data(data=X,
alpha=1.0, n_eigenvectors=9,
is_sparse=True, n_neighbors=100,
optimize_parameters=True,
kernel=kernel)
dmaps_object = DiffusionMaps(data=X,
alpha=1.0, n_eigenvectors=9,
is_sparse=True, n_neighbors=100,
kernel=kernel)

print('epsilon', kernel.epsilon)

# %% md
# Fit the data and calculate the embedding, the eigenvectors and eigenvalues
dmaps_object.fit()

# %% md
#
# Find the parsimonious representation of the eigenvectors. Identify the two most informative
# eigenvectors.

index, residuals = DiffusionMaps.parsimonious(dmaps_object.eigenvectors, 2)
dmaps_object.parsimonious(dim=2)

print('most informative eigenvectors:', index)
print('most informative eigenvectors:', dmaps_object.parsimonious_indices)

# %% md
#
# Plot the diffusion coordinates

DiffusionMaps._plot_eigen_pairs(dmaps_object.eigenvectors, pair_indices=index, color=X_color, figure_size=[12, 12])
DiffusionMaps._plot_eigen_pairs(dmaps_object.eigenvectors, pair_indices=dmaps_object.parsimonious_indices,
color=X_color, figure_size=[12, 12])
plt.show()
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
"""
import numpy as np
from UQpy.dimension_reduction.grassmann_manifold.projections.SvdProjection import SvdProjection
from UQpy.dimension_reduction.grassmann_manifold import Grassmann
from UQpy.dimension_reduction.grassmann_manifold.projections.SVDProjection import SVDProjection
from UQpy.dimension_reduction.grassmann_manifold import GrassmannOperations
from UQpy.utilities.kernels import ProjectionKernel
from UQpy.dimension_reduction.diffusion_maps.DiffusionMaps import DiffusionMaps
import sys
Expand Down Expand Up @@ -56,8 +56,8 @@
# Select the work with maximum rank (number of planes) from each data point (p=sys.maxsize).
# Use the matrix of left eigenvectors to calculate the kernel (KernelComposition.LEFT).

Grassmann_projection = SvdProjection(data=data[::10], p="max")
psi = Grassmann_projection.psi
Grassmann_projection = SVDProjection(data=data[::10], p="max")
psi = Grassmann_projection.u

#%%
#
Expand Down Expand Up @@ -89,11 +89,11 @@
#
# Diffusion maps with Grassmann kernel
kernel = ProjectionKernel()
kernel_matrix = kernel.calculate_kernel_matrix(psi)
kernel.calculate_kernel_matrix(psi)


Gdmaps_UQpy = DiffusionMaps(alpha=1.0, n_eigenvectors=5, is_sparse=True, n_neighbors=250,
kernel_matrix=kernel_matrix)
Gdmaps_UQpy.fit()
kernel_matrix=kernel.kernel_matrix)

#%%
#
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@
import matplotlib
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import make_axes_locatable
from UQpy import SvdProjection
from UQpy import SVDProjection
import sys

from UQpy.utilities import GrassmannPoint
from UQpy.utilities.distances.baseclass.GrassmannianDistance import GrassmannianDistance
from UQpy.utilities.distances.grassmannian_distances.GeodesicDistance import GeodesicDistance

from UQpy.dimension_reduction import Grassmann
from UQpy.dimension_reduction import GrassmannOperations

# %% md
#
Expand Down Expand Up @@ -67,16 +67,16 @@

# %%

manifold_projection = SvdProjection(matrices, p="max")
manifold_projection = SVDProjection(matrices, p="max")

# %% md
#
# Compute the pairwise distances for :math:`\Psi` and :math:`\Phi`, the left and right -singular eigenvectors,
# respectively, of singular value decomposition of each solution.

# %%
p_dim = [manifold_projection.p] * len(manifold_projection.psi)
pairwise_distance = GeodesicDistance().calculate_distance_matrix(points=manifold_projection.psi,
p_dim = [manifold_projection.p] * len(manifold_projection.u)
pairwise_distance = GeodesicDistance().calculate_distance_matrix(points=manifold_projection.u,
p_dim=p_dim)
print(pairwise_distance)

Expand All @@ -86,7 +86,7 @@

# %%

distance01 = GeodesicDistance().compute_distance(manifold_projection.psi[0], manifold_projection.psi[1])
distance01 = GeodesicDistance().compute_distance(manifold_projection.u[0], manifold_projection.u[1])
print(distance01)


Expand All @@ -113,8 +113,8 @@ def compute_distance(self, xi: GrassmannPoint, xj: GrassmannPoint):
return np.sqrt(abs(rank_i - rank_j) * np.pi ** 2 / 4 + np.sum(theta ** 2))


pairwise_distance_psi = UserDistance().calculate_distance_matrix(manifold_projection.psi, p_dim=p_dim)
pairwise_distance_psi = UserDistance().calculate_distance_matrix(manifold_projection.u, p_dim=p_dim)

pairwise_distance_phi = UserDistance().calculate_distance_matrix(manifold_projection.phi, p_dim=p_dim)
pairwise_distance_phi = UserDistance().calculate_distance_matrix(manifold_projection.v, p_dim=p_dim)
print(pairwise_distance_psi)
print(pairwise_distance_phi)
34 changes: 17 additions & 17 deletions docs/code/dimension_reduction/grassmann/plot_grassmann_karcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import make_axes_locatable
import sys
from UQpy.dimension_reduction.grassmann_manifold.projections.SvdProjection import SvdProjection
from UQpy.dimension_reduction import Grassmann
from UQpy.dimension_reduction.grassmann_manifold.projections.SVDProjection import SVDProjection
from UQpy.dimension_reduction import GrassmannOperations

#%% md
#
Expand Down Expand Up @@ -62,18 +62,18 @@

#%%

manifold_projection = SvdProjection(matrices, p="max")
manifold_projection = SVDProjection(matrices, p="max")

# Plot the points on the Grassmann manifold defined by the left singular eigenvectors.
fig, (ax1, ax2, ax3, ax4) = plt.subplots(1, 4)
ax1.title.set_text('Matrix 0')
ax1.imshow(manifold_projection.psi[0].data)
ax1.imshow(manifold_projection.u[0].data)
ax2.title.set_text('Matrix 1')
ax2.imshow(manifold_projection.psi[0].data)
ax2.imshow(manifold_projection.u[0].data)
ax3.title.set_text('Matrix 2')
ax3.imshow(manifold_projection.psi[0].data)
ax3.imshow(manifold_projection.u[0].data)
ax4.title.set_text('Matrix 3')
ax4.imshow(manifold_projection.psi[0].data)
ax4.imshow(manifold_projection.u[0].data)
plt.show()

#%% md
Expand All @@ -85,21 +85,21 @@
from UQpy.utilities.distances.grassmannian_distances.GeodesicDistance import GeodesicDistance

distance_method = GeodesicDistance()
karcher_psi = Grassmann.karcher_mean(grassmann_points=manifold_projection.psi,
distance=distance_method,
optimization_method="GradientDescent")
karcher_phi = Grassmann.karcher_mean(grassmann_points=manifold_projection.phi,
distance=distance_method,
optimization_method="GradientDescent")
karcher_psi = GrassmannOperations.karcher_mean(grassmann_points=manifold_projection.u,
distance=distance_method,
optimization_method="GradientDescent")
karcher_phi = GrassmannOperations.karcher_mean(grassmann_points=manifold_projection.v,
distance=distance_method,
optimization_method="GradientDescent")

#%% md
#
# Project :math:`\Psi`, the left singular eigenvectors, on the tangent space centered at the Karcher mean.

#%%

points_tangent = Grassmann.log_map(grassmann_points=manifold_projection.psi,
reference_point=karcher_psi)
points_tangent = GrassmannOperations.log_map(grassmann_points=manifold_projection.u,
reference_point=karcher_psi)

print(points_tangent[0])
print(points_tangent[1])
Expand All @@ -124,8 +124,8 @@

#%%

points_grassmann = Grassmann.exp_map(tangent_points=points_tangent,
reference_point=manifold_projection.psi[0])
points_grassmann = GrassmannOperations.exp_map(tangent_points=points_tangent,
reference_point=manifold_projection.u[0])

# Plot the matrices
fig, (ax1, ax2, ax3, ax4) = plt.subplots(1, 4)
Expand Down
30 changes: 18 additions & 12 deletions docs/code/dimension_reduction/grassmann/plot_grassmann_kernel.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
import matplotlib
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import make_axes_locatable
from UQpy.dimension_reduction.grassmann_manifold.projections.SvdProjection import SvdProjection
from UQpy.dimension_reduction import Grassmann
from UQpy.dimension_reduction.grassmann_manifold.projections.SVDProjection import SVDProjection
from UQpy.dimension_reduction import GrassmannOperations
from UQpy.utilities import GrassmannPoint
from UQpy.utilities.kernels import Kernel, ProjectionKernel
import sys
Expand Down Expand Up @@ -63,7 +63,7 @@

# %%

manifold_projection = SvdProjection(matrices, p="max")
manifold_projection = SVDProjection(matrices, p="max")

# %% md
#
Expand All @@ -73,8 +73,11 @@
# %%
projection_kernel = ProjectionKernel()

kernel_psi = projection_kernel.calculate_kernel_matrix(points=manifold_projection.psi)
kernel_phi = projection_kernel.calculate_kernel_matrix(points=manifold_projection.phi)
projection_kernel.calculate_kernel_matrix(points=manifold_projection.u)
kernel_psi = projection_kernel.kernel_matrix

projection_kernel.calculate_kernel_matrix(points=manifold_projection.v)
kernel_phi = projection_kernel.kernel_matrix

fig, (ax1, ax2) = plt.subplots(1, 2)
ax1.title.set_text('kernel_psi')
Expand All @@ -89,9 +92,10 @@

# %%

kernel_01 = projection_kernel.calculate_kernel_matrix(points=[manifold_projection.psi[0],
manifold_projection.psi[1],
manifold_projection.psi[2]])
projection_kernel.calculate_kernel_matrix(points=[manifold_projection.u[0],
manifold_projection.u[1],
manifold_projection.u[2]])
kernel_01 = projection_kernel.kernel_matrix

fig = plt.figure()
plt.imshow(kernel_01)
Expand All @@ -105,18 +109,20 @@
# %%
from UQpy.utilities.kernels.baseclass.GrassmannianKernel import GrassmannianKernel


class UserKernel(GrassmannianKernel):

def kernel_entry(self, xi: GrassmannPoint, xj: GrassmannPoint):
def _kernel_entry(self, xi: GrassmannPoint, xj: GrassmannPoint):
r = np.dot(xi.data.T, xj.data)
det = np.linalg.det(r)
return det * det


user_kernel = UserKernel()
kernel_user_psi = user_kernel.calculate_kernel_matrix(points=manifold_projection.psi)
kernel_user_phi = user_kernel.calculate_kernel_matrix(points=manifold_projection.phi)
user_kernel.calculate_kernel_matrix(points=manifold_projection.u)
kernel_user_psi = user_kernel.kernel_matrix

user_kernel.calculate_kernel_matrix(points=manifold_projection.v)
kernel_user_phi = user_kernel.kernel_matrix

fig, (ax1, ax2) = plt.subplots(1, 2)
ax1.title.set_text('kernel_psi')
Expand Down
22 changes: 11 additions & 11 deletions docs/code/dimension_reduction/grassmann/plot_grassmann_log_exp.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
import matplotlib
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import make_axes_locatable
from UQpy.dimension_reduction.grassmann_manifold.projections.SvdProjection import SvdProjection
from UQpy.dimension_reduction import Grassmann
from UQpy.dimension_reduction.grassmann_manifold.projections.SVDProjection import SVDProjection
from UQpy.dimension_reduction import GrassmannOperations
import sys

#%% md
Expand Down Expand Up @@ -62,18 +62,18 @@

#%%

manifold_projection = SvdProjection(matrices, p="max")
manifold_projection = SVDProjection(matrices, p="max")

# Plot the points on the Grassmann manifold defined by the left singular eigenvectors.
fig, (ax1, ax2, ax3, ax4) = plt.subplots(1, 4)
ax1.title.set_text('Matrix 0')
ax1.imshow(manifold_projection.psi[0].data)
ax1.imshow(manifold_projection.u[0].data)
ax2.title.set_text('Matrix 1')
ax2.imshow(manifold_projection.psi[0].data)
ax2.imshow(manifold_projection.u[0].data)
ax3.title.set_text('Matrix 2')
ax3.imshow(manifold_projection.psi[0].data)
ax3.imshow(manifold_projection.u[0].data)
ax4.title.set_text('Matrix 3')
ax4.imshow(manifold_projection.psi[0].data)
ax4.imshow(manifold_projection.u[0].data)
plt.show()

#%% md
Expand All @@ -82,8 +82,8 @@

#%%

points_tangent = Grassmann.log_map(grassmann_points=manifold_projection.psi,
reference_point=manifold_projection.psi[0])
points_tangent = GrassmannOperations.log_map(grassmann_points=manifold_projection.u,
reference_point=manifold_projection.u[0])

print(points_tangent[0])
print(points_tangent[1])
Expand All @@ -108,8 +108,8 @@

#%%

points_grassmann = Grassmann.exp_map(tangent_points=points_tangent,
reference_point=manifold_projection.psi[0])
points_grassmann = GrassmannOperations.exp_map(tangent_points=points_tangent,
reference_point=manifold_projection.u[0])

# Plot the matrices
fig, (ax1, ax2, ax3, ax4) = plt.subplots(1, 4)
Expand Down
9 changes: 6 additions & 3 deletions docs/code/reliability/form/plot_FORM_linear function_2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,17 @@

import shutil

from UQpy.run_model.RunModel import RunModel
from UQpy.run_model.RunModel_New import RunModel_New
from UQpy.run_model.model_execution.PythonModel import PythonModel
from UQpy.distributions import Normal
from UQpy.reliability import FORM

dist1 = Normal(loc=0., scale=1.)
dist2 = Normal(loc=0., scale=1.)
RunModelObject2 = RunModel(model_script='pfn.py', model_object_name="example2", vec=False, ntasks=3)

model = PythonModel(model_script='pfn.py', model_object_name="example2")
RunModelObject2 = RunModel_New(model=model)

Z = FORM(distributions=[dist1, dist2], runmodel_object=RunModelObject2)
Z.run()

Expand All @@ -35,4 +39,3 @@
print('Hasofer-Lind reliability index: %s' % Z.beta)
print('FORM probability of failure: %s' % Z.failure_probability)

shutil.rmtree(RunModelObject2.model_dir)
Loading

0 comments on commit dae5ab2

Please sign in to comment.