def dipolar_spectrum_vs_temp(self, fdd, weights_xi, spinA, spinB,
                              calc_settings):
     Nf = self.Nf
     Ntemp = self.temp_bins.size
     spc_vs_temp = np.zeros((Ntemp, Nf))
     g = self.set_gaxis(spinB['g'])
     Ng = g.size
     depth_vs_temp = np.zeros((Ntemp, Ng))
     for i in range(Ntemp):
         sys.stdout.write('\r')
         status = int(float(i + 1) / float(Ntemp) * 100)
         sys.stdout.write(
             "Calculating the dipolar spectrum vs temperature... %d%% " %
             (status))
         sys.stdout.flush()
         temp = self.temp_bins[i]
         weights_temp = flip_probabilities(self.gB, spinB['g'],
                                           calc_settings['magnetic_field'],
                                           temp)
         weights = self.total_weights(weights_temp, weights_xi)
         spc = self.dipolar_spectrum(fdd, weights)
         spc_vs_temp[i] = spc
         weights_temp2 = flip_probabilities(g, spinB['g'],
                                            calc_settings['magnetic_field'],
                                            temp)
         depth_vs_temp[i] = weights_temp2
     return [spc_vs_temp, g, depth_vs_temp]
def score_function(parameters, fit_settings, simulator, exp_data, spinA, spinB, calc_settings):
    # Set parameters
    indices = fit_settings['parameters']['indices']
    fixed = fit_settings['parameters']['fixed']
    all_parameters = set_parameters(parameters, indices, fixed)
    # Calculate the temperature-based weights
    weights_temp = []
    if (not (indices['temp'] == -1) and calc_settings['g_selectivity'] and (spinB['type'] == "anisotropic")):
        weights_temp = flip_probabilities(simulator.gB, spinB['g'], calc_settings['magnetic_field'], all_parameters['temp'])
    # Calculate dipolar frequencies
    fdd, theta, weights_xi = simulator.dipolar_frequencies(all_parameters, spinA, spinB) 
    # Calulate the total weights
    weights = simulator.total_weights(weights_temp, weights_xi)
    if fit_settings['settings']['fitted_data'] == 'spectrum':
        # Calculate the dipolar spectrum
        fit = simulator.dipolar_spectrum(fdd, weights)
        # Calculate the score
        score = chi2(fit, exp_data['spc'], exp_data['f'], calc_settings['f_min'], calc_settings['f_max'], calc_settings['noise_std'])         
    elif fit_settings['settings']['fitted_data'] == 'timetrace':
        # Calculate the dipolar timetrace
        #fit = simulator.dipolar_timetrace(fdd, weights)
        fit = simulator.dipolar_timetrace_fast(fdd, weights)
        # Calculate the score
        score = chi2(fit, exp_data['sig'], exp_data['t'], calc_settings['t_min'], calc_settings['t_max'], calc_settings['noise_std'])
    return score
