Skip to content

Commit

Permalink
feat: добавлен "Прогноз" (#92)
Browse files Browse the repository at this point in the history
Closes #85
  • Loading branch information
Maks1mS authored Jan 2, 2024
1 parent 51fc413 commit 8ea3cff
Show file tree
Hide file tree
Showing 12 changed files with 236 additions and 315 deletions.
4 changes: 2 additions & 2 deletions .idea/runConfigurations/statapp.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions statapp/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,6 @@ def main():
window.show()
return app.exec_()


if __name__ == "__main__":
sys.exit(main())
37 changes: 37 additions & 0 deletions statapp/calculations.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import numpy as np
import pandas as pd
import sympy as sp
from statapp._vendor.multipolyfit import multipolyfit, getTerms

DIRECT_LINK = 0
Expand Down Expand Up @@ -135,3 +136,39 @@ def calculateStats(data, params, residues, y):
out[1] = tStatistics

return out, mse[0], scaledResidualVariance, rSquared, fStatistic


def prediction(inputData, result: RegressionResult):
inputs = inputData[:, 1:]
outputs = inputData[:, 0]

params = result.paramsAndImportance[:, 0]

expr = sp.sympify(' '.join(
[
f'{param}' if m == 'c' else f' + ({param}) * {m}'
for param, m in zip(params, result.monomials)
]
))

results = []

for y, xValues in zip(outputs, inputs):
subsDict = dict(zip(expr.free_symbols, xValues))
predictedResult = expr.subs(subsDict)
difference = predictedResult - y

results.append([y, predictedResult, difference, 0.0])

results = np.array(results, dtype=np.float32)

# Расчет среднего значения и стандартного отклонения разностей
meanDifference = np.mean(results[:, 2])
stdDifference = np.std(results[:, 2])

# Установка флага 1.0, если разность выходит за пределы 3 стандартных отклонений
for row in results:
if abs(row[2] - meanDifference) > 3 * stdDifference:
row[3] = 1.0

return results
33 changes: 33 additions & 0 deletions statapp/models/prediction_table_model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#
# Copyright (c) 2023 Maxim Slipenko, Eugene Lazurenko.
#
# This file is part of Statapp
# (see https://github.com/shizand/statapp).
#
# This program 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.
#
# This program 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 for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
from PySide2.QtCore import Qt

from statapp.models.ro_table_model import ROTableModel


class PreditionTableModel(ROTableModel):
def getHorizontalHeader(self):
return ['Отклик', 'Прогноз', 'Отклонение', '1-3 сигмовые зоны']

def data(self, index, role):
if role == Qt.DisplayRole and index.column() == 3:
value = super().data(index, role)
return 'x' if value == 1 else ''
return super().data(index, role)
2 changes: 1 addition & 1 deletion statapp/polynoms/linear_polynom_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@
class LinearPolynomWindow(PolynomWindow):
def __init__(self, data):
result = linearPolynom(data)
super().__init__(result, "Линейный полином")
super().__init__(data, result, "Линейный полином")
10 changes: 9 additions & 1 deletion statapp/polynoms/polynom_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,17 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
from PySide2.QtWidgets import QDialog, QHeaderView

from statapp.calculations import prediction
from statapp.mathtex_header_view import MathTexHeaderView
from statapp.models.prediction_table_model import PreditionTableModel
from statapp.models.regression_result_model import RegressionResultModel
from statapp.ui.ui_polynom_window import Ui_PolynomWindow
from statapp.utils import addIcon, FloatDelegate


class PolynomWindow(QDialog):
def __init__(self, result, windowTitle):
def __init__(self, data, result, windowTitle):
super().__init__()
self.ui = Ui_PolynomWindow()
self.ui.setupUi(self)
Expand All @@ -43,3 +46,8 @@ def __init__(self, result, windowTitle):
self.ui.scaledResidualVarianceValueLabel.setText(str(result.scaledResidualVariance))
self.ui.fStatisticValueLabel.setText(str(result.fStatistic))
self.ui.rSquaredValueLabel.setText(str(result.scaledResidualVariance))

self.predictionModel = PreditionTableModel(prediction(data, result))
self.ui.predictionTableView.setModel(self.predictionModel)
header = self.ui.predictionTableView.horizontalHeader()
header.setSectionResizeMode(QHeaderView.ResizeMode.Stretch)
2 changes: 1 addition & 1 deletion statapp/polynoms/squared_polynom_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@
class SquaredPolynomWindow(PolynomWindow):
def __init__(self, data):
result = squaredPolynom(data)
super().__init__(result, "Квадратичный полином")
super().__init__(data, result, "Квадратичный полином")
11 changes: 9 additions & 2 deletions statapp/polynoms/transform_polynom_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@
from PySide2.QtCore import Qt
from PySide2.QtWidgets import QDialog, QHeaderView

from statapp.calculations import linearPolynom
from statapp.calculations import linearPolynom, prediction
from statapp.combo_delegate import ComboDelegate
from statapp.mathtex_header_view import MathTexHeaderView
from statapp.models.prediction_table_model import PreditionTableModel
from statapp.models.transform_polynom_model import TransformPolynomModel, TRANSFORMS
from statapp.ui.ui_transform_polynom_window import Ui_PolynomWindow
from statapp.ui.ui_polynom_window import Ui_PolynomWindow
from statapp.utils import addIcon


Expand All @@ -40,6 +41,11 @@ def __init__(self, data):
self.data = data
result = linearPolynom(data)

self.predictionModel = PreditionTableModel(prediction(data, result))
self.ui.predictionTableView.setModel(self.predictionModel)
header = self.ui.predictionTableView.horizontalHeader()
header.setSectionResizeMode(QHeaderView.ResizeMode.Stretch)

# Создание столбца из нулей
zeroCol = np.zeros((result.paramsAndImportance.shape[0], 1))
# Добавление столбца к исходному массиву
Expand Down Expand Up @@ -78,6 +84,7 @@ def on_data_changed(self):

def rebuildData(self, data):
result = linearPolynom(data)
self.predictionModel.updateAllData(prediction(data, result))
zeroCol = np.zeros((result.paramsAndImportance.shape[0], 1))
result.paramsAndImportance = np.column_stack((zeroCol, result.paramsAndImportance))
self.model.updateAllData(result)
Expand Down
180 changes: 104 additions & 76 deletions statapp/ui/polynom_window.ui
Original file line number Diff line number Diff line change
Expand Up @@ -6,93 +6,121 @@
<rect>
<x>0</x>
<y>0</y>
<width>630</width>
<height>400</height>
<width>537</width>
<height>444</height>
</rect>
</property>
<property name="windowTitle">
<string>Полином</string>
</property>
<layout class="QGridLayout" name="gridLayout_2">
<item row="0" column="0">
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="0">
<widget class="QTableView" name="tableView">
<attribute name="horizontalHeaderMinimumSectionSize">
<number>40</number>
</attribute>
<attribute name="verticalHeaderMinimumSectionSize">
<number>40</number>
</attribute>
<attribute name="verticalHeaderDefaultSectionSize">
<number>40</number>
</attribute>
</widget>
</item>
<item row="1" column="0">
<layout class="QGridLayout" name="polynomResult">
<property name="topMargin">
<number>10</number>
</property>
<item row="0" column="1">
<widget class="QLabel" name="residualVarianceValueLabel">
<property name="text">
<string>undefined</string>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QLabel" name="scaledResidualVarianceValueLabel">
<property name="text">
<string>undefined</string>
</property>
</widget>
</item>
<item row="2" column="0">
<widget class="QLabel" name="fStatisticLabel">
<property name="text">
<string>F1 - отношение Фишера</string>
</property>
</widget>
</item>
<item row="0" column="6">
<widget class="QTabWidget" name="tabWidget">
<property name="currentIndex">
<number>0</number>
</property>
<widget class="QWidget" name="model">
<attribute name="title">
<string>Модель</string>
</attribute>
<layout class="QGridLayout" name="gridLayout_3">
<item row="0" column="0">
<widget class="QLabel" name="residualVarianceLabel">
<property name="text">
<string>Остаточная дисперсия:</string>
</property>
</widget>
<layout class="QGridLayout" name="gridLayout">
<item row="1" column="3">
<widget class="QTableView" name="tableView">
<attribute name="horizontalHeaderMinimumSectionSize">
<number>40</number>
</attribute>
<attribute name="verticalHeaderMinimumSectionSize">
<number>40</number>
</attribute>
<attribute name="verticalHeaderDefaultSectionSize">
<number>40</number>
</attribute>
</widget>
</item>
<item row="2" column="3">
<layout class="QGridLayout" name="polynomResult">
<property name="topMargin">
<number>10</number>
</property>
<item row="0" column="1">
<widget class="QLabel" name="residualVarianceValueLabel">
<property name="text">
<string>undefined</string>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QLabel" name="scaledResidualVarianceValueLabel">
<property name="text">
<string>undefined</string>
</property>
</widget>
</item>
<item row="2" column="0">
<widget class="QLabel" name="fStatisticLabel">
<property name="text">
<string>F1 - отношение Фишера</string>
</property>
</widget>
</item>
<item row="0" column="0">
<widget class="QLabel" name="residualVarianceLabel">
<property name="text">
<string>Остаточная дисперсия:</string>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QLabel" name="scaledResidualVarianceLabel">
<property name="text">
<string>Остаточная дисперсия (масштабированная):</string>
</property>
</widget>
</item>
<item row="3" column="0">
<widget class="QLabel" name="rSquaredLabel">
<property name="text">
<string>Коэффициент множественной дереминизации</string>
</property>
</widget>
</item>
<item row="2" column="1">
<widget class="QLabel" name="fStatisticValueLabel">
<property name="text">
<string>undefined</string>
</property>
</widget>
</item>
<item row="3" column="1">
<widget class="QLabel" name="rSquaredValueLabel">
<property name="text">
<string>undefined</string>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</item>
<item row="1" column="0">
<widget class="QLabel" name="scaledResidualVarianceLabel">
<property name="text">
<string>Остаточная дисперсия (масштабированная):</string>
</property>
</widget>
</item>
<item row="3" column="0">
<widget class="QLabel" name="rSquaredLabel">
<property name="text">
<string>Коэффициент множественной дереминизации</string>
</property>
</widget>
</item>
<item row="2" column="1">
<widget class="QLabel" name="fStatisticValueLabel">
<property name="text">
<string>undefined</string>
</property>
</widget>
</item>
<item row="3" column="1">
<widget class="QLabel" name="rSquaredValueLabel">
<property name="text">
<string>undefined</string>
</property>
</layout>
</widget>
<widget class="QWidget" name="prediction">
<attribute name="title">
<string>Прогноз</string>
</attribute>
<layout class="QGridLayout" name="gridLayout_4">
<item row="0" column="0">
<widget class="QTableView" name="predictionTableView">
<attribute name="verticalHeaderVisible">
<bool>false</bool>
</attribute>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
</widget>
</item>
</layout>
</widget>
Expand Down
Loading

0 comments on commit 8ea3cff

Please sign in to comment.