---
title: "BigPhon Tutorial"
output: html_document
---

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

# BASwebServices

- Download and unzip demo data `ae_wavAndTxt.zip` here: <http://www.phonetik.uni-muenchen.de/~raphael/demoData/BigPhon/>
- Drag & drop files into drop zone here: <https://clarin.phonetik.uni-muenchen.de/BASWebServices/#/services/WebMAUSMultiple>
- Click "upload"
- Set language to "English - Australian" & output format to "emuDB" & output symbols to "ipa"
- Accept terms of usage
- Click "Run Web Service""
- After completion, click "Download as ZIP-File"



# EMU-SDMS

## Installation

Install package: 

```{r eval=FALSE}
install.packages("emuR")
```

and then load the library:

```{r results='hide', message=FALSE, warning=FALSE}
library("emuR")
```

# emuDB format 

For more details see:

```{r eval=FALSE}
vignette("emuDB")
```

# emuR package

For a more detailed version of this document see:

```{r eval=FALSE}
vignette("emuR_intro")
```


## Standard workflow:

1. Load database into current R session: `load_emuDB`
2. Database annotation / visual inspection: `serve` (automatically opens browser with EMU-webApp)
3. Query database: `query` (followed by `requery_hier` or `requery_seq` as necessary)
4. Get track data (e.g. formant values) for the result of a query: `get_trackdata`
5. Prepare data
6. Visually inspect data
7. Further analysis and statistical processing


## Loading and inspecting the database


```{r, results='hide', message=FALSE, warning=FALSE}
# get the path to emuDB (note that emuDBs usually have the folder suffix _emuDB
# in the near future WebMAUS multiple will also produce the a folder called MAUSOUTPUT_emuDB)
path2folder = "~/Desktop/MAUSOUTPUT/"

# load emuDB into current R session
dbHandle = load_emuDB(path2folder)
```

## Overview of emuDB

```{r}
# get summary of emuDBhandle object
summary(dbHandle)
```

## Database annotation / visual inspection

```{r eval=FALSE}
# serve emuDB to EMU-webApp
serve(dbHandle)
```

## Use cases


### 1.) *What is the average length of all 'n' phonetic segments in the emuDB?*

```{r message=FALSE, warning=FALSE}
sl = query(dbHandle, query = "phonetic==n")
# show head of resulting segment list
head(sl)
```

```{r}
# calculate durations
d = dur(sl)

# calculate mean and by doing so answering the question
mean(d)
```

## 2.) *What does the F1/F2 distribution of all phonetic segments containing labels ə, oː, ɪ, u:, e look like?*


```{r results='hide', message=FALSE, warning=FALSE}
# query emuDB
sl = query(dbHandle, query = "phonetic == ə|oː|ɪ|u:|e")
```

```{r results='hide', message=FALSE, warning=FALSE}
# get formant values for those segments
td = get_trackdata(dbHandle, sl,
                   onTheFlyFunctionName = "forest",
                   resultType = "emuRtrackdata")
```

```{r}
class(td)
```

```{r, fig.height = 5, fig.width = 5}
# load package
library(ggplot2)

# scatter plot of F1 and F2 values using ggplot
ggplot(td, aes(x=T2, y=T1, label=td$labels)) +
  geom_text(aes(colour=factor(labels))) +
  scale_y_reverse() + scale_x_reverse() +
  labs(x = "F2(Hz)", y = "F1(Hz)") +
  guides(colour=FALSE)
```

## 3.) In which words do phonetic segments s, z or ʃ occur, and what is the preceding phonetic context?

```{r}
sibil = query(dbHandle, "phonetic==s|z|ʃ|ʒ")

head(sibil)
```

```{r}
words = requery_hier(dbHandle, sibil, level = "word")

head(words)
```

```{r}
# get left context by off-setting the annotational units in sibil one unit to the left
leftContext = requery_seq(dbHandle, sibil, offset = -1)

head(leftContext)
```

## And the following context:

```{r eval=FALSE}
# get right context by off-setting the annotational units in sibil one unit to the right
rightContext = requery_seq(dbHandle, sibil, offset = 1)
```

## 4.) Do phonetic segments s, z and ʃ differ with respect to their first spectral moments?


```{r}
sibil = query(dbHandle,"phonetic =~ '[sz ʃ]'")
```

```{r results='hide', message=FALSE, warning=FALSE}
dftTd = get_trackdata(dbHandle,
                      seglist = sibil,
                      onTheFlyFunctionName = 'dftSpectrum')
```

```{r}
dftTdRelFreq = dftTd[, 1000:10000]
```

Now we are ready to calculate the spectral moments from the reduced spectra:

```{r}
dftTdRelFreqMom = fapply(dftTdRelFreq, moments, minval = T)
```

The resulting `dftTdRelFreqMom` object is once again a trackdata object of the same length as the previous one. It contains the first four spectral moments:

```{r}
dftTdRelFreqMom[1]
```

We can now use the information stored in the `dftTdRelFreqMom` and `sibil` objects to plot
by-phonetic-category ensembles and time normalized versions of the first spectral moments using `emuR`'s `dplot()` function:

```{r fig.height = 5, fig.width = 5}
dplot(dftTdRelFreqMom[, 1],
      sibil$labels,
      normalise = TRUE,
      xlab = "Normalized Time [%]",
      ylab = "1st spectral moment [Hz]")
```


Alternatively, we can average the ensembles into single trajectories by setting the `average` parameter of `dplot()` to `TRUE`:

```{r fig.height = 5, fig.width = 5}
dplot(dftTdRelFreqMom[,1],
      sibil$labels,
      normalise = TRUE,
      average = TRUE,
      xlab = "Normalized Time [%]",
      ylab = "1st spectral moment [Hz]")
```


As can be seen from the previous two plots, transitions to and from a sort of "steady state" around the temporal
midpoint of the sibilants are clearly visible. To focus on this "steady state" part of the sibilant we
will not cut out the middle 60% of the previously calculated moments using the `dcut()`
function:

```{r}
# cut out the middle 60%
dftTdRelFreqMomMid = dcut(dftTdRelFreqMom,
                          left.time = 0.2,
                          right.time = 0.8,
                          prop = T)

# display original moments of the first segment
dftTdRelFreqMom[1]

# display central 60% of moments of the first segment
dftTdRelFreqMomMid[1]
```

To wrap up, let's calculate the averages of these trajectories using the `trapply` function:

```{r}
meanFirstMoments = trapply(dftTdRelFreqMomMid[,1],
                           fun = mean,
                           simplify = T)

# display resulting vector
meanFirstMoments
```

As the resulting `meanFirstMoments` vector has the same length as the initial `sibil` segment list,
we can now easily visualize these values in the form of a boxplot:

```{r fig.height = 5, fig.width = 5}
boxplot(meanFirstMoments ~ sibil$labels)
```
