def get_cached_mse(self):
     
     if self.all_fits:
         if scale_fit_terms:
             min_term = unroll_term(self.all_fits[self.cluster_id][self.sim_key]['min_terms'], fit_terms_types['mse'])
             max_term = unroll_term(self.all_fits[self.cluster_id][self.sim_key]['max_terms'], fit_terms_types['mse'])
             return val_scale(self.all_fits[self.cluster_id][self.sim_key]['fit_terms'], max_term, min_term) 
         else:
             return self.all_fits[self.cluster_id][self.sim_key]['fit_terms']['mse']
         
     else:
         error_loading_fit_terms('mse')
    def __init__(self, model, sim_data, cluster_id, reinfection_penalty = 0.0, reinfection_penalty_weight = 0.0,  clinical_cases_penalty = 0.0, clinical_cases_penalty_weight = 0.0, all_fits = None):

        self.cluster_id = cluster_id
        self.reinfection_penalty = reinfection_penalty
        self.reinfection_penalty_term = reinfection_penalty
        self.reinfection_penalty_weight = reinfection_penalty_weight
        self.ref_reinfection_num_points = 0
        
        self.rho = None
        self.p_val = None
        
        self.clinical_cases_penalty = clinical_cases_penalty
        self.clinical_cases_penalty_term = clinical_cases_penalty 
        self.clinical_cases_penalty_weight = clinical_cases_penalty_weight
        self.ref_clinical_cases_num_points = 0
        self.sim_data = sim_data
        
        # pre calculated fits
        self.all_fits = all_fits
        
        self.ref_avg_reinfection_rate = 0.0
        self.sim_avg_reinfection_rate = 0.0 
        
        #debug_p('model id during kariba conversion prior model assignment ' + str(model.get_model_id()))
        self.model = model
        
        model_meta = self.model.get_meta()
        self.sim_key = model_meta['sim_key']
        
        #debug_p('model id during kariba conversion after model assignment ' + str(self.model.get_model_id()))
        
        # get reinfection rates from sim data, compute reinfection penalty and model penalties
        
        if not reinf_weight == 0:
            model_report_channels = sim_report_channels_model_format(reports_channels, self.sim_data)
            if not load_reinf_penalty:
                self.set_reinfection_penalty(model_report_channels['reinfections'], self.cluster_id)
            else:
                if self.all_fits:
                    self.reinfection_penalty = self.all_fits[self.cluster_id][self.sim_key]['reinf_penalty']
                    self.reinfection_penalty_weight = reinf_weight
                else:
                    error_loading_fit_terms('reinfection penalty')
                    

        if not load_cc_penalty:
            if 'ls_folded_norm' in cc_penalty_model: 
                self.set_clinical_cases_penalty_by_ls(self.sim_data['cc'], self.cluster_id)
            if 'ls_folded_no_norm' in cc_penalty_model: 
                self.set_clinical_cases_penalty_by_ls_no_norm(self.sim_data['cc'], self.cluster_id)
            if 'corr' in cc_penalty_model:
                self.set_clinical_cases_penalty_by_corr(self.sim_data['cc'], self.cluster_id)
        else:
            if self.all_fits:
                max_term = 0
                min_term = 0
                if 'ls_folded_norm' in cc_penalty_model: 
                    self.clinical_cases_penalty = self.all_fits[self.cluster_id][self.sim_key]['fit_terms']['cc_penalty']['ls_norm'] 
                    if scale_fit_terms:                        
                        max_term = unroll_term(self.all_fits[self.cluster_id][self.sim_key]['max_terms'], fit_terms_types['ls_norm'])
                        min_term = unroll_term(self.all_fits[self.cluster_id][self.sim_key]['min_terms'], fit_terms_types['ls_norm']) 

                elif 'ls_norm_not_folded' in cc_penalty_model:
                    self.clinical_cases_penalty = self.all_fits[self.cluster_id][self.sim_key]['fit_terms']['cc_penalty']['ls_norm_not_folded']
                    if scale_fit_terms:
                        
                        # change path in fit_terms_types for ls_norm_not_folded if we use that again; need to add corresponding entry as well 
                        # if we are not using that feature again, remove these lines altogether; this is just a placeholder
                        max_term = unroll_term(self.all_fits[self.cluster_id][self.sim_key]['max_terms'], fit_terms_types['ls_norm'])
                        min_term = unroll_term(self.all_fits[self.cluster_id][self.sim_key]['min_terms'], fit_terms_types['ls_norm'])
                
                
                if 'ls_no_norm' in cc_penalty_model: 
                    self.clinical_cases_penalty = self.all_fits[self.cluster_id][self.sim_key]['fit_terms']['cc_penalty']['ls_no_norm']
                    if scale_fit_terms:
                        
                        # change path in fit_terms_types for ls_norm_not_folded if we use that again; need to add corresponding entry as well 
                        # if we are not using that feature again, remove these lines altogether; this is just a placeholder
                        max_term = unroll_term(self.all_fits[self.cluster_id][self.sim_key]['max_terms'], fit_terms_types['ls_norm'])
                        min_term = unroll_term(self.all_fits[self.cluster_id][self.sim_key]['min_terms'], fit_terms_types['ls_norm'])
                        
                if 'corr_folded' in cc_penalty_model:
                    self.clinical_cases_penalty = self.all_fits[self.cluster_id][self.sim_key]['fit_terms']['cc_penalty']['corr_folded']['penalty']
                    
                    if scale_fit_terms:
                        max_term = unroll_term(self.all_fits[self.cluster_id][self.sim_key]['max_terms'], fit_terms_types['corr_folded'])
                        min_term = unroll_term(self.all_fits[self.cluster_id][self.sim_key]['min_terms'], fit_terms_types['corr_folded'])
                    
                    
                    self.rho = self.all_fits[self.cluster_id][self.sim_key]['fit_terms']['cc_penalty']['corr_folded']['rho']
                    self.p_val = self.all_fits[self.cluster_id][self.sim_key]['fit_terms']['cc_penalty']['corr_folded']['p_val']
                    

                if 'corr_not_folded' in cc_penalty_model:
                    self.clinical_cases_penalty = self.all_fits[self.cluster_id][self.sim_key]['fit_terms']['cc_penalty']['corr_not_folded']['penalty']
                    
                    if scale_fit_terms:
                        
                        # change path in fit_terms_types for ls_norm_not_folded if we use that again; need to add corresponding entry as well 
                        # if we are not using that feature again, remove these lines altogether; this is just a placeholder
                        max_term = unroll_term(self.all_fits[self.cluster_id][self.sim_key]['max_terms'], fit_terms_types['corr_not_folded'])
                        min_term = unroll_term(self.all_fits[self.cluster_id][self.sim_key]['min_terms'], fit_terms_types['corr_not_folded'])
                    
                    
                    self.rho = self.all_fits[self.cluster_id][self.sim_key]['fit_terms']['cc_penalty']['corr_not_folded']['rho']
                    self.p_val = self.all_fits[self.cluster_id][self.sim_key]['fit_terms']['cc_penalty']['corr_not_folded']['p_val']
                
                self.clinical_cases_penalty_term = self.clinical_cases_penalty
                if scale_fit_terms: # should have found proper min_term and max_term if scale_fit_terms is True
                    self.clinical_cases_penalty = val_scale(self.clinical_cases_penalty, max_term, min_term)
                    
                self.clinical_cases_penalty_weight = cc_weight
                
            else:
                error_loading_fit_terms('clinical cases penalty')
            
                    
        self.set_model_penalties()