Using RTV via python scripts¶
To execute a validation scenario using RTV via python script you need to
import, instatiate and invoke certain classes from the rtv
package inside
your script and then simply execute it:
python /path/to/your/script
Example¶
pred.csv
:
k1,k2,k3
1,0,0
0,1,0
0,0,1
true.csv
k1,k2,k3
0,0,1
0,1,1
1,0,1
script.py
:
from rtv.data.output.writer import JSONFileWriter
from rtv.data.reader import CSVReader
from rtv.validation import StrategyValidation
from rtv.validation.strategy import MeanAbsoluteError
from rtv.validation.validator import Validator
def main():
pred_filename = "pred.csv"
true_filename = "true.csv"
# Instantiate the Reader and the Writer entities
reader = CSVReader({"delimiter": ","})
writer = JSONFileWriter()
# Read sources to get reference and target DataCollection objects
reference = reader.read(true_filename)
target = reader.read(pred_filename)
# Instatiate ValidationStrategy entities
mae_strategy_05 = MeanAbsoluteError({"threshold": 0.5})
mae_strategy_03 = MeanAbsoluteError({"threshold": 0.3})
mae_strategy_01 = MeanAbsoluteError({"threshold": 0.1})
# Set names for validation strategies
mae_strategy_05.set_name("mae_05")
mae_strategy_03.set_name("mae_03")
mae_strategy_01.set_name("mae_01")
# Instatiate Validation entities
v1 = StrategyValidation(["default"], [mae_strategy_05])
v2 = StrategyValidation(["k1"], [mae_strategy_01])
v3 = StrategyValidation(["k2"], [mae_strategy_03])
# Set the names for validations
v1.name = "v1"
v2.name = "v2"
v3.name = "v3"
# Run the Validation entities via special Validator object.
result_collection = Validator().validate(reference, target, [v1, v2, v3])
# Write the outputs
writer.write(result_collection, "test_output")
if __name__ == "__main__":
main()
NOTE: More details on available classes/entities you can find in this tutorial.