|
|
|
|
|
""" |
|
|
NEURO-COSMIC DATA ANALYSIS FRAMEWORK v1.0 |
|
|
Production version: Core scientific analysis framework for neural and cosmological data. |
|
|
""" |
|
|
|
|
|
import hashlib |
|
|
import hmac |
|
|
import os |
|
|
import secrets |
|
|
from dataclasses import dataclass, field |
|
|
from datetime import datetime, timedelta |
|
|
from typing import Any, Dict, List, Optional, Tuple |
|
|
import logging |
|
|
|
|
|
import numpy as np |
|
|
import pandas as pd |
|
|
from scipy import stats, signal |
|
|
|
|
|
|
|
|
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s') |
|
|
logger = logging.getLogger('neuro_cosmic_framework') |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass |
|
|
class FrameworkConfig: |
|
|
max_requests_per_minute: int = 100 |
|
|
cache_ttl_hours: int = 24 |
|
|
max_file_size_mb: int = 100 |
|
|
allowed_data_types: Tuple[str, ...] = ('eeg', 'cosmological', 'behavioral') |
|
|
secret_key: str = field(default_factory=lambda: os.environ.get('FRAMEWORK_SECRET', secrets.token_hex(32))) |
|
|
|
|
|
def __post_init__(self): |
|
|
if len(self.secret_key) < 32: |
|
|
raise ValueError("Secret key must be at least 32 characters") |
|
|
|
|
|
class SecurityError(Exception): |
|
|
pass |
|
|
|
|
|
class DataValidationError(Exception): |
|
|
pass |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class SecureAuthManager: |
|
|
def __init__(self, secret_key: str): |
|
|
if len(secret_key) < 32: |
|
|
raise SecurityError("Insufficient secret key length") |
|
|
self.secret_key = secret_key.encode('utf-8') |
|
|
self.user_keys: Dict[str, str] = {} |
|
|
|
|
|
def create_user(self, user_id: str) -> str: |
|
|
if not user_id or not isinstance(user_id, str): |
|
|
raise SecurityError("Invalid user ID") |
|
|
api_key = secrets.token_urlsafe(32) |
|
|
key_hash = self._hash_key(api_key) |
|
|
self.user_keys[user_id] = key_hash |
|
|
return api_key |
|
|
|
|
|
def authenticate(self, user_id: str, api_key: str) -> bool: |
|
|
if not user_id or not api_key: |
|
|
return False |
|
|
stored_hash = self.user_keys.get(user_id) |
|
|
if not stored_hash: |
|
|
return False |
|
|
return hmac.compare_digest(stored_hash, self._hash_key(api_key)) |
|
|
|
|
|
def _hash_key(self, key: str) -> str: |
|
|
return hmac.new(self.secret_key, key.encode('utf-8'), 'sha256').hexdigest() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class DataValidator: |
|
|
@staticmethod |
|
|
def validate_eeg_data(df: pd.DataFrame) -> bool: |
|
|
required_columns = {'timestamp', 'channel_1'} |
|
|
if not required_columns.issubset(df.columns): |
|
|
return False |
|
|
if not pd.api.types.is_numeric_dtype(df['timestamp']): |
|
|
return False |
|
|
if len(df) > 1: |
|
|
time_diff = np.diff(df['timestamp']) |
|
|
if np.any(time_diff <= 0): |
|
|
return False |
|
|
return True |
|
|
|
|
|
@staticmethod |
|
|
def validate_cosmological_data(df: pd.DataFrame) -> bool: |
|
|
required_columns = {'redshift', 'distance'} |
|
|
if not required_columns.issubset(df.columns): |
|
|
return False |
|
|
if np.any(df['redshift'] < 0): |
|
|
return False |
|
|
if np.any(df['distance'] <= 0): |
|
|
return False |
|
|
return True |
|
|
|
|
|
@staticmethod |
|
|
def sanitize_input_data(df: pd.DataFrame) -> pd.DataFrame: |
|
|
numeric_df = df.select_dtypes(include=[np.number]) |
|
|
sanitized_df = numeric_df.fillna(method='ffill').fillna(method='bfill') |
|
|
return sanitized_df |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class NeuralAnalysisEngine: |
|
|
def __init__(self): |
|
|
self.supported_metrics = ['power_spectrum', 'coherence', 'correlation'] |
|
|
|
|
|
def analyze_eeg_power_spectrum(self, eeg_data: pd.DataFrame) -> Dict[str, Any]: |
|
|
if not DataValidator.validate_eeg_data(eeg_data): |
|
|
raise DataValidationError("Invalid EEG data format") |
|
|
signal_columns = [col for col in eeg_data.columns if col.startswith('channel_')] |
|
|
results = {} |
|
|
for channel in signal_columns: |
|
|
signal_data = eeg_data[channel].values |
|
|
freqs, psd = signal.welch(signal_data, fs=250) |
|
|
bands = { |
|
|
'delta': (0.5, 4), |
|
|
'theta': (4, 8), |
|
|
'alpha': (8, 13), |
|
|
'beta': (13, 30), |
|
|
'gamma': (30, 50) |
|
|
} |
|
|
band_powers = {} |
|
|
for band, (low, high) in bands.items(): |
|
|
band_mask = (freqs >= low) & (freqs <= high) |
|
|
band_powers[band] = float(np.trapz(psd[band_mask], freqs[band_mask])) |
|
|
results[channel] = { |
|
|
'total_power': float(np.trapz(psd, freqs)), |
|
|
'band_powers': band_powers, |
|
|
'peak_frequency': float(freqs[np.argmax(psd)]) |
|
|
} |
|
|
return results |
|
|
|
|
|
def compute_functional_connectivity(self, eeg_data: pd.DataFrame) -> pd.DataFrame: |
|
|
signal_columns = [col for col in eeg_data.columns if col.startswith('channel_')] |
|
|
signals = eeg_data[signal_columns].values.T |
|
|
correlation_matrix = np.corrcoef(signals) |
|
|
return pd.DataFrame( |
|
|
correlation_matrix, |
|
|
index=signal_columns, |
|
|
columns=signal_columns |
|
|
) |
|
|
|
|
|
class CosmologicalAnalysisEngine: |
|
|
def analyze_hubble_relation(self, cosmic_data: pd.DataFrame) -> Dict[str, Any]: |
|
|
if not DataValidator.validate_cosmological_data(cosmic_data): |
|
|
raise DataValidationError("Invalid cosmological data format") |
|
|
c = 299792.458 |
|
|
redshifts = cosmic_data['redshift'].values |
|
|
distances = cosmic_data['distance'].values |
|
|
velocities = c * redshifts |
|
|
valid_mask = (distances > 0) & (redshifts > 0) & (redshifts < 0.1) |
|
|
if np.sum(valid_mask) < 2: |
|
|
return {'error': 'Insufficient valid data for Hubble relation analysis'} |
|
|
v_valid = velocities[valid_mask] |
|
|
d_valid = distances[valid_mask] |
|
|
slope, intercept, r_value, p_value, std_err = stats.linregress(d_valid, v_valid) |
|
|
return { |
|
|
'hubble_constant_estimate': float(slope), |
|
|
'correlation_coefficient': float(r_value), |
|
|
'p_value': float(p_value), |
|
|
'standard_error': float(std_err), |
|
|
'data_points_used': int(np.sum(valid_mask)) |
|
|
} |
|
|
|
|
|
class CrossDomainAnalyzer: |
|
|
def __init__(self): |
|
|
self.neural_engine = NeuralAnalysisEngine() |
|
|
self.cosmic_engine = CosmologicalAnalysisEngine() |
|
|
|
|
|
def analyze_correlations(self, neural_data: pd.DataFrame, cosmic_data: pd.DataFrame, neural_metric: str = 'total_power') -> Dict[str, Any]: |
|
|
neural_results = self.neural_engine.analyze_eeg_power_spectrum(neural_data) |
|
|
cosmic_results = self.cosmic_engine.analyze_hubble_relation(cosmic_data) |
|
|
neural_metrics = [channel_data[neural_metric] for channel_data in neural_results.values() if neural_metric in channel_data] |
|
|
return { |
|
|
'neural_analysis': neural_results, |
|
|
'cosmological_analysis': cosmic_results, |
|
|
'correlation_analysis': { |
|
|
'status': 'exploratory', |
|
|
'disclaimer': 'Cross-domain correlations are speculative and require extensive validation', |
|
|
'neural_metrics_available': list(neural_metrics), |
|
|
'cosmic_metrics_available': list(cosmic_results.keys()) if isinstance(cosmic_results, dict) else [] |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass |
|
|
class AnalysisRequest: |
|
|
user_id: str |
|
|
analysis_type: str |
|
|
neural_data: Optional[pd.DataFrame] = None |
|
|
cosmic_data: Optional[pd.DataFrame] = None |
|
|
parameters: Dict[str, Any] = None |
|
|
|
|
|
def __post_init__(self): |
|
|
if self.analysis_type not in ['neural', 'cosmological', 'cross_domain']: |
|
|
raise ValueError(f"Invalid analysis type: {self.analysis_type}") |
|
|
if self.analysis_type in ['neural', 'cross_domain'] and self.neural_data is None: |
|
|
raise ValueError("Neural data required for neural analysis") |
|
|
if self.analysis_type in ['cosmological', 'cross_domain'] and self.cosmic_data is None: |
|
|
raise ValueError("Cosmological data required for cosmological analysis") |
|
|
|
|
|
@dataclass |
|
|
class AnalysisResult: |
|
|
request_id: str |
|
|
timestamp: str |
|
|
analysis_type: str |
|
|
results: Dict[str, Any] |
|
|
processing_time: float |
|
|
warnings: List[str] |
|
|
metadata: Dict[str, Any] |
|
|
|
|
|
class AnalysisFramework: |
|
|
def __init__(self, config: FrameworkConfig): |
|
|
self.config = config |
|
|
self.auth_manager = SecureAuthManager(config.secret_key) |
|
|
self.validator = DataValidator() |
|
|
self.cross_analyzer = CrossDomainAnalyzer() |
|
|
self.rate_limiter = RateLimiter(config.max_requests_per_minute) |
|
|
self.neural_engine = NeuralAnalysisEngine() |
|
|
self.cosmic_engine = CosmologicalAnalysisEngine() |
|
|
logger.info("Analysis framework initialized") |
|
|
|
|
|
async def process_request(self, request: AnalysisRequest) -> AnalysisResult: |
|
|
start_time = datetime.utcnow() |
|
|
request_id = hashlib.sha256(f"{request.user_id}{start_time.isoformat()}".encode()).hexdigest()[:16] |
|
|
try: |
|
|
if not self.rate_limiter.check_limit(request.user_id): |
|
|
raise SecurityError("Rate limit exceeded") |
|
|
if request.neural_data is not None: |
|
|
if not self.validator.validate_eeg_data(request.neural_data): |
|
|
raise DataValidationError("Invalid EEG data format") |
|
|
neural_data = self.validator.sanitize_input_data(request.neural_data) |
|
|
else: |
|
|
neural_data = None |
|
|
if request.cosmic_data is not None: |
|
|
if not self.validator.validate_cosmological_data(request.cosmic_data): |
|
|
raise DataValidationError("Invalid cosmological data format") |
|
|
cosmic_data = self.validator.sanitize_input_data(request.cosmic_data) |
|
|
else: |
|
|
cosmic_data = None |
|
|
if request.analysis_type == 'neural': |
|
|
results = self.neural_engine.analyze_eeg_power_spectrum(neural_data) |
|
|
elif request.analysis_type == 'cosmological': |
|
|
results = self.cosmic_engine.analyze_hubble_relation(cosmic_data) |
|
|
elif request.analysis_type == 'cross_domain': |
|
|
results = self.cross_analyzer.analyze_correlations(neural_data, cosmic_data) |
|
|
else: |
|
|
raise ValueError(f"Unsupported analysis type: {request.analysis_type}") |
|
|
processing_time = (datetime.utcnow() - start_time).total_seconds() |
|
|
return AnalysisResult( |
|
|
request_id=request_id, |
|
|
timestamp=start_time.isoformat(), |
|
|
analysis_type=request.analysis_type, |
|
|
results=results, |
|
|
processing_time=processing_time, |
|
|
warnings=self._generate_warnings(results), |
|
|
metadata={ |
|
|
'data_points_neural': len(neural_data) if neural_data is not None else 0, |
|
|
'data_points_cosmic': len(cosmic_data) if cosmic_data is not None else 0, |
|
|
'framework_version': '1.0' |
|
|
} |
|
|
) |
|
|
except Exception as e: |
|
|
logger.error(f"Analysis failed for request {request_id}: {str(e)}") |
|
|
raise |
|
|
|
|
|
def _generate_warnings(self, results: Dict[str, Any]) -> List[str]: |
|
|
warnings = [] |
|
|
if 'correlation_analysis' in results: |
|
|
warnings.append("Cross-domain correlations are exploratory and require rigorous validation") |
|
|
if isinstance(results, dict) and 'p_value' in results: |
|
|
if results['p_value'] > 0.05: |
|
|
warnings.append("Results are not statistically significant (p > 0.05)") |
|
|
return warnings |
|
|
|
|
|
class RateLimiter: |
|
|
def __init__(self, requests_per_minute: int): |
|
|
self.requests_per_minute = requests_per_minute |
|
|
self.requests: Dict[str, List[datetime]] = {} |
|
|
def check_limit(self, user_id: str) -> bool: |
|
|
now = datetime.utcnow() |
|
|
minute_ago = now - timedelta(minutes=1) |
|
|
if user_id not in self.requests: |
|
|
self.requests[user_id] = [] |
|
|
self.requests[user_id] = [req_time for req_time in self.requests[user_id] if req_time > minute_ago] |
|
|
if len(self.requests[user_id]) >= self.requests_per_minute: |
|
|
return False |
|
|
self.requests[user_id].append(now) |
|
|
return True |