📄 ownomogram.py
字号:
self.footer.setCanvas(None)
self.header.setCanvas(None)
self.graph.setCanvas(None)
if self.data and self.cl: # and not type(self.cl) == orngLR_Jakulin.MarginMetaClassifier:
#check domains
for at in self.cl.domain:
if at.getValueFrom and hasattr(at.getValueFrom, "variable"):
if (not at.getValueFrom.variable in self.data.domain) and (not at in self.data.domain):
return
else:
if not at in self.data.domain:
return
if type(self.cl) == orange.BayesClassifier:
# if len(self.cl.domain.classVar.values)>2:
# QMessageBox("OWNomogram:", " Please use only Bayes classifiers that are induced on data with dichotomous class!", QMessageBox.Warning,
# QMessageBox.NoButton, QMessageBox.NoButton, QMessageBox.NoButton, self).show()
# else:
self.nbClassifier(self.cl)
elif type(self.cl) == orngLR_Jakulin.MarginMetaClassifier and self.data:
self.svmClassifier(self.cl)
elif type(self.cl) == orange.LogRegClassifier:
# get if there are any continuous attributes in data -> then we need data to compute margins
cont = False
for at in self.cl.continuizedDomain.attributes:
if not at.getValueFrom:
cont = True
if self.data or not cont:
self.lrClassifier(self.cl)
else:
setNone()
else:
setNone()
if self.sort_type>0:
self.sortNomogram()
def sortNomogram(self):
def sign(x):
if x<0:
return -1;
return 1;
def compare_to_ordering_in_data(x,y):
return self.data.domain.attributes.index(self.data.domain[x.name]) - self.data.domain.attributes.index(self.data.domain[y.name])
def compare_to_ordering_in_domain(x,y):
return self.cl.domain.attributes.index(self.cl.domain[x.name]) - self.cl.domain.attributes.index(self.cl.domain[y.name])
def compate_beta_difference(x,y):
return -sign(x.maxValue-x.minValue-y.maxValue+y.minValue)
def compare_beta_positive(x, y):
return -sign(x.maxValue-y.maxValue)
def compare_beta_negative(x, y):
return sign(x.minValue-y.minValue)
if not self.bnomogram:
return
if self.sort_type == 0 and self.data:
self.bnomogram.attributes.sort(compare_to_ordering_in_data)
elif self.sort_type == 0 and self.cl.domain:
self.bnomogram.attributes.sort(compare_to_ordering_in_domain)
if self.sort_type == 1:
self.bnomogram.attributes.sort(compate_beta_difference)
elif self.sort_type == 2:
self.bnomogram.attributes.sort(compare_beta_positive)
elif self.sort_type == 3:
self.bnomogram.attributes.sort(compare_beta_negative)
# update nomogram
self.showNomogram()
def setProbability(self):
if self.probability and self.bnomogram:
self.bnomogram.showAllMarkers()
elif self.bnomogram:
self.bnomogram.hideAllMarkers()
def setBaseLine(self):
if self.bnomogram:
self.bnomogram.showBaseLine(self.showBaseLine)
def saveToFileCanvas(self):
EMPTY_SPACE = 25 # Empty space between nomogram and summarization scale
sizeW = self.graph.canvas().pright
sizeH = self.graph.canvas().gbottom + self.header.canvas().size().height() + self.footer.canvas().size().height()+EMPTY_SPACE
size = QSize(sizeW, sizeH)
qfileName = QFileDialog.getSaveFileName("graph.png","Portable Network Graphics (.PNG)\nWindows Bitmap (.BMP)\nGraphics Interchange Format (.GIF)", None, "Save to..")
fileName = str(qfileName)
if fileName == "": return
(fil,ext) = os.path.splitext(fileName)
ext = ext.replace(".","")
ext = ext.upper()
#create buffers and painters
headerBuffer = QPixmap(self.header.canvas().size())
graphBuffer = QPixmap(QSize(self.graph.canvas().pright, self.graph.canvas().gbottom+EMPTY_SPACE))
footerBuffer = QPixmap(self.footer.canvas().size())
headerPainter = QPainter(headerBuffer)
graphPainter = QPainter(graphBuffer)
footerPainter = QPainter(footerBuffer)
# fill painters
headerPainter.fillRect(headerBuffer.rect(), QBrush(QColor(255, 255, 255))) # make background same color as the widget's background
graphPainter.fillRect(graphBuffer.rect(), QBrush(QColor(255, 255, 255))) # make background same color as the widget's background
footerPainter.fillRect(footerBuffer.rect(), QBrush(QColor(255, 255, 255))) # make background same color as the widget's background
self.header.drawContents(headerPainter, 0, 0, sizeW, self.header.canvas().size().height())
self.graph.drawContents(graphPainter, 0, 0, sizeW, self.graph.canvas().gbottom+EMPTY_SPACE)
self.footer.drawContents(footerPainter, 0, 0, sizeW, self.footer.canvas().size().height())
buffer = QPixmap(size) # any size can do, now using the window size
painter = QPainter(buffer)
painter.fillRect(buffer.rect(), QBrush(QColor(255, 255, 255))) # make background same color as the widget's background
bitBlt(buffer, 0, 0, headerBuffer, 0, 0, sizeW, self.header.canvas().size().height(), Qt.CopyROP)
bitBlt(buffer, 0, self.header.canvas().size().height(), graphBuffer, 0, 0, sizeW, self.graph.canvas().gbottom+EMPTY_SPACE, Qt.CopyROP)
bitBlt(buffer, 0, self.header.canvas().size().height()+self.graph.canvas().gbottom+EMPTY_SPACE, footerBuffer, 0, 0, sizeW, self.footer.canvas().size().height(), Qt.CopyROP)
painter.end()
headerPainter.end()
graphPainter.end()
footerPainter.end()
buffer.save(fileName, ext)
def saveToFileCanvas_new(self):
EMPTY_SPACE = 25 # Empty space between nomogram and summarization scale
sizeW = self.graph.canvas().pright
sizeH = self.graph.canvas().gbottom + self.header.canvas().size().height() + self.footer.canvas().size().height()+EMPTY_SPACE
size = QSize(sizeW, sizeH)
#qfileName = QFileDialog.getSaveFileName("graph.png","Portable Network Graphics (.PNG)\nWindows Bitmap (.BMP)\nGraphics Interchange Format (.GIF)", None, "Save to..")
#fileName = str(qfileName)
#if fileName == "": return
#(fil,ext) = os.path.splitext(fileName)
#ext = ext.replace(".","")
#ext = ext.upper()
# create buffers and painters
#headerBuffer = QPixmap(self.header.canvas().size())
#graphBuffer = QPixmap(QSize(self.graph.canvas().pright, self.graph.canvas().gbottom+EMPTY_SPACE))
#footerBuffer = QPixmap(self.footer.canvas().size())
#headerPainter = QPainter(headerBuffer)
#graphPainter = QPainter(graphBuffer)
#footerPainter = QPainter(footerBuffer)
# fill painters
#headerPainter.fillRect(headerBuffer.rect(), QBrush(QColor(255, 255, 255))) # make background same color as the widget's background
#graphPainter.fillRect(graphBuffer.rect(), QBrush(QColor(255, 255, 255))) # make background same color as the widget's background
#footerPainter.fillRect(footerBuffer.rect(), QBrush(QColor(255, 255, 255))) # make background same color as the widget's background
#self.header.drawContents(headerPainter, 0, 0, sizeW, self.header.canvas().size().height())
#self.graph.drawContents(graphPainter, 0, 0, sizeW, self.graph.canvas().gbottom+EMPTY_SPACE)
#self.footer.drawContents(footerPainter, 0, 0, sizeW, self.footer.canvas().size().height())
#buffer = QPixmap(size) # any size can do, now using the window size
#painter = QPainter(buffer)
#painter.fillRect(buffer.rect(), QBrush(QColor(255, 255, 255))) # make background same color as the widget's background
#bitBlt(buffer, 0, 0, headerBuffer, 0, 0, sizeW, self.header.canvas().size().height(), Qt.CopyROP)
#bitBlt(buffer, 0, self.header.canvas().size().height(), graphBuffer, 0, 0, sizeW, self.graph.canvas().gbottom+EMPTY_SPACE, Qt.CopyROP)
#bitBlt(buffer, 0, self.header.canvas().size().height()+self.graph.canvas().gbottom+EMPTY_SPACE, footerBuffer, 0, 0, sizeW, self.footer.canvas().size().height(), Qt.CopyROP)
import copy
canvas_glued = QCanvas(self.graph.canvas().pright, self.graph.canvas().gbottom+EMPTY_SPACE+self.header.size().height()+self.footer.canvas().size().height())
# draw header items
items_header = self.header.canvas().allItems()
for item in items_header:
if item.visible():
item.setCanvas(canvas_glued)
# draw graph items
items_graph = self.graph.canvas().allItems()
for item in items_graph:
if item.visible():
item.setCanvas(canvas_glued)
if isinstance(item, QCanvasLine):
item.setPoints(item.startPoint().x(), item.startPoint().y()+self.header.size().height(), item.endPoint().x(), item.endPoint().y()+self.header.size().height())
else:
item.setY(item.y()+self.header.size().height())
# draw graph items
items_footer = self.footer.canvas().allItems()
for item in items_footer:
if item.visible():
item.setCanvas(canvas_glued)
item.setY(item.y()+self.header.size().height()+self.graph.canvas().gbottom+EMPTY_SPACE)
import OWDlgs
try:
import OWDlgs
except:
print "Missing file OWDlgs.py. This file should be in widget directory. Unable to print/save image."
return
sizeDlg = OWDlgs.OWChooseImageSizeDlg(canvas_glued)
sizeDlg.exec_loop()
for item in items_header:
item.setCanvas(self.header.canvas())
for item in items_graph:
item.setCanvas(self.graph.canvas())
item.setY(item.y()-self.header.size().height())
for item in items_footer:
item.setCanvas(self.footer.canvas())
item.setY(item.y()-self.header.size().height()-self.graph.canvas().gbottom-EMPTY_SPACE)
#painter.end()
#headerPainter.end()
#graphPainter.end()
#footerPainter.end()
#buffer.save(fileName, ext)
# Callbacks
def showNomogram(self):
if self.bnomogram and self.cl:
self.bnomogram.show()
# test widget appearance
if __name__=="__main__":
import orngLR, orngSVM
a=QApplication(sys.argv)
ow=OWNomogram()
a.setMainWidget(ow)
data = orange.ExampleTable("titanic.tab")
bayes = orange.BayesLearner(data)
bayes.setattr("data",data)
ow.classifier(bayes)
# here you can test setting some stuff
ow.show()
a.exec_loop()
# save settings
ow.saveSettings()
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -