示例#1
0
 def test_f(self):
     coords = Coordinates([[1, 2, 3, 4, 5]], ["time"])
     node = Arange()
     q = Queue()
     _f(node.json, coords.json, q, {})
     o = q.get()
     np.testing.assert_array_equal(o, node.eval(coords))
示例#2
0
    def test_mask_defaults_bool_op(self):
        coords = podpac.Coordinates(
            [podpac.clinspace(0, 1, 4),
             podpac.clinspace(0, 1, 3)],
            dims=["lat", "lon"])
        sine_node = Arange()
        a = sine_node.eval(coords).copy()

        # Less than
        node = Mask(source=sine_node, mask=sine_node, bool_op="<")
        output = node.eval(coords)
        b = a.copy()
        b.data[a.data < 1] = np.nan
        np.testing.assert_allclose(output, b)

        # Less than equal
        node = Mask(source=sine_node, mask=sine_node, bool_op="<=")
        output = node.eval(coords)
        b = a.copy()
        b.data[a.data <= 1] = np.nan
        np.testing.assert_allclose(output, b)

        # Greater than
        node = Mask(source=sine_node, mask=sine_node, bool_op=">")
        output = node.eval(coords)
        b = a.copy()
        b.data[a.data > 1] = np.nan
        np.testing.assert_allclose(output, b)

        # Greater than equal
        node = Mask(source=sine_node, mask=sine_node, bool_op=">=")
        output = node.eval(coords)
        b = a.copy()
        b.data[a.data >= 1] = np.nan
        np.testing.assert_allclose(output, b)
示例#3
0
 def test_f_fmt(self):
     coords = Coordinates([[1, 2, 3, 4, 5]], ["time"])
     node = Arange()
     q = Queue()
     _f(node.json, coords.json, q, {"format": "dict", "format_kwargs": {}})
     o = q.get()
     np.testing.assert_array_equal(o["data"],
                                   node.eval(coords).to_dict()["data"])
示例#4
0
    def test_mp_results_the_same(self):
        coords = Coordinates([[1, 2, 3, 4, 5]], ["time"])
        node = Arange()
        o_sp = node.eval(coords)

        node_mp = Process(source=node)
        o_mp = node_mp.eval(coords)

        np.testing.assert_array_equal(o_sp.data, o_mp.data)
示例#5
0
    def test_mp_results_the_same_set_output(self):
        coords = Coordinates([[1, 2, 3, 4, 5]], ["time"])
        node = Arange()
        o_sp = node.eval(coords)
        output = o_sp.copy()
        output[:] = np.nan

        node_mp = Process(source=node)
        o_mp = node_mp.eval(coords, output=output)

        np.testing.assert_array_equal(o_sp, output)
示例#6
0
    def test_masked_val(self):
        coords = podpac.Coordinates(
            [podpac.clinspace(0, 1, 4),
             podpac.clinspace(0, 1, 3)],
            dims=["lat", "lon"])
        sine_node = Arange()
        a = sine_node.eval(coords).copy()
        a.data[a.data == 1] = -9999

        node = Mask(source=sine_node, mask=sine_node, masked_val=-9999)
        output = node.eval(coords)

        np.testing.assert_allclose(output, a)
示例#7
0
    def test_mask_defaults(self):
        coords = podpac.Coordinates(
            [podpac.crange(-90, 90, 1.0),
             podpac.crange(-180, 180, 1.0)],
            dims=["lat", "lon"])
        sine_node = Arange()
        a = sine_node.eval(coords).copy()
        a.data[a.data == 1] = np.nan

        node = Mask(source=sine_node, mask=sine_node)
        output = node.eval(coords)

        np.testing.assert_allclose(output, a)
