Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4,746 changes: 4,746 additions & 0 deletions xtheta-lab/data/open_bell/hensen/raw/bell_open_data.txt

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions xtheta-lab/outputs/data/hensen_chsh_summary.csv
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
dataset_name,row_count,CHSH_S,CHSH_S_se,interpretation_warning,count_00,E_00,count_01,E_01,count_10,E_10,count_11,E_11,S_ci_low_95,S_ci_high_95,S_bootstrap_mean,S_bootstrap_std,bootstrap_samples,phi_eff,R_theta_eff,fit_status
hensen,4746,2.7777777777777777,0.20951312035156963,"Phi_eff is an effective phenomenological parameter only. Without gravitational path, altitude, curvature, or spacetime-baseline metadata, this is not evidence of spacetime-induced X-Theta holonomy.",0,0.0,9,0.7777777777777778,3,1.0,1,-1.0,1.1083333333333336,3.0,2.3399311940553424,0.5782461478532692,1000,0.13484626042491968,0.1419753086419753,success
dataset_name,row_count,CHSH_S,CHSH_S_se,interpretation_warning,count_00,E_00,count_01,E_01,count_10,E_10,count_11,E_11,S_ci_low_95,S_ci_high_95,S_bootstrap_mean,S_bootstrap_std,bootstrap_samples,phi_eff,R_theta_eff,fit_status,fit_warning
hensen,4746,2.7777777777777777,0.20951312035156963,"Phi_eff is an effective phenomenological parameter only. Without gravitational path, altitude, curvature, or spacetime-baseline metadata, this is not evidence of spacetime-induced X-Theta holonomy.",0,0.0,9,0.7777777777777778,3,1.0,1,-1.0,1.1083333333333336,3.0,2.3399311940553424,0.5782461478532692,1000,0.1348461424403639,0.14197506625061654,Success,"Phi_eff is an effective phenomenological parameter only. Without gravitational path, altitude, curvature, or spacetime-baseline metadata, this is not evidence of spacetime-induced X-Theta holonomy."
3 changes: 2 additions & 1 deletion xtheta-lab/outputs/reports/hensen_validation_report.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@

- **Effective Phase ($\Phi_{eff}$):** 0.134846 rad
- **Effective Anisotropy ($R_{\Theta, eff}$):** 0.141975
- **Fit Status:** success
- **Fit Status:** Success
- **Fit Warning:** Phi_eff is an effective phenomenological parameter only. Without gravitational path, altitude, curvature, or spacetime-baseline metadata, this is not evidence of spacetime-induced X-Theta holonomy.

## Setting Expectations and Counts

Expand Down
1 change: 1 addition & 0 deletions xtheta-lab/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ skyfield
sgp4
astropy
tabulate
requests
103 changes: 103 additions & 0 deletions xtheta-lab/scripts/run_all_open_data_validation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
#!/usr/bin/env python3
"""
Batch runner for all available open Bell/CHSH datasets.
"""
from __future__ import annotations

import csv
import subprocess
import sys
from pathlib import Path

# Add adapter path to access download functions if needed
sys.path.append(str(Path(__file__).parent.parent))
from xtheta.data.adapters.hensen import download_hensen_data

DATASETS = [
{
"name": "hensen",
"dataset": "hensen",
"data": Path("data/open_bell/hensen/raw/bell_open_data.txt"),
"output": Path("outputs/open_data/hensen"),
},
]


def run_dataset(item: dict) -> dict | None:
data_path = item["data"]

# Attempt download/cache if missing
if item["dataset"] == "hensen" and not data_path.exists():
download_hensen_data(data_path)

if not data_path.exists():
print(f"[SKIP] {item['name']}: missing data file: {data_path}")
return None

cmd = [
sys.executable,
"scripts/run_open_data_validation.py",
"--dataset",
item["dataset"],
"--data",
str(data_path),
"--output",
str(item["output"]),
"--bootstrap-samples",
"1000",
]

print(f"\n[RUN] {item['name']}")
print(" ".join(cmd))
subprocess.run(cmd, check=True)

summary_file = item["output"] / "data" / f"{item['dataset']}_chsh_summary.csv"
if not summary_file.exists():
print(f"[WARN] Summary not found: {summary_file}")
return None

with summary_file.open("r", newline="", encoding="utf-8") as f:
rows = list(csv.DictReader(f))
return rows[0] if rows else None


def main() -> None:
results = []

for item in DATASETS:
result = run_dataset(item)
if result:
results.append(result)

comparison_dir = Path("outputs/open_data/comparison")
comparison_dir.mkdir(parents=True, exist_ok=True)
comparison_file = comparison_dir / "open_data_comparison.csv"

if results:
fields = [
"dataset_name",
"row_count",
"CHSH_S",
"CHSH_S_se",
"S_ci_low_95",
"S_ci_high_95",
"S_bootstrap_mean",
"S_bootstrap_std",
"phi_eff",
"R_theta_eff",
"fit_status",
"interpretation_warning",
]

with comparison_file.open("w", newline="", encoding="utf-8") as f:
writer = csv.DictWriter(f, fieldnames=fields, extrasaction="ignore")
writer.writeheader()
writer.writerows(results)

print(f"\n[DONE] Comparison saved to: {comparison_file}")
else:
print("\n[WARN] No datasets were successfully processed.")


if __name__ == "__main__":
main()
8 changes: 7 additions & 1 deletion xtheta-lab/scripts/run_open_data_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"""
Command-line runner for Open Bell/CHSH Data Validation.
"""
from __future__ import annotations
import argparse
import sys
import os
Expand All @@ -12,7 +13,7 @@

