Пример #1
0
    def testCrpss(self):
        crpss_dict = self.crpssDict()
        crpss = Crpss(**crpss_dict)
        crpss.prepareInput()
        crpss.analyze()
        
        #test file names
        res_files = [f for r,_,files in os.walk(self.tmp_dir+'/output') for f in files]
        test_files = [f for r,_,files in os.walk(self.test_path+'/output') for f in files]
        self.assertEqual(sorted(res_files),sorted(test_files))
        res_files = [f for r,_,files in os.walk(self.tmp_dir+'/plots') for f in files]
        test_files = [f for r,_,files in os.walk(self.test_path+'/plots') for f in files]
        self.assertEqual(sorted(res_files),sorted(test_files))

        #test values of all .nc files
        res_files = sorted([os.path.join(r,f) for r,_,files in os.walk(self.tmp_dir+'/output') for f in files])
        test_files = sorted([os.path.join(r,f) for r,_,files in os.walk(self.test_path+'/output') for f in files])
        for i,f in enumerate(res_files):
            
            t1 = FileHandler.openNetCDFFile(res_files[i])
            t2 = FileHandler.openNetCDFFile(test_files[i])

            np.testing.assert_array_almost_equal(t1['variable'], t2['variable'], 3)
            #elf.assertTrue((t1['variable']==t2['variable']).all())
            self.assertTrue((t1['lon']==t2['lon']).all())
            self.assertTrue((t1['lat']==t2['lat']).all())
Пример #2
0
    def testMsss(self):
        msssDict = self.msssDict()
        msss = Msss(**msssDict)
        msss.prepareInput()
        msss.analyze()
         
        #test file names
        res_files = [f for r,_,files in os.walk(self.tmp_dir+'/output') for f in files]
        test_files = [f for r,_,files in os.walk(self.test_path+'/output') for f in files]
        self.assertEqual(sorted(res_files),sorted(test_files))
        res_files = [f for r,_,files in os.walk(self.tmp_dir+'/plots') for f in files]
        test_files = [f for r,_,files in os.walk(self.test_path+'/plots') for f in files]
        self.assertEqual(sorted(res_files),sorted(test_files))
 
        #test values of all .nc files
        res_files = sorted([os.path.join(r,f) for r,_,files in os.walk(self.tmp_dir+'/output') for f in files])
        test_files = sorted([os.path.join(r,f) for r,_,files in os.walk(self.test_path+'/output') for f in files])
        #test_files = sorted([os.path.join(r,f) for r,_,files in os.walk(tets_p) for f in files])
#        print len(res_files)
#        print len(test_files)
        for i,f in enumerate(res_files):
            tmp_test = cdo.copy(input=test_files[i],options='-f nc') 
            t1 = FileHandler.openNetCDFFile(res_files[i])
            t2 = FileHandler.openNetCDFFile(tmp_test)#test_files[i])
#            print '_______________________________________-'
#            print res_files[i] 
#            print test_files[i]
	    np.testing.assert_array_almost_equal(t1['variable'], t2['variable'], 0)
            self.assertTrue((t1['lon']==t2['lon']).all())
            self.assertTrue((t1['lat']==t2['lat']).all())
Пример #3
0
    def testMsss(self):
        msssDict = self.msssDict()
        msss = Msss(**msssDict)
        msss.prepareInput()
        msss.analyze()

        #test file names
        res_files = [
            f for r, _, files in os.walk(self.tmp_dir + '/output')
            for f in files
        ]
        test_files = [
            f for r, _, files in os.walk(self.test_path + '/output')
            for f in files
        ]
        self.assertEqual(sorted(res_files), sorted(test_files))
        res_files = [
            f for r, _, files in os.walk(self.tmp_dir + '/plots')
            for f in files
        ]
        test_files = [
            f for r, _, files in os.walk(self.test_path + '/plots')
            for f in files
        ]
        self.assertEqual(sorted(res_files), sorted(test_files))

        #test values of all .nc files
        res_files = sorted([
            os.path.join(r, f)
            for r, _, files in os.walk(self.tmp_dir + '/output') for f in files
        ])
        test_files = sorted([
            os.path.join(r, f)
            for r, _, files in os.walk(self.test_path + '/output')
            for f in files
        ])
        #test_files = sorted([os.path.join(r,f) for r,_,files in os.walk(tets_p) for f in files])
        #        print len(res_files)
        #        print len(test_files)
        for i, f in enumerate(res_files):
            tmp_test = cdo.copy(input=test_files[i], options='-f nc')
            t1 = FileHandler.openNetCDFFile(res_files[i])
            t2 = FileHandler.openNetCDFFile(tmp_test)  #test_files[i])
            #            print '_______________________________________-'
            #            print res_files[i]
            #            print test_files[i]
            np.testing.assert_array_almost_equal(t1['variable'],
                                                 t2['variable'], 0)
            self.assertTrue((t1['lon'] == t2['lon']).all())
            self.assertTrue((t1['lat'] == t2['lat']).all())
