Related
I am getting a AttributeError as follows:
self.filtered_df.to_excel(self.output_filepath, index=False)
AttributeError: 'tuple' object has no attribute 'to_excel'
I am inheriting a class for another class I am developing, currently the inheritance allows me to output one excel file, can I change the method in this class to allow me to output more than one excel file in the new class I am developing?
Here is the class, inherited:
class ReportQueryCommand(LogCommand):
"""Performs reports through a ReportQueryStrategy instance.
It is possible to overwrite existing queries; this means that it is possible
to perform subsequent filters by launching multiple commands having
key_input = strategy.name.
Attributes:
state: DatasetState containing so-far computed DataFrames.
strategy: defines how to perform the query.
filepath: optional. Defines where to save the queried DataFrame.
key_input: optional (None if df_to_filter is not None).
Defines a key that allows to access a DataFrame in the State.
df_to_filter: optional (None if key is not None).
DataFrame to apply the query on.
"""
def __init__(self,
strategy: ReportQueryStrategy,
base_path: Optional[str] = None,
key_input: Optional[str] = None,
key_output: Optional[str] = None,
df_to_filter: Optional[pd.DataFrame] = None,
filepath: Optional[str] = None,
directory_path: Optional[str] = None,
write_output: bool = True):
super().__init__(base_path=base_path,
directory_path=directory_path,
copy_before_update=False,
write_output=write_output)
self.state = DatasetState()
self.strategy = strategy
self.filepath = filepath
self.key_input = key_input
self._key_output = key_output
self.df_to_filter = df_to_filter
self.filtered_df = None
#property
def _output_file(self) -> str:
return self.strategy.output_filename
def write_to_file(self):
if self.filtered_df is None:
raise ValueError("Missing computed dataframe")
self.filtered_df.to_excel(self.output_filepath, index=False)
#property
def key_output(self) -> str:
# first scenario: key was defined
if self._key_output is not None:
return self._key_output
# second scenario: key was not defined, by default,
# concatenate key_input and strategy
strategy_output_name = self.strategy.output_name
if self.key_input is not None:
return f"{self.key_input}-{strategy_output_name}"
# WCS: no key defined, just assign the strategy key
return strategy_output_name
def execute(self, output_from_previous: Any = None, **kwargs) -> Any:
self.filtered_df = self.strategy.transform(key=self.key_input,
df=self.df_to_filter)
self.state.query_reports[self.key_output] = self.filtered_df
super().execute(output_from_previous=output_from_previous, **kwargs)
As you can see here the method I need to update is 'def write_to_file(self):'
Here is snippets of the relevant code in development to show where this problems 'could be occuring'. The following bits of code below are relevant, this might need to be updated to allow me to output two excel files or more:
class TtestStrategy(ReportQueryStrategy):
"""
"""
#staticmethod
def _strategy_key():
return 't-test', 'fisher-test'
def __init__(self,
query_name: Optional[str] = None,
reference_query: str = None,
sample_query: str = None,
alpha: float = 0.05,
chemical_key: str = 'chemical',
plate_key: str = 'plate',
value_key: str = 'processed_relative_fp',
group_column: str = 'Lot',
return_pivoted: bool = True):
super().__init__(query_name=query_name)
self.pvalues = []
self.alpha = alpha
self.chemical_key, self.plate_key, self.value_key, self.reference_query, self.sample_query = (chemical_key,
plate_key,
value_key,
reference_query,
sample_query)
self.group_column = group_column
self.return_pivoted = return_pivoted
def fishers_method(self, pvalues) -> tuple[bool, float, float, float]:
pvalues = [item for item in pvalues if not pd.isnull(item)]
comb_z, comb_pval = stats.combine_pvalues(pvalues, method="fisher")
k = len(pvalues)
mean_FDR = (self.alpha * (k + 1)) / (2 * k)
if comb_pval > mean_FDR:
decision = False
else:
decision = True
return decision, comb_z, comb_pval, mean_FDR
def transform(self,
key: Optional[str] = None,
df: Optional[pd.DataFrame] = None) -> tuple[Any, DataFrame]:
counter_nosig = 0
counter_sig_st_t = 0
pval_store_st_t = {}
pval_store_st_t_dec = {}
pval_store_st_t_val = {}
pval_store_st_t_val_adj = {}
pval_store_norm_A = {}
pval_store_norm_B = {}
var_store_A = {}
var_store_B = {}
pval_store_lev_bart = {}
decisions = []
st_pval_arr = []
df = super().transform(key=key, df=df)
df = df.loc[(df[self.group_column] == self.reference_query) | (df[self.group_column] == self.sample_query)]
df_i = df.filter(['plate', 'chemical', 'processed_relative_fp'], axis=1)
df_i = df.pivot(columns='plate', values='processed_relative_fp', index='chemical')
df_i.index.name = None
df_i.columns.name = ''
plates = {exps: {"processed_relative_fp": series}
for exps, series in df_i.to_dict("series").items()}
chem_order = list(df['chemical'].unique())
for chem in chem_order:
if chem != 'empty':
pool = []
sample_l = []
for q in [self.reference_query, self.sample_query]:
sample = pd.Series([sample for sample in plates])
sample = sample[sample.str.contains(q)]
for s in sample:
sample_l.append(s)
record = plates[s]
if record['processed_relative_fp'] is not None:
rel_fp = record['processed_relative_fp']
lot = s.split("_")[1]
cell = s.split('_')[2]
rep = s.split("_")[4]
val = rel_fp[chem]
pool.append({"chems": chem, "lot": lot, "cell": cell, "replicate": rep,
"key": chem + "::" + lot + "::" + rep + "::" + cell, "value": val})
pool = pd.DataFrame(pool)
massage = []
averages = []
sort_lots = list(pool['lot'].unique())
for lot in sort_lots:
value = list(pool[pool.lot.eq(lot)]['value'].dropna())
averages.append(np.mean(np.array(value)))
massage.append(value)
averages = np.array(averages)
min_ = np.nanmin(averages)
max_ = np.nanmax(averages)
pos_min_ = np.where(averages == min_)[0][0]
pos_max_ = np.where(averages == max_)[0][0]
perc_diff = (max_ - min_) * 100
fvalue_st_t, pvalue_st_t = stats.ttest_ind(*massage, equal_var=False)
st_pval_arr.append(pvalue_st_t)
st_pval_array = np.asarray(st_pval_arr)
mask = np.isfinite(st_pval_array)
st_t_t_pval_adj = np.empty(st_pval_array.shape)
st_t_t_pval_adj.fill(np.nan)
rej_st_t, st_t_t_pval_adj[mask], _, _ = sm.stats.multipletests(st_pval_array[mask], method='fdr_bh')
for v in st_t_t_pval_adj:
float(v)
pval_store_st_t_val_adj[chem] = v
for z in rej_st_t:
float(z)
if len(massage[0]) >= 3:
test_stat_norm_A, pvalue_norm_A = stats.shapiro(np.array(massage[0]))
if pvalue_norm_A < 0.05:
pval_store_norm_A[chem] = 'No'
else:
pval_store_norm_A[chem] = 'Yes'
else:
pval_store_norm_A[chem] = 'Not enough data'
if len(massage[1]) >= 3:
test_stat_norm_B, pvalue_norm_B = stats.shapiro(np.array(massage[1]))
if pvalue_norm_B < 0.05:
pval_store_norm_B[chem] = 'No'
else:
pval_store_norm_B[chem] = 'Yes'
else:
pval_store_norm_B[chem] = 'not enough data'
var_stat_A = stats.variation(np.array(massage[0]))
var_stat_B = stats.variation(np.array(massage[1]))
stat_lev_bart, pvalue_lev_bart = stats.levene(*massage, center='mean')
pval_store_st_t[chem] = pvalue_st_t
pval_store_st_t_val[chem] = pvalue_st_t
var_store_A[chem] = var_stat_A
var_store_B[chem] = var_stat_B
pval_store_lev_bart[chem] = pvalue_lev_bart
if pvalue_st_t < 0.05:
pval_store_st_t[chem] = 'diff'
else:
pval_store_st_t[chem] = 'same'
if v > 0.05:
pval_store_st_t_dec[chem] = 'same'
if v < 0.05 and perc_diff > 0.0:
pval_store_st_t_dec[chem] = 'diff'
counter_sig_st_t += 1
else:
counter_nosig += 1
decisions.append(pval_store_st_t)
decisions.append(pval_store_st_t_dec)
decisions.append(pval_store_st_t_val)
decisions.append(pval_store_st_t_val_adj)
decisions.append(pval_store_norm_A)
decisions.append(pval_store_norm_B)
decisions.append(var_store_A)
decisions.append(var_store_B)
decisions.append(pval_store_lev_bart)
decisions = pd.DataFrame(decisions)
decisions = decisions.T
decisions.columns = [
f'Welchs t-test result for lot {self.reference_query} v lot {self.sample_query}',
'Welchs t-test considering Multi-test Correction',
'Welchs t-test pvalue unadjusted',
'Welchs t-test pvalue adjusted',
f'are lot {self.reference_query} chemicals normally distributed?',
f'are lot {self.sample_query} chemicals normally distributed?',
f'lot {self.reference_query} variance',
f'lot {self.sample_query} variance',
'levenes statistic']
decisions_before_filter = decisions.copy()
decisions_before_filter.reset_index(inplace=True)
decisions_before_filter.rename(columns={'index': 'chemicals'}, inplace=True)
decisions = decisions.loc[(decisions['Welchs t-test pvalue adjusted'] < self.alpha)]
decisions.reset_index(inplace=True)
decisions.rename(columns={'index': 'chemicals'}, inplace=True)
decisions.sort_values(by=['Welchs t-test pvalue adjusted'], inplace=True)
decisions_before_filter = decisions_before_filter.filter(items=['chemicals', 'Welchs t-test pvalue adjusted'])
decisions_before_filter = decisions_before_filter.sort_values(by=['Welchs t-test pvalue adjusted'])
results_fishers = self.fishers_method(decisions_before_filter['Welchs t-test pvalue adjusted'].tolist())
results_fishers_df = pd.DataFrame(results_fishers,
index=['decision',
'combined z-score',
'combined p-value',
'meanFDR'])
results_fishers_df = results_fishers_df.T
results_fishers_df['k'] = len(decisions_before_filter.dropna())
results_fishers_df.insert(loc=0, column='comparison', value=(str(self.reference_query + '-' + 'vs'
+ '-' + self.sample_query)))
return decisions, results_fishers_df
See all the information above
The error is telling you that filtered_df is NOT a pandas.DataFrame.
It's a tuple. In fact your TtestStrategy's transform method returns the tuple:
return decisions, results_fishers_df
If you change your code to:
def execute(self, output_from_previous: Any = None, **kwargs) -> Any:
_ignored, self.filtered_df = self.strategy.transform(key=self.key_input,
df=self.df_to_filter)
self.state.query_reports[self.key_output] = self.filtered_df
super().execute(output_from_previous=output_from_previous, **kwargs)
then you shouldn't see that AttributeError anymore.
Here's the primary codeblock.
variables = ['V1','V2','V3',...]
caseList = ['C1','C2','C3',...]
exp = {}
conv = {}
result = {}
for case in caseList:
if case != "Processed":
load_case(case, dirPath, fileTyp)
import_rawData(fileTyp)
convert_units()
display('Hello')
merge_Data(case)
display('Hello')
Here's the output of that codeblock. Nothing executes after the merge_Data() function call.
'Case loaded'
'Raw data imported'
'Units converted'
'Hello'
'Data filtered'
'Data merged'
Here is the code for the merge_Data, and associated filter_expData functions:
def merge_Data(case):
fexpData = filter_expData(case)
for var in variables:
exists = fexpData.get(var,"")
if len(exists) > 0:
temp = conv[var]
temp[var,temp[var].columns[1].replace('Simulation','Experiment')] = pd.Series(fexpData[var].tolist())
temp[var,'Absolute Error'] = abs(temp[var,temp[var].columns[1]] - temp[var,temp[var].columns[2]])
temp[var,'Percentage Error'] = temp[var,temp[var].columns[3]] / temp[var,temp[var].columns[2]] * 100
result[var] = temp
else:
result[var] = conv[var]
display('Data merged')
def filter_expData(case):
var = next(iter(conv))
timeList = pd.Series(conv[var].iloc[:,conv[var].columns.get_level_values(1)=="Time (min)"].iloc[:,0].round(3))
filtExp = exp[case][exp[case]['Time (min)'].round(3).isin(timeList)]
display('Data filtered')
return filtExp
Can you help me understand why code isn't executing after the merge_Data() function call?
My current GUI is to show target tracking from a TI processor to PC via UART. Sometimes, GUI crashes due to UART performance. But everytime I want to reset GUI, I also have to reset my TI device. So I want to modify python code so as to only re-open GUI, enter COM port without having to reset device whenever GUI crashes.
Currently, each time GUI crashes, even though I re-opened GUI but didn't reset device (power off & on), I can not continue tracking.
I put python code for GUI below.In this GUI python program, we have 2 blanks & button to enter UART COM port, 2 buttons to select & send configuration to device. Then it can begin tracking.
I'm considering to modify the part of sendCfg(self), but still not sure how to make it work as my purpose due to my lack of experience in Python & GUI design. Please help me with sample code for this task.
class Window(QDialog):
def __init__(self, parent=None, size=[]):
super(Window, self).__init__(parent)
#when running Gree Demo
self.Gree = 1
# set window toolbar options, and title. #deb_gp
self.setWindowFlags(
Qt.Window |
Qt.CustomizeWindowHint |
Qt.WindowTitleHint |
Qt.WindowMinimizeButtonHint |
Qt.WindowMaximizeButtonHint |
Qt.WindowCloseButtonHint
)
self.setWindowTitle("mmWave People Counting")
print('Python is ', struct.calcsize("P")*8, ' bit')
print('Python version: ', sys.version_info)
gridlay = QGridLayout()
gridlay.addWidget(self.comBox, 0,0,1,1)
gridlay.addWidget(self.statBox, 1,0,1,1)
gridlay.addWidget(self.configBox,2,0,1,1)
gridlay.addWidget(self.plotControlBox,3,0,1,1)
gridlay.addWidget(self.boxTab,4,0,1,1)
gridlay.addWidget(self.spBox,5,0,1,1)
gridlay.addWidget(self.graphTabs,0,1,6,1)
gridlay.addWidget(self.gw, 0, 2, 6, 1)
#gridlay.addWidget(self.demoData, 0,3,1,2)
#gridlay.addWidget(self.hPlot,1,3,4,2)
gridlay.setColumnStretch(0,1)
gridlay.setColumnStretch(1,3)
self.setLayout(gridlay)
#
# left side pane layout
#
def setConnectionLayout(self):
self.comBox = QGroupBox('Connect to Com Ports')
self.uartCom = QLineEdit('') #deb_gp
self.dataCom = QLineEdit('') #deb_gp
self.uartLabel = QLabel('UART COM:')
self.dataLabel = QLabel('DATA COM:')
self.connectStatus = QLabel('Not Connected')
self.connectButton = QPushButton('Connect')
self.connectButton.clicked.connect(self.connectCom)
self.configLabel = QLabel('Config Type:')
self.configType = QComboBox()
self.configType.addItems(["3D People Counting", "SDK Out of Box Demo", "Long Range People Detection", "Indoor False Detection Mitigation", "(Legacy) 2D People Counting", "(Legacy): Overhead People Counting"])
self.comLayout = QGridLayout()
self.comLayout.addWidget(self.uartLabel,0,0)
self.comLayout.addWidget(self.uartCom,0,1)
self.comLayout.addWidget(self.dataLabel,1,0)
self.comLayout.addWidget(self.dataCom,1,1)
self.comLayout.addWidget(self.configLabel,2,0)
self.comLayout.addWidget(self.configType,2,1)
self.comLayout.addWidget(self.connectButton,3,0)
self.comLayout.addWidget(self.connectStatus,3,1)
self.comBox.setLayout(self.comLayout)
def setConfigLayout(self):
self.configBox = QGroupBox('Configuration')
self.selectConfig = QPushButton('Select Configuration')
self.sendConfig = QPushButton('Send Configuration')
self.selectConfig.clicked.connect(self.selectCfg)
self.sendConfig.clicked.connect(self.sendCfg)
self.configTable = QTableWidget(5,2)
#set parameter names
self.configTable.setItem(0,0,QTableWidgetItem('Radar Parameter'))
self.configTable.setItem(0,1,QTableWidgetItem('Value'))
self.configTable.setItem(1,0,QTableWidgetItem('Max Range'))
self.configTable.setItem(2,0,QTableWidgetItem('Range Resolution'))
self.configTable.setItem(3,0,QTableWidgetItem('Max Velocity'))
self.configTable.setItem(4,0,QTableWidgetItem('Velcoity Resolution'))
self.configLayout = QVBoxLayout()
self.configLayout.addWidget(self.selectConfig)
self.configLayout.addWidget(self.sendConfig)
self.configLayout.addWidget(self.configTable)
#self.configLayout.addStretch(1)
self.configBox.setLayout(self.configLayout)
def updateGraph(self, parsedData):
updateStart = int(round(time.time()*1000))
self.useFilter = 0
classifierOutput = []
pointCloud = parsedData[0]
targets = parsedData[1]
indexes = parsedData[2]
numPoints = parsedData[3]
numTargets = parsedData[4]
self.frameNum = parsedData[5]
fail = parsedData[6]
classifierOutput = parsedData[7]
fallDetEn = 0
indicesIn = []
if (fail != 1):
#left side
pointstr = 'Points: '+str(numPoints)
targetstr = 'Targets: '+str(numTargets)
self.numPointsDisplay.setText(pointstr)
self.numTargetsDisplay.setText(targetstr)
#right side fall detection
peopleStr = 'Number of Detected People: '+str(numTargets)
if (numTargets == 0):
fdestr = 'Fall Detection Disabled - No People Detected'
elif (numTargets == 1):
fdestr = 'Fall Detection Enabled'
fallDetEn = 1
elif (numTargets > 1):
fdestr = 'Fall Detected Disabled - Too Many People'
#self.numDetPeople.setText(peopleStr)
#self.fallDetEnabled.setText(fdestr)
if (len(targets) < 13):
targets = []
classifierOutput = []
if (fail):
return
#remove static points
if (self.configType.currentText() == '3D People Counting'):
if (not self.staticclutter.isChecked()):
statics = np.where(pointCloud[3,:] == 0)
try:
firstZ = statics[0][0]
numPoints = firstZ
pointCloud = pointCloud[:,:firstZ]
indexes = indexes[:,:self.previousFirstZ]
self.previousFirstZ = firstZ
except:
firstZ = -1
#point cloud persistence
fNum = self.frameNum%10
if (numPoints):
self.previousCloud[:5,:numPoints,fNum] = pointCloud[:5,:numPoints]
self.previousCloud[5,:len(indexes),fNum] = indexes
self.previousPointCount[fNum]=numPoints
#plotting 3D - get correct point cloud (persistent points and synchronize the frame)
if (self.configType.currentText() == 'SDK3xPeopleCount'):
pointIn = pointCloud
else:
totalPoints = 0
persistentFrames = int(self.persistentFramesInput.currentText())
#allocate new array for all the points
for i in range(1,persistentFrames+1):
totalPoints += self.previousPointCount[fNum-i]
pointIn = np.zeros((5,int(totalPoints)))
indicesIn = np.ones((1, int(totalPoints)))*255
totalPoints = 0
#fill array
for i in range(1,persistentFrames+1):
prevCount = int(self.previousPointCount[fNum-i])
pointIn[:,totalPoints:totalPoints+prevCount] = self.previousCloud[:5,:prevCount,fNum-i]
if (numTargets > 0):
indicesIn[0,totalPoints:totalPoints+prevCount] = self.previousCloud[5,:prevCount,fNum-i]
totalPoints+=prevCount
if (self.graphFin):
self.plotstart = int(round(time.time()*1000))
self.graphFin = 0
if (self.threeD):
try:
indicesIn = indicesIn[0,:]
except:
indicesIn = []
self.get_thread = updateQTTargetThread3D(pointIn, targets, indicesIn, self.scatter, self.pcplot, numTargets, self.ellipsoids, self.coordStr, classifierOutput, self.zRange, self.gw, self.plotByIndex.isChecked(), self.plotTracks.isChecked(), self.bbox,self.boundaryBoxes[0]['checkEnable'].isChecked())
self.get_thread.done.connect(self.graphDone)
self.get_thread.start(priority=QThread.HighestPriority-1)
else:
npc = pointIn[0:2,:]
print (np.shape(npc))
self.legacyThread = update2DQTGraphThread(npc, targets, numTargets, indexes, numPoints, self.trailData, self.activeTrail, self.trails, self.scatter2D, self.gatingScatter)
self.legacyThread.done.connect(self.graphDone)
self.legacyThread.start(priority=QThread.HighestPriority-1)
else:
return
#pointIn = self.previousCloud[:,:int(self.previousPointCount[fNum-1]),fNum-1]
#state tracking
if (numTargets > 0):
self.lastFrameHadTargets = True
else:
self.lastFrameHadTargets = False
if (numTargets):
self.lastTID = targets[0,:]
else:
self.lastTID = []
def graphDone(self):
plotend = int(round(time.time()*1000))
plotime = plotend - self.plotstart
try:
if (self.frameNum > 1):
self.averagePlot = (plotime*1/self.frameNum) + (self.averagePlot*(self.frameNum-1)/(self.frameNum))
else:
self.averagePlot = plotime
except:
self.averagePlot = plotime
self.graphFin = 1
pltstr = 'Average Plot time: '+str(plotime)[:5] + ' ms'
fnstr = 'Frame: '+str(self.frameNum)
self.frameNumDisplay.setText(fnstr)
self.plotTimeDisplay.setText(pltstr)
def resetFallText(self):
self.fallAlert.setText('Standing')
self.fallPic.setPixmap(self.standingPicture)
self.fallResetTimerOn = 0
def updateFallThresh(self):
try:
newThresh = float(self.fallThreshInput.text())
self.fallThresh = newThresh
self.fallThreshMarker.setPos(self.fallThresh)
except:
print('No numberical threshold')
def connectCom(self):
#get parser
self.parser = uartParserSDK(type=self.configType.currentText())
self.parser.frameTime = self.frameTime
print('Parser type: ',self.configType.currentText())
#init threads and timers
self.uart_thread = parseUartThread(self.parser)
if (self.configType.currentText() != 'Replay'):
self.uart_thread.fin.connect(self.parseData)
self.uart_thread.fin.connect(self.updateGraph)
self.parseTimer = QTimer()
self.parseTimer.setSingleShot(False)
self.parseTimer.timeout.connect(self.parseData)
try:
uart = "COM"+ self.uartCom.text() #deb_gp
data = "COM"+ self.dataCom.text() #deb_gp
#TODO: find the serial ports automatically.
self.parser.connectComPorts(uart, data)
self.connectStatus.setText('Connected') #deb_gp
self.connectButton.setText('Disconnect') #deb_gp
#TODO: create the disconnect button action
except:
self.connectStatus.setText('Unable to Connect')
if (self.configType.currentText() == "Replay"):
self.connectStatus.setText('Replay')
if (self.configType.currentText() == "Long Range People Detection"):
self.frameTime = 400
#
# Select and parse the configuration file
# TODO select the cfgfile automatically based on the profile.
def selectCfg(self):
try:
self.parseCfg(self.selectFile())
except:
print('No cfg file selected!')
def selectFile(self):
fd = QFileDialog()
filt = "cfg(*.cfg)"
filename = fd.getOpenFileName(directory='./../chirp_configs',filter=filt) #deb_gp - added folder name
return filename[0]
def parseCfg(self, fname):
cfg_file = open(fname, 'r')
self.cfg = cfg_file.readlines()
counter = 0
chirpCount = 0
for line in self.cfg:
args = line.split()
if (len(args) > 0):
if (args[0] == 'cfarCfg'):
zy = 4
#self.cfarConfig = {args[10], args[11], '1'}
elif (args[0] == 'AllocationParam'):
zy=3
#self.allocConfig = tuple(args[1:6])
elif (args[0] == 'GatingParam'):
zy=2
#self.gatingConfig = tuple(args[1:4])
elif (args[0] == 'SceneryParam' or args[0] == 'boundaryBox'):
self.boundaryLine = counter
self.profile['leftX'] = float(args[1])
self.profile['rightX'] = float(args[2])
self.profile['nearY'] = float(args[3])
self.profile['farY'] = float(args[4])
self.setBoundaryTextVals(self.profile)
self.boundaryBoxes[0]['checkEnable'].setChecked(True)
elif (args[0] == 'staticBoundaryBox'):
self.staticLine = counter
elif (args[0] == 'profileCfg'):
self.profile['startFreq'] = float(args[2])
self.profile['idle'] = float(args[3])
self.profile['adcStart'] = float(args[4])
self.profile['rampEnd'] = float(args[5])
self.profile['slope'] = float(args[8])
self.profile['samples'] = float(args[10])
self.profile['sampleRate'] = float(args[11])
print(self.profile)
elif (args[0] == 'frameCfg'):
self.profile['numLoops'] = float(args[3])
self.profile['numTx'] = float(args[2])+1
elif (args[0] == 'chirpCfg'):
chirpCount += 1
elif (args[0] == 'sensorPosition'):
self.profile['sensorHeight'] = float(args[1])
self.profile['az_tilt'] = float(args[2])
self.profile['elev_tilt'] = float(args[3])
counter += 1
maxRange = self.profile['sampleRate']*1e3*0.9*3e8/(2*self.profile['slope']*1e12)
#update boundary box
self.drawBoundaryGrid(maxRange)
#update chirp table values
bw = self.profile['samples']/(self.profile['sampleRate']*1e3)*self.profile['slope']*1e12
rangeRes = 3e8/(2*bw)
Tc = (self.profile['idle']*1e-6 + self.profile['rampEnd']*1e-6)*chirpCount
lda = 3e8/(self.profile['startFreq']*1e9)
maxVelocity = lda/(4*Tc)
velocityRes = lda/(2*Tc*self.profile['numLoops']*self.profile['numTx'])
self.configTable.setItem(1,1,QTableWidgetItem(str(maxRange)[:5]))
self.configTable.setItem(2,1,QTableWidgetItem(str(rangeRes)[:5]))
self.configTable.setItem(3,1,QTableWidgetItem(str(maxVelocity)[:5]))
self.configTable.setItem(4,1,QTableWidgetItem(str(velocityRes)[:5]))
#update sensor position
self.az_tilt.setText(str(self.profile['az_tilt']))
self.elev_tilt.setText(str(self.profile['elev_tilt']))
self.s_height.setText(str(self.profile['sensorHeight']))
def sendCfg(self):
try:
if (self.configType.currentText() != "Replay"):
self.parser.sendCfg(self.cfg)
self.configSent = 1
self.parseTimer.start(self.frameTime)
except:
print ('No cfg file selected!')
# Needed ?? deb_gp
# def setParser(self, uParser):
# self.parser = uParser
def parseData(self):
self.uart_thread.start(priority=QThread.HighestPriority)
def whoVisible(self):
if (self.threeD):
self.threeD = 0
else:
self.threeD = 1
print('3d: ', self.threeD)
if __name__ == '__main__':
if (compileGui):
appctxt = ApplicationContext()
app = QApplication(sys.argv)
screen = app.primaryScreen()
size = screen.size()
main = Window(size=size)
main.show()
exit_code = appctxt.app.exec_()
sys.exit(exit_code)
else:
app = QApplication(sys.argv)
screen = app.primaryScreen()
size = screen.size()
main = Window(size=size)
main.show()
sys.exit(app.exec_())
I have a script in Maya (python) that renders a series of frames to the V-ray frame buffer, one for each render layer. It is working well, but when switching render layers and launching the next render, Maya grabs focus, interrupting whatever task the artist had moved on to while the script was running. I would like this to be a background task. Maya is running on Windows.
import maya
import maya.cmds as cmds
import re
from functools import partial
import sys
from datetime import datetime
global cancel
cancel = 0
print 'batch_review'
def no_cam_window_popup(no_cam_set_list):
#print no_cam_set_list
cmds.window(title = 'WARNING: NO CAMERAS LINKED TO LAYERS', width = 300, height = 75, sizeable = False)
cmds.columnLayout("mainColumn", adjustableColumn = True)
cmds.rowLayout("nameRowLayout01", numberOfColumns = 15, parent = "mainColumn")
cmds.text(label = ("no cam set for layers:"))
for layer in no_cam_set_list:
cmds.text(label = ('' + layer + ', '),font = 'boldLabelFont')
cmds.showWindow()
def renderThumbs(checkBoxLow,checkBoxMid,checkBoxHigh,checkBoxRenderRegion,intField_res,floatField_thrhld,*args):
global cancel
cams = cmds.ls(type = "camera")
cancel = 0
popup_win = 0
no_cam_set_list = []
cmds.loadPlugin('vrayformaya', quiet=True)
cmds.pluginInfo('vrayformaya', edit=True, autoload=True)
cmds.setAttr("defaultRenderGlobals.ren", "vray", type = "string")
curLay = cmds.editRenderLayerGlobals( currentRenderLayer = True, query = True )
changeLay = curLay
rls = cmds.ls(type = "renderLayer")
renCams = cmds.ls(type = "camera")
renCam = "persp"
lowBut = cmds.checkBox(checkBoxLow,value = True, query = True)
midBut = cmds.checkBox(checkBoxMid,value = True, query = True)
highBut = cmds.checkBox(checkBoxHigh,value = True, query = True)
globopt_cache_geom_plugins = cmds.getAttr('vraySettings.globopt_cache_geom_plugins')
#print 'globopt_cache_geom_plugins = ',globopt_cache_geom_plugins
print " "
print "-- batch_review --"
res = cmds.intField(intField_res, v = True, query = True)
thr = cmds.floatField(floatField_thrhld, v = True,query = True)
if lowBut == 1:
cmds.setAttr("vraySettings.dmcThreshold",thr)
cmds.setAttr("vraySettings.width", res)
cmds.setAttr("vraySettings.height", res)
cmds.setAttr("vraySettings.globopt_cache_geom_plugins",1)
cmds.setAttr("vraySettings.globopt_ray_maxIntens_on",1)
#cmds.setAttr('vraySettings.globopt_cache_geom_plugins',globopt_cache_geom_plugins)
print " "
print "---"
print "quality = %s, dmcThreshold = %s"%(res,thr)
if midBut == 1:
cmds.setAttr("vraySettings.dmcThreshold",thr)
cmds.setAttr("vraySettings.width", res)
cmds.setAttr("vraySettings.height", res)
cmds.setAttr("vraySettings.globopt_cache_geom_plugins",1)
cmds.setAttr("vraySettings.globopt_ray_maxIntens_on",1)
#cmds.setAttr('vraySettings.globopt_cache_geom_plugins',globopt_cache_geom_plugins)
print " "
print "---"
print "quality = %s, dmcThreshold = %s"%(res,thr)
if highBut == 1:
cmds.setAttr("vraySettings.dmcThreshold",thr)
cmds.setAttr("vraySettings.width", res)
cmds.setAttr("vraySettings.height", res)
cmds.setAttr("vraySettings.globopt_cache_geom_plugins",1)
cmds.setAttr("vraySettings.globopt_ray_maxIntens_on",1)
#cmds.setAttr('vraySettings.globopt_cache_geom_plugins',globopt_cache_geom_plugins)
print " "
print "---"
print "quality = %s, dmcThreshold = %s"%(res,thr)
print "--- "
print " "
for rl in rls:
found_cam = 0
if rl != "defaultRenderLayer" and cancel == 0:
rlState = cmds.getAttr(rl + ".renderable")
if rlState == 1:
print ' '
print "rende layer = ",rl
cmds.editRenderLayerGlobals( currentRenderLayer = rl )
for cam in cams:
camState = cmds.getAttr(cam + ".renderable")
if camState == 1:
rrState = cmds.checkBox(checkBoxRenderRegion,value = True,query = True)
reg = cmds.vray("vfbControl","-getregion")
if rrState == 0:
cmds.vray("vfbControl","-setregion","reset")
if rrState == 1:
cmds.vray("vfbControl","-setregionenabled",1)
cmds.vray("vfbControl","-setregion",reg[0],reg[1],reg[2],reg[3])
mayaString = "renderWindowRenderCamera render renderView " + cam
print 'using ' + cam + ' for ' + rl
maya.mel.eval(mayaString)
cmds.vray("vfbControl", "-historysave")
cmds.vray("vfbControl", "-historyselect",0)
dte = datetime.now().strftime('%H:%M:%S')
editStr = dte + " ,render layer: " + rl + " , " + "cam: " + cam
cmds.vray("vfbControl", "-historycomment", editStr)
print " "
found_cam = 1
if found_cam == 0:
print 'no camera link found, using persp cam for ',rl
cam = 'persp'
rrState = cmds.checkBox(checkBoxRenderRegion,value = True,query = True)
reg = cmds.vray("vfbControl","-getregion")
if rrState == 0:
cmds.vray("vfbControl","-setregion","reset")
if rrState == 1:
cmds.vray("vfbControl","-setregionenabled",1)
cmds.vray("vfbControl","-setregion",reg[0],reg[1],reg[2],reg[3])
mayaString = "renderWindowRenderCamera render renderView " + cam
maya.mel.eval(mayaString)
cmds.vray("vfbControl", "-historysave")
cmds.vray("vfbControl", "-historyselect",0)
dte = datetime.now().strftime('%H:%M:%S')
editStr = dte + " ,render layer: " + rl + " , " + "cam: " + cam
cmds.vray("vfbControl", "-historycomment", editStr)
popup_win = 1
no_cam_set_list.append(rl)
#if popup_win == 1:
#no_cam_window_popup(no_cam_set_list)
def checkBoxCheckLow(checkBoxLow,checkBoxMid,checkBoxHigh,intField_res,floatField_thrhld,*args):
global cancel
lowButVal = cmds.checkBox(checkBoxLow,value = True, query = True)
midButVal = cmds.checkBox(checkBoxMid,value = True, query = True)
highButVal = cmds.checkBox(checkBoxHigh,value = True, query = True)
cmds.checkBox(checkBoxMid,value = False, edit = True)
cmds.checkBox(checkBoxHigh,value = False, edit = True)
cmds.intField(intField_res, v = 800,edit = True)
cmds.floatField(floatField_thrhld, v = .1,edit = True )
cancel = 0
def checkBoxCheckMid(checkBoxLow,checkBoxMid,checkBoxHigh,intField_res,floatField_thrhld,*args):
global cancel
lowButVal = cmds.checkBox(checkBoxLow,value = True, query = True)
midButVal = cmds.checkBox(checkBoxMid,value = True, query = True)
highButVal = cmds.checkBox(checkBoxHigh,value = True, query = True)
cmds.checkBox(checkBoxLow,value = False, edit = True)
cmds.checkBox(checkBoxHigh,value = False, edit = True)
cmds.intField(intField_res, v = 1000,edit = True)
cmds.floatField(floatField_thrhld, v = .5,edit = True )
cancel = 0
def checkBoxCheckHigh(checkBoxLow,checkBoxMid,checkBoxHigh,intField_res,floatField_thrhld,*args):
global cancel
lowButVal = cmds.checkBox(checkBoxLow,value = True, query = True)
midButVal = cmds.checkBox(checkBoxMid,value = True, query = True)
highButVal = cmds.checkBox(checkBoxHigh,value = True, query = True)
cmds.checkBox(checkBoxLow,value = False, edit = True)
cmds.checkBox(checkBoxMid,value = False, edit = True)
cmds.intField(intField_res, v = 2000,edit = True)
cmds.floatField(floatField_thrhld, v = .008,edit = True )
cancel = 0
def checkBoxAOVchange(checkBoxAOV,*args):
checkBoxAOVval = cmds.checkBox(checkBoxAOV,value = True,query = True)
if checkBoxAOVval == 1:
cmds.setAttr("vraySettings.relements_enableall", 1)
if checkBoxAOVval == 0:
cmds.setAttr("vraySettings.relements_enableall", 0)
def rrCheckbox(checkBoxRenderRegion,reg,*args):
global gReg
zeroes = ['0','0','0','0']
tRes = cmds.vray("vfbControl","-getregion")
if tRes != zeroes:
gReg = tRes
rrstate = cmds.checkBox(checkBoxRenderRegion,value = True,query = True)
if rrstate == 0:
if gReg != zeroes:
cmds.vray("vfbControl","-setregion",gReg[0],gReg[1],gReg[2],gReg[3])
else:
cmds.vray("vfbControl","-setregion",reg[0],reg[1],reg[2],reg[3])
cmds.vray("vfbControl","-setregion","reset")
if rrstate == 1:
cmds.vray("vfbControl","-setregionenabled",1)
if gReg != zeroes:
cmds.vray("vfbControl","-setregion",gReg[0],gReg[1],gReg[2],gReg[3])
else:
cmds.vray("vfbControl","-setregion",reg[0],reg[1],reg[2],reg[3])
return(reg)
def cancelOPs(*args):
global cancel
cancel = 1
raise Exception("quitting renders")
def set_threshhold(floatField_thrhld,*args):
threshhold_value = cmds.floatField(floatField_thrhld,value = True,query = True)
cmds.setAttr('vraySettings.dmcThreshold',threshhold_value)
def set_resolution(intField_res,*args):
intField_res_value = cmds.intField(intField_res,value = True,query = True)
cmds.setAttr('vraySettings.width',intField_res_value)
cmds.setAttr('vraySettings.height',intField_res_value)
def renthumbsWin():
name = "Batch_Review"
global gReg
gReg = ('0','0','0','0')
zeroes = ('0','0','0','0')
windowSize = (200,100)
if (cmds.window(name, exists = True)):
cmds.deleteUI(name)
window = cmds.window(name, title = name, width = 350, height = 50,bgc = (.2,.2,.2), s = False)
cmds.columnLayout("mainColumn", adjustableColumn = True)
cmds.rowLayout("nameRowLayout01", numberOfColumns = 15, parent = "mainColumn")
cmds.text(label = "preset quality: ")
checkBoxLow = cmds.checkBox(label = "low", value = False)
checkBoxMid = cmds.checkBox(label = "mid", value = True,)
checkBoxHigh = cmds.checkBox(label = "high", value = False)
cmds.text(label = " ")
cmds.text(label = "resolution: ")
intField_res = cmds.intField(v = 1000,width = 45)
#print 'intField_res = ',intField_res
cmds.intField(intField_res, changeCommand = partial(set_resolution,intField_res),edit = True)
cmds.text(label = " ")
cmds.text(label = "threshold: ")
floatField_thrhld = cmds.floatField(v = .5,width = 45)
cmds.floatField(floatField_thrhld, changeCommand = partial(set_threshhold,floatField_thrhld),edit = True)
cmds.checkBox(checkBoxLow, changeCommand = partial(checkBoxCheckLow,checkBoxLow,checkBoxMid,checkBoxHigh,intField_res,floatField_thrhld), edit = True)
cmds.checkBox(checkBoxMid, changeCommand = partial(checkBoxCheckMid,checkBoxLow,checkBoxMid,checkBoxHigh,intField_res,floatField_thrhld),edit = True)
cmds.checkBox(checkBoxHigh, changeCommand = partial(checkBoxCheckHigh,checkBoxLow,checkBoxMid,checkBoxHigh,intField_res,floatField_thrhld),edit = True)
cmds.rowLayout("nameRowLayout02", numberOfColumns = 10, parent = "mainColumn")
cmds.text(label = " ")
cmds.rowLayout("nameRowLayout03", numberOfColumns = 5, parent = "mainColumn")
renderButton = cmds.button(label = "render",width = 100, bgc = (.6,.8,1) )
cmds.text(label = " ")
cmds.button(label = "cancel renders", command = partial(cancelOPs),bgc = (1,.3,.3) )
cmds.rowLayout("nameRowLayout04", numberOfColumns = 10, parent = "mainColumn")
cmds.text(label = " ")
cmds.rowLayout("nameRowLayout05", numberOfColumns = 10, parent = "mainColumn")
checkBoxRenderRegion = cmds.checkBox(label = "use render region", value = False)
cmds.text(label = " ")
cmds.text(label = " ")
reg = cmds.vray("vfbControl","-getregion")
if reg != gReg and reg != zeroes:
gReg = reg
cmds.vray("vfbControl","-setregion","reset")
cmds.checkBox(checkBoxRenderRegion,changeCommand = partial(rrCheckbox,checkBoxRenderRegion,reg),edit = True)
cmds.rowLayout("nameRowLayout06", numberOfColumns = 10, parent = "mainColumn")
AOVstate = cmds.getAttr("vraySettings.relements_enableall")
checkBoxAOV = cmds.checkBox(label = "elements", value = AOVstate)
cmds.checkBox(checkBoxAOV,changeCommand = partial(checkBoxAOVchange,checkBoxAOV),edit = True)
cmds.button(renderButton,command = partial(renderThumbs,checkBoxLow,checkBoxMid,checkBoxHigh,checkBoxRenderRegion,intField_res,floatField_thrhld),edit = True)
cmds.showWindow()
def main():
renthumbsWin()
main()
i first use PyQT4 .
i'm create a QTableWidget to show runing message...
when my program run, it ill crash Within ten minutes.
i try diable my TableUpdate function , and it's don't crash again.
there is my code please help me
class table_work(QThread):
TableDataSignal = pyqtSignal()
def __init__(self,main_self):
# QThread.__init__(self)
super(table_work, self).__init__(main_self)
self.main_self = main_self
self.table_update_list = list()
#pyqtSlot(dict)
def update_table_thread_o(self,work):
try:
row_pos = work['row_position']
data = work['data']
table_key_sort = work['key_sort']
this_table = work['table']
k = 0
for table_key in table_key_sort:
this_table.setItem(row_pos, k, QTableWidgetItem(unicode(data[table_key])))
k += 1
del work
except:
pass
def update_table_thread(self):
main_self = self.main_self
table_work_list = self.table_update_list
while 1:
for work in self.table_update_list:
row_pos = work['row_position']
data = work['data']
table_key_sort = work['key_sort']
this_table = work['table']
k = 0
for table_key in table_key_sort:
this_table.setItem(row_pos, k, QTableWidgetItem(unicode(data[table_key])))
k += 1
time.sleep(0.5)
def run(self):
self.update_table_thread()
this's update table message
def update_table(self,address,change_obj=None,tabe_name='auto_card'):
sample_dict = dict()
table_key_sort = list()
now_table_sort = 0
if tabe_name == "auto_bot":
this_table = self.auto_bot_procc_table
table_data_list = self.auto_bot_procc_table_list
now_table_sort = self.auto_bot_now_table_sort
sample_dict['address'] = address
sample_dict['money'] = 0
sample_dict['run_time'] = 0
sample_dict['item_cd'] = u"60分鐘後"
sample_dict['stat'] = "Ready..."
sample_dict['sort'] = now_table_sort
table_key_sort.append('address')
table_key_sort.append('money')
table_key_sort.append('run_time')
table_key_sort.append('item_cd')
table_key_sort.append('stat')
if tabe_name == "auto_card":
this_table = self.process_table
table_data_list = self.now_procc_table_list
now_table_sort = self.now_table_sort
sample_dict['address'] = address
sample_dict['done_num'] = 0
sample_dict['pre_item'] = ""
sample_dict['procc'] = "Ready"
sample_dict['mission_procc'] = u"待命.."
sample_dict['mission_num'] = 0
sample_dict['mission_line'] = 0
sample_dict['update_time'] = db.get_time()
sample_dict['sort'] = now_table_sort
sample_dict['option'] = ""
table_key_sort.append('address')
table_key_sort.append('done_num')
table_key_sort.append('pre_item')
table_key_sort.append('mission_procc')
table_key_sort.append('procc')
table_key_sort.append('mission_num')
table_key_sort.append('mission_line')
table_key_sort.append('update_time')
if address not in table_data_list:
this_table.insertRow(sample_dict['sort'])
table_data_list[address] = sample_dict
sample_dict['sort'] = self.auto_bot_now_table_sort
self.auto_bot_now_table_sort += 1
acc_data = table_data_list[address]
if change_obj != None:
key = change_obj['key']
val = change_obj['val']
if key in acc_data:
acc_data[key] = val
acc_data['update_time'] = db.get_time()
rowPosition = acc_data['sort']
temp = dict()
temp['row_position'] = rowPosition
temp['data'] = acc_data
temp['key_sort'] = table_key_sort
temp['table'] = this_table
self.TableDataSignal.emit(temp)
del temp
Some time i get a ANS.
i'm a PYQT newbie , After this period of various projects experience.
I understand if you don't use Main Thread to Change UI, Always use sign/emit
even your code is worked,but always use sign/emit, Otherwise there will be a series of disasters.
you just like
class sample(QtCore.QThread):
table_data_change = QtCore.pyqtSignal(dict)
def __init__(self,main_win):
self.main = main_win
self.table_data_change.connect(self.main.change_fn)
def test(self):
data = dict()
data['btn'] = .....
data['val'] = .....
self.table_data_change.emit(data)
Save your time !