Beispiel #1
0
    def test_bool_vector(self):
        b = BoolVector()
        b.setDefault(True)

        b[4] = False

        self.assertEqual(list(b), [True, True, True, True, False])
Beispiel #2
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.createLocalObsdata("OBS")
            
            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)
Beispiel #3
0
    def test_analysis_module(self):
        rng = RandomNumberGenerator()
        module = self.createAnalysisModule()
        ens_size = 12
        obs_size = 1
        state_size = 2

        true_params = [1.25, 0.75]
        true_state = forward_model(true_params)
        obs = [(measure(true_state), 0.75)]
        A = Matrix(state_size, ens_size)

        ens = []
        for iens in range(ens_size):
            param = [random.gauss(1.00, 1.00), random.gauss(1.00, 1.00)]
            ens.append(param)

        mask = BoolVector(default_value=True, initial_size=ens_size)
        mask[2] = False
        (A, S, E, D, R, dObs) = init_matrices(ens, mask, obs, rng)

        module.initUpdate(mask, S, R, dObs, E, D)
        module.updateA(A, S, R, dObs, E, D)

        mask[10] = False
        mask[5] = False
        (A, S, E, D, R, dObs) = init_matrices(ens, mask, obs, rng)
        self.assertEqual(S.dims(), (obs_size, mask.countEqual(True)))
        self.assertEqual(E.dims(), (obs_size, mask.countEqual(True)))
        self.assertEqual(D.dims(), (obs_size, mask.countEqual(True)))

        module.initUpdate(mask, S, R, dObs, E, D)
        module.updateA(A, S, R, dObs, E, D)
Beispiel #4
0
    def test_analysis_module(self):
        rng = RandomNumberGenerator( )
        module = self.createAnalysisModule()
        ens_size = 12
        obs_size = 1
        state_size = 2

        true_params = [1.25 , 0.75]
        true_state  = forward_model( true_params )
        obs         = [(measure( true_state ) , 0.75)]
        A           = Matrix( state_size , ens_size )
        
        ens = []
        for iens in range(ens_size):
            param = [ random.gauss( 1.00 , 1.00 ) , random.gauss(1.00 , 1.00)]
            ens.append( param )
            
        mask = BoolVector(default_value = True , initial_size = ens_size)
        mask[2] = False
        (A , S , E , D , R , dObs) = init_matrices( ens , mask , obs , rng )

        module.initUpdate( mask , S , R , dObs , E , D )
        module.updateA( A , S , R , dObs , E , D )


        mask[10] = False
        mask[5] = False
        (A , S , E , D , R , dObs) = init_matrices( ens , mask , obs , rng )
        self.assertEqual( S.dims() , (obs_size , mask.countEqual( True )))
        self.assertEqual( E.dims() , (obs_size , mask.countEqual( True )))
        self.assertEqual( D.dims() , (obs_size , mask.countEqual( True )))
        
        module.initUpdate( mask , S , R , dObs , E , D )
        module.updateA( A , S , R , dObs , E , D )
Beispiel #5
0
    def test_bool_vector(self):
        b = BoolVector()
        b.setDefault(True)

        b[4] = False

        self.assertEqual(list(b), [True, True, True, True, False])
Beispiel #6
0
    def test_update_active_mask(self):
        vec = BoolVector(False, 10)

        self.assertTrue(BoolVector.updateActiveMask("1-2,5", vec))
        self.assertTrue(vec[1])
        self.assertTrue(vec[2])
        self.assertTrue(vec[5])
        self.assertFalse(vec[4])


        vec = BoolVector(False, 10)

        self.assertTrue(BoolVector.updateActiveMask("1-5,2,3", vec))
        self.assertTrue(vec[1])
        self.assertTrue(vec[2])
        self.assertTrue(vec[3])
        self.assertTrue(vec[4])
        self.assertTrue(vec[5])
        self.assertFalse(vec[0])
        self.assertFalse(vec[6])


        vec = BoolVector(False, 10)

        self.assertTrue(BoolVector.updateActiveMask("5,6,7,15", vec))
        self.assertTrue(vec[5])
        self.assertTrue(vec[6])
        self.assertTrue(vec[7])
        self.assertFalse(vec[4])
        self.assertFalse(vec[8])
        self.assertEqual(len(vec), 16)
