Skip to content

Commit

Permalink
Merge pull request #42 from lmejn/fix/fixed-tests
Browse files Browse the repository at this point in the history
Fixed Tests
  • Loading branch information
lmejn authored Mar 20, 2023
2 parents e2dd3f4 + a140932 commit 3c913a2
Show file tree
Hide file tree
Showing 19 changed files with 45 additions and 56 deletions.
1 change: 0 additions & 1 deletion .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ jobs:
- ubuntu-latest
arch:
- x64
- x86
steps:
- uses: actions/checkout@v2
- uses: julia-actions/setup-julia@v1
Expand Down
2 changes: 1 addition & 1 deletion src/DoseCalculationAlgorithms/DoseCalculationAlgorithms.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

abstract type AbstractDoseAlgorithm end

include("ScaledIsoplaneKernel.jl")
#include("ScaledIsoplaneKernel.jl")
include("FinitePencilBeamKernel.jl")

"""
Expand Down
4 changes: 2 additions & 2 deletions src/DoseCalculationAlgorithms/FinitePencilBeamKernel.jl
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ function FinitePencilBeamKernel(depths, parameters::AbstractMatrix, tanθ, scali
@assert length(tanθ) == size(scalingfactor, 2)

data = SVector{5}.(eachcol(parameters))
params_interpolator = LinearInterpolation(depths, data, extrapolation_bc=Interpolations.Line())
params_interpolator = linear_interpolation(depths, data, extrapolation_bc=Interpolations.Line())

scalingfactor_interpolator = LinearInterpolation((depths, tanθ), scalingfactor, extrapolation_bc=Interpolations.Line())
scalingfactor_interpolator = linear_interpolation((depths, tanθ), scalingfactor, extrapolation_bc=Interpolations.Line())

FinitePencilBeamKernel(params_interpolator, scalingfactor_interpolator)
end
Expand Down
14 changes: 8 additions & 6 deletions src/DoseCalculationAlgorithms/ScaledIsoplaneKernel.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#
# Scaled Isoplane Kernel Algorithm
#
#=
(DISABLED) Scaled Isoplane Kernel Algorithm
This has been temporarily disabled as it is incompatible with the current API
=#

export ScaledIsoplaneKernel, kernel, norm_depth_dose, calibrate!

Expand Down Expand Up @@ -37,10 +39,10 @@ function ScaledIsoplaneKernel(filename::String, max_kernel_radius; δsub=SVector
# Interpolate onto uniform grid
Δr = kernel_radius[2]-kernel_radius[1]
r = kernel_radius[1]:Δr:kernel_radius[end]
K = LinearInterpolation(kernel_radius, kernel_value)(r)
K = linear_interpolation(kernel_radius, kernel_value)(r)

# Create interpolation kernel
kernel = LinearInterpolation(r, K, extrapolation_bc=-Inf)
kernel = linear_interpolation(r, K, extrapolation_bc=-Inf)

# Load Depth Dose Data
pdd_depth = data["Depth Dose"]["Depth"]
Expand All @@ -52,7 +54,7 @@ function ScaledIsoplaneKernel(filename::String, max_kernel_radius; δsub=SVector
# Interpolate onto uniform grid
Δd = pdd_depth[2]-pdd_depth[1]
d = pdd_depth[1]:Δd:pdd_depth[end]
D = LinearInterpolation(pdd_depth, pdd_dose)(d)
D = linear_interpolation(pdd_depth, pdd_dose)(d)

# Create interpolation PDD
PDD = CubicSplineInterpolation(d, D, extrapolation_bc=Interpolations.Line())
Expand Down
2 changes: 1 addition & 1 deletion src/DosePoints.jl
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ function within(bounds::MeshBounds, p)

pmin, _ = extent(bounds)
line = Segment(Point(pmin), Point(p))
pI, _ = intersect_mesh(line, bounds.mesh)
pI = intersect_mesh(line, bounds.mesh)

length(pI) % 2 != 0
end
Expand Down
6 changes: 3 additions & 3 deletions src/ExternalSurfaces.jl
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ getSSD(surf::MeshSurface, pos, src) = getSSD(surf, Point(pos), Point(src))

function getSSD(surf::MeshSurface, pos::Point, src::Point)
line = Ray(src, pos-src)
pI, _ = intersect_mesh(line, surf.mesh)
pI = intersect_mesh(line, surf.mesh)
length(pI)==0 && return Inf
minimum(norm.(pI .- Ref(src)))
end
Expand All @@ -134,7 +134,7 @@ struct CylindricalSurface{Ty<:AbstractVector, Tϕ<:AbstractVector, Tdist<:Abstra
distance::Tdist
I::TInterpolation
function CylindricalSurface(ϕ, y, rho)
I = LinearInterpolation((ϕ, y), rho)
I = linear_interpolation((ϕ, y), rho)
new{typeof(ϕ), typeof(y), typeof(rho), typeof(I)}(ϕ, y, rho, I)
end
end
Expand Down Expand Up @@ -165,7 +165,7 @@ function CylindricalSurface(mesh::SimpleMesh; Δϕ°=2., Δy=2.)
src = Point(SAD*sin(ϕ[i]), y[j], SAD*cos(ϕ[i]))

line = Ray(src, pos-src)
pI, _ = intersect_mesh(line, mesh)
pI = intersect_mesh(line, mesh)
if length(pI)==0
ρᵢ = Inf
else
Expand Down
9 changes: 2 additions & 7 deletions src/Structures.jl
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,13 @@ Single-threaded version of `intersect_mesh`.
"""
function intersect_mesh_single_threaded(line::Geometry, mesh::Domain{Dim, T}) where {Dim, T}
intersection_points = Point{Dim, T}[]
intersection_cells = Geometry{Dim, T}[]
for cell in mesh
pt = intersect(line, cell)
if(pt !== nothing)
push!(intersection_points, pt)
push!(intersection_cells, cell)
end
end
intersection_points, intersection_cells
intersection_points
end

