Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
MTH607 committed Oct 28, 2023
2 parents a2819bd + bd3a94a commit 53adcd8
Show file tree
Hide file tree
Showing 17 changed files with 625 additions and 14 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
[![Project Status: proof-of-concept – repository is only intended to be a limited example, demo, or proof-of-concept.](https://www.repostatus.org/badges/latest/concept.svg)](https://www.repostatus.org/#concept)
[![Ruff](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/charliermarsh/ruff/main/assets/badge/v2.json)](https://github.com/charliermarsh/ruff)
[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)


# LunchHeroes

Lunch Heros Basel Hack Event Repo
Expand Down
Binary file modified backend/__pycache__/main.cpython-311.pyc
Binary file not shown.
7 changes: 4 additions & 3 deletions backend/main.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from src.routes.users.getUserWithId import getUserWithId
from src.routes.users.getAllUsers import getAllUsers


from src.routes.users.userFunctions import getAllUsers, getUserWithId
from fastapi import FastAPI
from supabase_client import supabase_client

Expand All @@ -12,7 +13,7 @@ def read_root():
return {"Hello": "World", "data" : data}

@app.get("/users")
def read_root():
def load_all_users():
allUsers = getAllUsers()
return { "allUsers" : allUsers}

Expand Down
5 changes: 4 additions & 1 deletion backend/src/lunchheros/distance/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
"""Subpackage related to calculation of distances between users."""

from lunchheros.distance._distances import EuclideanDistance, MahattanDistance

__all__ = ["EuclideanDistances"]
from lunchheros.distance._utils import calculate_distance_matrix

__all__ = ["EuclideanDistance", "MahattanDistance", "calculate_distance_matrix"]
19 changes: 19 additions & 0 deletions backend/src/lunchheros/distance/_distances.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,22 @@ def is_symmetric(self) -> bool:
``False`` should be returned.
"""
return True

class MahattanDistance(interface_distance.Distance):
"""Manhattan distance between two interests."""

def calculate(self, /, interst_vec1, interst_vec2) -> float:
"""Calculates distance between ``interst_val1`` and ``interst_val2``."""

return metrics.pairwise.manhattan_distances([interst_vec1], [interst_vec2])

def is_symmetric(self) -> bool:
"""Returns ``True`` if the similarity function is symmetric,
i.e., :math:`s(t_1, t_2) = s(t_2, t_1)` for all pairs of users.
Note:
If it is not known whether the similarity function is symmetric,
``False`` should be returned.
"""
return True

40 changes: 40 additions & 0 deletions backend/src/lunchheros/distance/_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
"""Temporarily existing module with utility functions.
When the structure of the subpackage starts to appear,
we will move the functions here to the right packages.
Note:
Treat this submodule as a part of the *private* API.
"""

from lunchheros.distance._interface import Distance

import numpy as np


def calculate_distance_matrix(
users: list[list],
*,
distance: Distance,
) -> np.ndarray:
"""Calculates a cross-distance matrix
``d[i, j] = distance(trees1[i], trees2[j])``
Args:
vec1: sequence of trees in one set, length m
vec1: sequence of trees in the second set, length n
distance: distance or similarity function
Returns:
distance matrix, shape (m, n)
"""
m, n = len(users), len(users)

result = np.zeros((m, n))

for i, vec1 in enumerate(users):
for j, vec2 in enumerate(users):
result[i, j] = distance().calculate(vec1, vec2)
# print(f"Distance between user {i} and {j} is {result[i, j]}")

return result
Binary file modified backend/src/routes/users/__pycache__/getUserWithId.cpython-311.pyc
Binary file not shown.
Binary file not shown.
5 changes: 0 additions & 5 deletions backend/src/routes/users/getAllUsers.py

This file was deleted.

4 changes: 0 additions & 4 deletions backend/src/routes/users/getUserWithId.py

This file was deleted.

13 changes: 13 additions & 0 deletions backend/src/routes/users/userFunctions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from supabase_client import supabase_client


users_select = "*, company:companies(*)"

def getUserWithId(userId):
return supabase_client.table("users").select(users_select).eq("id", userId).single().execute()

from supabase_client import supabase_client

def getAllUsers():
return supabase_client.table("users").select(users_select).execute()

46 changes: 45 additions & 1 deletion backend/tests/distance/test_distances.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import numpy as np

from lunchheros.distance import EuclideanDistance
from lunchheros.distance import EuclideanDistance, MahattanDistance


def test_euclidean_distance():
Expand All @@ -17,3 +17,47 @@ def test_euclidean_distance():
dist_func = EuclideanDistance().calculate(a, b)

assert dist == dist_func


def test_euclidean_distance_bool():
"""Tests for the euclidean distance."""

# make a vector of 3 dimensions
a = np.array([1, 0, 1])
b = np.array([1, 0, 0])
# calculate the euclidean distance
dist = np.linalg.norm(a - b)
# calculate the euclidean distance using the function
dist_func = EuclideanDistance().calculate(a, b)

assert dist == dist_func



def test_mahattan_distance():
"""Tests for the euclidean distance."""

# make a vector of 3 dimensions
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
# calculate the euclidean distance
dist = np.sum(np.abs(a - b))
# calculate the euclidean distance using the function
dist_func = MahattanDistance().calculate(a, b)

assert dist == dist_func


def test_mahattan_distance_bool():
"""Tests for the euclidean distance."""

# make a vector of 3 dimensions
a = np.array([1, 0, 1])
b = np.array([1, 0, 0])
# calculate the euclidean distance
dist = np.sum(np.abs(a - b))
# calculate the euclidean distance using the function
dist_func = MahattanDistance().calculate(a, b)

assert dist == dist_func

27 changes: 27 additions & 0 deletions backend/tests/distance/test_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
"""Tests for the distance module."""

import numpy as np

from lunchheros.distance import MahattanDistance, calculate_distance_matrix


def test_calculate_distance_matrix():
"""Tests for the calculate_distance_matrix function."""

# make 3 vectors of length 4
a = np.array([1, 2, 3, 4])
b = np.array([4, 5, 6, 7])
c = np.array([7, 8, 9, 10])
# make a list of the vectors
users = [a, b, c]
# calculate the euclidean distance for all pairs of vectors
dist = np.zeros((3, 3))
for i, vec1 in enumerate(users):
for j, vec2 in enumerate(users):
dist[i, j] = np.sum(np.abs(vec1 - vec2))
# calculate the manhattan distance using the function
dist2 = calculate_distance_matrix(users, distance=MahattanDistance)

print(dist)
print(dist2)
assert np.allclose(dist, dist2)

0 comments on commit 53adcd8

Please sign in to comment.