Ejemplo n.º 1
0
    def test_dual_mean_of_difference(self):
        # MEAN(B - A) and MEAN(C - A)
        shape = (500, 30, 40)
        size = np.prod(shape)
        raw_data = np.linspace(0, 1, num=size).reshape(shape)
        a_counter = AccessCounter(raw_data)
        a_array = biggus.NumpyArrayAdapter(a_counter)
        b_counter = AccessCounter(raw_data * 3)
        b_array = biggus.NumpyArrayAdapter(b_counter)
        c_counter = AccessCounter(raw_data * 5)
        c_array = biggus.NumpyArrayAdapter(c_counter)

        b_sub_a_array = biggus.sub(b_array, a_array)
        mean_b_sub_a_array = biggus.mean(b_sub_a_array, axis=0)
        c_sub_a_array = biggus.sub(c_array, a_array)
        mean_c_sub_a_array = biggus.mean(c_sub_a_array, axis=0)

        mean_b_sub_a, mean_c_sub_a = biggus.ndarrays([mean_b_sub_a_array,
                                                      mean_c_sub_a_array])

        # Are the resulting numbers equivalent?
        np.testing.assert_array_almost_equal(mean_b_sub_a,
                                             np.mean(raw_data * 2, axis=0))
        np.testing.assert_array_almost_equal(mean_c_sub_a,
                                             np.mean(raw_data * 4, axis=0))
        # Was the source data read just once?
        self.assert_counts(a_counter.counts, [1])
        self.assert_counts(b_counter.counts, [1])
        self.assert_counts(c_counter.counts, [1])
Ejemplo n.º 2
0
    def test_mean_of_a_and_mean_of_difference(self):
        # MEAN(A) and MEAN(A - B)
        shape = (500, 30, 40)
        size = np.prod(shape)
        raw_data = np.linspace(0, 1, num=size).reshape(shape)
        a_counter = AccessCounter(raw_data * 3)
        a_array = biggus.NumpyArrayAdapter(a_counter)
        b_counter = AccessCounter(raw_data)
        b_array = biggus.NumpyArrayAdapter(b_counter)

        sub_array = biggus.sub(a_array, b_array)
        mean_a_array = biggus.mean(a_array, axis=0)
        mean_sub_array = biggus.mean(sub_array, axis=0)
        mean_a, mean_sub = biggus.ndarrays([mean_a_array, mean_sub_array])

        # Are the resulting numbers equivalent?
        np.testing.assert_array_almost_equal(mean_a,
                                             np.mean(raw_data * 3, axis=0))
        np.testing.assert_array_almost_equal(mean_sub,
                                             np.mean(raw_data * 2, axis=0))

        # Was the source data read the minimal number of times?
        # (Allow first slice of A to be read twice because both `mean`
        # operations use it to to bootstrap their calculations.)
        self.assert_counts(a_counter.counts[0], [2])
        self.assert_counts(a_counter.counts[1:], [1])
        self.assert_counts(b_counter.counts, [1])
Ejemplo n.º 3
0
    def test_dual_mean_of_difference(self):
        # MEAN(B - A) and MEAN(C - A)
        shape = (500, 30, 40)
        size = np.prod(shape)
        raw_data = np.linspace(0, 1, num=size).reshape(shape)
        a_counter = AccessCounter(raw_data)
        a_array = biggus.NumpyArrayAdapter(a_counter)
        b_counter = AccessCounter(raw_data * 3)
        b_array = biggus.NumpyArrayAdapter(b_counter)
        c_counter = AccessCounter(raw_data * 5)
        c_array = biggus.NumpyArrayAdapter(c_counter)

        b_sub_a_array = biggus.sub(b_array, a_array)
        mean_b_sub_a_array = biggus.mean(b_sub_a_array, axis=0)
        c_sub_a_array = biggus.sub(c_array, a_array)
        mean_c_sub_a_array = biggus.mean(c_sub_a_array, axis=0)

        mean_b_sub_a, mean_c_sub_a = biggus.ndarrays(
            [mean_b_sub_a_array, mean_c_sub_a_array])

        # Are the resulting numbers equivalent?
        np.testing.assert_array_almost_equal(mean_b_sub_a,
                                             np.mean(raw_data * 2, axis=0))
        np.testing.assert_array_almost_equal(mean_c_sub_a,
                                             np.mean(raw_data * 4, axis=0))
        # Was the source data read just once?
        self.assert_counts(a_counter.counts, [1])
        self.assert_counts(b_counter.counts, [1])
        self.assert_counts(c_counter.counts, [1])
