Beispiel #1
0
    def __init__(self, history, parameters, basename="out"):
        """Creates an experiment class for uncertainty analysis methods for kinematic models
        **Arguments**:
         - *history* = The .his file this experiment is based on
         - *parameters* = A string pointing to a csv file defining the statistical
                          properties of the model properties being varied, or alternatively an array 
                          of python dictionaries with the same function. This file/dictionary array
                          should have collumns/keys defining:
                              1) the event and parameter being varied (titled 'event' and 'parameter')
                              2) the statistical distribution to sample from (titled 'type' and containing either 'normal',
                                 'vonmises' or 'uniform')
                              3) the distribution mean (titled 'mean') and,
                              4) a collumn defining the distance between the 2.5th and 97.5th percentiles 
                                 (titled '+-') OR one defining the standard deviation (titled 'stdev')
        """

        # init monte carlo class
        MonteCarlo.__init__(self, history, parameters, basename)

        # add empty block (otherwise something breaks...)
        self.block = None
Beispiel #2
0
    def estimate_uncertainty(self, n_trials, **kwds):
        """
        Samples the specified number of models, given the pdf's defined in the params file used to create this model.

        **Arguments**:
         - *n_trials* = The number of random draws to produce. The variation between these random draws
                        is used to estimate uncertainty.
        **Optional Keywords**:
         - *verbose* = If true, this funciton prints information to the print buffer. Default is True.
         - *model_path* = The directory to write models to. Default is a local directory called 'tmp'.
         - *cleanup* = True if this function should delete any models it creates (they're not needed anymore). Default
                       is True.
        """
        vb = kwds.get('verbose', False)
        model_path = kwds.get('model_path', 'tmp')
        cleanup = kwds.get('cleanup', True)

        # generate & load initial model
        self.write_history('tmp.his')
        pynoddy.compute_model('tmp.his', self.basename)
        self.load_model_info()
        self.load_geology()
        os.remove('tmp.his')

        # perform monte carlo sampling
        if vb:
            print "Producing model realisations..."
        self.generate_model_instances(model_path, n_trials, verbose=vb, write_changes=None)

        # thought: it would be more efficient (memory wise) to load models 1 at a time rather than
        # dumping them all in memory....

        # load results
        if vb:
            print "Loading models..."

        models = MonteCarlo.load_noddy_realisations(model_path, verbose=vb)
        self.models = models

        # compute strat column
        # self.determine_model_stratigraphy()
        # self.n_rocktypes = len(self.model_stratigraphy)

        # self.nx = models[0].nx
        # self.ny = models[0].ny
        # self.nz = models[0].nz

        # calculate probabilities for each lithology. p_block[lithology][x][y][z] = p(lithology | x, y ,z)
        self.p_block = [[[[0. for z in range(self.nz)] for y in range(self.ny)] for x in range(self.nx)] for l in
                        range(self.n_rocktypes)]
        p1 = 1 / float(n_trials)  # probability increment gained on each observation
        for m in models:
            # loop through voxels
            for x in range(self.nx):
                for y in range(self.ny):
                    for z in range(self.nz):
                        # get litho
                        litho = int(m.block[x][y][z]) - 1

                        # update litho probability
                        self.p_block[litho][x][y][z] += p1

        # calculate entropy & store in self.e_block
        self.e_block = np.ndarray((self.nx, self.ny, self.nz))
        for x in range(self.nx):
            for y in range(self.ny):
                for z in range(self.nz):
                    entropy = 0  # calculate shannons information entropy
                    for litho in range(self.n_rocktypes):
                        p = self.p_block[litho][x][y][z]

                        # fix domain to 0 < p < 1
                        if p == 0:
                            p = 0.0000000000000001
                        if p >= 0.9999999999999999:
                            p = 0.9999999999999999

                        # calculate
                        entropy += p * math.log(p, 2) + (1 - p) * (math.log(1 - p, 2))

                    entropy = entropy * -1 / float(self.n_rocktypes)  # divide by n
                    self.e_block[x][y][z] = entropy

        # cleanup
        if vb:
            print "Cleaning up..."
        if cleanup:
            self.cleanup()
        if vb:
            print "Finished."
Beispiel #3
0
    
    #generate random purturbation
    ex1.random_draw()
    
except Exception as e:
    sys.stderr.write("Error - could not randomly purturb the Experiment... %s\n" % e)
    err = True
    
if not err:
    print("Succesfully used Experiment class")

###########################
#Test MonteCarlo class
###########################
try:
    mc = MonteCarlo(history_path,params)
    mc.generate_model_instances("out",4,threads=4,verbose=False,write_changes=None)
    mc.cleanup() #delete files
except Exception as e:
    sys.stderr.write("Error - MonteCarlo class is not functioning... %s\n" % e)
    err = True

if not err:
    print("Succesfully used MonteCarlo class")
    
#cleanup
os.remove(history_path)
import glob
for filename in glob.glob("%s*" % output_name):
    os.remove(filename) 
os.remove('noddyBatchProgress.txt')
Beispiel #4
0
    #generate random purturbation
    ex1.random_draw()

except Exception as e:
    sys.stderr.write(
        "Error - could not randomly purturb the Experiment... %s\n" % e)
    err = True

if not err:
    print "Succesfully used Experiment class"

###########################
#Test MonteCarlo class
###########################
try:
    mc = MonteCarlo(history_path, params)
    mc.generate_model_instances("out",
                                4,
                                threads=4,
                                verbose=False,
                                write_changes=None)
    mc.cleanup()  #delete files
except Exception as e:
    sys.stderr.write("Error - MonteCarlo class is not functioning... %s\n" % e)
    err = True

if not err:
    print "Succesfully used MonteCarlo class"

#cleanup
os.remove(history_path)