Skip to content

Commit af23643

Browse files
committed
fix: Resolve Chart.js loading and optimize algorithmic performance
- Remove incorrect SRI hashes for Chart.js and JSON Formatter - Fix 'Chart is not defined' error in time series visualization - Optimize list.index() lookups with O(1) dictionary lookups - Improve performance from O(n³) to O(n²) in neighbor calculations - Prevent algorithmic DoS on large datasets - All tests passing with identical results
1 parent 8c2d3db commit af23643

7 files changed

Lines changed: 105 additions & 6 deletions

File tree

examples/DISTPOL2021_MSP_CEM_V1_2/ehsa_df.csv

Lines changed: 94 additions & 0 deletions
Large diffs are not rendered by default.
0 Bytes
Binary file not shown.
265 Bytes
Binary file not shown.
225 Bytes
Binary file not shown.

src/pyehsa/gi_star.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,12 +114,15 @@ def _create_spacetime_neighbors_r_style(w, n_times, n_locs, k):
114114
Create spacetime neighbors list exactly matching R's spt_nb function.
115115
"""
116116
# Get base spatial neighbors from weights object
117+
# Performance optimization: Create lookup dictionary to avoid repeated list.index() calls
118+
id_to_idx = {loc_id: idx for idx, loc_id in enumerate(w.id_order)}
119+
117120
nb_base = []
118121
for i in range(n_locs):
119122
region_id = w.id_order[i]
120123
neighbors = w.neighbors[region_id]
121-
# Convert neighbor IDs to indices
122-
neighbor_indices = [w.id_order.index(nid) for nid in neighbors]
124+
# Convert neighbor IDs to indices using O(1) dictionary lookup
125+
neighbor_indices = [id_to_idx[nid] for nid in neighbors]
123126
nb_base.append(neighbor_indices)
124127

125128
# Create spacetime neighbors list

src/pyehsa/spacial_weights.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ def create_spacetime_neighbors_and_weights(w, n_times, n_locs, k=1):
6262
spt_neighbors = {}
6363
spt_weights = {}
6464

65+
# Performance optimization: Create lookup dictionary to avoid repeated list.index() calls
66+
# This changes O(n) lookup to O(1), preventing algorithmic DoS on large datasets
67+
id_to_idx = {loc_id: idx for idx, loc_id in enumerate(w.id_order)}
68+
6569
# For each time period and location, create neighbors including time lags
6670
for t in range(n_times):
6771
for i, loc_id in enumerate(w.id_order):
@@ -73,7 +77,7 @@ def create_spacetime_neighbors_and_weights(w, n_times, n_locs, k=1):
7377

7478
# Add spatial neighbors from current time period
7579
for j, neighbor_id in enumerate(w.neighbors[loc_id]):
76-
neighbor_loc_idx = w.id_order.index(neighbor_id)
80+
neighbor_loc_idx = id_to_idx[neighbor_id] # O(1) lookup instead of O(n)
7781
neighbor_st_idx = t * n_locs + neighbor_loc_idx
7882
neighbors_list.append(neighbor_st_idx)
7983
weights_list.append(w.weights[loc_id][j])
@@ -83,7 +87,7 @@ def create_spacetime_neighbors_and_weights(w, n_times, n_locs, k=1):
8387
if t >= lag: # Only if we have enough previous time periods
8488
lag_t = t - lag
8589
for j, neighbor_id in enumerate(w.neighbors[loc_id]):
86-
neighbor_loc_idx = w.id_order.index(neighbor_id)
90+
neighbor_loc_idx = id_to_idx[neighbor_id] # O(1) lookup instead of O(n)
8791
neighbor_st_idx = lag_t * n_locs + neighbor_loc_idx
8892
neighbors_list.append(neighbor_st_idx)
8993
weights_list.append(w.weights[loc_id][j])

src/pyehsa/visualization/ehsa_visualization.html

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,10 @@
1818

1919
<!-- Chart.js -->
2020
<script src="https://cdn.jsdelivr.net/npm/chart.js@4.4.0/dist/chart.umd.min.js"
21-
integrity="sha384-NzCFDHso5NkdJ7VJqDg5QFd5rRUEKQDPPM8VBZw7pqP7G3yMHmNiW8YdRmNPTRJo"
2221
crossorigin="anonymous"></script>
2322

2423
<!-- JSON Formatter -->
2524
<script src="https://cdn.jsdelivr.net/npm/json-formatter-js@2.3.4/dist/json-formatter.umd.min.js"
26-
integrity="sha384-rquKbDwJ1P7qCGJAQo9bjIkLKBmLlBHQiGHQvPzKMCKw8fKpJ5R7fAQ9LGMqKG+x"
2725
crossorigin="anonymous"></script>
2826

2927
<style>

0 commit comments

Comments
 (0)