Ejemplo n.º 4
0
 def test_mean_of_mean(self):
     data = np.arange(24).reshape(3, 4, 2)
     array = biggus.NumpyArrayAdapter(data)
     mean1 = biggus.mean(array, axis=1)
     mean2 = biggus.mean(mean1, axis=-1)
     expected = np.mean(np.mean(data, axis=1), axis=-1)
     result = mean2.ndarray()
     np.testing.assert_array_equal(result, expected)
Ejemplo n.º 5
0
 def test_mean_of_mean(self):
     data = np.arange(24).reshape(3, 4, 2)
     array = biggus.NumpyArrayAdapter(data)
     mean1 = biggus.mean(array, axis=1)
     mean2 = biggus.mean(mean1, axis=-1)
     expected = np.mean(np.mean(data, axis=1), axis=-1)
     result = mean2.ndarray()
     np.testing.assert_array_equal(result, expected)
Ejemplo n.º 6
0
    def test__biggus_filter(self):
        shape = (1451, 1, 1)

        # Generate dummy data as biggus array.
        numpy_data = np.random.random(shape).astype(self.dtype)
        biggus_data = biggus.NumpyArrayAdapter(numpy_data)

        # Information for filter...
        # Dictionary of weights: key = offset (absolute value), value = weight
        weights = {0: 0.4, 1: 0.2, 2: 0.1}
        # This is equivalent to a weights array of [0.1, 0.2, 0.4, 0.2, 0.1].
        filter_halfwidth = len(weights) - 1

        # Filter data
        filtered_biggus_data = self._biggus_filter(biggus_data, weights)

        # Extract eddy component (original data - filtered data).
        eddy_biggus_data = (biggus_data[filter_halfwidth:-filter_halfwidth] -
                            filtered_biggus_data)

        # Aggregate over time dimension.
        mean_eddy_biggus_data = biggus.mean(eddy_biggus_data, axis=0)

        # Force evaluation.
        mean_eddy_numpy_data = mean_eddy_biggus_data.ndarray()

        # Confirm correct shape.
        self.assertEqual(mean_eddy_numpy_data.shape, shape[1:])
Ejemplo n.º 7
0
    def test__biggus_filter(self):
        shape = (1451, 1, 1)

        # Generate dummy data as biggus array.
        numpy_data = np.random.random(shape).astype(self.dtype)
        biggus_data = biggus.NumpyArrayAdapter(numpy_data)

        # Information for filter...
        # Dictionary of weights: key = offset (absolute value), value = weight
        weights = {0: 0.4, 1: 0.2, 2: 0.1}
        # This is equivalent to a weights array of [0.1, 0.2, 0.4, 0.2, 0.1].
        filter_halfwidth = len(weights) - 1

        # Filter data
        filtered_biggus_data = self._biggus_filter(biggus_data, weights)

        # Extract eddy component (original data - filtered data).
        eddy_biggus_data = (biggus_data[filter_halfwidth:-filter_halfwidth] -
                            filtered_biggus_data)

        # Aggregate over time dimension.
        mean_eddy_biggus_data = biggus.mean(eddy_biggus_data, axis=0)

        # Force evaluation.
        mean_eddy_numpy_data = mean_eddy_biggus_data.ndarray()

        # Confirm correct shape.
        self.assertEqual(mean_eddy_numpy_data.shape, shape[1:])
