From d84103560ecdec17e8fd42b3ce1250a45fa866cb Mon Sep 17 00:00:00 2001 From: castaneda Date: Fri, 21 Mar 2025 15:26:18 +0100 Subject: [PATCH] Update scripts/experiment_remote.py cleaning up of the code and commenting --- scripts/experiment_remote.py | 78 +++++++++++++++++++++--------------- 1 file changed, 45 insertions(+), 33 deletions(-) diff --git a/scripts/experiment_remote.py b/scripts/experiment_remote.py index a8da7b4..326468c 100644 --- a/scripts/experiment_remote.py +++ b/scripts/experiment_remote.py @@ -1,34 +1,46 @@ -#imports for runmanager - labscript -from runmanager_remote import run_experiment -from lyse import * - -import pandas as pd - -import TestDA -import xarray as xr - -if __name__ == '__main__': - - routine_name = 'TestSetup' - - TestVar = {'T_wlm': 1, - 'delta_freq': 0, - 'wait_AWG': 5, - 'buffer_time': 5, - 'carrier_amp': 2 - } - - - hdf_output_file = run_experiment(routine_name, global_var = TestVar) - - print('run finished') - - #df = pd.DataFrame(lyse_run.get_result('wlm', 'wlm_df'), columns = ['timestamp [s]','wavelength [nm]', 'frequency [THz]']) - #print(df) - - #results = TestDA.analysis(hdf_output_file) - - cost = TestDA.cost(hdf_output_file) - #print('I am back') - +#imports for runmanager - labscript +from runmanager_remote import run_experiment + + +import importlib +_module_cache = {} + +# use this script if you want to test a single iteration of NNDy with your sequence and DA, +# if you get the cost dictionary printed at the end as expected, the test is passed + +def cost_function(cost_model, hdf5_file): + module_name = cost_model + + #looks in the cache if the file was accessed already, otherwise imports the cost function + if module_name not in _module_cache: + try: + module = importlib.import_module(module_name) + cost_func = getattr(module, 'cost') + _module_cache[module_name] = { + "cost_func": cost_func , + } + except (ModuleNotFoundError, AttributeError) as e: + raise ImportError(f'Failed to load cost function from "{module_name}.py": {e}') + + cost_model = _module_cache[module_name]["cost_func"] + + return cost_model(hdf5_file) + +if __name__ == '__main__': + + #provide routine_name and cost_model like in NNDy.py + routine_name = '' + cost_model = '' + #set the input parameters with the values that you want to test in a dictionary + TestVar = { + } + + hdf_output_file = run_experiment(routine_name, global_var = TestVar) + + print('run finished') + + + cost = cost_function(cost_model, hdf_output_file) + #print('I am back') + print(cost) \ No newline at end of file