def get_elife(run_id, elife_conf): if isinstance(elife_conf, tuple) and len(elife_conf) == 3: # We want to use corrections management is_nt = elife_conf[-1] cmt = straxen.CorrectionsManagementServices(is_nt=is_nt) e = cmt.get_corrections_config(run_id, elife_conf[:2]) elif isinstance(elife_conf, str): warn("get_elife will be replaced by CorrectionsManagementSevices", DeprecationWarning, 2) # Let's remove these functions and only rely on the CMT in the future x = straxen.get_resource(elife_conf, fmt='npy') run_index = np.where(x['run_id'] == int(run_id))[0] if not len(run_index): # Gains not known: using placeholders e = 623e3 else: e = x[run_index[0]]['e_life'] else: raise ValueError( 'Wrong elife model. Either specify a string (url) or the ' 'Corrections Management Tools format: ' '(model_type->str, model_config->str, is_nT->bool)' '') return e
def get_correction_from_cmt(run_id, conf): if isinstance(conf, str) and conf.startswith('https://raw'): # Legacy support for pax files return conf if isinstance(conf, tuple) and len(conf) == 3: is_nt = conf[-1] model_type, global_version = conf[:2] correction = global_version # in case is a single value if 'constant' in model_type: if not isinstance(global_version, (float, int)): raise ValueError(f"User specify a model type {model_type} " "and should provide a number. Got: " f"{type(global_version)}") else: cmt = straxen.CorrectionsManagementServices(is_nt=is_nt) correction = cmt.get_corrections_config(run_id, conf[:2]) if correction.size == 0: raise ValueError(f"Could not find a value for {model_type} " "please check it is implemented in CMT. " f"for nT = {is_nt}") if 'samples' in model_type: return int(correction) else: return float(correction) else: raise ValueError( "Wrong configuration. " "Please use the following format: " "(model_type->str, model_config->str or number, is_nT->bool) " f"User specify {conf} please modify")
def test_offline(): """ Let's try and see which CMT versions are compatible with this straxen version """ cmt = straxen.CorrectionsManagementServices() cmt_versions = list(cmt.global_versions)[::-1] print(cmt_versions) success_for = [] for global_version in cmt_versions: try: xenonnt(global_version) success_for.append(global_version) except straxen.CMTVersionError: pass print(f'This straxen version works with {success_for} but is ' f'incompatible with {set(cmt_versions)-set(success_for)}') test = unittest.TestCase() # We should always work for one offline and the online version test.assertTrue(len(success_for) >= 2)
def get_config_from_cmt(run_id, conf): if isinstance(conf, str) and conf.startswith('https://raw'): # Legacy support for pax files return conf if not isinstance(conf, tuple): raise ValueError("conf must be a tuple") if not len(conf) == 3: raise ValueError("conf must have three elements: " "the model type, its specific configuration " "and detector (True = nT)") model_type, model_conf, is_nt = conf if model_type == 'CMT_model': cmt = straxen.CorrectionsManagementServices(is_nt=is_nt) this_file = cmt.get_corrections_config(run_id, model_conf) this_file = ' '.join(map(str, this_file)) else: raise ValueError(f"Wrong NN configuration, please look at this {conf} " "and modify it accordingly") return this_file
def get_correction_from_cmt(run_id, conf): """ Get correction from CMT general format is conf = ('correction_name', 'version', True) where True means looking at nT runs, e.g. get_correction_from_cmt(run_id, conf[:2]) special cases: version can be replaced by constant int, float or array when user specify value(s) :param run_id: run id from runDB :param conf: configuration :return: correction value(s) """ if isinstance(conf, str): # Legacy support for pax files return conf elif isinstance(conf, tuple) and len(conf) == 2: model_conf, cte_value = conf[:2] # special case constant to_pe values if model_conf in FIXED_TO_PE: correction = FIXED_TO_PE[model_conf] return correction # special case constant single value or list of values. elif 'constant' in model_conf: if not isinstance(cte_value, (float, int, str, list, tuple)): raise ValueError( f"User specify a model type {model_conf} " "and should provide a number or list of numbers. Got: " f"{type(cte_value)}") correction = cte_value return correction elif isinstance(conf, tuple) and len(conf) == 3: model_conf, global_version, is_nt = conf[:3] cmt = straxen.CorrectionsManagementServices(is_nt=is_nt) correction = cmt.get_corrections_config(run_id, conf[:2]) if correction.size == 0: raise ValueError(f"Could not find a value for {model_conf} " f"please check it is implemented in CMT. ") if model_conf in corrections_w_file: # file's name (maps, NN, etc) correction = ' '.join(map(str, correction)) return correction if model_conf in dict_corrections: return correction[0] elif model_conf in single_value_corrections: if 'samples' in model_conf: # int baseline samples, etc return int(correction) else: return float(correction) # float elife, drift velocity, etc elif model_conf in arrays_corrections: np_correction = correction.reshape(correction.size) np_correction = np_correction.astype( np.int16 ) # not sure if straxen can handle dtype:object therefore specify dtype return np_correction return correction else: raise ValueError( "Wrong configuration. " "Please use the following format: " "(config->str, model_config->str or number, is_nT->bool) " f"User specify {conf} please modify")
def get_to_pe(run_id, gain_model, n_pmts): if not isinstance(gain_model, tuple): raise ValueError(f"gain_model must be a tuple") if not len(gain_model) == 2: raise ValueError(f"gain_model must have two elements: " f"the model type and its specific configuration") model_type, model_conf = gain_model if model_type == 'disabled': # Somebody messed up raise RuntimeError("Attempt to use a disabled gain model") if model_type == 'CMT_model': if not isinstance(model_conf, tuple) or len(model_conf) != 2: # Raise a value error if the condition is not met. We should have: # ("CMT_model", -> To specify that we want to use the online # corrections management tool # ( # "to_pe_model", -> This is to specify that we want the gains # "v1", -> The version of the correction 'v1' is the online version # ) raise ValueError('CMT gain model should be similar to:' '("CMT_model", ("to_pe_model", "v1"). Instead got:' f'{model_conf}') # is this the best way to do this? is_nt = n_pmts == straxen.n_tpc_pmts or n_pmts == straxen.n_nveto_pmts or n_pmts == straxen.n_mveto_pmts corrections = straxen.CorrectionsManagementServices(is_nt=is_nt) to_pe = corrections.get_corrections_config(run_id, model_conf) return to_pe elif model_type == 'to_pe_per_run': warn("to_pe_per_run will be replaced by CorrectionsManagementSevices", DeprecationWarning, 2) # Load a npy file specifying a run_id -> to_pe array to_pe_file = model_conf x = straxen.get_resource(to_pe_file, fmt='npy') run_index = np.where(x['run_id'] == int(run_id))[0] if not len(run_index): # Gains not known: using placeholders run_index = [-1] to_pe = x[run_index[0]]['to_pe'] elif model_type == 'to_pe_constant': if model_conf in FIXED_TO_PE: return FIXED_TO_PE[model_conf] try: # Uniform gain, specified as a to_pe factor to_pe = np.ones(n_pmts, dtype=np.float32) * model_conf except np.core._exceptions.UFuncTypeError as e: raise(str(e) + f'\nTried multiplying by {model_conf}. Insert a number instead.') else: raise NotImplementedError(f"Gain model type {model_type} not implemented") if len(to_pe) != n_pmts: raise ValueError( f"Gain model {gain_model} resulted in a to_pe " f"of length {len(to_pe)}, but n_pmts is {n_pmts}!") return to_pe