Skip to content
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

Add onehot #61

Merged
merged 5 commits into from
May 23, 2024
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
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,7 @@ set(MAIN_SOURCES
${SOURCE_DIR}/main/ndps2c.cc
${SOURCE_DIR}/main/norm0.cc
${SOURCE_DIR}/main/nrand.cc
${SOURCE_DIR}/main/onehot.cc
${SOURCE_DIR}/main/par2lar.cc
${SOURCE_DIR}/main/par2lpc.cc
${SOURCE_DIR}/main/pca.cc
Expand Down
14 changes: 7 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ all: release

release:
mkdir -p $(BUILDDIR)
cd $(BUILDDIR); cmake .. -DCMAKE_INSTALL_PREFIX=.. -DCMAKE_BUILD_TYPE=Release
cd $(BUILDDIR); make -j $(JOBS) install
cd $(BUILDDIR) && cmake .. -DCMAKE_INSTALL_PREFIX=.. -DCMAKE_BUILD_TYPE=Release
cd $(BUILDDIR) && make -j $(JOBS) install

debug:
mkdir -p $(BUILDDIR)
cd $(BUILDDIR); cmake .. -DCMAKE_INSTALL_PREFIX=.. -DCMAKE_BUILD_TYPE=Debug
cd $(BUILDDIR); make -j $(JOBS) install
cd $(BUILDDIR) && cmake .. -DCMAKE_INSTALL_PREFIX=.. -DCMAKE_BUILD_TYPE=Debug
cd $(BUILDDIR) && make -j $(JOBS) install

doc:
@if [ ! -f ./tools/venv/bin/activate ]; then \
Expand All @@ -52,13 +52,13 @@ doc:
echo ""; \
exit 1; \
fi
cd $(DOCDIR); ../tools/doxygen/build/bin/doxygen
. ./tools/venv/bin/activate; cd $(DOCDIR); make html
cd $(DOCDIR) && ../tools/doxygen/build/bin/doxygen
. ./tools/venv/bin/activate && cd $(DOCDIR) && make html

doc-clean:
rm -rf $(DOCDIR)/xml
@if [ -f ./tools/venv/bin/activate ]; then \
. ./tools/venv/bin/activate; cd $(DOCDIR); make clean; \
. ./tools/venv/bin/activate && cd $(DOCDIR) && make clean; \
fi

format: format-sh format-py format-cc
Expand Down
6 changes: 6 additions & 0 deletions doc/main/onehot.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.. _onehot:

onehot
======

.. doxygenfile:: onehot.cc
2 changes: 1 addition & 1 deletion src/main/nrand.cc
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ int main(int argc, char* argv[]) {
case 'u': {
if (!sptk::ConvertStringToDouble(optarg, &mean)) {
std::ostringstream error_message;
error_message << "The argument for the -u option must be double";
error_message << "The argument for the -u option must be numeric";
sptk::PrintErrorMessage("nrand", error_message);
return 1;
}
Expand Down
219 changes: 219 additions & 0 deletions src/main/onehot.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,219 @@
// ------------------------------------------------------------------------ //
// Copyright 2021 SPTK Working Group //
// //
// Licensed under the Apache License, Version 2.0 (the "License"); //
// you may not use this file except in compliance with the License. //
// You may obtain a copy of the License at //
// //
// http://www.apache.org/licenses/LICENSE-2.0 //
// //
// Unless required by applicable law or agreed to in writing, software //
// distributed under the License is distributed on an "AS IS" BASIS, //
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. //
// See the License for the specific language governing permissions and //
// limitations under the License. //
// ------------------------------------------------------------------------ //

#include <fstream> // std::ifstream
#include <iomanip> // std::setw
#include <iostream> // std::cerr, std::cin, std::cout, std::endl, etc.
#include <sstream> // std::ostringstream
#include <vector> // std::vector

#include "GETOPT/ya_getopt.h"
#include "SPTK/utils/sptk_utils.h"

namespace {

enum WarningType { kIgnore = 0, kWarn, kExit, kNumWarningTypes };

const int kDefaultVectorLength(10);
const WarningType kDefaultWarningType(kIgnore);

void PrintUsage(std::ostream* stream) {
// clang-format off
*stream << std::endl;
*stream << " onehot - generate one-hot vector sequence" << std::endl;
*stream << std::endl;
*stream << " usage:" << std::endl;
*stream << " onehot [ options ] [ infile ] > stdout" << std::endl;
*stream << " options:" << std::endl;
*stream << " -l l : length of vector (double)[" << std::setw(5) << std::right << kDefaultVectorLength << "][ 1 <= l <= ]" << std::endl; // NOLINT
*stream << " -m m : order of vector (double)[" << std::setw(5) << std::right << "l-1" << "][ 0 <= m <= ]" << std::endl; // NOLINT
*stream << " -e e : out-of-range error ( int)[" << std::setw(5) << std::right << kDefaultWarningType << "][ 0 <= e <= 2 ]" << std::endl; // NOLINT
*stream << " 0 (no warning)" << std::endl;
*stream << " 1 (output the index to stderr)" << std::endl;
*stream << " 2 (output the index to stderr and" << std::endl;
*stream << " exit immediately)" << std::endl;
*stream << " -h : print this message" << std::endl;
*stream << " infile:" << std::endl;
*stream << " 0-based index ( int)[stdin]" << std::endl;
*stream << " stdout:" << std::endl;
*stream << " one-hot vector (double)" << std::endl;
*stream << std::endl;
*stream << " SPTK: version " << sptk::kVersion << std::endl;
*stream << std::endl;
// clang-format on
}

} // namespace

/**
* @a onehot [ @e option ] [ @e infile ]
*
* - @b -l @e int
* - length of vector @f$(1 \le L)@f$
* - @b -m @e int
* - order of vector @f$(0 \le L - 1)@f$
* - @b -e @e int
* - warning type
* \arg @c 0 no warning
* \arg @c 1 output index
* \arg @c 2 output index and exit immediately
* - @b infile @e str
* - int-type 0-based index
* - @b stdout
* - double-type one-hot vector
*
* @code{.sh}
* ramp -l 3 | x2x +di | onehot -l 3 | x2x +da
* # 1, 0, 0, 0, 1, 0, 0, 0, 1
* @endcode
*
* @param[in] argc Number of arguments.
* @param[in] argv Argument vector.
* @return 0 on success, 1 on failure.
*/
int main(int argc, char* argv[]) {
int vector_length(kDefaultVectorLength);
WarningType warning_type(kDefaultWarningType);

for (;;) {
const int option_char(getopt_long(argc, argv, "l:m:e:h", NULL, NULL));
if (-1 == option_char) break;

switch (option_char) {
case 'l': {
if (!sptk::ConvertStringToInteger(optarg, &vector_length) ||
vector_length <= 0) {
std::ostringstream error_message;
error_message
<< "The argument for the -l option must be a positive integer";
sptk::PrintErrorMessage("onehot", error_message);
return 1;
}
break;
}
case 'm': {
if (!sptk::ConvertStringToInteger(optarg, &vector_length) ||
vector_length < 0) {
std::ostringstream error_message;
error_message << "The argument for the -m option must be a "
<< "non-negative integer";
sptk::PrintErrorMessage("onehot", error_message);
return 1;
}
++vector_length;
break;
}
case 'e': {
const int min(0);
const int max(static_cast<int>(kNumWarningTypes) - 1);
int tmp;
if (!sptk::ConvertStringToInteger(optarg, &tmp) ||
!sptk::IsInRange(tmp, min, max)) {
std::ostringstream error_message;
error_message << "The argument for the -e option must be an integer "
<< "in the range of " << min << " to " << max;
sptk::PrintErrorMessage("onehot", error_message);
return 1;
}
warning_type = static_cast<WarningType>(tmp);
break;
}
case 'h': {
PrintUsage(&std::cout);
return 0;
}
default: {
PrintUsage(&std::cerr);
return 1;
}
}
}

const int num_input_files(argc - optind);
if (1 < num_input_files) {
std::ostringstream error_message;
error_message << "Too many input files";
sptk::PrintErrorMessage("onehot", error_message);
return 1;
}
const char* input_file(0 == num_input_files ? NULL : argv[optind]);

if (!sptk::SetBinaryMode()) {
std::ostringstream error_message;
error_message << "Cannot set translation mode";
sptk::PrintErrorMessage("onehot", error_message);
return 1;
}

std::ifstream ifs;
if (NULL != input_file) {
ifs.open(input_file, std::ios::in | std::ios::binary);
if (ifs.fail()) {
std::ostringstream error_message;
error_message << "Cannot open file " << input_file;
sptk::PrintErrorMessage("onehot", error_message);
return 1;
}
}
std::istream& input_stream(ifs.is_open() ? ifs : std::cin);

int index;
std::vector<double> onehot_vector(vector_length, 0.0);

for (int sample_index(0); sptk::ReadStream(&index, &input_stream);
++sample_index) {
const bool is_valid(0 <= index && index < vector_length);
if (is_valid) {
onehot_vector[index] = 1.0;
} else {
switch (warning_type) {
case kIgnore: {
// nothing to do
break;
}
case kWarn: {
std::ostringstream error_message;
error_message << sample_index << "th sample is out of range";
sptk::PrintErrorMessage("onehot", error_message);
break;
}
case kExit: {
std::ostringstream error_message;
error_message << sample_index << "th sample is out of range";
sptk::PrintErrorMessage("onehot", error_message);
return 1;
}
default: {
std::ostringstream error_message;
error_message << "Unknown warning type";
sptk::PrintErrorMessage("onehot", error_message);
return 1;
}
}
}
if (!sptk::WriteStream(0, vector_length, onehot_vector, &std::cout, NULL)) {
std::ostringstream error_message;
error_message << "Failed to write one-hot vector";
sptk::PrintErrorMessage("onehot", error_message);
return 1;
}
if (is_valid) {
onehot_vector[index] = 0.0;
}
}

return 0;
}
41 changes: 41 additions & 0 deletions test/test_onehot.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#!/usr/bin/env bats
# ------------------------------------------------------------------------ #
# Copyright 2021 SPTK Working Group #
# #
# Licensed under the Apache License, Version 2.0 (the "License"); #
# you may not use this file except in compliance with the License. #
# You may obtain a copy of the License at #
# #
# http://www.apache.org/licenses/LICENSE-2.0 #
# #
# Unless required by applicable law or agreed to in writing, software #
# distributed under the License is distributed on an "AS IS" BASIS, #
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
# See the License for the specific language governing permissions and #
# limitations under the License. #
# ------------------------------------------------------------------------ #

sptk3=tools/sptk/bin
sptk4=bin
tmp=test_onehot

setup() {
mkdir -p $tmp
}

teardown() {
rm -rf $tmp
}

@test "onehot: compatibility" {
echo 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 | $sptk3/x2x +ad > $tmp/1
$sptk3/ramp -s -1 -e 3 | $sptk3/x2x +di | $sptk4/onehot -l 3 > $tmp/2
run $sptk4/aeq $tmp/1 $tmp/2
[ "$status" -eq 0 ]
}

@test "onehot: valgrind" {
$sptk3/nrand -l 20 -v 10 | $sptk3/x2x +di -r > $tmp/1
run valgrind $sptk4/onehot $tmp/1
[ "$(echo "${lines[-1]}" | sed -r 's/.*SUMMARY: ([0-9]*) .*/\1/')" -eq 0 ]
}
18 changes: 9 additions & 9 deletions tools/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
# limitations under the License. #
# ------------------------------------------------------------------------ #

PYTHON_VERSION := 3.8
PYTHON_VERSION := 3.9
SHELLCHECK_VERSION := 0.10.0
SHFMT_VERSION := 3.8.0
JOBS := 4
Expand All @@ -30,8 +30,8 @@ venv:
else \
test -d venv || python$(PYTHON_VERSION) -m venv venv; \
fi
. venv/bin/activate; python -m pip install pip --upgrade; \
python -m pip install -r requirements.txt --upgrade
. ./venv/bin/activate && python -m pip install pip --upgrade
. ./venv/bin/activate && python -m pip install -r requirements.txt --upgrade
touch venv/bin/activate

venv_dev:
Expand All @@ -40,8 +40,8 @@ venv_dev:
else \
test -d venv || python$(PYTHON_VERSION) -m venv venv; \
fi
. venv/bin/activate; python -m pip install pip --upgrade; \
python -m pip install -r requirements.txt -r test_requirements.txt --upgrade
. ./venv/bin/activate && python -m pip install pip --upgrade
. ./venv/bin/activate && python -m pip install -r requirements.txt -r test_requirements.txt --upgrade
touch venv/bin/activate

bats.done:
Expand All @@ -52,7 +52,7 @@ bats.done:
doxygen.done:
rm -rf doxygen
git clone https://github.com/doxygen/doxygen.git -b Release_1_9_1 --single-branch --depth 1
cd doxygen; mkdir -p build; cd build; cmake -G "Unix Makefiles" ..; make -j $(JOBS)
cd doxygen && mkdir -p build && cd build && cmake -G "Unix Makefiles" .. && make -j $(JOBS)
touch doxygen.done

shellcheck.done:
Expand All @@ -72,9 +72,9 @@ shfmt.done:

sptk.done:
rm -rf sptk
export GIT_SSL_NO_VERIFY=1; git clone https://git.code.sf.net/p/sp-tk/SPTK sptk
cd sptk/src; aclocal; automake -a -c; autoconf; \
./configure --enable-double --prefix=$(CURDIR)/sptk; make -j $(JOBS); make install
export GIT_SSL_NO_VERIFY=1 && git clone https://git.code.sf.net/p/sp-tk/SPTK sptk
cd sptk/src && aclocal && automake -a -c && autoconf
cd sptk/src && ./configure --enable-double --prefix=$(CURDIR)/sptk && make -j $(JOBS) && make install
touch sptk.done

clean:
Expand Down