fixing some codestyles

This commit is contained in:
nazarchuk.as 2022-11-12 20:19:50 +03:00
parent 059abdc5e6
commit d33affdf1d
10 changed files with 127 additions and 142 deletions

View File

@ -10,6 +10,7 @@ import numpy as np
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
from scipy.optimize import curve_fit from scipy.optimize import curve_fit
class Data: class Data:
def __init__(self, x=[], y=[], xlabel='', ylabel='', caption='', xerr=None, def __init__(self, x=[], y=[], xlabel='', ylabel='', caption='', xerr=None,
yerr=None, through_0=0, data_filename='', color=None, yerr=None, through_0=0, data_filename='', color=None,
@ -19,8 +20,10 @@ class Data:
self.xlabel = xlabel self.xlabel = xlabel
self.ylabel = ylabel self.ylabel = ylabel
self.caption = caption self.caption = caption
self.xerr=xerr self.cap_point = None
self.yerr=yerr self.line_point = None
self.xerr = xerr
self.yerr = yerr
self.make_errors(xerr, yerr) self.make_errors(xerr, yerr)
self.through_0 = through_0 self.through_0 = through_0
if not color: if not color:
@ -30,14 +33,18 @@ class Data:
self.centering = centering self.centering = centering
self.size = size self.size = size
self.coefficient = coefficient self.coefficient = coefficient
self.data_filename=data_filename self.data_filename = data_filename
self.saving=saving self.saving = saving
def make_errors(self, xerr=None, yerr=None): def make_errors(self, xerr=None, yerr=None):
if not xerr: xerr=self.xerr if not xerr:
if not yerr: yerr=self.yerr xerr = self.xerr
if not xerr: xerr=0 if not yerr:
if not yerr: yerr=0 yerr = self.yerr
if not xerr:
xerr = 0
if not yerr:
yerr = 0
if not yerr or type(yerr) == float or type(yerr) == int: if not yerr or type(yerr) == float or type(yerr) == int:
self.yerr = [yerr for _ in self.y] self.yerr = [yerr for _ in self.y]
else: else:
@ -51,28 +58,32 @@ class Data:
with open(self.data_filename) as file: with open(self.data_filename) as file:
reader = list(csv.reader(file)) reader = list(csv.reader(file))
data = np.array(reader) data = np.array(reader)
dic=[] dic = []
for i in range(len(data[0])): for i in range(len(data[0])):
micro_data = [] micro_data = []
for j in range(len(data)): for j in range(len(data)):
micro_data.append(data[j][i]) micro_data.append(data[j][i])
dic.append(micro_data) dic.append(micro_data)
data=dic data = dic
dic = {} dic = {}
for i in range(len(data)): for i in range(len(data)):
dic[data[i][0]] = np.array(data[i][1:]).astype(np.float) dic[data[i][0]] = np.array(data[i][1:]).astype(np.float)
data = dic data = dic
self.data=data self.data = data
def make_point_grafic(self): def make_point_grafic(self):
self.make_errors() self.make_errors()
if self.cap_point:
cap = self.cap_point
else:
cap = self.caption
if self.xerr[1] != 0 or self.yerr[1] != 0: if self.xerr[1] != 0 or self.yerr[1] != 0:
plt.errorbar(self.x, self.y, yerr=self.yerr, xerr=self.xerr, linewidth=4, plt.errorbar(self.x, self.y, yerr=self.yerr, xerr=self.xerr, linewidth=4,
linestyle='', label=self.caption, color=self.color[0], linestyle='', label=cap, color=self.color[0],
ecolor=self.color[0], elinewidth=1, capsize=3.4, ecolor=self.color[0], elinewidth=1, capsize=3.4,
capthick=1.4) capthick=1.4)
else: else:
plt.scatter(self.x, self.y, linewidth=0.005, label=self.caption, plt.scatter(self.x, self.y, linewidth=0.005, label=cap,
color=self.color[0], edgecolor='black', s=self.size) color=self.color[0], edgecolor='black', s=self.size)
if not self.centering: if not self.centering:
@ -98,18 +109,24 @@ class Data:
xmax = max(self.x)*self.coefficient[1] xmax = max(self.x)*self.coefficient[1]
else: else:
xmax = max(self.x)*self.coefficient[0] xmax = max(self.x)*self.coefficient[0]
if self.line_point:
cap = self.line_point
else:
cap = self.caption
x = np.arange(xmin, xmax, (xmax-xmin)/10000) x = np.arange(xmin, xmax, (xmax-xmin)/10000)
plt.plot(x, k*x+b, label=self.caption, linewidth=2.4, plt.plot(x, k*x+b, label=cap, linewidth=2.4,
linestyle='-', color=self.color[1]) linestyle='-', color=self.color[1])
def make_grafic(self, named_by_points=True): def make_grafic(self, named_by_points=True):
plt.figure(dpi=500, figsize=(8, 5))
self.make_errors() self.make_errors()
if named_by_points: if named_by_points:
cap_point = self.caption self.cap_point = self.caption
line_point = None self.line_point = None
else: else:
line_point = self.caption self.line_point = self.caption
cap_point = None self.cap_point = None
self.make_point_grafic() self.make_point_grafic()
k, b, sigma = self.approx() k, b, sigma = self.approx()

View File

@ -2,38 +2,19 @@ import os
import sys import sys
import csv import csv
import time import time
from PyQt6 import QtCore from PyQt6.QtGui import QIcon
from PyQt6.QtGui import QAction, QIcon, QPixmap from PyQt6.QtWidgets import (QApplication,
from PyQt6.QtWidgets import (QHBoxLayout,
QApplication,
QCheckBox,
QComboBox,
QDateEdit,
QDateTimeEdit,
QDial,
QDoubleSpinBox,
QFontComboBox,
QLabel,
QLCDNumber,
QLineEdit, QLineEdit,
QMainWindow,
QProgressBar,
QPushButton, QPushButton,
QRadioButton,
QSlider,
QSpinBox,
QTimeEdit,
QVBoxLayout,
QWidget, QWidget,
QTableWidget, QTableWidget,
QGridLayout, QGridLayout,
QMenu,
QTableWidgetItem, QTableWidgetItem,
QHeaderView, QHeaderView,
QTextBrowser,
) )
from Abstract_window import AbstractWindow from Abstract_window import AbstractWindow
from Main_experiment_window import * from Main_experiment_window import (MainExperimentDataWindow,
MainExperimentChartWindow)
class Start: class Start:
@ -82,7 +63,7 @@ class StartWindow(AbstractWindow):
self.lineEdit = QLineEdit(placeholderText='Введите фамилию') self.lineEdit = QLineEdit(placeholderText='Введите фамилию')
self.lineEdit.returnPressed.connect(self.enter_name) self.lineEdit.returnPressed.connect(self.enter_name)
self.flow = QPushButton('измерение потока') self.flow = QPushButton('градуировка электромагнита')
self.flow.clicked.connect(self.flow_click) self.flow.clicked.connect(self.flow_click)
self.flow.setEnabled(False) self.flow.setEnabled(False)
self.main = QPushButton('основной эксперимент') self.main = QPushButton('основной эксперимент')
@ -118,35 +99,41 @@ class StartWindow(AbstractWindow):
class FlowWindow(AbstractWindow): class FlowWindow(AbstractWindow):
def __init__(self, parent): def __init__(self, parent):
super().__init__() super().__init__()
self.setWindowTitle('Градуировка электромагнита')
self.parent = parent self.parent = parent
self.parent = parent
# make csv file
self.parent.flow_dataname = 'Induction_data.csv'
head_1 = 'B,mTl'
head_2 = 'U,mV'
head_3 = 't,ms'
with open(os.path.join(self.parent.folder, self.parent.flow_dataname), 'w') as file:
wr = csv.writer(file)
wr.writerow([head_1, head_2, head_3])
self.centralwidget = QWidget() self.centralwidget = QWidget()
self.resize(1400, 800) self.resize(1400, 800)
self.setCentralWidget(self.centralwidget) self.setCentralWidget(self.centralwidget)
grid_layout = QGridLayout(self.centralwidget) self.grid_layout = QGridLayout(self.centralwidget)
self.start = QPushButton('Старт') self.start = QPushButton('Старт')
self.start.clicked.connect(self.start_clicked) self.start.clicked.connect(self.start_clicked)
self.start.setEnabled(True) self.start.setEnabled(True)
self.next = QPushButton(self)
self.next.setIcon(QIcon('arrow.png'))
self.next.setEnabled(False)
self.next.clicked.connect(self.next_clicked)
self.lineEdit = QLineEdit(placeholderText='Индукция B, мТл') self.lineEdit = QLineEdit(placeholderText='Индукция B, мТл')
self.lineEdit.returnPressed.connect(self.enter_value) self.lineEdit.returnPressed.connect(self.enter_value)
self.lineEdit.setReadOnly(True) self.lineEdit.setReadOnly(True)
self.parent.flow_dataname = 'Induction_data.csv'
head_1 = 'B,mTl'
head_2 = 'U,mV'
head_3 = 't,s'
with open(os.path.join(self.parent.folder, self.parent.flow_dataname), 'w') as file:
wr = csv.writer(file)
wr.writerow([head_1, head_2, head_3])
self.table = QTableWidget(self) # Create a self.table self.table = QTableWidget(self) # Create a self.table
self.table.setColumnCount(3) # Set three columns self.table.setColumnCount(3) # Set three columns
self.table.setRowCount(0) self.table.setRowCount(0)
self.table.setHorizontalHeaderLabels([head_1, head_2, head_3]) self.table.setHorizontalHeaderLabels([head_1, head_2, head_3])
header = self.table.horizontalHeader() header = self.table.horizontalHeader()
header.setSectionResizeMode(0, QHeaderView.ResizeMode.Stretch) header.setSectionResizeMode(0, QHeaderView.ResizeMode.Stretch)
header.setSectionResizeMode(1, QHeaderView.ResizeMode.Stretch) header.setSectionResizeMode(1, QHeaderView.ResizeMode.Stretch)
@ -156,22 +143,15 @@ class FlowWindow(AbstractWindow):
self.table.setItem(0, 2, QTableWidgetItem("Text in column 3")) self.table.setItem(0, 2, QTableWidgetItem("Text in column 3"))
self.table.resizeColumnsToContents() self.table.resizeColumnsToContents()
self.next = QPushButton(self) self.grid_layout.addWidget(self.start, 0, 0)
self.next.setIcon(QIcon('arrow.png')) self.grid_layout.addWidget(self.lineEdit, 1, 0)
self.next.setEnabled(False) self.grid_layout.addWidget(self.table, 1, 3)
self.next.clicked.connect(self.next_clicked) self.grid_layout.addWidget(self.next, 2, 0)
grid_layout.addWidget(self.start, 0, 0)
grid_layout.addWidget(self.lineEdit, 1, 0)
grid_layout.addWidget(self.table, 1, 3)
grid_layout.addWidget(self.next, 2, 0)
def next_clicked(self): def next_clicked(self):
self.parent.number = 20 self.parent.number = 20
self.parent.change_number() self.parent.change_number()
def start_clicked(self): def start_clicked(self):
self.start.setEnabled(False) self.start.setEnabled(False)
self.start_time = round(time.time()*1000) self.start_time = round(time.time()*1000)

View File

@ -10,37 +10,19 @@ import csv
import time import time
from PyQt6 import QtCore from PyQt6 import QtCore
from PyQt6.QtGui import QIcon, QPixmap from PyQt6.QtGui import QIcon, QPixmap
from PyQt6.QtWidgets import (QHBoxLayout, from PyQt6.QtWidgets import (QLabel,
QApplication,
QCheckBox,
QComboBox,
QDateEdit,
QDateTimeEdit,
QDial,
QDoubleSpinBox,
QFontComboBox,
QLabel,
QLCDNumber,
QLineEdit, QLineEdit,
QMainWindow,
QProgressBar,
QPushButton, QPushButton,
QRadioButton,
QSlider,
QSpinBox,
QTimeEdit,
QVBoxLayout,
QWidget, QWidget,
QTableWidget, QTableWidget,
QGridLayout, QGridLayout,
QMenu,
QTableWidgetItem, QTableWidgetItem,
QHeaderView, QHeaderView,
QTextBrowser, QTextBrowser)
)
from Analisis_data import Data from Analisis_data import Data
from Abstract_window import AbstractWindow from Abstract_window import AbstractWindow
class ThreadData(QtCore.QThread): class ThreadData(QtCore.QThread):
signal = QtCore.pyqtSignal(str) signal = QtCore.pyqtSignal(str)
@ -58,11 +40,9 @@ class ThreadData(QtCore.QThread):
class MainExperimentDataWindow(AbstractWindow): class MainExperimentDataWindow(AbstractWindow):
def __init__(self, parent): def __init__(self, parent):
# TODO : clean code
super().__init__() super().__init__()
self.setWindowTitle('Основной эксперимент. Получение данных') self.setWindowTitle('Основной эксперимент. Получение данных')
self.start_time = round(time.time()*1000)
self.parent = parent self.parent = parent
self.data_thread = ThreadData(self) self.data_thread = ThreadData(self)
self.resize(1400, 800) self.resize(1400, 800)
@ -71,13 +51,14 @@ class MainExperimentDataWindow(AbstractWindow):
self.parent.dataname = 'data.csv' self.parent.dataname = 'data.csv'
head_1 = 'I_0,mA' head_1 = 'I_0,mA'
head_2 = 'U_34,mV' head_2 = 'U_34,mV'
head_3 = 't,s' head_3 = 't,ms'
with open(os.path.join(self.parent.folder, self.parent.dataname), 'w') as file: with open(os.path.join(self.parent.folder, self.parent.dataname), 'w') as file:
wr = csv.writer(file) wr = csv.writer(file)
wr.writerow([head_1, head_2, head_3]) wr.writerow([head_1, head_2, head_3])
self.centralwidget = QWidget() self.centralwidget = QWidget()
self.setCentralWidget(self.centralwidget) self.setCentralWidget(self.centralwidget)
self.grid_layout = QGridLayout(self.centralwidget)
self.start = QPushButton('Старт') self.start = QPushButton('Старт')
self.start.clicked.connect(self.start_clicked) self.start.clicked.connect(self.start_clicked)
@ -92,34 +73,26 @@ class MainExperimentDataWindow(AbstractWindow):
self.next.setEnabled(False) self.next.setEnabled(False)
self.next.clicked.connect(self.next_clicked) self.next.clicked.connect(self.next_clicked)
grid_layout = QGridLayout(self.centralwidget) self.lineEdit = QLineEdit(placeholderText='Введите что-то')
self.lineEdit.returnPressed.connect(self.enter_smth)
self.table = QTableWidget(self) # Create a self.table # make table
self.table.setColumnCount(3) # Set three columns self.table = QTableWidget(self)
self.table.setColumnCount(3)
self.table.setRowCount(0) self.table.setRowCount(0)
self.table.setHorizontalHeaderLabels([head_1, head_2, head_3]) self.table.setHorizontalHeaderLabels([head_1, head_2, head_3])
header = self.table.horizontalHeader() header = self.table.horizontalHeader()
header.setSectionResizeMode(0, QHeaderView.ResizeMode.Stretch) header.setSectionResizeMode(0, QHeaderView.ResizeMode.Stretch)
header.setSectionResizeMode(1, QHeaderView.ResizeMode.Stretch) header.setSectionResizeMode(1, QHeaderView.ResizeMode.Stretch)
header.setSectionResizeMode(2, QHeaderView.ResizeMode.Stretch) header.setSectionResizeMode(2, QHeaderView.ResizeMode.Stretch)
self.table.setItem(0, 0, QTableWidgetItem("Text in column 1"))
self.table.setItem(0, 1, QTableWidgetItem("Text in column 2"))
self.table.setItem(0, 2, QTableWidgetItem("Text in column 3"))
self.table.resizeColumnsToContents() self.table.resizeColumnsToContents()
self.lineEdit = QLineEdit(placeholderText='Введите что-то')
self.lineEdit.returnPressed.connect(self.enter_smth)
# Adding the table to the grid # Adding the table to the grid
grid_layout.addWidget(self.table, 0, 0, -1, 1) self.grid_layout.addWidget(self.table, 0, 0, -1, 1)
grid_layout.addWidget(self.lineEdit, 0, 2, -1, -1) self.grid_layout.addWidget(self.lineEdit, 0, 2, -1, -1)
grid_layout.addWidget(self.next, 2, 2, -1, -1) self.grid_layout.addWidget(self.next, 2, 2, -1, -1)
grid_layout.addWidget(self.start, 1, 2) self.grid_layout.addWidget(self.start, 1, 2)
grid_layout.addWidget(self.stop, 1, 3) self.grid_layout.addWidget(self.stop, 1, 3)
self.parent.draw()
def enter_smth(self): def enter_smth(self):
# TODO # TODO
@ -134,19 +107,6 @@ class MainExperimentDataWindow(AbstractWindow):
if not self.data_thread.isRunning(): if not self.data_thread.isRunning():
self.data_thread.start() self.data_thread.start()
def no_data(self):
current_time = round(time.time()*1000)
v = str((current_time-self.start_time)/60)
a = v
t = str(current_time-self.start_time)
with open(os.path.join(self.parent.folder, self.parent.dataname), 'a') as file:
wr = csv.writer(file)
wr.writerow([v, a, str(current_time-self.start_time)])
self.table.insertRow(self.table.rowCount())
self.table.setItem(self.table.rowCount()-1, 0, QTableWidgetItem(v))
self.table.setItem(self.table.rowCount()-1, 1, QTableWidgetItem(a))
self.table.setItem(self.table.rowCount()-1, 2, QTableWidgetItem(t))
def stop_clicked(self): def stop_clicked(self):
self.data_thread.running = False self.data_thread.running = False
self.stop.setEnabled(False) self.stop.setEnabled(False)
@ -156,6 +116,22 @@ class MainExperimentDataWindow(AbstractWindow):
self.parent.number = 21 self.parent.number = 21
self.parent.change_number() self.parent.change_number()
def no_data(self):
current_time = round(time.time()*1000)
v = str((current_time-self.start_time)/60)
a = v
t = str(current_time-self.start_time)
self.save_data(v, a, t)
def save_data(self, v, a, t):
with open(os.path.join(self.parent.folder, self.parent.dataname), 'a') as file:
wr = csv.writer(file)
wr.writerow([v, a, t])
self.table.insertRow(self.table.rowCount())
self.table.setItem(self.table.rowCount()-1, 0, QTableWidgetItem(v))
self.table.setItem(self.table.rowCount()-1, 1, QTableWidgetItem(a))
self.table.setItem(self.table.rowCount()-1, 2, QTableWidgetItem(t))
def take_data(self): def take_data(self):
# measure voltage and current # measure voltage and current
volt_name = os.path.join('/dev', 'usbtmc1') volt_name = os.path.join('/dev', 'usbtmc1')
@ -173,22 +149,24 @@ class MainExperimentDataWindow(AbstractWindow):
a = f_amp.read(15) a = f_amp.read(15)
f_volt.close() f_volt.close()
f_amp.close() f_amp.close()
current_time = round(time.time()*1000) current_time = round(time.time()*1000)
with open(os.path.join(self.parent.folder, self.parent.dataname), 'a') as file: t = str(current_time-self.start_time)
wr = csv.writer(file) self.save_data(v, a, t)
wr.writerow([v, a, str(current_time-self.start_time)])
class MainExperimentChartWindow(AbstractWindow): class MainExperimentChartWindow(AbstractWindow):
def __init__(self, parent): def __init__(self, parent):
super().__init__() super().__init__()
self.setWindowTitle('Основной эксперимент. Обработка данных')
self.parent = parent self.parent = parent
self.parent.chartname = 'Chart' self.parent.chartname = 'Chart'
self.parent.data = Data(data_filename=os.path.join(self.parent.folder, self.parent.dataname), self.parent.data = Data(data_filename=os.path.join(self.parent.folder, self.parent.dataname),
saving=os.path.join(self.parent.folder, self.parent.chartname)) saving=os.path.join(self.parent.folder, self.parent.chartname))
self.parent.data.read_csv() self.parent.data.read_csv()
self.parent.data.x=self.parent.data.data['I_0,mA'] self.parent.data.x = self.parent.data.data['I_0,mA']
self.parent.data.y=self.parent.data.data['U_34,mV'] self.parent.data.y = self.parent.data.data['U_34,mV']
self.parent.data.make_grafic() self.parent.data.make_grafic()
self.setWindowTitle('Основной эксперимент. Обработка данных') self.setWindowTitle('Основной эксперимент. Обработка данных')
@ -197,7 +175,8 @@ class MainExperimentChartWindow(AbstractWindow):
self.centralwidget = QWidget() self.centralwidget = QWidget()
self.setCentralWidget(self.centralwidget) self.setCentralWidget(self.centralwidget)
pixmap = QPixmap(os.path.join(self.parent.folder, self.parent.chartname)) pixmap = QPixmap(os.path.join(
self.parent.folder, self.parent.chartname))
self.label = QLabel(self) self.label = QLabel(self)
self.label.setPixmap(pixmap) self.label.setPixmap(pixmap)
self.label.resize(pixmap.width(), pixmap.height()) self.label.resize(pixmap.width(), pixmap.height())
@ -208,4 +187,3 @@ class MainExperimentChartWindow(AbstractWindow):
self.hbox_layout = QGridLayout(self.centralwidget) self.hbox_layout = QGridLayout(self.centralwidget)
self.hbox_layout.addWidget(self.label, 0, 0) self.hbox_layout.addWidget(self.label, 0, 0)
self.hbox_layout.addWidget(self.text, 0, 1) self.hbox_layout.addWidget(self.text, 0, 1)

Binary file not shown.

Before

Width:  |  Height:  |  Size: 10 KiB

After

Width:  |  Height:  |  Size: 136 KiB

View File

@ -1 +1,11 @@
"B,mTl","U,mV","t,s" "B,mTl","U,mV","t,ms"
80,45.18333333333333,2711
90,70.75,4245
90,85.66666666666667,5140
90,99.33333333333333,5960
90,111.96666666666667,6718
90,124.51666666666667,7471
90,136.53333333333333,8192
90,148.96666666666667,8938
90,164.7,9882
90,177.73333333333332,10664

1 B,mTl U,mV t,s t,ms
2 80 45.18333333333333 2711
3 90 70.75 4245
4 90 85.66666666666667 5140
5 90 99.33333333333333 5960
6 90 111.96666666666667 6718
7 90 124.51666666666667 7471
8 90 136.53333333333333 8192
9 90 148.96666666666667 8938
10 90 164.7 9882
11 90 177.73333333333332 10664

View File

@ -1,2 +1,2 @@
"I_0,mA","U_34,mV","t,s" "I_0,mA","U_34,mV","t,ms"
0.016666666666666666,0.016666666666666666,1 0.0,0.0,0

1 I_0,mA U_34,mV t,s t,ms
2 0.016666666666666666 0.0 0.016666666666666666 0.0 1 0