-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2_Combine_spectra.jl
190 lines (163 loc) · 6.35 KB
/
2_Combine_spectra.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
### A Pluto.jl notebook ###
# v0.19.9
using Markdown
using InteractiveUtils
# This Pluto notebook uses @bind for interactivity. When running this notebook outside of Pluto, the following 'mock version' of @bind gives bound variables a default value (instead of an error).
macro bind(def, element)
quote
local iv = try Base.loaded_modules[Base.PkgId(Base.UUID("6e696c72-6542-2067-7265-42206c756150"), "AbstractPlutoDingetjes")].Bonds.initial_value catch; b -> missing; end
local el = $(esc(element))
global $(esc(def)) = Core.applicable(Base.get, el) ? Base.get(el) : iv(el)
el
end
end
# ╔═╡ 1d36be1e-12a7-11ef-0d8b-05789779f611
using Pkg; Pkg.activate("C:\\Users\\Alfonso\\MEGA\\PhD\\BOLD\\Data_analysis\\WideField_analysis")
# ╔═╡ 003ae74c-597c-4792-8c84-847942ea28dd
begin
using Images
using Plots
using Plots.PlotMeasures
using LaTeXStrings
using PlutoUI
using Base.Filesystem
using DelimitedFiles
using Statistics
gr();
end
# ╔═╡ 6e448be7-2eda-4243-b260-4b86101575d4
md"""
# Program explanation
"""
# ╔═╡ 297b76bb-abd1-4fff-848a-3a2a3f39ee38
md"""
This program takes the results from the notebook "Spectrum\_individual\_fields", combines all the spectra of the different fields in the same sample and does the combined analysis and plots for each sample
"""
# ╔═╡ 0779c935-6d5c-4f4b-9590-89027b39fe67
md"""
## Set the working directory
"""
# ╔═╡ f210f763-03f6-4685-8993-579b3e745799
folder_data = raw"C:\Users\Alfonso\BOLD\WideField\Data\april_2024\71\Spectrum"
# ╔═╡ 275b1e4d-5fef-40a7-9e35-3ec8d79a029e
md"""Check to store the results in the folder: $(@bind store_results CheckBox())"""
# ╔═╡ 576baf5a-155d-4457-81ed-2712d99a613b
md"""
##### Creation of the results folder (if not existent)
"""
# ╔═╡ 387cafb5-27b0-4321-8144-f94b60a63729
if store_results
results_folder_name = joinpath(folder_data, "Analysis_AYN")
if isdir(results_folder_name)
print("Directory already exists")
else
try
mkdir(results_folder_name)
println("Directory created")
catch e
println("Could not create directory")
end
end
end;
# ╔═╡ 1a81dd3d-4abb-480c-b755-d6cc3202bf3e
md"""
### Construction of the combined spectra and average spectrum for the sample
"""
# ╔═╡ a2c19a14-d58a-4635-b3c1-262b0ad47ba8
md"""
# TO DO
"""
# ╔═╡ 5beabc48-84ee-4f5b-a8e2-d576c673e260
md"""
Average spectrum
"""
# ╔═╡ 61999d4f-30d5-4bb3-92bc-8de8a15b9bc9
md"""
# Functions
"""
# ╔═╡ cc1905c7-7172-4dd8-9237-d9ea2214b56d
"""
Sorts the name of the folders containing the fields data by numerical
"""
function sort_fields(dir1, dir2)
num1 = parse(Int, match(r"Field(\d+)", dir1).captures[1])
num2 = parse(Int, match(r"Field(\d+)", dir2).captures[1])
return num1 < num2
end
# ╔═╡ 46d4dd19-6fab-4407-a9bd-2a5818c38518
begin
all_files = readdir(folder_data)
field_list_unordered = filter(file -> occursin(r"^Field\d+$", file), all_files)
field_list = sort(field_list_unordered, lt=sort_fields)
#println("Field directories: ", field_list)
end;
# ╔═╡ 1ce5793c-f58f-4998-b2f1-685c91b393e6
begin
x_values = []
y_values = []
x_errors = []
for field in field_list
complete_path = joinpath(folder_data, field, "Analysis_AYN")
files = readdir(complete_path)
txt_spectrum_file = filter(file -> contains(file, "spectrum_ROI.txt"), files)
contents = readlines(joinpath(complete_path, "spectrum_ROI.txt"))
x_values_ind, y_values_ind, x_errors_ind = [[parse(Float64, val) for val in split(line)] for line in contents]
push!(x_values, x_values_ind)
push!(y_values, y_values_ind)
push!(x_errors, x_errors_ind)
end
y_average = [mean([y_values[j][i] for j in 1:length(y_values)]) for i in 1:length(y_values[1])]
end;
# ╔═╡ fb1e75a1-0ef2-4c93-ba59-a0fbcb3c8619
begin
figure_plot_average_spectrum = "spectrum_ROI_average.png"
p2 = plot(xlabel=L"$\lambda$ (nm)", ylabel=L"$\gamma$/s/$\Delta \lambda$ per pixel", xlabelfontsize = 9, ylabelfontsize = 9, label=false, linecolor=:black, framestyle=:box, framelinewidth=3, xtickfontsize=9, ytickfontsize=9, xticks=400:50:800, size=(600,350), title="Average spectrum", titlefontsize=12, legend=:outertopright, legendfontsize=9, dpi=300)
plot!(p2, x_values[1], y_average, xerror = x_errors[1], marker=:circle, markersize = 5, line=:2.5, label = "$(size(y_values)[1]) spectra")
p2
end
# ╔═╡ 39edb00a-1b35-4ffd-a9b4-1a271723c67f
if store_results
savefig(joinpath(results_folder_name, figure_plot_average_spectrum))
end
# ╔═╡ b8bcedc8-75b7-4419-abe2-b5375ad20cc0
begin
figure_plot_spectra = "spectra_ROI_combined.png"
p = plot(xlabel=L"$\lambda$ (nm)", ylabel=L"$\gamma$/s/$\Delta \lambda$ per pixel", xlabelfontsize = 9, ylabelfontsize = 9, label=false, linecolor=:black, framestyle=:box, framelinewidth=3, xtickfontsize=9, ytickfontsize=9, xticks=400:50:800, size=(600,350), title="Combined spectra", titlefontsize=12, legend=:outertopright, legendfontsize=9, dpi=300)
for i in 1:length(field_list)
plot!(p, x_values[i], y_values[i], xerror = x_errors[i], marker=:circle, markersize = 5, line=:2.5, label=field_list[i])
end
p
end
# ╔═╡ 4db93882-f2ca-4159-b8d7-33140a5dafd0
if store_results
savefig(joinpath(results_folder_name, figure_plot_spectra))
end
# ╔═╡ 5dbcd6b1-47c4-4d68-b52a-2eddf1aa452b
md"""
# Other utilities
"""
# ╔═╡ 710b3535-43d8-4eca-bb32-b6855fc22644
PlutoUI.TableOfContents(title="WideField images analysis", indent=true)
# ╔═╡ Cell order:
# ╟─6e448be7-2eda-4243-b260-4b86101575d4
# ╟─297b76bb-abd1-4fff-848a-3a2a3f39ee38
# ╠═1d36be1e-12a7-11ef-0d8b-05789779f611
# ╠═003ae74c-597c-4792-8c84-847942ea28dd
# ╟─0779c935-6d5c-4f4b-9590-89027b39fe67
# ╠═f210f763-03f6-4685-8993-579b3e745799
# ╟─275b1e4d-5fef-40a7-9e35-3ec8d79a029e
# ╟─576baf5a-155d-4457-81ed-2712d99a613b
# ╠═387cafb5-27b0-4321-8144-f94b60a63729
# ╟─1a81dd3d-4abb-480c-b755-d6cc3202bf3e
# ╠═46d4dd19-6fab-4407-a9bd-2a5818c38518
# ╠═1ce5793c-f58f-4998-b2f1-685c91b393e6
# ╠═b8bcedc8-75b7-4419-abe2-b5375ad20cc0
# ╠═4db93882-f2ca-4159-b8d7-33140a5dafd0
# ╠═fb1e75a1-0ef2-4c93-ba59-a0fbcb3c8619
# ╠═39edb00a-1b35-4ffd-a9b4-1a271723c67f
# ╟─a2c19a14-d58a-4635-b3c1-262b0ad47ba8
# ╟─5beabc48-84ee-4f5b-a8e2-d576c673e260
# ╟─61999d4f-30d5-4bb3-92bc-8de8a15b9bc9
# ╟─cc1905c7-7172-4dd8-9237-d9ea2214b56d
# ╟─5dbcd6b1-47c4-4d68-b52a-2eddf1aa452b
# ╠═710b3535-43d8-4eca-bb32-b6855fc22644