Skip to content

Commit

Permalink
Add random forest models to report
Browse files Browse the repository at this point in the history
  • Loading branch information
jakobdanel committed Jan 29, 2024
1 parent 034b9c7 commit af147f4
Show file tree
Hide file tree
Showing 6 changed files with 349 additions and 0 deletions.
101 changes: 101 additions & 0 deletions results/results/rf/combined.qmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@

### Segmentation

```{r}
#| code-fold: true
#| warning: false
#| results: hide
chms <- lfa::lfa_visit_all_areas(lfa::lfa_chm)
patches <- lfa::lfa_get_all_areas()
patches$chm_mean = NA
patches$chm_var = NA
patches$chm_median = NA
for (area_key in names(chms)) {
area <- chms[area_key]
area[[area_key]] |> as.vector() -> vec
patches[patches$area == area_key, "chm_mean"] <-
mean(vec, na.rm = T)
patches[patches$area == area_key, "chm_var"] <-
var(vec, na.rm = T)
patches[patches$area == area_key, "chm_median"] <-
median(vec, na.rm = T)
}
density <- lfa::lfa_calculate_patch_density(detections = detections)
```


```{r}
#| code-fold: true
#| warning: false
#| results: hide
data <- sf::st_read("data/tree_properties.gpkg")
detections <- lfa::lfa_get_detections()
neighbors <- lfa::lfa_get_neighbor_paths() |> lfa::lfa_combine_sf_obj(lfa::lfa_get_all_areas())
```


```{r, cache=TRUE}
#| code-fold: true
#| warning: false
#| results: hide
combined <- sf::st_join(data,detections,join = sf::st_within)
combined$Z.x = NULL
names(combined)[names(combined) == 'Z.y'] <- 'Z'
combined$treeID.segmentation <- NULL
combined[["density"]][is.na(combined[["density"]])] <- -1
combined[["Z.mean"]][is.na(combined[["Z.mean"]])] <- -1
combined[["Z.var"]][is.na(combined[["Z.var"]])] <- -1
combined[["Intensity.mean"]][is.na(combined[["Intensity.mean"]])] <- -1
combined[["Intensity.var"]][is.na(combined[["Intensity.var"]])] <- -1
combined[["number_of_returns"]][is.na(combined[["number_of_returns"]])] <- -1
combined[["tree_area"]][is.na(combined[["tree_area"]])] <- -1
neighbors$treeID = NULL
neighbors$Z = NULL
neighbors$area = NULL
neighbors$specie = NULL
combined = sf::st_join(combined, neighbors, sf::st_within)
combined = dplyr::left_join(combined,patches, c("specie","area"))
density <- density |> as.data.frame()
density$id = NULL
density$geometry = NULL
density$area_size = NULL
density$detections = NULL
colnames(density) = c("specie","area","density")
combined <- dplyr::left_join(combined, density, c("specie","area"))
excluded_cols <- c("Z.x", "treeID.detection","treeID.segmentation","name_las_file","treeID","area","specie","geom")
```



```{r, cache=TRUE}
#| code-fold: true
#| warning: false
#| results: hide
data <- lfa::lfa_random_forest(tree_data = combined, excluded_input_columns = excluded_cols,response_variable = "specie")
```

```{r}
#| code-fold: true
#| warning: false
#| fig-cap: Confusion Matrix of randomForest with all parameters derived from tree level.
#| label: fig-cm-tree-level
cm <- data$confusion_matrix |> caret::confusionMatrix()
lfa::lfa_plot_confusion_matrix(cm)
```

```{r}
#| code-fold: true
#| warning: false
#| fig-cap: Precsion and Recall of randomForest with all parameters derived from tree level.
#| label: fig-pr-tree-level
data$confusion_matrix |> lfa::lfa_calculate_rf_metrics() |> lfa::lfa_visualize_rf_metrics()
```

26 changes: 26 additions & 0 deletions results/results/rf/height.qmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
## Predicting species with random forest

### Use height

```{r}
detections <- lfa::lfa_get_detections()
density <- lfa::lfa_calculate_patch_density(detections = detections)
colnames(density) <- c("id","specie","area","geometry","area_size","detections","density")
detections <- dplyr::left_join(detections,density |> as.data.frame(), by= c("area","specie"))
excluded_cols <- c("treeID","geom","area","specie","id","geometry","area_size","detections", "density")
```

