Created
February 19, 2014 11:36
-
-
Save cdeil/9090342 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class GPSPlot(object): | |
"""Handles looping over the slices. | |
You use this class by subclassing from it | |
and implementing the following two hooks: | |
#- init() -- file loading, option setting <- That's the way it should work!!! | |
- main() -- main plotting | |
- post() -- postprocessing | |
""" | |
def __init__(self, plot_slices=None, nslices=4): | |
self.plot_slices = plot_slices if plot_slices else range(nslices) | |
self.nslices = nslices | |
self.plot_pars = dict() | |
self.colorbar_pars = dict() | |
# self.colorbar_position | |
def main(self): | |
"""Main plotting hook""" | |
raise NotImplementedError | |
def post(self): | |
"""Post-processing hook""" | |
# Examples | |
# Use cmap = jet, gist_gray, gist_heat, gist_rainbow or hot | |
# self.fig.show_colorscale(**self.plot_pars) | |
def bottom(self): | |
logging.info('Adding colorbar') | |
self.fig.add_colorbar(**self.colorbar_pars) | |
self.fig.colorbar.set_font(size='small') | |
self.fig.colorbar._colorbar.set_label(self.colorbar_label) | |
def draw(self): | |
figsize, nslices, centers, boxes, width, height = self.stripe_geometry(self.nslices) | |
self.figure = plt.figure(figsize=figsize) | |
# Loop over slices | |
for ii, center, self.box in zip(range(nslices), centers, boxes): | |
if ii not in self.plot_slices: | |
logging.info('Skipping slice %s, center %s, box %s' % | |
(ii, center, self.box)) | |
continue | |
else: | |
logging.info('Processing slice %s, center %s, box %s' % | |
(ii, center, self.box)) | |
# self.fig.set_auto_refresh(False) | |
self.main() | |
self.fig.recenter(center[0], center[1], width=width, height=height) | |
#self.fig.set_theme('publication') | |
self.fig.axis_labels.hide() | |
self.fig.ticks.set_xspacing(5) | |
self.fig.ticks.set_yspacing(1) | |
self.fig.tick_labels.set_xformat('dd') | |
self.fig.tick_labels.set_yformat('dd') | |
self.fig.tick_labels.set_style('colons') | |
self.fig.tick_labels.set_font(size='small') | |
self.post() | |
if ii == 0: | |
logging.info('Processing bottom slice specific stuff') | |
try: | |
self.bottom() | |
except AttributeError: | |
logging.info('Nothing to do here.') | |
if ii == (nslices - 1): | |
logging.info('Processing top slice specific stuff') | |
try: | |
self.top() | |
except AttributeError: | |
logging.info('Nothing to do here.') | |
# self.fig.refresh() | |
return self | |
def save(self): | |
logging.info('CWD: {}'.format(os.getcwd())) | |
logging.info('Writing {}'.format(self.outfile)) | |
self.figure.savefig(self.outfile) | |
@staticmethod | |
def stripe_geometry(self, nslices=4, center=[-19, 0], fov=[188, 7.99], | |
xsize=15, xborder=1, yborder=0.5, yspacing=0.2): | |
""" | |
Old center [-20, 0] | |
Old fov [200,7.99] | |
This function computes all relevant quantities to plot | |
a very wide survey map in n slices. | |
This is surprisingly complicated because coordinates are | |
relative to figsize, which is already not 1:1. | |
nslices: the number of slices you want | |
center: FITS map center position | |
fov: full-width and full-height of the FITS map | |
xsize: width of the figure in inches | |
xborder: free space to x border in inches | |
yborder: free space to y border in inches | |
yspacing: free space between slices in inches""" | |
# Need floats for precise divisions | |
center[0] = float(center[0]) | |
center[1] = float(center[1]) | |
fov[0] = float(fov[0]) | |
fov[1] = float(fov[1]) | |
xsize = float(xsize) | |
xborder = float(xborder) | |
yborder = float(yborder) | |
yspacing = float(yspacing) | |
# Width and height in deg of a slice | |
width = fov[0] / nslices | |
height = fov[1] | |
# Aspect ratio y:x of a slice | |
aspectratio = fov[1] / (fov[0] / nslices) | |
# Absolute figure dimensions | |
ysize = 2 * yborder + (nslices - 1) * yspacing + \ | |
nslices * aspectratio * (xsize - 2 * xborder) | |
figsize = [xsize, ysize] | |
# Relative slice subplot dimensions | |
dx = 1 - 2 * xborder / xsize | |
dy = aspectratio * dx * xsize / ysize | |
dyspacing = yspacing / ysize | |
# List of y slice offsets | |
boxes = [] | |
box_centers = [] | |
for ii in range(nslices): | |
box_center = [center[0] - fov[0] / 2 + (ii + 0.5) * width, center[1]] | |
box = [xborder / xsize, yborder / ysize + ii * (dy + dyspacing), | |
dx, dy] | |
logging.info('Processing slice %s, center %s, box %s' % (ii, box_center, box)) | |
box_centers.append(box_center) | |
boxes.append(box) | |
logging.info('aspectratio: %s' % aspectratio) | |
logging.info('dx: %s %s' % (dx, dx * xsize)) | |
logging.info('dy: %s %s' % (dy, dy * ysize)) | |
logging.info('dyspacing: %s %s' % (dyspacing, dyspacing * ysize)) | |
logging.info('figsize: %s' % figsize) | |
logging.info('width: %s' % width) | |
logging.info('height: %s' % height) | |
return figsize, nslices, box_centers, boxes, width, height | |
class SignificanceGPSPlot(GPSPlot): | |
def __init__(self, infile, | |
contour_levels=None, contour_color='black', | |
regions=None, regions_color='black', | |
plot_slices=None, nslices=4): | |
self.infile = infile | |
self.contour_levels = contour_levels | |
self.contour_color = contour_color | |
self.regions = regions | |
self.regions_color = regions_color | |
super(SignificanceGPSPlot, self).__init__(plot_slices, nslices) | |
self.plot_pars.update(vmin=-5, vmid=7, stretch='sqrt', cmap='jet') | |
def main(self): | |
self.fig = FITSFigure(self.infile, figure=self.figure, subplot=self.box) | |
self.fig.show_colorscale(**self.plot_pars) | |
if self.contour_levels: | |
self.fig.show_contour(self.infile, levels=self.contour_levels, | |
colors=self.contour_color, smooth=7, linewidths=1) | |
if self.regions: | |
self.fig.show_regions(self.regions) | |
# Now you can customize the regions ... | |
layer = self.fig.get_layer('region_set_1') | |
for artist in layer.artistlist: | |
# artist.set_facecolor('orange') | |
artist.set_edgecolor(self.regions_color) | |
# artist.set_alpha(0.5) | |
# to hide text lables: | |
# f.hide_layer('region_set_1_txt') | |
layer = self.fig.get_layer('region_set_1_txt') | |
for artist in layer.artistlist: | |
# ACCEPTS: [ size in points | 'xx-small' | 'x-small' | 'small' | 'medium' | 'large' | 'x-large' | 'xx-large' ] | |
artist.set_size('small') | |
artist.set_color(self.regions_color) | |
# artist.set_fontsize(10) | |
# artist.set_weight('bold') | |
self.fig.refresh() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment