Skip to content

Commit a288e38

Browse files
committed
modernize
1 parent 7a1f1ac commit a288e38

10 files changed

Lines changed: 25 additions & 305 deletions

File tree

allocator/__init__.py

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,14 @@
3232
For more examples: https://geosensing.github.io/allocator/
3333
"""
3434

35+
import logging
36+
import sys
3537
import warnings
3638

3739
warnings.filterwarnings("ignore", message=".*SwigPyPacked.*")
3840
warnings.filterwarnings("ignore", message=".*SwigPyObject.*")
3941
warnings.filterwarnings("ignore", message=".*swigvarlink.*")
4042

41-
import logging
42-
import sys
43-
44-
# Import modern API
4543
from .api import (
4644
ClusterResult,
4745
ComparisonResult,
@@ -60,8 +58,6 @@
6058
tsp_ortools,
6159
tsp_osrm,
6260
)
63-
64-
# Import utilities for advanced users
6561
from .distances import (
6662
euclidean_distance_matrix,
6763
get_distance_matrix,
@@ -71,28 +67,24 @@
7167
osrm_distance_matrix,
7268
xy2latlog,
7369
)
74-
75-
# Import visualization functions
7670
from .viz.plotting import plot_assignments, plot_clusters, plot_comparison, plot_route
7771

78-
# Version
79-
__version__ = "1.0.0"
72+
__version__ = "1.2.0"
8073

81-
# Export public API
8274
__all__ = [
8375
# Result types
8476
"ClusterResult",
8577
"ComparisonResult",
8678
"ItineraryResult",
8779
"RouteResult",
8880
"SortResult",
89-
"assign_to_closest",
9081
# Main functions
82+
"assign_to_closest",
9183
"cluster",
9284
"create_itineraries",
9385
"distance_assignment",
94-
"euclidean_distance_matrix",
9586
# Distance utilities
87+
"euclidean_distance_matrix",
9688
"get_distance_matrix",
9789
"get_logger",
9890
"google_distance_matrix",
@@ -101,8 +93,8 @@
10193
"kmeans",
10294
"latlon2xy",
10395
"osrm_distance_matrix",
104-
"plot_assignments",
10596
# Visualization
97+
"plot_assignments",
10698
"plot_clusters",
10799
"plot_comparison",
108100
"plot_route",
@@ -125,20 +117,16 @@ def setup_logging(level: int = logging.INFO) -> logging.Logger:
125117
Args:
126118
level: Logging level (DEBUG, INFO, WARNING, ERROR)
127119
"""
128-
# Create formatter
129120
formatter = logging.Formatter(
130121
"%(asctime)s - %(name)s - %(levelname)s - %(message)s", datefmt="%Y-%m-%d %H:%M:%S"
131122
)
132123

133-
# Get root logger for allocator package
134124
logger = logging.getLogger("allocator")
135125
logger.setLevel(level)
136126

137-
# Remove existing handlers to avoid duplicates
138127
for handler in logger.handlers[:]:
139128
logger.removeHandler(handler)
140129

141-
# Console handler
142130
console_handler = logging.StreamHandler(sys.stdout)
143131
console_handler.setLevel(level)
144132
console_handler.setFormatter(formatter)
@@ -160,5 +148,4 @@ def get_logger(name: str) -> logging.Logger:
160148
return logging.getLogger(f"allocator.{name}")
161149

162150

163-
# Set up default logging
164151
setup_logging()

allocator/api/cluster.py

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
Modern clustering API for allocator package.
33
"""
44

5-
from __future__ import annotations
6-
75
from pathlib import Path
86

97
import numpy as np
@@ -66,15 +64,16 @@ def kmeans(
6664
Args:
6765
data: Input data as DataFrame or numpy array
6866
n_clusters: Number of clusters
69-
distance: Distance metric ('euclidean', 'haversine', 'osrm', 'google')
67+
distance: Distance metric (stored in metadata only; clustering uses Euclidean)
7068
max_iter: Maximum iterations
7169
random_state: Random seed for reproducibility
72-
**kwargs: Additional distance-specific arguments
70+
**kwargs: Additional arguments (unused, kept for API compatibility)
7371
7472
Returns:
7573
ClusterResult with clustering information
7674
"""
77-
# Ensure we have a DataFrame for output
75+
del kwargs
76+
7877
if isinstance(data, np.ndarray):
7978
df = DataHandler._from_numpy(data)
8079
elif isinstance(data, list):
@@ -84,36 +83,21 @@ def kmeans(
8483
else:
8584
df = data.copy()
8685

87-
# Run clustering algorithm
8886
result = _kmeans_cluster(
8987
df,
9088
n_clusters=n_clusters,
91-
distance_method=distance,
9289
max_iter=max_iter,
9390
random_state=random_state,
94-
**kwargs,
9591
)
9692

97-
# Add cluster assignments to DataFrame
9893
df_result = df.copy()
9994
df_result["cluster"] = result["labels"]
10095

101-
# Calculate inertia (sum of squared distances to centroids)
102-
inertia = None
103-
if distance == "euclidean":
104-
from ..distances import euclidean_distance_matrix
105-
106-
coords = df[["longitude", "latitude"]].values
107-
distances = euclidean_distance_matrix(coords, result["centroids"])
108-
inertia = np.sum(
109-
[distances[i, result["labels"][i]] ** 2 for i in range(len(result["labels"]))]
110-
)
111-
11296
return ClusterResult(
11397
labels=result["labels"],
11498
centroids=result["centroids"],
11599
n_iter=result["iterations"],
116-
inertia=inertia,
100+
inertia=result["inertia"],
117101
data=df_result,
118102
converged=result["converged"],
119103
metadata={

allocator/api/distance.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
Modern distance-based assignment API for allocator package.
33
"""
44

5-
from __future__ import annotations
6-
75
import numpy as np
86
import pandas as pd
97

allocator/api/itinerary.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
API for itinerary generation.
33
"""
44

5-
from __future__ import annotations
6-
75
from typing import Any
86

97
import numpy as np

allocator/api/route.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
Modern routing API for allocator package.
33
"""
44

5-
from __future__ import annotations
6-
75
import numpy as np
86
import pandas as pd
97

allocator/api/types.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
Type definitions and dataclasses for allocator API.
33
"""
44

5-
from __future__ import annotations
6-
75
from dataclasses import dataclass
86
from typing import Any
97

allocator/cli/main.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
Modern CLI interface for allocator package using Click.
33
"""
44

5-
from __future__ import annotations
6-
75
import logging
86

97
import click

allocator/core/__init__.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
"""Core algorithms for clustering and optimization."""
22

33
from .algorithms import (
4-
CustomKMeans,
54
calculate_cluster_statistics,
65
kmeans_cluster,
76
sort_by_distance_assignment,
87
)
98

109
__all__ = [
11-
"CustomKMeans",
1210
"calculate_cluster_statistics",
1311
"kmeans_cluster",
1412
"sort_by_distance_assignment",

0 commit comments

Comments
 (0)