Skip to content
This repository was archived by the owner on Jul 11, 2025. It is now read-only.

Commit 47c1ce7

Browse files
Merge pull request #55 from open-AIMS/perf-check
Range of performance changes
2 parents 5e47be1 + 00c1839 commit 47c1ce7

File tree

9 files changed

+443
-245
lines changed

9 files changed

+443
-245
lines changed

Manifest-v1.11.toml

Lines changed: 228 additions & 151 deletions
Large diffs are not rendered by default.

Project.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ ThreadsX = "ac1d9e8a-700a-412c-b207-f0111f4b6c0d"
4949
YAXArrays = "c21b50f5-aa40-41ea-b809-c0f5e47bfa5c"
5050

5151
[compat]
52-
DiskArrays = "=0.3.23, ^0.4.5"
52+
DiskArrays = "0.4.9"
5353
MKL_jll = "2024.2.0"
5454
NearestNeighbors = "0.4.20"
55+
YAXArrays = "0.5"

src/ReefGuideAPI.jl

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ function initialize_regional_data_cache(reef_data_path::String, reg_cache_fn::St
5656
String,Union{RegionalCriteria,DataFrame,Dict}
5757
}()
5858
for reg in get_regions()
59+
@debug "$(now()) : Initializing cache for $reg"
5960
data_paths = String[]
6061
data_names = String[]
6162

