Ejemplo n.º 1
0
def test_truncated():
    """
    this demonstrates that it doesn't work
    """
    import ngmix
    import biggles

    nsamp=1000000
    ptrunc=ngmix.priors.TruncatedSimpleGauss2D(0.0, 0.98, 0.4, 0.4, 1.0)

    g1,g2 = ptrunc.sample(nsamp)

    g=numpy.vstack( (g1,g2) ).T

    gmm=mixture.GMM(n_components=1, covariance_type='full')

    gmm.fit(g)

    gs=gmm.sample(nsamp)

    hc=biggles.make_histc(g[:,1], nbin=100, min=0.0, max=1.0, label='data', color='blue')
    hcs=biggles.make_histc(gs[:,1], nbin=100, min=0.0, max=1.0, label='fit',color='red')

    key=biggles.PlotKey(0.1,0.9,[hc,hcs], halign='left')

    plt=biggles.FramedPlot(xlabel='g2')
    plt.add( hc, hcs, key )

    plt.show()
Ejemplo n.º 2
0
def _plot_single(data, samples, comps, do_ylog=False):
    import biggles
    import esutil as eu
    import pcolors

    valmin=data.min()
    valmax=data.max()

    std = data.std()
    binsize=0.05*std

    ph,be,harr = biggles.make_histc(data, min=valmin, max=valmax,
                                    binsize=binsize,
                                    ylog=do_ylog, norm=1,
                                    get_hdata=True)
    sample_ph,sbe,sharr= biggles.make_histc(samples, min=valmin, max=valmax,
                                            binsize=binsize, color='red', ylog=do_ylog, norm=1,
                                            get_hdata=True)

    ph.label='data'
    sample_ph.label='fit'

    key = biggles.PlotKey(0.1, 0.9, [ph, sample_ph], halign='left')
    plt = biggles.FramedPlot()
    plt.add( ph, sample_ph, key )

    w,=where( (harr > 0) & (sharr > 0) )
    yrange=[min(harr[w].min(), sharr[w].min()),
            1.1*max(harr[w].max(), sharr[w].max())]

    if do_ylog:
        plt.ylog=True

    # now add the components
    h,rev=eu.stat.histogram(comps, rev=True)
    print(h)
    w,=where(h > 0)
    ncolors = w.size

    colors=pcolors.rainbow(ncolors)

    icolor=0
    for i in xrange(h.size):
        if rev[i] != rev[i+1]:
            w=rev[ rev[i]:rev[i+1] ]

            frac=float(w.size)/comps.size

            ph = biggles.make_histc(samples[w],
                                    #min=valmin, max=valmax,
                                    binsize=binsize,
                                    color=colors[icolor],
                                    ylog=do_ylog,
                                    norm=frac)
            plt.add(ph)
            icolor += 1

    plt.yrange=yrange
    return plt
Ejemplo n.º 3
0
def _plot_single(data, samples, do_ylog=False):
    import biggles

    valmin=data.min()
    valmax=data.max()

    std = data.std()
    binsize=0.05*std

    ph = biggles.make_histc(data, min=valmin, max=valmax, binsize=binsize,  ylog=do_ylog, norm=1)
    sample_ph= biggles.make_histc(samples, min=valmin, max=valmax, binsize=binsize, color='red', ylog=do_ylog, norm=1)

    ph.label='data'
    sample_ph.label='fit'

    '''
    hdict = get_norm_hist(data, min=valmin, max=valmax, binsize=binsize)
    sample_hdict = get_norm_hist(samples, min=valmin, max=valmax, binsize=binsize)


    hist=hdict['hist_norm']
    sample_hist=sample_hdict['hist_norm']

    if do_ylog:
        hist=hist.astype('f8').clip(min=0.001)
        sample_hist=sample_hist.astype('f8').clip(min=0.001)


    ph = biggles.Histogram(hist, x0=valmin, width=4, binsize=binsize)
    ph.label = 'data'
    sample_ph = biggles.Histogram(sample_hist, x0=valmin, 
                                  width=1, color='red', binsize=binsize)
    sample_ph.label = 'joint fit'
    '''


    key = biggles.PlotKey(0.1, 0.9, [ph, sample_ph], halign='left')

    plt = biggles.FramedPlot()

    plt.add( ph, sample_ph, key )

    if do_ylog:
        plt.ylog=True
    #if do_ylog:
    #    plt.yrange=[0.1, 1.1*sample_hist.max()]

    return plt
