示例#1
0
def build_geomodel(var_type, interpolated_variables, codes, grid, tg_grid_name,
                   tg_prop_name, ids, acceptance, rt_prop, num_models):
    print('Creating a geologic model...')

    int_variables = np.array(interpolated_variables).T
    geomodel = []
    idx = 0
    for i in int_variables:
        if np.isnan(i).all():
            geomodel.append(float('nan'))
            idx = idx + 1
        else:
            index = i.argmin(axis=0)
            geomodel.append(float(codes[index]))
            idx = idx + 1
    cm = confusion_matrix(rt_prop, np.array(geomodel)[ids], normalize='true')
    diag = np.diagonal(cm)
    print(diag)
    m = np.mean(diag)
    #if m < acceptance:
    if np.any(diag < acceptance):
        print('Model rejected!')
        pass
    else:
        sgems.set_property(tg_grid_name, tg_prop_name, geomodel)
        num_models.append('x')

    return geomodel
示例#2
0
    def execute(self):
	print self.params
	# ESTABELECA OS PARAMETROS INICIAIS
	propertie = self.params['prop']['property']
	grid = self.params['prop']['grid']
	
	measured = float(self.params['Measured']['value'])
	Indicated = float(self.params['Indicated']['value'])
	Inferred = float(self.params['Inferred']['value'])
	dx = float(self.params['dx']['value'])
	dy = float(self.params['dy']['value'])
	dz = float(self.params['dz']['value'])
	
	grid = self.params['prop']['grid'] 
	x = sgems.get_property(grid, "_X_")
	y = sgems.get_property(grid, "_Y_")
	z = sgems.get_property(grid, "_Z_")
	distance = []
	
	

	numx = int(round((max(x) - min(x))/dx,0)) + 1
	numy = int(round((max(y) - min(y))/dy,0)) + 1
	numz = int(round((max(z) - min(z))/dz,0)) + 1

		

	cmd="NewCartesianGrid rescat::"+str(numx)+"::"+str(numy)+"::"+str(numz)+"::"+str(dx)+"::"+str(dy)+"::"+str(dz)+"::"+str(min(x))+"::"+str(min(y))+"::"+str(min(z))+"::0"
        print cmd
	sgems.execute(cmd)

	xh = sgems.get_property("rescat","_X_")
	yh = sgems.get_property("rescat","_Y_")
	zh = sgems.get_property("rescat","_Z_")	

	

	
	v = []
	for i, j, k in zip(xh,yh,zh):
		valores = 0
		menor_valor = 1000
		for xind, yind, zind in zip(x,y,z):
			distance = (math.sqrt((xind-i)**2+(yind-j)**2+(zind-k)**2))
			if (distance < measured):
				valores = 0
			elif( distance < Indicated and distance > measured):
				valores = 1
			elif(distance < Inferred and distance > Indicated):
				valores = 2
			else:
				valores = 3
			if (valores < menor_valor):
				menor_valor = valores		
		v.append(menor_valor)
	print (v)
	sgems.set_property("rescat","R",v)
	
	
    	return True 
示例#3
0
def build_geomodel(var_type, interpolated_variables, codes, grid, tg_grid_name,
                   tg_prop_name):
    print('Creating a geologic model...')

    if var_type == 'Indicators':

        if len(interpolated_variables) == 1:
            nn_results = helpers.nn(x, y, z, variables[0], grid)
            if keep_variables == '1':
                prop_name = 'nn_' + str(codes[0])
                sgems.set_property(tg_grid_name, prop_name,
                                   nn_results.tolist())
            proportion = sum(nn_results == 1) / len(nn_results)
            print('Cutting-off interpolated indicator property in {}'.format(
                proportion.round(2)))
            q = np.quantile(interpolated_variables[0], (1 - proportion))
            solid = np.where(interpolated_variables[0] > q, 1, 0)
            sgems.set_property(tg_grid_name, 'rt_{}'.format(codes[0]),
                               solid.tolist())

        else:
            int_variables = np.array(interpolated_variables).T
            geomodel = []
            idx = 0
            for i in int_variables:
                if np.isnan(i).all():
                    geomodel.append(float('nan'))
                    idx = idx + 1
                else:
                    index = i.argmax(axis=0)
                    geomodel.append(float(codes[index]))
                    idx = idx + 1
            sgems.set_property(tg_grid_name, tg_prop_name, geomodel)
            print('Geologic model created!')

    else:

        if len(interpolated_variables) == 1:
            solid = np.where(interpolated_variables[0] < 0, 1, 0)
            sgems.set_property(tg_grid_name, 'rt_{}'.format(codes[0]),
                               solid.tolist())

        else:
            int_variables = np.array(interpolated_variables).T
            geomodel = []
            idx = 0
            for i in int_variables:
                if np.isnan(i).all():
                    geomodel.append(float('nan'))
                    idx = idx + 1
                else:
                    index = i.argmin(axis=0)
                    geomodel.append(float(codes[index]))
                    idx = idx + 1
            sgems.set_property(tg_grid_name, tg_prop_name, geomodel)
            print('Geologic model created!')

    return geomodel
示例#4
0
    def execute(self):
      
        #aqui vai o codigo
        #getting variables
        grid_name = self.params['rt_prop']['grid']
        prop_name = self.params['rt_prop']['property']
        prop_values = sgems.get_property(grid_name, prop_name)
        prop_values = np.array(prop_values)
        point_transform_type = self.params['comboBox']['value']

        if point_transform_type == 'Indicators':

            #calculating indicators
            nan_filter = np.isfinite(prop_values)
            unique_rts = np.unique(prop_values[nan_filter])

            for rt in unique_rts:
                print('calculating indicators for rock type {}'.format(int(rt)))
                ind_prop = prop_values == rt
                ind_prop = np.where(ind_prop == True, 1., 0.)
                ind_prop[~nan_filter] = float('nan')
                indprop_name = 'indicators_rt_{}'.format(int(rt))
                sgems.set_property(grid_name, indprop_name, ind_prop.tolist())

        else:

            x, y, z = np.array(sgems.get_X(grid_name)), np.array(sgems.get_Y(grid_name)), np.array(sgems.get_Z(grid_name))
            coords_matrix = np.vstack((x,y,z)).T

            #calculating signed distances
            nan_filter = np.isfinite(prop_values)
            unique_rts = np.unique(prop_values[nan_filter])

            for rt in unique_rts:
                print('calculating signed distances for rock type {}'.format(int(rt)))
                filter_0 = prop_values != rt
                filter_1 = prop_values == rt
                points_0 = coords_matrix[filter_0]
                points_1 = coords_matrix[filter_1]

                sd_prop = []
                for idx, pt in enumerate(prop_values):
                    if np.isnan(pt):
                        sd_prop.append(float('nan'))

                    else:
                        point = coords_matrix[idx]
                        if pt == rt:
                            sd_prop.append(-min_dist(point, points_0))
                        else:
                           sd_prop.append(min_dist(point, points_1)) 
            
                sgems.set_property(grid_name, 'signed_distances_rt_{}'.format(int(rt)), sd_prop)

        return True
