def instance_comptests_jobs2_m(context, module_name, create_reports): from .registrar import jobs_registrar_simple is_first = '.' not in module_name warn_errors = is_first try: module = import_name(module_name) except ValueError as e: msg = 'Could not import module %r' % module_name if warn_errors: logger.error(msg) raise_wrapped(Exception, e, msg) assert False fname = CompTests.hook_name if not fname in module.__dict__: msg = 'Module %s does not have function %s().' % (module_name, fname) if warn_errors: logger.debug(msg) else: ff = module.__dict__[fname] context.comp_dynamic(comptests_jobs_wrap, ff, job_id=module_name) jobs_registrar_simple(context, only_for_module=module_name)
def check_solve_r_chain(id_dp, dp): with primitive_dp_test(id_dp, dp): from mcdp_posets.utils import poset_check_chain R = dp.get_res_space() F = dp.get_fun_space() LF = LowerSets(F) r_chain = R.get_test_chain(n=8) poset_check_chain(R, r_chain) try: lfchain = map(dp.solve_r, r_chain) except NotSolvableNeedsApprox: return try_with_approximations(id_dp, dp, check_solve_r_chain) try: # now, notice that we need to reverse this lfchain_reversed = list(reversed(lfchain)) poset_check_chain(LF, lfchain_reversed) except ValueError as e: msg = 'The map solve_r() for %r is not monotone.' % id_dp raise_wrapped(Exception, e, msg, r_chain=r_chain, lfchain=lfchain, lfchain_reversed=lfchain_reversed, compact=True)
def eval_lfunction_invmult_sort_ops(ops, context, wants_constant): """ Divides in resources and constants Returns functions, constants """ from .eval_constant_imp import eval_constant constants = [] functions = [] for op in ops: try: x = eval_constant(op, context) check_isinstance(x, ValueWithUnits) constants.append(x) continue except NotConstant as e: pass except DPSemanticError as e: if 'Variable ref' in str(e): pass else: raise if wants_constant: msg = 'Product not constant because one op is not constant.' raise_wrapped(NotConstant, e, msg, op=op) x = eval_lfunction(op, context) assert isinstance(x, CFunction) functions.append(x) return functions, constants
def eval_codespec(r, expect): assert isinstance(r, (CDP.CodeSpecNoArgs, CDP.CodeSpec)) function = r.function.value if isinstance(r, CDP.CodeSpec): _args, kwargs = eval_arguments(r.arguments) else: kwargs = {} check_isinstance(function, str) try: res = instantiate_spec([function, kwargs]) except InstantiationException as e: msg = 'Could not instantiate code spec.' raise_wrapped(DPSemanticError, e, msg, compact=True, function=function, kwargs=kwargs) try: check_isinstance(res, expect) except ValueError as e: msg = 'The code did not return the correct type.' raise_wrapped(DPSemanticError, e, msg, r=r, res=res, expect=expect) return res
def make_list(x, where=None): if x is None: raise ValueError() # if where is None: # raise ValueError() try: if not len(x): return list_types[0](dummy='dummy', where=where) ltype = list_types[len(x)] w1 = x[0].where w2 = x[-1].where if w1 is None or w2 is None: raise_desc(ValueError, 'Cannot create list', x=x) assert w2.character_end is not None w3 = Where(string=w1.string, character=w1.character, character_end=w2.character_end) res = ltype(*tuple(x), where=w3) return res except BaseException as e: msg = 'Cannot create list' raise_wrapped(DPInternalError, e, msg, x=x, where=where, x_last=x[-1])
def eval_lfunction_invplus_sort_ops(ops, context, wants_constant): """ pos_constants, neg_constants, functions = sort_ops(ops, context) """ from .eval_constant_imp import eval_constant pos_constants = [] neg_constants = [] functions = [] for op in ops: try: x = eval_constant(op, context) check_isinstance(x, ValueWithUnits) if isinstance(x.unit, (RcompUnits, Rcomp, Nat)): pos_constants.append(x) elif isinstance(x.unit, RbicompUnits): neg_constants.append(x) else: msg = 'Invalid addition - needs error' raise_desc(DPInternalError, msg, x=x) except NotConstant as e: if wants_constant: msg = 'Sum not constant because one op is not constant.' raise_wrapped(NotConstant, e, msg, op=op) x = eval_lfunction(op, context) assert isinstance(x, CFunction) functions.append(x) return pos_constants, neg_constants, functions
def eval_solve_r(op, context): check_isinstance(op, CDP.SolveRModel) from .eval_ndp_imp import eval_ndp ndp = eval_ndp(op.model, context) dp = ndp.get_dp() r0 = eval_constant(op.r, context) F = dp.get_fun_space() R = dp.get_res_space() tu = get_types_universe() try: tu.check_leq(r0.unit, R) except NotLeq as e: msg = 'Input not correct.' raise_wrapped(DPSemanticError, e, msg, compact=True) r = r0.cast_value(R) res = dp.solve_r(r) try: LF = LowerSets(F) return ValueWithUnits(res, LF) except NotBelongs as e: msg = 'Invalid result of solve_r().' raise_desc(DPInternalError, msg, res=res, dp=dp.repr_long())
def eval_constant_SimpleValue(op, context): from .eval_space_imp import eval_space # @Reimport F = eval_space(op.space, context) assert isinstance(F, Space), op assert isinstance(F, RcompUnits) v = op.value.value # promote integer to float if isinstance(v, int) and isinstance(F, (Rcomp, RcompUnits)): v = float(v) if v < 0: if isinstance(F, RcompUnits): F = RbicompUnits(F.units, F.string) else: msg = 'Negative %s not implemented yet.' % F raise_desc(NotImplementedError, msg, F=F) try: F.belongs(v) except NotBelongs as e: msg = 'Not in space' raise_wrapped(DPSemanticError, e, msg, F=F, v=v, op=op) return ValueWithUnits(unit=F, value=v)
def _call(self, x): try: r = self.F.meet(x, self.value) except NotJoinable as e: msg = 'Cannot compute meet of elements.' raise_wrapped(MapNotDefinedHere, e, msg, value=self.value, x=x) return r
def dp_transform(dp, f): """ Recursive application of a map f that is equivariant with series and parallel operations. """ from .dp_series import Series0 from .dp_parallel import Parallel from .dp_series_simplification import check_same_spaces if isinstance(dp, Series0): return Series0(dp_transform(dp.dp1, f), dp_transform(dp.dp2, f)) elif isinstance(dp, Parallel): return Parallel(dp_transform(dp.dp1, f), dp_transform(dp.dp2, f)) elif isinstance(dp, ParallelN): dps = tuple(dp_transform(_, f) for _ in dp.dps) return ParallelN(dps) elif isinstance(dp, CoProductDPLabels): return CoProductDPLabels(dp_transform(dp.dp, f), dp.labels) elif isinstance(dp, CoProductDP): dps2 = tuple(dp_transform(_, f) for _ in dp.dps) return CoProductDP(dps2) elif isinstance(dp, DPLoop2): return DPLoop2(dp_transform(dp.dp1, f)) elif isinstance(dp, OpaqueDP): return OpaqueDP(dp_transform(dp.dp, f)) elif isinstance(dp, LabelerDP): return LabelerDP(dp_transform(dp.dp, f), dp.recname) else: dp2 = f(dp) try: check_same_spaces(dp, dp2) except AssertionError as e: # pragma: no cover msg = 'Transformation %s does not preserve spaces.' % f raise_wrapped(DPInternalError, e, msg, dp=dp, dp2=dp2, f=f, compact=True) return dp2
def make_list(x, where=None): if x is None: raise ValueError() # if where is None: # raise ValueError() try: if not len(x): return list_types[0](dummy='dummy', where=where) ltype = list_types[len(x)] w1 = x[0].where w2 = x[-1].where if w1 is None or w2 is None: raise_desc(ValueError, 'Cannot create list', x=x) assert w2.character_end is not None w3 = Where(string=w1.string, character=w1.character, character_end=w2.character_end) res = ltype(*tuple(x), where=w3) return res except BaseException as e: msg = 'Cannot create list' raise_wrapped(DPInternalError, e, msg, x=x, where=where, x_last=x[-1])
def abstract(self): if not self.context.names: # this means that there are nor children, nor functions nor resources dp = Mux(PosetProduct(()), ()) ndp = SimpleWrap(dp, fnames=[], rnames=[]) return ndp try: self.check_fully_connected() except NotConnected as e: msg = 'Cannot abstract because not all subproblems are connected.' raise_wrapped(DPSemanticError, e, msg, exc=sys.exc_info(), compact=True) from mocdp.comp.composite_abstraction import cndp_abstract res = cndp_abstract(self) assert isinstance(res, SimpleWrap), type(res) assert res.get_fnames() == self.context.fnames assert res.get_rnames() == self.context.rnames return res
def disk_event_interpret(disk_rep, disk_event): fs = { DiskEvents.disk_event_group: disk_event_disk_event_group_interpret, DiskEvents.dir_create: disk_event_dir_create_interpret, DiskEvents.dir_rename: disk_event_dir_rename_interpret, DiskEvents.dir_delete: disk_event_dir_delete_interpret, DiskEvents.file_create: disk_event_file_create_interpret, DiskEvents.file_modify: disk_event_file_modify_interpret, DiskEvents.file_delete: disk_event_file_delete_interpret, DiskEvents.file_rename: disk_event_file_rename_interpret, } ename = disk_event['operation'] if not ename in fs: raise NotImplementedError(ename) intf = fs[ename] arguments = disk_event['arguments'] try: logger.info('%s %s' % (ename, arguments)) intf(disk_rep=disk_rep, **arguments) except Exception as e: msg = 'Could not complete the replay of this event: \n' msg += indent(yaml_dump(disk_event), 'disk_event: ') msg += '\nFor this tree:\n' msg += indent((disk_rep.tree()), ' disk_rep: ') from mcdp_hdb.memdataview import InvalidOperation raise_wrapped(InvalidOperation, e, msg)
def get_repo_information(repo_root): """ Returns a dictionary with fields branch, commit, org, repo Raises RepoInfoException. """ gitrepo = Repo(repo_root) try: try: branch = gitrepo.active_branch except TypeError: # TypeError: HEAD is a detached symbolic reference as it points # to '4bcaf737955277b156a5bacdd80d1805e4b8bb25' branch = None commit = gitrepo.head.commit.hexsha try: origin = gitrepo.remotes.origin except AttributeError: raise ValueError('No remote "origin".') url = origin.url except ValueError as e: msg = 'Could not get branch, commit, url. Maybe the repo is not initialized.' raise_wrapped(RepoInfoException, e, msg, compact=True) # now github can use urls that do not end in '.git' if 'github' in url and not url.endswith('.git'): url = url + '.git' try: org, repo = org_repo_from_url(url) except NotImplementedError: org, repo = None, None return dict(branch=branch, commit=commit, org=org, repo=repo)
def _call(self, x): try: r = self.F.meet(x, self.value) except NotJoinable as e: msg = 'Cannot compute meet of elements.' raise_wrapped(MapNotDefinedHere, e, msg, value=self.value, x=x) return r
def get_implementations_f_r(self, f, r): f1, f2 = f r1, r2 = r _, pack, _ = self._get_product() m1s = self.dp1.get_implementations_f_r(f1, r1) m2s = self.dp2.get_implementations_f_r(f2, r2) options = set() if do_extra_checks(): for m1 in m1s: self.M1.belongs(m1) try: for m2 in m2s: self.M2.belongs(m2) except NotBelongs as e: msg = ' Invalid result from dp2' raise_wrapped(NotBelongs, e, msg, dp2=self.dp2.repr_long()) for m1 in m1s: for m2 in m2s: m = pack(m1, m2) options.add(m) if do_extra_checks(): for _ in options: self.I.belongs(_) return options
def sum_units(Fs, values, R): ''' Might raise IncompatibleUnits ''' for Fi in Fs: check_isinstance(Fi, RcompUnits) res = 0.0 for Fi, x in zip(Fs, values): if is_top(Fi, x): return R.get_top() # reasonably sure this is correct... try: factor = 1.0 / float(R.units / Fi.units) except pint_DimensionalityError as e: # pragma: no cover (DimensionalityError) raise_wrapped(IncompatibleUnits, e, 'Pint cannot convert', Fs=Fs, R=R) try: res += factor * x except FloatingPointError as e: if 'overflow' in str(e): res = np.inf break else: raise if np.isinf(res): return R.get_top() return res
def x_minus_constants(x, constants): R0 = x.unit if not isinstance(R0, RcompUnits): msg = 'Cannot evaluate "-" on this space.' raise_desc(DPSemanticError, msg, R0=R0) Rb = RbicompUnits.from_rcompunits(R0) # convert each factor to R0 try: v0 = x.value for c in constants: vi = express_value_in_isomorphic_space(c.unit, c.value, Rb) v0 = RbicompUnits_subtract(Rb, v0, vi) except TypeError as e: msg = 'Failure to compute subtraction.' raise_wrapped(DPInternalError, e, msg, x=x, constants=constants) if Rb.leq(0.0, v0): R1 = R0 else: R1 = Rb return ValueWithUnits(unit=R1, value=v0)
def eval_rvalue_divide(op, context): from .eval_constant_imp import eval_constant ops = get_odd_ops(unwrap_list(op.ops)) try: c2 = eval_constant(ops[1], context) except NotConstant as e: msg = 'Cannot divide by a non-constant.' raise_wrapped(DPSemanticError, e, msg, ops[0]) c2_inv = inv_constant(c2) try: c1 = eval_constant(ops[0], context) # also the first one is a constant from .misc_math import generic_mult_constantsN c = generic_mult_constantsN([c1, c2_inv]) assert isinstance(c, ValueWithUnits) return get_valuewithunits_as_resource(c, context) except NotConstant: pass # then eval as resource r = eval_rvalue(ops[0], context) res = get_mult_op(context, r=r, c=c2_inv) return res
def assert_parse_ndp_semantic_error(string, contains=None): """ Asserts that parsing this string as an NDP will raise a DPSemanticError. If contains is not None, it is a substring that must be contained in the error. Returns the exception. """ try: res = parse_ndp(string) except DPSemanticError as e: if contains is not None: s = str(e) if not contains in s: msg = 'Expected a DPSemanticError with substring %r.' % contains raise_wrapped(TestFailed, e, msg, string=string) return e else: return e except BaseException as e: msg = 'Expected DPSemanticError, but obtained %s.' % type(e) raise_wrapped(TestFailed, e, msg, string=string) msg = 'Expected DPSemanticError, but no exception was thrown.' raise_desc(TestFailed, msg, string=string, result=res) assert False
def eval_PlusN_sort_ops(ops, context, wants_constant): """ pos_constants, neg_constants, resources = sort_ops(ops, context) """ from .eval_constant_imp import eval_constant pos_constants = [] neg_constants = [] resources = [] for op in ops: try: x = eval_constant(op, context) check_isinstance(x, ValueWithUnits) if isinstance(x.unit, (RcompUnits, Rcomp, Nat)): pos_constants.append(x) elif isinstance(x.unit, RbicompUnits): neg_constants.append(x) else: msg = 'Cannot use the type %s in a sum.' % x.unit raise_desc(DPInternalError, msg, x=x) except NotConstant as e: if wants_constant: msg = 'Sum not constant because one op is not constant.' raise_wrapped(NotConstant, e, msg, op=op) x = eval_rvalue(op, context) assert isinstance(x, CResource) resources.append(x) return pos_constants, neg_constants, resources
def belongs(self, x): try: if not isinstance(x, tuple) or not len(x) == 2: raise NotBelongs() n = len(self.spaces) i, s = x if not isinstance(i, int) or not 0 <= i <= n - 1: raise NotBelongs() if not isinstance(s, tuple) or not len(s) == n: raise NotBelongs() for j, sj in enumerate(s): if j == i: try: self.spaces[j].belongs(sj) except NotBelongs as e: msg = 'Element %d' % j raise_wrapped(NotBelongs, e, msg, j=j, sj=sj, spacej=self.spaces[j]) else: if not sj == Coproduct1.fill: msg = 'sj is not fill' raise_desc(NotBelongs, msg, sj=sj) except NotBelongs as e: msg = '' raise_wrapped(NotBelongs, e, msg, x=x)
def eval_codespec(r, expect): assert isinstance(r, (CDP.CodeSpecNoArgs, CDP.CodeSpec)) function = r.function.value if isinstance(r, CDP.CodeSpec): _args, kwargs = eval_arguments(r.arguments) else: kwargs = {} check_isinstance(function, str) try: res = instantiate_spec([function, kwargs]) except InstantiationException as e: msg = 'Could not instantiate code spec.' raise_wrapped(DPSemanticError, e, msg, compact=True, function=function, kwargs=kwargs) try: check_isinstance(res, expect) except ValueError as e: msg = 'The code did not return the correct type.' raise_wrapped(DPSemanticError, e, msg, r=r, res=res, expect=expect) return res
def belongs(self, x): try: if not isinstance(x, tuple) or not len(x) == 2: raise NotBelongs() n = len(self.spaces) label, s = x if not label in self.labels: msg = 'Unknown label.' raise_desc(NotBelongs, msg, label=label, labels=self.labels) if not isinstance(s, tuple) or not len(s) == n: raise NotBelongs() i = self.labels.index(label) for j, sj in enumerate(s): if j == i: try: self.spaces[j].belongs(sj) except NotBelongs as e: msg = 'Element %d' % j raise_wrapped(NotBelongs, e, msg, j=j, sj=sj, spacej=self.spaces[j]) else: if not sj == Coproduct1.fill: msg = 'sj is not fill' raise_desc(NotBelongs, msg, sj=sj) except NotBelongs as e: msg = '' raise_wrapped(NotBelongs, e, msg, x=x)
def parse_wrap_semantic_error(string, expr, contains=None): """ Assert semantic error. If contains is not None, it is a substring that must be contained in the error. Returns the exception. """ if isinstance(expr, ParsingElement): expr = expr.get() try: res = parse_wrap(expr, string)[0] # note the 0, first element except DPSemanticError as e: if contains is not None: s = str(e) if not contains in s: msg = 'Expected a DPSemanticError with substring %r.' % contains raise_wrapped(TestFailed, e, msg, expr=find_parsing_element(expr), string=string) return e else: return e except BaseException as e: msg = 'Expected DPSemanticError, but obtained %s.' % type(e) raise_wrapped(TestFailed, e, msg, expr=find_parsing_element(expr), string=string) msg = 'Expected DPSemanticError, but no except was thrown.' raise_desc(TestFailed, msg, expr=find_parsing_element(expr), string=string, result=res) assert False
def assert_parse_ndp_semantic_error(string, contains=None): """ Asserts that parsing this string as an NDP will raise a DPSemanticError. If contains is not None, it is a substring that must be contained in the error. Returns the exception. """ try: res = parse_ndp(string) except DPSemanticError as e: if contains is not None: s = str(e) if not contains in s: msg = 'Expected a DPSemanticError with substring %r.' % contains raise_wrapped(TestFailed, e, msg, string=string) return e else: return e except BaseException as e: msg = 'Expected DPSemanticError, but obtained %s.' % type(e) raise_wrapped(TestFailed, e, msg, string=string) msg = 'Expected DPSemanticError, but no exception was thrown.' raise_desc(TestFailed, msg, string=string, result=res) assert False
def bb(tokens, loc, s): where = Where(s, loc) try: try: res = b(tokens) except TypeError as e: ttokens = list(tokens) s = "\n".join("- %s " % str(x) for x in ttokens) msg = 'Cannot invoke %r\nwith %d tokens:\n%s.' % ( b, len(ttokens), s) raise_wrapped(TypeError, e, msg) except DPSyntaxError as e: if e.where is None: e.where = where raise DPSyntaxError(str(e), where=where) else: raise except DPSemanticError as e: if e.where is None: raise DPSemanticError(str(e), where=where) else: raise except BaseException as e: raise_wrapped(DPInternalError, e, "Error while parsing.", where=where.__str__(), tokens=tokens) if isnamedtupleinstance(res) and res.where is None: res = get_copy_with_where(res, where=where) return res
def execute(self, dp1, dp2): """ Returns the simplified version. """ # check that everything is correct dp0 = Parallel(dp1, dp2) try: res = self._execute(dp1, dp2) except BaseException as e: # pragma: no cover msg = 'Error while executing Parallel simplification rule %s.' % type( self).__name__ raise_wrapped(DPInternalError, e, msg, dp1=dp1.repr_long(), dp2=dp2.repr_long(), rule=self) try: from mcdp_dp.dp_series_simplification import check_same_spaces check_same_spaces(dp0, res) except AssertionError as e: # pragma: no cover msg = 'Invalid Parallel simplification rule %s.' % type( self).__name__ raise_wrapped(DPInternalError, e, msg, dp1=dp1.repr_long(), dp2=dp2.repr_long(), res=res.repr_long(), rule=self) return res
def eval_lfunction_invmult_sort_ops(ops, context, wants_constant): """ Divides in resources and constants Returns functions, constants """ from .eval_constant_imp import eval_constant constants = [] functions = [] for op in ops: try: x = eval_constant(op, context) check_isinstance(x, ValueWithUnits) constants.append(x) continue except NotConstant as e: pass except DPSemanticError as e: if 'Variable ref' in str(e): pass else: raise if wants_constant: msg = 'Product not constant because one op is not constant.' raise_wrapped( NotConstant, e, msg, # op=op, compact=True) x = eval_lfunction(op, context) assert isinstance(x, CFunction) functions.append(x) return functions, constants
def get_resource_possibly_converted(r, P, context): """ Returns a resource possibly converted to the space P """ assert isinstance(r, CResource) R = context.get_rtype(r) tu = get_types_universe() if tu.equal(R, P): return r else: try: tu.get_super_conversion(R, P) except NotLeq as e: msg = 'Cannot convert %s to %s.' % (R, P) raise_wrapped(DPSemanticError, e, msg, R=R, P=P, exc=sys.exc_info()) conversion = get_conversion(R, P) if conversion is None: return r else: r2 = create_operation(context, conversion, [r], name_prefix='_conv_grpc', op_prefix='_op', res_prefix='_res') return r2
def __init__(self, F, R, unit, value): check_isinstance(F, RcompUnits) check_isinstance(R, RcompUnits) check_isinstance(unit, RcompUnits) try: check_mult_units_consistency(F, unit, R) except AssertionError as e: msg = 'Invalid units.' raise_wrapped(ValueError, e, msg, F=F, R=R, unit=unit) amap = MultValueMap(F=F, R=R, unit=unit, value=value) # if value = Top: # f |-> f * Top # if is_top(unit, value): amap_dual = MultValueDPHelper2Map(R, F) elif unit.equal(0.0, value): amap_dual = ConstantPosetMap(R, F, F.get_top()) else: value2 = 1.0 / value unit2 = inverse_of_unit(unit) amap_dual = MultValueMap(F=R, R=F, unit=unit2, value=value2) WrapAMap.__init__(self, amap, amap_dual)
def eval_solve_r(op, context): check_isinstance(op, CDP.SolveRModel) from .eval_ndp_imp import eval_ndp ndp = eval_ndp(op.model, context) dp = ndp.get_dp() r0 = eval_constant(op.r, context) F = dp.get_fun_space() R = dp.get_res_space() tu = get_types_universe() try: tu.check_leq(r0.unit, R) except NotLeq as e: msg = 'Input not correct.' raise_wrapped(DPSemanticError, e, msg, compact=True) r = r0.cast_value(R) res = dp.solve_r(r) try: LF = LowerSets(F) return ValueWithUnits(res, LF) except NotBelongs as e: msg = 'Invalid result of solve_r().' raise_desc(DPInternalError, msg, res=res, dp=dp.repr_long())
def event_interpret_(view, event): fs = { DataEvents.leaf_set: event_leaf_set_interpret, DataEvents.struct_set: event_struct_set_interpret, DataEvents.list_set: event_list_set_interpret, DataEvents.hash_set: event_hash_set_interpret, DataEvents.increment: event_increment_interpret, DataEvents.list_append: event_list_append_interpret, DataEvents.list_remove: event_list_remove_interpret, DataEvents.list_delete: event_list_delete_interpret, DataEvents.list_insert: event_list_insert_interpret, DataEvents.list_setitem: event_list_setitem_interpret, DataEvents.set_add: event_set_add_interpret, DataEvents.set_remove: event_set_remove_interpret, DataEvents.dict_setitem: event_dict_setitem_interpret, DataEvents.dict_delitem: event_dict_delitem_interpret, DataEvents.dict_rename: event_dict_rename_interpret, } ename = event['operation'] intf = fs[ename] arguments = event['arguments'] try: intf(view=view, **arguments) except Exception as e: msg = 'Could not complete the replay of this event: \n' msg += indent(yaml_dump(event), 'event: ') raise_wrapped(InvalidOperation, e, msg)
def solve_all(self, f1, trace): """ Returns an upperset in UR. You want to project it to R1 to use as the output. """ dp0 = self.dp1 R = dp0.get_res_space() R1 = R[0] UR = UpperSets(R) # we consider a set of iterates # we start from the bottom trace.log('Iterating in UR = %s' % UR.__str__()) s0 = R.Us(R.get_minimal_elements()) S = [ KleeneIteration(s=s0, s_converged=R.Us(set()), r=upperset_project(s0, 0), r_converged=R1.Us(set())) ] for i in range(1, 1000000): # XXX with trace.iteration(i) as t: si_prev = S[-1].s si_next, converged = solve_f_iterate(dp0, f1, R, si_prev, t) iteration = KleeneIteration(s=si_next, s_converged=converged, r=upperset_project(si_next, 0), r_converged=upperset_project( converged, 0)) S.append(iteration) t.log('R = %s' % UR.format(si_next)) if do_extra_checks(): try: UR.check_leq(si_prev, si_next) except NotLeq as e: msg = 'Loop iteration invariant not satisfied.' raise_wrapped(Exception, e, msg, si_prev=si_prev, si_next=si_next, dp=self.dp1) t.values(state=S[-1]) if UR.leq(si_next, si_prev): t.log('Breaking because converged (iteration %s) ' % i) #t.log(' solution is %s' % (UR.format(sip))) # todo: add reason why interrupted break trace.values(type='loop2', UR=UR, R=R, dp=self, iterations=S) res_all = S[-1].s res_r1 = upperset_project(res_all, 0) result = dict(res_all=res_all, res_r1=res_r1) return result
def eval_lfunction_invplus(lf, context): ops = get_odd_ops(unwrap_list(lf.ops)) pos_constants, neg_constants, functions = \ eval_lfunction_invplus_sort_ops(ops, context, wants_constant=False) if neg_constants: msg = 'Inverse plus of negative constants not implemented yet.' raise_desc(DPNotImplementedError, msg) constants = pos_constants try: if len(functions) == 0: c = plus_constantsN(constants) return get_valuewithunits_as_function(c, context) elif len(functions) == 1: if len(constants) > 0: c = plus_constantsN(constants) return get_invplus_op(context, functions[0], c) else: return functions[0] else: # there are some functions r = eval_lfunction_invplus_ops(functions, context) if not constants: return r else: c = plus_constantsN(constants) return get_invplus_op(context, r, c) except ConstantsNotCompatibleForAddition as e: msg = 'Incompatible units for addition.' raise_wrapped(DPSemanticError, e, msg, compact=True)
def get_nodejs_bin(): """ Raises NodeNotFound (XXX) """ tries = ['nodejs', 'node'] try: cmd = [tries[0], '--version'] _res = system_cmd_result(os.getcwd(), cmd, display_stdout=False, display_stderr=False, raise_on_error=True) return tries[0] # pragma: no cover except CmdException as e: try: cmd = [tries[1], '--version'] _res = system_cmd_result(os.getcwd(), cmd, display_stdout=False, display_stderr=False, raise_on_error=True) return tries[1] except CmdException as e: # pragma: no cover msg = 'Node.js executable "node" or "nodejs" not found.' msg += '\nOn Ubuntu, it can be installed using:' msg += '\n\n\tsudo apt-get install -y nodejs' raise_wrapped(PrerenderError, e, msg, compact=True)
def eval_constant_SimpleValue(op, context): from .eval_space_imp import eval_space # @Reimport F = eval_space(op.space, context) assert isinstance(F, Space), op assert isinstance(F, RcompUnits) v = op.value.value # promote integer to float if isinstance(v, int) and isinstance(F, (Rcomp, RcompUnits)): v = float(v) if v < 0: if isinstance(F, RcompUnits): F = RbicompUnits(F.units, F.string) else: msg = 'Negative %s not implemented yet.' % F raise_desc(NotImplementedError, msg, F=F) try: F.belongs(v) except NotBelongs as e: msg = 'Not in space' raise_wrapped(DPSemanticError, e, msg, F=F, v=v, op=op) return ValueWithUnits(unit=F, value=v)
def sum_units(Fs, values, R): for Fi in Fs: check_isinstance(Fi, RcompUnits) res = 0.0 for Fi, x in zip(Fs, values): if is_top(Fi, x): return R.get_top() # reasonably sure this is correct... try: factor = 1.0 / float(R.units / Fi.units) except Exception as e: # pragma: no cover (DimensionalityError) raise_wrapped(Exception, e, 'some error', Fs=Fs, R=R) try: res += factor * x except FloatingPointError as e: if 'overflow' in str(e): res = np.inf break else: raise if np.isinf(res): return R.get_top() return res
def checkout_repository(tmpdir, org, repo, branch): if branch is None: branch = 'master' path = os.path.join(tmpdir, org, repo, branch) url = '[email protected]:%s/%s.git' % (org, repo) url = 'https://github.com/%s/%s.git' % (org, repo) try: if not os.path.exists(path): checkout(path, url, branch) else: m = os.path.getmtime(path) age = time.time() - m if age < 10 * 60: pass # msg = 'Do not checkout repo if young.' # logger.debug(msg) else: # msg = 'Checkout repo of age %s.' % age # logger.debug(msg) repo = Repo(path) try: repo.remotes.origin.pull() os.utime(path, None) except: pass return path except GitCommandError as e: msg = 'Could not checkout repository %s/%s' % (org, repo) msg += '\n using url %s' % url raise_wrapped(CouldNotCheckoutRepo, e, msg, compact=True)
def get_function_possibly_converted(cf, P, context): """ Returns a resource possibly converted to the space P """ check_isinstance(cf, CFunction) F = context.get_ftype(cf) tu = get_types_universe() if tu.equal(F, P): return cf else: try: tu.check_leq(P, F) except NotLeq as e: msg = 'Cannot convert %s to %s.' % (P, F) raise_wrapped(DPSemanticError, e, msg,P=P, F=F) conversion = get_conversion(P, F) if conversion is None: return cf else: cf2 = create_operation_lf(context, dp=conversion, functions=[cf], name_prefix='_conv_gfpc', op_prefix='_op', res_prefix='_res') return cf2
def belongs(self, x): try: if not isinstance(x, tuple) or not len(x) == 2: raise NotBelongs() n = len(self.spaces) i, s = x if not isinstance(i, int) or not 0 <= i <= n - 1: raise NotBelongs() if not isinstance(s, tuple) or not len(s) == n: raise NotBelongs() for j, sj in enumerate(s): if j == i: try: self.spaces[j].belongs(sj) except NotBelongs as e: msg = 'Element %d' % j raise_wrapped(NotBelongs, e, msg, j=j, sj=sj, spacej=self.spaces[j]) else: if not sj == Coproduct1.fill: msg = 'sj is not fill' raise_desc(NotBelongs, msg, sj=sj) except NotBelongs as e: msg = '' raise_wrapped(NotBelongs, e, msg, x=x)
def parse_pint(s0): """ thin wrapper taking care of dollars not recognized """ check_isinstance(s0, str) replacements = { '$': ' dollars ', '¹': '^1', '²': '^2', '³': '^3', '⁴': '^4', '⁵': '^5', '⁶': '^6', '⁷': '^7', '⁸': '^8', '⁹': '^9', } s = s0 for p, replacement in replacements.items(): check_isinstance(p, str) check_isinstance(replacement, str) s = s.replace(p, replacement) ureg = get_ureg() try: return ureg.parse_expression(s) except UndefinedUnitError as e: msg = 'Cannot parse units %r: %s.' % (s0, str(e)) raise_desc(DPSemanticError, msg) except SyntaxError as e: msg = 'Cannot parse units %r.' % s0 raise_wrapped(DPSemanticError, e, msg, compact=True, exc=sys.exc_info()) # ? for some reason compact does not have effect here except Exception as e: msg = 'Cannot parse units %r (%s).' % (s0, type(e)) raise_wrapped(DPSemanticError, e, msg, compact=True, exc=sys.exc_info())
def belongs(self, x): try: if not isinstance(x, tuple) or not len(x) == 2: raise NotBelongs() n = len(self.spaces) label, s = x if not label in self.labels: msg = 'Unknown label.' raise_desc(NotBelongs, msg, label=label, labels=self.labels) if not isinstance(s, tuple) or not len(s) == n: raise NotBelongs() i = self.labels.index(label) for j, sj in enumerate(s): if j == i: try: self.spaces[j].belongs(sj) except NotBelongs as e: msg = 'Element %d' % j raise_wrapped(NotBelongs, e, msg, j=j, sj=sj, spacej=self.spaces[j]) else: if not sj == Coproduct1.fill: msg = 'sj is not fill' raise_desc(NotBelongs, msg, sj=sj) except NotBelongs as e: msg = '' raise_wrapped(NotBelongs, e, msg, x=x)
def get_value_from_impdict(imp_dict, ndp_name): assert isinstance(ndp_name, tuple), ndp_name if not ndp_name: return imp_dict k = ndp_name[0] # isitf, is_fname = is_fun_node_name(k) # if isitf: # use = is_fname # else: # isitr, is_rname = is_res_node_name(k) # if isitr: # use = is_rname # else: # use = k use = k if not use in imp_dict: msg = 'get_value_from_impdict: Expected to see key %r among %s' % (use, list(imp_dict)) raise_desc(ValueMissing, msg) sub = imp_dict[use] try: return get_value_from_impdict(sub, ndp_name[1:]) except ValueMissing as e: msg = 'get_value_from_impdict: Cannot find value for %s.' % ndp_name.__str__() raise_wrapped(ValueMissing, e, msg, imp_dict=imp_dict, compact=True)
def __init__(self, F, R, unit, value): check_isinstance(F, RcompUnits) check_isinstance(R, RcompUnits) check_isinstance(unit, RcompUnits) try: check_mult_units_consistency(F, unit, R) except AssertionError as e: msg = 'Invalid units.' raise_wrapped(ValueError, e, msg, F=F, R=R, unit=unit) amap = MultValueMap(F=F, R=R, unit=unit, value=value) # if value = Top: # f |-> f * Top # if is_top(unit, value): amap_dual = MultValueDPHelper2Map(R, F) elif unit.equal(0.0, value): amap_dual = ConstantPosetMap(R, F, F.get_top()) else: value2 = 1.0 / value unit2 = inverse_of_unit(unit) amap_dual = MultValueMap(F=R, R=F, unit=unit2, value=value2) WrapAMap.__init__(self, amap, amap_dual)
def get_implementations_f_r(self, f, r): f1, f2 = f r1, r2 = r _, pack, _ = self._get_product() m1s = self.dp1.get_implementations_f_r(f1, r1) m2s = self.dp2.get_implementations_f_r(f2, r2) options = set() if do_extra_checks(): for m1 in m1s: self.M1.belongs(m1) try: for m2 in m2s: self.M2.belongs(m2) except NotBelongs as e: msg = ' Invalid result from dp2' raise_wrapped(NotBelongs, e, msg, dp2=self.dp2.repr_long()) for m1 in m1s: for m2 in m2s: m = pack(m1, m2) options.add(m) if do_extra_checks(): for _ in options: self.I.belongs(_) return options
def check_connections(name2dp, connections): for c in connections: if not c.dp1 in name2dp: msg = 'Refers to unknown dp %r (known %r).' % (c.dp1, set(name2dp)) raise ValueError(msg) try: ndp1 = name2dp[c.dp1] ndp1.rindex(c.s1) except ValueError as e: raise_wrapped(ValueError, e, 'Unknown signal.', s1=c.s1, c=c, ndp1=ndp1) if not c.dp2 in name2dp: msg = 'Refers to unknown dp %r (known %r).' % (c.dp2, set(name2dp)) raise ValueError(msg) try: ndp2 = name2dp[c.dp2] ndp2.findex(c.s2) except ValueError as e: raise_wrapped(ValueError, e, 'Unknown signal.', s2=c.s2, c=c, ndp2=ndp2)
def eval_lfunction_invplus_sort_ops(ops, context, wants_constant): """ pos_constants, neg_constants, functions = sort_ops(ops, context) """ from .eval_constant_imp import eval_constant pos_constants = [] neg_constants = [] functions = [] for op in ops: try: x = eval_constant(op, context) check_isinstance(x, ValueWithUnits) if isinstance(x.unit, (RcompUnits, Rcomp, Nat)): pos_constants.append(x) elif isinstance(x.unit, RbicompUnits): neg_constants.append(x) else: msg = 'Invalid addition - needs error' raise_desc(DPInternalError, msg, x=x) except NotConstant as e: if wants_constant: msg = 'Sum not constant because one op is not constant.' raise_wrapped(NotConstant, e, msg, op=op, compact=True) x = eval_lfunction(op, context) assert isinstance(x, CFunction) functions.append(x) return pos_constants, neg_constants, functions
def check_get_id_indices(a, res): got = get_id_indices(a) if got != res: msg = 'Result is different' raise_desc(ValueError, msg, a=a, res=res, got=got) # now compute the composition from mcdp_posets.poset_product import PosetProduct if isinstance(a, PosetProduct): reducel = PosetProduct else: reducel = list try: a2 = get_it(a, got, reducel) except Exception as e: raise_wrapped(ValueError, e, "invalid index produced", a=a, res=res, got=got) if str(a2) != str(a): msg = 'Not invertible' raise_desc(ValueError, msg, a=a, res=res, got=got, a2=a2)
def __init__(self, ndps, labels=None): from mcdp_posets.types_universe import get_types_universe if not isinstance(ndps, tuple) or not len(ndps) >= 1: raise_desc(ValueError, 'Expected a nonempty tuple.', ndps=ndps) if labels is not None: if not isinstance(labels, tuple) or not len(labels) == len(ndps): raise_desc(ValueError, 'Need labels to be consistent', ndps=ndps, labels=labels) self.labels = labels tu = get_types_universe() first = ndps[0] ftypes = first.get_ftypes(first.get_fnames()) rtypes = first.get_rtypes(first.get_rnames()) for i, ndp in enumerate(ndps): ftypes_i = ndp.get_ftypes(ndp.get_fnames()) rtypes_i = ndp.get_rtypes(ndp.get_rnames()) name = 'model #%d' % i if not self.labels else self.labels[i].__repr__() try: tu.check_equal(ftypes, ftypes_i) except NotEqual as e: msg = 'Cannot create co-product: ftypes of %s do not match the first.' % name raise_wrapped(ValueError, e, msg, ftypes=ftypes, ftypes_i=ftypes_i) try: tu.check_equal(rtypes, rtypes_i) except NotEqual as e: msg = 'Cannot create co-product: rtypes of %s not match the first.' % name raise_wrapped(ValueError, e, msg, rtypes=rtypes, rtypes_i=rtypes_i) self.ndps = ndps
def __init__(self, constructor, **kwargs): try: self.dpf = constructor(**kwargs) except TypeError as e: msg = 'Could not instance %s with params %s' %\ (constructor, kwargs) raise_wrapped(TypeError, e, msg)
def substitute_command(s, name, sub): """ Subsitute \name{<inside>} with sub : name, inside -> s """ start = '\\' + name + '{' if not start in s: return s # points to the '{' istart = s.index(start) i = istart + len(start) - 1 # minus brace after = s[i:] # try: assert after[0] == '{' inside_plus_brace, after = get_balanced_brace(after) except Malformed as e: bit = after[:max(len(after), 15)] msg = 'Could not find completion for "%s".' % bit raise_wrapped(Malformed, e, msg) inside = inside_plus_brace[1:-1] replace = sub(name=name, inside=inside) before = s[:istart] after_tran = substitute_command(after, name, sub) res = before + replace + after_tran return res
def check_solve_f_chain(id_dp, dp): with primitive_dp_test(id_dp, dp): from mcdp_posets.utils import poset_check_chain F = dp.get_fun_space() f_chain = F.get_test_chain(n=8) poset_check_chain(F, f_chain) try: trchain = map(dp.solve, f_chain) except NotSolvableNeedsApprox: return try_with_approximations(id_dp, dp, check_solve_f_chain) R = dp.get_res_space() UR = UpperSets(R) try: poset_check_chain(UR, trchain) except ValueError as e: msg = 'The map solve() for %r is not monotone.' % id_dp raise_wrapped(Exception, e, msg, f_chain=f_chain, trchain=trchain, compact=True)
def check_solve_r_chain(id_dp, dp): with primitive_dp_test(id_dp, dp): from mcdp_posets.utils import poset_check_chain R = dp.get_res_space() F = dp.get_fun_space() LF = LowerSets(F) r_chain = R.get_test_chain(n=8) poset_check_chain(R, r_chain) try: lfchain = map(dp.solve_r, r_chain) except NotSolvableNeedsApprox: return try_with_approximations(id_dp, dp, check_solve_r_chain) try: # now, notice that we need to reverse this lfchain_reversed = list(reversed(lfchain)) poset_check_chain(LF, lfchain_reversed) except ValueError as e: msg = 'The map solve_r() for %r is not monotone.' % id_dp raise_wrapped(Exception, e, msg, r_chain=r_chain, lfchain=lfchain, lfchain_reversed=lfchain_reversed, compact=True)
def db_error_wrap(what, **args): try: yield except CompmakeDBError as e: raise_wrapped(CompmakeDBError, e, what, compact=True, **args)
def _call(self, x): if is_top(self.dom, x): return self.cod.get_top() try: return float(x) except BaseException as e: msg = 'Internal error in PromoteToFloat.' raise_wrapped(DPInternalError, e, msg, x=x)
def belongs(self, x): self.P.belongs(x) try: self.check_leq(self.a, x) self.check_leq(x, self.b) except NotLeq as e: msg = 'Does not belong to interval.' raise_wrapped(NotBelongs, e, msg, compact=True)
def _call(self, x): l, u = x try: self.F0.check_leq(l, u) except NotLeq as e: msg = 'Run-time check failed; wrong use of "Uncertain" operator.' raise_wrapped(WrongUseOfUncertain, e, msg, l=l, u=u, compact=True) return x
def get_samples(request, ndp): xaxis = str(request.params['xaxis']) # yaxis = str(request.params['yaxis']) xmin = request.params['xmin'].encode('utf-8') xmax = request.params['xmax'].encode('utf-8') nsamples = int(request.params['nsamples']) fnames = ndp.get_fnames() rnames = ndp.get_rnames() # must pass the other functions as parameters f = {} for fn in fnames: if fn == xaxis: continue if not fn in request.params: msg = 'You have to pass the value of function %r.' % fn raise_desc(ValueError, msg, rnames=rnames) s = request.params[fn] try: val = parse_constant(s) except DPSyntaxError as e: msg = 'Cannot parse value for %r.' % fn raise_wrapped(ValueError, e, msg, compact=True) F = ndp.get_ftype(fn) f[fn] = express_value_in_isomorphic_space(val.unit, val.value, F) F = ndp.get_ftype(xaxis) try: xmin = parse_constant(xmin) xmax = parse_constant(xmax) except DPSyntaxError as e: msg = 'Cannot parse value for xmax/xmin.' raise_wrapped(ValueError, e, msg, compact=True) xmin = express_value_in_isomorphic_space(xmin.unit, xmin.value, F) xmax = express_value_in_isomorphic_space(xmax.unit, xmax.value, F) import numpy as np xsamples = np.linspace(xmin, xmax, nsamples) samples = [] for xsample in xsamples: t = [] for fn in fnames: if fn == xaxis: t.append(xsample) else: t.append(f[fn]) sample = tuple(t) if len(fnames) == 1: sample = sample[0] samples.append(sample) return samples
def _import(self): with _sys_path_adjust(self.sys_path): try: function = import_name(self.function_name) return function except ImportFailure as e: msg = 'Could not import Python function name.' raise_wrapped(DPSemanticError, e, msg, function_name=self.function_name, compact=True)