Ejemplo n.º 4
0
def compare_hist(data1, data2, names=None, dataset_names=None, nsig=10.0, **kw):
    """
    Compare the normalized histograms for the two data sets.  Make a grid of
    plots if the data are multi-dimensional

    parameters
    ----------
    data1: array
        a [N] or [N,dim] array
    data2: array
        a [M] or [M,dim] array
    names: list, optional
        Optional list of names for each dimension
    dataset_names: list, optional
        Optional list of names for each dataset
    nsig: float, optional
        Optional number of standard deviations to clip histograms,
        default 10.0
    """
    import biggles
    from numpy import newaxis
    from .stat import sigma_clip

    if len(data1.shape)==1:
        data1=data1[:,newaxis]
    if len(data2.shape)==1:
        data2=data2[:,newaxis]

    n1,d1 = data1.shape
    n2,d2 = data2.shape

    if d1 != d2:
        raise ValueError("data must have same number of dims. "
                         "got %d and %d" % (d1,d2))
    
    if names is not None:
        if len(names) != d1:
            raise ValueError("names must have len equal to number of dims. "
                             " in data, got %d and %d" % (d1,len(names)))

    else:
        names = ['par%d' % i for i in xrange(d1)]
    if dataset_names is not None:
        if len(dataset_names) != 2:
            raise ValueError("dataset_names must be len 2, "
                             "got %d" % len(dataset_names))
    else:
        dataset_names=['dataset1','dataset2']

    if nsig is None:
        nsig=100.0

    grid = Grid(d1)
    tab = biggles.Table(grid.nrow, grid.ncol)

    pkw = {}
    pkw.update(kw)
    for dkeys in ['width','height']:
        del pkw[dkeys]

    pkw['visible']=False
    for i in xrange(d1):
        mn1,st1,ind1=sigma_clip(data1[:,i], nsig=nsig, get_indices=True)
        mn2,st2,ind2=sigma_clip(data2[:,i], nsig=nsig, get_indices=True)

        use_std = max(st1,st2)
        binsize = 0.2*use_std

        plt=biggles.FramedPlot()
        plt.xlabel=names[i]

        pkw['binsize']=binsize
        pkw['color']='blue'
        h1=biggles.make_histc(data1[ind1,i], **pkw)
        pkw['color']='red'
        h2=biggles.make_histc(data2[ind2,i], **pkw)

        plt.add(h1,h2)

        if i==0:
            h1.label=dataset_names[0]
            h2.label=dataset_names[1]
            key=biggles.PlotKey(0.9,0.9,[h1,h2],halign='right')
            plt.add(key)

        row,col=grid(i)
        tab[row,col] = plt

    if 'show' in kw:
        show=kw['show']
    elif 'visible' in kw:
        show=kw['visible']
    else:
        show=True

    tab.aspect_ratio=kw.get('aspect_ratio',float(grid.nrow)/grid.ncol)
    if show:
        width=kw.get('width',1000)
        height=kw.get('height',1000)
        tab.show(width=width, height=height)

    return tab
Ejemplo n.º 5
0
def plot_results(trials, **keys):
    """
    Plot the points and histograms of trials from an MCMC chain.
    """
    import biggles
    import esutil
    from esutil.numpy_util import where1, between

    npars = trials.shape[1]

    fontsize_min = keys.get("fontsize_min", 1)
    biggles.configure("default", "fontsize_min", fontsize_min)
    weights = keys.get("weights", None)

    nbin = keys.get("nbin", 35)
    names = keys.get("names", None)
    show = keys.get("show", True)

    nsigma = keys.get("nsigma", None)

    means, cov = extract_stats(trials, weights=weights)
    errs = sqrt(diag(cov))

    plt = biggles.Table(npars, 2)

    ind = numpy.arange(trials.shape[0])

    for i in xrange(npars):
        if names is not None:
            name = names[i]
        else:
            name = r"$p_{%d}$" % i

        use_trials = trials[:, i]
        use_ind = ind
        use_wts = weights
        if nsigma is not None:
            w = where1(between(trials[:, i], means[i] - nsigma * errs[i], means[i] + nsigma * errs[i]))
            use_trials = trials[w, i]
            use_ind = ind[w]
            if weights is not None:
                use_wts = weights[w]

        burn_plot_i = biggles.plot(use_ind, use_trials, type="solid", xlabel="step", ylabel=name, visible=False)
        plt[i, 0] = burn_plot_i

        hcurve, bin_edges, harray = biggles.make_histc(use_trials, nbin=nbin, weights=use_wts, get_hdata=True)

        plti = biggles.FramedPlot()

        plti.xlabel = name

        hmax = harray.max()
        plti.yrange = [-0.05 * hmax, 1.2 * hmax]

        plti.add(hcurve)

        lab = r"$<%s> = %0.4g \pm %0.4g$" % (name, means[i], errs[i])
        plab = biggles.PlotLabel(0.1, 0.8, lab, halign="left", color="blue")

        plti.add(plab)

        plt[i, 1] = plti

    plt.title = keys.get("title", None)

    if show:
        plt.show()

    return plt
