def posterior_predictions_plot(plottype, callback, analyzer, transformations, nsamples=None): """ Internal Routine used by posterior_predictions_unconvolved, posterior_predictions_convolved """ posterior = analyzer.get_equal_weighted_posterior() # for plotting, we don't need so many points, and especially the # points that barely made it into the analysis are not that interesting. # so pick a random subset of at least nsamples points if nsamples is not None and len(posterior) > nsamples: if hasattr(numpy.random, 'choice'): chosen = numpy.random.choice(numpy.arange(len(posterior)), replace=False, size=nsamples) else: chosen = list( set(numpy.random.randint(0, len(posterior), size=10 * nsamples)))[:nsamples] posterior = posterior[chosen, :] assert len(posterior) == nsamples prefix = analyzer.outputfiles_basename tmpfilename = '%s-wdatatmp.qdp' % prefix olddevice = Plot.device Plot.device = '/null' modelnames = set([t['model'].name for t in transformations]) while len(Plot.commands) > 0: Plot.delCommand(1) Plot.addCommand('wdata "%s"' % tmpfilename.replace('.qdp', '')) oldchatter = Xset.chatter, Xset.logChatter Xset.chatter, Xset.logChatter = 0, 0 # plot models widgets = [progressbar.Percentage()] if hasattr(progressbar, 'Counter'): widgets += [progressbar.Counter('%5d')] widgets += [progressbar.Bar(), progressbar.ETA()] pbar = progressbar.ProgressBar(widgets=widgets, maxval=len(posterior)).start() for k, row in enumerate(posterior): set_parameters(values=row[:-1], transformations=transformations) if os.path.exists(tmpfilename): os.remove(tmpfilename) xspec.Plot(plottype) content = numpy.genfromtxt(tmpfilename, skip_header=3) os.remove(tmpfilename) callback(content) pbar.update(k) pbar.finish() Xset.chatter, Xset.logChatter = oldchatter xspec.Plot.device = olddevice
def posterior_predictions_plot(plottype, callback, analyzer, transformations, nsamples = None): """ Internal Routine used by posterior_predictions_unconvolved, posterior_predictions_convolved """ posterior = analyzer.get_equal_weighted_posterior() # for plotting, we don't need so many points, and especially the # points that barely made it into the analysis are not that interesting. # so pick a random subset of at least nsamples points if nsamples is not None and len(posterior) > nsamples: if hasattr(numpy.random, 'choice'): chosen = numpy.random.choice( numpy.arange(len(posterior)), replace=False, size=nsamples) else: chosen = list(set(numpy.random.randint(0, len(posterior), size=10*nsamples)))[:nsamples] posterior = posterior[chosen,:] assert len(posterior) == nsamples prefix = analyzer.outputfiles_basename tmpfilename = '%s-wdatatmp.qdp' % prefix olddevice = Plot.device Plot.device = '/null' modelnames = set([t['model'].name for t in transformations]) while len(Plot.commands) > 0: Plot.delCommand(1) Plot.addCommand('wdata "%s"' % tmpfilename.replace('.qdp', '')) oldchatter = Xset.chatter, Xset.logChatter Xset.chatter, Xset.logChatter = 0, 0 # plot models widgets = [progressbar.Percentage()] if hasattr(progressbar, 'Counter'): widgets += [progressbar.Counter('%5d')] widgets += [progressbar.Bar(), progressbar.ETA()] pbar = progressbar.ProgressBar(widgets=widgets, maxval=len(posterior)).start() for k, row in enumerate(posterior): set_parameters(values=row[:-1], transformations=transformations) if os.path.exists(tmpfilename): os.remove(tmpfilename) xspec.Plot(plottype) content = numpy.genfromtxt(tmpfilename, skip_header=3) os.remove(tmpfilename) callback(content) pbar.update(k) pbar.finish() Xset.chatter, Xset.logChatter = oldchatter xspec.Plot.device = olddevice
def qq(analyzer, markers = 5, annotate = True): """ Create a quantile-quantile plot for model discovery (deviations in data from model). The current data and model is used, so call *set_best_fit(analyzer, transformations)* before, to get the qq plot at the best fit. * markers: list of energies/channels (whichever the current plotting xaxis unit) or number of equally spaced markers between minimum+maximum. * annotate: add information to the plot """ olddevice = Plot.device prefix = analyzer.outputfiles_basename Plot.device = '/null' tmpfilename = '%s-wdatatmp.qdp' % prefix while len(Plot.commands) > 0: Plot.delCommand(1) Plot.addCommand('wdata "%s"' % tmpfilename.replace('.qdp', '')) if os.path.exists(tmpfilename): os.remove(tmpfilename) Plot("counts") content = numpy.genfromtxt(tmpfilename, skip_header=3) #os.remove(tmpfilename) bins, width, data, dataerror, model = content[:,0:5].transpose() if not hasattr(markers, '__len__'): nmarkers = int(markers) decimals = int(-numpy.log10(bins[-1] - bins[0] + 1e-4)) markers = numpy.linspace(bins[0], bins[-1], nmarkers+2)[1:-1] markers = set(numpy.round(markers, decimals=decimals)) # make qq plot, with 1:1 line qq_plot(bins=bins, data=data * width * 2, model=model * width * 2, markers = markers, annotate = annotate, unit=Plot.xAxis) Plot.device = olddevice
def posterior_predictions_plot(self, plottype, callback, nsamples=None): """ Internal Routine used by posterior_predictions_unconvolved, posterior_predictions_convolved """ posterior = self.posterior # for plotting, we don't need so many points, and especially the # points that barely made it into the analysis are not that interesting. # so pick a random subset of at least nsamples points if nsamples is not None and len(posterior) > nsamples: if hasattr(numpy.random, 'choice'): chosen = numpy.random.choice(numpy.arange(len(posterior)), replace=False, size=nsamples) else: chosen = list( set( numpy.random.randint(0, len(posterior), size=10 * nsamples)))[:nsamples] posterior = posterior[chosen, :] assert len(posterior) == nsamples prefix = self.outputfiles_basename tmpfilename = os.path.join( os.path.dirname(prefix), os.path.basename(prefix).replace('.', '_') + '-wdatatmp.qdp') if os.path.exists(tmpfilename): os.remove(tmpfilename) with XSilence(): olddevice = Plot.device Plot.device = '/null' #modelnames = set([t['model'].name for t in transformations]) while len(Plot.commands) > 0: Plot.delCommand(1) Plot.addCommand('wdata "%s"' % tmpfilename.replace('.qdp', '')) # plot models for k, row in enumerate(tqdm(posterior, disable=None)): set_parameters(values=row, transformations=self.transformations) if os.path.exists(tmpfilename): os.remove(tmpfilename) xspec.Plot(plottype) content = numpy.genfromtxt(tmpfilename, skip_header=3) os.remove(tmpfilename) callback(content) xspec.Plot.device = olddevice while len(Plot.commands) > 0: Plot.delCommand(1) if os.path.exists(tmpfilename): os.remove(tmpfilename)
def posterior_predictions_plot(plottype, callback, analyzer, transformations, nsamples = None): """ Internal Routine used by posterior_predictions_unconvolved, posterior_predictions_convolved """ posterior = analyzer.get_equal_weighted_posterior() # for plotting, we don't need so many points, and especially the # points that barely made it into the analysis are not that interesting. # so pick a random subset of at least nsamples points if nsamples is not None and len(posterior) > nsamples: if hasattr(numpy.random, 'choice'): chosen = numpy.random.choice( numpy.arange(len(posterior)), replace=False, size=nsamples) else: chosen = list(set(numpy.random.randint(0, len(posterior), size=10*nsamples)))[:nsamples] posterior = posterior[chosen,:] assert len(posterior) == nsamples prefix = analyzer.outputfiles_basename tmpfilename = os.path.join(os.path.dirname(prefix), os.path.basename(prefix).replace('.','_') + '-wdatatmp.qdp') olddevice = Plot.device Plot.device = '/null' #modelnames = set([t['model'].name for t in transformations]) while len(Plot.commands) > 0: Plot.delCommand(1) Plot.addCommand('wdata "%s"' % tmpfilename.replace('.qdp', '')) oldchatter = Xset.chatter, Xset.logChatter Xset.chatter, Xset.logChatter = 0, 0 # plot models for k, row in enumerate(tqdm(posterior, disable=None)): set_parameters(values=row[:-1], transformations=transformations) if os.path.exists(tmpfilename): os.remove(tmpfilename) xspec.Plot(plottype) content = numpy.genfromtxt(tmpfilename, skip_header=3) os.remove(tmpfilename) callback(content) Xset.chatter, Xset.logChatter = oldchatter xspec.Plot.device = olddevice while len(Plot.commands) > 0: Plot.delCommand(1)