List of usage examples for weka.core Instances setRelationName
public void setRelationName(String newName)
From source file:adams.data.conversion.SpreadSheetToWekaInstances.java
License:Open Source License
/** * Performs the actual conversion.//from w w w. j a v a2s .c om * * @return the converted data * @throws Exception if something goes wrong with the conversion */ @Override protected Object doConvert() throws Exception { Instances result; SpreadSheet sheet; DenseInstance inst; ArrayList<Attribute> atts; HashSet<String> unique; ArrayList<String> labels; Row row; Cell cell; int i; int n; double[] values; Collection<ContentType> types; ContentType type; boolean added; int[] classIndices; sheet = (SpreadSheet) m_Input; // create header atts = new ArrayList<>(); for (i = 0; i < sheet.getColumnCount(); i++) { added = false; types = sheet.getContentTypes(i); if (types.contains(ContentType.DOUBLE)) types.remove(ContentType.LONG); if (types.contains(ContentType.LONG)) { types.add(ContentType.DOUBLE); types.remove(ContentType.LONG); } if (types.size() == 1) { type = (ContentType) types.toArray()[0]; if (type == ContentType.DOUBLE) { atts.add(new Attribute(sheet.getHeaderRow().getCell(i).getContent())); added = true; } else if (type == ContentType.DATE) { atts.add(new Attribute(sheet.getHeaderRow().getCell(i).getContent(), Constants.TIMESTAMP_FORMAT)); added = true; } else if (type == ContentType.TIME) { atts.add(new Attribute(sheet.getHeaderRow().getCell(i).getContent(), Constants.TIME_FORMAT)); added = true; } } if (!added) { unique = new HashSet<>(); for (n = 0; n < sheet.getRowCount(); n++) { row = sheet.getRow(n); cell = row.getCell(i); if ((cell != null) && !cell.isMissing()) unique.add(cell.getContent()); } if ((unique.size() > m_MaxLabels) || (m_MaxLabels < 1)) { atts.add(new Attribute(sheet.getHeaderRow().getCell(i).getContent(), (FastVector) null)); } else { labels = new ArrayList<>(unique); Collections.sort(labels); atts.add(new Attribute(sheet.getHeaderRow().getCell(i).getContent(), labels)); } } } result = new Instances(Environment.getInstance().getProject(), atts, sheet.getRowCount()); if (sheet.hasName()) result.setRelationName(sheet.getName()); // add data for (n = 0; n < sheet.getRowCount(); n++) { row = sheet.getRow(n); values = new double[result.numAttributes()]; for (i = 0; i < result.numAttributes(); i++) { cell = row.getCell(i); values[i] = weka.core.Utils.missingValue(); if ((cell != null) && !cell.isMissing()) { if (result.attribute(i).type() == Attribute.DATE) { if (cell.isTime()) values[i] = cell.toTime().getTime(); else values[i] = cell.toDate().getTime(); } else if (result.attribute(i).isNumeric()) { values[i] = Utils.toDouble(cell.getContent()); } else if (result.attribute(i).isString()) { values[i] = result.attribute(i).addStringValue(cell.getContent()); } else { values[i] = result.attribute(i).indexOfValue(cell.getContent()); } } } inst = new DenseInstance(1.0, values); result.add(inst); } if (sheet instanceof Dataset) { classIndices = ((Dataset) sheet).getClassAttributeIndices(); if (classIndices.length > 0) result.setClassIndex(classIndices[0]); } return result; }
From source file:adams.data.io.output.AbstractWekaSpreadSheetWriter.java
License:Open Source License
/** * Performs the actual writing. The caller must ensure that the output stream * gets closed.// www . jav a 2 s. co m * * @param content the spreadsheet to write * @param out the output stream to write the spreadsheet to * @return true if successfully written */ @Override protected boolean doWrite(SpreadSheet content, OutputStream out) { boolean result; Instances data; SpreadSheetToWekaInstances convert; String msg; result = false; try { convert = new SpreadSheetToWekaInstances(); convert.setInput(content); msg = convert.convert(); if (msg == null) { data = (Instances) convert.getOutput(); if (data.relationName().equals(Environment.getInstance().getProject())) { if (content.hasName()) data.setRelationName(content.getName()); } m_Saver.setInstances(data); if (m_Stopped) return false; m_Saver.setDestination(out); m_Saver.writeBatch(); result = true; } else { getLogger().severe("Failed to convert spreadsheet into WEKA Instances:\n" + msg); result = false; } convert.cleanUp(); } catch (Exception e) { getLogger().log(Level.SEVERE, "Failed to save dataset!", e); result = false; } return result; }
From source file:adams.flow.transformer.WekaFilter.java
License:Open Source License
/** * Executes the flow item.//w w w . jav a2s. c o m * * @return null if everything is fine, otherwise error message */ @Override protected String doExecute() { String result; weka.core.Instances data; weka.core.Instances filteredData; weka.core.Instance inst; adams.data.instance.Instance instA; weka.core.Instance filteredInst; String relation; result = null; data = null; inst = null; if (m_InputToken.hasPayload(weka.core.Instance.class)) inst = m_InputToken.getPayload(weka.core.Instance.class); else if (m_InputToken.hasPayload(adams.data.instance.Instance.class)) inst = m_InputToken.getPayload(adams.data.instance.Instance.class).toInstance(); else if (m_InputToken.hasPayload(weka.core.Instances.class)) data = m_InputToken.getPayload(weka.core.Instances.class); else result = m_InputToken.unhandledData(); if (result == null) { try { // initialize filter? if (!m_Initialized || !m_InitializeOnce) { if (data == null) { data = new weka.core.Instances(inst.dataset(), 0); data.add(inst); } initActualFilter(data); } synchronized (m_ActualFilter) { if (!m_FlowContextUpdated) { m_FlowContextUpdated = true; if (m_ActualFilter instanceof FlowContextHandler) ((FlowContextHandler) m_ActualFilter).setFlowContext(this); } // filter data filteredData = null; filteredInst = null; if (data != null) { relation = data.relationName(); filteredData = weka.filters.Filter.useFilter(data, m_ActualFilter); if (m_KeepRelationName) { filteredData.setRelationName(relation); if (isLoggingEnabled()) getLogger().info("Setting relation name: " + relation); } m_Initialized = true; } else { relation = inst.dataset().relationName(); m_ActualFilter.input(inst); m_ActualFilter.batchFinished(); filteredInst = m_ActualFilter.output(); if (m_KeepRelationName) { filteredInst.dataset().setRelationName(relation); if (isLoggingEnabled()) getLogger().info("Setting relation name: " + relation); } } } // build output token if (inst != null) { if (filteredInst != null) { if (m_InputToken.getPayload() instanceof weka.core.Instance) { m_OutputToken = new Token(filteredInst); } else { instA = new adams.data.instance.Instance(); instA.set(filteredInst); m_OutputToken = createToken(m_InputToken.getPayload(), instA); } } else if ((filteredData != null) && (filteredData.numInstances() > 0)) { m_OutputToken = createToken(m_InputToken.getPayload(), filteredData.instance(0)); } } else { m_OutputToken = createToken(m_InputToken.getPayload(), filteredData); } } catch (Exception e) { result = handleException("Failed to filter data: ", e); } } if (m_OutputToken != null) updateProvenance(m_OutputToken); return result; }
From source file:adams.flow.transformer.WekaInstancesAppend.java
License:Open Source License
/** * Executes the flow item./*from ww w . ja va2s. c o m*/ * * @return null if everything is fine, otherwise error message */ @Override protected String doExecute() { String result; String[] filesStr; File[] files; int i; int n; Instances[] inst; Instances full; String msg; StringBuilder relation; double[] values; result = null; // get filenames files = null; inst = null; if (m_InputToken.getPayload() instanceof String[]) { filesStr = (String[]) m_InputToken.getPayload(); files = new File[filesStr.length]; for (i = 0; i < filesStr.length; i++) files[i] = new PlaceholderFile(filesStr[i]); } else if (m_InputToken.getPayload() instanceof File[]) { files = (File[]) m_InputToken.getPayload(); } else if (m_InputToken.getPayload() instanceof Instances[]) { inst = (Instances[]) m_InputToken.getPayload(); } else { throw new IllegalStateException("Unhandled input type: " + m_InputToken.getPayload().getClass()); } // load data? if (files != null) { inst = new Instances[files.length]; for (i = 0; i < files.length; i++) { try { inst[i] = DataSource.read(files[i].getAbsolutePath()); } catch (Exception e) { result = handleException("Failed to load dataset: " + files[i], e); break; } } } // test compatibility if (result == null) { for (i = 0; i < inst.length - 1; i++) { for (n = i + 1; n < inst.length; n++) { if ((msg = inst[i].equalHeadersMsg(inst[n])) != null) { result = "Dataset #" + (i + 1) + " and #" + (n + 1) + " are not compatible:\n" + msg; break; } } if (result != null) break; } } // append if (result == null) { full = new Instances(inst[0]); relation = new StringBuilder(inst[0].relationName()); for (i = 1; i < inst.length; i++) { relation.append("+" + inst[i].relationName()); for (Instance row : inst[i]) { values = row.toDoubleArray(); for (n = 0; n < values.length; n++) { if (row.attribute(n).isString()) values[n] = full.attribute(n).addStringValue(row.stringValue(n)); else if (row.attribute(n).isRelationValued()) values[n] = full.attribute(n).addRelation(row.relationalValue(n)); } if (row instanceof SparseInstance) row = new SparseInstance(row.weight(), values); else row = new DenseInstance(row.weight(), values); full.add(row); } } full.setRelationName(relation.toString()); m_OutputToken = new Token(full); } return result; }
From source file:adams.flow.transformer.WekaMultiLabelSplitter.java
License:Open Source License
/** * Returns the generated token.//ww w. j ava2s . c o m * * @return the generated token */ @Override public Token output() { Token result; int index; Remove remove; Reorder reorder; StringBuilder indices; int i; int newIndex; Instances processed; result = null; index = m_AttributesToProcess.remove(0); remove = new Remove(); indices = new StringBuilder(); for (i = 0; i < m_ClassAttributes.size(); i++) { if (m_ClassAttributes.get(i) == index) continue; if (indices.length() > 0) indices.append(","); indices.append("" + (m_ClassAttributes.get(i) + 1)); } remove.setAttributeIndices(indices.toString()); try { remove.setInputFormat(m_Dataset); processed = weka.filters.Filter.useFilter(m_Dataset, remove); if (m_UpdateRelationName) processed.setRelationName(m_Dataset.attribute(index).name()); result = new Token(processed); } catch (Exception e) { processed = null; handleException( "Failed to process dataset with following filter setup:\n" + OptionUtils.getCommandLine(remove), e); } if (m_MakeClassLast && (processed != null)) { newIndex = processed.attribute(m_Dataset.attribute(index).name()).index(); indices = new StringBuilder(); for (i = 0; i < processed.numAttributes(); i++) { if (i == newIndex) continue; if (indices.length() > 0) indices.append(","); indices.append("" + (i + 1)); } if (indices.length() > 0) indices.append(","); indices.append("" + (newIndex + 1)); reorder = new Reorder(); try { reorder.setAttributeIndices(indices.toString()); reorder.setInputFormat(processed); processed = weka.filters.Filter.useFilter(processed, reorder); if (m_UpdateRelationName) processed.setRelationName(m_Dataset.attribute(index).name()); result = new Token(processed); } catch (Exception e) { handleException("Failed to process dataset with following filter setup:\n" + OptionUtils.getCommandLine(reorder), e); } } return result; }
From source file:adams.flow.transformer.WekaRenameRelation.java
License:Open Source License
/** * Executes the flow item./*from w ww. ja v a 2 s . c o m*/ * * @return null if everything is fine, otherwise error message */ @Override protected String doExecute() { String result; weka.core.Instance inst; weka.core.Instances data; adams.data.instance.Instance instA; String oldName; String newName; result = null; if (m_InputToken.getPayload() instanceof weka.core.Instance) { inst = (weka.core.Instance) m_InputToken.getPayload(); data = inst.dataset(); } else if (m_InputToken.getPayload() instanceof adams.data.instance.Instance) { inst = ((adams.data.instance.Instance) m_InputToken.getPayload()).toInstance(); data = inst.dataset(); } else { inst = null; data = (weka.core.Instances) m_InputToken.getPayload(); } if (isLoggingEnabled()) getLogger().info("Renaming: " + m_Find + " -> " + m_Replace); // perform rename if (data != null) { oldName = data.relationName(); newName = oldName.replaceAll(m_Find, m_Replace); data.setRelationName(newName); if (isLoggingEnabled()) getLogger().info("Renamed: " + oldName + " -> " + newName); } else { if (isLoggingEnabled()) getLogger().info("weka.core.Instance doesn't have access to dataset?"); } if (inst == null) { m_OutputToken = new Token(data); } else { if (m_InputToken.getPayload() instanceof adams.data.instance.Instance) { instA = new adams.data.instance.Instance(); instA.set(inst); m_OutputToken = new Token(instA); } else { m_OutputToken = new Token(inst); } } return result; }
From source file:adams.flow.transformer.WekaReorderAttributesToReference.java
License:Open Source License
/** * Executes the flow item./*from w ww . j av a 2s .co m*/ * * @return null if everything is fine, otherwise error message */ @Override protected String doExecute() { String result; Instances dataOld; Instance instOld; Instances dataNew; Instance instNew; Attribute att; int i; StringBuilder order; List<Add> adds; Add add; int index; StringBuilder labels; int n; List<Filter> filters; Reorder reorder; result = null; if (m_OnTheFly && (m_Reference == null)) { result = setUpReference(); if (result != null) return result; } dataNew = null; instNew = null; // get input data if (m_InputToken.getPayload() instanceof Instance) { instOld = (Instance) m_InputToken.getPayload(); dataOld = instOld.dataset(); } else { instOld = null; dataOld = (Instances) m_InputToken.getPayload(); } // do we need to initialize filter? if (m_InitializeOnce || (m_Reorder == null)) { // check incoming data if (!m_Lenient) { for (i = 0; i < m_Reference.numAttributes(); i++) { att = m_Reference.attribute(i); if (dataOld.attribute(att.name()) == null) { if (result == null) result = "Missing attribute(s) in incoming data: " + att.name(); else result += ", " + att.name(); } } if (result != null) getLogger().severe(result); } if (result == null) { try { // determine indices order = new StringBuilder(); adds = new ArrayList<Add>(); for (i = 0; i < m_Reference.numAttributes(); i++) { att = m_Reference.attribute(i); if (dataOld.attribute(att.name()) == null) { index = dataOld.numAttributes() + adds.size(); add = new Add(); add.setAttributeIndex("last"); add.setAttributeName(att.name()); add.setAttributeType(new SelectedTag(att.type(), Add.TAGS_TYPE)); if (att.isNominal()) { labels = new StringBuilder(); for (n = 0; n < att.numValues(); n++) { if (labels.length() > 0) labels.append(","); labels.append(att.value(n)); } add.setNominalLabels(labels.toString()); } adds.add(add); } else { index = dataOld.attribute(att.name()).index(); } if (order.length() > 0) order.append(","); order.append((index + 1)); } // build reorder filter reorder = new Reorder(); reorder.setAttributeIndices(order.toString()); // build multifilter filters = new ArrayList<Filter>(); filters.addAll(adds); filters.add(reorder); m_Reorder = new MultiFilter(); m_Reorder.setFilters(filters.toArray(new Filter[filters.size()])); // initialize filter m_Reorder.setInputFormat(dataOld); } catch (Exception e) { result = handleException("Failed to initialize reorder filter!", e); } } } // reorder data if (result == null) { try { if (instOld != null) { m_Reorder.input(instOld); m_Reorder.batchFinished(); instNew = m_Reorder.output(); if (m_KeepRelationName) instNew.dataset().setRelationName(dataOld.relationName()); } else { dataNew = Filter.useFilter(dataOld, m_Reorder); if (m_KeepRelationName) dataNew.setRelationName(dataOld.relationName()); } } catch (Exception e) { result = handleException("Failed to reorder data!", e); instNew = null; dataNew = null; } } if (instNew != null) m_OutputToken = new Token(instNew); else if (dataNew != null) m_OutputToken = new Token(dataNew); return result; }
From source file:adams.flow.transformer.WekaStreamFilter.java
License:Open Source License
/** * Executes the flow item.//from w w w . j av a 2 s .com * * @return null if everything is fine, otherwise error message */ @Override protected String doExecute() { String result; weka.core.Instances data; weka.core.Instance inst; adams.data.instance.Instance instA; weka.core.Instance filteredInst; weka.core.Instances filteredData; String relation; weka.filters.Filter filter; result = null; inst = null; data = null; filteredInst = null; filteredData = null; filter = (weka.filters.Filter) m_Filter; if (m_InputToken.getPayload() instanceof weka.core.Instance) inst = (weka.core.Instance) m_InputToken.getPayload(); else if (m_InputToken.getPayload() instanceof weka.core.Instances) data = (weka.core.Instances) m_InputToken.getPayload(); else inst = ((adams.data.instance.Instance) m_InputToken.getPayload()).toInstance(); if (data == null) data = inst.dataset(); try { // initialize filter? if (!m_Initialized) { result = setUpContainers(filter); if (result == null) result = updateObject(filter); filter.setInputFormat(new weka.core.Instances(data, 0)); } if (result == null) { // filter data relation = data.relationName(); if (inst == null) { filteredData = Filter.useFilter(data, filter); if (m_KeepRelationName) filteredData.setRelationName(relation); } else { filter.input(inst); filter.batchFinished(); filteredInst = filter.output(); if (m_KeepRelationName) filteredInst.dataset().setRelationName(relation); } // build output token if (m_InputToken.getPayload() instanceof weka.core.Instance) { m_OutputToken = new Token(filteredInst); } else if (m_InputToken.getPayload() instanceof weka.core.Instances) { m_OutputToken = new Token(filteredData); } else { instA = new adams.data.instance.Instance(); instA.set(filteredInst); m_OutputToken = new Token(instA); } } } catch (Exception e) { result = handleException("Failed to filter data: ", e); } if (m_OutputToken != null) updateProvenance(m_OutputToken); return result; }
From source file:adams.gui.menu.BatchFilterDatasets.java
License:Open Source License
/** * Performs the batch filtering.//from w w w . j av a 2s.c o m * * @param frame the frame to close * @param input the files to filter * @param filter the filter setup * @param classIndex the class index, empty for no class * @param keep whether to keep the relation name * @param outdir the output directory */ protected void batchFilter(ChildFrame frame, String[] input, Filter filter, String classIndex, boolean keep, File outdir) { Instances[] data; int i; AbstractFileLoader loader; Instances filtered; int clsIndex; String outfile; StringBuilder outfiles; 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]; for (i = 0; i < input.length; i++) { try { loader.setFile(new File(input[i])); data[i] = DataSource.read(loader); } catch (Exception e) { GUIHelper.showErrorMessage(getOwner(), "Failed to read '" + input[i] + "'!\n" + Utils.throwableToString(e)); return; } } // class index if (classIndex.isEmpty()) { clsIndex = -1; } else { try { if (classIndex.equalsIgnoreCase("first")) clsIndex = 0; else if (classIndex.equalsIgnoreCase("last")) clsIndex = data[0].numAttributes() - 1; else clsIndex = Integer.parseInt(classIndex) - 1; } catch (Exception e) { GUIHelper.showErrorMessage(getOwner(), "Failed to parse class attribute index: " + classIndex + "\n" + Utils.throwableToString(e)); return; } } // filter outfiles = new StringBuilder(); for (i = 0; i < input.length; i++) { try { outfile = outdir.getAbsolutePath() + File.separator + new File(input[i]).getName(); data[i].setClassIndex(clsIndex); if (i == 0) filter.setInputFormat(data[i]); filtered = Filter.useFilter(data[i], filter); if (keep) filtered.setRelationName(data[i].relationName()); DataSink.write(outfile, filtered); if (outfiles.length() > 0) outfiles.append("\n"); outfiles.append(outfile); } catch (Exception e) { GUIHelper.showErrorMessage(getOwner(), "Failed to filter dataset #" + (i + 1) + " ('" + input[i] + "')!\n" + Utils.throwableToString(e)); return; } } GUIHelper.showInformationMessage(null, "Successfully filtered!\n" + outfiles); frame.dispose(); }
From source file:adams.opt.genetic.AbstractClassifierBasedGeneticAlgorithm.java
License:Open Source License
/** * Creates a new dataset, with the setup as the new relation name. * * @param data the data to replace the relation name with the setup * @param job the associated job// w w w.j ava 2 s . co m * @return the updated dataset */ public Instances updateHeader(Instances data, GeneticAlgorithmJob job) { Properties props; props = storeSetup(data, job); data.setRelationName(props.toString()); return data; }