-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
374ddc3
commit 859c49d
Showing
4 changed files
with
141 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,77 @@ | ||
import numpy as np | ||
import os | ||
import types | ||
import pyccl as ccl | ||
from firecrown.likelihood.likelihood import load_likelihood | ||
from firecrown.likelihood.likelihood import load_likelihood_from_module_type | ||
from firecrown.parameters import ParamsMap | ||
|
||
|
||
# creates the smokescreen object | ||
class Smokescreen(): | ||
""" | ||
Class for calling a smokescreen on the measured data-vector. | ||
""" | ||
def __init__(self, cosmo, sacc_data, likelihood, systematics_dict, shifts_dict, **kwargs): | ||
""" | ||
Parameters | ||
---------- | ||
cosmo : pyccl.Cosmology | ||
Cosmology object from CCL with a fiducial cosmology. | ||
sacc_data : sacc.sacc.Sacc | ||
Data-vector to be blinded. | ||
likelihood : str or module | ||
path to the likelihood or a module containing the likelihood | ||
must contain both `build_likelihood` and `compute_theory_vector` methods | ||
systematics_dict : dict | ||
Dictionary of systematics names and corresponding fiducial values. | ||
shifts_dict : dict | ||
Dictionary of parameter names and corresponding shift widths. If the | ||
shifts are single values, the dictionary values should be the shift | ||
widths. If the shifts are tuples of values, the dictionary values | ||
should be the (lower, upper) bounds of the shift widths. | ||
""" | ||
# save the cosmology | ||
self.cosmo = cosmo | ||
# save the data-vector | ||
self.sacc_data = sacc_data | ||
# load the likelihood | ||
self.likelihood, self.tools = self.load_likelihood(likelihood, **kwargs) | ||
|
||
# # load the systematics | ||
# self.systematics = self.load_systematics(systematics_dict) | ||
# # load the shifts | ||
# self.shifts = self.load_shifts(shifts_dict) | ||
# # create the smokescreen data-vector | ||
# self.smokescreen_data = self.create_smokescreen_data() | ||
|
||
def load_likelihood(self, likelihood): | ||
""" | ||
Loads the likelihood either from a python module or from a file. | ||
Parameters | ||
---------- | ||
likelihood : str or module | ||
path to the likelihood or a module containing the likelihood | ||
must contain both `build_likelihood` and `compute_theory_vector` methods | ||
""" | ||
if type(likelihood) == str: | ||
# check if the file can be found | ||
if not os.path.isfile(likelihood): | ||
raise FileNotFoundError(f'Could not find file {likelihood}') | ||
# load the likelihood from the file | ||
likelihood, tools = load_likelihood(likelihood, None) | ||
# check if the likehood has a compute_vector method | ||
if not hasattr(likelihood, 'compute_theory_vector'): | ||
raise AttributeError('Likelihood does not have a compute_vector method') | ||
return likelihood, tools | ||
elif isinstance(likelihood, types.ModuleType): | ||
# check if the module has a build_likelihood method | ||
if not hasattr(likelihood, 'build_likelihood'): | ||
raise AttributeError('Likelihood does not have a build_likelihood method') | ||
# tries to load the likelihood from the module | ||
likelihood, tools = load_likelihood_from_module_type(likelihood, None) | ||
# check if the likehood has a compute_vector method | ||
if not hasattr(likelihood, 'compute_theory_vector'): | ||
raise AttributeError('Likelihood does not have a compute_vector method') | ||
return likelihood, tools |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import pytest | ||
import types | ||
import sacc | ||
from firecrown.likelihood.likelihood import Likelihood | ||
from firecrown.modeling_tools import ModelingTools | ||
from blinding.smokescreen import Smokescreen | ||
|
||
class EmptyLikelihood(Likelihood): | ||
""" | ||
empty mock likelihood based on: | ||
https://github.com/LSSTDESC/firecrown/blob/master/tests/likelihood/lkdir/lkmodule.py | ||
""" | ||
def __init__(self): | ||
self.nothing = 1.0 | ||
super().__init__() | ||
def read(self, sacc_data: sacc.Sacc): | ||
pass | ||
def compute_loglike(self, ModellingTools): | ||
return -self.nothing*2.0 | ||
def compute_theory_vector(self): | ||
return self.nothing | ||
|
||
class MockLikelihoodModule(types.ModuleType): | ||
def build_likelihood(self, *args, **kwargs): | ||
mocktools = ModelingTools() | ||
mocklike = EmptyLikelihood() | ||
return mocklike, mocktools | ||
|
||
def compute_theory_vector(self): | ||
return None | ||
|
||
def test_smokescreen_init(): | ||
# Create mock inputs | ||
cosmo = "cosmo" | ||
sacc_data = "sacc_data" | ||
likelihood = MockLikelihoodModule("mock_likelihood") | ||
systematics_dict = {"systematic1": 0.1} | ||
shifts_dict = {"param1": 1} | ||
|
||
# Check that Smokescreen can be instantiated with valid inputs | ||
smokescreen = Smokescreen(cosmo, sacc_data, likelihood, systematics_dict, shifts_dict) | ||
assert isinstance(smokescreen, Smokescreen) | ||
|
||
# Check that Smokescreen raises an error when given an invalid likelihood | ||
with pytest.raises(AttributeError): | ||
invalid_likelihood = types.ModuleType("invalid_likelihood") | ||
Smokescreen(cosmo, sacc_data, invalid_likelihood, systematics_dict, shifts_dict) |