engralimalik commited on
Commit
ffb3516
·
verified ·
1 Parent(s): 281c408

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -28
app.py CHANGED
@@ -5,36 +5,48 @@ from folium.plugins import MarkerCluster
5
  import requests
6
  from io import BytesIO
7
 
8
- # Load data from Excel URL
9
  def load_data(url):
10
- response = requests.get(url)
11
- # Read the Excel file from the response content
12
- lat_long_data = pd.read_excel(BytesIO(response.content), sheet_name="lat long", engine='openpyxl')
13
- measurement_data = pd.read_excel(BytesIO(response.content), sheet_name="measurement data", engine='openpyxl')
14
-
15
- # Merge data on school_id_giga
16
- merged_data = pd.merge(
17
- lat_long_data,
18
- measurement_data,
19
- left_on="school_id_giga",
20
- right_on="school_id_giga",
21
- how="inner"
22
- )
23
-
24
- # Strip any extra spaces from column names
25
- merged_data.columns = merged_data.columns.str.strip()
26
-
27
- return merged_data
 
 
 
 
 
 
 
 
28
 
29
  # Perform clustering to find data center location
30
  def find_data_center(df, n_clusters=1):
 
 
 
31
  kmeans = KMeans(n_clusters=n_clusters, random_state=0).fit(df[["latitude", "longitude"]])
32
  return kmeans.cluster_centers_
33
 
34
  # Plot the map with markers
35
  def plot_map(df, center):
36
- # Print column names for debugging
37
- print(df.columns)
 
38
 
39
  map = folium.Map(location=[center[0][0], center[0][1]], zoom_start=10)
40
  marker_cluster = MarkerCluster().add_to(map)
@@ -63,7 +75,9 @@ def plot_map(df, center):
63
 
64
  # Calculate the impact of data center on latency and bandwidth
65
  def calculate_impact(df, center):
66
- # Calculate average latency and bandwidth before the data center
 
 
67
  avg_latency_before = df['latency'].mean()
68
  avg_download_before = df['download_speed'].mean()
69
  avg_upload_before = df['upload_speed'].mean()
@@ -98,20 +112,27 @@ def main():
98
  url = "https://huggingface.co/spaces/engralimalik/lace/resolve/main/data%20barbados.xlsx" # URL of your Excel file
99
  df = load_data(url)
100
 
 
 
 
 
101
  # Find the data center location using clustering
102
  center = find_data_center(df)
 
 
 
103
 
104
  # Create the map and save it
105
  map = plot_map(df, center)
106
- map.save("index.html")
 
107
 
108
  # Calculate the impact of adding the data center
109
  latency_reduction, download_increase, upload_increase, avg_latency_before, avg_download_before, avg_upload_before = calculate_impact(df, center)
110
-
111
- # Display the impact
112
- impact_data = display_impact(latency_reduction, download_increase, upload_increase, avg_latency_before, avg_download_before, avg_upload_before)
113
- print("Impact of Data Center on Latency and Bandwidth:")
114
- print(impact_data)
115
 
116
  print("Map has been saved as index.html.")
117
 
 
5
  import requests
6
  from io import BytesIO
7
 
8
+ # Load data from Excel URL with error handling
9
  def load_data(url):
10
+ try:
11
+ print("Loading data from:", url)
12
+ response = requests.get(url)
13
+ if response.status_code == 200:
14
+ lat_long_data = pd.read_excel(BytesIO(response.content), sheet_name="lat long", engine='openpyxl')
15
+ measurement_data = pd.read_excel(BytesIO(response.content), sheet_name="measurement data", engine='openpyxl')
16
+
17
+ # Merge data on school_id_giga
18
+ merged_data = pd.merge(
19
+ lat_long_data,
20
+ measurement_data,
21
+ left_on="school_id_giga",
22
+ right_on="school_id_giga",
23
+ how="inner"
24
+ )
25
+
26
+ # Strip any extra spaces from column names
27
+ merged_data.columns = merged_data.columns.str.strip()
28
+ print("Data loaded successfully")
29
+ return merged_data
30
+ else:
31
+ print(f"Failed to load data. Status code: {response.status_code}")
32
+ return pd.DataFrame()
33
+ except Exception as e:
34
+ print(f"Error loading data: {e}")
35
+ return pd.DataFrame()
36
 
37
  # Perform clustering to find data center location
38
  def find_data_center(df, n_clusters=1):
39
+ if df.empty:
40
+ print("Dataframe is empty, skipping clustering")
41
+ return None
42
  kmeans = KMeans(n_clusters=n_clusters, random_state=0).fit(df[["latitude", "longitude"]])
43
  return kmeans.cluster_centers_
44
 
45
  # Plot the map with markers
46
  def plot_map(df, center):
47
+ if df.empty:
48
+ print("Dataframe is empty, skipping map plotting")
49
+ return None
50
 
51
  map = folium.Map(location=[center[0][0], center[0][1]], zoom_start=10)
52
  marker_cluster = MarkerCluster().add_to(map)
 
75
 
76
  # Calculate the impact of data center on latency and bandwidth
77
  def calculate_impact(df, center):
78
+ if df.empty:
79
+ print("Dataframe is empty, skipping impact calculation")
80
+ return None
81
  avg_latency_before = df['latency'].mean()
82
  avg_download_before = df['download_speed'].mean()
83
  avg_upload_before = df['upload_speed'].mean()
 
112
  url = "https://huggingface.co/spaces/engralimalik/lace/resolve/main/data%20barbados.xlsx" # URL of your Excel file
113
  df = load_data(url)
114
 
115
+ if df.empty:
116
+ print("No data to process, exiting application.")
117
+ return
118
+
119
  # Find the data center location using clustering
120
  center = find_data_center(df)
121
+ if center is None:
122
+ print("Could not find data center, exiting application.")
123
+ return
124
 
125
  # Create the map and save it
126
  map = plot_map(df, center)
127
+ if map:
128
+ map.save("index.html")
129
 
130
  # Calculate the impact of adding the data center
131
  latency_reduction, download_increase, upload_increase, avg_latency_before, avg_download_before, avg_upload_before = calculate_impact(df, center)
132
+ if latency_reduction is not None:
133
+ impact_data = display_impact(latency_reduction, download_increase, upload_increase, avg_latency_before, avg_download_before, avg_upload_before)
134
+ print("Impact of Data Center on Latency and Bandwidth:")
135
+ print(impact_data)
 
136
 
137
  print("Map has been saved as index.html.")
138