def test_loop_exp_frq_cpmg(self): """Unit test of the loop_exp_frq() 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) # Original data (exp_type, frq). data = [ ['SQ CPMG', 499862140.0], ['SQ CPMG', 599890858.69999993] ] # Original indices (ei, mi). indices = [ [0, 0], [0, 1] ] # Check the number of iterations. print("Checking the number of iterations of the loop.") count = 0 for exp_type, frq, ei, mi in loop_exp_frq(return_indices=True): print(exp_type, frq, ei, mi) count += 1 self.assertEqual(count, 2) # Check the values. print("Checking the values returned by the loop.") index = 0 for exp_type, frq, ei, mi in loop_exp_frq(return_indices=True): # Check the experiment info. self.assertEqual(exp_type, data[index][0]) self.assertEqual(ei, indices[index][0]) # Check the frequency info. self.assertEqual(frq, data[index][1]) self.assertEqual(mi, indices[index][1]) # Increment the data index. index += 1
def test_loop_exp_frq_cpmg(self): """Unit test of the loop_exp_frq() 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) # Original data (exp_type, frq). data = [['SQ CPMG', 499862140.0], ['SQ CPMG', 599890858.69999993]] # Original indices (ei, mi). indices = [[0, 0], [0, 1]] # Check the number of iterations. print("Checking the number of iterations of the loop.") count = 0 for exp_type, frq, ei, mi in loop_exp_frq(return_indices=True): print(exp_type, frq, ei, mi) count += 1 self.assertEqual(count, 2) # Check the values. print("Checking the values returned by the loop.") index = 0 for exp_type, frq, ei, mi in loop_exp_frq(return_indices=True): # Check the experiment info. self.assertEqual(exp_type, data[index][0]) self.assertEqual(ei, indices[index][0]) # Check the frequency info. self.assertEqual(frq, data[index][1]) self.assertEqual(mi, indices[index][1]) # Increment the data index. index += 1
pipe_name = '%s - relax_disp' % (model) pipes.append(pipe_name) pipe.switch(pipe_name=pipe_name) print("\nModel: %s" % (model)) # Loop over the spins. for cur_spin, mol_name, resi, resn, spin_id in spin_loop(full_info=True, return_id=True, skip_desel=True): # Generate spin string. spin_string = generate_spin_string(spin=cur_spin, mol_name=mol_name, res_num=resi, res_name=resn) # Loop over the parameters. print("\nOptimised parameters for spin: %s" % (spin_string)) for param in cur_spin.params + ['chi2']: # Get the value. if param in ['r1', 'r2']: for exp_type, frq, ei, mi in loop_exp_frq(return_indices=True): # Generate the R20 key. r20_key = generate_r20_key(exp_type=exp_type, frq=frq) # Get the value. value = getattr(cur_spin, param)[r20_key] # Print value. print("%-10s %-6s %-6s %3.8f" % ("Parameter:", param, "Value:", value)) # For all other parameters. else: # Get the value. value = getattr(cur_spin, param) # Print value.
def sherekhan_input(spin_id=None, force=False, dir='ShereKhan'): """Create the ShereKhan input files. @keyword spin_id: The spin ID string to restrict the file creation to. @type spin_id: str @keyword force: A flag which if True will cause all pre-existing files to be overwritten. @type force: bool @keyword dir: The optional directory to place the files into. If None, then the files will be placed into the current directory. @type dir: str or None """ # Test if the current pipe exists. check_pipe() # Test if sequence data is loaded. if not exists_mol_res_spin_data(): raise RelaxNoSequenceError # Test if the experiment type has been set. if not hasattr(cdp, 'exp_type'): raise RelaxError("The relaxation dispersion experiment type has not been specified.") # Test if the model has been set. if not hasattr(cdp, 'model_type'): raise RelaxError("The relaxation dispersion model has not been specified.") # Directory creation. if dir != None: mkdir_nofail(dir, verbosity=0) # Loop over the spin blocks. cluster_index = 0 for spin_ids in loop_cluster(): # The spin containers. spins = spin_ids_to_containers(spin_ids) # Loop over the magnetic fields. for exp_type, frq, ei, mi in loop_exp_frq(return_indices=True): # Loop over the time, and count it. time_i = 0 for time, ti in loop_time(exp_type=exp_type, frq=frq, return_indices=True): time_i += 1 # Check that not more than one time point is returned. if time_i > 1: raise RelaxError("Number of returned time poins is %i. Only 1 time point is expected."%time_i) # The ShereKhan input file for the spin cluster. file_name = 'sherekhan_frq%s.in' % (mi+1) if dir != None: dir_name = dir + sep + 'cluster%s' % (cluster_index+1) else: dir_name = 'cluster%s' % (cluster_index+1) file = open_write_file(file_name=file_name, dir=dir_name, force=force) # The B0 field for the nuclei of interest in MHz (must be positive to be accepted by the server). file.write("%.10f\n" % abs(frq / periodic_table.gyromagnetic_ratio('1H') * periodic_table.gyromagnetic_ratio('15N') / 1e6)) # The constant relaxation time for the CPMG experiment in seconds. file.write("%s\n" % (time)) # The comment line. file.write("# %-18s %-20s %-20s\n" % ("nu_cpmg (Hz)", "R2eff (rad/s)", "Error")) # Loop over the spins of the cluster. for i in range(len(spins)): # Get the residue container. res = return_residue(spin_ids[i]) # Name the residue if needed. res_name = res.name if res_name == None: res_name = 'X' # Initialise the lines to output (to be able to catch missing data). lines = [] # The residue ID line. lines.append("# %s%s\n" % (res_name, res.num)) # Loop over the dispersion points. for offset, point in loop_offset_point(exp_type=exp_type, frq=frq, skip_ref=True): # The parameter key. param_key = return_param_key_from_data(exp_type=exp_type, frq=frq, offset=offset, point=point) # No data. if param_key not in spins[i].r2eff: continue # Store the data. lines.append("%20.15g %20.13g %20.13g\n" % (point, spins[i].r2eff[param_key], spins[i].r2eff_err[param_key])) # No data. if len(lines) == 1: continue # Write out the data. for line in lines: file.write(line) # Close the file. file.close() # Increment the cluster index. cluster_index += 1
def loop_parameters(spins=None): """Generator function for looping of the 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 # Yield the two parameters. params = ['r2eff', 'i0'] for i in range(2): # First increment the indices. param_index += 1 # Yield the data. yield params[i], 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 R1 parameter. if 'r1' in spins[spin_index].params: for exp_type, frq in loop_exp_frq(): param_index += 1 yield 'r1', 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 R2 parameter. if 'r2' in spins[spin_index].params: for exp_type, frq in loop_exp_frq(): param_index += 1 yield 'r2', param_index, spin_index, generate_r20_key(exp_type=exp_type, frq=frq) # The R2A parameter. if 'r2a' in spins[spin_index].params: for exp_type, frq in loop_exp_frq(): param_index += 1 yield 'r2a', param_index, spin_index, generate_r20_key(exp_type=exp_type, frq=frq) # The R2B parameter. if 'r2b' in spins[spin_index].params: for exp_type, frq in loop_exp_frq(): param_index += 1 yield 'r2b', param_index, spin_index, generate_r20_key(exp_type=exp_type, frq=frq) # Then the chemical shift difference parameters 'phi_ex', 'phi_ex_B', 'phi_ex_C', 'padw2', 'dw', 'dw_AB', 'dw_BC', 'dw_AB' (one per spin). for spin_index in range(len(spins)): # Skip deselected spins. if not spins[spin_index].select: continue # Yield the data. if 'phi_ex' in spins[spin_index].params: param_index += 1 yield 'phi_ex', param_index, spin_index, None if 'phi_ex_B' in spins[spin_index].params: param_index += 1 yield 'phi_ex_B', param_index, spin_index, None if 'phi_ex_C' in spins[spin_index].params: param_index += 1 yield 'phi_ex_C', param_index, spin_index, None if 'padw2' in spins[spin_index].params: param_index += 1 yield 'padw2', param_index, spin_index, None if 'dw' in spins[spin_index].params: param_index += 1 yield 'dw', param_index, spin_index, None if 'dw_AB' in spins[spin_index].params: param_index += 1 yield 'dw_AB', param_index, spin_index, None if 'dw_BC' in spins[spin_index].params: param_index += 1 yield 'dw_BC', param_index, spin_index, None if 'dw_AC' in spins[spin_index].params: param_index += 1 yield 'dw_AC', 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 if 'dwH' in spins[spin_index].params: param_index += 1 yield 'dwH', param_index, spin_index, None if 'dwH_AB' in spins[spin_index].params: param_index += 1 yield 'dwH_AB', param_index, spin_index, None if 'dwH_BC' in spins[spin_index].params: param_index += 1 yield 'dwH_BC', param_index, spin_index, None if 'dwH_AC' in spins[spin_index].params: param_index += 1 yield 'dwH_AC', param_index, spin_index, None # All other parameters (one per spin cluster). for param in spins[0].params: if not param in ['r1', 'r2', 'r2a', 'r2b', 'phi_ex', 'phi_ex_B', 'phi_ex_C', 'padw2', 'dw', 'dw_AB', 'dw_BC', 'dw_AB', 'dwH', 'dwH_AB', 'dwH_BC', 'dwH_AB']: param_index += 1 yield param, param_index, None, None
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
# Loop over the spins. for cur_spin, mol_name, resi, resn, spin_id in spin_loop(full_info=True, return_id=True, skip_desel=True): # Generate spin string. spin_string = generate_spin_string(spin=cur_spin, mol_name=mol_name, res_num=resi, res_name=resn) # Loop over the parameters. print("\nOptimised parameters for spin: %s" % (spin_string)) for param in cur_spin.params + ['chi2']: # Get the value. if param in ['r1', 'r2']: for exp_type, frq, ei, mi in loop_exp_frq(return_indices=True): # Generate the R20 key. r20_key = generate_r20_key(exp_type=exp_type, frq=frq) # Get the value. value = getattr(cur_spin, param)[r20_key] # Print value. print("%-10s %-6s %-6s %3.8f" % ("Parameter:", param, "Value:", value)) # For all other parameters. else: # Get the value. value = getattr(cur_spin, param)
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