@@ -77,7 +78,8 @@ function initialize_regional_data_cache(reef_data_path::String, reg_cache_fn::St
7778
if occursin("slope", string(dp))
7879
slope_table = GeoParquet.read(parq_file)
7980
elseif occursin("flat", string(dp))
80-
flat_table = GeoParquet.read(parq_file)
81+
@warn "Skipping data for reef flats as it is currently unused"
82+
# flat_table = GeoParquet.read(parq_file)
8183
else
8284
msg = "Unknown lookup found: $(parq_file). Must be 'slope' or 'flat'"
8385
throw(ArgumentError(msg))
@@ -90,9 +92,9 @@ function initialize_regional_data_cache(reef_data_path::String, reg_cache_fn::St
9092
slope_table[!, :lons] .= first.(coords)
9193
slope_table[!, :lats] .= last.(coords)
9294

93-
coords = GI.coordinates.(flat_table.geometry)
94-
flat_table[!, :lons] .= first.(coords)
95-
flat_table[!, :lats] .= last.(coords)
95+
# coords = GI.coordinates.(flat_table.geometry)
96+
# flat_table[!, :lons] .= first.(coords)
97+
# flat_table[!, :lats] .= last.(coords)
9698

9799
rst_stack = RasterStack(data_paths; name=data_names, lazy=true)
98100

@@ -106,8 +108,10 @@ function initialize_regional_data_cache(reef_data_path::String, reg_cache_fn::St
106108
regional_assessment_data[reg] = RegionalCriteria(
107109
rst_stack,
108110
slope_table,
109-
flat_table
111+
slope_table[[1], :] # flat_table
110112
)
113+
114+
@debug "$(now()) : Finished initialization for $reg"
111115
end
112116

113117
regional_assessment_data["region_long_names"] = Dict(

src/criteria_assessment/criteria.jl

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,24 @@ function criteria_data_map()
3535
)
3636
end
3737

38+
function search_criteria()::Vector{String}
39+
return string.(keys(criteria_data_map()))
40+
end
41+
42+
function site_criteria()::Vector{String}
43+
return ["SuitabilityThreshold", "xdist", "ydist"]
44+
end
45+
46+
function suitability_criteria()::Vector{String}
47+
return vcat(search_criteria(), ["SuitabilityThreshold"])
48+
end
49+
50+
function extract_criteria(qp::T, criteria::Vector{String})::T where {T<:Dict{String,String}}
51+
return filter(
52+
k -> string(k.first) criteria, qp
53+
)
54+
end
55+
3856
struct RegionalCriteria{T}
3957
stack::RasterStack
4058
valid_slopes::T
@@ -175,18 +193,9 @@ function setup_region_routes(config, auth)
175193
# somewhere:8000/suitability/assess/region-name/reeftype?criteria_names=Depth,Slope&lb=-9.0,0.0&ub=-2.0,40
176194
# 127.0.0.1:8000/suitability/assess/Cairns-Cooktown/slopes?Depth=-4.0:-2.0&Slope=0.0:40.0&Rugosity=0.0:6.0
177195
# 127.0.0.1:8000/suitability/assess/Cairns-Cooktown/slopes?Depth=-4.0:-2.0&Slope=0.0:40.0&Rugosity=0.0:6.0&SuitabilityThreshold=95
178-
196+
# 127.0.0.1:8000/suitability/assess/Mackay-Capricorn/slopes?Depth=-12.0:-2.0&Slope=0.0:40.0&Rugosity=0.0:6.0&SuitabilityThreshold=95
179197
qp = queryparams(req)
180-
assessed_fn = cache_filename(qp, config, "$(reg)_suitable", "tiff")
181-
if isfile(assessed_fn)
182-
return file(assessed_fn; headers=COG_HEADERS)
183-
end
184-
185-
@debug "$(now()) : Assessing region $(reg)"
186-
assessed = assess_region(reg_assess_data, reg, qp, rtype)
187-
188-
@debug "$(now()) : Writing to $(assessed_fn)"
189-
_write_tiff(assessed_fn, assessed)
198+
assessed_fn = assess_region(config, qp, reg, rtype, reg_assess_data)
190199

191200
return file(assessed_fn; headers=COG_HEADERS)
192201
end
@@ -195,6 +204,7 @@ function setup_region_routes(config, auth)
195204
req::Request, reg::String, rtype::String
196205
)
197206
# 127.0.0.1:8000/suitability/site-suitability/Cairns-Cooktown/slopes?Depth=-4.0:-2.0&Slope=0.0:40.0&Rugosity=0.0:6.0&SuitabilityThreshold=95&xdist=450&ydist=50
207+
# 127.0.0.1:8000/suitability/site-suitability/Mackay-Capricorn/slopes?Depth=-12.0:-2.0&Slope=0.0:40.0&Rugosity=0.0:6.0&SuitabilityThreshold=95&xdist=100&ydist=100
198208
qp = queryparams(req)
199209
suitable_sites_fn = cache_filename(
200210
qp, config, "$(reg)_potential_sites", "geojson"
@@ -204,24 +214,16 @@ function setup_region_routes(config, auth)
204214
end
205215

206216
# Assess location suitability if needed
207-
assessed_fn = cache_filename(qp, config, "$(reg)_suitable", "tiff")
208-
if isfile(assessed_fn)
209-
assessed = Raster(assessed_fn)
210-
else
211-
assessed = assess_region(reg_assess_data, reg, qp, rtype)
212-
_write_tiff(assessed_fn, assessed)
213-
end
217+
assessed_fn = assess_region(config, qp, reg, rtype, reg_assess_data)
218+
assessed = Raster(assessed_fn; lazy=true)
214219

215220
# Extract criteria and assessment
216-
criteria_names = string.(keys(criteria_data_map()))
217-
pixel_criteria = filter(k -> k.first criteria_names, qp)
218-
site_criteria = filter(
219-
k -> string(k.first) ["SuitabilityThreshold", "xdist", "ydist"], qp
220-
)
221+
pixel_criteria = extract_criteria(qp, search_criteria())
222+
deploy_site_criteria = extract_criteria(qp, site_criteria())
221223

222224
best_sites = filter_sites(
223225
assess_sites(
224-
reg_assess_data, reg, rtype, pixel_criteria, site_criteria, assessed
226+
reg_assess_data, reg, rtype, pixel_criteria, deploy_site_criteria, assessed
225227
)
226228
)
227229

src/criteria_assessment/query_thresholds.jl

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -161,16 +161,17 @@ function apply_criteria_thresholds(
161161
ruleset::NamedTuple
162162
)::Raster
163163
# Result store
164-
res = Raster(criteria_stack.Depth; data=falses(size(criteria_stack)), missingval=0)
164+
data = falses(size(criteria_stack))
165165

166166
res_lookup = trues(nrow(lookup))
167167
for rule_name in keys(ruleset)
168168
res_lookup .= res_lookup .& ruleset[rule_name](lookup[!, rule_name])
169169
end
170170

171171
tmp = lookup[res_lookup, [:lon_idx, :lat_idx]]
172-
res[CartesianIndex.(tmp.lon_idx, tmp.lat_idx)] .= true
172+
data[CartesianIndex.(tmp.lon_idx, tmp.lat_idx)] .= true
173173

174+
res = Raster(criteria_stack.Depth; data=sparse(data), missingval=0)
174175
return res
175176
end
176177
function apply_criteria_thresholds(
@@ -179,22 +180,24 @@ function apply_criteria_thresholds(
179180
ruleset::Vector{CriteriaBounds{Function}}
180181
)::Raster where {T}
181182
# Result store
182-
res = Raster(criteria_stack.Depth; data=falses(size(criteria_stack)), missingval=0)
183+
data = falses(size(criteria_stack))
183184

184185
res_lookup = trues(nrow(lookup))
185186
for threshold in ruleset
186187
res_lookup .= res_lookup .& threshold.rule(lookup[!, threshold.name])
187188
end
188189

189190
tmp = lookup[res_lookup, [:lon_idx, :lat_idx]]
190-
res[CartesianIndex.(tmp.lon_idx, tmp.lat_idx)] .= true
191+
data[CartesianIndex.(tmp.lon_idx, tmp.lat_idx)] .= true
192+
193+
res = Raster(criteria_stack.Depth; data=sparse(data), missingval=0)
191194

192195
return res
193196
end
194197

195198
"""
196199
apply_criteria_lookup(
197-
reg_criteria,
200+
reg_criteria::RegionalCriteria,
198201
rtype::Symbol,
199202
ruleset::Vector{CriteriaBounds{Function}}
200203
)
@@ -247,7 +250,11 @@ applied to a set of criteria.
247250
# Returns
248251
True/false mask indicating locations within desired thresholds.
249252
"""
250-
function threshold_mask(reg_criteria, rtype::Symbol, crit_map)::Raster
253+
function threshold_mask(
254+
reg_criteria::RegionalCriteria,
255+
rtype::Symbol,
256+
crit_map::Vector{CriteriaBounds{Function}}
257+
)::Raster
251258
valid_lookup = getfield(reg_criteria, Symbol(:valid_, rtype))
252259
mask_layer = apply_criteria_thresholds(
253260
reg_criteria.stack,
@@ -258,9 +265,9 @@ function threshold_mask(reg_criteria, rtype::Symbol, crit_map)::Raster
258265
return mask_layer
259266
end
260267
function threshold_mask(
261-
reg_criteria,
268+
reg_criteria::RegionalCriteria,
262269
rtype::Symbol,
263-
crit_map,
270+
crit_map::Vector{CriteriaBounds{Function}},
264271
lons::Tuple,
265272
lats::Tuple
266273
)::Raster

0 commit comments

Comments
 (0)