Beispiel #1
0
    def fit(self, i, x, y, yerr = None):
        ff = self.ff[i]
        if ff:
            ff = ff.replace(' ', '')
            log.info('fitting function {}'.format(ff))
            fitfunc = eval('lambda x,*p:' + ff)
            x, y = np.array(x), np.array(y)
            m = np.logical_and(np.isfinite(x), np.isfinite(y))
            if yerr is not None:
                yerr = np.array(yerr)
                m = np.logical_and(m, np.isfinite(yerr))
                yerr = yerr[m]
            x , y = x[m], y[m]

            # gather fit parameters
            p = tuple([float(fp) for fp in self.fp[i].split(',')])
            try:
                p, c = curve_fit(fitfunc, x, y, p, yerr)
                log.info('parameters = {}'.format(p))
                log.info('covariance = {}'.format(c))
                fit_status = ''
            except Exception as e:
                fit_status = ' (failed: {})'.format(e)
                c = None
                log.exception('fit failed')

            # plot fit result
            xfit = np.linspace(np.nanmin(x), np.nanmax(x), 1000)
            yfit = fitfunc(xfit, *p)
            args = [xfit, yfit]
            if self.fl[i]: args.append(self.fl[i])
            l, = plt.plot(*args)

            N = len(x)
            chi2 = fitfunc(x, *p) - y
            if yerr is not None:
                chi2 = chi2 / yerr
            chi2 = (chi2 ** 2).sum()

            # add textbox
            t = 'y=' + ff
            t += '\n$\\chi^2$/N = {}/{}'.format(number_mathformat(chi2), number_mathformat(N))
            for k, v in enumerate(p):
                try:
                    t += '\np[{}] = {}$\\pm${}'.format(k, number_mathformat(v), number_mathformat(np.sqrt(c[k, k])))
                except:
                    t += '\np[{}] = {}$\\pm${}'.format(k, v, c)
            self.fitboxes.append(t)
            ll = ('Fit' + fit_status + ' y=' + ff)
            for k, v in  enumerate(p):
                ll = ll.replace('p[{}]'.format(k), number_mathformat(v, 3))
            self.legend.append((l, ll))
Beispiel #2
0
    def _map(self, i):
        import maps
        log.debug('map of {}'.format([getattr(self, v)[i] for v in 'sxyzc']))
        kwargs = self.opts(i)
        x, y, z = self.data(i)
        o = get_args_from(kwargs, margin = 0.05, width = 10e6, height = None, boundarylat = 50, projection = 'cyl',
                          drawcoastline = 1, drawgrid = 1, drawspecgrid = 1, drawcountries = 0, bluemarble = 0, nightshade = None)

        m = maps.drawmap(y, x, **o)
        x, y = m(x, y)

        if z is None:
            l, = plt.plot(x, y, **kwargs)
        else:
            # linestyle must not be 'none' when plotting 3D
            if 'linestyle' in kwargs and kwargs['linestyle'] == 'none':
                kwargs['linestyle'] = ':'

            o = get_args_from(kwargs, markersize = 6, cbfrac = 0.04, cblabel = self.alabel('z'))
            p = set_defaults(kwargs, zorder = 100)
            l = plt.scatter(x, y, c = z, s = o.markersize ** 2, edgecolor = 'none', **p)

            m = 6.0
            dmin, dmax = np.nanmin(z), np.nanmax(z)
            cticks = ticks.get_ticks(dmin, dmax, m, only_inside = 1)
            formatter = mpl.ticker.FuncFormatter(func = lambda x, i:number_mathformat(x))
            cb = plt.colorbar(fraction = o.cbfrac, pad = 0.01, aspect = 40, ticks = cticks, format = formatter)
            cb.set_label(o.cblabel)

        self.legend.append((l, self.llabel(i)))