Ejemplo n.º 6
0
def compare_hist(data1,
                 data2,
                 names=None,
                 dataset_names=None,
                 nsig=10.0,
                 **kw):
    """
    Compare the normalized histograms for the two data sets.  Make a grid of
    plots if the data are multi-dimensional

    parameters
    ----------
    data1: array
        a [N] or [N,dim] array
    data2: array
        a [M] or [M,dim] array
    names: list, optional
        Optional list of names for each dimension
    dataset_names: list, optional
        Optional list of names for each dataset
    nsig: float, optional
        Optional number of standard deviations to clip histograms,
        default 10.0
    """
    import biggles
    from numpy import newaxis
    from .stat import sigma_clip

    if len(data1.shape) == 1:
        data1 = data1[:, newaxis]
    if len(data2.shape) == 1:
        data2 = data2[:, newaxis]

    n1, d1 = data1.shape
    n2, d2 = data2.shape

    if d1 != d2:
        raise ValueError("data must have same number of dims. "
                         "got %d and %d" % (d1, d2))

    if names is not None:
        if len(names) != d1:
            raise ValueError("names must have len equal to number of dims. "
                             " in data, got %d and %d" % (d1, len(names)))

    else:
        names = ['par%d' % i for i in xrange(d1)]
    if dataset_names is not None:
        if len(dataset_names) != 2:
            raise ValueError("dataset_names must be len 2, "
                             "got %d" % len(dataset_names))
    else:
        dataset_names = ['dataset1', 'dataset2']

    if nsig is None:
        nsig = 100.0

    grid = Grid(d1)
    tab = biggles.Table(grid.nrow, grid.ncol)

    pkw = {}
    pkw.update(kw)
    for dkeys in ['width', 'height']:
        del pkw[dkeys]

    pkw['visible'] = False
    for i in xrange(d1):
        mn1, st1, ind1 = sigma_clip(data1[:, i], nsig=nsig, get_indices=True)
        mn2, st2, ind2 = sigma_clip(data2[:, i], nsig=nsig, get_indices=True)

        use_std = max(st1, st2)
        binsize = 0.2 * use_std

        plt = biggles.FramedPlot()
        plt.xlabel = names[i]

        pkw['binsize'] = binsize
        pkw['color'] = 'blue'
        h1 = biggles.make_histc(data1[ind1, i], **pkw)
        pkw['color'] = 'red'
        h2 = biggles.make_histc(data2[ind2, i], **pkw)

        plt.add(h1, h2)

        if i == 0:
            h1.label = dataset_names[0]
            h2.label = dataset_names[1]
            key = biggles.PlotKey(0.9, 0.9, [h1, h2], halign='right')
            plt.add(key)

        row, col = grid(i)
        tab[row, col] = plt

    if 'show' in kw:
        show = kw['show']
    elif 'visible' in kw:
        show = kw['visible']
    else:
        show = True

    tab.aspect_ratio = kw.get('aspect_ratio', float(grid.nrow) / grid.ncol)
    if show:
        width = kw.get('width', 1000)
        height = kw.get('height', 1000)
        tab.show(width=width, height=height)

    return tab
