示例#1
0
    def test_variables_not_none():
        """Tests _variables_not_none method."""

        names = ["a", "b", "c"]
        values = [1, 2, 3]

        Classifier._variables_not_none(names, values)
示例#2
0
    def test_valid_input_types_is_str_throws_wrong_type():
        """Test _valid_input_types_is_str."""

        h, j, v, z = [1 for _ in range(4)]

        with pytest.raises(ValueError):
            Classifier._valid_input_types_is_str(h, j, v, z)
示例#3
0
    def test_classify_image_mean_var_file():
        """Classify an image from files using mean and variance."""
        local = os.path.dirname(os.path.abspath(__file__))

        example.get_sample(local)

        h, j, v, z = [os.path.join(local, f"{b}.fits") for b in "hjvz"]

        Classifier.classify(h=h,
                            j=j,
                            v=v,
                            z=z,
                            out_dir=local,
                            out_type="mean_var")

        outs = dh.get_expected_morpheus_output(out_type="mean_var")

        for k in outs:

            np.testing.assert_allclose(
                outs[k],
                fits.getdata(os.path.join(local, f"{k}.fits")),
                atol=1e-5,
                err_msg=f"{k} failed comparison",
            )

            os.remove(os.path.join(local, f"{k}.fits"))

        for b in "hjvz":
            os.remove(os.path.join(local, f"{b}.fits"))
示例#4
0
    def test_arrays_same_size():
        """Tests _arrays_same_size method."""

        shape = (10, 10)
        arrs = [np.zeros(shape) for _ in range(3)]

        Classifier._arrays_same_size(arrs)
示例#5
0
    def test_variables_not_none_throws():
        """Tests _variables_not_none, throws ValueError."""

        names = ["a", "b", "c"]
        values = [1, 2, None]

        with pytest.raises(ValueError):
            Classifier._variables_not_none(names, values)
示例#6
0
    def test_valid_input_types_is_str_throws_mixed():
        """Test _valid_input_types_is_str."""

        h, j = ["" for _ in range(2)]
        v, z = [np.zeros([10]) for _ in range(2)]

        with pytest.raises(ValueError):
            Classifier._valid_input_types_is_str(h, j, v, z)
示例#7
0
    def test_validate_parallel_params_raises_single_cpu():
        """Test _validate_parallel_params.

        Throws ValueError for passing a single gpu.
        """
        cpus = 1

        with pytest.raises(ValueError):
            Classifier._validate_parallel_params(cpus=cpus)
示例#8
0
    def test_arrays_same_size_throws():
        """Tests _arrays_same_size, throws ValueError."""

        shape = (10, 10)
        arrs = [np.zeros(shape) for _ in range(3)]
        arrs.append(np.zeros((20, 20)))

        with pytest.raises(ValueError):
            Classifier._arrays_same_size(arrs)
示例#9
0
    def test_make_runnable_file():
        """Test _make_runnable_file."""
        local = os.path.dirname(os.path.abspath(__file__))

        Classifier._make_runnable_file(local)

        assert os.path.exists(os.path.join(local, "main.py"))

        os.remove(os.path.join(local, "main.py"))
示例#10
0
    def test_segmap_from_classified_fails():
        """Test the segmap_from_classified method."""

        data = dh.get_expected_morpheus_output()
        h, _, _, _ = example.get_sample()
        mask = np.zeros_like(h, dtype=np.int)
        mask[5:-5, 5:-5] = 1

        with pytest.raises(ValueError):
            Classifier.segmap_from_classified(data, h, mask=mask, bkg_src_threshold=1.0)
示例#11
0
    def test_validate_parallel_params_raises_cpus_gpus():
        """Test _validate_parallel_params.

        Throws ValueError for passing values for both cpus an gpus.
        """
        gpus = [0]
        cpus = 0

        with pytest.raises(ValueError):
            Classifier._validate_parallel_params(gpus=gpus, cpus=cpus)
