def add_noise_model_BA(A,B): if len(A) > 300: A = random.sample(A,300) if len(B) > 300: B = random.sample(B,300) pv = octave.call('/home/ole/src/cause_effect_sample_code/additive-noise/code/fitA.m',B,A) print "add_noise_model_BA=",pv return pv
def compare_results(name, function, args, values): args = [values.get(arg) for arg in args] res = function(*args) # Python call. # FIXME: Test only the first output when multiple outputs are present. if isinstance(res, tuple): nout = len(res) res = res[0] else: nout = 1 val = octave.call('sw_%s' % name, *args, nout=nout) # Octave call. if nout > 1: val = val[0] val, res = val.squeeze(), res.squeeze() np.testing.assert_allclose(val, res)
def run(self): print 'Reading values and calling Octave' [Calculate.y, Calculate.y2] = \ oc.call('Wurfp3.m', [self.rhop, self.dp, self.v, self.angle, self.prec, self.duration, self.windx, self.windy, self.rhog, self.eta, self.grav, self.posx, self.posz], verbose=False) print 'Done! Data from Octave available' self.guest.myplot(0, self.y[:, 0], self.y[:, 1], 'Time in [s]', 'Speed in [m/s]', pttl='Speed-Time-Horizontal') self.guest.Paper.show() # Return from octave: [t,nghs,nghp,ngvs,ngvp,hnt,hnp,hnv,vnt,vnv,vnp] self.maxhight = str(max(Calculate.y[:, 4])) self.maxhightxid = nm.argmax(Calculate.y[:, 4]) self.maxhightx = str(Calculate.y[self.maxhightxid, 2]) self.duration = str(Calculate.y2[0, 0]) self.relaxtime = str(Calculate.y2[0, 1]) self.VTSN = str(Calculate.y2[0, 2]) self.VTSS = str(Calculate.y2[0, 3]) self.nusv = str(Calculate.y2[0, 4]) self.absolutey = [abs(x) for x in Calculate.y[1:, 4]] self.hitground = str(Calculate.y[nm.argmin(self.absolutey), 2]) dc.getcontext().prec = 12 self.maxhightdec = self.decimalize(self.maxhight) self.maxhightxdec = self.decimalize(self.maxhightx) self.hitgrounddec = self.decimalize(self.hitground) self.VTSNdec = self.decimalize(self.VTSN) self.VTSSdec = self.decimalize(self.VTSS) self.nusvdec = self.decimalize(self.nusv) maxflight = "Max flight height: "+str(self.maxhightdec)+" meters at "\ + str(self.maxhightxdec)+" meters \n"+str(self.VTSNdec)\ + " m/s Newton settling velocity\n"\ + str(self.VTSSdec)+" m/s Stokes settling velocity\n"\ + str(self.nusvdec)+" m/s numerical settling velocity\n" if self.maxhightxid < nm.argmin(self.absolutey): hitground = "Hits ground in: "+str(self.hitgrounddec)\ + " meters distance \n" else: hitground = "Does not reach the ground in observed timespan \n" entries = "Done \n"+"Relax-time: "+self.relaxtime+" seconds\n" + \ "Duration: "+self.duration+" seconds\n"+maxflight+hitground\ self.guest.info.config(state=tki.NORMAL) self.guest.info.insert(tki.END, entries) self.guest.info.yview(tki.END) self.guest.info.config(state=tki.DISABLED) self.guest.redraw2() return
def __init__(self, octave, parent, debug=False): """ OBSOLETE: version 1.0 of oct2py gets this right!! oct2py doesn't handle nested Octave variables very well. However, the result is an empty NumPy array for which the dtypes are the nested variable names in Octave. This ugly monkey patch gets all the second nested variables. Parameters ---------- parent : string Octave variable name of type struct (aka as a nested variable) octave : oct2py._oct2py.Oct2Py object A valid living octave instance of oct2py for which all relevant BECAS commands have been executed. It should hence contain all the relevant BECAS output variables. """ # oct2py doesn't handle nested Octave variables very well. However, # the result is a NumPy array for which the dtypes are the nested # variable names in Octave. This ugly monkey patch gets all the second # nested variables. oct_var = octave.call(parent) for child in oct_var.dtype.names: # there seems to be a bug (or is it a feature?) in oct2py: # long nested variable names are truncated, or when ending with r? # Ugly monkey patch follows to get back the missing r at the end if child == 'AlphaPrincipleAxis_ElasticCente': child += 'r' # can't use eval because that is only for expressions (no =). # Exec can also supports statements expr = "self.%s = octave.call('%s.%s')" % (child, parent, child) if debug: print(expr) exec compile(expr, "<string>", "exec") # and set the class name to the parent name, but that needs to be # a string here.... self.__class__.__name__ = parent
def compare_results(name, function, args): args = [values.get(arg) for arg in args] try: # Python. res = function(*args) except: print('%s: python runtime error' % name) raise return 'no_python' # FIXME: Testing only the first output when multiple outputs are present. nout = 1 if isinstance(res, tuple): nout = len(res) res = res[0] try: # Octave. val = octave.call('sw_%s' % name, *args, verbose=False, nout=nout) if nout > 1: val = val[0] except Oct2PyError: print('%s: Octave runtime error' % name) print("python:\n%s" % res) return 'no_octave' val = val.squeeze() res = res.squeeze() try: perfect = (val == res).all() except: print('%s: Comparison failed' % name) print("octave:\n%s" % val) print("python:\n%s" % res) return 'no_comparison' if np.allclose(val, res, rtol=1e-15, atol=0): print('%s: Passed' % name) return 'passed' else: print('%s: Failed' % name) print("octave:\n%s" % val) print("python:\n%s" % res) return 'failed' print('')
def run(self): print 'Reading values and calling Octave - mode 3D' [Calculate3D.y, Calculate3D.y2] = \ oc.call('Wurf3D.m', [self.rhop, self.dp, self.v, self.anglee, self.anglea, self.prec, self.duration, self.windx, self.windy, self.windz, self.rhog, self.eta, self.grav, self.posx, self.posy, self.posz], verbose=False) print 'Done! Data from Octave available - mode 3D' self.guest.myplot(1, self.y[:, 4], self.y[:, 5], 'XDistance in [m]', 'YDistance in [m]', self.y[:, 6], 'ZHeight in [m]', 'Speed-Time-Horizontal') self.guest.Paper.show() # Return from octave: # dat1=[ngt,ngxs,ngys,ngzs,ngxp,ngyp,ngzp]; # dat2=[duration,trelax,VTSN,VTSS,nusv]; self.maxhight = str(max(Calculate3D.y[:, 6])) self.maxhightxid = nm.argmax(Calculate3D.y[:, 6]) self.maxhightx = str(Calculate3D.y[self.maxhightxid, 4]) self.maxhighty = str(Calculate3D.y[self.maxhightxid, 5]) self.duration = str(Calculate3D.y2[0, 0]) self.relaxtime = str(Calculate3D.y2[0, 1]) self.VTSN = str(Calculate3D.y2[0, 2]) self.VTSS = str(Calculate3D.y2[0, 3]) self.nusv = str(Calculate3D.y2[0, 4]) self.absolutez = [abs(x) for x in Calculate3D.y[1:, 6]] self.hitgroundx = str(Calculate3D.y[nm.argmin(self.absolutez), 4]) self.hitgroundy = str(Calculate3D.y[nm.argmin(self.absolutez), 5]) self.hitgroundxf = float(Calculate3D.y[nm.argmin(self.absolutez), 4]) self.hitgroundyf = float(Calculate3D.y[nm.argmin(self.absolutez), 5]) self.hitgrounddist = nm.sqrt(nm.square(self.hitgroundxf) + nm.square(self.hitgroundyf)) dc.getcontext().prec = 12 self.maxhightdec = self.decimalize(self.maxhight) self.maxhightxdec = self.decimalize(self.maxhightx) self.maxhightydec = self.decimalize(self.maxhighty) self.hitgroundxdec = self.decimalize(self.hitgroundx) self.hitgroundydec = self.decimalize(self.hitgroundy) self.hitgrounddist = self.decimalize(self.hitgrounddist) self.relaxtimedec = self.decimalize(self.relaxtime) self.durationdec = self.decimalize(self.duration) self.VTSNdec = self.decimalize(self.VTSN) self.VTSSdec = self.decimalize(self.VTSS) self.nusvdec = self.decimalize(self.nusv) maxflight = "Max flight height: \n" \ + str(self.maxhightdec)+" meters in Z at \n"\ + str(self.maxhightxdec)+" meters in X\n"\ + str(self.maxhightydec)+" meters in Y\n"\ + str(self.VTSNdec)+" m/s Newton settling velocity\n"\ + str(self.VTSSdec)+" m/s Stokes settling velocity\n"\ + str(self.nusvdec)+" m/s numerical settling velocity\n" if self.maxhightxid < nm.argmin(self.absolutez): hitground = "Hits ground at: \n"\ + str(self.hitgroundxdec)+" meters X \n"\ + str(self.hitgroundydec)+" meters Y \n"\ + str(self.hitgrounddist)+" meters from source \n" else: hitground = "Does not reach the ground in observed timespan \n" entries = "Done \n"+"Relax-time: "+self.relaxtime+" seconds\n" +\ "Duration: "+self.duration+" seconds\n"+maxflight+hitground\ self.guest.info.config(state=tki.NORMAL) self.guest.info.insert(tki.END, entries) self.guest.info.yview(tki.END) self.guest.info.config(state=tki.DISABLED) self.guest.redraw2() return
def ffun(x): return octave.call("myfun.m",x)*(-1) # ggf. durch octave.myfun(x) ersetzen # Grenzen, z.B. #xmin = [10.,3.,2.,0.05,0.05] #xmax = [60.,5.,5.,1.5,1.5] xmin = [0.,0.,0.,0.,0.]