Spaces:
Sleeping
Sleeping
Commit
·
b58a1ed
1
Parent(s):
fc0d8a3
Added multythreading for the point scoring function
Browse files- Gagna/303/276on/PHOTO-2024-11-13-15-14-58 2.jpg +0 -0
- Gagna/303/276on/PHOTO-2024-11-13-15-14-58 3.jpg +0 -0
- Gagna/303/276on/PHOTO-2024-11-13-15-14-58 4.jpg +0 -0
- Gagna/303/276on/PHOTO-2024-11-13-15-14-58 5.jpg +0 -0
- Gagna/303/276on/PHOTO-2024-11-13-15-14-58.jpg +0 -0
- Gagna/303/276on/PHOTO-2024-11-13-15-14-59 2.jpg +0 -0
- Gagna/303/276on/PHOTO-2024-11-13-15-14-59 3.jpg +0 -0
- Gagna/303/276on/PHOTO-2024-11-13-15-14-59 4.jpg +0 -0
- Gagna/303/276on/PHOTO-2024-11-13-15-14-59 5.jpg +0 -0
- Gagna/303/276on/PHOTO-2024-11-13-15-14-59.jpg +0 -0
- app/data_processing/point_scoring.py +25 -15
Gagna/303/276on/PHOTO-2024-11-13-15-14-58 2.jpg
ADDED
|
Gagna/303/276on/PHOTO-2024-11-13-15-14-58 3.jpg
ADDED
|
Gagna/303/276on/PHOTO-2024-11-13-15-14-58 4.jpg
ADDED
|
Gagna/303/276on/PHOTO-2024-11-13-15-14-58 5.jpg
ADDED
|
Gagna/303/276on/PHOTO-2024-11-13-15-14-58.jpg
ADDED
|
Gagna/303/276on/PHOTO-2024-11-13-15-14-59 2.jpg
ADDED
|
Gagna/303/276on/PHOTO-2024-11-13-15-14-59 3.jpg
ADDED
|
Gagna/303/276on/PHOTO-2024-11-13-15-14-59 4.jpg
ADDED
|
Gagna/303/276on/PHOTO-2024-11-13-15-14-59 5.jpg
ADDED
|
Gagna/303/276on/PHOTO-2024-11-13-15-14-59.jpg
ADDED
|
app/data_processing/point_scoring.py
CHANGED
|
@@ -201,32 +201,41 @@ def get_income_score(income_distribution):
|
|
| 201 |
|
| 202 |
return weighted_sum / total_population
|
| 203 |
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
|
|
|
|
|
|
| 212 |
PENALTY_SCALE = 1.0
|
| 213 |
individual_total_scores = [station["total_score"] for station in station_scores]
|
| 214 |
overlap_factors = [0] * len(stations_coordinates) # Initialize overlap factors for each station
|
| 215 |
-
total_penalty = 0
|
| 216 |
-
|
| 217 |
-
# Aggregate individual scores
|
| 218 |
total_individual_score = sum(individual_total_scores) / len(individual_total_scores) if individual_total_scores else 0
|
| 219 |
|
| 220 |
-
|
| 221 |
-
|
|
|
|
| 222 |
for j, coord2 in enumerate(stations_coordinates):
|
| 223 |
if i != j: # Avoid self-comparison
|
| 224 |
distance = calculate_distance(coord1, coord2)
|
| 225 |
if distance < radius:
|
| 226 |
# Calculate overlap fraction (inverse of distance within the radius)
|
| 227 |
overlap_factor = (radius - distance) / radius # Normalize overlap to [0, 1]
|
| 228 |
-
|
| 229 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 230 |
|
| 231 |
# Scale down individual scores based on overlap factors
|
| 232 |
adjusted_scores = [
|
|
@@ -247,3 +256,4 @@ def calc_score_line(stations_coordinates: List[Tuple[float]], station_scores: di
|
|
| 247 |
}
|
| 248 |
return result
|
| 249 |
|
|
|
|
|
|
| 201 |
|
| 202 |
return weighted_sum / total_population
|
| 203 |
|
| 204 |
+
|
| 205 |
+
from typing import List, Tuple, Dict
|
| 206 |
+
from concurrent.futures import ThreadPoolExecutor, as_completed
|
| 207 |
+
import math
|
| 208 |
+
|
| 209 |
+
def calculate_distance(coord1: Tuple[float, float], coord2: Tuple[float, float]) -> float:
|
| 210 |
+
"""Calculate the Euclidean distance between two coordinates."""
|
| 211 |
+
return math.sqrt((coord1[0] - coord2[0])**2 + (coord1[1] - coord2[1])**2)
|
| 212 |
+
|
| 213 |
+
def calc_score_line(stations_coordinates: List[Tuple[float, float]], station_scores: Dict[str, List[float]], w_density, w_income, w_age, radius):
|
| 214 |
PENALTY_SCALE = 1.0
|
| 215 |
individual_total_scores = [station["total_score"] for station in station_scores]
|
| 216 |
overlap_factors = [0] * len(stations_coordinates) # Initialize overlap factors for each station
|
|
|
|
|
|
|
|
|
|
| 217 |
total_individual_score = sum(individual_total_scores) / len(individual_total_scores) if individual_total_scores else 0
|
| 218 |
|
| 219 |
+
def compute_overlap(i: int, coord1: Tuple[float, float]) -> List[float]:
|
| 220 |
+
"""Compute the overlap factors for a single station."""
|
| 221 |
+
local_overlap = [0] * len(stations_coordinates)
|
| 222 |
for j, coord2 in enumerate(stations_coordinates):
|
| 223 |
if i != j: # Avoid self-comparison
|
| 224 |
distance = calculate_distance(coord1, coord2)
|
| 225 |
if distance < radius:
|
| 226 |
# Calculate overlap fraction (inverse of distance within the radius)
|
| 227 |
overlap_factor = (radius - distance) / radius # Normalize overlap to [0, 1]
|
| 228 |
+
local_overlap[i] += overlap_factor
|
| 229 |
+
local_overlap[j] += overlap_factor
|
| 230 |
+
return local_overlap
|
| 231 |
+
|
| 232 |
+
# Multithreading the computation of overlap factors
|
| 233 |
+
with ThreadPoolExecutor() as executor:
|
| 234 |
+
futures = {executor.submit(compute_overlap, i, coord1): i for i, coord1 in enumerate(stations_coordinates)}
|
| 235 |
+
for future in as_completed(futures):
|
| 236 |
+
i = futures[future]
|
| 237 |
+
local_overlap = future.result()
|
| 238 |
+
overlap_factors = [sum(x) for x in zip(overlap_factors, local_overlap)]
|
| 239 |
|
| 240 |
# Scale down individual scores based on overlap factors
|
| 241 |
adjusted_scores = [
|
|
|
|
| 256 |
}
|
| 257 |
return result
|
| 258 |
|
| 259 |
+
|