示例#1
0
def test_regions_spectral(data_adv):
    cube, data = cube_and_raw(data_adv)
    rf_cube = get_rest_value_from_wcs(cube.wcs).to("GHz",
                                                   equivalencies=u.spectral())

    # content of image.reg
    regpix = regions.RectanglePixelRegion(regions.PixCoord(0.5, 1),
                                          width=4,
                                          height=2)

    # Velocity range in doppler_optical same as that of the cube.
    vel_range_optical = u.Quantity([-318 * u.km / u.s, -320 * u.km / u.s])
    regpix.meta['range'] = list(vel_range_optical)
    sc1 = cube.subcube_from_regions([regpix])
    scsum1 = sc1.sum()

    freq_range = vel_range_optical.to("GHz",
                                      equivalencies=u.doppler_optical(rf_cube))
    regpix.meta['range'] = list(freq_range)
    sc2 = cube.subcube_from_regions([regpix])
    scsum2 = sc2.sum()

    regpix.meta['restfreq'] = rf_cube
    vel_range_gamma = freq_range.to("km/s",
                                    equivalencies=doppler_gamma(rf_cube))
    regpix.meta['range'] = list(vel_range_gamma)
    regpix.meta['veltype'] = 'GAMMA'
    sc3 = cube.subcube_from_regions([regpix])
    scsum3 = sc3.sum()

    vel_range_beta = freq_range.to("km/s", equivalencies=doppler_beta(rf_cube))
    regpix.meta['range'] = list(vel_range_beta)
    regpix.meta['veltype'] = 'BETA'
    sc4 = cube.subcube_from_regions([regpix])
    scsum4 = sc4.sum()

    vel_range_z = freq_range.to("km/s", equivalencies=doppler_z(rf_cube))
    regpix.meta['range'] = list(vel_range_z)
    regpix.meta['veltype'] = 'Z'
    sc5 = cube.subcube_from_regions([regpix])
    scsum5 = sc5.sum()

    dsum = data[1:-1, 1, :].sum()
    assert_allclose(scsum1, dsum)
    # Proves that the vel/freq conversion works
    assert_allclose(scsum1, scsum2)
    assert_allclose(scsum2, scsum3)
    assert_allclose(scsum3, scsum4)
    assert_allclose(scsum4, scsum5)
示例#2
0
def create_region(rtype, args, dx, dy):
    if rtype in ["Rectangle", "Box"]:
        xctr, yctr, xw, yw = args
        x = xctr+dx
        y = yctr+dy
        center = regions.PixCoord(x=x, y=y)
        reg = regions.RectanglePixelRegion(center=center, width=xw, height=yw)
        bounds = [x-0.5*xw, x+0.5*xw, y-0.5*yw, y+0.5*yw]
    elif rtype == "Circle":
        xctr, yctr, radius = args
        x = xctr+dx
        y = yctr+dy
        center = regions.PixCoord(x=x, y=y)
        reg = regions.CirclePixelRegion(center=center, radius=radius)
        bounds = [x-radius, x+radius, y-radius, y+radius]
    elif rtype == "Polygon":
        x = np.array(args[0])+dx
        y = np.array(args[1])+dy
        vertices = regions.PixCoord(x=x, y=y)
        reg = regions.PolygonPixelRegion(vertices=vertices)
        bounds = [x.min(), x.max(), y.min(), y.max()]
    else:
        raise NotImplementedError
    return reg, bounds
