Skip to content

Commit

Permalink
test for vline, hline, and circle and all the results of plots
Browse files Browse the repository at this point in the history
  • Loading branch information
warisa-r committed Sep 20, 2024
1 parent 1726d0e commit dcea267
Show file tree
Hide file tree
Showing 22 changed files with 322 additions and 0 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
120 changes: 120 additions & 0 deletions sandbox/parameter_init_circle.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
using LsqFit
using Plots # Import the Plots library
using BenchmarkTools # Import the BenchmarkTools library
using Interpolations
using LinearAlgebra

# circle model function
function circle_model(xy, p)
x0, y0, r = p
x = xy[:, 1]
y = xy[:, 2]
return (x .- x0).^2 + (y .- y0).^2 .- r^2
end

function fit_circle_with_random_guess(xy)
# Random initial guess for circle fitting
p0 = rand(3) # Random initial guesses for a, b, c
println("Random initial guess: ", p0)

# Perform the fit
fit = curve_fit(circle_model, xy, zeros(size(xy, 1)), p0)

# Calculate error (sum of squared residuals)
fit_error = sum(fit.resid .^ 2)

return p0, fit, fit_error
end

# Improved function to fit a circle with a better initial guess
function fit_circle_with_improved_guess(xy)

# Improved initial guess for circle fitting
x = xy[:, 1]
y = xy[:, 2]

p0 = [mean(x), mean(y), sqrt(mean((x .- mean(x)).^2 .+ (y .- mean(y)).^2))]

# Perform the fit
fit = curve_fit(circle_model, xy, zeros(size(xy, 1)), p0)

# Calculate error (sum of squared residuals)
fit_error = sum(fit.resid .^ 2)

return p0, fit, fit_error
end

# Function to generate noisy circular data
function generate_noisy_circular_data(p, noise_level)
x0, y0, r = p
num_points = 100

θ = LinRange(0, 2π, num_points)
x = x0 .+ r .* cos.(θ) .+ noise_level .* randn(num_points)
y = y0 .+ r .* sin.(θ) .+ noise_level .* randn(num_points)
# Add noise
y_noisy = y .+ randn(length(y)) * noise_level # Adjust noise level as needed

# Combine into xy matrix
xy = hcat(x, y_noisy)
return xy
end

# Define the parameter combinations to test
# Define the parameter combinations to test
param_combinations = [
[49, -49, 0.1],
[0.005, 0.005, 10],
[1, 1, 20],
[25, 25, 15],
[-25, -25, 30],
[50, 50, 5],
[-50, -50, 10]
]

noise_levels = [0.0, 1.0, 5.0, 7.5, 10.0]

for (i, params) in enumerate(param_combinations)
results = []
for noise_level in noise_levels
xy = generate_noisy_circular_data(params, noise_level)

# Benchmark improved initial guess
improved_benchmark = @benchmark fit_circle_with_improved_guess($xy)
improved_initial_guess, improved_fit, _ = fit_circle_with_improved_guess(xy)
improved_error = norm(improved_fit.param - params)

# Benchmark random initial guess
random_benchmark = @benchmark fit_circle_with_random_guess($xy)
random_initial_guess, random_fit, _ = fit_circle_with_random_guess(xy)
random_error = norm(random_fit.param - params)

push!(results, (noise_level, improved_benchmark, improved_error, random_benchmark, random_error))
end

# Extract time and memory allocation data
improved_times = [minimum(r[2]).time / 1e9 for r in results] # Convert to seconds
improved_memories = [minimum(r[2]).memory / 1024 for r in results] # Convert to KB
random_times = [minimum(r[4]).time / 1e9 for r in results] # Convert to seconds
random_memories = [minimum(r[4]).memory / 1024 for r in results] # Convert to KB
improved_errors = [r[3] for r in results]
random_errors = [r[5] for r in results]

# Plot the results once for all noise levels
p = plot(layout = (3, 1), size = (800, 1200))

# Time plot
plot!(p[1], noise_levels, improved_times, label="Improved Time (s)", xlabel="Noise Level", ylabel="Time (s)", legend=:topright, title="Time Comparison (Params: $params)")
plot!(p[1], noise_levels, random_times, label="Random Time (s)", xlabel="Noise Level", ylabel="Time (s)", legend=:topright)