示例#8
0
    def test_multiple_outputs(self):
        class MyAlgorithm(Algorithm):
            x = NodeTrait().tag(attr=True)
            y = NodeTrait().tag(attr=True)
            outputs = ["sum", "prod", "diff"]

            def algorithm(self, inputs):
                sum_ = inputs["x"] + inputs["y"]
                prod = inputs["x"] * inputs["y"]
                diff = inputs["x"] - inputs["y"]
                return np.stack([sum_, prod, diff], -1)

        coords = podpac.Coordinates([[0, 1, 2], [10, 20]], dims=["lat", "lon"])
        x = Arange()
        y = Array(source=np.full(coords.shape, 2), coordinates=coords)
        xout = np.arange(6).reshape(3, 2)

        # all outputs
        node = MyAlgorithm(x=x, y=y)
        result = node.eval(coords)
        assert result.dims == ("lat", "lon", "output")
        np.testing.assert_array_equal(result["output"],
                                      ["sum", "prod", "diff"])
        np.testing.assert_array_equal(result.sel(output="sum"), xout + 2)
        np.testing.assert_array_equal(result.sel(output="prod"), xout * 2)
        np.testing.assert_array_equal(result.sel(output="diff"), xout - 2)

        # extract an output
        node = MyAlgorithm(x=x, y=y, output="prod")
        result = node.eval(coords)
        assert result.dims == ("lat", "lon")
        np.testing.assert_array_equal(result, xout * 2)
示例#9
0
    def test_doy_window2_mean_rescale_max_min(self):
        with podpac.settings:
            podpac.settings.set_unsafe_eval(True)

            coords = podpac.coordinates.concat([
                podpac.Coordinates(
                    [podpac.crange("1999-12-29", "2000-01-03", "1,D",
                                   "time")]),
                podpac.Coordinates(
                    [podpac.crange("2001-12-30", "2002-01-02", "1,D",
                                   "time")]),
            ])

            node = Arange()
            node_max = Arithmetic(source=node, eqn="(source < 5) + source")
            node_min = Arithmetic(source=node, eqn="-1*(source < 5) + source")

            nodedoywindow_s = FM(
                source=node,
                window=2,
                cache_output=False,
                force_eval=True,
                scale_max=node_max,
                scale_min=node_min,
                rescale=False,
            )
            o_s = nodedoywindow_s.eval(coords)

            np.testing.assert_array_almost_equal([0.5] * o_s.size, o_s)
示例#10
0
 def test_year_substitution_orig_coords(self):
     node = YearSubstituteCoordinates(source=Arange(),
                                      year="2018",
                                      substitute_eval_coords=True)
     o = node.eval(COORDS)
     assert o.time.dt.year.data[0] == xr.DataArray(
         COORDS["time"].coordinates).dt.year.data[0]
     np.testing.assert_array_equal(o["time"], COORDS["time"].coordinates)
示例#11
0
    def test_outputs(self):
        node = Combine(a=Arange(), b=Arange(), c=Arange())
        assert set(node.outputs) == set(["a", "b", "c"])

        node = Combine(a=Arange(),
                       b=Arange(),
                       c=Arange(),
                       outputs=["o1", "o2", "o3"])
        assert set(node.outputs) == set(["o1", "o2", "o3"])
示例#12
0
    def test_multi_threading(self):
        coords = podpac.Coordinates([[1, 2, 3]], ["lat"])

        with podpac.settings:
            podpac.settings.set_unsafe_eval(True)
            podpac.settings["CACHE_NODE_OUTPUT_DEFAULT"] = False
            podpac.settings["DEFAULT_CACHE"] = []
            podpac.settings["RAM_CACHE_ENABLED"] = False

            node1 = Arithmetic(A=Arange(), B=Arange(), eqn="A+B")
            node2 = Arithmetic(A=node1, B=Arange(), eqn="A+B")

            # multithreaded
            podpac.settings["MULTITHREADING"] = True
            podpac.settings["N_THREADS"] = 8

            omt = node2.eval(coords)

            # single threaded
            podpac.settings["MULTITHREADING"] = False

            ost = node2.eval(coords)

        np.testing.assert_array_equal(omt, ost)
示例#13
0
    def test_doy_window2(self):
        coords = podpac.coordinates.concat([
            podpac.Coordinates(
                [podpac.crange("1999-12-29", "2000-01-03", "1,D", "time")]),
            podpac.Coordinates(
                [podpac.crange("2001-12-30", "2002-01-02", "1,D", "time")]),
        ])

        node = Arange()
        nodedoywindow = F(source=node,
                          window=2,
                          cache_output=False,
                          force_eval=True)
        o = nodedoywindow.eval(coords)

        np.testing.assert_array_equal(o, [6, 5, 3, 3, 5, 6])
示例#14
0
    def test_init(self):
        a = SinCoords()
        b = Arange()

        with podpac.settings:
            podpac.settings.set_unsafe_eval(True)
            node = Generic(code="import numpy as np\noutput = np.minimum(a,b)",
                           a=a,
                           b=b)

            podpac.settings.set_unsafe_eval(False)
            with pytest.warns(UserWarning, match="Insecure evaluation"):
                node = Generic(
                    code="import numpy as np\noutput = np.minimum(a,b)",
                    a=a,
                    b=b)
