def test_loop_exp_frq_offset_point_time_cpmg_setup(self): """U{Bug #21665<https://gna.org/bugs/?21665>} catch, the failure due to a a CPMG analysis recorded at two fields at two delay times, using minimise.calculate().""" # 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 ncyc_1 = [20, 16, 10, 36, 2, 12, 4, 22, 18, 40, 14, 26, 8, 32, 24, 6, 28] sfrq_1 = 499.86214*1E6 time_T2_1 = 0.04 cpmg_1 = sorted([ncyc/time_T2_1 for ncyc in ncyc_1]) ncyc_2 = [28, 4, 32, 60, 2, 10, 16, 8, 20, 52, 18, 40, 6, 12, 24, 14, 22] sfrq_2 = 599.8908587*1E6 time_T2_2 = 0.06 cpmg_2 = sorted([ncyc/time_T2_2 for ncyc in ncyc_2]) # Test the loop function. # First initialize index for the two lists. i = -1 j = -1 for exp_type, frq, offset, point, time, ei, mi, oi, di, ti in loop_exp_frq_offset_point_time(return_indices=True): if frq == sfrq_1: i += 1 self.assertEqual(time, time_T2_1) self.assertAlmostEqual(point, cpmg_1[i], 3) if frq == sfrq_2: j += 1 self.assertEqual(time, time_T2_2) self.assertAlmostEqual(point, cpmg_2[j], 3)
def test_loop_exp_frq_offset_point_time_cpmg_setup(self): """U{Bug #21665<https://web.archive.org/web/https://gna.org/bugs/?21665>} catch, the failure due to a a CPMG analysis recorded at two fields at two delay times, using minimise.calculate().""" # 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 ncyc_1 = [ 20, 16, 10, 36, 2, 12, 4, 22, 18, 40, 14, 26, 8, 32, 24, 6, 28 ] sfrq_1 = 499.86214 * 1E6 time_T2_1 = 0.04 cpmg_1 = sorted([ncyc / time_T2_1 for ncyc in ncyc_1]) ncyc_2 = [ 28, 4, 32, 60, 2, 10, 16, 8, 20, 52, 18, 40, 6, 12, 24, 14, 22 ] sfrq_2 = 599.8908587 * 1E6 time_T2_2 = 0.06 cpmg_2 = sorted([ncyc / time_T2_2 for ncyc in ncyc_2]) # Test the loop function. # First initialize index for the two lists. i = -1 j = -1 for exp_type, frq, offset, point, time, ei, mi, oi, di, ti in loop_exp_frq_offset_point_time( return_indices=True): if frq == sfrq_1: i += 1 self.assertEqual(time, time_T2_1) self.assertAlmostEqual(point, cpmg_1[i], 3) if frq == sfrq_2: j += 1 self.assertEqual(time, time_T2_2) self.assertAlmostEqual(point, cpmg_2[j], 3)
def test_loop_exp_frq_offset_point_time_cpmg(self): """Unit test of the loop_exp_frq_offset_point_time() 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, offset, point). data = [ ['SQ CPMG', 499862140.0, 0, [50.0, 100.0, 150.0, 200.0, 250.0, 300.0, 350.0, 400.0, 450.0, 500.0, 550.0, 600.0, 650.0, 700.0, 800.0, 900.0, 1000.0], 0.04], ['SQ CPMG', 599890858.69999993, 0, [33.3333, 66.666, 100.0, 133.333, 166.666, 200.0, 233.333, 266.666, 300.0, 333.333, 366.666, 400.0, 466.666, 533.333, 666.666, 866.666, 1000.0], 0.06] ] # Original indices (ei, mi, oi). indices = [ [0, 0, 0, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16], 0], [0, 1, 0, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16], 0] ] # Check the number of iterations. print("Checking the number of iterations of the loop.") count = 0 for exp_type, frq, offset, point, time, ei, mi, oi, di, ti in loop_exp_frq_offset_point_time(return_indices=True): print(exp_type, frq, offset, point, time, ei, mi, oi, di, ti) count += 1 self.assertEqual(count, 34) # Check the values. print("Checking the values returned by the loop.") frq_index = 0 disp_index = 0 for exp_type, frq, offset, point, time, ei, mi, oi, di, ti in loop_exp_frq_offset_point_time(return_indices=True): # Check the experiment info. self.assertEqual(exp_type, data[frq_index][0]) self.assertEqual(ei, indices[frq_index][0]) # Check the frequency info. self.assertEqual(frq, data[frq_index][1]) self.assertEqual(mi, indices[frq_index][1]) # Check the offset info. self.assertEqual(offset, data[frq_index][2]) self.assertEqual(oi, indices[frq_index][2]) # Check the dispersion point info. self.assertAlmostEqual(point, data[frq_index][3][disp_index], 2) self.assertEqual(di, indices[frq_index][3][disp_index]) # Check the time point info. self.assertEqual(time, data[frq_index][4]) self.assertEqual(ti, indices[frq_index][4]) # Increment the data index. if disp_index == 16: frq_index += 1 disp_index = 0 else: disp_index += 1
def test_loop_exp_frq_offset_point_time_cpmg(self): """Unit test of the loop_exp_frq_offset_point_time() 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, offset, point). data = [[ 'SQ CPMG', 499862140.0, 0, [ 50.0, 100.0, 150.0, 200.0, 250.0, 300.0, 350.0, 400.0, 450.0, 500.0, 550.0, 600.0, 650.0, 700.0, 800.0, 900.0, 1000.0 ], 0.04 ], [ 'SQ CPMG', 599890858.69999993, 0, [ 33.3333, 66.666, 100.0, 133.333, 166.666, 200.0, 233.333, 266.666, 300.0, 333.333, 366.666, 400.0, 466.666, 533.333, 666.666, 866.666, 1000.0 ], 0.06 ]] # Original indices (ei, mi, oi). indices = [ [ 0, 0, 0, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16], 0 ], [ 0, 1, 0, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16], 0 ] ] # Check the number of iterations. print("Checking the number of iterations of the loop.") count = 0 for exp_type, frq, offset, point, time, ei, mi, oi, di, ti in loop_exp_frq_offset_point_time( return_indices=True): print(exp_type, frq, offset, point, time, ei, mi, oi, di, ti) count += 1 self.assertEqual(count, 34) # Check the values. print("Checking the values returned by the loop.") frq_index = 0 disp_index = 0 for exp_type, frq, offset, point, time, ei, mi, oi, di, ti in loop_exp_frq_offset_point_time( return_indices=True): # Check the experiment info. self.assertEqual(exp_type, data[frq_index][0]) self.assertEqual(ei, indices[frq_index][0]) # Check the frequency info. self.assertEqual(frq, data[frq_index][1]) self.assertEqual(mi, indices[frq_index][1]) # Check the offset info. self.assertEqual(offset, data[frq_index][2]) self.assertEqual(oi, indices[frq_index][2]) # Check the dispersion point info. self.assertAlmostEqual(point, data[frq_index][3][disp_index], 2) self.assertEqual(di, indices[frq_index][3][disp_index]) # Check the time point info. self.assertEqual(time, data[frq_index][4]) self.assertEqual(ti, indices[frq_index][4]) # Increment the data index. if disp_index == 16: frq_index += 1 disp_index = 0 else: disp_index += 1
def calculate_r2eff(): """Calculate the R2eff values for fixed relaxation time period data.""" # Data checks. check_exp_type() check_disp_points() check_exp_type_fixed_time() # Printouts. print("Calculating the R2eff/R1rho values for fixed relaxation time period data.") # Loop over the spins. for spin, mol_name, resi, resn, spin_id in spin_loop(full_info=True, return_id=True, skip_desel=True): # Spin ID printout. print("Spin '%s'." % spin_id) # Skip spins which have no data. if not hasattr(spin, 'peak_intensity'): continue # Initialise the data structures. if not hasattr(spin, 'r2eff'): spin.r2eff = {} if not hasattr(spin, 'r2eff_err'): spin.r2eff_err = {} # Loop over all the data. for exp_type, frq, offset, point, time in loop_exp_frq_offset_point_time(): # The three keys. ref_keys = find_intensity_keys(exp_type=exp_type, frq=frq, offset=offset, point=None, time=time) int_keys = find_intensity_keys(exp_type=exp_type, frq=frq, offset=offset, point=point, time=time) param_key = return_param_key_from_data(exp_type=exp_type, frq=frq, offset=offset, point=point) # Check for missing data. missing = False for i in range(len(ref_keys)): if ref_keys[i] not in spin.peak_intensity: missing = True for i in range(len(int_keys)): if int_keys[i] not in spin.peak_intensity: missing = True if missing: continue # Average the reference intensity data and errors. ref_intensity = average_intensity(spin=spin, exp_type=exp_type, frq=frq, offset=offset, point=None, time=time) ref_intensity_err = average_intensity(spin=spin, exp_type=exp_type, frq=frq, offset=offset, point=None, time=time, error=True) # Average the intensity data and errors. intensity = average_intensity(spin=spin, exp_type=exp_type, frq=frq, offset=offset, point=point, time=time) intensity_err = average_intensity(spin=spin, exp_type=exp_type, frq=frq, offset=offset, point=point, time=time, error=True) # Check for math domain errors or log for values less than 0.0. if ref_intensity == 0.0: skip_data = True elif float(intensity) / ref_intensity <= 0.0: skip_data = True else: skip_data = False if skip_data: spin_string = generate_spin_string(spin=spin, mol_name=mol_name, res_num=resi, res_name=resn) msg = "Math log(I / I_ref) domain error for spin '%s' in R2eff value calculation for fixed relaxation time period data. I=%3.3f, I_ref=%3.3f. The point is skipped." % (spin_string, intensity, ref_intensity) warn(RelaxWarning("%s" % msg)) point_info = "This happened for '%s' at %3.1f MHz, for offset=%3.1f ppm and dispersion point %3.1f Hz and time %1.2f s.\n" % (exp_type, frq/1E6, offset, point, time) print(point_info) else: # Calculate the R2eff value. spin.r2eff[param_key] = calc_two_point_r2eff(relax_time=time, I_ref=ref_intensity, I=intensity) # Calculate the R2eff error. spin.r2eff_err[param_key] = calc_two_point_r2eff_err(relax_time=time, I_ref=ref_intensity, I=intensity, I_ref_err=ref_intensity_err, I_err=intensity_err)