Exemplo n.º 1
0
    def intersection(self, other):
        """TODO: method docstring."""
        name = temperature = h_larmor_frq = p_total = l_total = None

        if self.name == other.name:
            name = self.name

        nuclei = (peaks.Peak(self.nuclei).intersection(peaks.Peak(
            other.nuclei)).assignment)

        if not nuclei:
            nuclei = None

        if self.temperature == other.temperature:
            temperature = self.temperature

        if self.h_larmor_frq == other.h_larmor_frq:
            h_larmor_frq = self.h_larmor_frq

        if self.p_total == other.p_total:
            p_total = self.p_total

        if self.l_total == other.l_total:
            l_total = self.l_total

        return ParamName(
            name=name,
            nuclei=nuclei,
            temperature=temperature,
            h_larmor_frq=h_larmor_frq,
            p_total=p_total,
            l_total=l_total,
        )
Exemplo n.º 2
0
    def __init__(
        self,
        name=None,
        nuclei=None,
        temperature=None,
        h_larmor_frq=None,
        p_total=None,
        l_total=None,
    ):
        self.name = self.nuclei = self.temperature = self.h_larmor_frq = None
        self.p_total = self.l_total = None

        if name is not None:
            self.name = name.lower()

        if nuclei is not None:
            self.nuclei = peaks.Peak(nuclei).assignment

        if temperature is not None:
            self.temperature = round(float(temperature), 1)

        if h_larmor_frq is not None:
            self.h_larmor_frq = round(float(h_larmor_frq), 1)

        if p_total is not None:
            self.p_total = float(p_total)

        if l_total is not None:
            self.l_total = float(l_total)
Exemplo n.º 3
0
 def _shift(self, step):
     self.index += step
     self.index %= len(self.data)
     self._profile = self.data[self.index]
     self.name = peaks.Peak(self._profile.profile_name)
     self._profile_to_curve()
     self._clear_axis()
     self._plot()
Exemplo n.º 4
0
def group_data(dataset):
    """Group the data resonance specifically."""
    data_grouped = dict()

    for profile in dataset:
        resonance_id = profile.name
        peak = peaks.Peak(resonance_id)
        data_grouped[peak] = profile

    return data_grouped
Exemplo n.º 5
0
    def __lt__(self, other):
        tuple_self = (
            str(self.name),
            str(self.temperature),
            str(self.h_larmor_frq),
            str(self.p_total),
            str(self.l_total),
            peaks.Peak(self.nuclei),
        )

        tuple_other = (
            str(other.name),
            str(other.temperature),
            str(other.h_larmor_frq),
            str(other.p_total),
            str(other.l_total),
            peaks.Peak(other.nuclei),
        )

        return tuple_self < tuple_other
Exemplo n.º 6
0
def write_par(params, path):
    """Write the fitting parameters and their uncertainties to a file."""
    filename = path / "parameters.fit"

    print(f"  * {filename}")

    par_dict = {}

    for name, param in params.items():

        par_name = ParamName.from_fname(name)

        if par_name.nuclei is None:  # global parameter
            name_print = par_name
            section = "GLOBAL"

        else:  # residue-specific parameter
            name_print = peaks.Peak(par_name.nuclei)
            section = par_name.to_section_name()

        if not param.vary and param.expr is None:
            val_print = f"{param.value:.5e} ; fixed"
        elif param.stderr is None:
            val_print = f"{param.value:.5e} ; error not calculated"
        elif param.expr:
            val_print = f"{param.value:.5e} +/- {param.stderr:.5e} ; constrained"
        else:
            val_print = f"{param.value:.5e} +/- {param.stderr:.5e}"

        par_dict.setdefault(section, []).append((name_print, val_print))

    cfg = configparser.ConfigParser()
    cfg.optionxform = str

    section_global = par_dict.pop("GLOBAL", None)

    if section_global is not None:
        cfg.add_section("GLOBAL")
        for name, val in sorted(section_global):
            cfg.set("GLOBAL", str(name), val)

    for section, name_vals in sorted(par_dict.items()):
        cfg.add_section(section)
        for peak, val in sorted(name_vals):
            cfg.set(section, peak.assignment.upper(), val)

    with open(filename, "w") as f:
        cfg.write(f)
Exemplo n.º 7
0
def plot_param(args):

    params = configparser.ConfigParser()
    params.read(str(args.parameters))

    parname = args.parname

    curves = {}

    print("Plotting...")

    for section in params.sections():

        short_name = parameters.ParamName().from_section(section).name

        if parname.lower() in short_name:
            print("".join(["  [", section, "]"]))
            points = []

            for key, value in params.items(section):

                res = int(peaks.Peak(key).numbers["i"])
                split = value.split()
                value = float(split[0])

                try:
                    error = float(split[2])
                except ValueError:
                    error = 0.0

                points.append((res, value, error))

            curves[section] = zip(*points)

    _, axis = plt.subplots(figsize=(12, 5))

    axis.yaxis.grid(True)

    for section, (res, vals, errors) in curves.items():
        axis.errorbar(res, vals, yerr=errors, label=section, fmt=".", barsabove=True)

    plt.legend()
    plt.show()

    return 0