Beispiel #7
0
    def load(self, fs, report_step, input_mask=None):
        """
         @type fs: EnkfFs
         @type report_step: int
         @type input_mask: BoolVector
         @rtype: PlotBlockData
        """

        state_map = fs.getStateMap()
        ensemble_size = len(state_map)

        if not input_mask is None:
            mask = BoolVector.copy(input_mask)
        else:
            mask = BoolVector(False, ensemble_size)

        state_map.selectMatching(mask, RealizationStateEnum.STATE_HAS_DATA)

        depth = self.getDepthValues(report_step)

        self.__permutation_vector = depth.permutationSort()
        depth.permute(self.__permutation_vector)

        plot_block_data = PlotBlockData(depth)

        thread_pool = ThreadPool()
        for index in range(ensemble_size):
            if mask[index]:
                thread_pool.addTask(self.loadVector, plot_block_data, fs, report_step, index)

        thread_pool.nonBlockingStart()
        thread_pool.join()

        return plot_block_data
    def createActiveList(self, fs):
        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)

        return active_list
Beispiel #9
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)
Beispiel #10
0
    def createActiveList(ert, fs):
        state_map = fs.getStateMap()
        ens_mask = BoolVector(False, ert.getEnsembleSize())
        state_map.selectMatching(
            ens_mask, RealizationStateEnum.STATE_INITIALIZED
            | RealizationStateEnum.STATE_HAS_DATA)
        active_list = BoolVector.createActiveList(ens_mask)

        return [iens for iens in active_list]
Beispiel #11
0
    def realizationList(self, state_value):
        """
        Will create a list of all realisations with state equal to state_value.

        @type state_value: RealizationStateEnum
        @rtype: ert.util.IntVector
        """
        mask = BoolVector(False, len(self))
        self.selectMatching(mask, state_value)
        return BoolVector.createActiveList(mask)
Beispiel #12
0
    def test_update_active_mask(self):
        vec = BoolVector(False, 10)

        self.assertTrue(vec.updateActiveMask("1-2,5"))
        self.assertTrue(vec[1])
        self.assertTrue(vec[2])
        self.assertTrue(vec[5])
        self.assertFalse(vec[4])

        vec = BoolVector(False, 10)

        self.assertTrue(vec.updateActiveMask("1-5,2,3"))
        self.assertTrue(vec[1])
        self.assertTrue(vec[2])
        self.assertTrue(vec[3])
        self.assertTrue(vec[4])
        self.assertTrue(vec[5])
        self.assertFalse(vec[0])
        self.assertFalse(vec[6])

        vec = BoolVector(False, 10)

        self.assertTrue(vec.updateActiveMask("5,6,7,15"))
        self.assertTrue(vec[5])
        self.assertTrue(vec[6])
        self.assertTrue(vec[7])
        self.assertFalse(vec[4])
        self.assertFalse(vec[8])
        self.assertEqual(len(vec), 16)
    def getActiveRealizationsMask(self):
        count = getRealizationCount()

        mask = BoolVector(default_value=False, initial_size = count)
        if not mask.updateActiveMask(self.getValue()):
            raise ValueError('Error while parsing range string "%s"!' % self.getValue())

        if len(mask) != count:
            raise ValueError("Mask size changed %d != %d!" % (count, len(mask)))

        return mask
Beispiel #14
0
    def test_activeMask(self):
        active_list = BoolVector.active_mask("1 , 4 - 7 , 10")
        self.assertTrue(len(active_list) == 11)
        self.assertTrue(active_list[1])
        self.assertTrue(active_list[4])
        self.assertTrue(active_list[10])
        self.assertFalse(active_list[9])
        self.assertFalse(active_list[8])

        active_list = BoolVector.active_mask("1,4-7,10X")
        self.assertFalse(active_list)
    def getActiveRealizationsMask(self):
        mask = BoolVector(False, self.__realization_count)
        mask.updateActiveMask(self.getValue(), mask)
        # mask = BoolVector.active_mask(self.getValue())

        if mask is None:
            raise ValueError("Error while parsing range string!")

        if len(mask) > self.__realization_count:
            raise ValueError("Mask size changed %d != %d!" % (self.__realization_count, len(mask)))

        return mask
