📄 attributevisualizationpanel.java
字号:
* @param index The index of the attribute */ public void setAttribute(int index) { synchronized (m_locker) { m_threadRun = true; //if(m_hc!=null && m_hc.isAlive()) m_hc.stop(); m_attribIndex = index; m_as = m_data.attributeStats(m_attribIndex); //m_classIndex = m_colorAttrib.getSelectedIndex(); } calcGraph(); } /** * Recalculates the barplot or histogram to display, required usually when the * attribute is changed or the component is resized. */ public void calcGraph() { synchronized (m_locker) { m_threadRun = true; if(m_as.nominalCounts!=null) { m_hc = new BarCalc(); m_hc.setPriority(m_hc.MIN_PRIORITY); m_hc.start(); } else if(m_as.numericStats!=null) { m_hc = new HistCalc(); m_hc.setPriority(m_hc.MIN_PRIORITY); m_hc.start(); } else { m_histBarCounts = null; m_histBarClassCounts = null; this.repaint(); m_threadRun = false; } } } /** * Internal class that calculates the barplot to display, in a separate * thread. In particular it initializes some of the crucial internal fields * required by paintComponent() to display the histogram for the current * attribute. These include: m_histBarCounts or m_histBarClassCounts, * m_maxValue and m_colorList. */ private class BarCalc extends Thread { public void run() { synchronized (m_locker) { if((m_classIndex >= 0) && (m_data.attribute(m_classIndex).isNominal())) { int histClassCounts[][]; histClassCounts=new int[m_data.attribute(m_attribIndex).numValues()] [m_data.attribute(m_classIndex).numValues()+1]; m_maxValue = m_as.nominalCounts[0]; for(int i=0; i<m_data.attribute(m_attribIndex).numValues(); i++) { if(m_as.nominalCounts[i]>m_maxValue) m_maxValue = m_as.nominalCounts[i]; } if(m_colorList.size()==0) m_colorList.addElement(Color.black); for(int i=m_colorList.size(); i < m_data.attribute(m_classIndex).numValues()+1; i++) { Color pc = m_defaultColors[(i-1) % 10]; int ija = (i-1) / 10; ija *= 2; for (int j=0;j<ija;j++) { pc = pc.darker(); } m_colorList.addElement(pc); } for(int k=0; k<m_data.numInstances(); k++) { //System.out.println("attrib: "+ // m_data.instance(k).value(m_attribIndex)+ // " class: "+ // m_data.instance(k).value(m_classIndex)); if(!m_data.instance(k).isMissing(m_attribIndex)) if(m_data.instance(k).isMissing(m_classIndex)) histClassCounts[(int)m_data.instance(k).value(m_attribIndex)] [0]++; else histClassCounts[(int)m_data.instance(k).value(m_attribIndex)] [(int)m_data.instance(k).value(m_classIndex)+1]++; } //for(int i=0; i<histClassCounts.length; i++) { //int sum=0; //for(int j=0; j<histClassCounts[i].length; j++) { // sum = sum+histClassCounts[i][j]; //} //System.out.println("histCount: "+sum+" Actual: "+ // m_as.nominalCounts[i]); //} m_threadRun=false; m_histBarClassCounts = histClassCounts; //Image tmpImg = new BufferedImage(getWidth(), getHeight(), // BufferedImage.TYPE_INT_RGB); //drawGraph( tmpImg.getGraphics() ); //img = tmpImg; AttributeVisualizationPanel.this.repaint(); } else { int histCounts[]; histCounts = new int[m_data.attribute(m_attribIndex).numValues()]; m_maxValue = m_as.nominalCounts[0]; for(int i=0; i<m_data.attribute(m_attribIndex).numValues(); i++) { if(m_as.nominalCounts[i]>m_maxValue) m_maxValue = m_as.nominalCounts[i]; } for(int k=0; k<m_data.numInstances(); k++) { if(!m_data.instance(k).isMissing(m_attribIndex)) histCounts[(int)m_data.instance(k).value(m_attribIndex)]++; } m_threadRun=false; m_histBarCounts = histCounts; //Image tmpImg = new BufferedImage(getWidth(), getHeight(), // BufferedImage.TYPE_INT_RGB); //drawGraph( tmpImg.getGraphics() ); //img = tmpImg; AttributeVisualizationPanel.this.repaint(); } } //end synchronized } //end run() } /** * Internal class that calculates the histogram to display, in a separate * thread. In particular it initializes some of the crucial internal fields * required by paintComponent() to display the histogram for the current * attribute. These include: m_histBarCounts or m_histBarClassCounts, * m_maxValue and m_colorList. */ private class HistCalc extends Thread { public void run() { synchronized (m_locker) { if((m_classIndex >= 0) && (m_data.attribute(m_classIndex).isNominal())) { int intervals; double intervalWidth=0.0; //This uses the M.P.Wand's method to calculate the histogram's //interval width. See "Data-Based Choice of Histogram Bin Width", in //The American Statistician, Vol. 51, No. 1, Feb., 1997, pp. 59-64. //intervalWidth = Math.pow(6D/( -psi(2, g21())*m_data.numInstances()), // 1/3D ); //This uses the Scott's method to calculate the histogram's interval //width. See "On optimal and data-based histograms". // See Biometrika, 66, 605-610 OR see the same paper mentioned above. intervalWidth = 3.49 * m_as.numericStats.stdDev * Math.pow(m_data.numInstances(), -1/3D); //The Math.max is introduced to remove the possibility of //intervals=0 and =NAN that can happen if respectively all the numeric //values are the same or the interval width is evaluated to zero. intervals = Math.max(1, (int)Math.round( (m_as.numericStats.max - m_as.numericStats.min) / intervalWidth) ); //System.out.println("Max: "+m_as.numericStats.max+ // " Min: "+m_as.numericStats.min+ // " stdDev: "+m_as.numericStats.stdDev+ // "intervalWidth: "+intervalWidth); //The number 4 below actually represents a padding of 3 pixels on //each side of the histogram, and is also reflected in other parts of //the code in the shape of numerical constants like "6" here. if(intervals > AttributeVisualizationPanel.this.getWidth()) { intervals = AttributeVisualizationPanel.this.getWidth()-6; if(intervals<1)//if width is too small then use 1 and forget padding intervals = 1; } int histClassCounts[][] = new int[intervals] [m_data.attribute(m_classIndex).numValues()+1]; double barRange = (m_as.numericStats.max - m_as.numericStats.min) / (double)histClassCounts.length; m_maxValue = 0; if(m_colorList.size()==0) m_colorList.addElement(Color.black); for(int i = m_colorList.size(); i < m_data.attribute(m_classIndex).numValues()+1; i++) { Color pc = m_defaultColors[(i-1) % 10]; int ija = (i-1) / 10; ija *= 2; for (int j=0;j<ija;j++) { pc = pc.darker(); } m_colorList.addElement(pc); } for(int k=0; k<m_data.numInstances(); k++) { int t=0; //This holds the interval that the attibute value of the //new instance belongs to. try { if(!m_data.instance(k).isMissing(m_attribIndex)) { //1. see footnote at the end of this file t = (int)Math.ceil( (float)( (m_data.instance(k).value(m_attribIndex)-m_as.numericStats.min) / barRange) ); if(t==0) { if(m_data.instance(k).isMissing(m_classIndex)) histClassCounts[t][0]++; else histClassCounts[t][(int)m_data.instance(k).value(m_classIndex)+1]++; //if(histCounts[t]>m_maxValue) // m_maxValue = histCounts[t]; } else { if(m_data.instance(k).isMissing(m_classIndex)) histClassCounts[t-1][0]++; else histClassCounts[t-1][(int)m_data.instance(k).value(m_classIndex)+1]++; //if(histCounts[t-1]>m_maxValue) // m_maxValue = histCounts[t-1]; } } } catch(ArrayIndexOutOfBoundsException ae) { System.out.println("t:"+(t)+ " barRange:"+barRange+ " histLength:"+histClassCounts.length+ " value:"+m_data.instance(k).value(m_attribIndex)+ " min:"+m_as.numericStats.min+ " sumResult:"+ (m_data.instance(k).value(m_attribIndex) - m_as.numericStats.min)+ " divideResult:"+ (float)((m_data.instance(k).value(m_attribIndex) - m_as.numericStats.min) / barRange)+ " finalResult:"+ Math.ceil((float)((m_data.instance(k).value(m_attribIndex)- m_as.numericStats.min) / barRange)) ); } } for(int i=0; i<histClassCounts.length; i++) { int sum=0; for(int j=0; j<histClassCounts[i].length; j++) sum = sum+histClassCounts[i][j]; if(m_maxValue<sum) m_maxValue = sum; } m_histBarClassCounts = histClassCounts; m_barRange = barRange; } else { //else if the class attribute is numeric or the class is not set int intervals; double intervalWidth; //At the time of this coding the //possibility of datasets with zero instances //was being dealt with in the //PreProcessPanel of weka Explorer. //old method of calculating number of intervals //intervals = m_as.totalCount>10 ? // (int)(m_as.totalCount*0.1):(int)m_as.totalCount; //This uses the M.P.Wand's method to calculate the histogram's //interval width. See "Data-Based Choice of Histogram Bin Width", in //The American Statistician, Vol. 51, No. 1, Feb., 1997, pp. 59-64. //intervalWidth = Math.pow(6D/(-psi(2, g21())*m_data.numInstances() ), // 1/3D ); //This uses the Scott's method to calculate the histogram's interval //width. See "On optimal and data-based histograms". // See Biometrika, 66, 605-610 OR see the same paper mentioned above. intervalWidth = 3.49 * m_as.numericStats.stdDev * Math.pow(m_data.numInstances(), -1/3D); //The Math.max is introduced to remove the possibility of //intervals=0 and =NAN that can happen if respectively all the numeric //values are the same or the interval width is evaluated to zero. intervals = Math.max(1, (int)Math.round( (m_as.numericStats.max - m_as.numericStats.min) / intervalWidth) ); //The number 4 below actually represents a padding of 3 pixels on //each side of the histogram, and is also reflected in other parts of //the code in the shape of numerical constants like "6" here. if(intervals > AttributeVisualizationPanel.this.getWidth()) { intervals = AttributeVisualizationPanel.this.getWidth()-6; if(intervals<1) intervals = 1; } int histCounts[] = new int[intervals]; double barRange = (m_as.numericStats.max - m_as.numericStats.min) / (double)histCounts.length; m_maxValue = 0; for(int k=0; k<m_data.numInstances(); k++) { int t=0; //This holds the interval to which the current attribute's //value of this particular instance k belongs to. if(m_data.instance(k).isMissing(m_attribIndex)) continue; //ignore missing values try { //1. see footnote at the end of this file t =(int) Math.ceil((float)( (m_data.instance(k).value(m_attribIndex)-m_as.numericStats.min) / barRange)); if(t==0) { histCounts[t]++; if(histCounts[t]>m_maxValue) m_maxValue = histCounts[t]; } else { histCounts[t-1]++; if(histCounts[t-1]>m_maxValue) m_maxValue = histCounts[t-1]; } } catch(ArrayIndexOutOfBoundsException ae) { ae.printStackTrace(); System.out.println("t:"+(t)+ " barRange:"+barRange+ " histLength:"+histCounts.length+ " value:"+m_data.instance(k).value(m_attribIndex)+
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -