Exemplo n.º 1
0
    def test_it(self):
        state_size = 10
        with ErtTestContext("update", self.config_file) as tc:
            analysis = self.createAnalysisModule()
            ert = tc.getErt()
            obs = ert.getObservations()
            local_obsdata = obs.getAllActiveLocalObsdata()

            fs = ert.getEnkfFsManager().getCurrentFileSystem()
            state = EnkfStateType.FORECAST

            mask = BoolVector(initial_size=ert.getEnsembleSize(),
                              default_value=True)
            meas_data = MeasData(mask)
            obs_data = ObsData()
            obs.getObservationAndMeasureData(fs, local_obsdata, state,
                                             mask.createActiveList(),
                                             meas_data, obs_data)
            update(self.rng, mask, analysis, ert, meas_data, obs_data,
                   state_size)

            mask[0] = False
            mask[4] = False
            meas_data = MeasData(mask)
            obs_data = ObsData()
            obs.getObservationAndMeasureData(fs, local_obsdata, state,
                                             mask.createActiveList(),
                                             meas_data, obs_data)
            update(self.rng, mask, analysis, ert, meas_data, obs_data,
                   state_size)
Exemplo n.º 2
0
def init_matrices(ens, mask, obs, rng):
    state_size = 2
    report_step = 5
    meas_data = MeasData(mask)
    meas_block = meas_data.addBlock("OBS", report_step, len(obs))

    A = Matrix(state_size, mask.countEqual(True))
    active_iens = 0
    for iens, params in enumerate(ens):
        if mask[iens]:
            state = forward_model(params)
            meas_block[0, iens] = measure(state)

            A[0, active_iens] = params[0]
            A[1, active_iens] = params[1]

            active_iens += 1

    S = meas_data.createS()

    obs_data = ObsData()
    obs_block = obs_data.addBlock("OBS", 1)
    for iobs, obs_value in enumerate(obs):
        obs_block[iobs] = obs_value

    R = obs_data.createR()
    dObs = obs_data.createDObs()
    E = obs_data.createE(rng, meas_data.getActiveEnsSize())
    D = obs_data.createD(E, S)

    obs_data.scale(S, E=E, D=D, R=R, D_obs=dObs)
    return (A, S, E, D, R, dObs)
Exemplo n.º 3
0
def init_matrices(ens , mask , obs , rng):
    state_size = 2
    report_step = 5
    meas_data = MeasData( mask )
    meas_block = meas_data.addBlock("OBS" , report_step , len(obs) )

    A = Matrix( state_size , mask.countEqual( True ))
    active_iens = 0
    for iens,params in enumerate( ens ):
        if mask[iens]:
            state = forward_model( params )
            meas_block[0,iens] = measure( state )
            
            A[0 , active_iens] = params[0]
            A[1 , active_iens] = params[1]
            
            active_iens += 1

            
    S = meas_data.createS()
        
    obs_data = ObsData()
    obs_block = obs_data.addBlock("OBS" , 1)
    for iobs,obs_value in enumerate(obs):
        obs_block[iobs] = obs_value
    

    R = obs_data.createR()
    dObs = obs_data.createDObs()
    E = obs_data.createE( rng , meas_data.getActiveEnsSize() )
    D = obs_data.createD(E , S)
        
    obs_data.scale(S , E = E , D = D , R = R , D_obs = dObs)
    return (A , S , E , D , R , dObs)
Exemplo n.º 4
0
    def calculatePrincipalComponent(self,
                                    fs,
                                    local_obsdata,
                                    truncation_or_ncomp=3):
        pc = Matrix(1, 1)
        pc_obs = Matrix(1, 1)
        singular_values = DoubleVector()

        state_map = fs.getStateMap()
        ens_mask = BoolVector(False, self.ert().getEnsembleSize())
        state_map.selectMatching(ens_mask, RealizationStateEnum.STATE_HAS_DATA)
        active_list = ens_mask.createActiveList()

        if len(ens_mask) > 0:
            meas_data = MeasData(ens_mask)
            obs_data = ObsData()

            self.ert().getObservations().getObservationAndMeasureData(
                fs, local_obsdata, active_list, meas_data, obs_data)

            meas_data.deactivateZeroStdSamples(obs_data)

            active_size = len(obs_data)

            if active_size > 0:
                S = meas_data.createS()
                D_obs = obs_data.createDObs()

                truncation, ncomp = self.truncationOrNumberOfComponents(
                    truncation_or_ncomp)

                obs_data.scale(S, D_obs=D_obs)
                EnkfLinalg.calculatePrincipalComponents(
                    S, D_obs, truncation, ncomp, pc, pc_obs, singular_values)
                if self.__prior_singular_values is None:
                    self.__prior_singular_values = singular_values
                else:
                    for row in range(pc.rows()):
                        factor = singular_values[
                            row] / self.__prior_singular_values[row]
                        pc.scaleRow(row, factor)
                        pc_obs.scaleRow(row, factor)

                return PcaPlotData(local_obsdata.getName(), pc, pc_obs,
                                   singular_values)
        return None
Exemplo n.º 5
0
    def test_create(self):
        obs_data = ObsData()
        obs_size = 10
        block = obs_data.addBlock("OBS", obs_size)
        self.assertTrue(isinstance(block, ObsBlock))

        block[0] = (10, 10)
        block[1] = (12, 12)
        D = obs_data.createDObs()
        self.assertTrue(isinstance(D, Matrix))
        self.assertEqual(D.dims(), (2, 1))

        self.assertEqual(D[0, 0], 10)
        self.assertEqual(D[1, 0], 12)

        obs_data.scaleMatrix(D)
        self.assertEqual(D[0, 0], 1)
        self.assertEqual(D[1, 0], 1)

        R = obs_data.createR()
        self.assertEqual((2, 2), R.dims())

        with self.assertRaises(IndexError):
            obs_data[10]

        v, s = obs_data[0]
        self.assertEqual(v, 10)
        self.assertEqual(s, 10)

        v, s = obs_data[1]
        self.assertEqual(v, 12)
        self.assertEqual(s, 12)
