Exemple #1
0
 def test_Polygon(self):
     """ # ROISelector.Polygon """
     # from list
     print(">>> {}".format(self.points))
     p = ROISelector.Polygon(self.points, **self.v)
     self.assertIsInstance(p, ROISelector.Polygon)
     self.assertEqual(p, self.polygon)
     self.assertEqual(p.N, 8)
     self.assertEqual(p.area, 3.625)
     self.assertGreater(p.is_ccw, 0)
     self.assertFalse(p.is_closed)
     # from tuple
     print(">>> {}".format(tuple(self.points)))
     p = ROISelector.Polygon(*self.points, **self.v)
     self.assertIsInstance(p, ROISelector.Polygon)
     self.assertEqual(p, self.polygon)
     self.assertEqual(p.N, 8)
     self.assertEqual(p.area, 3.625)
     self.assertGreater(p.is_ccw, 0)
     self.assertFalse(p.is_closed)
     # from single point
     print(">>> {}".format((1, 1)))
     p = ROISelector.Polygon((1, 1), **self.v)
     self.assertIsInstance(p, ROISelector.Polygon)
     self.assertEqual(p.N, 1)
     self.assertEqual(p.area, 0)
     self.assertEqual(p.is_ccw, 0)
     self.assertFalse(p.is_closed)
Exemple #2
0
 def test_from_gleamobj(self):
     """ # from_gleamobj """
     # from skyf
     print(">>> {}".format(self.skyf))
     roi = ROISelector.from_gleamobj(self.skyf, **self.v)
     self.assertIsInstance(roi, ROISelector)
     self.assertIsNotNone(roi.data)
     # from skypatch
     print(">>> {}".format(self.skyp))
     roi = ROISelector.from_gleamobj(self.skyp, **self.v)
     self.assertIsInstance(roi, ROISelector)
     self.assertIsNotNone(roi.data)
Exemple #3
0
 def setUp(self):
     # arguments and keywords
     self.test_fits = os.path.abspath(os.path.dirname(__file__)) \
                      + '/W3+3-2.I.12907_13034_7446_7573.fits'
     self.test_ftss = os.path.dirname(self.test_fits)
     # __init__ test
     self.skyf = SkyF(self.test_fits)
     self.skyp = SkyPatch(self.test_ftss)
     self.roi = ROISelector(self.skyf.data)
     # verbosity
     self.v = {'verbose': 1}
     print("")
     print(self.separator)
     print(self.shortDescription())
Exemple #4
0
 def test_select_square(self):
     """ # select_square """
     # on shape (128, 128)
     anchor, dv = (0, 79), 48
     print(">>> {}, {}".format(anchor, dv))
     s = self.roi.select_square(anchor, dv, **self.v)
     self.assertIsInstance(s, np.ndarray)
     self.assertEqual(s.dtype, bool)
     self.assertTrue(np.any(s))
     self.roi.data[self.roi()] = 100
     plt.imshow(self.roi.data, cmap='bone', origin='lower')
     if 0:
         plt.show()
     plt.close()
     # on shape (200, 300, 4)
     W, H = 300, 200
     e = np.ones((H, W, 4), dtype=float)
     anchor, dv = (0, 100), 100
     print(">>> {}, {}".format(anchor, dv))
     roi = ROISelector(e)
     s = roi.select['square'](anchor, dv, **self.v)
     e[~roi()] = 0, 0, 1, 0
     e[:, :, 3] = 1
     plt.imshow(e, origin='lower')
     if 0:
         plt.show()
     plt.close()
Exemple #5
0
 def test_select_amorph(self):
     """ # test_amorph """
     # interrupted diagonal on shape (128, 128)
     points = [(i, i) for i in range(128)]
     points = points[0:32] + points[-32:]
     print(">>> {}".format(points))
     s = self.roi.select_amorph(*points, **self.v)
     self.assertIsInstance(s, np.ndarray)
     self.assertEqual(s.dtype, bool)
     self.assertTrue(np.any(s))
     self.roi.data[self.roi()] = 100
     plt.imshow(self.roi.data, cmap='bone', origin='lower')
     if 0:
         plt.show()
     plt.close()
     # scattered points on shape (200, 300, 4)
     W, H = 300, 200
     e = np.ones((H, W, 4), dtype=float)
     points = [(2 * i, i) for i in range(128)]
     points = points[0:32] + points[-32:]
     print(">>> {}".format(points))
     roi = ROISelector(e)
     s = roi.select['amorph'](*points, **self.v)
     e[~roi()] = 0, 0, 1, 0
     e[:, :, 3] = 1
     plt.imshow(e, origin='lower')
     if 0:
         plt.show()
     plt.close()