示例#5
0
def create_variable(grid, name, list):
    lst_props_grid = sgems.get_property_list(grid)
    prop_final_data_name = name

    if (prop_final_data_name in lst_props_grid):
        flag = 0
        i = 1
        while (flag == 0):
            test_name = prop_final_data_name + '-' + str(i)
            if (test_name not in lst_props_grid):
                flag = 1
                prop_final_data_name = test_name
            i = i + 1

    sgems.set_property(grid, prop_final_data_name, list)
示例#6
0
 def w_o_(cut_off):  #define waste as 0 and ore as grade
     for i in grid_value:
         if i <= cut_off:
             l_c.append(i)
         else:
             l_c.append(0)
     return sgems.set_property(head_grid, new_prop, l_c), l_c
示例#7
0
 def w_o(cut_off):  #define waste ore in 0 and 1
     for i in grid_value:
         if i >= cut_off:
             l_c.append(i)
         else:
             l_c.append(0)
     return sgems.set_property(head_grid, new_prop, l_c), l_c
示例#8
0
def build_geomodels(interpolated_variables, codes, tg_grid_name, tg_prop_name,
                    ids, acceptance, rt_prop):
    #implicit binary modeling
    if len(interpolated_variables) == 1:
        for t in interpolated_variables[int(codes[0])]:
            solid = np.where(interpolated_variables[int(codes[0])][t] < 0, 1,
                             0)
            sgems.set_property(tg_grid_name, 'rt_{}_{}'.format(codes[0], t),
                               solid.tolist())

    #multicategorical
    else:
        accepted_combs = []
        num_models = 0
        combinations = combinate(codes)
        for sc, c in enumerate(combinations):
            int_variables = []
            for idx, r in enumerate(codes):
                int_variables.append(interpolated_variables[int(r)][c[idx]])

            geomodel = []
            idx = 0
            for i in np.array(int_variables).T:
                if np.isnan(i).all():
                    geomodel.append(float('nan'))
                    idx = idx + 1
                else:
                    index = i.argmin(axis=0)
                    geomodel.append(float(codes[index]))
                    idx = idx + 1

            cm = confusion_matrix(rt_prop,
                                  np.array(geomodel)[ids],
                                  normalize='true')
            diag = np.diagonal(cm)
            print(diag)
            m = np.mean(diag)
            #if m < acceptance:
            if np.any(diag < acceptance):
                pass
            else:
                sgems.set_property(tg_grid_name, tg_prop_name + '_' + str(sc),
                                   geomodel)
                num_models = num_models + 1
                accepted_combs.append(c)
        print('{} geologic models accepted!'.format(num_models))
        print(accepted_combs)
示例#9
0
    def execute(self):
      
        #aqui vai o codigo
        #getting variables
        prob_grid_name = self.params['gridselectorbasic']['value']
        prob_var_names = self.params['orderedpropertyselector']['value']

        sim_grid_name = self.params['gridselectorbasic_2']['value']
        sim_var_names = self.params['orderedpropertyselector_2']['value']
        
        tg_grid_name = sim_grid_name
        tg_prop_name = self.params['lineEdit']['value'] 

        prob_var_names_lst = prob_var_names.split(';')
        sim_var_names_lst = sim_var_names.split(';')

        
        codes = [int(i.split('_')[-3]) for i in prob_var_names_lst]

        probs_matrix = np.array([sgems.get_property(prob_grid_name, p) for p in prob_var_names_lst])
        reals = np.array([sgems.get_property(sim_grid_name, p) for p in sim_var_names_lst])
        norm_reals = [norm.cdf(lst) for lst in reals]

        #sampling cats
        print('Sampling categories using the p-fields and building realizations...')
        t1 = time.time()
        
        probs_matrix = probs_matrix.T
        for real_idx, r in enumerate(norm_reals):
            realization = []
            for idx, b in enumerate(np.array(r).T):
                if np.isnan(probs_matrix[idx]).any():
                    realization.append(float('nan'))
                else:
                    position = cat_random_sample(probs_matrix[idx], b)
                    realization.append(int(codes[position]))
            sgems.set_property(tg_grid_name, tg_prop_name+'_real_'+str(real_idx), realization)

        t2 = time.time()
        print('Took {} seconds'.format(round((t2-t1), 2)))

        print('Finished!')

        return True
示例#10
0
 def repeat_all(grid, K, painter, n_iterations, n_steps_list, n_runs):
     for i, max_steps in zip(xrange(n_runs), n_steps_list):
         print i, "run"
         cost, painter = search_best_run(painter, K, n_iterations,
                                         max_steps)
         #plt_imshowN([painter.connected_map, painter.connected_map_ordem], [0, 0], [dict(cmap='jet', interpolation='nearest'), dict(cmap='jet')], dim=(1,2))
         plt_imshowN([
             painter.connected_map * (painter.grid != 0),
             painter.connected_map_ordem, painter.connected_map *
             (painter.grid != 0), painter.connected_map_ordem
         ], [0, 0, 1, 1],
                     [dict(cmap='jet', interpolation='nearest')] * 4,
                     dim=(2, 2))
         #np.reshape(grid, (1,np.product(grid.shape)))
         maps_seed = (painter.connected_map * painter.connected_map)
         maps_seed_ = np.ndarray.tolist(
             maps_seed.ravel())  #maps__ = np.array(maps_).T
         sgems.set_property(head_grid, new_prop1 + '-' + str(i),
                            maps_seed_)
         maps_ = painter.connected_map_ordem
         maps__ = np.ndarray.tolist(maps_.ravel())
         #
         sgems.set_property(head_grid, new_prop2 + '-' + str(i), maps__)
         ee = np.ma.masked_array(grid, mask=maps_ == 0)
         ee_ = ee.filled(0)
         ee__ = np.ndarray.tolist(ee_.ravel())
         sgems.set_property(head_grid, "sect_maps" + '-' + str(i), ee__)
示例#11
0
 def execute(self):
   
     #aqui vai o codigo
     #getting variables
     grid_name = self.params['gridselectorbasic']['value']
     prop_names = self.params['orderedpropertyselector']['value'].split(';')
     gamma = float(self.params['doubleSpinBox']['value'])
     var_type = self.params['comboBox']['value']
     
     props_values = []
     for v in prop_names:
         props_values.append(sgems.get_property(grid_name, v))
     props_values = np.array(props_values)
     
     #calculating probs
     print('Calculating probabilities...')
     if var_type == "Signed Distances":
         print('Applying softmax transformation...')
         probs_matrix = np.array([sofmax_transformation(sds, gamma, var_type) for sds in props_values.T])
         probs_matrix = probs_matrix.T
         for i, p in enumerate(probs_matrix):
             sgems.set_property(grid_name, prop_names[i]+'_gamma_'+str(gamma), p.tolist())
     else:
         print('Correcting proportions...')
         probs_matrix = prob_correction(props_values)
         for i, p in enumerate(probs_matrix):
             sgems.set_property(grid_name, prop_names[i]+'_corrected_prop', p.tolist())
     
     #calculating entropy
     print('Calculating entropy...')
     entropies = [entropy(probs) for probs in probs_matrix.T]
     entropies = np.array(entropies)
     entropies = (entropies - np.nanmin(entropies))/(np.nanmax(entropies) - np.nanmin(entropies))
     sgems.set_property(grid_name, 'entropy_gamma_'+str(gamma), entropies.tolist())
     
     print('Done!')
     
     return True
示例#12
0
import sgems

