Confidence threshold comparison

library(dplyr)

Attaching package: 'dplyr'
The following objects are masked from 'package:stats':

    filter, lag
The following objects are masked from 'package:base':

    intersect, setdiff, setequal, union
# 1. Set your specific directory
folder_path <- "Z:/Isaac/Piglet_Detection/SAM3/Results"

# 2. Get only the 'summary' CSV files
# Pattern looks for 'summary' followed by the .csv extension
files <- list.files(path = folder_path, 
                    pattern = "_summary\\.csv$", 
                    full.names = TRUE)

# 3. Loop through and rename
for (file in files) {
  
  # Get the raw filename: "Sow1_1_detection_summary"
  raw_name <- tools::file_path_sans_ext(basename(file))
  
  # Logic to turn "Sow1_1_detection_summary" into "sow_1_1"
  # This grabs the "Sow1_1" part, adds an underscore, and makes it lowercase
  clean_name <- tolower(gsub("(Sow\\d)_(\\d).*", "\\1_\\2", raw_name))
  
  # Read and assign to the environment
  assign(clean_name, read.csv(file))
  
  message(paste("Imported", file, "as", clean_name))
}
Imported Z:/Isaac/Piglet_Detection/SAM3/Results/Sow1_1_detection_summary.csv as sow1_1
Imported Z:/Isaac/Piglet_Detection/SAM3/Results/Sow1_2_detection_summary.csv as sow1_2
Imported Z:/Isaac/Piglet_Detection/SAM3/Results/Sow1_3_detection_summary.csv as sow1_3
Imported Z:/Isaac/Piglet_Detection/SAM3/Results/Sow1_4_detection_summary.csv as sow1_4
Imported Z:/Isaac/Piglet_Detection/SAM3/Results/Sow1_5_detection_summary.csv as sow1_5
Imported Z:/Isaac/Piglet_Detection/SAM3/Results/Sow1_6_detection_summary.csv as sow1_6
Imported Z:/Isaac/Piglet_Detection/SAM3/Results/Sow1_7_detection_summary.csv as sow1_7
Imported Z:/Isaac/Piglet_Detection/SAM3/Results/Sow1_8_detection_summary.csv as sow1_8
Imported Z:/Isaac/Piglet_Detection/SAM3/Results/Sow1_9_detection_summary.csv as sow1_9
Imported Z:/Isaac/Piglet_Detection/SAM3/Results/Sow2_1_detection_summary.csv as sow2_1
Imported Z:/Isaac/Piglet_Detection/SAM3/Results/Sow2_2_detection_summary.csv as sow2_2
Imported Z:/Isaac/Piglet_Detection/SAM3/Results/Sow2_3_detection_summary.csv as sow2_3
Imported Z:/Isaac/Piglet_Detection/SAM3/Results/Sow2_4_detection_summary.csv as sow2_4
Imported Z:/Isaac/Piglet_Detection/SAM3/Results/Sow2_5_detection_summary.csv as sow2_5
Imported Z:/Isaac/Piglet_Detection/SAM3/Results/Sow2_6_detection_summary.csv as sow2_6
Imported Z:/Isaac/Piglet_Detection/SAM3/Results/Sow2_7_detection_summary.csv as sow2_7
Imported Z:/Isaac/Piglet_Detection/SAM3/Results/Sow2_8_detection_summary.csv as sow2_8
Imported Z:/Isaac/Piglet_Detection/SAM3/Results/Sow2_9_detection_summary.csv as sow2_9
# 1. Get objects starting with 'sow' followed immediately by a digit
# This matches 'sow1_1', 'sow2_9', etc.
all_sow_names <- ls(pattern = "^sow\\d")

# 2. Build the master table
final_summary_table <- do.call(rbind, lapply(all_sow_names, function(x) {
  df <- get(x)
  
  # Calculate Sums
  tp <- sum(df$TP, na.rm = TRUE)
  fp <- sum(df$FP, na.rm = TRUE)
  fn <- sum(df$FN, na.rm = TRUE)
  
  # Calculate Precision, Recall, and F1 (with checks to avoid dividing by zero)
  precision <- ifelse((tp + fp) > 0, tp / (tp + fp), 0)
  recall    <- ifelse((tp + fn) > 0, tp / (tp + fn), 0)
  f1        <- ifelse((precision + recall) > 0, 
                      2 * (precision * recall) / (precision + recall), 0)
  
  # Return as a row
  data.frame(
    Source_DF = x,
    Total_TP  = tp,
    Total_FP  = fp,
    Total_FN  = fn,
    # Precision = round(precision, 4),
    # Recall    = round(recall, 4),
    # F1_Score  = round(f1, 4),
    stringsAsFactors = FALSE
  )
}))

# 3. View the full results
View(final_summary_table)
sow1sum <- final_summary_table[1:9,]
sow2sum <- final_summary_table[10:18,]

# Calculating metrics for Sow 1
sow1sum <- sow1sum %>% 
  mutate(
    Precision = Total_TP / (Total_TP + Total_FP),
    Recall    = Total_TP / (Total_TP + Total_FN),
    F1        = 2 * ((Precision * Recall) / (Precision + Recall))
  )

# Calculating metrics for Sow 2
sow2sum <- sow2sum %>% 
  mutate(
    Precision = Total_TP / (Total_TP + Total_FP),
    Recall    = Total_TP / (Total_TP + Total_FN),
    F1        = 2 * ((Precision * Recall) / (Precision + Recall))
  )

sow1sum
  Source_DF Total_TP Total_FP Total_FN  Precision Recall        F1
1    sow1_1       78      739       22 0.09547124   0.78 0.1701200
2    sow1_2       77      184       23 0.29501916   0.77 0.4265928
3    sow1_3       71       68       29 0.51079137   0.71 0.5941423
4    sow1_4       64       20       36 0.76190476   0.64 0.6956522
5    sow1_5       56       11       44 0.83582090   0.56 0.6706587
6    sow1_6       45        7       55 0.86538462   0.45 0.5921053
7    sow1_7       30        3       70 0.90909091   0.30 0.4511278
8    sow1_8       12        3       88 0.80000000   0.12 0.2086957
9    sow1_9        0        0      100        NaN   0.00       NaN
sow2sum
   Source_DF Total_TP Total_FP Total_FN  Precision      Recall         F1
10    sow2_1       20     1136      113 0.01730104 0.150375940 0.03103181
11    sow2_2       13      174      120 0.06951872 0.097744361 0.08125000
12    sow2_3       13      172      120 0.07027027 0.097744361 0.08176101
13    sow2_4        9       83      124 0.09782609 0.067669173 0.08000000
14    sow2_5        7       38      126 0.15555556 0.052631579 0.07865169
15    sow2_6        3       16      130 0.15789474 0.022556391 0.03947368
16    sow2_7        1        6      132 0.14285714 0.007518797 0.01428571
17    sow2_8        0        1      133 0.00000000 0.000000000        NaN
18    sow2_9        0        0      133        NaN 0.000000000        NaN