def get_context_data(self, **kwargs): context = super(ImagePlot, self).get_context_data(**kwargs) context['sources'] = self.object.extractedsources.all() try: context['size'] = int(self.request.GET.get('size', 5)) except ValueError: context['size'] = 5 context['hdu'] = get_hdu(self.object.url) return context
def extracted_sources_pixels(image, size): """ :param image: a banana.models.Image object :returns: a list of sources of an image """ hdu = get_hdu(image.url) if not hdu: return None # make an image fig = pyplot.figure(figsize=(size, size)) plot = aplpy.FITSFigure(hdu, figure=fig, subplot=[0, 0, 1, 1], auto_refresh=False) # get source info from database sources = image.extractedsources.all() ids = [source.id for source in sources] # If the image has a reference declination pointing to the north # celestial pole (ie, CRVAL2=90), our APLpy will incorrectly plot them # with an RA 180 degrees wrong. We rotate them back here. See Trap # issue #4599 for (much) more discussion. if "CRVAL2" in hdu[0].header and hdu[0].header["CRVAL2"] == 90: x_world = [(source.ra + 180) % 360 for source in sources] else: x_world = [source.ra for source in sources] y_world = [source.decl for source in sources] w_world = numpy.array([source.semimajor / 900 for source in sources]) h_world = numpy.array([source.semiminor / 900 for source in sources]) # first convert positions to matplotlib image coordinates x_plot, y_plot = plot.world2pixel(x_world, y_world) arcperpix = aplpy.wcs_util.arcperpix(plot._wcs) w_plot = 3600.0 * w_world / arcperpix h_plot = 3600.0 * h_world / arcperpix # then transform them to true pixel coordinates ax = fig.axes[0] xy_pixels = ax.transData.transform(numpy.vstack([x_plot, y_plot]).T) x_px, y_px = xy_pixels.T # In matplotlib, 0,0 is the lower left corner, whereas it's usually the # upper right for most image software, so we'll flip the y-coords fig_width, fig_height = fig.canvas.get_width_height() y_px = fig_height - y_px # because of an unknown reason we need to scale the coordinates with 25% y_px *= 1.25 x_px *= 1.25 # create average size since areamap can only draw circles size_px = (w_plot + h_plot) / 4 return zip(ids, list(x_px), list(y_px), list(size_px))
def extracted_sources_pixels(image, size): """ :param image: a banana.models.Image object :returns: a list of sources of an image """ hdu = get_hdu(image.url) if not hdu: return None # make an image fig = pyplot.figure(figsize=(size, size)) plot = aplpy.FITSFigure(hdu, figure=fig, subplot=[0, 0, 1, 1], auto_refresh=False) # get source info from database sources = image.extractedsources.all() ids = [source.id for source in sources] x_world = numpy.array([source.ra for source in sources]) y_world = numpy.array([source.decl for source in sources]) w_world = numpy.array([source.semimajor / 900 for source in sources]) h_world = numpy.array([source.semiminor / 900 for source in sources]) # first convert positions to matplotlib image coordinates x_plot, y_plot = plot.world2pixel(x_world, y_world) arcperpix = aplpy.wcs_util.arcperpix(plot._wcs) w_plot = 3600.0 * w_world / arcperpix h_plot = 3600.0 * h_world / arcperpix # then transform them to true pixel coordinates ax = fig.axes[0] xy_pixels = ax.transData.transform(numpy.vstack([x_plot, y_plot]).T) x_px, y_px = xy_pixels.T # In matplotlib, 0,0 is the lower left corner, whereas it's usually the # upper right for most image software, so we'll flip the y-coords fig_width, fig_height = fig.canvas.get_width_height() y_px = fig_height - y_px # because of an unknown reason we need to scale the coordinates with 25% y_px *= 1.25 x_px *= 1.25 # create average size since areamap can only draw circles size_px = (w_plot + h_plot) / 4 return zip(ids, list(x_px), list(y_px), list(size_px))
def get_context_data(self, **kwargs): context = super(ExtractedSourcePlot, self).get_context_data(**kwargs) context['size'] = int(self.request.GET.get('size', 1)) context['hdu'] = get_hdu(self.object.image.url) return context