mask=sgems.get_property('mask', 'facies')
porosity=sgems.get_property('kriging grid', 'estimated porosity')

for i in range(1,len(mask)) :
        if mask[i] == 0:
                porosity[i]= -9966699
sgems.set_property('kriging grid', 'estimated porosity', porosity )
示例#13
0
    def execute(self):

        #aqui vai o codigo
        #getting variables
        var_type = self.params['comboBox']['value']
        tg_grid_name = self.params['gridselector']['value']
        tg_region_name = self.params['gridselector']['region']
        tg_prop_name = self.params['lineEdit']['value']
        keep_variables = self.params['checkBox_2']['value']
        props_grid_name = self.params['gridselectorbasic']['value']
        var_names = self.params['orderedpropertyselector']['value'].split(';')
        codes = [v.split('_')[-1] for v in var_names]

        #getting variograms
        print('Getting variogram models')

        variables = []
        nan_filters = []
        for v in var_names:
            values = np.array(sgems.get_property(props_grid_name, v))
            nan_filter = np.isfinite(values)
            filtered_values = values[nan_filter]
            nan_filters.append(nan_filter)
            variables.append(filtered_values)
        nan_filter = np.product(nan_filters, axis=0)
        nan_filter = nan_filter == 1

        x, y, z = np.array(sgems.get_X(props_grid_name))[nan_filter], np.array(
            sgems.get_Y(props_grid_name))[nan_filter], np.array(
                sgems.get_Z(props_grid_name))[nan_filter]

        use_model_file = self.params['checkBox']['value']
        if use_model_file == '1':
            path = self.params['filechooser']['value']
            variograms = helpers.modelfile_to_ar2gasmodel(path)
            if len(variograms) == 1:
                values_covs = list(variograms.values())
                varg_lst = values_covs * len(codes)
                variograms = {}
                variograms[0] = varg_lst[0]
        else:
            p = self.params
            varg_lst = helpers.ar2gemsvarwidget_to_ar2gascovariance(p)
            if len(varg_lst) == 0:
                variograms = {}
            if len(varg_lst) == 1:
                varg_lst = varg_lst * len(codes)
                variograms = {}
                variograms[0] = varg_lst[0]

            else:
                variograms = dict(zip(codes, varg_lst))

        #interpolating variables
        a2g_grid = helpers.ar2gemsgrid_to_ar2gasgrid(tg_grid_name,
                                                     tg_region_name)

        interpolated_variables = interpolate_variables(
            x, y, z, variables, codes, a2g_grid, variograms, keep_variables,
            var_type, tg_prop_name, tg_grid_name)

        #creating a geologic model
        geomodel = build_geomodel(var_type, interpolated_variables, codes,
                                  a2g_grid, tg_grid_name, tg_prop_name)

        #Refining model
        iterations = int(self.params['iterations']['value'])
        fx, fy, fz = int(self.params['fx']['value']), int(
            self.params['fy']['value']), int(self.params['fz']['value'])

        if iterations > 0:

            if len(variables) == 1:
                print(
                    'Sorry! Refinemnt works only for multicategorical models.')
                return False

            grid = a2g_grid
            geomodel = geomodel
            if tg_region_name != '':
                region = sgems.get_region(tg_grid_name, tg_region_name)

            for i in range(iterations):
                print('Refinemnt iteration {}'.format(i + 1))
                print('Defining refinment zone...')
                ref_zone, indices = refinement_zone(grid, geomodel)

                if tg_region_name != '':
                    downscaled_grid, downscaled_props = helpers.downscale_properties(
                        grid, [ref_zone, geomodel,
                               np.array(region)], fx, fy, fz)
                    region = downscaled_props[2]
                    m1 = np.array(downscaled_props[0] == -999)
                    m2 = np.array(downscaled_props[2] == 1)
                    mask = m1 * m2
                    mask = np.where(mask == 1, True, False).tolist()
                    nx, ny, nz = downscaled_grid.dim()[0], downscaled_grid.dim(
                    )[1], downscaled_grid.dim()[2]
                    sx, sy, sz = downscaled_grid.cell_size(
                    )[0], downscaled_grid.cell_size(
                    )[1], downscaled_grid.cell_size()[2]
                    ox, oy, oz = downscaled_grid.origin(
                    )[0], downscaled_grid.origin()[1], downscaled_grid.origin(
                    )[2]
                    downscaled_grid = ar2gas.data.CartesianGrid(
                        nx, ny, nz, sx, sy, sz, ox, oy, oz)
                else:
                    downscaled_grid, downscaled_props = helpers.downscale_properties(
                        grid, [ref_zone, geomodel], fx, fy, fz)
                    mask = downscaled_props[0] == -999
                grid = downscaled_grid
                grid_name = tg_grid_name + '_' + tg_prop_name + '_iteration_{}'.format(
                    i + 1)
                helpers.ar2gasgrid_to_ar2gems(grid_name, grid)

                masked_grid = ar2gas.data.MaskedGrid(downscaled_grid, mask)

                interpolated_variables = interpolate_variables(
                    x, y, z, variables, codes, masked_grid, variograms,
                    keep_variables, var_type, tg_prop_name, grid_name)

                geomodel = build_refined_geomodel(interpolated_variables,
                                                  downscaled_props, codes,
                                                  var_type)

            sgems.set_property(grid_name, tg_prop_name, geomodel)
            print('Finished!')

        return True
示例#14
0
def ar2gasprop_to_ar2gems(grid, grid_name, prop, prop_name):
    sgems.execute('NewCartesianGrid  {}::{}::{}::{}::{}::{}::{}::{}::{}::{}::0,00'.format(grid_name,
                                                                                          grid.dim()[0], grid.dim()[1], grid.dim()[2],
                                                                                          grid.cell_size()[0], grid.cell_size()[1], grid.cell_size()[2],
                                                                                          grid.origin()[0], grid.origin()[1], grid.origin()[2]))
    sgems.set_property(grid_name, prop_name, prop)
