added a whole bunch of functions to Labels.jl for working with raven and avians labels

[?]
AEj8dahVWy718uSSFPe9VSRJ5qX5G8pC2zvFzJJ8yzBd
Nov 9, 2024, 1:30 AM
YODTMMPTZOUTK2OQWHDJB6D3QXZ2FJFYH2XPICCOITCK54EARC2AC

Dependencies

  • [2] YXAKJSDT added check_change_avianz_species! function to Labels.jl
  • [3] 2UBDFCJH new files tracked
  • [4] 2O6SHIVY added avianz_of_raven function to Labels.jl

Change contents

  • replacement in src/Labels.jl at line 3
    [3.15][3.15:53]()
    export aggregate_labels, audiodata_db
    [3.15]
    [3.53]
    export aggregate_labels, audiodata_db, raven_of_avianz, avianz_of_raven, label_summary
  • replacement in src/Labels.jl at line 5
    [3.54][2.9:73]()
    using CSV, DataFrames, Glob, Random, DBInterface, DuckDB, JSON3
    [3.54]
    [3.111]
    using CSV, DataFrames, Glob, Random, DBInterface, DuckDB, JSON3, Dates
  • edit in src/Labels.jl at line 159
    [3.4517]
    [3.9]
    end
    function avianz_file_of_dict(f, payload)
    isfile(f) && cp(f, "$f.backup")
    open(f, "w") do io
    JSON3.write(io, payload)
    end
  • replacement in src/Labels.jl at line 172
    [3.199][3.199:234]()
    # map(x -> avianz_of_raven!(x), a)
    [3.199]
    [3.234]
    # map(x -> avianz_of_raven(x), a)
  • replacement in src/Labels.jl at line 174
    [3.235][3.235:272]()
    function avianz_of_raven!(f::String)
    [3.235]
    [2.74]
    function avianz_of_raven(f::String)
  • replacement in src/Labels.jl at line 189
    [3.797][2.467:493](),[2.493][3.821:900](),[3.821][3.821:900]()
    avianzf = f * ".data"
    open(avianzf, "w") do io
    JSON3.write(io, data)
    println(io)
    [3.797]
    [3.900]
    basef = f = replace(f, "Table.1.selections.txt" => "")
    isfile((basef * ".WAV")) ? avianzf = basef * "WAV.data" : avianzf = basef * "wav.data"
    avianz_file_of_dict(avianzf, data)
    end
    function dict_of_avianz_file(f)
    json_string = read(f, String)
    x = JSON3.read(json_string)
    return x
    end
    function df_of_avianz_dict(data)
    df = DataFrame(
    start_time = Float64[],
    end_time = Float64[],
    low_f = Float64[],
    high_f = Float64[],
    Species = String[],
    )
    for (index1, item1) in enumerate(data[2:end])
    st, en, lf, hf = item1[1:4]
    sp = map(x -> x[:species], item1[5])
    lsp = length(sp)
    for (index2, item2) in enumerate(sp)
    push!(
    df,
    (
    start_time = st,
    end_time = en,
    low_f = lf,
    high_f = hf,
    Species = sp[index2],
    ),
    )
    end
  • replacement in src/Labels.jl at line 225
    [3.908][3.908:938]()
    #isfile(avianzf) && rm(f)
    [3.908]
    [2.494]
    return df
    end
    function prepare_df_for_raven(data)
    l = length(data.start_time)
    df = DataFrame(
    "Selection" => collect(1:l),
    "View" => ["Spectrogram 1" for x = 1:l],
    "Channel" => [1 for x = 1:l],
    "Begin Time (s)" => data.start_time,
    "End Time (s)" => data.end_time,
    "Low Freq (Hz)" => data.low_f,
    "High Freq (Hz)" => data.high_f,
    "Species" => data.Species,
    )
    return df
    end
    # using CSV, DataFrames, JSON3
    function raven_of_avianz(file::String)
    data = dict_of_avianz_file(file)
    if length(data) >= 2 #ignores empty .data files
    df = df_of_avianz_dict(data) |> prepare_df_for_raven
    outfile = replace(
    file,
    ".WAV.data" => ".Table.1.selections.txt",
    ".wav.data" => ".Table.1.selections.txt",
    )
    CSV.write(outfile, df, delim = '\t')
    end
  • edit in src/Labels.jl at line 275
    [2.1199][2.1199:1483]()
    end
    function read_avianz_data(f)
    json_string = read(f, String)
    x = JSON3.read(json_string)
    return x
    end
    function write_new_file!(f, payload)
    cp(f, "$f.backup")
    open(f, "w") do io
    JSON3.write(io, payload)
    end
  • replacement in src/Labels.jl at line 277
    [2.1512][2.1512:1547]()
    x = read_avianz_data(file)
    [2.1512]
    [2.1547]
    x = dict_of_avianz_file(file)
  • replacement in src/Labels.jl at line 282
    [2.1723][2.1723:1774]()
    write_new_file!(file, payload)
    [2.1723]
    [2.1774]
    avianz_file_of_dict(file, payload)
  • edit in src/Labels.jl at line 290
    [2.1922]
    function label_summary(folder::String, avianz = true)
    if avianz == true
    files = glob("$folder/*.['W','w']['A','a']['V','v'].data")
    df = DataFrame(
    start_time = Float64[],
    end_time = Float64[],
    low_f = Float64[],
    high_f = Float64[],
    Species = String[],
    File = String[],
    )
    for file in files
    df1 = dict_of_avianz_file(file) |> x -> df_of_avianz_dict(x)
    f = split(file, "/")[end] |> x -> replace(x, ".data" => "")
    df1.File = ["$f" for x = 1:length(df1.start_time)]
    df = vcat(df, df1)
    end
    select!(df, [:File, :start_time, :end_time, :low_f, :high_f, :Species])
    CSV.write("$folder/label_summary_avianz-$(Dates.today()).csv", df, delim = '\t')
    return df
    else
    files = glob("$folder/*.Table.1.selections.txt")
    df = DataFrame(
    File = String[],
    start_time = Float64[],
    end_time = Float64[],
    low_f = Float64[],
    high_f = Float64[],
    Species = String[],
    )
    for file in files
    df1 = DataFrame(CSV.File(file))
    for d in eachrow(df1)
    f =
    split(file, "/")[end] |>
    x -> replace(x, ".Table.1.selections.txt" => "")
    push!(
    df,
    (
    File = f,
    start_time = d."Begin Time (s)",
    end_time = d."End Time (s)",
    low_f = d."Low Freq (Hz)",
    high_f = d."High Freq (Hz)",
    Species = d."Species",
    ),
    )
    end
    end
    CSV.write("$folder/label_summary_raven-$(Dates.today()).csv", df, delim = '\t')
    return df
    end
    end