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 2 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
13 changes: 11 additions & 2 deletions cosmosis/output/in_memory_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@

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

self.apply_blinding_offsets = apply_blinding_offsets
if apply_blinding_offsets:
self._blinding_offsets = np.load(blinding_offset_file)

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

Expand All @@ -33,7 +37,12 @@ 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()
apply_blinding_offsets = utils.boolean_string(options.get('apply_blinding_offsets', False))
SebastianBocquet marked this conversation as resolved.
Show resolved Hide resolved
print("apply_blinding_offsets", type(apply_blinding_offsets), apply_blinding_offsets)
blinding_offset_file = options.get('blinding_offsets', None)
if apply_blinding_offsets & (blinding_offset_file is None):
raise RuntimeError("You set apply_blinding_offsets but did not provide blinding_offset_file")
return cls(apply_blinding_offsets=apply_blinding_offsets, 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.apply_blinding_offsets:
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
14 changes: 12 additions & 2 deletions cosmosis/output/text_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,13 @@ 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,
apply_blinding_offsets=False, blinding_offset_file=None):
super(TextColumnOutput, self).__init__()
self.delimiter = delimiter

self.apply_blinding_offsets = apply_blinding_offsets

#If filename already ends in .txt then remove it for a moment
if filename.endswith(self.FILE_EXTENSION):
filename = filename[:-len(self.FILE_EXTENSION)]
Expand Down Expand Up @@ -46,6 +49,8 @@ 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 apply_blinding_offsets:
self._blinding_offsets = np.load(blinding_offset_file)
if lock:
try:
self.lock_file(self._file)
Expand Down Expand Up @@ -143,8 +148,13 @@ def from_options(cls, options, resume=False):
delimiter = options.get('delimiter', '\t')
rank = options.get('rank', 0)
nchain = options.get('parallel', 1)
apply_blinding_offsets = utils.boolean_string(options.get('apply_blinding_offsets', False))
blinding_offset_file = options.get('blinding_offsets', None)
if apply_blinding_offsets & (blinding_offset_file is None):
raise RuntimeError("You set apply_blinding_offsets but did not provide blinding_offset_file")
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,
apply_blinding_offsets=apply_blinding_offsets, blinding_offset_file=blinding_offset_file)

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