示例#15
0
    def execute(self):

        #aqui vai o codigo
        #getting variables
        tg_grid_name = self.params['gridselector']['value']
        tg_region_name = self.params['gridselector']['region']
        tg_prop_name = self.params['lineEdit']['value']
        keep_variables = self.params['checkBox_2']['value']
        props_grid_name = self.params['gridselectorbasic']['value']
        var_names = self.params['orderedpropertyselector']['value'].split(';')
        codes = [v.split('_')[-1] for v in var_names]

        dcf_param = self.params['comboBox_2']['value']
        f_min_o = float(self.params['doubleSpinBox']['value'])
        f_min_p = float(self.params['doubleSpinBox_2']['value'])

        acceptance = [
            float(i) for i in self.params['lineEdit_9']['value'].split(',')
        ]
        if len(acceptance) == 1 and len(codes) > 1:
            acceptance = acceptance * len(codes)
        acceptance = np.array(acceptance)

        #kernels variables
        function = self.params['comboBox']['value']
        supports = [
            float(i) for i in self.params['lineEdit_5']['value'].split(',')
        ]
        azms = [
            float(i) for i in self.params['lineEdit_2']['value'].split(',')
        ]
        dips = [
            float(i) for i in self.params['lineEdit_3']['value'].split(',')
        ]
        rakes = [
            float(i) for i in self.params['lineEdit_4']['value'].split(',')
        ]
        nuggets = [
            float(i) for i in self.params['lineEdit_6']['value'].split(',')
        ]
        major_med = [
            float(i) for i in self.params['lineEdit_7']['value'].split(',')
        ]
        major_min = [
            float(i) for i in self.params['lineEdit_8']['value'].split(',')
        ]

        kernels_par = {
            'supports': supports,
            'azms': azms,
            'dips': dips,
            'rakes': rakes,
            'nuggets': nuggets,
            'major_meds': major_med,
            'major_mins': major_min
        }

        for key in kernels_par:
            if len(kernels_par[key]) == 1 and len(codes) > 1:
                kernels_par[key] = kernels_par[key] * len(codes)

        kernels_par['function'] = [function] * len(codes)

        print(kernels_par)

        #getting coordinates
        variables = []
        nan_filters = []
        for v in var_names:
            values = np.array(sgems.get_property(props_grid_name, v))
            nan_filter = np.isfinite(values)
            filtered_values = values[nan_filter]
            nan_filters.append(nan_filter)
            variables.append(filtered_values)
        nan_filter = np.product(nan_filters, axis=0)
        nan_filter = nan_filter == 1

        x, y, z = np.array(sgems.get_X(props_grid_name))[nan_filter], np.array(
            sgems.get_Y(props_grid_name))[nan_filter], np.array(
                sgems.get_Z(props_grid_name))[nan_filter]

        #Interpolating variables
        a2g_grid = helpers.ar2gemsgrid_to_ar2gasgrid(tg_grid_name,
                                                     tg_region_name)
        print('Computing results by RBF using a {} kernel...'.format(function))
        interpolated_variables = {}

        for idx, v in enumerate(variables):
            rt = int(codes[idx])
            interpolated_variables[rt] = {}
            print('Interpolating RT {}'.format(rt))

            if kernels_par['supports'][idx] == 0:
                mask = v < 0
                kernels_par['supports'][idx] = helpers.max_dist(
                    x, y, z, mask, a2g_grid)
                print('Estimated support is {}'.format(
                    kernels_par['supports'][idx]))

            rbf = RBF(x,
                      y,
                      z,
                      v,
                      function=kernels_par['function'][idx],
                      nugget=kernels_par['nuggets'][idx],
                      support=kernels_par['supports'][idx],
                      major_med=kernels_par['major_meds'][idx],
                      major_min=kernels_par['major_mins'][idx],
                      azimuth=kernels_par['azms'][idx],
                      dip=kernels_par['dips'][idx],
                      rake=kernels_par['rakes'][idx])

            dc_param = dc_parametrization(v, dcf_param, f_min_o, f_min_p)
            for t in dc_param:
                print('Working on {}...'.format(t))

                rbf.train(dc_param[t])
                results = rbf.predict(a2g_grid)
                interpolated_variables[rt][t] = results

                if keep_variables == '1':
                    prop_name = 'interpolated_' + tg_prop_name + '_' + dcf_param + '_' + t + '_' + str(
                        rt)
                    sgems.set_property(tg_grid_name, prop_name,
                                       results.tolist())

        print('Done!')

        #Generating geological models
        print('Building geomodel...')
        #getting closest node to each sample
        ids = [
            sgems.get_closest_nodeid(tg_grid_name, xi, yi, zi)
            for xi, yi, zi in zip(x, y, z)
        ]
        rt_prop = sd_to_cat(variables, codes)
        build_geomodels(interpolated_variables, codes, tg_grid_name,
                        tg_prop_name, ids, acceptance, rt_prop)

        print('Done!')

        return True
示例#16
0
    def execute(self):

        '''#Execute the funtion read_params
        read_params(self.params)
        print self.params'''

        #Get the grid and rock type propery
        grid = self.params['propertyselectornoregion']['grid']
        prop = self.params['propertyselectornoregion']['property']

        #Error message
        if len(grid) == 0 or len(prop) == 0:
            print 'Select the rocktype property'
            return False

        #Get the X, Y and Z coordinates and RT property
        X = sgems.get_property(grid, '_X_')
        Y = sgems.get_property(grid, '_Y_')
        Z = sgems.get_property(grid, '_Z_')
        RT = sgems.get_property(grid, prop)

        elipsoide = self.params['ellipsoidinput']['value']
        elipsoide_split = elipsoide.split()

        range1 = float(elipsoide_split[0])
        range2 = float(elipsoide_split[1])
        range3 = float(elipsoide_split[2])

        azimuth = float(elipsoide_split[3])
        dip = float(elipsoide_split[4])
        rake = float(elipsoide_split[5])

        X, Y, Z = anis_search(X, Y, Z, range1, range2, range3, azimuth, dip, rake)

        #Creates a list of all rock types
        rt_list = []
        for i in RT:
            if i not in rt_list and not math.isnan(i):
                rt_list.append(i)

        #Sort the rock type list in crescent order
        rt_list = [int(x) for x in rt_list]
        rt_list.sort()

        #Create a empty distance matrix
        dist_matrix = np.zeros(shape = ((len(rt_list)), (len(RT))))

        #Calculates the signed distances, and append it in the distance matrix
        for i in range(len(rt_list)):
            rock = rt_list[i]

            for j in range(len(RT)):

                if math.isnan(RT[j]):
                    dist_matrix[i][j] = float('nan')

                elif RT[j] == rock:
                    dsmin = 1.0e21

                    for k in range(len(RT)):

                        if RT[j] != RT[k] and not math.isnan(RT[k]):
                            if (dist(X[j], Y[j], Z[j], X[k], Y[k], Z[k])) < dsmin:
                                dsmin = (dist(X[j], Y[j], Z[j], X[k], Y[k], Z[k]))

                        dist_matrix[i][j] = -dsmin

                else:
                    dsmin = 1.0e21

                    for k in range(len(RT)):

                        if RT[k] == rock:
                            if (dist(X[j], Y[j], Z[j], X[k], Y[k], Z[k])) < dsmin:
                                dsmin = (dist(X[j], Y[j], Z[j], X[k], Y[k], Z[k]))

                        dist_matrix[i][j] = dsmin

        #Creates the signed distances properties
        lst_props_grid=sgems.get_property_list(grid)

        for k in range(len(dist_matrix)):
            prop_final_data_name = 'Signed_Distances_RT_' + str(rt_list[k])

            if (prop_final_data_name in lst_props_grid):
                flag=0
                i=1
                while (flag==0):
                    test_name=prop_final_data_name+'-'+str(i)
                    if (test_name not in lst_props_grid):
                        flag=1
                        prop_final_data_name=test_name
                    i=i+1

            list = dist_matrix[k].tolist()
            sgems.set_property(grid, prop_final_data_name, list)

        return True