Exemplo n.º 8
0
    def to_re(self):
        """TODO: method docstring."""
        re_components = [NAME_MARKERS["name"].format(EXPAND(self.name))]

        if self.nuclei is not None:
            group_name = peaks.Peak(self.nuclei)._resonances["i"]["group"]
            if not group_name:
                all_res = "\D?[0-9]+[abd-gi-mopr-z]*"
            else:
                all_res = ""
            re_components.append(NAME_MARKERS["nuclei"].format("".join(
                [all_res, EXPAND(self.nuclei)])))
        else:
            re_components.append(".*")

        if self.temperature is not None:
            re_components.append(NAME_MARKERS["temperature"].format(
                EXPAND(str(self.temperature))))
        elif re_components[-1] != ".*":
            re_components.append(".*")

        if self.h_larmor_frq is not None:
            re_components.append(NAME_MARKERS["h_larmor_frq"].format(
                EXPAND(str(self.h_larmor_frq))))
        elif re_components[-1] != ".*":
            re_components.append(".*")

        if self.p_total is not None:
            re_components.append(NAME_MARKERS["p_total"].format(
                EXPAND(str(self.p_total))))
        elif re_components[-1] != ".*":
            re_components.append(".*")

        if self.l_total is not None:
            re_components.append(NAME_MARKERS["l_total"].format(
                EXPAND(str(self.l_total))))
        elif re_components[-1] != ".*":
            re_components.append(".*")

        re_to_match = re.compile("".join(re_components), re.IGNORECASE)

        return re_to_match
Exemplo n.º 9
0
    def __init__(self, data, path):

        self.data = data
        self.out = path

        self.names = sorted([
            peaks.Peak(profile.profile_name).names["i"]
            for profile in self.data
        ])

        self.curve, self.curve_sp = None, None
        self.cs_a, self.cs_b = {}, {}
        self.lines = []
        self.cid = None

        self.fig, self.axis = plt.subplots(figsize=(12, 5))
        self.fig.subplots_adjust(left=0.07, bottom=0.1, right=0.8, top=0.9)

        self.index = -1
        self.next(None)
Exemplo n.º 10
0
def get_pairs_from_file(filename, name):
    """Read residue specific values for fitting parameters from a file.

    The file should be formatted like a Sparky peak list.
    Examples:
      * To set G23N to 105.0 and G23H to 8.0:
          G23N-H  105.0  8.0
      * To set a parameter depending on multiple nuclei (e.g., G23N and G23H):
          G23N-H  -93.0

    """
    pairs = []

    with open(filename) as f:
        for line in f:
            if "Assignment" in line:
                continue

            line = remove_comments(line, "#;")
            line = re.sub("\s*\[\s*", "[", line)
            line = re.sub("\s*\]\s*", "]", line)
            elements = line.split()

            if len(elements) > 1:
                peak = peaks.Peak(elements[0])
                n_resonances = len(peak)
                n_cols = len(elements[1:])

                if n_cols == n_resonances:
                    for nuc_name, value in zip(peak.names.values(),
                                               elements[1:]):
                        name.update_nuclei(nuc_name)
                        pairs.append((copy.deepcopy(name), value))

                else:
                    name.update_nuclei(peak.assignment)
                    pairs.append((copy.deepcopy(name), elements[1]))

    return pairs
Exemplo n.º 11
0
    def __init__(self, name=None, data=None, exp_details=None, model=None):
        """TODO: method docstring."""

        if name is None:
            name = ""

        self.conditions = self.check_exp_details(exp_details, self.CONDITIONS)
        self.exp_details = self.check_exp_details(exp_details,
                                                  self.EXP_DETAILS)

        self.name = name
        self.peak = peaks.Peak(self.name)
        self.model = util.parse_model(model)
        self.experiment_name = self.exp_details["name"]
        self.data = data
        self.mask = np.ones(len(self.data), dtype=np.bool)

        # Set the Liouvillian
        self.liouv = basis.Liouvillian(
            system=self.SPIN_SYSTEM,
            state_nb=self.model.state_nb,
            atoms=self.peak.atoms,
            h_larmor_frq=self.conditions["h_larmor_frq"],
            equilibrium=self.EQUILIBRIUM,
        )

        self.ppms_i = self.liouv.ppms["i"]

        # Set the parameters this profile depends on
        self.map_names, self.params = default.create_params(
            basis=self.liouv,
            model=self.model,
            nuclei=self.peak.names,
            conditions=self.conditions,
            constraints=self.CONSTRAINTS,
        )

        self.calculate_unscaled_profile = lru_cache(256)(
            self._calculate_unscaled_profile)
Exemplo n.º 12
0
    def update_nuclei(self, nuclei=None):
        """TODO: method docstring."""
        if nuclei is not None:
            self.nuclei = peaks.Peak(nuclei).assignment

        return self