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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license: mit
|
| 3 |
+
tags:
|
| 4 |
+
- pytorch
|
| 5 |
+
- spherical-cnn
|
| 6 |
+
- cmb
|
| 7 |
+
- healpix
|
| 8 |
+
- astronomy
|
| 9 |
+
- cosmology
|
| 10 |
+
library_name: pytorch
|
| 11 |
+
---
|
| 12 |
+
|
| 13 |
+
# torch-harmonics-healpix
|
| 14 |
+
|
| 15 |
+
Spectral CNN models for CMB parameter estimation on the HEALPix sphere, bridging [torch-harmonics](https://github.com/Philippe7427/torch-harmonics) with HEALPix maps.
|
| 16 |
+
|
| 17 |
+
These models reproduce and improve upon the benchmarks from [Krachmalnicoff & Tomasi (2019)](https://arxiv.org/abs/1902.04083), which originally used the pixel-space [NNhealpix](https://github.com/NToulis/nnhealpix) architecture.
|
| 18 |
+
|
| 19 |
+
**Source code:** `https://github.com/zonca/torch-harmonics-healpix`
|
| 20 |
+
|
| 21 |
+
## Model Summary
|
| 22 |
+
|
| 23 |
+
| Model | File | Task | Input | Output | Error | Params |
|
| 24 |
+
|-------|------|------|-------|--------|-------|--------|
|
| 25 |
+
| SpectralCNN T1 | `models/test1_v2_fix_noise0.pt` | β_peak estimation | T map | β_peak | 1.27% | 6.4M |
|
| 26 |
+
| SpectralCNN T2 | `models/test2_v2_fix_fsky1.0.pt` | β_Ep / β_Bp estimation | Q, U, mask | [β_Ep, β_Bp] | 1.69% / 1.53% | 9.8M |
|
| 27 |
+
| SpectralCNN T3 | `models/test3_v2_fix.pt` | Ο estimation | Q, U, mask | Ο | 3.76% | 9.8M |
|
| 28 |
+
|
| 29 |
+
## Architecture
|
| 30 |
+
|
| 31 |
+
**SpectralCNN** performs convolution in harmonic space instead of pixel space:
|
| 32 |
+
|
| 33 |
+
1. **HEALPix β Equiangular** resampling (bilinear interpolation)
|
| 34 |
+
2. **SHT** (Spherical Harmonic Transform) via torch-harmonics
|
| 35 |
+
3. **Learned spectral weights** β complex-valued 1Γ1 convolutions on (β, m) coefficients
|
| 36 |
+
4. **ISHT** (Inverse SHT) back to pixel space
|
| 37 |
+
5. **Equiangular β HEALPix** resampling
|
| 38 |
+
|
| 39 |
+
The network stacks multiple `SpectralConvBlock` layers (SHT β learned weights β ISHT + residual) followed by global average pooling and a linear head.
|
| 40 |
+
|
| 41 |
+
**Key advantage over pixel-space CNNs:** The spectral prior enforces physical smoothness in harmonic space, which is especially powerful for polarization estimation where E/B modes have characteristic spectral signatures.
|
| 42 |
+
|
| 43 |
+
### Design Decisions
|
| 44 |
+
|
| 45 |
+
- **Inpainting for partial sky:** Masked pixels are replaced with the observed-pixel mean before SHT to prevent mode-coupling artifacts
|
| 46 |
+
- **Shared mask:** Train/val/test use the same mask geometry; different masks corrupt spectral coefficients
|
| 47 |
+
- **Scalar SHT with Q/U stacking:** torch-harmonics v0.8.0 VectorSHT is slow, so Q/U are stacked as independent channels
|
| 48 |
+
|
| 49 |
+
See [ARCHITECTURE.md](https://github.com/zonca/torch-harmonics-healpix/blob/main/ARCHITECTURE.md) for the full comparison with NNhealpix.
|
| 50 |
+
|
| 51 |
+
## Benchmark Results
|
| 52 |
+
|
| 53 |
+
### Test 2 β Polarization (SpectralCNN dominates)
|
| 54 |
+
|
| 55 |
+
| f_sky | SpectralCNN (β_Ep / β_Bp) | NNhealpix | Improvement |
|
| 56 |
+
|-------|---------------------------|-----------|-------------|
|
| 57 |
+
| 1.0 | **1.69% / 1.53%** | 2.7% / 2.7% | 37% / 43% |
|
| 58 |
+
| 0.5 | **1.95% / 1.91%** | 3.9% / 3.9% | 50% / 51% |
|
| 59 |
+
| 0.2 | **2.15% / 2.17%** | 5.3% / 5.3% | 59% / 59% |
|
| 60 |
+
| 0.1 | **2.56% / 2.70%** | 6.4% / 6.4% | 60% / 58% |
|
| 61 |
+
| 0.05 | **3.01% / 3.11%** | 8.4% / 8.4% | 64% / 63% |
|
| 62 |
+
|
| 63 |
+
### Test 3 β Optical depth Ο
|
| 64 |
+
|
| 65 |
+
| Method | Ο % error |
|
| 66 |
+
|--------|----------|
|
| 67 |
+
| MCMC (paper) | 2.8% |
|
| 68 |
+
| **SpectralCNN** | **3.76%** |
|
| 69 |
+
| NNhealpix | 4.0% |
|
| 70 |
+
|
| 71 |
+
### Test 1 β Scalar maps (noise-free only)
|
| 72 |
+
|
| 73 |
+
| Ο_n | SpectralCNN | NNhealpix |
|
| 74 |
+
|-----|------------|-----------|
|
| 75 |
+
| 0 | **1.27%** | 1.3% |
|
| 76 |
+
| 5 | 3.58% | **2.9%** |
|
| 77 |
+
|
| 78 |
+
SpectralCNN wins for noise-free data but loses at high noise because SHT spreads local noise globally, while pixel-space convolution naturally filters it.
|
| 79 |
+
|
| 80 |
+
See [BENCHMARKS.md](https://github.com/zonca/torch-harmonics-healpix/blob/main/BENCHMARKS.md) for full tables including MCMC baselines.
|
| 81 |
+
|
| 82 |
+
## Usage
|
| 83 |
+
|
| 84 |
+
### Installation
|
| 85 |
+
|
| 86 |
+
```bash
|
| 87 |
+
uv venv .venv --python 3.11
|
| 88 |
+
source .venv/bin/activate
|
| 89 |
+
uv pip install torch==2.6.0 torchvision==0.21.0 --index-url https://download.pytorch.org/whl/cu124
|
| 90 |
+
uv pip install torch-harmonics==0.8.0 --no-deps
|
| 91 |
+
uv pip install healpy h5py scipy huggingface_hub
|
| 92 |
+
uv pip install -e "git+https://github.com/zonca/torch-harmonics-healpix#egg=torch-harmonics-healpix"
|
| 93 |
+
```
|
| 94 |
+
|
| 95 |
+
### Download and Load
|
| 96 |
+
|
| 97 |
+
```python
|
| 98 |
+
import torch
|
| 99 |
+
import numpy as np
|
| 100 |
+
from huggingface_hub import hf_hub_download
|
| 101 |
+
from torch_harmonics_healpix.models import SpectralCNN
|
| 102 |
+
|
| 103 |
+
# Download model weights
|
| 104 |
+
model_path = hf_hub_download(
|
| 105 |
+
repo_id="zonca/torch-harmonics-healpix",
|
| 106 |
+
filename="models/test2_v2_fix_fsky1.0.pt",
|
| 107 |
+
)
|
| 108 |
+
|
| 109 |
+
# Create model with matching architecture
|
| 110 |
+
model = SpectralCNN(
|
| 111 |
+
in_channels=3, # Test 1: 1, Test 2/3: 3 (Q, U, mask)
|
| 112 |
+
out_channels=1, # Test 1/3: 1, Test 2: 2
|
| 113 |
+
nside=16,
|
| 114 |
+
hidden_channels=32,
|
| 115 |
+
num_blocks=3,
|
| 116 |
+
inpaint=False, # True for f_sky < 1.0
|
| 117 |
+
)
|
| 118 |
+
|
| 119 |
+
# Load weights
|
| 120 |
+
state_dict = torch.load(model_path, map_location="cpu")
|
| 121 |
+
model.load_state_dict(state_dict)
|
| 122 |
+
model.eval()
|
| 123 |
+
|
| 124 |
+
# Run inference on a HEALPix Nside=16 map (3072 pixels)
|
| 125 |
+
# Stack [Q, U, mask] as 3 channels
|
| 126 |
+
input_tensor = torch.from_numpy(
|
| 127 |
+
np.stack([q_map, u_map, mask], axis=0).astype(np.float32)
|
| 128 |
+
).unsqueeze(0) # [1, 3, 3072]
|
| 129 |
+
|
| 130 |
+
with torch.no_grad():
|
| 131 |
+
prediction = model(input_tensor)
|
| 132 |
+
|
| 133 |
+
print(f"Predicted parameter: {prediction.item():.4f}")
|
| 134 |
+
```
|
| 135 |
+
|
| 136 |
+
## Training
|
| 137 |
+
|
| 138 |
+
To retrain from scratch (e.g., for different noise levels or f_sky values):
|
| 139 |
+
|
| 140 |
+
```bash
|
| 141 |
+
# Test 1: β_peak from T maps
|
| 142 |
+
python scripts/train_test1_v2.py --noise_std 0 --output results/test1_noise0.json
|
| 143 |
+
|
| 144 |
+
# Test 2: β_Ep/β_Bp from Q/U maps
|
| 145 |
+
python scripts/train_test2_v2.py --f_sky 0.5 --output results/test2_fsky0.5.json
|
| 146 |
+
|
| 147 |
+
# Test 3: Ο estimation (requires: pip install camb)
|
| 148 |
+
python scripts/train_test3_v2.py --f_sky 1.0 --output results/test3.json
|
| 149 |
+
```
|
| 150 |
+
|
| 151 |
+
Each script saves both `results/*.json` (metrics) and `results/*.pt` (model weights).
|
| 152 |
+
|
| 153 |
+
## Limitations
|
| 154 |
+
|
| 155 |
+
- **HEALPix Nside=16 only** (3072 pixels) β not tested at higher resolutions
|
| 156 |
+
- **torch-harmonics v0.8.0** β VectorSHT too slow; uses scalar SHT with stacked Q/U channels
|
| 157 |
+
- **No explicit E/B separation** β relies on spectral prior to learn E/B structure implicitly
|
| 158 |
+
- **Noise sensitivity** β SHT spreads local noise globally; pixel-space CNNs are more robust for high-noise scalar maps
|
| 159 |
+
- **Full-sky pre-trained models** β partial-sky models require retraining with `inpaint=True`
|
| 160 |
+
|
| 161 |
+
## Citation
|
| 162 |
+
|
| 163 |
+
If you use these models, please cite:
|
| 164 |
+
|
| 165 |
+
```bibtex
|
| 166 |
+
@article{krachmalnicoff2019,
|
| 167 |
+
title={Convolutional Neural Networks on the {HEALPix} sphere: a pixel-based approach for CMB data analysis},
|
| 168 |
+
author={Krachmalnicoff, N. and Tomasi, M.},
|
| 169 |
+
journal={Astronomy \& Astrophysics},
|
| 170 |
+
volume={624},
|
| 171 |
+
pages={A97},
|
| 172 |
+
year={2019},
|
| 173 |
+
doi={10.1051/0004-6361/201834952},
|
| 174 |
+
url={https://arxiv.org/abs/1902.04083}
|
| 175 |
+
}
|
| 176 |
+
```
|
| 177 |
+
|
| 178 |
+
## License
|
| 179 |
+
|
| 180 |
+
MIT
|