src/Stats.jl

Thu, 25 Apr 2024 14:54:34 -0500

author
Tuomo Valkonen <tuomov@iki.fi>
date
Thu, 25 Apr 2024 14:54:34 -0500
changeset 42
ddcf355f0b2d
parent 41
3e54b2b54029
child 44
7fb55885d85f
permissions
-rw-r--r--

Oops

34
aca9c90f151c Reduce dependencies; separate Stats from PlotResults (disabled by default to avoid heavy deps)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
1 __precompile__()
aca9c90f151c Reduce dependencies; separate Stats from PlotResults (disabled by default to avoid heavy deps)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
2
aca9c90f151c Reduce dependencies; separate Stats from PlotResults (disabled by default to avoid heavy deps)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
3 module Stats
aca9c90f151c Reduce dependencies; separate Stats from PlotResults (disabled by default to avoid heavy deps)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
4
aca9c90f151c Reduce dependencies; separate Stats from PlotResults (disabled by default to avoid heavy deps)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
5
aca9c90f151c Reduce dependencies; separate Stats from PlotResults (disabled by default to avoid heavy deps)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
6 ########################
aca9c90f151c Reduce dependencies; separate Stats from PlotResults (disabled by default to avoid heavy deps)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
7 # Load external modules
aca9c90f151c Reduce dependencies; separate Stats from PlotResults (disabled by default to avoid heavy deps)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
8 ########################
aca9c90f151c Reduce dependencies; separate Stats from PlotResults (disabled by default to avoid heavy deps)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
9
aca9c90f151c Reduce dependencies; separate Stats from PlotResults (disabled by default to avoid heavy deps)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
10 using CSV, DataFrames
aca9c90f151c Reduce dependencies; separate Stats from PlotResults (disabled by default to avoid heavy deps)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
11 using Statistics
aca9c90f151c Reduce dependencies; separate Stats from PlotResults (disabled by default to avoid heavy deps)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
12
41
Tuomo Valkonen <tuomov@iki.fi>
parents: 34
diff changeset
13 export calculate_statistics,
Tuomo Valkonen <tuomov@iki.fi>
parents: 34
diff changeset
14 extract_parameters
Tuomo Valkonen <tuomov@iki.fi>
parents: 34
diff changeset
15
Tuomo Valkonen <tuomov@iki.fi>
parents: 34
diff changeset
16 function extract_parameters(filename :: String)
Tuomo Valkonen <tuomov@iki.fi>
parents: 34
diff changeset
17 # Extracting parameters
Tuomo Valkonen <tuomov@iki.fi>
parents: 34
diff changeset
18 params_line = readlines(filename)[1]
Tuomo Valkonen <tuomov@iki.fi>
parents: 34
diff changeset
19
Tuomo Valkonen <tuomov@iki.fi>
parents: 34
diff changeset
20 # Split the line by commas and trim each part
Tuomo Valkonen <tuomov@iki.fi>
parents: 34
diff changeset
21 params_parts = map(strip, split(params_line, ','))
Tuomo Valkonen <tuomov@iki.fi>
parents: 34
diff changeset
22
Tuomo Valkonen <tuomov@iki.fi>
parents: 34
diff changeset
23 # Initialize variables to store parameter values
Tuomo Valkonen <tuomov@iki.fi>
parents: 34
diff changeset
24 α_value, τ₀_value, σ₀_value = missing, missing, missing
Tuomo Valkonen <tuomov@iki.fi>
parents: 34
diff changeset
25
Tuomo Valkonen <tuomov@iki.fi>
parents: 34
diff changeset
26 # Look for specific substrings to identify the values of α, τ₀, and σ₀
Tuomo Valkonen <tuomov@iki.fi>
parents: 34
diff changeset
27 for param_part in params_parts
Tuomo Valkonen <tuomov@iki.fi>
parents: 34
diff changeset
28 if contains(param_part, "α = ")
Tuomo Valkonen <tuomov@iki.fi>
parents: 34
diff changeset
29 α_value = parse(Float64, split(param_part, '=')[2])
Tuomo Valkonen <tuomov@iki.fi>
parents: 34
diff changeset
30 elseif contains(param_part, "τ₀ = ")
Tuomo Valkonen <tuomov@iki.fi>
parents: 34
diff changeset
31 τ₀_value = parse(Float64, split(param_part, '=')[2])
Tuomo Valkonen <tuomov@iki.fi>
parents: 34
diff changeset
32 elseif contains(param_part, "σ₀ = ")
Tuomo Valkonen <tuomov@iki.fi>
parents: 34
diff changeset
33 σ₀_value = parse(Float64, split(param_part, '=')[2])
Tuomo Valkonen <tuomov@iki.fi>
parents: 34
diff changeset
34 end
Tuomo Valkonen <tuomov@iki.fi>
parents: 34
diff changeset
35 end
Tuomo Valkonen <tuomov@iki.fi>
parents: 34
diff changeset
36
Tuomo Valkonen <tuomov@iki.fi>
parents: 34
diff changeset
37 # Assign the values to α, τ₀, and σ₀
Tuomo Valkonen <tuomov@iki.fi>
parents: 34
diff changeset
38 α = α_value
Tuomo Valkonen <tuomov@iki.fi>
parents: 34
diff changeset
39 τ₀ = τ₀_value
Tuomo Valkonen <tuomov@iki.fi>
parents: 34
diff changeset
40 σ₀ = σ₀_value
Tuomo Valkonen <tuomov@iki.fi>
parents: 34
diff changeset
41 return α, τ₀, σ₀
Tuomo Valkonen <tuomov@iki.fi>
parents: 34
diff changeset
42 end
34
aca9c90f151c Reduce dependencies; separate Stats from PlotResults (disabled by default to avoid heavy deps)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
43
aca9c90f151c Reduce dependencies; separate Stats from PlotResults (disabled by default to avoid heavy deps)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
44 function calculate_statistics()
aca9c90f151c Reduce dependencies; separate Stats from PlotResults (disabled by default to avoid heavy deps)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
45 ImName = ("lighthouse200x300", "shepplogan256x256", "brainphantom256x256")
aca9c90f151c Reduce dependencies; separate Stats from PlotResults (disabled by default to avoid heavy deps)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
46 AlgName = ("dualscaling", "greedy","noprediction","primalonly","proximal","rotation","zerodual")
aca9c90f151c Reduce dependencies; separate Stats from PlotResults (disabled by default to avoid heavy deps)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
47 mystart = 41 # Corresponds to the 500th iterate
aca9c90f151c Reduce dependencies; separate Stats from PlotResults (disabled by default to avoid heavy deps)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
48
aca9c90f151c Reduce dependencies; separate Stats from PlotResults (disabled by default to avoid heavy deps)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
49 # Define an array to store results
aca9c90f151c Reduce dependencies; separate Stats from PlotResults (disabled by default to avoid heavy deps)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
50 results = DataFrame(experiment = String[], α = Float64[], algorithm = String[], psnr_mean1 = Float64[], psnr_mean500 = Float64[], psnr_ci = String[], ssim_mean1 = Float64[], ssim_mean500 = Float64[], ssim_ci = String[])
aca9c90f151c Reduce dependencies; separate Stats from PlotResults (disabled by default to avoid heavy deps)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
51
aca9c90f151c Reduce dependencies; separate Stats from PlotResults (disabled by default to avoid heavy deps)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
52 for imname in ImName
aca9c90f151c Reduce dependencies; separate Stats from PlotResults (disabled by default to avoid heavy deps)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
53 for algname in AlgName
aca9c90f151c Reduce dependencies; separate Stats from PlotResults (disabled by default to avoid heavy deps)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
54 directory_path = "./img/"
aca9c90f151c Reduce dependencies; separate Stats from PlotResults (disabled by default to avoid heavy deps)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
55 files = readdir(directory_path)
aca9c90f151c Reduce dependencies; separate Stats from PlotResults (disabled by default to avoid heavy deps)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
56 filtered_files = filter(file -> startswith(file, "$(imname)_pdps_known_$(algname)") && endswith(file, ".txt"), files)
aca9c90f151c Reduce dependencies; separate Stats from PlotResults (disabled by default to avoid heavy deps)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
57
aca9c90f151c Reduce dependencies; separate Stats from PlotResults (disabled by default to avoid heavy deps)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
58 for file in filtered_files
aca9c90f151c Reduce dependencies; separate Stats from PlotResults (disabled by default to avoid heavy deps)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
59 filename = directory_path * file
aca9c90f151c Reduce dependencies; separate Stats from PlotResults (disabled by default to avoid heavy deps)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
60 data = CSV.File(filename, delim='\t', header=2) |> DataFrame
aca9c90f151c Reduce dependencies; separate Stats from PlotResults (disabled by default to avoid heavy deps)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
61
aca9c90f151c Reduce dependencies; separate Stats from PlotResults (disabled by default to avoid heavy deps)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
62 # Extract α from filename
aca9c90f151c Reduce dependencies; separate Stats from PlotResults (disabled by default to avoid heavy deps)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
63 α, _, _ = extract_parameters(filename)
aca9c90f151c Reduce dependencies; separate Stats from PlotResults (disabled by default to avoid heavy deps)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
64
aca9c90f151c Reduce dependencies; separate Stats from PlotResults (disabled by default to avoid heavy deps)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
65
aca9c90f151c Reduce dependencies; separate Stats from PlotResults (disabled by default to avoid heavy deps)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
66 # Extract SSIM and PSNR columns starting from 1st iteration
aca9c90f151c Reduce dependencies; separate Stats from PlotResults (disabled by default to avoid heavy deps)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
67 ssim_values1 = Float64.(data[:, :ssim])
aca9c90f151c Reduce dependencies; separate Stats from PlotResults (disabled by default to avoid heavy deps)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
68 psnr_values1 = Float64.(data[:, :psnr])
aca9c90f151c Reduce dependencies; separate Stats from PlotResults (disabled by default to avoid heavy deps)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
69
aca9c90f151c Reduce dependencies; separate Stats from PlotResults (disabled by default to avoid heavy deps)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
70 # Extract SSIM and PSNR columns starting from 500th iteration
aca9c90f151c Reduce dependencies; separate Stats from PlotResults (disabled by default to avoid heavy deps)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
71 ssim_values500 = Float64.(data[mystart:end, :ssim])
aca9c90f151c Reduce dependencies; separate Stats from PlotResults (disabled by default to avoid heavy deps)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
72 psnr_values500 = Float64.(data[mystart:end, :psnr])
aca9c90f151c Reduce dependencies; separate Stats from PlotResults (disabled by default to avoid heavy deps)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
73
aca9c90f151c Reduce dependencies; separate Stats from PlotResults (disabled by default to avoid heavy deps)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
74 # Calculate mean and confidence intervals
aca9c90f151c Reduce dependencies; separate Stats from PlotResults (disabled by default to avoid heavy deps)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
75 ssim_mean1 = round(mean(ssim_values1), digits=4)
aca9c90f151c Reduce dependencies; separate Stats from PlotResults (disabled by default to avoid heavy deps)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
76 psnr_mean1 = round(mean(psnr_values1), digits=4)
aca9c90f151c Reduce dependencies; separate Stats from PlotResults (disabled by default to avoid heavy deps)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
77
aca9c90f151c Reduce dependencies; separate Stats from PlotResults (disabled by default to avoid heavy deps)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
78 ssim_mean500 = round(mean(ssim_values500), digits=4)
aca9c90f151c Reduce dependencies; separate Stats from PlotResults (disabled by default to avoid heavy deps)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
79 psnr_mean500 = round(mean(psnr_values500), digits=4)
aca9c90f151c Reduce dependencies; separate Stats from PlotResults (disabled by default to avoid heavy deps)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
80 ssim_std500 = round(std(ssim_values500), digits=4)
aca9c90f151c Reduce dependencies; separate Stats from PlotResults (disabled by default to avoid heavy deps)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
81 psnr_std500 = round(std(psnr_values500), digits=4)
aca9c90f151c Reduce dependencies; separate Stats from PlotResults (disabled by default to avoid heavy deps)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
82 n = length(ssim_values500)
aca9c90f151c Reduce dependencies; separate Stats from PlotResults (disabled by default to avoid heavy deps)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
83
aca9c90f151c Reduce dependencies; separate Stats from PlotResults (disabled by default to avoid heavy deps)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
84 ssim_ci_lower = round(ssim_mean500 - 1.96 * ssim_std500 / sqrt(n), digits=4)
aca9c90f151c Reduce dependencies; separate Stats from PlotResults (disabled by default to avoid heavy deps)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
85 ssim_ci_upper = round(ssim_mean500 + 1.96 * ssim_std500 / sqrt(n), digits=4)
aca9c90f151c Reduce dependencies; separate Stats from PlotResults (disabled by default to avoid heavy deps)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
86 psnr_ci_lower = round(psnr_mean500 - 1.96 * psnr_std500 / sqrt(n), digits=4)
aca9c90f151c Reduce dependencies; separate Stats from PlotResults (disabled by default to avoid heavy deps)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
87 psnr_ci_upper = round(psnr_mean500 + 1.96 * psnr_std500 / sqrt(n), digits=4)
aca9c90f151c Reduce dependencies; separate Stats from PlotResults (disabled by default to avoid heavy deps)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
88
aca9c90f151c Reduce dependencies; separate Stats from PlotResults (disabled by default to avoid heavy deps)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
89 ssim_ci = "$(ssim_ci_lower) - $(ssim_ci_upper)"
aca9c90f151c Reduce dependencies; separate Stats from PlotResults (disabled by default to avoid heavy deps)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
90 psnr_ci = "$(psnr_ci_lower) - $(psnr_ci_upper)"
aca9c90f151c Reduce dependencies; separate Stats from PlotResults (disabled by default to avoid heavy deps)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
91 experiment = "$(imname)"
aca9c90f151c Reduce dependencies; separate Stats from PlotResults (disabled by default to avoid heavy deps)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
92 algorithm = "$(algname)"
aca9c90f151c Reduce dependencies; separate Stats from PlotResults (disabled by default to avoid heavy deps)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
93
aca9c90f151c Reduce dependencies; separate Stats from PlotResults (disabled by default to avoid heavy deps)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
94 # Append results to DataFrame
aca9c90f151c Reduce dependencies; separate Stats from PlotResults (disabled by default to avoid heavy deps)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
95 push!(results, (experiment, α, algorithm, psnr_mean1, psnr_mean500, psnr_ci, ssim_mean1, ssim_mean500, ssim_ci))
aca9c90f151c Reduce dependencies; separate Stats from PlotResults (disabled by default to avoid heavy deps)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
96 end
aca9c90f151c Reduce dependencies; separate Stats from PlotResults (disabled by default to avoid heavy deps)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
97 end
aca9c90f151c Reduce dependencies; separate Stats from PlotResults (disabled by default to avoid heavy deps)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
98 end
aca9c90f151c Reduce dependencies; separate Stats from PlotResults (disabled by default to avoid heavy deps)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
99 sort!(results, [:experiment, :α])
aca9c90f151c Reduce dependencies; separate Stats from PlotResults (disabled by default to avoid heavy deps)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
100 csv_path = "./img/summarystats.csv"
aca9c90f151c Reduce dependencies; separate Stats from PlotResults (disabled by default to avoid heavy deps)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
101 if isfile(csv_path)
aca9c90f151c Reduce dependencies; separate Stats from PlotResults (disabled by default to avoid heavy deps)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
102 rm(csv_path)
aca9c90f151c Reduce dependencies; separate Stats from PlotResults (disabled by default to avoid heavy deps)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
103 end
aca9c90f151c Reduce dependencies; separate Stats from PlotResults (disabled by default to avoid heavy deps)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
104 CSV.write(csv_path, results)
aca9c90f151c Reduce dependencies; separate Stats from PlotResults (disabled by default to avoid heavy deps)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
105 end
aca9c90f151c Reduce dependencies; separate Stats from PlotResults (disabled by default to avoid heavy deps)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
106
aca9c90f151c Reduce dependencies; separate Stats from PlotResults (disabled by default to avoid heavy deps)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
107 end

mercurial