Ejemplo n.º 8
0
 def _check(self, data):
     array = biggus.NumpyArrayAdapter(data)
     result = mean(array, axis=0).masked_array()
     expected = ma.mean(data, axis=0)
     if expected.ndim == 0:
         expected = ma.asarray(expected)
     np.testing.assert_array_equal(result.filled(), expected.filled())
     np.testing.assert_array_equal(result.mask, expected.mask)
Ejemplo n.º 9
0
 def _check(self, data):
     array = biggus.NumpyArrayAdapter(data)
     result = mean(array, axis=0).masked_array()
     expected = ma.mean(data, axis=0)
     if expected.ndim == 0:
         expected = ma.asarray(expected)
     np.testing.assert_array_equal(result.filled(), expected.filled())
     np.testing.assert_array_equal(result.mask, expected.mask)
Ejemplo n.º 10
0
 def _check(self, data, dtype=None, shape=None):
     data = np.asarray(data, dtype=dtype)
     if shape is not None:
         data = data.reshape(shape)
     array = biggus.NumpyArrayAdapter(data)
     result = mean(array, axis=0).ndarray()
     expected = np.mean(data, axis=0)
     if expected.ndim == 0:
         expected = np.asarray(expected)
     np.testing.assert_array_equal(result, expected)
Ejemplo n.º 11
0
 def test_key_names(self):
     a = biggus.ConstantArray((2, 5))
     expr = biggus.mean(a + a, axis=1)
     graph = DaskEngine().graph(expr)
     func_names = {
         task[0].__name__
         for task in graph.values() if callable(task[0])
     }
     expected = {'gather', 'add', 'ConstantArray\n(2, 5)', 'mean\n(axis=1)'}
     self.assertEqual(expected, func_names)
Ejemplo n.º 12
0
 def _check(self, data, dtype=None, shape=None):
     data = np.asarray(data, dtype=dtype)
     if shape is not None:
         data = data.reshape(shape)
     array = biggus.NumpyArrayAdapter(data)
     result = mean(array, axis=0).ndarray()
     expected = np.mean(data, axis=0)
     if expected.ndim == 0:
         expected = np.asarray(expected)
     np.testing.assert_array_equal(result, expected)
Ejemplo n.º 13
0
    def test_means_across_different_axes(self):
        # MEAN(A, axis=0) and MEAN(A, axis=1)
        shape = (500, 30, 40)
        size = np.prod(shape)
        raw_data = np.linspace(0, 1, num=size).reshape(shape)
        a_counter = AccessCounter(raw_data * 3)
        a_array = biggus.NumpyArrayAdapter(a_counter)

        mean_0_array = biggus.mean(a_array, axis=0)
        mean_1_array = biggus.mean(a_array, axis=1)
        mean_0, mean_1 = biggus.ndarrays([mean_0_array, mean_1_array])

        # Are the resulting numbers equivalent?
        np.testing.assert_array_almost_equal(mean_0,
                                             np.mean(raw_data * 3, axis=0))
        np.testing.assert_array_almost_equal(mean_1,
                                             np.mean(raw_data * 3, axis=1))

        # Was the source data read the minimal number of times?
        self.assert_counts(a_counter.counts, [1])
Ejemplo n.º 14
0
 def _test_flow(self, axis):
     data = np.arange(3 * 4 * 5, dtype='f4').reshape(3, 4, 5)
     array = biggus.NumpyArrayAdapter(data)
     mean = biggus.mean(array, axis=axis)
     # Artificially constrain the chunk size to eight bytes to
     # ensure biggus is stepping across axes in the correct
     # order.
     with mock.patch('biggus._init.MAX_CHUNK_SIZE', 8):
         op_result, = biggus.ndarrays([mean])
     np_result = np.mean(data, axis=axis)
     np.testing.assert_array_almost_equal(op_result, np_result)
