def move_peak_onto_spectrum(self, peak): freq = peak.frequency pos = sputil.alias_onto_spectrum(freq, peak.spectrum) if pos != freq: peak.position = pos peak.alias = pyutil.subtract_tuples(freq, pos)
def place_peaks(self, spectrum): hc_axes = pyutil.order(('1H', '13C'), spectrum.nuclei) if spectrum.dimension != 2 or hc_axes == None: self.progress_report('Spectrum must 2D with H and C axes.') return molecule = spectrum.molecule condition = spectrum.condition self.stoppable_loop('resonances', 50) for rH in condition.resonance_list(): self.check_for_stop() if rH.atom.nucleus == '1H': catom = atoms.attached_heavy_atom(rH.atom, molecule) if catom and catom.nucleus == '13C': rC = condition.find_resonance(catom) if rC: assignment = pyutil.permute((rH, rC), hc_axes) if not spectrum.find_peak(assignment): freq = pyutil.permute((rH.frequency, rC.frequency), hc_axes) pos = sputil.alias_onto_spectrum(freq, spectrum) peak = spectrum.place_peak(pos) peak.assign(hc_axes[0], rH.group.name, rH.atom.name) peak.assign(hc_axes[1], rC.group.name, rC.atom.name) if freq != pos: peak.alias = pyutil.subtract_tuples(freq, pos) peak.show_assignment_label()
def estimate_parameter_error(self, y_sd, trials): trial_params = [] n = self.fit_function.parameter_count() param_sd = n * [0] if self.converged: while len(trial_params) < trials: new_xy_pairs = [] for x, y in self.xy_pairs: new_y = y + random.gauss(0, y_sd) new_xy_pairs.append((x, new_y)) fit = least_squares_fit(new_xy_pairs, self.fit_function, self.params, self.tolerance) if fit.converged: trial_params.append(fit.params) sum_d2 = n * [0] for params in trial_params: diff = pyutil.subtract_tuples(params, self.params) diff2 = map(lambda d: d*d, diff) sum_d2 = pyutil.add_tuples(sum_d2, diff2) param_sd = map(lambda d, m=trials: math.sqrt(d/m), sum_d2) self.trial_params = trial_params self.param_sd = param_sd
def write_spectrum_table_entry(self, assignable_spectrum, file): type = assignable_spectrum.autoassign_type ref_type = self.root_spectrum if type == ref_type: ref_type = 'ROOT' file_name = type + '.pks' intra = type in self.intra_peak_spectra inter = type in self.inter_peak_spectra phase = assignable_spectrum.autoassign_phase file.write('%s %s %s %d %d 0 phase: {%s} {\n' % (type, ref_type, file_name, intra, inter, phase)) tolerances = assignable_spectrum.tolerances spectrum = assignable_spectrum.spectrum min_ppm, max_ppm = spectrum.region min_ppm = pyutil.subtract_tuples(max_ppm, spectrum.sweep_width) axis_names = self.autoassign_axis_names(spectrum, type) for a in range(spectrum.dimension): file.write('{%10s %8.3f %8.3f 0 %8.3f unfolded }\n' % (axis_names[a], min_ppm[a], max_ppm[a], tolerances[a])) file.write('}\n')
def estimate_parameter_error(self, y_sd, trials): trial_params = [] n = self.fit_function.parameter_count() param_sd = n * [0] if self.converged: while len(trial_params) < trials: new_xy_pairs = [] for x, y in self.xy_pairs: new_y = y + random.gauss(0, y_sd) new_xy_pairs.append((x, new_y)) fit = least_squares_fit(new_xy_pairs, self.fit_function, self.params, self.tolerance) if fit.converged: trial_params.append(fit.params) sum_d2 = n * [0] for params in trial_params: diff = pyutil.subtract_tuples(params, self.params) diff2 = map(lambda d: d * d, diff) sum_d2 = pyutil.add_tuples(sum_d2, diff2) param_sd = map(lambda d, m=trials: math.sqrt(d / m), sum_d2) self.trial_params = trial_params self.param_sd = param_sd
def fold_spectrum(spectrum, axis, multiples): if axis >= spectrum.dimension: return sweepwidth = spectrum.sweep_width[axis] delta = spectrum.dimension * [0] delta[axis] = multiples * sweepwidth spectrum.scale_offset = pyutil.add_tuples(spectrum.scale_offset, delta) # # Add alias to every peak # for peak in spectrum.peak_list(): peak.alias = pyutil.subtract_tuples(peak.alias, delta) peaklets = peak.peaklets() if peaklets: for p in peaklets: p.alias = pyutil.subtract_tuples(p.alias, delta)
return dict(dim_list) # place assigned peaks from sputil import split_group_atom, alias_onto_spectrum from pyutil import subtract_tuples dim_dict = detect_dimension(peak_list, spec) if dim_dict == None: s.show_message('Error', 'Spectrum does not look the correct experiment to use.') raise SystemExit for peak in peak_list: gas, freqs = peak[0], peak[1] gas2, freqs2 = list(gas), list(freqs) for a in range(spec.dimension): b = dim_dict[a] gas2[b], freqs2[b] = gas[a], freqs[a] peak = spec.place_peak(freqs2) # alias if edge of the spectrum freqs3 = alias_onto_spectrum(freqs2, spec) if freqs2 != freqs3: peak.position = freqs3 peak.alias = subtract_tuples(freqs2, freqs3) # attach assignment labels for i in range(spec.dimension): g, a = split_group_atom(gas2[i]) peak.assign(i, g, a) peak.show_assignment_label()