From d950f250dcc970accdd7d03d849cb23c36205a9c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=20Gro=C3=9F?= Date: Thu, 14 Nov 2024 14:25:31 +0100 Subject: [PATCH 1/5] Change random number generation to default_rng, Change location of seed init --- pyvisgen/simulation/data_set.py | 32 ++++++++++++++++++++++---------- 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/pyvisgen/simulation/data_set.py b/pyvisgen/simulation/data_set.py index dd4e3c8..17c3944 100644 --- a/pyvisgen/simulation/data_set.py +++ b/pyvisgen/simulation/data_set.py @@ -39,10 +39,9 @@ def simulate_data_set(config, slurm=False, job_id=None, n=None): out_path.mkdir(parents=True, exist_ok=True) data = load_bundles(conf["in_path"]) - if conf["seed"] is not None: - np.random.seed(conf["seed"]) - torch.manual_seed(conf["seed"]) - + # if conf["seed"] is not None: + # global rng + # rng = np.random.default_rng(conf["seed"]) if slurm: job_id = int(job_id + n * 500) out = out_path / Path("vis_" + str(job_id) + ".fits") @@ -121,6 +120,14 @@ def create_sampling_rc(conf): dict contains the observation parameters """ + + global rng + + if conf["seed"] is not None: + rng = np.random.default_rng(conf["seed"]) + else: + rng = np.random.default_rng() + samp_ops = draw_sampling_opts(conf) array_layout = layouts.get_array_layout(conf["layout"][0]) half_telescopes = array_layout.x.shape[0] // 2 @@ -145,27 +152,32 @@ def draw_sampling_opts(conf): dict contains randomly drawn observation options """ + + if "rng" not in globals(): + global rng + rng = np.random.default_rng(conf["seed"]) + angles_ra = np.arange( conf["fov_center_ra"][0][0], conf["fov_center_ra"][0][1], step=0.1 ) - fov_center_ra = np.random.choice(angles_ra) + fov_center_ra = rng.choice(angles_ra) angles_dec = np.arange( conf["fov_center_dec"][0][0], conf["fov_center_dec"][0][1], step=0.1 ) - fov_center_dec = np.random.choice(angles_dec) + fov_center_dec = rng.choice(angles_dec) start_time_l = datetime.strptime(conf["scan_start"][0], "%d-%m-%Y %H:%M:%S") start_time_h = datetime.strptime(conf["scan_start"][1], "%d-%m-%Y %H:%M:%S") start_times = pd.date_range(start_time_l, start_time_h, freq="1h").strftime( "%d-%m-%Y %H:%M:%S" ) - scan_start = np.random.choice( + scan_start = rng.choice( [datetime.strptime(time, "%d-%m-%Y %H:%M:%S") for time in start_times] ) - scan_duration = np.random.randint( - conf["scan_duration"][0], conf["scan_duration"][1] + scan_duration = int( + rng.integers(conf["scan_duration"][0], conf["scan_duration"][1]) ) - num_scans = np.random.randint(conf["num_scans"][0], conf["num_scans"][1]) + num_scans = int(rng.integers(conf["num_scans"][0], conf["num_scans"][1])) opts = np.array( [ conf["mode"], From 9939664f53c032a444356da563d90f8a08eed777 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=20Gro=C3=9F?= Date: Thu, 14 Nov 2024 14:32:54 +0100 Subject: [PATCH 2/5] Add changelog --- docs/changes/44.bugfix.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 docs/changes/44.bugfix.rst diff --git a/docs/changes/44.bugfix.rst b/docs/changes/44.bugfix.rst new file mode 100644 index 0000000..3868370 --- /dev/null +++ b/docs/changes/44.bugfix.rst @@ -0,0 +1 @@ +- Fixed random number drawing in tests by changing the location of the seed override From b5979e6b44cb867f6edf5bccfe5d0be92895c758 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=20Gro=C3=9F?= Date: Thu, 14 Nov 2024 14:43:21 +0100 Subject: [PATCH 3/5] Change seed, remove comment --- pyvisgen/simulation/data_set.py | 3 --- tests/test_conf.toml | 2 +- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/pyvisgen/simulation/data_set.py b/pyvisgen/simulation/data_set.py index 17c3944..e75dcdb 100644 --- a/pyvisgen/simulation/data_set.py +++ b/pyvisgen/simulation/data_set.py @@ -39,9 +39,6 @@ def simulate_data_set(config, slurm=False, job_id=None, n=None): out_path.mkdir(parents=True, exist_ok=True) data = load_bundles(conf["in_path"]) - # if conf["seed"] is not None: - # global rng - # rng = np.random.default_rng(conf["seed"]) if slurm: job_id = int(job_id + n * 500) out = out_path / Path("vis_" + str(job_id) + ".fits") diff --git a/tests/test_conf.toml b/tests/test_conf.toml index 68e577b..b9b0e36 100644 --- a/tests/test_conf.toml +++ b/tests/test_conf.toml @@ -1,7 +1,7 @@ [sampling_options] mode = "full" device = "cpu" -seed = 1337 +seed = 42 layout = "vla" img_size = 128 fov_center_ra = [90, 140] From 14fa0b89a01b560784ddbedc3668a2793da0ce28 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=20Gro=C3=9F?= Date: Thu, 14 Nov 2024 15:23:26 +0100 Subject: [PATCH 4/5] Add test for create_data_set method in data_set.py without slurm --- tests/test_simulation.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tests/test_simulation.py b/tests/test_simulation.py index 4227924..7614a99 100644 --- a/tests/test_simulation.py +++ b/tests/test_simulation.py @@ -64,3 +64,9 @@ def test_vis_loop(): out = out_path / Path("vis_0.fits") hdu_list = writer.create_hdu_list(vis_data, obs) hdu_list.writeto(out, overwrite=True) + + +def test_simulate_data_set_no_slurm(): + from pyvisgen.simulation.data_set import simulate_data_set + + simulate_data_set(config) From d38601daa7e5d402f56346037b357d33c3b757af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=20Gro=C3=9F?= Date: Thu, 14 Nov 2024 16:01:27 +0100 Subject: [PATCH 5/5] Add test for create_sampling_rc method without a seed --- tests/test_simulation.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/tests/test_simulation.py b/tests/test_simulation.py index 7614a99..7344dca 100644 --- a/tests/test_simulation.py +++ b/tests/test_simulation.py @@ -27,6 +27,18 @@ def test_create_sampling_rc(): test_opts(samp_ops) +def test_create_sampling_rc_no_seed(): + from pyvisgen.simulation.data_set import create_sampling_rc, test_opts + + mod_conf = conf.copy() + mod_conf["seed"] = None + + samp_ops = create_sampling_rc(mod_conf) + assert len(samp_ops) == 17 + + test_opts(samp_ops) + + def test_vis_loop(): import torch