Ejemplo n.º 15
0
    def test_means_across_different_axes(self):
        # MEAN(A, axis=0) and MEAN(A, axis=1)
        shape = (500, 30, 40)
        size = np.prod(shape)
        raw_data = np.linspace(0, 1, num=size).reshape(shape)
        a_counter = AccessCounter(raw_data * 3)
        a_array = biggus.NumpyArrayAdapter(a_counter)

        mean_0_array = biggus.mean(a_array, axis=0)
        mean_1_array = biggus.mean(a_array, axis=1)
        mean_0, mean_1 = biggus.ndarrays([mean_0_array, mean_1_array])

        # Are the resulting numbers equivalent?
        np.testing.assert_array_almost_equal(mean_0,
                                             np.mean(raw_data * 3, axis=0))
        np.testing.assert_array_almost_equal(mean_1,
                                             np.mean(raw_data * 3, axis=1))

        # Was the source data read the minimal number of times?
        self.assert_counts(a_counter.counts, [1])
Ejemplo n.º 16
0
    def test_mean_of_difference(self):
        shape = (3, 4)
        size = np.prod(shape)
        raw_data1 = np.linspace(0.2, 1.0, num=size).reshape(shape)
        raw_data2 = np.linspace(0.3, 1.5, num=size).reshape(shape)
        array1 = biggus.NumpyArrayAdapter(raw_data1)
        array2 = biggus.NumpyArrayAdapter(raw_data2)
        difference = biggus.sub(array2, array1)
        mean_difference = biggus.mean(difference, axis=0)

        # Check the NumPy and biggus numeric values match.
        result = mean_difference.ndarray()
        numpy_result = np.mean(raw_data2 - raw_data1, axis=0)
        np.testing.assert_array_equal(result, numpy_result)
Ejemplo n.º 17
0
    def test_mean_of_difference(self):
        shape = (3, 4)
        size = np.prod(shape)
        raw_data1 = np.linspace(0.2, 1.0, num=size).reshape(shape)
        raw_data2 = np.linspace(0.3, 1.5, num=size).reshape(shape)
        array1 = biggus.NumpyArrayAdapter(raw_data1)
        array2 = biggus.NumpyArrayAdapter(raw_data2)
        difference = biggus.sub(array2, array1)
        mean_difference = biggus.mean(difference, axis=0)

        # Check the NumPy and biggus numeric values match.
        result = mean_difference.ndarray()
        numpy_result = np.mean(raw_data2 - raw_data1, axis=0)
        np.testing.assert_array_equal(result, numpy_result)
Ejemplo n.º 18
0
    def _test_mean_with_mdtol(self, data, axis, numpy_result, mdtol=None):
        data = AccessCounter(data)
        array = biggus.NumpyArrayAdapter(data)

        # Perform aggregation.
        if mdtol is None:
            # Allow testing of default when mdtol is None.
            biggus_aggregation = biggus.mean(array, axis=axis)
        else:
            biggus_aggregation = biggus.mean(array, axis=axis, mdtol=mdtol)

        # Check the aggregation operation doesn't actually read any data.
        self.assertTrue((data.counts == 0).all())

        # Check results.
        biggus_result = biggus_aggregation.masked_array()
        # Check resolving `op_array` to a NumPy array only reads
        # each relevant source value once.
        self.assertTrue((data.counts <= 1).all())
        numpy_mask = np.ma.getmaskarray(numpy_result)
        biggus_mask = np.ma.getmaskarray(biggus_result)
        np.testing.assert_array_equal(biggus_mask, numpy_mask)
        np.testing.assert_array_equal(biggus_result[~biggus_mask].data,
                                      numpy_result[~numpy_mask].data)
