Skip to content

Commit

Permalink
Add singularity check to polyfit function to prevent NaN values
Browse files Browse the repository at this point in the history
  • Loading branch information
devin-ai-integration[bot] authored and ohhmm committed Sep 18, 2024
1 parent 8f1dda5 commit a310eae
Show file tree
Hide file tree
Showing 22 changed files with 3,294 additions and 3,422 deletions.
8 changes: 8 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,12 @@ else()
set(initTestsEnabled OFF)
endif()
option(OPENMIND_BUILD_TESTS "Build various TESTS" ${initTestsEnabled})

# Set output directories for executables and libraries
set(EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR}/bin)
set(LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
set(RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)

option(OPENMIND_DEBUG_CHECKS "Additional debug checks (NOOMDEBUG define)" ${_IS_DEBUG})
option(OPENMIND_DEPEND_ON_PREREQUISITES "Better consider installing prerequisites by building the target 'prerequisites' upon initial setup. This way its not checked each build." OFF)
option(OPENMIND_BUILD_SAMPLES "Build various samples" FALSE)
Expand Down Expand Up @@ -172,6 +178,8 @@ set(_BOOST_USED_COMPONENTS
filesystem
iostreams
locale
log
log_setup
serialization
system
thread
Expand Down
15 changes: 11 additions & 4 deletions omnn/extrapolator/Extrapolator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "Extrapolator.h"
#include "math/Integer.h"
#include "math/Sum.h"
#include <boost/log/trivial.hpp>

using namespace omnn;
using namespace math;
Expand All @@ -14,20 +15,20 @@ auto det_fast(extrapolator_base_matrix matrix)
{
using T = extrapolator_base_matrix::value_type;
ublas::permutation_matrix<std::size_t> pivots(matrix.size1());

auto isSingular = ublas::lu_factorize(matrix, pivots);
if (isSingular)
return T(0);

T det = 1;
for (std::size_t i = 0; i < pivots.size(); ++i)
{
if (pivots(i) != i)
det *= static_cast<double>(-1);

det *= matrix(i, i);
}

return det;
}

Expand All @@ -38,19 +39,25 @@ bool Extrapolator::Consistent(const extrapolator_base_matrix& augment)

Valuable Extrapolator::Factors(const Variable& row, const Variable& col, const Variable& val) const
{
BOOST_LOG_TRIVIAL(info) << "Entering Factors method";
BOOST_LOG_TRIVIAL(info) << "row: " << row << ", col: " << col << ", val: " << val;
Product e;
auto szy = size1();
auto szx = size2();
BOOST_LOG_TRIVIAL(info) << "szy: " << szy << ", szx: " << szx;
auto optsWas = Valuable::optimizations;
Valuable::optimizations = {};
for (auto y = 0; y < szy; ++y) {
for (auto x = 0; x < szx; ++x) {
BOOST_LOG_TRIVIAL(info) << "y: " << y << ", x: " << x << ", value: " << (*this)(y,x);
e.Add(((row-y)^2)
+((col-x)^2)
+((val-(*this)(y,x))^2));
BOOST_LOG_TRIVIAL(info) << "Product e after addition: " << e;
}
}
Valuable::optimizations = optsWas;
BOOST_LOG_TRIVIAL(info) << "Exiting Factors method with Product e: " << e;
return Valuable(std::move(e));
}

Expand Down
74 changes: 56 additions & 18 deletions omnn/extrapolator/test/image_codec_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
#include <fstream>
#include <sstream>
#include <boost/gil/extension/io/targa.hpp>
#include "Extrapolator.h"
#include "../Extrapolator.h"
#include <boost/log/trivial.hpp>


using namespace omnn::math;
Expand All @@ -27,75 +28,107 @@ std::string l(const omnn::math::Valuable& v)

BOOST_AUTO_TEST_CASE(ImageCodec_test)
{
BOOST_LOG_TRIVIAL(info) << "Starting ImageCodec_test";

rgba8_image_t src;
BOOST_LOG_TRIVIAL(info) << "Before reading image";
read_image(TEST_SRC_DIR "g.tga", src, targa_tag());
BOOST_LOG_TRIVIAL(info) << "Image read successfully";

BOOST_LOG_TRIVIAL(info) << "Before writing image";
write_view(TEST_BIN_DIR "was.tga", view(src), targa_tag());

BOOST_LOG_TRIVIAL(info) << "Image written successfully";

auto rows = src.dimensions().y;
auto cols = src.dimensions().x;
BOOST_LOG_TRIVIAL(info) << "Before initializing extrapolators";
Extrapolator a(rows, cols);
BOOST_LOG_TRIVIAL(info) << "Extrapolator a initialized";
Extrapolator r(rows, cols);
BOOST_LOG_TRIVIAL(info) << "Extrapolator r initialized";
Extrapolator g(rows, cols);
BOOST_LOG_TRIVIAL(info) << "Extrapolator g initialized";
Extrapolator b(rows, cols);
BOOST_LOG_TRIVIAL(info) << "Extrapolator b initialized";

auto& v = view(src);
BOOST_LOG_TRIVIAL(info) << "Before populating extrapolators";
for (auto i = rows; i--;) { // raw
for (auto j = cols; j--;) { // column
auto px = v(i,j);
BOOST_LOG_TRIVIAL(info) << "Populating extrapolators for pixel (" << i << ", " << j << ")";
a(i,j) = get_color(px,alpha_t());
r(i,j) = get_color(px,red_t());
g(i,j) = get_color(px,green_t());
b(i,j) = get_color(px,blue_t());
}
}
BOOST_LOG_TRIVIAL(info) << "Extrapolators populated";

Variable x, y, z;
std::list<Variable> formulaParamSequence = { y, x };
BOOST_LOG_TRIVIAL(info) << "Before optimizing fa";
auto fa = a.Factors(y, x, z);
// fa.SetView(Valuable::View::Flat);
BOOST_LOG_TRIVIAL(info) << "After creating fa: " << fa;
fa.optimize();
std::cout << fa << std::endl;
BOOST_LOG_TRIVIAL(info) << "fa optimized: " << fa;

BOOST_LOG_TRIVIAL(info) << "Before optimizing fr";
auto fr = r.Factors(y, x, z);
// fr.SetView(Valuable::View::Flat);
BOOST_LOG_TRIVIAL(info) << "After creating fr: " << fr;
fr.optimize();
std::cout << fr << std::endl;
BOOST_LOG_TRIVIAL(info) << "fr optimized: " << fr;

BOOST_LOG_TRIVIAL(info) << "Before optimizing fg";
auto fg = g.Factors(y, x, z);
// fg.SetView(Valuable::View::Flat);
BOOST_LOG_TRIVIAL(info) << "After creating fg: " << fg;
fg.optimize();
std::cout << fg << std::endl;
BOOST_LOG_TRIVIAL(info) << "fg optimized: " << fg;

BOOST_LOG_TRIVIAL(info) << "Before optimizing fb";
auto fb = b.Factors(y, x, z);
// fb.SetView(Valuable::View::Flat);
BOOST_LOG_TRIVIAL(info) << "After creating fb: " << fb;
fb.optimize();
std::cout << fb << std::endl;
BOOST_LOG_TRIVIAL(info) << "fb optimized: " << fb;

// Unification
BOOST_LOG_TRIVIAL(info) << "Before unification";
FormulaOfVaWithSingleIntegerRoot
afo(z, fa, &formulaParamSequence),
rfo(z, fr, &formulaParamSequence),
gfo(z, fg, &formulaParamSequence),
bfo(z, fb, &formulaParamSequence);
BOOST_LOG_TRIVIAL(info) << "After unification: " << "afo: " << afo << ", rfo: " << rfo << ", gfo: " << gfo << ", bfo: " << bfo;

// inbound data deduce
BOOST_LOG_TRIVIAL(info) << "Before deducing inbound data";
decltype(src) dst(src.dimensions());
auto dv = view(dst);
for (auto i = rows; i--;) { // raw
for (auto j = cols; j--;) { // column
auto& d = dv(i, j);
auto& s = v(i,j);
BOOST_LOG_TRIVIAL(info) << "Deducing alpha for pixel (" << i << ", " << j << ")";
get_color(d,alpha_t()) = static_cast<unsigned char>(afo(i,j));
BOOST_LOG_TRIVIAL(info) << "Deducing red for pixel (" << i << ", " << j << ")";
get_color(d,red_t()) = static_cast<unsigned char>(rfo(i,j));
BOOST_LOG_TRIVIAL(info) << "Deducing green for pixel (" << i << ", " << j << ")";
get_color(d,green_t()) = static_cast<unsigned char>(gfo(i,j));
BOOST_LOG_TRIVIAL(info) << "Deducing blue for pixel (" << i << ", " << j << ")";
get_color(d,blue_t()) = static_cast<unsigned char>(bfo(i,j));

BOOST_LOG_TRIVIAL(info) << "Verifying pixel (" << i << ", " << j << ")";
BOOST_TEST(unsigned(d[0])==unsigned(s[0]));
BOOST_TEST(unsigned(d[1])==unsigned(s[1]));
BOOST_TEST(unsigned(d[2])==unsigned(s[2]));
BOOST_TEST(unsigned(d[3])==unsigned(s[3]));
}
}
BOOST_LOG_TRIVIAL(info) << "Inbound data deduced successfully.";
write_view(TEST_BIN_DIR "o.tga", dv, targa_tag());

// outband data deduce
BOOST_LOG_TRIVIAL(info) << "Before deducing outband data";
afo.SetMode(FormulaOfVaWithSingleIntegerRoot::Newton);
afo.SetMin(0); afo.SetMax(255);
rfo.SetMode(FormulaOfVaWithSingleIntegerRoot::Newton);
Expand All @@ -104,7 +137,7 @@ BOOST_AUTO_TEST_CASE(ImageCodec_test)
gfo.SetMin(0); gfo.SetMax(255);
bfo.SetMode(FormulaOfVaWithSingleIntegerRoot::Newton);
bfo.SetMin(0); bfo.SetMax(255);

const auto d = 2; // 5
cols+=d;rows+=d;
dst = decltype(src)(rows+d, cols+d);
Expand All @@ -113,13 +146,18 @@ BOOST_AUTO_TEST_CASE(ImageCodec_test)
for (auto j = cols; --j>=-d;) { // column
auto c=j+d;
auto r = i+d;
auto& d = dv(r, c);
get_color(d,alpha_t()) = static_cast<unsigned char>(afo(i,j));
get_color(d,red_t()) = static_cast<unsigned char>(rfo(i,j));
get_color(d,green_t()) = static_cast<unsigned char>(gfo(i,j));
get_color(d,blue_t()) = static_cast<unsigned char>(bfo(i,j));
BOOST_LOG_TRIVIAL(info) << "Deducing alpha for pixel (" << r << ", " << c << ")";
get_color(dv(r, c),alpha_t()) = static_cast<unsigned char>(afo(i,j));
BOOST_LOG_TRIVIAL(info) << "Deducing red for pixel (" << r << ", " << c << ")";
get_color(dv(r, c),red_t()) = static_cast<unsigned char>(rfo(i,j));
BOOST_LOG_TRIVIAL(info) << "Deducing green for pixel (" << r << ", " << c << ")";
get_color(dv(r, c),green_t()) = static_cast<unsigned char>(gfo(i,j));
BOOST_LOG_TRIVIAL(info) << "Deducing blue for pixel (" << r << ", " << c << ")";
get_color(dv(r, c),blue_t()) = static_cast<unsigned char>(bfo(i,j));
}
}
BOOST_LOG_TRIVIAL(info) << "Outband data deduced successfully.";
write_view(TEST_BIN_DIR "e.tga", dv, targa_tag());
}

BOOST_LOG_TRIVIAL(info) << "Completed ImageCodec_test";
}
2 changes: 1 addition & 1 deletion omnn/math/3rdPartyTests/FANN_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include <boost/test/unit_test.hpp>
#include <fann.h>