Exemple #3
0
def score_function(parameters, fit_settings, simulator, exp_data, spinA, spinB,
                   calc_settings):
    # Set parameters
    indices = fit_settings['parameters']['indices']
    fixed = fit_settings['parameters']['fixed']
    all_parameters = set_parameters(parameters, indices, fixed)
    # Calculate dipolar frequencies
    fdd, theta = simulator.dipolar_frequencies(all_parameters, spinA, spinB)
    # Calculate weights for different g-values of spin B
    pB = []
    if (not (indices['temp'] == -1) and calc_settings['g_selectivity']
            and (spinB['type'] == "anisotropic")):
        pB = flip_probabilities(simulator.gB, spinB['g'],
                                calc_settings['magnetic_field'],
                                all_parameters['temp'])
    # Simulate the spectrum or the time trace and score them
    if fit_settings['settings']['fitted_data'] == 'spectrum':
        # Calculate the dipolar spectrum
        fit = simulator.dipolar_spectrum(fdd, pB)
        # Calculate the score
        score = rmsd(fit, exp_data['spc'], exp_data['f'],
                     calc_settings['f_min'], calc_settings['f_max'])
    elif fit_settings['settings']['fitted_data'] == 'timetrace':
        # Calculate the dipolar timetrace
        #fit = simulator.dipolar_timetrace(fdd, pB)
        fit = simulator.dipolar_timetrace_fast(fdd, pB)
        # Calculate the score
        score = rmsd(fit, exp_data['sig'], exp_data['t'],
                     calc_settings['t_min'], calc_settings['t_max'])
    return score
 def init_fitting(self, fit_settings, exp_data, spinA, spinB,
                  calc_settings):
     # Set the frequency axis
     if fit_settings['settings']['fitted_data'] == 'spectrum':
         self.f = self.set_faxis(exp_data['f'])
         self.Nf = self.f.size
     # Set the time axis
     elif fit_settings['settings']['fitted_data'] == 'timetrace':
         self.t = self.set_taxis(exp_data['t'])
         self.Nt = self.t.size
         # Set the modulation depth
         self.mod_depth = self.set_modulation_depth(exp_data['sig'])
     # Generate the ensemble of spin pairs
     self.field, self.gA, self.gB, self.qA, self.qB = self.spin_ensemble(
         spinA, spinB)
     # Calculate the temperature-based weights
     if (fit_settings['parameters']['indices']['temp'] == -1
             and calc_settings['g_selectivity']
             and spinB['type'] == "anisotropic"):
         self.weights_temp = flip_probabilities(
             self.gB, spinB['g'], calc_settings['magnetic_field'],
             fit_settings['parameters']['fixed']['temp'])
    def run_simulation(self, sim_settings, exp_data, spinA, spinB,
                       calc_settings):
        sys.stdout.write('Starting the simulation...\n')
        sys.stdout.write('Running pre-calculations... ')
        # Set the frequency axis
        if (sim_settings['modes']['spc']
                or sim_settings['modes']['spc_vs_theta']
                or sim_settings['modes']['spc_vs_xi']
                or sim_settings['modes']['spc_vs_phi']
                or sim_settings['modes']['spc_vs_temp']):
            self.f = self.set_faxis(exp_data['f'], spinA['g'], spinB['g'],
                                    sim_settings['parameters'])
            self.Nf = self.f.size
            # Normalize the frequency axis
            if sim_settings['settings']['faxis_normalized']:
                self.fn = self.normalize_faxis(sim_settings['parameters'])
        # Set the time axis
        if sim_settings['modes']['timetrace']:
            self.t = self.set_taxis(exp_data['t'], spinA['g'], spinB['g'],
                                    sim_settings['parameters'])
            self.Nt = self.t.size
        # Set the modulation depth parameter
        if sim_settings['modes']['timetrace']:
            self.mod_depth = self.set_modulation_depth(
                exp_data['sig'], sim_settings['settings']['mod_depth'])
        # Generate an ensemble of spin pairs
        self.field, self.gA, self.gB, self.qA, self.qB = self.spin_ensemble(
            spinA, spinB)
        # Calculate the temperature-based weights
        if (calc_settings['g_selectivity'] and spinB['type'] == "anisotropic"):
            self.weights_temp = flip_probabilities(
                self.gB, spinB['g'], calc_settings['magnetic_field'],
                sim_settings['parameters']['temp'])
        # Calculate the dipolar frequencies
        weights_xi = []
        if (sim_settings['modes']['spc'] or sim_settings['modes']['timetrace']
                or sim_settings['modes']['spc_vs_theta']
                or sim_settings['modes']['spc_vs_temp']):
            fdd, theta, weights_xi = self.dipolar_frequencies(
                sim_settings['parameters'], spinA, spinB, self.gA, self.gB,
                self.qA, self.qB, sim_settings['modes']['spc_vs_theta'])
        # Calulate the total weights
        weights = self.total_weights(self.weights_temp, weights_xi)
        sys.stdout.write('[DONE]\n')

        # Calculate the dipolar spectrum
        if sim_settings['modes']['spc']:
            sys.stdout.write('Calculating the dipolar spectrum... ')
            self.spc = self.dipolar_spectrum(fdd, weights)
            sys.stdout.write('[DONE]\n')
            if not (exp_data['f'] == []):
                score = rmsd(self.spc, exp_data['spc'], exp_data['f'],
                             calc_settings['f_min'], calc_settings['f_max'])
                sys.stdout.write("RMSD = %f \n" % score)

        # Calculate the PDS time trace
        if sim_settings['modes']['timetrace']:
            #time_start = time.time()
            sys.stdout.write('Calculating the dipolar time trace... ')
            #self.sig = self.dipolar_timetrace(fdd, weights)
            self.sig = self.dipolar_timetrace_fast(fdd, weights)
            sys.stdout.write('[DONE]\n')
            #time_finish = time.time()
            #time_elapsed = str(datetime.timedelta(seconds = time_finish - time_start))
            #sys.stdout.write('Calculation time: %s\n' % (time_elapsed))
            if not (exp_data['t'] == []):
                score = rmsd(self.sig, exp_data['sig'], exp_data['t'],
                             calc_settings['t_min'], calc_settings['t_max'])
                sys.stdout.write("RMSD = %f \n" % score)

        # Calculate the dipolar spectrum vs theta
        if (sim_settings['modes']['spc_vs_theta']):
            sys.stdout.write('Calculating the dipolar spectrum vs theta... ')
            self.theta_bins = sim_settings['settings']['theta_bins']
            self.spc_vs_theta = self.dipolar_spectrum_vs_theta(
                fdd, theta, weights)
            if not sim_settings['modes']['spc']:
                self.spc = self.dipolar_spectrum(fdd, weights)
            sys.stdout.write('[DONE]\n')

        # Calculate the dipolar spectrum vs xi
        if sim_settings['modes']['spc_vs_xi']:
            sys.stdout.write('Calculating the dipolar spectrum vs xi... ')
            self.xi_bins = sim_settings['settings']['xi_bins']
            self.spc_vs_xi = self.dipolar_spectrum_vs_xi(
                sim_settings['parameters'], spinA, spinB)
            sys.stdout.write('[DONE]\n')

        # Calculate the dipolar spectrum vs phi
        if sim_settings['modes']['spc_vs_phi']:
            sys.stdout.write('Calculating the dipolar spectrum vs phi... ')
            self.phi_bins = sim_settings['settings']['phi_bins']
            self.spc_vs_phi = self.dipolar_spectrum_vs_phi(
                sim_settings['parameters'], spinA, spinB)
            sys.stdout.write('[DONE]\n')

        # Calculate the dipolar spectrum vs temperature
        if (sim_settings['modes']['spc_vs_temp']
                and calc_settings['g_selectivity']):
            sys.stdout.write(
                'Calculating the dipolar spectrum vs temperature... ')
            self.temp_bins = sim_settings['settings']['temp_bins']
            self.spc_vs_temp, self.g, self.depth_vs_temp = self.dipolar_spectrum_vs_temp(
                fdd, weights_xi, spinA, spinB, calc_settings)
            sys.stdout.write('[DONE]\n')
        sys.stdout.write('The simulation is finished\n\n')