Ejemplo n.º 19
0
    def test_mean_of_difference(self):
        # MEAN(A - B)
        shape = (500, 30, 40)
        size = np.prod(shape)
        raw_data = np.linspace(0, 1, num=size).reshape(shape)
        data = AccessCounter(raw_data * 3)
        a_array = biggus.NumpyArrayAdapter(data)
        data = AccessCounter(raw_data)
        b_array = biggus.NumpyArrayAdapter(data)

        mean_array = biggus.mean(biggus.sub(a_array, b_array), axis=0)

        mean = mean_array.ndarray()
        np.testing.assert_array_almost_equal(mean,
                                             np.mean(raw_data * 2, axis=0))
Ejemplo n.º 20
0
    def test_mean_of_a_and_mean_of_difference(self):
        # MEAN(A) and MEAN(A - B)
        shape = (500, 30, 40)
        size = np.prod(shape)
        raw_data = np.linspace(0, 1, num=size).reshape(shape)
        a_counter = AccessCounter(raw_data * 3)
        a_array = biggus.NumpyArrayAdapter(a_counter)
        b_counter = AccessCounter(raw_data)
        b_array = biggus.NumpyArrayAdapter(b_counter)

        sub_array = biggus.sub(a_array, b_array)
        mean_a_array = biggus.mean(a_array, axis=0)
        mean_sub_array = biggus.mean(sub_array, axis=0)
        mean_a, mean_sub = biggus.ndarrays([mean_a_array, mean_sub_array])

        # Are the resulting numbers equivalent?
        np.testing.assert_array_almost_equal(mean_a,
                                             np.mean(raw_data * 3, axis=0))
        np.testing.assert_array_almost_equal(mean_sub,
                                             np.mean(raw_data * 2, axis=0))

        # Was the source data read the minimal number of times?
        self.assert_counts(a_counter.counts, [1])
        self.assert_counts(b_counter.counts, [1])
Ejemplo n.º 21
0
    def _test_mean_with_mdtol(self, data, axis, numpy_result, mdtol=None):
        data = AccessCounter(data)
        array = biggus.NumpyArrayAdapter(data)

        # Perform aggregation.
        if mdtol is None:
            # Allow testing of default when mdtol is None.
            biggus_aggregation = biggus.mean(array, axis=axis)
        else:
            biggus_aggregation = biggus.mean(array, axis=axis, mdtol=mdtol)

        # Check the aggregation operation doesn't actually read any data.
        self.assertTrue((data.counts == 0).all())

        # Check results.
        biggus_result = biggus_aggregation.masked_array()
        # Check resolving `op_array` to a NumPy array only reads
        # each relevant source value once.
        self.assertTrue((data.counts <= 1).all())
        numpy_mask = np.ma.getmaskarray(numpy_result)
        biggus_mask = np.ma.getmaskarray(biggus_result)
        np.testing.assert_array_equal(biggus_mask, numpy_mask)
        np.testing.assert_array_equal(biggus_result[~biggus_mask].data,
                                      numpy_result[~numpy_mask].data)
Ejemplo n.º 22
0
    def test_mean_of_difference(self):
        # MEAN(A - B)
        shape = (500, 30, 40)
        size = np.prod(shape)
        raw_data = np.linspace(0, 1, num=size).reshape(shape)
        data = AccessCounter(raw_data * 3)
        a_array = biggus.NumpyArrayAdapter(data)
        data = AccessCounter(raw_data)
        b_array = biggus.NumpyArrayAdapter(data)

        mean_array = biggus.mean(biggus.sub(a_array, b_array), axis=0)

        mean = mean_array.ndarray()
        np.testing.assert_array_almost_equal(mean,
                                             np.mean(raw_data * 2, axis=0))
Ejemplo n.º 23
0
 def _test_flow(self, axis):
     data = np.arange(3 * 4 * 5, dtype='f4').reshape(3, 4, 5)
     array = biggus.NumpyArrayAdapter(data)
     mean = biggus.mean(array, axis=axis)
     engine = biggus.AllThreadedEngine()
     chunk_size = biggus.MAX_CHUNK_SIZE
     try:
         # Artificially constrain the chunk size to eight bytes to
         # ensure biggus is stepping across axes in the correct
         # order.
         biggus.MAX_CHUNK_SIZE = 8
         op_result, = engine.ndarrays(mean)
     finally:
         biggus.MAX_CHUNK_SIZE = chunk_size
     np_result = np.mean(data, axis=axis)
     np.testing.assert_array_almost_equal(op_result, np_result)
