Exemple #1
0
def _parse_key(scope='base', usage='input', **match):
    if scope == 'flag':
        if match['flag'] == 'vehicle_family_id':
            scope = 'dice'
        yield scope, match['flag']
    elif scope == 'dice':
        yield scope, match['dice']
    elif scope == 'meta':
        meta = _re_space_dot.sub(match.get('meta', ''), '.').replace('-', '_')
        yield scope, meta, match['param']
    elif scope == 'plan':
        if 'param' in match:
            m = _re_params_name.match('.'.join((scope, match['param'])))
            if m:
                m = {i: j for i, j in m.groupdict().items() if j}
                if 'index' in m:
                    match = m

        if 'index' in match:
            yield scope, match['index']
        else:
            for k in _parse_key(match.get('v_scope', 'base'), usage, **match):
                yield scope, '.'.join(k)
    elif scope == 'base':
        i = match['param']

        if i.lower() == 'version':
            yield 'flag', 'input_version'
        else:
            m = match.copy()
            for c in sh.stlp(_get_cycle(usage=usage, **match)):
                m['cycle'] = c
                stage = _get_default_stage(usage=usage, **m)
                yield scope, usage, stage, c, i
Exemple #2
0
def _pipe2list(pipe, i=0, source=()):
    res, max_l = [], i
    idx = {'nodes L%d' % i: str(v) for i, v in enumerate(source)}
    node_id = 'nodes L%d' % i
    for k, v in pipe.items():
        k = sh.stlp(k)
        d = {node_id: str(k)}

        if 'error' in v:
            d['error'] = v['error']

        j, s = v['task'][2]
        n = s.workflow.nodes.get(j, {})
        if 'duration' in n:
            d['duration'] = n['duration']

        d.update(idx)
        res.append(d)

        if 'sub_pipe' in v:
            l, ml = _pipe2list(v['sub_pipe'], i=i + 1, source=source + (k, ))
            max_l = max(max_l, ml)
            res.extend(l)

    return res, max_l
Exemple #3
0
def notify_result_listener(result_listener, res, out_fpath=None):
    """Utility func to send to the listener the output-file discovered from the results."""
    are_in = sh.are_in_nested_dicts
    if result_listener:

        if not out_fpath:
            it = []
            for k in ('output_file_name', 'output_ta_file'):
                if sh.are_in_nested_dicts(res, 'solution', k):
                    it.append(res['solution'][k])
        else:
            it = sh.stlp(out_fpath)
        try:
            for fpath in it:
                result_listener((fpath, res))
        except Exception as ex:
            try:
                keys = list(res)
            except Exception:
                keys = '<no keys>'
            log.warning(
                "Failed notifying result-listener due to: %s\n  result-keys: %s",
                ex,
                keys,
                exc_info=1)
Exemple #4
0
def _select_index(rank, select=()):
    for k in sh.stlp(select):
        if k is None:
            i = -1
            break
        cycle = k.lower().replace('-', '_')
        gen = (i for i, m in enumerate(rank) if m[3] == cycle)
        i = next(gen, sh.NONE)
        if i is not sh.NONE:
            break
    else:
        i = 0

    return i
Exemple #5
0
def resample_data(labels, reference_name, data, shifts, methods):
    """
    Resample all data-sets using the reference signal.

    :param labels:
        Reference-labels (i.e., "x", "y") for each data-set.

        It is like `{"<set-name>": {"x": "<x-label>", "y": "<y-label>"}, ...}`.
    :type labels: collections.defaultdict

    :param reference_name:
        Reference data-set name.
    :type reference_name: str

    :param data:
        Data-sets.
    :type data: dict[str, dict[str, numpy.array]]

    :param shifts:
        Shifts from the reference data-set.
    :type shifts: dict[str, float]

    :param methods:
        Interpolation methods for each variable of each data-set.

        It is like `{"<set-name>": {"<var-name>": "<interp>", ...}, ...}`.
    :type methods: collections.defaultdict

    :return:
        Resampled data-sets.
    :rtype: dict[str, dict[str, numpy.array]]
    """
    x = data[reference_name][labels[reference_name]['x']]
    r, res = {reference_name: data[reference_name]}, {}
    for k, s in shifts.items():
        r[k] = _interpolate(x + s, labels[k]['x'], data[k], methods[k])

    for (i, j), v in sh.stack_nested_keys(r):
        j = sh.stlp(j)
        sh.get_nested_dicts(res, i, *j[:-1])[j[-1]] = v

    return res
Exemple #6
0
def plot_time_series(
        dsp, x_id, *y_ids, title=None, x_label=None, y_label=None, **kwargs):
    """
    Plot time series from the dsp.

    :param dsp:
        Co2mpas model.
    :type dsp: schedula.Dispatcher

    :param x_id:
        Id of X axes.
    :type x_id: str | tuple[str]

    :param y_ids:
        Ids of data to plot.
    :type y_ids: tuple[dict | str | tuple[str]]

    :param title:
        Plot title.
    :type title: str

    :param x_label:
        Label of X axes.
    :type x_label: str

    :param y_label:
        Label of X axes.
    :type y_label: str

    :param kwargs:
        Optional plot kwargs.
    :type y_label: dict
    """
    import matplotlib.pyplot as plt
    import schedula as sh

    x_id = sh.stlp(x_id)
    x, x_id = dsp.get_node(*x_id)
    if x_label is None:
        x_label = dsp.get_node(*x_id, node_attr='description')[0]

    if x_label:
        plt.xlabel(x_label)

    if title is not None:
        plt.title(title)

    for data in y_ids:
        if not isinstance(data, dict):
            data = {'id': data, 'x': x}

        if 'id' in data:
            y_id = sh.stlp(data.pop('id'))

            des = y_label is None or 'label' not in data
            if des or 'y' not in data:

                y, y_id = dsp.get_node(*y_id)

                if des:
                    label = dsp.get_node(*y_id, node_attr='description')[0]

                if y_label is None:
                    y_label = label

                if 'label' not in data:
                    data['label'] = label or y_id[-1]

                if 'y' not in data:
                    data['y'] = y

            elif 'y' not in data:
                data['y'] = dsp.get_node(*y_id)[0]

        x = data.pop('x', x)
        y = data.pop('y')

        if x.shape[0] != y.shape[0]:
            y = y.T

        data.update(kwargs)
        plt.plot(x, y, **data)

    if y_label:
        plt.ylabel(y_label)

    plt.legend()
Exemple #7
0
def _sort_rank_for_selecting_best(rank, select=(), **kwargs):
    select = tuple(k.lower().replace('-', '_') for k in sh.stlp(select))
    mw = len(select) + 1
    w = {k: v for v, k in enumerate(select)}
    rank = sorted(((w.get(m[3], mw), i), m) for i, m in enumerate(rank))
    return [v[-1] for v in rank]