Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from typing import Dict, List, Tuple, Optional
- class SymptomVsWaveform:
- """
- SymptomVsWaveform module by El Chaderino
- http://github.com/ElChaderino/The-Squiggle-Interpreter
- This module aids EEG practitioners in distinguishing between symptom-based neurofeedback protocols
- and waveform-informed targeting. It offers rules-of-thumb guidance, symptom-to-waveform analysis,
- targeted site recommendations, protocol suggestions, and common practitioner pitfalls.
- Attributes:
- rules_of_thumb (dict): Guidelines for interpreting EEG findings in relation to symptoms.
- Methods:
- analyze_site_drift(signal_profile, report_symptoms):
- Compares symptoms with EEG profiles to identify potential mismatches.
- match_site_to_target(eeg_findings):
- Suggests starting neurofeedback sites based on EEG data.
- suggest_protocols(eeg_findings, reported_symptoms):
- Recommends specific neurofeedback protocols according to EEG data and reported symptoms.
- explain_traps():
- Outlines common mistakes practitioners should avoid when interpreting EEG data.
- summarize(signal_profile, report_symptoms):
- Returns a user-friendly summary of all recommendations and flags.
- """
- def __init__(self):
- self.rules_of_thumb: Dict[str, Dict] = {
- 'left_frontal': {'adjustment': +2, 'notes': 'Often overstimulating if run early in dysregulated clients'},
- 'posterior': {'stabilize_before_frontal': True},
- 'cingulate_hibeta': {'suggests': 'OCD/rumination, vigilance drive, inner conflict'},
- 'alpha_asymmetry': {'suggests': 'valence bias, depression, affect regulation'},
- 'hibeta_dominance': {'suggests': 'instability or trauma override masking real signal'},
- 'central_delta': {'suggests': 'sleep regulation issues or potential underlying trauma'},
- 'right_parietal_alpha': {'suggests': 'attention issues in PTSD'},
- 'sensorimotor_theta': {'suggests': 'motor control issues, potential ADHD'},
- 'prefrontal_beta': {'suggests': 'executive dysfunction, hyperarousal in PTSD'},
- 'right_temporal_theta': {'suggests': 'language processing issues, potential ASD'}
- }
- def analyze_site_drift(self, signal_profile: Dict[str, float], report_symptoms: List[str]) -> Dict[str, str]:
- """
- Compares reported symptoms with EEG signals to identify mismatches.
- Returns symptoms flagged as potentially mistargeted.
- """
- flags = {}
- symptom_checks = {
- 'anxiety': lambda sp: sp.get('Pz_hibeta', 0) < sp.get('Fz_hibeta', 0),
- 'focus issues': lambda sp: sp.get('theta', 0) < 4.5,
- 'impulsivity': lambda sp: sp.get('F3_beta', 0) < 2.0,
- 'depression': lambda sp: abs(sp.get('F3_alpha', 0) - sp.get('F4_alpha', 0)) < 1.0,
- 'insomnia': lambda sp: sp.get('Cz_delta', 0) > 5.0,
- 'hyperactivity': lambda sp: sp.get('Fz_theta', 0) < 4.0,
- 'PTSD': lambda sp: sp.get('Fz_beta', 0) > 2.0 or sp.get('Cz_beta', 0) > 2.0 or (sp.get('P4_alpha', 0) > sp.get('P3_alpha', 0)),
- 'OCD': lambda sp: sp.get('Fz_hibeta', 0) > 2.0 or sp.get('Cz_hibeta', 0) > 2.0
- }
- symptom_messages = {
- 'anxiety': "Likely mistargeted. Pz not resolved before frontal work.",
- 'focus issues': "Reported focus trouble without elevated theta—may be vigilance crash, not ADD.",
- 'impulsivity': "No significant frontal beta—impulsivity may be emotional, not attentional.",
- 'depression': "No significant alpha asymmetry—depression may not be related to valence bias.",
- 'insomnia': "Elevated delta at Cz—may indicate sleep-related issues.",
- 'hyperactivity': "No elevated theta at Fz—hyperactivity may not be ADHD-related.",
- 'PTSD': "High beta at Fz/Cz or right parietal alpha asymmetry may indicate hyperarousal or attention issues in PTSD.",
- 'OCD': "High hibeta at Fz/Cz may indicate OCD/rumination."
- }
- for symptom in report_symptoms:
- if symptom in symptom_checks and symptom_checks[symptom](signal_profile):
- flags[symptom] = symptom_messages[symptom]
- return flags
- def match_site_to_target(self, eeg_findings: Dict[str, float], reported_symptoms: Optional[List[str]] = None) -> List[Tuple[str, str]]:
- """
- Suggests EEG sites for initiating neurofeedback based on EEG signal abnormalities, with cross-checks.
- Cross-checks ensure posterior stability, symptom consistency, and coherence metrics.
- Returns a list of (site, reason) tuples.
- """
- suggestions = []
- reported_symptoms = reported_symptoms or []
- # Define target checks with thresholds and cross-check conditions
- target_checks = [
- ('Pz_hibeta', 2.0, "Pz", "Start here to stabilize limbic/sensory loops before frontal work", lambda ef, rs: True),
- ('Fz_hibeta', 2.0, "Fz", "Only after posterior cleanup. Frontal beta may feel worse until then.",
- lambda ef, rs: ef.get('Pz_hibeta', 0) <= 2.0), # Posterior stability check
- ('T3_theta', 3.5, "T3", "Check for disconnection or developmental drift (possible ASD marker)",
- lambda ef, rs: 'hyperactivity' in rs or 'focus issues' in rs), # Symptom consistency
- ('Cz_delta', 5.0, "Cz", "Consider targeting delta at Cz for sleep regulation issues",
- lambda ef, rs: 'insomnia' in rs),
- ('Fz_beta', 2.0, "Fz", "Reduce beta for hyperarousal in PTSD",
- lambda ef, rs: 'PTSD' in rs and ef.get('Pz_hibeta', 0) <= 2.0),
- ('Cz_beta', 2.0, "Cz", "Reduce beta for hyperarousal in PTSD",
- lambda ef, rs: 'PTSD' in rs and ef.get('Pz_hibeta', 0) <= 2.0),
- ('P4_alpha', 5.0, "P3/P4", "Balance alpha asymmetry for attention issues in PTSD",
- lambda ef, rs: 'PTSD' in rs and ef.get('P4_alpha', 0) > ef.get('P3_alpha', 0)),
- ('F3_alpha', 5.0, "F3/F4", "Check for emotional regulation issues",
- lambda ef, rs: 'depression' in rs or abs(ef.get('F3_alpha', 0) - ef.get('F4_alpha', 0)) > 1.0),
- ('O1_alpha', 5.0, "O1/O2", "Increase alpha for relaxation in anxiety",
- lambda ef, rs: 'anxiety' in rs),
- ('C3_theta', 5.0, "C3/C4", "Target theta for motor control in ADHD",
- lambda ef, rs: 'hyperactivity' in rs or 'focus issues' in rs),
- ('T4_theta', 3.5, "T4", "Check for language processing issues (possible ASD marker)",
- lambda ef, rs: 'hyperactivity' in rs or 'focus issues' in rs),
- ('Fp1_beta', 2.0, "Fp1/Fp2", "Reduce beta for executive dysfunction in PTSD",
- lambda ef, rs: 'PTSD' in rs and ef.get('Pz_hibeta', 0) <= 2.0),
- ('Oz_alpha', 5.0, "Pz/Oz", "Increase alpha for visual processing stability",
- lambda ef, rs: 'anxiety' in rs or 'PTSD' in rs)
- ]
- for signal, threshold, site, reason, cross_check in target_checks:
- if eeg_findings.get(signal, 0) > threshold and cross_check(eeg_findings, reported_symptoms):
- suggestions.append((site, reason))
- # Additional cross-check: Ensure coherence between homologous sites (e.g., F3/F4)
- if 'depression' in reported_symptoms and abs(eeg_findings.get('F3_alpha', 0) - eeg_findings.get('F4_alpha', 0)) > 1.0:
- if ('F3/F4', "Check for emotional regulation issues") not in suggestions:
- suggestions.append(("F3/F4", "Significant alpha asymmetry detected, confirm for depression"))
- return suggestions
- def suggest_protocols(self, eeg_findings: Dict[str, float], reported_symptoms: List[str]) -> List[Tuple[str, str]]:
- """
- Provides neurofeedback protocol recommendations based on EEG findings and client symptoms.
- Returns a list of (protocol, reason) tuples.
- """
- protocols = []
- protocol_conditions = [
- ('anxiety', lambda ef: ef.get('O1_alpha', 0) < 5.0 or ef.get('O2_alpha', 0) < 5.0,
- "Alpha-theta training at O1/O2", "For relaxation and anxiety reduction"),
- ('focus issues', lambda ef: ef.get('theta', 0) > 5.0,
- "Theta/beta ratio training at Fz/Cz", "To improve focus by reducing theta"),
- ('impulsivity', lambda ef: ef.get('F3_beta', 0) < 1.5,
- "Beta increase at F3", "For attentional control"),
- ('depression', lambda ef: abs(ef.get('F3_alpha', 0) - ef.get('F4_alpha', 0)) > 1.0,
- "Alpha asymmetry correction at F3/F4", "To address valence bias"),
- ('insomnia', lambda ef: ef.get('Cz_beta', 0) > 2.0,
- "Beta reduction at Cz", "To promote sleep"),
- ('hyperactivity', lambda ef: ef.get('Fz_theta', 0) > 5.0,
- "Theta reduction at Fz", "To address hyperactivity"),
- ('PTSD', lambda ef: ef.get('Fz_beta', 0) > 2.0 or ef.get('Cz_beta', 0) > 2.0,
- "Beta reduction at Fz/Cz", "To reduce hyperarousal"),
- ('PTSD', lambda ef: ef.get('P4_alpha', 0) > ef.get('P3_alpha', 0),
- "Alpha balancing at P3/P4", "To address attention issues"),
- ('PTSD', lambda ef: ef.get('Fp1_beta', 0) > 2.0,
- "Beta reduction at Fp1/Fp2", "To improve executive function"),
- ('OCD', lambda ef: ef.get('Fz_hibeta', 0) > 2.0 or ef.get('Cz_hibeta', 0) > 2.0,
- "Hibeta reduction at Fz/Cz", "To reduce rumination"),
- ('hyperactivity', lambda ef: ef.get('C3_theta', 0) > 5.0,
- "SMR training at C3/C4", "To enhance motor control in ADHD"),
- ('focus issues', lambda ef: ef.get('T4_theta', 0) > 3.5,
- "Theta reduction at T4", "To address potential ASD-related issues")
- ]
- for symptom, condition, protocol, reason in protocol_conditions:
- if symptom in reported_symptoms and condition(eeg_findings):
- protocols.append((protocol, reason))
- return protocols
- def explain_traps(self) -> List[str]:
- """
- Outlines common mistakes practitioners should avoid when interpreting EEG data.
- """
- return [
- "Mistaking artifact for true signal (e.g., muscle, eye movement)",
- "Overfocusing on frontal sites before stabilizing posterior regions",
- "Ignoring alpha asymmetry in depression cases",
- "Assuming all high beta is pathological—context matters",
- "Neglecting client-reported symptoms in favor of only waveform data",
- "Failing to cross-check protocol with both symptoms and EEG findings",
- "Targeting sites without checking posterior stability, risking overstimulation",
- "Overlooking coherence issues between homologous sites (e.g., F3/F4)",
- "Not adjusting protocols for new EEG sites like C3/C4 or Fp1/Fp2"
- ]
- def summarize(self, signal_profile: Dict[str, float], report_symptoms: List[str]) -> str:
- """
- Returns a user-friendly summary of all recommendations and flags.
- """
- drift_flags = self.analyze_site_drift(signal_profile, report_symptoms)
- site_targets = self.match_site_to_target(signal_profile, report_symptoms)
- protocols = self.suggest_protocols(signal_profile, report_symptoms)
- traps = self.explain_traps()
- summary = ["\n=== Symptom vs Waveform Analysis Summary ==="]
- if drift_flags:
- summary.append("\nPotential Mistargeted Symptoms:")
- for k, v in drift_flags.items():
- summary.append(f"- {k}: {v}")
- if site_targets:
- summary.append("\nRecommended Starting Sites:")
- for site, reason in site_targets:
- summary.append(f"- {site}: {reason}")
- if protocols:
- summary.append("\nSuggested Protocols:")
- for protocol, reason in protocols:
- summary.append(f"- {protocol}: {reason}")
- summary.append("\nCommon Practitioner Traps:")
- for trap in traps:
- summary.append(f"- {trap}")
- return "\n".join(summary)
- # Example usage
- if __name__ == "__main__":
- eeg = {
- 'Pz_hibeta': 2.5, 'Fz_hibeta': 1.5, 'theta': 3.0, 'F3_beta': 1.0,
- 'F3_alpha': 6.0, 'F4_alpha': 4.5, 'Cz_delta': 6.0, 'Fz_theta': 3.5,
- 'Fz_beta': 2.5, 'Cz_beta': 2.1, 'P4_alpha': 6.0, 'P3_alpha': 4.0,
- 'O1_alpha': 4.0, 'O2_alpha': 4.5, 'T3_theta': 4.0, 'C3_theta': 5.5,
- 'T4_theta': 3.8, 'Fp1_beta': 2.2, 'Oz_alpha': 5.2
- }
- symptoms = ['anxiety', 'depression', 'insomnia', 'focus issues', 'PTSD', 'OCD', 'hyperactivity']
- svw = SymptomVsWaveform()
- print(svw.summarize(eeg, symptoms))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement