med.research - Research Privacy & Clinical Trials
HIPAA-compliant data de-identification, consent tracking, RCT randomization, sample size calculation, and stratified allocation.
De-identification
deidentify(patient) ? dict
Convert a Patient to an anonymized dictionary (Safe Harbor method):
- Names -> SHA-256 hash (first 12 chars)
- Ages > 89 -> "90+"
- Timestamps -> fuzzed +/-24 hours
- Clinical data -> preserved
from moisscode import Patient, StandardLibrary
lib = StandardLibrary()
p = Patient(name="Jane Doe", age=92, bp=88, hr=115, rr=26, sex='F')
anon = lib.research.deidentify(p)
# {'patient_hash': 'a3b8c1...', 'age': '90+', 'hr': 115, 'bp': 88, ...}
log_to_datalake(anon_data, study_id) ? bool
Write anonymized data to the research data lake.
Consent Tracking
consent_check(patient_id, study_id) ? dict
Check if a patient has consented to a research study. Returns consent status and date.
lib.research.consent_check("PT-001", "TRIAL-2026-001")
# {'consented': True, 'consent_date': '2026-02-13T10:30:00'}
Clinical Trial Design
randomize(patient_count, arms=2, ratio=None) ? dict
Randomize patients into treatment arms for an RCT. Supports equal or unequal allocation ratios.
result = lib.research.randomize(100, arms=2, ratio=[2, 1])
# {'arm_labels': ['Arm_A', 'Arm_B'],
# 'arm_counts': {'Arm_A': 67, 'Arm_B': 33},
# 'assignments': {'PT-0001': 'Arm_A', 'PT-0002': 'Arm_B', ...}}
sample_size(effect_size, alpha=0.05, power=0.80) ? dict
Calculate required sample size for a two-group comparison (t-test).
Formula: n = 2 * ((z_alpha + z_beta) / effect_size)^2
lib.research.sample_size(0.5)
# {'n_per_group': 63, 'total_n': 126, 'alpha': 0.05, 'power': 0.8}
lib.research.sample_size(0.3, alpha=0.01, power=0.90)
# {'n_per_group': 329, 'total_n': 658}
stratify(patient_count, variable, strata=None) ? dict
Create stratified allocation for a clinical trial. Ensures balanced representation across strata.
lib.research.stratify(100, "sex", ["Male", "Female"])
# {'allocation': {'Male': 50, 'Female': 50},
# 'patients': {'Male': ['PT-0001', ...], 'Female': ['PT-0051', ...]}}
Complete Example
protocol ClinicalTrial_Setup {
input: Patient p;
// Check consent
let consent = med.research.consent_check("PT-001", "SEPSIS-TRIAL-2026");
if consent.consented {
// Randomize into 3 arms (2:1:1)
let assignment = med.research.randomize(200, 3, [2, 1, 1]);
// Sample size calculation
let power = med.research.sample_size(0.4, 0.05, 0.90);
// De-identify for export
let anon = med.research.deidentify(p);
med.research.log_to_datalake(anon, "SEPSIS-TRIAL-2026");
}
}