geowatch.tasks.tracking.polygon_extraction module¶
- class geowatch.tasks.tracking.polygon_extraction.PolygonExtractor(heatmap_thwc, heatmap_time_intervals=None, bounds=None, classes=None, config=None)[source]¶
Bases:
object
Given a timesequence of heatmaps, extract spatially static polygons.
This class is being developed on
dev/refine-ac-polys
in the file: geowatch/tasks/tracking/polygon_extraction.py- Distributed Tweaking on:
https://colab.research.google.com/drive/1NEJpm36LviesZb45qy59myezi7JHu0bI#scrollTo=G8kHgCXSI3VS
Example
>>> from geowatch.tasks.tracking.polygon_extraction import * # NOQA >>> cls = PolygonExtractor >>> self = PolygonExtractor.demo(real_categories=True) >>> self.config['algo'] = 'crall' >>> print(f'self.heatmap_thwc.shape={self.heatmap_thwc.shape}')
>>> polys, info = self.predict_polygons(return_info=True) >>> label_mask = info['label_mask']
>>> # xdoctest: +REQUIRES(--show) >>> import kwplot >>> from geowatch.utils import util_kwimage >>> kwplot.autompl() >>> stacked = self.draw_timesequence() >>> canvas = label_mask.colorize() >>> for p in polys: >>> canvas = p.draw_on(canvas, fill=0, edgecolor='black') >>> pnum_ = kwplot.PlotNums(nSubplots=2) >>> kwplot.imshow(stacked, pnum=pnum_(), title='Colorized Time Series') >>> kwplot.imshow(canvas, pnum=pnum_(), title='Cluster Labels') >>> kwplot.show_if_requested()
- Parameters:
heatmap_thwc (ndarray) – An heatmap with dimensions [T, H, W, C] where T is number of timesteps and C are feature map reponces for each channel.
heatmap_time_intervals (None | List[Tuple[datetime, datetime]]) – A list of length T rows. Each row corresponds to a timestep in the heatmaps, and contains a tuple of two datetimes: corresponding to the start and end of times that information corresponds to. If a heatmap only corrseponds to a single time, then start and end will be the same.
bounds (kwimage.MultiPolygon | None) – if given, this is the polygon detected at BAS time.
classes (None | List[str]) – If specified, this corresponds to the class in each position of the C dimension.
config (None | Dict) – optional configuration
- predict()[source]¶
Predict the spatial polygons
- Returns:
A single [H, W] integer map indicating a cluster label for each pixel. I.e. a spatial segmentation of the sites.
- Return type:
ndarray
- classmethod demo(**kwargs)[source]¶
Create an instance of the problem on toy data.
- Parameters:
**kwargs – passed to
PolygonExtractor.demodata()
- Returns:
PolygonExtractor
- classmethod demodata(real_categories=False, rng=0)[source]¶
Create toydata to test and demo the API
- Parameters:
real_categories (bool) – if False, use fake cateogires
rng (int | None) – random seed, or None to use global seed.
- Returns:
- A dictionary that can be used as kwargs
to construct and instance of this class.
- Return type:
Dict[str, Any]
- geowatch.tasks.tracking.polygon_extraction.impute_nans(data)[source]¶
interpolate to fill nan values
TODO: tests
data = np.random.rand(5, 3, 3, 2) data[data < 0.1] = np.nan
data = np.random.rand(54, 295, 296, 5) data[data < 0.1] = np.nan
import timerit ti = timerit.Timerit(1, bestof=1, verbose=3)
- for timer in ti.reset(‘impute_nans2’):
- with timer:
impute_nans2(data)
- for timer in ti.reset(‘impute_nans’):
- with timer:
impute_nans(data)
- class geowatch.tasks.tracking.polygon_extraction.FeatureCube(heatmap_thwc, heatmap_time_intervals=None, classes=None)[source]¶
Bases:
NiceRepr
Container for a [T, H, W, C] heatmap and corresponding T-lengthed time intervals.
- class geowatch.tasks.tracking.polygon_extraction.TimeIntervalSequence(iterable=(), /)[source]¶
Bases:
list
A list of non-overlapping time intervals
- class geowatch.tasks.tracking.polygon_extraction.TimeInterval(*intervals)[source]¶
Bases:
Interval
Represents an interval in time.
Example
from geowatch.tasks.tracking.polygon_extraction import * # NOQA a = TimeInterval.coerce((‘2020-01-01’, ‘2020-02-01’)) b = TimeInterval.coerce((‘2020-02-01’, ‘2020-03-01’)) c = TimeInterval.coerce((‘2020-03-01’, ‘2020-03-15’)) d = TimeInterval.coerce((‘2020-03-07’, ‘2020-04-01’)) e = TimeInterval.coerce((‘2020-04-15’, ‘2020-05-01’))
Create a disjunction of zero, one or more intervals.
- Parameters:
intervals – zero, one or more intervals.
- property start¶
- property stop¶
- geowatch.tasks.tracking.polygon_extraction.toydata_demo()[source]¶
Example
from geowatch.tasks.tracking.polygon_extraction import * # NOQA cls = PolygonExtractor(heatmaps, bounds) self = PolygonExtractor.demo()