Beispiel #3
0
    def _xy(self, i):
        log.debug('xy plot of {}'.format([getattr(self, v)[i] for v in 'sxyzc']))
        kwargs = self.opts(i)
        x, y, z = self.data(i)

        if x is not None:
            args = (x, y)
        else:
            args = (y,)

        if z is None:
            l, = plt.plot(*args, **kwargs)
        else:
            # linestyle must not be 'none' when plotting 3D
            if 'linestyle' in kwargs and kwargs['linestyle'] == 'none':
                kwargs['linestyle'] = ':'

            o = get_args_from(kwargs, markersize = 2, cbfrac = 0.04, cblabel = self.alabel('z'))
            l = plt.scatter(x, y, c = z, s = o.markersize ** 2, edgecolor = 'none', **kwargs)

            m = 6.0
            dmin, dmax = np.nanmin(z), np.nanmax(z)
            cticks = ticks.get_ticks(dmin, dmax, m, only_inside = 1)
            formatter = mpl.ticker.FuncFormatter(func = lambda x, i:number_mathformat(x))
            cb = plt.colorbar(fraction = o.cbfrac, pad = 0.01, aspect = 40, ticks = cticks, format = formatter)
            cb.set_label(o.cblabel)

        self.legend.append((l, self.llabel(i)))

        # fit
        self.fit(i, x, y)
Beispiel #4
0
    def stats_fields2d(self, i, contents, xcenters, ycenters):
        stats = {}
        stats['N'] = N = contents.sum()
        stats['mean'] = mean = np.array([ (contents.sum(axis = 0) * xcenters).sum(),
                         (contents.sum(axis = 1) * ycenters).sum()]) / N
        stats['std'] = np.sqrt(np.array([(contents.sum(axis = 0) * (xcenters - mean[0]) ** 2).sum(),
                                  (contents.sum(axis = 1) * (ycenters - mean[1]) ** 2).sum()]) / N)
        cov = 0
        for k, l in product(xrange(contents.shape[1]), xrange(contents.shape[0])):
            cov += contents[l, k] * (xcenters[k] - mean[0]) * (ycenters[l] - mean[1])
        stats['cov'] = cov / N
        log.debug(stats)

        text = '{:6} {}'.format('hist', self.llabel(i))
        sb = self.sb[i]
        if 'a' in sb: sb = 'nmscpewx'
        if 'uflow' in stats and stats['uflow']: sb += 'u'
        if 'oflow' in stats and stats['oflow']: sb += 'o'
        for k in sb:
            k = stats_abrv[k]
            if k in stats:
                v = stats[k]
                try:
                    v = number_mathformat(v)
                except:
                    v = '({})'.format(','.join(map(number_mathformat, v)))
                text += '\n{:6} {}'.format(_(k), v)
        self.textboxes.append(text)
Beispiel #5
0
    def stats_fields1d(self, i, data, contents, errors, edges):
        centers = (edges[1:] + edges[:-1]) / 2
        widths = np.diff(edges)

        stats = {}
        stats['N'] = N = np.sum(contents)
        stats['uflow'] = np.sum(data < edges[0])
        stats['oflow'] = np.sum(edges[-1] < data)
        stats['mean'] = mean = np.sum(centers * contents) / N
        stats['std'] = std = np.sqrt(np.sum((centers - mean) ** 2 * contents) / N)
        stats['mode'] = centers[np.argmax(contents)]
        bc, be = get_density(contents, errors, widths)
        bc, be = get_cumulative(bc, be, 1, widths)
        median_i = np.minimum(len(centers)-1, np.searchsorted(bc, 0.5, side = 'right'))
        stats['median'] = median = centers[median_i]
        if len(centers) % 2 == 0:  # even # of s
            stats['median'] = median = (median + centers[median_i - 1]) / 2
        stats['skew'] = np.sum(((centers - mean) / std) ** 3 * contents) / N
        stats['kurtos'] = kurtosis = np.sum(((centers - mean) / std) ** 4 * contents) / N
        stats['excess'] = kurtosis - 3
        log.debug(stats)

        text = '{:6} {}'.format('hist', self.llabel(i))
        sb = self.sb[i]
        if 'a' in sb: sb = 'nmscpewx'
        if 'uflow' in stats and stats['uflow']: sb += 'u'
        if 'oflow' in stats and stats['oflow']: sb += 'o'
        for k in sb:
            k = stats_abrv[k]
            if k in stats:
                text += '\n{:6} {}'.format(_(k), number_mathformat(stats[k]))
        self.textboxes.append(text)
