Skip to content

Commit

Permalink
Merge pull request #23 from gjasny/abseil-statusor
Browse files Browse the repository at this point in the history
Replace protobuf-private StatusOr with abseil-cpp
  • Loading branch information
mchinen authored Oct 15, 2020
2 parents 54ecac3 + 703a27a commit 852a12a
Show file tree
Hide file tree
Showing 38 changed files with 196 additions and 158 deletions.
2 changes: 2 additions & 0 deletions BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ cc_library(
"@com_google_absl//absl/flags:usage",
"@com_google_absl//absl/flags:parse",
"@com_google_absl//absl/memory",
"@com_google_absl//absl/status",
"@com_google_absl//absl/status:statusor",
"@com_google_absl//absl/synchronization",
"@com_google_absl//absl/types:span",
"@com_google_protobuf//:protobuf_lite",
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ int main(int argc, char **argv) {
// Create an instance of the ViSQOL API. A new instance
// is required for each signal pair to be compared.
Visqol::VisqolApi visqol;
google::protobuf::util::Status status = visqol.Create(config);
absl::Status status = visqol.Create(config);

// Ensure that the creation succeeded.
if (!status.ok()) {
Expand All @@ -197,7 +197,7 @@ int main(int argc, char **argv) {
}

// Perform the comparison.
google::protobuf::util::StatusOr<Visqol::SimilarityResultMsg> comparison_status_or =
absl::StatusOr<Visqol::SimilarityResultMsg> comparison_status_or =
visqol.Measure(reference_signal, degraded_signal);

// Ensure that the comparison succeeded.
Expand All @@ -207,7 +207,7 @@ int main(int argc, char **argv) {
}

// Extract the comparison result from the StatusOr.
Visqol::SimilarityResultMsg similarity_result = comparison_status_or.ValueOrDie();
Visqol::SimilarityResultMsg similarity_result = comparison_status_or.value();

// Get the "Mean Opinion Score - Listening Quality Objective" for the degraded
// signal, following the comparison to the reference signal.
Expand Down
2 changes: 1 addition & 1 deletion WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ protobuf_deps()
git_repository(
name = "com_google_absl",
remote = "https://github.com/abseil/abseil-cpp.git",
tag = "20200225.2",
tag = "20200923",
)

# LIBSVM
Expand Down
12 changes: 6 additions & 6 deletions src/commandline_parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
#include "absl/flags/flag.h"
#include "absl/flags/parse.h"
#include "absl/flags/usage.h"
#include "google/protobuf/stubs/statusor.h"
#include "absl/status/statusor.h"

ABSL_FLAG(std::string, reference_file, "",
"The wav file path used as the reference audio.");
Expand Down Expand Up @@ -80,7 +80,7 @@ ABSL_CONST_INIT const char kDefaultSpeechModelFile[] =
"/model/tcdvoip_nu.568_c5.31474325639_g3.17773760038_model.txt";


google::protobuf::util::StatusOr<CommandLineArgs> VisqolCommandLineParser::Parse
absl::StatusOr<CommandLineArgs> VisqolCommandLineParser::Parse
(int argc, char **argv) {
absl::SetProgramUsageMessage(
"Perceptual quality estimator for speech and audio");
Expand Down Expand Up @@ -121,8 +121,8 @@ google::protobuf::util::StatusOr<CommandLineArgs> VisqolCommandLineParser::Parse
debug_output = absl::GetFlag(FLAGS_output_debug);

if (errorFound) {
return google::protobuf::util::Status(
google::protobuf::util::error::Code::INVALID_ARGUMENT,
return absl::Status(
absl::StatusCode::kInvalidArgument,
"Invalid command line arg detected. Run with --helpfull for usage.");
}
if (sim_to_qual_model.empty()) {
Expand All @@ -132,8 +132,8 @@ google::protobuf::util::StatusOr<CommandLineArgs> VisqolCommandLineParser::Parse
sim_to_qual_model = FilePath::currentWorkingDir() + kDefaultAudioModelFile;
}
if (!sim_to_qual_model.empty() && !FileExists(sim_to_qual_model)) {
return google::protobuf::util::Status(
google::protobuf::util::error::Code::INVALID_ARGUMENT,
return absl::Status(
absl::StatusCode::kInvalidArgument,
"Failed to load the default SVR model " + sim_to_qual_model + ". "
"Specify the correct path using '--similarity_to_quality_model"
" <path/to/libsvm_nu_svr_model.txt>'?");
Expand Down
16 changes: 8 additions & 8 deletions src/comparison_patches_selector.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
#include <vector>

#include "absl/base/internal/raw_logging.h"
#include "google/protobuf/stubs/status.h"
#include "google/protobuf/stubs/statusor.h"
#include "absl/status/status.h"
#include "absl/status/statusor.h"

#include "alignment.h"
#include "amatrix.h"
Expand Down Expand Up @@ -81,7 +81,7 @@ PatchSimilarityResult ComparisonPatchesSelector::FindMostSimilarDegPatch(
return res;
}

google::protobuf::util::StatusOr<std::vector<PatchSimilarityResult>>
absl::StatusOr<std::vector<PatchSimilarityResult>>
ComparisonPatchesSelector::FindMostSimilarDegPatches(
const std::vector<ImagePatch>& ref_patches,
const std::vector<size_t>& ref_patch_indices,
Expand All @@ -95,8 +95,8 @@ ComparisonPatchesSelector::FindMostSimilarDegPatches(
num_frames_in_deg_spectro, num_frames_per_patch);

if (!num_patches) {
return google::protobuf::util::Status(
google::protobuf::util::error::Code::CANCELLED ,
return absl::Status(
absl::StatusCode::kCancelled,
"Degraded file was too short, different, or misaligned to score any "
"of the reference patches.");
} else if (num_patches < ref_patch_indices.size()) {
Expand Down Expand Up @@ -194,7 +194,7 @@ AudioSignal ComparisonPatchesSelector::Slice(
return sliced_signal;
}

google::protobuf::util::StatusOr<std::vector<PatchSimilarityResult>>
absl::StatusOr<std::vector<PatchSimilarityResult>>
ComparisonPatchesSelector::FinelyAlignAndRecreatePatches(
const std::vector<PatchSimilarityResult>& sim_results,
const AudioSignal &ref_signal,
Expand Down Expand Up @@ -230,7 +230,7 @@ ComparisonPatchesSelector::FinelyAlignAndRecreatePatches(
ref_spectro_result.status().ToString().c_str());
return ref_spectro_result.status();
}
Spectrogram ref_spectrogram = ref_spectro_result.ValueOrDie();
Spectrogram ref_spectrogram = ref_spectro_result.value();

const auto deg_spectro_result = spect_builder->Build(deg_audio_aligned,
window);
Expand All @@ -239,7 +239,7 @@ ComparisonPatchesSelector::FinelyAlignAndRecreatePatches(
deg_spectro_result.status().ToString().c_str());
return deg_spectro_result.status();
}
Spectrogram deg_spectrogram = deg_spectro_result.ValueOrDie();
Spectrogram deg_spectrogram = deg_spectro_result.value();

MiscAudio::PrepareSpectrogramsForComparison(ref_spectrogram,
deg_spectrogram);
Expand Down
6 changes: 3 additions & 3 deletions src/gammatone_spectrogram_builder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ GammatoneSpectrogramBuilder::GammatoneSpectrogramBuilder(
const GammatoneFilterBank &filter_bank, const bool use_speech_mode) :
filter_bank_(filter_bank), speech_mode_(use_speech_mode) {}

google::protobuf::util::StatusOr<Spectrogram> GammatoneSpectrogramBuilder::Build
absl::StatusOr<Spectrogram> GammatoneSpectrogramBuilder::Build
(const AudioSignal &signal,
const AnalysisWindow &window) {
const auto &sig = signal.data_matrix;
Expand All @@ -56,8 +56,8 @@ google::protobuf::util::StatusOr<Spectrogram> GammatoneSpectrogramBuilder::Build

// ensure that the signal is large enough.
if (sig.NumRows() <= window.size) {
return google::protobuf::util::Status(
google::protobuf::util::error::Code::INVALID_ARGUMENT,
return absl::Status(
absl::StatusCode::kInvalidArgument,
"Too few samples ("+std::to_string(sig.NumRows())+") in signal to build"
" spectrogram ("+std::to_string(hop_size)+" required minimum).");
}
Expand Down
10 changes: 5 additions & 5 deletions src/image_patch_creator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@
#include <vector>

#include "absl/base/internal/raw_logging.h"
#include "google/protobuf/stubs/statusor.h"
#include "absl/status/statusor.h"

#include "amatrix.h"
#include "analysis_window.h"
#include "audio_signal.h"

namespace Visqol {
google::protobuf::util::StatusOr<std::vector<size_t>>
absl::StatusOr<std::vector<size_t>>
ImagePatchCreator::CreateRefPatchIndices(
const AMatrix<double> &spectrogram, const AudioSignal &ref_signal,
const AnalysisWindow &window) const {
Expand All @@ -48,16 +48,16 @@ std::vector<ImagePatch> ImagePatchCreator::CreatePatchesFromIndices(
return patches;
}

google::protobuf::util::StatusOr<std::vector<size_t>>
absl::StatusOr<std::vector<size_t>>
ImagePatchCreator::CreateRefPatchIndices(
const AMatrix<double> &spectrogram) const {
std::vector<size_t> refPatchIndices;
auto spectrum_length = spectrogram.NumCols();
auto init_patch_index = patch_size_ / 2;
// Ensure that the spectrum is at least as big as a single patch
if (spectrum_length < patch_size_ + init_patch_index) {
return google::protobuf::util::Status(
google::protobuf::util::error::Code::INVALID_ARGUMENT, "Reference spectrum "
return absl::Status(
absl::StatusCode::kInvalidArgument, "Reference spectrum "
"size (" + std::to_string(spectrum_length) + ") smaller than minimum "
"patch size (" + std::to_string(patch_size_ + init_patch_index) + ").");
}
Expand Down
4 changes: 2 additions & 2 deletions src/include/commandline_parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#include <vector>
#include <utility>

#include "google/protobuf/stubs/statusor.h"
#include "absl/status/statusor.h"

#include "file_path.h"

Expand Down Expand Up @@ -130,7 +130,7 @@ class VisqolCommandLineParser {
* @return A struct which holds the parsed args, if parsing was successful.
* Else an error status.
*/
static google::protobuf::util::StatusOr<CommandLineArgs> Parse(int argc,
static absl::StatusOr<CommandLineArgs> Parse(int argc,
char **argv);

/**
Expand Down
6 changes: 3 additions & 3 deletions src/include/comparison_patches_selector.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
#include <memory>
#include <vector>

#include "google/protobuf/stubs/statusor.h"
#include "absl/status/statusor.h"

#include "amatrix.h"
#include "image_patch_creator.h"
Expand Down Expand Up @@ -60,7 +60,7 @@ class ComparisonPatchesSelector {
* of the comparison between a patch from the reference spectrogram with
* its corresponding patch in the degraded spectrogram.
*/
google::protobuf::util::StatusOr<std::vector<PatchSimilarityResult>>
absl::StatusOr<std::vector<PatchSimilarityResult>>
FindMostSimilarDegPatches(const std::vector<ImagePatch> &ref_patches,
const std::vector<size_t> &ref_patch_indices,
const AMatrix<double> &spectrogram_data,
Expand All @@ -81,7 +81,7 @@ class ComparisonPatchesSelector {
* @return A StatusOr that may contain a vector of new, finely aligned
* PatchSimilarityResults.
*/
google::protobuf::util::StatusOr<std::vector<PatchSimilarityResult>>
absl::StatusOr<std::vector<PatchSimilarityResult>>
FinelyAlignAndRecreatePatches(
const std::vector<PatchSimilarityResult>& sim_results,
const AudioSignal &ref_signal,
Expand Down
2 changes: 1 addition & 1 deletion src/include/gammatone_spectrogram_builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class GammatoneSpectrogramBuilder : public SpectrogramBuilder {
const bool use_speech_mode);

// Docs inherited from parent.
google::protobuf::util::StatusOr<Spectrogram> Build(
absl::StatusOr<Spectrogram> Build(
const AudioSignal &signal,
const AnalysisWindow &window) override;

Expand Down
6 changes: 3 additions & 3 deletions src/include/image_patch_creator.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

#include <vector>

#include "google/protobuf/stubs/statusor.h"
#include "absl/status/statusor.h"

#include "amatrix.h"
#include "analysis_window.h"
Expand Down Expand Up @@ -55,7 +55,7 @@ class ImagePatchCreator {
* @param If successful, the vector of patch indices is returned. Else, an
* error status.
*/
virtual google::protobuf::util::StatusOr<std::vector<size_t>>
virtual absl::StatusOr<std::vector<size_t>>
CreateRefPatchIndices(const AMatrix<double> &spectrogram,
const AudioSignal &ref_signal,
const AnalysisWindow &window) const;
Expand Down Expand Up @@ -93,7 +93,7 @@ class ImagePatchCreator {
* @param If successful, the vector of patch indices is returned. Else, an
* error status.
*/
google::protobuf::util::StatusOr<std::vector<size_t>> CreateRefPatchIndices(
absl::StatusOr<std::vector<size_t>> CreateRefPatchIndices(
const AMatrix<double> &spectrogram) const;
};
} // namespace Visqol
Expand Down
4 changes: 2 additions & 2 deletions src/include/similarity_to_quality_mapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
#include <vector>


#include "google/protobuf/stubs/status.h"
#include "absl/status/status.h"

namespace Visqol {

Expand Down Expand Up @@ -52,7 +52,7 @@ class SimilarityToQualityMapper {
* @return An OK status if the model was successfully initialized with the
* model file. Else, an error status is returned.
*/
virtual google::protobuf::util::Status Init() = 0;
virtual absl::Status Init() = 0;
};
} // namespace Visqol

Expand Down
4 changes: 2 additions & 2 deletions src/include/spectrogram_builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#ifndef VISQOL_INCLUDE_SPECTROGRAMBUILDER_H
#define VISQOL_INCLUDE_SPECTROGRAMBUILDER_H

#include "google/protobuf/stubs/statusor.h"
#include "absl/status/statusor.h"

#include "analysis_window.h"
#include "audio_signal.h"
Expand Down Expand Up @@ -50,7 +50,7 @@ class SpectrogramBuilder {
*
* @return The spectrogram representation of the input signal.
*/
virtual google::protobuf::util::StatusOr<Spectrogram> Build(
virtual absl::StatusOr<Spectrogram> Build(
const AudioSignal &signal,
const AnalysisWindow &window) = 0;
};
Expand Down
4 changes: 2 additions & 2 deletions src/include/speech_similarity_to_quality_mapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

#include <vector>

#include "google/protobuf/stubs/status.h"
#include "absl/status/status.h"

#include "similarity_to_quality_mapper.h"
#include "support_vector_regression_model.h"
Expand Down Expand Up @@ -47,7 +47,7 @@ class SpeechSimilarityToQualityMapper : public SimilarityToQualityMapper {
const override;

// Docs inherited from parent.
google::protobuf::util::Status Init() override;
absl::Status Init() override;

protected:
/**
Expand Down
46 changes: 46 additions & 0 deletions src/include/statusor_macros.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#ifndef VISQOL_INCLUDE_STATUSOR_MACROS_H
#define VISQOL_INCLUDE_STATUSOR_MACROS_H

#include "absl/status/status.h"
#include "absl/status/statusor.h"

namespace Visqol {

#define VISQOL_RETURN_IF_ERROR(expr) \
do { \
/* Using _status below to avoid capture problems if expr is "status". */ \
const absl::Status _status = (expr); \
if (!_status.ok()) return _status; \
} while (0)

template<typename T>
absl::Status DoAssignOrReturn(T& lhs, absl::StatusOr<T> result) {
if (result.ok()) {
lhs = result.value();
}
return result.status();
}

#define VISQOL_ASSIGN_OR_RETURN_IMPL(status, lhs, rexpr) \
absl::Status status = ::Visqol::DoAssignOrReturn(lhs, (rexpr)); \
if (!status.ok()) return status;

#define VISQOL_STATUS_MACROS_CONCAT_NAME_INNER(x, y) x##y
#define VISQOL_STATUS_MACROS_CONCAT_NAME(x, y) VISQOL_STATUS_MACROS_CONCAT_NAME_INNER(x, y)

// Executes an expression that returns a util::StatusOr, extracting its value
// into the variable defined by lhs (or returning on error).
//
// Example: Assigning to an existing value
// ValueType value;
// ASSIGN_OR_RETURN(value, MaybeGetValue(arg));
//
// WARNING: VISQOL_ASSIGN_OR_RETURN expands into multiple statements; it cannot be used
// in a single statement (e.g. as the body of an if statement without {})!
#define VISQOL_ASSIGN_OR_RETURN(lhs, rexpr) \
VISQOL_ASSIGN_OR_RETURN_IMPL( \
VISQOL_STATUS_MACROS_CONCAT_NAME(_status_or_value, __COUNTER__), lhs, rexpr);

} // namespace Visqol

#endif // VISQOL_INCLUDE_STATUSOR_MACROS_H
4 changes: 2 additions & 2 deletions src/include/support_vector_regression_model.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

#include "absl/synchronization/mutex.h"
#include "svm.h"
#include "google/protobuf/stubs/status.h"
#include "absl/status/status.h"

namespace Visqol {
/**
Expand All @@ -51,7 +51,7 @@ class SupportVectorRegressionModel {
* @return An OK status if the model was successfully initialized with the
* model file. Else, an error status is returned.
*/
google::protobuf::util::Status Init(const FilePath &model_path);
absl::Status Init(const FilePath &model_path);

/**
* Initialize the SVR model using vectors of observations and targets. These
Expand Down
Loading

0 comments on commit 852a12a

Please sign in to comment.