```{r, cache=TRUE}
data <- lfa::lfa_random_forest(tree_data = detections, excluded_input_columns = excluded_cols)
```

```{r}
cm <- data$confusion_matrix |> caret::confusionMatrix()
lfa::lfa_plot_confusion_matrix(cm)
```

```{r}
data$confusion_matrix |> lfa::lfa_calculate_rf_metrics() |> lfa::lfa_visualize_rf_metrics()
```

39 changes: 39 additions & 0 deletions results/results/rf/neighbors.qmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@

### Use neighbors and height

```{r}
#| code-fold: true
#| warning: false
#| results: hide
detections <- lfa::lfa_get_detections()
neighbors <- lfa::lfa_get_neighbor_paths() |> lfa::lfa_combine_sf_obj(lfa::lfa_get_all_areas())
neighbors <- sf::st_join(neighbors,detections, join = sf::st_within)
names(neighbors)[names(neighbors) == 'specie.x'] <- 'specie'
names(neighbors)[names(neighbors) == 'area.x'] <- 'area'
excluded_cols <- c("area.x","specie.x","treeID.y","Z.y","area.y","specie.y","geom","treeID.x","Z.x")
```

```{r, cache=TRUE}
#| code-fold: true
#| warning: false
#| results: hide
data <- lfa::lfa_random_forest(tree_data = neighbors, excluded_input_columns = excluded_cols,response_variable = "specie")
```

```{r}
#| code-fold: true
#| warning: false
#| label: fig-cm-neighbors
#| fig-cap: Confusion Matrix of randomForest on the distance to 100 nearest neighbors.
cm <- data$confusion_matrix |> caret::confusionMatrix()
lfa::lfa_plot_confusion_matrix(cm)
```

```{r}
#| code-fold: true
#| warning: false
#| label: fig-pr-neighbors
#| fig-cap: Class wise precision and recall for randomForest-Classification with distance to the 100 nearest neighbors.
data$confusion_matrix |> lfa::lfa_calculate_rf_metrics() |> lfa::lfa_visualize_rf_metrics()
```

84 changes: 84 additions & 0 deletions results/results/rf/patch-level.qmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@

### Train with patch level information

```{r}
#| code-fold: true
#| warning: false
#| results: hide
chms <- lfa::lfa_visit_all_areas(lfa::lfa_chm)
patches <- lfa::lfa_get_all_areas()
patches$chm_mean = NA
patches$chm_var = NA
patches$chm_median = NA
for (area_key in names(chms)) {
area <- chms[area_key]
area[[area_key]] |> as.vector() -> vec
patches[patches$area == area_key, "chm_mean"] <-
mean(vec, na.rm = T)
patches[patches$area == area_key, "chm_var"] <-
var(vec, na.rm = T)
patches[patches$area == area_key, "chm_median"] <-
median(vec, na.rm = T)
}
```

```{r}
#| code-fold: true
#| warning: false
#| results: hide
neighbors <- lfa::lfa_get_neighbor_paths() |> lfa::lfa_combine_sf_obj(lfa::lfa_get_all_areas())
```


```{r}
#| code-fold: true
#| warning: false
#| results: hide
detections <- lfa::lfa_get_detections()
density <- lfa::lfa_calculate_patch_density(detections = detections)
colnames(density) <- c("id","specie","area","geometry","area_size","detections","density")
detections <- dplyr::left_join(detections,density |> as.data.frame(),by=c("area","specie"))
detections <- dplyr::left_join(detections,patches, by = c("area","specie"))
detections <- sf::st_join(detections, neighbors, join = sf::st_within)
detections$treeID.x = NULL
names(detections)[names(detections) == 'treeID.y'] <- 'treeID'
detections$Z.x = NULL
names(detections)[names(detections) == 'Z.y'] <- 'Z'
detections$area.x = NULL
names(detections)[names(detections) == 'area.y'] <- 'area'
detections$specie.x = NULL
names(detections)[names(detections) == 'specie.y'] <- 'specie'
excluded_cols = c("treeID","geom","area","specie","id","geometry","area_size","detections","geometry")
```

```{r, cache=TRUE}
#| code-fold: true
#| warning: false
#| results: hide
data <- lfa::lfa_random_forest(tree_data = detections, excluded_input_columns = excluded_cols,response_variable = "specie")
```

