Skip to content

Commit 8f38eec

Browse files
updated spherical l1-norm code to implement l2 normalization
1 parent 7dfe0d7 commit 8f38eec

File tree

1 file changed

+27
-24
lines changed

1 file changed

+27
-24
lines changed

pycs/astro/wl/hos_peaks_l1.py

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
from pycs.sparsity.sparse2d.dct_inpainting import dct_inpainting
2121
from pycs.misc.im_isospec import *
2222
from pycs.astro.wl.mass_mapping import *
23-
from pycs.sparsity.mrs.mrs_starlet import mrs_uwttrans
23+
from pycs.sparsity.mrs.mrs_starlet import mrs_uwttrans, CMRStarlet
24+
import healpy as hp # Added for Nside calculation
2425

2526

2627
def get_wt_noiselevel(W, NoiseSigmaMap, Mask=None):
@@ -581,18 +582,18 @@ def get_wtl1(self, nbins=None, Mask=None, min_snr=None, max_snr=None):
581582
self.l1norm = np.array(l1_coll)
582583

583584

584-
def get_norm_wtl1_sphere(
585+
def get_wtl1_sphere(
585586
Map,
586587
nscales,
587588
nbins=None,
588589
Mask=None,
589590
min_snr=None,
590591
max_snr=None,
591-
path="/.",
592-
normalize_energy=False,
593592
):
594593
"""
595-
Computes L1 norms of wavelet transform coefficients at different scales for a HEALPix map. The wavelet transform is performed using the undecimated wavelet transform.
594+
Computes L1 norms of normalized wavelet transform coefficients at different scales for a HEALPix map.
595+
The wavelet transform is performed using CMRStarlet. The normalization ensures || Psi_j ||^2 = 1.
596+
The values binned are C.coef[j] / C.TabNorm[j].
596597
597598
Parameters
598599
----------
@@ -605,13 +606,11 @@ def get_norm_wtl1_sphere(
605606
Mask : array_like, optional
606607
Mask indicating where we have observations. Only pixels where Mask != 0 are considered.
607608
min_snr : float, optional
608-
Minimum value for binning. If None, uses the minimum value in the coefficients.
609+
Minimum value for binning the normalized coefficients (C.coef[j] / C.TabNorm[j]).
610+
If None, uses the minimum value in the coefficients for the current scale.
609611
max_snr : float, optional
610-
Maximum value for binning. If None, uses the maximum value in the coefficients.
611-
path : str, optional
612-
Path to the directory for temporary files. Default is "/.".
613-
normalize_energy : bool, optional
614-
If True, normalize wavelet coefficients by their energy. Default is False.
612+
Maximum value for binning the normalized coefficients (C.coef[j] / C.TabNorm[j]).
613+
If None, uses the maximum value in the coefficients for the current scale.
615614
616615
Returns
617616
-------
@@ -625,33 +624,37 @@ def get_norm_wtl1_sphere(
625624
if nbins is None:
626625
nbins = 40
627626

628-
# Perform undecimated wavelet transform on the spherical map
629-
WT = mrs_uwttrans(Map, nscale=nscales, verbose=False, path=path, cxx=False)
627+
# Determine Nside from the input map
628+
Nside = hp.npix2nside(Map.shape[0])
629+
630+
# Initialize and perform CMRStarlet transform
631+
C = CMRStarlet()
632+
C.init_starlet(Nside, nscale=nscales)
633+
C.transform(Map)
630634

631635
l1norm_coll = []
632636
bins_coll = []
633637

634638
# Loop through each scale of the wavelet transform
635639
for i in range(nscales):
636-
ScaleCoeffs = WT[i] # coefficients for the i-th scale
640+
# Get normalized wavelet coefficients for the i-th scale
641+
if C.TabNorm[i] == 0: # Avoid division by zero if TabNorm is zero
642+
ScaleCoeffs = C.coef[i].copy() # Or handle as an error/warning
643+
else:
644+
ScaleCoeffs = C.coef[i] / C.TabNorm[i]
637645

638646
# Apply the mask if provided
639647
if Mask is not None:
648+
if Mask.shape[0] != ScaleCoeffs.shape[0]:
649+
raise ValueError("Mask shape does not match coefficient map shape.")
640650
ScaleCoeffs = ScaleCoeffs[Mask != 0]
641651

642-
# Normalize the wavelet scale to the same energy level if requested
643-
if normalize_energy:
644-
energy = np.sum(ScaleCoeffs**2)
645-
normalization_factor = np.sqrt(energy)
646-
if normalization_factor > 0:
647-
ScaleCoeffs = ScaleCoeffs / normalization_factor
648-
649652
# Set the minimum and maximum values based on inputs or defaults
650-
min_val = min_snr if min_snr is not None else np.min(ScaleCoeffs)
651-
max_val = max_snr if max_snr is not None else np.max(ScaleCoeffs)
653+
current_min_val = min_snr if min_snr is not None else np.min(ScaleCoeffs)
654+
current_max_val = max_snr if max_snr is not None else np.max(ScaleCoeffs)
652655

653656
# Define thresholds and bins
654-
thresholds = np.linspace(min_val, max_val, nbins + 1)
657+
thresholds = np.linspace(current_min_val, current_max_val, nbins + 1)
655658
bins = 0.5 * (thresholds[:-1] + thresholds[1:])
656659

657660
# Digitize the values into bins

0 commit comments

Comments
 (0)