示例#1
0
# Start the algorithm
cwt.analyze(cwt_data)


# ## Results of analysis
# 
# Look at the results of CWT algorithm. Print all the images.

# In[ ]:


PLOT_VALUE_MAX = 5
FIG_SIZE = (15, 35)

fig = plt.figure(figsize=FIG_SIZE)
images = cwt_data.images()
for index, (name, image) in enumerate(images.items()):
    ax = fig.add_subplot(len(images), 2, index + 1, projection=image.geom.wcs)
    image.plot(vmax=PLOT_VALUE_MAX, fig=fig, ax=ax)
    plt.title(name)  # Maybe add a Name in SkyImage.plots?


# As you can see in the implementation of CWT above, it has the parameter `keep_history`. If you set to it `True`-value, it means that CWT would save all the images from iterations. Algorithm keeps images of only last CWT start.  Let's do this in the demonstration.

# In[ ]:


history = cwt.history
print(
    "Number of iterations: {0}".format(len(history) - 1)
)  # -1 because CWT save start images too
示例#2
0
class TestCWTData:
    """
    Test CWTData class.
    """
    def setup(self):
        filename = "$GAMMAPY_DATA/tests/unbundled/poisson_stats_image/counts.fits.gz"
        image = Map.read(filename)
        background = image.copy(data=np.ones(image.data.shape, dtype=float))

        self.kernels = CWTKernels(n_scale=2,
                                  min_scale=3.0,
                                  step_scale=2.6,
                                  old=False)
        self.data = dict(image=image, background=background)
        self.cwt = CWT(kernels=self.kernels,
                       significance_threshold=2.0,
                       keep_history=True)
        self.cwt_data = CWTData(counts=image,
                                background=background,
                                n_scale=self.kernels.n_scale)
        self.cwt.analyze(data=self.cwt_data)

    def test_images(self):
        images = self.cwt_data.images()
        assert_allclose(images["counts"].data[25, 25],
                        self.data["image"].data[25, 25])
        assert_allclose(images["background"].data[36, 63],
                        self.data["background"].data[36, 63])

        model_plus_approx = images["model_plus_approx"].data
        assert_allclose(model_plus_approx[100, 100], 0.753205544726)
        assert_allclose(model_plus_approx[10, 10], -0.0210240420041)

        maximal = images["maximal"].data
        assert_allclose(maximal[100, 100], 0.0401320295446)
        assert_allclose(maximal[10, 10], 0.0)

        support_2d = images["support_2d"].data
        assert_allclose(support_2d.sum(), 2996)

    def test_cube_metrics_info(self):
        cubes = self.cwt_data.cubes()
        name = "transform_3d"
        cube = cubes[name].data
        info = self.cwt_data._metrics_info(data=cube, name=name)

        assert_equal(info["Shape"], "3D cube")
        assert_allclose(info["Variance"], 3.24405547338e-06)
        assert_allclose(info["Max value"], 0.041216412114)

    def test_image_info(self):
        t = self.cwt_data.image_info(name="residual")
        assert_equal(t.colnames, ["Metrics", "Source"])
        assert_equal(len(t), 7)

    def test_cube_info(self):
        t = self.cwt_data.cube_info(name="error")
        assert_equal(t.colnames, ["Metrics", "Source"])
        assert_equal(len(t), 7)

        t = self.cwt_data.cube_info(name="error", per_scale=True)
        assert_equal(t.colnames, ["Scale power", "Metrics", "Source"])
        assert_equal(len(t), 14)

    def test_info_table(self):
        t = self.cwt_data.info_table
        assert_equal(len(t.colnames), 7)
        assert_equal(len(t), 13)

    def test_sub(self):
        h = self.cwt.history
        diff = h[7] - h[5]
        assert_equal(diff.support_3d.data.sum(), 0)
        assert_allclose(diff.model.data.sum(), 20.4132906267)

    def test_io(self, tmpdir):
        filename = str(tmpdir / "test-cwt.fits")
        self.cwt_data.write(filename=filename, overwrite=True)
        approx = Map.read(filename, hdu="APPROX")
        assert_allclose(approx.data[100, 100], self.cwt_data._approx[100, 100])
        assert_allclose(approx.data[36, 63], self.cwt_data._approx[36, 63])

        transform_2d = Map.read(filename, hdu="TRANSFORM_2D")
        assert_allclose(transform_2d.data[100, 100],
                        self.cwt_data.transform_2d.data[100, 100])
        assert_allclose(transform_2d.data[36, 63],
                        self.cwt_data.transform_2d.data[36, 63])