示例#3
0
def ginga_canvas_object_to_astropy_region(obj):
    """
    Convert a Ginga canvas object to an AstroPy region object.

    Parameters
    ----------
    obj : subclass of `~ginga.canvas.CanvasObject`
        The Ginga canvas object to be converted

    Returns
    -------
    r : subclass of `~regions.PixelRegion`
        The corresponding AstroPy region object

    """
    if not HAVE_REGIONS:
        raise ValueError(
            "Please install the Astropy 'regions' package to use this function"
        )

    dc = get_canvas_types()
    r = None

    if isinstance(obj, (dc.Circle, )):
        r = regions.CirclePixelRegion(center=regions.PixCoord(x=obj.x,
                                                              y=obj.y),
                                      radius=obj.radius)

    elif isinstance(obj, (dc.Ellipse, )):
        r = regions.EllipsePixelRegion(center=regions.PixCoord(x=obj.x,
                                                               y=obj.y),
                                       width=obj.xradius * 2,
                                       height=obj.yradius * 2,
                                       angle=obj.rot_deg * u.deg)

    elif isinstance(obj, (dc.Text, )):
        r = regions.TextPixelRegion(center=regions.PixCoord(x=obj.x, y=obj.y),
                                    text=obj.text)
        r.visual['textangle'] = str(obj.rot_deg)

    elif isinstance(obj, (dc.Point, )):
        r = regions.PointPixelRegion(center=regions.PixCoord(x=obj.x, y=obj.y))
        style = pt_ginga.get(obj.style, '*')
        r.visual['symbol'] = style

    elif isinstance(obj, (dc.Line, )):
        r = regions.LinePixelRegion(start=regions.PixCoord(x=obj.x1, y=obj.y1),
                                    end=regions.PixCoord(x=obj.x2, y=obj.y2))

    elif isinstance(obj, (dc.Box, )):
        r = regions.RectanglePixelRegion(center=regions.PixCoord(x=obj.x,
                                                                 y=obj.y),
                                         width=obj.xradius * 2,
                                         height=obj.yradius * 2,
                                         angle=obj.rot_deg * u.deg)

    elif isinstance(obj, (dc.Polygon, )):
        x, y = np.asarray(obj.points).T
        r = regions.PolygonPixelRegion(vertices=regions.PixCoord(x=x, y=y))

    elif isinstance(obj, (dc.Annulus, )) and obj.atype == 'circle':
        rin = obj.radius
        rout = rin + obj.width
        r = regions.CircleAnnulusPixelRegion(center=regions.PixCoord(x=obj.x,
                                                                     y=obj.y),
                                             inner_radius=rin,
                                             outer_radius=rout)

    elif isinstance(obj, (dc.Annulus2R, )) and obj.atype == 'ellipse':
        r = regions.EllipseAnnulusPixelRegion(
            center=regions.PixCoord(x=obj.x, y=obj.y),
            inner_width=obj.xradius * 2,
            inner_height=obj.yradius * 2,
            outer_width=obj.xradius * 2 + obj.xwidth * 2,
            outer_height=obj.yradius * 2 + obj.ywidth * 2,
            angle=obj.rot_deg * u.deg)

    elif isinstance(obj, (dc.Annulus2R, )) and obj.atype == 'box':
        r = regions.RectangleAnnulusPixelRegion(
            center=regions.PixCoord(x=obj.x, y=obj.y),
            inner_width=obj.xradius * 2,
            inner_height=obj.yradius * 2,
            outer_width=obj.xradius * 2 + obj.xwidth * 2,
            outer_height=obj.yradius * 2 + obj.ywidth * 2,
            angle=obj.rot_deg * u.deg)

    else:
        raise ValueError("Don't know how to convert this object")

    # Set visual styling attributes
    r.visual['color'] = obj.color

    if hasattr(obj, 'font'):
        r.visual['font'] = obj.font
        if obj.fontsize is not None:
            r.visual['fontsize'] = str(obj.fontsize)

    if hasattr(obj, 'linewidth'):
        r.visual['linewidth'] = obj.linewidth

    if hasattr(obj, 'fill'):
        r.visual['fill'] = obj.fill = r.visual.get('fill', False)

    # Limited support for other metadata
    r.meta['edit'] = 1 if obj.editable else 0
    meta = obj.get_data()
    if meta is not None and meta.get('name', None) is not None:
        r.meta['name'] = meta.get('name')

    return r