|
20 | 20 | from pycs.sparsity.sparse2d.dct_inpainting import dct_inpainting |
21 | 21 | from pycs.misc.im_isospec import * |
22 | 22 | from pycs.astro.wl.mass_mapping import * |
| 23 | +from pycs.sparsity.mrs.mrs_tools import mrs_uwttrans |
23 | 24 |
|
24 | 25 |
|
25 | 26 | def get_wt_noiselevel(W, NoiseSigmaMap, Mask=None): |
@@ -580,6 +581,86 @@ def get_wtl1(self, nbins=None, Mask=None, min_snr=None, max_snr=None): |
580 | 581 | self.l1norm = np.array(l1_coll) |
581 | 582 |
|
582 | 583 |
|
| 584 | +def get_norm_wtl1_sphere( |
| 585 | + Map, nscales, nbins=None, Mask=None, min_snr=None, max_snr=None, path="/." |
| 586 | +): |
| 587 | + """ |
| 588 | + Computes L1 norms of wavelet transform coefficients at different scales for a HEALPix map. The wavelet transform is performed using the undecimated wavelet transform. |
| 589 | +
|
| 590 | + Parameters |
| 591 | + ---------- |
| 592 | + Map : array_like |
| 593 | + HEALPix map to analyze. |
| 594 | + nscales : int |
| 595 | + Number of wavelet scales to use. |
| 596 | + nbins : int, optional |
| 597 | + Number of bins for the histogram. Default is 40. |
| 598 | + Mask : array_like, optional |
| 599 | + Mask indicating where we have observations. Only pixels where Mask != 0 are considered. |
| 600 | + min_snr : float, optional |
| 601 | + Minimum value for binning. If None, uses the minimum value in the normalized coefficients. |
| 602 | + max_snr : float, optional |
| 603 | + Maximum value for binning. If None, uses the maximum value in the normalized coefficients. |
| 604 | + path : str, optional |
| 605 | + Path to the directory for temporary files. Default is "/.". |
| 606 | +
|
| 607 | + Returns |
| 608 | + ------- |
| 609 | + tuple of numpy arrays |
| 610 | + (bins, l1norm) where: |
| 611 | + - bins[i] are the bin centers for scale i |
| 612 | + - l1norm[i] are the L1 norms for each bin at scale i |
| 613 | + """ |
| 614 | + |
| 615 | + # Set default for nbins if not provided |
| 616 | + if nbins is None: |
| 617 | + nbins = 40 |
| 618 | + |
| 619 | + # Perform undecimated wavelet transform on the spherical map |
| 620 | + WT = mrs_uwttrans(Map, verbose=False, path=path) |
| 621 | + |
| 622 | + l1norm_coll = [] |
| 623 | + bins_coll = [] |
| 624 | + |
| 625 | + # Loop through each scale of the wavelet transform |
| 626 | + for i in range(nscales): |
| 627 | + ScaleCoeffs = WT[i] # coefficients for the i-th scale |
| 628 | + |
| 629 | + # Apply the mask if provided |
| 630 | + if Mask is not None: |
| 631 | + ScaleCoeffs = ScaleCoeffs[Mask != 0] |
| 632 | + |
| 633 | + # Normalize the wavelet scale to the same energy level |
| 634 | + energy = np.sum(ScaleCoeffs**2) |
| 635 | + normalization_factor = np.sqrt(energy) |
| 636 | + if normalization_factor > 0: |
| 637 | + ScaleCoeffs_normalized = ScaleCoeffs / normalization_factor |
| 638 | + |
| 639 | + # Set the minimum and maximum values based on inputs or defaults |
| 640 | + min_val = min_snr if min_snr is not None else np.min(ScaleCoeffs_normalized) |
| 641 | + max_val = max_snr if max_snr is not None else np.max(ScaleCoeffs_normalized) |
| 642 | + |
| 643 | + # Define thresholds and bins |
| 644 | + thresholds = np.linspace(min_val, max_val, nbins + 1) |
| 645 | + bins = 0.5 * (thresholds[:-1] + thresholds[1:]) |
| 646 | + |
| 647 | + # Digitize the values into bins |
| 648 | + digitized = np.digitize(ScaleCoeffs_normalized, thresholds) |
| 649 | + |
| 650 | + # Calculate the l1 norm for each bin |
| 651 | + bin_l1_norm = [ |
| 652 | + np.sum(np.abs(ScaleCoeffs_normalized[digitized == j])) |
| 653 | + for j in range(1, len(thresholds)) |
| 654 | + ] |
| 655 | + |
| 656 | + # Store the bins and l1 norms for this scale |
| 657 | + bins_coll.append(bins) |
| 658 | + l1norm_coll.append(bin_l1_norm) |
| 659 | + |
| 660 | + # Return the bins and l1 norms for each scale |
| 661 | + return np.array(bins_coll), np.array(l1norm_coll) |
| 662 | + |
| 663 | + |
583 | 664 | ############# TESTS routine ############ |
584 | 665 |
|
585 | 666 |
|
|
0 commit comments