def draw_libdepgraph(res): """ Just display the dependencies between libraries """ r = Report() fd = res['fd'] assert isinstance(fd, FindDependencies) G = fd.create_libgraph() # create another graph only with libraries gg = my_gvgen.GvGen(options="rankdir=TB") @memoize_simple def get_gg_node(entry): label = entry return gg.newItem(label) for entry in G.nodes(): get_gg_node(entry) for entry1, entry2 in G.edges(): n1 = get_gg_node(entry1) n2 = get_gg_node(entry2) gg.newLink(n1, n2) gg_figure(r, 'graph', gg, do_dot=False) return r
def draw_depgraph(res): r = Report() fd = res['fd'] assert isinstance(fd, FindDependencies) G = fd.create_graph() gg = my_gvgen.GvGen(options="rankdir=TB") @memoize_simple def get_gg_cluster(libname): print('creating cluster %s ' % entry) return gg.newItem(libname) @memoize_simple def get_gg_node(entry): print('creating node %s ' % entry) parent = get_gg_cluster(entry.libname) label = '%s/%s' % (entry.libname, entry.name) return gg.newItem(label, parent=parent) for entry in G.nodes(): get_gg_node(entry) for entry1, entry2 in G.edges(): n1 = get_gg_node(entry1) n2 = get_gg_node(entry2) gg.newLink(n1, n2) gg_figure(r, 'graph', gg, do_dot=False) return r
def plot_different_solutions(libname, ndpname, query, out, upper=None): if not os.path.exists(out): os.makedirs(out) library = get_test_library(libname) #library.use_cache_dir(os.path.join(out, 'cache')) context = Context() ndp = library.load_ndp(ndpname, context) context = library._generate_context_with_hooks() ndp_labelled = get_labelled_version(ndp) dp0 = ndp_labelled.get_dp() if upper is not None: _, dpU = get_dp_bounds(dp0, nl=1, nu=upper) dp = dpU else: dp = dp0 M = dp.get_imp_space() with open(os.path.join(out, 'ndp.txt'), 'w') as f: f.write(ndp.repr_long()) with open(os.path.join(out, 'M.txt'), 'w') as f: f.write(M.repr_long()) with open(os.path.join(out, 'dp.txt'), 'w') as f: f.write(dp.repr_long()) with open(os.path.join(out, 'dp0.txt'), 'w') as f: f.write(dp0.repr_long()) f = convert_string_query(ndp=ndp, query=query, context=context) report = Report() res = dp.solve(f) print('num solutions: %s' % len(res.minimals)) for ri, r in enumerate(res.minimals): ms = dp.get_implementations_f_r(f, r) for j, m in enumerate(ms): imp_dict = get_imp_as_recursive_dict(M, m) print imp_dict images_paths = library.get_images_paths() gv = GetValues(ndp=ndp, imp_dict=imp_dict, nu=upper, nl=1) gg = gvgen_from_ndp(ndp=ndp, style=STYLE_GREENREDSYM, images_paths=images_paths, plotting_info=gv) with report.subsection('%s-%s' % (ri, j)) as rr: gg_figure(rr, 'figure', gg, do_png=True, do_pdf=False, do_svg=False, do_dot=False) fn = os.path.join(out, 'solutions.html') print('writing to %s' % fn) report.to_html(fn)
def get_tree_report(self): r = Report() with r.subsection('regular') as rr: gg = self.draw_tree_get_tree_expand() gg_figure(rr, 'tree', gg, do_png=True, do_dot=False, do_svg=False) with r.subsection('compact') as rr: gg = self.draw_tree_get_tree_compact() gg_figure(r, 'tree', gg, do_png=True, do_dot=False, do_svg=False) return r
def plot_ndp(r, name, ndp, library): ndp = cndp_templatize_children(ndp) images_paths = library.get_images_paths() setattr(ndp, '_hack_force_enclose', True) gg = gvgen_from_ndp(ndp, style=STYLE_GREENREDSYM, images_paths=images_paths) gg_figure(r, name, gg, do_pdf=False, do_svg=False, do_dot=False)
def test_imp_dict_1(id_ndp, ndp): if '_inf' in id_ndp: # infinite return try: ndp.check_fully_connected() except NotConnected: print('Skipping test_imp_dict_1 because %r not connected.' % id_ndp) return ndp_labeled = get_labelled_version(ndp) dp0 = ndp_labeled.get_dp() F = dp0.get_fun_space() I = dp0.get_imp_space() # print ndp_labeled.repr_long() # print dp0.repr_long() print('I: %s' % I.repr_long()) f = list(F.get_minimal_elements())[0] try: ur = dp0.solve(f) except NotSolvableNeedsApprox: return imp_dict = None for r in ur.minimals: imps = dp0.get_implementations_f_r(f, r) for imp in imps: I.belongs(imp) context = {} imp_dict = get_imp_as_recursive_dict(I, imp) print('imp_dict: {}'.format(imp_dict)) artifact = ndp_make(ndp, imp_dict, context) print('artifact: {}'.format(artifact)) # Let's just do it with one if imp_dict is not None: gv = GetValues(ndp=ndp, imp_dict=imp_dict, nu=None, nl=None) images_paths = [] # library.get_images_paths() from mcdp_report.gdc import STYLE_GREENREDSYM gg = gvgen_from_ndp(ndp=ndp, style=STYLE_GREENREDSYM, images_paths=images_paths, plotting_info=gv) from reprep import Report from mcdp_report.gg_utils import gg_figure report = Report() gg_figure(report, 'figure', gg, do_png=True, do_pdf=False, do_svg=False, do_dot=False) fn = os.path.join('out', 'test_imp_dict_1', '%s.html' % id_ndp) print('written to %s' % fn) report.to_html(fn)
def test_imp_dict_1(id_ndp, ndp): if '_inf' in id_ndp: # infinite return try: ndp.check_fully_connected() except NotConnected: print('Skipping test_imp_dict_1 because %r not connected.' % id_ndp) return ndp_labeled = get_labelled_version(ndp) dp0 = ndp_labeled.get_dp() F = dp0.get_fun_space() I = dp0.get_imp_space() # print ndp_labeled.repr_long() # print dp0.repr_long() print('I: %s' % I.repr_long()) f = list(F.get_minimal_elements())[0] try: ur = dp0.solve(f) except NotSolvableNeedsApprox: return imp_dict = None for r in ur.minimals: imps = dp0.get_implementations_f_r(f, r) for imp in imps: I.belongs(imp) context = {} imp_dict = get_imp_as_recursive_dict(I, imp) print('imp_dict: {}'.format(imp_dict)) artifact = ndp_make(ndp, imp_dict, context) print('artifact: {}'.format(artifact)) # Let's just do it with one if imp_dict is not None: gv = GetValues(ndp=ndp, imp_dict=imp_dict, nu=None, nl=None) # images_paths = [] # library.get_images_paths() from mcdp_report.gdc import STYLE_GREENREDSYM gg = gvgen_from_ndp(ndp=ndp, style=STYLE_GREENREDSYM, plotting_info=gv) from reprep import Report from mcdp_report.gg_utils import gg_figure report = Report() gg_figure(report, 'figure', gg, do_png=True, do_pdf=False, do_svg=False, do_dot=False) fn = os.path.join('out', 'test_imp_dict_1', '%s.html' % id_ndp) print('written to %s' % fn) report.to_html(fn)
def solve_main( logger, config_dirs, maindir, cache_dir, model_name, lower, upper, out_dir, max_steps, query_strings, intervals, _exp_advanced, expect_nres, imp, expect_nimp, plot, do_movie, # expect_res=None, expect_res, # @UnusedVariable make, ): if out_dir is None: out = solve_get_output_dir(prefix="out/out") else: out = out_dir logger.info("Using output dir %r" % out) librarian = Librarian() logger.info("Looking for libraries in %s..." % config_dirs) for e in config_dirs: librarian.find_libraries(e) logger.info("Found %d libraries." % len(librarian.get_libraries())) library = librarian.get_library_by_dir(maindir) if cache_dir is not None: library.use_cache_dir(cache_dir) ndp = library.load_ndp(model_name) basename = model_name if make or (plot and imp): ndp_labelled = get_labelled_version(ndp) else: ndp_labelled = ndp basename, dp = solve_get_dp_from_ndp(basename=basename, ndp=ndp_labelled, lower=lower, upper=upper) F = dp.get_fun_space() R = dp.get_res_space() UR = UpperSets(R) query = " ".join(query_strings) c = library.parse_constant(query) tu = get_types_universe() try: tu.check_leq(c.unit, F) except NotLeq as e: msg = "The value given cannot be converted to functionality space." raise_wrapped(UserError, e, msg, unit=c.unit, F=F, compact=True) fg = express_value_in_isomorphic_space(c.unit, c.value, F) logger.info("query: %s" % F.format(fg)) tracer = Tracer(logger=logger) res, trace = solve_meat_solve(tracer, ndp, dp, fg, intervals, max_steps, _exp_advanced) nres = len(res.minimals) if expect_nres is not None: if nres != expect_nres: msg = "Found wrong number of resources" raise_desc(ExpectationsNotMet, msg, expect_nres=expect_nres, nres=nres) if imp: M = dp.get_imp_space() nimplementations = 0 for r in res.minimals: ms = dp.get_implementations_f_r(fg, r) nimplementations += len(ms) s = "r = %s " % R.format(r) for j, m in enumerate(ms): # print('m = %s' % str(m)) s += "\n implementation %d of %d: m = %s " % (j + 1, len(ms), M.format(m)) if make: imp_dict = get_imp_as_recursive_dict(M, m) # , ignore_hidden=False) print("imp dict: %r" % imp_dict) context = {} artifact = ndp_make(ndp, imp_dict, context) print("artifact: %s" % artifact) tracer.log(s) if expect_nimp is not None: if expect_nimp != nimplementations: msg = "Found wrong number of implementations" raise_desc(ExpectationsNotMet, msg, expect_nimp=expect_nimp, nimplementations=nimplementations) # if expect_res is not None: # value = interpret_string(expect_res) # tracer.log('value: %s' % value) # res_expected = value.value # tu = get_types_universe() # # If it's a tuple of two elements, then we assume it's upper/lower bounds # if isinstance(value.unit, PosetProduct): # subs = value.unit.subs # assert len(subs) == 2, subs # # lower_UR_expected, upper_UR_expected = subs # lower_res_expected, upper_res_expected = value.value # # lower_bound = tu.get_embedding(lower_UR_expected, UR)[0](lower_res_expected) # upper_bound = tu.get_embedding(upper_UR_expected, UR)[0](upper_res_expected) # # tracer.log('lower: %s <= %s' % (UR.format(lower_bound), UR.format(res))) # tracer.log('upper: %s <= %s' % (UR.format(upper_bound), UR.format(res))) # # UR.check_leq(lower_bound, res) # UR.check_leq(res, upper_bound) # else: # # only one element: equality # UR_expected = value.unit # tu.check_leq(UR_expected, UR) # A_to_B, _B_to_A = tu.get_embedding(UR_expected, UR) # # res_expected_f = A_to_B(res_expected) # try: # UR.check_equal(res, res_expected_f) # except NotEqual as e: # raise_wrapped(ExpectationsNotMet, e, 'res is different', # res=res, res_expected=res_expected, compact=True) if plot: r = Report() if _exp_advanced: from mcdp_report.generic_report_utils import generic_report generic_report(r, dp, trace, annotation=None, axis0=(0, 0, 0, 0)) else: f = r.figure() from mcdp_report.generic_report_utils import generic_plot generic_plot(f, space=UR, value=res) from mcdp_report.generic_report_utils import generic_report_trace generic_report_trace(r, ndp, dp, trace, out, do_movie=do_movie) out_html = os.path.join(out, "report.html") logger.info("writing to %r" % out_html) r.to_html(out_html) if plot and imp: from mcdp_report_ndp_tests.test1 import GetValues from mcdp_report.gg_ndp import gvgen_from_ndp from mcdp_report.gdc import STYLE_GREENREDSYM from mcdp_report.gg_utils import gg_figure M = dp.get_imp_space() report_solutions = Report() for i, r in enumerate(res.minimals): ms = dp.get_implementations_f_r(fg, r) for j, m in enumerate(ms): imp_dict = get_imp_as_recursive_dict(M, m) images_paths = library.get_images_paths() gv = GetValues(ndp=ndp, imp_dict=imp_dict, nu=upper, nl=1) setattr(ndp, "_hack_force_enclose", True) with report_solutions.subsection("sol-%s-%s" % (i, j)) as rr: # Left right gg = gvgen_from_ndp( ndp=ndp, style=STYLE_GREENREDSYM, images_paths=images_paths, plotting_info=gv, direction="LR" ) gg_figure(rr, "figure", gg, do_png=True, do_pdf=True, do_svg=False, do_dot=False) # Top-bottom gg = gvgen_from_ndp( ndp=ndp, style=STYLE_GREENREDSYM, images_paths=images_paths, plotting_info=gv, direction="TB" ) gg_figure(rr, "figure2", gg, do_png=True, do_pdf=True, do_svg=False, do_dot=False) out_html = os.path.join(out, "report_solutions.html") logger.info("writing to %r" % out_html) report_solutions.to_html(out_html)
def solve_main( logger, config_dirs, maindir, cache_dir, model_name, lower, upper, out_dir, max_steps, query_strings, intervals, _exp_advanced, expect_nres, imp, expect_nimp, plot, do_movie, # expect_res=None, expect_res, # @UnusedVariable make): if out_dir is None: out = solve_get_output_dir(prefix='out/out') else: out = out_dir logger.info('Using output dir %r' % out) librarian = Librarian() logger.info('Looking for libraries in %s...' % config_dirs) for e in config_dirs: librarian.find_libraries(e) logger.info('Found %d libraries.' % len(librarian.get_libraries())) library = librarian.get_library_by_dir(maindir) if cache_dir is not None: library.use_cache_dir(cache_dir) ndp = library.load_ndp(model_name) basename = model_name if make or (plot and imp): ndp_labelled = get_labelled_version(ndp) else: ndp_labelled = ndp basename, dp = solve_get_dp_from_ndp(basename=basename, ndp=ndp_labelled, lower=lower, upper=upper) F = dp.get_fun_space() R = dp.get_res_space() UR = UpperSets(R) query = " ".join(query_strings) c = library.parse_constant(query) tu = get_types_universe() try: tu.check_leq(c.unit, F) except NotLeq as e: msg = 'The value given cannot be converted to functionality space.' raise_wrapped(UserError, e, msg, unit=c.unit, F=F, compact=True) fg = express_value_in_isomorphic_space(c.unit, c.value, F) logger.info('query: %s' % F.format(fg)) tracer = Tracer(logger=logger) res, trace = solve_meat_solve_ftor(tracer, ndp, dp, fg, intervals, max_steps, _exp_advanced) nres = len(res.minimals) if expect_nres is not None: if nres != expect_nres: msg = 'Found wrong number of resources' raise_desc(ExpectationsNotMet, msg, expect_nres=expect_nres, nres=nres) if imp: M = dp.get_imp_space() nimplementations = 0 for r in res.minimals: ms = dp.get_implementations_f_r(fg, r) nimplementations += len(ms) s = 'r = %s ' % R.format(r) for j, m in enumerate(ms): # print('m = %s' % str(m)) s += "\n implementation %d of %d: m = %s " % (j + 1, len(ms), M.format(m)) if make: imp_dict = get_imp_as_recursive_dict( M, m) # , ignore_hidden=False) print('imp dict: %r' % imp_dict) context = {} artifact = ndp_make(ndp, imp_dict, context) print('artifact: %s' % artifact) tracer.log(s) if expect_nimp is not None: if expect_nimp != nimplementations: msg = 'Found wrong number of implementations' raise_desc(ExpectationsNotMet, msg, expect_nimp=expect_nimp, nimplementations=nimplementations) # if expect_res is not None: # value = interpret_string(expect_res) # tracer.log('value: %s' % value) # res_expected = value.value # tu = get_types_universe() # # If it's a tuple of two elements, then we assume it's upper/lower bounds # if isinstance(value.unit, PosetProduct): # subs = value.unit.subs # assert len(subs) == 2, subs # # lower_UR_expected, upper_UR_expected = subs # lower_res_expected, upper_res_expected = value.value # # lower_bound = tu.get_embedding(lower_UR_expected, UR)[0](lower_res_expected) # upper_bound = tu.get_embedding(upper_UR_expected, UR)[0](upper_res_expected) # # tracer.log('lower: %s <= %s' % (UR.format(lower_bound), UR.format(res))) # tracer.log('upper: %s <= %s' % (UR.format(upper_bound), UR.format(res))) # # UR.check_leq(lower_bound, res) # UR.check_leq(res, upper_bound) # else: # # only one element: equality # UR_expected = value.unit # tu.check_leq(UR_expected, UR) # A_to_B, _B_to_A = tu.get_embedding(UR_expected, UR) # # res_expected_f = A_to_B(res_expected) # try: # UR.check_equal(res, res_expected_f) # except NotEqual as e: # raise_wrapped(ExpectationsNotMet, e, 'res is different', # res=res, res_expected=res_expected, compact=True) if plot: r = Report() if _exp_advanced: from mcdp_report.generic_report_utils import generic_report generic_report(r, dp, trace, annotation=None, axis0=(0, 0, 0, 0)) else: f = r.figure() from mcdp_report.generic_report_utils import generic_plot generic_plot(f, space=UR, value=res) from mcdp_report.generic_report_utils import generic_report_trace generic_report_trace(r, ndp, dp, trace, out, do_movie=do_movie) out_html = os.path.join(out, 'report.html') logger.info('writing to %r' % out_html) r.to_html(out_html) if plot and imp: from mcdp_report_ndp_tests.test1 import GetValues from mcdp_report.gg_ndp import gvgen_from_ndp from mcdp_report.gdc import STYLE_GREENREDSYM from mcdp_report.gg_utils import gg_figure M = dp.get_imp_space() report_solutions = Report() for i, r in enumerate(res.minimals): ms = dp.get_implementations_f_r(fg, r) for j, m in enumerate(ms): imp_dict = get_imp_as_recursive_dict(M, m) images_paths = library.get_images_paths() image_source = ImagesFromPaths(images_paths) gv = GetValues(ndp=ndp, imp_dict=imp_dict, nu=upper, nl=1) setattr(ndp, '_hack_force_enclose', True) with report_solutions.subsection('sol-%s-%s' % (i, j)) as rr: # Left right gg = gvgen_from_ndp(ndp=ndp, style=STYLE_GREENREDSYM, image_source=image_source, plotting_info=gv, direction='LR') gg_figure(rr, 'figure', gg, do_png=True, do_pdf=True, do_svg=False, do_dot=False) # Top-bottom gg = gvgen_from_ndp(ndp=ndp, style=STYLE_GREENREDSYM, image_source=image_source, plotting_info=gv, direction='TB') gg_figure(rr, 'figure2', gg, do_png=True, do_pdf=True, do_svg=False, do_dot=False) out_html = os.path.join(out, 'report_solutions.html') logger.info('writing to %r' % out_html) report_solutions.to_html(out_html)
def plot_different_solutions(libname, ndpname, query, out, upper=None): if not os.path.exists(out): os.makedirs(out) library = get_test_library(libname) #library.use_cache_dir(os.path.join(out, 'cache')) context = Context() ndp = library.load_ndp(ndpname, context) context = library._generate_context_with_hooks() ndp_labelled = get_labelled_version(ndp) dp0 = ndp_labelled.get_dp() if upper is not None: _, dpU = get_dp_bounds(dp0, nl=1, nu=upper) dp = dpU else: dp = dp0 M = dp.get_imp_space() with open(os.path.join(out, 'ndp.txt'), 'w') as f: f.write(ndp.repr_long()) with open(os.path.join(out, 'M.txt'), 'w') as f: f.write(M.repr_long()) with open(os.path.join(out, 'dp.txt'), 'w') as f: f.write(dp.repr_long()) with open(os.path.join(out, 'dp0.txt'), 'w') as f: f.write(dp0.repr_long()) f = convert_string_query(ndp=ndp, query=query, context=context) report = Report() res = dp.solve(f) print('num solutions: %s' % len(res.minimals)) for ri, r in enumerate(res.minimals): ms = dp.get_implementations_f_r(f, r) for j, m in enumerate(ms): imp_dict = get_imp_as_recursive_dict(M, m) logger.info(imp_dict) images_paths = library.get_images_paths() image_source = ImagesFromPaths(images_paths) gv = GetValues(ndp=ndp, imp_dict=imp_dict, nu=upper, nl=1) gg = gvgen_from_ndp(ndp=ndp, style=STYLE_GREENREDSYM, image_source=image_source, plotting_info=gv) with report.subsection('%s-%s' % (ri, j)) as rr: gg_figure(rr, 'figure', gg, do_png=True, do_pdf=False, do_svg=False, do_dot=False) fn = os.path.join(out, 'solutions.html') print('writing to %s' % fn) report.to_html(fn)