Skip to content

Commit

Permalink
making beam file paths absolute paths
Browse files Browse the repository at this point in the history
  • Loading branch information
sgiardie committed Sep 12, 2024
1 parent 320ad11 commit 674bfbe
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 35 deletions.
52 changes: 21 additions & 31 deletions mflike/foreground.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,16 +65,15 @@
mflike.BandpowerForeground:
beam_profile:
Gaussian_beam: dict/False/null
beam_from_file: "filename"/False/null
beam_from_file: filename/False/null
There are several options:
* reading the beams from the sacc file (``Gaussian_beam: False/null``, ``beam_from_file: False/null``).
The beams have to be stored in the ``sacc.tracers[exp].beam`` tracer
(this is not working so far, since the sacc beam tracer doesn't like an array(freq, ell))
* reading the beams from an external yaml file (``Gaussian_beam: False/null``, ``beam_from_file: "filename"``).
Do not use the ".yaml" extension nor the path to the file, which has to be the same as the
data path. The yaml file has to be a dictionary ``{"{exp}_s0": {"nu": nu,
"beams": array(freqs, ells+2)}, "{exp}_s2": {"nu": nu, "beams": array(freqs, ells+2)},...}``
* reading the beams from an external yaml file (``Gaussian_beam: False/null``, ``beam_from_file: filename``). ``filename`` has to be the absolute path for the file, without the ``.yaml`` extension,
which is automatically added by the code. The yaml file has to be a dictionary
``{"{exp}_s0": {"nu": nu, "beams": array(freqs, ells+2)}, "{exp}_s2": {"nu": nu, "beams": array(freqs, ells+2)},...}``
* computing the beams as Gaussian beams (``Gaussian_beam: dict``, ``beam_from_file: ...``). When
``Gaussian_beam`` is not empty, the beam is automatically computed within the code. Both T and
polarization Gaussian beams are computed through ``healpy.gauss_beam``. We need to pass a
Expand Down Expand Up @@ -115,11 +114,11 @@
.. code-block:: yaml
beam_profile:
Bandpass_shifted_beams: "bandpass_shifted_beams"
Bandpass_shifted_beams: bandpass_shifted_beams
Gaussian_beam: dict/False/null
beam_from_file: "filename"/False/null
beam_from_file: filename/False/null
where the "bandpass_shifted_beams.yaml" file is structured as:
where the ``bandpass_shifted_beams.yaml`` file is structured as:
.. code-block:: yaml
Expand All @@ -141,7 +140,7 @@
alpha: ...
...
The "bandpass_shifted_beams.yaml" file has to be saved in the same path as the data.
``bandpass_shifted_beams`` has to be an absolute path, without the ``.yaml`` extension, which is added by the code.
It is important the keys of ``beam_profile["Bandpass_shifted_beams"]["{exp}_s0/2"]["beams"]`` are strings of floats representing the value of :math:`\Delta \nu` (if they are strings of int the code to read the associated beams would not work).
"""
Expand Down Expand Up @@ -478,7 +477,6 @@ def must_provide(self, **requirements):
"requested_cls must be the same in Foreground and MFLike")
self.ells = req.get("ells", self.ells)
self.experiments = req.get("experiments", self.experiments)
# self.data_folder = req.get("data_folder", self.data_folder)


class BandpowerForeground(Foreground):
Expand All @@ -488,7 +486,6 @@ class BandpowerForeground(Foreground):
bands: dict = None
beams: dict = None
beam_profile: dict = None
data_folder: Optional[str]

def initialize(self):
super().initialize()
Expand All @@ -499,6 +496,7 @@ def initialize(self):
self._initialized = False
self.init_bandpowers()


def init_bandpowers(self):
self.use_top_hat_band = bool(self.top_hat_band)
# Parameters for band integration
Expand Down Expand Up @@ -528,14 +526,7 @@ def init_bandpowers(self):
# this has to be present in case bandpass shifts != 0
self.bandsh_beams_path = self.beam_profile.get("Bandpass_shifted_beams")
if self.bandsh_beams_path:
if self.data_folder is not None:
self.bandpass_shifted_beams = self._read_yaml_file(self.bandsh_beams_path)
else:
if self._initialized:
self.log.info("The data path has not been found")



self.bandpass_shifted_beams = self._read_yaml_file(self.bandsh_beams_path)

self._bandint_shift_params = [f"bandint_shift_{f}" for f in self.experiments]
# default bandpass when shift is 0
Expand All @@ -555,9 +546,8 @@ def must_provide(self, **requirements):
self.beams = req.get("beams", self.beams)
self.top_hat_band = req.get("top_hat_band", self.top_hat_band)
self.beam_profile = req.get("beam_profile", self.beam_profile)
self.data_folder = req.get("data_folder", self.data_folder)
self.init_bandpowers()

def get_can_support_params(self):
return self._bandint_shift_params

Expand Down Expand Up @@ -728,9 +718,8 @@ def _bandpass_construction(self, _initialize=False, **params):
def _read_yaml_file(self, file_path):
import yaml

data_path = self.data_folder
filename = os.path.join(data_path, "%s.yaml" % file_path)
if not os.path.exists(filename):
filename = "%s.yaml" % file_path
if not os.path.exists("%s.yaml" % file_path):
raise ValueError("File " + filename + " does not exist!")

with open(filename, "r") as f:
Expand All @@ -757,13 +746,14 @@ def _init_beam_from_file(self):
else:
self.beams = self._read_yaml_file(self.beam_file)

#checking that the freq array is compatible with the bandpass one
for exp in self.experiments:
# checking nu is the same as the bandpass one
if not np.allclose(self.beams[f"{exp}_s0"]['nu'], self.bands[f"{exp}_s0"]['nu'], atol = 1e-5):
raise LoggedError(self.log, f"Frequency array for beam {exp}_s0 is not the same as the bandpass one!")
if not np.allclose(self.beams[f"{exp}_s2"]['nu'], self.bands[f"{exp}_s2"]['nu'], atol = 1e-5):
raise LoggedError(self.log, f"Frequency array for beam {exp}_s2 is not the same as the bandpass one!")
if self._initialized:
#checking that the freq array is compatible with the bandpass one
for exp in self.experiments:
# checking nu is the same as the bandpass one
if not np.allclose(self.beams[f"{exp}_s0"]['nu'], self.bands[f"{exp}_s0"]['nu'], atol = 1e-5):
raise LoggedError(self.log, f"Frequency array for beam {exp}_s0 is not the same as the bandpass one!")
if not np.allclose(self.beams[f"{exp}_s2"]['nu'], self.bands[f"{exp}_s2"]['nu'], atol = 1e-5):
raise LoggedError(self.log, f"Frequency array for beam {exp}_s2 is not the same as the bandpass one!")

def _init_gauss_beams(self):
"""
Expand Down
3 changes: 1 addition & 2 deletions mflike/mflike.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,7 @@ def get_fg_requirements(self):
"requested_cls": self.requested_cls,
"experiments": self.experiments,
"bands": self.bands,
"beams": self.beams,
"data_folder": self.data_folder}
"beams": self.beams}

def get_requirements(self):
r"""
Expand Down
5 changes: 3 additions & 2 deletions mflike/tests/test_mflike.py
Original file line number Diff line number Diff line change
Expand Up @@ -389,8 +389,9 @@ def compute_FWHM(nu):
},
"theory": {"camb": {"extra_args": {"lens_potential_accuracy": 1}},
"mflike.BandpowerForeground":{
"beam_profile": {"Gaussian_beam": beam_params,
"Bandpass_shifted_beams": "LAT_beam_bandshift"},
"beam_profile": {"Gaussian_beam": beam_params,
"Bandpass_shifted_beams": packages_path +
"/data/MFLike/v0.8/LAT_beam_bandshift"},
},
},
"params": cosmo_params | params,
Expand Down

0 comments on commit 674bfbe

Please sign in to comment.