Ejemplo n.º 7
0
def compare_hist(
    data1,
    data2,
    names=None,
    dataset_names=None,
    nsig=10.0,
    **kw,
):
    """
    Compare the normalized histograms for the two data sets.  Make a grid of
    plots if the data are multi-dimensional

    parameters
    ----------
    data1: array
        a [N] or [N,dim] array
    data2: array
        a [M] or [M,dim] array
    names: list, optional
        Optional list of names for each dimension
    dataset_names: list, optional
        Optional list of names for each dataset
    nsig: float, optional
        Optional number of standard deviations to clip histograms,
        default 10.0
    """
    import biggles
    from numpy import newaxis
    from .stat import sigma_clip

    if len(data1.shape) == 1:
        data1 = data1[:, newaxis]
    if len(data2.shape) == 1:
        data2 = data2[:, newaxis]

    n1, d1 = data1.shape
    n2, d2 = data2.shape

    if d1 != d2:
        raise ValueError("data must have same number of dims. "
                         "got %d and %d" % (d1, d2))

    if names is not None:
        if len(names) != d1:
            raise ValueError("names must have len equal to number of dims. "
                             " in data, got %d and %d" % (d1, len(names)))

    else:
        names = ["par%d" % i for i in range(d1)]
    if dataset_names is not None:
        if len(dataset_names) != 2:
            raise ValueError("dataset_names must be len 2, "
                             "got %d" % len(dataset_names))
    else:
        dataset_names = ["dataset1", "dataset2"]

    if nsig is None:
        nsig = 100.0

    grid = Grid(d1)
    tab = biggles.Table(grid.nrow, grid.ncol)

    pkw = {}
    pkw.update(kw)
    for dkey in ["width", "height"]:
        pkw.pop(dkey, None)

    pkw["visible"] = False
    pkw["norm"] = 1
    if "nbin" not in pkw and 'binsize' not in pkw:
        get_binsize = True
    else:
        get_binsize = False

    for i in range(d1):

        mn1, st1, ind1 = sigma_clip(data1[:, i], nsig=nsig, get_indices=True)
        mn2, st2, ind2 = sigma_clip(data2[:, i], nsig=nsig, get_indices=True)
        if get_binsize:
            use_std = max(st1, st2)
            pkw["binsize"] = 0.2 * use_std

        plt = biggles.FramedPlot()
        plt.xlabel = names[i]

        pkw["color"] = "blue"
        h1 = biggles.make_histc(data1[ind1, i], **pkw)
        pkw["color"] = "red"
        h2 = biggles.make_histc(data2[ind2, i], **pkw)

        plt.add(h1, h2)

        if i == 0:
            h1.label = dataset_names[0]
            h2.label = dataset_names[1]
            key = biggles.PlotKey(0.9, 0.9, [h1, h2], halign="right")
            plt.add(key)

        row, col = grid(i)
        tab[row, col] = plt

    if "show" in kw:
        show = kw["show"]
    elif "visible" in kw:
        show = kw["visible"]
    else:
        show = True

    tab.aspect_ratio = kw.get("aspect_ratio", float(grid.nrow) / grid.ncol)
    if show:
        width = kw.get("width", 1000)
        height = kw.get("height", 1000)
        tab.show(width=width, height=height)

    return tab
Ejemplo n.º 8
0
def multihist(data, binfac=0.1, **kw):
    """
    plot a histogram for each dimension of the data

    parameters
    ----------
    data: array
        array with shape [npoints, ndim]
    binfac: float
        The binsize for each dimension will be chosen as binfac*std(dimdata)
    labels: optional
        A sequence of labels for each dimension
    """
    import biggles

    if len(data.shape) != 2:
        raise ValueError("data should have shape [npoints,ndim]")

    ndim=data.shape[1]

    labels=kw.pop('labels',None)
    if labels is not None:
        nl=len(labels)
        assert len(labels)==ndim,"len(labels) = %d != %d" % (nl,ndim)

    grid=Grid(ndim)

    tab = kw.pop('plt',None)
    if tab is not None:
        add_to_existing_plots=True
    else:
        add_to_existing_plots=False
        tab=biggles.Table(grid.nrow, grid.ncol)
        tab.aspect_ratio=kw.pop('aspect_ratio',None)

        if tab.cols != grid.ncol or tab.rows != grid.nrow:
            m="input table has wrong dims.  Expected %s got %s"
            tup = ((grid.nrow,grid.ncol),(tab.rows,tab.cols))
            raise ValueError(m % tup)

    for dim in xrange(ndim):

        ddata = data[:,dim]

        mn, std = eu.stat.sigma_clip(ddata)

        binsize=binfac*std

        hc = biggles.make_histc(
            ddata,
            binsize=binsize,
            **kw
        )
        
        row,col=grid(dim)
        if add_to_existing_plots:
            plt = tab[row,col]
            plt.add(hc)
        else:
            plt = biggles.FramedPlot(aspect_ratio=1, **kw)
            plt.add(hc)
            tab[row,col]=plt

        
        if labels is not None:
            lab=labels[dim]
        else:
            lab='dim %d' % dim
        tab[row,col].xlabel=lab
    return tab