Resolve "Check whether results do already exist for current parameter values"
Closes #32 (closed)
New method solutionExists
Retrieve current value for all parameters (new method getAllValues)
Get a list with all the project parameter names (new method getList)
- Get the number of parameters in the project using the "GetNumberOfParameters" command.
- Use the "GetParameterName" command to retrieve the name of each parameter one by one.
For each parameter, retrieve its value using the retrieve() method of the Parameter class in CPA.
Find results ID for the current parameter combination (new method getResultIDForParamCombination)
Verify that all parameters exist.
Use the exist() method of the Parameter class. If not satisfied, raise a RunTimeError.
Retrieve the parameter values for all the runIDs
Returns two lists, one with the parameter names and other with the resultIDs. In addition, returns a NumPy two-dimensional matrix where each column corresponds to a parameter and each row to a resultID. Outputting the values in this ways (a matrix) instead of other options like a dictionary greatly improves the efficiency of data comparisons that will be carried in the following steps.
Get the resultIDs using the "GetResultIDsFromTreeItem" command.
This requires to know the result for which the user is interested. If the project hasn't been simulated yet, this may result in error. To prevent this, the existence of the specified tree item must be checked beforehand by using the "DoesTreeItemExist" command.
For each resultID, obtain its parameter values using "GetParameterCombination" (new method getValuesForGivenResultID)
This is a tricky command since it requires special input parameters that will in reality act as outputs.
import win32com.client
import pythoncom
import cst_python_api as cpa
projectFolder = ""
projectName = ".cst"
myCST = cpa.CST_MicrowaveStudio(projectFolder, projectName)
resultID = "3D:RunID:1"
parameterNames = win32com.client.VARIANT(pythoncom.VT_BYREF | pythoncom.VT_VARIANT, [])
parameterValues = win32com.client.VARIANT(pythoncom.VT_BYREF | pythoncom.VT_VARIANT, [])
myCST._CST_MicrowaveStudio__MWS._FlagAsMethod("GetParameterCombination")
result = myCST._CST_MicrowaveStudio__MWS.GetParameterCombination(resultID, parameterNames, parameterValues)
if result:
print("parameterNames:", parameterNames)
print("parameterValues:", parameterValues)
else:
print("Error while obtaining the parameters combination.")
Compare the current parameter values with the list of parameters for the already run simulations.
Take into account that when comparing the values it could be interesting to allow a certain tolerance. For this, the np.isclose function can be useful. Below an use example can be found.
import numpy as np
# Define a function that compares each row of the matrix with a vector
def find_approx_equal_rows(matrix, vector, rel_tol=1e-9, abs_tol=0.0):
# Compare each row of the matrix with the vector using np.allclose, and return a boolean array
return np.all(np.isclose(matrix, vector, rtol=rel_tol, atol=abs_tol), axis=1)
# Use example
matrix = np.array([[1.000000001, 2.0, 3.0],
[1.0, 2.0, 3.0],
[1.0, 2.1, 3.0]])
vector = np.array([1.0, 2.0, 3.0])
# Find the rows which are approximately equal to the vector
result = find_approx_equal_rows(matrix, vector)
# Show the indices of the rows that are approximately equal
approx_equal_indices = np.where(result)[0]
print("Indices of the approximately equal rows:", approx_equal_indices)
(Optional) If more than one resultID matches
Reduce the allowed tolerance to check which of them better matches the specified parameters.
Do not implement this in the first attempt. Make some test and, if it really seems necessary implement it in that case.
Methods to implement
-
getList -
getAllValues -
getValuesForGivenResultID -
getResultIDForParamCombination -
solutionExists