=#
#=
For Kahurangi Data
a=glob("*/*/*.csv")
b=glob("*/*/*/*.csv")
c=glob("*/*/*/*/*.csv")
list=[a ; b ; c]
# to delete empty preds.csv files
for file in list
size = stat(file).size
if size < 10
println("Deleting $file - $size")
rm(file)
end
end
## change date on preds below (no longer required at 25/10/24)
function make_clips_kahurangi(preds_path::String, label::Int = 1)
# Assumes function run from Kahurangi Data
#pth = replace(preds_path, "preds-2024-10-21.csv" => "")
pth = split(preds_path, "/") |> x -> joinpath(x[1:end-1]) |> x -> x * "/"
function assert_detections_present_(df::DataFrame, label::Int, preds_path)::DataFrame
label in levels(df.label) ? (return df) :
@error "No detections for label = $label at $preds_path"
end
# Load and group data frame by file
gdf =
#! format: off
DataFrame(CSV.File(preds_path)) |>
x -> Skraak.assert_not_empty(x, preds_path) |>
x -> Skraak.rename_column!(x, "1.0", "label") |> #can remove now, needs to be label
x -> assert_detections_present_(x, label, preds_path) |>
x -> Skraak.filter_positives!(x, label) |>
Skraak.group_by_file!
#! format: on
# Make clip and spectrogram
for (k, v) in pairs(gdf)
#file_name = chop(v.file[1], head = 2, tail = 4)
file_name, extension = Skraak.path_to_file_string(v.file[1])
start_times = v[!, :start_time] |> sort
detections = Skraak.cluster_detections(start_times)
isempty(detections) && continue
signal, freq = Skraak.wavread("$pth$(file_name).$(extension)")
if freq > 16000
signal, freq = Skraak.resample_to_8000hz(signal, freq)
end
freq = freq |> Float32
length_signal = length(signal)
for detection in detections
st, en = Skraak.calculate_clip_start_end(detection, freq, length_signal)
p=chop(pth, tail=1) |> x -> replace(x, "/" => "---") #delete trailing / then replace / with ---
#name = "$p-$file_name-$(Int(floor(st/freq)))-$(Int(ceil(en/freq)))"
name = "$file_name-$(Int(floor(st/freq)))-$(Int(ceil(en/freq)))" #leave off path, not necesaray
##name = "$location-$h-$trip_date-$file_name-$(Int(floor(st/freq)))-$(Int(ceil(en/freq)))"
f = "Clips_$(today())"
mkpath(f)
outfile = "$f/$name"
sample = signal[Int(st):Int(en)]
Skraak.wavwrite(sample, "$outfile.wav", Fs = Int(freq))
image = Skraak.get_image_from_sample(sample, freq)
PNGFiles.save("$outfile.png", image)
end
print(".")
end
print(".")
end
using Glob, Skraak, CSV, DataFrames, Dates, PNGFiles
a=glob("*/*/preds-2024-08-29.csv")
b=glob("*/*/*/preds-2024-08-29.csv")
c=glob("*/*/*/*/preds-2024-08-29.csv")
predictions = [a ; b ; c]
## or
predictions = glob("*/*/preds-2024-10-21.csv")
for file in predictions
try
make_clips_kahurangi(file)
catch x
println(x)
end
end