def render(self, context): qs = context[self.var_name] tmp = StringIO.StringIO() P = [c.p_bary for c in qs.all() if c.p_bary > 0] cv = SVGCanvas(940, 550, background_color='white') if P: binned = bin_data_log(P, 200) pc = PlotContainer(0, -20, 950, 550, color='black', x_log=True) pc.bottom.set_label('Period (ms)') pc.top.hide_label() pc.left.set_label('Count') pc.right.hide_label() hp = HistogramPlotter(binned, color='black') pc.add_plotter(hp) cv.add_plot_container(pc) # write number of candidates shown: tf = TextFragment(50, 520, '(Showing %d candidates.)' % len(P), color='black', font_size=15) cv.add_plot_container(tf) else: tf = TextFragment(200, 200, 'No Candidates found.', color='red', font_size=50) cv.add_plot_container(tf) tmp = StringIO.StringIO() cv.draw(tmp) return tmp.getvalue()
def plot(filename, spr, options, pulse_trains, pt_idx, rejected_pulses, nlink, plink, marker_dms): ''' Create a pulsetrain diagnostic plot. Note: Call this function to create the plot, not others in this module. ''' # Find DM range for this plot: lodm, hidm = find_dm_range(spr.dms, pulse_trains[pt_idx]) # Flatten pulses into list of candidates (foreground, background): foreground = flatten_pulses(pulse_trains[pt_idx], lodm, hidm) background = flatten_pulses(rejected_pulses, lodm, hidm) for i in range(len(pulse_trains)): if i != pt_idx: background.extend(flatten_pulses(pulse_trains[i], lodm, hidm)) # Determine whether it is wise to switch to rasterized plotting (called # fallback). n_candidates = len(foreground) + len(background) if n_candidates > MAX_N_CANDIDATES: fallback = True else: fallback = False # Set up the overall canvas to wich all panels are plot: cv = SVGCanvas(1250, 750) # Add the three panels to the plot: plot_main_panel(cv, foreground, background, lodm, hidm, marker_dms, fallback) plot_snr_scatter(cv, foreground, background, lodm, hidm, marker_dms, fallback) plot_count_histogram(cv, spr.dms, foreground, background, lodm, hidm, marker_dms) # Add the text identifying the data set etc. to the diagnostic plot: add_text(cv, spr.md_map[spr.dms[0]], nlink, plink, n_candidates, fallback) add_settings_text(cv, options) add_trigger_text(cv, pulse_trains, pt_idx) # Finally write everything out to an SVG (xml) file. with open(filename, 'w') as f: cv.draw(f)
p = get_commandline_parser() options, args = p.parse_args() if len(args) == 0: p.print_usage() p.print_help() # Some custom checks: delays_file = check_delays_option(options, args, p) check_type_option(options, args, p) output_files = check_directories(options, args, p) for plot_i, searchoutdir, fn in output_files: print plot_i, searchoutdir, fn # Do some setting up and calculations: cv = SVGCanvas(1250, 760) try: print 'Scanning for .singlepulse and .inf files ...' spr = SinglePulseReaderCondensed(searchoutdir, options.s, options.e, delays_file, options.lo, options.hi) print '... done' except: raise datapath = os.path.abspath(searchoutdir) msg = 'Problem with data in %s, nothing present?' % datapath cv.add_plot_container(TextFragment(100, 100, msg, font_size=15)) else: print 'Number of DM-trials: %d' % len(spr.dms) tf = TextFragment(870, 600, spr.sp_map[spr.dms[0]][:-12]) cv.add_plot_container(tf) min_dmi, max_dmi = get_dmi_range(spr, options.dmspercell)
from brp.svg.plotters.symbol import RADECSymbol from brp.svg.plotters.symbol import RasterDebugSymbol from brp.svg.plotters.line import LinePlotter from brp.svg.plotters.scatter import ScatterPlotter from brp.svg.plotters.gradient import RGBGradient, GradientPlotter from brp.svg.plotters.crosshair import CrossHairPlotter from brp.svg.plotters.limit import XLimitPlotter, YLimitPlotter from brp.svg.plotters.legend import LegendPlotter, UNDER_PLOT, BOTTOMLEFT from brp.core.interval import stretch_interval from brp.svg.plotters.histogram import HistogramPlotter, bin_data from brp.svg.plotters.error import ErrorPlotter from brp.svg.plotters.linkbox import LinkBox if __name__ == '__main__': # Draw a simple scatter + line plot to SVG (dumped to standard out). cv = SVGCanvas(1000, 2500) c = PlotContainer(100, 100, 600, 400, background_color="white", x_log=True, y_log=True) TMP = [10 * x + 10 for x in range(99)] TMP2 = [10 * x + 10 for x in range(99)] TMP2.reverse() gr = RGBGradient((200, 800), (1, 0, 0), (0, 0, 1)) c.add_plotter( LinePlotter(TMP, TMP,
def render(self, context): qs = context[self.var_name] P = [c.p_bary for c in qs.all() if c.best_dm > 0] DM = [c.best_dm for c in qs.all() if c.best_dm > 0] REDCHISQ = [c.reduced_chi_sq for c in qs.all() if c.best_dm > 0] LINKS = [reverse('bestprof_detail', args=[c.pk]) for c in qs.all() if c.best_dm > 0] RA = [c.ra_deg for c in qs.all() if c.best_dm > 0] DEC = [c.dec_deg for c in qs.all() if c.best_dm > 0] cv = SVGCanvas(940, 550, background_color='white') if P: lo_dm = min(DM) max_dm = max(DM) pc = PlotContainer(0, -20, 880, 550, color='black', x_log=True, y_log=True, data_background_color='gray') gr = RGBGradient((lo_dm, max_dm), (0, 0, 1), (1, 0, 0)) scp = ScatterPlotter(P, REDCHISQ, RA, DEC, DM, gradient=gr, gradient_i=4, links=LINKS, symbol=RADECSymbol) pc.add_plotter(scp) # pc.top.hide_tickmarklabels() pc.top.hide_label() # pc.right.hide_tickmarklabels() pc.right.hide_label() pc.left.set_label('Reduced Chi Square') pc.bottom.set_label('Period (ms)') cv.add_plot_container(pc) # Gradient: pc = PlotContainer(820, -20, 120, 550, color='black', data_padding=0) pc.top.hide_label() pc.top.hide_tickmarks() pc.left.hide_tickmarks() pc.bottom.hide_label() pc.bottom.hide_tickmarks() pc.left.hide_label() pc.right.set_label('Dispersion Measure cm^-3 pc') pc.add_plotter(GradientPlotter(gr)) cv.add_plot_container(pc) # write number of candidates shown: tf = TextFragment(50, 520, '(Showing %d candidates.)' % len(P), color='black', font_size=15) cv.add_plot_container(tf) else: tf = TextFragment(200, 200, 'No Candidates found.', color='red', font_size=50) cv.add_plot_container(tf) tmp = StringIO.StringIO() cv.draw(tmp) return tmp.getvalue()
from brp.svg.plotters.symbol import RADECSymbol from brp.svg.plotters.symbol import RasterDebugSymbol from brp.svg.plotters.line import LinePlotter from brp.svg.plotters.scatter import ScatterPlotter from brp.svg.plotters.gradient import RGBGradient, GradientPlotter from brp.svg.plotters.crosshair import CrossHairPlotter from brp.svg.plotters.limit import XLimitPlotter, YLimitPlotter from brp.svg.plotters.legend import LegendPlotter, UNDER_PLOT, BOTTOMLEFT from brp.core.interval import stretch_interval from brp.svg.plotters.histogram import HistogramPlotter, bin_data from brp.svg.plotters.error import ErrorPlotter from brp.svg.plotters.linkbox import LinkBox if __name__ == "__main__": # Draw a simple scatter + line plot to SVG (dumped to standard out). cv = SVGCanvas(1000, 2500) c = PlotContainer(100, 100, 600, 400, background_color="white", x_log=True, y_log=True) TMP = [10 * x + 10 for x in range(99)] TMP2 = [10 * x + 10 for x in range(99)] TMP2.reverse() gr = RGBGradient((200, 800), (1, 0, 0), (0, 0, 1)) c.add_plotter(LinePlotter(TMP, TMP, gradient=gr, gradient_i=1, symbol=BaseSymbol, linepattern="2 2 8 2")) c.add_plotter(ScatterPlotter(TMP, TMP2, symbol=BaseSymbol, gradient=gr, gradient_i=0)) c.add_plotter(CrossHairPlotter(500, 500)) c.add_plotter(CrossHairPlotter(200, 700, color="red")) c.add_plotter(XLimitPlotter(300)) c.add_plotter(YLimitPlotter(300)) c.add_plotter(CrossHairPlotter(1e6, 1e6)) c.add_plotter(ScatterPlotter([1000], [1000], symbol=CrossHairSymbol, color="lime")) c.add_plotter(LinkBox((100, 100, 10000, 10000), "http://www.slashdot.org"))
''' Some tests of the rasterized fallback mode. ''' import sys import random from brp.svg.base import SVGCanvas from brp.svg.base import PlotContainer from brp.svg.plotters.scatter import ScatterPlotter from brp.svg.plotters.error import ErrorPlotter from brp.svg.plotters.symbol import RADECSymbol, SquareSymbol from brp.svg.plotters.line import LinePlotter if __name__ == '__main__': cv = SVGCanvas(1200, 1800) # ------------------------------------------------------ # -- For debugging RADECSymbol raster fallback --------- NPULSARS = 100 P = [random.uniform(0.001, 10) for i in range(NPULSARS)] DM = [random.uniform(1, 100) for i in range(NPULSARS)] RA = [random.uniform(0, 24) for i in range(NPULSARS)] DEC = [random.uniform(-90, 90) for i in range(NPULSARS)] SIGMA = [random.uniform(2, 15) for i in range(NPULSARS)] # plot pc = PlotContainer(0, 0, 600, 400) pc.add(ScatterPlotter(P, DM, RA, DEC, SIGMA, symbol=RADECSymbol)) cv.add(pc)
def render(self, context): qs = context[self.var_name] tmp = StringIO.StringIO() cv = SVGCanvas(940, 550, background_color='white') P = [c.p_bary for c in qs.all() if c.best_dm > 0] DM = [c.best_dm for c in qs.all() if c.best_dm > 0] REDCHISQ = [c.reduced_chi_sq for c in qs.all() if c.best_dm > 0] LINKS = [ reverse('bestprof_detail', args=[c.pk]) for c in qs.all() if c.best_dm > 0 ] RA = [c.ra_deg for c in qs.all() if c.best_dm > 0] DEC = [c.dec_deg for c in qs.all() if c.best_dm > 0] if P: max_redchisq = max(REDCHISQ) min_redchisq = min(REDCHISQ) # Main panel showing candidate period-DM scatter plot: pc = PlotContainer(0, -20, 880, 550, color='black', x_log=True, y_log=True, data_background_color='gray') pc.bottom.set_label('Period (ms)') pc.top.hide_label() pc.left.set_label('Dispersion Measure (cm^-3 pc)') pc.right.hide_label() gr = RGBGradient((min_redchisq, max_redchisq), (0, 0, 1), (1, 0, 0)) scp = ScatterPlotter(P, DM, RA, DEC, REDCHISQ, gradient=gr, gradient_i=4, links=LINKS, symbol=RADECSymbol) pc.add_plotter(scp) cv.add_plot_container(pc) # Gradient: pc = PlotContainer(820, -20, 120, 550, color='black', data_padding=0) pc.top.hide_label() pc.top.hide_tickmarks() pc.left.hide_tickmarks() pc.bottom.hide_label() pc.bottom.hide_tickmarks() pc.left.hide_label() pc.right.set_label('Candidate Reduced Chi-Square') pc.add_plotter(GradientPlotter(copy.deepcopy(scp.gradient))) cv.add_plot_container(pc) # write number of candidates shown: tf = TextFragment(50, 520, '(Showing %d candidates.)' % len(P), color='black', font_size=15) cv.add_plot_container(tf) else: tf = TextFragment(200, 200, 'No Candidates found.', color='red', font_size=50) cv.add_plot_container(tf) cv.draw(tmp) return tmp.getvalue()
''' from __future__ import division import sys from random import random as r from brp.svg.base import SVGCanvas, PlotContainer from brp.svg.plotters import newhist2d from brp.svg.plotters.gradient import GradientPlotter from brp.svg.plotters.line import LinePlotter if __name__ == '__main__': x_data = [r() for i in range(1000)] y_data = [r() for i in range(1000)] cv = SVGCanvas(1200, 800) p = PlotContainer(0, 0, 600, 400) h = newhist2d.Histogram2dPlotter(x_data, y_data, x_bins=100) gr = h.get_gradient() collapsed_x, yvalues = h.collapse_x() xvalues, collapsed_y = h.collapse_y() p.add(h) cv.add(p) p = PlotContainer(800, 0, 160, 400) p.add(GradientPlotter(gr, 'vertical')) cv.add(p) p = PlotContainer(600, 0, 200, 400) p.add(LinePlotter(collapsed_x, yvalues))
'%.2f ms' % bpf.header.p_bary[0], alignment='start', font_size=TEXTSIZE)) cv.add_plot_container(TextFragment(x + width, y + height + 2 * TEXTSIZE, '%.3f pc cm^-3' % bpf.header.best_dm, alignment='end', font_size=TEXTSIZE)) files = os.listdir(sys.argv[1]) tmp = [] for f in files: if f.endswith('.bestprof'): try: bpf = BestprofFile(os.path.join(sys.argv[1], f)) except: print 'There was a problem reading %s' % os.path.join(sys.argv[1], f) else: tmp.append(bpf) tmp.sort(key=lambda x: x.header.reduced_chi_sq) tmp.reverse() cv = SVGCanvas(1140, 1240) N_HORIZONTAL = 5 N_VERTICAL = 10 for ix in range(N_HORIZONTAL): for iy in range(N_VERTICAL): try: plot_profile(cv, 220 * ix + 20, 140 * iy + 20, 200, 100, tmp[ix + N_HORIZONTAL * iy], psr_name='B0000+0000') except IndexError, e: pass cv.draw('test234.xml')
import sys #sys.path.append('../') from brp.svg.base import SVGCanvas, PlotContainer, TextFragment from brp.svg.plotters.histogram import HistogramPlotter, merge_bins if __name__ == '__main__': bins = [(0, 1, 10), (1, 2, 10), (2, 3, 11), (3, 4, 10), (4, 5, 10), (5, 6, 7)] cv = SVGCanvas(1200, 1200) # Histogram testing: c = PlotContainer(0, 0, 600, 400) c.add(HistogramPlotter(bins)) cv.add(c) cv.add(TextFragment(100, 100, 'Unmerged bins', color='red')) merged = merge_bins(bins) c2 = PlotContainer(600, 0, 600, 400) c2.add(HistogramPlotter(merged)) cv.add(c2) cv.add(TextFragment(700, 100, 'Merged bins', color='red')) cv.draw(sys.stdout)
files = os.listdir(sys.argv[1]) tmp = [] for f in files: if f.endswith('.bestprof'): try: bpf = BestprofFile(os.path.join(sys.argv[1], f)) except: print 'There was a problem reading %s' % os.path.join( sys.argv[1], f) else: tmp.append(bpf) tmp.sort(key=lambda x: x.header.reduced_chi_sq) tmp.reverse() cv = SVGCanvas(1140, 1240) N_HORIZONTAL = 5 N_VERTICAL = 10 for ix in range(N_HORIZONTAL): for iy in range(N_VERTICAL): try: plot_profile(cv, 220 * ix + 20, 140 * iy + 20, 200, 100, tmp[ix + N_HORIZONTAL * iy], psr_name='B0000+0000') except IndexError, e: pass cv.draw('test234.xml')