#define TEST_SRC_DIR "."

BOOST_AUTO_TEST_CASE(FANN_test_simple)
{
Expand Down
1 change: 1 addition & 0 deletions omnn/math/Cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Created by Sergej Krivonos on 31.12.2019.
//
#include "Cache.h"
#include "Valuable.h"

#include <iostream>
#include <thread>
Expand Down
3 changes: 2 additions & 1 deletion omnn/math/Constant.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
#include "Constant.h"
#include "Exponentiation.h"

using namespace omnn::math;
using namespace constants;

std::map<std::string_view, Valuable> ConstNameAdder::SerializationNamesMap;

APPLE_CONSTEXPR ConstNameAdder::ConstNameAdder(const std::string_view& name, const Valuable& obj) {
SerializationNamesMap[name] = obj;
SerializationNamesMap[name] = obj;
}
1 change: 1 addition & 0 deletions omnn/math/DuoValDescendant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include "DuoValDescendant.h"
#include "Product.h"
#include "ValuableDescendantContract.h"

namespace omnn::math {

Expand Down
31 changes: 15 additions & 16 deletions omnn/math/DuoValDescendant.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ namespace omnn::math {
Valuable::hash ^= _1.Hash();
Valuable::optimized = {};
}

const Valuable& get2() const { return _2; }
template<class T>
void set2(T&& p) {
Expand All @@ -99,13 +99,13 @@ namespace omnn::math {
Valuable::hash ^= _2.Hash();
Valuable::optimized = {};
}

using base::base;

template <class T1, class T2>
constexpr DuoValDescendant(T1&& v1, T2&& v2)
: _1(std::forward<T1>(v1))
, _2(std::forward<T2>(v2))
template <class T1, class T2>
constexpr DuoValDescendant(T1&& v1, T2&& v2)
: _1(std::forward<T1>(v1))
, _2(std::forward<T2>(v2))
{
Valuable::hash = _1.Hash() ^ _2.Hash();
Valuable::maxVaExp = Chld::getMaxVaExp(_1, _2);
Expand Down Expand Up @@ -165,16 +165,16 @@ namespace omnn::math {
max_exp_t getMaxVaExp() const override {
return Chld::getMaxVaExp(_1, _2);
}

bool IsSimple() const override {
return IsMultival() == Valuable::YesNoMaybe::No
&& _1.IsSimple() && _2.IsSimple();
}

Valuable::YesNoMaybe IsMultival() const override {
return _1.IsMultival() || _2.IsMultival();
}

void Values(const std::function<bool(const Valuable&)>& f) const override
{
if (f) {
Expand All @@ -184,14 +184,14 @@ namespace omnn::math {
}

// TODO: multival caching (inspect all optimized and optimization transisions)


Valuable::solutions_t vals, thisValues;
_1.Values([&](auto& thisVal){
thisValues.insert(thisVal);
return true;
});

_2.Values([&](auto&vVal){
for(auto& tv:thisValues){
Valuable v = Chld{tv,vVal};
Expand All @@ -200,17 +200,17 @@ namespace omnn::math {
}
return true;
});

for(auto& v:vals)
f(v);
}
}

a_int Complexity() const override {
return _1.Complexity() + _2.Complexity();
}


Valuable::solutions_t Distinct() const override {
Valuable::solutions_t branches;
for (auto&& f : _1.Distinct()) {
Expand All @@ -222,4 +222,3 @@ namespace omnn::math {
}
};
}

Loading

0 comments on commit a310eae

Please sign in to comment.