コード例 #1
0
def eval_lfunction_invmult_ops(fs, context):
    if len(fs) == 1:
        return fs[0]
    elif len(fs) > 2:
        mcdp_dev_warning('Maybe this should be smarter?')
        rest = eval_lfunction_invmult_ops(fs[1:], context)
        return eval_lfunction_invmult_ops([fs[0], rest], context)
    else:
        assert len(fs) == 2
        Fs = tuple(map(context.get_ftype, fs))

        if isinstance(Fs[0], Nat) and isinstance(Fs[1], Nat):
            dp = InvMult2Nat(Nat(), Fs)
        else:
            if isinstance(Fs[0], RcompUnits) and \
               isinstance(Fs[1], RcompUnits):
                R = mult_table(Fs[0], Fs[1])
                dp = InvMult2(R, Fs)
            elif isinstance(Fs[0], Rcomp) and isinstance(Fs[1], Rcomp):
                R = Rcomp()
                dp = InvMult2(R, Fs)
            else:
                msg = 'Could not create invmult for types {}.'.format(Fs)
                raise_desc(DPNotImplementedError, msg, Fs0=Fs[0], Fs1=Fs[1])

        return create_operation_lf(context,
                                   dp=dp,
                                   functions=fs,
                                   name_prefix='_invmult',
                                   op_prefix='_ops',
                                   res_prefix='_result')
コード例 #2
0
    def plot(self, pylab, axis, space, value, params={}):
        params0 = dict(color_shadow=[1.0, 0.8, 0.8],
                       markers='k.',
                       markers_params={})
        params0.update(params)

        color_shadow = params0.pop('color_shadow')
        markers = params0.pop('markers')
        markers_params = params0.pop('markers_params')
        if params0:
            raise ValueError(params0)

        self.axis = axis
        self.check_plot_space(space)

        minimals = [self._get_screen_coords(_, axis) for _ in value.minimals]
        print 'minimals', minimals
        R2 = PosetProduct((Rcomp(), Rcomp()))
        v = R2.Us(minimals)

        from mcdp_report.generic_report_utils import extra_space_finite

        mcdp_dev_warning('incomplete PlotterUR')
        logger.error('todo: change this to plot a line')
        plot_upset_R2(pylab,
                      v,
                      axis,
                      extra_space_shadow=extra_space_finite,
                      color_shadow=color_shadow,
                      markers=markers,
                      marker_params=markers_params)
コード例 #3
0
ファイル: dp_graph_flow_imp.py プロジェクト: rusi/mcdp
    def go_loop2(dp, imp):
        if do_imp:
            m0, _f2, _r2 = dp._unpack_m(imp)
        else:
            m0 = _f2 = _r2 = None

        (n1i, n1o) = go(dp.dp1, m0)

        i = gg.newItem('|')
        gg.propertyAppend(i, "shape", "plaintext")
        o = gg.newItem('|')
        gg.propertyAppend(o, "shape", "plaintext")

        l0 = gg.newLink(i, n1i, label=str(dp.dp1.get_fun_space()))
        l1 = gg.newLink(n1o, o, label=str(dp.dp1.get_res_space()))
        gg.propertyAppend(l0, 'arrowhead', 'none')
        gg.propertyAppend(l1, 'arrowhead', 'none')

        loop_label = str(dp.F2)

        mcdp_dev_warning('add option')
        #         if False:
        #             M = dp.get_imp_space_mod_res()
        #             M0 = dp.dp1.get_imp_space_mod_res()
        #             loop_label += ' M0: %s' % M0
        #             loop_label += ' M: %s' % M
        l = gg.newLink(o, i, label=loop_label)
        gg.propertyAppend(l, "color", "red")
        gg.propertyAppend(l, "headport", "sw")
        gg.propertyAppend(l, "tailport", "s")

        gg.propertyAppend(l, 'arrowhead', 'none')

        return (i, o)
コード例 #4
0
def gg_deepcopy(ggraph):
    try:
        return deepcopy(ggraph)
    except Exception as e:
        logger.error(traceback.format_exc(e))
        mcdp_dev_warning('Deep copy of gvgen graph failed: happens when in IPython.')
        return ggraph