示例#12
0
    def test_classify_mean_var_parallel_cpu():
        """Classify an image in parallel with two cpus."""
        local = os.path.dirname(os.path.abspath(__file__))
        os.mkdir(os.path.join(local, "output"))
        out_dir = os.path.join(local, "output")

        example.get_sample(local)
        h, j, v, z = [os.path.join(local, f"{b}.fits") for b in "hjvz"]

        outs = dh.get_expected_morpheus_output(out_type="mean_var")

        classified = Classifier.classify(
            h=h,
            j=j,
            v=v,
            z=z,
            out_dir=out_dir,
            out_type="mean_var",
            cpus=2,
            parallel_check_interval=0.25,  # check every 15 seconds
        )

        for k in outs:
            np.testing.assert_allclose(outs[k],
                                       classified[k],
                                       atol=1e-5,
                                       err_msg=f"{k} failed comparison")

        shutil.rmtree(out_dir)

        for b in [h, j, v, z]:
            os.remove(b)
示例#13
0
    def test_standardize_img():
        """Test _standardize_img method."""

        img = np.random.normal(loc=1.0, scale=3.0, size=(10, 10, 10))

        img = Classifier._standardize_img(img)

        np.testing.assert_allclose(np.mean(img), 0, atol=1e-07)
        np.testing.assert_allclose(np.var(img), 1, atol=1e-07)
示例#14
0
    def test_colorize_classified():
        """Test colorize_classified."""

        data = dh.get_expected_morpheus_output()
        expected_color = dh.get_expected_colorized_pngs()["no_hidden"]

        actual_color = Classifier.colorize_classified(data, hide_unclassified=False)

        actual_color = (actual_color * 255).astype(np.uint8)

        np.testing.assert_array_almost_equal(expected_color, actual_color)
示例#15
0
    def test_classify_image_rank_vote_in_mem():
        """Classify an image in memory using rank vote."""
        h, j, v, z = example.get_sample()

        expected_outs = dh.get_expected_morpheus_output()

        outs = Classifier.classify(h=h, j=j, v=v, z=z, out_dir=None)

        for k in outs:
            np.testing.assert_allclose(outs[k],
                                       expected_outs[k],
                                       err_msg=f"{k} failed comparison")
示例#16
0
    def test_catalog_from_classified():
        """Test the catalog_from_classified method."""

        classified = dh.get_expected_morpheus_output()
        h, _, _, _ = example.get_sample()
        segmap = dh.get_expected_segmap()["segmap"]

        expected_catalog = dh.get_expected_catalog()["catalog"]

        actual_catalog = Classifier.catalog_from_classified(classified, h, segmap)

        assert expected_catalog == actual_catalog
示例#17
0
    def test_colorize_classified_hidden():
        """Test colorize_classified with hidden."""

        classified = dh.get_expected_morpheus_output()
        expected_color = dh.get_expected_colorized_pngs()["hidden"]

        actual_color = Classifier.colorize_classified(
            classified, hide_unclassified=True
        )

        actual_color = (actual_color * 255).astype(np.uint8)

        np.testing.assert_array_almost_equal(expected_color, actual_color)
示例#18
0
    def test_classify_image_mean_var():
        """Classify an image from files using mean and variance."""
        h, j, v, z = example.get_sample()

        outs = Classifier.classify(h=h, j=j, v=v, z=z, out_type="mean_var")

        expected_outs = dh.get_expected_morpheus_output(out_type="mean_var")

        for k in outs:
            np.testing.assert_allclose(outs[k],
                                       expected_outs[k],
                                       atol=1e-5,
                                       err_msg=f"{k} failed comparison")
示例#19
0
    def test_segmap_from_classified():
        """Test the segmap_from_classified method."""

        data = dh.get_expected_morpheus_output()
        h, _, _, _ = example.get_sample()
        mask = np.zeros_like(h, dtype=np.int)
        mask[5:-5, 5:-5] = 1

        expected_segmap = dh.get_expected_segmap()["segmap"]

        actual_segmap = Classifier.segmap_from_classified(data, h, mask=mask)

        np.testing.assert_array_equal(expected_segmap, actual_segmap)
