Beispiel #1
0
def get_grouped_params(voevent):
    """
    Fetch grouped Params from the `What` section of a voevent as an omdict.

    This fetches 'grouped' Params, i.e. those enclosed in a Group element,
    and returns them as a nested dict-like structure, keyed by
    GroupName->ParamName->AttribName.

    Note that since multiple Params may share the same ParamName, the returned
    data-structure is actually an
    `orderedmultidict.omdict <https://github.com/gruns/orderedmultidict>`_
    and has extra methods such as 'getlist' to allow retrieval of all values.

    Args:
        voevent (:class:`voeventparse.voevent.Voevent`): Root node of the VOevent etree.
    Returns (orderedmultidict.omdict):
        Mapping of ``ParamName->Attribs``.
        Typical access like so::

            foo_val = top_params['foo']['value']
            # If there are multiple Param entries named 'foo':
            all_foo_vals = [atts['value'] for atts in top_params.getlist('foo')]

    """
    groups_omd = OMDict()
    w = deepcopy(voevent.What)
    lxml.objectify.deannotate(w)
    if w.find('Group') is not None:
        for grp in w.Group:
            groups_omd.add(grp.attrib.get('name'),
                           _get_param_children_as_omdict(grp))
    return groups_omd
Beispiel #2
0
def _get_param_children_as_omdict(subtree_element):
    elt = subtree_element
    omd = OMDict()
    if elt.find('Param') is not None:
        for p in elt.Param:
            omd.add(p.attrib.get('name'), p.attrib)
    return omd
Beispiel #3
0
def get_grouped_params(voevent):
    """
    Fetch grouped Params from the `What` section of a voevent as an omdict.

    This fetches 'grouped' Params, i.e. those enclosed in a Group element,
    and returns them as a nested dict-like structure, keyed by
    GroupName->ParamName->AttribName.

    Note that since multiple Params may share the same ParamName, the returned
    data-structure is actually an
    `orderedmultidict.omdict <https://github.com/gruns/orderedmultidict>`_
    and has extra methods such as 'getlist' to allow retrieval of all values.

    Args:
        voevent (:class:`voeventparse.voevent.Voevent`): Root node of the VOevent etree.
    Returns (orderedmultidict.omdict):
        Mapping of ``ParamName->Attribs``.
        Typical access like so::

            foo_val = top_params['foo']['value']
            # If there are multiple Param entries named 'foo':
            all_foo_vals = [atts['value'] for atts in top_params.getlist('foo')]

    """
    groups_omd = OMDict()
    w = deepcopy(voevent.What)
    lxml.objectify.deannotate(w)
    if w.find('Group') is not None:
        for grp in w.Group:
            groups_omd.add(grp.attrib.get('name'),
                           _get_param_children_as_omdict(grp))
    return groups_omd
Beispiel #4
0
def _get_param_children_as_omdict(subtree_element):
    elt = subtree_element
    omd = OMDict()
    if elt.find('Param') is not None:
        for p in elt.Param:
            omd.add(p.attrib.get('name'), p.attrib)
    return omd
Beispiel #5
0
def thompson_sample(mxmdl: omdict,
                    prior_a: float = 1,
                    prior_b: float = 1) -> tuple[Atom, float]:
    """Perform Thompson sampling over the mixture model.

    Meaning, for each action

    1. Select a TV according to its likelihood (derived from its
    cognitive schematic).

    2. From that TV, sample its second order distribution to obtain a
    first order probability variate, and return the pair (action,
    pblty) corresponding to the highest variate.

    Then return the action with the highest probability of success.

    """

    agent_log.fine("thompson_sample(mxmdl={}, prior_a={}, prior_b={})".format(
        mxmdl_to_str(mxmdl), prior_a, prior_b))

    # 1. For each action select its TV according its weight
    act_w8d_cogscms = [(action, weighted_sampling(w8d_cogscms))
                       for (action, w8d_cogscms) in mxmdl.listitems()]
    agent_log.fine("act_w8d_cogscms:\n{}".format(
        act_w8d_cogscms_to_str(act_w8d_cogscms)))

    # 2. For each action select its first order probability given its tv
    act_pblts = [(action, tv_rv(get_cogscm_tv(w8_cogscm[1]), prior_a, prior_b))
                 for (action, w8_cogscm) in act_w8d_cogscms]
    agent_log.fine("act_pblts:\n{}".format(act_pblts_to_str(act_pblts)))

    # Return an action with highest probability of success (TODO: take
    # case of ties)
    return max(act_pblts, key=lambda act_pblt: act_pblt[1])
Beispiel #6
0
def mxmdl_to_str(mxmdl: omdict, indent: str = "") -> str:
    """Pretty print the given mixture model of cogscms"""

    s = ""
    for act_w8d_cogscms in mxmdl.listitems():
        action = act_w8d_cogscms[0]
        w8d_cogscms = act_w8d_cogscms[1]
        s += "\n" + indent + str(action_to_str(action)) + "\n"
        s += w8d_cogscms_to_str(w8d_cogscms, indent + "  ")
    return s