Skip to content

Commit

Permalink
add create_covmat method
Browse files Browse the repository at this point in the history
  • Loading branch information
yusuke-takase committed Aug 7, 2024
1 parent 44e55ff commit 3b43c9f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
Binary file modified sbm/__pycache__/sbm.cpython-310.pyc
Binary file not shown.
26 changes: 26 additions & 0 deletions sbm/sbm.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ def __init__(self):
self.h = []
self.spins = []
self.compled_fields = None
self.nside = None
self.npix = None
self.mdim = None
self.ndet = None
self.all_channels = [
Expand Down Expand Up @@ -100,6 +102,8 @@ def load_det(cls, base_path: str, filename: str):
instance.h = f['h'][:, 0, :]
instance.h[np.isnan(instance.h)] = 1.0
instance.spins = f['quantify']['n'][()]
instance.nside = hp.npix2nside(len(instance.hitmap))
instance.npix = hp.nside2npix(instance.nside)
return instance

@classmethod
Expand All @@ -121,6 +125,8 @@ def load_channel(cls, base_path: str, channel: str):
instance.ndet = len(filenames)
instance.hitmap = np.zeros_like(first_sf.hitmap)
instance.h = np.zeros_like(first_sf.h)
instance.nside = hp.npix2nside(len(instance.hitmap))
instance.npix = hp.nside2npix(instance.nside)
for filename in filenames:
sf = cls.load_det(dirpath, filename)
instance.hitmap += sf.hitmap
Expand Down Expand Up @@ -159,6 +165,8 @@ def load_full_FPU(cls, base_path: str, channel_list: list, max_workers=None):
instance = cls()
hitmap = np.zeros_like(crosslink_channels[0].hitmap)
h = np.zeros_like(crosslink_channels[0].h)
instance.nside = hp.npix2nside(len(instance.hitmap))
instance.npix = hp.nside2npix(instance.nside)
ndet = 0
for sf in crosslink_channels:
hitmap += sf.hitmap
Expand All @@ -173,6 +181,8 @@ def load_full_FPU(cls, base_path: str, channel_list: list, max_workers=None):
def initialize(self, mdim):
self.hitmap = np.zeros_like(self.hitmap)
self.h = np.zeros_like(self.h)
self.nside = hp.npix2nside(len(self.hitmap))
self.npix = hp.nside2npix(self.nside)
self.spins = np.zeros_like(self.spins)
self.mdim = mdim
self.ndet = 0
Expand Down Expand Up @@ -219,6 +229,22 @@ def get_covmat(self, mdim):
raise ValueError("mdim is 2 or 3 only supported")
return covmat

def create_covmat(self, base_spin: list):
"""Get the covariance matrix of the detector in `mdim`x`mdim` matrix form
Args:
base_spin (list): list of spin to create the covariance matrix
"""
base_spin = np.array(base_spin)
waits = np.array([0.5 if x != 0 else 1.0 for x in base_spin])
spin_mat = base_spin[np.newaxis,:] - base_spin[:,np.newaxis]
wait_mat = np.abs(waits[np.newaxis,:]) * np.abs(waits[:,np.newaxis])
covmat = np.zeros([len(base_spin),len(base_spin),self.npix], dtype=complex)
for i in range(len(base_spin)):
for j in range(len(base_spin)):
covmat[i,j,:] = self.get_xlink(spin_mat[i,j])*wait_mat[i,j]
return covmat

def t2b(self):
"""Transform Top detector cross-link to Bottom detector cross-link
It assume top and bottom detector make a orthogonal pair.
Expand Down

0 comments on commit 3b43c9f

Please sign in to comment.