def test_divide_float(self): r = biggus.divide(np.arange(3.), 2.) assert_array_equal(r.ndarray(), [0., 0.5, 1.])
def test_other_array(self): a = FakeArray([2, 4]) b = FakeArray([2, 4]) r = a.__div__(b) self.assertIsInstance(r, biggus._Elementwise) self.assertElementwise(r, biggus.divide(a, b))
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