Skip to content

Commit 8d3cf4e

Browse files
update get_test_result
1 parent 2459038 commit 8d3cf4e

File tree

2 files changed

+37
-80
lines changed

2 files changed

+37
-80
lines changed

docker/config/excerpts/query_local/queryapi.py

Lines changed: 32 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -3416,7 +3416,7 @@ def get_surface_energy_relaxed_cubic(
34163416
return [relaxed_surface_energy]
34173417

34183418

3419-
def get_test_result(db, test, model, species, prop, keys, units):
3419+
def get_test_result(db, test, model, prop, keys, units):
34203420
"""
34213421
This function provides a simplified way of querying the KIM mongo database for Test Results.
34223422
Although only a single property may be queried on at a time with this function, multiple keys
@@ -3436,13 +3436,11 @@ def get_test_result(db, test, model, species, prop, keys, units):
34363436
inputs = {
34373437
"test": test,
34383438
"model": model,
3439-
"species": species,
34403439
"prop": prop,
34413440
"keys": keys,
34423441
"units": units,
34433442
}
34443443
helpers.check_input_args_are_lists(inputs)
3445-
helpers.check_types_in_input_arg("species", species, str)
34463444
helpers.check_types_in_input_arg("keys", keys, str)
34473445
helpers.check_types_in_input_arg("units", units, (str, type(None)))
34483446
helpers.check_for_invalid_null(units)
@@ -3474,12 +3472,9 @@ def get_test_result(db, test, model, species, prop, keys, units):
34743472

34753473
# Determine whether a full property-id with a contributor and date was given, or only the
34763474
# property short name. If the full property-id was given, query on it exactly. If not, do a
3477-
# regex on it and sort the results from the most recent property-id date to the oldest. Note
3478-
# that sorting on Test and Model versions takes priority over the sorting on property-id.
3479-
prop_has_ver = False
3475+
# regex on it
34803476
if re.match(RE_PROPERTY_ID, prop) is not None:
34813477
query["query"]["property-id"] = prop
3482-
prop_has_ver = True
34833478
elif re.match(RE_PROPERTY_SHORT_NAME, prop) is not None:
34843479
query["query"]["property-id"] = {}
34853480
query["query"]["property-id"]["$regex"] = (
@@ -3490,9 +3485,9 @@ def get_test_result(db, test, model, species, prop, keys, units):
34903485
"Invalid property name was passed to function get_test_result()"
34913486
)
34923487

3493-
Test_has_ver = helpers.modify_query_for_item(query, "runner", test)
3494-
Model_has_ver = helpers.modify_query_for_item(query, "subject", model)
3495-
helpers.modify_query_for_species(query, species)
3488+
# If either item does not have a version, these functions will add 'history': 1 to the query
3489+
helpers.modify_query_for_item(query, "runner", test)
3490+
helpers.modify_query_for_item(query, "subject", model)
34963491

34973492
# Perform the actual query
34983493
for key in query:
@@ -3501,75 +3496,37 @@ def get_test_result(db, test, model, species, prop, keys, units):
35013496

35023497
if len(results_from_query) == 0:
35033498
return []
3504-
else:
3505-
# Determine necessary sorting before taking the final result (which will be at the top of
3506-
# the list)
3507-
if (not Test_has_ver) and (not Model_has_ver) and (not prop_has_ver):
3508-
# sort on property date string
3509-
final_TestResult = helpers.sort_on_property_date(results_from_query)[0]
3510-
elif Test_has_ver and (not Model_has_ver) and (not prop_has_ver):
3511-
# sort on Model version and date string of prop
3512-
tmp = helpers.sort_on_Model_ver(results_from_query)
3513-
highest_Model_ver = tmp[0]["meta.subject.version"]
3514-
tmp = [
3515-
propinstance
3516-
for propinstance in tmp
3517-
if propinstance["meta.subject.version"] == highest_Model_ver
3518-
]
3519-
final_TestResult = helpers.sort_on_property_date(tmp)[0]
3520-
elif (not Test_has_ver) and Model_has_ver and (not prop_has_ver):
3521-
# sort on Test version and date string of prop
3522-
tmp = helpers.sort_on_Test_ver(results_from_query)
3523-
highest_Test_ver = tmp[0]["meta.runner.version"]
3524-
tmp = [
3525-
propinstance
3526-
for propinstance in tmp
3527-
if propinstance["meta.runner.version"] == highest_Test_ver
3528-
]
3529-
final_TestResult = helpers.sort_on_property_date(tmp)[0]
3530-
elif (not Test_has_ver) and (not Model_has_ver) and prop_has_ver:
3531-
# No sorting necessary. The pipeline will automatically only return the highest
3532-
# Test-Model version combination if 'history' is not turned on, and we already have a
3533-
# specific property version here. Therefore, only a single Test Result should have been
3534-
# returned.
3535-
final_TestResult = results_from_query[0]
3536-
elif Test_has_ver and Model_has_ver and (not prop_has_ver):
3537-
# sort on date string of prop
3538-
final_TestResult = helpers.sort_on_property_date(results_from_query)[0]
3539-
elif (not Test_has_ver) and Model_has_ver and prop_has_ver:
3540-
# sort on Test version
3541-
final_TestResult = helpers.sort_on_Test_ver(results_from_query)[0]
3542-
elif Test_has_ver and (not Model_has_ver) and prop_has_ver:
3543-
# sort on Model version
3544-
final_TestResult = helpers.sort_on_Model_ver(results_from_query)[0]
3545-
elif Test_has_ver and Model_has_ver and prop_has_ver:
3546-
# No sorting necessary, but make sure to only take one result since there could be
3547-
# duplicates lurking in the database for old versions
3548-
# FIXME: Should we take the one that has the most recent timestamp here (might
3549-
# still not be marked 'latest')?
3550-
# FIXME: What if the test-model pair has multiple property instances
3551-
# reported for it?
3552-
final_TestResult = results_from_query[0]
3553-
3554-
# If only an error was returned, return nothing
3555-
if final_TestResult["meta.uuid"] == "er":
3499+
3500+
# If we searched with 'history': 1, we may have ended up collecting results from multiple
3501+
# runs. This function picks out the single most up-to-date run from all results.
3502+
# It also ensures only Test Results are included (not errors)
3503+
# This is a redundant safety mechanism, as the query already should not have returned
3504+
# any errors (errors cannot have 'property-id' and property keys)
3505+
final_TestResult = helpers.filter_on_item_versions_and_timestamp(results_from_query)
3506+
3507+
# Just in case the removal of errors reduced our list to zero
3508+
if len(final_TestResult)==0:
35563509
return []
35573510

35583511
final_output = []
3559-
for ind, key in enumerate(keys):
3560-
# First, check to make sure the key is actually in the property definition
3561-
if (key not in final_TestResult) and (
3562-
key + ".source-value" not in final_TestResult
3563-
):
3564-
raise ValueError(
3565-
"Key '{}' specified as input to function get_test_result() is "
3566-
"not a valid part of the property definition specified or other metadata "
3567-
"associated with a Test Result".format(key)
3512+
for result in final_TestResult:
3513+
inner_output=[]
3514+
for ind, key in enumerate(keys):
3515+
# First, check to make sure the key is actually in the property definition
3516+
# TODO: This function is incompatible with optional keys. Fix this.
3517+
if (key not in result) and (
3518+
key + ".source-value" not in result
3519+
):
3520+
raise ValueError(
3521+
"Key '{}' specified as input to function get_test_result() is "
3522+
"not a valid part of the property definition specified or other metadata "
3523+
"associated with a Test Result".format(key)
3524+
)
3525+
key_value_in_desired_units = helpers.extract_key_from_result(
3526+
result, key, units[ind]
35683527
)
3569-
key_value_in_desired_units = helpers.extract_key_from_result(
3570-
final_TestResult, key, units[ind]
3571-
)
3572-
final_output.append(key_value_in_desired_units)
3528+
inner_output.append(key_value_in_desired_units)
3529+
final_output.append(inner_output)
35733530

35743531
return final_output
35753532

docker/config/excerpts/template.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ def intercept_query(query, subject_name, local, infofile):
204204
return tmp_answer
205205

206206

207-
def intercept_get_test_result(test, model, species, prop, keys, units, local, infofile):
207+
def intercept_get_test_result(test, model, prop, keys, units, local, infofile):
208208
"""
209209
Intercept any calls to get_test_result performed in pipeline.stdin.tpl. Check if 'meta.uuid' is contained
210210
in the list of keys specified as input to the function. If it is not, we add it ourselves as the last
@@ -218,7 +218,7 @@ def intercept_get_test_result(test, model, species, prop, keys, units, local, in
218218
if "meta.uuid" in keys:
219219
uuid_index = keys.index("meta.uuid")
220220
answer = kimquery.get_test_result(
221-
test, model, species, prop, keys, units, local, decode=True
221+
test, model, prop, keys, units, local, decode=True
222222
)
223223
if answer:
224224
uuid = answer[uuid_index]
@@ -227,7 +227,7 @@ def intercept_get_test_result(test, model, species, prop, keys, units, local, in
227227
keys.append("meta.uuid")
228228
units.append(None)
229229
answer = kimquery.get_test_result(
230-
test, model, species, prop, keys, units, local, decode=True
230+
test, model, prop, keys, units, local, decode=True
231231
)
232232

233233
# Revert keys and units to their original values by stripping off the last element
@@ -239,8 +239,8 @@ def intercept_get_test_result(test, model, species, prop, keys, units, local, in
239239
uuid = answer[-1]
240240
del answer[-1]
241241

242-
tmpstr = "get_test_result({}, {}, {}, {}, {}, {})".format(
243-
test, model, species, prop, keys, units
242+
tmpstr = "get_test_result({}, {}, {}, {}, {})".format(
243+
test, model, prop, keys, units
244244
)
245245
if answer:
246246
with open(infofile, "a", encoding="utf-8") as out:

0 commit comments

Comments
 (0)