@@ -3416,7 +3416,7 @@ def get_surface_energy_relaxed_cubic(
3416
3416
return [relaxed_surface_energy ]
3417
3417
3418
3418
3419
- def get_test_result (db , test , model , species , prop , keys , units ):
3419
+ def get_test_result (db , test , model , prop , keys , units ):
3420
3420
"""
3421
3421
This function provides a simplified way of querying the KIM mongo database for Test Results.
3422
3422
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):
3436
3436
inputs = {
3437
3437
"test" : test ,
3438
3438
"model" : model ,
3439
- "species" : species ,
3440
3439
"prop" : prop ,
3441
3440
"keys" : keys ,
3442
3441
"units" : units ,
3443
3442
}
3444
3443
helpers .check_input_args_are_lists (inputs )
3445
- helpers .check_types_in_input_arg ("species" , species , str )
3446
3444
helpers .check_types_in_input_arg ("keys" , keys , str )
3447
3445
helpers .check_types_in_input_arg ("units" , units , (str , type (None )))
3448
3446
helpers .check_for_invalid_null (units )
@@ -3474,12 +3472,9 @@ def get_test_result(db, test, model, species, prop, keys, units):
3474
3472
3475
3473
# Determine whether a full property-id with a contributor and date was given, or only the
3476
3474
# 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
3480
3476
if re .match (RE_PROPERTY_ID , prop ) is not None :
3481
3477
query ["query" ]["property-id" ] = prop
3482
- prop_has_ver = True
3483
3478
elif re .match (RE_PROPERTY_SHORT_NAME , prop ) is not None :
3484
3479
query ["query" ]["property-id" ] = {}
3485
3480
query ["query" ]["property-id" ]["$regex" ] = (
@@ -3490,9 +3485,9 @@ def get_test_result(db, test, model, species, prop, keys, units):
3490
3485
"Invalid property name was passed to function get_test_result()"
3491
3486
)
3492
3487
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 )
3496
3491
3497
3492
# Perform the actual query
3498
3493
for key in query :
@@ -3501,75 +3496,37 @@ def get_test_result(db, test, model, species, prop, keys, units):
3501
3496
3502
3497
if len (results_from_query ) == 0 :
3503
3498
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 :
3556
3509
return []
3557
3510
3558
3511
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 ]
3568
3527
)
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 )
3573
3530
3574
3531
return final_output
3575
3532
0 commit comments