コード例 #5
0
ファイル: dp_products.py プロジェクト: rusi/mcdp
 def __init__(self, nu):
     R = Rcomp()
     Fs = (R, R)
     self.nl = nu
     mcdp_dev_warning('This is not even true - it is the complicated function')
     amap = ProductNMap(Fs, R)
     WrapAMap.__init__(self, amap, None)
コード例 #6
0
ファイル: plot.py プロジェクト: kannode/mcdp
def do_plots_ndp(model_name, library, plots, extra_params):
    possible = [p for p, _ in allplots]
    plots = expand_string(plots, list(possible))

    filename = model_name + '.mcdp'
    x = library._get_file_data(filename)
    data = {}
    data['model_name'] = model_name

    data['s'] = x['data']
    data['filename'] = x['realpath']
    data['params'] = parse_params(extra_params)
    data['library'] = library

    d = dict(allplots)
    results = []
    for p in plots:
        try:
            if p in d:
                res = d[p](data)
            else:
                msg = 'Unknown plot.'
                raise_desc(ValueError, msg, plot=p, available=sorted(d.keys()))
                return  # XXX
        except CmdException as e:
            mcdp_dev_warning('Add better checks of error.')
            logger.error(e)
            continue
        except Exception as e:
            logger.error('While creating %r' % p)
            raise
        assert isinstance(res, list), res
        results.extend(res)

    return results
コード例 #7
0
ファイル: product_map.py プロジェクト: rusi/mcdp
 def __init__(self, fs):
     fs = tuple(fs)
     self.fs = fs
     mcdp_dev_warning('add promotion to SpaceProduct')
     dom = PosetProduct(tuple(fi.get_domain() for fi in fs))
     cod = PosetProduct(tuple(fi.get_codomain() for fi in fs))
     Map.__init__(self, dom=dom, cod=cod)
コード例 #8
0
    def __init__(self,
                 gg,
                 parent,
                 yourname,
                 level=0,
                 tmppath=None,
                 style='default',
                 image_source=None,
                 skip_initial=True):
        self.gg = gg
        self.parent = parent
        self.yourname = yourname
        self.level = level
        #         self.library = library

        if tmppath is None:
            d = get_mcdp_tmp_dir()
            prefix = 'GraphDrawingContext_tmppath'
            tmppath = mkdtemp(dir=d, prefix=prefix)
            mcdp_dev_warning('need to share icons')

        self.tmppath = tmppath
        self.image_source = image_source

        self.all_nodes = []

        self.set_style(style)
        self.skip_initial = skip_initial
コード例 #9
0
def gg_figure(r, name, ggraph, do_png=True, do_pdf=True, do_svg=True,
              do_dot=True):
    """ Adds a figure to the Report r that displays this graph
        and also its source. """
    f = r.figure(name, cols=1)

    # save file in dot file
    with tmpfile(".dot") as filename_dot:
        with open(filename_dot, 'w') as fo:
            s = get_dot_string(ggraph)
            fo.write(s)

#         if False:
#             ff = '%s.dot' % id(r)
#             print('writing to %r' % ff)
#             with open(ff, 'w') as f2:
#                 f2.write(s)

        prog = 'dot'
        try:

            if do_png:
                with f.data_file('graph', MIME_PNG) as filename:
                    graphviz_run(filename_dot, filename, prog=prog)

            if do_pdf:
                with f.data_file('graph_pdf', MIME_PDF) as filename:
                    graphviz_run(filename_dot, filename, prog=prog)

            if do_svg:
                with f.data_file('graph_svg', MIME_SVG) as filename:
                    graphviz_run(filename_dot, filename, prog=prog)

                    from mcdp_report.embedded_images import embed_svg_images
                    data = open(filename).read()
                    soup = bs(data)
                    embed_svg_images(soup)
                    # does not keep doctype: s = to_html_stripping_fragment(soup)
                    # this will keep the doctype
                    s = str(soup)
                    s = s.replace('<fragment>', '')
                    s = s.replace('</fragment>', '')
                    write_bytes_to_file_as_utf8(s, filename)

        except CmdException:
            if MCDPConstants.test_ignore_graphviz_errors:
                mcdp_dev_warning('suppressing errors from graphviz')
                logger.error('Graphivz failed, but I will ignore it '
                             'because of MCDPConstants.test_ignore_graphviz_errors.')
            else:
                raise

        # MIME_GRAPHVIZ
        if do_dot:
            with f.data_file('dot', MIME_PLAIN) as filename:
                with open(filename, 'w') as f:
                    f.write(s)

    return f