示例#17
0
    def execute(self):
        '''# Execute the funtion read_params
        read_params(self.params)
        print self.params'''

        #Get the grid and rock type propery
        grid = self.params['propertyselectornoregion']['grid']
        prop = self.params['propertyselectornoregion']['property']

        #Get the X, Y and Z coordinates and RT property
        X = sgems.get_property(grid, '_X_')
        Y = sgems.get_property(grid, '_Y_')
        Z = sgems.get_property(grid, '_Z_')
        RT_data = sgems.get_property(grid, prop)

        # Getting properties
        grid_krig = self.params['gridselectorbasic_2']['value']
        grid_var = self.params['gridselectorbasic']['value']
        props = (self.params['orderedpropertyselector']['value']).split(';')
        n_var = int(self.params['indicator_regionalization_input']
                    ['number_of_indicator_group'])
        n_prop = int(self.params['orderedpropertyselector']['count'])
        min_cond = self.params['spinBox_2']['value']
        max_cond = self.params['spinBox']['value']

        # Error messages
        if len(grid_var) == 0 or len(grid_krig) == 0:
            print 'Select the variables'
            return False

        if n_var != n_prop:
            print 'Number of variables and number of variograms models are diferent.'
            return False

        #Creating an empty list to store the interpolated distances
        SG_OK_list = []

        # Loop in every variable
        for i in xrange(0, n_var):

            # Getting variables
            prop_HD = props[i]
            prop_name = "Interpolated_" + str(prop_HD)
            prop_name_var = "Interpolated_" + str(prop_HD) + ' krig_var'
            var_str = ''
            indicator_group = "Indicator_group_" + str(i + 1)
            elipsoide = self.params['ellipsoidinput']['value']
            n_struct = int(
                self.params['indicator_regionalization_input'][indicator_group]
                ['Covariance_input']['structures_count'])

            # Error message
            if n_struct == 0:
                print 'Variogram have no structures'
                return False

            # Loop in every variogram structure
            for j in xrange(0, n_struct):
                # Getting variogram parameters
                Structure = "Structure_" + str(j + 1)

                cov_type = self.params['indicator_regionalization_input'][
                    indicator_group]['Covariance_input'][Structure][
                        'Two_point_model']['type']

                cont = self.params['indicator_regionalization_input'][
                    indicator_group]['Covariance_input'][Structure][
                        'Two_point_model']['contribution']

                if cov_type == 'Nugget Covariance':
                    #Writing variogram parameters on a variable in nugget effect case
                    var_str = var_str + '<{} type="{}">  <Two_point_model  contribution="{}"  type="{}"   >    </Two_point_model>    </Structure_1> '.format(
                        Structure, 'Covariance', cont, cov_type, Structure)

                else:
                    range1 = self.params['indicator_regionalization_input'][
                        indicator_group]['Covariance_input'][Structure][
                            'Two_point_model']['ranges']['range1']
                    range2 = self.params['indicator_regionalization_input'][
                        indicator_group]['Covariance_input'][Structure][
                            'Two_point_model']['ranges']['range2']
                    range3 = self.params['indicator_regionalization_input'][
                        indicator_group]['Covariance_input'][Structure][
                            'Two_point_model']['ranges']['range3']

                    rake = self.params['indicator_regionalization_input'][
                        indicator_group]['Covariance_input'][Structure][
                            'Two_point_model']['angles']['rake']
                    dip = self.params['indicator_regionalization_input'][
                        indicator_group]['Covariance_input'][Structure][
                            'Two_point_model']['angles']['dip']
                    azimuth = self.params['indicator_regionalization_input'][
                        indicator_group]['Covariance_input'][Structure][
                            'Two_point_model']['angles']['azimuth']

                    # Writing variogram parameters on a variable in other cases
                    var_str = var_str + '<{} type="{}">  <Two_point_model  contribution="{}"  type="{}"   >      <ranges range1="{}"  range2="{}"  range3="{}"   />      <angles azimuth="{}"  dip="{}"  rake="{}"   />    </Two_point_model>    </{}> '.format(
                        Structure, 'Covariance', cont, cov_type, range1,
                        range2, range3, azimuth, dip, rake, Structure)

            # Calling ordinary kriging for each variable, using the variograms parameters above
            sgems.execute(
                'RunGeostatAlgorithm  kriging::/GeostatParamUtils/XML::<parameters>  <algorithm name="kriging" />     <Variogram  structures_count="{}" >    {}  </Variogram>    <ouput_kriging_variance  value="1"  />     <output_n_samples_  value="0"  />     <output_average_distance  value="0"  />     <output_sum_weights  value="0"  />     <output_sum_positive_weights  value="0"  />     <output_lagrangian  value="0"  />     <Nb_processors  value="-2"  />    <Grid_Name value="{}" region=""  />     <Property_Name  value="{}" />     <Hard_Data  grid="{}"   property="{}"   region=""  />     <Kriging_Type  type="Ordinary Kriging (OK)" >    <parameters />  </Kriging_Type>    <do_block_kriging  value="1"  />     <npoints_x  value="5" />     <npoints_y  value="5" />     <npoints_z  value="5" />     <Min_Conditioning_Data  value="{}" />     <Max_Conditioning_Data  value="{}" />     <Search_Ellipsoid  value="{}" />    <AdvancedSearch  use_advanced_search="0"></AdvancedSearch>  </parameters>'
                .format(n_struct, var_str, grid_krig, prop_name, grid_var,
                        prop_HD, min_cond, max_cond, elipsoide))

            SG_OK_list.append(sgems.get_property(grid_krig, prop_name))

            #Deleting kriged distances
            sgems.execute('DeleteObjectProperties  {}::{}'.format(
                grid_krig, prop_name))
            sgems.execute('DeleteObjectProperties  {}::{}'.format(
                grid_krig, prop_name_var))

        RT = (self.params['orderedpropertyselector']['value']).split(';')

        #Determinig geomodel based on minimum estimed signed distance function
        GeoModel = SG_OK_list[0][:]

        t = 0
        for i in range(len(SG_OK_list[0])):
            sgmin = 10e21
            for j in range(len(SG_OK_list)):
                if SG_OK_list[j][i] < sgmin:
                    sgmin = SG_OK_list[j][i]
                    t = j
            if math.isnan(SG_OK_list[j][i]):
                GeoModel[i] = float('nan')
            else:
                GeoModel[i] = (int(RT[t].split('RT_')[-1]))

        #Creating GeoModel property
        lst_props_grid = sgems.get_property_list(grid_krig)
        prop_final_data_name = 'Geologic_Model'

        if (prop_final_data_name in lst_props_grid):
            flag = 0
            i = 1
            while (flag == 0):
                test_name = prop_final_data_name + '-' + str(i)
                if (test_name not in lst_props_grid):
                    flag = 1
                    prop_final_data_name = test_name
                i = i + 1

        #Assign conditioning data to grid node
        for i in range(len(RT_data)):
            if not math.isnan(RT_data[i]):
                closest_node = sgems.get_closest_nodeid(
                    grid_krig, X[i], Y[i], Z[i])
                GeoModel[closest_node] = RT_data[i]

        sgems.set_property(grid_krig, prop_final_data_name, GeoModel)

        #Operating softmax transformation
        if self.params['softmax_check']['value'] == '1':

            gamma = float(self.params['Gamma']['value'])
            Prob_list = SG_OK_list[:]

            for i in range(len(SG_OK_list[0])):
                soma = 0
                for j in range(len(SG_OK_list)):
                    soma = soma + math.exp(-SG_OK_list[j][i] / gamma)
                for j in range(len(SG_OK_list)):
                    Prob_list[j][i] = math.exp(
                        -SG_OK_list[j][i] / gamma) / soma

            #Creating probabilities propreties
            for k in range(len(Prob_list)):
                prop_final_data_name = 'Probability_RT' + str(
                    RT[k].split('RT_')[-1])

                if (prop_final_data_name in lst_props_grid):
                    flag = 0
                    i = 1
                    while (flag == 0):
                        test_name = prop_final_data_name + '-' + str(i)
                        if (test_name not in lst_props_grid):
                            flag = 1
                            prop_final_data_name = test_name
                        i = i + 1

                sgems.set_property(grid_krig, prop_final_data_name,
                                   Prob_list[k])

            #Operating servo-system
            if self.params['servo_check']['value'] == '1':
                var_rt_grid = self.params['targe_prop']['grid']
                var_rt_st = self.params['targe_prop']['property']
                var_rt_region = self.params['targe_prop']['region']
                if len(var_rt_grid) == 0 or len(var_rt_st) == 0:
                    print 'Select the target proportion property'
                    return False

                #Getting variables
                var_rt = sgems.get_property(var_rt_grid, var_rt_st)

                #Getting parameters
                lambda1 = float(self.params['Lambda']['value'])
                mi = lambda1 / (1 - lambda1)

                #Checking if a region exist
                if len(var_rt_region) == 0:
                    #Variable without a region
                    var_region = var_rt

                else:
                    region_rt = sgems.get_region(var_rt_grid, var_rt_region)
                    #Geting the variable inside the region
                    var_region = []
                    for i in range(len(var_rt)):
                        if region_rt[i] == 1:
                            var_region.append(var_rt[i])

                #Getting the target proportion
                target_prop = proportion(var_region, RT)

                #Getting the random path
                ran_path = random_path(Prob_list[0])

                #Removing the blocks outside the region from randon path
                if len(var_rt_region) != 0:
                    for i in range(len(region_rt)):
                        if region_rt[i] == 0:
                            ran_path.remove(i)

                #servo system
                p = 0
                GeoModel_corrected = GeoModel[:]

                visited_rts = []
                for j in ran_path:
                    visited_rts.append(GeoModel[j])
                    instant_proportions = proportion(visited_rts, RT)

                    sgmax = 10e-21
                    for i in range(len(Prob_list)):
                        Prob_list[i][j] = Prob_list[i][j] + (
                            mi * (target_prop[i] - instant_proportions[i]))
                        if Prob_list[i][j] > sgmax:
                            sgmax = Prob_list[i][j]
                            p = i

                    GeoModel_corrected[j] = int(RT[p][-1])
                    visited_rts[-1] = int(RT[p].split('RT_')[-1])

                #Correcting servo servo-system by the biggest proportion on a neighborhood
                GeoModel_corrected_servo_prop = GeoModel_corrected[:]
                ran_path_servo_correction = random_path(
                    GeoModel_corrected_servo_prop)
                for i in ran_path_servo_correction:
                    vizinhanca = neighb(grid_krig, i)

                    blk_geo_model_corrected_servo = []
                    for j in vizinhanca:
                        blk_geo_model_corrected_servo.append(
                            GeoModel_corrected_servo_prop[j])

                    proportions_servo = proportion(
                        blk_geo_model_corrected_servo, RT)
                    indice_max_prop = proportions_servo.index(
                        max(proportions_servo))

                    GeoModel_corrected_servo_prop[i] = int(
                        RT[indice_max_prop].split('RT_')[-1])

                #Creating Geologic_Model_Servo_System property
                prop_final_data_name = 'Geologic_Model_Servo_System'

                if (prop_final_data_name in lst_props_grid):
                    flag = 0
                    i = 1
                    while (flag == 0):
                        test_name = prop_final_data_name + '-' + str(i)
                        if (test_name not in lst_props_grid):
                            flag = 1
                            prop_final_data_name = test_name
                        i = i + 1

                #Creating Geologic_Model_Corrected property
                prop_final_data_name1 = 'Geologic_Model_Corrected'

                if (prop_final_data_name1 in lst_props_grid):
                    flag = 0
                    i = 1
                    while (flag == 0):
                        test_name1 = prop_final_data_name1 + '-' + str(i)
                        if (test_name1 not in lst_props_grid):
                            flag = 1
                            prop_final_data_name1 = test_name1
                        i = i + 1

                #Assign conditioning data to grid node
                for i in range(len(RT_data)):
                    if not math.isnan(RT_data[i]):
                        closest_node = sgems.get_closest_nodeid(
                            grid_krig, X[i], Y[i], Z[i])
                        GeoModel_corrected[closest_node] = RT_data[i]
                        GeoModel_corrected_servo_prop[closest_node] = RT_data[
                            i]

                #Setting properties
                sgems.set_property(grid_krig, prop_final_data_name,
                                   GeoModel_corrected)
                sgems.set_property(grid_krig, prop_final_data_name1,
                                   GeoModel_corrected_servo_prop)

        return True
