Skip to content

Commit

Permalink
[ProgressBar] Fix code style.
Browse files Browse the repository at this point in the history
  • Loading branch information
geektoni committed Jun 6, 2017
1 parent c106236 commit fa9d86c
Show file tree
Hide file tree
Showing 27 changed files with 604 additions and 545 deletions.
12 changes: 6 additions & 6 deletions examples/undocumented/libshogun/classifier_latent_svm.cpp
@@ -1,11 +1,11 @@
#include <shogun/labels/LatentLabels.h>
#include <shogun/features/LatentFeatures.h>
#include <shogun/latent/LatentSVM.h>
#include <shogun/features/DenseFeatures.h>
#include <shogun/base/init.h>
#include <shogun/base/progress.h>
#include <shogun/lib/common.h>
#include <shogun/features/DenseFeatures.h>
#include <shogun/features/LatentFeatures.h>
#include <shogun/io/SGIO.h>
#include <shogun/labels/LatentLabels.h>
#include <shogun/latent/LatentSVM.h>
#include <shogun/lib/common.h>
#include <shogun/mathematics/Math.h>

#include <libgen.h>
Expand Down Expand Up @@ -114,7 +114,7 @@ static void read_dataset(char* fname, CLatentFeatures*& feats, CLatentLabels*& l
feats = new CLatentFeatures(num_examples);
SG_REF(feats);

auto pb=progress(range(num_examples));
auto pb = progress(range(num_examples));
CMath::init_random();
for (int i = 0; (!feof(fd)) && (i < num_examples); ++i)
{
Expand Down
53 changes: 27 additions & 26 deletions src/shogun/base/progress.h
Expand Up @@ -124,18 +124,17 @@ namespace shogun
}

void print_progress_absolute(
float64_t current_val, float64_t val, float64_t min_val, float64_t max_val)
float64_t current_val, float64_t val, float64_t min_val,
float64_t max_val)
{
lock.lock();
if (val - m_min_value >
m_max_value - m_min_value)
if (val - m_min_value > m_max_value - m_min_value)
{
lock.unlock();
return;
}
print_progress_absolute_impl(current_val, val, min_val, max_val);
if (val - m_min_value ==
m_max_value - m_min_value)
if (val - m_min_value == m_max_value - m_min_value)
{
print_end();
lock.unlock();
Expand All @@ -144,7 +143,6 @@ namespace shogun
lock.unlock();
}


/**
* Manually increment to max size the current value
* to print a complete progress bar.
Expand Down Expand Up @@ -264,7 +262,8 @@ namespace shogun
* Logic implementation fo the absolute progress bar.
*/
void print_progress_absolute_impl(
float64_t current_val, float64_t val, float64_t min_value, float64_t max_value) const
float64_t current_val, float64_t val, float64_t min_value,
float64_t max_value) const
{
// Check if the progress was enabled
if (!m_io.get_show_progress())
Expand All @@ -279,15 +278,15 @@ namespace shogun
// a minimal resize functionality.
set_screen_size();

float64_t difference = max_value - min_value, v = -1,
estimate = 0, total_estimate = 0;
float64_t difference = max_value - min_value, v = -1, estimate = 0,
total_estimate = 0;
float64_t size_chunk = -1;

// Check if we have enough space to show the progress bar
// Use only a fraction of it to account for the size of the
// time displayed (decimals and integer).
int32_t progress_bar_space =
(m_columns_num - 50 - m_prefix.length()) * 0.9;
(m_columns_num - 50 - m_prefix.length()) * 0.9;

// TODO: this guy here brokes testing
// REQUIRE(
Expand All @@ -298,8 +297,7 @@ namespace shogun
float64_t runtime = CTime::get_curtime();

if (difference > 0.0)
v = 100 * (val - min_value) /
(max_value - min_value);
v = 100 * (val - min_value) / (max_value - min_value);

// Set up chunk size
size_chunk = difference / (float64_t)progress_bar_space;
Expand All @@ -318,10 +316,10 @@ namespace shogun

m_last_progress_time = runtime;
estimate = (1 - v / 100) *
(m_last_progress_time - m_progress_start_time) /
(v / 100);
(m_last_progress_time - m_progress_start_time) /
(v / 100);
total_estimate =
(m_last_progress_time - m_progress_start_time) / (v / 100);
(m_last_progress_time - m_progress_start_time) / (v / 100);
}

