Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Blind output with user-provided offsets #10

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions cosmosis/output/cosmomc_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@


class CosmoMCOutput(TextColumnOutput):
def __init__(self, filename, rank=0, nchain=1, delimiter=' ', lock=True, resume=False):
def __init__(self, filename, rank=0, nchain=1, delimiter=' ', lock=True, resume=False, blinding_offset_file=None):
super(CosmoMCOutput, self).__init__(filename, rank, nchain, '', lock=lock, resume=resume)
if filename.endswith(self.FILE_EXTENSION):
filename = filename[:-len(self.FILE_EXTENSION)]
Expand All @@ -19,6 +19,10 @@ def __init__(self, filename, rank=0, nchain=1, delimiter=' ', lock=True, resu
self._paramfile = None
self._last_params = None
self._multiplicity = 0
if blinding_offset_file is not None:
self._blinding_offsets = np.load(blinding_offset_file)
else:
self._blinding_offsets = None

def _close(self):
self._write_parameters_multiplicity()
Expand Down Expand Up @@ -97,5 +101,6 @@ def load_from_options(cls, options):
def from_options(cls, options, resume=False):
if resume:
raise ValueError("Cannot resume from cosmomc files yet")
return super(CosmoMCOutput,cls).from_options(options, resume=False)
blinding_offset_file = options.get('blinding_offsets', None)
return super(CosmoMCOutput,cls).from_options(options, resume=False, blinding_offset_file=blinding_offset_file)

10 changes: 8 additions & 2 deletions cosmosis/output/fits_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class FitsOutput(OutputBase):
FILE_EXTENSION = ".fits"
_aliases = ["fits"]

def __init__(self, filename, rank=0, nchain=1, clobber=True):
def __init__(self, filename, rank=0, nchain=1, clobber=True, blinding_offset_file=None):
super(FitsOutput, self).__init__()

#If filename already ends in .txt then remove it for a moment
Expand All @@ -56,6 +56,11 @@ def __init__(self, filename, rank=0, nchain=1, clobber=True):
self._metadata = OrderedDict()
self._final_metadata = OrderedDict()

if blinding_offset_file is not None:
self._blinding_offsets = np.load(blinding_offset_file)
else:
self._blinding_offsets = None

def _close(self):
self._flush_metadata(self._final_metadata)
self._final_metadata={}
Expand Down Expand Up @@ -126,8 +131,9 @@ def from_options(cls, options, resume=False):
delimiter = options.get('delimiter', '\t')
rank = options.get('rank', 0)
nchain = options.get('parallel', 1)
blinding_offset_file = options.get('blinding_offsets', None)
clobber = utils.boolean_string(options.get('clobber', True))
return cls(filename, rank, nchain, clobber=clobber)
return cls(filename, rank, nchain, clobber=clobber, blinding_offset_file=blinding_offset_file)

@classmethod
def load_from_options(cls, options):
Expand Down
10 changes: 8 additions & 2 deletions cosmosis/output/in_memory_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,18 @@

class InMemoryOutput(OutputBase):
_aliases = ["memory"]
def __init__(self):
def __init__(self, blinding_offset_file=None):
super(InMemoryOutput,self).__init__()
self.rows = []
self.meta = {}
self.final_meta = {}
self.comments = []

if blinding_offset_file is not None:
self._blinding_offsets = np.load(blinding_offset_file)
else:
self._blinding_offsets = None

def _write_parameters(self, params):
self.rows.append(params)

Expand All @@ -33,7 +38,8 @@ def __getitem__(self, key_or_index):
def from_options(cls, options, resume=False):
if resume:
raise ValueError("Cannot resume from in-memory output")
return cls()
blinding_offset_file = options.get('blinding_offsets', None)
return cls(blinding_offset_file=blinding_offset_file)

@classmethod
def load_from_options(cls, options):
Expand Down
10 changes: 9 additions & 1 deletion cosmosis/output/null_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

class NullOutput(OutputBase):
_aliases = ["none"]

def __init__(self, blinding_offset_file=None):
if blinding_offset_file is not None:
self._blinding_offsets = np.load(blinding_offset_file)
else:
self._blinding_offsets = None

def _write_parameters(self, params):
pass

Expand All @@ -18,7 +25,8 @@ def _write_final(self, key, value, comment):
def from_options(cls, options, resume=False):
if resume:
raise ValueError("Cannot resume from null output")
return cls()
blinding_offset_file = options.get('blinding_offsets', None)
return cls(blinding_offset_file=blinding_offset_file)

@classmethod
def load_from_options(cls, options):
Expand Down
7 changes: 7 additions & 0 deletions cosmosis/output/output_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,13 @@ def parameters(self, *param_groups):
if not self.begun_sampling:
self._begun_sampling(params)
self.begun_sampling=True

#Blind output by applying offsets
SebastianBocquet marked this conversation as resolved.
Show resolved Hide resolved
if self._blinding_offsets is not None:
if len(self._blinding_offsets)!=len(params):
raise ValueError("Length of blinding offsets %d does not match number of output parameters %d"%(len(self._blinding_offsets), len(params)))
params+= self._blinding_offsets

#Pass to the subclasses to write output
self._write_parameters(params)

Expand Down
11 changes: 9 additions & 2 deletions cosmosis/output/text_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ class TextColumnOutput(OutputBase):
FILE_EXTENSION = ".txt"
_aliases = ["text", "txt"]

def __init__(self, filename, rank=0, nchain=1, delimiter='\t', lock=True, resume=False):
def __init__(self, filename, rank=0, nchain=1, delimiter='\t', lock=True, resume=False,
blinding_offset_file=None):
super(TextColumnOutput, self).__init__()
self.delimiter = delimiter

Expand Down Expand Up @@ -46,6 +47,10 @@ def __init__(self, filename, rank=0, nchain=1, delimiter='\t', lock=True, resume
print("Note: You set resume=T but the file {} does not exist so I will start a new one".format(self._filename))
self._file = open(self._filename, "w")
self.resumed = False
if blinding_offset_file is not None:
self._blinding_offsets = np.load(blinding_offset_file)
else:
self._blinding_offsets = None
if lock:
try:
self.lock_file(self._file)
Expand Down Expand Up @@ -143,8 +148,10 @@ def from_options(cls, options, resume=False):
delimiter = options.get('delimiter', '\t')
rank = options.get('rank', 0)
nchain = options.get('parallel', 1)
blinding_offset_file = options.get('blinding_offsets', None)
lock = utils.boolean_string(options.get('lock', True))
return cls(filename, rank, nchain, delimiter=delimiter, lock=lock, resume=resume)
return cls(filename, rank, nchain, delimiter=delimiter, lock=lock, resume=resume,
blinding_offset_file=blinding_offset_file)

@classmethod
def load_from_options(cls, options):
Expand Down