clean_metadata()
In our “Getting started” tutorial, we worked with a set of files that matched the expected metadata patterns. However, this is probably not going to be the case much of the time.
Here we’ll go over how to customize ARUtools functions to work with your data.
For example, let’s assume your files look like this, with two recordings, one at Site 100-a45 May 4th 2020 at 5:25 am with ARU unit S4A1234. The other at Site 102-b56 on the same day but at 5:40 am with ARU unit S4A1111.
f <- c(
"site100-a45/2020_05_04_05_25_00_s4a1234.wav",
"site102-b56/2020_05_04_05_40_00_s4a1111.wav"
)
If we try to clean this with the default arguments, we’re going to have some problems.
clean_metadata(project_files = f)
#> Extracting ARU info...
#> Extracting Dates and Times...
#> Identified possible problems with metadata extraction:
#> ✖ No times were successfully detected (2/2)
#> ✖ No ARU ids were successfully detected (2/2)
#> ✖ No sites were successfully detected (2/2)
#> # A tibble: 2 × 8
#> file_name type path aru_type aru_id site_id date_time date
#> <chr> <chr> <chr> <chr> <chr> <chr> <dttm> <date>
#> 1 2020_05_04… wav site… SongMet… <NA> <NA> NA 2020-05-04
#> 2 2020_05_04… wav site… SongMet… <NA> <NA> NA 2020-05-04
First let’s talk a bit about how clean_metadata()
extracts information.
This function uses regular expressions to match specific text patterns in the file path of each recording. Regular expressions are really powerful, but also reasonably complicated and can be confusing.
For example, by default, clean_metadata()
matches site
ids with the expression ((Q)|(P))(())(_|-)(()).
Yikes!
Broken down, that means look for a “Q” or “P”
(((Q)|(P))
) followed by two digits (\\d{2}
)
followed by a separator, either _
or
-
(_|-
) followed by a single digit
(\\d{1}
).
This clearly doesn’t define the sites in our example here. You can supply your own regular expression, instead.
m <- clean_metadata(project_files = f, pattern_site_id = "site\\d{3}-(a|b)\\d{2}")
#> Extracting ARU info...
#> Extracting Dates and Times...
#> Identified possible problems with metadata extraction:
#> ✖ No times were successfully detected (2/2)
#> ✖ No ARU ids were successfully detected (2/2)
m
#> # A tibble: 2 × 8
#> file_name type path aru_type aru_id site_id date_time date
#> <chr> <chr> <chr> <chr> <chr> <chr> <dttm> <date>
#> 1 2020_05_04… wav site… SongMet… <NA> site10… NA 2020-05-04
#> 2 2020_05_04… wav site… SongMet… <NA> site10… NA 2020-05-04
m$site_id
#> [1] "site100-a45" "site102-b56"
However, with sites that follow a reasonable pattern of a prefix, followed by digits and optionally a suffix with digits, it might be easier to use a helper function to create the regular expression for you.
For example, to create a site id pattern we can use
create_pattern_site_id()
.
We specify the prefix
text as well as how many digits we
might expect, a separator, suffix
text and how many suffix
digits there might be.
pat_site <- create_pattern_site_id(
prefix = "site", p_digits = 3,
sep = "-",
suffix = c("a", "b"), s_digits = 2
)
pat_site
#> [1] "((site))((\\d{3}))(-)((b)|(a))((\\d{2}))"
m <- clean_metadata(project_files = f, pattern_site_id = pat_site)
#> Extracting ARU info...
#> Extracting Dates and Times...
#> Identified possible problems with metadata extraction:
#> ✖ No times were successfully detected (2/2)
#> ✖ No ARU ids were successfully detected (2/2)
m$site_id
#> [1] "site100-a45" "site102-b56"
It can be useful to look at the default patterns in the functions to see what might be different in your data.
See ?create_pattern_date
or any
create_pattern
function to pull up the documentation and
explore the defaults as well as examples.
It can also be useful to test out a pattern before running all your files.
We can use the test_pattern()
function to see if our
pattern successfully extracts the site id from the first file in our
list.
Let’s continue customizing our metadata patterns by specifying ARU ids, dates and times.
pat_aru <- create_pattern_aru_id(arus = "s4a", n_digits = 4)
m <- clean_metadata(
project_files = f,
pattern_site_id = pat_site,
pattern_aru_id = pat_aru,
pattern_dt_sep = "_"
)
#> Extracting ARU info...
#> Extracting Dates and Times...
m
#> # A tibble: 2 × 8
#> file_name type path aru_type aru_id site_id date_time date
#> <chr> <chr> <chr> <chr> <chr> <chr> <dttm> <date>
#> 1 2020_05_04… wav site… SongMet… s4a12… site10… 2020-05-04 05:25:00 2020-05-04
#> 2 2020_05_04… wav site… SongMet… s4a11… site10… 2020-05-04 05:40:00 2020-05-04
Depending on your date formatting, you may also need to specify the order of the year, month and day, in addition to changing the pattern.
clean_metadata(
project_files = f,
pattern_dt_sep = "_",
pattern_date = create_pattern_date(order = "mdy"),
order_date = "mdy"
)
#> Extracting ARU info...
#> Extracting Dates and Times...
#> # A tibble: 2 × 8
#> file_name type path aru_type aru_id site_id date_time date
#> <chr> <chr> <chr> <chr> <chr> <chr> <dttm> <date>
#> 1 05042020_0… wav P01-… SongMet… S4A12… P01-1 2020-05-04 05:25:00 2020-05-04
#> 2 05042020_0… wav P01-… SongMet… S4A11… P01-1 2020-05-04 05:40:00 2020-05-04
Note that you need to specify it once when making the pattern, and then again when telling the function how to turn the extracted text into a date.
You can specify more than one order with
c("mdy", "ymd")
, but only do this if you know you
have multiple orders in the file names. In particular, try to avoid
using both mdy
and dmy
. Some of these dates
can be ambiguous (for example, what order is 05/05/2020?) and may not be
parsed correctly in these situations.
f <- c(
"P01-1/05042020_052500_S4A1234.wav",
"P01-1/05042020_054000_S4A1111.wav",
"Site10/2020-01-01T09:00:00_BARLT100.wav",
"Site10/2020-01-02T09:00:00_BARLT100.wav"
)
Sometimes your files may use more than one pattern. You can address this problem in one of two ways.
One option is to run clean_metadata()
twice and
then join the outputs
m1 <- clean_metadata(
project_files = f,
pattern_dt_sep = "_",
pattern_date = create_pattern_date(order = "mdy"),
order_date = "mdy"
)
#> Extracting ARU info...
#> Extracting Dates and Times...
#> Identified possible problems with metadata extraction:
#> ✖ Not all dates were successfully detected (2/4)
#> ✖ Not all times were successfully detected (2/4)
#> ✖ Not all ARU ids were successfully detected (2/4)
#> ✖ Not all sites were successfully detected (2/4)
m1 <- filter(m1, !is.na(date_time)) # omit ones that didn't work
m1
#> # A tibble: 2 × 8
#> file_name type path aru_type aru_id site_id date_time date
#> <chr> <chr> <chr> <chr> <chr> <chr> <dttm> <date>
#> 1 05042020_0… wav P01-… SongMet… S4A12… P01-1 2020-05-04 05:25:00 2020-05-04
#> 2 05042020_0… wav P01-… SongMet… S4A11… P01-1 2020-05-04 05:40:00 2020-05-04
m2 <- clean_metadata(
project_files = f,
pattern_site_id = create_pattern_site_id(prefix = "Site", s_digits = 0),
pattern_aru_id = create_pattern_aru_id(n_digits = 3)
)
#> Extracting ARU info...
#> Extracting Dates and Times...
#> Identified possible problems with metadata extraction:
#> ✖ Not all dates were successfully detected (1/4)
#> ✖ Not all times were successfully detected (2/4)
#> ✖ Not all sites were successfully detected (2/4)
m2 <- filter(m2, !is.na(date_time)) # omit ones that didn't work
m2
#> # A tibble: 2 × 8
#> file_name type path aru_type aru_id site_id date_time date
#> <chr> <chr> <chr> <chr> <chr> <chr> <dttm> <date>
#> 1 2020-01-01… wav Site… BarLT BARLT… Site10 2020-01-01 09:00:00 2020-01-01
#> 2 2020-01-02… wav Site… BarLT BARLT… Site10 2020-01-02 09:00:00 2020-01-02
m <- bind_rows(m1, m2)
m
#> # A tibble: 4 × 8
#> file_name type path aru_type aru_id site_id date_time date
#> <chr> <chr> <chr> <chr> <chr> <chr> <dttm> <date>
#> 1 05042020_0… wav P01-… SongMet… S4A12… P01-1 2020-05-04 05:25:00 2020-05-04
#> 2 05042020_0… wav P01-… SongMet… S4A11… P01-1 2020-05-04 05:40:00 2020-05-04
#> 3 2020-01-01… wav Site… BarLT BARLT… Site10 2020-01-01 09:00:00 2020-01-01
#> 4 2020-01-02… wav Site… BarLT BARLT… Site10 2020-01-02 09:00:00 2020-01-02
With this approach you should check that the number of files in the end matches the number you expect.
Another option is to supply multiple patterns to
clean_metadata()
or to the
create_pattern_XXX()
functions
m <- clean_metadata(
project_files = f,
pattern_dt_sep = c("_", "T"),
pattern_date = create_pattern_date(order = c("ymd", "mdy")),
order_date = c("ymd", "mdy"),
pattern_aru_id = create_pattern_aru_id(n_digits = c(3, 4)),
pattern_site_id = create_pattern_site_id(
prefix = c("P", "Site"),
sep = c("-", ""),
s_digits = c(1, 0)
)
)
#> Extracting ARU info...
#> Extracting Dates and Times...
m
#> # A tibble: 4 × 8
#> file_name type path aru_type aru_id site_id date_time date
#> <chr> <chr> <chr> <chr> <chr> <chr> <dttm> <date>
#> 1 05042020_0… wav P01-… SongMet… S4A12… P01-1 2020-05-04 05:25:00 2020-05-04
#> 2 05042020_0… wav P01-… SongMet… S4A11… P01-1 2020-05-04 05:40:00 2020-05-04
#> 3 2020-01-01… wav Site… BarLT BARLT… Site10 2020-01-01 09:00:00 2020-01-01
#> 4 2020-01-02… wav Site… BarLT BARLT… Site10 2020-01-02 09:00:00 2020-01-02
Which approach you should use depends on the situation.
The first approach means that the patterns being matched are more rigid. There is less of a chance of accidentally matching an incorrect pattern. However, there is a chance of omitting files that don’t match either pattern.
The second approach is more flexible in matching patterns and allows you to do so all in one step, which is convenient. However, the more flexible a pattern is, the more opportunities there are to get incorrect matches and date parsing.
With both approaches, it is important to double check the results and make sure the ids and date/times make sense.
check_meta(m)
#> # A tibble: 3 × 11
#> site_id aru_type aru_id type n_files n_dirs n_days min_date
#> <chr> <chr> <chr> <chr> <int> <int> <int> <dttm>
#> 1 P01-1 SongMeter S4A1111 wav 1 1 1 2020-05-04 05:40:00
#> 2 P01-1 SongMeter S4A1234 wav 1 1 1 2020-05-04 05:25:00
#> 3 Site10 BarLT BARLT100 wav 2 1 2 2020-01-01 09:00:00
#> # ℹ 3 more variables: max_date <dttm>, min_time <time>, max_time <time>
check_meta(m, date = TRUE)
#> # A tibble: 4 × 10
#> site_id aru_type aru_id type date n_files n_dirs n_days min_time
#> <chr> <chr> <chr> <chr> <date> <int> <int> <int> <time>
#> 1 P01-1 SongMeter S4A1111 wav 2020-05-04 1 1 1 05:40
#> 2 P01-1 SongMeter S4A1234 wav 2020-05-04 1 1 1 05:25
#> 3 Site10 BarLT BARLT100 wav 2020-01-01 1 1 1 09:00
#> 4 Site10 BarLT BARLT100 wav 2020-01-02 1 1 1 09:00
#> # ℹ 1 more variable: max_time <time>
check_problems(m)
#> # A tibble: 0 × 5
#> # ℹ 5 variables: path <chr>, aru_id <chr>, site_id <chr>, date_time <dttm>,
#> # date <date>
unique(m$site_id)
#> [1] "P01-1" "Site10"
unique(m$aru_id)
#> [1] "S4A1234" "S4A1111" "BARLT100"
You may not want to extract meta data for every file in your list or directory. Possibly this is because they’re not relevant recordings, or because you have some formatting issues that make it easier to split into separate groups first.
You can omit files using the subset
and
subset_type
arguments.
To keep only certain files, use the default
subset_type = "keep"
. To omit certain files, use
subset_type = "omit"
.
To keep only files with the “a” prefix (note that ^
means ‘at the start’)
clean_metadata(project_files = example_files, subset = "^a")
#> Extracting ARU info...
#> Extracting Dates and Times...
#> # A tibble: 14 × 8
#> file_name type path aru_type aru_id site_id date_time date
#> <chr> <chr> <chr> <chr> <chr> <chr> <dttm> <date>
#> 1 P01_1_2020… wav a_BA… BarLT BARLT… P01_1 2020-05-02 05:00:00 2020-05-02
#> 2 P01_1_2020… wav a_BA… BarLT BARLT… P01_1 2020-05-03 05:20:00 2020-05-03
#> 3 P02_1_2020… wav a_S4… SongMet… S4A01… P02_1 2020-05-04 05:25:00 2020-05-04
#> 4 P02_1_2020… wav a_S4… SongMet… S4A01… P02_1 2020-05-05 07:30:00 2020-05-05
#> # ℹ 10 more rows
To omit all files with the “a” prefix
clean_metadata(project_files = example_files, subset = "^a", subset_type = "omit")
#> Extracting ARU info...
#> Extracting Dates and Times...
#> # A tibble: 28 × 8
#> file_name type path aru_type aru_id site_id date_time date
#> <chr> <chr> <chr> <chr> <chr> <chr> <dttm> <date>
#> 1 P01_1_2020… wav j_BA… BarLT BARLT… P01_1 2020-05-02 05:00:00 2020-05-02
#> 2 P01_1_2020… wav j_BA… BarLT BARLT… P01_1 2020-05-03 05:20:00 2020-05-03
#> 3 P02_1_2020… wav j_S4… SongMet… S4A01… P02_1 2020-05-04 05:25:00 2020-05-04
#> 4 P02_1_2020… wav j_S4… SongMet… S4A01… P02_1 2020-05-05 07:30:00 2020-05-05
#> # ℹ 24 more rows
By default clean_metadata()
looks for .wav files. If you
want it to match something else, adjust the file_type
argument.
f <- c(
"a_BARLT10962_P01_1/P01_1_20200502T050000_ARU.mp4",
"a_BARLT10962_P01_1/P01_1_20200503T052000_ARU.mp4"
)
Other wise we’ll run into problems…
clean_metadata(project_files = f)
#> Error in `clean_metadata()`:
#> ! Did not find any 'wav' files.
#> ℹ Use `file_type` to change file extension for sound files
#> ℹ Check `project_dir`/`project_files` are correct
clean_metadata(project_files = f, file_type = "mp4")
#> Extracting ARU info...
#> Extracting Dates and Times...
#> # A tibble: 2 × 8
#> file_name type path aru_type aru_id site_id date_time date
#> <chr> <chr> <chr> <chr> <chr> <chr> <dttm> <date>
#> 1 P01_1_2020… mp4 a_BA… BarLT BARLT… P01_1 2020-05-02 05:00:00 2020-05-02
#> 2 P01_1_2020… mp4 a_BA… BarLT BARLT… P01_1 2020-05-03 05:20:00 2020-05-03