Ejemplo n.º 24
0
 def _test_flow(self, axis):
     data = np.arange(3 * 4 * 5, dtype='f4').reshape(3, 4, 5)
     array = biggus.NumpyArrayAdapter(data)
     mean = biggus.mean(array, axis=axis)
     engine = biggus.AllThreadedEngine()
     chunk_size = biggus.MAX_CHUNK_SIZE
     try:
         # Artificially constrain the chunk size to eight bytes to
         # ensure biggus is stepping across axes in the correct
         # order.
         biggus.MAX_CHUNK_SIZE = 8
         op_result, = engine.ndarrays(mean)
     finally:
         biggus.MAX_CHUNK_SIZE = chunk_size
     np_result = np.mean(data, axis=axis)
     np.testing.assert_array_almost_equal(op_result, np_result)
Ejemplo n.º 25
0
    def test_different_shaped_accumulations(self):
        a = biggus.NumpyArrayAdapter(np.random.random(2))
        b = biggus.NumpyArrayAdapter(np.zeros((2, 2)))

        c = biggus.mean(b, axis=1)
        d = a - c

        graph = DaskEngine().graph(d)
        func_names = {
            task[0].__name__
            for task in graph.values() if callable(task[0])
        }
        expected = {
            'NumpyArrayAdapter\n(2, 2)', 'NumpyArrayAdapter\n(2,)',
            'mean\n(axis=1)', 'subtract', 'gather'
        }
        self.assertEqual(expected, func_names)
Ejemplo n.º 26
0
    def test_dual_aggregation(self):
        # Check the aggregation operations don't actually read any data.
        shape = (500, 30, 40)
        size = np.prod(shape)
        raw_data = np.linspace(0, 1, num=size).reshape(shape)
        counter = AccessCounter(raw_data)
        array = biggus.NumpyArrayAdapter(counter)
        mean_array = biggus.mean(array, axis=0)
        std_array = biggus.std(array, axis=0)
        self.assertIsInstance(mean_array, biggus.Array)
        self.assertIsInstance(std_array, biggus.Array)
        self.assertTrue((counter.counts == 0).all())

        mean, std_dev = biggus.ndarrays([mean_array, std_array])

        # Was the source data read just once?
        self.assert_counts(counter.counts, [1])
Ejemplo n.º 27
0
    def test_dual_aggregation(self):
        # Check the aggregation operations don't actually read any data.
        shape = (500, 30, 40)
        size = np.prod(shape)
        raw_data = np.linspace(0, 1, num=size).reshape(shape)
        counter = AccessCounter(raw_data)
        array = biggus.NumpyArrayAdapter(counter)
        mean_array = biggus.mean(array, axis=0)
        std_array = biggus.std(array, axis=0)
        self.assertIsInstance(mean_array, biggus.Array)
        self.assertIsInstance(std_array, biggus.Array)
        self.assertTrue((counter.counts == 0).all())

        mean, std_dev = biggus.ndarrays([mean_array, std_array])

        # Was the source data read just once?
        self.assert_counts(counter.counts, [1])
Ejemplo n.º 28
0
    def test_dual_aggregation(self):
        # Check the aggregation operations don't actually read any data.
        shape = (500, 30, 40)
        size = numpy.prod(shape)
        raw_data = numpy.linspace(0, 1, num=size).reshape(shape)
        data = _AccessCounter(raw_data)
        array = biggus.ArrayAdapter(data)
        mean_array = biggus.mean(array, axis=0)
        std_array = biggus.std(array, axis=0)
        self.assertIsInstance(mean_array, biggus.Array)
        self.assertIsInstance(std_array, biggus.Array)
        self.assertTrue((data.counts == 0).all())

        mean, std_dev = biggus.ndarrays([mean_array, std_array])
        # The first slice is read twice because both `mean` and `std`
        # use it to to bootstrap their rolling calculations.
        self.assertTrue((data.counts[0] == 2).all())
        self.assertTrue((data.counts[1:] == 1).all())
