Ejemplo n.º 1
0
	def bootcorr(	self, ntim = 1000, corrconf = 0.95, bootconf = 0.9,
					debug = False, quick = False	):
		from numpy import meshgrid, zeros, ma, isnan, linspace
		from atmos_ocean_data import vcorr, sig_test

		corrlevel = 1 - corrconf

		fieldData = self.sst.data
		clim_data = self.clim_data

		corr_grid = vcorr(X = fieldData, y = clim_data)

		n_yrs = len(clim_data)

		p_value = sig_test(corr_grid, n_yrs)

		#Mask insignificant gridpoints
		corr_grid = ma.masked_array(corr_grid, ~(p_value < corrlevel))
		#Mask land
		corr_grid = ma.masked_array(corr_grid, isnan(corr_grid))
		#Mask northern/southern ocean
		corr_grid.mask[self.sst.lat > 60] = True
		corr_grid.mask[self.sst.lat < -30] = True
		nlat = len(self.sst.lat)
		nlon = len(self.sst.lon)

		if quick:
			self.corr_grid = corr_grid
			self.n_pre_grid = nlat * nlon - corr_grid.mask.sum()
			if self.n_pre_grid == 0:
				self.flags['noSST'] = True
			else:
				self.flags['noSST'] = False
			return
		###INITIALIZE A NEW CORR GRID####

		count = np.zeros((nlat,nlon))


		dat = clim_data.copy()


		for boot in xrange(ntim):

			###SHUFFLE THE YEARS AND CREATE THE BOOT DATA###
			idx = np.random.randint(0, len(dat) - 1, len(dat))
			boot_fieldData = np.zeros((len(idx), nlat, nlon))
			boot_fieldData[:] = fieldData[idx]
			boot_climData = np.zeros((len(idx)))
			boot_climData = dat[idx]

			boot_corr_grid = vcorr(X = boot_fieldData, y = boot_climData)

			p_value = sig_test(boot_corr_grid, n_yrs)

			count[p_value <= corrlevel] += 1
			if debug:
				print 'Count max is %i' % count.max()


		###CREATE MASKED ARRAY USING THE COUNT AND BOOTCONF ATTRIBUTES
		corr_grid = np.ma.masked_array(corr_grid, count < bootconf * ntim)

		self.corr_grid = corr_grid
		self.n_pre_grid = nlat * nlon - corr_grid.mask.sum()
		if self.n_pre_grid == 0:
			self.flags['noSST'] = True
		else:
			self.flags['noSST'] = False
		return
Ejemplo n.º 2
0
	def bootcorr(self, n = 100, fig = None, ax = None, field = 'sst', \
		phase = 'allyears', corrconf = 0.9, bootconf = 0.9, cbloc = 'bottom',\
		quick = False, debug = False, monte = False):
		from numpy import meshgrid, zeros, ma, isnan, linspace
		import time
		from random import sample

		if field == 'sst':
			fieldData = self.sst[phase]
		if field == 'slp':
			fieldData = self.slp[phase]

		clim_data = self.clim_data[phase]

		corrlevel = 1 - corrconf

		corr_grid = vcorr(X = fieldData, y = clim_data)

		n_yrs = len(clim_data)

		p_value = sig_test(corr_grid, n_yrs)

		#Mask insignificant gridpoints
		corr_grid = ma.masked_array(corr_grid, ~(p_value < corrlevel))
		#Mask land
		corr_grid = ma.masked_array(corr_grid, isnan(corr_grid))
		#Mask northern/southern ocean
		corr_grid.mask[self.lat[field] > 60] = True
		corr_grid.mask[self.lat[field] < -30] = True

		###SAVE THE MASK TO FILTER THE BOOTSTRAP
		mask = corr_grid.mask

		if quick:
			self.corr_grid[field][phase] = corr_grid
			return
			###SET UP INDICES FOR FIELD DATA###

		###INITIALIZE A NEW CORR GRID####
		nlat = fieldData.shape[1]
		nlon = fieldData.shape[2]
		count = np.zeros((nlat,nlon))
		ntim = n
		dat = clim_data

		mask = corr_grid.mask
		if debug:
			print 'Starting %s' % phase


		for boot in xrange(ntim):
			if debug:
				print 'starting round %i' % boot

			###SHUFFLE THE YEARS AND CREATE THE BOOT DATA###
			idx = np.random.randint(0, len(dat) - 1, len(dat))
			bootdata = np.zeros((len(idx), nlat, nlon))
			bootdata[:] = fieldData[idx]
			bootvar = np.zeros((len(idx)))
			bootvar = dat[idx]

			corr_grid_boot = vcorr(X = bootdata, y = bootvar)

			n_yrs = len(bootvar)

			p_value = sig_test(corr_grid_boot, n_yrs)

			count[p_value <= corrlevel] += 1
			if debug:
				print 'Count max is %i' % count.max()
			# for lon, lat in zip(xx[~mask], yy[~mask]):
			# 		c, p = corr(bootdata[:,lat,lon], bootvar)
			# 		if p <= corrlevel:
			# 			count[lat,lon] += 1

		###GET THE ACTUAL CORRELATION AGAIN

		#_Mask insignificant values
		###CREATE MASKED ARRAY USING THE COUNT AND BOOTCONF ATTRIBUTES
		corr_grid = np.ma.masked_array(corr_grid, count < bootconf * ntim)

		self.corr_grid[field][phase] = corr_grid

		if monte:
			n_phase = len(clim_data)
			n_total = len(self.clim_data['allyears'])
			count = np.zeros((nlat, nlon))
			field = self.sst[phase]
			for t in xrange(100):
				#print 'Starting monte round %i' % t
				idx = sample(xrange(n_total), n_phase)
				var = self.clim_data['allyears'][idx]
				for lon, lat in zip(xx[~mask], yy[~mask]):
					r, p = corr(var, field[:,lat,lon])
					if p <= (1 - corrconf):
						#print 'Entering while loop for grid %i, %i' % (i, j)
						x = 0
						c2 = 0
						while x < 100:
							#print 'Monte round %i' % x
							idx2 = np.random.randint(0, n_phase-1, n_phase)
							x += 1
							r, p = corr(var[idx2], field[idx2,lat,lon])
							if p <= (1 - corrconf):
								c2 += 1
						if c2 >= 80:
							count[lat,lon] += 1
			print 'Count max is %.0f' % (count.max())
			self.monte_count[phase] = count.max()
			self.monte_grid[phase] = np.ma.masked_array(data = count, \
										mask = self.corr_grid['sst'][phase].mask)
		return