#7206 Add regression analysis.

This commit is contained in:
Kristian Bendiksen 2023-05-12 16:02:56 +02:00
parent 26275f7924
commit c23cdee17d
10 changed files with 434 additions and 0 deletions

3
.gitmodules vendored
View File

@ -19,3 +19,6 @@
[submodule "ThirdParty/openzgy"]
path = ThirdParty/openzgy
url = https://github.com/CeetronSolutions/openzgy.git
[submodule "ThirdParty/regression-analysis"]
path = ThirdParty/regression-analysis
url = https://github.com/CeetronSolutions/regression-analysis.git

View File

@ -52,6 +52,7 @@ set(SOURCE_GROUP_HEADER_FILES
${CMAKE_CURRENT_LIST_DIR}/RicNewSummaryTableFeature.h
${CMAKE_CURRENT_LIST_DIR}/RicDuplicateSummaryTableFeature.h
${CMAKE_CURRENT_LIST_DIR}/RicCreateDeclineCurvesFeature.h
${CMAKE_CURRENT_LIST_DIR}/RicCreateRegressionAnalysisCurveFeature.h
)
set(SOURCE_GROUP_SOURCE_FILES
@ -108,6 +109,7 @@ set(SOURCE_GROUP_SOURCE_FILES
${CMAKE_CURRENT_LIST_DIR}/RicNewSummaryTableFeature.cpp
${CMAKE_CURRENT_LIST_DIR}/RicDuplicateSummaryTableFeature.cpp
${CMAKE_CURRENT_LIST_DIR}/RicCreateDeclineCurvesFeature.cpp
${CMAKE_CURRENT_LIST_DIR}/RicCreateRegressionAnalysisCurveFeature.cpp
)
list(APPEND COMMAND_CODE_HEADER_FILES ${SOURCE_GROUP_HEADER_FILES})

View File

@ -0,0 +1,108 @@
/////////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2023- Equinor ASA
//
// ResInsight is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// ResInsight is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.
//
// See the GNU General Public License at <http://www.gnu.org/licenses/gpl.html>
// for more details.
//
/////////////////////////////////////////////////////////////////////////////////
#include "RicCreateRegressionAnalysisCurveFeature.h"
#include "RiaSummaryTools.h"
#include "RimSummaryCurve.h"
#include "RimSummaryMultiPlot.h"
#include "RimSummaryPlot.h"
#include "RimSummaryRegressionAnalysisCurve.h"
#include "RiuPlotMainWindowTools.h"
#include "cafSelectionManagerTools.h"
#include "cvfAssert.h"
#include <QAction>
CAF_CMD_SOURCE_INIT( RicCreateRegressionAnalysisCurveFeature, "RicCreateRegressionAnalysisCurveFeature" );
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
bool RicCreateRegressionAnalysisCurveFeature::isCommandEnabled()
{
RimSummaryPlot* selectedPlot = caf::firstAncestorOfTypeFromSelectedObject<RimSummaryPlot>();
return ( selectedPlot && !RiaSummaryTools::isSummaryCrossPlot( selectedPlot ) );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RicCreateRegressionAnalysisCurveFeature::onActionTriggered( bool isChecked )
{
RimSummaryCurve* curve = caf::firstAncestorOfTypeFromSelectedObject<RimSummaryCurve>();
if ( curve )
{
RimSummaryRegressionAnalysisCurve* newCurve = createRegressionAnalysisCurveAndAddToPlot( curve );
RiuPlotMainWindowTools::showPlotMainWindow();
RiuPlotMainWindowTools::selectAsCurrentItem( newCurve );
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RicCreateRegressionAnalysisCurveFeature::setupActionLook( QAction* actionToSetup )
{
actionToSetup->setText( "Create Regression Analysis Curve" );
actionToSetup->setIcon( QIcon( ":/SummaryCurve16x16.png" ) );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
RimSummaryRegressionAnalysisCurve*
RicCreateRegressionAnalysisCurveFeature::createRegressionAnalysisCurveAndAddToPlot( RimSummaryCurve* sourceCurve )
{
RimSummaryPlot* summaryPlot = caf::firstAncestorOfTypeFromSelectedObject<RimSummaryPlot>();
RimSummaryRegressionAnalysisCurve* newCurve = new RimSummaryRegressionAnalysisCurve();
CVF_ASSERT( newCurve );
newCurve->setSummaryCaseX( sourceCurve->summaryCaseX() );
newCurve->setSummaryAddressX( sourceCurve->summaryAddressX() );
newCurve->setSummaryCaseY( sourceCurve->summaryCaseY() );
newCurve->setSummaryAddressY( sourceCurve->summaryAddressY() );
newCurve->setColor( sourceCurve->color() );
// newCurve->setLineStyle( mapToLineStyle( declineCurveType ) );
summaryPlot->addCurveAndUpdate( newCurve );
newCurve->loadDataAndUpdate( true );
newCurve->updateConnectedEditors();
RimSummaryMultiPlot* summaryMultiPlot = summaryPlot->firstAncestorOrThisOfType<RimSummaryMultiPlot>();
if ( summaryMultiPlot )
{
summaryMultiPlot->updatePlotTitles();
}
else
{
summaryPlot->updatePlotTitle();
}
summaryPlot->updateAllRequiredEditors();
return newCurve;
}

View File

@ -0,0 +1,41 @@
/////////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2023- Equinor ASA
//
// ResInsight is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// ResInsight is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.
//
// See the GNU General Public License at <http://www.gnu.org/licenses/gpl.html>
// for more details.
//
/////////////////////////////////////////////////////////////////////////////////
#pragma once
#include "cafCmdFeature.h"
#include "RimSummaryRegressionAnalysisCurve.h"
class RimSummaryPlot;
class RimSummaryCurve;
//==================================================================================================
///
//==================================================================================================
class RicCreateRegressionAnalysisCurveFeature : public caf::CmdFeature
{
CAF_CMD_HEADER_INIT;
protected:
bool isCommandEnabled() override;
void onActionTriggered( bool isChecked ) override;
void setupActionLook( QAction* actionToSetup ) override;
static RimSummaryRegressionAnalysisCurve* createRegressionAnalysisCurveAndAddToPlot( RimSummaryCurve* sourceCurve );
};

View File

@ -718,6 +718,7 @@ caf::CmdFeatureMenuBuilder RimContextCommandBuilder::commandsFromSelection()
menuBuilder << "RicNewSummaryCurveFeature";
menuBuilder << "RicDuplicateSummaryCurveFeature";
menuBuilder << "RicCreateDeclineCurvesFeature";
menuBuilder << "RicCreateRegressionAnalysisCurveFeature";
menuBuilder << "RicNewSummaryCrossPlotCurveFeature";
menuBuilder << "RicDuplicateSummaryCrossPlotCurveFeature";
menuBuilder << "Separator";

View File

@ -51,6 +51,7 @@ set(SOURCE_GROUP_HEADER_FILES
${CMAKE_CURRENT_LIST_DIR}/RimSummaryTableCollection.h
${CMAKE_CURRENT_LIST_DIR}/RimSummaryTableTools.h
${CMAKE_CURRENT_LIST_DIR}/RimSummaryDeclineCurve.h
${CMAKE_CURRENT_LIST_DIR}/RimSummaryRegressionAnalysisCurve.h
)
set(SOURCE_GROUP_SOURCE_FILES
@ -106,6 +107,7 @@ set(SOURCE_GROUP_SOURCE_FILES
${CMAKE_CURRENT_LIST_DIR}/RimSummaryTableCollection.cpp
${CMAKE_CURRENT_LIST_DIR}/RimSummaryTableTools.cpp
${CMAKE_CURRENT_LIST_DIR}/RimSummaryDeclineCurve.cpp
${CMAKE_CURRENT_LIST_DIR}/RimSummaryRegressionAnalysisCurve.cpp
)
list(APPEND CODE_HEADER_FILES ${SOURCE_GROUP_HEADER_FILES})

View File

@ -0,0 +1,195 @@
/////////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2023 Equinor ASA
//
// ResInsight is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// ResInsight is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.
//
// See the GNU General Public License at <http://www.gnu.org/licenses/gpl.html>
// for more details.
//
/////////////////////////////////////////////////////////////////////////////////
#include "RimSummaryRegressionAnalysisCurve.h"
#include "cafPdmUiLineEditor.h"
#include "LinearRegression.hpp"
#include "PolynominalRegression.hpp"
#include "PowerFitRegression.hpp"
#include <cmath>
CAF_PDM_SOURCE_INIT( RimSummaryRegressionAnalysisCurve, "RegressionAnalysisCurve" );
namespace caf
{
template <>
void caf::AppEnum<RimSummaryRegressionAnalysisCurve::RegressionType>::setUp()
{
addItem( RimSummaryRegressionAnalysisCurve::RegressionType::LINEAR, "LINEAR", "Linear" );
addItem( RimSummaryRegressionAnalysisCurve::RegressionType::POLYNOMINAL, "POLYNOMINAL", "Polynominal" );
addItem( RimSummaryRegressionAnalysisCurve::RegressionType::POWER_FIT, "POWER_FIT", "Power Fit" );
setDefault( RimSummaryRegressionAnalysisCurve::RegressionType::LINEAR );
}
}; // namespace caf
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
RimSummaryRegressionAnalysisCurve::RimSummaryRegressionAnalysisCurve()
{
CAF_PDM_InitObject( "Regression Analysis Curve", ":/SummaryCurve16x16.png" );
CAF_PDM_InitFieldNoDefault( &m_regressionType, "RegressionType", "Type" );
CAF_PDM_InitField( &m_polynominalDegree, "PolynominalDegree", 3, "Degree" );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
RimSummaryRegressionAnalysisCurve::~RimSummaryRegressionAnalysisCurve()
{
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
std::vector<double> RimSummaryRegressionAnalysisCurve::valuesY() const
{
return computeRegressionCurve( RimSummaryCurve::timeStepsY(), RimSummaryCurve::valuesY() ).second;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
std::vector<double> RimSummaryRegressionAnalysisCurve::valuesX() const
{
return computeRegressionCurve( RimSummaryCurve::timeStepsX(), RimSummaryCurve::valuesX() ).second;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
std::vector<time_t> RimSummaryRegressionAnalysisCurve::timeStepsY() const
{
return computeRegressionCurve( RimSummaryCurve::timeStepsY(), RimSummaryCurve::valuesY() ).first;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
std::vector<time_t> RimSummaryRegressionAnalysisCurve::timeStepsX() const
{
return computeRegressionCurve( RimSummaryCurve::timeStepsX(), RimSummaryCurve::valuesX() ).first;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
std::pair<std::vector<time_t>, std::vector<double>>
RimSummaryRegressionAnalysisCurve::computeRegressionCurve( const std::vector<time_t>& timeSteps, const std::vector<double>& values ) const
{
if ( values.empty() || timeSteps.empty() ) return { timeSteps, values };
auto convertToDouble = []( const std::vector<time_t>& timeSteps )
{
std::vector<double> doubleVector( timeSteps.size() );
std::transform( timeSteps.begin(),
timeSteps.end(),
doubleVector.begin(),
[]( const auto& timeVal ) { return static_cast<double>( timeVal ); } );
return doubleVector;
};
std::vector<double> timeStepsD = convertToDouble( timeSteps );
if ( m_regressionType == RegressionType::LINEAR )
{
regression::LinearRegression linearRegression;
linearRegression.fit( timeStepsD, values );
std::vector<double> predictedValues = linearRegression.predict( timeStepsD );
return { timeSteps, predictedValues };
}
else if ( m_regressionType == RegressionType::POLYNOMINAL )
{
regression::PolynominalRegression polynominalRegression;
polynominalRegression.fit( timeStepsD, values, m_polynominalDegree );
std::vector<double> predictedValues = polynominalRegression.predict( timeStepsD );
return { timeSteps, predictedValues };
}
else if ( m_regressionType == RegressionType::POWER_FIT )
{
regression::PowerFitRegression powerFitRegression;
powerFitRegression.fit( timeStepsD, values );
std::vector<double> predictedValues = powerFitRegression.predict( timeStepsD );
return { timeSteps, predictedValues };
}
return { timeSteps, values };
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RimSummaryRegressionAnalysisCurve::defineUiOrdering( QString uiConfigName, caf::PdmUiOrdering& uiOrdering )
{
RimPlotCurve::updateFieldUiState();
caf::PdmUiGroup* regressionCurveGroup = uiOrdering.addNewGroup( "Regression Analysis" );
regressionCurveGroup->add( &m_regressionType );
if ( m_regressionType == RegressionType::POLYNOMINAL )
{
regressionCurveGroup->add( &m_polynominalDegree );
}
RimSummaryCurve::defineUiOrdering( uiConfigName, uiOrdering );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RimSummaryRegressionAnalysisCurve::fieldChangedByUi( const caf::PdmFieldHandle* changedField,
const QVariant& oldValue,
const QVariant& newValue )
{
RimSummaryCurve::fieldChangedByUi( changedField, oldValue, newValue );
if ( changedField == &m_regressionType || changedField == &m_polynominalDegree )
{
loadAndUpdateDataAndPlot();
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RimSummaryRegressionAnalysisCurve::defineEditorAttribute( const caf::PdmFieldHandle* field,
QString uiConfigName,
caf::PdmUiEditorAttribute* attribute )
{
RimSummaryCurve::defineEditorAttribute( field, uiConfigName, attribute );
if ( field == &m_polynominalDegree )
{
if ( auto* lineEditorAttr = dynamic_cast<caf::PdmUiLineEditorAttribute*>( attribute ) )
{
// Polynominal degree should be a positive number.
lineEditorAttr->validator = new QIntValidator( 1, 50, nullptr );
}
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
QString RimSummaryRegressionAnalysisCurve::createCurveAutoName()
{
return RimSummaryCurve::createCurveAutoName() + " " + m_regressionType().uiText() + " Regression";
}

View File

@ -0,0 +1,70 @@
/////////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2023 Equinor ASA
//
// ResInsight is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// ResInsight is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.
//
// See the GNU General Public License at <http://www.gnu.org/licenses/gpl.html>
// for more details.
//
/////////////////////////////////////////////////////////////////////////////////
#pragma once
#include "cafPdmField.h"
#include "cafPdmObject.h"
#include "RimSummaryCurve.h"
#include "cafAppEnum.h"
#include <utility>
//==================================================================================================
///
///
//==================================================================================================
class RimSummaryRegressionAnalysisCurve : public RimSummaryCurve
{
CAF_PDM_HEADER_INIT;
public:
enum class RegressionType
{
LINEAR,
POLYNOMINAL,
POWER_FIT
};
RimSummaryRegressionAnalysisCurve();
~RimSummaryRegressionAnalysisCurve() override;
// Y Axis functions
std::vector<double> valuesY() const override;
std::vector<time_t> timeStepsY() const override;
// X Axis functions
std::vector<double> valuesX() const override;
std::vector<time_t> timeStepsX() const override;
private:
QString createCurveAutoName() override;
// Overridden PDM methods
void fieldChangedByUi( const caf::PdmFieldHandle* changedField, const QVariant& oldValue, const QVariant& newValue ) override;
void defineUiOrdering( QString uiConfigName, caf::PdmUiOrdering& uiOrdering ) override;
void defineEditorAttribute( const caf::PdmFieldHandle* field, QString uiConfigName, caf::PdmUiEditorAttribute* attribute ) override;
std::pair<std::vector<time_t>, std::vector<double>> computeRegressionCurve( const std::vector<time_t>& timeSteps,
const std::vector<double>& values ) const;
caf::PdmField<caf::AppEnum<RegressionType>> m_regressionType;
caf::PdmField<int> m_polynominalDegree;
};

View File

@ -586,6 +586,17 @@ add_subdirectory(ThirdParty/roffcpp)
list(APPEND THIRD_PARTY_LIBRARIES roffcpp)
set_property(TARGET roffcpp-tests gtest gtest_main PROPERTY FOLDER "Thirdparty")
# ##############################################################################
# regression-analysis
# ##############################################################################
add_subdirectory(ThirdParty/regression-analysis)
list(APPEND THIRD_PARTY_LIBRARIES regression-analysis)
set_property(
TARGET regression-analysis-tests gtest gtest_main PROPERTY FOLDER
"Thirdparty"
)
# ##############################################################################
# Thirdparty libraries are put in ThirdParty solution folder
# ##############################################################################

1
ThirdParty/regression-analysis vendored Submodule

@ -0,0 +1 @@
Subproject commit e9f66501ac8b72c4f0654356e2dddb018c163d5a