```{r}
#| code-fold: true
#| warning: false
#| label: fig-cm-patch
#| fig-cap: Confusion Matrix of randomForest on returns per Tree.
cm <- data$confusion_matrix |> caret::confusionMatrix()
lfa::lfa_plot_confusion_matrix(cm)
```

```{r}
#| code-fold: true
#| warning: false
#| label: fig-pr-patch
#| fig-cap: Class wise precision and recall for randomForest-Classification with LiDAR returns per tree.
data$confusion_matrix |> lfa::lfa_calculate_rf_metrics() |> lfa::lfa_visualize_rf_metrics()
```

35 changes: 35 additions & 0 deletions results/results/rf/returns_per_tree.qmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@

### LiDAR point cloud returns per Tree

```{r}
#| code-fold: true
#| warning: false
#| results: hide
returns <- lfa::lfa_count_returns_all_areas()
excluded_cols = c("Var1","specie","area")
```

```{r, cache=TRUE}
#| code-fold: true
#| warning: false
#| results: hide
data <- lfa::lfa_random_forest(tree_data = returns, excluded_input_columns = excluded_cols,response_variable = "specie")
```

```{r}
#| code-fold: true
#| warning: false
#| label: fig-cm-returns
#| fig-cap: Confusion Matrix of randomForest on returns per Tree.
cm <- data$confusion_matrix |> caret::confusionMatrix()
lfa::lfa_plot_confusion_matrix(cm)
```

```{r}
#| code-fold: true
#| warning: false
#| label: fig-pr-returns
#| fig-cap: Class wise precision and recall for randomForest-Classification with LiDAR returns per tree.
data$confusion_matrix |> lfa::lfa_calculate_rf_metrics() |> lfa::lfa_visualize_rf_metrics()
```

64 changes: 64 additions & 0 deletions results/results/rf/segmentation.qmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@

### Segmentation

```{r}
#| code-fold: true
#| warning: false
#| results: hide
data <- sf::st_read("data/tree_properties.gpkg")
detections <- lfa::lfa_get_detections()
neighbors <- lfa::lfa_get_neighbor_paths() |> lfa::lfa_combine_sf_obj(lfa::lfa_get_all_areas())
```
```{r}
#| code-fold: true
#| warning: false
#| results: hide
combined <- sf::st_join(data,detections,join = sf::st_within)
combined$Z.x = NULL
names(combined)[names(combined) == 'Z.y'] <- 'Z'
combined$treeID.segmentation <- NULL
combined[["density"]][is.na(combined[["density"]])] <- -1
combined[["Z.mean"]][is.na(combined[["Z.mean"]])] <- -1
combined[["Z.var"]][is.na(combined[["Z.var"]])] <- -1
combined[["Intensity.mean"]][is.na(combined[["Intensity.mean"]])] <- -1
combined[["Intensity.var"]][is.na(combined[["Intensity.var"]])] <- -1
combined[["number_of_returns"]][is.na(combined[["number_of_returns"]])] <- -1
combined[["tree_area"]][is.na(combined[["tree_area"]])] <- -1
neighbors$treeID = NULL
neighbors$Z = NULL
neighbors$area = NULL
neighbors$specie = NULL
combined = sf::st_join(combined, neighbors, sf::st_within)
excluded_cols <- c("Z.x", "treeID.detection","treeID.segmentation","name_las_file","treeID","area","specie","geom")
```

```{r, cache=TRUE}
#| code-fold: true
#| warning: false
#| results: hide
data <- lfa::lfa_random_forest(tree_data = combined, excluded_input_columns = excluded_cols,response_variable = "specie")
```

```{r}
#| code-fold: true
#| warning: false
#| fig-cap: Confusion Matrix of randomForest with all parameters derived from tree level.
#| label: fig-cm-tree-level
cm <- data$confusion_matrix |> caret::confusionMatrix()
lfa::lfa_plot_confusion_matrix(cm)
```

```{r}
#| code-fold: true
#| warning: false
#| fig-cap: Precsion and Recall of randomForest with all parameters derived from tree level.
#| label: fig-pr-tree-level
data$confusion_matrix |> lfa::lfa_calculate_rf_metrics() |> lfa::lfa_visualize_rf_metrics()
```

0 comments on commit af147f4

Please sign in to comment.