Exemple #6
0
 def test_select_polygon(self):
     """ # test_polygon """
     # on shape (128, 128)
     polygon = [(43, 45), (42, 54), (39, 62), (38, 71), (31, 60), (36, 51)]
     print(">>> {}".format(polygon))
     s = self.roi.select_polygon(*polygon, **self.v)
     self.assertIsInstance(s, np.ndarray)
     self.assertEqual(s.dtype, bool)
     self.assertTrue(np.any(s))
     self.roi.data[self.roi()] = 100
     plt.imshow(self.roi.data, cmap='bone', origin='lower')
     if 0:
         plt.show()
     plt.close()
     # on shape (200, 300, 4)
     W, H = 300, 200
     e = np.ones((H, W, 4), dtype=float)
     polygon = [(125, 50), (175, 50), (200, 100), (175, 150), (125, 150),
                (100, 100)]
     print(">>> {}".format(polygon))
     roi = ROISelector(e)
     s = roi.select['polygon'](*polygon, **self.v)
     e[~roi()] = 0, 0, 1, 0
     e[:, :, 3] = 1
     plt.imshow(e, origin='lower')
     if 0:
         plt.show()
     plt.close()
Exemple #7
0
 def test_select_circle(self):
     """ # select_circle """
     # on shape (128, 128)
     center, radius = (0, 128), 30
     print(">>> {}, {}".format(center, radius))
     s = self.roi.select_circle(center, radius, **self.v)
     self.assertIsInstance(s, np.ndarray)
     self.assertEqual(s.dtype, bool)
     self.assertTrue(np.any(s))
     self.roi.data[self.roi()] = 100
     plt.imshow(self.roi.data, cmap='bone', origin='lower')
     if 0:
         plt.show()
     plt.close()
     # on shape (200, 300, 4)
     W, H = 300, 200
     e = np.ones((H, W, 4), dtype=float)
     center, radius = (50, 150), 40
     print(">>> {}, {}".format(center, radius))
     roi = ROISelector(e)
     s = roi.select['circle'](center, radius, **self.v)
     e[~roi()] = 0
     e[:, :, 3] = 1
     plt.imshow(e, origin='lower')
     if 0:
         plt.show()
     plt.close()
Exemple #8
0
 def test_ROISelector(self):
     """ # ROISelector """
     # None
     print(">>> {}".format(None))
     roi = ROISelector(None, **self.v)
     self.assertIsInstance(roi, ROISelector)
     self.assertEqual(roi.data, np.zeros((1, 1)))
     # with data shape (128, 128)
     print(">>> {}".format(self.skyf.data))
     roi = ROISelector(self.skyf.data, **self.v)
     self.assertIsInstance(roi, ROISelector)
     self.assertIsNotNone(roi.data)
     # with data shape (128, 128, 2)
     print(">>> {}".format(self.skyp.data))
     roi = ROISelector(self.skyp.data, **self.v)
     self.assertIsInstance(roi, ROISelector)
     self.assertIsNotNone(roi.data)
Exemple #9
0
 def setUp(self):
     # arguments and keywords
     self.anchor = [0, 0]
     self.rectangle = ROISelector.Rectangle(self.anchor, [1, 1])
     # # verbosity
     self.v = {'verbose': 1}
     print("")
     print(self.separator)
     print(self.shortDescription())
Exemple #10
0
 def setUp(self):
     # arguments and keywords
     self.anchor = (0, 0)
     self.square = ROISelector.Square(self.anchor, 1)
     # # verbosity
     self.v = {'verbose': 1}
     print("")
     print(self.separator)
     print(self.shortDescription())