コード例 #10
0
    def add_connection(self, c):
        self.info('Adding connection %r' % str(c))
        if not c.dp1 in self.names:
            raise_desc(DPSemanticError,
                       'Invalid connection: %r not found.' % c.dp1,
                       names=self.names,
                       c=c)

        if not c.dp2 in self.names:
            raise_desc(DPSemanticError,
                       'Invalid connection: %r not found.' % c.dp2,
                       names=self.names,
                       c=c)

        mcdp_dev_warning('redo this check')

        if self.is_new_function(c.dp2):
            msg = "Cannot add connection to new function %r." % c.dp2
            raise_desc(DPSemanticError, msg, c=c)

        if self.is_new_resource(c.dp1):
            msg = "Cannot add connection to new resource %r." % c.dp1
            raise_desc(DPSemanticError, msg, c=c)

        # Find if there is already a connection to c.dp2,c.s2
        # for c0 in self.connections:
        #    if c0.dp2 == c.dp2 and c0.s2 == c.s2:
        #        msg = 'There is already a connection to function %r of %r.' % (c.s2, c.dp2)
        #        raise_desc(DPSemanticError, msg)

        ndp1 = self.names[c.dp1]
        ndp2 = self.names[c.dp2]

        rnames = ndp1.get_rnames()
        if not c.s1 in rnames:
            msg = "Resource %r does not exist (known: %s)" % (
                c.s1, format_list(rnames))
            raise_desc(DPSemanticError, msg, known=rnames)

        fnames = ndp2.get_fnames()
        if not c.s2 in fnames:
            msg = "Function %r does not exist (known: %s)" % (
                c.s2, format_list(fnames))
            raise_desc(DPSemanticError, msg, known=fnames)

        R1 = ndp1.get_rtype(c.s1)
        F2 = ndp2.get_ftype(c.s2)
        # print('connecting R1 %s to R2 %s' % (R1, F2))
        if not (R1 == F2):
            msg = 'Connection between different spaces.'
            raise_desc(DPSemanticError,
                       msg,
                       c=c,
                       F2=F2,
                       R1=R1,
                       ndp1=ndp1,
                       ndp2=ndp2)

        self.connections.append(c)
コード例 #11
0
def make_parallel_n(dps):
    if len(dps) == 2:
        return make_parallel(dps[0], dps[1])

    if len(dps) == 0:
        mcdp_dev_warning('This works but should be a special case.')

    return ParallelN(tuple(dps))
コード例 #12
0
 def solve_r(self, r):  # @UnusedVariable
     mcdp_dev_warning('Not sure about this')
     algo = InvMult2.ALGO
     options = invmultL_solve_options(F=self.R,
                                      R=self.F,
                                      f=r,
                                      n=self.nl,
                                      algo=algo)
     return self.F.Ls(options)
コード例 #13
0
 def solve_r(self, r):
     # we want this to be pessimistic
     mcdp_dev_warning('Not sure about this: is it L or U?')
     algo = InvMult2.ALGO
     options = invmultL_solve_options(F=self.R,
                                      R=self.F,
                                      f=r,
                                      n=self.nu,
                                      algo=algo)
     return self.F.Ls(options)
コード例 #14
0
def enlarge(b, f):
    w = b[1] - b[0]
    h = b[3] - b[2]
    # print b, f, w, h

    dw = fix_underflow(f) * fix_underflow(w)
    dh = fix_underflow(h) * fix_underflow(f)
    a = (b[0] - dw, b[1] + dw, b[2] - dh, b[3] + dh)

    return a

    mcdp_dev_warning('this is not correct (axis might be negative)')
    return tuple(map(fix_underflow, a))