Beispiel #16
0
    def getActiveRealizationsMask(self):
        mask = BoolVector(False, self.__realization_count)
        mask.updateActiveMask(self.getValue())
        # mask = BoolVector.active_mask(self.getValue())

        if mask is None:
            raise ValueError("Error while parsing range string!")

        if len(mask) > self.__realization_count:
            raise ValueError("Mask size changed %d != %d!" %
                             (self.__realization_count, len(mask)))

        return mask
Beispiel #17
0
    def getActiveRealizationsMask(self):
        count = getRealizationCount()

        mask = BoolVector(default_value=False, initial_size=count)
        if not mask.updateActiveMask(self.getValue()):
            raise ValueError('Error while parsing range string "%s"!' %
                             self.getValue())

        if len(mask) != count:
            raise ValueError("Mask size changed %d != %d!" %
                             (count, len(mask)))

        return mask
Beispiel #18
0
    def test_activeMask(self):
        active_list = BoolVector.createActiveMask("1 , 4 - 7 , 10")
        self.assertTrue(len(active_list) == 11)
        self.assertTrue(active_list[1])
        self.assertTrue(active_list[4])
        self.assertTrue(active_list[10])
        self.assertFalse(active_list[9])
        self.assertFalse(active_list[8])

        self.assertEqual(6, active_list.count(True))

        active_list = BoolVector.createActiveMask("1,4-7,10X")
        self.assertFalse(active_list)
Beispiel #19
0
    def test_activeMask(self):
        active_list = BoolVector.createActiveMask("1 , 4 - 7 , 10")
        self.assertTrue(len(active_list) == 11)
        self.assertTrue(active_list[1])
        self.assertTrue(active_list[4])
        self.assertTrue(active_list[10])
        self.assertFalse(active_list[9])
        self.assertFalse(active_list[8])

        self.assertEqual(6, active_list.count(True))

        active_list = BoolVector.createActiveMask("1,4-7,10X")
        self.assertFalse(active_list)
Beispiel #20
0
    def load(self, fs, report_step, input_mask=None):
        """
         @type fs: EnkfFs
         @type report_step: int
         @type input_mask: BoolVector
         @rtype: PlotBlockData
        """

        state_map = fs.getStateMap()
        ensemble_size = len(state_map)

        if not input_mask is None:
            mask = BoolVector.copy(input_mask)
        else:
            mask = BoolVector(False, ensemble_size)

        state_map.selectMatching(mask, RealizationStateEnum.STATE_HAS_DATA)

        depth = self.getDepthValues(report_step)

        self.__permutation_vector = depth.permutationSort()
        depth.permute(self.__permutation_vector)

        plot_block_data = PlotBlockData(depth)

        thread_pool = ThreadPool()
        for index in range(ensemble_size):
            if mask[index]:
                thread_pool.addTask(self.loadVector, plot_block_data, fs, report_step, index)

        thread_pool.nonBlockingStart()
        thread_pool.join()

        return plot_block_data
Beispiel #21
0
    def test_update(self):
        key = "OBS"
        obs_size = 4
        ens_size = 10
        ens_mask = BoolVector(default_value=True, initial_size=ens_size)
        block = MeasBlock(key, obs_size, ens_mask)

        with self.assertRaises(TypeError):
            block["String"] = 10

        with self.assertRaises(TypeError):
            block[10] = 10

        with self.assertRaises(IndexError):
            block[obs_size, 0] = 10

        with self.assertRaises(IndexError):
            block[0, ens_size] = 10

        #-----------------------------------------------------------------

        with self.assertRaises(TypeError):
            a = block["String"]

        with self.assertRaises(TypeError):
            a = block[10]

        with self.assertRaises(IndexError):
            val = block[obs_size, 0]

        with self.assertRaises(IndexError):
            val = block[0, ens_size]

        block[1, 2] = 3
        self.assertEqual(3, block[1, 2])
