Python numpy.fromfunction() Examples

The following are 30 code examples of numpy.fromfunction(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module numpy , or try the search function .
Example #1
Source File: graph_util.py    From nasbench with Apache License 2.0 6 votes vote down vote up
def permute_graph(graph, label, permutation):
  """Permutes the graph and labels based on permutation.

  Args:
    graph: np.ndarray adjacency matrix.
    label: list of labels of same length as graph dimensions.
    permutation: a permutation list of ints of same length as graph dimensions.

  Returns:
    np.ndarray where vertex permutation[v] is vertex v from the original graph
  """
  # vertex permutation[v] in new graph is vertex v in the old graph
  forward_perm = zip(permutation, list(range(len(permutation))))
  inverse_perm = [x[1] for x in sorted(forward_perm)]
  edge_fn = lambda x, y: graph[inverse_perm[x], inverse_perm[y]] == 1
  new_matrix = np.fromfunction(np.vectorize(edge_fn),
                               (len(label), len(label)),
                               dtype=np.int8)
  new_label = [label[inverse_perm[i]] for i in range(len(label))]
  return new_matrix, new_label 
Example #2
Source File: from_data.py    From chainer with MIT License 6 votes vote down vote up
def fromfunction(function, shape, **kwargs):
    """ Constructs an array by executing a function over each coordinate.

    This is currently equivalent to :func:`numpy.fromfunction`
    wrapped by :func:`chainerx.array`, given the device argument.

    Note:
        Keywords other than ``dtype`` and ``device`` are passed to
        ```function```.

    .. seealso:: :func:`numpy.fromfunction`

    """
    dtype = kwargs.pop('dtype', float)
    device = kwargs.pop('device', None)
    return chainerx.array(
        numpy.fromfunction(
            function, shape, dtype=dtype, **kwargs),
        device=device)


# TODO(hvy): Optimize with pre-allocated memory using count for non-native
# devices. 
Example #3
Source File: test_kd_tree.py    From pyresample with GNU Lesser General Public License v3.0 6 votes vote down vote up
def test_nearest_masked_swath_target(self):
        """Test that a masked array works as a target."""
        data = np.fromfunction(lambda y, x: y * x, (50, 10))
        lons = np.fromfunction(lambda y, x: 3 + x, (50, 10))
        lats = np.fromfunction(lambda y, x: 75 - y, (50, 10))
        mask = np.ones_like(lons, dtype=np.bool)
        mask[::2, ::2] = False
        swath_def = geometry.SwathDefinition(
            lons=np.ma.masked_array(lons, mask=mask),
            lats=np.ma.masked_array(lats, mask=False)
        )
        res = kd_tree.resample_nearest(swath_def, data.ravel(),
                                       swath_def, 50000, segments=3)
        cross_sum = res.sum()
        # expected = 12716  # if masks aren't respected
        expected = 12000
        self.assertEqual(cross_sum, expected) 
Example #4
Source File: test_kd_tree.py    From pyresample with GNU Lesser General Public License v3.0 6 votes vote down vote up
def test_gauss_multi(self):
        data = np.fromfunction(lambda y, x: (y + x) * 10 ** -6, (5000, 100))
        lons = np.fromfunction(
            lambda y, x: 3 + (10.0 / 100) * x, (5000, 100))
        lats = np.fromfunction(
            lambda y, x: 75 - (50.0 / 5000) * y, (5000, 100))
        swath_def = geometry.SwathDefinition(lons=lons, lats=lats)
        data_multi = np.column_stack((data.ravel(), data.ravel(),
                                      data.ravel()))
        with catch_warnings(UserWarning) as w:
            res = kd_tree.resample_gauss(swath_def, data_multi,
                                         self.area_def, 50000, [25000, 15000, 10000], segments=1)
            self.assertFalse(len(w) != 1)
            self.assertFalse(('Possible more' not in str(w[0].message)))
        cross_sum = res.sum()
        expected = 1461.8429990248171
        self.assertAlmostEqual(cross_sum, expected) 
Example #5
Source File: test_kd_tree.py    From pyresample with GNU Lesser General Public License v3.0 6 votes vote down vote up
def test_gauss_multi_mp(self):
        data = np.fromfunction(lambda y, x: (y + x) * 10 ** -6, (5000, 100))
        lons = np.fromfunction(
            lambda y, x: 3 + (10.0 / 100) * x, (5000, 100))
        lats = np.fromfunction(
            lambda y, x: 75 - (50.0 / 5000) * y, (5000, 100))
        swath_def = geometry.SwathDefinition(lons=lons, lats=lats)
        data_multi = np.column_stack((data.ravel(), data.ravel(),
                                      data.ravel()))
        with catch_warnings(UserWarning) as w:
            res = kd_tree.resample_gauss(swath_def, data_multi,
                                         self.area_def, 50000, [
                                             25000, 15000, 10000],
                                         nprocs=2, segments=1)
            self.assertFalse(len(w) != 1)
            self.assertFalse(('Possible more' not in str(w[0].message)))
        cross_sum = res.sum()
        expected = 1461.8429990248171
        self.assertAlmostEqual(cross_sum, expected) 
Example #6
Source File: test_kd_tree.py    From pyresample with GNU Lesser General Public License v3.0 6 votes vote down vote up
def test_gauss_multi_mp_segments(self):
        data = np.fromfunction(lambda y, x: (y + x) * 10 ** -6, (5000, 100))
        lons = np.fromfunction(
            lambda y, x: 3 + (10.0 / 100) * x, (5000, 100))
        lats = np.fromfunction(
            lambda y, x: 75 - (50.0 / 5000) * y, (5000, 100))
        swath_def = geometry.SwathDefinition(lons=lons, lats=lats)
        data_multi = np.column_stack((data.ravel(), data.ravel(),
                                      data.ravel()))
        with catch_warnings(UserWarning) as w:
            res = kd_tree.resample_gauss(swath_def, data_multi,
                                         self.area_def, 50000, [
                                             25000, 15000, 10000],
                                         nprocs=2, segments=1)
            self.assertFalse(len(w) != 1)
            self.assertFalse('Possible more' not in str(w[0].message))
        cross_sum = res.sum()
        expected = 1461.8429990248171
        self.assertAlmostEqual(cross_sum, expected) 
Example #7
Source File: test_kd_tree.py    From pyresample with GNU Lesser General Public License v3.0 6 votes vote down vote up
def test_masked_gauss(self):
        data = np.ones((50, 10))
        data[:, 5:] = 2
        lons = np.fromfunction(lambda y, x: 3 + x, (50, 10))
        lats = np.fromfunction(lambda y, x: 75 - y, (50, 10))
        swath_def = geometry.SwathDefinition(lons=lons, lats=lats)
        mask = np.ones((50, 10))
        mask[:, :5] = 0
        masked_data = np.ma.array(data, mask=mask)
        res = kd_tree.resample_gauss(swath_def, masked_data.ravel(),
                                     self.area_def, 50000, 25000, segments=1)
        expected_mask = np.fromfile(os.path.join(os.path.dirname(__file__),
                                                 'test_files',
                                                 'mask_test_mask.dat'),
                                    sep=' ').reshape((800, 800))
        expected_data = np.fromfile(os.path.join(os.path.dirname(__file__),
                                                 'test_files',
                                                 'mask_test_data.dat'),
                                    sep=' ').reshape((800, 800))
        expected = expected_data.sum()
        cross_sum = res.data.sum()

        self.assertTrue(np.array_equal(expected_mask, res.mask))
        self.assertAlmostEqual(cross_sum, expected, places=3) 
Example #8
Source File: test_kd_tree.py    From pyresample with GNU Lesser General Public License v3.0 6 votes vote down vote up
def test_masked_full(self):
        data = np.ones((50, 10))
        data[:, 5:] = 2
        mask = np.ones((50, 10))
        mask[:, :5] = 0
        masked_data = np.ma.array(data, mask=mask)
        lons = np.fromfunction(lambda y, x: 3 + x, (50, 10))
        lats = np.fromfunction(lambda y, x: 75 - y, (50, 10))
        swath_def = geometry.SwathDefinition(lons=lons, lats=lats)
        res = kd_tree.resample_nearest(swath_def,
                                       masked_data.ravel(
                                       ), self.area_def, 50000,
                                       fill_value=None, segments=1)
        expected_fill_mask = np.fromfile(os.path.join(os.path.dirname(__file__),
                                                      'test_files',
                                                      'mask_test_full_fill.dat'),
                                         sep=' ').reshape((800, 800))
        fill_mask = res.mask

        self.assertTrue(np.array_equal(fill_mask, expected_fill_mask)) 
Example #9
Source File: ROI.py    From tf-pose with Apache License 2.0 6 votes vote down vote up
def getArrayRegion(self, arr, img=None, axes=(0, 1), **kwds):
        """
        Return the result of ROI.getArrayRegion() masked by the elliptical shape
        of the ROI. Regions outside the ellipse are set to 0.
        """
        # Note: we could use the same method as used by PolyLineROI, but this
        # implementation produces a nicer mask.
        arr = ROI.getArrayRegion(self, arr, img, axes, **kwds)
        if arr is None or arr.shape[axes[0]] == 0 or arr.shape[axes[1]] == 0:
            return arr
        w = arr.shape[axes[0]]
        h = arr.shape[axes[1]]

        ## generate an ellipsoidal mask
        mask = np.fromfunction(lambda x,y: (((x+0.5)/(w/2.)-1)**2+ ((y+0.5)/(h/2.)-1)**2)**0.5 < 1, (w, h))
        
        # reshape to match array axes
        if axes[0] > axes[1]:
            mask = mask.T
        shape = [(n if i in axes else 1) for i,n in enumerate(arr.shape)]
        mask = mask.reshape(shape)
        
        return arr * mask 
Example #10
Source File: graph_util.py    From eval-nas with MIT License 6 votes vote down vote up
def permute_graph(graph, label, permutation):
    """Permutes the graph and labels based on permutation.

    Args:
      graph: np.ndarray adjacency matrix.
      label: list of labels of same length as graph dimensions.
      permutation: a permutation list of ints of same length as graph dimensions.

    Returns:
      np.ndarray where vertex permutation[v] is vertex v from the original graph
    """
    # vertex permutation[v] in new graph is vertex v in the old graph
    forward_perm = zip(permutation, list(range(len(permutation))))
    inverse_perm = [x[1] for x in sorted(forward_perm)]
    edge_fn = lambda x, y: graph[inverse_perm[x], inverse_perm[y]] == 1
    new_matrix = np.fromfunction(np.vectorize(edge_fn),
                                 (len(label), len(label)),
                                 dtype=np.int8)
    new_label = [label[inverse_perm[i]] for i in range(len(label))]
    return new_matrix, new_label 
Example #11
Source File: test_kd_tree.py    From pyresample with GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_dtype(self):
        lons = np.fromfunction(lambda y, x: 3 + x, (50, 10))
        lats = np.fromfunction(lambda y, x: 75 - y, (50, 10))
        grid_def = geometry.GridDefinition(lons, lats)
        lons = np.asarray(lons, dtype='f4')
        lats = np.asarray(lats, dtype='f4')
        swath_def = geometry.SwathDefinition(lons=lons, lats=lats)
        valid_input_index, valid_output_index, index_array, distance_array = \
            kd_tree.get_neighbour_info(swath_def,
                                       grid_def,
                                       50000, neighbours=1, segments=1) 
Example #12
Source File: test_kd_tree.py    From pyresample with GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_masked_nearest_1d(self):
        data = np.ones((800, 800))
        data[:400, :] = 2
        lons = np.fromfunction(lambda x: 3 + x / 100., (500,))
        lats = np.fromfunction(lambda x: 75 - x / 10., (500,))
        swath_def = geometry.SwathDefinition(lons=lons, lats=lats)
        mask = np.ones((800, 800))
        mask[400:, :] = 0
        masked_data = np.ma.array(data, mask=mask)
        res = kd_tree.resample_nearest(self.area_def, masked_data.ravel(),
                                       swath_def, 50000, segments=1)
        self.assertEqual(res.mask.sum(), 112) 
Example #13
Source File: test_kd_tree.py    From pyresample with GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_custom_multi(self):
        def wf1(dist):
            return 1 - dist / 100000.0

        def wf2(dist):
            return 1

        def wf3(dist):
            return np.cos(dist) ** 2

        data = np.fromfunction(lambda y, x: (y + x) * 10 ** -6, (5000, 100))
        lons = np.fromfunction(
            lambda y, x: 3 + (10.0 / 100) * x, (5000, 100))
        lats = np.fromfunction(
            lambda y, x: 75 - (50.0 / 5000) * y, (5000, 100))
        swath_def = geometry.SwathDefinition(lons=lons, lats=lats)
        data_multi = np.column_stack((data.ravel(), data.ravel(),
                                      data.ravel()))
        with catch_warnings(UserWarning) as w:
            res = kd_tree.resample_custom(swath_def, data_multi,
                                          self.area_def, 50000, [wf1, wf2, wf3], segments=1)
            self.assertFalse(len(w) != 1)
            self.assertFalse('Possible more' not in str(w[0].message))
        cross_sum = res.sum()
        expected = 1461.8428378742638
        self.assertAlmostEqual(cross_sum, expected) 
Example #14
Source File: test_data_reduce.py    From pyresample with GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_reduce_north_pole(self):
        """Test reducing around the poles."""

        from pyresample import utils
        area_id = 'ease_sh'
        description = 'Antarctic EASE grid'
        proj_id = 'ease_sh'
        projection = '+proj=laea +lat_0=-90 +lon_0=0 +a=6371228.0 +units=m'
        x_size = 425
        y_size = 425
        area_extent = (-5326849.0625, -5326849.0625,
                       5326849.0625, 5326849.0625)
        area_def = utils.get_area_def(area_id, description, proj_id,
                                      projection, x_size, y_size, area_extent)

        grid_lons, grid_lats = area_def.get_lonlats()

        area_id = 'ease_sh'
        description = 'Antarctic EASE grid'
        proj_id = 'ease_sh'
        projection = '+proj=laea +lat_0=-90 +lon_0=0 +a=6371228.0 +units=m'
        x_size = 1000
        y_size = 1000
        area_extent = (-532684.0625, -532684.0625, 532684.0625, 532684.0625)
        smaller_area_def = utils.get_area_def(area_id, description, proj_id,
                                              projection, x_size, y_size,
                                              area_extent)

        data = np.fromfunction(lambda y, x: (y + x), (1000, 1000))
        lons, lats = smaller_area_def.get_lonlats()

        lons, lats, data = swath_from_lonlat_grid(grid_lons, grid_lats,
                                                  lons, lats, data, 7000)

        cross_sum = data.sum()
        expected = 999000000.0
        self.assertAlmostEqual(cross_sum, expected) 
Example #15
Source File: test_kd_tree.py    From pyresample with GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_custom(self):
        def wf(dist):
            return 1 - dist / 100000.0

        data = np.fromfunction(lambda y, x: (y + x) * 10 ** -5, (5000, 100))
        lons = np.fromfunction(
            lambda y, x: 3 + (10.0 / 100) * x, (5000, 100))
        lats = np.fromfunction(
            lambda y, x: 75 - (50.0 / 5000) * y, (5000, 100))
        swath_def = geometry.SwathDefinition(lons=lons, lats=lats)
        with catch_warnings(UserWarning) as w:
            res = kd_tree.resample_custom(swath_def, data.ravel(),
                                          self.area_def, 50000, wf, segments=1)
            # PyProj proj/CRS and "more than 8 neighbours" are warned about
            self.assertFalse(len(w) > 2)
            neighbour_warn = False
            for warn in w:
                if 'Possible more' in str(warn.message):
                    neighbour_warn = True
                    break
            self.assertTrue(neighbour_warn)
            if len(w) == 2:
                proj_crs_warn = False
                for warn in w:
                    if 'important projection information' in str(warn.message):
                        proj_crs_warn = True
                        break
                self.assertTrue(proj_crs_warn)

        cross_sum = res.sum()
        expected = 4872.8100347930776
        self.assertAlmostEqual(cross_sum, expected) 
Example #16
Source File: test_kd_tree.py    From pyresample with GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_nearest_from_sample(self):
        data = np.fromfunction(lambda y, x: y * x, (50, 10))
        lons = np.fromfunction(lambda y, x: 3 + x, (50, 10))
        lats = np.fromfunction(lambda y, x: 75 - y, (50, 10))
        swath_def = geometry.SwathDefinition(lons=lons, lats=lats)
        valid_input_index, valid_output_index, index_array, distance_array = \
            kd_tree.get_neighbour_info(swath_def,
                                       self.area_def,
                                       50000, neighbours=1, segments=1)
        res = kd_tree.get_sample_from_neighbour_info('nn', (800, 800), data.ravel(),
                                                     valid_input_index, valid_output_index,
                                                     index_array)
        cross_sum = res.sum()
        expected = 15874591.0
        self.assertEqual(cross_sum, expected) 
Example #17
Source File: test_kd_tree.py    From pyresample with GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_masked_fill_float(self):
        data = np.ones((50, 10))
        lons = np.fromfunction(lambda y, x: 3 + x, (50, 10))
        lats = np.fromfunction(lambda y, x: 75 - y, (50, 10))
        swath_def = geometry.SwathDefinition(lons=lons, lats=lats)
        res = kd_tree.resample_nearest(swath_def, data.ravel(),
                                       self.area_def, 50000, fill_value=None, segments=1)
        expected_fill_mask = np.fromfile(os.path.join(os.path.dirname(__file__),
                                                      'test_files',
                                                      'mask_test_fill_value.dat'),
                                         sep=' ').reshape((800, 800))
        fill_mask = res.mask
        self.assertTrue(np.array_equal(fill_mask, expected_fill_mask)) 
Example #18
Source File: test_kd_tree.py    From pyresample with GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_nearest_empty_masked(self):
        data = np.fromfunction(lambda y, x: y * x, (50, 10))
        lons = np.fromfunction(lambda y, x: 165 + x, (50, 10))
        lats = np.fromfunction(lambda y, x: 75 - y, (50, 10))
        swath_def = geometry.SwathDefinition(lons=lons, lats=lats)
        res = kd_tree.resample_nearest(swath_def, data.ravel(),
                                       self.area_def, 50000, segments=1,
                                       fill_value=None)
        cross_sum = res.mask.sum()
        expected = res.size
        self.assertTrue(cross_sum == expected) 
Example #19
Source File: test_kd_tree.py    From pyresample with GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_nearest_empty_multi_masked(self):
        data = np.fromfunction(lambda y, x: y * x, (50, 10))
        lons = np.fromfunction(lambda y, x: 165 + x, (50, 10))
        lats = np.fromfunction(lambda y, x: 75 - y, (50, 10))
        data_multi = np.column_stack((data.ravel(), data.ravel(),
                                      data.ravel()))
        swath_def = geometry.SwathDefinition(lons=lons, lats=lats)
        res = kd_tree.resample_nearest(swath_def, data_multi,
                                       self.area_def, 50000, segments=1,
                                       fill_value=None)
        self.assertEqual(res.shape, (800, 800, 3)) 
Example #20
Source File: test_kd_tree.py    From pyresample with GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_gauss_multi_mp_segments_empty(self):
        data = np.fromfunction(lambda y, x: (y + x) * 10 ** -6, (5000, 100))
        lons = np.fromfunction(
            lambda y, x: 165 + (10.0 / 100) * x, (5000, 100))
        lats = np.fromfunction(
            lambda y, x: 75 - (50.0 / 5000) * y, (5000, 100))
        swath_def = geometry.SwathDefinition(lons=lons, lats=lats)
        data_multi = np.column_stack((data.ravel(), data.ravel(),
                                      data.ravel()))
        res = kd_tree.resample_gauss(swath_def, data_multi,
                                     self.area_def, 50000, [
                                         25000, 15000, 10000],
                                     nprocs=2, segments=1)
        cross_sum = res.sum()
        self.assertTrue(cross_sum == 0) 
Example #21
Source File: test_kd_tree.py    From pyresample with GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_nearest_empty(self):
        data = np.fromfunction(lambda y, x: y * x, (50, 10))
        lons = np.fromfunction(lambda y, x: 165 + x, (50, 10))
        lats = np.fromfunction(lambda y, x: 75 - y, (50, 10))
        swath_def = geometry.SwathDefinition(lons=lons, lats=lats)
        res = kd_tree.resample_nearest(swath_def, data.ravel(),
                                       self.area_def, 50000, segments=1)
        cross_sum = res.sum()
        expected = 0
        self.assertEqual(cross_sum, expected) 
Example #22
Source File: test_kd_tree.py    From pyresample with GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_nearest_1d(self):
        data = np.fromfunction(lambda x, y: x * y, (800, 800))
        lons = np.fromfunction(lambda x: 3 + x / 100., (500,))
        lats = np.fromfunction(lambda x: 75 - x / 10., (500,))
        swath_def = geometry.SwathDefinition(lons=lons, lats=lats)
        res = kd_tree.resample_nearest(self.area_def, data.ravel(),
                                       swath_def, 50000, segments=1)
        cross_sum = res.sum()
        expected = 35821299.0
        self.assertEqual(res.shape, (500,))
        self.assertEqual(cross_sum, expected) 
Example #23
Source File: test_kd_tree.py    From pyresample with GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_nearest(self):
        data = np.fromfunction(lambda y, x: y * x, (50, 10))
        lons = np.fromfunction(lambda y, x: 3 + x, (50, 10))
        lats = np.fromfunction(lambda y, x: 75 - y, (50, 10))
        swath_def = geometry.SwathDefinition(lons=lons, lats=lats)
        res = kd_tree.resample_nearest(swath_def, data.ravel(),
                                       self.area_def, 50000, segments=1)
        cross_sum = res.sum()
        expected = 15874591.0
        self.assertEqual(cross_sum, expected) 
Example #24
Source File: test_kd_tree.py    From pyresample with GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_nearest_mp(self):
        data = np.fromfunction(lambda y, x: y * x, (50, 10))
        lons = np.fromfunction(lambda y, x: 3 + x, (50, 10))
        lats = np.fromfunction(lambda y, x: 75 - y, (50, 10))
        swath_def = geometry.SwathDefinition(lons=lons, lats=lats)
        res = kd_tree.resample_nearest(swath_def, data.ravel(),
                                       self.area_def, 50000, nprocs=2, segments=1)
        cross_sum = res.sum()
        expected = 15874591.0
        self.assertEqual(cross_sum, expected) 
Example #25
Source File: test_data_reduce.py    From pyresample with GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_area_con_reduce(self):
        data = np.fromfunction(lambda y, x: (y + x), (1000, 1000))
        lons = np.fromfunction(
            lambda y, x: -180 + (360.0 / 1000) * x, (1000, 1000))
        lats = np.fromfunction(
            lambda y, x: -90 + (180.0 / 1000) * y, (1000, 1000))
        grid_lons, grid_lats = self.area_def.get_lonlats()
        valid_index = get_valid_index_from_lonlat_grid(grid_lons, grid_lats,
                                                       lons, lats, 7000)
        data = data[valid_index]
        cross_sum = data.sum()
        expected = 20685125.0
        self.assertAlmostEqual(cross_sum, expected) 
Example #26
Source File: test_data_reduce.py    From pyresample with GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_cartesian_reduce(self):
        data = np.fromfunction(lambda y, x: (y + x), (1000, 1000))
        lons = np.fromfunction(
            lambda y, x: -180 + (360.0 / 1000) * x, (1000, 1000))
        lats = np.fromfunction(
            lambda y, x: -90 + (180.0 / 1000) * y, (1000, 1000))
        grid = self.area_def.get_cartesian_coords()
        lons, lats, data = swath_from_cartesian_grid(grid, lons, lats, data,
                                                     7000)
        cross_sum = data.sum()
        expected = 20685125.0
        self.assertAlmostEqual(cross_sum, expected) 
Example #27
Source File: test_data_reduce.py    From pyresample with GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_reduce_boundary(self):
        data = np.fromfunction(lambda y, x: (y + x), (1000, 1000))
        lons = np.fromfunction(
            lambda y, x: -180 + (360.0 / 1000) * x, (1000, 1000))
        lats = np.fromfunction(
            lambda y, x: -90 + (180.0 / 1000) * y, (1000, 1000))
        boundary_lonlats = self.area_def.get_boundary_lonlats()
        lons, lats, data = swath_from_lonlat_boundaries(boundary_lonlats[0],
                                                        boundary_lonlats[1],
                                                        lons, lats, data, 7000)
        cross_sum = data.sum()
        expected = 20685125.0
        self.assertAlmostEqual(cross_sum, expected) 
Example #28
Source File: test_data_reduce.py    From pyresample with GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_reduce(self):
        data = np.fromfunction(lambda y, x: (y + x), (1000, 1000))
        lons = np.fromfunction(
            lambda y, x: -180 + (360.0 / 1000) * x, (1000, 1000))
        lats = np.fromfunction(
            lambda y, x: -90 + (180.0 / 1000) * y, (1000, 1000))
        grid_lons, grid_lats = self.area_def.get_lonlats()
        lons, lats, data = swath_from_lonlat_grid(grid_lons, grid_lats,
                                                  lons, lats, data,
                                                  7000)
        cross_sum = data.sum()
        expected = 20685125.0
        self.assertAlmostEqual(cross_sum, expected) 
Example #29
Source File: linbasex.py    From PyAbel with MIT License 5 votes vote down vote up
def _Slices(Beta, legendre_orders, smoothing=0):
    """Convolve Beta with a Gaussian function of 1/e width smoothing.

    """

    pol = len(legendre_orders)
    NP = len(Beta[0])  # number of points in 3_d plot.
    index = range(NP)

    Beta_convol = np.zeros((pol, NP))
    Slice_3D = np.zeros((pol, 2*NP, 2*NP))

    # Convolve Beta's with smoothing function
    if smoothing > 0:
        # smoothing function
        Basis_s = np.fromfunction(lambda i: np.exp(-(i - (NP)/2)**2 /
                                  (2*smoothing**2))/(smoothing*2.5), (NP,))
        for i in range(pol):
            Beta_convol[i] = np.convolve(Basis_s, Beta[i], mode='same')
    else:
        Beta_convol = Beta

    # Calculate ordered slices:
    for i in range(pol):
        Slice_3D[i] = np.fromfunction(lambda k, l: _SL(i, (k-NP), (l-NP),
                       Beta_convol, index, legendre_orders), (2*NP, 2*NP))
    # Sum ordered slices up
    Slice = np.sum(Slice_3D, axis=0)

    return Slice, Beta_convol 
Example #30
Source File: test_dcorrx.py    From hyppo with Apache License 2.0 5 votes vote down vote up
def test_distance(self):
        n = 6
        x = np.ones(n)
        y = np.arange(1, n + 1)

        distx = np.zeros((n, n))
        disty = np.fromfunction(lambda i, j: np.abs(i - j), (n, n))

        stat1 = DcorrX(max_lag=1).test(x, y)[0]
        stat2 = DcorrX(max_lag=1).test(distx, disty)[0]

        assert_almost_equal(stat1, stat2, decimal=0)