示例#18
0
    def execute(self):
        dif = []
        Prop1 = sgems.get_property(self.params["GridName1"]["grid"], self.prop1)
        Prop2 = sgems.get_property(self.params["GridName2"]["grid"], self.prop2)

        dif2 = []
        for i in range(len(Prop1)):
            # 	  if(Prop1[i] != sgems.nan() and Prop2[i] != sgems.nan()):
            if math.isnan(Prop1[i]) == False and math.isnan(Prop2[i]) == False:
                dif.append((Prop1[i] - Prop2[i]) / Prop1[i])
            else:
                dif.append(sgems.nan())
        final_prop = "Deviation"
        lst = sgems.get_property_list(self.params["GridName1"]["grid"])
        if final_prop in lst:
            flag = 0
            i = 1
            while flag == 0:
                new_prop_name = final_prop + "_" + str(i)
                if new_prop_name not in lst:
                    flag = 1
                    final_prop = new_prop_name
                i = i + 1
        print final_prop
        # for i in range(len(Prop1)):
        # dif2.append((Prop1[i]+dif[i]))

        sgems.set_property(self.params["GridName1"]["grid"], final_prop, dif)
        # sgems.set_property(self.params["GridName2"]["grid"],"sum",dif2)
        PP = []
        a = float(self.cte)
        print a, self.cte
        for i in range(len(Prop2)):
            if self.check == "1":
                if self.radio1 == "1":
                    print Prop1[i], sgems.nan()
                    #  	      if(Prop1[i]==float(sgems.nan())):
                    if math.isnan(Prop1[i]) == True:
                        print "estou no laco i =", i,
                        PP.append(Prop2[i] + Prop2[i] * a)
                    else:
                        PP.append(Prop2[i])
                if self.radio2 == "1":
                    if math.isnan(Prop1[i]) == True:
                        PP.append(Prop2[i] - Prop2[i] * a)
                    else:
                        PP.append(Prop2[i])
        final_prop2 = "Post_process"
        lst2 = sgems.get_property_list(self.params["GridName2"]["grid"])
        if final_prop2 in lst2:
            flag = 0
            i = 1
            while flag == 0:
                new_prop_name2 = final_prop2 + "_" + str(i)
                if new_prop_name2 not in lst2:
                    flag = 1
                    final_prop2 = new_prop_name2
            i = i + 1
        print final_prop2
        sgems.set_property(self.params["GridName2"]["grid"], final_prop2, PP)

        return True
