Package: padme-conductor
PADME Conductor LibraryA library to simplify the interactions with the Personal Health Train (PHT) and its Stations.Connection ParametersWhen working with the Stations you typically want to retrieve the information of how to connect to the database first.
This can be done with theget_environment_varsfunction, by passing the variable keys which need to be retrieved.env=pc.get_environment_vars(["DATA_SOURCE_USERNAME","DATA_SOURCE_HOST","DATA_SOURCE_PASSWORD","DATA_SOURCE_PORT","STATION_NAME",])Database PluginsThe next step would be to use the connection parameters and query the database of the Station.
For that, we first instantiate a database plugin for the appropriate database type.SQLsql=SqlPlugin(db_name="patientdb",user=env["DATA_SOURCE_USERNAME"],password=env["DATA_SOURCE_PASSWORD"],host=env["DATA_SOURCE_HOST"],port=env["DATA_SOURCE_PORT"],)FHIRfhir_plugin=FHIRClient(f"http://192.168.0.1:8080/fhir")Querying DatabasesWith the database plugin, we can query the data from the Station.
For that, we pass a defaultQueryobject and the current station name to thequeryfunction.result=pc.query(Query("SELECT * FROM patients WHERE age >= 50",sql_plugin))If the queries for each station differ, you can pass a list of queries and the current station name instead:result=pc.query([Query("SELECT * FROM patients WHERE age >= 50",sql,"Klee"),Query("SELECT * FROM patient_info WHERE age >= 50",sql,"Bruegel"),],env["STATION_NAME"],)Executing the AnalysisYou can now design your analysis with the libraries and frameworks you like.
This analysis can be e.g. a Machine Learning model, you set up and train, or an analysis that collects statistical data.To execute the analysis we then pass the analysis function to theexecute_analysisfunction, with all the parameters your function expects.defmy_analysis(query_result):res=len(query_result)pc.log(f"found{res}patients")returnresres=pc.execute_analysis(my_analysis,result)Saving the ResultsWe can then save the results from our analysis in the Train file system.
To simplify this Train interaction we provide thesavefunction.You can separate the saved results, either by each run, each station, or not separate them.
The append parameter defines whether the content should be appended to the file or not.save_string=f"The result is{res}"pc.save(save_string,"result.txt",separate_by=Separation.STATION,append=True)Retrieving Previous ResultsTo retrieve the previous results, like a previous state of a machine learning model, you can use theretrieve_prev_resultfunction.If you have separated your results when saving, you also need to provide the separation strategy when retrieving.prev=pc.retrieve_prev_result("result.txt",separate_by=Separation.STATION)LoggingYou can use thelogfunctions to log simultaneously to stdout/stderr and a log file in the Train file system.
We also provide the ability to add custom tags to a log function with theextraparameter.pc.log("hello world",extra={"tags":["cpu_consumption"]})pc.log_debug("hello world")pc.log_info("hello world")pc.log_warning("hello world")pc.log_error("hello world")pc.log_critical("hello world")Simple exampleThis is a simple example Train-analysis showing the concepts described above.importpadme_conductoraspcfrompadme_conductorimportQuery,Separationfrompadme_conductor.Plugins.SQLimportSqlPluginenv=pc.get_environment_vars(["DATA_SOURCE_USERNAME","DATA_SOURCE_HOST","DATA_SOURCE_PASSWORD","DATA_SOURCE_PORT","STATION_NAME",])sql=SqlPlugin(db_name="patientdb",user=env["DATA_SOURCE_USERNAME"],password=env["DATA_SOURCE_PASSWORD"],host=env["DATA_SOURCE_HOST"],port=env["DATA_SOURCE_PORT"],)result=pc.query([Query("SELECT * FROM patients WHERE age >= 50",sql,"Klee"),Query("SELECT * FROM patient_info WHERE age >= 50",sql,"Bruegel"),],env["STATION_NAME"],)defanalysis(query_result):res=len(query_result)pc.log(f"found{res}patients")returnresres=pc.execute_analysis(analysis,result)prev=pc.retrieve_prev_result("result.txt",separate_by=Separation.STATION)pc.log(prev,extra={"tags":["cpu_consumption"]})# Write to filesave_string=env["STATION_NAME"]+":"+str(res)+"\n"pc.save(save_string,"result.txt",separate_by=Separation.STATION)