/** Print the actual progress bar to screen **/
Expand All @@ -331,8 +329,8 @@ namespace shogun
if (m_current_value.load() - min_value > i * size_chunk)
{
m_io.message(
MSG_MESSAGEONLY, "", "", -1, "%s",
get_pb_char().c_str());
MSG_MESSAGEONLY, "", "", -1, "%s",
get_pb_char().c_str());
}
else
{
Expand All @@ -344,19 +342,19 @@ namespace shogun
if (estimate > 120)
{
snprintf(
str, sizeof(str),
" %%1.1f minutes remaining %%1.1f minutes total\r");
str, sizeof(str),
" %%1.1f minutes remaining %%1.1f minutes total\r");
m_io.message(
MSG_MESSAGEONLY, "", "", -1, str, estimate / 60,
total_estimate / 60);
MSG_MESSAGEONLY, "", "", -1, str, estimate / 60,
total_estimate / 60);
}
else
{
snprintf(
str, sizeof(str),
" %%1.1f seconds remaining %%1.1f seconds total\r");
str, sizeof(str),
" %%1.1f seconds remaining %%1.1f seconds total\r");
m_io.message(
MSG_MESSAGEONLY, "", "", -1, str, estimate, total_estimate);
MSG_MESSAGEONLY, "", "", -1, str, estimate, total_estimate);
}
}

Expand Down Expand Up @@ -630,9 +628,12 @@ namespace shogun
* @param min_val minimum value
* @param max_val maximum value
*/
void print_absolute(float64_t current_val, float64_t val, float64_t min_value, float64_t max_value) const
void print_absolute(
float64_t current_val, float64_t val, float64_t min_value,
float64_t max_value) const
{
m_printer->print_progress_absolute(current_val, val, min_value, max_value);
m_printer->print_progress_absolute(
current_val, val, min_value, max_value);
}

/**
Expand Down
30 changes: 18 additions & 12 deletions src/shogun/classifier/svm/LibLinear.cpp
Expand Up @@ -10,15 +10,15 @@
*/
#include <shogun/lib/config.h>

#include <shogun/io/SGIO.h>
#include <shogun/lib/Signal.h>
#include <shogun/lib/Time.h>
#include <shogun/base/Parameter.h>
#include <shogun/base/progress.h>
#include <shogun/classifier/svm/LibLinear.h>
#include <shogun/optimization/liblinear/tron.h>
#include <shogun/features/DotFeatures.h>
#include <shogun/io/SGIO.h>
#include <shogun/labels/BinaryLabels.h>
#include <shogun/base/progress.h>
#include <shogun/lib/Signal.h>
#include <shogun/lib/Time.h>
#include <shogun/optimization/liblinear/tron.h>

using namespace shogun;

