Skip to content

Commit d6cc2fd

Browse files
committed
Totally new Linear Grid with new interface for grids
1 parent 3434b2c commit d6cc2fd

File tree

7 files changed

+194
-432
lines changed

7 files changed

+194
-432
lines changed

src/analysis/data_analysis.jl

Lines changed: 37 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@ end
398398
daProfile(
399399
data_dict::Dict,
400400
quantity::Symbol,
401-
grid::CircularGrid;
401+
grid::LinearGrid;
402402
<keyword arguments>
403403
)::Union{Tuple{Vector{<:Unitful.Length},Vector{<:Number}},Nothing}
404404
@@ -408,7 +408,7 @@ Compute a profile.
408408
409409
- `data_dict::Dict`: Data dictionary (see [`makeDataDict`](@ref) for the canonical description).
410410
- `quantity::Symbol`: Target quantity. It can be any of the quantities valid for [`scatterQty`](@ref).
411-
- `grid::CircularGrid`: Circular grid.
411+
- `grid::LinearGrid`: Linear grid.
412412
- `norm::Union{Symbol,Nothing}=nothing`: The value of `quantity` in each bin will be divided by the corresponding value of `norm`. It can be any of the quantities valid for [`scatterQty`](@ref). If set to `nothing`, no operation is applied.
413413
- `y_log::Union{Unitful.Units,Nothing}=nothing`: Target unit for `quantity`, if you want to apply ``\\log_{10}`` to `quantity`. If set to `nothing`, the data is left as is.
414414
- `flat::Bool=true`: If the profile will be 2D (rings), or 3D (spherical shells).
@@ -429,7 +429,7 @@ Compute a profile.
429429
function daProfile(
430430
data_dict::Dict,
431431
quantity::Symbol,
432-
grid::CircularGrid;
432+
grid::LinearGrid;
433433
norm::Union{Symbol,Nothing}=nothing,
434434
y_log::Union{Unitful.Units,Nothing}=nothing,
435435
flat::Bool=true,
@@ -476,7 +476,7 @@ function daProfile(
476476
density,
477477
)
478478

479-
x_axis = copy(grid.grid)
479+
x_axis = copy(grid.x_axis)
480480

481481
if !isnothing(y_log)
482482

@@ -502,7 +502,7 @@ end
502502
daBandProfile(
503503
data_dict::Dict,
504504
quantity::Symbol,
505-
grid::CircularGrid;
505+
grid::LinearGrid;
506506
<keyword arguments>
507507
)::Union{
508508
Tuple{Vector{<:Unitful.Length},Vector{<:Number},Vector{<:Number},Vector{<:Number}},
@@ -516,7 +516,7 @@ Compute a profile with uncertainty bands.
516516
517517
- `data_dict::Dict`: Data dictionary (see [`makeDataDict`](@ref) for the canonical description).
518518
- `quantity::Symbol`: Target quantity. It can be any of the quantities valid for [`scatterQty`](@ref).
519-
- `grid::CircularGrid`: Circular grid.
519+
- `grid::LinearGrid`: Linear grid.
520520
- `flat::Bool=true`: If the profile will be 2D (rings), or 3D (spherical shells).
521521
- `density::Bool=false`: If the profile will be of the density of `quantity`.
522522
- `ylog::Bool=false`: If true, returns the profile of ``\\log_{10}``(`quantity`).
@@ -543,7 +543,7 @@ Compute a profile with uncertainty bands.
543543
function daBandProfile(
544544
data_dict::Dict,
545545
quantity::Symbol,
546-
grid::CircularGrid;
546+
grid::LinearGrid;
547547
flat::Bool=true,
548548
density::Bool=false,
549549
ylog::Bool=false,
@@ -593,10 +593,10 @@ function daBandProfile(
593593
center, low, high = computeBandProfile(positions, values, grid; flat, density)
594594

595595
if error_bar
596-
return grid.grid, center, center .- low, high .- center
596+
return grid.x_axis, center, center .- low, high .- center
597597
end
598598

599-
return grid.grid, low, high
599+
return grid.x_axis, low, high
600600

601601
end
602602

@@ -662,7 +662,7 @@ function daStellarHistory(
662662
# Compute the birth time range
663663
min, max = extrema(birth_times)
664664

665-
grid = CircularGrid(max, n_bins; shift=min)
665+
grid = LinearGrid(min, max, n_bins)
666666

667667
# Compute the total stellar mass in each time bin
668668
stellar_masses = histogram1D(birth_times, masses, grid; empty_nan=false)
@@ -738,6 +738,7 @@ Compute a 1D histogram of a given `quantity`.
738738
- `data_dict::Dict`: Data dictionary (see [`makeDataDict`](@ref) for the canonical description).
739739
- `quantity::Symbol`: Target quantity. It can be any of the quantities valid for [`scatterQty`](@ref).
740740
- `grid::LinearGrid`: Linear grid.
741+
- `log::Bool=false`: If the histogram bins will be logarithmic.
741742
- `norm::Int=0`: Number of counts that will be use to normalize the histogram. If left as 0, the histogram will be normalize with the maximum bin count.
742743
- `filter_function::Function=filterNothing`: Filter function to be applied to `data_dict` before any other computation. See the required signature and examples in `./src/analysis/filters.jl`.
743744
@@ -752,6 +753,7 @@ function daHistogram(
752753
data_dict::Dict,
753754
quantity::Symbol,
754755
grid::LinearGrid;
756+
log::Bool=false,
755757
norm::Int=0,
756758
filter_function::Function=filterNothing,
757759
)::Union{Tuple{Vector{<:Number},Vector{Float64}},Nothing}
@@ -833,10 +835,10 @@ function daHistogram(
833835

834836
end
835837

836-
if grid.log
837-
x_axis = log10.(ustrip.(plot_params.unit, grid.grid))
838+
if log
839+
x_axis = log10.(ustrip.(plot_params.unit, grid.x_axis))
838840
else
839-
x_axis = copy(grid.grid)
841+
x_axis = copy(grid.x_axis)
840842
end
841843

842844
return x_axis, y_axis
@@ -847,8 +849,7 @@ end
847849
daHistogram(
848850
data_dict::Dict,
849851
quantity::Symbol,
850-
n_bins::Int,
851-
log::Bool;
852+
n_bins::Int;
852853
<keyword arguments>
853854
)::Union{Tuple{Vector{<:Number},Vector{<:Number}},Nothing}
854855
@@ -863,7 +864,7 @@ Compute a 1D histogram of a given `quantity`.
863864
- `data_dict::Dict`: Data dictionary (see [`makeDataDict`](@ref) for the canonical description).
864865
- `quantity::Symbol`: Target quantity. It can be any of the quantities valid for [`scatterQty`](@ref).
865866
- `n_bins::Int`: Number of bins.
866-
- `log::Bool`: If the histogram bins will be logarithmic.
867+
- `log::Bool=false`: If the histogram bins will be logarithmic.
867868
- `norm::Int=0`: Number of count that will be use to normalize the histogram. If left as 0, the histogram will be normalize with the maximum bin count.
868869
- `filter_function::Function=filterNothing`: Filter function to be applied to `data_dict` before any other computation. See the required signature and examples in `./src/analysis/filters.jl`.
869870
@@ -877,8 +878,8 @@ Compute a 1D histogram of a given `quantity`.
877878
function daHistogram(
878879
data_dict::Dict,
879880
quantity::Symbol,
880-
n_bins::Int,
881-
log::Bool;
881+
n_bins::Int;
882+
log::Bool=false,
882883
norm::Int=0,
883884
filter_function::Function=filterNothing,
884885
)::Union{Tuple{Vector{<:Number},Vector{Float64}},Nothing}
@@ -955,7 +956,7 @@ function daHistogram(
955956
\n Snapshot: $(data_dict[:snap_data].global_index) \
956957
\n Quantity: $(quantity) \
957958
\n Type: $(cp_type) \
958-
\n Largest bin: $(grid.grid[argmax(counts)]) \
959+
\n Largest bin: $(grid.x_axis[argmax(counts)]) \
959960
\n Max count: $(maximum(counts)) \
960961
\n Min - Max: $(min_max_v) \
961962
\n Mean: $(mean_v) \
@@ -966,9 +967,9 @@ function daHistogram(
966967
end
967968

968969
if log
969-
x_axis = log10.(ustrip.(plot_params.unit, grid.grid))
970+
x_axis = log10.(ustrip.(plot_params.unit, grid.x_axis))
970971
else
971-
x_axis = copy(grid.grid)
972+
x_axis = copy(grid.x_axis)
972973
end
973974

974975
return x_axis, y_axis
@@ -1092,7 +1093,7 @@ function daBarGasFractions(
10921093
histograms = [histogram1D(gas_qty, mass, grid; empty_nan=false) for mass in masses]
10931094

10941095
# Compute the number of bins
1095-
n_bins = length(grid.edges) - 1
1096+
n_bins = grid.size
10961097

10971098
# Compute the number of bars per bin
10981099
n_bars = length(components)
@@ -1126,7 +1127,7 @@ end
11261127
"""
11271128
daMolla2015(
11281129
data_dict::Dict,
1129-
grid::CircularGrid,
1130+
grid::LinearGrid,
11301131
quantity::Symbol;
11311132
<keyword arguments>
11321133
)::Union{
@@ -1142,7 +1143,7 @@ Compute a profile for the Milky Way, compatible with the experimental data in Mo
11421143
# Arguments
11431144
11441145
- `data_dict::Dict`: Data dictionary (see [`makeDataDict`](@ref) for the canonical description).
1145-
- `grid::CircularGrid`: Circular grid.
1146+
- `grid::LinearGrid`: Linear grid.
11461147
- `quantity::Symbol`: Quantity for the y axis. The options are:
11471148
11481149
+ `:stellar_area_density` -> Stellar mass surface density.
@@ -1174,7 +1175,7 @@ M. Mollá et al. (2015). *Galactic chemical evolution: stellar yields and the in
11741175
"""
11751176
function daMolla2015(
11761177
data_dict::Dict,
1177-
grid::CircularGrid,
1178+
grid::LinearGrid,
11781179
quantity::Symbol;
11791180
y_unit::Unitful.Units=Unitful.NoUnits,
11801181
filter_function::Function=filterNothing,
@@ -1226,7 +1227,7 @@ function daMolla2015(
12261227

12271228
density_profile = scaling(computeProfile(positions, masses, grid; norm, density))
12281229

1229-
return grid.grid, density_profile
1230+
return grid.x_axis, density_profile
12301231

12311232
end
12321233

@@ -1333,7 +1334,7 @@ function daDensity2DProjection(
13331334

13341335
# Reduce the resolution of the result using a circular grid
13351336
# `reduce_factor` here is the number of bins for the circular grid
1336-
density = projectIntoCircularGrid(density, reduce_factor)
1337+
density = projectIntoLinearGrid(density, reduce_factor)
13371338
x_axis = [grid.grid_size * (2 * i - 1) / (4 * reduce_factor) for i in 1:reduce_factor]
13381339
y_axis = x_axis
13391340

@@ -1476,7 +1477,7 @@ function daGasSFR2DProjection(
14761477

14771478
# Reduce the resolution of the result using a circular grid
14781479
# `reduce_factor` here is the number of bins for the circular grid
1479-
sfr = projectIntoCircularGrid(sfr, reduce_factor; total=true)
1480+
sfr = projectIntoLinearGrid(sfr, reduce_factor; total=true)
14801481
x_axis = [grid.grid_size * (2 * i - 1) / (4 * reduce_factor) for i in 1:reduce_factor]
14811482
y_axis = x_axis
14821483

@@ -1655,8 +1656,8 @@ function daMetallicity2DProjection(
16551656

16561657
# Reduce the resolution of the result into a circular grid
16571658
# `reduce_factor` here is the number of bins for the circular grid
1658-
metal_mass = projectIntoCircularGrid(metal_mass, reduce_factor; total=true)
1659-
norm_mass = projectIntoCircularGrid(norm_mass, reduce_factor; total=true)
1659+
metal_mass = projectIntoLinearGrid(metal_mass, reduce_factor; total=true)
1660+
norm_mass = projectIntoLinearGrid(norm_mass, reduce_factor; total=true)
16601661

16611662
x_axis = [grid.grid_size * (2 * i - 1) / (4 * reduce_factor) for i in 1:reduce_factor]
16621663
y_axis = x_axis
@@ -3213,7 +3214,7 @@ end
32133214
daClumpingFactorProfile(
32143215
data_dict::Dict,
32153216
component::Symbol,
3216-
grid::CircularGrid;
3217+
grid::LinearGrid;
32173218
<keyword arguments>
32183219
)::Tuple{Vector{<:Unitful.Length},Vector{Float64}}
32193220
@@ -3227,7 +3228,7 @@ C_\rho = \frac{\langle n^2 \rangle}{\langle n \rangle^2} \, ,
32273228
32283229
- `data_dict::Dict`: Data dictionary (see [`makeDataDict`](@ref) for the canonical description).
32293230
- `component::Symbol`: Target component. It can only be one of the elements of [`COMPONENTS`](@ref) with cell/partcile type :gas.
3230-
- `grid::CircularGrid`: Circular grid.
3231+
- `grid::LinearGrid`: Linear grid.
32313232
- `filter_function::Function=filterNothing`: Filter function to be applied to `data_dict` before any other computation. See the required signature and examples in `./src/analysis/filters.jl`.
32323233
32333234
# Returns
@@ -3240,7 +3241,7 @@ C_\rho = \frac{\langle n^2 \rangle}{\langle n \rangle^2} \, ,
32403241
function daClumpingFactorProfile(
32413242
data_dict::Dict,
32423243
component::Symbol,
3243-
grid::CircularGrid;
3244+
grid::LinearGrid;
32443245
filter_function::Function=filterNothing,
32453246
)::Tuple{Vector{<:Unitful.Length},Vector{Float64}}
32463247

@@ -3258,20 +3259,20 @@ function daClumpingFactorProfile(
32583259
positions = filtered_dd[:gas]["POS "]
32593260

32603261
# Compute the radial distance of each cell/particle
3261-
distances = colwise(Euclidean(), positions[1:2, :], grid.center[1:2])
3262+
distances = colwise(Euclidean(), positions[1:2, :], grid.origin[1:2])
32623263

32633264
# Find which cells/particles fall within each bin of `grid`
32643265
n_profile = listHistogram1D(distances, number_densities, grid)
32653266

3266-
= similar(grid.grid, Float64)
3267+
= fill(NaN, grid.size)
32673268

32683269
Threads.@threads for (i, n) in collect(pairs(n_profile))
32693270

32703271
Cρ[i] = computeClumpingFactor(n)
32713272

32723273
end
32733274

3274-
return grid.grid, Cρ
3275+
return grid.x_axis, Cρ
32753276

32763277
end
32773278

src/auxiliary_functions/grid.jl

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -130,13 +130,13 @@ function reduceMatrix(hr_matrix::Matrix{<:Number}, factor::Int; total::Bool=fals
130130
end
131131

132132
"""
133-
projectIntoCircularGrid(
133+
projectIntoLinearGrid(
134134
image::Matrix{<:Number},
135135
n_bins::Int;
136136
<keyword arguments>
137137
)::Vector{<:Number}
138138
139-
Project a given matrix into a circular grid, averaging or adding up the values in each concentric ring.
139+
Project a given matrix into a linear grid (in a 2D space), averaging or adding up the values in each concentric ring.
140140
141141
# Arguments
142142
@@ -149,7 +149,7 @@ Project a given matrix into a circular grid, averaging or adding up the values i
149149
150150
- A vector with the average or sum of the values that fall within each concentric ring.
151151
"""
152-
function projectIntoCircularGrid(
152+
function projectIntoLinearGrid(
153153
image::Matrix{<:Number},
154154
n_bins::Int;
155155
inscribed::Bool=true,
@@ -160,26 +160,26 @@ function projectIntoCircularGrid(
160160

161161
(
162162
r == c ||
163-
throw(ArgumentError("projectIntoCircularGrid: `image` has to be a square matrix, \
163+
throw(ArgumentError("projectIntoLinearGrid: `image` has to be a square matrix, \
164164
but it has $(c) columns and $(r) rows"))
165165
)
166166

167167
(
168168
n_bins >= 1 ||
169-
throw(ArgumentError("projectIntoCircularGrid: `n_bins` must be >= 1, \
169+
throw(ArgumentError("projectIntoLinearGrid: `n_bins` must be >= 1, \
170170
but I got `n_bins` = $(n_bins)"))
171171
)
172172

173173
# Construct a square grid centered at (0, 0)
174174
square_grid = SquareGrid(1.0, r)
175175

176-
# Construct a circular grid centered at (0, 0)
177-
circular_grid = CircularGrid(inscribed ? 0.5 : sqrt(0.5), n_bins)
176+
# Construct a linear grid centered at (0, 0)
177+
linear_grid = LinearGrid(0.0, inscribed ? 0.5 : sqrt(0.5), n_bins)
178178

179179
# Compute the radial distance of each pixel in the square grid to the origin
180180
positions = norm.(vec(square_grid.grid))
181181

182-
profile = histogram1D(positions, vec(image), circular_grid; total, empty_nan=false)
182+
profile = histogram1D(positions, vec(image), linear_grid; total, empty_nan=false)
183183

184184
return profile
185185

0 commit comments

Comments
 (0)