# Memory plot
plot!(p[2], noise_levels, improved_memories, label="Improved Memory (KB)", xlabel="Noise Level", ylabel="Memory (KB)", legend=:topright, title="Memory Comparison (Params: $params)")
plot!(p[2], noise_levels, random_memories, label="Random Memory (KB)", xlabel="Noise Level", ylabel="Memory (KB)", legend=:topright)

# Error plot
plot!(p[3], noise_levels, improved_errors, label="Improved Error", xlabel="Noise Level", ylabel="Error", legend=:topright, title="Error Comparison (Params: $params)")
plot!(p[3], noise_levels, random_errors, label="Random Error", xlabel="Noise Level", ylabel="Error", legend=:topright)

# Save the plot to a file
savefig(p, "sandbox/param_performance_results/circle/benchmark_results_params_$(i)_noise.png")
end
102 changes: 102 additions & 0 deletions sandbox/parameter_init_hline.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
using LsqFit
using Plots # Import the Plots library
using BenchmarkTools # Import the BenchmarkTools library
using Interpolations
using LinearAlgebra

function hline_model(xy, p)
b = p
y = xy[:, 2]
return y .- b
end
# Function to fit a hline with a random initial guess
function fit_hline_with_random_guess(xy)
p0 = rand(1) # Random initial guesses for m, b
println("Random initial guess: ", p0)

fit = curve_fit(hline_model, xy, zeros(size(xy, 1)), p0)
fit_error = sum(fit.resid .^ 2)

return p0, fit, fit_error
end

# Improved function to fit a hline with a better initial guess
function fit_hline_with_improved_guess(xy)
x_range = LinRange(-50, 50, 100)
y_range = LinRange(-50, 50, 100)
x = xy[:, 1]
y = xy[:, 2]

b_guess = mean(y)

p0 = [b_guess]

fit = curve_fit(hline_model, xy, zeros(size(xy, 1)), p0)
fit_error = sum(fit.resid .^ 2)

return p0, fit, fit_error
end

# Function to generate noisy hlinear data
function generate_noisy_hlinear_data(p, noise_level)
b = p[1]
x = range(-50, 50, length=100)
y = [b for _ in x]

y_noisy = y .+ randn(length(y)) * noise_level
xy = hcat(x, y_noisy)
return xy
end

# Define the parameter combinations to test
param_combinations = [
[0.005],
[0.5],
[1],
[5],
[10],
[50],
]

noise_levels = [0.0, 1.0, 5.0, 7.5, 10.0]

# Define the output path
output_path = "sandbox/param_performance_results/hline"

for (i, params) in enumerate(param_combinations)
results = []
for noise_level in noise_levels
xy = generate_noisy_hlinear_data(params, noise_level)

improved_benchmark = @benchmark fit_hline_with_improved_guess($xy)
improved_initial_guess, improved_fit, _ = fit_hline_with_improved_guess(xy)
improved_error = norm(improved_fit.param - params)

random_benchmark = @benchmark fit_hline_with_random_guess($xy)
random_initial_guess, random_fit, _ = fit_hline_with_random_guess(xy)
random_error = norm(random_fit.param - params)

push!(results, (noise_level, improved_benchmark, improved_error, random_benchmark, random_error))
end

improved_times = [minimum(r[2]).time / 1e9 for r in results]
improved_memories = [minimum(r[2]).memory / 1024 for r in results]
random_times = [minimum(r[4]).time / 1e9 for r in results]
random_memories = [minimum(r[4]).memory / 1024 for r in results]
improved_errors = [r[3] for r in results]
random_errors = [r[5] for r in results]

p = plot(layout=(3, 1), size=(800, 1200))

plot!(p[1], noise_levels, improved_times, label="Improved Time (s)", xlabel="Noise Level", ylabel="Time (s)", legend=:topright, title="Time Comparison (Params: $params)")
plot!(p[1], noise_levels, random_times, label="Random Time (s)", xlabel="Noise Level", ylabel="Time (s)", legend=:topright)

plot!(p[2], noise_levels, improved_memories, label="Improved Memory (KB)", xlabel="Noise Level", ylabel="Memory (KB)", legend=:topright, title="Memory Comparison (Params: $params)")
plot!(p[2], noise_levels, random_memories, label="Random Memory (KB)", xlabel="Noise Level", ylabel="Memory (KB)", legend=:topright)