Beispiel #22
0
    def run(self, target_case_format, weights):
        if not "%d" in target_case_format:
            raise UserWarning("The target case format requires a %d. For example: default_%d")

        weights = self.parseWeights(weights)
        weights = self.normalizeWeights(weights)

        iteration_count = len(weights)

        print("Running MDA ES for %d iterations with the following normalized weights: %s" % (iteration_count, ", ".join(str(weight) for weight in weights)))

        source_fs = self.ert().getEnkfFsManager().getCurrentFileSystem()
        target_case_name = target_case_format % 0
        target_fs = self.ert().getEnkfFsManager().getFileSystem(target_case_name)

        if not source_fs == target_fs:
            self.ert().getEnkfFsManager().switchFileSystem(target_fs)
            self.ert().getEnkfFsManager().initializeCurrentCaseFromExisting(source_fs, 0, EnkfStateType.ANALYZED)

        active_realization_mask = BoolVector(True, self.ert().getEnsembleSize())

        for iteration, weight in enumerate(weights):
            self.simulateAndPostProcess(target_case_format, active_realization_mask, iteration)
            self.update(target_case_format, iteration, weights[iteration])

        self.simulateAndPostProcess(target_case_format, active_realization_mask, iteration_count)

        return "MDA ES completed successfully!"
Beispiel #23
0
    def test_run_context(self):
        with TestAreaContext("enkf_test") as work_area:
            work_area.copy_directory(self.case_directory)
            main = EnKFMain("simple_config/minimum_config")
            fs_manager = main.getEnkfFsManager()
            fs = fs_manager.getCurrentFileSystem()
            iactive = BoolVector(initial_size=10, default_value=True)
            iactive[0] = False
            iactive[1] = False
            run_context = main.getRunContextENSEMPLE_EXPERIMENT(fs, iactive)

            self.assertEqual(len(run_context), 8)

            with self.assertRaises(IndexError):
                run_context[8]

            with self.assertRaises(TypeError):
                run_context["String"]

            run_arg = run_context[0]
            self.assertTrue(isinstance(run_arg, RunArg))

            with self.assertRaises(ValueError):
                run_context.iensGet(0)

            with self.assertRaises(ValueError):
                run_context.iensGet(1)

            arg0 = run_context[0]
            arg2 = run_context.iensGet(2)
Beispiel #24
0
    def testObs(self):
        with ErtTestContext("obs_test", self.config_file) as test_context:
            ert = test_context.getErt()
            obs = ert.getObservations()

            self.assertEqual(32, len(obs))
            for v in obs:
                self.assertTrue(isinstance(v, ObsVector))

            with self.assertRaises(IndexError):
                v = obs[-1]

            with self.assertRaises(IndexError):
                v = obs[40]

            with self.assertRaises(KeyError):
                v = obs["No-this-does-not-exist"]

            v1 = obs["WWCT:OP_3"]
            v2 = obs["GOPT:OP"]
            mask = BoolVector(True, ert.getEnsembleSize())
            current_fs = ert.getEnkfFsManager().getCurrentFileSystem()

            self.assertTrue(v1.hasData(mask, current_fs))
            self.assertFalse(v2.hasData(mask, current_fs))

            local_node = v1.createLocalObs()
            for t in v1.getStepList():
                self.assertTrue(local_node.tstepActive(t))