コード例 #15
0
ファイル: optimization.py プロジェクト: rusi/mcdp
 def get_providers_for_type(self, R):
     options = []
     tu = get_types_universe()
     for id_ndp in self.options:
         ndp = self.load_ndp(id_ndp)
         fnames = ndp.get_fnames()
         ftypes = ndp.get_ftypes(fnames)
         for fname, F in zip(fnames, ftypes):
             if tu.leq(R, F):
                 options.append((id_ndp, fname))
                 mcdp_dev_warning("assume that it is symmetric")
                 break
     return options
コード例 #16
0
ファイル: test_md_rendering.py プロジェクト: rusi/mcdp
def define_tests_mcdp_web(context):
    """
        Looks for directories called *.mcdplib in the root of the package.
        
        It also looks for the files *.mcdp_tests.yaml inside.
    """
    for libname in enumerate_test_libraries():
        c2 = context.child(libname)

        c2.comp_dynamic(define_tests_rendering, libname)

        if False:
            c2.comp(test_mcdpweb_server, libname)
        else:
            mcdp_dev_warning('test_mcdpweb_server() is not enabled')
コード例 #17
0
    def should_I_skip_leq(self, context, c):
        from mcdp_report.gg_ndp import is_simple
        if self.policy_skip == 'never':
            return False
        elif self.policy_skip == 'if_second_simple':
            second_simple = is_simple(context.names[c.dp2])
            # first_simple = is_simple(context.names[c.dp1])
            # any_simple = second_simple or first_simple
            # both_simple = second_simple and first_simple

            mcdp_dev_warning('Add options here')
            skip = second_simple
            return skip
        else:
            assert False, self.policy_skip
コード例 #18
0
def eval_ndp_approx_lower(r, context):
    from mcdp_lang.eval_ndp_imp import eval_ndp
    from mcdp_dp.dp_transformations import get_dp_bounds

    nl = r.level
    ndp = eval_ndp(r.ndp, context)
    dp = ndp.get_dp()
    mcdp_dev_warning('make it better')
    dpl, _ = get_dp_bounds(dp, nl, 1)

    fnames = ndp.get_fnames()
    rnames = ndp.get_rnames()
    rnames = rnames if len(rnames) > 1 else rnames[0]
    fnames = fnames if len(fnames) > 1 else fnames[0]
    ndp2 = SimpleWrap(dpl, fnames, rnames)
    return ndp2
コード例 #19
0
def gg_get_formats(gg, data_formats):
    check_isinstance(data_formats, (list, tuple))
    res = []
    mcdp_dev_warning('TODO: optimize gg_get_formats')
    for data_format in data_formats:
        if not data_format in allowed_formats:
            msg = 'Invalid data format.'
            raise_desc(ValueError, msg, data_formats=data_formats)

        if data_format == 'dot':
            d = get_dot_string(gg)
        else:
            d = gg_get_format(gg, data_format)

        res.append(d)
    return tuple(res)
コード例 #20
0
def get_conversion(A, B):
    """ Returns None if there is no need for a Conversion Map.
        Otherwise returns a Conversion (< WrapAMap). """
    tu = get_types_universe()

    if tu.equal(A, B):
        conversion = None
    else:
        try:
            A_to_B, B_to_A = tu.get_super_conversion(A, B)
            mcdp_dev_warning('not really sure of the semantics of this')
            conversion = Conversion(A_to_B, B_to_A)
        except NotLeq:
            raise

    return conversion
コード例 #21
0
ファイル: eval_uncertainty.py プロジェクト: kannode/mcdp
def eval_lfunction_Uncertain(r, context):
    from mcdp_dp import UncertainGateSym
    from .eval_lfunction_imp import eval_lfunction
    from .helpers import create_operation_lf

    assert isinstance(r, CDP.UncertainFun)

    fl = eval_lfunction(r.lower, context)
    fu = eval_lfunction(r.upper, context)

    F = context.get_ftype(fl)
    # Fu = context.get_rtype(fu)
    mcdp_dev_warning('Do explicit check of types.')

    dp = UncertainGateSym(F)

    return create_operation_lf(context, dp=dp, functions=[fl, fu])