"""
Expand All @@ -82,19 +80,16 @@ Multi-threaded version of `intersect_mesh`.
"""
function intersect_mesh_multi_threaded(line::Geometry, mesh::Domain{Dim, T}) where {Dim, T}
intersection_points = Vector{Vector{Point{Dim, T}}}(undef, Threads.nthreads())
intersection_cells = Vector{Vector{Geometry{Dim, T}}}(undef, Threads.nthreads())
for i=1:Threads.nthreads()
intersection_points[i] = Point{Dim, T}[]
intersection_cells[i] = Geometry{Dim, T}[]
end
Threads.@threads for cell in mesh
pt = intersect(line, cell)
if(pt !== nothing)
push!(intersection_points[Threads.threadid()], pt)
push!(intersection_cells[Threads.threadid()], cell)
end
end
vcat(intersection_points...), vcat(intersection_cells...)
vcat(intersection_points...)
end

#--- File IO ------------------------------------------------------------------
Expand Down
4 changes: 2 additions & 2 deletions src/utils/interpolation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ locate(xg, yg, xi, yi) = CartesianIndex(locate(xg, xi), locate(yg, yi))
Interpolate `xi` within uniform grid positions `xg` and grid values `fg`
"""
Base.@propagate_inbounds function interp(xg::AbstractVector{T}, fg::AbstractVector{T}, xi) where T<:Real
LinearInterpolation(xg, fg, extrapolation_bc = Flat())(xi)
linear_interpolation(xg, fg, extrapolation_bc = Interpolations.Flat())(xi)
end

"""
Expand All @@ -71,6 +71,6 @@ interp(f1, f2, α) = (1 - α)*f1 + α*f2
Bilinear interpolation at position `xi,yi`, on grid `xg-yg` with values `fg`
"""
Base.@propagate_inbounds function interp(xg::AbstractVector, yg::AbstractVector, fg::AbstractMatrix, xi, yi)
LinearInterpolation((xg, yg), fg, extrapolation_bc = Flat())(xi, yi)
linear_interpolation((xg, yg), fg, extrapolation_bc = Interpolations.Flat())(xi, yi)
end

2 changes: 0 additions & 2 deletions test/CoordinateSystems.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using Meshes

@testset "CoordinateSystems" begin

compare(v1, v2) = all(v1 .≈ v2)
Expand Down
9 changes: 3 additions & 6 deletions test/ExternalSurfaces.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using Meshes, LinearAlgebra, StaticArrays, Rotations

#=
Depth and SSD Calculation
Expand All @@ -11,8 +9,7 @@ same ray line return the same SSD.
Implemented Surfaces:
- ConstantSurface
- PlaneSurface
- MeshSurface (uses the same mesh and visual inspection as detailed in
test/meshes.jl)
- MeshSurface (uses the same mesh and visual inspection as detailed in meshes.jl)
=#

@testset "External Surfaces" begin
Expand Down Expand Up @@ -80,7 +77,7 @@ Implemented Surfaces:
end

@testset "MeshSurface" begin
structure = load_structure_from_ply("test/test_mesh.stl")
structure = load_structure_from_ply("test_mesh.stl")
surf = MeshSurface(structure)

# Test 1 - Visually inspected for accuracy
Expand All @@ -102,7 +99,7 @@ Implemented Surfaces:
end

@testset "Cylindrical Surface" begin
mesh = load_structure_from_ply("test/test_cylinder.stl")
mesh = load_structure_from_ply("test_cylinder.stl")
surf = CylindricalSurface(mesh; Δϕ°=1., Δy=1.)
meshsurf = MeshSurface(mesh)

Expand Down
4 changes: 0 additions & 4 deletions test/FinitePencilBeamKernel.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
using LinearAlgebra
using QuadGK
using HCubature

@testset "Pencil Beam Profile" begin
# Tests if profile is continuous
@testset "Continuity" begin
Expand Down
2 changes: 0 additions & 2 deletions test/Jaws.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using HDF5

@testset "HDF5" begin

jaws = Jaws(rand(2), rand(2))
Expand Down
6 changes: 6 additions & 0 deletions test/Project.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
[deps]
HCubature = "19dc6840-f33b-545b-b366-655c7e3ffd49"
HDF5 = "f67ccb44-e63f-5c2f-98bd-6dc0ccc4ba2f"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Meshes = "eacbb407-ea5a-433e-ab97-5258b1ca43fa"
QuadGK = "1fd47b50-473d-5c70-9696-f719f8f3bcdc"
Rotations = "6038ab10-8711-5258-84ad-4b1120ba62dc"
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
5 changes: 4 additions & 1 deletion test/ScaledIsoplaneKernel.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
using StaticArrays
#=
(DISABLED) Tests for the ScaledIsoplaneKernel Dose Calculation Algorithm
See src/DoseCalculationAlgorithms/ScaledIsoplaneKernel.jl
=#
@testset "ScaledIsoplaneKernel" begin

@testset "Sub-division" begin
Expand Down
2 changes: 0 additions & 2 deletions test/bixels.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using StaticArrays

@testset "Bixels" begin

pos = SVector(1., 2.)
Expand Down
18 changes: 8 additions & 10 deletions test/interpolation.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using Test

rand_in_range(x1, x2) = (x2-x1)*rand() + x1

function test_grid(xg)
Expand Down Expand Up @@ -39,12 +37,12 @@ function test_linear_interpolation(xg)

xs = [0., 1.]

@testset "f[$(i[1])] == f($(xs[i[1]]))" for i in eachindex(fg)
@test fg[i] == interp(xg, fg, xs[i])
@testset "f[$(i[1])] f($(xs[i[1]]))" for i in eachindex(fg)
@test fg[i] interp(xg, fg, xs[i])
end

@testset "avg(f) == f(0.5,0.5)" begin
@test sum(fg)/length(fg) == interp(xg, fg, 0.5)
@testset "avg(f) f(0.5,0.5)" begin
@test sum(fg)/length(fg) interp(xg, fg, 0.5)
end
end

Expand All @@ -66,15 +64,15 @@ function test_bilinear_interpolation(xg)
xs = [0., 1.]
ys = [0., 1.]

@testset "f[$(i[1]),$(i[2])] == f($(xs[i[1]]),$(ys[i[2]]))" for i in CartesianIndices(fg)
@testset "f[$(i[1]),$(i[2])] f($(xs[i[1]]),$(ys[i[2]]))" for i in CartesianIndices(fg)
xi = xs[i[1]]
yi = ys[i[2]]
fi = interp(xg, yg, fg, xi, yi)
@test fi == fg[i]
@test fi fg[i]
end

@testset "avg(f) == f(0.5,0.5)" begin
@test sum(fg)/length(fg) == interp(xg, yg, fg, 0.5, 0.5)
@testset "avg(f) f(0.5,0.5)" begin
@test sum(fg)/length(fg) interp(xg, yg, fg, 0.5, 0.5)
end
end

Expand Down
4 changes: 0 additions & 4 deletions test/meshes.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@

using Meshes
using LinearAlgebra

#=
Mesh Intersections
Expand Down
6 changes: 5 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
using Test
using DoseCalculations
using HDF5
using StaticArrays
using Meshes
using LinearAlgebra, Rotations
using QuadGK, HCubature

@testset "DoseCalculations" begin
include("utils.jl")
Expand All @@ -10,7 +14,7 @@ using HDF5
include("ExternalSurfaces.jl")
include("meshes.jl")
include("Fluence.jl")
include("ScaledIsoplaneKernel.jl")
#include("ScaledIsoplaneKernel.jl") # DISABLED, See src/DoseCalculationAlgorithms/ScaledIsoplaneKernel.jl
include("MultiLeafCollimator.jl")
include("MultiLeafCollimatorSequence.jl")
end
1 change: 0 additions & 1 deletion test/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

function test_snapped_range(x1, x2, Δ)
r = DoseCalculations.snapped_range(x1, x2, Δ)
print(r)
@test r[1]<=x1
@test x2<=r[end]
end
Expand Down

0 comments on commit 3c913a2

Please sign in to comment.