-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathintegration.py
More file actions
606 lines (544 loc) · 23.2 KB
/
integration.py
File metadata and controls
606 lines (544 loc) · 23.2 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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
#!/usr/bin/env python3
"""
Integration module for Fraud Detection MCP with Synthetic Data MCP
Provides seamless data generation and analysis pipeline for stored data fraud detection
"""
import logging
from datetime import datetime, timedelta
from typing import Dict, List, Any
import pandas as pd
import numpy as np
from pathlib import Path
# Setup logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
class SyntheticDataIntegration:
"""Integration with synthetic-data-mcp for fraud detection testing"""
def __init__(self):
import os
base_dir = os.environ.get(
"FRAUD_DETECTION_DATA_DIR", str(Path(__file__).parent / "test_data")
)
self.output_dir = Path(base_dir)
self.output_dir.mkdir(exist_ok=True)
def generate_fraud_patterns(self) -> Dict[str, Any]:
"""Generate comprehensive fraud patterns for synthetic data creation"""
fraud_patterns = {
"transaction_fraud": {
"high_amount_patterns": {
"description": "Unusually high transaction amounts",
"amount_ranges": [(5000, 50000), (50000, 100000), (100000, 500000)],
"frequency": 0.02, # 2% of transactions
"merchants": [
"Electronics Warehouse",
"Jewelry Store",
"Cash Advance",
],
"risk_indicators": ["amount_anomaly", "merchant_risk"],
},
"velocity_fraud": {
"description": "Multiple transactions in short time periods",
"pattern": "burst",
"transaction_count": (5, 20),
"time_window": (60, 300), # 1-5 minutes
"amount_range": (100, 2000),
"frequency": 0.01,
"risk_indicators": ["velocity_anomaly", "time_clustering"],
},
"geographic_anomaly": {
"description": "Transactions from unusual locations",
"high_risk_locations": [
"Unknown Location",
"High Risk Country A",
"Offshore Territory",
"Sanctioned Region",
],
"frequency": 0.03,
"risk_indicators": ["geographic_risk", "location_anomaly"],
},
"temporal_anomaly": {
"description": "Transactions at unusual times",
"unusual_hours": [2, 3, 4, 5], # 2 AM - 5 AM
"unusual_days": ["Sunday", "Holiday"],
"frequency": 0.015,
"risk_indicators": ["time_anomaly", "schedule_deviation"],
},
"payment_method_fraud": {
"description": "High-risk payment methods",
"high_risk_methods": ["crypto", "prepaid_card", "money_order"],
"frequency": 0.025,
"risk_indicators": ["payment_method_risk"],
},
},
"behavioral_fraud": {
"keystroke_anomalies": {
"description": "Abnormal typing patterns indicating account takeover",
"patterns": {
"too_slow": {
"dwell_time_multiplier": 3.0,
"flight_time_multiplier": 2.5,
},
"too_fast": {
"dwell_time_multiplier": 0.3,
"flight_time_multiplier": 0.4,
},
"irregular": {
"variation_multiplier": 5.0,
"consistency_factor": 0.2,
},
},
"frequency": 0.008,
"risk_indicators": ["keystroke_anomaly", "behavioral_deviation"],
},
"session_anomalies": {
"description": "Unusual session behavior patterns",
"patterns": {
"too_quick": {"session_duration": (5, 30)}, # Seconds
"unusual_navigation": {
"page_jumps": True,
"back_button_abuse": True,
},
"copy_paste_heavy": {"copy_paste_ratio": 0.8},
},
"frequency": 0.012,
"risk_indicators": ["session_anomaly", "navigation_risk"],
},
},
"network_fraud": {
"fraud_rings": {
"description": "Coordinated fraud networks",
"ring_sizes": [3, 5, 8, 12, 20],
"connection_patterns": ["star", "mesh", "chain"],
"transaction_patterns": {
"money_laundering": {
"circular_transfers": True,
"amount_structuring": True,
},
"account_farming": {
"new_accounts": True,
"similar_patterns": True,
},
},
"frequency": 0.005,
"risk_indicators": ["network_clustering", "coordinated_activity"],
},
"synthetic_identity": {
"description": "Fake identity creation and usage",
"identity_indicators": {
"new_account": True,
"minimal_history": True,
"inconsistent_data": True,
},
"frequency": 0.007,
"risk_indicators": ["identity_risk", "new_account_risk"],
},
},
}
return fraud_patterns
def create_dataset_schema(self) -> Dict[str, Any]:
"""Create standardized schema for fraud detection datasets"""
schema = {
"transaction_data": {
"required_fields": [
"transaction_id",
"user_id",
"amount",
"merchant",
"merchant_category",
"location",
"timestamp",
"payment_method",
],
"optional_fields": [
"device_id",
"ip_address",
"user_agent",
"geolocation_lat",
"geolocation_lon",
"merchant_id",
"card_type",
"currency",
],
"data_types": {
"transaction_id": "string",
"user_id": "string",
"amount": "float",
"merchant": "string",
"location": "string",
"timestamp": "datetime",
"payment_method": "categorical",
},
},
"behavioral_data": {
"keystroke_dynamics": {
"fields": [
"key",
"press_time",
"release_time",
"user_id",
"session_id",
],
"data_types": {
"key": "string",
"press_time": "integer",
"release_time": "integer",
"user_id": "string",
"session_id": "string",
},
},
"session_data": {
"fields": [
"session_id",
"user_id",
"start_time",
"end_time",
"pages_visited",
"actions_taken",
"form_interactions",
"copy_paste_events",
"idle_time",
]
},
},
"network_data": {
"fields": [
"entity_id",
"entity_type",
"connected_entities",
"relationship_type",
"relationship_strength",
"transaction_count",
"total_amount",
]
},
"labels": {
"is_fraud": "boolean",
"fraud_type": "categorical",
"fraud_confidence": "float",
"manual_review": "boolean",
},
}
return schema
def generate_comprehensive_test_dataset(
self,
num_transactions: int = 10000,
fraud_percentage: float = 5.0,
include_behavioral: bool = True,
include_network: bool = True,
output_format: str = "csv",
) -> Dict[str, Any]:
"""Generate comprehensive test dataset with all fraud patterns"""
try:
fraud_patterns = self.generate_fraud_patterns()
schema = self.create_dataset_schema()
# Calculate distribution
num_fraud = int(num_transactions * fraud_percentage / 100)
num_legitimate = num_transactions - num_fraud
transactions = []
behavioral_data = []
network_data = []
# Generate legitimate transactions
for i in range(num_legitimate):
transaction = self._generate_legitimate_transaction(i)
transactions.append(transaction)
if include_behavioral:
behavioral = self._generate_normal_behavioral_data(
transaction["user_id"]
)
behavioral_data.extend(behavioral)
# Generate fraudulent transactions with specific patterns
fraud_types = list(fraud_patterns["transaction_fraud"].keys())
for i in range(num_fraud):
fraud_type = np.random.choice(np.array(fraud_types))
pattern = fraud_patterns["transaction_fraud"][fraud_type]
transaction = self._generate_fraudulent_transaction(
i + num_legitimate, fraud_type, pattern
)
transactions.append(transaction)
if include_behavioral:
behavioral = self._generate_anomalous_behavioral_data(
transaction["user_id"], fraud_type
)
behavioral_data.extend(behavioral)
# Generate network connections if requested
if include_network:
network_data = self._generate_network_connections(
transactions, fraud_patterns
)
# Create DataFrames
transactions_df = pd.DataFrame(transactions)
# Save datasets
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
base_filename = f"fraud_test_dataset_{timestamp}"
# Save transaction data
if output_format == "csv":
transactions_path = (
self.output_dir / f"{base_filename}_transactions.csv"
)
transactions_df.to_csv(transactions_path, index=False)
else:
transactions_path = (
self.output_dir / f"{base_filename}_transactions.json"
)
transactions_df.to_json(transactions_path, orient="records", indent=2)
# Save behavioral data if generated
behavioral_path = None
if include_behavioral and behavioral_data:
behavioral_df = pd.DataFrame(behavioral_data)
if output_format == "csv":
behavioral_path = (
self.output_dir / f"{base_filename}_behavioral.csv"
)
behavioral_df.to_csv(behavioral_path, index=False)
else:
behavioral_path = (
self.output_dir / f"{base_filename}_behavioral.json"
)
behavioral_df.to_json(behavioral_path, orient="records", indent=2)
# Save network data if generated
network_path = None
if include_network and network_data:
network_df = pd.DataFrame(network_data)
if output_format == "csv":
network_path = self.output_dir / f"{base_filename}_network.csv"
network_df.to_csv(network_path, index=False)
else:
network_path = self.output_dir / f"{base_filename}_network.json"
network_df.to_json(network_path, orient="records", indent=2)
# Generate statistics
fraud_distribution = transactions_df["fraud_type"].value_counts().to_dict()
result = {
"generation_info": {
"total_transactions": num_transactions,
"legitimate_transactions": num_legitimate,
"fraudulent_transactions": num_fraud,
"fraud_percentage": fraud_percentage,
"generation_timestamp": datetime.now().isoformat(),
"includes_behavioral": include_behavioral,
"includes_network": include_network,
},
"dataset_paths": {
"transactions": str(transactions_path),
"behavioral": str(behavioral_path) if behavioral_path else None,
"network": str(network_path) if network_path else None,
},
"fraud_distribution": fraud_distribution,
"schema_compliance": self._validate_schema_compliance(
transactions_df, schema
),
"integration_status": "success",
"ready_for_analysis": True,
}
return result
except Exception as e:
logger.error(f"Comprehensive dataset generation failed: {e}")
return {
"error": str(e),
"status": "generation_failed",
"integration_status": "error",
}
def _generate_legitimate_transaction(self, index: int) -> Dict[str, Any]:
"""Generate a legitimate transaction"""
return {
"transaction_id": f"legit_{index:08d}",
"user_id": f"user_{np.random.randint(1000, 9999)}",
"amount": round(
np.random.lognormal(4.0, 1.0), 2
), # Log-normal distribution
"merchant": np.random.choice(
[
"Grocery Store",
"Gas Station",
"Coffee Shop",
"Restaurant",
"Pharmacy",
"Department Store",
"Online Retailer",
]
),
"merchant_category": "retail",
"location": np.random.choice(
[
"New York, NY",
"Los Angeles, CA",
"Chicago, IL",
"Houston, TX",
"Phoenix, AZ",
"Philadelphia, PA",
"San Antonio, TX",
]
),
"timestamp": (
datetime.now() - timedelta(days=np.random.randint(0, 365))
).isoformat(),
"payment_method": np.random.choice(
np.array(["credit_card", "debit_card", "bank_transfer"])
),
"is_fraud": False,
"fraud_type": "none",
"fraud_confidence": 0.0,
}
def _generate_fraudulent_transaction(
self, index: int, fraud_type: str, pattern: Dict[str, Any]
) -> Dict[str, Any]:
"""Generate a fraudulent transaction with specific pattern"""
base_transaction = {
"transaction_id": f"fraud_{index:08d}",
"user_id": f"user_{np.random.randint(1000, 9999)}",
"amount": 100.0, # Default, will be overridden
"merchant": "Suspicious Merchant",
"merchant_category": "high_risk",
"location": "Unknown Location",
"timestamp": datetime.now().isoformat(),
"payment_method": "credit_card",
"is_fraud": True,
"fraud_type": fraud_type,
"fraud_confidence": 0.9,
}
# Apply specific fraud pattern
if fraud_type == "high_amount_patterns":
amount_range = pattern["amount_ranges"][
np.random.randint(0, len(pattern["amount_ranges"]))
]
base_transaction["amount"] = round(np.random.uniform(*amount_range), 2)
base_transaction["merchant"] = np.random.choice(
np.array(pattern["merchants"])
)
elif fraud_type == "velocity_fraud":
base_transaction["amount"] = round(
np.random.uniform(*pattern["amount_range"]), 2
)
# Simulate burst timing
base_transaction["timestamp"] = (
datetime.now() - timedelta(seconds=np.random.randint(60, 300))
).isoformat()
elif fraud_type == "geographic_anomaly":
base_transaction["location"] = np.random.choice(
np.array(pattern["high_risk_locations"])
)
elif fraud_type == "temporal_anomaly":
unusual_hour = np.random.choice(np.array(pattern["unusual_hours"]))
anomalous_time = datetime.now().replace(
hour=unusual_hour, minute=np.random.randint(0, 59)
)
base_transaction["timestamp"] = anomalous_time.isoformat()
elif fraud_type == "payment_method_fraud":
base_transaction["payment_method"] = np.random.choice(
pattern["high_risk_methods"]
)
return base_transaction
def _generate_normal_behavioral_data(self, user_id: str) -> List[Dict[str, Any]]:
"""Generate normal behavioral patterns"""
behavioral_data = []
# Normal keystroke patterns
base_dwell = 80 # milliseconds
for i, key in enumerate("password123"):
press_time = 1000 + i * 150 + np.random.normal(0, 20)
dwell_time = base_dwell + np.random.normal(0, 15)
behavioral_data.append(
{
"user_id": user_id,
"session_id": f"session_{user_id}",
"key": key,
"press_time": int(press_time),
"release_time": int(press_time + dwell_time),
"is_anomaly": False,
}
)
return behavioral_data
def _generate_anomalous_behavioral_data(
self, user_id: str, fraud_type: str
) -> List[Dict[str, Any]]:
"""Generate anomalous behavioral patterns"""
behavioral_data = []
# Anomalous keystroke patterns (account takeover simulation)
if fraud_type in ["high_amount_patterns", "velocity_fraud"]:
# Simulate different user typing
base_dwell = 150 # Much slower
for i, key in enumerate("password123"):
press_time = 1000 + i * 400 + np.random.normal(0, 100) # More variation
dwell_time = base_dwell + np.random.normal(0, 50)
behavioral_data.append(
{
"user_id": user_id,
"session_id": f"session_{user_id}",
"key": key,
"press_time": int(press_time),
"release_time": int(press_time + dwell_time),
"is_anomaly": True,
}
)
return behavioral_data
def _generate_network_connections(
self, transactions: List[Dict[str, Any]], fraud_patterns: Dict[str, Any]
) -> List[Dict[str, Any]]:
"""Generate network connection data"""
network_data = []
# Create user networks
users = list(set(t["user_id"] for t in transactions))
fraud_users = [t["user_id"] for t in transactions if t["is_fraud"]]
# Create fraud rings
if len(fraud_users) >= 3:
# Group fraud users into rings
ring_size = min(5, len(fraud_users))
fraud_ring = fraud_users[:ring_size]
# Create connections within the ring
for i, user1 in enumerate(fraud_ring):
for user2 in fraud_ring[i + 1 :]:
network_data.append(
{
"entity_id": user1,
"connected_entity": user2,
"relationship_type": "frequent_interaction",
"relationship_strength": 0.8,
"transaction_count": np.random.randint(5, 20),
"is_suspicious": True,
}
)
# Create normal connections
normal_users = [u for u in users if u not in fraud_users]
for user in normal_users[:20]: # Limit for performance
# Each user connects to 1-3 others normally
num_connections = np.random.randint(1, 4)
connected_users = np.random.choice(
[u for u in normal_users if u != user],
size=min(num_connections, len(normal_users) - 1),
replace=False,
)
for connected_user in connected_users:
network_data.append(
{
"entity_id": user,
"connected_entity": connected_user,
"relationship_type": "normal_interaction",
"relationship_strength": 0.3,
"transaction_count": np.random.randint(1, 5),
"is_suspicious": False,
}
)
return network_data
def _validate_schema_compliance(
self, df: pd.DataFrame, schema: Dict[str, Any]
) -> Dict[str, Any]:
"""Validate dataset compliance with schema"""
required_fields = schema["transaction_data"]["required_fields"]
optional_fields = schema["transaction_data"]["optional_fields"]
compliance = {
"has_required_fields": all(
field in df.columns for field in required_fields
),
"missing_required_fields": [
field for field in required_fields if field not in df.columns
],
"has_optional_fields": [
field for field in optional_fields if field in df.columns
],
"data_quality": {
"null_values": df.isnull().sum().to_dict(),
"duplicate_transactions": df["transaction_id"].duplicated().sum(),
"data_types_correct": True, # Simplified validation
},
"fraud_labels_present": "is_fraud" in df.columns,
"schema_version": "1.0",
}
return compliance