Beispiel #25
0
    def createActiveList(ert, fs):
        state_map = fs.getStateMap()
        ens_mask = BoolVector(False, ert.getEnsembleSize())
        state_map.selectMatching(ens_mask, RealizationStateEnum.STATE_HAS_DATA)
        active_list = BoolVector.createActiveList(ens_mask)

        return [iens for iens in active_list]
    def test_load_results_manually(self):
        with ErtTestContext("manual_load_test",
                            self.config_file) as test_context:
            ert = test_context.getErt()
            load_into_case = "A1"
            load_from_case = "default"

            load_into = ert.getEnkfFsManager().getFileSystem(load_into_case)
            load_from = ert.getEnkfFsManager().getFileSystem(load_from_case)

            ert.getEnkfFsManager().switchFileSystem(load_from)
            realisations = BoolVector(default_value=True, initial_size=25)
            realisations[7] = False
            iteration = 0

            loaded = ert.loadFromForwardModel(realisations, iteration,
                                              load_into)

            load_into_case_state_map = load_into.getStateMap()

            load_into_states = [state for state in load_into_case_state_map]

            expected = [RealizationStateEnum.STATE_HAS_DATA] * 25
            expected[7] = RealizationStateEnum.STATE_UNDEFINED

            self.assertListEqual(load_into_states, expected)
            self.assertEqual(24, loaded)
            self.assertEqual(25, len(expected))
            self.assertEqual(25, len(realisations))
Beispiel #27
0
    def test_simulated_custom_kw(self):
        config = self.createTestPath("local/custom_kw/mini_config")
        with ErtTestContext("python/enkf/data/custom_kw_simulated",
                            config) as context:
            ert = context.getErt()

            ensemble_config = ert.ensembleConfig()
            self.assertTrue("AGGREGATED" in ensemble_config)

            config = ensemble_config.getNode(
                "AGGREGATED").getCustomKeywordModelConfig()

            self.assertEqual(len(config.getKeys()), 0)

            simulation_runner = EnkfSimulationRunner(ert)
            iteration_count = 0
            active = BoolVector(default_value=True,
                                initial_size=ert.getEnsembleSize())
            simulation_runner.createRunPath(active, iteration_count)
            simulation_runner.runEnsembleExperiment()

            config = ensemble_config.getNode(
                "AGGREGATED").getCustomKeywordModelConfig()

            self.assertEqual(len(config.getKeys()), 4)
            self.assertItemsEqual(
                config.getKeys(),
                ["PERLIN_1", "PERLIN_2", "PERLIN_3", "STATE"])
Beispiel #28
0
    def test_assert_symlink_deleted(self):
        with ErtTestContext(
                "create_runpath2",
                self.createTestPath(
                    "local/snake_oil_field/snake_oil.ert")) as tc:
            ert = tc.getErt()
            runpath_list = ert.getRunpathList()

            ens_size = ert.getEnsembleSize()
            runner = ert.getEnkfSimulationRunner()
            mask = BoolVector(initial_size=ens_size, default_value=True)

            # create directory structure
            runner.createRunPath(mask, 0)

            # replace field file with symlink
            linkpath = '%s/permx.grdcel' % str(runpath_list[0].runpath)
            targetpath = '%s/permx.grdcel.target' % str(
                runpath_list[0].runpath)
            open(targetpath, 'a').close()
            remove(linkpath)
            symlink(targetpath, linkpath)

            # recreate directory structure
            runner.createRunPath(mask, 0)

            # ensure field symlink is replaced by file
            self.assertFalse(path.islink(linkpath))
Beispiel #29
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
Beispiel #30
0
    def test_create(self):
        ens_size = 10
        ens_mask = BoolVector(default_value=True, initial_size=ens_size)
        data = MeasData(ens_mask)
        self.assertEqual(len(data), 0)
        self.assertTrue(isinstance(data, MeasData))

        block1 = data.addBlock("OBS1", 10, 5)
        block2 = data.addBlock("OBS2", 27, 10)

        with self.assertRaises(TypeError):
            data[1.782]

        with self.assertRaises(KeyError):
            data["NO-this-does-not-exist"]

        with self.assertRaises(IndexError):
            data[2]

        last0 = data[-1]
        last1 = data[1]
        self.assertEqual(last0, last1)

        self.assertTrue("OBS1-10" in data)
        self.assertTrue("OBS2-27" in data)
        self.assertEqual(len(data), 2)

        self.assertTrue(isinstance(block1, MeasBlock))
        self.assertTrue(isinstance(block2, MeasBlock))

        self.assertEqual(block1.getObsSize(), 5)
        self.assertEqual(block2.getObsSize(), 10)

        l = []
        for b in data:
            l.append(b)

        self.assertEqual(len(l), 2)
        self.assertEqual(l[0], block1)
        self.assertEqual(l[1], block2)

        with self.assertRaises(ValueError):
            S = data.createS()

        for iens in range(ens_size):
            block1[0, iens] = 5
            block2[0, iens] = 10
            block2[1, iens] = 15

        self.assertEqual(3, data.activeObsSize())
        S = data.createS()

        self.assertEqual(S.dims(), (3, ens_size))

        for iens in range(ens_size):
            self.assertEqual(S[0, iens], 5)
            self.assertEqual(S[1, iens], 10)
            self.assertEqual(S[2, iens], 15)