コード例 #22
0
    def decorate_arrow_function(self, l1):
        propertyAppend = self.gg.propertyAppend

        if self.style == STYLE_GREENRED:
            propertyAppend(l1, 'color', COLOR_DARKGREEN)
            propertyAppend(l1, 'arrowhead', 'normal')
            propertyAppend(l1, 'arrowtail', 'none')
            propertyAppend(l1, 'dir', 'both')

        if self.style == STYLE_GREENREDSYM:
            propertyAppend(l1, 'color', 'darkgreen')
            propertyAppend(l1, 'fontcolor', COLOR_DARKGREEN)
            propertyAppend(l1, 'arrowhead', 'dot')
            propertyAppend(l1, 'arrowtail', 'none')
            propertyAppend(l1, 'dir', 'both')

        mcdp_dev_warning('this above has no effect')
        propertyAppend(l1, 'fontcolor', COLOR_DARKGREEN)
コード例 #23
0
ファイル: image.py プロジェクト: rusi/mcdp
def ndp_template_graph_enclosed(library, template, style, yourname,
                                data_format, direction, enclosed,
                                image_source):
    assert isinstance(template, TemplateForNamedDP)
    mcdp_dev_warning('Wrong - need assume ndp const')

    context = library._generate_context_with_hooks()

    ndp = template.get_template_with_holes(context)

    if enclosed:
        setattr(ndp, '_hack_force_enclose', True)

    gg = gvgen_from_ndp(ndp,
                        style=style,
                        direction=direction,
                        image_source=image_source,
                        yourname=yourname)
    return gg_get_format(gg, data_format)
コード例 #24
0
ファイル: gg_ndp.py プロジェクト: kannode/mcdp
def format_unit(R):

    if R == BottomCompletion(TopCompletion(Any())):
        return '[*]'
    mcdp_dev_warning('fix bug')
    if isinstance(R, BottomCompletion):
        return '[*]'
    if R == R_dimensionless:
        # TODO: make option
        return ''
    elif isinstance(R, RcompUnits):
        return '[%s]' % format_pint_unit_short(R.units)
    elif isinstance(R, Rcomp):
        return '[]'
    elif hasattr(R, MCDPConstants.ATTR_LOAD_NAME):
        n = getattr(R, MCDPConstants.ATTR_LOAD_NAME)
        return '[`%s]' % n
    else:
        return '[%s]' % str(R)
コード例 #25
0
    def decorate_arrow_resource(self, l2):
        propertyAppend = self.gg.propertyAppend

        mcdp_dev_warning('this above has no effect')
        propertyAppend(l2, 'fontcolor', COLOR_DARKRED)

        if self.style == STYLE_GREENRED:
            propertyAppend(l2, 'color', COLOR_DARKRED)
            propertyAppend(l2, 'fontcolor', COLOR_DARKRED)
            propertyAppend(l2, 'arrowtail', 'inv')
            propertyAppend(l2, 'arrowhead', 'none')
            propertyAppend(l2, 'dir', 'both')

        if self.style == STYLE_GREENREDSYM:
            propertyAppend(l2, 'color', COLOR_DARKRED)
            propertyAppend(l2, 'arrowtail', 'dot')
            propertyAppend(l2, 'arrowhead', 'none')
            propertyAppend(l2, 'dir', 'both')

        propertyAppend(l2, 'style', 'dashed')
コード例 #26
0
ファイル: map_composition.py プロジェクト: rusi/mcdp
    def __init__(self, maps):
        """ 
            maps = [f, g, h]
            === h o g o f
            
            They are in order of application.
        """
        self.maps = tuple(maps)
        from mcdp_posets.types_universe import get_types_universe

        tu = get_types_universe()
        for i in range(len(maps) - 1):
            first = maps[i]
            second = maps[i + 1]
            cod1 = first.get_codomain()
            dom2 = second.get_domain()
            tu.check_equal(cod1, dom2)

        mcdp_dev_warning('Check that the composition makes sense')
        dom = self.maps[0].get_domain()
        cod = self.maps[-1].get_codomain()
        Map.__init__(self, dom=dom, cod=cod)