Пример #4
0
 def testTestfiles(self):
     
     from create_testfiles import createMsssInput
     std1_goal=1. 
     std2_goal=2. 
     corr_goal=0.5
     std_ratio = std2_goal/std1_goal 
     cond_bias = corr_goal - std1_goal/std2_goal
     msss_goal = corr_goal**2 + cond_bias**2
     length = 50
     (hindcasts,observations) = createMsssInput(1960, std1_goal, std2_goal, corr_goal, length)
 
     msssDict = self.msssDict()
     msssDict['decadals'] = hindcasts.keys()
     msssDict['observation'] = 'hadcrut3v'
     msssDict['leadtimes'] = '1'
     msss = Msss(**msssDict)
     msss.input1Remapped = hindcasts
     msss.input2Remapped = hindcasts
     msss.observationRemapped = observations
     
     msss.analyze()
 
     goal_dict = {'msss_goal': '1-1/baseline1_output_mpi-esm-lr_decs4e_input1/accuracy/1_1_tas_baseline1_output_mpi-esm-lr_decs4e_1960-2009_msss.nc',
                  'corr_goal': '1-1/baseline1_output_mpi-esm-lr_decs4e_input1/accuracy/1_1_tas_baseline1_output_mpi-esm-lr_decs4e_1960-2009_correlation.nc',
                  'cond_bias': '1-1/baseline1_output_mpi-esm-lr_decs4e_input1/accuracy/1_1_tas_baseline1_output_mpi-esm-lr_decs4e_1960-2009_conditional_bias.nc',
                  'std_ratio': '1-1/baseline1_output_mpi-esm-lr_decs4e_input1/accuracy/1_1_tas_baseline1_output_mpi-esm-lr_decs4e_1960-2009_std_ratio.nc'}
     
     for key,val in goal_dict.iteritems():
         t1 = FileHandler.openNetCDFFile(msss.outputDir+val,mode='var')
         self.assertAlmostEqual(np.round(t1[12,12],2), locals()[key], 1)
Пример #5
0
    def testTestfiles(self):

        from create_testfiles import createMsssInput
        std1_goal = 1.
        std2_goal = 2.
        corr_goal = 0.5
        std_ratio = std2_goal / std1_goal
        cond_bias = corr_goal - std1_goal / std2_goal
        msss_goal = corr_goal**2 + cond_bias**2
        length = 50
        (hindcasts, observations) = createMsssInput(1960, std1_goal, std2_goal,
                                                    corr_goal, length)

        msssDict = self.msssDict()
        msssDict['decadals'] = hindcasts.keys()
        msssDict['observation'] = 'hadcrut3v'
        msssDict['leadtimes'] = '1'
        msss = Msss(**msssDict)
        msss.input1Remapped = hindcasts
        msss.input2Remapped = hindcasts
        msss.observationRemapped = observations

        msss.analyze()

        goal_dict = {
            'msss_goal':
            '1-1/baseline1_output_mpi-esm-lr_decs4e/msss/1_1_tas_baseline1_output_mpi-esm-lr_decs4e_1960-2009_msss.nc',
            'corr_goal':
            '1-1/baseline1_output_mpi-esm-lr_decs4e/msss/1_1_tas_baseline1_output_mpi-esm-lr_decs4e_1960-2009_correlation.nc',
            'cond_bias':
            '1-1/baseline1_output_mpi-esm-lr_decs4e/msss/1_1_tas_baseline1_output_mpi-esm-lr_decs4e_1960-2009_conditional_bias.nc',
            'std_ratio':
            '1-1/baseline1_output_mpi-esm-lr_decs4e/msss/1_1_tas_baseline1_output_mpi-esm-lr_decs4e_1960-2009_std_ratio.nc'
        }

        for key, val in goal_dict.iteritems():

            t1 = FileHandler.openNetCDFFile(msss.outputDir + val, mode='var')

            print key
            print locals()[key]
            print t1[12, 12]
            self.assertAlmostEqual(np.round(t1[12, 12], 2), locals()[key], 2)
