diff --git a/Project.toml b/Project.toml index ea527ba..4bef1b1 100644 --- a/Project.toml +++ b/Project.toml @@ -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" diff --git a/src/FixedEffectSolvers/FixedEffectSolverCPU.jl b/src/FixedEffectSolvers/FixedEffectSolverCPU.jl index 925caa0..e7f3722 100644 --- a/src/FixedEffectSolvers/FixedEffectSolverCPU.jl +++ b/src/FixedEffectSolvers/FixedEffectSolverCPU.jl @@ -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 diff --git a/src/utils/progressbar.jl b/src/utils/progressbar.jl index 22e7d1d..d600997 100644 --- a/src/utils/progressbar.jl +++ b/src/utils/progressbar.jl @@ -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 @@ -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[] @@ -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 \ No newline at end of file