def process_bspots(depths, out, filter): """Label bluespots. Assign unique bluespot ID to all cells belonging to a bluespot. Optionally disregarding some bluespots based on a filter expression. ID 0 (zero) is used for cells not belonging to a bluespot. """ depths_reader = io.RasterReader(depths) labeled_writer = io.RasterWriter(out, depths_reader.transform, depths_reader.crs, 0) filter_function = parse_filter(filter) transform = depths_reader.transform cell_width = abs(transform[1]) cell_height = abs(transform[5]) cell_area = cell_width * cell_height depths_data = depths_reader.read() raw_labeled, raw_nlabels = label.connected_components(depths_data) if not filter: # This is the end my friend labeled_writer.write(raw_labeled) return del depths raw_bluespot_stats = label.label_stats(depths_data, raw_labeled) keepers = filterbluespots(filter_function, cell_area, raw_bluespot_stats) new_components = label.keep_labels(raw_labeled, keepers) labeled, nlabels = label.connected_components(new_components) labeled_writer.write(labeled)
def test_connected_components(filleddata, fillednoflatsdata): diff = fillednoflatsdata - filleddata labeled, nlabels = label.connected_components(diff) assert labeled.dtype == np.int32 assert nlabels == 525 background = labeled == 0 assert np.sum(background) == 40029 # This value depend on connected_components to label the same way every time. assert np.sum(labeled) == 1561377
def test_watersheds(flowdirdata, bspotdata): speedups.disable() assert not speedups.enabled watersheds = np.copy(bspotdata) flow.watersheds_from_labels(flowdirdata, watersheds, unassigned=0) labelled_indexes = bspotdata > 0 assert np.all(watersheds[labelled_indexes] == bspotdata[labelled_indexes]) assert np.max(watersheds) == np.max(bspotdata) assert np.sum(watersheds) == 2337891 # Check that all watersheds are a connected component for lbl in range(1, np.max(watersheds) + 1): labeled, nlabels = label.connected_components(watersheds == lbl) assert nlabels == 1, "Watershed {} is not a connected component".format( lbl)