示例#15
0
 def test_eval(self):
     coords = podpac.Coordinates([[0, 1, 2], [10, 20]], dims=["lat", "lon"])
     node = Combine(a=Arange(), b=Arange(), c=Arange())
     output = node.eval(coords)
     assert output.dims == ("lat", "lon", "output")
     assert set(output["output"].data) == set(["a", "b", "c"])
     np.testing.assert_array_equal(output.sel(output="a"),
                                   Arange().eval(coords))
     np.testing.assert_array_equal(output.sel(output="b"),
                                   Arange().eval(coords))
     np.testing.assert_array_equal(output.sel(output="c"),
                                   Arange().eval(coords))
示例#16
0
    def test_evaluate(self):
        with podpac.settings:
            podpac.settings.set_unsafe_eval(True)

            coords = podpac.Coordinates(
                [podpac.crange(-90, 90, 1.0),
                 podpac.crange(-180, 180, 1.0)],
                dims=["lat", "lon"])
            a = SinCoords()
            b = Arange()
            node = Generic(code="import numpy as np\noutput = np.minimum(a,b)",
                           a=a,
                           b=b)
            output = node.eval(coords)

            a = node.eval(coords)
            b = node.eval(coords)
            np.testing.assert_allclose(output, np.minimum(a, b))
示例#17
0
    def test_evaluate_not_allowed(self):
        with podpac.settings:
            podpac.settings.set_unsafe_eval(False)

            coords = podpac.Coordinates(
                [podpac.crange(-90, 90, 1.0),
                 podpac.crange(-180, 180, 1.0)],
                dims=["lat", "lon"])
            a = SinCoords()
            b = Arange()

            with pytest.warns(UserWarning, match="Insecure evaluation"):
                node = Generic(
                    code="import numpy as np\noutput = np.minimum(a,b)",
                    a=a,
                    b=b)

            with pytest.raises(PermissionError):
                node.eval(coords)
示例#18
0
    def test_multi_threading_stress_nthreads(self):
        coords = podpac.Coordinates([np.linspace(0, 1, 4)], ["lat"])

        A = Arithmetic(A=Arange(), eqn="A**0")
        B = Arithmetic(A=Arange(), eqn="A**1")
        C = Arithmetic(A=Arange(), eqn="A**2")
        D = Arithmetic(A=Arange(), eqn="A**3")
        E = Arithmetic(A=Arange(), eqn="A**4")
        F = Arithmetic(A=Arange(), eqn="A**5")

        node2 = Arithmetic(A=A, B=B, C=C, D=D, E=E, F=F, eqn="A+B+C+D+E+F")
        node3 = Arithmetic(A=A,
                           B=B,
                           C=C,
                           D=D,
                           E=E,
                           F=F,
                           G=node2,
                           eqn="A+B+C+D+E+F+G")

        with podpac.settings:
            podpac.settings["MULTITHREADING"] = True
            podpac.settings["N_THREADS"] = 8
            podpac.settings["CACHE_NODE_OUTPUT_DEFAULT"] = False
            podpac.settings["DEFAULT_CACHE"] = []
            podpac.settings["RAM_CACHE_ENABLED"] = False
            podpac.settings.set_unsafe_eval(True)

            omt = node3.eval(coords)

        assert node3._multi_threaded
        assert not node2._multi_threaded

        with podpac.settings:
            podpac.settings["MULTITHREADING"] = True
            podpac.settings[
                "N_THREADS"] = 9  # 2 threads available after first 7
            podpac.settings["CACHE_NODE_OUTPUT_DEFAULT"] = False
            podpac.settings["DEFAULT_CACHE"] = []
            podpac.settings["RAM_CACHE_ENABLED"] = False
            podpac.settings.set_unsafe_eval(True)

            omt = node3.eval(coords)

        assert node3._multi_threaded
        assert node2._multi_threaded