Exemple #11
0
 def setUp(self):
     # arguments and keywords
     self.points = [(0, 0), (1, 0), (0, 2)]
     self.amorph = ROISelector.Amorph(self.points)
     # # verbosity
     self.v = {'verbose': 1}
     print("")
     print(self.separator)
     print(self.shortDescription())
Exemple #12
0
 def setUp(self):
     # arguments and keywords
     self.points = [(0, 0), (1, 0), (2, 1), (3, 2.5), (3, 3), (2, 3),
                    (2, 2.5), (0.5, 1)]
     self.polygon = ROISelector.Polygon(*self.points)
     # # verbosity
     self.v = {'verbose': 1}
     print("")
     print(self.separator)
     print(self.shortDescription())
Exemple #13
0
 def setUp(self):
     # arguments and keywords
     self.center = (0, 0)
     self.radius = 1
     self.circle = ROISelector.Circle(self.center, self.radius)
     # # verbosity
     self.v = {'verbose': 1}
     print("")
     print(self.separator)
     print(self.shortDescription())
Exemple #14
0
 def test_add_point(self):
     """ # ROISelector.Amorph.add_point """
     # add point to grid points
     print(">>> {}".format((5, 5)))
     amorph = ROISelector.Amorph(np.flip(np.indices((3, 3)).T, 2))
     amorph = amorph.add_point((5, 5), **self.v)
     print(amorph.N)
     self.assertIsInstance(amorph, ROISelector.Amorph)
     # add point to flat points
     print(">>> {}".format((5, 5)))
     self.amorph.add_point((5, 5), **self.v)
     self.assertIsInstance(self.amorph, ROISelector.Amorph)
Exemple #15
0
 def test_contains(self):
     """ # ROISelector.Amorph.contains """
     # in flat points
     points = np.array([(5, 5), (0, 0), (1, 0)]).T
     print(">>> {}".format(points))
     self.amorph = self.amorph.add_point((5, 0), **self.v)
     self.amorph.contains(*points, **self.v)
     # in grid points
     print(">>> {}".format(points))
     amorph = ROISelector.Amorph(np.flip(np.indices((4, 4)).T, -1),
                                 **self.v)
     print(np.array([(5, 5), (3, 2), (0, 0), (1, 0)]).T)
     amorph.contains(*np.array([(5, 5), (3, 2), (0, 0), (1, 0)]).T,
                     **self.v)
Exemple #16
0
 def test_Rectangle(self):
     """ # ROISelector.Rectangle """
     # with dv
     print(">>> {}, {}".format(self.anchor, (1, 1)))
     r = ROISelector.Rectangle(self.anchor, (1, 1), **self.v)
     self.assertIsInstance(r, ROISelector.Rectangle)
     self.assertEqual(r, self.rectangle)
     self.assertEqual(r.N, 4)
     self.assertEqual(r.area, 1)
     # with corner
     print(">>> {}, {}".format(self.anchor, {'corner': (1, 1)}))
     r = ROISelector.Rectangle(self.anchor, corner=(1, 1), **self.v)
     self.assertIsInstance(r, ROISelector.Rectangle)
     self.assertEqual(r, self.rectangle)
     self.assertEqual(r.N, 4)
     self.assertEqual(r.area, 1)
     # no second input
     print(">>> {}".format(self.anchor))
     r = ROISelector.Rectangle(self.anchor, **self.v)
     self.assertIsInstance(r, ROISelector.Rectangle)
     self.assertNotEqual(r, self.rectangle)
     self.assertEqual(r.N, 4)
     self.assertEqual(r.area, 0)