plot!(p[3], noise_levels, improved_errors, label="Improved Error", xlabel="Noise Level", ylabel="Error", legend=:topright, title="Error Comparison (Params: $params)")
plot!(p[3], noise_levels, random_errors, label="Random Error", xlabel="Noise Level", ylabel="Error", legend=:topright)

# Save the plot to the specified path
savefig(p, "$(output_path)_params_$(i)_noise.png")
end
100 changes: 100 additions & 0 deletions sandbox/parameter_init_vline.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
using LsqFit
using Plots # Import the Plots library
using BenchmarkTools # Import the BenchmarkTools library
using Interpolations
using LinearAlgebra

function vline_model(xy, p)
c = p
x = xy[:, 1]
return x .- c
end

# Function to fit a vline with a random initial guess
function fit_vline_with_random_guess(xy)
p0 = rand(1) # Random initial guesses for m, b
println("Random initial guess: ", p0)

fit = curve_fit(vline_model, xy, zeros(size(xy, 1)), p0)
fit_error = sum(fit.resid .^ 2)

return p0, fit, fit_error
end

# Improved function to fit a vline with a better initial guess
function fit_vline_with_improved_guess(xy)
x_range = LinRange(-50, 50, 100)
y_range = LinRange(-50, 50, 100)
x = xy[:, 1]
y = xy[:, 2]

c_guess = mean(x)

p0 = [c_guess]

fit = curve_fit(vline_model, xy, zeros(size(xy, 1)), p0)
fit_error = sum(fit.resid .^ 2)

return p0, fit, fit_error
end

# Function to generate noisy vlinear data
function generate_noisy_vlinear_data(p, noise_level)
c = p[1]
y = range(-50, 50, length=100)
x = [c for _ in y]

y_noisy = y .+ randn(length(y)) * noise_level
xy = hcat(x, y_noisy)
return xy
end

# Define the parameter combinations to test
param_combinations = [
[0.005],
[0.5],
[1],
[5],
[10],
[50],
]

noise_levels = [0.0, 1.0, 5.0, 7.5, 10.0]

for (i, params) in enumerate(param_combinations)
results = []
for noise_level in noise_levels
xy = generate_noisy_vlinear_data(params, noise_level)

improved_benchmark = @benchmark fit_vline_with_improved_guess($xy)
improved_initial_guess, improved_fit, _ = fit_vline_with_improved_guess(xy)
improved_error = norm(improved_fit.param - params)

random_benchmark = @benchmark fit_vline_with_random_guess($xy)
random_initial_guess, random_fit, _ = fit_vline_with_random_guess(xy)
random_error = norm(random_fit.param - params)

push!(results, (noise_level, improved_benchmark, improved_error, random_benchmark, random_error))
end

improved_times = [minimum(r[2]).time / 1e9 for r in results]
improved_memories = [minimum(r[2]).memory / 1024 for r in results]
random_times = [minimum(r[4]).time / 1e9 for r in results]
random_memories = [minimum(r[4]).memory / 1024 for r in results]
improved_errors = [r[3] for r in results]
random_errors = [r[5] for r in results]

p = plot(layout=(3, 1), size=(800, 1200))

plot!(p[1], noise_levels, improved_times, label="Improved Time (s)", xlabel="Noise Level", ylabel="Time (s)", legend=:topright, title="Time Comparison (Params: $params)")
plot!(p[1], noise_levels, random_times, label="Random Time (s)", xlabel="Noise Level", ylabel="Time (s)", legend=:topright)

plot!(p[2], noise_levels, improved_memories, label="Improved Memory (KB)", xlabel="Noise Level", ylabel="Memory (KB)", legend=:topright, title="Memory Comparison (Params: $params)")
plot!(p[2], noise_levels, random_memories, label="Random Memory (KB)", xlabel="Noise Level", ylabel="Memory (KB)", legend=:topright)

plot!(p[3], noise_levels, improved_errors, label="Improved Error", xlabel="Noise Level", ylabel="Error", legend=:topright, title="Error Comparison (Params: $params)")
plot!(p[3], noise_levels, random_errors, label="Random Error", xlabel="Noise Level", ylabel="Error", legend=:topright)

# Save the plot to the specified path
savefig(p, "sandbox/param_performance_results/vline/benchmark_results_params_$(i)_noise.png")
end

0 comments on commit dcea267

Please sign in to comment.