Ejemplo n.º 29
0
    def test_dual_aggregation(self):
        # Check the aggregation operations don't actually read any data.
        shape = (500, 30, 40)
        size = np.prod(shape)
        raw_data = np.linspace(0, 1, num=size).reshape(shape)
        counter = AccessCounter(raw_data)
        array = biggus.NumpyArrayAdapter(counter)
        mean_array = biggus.mean(array, axis=0)
        std_array = biggus.std(array, axis=0)
        self.assertIsInstance(mean_array, biggus.Array)
        self.assertIsInstance(std_array, biggus.Array)
        self.assertTrue((counter.counts == 0).all())

        mean, std_dev = biggus.ndarrays([mean_array, std_array])

        # Was the source data read the minimal number of times?
        # (Allow first slice of A to be read twice because both
        # `mean` and `std` operations use it to to bootstrap their
        # calculations.)
        self.assert_counts(counter.counts[0], [2])
        self.assert_counts(counter.counts[1:], [1])
Ejemplo n.º 30
0
    def get_seasonal_means_with_ttest_stats(
            self,
            season_to_monthperiod=None,
            start_year=None,
            end_year=None,
            convert_monthly_accumulators_to_daily=False):
        """

        :param season_to_monthperiod:
        :param start_year:
        :param end_year:
        :param convert_monthly_accumulators_to_daily: if true converts monthly accumulators to daily,
        :return dict(season: [mean, std, nobs])
        """

        if True:
            raise NotImplementedError(
                "Biggus way of calculation is not implemented, use the dask version of the method"
            )

        # select the interval of interest
        timesel = [
            i for i, d in enumerate(self.time)
            if start_year <= d.year <= end_year
        ]
        data = self.data[timesel, :, :]
        times = [self.time[i] for i in timesel]

        if convert_monthly_accumulators_to_daily:
            ndays = np.array(
                [calendar.monthrange(d.year, d.month)[1] for d in times])

            data = biggus.divide(data, ndays[:, np.newaxis, np.newaxis])

        else:
            data = self.data

        year_month_to_index_arr = defaultdict(list)
        for i, t in enumerate(times):
            year_month_to_index_arr[t.year, t.month].append(i)

        # calculate monthly means
        monthly_data = {}
        for y in range(start_year, end_year + 1):
            for m in range(1, 13):
                aslice = slice(year_month_to_index_arr[y, m][0],
                               year_month_to_index_arr[y, m][-1] + 1)
                monthly_data[y, m] = biggus.mean(
                    data[aslice.start:aslice.stop, :, :], axis=0)

        result = {}
        for season, month_period in season_to_monthperiod.items():
            assert isinstance(month_period, MonthPeriod)

            seasonal_means = []
            ndays_per_season = []

            for p in month_period.get_season_periods(start_year=start_year,
                                                     end_year=end_year):
                lmos = biggus.ArrayStack([
                    monthly_data[start.year, start.month]
                    for start in p.range("months")
                ])
                ndays_per_month = np.array([
                    calendar.monthrange(start.year, start.month)[1]
                    for start in p.range("months")
                ])

                seasonal_mean = biggus.sum(biggus.multiply(
                    lmos, ndays_per_month[:, np.newaxis, np.newaxis]),
                                           axis=0)
                seasonal_mean = biggus.divide(seasonal_mean,
                                              ndays_per_month.sum())

                seasonal_means.append(seasonal_mean)
                ndays_per_season.append(ndays_per_month.sum())

            seasonal_means = biggus.ArrayStack(seasonal_means)
            ndays_per_season = np.array(ndays_per_season)

            print(seasonal_means.shape, ndays_per_season.shape)

            assert seasonal_means.shape[0] == ndays_per_season.shape[0]

            clim_mean = biggus.sum(biggus.multiply(
                seasonal_means, ndays_per_season[:, np.newaxis, np.newaxis]),
                                   axis=0) / ndays_per_season.sum()

            diff = biggus.subtract(seasonal_means,
                                   clim_mean.masked_array()[np.newaxis, :, :])
            sq_mean = biggus.sum(biggus.multiply(
                diff**2, ndays_per_season[:, np.newaxis, np.newaxis]),
                                 axis=0) / ndays_per_season.sum()
            clim_std = biggus.power(sq_mean, 0.5)

            clim_mean = clim_mean.masked_array()
            print("calculated mean")
            clim_std = clim_std.masked_array()
            print("calculated std")

            result[season] = [clim_mean, clim_std, ndays_per_season.shape[0]]

        return result