Beispiel #31
0
    def load(self, args):
        arguments = splitArguments(args)

        if len(arguments) < 1:
            self.lastCommandFailed("Loading requires a realization mask.")
            return False

        realization_count = self.ert().getEnsembleSize()

        mask = BoolVector(False, realization_count)
        mask_success = mask.updateActiveMask(arguments[0])

        if not mask_success:
            self.lastCommandFailed("The realization mask: '%s' is not valid." % arguments[0])
            return False

        fs = self.ert().getEnkfFsManager().getCurrentFileSystem()
        self.ert().loadFromForwardModel(mask, 0, fs)
Beispiel #32
0
    def filterObsKeys(self, obs_keys, fs):
        active_mask = BoolVector(True, self.ert().getEnsembleSize())
        ert_obs = self.ert().getObservations()

        result = []
        for obs_key in obs_keys:
            obsVector = ert_obs[obs_key]
            if obsVector.hasData(active_mask, fs):
                result.append(obs_key)
        return result
    def runEnsembleExperiment(self, active_realization_mask=None):
        """ @rtype: bool """
        if active_realization_mask is None:
            count = self.ert.getEnsembleSize()
            active_realization_mask = BoolVector(default_value=True,
                                                 initial_size=count)

        iter_nr = 0
        return self.runSimpleStep(active_realization_mask,
                                  EnkfInitModeEnum.INIT_CONDITIONAL, iter_nr)
Beispiel #34
0
    def realizationList(self , state_value):
        """
        Will create a list of all realisations with state equal to state_value.

        @type state_value: RealizationStateEnum
        @rtype: ert.util.IntVector
        """
        mask = BoolVector(False, len(self))
        self.selectMatching(mask, state_value)
        return BoolVector.createActiveList(mask)
Beispiel #35
0
    def load(self, args):
        arguments = splitArguments(args)

        if len(arguments) < 1:
            self.lastCommandFailed("Loading requires a realization mask.")
            return False

        realization_count = self.ert().getEnsembleSize()

        mask = BoolVector(False, realization_count)
        mask_success = mask.updateActiveMask(arguments[0])

        if not mask_success:
            self.lastCommandFailed("The realization mask: '%s' is not valid." %
                                   arguments[0])
            return False

        fs = self.ert().getEnkfFsManager().getCurrentFileSystem()
        self.ert().loadFromForwardModel(mask, 0, fs)
Beispiel #36
0
def initializeCurrentCaseFromExisting(source_case, target_case, source_report_step, parameters, members):
    if caseExists(source_case) and caseIsInitialized(source_case) and caseExists(target_case):
        total_member_count = getRealizationCount()

        member_mask = BoolVector.createFromList(total_member_count, members)
        selected_parameters = StringList(parameters)

        ERT.ert.getEnkfFsManager().customInitializeCurrentFromExistingCase(source_case, source_report_step, member_mask,
                                                                           selected_parameters)

        ERT.emitErtChange()
    def getActiveRealizationsMask(self):
        count = EnsembleSizeModel().getValue()

        mask = BoolVector.active_mask(self.getValue())

        if mask is None:
            raise ValueError("Error while parsing range string!")

        if len(mask) > count:
            raise ValueError("Mask size changed %d != %d!" % (count, len(mask)))

        return mask
Beispiel #38
0
    def getActiveRealizationsMask(self):
        count = getRealizationCount()

        mask = BoolVector.createActiveMask(self.getValue())

        if mask is None:
            raise ValueError("Error while parsing range string!")

        if len(mask) > count:
            raise ValueError("Mask size changed %d != %d!" % (count, len(mask)))

        return mask
