def display_fit(self, result, **kwargs): if 'plotjoined' not in self.dataset_kwargs: self.dataset_kwargs['plotjoined'] = False if 'size' not in self.dataset_kwargs: self.dataset_kwargs['size'] = 1 if 'scale' not in self.dataset_kwargs: self.dataset_kwargs['scale'] = 'linear' res_scale = kwargs.pop('res_scale', 'linear') ( list_plot( list(zip( self.xCol, self.residual_function(result.params,self.xCol) )), plotjoined=True,color='red', ymax=np.max(self.yCol), ymin=min(np.min(self.yCol),np.max(self.yCol)/100), **kwargs)\ + self.dataset.plot2d(**self.dataset_kwargs) ).show(figsize=[4,3]) list_plot(list( zip(self.xCol, self.residual_function(result.params, self.xCol, self.yCol))), size=self.dataset_kwargs['size']).show(figsize=[4, 3], scale=res_scale) peFit.display_fit(self, result, **kwargs)
def display_fit(self, result, **kwargs): if 'plotjoined' not in self.dataset_kwargs: self.dataset_kwargs['plotjoined'] = False if 'size' not in self.dataset_kwargs: self.dataset_kwargs['size'] = 1 if 'scale' not in self.dataset_kwargs: self.dataset_kwargs['scale'] = 'semilogy' fname_com = kwargs.pop('filename', None) res_scale = kwargs.pop('res_scale', 'linear') scale = kwargs.pop('scale', 'semilogy') fname_plot = None if fname_com is None else 'plot_' + fname_com fname_res = None if fname_com is None else 'res_' + fname_com #from sage.repl.rich_output import pretty_print #pretty_print(html('<table><tr><td>')) ( list_plot( list(zip( self.xCol, self.residual_function(result.params,self.xCol) )), plotjoined=True,color='red', **kwargs)\ + self.dataset.plot2d(**self.dataset_kwargs) ).show(#filename=fname_plot, figsize=[6,3], ymax=np.max(self.yCol)*1.1, ymin=min(np.min(self.yCol),np.max(self.yCol)/100), scale=scale ) resid_plt = list_plot( list( zip( self.xCol - self.xCol[0] + self.xCol[1], self.residual_function(result.params, self.xCol, self.yCol))), size=self.dataset_kwargs['size'], color='blue', ) resid_plt.show( #filename=fname_res, figsize=[6, 3], scale=res_scale) #plotfast(resid_plt,scale=res_scale) #if fname_com is not None: # pretty_print(html('<table><tr><td>')) #html('<img src="'+fname_plot+'">') # pretty_print(html('</td></tr><tr><td>')) #html('<img src="'+fname_res+'">') # pretty_print(html('</td></tr></table>')) # pretty_print(html('</td><td>')) peFit.display_fit(self, result, **kwargs)
def plot_raw(self, npoints=None, channel=0, plotjoined=True, **kwds): npoints = self._normalize_npoints(npoints) seconds = float(self._nframes) / float(self._width) sample_step = seconds / float(npoints) domain = [float(n*sample_step) / float(self._framerate) for n in range(npoints)] frame_skip = self._nframes / npoints values = [self.channel_data(channel)[frame_skip*i] for i in range(npoints)] points = zip(domain, values) return list_plot(points, plotjoined=plotjoined, **kwds)
def plot_fft(self, npoints=None, channel=0, half=True, **kwds): v = self.vector(npoints=npoints) w = v.fft() if half: w = w[:len(w) // 2] z = [abs(x) for x in w] if half: r = math.pi else: r = 2 * math.pi data = zip(srange(0, r, r / len(z)), z) L = list_plot(data, plotjoined=True, **kwds) L.xmin(0) L.xmax(r) return L
def plot_fft(self, npoints=None, channel=0, half=True, **kwds): v = self.vector(npoints=npoints) w = v.fft() if half: w = w[:len(w)//2] z = [abs(x) for x in w] if half: r = math.pi else: r = 2*math.pi data = zip(srange(0, r, r/len(z)), z) L = list_plot(data, plotjoined=True, **kwds) L.xmin(0) L.xmax(r) return L
def plot(self, npoints=None, channel=0, plotjoined=True, **kwds): """ Plots the audio data. INPUT: npoints -- number of sample points to take; if not given, draws all known points. channel -- 0 or 1 (if stereo). default: 0 plotjoined -- whether to just draw dots or draw lines between sample points OUTPUT: a plot object that can be shown. """ domain = self.domain(npoints = npoints) values = self.values(npoints=npoints, channel = channel) points = zip(domain, values) L = list_plot(points, plotjoined=plotjoined, **kwds) L.xmin(0) L.xmax(domain[-1]) return L
def plot(self, npoints=None, channel=0, plotjoined=True, **kwds): """ Plots the audio data. INPUT: npoints -- number of sample points to take; if not given, draws all known points. channel -- 0 or 1 (if stereo). default: 0 plotjoined -- whether to just draw dots or draw lines between sample points OUTPUT: a plot object that can be shown. """ domain = self.domain(npoints=npoints) values = self.values(npoints=npoints, channel=channel) points = zip(domain, values) L = list_plot(points, plotjoined=plotjoined, **kwds) L.xmin(0) L.xmax(domain[-1]) return L
def _plot2d_single_y(self,Xcol,Ycol,*args,**keywords): '''\ A simple plotting function for a single Y variable ''' #set default plot parameters if 'plotjoined' not in keywords: keywords['plotjoined']=True; if 'frame' not in keywords: keywords['frame']=True; if 'axes' not in keywords: keywords['axes']=False; if 'axes_pad' not in keywords: keywords['axes_pad']=0; if 'axes_labels_size' not in keywords: keywords['axes_labels_size']=1; #plot! from sage.plot.plot import list_plot # list_plot does not accept any args - it only treat the second arg as a plotjoined value # so we skip passing *args into.. return list_plot(zip(Xcol,Ycol),**keywords)
def plot_solution(self, x0=None, tini=0, T=1, NPOINTS=100, xcoord=0, ycoord=1, plotjoined=True, **kwargs): """ Solve and plot for the given coordinates. INPUT: - ``x0`` -- vector; initial condition - ``tini`` -- initial time of simulation - ``T`` -- final time of simulation - ``NPOINTS`` -- number of points sampled - ``xcoord`` -- (default: `0`), x-coordinate in plot - ``ycoord`` -- (default: `1`), y coordinate in plot EXAMPLES:: sage: from carlin.library import vanderpol sage: S = vanderpol(1, 1) sage: S.plot_solution(x0=[0.5, 1], T=20, NPOINTS=200) # not tested Graphics object consisting of 1 graphics primitive """ S = self.solve(x0=x0, tini=tini, T=T, NPOINTS=NPOINTS) sol_xy = [(S_ti[1][xcoord], S_ti[1][ycoord]) for S_ti in S.solution] return list_plot(sol_xy, plotjoined=plotjoined, **kwargs)
def plot_truncated(model, N, x0, tini, T, NPOINTS, xcoord=0, ycoord=1, **kwargs): """ Solve and return graphics in phase space of a given model. INPUT: - ``model`` -- PolynomialODE, defining the tuple `(f, n, k)` - ``N`` -- integer; truncation order - ``x0`` -- vector; initial condition - ``tini`` -- initial time of simulation - ``T`` -- final time of simulation - ``NPOINTS`` -- number of points sampled - ``xcoord`` -- (default: `0`), x-coordinate in plot - ``ycoord`` -- (default: `1`), y coordinate in plot NOTES: By default, returns a plot in the plane `(x_1, x_2)`. All other keyword arguments passes are sent to the `list_plot` command (use to set line color, style, etc.) EXAMPLES:: sage: from carlin.library import vanderpol sage: from carlin.io import plot_truncated sage: G = plot_truncated(vanderpol(1, 1), 2, [0.1, 0], 0, 5, 100) sage: G.show(gridlines=True, axes_labels = ['$x_1$', '$x_2$']) # not tested Graphics object consisting in 1 graphics primitive All other keyword arguments are passed to the ``list_plot`` function. For example, specify color and maximum and minimum values for the axes:: sage: G = plot_truncated(vanderpol(1, 1), 2, [0.1, 0], 0, 5, 100, color='green', xmin=-1, xmax=1, ymin=-1, ymax=1) sage: G.show(gridlines=True, axes_labels = ['$x_1$', '$x_2$']) # not tested Graphics object consisting in 1 graphics primitive """ from carlin.transformation import truncated_matrix from carlin.io import solve_ode_exp, get_Fj_from_model from sage.plot.plot import list_plot f, n, k = model.funcs(), model.dim(), model.degree() Fjnk = get_Fj_from_model(f, n, k) # this is a sparse matrix in coo format AN = truncated_matrix(N, *Fjnk, input_format='Fj_matrices') # solve the linea ODE using SciPy's sparse matrix solver sol = solve_ode_exp(AN, x0, N, tini=tini, T=T, NPOINTS=NPOINTS) sol_x1 = [sol[i][xcoord] for i in range(NPOINTS)] sol_x2 = [sol[i][ycoord] for i in range(NPOINTS)] return list_plot(zip(sol_x1, sol_x2), plotjoined=True, **kwargs)
def plot_time_triggered(model, N, x0, tini, T, NRESETS, NPOINTS, j, **kwargs): r""" Solve and plot the time-triggered algorithm for Carleman linarization. INPUT: - ``model`` -- PolynomialODE - ``N`` -- integer, truncation order - ``x0`` -- list, initial condition - ``tini`` -- initial time - ``T`` -- final time - ``NRESETS`` -- integer, fixed number of resets - ``NPOINTS`` -- integer, number of computation points - ``j`` -- integer in `0\ldots n-1`, variable to plot against time OUTPUT: Solution as a collection of lists, each list corresponding to the solution of a given chunk. NOTES: Other optional keyword argument include: - ``color`` -- color to be plotted """ G = Graphics() if 'color' in kwargs: color=kwargs['color'] else: color='blue' Fj = get_Fj_from_model(model.funcs(), model.dim(), model.degree()) AN = truncated_matrix(N, *Fj, input_format="Fj_matrices") solution = solve_time_triggered(AN, N, x0, tini, T, NRESETS, NPOINTS) # number of samples in each chunk CHUNK_SIZE = int(NPOINTS/(NRESETS+1)) tdom = srange(tini, T, (T-tini)/(NPOINTS-1)*1., include_endpoint=True) tdom_k = tdom[0:CHUNK_SIZE] G += list_plot(zip(tdom_k, solution[0][:, j]), plotjoined=True, linestyle="dashed", color=color, legend_label="$N="+str(N)+", r="+str(NRESETS)+"$") for i in range(1, NRESETS+1): # add point at switching time? G += point([tdom_k[0], x0_k[1]], size=25, marker='x', color=color) # add solution for this chunk G += list_plot(zip(tdom_k, solution[i][:, j]), plotjoined=True, linestyle="dashed", color=color) # add numerical solution of the nonlinear ODE # solution of the nonlinear ODE S = model.solve(x0=x0, tini=tini, T=T, NPOINTS=NPOINTS) x_t = lambda i : S.interpolate_solution(i) G += plot(x_t(j), tini, T, axes_labels = ["$t$", "$x_{"+str(j)+"}$"], gridlines=True, color="black") return G