Exemple #17
0
 def test_Amorph(self):
     """ # ROISelector.Amorph """
     # with list
     print(">>> {}".format(self.points))
     amorph = ROISelector.Amorph(self.points, **self.v)
     self.assertIsInstance(amorph, ROISelector.Amorph)
     self.assertTrue(np.all(amorph == self.amorph))
     # with tuple
     print(">>> {}".format(tuple(self.points)))
     amorph = ROISelector.Amorph(*self.points, **self.v)
     self.assertIsInstance(amorph, ROISelector.Amorph)
     self.assertTrue(np.all(amorph == self.amorph))
     # with numpy array
     print(">>> {}".format(np.flip(np.indices((3, 3)).T, 2)))
     amorph = ROISelector.Amorph(np.flip(np.indices((3, 3)).T, 2), **self.v)
     self.assertIsInstance(amorph, ROISelector.Amorph)
     self.assertFalse(np.all(amorph == self.amorph))
     self.assertIsInstance(amorph[:2, :2, :], ROISelector.Amorph)
     # single-point input (separate)
     print(">>> {}".format((0, 0)))
     amorph = ROISelector.Amorph(0, 0, **self.v)
     self.assertIsInstance(amorph, ROISelector.Amorph)
     self.assertFalse(np.all(amorph == self.amorph))
Exemple #18
0
 def test_Circle(self):
     """ # ROISelector.Circle """
     # with no radius
     print(">>> {}".format(self.center))
     circle = ROISelector.Circle(self.center, radius=0, **self.v)
     self.assertIsInstance(circle, ROISelector.Circle)
     self.assertNotEqual(circle, self.circle)
     self.assertEqual(circle.diameter, 0)
     self.assertEqual(circle.area, 0)
     # with scalar radius
     print(">>> {}".format((self.center, self.radius)))
     circle = ROISelector.Circle(self.center, self.radius, **self.v)
     self.assertIsInstance(circle, ROISelector.Circle)
     self.assertEqual(circle, self.circle)
     self.assertEqual(circle.diameter, 2 * self.radius)
     self.assertEqual(circle.area, np.pi)
     # with radial point
     print(">>> {}".format((self.center, (self.radius, 0))))
     circle = ROISelector.Circle(self.center, (self.radius, 0), **self.v)
     self.assertIsInstance(circle, ROISelector.Circle)
     self.assertEqual(circle, self.circle)
     self.assertEqual(circle.diameter, 2 * self.radius)
     self.assertEqual(circle.area, np.pi)
Exemple #19
0
 def test_Square(self):
     """ # ROISelector.Square """
     # with scalar dv
     print(">>> {}, {}".format(self.anchor, 1))
     s = ROISelector.Square(self.anchor, 1, **self.v)
     self.assertIsInstance(s, ROISelector.Square)
     self.assertEqual(s, self.square)
     self.assertEqual(s.N, 4)
     self.assertEqual(s.area, 1)
     # with corner
     print(">>> {}, {}".format(self.anchor, {'corner': (1, 2)}))
     s = ROISelector.Square(self.anchor, corner=(1, 2), **self.v)
     self.assertIsInstance(s, ROISelector.Square)
     self.assertNotEqual(s, self.square)
     self.assertEqual(s.N, 4)
     self.assertEqual(s.area, 4)
     # no second input
     print(">>> {}".format(self.anchor))
     s = ROISelector.Square(self.anchor, **self.v)
     self.assertIsInstance(s, ROISelector.Square)
     self.assertNotEqual(s, self.square)
     self.assertEqual(s.N, 4)
     self.assertEqual(s.area, 0)
Exemple #20
0
    stel_map = StarSampler.resample_map(stel_map, ml['i'].extent,
                                        lens_map.shape, glsextent)
    print(ml[0].filename.split('.')[0])
    lens_maps.append(lens_map)
    stel_maps.append(stel_map)
    px2arcsecs.append(px2arcsec)
    # plt.style.use('dark_background')
    # plt.imshow(stel_map, origin='lower', cmap='magma',
    #            extent=[glsextent[0], glsextent[2], glsextent[1], glsextent[3]])
    # plt.colorbar(label=r'$M_{\odot}/\mathrm{arcsec}^{2}$')
    # plt.xlabel(r'$\mathrm{arcsec}$')
    # plt.ylabel(r'$\mathrm{arcsec}$')
    # plt.tight_layout()
    # plt.savefig('SW06_light_model.png', transparent=True)

    Mtot = ROISelector.r_integrate(lens_map) * px2arcsec**2
    print("Total mass: {:e}".format(Mtot))
    Mstel = ROISelector.r_integrate(stel_map) * px2arcsec**2
    print("Stellar mass: {:e}".format(Mstel))

