Skip to content

only print progress bar when predicted time is high #44

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "FixedEffects"
uuid = "c8885935-8500-56a7-9867-7708b20db0eb"
version = "2.0.1"
version = "2.0.2"

[deps]
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Expand Down
23 changes: 12 additions & 11 deletions src/FixedEffectSolvers/FixedEffectSolverCPU.jl
Original file line number Diff line number Diff line change
Expand Up @@ -166,23 +166,24 @@ end
function solve_residuals!(X::AbstractMatrix, feM::FixedEffects.FixedEffectSolverCPU; progress_bar = true, kwargs...)
iterations = Int[]
convergeds = Bool[]
io = stdout
if progress_bar
bar = MiniProgressBar(header = "Demean Variables:", color = Base.info_color(), percentage = false, always_reprint=true)
bar.max = size(X, 2)
showprogress(io, bar)
end
bar = MiniProgressBar(header = "Demean Variables:", color = Base.info_color(), percentage = false, max = size(X, 2))
for j in 1:size(X, 2)
v0 = time()
_, iteration, converged = solve_residuals!(view(X, :, j), feM; kwargs...)
v1 = time()
# remove progress_bar if estimated time lower than 2sec
if progress_bar && (j == 1) && ((v1 - v0) * size(X, 2) <= 2)
progress_bar = false
end
if progress_bar
bar.current = j
showprogress(stdout, bar)
end
push!(iterations, iteration)
push!(convergeds, converged)
if progress_bar
bar.current = j
showprogress(io, bar)
end
end
if progress_bar
print(io)
end_progress(stdout, bar)
end
return X, iterations, convergeds
end
Expand Down
22 changes: 12 additions & 10 deletions src/utils/progressbar.jl
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
# from Pkg.jl
Base.@kwdef mutable struct MiniProgressBar
max::Int = 1.0
max::Int = 1
header::String = ""
color::Symbol = :white
width::Int = 40
current::Int = 0.0
prev::Int = 0.0
width::Int = 32
current::Int = 0
prev::Int = 0
has_shown::Bool = false
time_shown::Float64 = 0.0
percentage::Bool = true
always_reprint::Bool = false
indent::Int = 4
end

Expand All @@ -23,11 +23,6 @@ function showprogress(io::IO, p::MiniProgressBar)
perc = p.current / p.max * 100
prev_perc = p.prev / p.max * 100
end
# Bail early if we are not updating the progress bar,
# Saves printing to the terminal
if !p.always_reprint && p.has_shown && !((perc - prev_perc) > PROGRESS_BAR_PERCENTAGE_GRANULARITY[])
return
end
if !isinteractive()
t = time()
if p.has_shown && (t - p.time_shown) < NONINTERACTIVE_TIME_GRANULARITY[]
Expand All @@ -50,4 +45,11 @@ function showprogress(io::IO, p::MiniProgressBar)
print(io, p.current, "/", p.max)
end
print(io, "\r")
end

function end_progress(io, p::MiniProgressBar)
ansi_enablecursor = "\e[?25h"
ansi_clearline = "\e[2K"
print(io, ansi_enablecursor)
print(io, ansi_clearline)
end