def map_bands_to_indices(target_bands, satellite=8): band_map = defaultdict(list) for band in landsat_rasters()[satellite]: band_map[band] = [] for band in static_rasters(): band_map[band] = [] for band in climate_rasters(): band_map[band] = [] image_directory = '/home/thomas/share/image_data/train/37_28_2013/' extensions = (".tif", ".TIF") for dirpath, dirnames, filenames in os.walk(image_directory): for f in filenames: if any(ext in f for ext in extensions): for band in band_map: if f.endswith(band): band_map[band].append(os.path.join(dirpath, f)) for band in band_map: band_map[band] = sorted(band_map[band]) # ensures ordering within bands - sort by time. indices = [] i = 0 for feat in sorted(band_map.keys()): # ensures the stack is in the same order each time. feature_rasters = band_map[feat] for feature_raster in feature_rasters: for band in target_bands: if feature_raster.endswith(band): indices.append(i) i += 1 return indices
def median_of_three(paths_map, image_stack, target_shape, satellite=8): j = 0 out_image_stack = np.zeros((19, target_shape[1], target_shape[2])) out_idx = 0 for band in sorted(paths_map.keys()): if band in landsat_rasters()[satellite]: slc = np.zeros((3, target_shape[1], target_shape[2])) for i, sub_band in enumerate(paths_map[band]): slc[i] = image_stack[j] j += 1 out_image_stack[out_idx] = np.median(slc, axis=0) out_idx += 1 else: out_image_stack[out_idx] = image_stack[j] out_idx += 1 return out_image_stack
def _landsat_band_map(subdirectory, satellite=8): band_map = dict() for band in landsat_rasters()[satellite]: band_map[band] = None for band in static_rasters(): band_map[band] = None for band in climate_rasters(): band_map[band] = None extensions = (".tif", ".TIF") for dirpath, dirnames, filenames in os.walk(subdirectory): for f in filenames: if any(ext in f for ext in extensions): for band in band_map: if f.endswith(band): band_map[band] = os.path.join(dirpath, f) return band_map
def mean_of_three(paths_map, image_stack, target_shape, satellite=8): # iterate over paths_map # iterate over each raster in paths_map j = 0 out_image_stack = np.zeros((19, target_shape[1], target_shape[2])) out_idx = 0 for band in sorted(paths_map.keys()): if band in landsat_rasters()[satellite]: for sub_band in paths_map[band]: out_image_stack[out_idx] += image_stack[j] j += 1 out_image_stack[out_idx] /= 3 out_idx += 1 else: out_image_stack[out_idx] = image_stack[j] out_idx += 1 return out_image_stack
def all_rasters(image_directory, satellite=8): ''' Recursively get all rasters in image_directory and its subdirectories, and adds them to band_map. ''' band_map = defaultdict(list) for band in landsat_rasters()[satellite]: band_map[band] = [] for band in static_rasters(): band_map[band] = [] for band in climate_rasters(): band_map[band] = [] extensions = (".tif", ".TIF") for dirpath, dirnames, filenames in os.walk(image_directory): for f in filenames: if any(ext in f for ext in extensions): for band in band_map: if f.endswith(band): band_map[band].append(os.path.join(dirpath, f)) for band in band_map: band_map[band] = sorted(band_map[band]) # ensures ordering within bands - sort by time. return band_map