Exemplo n.º 6
0
    def calculatePrincipalComponent(self, fs, local_obsdata, truncation_or_ncomp=3):
        pc = Matrix(1, 1)
        pc_obs = Matrix(1, 1)
        singular_values = DoubleVector()

        state_map = fs.getStateMap()
        ens_mask = BoolVector(False, self.ert().getEnsembleSize())
        state_map.selectMatching(ens_mask, RealizationStateEnum.STATE_HAS_DATA)
        active_list = BoolVector.createActiveList(ens_mask)

        if len(active_list) > 0:
            state = EnkfStateType.FORECAST
            meas_data = MeasData(active_list)
            obs_data = ObsData()

            self.ert().getObservations().getObservationAndMeasureData(fs, local_obsdata, state, active_list, meas_data, obs_data)

            meas_data.deactivateZeroStdSamples(obs_data)

            active_size = len(obs_data)

            if active_size > 0:
                S = meas_data.createS(active_size)
                D_obs = obs_data.createDobs(active_size)

                truncation, ncomp = self.truncationOrNumberOfComponents(truncation_or_ncomp)

                obs_data.scale(S, D_obs=D_obs)
                EnkfLinalg.calculatePrincipalComponents(S, D_obs, truncation, ncomp, pc, pc_obs, singular_values)
                if self.__prior_singular_values is None:
                    self.__prior_singular_values = singular_values
                else:
                    for row in range(pc.rows()):
                        factor = singular_values[row]/self.__prior_singular_values[row]
                        pc.scaleRow( row , factor )
                        pc_obs.scaleRow( row , factor )


                return PcaPlotData(local_obsdata.getName(), pc , pc_obs , singular_values)
        return None
Exemplo n.º 7
0
    def test_create(self):
        obs_data = ObsData()
        obs_size = 10
        block = obs_data.addBlock("OBS" , obs_size)
        self.assertTrue( isinstance( block , ObsBlock ))

        block[0] = (10,10)
        block[1] = (12,12)
        D = obs_data.createDObs()
        self.assertTrue( isinstance(D , Matrix ))
        self.assertEqual( D.dims() , (2,1))
        
        self.assertEqual( D[0,0] , 10 )
        self.assertEqual( D[1,0] , 12 )
        
        obs_data.scaleMatrix( D )
        self.assertEqual( D[0,0] , 1 )
        self.assertEqual( D[1,0] , 1 )
        
        R = obs_data.createR()
        self.assertEqual( (2,2) , R.dims() )

        with self.assertRaises(IndexError):
            obs_data[10]

        v,s = obs_data[0]
        self.assertEqual( v , 10 )
        self.assertEqual( s , 10 )


        v,s = obs_data[1]
        self.assertEqual( v , 12 )
        self.assertEqual( s , 12 )
Exemplo n.º 8
0
    def test_scale_obs(self):
        with ErtTestContext("obs_test", self.config_file) as test_context:
            ert = test_context.getErt()
            obs = ert.getObservations()

            obs1 = obs["WWCT:OP_1"].getNode(50)
            obs2 = obs["WWCT:OP_1_50"].getNode(50)

            self.assertEqual(obs1.getStandardDeviation(),
                             obs2.getStandardDeviation())
            std0 = obs1.getStandardDeviation()

            local_obsdata = LocalObsdata("obs", obs)
            node1 = local_obsdata.addNode("WWCT:OP_1")
            node2 = local_obsdata.addNode("WWCT:OP_1_50")
            node1.addTimeStep(50)
            node2.addTimeStep(50)

            mask = BoolVector(default_value=True)
            mask[2] = True
            meas_data = MeasData(mask)
            obs_data = ObsData()
            fs = ert.getEnkfFsManager().getCurrentFileSystem()
            active_list = IntVector()
            active_list.initRange(0, 2, 1)
            obs.getObservationAndMeasureData(fs, local_obsdata,
                                             EnkfStateType.FORECAST,
                                             active_list, meas_data, obs_data)
            self.assertEqual(2, len(obs_data))

            v1 = obs_data[0]
            v2 = obs_data[1]

            self.assertEqual(v1[1], std0)
            self.assertEqual(v2[1], std0)

            meas_data = MeasData(mask)
            obs_data = ObsData(10)
            obs.getObservationAndMeasureData(fs, local_obsdata,
                                             EnkfStateType.FORECAST,
                                             active_list, meas_data, obs_data)
            self.assertEqual(2, len(obs_data))

            v1 = obs_data[0]
            v2 = obs_data[1]

            self.assertEqual(v1[1], std0 * 10)
            self.assertEqual(v2[1], std0 * 10)

            actl = ActiveList()
            obs1.updateStdScaling(10, actl)
            obs2.updateStdScaling(20, actl)
            meas_data = MeasData(mask)
            obs_data = ObsData()
            obs.getObservationAndMeasureData(fs, local_obsdata,
                                             EnkfStateType.FORECAST,
                                             active_list, meas_data, obs_data)
            self.assertEqual(2, len(obs_data))

            v1 = obs_data[0]
            v2 = obs_data[1]

            self.assertEqual(v1[1], std0 * 10)
            self.assertEqual(v2[1], std0 * 20)