1212import os
1313import sys
1414
15- # Ensure the backend root is on path so "app" is importable (works when run as script or -m)
16- _backend_root = os .path .abspath (
17- os .path .join (os .path .dirname (__file__ ), os .pardir , os .pardir , os .pardir )
18- )
19- if _backend_root not in sys .path :
20- sys .path .insert (0 , _backend_root )
15+ sys .path .insert (0 , "/app" )
2116
2217import matplotlib .pyplot as plt
2318import pandas as pd
2419import seaborn as sns
25-
2620from sqlmodel import Session , create_engine , func , select
2721
2822from app .models .location import Location
29- from app .models .location_group import LocationGroup # noqa: F401
30- from app .models .route import Route # noqa: F401
31- from app .models .route_group import RouteGroup # noqa: F401
32- from app .models .route_group_membership import RouteGroupMembership # noqa: F401
3323from app .models .route_stop import RouteStop # noqa: F401
3424from app .models .system_settings import SystemSettings
3525from app .services .implementations .sweep_clustering import (
3626 SweepClusteringAlgorithm ,
3727)
38- from app .utilities .geocoding import geocode
3928
4029# Use the same connection string as seed_database.py
4130DATABASE_URL = "postgresql://postgres:postgres@f4k_db:5432/f4k"
4231
4332# Configure number of locations pulled from csv for testing
4433LOCATIONS_COUNT = 18
4534
46- # Sweep clustering uses these in the loop; both must be set (use high box limit to avoid splitting by boxes).
4735NUM_CLUSTERS = 10
48- MAX_LOCATIONS_PER_CLUSTER = 10
49- MAX_BOXES_PER_CLUSTER = 9999
36+ MAX_LOCATIONS_PER_CLUSTER = 5
37+ MAX_BOXES_PER_CLUSTER = 50
5038
5139
5240async def main () -> None :
@@ -69,32 +57,14 @@ async def main() -> None:
6957 print ("Not enough locations with coordinates to cluster!" )
7058 return
7159
72- # Warehouse coordinates: from SystemSettings (lat/lon or geocode address) or centroid fallback
7360 warehouse_lat : float
7461 warehouse_lon : float
7562 system_settings = session .exec (select (SystemSettings ).limit (1 )).first ()
76- if (
77- system_settings
78- and system_settings .warehouse_latitude is not None
79- and system_settings .warehouse_longitude is not None
80- ):
81- warehouse_lat = system_settings .warehouse_latitude
82- warehouse_lon = system_settings .warehouse_longitude
83- print (f"Using warehouse from system settings: ({ warehouse_lat } , { warehouse_lon } )\n " )
84- elif system_settings and system_settings .warehouse_location :
85- coords = await geocode (system_settings .warehouse_location )
86- if coords is not None :
87- warehouse_lat = coords ["lat" ]
88- warehouse_lon = coords ["lng" ]
89- print (f"Geocoded warehouse: ({ warehouse_lat } , { warehouse_lon } )\n " )
90- else :
91- warehouse_lat = sum (loc .latitude for loc in locations ) / len (locations )
92- warehouse_lon = sum (loc .longitude for loc in locations ) / len (locations )
93- print (f"Geocode failed; using centroid: ({ warehouse_lat } , { warehouse_lon } )\n " )
94- else :
95- warehouse_lat = sum (loc .latitude for loc in locations ) / len (locations )
96- warehouse_lon = sum (loc .longitude for loc in locations ) / len (locations )
97- print (f"No warehouse in system settings; using centroid: ({ warehouse_lat } , { warehouse_lon } )\n " )
63+ warehouse_lat = system_settings .warehouse_latitude
64+ warehouse_lon = system_settings .warehouse_longitude
65+ print (
66+ f"Using warehouse from system settings: ({ warehouse_lat } , { warehouse_lon } )\n "
67+ )
9868
9969 total_boxes = sum (loc .num_boxes for loc in locations )
10070
@@ -151,7 +121,12 @@ async def main() -> None:
151121 print (f" Boxes: { loc .num_boxes } " )
152122 cluster_boxes += loc .num_boxes
153123 df_rows .append (
154- {"name" : name , "long" : loc .longitude , "lat" : loc .latitude , "group" : i }
124+ {
125+ "name" : name ,
126+ "long" : loc .longitude ,
127+ "lat" : loc .latitude ,
128+ "group" : i ,
129+ }
155130 )
156131
157132 print (f"\n Total boxes in cluster: { cluster_boxes } " )
@@ -176,7 +151,9 @@ async def main() -> None:
176151 print ("\n " + "=" * 60 )
177152 print ("Summary:" )
178153 print (f" Total clusters: { len (clusters )} " )
179- print (f" Number of locations in each cluster: { [len (c ) for c in clusters ]} " )
154+ print (
155+ f" Number of locations in each cluster: { [len (c ) for c in clusters ]} "
156+ )
180157 print (f" Total locations clustered: { sum (len (c ) for c in clusters )} " )
181158
182159 except ValueError as e :
0 commit comments