List of usage examples for weka.core Instances attribute
publicAttribute attribute(String name)
From source file:adams.flow.transformer.WekaSetInstancesValue.java
License:Open Source License
/** * Executes the flow item.// ww w. j av a2 s. c o m * * @return null if everything is fine, otherwise error message */ @Override protected String doExecute() { String result; Instances inst; int row; int index; result = null; inst = (Instances) m_InputToken.getPayload(); inst = new Instances(inst); m_Row.setMax(inst.numInstances()); m_Column.setData(inst); row = m_Row.getIntIndex(); index = m_Column.getIntIndex(); if (row == -1) result = "Failed to retrieve row: " + m_Row.getIndex(); else if (index == -1) result = "Failed to retrieve column: " + m_Column.getIndex(); if (result == null) { try { if (m_Value.equals("?")) { inst.instance(row).setMissing(index); } else { switch (inst.attribute(index).type()) { case Attribute.NUMERIC: inst.instance(row).setValue(index, Utils.toDouble(m_Value)); break; case Attribute.DATE: inst.instance(row).setValue(index, inst.attribute(index).parseDate(m_Value)); break; case Attribute.NOMINAL: case Attribute.STRING: inst.instance(row).setValue(index, m_Value); break; case Attribute.RELATIONAL: result = "Relational attributes cannot be set!"; break; default: result = "Unhandled attribute type: " + inst.attribute(index).type(); } } } catch (Exception e) { result = handleException("Failed to set value: " + m_Column.getIndex() + " -> " + m_Value, e); } } // broadcast data if (result == null) m_OutputToken = new Token(inst); return result; }
From source file:adams.gui.menu.AppendDatasets.java
License:Open Source License
/** * Performs the append.//from w w w . j a v a2s .c om * * @param frame the frame to close * @param input the files to merge * @param output the output file */ protected void doAppend(ChildFrame frame, File[] input, File output) { Instances[] data; Instances full; int i; int n; AbstractFileLoader loader; DataSink sink; int count; TIntArrayList transferAtt; int index; if (input.length < 2) { GUIHelper.showErrorMessage(getOwner(), "At least two files are required!"); return; } // load and check compatibility loader = ConverterUtils.getLoaderForFile(input[0]); data = new Instances[input.length]; count = 0; transferAtt = new TIntArrayList(); for (i = 0; i < input.length; i++) { try { loader.setFile(input[i]); data[i] = DataSource.read(loader); if (i > 0) { if (!data[0].equalHeaders(data[i])) { GUIHelper.showErrorMessage(getOwner(), "Datasets '" + input[0] + "' and '" + input[i] + "' are not compatible!\n" + data[0].equalHeadersMsg(data[i])); return; } } else { for (n = 0; n < data[0].numAttributes(); n++) { if (data[0].attribute(n).isString() || data[0].attribute(n).isRelationValued()) transferAtt.add(n); } } count += data[i].numInstances(); } catch (Exception e) { GUIHelper.showErrorMessage(getOwner(), "Failed to read '" + input[i] + "'!\n" + Utils.throwableToString(e)); return; } } // combine full = new Instances(data[0], count); for (i = 0; i < data.length; i++) { for (Instance inst : data[i]) { if (transferAtt.size() > 0) { for (n = 0; n < transferAtt.size(); n++) { index = transferAtt.get(n); if (inst.attribute(index).isString()) full.attribute(index).addStringValue(inst.stringValue(index)); else if (inst.attribute(n).isRelationValued()) full.attribute(index).addRelation(inst.relationalValue(index)); else throw new IllegalStateException( "Unhandled attribute type: " + Attribute.typeToString(inst.attribute(index))); } } full.add(inst); } } // save try { sink = new DataSink(output.getAbsolutePath()); sink.write(full); } catch (Exception e) { GUIHelper.showErrorMessage(getOwner(), "Failed to save data to '" + output + "'!\n" + Utils.throwableToString(e)); return; } GUIHelper.showInformationMessage(null, "Successfully appended!\n" + output); frame.dispose(); }
From source file:adams.gui.visualization.debug.objectrenderer.WekaInstancesRenderer.java
License:Open Source License
/** * Performs the actual rendering./*w w w .j ava2 s.c om*/ * * @param obj the object to render * @param panel the panel to render into * @return null if successful, otherwise error message */ @Override protected String doRender(Object obj, JPanel panel) { Instance inst; Instances data; InstancesTable table; InstancesTableModel model; BaseScrollPane scrollPane; PlainTextRenderer plain; SpreadSheet sheet; Row row; int i; SpreadSheetRenderer sprenderer; if (obj instanceof Instances) { data = (Instances) obj; if (data.numInstances() == 0) { sheet = new DefaultSpreadSheet(); row = sheet.getHeaderRow(); row.addCell("I").setContentAsString("Index"); row.addCell("N").setContentAsString("Name"); row.addCell("T").setContentAsString("Type"); row.addCell("C").setContentAsString("Class"); for (i = 0; i < data.numAttributes(); i++) { row = sheet.addRow(); row.addCell("I").setContent(i + 1); row.addCell("N").setContentAsString(data.attribute(i).name()); row.addCell("T").setContentAsString(Attribute.typeToString(data.attribute(i))); row.addCell("C").setContent((i == data.classIndex()) ? "true" : ""); } sprenderer = new SpreadSheetRenderer(); sprenderer.render(sheet, panel); } else { model = new InstancesTableModel(data); model.setShowAttributeIndex(true); table = new InstancesTable(model); scrollPane = new BaseScrollPane(table); panel.add(scrollPane, BorderLayout.CENTER); } } else { inst = (Instance) obj; if (inst.dataset() != null) { data = new Instances(inst.dataset(), 0); data.add((Instance) inst.copy()); table = new InstancesTable(data); scrollPane = new BaseScrollPane(table); panel.add(scrollPane, BorderLayout.CENTER); } else { plain = new PlainTextRenderer(); plain.render(obj, panel); } } return null; }
From source file:adams.gui.visualization.instance.InstanceExplorer.java
License:Open Source License
/** * Loads the given data into the container manager. * * @param dataset the dataset/* w w w . j av a 2s . c om*/ * @param data the data to add to the manager */ protected void loadData(Instances dataset, List<InstanceContainer> data) { boolean hasDBID; InstanceContainerList listInst; ReportContainerList listReport; // turn off anti-aliasing to speed up display if (getContainerManager().count() + data.size() > getProperties() .getInteger("MaxNumContainersWithAntiAliasing", 50)) { if (getInstancePanel().isAntiAliasingEnabled()) getInstancePanel().setAntiAliasingEnabled(false); } listInst = (InstanceContainerList) m_PanelInstance.getContainerList(); listReport = m_Reports.getReportContainerList(); hasDBID = (dataset.attribute(ArffUtils.getDBIDName()) != null); listInst.setDisplayDatabaseID(hasDBID); listReport.setDisplayDatabaseID(hasDBID); getContainerManager().addAll(data); }
From source file:adams.gui.visualization.instances.InstancesColumnComboBox.java
License:Open Source License
/** * Updates the content of the combobox.//from ww w. j a v a 2 s . c o m */ protected synchronized void update() { List<ColumnContainer> columns; int i; Instances data; columns = new ArrayList<>(); data = m_Table.getInstances(); if (data != null) { for (i = 0; i < data.numAttributes(); i++) columns.add(new ColumnContainer(data.attribute(i).name(), i)); Collections.sort(columns); } setModel(new DefaultComboBoxModel<>(columns.toArray(new ColumnContainer[0]))); }
From source file:adams.gui.visualization.instances.instancestable.AttributeStatistics.java
License:Open Source License
/** * Processes the specified column.//from w w w . ja va 2 s .co m * * @param table the source table * @param data the instances to use as basis * @param column the column in the spreadsheet * @return true if successful */ @Override protected boolean doProcessColumn(InstancesTable table, Instances data, int column) { AttributeStats stats; TextDialog dialog; stats = data.attributeStats(column); if (GUIHelper.getParentDialog(table) != null) dialog = new TextDialog(GUIHelper.getParentDialog(table), ModalityType.MODELESS); else dialog = new TextDialog(GUIHelper.getParentFrame(table), false); dialog.setDefaultCloseOperation(TextDialog.DISPOSE_ON_CLOSE); dialog.setTitle("Attribute statistics for column #" + (column + 1) + "/" + data.attribute(column).name()); dialog.setUpdateParentTitle(false); dialog.setContent(stats.toString()); dialog.pack(); dialog.setLocationRelativeTo(null); dialog.setVisible(true); return true; }
From source file:adams.gui.visualization.instances.instancestable.ColumnStatistic.java
License:Open Source License
/** * Processes the specified column.// w ww . ja v a2 s .co m * * @param table the source table * @param data the instances to use as basis * @param column the column in the spreadsheet * @return true if successful */ @Override protected boolean doProcessColumn(InstancesTable table, Instances data, int column) { GenericObjectEditorDialog setup; AbstractColumnStatistic last; SpreadSheet stats; SpreadSheetDialog dialog; // let user customize plot if (GUIHelper.getParentDialog(table) != null) setup = new GenericObjectEditorDialog(GUIHelper.getParentDialog(table), ModalityType.DOCUMENT_MODAL); else setup = new GenericObjectEditorDialog(GUIHelper.getParentFrame(table), true); setup.setDefaultCloseOperation(HistogramFactory.SetupDialog.DISPOSE_ON_CLOSE); setup.getGOEEditor().setClassType(AbstractColumnStatistic.class); setup.getGOEEditor().setCanChangeClassInDialog(true); last = (AbstractColumnStatistic) table.getLastSetup(getClass(), true, false); if (last == null) last = new Mean(); setup.setCurrent(last); setup.setLocationRelativeTo(GUIHelper.getParentComponent(table)); setup.setVisible(true); if (setup.getResult() != GenericObjectEditorDialog.APPROVE_OPTION) return false; last = (AbstractColumnStatistic) setup.getCurrent(); table.addLastSetup(getClass(), true, false, last); stats = last.generate(new InstancesView(data), column); if (stats == null) { if (last.hasLastError()) GUIHelper.showErrorMessage(GUIHelper.getParentComponent(table), "Failed to calculate statistics for column #" + (column + 1) + ": " + last.getLastError()); else GUIHelper.showErrorMessage(GUIHelper.getParentComponent(table), "Failed to calculate statistics for column #" + (column + 1) + "!"); } else { if (GUIHelper.getParentDialog(table) != null) dialog = new SpreadSheetDialog(GUIHelper.getParentDialog(table), ModalityType.MODELESS); else dialog = new SpreadSheetDialog(GUIHelper.getParentFrame(table), false); dialog.setDefaultCloseOperation(SpreadSheetDialog.DISPOSE_ON_CLOSE); dialog.setTitle("Statistics for column #" + (column + 1) + "/" + data.attribute(column).name()); dialog.setSpreadSheet(stats); dialog.pack(); dialog.setLocationRelativeTo(null); dialog.setVisible(true); } return (stats != null); }
From source file:adams.gui.visualization.instances.instancestable.Histogram.java
License:Open Source License
/** * Allows the user to generate a plot from either a row or a column. * * @param data the instances to use//from w w w .j av a 2 s . c o m * @param isColumn whether to use column or row * @param index the index of the row/column */ protected void plot(final InstancesTable table, final Instances data, final boolean isColumn, int index) { TDoubleArrayList list; HistogramFactory.SetupDialog setup; HistogramFactory.Dialog dialog; int i; ArrayHistogram last; int col; int row; Object value; // let user customize histogram if (GUIHelper.getParentDialog(table) != null) setup = HistogramFactory.getSetupDialog(GUIHelper.getParentDialog(table), ModalityType.DOCUMENT_MODAL); else setup = HistogramFactory.getSetupDialog(GUIHelper.getParentFrame(table), true); setup.setDefaultCloseOperation(HistogramFactory.SetupDialog.DISPOSE_ON_CLOSE); setup.setTitle("Histogram setup"); last = (ArrayHistogram) table.getLastSetup(getClass(), true, !isColumn); if (last == null) last = new ArrayHistogram(); setup.setCurrent(last); setup.setLocationRelativeTo(GUIHelper.getParentComponent(table)); setup.setVisible(true); if (setup.getResult() != GenericObjectEditorDialog.APPROVE_OPTION) return; last = (ArrayHistogram) setup.getCurrent(); table.addLastSetup(getClass(), true, !isColumn, last); // get data from instances list = new TDoubleArrayList(); if (isColumn) { col = index + 1; for (i = 0; i < table.getRowCount(); i++) { value = table.getValueAt(i, col); if ((value != null) && (Utils.isDouble(value.toString()))) list.add(Utils.toDouble(value.toString())); } } else { row = index; for (i = 0; i < data.numAttributes(); i++) { if (data.attribute(i).isNumeric() && !data.instance(row).isMissing(i)) list.add(data.instance(row).value(i)); } } // calculate histogram last.clear(); // display histogram if (GUIHelper.getParentDialog(table) != null) dialog = HistogramFactory.getDialog(GUIHelper.getParentDialog(table), ModalityType.MODELESS); else dialog = HistogramFactory.getDialog(GUIHelper.getParentFrame(table), false); dialog.setDefaultCloseOperation(HistogramFactory.Dialog.DISPOSE_ON_CLOSE); if (isColumn) dialog.add(last, list.toArray(), "Column " + (index + 1) + "/" + data.attribute(index).name()); else dialog.add(last, list.toArray(), "Row " + (index + 1)); dialog.setLocationRelativeTo(GUIHelper.getParentComponent(table)); dialog.setVisible(true); }
From source file:adams.gui.visualization.instances.instancestable.JFreeChart.java
License:Open Source License
/** * Allows the user to generate a plot from either a row or a column. * * @param data the instances to use//from ww w . jav a2s . c o m * @param isColumn whether the to use column or row * @param index the index of the row/column */ protected void plot(final InstancesTable table, final Instances data, final boolean isColumn, int index) { final List<Double> list; List<Double> tmp; GenericObjectEditorDialog setup; int i; final String title; SwingWorker worker; adams.flow.sink.JFreeChartPlot last; int numPoints; String newPoints; int col; int row; Object value; final SpreadSheet sheet; Row srow; boolean sorted; boolean asc; numPoints = isColumn ? data.numInstances() : data.numAttributes(); if (numPoints > MAX_POINTS) { newPoints = GUIHelper.showInputDialog(null, "More than " + MAX_POINTS + " data points to plot - enter sample size:", "" + numPoints); if (newPoints == null) return; if (!Utils.isInteger(newPoints)) return; if (Integer.parseInt(newPoints) != numPoints) numPoints = Integer.parseInt(newPoints); else numPoints = -1; } else { numPoints = -1; } // let user customize plot if (GUIHelper.getParentDialog(table) != null) setup = new GenericObjectEditorDialog(GUIHelper.getParentDialog(table), ModalityType.DOCUMENT_MODAL); else setup = new GenericObjectEditorDialog(GUIHelper.getParentFrame(table), true); setup.setDefaultCloseOperation(GenericObjectEditorDialog.DISPOSE_ON_CLOSE); setup.getGOEEditor().setClassType(Actor.class); setup.getGOEEditor().setCanChangeClassInDialog(false); last = (adams.flow.sink.JFreeChartPlot) table.getLastSetup(getClass(), true, !isColumn); if (last == null) last = new adams.flow.sink.JFreeChartPlot(); setup.setCurrent(last); setup.setLocationRelativeTo(GUIHelper.getParentComponent(table)); setup.setVisible(true); if (setup.getResult() != GenericObjectEditorDialog.APPROVE_OPTION) return; last = (adams.flow.sink.JFreeChartPlot) setup.getCurrent(); table.addLastSetup(getClass(), true, !isColumn, last); // get data from instances tmp = new ArrayList<>(); sorted = false; asc = table.isAscending(); if (isColumn) { col = index + 1; sorted = (table.getSortColumn() == col); for (i = 0; i < table.getRowCount(); i++) { value = table.getValueAt(i, col); if ((value != null) && (Utils.isDouble(value.toString()))) tmp.add(Utils.toDouble(value.toString())); } } else { row = index; for (i = 0; i < data.numAttributes(); i++) { if (data.attribute(i).isNumeric() && !data.instance(row).isMissing(i)) tmp.add(data.instance(row).value(i)); } } if (numPoints > -1) { numPoints = Math.min(numPoints, tmp.size()); Collections.shuffle(tmp, new Random(1)); list = tmp.subList(0, numPoints); if (sorted) { Collections.sort(list); if (!asc) Collections.reverse(list); } } else { list = tmp; } // create new spreadsheet sheet = new DefaultSpreadSheet(); sheet.getHeaderRow().addCell("x").setContentAsString(isColumn ? "Row" : "Column"); sheet.getHeaderRow().addCell("y") .setContentAsString(isColumn ? data.attribute(index).name() : ("Row " + (index + 1))); for (i = 0; i < list.size(); i++) { srow = sheet.addRow(); srow.addCell("x").setContent((double) i + 1.0); srow.addCell("y").setContent(list.get(i)); } // generate plot if (isColumn) title = "Column " + (index + 1) + "/" + data.attribute(index).name(); else title = "Row " + (index + 1); last.getChart().setTitle(sheet.getColumnName(1)); worker = new SwingWorker() { @Override protected Object doInBackground() throws Exception { Flow flow = new Flow(); flow.setDefaultCloseOperation(BaseFrame.DISPOSE_ON_CLOSE); StorageValue sv = new StorageValue(); sv.setStorageName(new StorageName("values")); flow.add(sv); Object last = table.getLastSetup(JFreeChart.this.getClass(), true, !isColumn); adams.flow.sink.JFreeChartPlot plot = (adams.flow.sink.JFreeChartPlot) ((adams.flow.sink.JFreeChartPlot) last) .shallowCopy(); plot.setShortTitle(true); plot.setName(title); plot.setX(-2); plot.setY(-2); flow.add(plot); flow.setUp(); flow.getStorage().put(new StorageName("values"), sheet); flow.execute(); flow.wrapUp(); return null; } }; worker.execute(); }
From source file:adams.gui.visualization.instances.instancestable.SimplePlot.java
License:Open Source License
/** * Allows the user to generate a plot from either a row or a column. * * @param data the instances to use//from w w w. j a v a 2 s . c o m * @param isColumn whether the to use column or row * @param index the index of the row/column */ protected void plot(final InstancesTable table, final Instances data, final boolean isColumn, int index) { final List<Double> list; List<Double> tmp; GenericObjectEditorDialog setup; int i; final String title; SwingWorker worker; adams.flow.sink.SimplePlot last; int numPoints; String newPoints; int col; int row; Object value; boolean sorted; boolean asc; numPoints = isColumn ? data.numInstances() : data.numAttributes(); if (numPoints > MAX_POINTS) { newPoints = GUIHelper.showInputDialog(null, "More than " + MAX_POINTS + " data points to plot - enter sample size:", "" + numPoints); if (newPoints == null) return; if (!Utils.isInteger(newPoints)) return; if (Integer.parseInt(newPoints) != numPoints) numPoints = Integer.parseInt(newPoints); else numPoints = -1; } else { numPoints = -1; } // let user customize plot if (GUIHelper.getParentDialog(table) != null) setup = new GenericObjectEditorDialog(GUIHelper.getParentDialog(table), ModalityType.DOCUMENT_MODAL); else setup = new GenericObjectEditorDialog(GUIHelper.getParentFrame(table), true); setup.setDefaultCloseOperation(GenericObjectEditorDialog.DISPOSE_ON_CLOSE); setup.getGOEEditor().setClassType(Actor.class); setup.getGOEEditor().setCanChangeClassInDialog(false); last = (adams.flow.sink.SimplePlot) table.getLastSetup(getClass(), true, !isColumn); if (last == null) { last = new adams.flow.sink.SimplePlot(); last.setNoToolTips(true); last.setMouseClickAction(new ViewDataClickAction()); } setup.setCurrent(last); setup.setLocationRelativeTo(GUIHelper.getParentComponent(table)); setup.setVisible(true); if (setup.getResult() != GenericObjectEditorDialog.APPROVE_OPTION) return; last = (adams.flow.sink.SimplePlot) setup.getCurrent(); table.addLastSetup(getClass(), true, !isColumn, last); // get data from instances tmp = new ArrayList<>(); sorted = false; asc = table.isAscending(); if (isColumn) { col = index + 1; sorted = (table.getSortColumn() == col); for (i = 0; i < table.getRowCount(); i++) { value = table.getValueAt(i, col); if ((value != null) && (Utils.isDouble(value.toString()))) tmp.add(Utils.toDouble(value.toString())); } } else { row = index; for (i = 0; i < data.numAttributes(); i++) { if (data.attribute(i).isNumeric() && !data.instance(row).isMissing(i)) tmp.add(data.instance(row).value(i)); } } if (numPoints > -1) { numPoints = Math.min(numPoints, tmp.size()); Collections.shuffle(tmp, new Random(1)); list = tmp.subList(0, numPoints); if (sorted) { Collections.sort(list); if (!asc) Collections.reverse(list); } } else { list = tmp; } // generate plot if (isColumn) title = "Column " + (index + 1) + "/" + data.attribute(index).name(); else title = "Row " + (index + 1); worker = new SwingWorker() { @Override protected Object doInBackground() throws Exception { Flow flow = new Flow(); flow.setDefaultCloseOperation(BaseFrame.DISPOSE_ON_CLOSE); StorageValue sv = new StorageValue(); sv.setStorageName(new StorageName("values")); flow.add(sv); ArrayToSequence a2s = new ArrayToSequence(); flow.add(a2s); MakePlotContainer mpc = new MakePlotContainer(); mpc.setPlotName(title); flow.add(mpc); Object last = table.getLastSetup(SimplePlot.this.getClass(), true, !isColumn); adams.flow.sink.SimplePlot plot = (adams.flow.sink.SimplePlot) ((adams.flow.sink.SimplePlot) last) .shallowCopy(); plot.setShortTitle(true); plot.setShowSidePanel(false); plot.setName(title); plot.setX(-2); plot.setY(-2); flow.add(plot); flow.setUp(); flow.getStorage().put(new StorageName("values"), list.toArray(new Double[list.size()])); flow.execute(); flow.wrapUp(); return null; } }; worker.execute(); }