def test_Image_split_overlap(array_image_src, num_chunks, overlap_pixels): nchunks = num_chunks overlap = overlap_pixels I = array_image_src._data Ichunks = [] images = [] for i in range(nchunks): img = Image(array_image_src, chunk_idx=i, nchunks=nchunks, overlap=overlap) images.append(img) chunk = img.data() if num_chunks == 1: Ichunks.append(chunk) elif i == 0: Ichunks.append(chunk[:, :-overlap]) elif i == (nchunks - 1): Ichunks.append(chunk[:, overlap:]) else: Ichunks.append(chunk[:, overlap:-overlap]) Irecon = np.hstack(Ichunks) assert I.shape == Irecon.shape assert np.all(I == Irecon)
def extract_subchunks(image_source, subchunk_index, n_subchunks, patchsize): equiv_chunks = n_subchunks * mpiops.chunks equiv_chunk_index = mpiops.chunks*subchunk_index + mpiops.chunk_index image = Image(image_source, equiv_chunk_index, equiv_chunks, patchsize) x = patch.all_patches(image, patchsize) 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 _extract_from_chunk(image_source, targets, chunk_index, total_chunks, patchsize): image_chunk = Image(image_source, chunk_index, total_chunks, patchsize) y_min = np.min(targets.positions[:, 1]) y_max = np.max(targets.positions[:, 1]) if _image_has_targets(y_min, y_max, image_chunk): x = patch.patches_at_target(image_chunk, patchsize, targets) else: x = None return x
def _extract_from_chunk(image_source, targets, chunk_index, total_chunks, patchsize): image_chunk = Image(image_source, chunk_index, total_chunks, patchsize) # figure out which chunks I need to consider y_min = targets.positions[0, 1] y_max = targets.positions[-1, 1] if _image_has_targets(y_min, y_max, image_chunk): x = patch.patches_at_target(image_chunk, patchsize, targets) else: x = None return x
def test_Image_split(array_image_src, num_chunks): Ichunks = [] I = array_image_src._data for i in range(num_chunks): chunk = Image(array_image_src, chunk_idx=i, nchunks=num_chunks).data() Ichunks.append(chunk) # Reverse Ichunks to account for y-decreasing convention in images Irecon = np.hstack(Ichunks) assert I.shape == Irecon.shape assert np.all(I == Irecon)
def make_image(pix_size_single, origin_point, is_flipped, num_chunks, chunk_position): data = np.random.rand(32, 24, 1) masked_array = np.ma.array(data=data, mask=False) pix_size = (pix_size_single, pix_size_single) if is_flipped: pix_size = (pix_size_single, -1 * pix_size_single) # origin needs to change as well origin = (origin_point, origin_point) src = geoio.ArrayImageSource(masked_array, origin, crs, pix_size) if num_chunks > 1: if chunk_position == 'start': chunk_index = 0 elif chunk_position == 'end': chunk_index = num_chunks - 1 else: chunk_index = round(num_chunks/2) ispec = Image(src, chunk_idx=chunk_index, nchunks=num_chunks) else: ispec = Image(src) return ispec
def test_Image_data(array_image_src): true_data = array_image_src._data data = Image(array_image_src).data() assert np.all(true_data.data == data.data) assert np.all(true_data.mask == data.mask)