-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate_security_cache.py
More file actions
464 lines (376 loc) Β· 16.9 KB
/
Copy pathgenerate_security_cache.py
File metadata and controls
464 lines (376 loc) Β· 16.9 KB
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
"""
Security & Integrity Cache Generator
Pre-generates all visualizations for Page 5 (Security & Integrity) and saves to JSON cache.
Run this script whenever data is updated to refresh the security cache.
Usage:
python generate_security_cache.py
"""
import pandas as pd
import numpy as np
import json
import os
import glob
import math
from datetime import datetime
from data_preprocessing import preprocess_dataframe
import plotly
import plotly.express as px
import plotly.graph_objects as go
import warnings
warnings.filterwarnings('ignore')
# ==========================================
# SECURITY ANALYTICS FUNCTIONS
# ==========================================
def get_benfords_law_global(df):
"""Global Benford's Law analysis across all data"""
# Get all age columns
age_cols = [col for col in df.columns if col.startswith('age_')]
# Calculate total activity per record
df['total_activity'] = df[age_cols].sum(axis=1)
# Get leading digits (excluding zeros)
data = df[df['total_activity'] > 0]['total_activity']
if len(data) < 10:
return {"error": "Insufficient data"}
# Extract first digit
first_digits = data.astype(str).str[0].astype(int)
actual_freq = first_digits.value_counts(normalize=True).sort_index()
# Benford's theoretical distribution
theoretical_freq = pd.Series({d: math.log10(1 + 1/d) for d in range(1, 10)})
# Combine for plotting
results = pd.DataFrame({
'digit': range(1, 10),
'actual_freq': [actual_freq.get(d, 0) for d in range(1, 10)],
'benford_freq': [theoretical_freq.get(d, 0) for d in range(1, 10)]
})
# Calculate deviation score
deviation = abs(results['actual_freq'] - results['benford_freq']).sum()
return {
'distribution': results,
'deviation_score': float(deviation),
'is_suspicious': bool(deviation > 0.15)
}
def get_statistical_outliers(df):
"""Detect anomalous activity spikes over time"""
age_cols = [col for col in df.columns if col.startswith('age_')]
# Daily aggregation
daily = df.groupby('date')[age_cols].sum().sum(axis=1).reset_index()
daily.columns = ['date', 'total_activity']
if len(daily) < 3:
return pd.DataFrame()
# Calculate statistical thresholds
mean = daily['total_activity'].mean()
std = daily['total_activity'].std()
threshold = mean + (2.5 * std)
daily['is_anomaly'] = daily['total_activity'] > threshold
daily['threshold'] = threshold
return daily
def get_volatility_analysis(df, top_n=20):
"""Analyze centers with erratic/inconsistent behavior"""
age_cols = [col for col in df.columns if col.startswith('age_')]
# Calculate variance score per pincode over time
pincode_stats = df.groupby('pincode').agg({
age_cols[0]: ['mean', 'std', 'count']
}).reset_index()
pincode_stats.columns = ['pincode', 'mean', 'std', 'count']
# Calculate coefficient of variation
pincode_stats['variance_score'] = (pincode_stats['std'] / pincode_stats['mean']).fillna(0)
# Filter centers with sufficient data and sort by variance
erratic = pincode_stats[pincode_stats['count'] > 5].sort_values('variance_score', ascending=False).head(top_n)
return erratic
def get_state_variance_data(df):
"""Analyze consistency across states using box plots"""
age_cols = [col for col in df.columns if col.startswith('age_')]
# Calculate activity per district
state_data = df.groupby(['state', 'district'])[age_cols].sum().sum(axis=1).reset_index()
state_data.columns = ['state', 'district', 'total_activity']
return state_data
def create_benford_chart(benford_data):
"""Create Benford's Law Plotly chart"""
df_ben = benford_data['distribution']
fig_ben = go.Figure()
fig_ben.add_trace(go.Bar(
x=df_ben['digit'],
y=df_ben['actual_freq'],
name='Your Data (Actual)',
marker_color='royalblue'
))
fig_ben.add_trace(go.Scatter(
x=df_ben['digit'],
y=df_ben['benford_freq'],
mode='lines+markers',
name="Benford's Law (Theoretical)",
line={'color': 'red', 'width': 3}
))
fig_ben.update_layout(
xaxis_title="Leading Digit",
yaxis_title="Relative Frequency",
template="plotly_white",
height=500,
xaxis={'tickmode': 'linear', 'tick0': 1, 'dtick': 1}
)
return fig_ben
def create_outliers_chart(outliers_data):
"""Create Statistical Outliers Plotly chart"""
anomaly_count = outliers_data['is_anomaly'].sum()
fig_outliers = px.scatter(
outliers_data,
x='date',
y='total_activity',
color='is_anomaly',
color_discrete_map={True: 'red', False: 'gray'},
labels={'total_activity': 'Daily Volume', 'is_anomaly': 'Suspicious Spike'},
template="plotly_white",
height=500
)
# Add trend line
fig_outliers.add_trace(go.Scatter(
x=outliers_data['date'],
y=outliers_data['total_activity'],
mode='lines',
line={'color': 'lightgray', 'width': 1},
showlegend=False,
hoverinfo='skip'
))
# Add threshold line
if 'threshold' in outliers_data.columns:
fig_outliers.add_hline(
y=outliers_data['threshold'].iloc[0],
line_dash="dash",
line_color="orange",
annotation_text="Anomaly Threshold"
)
return fig_outliers, int(anomaly_count)
def create_volatility_chart(volatility_data):
"""Create Volatility Analysis Plotly chart"""
fig_volatility = px.bar(
volatility_data,
x='pincode',
y='variance_score',
color='variance_score',
color_continuous_scale='Reds',
labels={'pincode': 'Pincode', 'variance_score': 'Erratic Behavior Score'},
template="plotly_white",
height=500
)
fig_volatility.update_xaxes(tickangle=45)
return fig_volatility
def create_state_variance_chart(state_variance):
"""Create State Variance Box Plot"""
fig_variance = px.box(
state_variance,
x='state',
y='total_activity',
color='state',
points="outliers",
labels={'total_activity': 'Activity Level', 'state': 'State Name'},
template="plotly_white",
height=600
)
fig_variance.update_xaxes(tickangle=45)
return fig_variance
# ==========================================
# DATA LOADING & CACHE GENERATION
# ==========================================
def read_all_csvs_in_folder(folder_name):
"""Finds all .csv files in a folder and merges them into one DataFrame."""
file_paths = glob.glob(os.path.join(folder_name, "*.csv"))
if not file_paths:
print(f"β οΈ No CSV files found in {folder_name}")
return pd.DataFrame()
df_list = []
for file in file_paths:
try:
df_chunk = pd.read_csv(file, low_memory=False)
df_list.append(df_chunk)
print(f" β Loaded {file}")
except Exception as e:
print(f" β Error loading {file}: {e}")
if df_list:
combined_df = pd.concat(df_list, ignore_index=True)
print(f" β Combined: {len(combined_df):,} rows")
return combined_df
else:
return pd.DataFrame()
def generate_security_cache():
"""Generate all security analytics and save to JSON cache"""
print("=" * 60)
print("π SECURITY & INTEGRITY CACHE GENERATOR")
print("=" * 60)
# Load enrollment data
print("\nπ Loading enrollment data...")
df_enrol = read_all_csvs_in_folder("api_data_aadhar_enrolment")
if df_enrol.empty:
print("β Error: No enrollment data found!")
return
print(f"β Loaded {len(df_enrol):,} enrollment records")
# Apply preprocessing
print("\nπ§ Preprocessing data...")
df_enrol = preprocess_dataframe(df_enrol, "Enrollment")
print("β Preprocessing complete")
# Initialize cache dictionary
security_cache = {}
# ==========================================
# 1. BENFORD'S LAW ANALYSIS
# ==========================================
print("\nπ Analyzing Benford's Law distribution...")
try:
benford_data = get_benfords_law_global(df_enrol)
if "error" not in benford_data:
# Save data as serializable format
benford_dict = {
'digits': benford_data['distribution']['digit'].tolist(),
'actual_freq': benford_data['distribution']['actual_freq'].tolist(),
'benford_freq': benford_data['distribution']['benford_freq'].tolist(),
'deviation_score': benford_data['deviation_score'],
'is_suspicious': benford_data['is_suspicious']
}
security_cache['benford_data'] = benford_dict
# Create and save Plotly chart
fig_benford = create_benford_chart(benford_data)
security_cache['benford_chart'] = plotly.io.to_json(fig_benford)
status = "SUSPICIOUS" if benford_data['is_suspicious'] else "NORMAL"
print(f" β Benford's Law analysis complete: {status} (deviation: {benford_data['deviation_score']:.3f})")
else:
print(" β οΈ Insufficient data for Benford's Law analysis")
security_cache['benford_data'] = {}
security_cache['benford_chart'] = None
except Exception as e:
print(f" β Error in Benford's Law analysis: {e}")
security_cache['benford_data'] = {}
security_cache['benford_chart'] = None
# ==========================================
# 2. STATISTICAL OUTLIERS
# ==========================================
print("\nπ Detecting statistical outliers...")
try:
outliers_data = get_statistical_outliers(df_enrol)
if not outliers_data.empty:
# Save data as serializable format
outliers_dict = {
'dates': outliers_data['date'].astype(str).tolist(),
'total_activity': outliers_data['total_activity'].tolist(),
'is_anomaly': outliers_data['is_anomaly'].tolist(),
'threshold': outliers_data['threshold'].tolist()
}
security_cache['outliers_data'] = outliers_dict
# Create and save Plotly chart
fig_outliers, anomaly_count = create_outliers_chart(outliers_data)
security_cache['outliers_chart'] = plotly.io.to_json(fig_outliers)
security_cache['anomaly_count'] = anomaly_count
print(f" β Outlier detection complete: {anomaly_count} suspicious days detected")
else:
print(" β οΈ Insufficient time-series data for outlier detection")
security_cache['outliers_data'] = {}
security_cache['outliers_chart'] = None
security_cache['anomaly_count'] = 0
except Exception as e:
print(f" β Error in outlier detection: {e}")
security_cache['outliers_data'] = {}
security_cache['outliers_chart'] = None
security_cache['anomaly_count'] = 0
# ==========================================
# 3. VOLATILITY ANALYSIS
# ==========================================
print("\nπ Analyzing center volatility...")
try:
volatility_data = get_volatility_analysis(df_enrol, top_n=20)
if not volatility_data.empty:
# Save data as serializable format
volatility_dict = {
'pincodes': volatility_data['pincode'].astype(str).tolist(),
'mean': volatility_data['mean'].tolist(),
'std': volatility_data['std'].tolist(),
'count': volatility_data['count'].astype(int).tolist(),
'variance_score': volatility_data['variance_score'].tolist()
}
security_cache['volatility_data'] = volatility_dict
# Create and save Plotly chart
fig_volatility = create_volatility_chart(volatility_data)
security_cache['volatility_chart'] = plotly.io.to_json(fig_volatility)
print(f" β Volatility analysis complete: {len(volatility_data)} most erratic centers identified")
else:
print(" β οΈ Insufficient data for volatility analysis")
security_cache['volatility_data'] = {}
security_cache['volatility_chart'] = None
except Exception as e:
print(f" β Error in volatility analysis: {e}")
security_cache['volatility_data'] = {}
security_cache['volatility_chart'] = None
# ==========================================
# 4. STATE VARIANCE ANALYSIS
# ==========================================
print("\nπΊοΈ Analyzing state variance...")
try:
state_variance = get_state_variance_data(df_enrol)
if not state_variance.empty and len(state_variance['state'].unique()) > 1:
# Save data as serializable format
state_dict = {
'states': state_variance['state'].tolist(),
'districts': state_variance['district'].tolist(),
'total_activity': state_variance['total_activity'].tolist()
}
security_cache['state_variance_data'] = state_dict
# Create and save Plotly chart
fig_variance = create_state_variance_chart(state_variance)
security_cache['state_variance_chart'] = plotly.io.to_json(fig_variance)
print(f" β State variance analysis complete: {len(state_variance['state'].unique())} states analyzed")
else:
print(" β οΈ Insufficient cross-state data for variance analysis")
security_cache['state_variance_data'] = {}
security_cache['state_variance_chart'] = None
except Exception as e:
print(f" β Error in state variance analysis: {e}")
security_cache['state_variance_data'] = {}
security_cache['state_variance_chart'] = None
# ==========================================
# SAVE TO CACHE FILE
# ==========================================
cache_file = 'security_cache.json'
# Add metadata
security_cache['metadata'] = {
'generated_at': datetime.now().isoformat(),
'total_records': len(df_enrol),
'date_range': {
'start': df_enrol['date'].min().isoformat() if 'date' in df_enrol.columns else None,
'end': df_enrol['date'].max().isoformat() if 'date' in df_enrol.columns else None
},
'districts_analyzed': int(df_enrol['district'].nunique()) if 'district' in df_enrol.columns else 0,
'states_analyzed': int(df_enrol['state'].nunique()) if 'state' in df_enrol.columns else 0,
'pincodes_analyzed': int(df_enrol['pincode'].nunique()) if 'pincode' in df_enrol.columns else 0
}
print("\nπΎ Saving cache to file...")
try:
with open(cache_file, 'w') as f:
json.dump(security_cache, f, indent=2)
file_size = os.path.getsize(cache_file) / (1024 * 1024) # MB
print(f" β Cache saved: {cache_file} ({file_size:.2f} MB)")
except Exception as e:
print(f" β Error saving cache: {e}")
return
# ==========================================
# SUMMARY
# ==========================================
print("\n" + "=" * 60)
print("β
SECURITY CACHE GENERATION COMPLETE!")
print("=" * 60)
print("π Cached Items:")
print(f" β’ Benford's Law Analysis: {'β' if security_cache.get('benford_data') else 'β'}")
print(f" β’ Statistical Outliers: {'β' if security_cache.get('outliers_data') else 'β'}")
print(f" β’ Volatility Analysis: {'β' if security_cache.get('volatility_data') else 'β'}")
print(f" β’ State Variance Analysis: {'β' if security_cache.get('state_variance_data') else 'β'}")
print(f"\nπ Cache file: {cache_file}")
print(f"π
Generated: {security_cache['metadata']['generated_at']}")
print(f"π Records analyzed: {security_cache['metadata']['total_records']:,}")
print(f"ποΈ Districts: {security_cache['metadata']['districts_analyzed']}")
print(f"πΊοΈ States: {security_cache['metadata']['states_analyzed']}")
print(f"π Pincodes: {security_cache['metadata']['pincodes_analyzed']}")
if security_cache.get('benford_data'):
status = "β οΈ SUSPICIOUS" if security_cache['benford_data']['is_suspicious'] else "β NORMAL"
print(f"\nπ Fraud Status: {status}")
print(f" Deviation Score: {security_cache['benford_data']['deviation_score']:.3f}")
if security_cache.get('anomaly_count', 0) > 0:
print(f"\nπ¨ Anomalies Detected: {security_cache['anomaly_count']} suspicious activity days")
print("\nπ‘ Next step: Run your Streamlit app to see the cached visualizations!")
print("=" * 60)
if __name__ == "__main__":
generate_security_cache()