# get half light radii
halflight_R = []
for px2arcsec, stel_map in zip(px2arcsecs, stel_maps):
    Mstel = ROISelector.r_integrate(stel_map) * px2arcsec**2
    prof = ROISelector.cumr_profile(stel_map) * px2arcsec**2
    idxR = (np.abs(prof - (0.5 * Mstel))).argmin()
    r = np.linspace(0, px2arcsec * 0.5 * stel_map.shape[0],
                    0.5 * stel_map.shape[0])[idxR]
    halflight_R.append(r)

# frames in units of halflight radii
Exemple #21
0
class TestROISelector(UnitTestPrototype):
    def setUp(self):
        # arguments and keywords
        self.test_fits = os.path.abspath(os.path.dirname(__file__)) \
                         + '/W3+3-2.I.12907_13034_7446_7573.fits'
        self.test_ftss = os.path.dirname(self.test_fits)
        # __init__ test
        self.skyf = SkyF(self.test_fits)
        self.skyp = SkyPatch(self.test_ftss)
        self.roi = ROISelector(self.skyf.data)
        # verbosity
        self.v = {'verbose': 1}
        print("")
        print(self.separator)
        print(self.shortDescription())

    def tearDown(self):
        print("")

    def test_ROISelector(self):
        """ # ROISelector """
        # None
        print(">>> {}".format(None))
        roi = ROISelector(None, **self.v)
        self.assertIsInstance(roi, ROISelector)
        self.assertEqual(roi.data, np.zeros((1, 1)))
        # with data shape (128, 128)
        print(">>> {}".format(self.skyf.data))
        roi = ROISelector(self.skyf.data, **self.v)
        self.assertIsInstance(roi, ROISelector)
        self.assertIsNotNone(roi.data)
        # with data shape (128, 128, 2)
        print(">>> {}".format(self.skyp.data))
        roi = ROISelector(self.skyp.data, **self.v)
        self.assertIsInstance(roi, ROISelector)
        self.assertIsNotNone(roi.data)

    def test_from_gleamobj(self):
        """ # from_gleamobj """
        # from skyf
        print(">>> {}".format(self.skyf))
        roi = ROISelector.from_gleamobj(self.skyf, **self.v)
        self.assertIsInstance(roi, ROISelector)
        self.assertIsNotNone(roi.data)
        # from skypatch
        print(">>> {}".format(self.skyp))
        roi = ROISelector.from_gleamobj(self.skyp, **self.v)
        self.assertIsInstance(roi, ROISelector)
        self.assertIsNotNone(roi.data)

    def test_select_circle(self):
        """ # select_circle """
        # on shape (128, 128)
        center, radius = (0, 128), 30
        print(">>> {}, {}".format(center, radius))
        s = self.roi.select_circle(center, radius, **self.v)
        self.assertIsInstance(s, np.ndarray)
        self.assertEqual(s.dtype, bool)
        self.assertTrue(np.any(s))
        self.roi.data[self.roi()] = 100
        plt.imshow(self.roi.data, cmap='bone', origin='lower')
        if 0:
            plt.show()
        plt.close()
        # on shape (200, 300, 4)
        W, H = 300, 200
        e = np.ones((H, W, 4), dtype=float)
        center, radius = (50, 150), 40
        print(">>> {}, {}".format(center, radius))
        roi = ROISelector(e)
        s = roi.select['circle'](center, radius, **self.v)
        e[~roi()] = 0
        e[:, :, 3] = 1
        plt.imshow(e, origin='lower')
        if 0:
            plt.show()
        plt.close()

    def test_select_rect(self):
        """ # select_rect """
        # on shape (128, 128)
        anchor, dv = (0, 0), (32, 64)
        print(">>> {}, {}".format(anchor, dv))
        s = self.roi.select_rect(anchor, dv, **self.v)
        self.assertIsInstance(s, np.ndarray)
        self.assertEqual(s.dtype, bool)
        self.assertTrue(np.any(s))
        self.roi.data[self.roi()] = 100
        plt.imshow(self.roi.data, cmap='bone', origin='lower')
        if 0:
            plt.show()
        plt.close()
        # on shape (200, 300, 4)
        W, H = 300, 200
        e = np.ones((H, W, 4), dtype=float)
        anchor, dv = (0, 0), (100, 150)
        print(">>> {}, {}".format(anchor, dv))
        roi = ROISelector(e)
        s = roi.select['rect'](anchor, dv, **self.v)
        e[~roi()] = 0, 0, 1, 0
        e[:, :, 3] = 1
        plt.imshow(e, origin='lower')
        if 0:
            plt.show()
        plt.close()

    def test_select_square(self):
        """ # select_square """
        # on shape (128, 128)
        anchor, dv = (0, 79), 48
        print(">>> {}, {}".format(anchor, dv))
        s = self.roi.select_square(anchor, dv, **self.v)
        self.assertIsInstance(s, np.ndarray)
        self.assertEqual(s.dtype, bool)
        self.assertTrue(np.any(s))
        self.roi.data[self.roi()] = 100
        plt.imshow(self.roi.data, cmap='bone', origin='lower')
        if 0:
            plt.show()
        plt.close()
        # on shape (200, 300, 4)
        W, H = 300, 200
        e = np.ones((H, W, 4), dtype=float)
        anchor, dv = (0, 100), 100
        print(">>> {}, {}".format(anchor, dv))
        roi = ROISelector(e)
        s = roi.select['square'](anchor, dv, **self.v)
        e[~roi()] = 0, 0, 1, 0
        e[:, :, 3] = 1
        plt.imshow(e, origin='lower')
        if 0:
            plt.show()
        plt.close()

    def test_select_polygon(self):
        """ # test_polygon """
        # on shape (128, 128)
        polygon = [(43, 45), (42, 54), (39, 62), (38, 71), (31, 60), (36, 51)]
        print(">>> {}".format(polygon))
        s = self.roi.select_polygon(*polygon, **self.v)
        self.assertIsInstance(s, np.ndarray)
        self.assertEqual(s.dtype, bool)
        self.assertTrue(np.any(s))
        self.roi.data[self.roi()] = 100
        plt.imshow(self.roi.data, cmap='bone', origin='lower')
        if 0:
            plt.show()
        plt.close()
        # on shape (200, 300, 4)
        W, H = 300, 200
        e = np.ones((H, W, 4), dtype=float)
        polygon = [(125, 50), (175, 50), (200, 100), (175, 150), (125, 150),
                   (100, 100)]
        print(">>> {}".format(polygon))
        roi = ROISelector(e)
        s = roi.select['polygon'](*polygon, **self.v)
        e[~roi()] = 0, 0, 1, 0
        e[:, :, 3] = 1
        plt.imshow(e, origin='lower')
        if 0:
            plt.show()
        plt.close()

    def test_select_amorph(self):
        """ # test_amorph """
        # interrupted diagonal on shape (128, 128)
        points = [(i, i) for i in range(128)]
        points = points[0:32] + points[-32:]
        print(">>> {}".format(points))
        s = self.roi.select_amorph(*points, **self.v)
        self.assertIsInstance(s, np.ndarray)
        self.assertEqual(s.dtype, bool)
        self.assertTrue(np.any(s))
        self.roi.data[self.roi()] = 100
        plt.imshow(self.roi.data, cmap='bone', origin='lower')
        if 0:
            plt.show()
        plt.close()
        # scattered points on shape (200, 300, 4)
        W, H = 300, 200
        e = np.ones((H, W, 4), dtype=float)
        points = [(2 * i, i) for i in range(128)]
        points = points[0:32] + points[-32:]
        print(">>> {}".format(points))
        roi = ROISelector(e)
        s = roi.select['amorph'](*points, **self.v)
        e[~roi()] = 0, 0, 1, 0
        e[:, :, 3] = 1
        plt.imshow(e, origin='lower')
        if 0:
            plt.show()
        plt.close()

    def test_mpl_interface(self):
        """ # mpl_interface"""
        pass