Beispiel #39
0
    def test_inactive(self):
        key = "OBS"
        obs_size = 2
        ens_size = 10
        ens_mask = BoolVector(default_value=True, initial_size=ens_size)
        ens_mask[5] = False
        block = MeasBlock(key, obs_size, ens_mask)

        self.assertFalse(block.iensActive(5))

        with self.assertRaises(ValueError):
            block[0, 5] = 10
Beispiel #40
0
    def buttonTriggered(self):
        source_case = InitializedCaseSelectorModel().getCurrentChoice()
        source_report_step = HistoryLengthModel().getSpinnerValue()
        selected_members = InitializationMembersModel().getSelectedItems()
        total_member_count = EnsembleSizeModel().getSpinnerValue()

        member_mask = BoolVector.createFromList(total_member_count, selected_members)
        selected_parameters = StringList((InitializationParametersModel()).getSelectedItems())

        self.ert().getEnkfFsManager().customInitializeCurrentFromExistingCase(source_case, source_report_step, member_mask, selected_parameters)

        self.observable().notify(ButtonModelMixin.BUTTON_TRIGGERED_EVENT)
Beispiel #41
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:
            state = EnkfStateType.FORECAST
            meas_data = MeasData(ens_mask)
            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()
                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
    def getActiveRealizationsMask(self):
        count = EnsembleSizeModel().getValue()

        mask = BoolVector.createActiveMask(self.getValue())

        if mask is None:
            raise ValueError("Error while parsing range string!")

        if len(mask) > count:
            raise ValueError("Mask size changed %d != %d!" %
                             (count, len(mask)))

        return mask
Beispiel #43
0
    def test_assert_export(self):
        with ErtTestContext("create_runpath1" , self.createTestPath("local/snake_oil_no_data/snake_oil.ert")) as tc:
            ert = tc.getErt( )
            runpath_list = ert.getRunpathList( )
            self.assertFalse( path.isfile( runpath_list.getExportFile( ) ))

            ens_size = ert.getEnsembleSize( )
            runner = ert.getEnkfSimulationRunner( )
            mask = BoolVector( initial_size = ens_size , default_value = True )
            runner.createRunPath( mask , 0 )

            self.assertTrue( path.isfile( runpath_list.getExportFile( ) ))
            self.assertEqual( "test_runpath_list.txt" , path.basename( runpath_list.getExportFile( ) ))
Beispiel #44
0
def initializeCurrentCaseFromExisting(source_case, target_case,
                                      source_report_step, parameters, members):
    if caseExists(source_case) and caseIsInitialized(
            source_case) and caseExists(target_case):
        total_member_count = getRealizationCount()

        member_mask = BoolVector.createFromList(total_member_count, members)
        selected_parameters = StringList(parameters)

        ERT.ert.getEnkfFsManager().customInitializeCurrentFromExistingCase(
            source_case, source_report_step, member_mask, selected_parameters)

        ERT.emitErtChange()
Beispiel #45
0
    def test_create(self):
        key = "OBS"
        ens_size = 100
        obs_size = 77
        ens_mask = BoolVector(default_value=True, initial_size=ens_size)

        ens_mask[67] = False
        block = MeasBlock(key, obs_size, ens_mask)
        self.assertEqual(block.getObsSize(), obs_size)
        self.assertEqual(block.getActiveEnsSize(), ens_size - 1)
        self.assertEqual(block.getTotalEnsSize(), ens_size)

        self.assertTrue(block.iensActive(66))
        self.assertFalse(block.iensActive(67))