示例#19
0
    def test_doy_window2_mean_rescale_float(self):
        coords = podpac.coordinates.concat([
            podpac.Coordinates(
                [podpac.crange("1999-12-29", "2000-01-03", "1,D", "time")]),
            podpac.Coordinates(
                [podpac.crange("2001-12-30", "2002-01-02", "1,D", "time")]),
        ])

        node = Arange()
        nodedoywindow = FM(source=node,
                           window=2,
                           cache_output=False,
                           force_eval=True)
        o = nodedoywindow.eval(coords)

        nodedoywindow_s = FM(source=node,
                             window=2,
                             cache_output=False,
                             force_eval=True,
                             scale_float=[0, coords.size],
                             rescale=True)
        o_s = nodedoywindow_s.eval(coords)

        np.testing.assert_array_almost_equal(o, o_s)
示例#20
0
    def test_multi_threading_cache_race(self):
        coords = podpac.Coordinates([np.linspace(0, 1, 1024)], ["lat"])
        with podpac.settings:
            podpac.settings["MULTITHREADING"] = True
            podpac.settings["N_THREADS"] = 3
            podpac.settings["CACHE_NODE_OUTPUT_DEFAULT"] = True
            podpac.settings["DEFAULT_CACHE"] = ["ram"]
            podpac.settings["RAM_CACHE_ENABLED"] = True
            podpac.settings.set_unsafe_eval(True)
            A = Arithmetic(A=Arange(), eqn="A**2")
            B = Arithmetic(A=Arange(), eqn="A**2")
            C = Arithmetic(A=Arange(), eqn="A**2")
            D = Arithmetic(A=Arange(), eqn="A**2")
            E = Arithmetic(A=Arange(), eqn="A**2")
            F = Arithmetic(A=Arange(), eqn="A**2")

            node2 = Arithmetic(A=A, B=B, C=C, D=D, E=E, F=F, eqn="A+B+C+D+E+F")

            om = node2.eval(coords)

            from_cache = [n._from_cache for n in node2.inputs.values()]

            assert sum(from_cache) > 0
示例#21
0
    def test_in_place(self):
        coords = podpac.Coordinates(
            [podpac.clinspace(0, 1, 4),
             podpac.clinspace(0, 1, 3)],
            dims=["lat", "lon"])
        sine_node = Arange()

        node = Mask(source=sine_node, mask=sine_node, in_place=True)
        output = node.eval(coords)
        a = sine_node.eval(coords)

        # In-place editing doesn't seem to work here
        # np.testing.assert_allclose(output, node.source._output)

        coords = podpac.Coordinates(
            [podpac.clinspace(0, 1, 4),
             podpac.clinspace(0, 2, 3)],
            dims=["lat", "lon"])
        sine_node = Arange()
        node = Mask(source=sine_node, mask=sine_node, in_place=False)
        output = node.eval(coords)
        a = sine_node.eval(coords)

        assert not np.all(a == output)
示例#22
0
 def get_data(self, coordinates, slc):
     node = Arange()
     return node.eval(coordinates)
示例#23
0
 def test_year_substitution(self):
     node = YearSubstituteCoordinates(source=Arange(), year="2018")
     o = node.eval(COORDS)
     assert o.time.dt.year.data[0] == 2018
     assert not np.array_equal(o["time"], COORDS["time"].coordinates)
 def test_interpolation_warning(self):
     node = ReprojectedSource(source=Arange(),
                              reprojected_coordinates=self.coordinates)
     output = node.eval(node.coordinates)
示例#25
0
 def test_time_expansion(self):
     node = ExpandCoordinates(source=Arange(), time=("-5,D", "0,D", "1,D"))
     o = node.eval(COORDS)
示例#26
0
 def test_spatial_expansion(self):
     node = ExpandCoordinates(source=Arange(), lat=(-1, 1, 0.1))
     o = node.eval(COORDS)
示例#27
0
 def test_no_expansion(self):
     node = SelectCoordinates(source=Arange())
     o = node.eval(COORDS)
示例#28
0
 def test_time_selection(self):
     node = SelectCoordinates(source=Arange(),
                              time=("2017-08-01", "2017-09-30", "1,D"))
     o = node.eval(COORDS)
示例#29
0
 def test_Arange(self):
     coords = podpac.Coordinates([[0, 1, 2], [0, 1, 2, 3, 4]],
                                 dims=["lat", "lon"])
     node = Arange()
     output = node.eval(coords)
     assert output.shape == coords.shape
示例#30
0
 def test_spatial_selection(self):
     node = SelectCoordinates(source=Arange(), lat=(46, 56, 1))
     o = node.eval(COORDS)