Expand Down Expand Up @@ -393,7 +393,8 @@ void CLibLinear::solve_l2r_l1l2_svc(

iter++;
float64_t gap=PGmax_new - PGmin_new;
pb.print_absolute(gap, -CMath::log10(gap), -CMath::log10(1), -CMath::log10(eps));
pb.print_absolute(
gap, -CMath::log10(gap), -CMath::log10(1), -CMath::log10(eps));

if(gap <= eps)
{
Expand Down Expand Up @@ -743,8 +744,9 @@ void CLibLinear::solve_l1r_l2_svc(
Gmax_init = Gmax_new;
iter++;

pb.print_absolute(Gmax_new, -CMath::log10(Gmax_new),
-CMath::log10(Gmax_init), -CMath::log10(eps*Gmax_init));
pb.print_absolute(
Gmax_new, -CMath::log10(Gmax_new), -CMath::log10(Gmax_init),
-CMath::log10(eps * Gmax_init));

if(Gmax_new <= eps*Gmax_init)
{
Expand Down Expand Up @@ -894,7 +896,7 @@ void CLibLinear::solve_l1r_lr(
}
}

auto pb=progress(range(10));
auto pb = progress(range(10));
CTime start_time;
while (iter < max_iterations && !CSignal::cancel_computations())
{
Expand Down Expand Up @@ -1107,7 +1109,9 @@ void CLibLinear::solve_l1r_lr(
if(iter == 0)
Gmax_init = Gmax_new;
iter++;
pb.print_absolute(Gmax_new, -CMath::log10(Gmax_new), -CMath::log10(Gmax_init), -CMath::log10(eps*Gmax_init));
pb.print_absolute(
Gmax_new, -CMath::log10(Gmax_new), -CMath::log10(Gmax_init),
-CMath::log10(eps * Gmax_init));

if(Gmax_new <= eps*Gmax_init)
{
Expand Down Expand Up @@ -1232,7 +1236,7 @@ void CLibLinear::solve_l2r_lr_dual(SGVector<float64_t>& w, const liblinear_probl
index[i] = i;
}

auto pb=progress(range(10));
auto pb = progress(range(10));
while (iter < max_iter)
{
for (i=0; i<l; i++)
Expand Down Expand Up @@ -1307,7 +1311,9 @@ void CLibLinear::solve_l2r_lr_dual(SGVector<float64_t>& w, const liblinear_probl
Gmax_init = Gmax;
iter++;

pb.print_absolute(Gmax, -CMath::log10(Gmax), -CMath::log10(Gmax_init), -CMath::log10(eps*Gmax_init));
pb.print_absolute(
Gmax, -CMath::log10(Gmax), -CMath::log10(Gmax_init),
-CMath::log10(eps * Gmax_init));

if(Gmax < eps)
break;
Expand Down
8 changes: 5 additions & 3 deletions src/shogun/classifier/svm/SVMLight.cpp
Expand Up @@ -642,7 +642,7 @@ int32_t CSVMLight::optimize_to_convergence(int32_t* docs, int32_t* label, int32_
CTime start_time;
mkl_converged=false;

auto pb = progress(range(10), *this->io);
auto pb = progress(range(10), *this->io);
#ifdef CYGWIN
for (;((iteration<100 || (!mkl_converged && callback) ) || (retrain && (!terminate))); iteration++){
#else
Expand Down Expand Up @@ -903,7 +903,9 @@ int32_t CSVMLight::optimize_to_convergence(int32_t* docs, int32_t* label, int32_
if (bestmaxdiff>worstmaxdiff)
worstmaxdiff=bestmaxdiff;

pb.print_absolute(bestmaxdiff, -CMath::log10(bestmaxdiff), -CMath::log10(worstmaxdiff), -CMath::log10(epsilon));
pb.print_absolute(
bestmaxdiff, -CMath::log10(bestmaxdiff), -CMath::log10(worstmaxdiff),
-CMath::log10(epsilon));

/* Terminate loop */
if (m_max_train_time > 0 && start_time.cur_time_diff() > m_max_train_time) {
Expand All @@ -912,7 +914,7 @@ int32_t CSVMLight::optimize_to_convergence(int32_t* docs, int32_t* label, int32_
}

} /* end of loop */
pb.complete_absolute();
pb.complete_absolute();

SG_DEBUG("inactive:%d\n", inactivenum)

Expand Down
10 changes: 5 additions & 5 deletions src/shogun/clustering/Hierarchical.cpp
Expand Up @@ -8,13 +8,13 @@
* Copyright (C) 2007-2009 Fraunhofer Institute FIRST and Max-Planck-Society
*/

#include <shogun/base/Parallel.h>
#include <shogun/base/progress.h>
#include <shogun/clustering/Hierarchical.h>
#include <shogun/distance/Distance.h>
#include <shogun/labels/Labels.h>
#include <shogun/features/Features.h>
#include <shogun/labels/Labels.h>
#include <shogun/mathematics/Math.h>
#include <shogun/base/Parallel.h>
#include <shogun/base/progress.h>

using namespace shogun;

Expand Down Expand Up @@ -91,14 +91,14 @@ bool CHierarchical::train_machine(CFeatures* data)
distances[offs] = distance->distance(i, j);
index[offs].idx1 = i;
index[offs].idx2 = j;
offs++; //offs=i*(i+1)/2+j
offs++; // offs=i*(i+1)/2+j
}
}

CMath::qsort_index<float64_t,pair>(distances, index, (num-1)*num/2);
//CMath::display_vector(distances, (num-1)*num/2, "dists");

auto pb = progress(range(0, num_pairs-1), *this->io);
auto pb = progress(range(0, num_pairs - 1), *this->io);
int32_t k=-1;
int32_t l=0;
for (; l<num && (num-l)>=merges && k<num_pairs-1; l++)
Expand Down
15 changes: 8 additions & 7 deletions src/shogun/distance/Distance.cpp
Expand Up @@ -9,15 +9,15 @@
* Copyright (C) 2006-2009 Fraunhofer Institute FIRST and Max-Planck-Society
*/

#include <shogun/lib/config.h>
#include <shogun/lib/common.h>
#include <shogun/io/SGIO.h>
#include <shogun/io/File.h>
#include <shogun/lib/Time.h>
#include <shogun/lib/Signal.h>
#include <shogun/base/Parallel.h>
#include <shogun/base/Parameter.h>
#include <shogun/base/progress.h>
#include <shogun/io/File.h>
#include <shogun/io/SGIO.h>
#include <shogun/lib/Signal.h>
#include <shogun/lib/Time.h>
#include <shogun/lib/common.h>
#include <shogun/lib/config.h>

#include <shogun/distance/Distance.h>
#include <shogun/features/Features.h>
Expand Down Expand Up @@ -285,7 +285,8 @@ SGMatrix<T> CDistance::get_distance_matrix()

result=SG_MALLOC(T, total_num);

PRange<int64_t> pb = PRange<int64_t>(range(total_num), *this->io, "PROGRESS: ", UTF8, [](){return true;});
PRange<int64_t> pb = PRange<int64_t>(
range(total_num), *this->io, "PROGRESS: ", UTF8, []() { return true; });
int32_t num_threads;
int64_t step;
#pragma omp parallel shared(num_threads, step)
Expand Down
2 changes: 1 addition & 1 deletion src/shogun/features/DotFeatures.cpp
Expand Up @@ -68,7 +68,7 @@ void CDotFeatures::dense_dot_range(float64_t* output, int32_t start, int32_t sto
int32_t num_threads;
int32_t step;
auto pb = progress(range(num_vectors), *this->io);
#pragma omp parallel shared(num_threads, step)
#pragma omp parallel shared(num_threads, step)
{
#ifdef HAVE_OPENMP
#pragma omp single
Expand Down
15 changes: 9 additions & 6 deletions src/shogun/features/StringFeatures.cpp
@@ -1,12 +1,12 @@
#include <shogun/base/Parameter.h>
#include <shogun/base/progress.h>
#include <shogun/features/StringFeatures.h>
#include <shogun/preprocessor/Preprocessor.h>
#include <shogun/preprocessor/StringPreprocessor.h>
#include <shogun/io/MemoryMappedFile.h>
#include <shogun/io/SGIO.h>
#include <shogun/mathematics/Math.h>
#include <shogun/base/Parameter.h>
#include <shogun/lib/SGStringList.h>
#include <shogun/base/progress.h>
#include <shogun/mathematics/Math.h>
#include <shogun/preprocessor/Preprocessor.h>
#include <shogun/preprocessor/StringPreprocessor.h>

#include <sys/types.h>
#include <sys/stat.h>
Expand Down Expand Up @@ -516,7 +516,10 @@ template<class ST> void CStringFeatures<ST>::load_ascii_file(char* fname, bool r
overflow=SG_MALLOC(uint8_t, blocksize);
features=SG_MALLOC(SGString<ST>, num_vectors);

auto pb2 = PRange<int>(range(num_vectors), *this->io, "LOADING: ", UTF8, [](){return true;});
auto pb2 =
PRange<int>(range(num_vectors), *this->io, "LOADING: ", UTF8, []() {
return true;
});
rewind(f);
sz=blocksize;
int32_t lines=0;
Expand Down
4 changes: 2 additions & 2 deletions src/shogun/features/hashed/HashedWDFeaturesTransposed.cpp
Expand Up @@ -240,8 +240,8 @@ void CHashedWDFeaturesTransposed::dense_dot_range(float64_t* output, int32_t sta
params.vec=vec;
params.bias=b;
params.progress=false; //true;
params.progress_bar=&pb
params.index=index;
params.progress_bar = &pb;
params.index = index;
dense_dot_range_helper((void*) &params);
pb.complete();
}
Expand Down

0 comments on commit fa9d86c

Please sign in to comment.