Beispiel #6
0
    def _hist2d(self, i):
        log.debug('2D histogram of {}'.format([getattr(self, v)[i] for v in 'sxyzc']))
        kwargs = self.opts(i)
        x, y, z = self.data(i)
        o = get_args_from(kwargs, style = 'color', density = False, log = False, cbfrac = 0.04, cblabel = 'bincontent', levels = 10)
        filled = 'color' in o.style or ('fill' in o.style)
        o.update(get_args_from(kwargs, hidezero = o.log or filled, colorbar = filled, clabels = not filled))

        # make binnings
        bins = self.bins(i, 'x')
        if  bins == 0:
            bins = int(1 + np.log2(len(x)))
        xedges, xcenters, xwidths = get_binning(bins, x)

        bins = self.bins(i, 'y')
        if  bins == 0:
            bins = int(1 + np.log2(len(y)))
        yedges, ycenters, ywidths = get_binning(bins, y)

        bincontents, _d1, _d2 = np.histogram2d(x, y, [xedges, yedges])
        bincontents = np.transpose(bincontents)
        assert np.all(_d1 == xedges)
        assert np.all(_d2 == yedges)

        # statsbox
        self.stats_fields2d(i, bincontents, xcenters, ycenters)

        if o.density:
            bincontents = get_density2d(bincontents, xwidths, ywidths)

        if o.hidezero:
            bincontents[bincontents == 0] = np.nan

        if o.log:
            bincontents = np.log10(bincontents)
            formatter = mpl.ticker.FuncFormatter(func = lambda x, i:number_mathformat(np.power(10, x)))
        else:
            formatter = mpl.ticker.FuncFormatter(func = lambda x, i:number_mathformat(x))

        if 'color' in o.style:
            pargs = set_defaults(kwargs, cmap = 'jet', edgecolor = 'none')
            plt.pcolor(xedges, yedges, ma.array(bincontents, mask = np.isnan(bincontents)), **kwargs)

        elif 'box' in o.style:
            pargs = set_defaults(kwargs, color = (1, 1, 1, 0), marker = 's', edgecolor = 'k')
            n = bincontents.size
            s = bincontents.reshape(n)
            s = s / np.nanmax(s) * (72. / 2. * self.w / max(len(xcenters), len(ycenters))) ** 2
            xcenters, ycenters = np.meshgrid(xcenters, ycenters)
            plt.scatter(xcenters.reshape(n), ycenters.reshape(n), s = s, **pargs)

        elif 'contour' in o.style:
            pargs = set_defaults(kwargs, cmap = 'jet')
            if not isinstance(pargs['cmap'], mpl.colors.Colormap):
                pargs['cmap'] = mpl.cm.get_cmap(pargs['cmap'])

            if filled:
                cs = plt.contourf(xcenters, ycenters, bincontents, o.levels, **pargs)
            else:
                cs = plt.contour(xcenters, ycenters, bincontents, o.levels, **pargs)
                if o.clabels:
                    plt.clabel(cs, inline = 1)

        else:
            raise ValueError('unknown style ' + o.style)

        if o.colorbar:
            m = 6.0
            dmin, dmax = np.nanmin(bincontents), np.nanmax(bincontents)
            if o.log:
                dmin, dmax = np.ceil(dmin), np.floor(dmax) + 1
                step = max(1, np.floor((dmax - dmin) / m))
                cticks = np.arange(dmin, dmax, step)
            else:
                cticks = ticks.get_ticks(dmin, dmax, m, only_inside = 1)

            cb = plt.colorbar(fraction = o.cbfrac, pad = 0.01, aspect = 40, ticks = cticks, format = formatter)
            cb.set_label(o.cblabel)