Ejemplo n.º 31
0
 def test_mean_nd_array(self):
     r = biggus.mean(np.arange(12), axis=0)
     self.assertIsInstance(r._array, biggus.NumpyArrayAdapter)
     self.assertEqual(r.ndarray(), 5.5)
Ejemplo n.º 32
0
 def test_add_non_supported_type(self):
     # Check that the Aggregation raises a TypeError
     # if neither an Array or np.ndarray is given
     msg = "list' object has no attribute 'ndim"
     with six.assertRaisesRegex(self, AttributeError, msg):
         biggus.mean([0, 1, 2, 3, 4], axis=0)
Ejemplo n.º 33
0
 def _check(self, source, target):
     array = biggus.NumpyArrayAdapter(np.arange(2, dtype=source))
     agg = mean(array, axis=0)
     self.assertEqual(agg.dtype, target)
Ejemplo n.º 34
0
 def test_multiple(self):
     array = biggus.NumpyArrayAdapter(np.arange(12).reshape(3, 4))
     with self.assertRaises(biggus.AxisSupportError):
         mean(array, axis=(0, 1))
Ejemplo n.º 35
0
 def test_none(self):
     with self.assertRaises(AssertionError):
         mean(self.array)
Ejemplo n.º 36
0
 def test_mean_nd_array(self):
     r = biggus.mean(np.arange(12), axis=0)
     self.assertIsInstance(r._array, biggus.NumpyArrayAdapter)
     self.assertEqual(r.ndarray(), 5.5)
Ejemplo n.º 37
0
 def test_add_non_supported_type(self):
     # Check that the Aggregation raises a TypeError
     # if neither an Array or np.ndarray is given
     with self.assertRaisesRegexp(AttributeError,
                                  "list' object has no attribute 'ndim"):
         biggus.mean(range(10), axis=0)
Ejemplo n.º 38
0
 def test_none(self):
     with self.assertRaises(biggus.AxisSupportError):
         mean(self.array)
Ejemplo n.º 39
0
 def test_too_large(self):
     with self.assertRaises(ValueError):
         mean(self.array, axis=1)
Ejemplo n.º 40
0
 def test_non_zero(self):
     with self.assertRaises(AssertionError):
         mean(self.array, axis=1)
Ejemplo n.º 41
0
 def test_multiple(self):
     array = biggus.NumpyArrayAdapter(np.arange(12).reshape(3, 4))
     with self.assertRaises(AssertionError):
         mean(array, axis=(0, 1))
Ejemplo n.º 42
0
 def test_too_small(self):
     with self.assertRaises(ValueError):
         mean(self.array, axis=-2)
Ejemplo n.º 43
0
 def _check(self, source, target):
     array = biggus.NumpyArrayAdapter(np.arange(2, dtype=source))
     agg = mean(array, axis=0)
     self.assertEqual(agg.dtype, target)