def test_spatiotemporalanalysis_dataframes(self): # test with the default constructor sta = pls.SpatioTemporalAnalysis(self.landscape_fps) # test that `class_metrics_df` and `landscape_metrics_df` are well # constructed class_metrics_df = sta.class_metrics_df self.assertTrue( np.all(class_metrics_df.index == pd.MultiIndex.from_product( [sta.classes, sta.dates]))) landscape_metrics_df = sta.landscape_metrics_df self.assertTrue(np.all(landscape_metrics_df.index == sta.dates)) # now test the same but with an analysis that only considers a # subset of metrics and a subset of classes sta_metrics = ['total_area', 'edge_density', 'proportion_of_landscape'] sta_classes = sta.classes[:2] sta = pls.SpatioTemporalAnalysis(self.landscape_fps, metrics=sta_metrics, classes=sta_classes, dates=self.dates) class_metrics_df = sta.class_metrics_df self.assertTrue( np.all(class_metrics_df.index == pd.MultiIndex.from_product( [sta_classes, self.dates]))) landscape_metrics_df = sta.landscape_metrics_df self.assertTrue(np.all(landscape_metrics_df.index == self.dates))
def test_spatiotemporalanalysis_dataframes(self): # test with the default constructor sta = pls.SpatioTemporalAnalysis(self.landscape_fps) # test that the data frames that result from `compute_class_metrics_df` # and `compute_landscape_metrics_df` are well constructed class_metrics_df = sta.compute_class_metrics_df() self.assertTrue( np.all(class_metrics_df.index == pd.MultiIndex.from_product( [sta.present_classes, sta.dates]))) landscape_metrics_df = sta.compute_landscape_metrics_df() self.assertTrue(np.all(landscape_metrics_df.index == sta.dates)) # now test the same but with an analysis that only considers a # subset of metrics and a subset of classes metrics = ['total_area', 'edge_density', 'proportion_of_landscape'] classes = sta.present_classes[:2] class_metrics_df = sta.compute_class_metrics_df(metrics=metrics, classes=classes) self.assertTrue( np.all(class_metrics_df.index == pd.MultiIndex.from_product( [classes, sta.dates]))) # 'proportion_of_landscape' cannot be computed at the landscape level # (TODO: test for that elsewhere) landscape_metrics = metrics[:2] landscape_metrics_df = sta.compute_landscape_metrics_df( metrics=landscape_metrics) self.assertTrue(np.all(landscape_metrics_df.index == sta.dates))
def test_spatiotemporalanalysis_init(self): # test that the `feature_name` is dates, and that if the `dates` # argument is not provided when instantiating a # `SpatioTemporalAnalysis`, the dates attribute is properly and # automatically generated sta = pls.SpatioTemporalAnalysis(self.landscape_fps) self.assertEqual(sta.feature_name, 'dates') self.assertEqual(len(sta), len(sta.dates))
def test_spatiotemporalanalysis_plot_metrics(self): sta = pls.SpatioTemporalAnalysis(self.landscape_fps, dates=self.dates) # test for `None` (landscape-level) and an existing class (class-level) for class_val in [None, sta.classes[0]]: # test that the x data of the line corresponds to the dates self.assertTrue( np.all( sta.plot_metric('patch_density', class_val=class_val) .lines[0].get_xdata() == self.dates))
def main(urban_extracts_dir, out_figure_filepath, metrics, clc_basenames, agglomeration_slugs): logger = logging.getLogger(__name__) URBAN_CLASS_VAL = 1 num_rows = len(agglomeration_slugs) num_cols = len(metrics) figwidth, figlength = plt.rcParams['figure.figsize'] fig, axes = plt.subplots( num_rows, num_cols, sharex=True, figsize=(figwidth * num_cols, figlength * num_rows)) # extract the year code from the CLC basename, e.g., `00` from # `g100_clc12_V18_5` dates = [clc_basename[8:10] for clc_basename in clc_basenames] for i, agglomeration_slug in enumerate(agglomeration_slugs): logger.info(f'computing landscape metrics for {agglomeration_slug}') sta = pls.SpatioTemporalAnalysis( [ path.join(urban_extracts_dir, f'{agglomeration_slug}-{clc_basename}.tif') for clc_basename in clc_basenames ], metrics=metrics, classes=[URBAN_CLASS_VAL], dates=dates) for j, metric in enumerate(metrics): sta.plot_metric( metric, class_val=URBAN_CLASS_VAL, ax=axes[i, j], metric_legend=False) for i, agglomeration_slug in enumerate(agglomeration_slugs): axes[i, 0].set_ylabel(agglomeration_slug.title()) for j, metric in enumerate(metrics): axes[0, j].set_title(metric) logger.info(f'saving figure to {out_figure_filepath}') fig.savefig(out_figure_filepath)