src/Stats.jl

Thu, 25 Apr 2024 15:12:09 -0500

author
Tuomo Valkonen <tuomov@iki.fi>
date
Thu, 25 Apr 2024 15:12:09 -0500
changeset 45
21909e22f875
parent 44
7fb55885d85f
child 47
70fd92ac9da0
permissions
-rw-r--r--

Fix default α

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
44
7fb55885d85f calculate_statistics options
Tuomo Valkonen <tuomov@iki.fi>
parents: 41
diff changeset
44 const default_imnames = ("lighthouse200x300", "shepplogan256x256", "brainphantom256x256")
7fb55885d85f calculate_statistics options
Tuomo Valkonen <tuomov@iki.fi>
parents: 41
diff changeset
45 const default_algnames = ("dualscaling", "greedy","noprediction","primalonly","proximal","rotation","zerodual")
7fb55885d85f calculate_statistics options
Tuomo Valkonen <tuomov@iki.fi>
parents: 41
diff changeset
46
7fb55885d85f calculate_statistics options
Tuomo Valkonen <tuomov@iki.fi>
parents: 41
diff changeset
47 function calculate_statistics(;imnames=default_imnames,
7fb55885d85f calculate_statistics options
Tuomo Valkonen <tuomov@iki.fi>
parents: 41
diff changeset
48 algnames=default_algnames,
7fb55885d85f calculate_statistics options
Tuomo Valkonen <tuomov@iki.fi>
parents: 41
diff changeset
49 csv_path = "./img/summarystats.csv")
34
aca9c90f151c Reduce dependencies; separate Stats from PlotResults (disabled by default to avoid heavy deps)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
50 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
51
aca9c90f151c Reduce dependencies; separate Stats from PlotResults (disabled by default to avoid heavy deps)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
52 # 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
53 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
54
44
7fb55885d85f calculate_statistics options
Tuomo Valkonen <tuomov@iki.fi>
parents: 41
diff changeset
55 for imname in imnames
7fb55885d85f calculate_statistics options
Tuomo Valkonen <tuomov@iki.fi>
parents: 41
diff changeset
56 for algname in algnames
34
aca9c90f151c Reduce dependencies; separate Stats from PlotResults (disabled by default to avoid heavy deps)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
57 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
58 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
59 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
60
aca9c90f151c Reduce dependencies; separate Stats from PlotResults (disabled by default to avoid heavy deps)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
61 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
62 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
63 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
64
aca9c90f151c Reduce dependencies; separate Stats from PlotResults (disabled by default to avoid heavy deps)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
65 # 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
66 α, _, _ = 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
67
aca9c90f151c Reduce dependencies; separate Stats from PlotResults (disabled by default to avoid heavy deps)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
68
aca9c90f151c Reduce dependencies; separate Stats from PlotResults (disabled by default to avoid heavy deps)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
69 # 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
70 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
71 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
72
aca9c90f151c Reduce dependencies; separate Stats from PlotResults (disabled by default to avoid heavy deps)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
73 # 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
74 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
75 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
76
aca9c90f151c Reduce dependencies; separate Stats from PlotResults (disabled by default to avoid heavy deps)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
77 # 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
78 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
79 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
80
aca9c90f151c Reduce dependencies; separate Stats from PlotResults (disabled by default to avoid heavy deps)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
81 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
82 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
83 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
84 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
85 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
86
aca9c90f151c Reduce dependencies; separate Stats from PlotResults (disabled by default to avoid heavy deps)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
87 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
88 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
89 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
90 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
91
aca9c90f151c Reduce dependencies; separate Stats from PlotResults (disabled by default to avoid heavy deps)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
92 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
93 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
94 experiment = "$(imname)"
aca9c90f151c Reduce dependencies; separate Stats from PlotResults (disabled by default to avoid heavy deps)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
95 algorithm = "$(algname)"
aca9c90f151c Reduce dependencies; separate Stats from PlotResults (disabled by default to avoid heavy deps)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
96
aca9c90f151c Reduce dependencies; separate Stats from PlotResults (disabled by default to avoid heavy deps)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
97 # 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
98 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
99 end
aca9c90f151c Reduce dependencies; separate Stats from PlotResults (disabled by default to avoid heavy deps)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
100 end
aca9c90f151c Reduce dependencies; separate Stats from PlotResults (disabled by default to avoid heavy deps)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
101 end
aca9c90f151c Reduce dependencies; separate Stats from PlotResults (disabled by default to avoid heavy deps)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
102 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
103 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
104 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
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 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
107 end
aca9c90f151c Reduce dependencies; separate Stats from PlotResults (disabled by default to avoid heavy deps)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
108
aca9c90f151c Reduce dependencies; separate Stats from PlotResults (disabled by default to avoid heavy deps)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
109 end

mercurial