Skip to content

Commit

Permalink
Merge pull request #50 from naotoo1/feature/development_updates
Browse files Browse the repository at this point in the history
Feature/development updates
  • Loading branch information
naotoo1 authored Jul 5, 2024
2 parents dfabffb + d79d527 commit e93d064
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 7 deletions.
19 changes: 19 additions & 0 deletions prosemble/core/distance.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,22 @@ def manhattan_distance(point1, point2):
absolute_difference = abs(difference)
sum_ += absolute_difference
return sum_

def lpnorm_distance(x, y, p):
distances = np.linalg.norm((x - y), ord=p)
return distances

def omega_distance(x, y, omega):
projected_x = x @ omega
projected_y = y @ omega
distances = squared_euclidean_distance(projected_x, projected_y)
return distances

def lomega_distance(x, y, omegas):
projected_x = x @ omegas
projected_y = (np.array(y @ omegas).diagonal()).T
expanded_y = np.expand_dims(projected_y, axis=1)
differences_squared = (expanded_y - projected_x)**2
distances = np.sum(differences_squared, axis=2)
distances = distances.transpose(1, 0)
return distances
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from setuptools import setup, find_packages

with open('README.md') as readme_file:
with open('README.md',encoding="utf-8") as readme_file:
readme = readme_file.read()

here = op.abspath(op.dirname(__file__))
Expand Down
95 changes: 89 additions & 6 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,19 +63,102 @@ def test_squared_euclidean(self):
desired = actual.copy()
for i in range(self.nx):
for j in range(self.ny):
actual[i][j] = (ps.core.squared_euclidean_distance(
actual[i][j] = ps.core.squared_euclidean_distance(
self.x[i],
self.y[j],
)**2)

desired[i][j] = (pairwise_distances(
)
desired[i][j] = pairwise_distances(
self.x[i].reshape(1, -1),
self.y[j].reshape(1, -1),
metric="sqeuclidean",
)**2)
)
mismatch = np.testing.assert_array_almost_equal(actual,
desired,
decimal=2)
self.assertIsNone(mismatch)


def test_lpnorm_p1(self):
actual = np.empty([self.nx, self.ny])
desired = actual.copy()
for i in range(self.nx):
for j in range(self.ny):
actual[i][j] = ps.core.lpnorm_distance(
self.x[i],
self.y[j],
1,
)
desired[i][j] = pairwise_distances(
self.x[i].reshape(1, -1),
self.y[j].reshape(1, -1),
metric="l1",
)
mismatch = np.testing.assert_array_almost_equal(actual,
desired,
decimal=2)
self.assertIsNone(mismatch)

def test_lpnorm_p2(self):
actual = np.empty([self.nx, self.ny])
desired = actual.copy()
for i in range(self.nx):
for j in range(self.ny):
actual[i][j] = ps.core.lpnorm_distance(
self.x[i],
self.y[j],
2,
)
desired[i][j] = pairwise_distances(
self.x[i].reshape(1, -1),
self.y[j].reshape(1, -1),
metric="l2",
)
mismatch = np.testing.assert_array_almost_equal(actual,
desired,
decimal=2)
self.assertIsNone(mismatch)

def test_omega_distance(self):
omega = np.eye(self.mx, self.my)
actual = np.empty([self.nx, self.ny])
desired = actual.copy()
for i in range(self.nx):
for j in range(self.ny):
actual[i][j] = ps.core.omega_distance(
self.x[i],
self.y[j],
omega,
)
desired[i][j] = pairwise_distances(
self.x[i].reshape(1, -1),
self.y[j].reshape(1, -1),
metric="l2",
)**2
mismatch = np.testing.assert_array_almost_equal(actual,
desired,
decimal=2)
self.assertIsNone(mismatch)
self.assertIsNone(mismatch)

def test_lomega_identity(self):
omega = np.eye(self.mx, self.my)
omegas = np.stack([omega for _ in range(self.ny)],axis=0)
actual = ps.core.lomega_distance(
self.x,
self.y,
omegas,
)
desired = np.empty([self.nx, self.ny])
for i in range(self.nx):
for j in range(self.ny):
desired[i][j] = pairwise_distances(
self.x[i].reshape(1, -1),
self.y[j].reshape(1, -1),
metric="l2",
)**2
mismatch = np.testing.assert_array_almost_equal(actual,
desired,
decimal=2)
self.assertIsNone(mismatch)

def tearDown(self):
del self.x, self.y

0 comments on commit e93d064

Please sign in to comment.