def _impute_lat_lon(cov_file, subchunk, config): cov = geoio.RasterioImageSource(cov_file) cov_data = features.extract_subchunks(cov, subchunk, config.n_subchunks, config.patchsize) nn_imputer = transforms.NearestNeighboursImputer() cov_data = nn_imputer(cov_data.reshape(cov_data.shape[0], 1)) return cov_data
def _mask_rows(x, subchunk, config): mask = config.mask if mask: mask_source = geoio.RasterioImageSource(mask) mask_data = features.extract_subchunks(mask_source, subchunk, config.n_subchunks, config.patchsize) mask_data = mask_data.reshape(mask_data.shape[0], 1) mask_x = mask_data.data[:, 0] != config.retain log.info('Areas with mask={} will be predicted'.format(config.retain)) assert x.shape[0] == mask_x.shape[0], 'shape mismatch of ' \ 'mask and inputs' x.mask = np.tile(mask_x, (x.shape[1], 1)).T return x
def cli(verbosity, geotiff, height, absorption, forward, outputdir, noise, impute): uncoverml.mllog.configure(verbosity) log.info("{} simulating gamma sensor model".format( "Forward" if forward else "Backward")) if os.path.isdir(geotiff): log.info("Globbing directory input for tif files") geotiff = os.path.join(geotiff, "*.tif") files = glob.glob(geotiff) my_files = np.array_split(files, mpiops.chunks)[mpiops.chunk_index] if len(my_files) == 0: log.critical("No files found. Exiting") sys.exit() for f in my_files: name = os.path.basename(f).rsplit(".", 1)[0] log.info("Loading {}".format(name)) image_source = geoio.RasterioImageSource(f) image = Image(image_source) data = image.data() # apply transforms here log.info("Computing sensor footprint") img_w, img_h, _ = data.shape S = filtering.sensor_footprint(img_w, img_h, image.pixsize_x, image.pixsize_y, height, absorption) # Apply and unapply the filter (mirrored boundary conditions) log.info("Applying transform to array of shape {}".format(data.shape)) if forward: t_data = filtering.fwd_filter(data, S) else: orig_mask = data.mask if np.ma.count_masked(data) > 0: data = filtering.kernel_impute(data, S) t_data = filtering.inv_filter(data, S, noise=noise) if impute: orig_mask = np.zeros_like(orig_mask, dtype=bool) t_data = np.ma.MaskedArray(data=t_data.data, mask=orig_mask) # Write output: log.info("Writing output to disk") write_data(t_data, name, image, outputdir, forward) log.info("All files transformed successfully")
def band_stats(ds, tif, band_no, partitions=100): log.info('Calculating band stats for {} band {} in {} partitions'.format( tif, band_no, partitions)) band = ds.GetRasterBand(band_no) # For statistics calculation stats = band.ComputeStatistics(False) no_data_val = band.GetNoDataValue() data_type = get_datatype(band) image_source = geoio.RasterioImageSource(tif) if data_type is 'Categorical': no_categories = stats[1] - stats[0] + 1 else: no_categories = np.nan l = [ basename(tif), band_no, no_data_val, ds.RasterYSize, ds.RasterXSize, stats[0], stats[1], stats[2], stats[3], data_type, no_categories, image_nans(image_source, partitions) ] ds = None # close dataset return [str(a) for a in l]
def numpy_band_stats(ds, tif, band_no, partitions=100): band = ds.GetRasterBand(band_no) data = band.ReadAsArray() no_data_val = band.GetNoDataValue() data_type = get_datatype(band) mask_data = ma.masked_where(data == no_data_val, data) if data_type is 'Categorical': no_categories = np.max(mask_data) - np.min(mask_data) + 1 else: no_categories = np.nan image_source = geoio.RasterioImageSource(tif) l = [ basename(tif), band_no, no_data_val, ds.RasterYSize, ds.RasterXSize, np.min(mask_data), np.max(mask_data), np.mean(mask_data), np.std(mask_data), data_type, float(no_categories), image_nans(image_source, partitions) ] ds = None return [str(a) for a in l]
def mask_subchunks(subchunk, config): image_source = geoio.RasterioImageSource(config.mask) result = features.extract_subchunks(image_source, subchunk, config.n_subchunks, config.patchsize) return result