def show(self, x, gd, density=None, axes=None): """ Function to plot a GMM, still in progress Currently, works only in 1D and 2D Parameters ---------- x: array of shape(n_samples, dim) the data under study gd: GridDescriptor instance density: array os shape(prod(gd.n_bins)) density of the model one the discrete grid implied by gd by default, this is recomputed """ import matplotlib.pylab as mp # recompute the density if necessary if density==None: density = self.mixture_likelihood(gd,x) import pylab if axes is None: axes = pylab.figure() if gd.dim==1: from nipy.neurospin.utils.emp_null import smoothed_histogram_from_samples h, c = smoothed_histogram_from_samples(x, normalized=True) #step = 3.5*np.std(x)/np.exp(np.log(np.size(x))/3) #bins = max(10,(x.max()-x.min())/step) #xmin = 1.1*x.min() - 0.1*x.max() #xmax = 1.1*x.max() - 0.1*x.min() #h,c = np.histogram(x, bins, [xmin, xmax], normed=True) offset = (c.max()-c.min())/(2*c.size) grid = gd.make_grid() h /= h.sum() h /= (2*offset) mp.plot(c[:-1]+offset, h) mp.plot(grid, density) if gd.dim==2: mp.figure() xm = gd.lim[0] xM = gd.lim[1] ym = gd.lim[2] yM = gd.lim[3] gd0 = gd.n_bins[0] Pdens= np.reshape(density, (gd0, np.size(density)/gd0)) axes.imshow(Pdens.T, None, None, None, 'nearest', 1.0, None, None,'lower',[xm, xM, ym, yM]) axes.plot(x[:, 0], x[:, 1], '.k') axes.axis([xm, xM, ym, yM]) return axes
def test_smooth_histo(): """ test smoothed histogram geenration """ n=100 x = np.random.randn(n) h, c = smoothed_histogram_from_samples(x, normalized=True) thh = 1./np.sqrt(2*np.pi) hm = h.max() #print hm, thh assert np.absolute(hm-thh)<0.15