示例#19
0
def interpolate_variables(x, y, z, variables, codes, grid, vargs,
                          keep_variables, var_type, tg_prop_name,
                          tg_grid_name):
    print('Computing results by ar2gas dual kriging...')

    interpolated_variables = []

    if len(vargs) == 0:
        print(
            'Interpolating using the same covariance model for all variables')

        for idx, v in enumerate(variables):
            rt = int(codes[idx])
            print('Interpolating RT {}'.format(rt))
            if var_type == 'Indicators':
                mask = v == 1
            else:
                mask = v < 0
            var_range = helpers.max_dist(x, y, z, mask, grid)
            print('Estimated range is {}'.format(var_range))
            nugget = 0.01
            cov = [
                ar2gas.compute.Covariance.nugget(nugget),
                ar2gas.compute.Covariance.gaussian(1 - nugget, var_range,
                                                   var_range, var_range, 0.,
                                                   0., 0.)
            ]
            vargs[rt] = cov
            results = ar2gas_dual_krig(cov, x, y, z, v, grid)
            interpolated_variables.append(results)

            if keep_variables == '1':
                prop_name = 'interpolated_' + var_type + '_' + tg_prop_name + '_' + str(
                    rt)
                sgems.set_property(tg_grid_name, prop_name, results.tolist())

    elif len(vargs) == 1:
        print(
            'Interpolating using the same covariance model for all variables')
        cov = list(vargs.values())[0]

        for idx, v in enumerate(variables):
            rt = int(codes[idx])
            print('Interpolating RT {}'.format(rt))
            results = ar2gas_dual_krig(cov, x, y, z, v, grid)
            interpolated_variables.append(results)

            if keep_variables == '1':
                prop_name = 'interpolated_' + var_type + '_' + tg_prop_name + '_' + str(
                    rt)
                sgems.set_property(tg_grid_name, prop_name, results.tolist())

    else:
        for idx, v in enumerate(variables):
            rt = int(codes[idx])
            print('Interpolating using one covariance model per variables')
            print('Interpolating RT {}'.format(rt))
            results = ar2gas_dual_krig(vargs[rt], x, y, z, v, grid)
            interpolated_variables.append(results)

            if keep_variables == '1':
                prop_name = 'interpolated_' + var_type + '_' + tg_prop_name + '_' + str(
                    rt)
                sgems.set_property(tg_grid_name, prop_name, results.tolist())

    print('Finished interpolating!')
    return interpolated_variables
示例#20
0
    def execute(self):
        '''#Execute the funtion read_params
        read_params(self.params)
        print self.params'''

        #Get the grid and rock type propery
        grid = self.params['propertyselectornoregion']['grid']
        prop = self.params['propertyselectornoregion']['property']

        #Error message
        if len(grid) == 0 or len(prop) == 0:
            print 'Select the rocktype property'
            return False

        #Get the X, Y and Z coordinates and RT property
        X = sgems.get_property(grid, '_X_')
        Y = sgems.get_property(grid, '_Y_')
        Z = sgems.get_property(grid, '_Z_')
        RT = sgems.get_property(grid, prop)

        elipsoide = self.params['ellipsoidinput']['value']
        elipsoide_split = elipsoide.split()

        range1 = float(elipsoide_split[0])
        range2 = float(elipsoide_split[1])
        range3 = float(elipsoide_split[2])

        azimuth = float(elipsoide_split[3])
        dip = float(elipsoide_split[4])
        rake = float(elipsoide_split[5])

        X, Y, Z = anis_search(X, Y, Z, range1, range2, range3, azimuth, dip,
                              rake)

        #Creates a list of all rock types
        rt_list = []
        for i in RT:
            if i not in rt_list and not math.isnan(i):
                rt_list.append(i)

        #Sort the rock type list in crescent order
        rt_list = [int(x) for x in rt_list]
        rt_list.sort()

        #Create a empty distance matrix
        dist_matrix = np.zeros(shape=((len(rt_list)), (len(RT))))

        #Calculates the signed distances, and append it in the distance matrix
        for i in range(len(rt_list)):
            rock = rt_list[i]

            for j in range(len(RT)):

                if math.isnan(RT[j]):
                    dist_matrix[i][j] = float('nan')

                elif RT[j] == rock:
                    dsmin = 1.0e21

                    for k in range(len(RT)):

                        if RT[j] != RT[k] and not math.isnan(RT[k]):
                            if (dist(X[j], Y[j], Z[j], X[k], Y[k],
                                     Z[k])) < dsmin:
                                dsmin = (dist(X[j], Y[j], Z[j], X[k], Y[k],
                                              Z[k]))

                        dist_matrix[i][j] = -dsmin

                else:
                    dsmin = 1.0e21

                    for k in range(len(RT)):

                        if RT[k] == rock:
                            if (dist(X[j], Y[j], Z[j], X[k], Y[k],
                                     Z[k])) < dsmin:
                                dsmin = (dist(X[j], Y[j], Z[j], X[k], Y[k],
                                              Z[k]))

                        dist_matrix[i][j] = dsmin

        #Creates the signed distances properties
        lst_props_grid = sgems.get_property_list(grid)

        for k in range(len(dist_matrix)):
            prop_final_data_name = 'Signed_Distances_RT_' + str(rt_list[k])

            if (prop_final_data_name in lst_props_grid):
                flag = 0
                i = 1
                while (flag == 0):
                    test_name = prop_final_data_name + '-' + str(i)
                    if (test_name not in lst_props_grid):
                        flag = 1
                        prop_final_data_name = test_name
                    i = i + 1

            list = dist_matrix[k].tolist()
            sgems.set_property(grid, prop_final_data_name, list)

        return True
示例#21
0
    def execute(self):

        #aqui vai o codigo
        #getting variables
        tg_grid_name = self.params['gridselector']['value']
        tg_region_name = self.params['gridselector']['region']
        tg_prop_name = self.params['lineEdit']['value']
        prop_grid_name = self.params['propertyselectornoregion']['grid']
        prop_name = self.params['propertyselectornoregion']['property']

        cmin = str_to_float(self.params['lineEdit_3']['value'])
        cmax = str_to_float(self.params['lineEdit_2']['value'])
        gamma_min = str_to_float(self.params['lineEdit_5']['value'])
        gamma_max = str_to_float(self.params['lineEdit_4']['value'])
        n = int(self.params['spinBox']['value'])

        n_folds = int(self.params['spinBox_2']['value'])

        values = np.array(sgems.get_property(prop_grid_name, prop_name))
        nan_filter = np.isfinite(values)
        y_val = values[nan_filter]

        x, y, z = np.array(sgems.get_X(prop_grid_name))[nan_filter], np.array(
            sgems.get_Y(prop_grid_name))[nan_filter], np.array(
                sgems.get_Z(prop_grid_name))[nan_filter]
        X = np.array([x, y, z]).T

        grid = helpers.ar2gemsgrid_to_ar2gasgrid(tg_grid_name, tg_region_name)
        X_new = grid.locations()

        #scaling
        scaler = MinMaxScaler(feature_range=(0, 1))
        X = scaler.fit_transform(X)
        X_new = scaler.fit_transform(X_new)

        #grid search parameters
        C = np.linspace(cmin, cmax, n)
        gamma = np.linspace(gamma_min, gamma_max, n)
        param_grid = [
            {
                'estimator__C': C,
                'estimator__gamma': gamma,
                'estimator__kernel': ["rbf"] * n
            },
        ]

        #clf
        print("Tunning parameters...")
        clf = OneVsOneClassifier(SVC())
        model_tunning = GridSearchCV(clf, param_grid=param_grid, cv=n_folds)
        model_tunning.fit(X, y_val)

        print('accuracy ', model_tunning.best_score_)
        print('parameters', model_tunning.best_params_)

        clf = model_tunning.best_estimator_

        print("Predicting...")
        clf.fit(X, y_val)
        results = clf.predict(X_new)

        tp = np.ones(grid.size_of_mask()) * float('nan') if hasattr(
            grid, 'mask') else np.ones(grid.size()) * float('nan')
        if hasattr(grid, 'mask'):
            mask = grid.mask()
            r_idx = 0
            for idx, val in enumerate(mask):
                if val == True:
                    tp[idx] = results[r_idx]
                    r_idx = r_idx + 1
        else:
            tp = results

        sgems.set_property(tg_grid_name, tg_prop_name, tp.tolist())
        print('Done!')

        return True
