Skip to content

Commit 7f87994

Browse files
updated approach to computing num_mismatched and fixed style
1 parent aa866bc commit 7f87994

File tree

1 file changed

+26
-15
lines changed

1 file changed

+26
-15
lines changed

tests/test_run_ref.py

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@
77
import pytest
88
from pyuvdata import UVData
99

10-
from pyuvsim.data import DATA_PATH as SIM_DATA_PATH
11-
1210
import pyuvsim
11+
from pyuvsim.data import DATA_PATH as SIM_DATA_PATH
1312
from pyuvsim.uvsim import run_uvsim
1413

1514
hasbench = importlib.util.find_spec("pytest_benchmark") is not None
@@ -40,12 +39,12 @@ def robust_response(url, n_retry=5):
4039

4140
# gets latest pid from api call
4241
def get_latest_pid(response):
43-
# takes as input the response from an api call to the Brown Digital Repository for the
44-
# collection "pyuvsim historical reference simulations". Parses the response for the latest
42+
# takes as input the response from an api call to the Brown Digital Repository for the
43+
# collection "pyuvsim historical reference simulations". Parses the response for the latest
4544
# uploaded created item matching the query, then returns the PID of that item to be downloaded.
46-
# In order to parse the response, further API calls are sent to get explicit data for each
45+
# In order to parse the response, further API calls are sent to get explicit data for each
4746
# item. If the increased number of API calls becomes an issue then this function can be changed
48-
# to simply determine basic datetime info from the input response with no further calls.
47+
# to simply determine basic datetime info from the input response with no further calls.
4948
collection_response_items = response.json()["items"]["docs"]
5049

5150
if len(collection_response_items) == 0:
@@ -54,7 +53,7 @@ def get_latest_pid(response):
5453
# using "object_created_dsi" key to sort the items, so we need to request that
5554
# for each item via the "json_uri", and get the pid as well to return
5655
print(
57-
f"requesting json_uri for each item in reponse, and parsing 'object_created_dsi' and 'pid'"
56+
"requesting json_uri for each item in reponse, and parsing 'object_created_dsi' and 'pid'"
5857
)
5958
json_uris = [item["json_uri"] for item in collection_response_items]
6059
object_created_dsis = []
@@ -89,7 +88,7 @@ def download_sim(target_dir, sim_name):
8988
# method to download the historical reference simulations from the Brown Digital
9089
# Repository. Sends an api call to the "pyuvsim historical reference simulations" collection,
9190
# then identifies the latest uploaded object in the response. Downloads that object to the
92-
# target directory and if the object requires the mwa beam file downloads that to the
91+
# target directory and if the object requires the mwa beam file downloads that to the
9392
# SIM_DATA_PATH
9493

9594
# Link to BDR API DOCS:
@@ -144,8 +143,8 @@ def download_sim(target_dir, sim_name):
144143

145144

146145
def compare_uvh5(uv_ref, uv_new):
147-
# takes as input two UVData objects, and computes relevant quantities for determining how
148-
# similar the data are. Prints the histories before setting them equal. Currently only runs
146+
# takes as input two UVData objects, and computes relevant quantities for determining how
147+
# similar the data are. Prints the histories before setting them equal. Currently only runs
149148
# an equality check but (TODO: FIXME) should make a much more exhaustive check OR just turn
150149
# back on the exact check and update the sim output when it differs (Do this tbh)
151150
import numpy as np
@@ -180,14 +179,26 @@ def compare_uvh5(uv_ref, uv_new):
180179
# should match output of np.testing.assert_allclose
181180
cases = np.abs(new_arr - ref_arr) <= (1e-8 + 1e-5 * np.abs(ref_arr))
182181
outcome = cases.all()
183-
num_mismatched = str(len(cases[cases == False])) + "/" + str(cases.size)
182+
183+
# get unique outcomes (true / false) and corresponding counts
184+
# then convert to dict and get result
185+
unique, counts = np.unique(cases, return_counts=True)
186+
outcome_dict = dict(zip(unique, counts, strict=False))
187+
188+
# need to check that key exists
189+
if False in outcome_dict:
190+
num_mismatched = str(outcome_dict[False]) + "/" + str(cases.size)
191+
else:
192+
num_mismatched = "0" + "/" + str(cases.size)
184193

185194
# print some things for reference
186195
print(
187-
f"mean of abs of diff of visibilities 'mean(abs(old_data_arr - new_data_arr)): {mean_diff_of_vis}"
196+
f"mean of abs of diff of visibilities "
197+
f"'mean(abs(old_data_arr - new_data_arr))': {mean_diff_of_vis}"
188198
)
189199
print(
190-
f"mean of diff of abs of visibilities 'mean(abs(old_data_arr) - abs(new_data_arr)): {mean_diff_of_abs}"
200+
f"mean of diff of abs of visibilities "
201+
f"'mean(abs(old_data_arr) - abs(new_data_arr))': {mean_diff_of_abs}"
191202
)
192203
print(f"max_absolute_diff: {max_absolute_diff}")
193204
print(f"max_relative_diff: {max_relative_diff}")
@@ -205,7 +216,7 @@ def compare_uvh5(uv_ref, uv_new):
205216

206217

207218
def construct_filepaths(target_dir, sim):
208-
# takes as input the sim name (NEEDS TO BE AN EXISTING SIM IN THE DATA DIRECTORY), then
219+
# takes as input the sim name (NEEDS TO BE AN EXISTING SIM IN THE DATA DIRECTORY), then
209220
# constructs the expected yaml_filepath to run the simulation and uvh5_filepath to locate
210221
# the downloaded historical output
211222

@@ -280,5 +291,5 @@ def test_run_sim(benchmark, goto_tempdir, refsim):
280291
if pyuvsim.mpi.rank != 0:
281292
return
282293

283-
# performs any assertions to confirm that the reference simulation output hasn't diverged
294+
# performs any assertions to confirm that the reference simulation output hasn't diverged
284295
compare_uvh5(uv_ref, uv_new)

0 commit comments

Comments
 (0)