Пример #6
0
    def testTestfiles(self):
        
        def crps(var,H,O):
            x = (H-O)/var
            crps = -var * (1./np.sqrt(np.pi) - 2. * stats.norm.pdf(x) - x * (2. * stats.norm.cdf(x) - 1.))
            return crps
        
        def condBias(H,O):  
            H_ensmean = np.mean(H, axis=1)
            r = np.corrcoef(H_ensmean, O)[0,1]
            std_H = np.std(H_ensmean)
            std_O = np.std(O)            
            cond_bias = r * std_O/std_H
            return cond_bias
            
        from create_testfiles import createCrpssInput, getCrpssTimeseries
        mse_goal = 4
        ensspread_goal = 3
        ensemble_size = 10
        length = 20    
        #create timeseries
        (t,hind,obs) = getCrpssTimeseries(mse_goal, ensspread_goal, ensemble_size, length)    
        
        (hindcasts,observations) = createCrpssInput(1960, hind, obs, ensemble_size, length)
        
        crpssDict = self.crpssDict()
        crpssDict['decadals'] = hindcasts.keys()
        crpssDict['observation'] = 'HadCrut'
        crpssDict['leadtimes'] = '1'
        crpss = Crpss(**crpssDict)
        crpss.inputRemapped = hindcasts
        crpss.observationRemapped = observations
        crpss.analyze()
        
        factor = length/(length-2.)
        factor2 = ensemble_size/(ensemble_size-1.)

        ensspread = np.mean((factor2*np.var(hind,axis=1)))**0.5        
        hind_mean = np.mean(hind,axis=1)
        
        #calculte crossvalidated mean
        hind_mean_anom = hind_mean.copy()
        crossval = np.zeros((length))
        for i in range(length):
            tmp = np.delete(hind_mean,i)
            crossval[i] =  np.mean(tmp)
            hind_mean_anom[i] = hind_mean[i] - crossval[i]
        hind_mean = hind_mean_anom.copy()
        
        #remove conditional bias
        bias = condBias(hind, obs)
        for i in range(length):
            hind[i,:] = hind[i,:] - crossval[i]
            hind[i,:] = hind[i,:] + (bias-1)*hind_mean[i]
        hind_mean_corrected = np.mean(hind,axis=1)
        RMSE = (factor*np.mean((np.mean(hind,axis=1)-obs)**2))**0.5
        
        #calculate crpss_goal
        crps1 = np.zeros((length))
        crps2 = np.zeros((length))
        for i in range(length):
            crps1[i] = crps(ensspread, hind_mean_corrected[i], obs[i])
            crps2[i] = crps(RMSE,hind_mean_corrected[i],obs[i]) 
        crpss_goal = 1 - np.mean(crps1)/np.mean(crps2)
        
        #test spreadscore
        tf = crpss.outputDir+'1-1/baseline1_output_mpi-esm-lr_decs4e_input1/ensemble_spread/1_1_tas_baseline1_output_mpi-esm-lr_decs4e_1960-1979_ensspread_vs_referror.nc'
        t1 = FileHandler.openNetCDFFile(tf,mode='var')
        self.assertAlmostEqual(t1[12,12],ensspread/RMSE,2)

        #test crpss ens vs ref
        tf = crpss.outputDir+'1-1/baseline1_output_mpi-esm-lr_decs4e_input1/ensemble_spread/1_1_tas_baseline1_output_mpi-esm-lr_decs4e_1960-1979_ens-vs-ref_crpss.nc'
        t1 = FileHandler.openNetCDFFile(tf,mode='var')
        self.assertAlmostEqual(t1[12,12],crpss_goal,2)