from xtheta.experiments.open_data_validation import run_open_data_chsh_validation
from xtheta.data.adapters.weihs import load_weihs_dataset
from xtheta.data.adapters.hensen import load_hensen_dataset
from xtheta.data.adapters.hensen import load_hensen_dataset, download_hensen_data
from xtheta.data.adapters.big_bell_test import load_big_bell_test_dataset
from xtheta.data.loaders import get_loader

Expand All @@ -31,6 +32,11 @@ def main():
args = parser.parse_args()

data_path = Path(args.data)

# Pre-flight for Hensen: try to download if missing
if args.dataset == "hensen" and not data_path.exists():
download_hensen_data(data_path)

if not data_path.exists():
print(f"Error: Data path does not exist: {data_path}")
sys.exit(1)
Expand Down
85 changes: 43 additions & 42 deletions xtheta-lab/xtheta/data/adapters/hensen.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,50 @@
"""
X-Theta Adapter for Hensen et al. 2015 Delft loophole-free Bell-test data.
"""
from __future__ import annotations
import pandas as pd
import numpy as np
import os
import requests
from pathlib import Path
from typing import Iterator, Dict
from xtheta.experiments.open_data_validation import run_open_data_chsh_validation

# The dataset is often referred to by its DOI or the 4TU.ResearchData link.
# Since we don't have a direct reliable URL for the raw text file in the wild,
# and the user expects a download/cache mechanism, we'll implement a robust
# local check with a placeholder for the remote URL if one is provided or found.
HENSEN_DATA_URL = "https://raw.githubusercontent.com/tonyhe-quantum/xtheta-lab/main/data/open_bell/hensen/raw/bell_open_data.txt"

def download_hensen_data(target_path: Path) -> bool:
"""
Attempt to download the Hensen dataset if it's missing.
"""
if target_path.exists():
return True

print(f"Data not found at {target_path}. Attempting download...")
try:
response = requests.get(HENSEN_DATA_URL, timeout=30)
if response.status_code == 200:
target_path.parent.mkdir(parents=True, exist_ok=True)
target_path.write_bytes(response.content)
print(f"Successfully downloaded Hensen data to {target_path}")
return True
else:
print(f"Download failed with status code: {response.status_code}")
except Exception as e:
print(f"Download error: {e}")

# Fallback: check repository root as per instructions
root_file = Path("../bell_open_data.txt")
if root_file.exists():
print(f"Found data at repository root. Copying to {target_path}")
target_path.parent.mkdir(parents=True, exist_ok=True)
import shutil
shutil.copy(root_file, target_path)
return True

return False

def load_hensen_dataset(path: str, chunksize: int = 200_000) -> Iterator[pd.DataFrame]:
"""
Expand All @@ -17,29 +56,18 @@ def load_hensen_dataset(path: str, chunksize: int = 200_000) -> Iterator[pd.Data
- Col 4: Bob outcome (0/1)
"""
p = Path(path)
if not p.exists():
raise FileNotFoundError(f"Hensen data not found at: {path}")
if not download_hensen_data(p):
raise FileNotFoundError(f"Hensen data not found and could not be downloaded to: {path}")

# Read raw lines to handle potential format variations
# Read raw lines
with open(p, 'r', encoding='utf-8') as f:
lines = [l.strip().split(',') for l in f.readlines() if l.strip()]

# Simple parser assuming CSV-like structure in bell_open_data.txt
# 2015-06-26 17:24:12.119993,1,2,5445065,1,5671004,1,1,1,10379,10371,11281,13113,0,0,0,0
# Paper mapping often uses different columns.
# Based on download_delft.py:
# Index 1: Setting A, Index 2: Setting B, Index 4: Outcome A, Index 6: Outcome B
# Wait, download_delft.py says Index 3/4 for outcome?
# Let's re-examine bell_open_data.txt:
# 2015-06-26 17:24:12.119993 (0), 1 (1), 2 (2), 5445065 (3), 1 (4), 5671004 (5), 1 (6), 1 (7), 1 (8) ...
# Delft 2015 settings are {a=1, 2} and {b=1, 2}. Map to 0/1.

data = []
for line in lines:
try:
# Map settings to 0/1: a=1->0, a=2->1; b=1->0, b=2->1 (approx)
# Actually Delft settings are a={0, 1} and b={0, 1} in CHSH terms.
# Raw data in bell_open_data.txt seems to have settings in col 1 and 2.
a_set = int(line[1]) - 1
b_set = int(line[2]) - 1
a_out = 1 if int(line[4]) == 1 else -1
Expand All @@ -58,32 +86,5 @@ def load_hensen_dataset(path: str, chunksize: int = 200_000) -> Iterator[pd.Data
continue

df = pd.DataFrame(data)
# Yield in chunks
for i in range(0, len(df), chunksize):
yield df.iloc[i : i + chunksize]

def run_hensen_count_table_validation(counts: Dict[str, int], output_dir: str = "outputs") -> dict:
"""
Run validation using published count tables (manual input mode).
counts = {
"n00_pp": ..., "n00_pm": ..., "n00_mp": ..., "n00_mm": ...,
...
}
"""
# Convert count table to synthetic event list
data = []
for a in [0, 1]:
for b in [0, 1]:
for oa in [1, -1]:
for ob in [1, -1]:
key = f"n{a}{b}_{'p' if oa==1 else 'm'}{'p' if ob==1 else 'm'}"
n = counts.get(key, 0)
for _ in range(n):
data.append({
"alice_setting": a, "bob_setting": b,
"alice_outcome": oa, "bob_outcome": ob,
"trial_id": len(data), "timestamp": 0.0, "source_file": "manual_input"
})

df = pd.DataFrame(data)
return run_open_data_chsh_validation(iter([df]), dataset_name="hensen_2015_manual", output_dir=output_dir)
Loading