def checkParameterValues(self, parameters, context): tiBands = numpy.array(self.parameterAsInts(parameters, self.BANDS_TI, context), dtype=int) diBands = numpy.array(self.parameterAsInts(parameters, self.BANDS_INPUT, context), dtype=int) if (tiBands.size < diBands.size): return False, self.tr( 'Training image require at least the same amount of bands that the simulation grid' ) serverAddress = self.parameterAsString(parameters, self.PARAM_SA, context) if (not (serverAddress) or serverAddress == ''): serverAddress = 'localhost' if (g2s('-sa', serverAddress, '-serverStatus') <= 0): return False, self.tr('no G2S server available at this address') # newField = self.parameterAsBool(parameters, self.NEW_FIELD, context) # fieldName = self.parameterAsString(parameters, self.FIELD_NAME, context).strip() # if newField and len(fieldName) == 0: # return False, self.tr('Field name is not set. Please enter a field name') return super(G2SAlgorithm, self).checkParameterValues(parameters, context)
sizeDest = numpy.size(conDestination) position1 = numpy.random.permutation(sizeDest)[1:math.ceil(sizeDest * pourcantage / 100)] position2 = numpy.random.permutation(sizeDest)[1:math.ceil(sizeDest * pourcantage / 100)] conDestination.reshape(sizeDest, 1)[position1] = source.reshape(sizeDest, 1)[position2] # Categorical sourceCat = misc.imread('../TrainingImages/gobi_dune.png') sourceCat = sourceCat[:math.floor(numpy.size(sourceCat, 0) / 2), :math. floor(numpy.size(sourceCat, 1) / 2), 2] sourceCat = sourceCat / 1. # simple echo data = g2s('-sa', serverAddress, '-a', 'echo', '-ti', source, '-dt', numpy.zeros(shape=(1, 1))) plt.imshow(data[0]) if verbose: print(data) plt.show(block=False) plt.pause(0.1) # simple unconditional simulation with QS data = g2s('-sa', serverAddress, '-a', 'qs', '-ti', source, '-di', destination, '-dt', numpy.zeros(shape=(1, 1)), '-k', 1.5, '-n', 50, '-s', 100) plt.imshow(data[0]) if verbose: print(data) plt.show(block=False) plt.pause(0.1)
ki = np.power(2, kernel) s = 100 # initial random seed jb = 4 # number of parallel jobs nr = 100 # number of realizations #%% LAUNCH SIMULATION # IMPORTANT: before launching the sim, start the server from the G2S folder: ~/lib/G2S/build/c++-build$ ./server qssim = np.empty(np.hstack([np.shape(di), nr])) * np.nan plt.figure(figsize=(4, 4)) t = time.time() for i in range(nr): s = s + 1 print("realization " + str(i)) simout = g2s('-sa', serverAddress, '-a', 'qs', '-ti', ti, '-di', di, '-dt', dt, '-ki', ki, '-k', k, '-n', n, '-s', s, '-j', jb) """ qssim[:,:,i]=simout[0] #plt.subplot(4,4,i+1) plt.figure() plt.imshow(simout[0], cmap='gray') plt.axis('off') plt.tight_layout() #plt.savefig('GeneratedImages/MPS/gaussian'+str(i)+'.png') """ print("time needed : " + str(time.time() - t)) #plt.tight_layout() #plt.savefig('generated_images/MPS/QS28.png') #%% VISUALIZE RESULT # ti vs sim images
import requests from io import BytesIO from g2s import g2s import matplotlib.pyplot as plt import time # load example training image ('stone') ti = numpy.array(Image.open(BytesIO(requests.get( 'https://raw.githubusercontent.com/GAIA-UNIL/TrainingImagesTIFF/master/stone.tiff').content))); # asynchronous QS call using G2S with the "-submitOnly" flag jobid_1=g2s('-a','qs', '-submitOnly', '-ti',ti, '-di',numpy.zeros((200,200))*numpy.nan, '-dt',[0], '-k',1.2, '-n',50, '-j',0.5); # second asynchronous call that waits for job 1 to finish using the "-after" flag jobid_2 = g2s('-a','qs', '-after',jobid_1, '-submitOnly', '-ti',ti, '-di',numpy.zeros((200,200))*numpy.nan, '-dt',[0], '-k',1.2, '-n',50, '-j',0.5);
# load data ti = numpy.array( Image.open( BytesIO( requests.get( 'https://raw.githubusercontent.com/GAIA-UNIL/TrainingImagesTIFF/master/stone.tiff' ).content))) # empty grid conditioning = numpy.zeros((200, 200)) * numpy.nan # fill the grid with 50 random points conditioning.flat[numpy.random.permutation( conditioning.size)[:50]] = ti.flat[numpy.random.permutation(ti.size)[:50]] # QS call using G2S simulation, _ = g2s('-a', 'qs', '-ti', ti, '-di', conditioning, '-dt', numpy.zeros((1, )), '-k', 1.2, '-n', 50, '-j', 0.5) # Display results fig, (ax1, ax2, ax3) = plt.subplots(1, 3) fig.suptitle('Unconditional simulation') ax1.imshow(ti) ax1.set_title('Training image') ax1.axis('off') ax2.imshow(conditioning) ax2.set_title('Conditioning') ax2.axis('off') ax3.imshow(simulation) ax3.set_title('Simulation') ax3.axis('off') plt.show()
import numpy from PIL import Image import requests from io import BytesIO from g2s import g2s import matplotlib.pyplot as plt #load data ti = numpy.array(Image.open(BytesIO(requests.get('https://raw.githubusercontent.com/GAIA-UNIL/TrainingImagesTIFF/master/stone.tiff').content))); # QS call using G2S simulation,_=g2s('-a','qs','-ti',ti,'-di',numpy.zeros((200,200))*numpy.nan,'-dt',numpy.zeros((1,)),'-k',1.2,'-n',50,'-j',0.5); #Display results fig, (ax1, ax2) = plt.subplots(1, 2) fig.suptitle('Unconditional simulation') ax1.imshow(ti) ax1.set_title('Training image'); ax1.axis('off'); ax2.imshow(simulation) ax2.set_title('Simulation'); ax2.axis('off'); plt.show()
import numpy from PIL import Image import requests from io import BytesIO from g2s import g2s import matplotlib.pyplot as plt #load data tiWithGap = numpy.array( Image.open( BytesIO( requests.get( 'https://raw.githubusercontent.com/GAIA-UNIL/TrainingImagesTIFF/master/stone.tiff' ).content))) tiWithGap[60:140, 60:140] = numpy.nan # QS call using G2S simulation, _ = g2s('-a', 'qs', '-ti', tiWithGap, '-di', tiWithGap, '-dt', numpy.zeros((1, )), '-k', 1.2, '-n', 25, '-j', 0.5) #Display results fig, (ax1, ax2) = plt.subplots(1, 2) fig.suptitle('Gap filling') ax1.imshow(tiWithGap) ax1.set_title('Training image') ax1.axis('off') ax2.imshow(simulation) ax2.set_title('Simulation') ax2.axis('off') plt.show()
#Load example boolean training image "Damier 3D" and crop it to reduce computational requirements dim = 30 ti = tf.imread( BytesIO( requests.get( 'https://raw.githubusercontent.com/GAIA-UNIL/TrainingImagesTIFF/master/Damier3D.tiff' ).content))[:dim, :dim, :dim] # QS call using G2S simulation, index, time, _ = g2s( '-a', 'qs', '-ti', ti, '-di', np.zeros_like(ti) * np.nan, '-dt', [1], #1 for categorical variables '-k', 1.2, '-n', 30) #Display results fig = plt.figure(figsize=(20, 15)) ax1 = fig.add_subplot(121, projection='3d') ax1.voxels(ti, alpha=0.9, facecolor='tab:blue', edgecolor='black') ax1.view_init(azim=45) ax1.set_title('3D Training image') ax2 = fig.add_subplot(122, projection='3d') ax2.voxels(simulation, alpha=0.9, facecolor='tab:blue', edgecolor='black')
# load example training image ('stone') ti = numpy.array( Image.open( BytesIO( requests.get( 'https://raw.githubusercontent.com/GAIA-UNIL/TrainingImagesTIFF/master/stone.tiff' ).content))) # create empty grid and add conditioning data conditioning = numpy.zeros((200, 200)) * numpy.nan # fill the grid with 50 random points conditioning.flat[numpy.random.permutation( conditioning.size)[:50]] = ti.flat[numpy.random.permutation(ti.size)[:50]] # QS call using G2S simulation, _ = g2s('-a', 'qs', '-ti', ti, '-di', conditioning, '-dt', [0], '-k', 1.2, '-n', 30, '-j', 0.5) # Display results fig, (ax1, ax2, ax3) = plt.subplots(1, 3, figsize=(7, 4), subplot_kw={'aspect': 'equal'}) fig.suptitle('QS Conditional simulation', size='xx-large', y=0.9) ax1.imshow(ti) ax1.set_title('Training image') ax1.axis('off') ax2.scatter(*numpy.meshgrid(numpy.arange(200), numpy.arange(200, 0, -1)), s=5, c=conditioning, marker='.') ax2.set_title('Conditioning')
def processAlgorithm(self, parameters, context, model_feedback): """ Here is where the processing itself takes place. """ feedback = QgsProcessingMultiStepFeedback(100, model_feedback) # Retrieve the feature source and sink. The 'dest_id' variable is used # to uniquely identify the feature sink, and must be included in the # dictionary returned by the processAlgorithm function. ti = self.parameterAsRasterLayer(parameters, self.TI, context) pixelSize = ti.rasterUnitsPerPixelX() source = self.parameterAsRasterLayer(parameters, self.INPUT, context) output = self.parameterAsOutputLayer(parameters, self.OUTPUT, context) k = self.parameterAsDouble(parameters, self.PARAM_K, context) n = self.parameterAsInt(parameters, self.PARAM_N, context) kernelType = self.parameterAsEnum(parameters, self.KERNEL_TYPE, context) alpha = self.parameterAsDouble(parameters, self.KERNEL_ALPHA, context) tiBands = numpy.array(self.parameterAsInts(parameters, self.BANDS_TI, context), dtype=int) diBands = numpy.array(self.parameterAsInts(parameters, self.BANDS_INPUT, context), dtype=int) jValue = self.parameterAsDouble(parameters, self.PARAM_J, context) serverAddress = self.parameterAsString(parameters, self.PARAM_SA, context) ignoreResolution = self.parameterAsBool(parameters, self.PARAM_IGNORE_RESOLUTION, context) if (not (serverAddress) or serverAddress == ''): serverAddress = 'localhost' #QgsMessageLog.logMessage(str(tiBands), 'G2S'); #QgsMessageLog.logMessage(ti.bandName(2), 'G2S'); #QgsMessageLog.logMessage(source.bandName(3), 'G2S'); tiGDAL = gdal.Open(ti.source()) diGDAL = gdal.Open(source.source()) if (not (ignoreResolution) and (abs(ti.rasterUnitsPerPixelX() - source.rasterUnitsPerPixelX()) + abs(ti.rasterUnitsPerPixelY() - source.rasterUnitsPerPixelY())) > 0.05 * ti.rasterUnitsPerPixelX()): QgsMessageLog.logMessage( "Resolution are too different please reproject first", 'G2S', level=Qgis.Critical) return {} # do mapping # di=numpy.zeros((diGDAL.GetRasterBand(1).ReadAsArray().shape)+(tiGDAL.RasterCount,))*numpy.nan; # ti=numpy.zeros((tiGDAL.GetRasterBand(1).ReadAsArray().shape)+(tiGDAL.RasterCount,))*numpy.nan; # atLeatOneMatchingName=False; # for y in tiBands: # ti[:,:,y-1]=tiGDAL.GetRasterBand(y).ReadAsArray(); # for x in range(1,diGDAL.RasterCount): # for y in range(1,tiGDAL.RasterCount): # tiName=tiGDAL.GetRasterBand(y).GetDescription(); # diName=diGDAL.GetRasterBand(x).GetDescription(); # if(diName==tiName and tiName!='' and tiName!=None): # di[:,:,y-1]=diGDAL.GetRasterBand(x).ReadAsArray(); # atLeatOneMatchingName=True; # if not(atLeatOneMatchingName): # for x in range(1,diGDAL.RasterCount): # di[:,:,x-1]=diGDAL.GetRasterBand(x).ReadAsArray(); # create numpy array and load data di = numpy.zeros((diGDAL.GetRasterBand(1).ReadAsArray().shape) + (tiBands.size, )) * numpy.nan ti = numpy.zeros((tiGDAL.GetRasterBand(1).ReadAsArray().shape) + (tiBands.size, )) * numpy.nan for y in range(0, tiBands.size): ti[:, :, y] = tiGDAL.GetRasterBand(int(tiBands[y])).ReadAsArray() for x in range(0, diBands.size): di[:, :, x] = diGDAL.GetRasterBand(int(diBands[x])).ReadAsArray() #check for missing bands in the TI numberOfDataProLayer = numpy.sum(numpy.logical_not(numpy.isnan(ti)), axis=(0, 1)) if (numpy.min(numberOfDataProLayer) == 0): QgsMessageLog.logMessage( "Some layers of the training image don't have data, please remove them.", 'G2S', level=Qgis.Critical) # QgsMessageLog.logMessage("Some layers of the training image don't have data, please remove them or select the appropriated option", 'G2S',level=Qgis.Critical); return {} #check for nan padding in the ti dataMaskTi = numpy.logical_not(numpy.min(numpy.isnan(ti), axis=2)) true_points = numpy.argwhere(dataMaskTi) top_left = true_points.min(axis=0) bottom_right = true_points.max(axis=0) ti = ti[top_left[0]:bottom_right[0] + 1, top_left[1]:bottom_right[1] + 1, :].copy() #generate the appropiate kernel sizeKernel = numpy.array(ti.shape) // 4 maxKernel = numpy.pad(numpy.ones(shape=(1, 1, 2)), ((sizeKernel[0], ), (sizeKernel[1], ), (0, )), 'constant') distKernel = pixelSize * ndimage.morphology.distance_transform_edt( 1 - maxKernel) if kernelType == 0: #Uniform kernel = distKernel <= alpha pass if kernelType == 1: #Exponential kernel = numpy.exp(-alpha * distKernel) pass if kernelType == 2: #Gaussian kernel = numpy.exp(-alpha * distKernel**2) pass kernel[kernel < 0.01] = 0 # argwhere will give you the coordinates of every non-zero point true_points = numpy.argwhere(kernel) # take the smallest points and use them as the top left of your crop top_left = true_points.min(axis=0) # take the largest points and use them as the bottom right of your crop bottom_right = true_points.max(axis=0) kernel = kernel[top_left[0]:bottom_right[0] + 1, top_left[1]:bottom_right[1] + 1, :].copy() # QgsMessageLog.logMessage(str(kernel.shape), 'G2S') # QgsMessageLog.logMessage(str(ti.shape), 'G2S') # QgsMessageLog.logMessage(str(di.shape), 'G2S') # QgsMessageLog.logMessage(str(numpy.zeros(shape=(tiBands.size,)).shape), 'G2S') feedback.setCurrentStep(0) id = g2s('-sa', serverAddress, '-a', 'qs', '-ti', ti, '-di', di, '-dt', numpy.zeros(shape=(ti.shape[2], )), '-ki', kernel, '-j', jValue, '-k', k, '-n', n, '-submitOnly', '-noTO') stop = False while not (stop): sleep(0.1) # Time in seconds progress = g2s('-sa', serverAddress, '-statusOnly', id) if (isinstance(progress, float)): feedback.setCurrentStep(progress) else: break if (progress >= 99): stop = True if model_feedback.isCanceled(): g2s('-sa', serverAddress, '-kill', id) return {} arr = g2s('-sa', serverAddress, '-waitAndDownload', id) feedback.setCurrentStep(100) driver = gdal.GetDriverByName("GTiff") outdata = driver.Create(output, arr[0].shape[1], arr[0].shape[0], arr[0].shape[2], tiGDAL.GetRasterBand(1).DataType) outdata.SetGeoTransform( diGDAL.GetGeoTransform()) ##sets same geotransform as input outdata.SetProjection( diGDAL.GetProjection()) ##sets same projection as input for y in range(0, arr[0].shape[2]): # QgsMessageLog.logMessage(str(y), 'G2S'); outdata.GetRasterBand(y + 1).WriteArray(arr[0][:, :, y].copy()) #outdata.GetRasterBand(1).SetNoDataValue(10000)##if you want these values transparent outdata.FlushCache() ##saves to disk!! # (sink, dest_id) = self.parameterAsSink(parameters, self.OUTPUT, # context, source.fields(), source.wkbType(), source.sourceCrs()) # Compute the number of steps to display within the progress bar and # get features from source # Return the results of the algorithm. In this case our only result is # the feature sink which contains the processed features, but some # algorithms may return multiple feature sinks, calculated numeric # statistics, etc. These should all be included in the returned # dictionary, with keys matching the feature corresponding parameter # or output names. return {self.OUTPUT: output}
ti = numpy.array( Image.open( BytesIO( requests.get( 'https://raw.githubusercontent.com/GAIA-UNIL/TrainingImagesTIFF/master/stone.tiff' ).content))) # QS call using G2S simulation, _ = g2s( '-a', 'qs', '-ti', ti, '-di', numpy.zeros((200, 200)) * numpy.nan, '-dt', [0], #Zero for continuous variables '-k', 1.2, '-n', 50, '-j', 0.5) #Display results fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(7, 4)) fig.suptitle('QS Unconditional simulation', size='xx-large') ax1.imshow(ti) ax1.set_title('Training image') ax1.axis('off') ax2.imshow(simulation)
ax1.imshow(ti_fine, norm=norm) ax1.set_title('Fine resolution training image') ax2.imshow(ti_coarse, norm=norm) ax2.set_title('Coarse resolution training image') #Crop half of the image to be used as ti ti = np.stack((ti_fine[:500, :500], ti_coarse[:500, :500]), axis=2) #Crop upper right corner to be used as di di_coarse = ti_coarse[:200, -200:] di_fine = np.zeros((200, 200)) * np.nan di = np.stack((di_fine, di_coarse), axis=2) #dt consists of two zeros representing two continuous variables dt = [0] * ti.shape[-1] # QS call using G2S simulation, index, _ = g2s('-a', 'qs', '-ti', ti, '-di', di, '-dt', dt, '-k', 1.2, '-n', 50, '-j', 0.5) #Display results norm = colors.Normalize(vmin=ti_fine.min(), vmax=ti_fine.max()) f, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2, figsize=(12, 12), sharex=True, sharey=True) plt.subplots_adjust(wspace=0.1, hspace=0.1) plt.suptitle('QS Downscaling', size='xx-large') ax1.imshow(di_coarse, norm=norm) ax1.set_title('Coarse res di') ax2.imshow(simulation[:, :, 0], norm=norm) ax2.set_title('Simulation') ax3.imshow(index)
import numpy from PIL import Image import requests from io import BytesIO from g2s import g2s import matplotlib.pyplot as plt # load example training image ('stone') and cut out a part of it tiWithGap = numpy.array( Image.open( BytesIO( requests.get( 'https://raw.githubusercontent.com/GAIA-UNIL/TrainingImagesTIFF/master/stone.tiff' ).content))) tiWithGap[60:140, 60:140] = numpy.nan # QS call using G2S simulation, _ = g2s('-a', 'qs', '-ti', tiWithGap, '-di', tiWithGap, '-dt', [0], '-k', 1.2, '-n', 25, '-j', 0.5) #Display results fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(7, 4)) fig.suptitle('QS Gap filling', size='xx-large') ax1.imshow(tiWithGap) ax1.set_title('Training image') ax1.axis('off') ax2.imshow(simulation) ax2.set_title('Simulation') ax2.axis('off') plt.show()