Beispiel #46
0
    def __init__(self, ert_handle, size, run_fs, run_count):
        self.ert_handle = ert_handle
        self.size = size
        self.runner = ert_handle.getEnkfSimulationRunner()

        site_config = self.ert_handle.siteConfig()
        self.queue_manager = JobQueueManager(site_config.getJobQueue())
        self.queue_manager.startQueue(size, verbose=True)

        mask = BoolVector(default_value=True)
        mask[size - 1] = True

        self.ert_handle.addDataKW("<ELCO_RUN_COUNT>", "%s" % run_count)
        self.ert_run_context = self.ert_handle.getRunContextENSEMPLE_EXPERIMENT(
            run_fs, mask)
    def buttonTriggered(self):
        source_case = InitializedCaseSelectorModel().getCurrentChoice()
        source_report_step = HistoryLengthModel().getSpinnerValue()
        source_state = EnkfStateType.ANALYZED
        selected_members = InitializationMembersModel().getSelectedItems()
        total_member_count = EnsembleSizeModel().getSpinnerValue()

        member_mask = BoolVector.createFromList(total_member_count, selected_members)
        ranking_key = None
        selected_parameters = StringList((InitializationParametersModel()).getSelectedItems())

        # print("%s %d %d %s %s" % (source_case, source_report_step, int(source_state), str(selected_members), str(selected_parameters)))

        self.ert().initializeFromExistingCase(source_case, source_report_step, source_state, member_mask, ranking_key, selected_parameters)

        self.observable().notify(ButtonModelMixin.BUTTON_TRIGGERED_EVENT)
    def load_active_masks(self, case1, case2 ):
        with ErtTestContext("gen_data_config_test", self.config_file) as test_context:
            ert = test_context.getErt()

            fs1 =  ert.getEnkfFsManager().getFileSystem(case1)
            config_node = ert.ensembleConfig().getNode("TIMESHIFT")
            data_node = EnkfNode(config_node)
            data_node.tryLoad(fs1, NodeId(60, 0, EnkfStateType.FORECAST))

            active_mask = get_active_mask( config_node.getDataModelConfig() )
            first_active_mask_length = len(active_mask)
            self.assertEqual(first_active_mask_length, 2560)

            fs2 =  ert.getEnkfFsManager().getFileSystem(case2)
            data_node = EnkfNode(config_node)
            data_node.tryLoad(fs2, NodeId(60, 0, EnkfStateType.FORECAST))

            active_mask = get_active_mask( config_node.getDataModelConfig() )
            second_active_mask_len = len(active_mask)
            self.assertEqual(second_active_mask_len, 2560)
            self.assertEqual(first_active_mask_length, second_active_mask_len)

            # Setting one element to False, load different case, check, reload, and check.
            self.assertTrue(BoolVector.cNamespace().iget(active_mask, 10))
            active_mask_modified = active_mask.copy()
            active_mask_modified[10] = False

            # Must switch filesystem, because the update mask (writes to storage)
            # functionality uses the current filesystem (current case)
            ert.getEnkfFsManager().switchFileSystem(fs2)
            update_active_mask(config_node.getDataModelConfig(),  60, active_mask_modified)
            active_mask = get_active_mask( config_node.getDataModelConfig() )
            self.assertFalse(active_mask[10])

            #Load first - check element is true
            data_node = EnkfNode(config_node)
            data_node.tryLoad(fs1, NodeId(60, 0, EnkfStateType.FORECAST))
            active_mask = get_active_mask( config_node.getDataModelConfig() )
            self.assertTrue(active_mask[10])

            # Reload second again, should now be false at 10, due to the update further up
            data_node = EnkfNode(config_node)
            data_node.tryLoad(fs2, NodeId(60, 0, EnkfStateType.FORECAST))
            active_mask = get_active_mask( config_node.getDataModelConfig() )
            self.assertFalse(active_mask[10])
Beispiel #49
0
 def test_BoolVector_active_mask(self):
     with warnings.catch_warnings():
         active_vector = BoolVector.active_mask("1,1,1,1,1,1")
Beispiel #50
0
 def runEnsembleExperiment(self, member_list, total_member_count):
     member_mask = BoolVector.createFromList(total_member_count, member_list)
     EnKFMain.cNamespace().run_exp(self, member_mask, True, 0, 0, EnkfStateType.ANALYZED, True)
Beispiel #51
0
 def test_BoolVector_active_mask(self):
     with self.assertRaises(DeprecationWarning):
         active_vector = BoolVector.active_mask("1,1,1,1,1,1")