File size: 8,108 Bytes
f9c0284 b5e340d f9c0284 b5e340d f9c0284 b5e340d f9c0284 b5e340d f9c0284 b5e340d f9c0284 b5e340d f9c0284 a5af9bb b5e340d f9c0284 b5e340d b213530 b9d47ff b213530 b5e340d f9c0284 b9d47ff b5e340d b9d47ff b5e340d f9c0284 b5e340d f9c0284 b213530 f9c0284 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 |
from io import BytesIO
import numpy as np
import pandas as pd
import plotly.express as px
import streamlit as st
from hilbertcurve.hilbertcurve import HilbertCurve
from sklearn.cluster import KMeans
def cluster_sites_hilbert_curve_same_size(
df: pd.DataFrame,
lat_col: str,
lon_col: str,
region_col: str,
max_sites: int = 25,
mix_regions: bool = False,
):
clusters = []
cluster_id = 0
if not mix_regions:
grouped = df.groupby(region_col)
else:
grouped = [("All", df)]
# Create Hilbert Curve (higher p = more precision)
p = 16 # Adjust based on your coordinate precision needs
hilbert_curve = HilbertCurve(p, 2) # 2D curve
for region, group in grouped:
if len(group) == 0:
continue
# Normalize coordinates to [0, 2^p-1] range
lat_min, lat_max = group[lat_col].min(), group[lat_col].max()
lon_min, lon_max = group[lon_col].min(), group[lon_col].max()
group = group.copy()
group["x"] = ((group[lat_col] - lat_min) / (lat_max - lat_min + 1e-10)) * (
2**p - 1
)
group["y"] = ((group[lon_col] - lon_min) / (lon_max - lon_min + 1e-10)) * (
2**p - 1
)
# Calculate Hilbert distance
group["hilbert"] = group.apply(
lambda row: hilbert_curve.distance_from_point(
[int(row["x"]), int(row["y"])]
),
axis=1,
)
# Sort by Hilbert value
group = group.sort_values("hilbert")
# Create fixed-size clusters
for i in range(0, len(group), max_sites):
cluster = group.iloc[i : i + max_sites].copy()
cluster["Cluster"] = f"C{cluster_id}"
clusters.append(cluster)
cluster_id += 1
result = pd.concat(clusters)
return result.drop(columns=["x", "y", "hilbert"], errors="ignore")
def cluster_sites_kmeans_lower_to_fixed_size(
df: pd.DataFrame,
lat_col: str,
lon_col: str,
region_col: str,
max_sites: int = 25,
mix_regions: bool = False,
):
clusters = []
cluster_id = 0
if not mix_regions:
grouped = df.groupby(region_col)
else:
grouped = [("All", df)]
for region, group in grouped:
coords = group[[lat_col, lon_col]].to_numpy()
remaining_sites = group.copy()
while len(remaining_sites) > 0:
# Calculate number of clusters needed for remaining sites
n_remaining = len(remaining_sites)
n_clusters = max(1, int(np.ceil(n_remaining / max_sites)))
if n_remaining <= max_sites:
# If remaining sites can fit in one cluster
cluster_group = remaining_sites.copy()
cluster_group["Cluster"] = f"C{cluster_id}"
clusters.append(cluster_group)
cluster_id += 1
break
else:
# Apply KMeans to remaining sites
kmeans = KMeans(n_clusters=n_clusters, random_state=42, n_init=10)
labels = kmeans.fit_predict(
remaining_sites[[lat_col, lon_col]].to_numpy()
)
# Split into clusters and check sizes
temp_df = remaining_sites.copy()
temp_df["Cluster"] = labels
temp_df["Temp_Cluster"] = labels
for cluster_num in range(n_clusters):
cluster_group = temp_df[temp_df["Temp_Cluster"] == cluster_num]
if len(cluster_group) <= max_sites:
# If cluster is small enough, keep it
cluster_group = cluster_group.drop(columns=["Temp_Cluster"])
cluster_group["Cluster"] = f"C{cluster_id}"
clusters.append(cluster_group)
cluster_id += 1
# Remove these sites from remaining_sites
remaining_sites = remaining_sites.drop(cluster_group.index)
# Else these sites will remain for next iteration
return pd.concat(clusters)
def to_excel(df: pd.DataFrame) -> bytes:
output = BytesIO()
with pd.ExcelWriter(output, engine="xlsxwriter") as writer:
df.to_excel(writer, index=False, sheet_name="Clusters")
return output.getvalue()
st.title("Automatic Site Clustering App")
# Add description
st.write(
"""This app allows you to cluster sites based on their latitude and longitude.
**Please choose a file containing the latitude and longitude region and site code columns.**
"""
)
# Download Sample file
clustering_sample_file_path = "samples/Site_Clustering.xlsx"
# Create a download button
st.download_button(
label="Download Clustering Sample File",
data=open(clustering_sample_file_path, "rb").read(),
file_name="Site_Clustering.xlsx",
mime="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
)
uploaded_file = st.file_uploader("Upload your Excel file ", type=["xlsx"])
if uploaded_file:
df = pd.read_excel(uploaded_file)
st.write("Sample of uploaded data:", df.head())
columns = df.columns.tolist()
with st.form("clustering_form"):
lat_col = st.selectbox("Select Latitude column", columns)
lon_col = st.selectbox("Select Longitude column", columns)
region_col = st.selectbox("Select Region column", columns)
code_col = st.selectbox("Select Site Code column", columns)
max_sites = st.number_input(
"Max sites per cluster", min_value=5, max_value=100, value=25
)
cluster_method = st.selectbox(
"Select clustering method",
[
"Uniform number of sites for each cluster", # Hilbert Curve
"Number of sites Lower than max but not uniform", # KMeans
],
)
mix_regions = st.checkbox(
"Allow mixing different regions in clusters", value=False
)
submitted = st.form_submit_button("Run Clustering")
if submitted:
if cluster_method == "Uniform number of sites for each cluster":
clustered_df = cluster_sites_hilbert_curve_same_size(
df, lat_col, lon_col, region_col, max_sites, mix_regions
)
elif cluster_method == "Number of sites Lower than max but not uniform":
clustered_df = cluster_sites_kmeans_lower_to_fixed_size(
df, lat_col, lon_col, region_col, max_sites, mix_regions
)
st.success("Clustering completed!")
# Show cluster size per cluster plot
cluster_size = clustered_df["Cluster"].value_counts().sort_index()
fig = px.bar(cluster_size, x=cluster_size.index, y=cluster_size.values)
fig.update_layout(title="Cluster Size")
st.plotly_chart(fig)
# Show cluster size per region plot
cluster_size_per_region = (
clustered_df.groupby([region_col, "Cluster"])
.size()
.reset_index(name="count")
)
fig = px.bar(cluster_size_per_region, x="Cluster", y="count", color=region_col)
fig.update_layout(title="Cluster Size per Region")
st.plotly_chart(fig)
# Map Plot
clustered_df["size"] = 10
fig = px.scatter_map(
clustered_df,
lat=lat_col,
lon=lon_col,
color="Cluster",
size="size",
hover_name=code_col,
hover_data=[region_col],
zoom=5,
height=600,
)
fig.update_layout(mapbox_style="open-street-map")
fig.update_traces(marker=dict(size=15))
st.plotly_chart(fig)
# Download button
st.download_button(
label="Download clustered Excel file",
data=to_excel(clustered_df),
file_name="clustered_sites.xlsx",
mime="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
on_click="ignore",
type="primary",
)
|