Skip to content

Commit

Permalink
Use Numpy's RNG instead of global seeds
Browse files Browse the repository at this point in the history
  • Loading branch information
santisoler committed Nov 18, 2024
1 parent 0ef5021 commit 6abde83
Show file tree
Hide file tree
Showing 15 changed files with 99 additions and 66 deletions.
13 changes: 7 additions & 6 deletions notebooks/03-gravity/fwd_gravity_anomaly_3d.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,12 @@
"outputs": [],
"source": [
"[x_topo, y_topo] = np.meshgrid(np.linspace(-200, 200, 41), np.linspace(-200, 200, 41))\n",
"\n",
"rng = np.random.default_rng(seed=737)\n",
"z_topo = (\n",
" -15 * np.exp(-(x_topo**2 + y_topo**2) / 80**2)\n",
" + 100.0\n",
" + 0.5 * np.random.rand(*x_topo.shape)\n",
" + rng.uniform(low=0.0, high=0.5, size=x_topo.shape)\n",
")"
]
},
Expand All @@ -139,8 +141,7 @@
"iopub.status.busy": "2024-07-24T17:50:17.868023Z",
"iopub.status.idle": "2024-07-24T17:50:18.225050Z",
"shell.execute_reply": "2024-07-24T17:50:18.224263Z"
},
"scrolled": false
}
},
"outputs": [
{
Expand Down Expand Up @@ -723,9 +724,9 @@
" fname = dir_path + \"gravity_topo.txt\"\n",
" np.savetxt(fname, np.c_[topo_xyz], fmt=\"%.4e\")\n",
"\n",
" np.random.seed(737)\n",
" rng = np.random.default_rng(seed=737)\n",
" maximum_anomaly = np.max(np.abs(dpred))\n",
" noise = 0.02 * maximum_anomaly * np.random.normal(size=len(dpred))\n",
" noise = rng.normal(scale=0.02 * maximum_anomaly, size=len(dpred))\n",
" fname = dir_path + \"gravity_data.obs\"\n",
" np.savetxt(fname, np.c_[receiver_locations, dpred + noise], fmt=\"%.4e\")"
]
Expand All @@ -751,5 +752,5 @@
}
},
"nbformat": 4,
"nbformat_minor": 1
"nbformat_minor": 4
}
5 changes: 3 additions & 2 deletions notebooks/03-gravity/fwd_gravity_gradiometry_3d.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,11 @@
"outputs": [],
"source": [
"[x_topo, y_topo] = np.meshgrid(np.linspace(-200, 200, 41), np.linspace(-200, 200, 41))\n",
"rng = np.random.default_rng(seed=737)\n",
"z_topo = (\n",
" -15 * np.exp(-(x_topo**2 + y_topo**2) / 80**2)\n",
" + 100.0\n",
" + 0.5 * np.random.rand(*x_topo.shape)\n",
" + rng.uniform(low=0.0, high=0.5, size=x_topo.shape)\n",
")"
]
},
Expand Down Expand Up @@ -778,5 +779,5 @@
}
},
"nbformat": 4,
"nbformat_minor": 1
"nbformat_minor": 4
}
9 changes: 5 additions & 4 deletions notebooks/04-magnetics/fwd_magnetics_induced_3d.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,11 @@
"outputs": [],
"source": [
"[x_topo, y_topo] = np.meshgrid(np.linspace(-200, 200, 41), np.linspace(-200, 200, 41))\n",
"rng = np.random.default_rng(seed=737)\n",
"z_topo = (\n",
" -15 * np.exp(-(x_topo**2 + y_topo**2) / 80**2)\n",
" + 100.0\n",
" + 0.5 * np.random.rand(*x_topo.shape)\n",
" + rng.uniform(low=0.0, high=0.5, size=x_topo.shape)\n",
")"
]
},
Expand Down Expand Up @@ -706,9 +707,9 @@
" fname = dir_path + \"magnetics_topo.txt\"\n",
" np.savetxt(fname, np.c_[topo_xyz], fmt=\"%.4e\")\n",
"\n",
" np.random.seed(211)\n",
" rng = np.random.default_rng(seed=211)\n",
" maximum_anomaly = np.max(np.abs(dpred))\n",
" noise = 0.02 * maximum_anomaly * np.random.normal(size=len(dpred))\n",
" noise = rng.normal(scale=0.02 * maximum_anomaly, size=len(dpred))\n",
" fname = dir_path + \"magnetics_data.obs\"\n",
" np.savetxt(fname, np.c_[receiver_locations, dpred + noise], fmt=\"%.4e\")"
]
Expand Down Expand Up @@ -741,5 +742,5 @@
}
},
"nbformat": 4,
"nbformat_minor": 1
"nbformat_minor": 4
}
11 changes: 5 additions & 6 deletions notebooks/04-magnetics/fwd_magnetics_mvi_3d.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,11 @@
"outputs": [],
"source": [
"[x_topo, y_topo] = np.meshgrid(np.linspace(-200, 200, 41), np.linspace(-200, 200, 41))\n",
"rng = np.random.default_rng(seed=42)\n",
"z_topo = (\n",
" -15 * np.exp(-(x_topo**2 + y_topo**2) / 80**2)\n",
" + 100.0\n",
" + 0.5 * np.random.rand(*x_topo.shape)\n",
" + rng.uniform(scale=0.5, size=x_topo.shape)\n",
")"
]
},
Expand Down Expand Up @@ -583,8 +584,7 @@
"iopub.status.busy": "2024-07-17T17:48:42.969668Z",
"iopub.status.idle": "2024-07-17T17:48:42.974834Z",
"shell.execute_reply": "2024-07-17T17:48:42.974163Z"
},
"scrolled": false
}
},
"outputs": [],
"source": [
Expand Down Expand Up @@ -627,8 +627,7 @@
"iopub.status.busy": "2024-07-17T17:48:42.991011Z",
"iopub.status.idle": "2024-07-17T17:48:43.433606Z",
"shell.execute_reply": "2024-07-17T17:48:43.432840Z"
},
"scrolled": false
}
},
"outputs": [
{
Expand Down Expand Up @@ -863,5 +862,5 @@
}
},
"nbformat": 4,
"nbformat_minor": 1
"nbformat_minor": 4
}
6 changes: 3 additions & 3 deletions notebooks/05-dcr/fwd_dcr_1d.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -435,8 +435,8 @@
" if not os.path.exists(dir_path):\n",
" os.mkdir(dir_path)\n",
"\n",
" np.random.seed(145)\n",
" noise = 0.025 * dpred_resistivity * np.random.normal(size=len(dpred_resistivity))\n",
" rng = np.random.default_rng(seed=145)\n",
" noise = rng.normal(scale=0.025 * dpred_resistivity, size=len(dpred_resistivity))\n",
"\n",
" data_array = np.c_[\n",
" survey.locations_a,\n",
Expand Down Expand Up @@ -471,5 +471,5 @@
}
},
"nbformat": 4,
"nbformat_minor": 1
"nbformat_minor": 4
}
6 changes: 3 additions & 3 deletions notebooks/05-dcr/fwd_dcr_2d.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -1046,9 +1046,9 @@
" os.mkdir(dir_path)\n",
"\n",
" # Add 5% Gaussian noise to each datum\n",
" np.random.seed(225)\n",
" rng = np.random.default_rng(seed=225)\n",
" std = 0.05 * np.abs(dpred_con)\n",
" dc_noise = std * np.random.normal(size=len(dpred_con))\n",
" dc_noise = rng.normal(scale=std, size=len(dpred_con))\n",
" dobs = dpred_con + dc_noise\n",
"\n",
" # Create a survey with the original electrode locations\n",
Expand Down Expand Up @@ -1102,5 +1102,5 @@
}
},
"nbformat": 4,
"nbformat_minor": 1
"nbformat_minor": 4
}
12 changes: 5 additions & 7 deletions notebooks/05-dcr/fwd_dcr_3d.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,7 @@
"iopub.status.busy": "2024-07-17T17:49:48.097426Z",
"iopub.status.idle": "2024-07-17T17:49:48.981316Z",
"shell.execute_reply": "2024-07-17T17:49:48.980667Z"
},
"scrolled": false
}
},
"outputs": [
{
Expand Down Expand Up @@ -820,8 +819,7 @@
"iopub.status.busy": "2024-07-17T17:50:25.388911Z",
"iopub.status.idle": "2024-07-17T17:50:25.394480Z",
"shell.execute_reply": "2024-07-17T17:50:25.393823Z"
},
"scrolled": false
}
},
"outputs": [
{
Expand Down Expand Up @@ -1018,9 +1016,9 @@
" os.mkdir(dir_path)\n",
"\n",
" # Add 10% Gaussian noise to each datum\n",
" np.random.seed(433)\n",
" rng = np.random.default_rng(seed=433)\n",
" std = 0.1 * np.abs(dpred_con)\n",
" noise = std * np.random.normal(size=len(dpred_con))\n",
" noise = rng.normal(scale=std, size=len(dpred_con))\n",
" dobs = dpred_con + noise\n",
"\n",
" # Create dictionary that stores line IDs\n",
Expand Down Expand Up @@ -1085,5 +1083,5 @@
}
},
"nbformat": 4,
"nbformat_minor": 1
"nbformat_minor": 4
}
6 changes: 3 additions & 3 deletions notebooks/06-ip/fwd_ip_2d.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -792,9 +792,9 @@
" os.mkdir(dir_path)\n",
"\n",
" # Add 5% Gaussian noise to each datum\n",
" np.random.seed(225)\n",
" rng = np.random.default_rng(seed=225)\n",
" std = 5e-3 * np.ones_like(dpred_ip)\n",
" ip_noise = std * np.random.normal(size=len(dpred_ip))\n",
" ip_noise = rng.normal(scale=std, size=len(dpred_ip))\n",
" dobs = dpred_ip + ip_noise\n",
"\n",
" # Create a survey with the original electrode locations\n",
Expand Down Expand Up @@ -848,5 +848,5 @@
}
},
"nbformat": 4,
"nbformat_minor": 1
"nbformat_minor": 4
}
6 changes: 3 additions & 3 deletions notebooks/06-ip/fwd_ip_3d.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -1007,9 +1007,9 @@
" os.mkdir(dir_path)\n",
"\n",
" # Add 10% Gaussian noise to each datum\n",
" np.random.seed(433)\n",
" rng = np.random.default_rng(seed=433)\n",
" std = 5e-3 * np.ones_like(dpred_ip)\n",
" noise = std * np.random.normal(size=len(dpred_ip))\n",
" noise = rng.normal(scale=std, size=len(dpred_ip))\n",
" dobs = dpred_ip + noise\n",
"\n",
" # Create dictionary that stores line IDs\n",
Expand Down Expand Up @@ -1074,5 +1074,5 @@
}
},
"nbformat": 4,
"nbformat_minor": 1
"nbformat_minor": 4
}
11 changes: 5 additions & 6 deletions notebooks/07-fdem/fwd_fdem_1d.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -570,11 +570,10 @@
" if not os.path.exists(dir_path):\n",
" os.mkdir(dir_path)\n",
"\n",
" np.random.seed(222)\n",
" noise = (\n",
" 0.05\n",
" * np.abs(dpred_conductivity)\n",
" * np.random.normal(size=len(dpred_conductivity))\n",
" rng = np.random.default_rng(seed=222)\n",
" noise = rng.normal(\n",
" scale=0.05 * np.abs(dpred_conductivity),\n",
" size=len(dpred_conductivity),\n",
" )\n",
" dpred_out = dpred_conductivity + noise\n",
"\n",
Expand Down Expand Up @@ -615,5 +614,5 @@
}
},
"nbformat": 4,
"nbformat_minor": 1
"nbformat_minor": 4
}
11 changes: 5 additions & 6 deletions notebooks/08-tdem/fwd_tdem_1d.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -1057,11 +1057,10 @@
" if not os.path.exists(dir_path):\n",
" os.mkdir(dir_path)\n",
"\n",
" np.random.seed(347)\n",
" noise = (\n",
" 0.05\n",
" * np.abs(dpred_conductivity)\n",
" * np.random.normal(size=len(dpred_conductivity))\n",
" rng = np.random.default_rng(seed=347)\n",
" noise = rng.normal(\n",
" scale=0.05 * np.abs(dpred_conductivity),\n",
" size=len(dpred_conductivity),\n",
" )\n",
" dpred_conductivity += noise\n",
" fname = dir_path + \"em1dtm_data.txt\"\n",
Expand Down Expand Up @@ -1096,5 +1095,5 @@
}
},
"nbformat": 4,
"nbformat_minor": 1
"nbformat_minor": 4
}
6 changes: 4 additions & 2 deletions notebooks/08-tdem/fwd_tdem_3d.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -932,7 +932,9 @@
"\n",
" # Write data with 2% noise added\n",
" fname = dir_path + \"tdem_data.obs\"\n",
" dpred = dpred + 0.02 * np.abs(dpred) * np.random.rand(len(dpred))\n",
" rng = np.random.default_rng(seed=42)\n",
" noise = rng.uniform(low=0.0, high=0.02 * np.abs(dpred), size=len(dpred))\n",
" dpred += noise\n",
" t_vec = np.kron(np.ones(ntx), time_channels)\n",
" receiver_locations = np.kron(receiver_locations, np.ones((len(time_channels), 1)))\n",
"\n",
Expand Down Expand Up @@ -967,5 +969,5 @@
}
},
"nbformat": 4,
"nbformat_minor": 1
"nbformat_minor": 4
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,7 @@
" directives,\n",
" inversion,\n",
" utils,\n",
")\n",
"\n",
"np.random.seed(0)"
")"
]
},
{
Expand Down
Loading

0 comments on commit 6abde83

Please sign in to comment.