---
title: "Celles d'Emma"
output: 
  html_document:
    self_contained: no
    toc: true
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE, cache = FALSE, message = FALSE, warning = FALSE)
```


```{r}
# Restitution individuelle spipoll

library(dplyr)
library(stringr)
library(ggplot2)

#importer les fonctions
source("fonctions/function_import_from_mosaic.R")

query <- 'SELECT 
  sp.id AS participation_id,
  si.fr_name as taxon_final,
  si2.fr_name as taxon_depose,
  so.nbValidation,
  so.nbSuggestIdent,
  m.url
FROM spipoll_participation sp 
LEFT JOIN spipoll_observation so ON sp.id = so.participationId 
LEFT JOIN spipoll_insecte si ON so.taxonId = si.id 
INNER JOIN users u ON u.id  = sp.userId 
LEFT JOIN medias m ON m.id = so.imgTaxon1Id 
LEFT JOIN spipoll_insecte si2 ON si2.id = so.initialTaxonId
where u.username like "sbenateau"'


# envoi de la requete
user_data <- import_from_mosaic(query, database_name = "spgp")

# remove url
user_data$url <- str_replace(user_data$url, pattern = "/api/containers/spgp/download/", replacement = "https://spgp-api.65mo.fr/image/medium/")

# calcul de la diversité
res <- user_data %>%
  group_by(participation_id) %>%
  summarise(diversite = n())

# remettre les 0
empty_collections <- user_data$participation_id[is.na(user_data$taxon_final)]
res$diversite[res$participation_id %in% empty_collections] <- 0
res$numero_participation <- 1:nrow(res)

```

# Diversité moyenne observée

```{r}

ggplot(res, aes(x = numero_participation, y = diversite)) +
  geom_point()+
  geom_smooth()
```


# Diversité cumulée et observation des espèces
```{r}


cum_diversite <- numeric(nrow(res))
before = character(0)
for (i in 1:nrow(res)){
  all_diversity = na.omit(unique(user_data$taxon_final[user_data$participation_id %in% res$participation_id[1:i]]))
  cum_diversite[i] = length(all_diversity)
  new = all_diversity[!all_diversity %in% before]
  if (length(new) > 0 ){
    cat( "À la session ", i ,", vous avez découvert les espèces suivantes :\n", sep = "")
    cat(sort(new), sep = "\n") 
    cat("\n")
    before <- all_diversity
  }
}
# make data.frame

res <- data.frame(cum_diversite, numero_collection = seq_along(cum_diversite))
ggplot(res, aes(x = numero_collection, y = cum_diversite)) +
  geom_line() +
  geom_point()
```


# Identification

```{r}

# select validated data without suggestions
data_valid <- filter(user_data, (nbSuggestIdent == 0 | is.na(nbSuggestIdent)) & nbValidation == 3 & !is.na(taxon_final))

  confusion_matrix <- table(data_valid$taxon_final, data_valid$taxon_depose)
if (ncol(confusion_matrix) > 2){
  heatmap(confusion_matrix, Colv = NA, Rowv = NA)
}

```

# Taxons observés

```{r, echo=FALSE, results='asis'}

especes <- sort(table(user_data$taxon_final), decreasing = TRUE)
user_data_rm_na <- user_data[!is.na(user_data$taxon_final), ]

for (i in names(especes)){
  cat("## ", i, "\n")
  photo <- user_data_rm_na$url[user_data_rm_na$taxon_final == i]
  cat("Vous avez observé ce taxon", length(photo), "fois\n\n")
  if (length(photo) > 4 ) photo <- sample(photo, size = 4)
  for (j in 1:length(photo)){
    cat("![](",photo[j],"){#id .class width=25% }", sep = "")
  }
  cat("\n\n")
}

```



