List of usage examples for weka.core Instances numInstances
publicint numInstances()
From source file:adams.flow.transformer.WekaSetInstancesValue.java
License:Open Source License
/** * Executes the flow item./*from w ww .j a v a 2 s .com*/ * * @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.flow.transformer.WekaSubsets.java
License:Open Source License
/** * Executes the flow item./*from w ww. j a va 2s .c o m*/ * * @return null if everything is fine, otherwise error message */ @Override protected String doExecute() { String result; Instances data; Double old; Double curr; int i; int index; Instance inst; result = null; m_Queue.clear(); // copy and sort data data = new Instances((Instances) m_InputToken.getPayload()); m_Index.setData(data); ; index = m_Index.getIntIndex(); data.sort(index); // create subsets old = null; i = 0; while (i < data.numInstances()) { inst = data.instance(i); curr = inst.value(index); if ((old == null) || !curr.equals(old)) { m_Queue.add(new Instances(data, data.numInstances())); old = curr; } m_Queue.get(m_Queue.size() - 1).add(inst); i++; } // compact subsets for (Instances sub : m_Queue) sub.compactify(); return result; }
From source file:adams.gui.menu.CostCurve.java
License:Open Source License
/** * Launches the functionality of the menu item. *//*from w w w .jav a 2 s .c o m*/ @Override public void launch() { File file; if (m_Parameters.length == 0) { // choose file int retVal = m_FileChooser.showOpenDialog(null); if (retVal != JFileChooser.APPROVE_OPTION) return; file = m_FileChooser.getSelectedFile(); } else { file = new PlaceholderFile(m_Parameters[0]).getAbsoluteFile(); m_FileChooser.setSelectedFile(file); } // create plot Instances result; try { result = m_FileChooser.getLoader().getDataSet(); } catch (Exception e) { GUIHelper.showErrorMessage(getOwner(), "Error loading file '" + file + "':\n" + adams.core.Utils.throwableToString(e)); return; } result.setClassIndex(result.numAttributes() - 1); ThresholdVisualizePanel vmc = new ThresholdVisualizePanel(); PlotData2D plot = new PlotData2D(result); plot.setPlotName(result.relationName()); plot.m_displayAllPoints = true; boolean[] connectPoints = new boolean[result.numInstances()]; for (int cp = 1; cp < connectPoints.length; cp++) connectPoints[cp] = true; try { plot.setConnectPoints(connectPoints); vmc.addPlot(plot); } catch (Exception e) { GUIHelper.showErrorMessage(getOwner(), "Error adding plot:\n" + adams.core.Utils.throwableToString(e)); return; } ChildFrame frame = createChildFrame(vmc, GUIHelper.getDefaultDialogDimension()); frame.setTitle(frame.getTitle() + " - " + file); }
From source file:adams.gui.menu.MarginCurve.java
License:Open Source License
/** * Launches the functionality of the menu item. */// ww w .j a v a2 s . co m @Override public void launch() { File file; if (m_Parameters.length == 0) { // choose file int retVal = m_FileChooser.showOpenDialog(null); if (retVal != JFileChooser.APPROVE_OPTION) return; file = m_FileChooser.getSelectedFile(); } else { file = new PlaceholderFile(m_Parameters[0]).getAbsoluteFile(); m_FileChooser.setSelectedFile(file); } // create plot Instances result; try { result = m_FileChooser.getLoader().getDataSet(); } catch (Exception e) { GUIHelper.showErrorMessage(getOwner(), "Error loading file '" + file + "':\n" + adams.core.Utils.throwableToString(e)); return; } result.setClassIndex(result.numAttributes() - 1); VisualizePanel vp = new VisualizePanel(); PlotData2D plot = new PlotData2D(result); plot.m_displayAllPoints = true; boolean[] connectPoints = new boolean[result.numInstances()]; for (int cp = 1; cp < connectPoints.length; cp++) connectPoints[cp] = true; try { plot.setConnectPoints(connectPoints); vp.addPlot(plot); } catch (Exception e) { GUIHelper.showErrorMessage(getOwner(), "Error adding plot:\n" + adams.core.Utils.throwableToString(e)); return; } ChildFrame frame = createChildFrame(vp, GUIHelper.getDefaultDialogDimension()); frame.setTitle(frame.getTitle() + " - " + file); }
From source file:adams.gui.menu.ROC.java
License:Open Source License
/** * Launches the functionality of the menu item. *///w w w.ja va 2 s. co m @Override public void launch() { File file; if (m_Parameters.length == 0) { // choose file int retVal = m_FileChooser.showOpenDialog(null); if (retVal != JFileChooser.APPROVE_OPTION) return; file = m_FileChooser.getSelectedFile(); } else { file = new PlaceholderFile(m_Parameters[0]).getAbsoluteFile(); m_FileChooser.setSelectedFile(file); } // create plot Instances result; try { result = m_FileChooser.getLoader().getDataSet(); } catch (Exception e) { GUIHelper.showErrorMessage(getOwner(), "Error loading file '" + file + "':\n" + adams.core.Utils.throwableToString(e)); return; } result.setClassIndex(result.numAttributes() - 1); ThresholdVisualizePanel vmc = new ThresholdVisualizePanel(); vmc.setROCString("(Area under ROC = " + Utils.doubleToString(ThresholdCurve.getROCArea(result), 4) + ")"); vmc.setName(result.relationName()); PlotData2D tempd = new PlotData2D(result); tempd.setPlotName(result.relationName()); tempd.addInstanceNumberAttribute(); // specify which points are connected boolean[] cp = new boolean[result.numInstances()]; for (int n = 1; n < cp.length; n++) cp[n] = true; try { tempd.setConnectPoints(cp); vmc.addPlot(tempd); } catch (Exception e) { GUIHelper.showErrorMessage(getOwner(), "Error adding plot:\n" + adams.core.Utils.throwableToString(e)); return; } ChildFrame frame = createChildFrame(vmc, GUIHelper.getDefaultDialogDimension()); frame.setTitle(frame.getTitle() + " - " + file); }
From source file:adams.gui.tools.previewbrowser.InstanceExplorerHandler.java
License:Open Source License
/** * Creates the actual view.// ww w .ja va 2s .co m * * @param file the file to create the view for * @return the view */ @Override protected PreviewPanel createPreview(File file) { InstancePanel result; Instances dataset; int i; Instance inst; List<InstanceContainer> data; result = new InstancePanel(); try { data = new ArrayList<InstanceContainer>(); dataset = DataSource.read(file.getAbsolutePath()); for (i = 0; i < dataset.numInstances(); i++) { inst = new Instance(); inst.set(dataset.instance(i)); inst.setID((i + 1) + "." + dataset.relationName()); data.add(result.getContainerManager().newContainer(inst)); } result.getContainerManager().addAll(data); } catch (Exception e) { getLogger().log(Level.SEVERE, "Failed to load dataset '" + file + "':", e); } return new PreviewPanel(result); }
From source file:adams.gui.visualization.debug.inspectionhandler.WekaInstances.java
License:Open Source License
/** * Returns further inspection values.//from w w w . j av a 2 s . c o m * * @param obj the object to further inspect * @return the named inspected values */ @Override public Hashtable<String, Object> inspect(Object obj) { Hashtable<String, Object> result; Instances data; Instance inst; result = new Hashtable<String, Object>(); if (obj instanceof Instances) { data = (Instances) obj; inst = null; } else { inst = (Instance) obj; data = inst.dataset(); } result.put("relation", data.relationName()); result.put("num attributes", data.numAttributes()); result.put("class attribute", (data.classIndex() == -1) ? "-none-" : ((data.classIndex() + 1) + " (" + data.classAttribute().name() + ")")); if (inst == null) { result.put("num instances", data.numInstances()); result.put("instances", data.toArray()); } return result; }
From source file:adams.gui.visualization.debug.objectrenderer.WekaInstancesRenderer.java
License:Open Source License
/** * Performs the actual rendering.//from ww w . j a v a 2 s .co m * * @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
/** * pops up SQL Viewer for SQL statement. *//* w w w .j a va 2 s. c o m*/ public void loadDataFromDatabase() { InstanceQuery query; List<InstanceContainer> data; Instances dataset; Instance inst; int i; if (m_LoadFromDatabaseDialog == null) { if ((getParentFrame() != null) && (getParentFrame() instanceof JFrame)) m_LoadFromDatabaseDialog = new SqlViewerDialog((JFrame) getParentFrame()); else m_LoadFromDatabaseDialog = new SqlViewerDialog(null); } m_LoadFromDatabaseDialog.setVisible(true); if (m_LoadFromDatabaseDialog.getReturnValue() != JOptionPane.OK_OPTION) return; try { showStatus("Executing query: " + m_LoadFromDatabaseDialog.getQuery()); query = new InstanceQuery(); query.setDatabaseURL(m_LoadFromDatabaseDialog.getURL()); query.setUsername(m_LoadFromDatabaseDialog.getUser()); query.setPassword(m_LoadFromDatabaseDialog.getPassword()); query.setQuery(m_LoadFromDatabaseDialog.getQuery()); query.setSparseData(m_LoadFromDatabaseDialog.getGenerateSparseData()); if (query.isConnected()) query.disconnectFromDatabase(); query.connectToDatabase(); showStatus("Loading data..."); data = new ArrayList<>(); dataset = query.retrieveInstances(); for (i = 0; i < dataset.numInstances(); i++) { inst = new Instance(); inst.set(dataset.instance(i)); inst.setID(dataset.relationName() + "." + i); data.add(getContainerManager().newContainer(inst)); showStatus("Loading data " + (i + 1) + "/" + dataset.numInstances()); } loadData(dataset, data); } catch (Exception e) { GUIHelper.showErrorMessage(this, "Failed to load data from database:\n" + Utils.throwableToString(e), "Database error"); } showStatus(""); }
From source file:adams.gui.visualization.instance.InstanceExplorer.java
License:Open Source License
/** * pops up file dialog for loading dataset form disk. * * @param file an optional file, use null to ignore *///from w w w.j a v a 2 s .co m public void loadDataFromDisk(File file) { if (m_LoadFromDiskDialog == null) { if (getParentDialog() != null) m_LoadFromDiskDialog = new LoadDatasetDialog(getParentDialog()); else m_LoadFromDiskDialog = new LoadDatasetDialog(getParentFrame()); m_LoadFromDiskDialog.setCurrent(new File(getProperties().getPath("InitialDir", "%h"))); m_LoadFromDiskDialog.setDefaultAttributeRange(getProperties().getPath("AttributeRange", "first-last")); m_LoadFromDiskDialog.setDefaultClassIndex(getProperties().getPath("ClassIndex", "")); m_LoadFromDiskDialog.setDefaultSortIndex(getProperties().getPath("SortIndex", "")); m_LoadFromDiskDialog.setDefaultIncludeAttributes(Attribute.NUMERIC, getProperties().getBoolean("IncludeNumericAttributes", true)); m_LoadFromDiskDialog.setDefaultIncludeAttributes(Attribute.DATE, getProperties().getBoolean("IncludeDateAttributes", false)); m_LoadFromDiskDialog.setDefaultIncludeAttributes(Attribute.NOMINAL, getProperties().getBoolean("IncludeNominalAttributes", false)); m_LoadFromDiskDialog.setDefaultIncludeAttributes(Attribute.STRING, getProperties().getBoolean("IncludeStringAttributes", false)); m_LoadFromDiskDialog.setDefaultIncludeAttributes(Attribute.RELATIONAL, getProperties().getBoolean("IncludeRelationalAttributes", false)); m_LoadFromDiskDialog.setAcceptListener((ChangeEvent e) -> { int[] indices = m_LoadFromDiskDialog.getIndices(); if (indices == null) return; if (m_RecentFilesHandler != null) m_RecentFilesHandler.addRecentItem(m_LoadFromDiskDialog.getCurrent()); HashSet<Integer> attTypes = new HashSet<>(); if (m_LoadFromDiskDialog.getIncludeAttributes(Attribute.NUMERIC)) attTypes.add(Attribute.NUMERIC); if (m_LoadFromDiskDialog.getIncludeAttributes(Attribute.DATE)) attTypes.add(Attribute.DATE); if (m_LoadFromDiskDialog.getIncludeAttributes(Attribute.NOMINAL)) attTypes.add(Attribute.NOMINAL); if (m_LoadFromDiskDialog.getIncludeAttributes(Attribute.STRING)) attTypes.add(Attribute.STRING); if (m_LoadFromDiskDialog.getIncludeAttributes(Attribute.RELATIONAL)) attTypes.add(Attribute.RELATIONAL); showStatus("Loading data..."); List<InstanceContainer> data = new ArrayList<>(); Instances dataset = m_LoadFromDiskDialog.getDataset(); int[] additional = m_LoadFromDiskDialog.getAdditionalAttributes(); Range range = m_LoadFromDiskDialog.getCurrentAttributeRange(); int id = m_LoadFromDiskDialog.getCurrentIDIndex(); for (int i = 0; i < indices.length; i++) { weka.core.Instance winst = dataset.instance(indices[i]); Instance inst = new Instance(); inst.set(winst, i, additional, range, attTypes); if (id == -1) { inst.setID((indices[i] + 1) + "." + dataset.relationName()); } else { if (winst.attribute(id).isNumeric()) inst.setID("" + winst.value(id)); else inst.setID(winst.stringValue(id)); } data.add(getContainerManager().newContainer(inst)); showStatus("Loading data " + (i + 1) + "/" + dataset.numInstances()); } loadData(dataset, data); showStatus(""); }); } if (file != null) m_LoadFromDiskDialog.setCurrent(file); m_LoadFromDiskDialog.setVisible(true); }