示例#20
0
    def test_variables_not_none_raises():
        """Test _variables_not_none."""

        with pytest.raises(ValueError):
            Classifier._variables_not_none(["good", "bad"], [1, None])
示例#21
0
# Copyright 2018 Ryan Hausen
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
# ==============================================================================
"""Simple classification example reading from disk."""

from morpheus.classifier import Classifier
from morpheus.data import example

# this saves the sample numpy arrays as FITS files in 'out_dir'
example.get_sample(out_dir=".")
h, j, v, z = [f"{band}.fits" for band in "hjvz"]

morphs = Classifier.classify(h=h, j=j, v=v, z=z)
示例#22
0
    def test_valid_input_types_is_str_ndarray():
        """Test _valid_input_types_is_str."""

        h, j, v, z = [np.zeros([10]) for _ in range(4)]

        assert not Classifier._valid_input_types_is_str(h, j, v, z)
示例#23
0
    def test_valid_input_types_is_str_str():
        """Test _valid_input_types_is_str."""

        h, j, v, z = ["" for _ in range(4)]

        assert Classifier._valid_input_types_is_str(h, j, v, z)
示例#24
0
def main():
    args = _parse_args(sys.argv[1:])

    if args.action == "None":
        Classifier.classify(
            h=args.h,
            j=args.j,
            v=args.v,
            z=args.z,
            batch_size=args.batch_size,
            out_dir=args.out_dir,
            gpus=args.gpus,
            cpus=args.cpus,
        )
    elif args.action == "catalog":
        classified = Classifier.classify(
            h=args.h,
            j=args.j,
            v=args.v,
            z=args.z,
            batch_size=args.batch_size,
            out_dir=args.out_dir,
            gpus=args.gpus,
            cpus=args.cpus,
        )

        segmap = Classifier.segmap_from_classified(classified,
                                                   args.h,
                                                   out_dir=args.out_dir)

        Classifier.catalog_from_classified(
            classified,
            args.h,
            segmap,
            out_file=os.path.join(args.out_dir, "colorized.png"),
        )
    elif args.action == "segmap":
        classified = Classifier.classify(
            h=args.h,
            j=args.j,
            v=args.v,
            z=args.z,
            batch_size=args.batch_size,
            out_dir=args.out_dir,
            gpus=args.gpus,
            cpus=args.cpus,
        )

        Classifier.segmap_from_classified(classified,
                                          args.h,
                                          out_dir=args.out_dir)
    elif args.action == "colorize":
        classified = Classifier.classify(
            h=args.h,
            j=args.j,
            v=args.v,
            z=args.z,
            batch_size=args.batch_size,
            out_dir=args.out_dir,
            gpus=args.gpus,
            cpus=args.cpus,
        )

        Classifier.colorize_classified(classified, out_dir=args.out_dir)
    elif args.action == "all":
        classified = Classifier.classify(
            h=args.h,
            j=args.j,
            v=args.v,
            z=args.z,
            batch_size=args.batch_size,
            out_dir=args.out_dir,
            gpus=args.gpus,
            cpus=args.cpus,
        )

        segmap = Classifier.segmap_from_classified(classified,
                                                   args.h,
                                                   out_dir=args.out_dir)

        Classifier.catalog_from_classified(
            classified,
            args.h,
            segmap,
            out_file=os.path.join(args.out_dir, "colorized.png"),
        )

        Classifier.colorize_classified(classified, out_dir=args.out_dir)
示例#25
0
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
# ==============================================================================
"""Simple classification example on disk."""

from morpheus.classifier import Classifier
from morpheus.data import example


# this saves the sample numpy arrays as fits files in 'out_dir'
example.get_sample(out_dir=".")
h, j, v, z = [f"{band}.fits" for band in "hjvz"]

Classifier.classify(h=h, j=j, v=v, z=z, out_dir=".")