/** * */ package analyze; import java.awt.*; import java.awt.event.*; import java.awt.geom.*; import java.text.*; import javax.swing.*; // FrontEnd imports import model.color.ColorConst; import gui.DirectionResponses; import util.*; /** *

Title:CorrelGaborGabor

*

Description: correlates two outputs from the Simple * Cell model of the cortex. *

*

Copyright: Copyright (c) 2009

*

Company: Hanover College

* @author John H. Krantz, Ph.D. * @version 0.1 */ public class DiffPlot { static final long serialVersionUID = 0; //original cortex data private double [][][] cortex1; private double [][][] dog; private CorrelTable correl = new CorrelTable(); // private double [][][] cortex2; for later private int numOrient = Defaults.NUM_ORIENT; // fonts private Font titleFont = new Font("SansSerif",Font.BOLD,18); private Font labelFont = new Font("SansSerif",Font.BOLD,12); // arrays to send to correlation private double [][] array1; private double [][] array2; private double [][] diffArray; private String [] array1Items; private String [] array2Items; private Summarize sum1 = new Summarize(); // labels for what is being correlated private String label1 = "Array1"; private String label2 = "Array2"; // display elements private JPanel titlePanel = new JPanel(); private JLabel titleLabel = new JLabel("Correlate two sets of values from the Gabor Analysis"); private JPanel diffItemPanel = new JPanel(new BorderLayout()); private JPanel array1Panel = new JPanel(); private JLabel array1Label = new JLabel("First Array: "); private JComboBox array1CBX; private JPanel array2Panel = new JPanel(); private JLabel array2Label = new JLabel("Second Array: "); private JComboBox array2CBX; // difference plot private PlotDiff diff = new PlotDiff(); // drawing objects private boolean doScale = true; private int x = -1, y = -1; private int startX = -1, startY = -1; private Rectangle2D rfArray [][]; // Difference plot objects private JButton plotDiff = new JButton("Plot Differences"); private JPanel plotDiffPanel = new JPanel(); private JPanel buttonPanel = new JPanel(new GridLayout(4,1)); DecimalFormat rFmt = new DecimalFormat("0.000"); // display frame private MainFrame df;; // control objects private JButton saveButton = new JButton("Save Differences"); String dir = ""; private JPanel clickPanel = new JPanel(); private JLabel clickMeans = new JLabel(" Click: "); private JCheckBox getData = new JCheckBox("Show Data", false); private JPanel scalePanel = new JPanel(); private JCheckBox scale = new JCheckBox("Scale",true); private JButton restoreButton = new JButton("100%"); // flags // picture movement objects private JButton centerButton = new JButton("Center"); private JPanel arrowPanel = new JPanel(); private DirectionResponses arrows = new DirectionResponses(); // plotting offsets int iOff = 0; int jOff = 0; int iMax = 0; int jMax = 0; private JPanel offPanel = new JPanel(new GridLayout(2,2)); private JLabel xLabel = new JLabel("X Offset:"); private JLabel yLabel = new JLabel("Y Offset:"); private JLabel xVal = new JLabel("0"); private JLabel yVal = new JLabel("0"); // constructor public DiffPlot(double [][][] ctx, double [][][] DOG){ setCortexData(ctx); setDOG_Data(DOG); numOrient = cortex1[0][0].length; doLayout(); } public void doLayout(){ // set up fonts titleLabel.setFont(titleFont); array1Label.setFont(labelFont); array2Label.setFont(labelFont); // set up the panels for the elements of the correlation array1Panel.add(array1Label); array1Items = GetItemLists.getCortexDogStrings(numOrient); array1CBX = new JComboBox(array1Items); array1CBX.setSelectedIndex(GetItemLists.SD_MEAN); array1Panel.add(array1CBX); array2Panel.add(array2Label); array2Items = GetItemLists.getCortexDogStrings(numOrient); array2CBX = new JComboBox(array2Items); array2CBX.setSelectedIndex(GetItemLists.SD_MEAN); array2Panel.add(array2CBX); plotDiffPanel.add(plotDiff); plotDiffPanel.add(saveButton); clickPanel.add(clickMeans); clickPanel.add(getData); clickPanel.add(scale); scalePanel.add(restoreButton); scalePanel.add(centerButton); arrowPanel.add(arrows); offPanel.add(xLabel);offPanel.add(xVal); offPanel.add(yLabel);offPanel.add(yVal); arrowPanel.add(offPanel); buttonPanel.add(arrowPanel); arrowPanel.setVisible(false); buttonPanel.add(clickPanel); buttonPanel.add(scalePanel); buttonPanel.add(plotDiffPanel); diffItemPanel.add(array1Panel,BorderLayout.WEST); diffItemPanel.add(array2Panel,BorderLayout.EAST); diffItemPanel.add(buttonPanel,BorderLayout.SOUTH); // add the title df = new MainFrame(titleLabel.getText()); Container c = df.getContentPane(); titlePanel.add(titleLabel); c.add(titlePanel,BorderLayout.NORTH); c.add(diffItemPanel,BorderLayout.WEST); c.add(diff,BorderLayout.CENTER); setupListeners(); df.setVisible(true); } private void setupListeners(){ plotDiff.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ showDiff(); df.toFront(); } }); array2CBX.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ showDiff(); df.toFront(); } }); diff.addMouseListener(new MouseListener(){ public void mouseClicked(MouseEvent e){ if (doScale){// change the scale of the image double curScale = diff.getDrawScale(); if (e.getButton()==MouseEvent.BUTTON1){ curScale = curScale*1.25; } else { curScale = curScale/1.25; if (curScale < 0.5) curScale = 0.5; } diff.setDrawScale(curScale); diff.repaint(); } } public void mouseEntered(MouseEvent e){} public void mouseExited(MouseEvent e){} public void mousePressed(MouseEvent e){ if (doScale){ // get the starting location of the mouse // during dragging startX = e.getX(); startY = e.getY(); } else { // get data and determine location to // get output y = -1; // default out of bounds x = -1; // showOutput(); rfArray = diff.getRFArray(); if (rfArray != null){ // give summary data for a single location // output.setEnabled(true); // allow to save data now. // first determine which column for (int i = 0; i < rfArray.length; i ++){ for (int j = 0; j < rfArray[0].length; j ++){ if (rfArray[i][j].contains(e.getX(), e.getY())){ y = i; x = j; } // end checking if clicked in box } // end j } // end i showPointSum(x,y); diff.grabFocus(); } } } public void mouseReleased(MouseEvent e){} });// end peaks mouse listener // plot mouse motion listener diff.addMouseMotionListener(new MouseMotionListener(){ public void mouseMoved(MouseEvent e){} public void mouseDragged(MouseEvent e){ if (doScale){ int curX = e.getX(); int curY = e.getY(); int moveX = curX - startX; int moveY = curY - startY; startX = curX; startY = curY; diff.moveFigure(moveX, moveY); diff.repaint(); } } }); // end plot mouse motion listener // listener to the mouse click check boxes getData.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ if (!getData.isSelected()){ getData.setSelected(true); } if (scale.isSelected()){ scale.setSelected(false); doScale = false; } } }); scale.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ if (!scale.isSelected()){ scale.setSelected(true); } if (getData.isSelected()){ getData.setSelected(false); doScale = true; } } }); restoreButton.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ diff.setDrawScale(1.0); diff.setCenter(true); diff.repaint(); } }); centerButton.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ diff.setCenter(true); diff.repaint(); } }); saveButton.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ dir = diff.SaveDiffData(dir, titleLabel.getText()+"\n\n"); } }); arrows.addMouseListener(new java.awt.event.MouseAdapter() { public void mouseClicked(MouseEvent e) { arrows_mouseClicked(e); } public void mouseExited(MouseEvent e) { arrows.mouseExited(e); } }); arrows.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() { public void mouseMoved(MouseEvent e) { arrows.mouseMoved(e); } }); arrows.addMouseListener(new java.awt.event.MouseAdapter() { public void mouseReleased(MouseEvent e) { arrows.mouseReleased(e); } }); } // arrows methods private void arrows_mouseClicked(MouseEvent e){ arrows.mouseClicked(e); int dir = arrows.getDirection(); switch (dir){ case (DirectionResponses.UP) : iOff --; if (iOff < 0) iOff = 0; break; case (DirectionResponses.DOWN) : iOff ++; if (iOff > iMax) iOff = iMax; break; case (DirectionResponses.LEFT) : jOff --; if (jOff < 0) jOff = 0; break; case (DirectionResponses.RIGHT) : jOff ++; if (jOff > jMax) jOff = jMax; break; } plotDiff(array1,array2); } private void showDiff(){ int a1 = array1CBX.getSelectedIndex(); int a2 = array2CBX.getSelectedIndex(); array1 = new double[cortex1.length][cortex1[0].length]; array2 = new double[cortex1.length][cortex1[0].length]; label1 = array1Items[array1CBX.getSelectedIndex()]; label2 = array2Items[array2CBX.getSelectedIndex()]; titleLabel.setText("Plot of Difference of "+ label1+" - "+label2); df.setTitle(titleLabel.getText()); // set up the arrays // set up array 1 if (a1 >= GetItemLists.SD_OR_START){ a1 -= GetItemLists.SD_OR_START; // create the arrays for (int i = 0; i < array1.length; i ++){ // if selecting an orientation for (int j = 0; j < array1[0].length; j ++){ array1[i][j] = cortex1[i][j][a1]; } } }// end if selecting single orientation else { // not a single orientation but a mean or max or min int indx = Summarize.MAX; boolean gabor = true; switch(a1){ case (GetItemLists.SD_ACHROM): indx = ColorConst.BL_WH; gabor = false; break; case (GetItemLists.SD_RG): indx = ColorConst.R_G; gabor = false; break; case (GetItemLists.SD_BY): indx = ColorConst.B_Y; gabor = false; break; case (GetItemLists.SD_MAX): indx = Summarize.MAX; gabor = true; break; case (GetItemLists.SD_MIN): indx = Summarize.MIN; gabor = true; break; case (GetItemLists.SD_MEAN): indx = Summarize.MEAN; gabor = true; break; } if (gabor){ array1 = new double[cortex1.length][cortex1[0].length]; // create the arrays for (int i = 0; i < array1.length; i ++){ // if selecting an orientation for (int j = 0; j < array1[0].length; j ++){ array1[i][j] = sum1.getPointSummary(i, j)[indx]; } } } else { array1 = new double[dog.length][dog[0].length]; // create the arrays for (int i = 0; i < array1.length; i ++){ // if selecting an orientation for (int j = 0; j < array1[0].length; j ++){ array1[i][j] = dog[i][j][indx]; } } } } if (a2 >= GetItemLists.SD_OR_START){ a2 -= GetItemLists.SD_OR_START; array2 = new double[cortex1.length][cortex1[0].length]; // create the arrays for (int i = 0; i < array2.length; i ++){ // if selecting an orientation for (int j = 0; j < array2[0].length; j ++){ array2[i][j] = cortex1[i][j][a2]; } } }// end if selecting single orientation else { // not a single orientation but a mean or max or min int indx = Summarize.MAX; boolean gabor = true; switch(a2){ case (GetItemLists.SD_ACHROM): indx = ColorConst.BL_WH; gabor = false; break; case (GetItemLists.SD_RG): indx = ColorConst.R_G; gabor = false; break; case (GetItemLists.SD_BY): indx = ColorConst.B_Y; gabor = false; break; case (GetItemLists.SD_MAX): indx = Summarize.MAX; gabor = true; break; case (GetItemLists.SD_MIN): indx = Summarize.MIN; gabor = true; break; case (GetItemLists.SD_MEAN): indx = Summarize.MEAN; gabor = true; break; } if (gabor){ array2 = new double[cortex1.length][cortex1[0].length]; // create the arrays for (int i = 0; i < array2.length; i ++){ // if selecting an orientation for (int j = 0; j < array2[0].length; j ++){ array2[i][j] = sum1.getPointSummary(i, j)[indx]; } } } else { array2 = new double[dog.length][dog[0].length]; // create the arrays for (int i = 0; i < array2.length; i ++){ // if selecting an orientation for (int j = 0; j < array2[0].length; j ++){ array2[i][j] = dog[i][j][indx]; } } } } // pick what gets plotted for default iOff = 0; jOff = 0; iMax = 0; jMax = 0; arrowPanel.setVisible(false); // get the correlation if (array1.length != array2.length | array1[0].length != array2[0].length){ arrowPanel.setVisible(true); arrows.grabFocus(); correl.setOutputs(array1, array2); double r[][] = correl.runCorrel(); iMax = r.length-1; jMax = r[0].length-1; double rMax = -2; // find the position of the maximum r for (int i = 0; i < r.length; i ++){ for (int j = 0; j < r[0].length; j ++){ if (r[i][j] > rMax){ rMax = r[i][j]; iOff = i; jOff = j; } } } //JOptionPane.showMessageDialog(null, //"iOff = "+iOff+"\n" //+"jOff = "+jOff+"\n" //+"rMax = "+rMax+"\n" //+"rLength = "+r.length); } plotDiff(array1,array2); } private void plotDiff(double [][] array1,double [][] array2){ // feedback on postion. xVal.setText(""+jOff); yVal.setText(""+iOff); // calculate the difference int x = array1.length; int y = array1[0].length; // find the smaller array if (array2.length < array1.length){ x = array2.length; } if (array2[0].length < array1[0].length){ y = array2[0].length; } diffArray = new double[x][y]; // calculate the difference for (int i = 0; i < diffArray.length; i ++){ for (int j = 0; j < diffArray[0].length; j ++){ // offsets for the two arrays if different sizes int i1 = 0; int i2 = 0; int j1 = 0; int j2 = 0; if (array1.length < array2.length){ i1 = 0; i2 = iOff; } else { i1 = iOff; i2 = 0; } if (array1[0].length < array2[0].length){ j1 = 0; j2 = jOff; } else { j1 = jOff; j2 = 0; } diffArray[i][j] = array1[i+i1][j+j1] - array2[i+i2][j+j2]; } } // send to plot diff.setDifferences(diffArray); diff.repaint(); } // show the summary information for a point on the screen private void showPointSum(int x, int y){ // now if in one of the boxes, report summary if (x != -1 & y != -1){ // dog sell summary String st = "Difference Value for x = "+x; st += " y = "+y+"\n"; st += "Difference = "+ rFmt.format(diffArray[y][x]); JOptionPane.showMessageDialog(null,st, "Point Difference", JOptionPane.PLAIN_MESSAGE); } } // set methods /** * Set the data to be used in determining the * correlations * @param double [][][] ctx the cortex data */ public void setCortexData(double [][][] ctx){ cortex1 = ctx; sum1.setColumns(cortex1); } public void setDOG_Data(double [][][] DOG){ dog = DOG; } public void setLabels(String array1Label, String array2Label){ label1 = array1Label; label2 = array2Label; } public void setDirectory(String directory){ dir = (dir == "" ? directory : dir); } }