コード例 #27
0
def check_compact(id_ndp, ndp):


    try:
        ndp.check_fully_connected()
    except NotConnected:
        print('Skipping check_compact because %r not connected.' % id_ndp)
        return

    mcdp_dev_warning("""
I'm not really sure why compact needs to abstract().

It should be replaced with one that creates a new NDP

[ A ]--[ B ]
[ A ]--[ B ]

( [ A ]-|__)__ (_|-[  ] )
( [ A ]-|  )   ( |-[ B] )
    """)
    ndp2 = ndp.compact()
    check_same_interface(ndp, ndp2)
コード例 #28
0
def plot_upset_R2(pylab,
                  us,
                  axis,
                  color_shadow,
                  extra_space_shadow=0.05,
                  color_lines='none',
                  markers='r.',
                  marker_params={}):

    points = us.minimals

    xmin, xmax, ymin, ymax = axis
    for p in points:
        if xmin <= p[0] <= xmax and (ymin <= p[1] <= ymax):
            mcdp_dev_warning('This should be smarter')

            # draw a little of them
            if p[0] == xmax:
                axis = enlarge_x(axis, extra_space_shadow)
            if p[1] == ymax:
                axis = enlarge_y(axis, extra_space_shadow)

            plot_cone(pylab,
                      p,
                      axis,
                      color_shadow=color_shadow,
                      color_lines=color_lines)
        else:
            logger.debug('Warning: point %s not in axis %s' % (p, axis))

    # cuteness
    if markers is not None:
        for p in points:
            # This is to avoid underflow
            # when using "finfo.tiny"
            eps = finfo.eps
            p = np.maximum(p, eps)
            #print('plot_upset_R2: marker params: %s ' % marker_params)
            pylab.plot(p[0], p[1], markers, clip_on=False, **marker_params)
コード例 #29
0
def eval_lfunction_invplus_ops(fs, context):
    if len(fs) == 1:
        raise DPInternalError(fs)
    elif len(fs) > 2:  # pragma: no cover
        mcdp_dev_warning('Maybe this should be smarter?')
        rest = eval_lfunction_invplus_ops(fs[1:], context)
        return eval_lfunction_invplus_ops([fs[0], rest], context)
    else:
        Fs = map(context.get_ftype, fs)
        R = Fs[0]

        if all(isinstance(_, RcompUnits) for _ in Fs):
            tu = get_types_universe()
            if not tu.leq(Fs[1], Fs[0]):
                msg = 'Inconsistent units %s and %s.' % (Fs[1], Fs[0])
                raise_desc(DPSemanticError, msg, Fs0=Fs[0], Fs1=Fs[1])

            if not tu.equal(Fs[1], Fs[0]):
                msg = 'This case was not implemented yet. Differing units %s and %s.' % (
                    Fs[1], Fs[0])
                raise_desc(DPNotImplementedError, msg, Fs0=Fs[0], Fs1=Fs[1])

            dp = InvPlus2(R, tuple(Fs))
        elif all(isinstance(_, Rcomp) for _ in Fs):
            dp = InvPlus2(R, tuple(Fs))
        elif all(isinstance(_, Nat) for _ in Fs):
            dp = InvPlus2Nat(R, tuple(Fs))

        else:  # pragma: no cover
            msg = 'Cannot find operator for these types.'
            raise_desc(DPInternalError, msg, Fs=Fs)

        return create_operation_lf(context,
                                   dp=dp,
                                   functions=fs,
                                   name_prefix='_invplus',
                                   op_prefix='_',
                                   res_prefix='_result')
コード例 #30
0
ファイル: recursive_name_labeling.py プロジェクト: rusi/mcdp
def collect(I, imp):
    I.belongs(imp)
    res = {}

    if isinstance(I, SpaceProduct):
        for j, sub in enumerate(I.subs):
            res.update(**collect(sub, imp[j]))

    elif isinstance(I, Coproduct1Labels):
        i, xi = I.unpack(imp)
        Ii = I.spaces[i]

        res.update(**collect(Ii, xi))
    else:
        pass

    mcdp_dev_warning('a little more thought')
    att = MCDPConstants.ATTRIBUTE_NDP_RECURSIVE_NAME
    if not res and hasattr(I, att):
        name = getattr(I, att)
        return {name: imp}

    return res