示例#22
0
    def execute(self):

        #Execute the function read_params
        read_params(self.params)
        print self.params

        # ----------------------------------------------------------------------
        #
        # Cut-offs domaining
        #
        # ----------------------------------------------------------------------

        # checking if box is checked
        if self.params['cutoff_check_box']['value'] == str(1):

            # Getting variables
            prop = self.params['prop_cutoff']['property']
            grid_d = self.params['prop_cutoff']['grid']
            cutoffs_user = (self.params['cutoffs_user']['value']).split()
            prop_cutoff = sgems.get_property(grid_d, prop)

            # substituting commas for points in users inputed cutoffs
            cutoffs_user_no_comma = []
            for i in cutoffs_user:
                cutoffs_user_no_comma.append(float(i.replace(",", ".")))

            coded_dataset = sample_class_cutoff(prop_cutoff, cutoffs_user_no_comma)

            # setting the variable
            prop_final_data_name = 'coded_cutoff_'+self.params['prop_cutoff']['property']
            lst_props_grid = sgems.get_property_list(grid_d)

            if (prop_final_data_name in lst_props_grid):
                flag = 0
                i = 1
                while (flag == 0):
                    test_name = prop_final_data_name + '-' + str(i)
                    if (test_name not in lst_props_grid):
                        flag = 1
                        prop_final_data_name = test_name
                    i = i + 1

            sgems.set_property(grid_d, prop_final_data_name, coded_dataset)

        # ----------------------------------------------------------------------
        #
        # K-means clustering
        #
        # ----------------------------------------------------------------------

        # checking if box is checked
        if self.params['k_check_box']['value'] == str(1):

            # Getting variables
            grid_k = self.params['K_grid']['value']
            nclus = int(self.params['k_number']['value'])
            sec_props_k = (self.params['k_sec_var']['value']).split(';')
            prim_var_k = self.params['K_prim_var']['value']

            var_isotopic_kmeans, nan_indices = isotopic_dataset(grid_k, prim_var_k, sec_props_k)

            #runing kmeans
            k = KMeans(n_clusters=nclus).fit(var_isotopic_kmeans)
            RT = k.labels_

            RT_lst = []
            m=0
            for i in range(len(nan_indices)+len(RT)):
                check = True
                for j in nan_indices:
                    if i == j:
                        RT_lst.append(float('nan'))
                        check = False
                if check == True:
                    RT_lst.append(RT[m])
                    m = m+1

            create_variable(grid_k, 'KMeans', RT_lst)

        # ----------------------------------------------------------------------
        #
        # Hierarchical clustering
        #
        # ----------------------------------------------------------------------

        # checking if box is checked
        if self.params['hier_check_box']['value'] == str(1):

            # Getting variables
            grid_h = self.params['hier_grid']['value']
            sec_props_h = (self.params['hier_sec_var']['value']).split(';')
            prim_var_h = self.params['hier_prim_var']['value']
            criterion = self.params['criterion']['value']
            treshold = float(self.params['treshold']['value'])
            method_h = self.params['method']['value']
            dist_metric = self.params['dist_met']['value']

            var_isotopic_hier, nan_indices_h = isotopic_dataset(grid_h, prim_var_h, sec_props_h)

            # generate the linkage matrix
            Z = linkage(var_isotopic_hier, method = method_h, metric = dist_metric)

            df = pd.DataFrame(Z)
            df.to_csv('linkage_matrix.csv', index = False)

            plt.figure(figsize=(20,20))
            dn = dendrogram(Z)
            plt.savefig('dendogram.png')

            #printing cophenet corr.
            c, coph_dists = cophenet(Z, pdist(var_isotopic_hier))
            print "Cophenet correlation should be close to 1 : {}".format(c)

            #runnig hierarchical clustering
            try:
                RT_lst_h = fcluster(Z, treshold, criterion= criterion)
            except:
                print 'erro'

            RT_lst_h_final = []
            m = 0
            for i in range(len(nan_indices_h) + len(RT_lst_h)):
                check = True
                for j in nan_indices_h:
                    if i == j:
                        RT_lst_h_final.append(float('nan'))
                        check = False
                if check == True:
                    RT_lst_h_final.append(RT_lst_h[m])
                    m = m + 1

            create_variable(grid_h, 'Hierarchical', RT_lst_h_final)

        # ----------------------------------------------------------------------
        #
        # GMM clustering
        #
        # ----------------------------------------------------------------------

        # checking if box is checked
        if self.params['gmm_checkbox']['value'] == str(1):

            # getting variables
            grid_gmm = self.params['gmm_grid']['value']
            prim_var_gmm = self.params['gmm_prim']['value']
            sec_gmm = (self.params['gmm_sec']['value']).split(';')
            components = int(self.params['gmm_components']['value'])
            cov_type = self.params['cov_type']['value']

            var_isotopic_gmm, nan_indices_gmm = isotopic_dataset(grid_gmm, prim_var_gmm, sec_gmm)

            gmm = GMM(n_components = components, covariance_type= cov_type).fit(var_isotopic_gmm)

            RT_lst_gmm = gmm.predict(var_isotopic_gmm)

            probs = gmm.predict_proba(var_isotopic_gmm)
            probs_trans = probs.T

            RT_lst_gmm_final = []
            m = 0
            for i in range(len(nan_indices_gmm) + len(RT_lst_gmm)):
                check = True
                for j in nan_indices_gmm:
                    if i == j:
                        RT_lst_gmm_final.append(float('nan'))
                        check = False
                if check == True:
                    RT_lst_gmm_final.append(RT_lst_gmm[m])
                    m = m + 1

            create_variable(grid_gmm, 'GMM', RT_lst_gmm_final)

            for i,j in enumerate(probs_trans):
                RT_lst_gmm_probs = []
                m = 0
                for k in range(len(nan_indices_gmm) + len(RT_lst_gmm)):
                    check = True
                    for l in nan_indices_gmm:
                        if k == l:
                            RT_lst_gmm_probs.append(float('nan'))
                            check = False
                    if check == True:
                        RT_lst_gmm_probs.append(probs_trans[i][m])
                        m = m + 1

                create_variable(grid_gmm, 'GMM_prob_cluster_'+str(i), RT_lst_gmm_probs)

        return True