From 648e4481cf110e41fbc58b83d8d21aef62f8b35c Mon Sep 17 00:00:00 2001 From: Arthur Loureiro Date: Tue, 6 Aug 2024 18:00:10 +0200 Subject: [PATCH 1/5] added metadata to the concealed sacc file --- .../sn_datavector_concealed_data_vector.fits | Bin 0 -> 20160 bytes src/smokescreen/datavector.py | 12 ++++++++++++ 2 files changed, 12 insertions(+) create mode 100644 examples/supernovae/sn_datavector_concealed_data_vector.fits diff --git a/examples/supernovae/sn_datavector_concealed_data_vector.fits b/examples/supernovae/sn_datavector_concealed_data_vector.fits new file mode 100644 index 0000000000000000000000000000000000000000..d8333f85b9e7fbef894d2558d2292c856a1a6b79 GIT binary patch literal 20160 zcmeHOdsI_L8duxe;;x={Rn%JD%N~`w6@i3D0GVrmL{oWbqV=(BypRh-^ME9Pfc1fk zvaLndSF1-|A1lSP?$V0%Xth&nr79M+O5LEgKJc}u$fMQ=w(Q*G&LzQ!Lfl>V?Box< znVJ0N_xMpaRD8vurP4ibrMak9j zxXH0mQk5buwkxoRpNQv|Xw1+v7K-W$j35Kzhw;o=X7xiH)qb6WNBqQ?pNVx>J)3?Q zPvj>G@bQ!Q_ywu_gF^$wq2hqiA!3P09MlPlh@Z_X(E4go;B!ExQtFL!STky14WpgrNBjagaKsPo zOm;k{0m95r_JH{DcrM1PT(mu=3GoZ!hH(5ibBrdp^}~2NW4ejenvCw4OCf&2JU=tb zYSiX7p}T~O^yBf`3nTLPw?$6-_Kx4%Hs_9qkY1tyQGh7$Oenw|Cza@|g7*6eMXX91 z5iN7vM|~-Dh+|Ayl!Zyqu@pPkVn3WdlKX%3{4DDH`Pp_JM<+H{=UowZ9@nvkWJ?Nn z9@pTLIAuHGxi;B`hrTCV;i1pD#_QmFB|Z`zAJO-=OFZZMLbA!2o=T-MCPvR%Sd;7f z!nls`IDT#S4;!9qls4N{a)m59O6i)P^Lp4_CP~ZcG>=AAsiwutZ0q4|Kz48wIr?AQ z#83F7lE+PovE%uRxr>dvcpN|5`g*6OGX{&+lI1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*Y!&_QV-dG2-V~gN7-s`rO9C{uc{%dsQ%ojJnhMF&a+G~ADh*KOtykBE~!+s#F z57rIa!_Px|y|8ysSydXSes40In<0jqzPhui?B;ty9M*sT89V&&N?||1af;&y`!(-x z*bngfVBN4icpiGt`rN5?%MZbwmA`+XF1r+3S5f&F3g^QMcU!8fyk-dd4&D!NoZ|Sw zevSPN`vG1bUN^xWeja+j;693Btr6I7!1b-SQ@rNQ6ZSW}@8JCa$0;8_f?s2QXVP#pAD{d_iuX6X?+E(=AE!8euwP?;6Z` Date: Tue, 6 Aug 2024 18:24:56 +0200 Subject: [PATCH 2/5] unit tests for the metadata --- tests/test_datavector.py | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/tests/test_datavector.py b/tests/test_datavector.py index 13c0707..bc65137 100644 --- a/tests/test_datavector.py +++ b/tests/test_datavector.py @@ -1,6 +1,8 @@ -import pytest +import pytest # noqa: F401 import types import os +import datetime +from unittest.mock import patch, MagicMock # noqa: F401 import numpy as np import sacc import pyccl as ccl @@ -306,7 +308,8 @@ def test_load_likelihood(): smokescreen._load_likelihood(invalid_likelihood, sacc_data) -def test_save_concealed_datavector(): +@patch('src.smokescreen.datavector.getpass.getuser', return_value='test_user') +def test_save_concealed_datavector(mock_getuser): # Create mock inputs cosmo = COSMO likelihood = "./examples/cosmic_shear/cosmicshear_likelihood.py" @@ -316,7 +319,8 @@ def test_save_concealed_datavector(): } shift_dict = {"Omega_c": 0.34, "sigma8": 0.85} sacc_data = sacc.Sacc.load_fits("./examples/cosmic_shear/cosmicshear_sacc.fits") - sck = ConcealDataVector(cosmo, syst_dict, likelihood, shift_dict, sacc_data) + sck = ConcealDataVector(cosmo, syst_dict, likelihood, + shift_dict, sacc_data, seed=1234) # Calculate the concealing factor and apply it to the likelihood data vector sck.calculate_concealing_factor() @@ -335,5 +339,11 @@ def test_save_concealed_datavector(): loaded_sacc = sacc.Sacc.load_fits(temp_file_name) np.testing.assert_array_equal(loaded_sacc.mean, blinded_dv) + info_str = 'Concealed (blinded) data-vector, created by Smokescreen.' + assert loaded_sacc.metadata['concealed'] is True + assert loaded_sacc.metadata['creator'] == mock_getuser.return_value + assert loaded_sacc.metadata['creation'][:10] == datetime.date.today().isoformat() + assert loaded_sacc.metadata['info'] == info_str + assert loaded_sacc.metadata['seed_smokescreen'] == 1234 # Clean up the temporary file os.remove(temp_file_name) From 0f0ca8ff4276f5c61dc70c701949d57bd99cbe06 Mon Sep 17 00:00:00 2001 From: Arthur Loureiro Date: Tue, 6 Aug 2024 18:29:41 +0200 Subject: [PATCH 3/5] increased test cov for datavec conceal --- src/smokescreen/datavector.py | 2 +- tests/test_datavector.py | 6 +++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/smokescreen/datavector.py b/src/smokescreen/datavector.py index c95ecf2..f9342e9 100644 --- a/src/smokescreen/datavector.py +++ b/src/smokescreen/datavector.py @@ -231,7 +231,7 @@ def _create_concealed_cosmo(self): # sometimes we have this extra paramters that can cause problems: try: del concealed_cosmo_dict['extra_parameters'] - except KeyError: + except KeyError: # pragma: no cover pass for k in self.__shifts.keys(): concealed_cosmo_dict[k] = self.__shifts[k] diff --git a/tests/test_datavector.py b/tests/test_datavector.py index bc65137..3d71230 100644 --- a/tests/test_datavector.py +++ b/tests/test_datavector.py @@ -330,7 +330,11 @@ def test_save_concealed_datavector(mock_getuser): temp_file_path = "./tests/" temp_file_root = "temp_sacc" temp_file_name = f"{temp_file_path}{temp_file_root}_concealed_data_vector.fits" - sck.save_concealed_datavector(temp_file_path, temp_file_root) + returned_sacc = sck.save_concealed_datavector(temp_file_path, + temp_file_root, + return_sacc=True) + # checks if the return is a sacc object + assert isinstance(returned_sacc, sacc.Sacc) # Check that the file was created assert os.path.exists(temp_file_name) From 729a86c59a09bd4d12f8edd2f67a67995c73a875 Mon Sep 17 00:00:00 2001 From: Arthur Loureiro Date: Tue, 6 Aug 2024 18:38:12 +0200 Subject: [PATCH 4/5] increased test cov for datavec --- tests/test_datavector.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tests/test_datavector.py b/tests/test_datavector.py index 3d71230..d2c9eea 100644 --- a/tests/test_datavector.py +++ b/tests/test_datavector.py @@ -336,6 +336,13 @@ def test_save_concealed_datavector(mock_getuser): # checks if the return is a sacc object assert isinstance(returned_sacc, sacc.Sacc) + returned_sacc = sck.save_concealed_datavector(temp_file_path, + temp_file_root, + return_sacc=False) + + # Check that the return is None + assert returned_sacc is None + # Check that the file was created assert os.path.exists(temp_file_name) From 53ba5afa96bec84445cac31ec5b699e8ed43348a Mon Sep 17 00:00:00 2001 From: Arthur Loureiro Date: Tue, 6 Aug 2024 19:12:23 +0200 Subject: [PATCH 5/5] ensure same seed is same shifts no matter the order of the params dict --- src/smokescreen/param_shifts.py | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/smokescreen/param_shifts.py b/src/smokescreen/param_shifts.py index b80d03b..da88bc5 100644 --- a/src/smokescreen/param_shifts.py +++ b/src/smokescreen/param_shifts.py @@ -88,13 +88,17 @@ def draw_flat_or_deterministic_param_shifts(cosmo, shifts_dict, seed): # failed_keys.append(key) raise ValueError(f"[{error}]Key {key} not in cosmology parameters") shifts = {} - for key, value in shifts_dict.items(): - if isinstance(value, tuple): - # check if the tuple is of length 2 - if len(value) == 2: - shifts[key] = np.random.uniform(value[0], value[1]) + # we loop over the ccl dict keys to ensure params are drawn in the same order! + for key in cosmo.to_dict().keys(): + if key in shifts_dict.keys(): + if isinstance(shifts_dict[key], tuple): + # check if the tuple is of length 2 + if len(shifts_dict[key]) == 2: + shifts[key] = np.random.uniform(shifts_dict[key][0], shifts_dict[key][1]) + else: + raise ValueError(f"Tuple {shifts_dict[key]} has to be of length 2") else: - raise ValueError(f"Tuple {value} has to be of length 2") + shifts[key] = shifts_dict[key] else: - shifts[key] = value + pass return shifts