def back_calc_peak_intensities(spin=None, exp_type=None, frq=None, offset=None, point=None): """Back-calculation of peak intensity for the given relaxation time. @keyword spin: The specific spin data container. @type spin: SpinContainer instance @keyword exp_type: The experiment type. @type exp_type: str @keyword frq: The spectrometer frequency. @type frq: float @keyword offset: For R1rho-type data, the spin-lock offset value in ppm. @type offset: None or float @keyword point: The dispersion point data (either the spin-lock field strength in Hz or the nu_CPMG frequency in Hz). @type point: float @return: The back-calculated peak intensities for the given exponential curve. @rtype: numpy rank-1 float array """ # Check. if not has_exponential_exp_type(): raise RelaxError("Back-calculation is not allowed for the fixed time experiment types.") # The key. param_key = return_param_key_from_data(exp_type=exp_type, frq=frq, offset=offset, point=point) # Create the initial parameter vector. param_vector = assemble_param_vector(spins=[spin], key=param_key) # The peak intensities and times. values = [] errors = [] times = [] for time in loop_time(exp_type=exp_type, frq=frq, offset=offset, point=point): # The data. values.append(average_intensity(spin=spin, exp_type=exp_type, frq=frq, offset=offset, point=point, time=time)) errors.append(average_intensity(spin=spin, exp_type=exp_type, frq=frq, offset=offset, point=point, time=time, error=True)) times.append(time) # The scaling matrix in a diagonalised list form. scaling_list = [] for i in range(len(param_vector)): scaling_list.append(1.0) # Initialise the relaxation fit functions. model = Relax_fit_opt(model='exp', num_params=len(spin.params), values=values, errors=errors, relax_times=times, scaling_matrix=scaling_list) # Make a single function call. This will cause back calculation and the data will be stored in the C module. model.func(param_vector) # Get the data back. results = model.back_calc_data() # Return the correct peak height. return results
def test_has_exponential_exp_type_cpmg(self): """Unit test of the has_exponential_exp_type() function. This uses the data of the saved state attached to U{bug #21665<https://gna.org/bugs/?21665>}. """ # Load the state. statefile = status.install_path + sep+'test_suite'+sep+'shared_data'+sep+'dispersion'+sep+'bug_21665.bz2' state.load_state(statefile, force=True) # Check the return of has_exponential_exp_type. exponential_exp_type = has_exponential_exp_type() print(exponential_exp_type) self.assertEqual(exponential_exp_type, False)
def test_has_exponential_exp_type_cpmg(self): """Unit test of the has_exponential_exp_type() function. This uses the data of the saved state attached to U{bug #21665<https://web.archive.org/web/https://gna.org/bugs/?21665>}. """ # Load the state. statefile = status.install_path + sep + 'test_suite' + sep + 'shared_data' + sep + 'dispersion' + sep + 'bug_21665.bz2' state.load_state(statefile, force=True) # Check the return of has_exponential_exp_type. exponential_exp_type = has_exponential_exp_type() print(exponential_exp_type) self.assertEqual(exponential_exp_type, False)
def write_results(self, path=None, model=None): """Create a set of results, text and Grace files for the current data pipe. @keyword path: The directory to place the files into. @type path: str """ # Printout. section(file=sys.stdout, text="Results writing", prespace=2) # If this is the final model selection round, check which models have been tested. if model == None: models_tested = [] for spin, spin_id in spin_loop(return_id=True, skip_desel=True): spin_model = spin.model # Add to list, if not in already. if spin_model not in models_tested: models_tested.append(spin_model) else: models_tested = None # Special for R2eff model. if model == MODEL_R2EFF: # The R2eff parameter. self.interpreter.value.write(param='r2eff', file='r2eff.out', dir=path, force=True) self.interpreter.grace.write(x_data_type='res_num', y_data_type='r2eff', file='r2eff.agr', dir=path, force=True) # Exponential curves. if has_exponential_exp_type(): self.interpreter.relax_disp.plot_exp_curves( file='intensities.agr', dir=path, force=True) # Average peak intensities. self.interpreter.relax_disp.plot_exp_curves( file='intensities_norm.agr', dir=path, force=True, norm=True) # Average peak intensities (normalised). # The I0 parameter. self.interpreter.value.write(param='i0', file='i0.out', dir=path, force=True) self.interpreter.grace.write(x_data_type='res_num', y_data_type='i0', file='i0.agr', dir=path, force=True) # Dispersion curves. self.interpreter.relax_disp.plot_disp_curves(dir=path, force=True) self.interpreter.relax_disp.write_disp_curves(dir=path, force=True) # The selected models for the final run. if model == None: self.interpreter.value.write(param='model', file='model.out', dir=path, force=True) # For CPMG models. if has_cpmg_exp_type(): # The R20 parameter. self.write_results_test(path=path, model=model, models_tested=models_tested, param='r2', file_name_ini='r20') # The R20A and R20B parameters. self.write_results_test(path=path, model=model, models_tested=models_tested, param='r2a', file_name_ini='r20a') self.write_results_test(path=path, model=model, models_tested=models_tested, param='r2b', file_name_ini='r20b') # For R1ho models. if has_r1rho_exp_type(): # The R1 parameter. self.write_results_test(path=path, model=model, models_tested=models_tested, param='r1') # The R1rho prime parameter. self.write_results_test(path=path, model=model, models_tested=models_tested, param='r2', file_name_ini='r1rho_prime') # Plot specific R1rho graphs. if model in [None] + MODEL_LIST_R1RHO: self.interpreter.relax_disp.plot_disp_curves( dir=path, x_axis=X_AXIS_THETA, force=True) self.interpreter.relax_disp.plot_disp_curves( dir=path, y_axis=Y_AXIS_R2_R1RHO, x_axis=X_AXIS_W_EFF, force=True) self.interpreter.relax_disp.plot_disp_curves( dir=path, y_axis=Y_AXIS_R2_EFF, x_axis=X_AXIS_THETA, interpolate=INTERPOLATE_OFFSET, force=True) # The calculation of theta and w_eff parameter in R1rho experiments. if model in MODEL_LIST_R1RHO_FULL: self.interpreter.value.write(param='theta', file='theta.out', dir=path, force=True) self.interpreter.value.write(param='w_eff', file='w_eff.out', dir=path, force=True) # The pA and pB parameters. self.write_results_test(path=path, model=model, models_tested=models_tested, param='pA') self.write_results_test(path=path, model=model, models_tested=models_tested, param='pB') # The pC parameter. self.write_results_test(path=path, model=model, models_tested=models_tested, param='pC') # The phi_ex parameter. self.write_results_test(path=path, model=model, models_tested=models_tested, param='phi_ex') # The phi_ex_B nd phi_ex_C parameters. self.write_results_test(path=path, model=model, models_tested=models_tested, param='phi_ex_B') self.write_results_test(path=path, model=model, models_tested=models_tested, param='phi_ex_C') # The dw parameter. self.write_results_test(path=path, model=model, models_tested=models_tested, param='dw') # The dw_AB, dw_BC and dw_AC parameter. self.write_results_test(path=path, model=model, models_tested=models_tested, param='dw_AB') self.write_results_test(path=path, model=model, models_tested=models_tested, param='dw_BC') self.write_results_test(path=path, model=model, models_tested=models_tested, param='dw_AC') # The dwH parameter. self.write_results_test(path=path, model=model, models_tested=models_tested, param='dwH') # The dwH_AB, dwH_BC and dwH_AC parameter. self.write_results_test(path=path, model=model, models_tested=models_tested, param='dwH_AB') self.write_results_test(path=path, model=model, models_tested=models_tested, param='dwH_BC') self.write_results_test(path=path, model=model, models_tested=models_tested, param='dwH_AC') # The k_AB, kex and tex parameters. self.write_results_test(path=path, model=model, models_tested=models_tested, param='k_AB') self.write_results_test(path=path, model=model, models_tested=models_tested, param='kex') self.write_results_test(path=path, model=model, models_tested=models_tested, param='tex') # The kex_AB, kex_BC, kex_AC parameters. self.write_results_test(path=path, model=model, models_tested=models_tested, param='kex_AB') self.write_results_test(path=path, model=model, models_tested=models_tested, param='kex_BC') self.write_results_test(path=path, model=model, models_tested=models_tested, param='kex_AC') # The kB and kC parameters. self.write_results_test(path=path, model=model, models_tested=models_tested, param='kB') self.write_results_test(path=path, model=model, models_tested=models_tested, param='kC') # Minimisation statistics. if not (model == MODEL_R2EFF and has_fixed_time_exp_type()): self.interpreter.value.write(param='chi2', file='chi2.out', dir=path, force=True) self.interpreter.grace.write(y_data_type='chi2', file='chi2.agr', dir=path, force=True) # Finally save the results. This is last to allow the continuation of an interrupted analysis while ensuring that all results files have been created. self.interpreter.results.write(file='results', dir=path, force=True)
def run(self): """Execute the auto-analysis.""" # Peak intensity error analysis. if MODEL_R2EFF in self.models: self.error_analysis() # R1 parameter fitting. if self.r1_fit: subtitle(file=sys.stdout, text="R1 parameter optimisation activation", prespace=3) self.interpreter.relax_disp.r1_fit(fit=self.r1_fit) else: # No print out. self.interpreter.relax_disp.r1_fit(fit=self.r1_fit) # Loop over the models. self.model_pipes = [] for model in self.models: # Printout. subtitle(file=sys.stdout, text="The '%s' model" % model, prespace=3) # The results directory path. model_path = model.replace(" ", "_") path = self.results_dir + sep + model_path # The name of the data pipe for the model. model_pipe = self.name_pipe(model) if self.is_model_for_selection(model): self.model_pipes.append(model_pipe) # Check that results do not already exist - i.e. a previous run was interrupted. path1 = path + sep + 'results' path2 = path1 + '.bz2' path3 = path1 + '.gz' if access(path1, F_OK) or access(path2, F_OK) or access( path2, F_OK): # Printout. print( "Detected the presence of results files for the '%s' model - loading these instead of performing optimisation for a second time." % model) # Create a data pipe and switch to it. self.interpreter.pipe.create(pipe_name=model_pipe, pipe_type='relax_disp', bundle=self.pipe_bundle) self.interpreter.pipe.switch(model_pipe) # Load the results. self.interpreter.results.read(file='results', dir=path) # Jump to the next model. continue # Create the data pipe by copying the base pipe, then switching to it. self.interpreter.pipe.copy(pipe_from=self.pipe_name, pipe_to=model_pipe, bundle_to=self.pipe_bundle) self.interpreter.pipe.switch(model_pipe) # Select the model. self.interpreter.relax_disp.select_model(model) # Copy the R2eff values from the R2eff model data pipe. if model != MODEL_R2EFF and MODEL_R2EFF in self.models: self.interpreter.value.copy( pipe_from=self.name_pipe(MODEL_R2EFF), pipe_to=model_pipe, param='r2eff') # Calculate the R2eff values for the fixed relaxation time period data types. if model == MODEL_R2EFF and not has_exponential_exp_type(): self.interpreter.minimise.calculate() # Optimise the model. else: self.optimise(model=model, model_path=model_path) # Write out the results. self.write_results(path=path, model=model) # The final model selection data pipe. if len(self.models) >= 2: # Printout. section(file=sys.stdout, text="Final results", prespace=2) # Perform model selection. self.interpreter.model_selection( method=self.modsel, modsel_pipe=self.name_pipe('final'), bundle=self.pipe_bundle, pipes=self.model_pipes) # Final Monte Carlo simulations only. if not self.mc_sim_all_models: self.interpreter.monte_carlo.setup(number=self.mc_sim_num) self.interpreter.monte_carlo.create_data() self.interpreter.monte_carlo.initial_values() self.interpreter.minimise.execute( 'simplex', func_tol=self.opt_func_tol, max_iter=self.opt_max_iterations, constraints=True) if self.eliminate: self.interpreter.eliminate() self.interpreter.monte_carlo.error_analysis() # Writing out the final results. self.write_results(path=self.results_dir + sep + 'final') # No model selection. else: warn( RelaxWarning( "Model selection in the dispersion auto-analysis has been skipped as only %s models have been optimised." % len(self.model_pipes))) # Finally save the program state. self.interpreter.state.save(state='final_state', dir=self.results_dir, force=True)
def param_num(spins=None): """Determine the number of parameters in the model. @keyword spins: The list of spin data containers for the block. @type spins: list of SpinContainer instances @return: The number of model parameters. @rtype: int """ # Initialise the number. num = 0 # The R2eff model. if cdp.model_type == 'R2eff': # Count the selected spins. spin_num = count_spins(spins) # Exponential curves (with clustering). if has_exponential_exp_type(): return 2 * spin_num # Fixed time period experiments (with clustering). return 1 * spin_num # Check the spin cluster. for spin in spins: # Skip deselected spins. if not spin.select: continue if len(spin.params) != len(spins[0].params): raise RelaxError("The number of parameters for each spin in the cluster are not the same.") # Count the number of R10 parameters. for spin in spins: # Skip deselected spins. if not spin.select: continue for i in range(len(spin.params)): if spin.params[i] in ['r1']: for exp_type, frq in loop_exp_frq(): num += 1 # Count the number of R20 parameters. for spin in spins: # Skip deselected spins. if not spin.select: continue for i in range(len(spin.params)): if spin.params[i] in PARAMS_R20: for exp_type, frq in loop_exp_frq(): num += 1 # Count the number of spin specific parameters for all spins. spin_params = ['phi_ex', 'phi_ex_B', 'phi_ex_C', 'padw2', 'dw', 'dwH'] for spin in spins: # Skip deselected spins. if not spin.select: continue for i in range(len(spin.params)): if spin.params[i] in spin_params: num += 1 # Count all other parameters, but only for a single spin. all_params = ['r1'] + PARAMS_R20 + spin_params for spin in spins: # Skip deselected spins. if not spin.select: continue for i in range(len(spin.params)): if not spin.params[i] in all_params: num += 1 break # Return the number. return num
def back_calc_peak_intensities(spin=None, spin_id=None, exp_type=None, frq=None, offset=None, point=None): """Back-calculation of peak intensity for the given relaxation time. @keyword spin: The specific spin data container. @type spin: SpinContainer instance @keyword spin_id: The optional spin ID string for use in warning messages. @type spin_id: str or None @keyword exp_type: The experiment type. @type exp_type: str @keyword frq: The spectrometer frequency. @type frq: float @keyword offset: For R1rho-type data, the spin-lock offset value in ppm. @type offset: None or float @keyword point: The dispersion point data (either the spin-lock field strength in Hz or the nu_CPMG frequency in Hz). @type point: float @return: The back-calculated peak intensities for the given exponential curve. @rtype: numpy rank-1 float array """ # Check. if not has_exponential_exp_type(): raise RelaxError("Back-calculation is not allowed for the fixed time experiment types.") # The key. param_key = return_param_key_from_data(exp_type=exp_type, frq=frq, offset=offset, point=point) # Create the initial parameter vector. param_vector = assemble_param_vector(spins=[spin], key=param_key) # The peak intensities and times. values = [] errors = [] times = [] for time in loop_time(exp_type=exp_type, frq=frq, offset=offset, point=point): # Check the peak intensity keys. int_keys = find_intensity_keys(exp_type=exp_type, frq=frq, offset=offset, point=point, time=time) for i in range(len(int_keys)): if int_keys[i] not in spin.peak_intensity: if spin_id: warn(RelaxWarning("The spin %s peak intensity key '%s' is not present, skipping the back-calculation." % (spin_id, int_keys[i]))) else: warn(RelaxWarning("The peak intensity key '%s' is not present, skipping the back-calculation." % int_keys[i])) return # The data. values.append(average_intensity(spin=spin, exp_type=exp_type, frq=frq, offset=offset, point=point, time=time)) errors.append(average_intensity(spin=spin, exp_type=exp_type, frq=frq, offset=offset, point=point, time=time, error=True)) times.append(time) # The scaling matrix in a diagonalised list form. scaling_list = [] for i in range(len(param_vector)): scaling_list.append(1.0) # Initialise the relaxation fit functions. model = Relax_fit_opt(model='exp', num_params=len(spin.params), values=values, errors=errors, relax_times=times, scaling_matrix=scaling_list) # Make a single function call. This will cause back calculation and the data will be stored in the C module. model.func(param_vector) # Get the data back. results = model.back_calc_data() # Return the correct peak height. return results
def loop_parameters(spins=None): """Generator function for looping of the model parameters of the cluster. @keyword spins: The list of spin data containers for the block. @type spins: list of SpinContainer instances @return: The parameter name, the parameter index (for the parameter vector), the spin index (for the cluster), and the R20 parameter key (for R20, R20A, and R20B parameters stored as dictionaries). @rtype: str, int, int, str """ # Make sure that the R1 parameter is correctly set up. r1_setup() # The parameter index. param_index = -1 # The R2eff model. if cdp.model_type == 'R2eff': # Loop over the spins. for spin_index in range(len(spins)): # Skip deselected spins. if not spins[spin_index].select: continue # One or two parameters. params = ['r2eff'] if has_exponential_exp_type(): params = ['r2eff', 'i0'] # Yield the parameters. for param in params: # First increment the indices. param_index += 1 # Yield the data. yield param, param_index, spin_index, None # All other models. else: # First the R1 fit parameter (one per spin per field strength). for spin_index in range(len(spins)): # Skip deselected spins. if not spins[spin_index].select: continue # The parameters. for param in PARAMS_R1: if param in spins[spin_index].params: for exp_type, frq in loop_exp_frq(): param_index += 1 yield param, param_index, spin_index, generate_r20_key( exp_type=exp_type, frq=frq) # Then the R2 parameters (one per spin per field strength). for spin_index in range(len(spins)): # Skip deselected spins. if not spins[spin_index].select: continue # The parameters. for param in PARAMS_R20: if param in spins[spin_index].params: for exp_type, frq in loop_exp_frq(): param_index += 1 yield param, param_index, spin_index, generate_r20_key( exp_type=exp_type, frq=frq) # Then the chemical shift difference parameters (one per spin). for spin_index in range(len(spins)): # Skip deselected spins. if not spins[spin_index].select: continue # Yield the data. for param in PARAMS_CHEM_SHIFT_DIFF: if param in spins[spin_index].params: param_index += 1 yield param, param_index, spin_index, None # Then a separate block for the proton chemical shift difference parameters for the MQ models (one per spin). for spin_index in range(len(spins)): # Skip deselected spins. if not spins[spin_index].select: continue # Yield the data. for param in PARAMS_CHEM_SHIFT_DIFF_MMQ: if param in spins[spin_index].params: param_index += 1 yield param, param_index, spin_index, None # All other parameters (one per spin cluster). for spin_index in range(len(spins)): # Skip deselected spins. if not spins[spin_index].select: continue # The parameters. for param in spins[0].params: if not param in PARAMS_SPIN: param_index += 1 yield param, param_index, None, None # No more spins. break
def write_results(self, path=None, model=None): """Create a set of results, text and Grace files for the current data pipe. @keyword path: The directory to place the files into. @type path: str """ # Printout. section(file=sys.stdout, text="Results writing", prespace=2) # If this is the final model selection round, check which models have been tested. if model == None: models_tested = [] for spin, spin_id in spin_loop(return_id=True, skip_desel=True): spin_model = spin.model # Add to list, if not in already. if spin_model not in models_tested: models_tested.append(spin_model) else: models_tested = None # Special for R2eff model. if model == MODEL_R2EFF: # The R2eff parameter. self.interpreter.value.write(param='r2eff', file='r2eff.out', dir=path, force=True) self.interpreter.grace.write(x_data_type='res_num', y_data_type='r2eff', file='r2eff.agr', dir=path, force=True) # Exponential curves. if has_exponential_exp_type(): self.interpreter.relax_disp.plot_exp_curves(file='intensities.agr', dir=path, force=True) # Average peak intensities. self.interpreter.relax_disp.plot_exp_curves(file='intensities_norm.agr', dir=path, force=True, norm=True) # Average peak intensities (normalised). # The I0 parameter. self.interpreter.value.write(param='i0', file='i0.out', dir=path, force=True) self.interpreter.grace.write(x_data_type='res_num', y_data_type='i0', file='i0.agr', dir=path, force=True) # Dispersion curves. self.interpreter.relax_disp.plot_disp_curves(dir=path, force=True) self.interpreter.relax_disp.write_disp_curves(dir=path, force=True) # The selected models for the final run. if model == None: self.interpreter.value.write(param='model', file='model.out', dir=path, force=True) # For CPMG models. if has_cpmg_exp_type(): # The R20 parameter. self.write_results_test(path=path, model=model, models_tested=models_tested, param='r2', file_name_ini='r20') # The R20A and R20B parameters. self.write_results_test(path=path, model=model, models_tested=models_tested, param='r2a', file_name_ini='r20a') self.write_results_test(path=path, model=model, models_tested=models_tested, param='r2b', file_name_ini='r20b') # For R1ho models. if has_r1rho_exp_type(): # The R1 parameter. self.write_results_test(path=path, model=model, models_tested=models_tested, param='r1') # The R1rho prime parameter. self.write_results_test(path=path, model=model, models_tested=models_tested, param='r2', file_name_ini='r1rho_prime') # Plot specific R1rho graphs. if model in [None] + MODEL_LIST_R1RHO: self.interpreter.relax_disp.plot_disp_curves(dir=path, x_axis=X_AXIS_THETA, force=True) self.interpreter.relax_disp.plot_disp_curves(dir=path, y_axis=Y_AXIS_R2_R1RHO, x_axis=X_AXIS_W_EFF, force=True) self.interpreter.relax_disp.plot_disp_curves(dir=path, y_axis=Y_AXIS_R2_EFF, x_axis=X_AXIS_THETA, interpolate=INTERPOLATE_OFFSET, force=True) # The calculation of theta and w_eff parameter in R1rho experiments. if model in MODEL_LIST_R1RHO_FULL: self.interpreter.value.write(param='theta', file='theta.out', dir=path, force=True) self.interpreter.value.write(param='w_eff', file='w_eff.out', dir=path, force=True) # The pA and pB parameters. self.write_results_test(path=path, model=model, models_tested=models_tested, param='pA') self.write_results_test(path=path, model=model, models_tested=models_tested, param='pB') # The pC parameter. self.write_results_test(path=path, model=model, models_tested=models_tested, param='pC') # The phi_ex parameter. self.write_results_test(path=path, model=model, models_tested=models_tested, param='phi_ex') # The phi_ex_B nd phi_ex_C parameters. self.write_results_test(path=path, model=model, models_tested=models_tested, param='phi_ex_B') self.write_results_test(path=path, model=model, models_tested=models_tested, param='phi_ex_C') # The dw parameter. self.write_results_test(path=path, model=model, models_tested=models_tested, param='dw') # The dw_AB, dw_BC and dw_AC parameter. self.write_results_test(path=path, model=model, models_tested=models_tested, param='dw_AB') self.write_results_test(path=path, model=model, models_tested=models_tested, param='dw_BC') self.write_results_test(path=path, model=model, models_tested=models_tested, param='dw_AC') # The dwH parameter. self.write_results_test(path=path, model=model, models_tested=models_tested, param='dwH') # The dwH_AB, dwH_BC and dwH_AC parameter. self.write_results_test(path=path, model=model, models_tested=models_tested, param='dwH_AB') self.write_results_test(path=path, model=model, models_tested=models_tested, param='dwH_BC') self.write_results_test(path=path, model=model, models_tested=models_tested, param='dwH_AC') # The k_AB, kex and tex parameters. self.write_results_test(path=path, model=model, models_tested=models_tested, param='k_AB') self.write_results_test(path=path, model=model, models_tested=models_tested, param='kex') self.write_results_test(path=path, model=model, models_tested=models_tested, param='tex') # The kex_AB, kex_BC, kex_AC parameters. self.write_results_test(path=path, model=model, models_tested=models_tested, param='kex_AB') self.write_results_test(path=path, model=model, models_tested=models_tested, param='kex_BC') self.write_results_test(path=path, model=model, models_tested=models_tested, param='kex_AC') # The kB and kC parameters. self.write_results_test(path=path, model=model, models_tested=models_tested, param='kB') self.write_results_test(path=path, model=model, models_tested=models_tested, param='kC') # Minimisation statistics. if not (model == MODEL_R2EFF and has_fixed_time_exp_type()): self.interpreter.value.write(param='chi2', file='chi2.out', dir=path, force=True) self.interpreter.grace.write(y_data_type='chi2', file='chi2.agr', dir=path, force=True) # Finally save the results. This is last to allow the continuation of an interrupted analysis while ensuring that all results files have been created. self.interpreter.results.write(file='results', dir=path, force=True)
def run(self): """Execute the auto-analysis.""" # Peak intensity error analysis. if MODEL_R2EFF in self.models: self.error_analysis() # R1 parameter fitting. if self.r1_fit: subtitle(file=sys.stdout, text="R1 parameter optimisation activation", prespace=3) self.interpreter.relax_disp.r1_fit(fit=self.r1_fit) else: # No print out. self.interpreter.relax_disp.r1_fit(fit=self.r1_fit) # Loop over the models. self.model_pipes = [] for model in self.models: # Printout. subtitle(file=sys.stdout, text="The '%s' model" % model, prespace=3) # The results directory path. model_path = model.replace(" ", "_") path = self.results_dir+sep+model_path # The name of the data pipe for the model. model_pipe = self.name_pipe(model) if self.is_model_for_selection(model): self.model_pipes.append(model_pipe) # Check that results do not already exist - i.e. a previous run was interrupted. path1 = path + sep + 'results' path2 = path1 + '.bz2' path3 = path1 + '.gz' if access(path1, F_OK) or access(path2, F_OK) or access(path2, F_OK): # Printout. print("Detected the presence of results files for the '%s' model - loading these instead of performing optimisation for a second time." % model) # Create a data pipe and switch to it. self.interpreter.pipe.create(pipe_name=model_pipe, pipe_type='relax_disp', bundle=self.pipe_bundle) self.interpreter.pipe.switch(model_pipe) # Load the results. self.interpreter.results.read(file='results', dir=path) # Jump to the next model. continue # Create the data pipe by copying the base pipe, then switching to it. self.interpreter.pipe.copy(pipe_from=self.pipe_name, pipe_to=model_pipe, bundle_to=self.pipe_bundle) self.interpreter.pipe.switch(model_pipe) # Select the model. self.interpreter.relax_disp.select_model(model) # Copy the R2eff values from the R2eff model data pipe. if model != MODEL_R2EFF and MODEL_R2EFF in self.models: self.interpreter.value.copy(pipe_from=self.name_pipe(MODEL_R2EFF), pipe_to=model_pipe, param='r2eff') # Calculate the R2eff values for the fixed relaxation time period data types. if model == MODEL_R2EFF and not has_exponential_exp_type(): self.interpreter.minimise.calculate() # Optimise the model. else: self.optimise(model=model, model_path=model_path) # Write out the results. self.write_results(path=path, model=model) # The final model selection data pipe. if len(self.models) >= 2: # Printout. section(file=sys.stdout, text="Final results", prespace=2) # Perform model selection. self.interpreter.model_selection(method=self.modsel, modsel_pipe=self.name_pipe('final'), bundle=self.pipe_bundle, pipes=self.model_pipes) # Final Monte Carlo simulations only. if not self.mc_sim_all_models: self.interpreter.monte_carlo.setup(number=self.mc_sim_num) self.interpreter.monte_carlo.create_data() self.interpreter.monte_carlo.initial_values() self.interpreter.minimise.execute('simplex', func_tol=self.opt_func_tol, max_iter=self.opt_max_iterations, constraints=True) if self.eliminate: self.interpreter.eliminate() self.interpreter.monte_carlo.error_analysis() # Writing out the final results. self.write_results(path=self.results_dir+sep+'final') # No model selection. else: warn(RelaxWarning("Model selection in the dispersion auto-analysis has been skipped as only %s models have been optimised." % len(self.model_pipes))) # Finally save the program state. self.interpreter.state.save(state='final_state', dir=self.results_dir, force=True)